code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main(){ long n,m;cin>>n; set<long> s; rep(i,n){cin>>m; s.insert(m);} //cout<<*rbegin(s)<<endl; while(1){ if(*rbegin(s)==*begin(s)||s.size()==1){cout<<*rbegin(s);break;} else { //if(s.size()==1){cout<<*begin(s);return 0;} s.insert(*rbegin(s)-*begin(s)); s.erase(*rbegin(s)); /*cout<<*rbegin(s)-*begin(s)<<endl;*/ } } }
#include <bits/stdc++.h> using namespace std; #define rep(i, c) for (int i = 0; i < (int)c; i++) using ll = long long; ll GCD(ll a, ll b) { if (b == 0) return a; else return GCD(b, a % b); } int main() { int n; cin >> n; ll res = 0; rep(i, n) { ll a; cin >> a; res = GCD(res, a); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long lnt; const lnt P = 1000000007; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, s; lnt PowMod(lnt x, int k) { lnt ret = 1; for (; k; k >>= 1, x = x*x%P) if (k&1) ret = ret*x%P; return ret; } lnt C(int n, int m) { lnt ret = 1; for (int i = 2; i <= m; i++) ret = ret*i%P; ret = PowMod(ret, P-2); for (int i = n; i > n-m; i--) ret = ret*i%P; return ret; } int main() { read(n), read(m); for (int i = 0, a; i < n; i++) read(a), s += a; return printf("%lld\n", C(n+m, n+s)), 0; }
#include <bits/stdc++.h> #define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a) #define rep2(i, s, n) rep3(i, s, n, 1) #define rep(i, n) rep2(i, 0, n) using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; int main() { string n; cin >> n; int nlen = n.size(), res=-1; rep2(i, 1, 1<<nlen){ int sum = 0; rep(j, nlen){ if((i>>j) & 1){ sum += n[j] - '0'; // cout << sum << endl; } } if(sum%3==0){ // cout << i << " " << sum << endl; res = max(res, __builtin_popcount(i)); } } if(res == -1){ cout << res << endl; }else{ cout << nlen - res << endl; } return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <map> #include <cmath> #include <iomanip> #include <bitset> using namespace std; #define REP(i,n) for (int i=0;i<(n);++i) #define rep(i,a,b) for(int i=a;i<(b);++i) template<class T> inline bool chmin(T &a, T b){ if(a > b) { a = b; return true;} return false;} template<class T> inline bool chmax(T &a, T b){ if(a < b) { a = b; return true;} return false;} using ll = long long; constexpr ll INF = 1LL << 60; constexpr int MOD = 998244353; int main() { ios::sync_with_stdio(false); cin.tie(0); ll N, K, M; cin >> N >> K >> M; vector<ll> A(N); for(int i = 0; i < N-1; ++i) cin >> A[i]; ll f = N * M; REP(i, N-1) f -= A[i]; if (f > K) { cout << -1; } else { cout << max(0LL, f); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n,k,m,s=0,i; cin>>n>>k>>m; int arr[n]; for(i=0;i<n-1;i++) { cin>>arr[i]; s+=arr[i]; } int ans=n*m-s; if(ans>k) cout<<"-1"<<endl; else if(ans<0) cout<<"0"<<endl; else cout<<ans<<endl; }
#include <iostream> using namespace std; int f[16][16],p[16][2]; int h,w,a,b,a1,b1; int set1(int j,int k){ int i0,i1; i0=j/w; i1=j%w; if(f[i0][i1]==1) return 0; switch(k){ case 1: if(a1==0||i0+1==h||f[i0+1][i1]==1) return 0; f[i0][i1]=1; f[i0+1][i1]=1; a1--; break; case 2: if(a1==0||i1+1==w||f[i0][i1+1]==1) return 0; f[i0][i1]=1; f[i0][i1+1]=1; a1--; break; case 3: if(b1==0) return 0; f[i0][i1]=1; b1--; } return 1; } void set0(int j,int k){ int i0,i1; i0=j/w; i1=j%w; switch(k){ case 1: f[i0][i1]=0; f[i0+1][i1]=0; a1++; break; case 2: f[i0][i1]=0; f[i0][i1+1]=0; a1++; break; case 3: f[i0][i1]=0; b1++; } } int main(){ int l,i,j,k,c=0; cin >> h >> w >> a >> b; for(i=0;i<h;i++){ for(j=0;j<w;j++){ f[i][j]=0; } } a1=a; b1=b; l=0; p[0][0]=0; while(l>=0){ if(p[l][0]==4){ l--; continue; } if(p[l][0]){ k=p[l][1]; set0(k,p[l][0]); } else { for(k=0;k<h*w;k++){ if(f[k/w][k%w]==0) break; } } for(i=p[l][0]+1;i<=3;i++){ if(set1(k,i)){ break; } } p[l][0]=i; if(i<=3){ p[l][1]=k; if(l==a+b-1){ c++; } else { l++; p[l][0]=0; } } } cout << c << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define vec vector #define rep(i,n) for (int i=0; i<(n); i++) #define print(a) cout << a << endl #define fix(n) fixed << setprecision(n) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define Yes "Yes" #define YES "YES" #define No "No" #define NO "NO" int main() { int N, M, Q; cin >> N; vec<ll> X(N), Y(N); rep(i,N) cin >> X[i] >> Y[i]; cin >> M; vec<ll> ax(M+1), ay(M+1), bx(M+1), by(M+1), sw(M+1,0); ax[0] = 1; ay[0] = 1; bx[0] = 0; by[0] = 0; rep(i,M) { int op,p; cin >> op; if ((op==1 && sw[i]==1) || (op==2 && sw[i]==0)) { ax[i+1] = ax[i]; ay[i+1] = ay[i]*(-1); bx[i+1] = bx[i]; by[i+1] = by[i]*(-1); sw[i+1] = (sw[i]+1)%2; } else if ((op==1 && sw[i]==0) || (op==2 && sw[i]==1)) { ax[i+1] = ax[i]*(-1); ay[i+1] = ay[i]; bx[i+1] = bx[i]*(-1); by[i+1] = by[i]; sw[i+1] = (sw[i]+1)%2; } else { cin >> p; if ((op==3 && sw[i]==1) || (op==4 && sw[i]==0)) { ax[i+1] = ax[i]; ay[i+1] = ay[i]*(-1); bx[i+1] = bx[i]; by[i+1] = 2*p-by[i]; sw[i+1] = sw[i]; } else { ax[i+1] = ax[i]*(-1); ay[i+1] = ay[i]; bx[i+1] = 2*p-bx[i]; by[i+1] = by[i]; sw[i+1] = sw[i]; } } } cin >> Q; rep(_,Q) { int A,B; cin >> A >> B; B--; ll x = X[B], y = Y[B]; x = ax[A]*x + bx[A]; y = ay[A]*y + by[A]; if (sw[A]) swap(x,y); print(x << " " << y); } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; typedef long long ll; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) FOR(i,0,n) const double EPS = 1e-10; const double PI = acos(-1.0); #define CLR(a) memset((a), 0 ,sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; 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; } bool compare_by_b(pair<LL, LL> a, pair<LL, LL> b) { if(a.second != b.second) return a.second < b.second; else return a.first < b.first; } std::uint32_t euclidean_gcd(std::uint32_t a, std::uint32_t b){return b != 0 ? euclidean_gcd(b, a % b) : a;} const string YES = "Yes"; const string NO = "No"; void solve(long long V, long long T, long long S, long long D){ if(V*T<=D && V*S>=D){ cout<<NO<<endl; } else cout<<YES<<endl; } int main(){ long long V; scanf("%lld",&V); long long T; scanf("%lld",&T); long long S; scanf("%lld",&S); long long D; scanf("%lld",&D); solve(V, T, S, D); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int V; int T; int S; int D; cin >> V >> T >> S >> D; if(V*T <= D && V*S >= D) { cout << "No"; } else { cout << "Yes"; } }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> 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 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; if ((tmp & ma0) ^ ma0) { 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); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int tmp[200001]; void pakuri_sort(int N, int A[]) { const int b = 8; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } int main() { //cin.tie(0); //ios::sync_with_stdio(false); int N = getint(), M = getint(); int A[200010]; rep1(i, M) A[i] = getint(); A[0] = 0; A[M + 1] = N + 1; pakuri_sort(M, A + 1); int saishou = 2e9; int t = 0; rep(i, M + 1) { int kari = A[i + 1] - A[i] - 1; if (kari) { if (kari < saishou) saishou = kari; tmp[t++] = kari; } } if (saishou > 1.5e9) { printf("0"); return 0; } ll kotae = 0; rep(i, t) kotae += (tmp[i] + saishou - 1) / saishou; printf("%d\n", kotae); Would you please return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; #define rep(i,n) for(ll i=0; i<n; i++) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rrep(i,n) for(ll i=n-1; i>=0; i--) #define fi first #define se second #define pcnt __builtin_popcountll typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> Pii; typedef pair<ll,ll> Pll; typedef pair<ll,Pll> PlP; template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; } template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; } template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";} template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);} string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();}; const ld eps = 1e-10; ull mo = 1000000007; ld PI=asin(1)*2; //using namespace atcoder; int main(){ ll N; cin >> N; vector<ld> A(N),B(N),C(N),D(N); ld aw=0,bw=0,cw=0,dw=0; rep(i,N){ cin >> A[i] >> B[i]; aw += A[i]; bw += B[i]; // A[i] *= N; // B[i] *= N; } aw /= N; bw /= N; rep(i,N){ cin >> C[i] >> D[i]; cw += C[i]; dw += D[i]; // C[i] *= N; // D[i] *= N; } cw /= N; dw /= N; rep(i,N){ A[i] += (-aw); B[i] += (-bw); } rep(i,N){ C[i] += (-cw); D[i] += (-dw); } vector<pair<ld,ld>> sp(N),tp(N); rep(i,N){ sp[i].fi = A[i]; sp[i].se = B[i]; } rep(i,N){ tp[i].fi = C[i]; tp[i].se = D[i]; } sort(sp.begin(), sp.end(), [](const auto &p1, const auto &p2) { if(-eps + atan2(p2.second, p2.first) <= atan2(p1.second, p1.first) && atan2(p1.second, p1.first) <= eps + atan2(p2.second, p2.first)){ return p1.first*p1.first + p1.second*p1.second < p2.first*p2.first + p2.second*p2.second; } return atan2(p1.second, p1.first) < atan2(p2.second, p2.first); }); sort(tp.begin(), tp.end(), [](const auto &p1, const auto &p2) { if(-eps + atan2(p2.second, p2.first) <= atan2(p1.second, p1.first) && atan2(p1.second, p1.first) <= eps + atan2(p2.second, p2.first)){ return p1.first*p1.first + p1.second*p1.second < p2.first*p2.first + p2.second*p2.second; } return atan2(p1.second, p1.first) < atan2(p2.second, p2.first); }); // cout << endl; // rep(i,N){ // cout << "(" << sp[i].fi << "," << sp[i].se << ")" << endl; // } // rep(i,N){ // cout << "(" << tp[i].fi << "," << tp[i].se << ")" << endl; // } if(N == 1) drop("Yes"); rep(d,N){ ll cnt =0; ld xs = sp[0].fi; ld ys = sp[0].se; ld xt = tp[d].fi; ld yt = tp[d].se; ld angle = atan2l(yt,xt)-atan2l(ys,xs); rep(i,N){ ld x1 = sp[i].fi*cosl(angle)-sp[i].se*sinl(angle); ld y1 = sp[i].fi*sinl(angle)+sp[i].se*cosl(angle); bool f = 0; rep(j,N){ ld x2 = tp[j].fi; ld y2 = tp[j].se; if( x2 - eps <= x1 && x1 <= x2 + eps){ if( y2 - eps <= y1 && y1 <= y2 + eps){ f = 1; } } } cnt += f; } if(cnt == N) drop("Yes"); } cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep_up(i, a, n) for (ll i = a; i < n; ++i) #define rep_down(i, a, n) for (ll i = a; i >= n; --i) #define P pair<ll, ll> #define all(v) v.begin(), v.end() #define fi first #define se second #define vvvll vector<vector<vector<ll>>> #define vvll vector<vector<ll>> #define vll vector<ll> #define pqll priority_queue<ll> #define pqllg priority_queue<ll, vector<ll>, greater<ll>> constexpr ll INF = (1ll << 60); constexpr ll mod = 1000000007; constexpr double pi = 3.14159265358979323846; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mypow(ll a, ll n) { ll ret = 1; rep(i, n) { if (ret > (ll)(1e18 + 10) / a) return -1; ret *= a; } return ret; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main(){ ll n,a,p,x,ans=INF; cin>>n; rep(i,n){ cin>>a>>p>>x; if(x-a>0) chmin(ans,p); } if(ans==INF) ans=-1; cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ long long A,B,C,D; cin >> A >> B >> C >> D; long long blue = A, red = 0; for(int i = 1; i <= A; i++){ blue += B; red += C; if(blue <= D*red){ cout << i << endl; return 0; } } cout << -1 << endl; }
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <tuple> #include <utility> #include <vector> #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false) template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; } template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; } template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int a, b, c; cin, a, b, c; print(a + b + c - min({a, b, c})); return 0; }
#include <bits/stdc++.h> using namespace std; int a, b, c; int main() { // freopen("in.txt", "r", stdin); cin >> a >> b >> c; cout << max({a + b, a + c, b + c}) << '\n'; return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #define rg register inline int read(){ rg int x=0,fh=1; rg char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') fh=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*fh; } const int maxn=1e4+5; bool vis[maxn][maxn]; int n,x[maxn],y[maxn],r[maxn],ax[maxn],ay[maxn],bx[maxn],by[maxn]; int main(){ n=read(); for(rg int i=1;i<=n;i++) x[i]=read(),y[i]=read(),r[i]=read(); for(rg int i=1;i<=n;i++) ax[i]=x[i],ay[i]=y[i],bx[i]=x[i]+1,by[i]=y[i]+1; for(rg int i=1;i<=n;i++){ for(rg int j=ax[i];j<=bx[i];j++){ for(rg int k=ay[i];k<=by[i];k++){ vis[j][k]=1; } } } rg int nsum=0; for(rg int i=1;i<=n;i++){ while(ax[i]>=1){ nsum=(bx[i]-ax[i]+1)*(by[i]-ay[i]+1); rg int jud=1; for(rg int j=ay[i];j<=by[i];j++){ if(vis[ax[i]-1][j]==1) jud=0; } if(!jud) break; rg double tmp1=(double)std::min(nsum,r[i])/(double)std::max(nsum,r[i]); rg double tmp2=(double)std::min(nsum+by[i]-ay[i]+1,r[i])/(double)std::max(nsum+by[i]-ay[i]+1,r[i]); if((1-tmp1*tmp1)*(1-tmp1*tmp1)>(1-tmp2*tmp2)*(1-tmp2*tmp2)){ nsum+=by[i]-ay[i]+1; for(rg int j=ay[i];j<=by[i];j++){ vis[ax[i]-1][j]=1; } ax[i]--; } else { break; } } while(ay[i]>=1){ nsum=(bx[i]-ax[i]+1)*(by[i]-ay[i]+1); rg int jud=1; for(rg int j=ax[i];j<=bx[i];j++){ if(vis[j][ay[i]-1]==1) jud=0; } if(!jud) break; rg double tmp1=(double)std::min(nsum,r[i])/(double)std::max(nsum,r[i]); rg double tmp2=(double)std::min(nsum+bx[i]-ax[i]+1,r[i])/(double)std::max(nsum+bx[i]-ax[i]+1,r[i]); if((1-tmp1*tmp1)*(1-tmp1*tmp1)>(1-tmp2*tmp2)*(1-tmp2*tmp2)){ nsum+=bx[i]-ax[i]+1; for(rg int j=ax[i];j<=bx[i];j++){ vis[j][ay[i]-1]=1; } ay[i]--; } else { break; } } while(bx[i]<=9999){ nsum=(bx[i]-ax[i]+1)*(by[i]-ay[i]+1); rg int jud=1; for(rg int j=ay[i];j<=by[i];j++){ if(vis[bx[i]+1][j]==1) jud=0; } if(!jud) break; rg double tmp1=(double)std::min(nsum,r[i])/(double)std::max(nsum,r[i]); rg double tmp2=(double)std::min(nsum+by[i]-ay[i]+1,r[i])/(double)std::max(nsum+by[i]-ay[i]+1,r[i]); if((1-tmp1*tmp1)*(1-tmp1*tmp1)>(1-tmp2*tmp2)*(1-tmp2*tmp2)){ nsum+=by[i]-ay[i]+1; for(rg int j=ay[i];j<=by[i];j++){ vis[bx[i]+1][j]=1; } bx[i]++; } else { break; } } while(by[i]<=9999){ nsum=(bx[i]-ax[i]+1)*(by[i]-ay[i]+1); rg int jud=1; for(rg int j=ax[i];j<=bx[i];j++){ if(vis[j][by[i]+1]==1) jud=0; } if(!jud) break; rg double tmp1=(double)std::min(nsum,r[i])/(double)std::max(nsum,r[i]); rg double tmp2=(double)std::min(nsum+bx[i]-ax[i]+1,r[i])/(double)std::max(nsum+bx[i]-ax[i]+1,r[i]); if((1-tmp1*tmp1)*(1-tmp1*tmp1)>(1-tmp2*tmp2)*(1-tmp2*tmp2)){ nsum+=bx[i]-ax[i]+1; for(rg int j=ax[i];j<=bx[i];j++){ vis[j][by[i]+1]=1; } by[i]++; } else { break; } } } for(rg int i=1;i<=n;i++) printf("%d %d %d %d\n",ax[i],ay[i],bx[i],by[i]); return 0; }
#include <cstdio> using namespace std; int n,m,k; int c[102],d[102],a[102],b[102],ans; bool vis[102]; void dfs(int x){ if(x==k){ int cnt=0; for(int i=0;i<m;++i){ if(vis[a[i]]&&vis[b[i]])++cnt; } if(ans<cnt)ans=cnt; }else if(vis[c[x]]&&vis[d[x]]){ dfs(x+1); }else{ if(!vis[c[x]]){ vis[c[x]]=true; dfs(x+1); vis[c[x]]=false; } if(!vis[d[x]]){ vis[d[x]]=true; dfs(x+1); vis[d[x]]=false; } } } int main(){ scanf("%d%d",&n,&m); for(int i=0;i<m;++i){ scanf("%d%d",a+i,b+i); } scanf("%d",&k); for(int i=0;i<k;++i){ scanf("%d%d",c+i,d+i); } dfs(0); printf("%d\n",ans); 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 CRingosFavoriteNumbers2 { public: static void solve(istream &cin, ostream &cout) { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int N; cin >> N; V<int> A(N); rep(i, N) cin >> A[i]; rep(i, N) A[i] = A[i] % 200; V<int> check(200); rep(i, N) check[A[i]]++; int ans = 0; rep(i, 200) ans += check[i] * (check[i] - 1) / 2; cout << ans << endl; } }; signed main() { CRingosFavoriteNumbers2 solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; #define F first #define S second #define PB push_back #define MP make_pair #define rep(i,a,b) for (int i = a; i < b; i++) const int INF =2e5+1; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t=1 ; ll f[INF]; f[1]=1; for(int i =2;i<INF;i++){ f[i]=f[i-1]+i; } while(t--){ int n ; cin>>n ; int a[n]; for(int i =0;i<n;i++){ cin>>a[i]; a[i]%=200; } sort(a,a+n); ll ans = 0; int num = 0 ; for(int i = 1 ; i<n;i++){ if(a[i]==a[i-1]){ num++; } else if (num>=1) { ans += f[num]; num=0; } else {num = 0;} } if (num>=1) { ans += f[num]; } else {num = 0;} cout<<ans<<endl; } }
#include <bits/stdc++.h> #include <algorithm> #define _GLIBCXX_DEBUG using namespace std; typedef long long ll; int main(){ ll a,b,mx=1; cin >> a >> b; vector<ll> count(b+1,0); for(ll i=a; i<=b; i++){ for(ll j=1; j*j<=i; j++){ if(i%j==0){ if(j*j==i){ count[j]++; } else{ count[j]++; count[i/j]++; } } } } for(ll i=1; i<=b; i++){ if(count[i]>=2 && mx < i){ mx = i; } } cout << mx << endl; }
#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 mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } } c(200005); void rot(vector<vector<int>>& s){ int h=s.size(),w=s[0].size(); vector<vector<int>>t(w,vector<int>(h)); swap(s,t); rep(j,w){ rep(i,h){ s[j][i]=t[h-1-i][j]; } } } int main(){ int h,w;cin>>h>>w; vector<string>s(h); rep(i,h){cin>>s[i];} vector<vector<int>>a(h,vector<int>(w)); int cnt=0; vector<vector<int>>sum(h,vector<int>(w)); rep(i,h){ rep(j,w){ if(s[i][j]=='.'){a[i][j]=1;sum[i][j]=1;cnt++;} } } rep(z,4){ vector<vector<int>>res(h,vector<int>(w)); rep(i,h){ rep(j,w){ if(j&&a[i][j]&&a[i][j-1]){res[i][j]+=(res[i][j-1]+1);} sum[i][j]+=res[i][j]; } } rot(a); rot(sum); swap(h,w); } vector<mint>two(h*w+1,1); rep(i,h*w){two[i+1]=two[i]*2;} mint ans=0; rep(i,h){ rep(j,w){ ans+=two[cnt]-two[cnt-sum[i][j]]; } } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int mx=505; const int mod=998244353; int n,m,dp[mx][mx]; long long tmp=1; char a[mx][mx]; int main() { // freopen("data.in","r",stdin); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",a[i]+1); for(int i=1;i<=n;i++) { int col=0,col2=0; for(int j=0;i-j>=1&&j+1<=m;j++) { if(a[i-j][j+1]=='B') {col=1;} if(a[i-j][j+1]=='R') {col2=1;} } if(!col&&!col2) tmp*=2,tmp%=mod; else if(col&&col2) { printf("0"); return 0; } } for(int i=2;i<=m;i++) { int col=0,col2=0; for(int j=0;n-j>=1&&i+j<=m;j++) { if(a[n-j][i+j]=='B') {col=1;} if(a[n-j][i+j]=='R') {col2=1;} } if(!col&&!col2) tmp*=2,tmp%=mod; else if(col&&col2) { printf("0"); return 0; } } printf("%lld",tmp); }
#include <bits/stdc++.h> #define rep(i,n) for (ll i = 0; i < (ll)(n); i++) #define repr(i,a,b) for (ll i = (a); i < (ll)(b); i++) #define rng(x) (x).begin(), (x).end() #define rrng(x) (x).rbegin(), (x).rend() #define popcnt(x) __builtin_popcountll(x) #define pb push_back #define eb emplace_back #define fr first #define sc second using namespace std; template <class T, class U> inline bool chmax(T &a, const U &b) { return (a < b ? a = b, 1 : 0); } template <class T, class U> inline bool chmin(T &a, const U &b) { return (a > b ? a = b, 1 : 0); } template <class T> using vc = vector<T>; template <class T> using vv = vc<vc<T>>; template <class T> using vvv = vc<vv<T>>; template <class T> using PQ = priority_queue<T, vc<T>, greater<T>>; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>; using vi = vc<int>; using vl = vc<ll>; using vpl = vc<pll>; using vvi = vv<int>; using vvl = vv<ll>; const ld pi = 3.141592653589793; const ll linf = 1001001001001001001LL; const int inf = 1001001001; vi dx = {1, 0}, dy = {0, 1}; int main() { ll h, w; cin >> h >> w; vc<string> s(h); rep(i,h) cin >> s[i]; vvl dist(h, vl(w, linf)); dist[0][0] = 0; queue<pll> que; que.emplace(0, 0); while(!que.empty()) { auto [y, x] = que.front(); que.pop(); rep(i,2) { ll ny = y+dy[i], nx = x+dx[i]; if (0 <= ny && ny < h && 0 <= nx && nx < w && dist[ny][nx] == linf) { dist[ny][nx] = dist[y][x]+1; que.emplace(ny, nx); } } } vpl state(h+w-1); rep(i,h) rep(j,w) { auto &[R, B] = state[dist[i][j]]; if (s[i][j] != '.') (s[i][j] == 'R' ? R : B) += 1; } ll ans = 1; const ll mod = 998244353; rep(i,h+w-1) { auto [R, B] = state[i]; if (R >= 1 && B >= 1) ans *= 0; else if (R >= 1 || B >= 1) ans *= 1; else ans *= 2; ans %= mod; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define ppll pair<ll,pair<ll,ll>> #define pll pair<ll,ll> #define rep(i,x,y) for(ll i=x;i<=y;i++) #define inf 1e16 #define imin INT_MIN #define imax INT_MAX #define pb push_back #define pf push_front #define vll vector<ll> #define vvll vector<vector<ll>> #define vvb vector<vector<bool>> #define vb vector<bool> int mod =1e9+7; bool comp(pair<int,int> a,pair<int,int> b) { if(a.second == b.second) return a.first<b.first; return a.second<b.second; } void display(vll a) { for(auto& val: a) cout<<val<<' '; } ll power(ll a, ll b) { ll ans = 1; while(b) { if (b & 1) ans = (ans * a)%mod; a = (a * a) % mod; b >>= 1; } return ans; } const ll N = 1e5 + 1; ll F[N], inv[N]; void precompute(ll n) { F[0] = 1; inv[0] = 1; rep(i, 1, n) { F[i] = (F[i-1] * i) % mod; inv[i] = power(F[i], mod - 2); } } bool isPrime(ll n) { if(n<2) return false; for(int i=2;i<=sqrt(n);i++) { if(n%i==0) return false; } return true; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n,q; cin>>n>>q; vll a(n); for(auto& val: a) cin>>val; sort(begin(a), end(a)); vll b(n); rep(i, 0, n-1) { b[i] = a[i] - (i+1); } while(q--) { ll k; cin >> k; int idx = lower_bound(begin(b), end(b), k) - begin(b); if(idx < n) cout<<k + idx<<'\n'; else { cout<<(k + (a[n -1] - b[n-1]))<<'\n'; } } return 0; }
#include"iostream" #include"cstdio" #include"cmath" #include"cstring" #include"algorithm" #include"stack" #include"queue" using namespace std; #define read(x) scanf("%d",&x) #define readl(x) scanf("%lld",&x) #define ll long long #define ull unsigned long long #define MOD 1000000007 #define MAXN 200005 int n,q,c; int t,x,y; struct node { int ls,rs,sum; }a[MAXN*50]; int opt=0,rt[MAXN]; int fa[MAXN]; int getf(int u){return (fa[u]==u)?u:fa[u]=getf(fa[u]);} void update(int k){a[k].sum=a[a[k].ls].sum+a[a[k].rs].sum;} int build(int k,int l,int r,int x) { if(!k) k=++opt; if(l==r) { a[k].sum+=1; return k; } int mid=(l+r)>>1; if(x<=mid) a[k].ls=build(a[k].ls,l,mid,x); else a[k].rs=build(a[k].rs,mid+1,r,x); return update(k),k; } int merge(int u,int v,int l,int r) { if(!u||!v) return u|v; if(l==r) { a[u].sum+=a[v].sum; return u; } int mid=(l+r)>>1; a[u].ls=merge(a[u].ls,a[v].ls,l,mid); a[u].rs=merge(a[u].rs,a[v].rs,mid+1,r); return update(u),u; } void work(int x,int y) { int u=getf(x),v=getf(y); if(u==v) return; else { fa[v]=u; rt[u]=merge(rt[u],rt[v],1,200000); } } int query(int k,int l,int r,int x) { if(!k) return 0; if(l==r) return a[k].sum; int mid=(l+r)>>1; return (x<=mid)?query(a[k].ls,l,mid,x):query(a[k].rs,mid+1,r,x); } int main() { read(n),read(q); for(int i=1;i<=n;i++) { read(c),fa[i]=i; rt[i]=build(rt[i],1,200000,c); } for(int i=1;i<=q;i++) { read(t),read(x),read(y); if(t==1) work(x,y); else { int p=getf(x); printf("%d\n",query(rt[p],1,200000,y)); } } return 0; }
#include <iostream> using namespace std; int main() { long long b, c; cin >> b >> c; long long ans = 0; if (b == 0) { if (c == 0) ans = 1; else ans = c; } else if (0 < b) { if (c < 2 * b) ans = c + 1; else ans = 2 * b + 1; if (2 < c) ans += c - 2; } else { if (c == 0) ans = 1; else if (c == 1) ans = 2; else if (c == 2) ans = 3; else { if (c <= 2 * abs(b)) ans = c; else ans = 2*abs(b)+1; ans += c - 1; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 200005; typedef pair<long long, long long> pll; long long b, c; int solve() { scanf("%lld %lld", &b, &c); long long ans = 0; pll range1 = {b - c/2, b + max(0LL, c - 2) / 2}; pll range2 = {-b - (c - 1) / 2, -b + (c - 1) / 2}; // cerr << range1.first << " " << range1.second << endl; // cerr << range2.first << " " << range2.second << endl; if (max(range1.first, range2.first) <= min(range1.second, range2.second)) ans = max(range1.second, range2.second) - min(range1.first, range2.first) + 1; else ans = range1.second - range1.first + 1 + range2.second - range2.first + 1; printf("%lld\n", ans); return 0; } int main() { int t = 1; // scanf("%d", &t); for (int tc = 0; tc < t; ++tc) { solve(); } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #ifdef LOCAL #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) #else #define debug(...) ; #endif vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out( vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx, __attribute__ ((unused)) int LINE_NUM) { cerr << endl; } template<typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } typedef long long ll; typedef long double ld; #define rep(i, start, end) for(int i = start; i < end; ++i) #define sz(x) (int)((x).size()) #define pb push_back #define all(x) x.begin(), x.end() #define clr(d, v) memset(d, v, sizeof(d)) #define pii pair<int, int> #define X first #define Y second //#define debug(x) cerr << #x << " : " << (x) << " " const long double PI = 3.14159265358979323846; const long double eps = (1e-15); //enum //{ // LESS, EQUAL, GREATER //}; //int dcmp(ld x, ld y, ld EPS = eps) //{ // if (fabs(x - y) <= EPS) // return EQUAL; // return x > y ? GREATER : LESS; //} int n; const int MAX_N = 2e5 + 5; vector<pair<int, int> > adj[MAX_N]; int val[MAX_N]; enum {EQUAL, NOT_EQUAL}; void dfs(int u, int state, int num) { if (state == EQUAL) val[u] = num; else { val[u] = 1; if (num == 1) val[u]++; } for (auto e : adj[u]) { int v = e.first, c = e.second; if (val[v] != -1) continue; if (c == val[u]) dfs(v, NOT_EQUAL, c); else dfs(v, EQUAL, c); } } void myMain() { cin >> n; int m; cin >> m; for (int i = 0; i < m; ++i) { int u, v, c; cin >> u >> v >> c; --u; --v; adj[u].pb({v, c}); adj[v].pb({u, c}); } clr(val, -1); dfs(0, EQUAL, 1); for (int u = 0; u < n; ++u) cout << val[u] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int tc = 1; // cin >> tc; while(tc--) myMain(); }
#include <bits/stdc++.h> using namespace std; // type alias typedef long long LL; typedef pair<int,int> II; typedef tuple<int,int,int> III; typedef vector<int> VI; typedef vector<string> VS; typedef unordered_map<int,int> MAPII; typedef unordered_set<int> SETI; template<class T> using VV=vector<vector<T>>; // minmax template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; } template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; } // repetition #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) for(int i=0;i<=(n);++i) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) for(int i=0;i<(n);++i) #define FORR(x,arr) for(auto& x:arr) #define SZ(a) int((a).size()) // collection #define ALL(c) (c).begin(),(c).end() // DP #define MINUS(dp) memset(dp, -1, sizeof(dp)) #define ZERO(dp) memset(dp, 0, sizeof(dp)) // stdout #define println(args...) fprintf(stdout, ##args),putchar('\n'); // debug cerr template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; } void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; } template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); } template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; } #define __KUMATRACE__ true #ifdef __KUMATRACE__ #define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); } #define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; } #define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); } #else #define dump(args...) #define dumpc(ar) #define dumpC(beg,end) #endif struct UF { public: int N,G/* # of disjoint sets */; UF(int N): N(N) { init(N); } void init(int N) { par=vector<int>(N),sz=vector<int>(N,1); for(int i=0; i<N; ++i) par[i]=i,sz[i]=1; G=N; } int find(int x) { return par[x]==x?x:par[x]=find(par[x]); } int size(int x) { return sz[find(x)]; } bool sameset(int x, int y) { return find(x)==find(y); } int unite(int x, int y) { x=find(x),y=find(y); if(x==y) return x; int p=x,c=y; if(sz[p]<sz[c]) swap(p,c); G--,sz[p]=sz[c]=sz[p]+sz[c]; par[c]=p; return p; } private: vector<int> par,sz; }; // $ cp-batch KeepGraphConnected | diff KeepGraphConnected.out - // $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address KeepGraphConnected.cpp && ./a.out /* 11/21/2020 21:31- */ const int MAX_N=1e6+1; III E[MAX_N]; int N,M; vector<II> G[MAX_N]; void solve() { REP(i,M) { int u,v,c; tie(u,v,c)=E[i]; G[u].emplace_back(v,c),G[v].emplace_back(u,c); } VI res(N); VI viz(N); queue<int> Q; Q.emplace(0),viz[0]=1,res[0]=0; while(SZ(Q)) { int u=Q.front(); Q.pop(); FORR(p,G[u]) { int v,c; tie(v,c)=p; if(viz[v]) continue; int col; if(res[u]!=c) col=c; else col=(c+1)%N; res[v]=col,viz[v]=1,Q.emplace(v); } } UF uf(N); REP(i,M) { int u,v,c; tie(u,v,c)=E[i]; if(res[u]==c&&res[v]!=c) uf.unite(u,v); else if(res[v]==c&&res[u]!=c) uf.unite(u,v); } assert(uf.G==1); REP(i,N) cout<<res[i]+1<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout<<setprecision(12)<<fixed; cin>>N>>M; REP(i,M) { int u,v,c; cin>>u>>v>>c; --u,--v,--c; E[i]={u,v,c}; } solve(); return 0; }
// author: erray #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } if (n % 2 == 1) { cout << "Second"; } else { sort(a.begin(), a.end()); bool ok = true; for (int i = 0; i < n; i += 2) { ok &= (a[i] == a[i + 1]); } cout << (ok ? "Second" : "First"); } cout << '\n'; } }
// Problem : D - Let's Play Nim // Contest : AtCoder - AtCoder Regular Contest 105 // URL : https://atcoder.jp/contests/arc105/tasks/arc105_d // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; //krishrawat //----------------------------MACROS------------------------------------------------- #define pb insert// | #define eb emplace_back// | #define ff first// | #define ss second// | typedef long long int ll;// | typedef long double ld;// | #define all(a) a.begin(), a.end()// | #define show(x) cerr << #x << " is " << x << "\n"// | #define show2(x, y) cerr << #x << " " << #y << " " << x << " " << y << "\n"// | typedef vector<ll> vl;// | typedef vector<vl> vvl;// | typedef pair<ll, ll> pp;// | typedef vector<pp> vp;// | typedef map<pp, ll> ipi;// | typedef map<pp, char> ipc;// | typedef map<ll, ll> ii;// | typedef set<ll> sl;// | typedef multiset<ll> msl;// | typedef map<char, ll> ci;// | typedef set<pair<ll, ll>> sp;// | const ll mod = 1e9 + 7;// | const ll N = 1e6 + 1;// | //---------------------------------------------------------------------------------- void solve(){ ll n;cin>>n; ii m; for(int i=0;i<n;i++){ ll k;cin>>k; m[k]++; } int f=0; for(auto i:m){ if(i.ss%2) f=1; } if(n%2){ f=!f; } if(!f) cout<<"Second\n"; else cout<<"First\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); ll t;cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define faster \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ff first #define ss second #define pb push_back #define gcd(a, b) __gcd(a, b) #define lcm(a, b) (a * (b / gcd(a, b))) #define all(a) a.begin(), a.end() #define mod 10000007 #define num 1000000 /**----data type----*/ typedef long long int ll; typedef unsigned long long int llu; int main() { faster int n,cnt=0; cin>>n; for(int i=1; i<=n; i++) { for(int j=1; i*j<=n; j++) { for(int k=1; i*j*k<=n; k++) { cnt++; // cout<<i<<' '<<j<<' '<<k<<endl; } } } cout<<cnt<<endl; }
#include <cstdio> long long two(int K) { long long ans = 0; for(int i = 1, j = 1; i <= K && j <= K; i = j+1, j = K/(K/i)) { ans += (j - i + 1) * (K/i); if(j == K) break; } return ans; } int main() { int K; scanf("%d", &K); long long ans = 0; for(int i = 1, j = 1; i <= K && j <= K; i = j+1, j = K/(K/i)) { ans += (j - i + 1ll) * two(K/i); if(j == K) break; } printf("%lld", ans); return 0; }
#include <iostream> #include <vector> #include <string> #include <math.h> /* MACROS START */ #define input std::cin #define for2(i,n) for(int i=0; i<n; i++) #define for3(i,a,n) for(int i=a; i<n; i++) #define for4(i,a,n,s) for(int i=a; i<n; i+=s) using lint = long long int; using namespace std; template <typename T> void printvec(T vec) { for (auto& a: vec) cout << a << " "; cout << endl; } /* MACROS END */ /* LIBRARY START */ /* LIBRARY END */ int main() { std::ios_base::sync_with_stdio(0); lint n; input >> n; lint lo = 0, hi = (lint) 1e10; while (lo < hi-1) { lint mid = (lo+hi)/2; if ((mid * (mid+1))/2 <= n+1) lo = mid; else hi = mid; } lint k = lo; /* cout << k << endl; */ cout << n-k + 1 << endl; }
#include<bits/stdc++.h> #define int long long int using namespace std; int32_t main() { int a; cin >> a; int now = 0, ind = 1; while(now + ind <= a + 1) { now += ind; ind ++; } cout << a + 1 - ind + 1 << '\n'; return 0; }
//bismillahir rahmanir rahim //Author:Fayed Anik #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> using namespace std; //using namespace __gnu_pbds; //#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define ll long long #define pb(x) push_back(x) #define ull unsigned long long #define FOR(x,n) for(ll x=1;x<=n;++x) #define pii pair< ll , ll > #define mp(a,b) make_pair(a,b) #define UNIQUE(v) v.resize(distance(v.begin(),unique(v.begin(),v.end()))) #define mod 1000000007 #define INF 2e18 #define EPS 1e-15 #define f1 first #define f2 second #define all(v) v.begin(),v.end() #define PI acos(-1) #define printminusone printf("-1") #define bug printf("bug") #define FILEIN freopen("in.txt","r",stdin) #define FILEOUT freopen("out.txt","w",stdout) #define FASTREAD ios_base::sync_with_stdio(0);cin.tie(nullptr); #define endl "\n" //ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); } //ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); } bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); } //priority_queue <ll, vector<ll>, greater<ll> > pq; //int dx[]={0,0,1,-1,1,1,-1,-1}; //int dy[]={1,-1,0,0,1,-1,1,-1}; ll a[45]; int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); FASTREAD ll n,lim,T,now,ans=0,fh,sh; cin>>n>>T; fh = n/2; sh = n/2; if( !(n&1) ) fh--; if( n&1 ) sh++; for(ll i=0;i<n;i++) cin>>a[i]; vector<ll>fst,sec; lim = 1LL<<(fh+1); for(ll mask=0;mask<lim;mask++){ ll sum=0; for(ll i=0;i<=fh;i++){ if( CHECK(mask,i) ) sum += a[i]; } if( sum<=T ) ans = max(ans,sum); fst.pb(sum); } lim = 1LL<<(n-sh); for(ll mask=0;mask<lim;mask++){ ll sum=0; for(ll i=sh;i<n;i++){ if( CHECK(mask,i-sh) ) sum += a[i]; } if( sum<=T ) ans = max(ans,sum); sec.pb(sum); } sort(all(sec)); for(ll i=0;i<fst.size();i++){ ll rem = T-fst[i]; if( rem<0 ) continue; now = fst[i]; ll lw=0,hi=sec.size()-1,mid,pos=-1; while(lw<=hi){ mid=(lw+hi)/2; if( sec[mid]<=rem ){ pos = mid; lw = mid+1; } else{ hi = mid-1; } } if( pos!=-1 ) now += sec[pos]; ans = max(ans,now); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template<typename T> void out(T x) { cout << x << endl; exit(0); } #define watch(x) cout << (#x) << " is " << (x) << endl using ll = long long; const int maxn = 55; int n; ll x; ll a[maxn]; ll b[maxn]; ll k[maxn]; ll dp[maxn][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>x; for (int i=1; i<=n; i++) { cin>>a[i]; } for (int i=1; i<n; i++) { b[i] = a[i+1]/a[i]; } for (int i=n; i>=1; i--) { k[i] = x/a[i]; x %= a[i]; } if (n==1) { out(1); } dp[1][0] = 1; if (k[1]!=0) { dp[1][1] = 1; } for (int i=2; i<n; i++) { dp[i][0] = dp[i-1][0]; if (k[i]!=0) { //use a[i] in remainder, initiate carrying dp[i][1] += dp[i-1][0]; } if (k[i]!=b[i]-1) { // let the previous carrying end here dp[i][0] += dp[i-1][1]; } dp[i][1] += dp[i-1][1]; } cout<<(dp[n-1][0]+dp[n-1][1])<<endl; return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } #define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0) int N; int normal(int x) { int t = 20; while (x >= N) { x &= ~(1<<t); t--; } return x; } void MAIN() { scanf("%d", &N); REP (i, N) { int a = normal(i * 2); int b = normal(i * 2 + 1); printf("%d %d\n", a+1, b+1); } } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#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() const int N=50; int t[N][N],s[N][N],sy,sx,highscore,dy[4]={-1,0,0,1},dx[4]={0,-1,1,0}; string highans; using B=bitset<1500>; const double TIMELIMIT=1.95*CLOCKS_PER_SEC; clock_t start; void dfs(int score,B used,int y,int x,string ans){ if(clock()-start>TIMELIMIT){ cout<<highans<<endl; exit(0); } REP(i,4){ int Y=y+dy[i],X=x+dx[i]; if(Y<0||Y>=N||X<0||X>=N||used[t[Y][X]])continue; dfs(score+s[Y][X],used|(B(1)<<t[Y][X]),Y,X,ans+"ULRD"[i]); } if(score>highscore){ highscore=score; highans=ans; } } signed main(){ start=clock(); cin>>sy>>sx; REP(i,N)REP(j,N)cin>>t[i][j]; REP(i,N)REP(j,N)cin>>s[i][j]; dfs(0,B(1)<<t[sy][sx],sy,sx,""); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using vl = vector<ll>; using vvl = vector<vl>; using pl = pair<ll, ll>; const ll INF = ll(1e18); const ll mod = ll(998244353); const double pi = acos(-1); #define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i)) #define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i)) #define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i)) #define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i)) #define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i)) #define rnfor(i,a,b) for(ll (i) = (a) - 1; (i) >= (b); --(i)) #define pf(x) cout << (x) << endl #define all(x) (x).begin(),(x).end() #define yes pf("Yes") #define no pf("No") 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; } const int dx[8]={1,0,-1,0,1,1,-1,-1}; const int dy[8]={0,1,0,-1,1,-1,1,-1}; int multipf(vector<string>& s){ if(s.size() == 0)return 0; cout << s[0]; rep1(i, s.size() - 1)cout << " " << s[i]; cout << endl; return 0; } int multipf(vector<ll>& n){ if(n.size() == 0)return 0; cout << n[0]; rep1(i, n.size() - 1)cout << " " << n[i]; cout << endl; return 0; } ll gcd(ll a,ll b){ if(a < b)swap(a, b); if(b == 0) return a; return gcd(b,a%b); } ll lcm(ll a,ll b){ ll g = gcd(a,b); return a / g * b; } ll factorial(ll n){ ll ans = 1; rep1(i, n){ ans *= i; ans %= mod; } return ans; } ll power(ll a, ll b, ll m){ ll ans = 1; while(b) { if(b & 1LL) ans = ans * a % m; ans %= m; a = a * a; a %= m; b >>= 1; } return ans % m; } vector<bool> fcolor(100000,false),f(100000,false); vl color(100000); vvl g(100000,vl(0)); vl ans(0); int dfs(ll n){ f[n] = true; bool ff = false; if(!fcolor[color[n]]){ ans.push_back(n + 1); ff = true; } fcolor[color[n]] = true; rep0(i, g[n].size()){ if(!f[g[n][i]])dfs(g[n][i]); } if(ff)fcolor[color[n]] = false; return 0; } //modを確認すること int main() { ll n; cin >> n; rep0(i, n)cin >> color[i]; rep0(i, n - 1){ ll a,b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } dfs(0); sort(all(ans)); rep0(i, ans.size())pf(ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll,ll> #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) #define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I) #define TO(x,t,f) ((x)?(t):(f)) #define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted #define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted #define REV(x) (reverse(x.begin(),x.end())) //reverse ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);} ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);} #define NEXTP(x) next_permutation(x.begin(),x.end()) const ll INF=ll(1e16)+ll(7); const ll MOD=1000000007LL; #define out(a) cout<<fixed<<setprecision((a)) //tie(a,b,c) = make_tuple(10,9,87); #define pop_(a) __builtin_popcount((a)) ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;} #define num_l(a,v) POSL(a,v) //v未満の要素数 #define num_eql(a,v) POSU(a,v) //v以下の要素数 #define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数 #define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数 // static_cast< long long ll > int main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m; cin >> n >> m; vector<int> A(n); FOR(i,0,n){ cin >> A[i]; } map<int,int> in,no; FOR(i,0,n+1)no[i] = 1; int ans = n; FOR(i,0,m){ in[ A[i] ]++; if(in[A[i]] == 1){ no.erase(A[i]); } } ans = min( ans, (*no.begin()).first ); FOR(i,m,n){ in[A[i]]++; if(in[A[i]] == 1){ no.erase(A[i]); } in[A[i-m]]--; if(in[A[i-m]] == 0){ no[A[i-m]] = 1; } ans = min( ans, (*no.begin()).first ); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; ll solve(ll y, ll x, unordered_map<ll, ll> &mp) { if(mp.find(y)!=mp.end()) return mp[y]; if(x>=y) return x-y; if(y%2==0) { ll temp = min(1 + solve(y/2, x, mp), y-x); mp[y] = temp; return temp; } else { ll temp = min(2 + min(solve((y-1)/2, x, mp), solve((y+1)/2, x, mp)), y-x); mp[y] = temp; return temp; } } int main() { ll x, y; cin>>x>>y; unordered_map<ll, ll> mp; cout<<solve(y, x, mp)<<endl; }
#include <bits/stdc++.h> //#include <chrono> #pragma GCC optimize("Ofast") using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() using ll = long long; using vec = vector<ll>; using mat = vector<vec>; ll N,M,H,W,Q,K,A,B; string S; using P = pair<ll, ll>; const ll INF = (1LL<<60); template<class T> bool chmin(T &a, const T &b){ if(a > b) {a = b; return true;} else return false; } template<class T> bool chmax(T &a, const T &b){ if(a < b) {a = b; return true;} else return false; } template<class T> void my_printv(std::vector<T> v,bool endline = true){ if(!v.empty()){ for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" "; std::cout<<v.back(); } if(endline) std::cout<<std::endl; } ll dfs(ll d, int num, bool minus, mat &dp, mat &visited){ assert(d < (1LL<<(num + 1))); if(num < 0) return (d == 0 ? 0 : INF); if(visited[num][minus]) return dp[num][minus]; visited[num][minus] = true; ll &res = dp[num][minus]; if((d>>num)&1) { chmin(res, dfs(d - (1LL << num), num - 1, minus, dp, visited) + 1); }else { if(d != 0) chmin(res, dfs((1LL << num) - d, num - 1, !minus, dp, visited) + 1); chmin(res, dfs(d, num - 1, minus, dp, visited)); } return res; } ll solve(int num){ ll res = num; ll from = N<<num, to = K, dif = abs(from - to), plus_max = 1LL<<num; res += dif / plus_max; dif %= plus_max; mat dp(num, vec(2, INF)), visited(num, vec(2, false)); assert(dif < (1LL<<num)); return res + dfs(dif, num - 1, false, dp, visited); } void solve(){ ll res = abs(N - K); for(int i = 0; (N<<i) < (1LL<<62); ++i){ chmin(res, solve(i)); } cout<<res<<endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin>>N>>K; solve(); }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> bata; vector<int> dscore, nscore; vector<int> stsize; void getScores(int v) { nscore[v] = 1; dscore[v] = 0; stsize[v] = 1; if (bata[v].size() == 0) { // printf("%d d n s %d %d %d\n", v, dscore[v], nscore[v], stsize[v]); return; } // dckids are all disadvantaged vector<int> dckids, ndckids; auto take = [&] (int c, int curDecider) { // 0 = the initial decider, 1 = the initial non-decider (curDecider == 0 ? dscore : nscore)[v] += dscore[c]; (curDecider == 1 ? dscore : nscore)[v] += nscore[c]; }; for (int c : bata[v]) { getScores(c); stsize[v] += stsize[c]; if (stsize[c] % 2 == 0) { if (dscore[c] >= nscore[c]) { // decider pre-takes this take(c, 0); } else { dckids.push_back(c); } } else { ndckids.push_back(c); } } sort(begin(ndckids), end(ndckids), [&] (int i, int j) { return dscore[i] - nscore[i] > dscore[j] - nscore[j]; }); int curPlayer = 0; for (int c : ndckids) { take(c, curPlayer); curPlayer = 1 - curPlayer; } for (int c : dckids) take(c, curPlayer); // printf("%d d n s %d %d %d\n", v, dscore[v], nscore[v], stsize[v]); } int main() { int n; scanf("%d", &n); dscore = nscore = stsize = vector<int>(n, -1); bata.assign(n, vector<int>()); for (int i = 1; i < n; ++i) { int p; scanf("%d", &p); bata[p-1].push_back(i); } getScores(0); printf("%d\n", nscore[0]); return 0; }
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <numeric> #include <functional> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for(ll i = 0; i < n; i++) #define exrep(i, a, b) for(ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; ll n; vvl G; vl dp; vl Size; // Size[v] : vの部分木のサイズ void dfs1(ll v, ll p = -1) { Size[v] = 1; for(ll u : G[v]) { if(u == p) { continue; } dfs1(u, v); Size[v] += Size[u]; } } void dfs2(ll v, ll p = -1) { dp[v] = 1; ll x = 0; vl vec; for(ll u : G[v]) { if(u == p) { continue; } dfs2(u, v); if(Size[u]%2 == 0) { if(dp[u] < 0) { dp[v] += dp[u]; } else { x += dp[u]; } } else { vec.pb(dp[u]); } } sort(all(vec)); ll k = vec.size(); rep(i, k) { if(i%2 == 0) { dp[v] += vec[i]; } else { dp[v] -= vec[i]; } } if(k%2 == 0) { dp[v] += x; } else { dp[v] -= x; } } int main() { cin >> n; G.resize(n); exrep(i, 1, n-1) { ll p; cin >> p; p--; G[i].pb(p); G[p].pb(i); } Size.resize(n); dfs1(0); dp.resize(n); dfs2(0); out((n + dp[0])/2); re0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; string s; cin>>s; int count=0; for(int i=0;i<s.length();i++){ int a=0,t=0,g=0,c=0; for(int j=i;j<s.length();j++){ switch(s[j]){ case 'A':a++; break; case 'T':t++; break; case 'G':g++; break; case 'C':c++; break; } if(a==t && g==c) count++; } //cout<<i<<" "<<a<<" "<<c<<" "<<g<<" "<<t<<endl; } cout<<count<<endl; return 0; }
#include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> #include <climits> #define rep(i, n) for(int i = 0; i < n; i++) #define per(i, n) for(int i = n - 1; i >= 0; i--) using ll = long long; #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mod 1000000007 using namespace std; template<class T, class U> T &chmax(T &a, const U &b){ if(a < b) a = b; return a; } template<class T, class U> T &chmin(T &a, const U &b){ if(a > b) a = b; return a; } int main(){ int n,m; cin >> n >> m; vector<pii> v(m); map<int, int> cnt; rep(i, m){ int x,y; cin >> x >> y; v[i] = {x, y}; cnt[x]++; } sort(all(v)); int left = 0, right = 0; set<int> s{n}; for(auto x : cnt){ vi a,b; right += x.second; for(int i = left; i < right; i++){ int y = v[i].second; if(s.count(y - 1) || s.count(y + 1)) a.push_back(y); else b.push_back(y); } for(int y : a) s.insert(y); for(int y : b) s.erase(y); left += x.second; } cout << (int)s.size() << "\n"; }
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> using namespace std; //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //using namespace __gnu_pbds; #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define pb push_back #define show(x) cout<<(#x)<<" : "<<x<<endl; #define ll long long #define ld long double #define fill(a,val) memset(a,val,sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<ll,ll> #define sq(x) ((x)*(x)) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define endl "\n" #define int long long #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n"; const ll MOD = 1000*1000*1000+7; const ll INF = 1ll*1000*1000*1000*1000*1000*1000 + 7; const ll MOD2 = 998244353; const ll N = 1000 * 100 + 10; const ll N2 = 70; const ld PI = 3.141592653589793; //template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);} ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;} ll lcm(ll a , ll b){return (a*b)/gcd(a,b);} signed main() { fastio(); //cout<<fixed<<setprecision(20); //CHECK for LONG LONG and LONG DOUBLE //*comment for all except cc/cf #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif//*/ int n; string s; cin>>n>>s; for(auto &x : s) { if(x=='A') x = '0'; if(x=='G') x = '1'; if(x=='C') x = '2'; if(x=='T') x = '3'; } int c[n][4]; fill(c,0); for (int i = 0; i < n; ++i) { c[i][s[i]-'0']++; } for (int i = 1; i < n; ++i) { for (int j = 0; j < 4; ++j) { c[i][j]+=c[i-1][j]; } } // cout<<s; int ans = 0; for(int i = 0 ; i < n ; i++) { for(int j = i+1 ; j < n ; j++) { int cc = c[j][2]; if(i) cc-=c[i-1][2]; int cg = c[j][1]; if(i) cg-=c[i-1][1]; int ct = c[j][3]; if(i) ct-=c[i-1][3]; int ca = c[j][0]; if(i) ca-=c[i-1][0]; if(ca==ct&&cg==cc) { ans++; } } } cout<<ans; printclock; return 0; }
#include<bits/stdc++.h> using namespace std; int n; #define Maxn 500010 #define cout cerr char ch[Maxn]; int S[Maxn],T[Maxn]; int seq[Maxn],cnt; int A[Maxn],a,B[Maxn],b; int main(){ scanf("%d",&n); scanf("%s",ch+1); for(int i=1;i<=n;++i)S[i]=ch[i]-'0'; scanf("%s",ch+1); for(int i=1;i<=n;++i)T[i]=ch[i]-'0'; long long Ans=0; for(int i=1;i<=n;++i){ if(S[i])A[++a]=i; if(T[i])B[++b]=i; } if(a<b){ puts("-1"); return 0; } if((a-b)&1){ puts("-1"); return 0; } int j=1; for(int i=1;i<=b;++i) if(A[j]<B[i]){ if(j>=a){ puts("-1"); return 0; } while(A[j]<B[i]){ if(j>=a){ puts("-1"); return 0; } Ans+=A[j+1]-A[j]; j+=2; } if(j>a){ puts("-1"); return 0; } Ans+=A[j]-B[i]; j++; }else{ Ans+=A[j]-B[i]; j++; } for(int i=j;i<=a;i+=2)Ans+=A[i+1]-A[i]; printf("%lld\n",Ans); return 0; }
#include<iostream> #include<vector> #include<string> #include<queue> #include<map> #include<algorithm> using namespace std; using ll = long long; using ld = long double; int main(){ ll l; cin >> l; ld prod = 1; for(int i=1;i <= 11;i++){ if(i == l-11) break; prod *= (l-i); prod /= i; } ll ans = (ll)prod; cout << ans << endl; return 0; }
/****************************** Author: jhnah917(Justice_Hui) g++ -std=c++14 -DLOCAL ******************************/ #include <bits/stdc++.h> #define x first #define y second #define all(v) v.begin(), v.end() #define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end()) using namespace std; typedef long long ll; int N; ll D[222][222]; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N; D[0][0] = 1; for(int i=1; i<=N; i++) D[i][0] = 1; for(int i=1; i<=N; i++){ for(int j=1; j<=i; j++) D[i][j] = D[i-1][j] + D[i-1][j-1]; } cout << D[N-1][11]; }
#include <bits/stdc++.h> using namespace std; bool ok(long long dx, long long dy, long long R) { return dx * dx + dy * dy <= R * R; } long long f(long long x, long long y, long long R, long long lim) { int l = 0, r = 1; long long res = 0; for (int i = int(1e9) + 50000; i >= lim; i -= 10000) { while (ok(r * 10000 - x, i - y, R)) r++; while (ok(l * 10000 - x, i - y, R)) l--; res += r - l - 1; } return res; } int main() { long double sx, sy, sr; cin >> sx >> sy >> sr; long long X = round(sx * 10000LL), Y = round(sy * 10000LL), R = round(sr * 10000LL); X %= 10000; Y %= 10000; long long ans = f(X, Y, R, 10000); ans += f(X, -Y, R, 0); cout << ans << endl; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstring> #include <iostream> #include <sstream> #include <numeric> #include <map> #include <set> #include <queue> #include <vector> using namespace std; typedef long long LL; typedef long double LD; const LD EPS = 1e-14; LL solve(long double X, long double Y, long double R) { LL ans = 0, cx = LL(round(X * 10000)), cy = LL(round(Y * 10000)), r = LL(round(R * 10000)); for (LL y = (cy - r) / 10000 - 1; y <= (cy + r) / 10000; ++y) { LL dy = y * 10000 - cy; if (dy * dy > r * r) continue; long double w = sqrt((long double)(r * r - dy * dy)); LL left = (cx - LL(w)) / 10000 - 2, right = (cx + LL(w)) / 10000 + 2; for (int i = 0; i < 4; ++i) { LL dx = left * 10000 - cx; if (dx * dx + dy * dy > r * r) { ++left; } dx = right * 10000 - cx; if (dx * dx + dy * dy > r * r) { --right; } } ans += max(0LL, right - left + 1); } return ans; } LL solve2(long double X, long double Y, long double R) { LL ans = 0; for (LL y = Y - R - 1; y <= Y + R + 1; ++y) { LD w = sqrt(max((LD)0.0, R * R - (y - Y) * (y - Y))); LL left = X - w - 1, right = X + w + 1; for (int i = 0; i < 2; ++i) { if (((left - X) * (left - X) + (Y - y) * (Y - y)) > R * R + EPS) { ++left; } if (((right - X) * (right - X) + (Y - y) * (Y - y)) > R * R + EPS) { --right; } } ans += max(0LL, right - left + 1); } return ans; } int main() { long double X, Y, R; std::cin >> X >> Y >> R; cout << solve(X, Y, R) << endl; #ifdef NDEBUG for (int t = 0; t < 10000000; ++t) { int x = (rand() % 10000) + 1; int y = (rand() % 10000) + 1; int r = (rand() % 10000) + 1; LD X = x * 0.0001; LD Y = y * 0.0001; LD R = r * 0.0001; X += rand() % 10; Y += rand() % 10; R += rand() % 10; LL a = solve(X, Y, R); LL b = solve2(X, Y, R); if (a != b) { cout << a << endl; cout << b << endl; cout << X << "," << Y << "," << R << endl; break; } } #endif return 0; }
//#include <bits/stdc++.h> #include <iostream> #include <iomanip> #include <math.h> #include <cmath> #include <algorithm> #include <climits> #include <functional> #include <cstring> #include <string> #include <cstdlib> #include <ctime> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <complex> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define itn int #define nit int #define ll long long #define ms multiset #define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i) #define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i) #define re register #define ri re int #define il inline #define pii pair<int,int> #define cp complex<double> //#pra gma G CC opti mize(3) using namespace std; using std::bitset; //using namespace __gnu_pbds; const double Pi=acos(-1); namespace fastIO { template<class T> inline void read(T &x) { x=0; bool fu=0; char ch=0; while(ch>'9'||ch<'0') { ch=getchar(); if(ch=='-')fu=1; } while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar(); if(fu)x=-x; } inline int read() { int x=0; bool fu=0; char ch=0; while(ch>'9'||ch<'0') { ch=getchar(); if(ch=='-')fu=1; } while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar(); return fu?-x:x; } char _n_u_m_[40]; template<class T> inline void write(T x ) { if(x==0){ putchar('0'); return; } T tmp = x > 0 ? x : -x ; if( x < 0 ) putchar('-') ; register int cnt = 0 ; while( tmp > 0 ) { _n_u_m_[ cnt ++ ] = tmp % 10 + '0' ; tmp /= 10 ; } while( cnt > 0 ) putchar(_n_u_m_[ -- cnt ]) ; } template<class T> inline void write(T x ,char ch) { write(x); putchar(ch); } } using namespace fastIO; const int MAXN=200002; int n,t1,t2,f[MAXN],dfn,ans[MAXN],d[MAXN]; vector<int>g[MAXN],c[MAXN]; bool vis[MAXN]; inline void make_tree(int pos){ vis[pos]=1;ri nxt; F(i,0,g[pos].size()-1){ nxt=g[pos][i]; if(!vis[nxt]){ f[nxt]=pos; c[pos].push_back(nxt); make_tree(nxt); } } } inline int play(int pos,int dp){ vis[pos]=1;ri rt=pos;d[pos]=dp; F(i,0,g[pos].size()-1) { if(!vis[g[pos][i]]){ ri nxt=play(g[pos][i],dp+1); if(d[nxt]>d[rt])rt=nxt; } }return rt; } inline void dfs(int pos){ ans[pos]=++dfn; F(i,0,c[pos].size()-1){ dfs(c[pos][i]);++dfn; } } #define mark vis inline bool cmp(int x,int y){ return mark[x]<mark[y]; } int main(){ cin>>n; F(i,2,n){ t1=read(),t2=read(); g[t1].push_back(t2); g[t2].push_back(t1); } memset(vis,0,sizeof(vis)); int p1=play(1,0); memset(vis,0,sizeof(vis)); int p2=play(p1,0); memset(vis,0,sizeof(vis)); make_tree(p1); memset(vis,0,sizeof(vis)); ri qwq=p2; while(qwq)mark[qwq]=1,qwq=f[qwq]; F(i,1,n)if(c[i].size()>1)sort(c[i].begin(),c[i].end(),cmp); dfs(p1); F(i,1,n)write(ans[i],' '); return 0; }
/* author:ryo3ihara        ”継続は力なり、雨だれ石を穿つ”           ”slow but steady wins the race” */ #pragma GCC optimize("Ofast") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; /* // 多倍長テンプレ #include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> namespace mp = boost::multiprecision; // 任意長整数型 using Bint = mp::cpp_int; // 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする) using Real = mp::number<mp::cpp_dec_float<1024>>; */ using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using pli = pair<ll, int>; using pii = pair<int, int>; using pld = pair<ll, ld>; using ppiii = pair<pii, int>; using ppiill = pair<pii, ll>; using ppllll = pair<pll, ll>; using pplii = pair<pli, int>; using mii = map<int, int>; using dll = deque<ll>; using qll = queue<ll>; using pqll = priority_queue<ll>; using pqrll = priority_queue<ll, vector<ll>, greater<ll>>; using vint = vector<int>; using vll = vector<ll>; using vpll = vector<pll>; using vvll = vector<vector<ll>>; using vvint = vector<vector<int>>; using vvpll = vector<vector<pll>>; //マクロ //forループ #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 ALL(x) x.begin(),x.end() #define SIZE(x) ll(x.size()) #define fs first #define sc second //定数 const ll MOD = 1000000007; const int inf = 1e9; const ll INF = 1e18; const ll MAXR = 100000; //10^5:配列の最大のrange inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; } inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; } //最大化問題最小化問題 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; } //高速冪乗化 modの時は一部入れ替える ll power(ll x, ll y) { if(y == 1) { return x; } ll ans; if(y % 2 == 1) { ll r = power(x,(y-1)/2); //ans = r * r % MOD; //ans = ans * x % MOD; ans = x * r * r; } else { ll r = power(x,y/2); //ans = r * r % MOD; ans = r * r; } return ans; } /* Bint powerb(Bint x, Bint y) { if(y == 1) { return x; } Bint ans; if(y % 2 == 1) { Bint r = powerb(x,(y-1)/2); //ans = r * r % MOD; //ans = ans * x % MOD; ans = x * r * r; } else { Bint r = powerb(x,y/2); //ans = r * r % MOD; ans = r * r; } return ans; } */ signed main(){ //入力の高速化用のコード ios::sync_with_stdio(false); cin.tie(nullptr); //入力 double Sx,Sy,Gx,Gy; cin >> Sx >> Sy >> Gx >> Gy; double ok = Sx,ng=Gx; double mid; REP(i,10000){ mid = ok + (ng-ok)/2; if((Sy/abs(Sx-mid))>(Gy/abs(Gx-mid))) ok = mid; else ng = mid; } //本文 //cout << ans << endl; //出力 //cout << ans << endl; cout << fixed << setprecision(10) << mid << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { // 入力 vector<int> taste(4); bool EQUAL = false; for(int i=0; i<4; i++){ cin >> taste.at(i); } // 入力終わり sort(taste.begin(),taste.end()); if(taste.at(0)+taste.at(3) == taste.at(1)+taste.at(2)){ EQUAL = true; }else if(taste.at(0)+taste.at(1)+taste.at(2) == taste.at(3)){ EQUAL = true; }else if(taste.at(0)+taste.at(1)==taste.at(2)+taste.at(3)){ EQUAL = true; } if(EQUAL) cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef queue<int> qi; typedef vector<int> vi; typedef vector<char> vc; typedef vector<ll> vll; typedef vector<pair<int, int>> vpii; typedef multiset<int> msi; typedef vector<vector<int>> vvi; typedef queue<int> di; typedef multimap<int, int> mmii; // ------------------------------------------------- // https://atcoder.jp/contests/abc198/tasks/abc198_c // ------------------------------------------------- int main() { ll r, x, y; cin >> r >> x >> y; ld d = sqrt(x * x + y * y); // cout << "d:" << d << ", r:" << r << endl; // ll ans = d==r ? 1 : (d!=r&&d<=2*r ? 2 : ceil((ld)d/r)); ll ans = d!=r&&d<=2*r ? 2 : ceil((ld)d/r); cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define arep(i,x,n) for(int i=int(x);i<(int)(n);i++) #define rep(i,n) for(long long i = 0;i < n;++i) #define rrep(i,n) for(int i=int(n-1);i>=0;i--) #define fs first #define sc second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define coy cout<<"Yes"<<endl #define con cout<<"No"<<endl #define pi 3.141592653589793 #define eps 0.00000001 #define INF 1e9+7 #define LINF 1e18+10 using ll = long long; using P = pair<int, int>; using lP = pair<ll, ll>; using fP = pair<double, double>; using PPI = pair<P, int>; using PIP = pair<int, P>; using Ps = pair<int, string>; using vi = vector<int>; using vl = vector<ll>; using vc = vector<char>; using vd = vector<double>; using vs = vector<string>; using vp = vector<P>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; using vvd = vector<vector<double>>; using vvc = vector<vector<char>>; using vvp = vector<vector<P>>; using vvb = vector<vector<bool>>; template <typename T> bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } //const ll mod=998244353; const ll mod = 1e9 + 7; const ll MAX = 300000; template <typename T> T abs(T a) { if (a < 0)return -a; else return a; }//2020/09/30 stdlib has abs(long) abs(long long) error ////////////////////////////////////// ll x,y,a,b; int main(){ cin>>x>>y>>a>>b; ll ans=0; y--; while(x<b&&x<y){ x*=a; ans++; } if(x>y){ cout<<ans-1<<endl; return 0; } ans+=(y-x)/b; cout<<ans<<endl; return 0; }
#include <iostream> #include <set> using namespace std; typedef long long ll; int main() { ll x, y, a, b; cin >> x >> y >> a >> b; ll exp = 0; while ((double)a*x < 2e18 && a*x <= x+b && a*x < y) { x = a * x; exp++; } if (x < y) { exp += (y - x - 1) / b; } cout << exp; }
#include <algorithm> #include <climits> // FOO_MAX, FOO_MIN #include <cmath> #include <cstdlib> // abs(int) #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; #define roundup(n, d) (((n) + ((d)-1)) / (d)) #define assign_max(into, compared) (into = max((into), (compared))) #define assign_min(into, compared) (into = min((into), (compared))) #define rep(i, n) for (long long i = 0; i < n; ++i) #define v_in(v) \ for (auto&& i : v) \ cin >> i #define vv_in(vv) rep(I, (long long)vv.size()) for (auto&& J : vv[I]) cin >> J #define FAST_IO \ ios::sync_with_stdio(false); \ cin.tie(nullptr); #define fix(n) fixed << setprecision(n) #define ALL(v) v.begin(), v.end() #define printvar(x) cout << #x << ":" << x << endl #define PI 3.14159265358979323846 using ll = long long; using ull = unsigned long long; using vll = vector<long long>; using vvll = vector<vector<long long>>; int main(void) { ll n, x; cin >> n >> x; ll x100 = x * 100; vvll in(n, vll(2)); vv_in(in); ll sum = 0; rep(i, n) { sum += in[i][0] * in[i][1]; if (sum > x100) { cout << i + 1 << endl; return 0; } } cout << -1 << endl; return 0; }
#include <iostream> #include <complex> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <numeric> #include <cstring> #include <ctime> #include <cstdlib> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <cmath> #include <bitset> #include <cassert> #include <queue> #include <stack> #include <deque> #include <random> using namespace std; template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;} template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;} #define files(FILENAME) read(FILENAME); write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 #define rank rank228 using ll = long long; using ld = long double; const string FILENAME = "input"; int n; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //read(FILENAME); cin >> n; string s, t; cin >> s >> t; int cnt = 0, cnt1 = 0; for (auto x: s) { if (x == '1') { cnt++; } } for (auto y: t) { if (y == '1') { cnt1++; } } if (cnt < cnt1) { cout << -1 << '\n'; return 0; } if (cnt % 2 != cnt1 % 2) { cout << -1 << '\n'; return 0; } vector<int> st; for (int i = 0; i < n; i++) { if (t[i] == '1') { st.pb(i); } } vector<int> st1; for (int i = 0; i < n; i++) { if (s[i] == '1') { st1.pb(i); } } int uk = 0; ll ans = 0; for (auto x: st) { while (true) { if (uk >= sz(st1)) { cout << -1 << '\n'; exit(0); } if (st1[uk] < x) { if (uk + 1 == sz(st1)) { cout << -1 << '\n'; exit(0); } ans += st1[uk + 1] - st1[uk]; uk+= 2; } else { ans += st1[uk] - x; uk++; break; } } } if (uk < sz(st1)) { for (int j = uk; j < sz(st1); j += 2) { ans += st1[j + 1] - st1[j]; } } cout << ans << '\n'; }
/*himanshukm2301*/ #include <bits/stdc++.h> using namespace std; #define hyperspeed \ ios::sync_with_stdio(0); \ cin.tie(0); #define INF 1000000000000000003 typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> pi; #define PB push_back #define PF push_front #define POB pop_back #define MP make_pair #define ff first #define ss second #define BS binary_search #define LB lower_bound #define UB upper_bound #define all(x) (x).begin(),(x).end() #define ln(x) (ll)x.length() #define sz(x) (ll)x.size() #define nl '\n' #define goog(x) cout<<"Case #"<<i<<": "; #define MAX 1000006 const unsigned int MOD = 1000000007; inline ll ceilM(ll a, ll b) { return (a / b) + ((a % b) != 0);} //O(1); ll gcd(ll a, ll b) {if (a == 0)return b; return gcd(b % a, a);} //O(log min(a, b)); bool isPrime(ll n) {if (n < 2) return 0; ll i = 2; while (i * i <= n) {if (n % i == 0) return 0; i++;} return 1;} //(O(sqrt(n))) ll arr[MAX]; void solve() { //cout << "hey" << endl; ll a, b; cin >> a >> b; double ans = (b * a) / 100.0; cout << fixed << setprecision(7) << ans << endl; } int main() { hyperspeed; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mp make_pair #define pb push_back #define ll long long #define ld long double #define debug(x) cout << '[' << #x << " is: " << x << "] " << endl; #define rsor(v) sort(v.rbegin() , v.rend()); #define rev(v) reverse(v.begin() , v.end()); #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() #define _cout(v) for(auto f : v ) cout << f << " " ; #define _cin(v) for(auto &it : v)cin >> it ; #define vi vector<int> #define fs first #define sc second const int mod =1e9+7; int32_t main() { IOS; int a,b,c; cin>>b>>c; if(b==0||c==0){ cout<<0; return 0; } cout<<fixed<<setprecision(5); cout<<(long double)(b*c)/100; }
#include <bits/stdc++.h> using i64 = long long; #define REP(i, n) for (int i = 0, REP_N_ = int(n); i < REP_N_; ++i) #define ALL(x) std::begin(x), std::end(x) template <class T> inline bool chmax(T &a, T b) { return a < b and ((a = std::move(b)), true); } template <class T> inline bool chmin(T &a, T b) { return a > b and ((a = std::move(b)), true); } template <typename T> std::istream &operator>>(std::istream &is, std::vector<T> &a) { for (auto &x : a) is >> x; return is; } using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); const int L = 100'000'000; int n, m; cin >> n >> m; vector<i64> w(n); cin >> w; vector<i64> vlmax(L + 1); i64 lmax = 0; i64 vmin = 1e9; REP(i, m) { i64 l, v; cin >> l >> v; chmax(vlmax[v], l); chmax(lmax, l); chmin(vmin, v); } if (vmin < *max_element(ALL(w))) { cout << -1 << endl; exit(0); } REP(i, L) { vlmax[i + 1] = max(vlmax[i], vlmax[i + 1]); } auto get_len = [&](i64 w) { if (w > L) return lmax; return vlmax[w - 1]; }; vector<int> camels(n); REP(i, n) { camels[i] = i; } i64 ans = (n - 1) * lmax; do { vector<i64> pos(n); pos[0] = 0; for (int i = 1; i < n; ++i) { i64 w_sub = w[camels[i]]; for (int j = i - 1; j >= 0; --j) { w_sub += w[camels[j]]; i64 len = get_len(w_sub); chmax(pos[i], pos[j] + len); } } chmin(ans, pos[n - 1]); } while (next_permutation(ALL(camels))); cout << ans << endl; }
#include <bits/stdc++.h> #define sz(a) a.size() #define pb(a) push_back(a) #define all(a) a.begin(),a.end() #define mem(a,b) memset(a,b,sizeof(a)) #define rep(i,a,b) for(int i=a;i<b;i++) using namespace std;typedef long long ll;const ll mod=1e9+7; #define inf 0x3f3f3f3f const int M=1e4+7,N=4e5+7; int T; int n,m; vector<int> va[N]; int cx[N],cy[N],dx[N],dy[N]; int u[N],v[N]; bool vis[N]; int nx,ny,k,dis; bool searchpath() { queue<int> q; dis=inf; memset(dx,-1,sizeof(dx)); memset(dy,-1,sizeof(dy)); for(int i=1;i<=nx;i++){ if(cx[i]==-1){ q.push(i); dx[i]=0; } } while(!q.empty()){ int u=q.front(); q.pop(); if(dx[u]>dis) break; for(int i=0,vv;i<va[u].size();i++){ vv=va[u][i]; if(dy[vv]==-1){ dy[vv]=dx[u]+1; if(cy[vv]==-1) dis=dy[vv]; else{ dx[cy[vv]]=dy[vv]+1; q.push(cy[vv]); } } } } return dis!=inf; } int find(int u) { for(int i=0,vv;i<va[u].size();i++){ vv=va[u][i]; if(!vis[vv]&&dy[vv]==dx[u]+1){ vis[vv]=1; if(cy[vv]!=-1&&dy[vv]==dis) continue; if(cy[vv]==-1||find(cy[vv])){ cy[vv]=u;cx[u]=vv; return true; } } } return false; } int MaxMatch(){ int res=0; memset(cx,-1,sizeof(cx)); memset(cy,-1,sizeof(cy)); while(searchpath()){ memset(vis,0,sizeof(vis)); for(int i=1;i<=nx;i++){ if(cx[i]==-1){ res+=find(i); } } } return res; } int main() { scanf("%d",&n); k=nx=ny=n; for(int i=1;i<=n;i++){ scanf("%d%d",&u[i],&v[i]); nx=ny=max(nx,max(u[i],v[i])); va[i].pb(u[i]); va[i].pb(v[i]); //va[u[i]].pb(v[i]); } printf("%d\n",MaxMatch()); return 0; } /*** 4 1 2 1 3 4 2 2 3 12 5 2 5 6 1 2 9 7 2 7 5 5 4 2 6 7 2 2 7 8 9 7 1 8 ***/
#include <bits/stdc++.h> using namespace std; struct dsuflipcard { vector<int> par, edgeincomp; dsuflipcard(int n) { par.assign(n + 1, -1); edgeincomp.assign(n + 1, 0); } int find(int u) { if (par[u] < 0) return u; return par[u] = find(par[u]); } void join(int u, int v) { u = find(u); v = find(v); if (u == v) { ++edgeincomp[u]; return; } par[u] += par[v]; par[v] = u; edgeincomp[u] += edgeincomp[v] + 1; } int takeflipcol(void) { int ans = 0; for (int i = 1; i < par.size(); ++i) { if (par[i] < 0) { ans += min(-par[i], edgeincomp[i]); } } return ans; } }; signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } const int maxn = 4e5 + 5; int n; cin >> n; dsuflipcard cards(maxn); while (n--) { int u, v; cin >> u >> v; cards.join(u, v); } cout << cards.takeflipcol(); }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++) #define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define vector1d(type,name,...) vector<type>name(__VA_ARGS__) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__))) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=(ll)1<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} int main(){ INT(a,b,x,y); int ans=0; if(a>b){ ans=min((a-b-1)*2*x+x,x+(a-b-1)*y); }else if(a==b){ ans=x; }else{ ans=min(2*x*(b-a)+x,(b-a)*y+x); } out(ans); }
#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<ll,ll>; const int INF=1001001001; const int mod=998244353; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } } c(2000005); int main() { int N,K; cin>>N>>K; vector<ll>A(N); rep(i,N){cin>>A[i];} vector<mint>sum(K+1); rep(i,N){ mint now=1; rep(j,K+1){ sum[j]+=now; now*=A[i]; } } for(int x=1;x<=K;x++){ mint ans=0; for(int i=0;i<=x;i++){ ans+=c(x,i)*sum[i]*sum[x-i]; } ans-=mint(2).pow(x)*sum[x]; cout<<ans/2<<endl; } return 0; }
#include <bits/stdc++.h> typedef long long int64; typedef unsigned int uint; typedef unsigned long long uint64; typedef double db; typedef long double db64; const int N = 2e5, K = 300, MOD = 998244353, I2 = (MOD + 1) / 2; int n, k, a[N + 5], c[K + 5][K + 5]; int64 s[K + 5]; int main() { scanf("%d%d", &n, &k); for (int i = 0; i <= k; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD; } } for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); for (int x = 1, j = 0; j <= k; j++, x = (int64)x * a[i] % MOD) { s[j] += x; } } for (int i = 0; i <= k; i++) { s[i] %= MOD; } int pw = 2; for (int i = 1; i <= k; i++) { int64 ans = 0; for (int j = 0; j <= i; j++) { ans += (int64)s[j] * s[i - j] % MOD * c[i][j] % MOD; } ans -= (int64)pw * s[i] % MOD; pw = (pw + pw) % MOD; ans %= MOD; printf("%lld\n", (ans * I2 % MOD + MOD) % MOD); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long N, X; //string S; cin >> N >> X; long long ans = X; for(int i=0; i<N; i++){ char c; cin >> c; if(c=='o'){ ans++; }else{ if(ans>0) ans--; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for (ll i = 0; i < (n); ++i) #define All(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() //reverse #define strall(v) (v).cbegin(),(v).cend() //const_itterator #define IN(a, b, x) (a<=x&&x<b) using namespace std; using ll = long long; using Pair = pair<int,int>; using Graph = vector<vector<ll>>; template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; template<typename t, typename u, typename Comp=less<>> bool chmax(t& xmax, const u& x, Comp comp={}) { if(comp(xmax, x)) { xmax = x; return true; } return false;} template<typename t, typename u, typename Comp=less<>> bool chmin(t& xmin, const u& x, Comp comp={}) { if(comp(x, xmin)) { xmin = x; return true;} return false;} const int INF = 1e9; const ll infl = ll(1e18)+5; int main(){ ll N,x; string s; int cnt1=0,cnt2=0; cin >> N >> x >> s; for(int i=0;i<s.size();i++){ if(s[i] == 'o') x++; else{ if(x == 0) continue; x--; } } cout << x << endl; }
#include <bits/stdc++.h> #define mp make_pair #define mt make_tuple #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define forn(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i) #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef long long i64; typedef vector<i64> vi64; typedef vector<vi64> vvi64; typedef pair<i64, i64> pi64; typedef double ld; template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; #ifdef LOCAL_DEFINE freopen("input.txt", "rt", stdin); #endif int n; cin >> n; vector<pii> a(n); // vector<pair<int, int> > forn(i, n) { // i = 0 .. n - 1 cin >> a[i].fi >> a[i].se; // a[i].first, a[i].second } vector<int> dp(n + 1); dp[0] = 1; forn(i, n) { // i = 0 .. n - 1 if (!dp[i]) continue; for (int x = 1; i + x <= n; ++x) { // (2 * i + 1, 2 * i + 1 + x), ... (2 * i + x, 2 * i + 2 * x) bool okBlock = true; for1(y, x) { // y = 1 .. x int v1 = 2 * i + y, v2 = v1 + x; bool ok = true; int fit = 0; forn(j, n) { if (a[j].fi == v2 || a[j].se == v1) ok = false; if (a[j].fi != v1 && a[j].se != v2) continue; if (a[j].fi == v1) { // (v1, -1) or (v1, v2) if (a[j].se != -1 && a[j].se != v2) ok = false; } if (a[j].se == v2) { if (a[j].fi != -1 && a[j].fi != v1) ok = false; } // (?, v1) if (ok) ++fit; } // (v1, -1), (-1, v2) okBlock &= ok && fit < 2; } if (okBlock) { // cerr << i << ' ' << x << '\n'; dp[i + x] = 1; } } } // O(n^4) cout << (dp[n] ? "Yes" : "No") << '\n'; #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define f first #define s second #define dbg(x) cout<< #x <<" "<<x<<"\n"; using namespace std; void solve(){ ll n; cin>>n; vector<string> v(n+1); vector<ll> dp(n+1,1); for(ll i=1; i<=n; ++i){ cin>>v[i]; } for(ll i=1; i<=n; ++i){ if(v[i]=="AND"){ dp[i] = dp[i-1]; } else{ dp[i] = dp[i-1] + (1LL<<i); } } cout<<dp[n]<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include "bits/stdc++.h" using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using vvvll = vector<vector<vector<ll>>>; using vvvvll = vector<vector<vector<vector<ll>>>>; using pl4 = pair<ll,ll>; using str = string; using vpl4 = vector<pair<ll,ll>>; #define sz size() #define be begin() #define en end() #define fi first #define se second #define pb push_back #define mp make_pair #define llin(x) ll (x);cin >>(x); #define stin(x) str (x);cin >>(x); #define vllin(x,n) vll (x)(n);FOR(i,0,n-1){cin >>(x)[i];} #define vllin2(a,b,n) vll (a)(n);vll (b)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];} #define vllin3(a,b,c,n) vll (a)(n);vll (b)(n);vll (c)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];} #define vlling(x,n) (x).assign(n,0);FOR(i,0,n-1){cin >>(x)[i];} #define vlling2(a,b,n) (a).assign(n,0);(b).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];} #define vlling3(a,b,c,n) (a).assign(n,0);(b).assign(n,0);(c).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];} #define vpl4in(x,n) vpl4 (x)((n),mp(0,0));FOR(i,0,n-1){cin >>x[i].fi>>x[i].se;} #define FOR(i,a,b) for(ll i = a ; i <= b ; i++) #define rFOR(i,b,a) for(ll i = a; i >= b ; i--) #define SORT(x) sort(x.be, x.en) #define rSORT(x) sort(x.rbegin(), x.rend()) #define say(x) cout<<(x); #define sal(x) cout<<(x)<<endl; #define says(x) cout<<(x)<<(' '); #define sas cout<<(' '); #define sayR(x) cout<<fixed<<setprecision(10)<<(x); #define salR(x) cout<<fixed<<setprecision(10)<<(x)<<endl; #define yn(a) cout <<((a)?"yes":"no")<<endl; #define Yn(a) cout <<((a)?"Yes":"No")<<endl; #define YN(a) cout <<((a)?"YES":"NO")<<endl; #define Imp(a) cout <<((a)?"Possible":"Impossible")<<endl; #define IMP(a) cout <<((a)?"POSSIBLE":"IMPOSSIBLE")<<endl; #define pow(a,b) ll(pow(a,b)) ll MOD=1000000007; ll INF=100000000000001; vector<ll> value; // ノードの値を持つ配列 ll N; // 葉の数 void update(ll i, ll x) { // i 番目の葉の値を x に変える i += N - 1; // i 番目の葉のノード番号 value[i] = x; while (i > 0) { i = (i - 1) / 2; // ノード i の親ノードの番号に変える value[i] = min(value[i * 2 + 1], value[i * 2 + 2]); // 左右の子の min を計算しなおす } } ll query(ll a, ll b, ll k, ll l, ll r) { // [a, b) の区間に対するクエリについて // ノード k (区間 [l, r) 担当)が答える if (r <= a || b <= l) return INF; // 区間が被らない場合は INF を返す if (a <= l && r <= b) return value[k]; // ノード k の担当範囲がクエリ区間 [a, b) // に完全に含まれる else { ll c1 = query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子に値を聞く ll c2 = query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子に値を聞く return min(c1, c2); // 左右の子の値の min を取る } } long long mi(long long a) { ll m=MOD; long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } signed main(){ llin(a); llin(b); llin(c); llin(d); sal(a*d-b*c) }
#include<bits/stdc++.h> using namespace std; #define ll long long #define yes {cout<<"Yes"<<endl;return 0;} #define no {cout<<"No"<<endl;return 0;} #define INF 2147483647 #define LINF 9223372036854775807 int DigitSum(int n1) { int digitsum; digitsum=0; while(n1!=0) { digitsum+=n1%10; n1=n1/10; } return digitsum; } int main() { int a,b,c,d; cin>>a>>b>>c>>d; cout<<a*d-c*b<<endl; }
#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; template <typename T> using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <class T> bool umin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool umax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } #define int long long #define ll long long #define ld long double #define pb push_back #define mp make_pair #define in insert #define fi first #define se second #define lb lower_bound #define ub upper_bound #define prec fixed << setprecision(20) #define sz(v) (int)(v.size()) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define mem(a, s) memset(a, s, sizeof(a)) #define rep(i, a, b) for (int i = a; i <= b; ++i) #define repr(i, a, b) for (int i = a; i >= b; --i) #define MAX 8000000000000000064LL #define MIN -8000000000000000064LL typedef vector<int> vi; typedef stack<int> sti; typedef queue<int> qi; typedef pair<int, int> pii; typedef set<int> si; typedef map<int, int> mii; typedef vector<pii> vpi; typedef priority_queue<int> pqmax; typedef priority_queue<int, vector<int>, greater<int> > pqmin; const int mod = 1e9 + 7; const int inf = 1e18; const int d4x[4] = { -1, 0, 1, 0 }, d4y[4] = { 0, 1, 0, -1 }; const int d8x[8] = { -1, -1, 0, 1, 1, 1, 0, -1 }, d8y[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; void solve(); int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; rep(i, 1, t) { // cout << "Case #" << i << ": "; solve(); } return 0; } void solve() { int n, m, t; cin >> n >> m >> t; int start = n; vector<pii> v(m); rep(i, 0, m - 1) cin >> v[i].fi >> v[i].se; start -= v[0].fi; if (start <= 0) { cout << "No\n"; return; } start += (v[0].se - v[0].fi); if (start > n) start = n; rep(i, 1, m - 1) { start -= (v[i].fi - v[i - 1].se); if (start <= 0) { cout << "No\n"; return; } start += (v[i].se - v[i].fi); if (start > n) start = n; } start -= (t - v[m - 1].se); if (start > 0) cout << "Yes\n"; else cout << "No\n"; } // solve
#include <bits/stdc++.h> using namespace std; const int maxm=1010; int N,n,m,t,a[maxm],b[maxm]; int main() { scanf("%d%d%d",&n,&m,&t);N=n; for(int i=1;i<=m;i++) scanf("%d%d",&a[i],&b[i]); a[0]=b[0]=0; a[m+1]=t; for(int i=1;i<=m+1;i++) { n-=a[i]-b[i-1]; if(n<=0) return !printf("No"); n+=b[i]-a[i]; n=min(n,N); } printf("Yes"); return 0; }
/*** keep hungry and calm PushyTao!***/ #include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <math.h> using namespace std; typedef long long ll; #define HEAP(...) priority_queue<__VA_ARGS__ > #define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > > template<class T> inline T min(T& x, const T& y) { return x > y ? y : x; } template<class T> inline T max(T& x, const T& y) { return x < y ? y : x; } ll read() { ll c = getchar(), Nig = 1, x = 0; while (!isdigit(c) && c != '-')c = getchar(); if (c == '-')Nig = -1, c = getchar(); while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar(); return Nig * x; } #define read read() const ll inf = 1e15; const ll INF = 0x3f3f3f3f; const int maxn = 1e6 + 7; const int mod = 1000000007; #define PI acos(-1.0) typedef int itn; namespace usual{ inline ll gcd(ll a, ll b) { ll t; while (b != 0) { t = a % b; a = b; b = t; } return a; } inline ll qPow(ll x, ll k) { ll res = 1; while (k) { if (k & 1) res = (res * x); k >>= 1; x = (x * x); } return res; } inline ll oula(ll x) { ll res = x; for (ll i = 2; i <= x / i; i++) { if (x % i == 0) { res = res / i * (i - 1); while (x % i == 0) x /= i; } } if (x > 1) res = res / x * (x - 1); return res; } } using namespace usual; priority_queue <int, vector<int>, greater<int> > xiaogen; priority_queue <int, vector<int>, less<int> > dagen; itn a[maxn],b[maxn]; ll cnt[maxn]; int main() { int n = read; for(int i=1;i<=n;i++){ a[i] = read; cnt[a[i]+200] ++; } ll ans = 0; for(ll i=0;i<=400;i++){ for(ll j=i+1;j<=400;j++){ ans += cnt[i] * cnt[j] * (i-j) * (i-j); } } cout<<ans<<endl; return 0; }/// ac_code /** **/
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) #define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i) #define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define popcount __builtin_popcount using ll = long long; constexpr ll mod = 1e9 + 7; constexpr ll INF = 1LL << 60; // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") 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 <typename T> T mypow(T x, T n, const T &p = -1) { //x^nをmodで割った余り T ret = 1; while (n > 0) { if (n & 1) { if (p != -1) ret = (ret * x) % p; else ret *= x; } if (p != -1) x = (x * x) % p; else x *= x; n >>= 1; } return ret; } using namespace std; // using namespace atcoder; void solve() { int n,m; cin>>n>>m; vector<ll>a(n); rep(i,n)cin>>a[i]; vector<vector<int>>g(n); rep(i,m){ int x,y; cin>>x>>y; x--;y--; g[x].eb(y); } vector<ll>dp(n,INF); ll ans=-INF; vector<bool>visited(n); auto dfs=[&](auto self,int v)->void{ visited[v]=true; for(auto x:g[v]){ chmin(dp[x],min(a[v],dp[v])); } }; rep(i,n){ dfs(dfs,i); } rep(i,n){ chmax(ans,a[i]-dp[i]); } cout<<ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; class mint{ static const int MOD=1e9+7; int x; public: mint():x(0){} mint(long long y){ x=y%MOD; if(x<0) x+=MOD; } mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; } mint& operator-=(const mint& m){ x-=m.x; if(x< 0) x+=MOD; return *this; } mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; } mint& operator/=(const mint& m){ return *this*=inverse(m); } mint operator+(const mint& m)const{ return mint(*this)+=m; } mint operator-(const mint& m)const{ return mint(*this)-=m; } mint operator*(const mint& m)const{ return mint(*this)*=m; } mint operator/(const mint& m)const{ return mint(*this)/=m; } mint operator-()const{ return -x; } friend mint inverse(const mint& m){ int a=m.x,b=MOD,u=1,v=0; while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); } return u; } friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=t; return is; } friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; } int to_int()const{ return x; } }; mint operator+(long long x,const mint& m){ return mint(x)+m; } mint operator-(long long x,const mint& m){ return mint(x)-m; } mint operator*(long long x,const mint& m){ return mint(x)*m; } mint operator/(long long x,const mint& m){ return mint(x)/m; } mint pow(mint m,long long k){ mint res=1; for(;k>0;k>>=1,m*=m) if(k&1) res*=m; return res; } mint fact(int n){ static vector<mint> memo={1}; if(memo.size()<=n){ int k=memo.size(); memo.resize(n+1); for(;k<=n;k++) memo[k]=memo[k-1]*k; } return memo[n]; } mint fact_inverse(int n){ static vector<mint> memo={1}; if(memo.size()<=n){ int k=memo.size(); memo.resize(n+1); memo[n]=inverse(fact(n)); for(int i=n;i>k;i--) memo[i-1]=memo[i]*i; } return memo[n]; } mint choose(int n,int k,int type=0){ if(k==0) return 1; if(n< k) return 0; if(type==0){ return fact(n)*fact_inverse(k)*fact_inverse(n-k); } else{ if(k>n-k) k=n-k; mint res=fact_inverse(k); rep(i,k) res*=n-i; return res; } } int k; mint pw[17][200001]; // pw[a][b] = a^b mint calc(vector<int> cnt,int rem){ int c=0; rep(i,16) if(cnt[i]>0) c++; if(c>k || k-c>rem) return 0; mint res=0; rep(i,k-c+1) res+=(i%2==0?1:-1)*choose(k-c,i)*pw[k-i][rem]; return choose(16-c,k-c)*res; } int main(){ string s; cin>>s>>k; int n=s.length(); int f[128]; rep(i,10) f['0'+i]=i; rep(i,6) f['A'+i]=10+i; rep(i,k+1) rep(j,n+1) pw[i][j]=pow(mint(i),j); mint ans=0; // leading zero なし vector<int> cnt(16); rep(i,n){ int d=f[s[i]]; rep(j,d) if(!(i==0 && j==0)) { cnt[j]++; ans+=calc(cnt,n-i-1); cnt[j]--; } cnt[d]++; } ans+=calc(cnt,0); // leading zero あり fill(cnt.begin(),cnt.end(),0); for(int i=1;i<n;i++){ for(int j=1;j<16;j++) { cnt[j]++; ans+=calc(cnt,n-i-1); cnt[j]--; } } cout<<ans<<'\n'; return 0; }
#include <iostream> #include <vector> #include<bits/stdc++.h> #include <algorithm> #include <math.h> #include <string> #include <iomanip> #include <string.h> using namespace std; bool used[1009]; int main(){ long long t; cin>>t; long long n; cin>>n; for(int i=1; i<=100; i++){ used[i*(100+t)/100] = true; } vector <long long> vec; for(int i=1; i<=100+t; i++){ if(used[i]==false){ vec.push_back(i); } } long long r1 = (n-1)/(long long)vec.size(); long long r2 = (n-1)%(long long)vec.size(); cout << r1*(100LL + t)+vec[r2] << "\n"; return 0; };
#include <iostream> #include <set> #include <algorithm> int main() { int N, M, tmp; std::cin >> N >> M; std::set<int> A, B, C, D, OUT; for (int i = 0; i < N; i++) { std::cin >> tmp; A.insert(tmp); } for (int i = 0; i < M; i++) { std::cin >> tmp; B.insert(tmp); } std::set_union(A.begin(), A.end(), B.begin(), B.end(), std::inserter(C, C.end())); std::set_intersection(A.begin(), A.end(), B.begin(), B.end(), std::inserter(D, D.end())); std::set_difference(C.begin(), C.end(), D.begin(), D.end(), std::inserter(OUT, OUT.end())); for (auto ite = OUT.begin(), end = OUT.end(); ite != end; ite++) { std::cout << *ite << " "; } std::cout << std::endl; }
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned long long #define pi 2*acos(0.0) #define readl(v,n) for(ll i=0;i<n;i++) {ll val; cin>>val; v.pb(val);} #define readi(v,n) for(int i=0;i<n;i++) {int val; cin>>val; v.pb(val);} #define srt(v) sort(v.begin(), v.end()); #define rsrt(v) sort(v.rbegin(), v.rend()); #define MIN(v) *min_element(v.begin(), v.end()) #define MAX(v) *max_element(v.begin(), v.end()) #define sz(x) ((ll) (x).size()) #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for(ll i = (a); i < (b); i++) #define rep2(i, a, b) for(ll i = (a); i <= (b); i++) #define vll vector <ll> #define vii vector <int> #define pii pair <int, int> #define pll pair <ll, ll> #define M 1000007 #define MOD 1000000007 #define pb push_back #define mp make_pair #define ff first #define ss second #define endl "\n" #define inf 1000000000000000000 #define ninf -1000000000000000000 #define deb(n) cout<<"--"<<(n)<<"--"<<endl #define debx(x) cout << #x << " = " << (x) << endl #define deb2(a,b) cout<<(a)<<"----"<<(b)<<endl #define debv(v) for(auto x: v) {cout<<x<<", ";} cout<<endl; #define put(n) cout<<(n)<<endl #define toDecimal(s) bitset<64>(s).to_ullong() #define toBinaryStr(n) bitset<64>(n).to_string() #define toBinary(n) stoll(bitset<64>(n).to_string()) /* ---- take notes here ---- */ void solve() { int n; double D,H; cin>>n>>D>>H; double maxx=0; for(int i=0; i<n; i++) { double d,h; cin>>d>>h; double x=D-d, y=H-h, a=d; double b=(y*a)/x; double b1=h-b; maxx=max(b1,maxx); } printf("%.4lf",maxx); } int main() { FAST int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int> ; #define rep(i,n) for(ll i=0;i<(n);i++) ll min_ll(ll a,ll b){return a>=b ? b:a;} ll max_ll(ll a,ll b){return a>=b ? a:b;} int main(){ int n; cin >> n; ll a[n]; rep(i,n){ ll m; cin >> m; if (i%2==0) a[i]=m; else a[i]=-m; } ll b[n+1]={0}; ll ans =0; map<ll,ll> d; d[0]=1; for (int i=0;i<n;++i){ b[i+1]=a[i]+b[i]; if (!d.count(b[i+1])) d[b[i+1]]=1; else d[b[i+1]]++; } for (auto i = d.begin();i!=d.end();++i){ ans+=i->second*((i->second)-1)/2; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <ll, ll> llll; const int Maxn = 300005; int n; map <ll, ll> M; ll res; int main() { scanf("%d", &n); llll cur = llll(0ll, 0ll); M[cur.first - cur.second]++; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); if (i % 2 == 0) cur.first += a; else cur.second += a; res += M[cur.first - cur.second]++; } cout << res << endl; return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define For(i, l, r) for (i = int64(l); i <= int64(r); i++) #define Fo(i, n) For(i, 1, n) #define Rof(i, r, l) for (i = int64(r); i >= int64(l); i--) #define Ro(i, n) Rof(i, n, 1) #define clr(a) memset(a, 0, sizeof(a)) #define cpy(a, b) memcpy(a, b, sizeof(b)) #define fc(a, ch) memset(a, ch, sizeof(a)) typedef long long int64; #define T1 template <class A> #define T2 template <class A, class B> #define T3 template <class A, class B, class C> #define T4 template <class A, class B, class C, class D> inline void read(char &x) { do x = getchar(); while (x <= ' '); } inline void read(char *s) { char ch; do ch = getchar(); while (ch <= ' '); while (ch > ' ') { (*s) = ch; s++; ch = getchar(); } (*s) = 0; } inline void read(std::string &s) { char ch; do ch = getchar(); while (ch <= ' '); while (ch > ' ') { s.push_back(ch); ch = getchar(); } } T1 inline void readint(A &x) { bool neg = false; char ch; do ch = getchar(); while (ch <= ' '); if (ch == '-') { neg = true; ch = getchar(); } x = 0; while (ch > ' ') { x = x * 10 + (ch & 15); ch = getchar(); } if (neg) x = -x; } inline void read(int &x) { readint(x); } inline void read(int64 &x) { readint(x); } T2 inline void read(A &a, B &b) { read(a); read(b); } T3 inline void read(A &a, B &b, C &c) { read(a); read(b); read(c); } T4 inline void read(A &a, B &b, C &c, D &d) { read(a); read(b); read(c); read(d); } inline void writeln() { putchar('\n'); } T1 inline void write(A x) { static char buf[20]; int top = 0; if (!x) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } while (x) { buf[++top] = (x % 10) | 48; x /= 10; } while (top) putchar(buf[top--]); } T1 inline void write_(A x) { write(x); putchar(' '); } T1 inline void writeln(A x) { write(x); putchar('\n'); } T2 inline void write(A a, B b) { write_(a); write(b); } T2 inline void writeln(A a, B b) { write_(a); writeln(b); } T3 inline void writeln(A a, B b, C c) { write_(a); write_(b); writeln(c); } T4 inline void writeln(A a, B b, C c, D d) { write_(a); write_(b); write_(c); writeln(d); } #undef T1 #undef T2 #undef T3 #undef T4 const int P[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; const int PN = sizeof(P) >> 2; const int full = (1 << PN) - 1; int64 a, b; int64 f[1 << PN | 5]; int main() { int64 i, ans; int j, x; read(a, b); f[0] = 1; For(i, a, b) { x = 0; For(j, 0, PN - 1) if(i % P[j] == 0) x |= 1 << j; Rof(j, full, 0) if(!(j & x)) f[j | x] += f[j]; } ans = 0; For(j, 0, full) ans += f[j]; writeln(ans); return 0; }
#include<iostream> using namespace std; int P[20]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71}; long A,B; long dp[1<<20]; main() { cin>>A>>B; dp[0]=1; for(long x=A;x<=B;x++) { int bit=0; for(int i=0;i<20;i++)if(x%P[i]==0)bit|=1<<i; for(int i=0;i<1<<20;i++)if(!(i&bit)) { dp[i|bit]+=dp[i]; } } long ans=0; for(int i=0;i<1<<20;i++)ans+=dp[i]; cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ostream &operator<<(ostream &os, const vector<lli> &x){ os << "{"; for(int i = 0; i < x.size(); i++){ if(i < x.size()-1) os << x[i] << ", "; else os << x[i]; } os << "}"; return os; } struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; const lli mod = 998244353; template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt< mod >; int main(void){ lli n; cin >> n; UnionFind u(n+1); vector<lli> f(n+1); for(int i = 1; i <= n; i++){ cin >> f[i]; u.unite(i, f[i]); } lli c = 0; vector<bool> seen(n+1); for(int i = 1; i <= n; i++){ if(seen[u.root(i)] == false){ seen[u.root(i)] = true; c++; } } modint ans = 2; ans = ans.pow(c)-1; cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long a, n, sum = 0, sumy = 0, amax = 0, ans = 0; cin >> n; for(int i = 1; i <= n; i++){ cin >> a; amax = max(a, amax); sum += a, sumy += sum; ans = sumy + amax * i; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #include <fstream> #include <bitset> using namespace std; using ll = long long; using P = pair<int, int>; const int INF=2147483647; // 9,223,372,036,854,775,807 const ll INFL = 9223372036854775807; bool func(const pair<ll,ll> &x,const pair<ll,ll> &y){ if(x.second<y.second) return true; else return false; } int main(){ ll n,k; cin >> n >> k; vector<deque<ll>> a_dequev(n+1); vector<ll> a_v(n); for(int i=0;i<n;i++){ cin >> a_v[i]; } sort(a_v.begin(),a_v.end()); for(int i=0;i<n;i++){ a_dequev[a_v[i]].push_back(a_v[i]); } ll sumpart = 0; ll ans = 0; int i = 0; int kchk = 0; for(int i=0;i<n;i++){ if(a_dequev[i].empty() == false){ a_dequev[i].pop_front(); sumpart++; } else{ if(i==0) break; kchk++; if(kchk>k) break; ans += sumpart; sumpart = 0 ; i = -1; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; int mod = 1e9 + 7; int mul(int a, int b) { ll ans = a * 1ll * b; return ans % mod; } int inv(int i) { int temp = mod - 2; int ans = 1; while (temp) { if (temp & 1) { ans = mul(ans, i); } temp >>= 1; i = mul(i, i); } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<int> a(n); int s = 0; for (int i = 0; i < n; i++) { cin >> a[i]; s += a[i]; } int t = m - s; if (t < 0) { cout << 0 << endl; return 0; } int up = 1, down = 1; for (int i = 1; i <= s + n; i++) { up = mul(i + t, up); down = mul(i, down); } int ans = mul(up, inv(down)); cout << ans << endl; return 0; }
#include <iostream> #include <set> #include <string> #include <vector> int main() { int n; std::cin >> n; std::string s, x; std::cin >> s >> x; std::vector<std::set<int>> dp(n+1); dp[n] = {0}; for (int i = n - 1; i >= 0; --i) { if (x[i] == 'A') { for (int j {0}; j < 7; ++j) { if (dp[i+1].count(((10 * j + s[i] - '0') % 7)) && dp[i+1].count((10 * j) % 7)) { dp[i].insert(j); } } } else { for (int j {0}; j < 7; ++j) { if (dp[i+1].count((10 * j + s[i] - '0') % 7) || dp[i+1].count((10 * j) % 7)) { dp[i].insert(j); } } } } if (dp[0].count(0)) { std::cout << "Takahashi" << std::endl; } else { std::cout << "Aoki" << std::endl; } }
#line 1 "/workspaces/compro/lib/template.hpp" #line 1 "/workspaces/compro/lib/io/vector.hpp" #include <iostream> #include <vector> #ifndef IO_VECTOR #define IO_VECTOR template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { int size = v.size(); for (int i = 0; i < size; i++) { std::cout << v[i]; if (i != size - 1) std::cout << " "; } return out; } template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) { for (auto &el : v) { std::cin >> el; } return in; } #endif #line 4 "/workspaces/compro/lib/template.hpp" #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) (v).begin(), (v).end() #define coutd(n) cout << fixed << setprecision(n) #define ll long long int #define vl vector<ll> #define vi vector<int> #define MM << " " << using namespace std; template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } // 重複を消す。計算量はO(NlogN) template <class T> void unique(std::vector<T> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } #line 2 "main.cpp" std::string solve(long long N, string S, std::string X) { vector<vector<bool>> dp(N + 1, vector<bool>(7, false)); dp[N][0] = true; for (int i = N - 1; i >= 0; i--) { REP(j, 7) { int m = 10 * j + (S[i] - '0'); // S[i]を選ぶ m %= 7; int m0 = 10 * j; // 0を選ぶ m0 %= 7; if (X[i] == 'T') { if (dp[i + 1][m0] || dp[i + 1][m]) { dp[i][j] = true; } else { dp[i][j] = false; } } else { // どっちに転んでも高橋君が勝つ場合 if (dp[i + 1][m0] && dp[i + 1][m]) { dp[i][j] = true; } else { dp[i][j] = false; } } } } if (dp[0][0]) { return "Takahashi"; } else { return "Aoki"; } } // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { int N; string S, X; cin >> N; cin >> S >> X; auto ans = solve(N, S, X); cout << ans << endl; }
/* 15 63 25 66 36 39 29 93 3 188 128 191 82 179 178 168 */ #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define trav(a, x) for(auto& a : x) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define endl '\n' typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vll; const ll mod = 1000000007; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); using State = pair<pii, bool>; vi a; // what cases do i have to handle? // if the mod = 0, need to not count degenerate one vi solve(vector<vector<vector<State>>>& prev, pii initial, bool ig) { vi ans; int whichDefault = (ig ? -1 : -2); bool once = false; while(initial != mp(-1, -1)) { int row = initial.first; int col = initial.second; //cout << row << " " << col << endl << flush; int len = sz(prev[row][col]); if(len + whichDefault >= 0) { // cout << len << " " << whichDefault << endl << flush; //assert(len + whichDefault < len); State taken = prev[row][col][len + whichDefault]; if(taken.second) { ans.pb(taken.first.first + 1); } initial = taken.first; if(!once) { whichDefault = -1; once = true; } } else { // take 0 State taken = prev[row][col][0]; // it means that to get to the current state, we took from the previous one if(taken.second) { ans.pb(taken.first.first + 1); } initial = taken.first; } } reverse(all(ans)); return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); // if there exists 2 different elements = x mod 200, then obv int n; cin >> n; a.resize(n); trav(x, a) cin >> x; map<int,vector<int>> cnt; for(int i = 0; i < n; i++) { cnt[a[i] % 200].pb(i); } trav(p, cnt) { if(sz(p.second) >= 2) { cout << "Yes" << endl; cout << 1 << " " << p.second[0] + 1 << endl; cout << 1 << " " << p.second[1] + 1 << endl; return 0; } } // includes the dummy subsequence vector<vector<ll>> dp(n + 1, vector<ll>(200, 0)); vector<vector<vector<State>>> prev(n + 1, vector<vector<State>>(200)); dp[0][0] = 1; prev[0][0].pb({mp(-1,-1), false}); for(int i = 0; i < n; i++) { for(int j = 0; j < 200; j++) { ll here = dp[i][j]; if(here != 0) { // include this element dp[i + 1][(j + a[i]) % 200] += here; if(prev[i + 1][(j + a[i]) % 200].size() <= 3) { prev[i + 1][(j + a[i]) % 200].pb({mp(i, j), true}); } dp[i + 1][j] += here; if(prev[i + 1][j].size() <= 3) { prev[i + 1][j].pb({mp(i,j), false}); } } } } vi b, c; for(int j = 0; j <= n; j++) { for(int i = 0; i < 200; i++) { if(i == 0) { if(dp[j][i] >= 3) { cout << "Yes" << endl; b = solve(prev, {n, i}, false); c = solve(prev, {n, i}, true); cout << sz(b) << " "; trav(x, b) cout << x << " "; cout << endl; cout << sz(c) << " "; trav(x, c) cout << x << " "; cout << endl; return 0; } } else { if(dp[j][i] >= 2) { cout << "Yes" << endl; b = solve(prev, {n, i}, false); c = solve(prev, {n, i}, true); cout << sz(b) << " "; trav(x, b) cout << x << " "; cout << endl; cout << sz(c) << " "; trav(x, c) cout << x << " "; cout << endl; return 0; } } } } cout << "No" << endl; return 0; }
//in dp prefix suffix sum helps.. #include<iostream> #include<vector> #include<string.h> #include<algorithm> #include<iomanip> #include<cmath> #include<stack> #include <iterator> #include <map> #include<list> #include <fstream> #include<unordered_map> #include<set> #include<queue> #define int long long #define double long double #define pb push_back #define mp make_pair #define pii pair<int,int> #define pip pair<int, pair<int, int> > #define viv vector<vector<int> > #define vip vector< pair<int, int> > #define vi vector<int> #define mii map<int, int> #define qu queue<int> #define set set<int> #define multi multiset<int> #define stack stack<int> #define fi first #define se second #define reverse(s) reverse(s.begin(), s.end()) #define sort1(v) sort(v.begin(), v.end()) #define it iterator #define pq1 priority_queue <pii, vector<pii>, greater<pii> > #define mem(x, y) memset(x, (int)y, sizeof(x)) #define ps(x,y) fixed<<setprecision(y)<<x // author :: Anurag Anand. using namespace std; int z= 1e9+7; //int z= 998244353; int gcd(int a, int b){ if(a==0)return b; if(b==0)return a; return gcd(b, a%b); } int power(int a,int b) { int res=1; while(b) { if(b&1) { res=(res*a)%z; b--; } else { a=(a*a)%z; b=b>>1; } } return res; } vi sieve; // vi seg; // void arrange(int i, int v, int x, int lx, int rx){ // if(rx-lx==1){ // seg[x] =v; // return; // } // int m= (lx+rx)/2; // if(i<m){ // arrange(i, v, 2*x+1, lx, m); // } // else arrange(i, v, 2*x+2, m, rx); // seg[x]=gcd(seg[2*x+1], seg[2*x+2]); // } // int calc(int l, int r, int x, int lx, int rx){ // if(l>=rx||r<=lx) return 0; // if(lx>=l&&rx<=r) return seg[x]; // int mid=(lx+rx)/2; // int sl= calc(l, r, 2*x+1, lx, mid), sr= calc(l, r, 2*x+2, mid, rx); // return gcd(sl, sr); // } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vi pp; pp.assign(1000001, 0); for(int i=2;i<1000;i++){ if(!pp[i]){ for(int j=2*i;j<=1000000;j+=i){ pp[j]=1; } } } for(int i=2;i<=1000000;i++) if(!pp[i]) sieve.pb(i); int t; t=1; //cin>>t; while(t--){ int n; cin>>n; set s; for(int i=2;i<=sqrt(n);i++){ int j=i; while(j<=n){ j*=i; if(j<=n) s.insert(j); } } cout<<n-s.size()<<"\n"; } }
#include <bits/stdc++.h> #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define rep(i, n) FOR(i,0,n) #define irep(i, n) IFOR(i,0,n) #define all(v) begin(v), end(v) using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using PLL = pair<ll, ll>; using VI = vector<int>; using VLL = vector<ll>; using VB = vector<bool>; using VP = vector<P>; using Graph2 = vector<vector<int>>; using PQ = priority_queue<int>; template<typename T>istream& operator>>(istream&s,vector<T>&v){for(auto &i: v)s>>i;return s;} 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; } bool second_compare(P a,P b){if(a.second!=b.second){return a.second<b.second;} else return true;} int main() { ll x, y, a, b; cin >> x >> y >> a >> b; ll ans = 0; while(1) { if ((double)x <= (double)(x + b)/a && x * a < y) { ans++; x *= a; } else break; } ans += (y-x) / b; if ((y-x) % b == 0) ans--; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define forn(i,x,n) for(int i = x;i <= n;++i) #define forr(i,x,n) for(int i = n;i >= x;--i) #define Angel_Dust ios::sync_with_stdio(0);cin.tie(0) const int N = 1005; struct Node { string name;int h; bool operator<(const Node& r) const { return h < r.h; } }a[N]; int main() { Angel_Dust; int n;cin >> n; forn(i,1,n) cin >> a[i].name >> a[i].h; sort(a + 1,a + n + 1); cout << a[n - 1].name << endl; return 0; }
#include <bits/stdc++.h> #include <variant> #define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++) #define rep(i,n) rep2(i,0,n) #define all(x) begin(x),end(x) #ifdef ENV_LOCAL #define dump if (1) cerr #else #define dump if (0) cerr #endif using namespace std; using namespace std::string_literals; using i32 = int32_t; using i64 = int64_t; using f64 = double; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; //using namespace harudake; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i32 n; cin>>n; vi64 ha(n+1); vi64 b(n), c(n); rep(i,n) { i32 a; cin>>a; ++ha[a]; } rep(i,n) cin>>b[i]; rep(i,n) cin>>c[i]; i64 ans = 0; rep(i,n) { ans += ha[b[c[i]-1]]; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define REP(i, e) for(int (i) = 0; (i) < (e); ++(i)) #define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i)) #define ALL(c) (c).begin(), (c).end() #define PRINT(x) cout << (x) << "\n" using namespace std; using ll = long long; using pll = pair<ll, ll>; const long long MOD = 1000000007; ll N, c[200010], q[200010]; pll edge[200010]; vector<ll> adj[200010]; ll depth[200010]; bool visited[200010]; void dep(ll i, ll d){ if(depth[i] != -1) return; depth[i] = d; for(ll j : adj[i]) dep(j, d + 1); } void dfs(ll i, ll s){ if(c[i] != -1) return; c[i] = s + q[i]; for(ll j : adj[i]) dfs(j, c[i]); } signed main(){ cin >> N; ll a, b; REP(i, N - 1){ cin >> a >> b; a--; b--; edge[i] = {a, b}; adj[a].push_back(b); adj[b].push_back(a); } REP(i, N) depth[i] = -1; dep(0, 0); ll Q, t, e, x; cin >> Q; REP(i, Q){ cin >> t >> e >> x; t--; e--; auto [a, b] = edge[e]; if(depth[a] > depth[b]){ swap(a, b); t = 1 - t; } if(t == 0){ q[0] += x; q[b] -= x; }else{ q[b] += x; } } REP(i, N) c[i]= -1; dfs(0, 0); REP(i, N) PRINT(c[i]); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 359; i < (int)(n); i++) int main () { int x,y; cin >> x >> y; if (x==y){cout<< x<<endl; return 0;} else { cout <<3-x-y <<endl; return 0;} }
#include<bits/stdc++.h> #define ll long long using namespace std; int main() { int a,b,c; cin>>a>>b; if(a==1 &&b==0||a==0&&b==1) cout<<2; else if(a==b) cout<<a; else if(a==1 &&b==2||a==2 &&b==1) cout<<0; else cout<<1; }
//#pragma GCC optimize("Ofast") //#pragma GCC optimize("O2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") //// //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #include <bits/stdc++.h> #include <utility> #define all(x) (x).begin(), (x).end() #define allp(x) (x)->begin(), (x)->end() #define pb push_back using namespace std; void dout() { cerr << endl; } //typedef long long ll; template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } using ll = long long; //#ifdef __int128 //using hll = __int128; //#endif using pii = pair<ll, ll>; using ld = long double; template <typename T> void do_uniq(vector<T> &vec){ sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin()); } #ifndef ONLINE_JUDGE clock_t timestamp_start = clock(); void time_calc() { cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n"; } #endif #ifdef _getchar_nolock #else # define _getchar_nolock getchar_unlocked #endif #define integer int integer mod = 1e9 + 7; integer ml(integer a, integer b) { return (a * 1ll * b) % mod; } integer add(integer a, integer b) { integer rt = a + b; if (rt < mod) return rt; return rt - mod; } integer sub(integer a, integer b) { return add(a, mod - b); } integer sq(integer a) { return ml(a, a); } integer b_p(integer b, ll p) { if (p == 0) return 1; if (p & 1) return ml(b, b_p(b, p - 1)); return sq(b_p(b, p >> 1)); } #define solvsh //#define multi #ifdef solvsh void precalc() { } void solve() { ll n, m; cin >> n >> m; mod = m; ll y = b_p(10, n); mod = m * m; ll val = b_p(10, n); val = sub(val, y); val /= m; cout << val << "\n"; } #else #endif void multisolve() { // precalc(); int t; cin >> t; int i = 1; while (t--) { solve(); i++; } } #ifndef ONLINE_JUDGE void gen() { } #endif #define int int int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cout << fixed << setprecision(20); #ifdef multi // gen(); multisolve(); #else solve(); // gen(); #endif // time_calc(); }
#include<bits/stdc++.h> using namespace std; using ll = long long; /*-----for personal-----*/ #define rep(i,a,b) for(int i=(a);i<(b);++i) #define repn(i,n) rep(i,0,n) /*-----for lib-----*/ #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) const double EPS = 1e-5; const int MOD = 1e9+7; const int INF = INT_MAX / 2; //for(auto &h : H){ cout << h << " ";} cout << endl; //for(auto &h : H){for(auto &k:h){cout << k << " ";}cout << endl;} //for(auto [k,v] : mp){cout << k << " " << v << endl;} //for(auto [k,v] : mp){cout << k << " "; for(auto &u:v){cout << u << " ";}cout << endl;} inline int32_t modinv_nocheck(int32_t value, int32_t MOD) { assert (0 <= value and value < MOD); if (value == 0) return -1; int64_t a = value, b = MOD; int64_t x = 0, y = 1; for (int64_t u = 1, v = 0; a; ) { int64_t q = b / a; x -= q * u; std::swap(x, u); y -= q * v; std::swap(y, v); b -= q * a; std::swap(b, a); } if (not (value * x + MOD * y == b and b == 1)) return -1; if (x < 0) x += MOD; assert (0 <= x and x < MOD); return x; } inline int32_t modinv(int32_t x, int32_t MOD) { int32_t y = modinv_nocheck(x, MOD); assert (y != -1); return y; } inline int32_t modpow(uint_fast64_t x, uint64_t k, int32_t MOD) { //assert (/* 0 <= x and */ x < (uint_fast64_t)MOD); uint_fast64_t y = 1; for (; k; k >>= 1) { if (k & 1) (y *= x) %= MOD; (x *= x) %= MOD; } //assert (/* 0 <= y and */ y < (uint_fast64_t)MOD); return y; } int main(){ ll N, M; cin >> N >> M; int a = modpow(10LL, N, M*M)/M; cout << a << endl; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using ld = long double; using vld = vector<ld>; using vb = vector<bool>; #define rep(i, n) for (ll i = 0; i < (n); i++) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif template <class T> bool chmin(T& a, T b) { if(a > b) { a = b; return true; } else return false; } template <class T> bool chmax(T& a, T b) { if(a < b) { a = b; return true; } else return false; } template <class T> ostream& operator<<(ostream& s, const vector<T>& a) { for(auto i : a) s << i << ' '; return s; } constexpr int INF = 1 << 30; constexpr ll INFL = 1LL << 62; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); struct mint { static const long long mod = 998244353; 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;} friend const mint operator+(const mint& a, const mint& b) { mint ret = a; return ret += b; } friend const mint operator-(const mint& a, const mint& b) { mint ret = a; return ret -= b; } friend const mint operator*(const mint& a, const mint& b) { mint ret = a; return ret *= b; } friend ostream& operator<<(ostream& s, const mint& a) { return s << a.x; } mint pow(long long 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();} friend const mint operator/(const mint& a, const mint& b) { mint ret = a; return ret /= b; } }; void solve() { ll n; cin >> n; vll a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); mint base = 1; vector<mint> b(n); b[0] = 1; for(int i = 1; i < n; ++i) { b[i] = base; base *= 2; } rep(i, n) b[i] *= a[i]; vector<mint> bs(n+1); // [l,r)のb[i]の和はbs[r]-bs[l] rep(i, n) bs[i+1] = bs[i] + b[i]; mint ans = 0; base = 1; rep(i, n) { ans += ((bs[n] - bs[i+1]) * base + a[i]) * a[i]; base /= 2; } cout << ans << endl; return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }
#include <bits/stdc++.h> using namespace std; const int P = 998'244'353; using ll = long long; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int a[n]; for (int &x : a) cin >> x; sort(a, a + n); ll s = 0, r = 0; for (int i = 0; i < n; ++i) { r += (s + a[i]) * a[i] % P; s = 2 * s + a[i]; s %= P; } cout << r % P << endl; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define int long long int #define popcount(x) __builtin_popcountll(x) #define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define sc second #define pb push_back #define fr first #define all(a) (a).begin(),(a).end() #define mem0(a) memset(a,0,sizeof(a)) #define ld long double const long long INF = __LONG_LONG_MAX__; #define rall(a) (a).rbegin(),(a).rend() #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroint-loops") int power(int a,int b,int m) { int res=1; while(b) { if(b&1) res=((res%m)*(a%m))%m; a=((a%m)*(a%m))%m; b>>=1; } return res; } int power(int a,int b) { int res=1; while(b) { if(b&1) res*=a; a*=a; b>>=1; } return res; } //a+b=a⊕b+2(a&b) //a+b=a|b+a&b //a⊕b=a|b−a&b void solve() { int n,c; cin>>n>>c; map<int,int>mp; for(int i=0;i<n;i++) { int a,b,c; cin>>a>>b>>c; mp[a]+=c; mp[b+1]-=c; } int ans=0,curr=0; pair<int,int> prev={-1,-1}; for(auto i:mp) { if(prev.fr==-1) { prev={i.fr,i.sc}; } else { curr+=prev.sc; int days=i.fr-prev.fr; ans+=min(c,curr)*days; prev={i.fr,i.sc}; } } cout<<ans; } signed main() { fastio int tt=1; // cin>>tt; // freopen("input.txt", "r" , stdin); // freopen("output.txt", "w", stdout); while(tt--) { solve(); cout<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD (long long int)(998244353) #define ll long long int #define rep(i,n) for(int i=0; i<(int)(n); i++) #define reps(i,n) for(int i=1; i<=(int)(n); i++) #define REP(i,n) for(int i=n-1; i>=0; i--) #define REPS(i,n) for(int i=n; i>0; i--) #define FOR(i,a,b) for(int i=a; i<(int)(b); i++) #define ALL(x) (x).begin(),(x).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset((a), 0 ,sizeof(a)) #define PB push_back #define MP make_pair #define SP << " " << const int INF = 1001001001; const ll LINF = 100100100100100100; const double EPS = 1e-10; const long double PI = acos(-1.0L); typedef pair<int,int> PII; typedef pair<ll,ll> PLL; typedef pair<double,double> PDD; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; #define chmax(a,b) a = (((a)<(b))?(b):(a)) #define chmin(a,b) a = (((a)>(b))?(b):(a)) __attribute__((constructor)) void initial(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } signed main() { ll n,p; cin>>n>>p; // VVL d(n); // rep(i,n){ // VL a(3); rep(j,3) cin>>a[j]; // d.PB(a); // } // sort(ALL(d)); // ll ans = 0LL, now = 0LL; // priority_queue<PLL,vector<PLL>,greater<PLL>> que; // ll today = 0LL; // ll yesterday = 0LL; // rep(i,n){ // today = a[i]; // if(now>p){ // ans += p*(today-yesterday); // }else{ // ans += now*(today-yesterday); // } // now += c[i]; que.push({b[i],c[i]}); // while(que.top().first<today){ // now -= que.top().second; // que.pop(); // } // } vector<PLL> d; rep(i,n){ ll a,b,c; cin>>a>>b>>c; d.PB({a,+c}); d.PB({b+1,-c}); } sort(ALL(d)); ll now=0LL, last=0LL; ll cost=0LL, ans=0LL; rep(i,n*2){ now=d[i].first; // cerr<<d[i].first SP d[i].second SP now SP last SP cost SP ans <<endl; if(d[i].second>0){ ans+=min(cost,p)*(now-last); cost+=d[i].second; }else{ ans+=min(cost,p)*(now-last); cost+=d[i].second; } last=now; } cout<<ans<<endl; 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> ostream &operator<<(ostream &os, multiset<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"; */ template<typename T, typename U> T modpow(T a, U n, int mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } char versus(const char&a, const char b){ char res = a; if(a == b)res = a; else{ if(a == 'R' && b == 'P')res = 'P'; if(a == 'P' && b == 'R')res = 'P'; if(a == 'P' && b == 'S')res = 'S'; if(a == 'S' && b == 'P')res = 'S'; if(a == 'S' && b == 'R')res = 'R'; if(a == 'R' && b == 'S')res = 'R'; } return res; } int N, K; string S; char dp[200][200]; // lenは2^lenになる char dfs(int st, int len){ if(dp[st][len] != 'X'){ return dp[st][len]; } else{ return dp[st][len] = versus(dfs(st, len-1), dfs((st + modpow(2, len-1, N))%N, len-1)); } } int main() { cin >> N >> K >> S; rep(i,200)rep(j,200)dp[i][j] = 'X'; rep(i,N){ dp[i][0] = S[i]; } cout << dfs(0, K) << endl; rep(i,N)rep(j,K){ dump(i, j, dp[i][j]); } }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; 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 #define PT pair<ll, int> int N, M, K; vector<int> E[100001]; int D[100001], C[100]; int DD[20][20]; int dp[20][1 << 17]; int sagasu(int p, int b) { if (dp[p][b]) return dp[p][b]; int ret = 1e9; rep(i, K) if(i != p && (b >> i & 1)) { chmin(ret, sagasu(i, b ^ (1 << p)) + DD[i][p]); } return dp[p][b] = ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; rep(i, M) { int a, b; cin >> a >> b; E[a].pb(b); E[b].pb(a); } cin >> K; rep(i, K) cin >> C[i]; rep(k, K) { rep1(i, N) D[i] = 1e9; priority_queue<PT, vector<PT>, greater<PT>> que; D[C[k]] = 0; que.push(mp(0, C[k])); while (que.size()) { PT p = que.top(); que.pop(); int i = p.second; if (D[i] != p.first) continue; for (int j : E[i]) { ll d = D[i] + 1; if (D[j] > d) { D[j] = d; que.push(mp(d, j)); } } } rep(p, K) { DD[k][p] = D[C[p]]; dp[p][1 << k | 1 << p] = DD[k][p] + 1; } } int kotae = 1e9; rep(i, K) chmin(kotae, sagasu(i, (1 << K) - 1)); if (kotae < 1e8) co(kotae); else co(-1); Would you please return 0; }
#include <bits/stdc++.h> using namespace std; using P = pair<int, long long>; int main() { long long n,m; long long a,b,c,d; cin >> n >> m; vector<vector<long long>> data; vector<vector<long>> path(n,vector<long>()); for(int i=0;i<m;i++) { cin >> a >> b >> c >> d; a--; b--; data.push_back({a,b,c,d,(long long)(sqrt(d*1.0)+0.5)-1}); path[a].push_back({i}); path[b].push_back({i}); } bitset<101010> flag; stack<int> st; st.push(0); int now, next; while(!st.empty()){ now = st.top(); st.pop(); flag[now]=true; for(int i=0;i<path[now].size();i++){ if(data[path[now][i]][0]==now) next = data[path[now][i]][1]; else next = data[path[now][i]][0]; if(!flag[next])flag[next]=1, st.push(next); } } if(!flag[n-1]) { cout << "-1" << endl; return 0; } vector<long long> dis(n, 1000000000000000LL); dis[0]=0; auto comp = [](P l, P r) { return l.second>r.second; }; priority_queue<P, vector<P>, decltype(comp)> q(comp); q.push({0,0}); pair<int,long long> u; long long tmin; int to; while(!q.empty()){ u=q.top(); q.pop(); now=u.first; if(dis[now]<u.second) continue; for(int i=0;i<path[now].size();i++){ to=path[now][i]; tmin=data[to][2]; if(u.second<=data[to][4]){ tmin+=data[to][4]+data[to][3]/(data[to][4]+1); } else { tmin+=u.second+data[to][3]/(u.second+1); } if(data[path[now][i]][0]==now) next = data[path[now][i]][1]; else next = data[path[now][i]][0]; if(tmin<dis[next]){ dis[next]=tmin; q.push({next,tmin}); } } } cout << dis[n-1] << endl; }
//https://atcoder.jp/contests/abc188/tasks/abc188_d #include <bits/stdc++.h> using namespace std; #define F first #define S second typedef long long ll; typedef long double ld; const ll mod = 1e9 + 7; int main () { ios_base::sync_with_stdio (0); cin.tie (0); cout.tie (0); ll n, c, ans = 0; cin >> n >> c; vector<pair<ll, ll>> v; for (ll i = 0; i < n; ++i) { ll a, b, c; cin >> a >> b >> c; v.push_back ({a - 1, c}); v.push_back ({b, -c}); } sort (v.begin (), v.end ()); ll t = 0, p = 0; for (auto& x : v) { if (x.F != t) { ans += min (c, p) * (x.F - t); t = x.F; } p += x.S; } cout << ans; }
#include<bits/stdc++.h> #include<iostream> #include<string> #include<cmath> #include<cstdio> #include<cctype> #include<cstring> #include<iomanip> #include<cstdlib> #include<ctime> #include<set> #include<map> #include<utility> #include<queue> #include<vector> #include<stack> #include<sstream> #include<algorithm> #define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++) #define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--) #define rep(i,n)for(int i=0,_n=n;i<n;i++) #define ll long long #define pii pair<int,int> #define vi vector<int> #define vpii vector<pii> #define m_p make_pair #define re return #define pb push_back #define si set<int> #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 #define x1 XZVJDFADSPFOE #define y1 GASDIJSLDAEJF #define x2 DFDAJKVOHKWIW #define y2 PSFSAODSXVNMQ #define LLINF 0x3f3f3f3f3f3f3f3fLL #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++) using namespace std; inline void read(int &x) { short negative=1; x=0; char c=getchar(); while(c<'0' || c>'9') { if(c=='-') negative=-1; c=getchar(); } while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar(); x*=negative; } ll quickpower(ll n,ll k){ ll ans=1; while(k){ if(k%2){ ans*=n; ans%=mod; } n*=n; n%=mod; k/=2; } return ans; } string int_to_string(int n) { string s=""; while(n) { int now=n%10; s+=now+'0'; n/=10; } reverse(s.begin(),s.end()); return s; } int string_to_int(string s) { int n=0; rep(i,s.size()) { n*=10; n+=s[i]-'0'; } return n; } int n,m; int a[111][111]; int main() { ios::sync_with_stdio(0); cin>>n>>m; rep(i,n)rep(j,m)cin>>a[i][j]; int mn=1e9; rep(i,n)rep(j,m)mn=min(mn,a[i][j]); ll ans=0; rep(i,n)rep(j,m)ans+=a[i][j]-mn; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> const int N = 200000; int v[2*N + 5], n; int main() { scanf("%d", &n); for(int i=1;i<=(n<<1);i++) scanf("%d", &v[i]); long long sum = 0; std::priority_queue<int>que; for(int i=n;i>=1;i--) { que.push(v[n - i + 1]), que.push(v[n + i]); sum += que.top(), que.pop(); } printf("%lld\n", sum); }
///[email protected] @Ankit Verma #include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define MP make_pair #define int long long #define ll long long #define inf 0x3f3f3f3f #define MOD 1000000007 #define ld long double #define eb emplace_back #define pii pair<int,int> #define ull unsigned long long #define all(v) v.begin(), v.end() #define f(i,x,n) for(int i=x;i<n;i++) #define fr(i,x,n) for(int i=x;i>=n;i--) #define fastio ios::sync_with_stdio(false) #define n_l '\n' template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } #define debug(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl void IO() { #ifndef ONLINE_JUDGE freopen("/home/ankit/Desktop/io/input.txt", "r", stdin); freopen("/home/ankit/Desktop/io/output.txt", "w", stdout); #endif } int32_t main() { //IO(); fastio; cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector <int> ar(n + 1); for (int i = 1; i <= n; i++) { cin >> ar[i]; } int res = 1; sort(all(ar)); for (int i = 1; i <= n; i++) { res *= (ar[i] - ar[i - 1] + 1 + MOD); res %= MOD; } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; static const ll MOD = 1e9 + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); string s; int k; cin >> s >> k; vector<vector<ll>> dp(s.size(), vector<ll>(17)); set<char> st; for (auto nc : {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}) { if (nc == s[0]) break; dp[0][1]++; } st.insert(s[0]); for (int i = 0; i < s.size() - 1; i++) { for (int j = 1; j <= 16; j++) { dp[i + 1][j] += dp[i][j] * (ll) j; dp[i + 1][j] %= MOD; if (j < 16) { dp[i + 1][j + 1] += dp[i][j] * (ll) (16 - j); dp[i + 1][j + 1] %= MOD; } } dp[i + 1][1] += (ll) 15; dp[i + 1][1] %= MOD; // int next; // if ('0' <= s[i] && s[i + 1] <= '9') next = (int) (s[i] - '0'); // else next = 10 + (int) (s[i] - 'A'); for (auto nc : {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}) { if (nc == s[i + 1]) break; if (st.find(nc) == st.end()) { dp[i + 1][st.size() + 1]++; dp[i + 1][st.size() + 1] %= MOD; } else { dp[i + 1][st.size()]++; dp[i + 1][st.size()] %= MOD; } } st.insert(s[i + 1]); } ll ans = dp[s.size() - 1][k]; // Is N composed of K characters? if (st.size() == k) ans++; if (ans >= MOD) ans -= MOD; cout << ans << '\n'; }
#include<bits/stdc++.h> using namespace std; #define ll long long void find_sum(vector<ll>& A, ll n, vector<ll>& v) { ll m=A.size(); for(ll i=0;i<n;i++) { ll sum=0,x=i,j=m-1; while(x) { if(x&1) sum+=A[j]; j--; x/=2; } v.push_back(sum); } } int main() { ll N,w; cin>>N>>w; ll n=N/2,m=N-n; vector<ll> u(n),v(m); for(ll i=0;i<n;i++) cin>>u[i]; for(ll i=0;i<m;i++) cin>>v[i]; vector<ll> x,y; ll temp=pow(2,n); find_sum(u,temp,x); temp=pow(2,m); find_sum(v,temp,y); sort(y.begin(),y.end()); ll ans=0; for(ll i:x) { if(w<i) continue; ll index=upper_bound(y.begin(),y.end(),w-i)-y.begin(); index--; if(index!=-1 && i+y[index]>ans) { ans=i+y[index]; } } cout<<ans; }
#include <bits/stdc++.h> #include <map> #include <iostream> using namespace std; typedef long long ll; typedef long double ld; #define BIG 2005 set<ll> solve(vector<ll> B, ll T){ set<ll> ans; for(int i=0;i<B.size();i++){ if(B[i] > T) continue; set<ll> add; add.insert(B[i]); for(auto tm: ans){ if(tm+B[i]<=T){ add.insert(tm+B[i]); } } for(auto tm:add){ ans.insert(tm); } } return ans; } int main() { int N; ll T; cin >> N >> T; vector<ll> B; vector<ll> C; for(int i=0;i<N;i++){ ll a; cin >> a; if(i<N/2){ B.push_back(a); }else{ C.push_back(a); } } // B set<ll> ansb = solve(B, T); vector<ll> bans; for(auto an:ansb){ bans.push_back(an); //昇順 } // C set<ll> ansc = solve(C, T); // CとBを組み合わせる ll answer = 0; for(auto an:ansb){ answer = max(answer, an); } for(auto an:ansc){ if(bans.empty() || bans.size()==0){ answer = max(answer, an); continue; } int l = 0; int r = bans.size()-1; if(bans[l]+an>T){ answer = max(answer, an); continue; } if(bans[r]+an<=T){ answer = max(answer, an+bans[r]); continue; } while(l+1!=r){ int mid = (l+r)/2; if(an+bans[mid]<=T){ l = mid; }else{ r = mid; } } answer = max(answer, an+bans[l]); } printf("%lld\n", answer); return 0; }
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; #define rep(i,a,b) for(i=(a);i<=(b);++i) #define per(i,a,b) for(i=(a);i>=(b);--i) #define REP(i,a,b) for(i=(a);i< (b);++i) #define PER(i,a,b) for(i=(a);i> (b);--i) #define ERR(...) fprintf(stderr,__VA_ARGS__) template<class T>inline void cmn(T &x,const T &a){if(a<x)x=a;} template<class T>inline void cmx(T &x,const T &a){if(x<a)x=a;} template<class T>inline bool bmn(T &x,const T &a){if(a<x){x=a;return true;}else return false;} template<class T>inline bool bmx(T &x,const T &a){if(x<a){x=a;return true;}else return false;} const int MAXN=100005; char S[MAXN]; signed main(){ #ifdef ONLINE_JUDGE //freopen("problemname.in","r",stdin); //freopen("problemname.out","w",stdout); #endif int N; scanf("%d%s",&N,S); if(S[0]!=S[N-1])puts("1"); else{ int i; REP(i,1,N-2)if(S[i]!=S[0]&&S[0]!=S[i+1]){ puts("2"); return 0; } puts("-1"); } return 0; }
#include<bits/stdc++.h> using namespace std; const int N=1e6+10; int cnt[N]; int main() { string s; cin>>s; int f=0; int g=0; int x=0; if(s.size()==1) { if(s[0]=='8') { cout<<"Yes"<<endl; return 0; } else { cout<<"No"<<endl; return 0; } } else if(s.size()==2) { int cur = s[0]-'0'; int cur1= s[1]-'0'; if((cur*10+cur1)%8==0 || (cur1*10+cur)%8==0) { cout<<"Yes"<<endl; return 0; } else { cout<<"No"<<endl; return 0; } } string op = s; for(int i = 0; i < op.size(); i++)//判断字符串有多少数字 { char tmp = op[i]; cnt[op[i] - '0']++; } int time = 100 / 8 + 1; int flg = 1; for(int i = time; 8 * i < 1000; i++)//枚举100-1000里8的倍数 { flg = 1; int tmp = 8 * i; int tmpp[10] = {0};//8的倍数的数字组成 while(tmp) { if(tmp%10 == 0) { flg = 0; break; } tmpp[tmp%10]++; tmp /= 10; } if(flg == 0) continue; for(int i = 1; i < 10; i++) { if(cnt[i] < tmpp[i]) { flg = 0; break; } } if(flg) { break; } } if(flg) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
//#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using db = double; using ld = long double; template<typename T> using V = vector<T>; template<typename T> using VV = vector<vector<T>>; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(),(v).end() #define siz(v) (ll)(v).size() #define rep(i,a,n) for(ll i=a;i<(ll)(n);++i) #define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i) #define ENDL '\n' typedef pair<int,int> Pi; typedef pair<ll,ll> PL; constexpr ll mod = 1000000007; // 998244353; constexpr ll INF = 1000000099; constexpr ll LINF = (ll)(1e18 +99); const ld PI = acos((ld)-1); const vector<ll> dx={-1,1,0,0},dy={0,0,-1,1}; template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;} template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;} template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;} template<typename T,typename Y> inline T mpow(T a, Y n) { T res = 1; for(;n;n>>=1) { if (n & 1) res = res * a; a = a * a; } return res; } template <typename T> V<T> prefix_sum(const V<T>& v) { int n = v.size(); V<T> ret(n + 1); rep(i, 0, n) ret[i + 1] = ret[i] + v[i]; return ret; } template<typename T> istream& operator >> (istream& is, vector<T>& vec){ for(auto&& x: vec) is >> x; return is; } template<typename T,typename Y> ostream& operator<<(ostream& os,const pair<T,Y>& p){ return os<<"{"<<p.fs<<","<<p.sc<<"}"; } template<typename T> ostream& operator<<(ostream& os,const V<T>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<typename ...Args> void debug(Args&... args){ for(auto const& x:{args...}){ cerr<<x<<' '; } cerr<<ENDL; } template <long long MOD = 1000000007> // MODを適宜設定すること struct Mint { using M = Mint; ll a; Mint(ll x = 0) { a = x % MOD; if(a < 0) a += MOD; } // Mintで M pow(ll n) { M b = *this, r = 1 % MOD; while(n) { if(n & 1) r *= b; b *= b; n >>= 1; } return r; } M inv() { return pow(MOD - 2); } M& operator+=(M r) { a += r.a; if(a >= MOD) a -= MOD; return *this; } M& operator-=(M r) { a += MOD - r.a; if(a >= MOD) a -= MOD; return *this; } M& operator*=(M r) { a = 1LL * a * r.a % MOD; return *this; } M& operator/=(M r) { return (*this) *= r.inv(); } M operator+(M r) const { return M(a) += r; }; M operator-(M r) const { return M(a) -= r; }; M operator*(M r) const { return M(a) *= r; }; M operator/(M r) const { return M(a) /= r; }; M operator-() const { return M() - *this; } friend ostream& operator<<(ostream& os, const M& r) { return os << r.a; } }; using M = Mint<mod>; M sum(ll x){ ll res=x; res*=x+1; res/=2; return res%mod; } M sum2(ll x,ll lim){ if(x<=lim)return sum(x); M res=sum(lim); res+=lim*(x-lim); return res; } signed main(){ cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false); cout<<fixed<<setprecision(20); int qq;cin>>qq; while(qq--){ ll n,a,b;cin>>n>>a>>b; if(a+b>n){ cout<<0<<ENDL; continue; } if(a<b)swap(a,b); M ans=0; ans+=sum2((n*2-a-b+2)/2,n-a+1)*sum(n-a-b+1); ans+=(sum2((n*2-a-b+1)/2,n-a+1)-sum(n-a-b+1))*sum(n-a-b+1); ans*=4; cout<<ans<<ENDL; } } //! ( . _ . ) ! //CHECK overflow,vector_size,what to output?
#include<bits/stdc++.h> #define ll long long using namespace std; const ll mod=1e9+7; int main() { int t; scanf("%lld",&t); while(t--) { ll a,b,c,ans=0; scanf("%lld%lld%lld",&a,&b,&c); if(a>=b+c){ ll res=(a-b-c+1)*(a-b-c+2)%mod; ll num=2*(a-b+1)*(a-c+1)%mod-res; if(num<0) num=(mod-(-num)%mod)%mod; ans=res*num%mod; } cout<<ans<<endl; } }
/** * author: shu8Cream * created: 26.04.2021 09:06:13 **/ #include <bits/stdc++.h> #include <chrono> using namespace std; #define rep(i,n) for (int i=0; i<(n); i++) #define all(x) (x).begin(), (x).end() using ll = long long; using P = pair<int,int>; using vi = vector<int>; using vvi = vector<vi>; using namespace std::chrono; inline double get_time_sec(void){ return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count())/1000000000; } int si,sj; vvi t(50,vi(50)); vvi p(50,vi(50)); string finans = ""; int finscore = 0; const vi di = {1,0,-1,0}; const vi dj = {0,1,0,-1}; void input(){ cin >> si >> sj; rep(i,50)rep(j,50) cin >> t[i][j]; rep(i,50)rep(j,50) cin >> p[i][j]; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); input(); set<int> allrem; int loop = 20; while(loop--){ double start = get_time_sec(); int score = 0; string ans = ""; vector<P> remcell; while(get_time_sec() - start<0.098){ int tscore = 0; string tans = ""; set<int> rem; queue<P> pq; pq.push({si,sj}); int i=0,j=0; while(!pq.empty()){ int pi=i,pj=j; tie(i,j) = pq.front(); pq.pop(); remcell.push_back({i,j}); rem.insert(t[i][j]); tscore += p[i][j]; queue<P> emp; swap(pq,emp); if(!(i == si && j == sj)){ if(i-pi==1) tans+="D"; else if(i-pi==-1) tans+="U"; else if(j-pj==1) tans+="R"; else if(j-pj==-1) tans+="L"; } // cout << ans << endl; vi rand = {0,1,2,3}; std::random_device seed_gen; std::mt19937 engine(seed_gen()); shuffle(all(rand),engine); for(auto k : rand){ int ni = i + di[k]; int nj = j + dj[k]; if(ni<0 || ni>=50 || nj<0 || nj>=50) continue; if(t[i][j]==t[ni][nj]) continue; if(rem.count(t[ni][nj])) continue; if(allrem.count(t[ni][nj])) continue; int ngcnt = 0; rep(kk,4){ int nni = ni + di[kk]; int nnj = nj + dj[kk]; if(nni<0 || nni>=50 || nnj<0 || nnj>=50) {ngcnt++; continue;} if(t[nni][nnj]==t[ni][nj]) {ngcnt++; continue;} if(rem.count(t[nni][nnj])) {ngcnt++; continue;} if(allrem.count(t[nni][nnj])) {ngcnt++; continue;} } if(ngcnt==4) continue; pq.push({ni,nj}); } } if(tscore>score){ score = tscore; ans = tans; } } int anslen = (ans.size()+1)/2; ans = ans.substr(0,anslen); finans += ans; allrem.insert(t[si][sj]); rep(k,anslen){ if(ans[k]=='U') si--; if(ans[k]=='D') si++; if(ans[k]=='L') sj--; if(ans[k]=='R') sj++; allrem.insert(t[si][sj]); } } cout << finans << endl; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef long long LL; typedef vector<LL> VL; typedef vector<VL> VVL; typedef pair<LL, LL> PLL; typedef vector<PLL> VPLL; typedef vector<bool> VB; typedef priority_queue<LL> PQ_DESC; typedef priority_queue<LL, VL, greater<LL>> PQ_ASC; typedef priority_queue<PII> PQ_DESC_PII; typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII; typedef priority_queue<VL> PQ_DESC_VL; typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL; typedef priority_queue<PLL> PQ_DESC_PLL; typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL; #define ALL(c) (c).begin(),(c).end() #define PB push_back #define MP make_pair #define SORT_ASC(c) sort(ALL(c)) //#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>()) #define SORT_DESC(c) sort((c).rbegin(),(c).rend()) #define REV(c) reverse((c).begin(), (c).end()) #define SIZE(a) LL((a).size()) #define FOR(i,a,b) for(LL i=(a);i<(b);++i) #define ROF(i,a,b) for(LL i=(b-1);i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define PER(i,n) ROF(i,0,n) const double EPS = 1e-10; const double PI = acos(-1.0); const LL LARGE_INT = 1e9+100; const LL INF = 2e9+100; const LL INF_LL = (LL)INF*(LL)INF; const LL MOD = 1e9+7; //debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; LL TIME_LIMIT = 1990; LL start_time; string result_string; LL result_point=0; LL h=50; LL w=50; VVL t; VVL p; void check(string result, LL point){ if(point > result_point){ result_point = point; result_string = result; } } void dfs(LL x, LL y, VB v, string result, LL point){ LL time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count(); if(time-start_time > TIME_LIMIT){ check(result, point); return; } v[t[x][y]]=true; VL dx={1,-1,0,0}; VL dy={0,0,1,-1}; VVL tp; REP(i,4){ LL nx = x+dx[i]; LL ny = y+dy[i]; if(nx >= h || nx < 0 || ny >= w || ny < 0){ continue; } if(v[t[nx][ny]]){ continue; } tp.PB({p[nx][ny],i,nx,ny}); } if(tp.empty()){ check(result, point); return; } SORT_DESC(tp); for(auto w : tp){ LL np = w[0]; LL i = w[1]; LL nx = w[2]; LL ny = w[3]; //cerr<<np<<" "<<i<<" "<<nx<<" "<<ny<<endl; string d; if(i==0){ d = "D"; } if(i==1){ d = "U"; } if(i==2){ d = "R"; } if(i==3){ d = "L"; } dfs(nx, ny, v, result+d, point+np); } } void Main() { start_time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count(); //dump(start_time) LL si,sj;cin>>si>>sj; t.resize(h); REP(i,h){ t[i].resize(w); REP(j,w){ cin>>t[i][j]; } } p.resize(h); REP(i,h){ p[i].resize(w); REP(j,w){ cin>>p[i][j]; } } VB v(h*w); string result = ""; LL point = 0; dfs(si, sj, v, result, point); cout<<result_string<<endl; return; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; template<class T> auto add = [](T a, T b) -> T { return a + b; }; template<class T> auto f_max = [](T a, T b) -> T { return max(a, b); }; template<class T> auto f_min = [](T a, T b) -> T { return min(a, b); }; template<class T> using V = vector<T>; using Vl = V<lint>; using VVl = V<Vl>; template< typename T > ostream& operator<<(ostream& os, const vector< T >& v) { for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; } template< typename T >istream& operator>>(istream& is, vector< T >& v) { for (T& in : v) is >> in; return is; } template<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; } lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); } lint ceil(lint a, lint b) { return (a + b - 1) / b; } lint digit(lint a) { return (lint)log10(a); } lint dist(plint a, plint b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); } lint m_dist(plint a, plint b) { return abs(a.first - b.first) + abs(a.second - b.second); } void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); } const lint MOD = 1e9 + 7, INF = 5e18; lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 }; bool YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; return flag; } struct Edge { lint from, to; lint cost; Edge(lint u, lint v, lint c) { cost = c; from = u; to = v; } bool operator<(const Edge& e) const { return cost < e.cost; } }; struct WeightedEdge { lint to; lint cost; WeightedEdge(lint v, lint c = 1) { to = v; cost = c; } bool operator<(const WeightedEdge& e) const { return cost < e.cost; } }; using WeightedGraph = V<V<WeightedEdge>>; typedef pair<plint, lint> tlint; typedef pair<plint, plint> qlint; typedef pair<string, lint> valstring; lint N, M, u, v; int main() { cin >> N >> M; VVl mat(N, Vl(N, 0)); REP(i, M) { cin >> u >> v; u--; v--; mat[u][v] = 1; mat[v][u] = 1; } Vl dp(1 << N, INF); V<plint> vec; FOR(mask, 1, 1 << N) { bool flag = true; REP(i, N) { FOR(j, i + 1, N) { if (((mask >> i) % 2) && (mask >> j) % 2) { if (mat[i][j] == 0) flag = false; } } } lint cnt = 0; REP(i, N) { if ((mask >> i) % 2 == 1) cnt++; } if (flag) { dp[mask] = 1; } vec.push_back({ cnt, mask }); } sort(ALL(vec)); for(plint v : vec) { lint mask = v.second; for (lint bit = mask; ; bit = (bit - 1) & mask) { if (!(dp[bit] == INF || dp[mask ^ bit] == INF)) { chmin(dp[mask], dp[bit] + dp[mask ^ bit]); } if (bit == 0) break; } } cout << dp[(1ll << N) - 1] << endk; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int sz = 19, msz = 1 << sz; int n, m, g[sz][sz], dp[msz]; bitset <msz> can; int main() { cin >> n >> m; while(m--) { int u, v; scanf("%d %d", &u, &v); u--, v--; g[u][v] = g[v][u] = 1; } int lim = 1 << n; can.set(); for(int i=1; i<lim; i++) { vector <int> nodes; for(int j=0; j<n; j++) if(i & (1 << j)) nodes.push_back(j); for(int &x : nodes) { for(int &y : nodes) { if(x ^ y and !g[x][y]) can[i] = 0; } } } for(int i=1; i<lim; i++) { dp[i] = n; for(int j=i; j>0; j=(j-1)&i) if(can[j]) dp[i] = min(dp[i], 1 + dp[i ^ j]); } cout << dp[lim-1]; }
#include<bits/stdc++.h> using namespace std; #define int long long vector<int> a; int n; long double f(long double x){ long double sum = 0; for(int i = 0; i < n; i++){ sum += (x + a[i] - min(2 * x, (long double)a[i])); // if(x == 1.5) // cout << x << " " << a[i] << " " << min(2 * x, (long double)a[i]) << endl; } // if(x == 1.5) // cout << x << " " << sum << endl; return sum / n; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; a.resize(n); for(int i = 0; i < n; i++) cin >> a[i]; long double eps = 1e-6; long double l = 0, r = 2000000000; while (r - l > eps) { double m1 = l + (r - l) / 3; double m2 = r - (r - l) / 3; long double f1 = f(m1); long double f2 = f(m2); if (f1 > f2) l = m1; else r = m2; } printf("%0.7Lf\n", f(l)); // cout << std::setprecision(8) << f(l) << endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define P pair<int,int> #define fi first #define se second #define rep(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define pb push_back template<class T,class U> inline bool chmax(T &a,U b){ if(a<b){ a=b; return true; } return false; } template<class T,class U> inline bool chmin(T &a,U b){ if(a>b){ a=b; return true; } return false; } constexpr int INF=1000000000; constexpr ll llINF=1000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-8; const double pi=acos(-1); int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int Random(int mi,int ma){ random_device rnd; mt19937 mt(rnd());//32bit //[mi,ma] uniform_int_distribution<int>engine(mi,ma); return engine(mt); } /* vector<vector<ll>>C,sC; void init_comb(int n,int m){ C.resize(n+1,vector<ll>(m+1,0)); sC.resize(n+1,vector<ll>(m+1,0)); C[0][0]=1; for(int i=1;i<=n;i++){ C[i][0]=1; for(int j=1;j<=m;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } rep(i,n+1){ rep(j,m){ sC[i][j+1]=(sC[i][j]+C[i][j])%mod; } } }*/ ll gcd(ll a,ll b){ while(a%b){ a%=b; swap(a,b); } return b; } ll lcm(ll a,ll b){ return a/gcd(a,b)*b; } bool isprime(int a){ if(a==1)return false; for(int i=2;i*i<=a;i++){ if(a%i==0)return false; } return true; } set<int>primes; void init_prime(int n){ primes.insert(2); for(int i=3;i<=n;i+=2){ bool f=true; for(int j:primes){ if(j*j>i)break; if(i%j==0){ f=false; break; } } if(f)primes.insert(i); } } ll modpow(ll a,ll b){ ll res=1; while(b){ if(b&1){ res*=a; res%=mod; } a*=a; a%=mod; b>>=1; } return res; } vector<ll>inv,fact,factinv; void init_fact(int n){ inv.resize(n+1); fact.resize(n+1); factinv.resize(n+1); inv[0]=0; inv[1]=1; fact[0]=1; factinv[0]=1; for(ll i=1;i<=n;i++){ if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod); fact[i]=(fact[i-1]*i)%mod; factinv[i]=factinv[i-1]*inv[i]%mod; } } ll _inv(ll a,ll m=mod){ //gcd(a,m) must be 1 ll b=m,u=1,v=0; while(b){ ll t=a/b; a-=t*b;swap(a,b); u-=t*v;swap(u,v); } u%=m; if(u<0)u+=m; return u; } ll comb(int a,int b){ if(a<b)return 0; if(a<0)return 0; return fact[a]*factinv[a-b]%mod*factinv[b]%mod; } ll multicomb(int a,int b){ return comb(a+b-1,b); } struct BIT{ int n,beki=1; vector<int>bit; BIT(int x){ bit.resize(x+1,0); n=x; while(beki*2<=n)beki*=2; } int sum(int i){ int res=0; while(i>0){ res+=bit[i]; i-=i&-i; } return res; } int sum(int l,int r){ //[l,r] return sum(r)-(l==0?0:sum(l-1)); } int get(int l){ return sum(l,l); } void set(int i,int x){ add(i,x-get(i)); } void add(int i,int x){ while(i<=n){ bit[i]+=x; i+=i&-i; } } void add(int l,int r,int x){ //[l,r]+=x add(l,x); if(r<n)add(r+1,-x); } int lowerbound(int w){ if(w<=0)return 0; int x=0; for(int k=beki;k>0;k>>=1){ if(x+k<=n&&bit[x+k]<w){ w-=bit[x+k]; x+=k; } } return x+1; } }; int main(){ cin.tie(0);ios::sync_with_stdio(false); int n,a[300010]; cin>>n; rep(i,n){ cin>>a[i]; a[i]++; } BIT bit(n); ll ans=0; rep(i,n){ ans+=bit.sum(a[i]+1,n); bit.add(a[i],1); } cout<<ans<<endl; rep(i,n-1){ ans+=(n-a[i]-a[i]+1); cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const int MAXN = 1e5, INF = 1e9; int n, m, vis[MAXN], dis[17][17], gems[17]; vector<int> adj[MAXN]; int bfs(int start, int find) { int dis = INF; queue<pair<int, int>> q; q.push({start, 0}); vis[start] = 1; while(!q.empty()) { auto curr = q.front(); if(curr.first == find) { dis = curr.second; break; } q.pop(); for(int next: adj[curr.first]) { if(!vis[next]) { q.push({next, curr.second+1}); vis[next] = 1; } } } return dis; } int main() { cin >> n >> m; for(int i=0; i<m; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } int k; cin >> k; for(int i=0; i<k; i++) { cin >> gems[i]; gems[i]--; } for(int i=0; i<k; i++) { for(int j=i+1; j<k; j++) { memset(vis, 0, sizeof(vis)); int res = bfs(gems[i], gems[j]); dis[i][j] = res, dis[j][i] = res; if(dis[i][j] == INF) { cout << -1 << endl; return 0; } } } int dp[(1<<k)][k]; for(int i=0; i<(1<<k); i++) { for(int j=0; j<k; j++) { dp[i][j] = INF; } } for(int i=0; i<k; i++) { dp[(1<<i)][i] = 1; } for(int i=0; i<(1<<k); i++) { for(int j=0; j<k; j++) { if(!(i>>j&1)) continue; for(int prev=0; prev<k; prev++) { if(!(i>>prev&1) || prev == j) continue; // cout << i << ' ' << j << ' ' << prev << endl; dp[i][j] = min(dp[i][j], dp[i^(1<<j)][prev]+dis[prev][j]); } } } int ans = INF; for(int i=0; i<k; i++) { ans = min(ans, dp[(1<<k)-1][i]); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).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 (b<a) { a=b; return 1; } return 0; } const ll INF=1LL<<60; const int inf=(1<<30)-1; const int mod=1e9+7; int dx[8]={1,0,-1,0,-1,-1,1,1}; int dy[8]={0,1,0,-1,-1,1,-1,1}; const int nmax=100005; vector<vector<pair<ll,ll>>> e(nmax); ll d[nmax]; void dijkstra(ll s){ priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq; fill(d,d+nmax,INF); d[s]=0; pq.push(make_pair(0,s)); while(!pq.empty()){ pair<ll,ll> p=pq.top(); pq.pop(); ll v=p.second; if(d[v]<p.first){ continue; } for(auto x:e[v]){ if(d[x.first]>d[v]+x.second){ d[x.first]=d[v]+x.second; pq.push(make_pair(d[x.first],x.first)); } } } } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,m;cin >> n >> m; vector<int> a(m),b(m); REP(i,m){ cin >> a[i] >> b[i]; a[i]--,b[i]--; e[a[i]].push_back({b[i],1}); e[b[i]].push_back({a[i],1}); } int k;cin >> k; vector<int> c(k); REP(i,k){ cin >> c[i]; c[i]--; } vector<vector<ll>> dis(k,vector<ll>(k)); REP(i,k){ dijkstra(c[i]); REP(j,k) dis[i][j]=d[c[j]]; } vector<vector<ll>> dp(1<<k,vector<ll>(k,INF)); REP(i,k) dp[1<<i][i]=1; REP(t,1<<k){ REP(i,k){ if(dp[t][i]==INF) continue; if(!((t>>i)&1)) continue; REP(j,k){ if((t>>j)&1) continue; chmin(dp[t|(1<<j)][j],dp[t][i]+dis[i][j]); } } } ll ans=INF; REP(i,k) chmin(ans,dp[(1<<k)-1][i]); cout << (ans==INF?-1:ans) << endl; }
#include <bits/stdc++.h> #define przxct "" #define fto(i,j,h) for (int i=j; i<=h; ++i) #define maxn #define ll long long #define pi 3.141592653589 using namespace std; string s; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> s; int n = s.length(); s = " " + s; for (int i = 1; i <= n; i += 2) { if (s[i] < 'a' || s[i] > 'z') { cout << "No"; return 0; } } for (int i = 2; i <= n; i += 2) { if (s[i] < 'A' || s[i] > 'Z') { cout << "No"; return 0; } } cout << "Yes"; return 0; }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T> ostream &operator<<(ostream &os, const vector<T> &x){ os << "{"; for(size_t i = 0; i < x.size(); i++){ if(i < x.size()-1) os << x[i] << ", "; else os << x[i]; } os << "}"; return os; } int main(void){ int n; cin >> n; n-=1; n/=100; cout << n+1 << endl; return 0; }
#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define MP std::make_pair #define PR std::pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int n; std::vector<PII> A; std::vector<PII> B; int check(int i){ if(A[i].X == -1 || A[i].Y == -1) return -1; return A[i].Y-A[i].X; } bool can(int a, int b){ int len = (b-a+1); if(len%2 != 0) return false; int dl = len/2; FOR(i, dl){ if(B[a+i].X == -1 && B[a+i+dl].X == -1) continue; if(B[a+i].Y == 0) return false; if(B[a+i+dl].Y == 1) return false; if(B[a+i].X != -1 && B[a+i+dl].X != -1 && B[a+i].X != B[a+i+dl].X) return false; } return true; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; A.resize(n); B.resize(2*n+1, MP(-1, -1)); std::set<int> set; int cnt = 0; FOR(i, n){ std::cin >> A[i].X >> A[i].Y; if(A[i].X != -1 && A[i].Y != -1 && A[i].X >= A[i].Y){ std::cout << "No\n"; return 0; } if(A[i].X != -1) B[A[i].X] = MP(i, 1), set.insert(A[i].X), cnt++; if(A[i].Y != -1) B[A[i].Y] = MP(i, 0), set.insert(A[i].Y), cnt++; } if(SZ(set) != cnt){ std::cout << "No\n"; return 0; } std::vector<bool> dp(2*n+1); dp[0] = true; REP(i, 1, 2*n+1){ FOR(j, i){ dp[i] = (dp[i] || (dp[j] && can(j+1, i))); } } std::cout << (dp[2*n] ? "Yes\n" : "No\n"); return 0; }
//LIBRARIES #include <iostream> #include <iomanip> #include <cstdio> #include <fstream> #include <cmath> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <stack> #include <queue> #include <deque> #include <bitset> #include <chrono> //MACROS #define _crt_secure_no_warnings #define FREEELO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define mp make_pair #define INF 0x3F3F3F3F #define MAX 100001 #define MAXLL 1000000000000000000LL #define MOD 1000000007 #define LOGMAX 20 #define ALPHABET 128 #define loop(i, n) for (int i = 1; i <= (n); i++) #define loop0(i, n) for (int i = 0; i < (n); i++) #define sz(x) (int)x.size() #define all(v) v.begin(), v.end() #define all1(v) v.begin() + 1, v.end() #define F first #define S second #define EPS 1e-6 using namespace std; //TYPE DEFINITIONS typedef long long ll; typedef unsigned long long ull; typedef long double lld; typedef pair<ll, ll> llll; typedef pair<int, int> ii; typedef unordered_map<int, int> umii; typedef unordered_map<ll, ll> umll; typedef unordered_set<int> usi; typedef unordered_multiset<int> usmi; //main exec int main(){ FREEELO string s; cin >> s; int n = sz(s); s = " " + s; vector<int> dp(n + 1); for (int i = 1; i <= n; i++){ dp[i] = dp[i - 1] + (s[i] - '0'); } if (dp[n] % 3 == 0) { cout << 0 << endl; return 0; } for (int k = 1; k <= n - 1; k++){ for (int i = 1; i + k - 1 <= n; i++){ int cur_sum = dp[i - 1] + dp[n] - dp[i + k - 1]; if (cur_sum % 3 == 0) { cout << k << endl; return 0; } } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string a; getline(cin, a ,'.'); cout << a; return 0; }
#include <bits/stdc++.h> using namespace std ; int main(){ int n ; cin >> n ; if(n%100 == 0){ cout << 100 << endl ; }else{ cout << 100 - (n%100) << endl ; } return 0 ; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) // For debug // Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac #define dump(var) do{std::cerr << #var << " : ";view(var);}while(0) template<typename T> void view(T e){std::cerr << e << "\n";} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";} template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } } template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); } void solve() { ll n, m; cin >> n >> m; vector<vector<bool>> G(n, vector<bool>(n, false)); rep(i, 0, m) { ll a, b; cin >> a >> b; a--; b--; G[a][b] = G[b][a] = true; } vector<bool> ok(1<<n, true); rep(bit, 0, 1<<n) { vector<ll> vs; rep(i, 0, n) if(bit & (1<<i)) vs.push_back(i); rep(i, 0, vs.size()) { if(!ok[bit]) break; rep(j, i+1, vs.size()) { if(!ok[bit]) break; if(!G[vs[i]][vs[j]]) ok[bit] = false; } } } const ll inf = 1LL<<60; vector<ll> dp(1<<n, inf); dp[0] = 0; rep(bit, 0, 1<<n) { if(dp[bit] >= inf) continue; ll cbit = (1<<n) - 1 - bit; for(ll add = cbit; ; add = (add-1) & cbit) { if(!add) break; if(ok[add]) dp[bit|add] = min(dp[bit|add], dp[bit]+1); } } cout << dp[(1<<n)-1] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <string.h> #include <vector> #include <queue> #include <cmath> #include <complex> #include <functional> #include <numeric> #include <iomanip> #include <cassert> #include <random> #include <chrono> /* #include <atcoder/all> */ /* using namespace atcoder; */ using namespace std; void debug_out(){ cout << "\n"; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) 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 int ll using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1000000007; /* const ll MOD = 998244353; */ const ll INF = 1ll<<60; const double PI = acos(-1); struct INIT { INIT(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }}INIT; vector<int> compress(vector<int> &v){ int N = v.size(); vector<int> ret = v; sort(ALL(ret)); ret.erase(unique(ALL(ret)), ret.end()); REP(i, N){ v[i] = lower_bound(ALL(ret), v[i])-ret.begin(); } return ret; } // a -> b -> cの最短距離 int f(int a, int b, int c){ return abs(b-a) + abs(c-b); } signed main() { int N; cin >> N; vector<int> X(N), C(N); REP(i, N) cin >> X[i] >> C[i]; vector<int> c = compress(C); REP(i, C.size()){ C[i]++; } int MAX = 0; REP(i, N) chmax(MAX, C[i]); MAX++; vector<int> r(MAX, -INF), l(MAX, INF); REP(i, N){ chmax(r[C[i]], X[i]); chmin(l[C[i]], X[i]); } r[0] = 0; l[0] = 0; // dp[i][j]: i番目の左右に来た時の最小値 vector<vector<int>> v(MAX, vector<int>(2, INF)); v[0][0] = 0; v[0][1] = 0; FOR(i, 1, MAX){ chmin(v[i][0], v[i-1][0]+f(r[i-1], l[i], r[i])); chmin(v[i][0], v[i-1][1]+f(l[i-1], l[i], r[i])); chmin(v[i][1], v[i-1][0]+f(r[i-1], r[i], l[i])); chmin(v[i][1], v[i-1][1]+f(l[i-1], r[i], l[i])); } int ans = min(v[MAX-1][0]+abs(r[MAX-1]), v[MAX-1][1]+abs(l[MAX-1])); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> const int N = 3e5+5; int n, q, t[N], a[N]; int ask(int x) { int y = 0; for( ; x; x -= x&-x) y ^= t[x]; return y; } void add(int x, int y) { for( ; x <= n; x += x&-x) t[x] ^= y; } int main() { scanf("%d%d", &n, &q); for(int i = 1; i <= n; ++i) scanf("%d", a+i), a[i] ^= a[i-1]; while(q--) { int op, x, y; scanf("%d%d%d", &op, &x, &y); if(op == 1) add(x, y); else printf("%d\n", a[y]^a[x-1]^ask(y)^ask(x-1)); } return 0; }
#include <bits/stdc++.h> #define ll long long int #define ld long double #define f first #define s second #define pb push_back #define eb emplace_back #define mk make_pair #define mt make_tuple #define MOD 1000000007 #define fo(i,a,b) for(i=a;i<b;i++) #define foe(i,a,b) for(i=a;i<=b;i++) #define all(x) x.begin(), x.end() #define vi vector<int> #define vl vector <long long int> #define pii pair <int,int> #define pll pair <long long int, long long int> #define vpii vector< pair<int,int> > #define vpll vector < pair <long long int,long long int> > #define boost ios::sync_with_stdio(false); cin.tie(0) using namespace std; const int inf = 1e9 + 5; const ll inf64 = 1e18 + 5; template <typename T> class segtree { private: vector<T> value; int size; public: const T INITIAL_VALUE = 0; const T NEUTRAL = 0; segtree (int n) { size = 1; while(size < n) size *= 2; value.assign(2 * size, INITIAL_VALUE); } T calc_op(T a, T b) { return (a ^ b); } void build(vector<T> &v, int x, int lx, int rx) { if(rx - lx == 1) { if(lx < (int)v.size()) value[x] = v[lx]; return; } int m = (lx + rx) / 2; build(v, 2 * x + 1, lx, m); build(v, 2 * x + 2, m, rx); value[x] = calc_op(value[2 * x + 1], value[2 * x + 2]); } void upd(int i, T v, int x, int lx, int rx) { if(rx - lx == 1) { value[x] = v; return; } int m = (lx + rx) / 2; if(i < m) upd(i, v, 2 * x + 1, lx, m); else upd(i, v, 2 * x + 2, m, rx); value[x] = calc_op(value[2 * x + 1], value[2 * x + 2]); } T qry(int l, int r, int x, int lx, int rx) { if(l >= rx || lx >= r) return NEUTRAL; if(l <= lx && rx <= r) return value[x]; int m = (lx + rx) / 2; T s1 = qry(l, r, 2 * x + 1, lx, m); T s2 = qry(l, r, 2 * x + 2, m, rx); return calc_op(s1, s2); } void build(vector<T> &v) { build(v, 0, 0, size); } void upd(int i, T v) { upd(i, v, 0, 0, size); } T qry(int l, int r) { return qry(l, r, 0, 0, size); } }; int main() { boost; int n, q; cin >> n >> q; vector <int> a(n); for(int i = 0; i < n; i++) cin >> a[i]; segtree<int> st(n); st.build(a); while(q--) { int t; cin >> t; if(t == 1) { int x, y; cin >> x >> y; --x; st.upd(x, (a[x] ^ y)); a[x] ^= y; } else { int l, r; cin >> l >> r; --l; cout << st.qry(l, r) << '\n'; } } }
// Problem: B - RGB Matching // Contest: AtCoder - NOMURA Programming Contest 2021(AtCoder Regular Contest 121) // URL: https://atcoder.jp/contests/arc121/tasks/arc121_b // Memory Limit: 1024 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int,int>; #define pb push_back #define mp make_pair const double PI = 4*atan(1); const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9+7; const int MAX_N = 2e6+10; int n; ll a[MAX_N]; char c[MAX_N]; void solve() { map<int,vector<ll>> x; for(int i=0;i<2*n;++i){ if(c[i]=='R') x[0].pb(a[i]); else if(c[i]=='B')x[1].pb(a[i]); else x[2].pb(a[i]); } vector<int> res; for(int i=0;i<3;++i){ sort(x[i].begin(),x[i].end()); if((int)x[i].size()%2) res.pb(i); } if(res.empty()){ cout<<"0\n"; return; } int u = res[0], v = res[1]; auto cal = [&] (int u, int v) { ll ans = LINF; if(x[v].empty()) return ans; for(int i=0,j=0;i<(int)x[u].size();++i){ while(j+1<(int)x[v].size() && x[v][j+1]<=x[u][i]) j++; ans = min(ans, abs(x[u][i]-x[v][j])); if(j+1<(int)x[v].size()){ ans = min(ans, abs(x[u][i]-x[v][j+1])); } } return ans; }; int w = 3-u-v; ll ans = min(cal(u,v),cal(u,w)+cal(v,w)); cout<<ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; for(int i=0;i<2*n;++i)cin>>a[i]>>c[i]; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 20; int x[N], y[N], z[N]; int g[N][N]; int f[N][1 << N], n; int get_dis(int a, int b) { return abs(x[a] - x[b]) + abs(y[a] - y[b]) + max(0ll, z[b] - z[a]); } signed main() { #ifndef ONLINE_JUDGE freopen("E:\\in.txt", "r", stdin); #endif cin >> n; int M = 1 << (n - 1); memset(f, 0x3f, sizeof f); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { g[i][j] = get_dis(i, j); } } for (int i = 0; i < n; i++) { f[i][0] = g[i][0]; } for (int j = 1; j < M; j++) { for (int i = 0; i < n; i++) { if ((j >> (i - 1)) & 1) continue; for (int k = 1; k < n; k++) { if (i == k) continue; if ((j >> (k - 1) & 1) == 0) continue; f[i][j] = min(f[i][j], g[i][k] + f[k][j ^ (1 << (k - 1))]); } } } cout << f[0][M - 1] << endl; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using Graph = vector<vector<int>>; using ll = long long; using P = pair<int, int>; vector<bool> seen; void dfs(const Graph &G, int v) { seen[v] = true; for (auto next_v : G[v]) { if (seen[next_v]) continue; dfs(G, next_v); } } int main() { int N, M; cin >> N >> M; Graph G(N); for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; G[a-1].push_back(b-1); } int ans = 0; rep(i,N){ seen.assign(N, false); dfs(G, i); rep(j,N){ if(seen.at(j)){ ans++; } } } cout << ans << endl; }
#include<bits/stdc++.h> #define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++) #define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--) #define rep(i,n)for(int i=0,_n=n;i<n;i++) #define ll long long #define pii pair<int,int> #define m_p make_pair #define pb push_back #define fi first #define se second #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++) using namespace std; const int N=2010; int n,m; vector<int>g[N]; int vis[N]; int ans=0; void dfs(int u){ vis[u]=1; ans++; rep(i,g[u].size()){ int v=g[u][i]; if(vis[v])continue; dfs(v); } } int main(){ ios::sync_with_stdio(0); cin>>n>>m; rep(i,m){ int u,v; cin>>u>>v; u--;v--; g[u].pb(v); } rep(i,n){ memset(vis,0,sizeof(vis)); dfs(i); } cout<<ans<<"\n"; return 0; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; // using ll = long long; // int : -2**31 〜 2**31-1 (2**31=2147483648=2*10**9) // long long : -2**63 〜 2**63-1 (2**63=9223372036854775808=9*10**18) using P = pair<int,int>; // #define int ll // int main() --> signed main() として使う. #define rep(i,n) for (int i=0; i<n; i++) #define rep2(i,s,e) for (int i=s; i<e; i++) template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;} // template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;} const int INF = (int)2e9; struct Edge { int cost; int to; Edge(int c, int t) : cost(c), to(t) {} }; void dijkstra_heap(int s, vector<vector<Edge>> &edges, vector<int> &dist) { dist[s] = 0; priority_queue<P,vector<P>,greater<P>> q; q.push(make_pair(dist[s],s)); while (!q.empty()) { int v = q.top().second; int d = q.top().first; q.pop(); if (d>dist[v]) continue; for (auto e: edges[v]) { if (chmin(dist[e.to],dist[v]+e.cost)) { q.push(make_pair(dist[e.to],e.to)); } } } } int no(int i, int j, int C) { return i*C + j; } int main() { cin.tie(nullptr); // 小数点以下12桁まで cout << fixed << setprecision(12); // input int R,C; cin >> R >> C; vector<vector<int>> a(R,vector<int>(C-1)); vector<vector<int>> b(R-1,vector<int>(C)); vector<int> dist(R*C,INF); vector<vector<Edge>> edges(R*C); rep(i,R) rep(j,C-1) cin >> a[i][j]; rep(i,R-1) rep(j,C) cin >> b[i][j]; // edges rep(i,R) { rep(j,C) { // left if (j>0) edges[no(i,j,C)].push_back(Edge(a[i][j-1],no(i,j-1,C))); // right if (j<C-1) edges[no(i,j,C)].push_back(Edge(a[i][j],no(i,j+1,C))); // down if (i<R-1) edges[no(i,j,C)].push_back(Edge(b[i][j],no(i+1,j,C))); // up rep2(k,1,i+1) { edges[no(i,j,C)].push_back(Edge(1+k,no(i-k,j,C))); } } } // calc dijkstra_heap(no(0,0,C),edges,dist); // output // rep(i,R) { // rep(j,C) { // cout << dist[no(i,j,C)] << " "; // } // cout << endl; // } int ans = dist[no(R-1,C-1,C)]; cout << ans << endl; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define int long long int #define double long double #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define endl '\n' #define ff first #define ss second #define mp make_pair #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define rev(a) a.rbegin(),a.rend() #define all(a) a.begin(),a.end() #define setbits(x) (__builtin_popcount(x)) #define pi pair<int,int> #define precise(a,k) cout<<fixed<<setprecision(k)<<a; #define fill(s,x,n) vector<s> x(n); rep(i,0,n) cin>>x[i]; #define minpq(type) priority_queue<type,vector<type>,greater<type>> #define debug(...) cerr<<"{ "<<#__VA_ARGS__<<" = "<<(__VA_ARGS__)<<" }"<<endl; #define debugp(...) cerr<<"{ "<<#__VA_ARGS__<<" = "<<"("<<(__VA_ARGS__).ff<<","<<(__VA_ARGS__).ss<<")"<<" }"<<endl; #define debugv(...) cerr<<"{ "; for(int i=0; i<size(__VA_ARGS__); i++) cerr<<#__VA_ARGS__<<"["<<i<<"]"<<" = "<<(__VA_ARGS__)[i]<<" ; "; cerr<<"}"<<endl; #define debugpv(...) cerr<<"{ "; for(int i=0; i<size(__VA_ARGS__); i++) cerr<<#__VA_ARGS__<<"["<<i<<"]"<<" = "<<"("<<(__VA_ARGS__)[i].ff<<","<<(__VA_ARGS__)[i].ss<<")"<<" ; "; cerr<<"}"<<endl; #define mod 998244353 #define inf 1000000007 #define inf64 1000000000000000005 #define PI 3.141592653589793 void solve(){ int n,m; cin>>n>>m; fill(int,a,m); if(m==0){ cout<<1; return; } sort(all(a)); vector<int> v; int t = 1; int prev = 0; for(int x: a){ if(x-prev>1){ v.push_back(x-prev-1); t+=x-prev; } prev = x; } if(n-prev>0){ v.push_back(n-prev); t+=n-prev; } // debugv(v) if(v.size()<=1){ cout<<v.size(); return; } sort(all(v)); int ans = 0; for(int i=0; i<(int)v.size(); i++){ ans+=v[i]/v[0] + (bool)(v[i]%v[0]); } cout<<ans; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); /* #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif */ int test; // cin>>test; test=1; //sieve(); //int cse=1; while(test--){ // cout<<"Case #"<<cse<<": "; solve(); cout<<endl; //cse++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; constexpr int Inf = 2000000030; constexpr long long INF= 2000000000000000000; template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template<typename T> T modpow(T N, T P, T M){ if(P < 0) return 0; if(P == 0) return 1; if(P % 2 == 0){ T t = modpow(N, P/2, M); if(M == -1) return t * t; return t * t % M; } if(M == -1) return N * modpow(N,P - 1,M); return N * modpow(N, P-1, M) % M; } ll MOD = 1000000007; long long fac[2000010], finv[2000010], inv[2000010]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < 2000010; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { int N,M,K; cin >> N >> M >> K; if(N > M + K) { cout << 0 << endl; return 0; } COMinit(); long long ret = COM(N + M,N); ret -= COM(N + M,N - K - 1); if(ret < 0) ret += MOD; cout << ret << endl; }
//@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 ACompetition { public: static void solve(istream &cin, ostream &cout) { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int X, Y, Z; cin >> X >> Y >> Z; cout << (Y * Z - 1) / X << endl; } }; signed main() { ACompetition solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
//fake_name #include<bits/stdc++.h> using namespace std ; #define int long long int #define F first #define S second int MOD = 998244353 ; int mod = 1e9 + 7 ; int inf = 1e18 ; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); signed main() { ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ; int n , x ; cin >> n >> x ; for( int i = 0 ; i < n ; i++ ){ int y ; cin >> y ; if( y != x ) cout << y << ' ' ; } cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n"; }
#include<bits/stdc++.h> using namespace std; typedef long long int lli; typedef long double ld; /* #include <chrono> using namespace std::chrono; #include <boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<pair<lli,lli>, null_type,less<pair<lli,lli>>, rb_tree_tag,tree_order_statistics_node_update> */ #define endl "\n" #define pb push_back const lli mod=1e9+7; const lli mod1=998244353; #define fir first #define sec second #define plli pair<lli,lli> /* lli power(lli a, lli b) { lli res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } lli powermod(lli a, lli b) { lli res = 1; while (b > 0) { if (b & 1) res = ((res%mod)*(a%mod))%mod; a = (a * a)%mod; b >>= 1; } return res; } */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); lli T; T=1; //cin>>T; while(T--) { lli n,m; cin>>n>>m; if(m%n==0) cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
#include <bits/stdc++.h> #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; using ll = long long; using P = pair<int, ll>; int main() { int n; cin >> n; vector<string> s(n); vector<int> t(n); rep(i,n){ cin >> s[i] >> t[i]; } int mx = 0; rep(i,n){ mx = max(t[i], mx); } int mx2 = 0; string name; rep(i,n){ if(t[i] == mx){ continue; } if(t[i] > mx2){ mx2 = t[i]; name = s[i]; } } cout << name << endl; return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int N; int A[1000]; int B[1000]; int dist[1000][1000]; int doit(int k){ if(k >= N){ int i; for(i=(30)-1;i>=(0);i--){ if(((k) &(1<<(i)))){ k ^= (1<<i); break; } } } return k; } int main(){ int i; rd(N); for(i=(0);i<(N);i++){ A[i] = doit(2 * i); B[i] = doit(2 * i + 1); } for(i=(0);i<(N);i++){ wt_L(A[i]+1); wt_L(' '); wt_L(B[i]+1); wt_L('\n'); } return 0; } // cLay version 20201206-1 // --- original code --- // int N; // int A[1000], B[1000]; // // int dist[1000][1000]; // // int doit(int k){ // if(k >= N) rrep(i,30) if(BIT_ith(k,i)) k ^= (1<<i), break; // return k; // } // // { // rd(N); // rep(i,N){ // A[i] = doit(2 * i); // B[i] = doit(2 * i + 1); // } // rep(i,N) wt(A[i]+1, B[i]+1); // // // rep(i,N) rep(j,N) dist[i][j] = int_inf; // // rep(i,N) dist[i][A[i]] <?= 1; // // rep(i,N) dist[i][B[i]] <?= 1; // // rep(i,N) dist[i][i] = 0; // // rep(k,N) rep(i,N) rep(j,N) dist[i][j] <?= dist[i][k] + dist[k][j]; // // int mx = 0; // // rep(i,N) rep(j,N) mx >?= dist[i][j]; // // wt("mx", mx); // }
///Bismillahir Rahmanir Rahim #include "bits/stdc++.h" #define ll long long #define int ll #define fi first #define si second #define mp make_pair #define pb push_back #define pi pair<int,int> #define node(a,b,c) mp(mp(a,b),c) #define clr(x) memset(x,0,sizeof(x)); #define f(i,l,r) for(int i=l;i<=r;i++) #define rf(i,r,l) for(int i=r;i>=l;i--) #define done(i) cout<<"done = "<<i<<endl; #define show(x,y) cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl; #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; const ll inf=1e18; const int mod=1e9+7; const int M=12; int a[M][M]; main() { fast int n,k; cin>>n>>k; f(i,1,n) { f(j,1,n) { cin>>a[i][j]; } } int ses=0; vector<int>vec; f(i,2,n)vec.pb(i); sort(vec.begin(),vec.end()); do { int tym=a[1][vec[0]]+a[vec[n-2]][1]; int sz=n-1; for(int i=1;i<sz;i++) { int x=vec[i-1]; int y=vec[i]; tym+=(a[x][y]); } if(tym==k)ses++; }while(next_permutation(vec.begin(),vec.end())); cout<<ses<<"\n"; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll=long long; using vi=vector<int>; int main() { int N,K; cin>>N>>K; vector<vector<ll>> T(N,vector<ll>(N)); for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ cin>>T[i][j]; } } vi v; for (int i = 1; i < N; i++)v.push_back(i); int ans=0; do{ ll tmp=0; tmp+=T[0][v[0]]; for (int i = 0; i < N-2; i++){ tmp+=T[v[i]][v[i+1]]; } tmp+=T[v[N-2]][0]; if(tmp==K)ans++; }while(next_permutation(v.begin(),v.end())); cout<<ans<<endl; }
#include<bits/stdc++.h> #define pi acos(-1) #define ll long long using namespace std; void solve(); int main(){ ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int q=1;//cin>>q; while(q--){ solve(); } return 0; } void solve(){ int n;cin>>n; int a[n],p[n],x[n],ans=INT_MAX; for(int i=0;i<n;i++){ cin>>a[i]>>p[i]>>x[i]; if(x[i]-a[i]>0){ ans=min(ans,p[i]); } } if(ans==INT_MAX) cout<<-1<<endl; else cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define print_upto(a, n) \ for (ll i = 1; i <= n; i++) \ cout << a[i] << " "; \ cout << endl #define take(a, n) \ for (ll i = 1; i <= n; i++) \ cin >> a[i]; #define watch(x) cout << (#x) << " is " << (x) << "\n" #define watch2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << "\n" #define watch3(x, y, z) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << " and " << (#z) << " is " << (z) << "\n" #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) { } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define ff first #define ss second #define null NULL #define all(c) (c).begin(), (c).end() #define nl "\n" #define ld long double #define eb emplace_back #define pb push_back #define pf push_front #define MOD 1000000007 #define inf 1e17 // cout << fixed << setprecision(9) << ans << nl; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef map<ll, ll> mll; const ll N = 200009; void solve() { ll n; cin >> n; vector<ll> v; for (ll i = 1; i <= n; i++) { ll a, p, x; cin >> a >> p >> x; x = x - a; if (x > 0) { v.pb(p); } } sort(all(v)); if (v.size() == 0) { cout << -1 << nl; } else { cout << v[0] << nl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll T = 1; // cin >> T; while (T--) { solve(); } return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <iomanip> #include <unordered_map> #include <unordered_set> #include <algorithm> #include <map> #include <set> #include <cmath> using namespace std; using VLL = vector <long long>; using VVLL = vector <VLL>; long long gcd(long long a, long long b) { return b ? gcd(b, a%b) : a; } int main() { long long A; long long B; cin >> A >> B; VLL primesList = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; // p <= 72 unordered_map <long long, long long> pMask; for (long long n = A; n <= B; ++n) { pMask[n] = 0; for (int j = 0; j < primesList.size(); ++j) { if (gcd(n, primesList[j]) != 1) { pMask[n] |= 1<<j; } } } int UPPER = 1 << primesList.size(); // cout << "DEBUG SIZE " << primesList.size() << endl; VVLL dp(2, VLL(UPPER)); dp[0][0] = dp[1][0] = 1; for (long long n = A; n <= B; ++n) { for (int i = 0; i < UPPER; ++i) { if ((i & pMask[n]) == 0) { dp[1][i | pMask[n]] += dp[0][i]; } } dp[0] = dp[1]; } long long ans = 0; for (int i = 0; i < UPPER; ++i) { ans += dp[0][i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ln '\n' #define all(dat) dat.begin(), dat.end() #define loop(i, to) for (__typeof(to) i = 0; i < to; ++i) #define cont(i, to) for (__typeof(to) i = 1; i <= to; ++i) #define circ(i, fm, to) for (__typeof(to) 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 = 74; const int hsz = int(1e7) + 39, esz = 2e6; bitset<nsz> g[nsz + 5]; int vis[nsz + 5], pw[nsz + 5]; num n, fm, to, ans = 1, a[nsz + 5]; struct hash_table { int sz, hd[hsz + 5]; struct node { int nxt; bitset<nsz> w; node() {} node(int nxt, bitset<nsz> w): nxt(nxt), w(w) {} } e[esz + 5]; int inline to_key(const bitset<nsz> &w) { int u = 0; cont (i, n) u = (u * 2 + w[i]) % hsz; return u; } void inline ins(const bitset<nsz> &w, int u) { e[++sz] = node(hd[u], w), hd[u] = sz; } bool inline qry(const bitset<nsz> &w, int u) { for (int i = hd[u]; i; i = e[i].nxt) { if (e[i].w == w) return 1; } return 0; } }; hash_table dat; num gcd(num a, num b) { return !b ? a : gcd(b, a % b); } void bfs(int s, int &sz, int *nd) { static int q[nsz + 5]; int ql = 0, qr = 0; for (q[qr++] = s, vis[s] = s; ql != qr;) { int u = q[ql++]; nd[sz++] = u; cont (v, n) if (g[u][v] && !vis[v]) q[qr++] = v, vis[v] = s; } } num inline calc(int s) { static int nd[nsz + 5], d[esz + 5]; static bitset<nsz> q[esz + 5], p[esz + 5]; int sz = 0, ql = 0, qr = 1, cnt = 0; bfs(s, sz, nd); if (sz == 1) return 2; for (; ql != qr; ++cnt) { bitset<nsz> cur = q[ql], ban = p[ql], nban; int key = d[ql]; ++ql; loop (i, sz) if (!ban[nd[i]]) { int v = nd[i], nkey = (key + pw[v]) % hsz; cur[v] = 1, nban = ban | g[v]; if (!dat.qry(cur, nkey)) { q[qr] = cur, p[qr] = nban, d[qr] = nkey; ++qr; dat.ins(cur, nkey); } cur[v] = 0; } } return cnt; } void inline init() { pw[0] = 1; cont (i, n) pw[i] = pw[i - 1] * 2 % hsz; } int main() { scanf("%lld%lld", &fm, &to); n = to - fm + 1; circ (i, fm, to) a[num(i) - fm + 1] = i; cont (u, n) cont (v, u) g[u][v] = g[v][u] = gcd(a[u], a[v]) > 1; init(); cont (u, n) if (!vis[u]) ans *= calc(u); printf("%lld\n", ans); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int M=2e5+1; struct BIT{ ll bit[200004]; int dat[200004]; void init(){ for(int i=0;i<=M;i++)bit[i]=dat[i]=0; } void add(int id,int a,int b){ for(;id;id-=(id&-id))dat[id]+=a,bit[id]+=b; } ll got1(int id){ ll res=0; for(;id<=M;id+=(id&-id))res+=bit[id]; return res; } int got2(int id){ int res=0; for(;id<=M;id+=(id&-id))res+=dat[id]; return res; } }X,Y; vector<int>all; int n,m,Q; ll ans=0; int a[200005],b[200004]; int x[200004],y[200004],t[200004]; ll gotx(int t){ return Y.got1(t)+(ll)(m-Y.got2(t))*all[t]; } ll goty(int t){ return X.got1(t)+(ll)(n-X.got2(t))*all[t]; } int main(){ X.init(),Y.init(); cin>>n>>m>>Q; all.push_back(0); for(int i=1;i<=Q;i++){ cin>>t[i]>>x[i]>>y[i],all.push_back(y[i]); } all.push_back(-1); sort(all.begin(),all.end()),all.erase(unique(all.begin(),all.end()),all.end()); for(int i=1;i<=n;i++)a[i]=1,X.add(1,1,0); for(int i=1;i<=m;i++)b[i]=1,Y.add(1,1,0); for(int i=1;i<=Q;i++){ y[i]=lower_bound(all.begin(),all.end(),y[i])-all.begin(); if(t[i]==1){ ans-=gotx(a[x[i]]);X.add(a[x[i]],-1,-all[a[x[i]]]); a[x[i]]=y[i]; ans+=gotx(a[x[i]]);X.add(a[x[i]],1,all[a[x[i]]]); }else{ ans-=goty(b[x[i]]);Y.add(b[x[i]],-1,-all[b[x[i]]]); b[x[i]]=y[i]; ans+=goty(b[x[i]]);Y.add(b[x[i]],1,all[b[x[i]]]); } cout<<ans<<"\n"; } }
// Jai Shree Ram #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ook order_of_key #define fbo find_by_order #define rep(i,a,n) for(int i=a;i<n;i++) #define ll long long #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define endl "\n" #define x first #define y second #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define sz(a) (int)a.size() #define pii pair<int,int> #define hell 1000000007 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;} template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;} const int N=1e9+5; const int M=1e7; struct node{ int l;int r; int val; int cnt; }t[M]; int cnt=0; int n; void extend(int v){ if(t[v].l==0){ t[v].l=++cnt; t[v].r=++cnt; } } void pushup(int v){ t[v].val = t[t[v].l].val + t[t[v].r].val; t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt; } void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){ if(tl>r || l>tr)return; if(l<=tl && tr<=r){ assert(tl == tr); t[v].val += val.x; t[v].cnt += val.y; return; } extend(v); int mid=(tl+tr)/2; update(t[v].l,l,r,val,tl,mid); update(t[v].r,l,r,val,mid+1,tr); pushup(v); } pii query(int v,int l,int r,int tl=0,int tr=1e9){ if(tl>r || l>tr)return {0,0}; if(l<=tl && tr<=r){ return {t[v].val,t[v].cnt}; } int mid=(tl+tr)/2; extend(v); pii a =query(t[v].l,l,r,tl,mid); pii b =query(t[v].r,l,r,mid+1,tr); return {a.x+b.x,a.y+b.y}; } int solve(){ int n,m,q; cin >> n >> m >> q; int ans = 0; int roota = ++cnt; int rootb = ++cnt; vector<int>v1(n+1),v2(m+1); update(roota,0,0,{0,n}); update(rootb,0,0,{0,m}); rep(i,0,q){ int t; cin >> t; int x,y; cin >> x >> y; if(t == 1){ if(y > v1[x]){ auto s = query(rootb,0,y); ans += s.y*y; s = query(rootb,v1[x]+1,y); ans -= s.x; s = query(rootb,0,v1[x]); ans -= v1[x]*s.y; } else if(y < v1[x]){ auto s = query(rootb,0,v1[x]); ans -= s.y*v1[x]; s = query(rootb,0,y); ans += s.y*y; s = query(rootb,y+1,v1[x]); ans += s.x; } update(roota,v1[x],v1[x],{-v1[x],-1}); update(roota,y,y,{y,1}); v1[x] = y; } else{ if(y > v2[x]){ auto s = query(roota,0,y-1); ans += s.y*y; s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,v2[x],y-1); ans -= s.x; } else if(y < v2[x]){ auto s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,0,y-1); ans += s.y*y; s = query(roota,y,v2[x]-1); ans += s.x; } update(rootb,v2[x],v2[x],{-v2[x],-1}); update(rootb,y,y,{y,1}); v2[x] = y; } cout << ans << endl; } return 0; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t=1;//cin>>t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> #define For(i,j,k) for(int i=j;i<=k;++i) #define Dow(i,j,k) for(int i=k;i>=j;--i) #define ll long long #define pb push_back #define fir first #define sec second #define pb push_back #define pa pair<int,int> #define mk make_pair using namespace std; inline ll read() { ll t=0,dp=1;char c=getchar(); while(!isdigit(c)) {if(c=='-') dp=-1;c=getchar();} while(isdigit(c)) t=t*10+c-48,c=getchar(); return t*dp; } inline void write(ll x){if(x<0) {putchar('-');x=-x;} if(x>=10) write(x/10);putchar(x%10+48);} inline void writeln(ll x){write(x);puts("");} inline void write_p(ll x){write(x);putchar(' ');} const int N=3005; int n,k; int dp[N][N],sum[N][N],mo=998244353;; int main() { n=read();k=read(); if(n==k) {cout<<1<<endl;return 0;} For(i,1,k) dp[i][i]=sum[i][i]=1; For(i,1,k) For(j,1,i) sum[i][j]=1; For(i,1,n-k) { Dow(j,1,i-1) { dp[i][j]=sum[i-j][(j+1)/2]; sum[i][j]=(sum[i][j+1]+dp[i][j])%mo; } } int ans=0; For(j,1,n-k) ans=(ans+dp[n-k][j])%mo; writeln(ans); }
#include <cstdio> #include <algorithm> typedef long long LL; const int Mod = 998244353; const int MN = 3005; int N, K, f[MN][MN]; int main() { scanf("%d%d", &N, &K), N -= K; f[0][0] = 1; for (int i = 1; i <= N; ++i) { f[i][0] = 0; for (int j = 1; j <= i; ++j) f[i][j] = (f[i][j - 1] + f[i - j][std::min(i - j, 2 * j)]) % Mod; } printf("%d\n", f[N][std::min(N, K)]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int A; cin >> A; int B; cin >> B; cout << N - A + B << endl; }
#include<bits/stdc++.h> typedef long long ll; //forループ //引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか //Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる #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--) // // using namespace std; int ctoi(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } long long a,b,c,s,t,x,y,z; long long A,B,C,S,T,X,Y,Z; long long cnt; long double pi=3.14159265358979323846; string N,M; unsigned long long D; ll gcd(ll a,ll b){ if(a==1 || b==1) return 1; if(a==0) return b; return gcd(b%a,a); } int main(){ cin>>x>>a>>b; cout<<x-a+b; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define pb emplace_back #define mp make_pair #define fi first #define se second #define all(v) v.begin(),v.end() #define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0); #define mod 1000000007 #define decimal(k) cout<<fixed<<setprecision(k)<<endl #define LL_MAX LLONG_MAX #define yes cout<<"Yes"<<endl #define no cout<<"No"<<endl int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;} int modinv(int x){return exp(x,mod-2);} int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;} int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;} int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;} int gcd(int a, int b){if (b == 0)return a; return gcd(b, a % b);} int Log2n( int n){ return (n > 1) ? 1 + Log2n(n / 2) : 0;} int fac[100009];int ncr_mod(int n,int k){int ans=fac[n];ans*=modinv(fac[k]);ans%=mod;ans*=modinv(fac[n-k]);ans%=mod;return ans;} vector<int>v_prime;void Sieve(int n){bool prime[n + 1];memset(prime,true,sizeof(prime));for (int p = 2; p*p <=n;p++){if(prime[p] ==true) {for(int i = p*p; i<= n; i += p)prime[i]=false;}}for(int p = 2;p<= n;p++)if (prime[p])v_prime.pb(p);} vector<int>v_factor;void factors(int n){ for (int i=1; i<=sqrt(n); i++) {if (n%i == 0) {if (n/i == i) v_factor.pb(i);else { v_factor.pb(i),v_factor.pb(n/i);};} } sort(all(v_factor)); } void out(vector<int>&a){for(int i=0;i<a.size();i++) cout<<a[i]<<" "; cout<<endl;} bool convertOpposite(string str) { int ln = str.length(); // Conversion according to ASCII values for (int i = 0; i < ln; i++) { if(i%2==0) { if (str[i] >= 'a' && str[i] <= 'z') { } else return false; } if(i%2==1) { if (str[i] >= 'A' && str[i] <= 'Z') { } else return false; } } return true; } signed main() { run; /* open for mod factorial fac[0]=1; for(int i=1;i<100009;i++) { fac[i]=fac[i-1]*i; fac[i]%=mod; } */ /* for sieve open this and use v_prime int pp=pow(10,6)+100000; Sieve(pp); */ // USE v_factor For calculating factors int t=1; //cin>>t; while(t--) { // vector< pair <int,int> > vp; // map<int,int>mp; // x.second-->frequency // set<int>st; int n,i,x,y,ok=0,sum=0,ans=0,j,k,cnt=0,m,c=0; int h[100009]={0}; string s; cin>>s; if(convertOpposite(s)==true) yes; else no; } }
// Etavioxy #include<cstdio> #include<cctype> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #define il inline #define ll long long #define rep(i,s,t) for(register int i=(s);i<=(t);i++) #define rev_rep(i,s,t) for(register int i=(s);i>=(t);i--) #define each(i,u) for(int i=head[u];i;i=bow[i].nxt) #define file(s) freopen(s".in" ,"r",stdin),freopen(s".out","w",stdout) #define pt(x) putchar(x) using namespace std; const int mod = 1e9+7; il ll qpow(ll a,ll b){ ll ans= 1; for(; b; b>>=1,a=a*a%mod) if( b&1 ){ ans= ans*a%mod; } return ans; } il ll inv(ll x){ return qpow(x,mod-2); } il ll comp(ll n,ll m){ if( m>n-m ) m = n-m; if( n<0 or m<0 ) return 0; ll up=1, down=1; rep(i,1,m){ up = (up*(n-i+1))%mod; down = (down*i)%mod; } return up*inv(down)%mod; } int main(){ int n,m,k; cin>>n>>m>>k; if( n>m+k ){ cout<<0<<endl; return 0; } cout<<((comp(n+m,n)-comp(n+m,n-k-1))%mod+mod)%mod<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int nax = 805; int n, k, a[nax][nax]; int sum[nax][nax]; int main() { cin >> n >> k; for(int i = 1; i <= n; i += 1) for(int j = 1; j <= n; j += 1) cin >> a[i][j]; int L = 0, R = 1e9; while(L < R) { int M = (L + R + 1) / 2; for(int i = 1; i <= n; i += 1) for(int j = 1; j <= n; j += 1) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (a[i][j] >= M ? 1 : -1); } int ok = 1; for(int i = k; i <= n; i += 1) { for(int j = k; j <= n; j += 1) { if(sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] <= 0) ok = 0; } } if(ok) L = M; else R = M - 1; } cout << L << endl; } //设x是所求最小的中位数,若M <= x, 所有的k * k的sum矩阵值为正;若M > x,所有的k * k的sum矩阵非正。所以二分求最大值即可
#include<bits/stdc++.h> using namespace std; #define GODSPEED ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15); #define f first #define s second #define newl cout<<"\n"; #define pb push_back #define mset(a,x) memset(a,x,sizeof(a)) #define debv(a) for(auto it: a)cout<<it<<" ";newl; #define deb1(a) cout<<a<<"\n"; #define deb2(a,b) cout<<a<<" "<<b<<"\n"; #define deb3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n"; #define deb4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"; #define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin()); #define all(a) a.begin(),a.end() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; const ll N = 2e3 + 5; const ll mod = 1e9 + 7; const ll INF = 1e18; const int INFi = 0x7f7f7f7f; const ll LEVEL = log2(N) + 1; int n, k, a[805][805], b[805][805], x[805][805], y[805][805]; int f(int mid) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { b[i][j] = a[i][j] <= mid; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { x[i][j] = y[i][j] = b[i][j]; if (i) y[i][j] += y[i - 1][j]; if (j) x[i][j] += x[i][j - 1]; } } int ct = 0; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { ct += b[i][j]; } } for (int i = 0; i <= n - k; i++) { if (i % 2 == 0) { if (i) { // for (int j = 0; j < k; j++) ct -= x[i - 1][k - 1]; ct += x[i - 1 + k][k - 1]; } // deb1(ct) if (ct >= (k * k + 1) / 2) return 1; for (int j = 0; j <= n - k - 1; j++) { // for (int ii = i; ii < i + k; ii++) ct -= y[i + k - 1][j]; if (i) ct += y[i - 1][j]; ct += y[i + k - 1][j + k]; if (i) ct -= y[i - 1][j + k]; // deb1(ct) if (ct >= (k * k + 1) / 2) return 1; } } else { // for (int j = n - 1; j > n - k - 1; j--) ct -= x[i - 1][n - 1]; if (n - k) ct += x[i - 1][n - k - 1]; ct += x[i + k - 1][n - 1]; if (n - k) ct -= x[i + k - 1][n - k - 1]; // deb1(ct) if (ct >= (k * k + 1) / 2) return 1; for (int j = n - 1; j >= k; j--) { // for (int ii = i; ii < i + k; ii++) ct -= y[i + k - 1][j]; if (i) ct += y[i - 1][j]; ct += y[i + k - 1][j - k]; if (i) ct -= y[i - 1][j - k]; // deb1(ct) if (ct >= (k * k + 1) / 2) return 1; } } } return 0; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } ll l = 0, r = 1e9, ans; while (l <= r) { ll mid = (l + r) / 2; if (f(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } deb1(ans) } int main() { GODSPEED; int test = 1; // cin >> test; for (int i = 1; i <= test; i++) { solve(); } }
#ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #pragma region macros using str=string; using ll=long long; using vl=vector<ll>; using vc=vector<char>; using pl=pair<ll,ll>; using ml=map<ll,ll>; using sl=set<ll>; template<class T> using V=vector<T>; template<class T,class U> using P=pair<T,U>; #define FOR(n) for(ll i=0;i<n;i++) #define rep(i,n) for(ll i=0;i<n;i++) #define reps(i,n) for(ll i=1;i<n;i++) #define REP(i,m,n) for(ll i=m;i<n;i++) #define drep(i,n) for(ll i=n-1;i>=0;i--) #define fore(n) for(auto&& i:n) #define fors(n) for(auto&&[i,j]:n) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define sor(v) sort(all(v)) #define rsor(v) sort(rall(v)) #define rev(v) reverse(all(v)) #define low(v,x) lower_bound(all(v),x)-v.begin() #define up(v,x) upper_bound(all(v),x)-v.begin() #define acc(v) accumulate(all(v),0LL) #define ef(x) emplace_front(x) #define eb(x) emplace_back(x) #define pf() pop_front() #define pb() pop_back() #define mp(a,b) make_pair(a,b) #define ceil(a,b) (a+b-1)/b #define bit(n) (1LL<<n) #define fi first #define se second #define fr front() #define ba back() #define be begin() #define en end() #define br break #define cn continue #define wh while #define el else if #define re return const ll inf=1LL<<60; const ll mod=1000000007; const ll MOD=998244353; const double pi=3.1415926535; const double eps=1e-10; const ll dx[4]={1,0,-1,0}; const ll dy[4]={0,1,0,-1}; void input(){} void inputs(){} void output(){} template<class T,class...U> void input(T& x,U&...y){cin>>x;input(y...);} template<class...T> void in(T&...x){input(x...);} template<class T> void inputs(const ll t,T& x){cin>>x[t];} template<class T,class...U> void inputs(const ll t,T& x,U&...y){cin>>x[t];inputs(t,y...);} template<class T,class...U> void inputs(T& x,U&...y){rep(t,size(x))inputs(t,x,y...);} template<class...T> void ins(T&...x){inputs(x...);} template<class T,class...U> void output(T x,U...y){cout<<x<<"\n";output(y...);} template<class...T> void out(T...x){output(x...);} template<class...T> void fin(T...x){out(x...);exit(0);} template<class T> istream&operator>>(istream& i,V<T>& v){for(T& x:v)i>>x;re i;} template<class T,class U> istream&operator>>(istream& i,P<T,U>& p){re i>>p.fi>>p.se;} template<class T> ostream&operator<<(ostream& o,V<T>& v){for(T& x:v)o<<x<<" ";re o;} template<class T,class U> ostream&operator<<(ostream& o,P<T,U>& p){re o<<p.fi<<" "<<p.se;} void yn(bool b){fin((b?"Yes":"No"));} void YN(bool b){fin((b?"YES":"NO"));} void pos(bool b){fin((b?"POSSIBLE":"IMPOSSIBLE"));} template<class T,class U> void chmax(T& a,U b){if(a<b)a=b;} template<class T,class U> void chmin(T& a,U b){if(a>b)a=b;} template<class T,class U> auto max(T a,U b){re a>b?a:b;} template<class T,class U> auto min(T a,U b){re a<b?a:b;} ll qp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r*=x;x*=x;n>>=1;}re r;} ll mdp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%mod;x=x*x%mod;n>>=1;}re r;} ll MDp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%MOD;x=x*x%MOD;n>>=1;}re r;} struct UnionFind{ vl par,sz; UnionFind(ll n):par(n),sz(n,1){rep(i,n) par[i]=i;} ll rot(ll x){ if(par[x]==x) re x; re par[x]=rot(par[x]); } void uni(ll x,ll y){ x=rot(x),y=rot(y); if(x==y) re; if(sz[x]<sz[y]) swap(x,y); sz[x]+=sz[y]; par[y]=x; } bool eql(ll x,ll y){re rot(x)==rot(y);} ll siz(ll x){re sz[rot(x)];} }; struct IOSetup{ IOSetup(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); } }iosetup; #pragma endregion signed main(){ ll h,w; in(h,w); V<str> s(h); in(s); ll ans=1; V<vc> v(h+w-1); rep(i,h) rep(j,w) v[i+j].eb(s[i][j]); rep(j,h+w-1){ bool a=0,b=0; fore(v[j]){ if(i=='R') a=1; if(i=='B') b=1; } if(a&&b) fin(0); if(!a&&!b){ ans*=2; ans%=MOD; } } out(ans); }
#include <bits/stdc++.h> #define int long long int #define all(x) x.begin(), x.end() #define send ios_base::sync_with_stdio(false); #define help cin.tie(NULL) #define inf (int)(1e17+1) #define mod (int)(998244353) #define N (int)(2e5+5) #define fi first #define se second #define endl "\n" #define double long double #define eps (double)(1e-9) #define sa cout<<"sa"<<endl using namespace std; namespace math{ vector <int> fac(N,1); int exp(int a,int b){ if(!b) return 1; if(b&1) return a*exp(a,b-1)%mod; int t=exp(a,b/2); return t*t%mod; } void fc(){for(int i=2;i<N;i++)fac[i]=i*fac[i-1]%mod;} int com(int n,int r){return (fac[n]*exp(fac[r]*fac[n-r]%mod,mod-2))%mod;} int per(int n,int r){return (fac[n]*exp(fac[n-r],mod-2))%mod;} vector <int> getpr(int x){ vector <int> res;if(x!=1) res.push_back(x); for(int i=2;i*i<=x;i++){ if(i*i==x) res.push_back(i); else if(x%i==0) res.push_back(i),res.push_back(x/i);; }return res;} }using namespace math; void solve(){ int n,m; cin>>n>>m; vector <string> v(n); for(int i=0;i<n;i++) cin>>v[i]; int ans=0; vector <int> path(m+n); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(v[i][j]=='B'&&path[i+j]==1) {cout<<0;return;} if(v[i][j]=='R'&&path[i+j]==2) {cout<<0;return;} if(v[i][j]=='B') path[i+j]=2; else if(v[i][j]=='R') path[i+j]=1; } } for(int i=0;i<n+m-1;i++) ans+=!path[i]; cout<<exp(2,ans); } int32_t main(){ send help; #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t=1; //cin>>t; while(t--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define rg register #define In inline #define ll long long #define rep(i,l,r) for(rg int i = (l);i <= (r);i++) #define dwn(i,r,l) for(rg int i = (r);i >= (l);i--) #define mp make_pair #define fi first #define se second const int N = 2e5; namespace IO{ In ll read(){ ll s = 0,ww = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')ww = -1;ch = getchar();} while('0' <= ch && ch <= '9'){s = 10 * s + ch - '0';ch = getchar();} return s * ww; } In void write(ll x){ if(x < 0)putchar('-'),x = -x; if(x > 9)write(x / 10); putchar('0' + x % 10); } }; using namespace IO; ll n,v[2*N+5]; priority_queue<ll,vector<ll>,greater<ll> > pq; void solve(){ rep(i,1,n){ pq.push(v[n+i]); pq.push(v[n-i+1]); pq.pop(); } ll ans = 0; while(!pq.empty()){ ans += pq.top(); pq.pop(); } write(ans),putchar('\n'); } int main(){ // freopen("B.in","r",stdin); // freopen("B.out","w",stdout); // USE LONG LONG n = read(); rep(i,1,2 * n)v[i] = read(); solve(); return 0; } // T: Think // E: Enough array size // M: Memory limit // P: Precision // B: Boundary // O: Overflow // T: Time limit // M: Mod // F: File
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(i=0;i<n;i++) #define Rep(i,x,n) for(i=x;i<=n;i++) #define foreach(c,itr) for(__typeof(c)::iterator itr=c.begin();itr!=c.end();itr++) #define all(v) v.begin(),v.end() #define p_b push_back #define fr first #define sc second #define m_p make_pair #define zero(a) memset(a,0,sizeof(a)) #define set_P setprecision typedef long long ll; typedef long double ld; const ll INF=0x3f3f3f3f; ll gcd(ll x,ll y) {return x%y==0 ? y:gcd(y,x%y);} int i,j; int n,k; int a[300005]; int num[300005]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>k; Rep(i,1,n){ cin>>a[i]; num[a[i]]++; } ll ans=0; for(i=1;i<=k;i++){ int pos=0; while(num[pos]>0){ num[pos]--; pos++; } ans+=pos; } cout<<ans<<endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <memory> #include <list> #include <deque> #include <queue> #include <iomanip> #include <stack> #include <string.h> using namespace std; #define REP(i,n) for (decltype(n) i = 0; i < n; i++) using ll = long long; using ivec = vector<int>; using lvec = vector<long>; using ipair = pair<int, int>; using llvec = vector<ll>; using llpair = pair<ll, ll>; #define umap unordered_map #define uset unordered_set #define THE_MOD 1000000007 template<typename T> vector<T> read_args() { T N; cin >> N; vector<T> v(N); for (auto& e : v) { cin >> e; } return v; } template<class T> void sort(T& v) { sort(v.begin(), v.end()); } template<class T> void rsort(T& v) { sort(v.begin(), v.end(), greater<typename T::value_type>()); } void answer(bool b) { cout << (b ? "Yes" : "No") << endl; } ll run() { // int a,b,c; cin>>a>>b>>c; int a,b,x,y;cin >> a>>b>>x>>y; if (a == b) { return x; } if (a < b) { int t1 = (b - a) * y + x; int t2 = (b - a) * (2 * x) + x; return min(t1, t2); } { int t1 = (a - b - 1) * y + x; int t2 = (a - b - 1) * (2 * x) + x; return min(t1, t2); } } int main() { // std::setprecision(15) auto r = run(); cout << r << endl; return 0; }
#include <iostream> using namespace std; #define ll long long const ll mod = 1e9 + 7; const ll MAX = 2e5 + 1; ll t; ll power(ll x, ll p) { ll ans = 1; while (p) { if (p & 1) { ans = (ans * x) % mod; p--; } x = (x * x) % mod; p >>= 1; } return ans; } ll add(ll x, ll y) { return ((x % mod) + (y % mod)) % mod; } ll mul(ll x, ll y) { return ((x % mod) * (y % mod)) % mod; } ll sub(ll x, ll y) { return ((x % mod) - (y % mod) + mod) % mod; } ll nC2(ll x) { return mul(mul(x, x - 1), power(2, mod - 2)); } void Solve() { ll n, a, b; cin >> n >> a >> b; if (a + b > n) { cout << 0 << endl; return; } ll tot = mul(mul(4, nC2(n - a - b + 2)), mul(n - a + 1, n - b + 1)); ll overlap = mul(4, mul(nC2(n - a - b + 2), nC2(n - a - b + 2))); ll ans = sub(tot, overlap); cout << ans << endl; } signed main() { cin >> t; while (t--) { Solve(); } }
#include <math.h> #include <iostream> #include <string> #include <iomanip> #include <algorithm> #include <vector> #include <cmath> #include<set> #include <sstream> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(a) (a).begin(),(a).end()//配列を全部参照 using namespace std; using ll = long long int; //vector<vector<int>> hako(n, vector<int>(2)); int main() { int h,w,x,y; string s; int ans=1; cin >> h >> w >> x >>y; vector<vector<char>> hako(h, vector<char>(w)); rep(i, h)rep(j, w) cin >> hako[i][j]; x--; y--; for (int i = x+1; i < h; i++) { if (hako[i][y] == '#')break; ans++; } for (int i = x-1; i >= 0; i--) { if (hako[i][y] == '#')break; ans++; } for (int i = y+1; i < w; i++) { if (hako[x][i] == '#')break; ans++; } for (int i = y-1; i >= 0; i--) { if (hako[x][i] == '#')break; ans++; } cout << ans << endl; return 0; }
/*{{{ #include */ #include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <map> #include <tuple> #include <algorithm> #include <utility> #include <cmath>/*}}}*/ /*{{{ namespace R2357 */ namespace R2357 { inline void IN(void){return;} template<class F,class... R>inline void IN(F& f,R&... r){std::cin>>f;IN(r...);} template <class T>inline void OUT(T x){std::cout<<x<<'\n';} template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<class T=int>T read(){ T x=0; char c; while(((c=getchar())>'9'||c<'0')&&c!='-'); const bool f=(c=='-')&&(c=getchar()); while(x=x*10-48+c,(c=getchar())>='0'&&c<='9'); return f?-x:x; } template<class T>std::istream& operator>>(std::istream& is,std::vector<T>& V){ for(auto& v:V)is>>v; return is; } template<class T1,class T2> std::istream& operator>>(std::istream& is,std::pair<T1,T2>& P){ return is>>P.first>>P.second; } template<class T>inline T ceil_div(T a,T b){ return (a+(b-1))/b; } namespace debug { using namespace std; void Write(void){cerr<<endl;} template<class F,class...R>void Write(F f,R...r){cerr<<' '<<f;Write(r...);} template<class T>void Write(vector<T>& V,int n=-1){ if(n==-1) n = V.size(); for(int i=0;i<n;i++)cerr<<' '<<V[i]; cerr<<endl; } template<class T,class U> void Write(pair<T,U>& p){cerr<<' '<<'{'<<p.first<<','<<p.second<<'}'<<endl;} #include <cxxabi.h> template<class T>string TypeName(T& x){ return string(abi::__cxa_demangle(typeid(T).name(), 0, 0, 0)); } } }/*}}}*/ /*{{{ using */ using namespace std; using namespace R2357; using ll=long long; using ld=long double; using pint=pair<int,int>;/*}}}*/ /*{{{ #define */ #define rep(i,a,b) for(ll i=a;i<ll(b);i++) #define repr(i,a,b) for(ll i=a;i>=ll(b);i--) #define each(x,v) for(auto& x:v) #define el '\n' #define ALL(x) x.begin(),x.end() #define ALLR(x) x.rbegin(),x.rend() #define Unique(x) sort(x.begin(),x.end());x.erase(unique(x.begin(),x.end()),x.end()) #define Fill(v,n) fill(v,v+sizeof(v)/sizeof(*v),n) #define max_element(x) *max_element(x.begin(),x.end()) #define min_element(x) *min_element(x.begin(),x.end()) #define INF 1073741824 // 2e30 #define INFL 2305843009213693952ll // 2e61 #define _FASTIO_ ios::sync_with_stdio(false),cin.tie(nullptr); /*}}}*/ int main(){ _FASTIO_ int v, t, s, d; IN(v, t, s, d); t *= v; s *= v; if(t <= d && d <= s) OUT("No"); else OUT("Yes"); return 0; }
#include <bits/stdc++.h> #if MYDEBUG #include "lib/cp_debug.hpp" #else #define DBG(...) ; #endif #if __cplusplus <= 201402L template <typename T> T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #endif using LL = long long; constexpr LL LINF = 334ll << 53; constexpr int INF = 15 << 26; constexpr LL MOD = 1E9 + 7; namespace Problem { using namespace std; struct RangeSum { using T = double; T operator()(const T &a, const T &b) { return a + b; } static constexpr T id() { return T(0); }; }; template <typename M> struct SegmentTree { using Val = typename M::T; M calc; vector<Val> data; int n; explicit SegmentTree(int size) { n = 1; while (n < size) n <<= 1; data = vector<Val>(n << 1, calc.id()); } explicit SegmentTree(vector<Val> &v) { n = 1; while (n < v.size()) n <<= 1; data = vector<Val>(n << 1); copy(v.begin(), v.end(), data.begin() + n); for (int i = n + (int)v.size(); i < 2 * n; i++) data[i] = calc.id(); for (int i = n - 1; i > 0; --i) { data[i] = calc(data[i * 2], data[i * 2 + 1]); } } void update(int i, Val v) { int k = i + n; data[k] = v; for (k >>= 1; k >= 1; k >>= 1) data[k] = calc(data[k * 2], data[k * 2 + 1]); } Val query(int l, int r) { //get[a,b) Val resl = calc.id(), resr = calc.id(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = calc(resl, data[l++]); if (r & 1) resr = calc(data[r - 1], resr); } return calc(resl, resr); } }; class Solver2 { public: int n, m; vector<int> is_return; SegmentTree<RangeSum> a_0, a_x; Solver2(LL n, LL m) : n(n), m(m), is_return(n + 1), a_0(n + 1), a_x(n + 1){}; void solve() { int k; cin >> k; for (int i = 0; i < k; ++i) { int tmp; cin >> tmp; is_return[tmp] = 1; } for (int i = 0, j = 0; i <= n; ++i) { if (is_return[i]) { j++; if (j == m) { cout << -1 << endl; return; } } else { j = 0; } } a_0.update(n, 0); a_x.update(n, 0); for (int i = n - 1; i > 0; --i) { if (is_return[i]) { a_0.update(i, 0); a_x.update(i, 1); } else { double sum_0 = a_0.query(i + 1, min(i + m, n) + 1); double sum_x = a_x.query(i + 1, min(i + m, n) + 1); a_0.update(i, sum_0 / m + 1.0); a_x.update(i, sum_x / m); if (i > 99900) DBG(i, m - (n - i), sum_0 / m + 1.0, sum_x / m) } } double sum_0 = a_0.query(1, min(m, n) + 1); double sum_x = a_x.query(1, min(m, n) + 1); DBG(sum_0 / m + 1.0, sum_x / m) double ans = (sum_0 / m + 1.0) / (1.0 - sum_x / m); cout << ans << endl; } }; } // namespace Problem int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(12); long long n = 0, m; std::cin >> n >> m; Problem::Solver2 sol(n, m); sol.solve(); return 0; }
#include<bits/stdc++.h> #define Pair pair<ll,int> #define ar array #define ll long long #define ull unsigned long long #define pb push_back using namespace std ; const ll LLMAX = 9223372036854775807; const int INF = 1e9; struct Exp{ long double a, b; }; Exp operator +(Exp x, Exp y){ return {x.a+y.a, x.b+y.b}; } Exp operator -(Exp x, Exp y){ return {x.a-y.a, x.b-y.b}; } Exp operator /(Exp x, long double d){ return {x.a/d, x.b/d}; } bool mark[200005]; Exp dp[200005]; Exp sum[200005]; int n, m, k; int main(){ cin>>n>>m>>k; vector<int> kk; for(int i=0; i<k; i++){ int a; cin>>a; kk.pb(a); mark[a] = true; } sort(kk.begin(), kk.end()); if(k>0){ int consec = 1; int max_consec = 1; for(int i=1; i<kk.size(); i++){ if(kk[i]-kk[i-1]!=1){ consec = 1; } else consec++; max_consec = max(max_consec, consec); } if(max_consec>=m){ cout<<-1<<endl; return 0; } } sum[n] = {0,0}; for(int i=n-1; i>=0; i--){ if(!mark[i]) dp[i] = (sum[i+1]-sum[i+m+1])/m + (Exp){0,1}; else dp[i] = {1,0}; sum[i] = sum[i+1]+dp[i]; } long double res = dp[0].b / (1-dp[0].a); cout<<fixed<<setprecision(4)<<res<<endl; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) v.begin(), v.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 (b<a) { a=b; return 1;} return 0;} int main() { int H, W, N, M; cin >> H >> W >> N >> M; vector<vector<int>> board(H, vector<int>(W, 0)); vector<vector<bool>> res(H, vector<bool>(W, false)); rep(i, N) { int a, b; cin >> a >> b; --a, --b; board[a][b] = 1; } rep(i, M) { int c, d; cin >> c >> d; --c; --d; board[c][d] = -1; } rep(y, H) { bool light = false; rep(x, W) { if (board[y][x] == 1) light = true; if (board[y][x] == -1) light = false; if (light) res[y][x] = true; } light = false; for (int x = W-1; x >= 0; --x) { if (board[y][x] == 1) light = true; if (board[y][x] == -1) light = false; if (light) res[y][x] = true; } } rep(x, W) { bool light = false; rep(y, H) { if (board[y][x] == 1) light = true; if (board[y][x] == -1) light = false; if (light) res[y][x] = true; } light = false; for (int y = H-1; y >= 0; --y) { if (board[y][x] == 1) light = true; if (board[y][x] == -1) light = false; if (light) res[y][x] = true; } } int sum = 0; rep(y, H) rep(x, W) if (res[y][x]) sum++; cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pi; //bool vis[1000]; int x[]={1,-1,0,0}; int y[]={0,0,1,-1}; //int f=0; //int t[500000]; //int ll[500000]; //"''; int main(){ int r,c,n,m; cin>>r>>c>>n>>m; int dp[r][c][4]; memset(dp,0,sizeof(dp)); vector<vector<int> > v(r,vector<int>(c,0)); queue<vector<int> > q; int ans=0; for(int i=0;i<n;i++){ int a,b; cin>>a>>b; a--; b--; v[a][b]=1; for(int j=0;j<4;j++){ q.push({a,b,j}); dp[a][b][j]=1; } } for(int i=0;i<m;i++){ int a,b; cin>>a>>b; a--; b--; v[a][b]=-1; } while(!q.empty()){ vector<int> u=q.front(); q.pop(); int na=u[0]+x[u[2]]; int nb=u[1]+y[u[2]]; int nd=u[2]; if(na<r&&na>=0&&nb<c&&nb>=0&&dp[na][nb][nd]==0&&v[na][nb]!=-1){ dp[na][nb][nd]=1; q.push({na,nb,nd}); } } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ int f=0; for(int k=0;k<4;k++){ f=max(f,dp[i][j][k]); } ans+=f; } } cout<<ans<<endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; vector<ll> a(N), b(N, 0); rep(i, N) { cin >> a[i]; b[a[i]]++; } b[0] = min(b[0], K); rep(i, N - 1) b[i + 1] = min(b[i], b[i + 1]); ll ans = 0; rep(i, N - 1) { ans += (b[i] - b[i + 1]) * (i + 1); } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; //TEMPLATE #define len(a) (ll) a.size() #define ms(a, x) memset(a, x, sizeof a) #define bitcount(n) __builtin_popcountll(n) #define FR ios_base::sync_with_stdio(false);cin.tie(NULL) #define cin1(a) cin >> a #define cin2(a, b) cin >> a >> b #define cin3(a, b, c) cin >> a >> b >> c #define cin4(a, b, c, d) cin >> a >> b >> c >> d #define cots(a) cout << a << " " #define cot1(a) cout << a << "\n" #define cot2(a, b) cout << a << " " << b << "\n" #define cot3(a, b, c) cout << a << " " << b << " " << c << "\n" #define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n" #define cotcase(cs) cout << "Case " << cs << ": " #define yes cout << "Yes\n" #define no cout << "No\n" #define line cout << "\n" #define reversed(s) reverse(s.begin(), s.end()) #define asort(s) sort(s.begin(), s.end()) #define dsort(s) sort(s.rbegin(), s.rend()) #define all(s) s.begin(), s.end() #define uniq(s) s.resize(distance(s.begin(),unique(s.begin(), s.end()))) #define found(s, x) (s.find(x) != s.end()) #define for0(i, n) for (i = 0; i < n; i++) #define for1(i, n) for (i = 1; i <= n; i++) #define fora(i, a, b) for (i = a; i <= b; i++) #define forb(i, b, a) for (i = b; i >= a; i--) #define ll long long #define pb push_back #define mp make_pair #define ld long double #define pii pair <ll, ll> #define piii pair <ll, pii> #define F first #define S second #define pi acos(-1.0) #define bug(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << '\n' void err(istringstream *iss) {} template<typename T, typename... Args> void err(istringstream *iss, const T a, const Args... args) { string _name; *iss >> _name; if (_name.back()==',') _name.pop_back(); cerr << _name << " = " << fixed << setprecision(5) << a << "; ", err(iss, args...); } const ll INF = LLONG_MAX; const ll SZ = 4e5+5; const ll mod = 1e9+7; string s, s1, s2; ll n, k, ans = 0; ll a[SZ]; ll f[SZ]; int main() { FR; ll cs = 0, tc = 1, x, y, z, i, j, g, p, q, sum = 0, c = 0, t = 0; // ll a, b, d; // string s, s1, s2; cin2(n, k); for1(i, n) cin1(a[i]), f[a[i]]++; fora(i, 0, n) { c = max(0LL, k-f[i]); c = min(c, k); ans += (c * i); k -= c; } cot1(ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define For(i,a,b) for(int i=(a),(i##i)=(b);i<=(i##i);++i) #define Rep(i,n) for(int i=0,(i##i)=(n);i<(i##i);++i) #define Fodn(i,a,b) for(int i=(a),(i##i)=(b);i>=(i##i);--i) #define pln puts("") #define il inline #define ff first #define ss second using LL=long long;using u32=unsigned int;using u64=unsigned LL;using LD=long double; template<typename T>using pii=pair<T,T>; template<typename T>il bool read(T&x){ x=0;char c=getchar();int f=1;while(!isdigit(c)&&(c!='-')&&(c!=EOF))c=getchar(); if(c==EOF)return 0;if(c=='-')f=-1,c=getchar(); while(isdigit(c)){x=(x<<1)+(x<<3)+(c&15);c=getchar();}x*=f;return 1;} template<typename T,typename...Args> il bool read(T&x,Args&...args){bool res=1;res&=read(x);res&=read(args...);return res;} const int M=1000000007,INF=0x3f3f3f3f;const LL INFLL=0x3f3f3f3f3f3f3f3fLL; const int N=10010;const double EPS=1e-6; LL n,f[N][16]; inline void init(){ read(n); For(i,1,n)read(f[i][0]); int t=log(n)/log(2)+1; For(i,1,t-1)For(j,1,(n-(1<<i)+1))f[j][i]=min(f[j][i-1],f[j+(1<<(i-1))][i-1]); } il LL find(int l,int r){ int k=log(r-l+1)/log(2); return min(f[l][k],f[r-(1<<k)+1][k]); } LL ans=0; inline void solve(){ For(i,1,n)For(j,i,n)ans=max(ans,(j-i+1)*find(i,j)); printf("%lld\n",ans); } signed main(){ init();solve(); return 0; }
#include<bits/stdc++.h> #define l long #define ll long long #define ld long double #define MOD 1000000007 #define MAX_N 1000006 #define f first #define s second #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; const ll inf=1e18; ll gcd(ll a,ll b){ if(a==0){ return b; } if(b==0){ return a; } return gcd(b,a%b); } int main(){ // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll n,lcm=1; cin>>n; for(ll i=2;i<=n;i++){ lcm=lcm*(i/gcd(i,lcm)); } lcm++; cout<<lcm<<"\n"; return 0; }
#include "iostream" #include "climits" #include "list" #include "queue" #include "stack" #include "set" #include "functional" #include "algorithm" #include "string" #include "map" #include "unordered_map" #include "unordered_set" #include "iomanip" #include "cmath" #include "random" #include "bitset" #include "cstdio" #include "numeric" #include "cassert" #include "ctime" using namespace std; //constexpr long long int MOD = 1000000007; //constexpr int MOD = 1000000007; //constexpr int MOD = 998244353; constexpr long long int MOD = 998244353; constexpr double EPS = 1e-8; //int N, M, K, T, H, W, L, R; long long int N, M, K, T, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); long long int r, x, y; cin >> r >> x >> y; if (x * x + y * y == r * r) { cout << 1 << endl; return 0; } for (long long int i = 2; i <= 200000; i++) { if (r * r * i * i >= x * x + y * y) { cout << i << endl; return 0; } } }
//@formatter:off #include<bits/stdc++.h> #define overload4(_1,_2,_3,_4,name,...) name #define rep1(i,n) for (ll i = 0; i < ll(n); ++i) #define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i) #define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d) #define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__) #define rrep1(i,n) for (ll i = ll(n)-1; i >= 0; i--) #define rrep2(i,n,t) for (ll i = ll(n)-1; i >= (ll)t; i--) #define rrep3(i,n,t,d) for (ll i = ll(n)-1; i >= (ll)t; i-=d) #define rrep(...) overload4(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define popcount(x) __builtin_popcountll(x) #define pb push_back #define eb emplace_back #ifdef __LOCAL #define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); cout << flush; } #else #define debug(...) void(0) #endif #define INT(...) int __VA_ARGS__;scan(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__) #define STR(...) string __VA_ARGS__;scan(__VA_ARGS__) #define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__) #define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__) #define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__) using namespace std; using ll = long long; using ld = long double; using P = pair<int,int>; using LP = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vector<int>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vd = vector<double>; using vvd = vector<vector<double>>; using vs = vector<string>; using vc = vector<char>; using vvc = vector<vector<char>>; using vb = vector<bool>; using vvb = vector<vector<bool>>; using vp = vector<P>; using vvp = vector<vector<P>>; template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; } template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; } template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; } template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; } template<class T> void vecout(const vector<T> &v,char div='\n') { rep(i,v.size()) cout<<v[i]<<(i==int(v.size()-1)?'\n':div);} template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;} template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;} void scan(){} template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); } template<class T> void print(const T& t){ cout << t << '\n'; } template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); } template<class... T> void fin(const T&... a) { print(a...); exit(0); } struct Init_io { Init_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << boolalpha << fixed << setprecision(15); } } init_io; const string yes[] = {"no","yes"}; const string Yes[] = {"No","Yes"}; const string YES[] = {"NO","YES"}; const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on int main() { LL(r, x, y); ll l = x * x + y * y; if (l < r * r) fin(2); r *= r; ll ok = inf, ng = 0; auto f = [&](ll x) -> bool { if (x * x > linf / r) return true; return l <= x * x * r; }; while (abs(ok - ng) > 1) { ll mid = (ng + ok) / 2; if (f(mid)) ok = mid; else ng = mid; } fin(ok); }
#include <bits/stdc++.h> using namespace std; int main(){ int n; string s; char a, b; cin >> n >> s; a = s[0]; b = s[s.length()-1]; bool ok = false; for(int i=1; i<s.length(); i++){ if(s[i]!=a && s[i+1]!=b) ok= true; } if(a!=b) cout << "1"; else if(ok) cout << "2"; else cout << "-1"; return 0; }
#include <bits/stdc++.h> #include <climits> using namespace std; template<class T> using V = vector<T>; template<class T> using VV = V<V<T>>; using ld = long double; #define ll long long using ull = unsigned ll; using PLL = pair<ll, ll>; using VLL = V<ll>; using VB = V<bool>; using VVB = VV<bool>; using VVLL = VV<ll>; using Gr = VVLL; using MLL = map<ll, ll>; #define UMLL unordered_map<ll, ll, custom_hash> #define fast ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); #define R & #define CR const R #define FORI(i, a, b) for(ll i = a, max##i = b; i < max##i; ++i) #define FOR(i, n) FORI(i, 0, n) #define RFORI(i, a, b) for(ll i = a, min##i = b; i >= min##i; --i) #define RFOR(i, n) RFORI(i, n, 0) #define FORA(i, a) for(auto i : a) #define FORAR(i, a) for(auto R i : a) #define FORACR(i, a) for(auto CR i : a) #define ALL(obj) begin(obj), end(obj) #define Count(q) while(q--) #define OK cerr << "OK\n"; #define mp make_pair #define pb push_back //#define DEBUG template<class T> T sqr(T x) { return x * x; } void YES(bool g, ostream R os, bool upper = true) { if(g) if(upper) os << "YES"; else os << "Yes"; else if(upper) os << "NO"; else os << "No"; os << "\n"; } template<class T> void show(T CR t, ostream R os = cerr) { FORACR(i, t) os << i << " "; os << "\n"; } template<class T> void show2d(T CR t, ostream R os = cerr) { FORACR(i, t) show(i, os); os << "\n"; } constexpr ll MOD = 1e9 + 7; //998244353; constexpr ll len = 'z' - 'a' + 1; constexpr ll INF = 1e15, MINF = -INF; constexpr ld PI = atanl(1.0L) * 4; constexpr ld eps = 1e-9, EPS = 1e-9; 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); } }; //arr void init() {} void solve(istream R is, ostream R os) { ll n; is >> n; string s; is >> s; char t = s[0]; bool g = 1; FORA(i, s) g &= i == t; if(g) { os << "-1\n"; return; } if(s.front() != s.back()) { os << "1\n"; return; } g = 0; FOR(i, n - 1) if(t != s[i] && t != s[i + 1]) { g = 1; } if(g) os << "2\n"; else os << "-1\n"; } void tester(istream R is, ostream R os) { auto seed = time(nullptr); //cerr << "seed: " << seed << "\n"; srand(seed); fast init(); ll q = 1; //is >> q; os << setprecision(999); Count(q) solve(is, os); } int main() { //ifstream in("input.txt"); //ofstream out("output.txt"); tester(cin, cout); }
#include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; //required using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define debug(x) cout << #x << " is " << x << endl #define yes cout << "YES" << endl #define no cout << "NO" << endl typedef long long ll; const int MOD = 1e9 + 7; int main(){ fast; vector<ll> prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; ll a, b; cin >> a >> b; vector<ll> dp(1048578, 0), prev(1048578, 0); dp[0] = 1; vector<int> state; int tmp; for (ll i = a; i <= b; ++i){ tmp = 0; for (int j = 0; j < 20; ++j){ if (i % prime[j] == 0){ tmp |= (1 << j); } } // debug(tmp); state.push_back(tmp); } for (ll s: state){ for (int i = 0; i < 1048578; ++i){ prev[i] = dp[i]; } for (int i = 0; i < 1048578; ++i){ dp[i] = prev[i]; if ((i & s) == s){ dp[i] += dp[i^s]; } } } ll res = 0; for (int i = 0; i < 1048578; ++i){ res += dp[i]; } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using lli = int64_t; using ld = long double; lli g = 10000; lli dist(lli x1, lli y1, lli x2, lli y2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } lli bsearch(lli x, lli y, lli r, lli y1, lli l, lli h, bool fl) { lli ans = LLONG_MAX; while (l <= h) { lli mid = l + (h - l) / 2; mid -= mid % g; if (dist(x, y, mid, y1) <= r * r) { ans = mid; (fl ? l : h) = mid + (fl ? g : -g); } else (fl ? h : l) = mid + (fl ? -g : g); } return ans; } lli calc(lli x, lli y, lli r, lli y1) { lli rx = x; rx -= rx % g; lli mid = LLONG_MAX; if (dist(x, y, rx, y1) <= r * r) mid = rx; else if (dist(x, y, rx + g, y1) <= r * r) mid = rx + g; if (mid == LLONG_MAX) return 0; lli rl = x - r; lli rr = x + r; rl -= rl % g; rr -= rr % g; rr += g; lli l = bsearch(x, y, r, y1, rl, mid, false); lli h = bsearch(x, y, r, y1, mid, rr, true); return (h - l) / g + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ld xd, yd, rd; cin >> xd >> yd >> rd; lli x = llroundl(xd * ld(g)); lli y = llroundl(yd * ld(g)); lli r = llroundl(rd * ld(g)); lli ans = 0; for (lli y1 = y - y % g; y - y1 <= r; y1 -= g) ans += calc(x, y, r, y1); for (lli y1 = y - (y % g) + g; y1 - y <= r; y1 += g) ans += calc(x, y, r, y1); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back void solve(){ int n; string a,b,c; cin>>n; cin>>a>>b>>c; for(int i=1;i<=n;i++) cout<<0; for(int i=1;i<=n;i++) cout<<1; cout<<0<<"\n"; } int main(){ int t=1; cin>>t; for(int tc=1; tc<=t; tc++){ solve(); } return 0; }
#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); ll cat(string s) { if (s[0]=='0' && s[s.length()-1]=='0') return 0; if (s[0]=='0' && s[s.length()-1]=='1') return 1; if (s[0]=='1' && s[s.length()-1]=='0') return 2; else return 3; } void solve() { ll n; string a, b, c; cin >> n >> a >> b >> c; if (cat(a)!=3 && cat(b)!=3 && cat(c)!=3) { string ans = ""; REP(i,n) ans+='1'; ans+='0'; REP(i,n) ans+='1'; cout << ans << endl; return; } if (cat(a)!=0 && cat(b)!=0 && cat(c)!=0) { string ans = ""; REP(i,n) ans+='0'; ans+='1'; REP(i,n) ans+='0'; cout << ans << endl; return; } else { string ans =""; REP(i,n) ans+='0'; REP(i,n) ans+='1'; ans+='0'; cout << ans << endl; return; } } int main(){ ll t; cin >> t; REP(i,t) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define ub upper_bound #define lb lower_bound #define endl "\n" #define mod 1000000007 #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n - 1; i >= 0; i--) void solve() { int n; cin >> n; vector<vector<ll>> ncr(205, vector<ll>(15)); ncr[0][0] = 1; for (int i = 1; i <= 200; i++) { ncr[i][0] = 1; for (int j = 1; j <= min(12, i); j++) { ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1]; } } cout << ncr[n - 1][11]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <stdio.h> int N, Ans; int d[1500][2]; int main() { int i, u, Max; scanf("%d", &N); for (i = 0; i < N; i++) scanf("%d%d", &d[i][0], &d[i][1]); Ans = d[0][0] + d[0][1]; for (i = 0; i < N; i++) Ans = ((Ans < d[i][0] + d[i][1]) ? Ans : d[i][0] + d[i][1]); for (i = 0; i < N; i++) for (u = 0; u < N; u++) if (i - u) Max = ((d[i][0] > d[u][1]) ? d[i][0] : d[u][1]), Ans = ((Ans < Max) ? Ans : Max); printf("%d", Ans); return 0; }
#include<bits/stdc++.h> #define N 505 #define ll long long #define inf 0x3f3f3f3f using namespace std; bool mmp1; int n,m,K; namespace P100 { int C[N][N]; int A[N],B[N]; int D[N][N]; void solve() { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&C[i][j]); for(int i=1;i<=n;i++) B[i]=C[1][i]; for(int i=1;i<=n;i++) A[i]=C[i][1]-B[1]; int ma=A[1],mb=B[1]; for(int i=2;i<=n;i++)ma=min(ma,A[i]); for(int i=2;i<=n;i++)mb=min(mb,B[i]); if(ma+mb<0){ puts("No"); return; }else if(ma<0)for(int i=1;i<=n;i++)A[i]-=ma,B[i]+=ma; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ // printf("D[%d][%d]=%d\n",i,j,A[i]+B[j]); if(C[i][j]!=A[i]+B[j]){ puts("No"); return; } } puts("Yes"); for(int i=1;i<=n;i++) printf("%d ",A[i]); puts(""); for(int i=1;i<=n;i++) printf("%d ",B[i]); } } bool mmp2; int main() { // srand(time(0)); // printf("%.6fMB\n",(&mmp2-&mmp1)/1024.0/1024); // freopen(".in","r",stdin); // freopen(".out","w",stdout); P100::solve(); return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll=long long; using ld=long double; //using mint = modint1000000007; #define rep(i,n) for (ll i=0; i<n; ++i) #define all(c) begin(c),end(c) #define PI acos(-1) #define oo 2e18 template<typename T1, typename T2> bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} template<typename T1, typename T2> bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} //priority_queue<ll, vector<ll>, greater<ll>> Q; /* 250000 2ぶたん? 最初のAさえ決まればあとは全部? */ ll C[550][550]; int main(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); ll N; cin >> N; ll low = 1e9; ll high = 0; rep(i, N) rep(j, N)cin >> C[i][j]; rep(i, N) rep(j, N){ chmin(low, C[i][j]); chmax(high, C[i][j]); } // lowより小さい数だけが候補 // さが同じかどうか ll dif[550] = {}; rep(i, N-1){ dif[i] = C[i+1][0] - C[i][0]; rep(j, N){ if (C[i+1][j] - C[i][j] != dif[i]){ cout << "No" << endl; return 0; } } } ll bmin=1e9; // Bは一番小さい数がある行 ll B[550]={}; rep(i, N) chmin(bmin, C[i][0]); rep(i, N){ if (C[i][0] == bmin) { rep(j, N) B[j] = C[i][j]; break; } } ll A[550] = {}; rep(i, N){ A[i] = C[i][0] - bmin; } cout << "Yes" << endl; rep(i, N){ cout << A[i]; if (i==N-1) cout << endl; else cout << " "; } rep(i, N){ cout << B[i]; if (i==N-1) cout << endl; else cout << " "; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; ll gcd(ll x, ll y) { if (y == 0) return x; return gcd(y, x % y); } int main() { int prime[20] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; int N; cin >> N; vector<int> X(N); rep(i, N) cin >> X[i]; ll ans = 1e18; rep(i, 1 << 15) { ll a = 1; rep(j, 15) { if ((i >> j) & 1) a *= prime[j]; } bool f = true; rep(j, N) { if (gcd(a, X[j]) == 1) { f = false; break; } } if (f) ans = min(ans, a); } cout << ans << "\n"; }
#include <iostream> #include<vector> #include<algorithm> #include<math.h> #include<map> #define f first #define s second using namespace std; vector<pair<int,int> > e(60); vector<int> num; bool cmp(pair<int,int> a,pair<int,int> b){ if(a.first==b.first) return a.second<b.second; return a.first>b.first; } int main() { int n,x; cin>>n; for(int i=0;i<n;i++){ cin>>x; for(int j=2;j<=x;j++){ if(x%j==0){ e[j].first++; e[j].second=j; } } num.push_back(x); } long long now=n,ans=1; while(now>0){ sort(e.begin(),e.end(),cmp); long long tmp=e[0].second; //cout<<"tmp="<<tmp<<endl; for(int i=0;i<n;i++){ if(num[i]%tmp==0){ //cout<<"who="<<i<<endl; num[i]=1; now--; } } for(int i=0;i<60;i++){ e[i].first=0; e[i].second=0; } ans*=tmp; for(int i=0;i<n;i++){ for(int j=2;j<=num[i];j++){ if(num[i]%j==0){ e[j].first++; e[j].second=j; } } } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; //#define MULTITEST #define x first #define y second #define mp make_pair #define pb push_back #define sqr(a) ((a) * (a)) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define forn(i, n) for(int i = 0; i < int(n); i++) #define fore(i, l, r) for(int i = int(l); i < int(r); i++) typedef long long li; typedef long double ld; typedef pair<int, int> pt; template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) { return out << "(" << a.x << ", " << a.y << ")"; } template <class A> ostream& operator << (ostream& out, const vector<A> &v) { out << "["; forn(i, sz(v)) { if(i) out << ", "; out << v[i]; } return out << "]"; } mt19937 rnd(time(NULL)); const int INF = int(1e8); const li INF64 = li(1e18); const int MOD = int(1e9) + 7; const ld EPS = 1e-9; const ld PI = acos(-1.0); int n, m; bool read () { if (scanf("%d%d", &n, &m) != 2) return false; return true; } void solve() { if (m == 0){ forn(i, n) printf("%d %d\n", 2 * i + 1, 2 * i + 2); return; } if (m < 0 || m >= n - 1){ puts("-1"); return; } vector<pt> ans; ans.pb(mp(0, 2 * m + 2)); forn(i, m) ans.pb(mp(2 * i + 1, 2 * i + 2)); ans.pb(mp(2 * m + 1, 2 * m + 3)); int lst = 2 * m + 3; while (sz(ans) < n){ ans.pb(mp(lst + 1, lst + 2)); lst += 2; } for (auto it : ans) printf("%d %d\n", it.x + 1, it.y + 1); } int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int tt = clock(); #endif cerr.precision(15); cout.precision(15); cerr << fixed; cout << fixed; #ifdef MULTITEST int tc; scanf("%d", &tc); while(tc--){ read(); #else while(read()) { #endif solve(); #ifdef _DEBUG cerr << "TIME = " << clock() - tt << endl; tt = clock(); #endif } }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll solve() { ll N, M; cin >> N >> M; vector<ll> L(N), R(N); if ( N == 1 ) { if ( M != 0 ) return -1; cout << 1 << " " << 2 << "\n"; return 0; } if ( M > N-2 ) return -1; if ( M < 0 ) return -1; for ( int i = 1; i < N; i++ ) { L[i] = 4*i+1; R[i] = 4*i+3; } L[0] = 2; R[0] = 4*(M+1)+2; for ( int i = 0; i < N; i++ ) { cout << L[i] << " " << R[i] << "\n"; } return 0; } int main() { auto ans = solve(); if ( ans < 0 ) { cout << ans << "\n"; } return 0; }