code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include <bits/stdc++.h> using namespace std; /* string r(string s){ string r; for(int i=s.size()-1;i>=0;i--){ r+=s.at(i); } return r; } */ int main(){ string s; cin >> s; string t; bool r=false; for(int i=0;i<s.size();i++){ if(s.at(i)!='R'){ if(!r){ if(t=="" || t.at(t.size()-1)!=s.at(i)){ t+=s.at(i); }else{ t.erase(t.size()-1); } }else{ if(t=="" || t.at(0)!=s.at(i)){ t=s.at(i)+t; }else{ t.erase(0,1); } } }else{ r=!r; } } if(r){ reverse(t.begin(),t.end()); cout << t <<endl; }else{ cout << t <<endl; } }
// Problem: D - Message from Aliens // Contest: AtCoder - ZONe Energy Programming Contest // URL: https://atcoder.jp/contests/zone2021/tasks/zone2021_d // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include<bits/stdc++.h> typedef long long int ll; #define pb push_back #define pi 3.141592653589793238462643383279 #define int long long #define lld long double #define ff first #define ss second #define endl '\n' #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=(int)x; i<y; i++) #define VI vector <int> #define VVI vector <VI> #define pii pair<int, int> #define ppi pair<pii, int> #define mii map<int, int> #define mci map<char, int> #define miv map<int, VI> #define mis map<int, set<int>> #define high_functioning_sociopath ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; const unsigned int M = 998244353; /*_____________________________THE GAME IS ON_____________________________*/ void solve() { string s; cin >> s; int n = s.size(); deque<char>dq; bool off = 0; for(int i=0;i<n;i++) { if(s[i]=='R') { off^=1; continue; } if(off==0)dq.push_front(s[i]); else dq.push_back(s[i]); } stack<int>st; while(!dq.empty()) { if(off) { char x = dq.back(); if(st.empty())st.push(x); else { if(st.top()==x)st.pop(); else st.push(x); }dq.pop_back(); } else { char x = dq.front(); if(st.empty())st.push(x); else { if(st.top()==x)st.pop(); else st.push(x); }dq.pop_front(); } } vector<char>v; while(!st.empty()) { v.pb(st.top()); st.pop(); } for(auto x : v) cout << x; } int32_t main() { high_functioning_sociopath //pre(); int t=1; //cin>>t; while(t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; char s[200100]; ll N, pw[17][200100], a[200100], C[17][17]; const ll mod = 1e9+7; ll D[20][20][200100], D2[20][20][200100]; ll f(ll n){ ll i, j, cnt = 0, ret = 0; set <ll> S; if(n == 0) return -1; for(i=1; i<=N; i++) { for(j=0; j<a[i]; j++) { if(i == 1 && j == 0) continue; ll now, flag = 0; if(S.find(j) == S.end()) { cnt++; flag = 1; } // now = n-cnt; // ret -= pw[cnt][N-i] * (C[16-cnt][now] - 1); ret %= mod; // ret += pw[n][N-i] * C[16-cnt][now]; ret %= mod; // ret += mod; ret %= mod; ret += D[cnt][n][N-i]; ret %= mod; if(flag) cnt--; } if(S.find(a[i]) == S.end()) { cnt++; S.insert(a[i]); if(cnt > n) break; } } if(i == N+1) ret++; for(i=2; i<=N; i++) { ret += D2[0][n][N-i+1]; } ret--; ret %= mod; return ret; } int main(){ //ios_base::sync_with_stdio(false); cin.tie(0); ll i, j, k, K; scanf("%s", s+1); cin>>K; N = strlen(s+1); for(i=0; i<=16; i++) pw[i][0] = 1; for(i=1; i<=N; i++) for(j=0; j<=16; j++) { pw[j][i] = pw[j][i-1] * j; pw[j][i] %= mod; } for(i=1; i<=16; i++) { for(j=0; j<=i; j++) { if(j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i-1][j-1] + C[i-1][j]; } } for(k=0; k<=N; k++) { for(i=0; i<=16; i++) { for(j=i; j<=16; j++) { if(k == 0){ D[i][j][k] = D2[i][j][k] = 1; continue; } D[i][j][k] = i * D[i][j][k-1] + (16-i) * D[i+1][j][k-1]; D[i][j][k] %= mod; if(i == 0) D2[i][j][k] = i * D2[i][j][k-1] + (15-i) * D2[i+1][j][k-1]; else D2[i][j][k] = i * D2[i][j][k-1] + (16-i) * D2[i+1][j][k-1]; D2[i][j][k] %= mod; } } } for(i=1; i<=N; i++) { if(s[i] >= 'A' && s[i] <= 'F') a[i] = s[i] - 'A' + 10; else a[i] = s[i] - '0'; } cout<< ( ( f(K) - f(K-1) ) % mod + mod ) % mod; }
#include<bits/stdc++.h> using namespace std; using ll=long long; static const ll mod=1000000007; string S; ll K; ll comb[17][17]; ll Pow[17][250001]; void init(){ for(ll n=0;n<17;n++)comb[n][0]=1; for(ll n=1;n<17;n++) for(ll k=1;k<=n;k++) comb[n][k]=(n*comb[n-1][k-1])/k; for(ll i=0;i<17;i++)Pow[i][0]=1; for(ll k=1;k<250001;k++) for(ll n=1;n<17;n++){ Pow[n][k]=(Pow[n][k-1]*n)%mod; } } ll Combb(ll y,ll N){ ll sum=0; for(ll i=0;i<K;i++){ ll x=(Pow[K-i][N]*comb[y][i])%mod; if(i%2==0)sum=(sum+x)%mod; else sum=(sum-x+mod)%mod; } return sum; } int main(){ cin>>S>>K;ll N=S.size();init(); ll ans=0; for(ll n=1;n<N;n++){ ans=(ans+Combb(K-1,n-1)*15)%mod; }ans=(ans*comb[15][K-1])%mod; set<ll>SS; for(ll i=0;i<N;i++){ ll a=S[i]-'0'; if(!(0<=a&&a<=9)) a=S[i]-'A'+10; ll Y=SS.size(); for(ll j=0;j<a;j++){ if(i==0 && j==0)continue; ll y=Y;if(!SS.count(j))y++; if(K<y)continue; y=K-y; ll x=Combb(y,N-1-i); ans=(ans+comb[16-K+y][y]*x)%mod; } SS.insert(a); }if(SS.size()==K)ans++; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; void Main() { int n; string s; cin >> n >> s; int i = 0; while (i < s.size()){ if (s.substr(i, 3) == "fox") { s = s.substr(0, i) + s.substr(i+3); i = max(i-3,-1); } i += 1; } cout << s.size() << endl; } int main(int argc, char **argv) { ios::sync_with_stdio(false); std::cin.tie(nullptr); Main(); return 0; }
#include<bits/stdc++.h> using namespace std; char s[200005]; int len,ans,l[200005],f[200005]; int main(){ cin>>len>>(s+1); if(len<3){ printf("%d\n",len); return 0; } for(int i=1;i<=len;i++){ l[i]=i-1; } for(int i=3;i<=len;i++){ if(s[i]=='x'&&s[l[i]]=='o'&&s[l[l[i]]]=='f'){ ans+=3; l[i+1]=l[l[l[i]]]; } } cout<<len-ans; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<char> vc; typedef queue<ll> ql; typedef deque<ll> dql; typedef priority_queue<ll> pql; typedef set<ll> sl; typedef pair<ll, ll> pl; typedef vector<vl> vvl; typedef vector<pl> vpl; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++) #define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--) #define all(v) (v).begin(), (v).end() ll mod(ll a, ll b) {if(b == 0) return 0; return (a % b + b) % b;} bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;} bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;} const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const ll MAX = 1e3; const ld eps = 1e-9; const char newl = '\n'; vl parent(MAX), depth(MAX); ll find(ll x) {if(parent[x]==x) return x; return parent[x] = find(parent[x]);} void unite(ll x, ll y) { x = find(x); y = find(y); if (x==y) return; if(depth[x] < depth[y]) { parent[x] = y; }else { parent[y] = x; if(depth[x] == depth[y]) depth[x]++; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); ll n; cin >> n; vl x(n), y(n); rep(i, n) cin >> x[i] >> y[i]; ll ok=0, ng=40001; while(ng-ok>1) { ll RR = (ok+ng)/2; rep(i, n) {parent[i] = i; depth[i] = 0;} rep2(i, 200-99, 200+99) {parent[i] = 200; depth[i] = 0;} rep2(i, 400-99, 400+99) {parent[i] = 400; depth[i] = 0;} rep(i, n) { if((100+y[i])*(100+y[i]) < RR) unite(i, x[i]+200); if((100-y[i])*(100-y[i]) < RR) unite(i, x[i]+400); } rep(i, n-1) rep2(j, i+1, n-1) { ll dx = x[j]-x[i], dy = y[j]-y[i]; if(dx*dx+dy*dy < RR) unite(i, j); } if(find(200)==find(400)) ng = (ok+ng)/2; else ok = (ok+ng)/2; } cout << sqrt(ok)/2 << newl; return 0; }
#include<bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define all(v) (v).begin(),(v).end() using ll=long long; using ld=long double; using pii=pair<int, int>; using vi=vector<int>; using vii=vector<vector<int>>; const ll LINF=1LL<<60; const int INF=1<<29; const ll MOD=1e9+7; 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;} struct Union_Find{ vector<int> par, depth; Union_Find(int n){init(n);} void init(int n){ par.resize(n); depth.resize(n); for(int i=0; i<n; i++){ par[i]=i; depth[i]=0; } } int find(int x){ if(par[x] == x) return x; return par[x]=find(par[x]); } void unite(int x, int y){ x=find(x), y=find(y); if(x == y) return; if(depth[x] < depth[y]) par[x]=y; else{ par[y]=x; if(depth[x] == depth[y]) depth[x]++; } } bool same(int x, int y){ return find(x) == find(y); } }; int main(){ int n; cin >> n; vi x(n), y(n); for(int i=0; i<n; i++){ cin >> x[i] >> y[i]; } vector<vector<double>> dist(n+2, vector<double>(n+2)); vector<double> d; for(int i=0; i<n; i++){ for(int j=i+1; j<n; j++){ int dx=x[i]-x[j], dy=y[i]-y[j]; double val=sqrt(dx*dx+dy*dy); dist[i][j]=val; d.push_back(val); } } for(int i=0; i<n; i++){ dist[i][n]=100-y[i]; dist[i][n+1]=y[i]+100; d.push_back(100-y[i]); d.push_back(y[i]+100); } sort(all(d)); for(auto ans : d){ Union_Find uf(n+2); for(int i=0; i<n; i++){ for(int j=i+1; j<n+2; j++){ if(dist[i][j] <= ans) uf.unite(i, j); } } if(uf.same(n, n+1)){ cout << fixed << setprecision(5); cout << ans/2 << endl; break; } } return 0; }
#include<bits/stdc++.h> using namespace std; #define rapid() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define endl '\n' #define inf 1e36 #define pb push_back #define mp make_pair #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); typedef long long ll; const ll dp=1e9+7; int main(){ rapid(); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ll n,cnt=0; cin>>n; vector<ll>vec(n); map<ll,ll>m; for(int i=0;i<n;i++){ cin>>vec[i]; m[vec[i]%200]++; } for(auto u:m){ cnt+=(u.second*(u.second-1))/2; } cout<<cnt<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);i++) #define REP1(i,a,b) for(int i=a;i<=(int)b;i++) #define ll long long int #define ld long double #define nl endl #define s(a) scanf("%d",&a) #define p(a) printf("%d",a); #define pb push_back const double PI=acos(-1.0); int main() { #ifndef ONLINE_JUDGE freopen("F:/bla bla bal/sublime/input.txt","r",stdin); freopen("F:/bla bla bal/sublime/output.txt","w",stdout); #endif int n;cin>>n; int a[1001]; int s=0; REP(i,n){ cin>>a[i]; if(a[i]>10)s+=(a[i]-10); } cout<<s<<nl; return 0; }
#include <bits/stdc++.h> #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef long long LL; template <class T> void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar(); x *= f; } template <class T, class... Args> void read(T& x, Args&... args) { read(x), read(args...); } template <class T> void write(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int MAXN = 2E5 + 5; const int MOD = 1E9 + 7; int n, m, q, a[MAXN], b[MAXN]; struct Node { int lson, rson, cnt; LL sum; Node() { cnt = sum = lson = rson = 0; } } tr[MAXN * 80]; int roota, rootb; int newNode() { static int cntNode = 1; return cntNode++; } void update(int cur) { tr[cur].cnt = tr[tr[cur].lson].cnt + tr[tr[cur].rson].cnt; tr[cur].sum = tr[tr[cur].lson].sum + tr[tr[cur].rson].sum; } void modify(int cur, int L, int R, int x, bool tag) { if (x == L && x == R) { if (tag == 0) { tr[cur].sum -= x; tr[cur].cnt--; } else { tr[cur].sum += x; tr[cur].cnt++; } return; } int mid = L + R >> 1; if (!tr[cur].lson) tr[cur].lson = newNode(); if (!tr[cur].rson) tr[cur].rson = newNode(); if (x <= mid) modify(tr[cur].lson, L, mid, x, tag); else modify(tr[cur].rson, mid + 1, R, x, tag); update(cur); } LL qsum(int cur, int L, int R, int l, int r) { if (!cur) return 0; if (l == L && r == R) return tr[cur].sum; int mid = L + R >> 1; if (r <= mid) return qsum(tr[cur].lson, L, mid, l, r); else if (l > mid) return qsum(tr[cur].rson, mid + 1, R, l, r); else return qsum(tr[cur].lson, L, mid, l, mid) + qsum(tr[cur].rson, mid + 1, R, mid + 1, r); } int qcnt(int cur, int L, int R, int l, int r) { if (!cur) return 0; if (l == L && r == R) return tr[cur].cnt; int mid = L + R >> 1; if (r <= mid) return qcnt(tr[cur].lson, L, mid, l, r); else if (l > mid) return qcnt(tr[cur].rson, mid + 1, R, l, r); else return qcnt(tr[cur].lson, L, mid, l, mid) + qcnt(tr[cur].rson, mid + 1, R, mid + 1, r); } signed main() { // freopen("1.in", "r", stdin); read(n, m, q); roota = newNode(); rootb = newNode(); LL ans = 0; for (int i = 1; i <= q; i++) { int t, x, y; read(t, x, y); if (t == 1) { int pre = a[x]; a[x] = y; LL presum = qsum(rootb, 0, 1E8, pre, 1E8) + 1ll * (m - qcnt(rootb, 0, 1E8, pre, 1E8)) * pre; LL cursum = qsum(rootb, 0, 1E8, y, 1E8) + 1ll * (m - qcnt(rootb, 0, 1E8, y, 1E8)) * y; modify(roota, 0, 1E8, y, 1); modify(roota, 0, 1E8, pre, 0); ans = ans - presum + cursum; } else { int pre = b[x]; b[x] = y; LL presum = qsum(roota, 0, 1E8, pre, 1E8) + 1ll * (n - qcnt(roota, 0, 1E8, pre, 1E8)) * pre; LL cursum = qsum(roota, 0, 1E8, y, 1E8) + 1ll * (n - qcnt(roota, 0, 1E8, y, 1E8)) * y; modify(rootb, 0, 1E8, y, 1); modify(rootb, 0, 1E8, pre, 0); ans = ans - presum + cursum; } printf("%lld\n", ans); } return 0; }
#include<iostream> #include<vector> #include<map> using namespace std; int main(){ int a, b; cin >> a >> b; long long k; cin >> k; vector<vector<long long>> s(a+1, vector<long long>(b+1)); for(int i = a; i >= 0; i--){ for(int j = b; j >= 0; j--){ if(i==a || j==b) s[i][j] = 1; else{ s[i][j] = s[i+1][j] + s[i][j+1]; } } } string ans; long long min = 0; int i = 0, j = 0; for(int p = 0; p < a+b; p++){ if(i==a){ ans += 'b'; continue; } if(j==b){ ans += 'a'; continue; } if(k <= min + s[i+1][j]){ ans += 'a'; i++; } else{ ans += 'b'; min += s[i+1][j]; j++; } //cout << min << endl; } cout << ans << endl; /*for(int x = 0; x <= a; x++){ for(int y = 0; y <= b; y++){ cout << s[x][y] << " ";} cout << endl; }*/ }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define abs(x)(x<0 ? -x : x) int main(){ int x,y,a,b; cin>>x>>y>>b>>a; int t; if(2*b<a){ a=2*b; } if(y>x){ cout<<(a*(y-x))+b; }else if(x-y<2){ cout<<b; }else{ cout<<(a*(x-y-1))+b; } }
#include<bits/stdc++.h> using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; 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==(int)v.size()?"":" "); return os; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } template<typename T> vector<T> dijkstra(int s,vector<vector<pair<int,T>>> &G){ const T TINF=numeric_limits<T>::max(); using P=pair<T,int>; int n=G.size(); vector<T> d(n,TINF); vector<int> b(n,-1);//previous point priority_queue<P,vector<P>,greater<P>> q; d[s]=0; q.emplace(d[s],s); while(!q.empty()){ P p=q.top();q.pop(); int v=p.second; if(d[v]<p.first) continue; for(auto& e:G[v]){ int u=e.first; T c=e.second; if(d[u]>d[v]+c){ d[u]=d[v]+c; b[u]=v; q.emplace(d[u],u); } } } return d; } signed main(){ int a,b,x,y;cin>>a>>b>>x>>y;a--,b--; int n=100; vector<vector<pair<int,ll>>> g(2*n); rep(i,n){ g[i].emplace_back(i+n,x); g[i+n].emplace_back(i,x); if(i+1<n){ g[i+1].emplace_back(i+n,x); g[i+n].emplace_back(i+1,x); g[i].emplace_back(i+1,y); g[i+1].emplace_back(i,y); g[i+n].emplace_back(i+1+n,y); g[i+1+n].emplace_back(i+n,y); } } auto d=dijkstra(a,g); cout<<d[b+n]<<endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) typedef long long ll; typedef string str; typedef double db; typedef long double ld; typedef pair<int, int> pi; #define fi first #define se second #define pb push_back #define mp make_pair #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define endl "\n" #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) const ll MOD = 1e9 + 7; //998244353 const ll INF = 1e18; const ll MX = 1e9 + 10; const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up #ifndef LOCAL #define cerr if(false) cerr #endif #define debug(x) cerr << #x << " : " << x << endl; #define debugs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl; #define debugv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl; #define here() cerr << "here" << endl; void IO() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } ll solve(ll n) { return n*(n-1)/2; } int main() { boost; IO(); ll n; cin >> n; vector<ll> arr(n); FOR(i, 0, n) { cin >> arr[i]; arr[i]%=200; } vector<ll> vis(200, 0); vector<ll> res; FOR(i, 0, n) { vis[arr[i]]++; } FOR(i, 0, 200) { if(vis[i]>0) res.pb(vis[i]); } ll sum = 0; ll len = res.size(); FOR(i, 0, len) { ll temp = res[i]; sum+=solve(temp); } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") using ll = long long; using ld = long double; #define mp make_pair int p = 1e9 + 7; int mul(int a, int b) { return (1LL * a * b) % p; } int add(int a, int b) { int s = (a+b); if (s>=p) s-=p; return s; } int sub(int a, int b) { int s = (a+p-b); if (s>=p) s-=p; return s; } int po(int a, int deg) { if (deg==0) return 1; if (deg%2==1) return mul(a, po(a, deg-1)); int t = po(a, deg/2); return mul(t, t); } int inv(int n) { return po(n, p-2); } mt19937 rnd(time(0)); const int N = 1000005; vector<int> facs(N), invfacs(N); void init() { facs[0] = 1; for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i); invfacs[N-1] = inv(facs[N-1]); for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1); } int C(int n, int k) { if (n<k) return 0; if (n<0 || k<0) return 0; return mul(facs[n], mul(invfacs[k], invfacs[n-k])); } /*struct DSU { vector<int> sz; vector<int> parent; void make_set(int v) { parent[v] = v; sz[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; } } DSU (int n) { parent.resize(n); sz.resize(n); for (int i = 0; i<n; i++) make_set(i); } };*/ int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n; cin>>n; string s; cin>>s; vector<char> a(2e5); int cur = 0; for (auto c: s) { a[cur] = c; if (cur>=2 && a[cur-2]=='f' && a[cur-1]=='o' && a[cur]=='x') cur-=3; cur++; } cout<<cur; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> as(n), bs(n), ps(n); for (int i = 0; i < n; i++) { cin >> as[i]; } for (int i = 0; i < n; i++) { cin >> bs[i]; } for (int i = 0; i < n; i++) { cin >> ps[i]; ps[i]--; } for (int i = 0; i < n; i++) { if (i != ps[i] && as[i] <= bs[ps[i]]) { cout << -1 << endl; return 0; } } vector<int> used(n); vector<tuple<int, int>> outputs; for (int i = 0; i < n; i++) { if (i == ps[i]) { continue; } if (used[i]) { continue; } vector<int> cycle; int current = i; while (true) { if (used[current]) break; used[current] = true; cycle.push_back(current); current = ps[current]; } reverse(cycle.begin(), cycle.end()); int min_b = INT_MAX; int min_b_arg = -1; for (int j = 0; j < cycle.size(); j++) { if (bs[ps[cycle[j]]] < min_b) { min_b = bs[ps[cycle[j]]]; min_b_arg = j; } } for (int j = 0; j + 1 < cycle.size(); j++) { int k1 = cycle[(j + min_b_arg) % cycle.size()]; int k2 = cycle[(j + min_b_arg + 1) % cycle.size()]; outputs.push_back({k1, k2}); } } cout << outputs.size() << endl; for (const auto [k1, k2] : outputs) { cout << k1 + 1 << " " << k2 + 1 << "\n"; } }
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N; cin >> N; vector<int> A(N), B(N), P(N); REP(i, N) scanf("%d", &A[i]); REP(i, N) scanf("%d", &B[i]); REP(i, N) scanf("%d", &P[i]), --P[i]; vector<pii> ans; auto P2 = P; vector<bool> done(N, false); vector<int> prv(N, -1); REP(i, N) { if (done[i]) continue; if (P[i] == i) continue; vector<int> g; g.emplace_back(i); int pos = P[i]; while (pos != i) { g.emplace_back(pos); pos = P[pos]; } for (int p : g) { if (A[p] <= B[P[p]]) { puts("-1"); return 0; } prv[P[p]] = p; } set<int> ss; for (int p : g) if (A[p] > B[P[P[p]]]) ss.insert(p); int cnt = 0; while (!ss.empty()) { int p = *ss.begin(); ss.erase(p); if (p == P[p]) continue; if (A[p] <= B[P[p]] || A[P[p]] <= B[P[P[p]]]) continue; ans.emplace_back(p, P[p]); ss.erase(P[p]); ss.erase(prv[p]); int tmp = P[p]; swap(P[p], P[tmp]); if (P[p] != p) if (A[p] > B[P[P[p]]]) ss.insert(p); prv[P[p]] = p; p = prv[p]; if (P[p] != p) if (A[p] > B[P[P[p]]]) ss.insert(p); ++cnt; } if (cnt + 1 != g.size()) { assert(false); // こないはず } for (int j : g) done[j] = true; } { bool ok = true; P = P2; for (auto [i, j] : ans) { // assert(A[i] > B[P[i]]); // assert(A[j] > B[P[j]]); swap(P[i], P[j]); } // REP(i, N) assert(i == P[i]); } cout << ans.size() << endl; for (auto [i, j] : ans) printf("%d %d\n", i + 1, j + 1); }
#include <cstdio> #include <random> #include <ctime> using namespace std; std::mt19937 rnd(time(0)); long long gcd(long long a,long long b) { if(b==0) return a; return gcd(b,a%b); } bool f; int ans[135]; void solve(long long x,long long y,int &now) { if(now>130) { f=false; return; } if(x==1&&y==0) { ans[now]=1; return; } if(x==0&&y==1) { ans[now]=2; return; } if(x>=y) { ans[now]=3; now++; solve(x-y,y,now); } else { ans[now]=4; now++; solve(x,y-x,now); } } int main() { long long n,x,y; int now; scanf("%lld",&n); for(int i=0;i<1000000;i++) { x=n; y=1LL*rnd()*rnd()%n; if(gcd(x,y)!=1) { i--; continue; } f=true; now=1; solve(x,y,now); if(f) { printf("%d\n",now); for(int j=now;j>=1;j--) printf("%d\n",ans[j]); break; } } return 0; }
#include <iostream> #include <queue> #include <map> #include <cmath> #include <algorithm> #include <queue> #include <cstdarg> #include <set> #include <limits> #include <iomanip> using namespace std; #define INF 2e9 + 9 #define MOD 1000000007 #define ALL(v) v.begin() , v.end() #define Int int64_t #define pll pair<Int,Int> template<class T> using pql = priority_queue<T, vector<T>>; template<class T> using pq = priority_queue<T, vector<T>, greater<T>>; #define ll(n) Int n; cin >> n; #define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___; #define OUT(...) cout << __VA_ARGS___; #define RP(n, i, s) for(Int i = s ;i < n ;i++) #define REP(n, i) for (Int i = 0; i < n; i++) #define REPe(n, i) for (Int i = 1; i <= n; i++) #define RPe(n, i, s) for (Int i = s; i <= n; i++) #define upd_max(maxer, updater) if (maxer < updater) maxer = updater; #define upd_min(miner, updater) if (miner > updater) miner = updater; #ifdef DEBUG #else # pragma GCC optimize("O3,no-stack-protector") # pragma GCC optimize("unroll-loops") # if __cplusplus < 201703L # pragma GCC target("avx") # else # pragma GCC target("avx2") # endif #endif // 実数の場合 // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") void ansYes(bool flag){ if (flag) cout << "Yes" << endl; else cout << "No" << endl; } void ansYES(bool flag){ if (flag) cout << "YES" << endl; else cout << "NO" << endl; } struct node { Int to; Int length; }; Int gcd(Int a, Int b) { if (a%b == 0) { return(b); } else { return(gcd(b, a%b)); } } vector<Int> divisor(Int n) { vector<Int> ret; for (Int i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } return (ret); } Int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1}; Int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; //UnionFind群 /* class UnionFind { protected: vector<Int> par; vector<Int> data; Int cnt; public: UnionFind() : par(1), data(1), cnt(1) {}; UnionFind(Int N); Int root(Int x); Int marge(Int x, Int y); bool same(Int x, Int y); Int add(); Int add(Int x); Int solve(); inline Int getSize(Int x) { return data.at(root(x)); }; }; UnionFind::UnionFind(Int N) : par(N), data(N), cnt(N) { for (Int i = 0; i < N; i++) { par.at(i) = -1; data.at(i) = 1; } } Int UnionFind::root(Int x) { if (par.at(x) == -1) return x; return par.at(x) = root(par.at(x)); // 経路圧縮 } Int UnionFind::marge(Int x, Int y) { Int rx = root(x); Int ry = root(y); if (rx == ry) return rx; else { // ノード数の少ない方に接続 if (data.at(rx) < data.at(ry)) swap(rx, ry); par.at(ry) = rx; data.at(rx) += data.at(ry); return rx; } return rx; } bool UnionFind::same(Int x, Int y) { return root(x) == root(y); } Int UnionFind::add() { par.push_back(cnt); cnt++; return cnt - 1; } Int UnionFind::add(Int x) { par.push_back(cnt); cnt++; return marge(x, cnt); } Int UnionFind::solve() { for (Int i = 0; i < cnt;i++) { if (par.at(i) == -1) { } } }*/ /* Int dfs(Int par, Int maxs, bool ans) { if (mita.at(par) == 1) return 0; if (maxs > h.at(par)) { maxs = h.at(par); ans = true; }else if (maxs == h.at(par)){ ans = false; } Int maxer = 0; for (auto z : to[par]){ mita.at(par) = 1; maxer = dfs(z,maxs,ans); } }*/ int main() { Int n , a, b; cin >> n >> a >> b; cout << n - a + b << endl; }
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; for(int i=s.size()-1;i>=0;i--) { if(s[i]=='1' || s[i]=='0' || s[i]=='8') cout<<s[i]; if(s[i]=='6') cout<<'9'; if(s[i]=='9') cout<<'6'; } return 0; }
#include<bits/stdc++.h> #define ll long long int #define f first #define s second #define pb(x) push_back(x) #define inf (int)1e9 #define PI 3.1415926535897932384626433832795 #define mod 1000000007 #define imax INT_MAX #define imin INT_MIN using namespace std; char c[5000000]; void input() { string s; cin>>s; string ans; int r=0; int x = s.size(); int l = 500000,r1=500001; for(int i=0;i<x;i++) { if(s[i] == 'R') { r++; continue; } if(r%2) { c[l] = s[i]; l--; } else { c[r1] = s[i]; r1++; } } string a1; for(int i=l+1;i<r1;i++) { a1 += c[i]; } if(r%2) { reverse(a1.begin(), a1.end()); } ans = a1; // cout<<a1<<"\n"; // cout<<ans<<"\n"; stack<char> st; x = ans.size(); for(int i=0;i<x;i++) { if(!st.empty() && st.top() == ans[i]) { st.pop(); } else { st.push(ans[i]); } } string a; while(!st.empty()) { a += st.top(); st.pop(); } reverse(a.begin(), a.end()); cout<<a<<"\n"; } 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); input(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define mem(x) memset(x,0,sizeof(x)) const ll inf=1000000000000000000; const long double ep=2.6180339887498948482045868343656; inline int read() { char C=getchar(); int F=1,ANS=0; while (C<'0'||C>'9') { if (C=='-') F=-1; C=getchar(); } while (C>='0'&&C<='9') { ANS=ANS*10+C-'0'; C=getchar(); } return F*ANS; } ll n; ll fib[200],cnt; ll ans[200],sum; int main() { cin >> n; fib[1]=fib[2]=1; for (int i=3;i<=1000;i++) { fib[i]=fib[i-1]+fib[i-2]; if (fib[i]>n) { cnt=i-1; break; } } int tot=1; for (int i=cnt;i>=2;i--) { ll p=n/fib[i]; ans[tot]=p; sum+=p; n-=p*fib[i]; tot++; } cout << sum+cnt-1 << endl; ll x=0,y=0; for (int i=1;i<=cnt;i++) { if (i!=1) { if ((i+cnt)%2==0) puts("3"); else puts("4"); if (((i+cnt)%2==0)) x=x+y; else y=y+x; } for (int j=1;j<=ans[i];j++) { if ((i+cnt)%2==0) puts("1"); else puts("2"); if (((i+cnt)%2==0)) x=x+1; else y=y+1; } } return 0; } //679891637638612258 //381966011250105144 618033988749894856
#include <bits/stdc++.h> // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18; void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);} void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);} void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);} void ioinit() { cout << fixed << setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); } // clang-format on ll fibrest(ll x0, ll y0, ll n, bool isx) { ll iter = 0; vector<ll> fib = {x0, y0}; ll x = x0; ll y = y0; ll op = 0; while (true) { if (isx) { x = x + y; } else { y = y + x; } if (x >= n) break; op++; isx = !isx; } return op; } void op(vector<ll> &ops, ll &x, ll &y, ll t) { if (t == 1) { x = x + 1; } if (t == 2) { y = y + 1; } if (t == 3) { x = x + y; } if (t == 4) { y = y + x; } ops.push_back(t); } void output(vector<ll> &ops) { ll k = ops.size(); print(ops.size()); for (auto ope : ops) { print(ope); } } int main() { ioinit(); ll n; cin >> n; ll x = 0; ll y = 0; vector<ll> ops; op(ops, x, y, 1); bool isx = true; while (true) { if (x + y > n || n < 10) { while (x < n) { op(ops, x, y, 1); } break; } ll curop = fibrest(x, y, n, isx); while (true) { ll nx = isx ? x+1 : x; ll ny = isx ? y : y+1; if (fibrest(nx, ny, n, isx) != curop) break; if(isx){ op(ops, x, y, 1); }else{ op(ops, x, y, 2); } } if (isx) { op(ops, x, y, 3); } else { op(ops, x, y, 4); } isx = !isx; } output(ops); }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define endl "\n" typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;} template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;} void solve() { string S; cin >> S; int N = S.size(); const int INF = 1 << 29; vector<vector<int>> dp(N + 1, vector<int>(3, INF)); dp[0][0] = 0; for(int i = 0; i < N; i++) { for(int j = 0; j < 3; j++) { int nj = (j + S[i] - '0') % 3; dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j]); dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1); } } if (dp[N][0] == N) { cout << -1 << endl; } else { cout << dp[N][0] << endl; } } int main() { #ifdef LOCAL_ENV cin.exceptions(ios::failbit); #endif cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }
#include "bits/stdc++.h" #define int long long #define endl '\n' using namespace std; typedef long long ll; typedef long double ld; #define db(x) cerr << #x << ": " << x << '\n'; #define read(a) int a; cin >> a; #define reads(s) string s; cin >> s; #define readb(a, b) int a, b; cin >> a >> b; #define readc(a, b, c) int a, b, c; cin >> a >> b >> c; #define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];} #define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];} #define print(a) cout << a << endl; #define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl; #define printv(v) for (int i: v) cout << i << " "; cout << endl; #define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;} #define all(v) v.begin(), v.end() #define sz(v) (int)(v.size()) #define rz(v, n) v.resize((n) + 1); #define pb push_back #define fi first #define se second #define vi vector <int> #define pi pair <int, int> #define vpi vector <pi> #define vvi vector <vi> #define setprec cout << fixed << showpoint << setprecision(20); #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) const ll inf = 1e18; const ll mod = 998244353; const ll N = 2e5 + 1; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int power (int a, int b = mod - 2) { int res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); readb(n, m); int one[n + 1][m + 1] = {}, zer[n + 1][m + 1] = {}; int onesum[n + 1][m + 1] = {}, zersum[n + 1][m + 1] = {}; int precalc[m + 1][n + 1] = {}; FOR (i, 1, m) precalc[i][0] = 1; FOR (i, 1, m) FOR (j, 1, n) precalc[i][j] = i*precalc[i][j - 1] % mod; FOR (i, 1, n) FOR (j, 1, m) { one[i][j] = (one[i][j - 1] + onesum[i - 1][j - 1] + zersum[i - 1][j - 1] + precalc[j][i] - precalc[j - 1][i]) % mod; zer[i][j] = (one[i][j] + onesum[i - 1][j] + zersum[i - 1][j]) % mod; onesum[i][j] = (onesum[i - 1][j]*(j + 1) + one[i][j]) % mod; zersum[i][j] = (zersum[i - 1][j]*j + zer[i][j]) % mod; } print((one[n][m] + 5*mod) % mod); }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++) #define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define vector1d(type,name,...) vector<type>name(__VA_ARGS__) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__))) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=(ll)1<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} int main(){ LL(n,t); vector<ll> a(n),suma(1,0),sumb(1,0); in(a); int pre; for(int i=0;i<n/2;i++){ pre=suma.size(); for(int j=0;j<pre;j++){ if(suma[j]+a[i]<=t)suma.emplace_back(suma[j]+a[i]); } } for(int i=n/2;i<n;i++){ pre=sumb.size(); for(int j=0;j<pre;j++){ if(sumb[j]+a[i]<=t)sumb.emplace_back(sumb[j]+a[i]); } } sort(all(suma)); sort(all(sumb)); ll ans=0,d,bt,bi=sumb.size()-1; for(int i=0;i<suma.size();i++){ bt=t-suma[i]; while(sumb[bi]>bt)bi--; chmax(ans,suma[i]+sumb[bi]); } out(ans); }
//g++ 7.4.0 #include <iostream> #include <bits/stdc++.h> using namespace std; typedef long long ll; #define MAXN 200005 vector<ll> adj[MAXN]; ll vis[MAXN]; ll dp[MAXN]; ll A[MAXN]; void DFS(ll s) { vis[s] = 1; for(auto it: adj[s]) { ll v = it; if(!vis[v]) DFS(v); ll val = max(A[v],dp[v]); dp[s] = max(dp[s],val); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll N,M; cin>>N>>M; for(ll i=0;i<N;++i) cin>>A[i + 1]; for(ll i=0;i<M;++i) { ll u,v; cin>>u>>v; adj[u].push_back(v); } for(ll i=1;i<=N;++i) { if(!vis[i]) DFS(i); } ll ans = LLONG_MIN; for(ll i=1;i<=N;++i) { if(adj[i].size()) //Not a leaf { ans = max(ans,dp[i] - A[i]); } } cout<<ans<<endl; }
#include<bits/stdc++.h> #define int long long using namespace std; const int mod=1000000007; int quickpower(int a,int b){ int ans=1; a%=mod; while(b){ if(b&1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans%mod; } int C(int n,int m){ if(n<m) return 0; if(m*2>n) m=n-m; if(m==0) return 1; int up=1,down=1; for(int i=1;i<=m;i++){ up=(up*(n-i+1))%mod; down=(down*i)%mod; } return up*quickpower(down,mod-2)%mod; } signed main(){ int n,m,s=0; cin>>n>>m; for(int i=1;i<=n;i++){ int k; cin>>k; s+=k; } cout<<C(m+n,s+n); return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int N; int M; int L[200000]; int R[200000]; int main(){ int i; int fg = 0; rd(N); rd(M); if(M==0){ int i; for(i=(0);i<(N);i++){ wt_L(2*i+1); wt_L(' '); wt_L(2*i+2); wt_L('\n'); } return 0; } if(M < 0){ fg = 1; M *= -1; } if(M > N-2 || fg==1){ wt_L(-1); wt_L('\n'); return 0; } for(i=(0);i<(N);i++){ { auto cTE1_r3A = (200000000+2*i); auto RZTsC2BF = ( 200000000+2*i+1); L[i] = cTE1_r3A; R[i] = RZTsC2BF; } } { auto FmcKpFmN = (1); auto xr20shxY = ( 100000000); L[0] = FmcKpFmN; R[0] = xr20shxY; } { auto WYIGIcGE = (2); auto t_ynMSdg = ( 3); L[1] = WYIGIcGE; R[1] = t_ynMSdg; } for(i=(0);i<(M);i++){ { auto tU__gIr_ = (10+2*i); auto a2conNHc = ( 10+2*i+1); L[2+i] = tU__gIr_; R[2+i] = a2conNHc; } } for(i=(0);i<(N);i++){ wt_L(L[i]); wt_L(' '); wt_L(R[i]); wt_L('\n'); } return 0; } // cLay varsion 20201115-2 // --- original code --- // int N, M, L[2d5], R[2d5]; // { // int fg = 0; // rd(N,M); // // if(M==0){ // rep(i,N) wt(2*i+1, 2*i+2); // return 0; // } // // if(M < 0) fg = 1, M *= -1; // if(M > N-2 || fg==1) wt(-1), return 0; // // rep(i,N) (L[i], R[i]) = (2d8+2*i, 2d8+2*i+1); // (L[0], R[0]) = (1, 1d8); // (L[1], R[1]) = (2, 3); // rep(i,M) (L[2+i], R[2+i]) = (10+2*i, 10+2*i+1); // // rep(i,N) wt(L[i], R[i]); // }
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<cctype> #include<cstdlib> #include<algorithm> #define fs(i,x,y) for(int i=(x),_=(y);i<=_;++i) #define fn(i,x,y) for(int i=(x),_=(y);i>=_;--i) #define tor(i,v,x) for(int i=head[x],v=to[i];i;i=nxt[i],v=to[i]) #define Li inline #define R register int #define ls(x) ch[x][0] #define rs(x) ch[x][1] #define mpi(x,y) make_pair(x,y) #define pi pair<int,int> #define int ll using namespace std; typedef long long ll; template<typename T> void read(T &x){ x=0;char ch=getchar();bool f=0; while(ch<'0'||ch>'9'){ if(ch=='-')f=1; ch=getchar(); } while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar(); x=f?-x:x; } const int N=2e5+7; const double eps=1e-8; int n,m,k; int a[N]; double dp1[N],dp2[N]; double suf1[N],suf2[N]; bool vis[N]; signed main(){ read(n);read(m);read(k); fs(i,1,k)read(a[i]),vis[a[i]]=true; fn(i,n-1,0){ dp1[i]=(suf1[i+1]-suf1[i+m+1])/m+1.0; dp2[i]=(suf2[i+1]-suf2[i+m+1])/m; fs(j,1,k){ if(i+1<=a[j]&&a[j]<=i+m)dp2[i]+=1.0/m; } if(vis[i])dp1[i]=dp2[i]=0; suf1[i]=suf1[i+1]+dp1[i]; suf2[i]=suf2[i+1]+dp2[i]; } if(fabs(1.0-dp2[0])<eps)printf("-1"); else printf("%lf",dp1[0]/(1-dp2[0])); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=1;i<=n;++i) #define mp make_pair #define pb push_back #define size sze #define inf 998244353 int n,k; vector<int> g[210000]; int v[210000],dep[210000]; int q[210000]; int f[210000][20],size[210000],son[210000],minp[210000]; int father[210000],root; struct node { int adj,valid,next; }edge[2*210000]; int gh[210000],top; void addedge(int x, int y) { edge[++top].adj = y; edge[top].valid = 1; edge[top].next = gh[x]; gh[x] = top; } void get_size(int x, int root=0) { size[x] = 1; son[x] = 0; int dd = 0; for (int p=gh[x]; p; p=edge[p].next) if (edge[p].adj != root && edge[p].valid) { get_size(edge[p].adj, x); size[x] += size[edge[p].adj]; if (size[edge[p].adj] > dd) { dd = size[edge[p].adj]; son[x] = edge[p].adj; } } } int getroot(int x) { get_size(x); int sz = size[x]; while (size[son[x]] > sz/2) x = son[x]; return x; } int dc(int x,int fa) { x = getroot(x); father[x]=fa; for (int p=gh[x]; p; p=edge[p].next) if (edge[p].valid) { edge[p].valid = 0; edge[p^1].valid = 0; dc(edge[p].adj,x); } return x; } bool cmp(int x,int y) { return dep[x]>dep[y]; } void dfs(int x,int pre,int depp) { f[x][0]=pre; for(int i=1;i<=17;i++)f[x][i]=f[f[x][i-1]][i-1]; dep[x]=depp; for(int p=gh[x];p;p=edge[p].next) if (edge[p].adj!=pre)dfs(edge[p].adj,x,depp+1); } int jump(int x,int d) { int delta=dep[x]-d; for(int i=17;i>=0;i--) if (delta & (1<<i))x=f[x][i]; return x; } int lca(int x,int y) { if (dep[x]<dep[y])swap(x,y); x=jump(x,dep[y]); // cout<<x<<" "<<y<<endl; if (x==y)return x; for(int i=17;i>=0;i--) if (f[x][i]!=f[y][i]) { x=f[x][i]; y=f[y][i]; } return f[x][0]; } int dis(int x,int y) { return dep[x]+dep[y]-2*dep[lca(x,y)]; } void insert(int x) { int now=x; while (now!=0) { if (minp[now]==n+1 || dis(now,x)<dis(now,minp[now]))minp[now]=x; now=father[now]; } } int get(int x) { int ret=2147483647; int now=x; while (now!=0) { if (minp[now]!=n+1)ret=min(ret,dis(x,minp[now])); now=father[now]; } return ret; } bool check(int lim) { for(int i=1;i<=n;i++)minp[i]=n+1; int tot=0; for(int i=1;i<=n;i++) { //cout<<q[i]<<endl; if (get(q[i])>lim) { int fa=jump(q[i],max(dep[q[i]]-lim,0)); insert(fa); tot++; } if (tot>k)return 0; } return 1; } int main() { cin>>n>>k; top=1; for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); addedge(x,y); addedge(y,x); } dfs(1,0,0); root=dc(1,0); //cout<<lca(3,2)<<" "<<lca(3,5)<<" "<<lca(4,5)<<endl; for(int i=1;i<=n;i++)q[i]=i; sort(q+1,q+n+1,cmp); long long l=1; long long r=n; //for(int i=1;i<=n;i++)cout<<father[i]<<" "; puts(""); while (l<r) { int mid=(l+r)/2; // cout<<mid<<endl; if (check(mid))r=mid; else l=mid+1; } cout<<l<<endl; return 0; }
#include <iostream> // cout, endl, cin #include <string> // string, to_string, stoi #include <vector> // vector #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <utility> // pair, make_pair #include <tuple> // tuple, make_tuple #include <cstdint> // int64_t, int*_t #include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long small = 100; vector<long long> fac, finv, inv; void COMinit(int max) { vector<long long> f(max+1); vector<long long> finv(max+1); vector<long long> inv(max+1); f[0] = 1; finv[0] = 1; inv[0] = 1; for (int i = 1; i <= max; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } vector<long> BINARY; int biFind(long Key,int mode) { //need sort beforehand //mode 0:first > 1:first >= //return -1 if the last is <= or < if (mode == 0) { if (Key >= BINARY[BINARY.size() - 1]) { return -1; } int l, r, m; l = 0; r = BINARY.size(); while (l < r) { m = (l + r) / 2; if (Key >= BINARY[m]) { l = m + 1; } else { r = m; } } return l; } else { if (Key > BINARY[BINARY.size() - 1]) { return -1; } int l, r, m; l = 0; r = BINARY.size(); while (l < r) { m = (l + r) / 2; if (Key > BINARY[m]) { l = m + 1; } else { r = m; } } return l; } } bool biFind(long Key) { int l, r, m; l = 0; r = BINARY.size(); while (l < r) { m = (l + r) / 2; if (Key == BINARY[m]) { break; } if (Key > BINARY[m]) { l = m + 1; } else { r = m; } } return Key == BINARY[l]; } int main() { //cout << fixed << setprecision(16); long N; cin >> N; vector<long> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } sort(A.begin(), A.end()); vector<long> B(1, 1); int pos = 0; for (int i = 1; i < N; i++) { if (A[i] == A[i-1]) { B[pos]++; } else { B.push_back(1); pos++; } } long R = 0; for (int i = 0; i < B.size(); i++) { //cout << B[i] << endl; R += B[i] * (N - B[i]); } cout << R/2 << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define f(i,n) for(ll i=0;i<n;i++) #define fa(i,a,n) for(ll i=a;i<n;i++) #define M 1000000007 #define pb push_back #define vl vector #define sp(x) setprecision(x) void code() { ll n; cin>>n; ll a[n]; f(i,n) cin>>a[i]; ll c=0; unordered_map<int,int> m; f(i,n) { m[a[i]]++; } f(i,n-1) { if(m[a[i]]>1){ c+=(n-1-i)-(m[a[i]]-1); m[a[i]]--; }else if(m[a[i]]==1) c+=(n-1-i); } cout<<c<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; t=1; //cin>>t; while(t--) { code(); } return 0; }
/** made by amunduzbaev **/ #include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define mp make_pair #define ub upper_bound #define lb lower_bound #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(),x.rend() #define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define vv vector #define tut(x) array<int, x> #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vii; typedef vector<pii> vpii; template<class T> bool umin(T& a, const T& b) {return a > b? a = b, true:false;} template<class T> bool umax(T& a, const T& b) {return a < b? a = b, true:false;} void fropen(string s) { freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } const int N = 3e5+5; const int mod = 1e9+7; const ll inf = 1e18; const ld Pi = acos(-1); #define MULTI 0 int n, m, k, res, ans, a[N]; void solve(int t_case){ cin>>n>>k; for(int i=0;i<n;i++) cin>>a[i]; sort(a, a+n); multiset<int> ss; for(int i=0;i<k;i++) ss.insert(0); for(int i=0;i<n;i++){ auto pos = ss.find(a[i]); if(pos != ss.end()){ ss.erase(pos); ss.insert(a[i]+1); } //for(auto x:ss) cout<<x<<" "; //cout<<"\n"; } for(auto x:ss) res += x; cout<<res<<"\n"; } signed main(){ NeedForSpeed if(!MULTI) { solve(1); } else { int t; cin>>t; for(int t_case = 1; t_case <= t; t_case++) solve(t_case); } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,f,n) for(ll i=(f); (i) < (n); i++) #define repe(i,f,n) for(ll i=(f); (i) <= (n); i++) #define repc(i,f,n) for(char i=(f); (i) <= (n); i++) #define PI 3.14159265358979323846264338327950ll #define debug(x) cout<<#x<<" :: "<<x<<"\n"; #define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"; #define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"; #define Pl pair<ll, ll> #define dvec vector<vector<ll>> #define OUT(x) cout << x << endl; return 0; //printf("%.10f\n") //cout << fixed << setprecision(10); template<class T> inline bool chmax(T& a, T b){if (a < b) { a = b; return true; } return false;} template<class T> inline bool chmaxe(T& a, T b){if (a <= b) { a = b; return true; } return false;} template<class T> inline bool chmin(T& a, T b){if (a > b) { a = b; return true; } return false;} const ll MOD = 1000000007ll; const ll INF = 1e+18; const int iINF = 1e9; const double EPS = 1e-8; int main() { ll H, W; cin >> H >> W; vector<string> grd(H); rep(i, 0, H){ cin >> grd[i]; } vector<vector<ll>> dp(H, vector<ll>(W, -INF)); dp[H - 1][W - 1] = 0; for(ll h = H - 1; h >= 0; h -= 1){ for(ll w = W - 1; w >= 0; w -= 1){ if((h + w) % 2 == 0){ ll cmp = -INF; if(h + 1 < H) chmax(cmp, dp[h + 1][w] + (grd[h + 1][w] == '+' ? 1 : -1)); if(w + 1 < W) chmax(cmp, dp[h][w + 1] + (grd[h][w + 1] == '+' ? 1 : -1)); if(cmp != -INF)dp[h][w] = cmp; } else { ll cmp = INF; if(h + 1 < H) chmin(cmp, dp[h + 1][w] + (grd[h + 1][w] == '+' ? -1 : 1)); if(w + 1 < W) chmin(cmp, dp[h][w + 1] + (grd[h][w + 1] == '+' ? -1 : 1)); if(cmp != INF)dp[h][w] = cmp; } } } ll rt = dp[0][0]; if(rt == 0){OUT("Draw")} else if(rt > 0){OUT("Takahashi")} else{OUT("Aoki")} }
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for(int i = 0; i < n; ++i) using namespace std; using LLONG = long long; const LLONG MOD = 1000000007; int main() { int N; cin >> N; vector<vector<int>> adList(N); REP(i, N) { string S; cin >> S; REP(j, N) { if (S[j] == '1') { adList[i].push_back(j); } } } vector<set<int>> ss(N); REP(s, N) { deque<int> Q{ s }; vector<bool> arrived(N, false); arrived[s] = true; while (!Q.empty()) { int t = Q.front(); Q.pop_front(); ss[t].insert(s); for (const int n : adList[t]) { if (arrived[n]) continue; Q.push_back(n); arrived[n] = true; } } } double ans = 0.0; REP(s, N) { const double sv = static_cast<double>(ss[s].size()); ans += 1.0 / sv; } cout << setprecision(12) << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; string dispVec(vector<int> b){ string ret = to_string(b.size()) + " "; rep(i,b.size()) ret += to_string(b[i] + 1) + " "; return ret; } int main(){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; int b = min(n,8); vector<vector<int>> can(200); rep(i,1<<b ){ if(i==0) continue; ll m = 0; vector<int> v; rep(j,b) if(i >> j & 1) { m = m + a[j]; v.push_back(j); } if (can[m%200].size() > 0){ cout << "Yes" << endl; cout << dispVec(can[m%200]) << endl; cout << dispVec(v) << endl; return 0; } else { can[m%200] = v; } } cout << "No" << endl; }
#include <iostream> #include<vector> #include<algorithm> #include<string> #include<map> #include<set> #include<stack> #include<queue> #include<math.h> using namespace std; typedef long long ll; #define int long long #define double long double typedef vector<int> VI; typedef pair<int, int> pii; typedef vector<pii> VP; typedef vector<string> VS; typedef priority_queue<int> PQ; template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i<n;i++) #define eREP(i,n) for(int i=0;i<=n;i++) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define eFOR(i,a,b) for(int i=(a);i<=(b);++i) #define SORT(c) sort((c).begin(),(c).end()) #define rSORT(c) sort((c).rbegin(),(c).rend()) #define LB(x,a) lower_bound((x).begin(),(x).end(),(a)) #define UB(x,a) upper_bound((x).begin(),(x).end(),(a)) #define INF 1000000000 #define LLINF 9223372036854775807 #define mod 1000000007 #define eps 1e-12 //priority_queue<int,vector<int>, greater<int> > q2; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; VP A(N); REP(i, N)cin >> A[i].first >> A[i].second; bool F = 0; REP(i, N) { FOR(j, i + 1, N) { FOR(k, j + 1, N) { int x1 = A[i].first, y1 = A[i].second; int x2 = A[j].first, y2 = A[j].second; int x3 = A[k].first, y3 = A[k].second; int ax = x2 - x1; int ay = y2 - y1; int bx = x3 - x1; int by = y3 - y1; int tmp = abs(ax*by - bx * ay); if (tmp == 0) { F = 1; } } } } if (F)cout << "Yes" << endl; else cout << "No" << endl; return 0; }
//CODED BY SUMIT KUMAR PRAJAPATI #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pl; #define PI 3.1415926535897932384626 #define pb push_back #define mk make_pair #define ff first #define ss second #define watch(x) cerr<<#x<<" = "<<x<<'\n' #define rep(i,n) for(ll i=0;i<n;i++) #define repe(i,n) for(ll i=1;i<=n;i++) #define FOR(i,a,b) for(ll i=a;i<=b;i++) #define printar(a,s,e) FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n' #define curtime chrono::high_resolution_clock::now() #define timedif(start,end) chrono::duration_cast<chrono::nanoseconds>(end - start).count() const ll INF=1e18; const int MX=1e5+5; const int MD=1e9+7; const int MDL=99824453; auto time0 = curtime; string X; ll maxCanGo(ll base){ ll ans=(log2(INF)/log2(base))+1; //cout<<ans<<" ^ \n"; return ans; } ll evaluate(ll base,ll n){ ll res=0; ll cur_power=1; for(ll i=n-1;i>=0;i--){ res+=(cur_power*(X[i]-'0')); cur_power*=base; } return res; } void solve(){ ll M; cin>>X>>M; ll n=X.length(); if(n==1){ ll one=X[0]-'0'; if (one<=M){ cout<<"1"; } else cout<<"0"; return; } char max_digit; for(auto ch:X) if(max_digit<ch) max_digit=ch; ll cur_digit=max_digit-'0'+1; //cout<<cur_digit<<'\n'; ll cur_value; ll ans=0; ll l=cur_digit,r=1e18,mid; while(l<=r){ mid=(l+r)>>1; if(maxCanGo(mid)>=n && evaluate(mid,n)<=M){ //cout<<mid<<" "<<ans<<" "<<evaluate(mid,n)<<'\n'; ans=mid; l=mid+1; } else r=mid-1; } //cout<<ans<<'\n'; if(ans!=0){ ans=(ans-(cur_digit)+1); } cout<<ans<<'\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(time(0)); time0 = curtime; ll t=1; //cin>>t; repe(tt,t){ //cout<<"Case #"<<tt<<": "; solve(); } cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const ll mod=998244353; ll n,k; ll f[3010][3010]; int read(){ int f=1,x=0; char ss=getchar(); while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();} while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();} return f*x; } ll calc(int n,int k){ if (n<k)return 0; ll &ret=f[n][k]; if (ret!=-1)return ret; ret=0; if (k==0)ret=0; else if (n==k)ret=1; else if (n==1)ret=1; else ret=(calc(n-1,k-1)+calc(n,2*k))%mod; return ret; } int main() { n=read();k=read(); for (int i=1;i<=n;i++)for(int j=1;j<=n;j++)f[i][j]=-1; printf("%lld\n",calc(n,k)); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int n; map<pair<int, int>, int> ans; int solve(vector<int> & a, int x, int st) { if (ans[{x, st}]) { return ans[{x, st}]; } if (st == n - 1) { return 1; } if (x % (a[st + 1] / a[st]) == 0) { int k = (a[st + 1] / a[st]); int res = solve(a, x / k, st + 1); ans[{x, st}] = res; return res; } int k = (a[st + 1] / a[st]); int res = solve(a, x / k, st + 1) + solve(a, x / k + 1, st + 1); ans[{x, st}] = res; return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); srand(clock()); int x; cin >> n >> x; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } cout << solve(a, x, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int tc; cin >> tc; while(tc--){ int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;i++) cin >> a[i]; if(n&1){ cout << "Second\n"; }else{ sort(a.begin(),a.end()); bool bo=true; for(int i=0;i<n;i+=2){ if(a[i]!=a[i+1]) bo=false; } if(bo){ cout << "Second\n"; }else{ cout << "First\n"; } } } return 0; }
#include <bits/stdc++.h> #define pb push_back #define SZ(x) ((int)(x.size())) #define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i) #define FORD(i,s,l) for (ll i = (s); (i) >= l; --i) #define F first #define S second #define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1) #define TEST(x,i) ((x)&(1ll<<(i))) #define SET(x,i) ((x)|(1ll<<(i))) #define FLIP(x,i) ((x)^(1ll<<(i))) #define CLEAR(x,i) ((x)&~(1ll<<(i))) const double pi = 4 * atan(1); using namespace std; typedef long long ll; template<class ForwardIterator> void print_vec(ForwardIterator first, ForwardIterator last, string sep = " ", string end="\n") { bool ft = true; while (first != last) { if (! ft) {cout << sep;} else {ft = false;} cout << (*first); ++first; } cout << end; } inline ll floorDiv(ll x, ll y) { ll d = x / y; ll r = x % y; return r ? (d - ((x < 0) ^ (y < 0))) : d; } ll ceilDiv(ll x, ll y) { return -floorDiv(-x, y); } bool predicate(ll mid) { return mid > 10; } ll bin_search(ll lo, ll hi) { int sign = lo < hi ? 1 : -1; lo *= sign; hi *= sign; hi++; while (lo < hi) { ll mid = floorDiv(lo + hi, 2); if (!predicate(sign * mid)) { lo = mid + 1; } else { hi = mid; } } return sign * lo; } const ll MOD = 1000000007; const int MAXN = 200005; ll num[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); TC { int n; cin >> n; FOR(i,0,n){ cin >> num[i]; } if (n%2 == 1) { cout << "Second\n"; continue; } map<ll,int> cnt; FOR(i,0,n) { cnt[num[i]]++; } bool g = true; for (auto &p : cnt) { if (p.S%2!=0) { g=false; break; } } cout << (g ? "Second\n" : "First\n" ); } }
//g++ -std=gnu++14 a.cpp #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <tuple> #include <iomanip> #include <random> #include <math.h> #include <stdio.h> using namespace std; #define ll long long #define rep(i, n) for(ll i = 0; i < (n); i++) #define P pair<ll,ll> ll MOD = 1e9 + 7; int INF = 1 << 30; ll INFL = 1LL << 60; ll MODP = 998244353; const ll M = 2000000; ll lmax(ll x,ll y){ if(x < y) return y; else return x; } ll lmin(ll x, ll y){ if(x > y)return y; else return x; } ll gen(ll x,ll y){ if(y == 0)return 1; if(y == 1)return x; if(y%2 == 1){ return ((x%M)*gen(x,y-1)%M)%M; }else{ ll res = gen(x,y/2)%M; return ((res%M)*(res%M))%M; } } ll gcd(ll x, ll y){ if(x < y) swap(x,y); if(y == 0) return x; return gcd(y,x%y); } ll lcm(ll x,ll y){ ll s = gcd(x,y); x /= s; y /= s; return x*y*s; } struct UnionFind{ vector<ll> d; vector<P> m; UnionFind(int n) : d(n,-1),m(n,make_pair(-INFL,INFL)) {}; int find(int x){ if(d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x,int y){ x = find(x); y = find(y); if(x == y) return false; if(d[x] > d[y])swap(x,y); //yをxにくっつける d[x] += d[y]; d[y] = x; m[y].first = lmax(m[x].first,m[y].first); m[y].second = lmin(m[x].second,m[y].second); return true; } bool same(int x,int y){ return find(x)==find(y); } int size(int x){ return -d[find(x)]; } }; int main(){ ll n,x; cin >> n >> x; vector<ll> ans; rep(i,n){ ll a; cin >> a; if(a != x)ans.push_back(a); } for(auto x : ans){ cout << x << " "; } cout << endl; return 0; }
#include <iostream> #include <vector> #include <string> int main() { int N; int64_t X; std::cin>>N>>X; std::vector<int64_t> A(N); for(auto &e:A)std::cin>>e; std::string result; result.reserve(11*N); for(auto e:A)if(e!=X)(result+=std::to_string(e))+=' '; if(result.empty()==false)result.pop_back(); std::cout<<result<<std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn=55; struct to { int x,y,id; } dd[5],d[5]; int a[maxn][maxn],w[maxn][maxn],ans[maxn*maxn],temp[maxn*maxn],vis[maxn*maxn],sum=0,n=50,finalans=0,s[maxn]; char p[5]= {'\0','D','R','U','L'}; bool cmp(to a,to b) { return s[a.id]>s[b.id]; } int get(int x,int y,to a) { x=x+a.x; y=y+a.y; if(vis[::a[x][y]]||x>n||y>n||x<1||y<1) { return -1; } return w[x][y]; } void dfs(int x,int y) { if(1.0*clock()/CLOCKS_PER_SEC>1.8) { if(sum>finalans) { finalans=sum; memcpy(ans,temp,sizeof(temp)); } for(int i=1; i<=ans[0]; i++) { cout<<p[ans[i]]; } exit(0); } vis[a[x][y]]=1; // temp[++temp[0]]= sum+=w[x][y]; int fl=0; to d[5]; memcpy(d,dd,sizeof(d)); // random_shuffle(d+1,d+4+1); for(int i=1;i<=4;i++) { s[i]=get(x,y,d[i]); } sort(d+1,d+4+1,cmp); for(int i=1; i<=4; i++) { int nx=x+d[i].x,ny=y+d[i].y; if(!vis[a[nx][ny]]&&nx<=n&&nx>=1&&ny<=n&&ny>=1) { temp[++temp[0]]=d[i].id; dfs(nx,ny); fl=1; --temp[0]; } } if(fl==0) { if(sum>finalans) { finalans=sum; memcpy(ans,temp,sizeof(temp)); } } sum-=w[x][y]; vis[a[x][y]]=0; } int main() { dd[1]=(to) { 1,0,1 }; dd[2]=(to) { 0,1,2 }; dd[3]=(to) { -1,0,3 }; dd[4]=(to) { 0,-1,4 }; int x,y; cin>>x>>y; x++,y++; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>a[i][j]; } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>w[i][j]; } } srand(time(0)); dfs(x,y); for(int i=1; i<=ans[0]; i++) { cout<<p[ans[i]]; } exit(0); }
#include "bits/stdc++.h" using namespace std; typedef long long ll; #define MOD 1000000007ll #define INF 1000000000000000000ll #define pb push_back #define fs first #define sc second const double pi = acos(-1); vector<bool> dfs(int start, vector<vector<int>>& adj) { stack<int> s; s.push(start); vector<bool> vis(adj.size(), false); vis[start] = true; while (s.size()) { int cur = s.top(); s.pop(); for (int nxt : adj[cur]) { if (!vis[nxt]) { s.push(nxt); vis[nxt] = true; } } } return vis; } int n; vector<pair<int, int>> A; bool can(double r) { set<pair<int, int>> points; for (int i = 0; i < n; i++) { points.insert(A[i]), points.insert(make_pair(A[i].fs, 100)), points.insert(make_pair(A[i].fs, -100)); } map<pair<int, int>, int> from; int idx = 0; for (pair<int, int> point : points) { from[point] = idx++; } int m = points.size(); vector<vector<int>> adj(m); for (pair<int, int> point1 : points) { for (pair<int, int> point2 : points) { ll dx = abs(point1.fs - point2.fs), dy = abs(point1.sc - point2.sc); if (sqrt(dx * dx + dy * dy) < 2 * r) adj[from[point1]].pb(from[point2]); } } for (int i = 0; i < n; i++) { pair<int, int> curs = make_pair(A[i].fs, 100); vector<bool> reach = dfs(from[curs], adj); for (int j = 0; j < n; j++) { pair<int, int> cor = make_pair(A[j].fs, -100); if (reach[from[cor]]) { return false; } } } return true; } void solve() { cin >> n; A = vector<pair<int, int>>(n); for (int i = 0; i < n; i++) cin >> A[i].fs >> A[i].sc; double lo = 0, hi = 200, bst = 0; while (abs(hi - lo) > 1e-7) { double mid = (hi + lo) / 2; if (can(mid)) { lo = mid; bst = mid; } else { hi = mid; } } cout << fixed << setprecision(12) << bst << endl; } signed main() { ios_base::sync_with_stdio(false); /*int t; cin >> t; while (t--)*/ solve(); }
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> using namespace std; const int N = 1e5 + 10; int n, m; int tot, Head[N], ver[N << 1], Next[N << 1], Leng[N << 1]; int fa[N], ans[N]; void add(int x, int y, int z) { tot++; ver[tot] = y; Leng[tot] = z; Next[tot] = Head[x]; Head[x] = tot; } int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); } void merge(int x, int y) { fa[findfa(x)] = findfa(y); } void dfs(int x, int f) { for (int i = Head[x]; i; i = Next[i]) { int y = ver[i]; if (y == f) continue; if (Leng[i] == ans[x]) { if (ans[x] == 1) ans[y] = 2; else ans[y] = 1; } else ans[y] = Leng[i]; dfs(y, x); } } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); if(findfa(x) != findfa(y)) { merge(x, y); add(x, y, z); add(y, x, z); } } ans[1] = 1; dfs(1, 0); for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
#include<bits/stdc++.h> using namespace std; using namespace std::chrono; const int mod=1e9+7; const int mex=300005; #define ll long long #define test int t;cin>>t;while(t--) #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define fo(i,a,n) for(int i=a;i<n;i++) #define rfo(i,a,b) for(int i=a;i>=b;i--) #define bg begin() #define en end() #define fi first #define se second #define ub upper_bound #define lb lower_bound #define pb push_back #define veci vector<int> #define veclli vector<long long int> #define all(x) x.begin(),x.end() #define sci(x) scanf("%d",&x); #define scc(x) scanf("%c",&x); #define scs(x) scanf("%s",x); #define debug(arr,n) for(int i=0;i<n;i++) printf("%d ",arr[i]); #define sz(x) x.size() #define loop(x) for(auto it=x.begin();it!= x.end();it++) vector<pair<int,int> > v[mex]; int vis[mex],val[mex]; int N; void dfs(int n) { vis[n]=1; if(val[n]==0) val[n]=1; fo(i,0,sz(v[n])) { if(vis[v[n][i].fi]==0&&val[v[n][i].fi]==0) { if(val[n]!=v[n][i].se) val[v[n][i].fi]=v[n][i].se; else val[v[n][i].fi]=v[n][i].se%N+1; dfs(v[n][i].fi); } } } int main() { int n,m; cin>>n>>m; N=n; fo(i,0,m) { int a,b,c; cin>>a>>b>>c; v[a].pb({b,c}); v[b].pb({a,c}); } dfs(1); fo(i,1,n+1) cout<<val[i]<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define Rep(i,a,b) for(int i=a;i<b;i++) #define ALL(x) (x).begin(),(x).end() using P = pair<int,int>; using ll = long long; using ull = unsigned long long; 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 pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<int> vint; const int INF=1001001001; int main(){ int a,b,c,d; cin >> a >> b >> c >> d ; cout << b-c << 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; const int INF = (1 << 30) - 1; const ll LINF = (1LL << 60) - 1; const double PI = acos(-1); ll n, A; ll a[110]; // dpテーブル bool dp[110][100100]; int main() { cin >> n; ll sum = 0; rep(i, n) { ll val; cin >> val; sum += val; a[i] = val; } memset(dp, 0, sizeof(dp)); // 一旦すべて false に dp[0][0] = true; // dp[0][0] だけ true に rep(i, n) { rep(j, sum+1) { dp[i+1][j] |= dp[i][j]; if (j >= a[i]) dp[i+1][j] |= dp[i][j-a[i]]; } } ll cnt = 100100; for (ll ans = sum; ans >= 0; ans--) { if (dp[n][ans]) { ll big = max(ans, sum - ans); // cout << ans << " " << sum-ans << endl; cnt = min(cnt, big); } } cout << cnt << endl; return 0; } //小数点の出力 //cout << fixed << std::setprecision(15) << ans << endl;
#include <bits/stdc++.h> template <class T> inline void read(T &x) { static char ch; static bool opt; while (!isdigit(ch = getchar()) && ch != '-'); x = (opt = ch == '-') ? 0 : ch - '0'; while (isdigit(ch = getchar())) x = x * 10 + ch - '0'; if (opt) x = ~x + 1; } template <class T> inline void putint(T x) { static char buf[45], *tail = buf; if (!x) putchar('0'); else { if (x < 0) putchar('-'), x = ~x + 1; for (; x; x /= 10) *++tail = x % 10 + '0'; for (; tail != buf; --tail) putchar(*tail); } } template <class T> inline bool tense(T &x, const T &y) { return y < x ? x = y, true : false; } template <class T> inline bool relax(T &x, const T &y) { return x < y ? x = y, true : false; } template <class T> inline T getAbs(const T &x) { return x < 0 ? -x : x; } typedef long long s64; typedef long double ld; typedef std::vector<int> vi; typedef std::pair<int, int> pii; #define mp(x, y) std::make_pair(x, y) using std::cout; using std::cerr; int t, n; inline s64 getless(s64 k) { s64 l = 1, r = k + 1, res = 1; while (l <= r) { s64 mid = (l + r) >> 1; if ((100 + t) * mid / 100 > k) res = mid, r = mid - 1; else l = mid + 1; } // cerr << k << ' ' << res - 1 << '\n'; return res - 1; } inline s64 findnth(int n) { s64 l = 1, r = 1e18, res = 1; while (l <= r) { s64 mid = (l + r) >> 1; if (mid - getless(mid) >= n) res = mid, r = mid - 1; else l = mid + 1; } return res; } int main() { #ifdef orzczk freopen("a.in", "r", stdin); #endif read(t), read(n); cout << findnth(n) << '\n'; return 0; }
#include <bits/stdc++.h> #define REP(i, e) for(int (i) = 0; (i) < (e); ++(i)) #define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i)) #define ALL(c) (c).begin(), (c).end() #define PRINT(x) cout << (x) << "\n" using namespace std; using ll = long long; using pll = pair<ll, ll>; const long long MOD = 1000000007; signed main(){ ll N; cin >> N; ll a = 3, b = 5; FOR(A, 1, 100){ b = 5; FOR(B, 1, 100){ if(a + b == N){ cout << A << " " << B << endl; return 0; } if(5 * b > N) break; else b *= 5; } if(3 * a > N) break; else a *= 3; } PRINT(-1); return 0; }
#include <iostream> #include <cstdio> #include <stdio.h> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <stack> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <utility> #include <numeric> #define pb push_back #define mk make_pair #define endl "\n" #define mod 1000000007 #define mod1 1610612741 #define mul 31 #define PI 3.14159265358979323846264 //#include bits/stdc++.h //#include <ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/assoc_container.hpp> using namespace std; typedef long long int lli; typedef long double ld; typedef pair<lli,lli> ii; //priority_queue <lli, vector<lli>, greater<lli> > ti; //priority_queue<pair<lli,pair<lli,lli>>>e; vector <lli> p[300005],q[300005],f(200005,-1),d(300005,0),a(300005); //vector<set<lli>>s(200005); //set<pair<lli,lli>>s; //vector<vector<lli>> dp(200005,vector<lli>(25,0)); //vector<lli>f(10000005); //lli b[1000005],l[1000005]; //vector<vector<lli>> d(300005,vector<lli>(18,0)); //vector<pair<lli,ii>>p[300005]; //map<pair<lli,lli>,lli>mp,np; //vector<pair<pair<lli, lli>,lli> > st; //queue<lli> qy; //freopen("codecoder.in", "r", stdin); //freopen("codecoder.out", "w", stdout); lli gcd(lli a, lli b) { if (b == 0) return a; return gcd(b, a % b); } lli bpow(lli a, lli b) { lli res = 1; while (b > 0) { if (b & 1) res = (res * a)%mod; a = (a * a)%mod; b >>= 1; } return res%mod; } void fact(lli i) { f[0]=1; for(lli k=1;k<=i;k++) { (f[k]=f[k-1]*k)%=mod; } } lli isprime(lli n) { if(n==1) return 0; for(lli i=2;i<=sqrt(n);i++) if(n%i==0) return 0; return 1; } lli find(lli x) { if(f[x]==x) return x; else return f[x]=find(f[x]); } bool cmp(lli x,lli y) { return x<y; } void check() { cout<<"HI"<<endl; } lli comb(lli i,lli j) { if(j>i)return 0; lli k=f[i]; lli g=(f[j]*(f[i-j]))%mod; lli h=bpow(g,mod-2); return (k*h)%mod; } pair<ld,ld>az(ld a,ld b,ld c,ld d,ld u) { ld x1=(a-c)*cos(u)-(b-d)*sin(u)+c; ld y1=(a-c)*sin(u)+(b-d)*cos(u)+d; return {x1,y1}; } /*void sieve() { for(lli i=2;i<=sqrt(10000000);i++) { if(b[i]==0) { k.pb(i); for(lli j=2;i*j<=sqrt(10000000);j++) { b[i*j]=1; } } } }*/ int main () { ios_base::sync_with_stdio(false); cin.tie(NULL); lli t;t=1; while(t--) { lli n;cin>>n; vector<lli> a(n); for(lli i=0;i<n;i++) cin>>a[i]; lli cnt=min(8ll,n); vector<vector<lli>> b(200,vector<lli>(0)); for(lli i=1;i<(1ll<<cnt);i++) { vector<lli>v; lli s=0; for(lli j=0;j<cnt;j++) { if(i&(1ll<<j)) { v.pb(j+1); (s+=a[j])%=200; } } if(b[s].size()!=0) { cout<<"Yes"<<endl; cout<<b[s].size()<<" "; for(lli j=0;j<b[s].size();j++) { cout<<b[s][j]<<" "; } cout<<endl; cout<<v.size()<<" "; for(lli j=0;j<v.size();j++) { cout<<v[j]<<" "; } cout<<endl; return 0; } else { b[s]=v; } } cout<<"No"<<endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ull = unsigned long long int; 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; } int main() { int n; cin >> n; vector<ll> a(n); for (auto &i : a) cin >> i; vector<ll> b, c; vector<vector<ll>> v(200); for (int i = 0; i < n; i++) { if (v[a[i] % 200].size() == 0) v[a[i] % 200].push_back(i); else { b.push_back(i); c = (v[a[i] % 200]); goto END; } for (int j = 0; j < 200; j++) { if (v[j].size() == 0) continue; if (j == a[i] % 200) continue; ll t = (a[i] + j) % 200; if (v[t].size() == 0) { if (find(v[j].begin(), v[j].end(), i) != v[j].end()) continue; v[t] = v[j]; v[t].push_back(i); } else { if (find(v[j].begin(), v[j].end(), i) != v[j].end()) continue; b = v[j]; b.push_back(i); c = v[t]; goto END; } } } END: if (b.size() != 0 && c.size() != 0) { cout << "Yes" << endl; std::sort(b.begin(), b.end()); std::sort(c.begin(), c.end()); cout << b.size() << " "; for (auto i : b) cout << i + 1 << " "; cout << endl << c.size() << " "; for (auto i : c) cout << i + 1 << " "; cout << endl; } else { cout << "No" << endl; } }
//Ctrl+Shift+Bでコンパイル #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(){ long t,N; cin >> t >> N; long long ans = 0; long num = N/t; int num_a = N%t; if(num_a == 0){ ans = num*(100+t) - 1; } else{ ans = num*(100+t); double num_aa = double(num_a); double tt = double(t); vector<int> a ; int prs = 0; for(int i = 1; i < 100; i++){ int buy = i*(100.0+tt)/100.0; if(buy == prs+1){ prs = buy; } else{ a.push_back(buy-1); prs = buy; } } ans += a.at(num_a-1); } cout << ans << endl; }
#include<bits/stdc++.h> #include<algorithm> #include<math.h> #include<utility> using namespace std; #define pb push_back #define mp make_pair #define fs first #define sec second #define pq priority_queue #define lb lower_bound #define ub upper_bound #define ll long long int #define endl "\n" #define all(x) (x).begin(),(x).end() #define pb push_back #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i, n) for(int i=n-1;i>=0;i--) typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; long long int mod=1000000007; int 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 ll t,n; cin>>t>>n; ll r=n*100; ll s=r%t; if(r%t) cout<<r/t+n<<endl; else cout<<r/t+n-1<<endl; return 0; }
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <climits> #include <vector> #include <sstream> #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 all(v) v.begin(),v.end() #define div 1000000007 #define div2 998244353 #define PI 3.141592653589793 #define inf 1000000000000000000LL #define blk 320 #define pb push_back void solve() { int a,b,c,d;cin>>a>>b>>c>>d; if(d*c-b<=0) { cout<<-1; return; } int x=d*c-b; int ans=(a+x-1)/x; 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(); } }
#include<bits/stdc++.h> using namespace std; #define ll long long int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif double n,x,y; cin>>n>>x>>y; double maxi=INT_MAX; while(n--) { double u,i; cin>>u>>i; maxi=min(maxi,x*((y-i)/(x-u))); } if((y-maxi)<0) cout<<0.0<<"\n"; else cout<<y-maxi<<"\n"; }
#include <bits/stdc++.h> using namespace std; int main(){ long long int N, T; cin >> T; for(int i = 0; i < T; i++){ cin >> N; if(N % 2 == 0){ if(N % 4 == 0){ cout << "Even" << endl; }else{ cout << "Same" << endl; } }else{ cout << "Odd" << endl; } } return 0; }
#include <map> #include <set> #include <math.h> #include <queue> #include <cstdio> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <ctime> #include <iostream> #include <algorithm> #include<bits/stdc++.h> #include<unordered_map> #include<unordered_set> #define f(i,a,b) for( int i=a;i<=b;++i) #define ff(i,a,b) for( int i=a;i>=b;--i) #define debug(x) cerr << #x << " : " << x << " " << endl using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<string, string> pss; const ll mod = 1e9 + 7; const ll mod2 = 998244353; const ll inf = 2e18; const double tiaohe = 0.57721566490153286060651209; ll oula(ll x) { ll res = x;f(i, 2, x / i) { if (x % i == 0) { res = res / i * (i - 1);while (x % i == 0) x /= i; } }if (x > 1) res = res / x * (x - 1);return res; } ll quickmod(ll a, ll n, ll m) { ll s = 1;while (n) { if (n & 1) { s = s * a % m; }a = (a*a) % m;n = n / 2; }return s; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) { if (!b) { d = a, x = 1, y = 0; } else { ex_gcd(b, a % b, y, x, d);y -= x * (a / b); } } ll inv(ll t, ll p) { ll d, x, y;ex_gcd(t, p, x, y, d);return d == 1 ? (x % p + p) % p : -1; } bool isPrime(ll x) { if (x == 2)return true;if (x % 2 == 0)return false;for (ll i = 2;i*i <= x;i++) if (x % i == 0)return false; return true; } inline ll in() { char ch = getchar();ll x = 0, f = 1;while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }return x * f; } void print(double x) { printf("%.6lf\n", x); } //double a = log(n) +tiaohe + 1.0 / (2 * n); double eqa = (1 + sqrt(5.0)) / 2.0; double E = 2.7182818284; const double eps = 1e-10; const double pi = 3.1415926535; const int N = 1e6 + 100; int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int t; cin >> t; while (t--) { ll n; cin >> n; int num = 0; while (n % 2 == 0) { n /= 2; num++; } if (num == 1) { puts("Same"); } else if (num >= 2) { puts("Even"); } else puts("Odd"); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; int ans=0; for(int i=0;i<=9999;i++) { bool flag[10]={false}; int x=i; for(int j=0;j<4;j++) { flag[x%10]=true; x/=10; } int f2=1; for(int i=0;i<10;i++) { if(s[i]=='o'&&flag[i]==false) f2=0; if(s[i]=='x'&&flag[i]) f2=0; } if(f2==1) ans+=1; } cout<<ans<<endl; return 0; }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i)) #define rep(i,n) FOR(i,0,n) #define all(v) begin(v),end(v) #ifdef NDEBUG #define assert(e) if(!(e)){stod("");} #endif #ifdef ONLINE_JUDGE #define debug(x) #define debug2(x,y) #define debug3(x,y,z) #else #define debug(x) std::cerr<<#x<<": "<<x<<"\n" #define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n" #define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n" #endif using ll=long long; using vi=std::vector<int>; using vvi=std::vector<vi>; using vll=std::vector<ll>; using vvll=std::vector<vll>; template<typename T> using vvec=std::vector<std::vector<T>>; template<typename T> auto make_v(size_t sz){return std::vector<T>(sz);} template<typename T,typename... Ts> auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));} template<typename T> void fill_v(T&var,const T&x){var=x;} template<typename V,typename T> void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){ s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;} inline void scan(int&a){scanf("%d",&a);} inline void scan(ll&a){scanf("%lld",&a);} inline void scan(char&a){scanf(" %c",&a);} inline void scan(double&a){scanf("%lf",&a);} template<typename T> inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}} template<typename First,typename...Args> inline void scan(First&f,Args&...args){scan(f);scan(args...);} inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);} inline void print(int a){printf("%d\n",a);} inline void print(ll a){printf("%lld\n",a);} inline void print(double a){printf("%.12f\n",a);} inline void print(std::string s){std::cout<<s<<"\n";} using namespace std; void solve() { int n, d, h; scan(n, d, h); vi x(n), y(n); rep (i, n) { scan(x[i], y[i]); } double ans = 0; rep (i, n) { double needed = y[i] - (double)(h - y[i]) / (d - x[i]) * x[i]; ans = max(ans, needed); } print(ans); } int main() { solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int t[10]; int tp[50]={8,16,24,32,40,48,56,64,72,80,88,96}; int main() { string s; cin>>s; for(int i=0;i<s.size();i++) { int d=s[i]-'0'; t[d]++; } if(s.size()>=3) { for(int i=112;i<=999;i+=8) { int a1,b1,c1; a1=i/100; b1=i/10%10; c1=i%10; t[a1]--; t[b1]--; t[c1]--; if(t[a1]>=0&&t[b1]>=0&&t[c1]>=0) { cout<<"Yes"; return 0; } else { t[a1]++; t[b1]++; t[c1]++; } } cout<<"No"; return 0; } if(s.size()==2) { for(int i=0;i<=96;i+=8) { int a1,b1; a1=i/10; b1=i%10; t[a1]--; t[b1]--; if(t[a1]>=0&&t[b1]>=0) { cout<<"Yes"; return 0; } else { t[a1]++; t[b1]++; } } cout<<"No"; return 0; } if(s.size()==1) { int a=s[0]-'0'; if(a%8==0) cout<<"Yes"; else cout<<"No"; } return 0; }
#include<bits/stdc++.h> #define ll long long #define re register #define INF 2147483647 using namespace std; inline ll read() { ll f=1,x=0;char s=getchar(); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=x*10+s-48; s=getchar(); } return f*x; } ll mul(ll a,ll b,ll mod) { ll res=0; while(b>0) { if(b&1) res=(res+a)%mod; a=(a+a)%mod; b>>=1; } return res; } ll exgcd(ll a,ll b,ll &x,ll &y) { if(b==0){x=1;y=0;return a;} ll gcd=exgcd(b,a%b,x,y); ll tp=x; x=y; y=tp-a/b*y; return gcd; } int n=2; ll ai[20],bi[20]; ll excrt() { ll x,y,k; ll M=bi[1],ans=ai[1]; for(int i=2;i<=n;i++) { ll a=M,b=bi[i],c=(ai[i]-ans%b+b)%b; ll gcd=exgcd(a,b,x,y),bg=b/gcd; if(c%gcd!=0) return -1; x=mul(x,c/gcd,bg); ans+=x*M; M*=bg; ans=(ans%M+M)%M; } return (ans%M+M)%M; } const ll LLINF=1ll<<62; int main() { int T=read(); while(T--) { int x=read(),y=read(),p=read(),q=read(); ll A=2*x+2*y,B=p+q; ll ans=LLINF; for(int i=0;i<y;i++) for(int j=0;j<q;j++) { ai[1]=i+x,bi[1]=A; ai[2]=j+p,bi[2]=B; ll res=excrt(); if(res!=-1) ans=min(ans,res); } if(ans==LLINF) puts("infinity"); else printf("%lld\n",ans); } return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int (i)=0;(i) < (n);(i)++) #define repsec(i,m,n) for(int (i)=(m);(i) < (n);(i)++) #define ll long long #define mod 998244353 #define MOD 1000000007 using namespace std; int gcd(int a,int b){ if(b == 0) return a; else return gcd(b,a%b); } int main(){ int n,ans; cin >> n; vector<int> a(n); for(int i=0;i < n;i++){ cin >> a[i]; if(i == 0) ans = a[i]; else ans = gcd(ans,a[i]); } cout << ans << endl; return 0; }
//#pragma GCC optimize(4) #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define REP(u) for(int i=p[u];i!=-1;i=e[i].nxt) #define ll long long #define ull unsigned long long #define PII pair<int,int> #define rd (rand()<<16^rand()) #define pb push_back #define iter multiset<int>::iterator using namespace std; inline int gi() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); } return (f==1)?x:-x; } inline int max(int a,int b){return a>b?a:b;} inline int min(int a,int b){return a<b?a:b;} multiset<int>s; int n,x; int main() { n=gi(); FOR(i,1,n) { x=gi(); s.insert(x); } while(s.size()) { int x=*s.begin(); iter it=s.end(); --it; int y=*it; if(x==y)break; else {s.erase(y);s.insert(y-x);} } printf("%d\n",*s.begin()); return 0; }
#include <iostream> using namespace std; int main() { int H, W, aij; cin >> H >> W; int minblock = 1e9; int sum = 0; for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { cin >> aij; minblock = min(aij, minblock); sum += aij; } } cout << sum - (minblock * H * W) << endl; }
#pragma GCC optimize("Ofast") #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define max(a, b) (a < b? b : a) #define min(a, b) ((a>b)?b:a) #define MOD 1000000007 #define FOR(a,c) for ( ll (a)=0; (a)<(c); (a)++) #define FORL(a,b,c) for ( ll (a)=(b); (a)<(c); (a)++) #define FORR(a,b,c) for ( ll (a)=(b); (a)>=(c); (a)--) #define INF 9223372036854775807 //18446744073709551615 #define abs llabs #define by_ANIKET ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define no cout<<"NO\n"; #define yes cout<<"YES\n"; #define Clear( a, b ) memset( a, b, sizeof( a ) ) #define run if(1)cout<<__LINE__<<endl; #define pr3(a,b,c) cerr<<#a<<"="<<a<<"\t"<<#b<<"="<<b<<"\t"<<#c<<"="<<c<<"\n"; #define pr2(a,b) cerr<<#a<<"="<<a<<"\t"<<#b<<"="<<b<<"\n"; #define pr1(a) cerr<<#a<<"="<<a<<"\n"; #define debug(x) cerr<< #x <<" : "<< x << endl; #define debuga(A,N) cerr<< #A <<" : [";for(int i = 0; i<N;i++) cerr<<A[i]<<" "; cerr<<"]\n"; #define debuga2(A,N,M) cerr<< #A << " : \n"; for(int i=0;i<N;i++){cerr<<"[";for(int j=0;j<M;++j) cerr<<A[i][j]<<" ";cerr<<"]\n";} #define debugp(p) cerr<< #p <<" : "<<"("<<(p).first<<","<<(p).second<<")\n"; #define debugv(v) cerr<< #v <<" : "<<"[";for(int i = 0; i< (v).size(); i++) cerr<<v[i]<<" "; cerr<<"]\n"; #define debugv2(v) cerr<< #v << " : \n"; for(int i=0;i<v.size();i++){cerr<<"[";for(int j=0;j<(v[0].size());++j) cerr<<v[i][j]<<" ";cerr<<"]\n";} #define debugs(m) cerr<< #m <<" : [ "; for(auto itr = m.begin(); itr!=m.end();itr++) cerr<<*itr<<" "; cerr<<"]\n"; #define debugm(m) cerr<< #m <<" : [ "; for(auto itr = m.begin();itr!=m.end(); itr++) cerr<<"("<<itr->first<<","<<itr->second<<") ";cerr<<"]\n"; typedef unsigned long long ull; typedef long long int ll; typedef vector<ll> vi; typedef pair<ll,ll> pi; #define F first #define S second #define PB push_back #define POB pop_back #define MP make_pair inline long long max3(long long a, long long b,long long c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));} inline long long min3(long long a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));} void solve() { int n,m,t=0,k=0,l,x=0,y=0,z=0;//,p,a1,a2,a3,a4,a5,ex,che,var=1,yd,f=INF,templ,tempr; cin>>n>>m; // vi a(n); // FOR(i,n)cin>>a[i]; int a[n][m]; z=100; FOR(i,n) { FOR(j,m) { cin>>a[i][j]; z=min(z,a[i][j]); } } FOR(i,n) { FOR(j,m) { x+=(a[i][j]-z); // z=min(z,a[i][j]); } } cout<<x<<"\n"; } int main() { by_ANIKET ll T=1,t=0; // cin>>T; while(t++<T) { //cout<<"Case "<<t<<":"<<' '; solve(); //cout<<'\n'; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; } // © // ______ __ __ ______ __ __ ________ ________ // / \| \ | \ \ \ / \ \ \ // | ▓▓▓▓▓▓\ ▓▓\ | ▓▓\▓▓▓▓▓▓ ▓▓ / ▓▓ ▓▓▓▓▓▓▓▓\▓▓▓▓▓▓▓▓ // ______ ______ | ▓▓__| ▓▓ ▓▓▓\| ▓▓ | ▓▓ | ▓▓/ ▓▓| ▓▓__ | ▓▓ // | \ \ | ▓▓ ▓▓ ▓▓▓▓\ ▓▓ | ▓▓ | ▓▓ ▓▓ | ▓▓ \ | ▓▓ // \▓▓▓▓▓▓\▓▓▓▓▓▓ | ▓▓▓▓▓▓▓▓ ▓▓\▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓\ | ▓▓▓▓▓ | ▓▓ // | ▓▓ | ▓▓ ▓▓ \▓▓▓▓_| ▓▓_| ▓▓ \▓▓\| ▓▓_____ | ▓▓ // | ▓▓ | ▓▓ ▓▓ \▓▓▓ ▓▓ \ ▓▓ \▓▓\ ▓▓ \ | ▓▓ // \▓▓ \▓▓\▓▓ \▓▓\▓▓▓▓▓▓\▓▓ \▓▓\▓▓▓▓▓▓▓▓ \▓▓
#include <stdio.h> #include <vector> #include <utility> using namespace std; typedef long long ll; typedef pair<ll, ll> pl; int main(void) { ll i, j, n, m, x, y, z, cnt; scanf("%lld%lld", &n, &m); vector<pl> vec[n + 1]; ll dp[1 << n], sum[n]; for(i = 0; i < 1 << n; ++i) dp[i] = 0; dp[0] = 1; for(i = 0; i < m; ++i) { scanf("%lld%lld%lld", &x, &y, &z); vec[x].push_back(make_pair(--y, z)); } for(i = 0; i < 1 << n; ++i) { cnt = 0; for(j = 0; j < n; ++j) { sum[j] = i & (1 << j) ? 1 : 0, cnt += sum[j]; if(j) sum[j] += sum[j - 1]; } for(j = 0; j < vec[cnt].size(); ++j) if(sum[vec[cnt][j].first] > vec[cnt][j].second) dp[i] = 0; if(dp[i]) for(j = 0; j < n; ++j) if(!(i & (1 << j))) dp[i | (1 << j)] += dp[i]; } printf("%lld", dp[(1 << n) - 1]); return 0; }
#pragma GCC target ("avx2") // #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <iostream> #include <array> #include <algorithm> #include <vector> #include <bitset> #include <set> #include <unordered_set> #include <cmath> #include <complex> #include <deque> #include <iterator> #include <numeric> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <string> #include <tuple> #include <utility> #include <limits> #include <iomanip> #include <functional> #include <cassert> // #include <atcoder/all> using namespace std; using ll=long long; template<class T> using V = vector<T>; template<class T, class U> using P = pair<T, U>; using vll = V<ll>; using vii = V<int>; using vvll = V<vll>; using vvii = V< V<int> >; using PII = P<int, int>; using PLL = P<ll, ll>; #define RevREP(i,n,a) for(ll i=n;i>a;i--) // (a,n] #define REP(i,a,n) for(ll i=a;i<n;i++) // [a,n) #define rep(i, n) REP(i,0,n) #define ALL(v) v.begin(),v.end() #define eb emplace_back #define pb push_back #define sz(v) int(v.size()) template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; } template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; } template< class A, class B > ostream& operator <<(ostream& out, const P<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template< class A > ostream& operator <<(ostream& out, const V<A> &v) { out << '['; for (int i=0;i<int(v.size());i++) { if (i) out << ", "; out << v[i]; } return out << ']'; } template<class A, class B> istream& operator >>(istream& in, P<A, B> &p) { return in >> p.first >> p.second; } template<class A> istream& operator >>(istream& in, V<A> &v) { for (int i = 0; i < int(v.size()); i++) in >> v[i]; return in; } const long long MOD = 1000000007; const long long HIGHINF = (long long)1e18; const int INF = (int)1e9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; V< V<PII> > xyz(n + 1); rep(i, m) { int x, y, z; cin >> x >> y >> z; xyz[x].eb(PII(y - 1, z)); } vll dp(1 << n, 0); dp[0] = 1; rep(bit, 1 << n) { int tx = __builtin_popcount(bit); rep(i, n) { if (!(bit >> i & 1)) { ll tobit = bit | (1 << i); vii acc(n, 0); rep(j, n) if (tobit >> j & 1) acc[j]++; rep(j, n - 1) acc[j + 1] += acc[j]; bool ok = true; for (auto [y, z]: xyz[tx + 1]) { if (acc[y] > z) ok = false; } if (ok) dp[tobit] += dp[bit]; } } } cout << dp.back() << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i) #define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i) #define revrep(i,n) for(ll i=(ll)(n)-1; i>=0; --i) inline constexpr ll Inf = (1ULL << 60) -123456789; #define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10); #define newl '\n' #define YN(e) ((e)?"Yes":"No") #define all(a) begin(a),end(a) #define rall(a) rbegin(a),rend(a) #define delif(c,pred) (c).erase(remove_if(all(c),(pred)), end(c)) template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;} template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;} inline constexpr int Mod = 1000000007; //inline constexpr int Mod = 998244353; #define dbg(a) cerr << #a << ": " << a << endl; #define dbgs(s) cerr << #s << endl; #define dbg1(a,n) cerr<<#a<<": "; rep(i,n) cerr<<a[i]<<" "; cerr<<endl; #define dbg2(m,h,w) cerr<<#m<<":"<<endl; rep(i,h){ rep(j,w)cerr<<m[i][j]<<" "; cerr<<endl; } template <class T, class U> ostream& operator << (ostream& os, pair<T,U> v) {os<<v.first<<","<<v.second;return os;} template <class T, size_t N> ostream& operator << (ostream& os, array<T,N> v) {rep(i,N)os<<v[i]<<(i+1<(ll)N?" ":"");return os;} int rng(int n) { return rand()/(RAND_MAX+1.0)*n; } ll dp[1<<18]; int main() { fastio; ll ans{}; ll N,M; cin >> N >> M; vector<vector<array<ll,2>>> m(N+1); rep(i,M) { ll x,y,z; cin >> x >> y >> z; m[x].push_back({y,z}); } dp[0] = 1; rep(p,1<<N) { rep(j,N) { if (p>>j&1) continue; int to = p|(1<<j); dp[to] += dp[p]; for (auto [y,z]:m[__builtin_popcount(p)+1]) { int c = to & ((1<<y)-1); if (__builtin_popcount(c) > z) { dp[to] = 0; break; } } } } ans = dp[(1<<N)-1]; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; static const int INF=1000000007; #define rep(i,n) for(int i=0;i<n;i++) int A[200010]; int dp[200010]; int main(){ int n,m; cin>>n>>m; rep(i,n) cin>>A[i]; vector<int>edge[n]; rep(i,m){ int a,b; cin>>a>>b; a--; b--; edge[a].push_back(b); } rep(i,n) dp[i]=INF; for(int i=0;i<n;i++){ for(auto j:edge[i]){ dp[j]=min(dp[j],min(dp[i],A[i])); } } int ans=-INF; rep(i,n){ if(dp[i] != INF){ ans=max(ans,A[i]-dp[i]); } } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #define re register #define mod 998244353 #define eps (1e-10) #define int long long using namespace std; #define N 100010 int n; double f[N]; template <class T> inline void read(T &x) { x=0;int g=1;char ss=getchar(); for (;ss>'9'||ss<'0';ss=getchar()) if (ss=='-') g=-1; for (;ss<='9'&&ss>='0';ss=getchar()) x=(x<<1)+(x<<3)+(ss^48); x*=g; } signed main() { read(n); f[1]=0; for(int i=2;i<=n;i++) { f[i]=f[i-1]*(n-i+1)/n+1; f[i]/=1-(i-1.0)/n; } printf("%.8lf",f[n]); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; int main(){ int N; cin >> N; double sum = 0; for(int i = 1; i < N; i++){ sum += double(1) / i; } cout << fixed << setprecision(7); cout << N * sum << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; vector<int> x(200001, 0); for (int i = 1; i <= b; ++i) { x[i] = b / i - (a - 1) / i; } int ret = 1; for (int i = 1; i < 200001; ++i) { if (x[i] >= 2) { if (ret < i) ret = i; } } cout << ret << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <array> #include <queue> #include <cstring> #include <unordered_map> #include <set> #include <climits> #include <cmath> using namespace std; #define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define MOD 1000000007 #define MAX 100005 using ll = long long int; #define all(x) (x).begin(), (x).end() const int N = 1e3 + 1; int main() { speed; ll a, b; cin >> a >> b; ll total = 2 * a + 100; cout << total - b << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; int solve(int nkr, int b, int a, int i) { if (nkr == 0) return 0; int cur = 1; while (true) { int we = nkr / cur; if (we < a) return -1; else if (we < b) return cur; cur++; } } int main() { int a, b, w; cin >> a >> b >> w; int w1000 = w * 1000; int INF =1001001001; int mi=INF; int bmax = w1000 / b; for (int i = bmax; i >= 0; --i) { int sol = solve(w1000 - i * b, b, a, i); if (sol != -1) { mi = i + sol; break; } } int ma; int amax = w1000 / a; for (int i = amax; i >= 0; --i) { int sol = solve(w1000 - i * a, b, a, i); if (sol != -1) { ma = i + sol; break; } } // if (w1000/a == w1000/b) cout << "UNSATISFIABLE" << endl; if (mi==INF) cout << "UNSATISFIABLE" << endl; else cout << mi << " " << ma << endl; }
typedef long long ll; typedef long double ld; #include <bits/stdc++.h> using namespace std; int main() { ld a,b,c,d; std::cin >> a>>b>>c>>d; d *= -1; std::cout <<setprecision(20)<< -b*(c-a)/(d-b)+a << std::endl; }
#include <bits/stdc++.h> #include <chrono> using namespace std; using namespace chrono; typedef long long int ll; typedef vector<int> vii; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define pb push_back #define odd(x) ((x)&1) #define even(x) (!odd(x)) #define all(v) (v).begin(),(v).end() #define rep(i,n) for(auto i=0;i<n;++i) #define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define TEST_CASE int tc;cin>>tc;while(tc--) #define Clock high_resolution_clock::now() template<class T> inline bool chmax(T &a, T b) {if(a < b) {a = b;return true;}return false;} template<class T> inline bool chmin(T &a, T b) {if(a > b) {a = b;return true;}return false;} #ifdef LOCAL #define cerr cout #else #endif #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // #define TIME /******************************************************************************************************************************/ const ll inf = 2e18; const ll mod = 998244353; const ll N = 1e5+3; int main() { auto start_time = Clock; FASTIO ll n,m; cin >> n >> m; vector<string> s(n); vector<vll> mp(n+m+1); rep(i,n) { cin >> s[i]; rep(j,m) { ll x = i+j; if(s[i][j] == 'R') mp[x].pb(1); else if(s[i][j] == 'B') mp[x].pb(-1); else mp[x].pb(0); } } ll ans = 1, ok = 1; rep(i,n+m+1) { ll R,B,D; R = B = D = 0; for(auto j: mp[i]) { if(j==1) ++R; else if(j==0) ++D; else ++B; } // if(i==2) trace(R,B,D); if(R>0 && B>0) ok = 0; else if(D>0 && R==0 && B==0) {ans *= 2;ans %= mod;} } if(!ok) ans = 0; cout << ans << "\n"; auto end_time = Clock; #ifndef TIME return 0; #endif cout << "\nTime elapsed: " << (double)duration_cast<milliseconds>(end_time-start_time).count() << "ms"; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for (int i = 0; i < (n); ++i) #define DREP(i,s,n) for(int i = (s); i < (n); i++) 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;} using ll = long long; using P = pair<int,int>; using Pl = pair<long long,long long>; using veci = vector<int>; using vecl = vector<long long>; using vecveci = vector<vector<int>>; using vecvecl = vector<vector<long long>>; const int MOD = 998244353; const double pi = acos(-1); ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);} ll lcm(ll a, ll b) {return a*b/gcd(a,b);} long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { int H,W; cin >> H >> W; vector<string> S(H); REP(i,H) cin >> S[i]; int cnt = 0; REP(j,W) { set<char> st; for(int i = 0,k = j; i < H && k >= 0; i++,k--) { st.insert(S[i][k]); } if(st.count('R') && st.count('B')) { cout << 0 << endl; return 0; } if(st.size() == 1 && st.count('.')) cnt++; } REP(i,H) { if(i == 0) continue; set<char> st; for(int k = i,j = W-1; k < H && j >= 0; k++,j--) { st.insert(S[k][j]); } if(st.count('R') && st.count('B')) { cout << 0 << endl; return 0; } if(st.size() == 1 && st.count('.')) cnt++; } cout << modpow(2LL,cnt,MOD) << endl; return 0; }
#include<bits/stdc++.h> #define ll long long int using namespace std; #define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define mp make_pair #define fi first #define se second #define pb push_back #define endl "\n" #define maxpq priority_queue<ll> #define minpq priority_queue<ll, vector<ll>, greater<ll> > #define vi vector<ll> #define pii pair<ll, ll> #define vii vector<pii> #define for0(i, n) for (int i = 0; i < n; i++) #define for1(i, n) for (int i = 1; i <= n; i++) #define loop(i,a,b) for (int i = a; i < b; i++) #define bloop(i,a,b) for (int i = a ; i>=b;i--) #define MOD 1000000007 #define INT_MAXI 1000000000000000000 #define INT_MINI -9000000000000000000 ll max(ll a, ll b) {if (a > b) return a; else return b;} ll min(ll a, ll b) {if (a < b) return a; else return b;} const int dx[4] = { -1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; char arr[1005][1005]; int main() { fio; ll t,n,i,j,k,x,l,r,b,h,y,m,a,c,d; cin>>a>>b>>c>>d; double val=d*1.0/a; if(val>=b&&val<=c) { cout<<"No"; } else cout<<"Yes"; }
#include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <limits> #include <string> using namespace std; int main() { int V, T, S, D; cin >> V >> T >> S >> D; cout << ((V * T > D || (V * T + (V * (S - T)) < D)) ? "Yes" : "No") << endl; return 0; }
/* cerberus97 - Hanit Banga */ #include <iostream> #include <iomanip> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <algorithm> #include <numeric> using namespace std; #define pb push_back #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL) typedef long long ll; typedef long double ld; typedef pair <int, int> pii; typedef pair <ll, ll> pll; const int N = 1e5 + 10, mod = 1e9 + 7; int add(int a, int b) { return (a + b) % mod; } int mul(ll a, int b) { return (a * b) % mod; } int main() { fast_cin(); int n; cin >> n; char caa, cab, cba, cbb; cin >> caa >> cab >> cba >> cbb; if (cab == 'B') { cab ^= 'A' ^ 'B'; cba ^= 'A' ^ 'B'; tie(caa, cbb) = make_pair(cbb ^ 'A' ^ 'B', caa ^ 'A' ^ 'B'); } if (caa == 'A') { cout << 1 << '\n'; } else { if (cba == 'A') { int m = n - 2, a = 1, b = 1; while (m--) { tie(a, b) = make_pair(b, add(a, b)); } cout << a << '\n'; } else { int p2 = 1; for (int i = 0; i < n - 3; ++i) { p2 = mul(p2, 2); } cout << p2 << '\n'; } } }
#include <cmath> #include <iostream> #include <string> #include <algorithm> #include <vector> #include <queue> #include <map> #include<cstdio> #include<functional> #include <bitset> #include <iomanip> #include <cctype> #include <list> #include <cassert> #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for(ll i = n; i >= 0; i--) #define ll long long #define repi(i,a,b) for(ll i=a;i<b;++i) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() using namespace std; 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; } const ll INF = 1000000000000000000; const ll MOD = 1e9 + 7; using vvv = vector<vector<vector<ll>>>; using vv = vector<vector<ll>>; using vec = vector<ll>; using P = pair<ll, ll>; int main() { ll a, b, c; cin >> a >> b >> c; if (c == 0) { if (a > b) cout << "Takahashi" << endl; else cout << "Aoki" << endl; } else { if (b > a) cout << "Aoki" << endl; else cout << "Takahashi" << endl; } system("Pause"); }
//#include<math.h> #include<algorithm> #include<stdlib.h> #include<time.h> #include<stdio.h> #include<string.h> #define un unsigned #define srd srand(time(0)) #define ll long long #define con continue #define gtc getchar() #define ptc putchar #define dou double #define eps 0.00000000001 #define opr operator #define cl(x,a) memset(x,a,sizeof(x)) #define fo0(i,k) for(i=fr[k];i;i=nx[i]) #define fo1(i,l,r) for(i=l;i<=r;i++) #define fo2(i,l,r) for(i=l;i>=r;i--) #define fo(i,n) for(i=1;i<=n;i++) #define ret return #define x first #define cint const int #define y second #define opi(x) freopen(x,"r",stdin) #define opo(x) freopen(x,"w",stdout) #define tpl template<class T> #define priq priority_queue #define mp make_pair #define use using namespace #define WT while(T--) use std; typedef pair<int,int> pii;typedef pair<int,ll> pil;typedef pair<ll,int> pli;typedef pair<ll,ll> pll; namespace io { void _(int &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;} void _(ll &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;} void _(char &c){while((c=gtc)==' '||c=='\n');}void _(dou &c){scanf("%lf",&c);}void _(char *s){char c;while((c=gtc)!=EOF&&c!=' '&&c!=10)*s++=c;} template<class t1,class t2>void _(t1 &a,t2 &b){_(a);_(b);}template<class t1,class t2,class t3>void _(t1 &a,t2 &b,t3 &c){_(a);_(b);_(c);} template<class t1,class t2,class t3,class t4>void _(t1 &a,t2 &b,t3 &c,t4 &d){_(a);_(b);_(c);_(d);} template<class t1,class t2,class t3,class t4,class t5>void _(t1 &a,t2 &b,t3 &c,t4 &d,t5 &e){_(a);_(b);_(c);_(d);_(e);} void _p(dou k){printf("%.6lf",k);}void _p(char *c){for(;*c;ptc(*c++));}void _p(const char *c){for(;*c;ptc(*c++));}void _p(char c){ptc(c);} tpl void _p0(T k){if(k>=10)_p0(k/10);ptc(k%10+'0');}tpl void _p(T k){if(k<0){ptc('-');_p0(-k);}else _p0(k);}tpl void __p(T k){_p(k);ptc(' ');} tpl void _pn(T k){_p(k);ptc('\n');}template<class t1,class t2>void _p(t1 a,t2 b){__p(a);_pn(b);} template<class t1,class t2,class t3>void _p(t1 a,t2 b,t3 c){__p(a);__p(b);_pn(c);} template<class t1,class t2,class t3,class t4>void _p(t1 a,t2 b,t3 c,t4 d){__p(a);__p(b);__p(c);_pn(d);} tpl void op(T *a,int n){int i;n--;fo(i,n)__p(a[i]);_pn(a[n+1]);}int gi(){int x;_(x);ret x;}ll gll(){ll x;_(x);ret x;} } int gcd(int a,int b){ret b?gcd(b,a%b):a;}void fcl(){fclose(stdin);fclose(stdout);} void fop(const char *s){char c[256],d[256];cl(c,0);cl(d,0);strcpy(c,s);strcpy(d,s);opi(strcat(c,".in"));opo(strcat(d,".out"));} int eq(dou a,dou b){return a+eps>=b&&b+eps>=a;}tpl void _ma(T &a,T b){if(a<b)a=b;}tpl void _mi(T &a,T b){if(a>b)a=b;} cint N=1234567,EE=100000000,GG=1000000000,ima=2147483647; use io; int n,m,a[N],f[N],T; char an[666][666]; void ck(int k,int x,int y,int b) { if(k==0) an[x][y]='A'+b; else { ck(k-1,x,y,b); ck(k-1,x+(1<<(k-1)),y,b); ck(k-1,x,y+(1<<(k-1)),b); ck(k-1,x+(1<<(k-1)),y+(1<<(k-1)),b^1); } } int main() { int i,j,a1,a2; _(n); ck(n,0,0,0); _pn((1<<n)-1); fo(i,(1<<n)-1) puts(an[i]); }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { int n; cin >> n; if (n == 1) { cout << "Yes\n"; return 0; } vector<int> a(n), b(n); REP(i, n) cin >> a[i] >> b[i]; for (int i = n - 1; i >= 0; --i) { a[i] -= a.front(); b[i] -= b.front(); } int dist1 = a[1] * a[1] + b[1] * b[1]; set<pair<int, int>> s; REP(i, n) s.emplace(a[i], b[i]); vector<int> c(n), d(n); REP(i, n) cin >> c[i] >> d[i]; REP(r, n) { vector<int> x(n), y(n); REP(i, n) { x[i] = c[i] - c[r]; y[i] = d[i] - d[r]; } REP(i, n) { if (i == r || x[i] * x[i] + y[i] * y[i] != dist1) continue; double theta = atan2(b[1], a[1]) - atan2(y[i], x[i]), co = cos(theta), si = sin(theta); bool is_valid = true; REP(j, n) { double nx = x[j] * co - y[j] * si, ny = x[j] * si + y[j] * co; if (abs(round(nx) - nx) > EPS || abs(round(ny) - ny) > EPS || s.count({static_cast<int>(round(nx)), static_cast<int>(round(ny))}) == 0) { is_valid = false; break; } } if (is_valid) { cout << "Yes\n"; return 0; } } } cout << "No\n"; return 0; }
#include <bits/stdc++.h> # define INF32 2147483647 # define INF64 9223372036854775807 # define rep(i,l,n) for(int i=l,i##_end=n;i<i##_end;++i) # define rrep(i,n,l) for(int i=((int)(n)-1),i##_end=l;i>=i##_end;--i) # define erep(i,n) for(auto &i : n) # define ALL(x) (x).begin(), (x).end() # define SZ(x) ((int)(x).size()) # define pb push_back 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; } int main() { // 1. input string S; cin>>S; // 2. bool ans = true; rep(i,0,S.size()){ if( (i%2 == 0 && isupper(S[i])) || (i%2 == 1 && !isupper(S[i])) ) { ans = false; break; } } // 3. output cout << (ans?"Yes":"No") << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; int flag=0; for(int i=0;i<s.length();i++) { if( (i%2==0) ) { if(s[i]>= 'a' && s[i]<= 'z') flag=1; else {flag=0; break;} } else if(i%2==1) { if(s[i]>= 'A' && s[i]<= 'Z') flag=1; else {flag=0; break;} } } if(flag==1) cout<<"Yes\n"; else cout<<"No\n"; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i] #define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep_up(i, a, n) for (ll i = a; i < n; ++i) #define rep_down(i, a, n) for (ll i = a; i >= n; --i) #define P pair<ll, ll> #define all(v) v.begin(), v.end() #define fi first #define se second #define vvvll vector<vector<vector<ll>>> #define vvll vector<vector<ll>> #define vll vector<ll> #define pqll priority_queue<ll> #define pqllg priority_queue<ll, vector<ll>, greater<ll>> constexpr ll INF = (1ll << 60); // constexpr ll mod = 1000000007; constexpr ll mod = 998244353; constexpr double pi = 3.14159265358979323846; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> void pt(T val) { cout << val << "\n"; } template <typename T> void pt_vll(vector<T> &v) { ll vs = v.size(); rep(i, vs) { cout << v[i]; if (i == vs - 1) cout << "\n"; else cout << " "; } } ll mypow(ll a, ll n) { ll ret = 1; if(n==0) return 1; if(a==0) return 0; rep(i, n) { if (ret > (ll)(1e18 + 10) / a) return -1; ret *= a; } return ret; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long 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 dig(ll n) {//桁数 ll res = 0; while(n > 0) { res ++; n /= 10; } return res; } vector<bool> seen; void dfs(vvll &G, ll v){ seen[v]=true; for(auto next:G[v]){ if(seen[next]) continue; dfs(G,next); } } int main() { ll n,m,x,ans=1; cin>>n>>m; vll A(n); vll X(m+1,1); rep(i,n){ if(i==0){ A[0]=m; continue; } ll sum=0; rep(j,m){ A[i]+=mod+A[i-1]+modpow(m,i,mod)-X[j]; A[i]%=mod; X[j]=(m-j-1)*X[j]+modpow(m,i,mod); X[j]%=mod; } } cout<<A[n-1]%mod<<endl; }
#include<bits/stdc++.h> using namespace std; long long f[5001][5001]; const int mod=998244353; int main() { int n,m; cin>>n>>m; for (int i=0;i<=m;i++){ f[i][0]=1; for (int j=1;j<=n;j++){ f[i][j]=f[i][j-1]*1ll*i%mod; } } long long res=1ll*n*f[m][n]%mod; for (int i=1;i<=m;i++){ for (int len=2;len<=n;len++){ int pos=n-len+1; int in=f[m-i][len-2]; int out=f[m][n-len]; res=(res+mod-pos*1ll*in%mod*1ll*out)%mod; } } cout<<res<<endl; return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define mod ((int)1e9+7) #define lim 1000000000000000007 #define lim1 18446744073709551615 //Unsigned #define sq(a) ((a)*(a)) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define mms(v,i) memset(v,i,sizeof(v)) #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define REP(i,a,b) for (int i = a; i <= b; i++) #define REPN(i,a,b) for (int i = a; i >= b; i--) using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef pair<int,int> pi; typedef pair<ll,ll> PL; typedef pair<ll,int> PLI; typedef pair<int,ll> PIL; typedef pair<int,pair<int,int> > pii; typedef pair<double,double> pdd; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; ll power(ll a,ll b) { ll res=1; while(b){ if(b%2==1) res=(res*a)%mod; a=(a*a)%mod; b>>=1; } return res; } ll gcdll(ll a,ll b) { if (b==0) return a; return gcdll(b,a%b); } int gcd(int a,int b) { if (b==0) return a; return gcd(b,a%b); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; const int N = (int)2e5+5; const int M = (int)3e3+5; const int Q = 301; const int logN = 19; void solve() { int n; cin>>n; if(n&1) cout << "Black"; else cout << "White"; } int main() { //freopen("output.txt","r",stdin); //freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T=1; // cin>>T; REP(TC,1,T) { //cout<<"Case #"<<TC<<": "; solve(); cout<<"\n"; } }
#include<iostream> using namespace std; int main(void) { int n; cin>>n; if(n&1) puts("Black"); else puts("White"); return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,s,n) for (int i = (s); i < (n); ++i) #define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define len(x) (int)(x).size() #define dup(x,y) (((x)+(y)-1)/(y)) #define pb push_back #define eb emplace_back #define Field(T) vector<vector<T>> #define pq(T) priority_queue<T,vector<T>,greater<T>> using namespace std; using ll = long long; using P = pair<int,int>; int main() { int a, b, c, d; cin >> a >> b >> c >> d; cout << min({a, b, c, d}) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MAXN 200005 typedef long long ll; typedef unsigned long long ull; ull Max = 0x3f3f3f3f3f3f3f3f; ull dist[MAXN]; typedef struct { ull to; ull t, k; } Node; vector<Node> vec[MAXN]; ull N, M, X, Y; void min_len2() { using Pair = pair<ull, ull>; for (ull i = 1; i <= N; i++) dist[i] = Max; dist[X] = 0; priority_queue<Pair, vector<Pair>, greater<Pair>> q; // 小根堆 q.push(Pair(0, X)); while (!q.empty()) { auto top = q.top(); auto tick = top.first; auto node = top.second; if (node == Y && tick != 0) return; q.pop(); for (auto &x : vec[node]) { ull d = tick + (x.k - tick % x.k) % x.k + x.t; // dist + wait + travel time if (d < dist[x.to]) { dist[x.to] = d; q.push(Pair(dist[x.to], x.to)); } } } } int main() { ios::sync_with_stdio(false); cin >> N >> M >> X >> Y; for (ull i = 0; i < M; i++) { ull a, b, t, k; cin >> a >> b >> t >> k; vec[a].push_back({b, t, k}); vec[b].push_back({a, t, k}); } min_len2(); if (dist[Y] >= Max) cout << "-1" << endl; else cout << dist[Y] << endl; return 0; }
#include<iostream> using namespace std; int main() { int a,b,c,d; cin>>a>>b>>c>>d; cout<<a*d-b*c<<endl; return 0; }
//Bismillahirrahmanirrahim #pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long lo; typedef pair< lo,lo > PII; #define fi first #define se second #define mp make_pair #define endl "\n" #define pb push_back #define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define FOR for(int i=1;i<=n;i++) #define mid ((start+end)/2) #define ort ((bas+son)/2) const lo inf = 1000000000000000000; const lo KOK = 100000; const lo LOG = 30; const lo li = 500005; const lo mod = 1000000007; int n,m,b[li],a[li],k,flag,t; int cev; string s; vector<int> v; int main(void){ scanf("%d %d %d %d",&n,&m,&k,&t); printf("%d\n",n*t-m*k); return 0; }
// C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using LL = long long; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) const LL MOD = 998244353; int board[1010][1010]; // マスの色(0: 色なし, 1: 赤, 2: 青). int cnt[1010][3]; // 出現回数(0: 色なし, 1: 赤, 2: 青). int main(){ // 1. 入力情報. int H, W; scanf("%d %d", &H, &W); rep(i, H){ char c[505]; scanf("%s", c); rep(j, W){ if(c[j] == 'R') board[i][j] = 1; if(c[j] == 'B') board[i][j] = 2; } } // 2. 右上から左下に向かって, マスの色を確認. rep(k, H + W - 1){ rep(i, H + W - 1){ int j = k - i; if(j >= 0) cnt[k][board[i][j]]++; else break; } } // rep(i, H + W - 1) printf("n=%d r=%d b=%d\n", cnt[i][0], cnt[i][1], cnt[i][2]); // 3. 色なしのみをカウント. // -> 赤, 青が混在している場合は, 着色不可能と判定. // -> 開始地点, 終了地点も, '.' となる場合があるので, 注意. LL ans = 1; int noColor = 0; rep(i, H + W - 1){ if(cnt[i][1] && cnt[i][2]) ans = 0; if(!cnt[i][1] && !cnt[i][2]) noColor++; } // 4. 塗り方の総数は? rep(i, noColor) ans <<= 1, ans %= MOD; // 5. 出力. printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for ((i)=1;(i)<=(n);(i)++) using namespace std; const int mod=998244353; int n,m,i,j,s=1; char c[505][505]; int main(){ cin>>n>>m; rep(i,n)rep(j,m)cin>>c[i][j]; for(i=1+1;i<=n+m;i++){ bool b=0,r=0; rep(j,n){ int k=i-j; if(1<=k&&k<=m){ if(c[j][k]=='B')b=1; if(c[j][k]=='R')r=1; } } s=1ll*s*(2-b-r)%mod; } cout<<s<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long int /**********************************************/ void solve() { } 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 = 1; // cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; if (a > b || (a == b && c == 1)) { cout << "Takahashi" << endl; } else if (a < b || (a == b && c == 0)) { cout << "Aoki" << endl; } } }
// Winners don't quit and Quitters don't win #include<bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define pii pair<int,int> #define pll pair<ll,ll> #define vii vector<int> #define vll vector<ll> #define mod 998244353 #define MAX 100005 int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,w,i,x,y,p,m = -100; ll idx[2*MAX]; for(i=0;i<=200005;++i){ idx[i] = 0; } cin>>n>>w; for(i=0;i<n;++i){ cin>>x>>y>>p; idx[x] += p; idx[y] -= p; m = max(m,y); } for(i=1;i<=m;++i){ idx[i] += idx[i-1]; } for(i=0;i<=m;++i){ if(idx[i] > w){ cout<<"No"; return 0; } } cout<<"Yes"; }
/* 设dp[i][j]为当前算到tak拿了i个物品,它的和为j的方案数。 */ #include<cstdio> const long long mod=998244353; long long n,w[110],sum,dp[110][10010],power[110],ans; int main() { scanf("%lld",&n); for(long long i=1;i<=n;++i) scanf("%lld",&w[i]); dp[0][0]=1; power[0]=1; for(long long i=1;i<=n;++i) { power[i]=(power[i-1]*i)%mod; for(long long j=i-1;j>=0;--j) { for(long long k=0;k<=sum;++k) dp[j+1][k+w[i]]=(dp[j+1][k+w[i]]+dp[j][k])%mod; } sum+=w[i]; } if(sum%2==0) { for(long long i=1;i<=n;++i) ans=(ans+((power[i]*power[n-i])%mod)*dp[i][sum/2])%mod; } printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> const int N = 105, P = 998244353; int n, a[N], sum = 0, f[N][N * N], g[N][N * N], fac[N]; // 1:当前权值;2:<0次数 int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += a[i]; if (sum & 1) { puts("0"); return 0; } fac[1] = 1; for (int i = 2; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % P; f[0][0] = 1; for (int i = 1; i <= n; i++) { memset(g, 0, sizeof g); for (int j = 0; j <= i; j++) for (int k = 0; k <= i * 100; k++) { g[j][k] = f[j][k]; if (j && k >= a[i]) g[j][k] = (g[j][k] + f[j - 1][k - a[i]]) % P; } memcpy(f, g, sizeof g); } int ans = 0; for (int i = 1; i <= n; i++) ans = (ans + 1ll * fac[i] * fac[n - i] % P * g[i][sum / 2]) % P; printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define pb(x) push_back(x) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define F(w,x,y) for(ll w=x; w<y; w++) #define endl "\n" #define mod 1000000007 typedef long long ll; typedef long double ld; using namespace std; void play() { ll n,a; cin>>n; vector <ll> pref(n+1); pref[0]=0; vector <ll> maxi(n+1,0); maxi[0]=0; F(i,1,n+1) { cin>>a; pref[i]=pref[i-1]+a; maxi[i]=max(maxi[i-1],a); } ll sum=0; F(i,1,n+1) { sum+=pref[i]; cout<<sum+(maxi[i]*i)<<endl; } } int main() { //freopen("milkvisits.in","r",stdin); //freopen("milkvisits.out","w",stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); //pre(); ll t; t=1; //cin>>t; F(i,1,t+1) { //cout<<"Case #"<<i<<": "; play(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; int n,a[200010]; long long sum[200010],ma; void solve() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; for(int i=1;i<=n;i++) sum[i]=sum[i-1]+sum[i]; for(int i=1;i<=n;i++) { ma=max(ma,(long long)a[i]); printf("%lld\n",sum[i]+1ll*ma*i); } } int main() { int t=1;//scanf("%d",&t); while(t--) solve(); }
#include<bits/stdc++.h> #define MAXN 50+10 #define MAXM 2500+10 using namespace std; const int n = 50; int si,sj; string Fs = "RDLU"; int Fx[10] = {0,1,0,-1}; int Fy[10] = {1,0,-1,0}; int Soc[MAXM]; bool vis[MAXM]; int t[MAXN][MAXN]; int p[MAXN][MAXN]; string Move,s; int Now = 0; int Ans = 0; const int Time_Limit = (int) (1.9 * CLOCKS_PER_SEC); void DFS(int x,int y) { if(Now > Ans) { Move = s; Ans = Now; } if((int)clock() > Time_Limit) { cout << Move; exit(0); } vis[t[x][y]] = true; Now += Soc[t[x][y]]; for(int i=0;i<4;i++) { int nx = x + Fx[i]; int ny = y + Fy[i]; if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue; if(vis[t[nx][ny]]) continue; s += Fs[i]; DFS(nx,ny); s.pop_back(); } vis[t[x][y]] = false; Now -= Soc[t[x][y]]; } int main() { scanf("%d%d",&si,&sj); for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d",&t[i][j]); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { scanf("%d",&p[i][j]); Soc[t[i][j]] += p[i][j]; } DFS(si,sj); cout << Move; //cout << Ans; }
#include<bits/stdc++.h> using namespace std; int n,t,a[102],ans; set<int> q; void Dfs1(int dep,int sum){ if(sum>t) return; if(dep==(n>>1)+1){ q.insert(sum); return; } Dfs1(dep+1,sum), Dfs1(dep+1,sum+a[dep]); } void Dfs2(int dep,int sum){ if(sum>t) return; if(dep==n+1){ set<int>::iterator i=q.upper_bound(t-sum); --i; ans=max(ans,sum+(*i)); return; } Dfs2(dep+1,sum), Dfs2(dep+1,sum+a[dep]); } int main(){ scanf("%d%d",&n,&t); for(int i=1;i<=n;++i) scanf("%d",&a[i]); Dfs1(1,0); Dfs2((n>>1)+1,0); printf("%d\n",ans); }
// D - RGB Coloring 2 // sample code // reference: https://atcoder.jp/contests/abc199/submissions/22053625 #include <bits/stdc++.h> using namespace std; #define vec vector using vi = vec<int>; using ll = int64_t; #define rep(i,e) for(int i=0;i<(e);++i) vec<vi> G; vi IX; //彩色順を記録。違う連結成分なら同じ index もある ll dfs(int now, vi&v){ ll res = 0; for(int to:G[now]) if(v[to] == v[now]) return res; // 隣接色かぶり res = 1; for(int to:G[now]){ // 隣接点 if(IX[to] == -1) IX[to] = IX[now] + 1; // 未訪問->彩色順を採番 if(IX[to] != IX[now] + 1) continue; // 彩色順どおりでない->訪問済み ll tmp = 0; rep(i, 2){ v[to] = (v[now] + i+1) % 3; // 隣接点を彩色 tmp += dfs(to, v); // 隣接点へ } v[to] = -1; // 無色に戻す res *= tmp; if (tmp == 0) break; // tmp == 0 の時、早期に return してるので // IX が振り切れていない事に注意 } return res; } int main(){ int n, m; cin>>n>> m; G.resize(n); rep(i, m){ int a, b; cin>>a>>b; --a, --b; G[a].push_back(b); G[b].push_back(a); } IX.resize(n, -1); vi v(20, -1); // 彩色された点 ll ans = 1; rep(i, n){ if(IX[i] != -1) continue; // 採番済み->訪問済み IX[i] = v[i] = 0; // 開始点の訪問済み&彩色 ans *= dfs(i, v) * 3; } cout<< ans <<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> iint; typedef pair<ll,ll> llll; #define ALL(x) (x).begin(),(x).end() const ll zero = 0; const ll one = 1; const ll INF = 9223372036854775807; //10^18 const int inINF = 2147483647; //10^9 const ll MOD = 1000000007; //10^9+7 const ll MOD2 = 998244353; void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} int main(){ ll N, L; cin >> N >> L; vector<ll> A(N+3), B(N+2); for (ll i = 0; i < N; i++) { cin >> A[i+1]; } for (ll i = 0; i < N; i++) { cin >> B[i+1]; } A[0] = 0; A[N+1] = L+1; A[N+2] = L+3; B[N+1] = L+3; vector<ll> J(N+2); ll tmpj = 0; for (int i = 1; i <= N; i++) { while(tmpj <= N+1){ if(B[i] == A[tmpj] - (tmpj - i)){ J[i] = tmpj; break; } tmpj++; } } if(tmpj == N+2){ printf("%d\n", -1); return 0; } J[N+1] = N+3; ll ans = 0; J[0] = -1; for (ll i = 1; i <= N; i++) { if(J[i] == J[i-1]) continue; ll mi, ma; mi = min(i, J[i]); ma = max(i, J[i]); ll tmpi = i; while(J[tmpi] == J[i]){ mi = min(mi, tmpi); ma = max(ma, tmpi); tmpi++; } ans += ma - mi; // printf("%lld %lld %lld\n", J[i], mi, ma); ll tmp = 0; tmpi = J[i] - 1; while(tmpi >= mi && A[tmpi] + 1 == A[tmpi+1]){ tmp++; tmpi--; } tmpi = J[i] + 1; while(tmpi <= ma && A[tmpi-1] + 1 == A[tmpi]){ tmp++; tmpi++; } // for (int j = mi; j < ma+1; j++) { // if(j == J[i]) continue; // if(A[j] == B[j]){ // tmp++; // } // } ans -= tmp; } // for (int i = 1; i <= N; i++) { // printf("%lld\n", J[i]); // } printf("%lld\n", ans); }
// #define LOCAL #define _USE_MATH_DEFINES #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <vector> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <complex> #include <cmath> #include <numeric> #include <bitset> #include <functional> #include <random> #include <ctime> using namespace std; template <typename A, typename B> ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } template <class T> auto vect(const T& v, int n) { return vector<T>(n, v); } template <class T, class... D> auto vect(const T& v, int n, D... m) { return vector<decltype(vect(v, m...))>(n, vect(v, m...)); } typedef long long int64; typedef pair<int, int> ii; #define SZ(x) (int)((x).size()) template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2; const int MOD = 1e9 + 7; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; int main() { int cas; cin >> cas; while (cas--) { int n; cin >> n; vector<int> a(n), pos(n); for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; pos[a[i]] = i; } if (n == 2) { if (a[0] == 0) { cout << 0 << '\n'; cout << '\n'; } else { cout << 1 << '\n'; cout << 1 << '\n'; } continue; } vector<int> ret; auto add = [&](int x) { ret.push_back(x); swap(pos[a[x]], pos[a[x + 1]]); swap(a[x], a[x + 1]); }; int k = 0; for (int i = n - 1; i >= 0; --i) { trace(a, pos, ret, i); if (i == 2) { while (a[0] != 0 || a[1] != 1 || a[2] != 2) { add(k); k ^= 1; } break; } if (pos[i] == i) continue; while (pos[i] % 2 != k) { add(k); k ^= 1; } int j = pos[i]; for (int t = j; t < i; ++t) { add(t); k ^= 1; } } cout << SZ(ret) << '\n'; for (int i = 0; i < SZ(ret); ++i) { cout << ret[i] + 1 << " \n"[i + 1 == SZ(ret)]; } if (SZ(ret) == 0) cout << '\n'; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define pb push_back #define COUT(x) cout << (x) << "\n" #define COUTF(x) cout << setprecision(15) << (x) << "\n" #define ENDL cout << "\n" #define DF(x) x.erase(x.begin()) #define ALL(x) x.begin(), x.end() #define SORT(x) sort(ALL(x)) #define RSORT(x) sort(x.rbegin(), x.rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MAXI(x) max_element(ALL(x)) - x.begin() #define SUM(x) accumulate(ALL(x), 0ll) #define COUNT(x, y) count(ALL(x), y); #define ANS cout << ans << "\n" #define YES cout << "YES\n"; #define NO cout << "NO\n"; #define Yes cout << "Yes\n"; #define No cout << "No\n"; #define init() \ cin.tie(0); \ ios::sync_with_stdio(false) #define debug(x) cerr << "[debug] " << #x << ": " << x << endl; #define debugV(v) \ cerr << "[debugV] " << #v << ":"; \ rep(z, v.size()) cerr << " " << v[z]; \ cerr << endl; using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vector<ll>>; using mll = map<ll, ll>; using qll = queue<ll>; using P = pair<ll, ll>; using vp = vector<P>; using vs = vector<string>; template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { rep(j, v.size()) i >> v[j]; return i; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; ll get_digit(ll x) { return to_string(x).size(); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } template <class T> void chmin(T& a, const T& b) { if (b < a) a = b; } signed main() { init(); ll N; string S; cin >> N >> S; if (S[0] != S[N - 1]) { COUT(1); return 0; } bool ok = false; char c = S[0]; rep2(i, 1, N - 1) { if (S[i] != c and S[i + 1] != c) ok = true; } if (ok) { COUT(2); } else { COUT(-1); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep_up(i, a, b) for (ll i = a; i < b; ++i) #define rep_down(i, a, b) for (ll i = a; i > b; --i) #define P pair<ll, ll> #define Graph vector<vector<ll>> #define fi first #define se second #define vvvvll vector<vector<vector<vector<ll>>>> #define vvvll vector<vector<vector<ll>>> #define vvll vector<vector<ll>> #define vll vector<ll> #define vvvvdo vector<vector<vector<vector<double>>>> #define vvvdo vector<vector<vector<double>>> #define vvdo vector<vector<double>> #define vdo vector<double> #define pqll priority_queue<ll> #define pqllg priority_queue<ll, vector<ll>, greater<ll>> constexpr ll INF = (1ll << 60); // constexpr ll mod = 998244353; constexpr ll mod = 1000000007; constexpr double pi = 3.14159265358979323846; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void pt_vvll(vvll v) { ll vs = v.size(), vs0 = v[0].size(); rep(i, vs) { rep(j, vs0) { if (v[i][j] != INF) cout << v[i][j]; else cout << -1; if (j != vs0 - 1) cout << " "; else cout << "\n"; } } } void pt_vll(vll v) { ll vs = v.size(); rep(i, vs) { cout << v[i]; if (i == vs - 1) cout << "\n"; else cout << " "; } } void make_prime(vector<ll> &ret, ll n) { ll x = n; for (ll i = 2; i * i <= x; i++) { while (n % i == 0) { n /= i; ret.push_back(i); } } if (n != 1) { ret.push_back(n); } return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; vll a(N); rep(i, N) cin >> a[i]; vll ans; rep(i, N - 1) { if (a[i] < a[i + 1]) continue; ll see = i + 1; while ((see != 0 && a[see] < a[see - 1])) { ans.push_back(see - 1); swap(a[see], a[see - 1]); if (ans.size() >= N) { cout << -1 << "\n"; return 0; } see--; if (see == 0) break; } } // pt_vll(ans); if (ans.size() != N - 1) { cout << -1 << "\n"; return 0; } vector<bool> used(N - 1); rep(i, N - 1) { if (!used[i]) used[i] = true; else { cout << -1 << "\n"; } } rep(i, N - 1) { cout << ans[i] + 1 << "\n"; } return 0; }
#include "bits/stdc++.h" using namespace std; int h, w, a, b; long long dp[1 << 16][9]; bool ok[16]; void solution() { cin >> h >> w >> a >> b; int mx = h * w; for (int i = 0; i < mx; i ++) { if ((i + 1) % w == 0) ok[i] = false; else ok[i] = true; } dp[0][0] = 1; for (int msk = 0; msk < (1 << mx); msk ++) { int fbit; for (int i = 0; i < mx; i ++) if (!(msk >> i & 1)) { fbit = i; break; } for (int alr = 0; alr <= a; alr ++) { dp[msk ^ (1 << fbit)][alr] += dp[msk][alr]; if (alr < a) { if (ok[fbit] && !(msk >> (fbit + 1) & 1)) dp[msk ^ (1 << fbit) ^ (1 << (fbit + 1))][alr + 1] += dp[msk][alr]; if (fbit < mx - w && !(msk >> (fbit + w) & 1)) { dp[msk ^ (1 << fbit) ^ (1 << (fbit + w))][alr + 1] += dp[msk][alr]; } } } } cout << dp[(1 << mx) - 1][a]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc = 1; #ifdef DEBUG freopen("input.txt", "r", stdin); #endif // cin >> tc; while (tc --) { solution(); cout << "\n"; } }
// Jai Shree Ram #include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=a;i<n;i++) #define ll long long #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define endl "\n" #define x first #define y second #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define sz(a) (int)a.size() #define pii pair<int,int> #define hell 1000000007 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;} template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;} int solve(){ int k,n,m; cin >> k >> n >> m; vector<int>a(k+1); rep(i,1,k+1){ cin >> a[i]; } pii p; auto func = [&](int x){ int l = 0; int r = 0; for(int i = 1; i <= k; i++){ // |bn - am| <= x // [(am-x)/(n),(am+x)(n)] int mx = (a[i]*m + x)/n; int mn = (a[i]*m - x)/n; if(mn*n < a[i]*m - x)mn++; maxs(mn,0LL); r += mx; l += mn; } p = {l,r}; return m <= r and m >= l; }; int L = 0,R = 1e18; int ans = 0; while(L <= R){ int M = (L + R)/2; if(func(M)){ ans = M; R = M-1; } else{ L = M+1; } } func(ans); int l = p.x; int r = p.y; vector<int>b(k+1); int sum = m; for(int i = 1; i <= k; i++){ int mx = (a[i]*m + ans)/n; int mn = (a[i]*m - ans)/n; if(mn*n < a[i]*m - ans)mn++; l -= mn; r -= mx; // find y in [mn,mx] such that y >= 0 and m - y >= l and m - y <= r //y <= m - l and y >= m - r //cout << l << " " << r << endl; b[i] = max(max(0LL,sum-r),mn); sum -= b[i]; cout << b[i] << " "; } cout << endl; return 0; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t=1;//cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; using C = complex<double>; const double gs = 0.000001; int main(){ int n; cin >> n; vector<double> lx(n), ly(n), rx(n), ry(n); rep(i,n) cin >> lx[i] >> ly[i]; rep(i,n) cin >> rx[i] >> ry[i]; double ljx, ljy, rjx, rjy; ljx = ljy = rjx = rjy = 0; rep(i,n){ ljx += lx[i]; ljy += ly[i]; rjx += rx[i]; rjy += ry[i]; } ljx /= n; ljy /= n; rjx /= n; rjy /= n; rep(i,n){ lx[i] -= ljx; ly[i] -= ljy; rx[i] -= rjx; ry[i] -= rjy; } string ans = "No"; rep(i,n){ rep(j,n){ double llen = sqrt(lx[i]*lx[i] + ly[i]*ly[i]); double rlen = sqrt(rx[j]*rx[j] + ry[j]*ry[j]); if(abs(rlen-llen) >= gs) continue; double lshi = atan2(ly[i], lx[i]); double rshi = atan2(ry[j], rx[j]); double shi = lshi - rshi; bool allMitukata = true; rep(j2, n){ C tani(cos(shi), sin(shi)); C r(rx[j2], ry[j2]); r = r*tani; double rx2 = r.real(); // kaitenn double ry2 = r.imag(); bool mitukata = false; rep(i2, n){ if(abs(lx[i2]-rx2)>=gs || abs(ly[i2]-ry2)>=gs) continue; mitukata = true; } if(!mitukata) allMitukata = false; } if(allMitukata) ans = "Yes"; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long #define ld long double #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define rep(i,a,b) for(int i=a;i<b;i++) #define all(c) c.begin(),c.end() #define deb(x) cout<<"[ "<<#x<<" = "<<x<<"] " #define mp(x,y) make_pair(x,y) #define pb push_back #define f first #define s second 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;} const long long INF=1e18; const int32_t mod=1e9+7; const int N=5e5+5; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } void solve(){ ll r,x,y; cin>>r>>x>>y; ll dist = x*x + y*y; if(dist < r*r) { puts("2"); return; } ll ng = 1e6 , ok = 0; while((ng - ok) > 1){ ll mid = (ng + ok)/2ll; ll u = mid * r; if(1ll*u*u >= dist) ng = mid; else ok = mid; } cout<<ng<<endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { double R, X, Y; cin >> R >> X >> Y; double dist = sqrt(pow(X, 2) + pow(Y, 2)); double ans = dist / R; if (ans < 1.0) { ans = 2; } cout << ceil(ans) << endl; return 0; }
#include<iostream> #include <iomanip> #include<bits/stdc++.h> using namespace std; #define ll long long #define f(i,a,b) for(int i=a;i<b;i++) #define it(x,b) for(auto &x:b) #define NUM 1000000007 typedef vector<ll> vll; typedef pair<ll,ll> pll; #define endl "\n" long long binpow(long long a, long long b) { if (b == 0) return 1; long long res = binpow(a, b / 2)%NUM; if (b % 2) return (((res * res)%NUM)* a)%NUM; else return (res * res)%NUM; } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } };//unordered_map<pair<long long, long long>, bool, hash_pair> um; long long fact(long long n) { long long res = 1; for (long long i = 2; i <= n; i++) res = (res * i)%NUM; return (res)%NUM; } bool isPrime(long long n) { if (n == 1)return false; if (n == 2)return true; for (long long i = 2; i * i <= n; i++) if (n % i == 0)return false; return true; } ll ceil(ll a, ll b) { if(a%b==0) { return a/b; } else { return ((a/b)+1); } } ll min(ll a,ll b) { if(a>b) { return b; } return a; } ll max(ll a, ll b) { if(a>b) return a; return b; } long long dif(long long a,long long b) { if(a>b) return a-b; return b-a; } long long gcd(long long a,long long b) { if(b==0) return a; return gcd(b,a%b); } long long lcm(long long a,long long b) { long long k=gcd(a,b); return (a*b)/k; } void read(ll arr[],long long n) { for(long long i=0;i<n;i++) { cin>>arr[i]; } } void addedge(vector<vector<long long>>&v,long long p1,long long p2) { v[p1].push_back({p2}); v[p2].push_back({p1}); } bool mycmp(pair<long long ,float>p1,pair<long long ,float>p2) { return p1.first<p2.first; } void print(ll arr[],ll n) { for (long long i = 0; i < n; ++i) { cout<<arr[i]<<" "; } cout<<endl; } bool mycmp2(pair<long long ,pair<long long ,long long >>&p1,pair<long long ,pair<long long ,long long >>&p2) { if(p1.second.first==p2.second.first) { return p1.first>p2.first; } else { return p1.second.first<p2.second.first; } } vector<long long >prime; void sieveOferatosthenes(long long MAX) { bool pri[MAX + 1]; memset(pri, true, sizeof(pri)); pri[1] = false; for (long long p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then it is a prime if (pri[p]) { // Set all multiples of p to non-prime for (long long i = p * 2; i <= MAX; i += p) pri[i] = false; } } for(long long i=1;i<=MAX;i++) { if(pri[i]) { prime.push_back(i); } } } bool mycmp3(pair<long long,long long>&p1,pair<long long,long long>&p2) { if(p1.first==p2.first) { return p1.second<p2.second; } return p1.first<p2.first; } #define tr first #define fa second void solve() { int n;cin>>n; vector<string>v; for(int i=0;i<n;i++) { string s;cin>>s; v.push_back(s); } pair<ll,ll>p[n+1]; p[0].tr=1;p[0].fa=1; for(int i=1;i<=n;i++) { if(v[i-1]=="AND") { p[i].tr=p[i-1].tr; p[i].fa=p[i-1].tr+2*(p[i-1].fa); } else { p[i].fa=p[i-1].fa; p[i].tr=p[i-1].fa+2*(p[i-1].tr); } } cout<<p[n].first<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t;t=1; //cin>>t; // WE WILL WIN. while(t--) { solve(); } }
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <climits> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <bits/stdc++.h> using namespace std; #define lson 2*i #define rson 2*i+1 #define LS l,mid,lson #define RS mid+1,r,rson #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define ll long long #define MAX 1000005 #define MOD 1000000007 #define EXP 1e-8 #define lowbit(x) (x&-x) // const int INF = 0x1fffffffffffffff; const int INF = INT_MAX; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} void chmin(ll& a, ll b){ if(a > b) a = b; } void chmax(ll& a, ll b){ if(a < b) a = b; } void dfs(int u, unordered_map<int, int> &cls, vector<vector<int>> &adjl, vector<int> &visited, vector<int> &color, unordered_map<int, int> &no) { for (auto &v : adjl[u]) { if (visited[v]) continue; visited[v] = 1; if (cls[color[v]] > 0) no[v] = 1; cls[color[v]]++; dfs(v, cls, adjl, visited, color, no); cls[color[v]]--; } } int main () { ll n, m, x, y; ll a, b; ll t, k; cin >> n; vector<int> color(n+1, -1); for (int i = 0; i < n; ++i) cin >> color[i]; vector<vector<int>> adjl(n+1, vector<int> (0)); m = n-1; for (ll i = 0; i < m; ++i) { cin >> a >> b; a--, b--; adjl[a].push_back(b); adjl[b].push_back(a); } typedef pair<int, set<int>> dist; auto cmp = [](const dist &a, const dist &b) {return a.second > b.second;}; queue<dist> q; vector<dist> dists(n+1); vector<int> visited(n+1, 0); unordered_map<int, int> pre; unordered_map<int, int> no; for (ll i = 0; i <= n; ++i) dists[i] = make_pair(i, set<int> {}); x = 0; dists[x] = make_pair(x, set<int> {}); q.push(dists[x]); visited[x] = 1; unordered_map<int, int> color_cnt; color_cnt[color[0]] = 1; dfs(0, color_cnt, adjl, visited, color, no); for (int i = 0; i < n; ++i) { if (no[i] == 1) continue; else cout << i+1<<endl; // int flag = 1, start_color = color[i], u = i; // while (pre.count(u)) { // u = pre[u]; // // cout << i << ' ' << u << ' ' << color[u] << ' ' << color[i] <<endl; // if (color[u] == start_color) { // flag = 0; break; // } // } // if (flag) cout << i+1 <<endl; } return 0; }
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #include <bits/stdc++.h> using namespace std ; const int MAX = 100 + 10 ; int Sub(int x , int y , int mod) { x -= y ; if(x < 0) x += mod ; return x ; } int arr[MAX] ; int n ; long long x ; int dp[2][MAX][MAX] ; int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>x ; for(int i = 1 ; i <= n ; ++i) cin>>arr[i] ; long long ans = 4e18 ; for(int i = 1 ; i <= n ; ++i) { for(int k = 0 ; k < i ; ++k) { for(int x = 0 ; x <= i ; ++x) dp[0][k][x] = dp[1][k][x] = -2e9 ; } dp[0][0][0] = 0 ; for(int j = 1 ; j <= n ; ++j) { bool now = j & 1 ; bool prv = !now ; for(int k = 0 ; k < i ; ++k) { dp[now][k][0] = dp[prv][k][0] ; for(int x = 1 ; x <= i ; ++x) { dp[now][k][x] = max(dp[prv][k][x] , dp[prv][Sub(k , arr[j] % i , i)][x-1] + arr[j]) ; } } } if(dp[n&1][x%i][i] >= 0) ans = min(ans , (x - dp[n&1][x%i][i]) / (i * 1ll)) ; } return cout<<ans<<"\n" , 0 ; }
#include <bits/stdc++.h> using namespace std; const long long MOD=998244353; vector<long long> fact, fact_inv, inv; void init_nCk(int n){ fact.resize(n+54); fact_inv.resize(n+54); inv.resize(n+54); fact[0]=fact[1]=fact_inv[0]=fact_inv[1]=inv[1]=1; for(int i=2;i<n+54;i++){ fact[i]=(fact[i-1]*i)%MOD; inv[i]=MOD-(inv[MOD%i]*(MOD/i))%MOD; fact_inv[i]=(fact_inv[i-1]*inv[i])%MOD; } } int nCk(int n,int k){ return fact[n]*((fact_inv[k]*fact_inv[n-k])%MOD)%MOD; } int main(){ #define int long long int N,K; cin>>N>>K; vector<int> A(N),a(N),b(N,1),vec(K+1); for(int i=0;i<N;i++){ cin>>A.at(i); a.at(i)=2*A.at(i); } for(int i=0;i<=K;i++){ int sum=0; for(int j=0;j<N;j++){ sum+=b.at(j); sum%=MOD; b.at(j)*=A.at(j); b.at(j)%=MOD; } vec.at(i)=sum; } init_nCk(250); for(int i=1;i<=K;i++){//X int sum=0; for(int j=0;j<=i;j++){//k sum+=(((nCk(i,j)*vec.at(j))%MOD)*vec.at(i-j))%MOD; sum%=MOD; } for(int j=0;j<N;j++){ sum-=a.at(j)-MOD; a.at(j)*=2*A.at(j); sum%=MOD; a.at(j)%=MOD; } if(sum%2==0){cout<<sum/2<<endl;} else{cout<<(sum+MOD)/2<<endl;} } }
#include <bits/stdc++.h> using namespace std; template<typename T> void out(T x) { cout << x << endl; exit(0); } #define watch(x) cout << (#x) << " is " << (x) << endl using ll = long long; const int maxn = 1e6 + 5; int n; int a[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; for (int i=0; i<n; i++) { cin>>a[i]; } int g = a[0]; for (int i=0; i<n; i++) { g = __gcd(g, a[i]); } cout<<g<<endl; return 0; }
#include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <string> #include <sstream> #include <iomanip> #include <map> #include <unordered_map> #include <stack> #include <cstdio> #include <climits> #include <tuple> #include <ctime> #include <cstring> #include <numeric> #include <functional> #define sz(x) ((int)(x).size()) #define mp make_pair #define full(a) a.begin(), a.end() #define pii pair<int, int> using ull = unsigned long long; using ll = long long; using ld = long double; using namespace std; const ll mod = 1e9 + 7; const int N = 3e5 + 5; // printf("%.2f\n", value); digits after decimal // 'a' - 97 // 'A' - 65 int main() { ios::sync_with_stdio(false); cin.tie(NULL); //cout.tie(NULL); int n; cin >> n; map<int, int> mmp; for (int i = 0; i < n; i++) { int a; cin >> a; mmp[a]++; } while (sz(mmp) > 1) { pii p = *mmp.rbegin(); int X = p.first, c = p.second; p = *mmp.begin(); int x = p.first; mmp.erase(prev(mmp.end())); mmp[X - x] += c; } cout << mmp.begin() -> first; return 0; }
#pragma GCC optimize("Ofast") //#pragma GCC target ("sse4") #include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<bitset> #include<stack> #include<unordered_map> #include<unordered_set> #include<utility> #include<cassert> #include<complex> #include<numeric> #include<array> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; constexpr ll mod = 1000000007; const ll INF = mod * mod; typedef pair<int, int>P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) #define all(v) (v).begin(),(v).end() typedef pair<ll, ll> LP; typedef double ld; typedef pair<ld, ld> LDP; const ld eps = 1e-12; const ld pi = acosl(-1.0); ll mod_pow(ll x, ll n, ll m = mod) { if (n < 0) { ll res = mod_pow(x, -n, m); return mod_pow(res, m - 2, m); } if (abs(x) >= m)x %= m; if (x < 0)x += m; ll res = 1; while (n) { if (n & 1)res = res * x % m; x = x * x % m; n >>= 1; } return res; } struct modint { ll n; modint() :n(0) { ; } modint(ll m) :n(m) { if (n >= mod)n %= mod; else if (n < 0)n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; } modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; } modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, ll n) { if (n == 0)return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2)res = res * a; return res; } ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } modint operator/=(modint& a, modint b) { a = a / b; return a; } const int max_n = 1 << 17; modint fact[max_n], factinv[max_n]; void init_f() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[b] * factinv[a - b]; } modint combP(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[a - b]; } void solve() { int n, m; cin >> n >> m; vector<ll> a(n); rep(i, n)cin >> a[i]; vector<vector<int>> G(n); rep(i, m) { int x, y; cin >> x >> y; x--; y--; G[x].push_back(y); } vector<ll> dp(n); rep(i, n)dp[i] = INF; ll ans = -INF; rep(i, n) { ans = max(ans, a[i] - dp[i]); dp[i] = min(dp[i], a[i]); for (int to : G[i])dp[to] = min(dp[to], dp[i]); } cout << ans << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(15); //init_f(); //init(); //expr(); //int t; cin >> t; rep(i, t) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define mod 1000000007 #define ps(x, y) fixed << setprecision(y) << x #define w(x) int x; cin >> x; while(x --) #define mk(arr, n, type) type *arr = new type[n]; #define gcd(x, y) __gcd(x, y) #define len(x) int(x.size()) #define PI 3.1415926535897932384626433832795 #define for0(i, n) for (int i = 0; i < (int)(n); i ++) //0 based indexing #define for1(i, n) for (int i = 1; i <= (int)(n); i ++) // 1 based indexing #define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // l to r inclusive #define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based. #define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 base //short hand for usual tokens #define pb push_back #define fi first #define se second // to be used with algorithms that processes a container Eg: find(all(c),42) #define all(x) (x).begin(), (x).end() //Forward traversal #define rall(x) (x).rbegin, (x).rend() //reverse traversal // shorthand for common types typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; //static_cast<new_type>(expression) //swap(a,b) //sort(a, a + n) sort(all(x)) void solve(){ int n; cin >> n; vector<pair<int, int>> arr1; for0(i, n){ int a, b; cin >> a >> b; arr1.pb({a, b}); } bool l = false; for(int i = 0; i < n; i ++){ for(int j = i + 1; j < n; j ++){ for(int k = j + 1; k < n; k ++){ int a = arr1[i].first, b = arr1[i].second; int c = arr1[j].first, d = arr1[j].second; int e = arr1[k].first, f = arr1[k].second; if((a * (d - f) + c * (f - b) + e * (b - d)) == 0){ cout << "Yes" ; l = true; break; } } if(l) break; } if(l) break; } if(!l) cout << "No"; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define mem(x) (memset(x,0,sizeof(x))) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(random_device{}()); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int __=1; //cin>>__; while(__--) { int n; cin>>n; int a,c,s; int num=1e9+5; rep(i,0,n) { cin>>a>>c>>s; if(a<s) num=min(c,num); } if(num>1e9) cout<<"-1"; else cout<<num<<endl; } }
#include <iostream> #include <string.h> #include <algorithm> #include <vector> #include <set> typedef long long ll; #define int long long #define endl "\n" #define forp(i, x, t) for (int i = x; i < t; i++) #define forn(i, t) for (int i = t; i >= 0; i--) #define deb(x) cout << #x << " = " << x << endl const int mod = 1e9 + 7; using namespace std; void solve(){ } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // int t = 1; // cin >> t; // while (t--){ // solve(); // } int n; cin>>n; int time = 0; int ans = INT32_MAX; while (n--){ int a,p,x; cin>>a>>p>>x; time += a; x -= a; if (x > 0){ ans = min(ans, p); } } if (ans == INT32_MAX) cout << -1; else cout<<ans; return 0; }
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; typedef long long ll; typedef pair<int,int> pii; int main(void) { int n,m; scanf("%d%d",&n,&m); if(m == 0) { for(int i=1; i<=n; ++i) printf("%d %d\n",2*i-1,2*i); return 0; } if(m<0 || m>=n-1) return printf("-1"), 0; int cur = 1; for(int i=1; i<=m+1; ++i) printf("%d %d\n",cur+1,cur+2), cur += 2; printf("1 %d\n",cur+1); ++cur; n -= m+2; while(n--) printf("%d %d\n",cur+1,cur+2), cur += 2; return 0; }
#include <iostream> #include <algorithm> #include <cmath> #include <string> #include <map> #include <set> #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}; vl A, B; int N, M; vi G[200000]; bool visited[200000] = {}; ll sum1, sum2; void dfs(int v) { visited[v] = true; sum1 += A[v]; sum2 += B[v]; for (auto x : G[v]) { if (visited[x]) continue; dfs(x); } } int main() { cin >> N >> M; A.resize(N); B.resize(N); FOR(i, 0, N) cin >> A[i]; FOR(i, 0, N) cin >> B[i]; FOR(i, 0, M) { int c, d; cin >> c >> d; c--, d--; G[c].push_back(d); G[d].push_back(c); } FOR(i, 0, N) { if (visited[i]) continue; sum1 = 0, sum2 = 0; dfs(i); if (sum1 != sum2) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; }
#include <bits/stdc++.h> #define ff first #define ss second #define endl '\n' using namespace std; const long long INF = (long long) 1e18; const int mod = (int) 1e9+7; const int MAXN = (int) 3e5+5; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; ll n, q; int t[4*MAXN]; int a[MAXN]; void build(int v, int tl, int tr){ if(tl == tr){ t[v] = a[tl]; }else{ int tm = (tl + tr) / 2; build(2*v, tl, tm); build(2*v + 1, tm + 1, tr); t[v] = (t[2*v] ^ t[2*v + 1]); } } void upd(int v, int tl, int tr, int pos){ if(tl == tr){ t[v] = a[tl]; }else{ int tm = (tl + tr) / 2; if(pos <= tm) upd(2*v, tl, tm, pos); else upd(2*v + 1, tm + 1, tr, pos); t[v] = (t[2*v] ^ t[2*v + 1]); } } int gt(int v, int tl, int tr, int l, int r){ if(l > r) return 0; if(tl == l && tr == r){ return t[v]; }else { int tm = (tl + tr) / 2; return (gt(2*v, tl, tm, l, min(tm, r)) ^ gt(2*v + 1, tm + 1, tr, max(tm + 1, l), r)); } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); #ifdef Local freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin); freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout); #endif cin>>n>>q; for(int i = 0; i < n; i++) cin>>a[i]; build(1, 0, n - 1); while(q--){ int t; cin>>t; if(t == 1){ int x, y; cin>>x>>y; x--; a[x] ^= y; upd(1, 0, n - 1, x); }else{ int l, r; cin>>l>>r; cout<<gt(1, 0, n - 1, l - 1, r - 1)<<endl; } } #ifdef Local cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds "; #endif }
/********include********/ #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #define popcount __builtin_popcount using namespace std; //#include <atcoder/all> /***/ //#include <iomanip> //#include <cmath> #include <bits/stdc++.h> /********define********/ const int MAX = 510000; const int MOD = 1000000007; #define rep(i,x) for(long long i=0;i<x;i++) #define repn(i,x) for(long long i=1;i<=x;i++) #define rrep(i,x) for(long long i=x-1;i>=0;i--) #define rrepn(i,x) for(long long i=x;i>1;i--) #define REP(i,n,x) for(long long i=n;i<x;i++) #define REPN(i,n,x) for(long long i=n+1;i<x;i++) #define pr printf #define re return #define mod 1000000007 //#define mod 998244353 #define inf INT_MAX//19桁 #define INF 1e18+5//19桁 const double PI=3.14159265358979323846; #define fi first #define se second #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) #define all(x) (x).begin(),(x).end() typedef long long int ll; typedef pair<long long, long long> P; /********変数宣言********/ //vector<long long> g[200020]; vector<pair<long long,long long>> g[200020]; ll s[200020]; bool used[200020]; //bool dp[100005]; ll A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,Q,R,S,T,U,V,W,X,Y,Z; double dA,dB,dC,dD,dE,dF,dG,dH,dI,dJ,dK,dL,dM,dN,dO,dP,dQ,dR,dS,dT,dU,dV,dW,dX,dY,dZ; string sa,sb,sc,sd,se,sf,sg,sh,si,sj,sk,sl,sm,sn,so,sp,sq,sr,ss, su,sv,sw,sx,sy,sz; int main() { cin.tie(0); ios::sync_with_stdio(false); cin>>N;cin>>M; vector<string>v(M); vector<pair<long long, string> > p(M); //sort(p.begin(), p.end()); //sort(p.begin(), p.end(),greater<pair<long long,long long> >()); rep(j,M){ cin>>v[j]; } rep(i,M){ p[i].fi=v[i].size(); p[i].se=v[i]; } sort(p.begin(), p.end()); ll check; check=0; ll i; i=0; ll flg; while(check<N*N && i<M){ if(check%N!=0){ if(p[i-1].se[p[i-1].se.size()-1]==p[i].se[0]){ rep(j,p[i].se.size()-1){ p[i].se[j]=p[i].se[j+1]; } p[i].se.resize(p[i].se.size()-1); } } if(check%N+p[i].se.size()<=N){ cout<<p[i].se; check+=p[i].se.size(); if(check%N==0){ cout<<"\n"; } } else{ while(check%N!=0){ cout<<"."; check++; } cout<<"\n"; } i++; } re 0; }
#include <ctime> #include <deque> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <queue> #include <climits> #include <cmath> #include <sstream> #include <iomanip> #include <map> #include <stack> #include <regex> #include <set> #include <bitset> const int64_t infl = 1LL << 60; const int64_t cc = pow(10, 9) + 7; bool sort_pair(std::pair<int64_t, std::string> origin, std::pair<int64_t, std::string> another){return origin.first > another.first;} 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;} std::vector<std::string> list; int main() { //std::vector< std::pair< int64_t, int64_t > > list; std::vector< std::string > str_list; std::string str1; std::string str2; int64_t n,k,l,a,b,c; std::cin>> a; std::cin>> b; std::cin>> c; if( pow( a, 2 ) + pow( b, 2 ) < pow( c, 2 ) ) { std::cout<< "Yes"; } else { std::cout<< "No"; } }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;} template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;} template<typename T> 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 U,typename... V> typename enable_if<is_same<T, U>::value!=0>::type fill_v(U &u,const V... v){u=U(v...);} template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value==0>::type fill_v(U &u,const V... v){ for(auto &e:u) fill_v<T>(e,v...); } template <typename F> class #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) FixPoint final : private F { public: template <typename G> explicit constexpr FixPoint(G&& g) noexcept : F{std::forward<G>(g)} {} template <typename... Args> constexpr decltype(auto) operator()(Args&&... args) const #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9 noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...))) #endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9 { return F::operator()(*this, std::forward<Args>(args)...); } }; // class FixPoint #if defined(__cpp_deduction_guides) template <typename F> FixPoint(F&&) -> FixPoint<std::decay_t<F>>; #endif // defined(__cpp_deduction_guides) namespace { template <typename F> #if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard) # if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) __attribute__((warn_unused_result)) # elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_) _Check_return_ # endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) #endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard) inline constexpr decltype(auto) makeFixPoint(F&& f) noexcept { return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)}; } } // namespace int main(void){ cin.tie(0); ios::sync_with_stdio(false); int64 A, B, W; cin >> A >> B >> W; W *= 1000; int64 rmin = INF, rmax = 0; REP(i, 1000100) { if (i * A + i * (B - A) >= W && i * A <= W) { chmin(rmin, i); } if (i * B - i * (B - A) <= W && W <= i * B) { chmax(rmax, i); } } if (rmin == INF) { cout << "UNSATISFIABLE" << endl; } else { cout << rmin << " " << rmax << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; using V = vector<int>; using Q = queue<int>; using Graph = vector<vector<int>>; #define sz(x) ((int)(x).size()) #define in(x) int x;cin>>x #define inn(x, y) int x,y;cin>>x>>y #define innn(x, y, z) int x,y,z;cin>>x>>y>>z #define bit(n) (1<<(n)) #define out(x) cout<<(x)<<endl #define outt(x,y) cout<<(x)<<" "<<(y)<<endl #define err(x) cerr<<(x)<<endl #define errr(x,y) cerr<<(x)<<" "<<(y)<<endl #define errrr(x,y,z) cerr<<(x)<<" "<<(y)<<" "<<(z)<<endl #define rep(i,n) for (int i=0;i<(n);i++) #define repp(i,n) for (int i=1;i<=(n);i++) #define reppp(j, i, n) for (int j=i+1; j<n; j++) #define lim5 200010 #define lim9 1000000010 #define YesNo(x) cout<<(x?"Yes":"No")<<endl #define YESNO(x) cout<<(x?"YES":"NO")<<endl #define yesno(x) cout<<(x?"yes":"no")<<endl #define yay(x) cout<<(x?"yay":":(")<<endl #define deg(_rad) (((_rad)/2/M_PI)*360) #define rad(_deg) (((_deg)/360)*2*M_PI) #define mid(min, max) ((min + max) / 2) bool isComp(const char& c1, const char& c2) { if (c1 == 'A') return c2 == 'T'; if (c1 == 'T') return c2 == 'A'; if (c1 == 'C') return c2 == 'G'; if (c1 == 'G') return c2 == 'C'; return false; } int main() { in(n); V a(n); rep(i, n) cin >> a[i]; int max_num = 2; int max_gcd = 0; for(int i = 2; i <= 1000; i++) { int warikireta = 0; rep(j, a.size()) { if (a[j] % i == 0) ++warikireta; } if (warikireta > max_gcd) { max_gcd = warikireta; max_num = i; } } out(max_num); 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 ll 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); ll test=1; //cin>>test; for(ll tt=1;tt<=test;tt++) { //cout<<"Case #"<<tt<<": "; maester(); } #ifdef LOCAL cout<<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 v(n); cin>>v; ll ans=2,maxi=-1; for(i=2;i<=1000;i++) { ll ansi=0; for(auto j:v)if(j%i==0)ansi++; if(ansi>maxi) { maxi=ansi; ans=i; } } cout<<ans; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; typedef long long ll; typedef long double ld; #define rep(i,n) for(ll i=0;i<(n);i++) #define repr(i,n) for(ll i=(n-1);i>=0;i--) #define all(x) x.begin(),x.end() #define br cout << "\n"; using namespace std; const long long INF = 1e18; const long long MOD = 1e9+7; using Graph = vector<vector<ll>>; 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;} ll ceilll(ll a, ll b) {return (a + b-1) / b;} // if(a%b != 0) (a/b) + 1 ll get_digit(ll a) {ll digit = 0; while(a != 0){a /= 10; digit++;} return digit;} // a != 0 template<typename T> void vecdbg(vector<T>& v){ rep(i, v.size()){cout << v[i] << " ";} br;} template<typename T> void vecvecdbg(vector<vector<T>>& v){ rep(i, v.size()){rep(j, v[i].size()){cout << v[i][j] << " ";} br;}} ll POW(ll a, ll n){ ll res = 1; while(n > 0){ if(n & 1){ res = res * a; } a *= a; n >>= 1; } return res; } using P = pair<ll, ll>; // 0 false, 1 true // string number to int : -48 or - '0' // a to A : -32 // ceil(a) 1.2->2.0 // c++17 g++ -std=c++17 a.cpp // global vector -> 0 initialization // DONT FORGET TO INTIALIZE // The type of GRID is CHAR. DONT USE STRING // If the result in local and judge is different, USE CODETEST!! // (a * b)over flow? if(a > INF / b){ /* overflow */} struct UnionFind { vector<long long> d; UnionFind(long long n = 0) : d(n, -1) { } //root : -size, not root: root long long root(long long x){ if(d[x] < 0){ return x; } return d[x] = root(d[x]); } bool unite(long long x, long long y){ x = root(x); y = root(y); if(x == y){ return false; } if(d[x] > d[y]){ swap(x, y); } d[x] += d[y]; d[y] = x; return true; } long long size(long long x){ return -d[root(x)]; } bool issame(long long a, long long b){ return root(a) == root(b); } }; int main() { std::cout << std::fixed << std::setprecision(15); ll n; cin >> n; vector<long long> a(n); for(long long i = 0; i < n; i ++){ cin >> a[i]; } ll ans = INF; for(ll bit = 0; bit < (1<<(n-1)); bit++){ UnionFind uf(n); vector<ll> tmp; //cout << "{ "; for(ll i = 0; i < (n-1); i++){ if((bit>>i) & 1){ tmp.push_back(i); //cout << i << " "; } } //cout << " }" << endl; for(ll i = 0; i < tmp.size(); i++){ uf.unite(tmp[i], tmp[i]+1); } //cout << "uf "; vector<ll> res(n); for(ll i = 0; i < n; i++){ ll x = uf.root(i); res[x] = res[x] | a[i]; //cout << uf.root(i) << " "; } ll now = 0; rep(i, n){ now = now ^ res[i]; } //cout << "now = " << now << endl; ans = min(ans, now); } cout << ans << endl; }
#include <bits/stdc++.h> #define DEBUG if(0) #define lli long long int #define ldouble long double using namespace std; const int maxN = 20; int n; lli a[maxN]; lli solve(int i = 0, lli currOr = 0, lli currXor = 0) { if (i == n) return currXor ^ currOr; currOr |= a[i]; lli ans = min( solve(i + 1, 0, currXor ^ currOr), solve(i + 1, currOr, currXor) ); return ans; } int main() { while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) scanf("%lld", &a[i]); lli ans = solve(); printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> //#define DEBUG #define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++) #define deb(x) std::cout << #x << " " << x << endl; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; template<typename... T> void read(T& ... a){ ((cin >> a),...); } template<typename... T> void write(T... a){ ((cout << a << "\n"),...); } template<typename... T> void write_space(T... a){ ((cout << a << " "),...); } void solve(){ ll a, b; read(a,b); ll x = (a + b) / 2; ll y = ( a - b) /2 ; cout << x << " " << y << endl; } int main() { //making data IO Fast std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); /****************************/ solve(); return 0; }
#include<bits/stdc++.h> using namespace std; using ll=long long; template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;} template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;} void solve(){ int a,b; cin>>a>>b; cout<<(a+b)/2<<" "<<(a-b)/2<<'\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); solve(); }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define printV(v) for(int i=0;i<(int)v.size();i++){cout << v[i]<< " ";} cout << endl #define printRev(v) for(int i=(int)v.size()-1;i >= 0;i--){cout << v[i] << " ";} cout << endl #define sortV(v) sort(v.begin(),v.end()) #define revV(v) reverse(v.begin(),v.end()) ll a,b,c,d,e,f,g,n,m,k,t,x,h,w,p,q,y; string str = ""; ll mod = 1e9 + 7; const int N = 100009; void solve(){ cin >> str; if(str.size() < 4){ n = stoi(str); if(n%8 == 0){ cout << "Yes\n"; return; } if(str.size() == 2){ string newOne = str; newOne[0] = str[1]; newOne[1] = str[0]; n = stoi(newOne); if(n%8 == 0){ cout << "Yes\n"; return; } } } map<char,int> mp; for(char ch : str){ mp[ch]++; } for(int i=104; i < 1000; i+= 8){ string str = to_string(i); map<char, int> curr; for(char ch : str) curr[ch]++; bool flag = true; for(auto it : curr){ char ch = it.first; int amount = it.second; int amount2 = mp[ch]; if(amount > amount2) flag = false; } if(flag){ cout << "Yes\n"; return; } } cout << "No\n"; } int main(){ ios_base::sync_with_stdio(false); int cases = 1; //cin >> cases; while(cases--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define debug(x) cout << #x << ": " << (x) << endl; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> iip; string s; int cnt[10] = {0}; bool check(int last_digit) { // debug(last_digit); if (!cnt[last_digit]) return false; --cnt[last_digit]; int i; if (last_digit == 8) i = 0; else if (last_digit == 6) i = 1; else if (last_digit == 4) i = 2; else if (last_digit == 2) i = 3; for (; i < 100; i += 4) { if (s.length() > 2 && i < 10) continue; vector<int> v(cnt, cnt + 10); bool f = true; string s = to_string(i); // debug(s); for (auto c : s) { // if (c == '0') continue; if (!v[c - '0']) { f = false; break; } --v[c - '0']; } if (f) return true; } ++cnt[last_digit]; return false; } void solve() { // string s; cin >> s; if (s == "8") { cout << "Yes\n"; return; } for (auto c : s) { ++cnt[c - '0']; } if (check(8) || check(6) || check(4) || check(2)) cout << "Yes\n"; else cout << "No\n"; } int main() { // For fast I/O ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
#include "bits/stdc++.h" //custom precompiled stdc++.h header :) using namespace std; #define ll long long int #define Print(x) cout << x << endl #define PrintAns(x) cout << x << endl; return #define PrintPrec(x, y) cout << fixed << setprecision(y) << x << endl; #ifdef SUBLIME #define deb(x) cout << #x << ": " << x << endl; #define deb2(x, y) cout << #x << ": " << x << ", " << #y << ": " << y <<endl #else #define deb(x) //luKaS iS No0b #define deb2(x, y) //XD #endif #define cint(x) int x; cin >> x #define cll(x) ll x; cin >> x #define cstring(x) string x; cin >> x #define pb push_back #define mp make_pair typedef vector<int> vi; typedef vector<ll> vll; typedef vector<string> vs; const int MAX_N = 1e5 + 1; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e18; // cint(n); // vi vec(n); // for(int i = 0; i < n; i++){ // cin >> vec[i]; // } void test_case() { ll n; cin >> n; ll ans = 0; for (int i = 0; i < n; i++) { ll a, b; cin >> b >> a; ans += a * (a + 1) / 2 - b * (b + 1) / 2 + b; } Print(ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef SUBLIME freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif long long t = 1; ///cin >> t; while (t--) { test_case(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ll n,i; cin>>n; ll A[n]; ll S=0; for(i=0;i<n;i++) { cin>>A[i]; S+=A[i]; } sort(A,A+n); ll prev=0,ans=0; for(i=0;i<n;i++) { ll p1,p2; prev+=A[i]; p1=(i+1)*A[i]-prev; p2=(S-prev)-(n-i-1)*A[i]; ans+=(p1+p2); } ans/=2; cout<<ans; }
#include <iostream> using namespace std; #define STRMAX 101 int main() { int V, T, S, D; char str[STRMAX]; cin >> V >> T >> S >> D; int x_T = V * T; int x_S = V * S; if(D >= x_T && D <= x_S) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int ans = n*1.08; if(206>ans){ cout << "Yay!" << endl; } else if(206 == ans){ cout << "so-so" << endl; } else{ cout << ":(" << endl; } }
#include <bits/stdc++.h> #define rei register int using namespace std; const int maxn = 1010; int h,w; string s[maxn]; queue<int>q; int vish[maxn],visw[maxn],cnth = 2,cntw = 2; void updateh(int x){ for(rei i = 0;i < w;++i) if(s[x][i] == '#') { if(visw[i + 1] == 0){ visw[i + 1] = 1; q.push(i + 1); ++cntw; } } } void updatew(int x){ --x; for(rei i = 1;i <= h;++i) if(s[i][x] == '#'){ if(vish[i] == 0){ vish[i] = 1; q.push(i); ++cnth; } } } void bfsh(int x){ q.push(x); vish[x] = 1; while(!q.empty()){ int x = q.front(); q.pop(); if(vish[x]) updateh(x); if(visw[x]) updatew(x); } } void bfsw(int x){ q.push(x); visw[x] = 1; while(!q.empty()){ int x = q.front(); q.pop(); if(vish[x]) updateh(x); if(visw[x]) updatew(x); } } void work(){ vish[1] = vish[h] = visw[1] = visw[w] = 1; updateh(1),updateh(h),updatew(1),updatew(w); while(!q.empty()){ int x = q.front(); q.pop(); if(vish[x]) updateh(x); if(visw[x]) updatew(x); } } int main(){ ios::sync_with_stdio(false); cin >> h >> w; for(rei i = 1;i <= h;++i){ cin >> s[i] ; } int ans = 99999,cnt = 0; work(); for(rei i = 1;i <= h;++i) if(!vish[i]) bfsh(i),++cnt; ans = cnt; cnt = 0; memset(vish,0,sizeof(vish)),memset(visw,0,sizeof(visw)); work(); for(rei i = 1;i <= w;++i) if(!visw[i]) bfsw(i),++cnt ; ans = min(ans,cnt); cout << ans ; return 0; }
#include<bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2") #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define req(i,a,b) for(int i=(a);i>=(b);--i) #define rep_(i,a,b) for(int i=(a);i<(b).size();++i) #define F(a) rep(a,1,n) #define M(a,b) memset(a,b,sizeof a) #define DC int T;cin>>T;while(T--) #define ll long long #define Z(a,b) sort(a+1,a+b+1) using namespace std; const unsigned _mod=998244353; const unsigned mod=1e9+7; const ll infi=0x3f3f3f3f3f3f3fll; const int inf=0x3f3f3f3f; void rd(auto &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;} ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;} ll qpow(ll x,ll y){ll ret=1;while(y){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;} /* [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] */ int h,w,fa[2010],s,t; char c; unordered_map<int,bool> mp; inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} inline void merge(int x,int y){fa[find(x)]=find(y);} int main() { cin>>h>>w; rep(i,1,h+w) fa[i]=i; merge(1,h+1),merge(1,h+w),merge(h,h+1),merge(h,h+w); rep(i,1,h) rep(j,1,w) cin>>c,c=='#'&&(merge(i,h+j),1); rep(i,1,h) s+=!mp[find(i)],mp[find(i)]=1; mp.clear(); rep(i,h+1,h+w) t+=!mp[find(i)],mp[find(i)]=1; cout<<min(s,t)-1<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define forn(i,a,n) for (int i=a; i<n; i++) signed main(){ ios::sync_with_stdio(false); cin.tie(0); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int b,c; cin>>b>>c; if (b!=0 && c==1){ cout<<2<<endl; return 0; } int A,B,C,D; if (c%2==1){ A=-b-c/2; B=-b+c/2; c--; C=b-c/2; D=b+c/2-1; } else{ A=b-c/2; B=b+c/2-1; c--; C=-b-c/2; D=-b+c/2; } //cout<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<endl; cout<<(B-A+1)+(D-C+1)-max(0ll,min(B,D)-max(A,C)+1)<<endl; return 0; }
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <bits/stdc++.h> using namespace std; #define lson 2*i #define rson 2*i+1 #define LS l,mid,lson #define RS mid+1,r,rson #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define ll long long #define MAX 1000005 #define MOD 1000000007 #define INF 0x3f3f3f3f #define EXP 1e-8 #define lowbit(x) (x&-x) ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int main () { ll n, k; cin >> n >> k; while (k--) { if (n % 200) { n *= 1000; n += 200; } else n /= 200; } cout << n << endl; return 0; }
// Hail god Yato #include <bits/stdc++.h> using namespace std; #define hs ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); typedef long long ll; const ll MOD = 1000000007; const ll INF = 1e18; const ll MAX = 100001; // // int ncr(int n, int r){ if(r > n || r < 0) return 0; if(n < 3){ int ans = 1; for(int i = 1; i <= r; i++) ans *= (n - i + 1); for(int i = 1; i <= r; i++) ans /= (i ); return ans; } return ncr(n/3, r/3) * ncr(n%3, r%3); // Lucas's theorem } void solve(){ int n; string str; cin>>n>>str; string test = "BWR"; int arr[n]; for(int i = 0; i < n; i++) arr[i] = test.find(str[i]); int ans = 0; for(int i = 0; i < n; i++){ ans += ncr(n-1, i)*arr[i]; ans %= 3; } if(n%2 == 0){ ans *= -1; ans += 3; } ans %= 3; cout<<test[ans]; } signed main(){ hs; ll t; t=1; // cin>>t; for (int i=1; i<=t; i++){ //cout<<"Case #"<<i<<": "; solve(); } return 0; }
/* ALLAH is Almighty.... */ #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 pi acos( -1.0 ) #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 ll long long #define all( x ) ( x ).begin(), ( x ).end() typedef tree < ll, null_type, less < ll >, rb_tree_tag, tree_order_statistics_node_update > ordered_set; const ll N = 2e5 + 9; const ll MOD = 1e9 + 7; ll n, ans, T; ll A[ 42 ]; vector < ll > part1, part2; void solve( int t ) { cin >> n >> T; rep( i, 0, n ) { cin >> A[ i ]; } ll num = 1 << ( n / 2 ); while ( num-- ) { ll sum = 0; rep( i, 0, 22 ) { if ( ( 1 << i ) & num ) { sum += A[ i ]; } } part1.push_back( sum ); } num = 1 << ( n - n / 2 ); while ( num-- ) { ll sum = 0; rep( i, 0, 22 ) { if ( ( 1 << i ) & num ) { sum += A[ i + n / 2 ]; } } part2.push_back( sum ); } part1.push_back( 0 ); part2.push_back( 0 ); part2.push_back( 1e18 ); sort( all( part2 ) ); rep( i, 0, part1.size() ) { ll res = part1[ i ]; if ( res > T ) { continue; } ll idx = upper_bound( all( part2 ), T - res ) - part2.begin(); res += part2[ idx - 1 ]; ans = max( ans, res ); } cout << ans; } int main() { ios_base::sync_with_stdio( false ); cin.tie( NULL ); cout.tie( NULL ); cout << setprecision( 12 ); int t = 1; //cin >> t; for ( int i = 1; i <= t; ++i ) { solve( i + 1 ); } return 0; }
//2*10^9 //9*10^18 // __int128_t #include<bits/stdc++.h> using namespace std; #define ull unsigned long long int #define ll long long int const ll M=1e9+7; //to handle when remainder is -neg we add + M //but when rem is +pos that time also we add so use %M //so it will work for both ll mod(ll n){ return (n%M + M)%M; } ll modAdd(ll a, ll b){ return mod(mod(a)+mod(b)); } ll modMul(ll a, ll b){ return mod(mod(a)*mod(b)); } ll modMinus(ll a, ll b){ return mod(mod(a)-mod(b)); } ll modpow(ll x, ll n) { if (n == 0) return 1%M; if (n == 1) return x%M; ll u = modpow(x,n/2); u = modMul(u,u); if (n%2) u = modMul(u,x); return u; } ll mod1(ll n, ll mod){ return (n%mod + mod)%mod; } ll modMul1(ll a, ll b, ll mod){ return mod1(mod1(a,mod)*mod1(b,mod),mod); } ll modpow1(ll x, ll n, ll mod) { if (n == 0) return 1%mod; if (n == 1) return x%mod; ll u = modpow1(x,n/2,mod); u = modMul1(u,u,mod); if (n%2) u = modMul1(u,x,mod); return u; } ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b); } bool isPrime(ll n){ int end = sqrt(n); for(int i=2;i<=end;i++) if(n%i==0) return false; return true; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while(t--){ ll n,m; cin>>n>>m; ll result = modpow1(10,n,m*m); cout<<(result/m)%m<<endl; } return 0; }
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;++i) #define rep(i,n) REP(i,0,n) #define fst first #define snd second #define pb push_back #define ALL(x) x.begin(),x.end() #define EPS (1e-9) #define equals(a,b) (fabs((a)-(b))<EPS) using namespace std; bool LT(double a,double b) { return !equals(a,b) && a < b; } bool LTE(double a,double b) { return equals(a,b) || a < b; } const string YES = ""; const string NO = ""; typedef long long ll; typedef pair<int,int> ii; int V,E; vector<ll> A; struct Edge{ int to; ll cost; Edge(int to, ll cost):to(to),cost(cost){} }; template<typename T> bool visit(const T& G,int v,vector<int>& order,vector<int>& color){ color[v] = 1; for(__typeof((G[v]).begin()) e = G[v].begin();e != G[v].end();e++){ if(color[e->to] == 2)continue;//このvisit以外のvisitで訪れた if(color[e->to] == 1)return false;//このvisitで訪れた if(!visit(G,e->to,order,color))return false; } order.push_back(v); color[v] = 2; return true; } template<typename T> bool topologicalSort(const T& G,vector<int>& order){ int SizeG = G.size(); vector<int> color(SizeG); for(int u=0;u<SizeG;u++) if(!color[u] && !visit(G,u,order,color)) return false; reverse(order.begin(),order.end()); return true; } int main() { cin >> V >> E; vector<vector<Edge>> G(V); A.resize(V); rep(i,V) cin >> A[i]; int s,t; rep(i,E) { cin >> s >> t; --s, --t; G[s].pb((Edge){t,0}); } vector<int> order; assert( topologicalSort(G,order) ); reverse(ALL(order)); if(0) { rep(i,(int)order.size()){ cout << order[i]+1 << endl; } } ll ans = -LLONG_MAX; vector<ll> maxi(V,-1); rep(i,(int)order.size()) { int v = order[i]; ll tmp_maxi = -1; rep(j,(int) G[v].size()) { int t = G[v][j].to; tmp_maxi = max(tmp_maxi, maxi[t]); } if( tmp_maxi != -1LL ) { ans = max(ans,tmp_maxi - A[v]); } tmp_maxi = max(tmp_maxi,A[v]); maxi[v] = tmp_maxi; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<bool>> masu(10,vector<bool>(10)); int h,w; int main(){ char s; int ans=0; bool a,b,c,d; cin>>h>>w; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cin>>s; if(s=='#'){ masu.at(i).at(j)=true; }else{ masu.at(i).at(j)=false; } } } for(int i=1;i<h-1;i++){ for(int j=1;j<w-1;j++){ if(masu.at(i).at(j)){ a=masu.at(i-1).at(j-1); b=masu.at(i-1).at(j); c=masu.at(i-1).at(j+1); d=masu.at(i).at(j-1); if(!a&&!b&&c&&!d)ans+=2; if(a&&b&&!c&&!d)ans+=2; if(a&&!b&&!c&&d)ans+=2; if(!a&&b&&c&&!d)ans+=2; if(!a&&!b&&c&&d)ans-=2; if(a&&b&&c&&!d)ans+=4; if(a&&b&&!c&&d)ans-=2; if(!a&&b&&c&&d)ans+=2; if(!a&&!b&&!c&&!d)ans+=4; // cout<<"("<<i<<","<<j<<") ans="<<ans<<"abcd"<<a<<b<<c<<d<<endl; } } } cout<<ans<<endl; }
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> #include <utility> using namespace std; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase #define REP(i, n) for(int i = 0;i < (n);i++) /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const llint mod=998244353; const llint inf=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-10; template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} int main(void){ int i,j,H,W;cin>>H>>W; vector<string>s(H); for(i=0;i<H;i++){cin>>s[i];} s[0][0]='#'; s[0][W-1]='#'; s[H-1][0]='#'; s[H-1][W-1]='#'; //地面の連結成分を数える vector<int>renR(H); vector<int>renC(W); queue<int>que; int rs=0; int Rx=0,Cx=0; for(i=0;i<H+W;i++){ if(i<H){ if(renR[i]!=0){continue;} bool aru=0; for(j=0;j<W;j++){ if(s[i][j]=='#'){aru=1;break;} } if(!aru){Rx++;continue;} rs++; renR[i]=rs; que.push(i); }else{ if(renC[i-H]!=0){continue;} bool aru=0; for(j=0;j<H;j++){ if(s[j][i-H]=='#'){aru=1;break;} } if(!aru){Cx++;continue;} rs++; renC[i-H]=rs; que.push(i); } while(que.size()){ if(que.front()<H){ int t=que.front(); que.pop(); for(j=0;j<W;j++){ if(renC[j]==0&&s[t][j]=='#'){ renC[j]=renR[t]; que.push(j+H); } } }else{ int t=que.front()-H; que.pop(); for(j=0;j<H;j++){ if(renR[j]==0&&s[j][t]=='#'){ renR[j]=renC[t]; que.push(j); } } } } } //for(i=0;i<H;i++){cerr<<renR[i]<<" ";} //for(i=0;i<W;i++){cerr<<renC[i]<<" ";} //cerr<<endl; cout<<rs-1+min(Rx,Cx)<<endl; }
#include <bits/stdc++.h> using namespace std; template <int64_t mod> class modint { int64_t x; public: modint(int64_t x = 0) : x((x % mod + mod) % mod) {} modint operator-() const { return modint(-x); } modint& operator+=(const modint& a) { if ((x += a.x) >= mod) x -= mod; return *this; } modint& operator-=(const modint& a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } modint& operator*=(const modint& a) { (x *= a.x) %= mod; return *this; } modint operator+(const modint& a) const { return modint(*this) += a; } modint operator-(const modint& a) const { return modint(*this) -= a; } modint operator*(const modint& a) const { return modint(*this) *= a; } modint pow(int64_t t) const { if (not t) return 1; modint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod modint inv() const { return pow(mod - 2); } modint& operator/=(const modint& a) { return (*this) *= a.inv(); } modint operator/(const modint& a) const { return modint(*this) /= a; } friend ostream& operator<<(ostream& os, const modint& m) { os << m.x; return os; } }; const int64_t mod = 1e9 + 7; using mint = modint<mod>; int main() { int64_t n, p; cin >> n >> p; mint ans = mint(p - 1) * mint(p - 2).pow(n - 1); cout << ans << endl; return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author NikuKyabe */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i) #define REV(i, a, b) for (int i = (b) - 1; i >= (a); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001l #define MOD 1000000007 #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; /** * x^a mod MODを求める O(log a) * @return x^a mod MOD */ int mod_pow(int x, int a) { int res = 1; while(a > 0){ if(a & 1) res = res * x % MOD; x = x * x % MOD; a >>= 1; } return res; } /** * xのmod Mでの逆元を求める O(log MOD) * mod MODの中でのa / b はa * mod_inv(b)をする * @return xの逆元(mod MOD) */ int mod_inv(int x){ return mod_pow(x, MOD - 2); } class DNowhereP { public: void solve(std::istream& cin, std::ostream& cout) { int n, p; cin >> n >> p; int res = mod_pow(p-2, n-1); res *= ((p-1) % MOD); res %= MOD; cout << res << endl; } }; signed main() { DNowhereP solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N, M, K; cin >> N >> K >> M; int sum=0; int purpose = M*N; for(int i=0; i<N-1; i++){ int x; cin >> x; sum+=x; } if(purpose - sum <=K){ if(purpose-sum >=0){ cout << purpose-sum << endl; }else { cout << 0 << endl; } } if(purpose-sum>K)cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; static const auto do_fast_io = [] { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }(); int main() { int a, b, c; cin >> a >> b >> c; long long req = (a * a) + (b * b); if (req < c * c) { cout << "Yes"; } else { cout << "No"; } return 0; }
// #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long int; using int64 = long long int; template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int INF = 1LL << 29; const ll LONGINF = 1LL << 60; const ll MOD = 1000000007LL; int main() { int N; cin >> N; vector<ll> A(N), B(N), M(N); for(int i=0; i<N; i++) { cin >> A[i]; B[i] = A[i]; if(i > 0) B[i] += B[i-1]; if(i == 0) M[i] = max(0LL, A[i]); else M[i] = max(M[i-1], B[i]); } ll pos = 0, ma = 0; for(int i=0; i<N; i++) { chmax(ma, pos+M[i]); pos += B[i]; } cout << ma << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define INF ((1LL<<62)-(1LL<<31)) typedef long long ll; vector<pair<long long, long long> > prime_factorize(long long N) { vector<pair<long long, long long> > res; for (long long a = 2; a * a <= N; ++a) { if (N % a != 0) continue; long long ex = 0; while (N % a == 0) { ++ex; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } int calc(int i) { int sum; sum=(3+i-2)*(i+3-1)/2; return sum; } int main() { int k; cin >> k; vector<int> dp(200020); dp[0]=0; dp[1]=1; for(int i=2;i<=k;i++) { ll tot=1,cnt=0; const auto &res = prime_factorize(i); for(auto x:res) { tot*=calc(x.second); } //cout << i << " " << tot+cnt << endl; dp[i]=dp[i-1]+tot+cnt; } cout << dp[k] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i=1; i < (int)(n); i++) #define rrep(i, n) for (int i=((int)(n)-1); i>=0; i--) #define rreps(i, n) for (int i=((int)(n)-1); i>0; i--) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; using lint = long long; using p = pair<int, int>; 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; } int main() { int N, X; scanf("%d%d", &N, &X); vector<int> a(N); rep (i, N) cin >> a[i]; rep (i, N) { if (a[i] == X) continue; cout << a[i] << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { // ll t; // cin>>t; // while (t--) // { ll n,x; cin>>n>>x; ll a[n]; for (int i = 0; i < n; i++) { cin>>a[i]; } for (int i = 0; i < n; i++) { if(a[i]!=x) cout<<a[i]<<" "; } cout<<endl; //} }
#include<iostream> using namespace std; int main(){ long long int i,j,k,n; cin>>n; long long int a[n],b[n]; long long int ma,mi; cin>>a[0]; ma=a[0]; for(i=1;i<n;i++){ cin>>a[i]; if(a[i]>ma){ ma=a[i]; } } cin>>b[0]; mi=b[0]; for(i=1;i<n;i++){ cin>>b[i]; if(b[i]<mi){ mi=b[i]; } } if(ma>mi){ cout<<"0"; } else{ cout<<mi-ma+1; } }
#include <bits/stdc++.h> using namespace std; const int N=1100; int pre[N]; int main() { int n,x=1; cin>>n; vector<int> a(n+1); vector<int> b(n+1); for(int i=0;i<n;i++) { cin>>a[i]; pre[a[i]]++; } for(int i=0;i<n;i++) { cin>>b[i]; pre[b[i]+1]--; } int ans=0; for(int i=1;i<1100;i++) { pre[i] += pre[i-1]; if(pre[i] == n) ans++; } cout<<ans<<endl; return 0; }
//#include <atcoder/all> //using namespace atcoder; #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for(int i=0; i<n; i++) #define REPR(i, n) for(int i=n-1; i>=0; i--) #define FOR(i, m, n) for(int i=m; i<n; i++) #define ALL(v) v.begin(), v.end() #define bit(n) (1LL<<(n)) #define FIX(d) fixed << setprecision(d) using P = pair<int, int>; using LP = pair<ll, ll>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1e9; const ll LINF = (1LL<<60); unsigned int xor128(){ static unsigned int x=123456789,y=362436069,z=521288629,w=88675123; unsigned int t = (x^(x<<11)); x=y; y=z; z=w; return( w=(w^(w>>19))^(t^(t>>8)) ); } int n; int cx[200], cy[200], r[200]; int a[200], b[200], c[200], d[200]; //int ma[200], mb[200], mc[200], md[200]; ll score(){ double sc = 0.0; REP(i,n){ int area = (c[i]-a[i]) * (d[i]-b[i]); double tmp = 1.0 - (double)min(area,r[i]) / max(area,r[i]); sc += 1.0 - tmp*tmp; } sc *= 1e9; sc /= n; return (ll)round(sc); } void init(){ REP(i,n){ a[i] = cx[i]; b[i] = cy[i]; c[i] = cx[i]+1; d[i] = cy[i]+1; } } void add(int ind, int xy){ int area = (c[ind]-a[ind]) * (d[ind]-b[ind]); bool minus = false, plus = false; if(xy&1){ REP(i,n){ if(i==ind) continue; if(max(a[i],a[ind]-1)<min(c[i],c[ind]) && max(b[i],b[ind])<min(d[i],d[ind])){ minus = true; } if(max(a[i],a[ind])<min(c[i],c[ind]+1) && max(b[i],b[ind])<min(d[i],d[ind])){ plus = true; } } if(abs(area-r[ind])>abs(area+d[ind]-b[ind]-r[ind])){ if(c[ind]<10000 && !plus && (xy&2)) c[ind]++; else if(a[ind]>0 && !minus) a[ind]--; } }else{ REP(i,n){ if(i==ind) continue; if(max(a[i],a[ind])<min(c[i],c[ind]) && max(b[i],b[ind]-1)<min(d[i],d[ind])){ minus = true; } if(max(a[i],a[ind])<min(c[i],c[ind]) && max(b[i],b[ind])<min(d[i],d[ind]+1)){ plus = true; } } if(abs(area-r[ind])>abs(area+c[ind]-a[ind]-r[ind])){ if(d[ind]<10000 && !plus && (xy&2)) d[ind]++; else if(b[ind]>0 && !minus) b[ind]--; } } } void les(int ind, int xy){ if(xy&1){ if(c[ind]-a[ind]==1) return; if(a[ind]==cx[ind]) c[ind]--; else if(c[ind]==cx[ind]+1) a[ind]++; else if(xy&2) a[ind]++; else c[ind]--; }else{ if(d[ind]-b[ind]==1) return; if(b[ind]==cy[ind]) d[ind]--; else if(d[ind]==cy[ind]+1) b[ind]++; else if(xy&2) b[ind]++; else d[ind]--; } } const ll LIMIT = 490 * CLOCKS_PER_SEC / 100; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; REP(i,n) cin >> cx[i] >> cy[i] >> r[i]; init(); //ll mx = score(); //cout << mx << endl; ll ti = clock(); while(true){ ll now = clock(); ll sub = now - ti; if(sub > LIMIT) break; REP(loop, 300){ //int ind = xor128() % n; //int xy = xor128() % 2; add(xor128()%n, (xor128()%4)+1); if((loop+1)%30==0) les(xor128()%n, (xor128()%4)+1); } //cout << mx << endl; } REP(i,n) cout << a[i] << " " << b[i] << " " << c[i] << " " << d[i] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define len(a) (int) a.size() #define long long long int nxt() { int a; scanf("%d", &a); return a; } const int N = 55; long dp[N][2]; long a[N]; long k[N]; long s; int can[N]; int n; int main() { n = nxt(); scanf("%lld", &s); for (int i = 0; i < n; ++i) { scanf("%lld", &a[i]); } for (int i = 0; i + 1 < n; ++i) { can[i] = a[i + 1] / a[i] - 1; } for (int i = n - 1; i >= 0; --i) { k[i] = s / a[i]; s %= a[i]; } dp[0][0] = 1; for (int i = 0; i + 1 < n; ++i) { for (int j = 0; j < 2; ++j) { if (dp[i][j] == 0) continue; long cur = k[i]; if (j) { cur++; } if (cur == can[i] + 1) { dp[i + 1][1] += dp[i][j]; } else { dp[i + 1][0] += dp[i][j]; if (cur != 0) { dp[i + 1][1] += dp[i][j]; } } } } printf("%lld", dp[n - 1][0] + dp[n - 1][1]); return 0; }
//libraries #include<bits/stdc++.h> //namespace using namespace std; //typedef //typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; //typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<long long,long long> pll; typedef vector<long long> vll; //define #define i(X) cin>>X; #define ol(X) cout<<X<<endl; #define os(X) cout<<X<<" "; #define inpv(a,n) vector<ll> a(n); for(ll i=0;i<n;i++){cin>>a[i];} #define pb push_back #define pf push_front #define fi first #define se second #define line cout<<"\n"; #define pbp(a,b) push_back(make_pair(a,b)); #define iniv(A,n,p) vector<ll> A(n,p); #define vst(A) sort(A.begin(),A.end()); #define rvst(A) sort(A.rbegin(),A.rend()); #define forin(i,a,b) for(ll i=a;i<=b;i++) #define forde(i,a,b) for(ll i=a;i>=b;i--) #define fio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); const long long inf=(long long)1e18; long long MOD=(long long)(1e9+7); ll mod=MOD; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } int main() { // #ifndef ONLINE_JUDGE // freopen ("input.txt" , "r" , stdin); // // freopen ("OUTPUT.txt" , "w" , stdout); // #endif fio; ll t=1; //cin>>t; while(t--) { ll k,n,m; cin>>k>>n>>m; vector<ll> a(k); vector<ll> b(k); vector<pair<ld,ll> > z(k); ll cur=0; forin(i,0,k-1) { cin>>a[i]; b[i]=(a[i]*m)/n; cur+=b[i]; ld f=(b[i]*n)-(a[i]*m); z.pb({f,i}); } vst(z); ll i=0; while(cur<m) { //cout<<z[i].second<<" "; b[z[i].second]++; i++; cur++; } forin(i,0,k-1) cout<<b[i]<<" "; cout<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(v) v.begin(), v.end() //ll の最大値 2147483647≒10^9 //long longの最大値 9,223,372,036,854,775,807≒10^18 ll gcd(ll a, ll b){ if(a%b==0){ return b; } else{ return gcd(b,a%b); } } ll digit(ll x){ ll d=0; while(x){ d++; x/=10; } return d; } int main(){ cout<<fixed<<setprecision(20); double N; cin>>N; double x,y,x_,y_; cin>>x>>y>>x_>>y_; double cx=(x+x_)/2; double cy=(y+y_)/2; //cout<<cx<<' '<<cy<<endl; x-=cx; y-=cy; double r=sqrt(x*x+y*y); //cout<<r<<endl; //cout<<x<<' '<<y<<endl; double pi=3.141592653589793; double d=2*pi/N; double cos_=cos(d); double sin_=sin(d); double cos=x/r; double sin=y/r; cout<<r*(cos*cos_-sin*sin_)+cx<<' '<<r*(sin*cos_+cos*sin_)+cy<<endl; }
#include "bits/stdc++.h" using namespace std; using ll = int64_t; using vl = vector<ll>; #define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i)) const ll INF = 1e18; ll A, B, C, A2, B2, C2; vl a(200000), t(200000); void fix(ll& X, ll& Y, ll& Z, int idx) { if (t[idx] == 1) { X = a[idx], Y = -INF, Z = INF; } else if (t[idx] == 2) { X = 0, Y = a[idx], Z = INF; } else if (t[idx] == 3) { X = 0, Y = -INF, Z = a[idx]; } } void solve() { int n; cin >> n; ice(i, 0, n) { cin >> a[i] >> t[i]; } fix(A, B, C, 0); ice(i, 1, n) { fix(A2, B2, C2, i); ll X = A2 + A, Y = max(B2, min(A2 + C, A2 + B)), Z = min(C2, max(B2, A2 + C)); A = X, B = Y, C = Z; } int q; cin >> q; ice(i, 0, q) { int x; cin >> x; cout << min(C, max(B, x + A)) << '\n'; } } int main() { int tc = 1; ice(i, 0, tc) { solve(); } }
#include <iostream> #include <map> #include <set> #include <cmath> #include <queue> #include <string> #include <vector> #include <algorithm> #include <functional> using namespace std; long long N, A[1 << 18], T[1 << 18]; long long Q, X[1 << 18]; int main() { // Step #1. 入力 cin >> N; for (int i = 0; i < N; i++) cin >> A[i] >> T[i]; cin >> Q; for (int i = 0; i < Q; i++) cin >> X[i]; // Step #2. 上限と下限を求める long long cl = -(1LL << 60), cr = (1LL << 60), sum = 0; for (int i = 0; i < N; i++) { if (T[i] == 1) { cl += A[i]; cr += A[i]; sum += A[i]; } if (T[i] == 2) { cl = max(cl, A[i]); cr = max(cr, A[i]); } if (T[i] == 3) { cl = min(cl, A[i]); cr = min(cr, A[i]); } } // Step #3. クエリに答える for (int i = 0; i < Q; i++) { long long val = 0; if (cl - sum <= X[i] && X[i] <= cr - sum) { val = X[i] + sum; } else if (X[i] <= cl - sum) { val = cl; } else if (cr - sum <= X[i]) { val = cr; } cout << val << endl; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/modint> #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define INF 2000000000000000000 #define ll long long #define ld long double #define pll pair<ll, ll> using namespace std; 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; } //==================param========== ll depth = 10; //================================= ll si, sj; vector<vector<ll>> board(50, vector<ll>(50)); vector<vector<ll>> point(50, vector<ll>(50)); map<ll, bool> already; vector<char> way_char = {'R', 'D', 'L', 'U'}; vector<vector<ll>> ways = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; stack<ll> max_ans; stack<ll> ans; ll max_point = 0; ll now_point = 0; clock_t start; void get_input() { cin >> si >> sj; for (ll i = 0; i < 50; ++i) { for (ll j = 0; j < 50; ++j) { cin >> board.at(i).at(j); } } for (ll i = 0; i < 50; ++i) { for (ll j = 0; j < 50; ++j) { cin >> point.at(i).at(j); } } already[board.at(si).at(sj)] = true; } bool can_move(ll way) { ll nh = si + ways.at(way).at(0), nw = sj + ways.at(way).at(1); if (nh < 0 || nw < 0 || nh >= 50 || nw >= 50) { return false; } if (already[board.at(nh).at(nw)]) { return false; } return true; } void make_move(ll way) { si += ways.at(way).at(0), sj += ways.at(way).at(1); already[board.at(si).at(sj)] = true; ans.push(way); now_point += point.at(si).at(sj); if (now_point > max_point) { max_ans = ans; max_point = now_point; } } void rev_move() { ll way = ans.top(); ans.pop(); now_point -= point.at(si).at(sj); already[board.at(si).at(sj)] = false; si -= ways.at(way).at(0), sj -= ways.at(way).at(1); } ll cnt = 0; bool fin = false; void dfs() { if (fin) { return; } cnt += 1; if (cnt % 100 == 0) { clock_t end = clock(); ld time = (ld)(end - start) / CLOCKS_PER_SEC * 1000.0; if (time > 1.9) { fin = true; return; } } for (ll i = 0; i < 4; ++i) { if (can_move(i)) { make_move(i); dfs(); rev_move(); } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); start = clock(); get_input(); dfs(); string s_ans = ""; while (!max_ans.empty()) { s_ans += way_char.at(max_ans.top()); max_ans.pop(); } reverse(rng(s_ans)); cout << s_ans << "\n"; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; typedef pair<string,string> PS; typedef pair<ll,PS> PP; const int n=50; ll a[n][n],b[n][n]; ll dr(char c) { if(c=='U') return 0; else if(c=='R') return 1; else if(c=='D') return 2; return 3; } priority_queue<PP,vector<PP>,greater<PP> > que[2500]; void Main() { srand((unsigned)time(NULL)); ll sx,sy; cin >> sx >> sy; rep(i,n)rep(j,n) R a[i][j]; rep(i,n)rep(j,n) R b[i][j]; ll e[4]={0,1,2,3}; string w="URDL"; que[0].push(PP(0,PS("",""))); ll M=-1; string ans=""; rep(l,2000) { while(!que[l].empty()) { PP p=que[l].top();que[l].pop(); string t=p.S.F,r=p.S.S; vector<ll> u(2500,0); ll x=sx,y=sy,sum=0; u[a[x][y]]=1; sum+=b[x][y]; rep(i,t.size()) { ll k=dr(t[i]); x+=dx[k],y+=dy[k]; u[a[x][y]]=1; sum+=b[x][y]; } rep(jj,r.size()+1) { rep(o,10) { string rr=r.substr(0,jj); vector<ll> uu=u; ll ss=sum,x1=x,y1=y; REP(kk,jj,20) { random_shuffle(e,e+4); ll f=0; rep(j,4) { ll x2=x1+dx[e[j]],y2=y1+dy[e[j]]; if(check(n,n,x2,y2)&&!uu[a[x2][y2]]&&a[x1][y1]!=a[x2][y2]) { uu[a[x2][y2]]=1; x1=x2,y1=y2; f=1; rr+=w[e[j]]; ss+=b[x1][x2]; break; } } if(!f) break; } if(ss>M) { M=ss; ans=t+rr; } if(rr.size()==0) break; string tt=t; tt+=rr[0]; rr=rr.substr(1); que[l+1].push(PP(ss,PS(tt,rr))); while(que[l+1].size()>=10) que[l+1].pop(); } if(jj>=t.size()) break; ll k=dr(r[jj]); x+=dx[k],y+=dy[k]; u[a[x][y]]=1; sum+=b[x][y]; } } } pr(ans); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
#include<bits/stdc++.h> #define For(a,b,c) for(int a=b;a<=c;++a) #define Dor(a,b,c) for(int a=b;a>=c;--a) using namespace std; enum{N=207}; int n,X[N],Y[N],R[N],A[N],B[N],C[N],D[N],P[N]; int O(int x,int y,int z) { return x<=y&&y<=z; } int banana(int a,int b) { return ( (O(A[a],A[b],C[a])||O(A[a],C[b],C[a])||O(A[b],A[a],C[b]))&& (O(B[a],B[b],D[a])||O(B[a],D[b],D[a])||O(B[b],B[a],D[b]))); } bool cmp(int x,int y) { return R[x]<R[y]; } int main() { scanf("%d",&n); For(i,1,n) scanf("%d%d%d",X+i,Y+i,R+i),P[i]=i,A[i]=X[i],B[i]=Y[i],C[i]=X[i]+1,D[i]=Y[i]+1; sort(P+1,P+1+n,cmp); For(i,1,n) { int u=P[i],l=1,r=min(int(sqrt(R[u])/2),min(10000-X[u],10000-Y[u])); while(l<=r) { int m=(l+r)>>1,o=1; C[u]=A[u]+m,D[u]=B[u]+m; For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;} if(o) l=m+1; else r=m-1; } C[u]=A[u]+r,D[u]=B[u]+r; l=1,r=min(int(sqrt(R[u])/2),min(X[u],Y[u])); while(l<=r) { int m=(l+r)>>1,o=1; A[u]=X[u]+1-m,B[u]=Y[u]+1-m; For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;} if(o) l=m+1; else r=m-1; } A[u]=X[u]+1-r,B[u]=Y[u]+1-r; l=1,r=min(R[u]/(C[u]-A[u])+Y[u],10000)-Y[u]; while(l<=r) { int m=(l+r)>>1,o=1; D[u]=Y[u]+m; For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;} if(o) l=m+1; else r=m-1; } D[u]=Y[u]+r; l=1,r=Y[u]-max(D[u]-R[u]/(C[u]-A[u]),1); while(l<=r) { int m=(l+r)>>1,o=1; B[u]=Y[u]-m; For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;} if(o) l=m+1; else r=m-1; } B[u]=Y[u]-r; } For(i,1,n) { int u=P[i],l,r; l=1,r=min(R[u]/(D[u]-B[u])+X[u],10000)-X[u]; while(l<=r) { int m=(l+r)>>1,o=1; C[u]=X[u]+m; For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;} if(o) l=m+1; else r=m-1; } C[u]=X[u]+r; l=1,r=X[u]-max(C[u]-R[u]/(D[u]-B[u]),1); while(l<=r) { int m=(l+r)>>1,o=1; A[u]=X[u]-m; For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;} if(o) l=m+1; else r=m-1; } A[u]=X[u]-r; } For(i,1,n) printf("%d %d %d %d\n",A[i],B[i],C[i],D[i]); return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <map> #include <set> //#include <queue> //#include <stack> #include <cstring> #include <cassert> using namespace std; #ifdef LOCAL_DEBUG #include <local_debug.h> #define DEBUG(...) DBG2::print(#__VA_ARGS__, __LINE__, __VA_ARGS__) #else #define DEBUG(...) #endif #define SZ(a) int((a).size()) #define REP(i,n) for(int i=0,_n=(n);i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) typedef long long llong; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<llong> VL; typedef vector<VL> VVL; const llong INF = 3e18; int N, M; VI H, W; int nearest_teacher_height(int h, bool trace = false) { int idx = lower_bound(W.begin(), W.end(), h) - W.begin(); if (trace) DEBUG(idx); if (idx == SZ(W)) { --idx; assert(idx >= 0); if (trace) DEBUG(1); return W[idx]; } if (idx == 0) { if (trace) DEBUG(2); return W[idx]; } if (trace) DEBUG(3, W[idx-1], W[idx]); if (abs(W[idx-1] - h) < abs(W[idx] - h)) return W[idx-1]; else return W[idx]; } llong memo[2][200002]; llong go(int n, bool t) { if (n >= SZ(H)) return 0; llong& res = memo[t][n]; if (res < 0) { bool trace = n == 4 && !t; // match n-th student with next student res = INF; if (n+1 < N) { llong cur = go(n+2, t) + llabs(H[n+1] - H[n]); if (trace) DEBUG("1", cur); res = min(res, cur); } // match n-th student to nearest height of teacher if (!t) { int w = nearest_teacher_height(H[n]); if (trace) DEBUG(H[n], w); llong cur = go(n+1, true) + llabs(H[n] - w); if (trace) DEBUG("2", cur); res = min(res, cur); } } return res; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; H = VI(N); REP(i, N) cin >> H[i]; W = VI(M); REP(j, M) cin >> W[j]; sort(H.begin(), H.end()); sort(W.begin(), W.end()); DEBUG(H); DEBUG(W); // int t = nearest_teacher_height(7, true); // DEBUG(t); memset(memo, -1, sizeof(memo)); llong res = go(0, 0); cout << res << endl; return 0; }
#include <stdio.h> int n; int h[256][256]; int main() { scanf("%d", &n); h[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < (1 << i); j++) { for (int k = 0; k < (1 << i); k++) { h[j + (1 << i)][k] = h[j][k]; h[j][k + (1 << i)] = h[j][k]; h[j + (1 << i)][k + (1 << i)] = -h[j][k]; } } } printf("%d\n", (1 << n) - 1); for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < (1 << n); j++) { if (h[i][j] == 1)printf("A"); else printf("B"); } printf("\n"); } }
#pragma GCC optimize("O3") #include "bits/stdc++.h" #define sz(x) (int)(x).size() using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; cout << (1<<n)-1 << "\n"; for(int i=1; i<=(1<<n)-1; i++) { for(int j=0; j<(1<<n); j++) cout << (__builtin_popcount(i & (j+1)) % 2 ? 'A' : 'B'); cout << "\n"; } return 0; }
#include<bits/stdc++.h> #define ll long long #define re register #define INF 2147483647 using namespace std; inline int read() { int f=1,x=0;char s=getchar(); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=x*10+s-48; s=getchar(); } return f*x; } struct node { int op; int x,y; }; bool cmp(node a,node b) { if(a.x!=b.x) return a.x<b.x; else return a.op<b.op; } const int N=2e5+5; vector<node> v; vector<int> v1[N],v2[N]; const int mxv=2e5+5; int t[mxv+5]; inline int lowbit(int x) { return x&(-x); } inline void updata(int x,int v) { for(;x<=mxv;x+=lowbit(x)) t[x]+=v; } inline int ask(int x) { int ans=0; for(;x;x-=lowbit(x)) ans+=t[x]; return ans; } int main() { int n=read(),m=read(); int k=read(); for(int i=1;i<=k;i++) { int x=read(),y=read(); v1[x].push_back(y-1); v2[y].push_back(x-1); } for(int i=1;i<=n;i++) v1[i].push_back(m); for(int i=1;i<=m;i++) v2[i].push_back(n); for(int i=1;i<=n;i++) sort(v1[i].begin(),v1[i].end()); for(int i=1;i<=m;i++) sort(v2[i].begin(),v2[i].end()); ll ans=v1[1][0]+v2[1][0]-1; //cout<<ans<<endl; for(int i=2;i<=n;i++) { if(i>v2[1][0]) break; //cout<<v1[i][0]<<endl; ans+=v1[i][0]-1; //cout<<ans<<endl; } // cout<<ans<<endl; for(int i=2;i<=n;i++) v.push_back((node){1,i,v1[i][0]}); for(int i=2;i<=m;i++) v.push_back((node){2,v2[i][0],i}); sort(v.begin(),v.end(),cmp); //for(int i=0;i<v.size();i++) printf("%d %d %d\n",v[i].op,v[i].x,v[i].y); for(int i=0;i<v.size();i++) {//printf("%d %d %d\n",v[i].op,v[i].x,v[i].y); if(v[i].op==1) { if(v[i].x>v2[1][0]) continue; //cout<<v[i].y+1<<endl; updata(v[i].y+1,1); } else { //if(v[i].x==0) continue; if(v[i].y>v1[1][0]) continue; //cout<<ask(v[i].y)-ask(mxv)<<endl; ans+=v[i].x-1+ask(v[i].y)-ask(mxv); } //cout<<ans<<endl; } printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(v) (v).begin(),(v).end() using pii = pair<int, int>; 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; } template<typename T> struct SegmentTree{ using OP = function<T(T,T)>; int n, defn; vector<T> node; T t; OP op; SegmentTree(int n_, T t, OP op) : t(t),op(op){ n = 1; defn = n_; while(n < n_) n<<=1; node.assign(2*n,t); } void init(int n_){ n = 1; defn = n_; while(n < n_) n<<=1; node.assign(2*n,t); } void set(int k,T x){ assert(0 <= k && k < n); node[k+n] = x; } void build(){ for(int k = n - 1; k > 0; k--){ node[k] = op(node[2*k],node[2*k+1]); } } void change(int k, T x){ assert(0 <= k && k < n); k += n; node[k] = x; while(k >>= 1){ node[k] = op(node[2*k],node[2*k+1]); } } void change(int l, int r, T x){ for(int k = l; k < r; k++) change(k,x); } T at(int k){ assert(0 <= k && k < n); return node[k+n]; } void add(int k, T a){ assert(0 <= k && k < n); T x = at(k)+a; change(k,x); } T query(int a, int b){ assert(0 <= a && b <= n); if(a==0 && b==defn) return all_query(); T L = t, R = t; for(a += n, b += n; a < b; a >>= 1, b >>= 1) { if(a & 1) L = op(L, node[a++]); if(b & 1) R = op(node[--b], R); } return op(L, R); } T all_query() { return node[1]; } template<typename C> int max_right(int l, C &check){ if(l == defn) return defn; l += n; T tm = t; do{ while(l%2 == 0) l >>= 1; if(!check(op(tm, node[l]))){ while(l < n){ l *= 2; if(check(op(tm, node[l]))){ tm = op(tm, node[l]); l++; } } return l - n; } tm = op(tm, node[l]); l++; }while((l&-l) != l); return defn; } template<typename C> int max_left(int r, C &check){ if(r == 0) return 0; r += n; T tm = t; do{ r--; while(r>1 && (r%2)) r >>= 1; if(!check(op(node[r], tm))){ while(r < n){ r = 2*r + 1; if(check(op(node[r], tm))){ tm = op(node[r], tm); r--; } } return r + 1 - n; } tm = op(node[r], tm); }while((r&-r) != r); return 0; } }; int main(){ int H, W, M; cin >> H >> W >> M; vector<int> height(W, H), width(H, W); vector<vector<int>> ps(H); rep(i, M){ int x, y; cin >> x >> y; x--; y--; chmin(height[y], x); chmin(width[x], y); ps[x].emplace_back(y); } ll ans = 0; SegmentTree<int> seg( W, 0, [](int a, int b) { return a+b; } ); rep(i, W){ if(i < width[0]) ans += height[i]; else seg.set(i, 1); } seg.build(); rep(i, height[0]){ for(auto&&y : ps[i]){ seg.change(y, 1); } ans += seg.query(0, width[i]); } cout << ans << endl; }
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include<bits/stdc++.h> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define pf printf #define sf(a) scanf("%d",&a) #define sfl(a) scanf("%lld",&a) #define sff(a,b) scanf("%d %d",&a,&b) #define sffl(a,b) scanf("%lld %lld",&a,&b) #define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c) #define sfffl(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define sffff(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d) #define sffffl(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d) #define sfffff(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e) #define sfffffl(a,b,c,d,e) scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e) #define sfc(a) scanf("%c",&a) #define pii pair<int,int> #define ms(a,b) memset(a,b,sizeof(a)) #define pb(a) push_back(a) #define pbp(a,b) push_back({a,b}) #define db double #define ft float #define ll long long #define ull unsigned long long #define pii pair<int,int> #define ff first #define ss second #define sz(x) x.size() #define all(x) x.begin(),x.end() #define CIN ios_base::sync_with_stdio(0); cin.tie(0) #define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c) #define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c) #define for0(i,n) for(int i=0;i<n;i++) #define for1(i,n) for(int i=1;i<=n;i++) #define forrev(i,n) for(int i=n-1; i>=0; i--) #define forab(i,a,b) for(int i=a;i<=b;i++) #define forba(i,b,a) for(int i=b;i>=a;i--) #define stlloop(x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++) #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a)*((b)/gcd(a,b))) #define case1(z) cout<<"Case "<<z<<": " #define case2(z) printf("Case %d: ",z) #define PI acos(-1) //3.14159265358979323846264338328 #define valid(tx,ty) tx>=0 && tx<row && ty>=0 && ty<col #define intlim 2147483648 #define N 200005 #define inf 100000008 #define mod 1000000007 /*------------------------------Graph Moves----------------------------*/ //const int fx[]={+1,-1,+0,+0}; //const int fy[]={+0,+0,+1,-1}; //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move /*---------------------------------------------------------------------*/ int main() { db n; cin>>n; ll s = 1.0,d = 1; ll v = min(11ll,(ll)(n-12)); for(ll i=n-1,j=1;j<=v;i=i-1.0,j=j+1.0) { s = s * i / j; //s /= db(j); //pf("%.20f\n",s); //cout<<(ll)s<<" "<<(n-i)<<endl; } cout<<(ll)s<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define int long long #define pb push_back #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define all(v) v.begin(),v.end() #define F first #define S second #define pi pair<int,int> #define vi vector<int> #define mii map<int,int> #define vp vector<pair< int,int> > #define mpi map<pair<int,int> ,int> #define endl "\n" #define fill(A,x) memset(A,x,sizeof A); void solve() { int n; cin>>n; vi pos(n+5),ar(n+5); vi ans; for(int i=1;i<=n;i++) { cin>>ar[i]; pos[ar[i]]=i; } if(n==2) { if(pos[1]==1)cout<<0<<endl; else {cout<<1<<endl;cout<<1;} cout<<endl; return; } for(int i=n;i>0;i--) { if(i>n||pos[i]==i) continue; int c=pos[i]; if(c%2!=(ans.size())%2) { while(pos[i]!=i) { int x=ar[pos[i]+1]; ar[pos[i]]=x;ar[pos[i]+1]=i; ans.pb(pos[i]); pos[i]++;pos[x]--; } } else { if(c%2==0) { ans.pb(1); ans.pb(2); ans.pb(1); pos[ar[1]]+=2;pos[ar[3]]-=2; swap(ar[1],ar[3]); i++; } else { ans.pb(2); ans.pb(1); ans.pb(2); pos[ar[1]]+=2;pos[ar[3]]-=2; swap(ar[1],ar[3]); i++; i++; } } } int x=ans.size(); cout<<x<<endl; for(auto it:ans)cout<<it<<" "; cout<<endl; } signed main() { boost int test=0,t; cin >> t ; //t=1; while(test++ <t){ // cout<<"Case #"<<test<<": "; solve();} }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar('-'); } while(s--){ my_putchar(f[s]+'0'); } } int N; double L[2000+10]; double R[2000+10]; int intersect(int i, int j){ if(L[j] <= L[i] && L[i] <= R[j] ){ return 1; } if(L[j] <= R[i] && R[i] <= R[j] ){ return 1; } if(L[i] <= L[j] && L[j] <= R[i] ){ return 1; } if(L[i] <= R[j] && R[j] <= R[i] ){ return 1; } return 0; } int main(){ int i; rd(N); for(i=(0);i<(N);i++){ int t; int l; int r; rd(t); rd(l); rd(r); if(t == 1){ L[i] = l; R[i] = r; } else if( t == 2){ L[i] = l; R[i] = r-0.5; } else if( t == 3){ L[i] = l+0.5; R[i] = r; } else if( t == 4){ L[i] = l+0.5; R[i] = r-0.5; } if(L[i] > R[i]){ L[i] = R[i] = -1; } } int ans = 0; for(i=(0);i<(N);i++){ int j; for(j=(i+1);j<(N);j++){ ans += intersect(i, j); } } wt_L(ans); wt_L('\n'); return 0; } // cLay version 20210405-1 // --- original code --- // //no-unlocked // int N; // double L[2d3+10], R[]; // // int intersect(int i, int j){ // if(L[j] <= L[i] <= R[j] ) return 1; // if(L[j] <= R[i] <= R[j] ) return 1; // if(L[i] <= L[j] <= R[i] ) return 1; // if(L[i] <= R[j] <= R[i] ) return 1; // return 0; // } // { // rd(N); // rep(i, N){ // int t, l, r; // rd(t, l, r); // if(t == 1){ // L[i] = l; // R[i] = r; // }else if( t == 2){ // L[i] = l; // R[i] = r-0.5; // }else if( t == 3){ // L[i] = l+0.5; // R[i] = r; // }else if( t == 4){ // L[i] = l+0.5; // R[i] = r-0.5; // } // if(L[i] > R[i]) L[i] = R[i] = -1; // } // int ans = 0; // rep(i, N){ // rep(j, i+1, N){ // ans += intersect(i, j); // } // } // wt(ans); // }
#include <iostream> #include <vector> #include <unordered_map> #include <cmath> #include <algorithm> #include <unordered_set> #include <bits/stdc++.h> #include <iomanip> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; //cout << std::fixed << std::setprecision(15) << res << endl; int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); int N; cin >> N; struct edge { int t; int l; int r; edge(int t, int l, int r) : t(t), l(l), r(r) {} edge() : t(0),l(0),r(0) {} }; vector<edge> kukan(N); rep(i, N) { int t,l,r; cin >> t >> l >> r; kukan[i] = edge(t,l,r); } ll res = 0; rep (i, N) { for (int j = i+1; j < N; j++) { edge e1 = kukan[i]; edge e2 = kukan[j]; if (e2.l < e1.l) { edge tmp = e2; e2 = e1; e1 = tmp; } else if (e2.l == e1.l && ((e1.t == 2 || e1.t == 3) && (e2.t == 1 || e2.t == 2))) { edge tmp = e2; e2 = e1; e1 = tmp; } if (e2.l < e1.r) { res++; } else if (e2.l == e1.r) { if ((e1.t == 1 || e1.t == 3) && (e2.t == 1 || e2.t == 2)) { res++; } } } } cout << res << endl; return 0; }
#include <bits/stdc++.h> #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 SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PB push_back #define MP make_pair #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 using namespace std; using ll = long long; using P = pair<ll,int>; using PP = pair<int,pair<int,int>>; const int N = 1e5; const ll INF = 1e18; int n, m, x, y, a, b, t, k; vector<PP> g[N]; ll d[N]; int main() { cin >> n >> m >> x >> y; --x; --y; rep(i, m) { cin >> a >> b >> t >> k; --a; --b; g[a].PB(MP(b, MP(t, k))); g[b].PB(MP(a, MP(t, k))); } rep(i, n) d[i] = INF; d[x] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(MP(d[x], x)); while (q.size()) { P p = q.top(); q.pop(); int pos = p.second; if (p.first > d[pos]) continue; rep(i, SZ(g[pos])) { int to = g[pos][i].first; int ti = g[pos][i].second.first; int ki = g[pos][i].second.second; ll tl = (d[pos] + ki - 1) / ki * ki; ll ta = tl + ti; if (ta >= d[to]) continue; d[to] = ta; q.push(MP(d[to], to)); } } rep(i, n) { // printf("d[%d] = %lld\n", i, d[i]); } if (d[y] == INF) cout << -1 << endl; else cout << d[y] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long n; cin >> n; long c[n][n], k[n] = {}, g[n] = {}, mnx = 0, mny = 0; bool b = 1; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++) cin >> c[i][j]; } for(int i = 1; i < n; i++){ k[i] = c[i][0] - c[0][0]; mnx = min(mnx, k[i]); } for(int i = 1; i < n; i++){ for(int j = 1; j < n; j++){ if(k[j] != c[j][i] - c[0][i]) b = 0; } } for(int i = 1; i < n; i++){ g[i] = c[0][i] - c[0][0]; mny = min(mny, g[i]); } for(int i = 1; i < n; i++){ for(int j = 1; j < n; j++){ if(g[j] != c[i][j] - c[i][0]) b = 0; } } if(b == 0) cout << "No" << endl; else{ for(int i = 0; i < n; i++){ k[i] -= mnx; g[i] -= mny; } long f = c[0][0] - k[0] - g[0]; if(f < 0) cout << "No" << endl; else{ cout << "Yes" << endl; for(int i = 0; i < n; i++){ cout << k[i] + f; if(i == n - 1) cout << endl; else cout << " "; } for(int i = 0; i < n; i++){ cout << g[i]; if(i == n - 1) cout << endl; else cout << " "; } } } }
#include <iostream> #define ll long long using namespace std; const ll MOD=1e9+7; int main() { ll n,p,now,ans=1; cin>>n>>p; now=p-2; n--; while(n){ if(n%2) { ans*=now; ans%=MOD; } now*=now; now%=MOD; n/=2; } ans*=(p-1); ans%=MOD; cout<<ans; }
#include<bits/stdc++.h> using namespace std; 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 int a, b; cin >> a >> b; vector<int> seq(a + b); int sum = 0; int mn = min(a, b); int mx = max(a, b); for(int i = 0; i < mx; i++) { seq[i] = i + 1; sum += seq[i]; } for(int i = mx; i < a + b; i++) { seq[i] = -1 * (i - a + 1); sum += seq[i]; } seq[a + b - 1] -= sum; for(int i : seq) { if(a > b) { cout << i << " "; } else { cout << -i << " "; } } cout << "\n"; return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> using ll=long long; using ld=long double; using namespace std; int main(){ ll n;cin>>n;ll res=0; ll m=n+1;ll ns=0; for(ll i=1;;i++){ ns+=i; if(ns>m)break; res++; } cout<<n+1-res<<endl; }
#include <bits/stdc++.h> using namespace std; #define fo(a,b) for(int64_t a=0;a<b;a++) #define sor(a) sort(a.begin(),a.end()) #define rev(a) reverse(a.begin(),a.end()) #define ll int64_t #define mod 1000000007 #define vl vector<int64_t> int main(){ ll n; cin>>n; ll ub=n+2; ll lb=-1; while(ub-lb>1){ ll mid=(lb+ub)/2; if(mid>=2*mod){ub=mid;} else{if(mid*(mid-1)<=(2*n)+2){lb=mid;} else{ub=mid;}} } cout<<(n+1)-lb+1<<endl; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=n-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define mkp make_pair #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define vtpl(x,y,z) vector<tuple<x,y,z>> #define rev(x) reverse(x); #define lin(x) ll x; cin>>x; #define stin(x) string x; cin>>x; #define yn(x) if(x) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; } #define YN(x) if(x) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } #define co(x) cout<<x<<endl; using ll=long long; using ld=long double; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll mod=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={1,0,-1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } ll powmod(ll n, ll k, ll m) { ll r=1; for(; k>0; k >>=1) { if(k&1) r=(r*n)%m; n=(n*n)%m; } return r; } ll fact(ll n) { ll a=1; rep(i,n) { a=a*(n-i); } return a; } ll pow(ll a, ll b) { ll x=1; rep(i,b) { x=x*a; } return x; } int main() { lin(S); lin(P); ll x=sqrt(P)+3; repl(i,1,x) { ll y=S-i; if((P%i==0) &&(y==P/i)) { co("Yes"); return 0; } } co("No"); }
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define ll long long using namespace std; int read() { char ch=getchar();int f=0,x=1; while(ch<'0'||ch>'9'){if(ch=='-') x=-1;ch=getchar();} while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();} return f*x; } char s[5000005],t[55]="0atcoder"; int main() { int T=read(); while(T--) { scanf("%s",s+1); int n=strlen(s+1); bool ok=1; for(int i=1;i<=n;i++) if(s[i]!='a') ok=0; if(ok) puts("-1"); else { bool ok=1,no=1; for(int i=1;i<=min(n,7);i++) if(s[i]>t[i]) { puts("0");ok=0;break; } else if(s[i]<t[i]) {no=0;break; } if(ok==0) continue; if(no&&strlen(s+1)>7) { puts("0"); continue; } for(int i=1;i<=n;i++) { if(s[i]!='a') { int ans=i-1; if(s[i]>'t') ans--; cout<<ans<<endl; break; } } } } }
#include<bits/stdc++.h> int main(){ int a,b,c,d; scanf("%d%d%d",&a,&b,&c); d=a+b+c; puts(d==a*3||d==b*3||d==c*3?"Yes":"No"); }
#include<bits/stdc++.h> using ll= long long; #define REP(i,n) for(ll i=0;i<ll(n);i++) #define RREP(i,n) for(ll i=1;i<ll(n+1);i++) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define ALL(x) x.begin(),x.end() #define INF (int)1e9 //10^9:∞ #define LLINF (long long)1e12 #define MOD (int)1e9+7 //10^9+7:合同式の法 #define PI 3.141592653589 #define PB push_back #define Fir first #define Sec second #define __MAGIC__ ios::sync_with_stdio(false);cin.tie(nullptr); #define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;} #define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;} #define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;} #define Graph vector<vector<int>> #define PII pair<int,int> #define VI vector<int> #define VVI vector<vector<int>> // Graph #define VPII vector<pair<int,int>> #define VPLL vector<pair<ll, ll>> #define DDD fixed<<setprecision(17) using namespace std; /*..................DEFINE GLOBAL VARIABLES...................*/ /*.....................DEFINE FUNCTIONS ......................*/ template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } /*.........................memo0 ...........................*/ /*.........................memo1 ...........................*/ /*.........................memo2 ...........................*/ signed main() { __MAGIC__ int a, b, c; cin >> a >> b >> c; int m1 = 0, m2 = 0, m3 = 0; chmax(m1, a); chmax(m1, b); chmax(m1, c); bool isB = false; if(m1 != a) { chmax(m2, a); isB = true; } if (m1 != b) { chmax(m2, b); isB = true; } if (m1 != c) { chmax(m2, c); isB = true; } if (!isB) { m2 = m1; } bool isC = false; if(m1 != a && m2 != a) { chmax(m3, a); isC = true; } if (m1 != b && m2 != b) { chmax(m3, b); isC = true; } if (m1 != c && m2 != c) { chmax(m3, c); isC = true; } if (!isC) { m3 = m2; } YesNo(m3-m2 == m2-m1); return 0; }
//Never stop trying #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) typedef string str; typedef long long ll; #define int ll typedef double db; typedef long double ld; typedef pair<int, int> pi; #define fi first #define se second typedef vector<int> vi; typedef vector<pi> vpi; typedef vector<str> vs; typedef vector<ld> vd; #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define endl "\n" #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) const int MOD = 1e9 + 7; //998244353 const ll INF = 1e18; const int MX = 2e5 + 10; const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up template<class T> using V = vector<T>; template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x)) #define dbg(x) cerr << " - " << #x << " : " << x << endl; #define dbgs(x,y) cerr << " - " << #x << " : " << x << " / " << #y << " : " << y << endl; #define dbgv(v) cerr << " - " << #v << " : " << endl << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl; void IO() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int power[4000009]; int32_t main() { boost; IO(); power[0]=1; FOR(i,1,4000009) power[i]=(power[i-1]*2)%MOD; int N,M; cin>>N>>M; char g[N][M]; int a[N][M],b[N][M],c[N][M],d[N][M]; int K=0; FOR(i,0,N) FOR(j,0,M){ cin>>g[i][j]; if(g[i][j]=='.') K++; } //L FOR(i,0,N) FOR(j,0,M){ if(g[i][j]=='#'){ a[i][j]=0; continue; } a[i][j]=1; if(j) a[i][j]+=a[i][j-1]; } //R FOR(i,0,N) ROF(j,0,M){ if(g[i][j]=='#'){ b[i][j]=0; continue; } b[i][j]=1; if(j+1<M) b[i][j]+=b[i][j+1]; } //U FOR(j,0,M) FOR(i,0,N){ if(g[i][j]=='#'){ c[i][j]=0; continue; } c[i][j]=1; if(i) c[i][j]+=c[i-1][j]; } FOR(j,0,M) ROF(i,0,N){ if(g[i][j]=='#'){ d[i][j]=0; continue; } d[i][j]=1; if(i+1<N) d[i][j]+=d[i+1][j]; } /*FOR(i,0,N){ FOR(j,0,M) cout << d[i][j] << ' '; cout << endl; }*/ int ans=0; FOR(i,0,N) FOR(j,0,M)if(g[i][j]=='.'){ int y=a[i][j]+b[i][j]+c[i][j]+d[i][j]-3; //cout << y << endl; int x=power[y]-1; x%=MOD; //cout << x << endl; x*=power[K-(y)]; x%=MOD; ans+=x; ans%=MOD; } cout << ans << endl; return 0; } /* Careful!!! .Array bounds .Infinite loops .Uninitialized variables / empty containers .Order of input Some insights: .Binary search .Graph representation .Write brute force code .Change your approach */
#include<bits/stdc++.h> #define int long long using namespace std; constexpr int mod=1e9+7; int p2[4000001],y[2000][2000],t[2000][2000]; signed main(){ int h,w; string s[2000]; cin>>h>>w; for(int i=0;i<h;i++) cin>>s[i]; for(int i=0;i<h;i++){ int tmp=0; for(int j=0;j<w;j++){ if(s[i][j]=='#'){ for(int k=tmp;k<j;k++) y[i][k]=j-tmp; tmp=j+1; } } for(int j=tmp;j<w;j++) y[i][j]=w-tmp; } for(int j=0;j<w;j++){ int tmp=0; for(int i=0;i<h;i++){ if(s[i][j]=='#'){ for(int k=tmp;k<i;k++) t[k][j]=i-tmp; tmp=i+1; } } for(int i=tmp;i<h;i++) t[i][j]=h-tmp; } p2[0]=1; for(int i=1;i<=h*w;i++) p2[i]=p2[i-1]*2%mod; int cnt=0; for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(s[i][j]=='.') cnt++; int ans=0; for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(s[i][j]=='.') (ans+=p2[cnt-y[i][j]-t[i][j]+1]*(p2[y[i][j]+t[i][j]-1]+mod-1))%=mod; cout<<ans<<endl; }
#include <bits/stdc++.h> #include <iostream> #include <string> using namespace std; #define rep(i,n) for(long long i=0; i<(long long)(n);i++) //rep…「0からn-1まで」の繰り返し #define rep2(i,s,n) for(long long i=s; i<=(long long)(n);i++) //rep2…「sからnまで」の繰り返し #define repr(i,s,n) for(long long i=s;i>=(long long)(n);i--) //repr…「sからnまで」の降順の繰り返し typedef long long ll; const ll inf = 1e18+7; const ll mod = 1e9+7; int main(){ string n; cin>>n; int cnt1=0,cnt2=0,sum=0; rep(i,n.size()){ int m=n[i]-'0'; sum+=m; if(m%3==1)cnt1++; else if(m%3==2)cnt2++; } int ans=-1; if(sum%3==0) ans=0; else if(sum%3==1){ if(cnt1>=1&&n.size()>1)ans=1; else if(cnt2>=2&&n.size()>2)ans=2; } else { if(cnt2>=1&&n.size()>1)ans=1; else if(cnt1>=2&&n.size()>2)ans=2; } cout<<ans<<endl; }
#include<iostream> #include<vector> #include<algorithm> #include<stack> #include<map> #include<set> #include<queue> #include<string.h> #include<math.h> #define ll long long #define FOR(i,a,b) for(int i=a;i<b;i++) #define pb push_back #define F first #define S second #define MAX 500005 #define MOD (ll)(1e9+7) #define INF (ll)(1e18) #define AC ios::sync_with_stdio(0);cin.tie(0); using namespace std; signed main(){ AC; int n; ll a[200005],pre[200005]={0},maxn=0; cin>>n; FOR(i,1,n+1){ cin>>a[i]; pre[i]=pre[i-1]+a[i]; } ll now=0,ans=0; FOR(i,1,n+1){ ans=max(ans,now+maxn); maxn=max(maxn,pre[i]); now+=pre[i]; } ans=max(ans,now); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; const long long INF = 10000000000000007; const long long mod = 1000000007; typedef long long ll; typedef pair<int, int> P; template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } vector<int> table(2500); vector<vector<int>> tile(2500); vector<vector<bool>> seen(50,vector<bool>(50,0)); vector<vector<int>>point(50,vector<int>(50)); int now_x, now_y; // ll sum = 0; vector<int> dx = {1, 0, -1, 0}; vector<int> dy = {0, 1, 0, -1}; vector<int>ans; void solve(int now_x, int now_y) { while (1) { int num = table[now_x+now_y*50]; for (int j = 0; j < tile[num].size(); j++) { int k = tile[num][j]; seen[k/50][k%50] = 1; } int nx,ny,ma=0,index=-1; for (int i = 0; i < 4; i++) { nx = now_x + dx[i]; ny = now_y + dy[i]; if (nx<0||nx>=50||ny<0||ny>=50) continue; if (seen[ny][nx]==1) continue; if (chmax(ma, point[ny][nx])) { index = i; } } if (index==-1) break; now_x = now_x + dx[index]; now_y = now_y + dy[index]; ans.push_back(index); } return; } int main() { int si, sj; cin >> sj >> si; now_x = si; now_y = sj; for (int i = 0; i < 2500; i++) { int num; cin >> num; table[i] = num; tile[num].push_back(i); } for (int i = 0; i < 2500; i++) { int x = i%50; int y = i/50; cin >> point[y][x]; } solve(now_x, now_y); // 出力 for (int i = 0; i < ans.size(); i++) { if (ans[i]==0) cout << 'R'; else if (ans[i]==1) cout << 'D'; else if (ans[i]==2) cout << 'L'; else if (ans[i]==3) cout << 'U'; } cout << endl; }
#include <iostream> #include <queue> using namespace std; int R, C; int A[501][501], B[501][501]; int dijkstra() { bool used[501][501] = {}; int minScore[501][501] = {}; for(int i = 0;i < 501;i++) { for(int j = 0;j < 501;j++) { minScore[j][i] = 1000000000; } } priority_queue<pair<int, pair<int, int>>> que; que.push(make_pair(0, make_pair(0, 0))); while(que.size()) { pair<int, pair<int, int>> now = que.top(); que.pop(); int score = -now.first; int x = now.second.first; int y = now.second.second; if (x == C-1 && y == R-1) { return score; } if (used[x][y]) continue; // cout << y + 1 << ", " << x + 1 << " = " << score << endl; used[x][y] = true; // 1 int nextScore, toX, toY; if (x < C-1) { nextScore = score + A[x][y]; toX = x + 1; toY = y; if(!used[toX][toY] && minScore[toX][toY] > nextScore) { minScore[toX][toY] = nextScore; que.push(make_pair(-nextScore, make_pair(toX, toY))); } } // 2 if (x > 0) { nextScore = score + A[x-1][y]; toX = x - 1; toY = y; if(!used[toX][toY] && minScore[toX][toY] > nextScore) { minScore[toX][toY] = nextScore; que.push(make_pair(-nextScore, make_pair(toX, toY))); } } // 3 if (y < R-1) { nextScore = score + B[x][y]; toX = x; toY = y + 1; if(!used[toX][toY] && minScore[toX][toY] > nextScore) { minScore[toX][toY] = nextScore; que.push(make_pair(-nextScore, make_pair(toX, toY))); } } // 4 for (int i = 1;i <= y;i++) { nextScore = score + 1 + i; toX = x; toY = y - i; if(!used[toX][toY] && minScore[toX][toY] > nextScore) { minScore[toX][toY] = nextScore; que.push(make_pair(-nextScore, make_pair(toX, toY))); } } } return 0; } int main() { cin >> R >> C; for(int i = 0;i < R;i++){ for(int j = 0;j < C-1;j++) { cin >> A[j][i]; } } for(int i = 0;i < R-1;i++){ for(int j = 0;j < C;j++) { cin >> B[j][i]; } } int ans = dijkstra(); cout << ans << endl; return 0; }