code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include <bits/stdc++.h> using namespace std; using ll = long long; using que_type = tuple<int, pair<int, int>, vector<int>>; void solve(long long si, long long sj, std::vector<std::vector<long long>> t, std::vector<std::vector<long long>> p){ priority_queue<que_type, vector<que_type>, greater<que_type>> que; que.push(make_tuple(p[si][sj], make_pair(si, sj), vector<int>{(int)t[si][sj]})); int times = 1000; int beam = 10; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int ans = 0; vector<int> ansv; while(times--){ priority_queue<que_type, vector<que_type>, greater<que_type>> next_que; while(!que.empty()){ que_type q = que.top(); int score; pair<int, int> pos; vector<int> tiles; tie(score, pos, tiles) = q; que.pop(); for(int i = 0;i < 4;i++){ int nx = dx[i] + pos.first; int ny = dy[i] + pos.second; if(0 <= dx[i] + pos.first && dx[i] + pos.first < 50 && 0 <= dy[i] + pos.second && dy[i] + pos.second < 50){ bool f = false; for(int x : tiles){ if(x == t[nx][ny]){ f = true; break; } } if(f == false){ tiles.push_back(t[nx][ny]); next_que.push(make_tuple(score + p[nx][ny], make_pair(nx, ny), tiles)); tiles.pop_back(); if(score + p[nx][ny] > ans){ ans = score + p[nx][ny]; ansv = tiles; } } } } } while(next_que.size() > beam) next_que.pop(); que = next_que; //cout<<que.size()<<endl; //cout<<ans<<endl; } int pi = si; int pj = sj; string ans_str; for(int i = 1;i < ansv.size();i++){ bool f = false; //cout<<ansv[i]<<" "; for(int idx = 0;idx < 4;idx++){ int nx = pi + dx[idx]; int ny = pj + dy[idx]; if(0 <= nx && nx < 50 && 0 <= ny && ny < 50){ if(t[nx][ny] == ansv[i]){ //cout<<idx<<" "; if(idx == 0){ ans_str.push_back('D'); }else if(idx == 1){ ans_str.push_back('R'); }else if(idx == 2){ ans_str.push_back('U'); }else if(idx == 3){ ans_str.push_back('L'); } f = true; } } if(f){ pi = nx; pj = ny; break; } } } cout<<ans_str<<endl; } int main(){ long long si; scanf("%lld",&si); long long sj; scanf("%lld",&sj); std::vector<std::vector<long long>> t(49+1, std::vector<long long>(49+1)); for(int i = 0 ; i < 49+1 ; i++){ for(int j = 0 ; j < 49+1 ; j++){ scanf("%lld",&t[i][j]); } } std::vector<std::vector<long long>> p(49+1, std::vector<long long>(49+1)); for(int i = 0 ; i < 49+1 ; i++){ for(int j = 0 ; j < 49+1 ; j++){ scanf("%lld",&p[i][j]); } } solve(si, sj, std::move(t), std::move(p)); return 0; }
#include<bits/stdc++.h> using namespace std; #define hi {ios_base::sync_with_stdio(false);} #define mani_jo {cin.tie(NULL);} typedef long long ll; typedef unsigned long long ull; typedef vector<ll> vi; #define pp pair<ll,ll> #define MAX 10000000 #define all(v) ((v).begin()), ((v).end()) #define sz(v) ((int)((v).size())) #define clr(v, d) memset(v, d, sizeof(v)) #define sp " " #define line "\n" const int MOD = 1e9+7; /* ll power(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } */ void solve(ll tc = 0) { ll n,q; cin>>n>>q; ll a[n]; vi ans(n); for(ll i=0;i<n;i++) cin>>a[i]; for(ll i=0;i<n;i++) { ans[i] = a[i] - (i+1); } while(q--) { ll k; cin>>k; const int idx = lower_bound(all(ans),k) - ans.begin(); if(idx == n) cout<<a[n-1] +( k - ans[n-1])<<line; else cout<<a[idx] - (ans[idx] - k +1)<<line; } } int main() { hi mani_jo ll T{1}; // cin>>T; for(ll t=0;t<T;t++) solve(t); return 0; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int N, M, K; int to[200001], ne[200001], he[100001]; int D[100001], C[20], Q[100001], q; int DD[20][20]; int dp[1 << 17][17]; const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } int main() { //cin.tie(0); //ios::sync_with_stdio(false); N = getint(); M = getint() * 2; rep1(i, M) { int a = getint(), b = getint(); to[i] = b; ne[i] = he[a]; he[a] = i++; to[i] = a; ne[i] = he[b]; he[b] = i; } K = getint(); rep(i, K) C[i] = getint(); if (K == 1) { printf("1"); return 0; } const int dekai = 1e8, choidekai = 5e6; for (int p = 1; p < K; p++) D[C[p]] = dekai + p; rep(k, K - 1) { int b = -choidekai * k; D[C[k]] = b - choidekai; int mada = 0; for (int i = K - 1; i > k; i--) mada |= (1 << i); int p = 0; q = 0; Q[q++] = C[k]; while (mada && p < q) { int i = Q[p++]; int d = D[i] + 1; for (int t = he[i]; t; t = ne[t]) { int T = to[t]; if (D[T] >= b) { if (D[T] >= dekai) mada ^= 1 << (D[T] - dekai); D[T] = d; Q[q++] = T; } } } rep(p, K) { if (p > k) { DD[k][p] = D[C[p]] - b + choidekai; DD[p][k] = D[C[p]] - b + choidekai; } if (p > k + 1) D[C[p]] = dekai + p; } } rep(i, K) dp[1 << i][i] = 1 - choidekai; rep1(b, (1 << K) - 1) { int X[17], Y[17]; auto x = X, y = Y; rep(i, K) { if (b >> i & 1) *x++ = i; else *y++ = i; } auto dp0 = dp[b]; for (auto j = Y; j < y; j++) { int saishou = 0; for (auto i = X; i < x; i++) { if (saishou > dp0[*i] + DD[*j][*i]) saishou = dp0[*i] + DD[*j][*i]; } dp[1 << *j | b][*j] = saishou; } } int kotae = 0; rep(i, K) chmin(kotae, dp[(1 << K) - 1][i]); if (kotae < 0) printf("%d", kotae + choidekai); else printf("-1"); Would you please return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long i64; typedef unsigned long long ui64; typedef vector<i64> vi; typedef vector<vi> vvi; typedef pair<i64, i64> pi; #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) inline void RI(i64 &i) {scanf("%lld", &(i));} inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } } inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } } inline void WI(const i64 &i) {printf("%lld\n", i);} inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); } inline void WS(const string &s) { printf("%s\n", s.c_str()); } inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} } inline void YESNO(bool b) { WB(b, "YES", "NO"); } #define BUF_LENGTH 1000000 inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;} template<typename T> inline bool IN(T &S, const typename T::key_type &key) { return S.find(key) != S.end(); } int main(int argc, char *argv[]) { i64 i, j, k; i64 N, M; cin >> N >> M; vvi E0(N); REP(0, M, i) { i64 a, b; RI(a); RI(b); a--; b--; E0[a].pb(b); E0[b].pb(a); } i64 K; cin >> K; vi C(K); RVI(C); vi dists0(N, -1); vvi dists(K, vi(K, -1)); REP(0, K, i) { C[i]--; } if(K == 1) { cout << 1 << endl; return 0; } REP(0, K, i) { fill(all(dists0), -1); queue<i64> q; dists0[C[i]] = 0; q.push(C[i]); while(!q.empty()) { i64 cur = q.front(); q.pop(); for(auto next: E0[cur]) { if(dists0[next] == -1) { dists0[next] = dists0[cur] + 1; q.push(next); } } } REP(0, K, j) { dists[i][j] = dists0[C[j]]; } } /* REP(0, K, i) { REP(0, K, j) { assert(dists[i][j] == dists[j][i]); } } */ i64 B = 1 << K; vvi ans(B, vi(K, -1)); fill(all(ans[0]), 0); using ppi = pair<i64, pi>; // dist, {b, k} set<ppi> q; REP(0, K, i) { q.insert({0, {1<<i, i}}); } while(!q.empty()) { auto t = *q.begin(); q.erase(q.begin()); i64 d = t.first, b = t.second.first, i = t.second.second; REP(0, K, j) { if(i != j && dists[i][j] != -1) { i64 bn = b | (1 << j); if(bn == b) { continue; } i64 dn = d + dists[i][j]; if(ans[bn][j] == -1 || dn < ans[bn][j]) { auto it = q.find({ans[bn][j], {bn, j}}); assert(ans[bn][j] == -1 || it != q.end()); if(it != q.end()) { q.erase(it); } ans[bn][j] = dn; q.insert({ans[bn][j], {bn, j}}); } } } } i64 res = -1; REP(0, K, i) { if(ans.back()[i] != -1) { if(res == -1) { res = ans.back()[i] + 1; } else { res = min(res, ans.back()[i] + 1); } } } printf("%lld\n", res); return 0; }
// #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define ll long long const ll p = 1e9 + 7; const ll nax = 55; ll ps[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; ll dp[(1 << 20)]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll a, b, ans = 0; cin >> a >> b; vector<ll> ms; for (ll i = a; i <= b; ++i) { ll mask = 0; for (int j = 0; j < 20; ++j) { if (i % ps[j] == 0) { mask |= (1 << j); } } ms.push_back(mask); } dp[0] = 1; for (auto& x : ms) { for (int mask = 0; mask < (1 << 20); ++mask) { if ((mask & x) == x) { dp[mask] += dp[mask ^ x]; } } } for (int mask = 0; mask < (1 << 20); ++mask) { ans += dp[mask]; } cout << ans; }
#include <bits/stdc++.h> #include <cassert> #define rep(i, N) for (int i = 0; i < (N); ++i) #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep3(i, a, b) for (ll i = a; i >= b; --i) #define pb push_back #define eb emplace_back #define fi first #define se second #define all(c) begin(c), end(c) #define ok() puts(ok ? "Yes" : "No"); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vi>; using vii = vector<ii>; using gt = greater<int>; using minq = priority_queue<int, vector<int>, gt>; using P = pair<ll, ll>; template <class T> void takeUnique(vector<T> &v) { auto last = std::unique(v.begin(), v.end()); v.erase(last, v.end()); } const ll LINF = 1e18L + 1; const int INF = 1e9 + 1; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1}; // clang++ -std=c++11 -stdlib=libc++ int main() { ll B, C; cin >> B >> C; ll ans = 0; if (C == 1) { if (B == 0) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } if (B == 0) { ans = (2 * C - 1) / 2 + 1; } else if (B > 0) { if (B - C / 2 > (-B + (C - 1) / 2)) { ans = (C - 1) / 2 + (C - 1) / 2 + C / 2 + (C - 2) / 2 + 2; } else { ans = B + (C - 2) / 2 - (-B - (C - 1) / 2) + 1; } } else { if (-B - (C - 1) / 2 > B + (C - 2) / 2) { ans = (C - 1) / 2 + (C - 1) / 2 + C / 2 + (C - 2) / 2 + 2; } else { ans = (-B + (C - 1) / 2) - (B - C / 2) + 1; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define MAXN 2000005 #define ll long long #define F first #define S second #define MOD 1000000007 #define ll long long using namespace std; ll a, b, c, d; void solve() { cin >> a >> b >> c >> d; ll ans = (b - 1ll*c * d); if(ans == 0) { cout << -1; return; } if(-a*1ll/ ans < 0) { cout << -1 ; return; } ans = -a / ans + ((a % ans) ? 1 : 0); cout << ans << "\n"; } int main() { ios_base :: sync_with_stdio(0); cin.tie(); cout.tie(); if(fopen(".inp", "r")) { freopen(".inp", "r", stdin); freopen(".out", "w", stdout); } int t; t = 1; //cin >> t; while(t--) { //cerr << t << '\n'; solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using uli = unsigned long int; using ll = long long; #define rep(i, x) for (int i = 0; i < x; i++) #define REP(i, x) for (int i = 1; i <= x; i++) #define chmin(a, b) \ if (a > b) a = b #define chmax(a, b) \ if (a < b) a = b int main() { double V, T, S, D; cin >> V >> T >> S >> D; double t = D / V; if (t >= T && t <= S) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define int long long using ll = long long; using P = pair<ll, ll>; const ll INF = (1LL << 61); ll mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x = 0) :x((x%mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream& operator>>(istream& is, mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } signed main() { ios::sync_with_stdio(false); cin.tie(0); ll N, M, K; cin >> N >> M >> K; if (N == 1) { mint ans = mint(K).pow(M); cout << ans << endl; return 0; } if(M == 1){ mint ans = mint(K).pow(N); cout << ans << endl; return 0; } mint ans = 0; for (int i = 1; i <= K; i++) { mint now = 0; now = mint(i).pow(N); now -= mint(i - 1).pow(N); mint now2 = 0; now2 = mint(K -i + 1).pow(M); now *= now2; ans += now; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; namespace Sakurajima_Mai{ #define ms(a) memset(a,0,sizeof(a)) #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ? #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i) #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to) #define ce(i,r) i==r?'\n':' ' #define pb push_back #define all(x) x.begin(),x.end() #define gmn(a,b) a=min(a,b) #define gmx(a,b) a=max(a,b) #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef double db; const int infi=2e9;//infi较大,注意涉及inf相加时爆int const ll infl=4e18; inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; } inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; } //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0)); } using namespace Sakurajima_Mai; namespace Fast_Read{ inline int qi(){ int f=0,fu=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); } while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); } return f*fu; } inline ll ql(){ ll f=0;int fu=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); } while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); } return f*fu; } inline db qd(){ char c=getchar();int flag=1;double ans=0; while((!(c>='0'&&c<='9'))&&c!='-') c=getchar(); if(c=='-') flag=-1,c=getchar(); while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar(); if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();} return ans*flag; } } namespace Read{ #define si(a) scanf("%d",&a) #define sl(a) scanf("%lld",&a) #define sd(a) scanf("%lf",&a) #define ss(a) scanf("%s",a) #define rai(x,a,b) repi(i,a,b) x[i]=qi() #define ral(x,a,b) repi(i,a,b) x[i]=ql() } namespace Out{ #define pi(x) printf("%d",x) #define pl(x) printf("%lld",x) #define ps(x) printf("%s",x) #define pc(x) printf("%c",x) #define pe() puts("") } namespace DeBug{ #define MARK false #define DB if(MARK) #define pr(x) cout<<#x<<": "<<x<<endl #define pra(x,a,b) cout<<#x<<": "<<endl; \ repi(i,a,b) cout<<x[i]<<" "; \ puts(""); #define FR(a) freopen(a,"r",stdin) #define FO(a) freopen(a,"w",stdout) } using namespace Fast_Read; using namespace Read; using namespace Out; using namespace DeBug; const int mod=998244353; int qpow(int x,int n,int mod){ int res=1;while(n){ if(n&1) res=(1ll*res*x)%mod; x=1ll*x*x%mod,n>>=1; } return res; } int main() { int n=qi(),m=qi(),k=qi(); if(n>m) swap(n,m); if(n==1){ pi(qpow(k,m,mod)),pe(); return 0; } int ans=0; repi(i,1,k){ int a=(1ll*qpow(i,n,mod)-1ll*qpow(i-1,n,mod)+mod)%mod; int b=qpow(k-i+1,m,mod); ans=(1ll*ans+1ll*a*b%mod)%mod; } pl(pos_mod(ans,mod)),pe(); return 0; }
#include <bits/stdc++.h> using namespace std; template<class T> ostream& operator<<(ostream &os, vector<T> V) { os << "[ "; for(auto v:V)os << v << " "; return os << "]"; } template<class T> ostream& operator<<(ostream &os, set<T> S){ os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; } template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) { return os<<"("<<P.first<<","<< P.second << ")"; } template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) { os<<"{ ";for(auto m:M)os<<"("<<m.first<<":"<<m.second<<")"; return os<<"}"; } #define cerr cout #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(...) 1 #endif // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // find_by_order(k) returns iterator to kth element starting from 0; // order_of_key(k) returns count of elements strictly smaller than k; //For multiset use less_equal operator but it does support erase operations for multiset #define ll long long #define ld long double #define vll vector<ll> #define pll pair<ll,ll> #define vpll vector<pll> #define I insert #define pb push_back #define F first #define S second #define endl "\n" #define all(v) (v).begin(),(v).end() #define For(i,n) for(int i=0;i<(int)n;++i) #define Rev(i,n) for(int i=(int)n-1;i>=0;--i) #define Rep(i,n) for(int i=1;i<=(int)n;++i) typedef vector<int> vi; typedef pair<int,int> pii; typedef pair<pii,int> ppi; typedef vector<pii> vpi; #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define mp make_pair // inline void modadd(int &a,int &b){a+=b;if(a>=mod)a-=mod;} const int N = 2e5 + 100; int P[N]; int idx[N]; int A[N]; int rev[N]; int main(){ fio; cout<<fixed<<setprecision(25); int n; cin >> n; int x; for(int i = 1;i <= n ; ++i){ cin >> x; A[i] = x; rev[A[i]] = i; } int lft = n-1; vi opr; for(int i = 1;i <= n ; ++i){ if(A[i] == i) continue; int idx = rev[i]; assert(idx > i); while(idx != i){ opr.pb(idx-1); swap(A[idx],A[idx-1]); rev[A[idx]] = idx; rev[A[idx-1]] = idx-1; idx--; } if(opr.size() >= n) break; } if(opr.size() == n-1){ for(auto it : opr) cout << it << endl; //cout << endl; } else cout << -1 << endl; }
// #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++) #define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--) #define mst(a,x) memset(a,x,sizeof(a)) #define fi first #define se second mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); // int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0); typedef long long ll; const int inf=~0u>>2; const ll INF=~0ull>>2; ll read(){ll x; if(scanf("%lld",&x)!=1)exit(0); return x;} typedef double lf; const lf pi=acos(-1.0); lf readf(){lf x; if(scanf("%lf",&x)!=1)exit(0); return x;} typedef pair<ll,ll> pii; template<typename T> vector<T> &operator<<(vector<T> &a,T b){a.push_back(b); return a;} template<typename T> T sqr(T x){return x*x;} void print(ll x,bool e=0){printf("%lld%c",x," \n"[e]);} const int N=1010; const ll mod=(0?1000000007:998244353); ll mul(ll a,ll b,ll m=mod){return a*b%m;} ll qpow(ll a,ll b,ll m=mod){ll ans=1; for(;b;a=mul(a,a,m),b>>=1)if(b&1)ans=mul(ans,a,m); return ans;} #define int ll char s[10]; vector<int> a[N][26]; int dis[N][N]; queue<pii> q; void Solve(){ int n=read(),m=read(); repeat(i,0,m){ int x=read()-1,y=read()-1; scanf("%s",s); a[x][*s-'a'].push_back(y); a[y][*s-'a'].push_back(x); } mst(dis,-1); dis[0][n-1]=0; q.push(pii(0,n-1)); repeat(c,0,26) for(auto px:a[0][c])if(px==n-1){ print(1,1); return; } ll ans=inf; while(!q.empty()){ int x=q.front().fi,y=q.front().se; q.pop(); if(x==y){ ans=min(ans,dis[x][y]); } repeat(c,0,26){ for(auto px:a[x][c]) for(auto py:a[y][c]){ if(dis[px][py]==-1){ dis[px][py]=dis[py][px]=dis[x][y]+2; q.push(pii(px,py)); } } for(auto px:a[x][c])if(px==y){ ans=min(ans,dis[x][y]+1); } } } if(ans==inf)print(-1,1); else print(ans,1); } signed main(){ // freopen("data.txt","r",stdin); int T=1; // T=read(); repeat(ca,1,T+1){ Solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; template<typename T> using V = vector<T>; template<typename T> using P = pair<T, T>; template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); } template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); } template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;} template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());} template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++) {cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}}; template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0]; for(ll i=1;i<n;i++)cerr spa v[i]; cerr<<"\n";}; const ll INF = (1ll<<62); // const ld EPS = 1e-10; // const ld PI = acos(-1.0); const ll mod = (int)1e9 + 7; //const ll mod = 998244353; int main(){ ll N; cin >> N; V<ll> A(N), B(N); REP(i, N) cin >> A[i]; REP(i, N) cin >> B[i]; V<ll> C(N); REP(i, N) C[i] = B[i] - A[i]; ll res = 0; REP(i, N){ res += A[i]; } V<ll> odd, even; REP(i, N){ if(i&1) odd.push_back(C[i]); else even.push_back(C[i]); } sort(ALLR(odd)); sort(ALLR(even)); // dump(C) // dump(odd) // dump(even) REP(i, N/2){ if(odd[i]+even[i]>0){ res += odd[i]+even[i]; } } cout << res << endl; return 0; }
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> using namespace std; typedef long long ll; const int N=100005; int n,a[N],b[N],c[N],d[N]; ll ans; priority_queue<ll> q[2]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); ans+=a[i]; } for(int i=1;i<=n;i++){ scanf("%d",&b[i]); b[i]-=a[i]; if(i&1){ c[i/2+1]=b[i]; }else{ d[i/2]=b[i]; } } sort(c+1,c+n/2+1); sort(d+1,d+n/2+1); for(int i=n/2;c[i]+d[i]>0;i--){ ans+=c[i]+d[i]; } printf("%lld\n",ans); return 0; }
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <iomanip> #include <functional> #include <deque> #include <stack> #include <queue> #include <bitset> #include <map> using namespace std; typedef long long ll; #define rep(i,n) for (ll i = 0; i < (ll)(n); i++) #define REP(i,n) for (ll i = 1; i <= (ll)(n); i++) #define all(v) v.begin(), v.end() #define fcout cout<<fixed<<setprecision(15) #define PI 3.1415926535897932384626433832795028841971693993751058209749445923078160628620899 const ll MOD = 1000000007; const ll INF = 1000000000000000000; const ll NIL = -1; ll N; int main() { cin >> N; rep(i, N) { ll P; cin >> P; ll A = 0; while (P % 2 == 0) { P /= 2; A++; } if (A == 1) cout << "Same" << endl; else if (A == 0) cout << "Odd" << endl; else cout << "Even" << endl; } return 0; }
/* -------------------- | LOSER | | ~NOOBOSS~ | -------------------- */ #include <bits/stdc++.h> using namespace std; #define mxx 1e18 #define mnn -1e18 #define Y() cout<< "YES" <<endl #define N() cout << "NO"<<endl #define endl "\n" #define Ceil(x,y) ((x+y-1)/y) #define sz(s) (int)s.size() #define angle(x) double(x * acos(-1) / 180.0) #define max_3(a,b,c) max(a, max(b,c)) #define min_3(a,b,c) min(a, min(b,c)) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*b)/gcd(a,b) #define loser return 0 #define ll long long #define PI acos(-1) #define mem(a,v) memset(a,v,sizeof(a)) #define SORT(v) sort(v.begin(),v.end()) #define REV(v) reverse(v.begin(),v.end()) #define B begin() #define E end() #define V vector #define F first #define S second #define PSB push_back #define MP make_pair #define flash cout.flush() #define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); constexpr ll MOD = 998244353; constexpr ll mod = 1e9 + 7; int dx[] = {0,0,1,-1}; int dy[] = {1,-1,0,0}; /*-----*/ #define bug1(a) cerr<<a<<endl; #define bug2(a,b) cerr<<a<<" "<<b<<endl; #define bug3(a,b,c) cerr<<a<<" "<<b<<" "<<c<<endl; /*----*/ const ll N=3e6+5; vector<ll> adj[N]; ll power(ll n,ll p){if(p==0) return 1;if(p==1)return n;if(p%2)return power(n,p-1)*n;else{ll x=power(n,p/2);return x*x;}} ll modpow(ll a,ll b,ll m){ll ans=1;while(b){if(b&1)ans=(ans*a)%m;b/=2;a=(a*a)%m;}return ans;} ll nsum(ll num){return (num*(num+1))/2;} void edge (ll u,ll v) {adj[u].PSB(v) ;adj[v].PSB(u);} /*------------------START---------------------*/ /*-----*/ void solve(){ ll n; string s; cin>>n; if(n%2) cout<<"Odd"<<endl; else if(n%4==2) cout<<"Same"<<endl; else cout<<"Even"<<endl; //cout << fixed << setprecision(10); } /*-----*/ int main(){ InTheNameOfGod ll Test=1; cin>>Test; while(Test--){ solve(); } loser; } /////// C O D I N G I S L I F E ///////
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<int> vi; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<ll> vll; typedef vector<pair<int,int> > vpii; typedef vector<vector<int> > vvi; typedef vector<vector<char> > vvc; typedef vector<vector<bool> > vvb; typedef vector<vector<string> > vvs; typedef vector<vector<ll> > vvll; #define Rep(i,n) for(ll i=0;i<ll(n);i++) #define Repd(i,n) for(ll i=n-1;i>=0;i--) #define For(i,a,b) for(ll i=a;i<=ll(b);i++) #define Ford(i,a,b) for(ll i=a;i>=ll(b);i--) #define Fora(i,I) for(const auto& i:I) #define ALL(a) a.begin(),a.end() #define RALL(a) a.rbegin(),a.rend() #define pusb push_back #define emb emplace_back #define pob push_back const int INF30 = 1<<30; const ll INF60 = 1LL<<60; const ll MOD = 1000000007; const ll val = 0; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dx[8] = {1, 0, -1, 0, 1, -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)); } ll myceil(ll a,ll b){return (a+(b-1))/b;} ll myfloor(ll a,ll b){return a/b;} int point(vvc &A, int y, int x){ if(A.at(y).at(x) == '+')return 1; return -1; } int main(void) { int H,W; cin>>H>>W; vvc A(H, vc(W)); Rep(i,H)Rep(j,W)cin>>A.at(i).at(j); vvi dp(H, vi(W)); Ford(i,H-1,0)Ford(j,W-1,0){ if(i == H - 1 && j == W - 1)continue; //cout<<i<<' '<<j<<endl; int tmp = 0; if((i + j)%2 == 0){ tmp = -INF30; if(i + 1 < H)chmax(tmp,dp.at(i + 1).at(j) + point(A,i+1,j)); if(j + 1 < W)chmax(tmp,dp.at(i).at(j + 1) + point(A,i,j+1)); }else{ tmp = INF30; if(i + 1 < H)chmin(tmp,dp.at(i + 1).at(j) - point(A,i+1,j)); if(j + 1 < W)chmin(tmp,dp.at(i).at(j + 1) - point(A,i,j+1)); } dp.at(i).at(j) = tmp; //cout<<'*'<<tmp<<endl; } if(dp.at(0).at(0) > 0)cout<<"Takahashi"<<endl; else if(dp.at(0).at(0) == 0)cout<<"Draw"<<endl; else cout<<"Aoki"<<endl; }
// Code Written by Pinaki Bhattacharjee (pinakipb2) #include<bits/stdc++.h> using namespace std; // typedef typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; // #define #define LOCAL #define endl "\n" #define PI 2*acos(0.0) #define ff first #define ss second #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define mpr make_pair #define all(x) (x).begin(),(x).end() #define ppc __builtin_popcountll void pinakipb2() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);} // constants const int MAX = 1e7 + 10; const int MOD = (int) 1e9 + 7; ll h,w; ll a[2005][2005]; bool vis[2005][2005]; ll dp[2005][2005]; ll func(ll i, ll j) { if(i==h-1 && j==w-1) return 0; if(vis[i][j]) return dp[i][j]; vis[i][j] = true; ll ans = INT_MIN; if(j+1<w) ans = max(ans,a[i][j+1]-func(i,j+1)); if(i+1<h) ans = max(ans,a[i+1][j]-func(i+1,j)); return dp[i][j] = ans; } int main() { pinakipb2(); cin>>h>>w; vector<string> v(h); for(int i=0;i<h;i++) { cin>>v[i]; } for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { a[i][j] = v[i][j]=='+' ? 1 : -1; } } ll ans = func(0,0); if(ans==0) cout<<"Draw"<<endl; else if(ans<0) cout<<"Aoki"<<endl; else cout<<"Takahashi"<<endl; return 0; }
/** * Author: dhruv_gheewala * Problem: A - Hands * Date: 28.11.2020 * Time: 18:31:28 **/ #if __has_include("debug.h") #include "debug.h" #else #include<bits/stdc++.h> using namespace std; #define fundri 108 #define debug(...) 1729 #endif #define endl '\n' #define int int64_t typedef pair<int, int> pii; typedef vector<int> vi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);} template<typename H, typename ...T>void inp(H &head) {cin >> head;} template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);} template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;} template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.ff >> a.ss; return in;} const int inf = 1e15; const bool testcases = false; void init_main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DHRUV_GHEEWALA freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif } void solve(int tc) { int a, b, x, y; cin >> a >> b >> x >> y; if(a == b) { cout << x << endl; return; } if(a > b) { // a, 0 -> b + 1, 0 // b + 1, 0 -> b, 1 cout << (a - b - 1) * min(y, 2 * x) + x << endl; } else { // a, 0 -> b, 0 // b, 0 -> b, 1 cout << (b - a) * min(y, 2 * x) + x << endl; } } int32_t main(int32_t argc, char **argv) { init_main(); int TC = 1; if(testcases) cin >> TC; for(int tc = 1; tc <= TC; ++tc) { solve(tc); fundri; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int , int>; int main(int argc, char const *argv[]) { int a, b , c; cin >> a >> b >> c; a = 7-a; b = 7- b; c = 7-c; cout << a + b + c << endl; return 0; }
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include <stack> #include <string> #include <bitset> #include <set> #include <cmath> #define LL long long #define ull unsigned long long #define mk(t1, t2) make_pair(t1, t2) using namespace std; const LL mo = 1e9 + 7; const int MAXN = 5e5 + 5; int n; char s[MAXN]; LL a[MAXN], b[MAXN]; LL sta[MAXN], ans, top; void push (int &pos) { while (pos <= n && b[pos] == 0) ++pos; } int main() { #ifdef IN freopen ("x.in", "r", stdin); freopen ("x.out", "w", stdout); #endif int i,j,k; scanf ("%d", &n); scanf ("%s", s + 1); for (i=1; i<=n; ++i) a[i] = s[i] - '0'; scanf ("%s", s + 1); for (i=1; i<=n; ++i) b[i] = s[i] - '0'; j = 1, push (j); for (i=1; i<=n; ++i) { if (a[i] == 1) { if (top) ans += i - sta[top--]; else if (j <= i && j <= n) ans += i - j, ++j, push (j); else sta[++top] = i; } } if (j <= n || top) printf ("-1"); else printf ("%lld", ans); return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <fstream> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <math.h> #include <numeric> #include <ostream> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> namespace asl { typedef long long i64; } #include <stdint.h> #include <experimental/optional> #include <utility> #include <tuple> #include <random> #define endl '\n' using namespace std; using namespace asl; void solve() { int n; cin >> n; string a, b; cin >> a >> b; queue<int> q; int last = -1; i64 res = 0; for (int i = 0; i < n; ++i) { if (b[i] == '1') q.push(i); if (a[i] == '1') { if (last != -1) { res += i - last; last = -1; } else if (!q.empty()) { res += i - q.front(); q.pop(); } else { last = i; } } } if (!q.empty() || last != -1) { cout << -1 << endl; } else { cout << res << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; for (int i = 1; i <= t; ++i) { solve(); } return 0; }
#include <bits/stdc++.h> #define arr array #define pb push_back #define fs first #define sc second #define eb emplace_back #define vt vector #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) using namespace std; typedef long long ll; const int maxn=2e5; const int MAXINT=1e9; const ll MAXLL=1e18; void solve(){ int n; cin >> n; n*=2; vt<arr<int, 3>> a(n); rep(i, 0, n) cin >> a[i][0], a[i][1]=i; sort(a.begin(), a.end(), [&](arr<int, 3> x, arr<int, 3> y){ return x[0]<y[0]; }); rep(i, 0, n/2) a[i][2]=1; rep(i, n/2, n) a[i][2]=0; sort(a.begin(), a.end(), [&](arr<int, 3> x, arr<int, 3> y){ return x[1]<y[1]; }); //rep(i, 0, n) cout << a[i][2] << " "; cout << endl; vt<int> st; vt<char> ans(n); rep(i, 0, n){ if (!st.empty()&&a[i][2]!=a[st.back()][2]) ans[st.back()]='(', ans[i]=')', st.pop_back(); else st.pb(i); } rep(i, 0, n) cout << ans[i]; cout << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; while(t--){ solve(); } }
/** * author: BohaoWang ([email protected]) * id: * tag: */ #include <iostream> #include <iomanip> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <string> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <list> #include <algorithm> #include <climits> #include <cassert> #ifdef XDebug #include "../../debug.h" #endif using namespace std; typedef long long ll; typedef pair<int,int> PII; #define INF 1e9 #define MAXN 100 #define all(A) A.begin(), A.end() #ifndef XDebug #define dumpArray(...) 42 #define dumpGraph(...) 42 #endif // CODE HERE ll extgcd(ll a, ll b, ll &x, ll &y) { ll d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } ll modeq(ll a, ll b, ll n) { ll e, i, d, x, y; d = extgcd(a, n, x, y); if (b % d > 0) { return -1; } else { ll mi = -1; e = (x * (b / d)) % n; ll super = ((e + n) % (n/d)) %n; return super; } } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); int n, t; cin >> t; for (int i = 0; i < t; ++i) { int s, k; cin >> n >> s >> k; cout << modeq(k,n-s,n) << 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, n) for (int i = 1; i <= (int) (n); i++) #define all(x) (x).begin(), (x).end() #define SZ(x) ((int) (x).size()) using ll = long long; int main() { int n, k, m; cin >> n >> k >> m; vector <int> a(n - 1); int sum = 0; REP(i, n - 1) { cin >> a[i]; sum += a[i]; } if (m * n - sum <= k) { cout << max(0, m * n - sum) << endl; } else { cout << -1 << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int N; string S, X; cin >> N >> S >> X; vector<vector<bool>> dp(N + 1, vector<bool>(7, false)); dp[N][0] = true; for (int i = N - 1; i >= 0; i--) { int s = (int)S[i] - 0x30; if (X[i] == 'A') { for (int j = 0; j < 7; j++) { int next1 = j * 10 % 7, next2 = (j * 10 + s) % 7; if (dp[i + 1][next1] && dp[i + 1][next2]) dp[i][j] = 1; } } else { for (int j = 0; j < 7; j++) { int next = j * 10 % 7; if (dp[i + 1][next]) dp[i][j] = 1; next = (j * 10 + s) % 7; if (dp[i + 1][next]) dp[i][j] = 1; } } } cout << (dp[0][0] ? "Takahashi" : "Aoki") << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int a[801][801],b[640001],n,k,sum[801][801]; bool check(int x){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(a[i][j]>x) sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+1; else sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]; } for(int i=1;i<=n-k+1;i++) for(int j=1;j<=n-k+1;j++){ if(sum[i-1][j-1]+sum[i+k-1][j+k-1]-sum[i+k-1][j-1]-sum[i-1][j+k-1]<k*k/2+1) return true; } return false; } int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j]; b[(i-1)*n+j]=a[i][j]; } } sort(b+1,b+n*n); int l=0,r=n*n; while(l<=r){ int mid=(l+r)/2; if(check(b[mid])) r=mid-1; else l=mid+1; } cout<<b[r+1]<<endl; return 0; }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; using lpair = pair<ll, ll>; using vll = vector<ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;} #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } void solve(){ ll N,K; cin >> N >> K; vector<vll> A(N, vll(N)); rep(i,0,N){ rep(j,0,N){ cin >> A[i][j]; } } ll lv = -1, rv = INF; while(rv - lv > 1){ ll mid = (lv + rv) / 2; vector<vll> sum(N+1, vll(N+1, 0)); rep(i,0,N){ rep(j,0,N){ if(A[i][j] <= mid) sum[i+1][j+1]++; } } rep(i,0,N+1){ rep(j,0,N){ sum[i][j+1] += sum[i][j]; } } rep(j,0,N+1){ rep(i,0,N){ sum[i+1][j] += sum[i][j]; } } bool ok = false; rep(i,0,N-K+1){ rep(j,0,N-K+1){ if(sum[i+K][j+K] - sum[i+K][j] - sum[i][j+K] + sum[i][j] >= (K*K+1)/2) ok = true; } } if(ok){ rv = mid; }else{ lv = mid; } } ll ans = rv; print(ans); } int main(){ cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include<bits/stdc++.h> using namespace std; #define F first #define S second #define int long long int #define pb push_back #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define mod 1000000007 #define endl '\n' #define rep(i,a,b) for(int i=a;i<b;i++) #define db(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } const int N = 2e5 + 10; vi gr[N]; int ans = 0; int st[N]; int et[N]; int tim = 0; vi d[N]; void dfs(int cur, int depth) { st[cur] = tim ++; d[depth].pb(st[cur]); for (auto x : gr[cur]) { dfs(x, depth + 1); } et[cur] = tim; } void solve() { int n; cin >> n; rep(i, 2, n + 1) { int p; cin >> p; gr[p].pb(i); } dfs(1, 0); int q; cin >> q; while (q--) { int U, D; cin >> U >> D; int l = st[U]; int r = et[U]; int ans = lower_bound(d[D].begin(), d[D].end(), r) - lower_bound(d[D].begin(), d[D].end(), l); cout << ans << endl; } } int32_t main() { c_p_c(); int t = 1; //cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; set<int> st; for(int i = 0 ; i < N ; i++) { int n; cin >> n; st.insert(n); } while(1) { if(st.size() == 1)break; int may = *(--st.end()); int men = *(st.begin()); st.erase(may); st.insert(may-men); } cout << *st.begin() << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) int nota(string S){ rep(i,S.size()){ if(S.at(i) != 'a'){ return i; } if(i== (int) (S.size()-1) && S.at(i) == 'a'){ return -10; } } } int main(){ int t; cin >> t; rep(i,t){ string s; cin >> s; if(s > "atcoder"){ cout << 0 <<endl;; } else if(nota(s) >0){ if(s.at(nota(s)) > 't'){ cout << nota(s) -1 <<endl; } else{ cout << nota(s) <<endl; } } else{ cout << -1 << endl; } } }
#include <bits/stdc++.h> using namespace std; #define rep(i, c) for(int i = 0; i < (int)c; i++) int main(){ int n, ans = 0; cin >> n; int a[n]; rep(i, n) cin >> a[i]; rep(i, n){ if(a[i] > 10){ ans += a[i] - 10; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi firstk #define se second #define SZ(x) ((int)(x).size()) #define mem(a,b) memset(a,b,sizeof (a)) #define ios stkd::ios::sync_with_stkdio(false); stkd::cin.tie(0); stkd::cout.tie(0); // ios can not be used with scanf,printf. IF ios MUstk cin,cout. typedef long long ll; typedef vector<int> VI; typedef pair<int,int> PII; const ll mod=1000000; const int INF=0x3f3f3f3f; ll ksm(ll a,ll b){ll res=1;a%=mod;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;} const int N=1510; int n; int fa[N]; double x[N],y[N]; int find(int x){ if(fa[x]!=x){ fa[x]=find(fa[x]); } return fa[x]; } double dis(double x1,double y1,double x2,double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } bool check(double r){ for(int i=0;i<=n+1;i++)fa[i]=i; for(int i=1;i<=n;i++){ if(100-y[i]<2*r)fa[find(i)]=find(0); if(abs(-100-y[i])<2*r)fa[find(i)]=find(n+1); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ int a=find(i),b=find(j); if(dis(x[i],y[i],x[j],y[j])<2*r){ fa[b]=a; } } } return find(0)==find(n+1); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lf%lf",&x[i],&y[i]); } double l=1,r=100; while(r-l>1e-5){ double mid=(l+r)/2; if(check(mid))r=mid; else l=mid; } printf("%.4lf\n",r); return 0; }
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; vector<int> sz; UnionFind(int n=0){ if(n>0) initialize(n); } void initialize(int n){ par.resize(n); sz.assign(n, 1); for(int i=0; i<n; i++){ par[i] = i; } } int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } } bool unite(int x, int y){ x = find(x), y = find(y); if(x == y) return false; if(sz[x] > sz[y]) swap(x, y); par[x] = y; sz[y] += sz[x]; return true; } bool same(int x, int y){ return find(x) == find(y); } int size(int x){ return sz[find(x)]; } }; int main(){ int N; cin >> N; vector<double> X(N), Y(N); for(int i=0; i<N; i++) cin >> X[i] >> Y[i]; using T = tuple<double, int, int>; vector<T> es; for(int i=0; i<N; i++) for(int j=0; j<i; j++) es.emplace_back(hypot(X[i]-X[j], Y[i]-Y[j]), i, j); for(int i=0; i<N; i++){ es.emplace_back(100-Y[i], i, N); es.emplace_back(Y[i]+100, i, N+1); } sort(es.begin(), es.end()); UnionFind uf(N+2); for(auto& [d, a, b] : es){ uf.unite(a, b); if(uf.same(N, N+1)){ cout << fixed << setprecision(10) << d/2 << endl; return 0; } } return 0; }
#include "bits/stdc++.h" using namespace std; int main() { string S; cin >> S; map<int, int> s; string ans = "No\n"; for (int i = 0; i < S.size(); ++i) ++s[S.at(i) - '0']; if (S.size() == 1) { if (S == "0" || S == "8") ans = "Yes\n"; } else if (S.size() == 2) { for (int i = 0; i < 100; i += 8) { --s[i / 10]; --s[i % 10]; if (s[i / 10] >= 0 && s[i % 10] >= 0) { ans = "Yes\n"; break; } ++s[i / 10]; ++s[i % 10]; } } else { for (int i = 0; i < 1000; i += 8) { --s[i / 100]; --s[(i % 100 - i % 10) / 10]; --s[i % 10]; if (s[i / 100] >= 0 && s[(i % 100 - i % 10) / 10] >= 0 && s[i % 10] >= 0) { ans = "Yes\n"; break; } ++s[i / 100]; ++s[(i % 100 - i % 10) / 10]; ++s[i % 10]; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define ff first #define ss second #define rep(i, n) for(int i = 0; i < n; ++i) #define repR(i, n) for (int i = n-1; i >= 0; --i) #define repA(i, a, n) for (int i = a; i <= n; ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) (int)(x).size() typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vs vector<string> #define vd vector<double> #define vb vector<bool> #define vpii vector<pii> #define vpll vector<pll> #define vpdd vector<pdd> #define mod 1000000007 #define ps(x,y) fixed << setprecision(y) << x #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); bool Solve(string s) { if (s.length() == 1) { if (s == "8") return true; return false; } else if (s.length() == 2) { if (stoi(s) % 8 == 0) { return true; } swap(s[0], s[1]); if (stoi(s) % 8 == 0) { return true; } return false; } vi freq(10, 0); for (auto c : s) { freq[c - '0']++; } if (freq[0] >= 3) { // 000 (in last 3 digit) is always divisible by 8 return true; } for (int i = 104; i < 1000; i += 8) { string temp = to_string(i); vi freq2(10, 0); for (auto c : temp) { freq2[c - '0']++; } bool flag = false; for (int j = 0; j < 10; j++) { if (freq[j] < freq2[j]) { flag = true; } } if (!flag) { return true; } } return false; } int main() { fastio #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s; cin >> s; if (Solve(s)) { cout << "Yes" << '\n'; } else { cout << "No" << '\n'; } return 0; }
#define NDEBUG #include <bits/stdc++.h> using namespace std; void dbg() { cerr << endl; } template <typename U, typename... Ts> void dbg(U foo, Ts... bar) { cerr << ' ' << to_string(foo); dbg(bar...); } #ifndef NDEBUG #define debug(...) cerr << '[' << #__VA_ARGS__ << "]: ", dbg(__VA_ARGS__) #else #define debug(...) ((void)0) #endif int greatestDigit(const string &x) { int res = 0; for (const auto &z : x) { res = max(res, z - '0'); } return res; } long long baseN(const string &x, long long n) { long long res = 0; for (const auto &z : x) { if (res > (long double)(LLONG_MAX)/n) { return LLONG_MAX; } res = res * n; if (res > LLONG_MAX - (z - '0')) { return LLONG_MAX; } res += z - '0'; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string x; long long m; cin >> x >> m; if (x.length() == 1) { cout << (x[0] - '0' <= m) << '\n'; return 0; } int d = greatestDigit(x); long long l = d + 1, r = m, mid = r / 2 + l / 2, res = -1; debug(l, r, mid); while (l <= r) { debug(l, r, mid, baseN(x, mid)); if (baseN(x, mid) <= m) { res = mid; l = mid + 1; } else { r = mid - 1; } mid = r / 2 + l / 2; if (r % 2 == 1 && l % 2 == 1) { mid++; } } if (res == -1) { cout << 0 << '\n'; return 0; } debug(res, d); cout << res - d << '\n'; return 0; }
#include <bits/stdc++.h> #include <math.h> using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vpl = vector<pair<ll, ll>>; using pll = pair<ll, ll>; #define rep(i, k, n) for(ll i = k; i < n; i++) #define pb push_back #define mp make_pair ll change(string x, ll k, ll m){ ll rec = 0; rep(i, 0, x.size()){ if((x[i] - '0')*pow(k, x.size()-1-i) > m) return -1; rec += (x[i] - '0')*pow(k, x.size()-1-i); if(rec > m) return -1; } return rec; } int main(){ string x; cin >> x; ll m; cin >> m; if(x.size() == 1){ if(x[0] - '0' <= m) cout << 1 << endl; else cout << 0 << endl; } else{ ll d = 0; rep(i, 0, x.size()){ if(x[i] - '0' > d) d = x[i] - '0'; } ll left = 0; ll right = m+1; while(right - left > 1){ ll mid = (right + left) / 2; ll p = change(x, mid, m); if(p == -1 || p > m) right = mid; else left = mid; } cout << max(left - d, 0LL) << endl; } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,b) for(int i=a;i<b;i++) #define FOR(i,a,b) for(int i=a;i<b;i++) #define FORe(i,a,b) for(int i=a;i<=b;i++) #define FORr(i,a,b) for(int i=a;i>b;i--) #define FORre(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " void solve(){ ll n,x; cin >> n >> x; ll ans = x; vector<ll> a(n); rep(i,0,n) cin >> a[i]; for(int k = 1; k <= n; k++){ vector<vector<int> >dp(k+1,vector<int>(k,-1)); dp[0][0] = 0; for(ll v : a){ for(int c = k - 1; c >= 0; c--){ for(int r = 0; r < k; r++){ if(dp[c][r] == -1) continue; int next = dp[c][r] + v; dp[c+1][next%k] = max(dp[c+1][next%k],next); } } } int start = dp[k][x%k]; if(start == -1) continue; ans = min(ans,(x-start)/k); } cout << ans << endl; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while(t--){ solve(); } return 0; }
#include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<algorithm> using namespace std; typedef int int_; #define int long long #define maxn 400010 const int inf=1e9+7; queue<int> q; inline int read(){ int ret=0,ff=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') ff=-ff; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } return ret*ff; } struct Edge{ int u,v,w,next; }E[maxn<<1]; int ecnt=0,head[maxn]; int in[maxn]; void addedge(int u,int v){ ++in[v]; E[++ecnt].u=u; E[ecnt].v=v; E[ecnt].next=head[u]; head[u]=ecnt; } void Addedge(int u,int v){ addedge(u,v); addedge(v,u); } int gcd(int a,int b){ return b?gcd(b,a%b):a; } int ksm(int a,int b,int p){ int ret=1; while(b){ if(b&1) ret=ret*a%p; a=a*a%p; b>>=1; } return ret%p; } bool cmp(int x,int y){ return x>y; } int a[10010]; int G2(int x){ int cnt=0; while(x){ a[++cnt]=x%10; x/=10; } sort(a+1,a+1+cnt); int ret=0; for(int i=1;i<=cnt;++i) ret=ret*10+a[i]; return ret; } int G1(int x){ int cnt=0; while(x){ a[++cnt]=x%10; x/=10; } sort(a+1,a+1+cnt,cmp); int ret=0; for(int i=1;i<=cnt;++i) ret=ret*10+a[i]; return ret; } int F(int x){ return G1(x)-G2(x); } char X[10010]; bool ck(int Mid,int len,int m){ int sum=0,tmp=1; for(int j = len; j;--j){ sum+=tmp*(X[j]-'0'); tmp*=Mid; } return sum>m; } int dp[2][110][110][110]; int_ main(){ int n=read(),x=read(); for(int i=1;i<=n;++i) a[i]=read(); memset(dp, -1, sizeof dp); for(int i=1;i<=n;i++) dp[0][i][0][0] = 0; for(int i=0;i<n;i++){ int qwq=i&1; for(int j =1;j<=n;j++){ for(int qaq =0;qaq<=i;qaq++){ for(int res=0;res<j;res++){ if(dp[qwq][j][qaq][res]==-1) continue; dp[qwq^1][j][qaq+1][(res+a[i+1])%j]=max(dp[qwq^1][j][qaq+1][(res+a[i+1])%j],dp[qwq][j][qaq][res]+a[i+1]); dp[qwq^1][j][qaq][res]=max(dp[qwq^1][j][qaq][res], dp[qwq][j][qaq][res]); } } } } int qwq=n&1,ans=x; for(int qaq=1;qaq<=n;qaq++){ if(dp[qwq][qaq][qaq][x%qaq]>0&&x>=dp[qwq][qaq][qaq][x%qaq])ans=min(ans,(x-dp[qwq][qaq][qaq][x%qaq])/qaq); } printf("%lld",ans); return 0; } /* 1 6 3 */ /* 111111111111111111111111111111111111111111111111111111111111 1000000000000000000 100000000000000000000000000000000000000000000000000000000000 1000000000000000000 2 1000000000000000000 */
#include<bits/stdc++.h> using namespace std; using ll = long long; using ii = pair <int, int>; const int N = 1e3 + 7; int main () { ios::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL freopen("in.txt", "r", stdin); #endif int n, m; cin >> n >> m; vector <bool> a(N), b(N); for (int i = 0; i < n; ++i) { int x; cin >> x; a[x] = 1; } for (int i = 0; i < m; ++i) { int x; cin >> x; b[x] = 1; } for (int i = 1; i < N; ++i) { if (a[i] == 0 and b[i]) { cout << i; } else if (b[i] == 0 and a[i]) { cout << i; } cout << " "; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; const int INF = (int)1e9; const ll INFL = (ll)1e18; const int MOD = 1e9 + 7; const double EPS = 1e-10; int dx[]={0, 0, -1, 1}; int dy[]={1, -1, 0, 0}; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main() { int n, m; cin >> n >> m; map<int, int> mp; vector<int> ans; rep(i,n) { int a; cin >> a; mp[a]++; } rep(i,m) { int b; cin >> b; mp[b]++; } for (auto p : mp) { if (p.second == 1) ans.push_back(p.first); } sort(ans.begin(), ans.end()); for (auto p : ans) cout << p << " "; cout << endl; return 0; }
#include <iostream> using namespace std; struct point { int x, y; }p[105]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> p[i].x >> p[i].y; } int max = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int cnt = 2; point vec1; vec1.x = p[j].x - p[i].x; vec1.y = p[j].y - p[i].y; for (int h = 0; h < n; h++) { if (h == i || h == j) continue; point vec2; vec2.x = p[h].x - p[i].x; vec2.y = p[h].y - p[i].y; if (vec1.x * vec2.y == vec1.y * vec2.x) { cnt++; } } if (cnt > max) max = cnt; } } if (max >= 3) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
#include <algorithm> // #include <atcoder/all> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) constexpr double PI = 3.141592653589793; constexpr int INF = 1e9 + 10; constexpr ll INFL = 1e18 + 10; 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> void dump(const std::vector<T> &ar) { for (T e : ar) { std::cout << e << "\n"; } } template <typename T> void dump(const std::set<T> &s) { for (T e : s) { std::cout << e << " "; } std::cout << "\n"; } using namespace std; // using namespace atcoder; // cout << setprecision(20) << ans << "\n"; int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; cin >> n; vector<int> x(n), y(n); REP(i, n) { cin >> x[i] >> y[i]; } REP(i, n) { REP(j, n) { if (i == j) { continue; } REP(k, n) { if (i == j || j == k || k == i) { continue; } int dx1 = x[i] - x[j]; int dx2 = x[i] - x[k]; int dy1 = y[i] - y[j]; int dy2 = y[i] - y[k]; int dx3 = x[j] - x[k]; int dy3 = y[j] - y[k]; int dis1 = dx1 * dx1 + dy1 * dy1; int dis2 = dx2 * dx2 + dy2 * dy2; int dis3 = dx3 * dx3 + dy3 * dy3; // if (dis1 + dis2 == dis3 || dis1 + dis3 == dis2 || dis2 + dis3 == // dis1) { // if (dis1 != 0 && &dis2 != 0 && dis3 != 0) { if (dx1 * dy2 == dx2 * dy1) { cout << "Yes" << "\n"; return 0; } // } // } } } } cout << "No" << "\n"; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <numeric> #include <utility> #include <tuple> #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << '\n'; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on template<ll MOD = 1000000007> class ModInt { ll n; ModInt constexpr inverse() const { return ModInt::pow(*this, MOD - 2); } public: ModInt() : n(0) {} ModInt(ll _n) : n(((_n % MOD) + MOD) % MOD) {} ModInt operator+=(const ModInt &m) { n += m.n; if (n >= MOD) n -= MOD; return *this; } ModInt operator-=(const ModInt &m) { n -= m.n; if (n < 0) n += MOD; return *this; } ModInt operator*=(const ModInt &m) { n *= m.n; if (n >= MOD) n %= MOD; return *this; } ModInt operator/=(const ModInt &m) { (*this) *= m.inverse(); return *this; } friend ModInt operator+(ModInt t, const ModInt &m) { return t += m; } friend ModInt operator-(ModInt t, const ModInt &m) { return t -= m; } friend ModInt operator*(ModInt t, const ModInt &m) { return t *= m; } friend ModInt operator/(ModInt t, const ModInt &m) { return t /= m; } ModInt operator=(const ll l) { n = l % MOD; if (n < 0) n += MOD; return *this; } friend ostream &operator<<(ostream &out, const ModInt &m) { out << m.n; return out; } friend istream &operator>>(istream &in, ModInt &m) { ll l; in >> l; m = l; return in; } static constexpr ModInt pow(const ModInt x, ll p) { ModInt<MOD> ans = 1; for (ModInt<MOD> m = x; p > 0; p /= 2, m *= m) { if (p % 2) ans *= m; } return ans; } }; using mint = ModInt<>; mint operator"" _m(unsigned long long m) { return mint(m); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin, n, m; int sum = 0; rep(i, 0, n) { int a; cin, a; sum += a; } if (m < sum) { print(0); return 0; } // C(m + n, sum + n) = (m + n)P(sum + n) / (sum + n)!; mint a = 1, b = 1; rep(i, 0, sum + n) { a *= (m + n - i); b *= (i + 1); } print(a / b); return 0; }
#include <bits/stdc++.h> #define ln '\n' #define all(dat) dat.begin(), dat.end() #define loop(i, to) for (int i = 0; i < to; ++i) #define cont(i, to) for (int i = 1; i <= to; ++i) #define circ(i, fm, to) for (int i = fm; i <= to; ++i) #define foreach(i, dat) for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i) typedef long long num; using namespace std; const int nsz = 2e5; int n, a[nsz + 5]; num pre[2][nsz + 5]; int main() { scanf("%d", &n); cont (i, n) scanf("%d", &a[i]); int mx = 0; cont (i, n) { mx = max(mx, a[i]); pre[0][i] = pre[0][i - 1] + a[i]; pre[1][i] = pre[1][i - 1] + pre[0][i]; printf("%lld\n", pre[1][i] + (num) mx * i); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define PI 3.14159265359 #define mod 1000000007 #define ALL(a) (a).begin(),(a).end() int main(){ ll a,b; cin >> a >> b; int ans = 1; for(int i = 1; i <= 200000; i++){ if(a % i != 0){ if(a <= i * (a / i + 1) && i * (a / i + 1) <= b){ if(a <= i * (a / i + 2) && i * (a / i + 2) <= b){ ans = max(ans,i); } } } else{ if(a <= i * (a / i + 1) && i * (a / i + 1) <= b){ ans = max(ans,i); } } } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main() { int a,b; cin >> a >> b; //cout<<__gcd(a,b)<<endl; int dis = b-a; for(int i=dis;i>=1;i--) { int n = b/i; // cout<<i<<" "<<n<<endl; if((n+1)*i <=b && (n+1)*i>=a) { cout<<i<<endl; return 0; } else if((n-1)*i <=b && (n-1)*i >= a) { cout<<i<<endl; return 0; } } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define ls rt<<1 #define rs rt<<1|1 const int N=2e5+5; const ll REQ=1e13; const int INF=0x3f3f3f3f; const int mod=1e9+7; const double eps=1e-9; const double pi=acos(-1); ll quick(ll a,int n){ ll re=1; while(n){ if(n&1) re=(re*a)%mod; a=(a*a)%mod; n>>=1; } return re; } ll inv(ll a){return quick(a,mod-2);} ll C(int n,int m){ //n里选m m=min(m,n-m); ll r1=1,r2=1; for(int i=n;i>n-m;--i) r1=(r1*i)%mod; for(int i=2;i<=m;++i) r2=(r2*i)%mod; return r1*inv(r2)%mod; } int main(){ int n,m;scanf("%d%d",&n,&m); int sum=0; for(int i=1;i<=n;++i){ int a;scanf("%d",&a); sum+=a; } if(m<sum) puts("0"); else printf("%lld\n",C(n+m,n+sum)); return 0; }
#include <bits/stdc++.h> #define rei register int #define int long long using namespace std; const int N=2e3+100,mod=1e9+7; int n,m,a[N]; inline int read(){ int sum=0,sign=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') sign=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ sum=(sum<<1)+(sum<<3)+(ch^48); ch=getchar(); } return sum*sign; } inline int Qpower(long long base, long long power) { int result = 1; base%=mod; while (power > 0) { if (power & 1) { result = result * base%mod; } power >>= 1; base = (base * base)%mod; } return result; } signed main(){ scanf("%lld%lld",&n,&m); int sum=0; for(rei i=1;i<=n;++i) sum+=read(); if(m<sum){ printf("0\n"); return 0; } //ans=(∑Ai+N) // (M+N ) m+=n,sum+=n; int ans=1; for(rei i=1;i<=sum;++i){ ans=ans*Qpower(i,mod-2)%mod *(m-i+1) %mod; } printf("%lld\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=5e5+5; const int mod=1000000000+7; const int inf=0x3f3f3f3f; int h,w; char g[5050][5050]; ll x[5050][5050],y[5050][5050],z[5050][5050]; ll dp[5050][5050]; int main(){ scanf("%d%d",&h,&w); for(int i=1;i<=h;i++) scanf("%s",g[i]+1); dp[1][1]=1; for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ if(i==1&&j==1) continue; if(g[i][j]=='#') continue; x[i][j]=(x[i][j-1]+dp[i][j-1])%mod; y[i][j]=(y[i-1][j]+dp[i-1][j])%mod; z[i][j]=(z[i-1][j-1]+dp[i-1][j-1])%mod; dp[i][j]=(x[i][j]+y[i][j]+mod)%mod; dp[i][j]=(dp[i][j]+z[i][j]+mod)%mod; } } printf("%lld",dp[h][w]); }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) 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 H,W; string S[10]; void solve() { int i,j,k,l,r,x,y; string s; cin>>H>>W; FOR(y,H) cin>>S[y]; int ret=0; FOR(y,H-1) FOR(x,W-1) { if(S[y][x]=='#'&&S[y+1][x]=='.'&&S[y][x+1]=='.') ret++; if(S[y][x+1]=='#'&&S[y+1][x+1]=='.'&&S[y][x]=='.') ret++; if(S[y+1][x]=='#'&&S[y][x]=='.'&&S[y+1][x+1]=='.') ret++; if(S[y+1][x+1]=='#'&&S[y][x+1]=='.'&&S[y+1][x]=='.') ret++; if((S[y][x]=='#')+(S[y][x+1]=='#')+(S[y+1][x]=='#')+(S[y+1][x+1]=='#')==3) ret++; } cout<<ret<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
#include<iostream> #include<cstdio> #include<stack> #include<queue> #include<cstring> #include<algorithm> #include<cmath> #include<set> #include<iomanip> using namespace std; #define N 300009 #define LL long long #define INF 0x3f3f3f3f #define mymin(x,y) (x>=y?y:x) #define mymax(x,y) (x>=y?x:y) int tong[N]; int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int n, k; cin >> n >> k; for(int i = 1; i <= n; i++){ int a; cin >> a; tong[a]++; } int num = INF, res; LL ans = 0; for(int i = 0; i < n; i++){ num = min(num, tong[i]); res = max(k - num, 0); //cout << i << " " << num << " " << res << "\n"; k -= res; ans += (i * res); if(k == 0) break; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ff first #define brn "\n" #define ss second #define mk make_pair #define int long long #define pb push_back #define ps(x,y) fixed<<setprecision(y)<<x #define w(x) int x; cin>>x; while(x--) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define ROF(i,a,b) for(int i=(a);i>=(b);i--) #define so(a,n) sort(a,a+n) #define rso(a,n) sort(a,a+n),reverse(a,a+n) #define all(v) (v).begin(),(v).end() #define ps(x,y) fixed<<setprecision(y)<<x #define sz size(); #define p_sieve 10000007 const int maxn = 1e3 + 5; const int modulo = 1000000007; #define mod 1000000009; const double pi = 3.14159; #define INF 1000001000 #define imx 9999999999999999 #define imi -9999999999999999 //check n==1 and base cases// //********************************---------always check array boundness-----------**************** // factorial ncr // int printNcR(int n, int r) // { // if(n<r)return 0; // long long p = 1, k = 1; // if (n - r < r) // r = n - r; // if (r != 0) { // while (r) { // p *= n; // k *= r; // long long m = __gcd(p, k); // p /= m; // k /= m; // n--; // r--; // } // } // else p = 1; // return p; // } // int power(int x, unsigned int y) // { // int res = 1; // x = x % modulo; // if (x == 0) return 0; // while (y > 0) // { // if (y & 1) res = (res*x); // y = y>>1; // x = (x*x); // } // return res; // } const int N=300005; vector<int>v[N]; void solve() { int n,m;cin>>n>>m; int a[n]; FOR(i,0,n)cin>>a[i]; int maxi=1; sort(a,a+n); int cn=0,i=0; while(a[i]==0 and i<n)v[maxi].pb(0),i++,maxi++; cn++; FOR(j,i,n) { int k=j; int tt=1; while(a[k]==cn and k<n) { if(v[tt].size()>0)if(v[tt].back()==cn-1 and tt<maxi)v[tt].pb(cn),tt++; k++; } j=k; j--; cn++; } int ans=0; for(int i=1;i<=m;i++) { if(v[i].size()>0)ans+=(v[i].back()+1); } cout<<ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //prime_sieve(); //w(t) solve(); }
#include <bits/stdc++.h> using namespace std; #define co(n) cout<<n<<endl; const double eps=1e-10,pi=3.1415926535898; const int mod=998244353,maxn=5e3+10; int T,n,m,q,k; long long x,y,a[maxn][maxn]; long long qpow(long long x, int k) { long long cnt=1; while (k) { if (k&1) cnt = cnt*x%mod; x = x*x%mod; k >>= 1; } return cnt; } int main(int argc, char const *argv[]) { for (int i=0;i<maxn;++i) { a[i][0] = 1; } for (int i=0;i<maxn;++i) { for (int j=1;j<maxn;++j) { a[i][j] = a[i][j-1]*i%mod; } } scanf("%d %d", &n, &m); long long ans=qpow(m, n)*n%mod; for (int i=1;i<=m;++i) { for (int j=0;j<=n-2;++j) { ans += mod-a[m][n-j-2]*a[m-i][j]%mod*(n-j-1)%mod; } } printf("%lld\n", ans%mod); return 0; }
#include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define forn(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i) #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define rep(i, l, r) for (int i = (l); i <= (r); i++) #define per(i, r, l) for (int i = (r); i >= (l); i--) #define ms(x, y) memset(x, y, sizeof(x)) #define SZ(x) int(x.size()) #define fk cerr<<"fk"<<endl #define db(x) cerr<<(#x)<<'='<<(x)<<endl using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef long long i64; typedef vector<i64> vi64; typedef vector<vi64> vvi64; typedef pair<i64, i64> pi64; typedef double ld; template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } //1.integer overflow (1e5 * 1e5) (2e9 + 2e9) //2.runtime error //3.boundary condition string origin,t; int n; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout<<fixed; #ifdef LOCAL_DEFINE freopen("input.txt", "r", stdin); #endif origin=""; forn(i, 2*(int)1e5+100){ origin+="110"; } cin>>n; cin>>t; if(t=="1"){ cout<<2*(i64)1e10<<'\n'; return 0; } i64 minn=INT_MAX; for(int i=0; i<3; ++i){ i64 now=i-1; bool ok=true; forn(j,n){ ++now; if(origin[now]!=t[j]){ ok=false; break; } } if(ok) uin(minn,(now+3)/3); } if(minn==INT_MAX) cout<<0<<'\n'; else cout<<(i64)1e10-minn+1<<'\n'; #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long void read (int &x) { char ch = getchar(); x = 0; int f = 0; while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); if (f) x = -x; } const int N = 1e5 + 5, mod = 1e9 + 7; int n, a[N], f[N]; signed main() { read (n); for (int i = 1; i <= n; ++i) read (a[i]); f[1] = 2, f[0] = 1; for (int i = 2; i <= n; ++i) f[i] = (f[i - 1] + f[i - 2]) % mod; int res = a[1] * f[n - 1]; for (int i = 2; i <= n; ++i) { (res += a[i] * f[i - 2] % mod * f[n - i]) %= mod; int A = i == 2 ? 1 : f[i - 3], B = i == n ? 1 : f[n - i - 1]; (res -= a[i] * A % mod * B) %= mod; } res = (res % mod + mod) % mod; printf ("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod = 998244353; ll mem[3005][3005]; ll dp(int n, int k){ if (k > n) return 0; if (n == 0) return (k == 0); if (k <= 0) return 0; //printf("%d %d\n",n,k); if (mem[n][k] != -1) return mem[n][k]; int ans1 = 0; if (k >= 1) ans1 = dp(n-1,k-1); int ans2 = dp(n,2*k); return mem[n][k] = (ans1+ans2)%mod; } int main(){ int n,k; scanf("%d%d",&n,&k); memset(mem,-1,sizeof(mem)); printf("%lld\n",dp(n,k)); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 100 + 5; const ll mod = 1e9 + 7; int n, m, k, b[maxn][maxn]; ll powMod(ll a, ll k) { ll ret = 1; while(k) { if(k & 1) ret = ret * a % mod; a = a * a % mod; k >>= 1;} return ret;} ll getinv(ll x) { return powMod(x, mod - 2);} struct Mat { int r, c; ll a[maxn][maxn]; Mat (const int & _r, const int & _c) : r(_r), c(_c) { memset(a, 0, sizeof a);} void setone() { assert(r == c); memset(a, 0, sizeof a); for(int i = 0; i < r; ++i) a[i][i] = 1; } Mat operator * (const Mat & rhs) const { Mat ret(r, rhs.c); for(int i = 0; i < r; ++i) for(int j = 0; j < c; ++j) { ll tmp = a[i][j]; for(int k = 0; k < rhs.c; ++k) ret.a[i][k] = (ret.a[i][k] + tmp * rhs.a[j][k] % mod) % mod; } return ret; } }; Mat powMod(Mat a, ll k) { assert(a.r == a.c); Mat ret(a.r, a.r); ret.setone(); while(k) { if(k & 1) ret = ret * a; a = a * a; k >>= 1; } return ret; } int main() { scanf("%d%d%d", &n, &m, &k); const ll inv2m = getinv(2 * m); Mat v(n, 1); for(int i = 0; i < n; ++i) scanf("%lld", &v.a[i][0]); for(int i = 0; i < m; ++i) { int u, v; scanf("%d%d", &u, &v); --u, --v; ++b[u][v]; ++b[v][u]; } Mat A(n, n); A.setone(); for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) { if(i == j) continue; A.a[i][j] = b[i][j] * inv2m % mod; A.a[i][i] = (A.a[i][i] + mod - A.a[i][j]) % mod; } v = powMod(A, k) * v; for(int i = 0; i < n; ++i) printf("%lld\n", v.a[i][0]); return 0; }
#include<cstdio> #include<vector> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> #include<set> using namespace std; #define NDEBUG #include<cassert> namespace Elaina{ #define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i) #define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i) #define fi first #define se second #define mp(a, b) make_pair(a, b) #define Endl putchar('\n') #define mmset(a, b) memset(a, b, sizeof a) #define siz(a) ((int)(a).size()) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template<class T>inline T fab(T x){ return x<0? -x: x; } template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); } template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); } template<class T>inline T readin(T x){ x=0; int f=0; char c; while((c=getchar())<'0' || '9'<c) if(c=='-') f=1; for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48)); return f? -x: x; } template<class T>inline void writc(T x, char s='\n'){ static int fwri_sta[1005], fwri_ed=0; if(x<0) putchar('-'), x=-x; do fwri_sta[++fwri_ed]=x%10, x/=10; while(x); while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed); putchar(s); } } using namespace Elaina; const int maxm=2e5; pii pawn[maxm+5]; int n, m; inline void input(){ n=readin(1), m=readin(1); rep(i, 1, m) pawn[i].fi=readin(1), pawn[i].se=readin(1); sort(pawn+1, pawn+m+1); } set<int>avai; vector<int>rem, ins; inline int saya(int x){ auto it=avai.find(x); if(it!=avai.end() && (*it)==x) return 1; return 0; } signed main() { input(); avai.insert(n); for(int l=1, r; l<=m; l=r+1){ for(r=l; r<=m && pawn[r+1].fi==pawn[l].fi; ++r); rep(i, l, r) rem.push_back(pawn[i].se); rep(i, l, r){ if(saya(pawn[i].se-1) || saya(pawn[i].se+1)) ins.push_back(pawn[i].se); } while(!rem.empty()) avai.erase(rem.back()), rem.pop_back(); while(!ins.empty()) avai.insert(ins.back()), ins.pop_back(); } writc(siz(avai)); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) #define all(v) v.begin(),v.end() using namespace std; using ll = long long; int H, W; int pt(char c){ if(c == '+') return 1; else return -1; } bool inarea(int y, int x){ if(0 <= y && y < H && 0 <= x && x < W) return true; else return false; } int toint(int y, int x){ return y * W + x; } pair<int, int> topair(int N){ int x = N % W; int y = (N - x) / W; return make_pair(y, x); } int main(){ cin >> H >> W; vector<vector<char>> A(H, vector<char> (W)); rep(i, H) rep(j, W){ cin >> A[i][j]; } int dp[4000000]; rep(i, H * W - 1){ int y = topair(i).first; int x = topair(i).second; if((y + x) % 2 == 0) dp[i] = -1e9; else dp[i] = 1e9; } dp[H * W - 1] = 0; dp[H*W-1] = 0; int dy[2] = {1, 0}; int dx[2] = {0, 1}; queue<int> todo; todo.push(H * W - 1); vector<bool> seen(H * W, false); seen[H * W - 1] = true; while(!todo.empty()){ int v = todo.front(); todo.pop(); int y = topair(v).first; int x = topair(v).second; rep(i, 2){ int ny = y - dy[i]; int nx = x - dx[i]; if(!inarea(ny, nx)) continue; if(seen[toint(ny, nx)]) continue; seen[toint(ny, nx)] = true; todo.push(toint(ny, nx)); rep(j, 2){ int nny = ny + dy[j]; int nnx = nx + dx[j]; if(!inarea(nny, nnx)) continue; if((ny + nx) % 2 == 1){ dp[toint(ny, nx)] = min(-pt(A[nny][nnx]) + dp[toint(nny, nnx)], dp[toint(ny, nx)]); } else{ dp[toint(ny, nx)] = max(pt(A[nny][nnx]) + dp[toint(nny, nnx)], dp[toint(ny, nx)]); } } } } if(dp[0] > 0) cout << "Takahashi" << endl; else if(dp[0] < 0) cout << "Aoki" << endl; else cout << "Draw" << endl; }
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <iomanip> #include <functional> #include <bitset> #include <limits> #include <cstdio> #include <cmath> #include <cassert> #include <random> #ifdef DEBUG #include "library/Utility/debug.cpp" #else #define debug(...) #endif #define rep(i,n) for(int i=0;i<(n);++i) #define EL '\n' #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() using lnt = long long; struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO; template<typename T> using V = std::vector<T>; template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; } /*-*/ lnt MOD = 998244353; struct mint { lnt v; mint():v(0){} mint(lnt v):v((v+MOD)%MOD){} mint operator-()const{ return mint(0) - *this; } mint& operator+=(const mint& a){ if((v+=a.v)>=MOD) v-=MOD; return *this; } mint& operator-=(const mint& a){ if((v+=MOD-a.v)>=MOD) v-=MOD; return *this; } mint& operator*=(const mint& a){ (v*=a.v)%=MOD; return *this; } mint& operator/=(const mint& a){ (*this) *= a.inv(); return *this; } mint operator+(const mint& a)const{ return mint(*this) += a; } mint operator-(const mint& a)const{ return mint(*this) -= a; } mint operator*(const mint& a)const{ return mint(*this) *= a; } mint operator/(const mint& a)const{ return mint(*this) /= a; } bool operator<(const mint& a)const{ return v < a.v; } bool operator==(const mint& a)const{ return v == a.v; } mint pow(lnt k)const{ mint r(1),t(v); while(k){ if(k&1) r*=t; t*=t; k>>=1; } return r; } mint inv()const{ return pow(MOD-2); } static mint comb(lnt n, lnt k) { if(n-k<k) k=n-k; mint num(1), dom(1); for(int i=0;i<k;i++) { num*=n-i; dom*=i+1; } return num/dom; } static std::vector<mint> construct_comb(int n) { std::vector<mint> c(n+1); mint a = 1; c[0] = a; for(int i=1;i<=n;i++) { a = a*mint(n+1-i)/i; c[i] = a; } return c; } static std::vector<mint> construct_fact(int n) { std::vector<mint> f(n+1,1); for(int i=2;i<=n;i++) f[i]=f[i-1]*i; return f; } }; std::istream& operator>>(std::istream&i,mint&a){ lnt t; i>>t; a=mint(t); return i; } std::ostream& operator<<(std::ostream&o,const mint&a){ o<<a.v; return o; } int main() { int n,k; lnt m; std::cin >> n >> k >> m; MOD=m; lnt N=n*(n+1)/2; V<V<mint> > pre(n+1,V<mint>(N*k+1,0)); { // precompute pre[0][0]=1; for(int i=1;i<=n;i++) { rep(j,N*k+1) { if(j==0) pre[i][j]=1; else { if(j-i<0) pre[i][j]=pre[i-1][j]; else pre[i][j]=pre[i][j-i]+pre[i-1][j]; } } for(int j=N*k;j>=0;j--) { if(j-(k+1)*i<0) break; pre[i][j]-=pre[i][j-(k+1)*i]; } } } for(int x=1;x<=n;x++) { mint ans=0; // up to n-x and up to x-1 rep(i,N*k+1) { ans+=pre[x-1][i]*pre[n-x][i]*mint(k+1); } print(ans-1); } }
/* om namah shivay */ #include<bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define REP(i,a,b) for(i=a;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ll long long #define si(x) scanf("%d",&x) #define sl(x) scanf("%lld",&x) #define ss(s) scanf("%s",s) #define pi(x) printf("%d\n",x) #define pl(x) printf("%lld\n",x) #define ps(s) printf("%s\n",s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 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; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0,lim-1); return uid(rang); } int mpow(int base, int exp); const int mod = 1'000'000'007; //======================= void solve() { ll int m,h; cin>>m>>h; if(h%m==0) cout<<"Yes"<<endl; else { cout<<"No"<<endl; } } int main() { ll int t = 1; //cin >> t; while(t--) { solve(); } return 0; } ll int mpow(ll int base,ll int exp) { base %= mod; ll int result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; }
// clang++ -std=c++14 probA.cpp #include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <limits> #include <string> #include <cassert> #include <unordered_map> using namespace std; typedef long long ll; typedef long long LL; typedef pair<int,int> pii; #define forup(i,a,b) for(int i=a; i<b; ++i) #define fordn(i,a,b) for(int i=a; i>b; --i) #define rep(i,a) for(int i=0; i<a; ++i) #define dforup(i,a,b) for(i=a; i<b; ++i) #define dfordn(i,a,b) for(i=a; i>b; --i) #define drep(i,a) for(i=0; i<a; ++i) // #define slenn(s,n) for(n=0; s[n]!=13 and s[n]!=0; ++n);s[n]=0 #define gi(x) scanf("%d",&x) #define gl(x) cin>>x #define gd(x) scanf("%lf",&x) #define gs(x) scanf("%s",x) #define pis(x) printf("%d ",x) #define pin(x) printf("%d\n",x) #define pls(x) cout<<x<<" " #define pln(x) cout<<x<<"\n" #define pds(x) printf("%.12f ",x) #define pdn(x) printf("%.12f\n",x) #define pnl() printf("\n") #define vec vector<int> #define fs first #define sc second #define pb push_back const int inv=1000000000; const int minv=-inv; const int max_n=200000+5; int n,r; int b[max_n]; vector<int> chl[max_n]; int depth[max_n]; LL res; vector<int> sol; vector<pii> q; bool used[max_n]; set<pii,greater<pii> > Sdec; set<pii,greater<pii> >::iterator it; // #include <algorithm> // int bin_sea .rc // vector<int> travel {0,1}; bool cmp(const vector<int> &a,const vector<int> &b) { if (a[0] == b[0]) return a[1]>b[1]; return a[0]<b[0]; } vector<int> visited(2*(100000)+2, -1); vector<int> ans(2*(1000000)+2, -1); void solve(){ vector<int> arr; for (int i = 0;i<4;i++){ int x; gi(x); arr.push_back(x); } sort(arr.begin(),arr.end()); if (arr[0] + arr[1] +arr[2] == arr[3]){ cout<<"Yes"<<endl; } else if (arr[0] + arr[3] == arr[1] +arr[2] ) cout<<"Yes"<<endl; else cout<<"No"<<endl; return; } int main() { ll test = 1; for (int i = 0;i<test;i++){ solve(); } }
#include<bits/stdc++.h> using namespace std; #define INF 1000000007 #define MOD 998244353 #define LINF (1LL << 62) #define PI 3.14159265358979 typedef long long i64; typedef pair<int,int> P; inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; } 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; } i64 ceil(i64 a, i64 b) { return (a+b-1)/b; } mt19937_64 rnd(time(NULL)); const int n = 30; const int Q = 1000; int e[30][30][4]; int etimes[30][30][4]; int dh[4] = {0,-1,0,1}, dw[4] = {-1,0,1,0}; char dc[4] = {'L','U','R','D'}; map<char, int> dtoi; void update(string s, int cost, int h, int w){ int sz = s.size(); int c = cost / sz; for(int i = 0; i < sz; i++){ int d = dtoi[s[i]]; etimes[h][w][d]++; e[h][w][d] += (c - e[h][w][d]) / (etimes[h][w][d] + 1); int nh = h + dh[d]; int nw = w + dw[d]; etimes[nh][nw][d ^ 2] = etimes[h][w][d]; e[nh][nw][d ^ 2] = e[h][w][d]; h = nh; w = nw; } } void solve(){ //前処理 for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ for(int d = 0; d < 4; d++){ e[i][j][d] = 1000; } } } for (int d = 0; d < 4; d++) { dtoi[dc[d]] = d; } // for(int t = 0; t < Q; t++){ int ah, aw, bh, bw; cin >> ah >> aw >> bh >> bw; string ans; // priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> pq; pq.push(make_pair(0, P(ah, aw))); string s[30][30]; int mi[30][30]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { mi[i][j] = INF; } } mi[ah][aw] = 0; while (!pq.empty()) { auto p = pq.top(); pq.pop(); int c = p.first; int h = p.second.first; int w = p.second.second; if (h == bh && w == bw) { break; } for (int d = 0; d < 4; d++) { int nh = h + dh[d]; int nw = w + dw[d]; if (nh < 0 || nw < 0 || nh >= n || nw >= n) continue; int nc = c + e[h][w][d]; if (mi[nh][nw] <= nc) continue; mi[nh][nw] = nc; pq.push(make_pair(nc, P(nh, nw))); s[nh][nw] = s[h][w]; s[nh][nw] += dc[d]; } } // ans = s[bh][bw]; cout << ans << endl; int cost; cin >> cost; // update(ans, cost, ah, aw); // } } int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<random> #include<map> long long int realOdometer = 0; long long int manhattanOdometer = 0; long long int generalAverage = 0; using namespace std; int main(){ pair<int, int>s, t; map<pair<pair<int, int>, pair<int, int>>, pair<int, int>>distance; int loopCount = 0; while(cin >> s.first >> s.second >> t.first >> t.second){ string str = ""; for(int i=0; i<abs(t.first-s.first); i++){ str = str + (t.first-s.first>0? "D": "U"); } for(int i=0; i<abs(t.second-s.second); i++){ str = str + (t.second-s.second>0? "R": "L"); } mt19937 g(loopCount); shuffle(str.begin(), str.end(), g); if(loopCount<2){ cout << str << endl; }else{ str.clear(); pair<int, int>currentPos = s; while(currentPos!=t){ if(currentPos.first==t.first||currentPos.second==t.second){ if(currentPos.first==t.first){ str.push_back(currentPos.second<t.second? 'R': 'L'); }else{ str.push_back(currentPos.first<t.first? 'D': 'U'); } }else{ pair<int, int>component0 = make_pair(currentPos.first, currentPos.second + (currentPos.second<t.second? +1: -1)); pair<int, int>component1 = make_pair(currentPos.first + (currentPos.first<t.first? +1: -1), currentPos.second); pair<pair<int, int>, pair<int, int>>candidate0 = make_pair(currentPos, component0); pair<pair<int, int>, pair<int, int>>candidate1 = make_pair(currentPos, component1); if(candidate0.first>candidate0.second){ swap(candidate0.first, candidate0.second); } if(candidate1.first>candidate1.second){ swap(candidate1.first, candidate1.second); } int d0 = distance[candidate0].first!=0? distance[candidate0].first: generalAverage; int d1 = distance[candidate1].first!=0? distance[candidate1].first: generalAverage; if(d0<d1){ str.push_back(currentPos.second<t.second? 'R': 'L'); }else{ str.push_back(currentPos.first<t.first? 'D': 'U'); } } currentPos.first+= (str.back()=='D') - (str.back()=='U'); currentPos.second+= (str.back()=='R') - (str.back()=='L'); } cout << str << endl; } long long int D; cin >> D; realOdometer+= D; manhattanOdometer+= str.length(); generalAverage = realOdometer/manhattanOdometer; int manhattan_d = str.length(); vector<pair<pair<int, int>, pair<int, int>>>toDoList; for(int i=0; i<manhattan_d; i++){ pair<int, int>temp1, temp2; if(i!=0){ temp1 = toDoList.back().second; }else{ temp1 = s; } auto convert = [&](char c){ pair<int, int> val = temp1; if(c=='R'||c=='L'){ val = make_pair(val.first, val.second + (c=='R'? +1: -1)); }else{ val = make_pair(val.first + (c=='D'? +1: -1), val.second); } return val; }; temp2 = convert(str.at(i)); toDoList.push_back(make_pair(temp1, temp2)); } while(toDoList.size()!=0){ if(toDoList.back().first>toDoList.back().second){ swap(toDoList.back().first, toDoList.back().second); } distance[toDoList.back()].second++; distance[toDoList.back()].first = (distance[toDoList.back()].first * (distance[toDoList.back()].second-1) + D/manhattan_d)/distance[toDoList.back()].second; toDoList.pop_back(); } loopCount++; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; void solve() { int n; cin >> n; vector<int> a(n); for (int &i : a) { cin >> i; } sort(a.begin(), a.end()); ll f = 0; for (int i = n - 1; i >= 0; i -= 2) { f += a[i]; } ll s = accumulate(a.begin(), a.end(), 0ll); if (f > s - f) { cout << (n % 2 ? "Second" : "First") << endl; return; } cout << "Second" << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,N) for(int i=0;i<int(N);++i) #define rep1(i,N) for(int i=1;i<int(N);++i) #define all(a) (a).begin(),(a).end() #define bit(k) (1LL<<(k)) #define SUM(v) accumulate(all(v), 0LL) typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; struct fast_ios{ fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; }fast_ios_; 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; } #define TOSTRING(x) string(#x) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for(auto _: v) os << _ << ", "; os << "]"; return os; }; template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for(auto _: st) { os << _ << ", "; } os << ")";return os;} template <typename T, typename U> ostream &operator<<(ostream &os, const pair< T, U >& p){os << "{" <<p.first << ", " << p.second << "}";return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp){ os << "["; for(auto _: mp){ os << _ << ", "; } os << "]" << endl; return os; } #define DUMPOUT cerr void dump_func(){ DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG #define dbg(...) dump_func(__VA_ARGS__) #define dump(...) DUMPOUT << string(#__VA_ARGS__) << ": "; dump_func(__VA_ARGS__) #else #define dbg(...) #define dump(...) #endif const int INF = (ll)1e9; const ll INFLL = (ll)1e18+1; const ll MOD = 1000000007; // const ll MOD = 998244353; const long double PI = acos(-1.0); /* const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string dir = "DRUL"; */ void fi(){ cout << "First" << endl; } void se(){ cout << "Second" << endl; } void solve(){ int N; cin >> N; vector<ll> a(N); rep(i,N)cin >> a[i]; sort(all(a)); ll cnt[2] = {0, 0}; rep(i,N)cnt[i&1] +=a[i]; dump(cnt[0], cnt[1]); if(N == 1){ se(); return; } if(N == 2){ if(a[0] == a[1])se(); else fi(); return; } if(N&1){ if(cnt[0] == cnt[1])fi(); else se(); } else{ if(cnt[0] == cnt[1])se(); else fi(); } } int main(){ int t; cin >> t; while(t--)solve(); }
/* Author: Aryan Yadav */ #include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define f(i, a, b) for (long long i = a; i < b; i++) #define rep(i, a, b) for (long long i = a; i <= b; i++) #define fm(i, a, b) for (long long i = a; i > b; i--) #define repm(i, a, b) for (long long i = a; i >= b; i--) #define vi vector<int> #define vl vector<long long> #define vs vector<string> #define pqmaxi priority_queue<int> #define pqmaxl priority_queue<ll> #define pqmini priority_queue<int, vi, greater<int>> #define pqminl priority_queue<ll, vl, greater<ll>> #define all(x) x.begin(), x.end() #define test \ ll test; \ cin >> test; \ while (test--) #define pb(i) push_back(i) #define mp(i, j) make_pair(i, j) #define fast \ ios::sync_with_stdio(false); \ cin.tie(NULL) int main() { fast; int n, m, t1; cin >> n >> m >> t1; int cap = n; ll prev = 0; f(i, 0, m) { ll a, b; cin >> a >> b; cap -= (a - prev); cap = max(0, cap); if (cap == 0) { cout << "No"; exit(0); } f(j, a, b) { cap += 1; } cap = min(cap, n); prev = b; } cap -= (t1 - prev); if (cap <= 0) { cout << "No"; } else { cout << "Yes"; } }
//#include <atcoder/all> //using namespace atcoder; #include <bits/stdc++.h> using namespace std; typedef long long ll; #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 SIZE(x) int(x.size()) #define bit(n) (1LL<<(n)) #define FIX(d) fixed << setprecision(d) using P = pair<int, int>; using vvi = vector<vector<int>>; using vvl = vector<vector<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 = 2e9; const ll LINF = (1LL<<60); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; if(n==1 && m==0){ cout << "1 2\n"; return 0; } if(m<0 || m>=n-1){ cout << "-1\n"; return 0; } vector<int> l(n), r(n); l[0] = 1; r[0] = 2; FOR(i,1,n){ l[i] = r[i-1]+2; r[i] = l[i]+1; } if(m>0) r[0] = r[m+1]+1; REP(i,n){ cout << l[i] << " " << r[i] <<'\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; typedef long long int ll; using ii= pair<ll,ll>; #define fr(i,a,b) for(ll i=a;i<b;i++) #define all(o) (o).begin(),(o).end() #define F first #define S second #define MP make_pair #define PB push_back #define ld long double #define eps 0.00000000001 #define mod 1000000007 class prioritize{ public: bool operator() (ii &p1,ii &p2){ return p1.F<p2.F; } }; auto start_time= high_resolution_clock::now(); void time() { #ifndef ONLINE_JUDGE auto stop_time= high_resolution_clock::now(); auto duration= duration_cast<milliseconds>(stop_time-start_time); cout<<"run time: "<<duration.count()<<" ms"<<"\n"; #endif return; } void ojudge() { #ifndef ONLINE_JUDGE freopen("input1.txt","r", stdin); freopen("output1.txt","w",stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); return; } const int N= 2010; ll dp[N][N][2]; ll mat[N][N];ll h,w; ll rec(ll i,ll j,ll turn){ if(i<1||i>h||j<1||j>w) return 0; if(i==h && j==w) return 0; if(dp[i][j][turn]!=-1) return dp[i][j][turn]; if(turn==0){ ll ans= -1e9; if(i+1<=h) ans= max(ans,mat[i+1][j]-rec(i+1,j,1)); if(j+1<=w) ans= max(ans,mat[i][j+1]-rec(i,j+1,1)); return dp[i][j][turn]= ans; } else{ ll ans= -1e9; if(i+1<=h) ans= max(ans,mat[i+1][j]-rec(i+1,j,0)); if(j+1<=w) ans= max(ans,mat[i][j+1]-rec(i,j+1,0)); return dp[i][j][turn]= ans; } } int main() { ojudge(); cin>>h>>w; map<char,ll> mp; mp['+']= 1,mp['-']= -1; memset(dp,-1,sizeof(dp)); fr(i,1,h+1){ fr(j,1,w+1){ char x;cin>>x; mat[i][j]= mp[x]; } } ll v1= rec(1,1,0); if(v1>0) cout<<"Takahashi\n"; else if(v1<0) cout<<"Aoki\n"; else cout<<"Draw\n"; time(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N, M,T; cin >> N >> M>>T; int Nmax = N; vector<int> A(M); vector<int> B(M); for(int i = 0; i < M;i++){ int x = 0; int y = 0; cin >> x >> y; A.at(i) = x; B.at(i) = y; } bool flag = true; for(int i = 0; i < M; i++){ if(i == 0) N -= A.at(i); else N -= (A.at(i) - B.at(i-1)); if(N <= 0){ flag = false; break; } N += (B.at(i)-A.at(i)); if(N > Nmax){ N = Nmax; } } N -= (T - B.at(M-1)); if(N <= 0){ flag = false; } if(flag){ cout << "Yes" << endl; } if(flag == false) cout << "No" <<endl; }
#include <iostream> #include <cassert> #include <vector> #include <algorithm> using namespace std; int main(){ long long n; cin >> n; long long x = 0, y = 0; vector<int> Q; auto q = [&](int i){ if(i == 0){ x += 1; } if(i == 1){ y += 1; } if(i == 2){ x += y; } if(i == 3){ y += x; } Q.emplace_back(i); }; vector<long long> F(2,0); F[1] = 1; for(int i = 2; i < 130; ++i){ long long t = F[i-1] + F[i-2]; if(t > n) break; F.emplace_back(t); } vector<int> A; long long n_ = n; for(int i = F.size()-1; i >= 0; --i){ if(F[i] <= n_){ A.emplace_back(i); n_ -= F[i]; } } q(0); int p = 3, j = A[0]-1; for(auto a : A){ if(a == A[0]) continue; while(j > a){ q(p); p ^= 1; --j; } q(p&1); } if(y == n){ swap(x,y); for(auto& q : Q) q ^= 1; } cout << Q.size() << endl; for(auto e : Q){ cout << e+1 << '\n'; } cerr << x << " " << y << endl; assert(x == n); }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #include <climits> #include <cstring> #include <cassert> using namespace std; //using namespace atcoder; #define REP(i, n) for (int i=0; i<(n); ++i) #define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i) #define FOR(i, a, n) for (int i=(a); i<(n); ++i) #define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define DUMP(x) cerr<<#x<<" = "<<(x)<<endl #define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl; template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; REP(i, SZ(v)) { if (i) os << ", "; os << v[i]; } return os << "]"; } template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << " " << p.second << ")"; } template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; const ll MOD = 1e9 + 7; const int INF = INT_MAX / 2; const ll LINF = LLONG_MAX / 2; const ld eps = 1e-9; // edit void solve() { int N; cin >> N; vector<int> A(N), B(N); REP(i, N) { cin >> A[i]; } REP(i, N) { cin >> B[i]; } int diff = *min_element(ALL(B)) - *max_element(ALL(A)); if (diff < 0) { cout << 0 << endl; } else { cout << diff + 1 << endl; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); // std::ifstream in("input.txt"); // std::cin.rdbuf(in.rdbuf()); solve(); return 0; }
#define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 0 #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; // clang-format off #define PI 3.14159265358979323846264338327950L #define rep(i,n) for (int i=0; i<(int)(n); ++i) #define rep2(i,s,n) for (int i=(s); i<(int)(n); ++i) #define prep_ios(do_flush) if(!do_flush){ ios::sync_with_stdio(false);cin.tie(nullptr);constexpr char endl='\n'; } cout<<fixed<<setprecision(20) #define put(expr) cout<<expr<<'\n' template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "[ "; for (auto v: vec) os << v << " "; os << "]"; return os; }template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "< "; for (auto v: vec) os << v << " "; os << ">"; return os; } template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; }template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << ", " << pa.second << ")"; return os; } typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef float f32; typedef double f64; typedef long double f80; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef map<int, int> mii; typedef __int128_t i128; i128 parse(string &s) { i128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } istream &operator>>(istream &is, i128 &v) { string s; is >> s; v = parse(s); return is; } ostream &operator<<(ostream &os, const i128 &v) { if (v == 0) return (os << "0"); i128 num = v; if (v < 0) { os << '-'; num = -num; } string s; for (; num > 0; num /= 10) s.push_back('0' + (char)(num % 10)); reverse(s.begin(), s.end()); return (os << s); } template <typename T> bool chmax(T &now, const T &cand) { if (now < cand) { now = cand; return true; } return false; } template <typename T> bool chmin(T &now, const T &cand) { if (now > cand) { now = cand; return true; } return false; } template <typename T> T gcd(const T &a, const T &b) { if (a % b == 0) return b; return gcd(b, a % b); } template <typename T> T lcm(const T &a, const T &b) { return a * b / gcd(a, b); } template <typename T> T nCr(const T &n, T k) { if (k > n) return 0; if (k * 2 > n) k = n - k; if (k == 0) return 1; int result = n; for (int i = 2; i <= k; ++i) { result *= (n - i + 1); result /= i; } return result; } const int INF = 1<<30; /* INF > 10^9 */ const i64 INFLL = 1LL<<60; /* INFLL > 10^18 */ const string YES = "Yes", NO = "No"; // clang-format on? int main() { prep_ios(true); vi anss = vi(1010); i64 N, M; cin >> N >> M; rep(i, N) { i64 tmp; cin >> tmp; ++anss[tmp]; } rep(i, M) { i64 tmp; cin >> tmp; ++anss[tmp]; } bool first = true; rep(i, anss.size()){ if (anss[i]==1){ if (first) { first = false; } else{ cout << ' '; } cout<<i; } } cout<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define fi first #define se second #define N 214514 #define all(x) (x).begin(), (x).end() const ll mod = 998244353; const ll inf = 8000000000000000000; int main() { vector<ll> used(1010); ll n, m; cin >> n >> m; for (int i = 0; i < n; i++) { ll x; cin >> x; used[x] += 1; } vector<ll> ans; for (int i = 0; i < m; i++) { ll x; cin >> x; used[x]++; } for (int i = 1; i <= 1000; i++) { if (used[i] == 1) cout << i << ' '; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int x; cin >> x; if(x < 0){ cout << 0 << endl; }else { cout << x << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(int i = 0; i < (n); i++) #define INF 2 << 29 #define MOD 1000000007 #define Yes(f) puts((f)? "Yes" : "No") #define YES(f) puts((f)? "YES" : "NO") #define pi 3.1415926535897 int gcd(int a, int b) { if (b != 0) return gcd(b, a % b); return a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } using Graph = vector<vector<int>>; int main(){ int x; cin >> x; cout << (max(x, 0)) << endl; }
#include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <bits/stdc++.h> using namespace std; int main(){ string S; cin >> S; int cnt = 0; for( int i = 0; i < 10000; i++ ){ int used[10] = {0}; for( int a = 0; a < 4; a++ ){ int temp = i / int(pow(10,a)) % 10; used[temp]++; } int NGflag = 0; for( int b = 0; b < 10; b++ ){ if( S[b] == 'o' && used[b] == 0 ){ NGflag = 1; break; } if( S[b] == 'x' && used[b] != 0 ){ NGflag = 1; break; } } if( NGflag == 0 ){ cnt++; } } cout << cnt << endl; return 0; }
/* written by Pankaj Kumar. country:-INDIA Institute: National Institute of Technology, Uttarakhand */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll ; typedef unsigned long long ull; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; #define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0); /* Abbrevations */ #define ff first #define ss second #define mp make_pair #define line cout<<endl; #define pb push_back #define Endl "\n" // loops #define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i]; // Some print #define no cout<<"No"<<endl; #define yes cout<<"Yes"<<endl; // sort #define all(V) (V).begin(),(V).end() #define srt(V) sort(all(V)) #define srtGreat(V) sort(all(V),greater<ll>()) // some extra #define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line; #define precision(x) cout<<fixed<<setprecision(x); #define sz(V) ll(V.size()) // template template <typename T> T mymax(T x,T y) { return (x>y)?x:y; } // function ll power(ll x,ll y,ll mod) { ll res=1; // x=x%mod; while(y>0) { if(y%2==1) { res*=x; // res=res%mod; } y/=2; x*=x; // x=x%mod; } return res; } ll str_to_num(string s) { return stoi(s); } string num_to_str(ll num) { return to_string(num); } // datatype definination #define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> class Point { public: ll x; ll y; ll z; ll getsum() { return x+y+z; } }; /* ascii value A=65,Z=90,a=97,z=122 */ /* --------------------MAIN PROGRAM----------------------------*/ // to run ctrl+b const ll INF=LONG_MAX; const ll mod1=1e9+7; const ll mod2=998244353; // Techniques // divide into cases, brute force, pattern finding // sort, greedy, binary search, two pointer // transform into graph ll solve() { ll n; cin>>n; vl a(n); vl b(n); vl c(n); forin(a,n); forin(b,n); forin(c,n); map<ll,ll> mc; map<ll,vector<ll>> mb; map<ll,ll> ma; for(auto x:a){ ma[x]++; } ll ans=0; for(ll i=0;i<n;i++){ mb[b[i]].pb(i+1); } for(auto x:c){ mc[x]++; } for(auto x:ma){ if(sz(mb[x.ff])){ for(auto y:mb[x.ff]){ ans+=x.ss*mc[y]; } } } // for(ll i=0;i<n;i++){ // // cout<<"ai si "<<a[i]<<endl; // if(sz(mb[a[i]])){ // // cout<<"" // for(auto x:mb[a[i]]){ // ans+=mc[x]; // } // } // } cout<<ans<<endl; return 0; } int main() { speed; /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif */ ll TestCase=1; // cin>>TestCase; while(TestCase--) { solve(); } } /* -----------------END OF PROGRAM --------------------*/ /* * stuff you should look before submission * constraint and time limit * int overflow * special test case (n=0||n=1||n=2) * don't get stuck on one approach if you get wrong answer */
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> p; #define MOD 1000000007 map<ll, ll> m; map<ll, bool> c; ll memo(ll x, ll y) { if(c[y]) return m[y]; ll a; if(y == 1LL) { a = x - 1LL; } else if(y % 2LL) { a = min(abs(x - y), min(memo(x, (y + 1LL) / 2LL) + 2LL, memo(x, (y - 1LL) / 2LL) + 2LL)); } else { a = min(abs(x - y), memo(x, y / 2LL) + 1LL); } c[y] = true; return m[y] = a; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll X, Y; cin >> X >> Y; cout << memo(X, Y) << endl; }
#include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define REP(i,n) for(ll i = 0; i < (ll)(n); ++i) #define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<int MOD> struct Fp { ll val; constexpr Fp(ll v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { ll a = r.val, b = MOD, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; constexpr ll MOD = 1e9 + 7; using mint = Fp<MOD>; #define pb push_back #define mp make_pair #define mt make_tuple int N, M; vector<int> As; vector<int> Xs; vector<int> Ys; vector<vector<int>> from_to; vector<vector<int>> to_from; vector<int> cache; int dfs(int n) { if (cache[n] != -1e9) return cache[n]; int mx = 0; for(auto v: from_to[n]) { chmax(mx, As[v]); chmax(mx, dfs(v)); } return cache[n] = mx; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; As.resize(N); Xs.resize(M); Ys.resize(M); from_to.resize(N); cache.resize(N, -1e9); REP(n, N) cin >> As[n]; REP(m, M) { int x, y; cin >> x >> y; --x; --y; from_to[x].pb(y); } cin >> N; int ans = -1e9; REP(n, N) { // 買うことができない if (SIZE(from_to[n]) == 0) continue; int tmp = dfs(n) - As[n]; chmax(ans, tmp); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; #define rep(i, n) for(int i = 0; i < n; i++) int main() { int n; string s,t; cin >> n >> s >> t; vector<int> s0; vector<int> t0; rep(i,n){ if(s[i] == '0')s0.push_back(i); if(t[i] == '0')t0.push_back(i); } if(s0.size()!=t0.size()){ cout << -1 << endl; return 0; } int ans = s0.size(); rep(i,s0.size()){ if(s0[i]==t0[i])ans--; } if(ans < 0)ans = 0; cout << ans << endl; return 0; }
#include<cstdio> #include<vector> #include<algorithm> using namespace std; #define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i) #define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i) #define fi first #define se second #define Endl putchar('\n') typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template<class T>inline T fab(T x){ return x<0? -x: x; } template<class T>inline T readin(T x){ x=0; int f=0; char c; while((c=getchar())<'0' || '9'<c) if(c=='-') f=1; for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48)); return f? -x: x; } const int maxn=500000; int a[maxn+5], b[maxn+5], n; vector<int>pa, pb; inline void input(){ n=readin(1); rep(i, 1, n) scanf("%1d", &a[i]); rep(i, 1, n) scanf("%1d", &b[i]); } signed main(){ input(); rep(i, 1, n) if(!a[i]) pa.push_back(i); rep(i, 1, n) if(!b[i]) pb.push_back(i); if(pa.size()!=pb.size()) return printf("-1\n"), 0; int ans=0; for(int i=0, siz=pa.size(); i<siz; ++i) if(pa[i]!=pb[i]) ++ans; printf("%d\n", ans); return 0; }
#include <iostream> using namespace std; int main() { int x, y; cin >> x >> y; if (x > y && y + 3 > x) cout << "Yes"; else if(y > x && x + 3 > y) cout << "Yes"; else cout << "No"; }
#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <chrono> #include <complex> using namespace std; #define ll long long #define ld long double #define ui unsigned int #define ull unsigned ll #define mp make_pair #define eb emplace_back #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define hashmap unordered_map #define hashset unordered_set #define lb lower_bound #define ub upper_bound #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define ff first #define ss second #define foi(n) for(ll i=0;i<n;i++) #define foj(n) for(ll j=0;j<n;j++) #define fok(n) for(ll k=0;k<n;k++) #define forr(i,a,b) for(ll i=a;i<b;i++) #define forrr(i,b,a) for(ll i=b;i>=a;i--) #define forrrr(i,a,b,k) for(ll i=a;i<b;i=i+k) #define graph vector<vector<int>> #define sz(v) v.size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<string> vs; typedef vector<double> vd; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef pair< ll, pll> plll; typedef vector<plll> vplll; typedef vector<string> vs; typedef vector<char> vc; typedef vector<bool> vb; typedef map<string, int> msi; typedef map<int, int> mii; typedef map<ll, ll> mll; typedef map<char, int> mci; typedef map<int, string> mis; typedef pair<string, int> psi; typedef pair<string, string> pss; typedef priority_queue <ll> pq; typedef priority_queue<pii, vector<pii>, greater<pii> > pqq; const ll MOD = 1000000007; void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input1.txt", "r", stdin); freopen("error1.txt", "w", stderr); freopen("output1.txt", "w", stdout); #endif // ll t ; cin >> t; // while (t--) { solve(); cout << "\n"; } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; return 0; } ll ceils(ll x, ll y) { return x / y + ((x % y) != 0); } 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 / gcd(a, b) * b; } void solve() { ll a, b; cin >> a >> b; if (a < b) swap(a, b); if (b + 3 > a) cout << "Yes"; else cout << "No"; }
#include<set> #include<ctime> #include<cstdio> #include<cctype> #include<vector> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int N=1e4+7; int read(){ char c; int x=0,f=1; while(!isdigit(c=getchar())) f-=2*(c=='-'); while(isdigit(c)){ x=x*10+f*(c-48); c=getchar(); } return x; } struct que{ int p,i; que(int a=0,int b=0){ p=a; i=b; } friend bool operator <(que a,que b){ return a.p<b.p; } }; struct sol{ int a,b,c,d; sol(int A=0,int B=0,int C=0,int D=0){ a=A; b=B; c=C; d=D; } }; int c[N]; bool vis[N]; int n,x,y,r,tot; vector<que>l[N]; sol ans[N]; int random(int l,int r){ return rand()%(r-l+1)+l; } int main(){ #ifndef ONLINE_JUDGE freopen("A.in","r",stdin); freopen("A.out","w",stdout); #endif clock_t t1=clock(); //-------- n=read(); srand(210280344); for(int i=1;i<=n;++i){ x=read(); y=read(); r=read(); l[x].push_back(que(y,i)); vis[x]=1; } for(int i=0;i<N-7;++i){ if(vis[i]){ ++tot; c[tot]=i; sort(l[i].begin(),l[i].end()); } } c[tot+1]=N-7; for(int i=1;i<=tot;++i){ int A; int B; int C; int D; int sz=l[c[i]].size(); if(i==1) A=random(0,c[i]); else A=random(c[i-1],c[i]); C=random(c[i]+1,c[i+1]); B=random(0,l[c[i]][0].p); if(sz==1){ D=random(l[c[i]][0].p+1,N-7); ans[l[c[i]][0].i]=sol(A,B,C,D); c[i]=C; continue; } D=random(l[c[i]][0].p+1,l[c[i]][1].p); l[c[i]][0].p=D; ans[l[c[i]][0].i]=sol(A,B,C,D); for(int j=1;j<sz-1;++j){ B=random(l[c[i]][j-1].p,l[c[i]][j].p); D=random(l[c[i]][j].p+1,l[c[i]][j+1].p); l[c[i]][j].p=D; ans[l[c[i]][j].i]=sol(A,B,C,D); } B=random(l[c[i]][sz-2].p,l[c[i]][sz-1].p); D=random(l[c[i]][sz-1].p+1,N-7); ans[l[c[i]][sz-1].i]=sol(A,B,C,D); c[i]=C; } for(int i=1;i<=n;++i){ int A=ans[i].a; int B=ans[i].b; int C=ans[i].c; int D=ans[i].d; printf("%d %d %d %d\n",A,B,C,D); } //-------- fprintf(stderr,"%0.3lfs",1.0*(clock()-t1)/CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> using namespace std; void chmin(long long int& a, long long int b) { if (a > b) { a = b; } } int main() { int N, M; cin >> N >> M; // 入力する vector<long long int> H(N); vector<long long int> W(M); for (int i = 0; i < N; i++) { cin >> H[i]; } for (int i = 0; i < M; i++) { cin >> W[i]; } // vectorをソートする sort(H.begin(), H.end()); vector<long long int> sum1((N + 1) / 2); vector<long long int> sum2((N + 1) / 2); for (int i = 0; i + 1 < N; i += 2) { sum1[i / 2 + 1] = sum1[i / 2] + H[i + 1] - H[i]; } for (int i = N - 2; i > 0; i -= 2) { sum2[i / 2] = sum2[i / 2 + 1] + H[i + 1] - H[i]; } long long int res = LLONG_MAX; // lower_boundで各W_iの入るindexを調べる for (long long int w : W) { long long int index = lower_bound(H.begin(), H.end(), w) - H.begin(); if (index & 1) { index ^= 1; } chmin(res, sum1[index / 2] + sum2[index / 2] + abs(H[index] - w)); } cout << res << endl; }
#include<bits/stdc++.h> #include<iostream> #include<map> #include<math.h> #include<string> #include<string.h> #include<vector> #include<queue> #include<algorithm> #include<set> #define _GLIBCXX_DEBUG #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() using namespace std; using ll=long long; const ll INF=50000000000000000; const ll dx[8]={1,0,-1,0,1,1,-1,-1}; const ll dy[8]={0,1,0,-1,1,-1,1,-1}; ll mod(ll x,ll m){return x & m;} ll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0){u+=m;}return u;} ll modpow(ll a,ll n,ll m){ll res=1;while(n>0){if(n&1){res=res*a%m;}a=a*a%m;n>>=1;}return res;} ll npow(ll a,ll n){ll res=1;while(n>0){if(n&1){res=res*a;}a=a*a;n>>=1;}return res;} ll yaku(ll x){ll cnt=0;for(int i=1;i*i<=x;i++){if(x%i==0){if(i==x/i){cnt++;}else{cnt+=2;}}}return cnt;} ll modwaru(ll a, ll b, ll m){a%=m;return a*modinv(b,m)%m;} ll gcd(ll x, ll y){if(x%y==0){return y;}else{return gcd(y, x % y);}} struct UnionFind{vector<ll>par;UnionFind(ll N):par(N){for(ll i=0;i<N;i++)par[i]=i;}ll root(ll x){if(par[x]==x){return x;}return par[x]=root(par[x]);}void unite(ll x,ll y){ ll rx=root(x);ll ry=root(y);if(rx==ry){return;}par[rx]=ry;}bool same(ll x,ll y){ll rx=root(x);ll ry=root(y);return rx==ry;}}; int main(){ ll N, ans = 0; cin >> N; vector<ll> A(N), rui(N + 1, 0); for(int i = 0; i < N; i++) cin >> A[i]; rui[0] = 0; for(int i = 1; i <= N; i++){ if(i % 2 == 1) rui[i] += rui[i - 1] + A[i - 1]; else rui[i] += rui[i - 1] - A[i - 1]; } map<ll,ll> cnt; for(int i = 0; i <= N; i++){ cnt[rui[i]]++; } for(pair<ll,ll> a : cnt){ ans += a.second * (a.second - 1) / 2; } cout << ans << endl; }
#include<bits/stdc++.h> using ll = int_fast64_t; using P = std::pair<ll,ll>; using PP = std::pair<ll,P>; #define REP(i,b,e) for(int i=b; i<e; i++) #define PRINT(vec) {printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");} #define fi first #define se second const int MOD = 1e9+7; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; int main(){ ll n; scanf("%ld", &n); ll a[n]; REP(i, 0, n) scanf("%ld", &a[i]); ll waO[1+n] = {}, waE[1+n] = {}; REP(i, 0, n){ if(i%2) waO[i+1] += a[i]; else waE[i+1] += a[i]; waO[i+1] += waO[i]; waE[i+1] += waE[i]; } std::map<ll, ll> mp; REP(i, 0, n+1) mp[waO[i] - waE[i]]++; ll ans = 0; for(auto [k,v]: mp){ ans += v * (v-1) / 2; } printf("%ld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int MAX32 = 2147483647; const double PI = 3.14159265358979323846; int main() { long long n, p, min = 0; cin >> n; vector<char> num(300000, 0); for (int i = 0; i < n; i++) { cin >> p; num[p] = 1; while (num[min]) { min++; } cout << min << endl; } }
#include<bits/stdc++.h> using namespace std; const int N=100; string a[N]; long long dp[N]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=n;i++){ if(a[i]=="AND") dp[i]=dp[i-1]; else dp[i]=dp[i-1]+(1LL<<i); } cout<<dp[n]+1; return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int main(){ IOS; int n; cin >> n; vector<long long int> vec; for(int i=0; i<n; i++){ int a; cin >> a; vec.push_back(a); } long long first=0; for(int i=0; i<vec.size(); i++){ first+=abs(vec[i]); } cout << first <<endl; double second=0; for(int i=0; i<vec.size(); i++){ second+=pow(vec[i],2); } cout << setprecision(20)<<sqrt(second) <<endl; long long mx=INT32_MIN; for(int i=0; i<vec.size(); i++){ mx = max(mx,abs(vec[i])); } cout << mx <<endl; }
/* * * AUTHOR :- @soham01 * // "WORK HARD AND NEVER GIVE UP" // * */ #include "bits/stdc++.h" using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> //find_by_order(), order_of_key() #define pb push_back #define mp make_pair #define vi vector<long long int > #define vb vector<bool> #define vs vector<string> #define vd vector<double> #define vvi vector<vector<long long int> > #define vp vector<pair<long long int , long long int > > #define seti set<long long int > #define setc set<char> #define setd set<double> #define mod 1000000009 #define endl '\n' typedef long long int lli; // ITS MY REQUEST TO PLEASE PLEASE DO LOOK AT THE CONSTRAINS GIVEN CAREFULLY // DO CHECK FOR CORNER CASES /*-------LET THE GAME BEGIN-----------*/ lli power(lli x, lli y) { lli temp; if ( y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } const lli N = 1000005; lli prime_arr[N + 1]; vector<lli> pr; void sieve() { for (lli i = 2; i <= N; ++i) { if (prime_arr[i] == 0) { prime_arr[i] = i; pr.push_back (i); } for (lli j = 0; j < (lli)pr.size() && pr[j] <= prime_arr[i] && i * pr[j] <= N; ++j) prime_arr[i * pr[j]] = pr[j]; } } void solve() { lli n, i, j, c = 0, s = 0, f = 0; cin >> n; lli arr[n], brr[n]; for (i = 0; i < n; i++) { cin >> arr[i]; } for (i = 0; i < n; i++) { cin >> brr[i]; } for (i = 1; i <= 1000; i++) { lli flag = 0; for (j = 0; j < n; j++) { if (arr[j] <= i and brr[j] >= i) { //cout << arr[j] << " " << i << " " << brr[j] << endl; if (brr[j] >= i) { flag = 1; } else { flag = 0; break; } } else { flag = 0; break; } } if (flag == 1) { //cout << i << endl; c++; } } cout << c << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); /*lli t; cin >> t; while (t--)*/ // sieve(); solve(); return 0; } /* 1 3 3 5 6 1 3 1 3 3 1 3 */
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) using namespace std; typedef long long ll; typedef pair<int,int> P; const int MOD = 1e9 + 7; int main(){ string s; cin >> s; deque<char> t; int r = 0; for(char c : s){ if(c == 'R') r = 1 - r; else { if(r == 0) t.push_back(c); else t.push_front(c); } } if(r == 1) reverse(t.begin(),t.end()); string S; for(char c : t) { if(S.back() != c) S.push_back(c); else S.pop_back(); } cout << S << endl; return 0; }
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #include <stack> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} int main(){ cin.tie(0); ios::sync_with_stdio(false); string S; cin>>S; int N=S.size(); deque<char> DQ; bool rev=false; rep(i,N){ if(S[i]=='R'){ rev^=true; }else{ if(rev) DQ.push_front(S[i]); else DQ.push_back(S[i]); } } string T=""; while(!DQ.empty()){ if(rev){ T+=DQ.back(); DQ.pop_back(); }else{ T+=DQ.front(); DQ.pop_front(); } } string ans=""; stack<char> st; rep(i,T.size()){ if(st.empty()) st.push(T[i]); else{ if(st.top()==T[i]){ st.pop(); }else{ st.push(T[i]); } } } while(!st.empty()){ ans+=st.top(); st.pop(); } reverse(all(ans)); cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main () { int n; cin >> n; pair<int, int> ar[n]; for (int i = 0; i < n; i++) { cin >> ar[i].first >> ar[i].second; } int sol = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ld x = ar[j].first - ar[i].first, y = ar[j].second - ar[i].second; ld un = 1; if (-un <= y/x && y/x <= un) sol++; } } cout << sol << '\n'; }
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools #include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const i64 MOD = 1e9 + 7; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template <typename T> bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } void solve(std::vector<long long> r, std::vector<long long> c){ auto calc = [&](i64 x0, i64 y0, i64 x1, i64 y1){ if((x0 + y0 + i64(1e12)) % 2 != (x1 + y1 + i64(1e12)) % 2) return MOD; i64 cnt = 0; cnt += (x0 + y0) != (x1 + y1); cnt += (x0 - y0) != (x1 - y1); return cnt; }; int ans = (abs(r[0] - r[1]) + abs(c[0] - c[1]) + 2) / 3; for(int i = -5; i < 5; ++i){ for(int j = -5; j < 5; ++j){ if(abs(i) + abs(j) > 3) continue; int cnt = (abs(i) + abs(j) == 0 ? 0 : 1); i64 nx = r[0] + i; i64 ny = c[0] + j; ans = min(ans, int(calc(nx, ny, r[1], c[1])) + cnt); } } cout << ans << endl; } signed main(){ std::vector<long long> r(2); std::vector<long long> c(2); for(int i = 0 ; i < 2 ; i++){ scanf("%lld",&r[i]); scanf("%lld",&c[i]); } solve(std::move(r), std::move(c)); return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int to[400001], ne[400001], he[200001]; int pl[200001], dl[200001], Q[400010]; const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } int main() { //cin.tie(0); //ios::sync_with_stdio(false); int N = getint(), K = getint(); int k = 1; rep(i, N - 1) { int u = getint(), v = getint(); to[k] = v; ne[k] = he[u]; he[u] = k++; to[k] = u; ne[k] = he[v]; he[v] = k++; } int q = 0, p = 0, kazu = 1, kyori = 0; Q[q++] = 1; while(kyori < N) { while (p < kazu) { int u = Q[p++]; int tmp = 0; int ind = he[u]; he[u] = 0; while (ind) { int v = to[ind]; tmp = ne[ind]; if (he[v] > 0) { Q[q++] = v; ne[ind] = he[u]; he[u] = -ind; } ind = tmp; } } if (p == N) break; kazu = q; kyori++; } int are = 1000000, pd0 = are; int L = max(0, kyori / K / 2 - 1), R = (N + K - 1) / K; while (L + 1 < R) { int wj = (L + R) / 2; int num = 0; for (int i = N - 1; i >= 0; i--) { int po = pd0 - 1; int dd = pd0; int u = Q[i]; for (int ind = -he[u]; ind; ind = -ne[ind]) { int v = to[ind]; if (po < pl[v]) po = pl[v]; if (dd < dl[v]) dd = dl[v]; } if (po >= dd) dd = 0; if (dd >= pd0 + wj) { num++; po = pd0 + wj; dd = 0; } pl[u] = po - 1; dl[u] = dd + 1; } int are = num + (dl[1] > pd0); if (are > K) L = wj; else R = wj; pd0 += are; } printf("%d", R); Would you please return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 10; vector<int> edge[maxn]; int n, k, p, tot; int mx[maxn]; void dfs(int u, int pre) { mx[u] = 0; for(int v : edge[u]) if(v != pre) { dfs(v, u); if(mx[u] + mx[v] + 1 > 2 * p) { tot ++; mx[u] = min(mx[u], mx[v] + 1); } else mx[u] = max(mx[u], mx[v] + 1); } } bool check() { tot = 0; dfs(1, 0); return tot + 1 <= k; } int main() { scanf("%d%d", &n, &k); for(int i = 1; i < n; i ++) { int u, v; scanf("%d%d", &u, &v); edge[u].push_back(v); edge[v].push_back(u); } int l = 1, r = n, res; while(l <= r) { p = l + r >> 1; if(check()) r = p - 1, res = p; else l = p + 1; } printf("%d\n", res); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); long long n; unordered_set <long long> se; cin >> n; int x = sqrt(n); for (int i = 2;i < x+2;i++) for (int j = 2;j < n && pow(i,j) <= n;j++) se.insert(pow(i,j)); // for (long long l : se) // cout << l << endl; cout << n - se.size() << endl; return 0; }
//Codeforcesで128bit整数を使いたいとき //→__int128_tを使う&GNU C++17 (64)で提出する //インクルードなど #include <bits/stdc++.h> using namespace std; typedef long long ll; //イテレーション #define REP(i, n) for (ll i = 0; i < ll(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= ll(b); i++) #define FORD(i, a, b) for (ll i = a; i >= ll(b); i--) #define FORA(i, I) for (const auto &i : I) //x:コンテナ #define ALL(x) x.begin(), x.end() #define SIZE(x) ll(x.size()) //定数 #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //問題による //略記 #define F first #define S second //出力(空白区切りで昇順に) #define coutALL(x) \ for (auto i = x.begin(); i != --x.end(); i++) \ cout << *i << " "; \ cout << *--x.end() << endl; //aをbで割る時の繰上げ,繰り下げ ll myceil(ll a, ll b) { return (a + (b - 1)) / b; } ll myfloor(ll a, ll b) { return a / b; } ll A[17][65536]; int N; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; int t = static_cast<int>(pow(2,N)); REP(i,t){ cin >> A[N][i]; } for(int i= N;i>=1;i--){ for(int j=1,c=0;j<t;j+=2){ A[i-1][c++] = max(A[i][j-1],A[i][j]); } t/=2; } ll n1=min(A[1][0],A[1][1]); int ans = 0; t = static_cast<int>(pow(2,N)); REP(i,t){ if(A[N][i]==n1){ ans = i+1; break; } } cout << ans << endl; }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cstdlib> #include<queue> #include<set> #include<cstdio> #include<map> #include<cassert> using namespace std; #define ll long long #define reps(i, a, b) for(int i = a; i < b; i++) #define rreps(i, a, b) for(int i = a-1; i >= b; i--) #define rep(i, n) reps(i, 0, n) #define rrep(i, n) rreps(i, n, 0) #define P pair<int, int> #define vec vector<int> #define mat vector<vector<int> > const ll mod = 1000000007; const ll INF = 1001001001001001001; int n; vec a, t; ll fi(int i, ll num){ if(i == n){ return num; } if(t[i] == 1) { return fi(i+1, num + a[i]); }else if(t[i] == 2){ return fi(i+1, max(num, (ll)a[i])); }else{ return fi(i+1, min(num, (ll)a[i])); } } int main(){ cin >> n; a = vec(n); t = vec(n); rep(i, n) { cin >> a[i] >> t[i]; } int q; cin >> q; vec x(q); rep(i, q) cin >> x[i]; ll lval = fi(0, -INF); ll rval = fi(0, INF); if(lval == rval){ rep(i, q){ cout << lval << endl; } }else{ ll llim, rlim; { ll l = -INF, r = INF; while(l < r - 1){ ll mid = (l + r) / 2; if(fi(0, mid) == lval) l = mid; else r = mid; } llim = l; } { ll l = -INF, r = INF; while(l < r - 1){ ll mid = (l + r) / 2; if(fi(0, mid) == rval) r = mid; else l = mid; } rlim = r; } ll dif = fi(0, llim) - llim; rep(i, q){ ll ans; if(x[i] <= llim) ans = lval; else if(x[i] >= rlim) ans = rval; else ans = (ll)x[i] + dif; cout << ans << endl; } } }
/************************************************************************* > File Name: 1.cpp > Author: Knowledge_llz > Mail: [email protected] > Blog: https://blog.csdn.net/Pig_cfbsl > Created Time: 2020/10/11 14:09:05 ************************************************************************/ #include<bits/stdc++.h> #define For(i,a,b) for(register int i=(a);i<=(b);++i) #define pb push_back #define pr pair<int,int> #define fi first #define se second #define LL long long using namespace std; int read(){ char x=getchar(); int u=0,fg=0; while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); } while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); } return fg?-u:u; } const int mod=1e9+7; int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); freopen("output.out", "w", stdout); #endif int T=read(); while(T--){ LL n=read(),a=read(),b=read(),d=n-a-b; if(n<a+b){ puts("0"); continue; } d=(d+1)*(d+2)%mod; a=n-a+1; b=n-b+1; n=a*b%mod; LL ans=2*d*n%mod-d*d%mod; cout<<(ans+mod)%mod<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; //UnionFindテンプレ class UnionFind { // まとめる 判定 サイズを知る public: // Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。 vector<int> Parent; // 初期化 UnionFind(int N) { Parent = vector<int>(N, -1); } // Aのrootを調べる int root(int A) { if (Parent[A] < 0) return A; // マイナスならそれはroot return Parent[A] = root(Parent[A]); } // rootの値をプラスに戻して返す(サイズ) int size(int A) { return -Parent[root(A)]; } // くっつける関数 bool connect(int A, int B) { // AとBのroot同士をくっつける A = root(A); // ここのAは、"rootの場所"の意味 B = root(B); if (A == B) return false; // 既にくっついている if (size(A) < size(B)) swap(A, B); // 大きい方にくっつけるために中身交換 Parent[A] += Parent[B]; // 中身更新 Parent[B] = A; return true; } //連結か調べる関数 bool issame(int A, int B) { return root(A) == root(B); } }; //chmin,chmax関数(DP用) 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; } //aのZ/mZでの逆元 int64_t modinv(int64_t a, int64_t m) { int64_t b = m, u = 1, v = 0; while (b) { int64_t t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } //最大公約数 int64_t gcd(int64_t a, int64_t b) { if (a % b == 0) { return b; } return gcd(b, a%b); } //エラストテレスのふるい vector<bool> make_is_prime(int N) { vector<bool> prime(N + 1, true); if (N >= 0) prime[0] = false; if (N >= 1) prime[1] = false; for (int i = 2; i * i <= N; i++) { if (!prime[i]) continue; for (int j = i * i; j <= N; j += i) { prime[j] = false; } } return prime; } const int64_t mod = 998244353; //累乗a^b int64_t pow(int64_t a, int64_t b) { int64_t ans = (int64_t)1; for (int64_t i = 0; i < b; i++) { ans *= a; ans %= mod; } return ans; } //ここから int64_t pow2(int64_t a, int64_t b) { a%=mod; if (a == 0) return 0; else if (b == 0) return 1; else if (b % 2 == 0) { int64_t a1 = a*a; a%=mod; return pow2(a1,b/2); } else { int64_t a1 = a*a; a%=mod; return (a*pow2(a1,(b-1)/2))%mod; } } int main() { int64_t N,M,K; cin>>N>>M>>K; if (N == 1) { cout << pow2(K,M) << endl; return 0; } if (M == 1) { cout << pow2(K,N) << endl; return 0; } vector<int64_t> Ntimes(K+1); vector<int64_t> Mtimes(K+1); for (int64_t i = 0; i < K+1; i++) { Ntimes.at(i) = pow2(i,N); Mtimes.at(i) = pow2(i,M); } int64_t ans = 0; for (int64_t i = 0; i < K; i++) { int64_t pl = Ntimes.at(i+1)-Ntimes.at(i); pl%=mod; pl *= Mtimes.at(K-i); pl%mod; ans += pl; ans%=mod; } //int64_t x = pow(pow((int64_t)2,(int64_t)10),(int64_t)10); //cout << x << endl; if (ans < 0) ans += mod; cout << ans << endl; }
#include<bits/stdc++.h> #define title "title" #define ll long long #define ull unsigned ll #define fix(x) fixed<<setprecision(x) #define pii pair<int,int> #define vint vector<int> #define pb push_back #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define red(i,a,b) for(int i=(a);i>=(b);i--) #define Help freopen("a.cpp","r",stdin) #define db double #define ld long db using namespace std; void Freopen(){ freopen(title".in","r",stdin); freopen(title".out","w",stdout); } int read(){ int g=0,f=1; char ch=getchar(); while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();} return g*f; } const int mod=998244353; int n,m,k; int ksm(int x,int y){ int re=1; for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)re=1ll*re*x%mod; return re; } signed main(){ //Freopen(); #ifndef ONLINE_JUDGE Help; #endif n=read(),m=read(),k=read(); if(n==1||m==1){ int ans=0; rep(i,1,k)ans=(ans+1ll*(ksm(i,n)+mod-ksm(i-1,n))%mod*(ksm(k-i+1,m)-ksm(k-i,m)+mod)%mod)%mod; cout<<(ans%mod+mod)%mod; return signed(); } int ans=0; rep(i,1,k)ans=(ans+1ll*(ksm(i,n)+mod-ksm(i-1,n))%mod*ksm(k-i+1,m)%mod)%mod; cout<<(ans%mod+mod)%mod; return signed(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; //int:2*10**9 typedef long double ld; typedef pair<ll,ll> P; #define REP(i,n) for(ll i = 0; i<(ll)(n); i++) #define FOR(i,a,b) for(ll i=(a);i<=(b);i++) #define FORD(i,a,b) for(ll i=(a);i>=(b);i--) #define pb push_back #define MOD 1000000007 //998244353 #define PI 3.141592653 #define INF 100000000000000 //14 //cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); int main(){ ll a, b, w; cin >> a >> b >> w; w*=1000; ll ma = -1; ll mi = INF; FOR(i,1,1000100) { if (a*i<=w && b*i>=w) { ma = max(ma,i); mi = min(mi,i); } } if (ma==-1 || mi==INF) cout << "UNSATISFIABLE" << endl; else cout << mi << " " << ma << endl; }
#include <iostream> #include <string> #include <set> #include <map> #include <algorithm> #include <iomanip> #include <vector> #include <cmath> #include <queue> #include <sstream> #include <ctime> #include <iterator> #include <string.h> #include <stack> #include <unordered_set> #include <unordered_map> #include <bitset> #include <fstream> #include <assert.h> using namespace std; void solve() { int a, b, w; cin >> a >> b >> w; w *= 1000; int c = w / b; int d = w / a; int e = w - d * a; if (e > (b - a) * d) cout << "UNSATISFIABLE"; else cout << c + (c * b != w) << " " << d; } int main() { ios_base::sync_with_stdio(0); int t = 1; //cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define REP(i, a, n) for (int i = (a); i < (int)(n); i++) #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() using ll = long long; int main() { int A,B,C; cin >> A >> B >> C; bool c = (C%2 == 0 ? true : false); string ans; if( A == B ) ans = "="; else if( abs(A) == abs(B) && c) ans = "=" ; else if( A > B ) { if(abs(A) < abs(B) && c) ans = "<"; else ans = ">"; } else if( A < B ) { if(abs(A) > abs(B) && c) ans = ">"; else ans = "<"; } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <set> #include <sstream> #include <queue> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; ++i) #define reps(i, s, n) for (int i = s; i < n; i++) #define debug(s, param) std::cerr << s << param << std::endl; using namespace std; using ll = long long; using pi = pair<int, int>; using pl = pair<ll, ll>; // 参考にするコード // https://atcoder.jp/contests/abc199/submissions/22050932 const int INF = 1e9; const ll INFL = 1e18; const ll MOD = 1000000007; int main() { int n, m; cin >> n >> m; vector<vector<pi>> constraints(n+1); rep (i, m) { int x, y, z; cin >> x >> y >> z; constraints[x].emplace_back(pi(y, z)); } vector<int> popCount(1 << n); rep (i, 1 << n) popCount[i] = bitset<18>(i).count(); vector<ll> dp(1 << n, 0); dp[0] = 1; rep (S, 1 << n) { bool ok = true; for (auto [y, z] : constraints[popCount[S]]) { int mask = (1 << y) - 1; if (popCount[S & mask] > z) { ok = false; break; } } if (!ok) dp[S] = 0; rep (i, n) { if (S >> i & 1) continue; dp[S | 1 << i] += dp[S]; } } cout << dp[(1 << n) - 1] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define INF 1234567890 #define ll long long int N, M, sy, sx, sd, res = 0; string s[11]; int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int block[4][2] = {{-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; bool vis[11][11][4]; void f(int y, int x, int d) { if (vis[y][x][d]) return; vis[y][x][d] = true; int ny = y + dir[d][0], nx = x + dir[d][1]; int by = y + block[d][0], bx = x + block[d][1]; if (s[by][bx] == '#') { res++; f(by, bx, (d+3)%4); } else if (s[ny][nx] == '.') { res++; f(y, x, (d+1)%4); } else f(ny, nx, d); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> M; for(int i=0; i<N; i++) cin >> s[i]; for(int i=0; i<N; i++) for(int j=0; j<M; j++) if (s[i][j] == '#') { f(i, j, 0); goto END; } assert(0); END: cout << res; return 0; }
#include <iostream> #define rep(i,a,n) for(int i = a; i < (n); i++) using namespace std; int main(){ int h,w,cnt = 0; cin >> h >> w; char g[h][w]; rep(i,0,h) rep(j,0,w){ cin >> g[i][j]; //if(g[i][j] == '#') cout << "Ye" << endl; } rep(i,0,h-1) rep(j,0,w-1){ int v = (g[i][j] == '#') + (g[i+1][j] == '#') + (g[i][j+1] == '#') + (g[i+1][j+1] == '#'); //cout << v << endl; if(v == 1 || v == 3) cnt++; } cout << cnt << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <numeric> #include <utility> #include <tuple> #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << '\n'; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll s, p; cin, s, p; for (ll i = 1; i * i <= p; i++) { if (p % i == 0) { ll n = i, m = p / i; if (n + m == s) { print("Yes"); return 0; } } } print("No"); return 0; }
/** * Coded by : lucky_21 * --------Lokesh Singh **/ #include<bits/stdc++.h> using namespace std; #include<ext/pb_ds/tree_policy.hpp> #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; #define F first #define S second #define pb push_back #define lb lower_bound #define ub upper_bound #define vi vector<int> #define all(x) x.begin(),x.end() #define fix fixed<<setprecision(10) #define rep(i,a,b) for(int i=int(a);i<=int(b);i++) #define repb(i,b,a) for(int i=int(b);i>=int(a);i--) #define FastIO ios_base::sync_with_stdio(0),cin.tie(0) typedef double db; typedef long long ll; const int N=2e5+5; const int mod=1e9+7; void solve(){ ll n; cin>>n; int cnt=0; while(n%2==0) n/=2,cnt++; if(!cnt) cout<<"Odd\n"; else if(cnt==1) cout<<"Same\n"; else cout<<"Even\n"; } signed main(){ FastIO; int t; cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> #define rep(i,s,n) for (int i = s; i < (n); ++i) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; using Graph = vector<vector<int>>; using Matrix = vector<vector<int>>; const ll INF = LLONG_MAX; const ld EPS = 1e-14; const ll MOD = 1e9+7; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; template<class T> void chmax(T& a, T b) { if (a < b) a = b; } template<class T> void chmin(T& a, T b) { if (a > b) a = b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) { return a / gcd(a, b) * b;} int main() { int n, k; cin >> n >> k; Matrix a(n, vector<int>(n)); rep(i,0,n) rep(j,0,n) cin >> a[i][j]; int L = k*k/2+1; int wa = -1, ac = 1001001001; while(wa+1 < ac) { int wj = (wa+ac) / 2; bool ok = false; Matrix s(n+1, vector<int>(n+1)); rep(i,0,n) rep(j,0,n) s[i+1][j+1] = a[i][j] > wj ? 1 : 0; rep(i,0,n+1) rep(j,0,n) s[i][j+1] += s[i][j]; rep(i,0,n) rep(j,0,n+1) s[i+1][j] += s[i][j]; rep(i,0,n-k+1) rep(j,0,n-k+1) { int now = s[i+k][j+k]; now -= s[i][j+k]; now -= s[i+k][j]; now += s[i][j]; if (now < L) ok = true; } if (ok) ac = wj; else wa = wj; } cout << ac << endl; return 0; }
#include "bits/stdc++.h" using namespace std; // #define int long long constexpr int N = 801; int f[N][N]; int n, k; int bound; int a[N][N]; int get(int x1, int y1, int x2, int y2) { return f[x2][y2] - f[x1 - 1][y2] - f[x2][y1 - 1] + f[x1 - 1][y1 - 1]; } bool check(int t) { for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + (a[i][j] <= t); // cout << f[i][j] << " "; } // cout << endl; } // cerr << get(2, 2, 3, 3); for (int i = 1; i + k - 1 <= n; i ++) for (int j = 1; j + k - 1 <= n; j ++) { if (get(i, j, i + k - 1, j + k - 1) >= bound) return true; } return false; } void solution() { cin >> n >> k; for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) cin >> a[i][j]; bound = ((k * k + 1) / 2); // cerr << bound << endl; int l = 0, r = 1000000000, ans; while (l <= r) { int m = (l + r) >> 1; // cout << l << ' ' << r << ' ' << m << endl; if (check(m)) ans = m, r = m - 1; else l = m + 1; } // check(0); // for (int i = 0; i <= 10; i ++) cout << check(i) << endl; // assert(check(ans)); cout << ans; } 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"; } }
#include <bits/stdc++.h> using ull = unsigned long long int; using ll = long long; using ld = long double; using pii = std::pair<int,int>; using pll = std::pair<ll, ll>; using vi = std::vector<int> ; using vvi = std::vector<vi> ; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double> ; using vvd = std::vector<vd> ; using qi = std::queue<int> ; using vpii = std::vector<std::pair<int, int> >; using vpll = std::vector<pll>; using namespace std; #define rep(i,j) for(int (i)=0;(i)<(j);(i)++) #define drep(i,j) for(int (i)=(j);(i) >= 0;(i)--) template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;} template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;} template<class T> inline void pri(T a){cout << a << endl;} template<class Z> using vec = vector<Z>; template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; inline void IN(void){ return; } template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; } inline void OUT(void){ cout << endl; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest){ cout << first << " " ; OUT(rest...); return; } const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; const int INF = int(1e9); const long long int INFL = LLONG_MAX; int n,m,k; string S; int ans; vll A; ll f(ll mod,ll n,ll x) { ll dp[n+1][mod+1][mod];// memset(dp,-1,sizeof(dp)); dp[0][0][0] = 0; rep(i,n) { rep(j,mod+1) { rep(k,mod) { if(dp[i][j][k] < 0)continue; chmax(dp[i+1][j][k],dp[i][j][k]); if(j<mod)chmax(dp[i+1][j+1][(k+A[i])%mod],dp[i][j][k] + A[i]); } } } x = x%mod; return dp[n][mod][x]; } void solve() { ll N; ll X; IN(N,X); A.resize(N); rep(i,N)IN(A[i]); ll ans = INFL; for(ll i=1;i<=N;i++) { ll tmp = f(i,N,X); //OUT(tmp); if(tmp>0)chmin(ans,(X - tmp)/i); } cout << ans << endl; } signed main (int argc, char* argv[]) { cin.tie(0); ios::sync_with_stdio(false); int cases=1; //IN(cases); while(cases--)solve(); //pri(ans); //for(auto c : ans){cout << c << endl;} //cout << fixed << setprecision(15) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<vl> vvl; double EPS=1e-9; int INF=1000000005; 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 }; const ll MOD = 1000000007; // ll sum() { return 0; } // template<typename T, typename... Args> // T sum(T a, Args... args) { return a + sum(args...); } #define DEBUG fprintf(stderr, "====TESTING====\n") #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #define OUT(x) cout << x << endl #define OUTH(x) cout << x << " " #define debug(...) fprintf(stderr, __VA_ARGS__) #define READ(x) for(auto &(z):x) cin >> z; #define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a)) #define EACH(a, b) for (auto&(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define MAX(a, b) a=max(a, b) #define MIN(a, b) a=min(a, b) #define SQR(x) ((ll)(x) * (x)) #define RESET(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define TC(t) while (t--) #define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL) #define what_is(x) cerr << #x << " is " << x << endl void solve() { ll n, x; cin >> n >> x; vl A(n); READ(A); ll ans = INFF; for(ll k = 1; k <= n; k++) { vvl dp(k + 1, vl(k, -INFF)); dp[0][0] = 0; for(ll a : A) { for(ll i = k; i--;) { for(ll j = 0; j < k; j++) { if(dp[i][j] != -INFF) { ll x = a + dp[i][j]; dp[i + 1][x % k] = max<ll>(dp[i + 1][x % k], x); } } } } if(dp[k][x % k] != -INFF) { ans = min<ll>(ans, (x - dp[k][x % k]) / k); } } OUT(ans); } int main() { FAST_INP; // #ifndef ONLINE_JUDGE // freopen("input.txt","r", stdin); // freopen("output.txt","w", stdout); // #endif // int tc; cin >> tc; // TC(tc) solve(); solve(); return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> //#include<boost/multiprecision/cpp_int.hpp> //typedef boost::multiprecision::cpp_int mulint; #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 //#define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template < typename T > ostream &operator<<(ostream &os, const vector< T > &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector< vector< int > > &A) { int N = A.size(); for (int i = 0; i < N; i++) { for (int j = 0; j < A[i].size(); j++) os << A[i][j] << " "; os << endl; } return os; } template < typename T, typename U > ostream &operator<<(ostream &os, const pair< T, U > &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template < typename T, typename U > istream &operator>>(istream &is, pair< T, U > &p) { is >> p.first >> p.second; return is; } template < typename T > istream &operator>>(istream &is, vector< T > &A) { rep(i, A.size()) is >> A[i]; return is; } template < typename... Args > auto print(Args const &... args) { for (auto const &e : {args...}) cerr << e << " "; cerr << endl; } typedef pair< int, int > pii; typedef long long ll; struct edge { int from, to, d, c, i; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0, int _i = 0) { from = _from; to = _to; d = _d; c = _c; i = _i; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector< edge > edges; typedef vector< edges > graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector< vector< flow > > flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template < typename T, typename U > bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template < typename T, typename U > bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } template < typename A, std::size_t N, typename T > void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } //expicit argument required with multi-precision template < typename T > vector< int > itod(T n) { vector< int > ret; while (n > 0) { ret.push_back((int)(n % 10)); n /= 10; } return ret; } template < typename T > T dtoi(const vector< int > &d) { T ret = 0; for (int i = d.size() - 1; i >= 0; i--) { ret *= 10; ret += d[i]; } return ret; } struct initializer { initializer() { cout << fixed << setprecision(20); ios::sync_with_stdio(false); std::cin.tie(nullptr); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> N; string S; cin >> S; string T; rep(i, N) { T.push_back(S[i]); while (T.size() >= 3 && T[T.size() - 3] == 'f' && T[T.size() - 2] == 'o' && T[T.size() - 1] == 'x') { rep(_, 3) T.pop_back(); } } cout << T.size() << endl; return 0; }
//@Author: KeinYukiyoshi // clang-format off #include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC target("avx") #define int long long using namespace std; #define stoi stoll #define fi first #define se second #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++) #define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++) #define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--) #define FOR(i, a) for (auto &i: a) #define ALL(obj) begin(obj), end(obj) #define _max(x) *max_element(ALL(x)) #define _min(x) *min_element(ALL(x)) #define _sum(x) accumulate(ALL(x), 0LL) const int MOD = 1000000007; // const int MOD = 998244353; // const int INF = (int)1e18; // const int INF = 10000000000007; // 1e13 + 7 // const int INF = LLONG_MAX; // 9.2e18 const double EPS = 1e-8; const double PI = 3.14159265358979; template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T> using VVV = vector<vector<vector<T>>>; template <class T, class S> using P = pair<T, S>; template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return true;}return false;} template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return true;}return false;} int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } template<class T> T chmod(T &a, T mod=MOD) {a = a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;}; int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));} int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;} struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // clang-format on class AGodSequence { public: static void solve(istream &cin, ostream &cout) { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int A, B; cin >> A >> B; V<int> E(A + B); if (A >= B){ rep(i, A) E[i] = i + 1; rep(i, B - 1) E[i + A] = -(i + 1); int sum = _sum(E); E[A + B - 1] = -sum; }else{ rep(i, B) E[i] = -(i + 1); rep(i, A - 1) E[i + B] = (i + 1); int sum = _sum(E); E[A + B - 1] = -sum; } FOR(i, E) cout << i << " "; cout << endl; } }; signed main() { AGodSequence solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' #define vec vector<ll> #define mat vector<vector<ll> > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; //typedef long double ld; typedef complex<double> Complex; const ll INF=1e9+7; const ll MOD=998244353; const ll inf=INF*INF; const ll mod=MOD; const ll MAX=20000010; signed main(){ cin.tie(0); ios::sync_with_stdio(false); ll n;cin>>n; vector<ll>a(n),t(n); rep(i,n)cin>>a[i]>>t[i]; ll su=inf,ma=inf; ll de=-inf,mi=-inf; ll now=0; rep(i,n){ if(t[i]==1){ now+=a[i]; ma+=a[i]; mi+=a[i]; } if(t[i]==2){ if(mi>a[i])continue; if(ma<=a[i]){ de=inf; su=-inf; ma=a[i],mi=a[i]; continue; } de=a[i]-now; mi=a[i]; } if(t[i]==3){ if(ma<a[i])continue; if(mi>=a[i]){ de=inf; su=-inf; ma=a[i],mi=a[i]; continue; } su=a[i]-now; ma=a[i]; } //cout<<de<<' '<<su<<' '<<mi<<' '<<ma<<endl; } ll q;cin>>q; rep(i,q){ ll x;cin>>x; if(su<=de){ cout<<ma<<endl; }else{ if(x>=su){ cout<<ma<<endl; }else if(x<=de){ cout<<mi<<endl; }else{ cout<<x+now<<endl; } } } }
#include <bits/stdc++.h> #include <fstream> #define FIXED_FLOAT(x) std::fixed <<std::setprecision(3)<<(x) #define all(v) (v).begin(), ( v).end() using namespace std; #define forn(i, b) for (int i=0; i<b; ++i) using ll = long long; const int mod = (ll)1e9 + 7; #define PI acos(-1) typedef pair<int , int> pairs; typedef complex<ll> G; const int INF = 1e9 + 1 ; const int N = 1e5 + 2; using TP = tuple<int, int, int>; int power(ll a,ll b){ if(!b) return 1; int c=power(a,b/2); c=(1LL*c*c)%mod; if(b%2) c=(1LL*c*a)%mod; return c; } ll mul(ll A, ll B) { return (A*B)%mod; } struct Fenwick { vector<ll>ss; int n; void init(int n) { this->n = n; ss.resize(n + 1); for(int i = 0;i <= n;++i) { ss[i] = 0; } } ll get_val(int pos) { return ss[pos]; } void add(int pos, ll x) { while(pos <= n) { ss[pos] += x; pos += (pos & -pos); } } void range_add(int l, int r, int val) { add(l, (ll)val); add(r + 1, -val); } ll sum(int pos) { ll ans = 0; while(pos >= 1) { ans += ss[pos]; pos -= (pos & -pos); } return ans; } int point_query(int idx) { int ret = 0; for (idx; idx > 0; idx -= idx & -idx) ret += ss[idx]; return ret; } }; void done() { string s; cin >> s; int n = s.size(); int ans = 0; for(int i = 0;i < 1 << n;++i) { int p = 0; int tot = 0; for(int ad = 0; ad < n;++ad) { if(i >> ad & 1) { ++p; tot += s[ad] - '0'; } } if(tot % 3 == 0)ans = max(ans, p); } cout << (ans == 0 ? -1 : n - ans) << '\n'; } struct Edge { int u, v, weight; bool operator<(Edge const &other) { return weight < other.weight; } }; void solve() { int n; cin >> n; vector<ll>a(n); for(int i = 0;i < n;++i) cin >> a[i]; } void another() { ll n; cin >> n; int sz = to_string(n).size(); int tot = 0; map<int, int>m; while(n) { m[(n%10)%3]++; tot += n % 10; n /= 10; } if(tot % 3 == 0) { cout << 0 << '\n'; return; } int rem = tot % 3; if(sz == 1) { cout << -1 << '\n'; return; } if(m[rem]) { cout << 1 << '\n'; return; } if(rem == 2 && m[1] >= 2 && sz > 2) { cout << 2 << '\n'; return; } cout << -1 << '\n'; } void test_case() { int t; cin >> t; while(t--)another(); } int main() { ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); done(); }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define fi first #define se second #define pb push_back #define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k)) #define forn(i,a,b) for(int i = a; i < b; i++) #define forr(i,a,b) for(int i = a; i >= b; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int,int> ii; typedef vector<int> vi; typedef vector<ii> vii; const ll mod = 1e9+7; ll mul(ll a,ll b){return ((a%mod)*(b%mod))%mod;} ll sum(ll a,ll b){return ((a%mod)+(b%mod))%mod;} ll sub(ll a,ll b){return ((a%mod)-(b%mod))%mod;} ll power(ll a,ll b){ ll res = 1; while(b){ if(b&1)res = mul(res,a); b >>= 1; a = mul(a,a); } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector<ll> A(N),sum(N),maxi(N); forn(i,0,N)cin >> A[i]; forn(i,0,N){ sum[i] = A[i]; if(i)sum[i] += sum[i-1]; maxi[i] = sum[i]; if(i)maxi[i] = max(maxi[i],maxi[i-1]); } debug(sum); debug(maxi); ll res = 0; ll pos = 0; forn(i,0,N){ res = max(res,pos+maxi[i]); debug(i,pos,sum[i],maxi[i]); pos += sum[i]; } cout << res << '\n'; return 0; } /* __builtin_mul_overflow(x,y,&x) -fsplit-stack */
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define ff first #define ss second int main(void){ // Your code here! ll i,ans,n; cin >> n; ll a[n],b[n]; for(i=0;i<n;i++) { cin >> a[i]; b[i]=a[i]; } for(i=1;i<n;i++) { a[i]+=a[i-1]; b[i]+=b[i-1]; } for(i=1;i<n;i++) { a[i]=max(a[i],a[i-1]); b[i]+=b[i-1]; } ans=0; ans=max(ans,a[0]); ans=max(ans,b[n-1]); for(i=0;i<(n-1);i++) ans=max(ans,b[i]+a[i+1]); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i=0; i<n; i++) using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; const int INF = 1001001001; #define pcnt __builtin_popcount map<int, int> mp; vector<int> A; int b, c; int main(){ int N; cin >> N; A = vector<int>(N); rep(i,N){ cin >> A[i]; } int N2 = 1 << min(N, 8); ll sum = 0; rep(s, N2) { sum = 0; rep(i, min(N,8)) if (s >> i & 1) { sum += A[i]; } if (mp[sum%200]) { b = mp[sum%200]; c = s; break; } else { mp[sum%200] = s; } } if (b == 0) { cout << "NO" << endl; } else { cout << "YES" << endl; int x = pcnt(b); int y = pcnt(c); cout << x << " "; rep(i, min(N,8)) if (b >> i & 1) { cout << i+1 << " "; } cout << endl; cout << y << " "; rep(i, min(N,8)) if (c >> i & 1) { cout << i+1 << " "; } } return 0; }
#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 popcount(v) __builtin_popcount(v) #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(){ INT(n); vector<ll> a(n); in(a); vector<vector<int>> dp(200); int to; for(int i=0;i<n;i++){ for(int j=0;j<200;j++){ if(dp[j].size()==0)continue; if(dp[j][dp[j].size()-1]==i+1)continue; to=(j+a[i])%200; if(dp[j].size()>=1){ if(dp[to].size()>=1){ out("Yes"); out(dp[(j+a[i])%200].size(),dp[(j+a[i])%200]); out(dp[j].size()+1,dp[j],i+1); return 0; }else{ dp[to]=dp[j]; dp[to].push_back(i+1); } } } if(dp[a[i]%200].size()>=1){ out("Yes"); out(dp[a[i]%200].size(),dp[a[i]%200]); out(1,i+1); return 0; }else{ dp[a[i]%200].push_back(i+1); } } out("No"); }
#include<bits/stdc++.h> using namespace std; int n,m; int edge[1001][1001]; bool visited[1001][1001]; vector<int> vc[1001][26]; struct PT{ int st,ed; int size; }; int main(){ int x,y,sz; char c; cin>>n>>m; for(int i=0;i<m;i++){ cin>>x>>y>>c; edge[x][y]=edge[y][x]=1; vc[x][c-'a'].push_back(y); vc[y][c-'a'].push_back(x); } bool flag=false; queue<PT> q; PT temp,in; temp.st=1; temp.ed=n; temp.size=0; visited[temp.st][temp.ed]=true; q.push(temp); int ans=1e9; while(!q.empty()){ temp=q.front(); q.pop(); x=temp.st; y=temp.ed; sz=temp.size; if(edge[x][y]>0||x==y){ if(x==y) ans=min(ans,sz); else ans=min(ans,sz+1); } for(int t=0;t<26;t++){ for(auto i:vc[x][t]){ for(auto j:vc[y][t]){ if(visited[i][j]) continue; visited[i][j]=true; temp.st=i; temp.ed=j; temp.size=sz+2; q.push(temp); } } } } if(ans==1e9) cout<<"-1\n"; else cout<<ans<<'\n'; }
#include <bits/stdc++.h> using namespace std; #define la1ee ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int main() { la1ee int n; cin>>n; bool f = false; long long int a[n]{}, b[n]{}, c[n]{}; for(int i=0; i<n; ++i) { cin>>a[i]>>b[i]>>c[i]; } long long int ans = 1e11; //cout<<ans; for (int i = 0; i < n; ++i) { if( (c[i] - a[i]) > 0 ) { ans = min(ans,b[i]); f = true; } } if(f) cout<<ans; else cout<<"-1"; return 0; }
#include <bits/stdc++.h> #define ll long long int #define w(t) int t; cin>>t; while(t--) #define F first #define S second #define pb push_back #define mp make_pair #define pii pair<int,int> #define mii map<int,int> #define sp(x,y) fixed<<setprecision(y)<<x using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int m,h; cin>>m>>h; if(h%m==0)cout<<"Yes"; else cout<<"No"; return 0; }
#include <iostream> using namespace std; int main() { int M,H; cin>>M>>H; if(H%M==0) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N=101000; #define x first #define y second struct node{ int val; string ch; }arr[13456]; bool cmp(node a,node b) { return a.val>b.val; } long long dp[105][105]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i].ch>>arr[i].val; sort(arr+1,arr+1+n,cmp); cout<<arr[2].ch<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int x; cin>>x; int ans = 100-x%100; cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define endl '\n' #define all(x) (x).begin(),(x).end() #define trace(...) debug(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void debug(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void debug(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " | "; debug(comma + 1, args...); } #define mp(x,y) make_pair(x,y) const int INF=1000000000+5; const int N=2e5+5; const int M=3e5+5; const ll oo=1e18+5; const ll mod=998244353; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; vector<string> ans; cin>>n; ans.push_back("AB"); for(int i=1;i<n;++i){ vector<string> tmp; for(string s:ans){ tmp.push_back(s+s); string newS=s; for(char ch:s){ if(ch=='A') newS+='B'; else newS+='A'; } tmp.push_back(newS); } string def; for(int j=0;j<(1<<i);++j) def+='A'; for(int j=0;j<(1<<i);++j) def+='B'; tmp.push_back(def); ans=tmp; } cout<<(int)ans.size()<<endl; for(string x:ans) cout<<x<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define each(e, v) for(auto &e: v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; int main(){ int N; cin >> N; if(N == 1){ cout << "1\n"; cout << "AB\n"; return 0; } vector<vector<int>> now(1, vector<int>(1, 1)); rep(i, N){ int n = sz(now); vector<vector<int>> tmp(2*n, vector<int>(2*n)); rep(i, n){ rep(j, n){ tmp[i][j] = now[i][j]; tmp[n+i][j] = now[i][j]; tmp[i][n+j] = now[i][j]; tmp[n+i][n+j] = -now[i][j]; } } swap(now, tmp); } cout << (1<<N)-1 << '\n'; rep2(i, 1, sz(now)-1){ each(e, now[i]){ cout << (e == 1? 'A' : 'B'); } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<double> vd; typedef vector<string> vs; typedef vector<char> vc; typedef pair<int,int> pii; #define ALL(x) x.begin(),x.end() #define REP(i,n) for(ll i=0;i<ll(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,n) for(ll i=a;i<n;i++) #define FORD(i,a,n) for(ll i=n-1;i>=a;i--) #define SIZE(x) ll(x.size()) #define F first #define S second #define __lcm(a,b) (ll(a)/__gcd(ll(a),ll(b)))*ll(b) int main() { int n; cin >> n; vi x(n); REP(i,n) cin >> x[i]; vi p = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; ll ans = pow(2,60); for (int i=0;i<(1<<15);i++) { vi v(n,0); ll c = 0; ll a = 1; for (int j=0;j<15;j++) { if (i & 1<<j) { a *= p[j]; REP(k,n) { if(x[k]%p[j]==0) { v[k]++; if (v[k]==1) c++; } } } } if (c==n) { if (ans>a) ans = a; } } cout << ans; }
#include<iostream> #include<vector> #include<chrono> #define int long long #define fi first #define se second using namespace std; //const int MAXN = 100005; const int MOD = 1e9 + 7; //const int INF = 1e18; struct custom_hash { static uint64_t splitmix64(uint64_t x) { 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); } }; signed main() { cin.tie(0); cout.tie(0); ios_base :: sync_with_stdio(0); int n; cin >> n; int t[n]; for (int i = 0; i < n; i++) { cin >> t[i]; } int p[n + 1]; p[0] = 1; for (int i = 1; i <= n; i++) { p[i] = p[i - 1] * 2 % MOD; } int dp[n][2]; dp[0][0] = t[0]; dp[0][1] = 0; int a = 1, b = 0; for (int i = 1; i < n; i++) { int c = (a + b) % MOD; b = a; a = c; dp[i][0] = (3 * MOD + dp[i - 1][0] + dp[i - 1][1] + t[i] * a % MOD) % MOD; dp[i][1] = (2 * MOD + dp[i - 1][0] - t[i] * b % MOD) % MOD; } cout << (dp[n - 1][0] + dp[n - 1][1]) % MOD; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,b) for(int i=a;i<b;i++) void solve(){ string s; cin >> s; int countl = 0 ,countu = 0; rep(i,0,s.size()){ if((i+1) % 2 == 0){ if(isupper(s[i]))countu++; } else{ if(islower(s[i]))countl++; } } if(countl + countu == s.size()){ cout << "Yes\n"; } else{ cout << "No\n"; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); solve(); }
#include<bits/stdc++.h> #define ll long long #define ff first #define ss second #define pb push_back using namespace std; const int N = 4e5+5, mod = 3; int n; ll A[N],f[N],g[N]; ll answer = 0; int C(int a,int b){ if(f[b] > f[a] + f[b-a]){ return 0; } int x = 4*g[b]/g[a]/g[b-a]; if(x == 1) return 4; if(x == 2) return 2; if(x == 4) return 1; if(x == 8) return 2; } inline void test_case(){ cin >> n; for(int i = 1; i <= n; i++){ char c; cin >> c; if(c == 'B') A[i] = 0; if(c == 'W') A[i] = 1; if(c == 'R') A[i] = 2; } f[0] = 0; g[0] = 1; for(int i = 1; i <= n; i++){ int cnt = 0; ll y = g[i-1]*i; while(y%3 == 0){ y /= 3; cnt++; } y %= 3; f[i] = f[i-1] + cnt; g[i] = y; //cout << f[i] << " " << g[i] << endl; } for(int i = 1; i <= n; i++){ //cout << C(i-1,n-1) << endl; //cout << A[i] << endl; answer += A[i]*C(i-1,n-1); answer %= 3; } //cout << answer << endl; if(n%2 == 0) answer = (mod - answer)%mod; if(answer == 0) cout << 'B' << endl; if(answer == 1) cout << 'W' << endl; if(answer == 2) cout << 'R' << endl; } main(){ ios::sync_with_stdio(0); int T = 1; //cin >> T; while(T--){ test_case(); } }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int sum = 2*n; int i=1; while(1){ if(i*(i+1) >= sum){ break; } i++; } cout<<i; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define fastIO ios_base::sync_with_stdio(false); cin.tie(0); #define mod 1000000007 ll power(ll x, ll n) { ll ans = 1; while(n>0) { if(n&1){ ans = (ans*x)%mod; n = n-1; } else { x = (x*x)%mod; n = n/2; } } return ans; } int main() { fastIO; ll n; cin>>n; ll sum = 0; int i = 1; while(sum < n) { sum += i; i++; } cout<<i-1<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define range(a) a.begin(), a.end() #define endl "\n" #define Yes() cout << "Yes" << endl #define No() cout << "No" << endl #define MP make_pair const unsigned long long mod = 1e9 + 7; const long long INF = 1LL<<60; const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; void chmin(long long &a, long long b) { if (a > b) a = b; } void chmax(long long &a, long long b) { if (a < b) a = b; } int main(void){ ios::sync_with_stdio(0); cin.tie(0); ll N, K; cin >> N >> K; ll A[300010] = {}; FOR(i,0,N){ ll b; cin >> b; if(A[b]<K){ A[b]++; } } ll now = K; ll sum = 0; FOR(i,0,N){ ll c = A[i]; if(c == 0){ sum += (now - c) * i; break; } if(now<=c){ continue; } sum += (now - c) * i; now = min(now, c); } cout << sum << endl; return 0; }
#include <vector> #include <stdio.h> using namespace std; #include <bits/stdc++.h> #define rep(i,n) for(int i = 0;i<(n);++i) using ll = long long; using p = pair<int,int>; int n; vector<vector<int>> to; vector<int> used,col; vector<int> vs; void dfs(int v)//引数は今見ている頂点 { if(used[v])//頂点に訪れていたならreturn { return; } used[v] = 1;//これがないと無限ループになる vs.push_back(v); for(int u : to[v])//vに隣接する頂点に対して { dfs(u); } } ll now; void dfs2(int i) { if(i == (int)vs.size()) { now++; return; } int v = vs[i];//今見ている頂点 rep(c,3)//色を0,1,2で表す { col[v] = c;//vをc色に塗る bool ok = true; for(int u : to[v]) { if(col[u] == c) { ok = false; } } if(!ok) { continue; } dfs2(i+1); } col[v] = -1; } int main() { int m; cin >> n >> m; to = vector<vector<int>>(n); rep(i,m) { int a,b; cin >> a >> b; --a; --b; to[a].push_back(b); to[b].push_back(a); } ll ans = 1; used = vector<int>(n); /*dfsの判定に使う*/ col = vector<int>(n,-1);/*塗り方を試すために使う*/ rep(i,n) { if(used[i]) { continue; } ans *= 3; vs = vector<int>();//dfsの順番で頂点を入れていく dfs(i); col[vs[0]] = 0; now = 0; dfs2(1);//色塗りを探索 ans *= now; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll n,m; cin>>n>>m; vector<ll>a(n),b(m); map<ll,ll>mp; for(auto &it : a) { cin>>it; mp[it]++; } for(auto &it : b) { cin>>it; mp[it]++; } for(auto i : mp) { if(i.second ==1) cout<<i.first<<" "; } cout<<endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ull unsigned long long #define ld long double #define F first #define S second #define eb emplace_back #define pb push_back #define mp make_pair #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) using namespace __gnu_pbds; using namespace std; template <typename T> using oset = tree <pair <T, T>, null_type, less <pair <T, T>>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> void ckmin(T& a, const T b) {a = min(a, b);} template <typename T> void ckmax(T& a, const T b) {a = max(a, b);} template <typename T> void print(vector <T> v) { for (T i : v) cout << i << " "; cout << '\n'; } template <typename T> void print(vector <vector <T>>& v) { for (vector <T>& vv : v) { for (T& i : vv) cout << i << " "; cout << '\n'; } } template <typename T> void read(vector <T>& v) {for (T& i : v) cin >> i;} template <typename T> void read(T&& t) {cin >> t;} template <typename T, typename... Args> void read(T&& t, Args&&... args) { cin >> t; read(forward<Args>(args)...); } template <typename T> void print(T&& t) {cout << t << '\n';} template <typename T, typename... Args> void print(T&& t, Args&&... args) { cout << t << " "; print(forward<Args>(args)...); } void usaco(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(name.size()) { freopen((name+".in").c_str(), "r", stdin); freopen((name+".txt").c_str(), "w", stdout); } } void solve() { ll a, b; cin >> a >> b; print(2*a+100-b); } int32_t main() { usaco(); int t = 1; while (t--) solve(); }
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template <typename T> struct coord_comp { vector<T> v; bool sorted = false; coord_comp() {} int size() { return v.size(); } void add(T x) { v.push_back(x); } void build() { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); sorted = true; } int get_idx(T x) { assert(sorted); return lower_bound(v.begin(), v.end(), x) - v.begin(); } }; constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 200010; int main() { int n, m; scanf("%d%d", &n, &m); vector<int> G[n][26]; bool E[n][n]; rep(i, n) rep(j, n) E[i][j] = false; rep(i, m) { int a, b; char c; scanf("%d%d %c", &a, &b, &c); --a; --b; G[a][c - 'a'].push_back(b); G[b][c - 'a'].push_back(a); E[a][b] = E[b][a] = true; } rep(i, n) rep(c, 26) { sort(G[i][c].begin(), G[i][c].end()); G[i][c].erase(unique(G[i][c].begin(), G[i][c].end()), G[i][c].end()); } int d[n][n]; rep(i, n) rep(j, n) d[i][j] = mod; d[0][n - 1] = 0; queue<pii> que; que.emplace(0, n - 1); while (!que.empty()) { auto [u, v] = que.front(); if (E[u][v]) { printf("%d\n", d[u][v] + 1); return 0; } que.pop(); rep(c, 26) for (auto nu : G[u][c]) for (auto nv : G[v][c]) { if (chmin(d[nu][nv], d[u][v] + 2)) { if (nu == nv) { printf("%d\n", d[nu][nv]); return 0; } que.emplace(nu, nv); } } } puts("-1"); }
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <cstdio> #include <utility> #include <algorithm> #include <vector> #include <queue> using namespace std; using ll = long long int; int n, m; vector<int> g[1001][30]; int d[1001][1001]; bool g2[1001][1001]; int res; constexpr int INF = 987654321; int main(void) { #ifdef CUBE_PS freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for(int i = 0; i < m; ++i) { int a, b; char c; cin >> a >> b >> c; g[a][c - 'a'].push_back(b); g[b][c - 'a'].push_back(a); g2[a][b] = true; g2[b][a] = true; } for(int i = 0; i <= n; ++i) { for(int j = 0; j <= n; ++j) { d[i][j] = INF; } } res = INF; queue<pair<int, int>> q; q.push({ 1, n }); d[1][n] = 0; while(q.empty() == false) { int s = q.front().first; int e = q.front().second; q.pop(); if(s == e) { res = min(res, d[s][e]); } else if(g2[s][e] == true) { res = min(res, d[s][e] + 1); } for(int i = 0; i <= 'z' - 'a'; ++i) { for(int ns : g[s][i]) { for(int ne : g[e][i]) { if(d[ns][ne] > d[s][e] + 2) { d[ns][ne] = d[s][e] + 2; q.push({ ns, ne }); } } } } } if(res == INF) res = -1; cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define pb push_back #define ll long long #define ld long double #define sz(x) (ll)x.size() #define vll vector<long long> #define infi INT_MAX #define infl LONG_LONG_MAX #define infd LDBL_MAX #define f(i,a,b) for(ll i=a;i<b;i++) #define fi(i,a,b) for(ll i=(b-1);i>=a;i--) #define F first #define S second #define G(a,b) get<a>(b) #define MP make_pair #define MT make_tuple #define pll pair<ll,ll> #define endl "\n" #define ALL(v) v.begin(),v.end() #define nl cout<<"\n"; #define pr(x) cout<<x; #define pr1(x) cout<<x<<" "; #define pr2(x,y) cout<<x<<" "<<y; #define pr3(x,y,z) cout<<x<<" "<<y<<" "<<z; #define pr4(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w; #define deb cout<<"***************************************************************************************\n"; #define moshi cout<<"moshi moshi ! \n"; #define hi cout<<"hi ! \n"; #define bye cout<<"bye bye ! \n"; #define kawai cout<<"O kawai koto ! \n"; #define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define number_of_set_bits __builtin_popcountll #define eb emplace_back const ld PI=3.1415926535897932384626433; auto clk=clock(); mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 <<"\n"; //use cerr if u want to display at the bottom } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif //********************************************************** Code begins *********************************************** const ll N=2e5+5; ll n,m,a[N],dp[N]; vll adj[N]; void solve() { cin>>n>>m; f(i,1,n+1) cin>>a[i]; f(i,1,m+1) { ll x,y; cin>>x>>y; adj[x].pb(y); } ll ans=-infl; fi(i,1,n+1) { dp[i]=-infl; for(auto &j:adj[i]) { dp[i]=max(dp[i],max(a[j],dp[j])); } if(dp[i]!=-infl) ans=max(ans,dp[i]-a[i]); } if(ans!=-infl) cout<<ans<<"\n"; else cout<<"0\n"; } int main() { ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(8); ll test_cases=1; //cin>>test_cases; f(i,1,test_cases+1) { solve(); //clr(); } #ifdef rd cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl; #endif return 0; }
// #pragma GCC optimise ("O1") #include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,j,n) for(ll i=j;i<n;i++) #define _rep(i,n) for(ll i=n-1;i>=1;i--) #define scn(a) scanf("%lld",&a) #define scns(a,b) scanf("%lld %lld",&a,&b) #define print(a) printf("%lld",a) #define vec vector<ll> #define pb push_back #define pairs pair<ll,ll> #define f first #define s second #define all(v) v.begin(),v.end() #define srt(v) sort(v.begin(),v.end()) #define read(a) for(auto &it:a) cin>>it; #define write(a) for(auto it:a) cout<<it<<" "; #define yeS(GOOD) GOOD ? cout<<"YES\n" : cout<<"NO\n" #define mem(a,b) memset(a,b,sizeof(a)) #define nl printf("\n") #define inf 1e9 #define zer INT_MIN #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); const ll N=3e5+5; int n,a[N]; int tree[4*N]; void build(int st,int se,int node) { if(st==se) { tree[node] = a[st]; return; } int mid=(st+se)/2; build(st,mid,2*node); build(mid+1,se,2*node+1); tree[node] = tree[2*node]^tree[2*node+1]; } void update(int st,int se,int ind,int val,int node) { if(st == se) { tree[node]^= val; a[ind]^= val; return; } int mid = st+(se-st)/2; if(ind <= mid) update(st,mid,ind,val,2*node); else update(mid+1,se,ind,val,2*node+1); tree[node] = tree[2*node]^tree[2*node+1]; } int get(int st,int se,int qs,int qe,int node) { if(st>qe || se<qs) return 0; if(st>=qs && se<=qe) return tree[node]; int mid=(st+se)/2; return get(st,mid,qs,qe,2*node)^get(mid+1,se,qs,qe,2*node+1); } void f() { // saafsafai(); ll q; cin>>n>>q; rep(i,0,n) cin>>a[i]; build(0,n-1,1); ll ans=0; while(q--) { int type,x,y; cin>>type>>x>>y; if(type == 1) { update(0,n-1,x-1,y,1); } else { int ans = get(0,n-1,x-1,y-1,1); cout<<ans<<endl; } } // cout<<solve(1,n); } int main() { int queries=1; // cin>>queries; while(queries--) { f(); } }
#include<bits/stdc++.h> #define ll long long int #define ld long double 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 INT_MAXI 9000000000000000000 #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 }; ll n,m; ll arr[200005]; ll arr2[200005]; bool visited[200005]; ll ans=0,maxi=0; vector<ll>vec[200005]; vi vec2,ans2; /*void dfs(ll node,ll mini) { vec2.pb(node); ans=max(ans,arr[node]-mini); for(auto itr:vec[node]) { if(!visited[itr]) { visited[itr]=1; maxi=max(maxi,arr[itr]); dfs(itr,min(mini,arr[itr])); } else { ans=max(ans,arr2[itr]-mini); } } }*/ map<ll,ll>mpp; ll func(ll x,ll y) { if(x>=y) return x-y; if(mpp.count(y)) return mpp[y]; ll count=INT_MAXI; count=abs(x-y); if(y%2==0) { count= min(count,1+func(x,y/2)); } else { count=min(count,min(func(x,y/2),func(x,y/2+1))+2); } return mpp[y]= count; } int main() { fio; ll t,i,j,u,v,x,k,p,c,y,a,b; cin>>x>>y; ll ans=func(x,y); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; /*---------------------DEBUGGING--------------------------------------------*/ 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 pb push_back #define ll long long #define pii pair<int,int> #define pcc pair<char,char> #define F first #define S second #define int long long #define pi 3.141592653589793238462643383279502 #define M 1000000007 #define rep(i,a,n) for(int i=a;i<n;i++) #define INF 10000000000005 #define N 200005 #define vi vector<int> #define all(v) v.begin(),v.end() #define endl "\n" unordered_map<int,int>dp; unordered_map<int,int>vis; int x,y; int calc(int val) { if(val<=x)return (x-val); if(vis.count(val))return dp[val]; vis[val]=1; int ans=abs(val-x); if(val & 1) { ans=min(ans,2 + calc((val+1)/2)); ans=min(ans,2 + calc((val-1)/2)); } else { ans=min(ans,1 + calc(val/2)); } return dp[val]=ans; } void solve() { cin>>x>>y; if(x>=y) { cout<<(x-y)<<endl; return; } cout<<calc(y)<<endl; } signed main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int tests; //cin>>tests; tests=1; for(int i=0;i<tests;i++) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using ll = long long; const long long INF = 1LL << 60; const int SINF = 1LL << 30; const ll mod = 1000000000+7; const ll MOD = 998244353; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; 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; } ll c[61][61]; int main() { int a, b; ll k; cin >> a >> b >> k; c[0][0] = 1; for(int i=1; i<=60; i++) { for(int j=0; j<=60; j++) { if(j-1 >= 0) c[i][j] += c[i-1][j-1]; c[i][j] += c[i-1][j]; } } string ans = ""; while(a + b > 0) { if(b > 0 && k > c[a+b-1][b]) { ans.push_back('b'); k -= c[a+b-1][b]; b--; } else if(a > 0) { ans.push_back('a'); a--; } else { ans.push_back('b'); b--; } } cout << ans << endl; }
/* ॐ JAI SHREE RAM Hare Krishna Hare Krishna Krishna Krishna Hare Hare Hare Rama Hare Rama Rama Rama Hare Hare ॐ */ //Written by Bhuwanesh Nainwal #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; #define fast ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL) #define int long long int #define vci vector<int> #define vcvci vector<vector<int>> #define vcpi vector<pair<int,int>> #define vcs vector<string> #define pb push_back #define mpii map<int , int> #define mpsi map<string , int> #define mpci map<char , int> #define umpii unordered_map<int , int> #define umpsi unordered_map<string , int> #define umpci unordered_map<char , int> #define all(x) auto it = x.begin() ; it != x.end() ; it++ #define vcc vector<char> #define vcs vector<string> #define sti set<int> #define stc set<char> #define sts set<string> #define pqmx priority_queue<int> #define pqmn priority_queue<int , vi , greater<int>> #define null NULL #define ff first #define ss second #define stb(x) __builtin_popcount(x) #define lzr(x) __builtin_clz(x) #define tzr(x) __builtin_ctz(x) #define prc(x , y) fixed << setprecision(y) << x #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) using namespace std; const int mod = 1e9 + 7; const int INF = 1e9; const int LINF = 1e18; const int N = 1e4; const int chkprc = 1e-9; // Check for precision error in double void solve() { int a , b , k; cin >> a >> b >> k; int dp[62][62]; dp[0][0] = 1; for(int i = 1 ; i <= 60 ; i++) { dp[i][0] = 1; for(int j = 1 ; j < i ; j++) { dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; } dp[i][i] = 1; } int n = a + b; for(int i = 0 ; i < n ; i++) { int cnt = dp[a + b - 1] [b]; if(cnt >= k) { cout << 'a'; a--; } else { cout << 'b'; b--; k -= cnt; } } } void local() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE } int32_t main() { fast; //local(); int T = 1; // cin>>T; while(T--) { solve(); } return 0; }
// E - Traveler #include <bits/stdc++.h> using namespace std; using ll = int64_t; #define rep(i,e) for(int i=0;i<(e);++i) int main(){ ll INF = 1e9 + 1; int n; cin>>n; vector<pair<ll,ll>> A(n+1); rep(i, n) A[i] = {INF, -INF}; rep(i, n){ ll x, c; cin>>x>>c; --c; auto[mn, mx] = A[c]; A[c] = {min(mn, x), max(mx, x)}; } ll pre_mn, pre_mx; int pre = -1; rep(now, n+1){ auto[mn, mx] = A[now]; if(mn == INF) continue; ll p_mn = 0, p_mx = 0; if(pre < 0) pre_mn = pre_mx = 0; else tie(p_mn, p_mx) = A[pre]; ll a = pre_mn + abs(p_mn - mn); ll b = pre_mn + abs(p_mn - mx); ll c = pre_mx + abs(p_mx - mn); ll d = pre_mx + abs(p_mx - mx); ll e = mx - mn; pre_mn = min(b, d) + e; pre_mx = min(a, c) + e; pre = now; } cout<< pre_mn <<endl; }
//AnkitCode99 here.... //every ups and downs matter! #include<bits/stdc++.h> #define endl "\n" #define IOS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr) typedef long long int ll; #define rep(i,a,b) for(ll i=a;i<b;i++) #define pb push_back using namespace std; const ll sz=(1e5+5)*2; const ll szz=1e6+6; const ll mod=1e9+7; ll C[sz][2],dp[sz][2]; ll ss; ll solve(ll pos,ll flag) { if(pos==0) { return 0; } ll &ans = dp[pos][flag]; if(ans!=-1) return ans; ans = abs(C[pos][0]-C[pos][1]); ans+=min(solve(pos-1,0)+abs(C[pos-1][0] - C[pos][flag^1]), solve(pos-1,1)+abs(C[pos-1][1] - C[pos][flag^1])); return ans; } int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("op.txt","w",stdout); #endif clock_t startTime=clock(); ll n; cin>>n; ll ar[n]; map<ll,ll> l,r; rep(i,0,n) { ll x,c; cin>>x>>c; if(l.count(c)) { l[c]=min(l[c],x); r[c]=max(r[c],x); } else { l[c]=r[c]=x; } } ss=0; for(auto x:l) { C[++ss][0]=x.second; C[ss][1]=r[x.first]; } memset(dp,-1,sizeof dp); cout<<solve(ss+1,0); cerr << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl; }//Goodbye...
#include<bits/stdc++.h> using namespace std; char s[1000005]; int f[1000005],mn[205]; int main() { int n; scanf("%d%s",&n,s+1); memset(f,0x3f,sizeof(f)); memset(mn,0x3f,sizeof(mn)); f[0]=0,mn[s[1]-'a']=0; for(int i=1;i<=n;i++) { for(int j=0;j<=25;j++) if(j!=s[i]-'a')f[i]=min(f[i],mn[j]+1); if(i<n)mn[s[i+1]-'a']=min(mn[s[i+1]-'a'],f[i]); } if(f[n]<=n)printf("%d\n",f[n]); else puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; long long i,j,p,l,t; long long c[234567],a[213167],b[211167],d,e[234567],tt=0,k=0,y,z,n=0,x; char m; string q,w,s; int main(){ cin >>t>>s; if(s[0]!=s[s.size()-1]){ cout<<1; return 0; } k=1; for(i=1; i<s.size()-1; i++){ k++; if(s[i]!=s[0]&&s[i+1]!=s[s.size()-1]){ cout<<k; return 0; } } cout<<-1; }
using namespace std; #include <bits/stdc++.h> #define frr(i, r, l) for(int i = r; i >= l; --i) #define fr(i, l, r) for(int i = l; i < r; ++i) #define forit(it, type, var) for(type::iterator it = var.begin(); it != var.end(); it++) #define ii pair<int, int> #define iii pair<int, ii> #define MEMS(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define m_p make_pair #define pb push_back #define ll long long #define ld long double typedef pair<ll, ll> iPair; priority_queue< ll, vector <ll> , greater<ll>>pq1,pq2; #define maxN 1000000000000000000 ll power(ll a,ll b){ll res=1;while(b>0){if(b%2!=0)res=res*a;b/=2; a=a*a;}return res;} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n,cnt=0,ans=0,tot=0,x,y; vector<ll>v; cin>>n; fr(i,0,n) { cin>>x>>y; tot+=x; v.pb(y+2*x); } sort(v.begin(),v.end()); frr(i,v.size()-1,0) { ans+=v[i]; cnt++; if(ans>tot) { cout<<cnt<<endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define clr(a) memset(a, 0, sizeof(a)) #define full(a) memset(a, 0x3f, sizeof(a)) #define fornext(x, i) for(int i = head[x], y = ver[i]; i; i = nxt[i], y = ver[i]) #define mset(a, b) memset(a, b, sizeof(a)) #define Rep(i, a, b) for(int i = (a); i <= (b); ++i) #define dRep(i, a, b) for(int i = (a); i >= (b); --i) #define IOset(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout); #define endl '\n' #define int ll #define un unsigned #define ll long long #define db double #define rept cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl; #define dbg cout<<"c " template<typename _T> inline void rd(_T &_d) { int _f; char _c; _f=_d=0; while(_c=getchar(),!isdigit(_c)) if (_c=='-') _f=1; while(isdigit(_c)) _d=_d*10+_c-'0',_c=getchar(); if (_f) _d=-_d; } template <typename _T,typename ...Args> inline void rd(_T &_d,Args &..._a) { rd(_d); rd(_a...); } #define mod(x) (x=(x>=M?x-M:x)) const int N=2e5+5,M=998244353; int n,k,c[305][305],a[N],p[N][305],s[305]; signed main() { rd(n,k); Rep(i,1,n) rd(a[i]); Rep(i,0,k) c[i][0]=1; Rep(i,1,k) Rep(j,1,k) c[i][j]=c[i-1][j]+c[i-1][j-1],mod(c[i][j]); int tmp; Rep(i,1,n) { tmp=1; Rep(j,1,k) tmp=tmp*a[i]%M,s[j]+=tmp,mod(s[j]),p[i][j]=tmp; } Rep(i,1,n) p[i][0]=1; s[0]=n; int ans,inv=499122177; Rep(i,1,k) { ans=0; Rep(j,0,i) { ans=(ans+c[i][j]*s[j]%M*s[i-j]%M+M-s[i]*c[i][j]%M)%M; } cout<<ans*inv%M<<endl; } return 0; }
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<set> #include<map> #include<iostream> #define fi first #define se second #define mk make_pair #define pb push_back #define ls (t<<1) #define rs ((t<<1)|1) #define mid ((l+r)>>1) #define N 300005 using namespace std; int A[N], B[N], C[N], n, tree[N]; multiset<int> st, num[N]; int lowbit(int x) { return x & -x; } int ask(int x) { int s = 0; for ( ; x; x -= lowbit(x)) s += tree[x]; return s; } void insert(int x) { for ( ; x <= n; x += lowbit(x)) tree[x] += 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &A[i]); A[i] += i; st.insert(A[i]); C[++C[0]] = A[i]; } for (int i = 1; i <= n; ++i) { scanf("%d", &B[i]); B[i] += i; if (st.find(B[i]) == st.end()) { puts("-1"); return 0; } st.erase(st.find(B[i])); } sort(C + 1, C + C[0] + 1); C[0] = unique(C + 1, C + C[0] + 1) - (C + 1); for (int i = 1; i <= n; ++i) { A[i] = lower_bound(C + 1, C + C[0] + 1, A[i]) - C; num[A[i]].insert(i); B[i] = lower_bound(C + 1, C + C[0] + 1, B[i]) - C; } long long ans = 0; for (int i = 1; i <= n; ++i) { int pos = *num[B[i]].begin(); num[B[i]].erase(pos); int realpos = pos + (i - 1) - ask(pos); insert(pos); ans += realpos - i; } printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p_ll; template<class T> void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; } #define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++) #define all(vec) vec.begin(), vec.end() #define rep(i,N) repr(i,0,N) #define per(i,N) for (ll i=(ll)N-1; i>=0; i--) #define popcount __builtin_popcount const ll LLINF = pow(2,61)-1; const ll INF = pow(2,30)-1; ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); } ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } struct num { ll x, i; bool operator < (const num &n) const { return x<n.x||(x==n.x&&i<n.i); }; }; // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- struct SegTree { ll size; vector<ll> pos; SegTree(ll N) { size = 1; while(size<N) size<<=1; pos.resize(2*size,0); } ll operator[](const ll &x) const { return pos[x+size]; } void set(ll x, const ll v) { pos[x+size] = v; } void update(ll x, const ll v) { set(x,v); x+=size; while (x>>=1) operate(x); } ll query(ll a, ll b) { ll L = 0, R = 0; for (a+=size, b+=size; a<b; a>>=1, b>>=1) { if (a&1) { L = q(L,pos[a]); a++; } if (b&1) { b--; R = q(pos[b],R); } } return q(L,R); } void operate(ll i) { pos[i] = q(pos[i*2], pos[i*2+1]); } ll q(ll x, ll y) { return x + y; } }; // ---------------------------------------------------------------------- // ---------------------------------------------------------------------- int main() { ll N; cin >> N; num A[N]; rep(i,N) { ll x; cin >> x; A[i] = {x+i,i}; } num B[N]; rep(i,N) { ll x; cin >> x; B[i] = {x+i,i}; } sort(A,A+N); sort(B,B+N); rep(i,N) { if (A[i].x!=B[i].x) { cout << -1 << endl; return 0; } } // rep(i,N) cout << A[i].i << " "; cout << endl; // rep(i,N) cout << B[i].i << " "; cout << endl; ll to[N] = {}; rep(i,N) to[A[i].i] = B[i].i; // debug(to,to+N); ll result = 0; SegTree st(N); rep(i,N) { result += st.query(to[i],N); st.update(to[i],1); } cout << result << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (n); i++) using namespace std; using P = pair<int, int>; using ll = long long; int main(){ int n; cin >> n; vector<string> s(n); rep(i, n) cin >> s[i]; unordered_set<string> h(s.begin(), s.end()); for(string t : s) if(h.count('!' + t)){ cout << t << endl; return 0; } cout << "satisfiable" << endl; }
//BISMILLAH #include <bits/stdc++.h> using namespace std; #define read(x) freopen(x, "r", stdin); #define write(x) freopen(x, "w", stdout); #define ll long long #define lli long long int #define mod 1000000007 #define mx 1000005 // #define sazzad int main() { #ifdef sazzad read("input.txt"); // write("output.txt"); #endif int a,n,flag=0,sum=0,check,min1= INT_MAX,min2 = INT_MAX,summin= INT_MAX; cin>>n; a = n; vector<int>v; vector<int>vv; while(a--) { int x , y; cin>>x>>y; v.push_back(x); vv.push_back(y); sum = x+y; summin = min(summin,sum); } for(int i=0;i<v.size();i++) { if( v[i] < min1 ) { min1 = v[i]; flag = i; } } //cout<< flag<<endl; for(int i=0; i<vv.size();i++ ) { if( i != flag ) min2 = min(min2,vv[i]); } // cout<<min2<<endl; int res = max(min1,min2); cout<< min(summin,res)<<endl; }
#include<iostream> #include<vector> using namespace std; int main(){ string S; string T=""; cin >> S; for(int i=S.size()-1; i>=0; i--){ T += S[i]; } for(int i=0;i<T.size();i++){ if(T[i]=='6'){ T[i] = '9'; }else if(T[i] == '9'){ T[i] = '6'; } } cout << T << endl; }
#include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long #define ld long double #define mp make_pair #define pb push_back #define fo(i,n) for(ll i=0;i<n;i++) #define fo1(i,n) for(ll i=1;i<=n;i++) #define loop(i,a,b)for(ll i=a;i<=b;i++) #define loopr(i,a,b)for(ll i=b;i>=a;i--) #define vll vector<ll> #define vvl vector<vll> #define pii pair<ll,ll> #define F first #define S second #define M 1000000007 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; return b;} ll mod=998244353; ll po(ll x, ll y){ if(y==0) return 1; if(y%2){ return ((x)*(po(x,y/2)%M)*(po(x,y/2)%M) + M)%M; } return ((po(x,y/2)%M)*(po(x,y/2)%M) + M)%M; } void seive(bool prime[]){ //bool prime[100002]; for (ll p=2; p*p<=1000001; p++) { if (prime[p]) { for (ll i=p*p; i<=1000001; i+=p) prime[i] = false; } } } ll powermod(ll x,ll y,ll p) { ll res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; // y = y/2 x = (x*x) % p; } return res; } ll gcd(ll x, ll y){ if (y == 0) return x; else return gcd(y, x % y); } ll nck(ll n, ll k){ ll C[k + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (ll i = 1; i <= n; i++) { for (ll j = min(i, k); j > 0; j--) C[j] = (C[j]%M + C[j - 1]%M)%M; } return C[k]%M; } /*void call(vll &primefactor,int N){ for(int i=1;i<N;i++) { primefactor[i]=i; } for(int i=2;i<N;i++) { if(primefactor[i]!=i) continue; for(int j=i;j<N;j+=i) primefactor[j]=i; } }*/ int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t=1; //cin>>t; while(t--){ string s; cin>>s; ll n=s.size(); fo(i,n) if(s[i]=='9') s[i]='6'; else if(s[i]=='6') s[i]='9'; reverse(s.begin(),s.end()); cout<<s<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define maxn 200010 int mod; vector<int> getCycle(int val) { vector<int> v; int now = val % 10; while (v.empty() or v.front() != now) { v.push_back(now); now *= val; now %= 10; } return v; } int pw(int a, int b, int m) { if (b == 0) return 1; if (b & 1)return a * pw(a, b - 1, m) % m; return pw(a * a % m, b / 2, m); } void solve() { int a, b, c; cin >> a >> b >> c; vector<int> prt = getCycle(a); int ans = pw(b, c, prt.size()) - 1; if(ans < 0)ans += prt.size(); cout << prt[ans] << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); 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 int INF=1e9; const int MX=1e5+5; const int MD=1e9+7; const int MDL=99824453; auto time0 = curtime; ll A,B,C,phi=4; ll bin_power(ll a,ll b,ll md){ ll res=1; while(b){ if (b&1) res=(res*a)%md; a=(a*a)%md; b>>=1; } return res; } void solve(){ cin>>A>>B>>C; A%=10; if (A==0){ cout<<"0"; } else if(A==1){ cout<<"1"; } else if (A==2){ ll x=bin_power(B,C,4); ll m[4]={6,2,4,8}; cout<<m[x]; } else if (A==3){ ll x=bin_power(B,C,4); ll m[4]={1,3,9,7}; cout<<m[x]; } else if (A==4){ ll x=bin_power(B,C,2); ll m[2]={6,4}; cout<<m[x]; } else if(A==5){ cout<<"5"; } else if (A==6){ cout<<6; } else if (A==7){ ll x=bin_power(B,C,4); ll m[4]={1,7,9,3}; cout<<m[x]; } else if (A==8){ ll x=bin_power(B,C,4); ll m[4]={6,8,4,2}; cout<<m[x]; } else if(A==9){ ll x=bin_power(B,C,2); ll m[2]={1,9}; cout<<m[x]; } } 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; using ll = long long; //macros #define fi first #define se second #define pb push_back #define ReadInputFile() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #define FAST_IO() ios_base::sync_with_stdio(false);cin.tie(nullptr); const int MAX = 1e9+1; const long long int Pinf = INT_MAX, Ninf = INT_MIN; const long int mod = 1e9+7; //debug template 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 int main(){ ll n; cin >> n; vector<pair<ll, ll>> arr(n); for(ll i = 0; i < n; i++){ int x, y; cin >> x >> y; arr[i] = {x,y}; } debug(arr); ll sum = 0; for(auto i : arr){ int a = i.fi, b = i.se; ll d = b-a+1; sum += (d*b) - (d*(d-1))/2; } cout << sum << endl; }
#include<bits/stdc++.h> #define repr(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) for(int i=0;i<n;i++) #define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b) #define reprev(i,n) reprrev(i,0,n) 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 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 = 1e9+7; void chmod(ll &M){ if(M >= mod) M %= mod; else if(M < 0){ M += (abs(M)/mod + 1)*mod; M %= mod; } } ll modpow(ll x, ll n){ if(n==0) return 1; ll res=modpow(x, n/2); if(n%2==0) return res*res%mod; else return res*res%mod*x%mod; } int getl(int i, int N) { return i==0? N-1:i-1; }; int getr(int i, int N) { return i==N-1? 0:i+1; }; long long GCD(long long a, long long b) { if (b == 0) return a; else return GCD(b, a % b); } using namespace std; int main() { int N; cin >> N; ll sum = 0; rep(i, N) { ll A, B; cin >> A >> B; sum += ((A+B)*(B-A+1))/2; } cout << sum << endl; }
// Problem: F - Sugoroku2 // Contest: AtCoder - AtCoder Beginner Contest 189 // URL: https://atcoder.jp/contests/abc189/tasks/abc189_f // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef tuple<int,int> tii; typedef tuple<ll,ll> tll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<tii> vtii; typedef vector<tll> vtll; typedef vector<vector<int>> vvi; #define INF 0x3f3f3f3f #define X(t) get<0>(t) #define Y(t) get<1>(t) #define Z(t) get<2>(t) #define pb push_back #define dlt decltype #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__) #define prt(...) printf(__VA_ARGS__) #ifdef LOCAL constexpr const bool local=true; #else constexpr const bool local=false; #endif template <typename T> void _debug(const char* format,const T t) { if (local) cerr << format << '=' << t << endl; } template <class First, class... Rest> void _debug(const char* format,const First first,const Rest... rest) { if (local){ while (*format != ',') cerr << *format++; cerr << '=' << first << ","; _debug(format + 1, rest...); } } template <typename T> ostream& operator<<(ostream& os, const vector<T>& V) { if (local) os << "["; for (const auto& vv : V) os << vv << " "; if (local) os << "]"; return os; } template<class Tuple,size_t N> struct TuplePrinter { static void print(ostream& os,const Tuple& t) { TuplePrinter<Tuple, N-1>::print(os,t); os << " " << get<N-1>(t); } }; template<class Tuple> struct TuplePrinter<Tuple,1>{ static void print(ostream& os,const Tuple& t) { os << get<0>(t); } }; template<typename... Rest> ostream& operator<<(ostream& os, const tuple<Rest...>& V) { if (local) os << "("; TuplePrinter<decltype(V),sizeof...(Rest)>::print(os,V); if (local) os << ")"; return os; } template<class T1,class T2> bool cmin(T1 &a,const T2 b){return a>b?a=b,1:0;} template<class T1,class T2> bool cmax(T1 &a,const T2 b){return a<b?a=b,1:0;} template<class T1,class T2,class ...T3> bool cmin(T1 &a,const T2 b,const T3 ...rest){return cmin(a,b)|cmin(a,rest...);} template<class T1,class T2,class ...T3> bool cmax(T1 &a,const T2 b,const T3 ...rest){return cmax(a,b)|cmax(a,rest...);} ll _gcd(ll a,ll b){return b==0?a:_gcd(b,a%b);} ll gcd(ll a,ll b){if (a<b) swap(a,b);return _gcd(a,b);} ll gi(){ ll i=0;bool f=false;char c; while (!isdigit(c=cin.get())&&cin.good()) f=c=='-'; if (!cin.good()) return 0; do i=(i<<3)+(i<<1)+c-'0'; while (isdigit(c=cin.get())); return f?-i:i; } struct lnr{ double a,b; lnr():a(0),b(0){} lnr(double b):a(0),b(b){} lnr(double a,double b):a(a),b(b){} lnr operator*(double x){return {a*x,b*x};} lnr& operator*=(double x){a*=x;b*=x;return *this;} lnr operator/(double x){return {a/x,b/x};} lnr& operator/=(double x){a/=x;b/=x;return *this;} lnr operator+(lnr x){return {a+x.a,b+x.b};} lnr& operator+=(lnr x){a+=x.a;b+=x.b;return *this;} lnr& operator-=(lnr x){a-=x.a;b-=x.b;return *this;} lnr operator()(const lnr &l){ return {a*l.a,a*l.b+b}; } double operator()(const double &l){ return a*l+b; } }; int n,m,k; bitset<100010> b; lnr f[100010]; struct solution{ solution(){ b.reset(); memset(f,0,sizeof(f)); n=gi();m=gi();k=gi(); for (int i=1;i<=k;i++) b[gi()]=1; } void solve(){ lnr sumf=0; for (int i=n-1;i>=0;i--){ if (b[i]) f[i]=lnr(1,0); else f[i]=sumf/m+1; //debug(i,f[i].a,f[i].b); sumf+=f[i]; if (i+m<=n) sumf-=f[i+m]; } //x==f[0].a*x+f[0].b if (fabs(f[0].a-1)<1e-6){ cout << -1 << endl; }else{ printf("%.4lf",f[0].b/(1-f[0].a)); } } }; int main(){ ios::sync_with_stdio(false); int T=1; while (T--&&cin.good()){ solution().solve(); } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define debug(x) {cerr<<#x<<" = "<<(x)<<endl;} int main() { ios::sync_with_stdio(false); ll b,c; cin>>b>>c; if(c==0) {cout<<1; return 0;} if(c==1) {cout<<2-(b==0); return 0;} ll d=c/2, e=(c-1)/2; if(abs(b)>c/2) { if(c%2) cout<<2*(d+e)+1; else cout<<3*d+e; } else if(abs(b)<=c/2) { if(b>0) if(c%2) cout<<2*d+2*b; else cout<<2*b+2*e+1; else if(c%2) cout<<2*(-b)+2*e+1; else cout<<2*(-b)+2*d; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, d; long long m; char s[1005]; int debug() { int res = 0; bool gg = 0; for (int base = d + 1; ; base++) { long long num = 0LL; for (int i = 0; i < n; i++) { if (num > (m - (s[i] - '0')) / base) { gg = 1; break; } num = num * base + (s[i] - '0'); } if (gg) break; res++; } return res; } int main() { scanf("%s", s); scanf("%lld", &m); n = strlen(s); for (int i = 0; i < n; i++) { d = max(d, s[i] - '0'); } if (n == 1) { if (s[0] - '0' <= m) printf("1\n"); else printf("0\n"); } else if (n == 2) { printf("%lld\n", max(0LL, (m - (s[1] - '0')) / (s[0] - '0') - d)); } else if (n == 3) { double a = (s[0] - '0'); double b = (s[1] - '0'); double c = (s[2] - '0') - m; double delta = b * b - 4.0 * a * c; if (delta < 0) return 0 * printf("0\n"); double rt1 = (-b + sqrt(delta)) / (2.0 * a); long long lim = (long long)rt1; printf("%lld\n", max(0LL, lim - d)); } else { int res = 0; bool gg = 0; for (int base = d + 1; ; base++) { long long num = 0LL; for (int i = 0; i < n; i++) { if (num > (m - (s[i] - '0')) / base) { gg = 1; break; } num = num * base + (s[i] - '0'); } if (gg) break; res++; } printf("%d\n", res); } }
#include <bits/stdc++.h> using namespace std; int main() { string x; cin>>x; long long m; cin>>m; vector<long long> x1,x2; bool flag=true; for (int i = 0; i < x.length(); i++) x1.push_back(x[i]-'0'); long long n=*max_element(x1.begin(),x1.end()); n++; long long m1=m; while (m1>0) {x2.push_back(m1%n); m1/=n;} reverse(x2.begin(),x2.end()); if (x1.size()>x2.size()) {cout<<0<<endl; return 0;} else if (x1.size()==x2.size()) { for (int i = 0; i < x2.size(); i++) { if (x1[i]>x2[i]) {cout<<0<<endl; return 0;} else if (x1[i]<x2[i]) break; } } if (x.size()==1) {cout<<1<<endl; return 0;} while (flag) { vector<long long> x3; n*=2; m1=m; flag=true; while (m1>0) {x3.push_back(m1%n); m1/=n;} reverse(x3.begin(),x3.end()); if (x1.size()>x3.size()) flag=false; else if (x1.size()==x3.size()) { for (int i = 0; i < x3.size(); i++) { if (x1[i]>x3[i]) flag=false; else if (x1[i]<x3[i]) break; } } } long long n1=n/2; while (n-n1>1) { long long n2=(n+n1)/2; m1=m; vector<long long> x3; while (m1>0) {x3.push_back(m1%n2); m1/=n2;} reverse(x3.begin(),x3.end()); if (x1.size()>x3.size()) n=n2; else if (x1.size()==x3.size()) { flag=true; for (int i = 0; i < x3.size(); i++) { if (x1[i]>x3[i]) {n=n2; flag=false;} else if (x1[i]<x3[i]) break; } if (flag) n1=n2; } else n1=n2; } long long ans=n1-*max_element(x1.begin(),x1.end()); cout<<ans<<endl; }
#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include<bits/stdc++.h> using namespace std; int n; double A[100010]; double f(double x) { double res=0; rep(i,n){ res+=x+A[i]-min(A[i],2.0*x); } return res; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cin>>n; rep(i,n) cin>>A[i]; double low = 0.0; double high = 10000000000; int cnt = 500; while (cnt--) { double c1 = (low * 2 + high) / 3; double c2 = (low + high * 2) / 3; if (f(c1) > f(c2)) low = c1; else high = c2; } cout<<fixed<<setprecision(10)<<f(low)/n<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using int128 = __int128; constexpr int DEBUG = 0; int main() { ios::sync_with_stdio(false); cin.tie(0); int64 x, y, a, b; cin >> x >> y >> a >> b; int64 z = 0; while (true) { if (static_cast<int128>(x) * a >= x + b) break; if (static_cast<int128>(x) * a >= y) break; x = x * a; z++; // cout << x << " " << z << endl; } z += (y - x - 1) / b; cout << z << endl; }
#include <iostream> using namespace std; int main(){ string s; cin >> s; if(s[0] == s[1] && s[1] == s[2]) cout << "Won\n"; else cout << "Lost\n"; }
#include <bits/stdc++.h> #define debug(var) do{std::cout << #var << " :";std::cout << std::endl;view(var);}while(0) template<typename T> void view(T e){ std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::pair<T, U>& v){ std::cout << v.first << ", " << v.second << std::endl; } template<typename T> void view(const std::set<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::map<T, U>& v){ std::cout << "{" << std::endl; for(const std::pair<T, U>& e : v){ std::cout << e.first << ", " << e.second << std::endl; } std::cout << "}" << std::endl; } template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cout << "{" << std::endl; for(const auto& v : vv){ view(v); } std::cout << "}" << std::endl; } using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long 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 long long INF = 1LL << 60; const long long MOD = 1000000007; const double PI = acos(-1); int main() { string s; cin >> s; bool won = true; for (int i = 1; i < s.size(); ++i) { if (s[i - 1] != s[i]) won = false; } cout << (won ? "Won" : "Lost") << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int h,w; cin>>h>>w; vector<string> field(h); for(int i=0;i<h;i++) cin>>field[i]; vector<vector<int>> dp(h,vector<int>(w,2e9)); dp[h-1][w-1]=0; for(int i=h-1;i>=0;i--){ for(int j=w-1;j>=0;j--){ if(0<=i-1){ if((i+1+j)%2){ int op=0; if(field[i][j]=='+')op++; else op--; if(dp[i-1][j]!=2e9)dp[i-1][j]=max(dp[i][j]+op,dp[i-1][j]); else dp[i-1][j]=dp[i][j]+op; } else{ int op=0; if(field[i][j]=='+')op--; else op++; if(dp[i-1][j]!=2e9)dp[i-1][j]=min(dp[i][j]+op,dp[i-1][j]); else dp[i-1][j]=dp[i][j]+op; } } if(0<=j-1){ if((j+1+i)%2){ int op=0; if(field[i][j]=='+')op++; else op--; if(dp[i][j-1]!=2e9)dp[i][j-1]=max(dp[i][j]+op,dp[i][j-1]); else dp[i][j-1]=dp[i][j]+op; } else{ int op=0; if(field[i][j]=='+')op--; else op++; if(dp[i][j-1]!=2e9)dp[i][j-1]=min(dp[i][j]+op,dp[i][j-1]); else dp[i][j-1]=dp[i][j]+op; } } } } //for(int i=0;i<h;i++){ if(dp[0][0]<0){ cout<<"Takahashi"<<endl; } else if(dp[0][0]==0){ cout<<"Draw"<<endl; } else cout<<"Aoki"<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int,int>> vpi; #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) #define trav(a,x) for (auto& a: x) #define pb push_back #define rsz resize #define all(x) begin(x), end(x) #define pi pair<int, int> #define mp make_pair #define F first #define S second int h, w; string s; vector<vi> a; vector<vi> dp; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> h >> w; a.rsz(h, vi(w, -1)); dp.rsz(h, vi(w, 0)); F0R(i, h) { cin >> s; F0R(j, w) if (s[j]=='+') a[i][j]=1; } R0F(i, h) R0F(j, w) { if (i==h-1 && j==w-1) continue; dp[i][j]=INT_MIN; if (i<h-1) dp[i][j]=max(dp[i][j], a[i+1][j]-dp[i+1][j]); if (j<w-1) dp[i][j]=max(dp[i][j], a[i][j+1]-dp[i][j+1]); } if (dp[0][0]>0) cout << "Takahashi\n"; else if (dp[0][0]==0) cout << "Draw\n"; else cout << "Aoki\n"; }
#include <bits/stdc++.h> using namespace std; // ******************* SNIPPET TEMPLATE - STARTS*************** #define range(t) for (int i = 0; i < t; i++) #define rep(i, t, q) for (int i = t; i < q; i++) #define pb(t) push_back(t) #define rangeG(i, t) for (i = 0; i < t; i++) #define printVec(vec) \ for (auto c : vec) \ { \ cout << c << endl; \ } typedef vector<int> vi; typedef vector<string> vs; typedef long long ll; template <typename first> void print(first f) { cout << f << endl; } template <typename first, typename... args> void print(first f, args... a) { cout << f << " "; print(a...); } int inp(string s = "") { cout << s; int t = 0; cin >> t; return t; } // X--------------X---------X SNIPPET TEMPLATE - ENDS* X--------------X---------X void solve() { } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; set<int> a, b; range(n) { int temp; cin >> temp; a.insert(temp); } range(m) { int temp2; cin >> temp2; b.insert(temp2); } //sort(a.begin(), a.end()); //sort(b.begin(), b.end()); set<int> result; for (int ax : a) { if (count(b.begin(), b.end(), ax) == 0) { result.insert(ax); } } for (int bx : b) { if (count(a.begin(), a.end(), bx) == 0) { result.insert(bx); } } //sort(result.begin(), result.end()); for (int ans : result) { cout << ans << " "; } cout << "\n"; return 0; }
#include <iostream> #include <fstream> #include <algorithm> #include <vector> #include <set> #include <stack> #include <numeric> #include <chrono> #include <random> #include <bitset> #include <tuple> #include <queue> #include <map> #include <unordered_map> #include <cstring> #include <cassert> #include <climits> #include <complex> #include <math.h> using namespace std; const int N = 2e5 + 10, mod = 1e9 + 7; int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; for(int cn = 1; cn <= tc; cn++) { int n, m; cin >> n >> m; vector<int> res; vector<bool> a(N, 0), b(N, 0); for(int i = 0; i < n; i++) { int x; scanf("%d", &x); a[x] = 1; } for(int i = 0; i < m; i++) { int x; scanf("%d", &x); b[x] = 1; } for(int i = 1; i <= 1e3; i++) { if(a[i] ^ b[i]) cout << i << ' '; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std ; #define int long long int32_t main(){ int n , m ; cin >> n >> m ; int a[n][m] ; int mn = 101 ; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < m ; j++){ cin >> a[i][j] ; mn = min(mn , a[i][j]) ; } } int ans = 0 ; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < m ; j++){ ans += a[i][j] - mn ; } } cout << ans ; return 0 ; }
// Created at 2020/12/19 21:01 // {TODO}WA, {TODO}min #include <bits/stdc++.h> //#include <atcoder/all> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i, n) for (lint i = 0, i##_len = n; i < i##_len; i++) #define rep2(i, n) for (lint i = 1, i##_len = n; i <= i##_len; i++) #define rep3(i, s, e) for (lint i = s, i##_len = e; i < i##_len; i++) #define all(i) (i).begin(), (i).end() #define print(s) cout << s << enl; #define mk_p make_pair using namespace std; //using namespace atcoder; using lint = long long; using pi = pair<int, int>; using pl = pair<lint, lint>; using vi = vector<int>; using vl = vector<lint>; using vvi = vector<vi>; using vvl = vector<vl>; using qi = queue<int>; using ql = queue<lint>; const char enl = '\n'; const int INF = INT_MAX; const lint INFl = LLONG_MAX; template<class T> inline void input(vector<T> &v) { for (T &x : v) cin >> x; } template<class T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } 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 (b < a) { a = b; return true; } return false; } // 素数とその個数の pair の vector template<class T> vector<pair<T, T>> prime_factorize(T N) { vector<pair<T, T>> res; // 2 だけ別 lint ex = 0; // 指数 // 割れる限り割り続ける while (N % 2 == 0) { ++ex; N /= 2; } // その結果を push if (ex != 0) res.push_back({2, ex}); for (lint a = 3; a * a <= N; a += 2) { lint ex = 0; // 指数 // 割れる限り割り続ける while (N % a == 0) { ++ex; N /= a; } // その結果を push if (ex != 0) res.push_back({a, ex}); } // 最後に残った数について if (N != 1) res.push_back({N, 1}); return res; } template<class T> T gcd(T x, T y) { if (x == 0) { if (y == 0) return 1; else return y; } if (y == 0) return x; if (x % y == 0) return y; else return gcd(y, x % y); } template<class T> inline T lcm(T x, T y) { return x / gcd(x, y) * y; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); // cout << fixed << setprecision(15); /*----------------------------------------------------*/ int h, w; cin >> h >> w; vvi a(h, vi(w)); int min = INF; rep(i, h) rep(j, w) { cin >> a.at(i).at(j); chmin(min, a.at(i).at(j)); } int ans = 0; rep(i, h) rep(j, w) { ans += a.at(i).at(j) - min; } print(ans); }
#include <bits/stdc++.h> using namespace std; // 型 #define ll int64_t // ll -> int64_t using graph = vector<vector<int>>; // グラフ型 // for文 #define lp(i,n) for(int i=0;i<n;i++) // 変数 i をおいて n 回繰り返し #define lp_(i,n) for(int i=0;i<=n;i++) // 条件に=つき #define lpp(i,n,m) for(int i=n;i<m;i++) // 開始と終了を指定して繰り返し #define lpp_(i,n,m) for(int i=n;i<=m;i++) // 開始と終了を指定,条件に=つき // vector定義 #define _GLIBCXX_DEBUG #define vec(v,n) vector<int> v(n) // int #define vec_(v,n,m) vector<int> v(n,m) // int 初期条件あり #define vec64(v,n) vector<ll> v(n) // int64_t #define vec64_(v,n,m) vector<ll> v(n,m) // int64_t 初期条件あり #define vecc(v,n) vector<char> v(n) // char #define vecs(v,n) vector<string> v(n) // string #define vece(v) vector<int> v; // int 空 #define vecec(v) vector<char> v; // char 空 #define vec2i(v,h,w) vector<vector<int>> v(h,vector<int>(w)) // 二次元配列 int #define vec2l(v,h,w) vector<vector<ll>> v(h,vector<ll>(w)) #define vec2c(v,h,w) vector<vector<char>> v(h,vector<char>(w)) // 二次元配列 char // vector,string操作 #define all(v) v.begin(),v.end() #define back(v) v.back() // 最後の要素 #define sum(v) accumulate(all(v),0) // 総和 #define sort(v) sort(all(v)) // ソート #define reverse(v) reverse(all(v)) // 反転 #define lower(v,x) lower_bound(all(v),x)-v // x<=a[i]となる最小のi / a[i]<xとなるiの個数 #define count(v,n) count(all(v),n) // n の出現回数を数える #define pb(v,n) v.push_back(n) // 最後尾に n を追加 #define ins(v,i,n) v.insert(v.begin()+i,n) // 場所を指定してv[i]に n を追加 #define del(v) v.pop_back() // 最後尾を削除 #define del0(v) v.erase(v.begin()) // 最初の要素v[0]を削除 #define del_(v,i) v.erase(v.begin()+i) // 場所を指定してv[i]を削除 #define del__(v,i,j) v.erase(v.begin()+i,v.begin()+j+1) // 範囲を指定してv[i]~v[j]を削除 #define sub(s,i,j) s.substr(i,j-i+1) // 範囲を指定してs[i]~s[j]を取得【stringのみ】 // others #define maxp 1000000007 #define under(n) cout<<fixed<<setprecision(n) // 小数点以下の桁数を指定 #define cout(n) cout<<n<<endl // JSC2021 int main() { int n,m; cin>>n>>m; vec(a,n); vec(b,m); lp(i,n) cin>>a[i]; lp(i,m) cin>>b[i]; vec(c,2000); lp(i,n) c[a[i]]++; lp(i,m) c[b[i]]++; lp(i,2000) { if(c[i]==1) cout<<i<<" "; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; const ll M = 1e18; int si[] = {-1, 0, 1, 0}; int sj[] = {0, -1, 0, 1}; int main(){ ll a,b; cin >> a >> b; cout << max(2*a+100-b,(ll)0) << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> //#include <algorithm> // #include <iomanip> #define ll long long #define map unordered_map #define set unordered_set #define l_l pair<ll, ll> #define vll vector<ll> #define mll map<ll, ll> #define mp make_pair using namespace std; const ll MOD = 1000000007LL; const ll INF = (1LL << 60LL); ll mod(ll a, ll m) { ll r = (a % m + m) % m; return r; } ll extGcd(ll a, ll b, ll &p, ll &q) { if (b == 0) { p = 1; q = 0; return a; } ll d = extGcd(b, a % b, q, p); q -= a / b * p; return d; } pair<ll, ll> crt(const vector<ll> &b, const vector<ll> &m) { ll r = 0, M = 1; for (int i = 0; i < (int)b.size(); ++i) { ll p, q; ll d = extGcd(M, m[i], p, q); if ((b[i] - r) % d != 0) { return make_pair(0, -1); } ll tmp = (b[i] - r) / d * p % (m[i] / d); r += M * tmp; M *= m[i] / d; } return make_pair(mod(r, M), M); } int main() { ll T; scanf("%lld", &T); for (ll i = 0; i < T; i++) { ll N, S, K; scanf("%lld %lld %lld", &N, &S, &K); auto r1 = crt({0, N - S}, {K, N}); // cout << r1.second << "k+" << r1.first << endl; // 15k + 8 ll ans; if (r1.second <= 0) { ans = -1; } else { ans = r1.first / K; } cout << (ans) << endl; // cout << ("") << endl; } // }
#include <iostream> #include <algorithm> using namespace std; const int inf = 3e5+10; const int N = 3e5+10; typedef long long ll; struct node { int val,i; bool operator<(node x)const { return val<x.val; } }sn[inf]; ll tree[inf],b[inf],n; void add(int i,ll x) { while(i<=n){ tree[i]+=x; i+=i&-i; } } ll query(int i) { ll sum=0; while(i>=1) { sum+=tree[i]; i-=i&-i; } return sum; } int pai[inf]; int main() { ios::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++) { cin>>pai[i]; pai[i]++; sn[i].val=pai[i]; sn[i].i=i; } sort(sn+1,sn+n+1); int cnt=1; for(int i=1;i<=n;i++) { if(i>1&&sn[i].val>sn[i-1].val) cnt++; b[sn[i].i]=cnt; } ll ans=0; for(int i=1;i<=n;i++) { add(b[i],1); ans+=i-query(b[i]); } cout<<ans<<endl; for(int i = 1; i < n; i++ ){ ll right = pai[i]-1; ll left;// = pai[i]+1; if(pai[i]==n)left = 0; else left = n - pai[i]; ans = ans-right+left; cout<<ans<<endl; } // cout<<ans<<endl; return 0; }
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} //0-indexed class BIT{ public: int N; vector<ll> data; BIT(){} BIT(int n):N(n),data(n){} ll sum(int l,int r){//[l,r) return sum(r)-sum(l); } void add(int p,ll x){ p++; while(p<=N){ data[p-1]+=x; p+=p&(-p); } } private: ll sum(int r){ ll s=0; while(r>0){ s+=data[r-1]; r-=r&(-r); } return s; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector<ll> A(N),B(N); rep(i,N) cin>>A[i]; rep(i,N) cin>>B[i]; { ll asum=0,bsum=0; rep(i,N) asum+=A[i]; rep(i,N) bsum+=B[i]; if(asum!=bsum){ cout<<-1<<endl; return 0; } } map<ll,vector<int>> vmp; for(int i=N-1;i>=0;i--) vmp[A[i]+i].push_back(i); vector<int> P(N,0); rep(i,N){ ll tarv=B[i]+i; if(vmp.count(tarv)==false||vmp[tarv].empty()){ cout<<-1<<endl; return 0; } int t=vmp[tarv].back(); vmp[tarv].pop_back(); P[t]=i; } BIT bit(N); ll ans=0; rep(i,N){ ll res=bit.sum(P[i],N); ans+=res; bit.add(P[i],1); } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define int ll typedef pair<int, int> P; ll gcd(ll a, ll b){return b? gcd(b, a % b): a;} ll quickpow(ll a, ll b){ll res = 1; while(b){if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;} // head const int N = 2e5 + 5; int a[N], b[N], n, num, ans, tmp[N]; map<pair<int, int>, int> mp; map<int, int> cnt; void merge(int l, int r) { int mid = (l + r) >> 1, i = l, j = mid + 1, now = l; while (i <= mid && j <= r) { if (a[i] < a[j]) tmp[now++] = a[i++]; else { ans += mid - i + 1; tmp[now++] = a[j++]; } } while (i <= mid) tmp[now++] = a[i++]; while (j <= r) tmp[now++] = a[j++]; for (int i = l; i <= r; i++) a[i] = tmp[i]; } void merge_sort(int l, int r) { if (l == r) return; int mid = (l + r) >> 1; merge_sort(l, mid); merge_sort(mid + 1, r); merge(l, r); } signed main(void){ ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += i; } for (int i = 1; i <= n; i++) { cin >> b[i]; b[i] += i; } for (int i = 1; i <= n; i++) { mp[make_pair(++cnt[b[i]], b[i])] = ++num; } cnt.clear(); for (int i = 1; i <= n; i++) { pair<int, int> now = make_pair(++cnt[a[i]], a[i]); if (!mp.count(now)) { cout << -1 << endl; return 0; } a[i] = mp[now]; //cout<<a[i]<<" "; }//cout<<endl; merge_sort(1, n); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x) #define all(v) (v).begin(), (v).end() using namespace std; #define forn(i,n) for (int i = 0; i < (n); ++i) using ll = long long; int mod = (ll)1e9 + 7; #define PI acos(-1) typedef pair<int, int> pairs; const int INF = 1e9 + 1; const int N = 2e5 + 100; const double eps = 1e-3; template <typename XPAX> void ckma(XPAX &x, XPAX y) { x = (x < y ? y : x); } template <typename XPAX> void ckmi(XPAX &x, XPAX y) { x = (x > y ? y : x); } void solve() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w - 1)); forn(i, h) forn(j, w - 1) cin >> a[i][j]; vector<vector<int>> b(h - 1, vector<int>(w)); forn(i, h - 1) forn(j, w) cin >> b[i][j]; using P = tuple<int, int, int>; auto myid = [&](P p) { int r, c , f; tie(r, c, f) = p; return f * (h * w) + r * w + c; }; using Q = pair<int, P>; vector<int> dist(2 * h * w + 1, INF); priority_queue<Q, vector<Q>, greater<Q>>q; auto push = [&](P p, int D){ if(dist[myid(p)] <= D)return; dist[myid(p)] = D; q.push({D, p}); }; push(P{0, 0, 0}, 0); auto get = [&](P p, int &r, int &c, int &f ) { tie(r, c, f) = p; }; while(q.size()) { Q s = q.top(); q.pop(); int D = s.first; int r, c, f; get(s.second, r, c, f); if(r == h - 1 && c == w - 1) { cout << D << endl; return; } if(f == 0) { push(P{r, c, 1}, D + 1); if(c) push(P{r, c - 1, f},D + a[r][c - 1]); if(c + 1 < w) { push(P{r, c + 1, f},D + a[r][c]); } if(r + 1 < h) push(P{r + 1, c, f}, D + b[r][c]); } else { if(r) push(P{r - 1, c, f}, D + 1); push(P{r, c, 0}, D); } } } void test_case() { int t; cin >> t; forn(p, t) { //cout << "Case #" << p + 1 << ": "; solve(); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); solve(); }
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ii = pair<int, int>; const int MAXN = 500; int r, c; int a[MAXN+1][MAXN]; int b[MAXN][MAXN+1]; int cost[MAXN+1][MAXN+1]; int dijkstra() { memset(cost, -1, sizeof(cost)); priority_queue<pair<int, ii>, vector<pair<int, ii>>, greater<pair<int, ii>>> pq; cost[1][1] = 0; pq.push({0, {1, 1}}); while(!pq.empty()) { pair<int, ii> u = pq.top(); pq.pop(); int curCost = u.first; ii pos = u.second; // printf("%d %d %d\n", curCost, pos.first, pos.second); if(pos.first == r and pos.second == c) { return curCost; } if(curCost > cost[pos.first][pos.second]) { continue; } if(pos.first < r and (cost[pos.first+1][pos.second] == -1 or curCost+b[pos.first][pos.second] < cost[pos.first+1][pos.second])) { cost[pos.first+1][pos.second] = curCost+b[pos.first][pos.second]; pq.push({cost[pos.first+1][pos.second], {pos.first+1, pos.second}}); } if(pos.second > 1 and (cost[pos.first][pos.second-1] == -1 or curCost+a[pos.first][pos.second-1] < cost[pos.first][pos.second-1])) { cost[pos.first][pos.second-1] = curCost+a[pos.first][pos.second-1]; pq.push({cost[pos.first][pos.second-1], {pos.first, pos.second-1}}); } if(pos.second < c and (cost[pos.first][pos.second+1] == -1 or curCost+a[pos.first][pos.second] < cost[pos.first][pos.second+1])) { cost[pos.first][pos.second+1] = curCost+a[pos.first][pos.second]; pq.push({cost[pos.first][pos.second+1], {pos.first, pos.second+1}}); } for(int i=1; i<pos.first; i++) { if(cost[pos.first-i][pos.second] == -1 or curCost+i+1 < cost[pos.first-i][pos.second]) { cost[pos.first-i][pos.second] = curCost+i+1; pq.push({cost[pos.first-i][pos.second], {pos.first-i, pos.second}}); } else if(curCost+i+1 == cost[pos.first-i][pos.second]) { continue; } else { break; } } } } int main() { scanf("%d %d", &r, &c); for(int i=1; i<=r; i++) { for(int j=1; j<c; j++) { scanf("%d", &a[i][j]); } } for(int i=1; i<r; i++) { for(int j=1; j<=c; j++) { scanf("%d", &b[i][j]); } } printf("%d\n", dijkstra()); }
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> #include <string> #include <sstream> 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; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; using P = pair<int,int>; const int INF=1001001001; const int mod =1e9+7; struct Unionfind{ vector<int>d; vector<map<int,int>>mp; Unionfind(int n):d(n,-1),mp(n) {} int root(int x){ if(d[x]<0){return x;} return d[x]=root(d[x]); } bool unite(int x,int y){ x=root(x);y=root(y); if(x==y){return false;} if(d[x]>d[y]){swap(x,y);} for(auto p:mp[y]){ mp[x][p.first]+=p.second; } d[x]+=d[y]; d[y]=x; return true; } bool same(int x,int y){return root(x)==root(y);} int size(int x){return -d[root(x)];} }; int main() { int N,Q;cin>>N>>Q; Unionfind uf(N); rep(i,N){ int c;cin>>c; uf.mp[i][c]=1;//集団iの中でクラスiにいる人数 } rep(i,Q){ int q;cin>>q; if(q==1){ int a,b;cin>>a>>b;a--;b--; uf.unite(a,b); } else{ int x,y;cin>>x>>y;x--; int px=uf.root(x); cout<<uf.mp[px][y]<<endl; } } return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <ios> #include <iostream> #include <limits> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> #include <utility> using namespace std; using i64 = int64_t; using u64 = uint64_t; using i32 = int32_t; using pi64 = pair<i64, i64>; constexpr i64 MAXN = 3*100*1000LL+5LL; constexpr i64 MOD = 1000000007LL; constexpr i64 INF64 = MOD * MOD; #define vec vector #define let const #define DRi64(x) i64 x; cin >> x; #define DRS(x) string x; cin >> x; #define DRVi64(v, n) vec<i64> v(n); { for (i64 i = 0; i < n; ++i) { cin >> v[i]; }} #ifdef DEBUG #define P(x) cerr << x << "\n" #else #define P(x) #endif i64 find_(const i64 a, vec<i64>& parents) { if (parents[a] != a) { parents[a] = find_(parents[a], parents); } return parents[a]; } void union_(const i64 a, const i64 b, vec<i64>& parents, vec<i64>& sizes, vec<map<i64, i64>>& class_hists) { let i64 r_a = find_(a, parents); let i64 r_b = find_(b, parents); if (r_a == r_b) { return; } if (sizes[r_a] < sizes[r_b]) { union_(r_b, r_a, parents, sizes, class_hists); return; } sizes[r_a] += sizes[r_b]; parents[r_b] = r_a; // merge map of smaller vertex into that of larger vertex for (let auto& [cls, cnt] : class_hists[r_b]) { class_hists[r_a][cls] += cnt; } } int main() { ios_base::sync_with_stdio(false); DRi64(N); DRi64(Q); DRVi64(C, N); // Ci \in [1, N] vec<i64> class_hist(N + 1, 0); for (let i64 c : C) { ++class_hist[c]; } vec<i64> parents(N + 1); iota(parents.begin(), parents.end(), 0LL); vec<i64> sizes(N + 1, 1); vec<map<i64, i64>> class_hists(N + 1); for (i64 i = 1; i <= N; ++i) { class_hists[i][C[i - 1]] = 1; } for (; Q-->0;) { DRi64(typ); if (typ == 1) { DRi64(A); DRi64(B); union_(A, B, parents, sizes, class_hists); } else if (typ == 2) { DRi64(X); DRi64(Y); if (let i64 r_X = find_(X, parents); class_hists[r_X].count(Y)) { cout << class_hists[r_X][Y] << "\n"; } else { cout << "0\n"; } } else { abort(); } } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+5; const int mod=1e9+7; int n,m; int C[17][17]; char s[N]; int a[N]; int w[200]; inline int po(int x,int k){ int ret=1; while(k){ if(k&1) ret=1ll*ret*x%mod; x=1ll*x*x%mod; k>>=1; } return ret; } inline int calc(int f,int c){ if(c<m-f) return 0; int ret=0; for(int i=m;i>=f;i--){ if(m-i&1) ret=(ret-1ll*C[m-f][i-f]*po(i,c)%mod+mod)%mod; else ret=(ret+1ll*C[m-f][i-f]*po(i,c))%mod; } return ret; } int cnt[17],tot; int main(){ int i,j; for(i='0';i<='9';i++) w[i]=i-'0'; for(i='A';i<='F';i++) w[i]=i-'A'+10; for(i=0;i<17;i++){ for(j=0;j<17;j++){ if(j==0||j==i) C[i][j]=1; else C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod; } } cin>>(s+1);n=strlen(s+1); for(i=1;i<=n;i++) a[i]=w[s[i]]; cin>>m; int ans=0; for(i=2;i<=n;i++){ ans=(ans+1ll*C[15][m]*calc(0,n-i+1))%mod; ans=(ans+1ll*C[15][m-1]*(m-1)%mod*calc(1,n-i))%mod; } ans=(ans+1ll*C[15][m-1]*(a[1]-1)%mod*calc(1,n-1))%mod; for(i=1;i<n;i++){ cnt[a[i]]++;if(cnt[a[i]]==1) tot++; if(tot>m) break; if(a[i+1]==0) continue; int s1=0,s2=0; for(j=0;j<a[i+1];j++) if(cnt[j]) s1++; s2=tot-s1; int t1=a[i+1]-s1,t2=16-a[i+1]-s2; for(j=0;j<=t1&&j<=m-tot;j++) ans=(ans+1ll*C[t1][j]*C[t2][m-tot-j]%mod*(1ll*s1*calc(tot,n-i-1)+1ll*j*calc(tot+1,n-i-1)))%mod; } cnt[a[n]]++;if(cnt[a[n]]==1) tot++; if(tot==m) ans=(ans+1)%mod; cout<<ans; return 0; }
#include <iostream> #include <iomanip> #include <fstream> #include <cmath> #include <cstdlib> #include <cstring> #include <climits> #include <algorithm> #include <numeric> #include <utility> #include <random> #include <chrono> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <unordered_set> #include <unordered_map> using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using PIL = pair<int, LL>; using PLL = pair<LL, LL>; const int mod = 1e9 + 7; // const int mod = 998244353; inline void quickread() { ios::sync_with_stdio(false); cin.tie(nullptr); } string s; int add[200010], keep[200010], has[200010]; int f[200010][2][17]; int k; inline int tonum(char c) { return (c <= '9' ? c - '0' : c - 'A' + 10); } inline int dfs(int pos, bool bound, int diff) { if (pos == s.size()) { return diff == k; } if (diff > k) { return 0; } if (f[pos][bound][diff] != -1) { return f[pos][bound][diff]; } f[pos][bound][diff] = 0; if (!diff) { f[pos][bound][diff] = dfs(pos + 1, false, 0); } if (bound) { f[pos][bound][diff] += dfs(pos + 1, true, has[pos]); f[pos][bound][diff] %= mod; f[pos][bound][diff] += (LL)(diff ? add[pos] : add[pos] - 1) * dfs(pos + 1, false, diff + 1) % mod; f[pos][bound][diff] %= mod; f[pos][bound][diff] += (LL)keep[pos] * dfs(pos + 1, false, diff) % mod; f[pos][bound][diff] %= mod; } else { f[pos][bound][diff] += (LL)((diff ? 16 : 15) - diff) * dfs(pos + 1, false, diff + 1) % mod; f[pos][bound][diff] %= mod; f[pos][bound][diff] += (LL)diff * dfs(pos + 1, false, diff) % mod; f[pos][bound][diff] %= mod; } return f[pos][bound][diff]; } inline void work() { cin >> s >> k; unordered_set<int> o; for (int i = 0; i < s.size(); ++i) { int jm = tonum(s[i]); for (int j = 0; j < jm; ++j) { if (!o.count(j)) { ++add[i]; } else { ++keep[i]; } } o.insert(jm); has[i] = o.size(); } memset(f, -1, sizeof(f)); int ans = dfs(0, 1, 0); cout << ans << "\n"; } int main() { // freopen("input.txt", "r", stdin); quickread(); work(); return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #include <map> #include <cmath> #include <queue> #include <cstdio> #include <limits> #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) 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(a > b){ a = b; return 1; } return 0; } template<class T> inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; const int inf = numeric_limits<int>::max(); const ll infll = numeric_limits<ll>::max(); struct UFT{ vector<int> par;//親 vector<int> rank;//木の深さ vector<int> size;//木の大きさ int n; UFT(int _n) { n = _n; par.resize(n); rank.assign(n,0); size.assign(n,1); rep(i,n){ par[i] = i; } } //xの根を返す int find(int x) { if(par[x] == x) return x; else return par[x] = find(par[x]); } //x,yを併合 void unite(int x,int y) { x = find(x); y = find(y); if(x == y) return; if(size[x] < size[y]){ par[x] = y; size[y] += size[x]; } else{ par[y] = x; size[x] += size[y]; if(rank[x] == rank[y]) rank[x]++; } } //x,yが同じグループにいるかどうかを返す bool same(int x,int y) { return find(x) == find(y); } //xの属する木のサイズを探す int usize(int x) { return size[find(x)]; } }; int main() { int n,q; cin >> n >> q; vi c(n); rep(i,n) cin >> c[i]; UFT uf(n); vector<map<int,int>> mp(n); rep(i,n) { mp[i][c[i]]++; } rep(i,q) { int t,x,y; cin >> t >> x >> y; if(t == 1) { x--; y--; int a = uf.find(x), b = uf.find(y); uf.unite(x, y); if(b != uf.find(b)) { for(auto val: mp[b]) { auto [id, num] = val; mp[a][id] += num; } } if(a != uf.find(a)) { for(auto val: mp[a]) { auto [id, num] = val; mp[b][id] += num; } } } else { x--; cout << mp[uf.find(x)][y] << "\n"; } } return 0; }
#include <cstdio> #include <vector> inline int min(int a,int b){return a<b?a:b;} inline int max(int a,int b){return a>b?a:b;} const int maxn = 8e5+5; int n,q,c[maxn]; int head[maxn],tot; struct Edge{ int next,to; }e[maxn]; inline void add_edge(int x,int y){ e[++tot].next = head[x]; e[tot].to = y,head[x] = tot; } int ANS[maxn]; int ans[maxn],siz[maxn],son[maxn],in[maxn],out[maxn],id[maxn],tim; inline void dfs1(int u){ siz[u] = (u <= n); if(u <= n)in[u] = out[u] = ++tim,id[tim] = u; if(u > n){ in[u] = n+1,out[u] = 0; for(int i=head[u],v;v=e[i].to,i;i=e[i].next){ dfs1(v); // printf("dfs:%d %d\n",u,v); siz[u] += siz[v]; if(siz[v] > siz[son[u]])son[u] = v; in[u] = min(in[u],in[v]); out[u] = max(out[u],out[v]); } } } struct Query{int id,col;}; std :: vector <Query> qu[maxn]; inline void dfs2(int u){ if(u <= n)++ans[c[u]]; else{ for(int i=head[u],v;v=e[i].to,i;i=e[i].next){ if(v != son[u]){ // printf("vark :%d %d\n",u,ans[2]); // printf("inertval : %d %d %d\n",v,in[v],out[v]); dfs2(v); for(int j=in[v];j<=out[v];++j)ans[c[id[j]]]--; } } dfs2(son[u]); // printf("vark :%d %d\n",u,ans[2]); for(int i=head[u],v;v=e[i].to,i;i=e[i].next) if(v != son[u]) for(int j=in[v];j<=out[v];++j)ans[c[id[j]]]++; } for(int i=0;i<qu[u].size();++i)ANS[qu[u][i].id] = ans[qu[u][i].col]; } int fa[maxn<<1]; inline int getfa(int x){return fa[x] == x ? x : fa[x] = getfa(fa[x]);} int nodcnt,ccnt; int main(){ scanf("%d %d",&n,&q); for(int i=1;i<=n;++i)scanf("%d",&c[i]); for(int i=1;i<=n*2;++i)fa[i] = i; nodcnt = n; for(int i=1;i<=q;++i){ int ty,a,b; scanf("%d %d %d",&ty,&a,&b); a = getfa(a); if(ty == 1)b = getfa(b); if(ty == 1){ if(a != b) add_edge(++nodcnt,a),add_edge(nodcnt,b),fa[a] = nodcnt,fa[b] = nodcnt; } else qu[a].push_back((Query){++ccnt,b}); } for(int i=1;i<=2*n;++i)if(i == getfa(i))dfs1(i); for(int i=1;i<=2*n;++i) if(i == getfa(i)){ dfs2(i); for(int j=in[i];j<=out[i];++j)--ans[c[id[j]]]; } // for(int i=1;i<=2*n;++i)printf("%d %d\n",in[i],out[i]); // puts(""); // for(int i=1;i<=2*n;++i){ // for(int j=0;j<qu[i].size();++j)printf("%d %d\n",qu[i][j].col,qu[i][j].id); // puts("end1"); // } // puts(""); for(int i=1;i<=ccnt;++i)printf("%d\n",ANS[i]); return 0; } /* 5 5 2 2 2 2 2 1 1 2 1 1 3 1 2 3 2 2 2 2 4 2 */
#include<bits/stdc++.h> #define lli long long int using namespace std; bool sumpro(lli s, lli p) { if(s == p+1) return true; else if(s == p || s>p) return false; for(int i =1; i<int(sqrt(p)) + 1; i++) { if (p%i == 0) { if(p/i + i == s) return true; } } return false; } int main() { lli s,p; cin>>s>>p; if(sumpro(s,p)) cout<<"Yes\n"; else cout<<"No\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> p_ll; typedef vector<pair<ll, ll>> vec_p; //vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i) #define ture ture #define flase false #define falg flag #define REP(i, x) for (ll i = 0; i < (ll)(x); i++) #define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++) #define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--) #define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--) #define all(x) (x).begin(), (x).end() const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1;//llの最大9*10^18 const int INF = pow(2, 30) - 1; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } vector<ll> vec; vector<ll> divisor(ll n) { vector<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } int main() { ios::sync_with_stdio(false); cin.tie(0); ll S,P; string A; cin >> S>>P; bool flag = false; vec = divisor(P); for (ll i = 0; i < vec.size(); i++) { ll M = P / vec.at(i); if(M+vec.at(i)==S){ flag = true; } } if(flag){ cout << "Yes" << endl; } else{ cout << "No" << endl; } }
//#define _GLIBCXX_DEBUG //#include "atcoder/all" //using namespace atcoder; #include <bits/stdc++.h> #define int long long #define ll long long using ull = unsigned long long; using namespace std; #define Dump(x) \ if (dbg) { \ cerr << #x << " = " << (x) << endl; \ } #define overload4(_1, _2, _3, _4, name, ...) name #define FOR1(n) for (ll i = 0; i < (n); ++i) #define FOR2(i, n) for (ll i = 0; i < (n); ++i) #define FOR3(i, a, b) for (ll i = (a); i < (b); ++i) #define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c)) #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FORR(i, a, b) for (int i = (a); i <= (b); ++i) #define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/ namespace mydef { const int INF = 1ll << 60; const int MOD = 1e9 + 7; template <class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } else return 0; } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } else return 0; } void Yes(bool flag = true) { if (flag) cout << "Yes" << endl; else cout << "No" << endl; } void No(bool flag = true) { Yes(!flag); } void YES(bool flag = true) { if (flag) cout << "YES" << endl; else cout << "NO" << endl; } void NO(bool flag = true) { YES(!flag); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } bool dbg = true; } // namespace mydef using namespace mydef; #define pb push_back //#define mp make_pair #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(), (v).end() #define SZ(x) ((int)(x).size()) #define vi vector<int> #define vvi vector<vector<int>> #define vp vector<pair<int, int>> #define vvp vector<vector<pair<int, int>>> #define pi pair<int, int> //#define P pair<int, int> //#define V vector<int> //#define S set<int> #define asn ans int N; vi A; void solve() { int ans = 0, sum = 0, MAX = 0; for (int i = 0; i < N; i++) { chmax(MAX, A[i]); sum += A[i]; ans += sum; cout << ans + MAX * (i + 1) << endl; } } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> N; A.resize(N); for (auto&& a : A) { cin >> a; } solve(); return 0; }
#pragma GCC optimize("Ofast") #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 <iomanip> // setprecision #include <complex> // complex #include <math.h> #include <functional> #include <cassert> using namespace std; using ll = long long; using P = pair<ll,ll>; constexpr ll INF = 1e18; constexpr ll LLMAX = 9223372036854775807; constexpr int inf = 1e9; constexpr ll mod = 1000000007; // constexpr ll mod = 998244353; const int dx[8] = {1, 0, -1, 0,1,1,-1,-1}; const int dy[8] = {0, 1, 0, -1,1,-1,1,-1}; 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; } // --------------------------------------------------------------------------- int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll N,T; cin >> N >> T; vector<ll> A(N); for(int i=0; i<N; i++){ cin >> A[i]; } set<ll> pre; for(int bit=0; bit<(1LL<<(N/2)); bit++){ ll sum = 0; for(int i=0; i<N/2; i++){ if(bit & (1LL<<i)) sum += A[i]; } pre.emplace(sum); } set<ll> suf; for(int bit=0; bit<(1LL<<(N-N/2)); bit++){ ll sum = 0; for(int i=0; i<(N-N/2); i++){ if(bit & (1LL<<i)) sum += A[N/2+i]; } suf.emplace(-sum); } ll ans = 0; for(auto p: pre){ if(p > T) break; ll now = T-p; ll s = *suf.lower_bound(-now); // cout << p << " " << s << "\n"; chmax(ans,p-s); } cout << ans << "\n"; return 0; }