code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include<bits/stdc++.h> using namespace std; using ll=long long; constexpr ll mod=1e9+7; vector<vector<int>> graph; vector<vector<bool>> isReach; int n,m; void init() { for (int i = 0; i < n; ++i) { isReach[i][i] = true; queue<int> nxt; nxt.push(i); vector<bool> seen(n, false); while (!nxt.empty()) { auto now = nxt.front(); nxt.pop(); seen[now] = true; isReach[i][now] = true; for (const auto& e: graph[now]) { if (seen[e]) continue; nxt.push(e); } } } } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin>>n>>m; graph.resize(n); for (int i = 0; i < m; ++i) { int a,b; cin>>a>>b; a--; b--; graph[a].push_back(b); } int ans = 0; isReach = vector<vector<bool>>(n, vector<bool>(n, false)); init(); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (isReach[i][j]) ans++; } } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T,class U> using P = pair<T,U>; template<class T> using vec = vector<T>; template<class T> using vvec = vector<vec<T>>; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) int main(){ int n, k; cin >> n >> k; vvec<int> t(n, vec<int>(n, 0)); rep(i, n) rep(j, n) cin >> t[i][j]; int array[n-1]; rep(i, n-1) array[i] = i + 1; int prev, cost, ans; ans = 0; do{ prev = 0; cost = 0; // for(int i=0; i<n-1; i++){ // cout<<array[i]; // if(i!=3)cout<<" "; // } // cout<<endl; for(int i=0; i<n-1; i++){ cost += t[prev][array[i]]; prev = array[i]; } cost += t[prev][0]; if (cost == k) ans++; } while (next_permutation(array,array+n-1)); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (long long i = 0; i < (n); i++) using ll = long long; using P = pair<ll, ll>; template<class T> bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;} template<class T> bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;} // struct Edge { ll to; ll cost; Edge(ll t, ll c) { to = t; cost = c;} }; // using Graph = vector<vector<Edge>>; // using Graph = vector<vector<ll>>; // priority_queue<P, vector<P>, greater<P>> q; int main() { ll n; cin >> n; ll ans = 10010010010; vector<ll> a(n), b(n); rep(i, n) cin >> a[i] >> b[i]; for (ll i = 0; i < n; i++) { for (ll j = 0; j < n; j++) { if (i == j) ans = min(ans, a[i] + b[i]); else { ans = min(ans, max(a[i], b[j])); } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b; cin >> a >> b; if(b>a) { int sum=0; int val=-1; for(int i=0;i<b;i++) { cout << val << " "; sum += val; val -= 1; } int val2=1; for(int i=0;i<a-1;i++) { cout << val2 << " "; sum += val2; val2 += 1; } cout << abs(sum); } else { int sum=0; int val=1; for(int i=0;i<a;i++) { cout << val << " "; sum += val; val += 1; } int val2=-1; for(int i=0;i<b-1;i++) { cout << val2 << " "; sum += val2; val2 -= 1; } cout << -1*sum; } return 0; }
#include <bits/stdc++.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 RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_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 #define RF(x) freopen(x,"r",stdin) #define WF(x) freopen(x,"w",stdout) typedef long long LL; using namespace std; typedef pair<LL,LL> PLL; typedef pair<int,int> PII; const LL MOD = (LL)998244353; const int SIZE = 2e5+5; const LL INF = 1LL<<60; const double eps = 1e-4; const double PI=3.1415926535897932; int a[100009]; int b[100009]; int c[100009]; map<int,vector<int> > m; int main(){ DRI(n); RI(a[n+1]);a[n+1]++; REPP(i,1,n+1)RI(a[i]); REPP(i,1,n+1)RI(b[i]); b[n+1]=a[n+1]; REP(i,n+2){ m[a[i]-i].PB(i); } LL ans = 0; REPP(i,1,n+1){ if(!m.count(b[i]-i)){ puts("-1");return 0; } const vector<int> &v = m[b[i]-i]; auto it = lower_bound(ALL(v),i); if(it==v.end()){ //ans += abs(i-*prev(it)); c[i] = *prev(it); } else if(it==v.begin()){ //ans += abs(*it - i); c[i] = *it; } else{ //ans += min(abs(*it - i),abs(i-*prev(it))); if(abs(*it - i)<abs(i-*prev(it)))c[i] = *it; else c[i] = *prev(it); } } c[n+1]=n+1; REPP(i,1,n+1){ //printf("%d ",c[i]); if(a[i]==b[i])continue; bool same = (c[i]>i && c[i+1] == c[i])||(c[i]<i && c[i-1] == c[i]); if (same) ans++; else { ans += abs(i-c[i]); } } printf("%lld",ans); }
#include<bits/stdc++.h> using namespace std; #define ll long long const int N=1.1e5; int n,m,ta,tb; struct qj{ int l,r; }a[N],b[N]; int main() { cin>>n>>m;int la=0; for(int i=1,x;i<=n;i++){ scanf("%d",&x);if(la+1<=x-1)a[++ta]=(qj){la+1,x-1};la=x;} int x=m;if(la+1<=x)a[++ta]=(qj){la+1,x};la=0; for(int i=1,x;i<=n;i++){ scanf("%d",&x);if(la+1<=x-1)b[++tb]=(qj){la+1,x-1};la=x;} x=m;if(la+1<=x)b[++tb]=(qj){la+1,x};la=0; ll ans=0;int j=1; for(int i=1;i<=tb;i++){//cout<<i<<" "<<ans<<endl; if(a[j].l<b[i].l)ans+=b[i].l-a[j].l; while(b[i].l<=b[i].r){ if(a[j].r-a[j].l>b[i].r-b[i].l)return puts("-1"),0; b[i].l+=(a[j].r-a[j].l+1);if(b[i].l>b[i].r){ if(a[j].r>b[i].r)ans+=a[j].r-b[i].r;j++;break; }j++; } }cout<<ans; }
#include<iostream> #include<algorithm> #include<string> using namespace std; int main() { string s; cin >> s; int cnt = 0; for (int i = 0; i < s.size(); ++i) { string str = s.substr(i, 4); if (str == "ZONe") cnt ++; } cout << cnt << endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define mp make_pair #define fi first #define se second #define REP(i,j) for(int i=0;i<j;i++) #define REPA(i,j) for(int i=1;i<=j;i++) #define FORN(i,j,k) for(int i=j;i<k;i++) #define vi vector<int> #define vvi vector<vi > #define pii pair<int,int> #define vpii vector<pii > #define all(a) a.begin(),a.end() using namespace std; const int INF=1<<30; inline void read(int &x){ // short neg=1; x=0; char c=getchar(); /*while(c<'0'||c>'9'){ if(c=='-')neg=-1; c=getchar(); }*/ while(c>='0'&&c<='9'){ x=(x<<3)+(x<<1)+(c^48),c=getchar(); } // x*=neg; } const int N=3e5+10; int n,k; int a[N]; int cnt[N]; pii minn[N];// without itself int main(void){ read(n);read(k); REP(i,n)read(a[i]); memset(cnt,0,sizeof(cnt)); REP(i,n)cnt[a[i]]++; minn[1]=mp(cnt[0],0); for(int i=2;i<n;i++){ if(cnt[i-1]<minn[i-1].fi){ minn[i]=mp(cnt[i-1],i-1); } else{ minn[i]=minn[i-1]; } } ll ans=0; int m=0,it=0;//cnt[it]==0 while(cnt[it]!=0)it++;//find the first i->0 while(k>0&&cnt[0]-m>0&&it>0){ // cout<<it<<' '<<m<<' '<<ans<<' '<<minn[it].fi<<endl; ans+=it*(min(k,minn[it].fi-m)); k-=minn[it].fi-m; m=minn[it].fi; it=minn[it].se; } printf("%lld",ans); return 0; }
#include<iostream> #include<string> using namespace std; typedef long long ll; int main() { ll n,x; ll a[100001]; ll b = 0,c=0; cin >> n>>x; for (ll i = 0; i < n; i++) { cin >> a[i]; if (a[i] == x) { b += 1; } else { c = i; } } if (b == n) { cout << " " << endl; }else { for (ll i = 0; i < n; i++) { if (i == c) { cout << a[i] << endl; } else { if (a[i] != x) { cout << a[i] << " "; } } } } return 0; }
/** * @author: adityasonani * */ #include <bits/stdc++.h> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define ll long long #define ld long double #define ln "\n" #define fastio ios_base::sync_with_stdio(0); cin.tie(nullptr) #define MOD (int) 1000000007 #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(x) (x).begin(), (x).end() #define debug(x) cerr << #x << " = " << x << endl; #define precise(x) cout << fixed << setprecision(x) #define MAX (int) 10000007 // 10^7 using namespace std; /* */ void solve() { int n, k; cin>>n>>k; rep(i, n) { int a; cin>>a; if(a!=k) cout << a << " "; } } signed main() { fastio; auto start = std::chrono::high_resolution_clock::now(); int t=1; // cin>>t; while(t--) solve(); auto stop = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start); // cerr << "\nTime taken : " << ((long double)duration.count())/((long double) 1e9) <<" s "<< endl; return 0; }
# include <bits/stdc++.h> using namespace std; # define rep(i,a,n) for (int i=a; i<=n; ++i) # define per(i,a,n) for (int i=a; i>=n; --i) # define bug puts("H"); # define pb push_back # define mp make_pair # define all(x) (x).begin(), (x).end() # define SZ(x) (int)x.size() # define fi first # define se second # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mem(a,b) memset(a,b,sizeof(a)) # define INF 1000000001 # define MOD 1000000007 typedef vector<int> Vi; typedef long long i64; typedef pair<int, int> Pi; mt19937 mrand(random_device{}()); int rnd(int x) {return mrand()%x;} i64 powmod(i64 a, i64 b){i64 res=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;} i64 Inv(i64 b){return powmod(b,(i64)MOD-2);} typedef long long ll; const int N = 100005; const double eps = 1e-9; const double inf = 1e18; int a[N], b[N]; Vi odd, even; bool comp(int aa, int bb) {return aa>bb;} int main () { int n; i64 sum = 0; scanf("%d", &n); rep(i,1,n) scanf("%d", a+i), sum += a[i]; rep(i,1,n) { scanf("%d", b+i), b[i] -= a[i]; if (i&1) odd.pb(b[i]); else even.pb(b[i]); } sort(all(odd), comp); sort(all(even), comp); rep(i,0,n/2-1) { if (odd[i]+even[i] > 0) sum += odd[i]+even[i]; } printf("%lld\n", sum); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<int, int>; using VI = vector<int>; #define fst first #define snd second const int N = 1e5 + 5; int a[N], b[N]; //ll dp[N][2][2]; //const ll INF = 1e18; VI diff[2]; int main() { #ifdef LOCAL_EXEC // freopen("sample.in", "r", stdin); // freopen("sample.out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(NULL); #endif int n; cin >> n; ll ans = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; ans += a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; b[i] -= a[i]; } // dp[0][0][1] = dp[0][1][1] = -INF; // for (int i = 1; i <= n; i++) { // for (int p : {0, 1}) { // for (int nb : {0, 1}) { // dp[i][p][nb] = dp[i - 1][p][nb]; // } // } // int p = i & 1; // for (int nb : {0, 1}) { // dp[i][p][nb] = max(dp[i][p][nb], dp[i - 1][1 - p][1 - nb] + b[i]); // } // } // ans += max(dp[n][0][0], dp[n][1][0]); for (int i = 1; i <= n; i++) { diff[i & 1].push_back(b[i]); } sort(diff[0].begin(), diff[0].end()); sort(diff[1].begin(), diff[1].end()); while (!diff[0].empty() && !diff[1].empty()) { ll cur = diff[0].back() + diff[1].back(); diff[0].pop_back(); diff[1].pop_back(); if (cur > 0) ans += cur; } cout << ans << endl; return 0; }
#include<stdio.h> #include<bits/stdc++.h> #define ll long long int #define inf 1000000000000 #define mod 998244353 #define all(v) v.begin(),v.end() #define fi first #define se second #define scan(a) scanf("%d",&a) #define scanl(a) scanf("%lld",&a) #define print(a) printf("%d\n",a) #define printl(a) printf("%lld\n",a) #define prints(a) printf("%d ",a) #define printsl(a) printf("%lld ",a) #define ps(x,y) fixed<<setprecision(y)<<x using namespace std; const int N=303; ll C[N][N]; ll a[N],ans[N]; ll add(ll x,ll y){ ll res=x+y; if(res>=mod)res-=mod; return res; } ll sub(ll x,ll y){ ll res=x-y; if(res<0)res+=mod; return res; } ll mult(ll x,ll y){ return (x*y)%mod; } void solve() { for(int i=0;i<N;i++){ C[i][0]=C[i][i]=1; } for(int i=1;i<N;i++){ for(int j=1;j<i;j++){ C[i][j]=add(C[i-1][j-1],C[i-1][j]); } } int n,k; scan(n),scan(k); while(n--){ ll x; scanl(x); ll p=1,q=1; a[0]=add(a[0],p); for(int i=1;i<=k;i++){ p=mult(x,p); q=mult(2*x,q); a[i]=add(a[i],p); ans[i]=sub(ans[i],q); } } for(int x=1;x<=k;x++){ for(int i=0;i<=x;i++){ ans[x]=add(ans[x],mult(C[x][i],mult(a[x-i],a[i]))); } } for(int i=1;i<=k;i++){ ll x=ans[i]; if(x&1)x+=mod; x/=2; printl(x); } } int main() { // #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // #endif int t = 1; //scan(t); while (t--) { solve(); } }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; //#include<boost/multiprecision/cpp_int.hpp> //#include<boost/multiprecision/cpp_dec_float.hpp> //namespace mp=boost::multiprecision; //#define mulint mp::cpp_int //#define mulfloat mp::cpp_dec_float_100 struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init; #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) //constexpr int MOD=1000000007; constexpr int MOD=998244353; #define INF (1<<30) #define LINF (lint)(1LL<<56) #define endl "\n" #define rep(i,n) for(lint (i)=0;(i)<(n);(i)++) #define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--) #define Flag(x) (1<<(x)) #define Flagcount(x) __builtin_popcountll(x) #define pint pair<int,int> #define pdouble pair<double,double> #define plint pair<lint,lint> #define fi first #define se second typedef long long lint; int dx[8]={1,1,0,-1,-1,-1,0,1}; int dy[8]={0,1,1,1,0,-1,-1,-1}; const int MAX_N=2e5+5; //struct edge{lint to,num;}; //vector<int> bucket[MAX_N/1000]; lint powmod(lint a,lint b){ return b?powmod(a*a%MOD,b/2)*(b%2?a:1)%MOD:1; } lint fac[510000],finv[510000],inv[510000]; void COMinit(){ fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for(int i=2;i<510000;i++){ fac[i]=fac[i-1]*i%MOD; inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD; finv[i]=finv[i-1]*inv[i]%MOD; } } lint COM(lint n,lint k){ if (n<k) return 0; if (n<0 || k<0) return 0; return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD; } lint facdiv(lint n,lint k){ if (n<k) return 0; if (n<0 || k<0) return 0; return (fac[n]*finv[k])%MOD; } int main(void){ COMinit(); lint div2=powmod(2,MOD-2); lint N,K; cin >> N >> K; lint A[N]; rep(i,N) cin >> A[i]; //lint sum=accumulate(A,A+N,0LL); //sum%=MOD; lint powsumk[301]; rep(i,301){ lint suma=0; rep(j,N) suma+=powmod(A[j],i); powsumk[i]=suma%MOD; } for(int i=1;i<=K;i++){ lint ans=0; rep(j,i+1){ lint keisu=COM(i,j); lint res=(powsumk[j]*powsumk[i-j]-powsumk[i]+MOD)%MOD; //cout << powsumk[j] << " " << powsumk[j-i] << endl; res=(res*div2)%MOD; ans=(ans+res*keisu)%MOD; } cout << ans << endl; } }
#include<iostream> #include<vector> #include<algorithm> #include<set> #include<map> #include<queue> #include<cmath> #include<iomanip> #include<cstring> #include<complex> #include<cstdio> #define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1; #define fi first #define se second #define pb push_back #define pii pair<int,int> #define ppi pair<pii,int> #define pip pair<int,pii> #define ll long long #define pll pair<ll,ll> #define rep(i,n) for(int i=0;i<n;i++) #define repd(i,n) for(int i=n-1;i>=0;i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 1000005 #define mid(l,r) l+(r-l)/2 using namespace std; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1}; void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;} ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);} int st[205],en[205]; int dp[205][205]; int rec(int l,int r){ if(dp[l][r]!=-1)return dp[l][r]; int l1=l,r1=(l+r)/2,d=(r-l)/2+1; int f=0; for(;l1<=r1;l1++){ int s=st[l1]; int e=en[l1+d]; //cout<<s<<" "<<l1<<" "<<e<<" "<<l1+d<<"\n"; if(s>0&&s!=l1+d){f=1;break;} if(e>0&&e!=l1){f=1;break;} if(s==-1&&e==-1){f=1;break;} } //cout<<l<<" "<<r<<" "<<f<<"\n"; if(!f)return dp[l][r]=1; dp[l][r]=0; for(int i=l+1;i+1<r;i+=2){ dp[l][r]|=(rec(l,i)&rec(i+1,r)); } return dp[l][r]; } void solve(){ int n; cin>>n; int f=0; for(int i=0;i<n;i++){ int s,e; cin>>s>>e; if(s!=-1){ if(st[s])f=1; st[s]=e; } if(e!=-1){ if(en[e])f=1; en[e]=s; } } if(f){cout<<"No";return;} for(int i=1;i<=2*n;i++){ if(st[i]>0&&st[i]<=i){cout<<"No";return;} if(en[i]>0&&en[i]>=i){cout<<"No";return;} if(st[i]>0&&en[i]>0){cout<<"No";return;} if(st[i]!=0)en[i]=i; else if(en[i]!=0)st[i]=i; } for(int i=0;i<=2*n;i++)for(int j=0;j<=2*n;j++)dp[i][j]=-1; if(rec(1,2*n))cout<<"Yes"; else cout<<"No"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; //cin>>t; t=1; while(t--){ solve(); } }
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <climits> #include <vector> #include <cstring> #include <set> #include <map> #include <stack> #include <queue> #include <unordered_map> #include <unordered_set> #include <string.h> #include <assert.h> #include <iomanip> #include <bitset> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> #define int long long #define ull unsigned long long #define F first #define S second #define nl "\n" #define ld long double #define sortv(v) sort(v.begin(),v.end()) #define rsortv(v) sort(v.rbegin(),v.rend()) #define div 1000000007 #define div2 998244353 #define PI 3.141592653589793 #define inf 1000000000000000000 #define blk 320 #define pb push_back void solve() { int n;cin>>n; vector<pair<int,int>> v; set<int> s; for(int i=0;i<n;i++) { int x,col;cin>>x>>col; --col; v.pb({x,col}); s.insert(col); } int m=s.size(); map<int,int> mp; int idx=0; for(int x:s) mp[x]=idx++; int c[m][2]; for(int i=0;i<m;i++) { c[i][0]=inf; c[i][1]=-inf; } for(int i=0;i<n;i++) { int x=v[i].F,col=v[i].S; col=mp[col]; c[col][0]=min(c[col][0],x); c[col][1]=max(c[col][1],x); } int dp[m][2]; for(int i=0;i<m;i++) dp[i][0]=dp[i][1]=inf; if(c[0][0]<0 && c[0][1]>0) { dp[0][0]=abs(c[0][1])+abs(c[0][1]-c[0][0]); dp[0][1]=abs(c[0][0])+abs(c[0][0]-c[0][1]); } else { if(c[0][1]<0) { dp[0][0]=abs(c[0][0]); dp[0][1]=abs(c[0][0])+abs(c[0][0]-c[0][1]); } else { dp[0][1]=c[0][1]; dp[0][0]=c[0][1]+(c[0][1]-c[0][0]); } } for(int i=1;i<m;++i) { int l=c[i][0],r=c[i][1]; if(r<=c[i-1][0]) dp[i][0]=dp[i-1][0]+abs(c[i-1][0]-l); else dp[i][0]=dp[i-1][0]+abs((r-l))+abs(r-c[i-1][0]); if(r<=c[i-1][1]) dp[i][0]=min(dp[i][0],dp[i-1][1]+abs(c[i-1][1]-l)); else dp[i][0]=min(dp[i][0],dp[i-1][1]+(r-l)+abs(r-c[i-1][1])); if(c[i-1][0]<l) dp[i][1]=min(dp[i][1],dp[i-1][0]+abs(c[i-1][0]-r)); else dp[i][1]=min(dp[i][1],dp[i-1][0]+abs(c[i-1][0]-l)+abs(r-l)); if(c[i-1][1]<l) dp[i][1]=min(dp[i][1],dp[i-1][1]+abs(c[i-1][1]-r)); else dp[i][1]=min(dp[i][1],dp[i-1][1]+abs(c[i-1][1]-l)+abs(r-l)); } int ans=min(dp[m-1][0]+abs(c[m-1][0]),dp[m-1][1]+abs(c[m-1][1])); cout<<ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int t=1; //cin>>t; while(t--) { solve(); } }
#pragma GCC optimize("O3") #include <stdio.h> #include <bits/stdc++.h> int main() { int i, N, A[2][131072]; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d", &(A[0][i])); for (i = 1; i <= N; i++) scanf("%d", &(A[1][i])); int j, k, l, r; const long long inf = -(1LL << 60); long long dp[2][131072]; for (i = 0; i <= N; i++) { dp[0][i] = inf; dp[1][i] = inf; } dp[0][1] = A[0][1]; dp[1][1] = A[1][1]; for (i = 2; i <= N; i++) { if (i % 2 == 0) { dp[0][0] = dp[0][1] + A[0][i]; if (dp[0][0] < dp[1][1] + A[1][i]) dp[0][0] = dp[1][1] + A[1][i]; } else dp[1][0] = dp[0][0]; k = (i < N - i)? i: N - i; l = i % 2; r = 1 - i % 2; long long ar = A[r][i], al = A[l][i]; #pragma unroll(8) for (j = 2 - l; j <= k; j += 2) { dp[0][j] = std::max(dp[0][j-1] + ar, dp[0][j+1] + al); dp[1][j] = std::max(dp[1][j-1] + al, dp[1][j+1] + ar); } } printf("%lld\n", dp[0][0]); fflush(stdout); return 0; }
#include<bits/stdc++.h> typedef long long ll; ll gi(){ ll x=0,f=1; char ch=getchar(); while(!isdigit(ch))f^=ch=='-',ch=getchar(); while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f?x:-x; } #define all(x) (x).begin(),(x).end() template<class T>void cxk(T&a,T b){a=a>b?a:b;} template<class T>void cnk(T&a,T b){a=a<b?a:b;} int a[100010],b[100010]; std::vector<ll>A,B; int main(){ #ifdef LOCAL freopen("in.in","r",stdin); //freopen("out.out","w",stdout); #endif int n=gi(); ll sa=0; for(int i=1;i<=n;++i)sa+=a[i]=gi(); for(int i=1;i<=n;++i)b[i]=gi()-a[i]; A.resize(n/2+1),B.resize(n/2+1); for(int i=1,j=1;i<=n;i+=2,++j)A[j]=b[i],B[j]=b[i+1]; std::sort(A.begin()+1,A.end(),std::greater<ll>()); std::sort(B.begin()+1,B.end(),std::greater<ll>()); for(int i=1;i<=n/2;++i)A[i]+=A[i-1],B[i]+=B[i-1]; ll ans=0;for(int i=0;i<=n/2;++i)cxk(ans,A[i]+B[i]); printf("%lld\n",ans+sa); return 0; }
#include<iostream> #include<algorithm> #include<map> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<set> #define ll long long #define pb push_back #define forn(i,x,n) for(int i=x;i<=n;++i) #define forr(i,x,n) for(int i=n;i>=x;--i) #define lson (rt<< 1) #define rson (rt<< 1 | 1) #define gmid ((l+r)>> 1 ) using namespace std; const int maxn=3050; const int p=1e9+7; ll a[maxn]; ll f[maxn][maxn],s[maxn][maxn],b[maxn][maxn]; int main() { int n; cin>>n; forn(i,1,n) cin>>a[i]; forn(i,1,n) { s[i][0]=s[i-1][0]+a[i]; forn(j,1,n) s[i][j]=(s[i][0]%j); } b[0][0]=1; forn(i,1,n) forr(j,1,i) { f[i][j]=b[s[i][j]][j-1]; b[s[i][j+1]][j]=(b[s[i][j+1]][j]+f[i][j])%p; } ll ans=0; forn(i,1,n) ans=(ans+f[n][i])%p; cout<<ans<<endl; }
#include<bits/stdc++.h> #define f first #define s second #define int long long using namespace std; const int N=3e3+5,mod=1e9+7; int t,a[N],pref[N],dp[N][N],n; vector<int>c[N]; main(){ cin>>n; for(int i=1;i<=n;i++){ cin >> a[i]; pref[i] = pref[i-1] + a[i] ; } dp[0][0] = 1; int ans = 0; for(int i=1;i<=n;i++) { for(int j=0;j<=n;j++) { c[pref[j]%i].push_back(j); } for(int j=0;j<i;j++) { int p = 0; for(int k=0;k<c[j].size();k++) { dp[c[j][k]][i] = p; p += dp[c[j][k]][i-1]; if(p>=mod) p-=mod; } c[j].clear(); } ans += dp[n][i]; if(ans>=mod) ans -= mod; } cout<<ans; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define P pair<int,int> #define fi first #define se second #define rep(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define pb push_back template<class T,class U> inline bool chmax(T &a,U b){ if(a<b){ a=b; return true; } return false; } template<class T,class U> inline bool chmin(T &a,U b){ if(a>b){ a=b; return true; } return false; } constexpr int INF=1000000000; constexpr ll llINF=1000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-8; const double pi=acos(-1); int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int Random(int mi,int ma){ random_device rnd; mt19937 mt(rnd());//32bit //[mi,ma] uniform_int_distribution<int>engine(mi,ma); return engine(mt); } /* vector<vector<ll>>C,sC; void init_comb(int n,int m){ C.resize(n+1,vector<ll>(m+1,0)); sC.resize(n+1,vector<ll>(m+1,0)); C[0][0]=1; for(int i=1;i<=n;i++){ C[i][0]=1; for(int j=1;j<=m;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } rep(i,n+1){ rep(j,m){ sC[i][j+1]=(sC[i][j]+C[i][j])%mod; } } }*/ ll gcd(ll a,ll b){ while(a%b){ a%=b; swap(a,b); } return b; } ll lcm(ll a,ll b){ return a/gcd(a,b)*b; } bool prime(int a){ if(a==1)return false; for(int i=2;i*i<=a;i++){ if(a%i==0)return false; } return true; } ll modpow(ll a,ll b){ ll res=1; while(b){ if(b&1){ res*=a; res%=mod; } a*=a; a%=mod; b>>=1; } return res; } vector<ll>inv,fact,factinv; void init_fact(int n){ inv.resize(n+1); fact.resize(n+1); factinv.resize(n+1); inv[0]=0; inv[1]=1; fact[0]=1; factinv[0]=1; for(ll i=1;i<=n;i++){ if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod); fact[i]=(fact[i-1]*i)%mod; factinv[i]=factinv[i-1]*inv[i]%mod; } } ll _inv(ll a,ll m=mod){ //gcd(a,m) must be 1 ll b=m,u=1,v=0; while(b){ ll t=a/b; a-=t*b;swap(a,b); u-=t*v;swap(u,v); } u%=m; if(u<0)u+=m; return u; } ll comb(int a,int b){ if(a<b)return 0; if(a<0)return 0; return fact[a]*factinv[a-b]%mod*factinv[b]%mod; } ll multicomb(int a,int b){ return comb(a+b-1,b); } int dp[200010]; int main(){ cin.tie(0);ios::sync_with_stdio(false); int n,m,a[200010]; vector<int>g[200010]; cin>>n>>m; rep(i,n){ cin>>a[i]; dp[i]=INF; } rep(i,m){ int x,y; cin>>x>>y; x--,y--; g[x].pb(y); } int ans=-INF; rep(i,n){ chmin(dp[i],a[i]); for(int j:g[i]){ chmin(dp[j],dp[i]); chmax(ans,a[j]-dp[j]); } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define pii pair<int,int> #define x first #define y second using namespace std; int n,m,a[1001],b[1001],dp[1001][1001]; int main(){ cin>>n>>m; for(int k=1;k<=n;k++){ cin>>a[k]; dp[k][0]=k; } for(int k=1;k<=m;k++){ cin>>b[k]; dp[0][k]=k; } if(a[1]!=b[1]) dp[1][1]=1; for(int k=1;k<=n;k++){ for(int i=1;i<=m;i++){ dp[k][i]=dp[k-1][i]+1; dp[k][i]=min(dp[k][i],dp[k][i-1]+1); int gt=dp[k-1][i-1]; if(a[k]!=b[i]) gt++; dp[k][i]=min(dp[k][i],gt); } } cout<<dp[n][m]; return 0; }
#include <iostream> #include <algorithm> #include <cmath> #include <map> #include <vector> #include <set> #include <unordered_map> #include <cstring> #include <string> #include <queue> #include <stack> #include <numeric> #include <iomanip> using namespace std; typedef long long ll; #define int long long #pragma GCC optimize("O2") #define CLR(s) memset(&s, 0, sizeof(s)) #define sz(x) ((long long)(x).size()) #define all(x) x.begin(),x.end() #define rall(x) (x).rbegin(), (x).rend() #define trav(a,x) for (auto& a : x) #define ff first #define ss second #define mp make_pair #define pb push_back #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define leastsigbit(x) __builtin_ffs(x) const int MOD = 1e9 + 7; #define hmap unordered_map #define vi vector<ll> #define vvi vector<vector<ll>> #define pii pair<ll,ll> #define vpii vector<pii> #define tt ll tt;cin >> tt;while(tt--) #define fio ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); #define D1(x) { cerr << " [" << #x << ": " << x << "]\n"; } #define D2(x) { cerr << " [" << #x << ": "; for(auto it:x) cerr << it << " "; cerr << "]\n";} const double PI = acos(-1); ll add(ll x, ll y) {ll res=x+y; return(res>=MOD?res-MOD:res);} ll mul(ll x, ll y) {ll res=x*y; return(res>=MOD?res%MOD:res);} ll sub(ll x, ll y) {ll res=x-y; return(res<0?res+MOD:res);} ll power(ll a,ll b,ll m=MOD){ ll ans=1; a=a%m; while(b>0) { if(b&1) ans=(1ll*a*ans)%m; b>>=1;a=(1ll*a*a)%m;}return ans;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} ll lcm( ll x, ll y) { return (x*y)/gcd(x,y);} bool isprime(ll n){if(n < 2) return 0; ll i = 2; while(i*i <= n){if(n%i == 0) return 0; i++;} return 1;} bool isPowerOfTwo(int x) { /* First x in the below expression is for the case when x is 0 */ return x && (!(x&(x-1))); } double distform(int x, int y, int z, int w) {//(x1,y1,x2,y2) return sqrt(1. * pow(x-z,2) + 1. * pow(y-w,2)); } int dx[] = {-1,1,0,0,-1,1,1,-1}; int dy[] = {0,0,1,-1,1,1,-1,-1}; const int MAXN = 2e5+ 10; const ll inf = 1e18; template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;} int intlog(double base, double x) { return (int)(log(x) / log(base)); } void read(vi & a) { for (int i = 0; i < sz(a); ++i) cin >> a[i];} void solve() { } int32_t main() { string str; cin>>str; set<char> s; for(int i=0;i<str.size();i++){ s.insert(str[i]); } if(s.size()==1) cout<<"Won"<<endl; else cout<<"Lost"<<endl; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i=(a); i<(b); ++i) #define ROF(i, a, b) for (int i=(a); i>(b); --i) #define lg long long #define vi vector<int> template <class T> ostream& operator<<(ostream& os, vector<T>& x) { os << '['; bool first = true; for (T& i : x) { if (!first) os << ", "; first = false; os << i; } return os << ']'; } signed main() { ios::sync_with_stdio(0), cin.tie(0); map<lg, pair<int,int>> mp; lg pi = 1; FOR(i, 1, 1e9) { pi *= 3; if (pi > 1e18) break; lg pj = 1; FOR(j, 1, 1e9) { pj *= 5; if (pi+pj > 1e18) break; mp[pi+pj] = {i, j}; } } lg n; cin >> n; if (mp.count(n)) { cout << mp[n].first << ' ' << mp[n].second; } else { cout << -1; } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0;i < (int)(n); ++i) 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; } typedef long long ll; const int mod = 1e9+7; const int INF = 2000000; const double PI = acos(-1.0); int main() { int N; cin >> N; vector<ll> A(N); rep(i, N) cin >> A[i]; ll ans = 1LL << 35; for(int i = 0; i < (1<<(N-1)); ++i) { vector<ll> v; ll val = A[N-1]; for(int j = 0; j < N-1; ++j) { if(i & (1 << j)) { v.push_back(val); val = 0; } val = val | A[N-2-j]; } v.push_back(val); ll t = 0; for(ll n : v) t = t ^ n; chmin(ans, t); } cout << ans << endl; }
// Created by ... #include <bits/stdc++.h> #define db1(x) cout<<#x<<"="<<x<<'\n' #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n' #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n' #define vll vector< ll > #define M 100000 #define MD 1000000007 #define pb push_back #define rep(i,a,b) for(ll i = a; i <= b; ++i) #define fo(i,a,b) for(int i = a; i<= b; ++i) #define pii pair<ll,ll> #define vec(a) vector<a > #define all(a) a.begin(),a.end() #define se second #define fi first #define inf 0xffffffff #define inchar getchar_unlocked #define outchar(x) putchar_unlocked(x) #define int long long #define endl '\n' #define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) using namespace std; using ll = long long; ll md=MD; ll exp(ll a,ll b){ll r=1ll;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b>>=1;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll Min(ll a,ll b){if(a<b)return a; return b;} ll Max(ll a,ll b){if(a>b)return a; return b;} int32_t main() { IOS; int a,b,c; cin>>a>>b>>c; cout<<a-b+c<<endl; return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <string> #define amax(a, b) a = std::max(a, b) #define amin(a, b) a = std::min(a, b) using ll = long long; // 返り値: a と b の最大公約数 // ax + by = gcd(a, b) を満たす (x, y) が格納される long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } int main() { int T; std::cin >> T; while (T--) { ll X, Y, P, Q; std::cin >> X >> Y >> P >> Q; ll N = (X + Y) * 2, M = (P + Q); ll inf = N * M; ll ans = N * M; ll _a, _b; ll gcd = extGCD(N, M, _a, _b); const ll a = _a, b = _b; // std::cerr << N << ' ' << M << ' ' << a << ' ' << b << std::endl; // a > 0 && b <= 0 for (ll y=0; y<Y; y++) { ll yoff = X + y; for (ll q=0; q<Q; q++) { ll qoff = P + q; ll a0 = a, b0 = b; if (std::abs(qoff - yoff) % gcd != 0) continue; ll r = (qoff - yoff) / gcd; a0 *= r; b0 *= r; if (a0 < 0) { ll dif = (std::abs(a0) + (M / gcd) - 1) / (M / gcd); // ll dif = std::abs(a0) / (M / gcd) + 1; a0 += dif * M / gcd; b0 -= dif * N / gcd; } a0 %= (M / gcd); // a > 0 && b < 0 // std::cerr << y << ' ' << q << ' ' << a0 << ' ' << a0 * N + yoff << std::endl; amin(ans, a0 * N + yoff); } } std::cout << (ans < inf ? std::to_string(ans) : std::string("infinity")) << '\n'; // std::cerr << std::endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define x first #define y second #define FOR(i, m, n) for (ll i(m); i < n; i++) #define DWN(i, m, n) for (ll i(m); i >= n; i--) #define REP(i, n) FOR(i, 0, n) #define DW(i, n) DWN(i, n, 0) #define F(n) REP(i, n) #define FF(n) REP(j, n) #define D(n) DW(i, n) #define DD(n) DW(j, n) using ll = long long; using ld = long double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vtll = vector<tll>; using gr = vector<vll>; using wgr = vector<vpll>; void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); } void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); } template<typename T,typename U> ostream& operator<<(ostream& os, const pair<T,U>& p) { cerr << ' ' << p.x << ',' << p.y; return os; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for(auto x: v) os << ' ' << x; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for(auto x: v) os << ' ' << x; return os; } template<typename T,typename U> ostream& operator<<(ostream& os, const map<T,U>& v) { for(auto x: v) os << ' ' << x; return os; } struct d_ { template<typename T> d_& operator,(const T& x) { cerr << ' ' << x; return *this;} } d_t; #define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; } #define deb(X ...) dbg(#X, "=", X); #define EPS (1e-10) #define INF (1LL<<61) #define YES(x) cout << (x ? "YES" : "NO") << endl; #define CL(A,I) (memset(A,I,sizeof(A))) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() ll mod(ll a, ll b) { return ((a%b)+b)%b; } ll extended_euclid(ll a,ll b,ll &x,ll &y) { if (a == 0) { x = 0; y = 1; return b;} ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } ll mod_inverse(ll a, ll n) { ll x, y; ll d = extended_euclid(a, n, x, y); if (d > 1) return -1; return mod(x,n); } void linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) { ll d = __gcd(a,b); if (c%d) { x = y = -INF; } else { x = c/d * mod_inverse(a/d, b/d); y = (c-a*x)/b; } } void solve() { ll x,y,p,q; cin >> x >> y >> p >> q; ll u = 2*(x+y); ll v = p+q; ll ret = INF; FOR(X,x,x+y) FOR(Y,p,p+q) { ll k,l; linear_diophantine(u,-v,Y-X, k,l); if(k==-INF && l==-INF) continue; ll g = __gcd(u,v); ll s = v/g; if(k<0) k += ((-k+s-1)/s)*s; else k-= s*(k/s); ret = min(ret, k*u+X); } cout << (ret==INF?"infinity":to_string(ret)) << endl; } int main(void) { ios_base::sync_with_stdio(false); ll _T; cin >> _T; REP(_,_T) solve(); return 0; }
/* { ###################### # Author # # Gary # # 2021 # ###################### */ #include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen("check.out","w",stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; inline int read(){ int x=0; char ch=getchar(); while(ch<'0'||ch>'9'){ ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x; } const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ const int MAXN=2e5+233; vector<pair<int,LL> > g[MAXN]; int n,depth[MAXN]; const int MOD=1e9+7; LL ans=0; int t; int cnt[2]; void solve(int now,int pre){ for(auto it:g[now]) if(it.FIR!=pre){ depth[it.FIR]=depth[now]^((it.SEC>>t)&1); solve(it.FIR,now); } cnt[depth[now]]++; } int main(){ // freopen("test.txt","r",stdin); scanf("%d",&n); rb(i,1,n-1){ int u,v; LL w; u=read(); v=read(); scanf("%lld",&w); g[u].PB(II(v,w)); g[v].PB(II(u,w)); } rep(i,60){ cnt[0]=cnt[1]=0; t=i; solve(1,0); LL cnt_=1ll*cnt[0]*cnt[1]%MOD; ans+=(1ll<<t)%MOD*cnt_%MOD; // cout<<cnt_<<endl; ans%=MOD; } cout<<ans<<endl; return 0; }
#pragma GCC optimize("O3") #pragma GCC target("sse4") #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; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multi_set; #define mod 1000000007 #define pi 3.1415926535898 #define eps 1e-9 #define fast ios::sync_with_stdio(0); cin.tie(0);cout.tie(0) #define vt vector #define ar array #define fs first #define sc second #define pb push_back #define sp printf(" ") #define nl '\n' #define all(a) a.begin(),a.end() #define unique(c) (c).resize(unique(all(c)) - (c).begin()) #define sz(x) (int)(x).size() #define revsort(x) sort(all(x));reverse(all(x)) #define set0(a) memset(a,0,sizeof(a)) #define setneg(a) memset(a,-1,sizeof(a)) #define setinf(a) memset(a,126,sizeof(a)) #define REP(i,start,end) for (int i = start; i <=end; i++) #define RREP(i,end,start) for (int i=end; i>=start ;i--) #define EACH(x, a) for (auto& x: a) typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<LL> vll; typedef vector<double> vd; typedef vector<vector<LL> > matrix; typedef vector<vector<pii> > graph; template<class A> void read(vt<A>& v); template<class A, size_t S> void read(ar<A, S>& a); template<class T> void read(T& x) { cin >> x; } void read(double& d) { string t; read(t); d=stod(t); } void read(long double& d) { string t; read(t); d=stold(t); } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template<class A> void read(vt<A>& x) { EACH(a, x) read(a); } template<class A, size_t S> void read(array<A, S>& x) { EACH(a, x) read(a); } LL powm(LL x, LL y,LL m) { if (y==0) return 1; if (y==1) return x%m; if (y&1) return (x*(powm((x*x)%m,y/2,m)))%m; return powm((x*x)%m,y/2,m)%m; } LL inv(LL x, LL m) { return powm(x,m-2,m); } int add(int var1, int var2) { return (((((LL)(var1)) % mod + (LL)(var2)) % mod) + mod) % mod; } int mul(int var1, int var2) { return (((LL)(var1)) % mod * (LL)(var2)) % mod; } LL INF=1e9; graph adj; int n, m; vector<bool> visited; vector<vector<int>> ex; vi ans; void dfs(int i) { visited[i]=1; for(auto tx: adj[i]) { if(!visited[tx.fs]) { if(tx.sc==ans[i]) { ex[tx.fs].pb(ans[i]); } else { ans[tx.fs] = tx.sc; ex[i].pb(tx.sc); } dfs(tx.fs); } } } void solve() { read(n,m); adj.resize(n); ans.assign(n,-1); ex.resize(n); visited.assign(n,0); REP(i,1,m) { int x,y,z; read(x,y,z); x--; y--; adj[x].pb({y,z}); adj[y].pb({x,z}); } ans[0]=1; dfs(0); REP(i,0,n-1) { if(ans[i]==-1) { int ix=1; sort(all(ex[i])); REP(j,0,sz(ex[i])) { if(ix==ex[i][j]) { ix++; continue; } break; } ans[i]=ix; } } EACH(x,ans) cout<<x<<nl; } int main() { fast; solve(); }
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<n;i++) int N; string S; int A[110]; int ret[10010][110]; signed main() { cin >> N >> S; rep(i, N + 1)cin >> A[i]; int ans = 1e10; rep(i, N)ans = min(ans, abs(A[i] - A[i + 1])); rep(i, N + 1) { int idx = 0; while(A[i]) { ret[idx][i]++; idx = (idx + 1) % ans; A[i]--; } } cout << ans << endl; rep(i, ans) { rep(j, N + 1) { if(j)cout << " "; cout << ret[i][j]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using int128 = __int128_t; using int64 = long long; using int32 = int; using uint128 = __uint128_t; using uint64 = unsigned long long; using uint32 = unsigned int; #define ALL(obj) (obj).begin(),(obj).end() template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>; constexpr int64 MOD = 1'000'000'000LL + 7; //' constexpr int64 MOD2 = 998244353; constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL; constexpr int64 LOWINF = 1'000'000'000'000'000LL; //' constexpr long double PI = 3.1415926535897932384626433L; template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);} template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));} template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}} template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} void print(void) {cout << endl;} template <class Head> void print(Head&& head) {cout << head;print();} template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);} template <class T> void chmax(T& a, const T b){a=max(a,b);} template <class T> void chmin(T& a, const T b){a=min(a,b);} vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;} inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;} inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b) void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;} void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;} void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;} /** * @url * @est */ int main() { cin.tie(0);ios::sync_with_stdio(false); int N; cin >> N; string S; cin >> S; vector<int> A(N+1); for(int i=0;i<=N;++i) cin >> A[i]; int d = 100000; for(int i=0;i<N;++i) chmin(d,abs(A[i]-A[i+1])); vector<vector<int>> ans(d,vector<int>(N+1)); for(int i=0;i<=N;++i) { int q = A[i]/d, r = A[i]%d; for(int j=0;j<d;++j) { ans[j][i] = q; } for(int j=0;j<r;++j) { ans[j][i] += 1; } } cout << ans.size() << endl; for(auto v:ans) { for(int i=0;i<=N;++i) cout << v[i] << " \n"[i==N]; } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vpii vector<pair<int,int>> #define vpll vector<pair<ll,ll>> #define fr(i,k,n) for (int i = k; i < n; ++i) #define fri(i,k,n) for (int i = k; i >= n; --i) #define pb push_back #define mp make_pair #define all(arr) arr.begin(),arr.end() #define ff first #define ss second const double pi=3.1415926535897932384626433832795; const int inf=1e9; const ll inf2=1e18; const int mod=1e9+7; void boost(){ ios_base::sync_with_stdio(false); cin.tie(NULL); } void solve(){ int n,m,k; cin>>n>>m>>k; vector<bool> mark(n+1,false); int x; fr(i,0,k){ cin>>x; mark[x]=true; } x=0; fr(i,0,n+1){ if(mark[i]) x++; else x=0; if(x>=m){ cout<<-1; return; } } vector<ld> val(n+1,0),coeff(n+1,0); ld curval=0,curc=0; for(int i=n-1;i>=0;i--){ if(i+m+1<n){ curval-=val[i+m+1]; curc-=coeff[i+m+1]; } curc+=coeff[i+1]; curval+=val[i+1]; if(mark[i]){ coeff[i]=1; continue; } val[i]=(curval)/(1.0*m) +1.0; coeff[i]=curc/(1.0*m); } ld y=1.0-coeff[0]; ld ans=(val[0]/y); cout<<fixed<<setprecision(5)<<ans<<endl; return; } int main() { boost(); int tc=1; //cin>>tc; while(tc--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rep1(i,n) for(ll i=1;i<=(ll)(n);i++) #define LOCAL 1; #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) { out << "["; size_t last = v.size() - 1; for (size_t i = 0; i < v.size(); ++i) { out << v[i]; if (i != last) out << ", "; } out << "]"; return out; } template<typename F, typename S> ostream& operator<< (ostream& out, const pair<F,S>& p) { out << "[" << p.first << ", " << p.second << "]"; return out; } const int mod = 998244353; template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(ll n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { ll t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt< mod >; int main(){ ios::sync_with_stdio(false); cin.tie(0); ll h,w,k;cin >> h >> w >> k; vector<vector<char>> s(h,vector<char>(w,'.')); rep(i,k){ ll hi,wi; char c; cin >> hi >> wi >> c; hi--;wi--; s[hi][wi] = c; } vector<vector<modint>> dp(h,vector<modint>(w,0)); dp[0][0] = ((modint) 3).pow(h*w-k); modint fac = ((modint)2)/((modint)3); rep(hi,h)rep(wi,w){ if(hi!=h-1){ if(s[hi][wi]=='D') dp[hi+1][wi]+=dp[hi][wi]; else if(s[hi][wi]=='X') dp[hi+1][wi]+=dp[hi][wi]; else if(s[hi][wi]=='.') { dp[hi+1][wi]+=fac*dp[hi][wi]; // dbg(dp[hi][wi]); // dbg(fac*dp[hi][wi]); } } if(wi!=w-1){ if(s[hi][wi]=='R') dp[hi][wi+1]+=dp[hi][wi]; else if(s[hi][wi]=='X') dp[hi][wi+1]+=dp[hi][wi]; else if(s[hi][wi]=='.') dp[hi][wi+1]+=fac*dp[hi][wi]; } } // dbg(dp); cout << dp[h-1][w-1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define per(i, a, b) for(int i = (b)-1; i >= (a); --i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pii> vii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int a, b; cin >> a >> b; cout << fixed << setprecision(10) << double(a) * double(b) / 100.0 << '\n'; }
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e5; const int mod = 998244353; int32_t main() { double a, b; cin >> a >> b; cout << (a * b) / 100; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i=0; i<(n); ++i) #define FOR(i, a, n) for (int i=(a); i<(n); ++i) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define DUMP(x) cerr<<#x<<" = "<<(x)<<endl #define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl; template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; REP(i, SZ(v)) { if (i) os << ", "; os << v[i]; } return os << "]"; } template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << " " << p.second << ")"; } 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; } using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; const ll MOD = 1e9 + 7; const int INF = INT_MAX / 2; const ll LINF = LLONG_MAX / 2; const ld eps = 1e-9; template<int m> struct mint { int x; mint(ll x = 0) : x(((x % m) + m) % m) {} mint operator-() const { return x ? m - x : 0; } mint &operator+=(mint r) { if ((x += r.x) >= m) x -= m; return *this; } mint &operator-=(mint r) { if ((x -= r.x) < 0) x += m; return *this; } mint &operator*=(mint r) { x = ((ll) x * r.x) % m; return *this; } mint inv() const { return pow(m - 2); } mint &operator/=(mint r) { return *this *= r.inv(); } friend mint operator+(mint l, mint r) { return l += r; } friend mint operator-(mint l, mint r) { return l -= r; } friend mint operator*(mint l, mint r) { return l *= r; } friend mint operator/(mint l, mint r) { return l /= r; } mint pow(ll n) const { mint ret = 1, tmp = *this; while (n) { if (n & 1) ret *= tmp; tmp *= tmp, n >>= 1; } return ret; } friend bool operator==(mint l, mint r) { return l.x == r.x; } friend bool operator!=(mint l, mint r) { return l.x != r.x; } friend ostream &operator<<(ostream &os, mint a) { return os << a.x; } friend istream &operator>>(istream &is, mint &a) { ll x; is >> x; a = x; return is; } }; using Int = mint<MOD>; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int h, w; cin >> h >> w; vector<vector<bool>> v(h, vector<bool>(w)); REP(i, h) { string s; cin >> s; REP(j, w) { v[i][j] = s[j] == '.'; } } vector<vector<Int>> dp(h, vector<Int>(w, 0)); dp[0][0] = 1; // 右移動 vector<vector<Int>> su1(h+1, vector<Int>(w+1)); // 下移動 vector<vector<Int>> su2(h+1, vector<Int>(w+1)); // 右下移動 vector<vector<Int>> su3(h+1, vector<Int>(w+1)); su1[1][1] = 1; su2[1][1] = 1; su3[1][1] = 1; REP(i, h) { REP(j, w) { if(i == 0 && j == 0) continue; if(!v[i][j]) continue; dp[i][j] = su1[i+1][j] + su2[i][j+1] + su3[i][j]; su1[i+1][j+1] = su1[i+1][j] + dp[i][j]; su2[i+1][j+1] = su2[i][j+1] + dp[i][j]; su3[i+1][j+1] = su3[i][j] + dp[i][j]; } } cout << dp[h-1][w-1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = (int) 2e5 + 7; const int A = 18; const int M = (int) 1e9 + 7; int add(int a, int b) { a += b; if (a >= M) return a - M; if (a < 0) return a + M; return a; } int mul(int a, int b) { return a * (ll) b % M; } int n, k, dig[N], f[A], pre, dp[A][N][A]; string s; void compute_dp() { for (int bef = 0; bef <= 16; bef++) { dp[bef][0][bef] = 1; for (int i = 0; i <= n; i++) { for (int c = bef; c <= 16; c++) { dp[bef][i + 1][c] = add(dp[bef][i + 1][c], mul(c, dp[bef][i][c])); dp[bef][i + 1][c + 1] = add(dp[bef][i + 1][c + 1], mul(16 - c, dp[bef][i][c])); } } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); ///freopen ("input", "r", stdin); cin >> s >> k; n = (int) s.size(); for (int i = 1; i <= n; i++) { char c = s[i - 1]; if ('0' <= c && c <= '9') dig[i] = c - '0'; else dig[i] = 10 + c - 'A'; } compute_dp(); int ret = 0; for (int dim = 1; dim < n; dim++) { int init = ret; ret = add(ret, dp[1][dim - 1][k]); } ret = mul(ret, 15); for (int dif = 1; dif <= n; dif++) { if (dif - 1 >= 1) { f[dig[dif - 1]]++; pre += (f[dig[dif - 1]] == 1); } int start = 0; if (dif == 1) { start = 1; } int init = ret; for (int x = start; x < dig[dif]; x++) { int before = pre + (f[x] == 0); ret = add(ret, dp[before][n - dif][k]); } } int cntdif = 1; sort(s.begin(), s.end()); for (int i = 1; i < n; i++) cntdif += (s[i] != s[i - 1]); ret = add(ret, (cntdif == k)); cout << ret << "\n"; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define FOR(i,a,b) for(int i=a; i<=b; i++) #define all(v) v.begin(), v.end() #define F first #define S second #define INF 2147483647 #define INFLL 1000000000000000000 #define MOD 998244353 #define MOD2 1000000007 #define PI 3.14159265359 #define dij priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> #define cout_all(v) for(auto e:v)cout<<e<<" ";cout<<endl #define smart(v) v.erase(unique(all(v)),v.end()) typedef vector<vector<int>> graph; typedef long long ll; typedef pair<int,int> pii; typedef map<int,int> mii; typedef pair<ll,ll> pll; vector<pii> mov={make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; // int main(){ int n;cin>>n; string s;cin>>s; int ans=0; rep(i,n){ map<char,int> mp; FOR(j,i,n-1){ mp[s[j]]++; if(mp['A']==mp['T']&&mp['G']==mp['C'])ans++; } } cout<<ans<<endl; }
#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; #define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update> */ typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define rep(i,a,b) for(ll i=a;i<=b;++i) #define rrep(i,a,b) for(ll i=a;i>=b;--i) #define FOR(i,n) for(ll i=0;i<n;i++) #define pb push_back #define mp make_pair #define PI 3.14159265358979323846 #define fi first #define se second #define all(x) x.begin(),x.end() #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); const ll INF = 1e18+7; const ll mod = 1e9+7; const ll MAXN = 1e4+10; ll poww(ll a, ll b) { if(b<0) return 0LL; ll ans = 1; while (b) { if (b & 1)ans = ans * a; a = a * a; b >>= 1; } return ans; } ll binpow(ll a, ll b, ll m) { if (b < 0) return 0LL; if (a <= 0)return 0LL; a %= m; ll ans = 1LL; while (b) { if (b & 1)ans = ans * a % m; a = a * a % m; b >>= 1; } return ans; } ll modinv(ll n) { return binpow(n, mod - 2, mod); } void solve() { ll n,k; cin>>n>>k; vector<vector<ll>> dp(3*n+10, vector<ll> (3,0)); rep(i,1,n) { dp[i][0] = 1 + dp[i-1][0]; }; rep(i,n+1,3*n) dp[i][0] = n; for(ll i=1; i<=3*n; i++) { dp[i][1] = dp[i-1][1] + dp[i-1][0] - (i-n-1>=0 ? dp[i-n-1][0] : 0); dp[i][2] = dp[i-1][1] - (i-n-1>=0 ? dp[i-n-1][1] : 0); } ll x = 1,ct=0; while(ct + dp[x][2] < k) { ct += dp[x][2]; x++; } ll rem = k - ct; ll ans[3]; ll y = 1; while(true){ int ss = x - y; int z = max(0ll, n - abs(n + 1 - ss)); if(z >= rem) break; rem -= z; y++; } ans[0] = y; ct = 0; ll ns = x - ans[0]; rep(i,1,n) { if(ns-i>=1 && ns-i<=n) { ct++; } if(ct==rem) { ans[1] = i; break; } } ans[2] = x - ans[0] - ans[1]; cout<<ans[0]<<" "<<ans[1]<<" "<<ans[2]; } int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif //ll no_of_test_cases; cin>>no_of_test_cases; ll no_of_test_cases = 1; //cout<<setprecision(9); for(ll i=1; i<=no_of_test_cases; i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n,m,t; cin >> n >> m; vector<char> str; for(int i=0; i<m; i++) { string s; cin >> s; if(i == 0) t = s.size(); for(int j=0; j<s.size(); j++) str.push_back(s.at(j)); } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cout << str.at(i*20 + j + t); } cout << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<n; i++) #define rng(i,l,r) for (int i=l; i<r; i++) #define V(t) vector<t> #define VV(t) V(vector<t>) using namespace std; using ll = long long; using P = pair<int,int>; const int INF = 1 << 30; template<class T> bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; } template<class T> bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; } int main() { int N, M; cin >> N >> M; bool ok = false; vector<P> res; if (M >= 0) { int s = 1; res.push_back(make_pair(s, s+(M+1)*2+1)); N--; // Mの差を作る if (M > 0) { rep(i, M+1) { s++; N--; res.push_back(make_pair(s, s+1)); s++; } } else { s+=(M+1)*2+1; } ok = N >= 0; // 埋める s++; while (N > 0) { s++; N--; res.push_back(make_pair(s, s+1)); s++; } } if (ok) { for (auto p : res) { cout << p.first << " " << p.second << endl; } } else { cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define dump(...) #endif #define endl '\n' //#define int long long #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define INF 2147483647 #define LLINF 9223372036854775807LL #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define BIT(n) (1LL << (n)) #define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper) #define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower) #define FILLZERO(s, n) \ do { \ ostringstream os; \ os << setw(n) << setfill('0') << s; \ s = os.str(); \ } while (0); #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと using ll = long long; using vi = vector<int>; using vvi = vector<vector<int>>; using pi = pair<int, int>; template <typename T> using Pq = priority_queue<T>; template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>; template <typename T> vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); } template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } 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; } template <typename T> bool isProductOverflow(T a, T b) { return (a > numeric_limits<T>::max() / b); } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } __attribute__((constructor)) void ioInit() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << std::fixed << std::setprecision(10); } constexpr ll MOD = 1e9 + 7; // constexpr ll MOD = 998244353; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z; string S, T; ll ans = 0; signed main() { cin >> N; vi v(N + 1, 0); v[1] = 1; REPS(i, N) { for (int j = 2 * i; j <= N; j += i) { dump(j); chmax(v[j], v[i] + 1); } } REPS(i, N) { cout << v[i] << endl; } }
#include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> s(n+1); s.at(1) = 1; cout << 1 << " "; for(int i = 2; i <= n; ++i){ for(int j = i/2; j >= 1;--j){ if(i % j == 0){ int k = s[j] + 1; cout << k << " "; s[i] = k; break; } } } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<complex<double>> st(N), en(N); for(int i = 0; i < N; i++) { int a, b; cin >> a >> b; st[i] = {a * 1.0 , b * 1.0}; } set<pair<double, double>> in; for(int i = 0; i < N; i++) { int c, d; cin >> c >> d; en[i] = {c * 1.0, d * 1.0}; in.insert({c, d}); } if(N == 1) { cout << "Yes" << "\n"; return 0; } for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(j == i) continue; complex<double> rot = (en[i] - en[j]) / (st[0] - st[1]); complex<double> trans = en[i] - st[0] * rot; double norm = real(rot) * real(rot) + imag(rot) * imag(rot); if(abs(norm - 1.0) > 0.00001) { continue; } bool work = true; for(int k = 0; k < N; k++) { complex<double> tmp = st[k] * rot + trans; bool close = false; for(int look = 0; look < N; look++) { if(abs(real(tmp) - real(en[look])) < 0.00001 && abs(imag(tmp) - imag(en[look])) < 0.00001) close = true; } if(!close) { work = false; break; } } if(work) { //cout << real(rot) << " " << imag(rot) << "\n"; //cout << real(trans) << " " << imag(trans) << "\n"; cout << "Yes" << "\n"; return 0; } } } cout << "No" << "\n"; }
#include <bits/stdc++.h> using namespace std; #define reps(i, a, n) for (int i = (a); i < (n); ++i) #define rep(i, n) reps(i, 0, n) #define deps(i, a, n) for (int i = (a); i >= (n); --i) #define dep(i, n) deps(i, n, 0) #define inf LLONG_MAX #define int long long #define mod 998244353 #define pi M_PI struct _xy{ double x; double y; }; int n; struct _xy s[105]; struct _xy t[105]; struct _xy u[105]; void func(int ang){ rep (i, n){ u[i].x = s[i].x*cos(ang*pi/180.0) - s[i].y*sin(ang*pi/180.0); u[i].y = s[i].x*sin(ang*pi/180.0) + s[i].y*cos(ang*pi/180.0); } int cnt = 0; rep (i, n){ rep (j, n){ if ((u[j].x-0.1 <= t[i].x && t[i].x <= u[j].x+0.1) && (u[j].y-0.1 <= t[i].y && t[i].y <= u[j].y+0.1) ) {u[j] = {-200, -200}; cnt++; break;} } } if (cnt == n) {cout << "Yes" << endl; exit(EXIT_SUCCESS);} } signed main(void){ cin >> n; rep (i, n) cin >> s[i].x >> s[i].y; rep (i, n) cin >> t[i].x >> t[i].y; struct _xy so = {0.0, 0.0}; struct _xy to = {0.0, 0.0}; rep (i, n){ so.x += s[i].x; so.y += s[i].y; to.x += t[i].x; to.y += t[i].y; } so.x /= n; so.y /= n; to.x /= n; to.y /= n; rep (i, n){ s[i].x -= so.x; s[i].y -= so.y; t[i].x -= to.x; t[i].y -= to.y; } rep (i, 360){ func(i); } cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rep(i, n) for(int i = 0;i < (int)n;i++) int main(){ double x, y, z;cin >> x >> y >> z; double pur = z/x; double n = pur * y; if(n == (int)n) cout << n-1 << endl; else cout << (int)n << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int x, y, z; cin >> x >> y >> z; for (int i = 0; ; ++i) if (i * x >= y * z) return cout << i - 1, 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); #define int long long #define ld long double using namespace __gnu_pbds; using namespace std; template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ; void DBG() { cerr << "]" << endl; } template<class H, class... T> void DBG(H h, T... t) { cerr << h; if (sizeof...(t)) cerr << ", "; DBG(t...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #else #define dbg(...) 0 #endif const int N = 1e2 + 5; const int mod = 1e9 + 7; const int INF = 1e18; const double eps = 1e-5; const double PI = acos(-1); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void solve(int tc){ int t, n; cin >> t >> n; cout << (n*100 +(t-1))/t + (n-1); } int32_t main(){ fast; int tc; //cin >> tc; tc = 1; for(int tt=1; tt<=tc; tt++){ solve(tt); } return 0; }
#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; #define pb push_back #define ll long long #define ld long double #define sz(x) (ll)x.size() #define vll vector<long long> #define infi INT_MAX #define infl LONG_LONG_MAX #define infd LDBL_MAX #define f(i,a,b) for(ll i=a;i<b;i++) #define fi(i,a,b) for(ll i=(b-1);i>=a;i--) #define F first #define S second #define G(a,b) get<a>(b) #define MP make_pair #define MT make_tuple #define pll pair<ll,ll> #define endl "\n" #define ALL(v) v.begin(),v.end() #define nl cout<<"\n"; #define deb cout<<"***************************************************************************************\n"; #define moshi cout<<"moshi moshi ! \n"; #define hi cout<<"hi ! \n"; #define bye cout<<"bye bye ! \n"; #define kawai cout<<"O kawai koto ! \n"; #define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define number_of_set_bits __builtin_popcountll #define eb emplace_back const ld PI=3.1415926535897932384626433; template<class T> using min_heap = priority_queue<T,vector<T>,greater<T> >; auto clk=clock(); mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 <<"\n"; //use cerr if u want to display at the bottom } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif //********************************************************** Code begins *********************************************** // trick for traversing all submasks // for (ll j=mask ;j; j=(j-1)&mask) // also include 0 manually // v.resize(unique(v.begin(), v.end()) - v.begin()); // remove duplicates ll t,n; vll v; ll calc(ll mid) { ll cnt=mid; for(auto j : v) { if(j<=mid) { ll rem=mid-j; cnt--; cnt-=(rem/(100+t)); } } return cnt; } void solve() { cin>>t>>n; f(i,1,101) { ll val=((100+t)*i)/100; v.pb(val); } ll lef=1,rig=infl,mid,res=-1; while(lef<=rig) { mid=(lef+rig)>>1; if(calc(mid)>=n) res=mid,rig=mid-1; else lef=mid+1; } cout<<res<<"\n"; } int main() { ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(8); ll test_cases=1; //cin>>test_cases; f(i,1,test_cases+1) { solve(); //clr(); } #ifdef rd cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl; #endif return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #define rep(i,N) for(int i=0;i<(int)N;i++) #define Swap(a,b) (a+=b,b=a-b,a-=b) static inline long Max(long a,long b){return a>b?a:b;} static inline void PUT(char c) { static char buf[1<<15],*ptr=buf; if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c; } static inline int IN(void) { int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();} while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x; } static inline void OUT(long a) { if(a<0)PUT('-'),a=-a; int d[40],i=0;do{d[i++]=a%10;}while(a/=10); while(i--){PUT(d[i]+48);}PUT('\n'); } static inline void DescRadix32(int *a,const int sz,const int minus) { int x,shift=0,elem[0400],temp[sz]; while(shift<040) { int bucket[0400]={0}; rep(i,sz){x=(a[i]>>shift)&0377;bucket[x]++;temp[i]=a[i];} elem[0]=0;rep(i,0377)elem[i+1]=elem[i]+bucket[i]; rep(i,sz){x=(temp[i]>>shift)&0377;a[elem[x]]=temp[i];elem[x]++;} shift+=010; } rep(i,minus>>1)Swap(a[sz-minus+i],a[sz-1-i]); rep(i,(sz-minus)>>1)Swap(a[i],a[sz-minus-1-i]); } int main(void) { int N=IN(),A[N],B=0,m1=0,m2=0,p1[N/2],p2[N/2];long score=0; rep(i,N){A[i]=IN(),score+=A[i];} rep(i,N){B=IN();if(i&1){p1[i/2]=B-A[i];if(B-A[i]<0)m1++;}else{p2[i/2]=B-A[i];if(B-A[i]<0)m2++;}} DescRadix32(p1,N/2,m1);DescRadix32(p2,N/2,m2); rep(i,N>>1){if(p1[i]+p2[i]>0)score+=p1[i]+p2[i];else break;} return OUT(score),0; }
#include <bits/stdc++.h> #define debug(x) cout << ##x << ":\t" << (x) << endl; using namespace std; #define ll long long const int N = 2e6 + 10; const int INF = 0x3f3f3f3f; const ll inf = 0x3f3f3f3f3f3f3f3f; const ll mod = 998244353; int n; ll a[N], b[N]; ll ans; priority_queue<ll>q[2]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++)scanf("%lld", &a[i]), ans += a[i]; for (int i = 1; i <= n; i++)scanf("%lld", &b[i]); for (int i = 1; i <= n; i++) { q[i & 1].push(b[i] - a[i]); } while (1) { if (!q[0].empty() && q[0].top() + q[1].top() > 0) { ans += q[0].top() + q[1].top(); q[0].pop(); q[1].pop(); } else break; } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int a, b; cin >> a >> b; int res = 1; for (int i = 1; i <= b; ++i) { if (b / i - (a - 1) / i >= 2) res = i; } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++)//iをaからn #define per(i,n,a) for (int i=a-1;i>=n;i--)//iをnからa #define db(x) cout << #x << " = " << x << endl//デバッグ用 #define db2(x, y) cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; //デバッグ用 #define all(x) (x).begin(), (x).end() #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 998244353 //10^9+7:合同式の法 #define pb push_back #define F first #define S second typedef long long ll; //sort(all(a)); ソート //sort(all(a),greater<int>()); 逆順ソート //a.erase(unique(all(a)), a.end()); ソート後に重複を削除 //cout<< std::fixed << std::setprecision(15)<<0.000001<<endl; 小数点以下を表示させる //vector<vector<int>> data(3, vector<int>(4)); int型の2次元配列(3×4要素の)の宣言 int main() { int a,b; cin>>a>>b; int ans=b-a; per(i,0,ans+1){ //cout<<i<<" "<<a/i<<endl; //cout<<(((a+i-1)/i)+1)*i<<" "<<b<<endl; if((((a+i-1)/i)+1)*i<=b){ cout<<i<<endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using pii = pair<ll, ll>; const ll INF = 4e18; const ll mod = 1e9 + 7; #define all(a) (a).begin(), (a).end() // Print for debug template<class T> ostream& operator<<(ostream &out, vector<T> const &v) { out << "["; for(int i = 0; i < v.size(); i++) {if (i) out << ", "; out << v[i];} return out << "]"; } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> const &p) { return out << "(" << p.first << ", " << p.second << ")"; } ll nxt() { ll x; cin >> x; return x; } ll mmod(ll x, ll y) { if (x >= 0) return x % y; else return (y - (-x) % y) % y; } ll powmod(ll a, ll n) { ll res = 1; a %= mod; assert(n >= 0); for(; n ; n>>=1) { if (n & 1) res = res * a % mod; a = a * a % mod; } return res; } void solve() { int n, T; cin >> n >> T; vector<ll> a(n); generate(all(a), nxt); vector<ll> A; vector<ll> B; for (int i = 0; i < (1 << (n / 2)); ++i) { ll cursum = 0; for (int k = 0; k < n / 2; ++k) { if (i & (1 << k)) cursum += a[k]; } A.push_back(cursum); } for (int i = 0; i < (1 << (n - n / 2)); ++i) { ll cursum = 0; for (int k = 0; k < n - n / 2; ++k) { if (i & (1 << k)) cursum += a[n / 2 + k]; } B.push_back(cursum); } sort(all(A)); vector<ll> C; auto search = [&](ll x) -> ll { int l = -1; int r = A.size(); while (r - l > 1) { int m = (l + r) / 2; if (x < A[m]) r = m; else l = m; } return l; }; for (auto i : B) { int ind = search(T - i); if (ind != -1) C.push_back(i + A[ind]); } sort(all(C)); cout << C.back() << endl; } // #define LOCAL //#define TESTS int 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 #ifdef TESTS int t; cin >> t; while (t--) #endif solve(); return 0; } // Seeing WA2 fills you with DETERMINATION
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 62; int mod = 1000000007; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); ll N, T; cin >> N >> T; vector<ll> num(N); for (ll i = 0; i < N; i++) { cin >> num.at(i); } // グループ1をビット全探索で全列挙 (前半の半分) vector<ll> A; for (ll bit = 0; bit < (1 << (N+1) / 2); bit++) { ll sum = 0; for (ll i = 0; i < ((N+1) / 2); i++) { ll mask = 1 << i; if (bit & mask) { sum += num[i]; } } A.push_back(sum); } // グループ2をビット全探索で全列挙 (後半の半分) vector<ll> B; for (ll bit = 0; bit < (1 << N / 2); bit++) { ll sum = 0; for (ll i = 0; i < (N / 2); i++) { ll mask = 1 << i; if (bit & mask) { sum += num[(N+1) / 2 + i]; } } B.push_back(sum); } sort(B.begin(), B.end()); //rep(i, A.size()) cout << A[i] << " "; cout << endl; //rep(i, B.size()) cout << B[i] << " "; cout << endl; ll ans = 0; for(auto item: A){ ll key = T-item; if(key < 0) continue; int ok = -1, ng = B.size(); int m = (ok+ng)/2; while(abs(ok-ng) > 1){ if(B[m] <= key) ok = m; else ng = m; m = (ok+ng)/2; } if(ok != -1) ans = max(ans, item + B[ok]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++) #define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--) #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define eb emplace_back using ll = long long; using vll = vector<ll>; using vi = vector<int>; using vvi = vector<vector<int>>; using P = pair<int, int>; using LD = long double; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; } void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; } void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; } void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; } ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); } ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); } void Main(){ int n; cin >> n; LD ans = 0; rep3(i, 1, n){ ans += (LD)1/i; } ans *= n; cout << ans - 1 << endl; return; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[311111]; ll sum[311111]; int main(){ int n; while(scanf("%d",&n)!=EOF){ double ans = 0; for(int i = 1 ; i <= n ; i++){ ans += (1.0/i)*n; } printf("%.6f\n",ans-1); } return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <ios> #include <iostream> #include <limits> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> #include <utility> using namespace std; using i64 = int64_t; using u64 = uint64_t; using i32 = int32_t; using pi64 = pair<i64, i64>; constexpr i64 MAXN = 3*100*1000LL+5LL; constexpr i64 MOD = 1000000007LL; constexpr i64 INF64 = MOD * MOD; #define vec vector #define let const #define DRi64(x) i64 x; cin >> x; #define DRS(x) string x; cin >> x; #define DRVi64(v, n) vec<i64> v(n); { for (i64 i = 0; i < n; ++i) { cin >> v[i]; }} #ifdef DEBUG #define P(x) cerr << x << "\n" #else #define P(x) #endif i64 find_(const i64 a, vec<i64>& parents) { if (parents[a] != a) { parents[a] = find_(parents[a], parents); } return parents[a]; } void union_(const i64 a, const i64 b, vec<i64>& parents, vec<i64>& sizes, vec<map<i64, i64>>& class_hists) { let i64 r_a = find_(a, parents); let i64 r_b = find_(b, parents); if (r_a == r_b) { return; } if (sizes[r_a] < sizes[r_b]) { union_(r_b, r_a, parents, sizes, class_hists); return; } sizes[r_a] += sizes[r_b]; parents[r_b] = r_a; // merge map of smaller vertex into that of larger vertex for (let auto& [cls, cnt] : class_hists[r_b]) { class_hists[r_a][cls] += cnt; } } int main() { ios_base::sync_with_stdio(false); DRi64(N); DRi64(Q); DRVi64(C, N); // Ci \in [1, N] vec<i64> class_hist(N + 1, 0); for (let i64 c : C) { ++class_hist[c]; } vec<i64> parents(N + 1); iota(parents.begin(), parents.end(), 0LL); vec<i64> sizes(N + 1, 1); vec<map<i64, i64>> class_hists(N + 1); for (i64 i = 1; i <= N; ++i) { class_hists[i][C[i - 1]] = 1; } for (; Q-->0;) { DRi64(typ); if (typ == 1) { DRi64(A); DRi64(B); union_(A, B, parents, sizes, class_hists); } else if (typ == 2) { DRi64(X); DRi64(Y); if (let i64 r_X = find_(X, parents); class_hists[r_X].count(Y)) { cout << class_hists[r_X][Y] << "\n"; } else { cout << "0\n"; } } else { abort(); } } return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long int ll; constexpr ll mod=1e9+7; vector<int> st[200010]; vector<int> que[200010]; struct UnionFind{ vector<int> par,num; UnionFind(int n):par(n),num(n,1){ iota(par.begin(),par.end(),0); //include<numeric> } int find(int v){ return (par[v]==v)?v:(par[v]=find(par[v])); } void unite(int u,int v){ u=find(u),v=find(v); if(u==v)return; if(num[u]<num[v])swap(u,v); if((int)que[u].size()+(int)que[v].size()<500){ que[u].push_back(v); for(auto p:que[v]){ que[u].push_back(p); } } else{ vector<int> V=st[u]; for(auto p:que[u]){ for(int pp:st[p]){ V.push_back(pp); } } que[v].push_back(v); for(auto p:que[v]){ for(int pp:st[p]){ V.push_back(pp); } } que[v].clear(); que[u].clear(); sort(V.begin(), V.end()); st[u]=V; } num[u]+=num[v]; par[v]=u; } bool same(int u,int v){ return find(u) == find(v); } int size(int v){ return num[find(v)]; } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,q; cin >> n >> q; vector<int> c(n); vector<int> cnt(n+1,0); vector<int> sum(n+1,0); for(int i=0;i<n;i++){ cin >> c[i]; cnt[c[i]]++; } sum=cnt; for(int i=0;i<n;i++){ sum[i+1]+=sum[i]; } vector<int> rem=cnt; for(int i=0;i<n;i++){ int sm=sum[c[i]-1]; rem[c[i]]--; c[i]=sm+rem[c[i]]; } UnionFind uf(n); for(int i=0;i<n;i++){ st[i].push_back(c[i]); } auto cal=[&](int x,vector<int> &v)->int{ return v.end()-lower_bound(v.begin(), v.end(),x); }; while(q--){ int t; cin >> t; if(t==1){ int a,b; cin >> a >> b; a--; b--; uf.unite(a,b); } else{ int a,b; cin >> a >> b; a--; a=uf.find(a); int res=0; res+=cal(sum[b-1],st[a])-cal(sum[b],st[a]); for(auto p:que[a]){ res+=cal(sum[b-1],st[p])-cal(sum[b],st[p]); } printf("%d\n",res); } } }
/*input 10 165 82 94 21 65 28 22 61 80 81 79 93 35 59 85 96 1 78 72 43 5 12 15 97 49 69 53 18 73 6 58 60 14 23 19 44 99 64 17 29 67 24 39 56 92 88 7 48 75 36 91 74 16 26 10 40 63 45 76 86 3 9 66 42 84 38 51 25 2 33 41 87 54 57 62 47 31 68 11 83 8 46 27 55 70 52 98 20 77 89 34 32 71 30 50 90 4 37 95 13 100 */ #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 int long long #define double long double #define f first #define s second #define mp make_pair #define pb push_back #define RE(i,n) for (int i = 1; i <= n; i++) #define REP(i,n) for (int i = 0; i < (int)n; i++) #define all(v) v.begin(),v.end() #define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int INF = 1e18+1; const int MOD = 998244353; const double PI = 3.14159265358979323846264338; int raise(int a,int n,int m = MOD){ if(n == 0)return 1; if(n == 1)return a; int x = 1; x *= raise(a,n/2,m); x %= m; x *= x; x %= m; if(n%2)x*= a; x %= m; return x; } const int N = 51; vector<int> adj[N]; int a[N][N]; bool vis[N]; int sz = 0; int n,k; int ans = 1; void dfs(int u){ vis[u] = 1; sz++; ans *= sz; ans %= MOD; for(int v:adj[u]){ if(vis[v])continue; dfs(v); } } void solve(){ cin >> n >> k; RE(i,n){ RE(j,n){ cin >> a[i][j]; } } RE(x,n){ RE(y,n){ // columns if(x == y)continue; bool ok = 1; RE(i,n){ ok &= (a[i][x]+a[i][y] <= k); } if(ok)adj[x].pb(y); } } RE(i,n){ if(vis[i])continue; sz = 0; dfs(i); } // rows RE(i,n){ vis[i] = 0; adj[i].clear(); } RE(x,n){ RE(y,n){ // rows if(x == y)continue; bool ok = 1; RE(i,n){ ok &= (a[x][i]+a[y][i] <= k); } if(ok)adj[x].pb(y); } } RE(i,n){ if(vis[i])continue; sz = 0; dfs(i); } cout << ans; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(".in","r",stdin);freopen(".out","w",stdout); int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define t3 1000+10 #define t4 10000+10 #define t5 100000+10 #define t6 1000000+10 #define MOD 1000000007 #define rep(i,a,n) for (ll i=a;i<n;i++) #define per(i,a,n) for (ll i=n-1;i>=a;i--) #define pb push_back const double pi = acos(-1.0); namespace X{ ll r(){ll r;scanf("%lld",&r);return r;} // read void add(ll &v0,ll &v1){(v0+=v1%MOD)%=MOD;} // add with MOD ll mpow(ll v,ll mi){ll r = 1;while(mi){if(mi%2)(r*=v)%=MOD;(v*=v)%=MOD;mi>>=1;};return r;} // quick power with MOD ll mod(ll v){return (v%MOD+MOD)%MOD;} // output ll gcd(ll a,ll b){return b == 0?a:gcd(b,a%b);} }; char s[5010]; pair<int,int>cnt[5010]; int main(){ int n; scanf("%d",&n); scanf("%s",s); cnt[0] ={0,0}; rep(i,0,n){ cnt[i+1] = cnt[i]; if(s[i] == 'A'){ cnt[i+1].first ++; }else if(s[i] == 'T'){ cnt[i+1].first --; }else if(s[i] == 'C'){ cnt[i+1].second ++; }else if(s[i] == 'G'){ cnt[i+1].second --; } } int ans = 0; rep(i,0,n){ rep(j,i+1,n+1){ if(cnt[j].first == cnt[i].first && cnt[j].second == cnt[i].second){ ans++; } } } cout<<ans<<endl; return 0; } /* * * hash prime 61,83,113,151,211,281,379,509683,911 1217,1627,2179,2909,3881,6907,9209 12281,16381,21841,29123,38833,51787,69061,92083 122777,163729,218357,291143,388211,517619,690163,999983 1226959,1635947,2181271,2908361,3877817,5170427,6893911,9191891 12255871,16341163,21788233,29050993,38734667,51646229,68861641,91815541 122420729,163227661,217636919,290182597,386910137,515880193,687840301,917120411 1000000007,1000000009 * */
#include <bits/stdc++.h> using namespace std; using ll = long long; // -------------------------------------------------------- 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 FOR(i,l,r) for (int i = (l); i < (r); ++i) #define RFOR(i,l,r) for (int i = (r)-1; (l) <= i; --i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define SUM(c) accumulate(ALL(c), 0) #define SUMLL(c) accumulate(ALL(c), 0LL) #define SZ(c) ((int)(c).size()) #define CIN(c) cin >> (c) #define COUT(c) cout << (c) << '\n' #define debug(x) cerr << #x << " = " << (x) << '\n'; using P = pair<int,int>; using VP = vector<P>; using VVP = vector<VP>; using VS = vector<string>; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<ll>; using VVLL = vector<VLL>; using VB = vector<bool>; using VVB = vector<VB>; using VD = vector<double>; using VVD = vector<VD>; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const ll MOD = 1000000007; // static const ll MOD = 998244353; static const int INF = (1 << 30) - 1; // 1073741824 - 1 // static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1 // -------------------------------------------------------- // #include <atcoder/all> // using namespace atcoder; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); int N, M; cin >> N >> M; VVP G(N+1); REP(_,M) { int u, v, c; cin >> u >> v >> c; G[u].push_back(P(v, c)); G[v].push_back(P(u, c)); } const int root = 1; VVP H(N+1); VB used(N+1, false); used[root] = true; queue<int> q; q.push(root); while (!q.empty()) { int u = q.front(); q.pop(); for (auto p : G[u]) { int v = p.first; int c = p.second; if (used[v]) continue; used[v] = true; q.push(v); H[u].push_back(P(v, c)); } } VI ans(N+1); ans[root] = 1; q.push(root); while (!q.empty()) { int u = q.front(); q.pop(); for (auto p : H[u]) { int v = p.first; int c = p.second; if (ans[u] == c) { ans[v] = (c == 1) ? N : 1; } else { ans[v] = c; } q.push(v); } } FOR(i,1,N+1) { COUT(ans[i]); } return 0; }
#include<bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define DEBUG //#define STRESS #endif // ONLINE_JUDGE template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; for (int i = 0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << "]"; return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "["; os << p.first; os << ", "; os << p.second; os << "]"; return os; } vector<long long> dp; struct constraint { //int xElements; // under the first x Elements int atMostZ; // atMostZ can be int NumY; // Less or equal to num }; bool checkConstraints(vector<vector<constraint>>& cons, int state) { int cntAll = __builtin_popcount(state); for(constraint& con : cons[cntAll]) { int cntSmallerEqualY = __builtin_popcount(state & ((1 << con.NumY) - 1)); if(cntSmallerEqualY > con.atMostZ) { return false; } } return true; } int n = 0, m = 0; void doDp(vector<vector<constraint>>& cons, int num) { long long sum = 0; if(checkConstraints(cons, num)) { //cout << "PASS "; for(int i = 0; i < n; i++) { if(num & 1 << i) { sum += dp[num ^ (1 << i)]; } } } //cout << std::bitset<18>(num) << " :" << sum << "\n"; dp[num] = sum; } int solve() { cin >> n >> m; vector<vector<constraint>> cons(n + 1, vector<constraint>(0)); for(int i = 0; i < m; ++i) { constraint con; int xelement; cin >> xelement >> con.NumY >> con.atMostZ; cons[xelement].push_back(con); } dp.resize(1<<n); dp[0] = 1; for(int i = 1; i < 1 << n; i++) { doDp(cons, i); } vector<bool> ans(n, true); cout << dp[(1 << n) - 1] << "\n"; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); //freopen("a.out","w",stdout); // console output is better (in most cases) #else // add i/o method of specific testing system #endif { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() using ll = long long; const ll mod = 1000000007; const double pi = 3.14159265358979; const ll INF = 1e18; vector<ll> divisor(ll n) { vector<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); return ret; } int main() { ll n; cin >> n; vector<ll> div = divisor(2 * n); ll ans = 0; for (ll m : div) { if ((2 * n / m - m + 1) % 2 == 0) ans++; } if (n == 1) cout << 2 << endl; else cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define deb(x)cout << #x << " = " << x << " , " << '\n'; #define mod 100000000 int main(){ ll s, ans = 0; cin >> s; s *= 2; for(ll i=1; i*i<=s; i++){ if(s%i==0){ ll tmp = (s/i) - (i-1); if(tmp%2==0)ans++; if(i!=(s/i)){ ll tmp = (s/(s/i)) - (s/i) - 1; if(tmp%2==0)ans++; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x, y; scanf("%d%d", &x, &y); if (x > y) swap(x, y); printf(x + 3 > y ? "Yes" : "No"); return 0; }
#include <iostream> #include <algorithm> #include <climits> #include <vector> #include <unordered_set> #include <queue> #include <bits/stdc++.h> #include <unordered_map> #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0) using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,a,n) for(int i=n;i>=a;i--) #define pb push_back #define mp make_pair #define FI first #define SE second #define mod 998244353 #define inf 0x3f3f3f3f typedef pair<int, int> pii; typedef vector<int> vi; typedef double db; using namespace std; void solve() { ll x, y; cin >> x>>y; string ans; ans = "No"; if (x - y <= 2 && x - y >= -2) ans = "Yes"; cout << ans << endl; } int main(void) { //int t; cin >> t; for (int i = 0; i < t; i++) {solve();} solve(); return 0; }
/* Author: Tarun Kumar E-mail: [email protected] " A code is like love, it has created with clear intentions at the beginning, but it can get complicated:) " */#include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ll long long int #define ld long double #define vll vector<ll> #define vvll vector<vector<ll>> #define vpll vector<pair<ll,ll>> #define prqueue priority_queue<ll,vector<ll>,greater<ll>>// top=small #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define ff first #define ss second #define all(v) v.begin(), v.end() #define f0(i,n) for(i=0;i<n;i++) #define f1(i,n) for(i=1;i<=n;i++) #define fab(i,a,b) for(i=a;i<=b;i++) #define f0r(i,n) for(i=n-1;i>=0;i--) #define f1r(i,n) for(i=n;i>=1;i--) #define fabr(i,a,b) for(i=b;i>=a;i--) #define memo(a,b) memset(a,b,sizeof(a)) #define display(x) {for(auto dsp:x)cout<<dsp<<" ";cout<<"\n";} #define hi cout<<"hi\n" #define ln "\n" using namespace std; const ld pi=acos(-1);const ll mod=1e9+7; int main() {FAST;ll t,n,i,j,k,len,x,y,z,c,f,flag,p,q,mx,mn,l,r,sum,ans,tmp,it,pos,avg,m,cnt; string s;char ch;vll v;vpll vec;unordered_map<ll,ll> mappu;pair<ll,ll> pr; t=1; // cin>>t;while(t--) { f=0;sum=0;flag=0;ans=0;cnt=0;v.clear();mappu.clear();vec.clear(); cin>>n; ll a[n],b[n]; f0(i,n) { cin>>a[i]>>b[i]; } f0(i,n) { fab(j,i+1,n-1) { x=a[j]-a[i]; y=b[j]-b[i]; ld pp=(ld)y/(ld)x; if(pp<=1&&pp>=-1) ans++; } } cout<<ans<<ln; } return 0; }
#include <iostream> #include <algorithm> using namespace std; inline bool cmp(pair<int, int> x, pair<int, int> y) { int up = x.second - y.second; int dw = x.first - y.first; if(dw < 0) dw = -dw, up = -up; return up <= dw && up >= -dw; } const int maxn = 1111; pair<int, int> arr[maxn]; int main() { int n; cin >> n; for(int i = 0; i < n; ++i) cin >> arr[i].first >> arr[i].second; int ans = 0; for(int i = 0; i < n; ++i) { for(int j = i + 1; j < n; ++j) { if(cmp(arr[i], arr[j])) ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define fz(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) #define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++) #define all(a) a.begin(),a.end() #define fill0(a) memset(a,0,sizeof(a)) #define fill1(a) memset(a,-1,sizeof(a)) #define fillbig(a) memset(a,0x3f,sizeof(a)) #define y1 y10101010101 #define y0 y01010101010 typedef pair<int,int> pii; typedef long long ll; const ll MOD=998244353; int n,k,a[55][55]; ll f[55]; vector<int> gc[55],gr[55]; bool vc[55],vr[55]; int ac=0,ar=0; inline void dc(int x){ if(vc[x]) return; ac++;vc[x]=1; foreach(it,gc[x]) dc(*it); } inline void dr(int x){ if(vr[x]) return; ar++;vr[x]=1; foreach(it,gr[x]) dr(*it); } 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]); f[0]=1;for(int i=1;i<=n;i++) f[i]=f[i-1]*i%MOD; for(int x=1;x<=n;x++) for(int y=1;y<x;y++){ bool flag=1; for(int i=1;i<=n;i++) flag&=(a[i][x]+a[i][y]<=k); if(flag) gc[x].pb(y),gc[y].pb(x); flag=1; for(int i=1;i<=n;i++) flag&=(a[x][i]+a[y][i]<=k); if(flag) gr[x].pb(y),gr[y].pb(x); } ll ans=1; for(int i=1;i<=n;i++) if(!vc[i]) ac=0,dc(i),ans=ans*f[ac]%MOD; for(int i=1;i<=n;i++) if(!vr[i]) ar=0,dr(i),ans=ans*f[ar]%MOD; printf("%lld\n",ans); return 0; }
#include<bits/stdc++.h> #define int long long #define ri register signed #define rd(x) x=read() using namespace std; const int N=1e7+5; const int M=1e6+5; const int maxn=5e6+5; const int mod=998244353; const int inf=0x3f3f3f3f3f3f3f3f; inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;} inline int ksm(int x,int y=mod-2,int z=mod){int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;} int inv[N],fac[N],ifc[N]; void Init(int n) { inv[1]=1;for (ri i=2;i<=n;i++)inv[i]=inv[mod%i]*(mod-mod/i)%mod; fac[0]=1;for (ri i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod; ifc[0]=1;for (ri i=1;i<=n;i++) ifc[i]=ifc[i-1]*inv[i]%mod; } int C(int n,int m){if (m>n || m<0) return 0;return fac[n]*ifc[m]%mod*ifc[n-m]%mod;} int n,m,ans; int x[N],y[N],z[N]; int get(int S,int k) { int ret=0; for (int i=0;i<k;i++) ret+=(S>>i)&1; return ret; } int mp[N]; int dfs(int S) { if (S==(1<<n)-1) return 1; if (mp[S]!=-1) return mp[S]; int ret=0; int num=get(S,n); for (int i=0;i<n;i++) if ((S>>i)&1^1) { int T=S^(1<<i); bool flag=1; for (int j=1;j<=m;j++) if (y[j]==num+1) if (get(T,x[j])>z[j]) flag=0; if (flag) ret+=dfs(T); } return mp[S]=ret; } signed main() { rd(n);rd(m); for (int i=1;i<=m;i++) rd(x[i]),rd(y[i]),rd(z[i]); memset(mp,-1,sizeof(mp)); cout<<dfs(0)<<endl; }
// // Created by yamunaku on 2021/04/17. // #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD1 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<<endl #define CNO cout<<"No"<<endl #define CFS cin.tie(0);ios::sync_with_stdio(false) #define CST(x) cout<<fixed<<setprecision(x) using ll = long long; using ld = long double; using vi = vector<int>; using mti = vector<vector<int>>; using vl = vector<ll>; using mtl = vector<vector<ll>>; using pi = pair<int, int>; using pl = pair<ll, ll>; template<typename T> using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>; int main() { //CFS; int a, b; cin >> a >> b; int ans = 1; repl(i, 1, b + 1) { if (b / i - (a - 1) / i >= 2) ans = i; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define pb emplace_back #define pf first #define ps second #define all(x) x.begin(),x.end() #define allr(x) x.rbegin(),x.rend() #define Endl '\n' #define codejam cout<<"Case #"<<t<<": "; #define pl pair<ll,ll> #define mod 1000000007 #define INF 1000000001 #define forn(i,n) for(ll i=0;i<n;i++) #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); using namespace std; typedef long long int ll; ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } //////////////////////////////////// JAI SHREE RAM ///////////////////////////////////// const ll N=1e5; void JAISHREERAM() { ll n,i,j,ans=0; ll a,b; cin>>a>>b; ans=1; vector<int> f(200001,0); for(i=a;i<=b;i++) { for(j=1;j*j<=i;j++) { if(i%j==0) { f[i/j]++; if(f[i/j]>1) ans=max(ans,i/j); if(i/j!=j) { int x=i/j; f[i/x]++; if(f[i/x]>1) ans=max(ans,i/x); } } } } cout<<ans; } int main() { fast; int T=1; // cin>>T; for(int t=1;t<=T;t++) { // codejam JAISHREERAM(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long const ll N = 17; ll n; ll a[1 << N]; int main() { scanf("%lld", &n); vector<pair<ll, ll>>v; for (ll i = 1 ; i <= (1 << n) ; i++) { scanf("%lld", &a[i]); v.push_back({a[i], i}); } while (v.size() > 2) { vector<pair<ll, ll>>test; for (ll i = 0; i + 1 < v.size() ; i += 2) { if (v[i].first > v[i + 1].first) test.push_back(v[i]); else test.push_back(v[i + 1]); } v = test; } if (v[0].first > v[1].first) printf("%lld\n", v[1].second); else printf("%lld\n", v[0].second); } //Author: Mehedi.cm //Created: 11 January 2021 (Monday) at 12:12 AM
#include<bits/stdc++.h> #define w(x) int x; cin>>x; for(int tc=1;tc<=x;tc++) #define trace(x) cerr<<#x<<": "<<x<<" "<<endl; #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long #define pb push_back #define pqg priority_queue<int> #define pqs priority_queue<int,vector<int>,greater<int>> #define mpii map<int,int> #define endl "\n" #define YES cout<<"YES\n"; #define Yes cout<<"Yes\n"; #define NO cout<<"NO\n"; #define No cout<<"No\n"; #define mod 1000000007 #define inp(x) cin>>x; using namespace std; void solve(int tc) { int a, b; cin >> a >> b; int c = b; a += b; b = c; if (a >= 15 and b >= 8) { cout << 1 << endl; } else if (a >= 10 and b >= 3) { cout << 2 << endl; } else if (a >= 3) { cout << 3 << endl; } else cout << 4 << endl; } int32_t main() { FIO #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif // w(x){ solve(1); // } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; //typedef long long ll; const int M_ans = 0x3f3f3f3f; const int M = 2e5 + 10; int h[M], w[M], qsm[M], hsm[M]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 1; i <= m; i++) cin >> w[i]; sort(h + 1, h + 1 + n); sort(w + 1, w + 1 + m); for (int i = 2; i <= n; i += 2) qsm[i] = qsm[i - 2] + h[i] - h[i - 1]; for (int i = n - 1; i >= 1; i -= 2) hsm[i] = hsm[i + 2] + h[i + 1] - h[i]; int ans = M_ans; for (int i = 1; i <= m; i++) { int p = lower_bound(h + 1, h + 1 + n, w[i]) - h; if (p % 2 == 0) p--; ans = min(ans, qsm[p - 1] + hsm[p + 1] + abs(w[i] - h[p])); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #include<algorithm> #include<cmath> #include<climits> using namespace std; typedef long long int lli; typedef vector<int> vi; typedef vector<long long int> vlli; typedef pair<int,int> pii; typedef pair<long long int,long long int> plli; typedef vector< vi > vvi ; typedef vector< vlli > vvlli ; #define fi(i,a,b) for(int i=a;i<=b;i++) #define flli(i,a,b) for(long long int i=a;i<=b;i++) #define bi(i,a,b) for(int i=a;i>=b;i--) #define blli(i,a,b) for(long long int i=a;i>=b;i--) #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define all(x) x.begin(),x.end() #define sz(x) x.size() #define pi 2*acos(0.0) #define pb push_back #define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++) #define present(v,num) (v.find(num)!=v.end()) #define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end()) #define pq priority_queue #define mp make_pair const int inf=INT_MAX; const lli INF =LLONG_MAX; const lli mod = 1e9+7; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast; lli n,m;cin>>n>>m; vlli h(n); vlli w(m); flli(i,0,n-1)cin>>h[i]; flli(i,0,m-1)cin>>w[i]; sort(all(h)); sort(all(w)); vlli forans(n); vlli bacans(n); lli ind=1; lli last=0; while(ind<n) { last+=h[ind]-h[ind-1]; forans[ind]=last; ind+=2; } ind=n-2; last=0; while(ind>=0) { last+=h[ind+1]-h[ind]; bacans[ind]=last; ind-=2; } lli ans=INF; ind=-1; flli(i,0,m-1) { if(w[i]<=h[0])ind=i; } if(ind!=-1) { lli nexti=0; if(1<=n-2)nexti=bacans[1]; ans=min(ans,-w[ind]+h[0]+nexti); } flli(i,0,n-1) { //will put after h[i]; if(h[i]>w[m-1])continue; auto var=lower_bound(all(w),h[i]); if(i%2==0) { lli nexti=0; lli prev=0; if(i+1<n)nexti=bacans[i+1]; if(i-1>=0)prev=forans[i-1]; ans=min(ans,*var-h[i]+prev+nexti); } else { lli nexti=0; lli prev=0; prev=forans[i]; if(i+2<n)nexti=bacans[i+2]; ans=min(ans,abs(*var-h[i+1])+prev+nexti); } } cout<<ans; cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
/** * @created : April 11, 2021 * @handle : 🇨🇴 @SorKierkegaard */ #include <bits/stdc++.h> using namespace std; #define endl '\n' #define finally(value) {cout << (value) << '\n'; return 0;} #define forn(i, b) for(int i = 0; i < (int) b; ++i) #define rep(i, a, b) for(int i = int(a); i < (int) b; ++i) #define trav(x, a) for(auto &x: a) #define sz(v) ((int) v.size()) using int64 = int64_t; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); const string no = "UNSOLVABLE"; string s1, s2, s3; cin >> s1 >> s2 >> s3; int sz1 = (int) s1.size(); int sz2 = (int) s2.size(); int sz3 = (int) s3.size(); set<char> counter; forn(i, sz1) counter.insert(s1[i]); forn(i, sz2) counter.insert(s2[i]); forn(i, sz3) counter.insert(s3[i]); if(sz(counter) > 10) finally(no) int count = (int) counter.size(); map<char, int> letters; trav(a, counter) letters[a] = 0; vector<int> perm(10, 0); iota(perm.begin(), perm.end(), 0); int64 n1, n2, n3; function<int64(string&)> to_number = [&](string &s) -> int64 { int64 answer = 0LL; forn(i, sz(s)) { answer *= 10LL; answer += int64(letters[s[i]]); } return answer; }; do { int idx = 0; trav(ch, letters) { ch.second = perm[idx]; idx++; } if(letters[s1[0]] == 0 || letters[s2[0]] == 0 || letters[s3[0]] == 0) { continue; } n1 = to_number(s1); n2 = to_number(s2); n3 = to_number(s3); if(n1+n2==n3) { cout << n1 << endl; cout << n2 << endl; cout << n3 << endl; return 0; } } while(next_permutation(perm.begin(), perm.end())); cout << no << endl; cout.flush(); return 0; } // THINGS TO KEEP IN MIND // * int overflow, time and memory limits // * Special case (n = 1?) // * Do something instead of nothing and stay organized // * Don't get stuck in one approach // TIME AND MEMORY LIMITS // * 1 second is approximately 10^8 operations (c++) // * 10^6 Elements of 32 Bit (4 bytes) is equal to 4 MB // * 62x10^6 Elements of 32 Bit (4 bytes) is equal to 250 MB // * 10^6 Elements of 64 Bits (8 bytes) is equal to 8 MB // * 31x10^6 Elements of 64 Bit (8 bytes) is equal to 250 MB
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define fi first #define se second #define mp make_pair #define mt make_tuple #define all(A) A.begin(), A.end() #define sz(A) (int)A.size() typedef vector <int> vi; typedef vector <ll> vl; typedef pair <int, int> pii; typedef tuple <int, int, int> tii; const ll mod = 1e9 + 7; //const ll mod = 998244353; const int inf = 1e9; //const ll inf = 1e18; const int N = 2e5; ll a[10]; vector <ll> v; string s[3]; bool ok = false; ll val[26]; void heapPermutation(ll a[], ll size, ll n) { if (ok) return; if (size == 1) { //printArr(a, n); ll u[3] = {0}; for (ll i = 0; i < sz(v); i++) { val[v[i]] = a[i]; } bool np = true; for (ll i = 0; i < 3; i++) { ll x = 1; for (ll j = s[i].length() - 1; j >= 0; j--) { if (j == 0 && val[s[i][j] - 'a'] == 0) np = false; u[i] += (val[s[i][j] - 'a']) * x; x *= 10; } } if (np && u[0] + u[1] == u[2] && u[0] > 0 && u[1] > 0) { cout << u[0] << '\n'; cout << u[1] << '\n'; cout << u[2] << '\n'; ok = true; } return; } for (ll i = 0; i < size; i++) { heapPermutation(a, size - 1, n); if (size % 2 == 1) swap(a[0], a[size - 1]); else swap(a[i], a[size - 1]); } } void solve () { for (ll i = 0; i < 10; i++) { a[i] = i; } for (ll i = 0; i < 3; i++) { cin >> s[i]; } set <ll> st; for (ll i = 0; i < 3; i++) { for (ll j = 0; j < s[i].length(); j++) { st.insert(s[i][j] - 'a'); } } if (sz(st) > 10) { cout << "UNSOLVABLE\n"; return; } for (ll i : st) { v.pb(i); } heapPermutation(a, 10, 10); if (!ok) { cout << "UNSOLVABLE\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int t = 1; while (t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; const int INF = 1<<30; const long long LINF = 1LL<<60; const long long MOD = (long long)1e9 + 7; int main(){ int n, ans = INF; cin >> n; vector<int> a(n), b(n); for(int i = 0; i < n; i++){ cin >> a[i] >> b[i]; } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(i != j){ ans = min(ans, max(a[i], b[j])); }else{ ans = min(ans, a[i] + b[j]); } } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define lli long long int #define sz 10000010 #define pb push_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define in(x) scanf("%lld",&x) #define ins(x) scanf("%s",x) #define out(x) printf("%lld",x) #define outs(x) printf("%s",x) #define loop(x) for(lli i=0;i<x;i++) #define loop2(x) for(lli j=0;j<x;j++) #define eps 0.000000001 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define C 0.57721566490153286060651209s using namespace std; void solve() { lli n; cin >> n; vector <pair<lli,lli>> a(n),b(n); loop(n) { lli x,y; cin >> x >> y; a[i] = {x,i+1}; b[i] = {y,i+1}; } sort(a.begin(),a.end()); sort(b.begin(),b.end()); if(a[0].second!=b[0].second) cout << max(a[0].first,b[0].first) << endl; else if(n>=2) cout << min((a[0].first+b[0].first),min(max(a[0].first,b[1].first),max(a[1].first,b[0].first))) << endl; else cout << a[0].first+b[0].first << endl; return; } int main() { fast; lli t; t=1; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<string> s(h); for (int i = 0; i < h; i++) cin >> s[i]; int result = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') continue; if (j < w - 1) { if (s[i][j + 1] == '.') result++; } if (i < h - 1) { if (s[i + 1][j] == '.') result++; } } } cout << result << endl; return 0; }
/* © 2020-10-10 06:54:50 Franco1010 All Rights Reserved */ #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> #define f first #define s second #define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' using namespace std; typedef long double ld; typedef long long lli; typedef pair<lli,lli> ii; typedef vector<lli> vi; #define deb(x) cout << #x": " << (x) << endl; const lli N = 105; string mat[N]; lli h, w; const ii c[4] = {{-1, 0}, {+1, 0}, {0, +1}, {0, -1}}; bool valid(lli x, lli y){ return 0 <= x and x < h and 0 <= y and y < w and mat[x][y] == '.'; } lli solve(lli x, lli y){ lli res = 0; for(auto cc : c){ ii n = {x + cc.f, y + cc.s}; if(valid(n.f, n.s)) res++; } return res; } int main(){ _ cin >> h >> w; fore(i, 0, h) cin >> mat[i]; lli res = 0; fore(i, 0, h){ fore(j, 0, w){ if(mat[i][j] == '#') continue; lli foo = solve(i, j); res += foo; } } cout << res/2ll << ENDL; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define db double #define in inline namespace fast_io { char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny; in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;} in int read() { int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc; x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny; } in db gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);} in int gs(char *s) {char c,*t=s;while(c=gc(),c<=32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;} in void ot() {fwrite(sr,1,C+1,stdout);C=-1;} in void flush() {if(C>1<<22) ot();} template <typename T> in void write(T x,char t) { int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10); if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush(); } in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();} }; using namespace fast_io; #define pb push_back const int N=1e6+5,p=998244353; int n,m,ans=1,L,R,siz[N],a[N],vis[N],q[N],inq[N];vector<int>e[N];in void add(int u,int v) {e[u].pb(v),e[v].pb(u);} in void M(int &x) {x-=p;x+=x>>31&p;} void dfs1(int u,int fa) { vis[u]=1; for(int v:e[u]) if(!vis[v]) dfs1(v,u); else !inq[v]?L=u,R=v,inq[u]=inq[v]=1:inq[u]=1; } void get(int u,int fa) {siz[u]=1;for(int v:e[u])if(v^fa&&!inq[v])get(v,u),siz[u]+=siz[v];} void dfs2(int u,int fa) { for(int v:e[u]) if(v^fa&&inq[v]&&(u^L||(u==R&&v^L))) dfs2(v,u); get(u,0);m=1ll*m*siz[u]%p; } int main() { n=read();for(int i=1;i<=n;i++) a[i]=read(),add(i,a[i]); for(int i=1;i<=n;i++) if(!vis[i]) m=1,dfs1(i,0),dfs2(L,R),ans=1ll*ans*(m+1)%p; write((ans+p-1)%p,'\n'); return ot(),0; } //Author: disangan233
#include <cstdio> #include <cmath> #include <stack> #include <queue> #include <string> #include <iostream> #include <sstream> #include <ostream> #include <cstring> #include <vector> #include <cstdlib> #include <algorithm> #include <map> #include <iomanip> #include <set> #include <bitset> #include<unordered_map> #define INT_MINs -2000000000 #define INT_MAXs 1000000001 #define MID int mid=(l+r)/2 #define REP1(i,x,y) for(int i=x;i<y;i++) #define REP2(i,x,y) for(int i=x;i<=y;i++) #define ls (2*k) #define rs (2*k+1) #define lson l,mid,2*k #define rson mid+1,r,2*k+1 #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define IOS ios::sync_with_stdio(0);cin.tie(NULL); #define pb(a) push_back(a) #define pi acos(-1) #define endl '\n' using namespace std; typedef long long ll; typedef unsigned long long ull; const ll mod=998244353; const ll ba=131; ll qk(ll x,ll y){ll ans=1;while(y){if(y&1) ans=ans*x%mod;y>>=1;x=x*x%mod;}return ans;} const int dx[8] = { 0,-1,0,1,-1,-1,1,1}, dy[8] = { -1,0,1,0,-1,1,-1,1}; const int dxx[8]= {2,1,-1,-2,-2,-1, 1, 2},dyy[8]= {1,2,2,1, -1,-2,-2,-1}; int a[200005]; ll ans=0; int vis[200005]; int f[200005]; int finds(int x){ return f[x]==x?f[x]:f[x]=finds(f[x]); } void solve() { int n; cin>>n; for(int i=1;i<=n;i++) f[i]=i; for(int i=1;i<=n;i++){ cin>>a[i]; vis[a[i]]=1; } for(int i=1;i<=n;i++){ //if(vis[i]) continue; f[finds(i)]=finds(finds(a[i])); } for(int i=1;i<=n;i++){ if(vis[i]&&f[i]==i) ans++; } cout<<qk(1ll*2,ans)-1<<endl; } int main() { //IOS; solve(); return 0; }
#include <bits/stdc++.h> #include <cassert> #define rep(i, N) for (int i = 0; i < (N); ++i) #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep3(i, a, b) for (ll i = a; i >= b; --i) #define pb push_back #define eb emplace_back #define fi first #define se second #define all(c) begin(c), end(c) #define ok() puts(ok ? "Yes" : "No"); 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 (a > b) { a = b; return true; } return false; } using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vi>; using vii = vector<ii>; using gt = greater<int>; using minq = priority_queue<int, vector<int>, gt>; using P = pair<ll, ll>; template <class T> void takeUnique(vector<T> &v) { auto last = std::unique(v.begin(), v.end()); v.erase(last, v.end()); } const ll LINF = 1e18L + 1; const int INF = 1e9 + 1; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1}; // clang++ -std=c++11 -stdlib=libc++ // vi to[200005]; int d[200005]; ll ans[200005]; void add(int v, int from = -1) { for (int u : to[v]) { if (u == from) continue; ans[u] += ans[v]; add(u, v); } } void dfs(int v, int from = -1) { for (int u : to[v]) { if (u == from) continue; chmin(d[u], d[v] + 1); dfs(u, v); } } int main() { int n; cin >> n; rep(i, n) d[i] = INF; d[0] = 0; vi a(n - 1); vi b(n - 1); rep(i, n - 1) { cin >> a[i] >> b[i]; a[i]--; b[i]--; to[a[i]].pb(b[i]); to[b[i]].pb(a[i]); } dfs(0); int q; cin >> q; while (q--) { int t, i, x; cin >> t >> i >> x; i--; int avoid; int v; if (t == 1) { avoid = b[i]; v = a[i]; } else { avoid = a[i]; v = b[i]; } if (d[avoid] < d[v]) { ans[v] += x; } else { ans[0] += x; ans[avoid] -= x; } } add(0); rep(i, n) cout << ans[i] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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; } #define all(x) (x).begin(),(x).end() #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=1000000007,MAX=400005,INF=1<<30; vector<pair<int,int>> G[MAX]; ll imo[MAX]; bool seen[MAX]; int s[MAX],g[MAX],timer; pair<int,int> wh[MAX]; void DFS(int u){ seen[u]=1; s[u]=timer; timer++; for(auto to:G[u]){ if(seen[to.fi]) continue; wh[to.se]=mp(u,to.fi); DFS(to.fi); } g[u]=timer; timer++; } int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); int N;cin>>N; vector<pair<int,int>> E(N-1); for(int i=0;i<N-1;i++){ int a,b;cin>>a>>b; a--;b--; E[i]=mp(a,b); G[a].push_back(mp(b,i)); G[b].push_back(mp(a,i)); } DFS(0); //for(int i=0;i<N;i++) cout<<s[i]<<" "<<g[i]<<endl; int Q;cin>>Q; while(Q--){ int t,e,x;cin>>t>>e>>x; e--; if(t==1){ if(wh[e].fi==E[e].fi){ imo[0]+=x; imo[s[wh[e].se]]-=x; imo[g[wh[e].se]+1]+=x; imo[2*N]-=x; }else{ imo[s[wh[e].se]]+=x; imo[g[wh[e].se]+1]-=x; } }else{ if(wh[e].fi==E[e].se){ imo[0]+=x; imo[s[wh[e].se]]-=x; imo[g[wh[e].se]+1]+=x; imo[2*N]-=x; }else{ imo[s[wh[e].se]]+=x; imo[g[wh[e].se]+1]-=x; } } } for(int i=1;i<2*N;i++) imo[i]+=imo[i-1]; vector<ll> ans(N); for(int i=0;i<N;i++){ cout<<imo[s[i]]<<endl; } }
#include<bits/stdc++.h> using namespace std ; int main() { string str ; cin>>str ; bool f = 1 ; for(int i = 0 ; i < str.size() ; i++) if(str[i] != str[str.size()-i-1]) f = 0 ; if(f) cout<<"Yes"<<endl ; else { int j = str.size()-1 , i = 0 ; while( j >= 0 && str[j] == '0' && str[str.size()-j-1] != 0) j-- ; //int i = 0 ; bool f1 = 1 ; while(i <= j) { if(str[i] != str[j]) { f1 = 0 ; break ; } else i++,j-- ; } if(f1) cout<<"Yes" ; else cout<<"No" ; } }
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n % 2) cout << "Black" << endl; else cout << "White" << endl; }
#include<bits/stdc++.h> #define mp make_pair #define fi first #define se second #define VAL 100000 #define ll long long #define INF 0x3f3f3f3f #define pll pair<long long , long long> const int MAX=5e6+9; const ll mod=1e9+7; const int TOT_PRIMES=19; const int MAX_A=70; using namespace std; vector<pair<int , int > >adj[MAX]; bool check(int x, int y , int n , int m){ if(x<0||y<0||x>=n||y>=m) return false; else return true; } int dijikstra(int n , int m){ multiset<pair< int , int > > st; int dis[n*m+4]; bool vis[n*m+4]; for(int i = 0;i<n*m;i++){ dis[i]=INF; vis[i]=false; } dis[0]=0; st.insert({0 , 0}); while(!st.empty()){ pair<int , int > it = *st.begin(); int from = it.second; st.erase(st.begin()); // cout<<23; if(vis[from]) continue; vis[from]=true; //cout<<from<<endl; for(auto v:adj[from]){ if(dis[v.first]>=dis[from]+v.second){ dis[v.first]=dis[from]+v.second; st.insert({dis[v.first],v.first}); } } } return dis[(n)*m-1]; } int main() { int n , m; cin>>n>>m; int a[n+1][m+1] , b[n+1][m+1]; for(int i = 0;i<n;i++){ for(int j = 0;j<m-1;j++) cin>>a[i][j]; } for(int i = 0;i<n-1;i++){ for(int j = 0;j<m;j++) cin>>b[i][j]; } for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ if(check(i , j+1, n , m)){ adj[i*m+j].push_back({i*m+j+1 , a[i][j]}); } if(check(i , j-1, n , m)){ adj[i*m+j].push_back({i*m+j-1 , a[i][j-1]}); } if(check(i+1 , j ,n , m)){ adj[i*m+j].push_back({(i+1)*m+j , b[i][j]}); } for(int k = 1;k<=n;k++){ if(check(i-k , j, n , m)){ adj[i*m+j].push_back({(i-k)*m+j , 1+k}); } } } } cout<<dijikstra(n , m); }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i, s, t) for (int i = s; i < t; ++i) using namespace std; // using namespace atcoder; using ll = long long; using P = pair<int, int>; constexpr int INF = (int)2e9; constexpr ll INFll = (ll)1e18; constexpr int MOD = 1e9 + 7; struct edge { int to, cost; edge(int t, int c) : to(t), cost(c) {} }; vector<vector<edge>> G; int R, C; int dijkstra(int s, int t) { priority_queue<P, vector<P>, greater<P>> que; vector<int> d(2 * R * C, INF); que.emplace(s, 0); d[s] = 0; while (!que.empty()) { auto p = que.top(); que.pop(); int curd = p.first, curv = p.second; if (curd > d[curv]) continue; for (auto e : G[curv]) { if (curd + e.cost < d[e.to]) { d[e.to] = curd + e.cost; que.emplace(d[e.to], e.to); } } } return d[t]; } int main() { cin >> R >> C; G.resize(2 * R * C); rep(r, 0, R) rep(c, 0, C - 1) { int a; cin >> a; G[r * C + c].emplace_back(r * C + c + 1, a); G[r * C + c + 1].emplace_back(r * C + c, a); } rep(r, 0, R - 1) rep(c, 0, C) { int b; cin >> b; G[r * C + c].emplace_back((r + 1) * C + c, b); } rep(r, 0, R) rep(c, 0, C) { G[r * C + c].emplace_back(r * C + c + R * C, 1); G[r * C + c + R * C].emplace_back(r * C + c, 0); } rep(r, 0, R - 1) rep(c, 0, C) { G[(r + 1) * C + c + R * C].emplace_back(r * C + c + R * C, 1); } cout << dijkstra(0, R * C - 1) << endl; }
#include <cstdio> #include <algorithm> using namespace std; long long a[200005],t[200005]; pair<long long,int> x[200005]; long long treemax[800005],treemin[800005],lazy[800005],sum,ans[200005]; bool flazy[800005]; int p; void build(int p,int l,int r) { if(l==r) { treemax[p]=treemin[p]=x[l].first; return; } int mid=(l+r)/2; build(p<<1,l,mid); build(p<<1|1,mid+1,r); treemax[p]=max(treemax[p<<1],treemax[p<<1|1]); treemin[p]=min(treemin[p<<1],treemin[p<<1|1]); } void pushdown(int p,int l,int r) { if(flazy[p]) { treemax[p<<1]=treemin[p<<1]=treemax[p<<1|1]=treemin[p<<1|1]=lazy[p<<1]=lazy[p<<1|1]=lazy[p]; flazy[p<<1]=flazy[p<<1|1]=true; flazy[p]=false; } } void update(int p,int l,int r,int ql,int qr,long long d) { if(ql==l&&qr==r) { treemax[p]=treemin[p]=d; lazy[p]=d; flazy[p]=true; return; } pushdown(p,l,r); int mid=(l+r)/2; if(qr<=mid) update(p<<1,l,mid,ql,qr,d); else if(ql>mid) update(p<<1|1,mid+1,r,ql,qr,d); else { update(p<<1,l,mid,ql,mid,d); update(p<<1|1,mid+1,r,mid+1,qr,d); } treemax[p]=max(treemax[p<<1],treemax[p<<1|1]); treemin[p]=min(treemin[p<<1],treemin[p<<1|1]); } int querymin(int p,int l,int r,long long d) { if(l==r) return l; pushdown(p,l,r); int mid=(l+r)/2; if(treemin[p<<1|1]<d) return querymin(p<<1|1,mid+1,r,d); else return querymin(p<<1,l,mid,d); } int querymax(int p,int l,int r,long long d) { if(l==r) return l; pushdown(p,l,r); int mid=(l+r)/2; if(treemax[p<<1]>d) return querymax(p<<1,l,mid,d); else return querymax(p<<1|1,mid+1,r,d); } long long query(int p,int l,int r,int d) { if(l==r) return treemax[p]; pushdown(p,l,r); int mid=(l+r)/2; if(d<=mid) return query(p<<1,l,mid,d); else return query(p<<1|1,mid+1,r,d); } int main() { int N,Q; scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%lld%lld",&a[i],&t[i]); scanf("%d",&Q); for(int i=1;i<=Q;i++) { scanf("%lld",&x[i].first); x[i].second=i; } sort(x+1,x+Q+1); build(1,1,Q); for(int i=1;i<=N;i++) if(t[i]==1) sum+=a[i]; else if(t[i]==2) { if(treemin[1]>=a[i]-sum) continue; p=querymin(1,1,Q,a[i]-sum); update(1,1,Q,1,p,a[i]-sum); } else { if(treemax[1]<=a[i]-sum) continue; p=querymax(1,1,Q,a[i]-sum); update(1,1,Q,p,Q,a[i]-sum); } for(int i=1;i<=Q;i++) ans[x[i].second]=query(1,1,Q,i); for(int i=1;i<=Q;i++) printf("%lld\n",ans[i]+sum); return 0; }
#include <bits/stdc++.h> using namespace std; template<typename T> void debug(vector<T> item) { for (auto i : item) { cout << i << " "; } cout << endl; } int main() { int N; cin >> N; int64_t mn = -(int64_t)1e9; int64_t mx = 1e9; int64_t sum = 0; for (int i = 0; i < N; ++i) { int64_t a, b; cin >> a >> b; if (b == 1) { sum += a; mn += a; mx += a; } else if (b == 2) { mn = max(a, mn); mx = max(a, mx); } else { mn = min(a, mn); mx = min(a, mx); } } int Q; cin >> Q; for (int i = 0; i < Q; ++i) { int64_t q; cin >> q; q += sum; if (mn <= q && q <= mx) { cout << q << endl; } else if (q < mn) { cout << mn << endl; } else { cout << mx << endl; } } return 0; }
#include <iostream> using namespace std; int n, max_ = 1000, min_ = 1; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; int temp; for(int i = 0;i < n;++i) { cin >> temp; min_ = max(temp, min_); } for(int i = 0;i < n;++i) { cin >> temp; max_ = min(temp, max_); } if(max_ - min_ + 1 <= 0){cout << 0;} else{cout << max_ - min_ + 1;} return 0; }
#include <stdio.h> long long int x[200010]; long long int sum[200010],sum2[200010],max[200010]; int main() { int a; scanf("%d",&a); for(int i=1;i<=a;i++) scanf("%lld",&x[i]); for(int i=1;i<=a;i++) sum[i] = sum[i-1] + x[i]; for(int i=1;i<=a;i++) sum2[i] = sum2[i-1] + sum[i]; max[1] = x[1]; for(int i=1;i<=a;i++) max[i] = max[i-1]>x[i]?max[i-1]:x[i]; for(int i=1;i<=a;i++) { long long int S = sum[i] + sum2[i-1] + i*max[i]; printf("%lld\n",S); } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (n); ++i) #define replr(i, l, r) for (int i = (l); i < (r); ++i) const int mod = 1e9 + 7; vector<int> T[202020]; ll ans[202020], C[202020]; void rec(int x, ll c, int par) { c += C[x]; for (int to : T[x]) { if (to == par) continue; rec(to, c, x); } ans[x] = c; } int main() { int n, q; cin >> n; vector<int> A(n), B(n), X(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; A[i] = a; B[i] = b; T[a].push_back(b); T[b].push_back(a); } queue<P> Q; Q.push({1, 0}); while (!Q.empty()) { P p = Q.front(); Q.pop(); if (X[p.second] > 0) continue; X[p.second] = p.first; for (int to : T[p.second]) Q.push({p.first + 1, to}); } cin >> q; rep(i, q) { ll t, e, x; cin >> t >> e >> x; e--; int a = A[e], b = B[e]; if (X[a] > X[b]) { if (t == 1) C[a] += x; else { C[0] += x; C[a] -= x; } } else { if (t == 1) { C[0] += x; C[b] -= x; } else C[b] += x; } } rec(0, 0, -1); rep(i, n) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll n, a, b, q, c[200007], buf, p[200007]; vector<pair<ll, ll>> e; vector<pair<ll, bool>> op[200007]; vector<ll> adj[200007]; void find_parent(ll x, ll par) { p[x] = par; for(ll i : adj[x]) { if(i != par) { find_parent(i, x); } } } void dfs(ll x, ll add) { ll cur = add; for(pair<ll, bool> i : op[x]) { if(i.second) { cur += i.first; } else { cur -= i.first; buf += i.first; } } c[x] += cur; for(ll i : adj[x]) { if(i != p[x]) { dfs(i, cur); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(ll i = 0; i < n - 1; ++i) { cin >> a >> b; e.push_back({a, b}); adj[a].push_back(b); adj[b].push_back(a); } find_parent(1, -1); cin >> q; for(ll i = 0; i < q; ++i) { ll t, k, x; cin >> t >> k >> x; k--; if(t == 1) { //1 - adding to subtree, 0 - subtracting and adding to buffer if(p[e[k].first] == e[k].second) op[e[k].first].push_back({x, 1}); else op[e[k].second].push_back({x, 0}); } else { if(p[e[k].second] == e[k].first) op[e[k].second].push_back({x, 1}); else op[e[k].first].push_back({x, 0}); } } dfs(1, 0); for(ll i = 1; i <= n; ++i) cout << c[i] + buf << '\n'; return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll MOD = (ll)1000000007; vector<ll> nums; int occ[26]; int main() { ios::sync_with_stdio(false); cin.tie(0); int t, m, n; ll k, ans = 0; string s1, s2; bool sad = false; cin >> s1; for(int i = s1.size()-1; i >= 0; i--) { occ[s1.at(i)-'a']++; if(i != s1.size()-1) { if(s1.at(i) == s1.at(i+1)) { ans += s1.size() - i - occ[s1.at(i)-'a']; for(int j = 0; j < 26; j++) occ[j] = 0; occ[s1.at(i)-'a'] = s1.size()-i; } } } cout << ans << '\n'; }
#include <bits/stdc++.h> #include <cassert> #include <functional> #define rep(i, N) for (int i = 0; i < (N); ++i) #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep3(i, a, b) for (ll i = a; i >= b; --i) #define pb push_back #define eb emplace_back #define fi first #define se second #define all(c) begin(c), end(c) #define ok() puts(ok ? "Yes" : "No"); 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 (a > b) { a = b; return true; } return false; } using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vi>; using vii = vector<ii>; using gt = greater<int>; using minq = priority_queue<int, vector<int>, gt>; using P = pair<ll, ll>; const ll LINF = 1e18L + 1; const int INF = 1e9 + 1; // clang++ -std=c++11 -stdlib=libc++ int N; int main() { cin >> N; vi p(N); rep(i, N) cin >> p[i]; vector<int> used(200005); int mn = 0; rep(i, N) { int a = p[i]; used[a] = true; while (mn == a || used[mn]) { mn++; } cout << mn << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); // freopen((s + ".in").c_str(), "r", stdin); // freopen((s + ".out").c_str(), "w", stdout); } int descending(string a) { int arr[a.length()]; for (int i = 0; i < a.length(); i++) { arr[i] = a[i] - '0'; } sort(arr, arr + (sizeof(arr) / sizeof(arr[0])), greater<int>()); for (int i = 0; i < a.length(); i++) { a[i] = arr[i] + '0'; } int ab = stoi(a); return ab; } int ascending(string a) { int arr[a.length()]; for (int i = 0; i < a.length(); i++) { arr[i] = a[i] - '0'; } sort(arr, arr + (sizeof(arr) / sizeof(arr[0]))); bool zero = true; string s = ""; for (int i = 0; i < a.length(); i++) { if (arr[i] != 0) zero = false; else continue; if (!zero) s.push_back(arr[i] + '0'); } int ab = stoi(s); return ab; } int main() { setIO(""); string n; cin >> n; int k; cin >> k; for (int i = 0; i < k; i++) { int nDup = descending(n) - ascending(n); n = to_string(nDup); if (n == "0") break; } cout << n; }
#include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; int ak(int num){ string n = to_string(num); sort(n.begin(), n.end()); int g2 = stoi(n); reverse(n.begin(), n.end()); int g1 = stoi(n); return g1- g2; } int main(){ int n; cin >> n; int k; cin >> k; for(int i=0;i<k;i++){ n = ak(n); } cout << n; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef vector<int> V; #define F(N,S,E) for(int N=(int)(S);N<=(int)(E);N++) #define R(N,S,E) for(int N=(int)(S);N>=(int)(E);N--) #define D(N,E) for(int N=0;N<(int)(E);N++) LL read(){LL x;scanf("%lld",&x);return x;} const int N=2.1e5; int a[N],b[N]; int st[N]; int main(){ int n=read(); F(i,1,n)a[n-i+1]=read(); F(i,1,n)b[i]=read(); F(i,1,n)if(a[i]<b[i])swap(a[i],b[i]); // F(i,1,n)cerr<<a[i]<<" "<<b[i]<<endl; priority_queue<pair<int,int> > q1; F(i,1,n){ if(!q1.empty()&&q1.top().first+b[i]>0){ pair<int,int> t=q1.top(); q1.pop(); if(--st[t.second]==0){ q1.push(make_pair(-a[t.second],t.second)); } st[i]=1; q1.push(make_pair(-b[i],i)); } else{ q1.push(make_pair(-a[i],i)); } } // F(i,1,n)cerr<<i<<" "<<st[i]<<endl; LL ans=0; F(i,1,n)ans+=st[i]==0?a[i]:st[i]==1?a[i]+b[i]:0; printf("%lld\n",ans); return 0; }
// Good Bye rng_58 /** * author: otera **/ #include<bits/stdc++.h> using namespace std; #define int long long typedef long long ll; typedef long double ld; const int inf=1e9+7; const ll INF=1LL<<60; #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++) typedef pair<int, int> P; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(),c.end() 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; } void solve() { int n; cin >> n; for(int i = 0; i < n; ++ i) { cout << 2 * i % n + 1 << " " << (2 * i + 1) % n + 1 << "\n"; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(20); //int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long power(int y) { int i; long long power=1; if(y == 0) return 1; for(i=1;i<=y;i++) power=power*2; return power; } int main() { int n; cin >> n; string s[n]; for(int i = 0; i < n; i++) cin >> s[i]; long long x = 1; for(int i = 0; i < n; i++){ if(s[i] == "OR"){ x = x + power(i + 1); } } cout << x; return 0; }
#include <iostream> #include <cmath> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> dp(2,1); for(int i = 0; i < n;++i){ string s; cin >> s; vector <long long > p(2,0); swap (dp,p); for(int j = 0; j < 2;++j){ for(int x = 0;x < 2; ++x){ int nj = j; if(s == "AND"){ nj &= x; } else nj |= x; dp[nj] += p[j]; } } } cout << dp[1] << endl; return 0; } /*long long count(string S[],long long n){ if(n == 0){ return 1; } else if(S[n-1] == "OR"){ return (pow(2,n) + count(S,n-1)); } else return count(S,n-1); } /*int main() { long long n; cin >> n; string S[n]; if(n == 0){ cout << 0 << endl; return 0; } for(long long i = 0; i < n; ++i){ cin >> S[i]; } cout << count(S,n) << endl; return 0; /*int cnt = 0; for(int bit = 0; bit < (1<<(n+1)); ++bit){ int bitset[n+1] = {0}; for(int k = 0; k <= n; ++k){ int Div = (1 << k); bitset[k] = (bit / Div) % 2; } int y[n+1]; y[0] = bitset[0]; for(int i = 1; i <= n; ++i){ if(S[i-1] == "AND"){ y[i] = y[i-1] & bitset[i]; } else y[i] = y[i-1] | bitset[i]; } if(y[n] == 1){ cnt++; } } cout << cnt << endl; }*/
#include <iostream> using namespace std; int main(){ int h,w; cin >> h >> w; char** s = (char**)calloc(h, sizeof(char*)); for(int i = 0; i < h; i++){ s[i] = (char*)calloc(w+1, sizeof(char)); cin >> s[i]; } //→と↓だけ見る int count = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(s[i][j] == '.'){ if(i == h-1 && j == w-1) break; if(i == h-1 && j != w-1){ if(s[i][j+1] == '.') count++; }else if(j == w-1){ if(s[i+1][j] == '.') count++; } else{ if(s[i+1][j] == '.' && s[i][j+1] == '.') count+=2; else if(s[i+1][j] == '.' || s[i][j+1] == '.')count++; } } } } cout << count << endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define all(v) v.begin(),v.end() #define rall(v) v.begin(),v.end() #define sz(x) (int)sz.size() #define PB push_back #define PI 3.1415926535897932384626433832795 #define what(x) cout<<#x<<" is "<<x<<endl; using namespace std; #ifdef LOCAL//ONLINE_JUDGE #include "D:\c_c++\template.h" #else #define debug(...) 42 #endif ll powmod(ll a,ll b,ll mod) { ll res=1;a%=mod; for(;b;b>>=1){ if(b&1)res=res*a%mod; a=a*a%mod; } return res; } int h,w; bool isval(int x,int y){ return x>=0 && x<h && y>=0 && y<w; } int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; void solve(){ cin>>h>>w; string s[h]; for(int i=0;i<h;i++)cin>>s[i]; ll cnt=0; for(int i=0;i<h;i++) for(int j=0;j<w;j++){ if(s[i][j]=='#')continue; for(int d=0;d<4;d++){ int nx=i+dir[d][0],ny=j+dir[d][1]; if(isval(nx,ny)){ debug(i,j,nx,ny); if(s[nx][ny]=='.')cnt++; } } } debug(cnt); cout<<(cnt/2LL)<<'\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(0); // #ifdef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int tc=1; // cin>>tc; while(tc--)solve(); }
// Code Written by Pinaki Bhattacharjee (pinakipb2) #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; // typedef typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; // template<typename num_t> using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>; // #define #define LOCAL #define endl "\n" #define PI 2*acos(0.0) #define ff first #define ss second #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define mpr make_pair #define all(x) (x).begin(),(x).end() #define ppc __builtin_popcountll void pinakipb2() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} // constants const int MAX = 1e7 + 10; const int MOD = (int) 1e9 + 7; ll ans; vector<ll> parent(MAX),sz(MAX); void make_set(ll v) { parent[v] = v; sz[v] = 1; } ll find_set(ll v) { if(v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(ll a, ll b) { a = find_set(a); b = find_set(b); if(a==b) return; ans++; if(a!=b) { if(sz[a]<sz[b]) swap(a,b); parent[b] = a; sz[a] += sz[b]; } } int main() { pinakipb2(); ll n; cin>>n; for(int i=1;i<=MAX-2;i++) { make_set(i); } vector<ll> v(n); for(int i=0;i<n;i++) cin>>v[i]; for(int i=0;i<n;i++) { union_sets(v[i],v[n-i-1]); } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b,ll mod=MOD){ ll res = 1; while(b){ if(b&1)res = res * x % mod; x = x * x % mod; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> ostream& operator<<(ostream&os, vector<t> &v){ os << "{"; for(t &i:v){ os << i << ", "; } os << "}"; return os; } template<class t = long long> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } string reversed(string str){ reverse(str.begin(),str.end()); return str; } long long gcd(long long a,long long b){ while(b){ a %= b; swap(a,b); } return a; } long long lcm(long long a,long long b){ return a / gcd(a,b) * b; } } using namespace templates; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n = in(); double ans = 0; rep(i,1,n){ ans += 1 / (1 - (n-i) / (double) n); } printf("%.20lf\n",ans); return 0; }
#include <iostream> using namespace std; int main() { int a, b; cin>>a>>b; if(a+b>=15 && b>=8){ cout<<1; }else if(a+b>=10 && b>=3){ cout<<2; }else if(a+b>=3){ cout<<3; }else{ cout<<4; } }
#include <bits/stdc++.h> // #include "atcoder/all" #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define rep(i,n) for (int i = 0; i < (n); ++i) #define rep2(i,k,n) for (int i = (k); i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; const ll INF = (ll)1e18; void chmin(ll &a, ll b) { a = min(a, b); } void chmax(ll &a, ll b) { a = max(a, b); } void solve() { int a, b; cin >> a >> b; int ans = 0; int c = a+b; if (c >= 15 && b >= 8) ans = 1; else if (c >= 10 && b >= 3) ans = 2; else if (c >= 3) ans = 3; else ans = 4; cout << ans << endl; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef ptrdiff_t isize; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef size_t usize; typedef float f32; typedef double f64; #define unordered_set set auto main() -> i32 { ios::sync_with_stdio(false), cin.tie(nullptr); i64 n; string s, x; cin >> n >> s >> x; unordered_set<i64> need; need.insert(0); for (i64 i = n - 1; i >= 0; i--) { if (x[i] == 'A') { unordered_set<i64> next; for (i64 d = 0; d < 7; d++) { i64 ned = (d * 3) % 7 + 0; if (need.find(ned) != need.end()) { i64 ned = ((d * 3) % 7 + s[i] - '0') % 7; if (need.find(ned) != need.end()) { next.insert(d); } } } need = next; } else { unordered_set<i64> next; for (auto d : need) { i64 nxt = (d - (s[i] - '0')) % 7; if (nxt < 0) nxt += 7; nxt = (nxt * 5) % 7; next.insert(nxt); nxt = (d - 0) % 7; if (nxt < 0) nxt += 7; nxt = (nxt * 5) % 7; next.insert(nxt); } need = next; } } if (need.find(0) != need.end()) { cout << "Takahashi"; } else { cout << "Aoki"; } cout << "\n"; return 0; }
// 問題の URL を書いておく // #include <bits/stdc++.h> using namespace std; //#define ENABLE_PRINT #if defined(ENABLE_PRINT) #define Print(v) \ do {\ cout << #v << ": " << v << endl; \ }while(0) #define PrintVec(v) \ do {\ for(int __i = 0; __i < v.size(); ++__i) \ { \ cout << #v << "[" << __i << "]: " << v[__i] << endl; \ }\ }while(0) #else #define Print(v) ((void)0) #define PrintVec(v) ((void)0) #endif #define rep(i, n) for(int i = 0; i < (int)(n); ++i) using ll = long long; // 0 なら高橋勝ち // 1 なら青木勝ち int calc(int round, int m, string& S, string& T, vector<vector<int>>& state) { Print(round); Print(m); if(state[round][m] != -1) return state[round][m]; // 最終ラウンド auto n = S[round] - '0'; if(round == (int)state.size() - 1) { if(T[round] == 'A') { if(m != 0 || (m + n) % 7 != 0) { state[round][m] = 1; } else state[round][m] = 0; } else { if(m == 0 || (m + n) % 7 == 0) { state[round][m] = 0; } else state[round][m] = 1; } Print(state[round][m]); return state[round][m]; } if(T[round] == 'A') { if(calc(round + 1, (m * 10) % 7, S, T, state) == 1 || calc(round + 1, (m + n) * 10 % 7, S, T, state) == 1) { state[round][m] = 1; return 1; } state[round][m] = 0; return 0; } else { if(calc(round + 1, (m * 10) % 7, S, T, state) == 0 || calc(round + 1, (m + n) * 10 % 7, S, T, state) == 0) { state[round][m] = 0; return 0; } state[round][m] = 1; return 1; } } int main(int, const char**) { int N; cin >> N; string S, T; cin >> S >> T; vector<vector<int>> state(N, vector<int>(7, -1)); auto ans = calc(0, 0, S, T, state); Print(ans); cout << (ans == 0 ? "Takahashi" : "Aoki") << endl; return 0; }
#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 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 V, T, S, D; string Str; cin >> V >> T >> S >> D; ll vanish_st = V*T; ll vanish_end = V*S; if(vanish_st <= D && vanish_end >= D) { cout << "No" << endl; } else { cout << "Yes" << endl; } return 0; }
#include <bits/stdc++.h> signed main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int v, tstart, tfinish, d; std::cin >> v >> tstart >> tfinish >> d; if (v * tstart <= d and d <= v * tfinish) { printf("No\n"); } else { printf("Yes\n"); } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; const int maxn=400005; const int logn=21; const int inf=1ll<<60; struct Edge { int to,next; Edge(int to=0,int next=0):to(to),next(next) {} }; Edge a[maxn*2]; int cnt; int top; int Max; int root; int s[maxn]; int h[maxn]; int t[maxn]; int ss[maxn]; int lg[maxn*2]; int ans[maxn]; int pre[maxn]; int depth[maxn]; int st[logn][maxn*2]; bool vis[maxn]; void add(int x,int y) { a[++cnt]=Edge(y,h[x]); h[x]=cnt; } void dfs(int x,int fa) { st[0][++top]=x; pre[x]=top; // t[x]=top; depth[x]=depth[fa]+1; for(int i=h[x]; i; i=a[i].next) { if(a[i].to!=fa) { dfs(a[i].to,x); st[0][++top]=x; } } } void dfs2(int x,int fa) { ++top; t[x]=top; for(int i=h[x]; i; i=a[i].next) { if(a[i].to!=fa&&!vis[a[i].to]) { dfs2(a[i].to,x); ++top; } } } int getmin(int x,int y) { return depth[x]<depth[y]?x:y; } int lca(int x,int y) { x=pre[x],y=pre[y]; if(x>y) { swap(x,y); } int k=lg[y-x+1]; return getmin(st[k][x],st[k][y-(1<<k)+1]); } int dist(int x,int y) { return depth[x]+depth[y]-2*depth[lca(x,y)]; } int prt[maxn]; void dfsmax(int x,int dis) { s[++top]=x; if(dis>Max) { Max=dis; root=x; // memcpy(ss,s,sizeof(ss)); ss[0]=top; } vis[x]=1; for(int i=h[x]; i; i=a[i].next) { if(!vis[a[i].to]) { prt[a[i].to]=x; dfsmax(a[i].to,dis+1); } } vis[x]=0; --top; } void prepare() { top=0; Max=0; } void getdis(int x) { prepare(); dfsmax(x,0); prepare(); int tp=root; dfsmax(root,0); for(int i=root;i!=tp;i=prt[i]) { ans[++ans[0]]=i; } ans[++ans[0]]=tp; } signed main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int n; cin>>n; for(int i=1; i<=n-1; i++) { int x,y; cin>>x>>y; add(x,y); add(y,x); } dfs(1,0); // for(int i=2; i<=top; i++) { // lg[i]=lg[i>>1]+1; // } // for(int i=1; (1<<i)<=top; i++) { // for(int j=1; j+(1<<i)-1<=top; j++) { // st[i][j]=getmin(st[i-1][j],st[i-1][j+(1<<(i-1))]); // } // } getdis(1); top=0; // dfs2(root,0); // int k=Max; // memcpy(ans,ss,sizeof(ans)); for(int i=1;i<=ans[0];i++) { vis[ans[i]]=1; } t[ans[1]]=1; top=1; for(int i=2;i<=ans[0]-1;i++) { t[ans[i]]=++top; for(int j=h[ans[i]];j;j=a[j].next) { if(!vis[a[j].to]) { dfs2(a[j].to,0); ++top; // p=max(p,Max); } } } t[ans[ans[0]]]=++top; // int p=-1; // for(int i=1;i<=ans[0];i++) { // for(int j=h[ans[i]];j;j=a[j].next) { // if(!vis[a[j].to]) { // getdis(a[j].to); // p=max(p,Max); // } // } // } // int sum=1+k+2*(p+1); // t[ans[1]]=1; // t[ans[ans[0]]]=sum; // for(int i=1;i<=ans[0];i++) { // // for(int j=h[ans[i]];j;j=a[j].next) { // if(!vis[a[j].to]) { // getdis(a[j].to); // p=max(p,Max); // } // } // } //// for(int i=1;i<=n;i++) { //// if(i!=ans[1]&&i!=ans[ans[0]]) { //// t[i]=dist(ans[1],i)+1; //// } //// } for(int i=1; i<=n; i++) { cout<<t[i]<<" "; } }
#include<algorithm> #include<cstdio> #define MaxN 400500 using namespace std; int f[MaxN],o[MaxN],c[MaxN]; int find(int u) {return f[u]==u ? u : f[u]=find(f[u]);} void merge(int u,int v){ u=find(u);v=find(v); if (u!=v){ o[f[u]=v]+=o[u]; c[v]+=c[u]; }o[v]++; } bool vis[MaxN]; int n; int main() { scanf("%d",&n); for (int i=1;i<=400000;i++)c[f[i]=i]=1; for (int i=1,u,v;i<=n;i++){ scanf("%d%d",&u,&v); merge(u,v); } int ans=0; for (int i=1;i<=400000;i++){ int u=find(i); if (vis[u])continue; vis[u]=1; ans+=o[u]>=c[u] ? c[u] : c[u]-1; }printf("%d",ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define pb push_back typedef long long ll; typedef pair<ll,ll> P; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; int main(){ int x,y; cin>>x>>y; int r=min(x,y); int m=max(x,y); if(r+3>m)cout<<"Yes"; else{ cout<<"No"; } }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <x86intrin.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define V vector template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;} template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;} template<typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);} V<int> dx = {-1, 1, 0, 0}; V<int> dy = { 0, 0, -1, 1}; int main () { int h, w; cin >> h >> w; V<string> a(h); rep(i, h) cin >> a[i]; V<V<int>> d(h, V<int> (w, -1)); int sx, sy, gx, gy; V<set<P>> st(26); rep(i, h) rep(j, w) { if (a[i][j] == 'S') { sx = i, sy = j; } if (a[i][j] == 'G') { gx = i, gy = j; } if ('a' <= a[i][j] && a[i][j] <= 'z') { st[a[i][j] - 'a'].insert({i, j}); } } d[sx][sy] = 0; queue<P> que; que.push({sx, sy}); V<bool> checked(26, false); while (!que.empty()) { auto v = que.front(); que.pop(); int x = v.first, y = v.second; rep(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (a[nx][ny] == '#') continue; if (d[nx][ny] != -1) continue; d[nx][ny] = d[x][y] + 1; que.push({nx, ny}); } if ('a' <= a[x][y] && a[x][y] <= 'z') { int m = a[x][y] - 'a'; if (checked[m]) continue; for (auto p : st[m]) { int nx = p.first, ny = p.second; if (d[nx][ny] != -1) continue; d[nx][ny] = d[x][y] + 1; que.push({nx, ny}); } //checked[m] = true; st[m] = set<P> (); } } cout << d[gx][gy] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") typedef long long ll; typedef long double ld; typedef pair<ll,ll> ii; #define all(a) a.begin(),a.end() #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define mp make_pair #define F first #define S second #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define nl "\n" const int MOD = 1e9+7; const ld PI = 3.1415926535898; const ll INF = LLONG_MAX; #define N (int)2e5+5 ll base_mat[3][3]; ll mat[N][3][3]; void update(int i) { for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { for(int p=0;p<3;p++) { mat[i][j][k]+=base_mat[j][p]*mat[i-1][p][k]; } } } } int main() { ios; int n; cin>>n; ll p[n+1][2]; for(int i=1;i<=n;i++) { cin>>p[i][0]>>p[i][1]; } int m; cin>>m; memset(mat,0,sizeof(mat)); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i==j)mat[0][i][j]=1; } } for(int i=1;i<=m;i++) { ll t,p; cin>>t; memset(base_mat,0,sizeof(base_mat)); if(t==1) { base_mat[0][1]=1; base_mat[1][0]=-1; base_mat[2][2]=1; } else if(t==2) { base_mat[0][1]=-1; base_mat[1][0]=1; base_mat[2][2]=1; } else if(t==3) { cin>>p; base_mat[0][0]=-1; base_mat[0][2]=2*p; base_mat[1][1]=1; base_mat[2][2]=1; } else if(t==4) { cin>>p; base_mat[0][0]=1; base_mat[1][2]=2*p; base_mat[1][1]=-1; base_mat[2][2]=1; } update(i); } int q; cin>>q; while(q--) { int after_op,point_num; cin>>after_op>>point_num; ll x=p[point_num][0],y=p[point_num][1]; ll ansx=mat[after_op][0][0]*x+mat[after_op][0][1]*y+mat[after_op][0][2]; ll ansy=mat[after_op][1][0]*x+mat[after_op][1][1]*y+mat[after_op][1][2]; cout<<ansx<<" "<<ansy<<nl; } }
#include "bits/stdc++.h" #include <chrono> #include <random> #define lli long long int using namespace std; #define mod 1000000007 #define MOD 1000000037 #define mod1 998244353 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define INF 1000000000 #define common cout << "Case #" << w+1 << ": " #define MAXN 200000 int main() { int n; cin >> n; vector<pair<lli,lli> >v(n); for(int i=0;i<n;i++) cin >> v[i].first >> v[i].second; vector<pair<lli,pair<lli,lli> > >xc,yc; lli px=0,py=0,x1=1,y1=0,x2=0,y2=1; int m; cin >> m; lli t,p; for(int i=0;i<m;i++) { cin >> t; if(t==1) { swap(py,px); swap(x1,x2);swap(y1,y2); py=-py;x2=-x2;y2=-y2; } else if(t==2) { swap(py,px); swap(x1,x2);swap(y1,y2); px=-px;x1=-x1;y1=-y1; } else if(t==3) { cin >> p; x1=-x1;y1=-y1; px=2*p-px; } else if(t==4) { cin >> p; x2=-x2;y2=-y2; py=2*p-py; } xc.push_back({px,{x1,y1}}); yc.push_back({py,{x2,y2}}); } int q; cin >> q; for(int i=0;i<q;i++) { int a,b; cin >> a >> b; --a;--b; if(a==-1) { cout << v[b].first << " " << v[b].second << endl; continue; } lli x=xc[a].first+xc[a].second.first*v[b].first+xc[a].second.second*v[b].second; lli y=yc[a].first+yc[a].second.first*v[b].first+yc[a].second.second*v[b].second; cout << x << " " << y << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; while(k--) { if(n%200) n = n*1000+200; else n /= 200; } cout << n; return 0; }
#include <map> #include <set> #include <stack> #include <queue> #include <deque> #include <cmath> #include <ctime> #include <chrono> #include <random> #include <bitset> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> #include <iostream> #include <algorithm> #include <unordered_map> #define fi first #define se second #define il inline #define Ld double #define pc putchar #define rg register #define ll long long #define lll __int128 #define pb push_back #define mp make_pair #define It ::iterator #define W(p) while(p--) #define pll pair<ll, ll> #define pli pair<ll, int> #define pil pair<int, ll> #define pii pair<int, int> #define ull unsigned long long #define all(x) x.begin(), x.end() #define def(N, K, b) const K N = b; #define FOR(i, b, s) for(int i = (b); i < (s); ++i) #define rep(i, b, s) for(int i = (b); i <= (s); ++i) #define per(i, b, s) for(int i = (b); i >= (s); --i) #define FOR_(i, b, s) for(int i = (b); i > (s); --i) #define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); namespace IO { const int SIZE = (1 << 20) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1; char _st[55]; int _qr = 0; inline char gc() { return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++) : *iS++); } inline void qread() {} template<class T1, class ...T2> inline void qread(T1 &IEE, T2&... ls) { register T1 __ = 0, ___ = 1; register char ch; while(!isdigit(ch = gc())) ___ = (ch == '-') ? -___ : ___; do { __ = (__ << 1) + (__ << 3) + (ch ^ 48); }while(isdigit(ch = gc())); __ *= ___; IEE = __; qread(ls...); return ; } template<class T> inline void QreadArr(T Begin, T End) { while(Begin != End) { qread(*Begin); ++Begin; } } inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; return ; } inline void putc_(char _x) { *oS++ = _x; if(oS == oT) flush(); } inline void qwrite() {} template<class T1, class ...T2> inline void qwrite(T1 IEE, T2... ls) { if(!IEE) putc_('0'); if(IEE < 0) putc_('-'), IEE = -IEE; while(IEE) _st[++_qr] = IEE % 10 + '0', IEE /= 10; while(_qr) putc_(_st[_qr--]); qwrite(ls...); return ; } template<class T> inline void WriteArr(T Begin, T End) { const char Kg = ' ', Edl = '\n'; while(Begin != End) { qwrite(*Begin); ++Begin; putc_(Kg); } putc_(Edl); } struct Flusher_{ ~Flusher_() { flush(); } } io_flusher; } using namespace IO; using namespace std; mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count()); ll My_Rand(ll Mod) { return (ull)(rnd()) % Mod; } template<class T1, class T2> T1 Min(const T1 x, const T2 y) { return x > y ? y : x; } template<class T1, class T2> T1 Max(const T1 x, const T2 y) { return x > y ? x : y; } template<class T1, class T2> void To_max(T1 &x, const T2 y) { x < y ? x = y : x = x; } template<class T1, class T2> void To_min(T1 &x, const T2 y) { x > y ? x = y : x = x; } il ll qmul(ll x, ll y, ll p) { ll l = x * (y >> 25) % p * (1 << 25) % p; ll r = x * (y & ((1 << 25) - 1)) % p; return (l + r) % p; } il ll qpow(ll n, ll base, ll mod = 1e18) { ll ret = 1; while(n) { if(n & 1) ret = ret * base % mod; base = base * base % mod; n >>= 1; } return ret % mod; } il ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); } il ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } void InitHash(ull &base, int &prime, ull &mod) { base = 131, prime = 233317, mod = 212370440130137957ll; } //header int main() { ll n, k; qread(n, k); W(k) { if(n % 200 == 0) n /= 200; else n *= 1000, n += 200; } cout << n << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <climits> #include <cmath> #include <iomanip> using namespace std; #define vi vector<int> #define vii vector<vector<int>> #define ll long long int void testcase(){ double a,b,c,d; cin>>a>>b>>c>>d; cout << setprecision(12) << (a * d + c * b)/(b + d) << "\n"; } int main(){ ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0); testcase(); 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); cout << std::setprecision(12); cin.tie(NULL); get(sx); get(sy); get(gx); get(gy); double answer = double( sy*gx + sx*gy ) / double(gy + sy) ; cout << answer << endl; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long int lli; typedef unsigned long long int ulli; typedef long double ld; #define vec(s) vector<s> #define vvec(s) vector<vector<s>> typedef vector<lli> vi; typedef vector<vi> vvi; typedef pair<lli, lli> pii; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define Yes(x) cout << (x ? "Yes" : "No") << endl; #define YES(x) cout << (x ? "YES" : "NO") << endl; #define out(s) cout << s << endl; #define Reverse(x) reverse(all(x)); #define Sort(x) sort(all(x)); #define pb(s) push_back(s); #define sp " " #define INF 10000000000 #define LINF 9000000000000000000 #define all(s) s.begin(), s.end() void vout(vi v) { for (lli i = 0; i < v.size(); i++) cout << v.at(i) << endl; } int main() { cout << std::fixed << std::setprecision(10); lli n; cin >> n; map<lli, lli> rec; rep(k, n) { map<long long int, long long int> prime; long long int nc = k + 1; long long int i = 2; while (nc != 1) { if (i * i > nc) { if (prime.count(nc)) prime.at(nc) += 1; else { prime[nc] = 1; } break; } if (nc % i == 0) { long long int count = 0; while (nc % i == 0) { nc /= i; count++; } if (prime.count(i)) prime.at(i) += count; else { prime[i] = count; } } i++; } for(auto v: prime){ if(!rec.count(v.first)||(rec.count(v.first)&&rec[v.first]<v.second)){ rec[v.first] = v.second; } } } lli ans = 1; for(auto v: rec){ ans *= pow(v.first, v.second); } ans++; out(ans); }
#include <iostream> using namespace std; template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } int main() { long long N; cin >> N; long long div = 1; for (long long i = 2; i <= N; i++) { div = lcm(i, div); } cout << div+1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define cm (1 << 17) char cn[cm], *ci = cn + cm, ct; inline char getcha() { if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; } return *ci++; } inline int getint() { int A = 0; while ((ct = getcha()) >= '0') A = A * 10 + ct - '0'; return A; } static struct FastOutput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t buf_pos = 0; static constexpr int TMP_SIZE = 1 << 20; char tmp[TMP_SIZE]; FILE* out = stdout; inline void put_char(char c) { buf[buf_pos++] = c; if (buf_pos == BUF_SIZE) { fwrite(buf, 1, buf_pos, out); buf_pos = 0; } } ~FastOutput() { fwrite(buf, 1, buf_pos, out); } inline FastOutput& operator<<(char c) { put_char(c); return *this; } inline FastOutput& operator<<(const char* s) { while (*s) { put_char(*s++); } return *this; } inline FastOutput& operator<<(const string& s) { for (int i = 0; i < (int)s.size(); i++) { put_char(s[i]); } return *this; } template <typename T> inline char* integer_to_string(T n) { // beware of TMP_SIZE char* p = tmp + TMP_SIZE - 1; if (n == 0) { *--p = '0'; } else { bool is_negative = false; if (n < 0) { is_negative = true; n = -n; } while (n > 0) { *--p = (char)('0' + n % 10); n /= 10; } if (is_negative) { *--p = '-'; } } return p; } template <typename T> inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) { return integer_to_string(n); } #if !defined(_WIN32) || defined(_WIN64) inline char* stringify(__int128 n) { return integer_to_string(n); } #endif template <typename T> inline typename enable_if<is_floating_point<T>::value, char*>::type stringify(T n) { sprintf(tmp, "%.17f", n); return tmp; } template <typename T> inline FastOutput& operator<<(const T& n) { auto p = stringify(n); for (; *p != 0; p++) { put_char(*p); } return *this; } } fast_output; #define cout fast_output vector<int> p; int get(int x) { return p[x] == -1 ? x : p[x] = get(p[x]); } int main() { int n = getint(); constexpr int mx = 400010; p.resize(mx, -1); vector<int> cnt(mx); while (n--) { int a = getint(), b = getint(); a = get(a), b = get(b); if (a != b) { p[b] = a; cnt[a] += cnt[b]; } cnt[a]++; } vector<int> siz(mx); for (int i = 0; i < mx; i++) { siz[get(i)]++; } int ans = 0; for (int i = 0; i < mx; i++) { if (get(i) == i) { ans += min(cnt[i], siz[i]); } } cout << ans; }
#include <bits/stdc++.h> #define fo(i,a,b) for(int i=(a);i<=(b);++i) #define __unique(V) (V).resize(unique((V).begin(),(V).end())-(V).begin()) #define cntbit(X) __builtin_popcount((X)) #define bit(S,i) (((S)>>(i - 1))&1) #define PI acos(-1) #define fi first #define se second #define LL long long #define ii pair<int,int> #define iii pair<int,ii> #define eb emplace_back #define TASK "abc187" using namespace std; const int maxn = 20; int n,m,g[1 << maxn]; bool a[maxn][maxn],dp[1 << maxn]; ///-------------------------- void readf(){ cin >> n >> m; for(int i = 1; i <= m; ++i){ int u,v; cin >> u >> v; a[u][v] = 1; } } ///-------------------------- void solve(){ for(int mask = 0; mask < (1 << n); ++mask){ for(int i = 1; i <= n; ++i) if (bit(mask,i)) for(int j = i + 1; j <= n; ++j) if (bit(mask,j)){ if (a[i][j] == 0) goto nxt; } dp[mask] = 1; nxt : ; } memset(g,0x3f,sizeof(g)); g[0] = 0; for(int mask = 1; mask < (1 << n); ++mask) for(int i = mask; i > 0; i = (i - 1) & mask) if (dp[i] == 1){ g[mask] = min(g[mask],g[mask ^ i] + 1); } cout << g[(1 << n) - 1] << '\n'; } ///-------------------------- int main(){ srand(time(NULL)); ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); #ifdef TLH2203 freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); #endif // TLH2203 readf(); solve(); }
#include<bits/stdc++.h> #include<stdio.h> using namespace std; #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define MAX 1000050 #define ll long long #define ld long double #define lli long long int #define pb push_back #define INF 100000000000000 #define mod 1000000007 // trignometric function always give value in Radians only #define PI acos(-1) //3.1415926535897932384626433832795028 #define dsin(degree) sin(degree*(PI/180.0)) #define dcos(degree) cos(degree*(PI/180.0)) #define dtan(degree) tan(degree*(PI/180.0)) #define rsin(radian) sin(radian) #define rcos(radian) cos(radian) #define rtan(radian) tan(radian) #define counttrailingzeroes(n) __builtin_ctzll(n) #define countsetbits(n) __builtin_popcountll(n) #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define memf(a) memset(a,false,sizeof(a)) #define loop(i,n) for (lli i = 0; i < n; i++) #define FOR(i,a,b) for (lli i = a; i < b; i++) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define makeuniq(v) v.resize(unique(all(v)) - v.begin()); //only uniq element in vector after this #define sz(x) int(x.size()) #define F first #define S second #define mii map<lli,lli> #define pii pair<lli,lli> #define vi vector<lli> #define vvi vector<vi> #define vpi vector<pii> #define vbool vector<bool> #define seti set<lli> #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) (a/gcd(a,b))*b #define abs(x) ((x < 0)?-(x):x) #define isvowel(v) (0x208222>>(v&0x1f))&1 #define endl '\n' template <typename Head> void print(Head&& head) { cout<<head<<endl; } template <typename Head, typename... Tail> void print(Head&& head, Tail... tail) { cout<<head<<" "; print(tail...); } #define scanarr(a,n) for(lli i=0;i<n;i++) cin>>a[i]; #define scanvec(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);} #define printarr(a,n) for(lli i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; #define printvec(vec) for(auto xt : vec) cout<<xt<<" "; cout<<"\n"; #define FD(N) fixed<<setprecision(N) #define deb(x) cout<<#x<<" "<<x<<endl; /* 1D vector - vi dp(n,value); */ lli mceil(lli a,lli b){ return(a/b + (a%b != 0)); } lli mfloor(lli a,lli b){ return(a/b); } ll modmul(ll a, ll b,ll MOD = mod) { return ((a%MOD) * (b%MOD)) % MOD; } ll modadd(ll a, ll b,ll MOD = mod){ return((a%MOD)+(b%MOD)+MOD)%MOD; } ll modsub(ll a, ll b,ll MOD = mod){ return((a%MOD) - (b%MOD) + MOD)%MOD; } lli fastexpo(lli a,lli b ,lli MOD = mod){ a = a%MOD; lli ans=1; while(b){ if(b&1) ans=(ans*1ll*a)%MOD; a=(a*1ll*a)%MOD; b=b/2; } return ans; } vector<int>all_div[200050]; void alldivisors(){ for(int i=1;i<200050;++i){ for(int j=i;j<200050;j+=i) all_div[j].pb(i); } } bool ispower2(lli n){ return(n and (n&(n-1))==0 ); } bool isp(lli n) { if(n<=1) return 0; if(n==2 or n==3) return 1; for(lli i=2;i*i<=n;i++) if(n%i==0) return 0; return 1; } int main(){ fastio lli tt =1; //cin>>tt; //alldivisors(); while(tt--) { lli n,x; lli a[3]; scanarr(a,3); sort(a,a+3); print(a[1]+a[2]); } return 0; }
/* Nikkz99 */ #include <bits/stdc++.h> #define ll long long #define s string using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //string w,a; getline(cin,a); // stringstream S(a); //set<s> d; //map<s,ll> F; // while(S>>w) { } ll A[102]; for(auto i=0;i<3;i++) cin>>A[i]; sort(A,A+3); cout<<A[2]+A[1]; return 0; }
#include<bits/stdc++.h> #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--) #define pii pair<int,int> #define fi first #define se second #define PB push_back #define ll long long #define ull unsigned long long #define y1 orzkcz using namespace std; const int N=500005; int n,q1[N],q2[N]; char s[N],t[N]; ll res; int main(){ scanf("%d",&n); scanf("%s",s+1); scanf("%s",t+1); For(i,1,n) if (s[i]=='1') q1[++*q1]=i; For(i,1,n) if (t[i]=='1') q2[++*q2]=i; int p=1; For(i,1,*q2){ for (;p<=*q1&&q2[i]>q1[p];p+=2) res+=q1[p+1]-q1[p]; if (p>*q1) return puts("-1"),0; res+=q1[p]-q2[i]; ++p; } for (;p<=*q1;p+=2) res+=q1[p+1]-q1[p]; if (p!=(*q1)+1) puts("-1"); else printf("%lld\n",res); }
//Author: Fuadul Hasan([email protected]) //BSMRSTU,Gopalganj //#include<bits/stdc++.h> #define _USE_MATH_DEFINES #include <set> #include <map> #include <list> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <random> #include <cassert> #include <cstring> #include <sstream> #include <complex> #include <numeric> #include <iostream> #include <algorithm> #include <functional> #include <unordered_set> #include <unordered_map> using namespace std; //int input........ template <typename T> inline void readline(T &n) {n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';n = n * f;} template <typename T, typename TT> inline void readline(T &n, TT &m) { readline(n); readline(m); } template <typename T, typename TT, typename TTT> inline void readline(T &n, TT &m, TTT &l) { readline(n, m); readline(l);} // long long input........ template <typename T> inline void readlinel(T &n) {n = 0; long long f = 1; register long long ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';n = n * f;} template <typename T, typename TT> inline void readlinel(T &n, TT &m) { readlinel(n); readlinel(m); } template <typename T, typename TT, typename TTT> inline void readlinel(T &n, TT &m, TTT &l) { readlinel(n, m); readlinel(l);} //debug.......... #define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;} vector<string> split(const string &s, char c) {vector<string>v; stringstream ss(s); string x; while (getline(ss, x, c))v.emplace_back(x); return move(v);} void err(vector<string>::iterator it) {} template<typename T, typename... Args>void err(vector<string>::iterator it, T a, Args...args) { cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...);} //............ignore it..................// int dx[8]= {1,0,-1,0,-1,-1,1,1};int dy[8]= {0,1,0,-1,-1,1,-1,1}; int tc = 1; #define F first #define S second #define pb push_back #define mp make_pair #define Pi atan(1)*4 #define ll long long int #define happy cin.tie(0); #define point(x) setprecision(x) #define coding ios::sync_with_stdio(false); #define Test printf("Case %d: ",tc++); #define Unique(c) (c).resize(unique(all(c))-(c).begin()) #define vout(v) for (auto z: v) cout << z << " "; cout << endl; void print(int n){printf("%d\n", n);} void Print(int n){printf("%d ", n);} void print(int n,int m){printf("%d ",n);printf("%d ",m);} void print(int n,int m,int k){printf("%d ",n);printf("%d ",m);printf("%d ",k);} void print(ll n){printf("%I64d\n", n);} void Print(ll n){printf("%I64d ", n);} void print(ll n,ll m){printf("%I64d ",n);printf("%I64d ",m);} void print(ll n,ll m,ll k){printf("%I64d ",n);printf("%I64d ",m);printf("%I64d ",k);} int length(long long x){int l = 0;for(long long i=x;i;i/=10)l++;return l;} int length(string s){return s.size();} const int M = 1e9 + 7; long long pow(long long a,long long n){ll res = 1;while(n){if(n&1) res = ((res%M)*(a%M))%M;a = ((a%M)*(a%M))%M;n>>=1;}return res%M;} #define pr pair<int, int> #define vpr vector<pr> #define vi std::vector<int> #define vll std::vector<ll> #define all(n) n.begin(),n.end() const int Inf = (int)2e9 + 5; const ll Lnf = (ll)2e18 + 5; const int N = 4e5 + 5; const int NN = 1e6 + 5; int solve() { //happy coding int a,b; cin>>a>>b; int x = (a+b)/2; cout<<x<<" "<<a - x<<endl; return 0; //error(); } int main(){ int test = 1; //readline(test);//(void)getchar(); while (test--)solve();return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; ll I=167167167167167167; ll Q=1e9+7; int main(){ ll N,X; cin>>N>>X; vector<ll> p(N); vector<ll> high(N); vector<ll> low(N); for(int i=0;i<N;i++){ cin>>p[i]; high[i]=((X-1)/p[i]+1)*p[i]; low[i]=(X/p[i])*p[i]; } vector<ll> dp1(N); auto dp2=dp1; dp1[0]=1; for(int i=0;i<N-1;i++){ dp1[i+1]=dp1[i]; dp2[i+1]=dp2[i]; if((high[i+1]-low[i])%p[i+1]!=0) dp2[i+1]+=dp1[i]; if((high[i]-low[i+1])%p[i+1]!=0) dp1[i+1]+=dp2[i]; } cout<<dp1[N-1]+dp2[N-1]<<endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++) #define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) #define setp(n) fixed << setprecision(n) 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; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define ins insert #define debug(a) cerr<<(a)<<endl #define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl #define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;} using namespace std; template<class A, class B> ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";} template<class A, class B> istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;} /* Some Libraries */ //------------------------------------------------- const int N_MAX = 50; ll dp[N_MAX][2]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; ll X; cin>>N>>X; vll a(N); rep(i,N) cin>>a[i]; if (N==1){ cout<<"1\n"; return 0; } vll cnt(N); ll now = X; RFOR(i,N-1,0){ cnt[i]+=now/a[i]; now%=a[i]; } vll r(N-1); rep(i,N-1) r[i] = a[i+1]/a[i]; dp[0][0] = 1; rep(i,N-1)rep(j,2){ if (dp[i][j]==0) continue; ll t = cnt[i]+j; if (t%r[i]==0){ dp[i+1][(t==r[i])] += dp[i][j]; }else{ dp[i+1][0] += dp[i][j]; dp[i+1][1] += dp[i][j]; } } cout<<dp[N-1][0]+dp[N-1][1]<<"\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W, X, Y; cin >> H >> W >> X >> Y; vector<string> data(H); for (int i = 0; i < H; i++){ cin >> data.at(i); } int answer = 1; for (int i = X-2; i >= 0; i--){ if (data.at(i).at(Y-1) == '#') break; else answer += 1; } for (int i = X; i <= H-1; i++){ if (data.at(i).at(Y-1) == '#') break; else answer += 1; } for (int i = Y-2; i >= 0; i--){ if (data.at(X-1).at(i) == '#') break; else answer += 1; } for (int i = Y; i <= W-1; i++){ if (data.at(X-1).at(i) == '#') break; else answer += 1; } cout << answer << endl; }
#pragma region #include <bits/stdc++.h> #define _GLIBCXX_DEBUG using namespace std; using ll = long long; using ld = long double; const int INF = INT_MAX; const ll LLINF = LLONG_MAX; #define all(x) x.begin(), x.end() #define rep(i, s, e) for(ll i = s; i < e; ++i) #define repr(i, s, e) for(ll i = s; i > e; --i) 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; } void CIN() {} template<class T, class ...Ts> void CIN(T &&t, Ts &&...ts){ cin >> t; CIN(ts...); } void COUT() { cout << endl; } template<class T, class ...Ts> void COUT(T &&t, Ts &&...ts){ cout << t << " "; COUT(ts...); } #pragma endregion int dfs(const vector<string> s, const int h, const int w, const int sy, const int sx) { stack<pair<int, int>> d; //directions(y, x) d.push(make_pair(1, 0)); //down d.push(make_pair(-1, 0)); //up d.push(make_pair(0, 1)); //right d.push(make_pair(0, -1)); //left int res = 0; while (!d.empty()){ int y = sy - 1, x = sx - 1; while(1){ int dir_y = d.top().first, dir_x = d.top().second; d.pop(); if ((y + dir_y < 0) || (h <= y + dir_y) || (x + dir_x < 0) || (w <= x + dir_x)) break; else { if (s[y + dir_y][x + dir_x] == '.') { d.push(make_pair(dir_y, dir_x)); x += dir_x; y += dir_y; res ++; } else break; } } } return res; } int main() { cin.tie(nullptr); int h, w, sy, sx; CIN(h, w, sy, sx); vector<string> s(h); for (auto &in :s) cin >> in; cout << dfs(s, h, w, sy, sx) + 1 << endl; return 0; }
#include<cmath> #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<bitset> #include<cstring> #include<ctime> #include<deque> #include<iomanip> #include<list> #include<map> #include<deque> #include<queue> #include<set> #include<stack> #include<vector> #include<string> #include<sstream> #include<chrono> #include<random> #include<unordered_map> #include<ctype.h> #include<cassert> #include<numeric> #include<functional> using namespace std; typedef long long ll; typedef std::pair<ll, ll> pll; typedef std::pair<int, int> pii; typedef unsigned long long ull; typedef long double ldb; typedef double db; #define pb push_back #define eb emplace_back #define all(a) (a).begin(),(a).end() template <class output_type, class input_type> output_type Convert(const input_type& input) { stringstream ss; output_type result; ss.clear(); ss << input; ss >> result; return result; } const double Pi = acos(-1.0); const int inf = 0x3f3f3f3f, Mod = 1e9+7, N = 5e5 + 3; const ll INF = 1ll << 60; inline ll qpow(ll a, ll b, ll mod = Mod, ll ans = 1) { while (b) { if (b & 1)ans = ans * a % mod; a = a * a % mod; b >>= 1; }return ans; } ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }; ll lcm(ll a, ll b) { return a * b / (gcd(a, b)); } pii EXgcd(int x, int y) { //return.first ny if (!y) { return { 1,0 }; }pii a = EXgcd(y, (x + y) % y); return { a.second, a.first - a.second * (x / y) }; } inline bool Ezisprime(int n){ if (n <= 1)return 0;if (n <= 3)return 1; vector<int> ezp{ 5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 }; for (int i = 0; i < ezp.size(); i++)if (n == ezp[i])return 1; for (int i = 2; i <= sqrt(n); i++)if (n % i == 0)return 0; return 1; } inline int LIS(const vector<int> Origins, const int n){ if (!n || !Origins.size()) return 0; int len = 0; vector<int> d(n, 0); d[0] = Origins[0]; for (int i = 1; i < n; i++) if (Origins[i] >= d[len])d[++len] = Origins[i]; else d[upper_bound(d.begin(), d.begin() + len, Origins[i]) - d.begin()] = Origins[i]; return len + 1; } struct edge {ll to, cost, time;}; typedef vector<vector<edge>> Graph; inline vector<ll> Dijkstra(const Graph& G, ll s)// G为路表,s为起点,结果为各点距离容器 { priority_queue<pll, vector<pll>, greater<pll> >q; //first 为距离,second 为点号 vector<ll> d(G.size(), INF); d[s] = 0;q.push({ 0,s }); while (!q.empty()){ pll tp = q.top(); q.pop(); if (d[tp.second] < tp.first)continue; for (auto& e : G[tp.second]){ if (e.cost + d[tp.second] < d[e.to]){ d[e.to] = e.cost + d[tp.second]; q.push({ d[e.to], e.to }); } } } return d; } inline int Alptonum(const char a) { if (a <= 'z')return int(a - 'a'); else return int(a - 'A'); } inline char Numtoalp(const int n, bool t) { if (!t)return char('a' + n); else return char('A' + n); } inline bool cmp(pii a, pii b) { //default more and more larger return a.first < b.first; } struct cmpstl { //reverse inline bool operator()(pii a, pii b) { if (a.second == b.second)return a.first > b.first; return a.second < b.second; } }; //partial_sum(begin,end,place,minus<int>()) adjacent_differece(begin,end,place) //count(begin(),end(),char=='x') count_if(begin(),end(),boolcmp) vector<int> p(n);iota(p.begin(), p.end(), 0); //|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Code follows ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\\ //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV Code follows VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVII db dp[1001][1001]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; // int ts; cin >> ts; double res = 0; for (int i = 1; i < n; i++)res += double(n) / i; cout << setprecision(11) << fixed << res << '\n'; } /* */
#include<bits/stdc++.h> using namespace std; #define I inline int #define V inline void #define ll long long int #define isnum(ch) ('0'<=ch&&ch<='9') #define FOR(i,a,b) for(int i=a;i<=b;i++) #define ROF(i,a,b) for(int i=a;i>=b;i--) #define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n) #define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++) char _buf[100000],*_op(_buf),*_ed(_buf); I getint(){ int _s=0,_f=1;char _ch=gc; while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc; while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc; return _s*_f; } const int N=5e5+2,mod=998244353; V check(int&x){x-=mod,x+=x>>31&mod;} V cmax(int&x,int y){if(x-y>>31)x=y;} V cmin(int&x,int y){if(y-x>>31)x=y;} I Pow(ll t,int x,ll s=1){ for(;x;x>>=1,t=t*t%mod)if(x&1)s=s*t%mod; return s; } char a[N]; int n,cnt[26],m,k,ans; V input(){ n=getint(),m=getint(),k=getint(); if(n==1||m==1)ans=Pow(k,n*m); else FOR(i,1,k) check(ans+=1ll*Pow(i,n)*(Pow(k-i+1,m)-Pow(k-i,m)+mod)%mod); cout<<ans; } V init(){ } V work(){ } int main(){ input(); init(); work(); // } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 998244353; const ll INF = 1e18; #define rep(i,m,n) for(ll i = (m); i <= (n); i++) #define zep(i,m,n) for(ll i = (m); i < (n); i++) #define rrep(i,m,n) for(ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl; ll n, w[101], ex[101], sm, dp[2][100 * 101][101]; int main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; zep(i, 0, n)cin >> w[i]; ex[0] = 1; rep(i, 1, n)ex[i] = i * ex[i - 1] % MOD; zep(i, 0, n)sm += w[i]; if(sm % 2 == 1){print(0) return 0;} dp[0][0][0] = 1; zep(i, 0, n){ memset(dp[(i + 1) % 2], 0, sizeof(dp[(i + 1) % 2])); rep(j, 0, sm / 2){ rep(k, 0, i){ dp[(i + 1) % 2][j][k] += dp[i % 2][j][k]; dp[(i + 1) % 2][j][k] %= MOD; dp[(i + 1) % 2][j + w[i]][k + 1] += dp[i % 2][j][k]; dp[(i + 1) % 2][j + w[i]][k + 1] %= MOD; } } } ll ans = 0; zep(k, 1, n){ ans += dp[n % 2][sm / 2][k] * (ex[k] * ex[n - k] % MOD) % MOD; ans %= MOD; } print(ans) return 0; }
#include <bits/stdc++.h> #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if(!(p)) #define until(p) while(!(p)) #ifdef LOCAL #define log(val) std::cerr << std::setw(3) << __LINE__ << "| " << #val << " = " << (val) << std::endl #define dbg(proc) proc #else #define log(val) 0 #define dbg(proc) 0 #endif template <typename T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){os << "{";if(!v.empty()){os << v[0];for(std::size_t i=1;i<v.size();++i){os << ", " << v[i];}}return os << "}";} using ll = std::int64_t; using P = std::tuple<int,int>; constexpr int INF = 1001001001; int N; ll X; int A[110]; int dp[110][110][110]; int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N >> X; for(int i=0;i<N;++i){ std::cin >> A[i]; } ll res = std::numeric_limits<ll>::max(); for(int k=1;k<=N;++k){ std::fill(&dp[0][0][0], &dp[0][0][0] + 110 * 110 * 110, -INF); dp[0][0][0] = 0; for(int i=0;i<N;++i){ for(int j=0;j<=k;++j){ for(int r=0;r<k;++r){ dp[i+1][j][r] = std::max(dp[i+1][j][r], dp[i][j][r]); if(j + 1 <= k){ dp[i+1][j+1][(r+A[i])%k] = std::max(dp[i+1][j+1][(r+A[i])%k], dp[i][j][r] + A[i]); } } } } if(dp[N][k][X%k] < 0){ continue; } res = std::min(res, (X - dp[N][k][X%k]) / k); } std::cout << res << std::endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; using P = pair<ll, ll>; using V = vector<ll>; using VV = vector<V>; using SV = vector<string>; #define rep(i, n) for(ll i=0;i<(ll)(n);i++) #define rep1(i, n) for(ll i=1;i<=(ll)(n);i++) #define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++) #define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define MOD 1000000007 #define INF 1000000000 #define LLINF 1e18 const double PI = acos(-1); const int dx[] = { -1,0,1,0 }; const int dy[] = { 0,-1,0,1 }; 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; } void solve_abc_a() { ll l_ans = 0; string s_ans; double d_ans = 0; ll n, m; ll x, y; ll h, w; ll a, b; ll c; //char c; //ll d; char yc, ac; ll k; string s, t; //ll s, t; ll q, r; cin >> a >> b >> c; if (a * a + b * b < c * c) { cout << "Yes\n"; } else { cout << "No\n"; } //cout << l_ans << "\n"; //cout << s_ans << "\n"; //cout << d_ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed; cout << setprecision(12); solve_abc_a(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; if((a*a)+(b*b)<(c*c)){ cout<<"Yes"<<endl; } else cout<<"No"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; vector<long> a(n); vector<long> b(n); long ans = 0; for(int i = 0; i < n; i++){ cin >> a.at(i); cin >> b.at(i); long total = (a.at(i) + b.at(i)) * (b.at(i) - a.at(i) + 1) / 2; ans += total; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < (n); i++) #define REPS(i, n) for(int i = 1; i <= (n); i++) #define RREP(i, n) for(int i = (n)-1; i >= 0; i--) #define RREPS(i, n) for(int i = (n); i > 0; i--) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define mp make_pair #define mt make_tuple #define pb push_back #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl using ll = long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vb = vector<bool>; using vvi = vector<vi>; using vvl = vector<vl>; const int MOD = 1e9 + 7; const int INF = 1e9 + 7; const ll INFL = 1e18; 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(a > b) { a = b; return true; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); ll N; cin >> N; ll ans = 0; REP(i, N) { ll A, B; cin >> A >> B; ans += (A-1)*(B-A+1) + (B-A+1)*(B-A+2)/2; } cout << ans << endl; }
#include<bits/stdc++.h> typedef long long int ll ; #define pb push_back #define rep(i,k,n) for (i = k; i < n; i++) #define coy cout<<"YES\n" #define con cout<<"NO\n" using namespace std; //const unsigned long long int M = 1000*100 ; //ll sieve[M]; //vector<ll>ans; //const unsigned int n=10000005; ll ceil_div(ll a, ll b) { return (a + b - 1) / b; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll dig_sum(ll n) { ll x=n; ll cnt=0; while(x>0) { ll a=x%10; cnt=cnt+a; x=x/10; } return cnt; } ll dig_cnt(ll n) { ll x=n; ll cnt=0; while(x>0) { ll a=x%10; cnt++; x=x/10; } return cnt; } bool dig_check(ll n,ll num,ll len) { ll x=n; ll cnt=0; while(x>0) { ll a=x%10; if(a==num) { cnt++; } x=x/10; } if(cnt==len) { return true; } else { return false; } } // void si() // { // ll i,j; // rep(i,0,M) // { // sieve[i]=1; // } // sieve[0]=sieve[1]=0; // for(i=2;i<M;i++) // { // for(j=i*i;j<M;j=j+i) // { // sieve[j]=0; // } // } // for(i=2;i<M;i++) // { // if(sieve[i]==1) // { // ans.push_back(i); // } // } ////cout<<ans[ans.size()-1]*ans[ans.size()-2]; //// rep(i,0,100) //// { //// cout<<ans[i]<<" "; //// } // // } ll low(vector<ll>arr,ll l,ll r,ll num) { ll m=0; while(l<r) { m=l+(r-l)/2; if(arr[m]<num) { l=m+1; } else { r=m; } } //cout<<arr[l]<<" "; if(arr[l]>=num) { return l; } else { return arr.size(); } // if(arr[l]<=num) // { // return true; // } // else // { // return false; // } } void fun() { ll n,m,j,d,l,r,i,x,y,k,z,a,b,c,p,q; cin>>n; vector<ll>vec(n); map<ll,ll>mp; rep(i,0,n) { cin>>vec[i]; mp[vec[i]]++; } ll ans=0; for(i=n-1;i>=0;i--) { ans=ans+(i-(mp[vec[i]]-1)); mp[vec[i]]--; } cout<<ans; } int main() { //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); long long int t; //si(); t=1; //cin>>t; while(t--) { fun(); } }
#include<bits/stdc++.h> using namespace std; int main() { int a; cin >> a; long long sum = 0; vector<int>vec(a); for(int i = 0;i < a;i++)cin >> vec[i]; map<int,long long> mp; for(int i = 0;i < a;i++){ mp[vec[i]]++; } for(int i = 0;i < a;i++){ sum += a - mp[vec[i]]; } sum /=2; cout << sum; }
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 3e3 + 5; const int mod = 998244353; int f[MAXN][MAXN];//use x numbers to get y int gao(int x,int y) { if(y>x) return 0; if(~f[x][y]) return f[x][y]; if(!x) return f[x][y] = !y; if(!y) return f[x][y] = 0; return f[x][y] = (gao(x, y*2) + gao(x-1, y-1)) %mod; } int main(void) { int n,d; scanf("%d%d",&n,&d); memset(f,-1,sizeof(f)); printf("%d",gao(n,d)); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll,ll>; using Graph= vector<vector<int>>; #define rep(i,n) for (ll i=0; i < (n); ++i) #define rep2(i,n,m) for(ll i=n;i<=m;i++) #define rep3(i,n,m) for(ll i=n;i>=m;i--) #define pb push_back #define eb emplace_back #define ppb pop_back #define fi first #define se second #define mpa make_pair const ll INF=1e18 ; inline void chmax(ll& a,ll b){a=max(a,b);} inline void chmin(ll& a,ll b){a=min(a,b);} ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} #define set20 cout<<fixed<<setprecision(20) ; // ミント //int mod ; cin>>mod ; const ll mod = 1e9+7 ;//924844033; // 998244353; struct mint { ll x; // typedef long long ll; mint(ll 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(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod 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, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} //昆布 struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); //任意modではここ消す fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } mint p(int n,int k){ return fact[n]*ifact[n-k] ; } } ; mint modpow(ll a,ll b){ if(b==0) return 1 ; mint c= modpow(a,b/2) ; if(b%2==1) return c*c*a ; else return c*c ; } mint komb(ll n,ll m){ mint x=1 ;mint y=1 ; rep(i,m){ x*= n-i ; y*= i+1 ; } return x/y ; } map<ll,ll> factor(ll n){ //素因数とオーダーをマップで管理 map <ll,ll> ord ; for(ll i=2;i*i<=n;i++){ if(n%i==0){ int res=0; while(n%i==0){ n/=i; res++; } ord[i]=res; } } if(n!=1) ord[n]++; return ord ; } mint dp[100005][2] ; mint sum[100005][2] ; int main(){ ll n ; cin>>n ; vector<ll> A(n) ; rep(i,n) cin>>A[i] ; dp[1][0]=1ll ; //dp[1][1] =1ll ; sum[1][0]=A[0] ; //sum[1][1]=A[0] ; rep(i,n-1){ dp[2+i][0]=dp[1+i][0]+dp[1+i][1] ; dp[2+i][1]=dp[1+i][0] ; sum[2+i][0]=sum[1+i][0]+sum[1+i][1]+mint(A[i+1])*(dp[2+i][0]) ; sum[2+i][1]=sum[1+i][0]-mint(A[i+1])*(dp[2+i][1]) ; } mint ans=sum[n][0]+sum[n][1] ; cout<<ans<<endl ; return 0 ; }
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <numeric> #include <functional> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for(ll i = 0; i < n; i++) #define exrep(i, a, b) for(ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; struct Edge { ll to, cost; Edge(ll to, ll cost): to(to), cost(cost) {} }; int main() { ll n, m; cin >> n >> m; vl w(n); ll maW = 0; rep(i, n) { cin >> w[i]; chmax(maW, w[i]); } vl L(m), v(m); rep(i, m) { cin >> L[i] >> v[i]; if(maW > v[i]) { out(-1); re0; } } vl dist(1<<n); // dist[S] : ラクダi,j(i < j)間のラクダの集合がSのときのx[j] - x[i]の最小値 rep(S, 1<<n) { ll W = 0; rep(i, n) { if(S & 1<<i) { W += w[i]; } } rep(i, m) { if(W > v[i]) { chmax(dist[S], L[i]); } } } vl perm(n); rep(i, n) { perm[i] = i; } ll ans = -INF; do { vector<vector<Edge>> G(n); vl indeg(n); // indeg[v] : 頂点vの入次数 exrep(i, 0, n-2) { ll a = perm[i]; ll S = 1<<a; exrep(j, i+1, n-1) { ll b = perm[j]; S |= 1<<b; G[b].emplace_back(a, -dist[S]); indeg[a]++; } } ll sv = perm[n-1]; ll gv = perm[0]; queue<ll> Q; Q.push(sv); vl dp(n, INF); // dp[v] : 頂点svからvへの最短距離 dp[sv] = 0; while(!Q.empty()) { ll v = Q.front(); Q.pop(); for(Edge e : G[v]) { ll u = e.to; indeg[u]--; chmin(dp[u], dp[v] + e.cost); if(indeg[u] == 0) { Q.push(u); } } } chmax(ans, dp[gv]); } while(next_permutation(all(perm))); ans *= -1; out(ans); re0; }
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define ROF(i, a, b) for (int i = a; i >= b; --i) #define NEXT(i, x) for (int i = h[x]; i; i = e[i].u) const int INF = 2e9; inline int Max(const int &a, const int &b) { return a > b ? a : b; } inline ll Min(const ll &a, const ll &b) { return a > b ? b : a; } inline void read(int &x) { x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } x *= f; } const int N = 2e5 + 7; int n; ll s[3][3][3][3]; int sum[3]; ll ans; bool ff[3]; int pos[3][3]; struct jgt { ll x; int id, f; bool operator<(const jgt &a) const { return x < a.x; } } a[3][N], tmp[N]; int main() { read(n); n <<= 1; FOR(i, 1, n) { ll x; char ch; scanf(" %lld %c", &x, &ch); a[0][i].f = a[1][i].f = a[2][i].f = -1; if (ch == 'R') { ++sum[0]; a[0][i] = (jgt){x, i, 0}; } else if (ch == 'G') { ++sum[1]; a[1][i] = (jgt){x, i, 1}; } else if (ch == 'B') { ++sum[2]; a[2][i] = (jgt){x, i, 2}; } } FOR(i, 0, 2) { if (sum[i] & 1) ff[i] = 1; } if (ff[0] == ff[1] && ff[1] == ff[2] && ff[0] == 0) { puts("0"); return 0; } FOR(i, 0, 1) { FOR(j, i + 1, 2) { FOR(k, 1, n) { if (a[i][k].f == i) tmp[k] = a[i][k]; else if (a[j][k].f == j) tmp[k] = a[j][k]; else { tmp[k].x = (ll)1e18; tmp[k].f = -1; } } std::sort(tmp + 1, tmp + n + 1); ll mn = (ll)1e18; int P = 0; FOR(k, 1, n - 1) { if (tmp[k].f != tmp[k + 1].f) { if (tmp[k + 1].x - tmp[k].x < mn) { mn = tmp[k + 1].x - tmp[k].x; P = (tmp[k].f == i ? tmp[k].id : tmp[k + 1].id); } } } s[i][j][0][0] = s[j][i][0][0] = mn; pos[i][j] = pos[j][i] = P; } } FOR(i, 0, 2) { FOR(j, 0, 2) { if (i == j) continue; FOR(w1, 0, 2) { FOR(w2, 0, 2) { if (w1 == w2) continue; if (w1 == i && w2 == j) continue; if (w1 == j && w2 == i) continue; FOR(k, 1, n) { if (a[i][k].f == i) tmp[k] = a[i][k]; else if (a[j][k].f == j) tmp[k] = a[j][k]; else { tmp[k].x = (ll)1e18; tmp[k].f = -1; } } tmp[pos[w1][w2]].x = (ll)1e18; tmp[pos[w1][w2]].f = -1; std::sort(tmp + 1, tmp + n + 1); ll mn = (ll)1e18; FOR(k, 1, n - 1) { if (tmp[k].f != tmp[k + 1].f) { mn = Min(mn, tmp[k + 1].x - tmp[k].x); } } s[i][j][w1][w2] = mn; } } } } int k = 0, kk = 0, kkk = 0; if (ff[0] == 0) k = 1, kk = 2, kkk = 0; else if (ff[1] == 0) k = 0, kk = 2, kkk = 1; else if (ff[2] == 0) k = 0, kk = 1, kkk = 2; ans = s[k][kk][0][0]; ans = Min(ans, Min(s[k][kkk][0][0] + s[kkk][kk][k][kkk], s[kkk][k][kk][kkk] + s[kk][kkk][0][0])); printf("%lld\n", ans); return 0; }
/* Hardwork allways pays off */ //You never know how close to the solution you are ,so keep practicing #include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define int long long #define ld long double #define pb push_back #define lb lower_bound #define ub upper_bound #define pii pair<int,int> #define endl '\n' #define REP(i,n) for(int i=1;i<=n;i++) #define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower) #define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper) #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define printclock cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n"; #define all(x) (x).begin(), (x).end() #define mod1 998244353 #define mod 1000000007 #define pie 3.141592653589793238 #define printv(x) for(i=0;i<x.size();i++) cout<<x[i]<<" "; #define printA(x,n) for(i=0;i<n;i++) cout<<x[i]<<" "; #define F first #define S second #define vi vector<int> void debb(int x) {cerr << x;} void debb(double x) {cerr << x;} void debb(long double x) {cerr << x;} void debb(char x) {cerr << '\'' << x << '\'';} void debb(const char *x) {cerr << '\"' << x << '\"';} void debb(const string &x) {cerr << '\"' << x << '\"';} void debb(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void debb(const pair<T, V> &x) {cerr << '{'; debb(x.ff); cerr << ','; debb(x.ss); cerr << '}';} template<typename T> void debb(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), debb(i); cerr << "}";} void _debb() {cerr << "]\n";} template <typename T, typename... V> void _debb(T t, V... v) {debb(t); if (sizeof...(v)) cerr << ", "; _debb(v...);} // typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; void make_unique(vector<int> vec) { sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin()); } bool isPowerOfTwo(int n) { if(n==0) return false; return (ceil(log2(n)) == floor(log2(n))); } int cceil(int x, int y){ return (x+y-1)/y; } struct Cmp { bool operater(const pair<int,int> &a,const pair<int,int> &b) { return (a.first)<(b.first); } }; bool cmp(const pair<int,int> &a,const pair<int,int> &b) { return (a.second)<(b.second); } int bpower(int a,int n) { int res=1; while(n) { if(n&1) res*=a,n--; else a*=a,n/=2; } return res; } void solve(){ int i,j,k; int n; cin>>n; if(n==1){ cout<<1<<endl; return; } int sum=1e18; int b=log2(n)+1; while(b>0){ int num=bpower(2,b); int a=n/num; int c=n%num; sum=min(sum,a+b+c); b--; } cout<<sum<<endl; } signed main() { ios; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t=1; // cin>>t; // init(); while(t--){ int k,n,i,j,m; solve(); } printclock; return 0; }
// Powered by CP Editor (https://cpeditor.org) //a+b = (a^b) + 2*(a&b) //b^c = (a^b)^(a^c) //gcd(x,y) = gcd(x-y,y) //if n = 5000 then complexity cannot be (n^2*log(n)) means no map ,no sets //check for long long overflow if input is large #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define int long long #define mod 1000000007 // #define mod 998244353 #define pb push_back #define ll long long #define fi first #define se second #define vi vector<int> #define pii pair<int,int> #define mii map<int,int> #define loop(i,n) for(int i=0;i<n;i++) #define pp ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define fil(a,b) memset(a, b, sizeof(a)) #define all(x) (x).begin(), (x).end() #define en cout<<"\n" #define trace(x) cout<<#x<<": "<<x<<" "<<endl #define trace2(x,y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl #define trace3(x,y,z) cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl #define trace4(a,b,c,d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl 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 INF = 2000000000000000000; long long power(long long a,long long b){ long long ans=1; while(b>0){ if(b&1){ans=(ans*a)%mod;} a=(a*a)%mod; b>>=1; } return ans; } void solve() { int n; cin>>n; int ar[n]; loop(i,n) cin>>ar[i]; int dp[n][2]; if(n<=2) { cout<<(n*ar[0])%mod;return; } for(int i=0;i<n;i++) for(int j=0;j<2;j++) dp[i][j]=0; dp[0][0]=1; dp[1][0]=1; dp[1][1]=1; for(int i=2;i<n;i++) { dp[i][0] = (dp[i-1][0] + dp[i-1][1])%mod; dp[i][1] = dp[i-1][0]; } int total = dp[n-1][0] + dp[n-1][1]; total%=mod; int ans=0; // trace(total); for(int i=0;i<n;i++) { int pos = (dp[i][0]*(dp[n-i-1][0] + dp[n-i-1][1])%mod)%mod; pos%=mod; pos = (pos - (total - pos) + mod)%mod; // trace2(i,pos); ans += (pos*ar[i])%mod; ans%=mod; } cout<<ans; } int32_t main() { pp; int test=1; // cin>>test; while(test--) { solve(); en; } return 0 ; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define scl(n) scanf("%lld", &n) #define pcl(n) printf("%lld\n", n) #define pcl1(n) printf("%lld ", n) #define nln printf("\n") #define dev(x) cout << #x << " " << x << " "; #define PTT pair<ll, ll> map<ll, ll> mp, mp1; map<ll, ll>::iterator itr; int main() { ll t, n, k, x, m; vector<PTT> v; scl(n); scl(m); scl(t); PTT p; for (int i = 0; i < m; i++) { scl(p.first); scl(p.second); v.push_back(p); } ll charge = n, tmp = 0; bool bl = true; for (int i = 0; i < m; i++) { charge -= (v[i].first - tmp); if (charge <= 0) { bl = false; } //cout << charge << " "; charge += (v[i].second - v[i].first); if (charge >= n) { charge = n; } //cout << charge << endl; tmp = v[i].second; } charge -= (t - tmp); if (charge <= 0) { bl = false; } if (bl) { puts("Yes"); } else { puts("No"); } return 0; }
#include<iostream> #include<vector> #include<map> #include<stack> #include<set> #include<queue> #include<stdlib.h> #include<math.h> #include<string> #include<string.h> #include<algorithm> #include<iomanip> #include<sstream> #define endl "\n" #define Doura() ios_base::sync_with_stdio(false),cin.tie(NULL), cout.tie(NULL) #define ll long long using namespace std; const long long MXX=1e7; int a[MXX],b[MXX]; void kajelag() { int n; cin>>n; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) cin>>b[i]; sort(a,a+n); sort(b,b+n); //cout<<b[0]<<" "<<a[n-1]<<endl; int ans=b[0]-a[n-1]; if(ans<0) cout<<0<<endl; else cout<<ans+1<<endl; } int main() { Doura(); #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ONLINE_JUDGE int t; //cin>>t; //while(t--) { kajelag(); } return 0; }
#include <iostream> using namespace std; int main(){ int N; cin >> N; cout << N-1; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define REP(i,a,b) for(int i = (a); i < (b); i++) #define MOD 1000000007 int main(void) { int n; cin >> n; int ans; cout << n+1-2 << endl; }
//#include<bits/stdc++.h> #include<map> #include<queue> #include<time.h> #include<limits.h> #include<cmath> #include<ostream> #include<iterator> #include<set> #include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep_1(i,m,n) for(int i=m;i<=n;i++) #define mem(st) memset(st,0,sizeof st) //namespace first_space //{ // int a=10; // void func() // { // cout << "Inside first_space" << endl; // } //} int read() { int res=0,ch,flag=0; if((ch=getchar())=='-') //判断正负 flag=1; else if(ch>='0'&&ch<='9') //得到完整的数 res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return flag?-res:res; } typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef pair<double,double> pdd; const int inf = 0x3f3f3f3f; const int N=5e5+10; int dp[N]; void solve() { int n; cin>>n; dp[1]=1; for(int i=1;i<=n;i++) for(int j=i+i;j<=n;j=j+i) dp[j]=max(dp[j],dp[i]+1); for(int i=1;i<=n;i++) cout<<dp[i]<<" "; cout<<endl; } signed main() { int t=1; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,m,n) for(int i=(m); i<(int)(n); i++) #define RREP(i,m,n) for(int i=(int)((n)-1); i>=m; i--) #define rep(i,n) REP(i,0,n) #define rrep(i,n) RREP(i,0,n) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define fi first #define se second #define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);} template<typename T> string join(const vector<T>&v, string del=", "){ stringstream s; for(auto x : v) s << del << x; return s.str().substr(del.size()); } template<typename T> ostream& operator<<(ostream& o, const vector<T>&v){ if(v.size()) o << "[" << join(v) << "]"; return o; } template<typename T> ostream& operator<<(ostream& o, const vector<vector<T> >&vv){ int l = vv.size(); if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n " ) << vv[i] << (i==l-1 ? " ]" : ""); } return o; } template<typename T1, typename T2> ostream& operator<<(ostream& o, const pair<T1, T2>& p){ return o << "(" << p.first << ", " << p.second << ")"; } inline void _debug(){cerr<<endl;} template<class First, class... Rest> void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);} template <typename ... Args> string format(const std::string& fmt, Args ... args ){ size_t len = std::snprintf(nullptr, 0, fmt.c_str(), args ... ); vector<char> buf(len + 1); snprintf(&buf[0], len + 1, fmt.c_str(), args ... ); return string(&buf[0], &buf[0] + len); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1*acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } int main(){ ios_base::sync_with_stdio(0); // finput("./input"); int n; cin >> n; vi cnt(n,1); REP(i,1,n+1){ for(int j=i*2; j<=n; j+=i){ cnt[j-1] = max(cnt[j-1], cnt[i-1]+1); } } cout << join(cnt, " ") << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define lli long long int #define ulli unsigned long long int #define vi vector<int> #define ii pair<int,int> #define all(v) v.begin(),v.end() #define getunique(v) {v.erase(unique(v.begin(),v.end()),v.end());} #define pb push_back #define ff first #define ss second #define endl "\n" #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) const int MS=1000001; const lli INF=1e9+7; template<typename T> bool comp(T a, T b){ if(a<b) return true; return false; } int vis[3000]; set<ii > s; int cnt=0; void dfs(int cnode,vi ar[],int par){ s.insert({par,cnode}); vis[cnode]=1; cnt++; for(auto child:ar[cnode]){ if(vis[child]==0) dfs(child,ar,par); } } int main(){ //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); fast_io; int n,m; cin>>n>>m; vi ar[n+1]; for(int i=0; i<m; i++){ int a,b; cin>>a>>b; ar[a].pb(b); } int ans=0; for(int i=1; i<=n; i++){ memset(vis,0,sizeof vis); dfs(i,ar,i); } // for(auto itr:s) // cout<<itr.ff<<" "<<itr.ss<<endl; cout<<s.size(); }
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define pb push_back #define eb emplace_back #define fi first #define se second using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); } template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); } template <class T> inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); } inline int popcount(const int &n) { return __builtin_popcount(n); } inline ll popcountll(const ll &n) { return __builtin_popcountll(n); } inline ll mask(const ll &k) { return (1LL << k) - 1LL; } template <class T> void zip(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class T> int lwb(V<T> &v, const T &x) { return lower_bound(all(v), x) - v.begin(); } template <class T> int upb(V<T> &v, const T &x) { return upper_bound(all(v), x) - v.begin(); } void dump() { cerr << '\n'; } template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) { cerr << head << (sizeof...(Tail) == 0 ? " " : ", "); dump(std::move(tail)...); } template <class T> void print(const vector<T> &v) { for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' '); } template <class T> void read(vector<T> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; } constexpr char sp = ' ', newl = '\n'; constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; constexpr ll INF = (1LL << 60) - 1LL; constexpr ll MOD = 998244353LL; //@docs Docs/UnionFind.md struct UnionFind { vector<int> par, sz; UnionFind(int n) : par(n), sz(n, 1) { iota(par.begin(), par.end(), 0); } inline int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } bool unite(int u, int v) { u = find(u), v = find(v); if (u == v) return false; if (sz[u] < sz[v]) swap(u, v); par[v] = u; sz[u] += sz[v]; return true; } inline int size(int x) { return sz[find(x)]; } inline bool same(int u, int v) { return find(u) == find(v); } }; //////////////////////////////////////////////INSERT ABOVE HERE int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; V<ll> a(n), b(n); read(a); read(b); UnionFind uf(n); for (int i = 0; i < m; i++) { int c, d; cin >> c >> d; --c; --d; uf.unite(c, d); } V<ll> sa(n), sb(n); for (int i = 0; i < n; i++) { sa[uf.find(i)] += a[i]; sb[uf.find(i)] += b[i]; } for (int i = 0; i < n; i++) { if (sa[i] != sb[i]) { cout << "No" << newl; return 0; } } cout << "Yes" << newl; }
#include <iostream> #include <vector> #include <cstring> #include <set> #include <algorithm> #include <cmath> #include <iomanip> #include <queue> #include <deque> #include <map> #include <stack> using namespace std; #define ll long long #define pb push_back #define pp pair<long long,long long> #define ppp pair<pair<long long,long long>, long long > #define f first #define s second #define pd pair<double,long long> #define pv pair<long long,vector<pair<long long,long long> > > void solveTestCase() { ll i,j,k,n,m,x,y; cin>>n>>k; ll op1 = 2*n+k; ll op2 = 2+k; op1=min(op1,2*n); op2=max(op2,2ll); ll ans=0; for (i=op2;i<=op1;i++) { ll res=i-1; if (i>n) res=(2*n-i+1); j=i-k; ll res2=j-1; if (j>n) res2=(2*n-j+1); ans+=res*res2; } cout<<ans<<'\n'; } int main() { ll t,n,i,j,k,l; ios_base::sync_with_stdio(0); cin.tie(0); ll p; t=1; // cin>>t; for (ll T=1;T<=t;T++) { // cout<<"Case #"<<T<<": "; solveTestCase(); } return 0; }
#include <iostream> using namespace std; typedef long long ll; #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)<(y)?(y):(x)) ll comb(ll n, ll k){ if (k == 0 || k == n) return 1; if (k == 1 || k == n-1) return n; return comb(n-1,k)+comb(n-1,k-1); } int main(){ ll n,k;cin >> n >> k;--k; for(ll xyz=3;xyz<=3*n;++xyz){ if(xyz <= n+1){ ll s = (xyz-1)*(xyz-2)/2; if(s <= k){ k -= s; continue; } for(ll x=1;x<=xyz-2;++x){ ll c = xyz-1-x; if(c <= k){ k -= c; continue; } ll y=1+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } }else if(xyz < 2*n+1){ ll s = (3*n-xyz+2)*(xyz-n-1)/2; if(s <= k){ k -= s; }else{ for(ll x=1;x<=xyz-n-1;++x){ ll c = 2*n-xyz+1+x; if(c <= k){ k -= c; continue; } ll y=xyz-n-x+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } } s = (xyz-2)*(2*n-xyz+1)/2; if(s <= k){ k -= s; continue; } for(ll x=xyz-n;x<=n;++x){ ll c = xyz-1-x; if(c <= k){ k -= c; continue; } ll y=1+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } }else{ ll s = (3*n-xyz+2)*(3*n-xyz+1)/2; if(s <= k){ k -= s; continue; } for(ll x=xyz-2*n;x<=n;++x){ ll c = 2*n-xyz+1+x; if(c <= k){ k -= c; continue; } ll y=xyz-n-x+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } } } return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define ll long long #define int long long #define ld long double #define endl "\n" #define pb push_back #define fill(a,val) memset(a,val,sizeof(a)) #define ff first #define ss second #define pii pair<ll,ll> #define all(v) v.begin(),v.end() const ll mod = 1000*1000*1000+7; const ll inf = 1ll*1000*1000*1000*1000*1000*1000 + 7; const ll mod2 = 998244353; const ll N = 1000 * 1000 + 10; const ld pi = 3.141592653589793; ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;} void IO(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } signed main(){ fastio(); cout<<fixed<<setprecision(20); ll n,k; cin>>n>>k; ll a[n+1][n+1]; for(ll i=1;i<=n;i++){ for(ll j=1;j<=n;j++){ cin>>a[i][j]; } } vector<ll>p; for(ll i=2;i<=n;i++){ p.pb(i); } ll cnt=0; do{ ll pr=1,sum=0; for(ll i=0;i<(ll)p.size();i++){ sum+=(a[pr][p[i]]); pr=p[i]; } sum+=(a[pr][1]); if(sum==k){ cnt++; } }while(next_permutation(p.begin(),p.end())); cout<<cnt; return 0; }
#include<bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define LOCAL #endif typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; typedef vector<ll> vi; typedef vector<pll> vpll; const long infl=0x3f3f3f3f3f3f3f3fLL; const int infi=0x3f3f3f3f; #define endl '\n' #define pb push_back #define all(a) (a).begin(),(a).end() #define F first #define S second #define rs(v,n) v.resize(n) #define hell 1000000007 //#define hell 998244353 #define peak 9e18 #define pii acos(-1) #define clr(a,x) memset(a,x,sizeof(a)) auto clk=clock(); mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; namespace Ops{ template<class y,class z> ll expo(ll b,y e,z m){ll a=1;b%=m;while(e!=0){if((e&1)==1){a=a*b;a=a%m;}b=b*b;b%=m;e>>=1;}return a%m;} template<class y> ll power(ll b,y e){ll a=1;while(e!=0){if((e&1)==1){a=a*b;}b=b*b;e>>=1;}return a;} template<class x,class z> ll inv(x b,z m){return expo(b,m-2,m);} template<class x,class y> x invGeneral(x a,y b){if(a==0)return b==1?0:-1;x m=invGeneral(b%a,a);return m==-1?-1:((1-(ll)b*m)/a+b)%b;} template<class x,class y> x min(x a,y b){x c;if(a<=b)c=a;else c=b;return c;} template<class x,class y> x max(x a,y b){x c;if(a>=b)c=a;else c=b;return c;} ll mul(ll x,ll y,ll m){x%=m;y%=m;if(x==0)return 0;if(x%2==0)return (mul(x/2,y,m)*2)%m;return ((mul(x/2,y,m)*2)%m+y)%m;} } using namespace Ops; namespace InOp{ template<class x>istream &operator>>(istream &in,vector<x> &v) {for(auto& i:v)in>>i;return in;} template<class x,class y> ostream &operator<<(ostream &out,pair<x,y> &p) {out<<"("<<p.F<<","<<p.S<<")";return out;} template<class x> ostream &operator<<(ostream &out,vector<x> &v) {out<<"Size : "<<v.size()<<endl;for(auto i:v)out<<i<<" ";out<<endl;return out;} } using namespace InOp; namespace Debug{ #ifdef LOCAL #define deb(...) printall(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void printall(const char* name, Arg1&& arg1){cerr<<name<<" : "<<arg1<<endl;} template <typename Arg1, typename... Args> void printall(const char* names,Arg1&& arg1,Args&&... args){const char* comma=strchr(names,',');cerr.write(names,comma-names)<<" : " <<arg1<<" | ";printall(comma+1,args...);} #else #define deb(...) void(0) #endif } using namespace Debug; namespace Bits{ template<class x> constexpr int onbits(x a){return __builtin_popcount(a);} constexpr int bits(long long x){return 64-__builtin_clzll(x);}//take care of 0 constexpr int bits(int x){return 32-__builtin_clz(x);}//take care of 0 constexpr int traz(int a){return __builtin_ctz(a);} constexpr int traz(long long a){return __builtin_ctzll(a);} } using namespace Bits; namespace YesNo{ //#define CAPITAL void yes(){ #ifdef CAPITAL cout<<"YES"<<"\n"; #else cout<<"Yes"<<"\n"; #endif } void no(){ #ifdef CAPITAL cout<<"NO"<<"\n"; #else cout<<"No"<<"\n"; #endif } } using namespace YesNo; void maester(); int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(6); cerr<<fixed<<setprecision(6); ll test=1; //cin>>test; for(ll tt=1;tt<=test;tt++) { //cout<<"Case #"<<tt<<": "; maester(); } #ifdef LOCAL cerr<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl; #endif return 0; } #define N 1000005 void maester() { ll i,j,k,l,r,m,n,x,y; cin>>n; vi a(n),b(n),c(n); cin>>a>>b>>c; for(i=0;i<n;i++)c[i]=b[c[i]-1]; map<ll,ll> ma; for(auto i:c)ma[i]++; ll ans=0; for(auto i:a)ans+=ma[i]; cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; vector<int> sz; UnionFind(int n=0){ if(n>0) initialize(n); } void initialize(int n){ par.resize(n); sz.assign(n, 1); for(int i=0; i<n; i++){ par[i] = i; } } int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } } bool unite(int x, int y){ x = find(x), y = find(y); if(x == y) return false; if(sz[x] > sz[y]) swap(x, y); par[x] = y; sz[y] += sz[x]; return true; } bool same(int x, int y){ return find(x) == find(y); } int size(int x){ return sz[find(x)]; } }; int main(){ int N, M; cin >> N >> M; vector<int64_t> A(N), B(N); for(int i=0; i<N; i++) cin >> A[i]; for(int i=0; i<N; i++) cin >> B[i]; UnionFind uf(N); while(M--){ int a, b; cin >> a >> b; uf.unite(a-1, b-1); } vector<int64_t> C(N), D(N); for(int i=0; i<N; i++){ int r = uf.find(i); C[r] += A[i]; D[r] += B[i]; } cout << (C==D ? "Yes" : "No") << endl; return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <fstream> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <math.h> #include <numeric> #include <ostream> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <stdint.h> namespace asl { template <typename T> using vec = std::vector<T>; template <typename T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { for (auto &value : vec) is >> value; return is; } } #include <experimental/optional> #include <tuple> namespace asl { typedef long long i64; } #include <random> #include <utility> #define endl '\n' using namespace std; using namespace asl; vec<int> get_diff(vec<int> &a) { int l = 0; vec<int> res(a.size()); for (int i = 0; i < a.size(); ++i) { res[i] = a[i] - l - 1; l = a[i]; } return res; } void solve() { int n, l; cin >> n >> l; vec<int> a(n), b(n); cin >> a >> b; a.push_back(l + 1); b.push_back(l + 1); a = get_diff(a); b = get_diff(b); i64 answer = 0; for (int i = 0, j = 0, k; i <= n; ++i, j = k) { int s = 0; k = j; while (s < b[i]) s += a[k++]; while (j < k && a[j] == 0) ++j; if (s > b[i]) { answer = -1; break; } if (b[i] > 0) { int cur = max(i - j, 0) + max(k - 1 - i, 0); answer += cur; } } cout << answer << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; for (int i = 1; i <= t; ++i) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; // Type alias using ll=long long; using ld=double; using pi=pair<int,int>; using pll=pair<ll,ll>; using pld=pair<ld,ld>; using ti3=tuple<int, int, int>; using vi=vector<int>; using vll=vector<ll>; using vld=vector<ld>; using vpi=vector<pi>; using vpll=vector<ll>; using vpld=vector<ld>; using vti3=vector<ti3>; // Constants const double EPS = 1e-9; const int inf = numeric_limits<int>::max() / 2; const ll mod = 1e9+7; // Macro #define rep(i,n) for(int i=0;i<int(n);++i) #define rrep(i,n) for(int i=int(n-1);i>=0;--i) #define REP(i,a,b) for(int i=int(a);i<int(b);++i) #define RREP(i,a,b) for(int i=int(b-1);i>=int(a);--i) #define SHOW(a) cout << #a << " = " << a << endl #define ARR(a,n) for(int i=0;i<int(n);++i) cout << #a << "[" << i << "]" << " = " << a[i] << endl #define ALL(a) a.begin(),a.end() // Funtcions ll pow(ll base, ll i, ll mod){ ll a = 1; while(i){ if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i >>= 1; } return a; } ll gcd(ll a, ll b){ while(b){ ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll 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; } void solve(){ ll t, n; cin >> t >> n; ll k = n * (100 + t) / t; if(k * t / (100 + t) == n) k--; cout << k << endl; } int main(){ // FastIO //ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <iostream> #include <fstream> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <math.h> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <functional> #include <queue> #include <unordered_set> #include <climits> #include <set> #include <list> #include <cmath> #include <map> //#include <boost/multiprecision/cpp_int.hpp> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; //typedef boost::multiprecision::cpp_int ll; typedef long long ll; int main() { #ifdef DEBUG std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif ll t, N; cin >> t >> N; ll X = (100*N+t-1)/t; cout << X + N - 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p_ll; template<class T> void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; } #define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++) #define all(vec) vec.begin(), vec.end() #define rep(i,N) repr(i,0,N) #define per(i,N) for (ll i=(ll)N-1; i>=0; i--) const ll MOD = pow(10,9)+7; const ll LLINF = pow(2,61)-1; const ll INF = pow(2,30)-1; vector<ll> fac; void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; } ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; } ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; } ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; } ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); } ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- vector<vector<ll>> matsum(vector<vector<ll>> A, vector<vector<ll>> B) { if (A.size()!=B.size()||A[0].size()!=B[0].size()) return {{}}; ll h = A.size(), w = A[0].size(); vector<vector<ll>> result(h,vector<ll>(w,0)); rep(i,h) rep(j,w) result[i][j] = (A[i][j]+B[i][j])%MOD; return result; } vector<vector<ll>> matdiff(vector<vector<ll>> A, vector<vector<ll>> B) { if (A.size()!=B.size()||A[0].size()!=B[0].size()) return {{}}; ll h = A.size(), w = A[0].size(); vector<vector<ll>> result(h,vector<ll>(w,0)); rep(i,h) rep(j,w) result[i][j] = (MOD+(A[i][j]-B[i][j])%MOD)%MOD; return result; } vector<vector<ll>> matdot(vector<vector<ll>> A, vector<vector<ll>> B) { if (A[0].size()!=B.size()) return {{}}; ll h = A.size(), w = B[0].size(), x = A[0].size(); vector<vector<ll>> result(h,vector<ll>(w,0)); rep(i,h) rep(j,w) rep(k,x) result[i][j] = (result[i][j] + A[i][k]*B[k][j]); return result; } vector<vector<ll>> matpow(vector<vector<ll>> A, ll n) { if (A.size()!=A[0].size()) return {{}}; int N = A.size(); vector<vector<ll>> result(N,vector<ll>(N,0)), now; copy(all(A),back_inserter(now)); rep(i,N) result[i][i] = 1; ll p2 = 1; while (n>=p2) { if (n&p2) result = matdot(result,now); now = matdot(now,now); p2 *= 2; } return result; } // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- int main() { ll N; cin >> N; ll X[N], Y[N]; rep(i,N) cin >> X[i] >> Y[i]; ll M; cin >> M; ll op[M][2] = {}; rep(i,M) { cin >> op[i][0]; if (op[i][0]>=3) cin >> op[i][1]; } ll Q; cin >> Q; ll A[Q], B[Q]; rep(i,Q) { cin >> A[i] >> B[i]; B[i]--; } vector<vector<ll>> ope = { {1,0,0}, {0,1,0}, {0,0,1} }; vector<vector<vector<ll>>> opmat(M+1); opmat[0] = ope; rep(i,M) { vector<vector<ll>> mulmat; if (op[i][0]==1) { mulmat = { {0,1,0}, {-1,0,0}, {0,0,1} }; } else if (op[i][0]==2) { mulmat = { {0,-1,0}, {1,0,0}, {0,0,1} }; } else if (op[i][0]==3) { mulmat = { {-1,0,2*op[i][1]}, {0,1,0}, {0,0,1} }; } else if (op[i][0]==4) { mulmat = { {1,0,0}, {0,-1,2*op[i][1]}, {0,0,1} }; } opmat[i+1] = matdot(mulmat,opmat[i]); } // rep(i,M+1) { // for (auto x: opmat[i]) debug(all(x)); // cout << endl; // } rep(i,Q) { vector<vector<ll>> resmat = matdot(opmat[A[i]],{{X[B[i]]},{Y[B[i]]},{1}}); cout << resmat[0][0] << " " << resmat[1][0] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; //const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } /** * @brief Union-Find * @docs docs/union-find.md */ struct UnionFind { vector< int > data; UnionFind() = default; explicit UnionFind(size_t sz) : data(sz, -1) {} bool unite(int x, int y) { x = find(x), y = find(y); if(x == y) return false; //if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return true; } int find(int k) { if(data[k] < 0) return (k); return data[k] = find(data[k]); } int size(int k) { return -data[find(k)]; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int N, Q; cin >> N >> Q; vector< int > C(N); cin >> C; for(auto &c : C) --c; UnionFind uf(N); vector< map< int, int > > mp(N); for(int i = 0; i < N; i++) mp[i][C[i]]++; while(Q--) { int t, a, b; cin >> t >> a >> b; --a, --b; if(t == 1) { int p = uf.find(a); int q = uf.find(b); if(p == q) continue; if(mp[p].size() < mp[q].size()) { swap(p, q); } for(auto &z : mp[q]) mp[p][z.first] += z.second; mp[q].clear(); uf.unite(p, q); } else { cout << mp[uf.find(a)][b] << "\n"; } } }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; int res = a1; if(res > a2) res = a2; if(res > a3) res = a3; if(res > a4) res = a4; cout << res; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); double a,b,c,d; cin >> a >> b >> c >> d; d = - d; double A = (b - d) / (a - c); double B = b - A * a; cout.precision(17); cout << - B / A << endl; }
#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 const ll MODULO = 1000000007LL; ll mod_pow(ll x, ll n, ll modval) { if (n == 0) { return 1; } ll res = mod_pow(x * x % modval, n / 2, modval); if (n & 1) { res = res * x % modval; } return res; } ll mod_minus(ll x, ll modval) { if (x >= 0) { return x % modval; } else { ll mm = ((-x) % modval); if (mm == 0) { return 0; } else { return modval - mm; } } } ll mod(ll x) { return mod_minus(x, MODULO); } ll mod_inv(ll k, ll modval) { return mod_pow(k, modval - 2, modval); } ll minv(ll k) { return mod_pow(k, MODULO - 2, MODULO); } ll mmult(ll x, ll y) { return mod(x * y); } ll sum2(ll x) { return mmult(mmult(mmult(x, x + 1), 2 * x + 1), minv(6)); } ll summ2(ll x) { return x * (x + 1) * (2 * x + 1) / 6; } ll sum1(ll x) { return mod(x * (x + 1) / 2); } void solve() { ll N, A, B; cin >> N >> A >> B; ll x4; if (N - A - B < 0) { x4 = 0; } else { x4 = mmult(mmult(N - A - B + 2, N - A - B + 1), minv(2)); } ll x3 = mmult(x4, 2); ll x2 = mod(mmult(N - A + 1, N - B + 1) - x3); ll x1 = mmult(x2, x2); ll result = mod(mmult(mmult(mmult(N - A + 1, N - A + 1), N - B + 1), N - B + 1) - x1); print(result); // if (A < B) { // swap(A, B); // } // ll result = mmult(mmult(N - A + 1, N - A + 1), mmult(N - B + 1, N - B + 1)); // // 重複を減らす // ll invalid = 0; // invalid = mod(invalid + mmult(A - B + 1, A - B + 1)); // invalid = mod(invalid + mmult(mmult(A - B + 1, B - 1), 4)); // invalid = mod(invalid + mmult(mmult(mmult(A - B + 1, B - 1), B - 1), 4)); // result = mod(result - mmult(invalid, mmult(N - A + 1, N - A + 1))); // // 重複のうち起こりえないものを戻す // result = mod(result + mmult(mmult(sum1(B - 1), N - A + 1), 4)); // result = mod(result + mmult(mmult(mmult(sum1(B - 1), min(N - A + 1, B - 1)), N - A + 1), 8)); // // 戻した中に重複がある // result = mod(result - mmult(sum1(B - 1), 4)); // print(result); } int main() { ll T; cin >> T; for (ll i = 0; i < T; i++) { solve(); } }
#include <vector> #include <stack> #include <queue> #include <list> #include <bitset> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <numeric> #include <iostream> #include <iomanip> #include <string> #include <chrono> #include <random> #include <cmath> #include <cassert> #include <climits> #include <cstring> #include <cstdlib> #include <functional> #include <sstream> using namespace std; const long long MOD = 1000000007; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); int t; cin >> t; auto count = [&](long long N, long long l, long long r, long long t, long long b) { auto n = r - l + 1; auto m = b - t + 1; if (n <= 0 || m <= 0) { return 0LL; } long long x = ((N + 1 - l) + (N + 1 - r)) * n / 2 % MOD; long long y = ((N + 1 - t) + (N + 1 - b)) * m / 2 % MOD; long long res = x * y % MOD; res *= 4; res %= MOD; return res; }; auto count2 = [&](long long N, long long A, long long B) { long long res = 0; long long cnt = (N - A - B + 1); long long x = (1 + cnt) * cnt / 2 % MOD; x = x * (B - A + 1) % MOD; res = x * (N - B + 1) % MOD; res *= 4; res %= MOD; return res; }; while (t-- > 0) { long long N, A, B; cin >> N >> A >> B; if (N < A + B) { cout << 0 << '\n'; } else { if (A > B) { swap(A, B); } long long res = 0; res += count(N, B + 1, A + B - 1, A + B, N); res += count(N, A + B, N, B + 1, A + B - 1); res += count(N, A + B, N, A + B, N); res += count2(N, A, B); res %= MOD; cout << res << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=3e5+5,M=1e9+7,OO=0x3f3f3f3f; int n,q; int arr[N]; int seg[4*N]; void build(int si=0,int ss=0,int se=n-1){ if(ss==se){ seg[si]=arr[ss]; return; } int md=ss+(se-ss)/2,lf=si*2+1,rt=si*2+2; build(lf,ss,md); build(rt,md+1,se); seg[si]=seg[lf]^seg[rt]; } void update(int qi,int qv,int si=0,int ss=0,int se=n-1){ if(ss>qi||se<qi) return; if(ss==se){ seg[si]^=qv; return; } int md=ss+(se-ss)/2,lf=si*2+1,rt=si*2+2; update(qi,qv,lf,ss,md); update(qi,qv,rt,md+1,se); seg[si]=seg[lf]^seg[rt]; } int query(int qs,int qe,int si=0,int ss=0,int se=n-1){ if(ss>qe||se<qs) return 0; if(ss>=qs&&se<=qe) return seg[si]; int md=ss+(se-ss)/2,lf=si*2+1,rt=si*2+2; return query(qs,qe,lf,ss,md)^query(qs,qe,rt,md+1,se); } int main(){ //freopen("myfile.txt","w",stdout); scanf("%d%d",&n,&q); for(int i=0;i<n;++i) scanf("%d",arr+i); build(); while(q--){ int t,x,y; scanf("%d%d%d",&t,&x,&y); if(t==1){ update(--x,y); } else{ --x,--y; printf("%d\n",query(x,y)); } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> ii; typedef long long ll; typedef tuple<int,int,int> i3; int x[105], y[105], z[105], n, m; ll fat[20], dp[(1 << 18) + 5][105]; vector<int> mask[25]; ll solve(int curmask, int pos){ int cursz = __builtin_popcount(curmask); if(pos == m) return fat[n-cursz]; if(dp[curmask][pos] != -1) return dp[curmask][pos]; int cnt = 0; for(int i = 0; i < y[pos]; i++) cnt += !!((1 << i) & curmask); ll res = 0; int biton = __builtin_popcount(curmask); if(cnt <= z[pos]){ if(biton == x[pos]) res = solve(curmask, pos+1); else{ for(int j = 0; j < n; j++) if(!((1 << j) & curmask)) res += solve(curmask|(1 << j), pos); } } return dp[curmask][pos] = res; } int main(){ cout.sync_with_stdio(0); cin.tie(0); memset(dp, -1, sizeof dp); cin >> n >> m; vector<i3> vec; for(int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; vec.emplace_back(a, b, c); } sort(vec.begin(), vec.end()); for(int i = 0; i < m; i++){ int a, b, c; tie(a, b, c) = vec[i]; x[i] = a; y[i] = b; z[i] = c; } fat[0] = 1; for(int i = 1; i <= n; i++) fat[i] = fat[i-1] * i; if(m == 0){ cout << fat[n] << endl; return 0; } cout << solve(0, 0) << endl; return 0; }
#include <bits/stdc++.h> #define ALL(v) v.begin(), v.end() #define MAX 510000 #define rrep(i, n) for(ll i = 0; i < (ll)(n); i++) #define rep(i, n) for(ll i = 1; i <= (ll)(n); i++) #define dcout cout<<fixed<<setprecision(15); #define mp make_pair #define pb push_back using namespace std; typedef long long int ll; typedef pair<ll, ll> P; constexpr int MOD = 1e9 + 7; constexpr ll inf = 1LL << 60; template< typename S, typename T > inline void chmax(S &a, const T &b) { if(a < b) a = b; } template< typename S, typename T > inline void chmin(S &a, const T &b) { if(a > b) a = b; } ll gcd(ll x, ll y) { if (x == 0) return y; return gcd(y%x, x);} ll lcm(ll x, ll y) { return x * y / gcd(x, y); } ll modpow(ll a, ll n, ll p) { if(n==0) return (ll)1; if (n == 1) return a % p; if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p; ll t = modpow(a, n / 2, p); return (t * t) % p;} ll modinv(ll a, ll m) { if(m==0)return (ll)1; ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u;} ll fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; }} ll COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;} ll com(ll n,ll m){ if(n<m || n<=0 ||m<0){ return 0; } if( m==0 || n==m){ return 1; } ll k=1; for(ll i=1;i<=m;i++){ k*=(n-i+1); k%=MOD; k*=modinv(i,MOD); k%=MOD; } return k;} ll rad(ll u, ll p){ ll cnt=0; while(u%p==0){ u/=p; cnt++; } return cnt;} //////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); ll n; cin>>n; vector <pair<ll,ll>> x; vector <pair<ll,ll>> y; rep(i,n){ ll a,b; cin>>a>>b; x.pb(mp(a,i)); y.pb(mp(b,i)); } sort(ALL(x),greater<P>()); sort(ALL(y),greater<P>()); ll ans=0; ll m1=x[0].second; ll m2=x[n-1].second; ll m3=y[0].second; ll m4=y[n-1].second; if((m1==m3&&m2==m4)||(m1==m4&&m2==m3)){ ans=max(max(max(x[1].first-x[n-1].first,x[0].first-x[n-2].first),y[1].first-y[n-1].first),y[0].first-y[n-2].first); } else{ if(x[0].first-x[n-1].first==y[0].first-y[n-1].first){ ans=x[0].first-x[n-1].first; } else if(x[0].first-x[n-1].first>y[0].first-y[n-1].first){ ans=max(max(x[1].first-x[n-1].first,x[0].first-x[n-2].first),y[0].first-y[n-1].first); } else{ ans=max(max(y[1].first-y[n-1].first,y[0].first-y[n-2].first),x[0].first-x[n-1].first); } } cout<<ans<<endl; return 0; }
// Generated by 2.3.1 https://github.com/kyuridenamida/atcoder-tools #include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const i64 MOD = 1e9 + 7; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template <typename T> bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } signed main(){ int n; cin >> n; vector<int> x(n), y(n); for(int i = 0; i < n; ++i){ cin >> x[i] >> y[i]; } vector<pair<int,int>> vx(n), vy(n); for(int i = 0; i < n; ++i){ vx[i] = {x[i], i}; vy[i] = {y[i], i}; } sort(vx.begin(), vx.end()); sort(vy.begin(), vy.end()); vector<tuple<int,int,int>> v; for(int i = 0; i < min(10, n); ++i){ for(int j = 0; j < min(10, n); ++j){ int dist = abs(vx[i].first - vx[n - j - 1].first); if(vx[i].second == vx[n - j - 1].second) continue; v.emplace_back(dist, min(vx[i].second, vx[n - j - 1].second), max(vx[i].second, vx[n - j - 1].second)); } } for(int i = 0; i < min(10, n); ++i){ for(int j = 0; j < min(10, n); ++j){ int dist = abs(vy[i].first - vy[n - j - 1].first); if(vy[i].second == vy[n - j - 1].second) continue; v.emplace_back(dist, min(vy[i].second, vy[n - j - 1].second), max(vy[i].second, vy[n - j - 1].second)); } } sort(v.begin(), v.end(), greater<>()); set<pair<int,int>> s; for(auto [d, i, j] : v){ if(s.find({i, j}) == s.end()){ s.emplace(i, j); } if(s.size() == 2){ cout << d << endl; return 0; } } return 0; }
#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 K, long long N, long long M, std::vector<long long> A){ vector<ll> B(K); ll used = 0; vector<pair<double, int>> ps; rep(i, K) { B[i] = A[i] * M / N; used += B[i]; // (BN - AM) / NM を最小化する double cur = abs(1.0 * (B[i] * N - A[i] * M) / (N * M)); // double updated = abs(1.0 * ((B[i] + 1) * N - A[i] * M) / (N * M)); // ps.emplace_back(cur - updated, i); // 下降が大きいものから ps.emplace_back(cur, i); // 大きいものから } assert(used <= M); // for (ll e: B) { // cout << e << " "; // } // cout << endl; sort(ps.rbegin(), ps.rend()); rep(i, M - used) { B[ps[i].second]++; } for (ll e: B) { cout << e << " "; } cout << endl; } // Generated by 2.2.0 https://github.com/kyuridenamida/atcoder-tools int main(){ long long K; scanf("%lld",&K); long long N; scanf("%lld",&N); long long M; scanf("%lld",&M); std::vector<long long> A(K); for(int i = 0 ; i < K ; i++){ scanf("%lld",&A[i]); } solve(K, N, M, std::move(A)); return 0; }
#include <bits/stdc++.h> int main(){ int K, N, M; std::cin >> K >> N >> M; std::vector< long long > A(K); for(int i=0; i<K; i++){ std::cin >> A[i]; } std::vector< long long > B(K); std::vector< std::pair< long long, long long > > B_zan_i(K); long long total = 0; for(int i=0; i<K; i++){ B[i] = A[i] * M / N; total += B[i]; B_zan_i[i].first = (A[i] * M) % N; B_zan_i[i].second = i; } std::sort(B_zan_i.begin(), B_zan_i.end()); for(int i=0; i<M-total; i++){ B[B_zan_i[K - i - 1].second]++; } for(int i=0; i<K; i++){ if(i == K-1){ std::cout << B[i] << std::endl; }else{ std::cout << B[i] << " "; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define rep(i, n) for (int i = 0; i < n; ++i) #define rep1(i, n) for (int i = 1; i < n; ++i) #define exrep(i, a, b) for (ll i = a; i < b; i++) #define out(x) cout << x << endl #define EPS (1e-7) #define gearup \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<pair<int, int>> vpii; typedef vector<vector<int>> vvi; typedef vector<vector<char>> vvc; typedef vector<vector<bool>> vvb; typedef vector<vector<double>> vvd; typedef vector<vector<string>> vvs; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; ll MOD = 998244353; const long long L_INF = 1LL << 60; const int INF = 2147483647; // 2^31-1 const double PI = acos(-1); //cout<<fixed<<setprecision(10); template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> void debug(T v) { rep(i, v.size()) cout << v[i] << " "; cout << endl; } ll pcount(ll x) { return __builtin_popcountll(x); } const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; //std::ifstream in("input.txt"); //std::cin.rdbuf(in.rdbuf()); signed main() { gearup; ll a, b, c; cin >> a >> b >> c; if (c % 2 == 0) { if (abs(a) > abs(b)) out(">"); else if (abs(a) < abs(b)) out("<"); else out("="); } else { if (a < 0) { if (b >= 0) out("<"); else { if (a > b) out(">"); else if (a < b) out("<"); else out("="); } } else { if (a > b) out(">"); else if (a < b) out("<"); else out("="); } } }
#include <bits/stdc++.h> using namespace std; int main() { int64_t a, b, c; cin >> a >> b >> c; uint64_t abs_a = abs(a); uint64_t abs_b = abs(b); if (!(c & 1)) cout << (abs_a == abs_b ? "=" : (abs_a > abs_b ? ">" : "<")); else cout << (a == b ? "=" : (a > b ? ">" : "<")); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; double X0, Y0, XN2, YN2; cin >> N >> X0 >> Y0 >> XN2 >> YN2; double mX = (X0 + XN2) / 2, mY = (Y0 + YN2) / 2; double oX = X0 - mX, oY = Y0 - mY; double angle = 2 * M_PI / N; double X1 = oX * cos(angle) - oY * sin(angle), Y1 = oX * sin(angle) + oY * cos(angle); cout << fixed << setprecision(11) << (X1 + mX) << " " << (Y1 + mY) << endl; }
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <functional> #include <iterator> #include <numeric> #include <cstdint> #include <cmath> #include <bitset> #include <string> #include <list> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <random> #include <chrono> #include <cassert> #define N_TIMES(i, n) for ( uint64_t i = 0; i < n; ++i ) #define N_TIMES_REV(i, n) for ( int64_t i = n - 1; i >= 0; --i ) template<typename T> using maximum_heap = std::priority_queue<T, std::vector<T>, std::less<T>>; template<typename T> using minimum_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<typename T> std::istream& operator>>(std::istream &is, std::vector<T> &v) { for (typename std::vector<T>::size_type i = 0; i < v.size(); ++i) { is >> v[i]; } return is; } template<typename T> struct Point2D { T x, y; Point2D(T x = 0, T y = 0) : x(x), y(y) {} bool operator==(const Point2D &p) const { return x == p.x && y == p.y; } bool operator!=(const Point2D &p) const { return x != p.x || y != p.y; } Point2D operator+ (const Point2D &p) const { return Point2D(x + p.x, y + p.y); } Point2D operator- (const Point2D &p) const { return Point2D(x - p.x, y - p.y); } Point2D& operator+=(const Point2D &p) { x += p.x; y += p.y; return *this; } Point2D& operator-=(const Point2D &p) { x -= p.x; y -= p.y; return *this; } Point2D operator* (T k) const { return Point2D(x * k, y * k); } Point2D operator/ (T k) const { return Point2D(x / k, y / k); } Point2D& operator*=(T k) { x *= k; y *= k; return *this; } Point2D& operator/=(T k) { x /= k; y /= k; return *this; } T operator*(const Point2D &p) const { return x * p.x + y * p.y; } T dist_pow2() const { return x * x + y * y; } bool operator< (const Point2D &p) const { return dist_pow2() == p.dist_pow2() ? ( y == p.y ? x < p.x : y < p.y ) : dist_pow2() < p.dist_pow2(); } bool operator> (const Point2D &p) const { return dist_pow2() == p.dist_pow2() ? ( y == p.y ? x > p.x : y > p.y ) : dist_pow2() > p.dist_pow2(); } bool operator<=(const Point2D &p) const { return (*this == p) || (*this < p); } bool operator>=(const Point2D &p) const { return (*this == p) || (*this > p); } }; template<typename T> bool is_parallel(const Point2D<T> &p, const Point2D<T> &q) { return p.x * q.y == q.y * p.x; } template<typename T> Point2D<T> orthogonal(const Point2D<T> &p) { return Point2D<T>(p.y, -p.x); } template<typename T> long double angle(const Point2D<T> &p, const Point2D<T> &q) { T ip = p * q; T dist = p.dist_pow2() * q.dist_pow2(); long double cosine = static_cast<long double>(ip) / sqrt(static_cast<long double>(dist)); return acos(cosine); } auto rad2deg = [] (long double deg) { return (deg * 180.0) / M_PI; }; auto deg2rad = [] (long double deg) { return (deg * M_PI) / 180.0; }; template<typename T> std::istream& operator>>(std::istream &is, Point2D<T> &p) { is >> p.x >> p.y; return is; } template<typename T> std::ostream& operator<<(std::ostream &os, const Point2D<T> &p) { os << '(' << p.x << ", " << p.y << ')'; return os; } typedef Point2D<long double> Point; Point rotate(const Point &p, long double theta) { return Point( p.x * cos(theta) - p.y * sin(theta), p.x * sin(theta) + p.y * cos(theta) ); } using namespace std; int main() { uint64_t N; cin >> N; Point p0, ph; cin >> p0 >> ph; Point x = (p0 + ph) / 2; Point v = p0 - x; Point u = rotate(v, deg2rad(180.0 / (N >> 1))); Point p1 = u + x; cout << fixed << setprecision(12) << p1.x << ' ' << fixed << setprecision(12) << p1.y << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() void chmax(ll &x, ll y) {x = max(x,y);} void chmin(ll &x, ll y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} template<class in_vec_cout> void vec_cout(vector<in_vec_cout> vec) { for (in_vec_cout res : vec) {cout << res << " ";} cout << endl; } const ll inf = 1e18; const ll mod = 1e9 + 7; ll POW(ll x, ll n) { if (n==0) return 1; if (n%2) return x * POW(x,n-1); ll res = POW(x,n/2); return res * res; } int main() { ll N; cin >> N; ll ans = inf; for (ll b=0; b<=60; b++) { ll x = POW(2,b); ll a = N/x, c = N%x; ll res = a + b + c; chmin(ans,res); } cout << ans << endl; }
#include<bits/stdc++.h> #define pll pair<ll, ll> #define fi first #define se second #define pb push_back #define task "nondec" #define pii pair<pll, ll> using namespace std; using ll = long long; const int N = 1e5+5; const ll mod = 1e9 + 7; const ll base = 350; const ll cs = 331; const long double Exp = 1e-7; ll m, n, t, k, tong, ans, b[N], fe[N], c[N], d[N]; long double a[N]; vector<ll> adj[N]; vector<ll> kq; ll pw(ll k, ll n) { ll total = 1; for(; n; n >>= 1) { if(n & 1)total = total * k % mod; k = k * k % mod; } return total; } void add(ll& x, ll y) { x += y; if(x >= mod)x -= mod; } long double f(long double x) { long double res = 0; for(int i = 1; i <= n; i ++)res += x + a[i] - min(x*2,a[i]); return res * 1.0 / n; } void sol() { cin >> n; for(int i = 1; i <= n; i ++)cin >> a[i]; sort(a+1, a+1+n); ll lf = 1, rt = n, mid; while(lf <= rt) { mid = (lf + rt) / 2; if(mid < n && f(a[mid] /2.0) > f(a[mid+1] / 2.0))lf = mid + 1; else rt = mid - 1; } cout << fixed << setprecision(9) << f(a[lf] / 2.0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(task".in", "r")) { freopen(task".in", "r", stdin); freopen(task".out", "w", stdout); } int ntest = 1; //cin >> ntest; while(ntest -- > 0) sol(); } /* 4 3 3 2 1 1 2 1 2 3 4 */
# include <iostream> # include <vector> # include <utility> # include <algorithm> using namespace std; int main() { int n,m,q; cin>>n>>m>>q; vector<pair<int,int>>v; for(int i=0; i<n; i++) { int a,b; cin>>a>>b; v.push_back(make_pair(a,b)); } sort(v.begin(),v.end()); int c[m]; for(int i=0; i<m; i++) { cin>>c[i]; } for(int i=0; i<q; i++) { int x,y; cin>>x>>y; int s=0; vector<int>u; for(int j=0; j<m; j++) { if(j+1<x || j+1>y) { u.push_back(c[j]); } } sort(u.begin(),u.end()); int visit[10000]={0}; for(int j=0; j<u.size(); j++) { int l=0; int h=10000; for(int z=0; z<n; z++) { if(u[j]>=v[z].first && visit[z]==0) { l=max(l,v[z].second); if(l==v[z].second) { h=z; } } } visit[h]=1; s=s+l; } cout<<s<<endl; } }
#include <bits/stdc++.h> template <typename T> T next() { T temp; std::cin >> temp; return temp; } template <> int next() { int temp; scanf("%d", &temp); return temp; } template <> long long next() { long long temp; scanf("%lld", &temp); return temp; } template <> double next() { double temp; scanf("%lf", &temp); return temp; } template <> float next() { float temp; scanf("%f", &temp); return temp; } template <typename T1, typename T2> std::pair<T1, T2> next() { std::pair<T1, T2> temp; temp.first = next<T1>(); temp.second = next<T2>(); return temp; } template <typename T> std::vector<T> next(int n) { std::vector<T> temp(n); for(int i = 0; i < n; i++) temp[i] = next<T>(); return temp; } template <typename T1, typename T2> std::vector<std::pair<T1, T2>> next(int n) { std::vector<std::pair<T1, T2>> temp(n); for(int i = 0; i < n; i++) temp[i] = next<T1, T2>(); return temp; } template <typename T> void print(const T &x, char endc = '\n') { std::cout << x << endc; return; } void print(const int &x, char endc = '\n') { printf("%d%c", x, endc); return; } void print(const long long &x, char endc = '\n') { printf("%lld%c", x, endc); return; } void print(const size_t &x, char endc = '\n') { printf("%zu%c", x, endc); return; } void print(const float &x, char endc = '\n') { printf("%f%c", x, endc); return; } void print(const double &x, char endc = '\n') { printf("%lf%c", x, endc); return; } template<typename T1, typename T2> void print(const std::pair<T1, T2> &p, char sepc = ' ', char endc = '\n') { print(p.first, sepc); print(p.second, endc); return; } template<typename T> void print(const std::vector<T> &v, char sepc = ' ', char endc = '\n') { for(const T &e : v) print(e, sepc); printf("%c", endc); return; } template<typename T> class SparseGraph { struct Node { std::vector<std::pair<int, T>> adj; T value; }; private: std::vector<Node> nodes; int nodeSize; int edgeSize = 0; public: SparseGraph(int n) { nodes.resize(n); nodeSize = n; } SparseGraph(const std::vector<T> &values) { int n = values.size(); nodes.resize(n); nodeSize = n; for(int i = 0; i < n; i++) nodes[i].value = values[i]; } void addEdge(int u, int v, const T &edgeVal = 0) { nodes[u].adj.push_back({v, edgeVal}); edgeSize++; } const int &getNodeSize() const { return nodeSize; } const int &getEdgeSize() const { return edgeSize; } const T &valueOf(int here) const { return nodes[here].value; } const std::vector<std::pair<int, T>> &adjOf(int here) const { return nodes[here].adj; } }; using namespace std; double solve(int connected, int disconnected, vector<double> &memo) { if(disconnected == 0) return 0.0; double &ret = memo[connected]; if(!ret) { ret = solve(connected+1, disconnected-1, memo) + 1.0 + 1.0*connected/disconnected; } return ret; } void eachTC() { int N = next<int>(); vector<double> memo(N, 0.0); double res = solve(1, N-1, memo); printf("%.9lf", res); } int main() { constexpr bool multipleTC = false; int T = multipleTC ? next<int>() : 1; while(T--) eachTC(); return 0; }
#include<bits/stdc++.h> using namespace std; //dengyaotriangle! const int maxn=1005*2; int n,m; vector<int> adj[maxn]; bool vis[maxn]; void adde(int a,int b){ adj[a].push_back(b); adj[b].push_back(a); } int dfs(int u){ vis[u]=1; int msk=1<<(u<=n); for(int i=0;i<(int)adj[u].size();i++){ int v=adj[u][i]; if(!vis[v])msk|=dfs(v); } return msk; } int cnt[2]; int main(){ ios::sync_with_stdio(0);cin.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ char _[maxn]; cin>>_; for(int j=1;j<=m;j++){ if(_[j-1]=='#')adde(i,j+n); } } adde(1,1+n);adde(n,1+n);adde(1,n+m);adde(n,n+m); for(int i=1;i<=n+m;i++)if(!vis[i]){ int cw=dfs(i); for(int j=0;j<2;j++)cnt[j]+=(cw>>j)&1; } cout<<min(cnt[0],cnt[1])-1; return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <cassert> #include <cmath> #include <string> #include <queue> #include <set> #include <map> #include <cstdlib> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define li long long #define pii pair<int, int> #define vi vector<int> #define li long long #define forn(i, n) for (int i = 0; i < (int)n; i++) #define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++) const int MAXN = 2005; bool visited[MAXN]; vi edges[MAXN]; int n, m; void dfs(int v) { visited[v] = true; for (int u : edges[v]) { if (!visited[u]) dfs(u); } } void add_edge(int row, int col) { edges[row].pb(col + n); edges[col + n].pb(row); } int main() { scanf("%d%d", &n, &m); forn(i, n) { string s; cin >> s; forn(j, m) { if (s[j] == '#') { add_edge(i, j); } } } /*forn(i, n) { add_edge(i, 0); add_edge(i, m - 1); } forn(j, m) { add_edge(0, j); add_edge(n - 1, j); }*/ add_edge(0, 0); add_edge(0, m); add_edge(n - 1, 0); add_edge(n - 1, m - 1); int row_comp = 0; forn(i, n) { if (!visited[i]) { ++row_comp; dfs(i); } } memset(visited, false, sizeof(visited)); int col_comp = 0; fore(i, n, n + m - 1) { if (!visited[i]) { ++col_comp; dfs(i); } } //cerr << row_comp << " " << col_comp << endl; printf("%d", min(row_comp - 1, col_comp - 1)); }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <tuple> #include <vector> const long long P = 1e9 + 7; const double EPS = 1e-9; using namespace std; void solve() { int n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<vector<long long>> dp(n, vector<long long>(n + 1)); // dp[i][j]: num ways for v[0..i] with j segments // dp[i][j] = r[SUM(v[0..i]) % j] for (int i = 0; i < n; i++) dp[i][1] = 1; for (int k = 2; k <= n; k++) { long long s = 0; vector<long long> ct(k, 0); for (int i = 0; i < n; i++) { s = (s + v[i]) % k; dp[i][k] = ct[s]; ct[s] = (ct[s] + dp[i][k - 1]) % P; } } long long res = 0; for (int k = 1; k <= n; k++) res = (res + dp[n - 1][k]) % P; cout << res << '\n'; } /* YOU CAN DO THIS!! ;) 1. Note the limits! 2. Give logical, short variable names 3. If you are stuck for a long time, skip to next problem 4. Can't prove a pattern (Esp Game Theory)? Brute force for small numbers and observe! */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // int t; // cin >> t; while (t--) { solve(); } }
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <tuple> #include <utility> #include <vector> #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false) template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; } #else #define dump(...) do{ } while(false) #endif 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 T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; } template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; } template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on template<ll MOD = 1000000007> class ModInt { ll n; ModInt constexpr inverse() const { return ModInt::pow(*this, MOD - 2); } public: ModInt() : n(0) {} ModInt(ll _n) : n(((_n % MOD) + MOD) % MOD) {} ModInt operator+=(const ModInt &m) { n += m.n; if (n >= MOD) n -= MOD; return *this; } ModInt operator-=(const ModInt &m) { n -= m.n; if (n < 0) n += MOD; return *this; } ModInt operator*=(const ModInt &m) { n *= m.n; if (n >= MOD) n %= MOD; return *this; } ModInt operator/=(const ModInt &m) { (*this) *= m.inverse(); return *this; } friend ModInt operator+(ModInt t, const ModInt &m) { return t += m; } friend ModInt operator-(ModInt t, const ModInt &m) { return t -= m; } friend ModInt operator*(ModInt t, const ModInt &m) { return t *= m; } friend ModInt operator/(ModInt t, const ModInt &m) { return t /= m; } ModInt operator=(const ll l) { n = l % MOD; if (n < 0) n += MOD; return *this; } friend ostream &operator<<(ostream &out, const ModInt &m) { out << m.n; return out; } friend istream &operator>>(istream &in, ModInt &m) { ll l; in >> l; m = l; return in; } static constexpr ModInt pow(const ModInt x, ll p) { ModInt<MOD> ans = 1; for (ModInt<MOD> m = x; p > 0; p /= 2, m *= m) { if (p % 2) ans *= m; } return ans; } static constexpr ll mod() { return MOD; } }; using mint = ModInt<>; mint operator"" _m(unsigned long long m) { return mint(m); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin, n; vector<ll> a(n); rep(i, 0, n) { cin, a[i]; } auto dp = make_v(n + 1, n + 1, 0_m); dp[0][0] = 1; mint ans = 0; vector<ll> sum(n + 1, 0); auto mp = make_v(n + 1, n + 1, 0_m); rep(i, 0, n) { sum[i + 1] = sum[i] + a[i]; rep(j, 0, n) { mp[j][sum[i] % (j + 1)] += dp[i][j]; } rep(j, 0, n) { dp[i + 1][j + 1] += mp[j][sum[i + 1] % (j + 1)]; /* rep(k, 0, i + 1) { if (sum[i + 1] % (j + 1) == sum[k] % (j + 1)) { dp[i + 1][j + 1] += dp[k][j]; } } //*/ } } rep(i, 0, n + 1) { ans += dp[n][i]; } print(ans); return 0; }
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; 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 (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 200010; int main() { int n; scanf("%d", &n); vector<int> a(n), b(n); rep(i, n) scanf("%d", &a[i]); rep(i, n) scanf("%d", &b[i]); int p[n], owner[n]; rep(i, n) { scanf("%d", &p[i]); --p[i]; owner[p[i]] = i; } vector<int> idx(n); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&](const int& i, const int& j) { return a[i] < a[j]; }); vector<pii> ans; for (auto i : idx) { int j = owner[i]; if (i == j) continue; if (a[i] <= b[p[i]] || a[j] <= b[p[j]]) { puts("-1"); return 0; } ans.emplace_back(i, j); swap(p[i], p[j]); swap(owner[p[i]], owner[p[j]]); } printf("%d\n", ans.size()); for (auto [i, j] : ans) printf("%d %d\n", i + 1, j + 1); }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define INF 0x3f3f3f3f #define pi M_PI typedef pair<ll, ll> llPair; typedef vector<ll> vll; typedef vector<ld> vld; typedef vector<bool> vb; typedef priority_queue<ll> pqll; ll MOD = 1000000007; /* Really doe, like really doe Really doe, like really doe */ class DisjSet { int *rank, *parent, *size, n; public: DisjSet(int n) { rank = new int[n]; parent = new int[n]; size = new int[n]; this->n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } int siz(int x) { return size[x]; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void Union(int x, int y) { int xset = find(x); int yset = find(y); if (xset == yset) return; if (rank[xset] < rank[yset]) { parent[xset] = yset; size[yset] = size[yset] + size[xset]; } else if (rank[xset] > rank[yset]) { parent[yset] = xset; size[xset] = size[yset] + size[xset]; } else { parent[yset] = xset; rank[xset] = rank[xset] + 1; size[xset] = size[yset] + size[xset]; } } }; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; t = 1; for (ll z = 0; z < t; ++z) { ll n; cin >> n; vll a(n); vll b(n); vll p(n); vll p2(n); vb finish(n, false); DisjSet obj(n); bool pos = true; ll res = n; for (ll i = 0; i < n; ++i) { cin >> a[i]; } for (ll i = 0; i < n; ++i) { cin >> b[i]; } for (ll i = 0; i < n; ++i) { cin >> p[i]; p[i]--; if (obj.find(i) != obj.find(p[i])) { obj.Union(i, p[i]); --res; } p2[p[i]] = i; if (p[i] != i && a[i] <= b[p[i]]) { pos = false; } else if (p[i] == i) { finish[i] = true; } } if (!pos) { cout << -1 << '\n'; continue; } else if (n == 1) { cout << 0 << '\n'; continue; } vector<tuple<ll, ll, ll>> pair(n); for (ll i = 0; i < n; ++i) { pair[i] = make_tuple(b[p[i]], i, p2[i]); } cout << n - res << '\n'; ll k = n - res; sort(pair.begin(), pair.end()); for (ll i = 0; i < n; ++i) { ll j = get<1>(pair[i]); if (!finish[obj.find(j)]) { finish[obj.find(j)] = true; ll t = get<2>(pair[i]); while (t != j) { cout << t + 1 << ' ' << p[t] + 1 << '\n'; t = p2[t]; } } } } return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll=int; using ld=long double; using pll=pair<ll, ll>; //using mint = modint1000000007; #define rep(i,n) for (ll i=0; i<n; ++i) #define all(c) begin(c),end(c) #define PI acos(-1) #define oo 2e18 template<typename T1, typename T2> bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} template<typename T1, typename T2> bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} //priority_queue<ll, vector<ll>, greater<ll>> Q; /* 01234でタイルの方向管理? */ #define TIMEOVER_M 20000 #define TIMEOVER 1950000 ll T[50][50]; bool B[50][50]; ll P[50][50]; ll D[50][50]; char *ans; char *W; ll score=0; ll high=0; ll dx[]={0, 1, 0, -1}; ll dy[]={-1, 0, 1, 0}; char dc[]={'U', 'R', 'D', 'L'}; bool all_fin; bool fin; clock_t start; clock_t start_m; ll loop; void steped(ll y, ll x, bool b){ ll d=D[y][x]; if (d){ --d; ll ny=y+dy[d]; ll nx=x+dx[d]; B[ny][nx]=b; } B[y][x] = b; } // 行き止まりになったら、その時点でのstringを出力 void dfs(ll y, ll x, ll c){// {y, x, 歩数} bool used[4]={}; rep(i, 4){ clock_t now = clock(); if(now-start_m>TIMEOVER_M) fin=true; if(now-start>TIMEOVER) all_fin=true; ll r=now%4; while(used[r]) (r+=1)%=4; used[r]=true; ll ny=y+dy[r]; ll nx=x+dx[r]; // cout << "time:" << now-start << endl; if(ny<0 || nx<0 || ny>49 || nx>49 || B[ny][nx] || fin || all_fin){ if(chmax(high, score)){ if(ans) free(ans); ans=strdup(W); } } else{ //反映 score+=P[ny][nx]; W[c]=dc[r]; steped(ny, nx, true); dfs(ny, nx, c+1); //復元 score-=P[ny][nx]; W[c]='\0'; steped(ny, nx, false); } } } int main(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); W=(char*)calloc(2000, 1); start = clock(); ll si, sj; cin >> si >> sj; rep(i, 50) rep(j, 50) cin >> T[i][j]; rep(i, 50) rep(j, 50) cin >> P[i][j]; // タイルの方向D rep(i, 50) rep(j, 50){ rep(k, 4){ ll ny=i+dy[k]; ll nx=j+dx[k]; if(ny<0 || nx<0 || ny>49 || nx>49) continue; if(T[i][j]==T[ny][nx]) D[i][j]=k+1; } } while(!all_fin){ loop++; fin=false; start_m = clock(); score = P[si][sj]; chmax(high, score); rep(y, 50) rep(x, 50) B[y][x]=false; rep(i, 2000) W[i]='\0'; steped(si, sj, true); dfs(si, sj, 0); } // cout << high << " " << loop << endl; printf("%s\n", ans); }
#include <bits/stdc++.h> using namespace std; const int Mod = 998244353; int n, s, w[105]; long long ans, fac[105], dp[105][10005]; int main(){ scanf("%d", &n); for(int i=1; i<=n; ++i){ scanf("%d", &w[i]); s += w[i]; } if(s&1){ puts("0"); return 0; } dp[0][0] = 1; for(int i=1; i<=n; ++i){ for(int j=i; j>=1; --j){ for(int k=s/2; k>=w[i]; --k){ dp[j][k] += dp[j-1][k-w[i]]; dp[j][k] %= Mod; } } } fac[0] = 1; for(int i=1; i<=n; ++i) fac[i] = fac[i-1] * i % Mod; for(int i=1; i<=n; ++i) ans = (ans+fac[i]*dp[i][s/2]%Mod*fac[n-i]%Mod) % Mod; printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> #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; typedef pair<int,int> P; const int INF = 1001001001; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; int main() { long double n,d,h; cin >> n >> d >> h; double mx=0,dx=0; rep(i,n){ double di,hi; cin >> di >> hi; double k = (hi-h)/(di-d); double b = h-(k*d); mx = max(b,mx); } if(mx<=0.0){ cout << 0.0 <<endl; }else{ cout << mx << endl; } return 0; }
#include <iostream> #include <algorithm> #include <cmath> #include <string> #include <map> //http://vivi.dyndns.org/tech/cpp/map.html #include <set> //http://vivi.dyndns.org/tech/cpp/set.html #include <vector> #include <deque> #include <queue> #include <numeric> //gcd,lcm c++17 #include <tuple> #include <iomanip> //setprecision #include <unordered_map> #include <chrono> //#define _GLIBCXX_DEBUG using namespace std; template <typename T> using vc = vector<T>; template <typename T> using vv = vc<vc<T>>; template <typename T> using PQ = priority_queue<T, vc<T>, greater<T>>; typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; //typedef __uint128_t Int; using vi = vc<int>; using vl = vc<ll>; using vd = vc<double>; #define FOR(i, a, b) for (int i = a; i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() #define sz(x) (int)(x).size() template <class T1, class T2, class Pred = std::less<T2>> struct sort_pair_second { //sort_pair_second<ll,ll,greater<ll>>() bool operator()(const std::pair<T1, T2> &left, const std::pair<T1, T2> &right) { Pred p; return p(left.second, right.second); } }; 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; } int MOD = 1e9 + 7; double EPS = 1e-9; int INF = 2000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1); int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; int rdx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int rdy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; vi G[200000]; int mx, mn; vc<pi> A(200000); bool visited[200000] = {}; int ans = -INF; void dfs(int v) { if (sz(G[v])) visited[v] = true; for (auto x : G[v]) { if (visited[x]) continue; chmax(ans, A[x].first - mn); dfs(x); } } int main() { int N, M; cin >> N >> M; A.resize(N); FOR(i, 0, N) { cin >> A[i].first; A[i].second = i; } auto B = A; sort(ALL(B)); FOR(i, 0, M) { int x, y; cin >> x >> y; x--, y--; G[x].push_back(y); } FOR(i, 0, N) { if (visited[B[i].second]) continue; mn = B[i].first; dfs(B[i].second); } cout << ans << endl; }