code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
//Problem link : //done by sanath kumar #include<bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define mod 1000000007 #define inf 1e9 #define endl "\n" #define pb push_back #define vi vector<ll> #define vs vector<string> #define pii pair<ll,ll> #define ump unordered_map #define mp make_pair #define pq_max priority_queue<ll> #define pq_min priority_queue<ll,vector<ll>,greater<ll>> #define all(v) v.begin(),v.end() #define ff first #define ss second #define mid(l,r) (l+(r-l))/2 #define bitc(x) __builtin_popcount(x) #define loop(i,a,b) for(int i=(a);i<=(b);i++) #define looprev(i,a,b) for(int i=(a);i>=(b);i--) #define iter(c,it) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++) #define log(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);} #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl; template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);} template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));} void err(istream_iterator<string> it){} template<typename T,typename... Args> void err(istream_iterator<string> it,T a,Args... args){ cout<<*it<<" = "<<a<<endl; err(++it,args...); } void file_i_o(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } int main(){ file_i_o(); clock_t begin=clock(); string S; cin>>S; int n=S.length(); loop(i,0,n-1){ if(S[i]=='9') S[i]='6'; else if(S[i]=='6') S[i]='9'; } looprev(i,n-1,0){ cout<<S[i]; } #ifndef ONLINE_JUDGE clock_t end=clock(); cout<<"\n\n\nExecuted in :"<<double(end-begin)/CLOCKS_PER_SEC*1000<<" ms"<<endl; #endif }
#include<stdio.h> #include<string.h> char ch[101000]; int main(){ scanf("%s",ch+1); // scanf("%d%d%d",&a,&b,&c); // printf("%d",21-a-b-c); int len = strlen(ch+1); for(int i = len;i>=1;i--){ if(ch[i]=='0'||ch[i]=='1'||ch[i]=='8'){ printf("%c",ch[i]); } else { if(ch[i]=='6'){ printf("9"); } else { printf("6"); } } } return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repab(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define repr(i, n) for (ll i = (ll)(n); i >= 0; i--) #define vsort(v) sort(v.begin(), v.end()) #define vsortr(v) sort(v.begin(), v.end(), greater<ll>{}) //定数 #define INF32 2147483647 // 2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 // 9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //問題による int main() { ll n, s, d; cin >> n >> s >> d; vector<ll> x(n), y(n); rep(i, n) cin >> x[i] >> y[i]; string ans = "No"; rep(i, n) { if (x[i] >= s) continue; if (y[i] <= d) continue; ans = "Yes"; break; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int A,B,W; cin >> A >> B >> W; W*=1000; int mi=1e9; int ma=-1; for(int i=1;i<=1e6;i++){ if(A*i>W){ break; }else if(B*i<W){ continue; }else{ mi=min(mi,i); ma=max(ma,i); } } if(ma==-1){ cout << "UNSATISFIABLE" << endl; }else{ cout << mi << " " << ma << endl; } return 0; }
/* -*- coding: utf-8 -*- * * f.cc: F - Graph Smoothing */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 100; const int MOD = 1000000007; /* typedef */ typedef long long ll; struct MI { int v; MI(): v() {} MI(int _v): v(_v) {} MI operator+(const MI m) const { return MI((v + m.v) % MOD); } MI operator-(const MI m) const { return MI((v + MOD - m.v) % MOD); } MI operator*(const MI m) const { return MI((ll)v * m.v % MOD); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } MI pow(int n) { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() { return pow(MOD - 2); } }; typedef MI vec[MAX_N]; typedef vec mat[MAX_N]; /* global variables */ vec va, vb; mat ma, mb; /* subroutines */ inline void initmat(const int n, mat a) { memset(a, 0, sizeof(mat)); } inline void unitmat(const int n, mat a) { initmat(n, a); for (int i = 0; i < n; i++) a[i][i] = 1; } inline void copymat(const int n, const mat a, mat b) { memcpy(b, a, sizeof(mat)); } inline void mulmat(const int n, const mat a, const mat b, mat c) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { c[i][j] = 0; for (int k = 0; k < n; k++) c[i][j] += a[i][k] * b[k][j]; } } inline void powmat(const int n, const mat a, int b, mat c) { mat s, t; copymat(n, a, s); unitmat(n, c); while (b > 0) { if (b & 1) { mulmat(n, c, s, t); copymat(n, t, c); } mulmat(n, s, s, t); copymat(n, t, s); b >>= 1; } } inline void mulmatvec(const int n, const mat a, const vec b, vec c) { for (int i = 0; i < n; i++) { c[i] = 0; for (int j = 0; j < n; j++) c[i] += a[i][j] * b[j]; } } /* main */ int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%d", &(va[i].v)); unitmat(n, ma); MI inv2m = (MI(2) * m).inv(); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); x--, y--; ma[x][x] -= inv2m, ma[x][y] += inv2m; ma[y][y] -= inv2m, ma[y][x] += inv2m; } powmat(n, ma, k, mb); mulmatvec(n, mb, va, vb); for (int i = 0; i < n; i++) printf("%d\n", vb[i].v); return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #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 ll tmp[600001], *A = tmp + 200000, *B = tmp + 400000; void pakuri_sort(int N, ll 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]; } } const int CM = 400001, 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 main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); rep(i, N) { A[i] = ll(i) << 32 | getint(); B[i] = ll(i) << 32 | getint(); } pakuri_sort(N, A); pakuri_sort(N, B); const ll ma = (1ll << 32) - 1; rep(i, N) { auto tmp = (A[i] & ma) < (B[N - 1 - i] & ma); cn[A[i] >> 31] = tmp; cn[B[N - 1 - i] >> 31 | 1] = tmp; } int c = -1, pn = 0; rep(i, N << 1) { if (c != -1 && pn == cn[i]) { cn[c] = '('; cn[i] = ')'; c = tmp[c]; pn ^= 1; } else { pn = cn[i]; tmp[i] = c; c = i; } } fwrite(cn, 1, N << 1, stdout); Would you please return 0; }
#include <iostream> #include <vector> #include <map> using namespace std; class UnionFind { public: UnionFind(int size) : r(size), p(size) // 0 origin { for (int i = 0; i < size; i++) p[i] = i; } int unite(int x, int y) { x = find(x), y = find(y); if (r[x] > r[y]) swap(x, y); else if (r[x] == r[y]) r[y]++; return p[x] = y; // return root } int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } bool same(int x, int y) { return find(x) == find(y); } int rank(int x) const { return r[x]; } int parent(int x) const { return p[x]; } private: UnionFind(); vector<int> r, p; }; int main() { int n, q; cin >> n >> q; vector<map<int, int>> cnt(n); for (int i = 0; i < n; i++) { int c; scanf("%d", &c); cnt[i][c - 1] = 1; } UnionFind uf(n); while (q--) { int com, a, b; scanf("%d%d%d", &com, &a, &b); a--, b--; if (com == 1) { a = uf.find(a); b = uf.find(b); if (a == b) continue; int root = uf.unite(a, b); if (root != a) swap(a, b); for (auto [c, v] : cnt[b]) cnt[a][c] += v; } else printf("%d\n", cnt[uf.find(a)][b]); } return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pll> vpl; double EPS = 1e-9; int INF = 1000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1.0); #define sqr(x) ((ll)(x) * (x)) #define reset(a,b) memset(a, b, sizeof(a)) #define ff first #define int long long #define ss second #define mp make_pair #define pb push_back #define all(v) v.begin(), v.end() #define len(v) (ll)v.size() #define srt(v) sort( v.begin(), v.end()) #define rsrt(v) sort( v.rbegin(), v.rend()) #define rvrs(v) reverse( v.begin(), v.end()) #define PERMUTE next_permutation #define tc(t) while (t--) #define rep(i,x,n) for(int i=x;i<n;i++) #define ses cout<<"\n" #define input(x,n) for(int i=0;i<n;i++) cin>>x[i] #define output(x,n) for(int i=0;i<n;i++) cout<<x[i]<<" " bool odd(ll n){if(n&1)return true;return false;} //ll xor(ll a,ll b)return a^b; ll power(ll x, ll y,ll M){ll res=1;while(y>0){ if(y&1)res=(res*x)%M;y=(y>>1)%M;x=(x*x)%M;}return res;} constexpr int N = 2e5+5, P = 1000000007; struct cmp {bool operator() (const pair<int, int> &a, const pair<int, int> &b) const { int lena = a.second -a.first + 1;int lenb = b.second-b.first + 1;if (lena == lenb) return a.first < b.first;return lena > lenb; } }; bool sortby(const pair<int,int>&a,const pair<int,int>&b) { return (a.first<b.first); } int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } int mod=1e9+7; void code() { int x,y; cin>>x>>y; if(x==y) cout<<x; else{ for(int i=0;i<=2;i++) { if(i!=x&&i!=y) { cout<<i;return; } } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t=1; //cin>>t; tc(t) { code();ses; } }
//int a = s - '0'; 文字から数字 //小文字から大文字 //transform(a.begin(), a.end(), a.begin(), ::toupper); //map 全探索 //auto begin = p.begin(), end = p.end(); //for (auto it = begin; it != end; it++) {} //mapのキー:it->first mapのバリュー:it->second //大文字判定 isupper(文字) 小文字判定 islower(文字) //do{}while(next_permutation(ALL(配列))) //小文字に対応する文字コード:S[i] - 'a' //文字コード→小文字:(char)(数字+'a') //グラフの距離:隣接行列で扱う //bool型 初期値はTrue //島渡りの問題:中間ノードに着目 //数が大きい時の比較はstring型で行う //全て0になったか調べたい->0になるたびにcntする //例外処理は最初にする //x = p^m + q^n...の約数の個数:(n+1)*(m+1).... //N!のどの素因数で何回割れるか //⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和 //パズルの問題->一般化して全探索 //stack<ll> s; //s.push(要素);s.top();s.pop(); //queue<ll> q; //q.push(要素);q.front();q.pop(); //同じ作業繰り返す系の問題:収束先を見つける //過半数:N/2.0で判定 //優先度付きキュー //priority_queue< //ll, //vector<ll> //> q; #include <bits/stdc++.h> #define rep(i,N) for(int i = 0; i < N;i++) #define ALL(a) (a).begin(),(a).end() #define ll long long int #define PI 3.14159265358979323846264338327950L using namespace std; //割るやつ const ll MOD = (pow(10, 9) + 7); // K進数でのNの桁数 ll dig(ll N, ll K) { ll dig = 0; while (N) { dig++; N /= K; } return dig; } // a,bの最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); } //a,bの最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } //階乗計算 ll f(ll n) { if (n == 0 || n == 1) return 1; else return (n * f(n - 1)); } //Nのd桁目の数 ll dignum(ll N, ll d) { ll x = pow(10, d); N %= x; ll y = pow(10, d - 1); N /= y; return N; } //Nをdで何回割れるか ll divcnt(ll N, ll d) { ll ans = 0; while (1) { if (N%d == 0) { ans++; N /= d; } else break; } return ans; } //素数判定 bool prime(ll num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } //フィボナッチ数列 vector<ll> memo(pow(10, 6) + 1); ll fibo(ll n) { if (n == 1) return 1; else if (n == 2) return 1; else if (memo[n] != 0) return memo[n]; else return memo[n] = fibo(n - 1) + f(n - 2); } ll RS(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } return N * RS(N, P - 1, M); } vector<int> IntegerToVector(int bit, int N) { vector<int> S; for (int i = 0; i < N; ++i) { if (bit & (1 << i)) { S.push_back(i); } } return S; } int main() { double a, b, c, d; cin >> a >> b >> c >> d; long double e = (b + d)/(a - c); long double f = b - a * e; cout << setprecision(20) << -f/e << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<double,double> ; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rep2(i,s1,n) for (ll i=(s1);i<(ll)(n);i++) #define rrep(i,a,b) for (ll i=(a);i>=(ll)(b);i--) #define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(x) (x).begin(),(x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end()) #define pb push_back const ll inf=1LL<<60; ll min(ll a,ll b) {return a>=b ? b:a;} ll max(ll a,ll b) {return a>=b ? a:b;} template<typename T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;} template<typename T>bool chmin(T &a, const T &b) {if (b<a) {a=b; return 1;} return 0;} const ll mod=1000000007LL; //const ll mod=998244353LL; ll b[10000]; ll dp[3009][3009]; int main(){ ll n; cin >> n; ll a[n]; rep(i,n) cin >> a[i]; rep(i,n) b[i+1]=b[i]+a[i]; dp[1][0]=1; ll ans=0; rep2(i,1,n+1){ rrep(j,n,1){ dp[j+1][b[i]%(j+1)]+=dp[j][b[i]%j]; dp[j+1][b[i]%(j+1)]%=mod; if (i==n){ ans+=dp[j][b[i]%j]; ans%=mod; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF=1001001001; const int mod = 1000000007; int main() { int N; cin>>N; vector<int>p(N); for(int i=0;i<N;i++){ cin>>p[i]; } vector<bool>t(200005); int s=0; for(int i=0;i<N;i++){ t[p[i]]=true; for(int j=s;j<200005;j++){ if(!t[j]){cout<<j<<endl;s=j;break;} } } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <queue> #include <stack> #include <set> #include <map> #include <iomanip> #include <utility> #include <tuple> #include <functional> #include <bitset> #include <cassert> #include <complex> #include <stdio.h> #include <time.h> #include <numeric> #include <random> #include <unordered_map> #include <unordered_set> #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n' #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef complex<ld> com; template<class T> using prique = priority_queue<T, vector<T>, greater<T>>; constexpr int inf = 1000000010; constexpr ll INF = 1000000000000000010; constexpr int mod1e9 = 1000000007; constexpr int mod998 = 998244353; constexpr ld eps = 1e-12; constexpr ld pi = 3.141592653589793238; constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; }; int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 }; void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); } template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; } template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; } struct fastio { fastio() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(20); } }fastio_; int main() { int n; cin >> n; set<int> st; rep(i, n + 3) st.insert(i); rep(i, n) { int a; cin >> a; if (st.count(a)) st.erase(a); cout << *st.begin() << '\n'; } }
#include<bits/stdc++.h> using namespace std; using ll = int64_t; ll score(string s){ vector<ll> cnt(10); iota(cnt.begin(), cnt.end(), 0); for(char c : s) cnt[c-'0'] *= 10; return accumulate(cnt.begin(), cnt.end(), 0); } int main(){ ll k; string s,t; cin >> k >> s >> t; vector<ll> cnt(10,k); for(char c : s+t) cnt[c-'0']--; ll win = 0; for(ll x=1; x<=9; x++){ for(ll y=1; y<=9; y++){ s.back() = '0'+x; t.back() = '0'+y; if(score(s)<=score(t)) continue; win += cnt[x]*(cnt[y]-(x==y)); } } const ll rem = 9*k-8; cout << double(win)/rem/(rem-1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() using ll = long long; const ll mod = 1000000007; const double pi = 3.14159265358979; const ll INF = 1e18; int main() { ll k; cin >> k; ll cards = k * 9 - 8; string s, t; cin >> s >> t; map<char, ll> mps, mpt; rep(i, 4) { mps[s[i]]++; mpt[t[i]]++; } double ans = 0; for (int i = 1; i < 10; i++) { if (mps[i + '0'] + mpt[i + '0'] < k) { int sprob = k - mps[i + '0'] - mpt[i + '0']; mps[i + '0']++; ll spoint = 0; for (int j = 1; j < 10; j++) { spoint += j * pow(10, mps[j + '0']); } for (int j = 1; j < 10; j++) { if (mps[j + '0'] + mpt[j + '0'] < k) { int tprob = k - mps[j + '0'] - mpt[j + '0']; mpt[j + '0']++; ll tpoint = 0; for (int k = 1; k < 10; k++) { tpoint += k * pow(10, mpt[k + '0']); } if (spoint > tpoint) { ans += (double)sprob * tprob / (cards * (cards - 1)); } mpt[j + '0']--; } } mps[i + '0']--; } } printf("%.16lf\n", ans); 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; int main() { ll n; string s; cin >> n >> s; string t = ""; for(auto c : s) { t += c; if(t.size() >= 3 && t.substr(t.size() - 3, 3) == "fox") { rep(i, 3) { t.pop_back(); } } } out(t.size()); re0; }
#include <bits/stdc++.h> #define INF 1000000007 #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++) using namespace std; typedef long long ll; typedef long long int lli; typedef long double ld; typedef vector<int> vi; typedef unordered_map<int,int> umi ; typedef unordered_set<int> usi ; typedef pair<int,int> pi; #include <bits/stdc++.h> using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif //declare here ------------------------------------------------------------------------------- const int N =4e5+1; bool taketest=false ; vi adj[N] ; bool visited[N] ; bool visited2[N] ; bool cycle(int u ,int p){ if(visited2[u]) return true ; visited2[u] =true ; bool ans=false ; for(auto v:adj[u]){ if(v==p) continue ; ans|= cycle(v,u) ; } return ans ; } int sze(int u ,int p){ if(visited[u]) return 0 ; visited[u] =true ; int res=0 ; res++ ; for(auto v:adj[u]){ if(v==p) continue ; res+= sze(v,u) ; } return res ; } // solve here --------------------------------------------------------------------------------- void solve(){ int n ; cin>>n ; int u,v ; REP(i,0,n){ cin>>u>>v ; adj[u].PB(v) ; adj[v].PB(u) ; } int ans=0 ; REP(i,0,N){ if(visited[i]) continue ; ans+=sze(i,-1) ; if(!cycle(i,-1)) ans-- ; } cout<<ans ; } //-------------------------------------------------------------------------------------------- int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); long long test =1 ; if(taketest) cin>>test ; while(test--){ solve() ; cout<<"\n" ; } return 0 ; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll mod = 1000000007; typedef unsigned int uint; int main() { ll R, C; cin >> R >> C; vector<vector<ll>> a(R, vector<ll>(C-1)); for (int i = 0; i < R; ++i) { for (int j = 0; j < C - 1; ++j) { cin >> a[i][j]; } } vector<vector<ll>> b(R-1, vector<ll>(C)); for (int i = 0; i < R - 1; ++i) { for (int j = 0; j < C; ++j) { cin >> b[i][j]; } } priority_queue<tuple<ll, int, int, int>, vector<tuple<ll, int, int, int>>, greater<>> pq; pq.push(make_tuple(0, 0, 0, 0)); vector<vector<vector<ll>>> dist(2, vector<vector<ll>>(R, vector<ll>(C, -1))); while (!pq.empty()) { auto [cost, f, r, c] = pq.top(); pq.pop(); if (dist[f][r][c] != -1) continue; dist[f][r][c] = cost; if (f == 0 && r == R-1 && c == C-1) break; if (f == 0) { if (c + 1 < C && dist[0][r][c + 1] == -1) pq.push(make_tuple(cost + a[r][c], 0, r, c + 1)); if (c - 1 >= 0 && dist[0][r][c - 1] == -1) pq.push(make_tuple(cost + a[r][c - 1], 0, r, c - 1)); if (r + 1 < R && dist[0][r + 1][c] == -1) pq.push(make_tuple(cost + b[r][c], 0, r + 1, c)); if (dist[1][r][c] == -1) pq.push(make_tuple(cost + 1, 1, r, c)); } else { if (dist[0][r][c] == -1) pq.push(make_tuple(cost, 0, r, c)); if (r - 1 >= 0 && dist[1][r - 1][c] == -1) pq.push(make_tuple(cost + 1, 1, r - 1, c)); } } cout << dist[0][R-1][C-1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9+7; int main() { ll H,W; cin>>H>>W; vector<string> field(H); for(int i=0; i<H; ++i) cin>>field[i]; // マス目と縦横ナナメの三種類の移動について vector<vector<vector<ll>>> dp(H,vector<vector<ll>>(W,vector<ll>(3,0))); for(int i=0; i<H; ++i) { for(int j=0; j<W; ++j) { if(field[i][j]=='#' || (i==0 && j==0)) continue; if(i==0 && j==1) { dp[i][j][1]=1; continue; } if(i==1 && j==0) { dp[i][j][0]=1; continue; } if(i==1 && j==1) { if(i-1>=0) dp[i][j][0]=((dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2])+dp[i-1][j][0])%MOD; if(j-1>=0) dp[i][j][1]=((dp[i][j-1][0]+dp[i][j-1][1]+dp[i][j-1][2])+dp[i][j-1][1])%MOD; dp[i][j][2]=1; continue; } // 縦 if(i-1>=0) dp[i][j][0]=((dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2])+dp[i-1][j][0])%MOD; // 横 if(j-1>=0) dp[i][j][1]=((dp[i][j-1][0]+dp[i][j-1][1]+dp[i][j-1][2])+dp[i][j-1][1])%MOD; // ナナメ if(i-1>=0 && j-1>=0) dp[i][j][2]=((dp[i-1][j-1][0]+dp[i-1][j-1][1]+dp[i-1][j-1][2])+dp[i-1][j-1][2])%MOD; } } /* for(auto &v:dp) { for(auto &el:v) cout << el[0]+el[1]+el[2] << " "; cout << endl; } */ cout << (dp[H-1][W-1][0]+dp[H-1][W-1][1]+dp[H-1][W-1][2])%MOD << endl; }
#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<ll, null_type , less<ll> , rb_tree_tag , tree_order_statistics_Node_update> #define ll long long #define ull unsigned long long #define pb push_back #define inf 1e18 #define mk make_pair #define ld long double #define mod 1000000007 #define fi first #define se second #define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define test ll t; cin>>t; while(t--) #define setbits __builtin_popcount #define endl '\n' #define LOCAL #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i));} sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define maxn 61 int main() { fastIO; vector<vector<ll>>ncr(maxn,vector<ll>(maxn)); ncr[0][0]=1; for(int i=1;i<maxn;i++) { ncr[i][0]=1; for(int j=1;j<i;j++) { ncr[i][j]=ncr[i-1][j] + ncr[i-1][j-1]; } ncr[i][i]=1; } ll a,b,k; cin>>a>>b>>k; string ans; ll n=a+b; for(int i=0;i<n;i++) { if(k<=ncr[a+b-1][b]) { ans+='a'; a--; } else{ ans+='b'; k-=ncr[a+b-1][b]; b--; } } cout<<ans<<endl; }
#include <bits/stdc++.h> #define MAX_H 16 #define MAX_W 16 using namespace std; typedef long long ll; ll H, W, A, B; bool used[MAX_H][MAX_W]; ll dfs(ll i, ll bit, ll a, ll b) { if (i == H * W) return 1; if (bit & 1 << i) return dfs(i + 1, bit, a, b); ll res = 0; if (b) res += dfs(i + 1, bit | 1 << i, a, b - 1); if (a) { if (i % W != W - 1 && ~bit && 1 << (i + 1)) res += dfs(i + 1, bit | 1 << i | 1 << (i + 1), a - 1, b); if (i + W < H * W) res += dfs(i + 1, bit | 1 << i | 1 << (i + W), a - 1, b); } return res; } int main() { cin >> H >> W >> A >> B; ll res = dfs(0, 0, A, B); printf("%lld\n", res); }
#include <bits/stdc++.h> #include <unordered_map> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef WA_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif using ll = long long; using vi = vector<int>; using ull = unsigned long long; #define pb push_back #define fi first #define se second #define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++) #define per(i,a,b) for(int i=int(a);i>=(int)(b);i--) const int mod = 1e9+7; const int inf = 0x3f3f3f3f; const int maxn = 1e5+10; int main() { #ifndef WA_DEBUG ios::sync_with_stdio(false);cin.tie(nullptr); #endif int n; cin>>n; string s; ll ans=1e10; cin>>s; if(n==1) { if(s[0]=='1') cout<<ans*2<<'\n'; else cout<<ans<<'\n'; return 0; } rep(i,0,2) { int now=i; bool f=true; for(char c:s) { char tmp; if(now==0) tmp='1'; if(now==1) tmp='1'; if(now==2) tmp='0'; if(tmp!=c) { f=false; break; } now=(now+1)%3; } if(f) { int len=i+n; len=len/3+(!!(len%3)); ans=ans-len+1; cout<<ans<<'\n'; return 0; } } cout<<0<<'\n'; return 0; }
#include<iostream> #include<cmath> using namespace std; #define ll long long int main(){ ll n,ans,N=10000000000; char s[200005]; cin>>n; for(ll i=0;i<n;i++){ cin>>s[i]; } char last='2'; ll num1=0,num0=0; bool flag=0; for(ll i=0;i<n;i++){ if(s[i]=='0'){ num0++; if(num0>1){ flag=1; break; } if(last=='1'){ if(num1<2&&i!=1){ flag=1; break; } num1=0; } } else{ num1++; if(num1>2){ flag=1; break; } num0=0; } last=s[i]; if(flag){ break; } } if(flag){ cout<<0; return 0; } if(n==2&&!(s[0]=='0'&&s[1]=='1')){ cout<<N; return 0; } else if(n==1){ if(s[0]=='0'){ cout<<N; return 0; } else{ cout<<N*2; return 0; } } if(s[0]=='0'){ n-=1; } else{ if(s[1]=='0'){ n-=2; } else{ n-=3; } } ans=n/3; if(n%3){ ans++; } cout<<N-ans; }
#include<bits/stdc++.h> #define int long long using namespace std; char s[500001]; int bb[30]; int vis,numm,ans; char viss; signed main() { scanf("%s",s+1); viss='!'; int len=strlen(s+1); for(int i=len;i>=3;i--) { if(s[i-1]==s[i-2]&&s[i]!=s[i-2]) { if(s[i-1]!=viss) { ans+=(long long)(len-i+1-bb[s[i-1]-'a']); } else { ans+=(long long)(vis-i-bb[s[i-1]-'a']); } //cout<<ans<<endl; viss=s[i-1]; vis=i; for(int j=0;j<=26;j++) bb[j]=0; } else { bb[s[i]-'a']++; } } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define mod 1000000007 #define endl "\n" #define test ll txtc; cin>>txtc; while(txtc--) typedef long long int ll; typedef long double ld; int main() { FIO; //test { string s; cin>>s; ll n=(ll)s.length(); vector<vector<ll>>dp(n,vector<ll>(26,0)); dp[0][s[0]-'a']++; for(ll i=1;i<n;i++){ dp[i][s[i]-'a']++; for(ll j=0;j<26;j++){ dp[i][j]+=dp[i-1][j]; } } ll ans=0,last=n,op=-1; for(ll i=n-2;i>=1;i--){ ll ch=s[i]-'a'; if(s[i]==s[i-1]){ if(op!=ch){ ans+=(n-last); } //cout<<ans<<endl; ans-=(dp[last-1][ch]-dp[i][ch]); //cout<<ans<<endl; ans+=(last-i-1); //cout<<ans<<endl; op=ch; last=i; } } cout<<ans<<endl; } return 0; }
// #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long int; using int64 = long long int; template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int INF = 1LL << 29; const ll LONGINF = 1LL << 60; const ll MOD = 1000000007LL; // ModInt begin using ll = long long; template<ll mod> struct ModInt { ll v; ll mod_pow(ll x, ll n) const { return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod; } ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {} ModInt operator+ ( const ModInt& b ) const { return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v)); } ModInt operator- () const { return ModInt(-v); } ModInt operator- ( const ModInt& b ) const { return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v)); } ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;} ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;} bool operator== ( const ModInt &b ) const {return v == b.v;} bool operator!= ( const ModInt &b ) const {return !(*this == b); } ModInt& operator+= ( const ModInt &b ) { v += b.v; if(v >= mod) v -= mod; return *this; } ModInt& operator-= ( const ModInt &b ) { v -= b.v; if(v < 0) v += mod; return *this; } ModInt& operator*= ( const ModInt &b ) { (v *= b.v) %= mod; return *this; } ModInt& operator/= ( const ModInt &b ) { (v *= mod_pow(b.v, mod-2)) %= mod; return *this; } ModInt pow(ll x) { return ModInt(mod_pow(v, x)); } // operator int() const { return int(v); } // operator long long int() const { return v; } }; template<ll mod> ModInt<mod> pow(ModInt<mod> n, ll k) { return ModInt<mod>(n.mod_pow(n.v, k)); } template<ll mod> ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;} template<ll mod> istream& operator>> (istream& in, ModInt<mod>& a) { in >> a.v; return in; } // ModInt end using mint = ModInt<MOD>; mint dp[3010][3010], rec[3010][3010]; int main() { int N; cin >> N; vector<ll> A(N+1); for(int i=0; i<N; i++) { cin >> A[i+1]; A[i+1] += A[i]; } dp[0][0] = mint(1); for(int i=0; i<N; i++) { for(int j=0; j<=i; j++) { ll m = A[i] % (j+1); rec[j+1][m] += dp[i][j]; } for(int j=0; j<=i; j++) { // j+1 番目の区間を作る ll m = A[i+1] % (j+1); dp[i+1][j+1] += rec[j+1][m]; } } mint ans(0); for(int j=1; j<=N; j++) { ans += dp[N][j]; } cout << ans << endl; return 0; }
/*ver 7*/ #include <bits/stdc++.h> using namespace std; void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);} using ll = long long; using ld = long double; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vb = vector<bool>; using vvl = vector<vector<ll>>; using vvd = vector<vector<ld>>; using vvs = vector<vector<string>>; using vvb = vector<vector<bool>>; using pll = pair<ll,ll>; using mll = map<ll,ll>; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; #define each(x,v) for(auto& x : v) #define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++) #define rep(i,n) for(ll i=0;i<(ll)n;i++) #define pb push_back #define eb emplace_back #define fi first #define se second #define mp make_pair const ll INF = 1LL << 60; #define CLR(mat,f) memset(mat, f, sizeof(mat)) #define IN(a, b, x) (a<=x&&x<=b) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除 #define debug cout << "line : " << __LINE__ << " debug" << endl; #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ind(...) long double __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}} template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}} template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}} template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}} template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}} void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);} void die(){cout << endl;exit(0);} template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);} template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;} template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}} template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}} template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}} template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}} template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}} #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;} ll ceilDiv(ll a,ll b) {return (a+b-1)/b;} ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離 ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } #define YES(n) ((n) ? "YES" : "NO" ) #define Yes(n) ((n) ? "Yes" : "No" ) #define yes(n) ((n) ? "yes" : "no" ) int main(){ init(); inl(n); ld ans=0; reps(i,1,n+1){ ans+=(ld)1/i; } out(ans*n-1); return 0; }
#include <bits/stdc++.h> // clang-format off using namespace std; class Rep{struct I{int i;void operator++(){++i;}int operator*(){return i;}bool operator!=(I o){return i<*o;}}b,e; public:Rep(int l,int r):b{l},e{r}{}Rep(int n):Rep(0,n){}I begin(){return b;}I end(){return e;}}; class Per{struct I{int i;void operator++(){--i;}int operator*(){return i;}bool operator!=(I o){return i>*o;}}b,e; public:Per(int l,int r):b{r-1},e{l-1}{}Per(int n):Per(0,n){}I begin(){return b;}I end(){return e;}}; template<class F>struct Fix:F{Fix(F f):F(f){} template<class...T>decltype(auto)operator()(T&&...a){return F::operator()(*this,forward<T>(a)...);}}; template<class T=int>T scan(){T r;cin>>r;return r;} template<class T,class U=T>bool chmin(T&a,U&&b){return b<a?a=forward<U>(b),1:0;} template<class T,class U=T>bool chmax(T&a,U&&b){return a<b?a=forward<U>(b),1:0;} #ifndef LOCAL #define DUMP(...) void(0) #endif #define ALL(c) begin(c), end(c) // clang-format on main() { cin.tie(nullptr)->sync_with_stdio(false); auto s = scan<string>(); int ans = 0; for (int i : Rep(10000)) { vector<bool> exists(10); for (int _ = 4; _--;) { exists[i % 10] = true; i /= 10; } ans += [&]() -> bool { for (int x : Rep(10)) { if (s[x] == 'o' and not exists[x]) return false; if (s[x] == 'x' and exists[x]) return false; } return true; }(); } cout << ans << '\n'; }
#include <iostream> #include <string> #include <set> #include <map> using namespace std; int main() { string s; cin >> s; int r = 0; map<int, int> c; int d[10] = {}; for (int i = 0; i < 10; i++) { if (s[i] == 'o') { c[i] = 1; d[i] = 1; r++; } else if (s[i] == '?') { d[i] = 1; } } if (c.size() > 4) { cout << 0; return 0; } int sum = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { for (int h = 0; h < 10; h++) { int temp = 0; if (c[i]) temp++; if (i != j && c[j]) temp++; if (k != j && k != i && c[k]) temp++; if (h != i && h != j && h != k && c[h]) temp++; if (temp == r) { if (d[i] + d[j] + d[k] + d[h] == 4) { sum++; } } } } } } cout << sum << endl; }
#ifdef MY_LOCAL #define MY_NAMESPACE(ns) namespace ns { #define MY_NAMESPACE_ } #define MY_DEBUG(s) s #define MY_IS_DEBUG (true) #else #define MY_NAMESPACE(ns) #define MY_NAMESPACE_ #define MY_DEBUG(s) #define MY_IS_DEBUG (false) #endif #include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; constexpr int IMIN = numeric_limits<int>::min(); constexpr int IMAX = numeric_limits<int>::max(); constexpr ll LLMIN = numeric_limits<ll>::min(); constexpr ll LLMAX = numeric_limits<ll>::max(); #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto& i: a) #define all(x) (x).begin(), (x).end() template <typename T> bool chmax(T& maxval, T const& newval) { if (newval > maxval) { maxval = newval; return true; } return false; } template <typename T> bool chmin(T& minval, T const& newval) { if (newval < minval) { minval = newval; return true; } return false; } MY_NAMESPACE(testbed) int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<vector<tuple<int, ll, ll, ll>>> e(n); rep(i, 0, m) { int a, b; ll c, d; cin >> a >> b >> c >> d; a--; b--; ll opt = (d == 0 ? 0LL : (ll)exp(-1.0 / (double)d)); e[a].push_back({ b, c, d, opt }); e[b].push_back({ a, c, d, opt }); } auto priority_fn = [](pil const& lhs, pil const& rhs)->bool // return true to prioritize rhs { return (rhs.second < lhs.second); }; vector<ll> min_time(n, LLMAX); priority_queue<pil, vector<pil>, decltype(priority_fn)> q(priority_fn); q.push({ 0, 0 }); min_time[0] = 0; while (!q.empty()) { int this_city; ll this_time; tie(this_city, this_time) = q.top(); q.pop(); if (this_time > min_time[this_city]) continue; fore(tpl, e[this_city]) { int next_city = get<0>(tpl); ll cval = get<1>(tpl); ll dval = get<2>(tpl); ll optval = get<3>(tpl); ll next_time = this_time; if (dval == 0) { next_time += cval; } else { ll min_move = LLMAX; ll wait_time = max((ll)sqrt((double)dval) - 1LL - this_time, 0LL); for (ll adj = -100; adj <= 100; adj++) { ll this_wait_time = max(wait_time + adj, 0LL); ll this_move = this_wait_time + cval + dval / (this_time + this_wait_time + 1LL); chmin(min_move, this_move); } next_time += min_move; } if (chmin(min_time[next_city], next_time)) { q.push({ next_city, next_time }); } } } ll ans = min_time[n - 1]; if (ans == LLMAX) ans = -1; cout << ans << "\n"; return 0; } MY_NAMESPACE_
#include <bits/stdc++.h> // #include <atcoder/modint> #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define INF 2000000000000000000 #define ll long long #define ld long double #define pll pair<ll, ll> using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } struct edge {ll to, c, d, index;}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, M; cin >> N >> M; vector<vector<edge>> connection(N); map<ll, ll> mp; for (ll i = 0; i < M; ++i) { ll a, b, c, d; cin >> a >> b >> c >> d; a -= 1, b -= 1; connection.at(a).push_back({b, c, d, i}); connection.at(b).push_back({a, c, d, i}); ld l = 0, r = d; while (l + 0.01 < r) { ld m1 = (l * 2 + r) / 3.0; ld m2 = (l + r * 2) / 3.0; ld c1 = m1 + (d / (m1 + 1)); ld c2 = m2 + (d / (m2 + 1)); if (c1 < c2) { r = m2; } else { l = m1; } } ll l1 = floor(l), l2 = ceil(l); ll l0 = l1; if (l1 + d / (l1 + 1) > l2 + d / (l2 + 1)) { l0 = l2; } mp[i] = max(0ll, l0); } vector<ll> dist(N, INF); dist.at(0) = 0; typedef pair<ll, ll> P;// first は最短距離 second は頂点の番号 priority_queue<P, vector<P>, greater<P>> q; q.push({0, 0}); while (!q.empty()) { ll now = q.top().second; ll now_dist = q.top().first; q.pop(); if (dist.at(now) < now_dist) continue; for (ll i = 0; i < connection.at(now).size(); ++i) { edge e = connection.at(now).at(i); ll next = e.to, c = e.c, d = e.d, index = e.index; if (dist.at(next) <= dist.at(now) + c) { continue; } ll temp = mp[index]; // cout << temp << "\n"; if (dist.at(now) >= temp) { if (dist.at(next) > dist.at(now) + c + d / (dist.at(now) + 1)) { chmin(dist.at(next), dist.at(now) + c + d / (dist.at(now) + 1)); q.push({dist.at(next), next}); } } else { if (dist.at(next) > temp + c + d / (temp + 1)) { chmin(dist.at(next), temp + c + d / (temp + 1)); q.push({dist.at(next), next}); } } } } if (dist.at(N - 1) == INF) { cout << -1 << "\n"; } else { cout << dist.at(N - 1) << "\n"; } }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <tuple> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; double f(double w, double t, double c, double d) { return t + w + c + (d / (t + w + 1.0)); } int64_t get_wait(int64_t time, int64_t c, int64_t d) { double low = 0; double high = 1e9; int cnt = 64; while (cnt--) { double c1 = (low * 2 + high) / 3; double c2 = (low + high * 2) / 3; if (f(c1, time, c, d) > f(c2, time, c, d)) low = c1; else high = c2; } return ceil(low); } int main(void) { ios::sync_with_stdio(false); int N, M; cin >> N >> M; using gtype = tuple<int64_t, int64_t, int64_t>; vector<vector<gtype>> G(N); rep(i, M) { int A, B; int64_t C, D; cin >> A >> B >> C >> D; A--, B--; G[A].emplace_back(B, C, D); G[B].emplace_back(A, C, D); } using qtype = tuple<int64_t, int64_t>; priority_queue<qtype, vector<qtype>, greater<qtype>> que; que.emplace(0, 0); vector<int64_t> visit(N, INT64_MAX / 2); visit[0] = 0; while (!que.empty()) { auto [time, u] = que.top(); que.pop(); if (u == N - 1) { cout << time << endl; return 0; } if (visit[u] < time) continue; for (const auto& [v, c, d] : G[u]) { int64_t wait = get_wait(time, c, d); int64_t ntime = time + wait + c + d / (time + wait + 1); if (ntime < visit[v]) { visit[v] = ntime; que.emplace(ntime, v); } } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i= 0; i < (n); i++) using ll= long long int; using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mod= 1e9 + 7; struct UnionFind { //自身が親であれば、その集合に属する頂点数に-1を掛けたもの //そうでなければ親のid vector<int> r; UnionFind(int N) { r = vector<int>(N, -1); } int root(int x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } int size(int x) { return -r[root(x)]; } }; int main() { int n,m; cin >> n >> m; ll a[n],b[n]; rep(i,n)cin >> a[i]; rep(i,n)cin >> b[i]; UnionFind tree(n); rep(i,m){ int a,b; cin >> a >> b; a--,b--; tree.unite(a,b); } ll ansa[n]={},ansb[n]={}; rep(i,n){ ll ak=a[i],bk=b[i]; a[i]=0,b[i]=0; ansa[tree.root(i)]+=ak; ansb[tree.root(i)]+=bk; } //rep(i,n)cout << ansa[i] << " " << ansb[i] << " " << tree.root(i) << endl; rep(i,n)if(ansa[tree.root(i)]!=ansb[tree.root(i)]){cout << "No" << endl; return 0;} cout << "Yes" << endl; }
#include<bits/stdc++.h> using namespace std; #define f(i,a,b) for(int i=a; i<b; i++) #define fr(i,a,b) for(int i=a; i>=b; i--) #define fll(i,a,b) for(ll i=a; i<b; i++) #define frll(i,a,b) for(ll i=a; i>=b; i--) typedef vector<int> vi; typedef long long int ll; typedef vector<ll> vll; #define fastio std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define PI 3.1415926535 ll Mod = 1e9+7; ll N = 1e5; // ll Mod = 998244353; // ll mult(ll a, ll b, ll mod = Mod) { // ll ret = 1; // ret = ((a%mod) * (b%mod))%mod; // return ret; // } // ll pow(ll a, ll b, ll mod = Mod-2) { // if(b == 0) return 1; // ll ret = 1; // while(b) { // if(b&1) { // ret = mult(ret,a,mod); // } // a = mult(a,a,mod); // b>>=1; // } // return ret; // } // ll sub(ll a, ll b) { // ll ret = (a%Mod - b%Mod + Mod)%Mod; // return ret; // } // void seive(vector<bool> &status) { // f(i,2,2e6+1) { // if(status[i]) { // for(ll j = i*i; j<=2e6; j+=i) status[j] = 0; // } // } // } // int getmex(vi& occr, int n, int mex) { // for(int i = mex; i<n; i++) { // if(occr[i] == 0) return i; // } // return n; // } // void swap(int &a, int &b) { // a = a + b; // b = a - b; // a = a - b; // } // int gcd(int a, int b) { // if(b == 0) return a; // return gcd(b,a%b); // } void test_case() { int n,u,v; ll w; cin>>n; vector<vector<int>> tree(n+1); vector<vector<ll>> wt(n+1); f(i,0,n-1) { cin>>u>>v>>w; tree[v].push_back(u); tree[u].push_back(v); wt[v].push_back(w); wt[u].push_back(w); } queue<int> q; q.push(1); ll dist[n+1]; memset(dist, -1, sizeof dist); dist[1] = 0; while(!q.empty()) { int p = q.front(); q.pop(); f(i,0,tree[p].size()) { int x = tree[p][i]; ll w = wt[p][i]; if(dist[x] == -1) { dist[x] = dist[p]^w; q.push(x); } } } ll ans = 0; f(i,0,60) { ll up = 0, lo = 0; f(j,1,n+1) { if((dist[j]>>i)&1) up++; else lo++; } ans = (ans + ((1LL<<i)%Mod*((up*lo)%Mod))%Mod)%Mod; } cout<<ans<<'\n'; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif fastio; // vector<bool> status(2e6+1,1); int t = 1; // vi primes; // seive(status); // f(i,2, 2e6+1) { // if(i > 5000) { // break; // } // if(status[i]) primes.push_back(i); // } // cin>>t; while(t--) { test_case(); } }
#include <bits/stdc++.h> using namespace std; #define io ios_base::sync_with_stdio(false); cin.tie(nullptr); #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } #define int long long const int nax = 200001; vector<int> graph[nax]; vector<int> weight[nax]; int32_t main() { int n; scanf("%lld", &n); for(int i = 0; i < n - 1; ++i){ int u, v, w; scanf("%lld %lld %lld", &u, &v, &w); graph[u].push_back(v); graph[v].push_back(u); weight[u].push_back(w); weight[v].push_back(w); } int dist_from_root[n + 1] = {0}; function<void(int, int, int)> dfs = [&] (int vertex, int parent, int curr){ dist_from_root[vertex] = curr; for(int i = 0; i < (int)graph[vertex].size(); ++i){ int child = graph[vertex][i]; int node_weight = weight[vertex][i]; if(child == parent) continue; dfs(child, vertex, curr^node_weight); } }; dfs(1, 0, 0); int track_bits[60] = {0}; for(int i = 1; i <= n; ++i){ for(int j = 0; j < 60; ++j){ if((dist_from_root[i] & (1ll << j))) ++track_bits[j]; } } int ans = 0; int MOD = 1e9 + 7; for(int i = 0; i < 60; ++i){ int value = ((track_bits[i] % MOD) * ((n - track_bits[i]) % MOD)) % MOD * ((1ll << i) % MOD); value = value % MOD; ans = ans + value; ans = (ans + MOD) % MOD; } printf("%lld\n", ans); return 0; }
// --------------------<optimizations>-------------------- #pragma GCC optimize("O3") //(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS) #pragma comment(linker, "/stack:200000000") //(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS) #pragma GCC optimize("unroll-loops") // -------------------</optimizations>-------------------- #include <iostream> #include <bitset> #include <algorithm> // sort() #include <vector> // for vectors #include <map> // for ordered map #include <unordered_map> #include <list> // for list #include <iomanip> // for setprecision() #include <cmath> // for pow(), sqrt(), log(), log10(), .... #include <cstring> // for memset() #include <unordered_set> // for unordered_set #include <climits> // for INT_MAX & INT_MIN #include <set> // for ordered set #define C ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; typedef long long ll; #define INF INT_MAX #define NINF INT_MIN #define MOD 1000000007 const double pi=acos(-1); // value of pi bool isVowel(char ch) { if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') { return 1; } return 0; } bool compare(pair<ll, ll> a, pair<ll, ll> b) { return a.second<b.second; } bool isPrime(ll n) { if(n%2==0 && n!=2) { return false; } for(ll i=3; i*i<=n; i+=2) { if(n%i==0) { return false; } } return true; } ll binary_exp(ll x, ll n, ll mod) { if (n == 0) { return 1; } else if (n == 1) { return x % mod; } else { ll temp = binary_exp(x, n / 2, mod); temp = (temp * temp) % mod; if (n % 2 == 0) { return temp; } else { return ((x % mod) * temp) % mod; } } } map<ll, ll> a, b; // map<ll, ll> m; int main() { C // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int t=1; // cin >> t; while(t--) { ll n, a, b; cin >> n >> a >> b; cout << n-a+b << endl; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f; const double pi=3.1415926535897932384626; inline ll read(){ ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } const int maxn=1e5+5; int n,m,k,x,vis[maxn]; double dp[maxn][2],sum[maxn][2]; int main(){ n=read(),m=read(),k=read(); for(int i=1;i<=k;++i){ x=read(); vis[x]=1; } dp[n][0]=dp[n][1]=0; for(int i=n-1;i>=0;--i){ if(vis[i]){ dp[i][0]=1,dp[i][1]=0; } else{ dp[i][0]=(sum[i+1][0]-sum[i+m+1][0])/m; dp[i][1]=(sum[i+1][1]-sum[i+m+1][1])/m+1.0; } sum[i][0]=sum[i+1][0]+dp[i][0]; sum[i][1]=sum[i+1][1]+dp[i][1]; } if(fabs(dp[0][0]-1.0)<1e-6) printf("-1\n"); else printf("%.4f\n",dp[0][1]/(1.0-dp[0][0])); return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #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 ll tmp[600001], *A = tmp + 200000, *B = tmp + 400000; void pakuri_sort(int N, ll 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]; } } const int CM = 400001, 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 main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); rep(i, N) { A[i] = ll(i) << 32 | getint(); B[i] = ll(i) << 32 | getint(); } pakuri_sort(N, A); pakuri_sort(N, B); const ll ma = (1ll << 32) - 1; rep(i, N) { auto tmp = (A[i] & ma) < (B[N - 1 - i] & ma); cn[A[i] >> 31] = tmp; cn[B[N - 1 - i] >> 31 | 1] = tmp; } int c = -1, pn = 0; rep(i, N << 1) { if (c != -1 && pn == cn[i]) { cn[c] = '('; cn[i] = ')'; c = tmp[c]; pn ^= 1; } else { pn = cn[i]; tmp[i] = c; c = i; } } fwrite(cn, 1, N << 1, stdout); Would you please return 0; }
/*@author : ashnove*/ #include <bits/stdc++.h> using namespace std; #define ll long long int #define lld long double #define F first #define S second #define endl "\n" #define nl cout<<endl; #define pb push_back #define f(i,a,b) for(ll i=a;i<b;i++) #define what_is(x) cerr << #x << " is " << x << endl; #define mat vector<vector<ll>> mat cn(ll n, ll m){return vector< vector<ll> >(n, vector<ll>(m));} bool compare(char &s1,char &s2){return s1 > s2;} bool sortmahstyle(const pair<ll, pair<ll,ll>> &a, const pair<ll, pair<ll,ll>> &b){ if(a.first > b.first ) return 1; if( a.first==b.first && a.S.S > b.S.S ) return 1; return 0; } ll const mod = 1e9+7; ll const inf = 1e18; ll const maxn = 2e5+1; class edge{ public: ll a,b,w; }; vector<edge> adj; void solve() { ll n,x; cin >> n >> x; ll a[n]; for(ll i = 0; i < n; i++) cin >> a[i]; ll dp[n][n+1][n]; f(i,0,n)f(j,0,n+1)f(k,0,n) dp[i][j][k] = -1; f(i,0,n) dp[i][0][0] = 0; for(ll i = 0; i < n; i++){ for(ll j = 0; j < n; j++){ for(ll cnt = i+1; cnt>0; cnt--){ for(ll rem = 0; rem <= i; rem++){ if(dp[i][cnt-1][rem]!=-1){ ll sum = dp[i][cnt-1][rem] + a[j]; dp[i][cnt][ sum%(i+1) ] = max(dp[i][cnt][ sum%(i+1) ] , sum); } } } } } ll res = x; for(ll i = 0; i < n; i++){ if(dp[i][i+1][ x % (i+1) ] != -1) res = min(res, (x - dp[i][i+1][ x % (i+1) ])/(i+1)); } cout << res << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); //* #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif /**/ ll t; /**/t=1; /*/cin>>t;/**/ f(testcases,0,t){ //cout << "Case #" <<testcases+1<<": "; solve(); } // printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int 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) template <typename T, typename V> void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); } template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) {} #endif int main() { int N; cin >> N; lint ret = 0; while (N--) { lint a, b; cin >> a >> b; ret += (b + a) * (b - a + 1) / 2; } cout << ret << '\n'; }
#include <bits/stdc++.h> using namespace std; #define INF (int)1e9 #define eps (int)1e-9 #define ll long long #define ld long double #define FOR(i,m,n) for(int i=m; i<n; i++) #define vt vector #define pb push_back #define all(c) (c).begin(), (c).end() #define sz(x) (int)(x).size() void solve(){ ld n; cin >> n; vt<ld> a(n); vt<ld> b(n); ld sum=0; FOR(i,0,n){ cin >> a[i]; cin >> b[i]; } FOR(i,0,n){ ld diff = b[i]-a[i]+1; sum += b[i]*diff - (diff-1)*(diff)/2; } cout << setprecision(30) << sum << endl; } int main(){ int tt; //cin >> tt; tt=1; while (tt--){ solve(); } }
/** * author: stein * created: 2020.12.19 14:59:32 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, W; cin >> N >> W; cout << N / W << '\n'; return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define ook order_of_key #define fbo find_by_order #define ll long long ll md=998244353; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define g(v,i,j) get<i>(v[j]) #define vi vector<int> #define vll vector<ll> #define srt(v) sort(v.begin(),v.end()) #define rsrt(v) sort(v.begin(),v.end(),greater<int>()) #define fi(i,a,b) for(int i=a;i<b;i++) #define fll(i,a,b) for(ll i=a;i<b;i++) using namespace std; using namespace std::chrono; template<typename T> T pw(T a, T b) { T c=1,m=a; while (b){ if(b&1) c=(c * m); m=(m*m),b/=2; } return c; } template<typename T> T ceel(T a,T b) { if(a%b==0) return a/b; else return a/b+1; } template<typename T> T my_log(T n, T b) { T i=1,ans=0; while(1){ if(i>n){ ans--; break; } if(i==n) break; i*=b,ans++; } return ans; } template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template<typename T> T mysqt(T a) { T ans=1; while(ans*ans<=a) ans++; ans--; return ans; } ll pwmd(ll a,ll b) { ll c=1,m=a; while(b){ if(b&1) c=(c*m)%md; m=(m*m)%md; b/=2; } return c; } ll modinv(ll n) { return pwmd(n, md - 2); } ll inverse(ll i){ if(i==1) return 1; return (md - ((md/i)*inverse(md%i))%md+md)%md; } bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b) { return (a.second < b.second); } /* auto start = high_resolution_clock::now(); auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); cerr << "Time taken: " << duration.count()<< "ms" <<"\n";*/ vector<char> capl={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; vector<char> sml={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; template<typename T> string conv(T a) { string b; T e=2; T c=my_log(a,e); c++,b='1'; for(T i=0;i<c-1;i++){ T k=a>>(c-2-i); if(k&1) b+='1'; else b+='0'; } if(a==0) return "0"; return b; } int main() { fast; int n,w; cin>>n>>w; cout<<n/w; }
#ifdef LOCAL //#define _GLIBCXX_DEBUG #endif //#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, int> Pi; typedef vector<ll> Vec; typedef vector<int> Vi; typedef vector<string> Vs; typedef vector<char> Vc; typedef vector<P> VP; typedef vector<vector<ll>> VV; typedef vector<vector<int>> VVi; typedef vector<vector<char>> VVc; typedef vector<vector<vector<ll>>> VVV; typedef vector<vector<vector<vector<ll>>>> VVVV; #define endl '\n' #define REP(i, a, b) for(ll i=(a); i<(b); i++) #define PER(i, a, b) for(ll i=(a); i>=(b); i--) #define rep(i, n) REP(i, 0, n) #define per(i, n) PER(i, n, 0) const ll INF=1e18+18; const ll MOD=1000000007; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define ALL(v) v.begin(), v.end() #define rALL(v) v.rbegin(), v.rend() #define pb(x) push_back(x) #define mp(a, b) make_pair(a,b) #define Each(a,b) for(auto &a :b) #define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i) #ifdef LOCAL #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;} #define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl; #define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;} #define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl; #else #define dbg(...) #define dbgmap(...) #define dbgset(...) #define dbgarr(...) #define dbgdp(...) #endif #define out(a) cout<<a<<endl #define out2(a,b) cout<<a<<" "<<b<<endl #define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl #define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define fi first #define se second template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } template<typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; } template<typename T>istream& operator>>(istream&i,vector<T>&v) {rep(j,v.size())i>>v[j];return i;} // vector template<typename T> ostream &operator<<(ostream &s, const vector<T> &v) { int len=v.size(); for(int i=0; i<len; ++i) { s<<v[i]; if(i<len-1) s<<" "; } return s; } // 2 dimentional vector template<typename T> ostream &operator<<(ostream &s, const vector<vector<T> > &vv) { s<<endl; int len=vv.size(); for(int i=0; i<len; ++i) { s<<vv[i]<<endl; } return s; } int solve(){ string s; cin>>s; ll ok = 1; auto is_upper = [](char c)->bool{ return toupper(c) == c; }; auto is_lower = [](char c)->bool{ return tolower(c) == c; }; ll n = s.length(); string S; rep(i,n){ if(i%2==0){ if(!is_lower(s[i])){ ok = 0; } S += tolower(s[i]); }else{ if(!is_upper(s[i])){ ok = 0; } S += toupper(s[i]); } } dbg(s) dbg(S) Yes(ok); return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout<<std::setprecision(10); // ll T; // cin>>T; // while(T--) solve(); }
#include<bits/stdc++.h> using namespace std; const int maxn=102; vector<int> g[maxn]; bool vis[maxn]; int n; string s[maxn]; int dfs(int s,int p) { int res=1; if(vis[s]) return 0; vis[s]=1; for(auto it:g[s]) if(it!=p) res+=dfs(it,s); return res; } int main() { cin>>n; for(int i=0;i<n;i++) cin>>s[i]; for(int i=0;i<n;i++) for(int j=0;j<s[i].size();j++) if(s[i][j]=='1') g[j].push_back(i); double res=0.0; for(int i=0;i<n;i++) { memset(vis,0,sizeof(vis)); res+=1.0/dfs(i,i); } cout<<fixed<<setprecision(10)<<res; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mxN = 1+1000*100; #define int long long const int mod = 1e9+7; signed main() { int n; cin>>n; string s; cin>>s; vector<char> v; for(int i = 0 ;i<n;i++) { v.push_back(s[i]); if(v.size()>=3) { int sz = v.size(); if(v[sz-1]=='x' && v[sz-2]=='o' && v[sz-3]=='f') { v.pop_back(); v.pop_back(); v.pop_back(); } } } cout<<v.size(); }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> count(N+10); int a; for (int i = 0; i < N; i++) { cin >> a; count[a]++; } count[0] = min(count[0],K); int sum = 0; for (int i = 1; i < N+10; i++) { if (count[i-1] <= count[i]) { count[i] = count[i-1]; } else { sum += (count[i-1]-count[i])*i; } if (count[i] == 0) { break; } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) int main(){ string s; cin >> s; deque<char> t; string ans; bool h = false; rep(i, s.size()){ if (s[i] == 'R') h = !h; else{ if (!h) t.push_back(s[i]); else t.push_front(s[i]); } } if (h){ reverse(t.begin(), t.end()); } for (char c : t){ if (ans.size() && ans.back() == c) ans.pop_back(); else ans.push_back(c); } cout << ans << endl; }
// Author :: <Hitesh_Saini> #include<bits/stdc++.h> #define __speed() ios_base::sync_with_stdio(false), cin.tie(nullptr); #define dbg(x) cout << "(" << __LINE__ << ": "<< #x << " = " << x << ")\n" #define Yes(x) puts((x) ? "Yes" : "No") #define tt int t; for (cin >> t; t--; ) #define f0(i, n) for (i = 0; i < (int)(n); i++) #define f1(i, n) for (i = 1; i <=(int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)(x.size()) #define EB emplace_back #define PB push_back #define LB lower_bound #define UB upper_bound #define endl "\n" #define S second #define F first using namespace std; using mii = map<int, int>; using pii = pair<int, int>; using ll = int64_t; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; const pair<int, int> DD[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int mod = 1e9 + 7, mxN = 2e5 + 5, INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; template <typename... T> void print(T... args) { ((cout << args << " "), ...), cout << endl; } template <typename T1, typename T2> bool cmax(T1& a, T2 b) { if (b > a) { a = b; return true; } return false; } template <typename T1, typename T2> bool cmin(T1& a, T2 b) { if (b < a) { a = b; return true; } return false; } template <typename T> istream& operator>>(istream& in, vector<T>& v) { for (T& x:v) in >> x; return in; } template <typename T> ostream& operator<<(ostream& ot, vector<T>& v) { for (T& x:v) ot << x << ' '; return ot;} void solve() { int n, d, h, i, j; string s; cin >> s; n = sz(s); deque<char> ans; int x = 1; for (char z : s) { if (z == 'R') x ^= 1; else { if (x) { if (sz(ans) && ans.back() == z) ans.pop_back(); else ans.push_back(z); } else { if (sz(ans) && ans.front() == z) ans.pop_front(); else ans.push_front(z); } } } // ans.erase(unique(all(ans)), ans.end()); if (!x) reverse(all(ans)); for (char x : ans) cout << x; } signed main() { cout << fixed << setprecision(12); __speed() solve(); // int t, i; for (cin >> t, i = 1; i <= t; i++) cout << "Case #" << i << ": ", solve(); }
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <bitset> #include <climits> #include <string> #include <cmath> #include <bitset> #include <complex> #include <functional> #include <ctime> #include <cassert> #include <fstream> #include <stack> #include <random> #include <iomanip> #include <fstream> using namespace std; typedef long long ll; typedef long double dd; #define i_7 (ll)(1E9+7) //#define i_7 998244353 #define i_5 i_7-2 ll mod(ll a){ ll c=a%i_7; if(c>=0)return c; return c+i_7; } typedef pair<ll,ll> l_l; ll inf=(ll)1E18; #define rep(i,l,r) for(ll i=l;i<=r;i++) #define pb push_back ll max(ll a,ll b){if(a<b)return b;else return a;} ll min(ll a,ll b){if(a>b)return b;else return a;} dd EPS=1E-9; #define endl "\n" #define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int main(){fastio ll a[4];rep(i,0,3)cin>>a[i]; cout<<a[0]*a[3]-a[1]*a[2]<<endl; return 0; }
/* これを入れて実行 g++ code.cpp ./a.out */ #include <iostream> #include <cstdio> #include <stdio.h> #include <vector> #include <string> #include <cstring> #include <queue> #include <deque> #include <stack> #include <algorithm> #include <utility> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cmath> #include <math.h> #include <tuple> #include <iomanip> #include <bitset> #include <functional> #include <cassert> #include <random> #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 61; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){ return f.second > s.second; } ll gcd(ll a, ll b){ if (b == 0)return a; return gcd(b, a % b); } ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } ll conbinationMemo[201][12]; void cmemoInit(){ rep(i, 201){ rep(j, 12){ conbinationMemo[i][j] = -1; } } } ll nCr(ll n, ll r){ if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r]; if(r == 0 || r == n){ return 1; } else if(r == 1){ return n; } return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r){ r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- int main(void){ ll n; cin >> n; cout << n - 1 << endl; }
#include<iostream> #include<unordered_set> using namespace std; int main(){ int N; cin >> N; string str; string ss[N]; unordered_set<string> s; for(int i=0; i<N; i++){ cin >> str; s.insert(str); ss[i] = str; } for(int i=0; i<N; i++){ string ch = "!" + ss[i]; if(s.find(ch)!=s.end()){ cout << ss[i] << endl; return 0; } } cout << "satisfiable" << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<n;i++) #define rep3(i,in,fn) for(int i=in;i<=fn;i++) int main(){ int N,S,D; cin >> N >> S >> D; int X,Y; rep(i,N){ cin >> X >> Y; if(X<S && Y>D){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; }
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> #include<map> #include<stack> #include<queue> #include<ctime> #include<vector> #include<set> #include<cstdlib> #include<utility> using namespace std; const int mod=1e9+7; int a[100000+5]; int main() { int n,i,ans=1; scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); for (i=n;i>=2;i--) a[i]-=a[i-1]; for (i=1;i<=n;i++) ans=1ll*ans*(a[i]+1)%mod; printf("%d\n",ans); }
#include <bits/stdc++.h> #define pb push_back #define sz size() #define all(c) (c).begin(), (c).end() using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MOD = 1e9 + 7; const int maxn = 2e5 + 7; int cnt[200]; vector<int> g[200]; void solve(){ int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++)cin >> a[i]; for(int i = 0; i < n; i++)a[i] %= 200; int s = min(8, n); for(int mask = 0; mask < (1 << s); mask++){ int rem = 0; vector<int> e; for(int bit = 0; bit < s; bit++){ if(mask & (1 << bit)){ rem = (rem + a[bit]) % 200; e.pb(bit + 1); } } if(g[rem].sz != 0){ cout << "YES\n"; cout << e.sz << ' '; for(int i : e)cout << i << ' '; cout << '\n'; cout << g[rem].sz << ' '; for(int i : g[rem])cout << i << ' '; return; } else{ g[rem] = e; } } cout << "NO\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string X; long long M; cin >> X >> M; int d = (*max_element(X.begin(), X.end())) - '0'; if(X.size() == 1){ if(atoi(X.c_str()) <= M)cout << "1\n"; else cout << "0\n"; return 0; } auto ok = [&](long long base){ long long power = 1, total = 0; for(int i = (int)X.size() - 1; i >= 0; i--){ long long cur; if((X[i] - '0') > M / power)cur = M + 1; else cur = (X[i] - '0') * power; total = min(M + 1, total + cur); if(power > M / base)power = M + 1; else power = power * base; } if(total <= M)return true; else return false; }; if(!ok(d + 1)){ cout << "0\n"; return 0; } long long L = d + 1, R = M; while(L < R){ long long mid = (L + R + 1) / 2; if(ok(mid))L = mid; else R = mid - 1; } cout << L - d << '\n'; return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; template<class S, class T> inline S max_L(S a,T b){ return a>=b?a:b; } 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(long long &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; } } inline void rd(char &c){ int i; for(;;){ i = my_getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c = i; } inline int rd(char c[]){ int i; int sz = 0; for(;;){ i = my_getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c[sz++] = i; for(;;){ i = my_getchar_unlocked(); if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){ break; } c[sz++] = i; } c[sz]='\0'; return sz; } 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(long long x){ int s=0; int m=0; char f[20]; 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; char X[70]; long long M; int main(){ int i; long long res = 0; long long d; long long mx; long long b; __int128_t val; N = rd(X); rd(M); for(i=(0);i<(N);i++){ X[i] -= '0'; } int Q5VJL1cS; char e98WHCEY; if(N==0){ e98WHCEY = 0; } else{ e98WHCEY = X[0]; for(Q5VJL1cS=(1);Q5VJL1cS<(N);Q5VJL1cS++){ e98WHCEY = max_L(e98WHCEY, X[Q5VJL1cS]); } } d =e98WHCEY; if(N==1){ if(X[0] <= M){ res++; } wt_L(res); wt_L('\n'); return 0; } long long FmcKpFmN; long long xr20shxY; long long WYIGIcGE; FmcKpFmN = d; xr20shxY = 1e18; while(FmcKpFmN < xr20shxY){ if((FmcKpFmN + xr20shxY)%2==0){ WYIGIcGE = (FmcKpFmN + xr20shxY) / 2; } else{ WYIGIcGE = (FmcKpFmN + xr20shxY + 1) / 2; } val = 0; for(i=(0);i<(N);i++){ val = val * WYIGIcGE + X[i]; if(val > M){ break; } } if(val <= M){ FmcKpFmN = WYIGIcGE; } else{ xr20shxY = WYIGIcGE - 1; } } b =xr20shxY; res = b - d; wt_L(res); wt_L('\n'); return 0; } // cLay version 20210103-1 [bug fixed 4] // --- original code --- // int N; char X[70]; // ll M; // { // ll res = 0, d, mx, b; // __int128_t val; // rd(X@N,M); // rep(i,N) X[i] -= '0'; // d = max(X(N)); // // if(N==1){ // if(X[0] <= M) res++; // wt(res); // return 0; // } // // if(N==2){ // // res = (M - X[1]) / X[0] - d; // // res >?= 0; // // wt(res); // // return 0; // // } // b = bsearch_max[ll,b,d,1e18][ // val = 0; // rep(i,N){ // val = val * b + X[i]; // if(val > M) break; // } // ](val <= M); // res = b - d; // wt(res); // }
#include <bits/stdc++.h> using namespace std; #ifdef __local_leywar #include <debug/debugger.h> #endif #define int long long #define endl '\n' const int INF = 2e9, MOD = 1e9+7, INFLL = 1e18; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int cnt = 3; cout << 6 << ' ' << 10 << ' ' << 15 << ' ' ; for(int i = 1 ; i <= 10000 and cnt < n ; i++) { if((i % 6 == 0 or i % 10 == 0 or i % 15 == 0) and (i != 6 and i != 10 and i != 15)) { cout << i << ' ' ; cnt++; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) using ll = long long; const ll INF = 1LL << 30; using pll = pair<string, ll>; using Graph = vector<vector<int>>; int main() { int N; cin >> N; if(N == 3) { cout << 6 << " " << 10 << " " << 15 << endl; return 0; } vector<int> S(16); S = {6, 10, 12, 15, 18, 20, 24, 30, 36, 40, 42, 45, 48, 50, 54, 60}; rep(i, N) { cout << S[i%16] << " "; S[i%16] += 60; } cout << endl; }
#include <bits/stdc++.h> using namespace std; #define int int64_t #define MOD 998244353 #define inf 9e18 #define tikoo (int) (1e9 + 7) #define amat(mat, n, m, val) mat.assign(n, vector<int>(m, val)) #define mmat(mat, n, m, val) vector<vector<int>> mat(n, vector<int>(m, val)) #define _mul(a, b) ((a % tikoo) * (b % tikoo)) % tikoo #define _add(a, b) ((a % tikoo) + (b % tikoo)) % tikoo #define _sub(a, b) (((a % tikoo) - (b % tikoo)) % tikoo + tikoo) % tikoo #define _all(a) a.begin(), a.end() #define print(a) for (auto i : a) cout << i << " "; cout << endl; #define take(x) int x; cin >> x; double logn(int n, int base) { return log2(n) / log2(base); } template<typename T, typename T1> T max(T a, T1 b) { if (b > a) a = b; return a; } template<typename T, typename T1> T min(T a, T1 b) { if (b < a) a = b; return a; } int fastpow(int a, int b, int m = tikoo) { int res = 1; a %= m; while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; } void go() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } signed main() { go(); int t = 1; // cin >> t; while (t--) { take(n); map<int, int> a; for (int i = 0; i < n; i++) { take(x); a[x]++; } vector<int> b(n); for (int i = 0; i < n; i++) { cin >> b[i]; } int ans = 0; for (int i = 0; i < n; i++) { take(x); x--; ans += a[b[x]]; } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> #define rng(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define per(i, b) gnr(i, 0, b) #define all(x) x.begin(), x.end() using namespace std; //using namespace atcoder; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> a(n), b(n), c(n); rep(i, n) cin >> a.at(i); rep(i, n) cin >> b.at(i); rep(i, n) cin >> c.at(i); vector<int> bc(n, -1); rep(i, n) { bc.at(i) = b.at(c.at(i) - 1); } vector<int> cnt(n, 0); rep(i, n) { cnt.at(bc.at(i) - 1)++; } ll res = 0; rep(i, n) { res += cnt.at(a.at(i) - 1); } cout << res << endl; return 0; }
// Mere bas ka nahi hai // still i'm trying // apna time aayega // // .--------------. // | Try First One| // '--------------' // | .--------------. // | | | // V V | // .--------------. | // | AC. |<---. | // '--------------' | | // (True)| |(False) | | // .--------' | | | // | V | | // | .--------------. | | // | | Try Again |----' | // | '--------------' | // | | // | .--------------. | // '->| Try Next One |-------' // '--------------' // #include<bits/stdc++.h> using namespace std; //#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 int long long int #define ld long double #define ull unsigned long long #define pb push_back #define mp make_pair #define ff first #define ss second #define r0 return 0; #define for0(i, n) for (int i = 0; i < n; i++) #define for1(i, n) for (int i = 1; i <= n; i++) #define loop(i,a,b) for (int i = a; i < b; i++) #define vi vector<int> #define vii vector<long long int> #define SORT(v) sort(v.begin(),v.end()); #define PI 3.1415926535897932384626433832795 #define mod 1000000007 #define md 998244353 #define inf 1000000000000000 //1e15 #define gcd __gcd #define lcm(a,b) ((a)*(b))/gcd(a,b) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define crap ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define endl "\n" int min(int a,int b){ return (a<b)?a:b; } int max(int a,int b){ return (a>b)?a:b; } int fp(int a,int b){ if(b==0) return 1; int x=fp(a,b/2); x=(x*x)%mod; if(b&1) x=(x*a)%mod; return x; } int factorial(int n) { int fact=1; for(int i=2;i<=n;i++) fact=fact*i; return fact; } int ncr(int n,int r) { return factorial(n)/(factorial(r)*factorial(n-r)); } int binomialCoeff(int n,int k) { int dp[k+1]; memset(dp,0,sizeof(dp)); dp[0]=1; for(int i=1;i<=n;i++) { for(int j=min(i,k);j>0;j--) dp[j]=(dp[j]+dp[j-1])%mod; } return dp[k]%mod; } void c_p_c() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } void solve() { int a,b; cin >> a >> b; cout << (a * b * 1.0) / (100 * 1.0) << endl; } signed main() { crap; int t=1;; //cin>>t; for(int o=1;o<=t;o++) { solve(); } }
#include<bits/stdc++.h> using namespace std; #define int long long int #define double long double #define endl '\n' #define pb push_back #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define umii unordered_map<int,int> #define si set<int> #define usi unordered_set<int> #define all(x) x.begin(),x.end() #define setbits(x) __builtin_popcountll(x) #define dig(x,y) fixed<<setprecision(y)<<x #define trace(x) cerr<<#x<<": "<<x<<" "<<endl; #define fio ios_base::sync_with_stdio(false);cin.tie(NULL); const int mod = 1e9+7; const int inf = 1e18; int lcm(int a, int b) { return a * (b / __gcd(a, b)); } int power(int a, int b) { int ans=1; while(b) { if(b & 1) { ans = ans * a; } b = b >> 1; a = a * a; } return ans; } void solve() { int a,b; cin>>a>>b; double ans = a*b*1.0 / 100; cout<<ans; } signed main() { fio int t=1; //cin>>t; for(int i=1;i<=t;i++) { //cout<<"Case #"<<i<<": "; solve(); } return 0; }
#include <bits/stdc++.h> #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(),(x).end() #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second using namespace std; typedef double DB; typedef long double LD; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VPII; // head const int N=1e3+5; VPII g[N]; int n,m; bool st[N][N]; struct R { int u,v,d; R() {} R(int u,int v,int d):u(u),v(v),d(d) {} bool operator < (const R &T) const { return d>T.d; } }; int bfs() { priority_queue<R> q; q.emplace(1,n,0); while(SZ(q)) { int u=q.top().u,v=q.top().v,d=q.top().d; q.pop(); if(u==v) return d; if(st[u][v]) continue; st[u][v]=true; for(auto x:g[u]) if(x.FI==v) return d+1; for(auto x:g[u]) for(auto y:g[v]) if(!st[x.FI][y.FI]&&x.SE==y.SE) { if(x.FI==y.FI) return d+2; q.emplace(x.FI,y.FI,d+2); } } return -1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>m; for(int i=1;i<=m;i++) { int u,v,w; char c; cin>>u>>v>>c; w=c-'a'; g[u].EB(v,w); if(u!=v) g[v].EB(u,w); } cout<<bfs()<<'\n'; return 0; }
#include<bits/stdc++.h> //#include <atcoder/all> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i,n)for(int i=0;(i)<(int)(n);i++) #define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++) #define ALL(a) (a).begin(),(a).end() #define pb push_back #define fi first #define se second #define sz(x) ((int)x.size()) using namespace std; //using namespace atcoder; using ld = long double; using ll = long long; using P = pair<ll, ll>; template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; } template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; } const ll MOD = 1e9 + 7; const ll INF = 1e16; struct Edge{ int to; char col; }; int main(){ int n, m; cin >> n >> m; vector<vector<Edge>> G(n); vector<int> a(m), b(m); rep(i, m){ char c; cin >> a[i] >> b[i] >> c; a[i]--;b[i]--; G[a[i]].pb(Edge{b[i], c}); G[b[i]].pb(Edge{a[i], c}); } vector<vector<int>> nG(n * n); //グラフを再構築する auto push = [&](int ua, int ub, int va, int vb){ nG[ua * n + ub].pb(va * n + vb); }; rep(a, n){ rep(b, n){ for(auto ea : G[a]){ for(auto eb : G[b]){ if(ea.col == eb.col)push(a, b, ea.to, eb.to); } } } } vector<ll> d(n * n, INF); priority_queue<P, vector<P>, greater<P>> pq; d[n - 1] = 0; pq.push(P(0, n - 1)); while(sz(pq)){ auto [cost, v] = pq.top(); pq.pop(); if(d[v] < cost)continue; for(auto nv : nG[v]){ if(d[nv] > d[v] + 1){ d[nv] = d[v] + 1; // cout << nv << " " << d[nv] << endl; pq.push(P(d[nv], nv)); } } } ll res = INF; // rep(i, n)cout << d[i * n + i] << endl; rep(i, n)chmin(res, 2 * d[i * n + i]); rep(i, m){ chmin(res, 2 * d[a[i] * n + b[i]] + 1); chmin(res, 2 * d[b[i] * n + a[i]] + 1); } if(res == INF)res = -1; cout << res << endl; }
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int, int>>> graph; // {nxt, eid} vector<pair<int, int>> elist; // edges, in orientation in input vector<bool> marked, exited; vector<bool> toDirectForward; void direct(int eid, int i, int j) { // printf("directing %d %d->%d\n", eid, i, j); toDirectForward[eid] = (elist[eid].first == i); } void directForSCC(int v, int p, vector<bool> &pwede) { if (!pwede[v] || marked[v]) return; marked[v] = true; // printf("submarking %d %d\n", v, p); for (auto [nxt, eid] : graph[v]) { if (!pwede[nxt]) continue; if (marked[nxt]) { if (nxt != p && !exited[nxt]) direct(eid, v, nxt); } else { direct(eid, v, nxt); directForSCC(nxt, v, pwede); } } exited[v] = true; } int main() { int n, m; scanf("%d %d", &n, &m); graph.assign(n, vector<pair<int, int>>()); marked.assign(n, false); exited.assign(n, false); toDirectForward.assign(m, false); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); elist.emplace_back(--a, --b); graph[a].emplace_back(b, i); graph[b].emplace_back(a, i); } vector<int> c(n); for (int i = 0; i < n; ++i) scanf("%d", &c[i]); // while i have edges i haven't figured out: while (!all_of(begin(marked), end(marked), [&] (bool b) {return b;})) { // printf("!NEW SET\n"); // consider the set of vertices with the lowest c_i. int min_ci = n + 1; for (int i = 0; i < n; ++i) if (!marked[i]) min_ci = min(min_ci, c[i]); vector<int> toCheck; vector<bool> canVisit(n, false); for (int i = 0; i < n; ++i) if (!marked[i] && c[i] == min_ci) { toCheck.push_back(i); canVisit[i] = true; } // consider the connected components this set defines in G // for each CC: // direct all vertices internally to create an SCC // for (u, v) such that u hasn't been marked, and v is in this CC, u->v // // mark all vertices in the CC for (int v : toCheck) directForSCC(v, -1, canVisit); for (int i = 0; i < m; ++i) { auto [u, v] = elist[i]; if (!marked[u] && canVisit[v]) direct(i, u, v); if (!marked[v] && canVisit[u]) direct(i, v, u); } } for (int i = 0; i < m; ++i) { if (toDirectForward[i]) printf("->\n"); else printf("<-\n"); } }
#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; } struct edge{ int to, id; bool rev; edge(){} edge(int a, int b, bool c) : to(a), id(b), rev(c){} }; vec a(10000), b(10000), c(100); bitset<10000> res(0), used(0); vector<vector<edge> > G(100); void dfs(int v){ for(edge &e : G[v]){ if(!used[e.id]){ used[e.id] = true; res[e.id] = e.rev; dfs(e.to); } } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin>>N>>M; rep(i, M) { cin>>a[i]>>b[i]; --a[i]; --b[i]; G[a[i]].emplace_back(b[i], i, false); G[b[i]].emplace_back(a[i], i, true); } rep(i, N) cin>>c[i]; rep(i, M) { if(c[a[i]] != c[b[i]]){ used[i] = true; res[i] = (c[a[i]] < c[b[i]]); } } rep(i, N) dfs(i); rep(i, M) cout<<(res[i] ? "<-" : "->")<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef pair<int,int> P; typedef tuple<int,int,int> tpl; #define ALL(a) (a).begin(),(a).end() #define SORT(c) sort((c).begin(),(c).end()) #define REVERSE(c) reverse((c).begin(),(c).end()) #define EXIST(m,v) (m).find((v)) != (m).end() #define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i) #define RREP(i,n) RFOR(i,n,0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL<<60; constexpr long long MOD = 998244353; 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; } 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 { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(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(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } }; void Main(){ ll N,K; cin >> N >> K; VL A(N); REP(i,N) cin >> A[i]; mint kai[K+1]; kai[0]=1; REP(i,K) kai[i+1] = kai[i]*(i+1); mint p[N], sum[K+1]; REP(i,N) p[i] = 1; sum[0] = N; REP(i,K){ REP(j,N){ p[j] *= A[j]; sum[i+1] += p[j]; } sum[i+1] /= kai[i+1]; } mint same_pair[K+1]; same_pair[0] = N; REP(i,N) p[i] = 1; REP(i,K)REP(j,N){ p[j] *= A[j]+A[j]; same_pair[i+1] += p[j]; } FOR(X,1,K+1){ mint ans(0); REP(k,X+1) ans += sum[X-k] * sum[k]; ans *= kai[X]; ans = (ans - same_pair[X]) / 2; cout << ans.x << en; } return; } int main(void){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15); int t=1; //cin>>t; REP(_,t) Main(); return 0; }
#include <bits/stdc++.h> using namespace std; using Pair = pair<int, int>; using Queue = queue<int>; using Graph = vector<vector<int>>; int main() { int N, M; cin >> N >> M; int M2 = M + 1; vector<Pair> XY(M2); map<Pair, int> idx_map; map<int, vector<int>> YX; XY.at(0) = {0, N}; idx_map[{0, N}] = 0; YX[N].push_back(0); for (int i = 1; i < M2; ++i) { auto &[Xi, Yi] = XY.at(i); cin >> Xi >> Yi; idx_map[{Xi, Yi}] = i; YX[Yi].push_back(Xi); } for (auto &[Yi, X] : YX) sort(X.begin(), X.end()); Graph graph(M2); for (int i = 0; i < M2; ++i) { auto [Xi, Yi] = XY.at(i); auto ub = 2 * N + 1; auto &vec1 = YX[Yi]; auto it1 = upper_bound(vec1.begin(), vec1.end(), Xi); if (it1 != vec1.end()) ub = *it1; int y2 = Yi - 1, y3 = Yi + 1; if (YX.count(y2)) { auto &vec2 = YX.at(y2); auto it2 = upper_bound(vec2.begin(), vec2.end(), Xi); for ( ; (it2 != vec2.end()) && (*it2 <= ub); ++it2) graph.at(i).push_back(idx_map.at({*it2, y2})); } if (YX.count(y3)) { auto &vec3 = YX.at(y3); auto it3 = upper_bound(vec3.begin(), vec3.end(), Xi); for ( ; (it3 != vec3.end()) && (*it3 <= ub); ++it3) graph.at(i).push_back(idx_map.at({*it3, y3})); } } Queue que; vector<bool> seen(M2, false); que.push(0); seen.at(0) = true; assert(idx_map.at({0, N}) == 0); while (!que.empty()) { auto u = que.front(); que.pop(); for (auto v : graph.at(u)) { if (seen.at(v)) continue; seen.at(v) = true; que.push(v); } } int cnt = 0; for (auto &[Yi, X] : YX) { auto idx = idx_map.at({X.back(), Yi}); if (seen.at(idx)) ++cnt; } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define pb emplace_back #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; typedef multiset<ll> S; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; 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;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void noyes(T b){if(b)out("no");else out("yes");} template<class T> void NoYes(T b){if(b)out("No");else out("Yes");} template<class T> void NOYES(T b){if(b)out("NO");else out("YES");} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} ll modcom(ll a,ll b){ if(a<b)return 0; ll res=1; rep(i,b){ res*=modpow(i+1,mod-2); res%=mod; res*=a-i; res%=mod; } return res; } int main(){ ll n,m;cin>>n>>m; ll sum=0; rep(i,n){ ll a;cin>>a;sum+=a; } out(modcom(m+n,sum+n)); }
#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; void solve(){ int n; cin>>n; vector<ll>a(n); rep(i,n){cin>>a[i];} sort(a.begin(),a.end()); ll sum=0,ans=0; for(int i=n-1;i>=0;i--){ (ans+=sum*a[i]+a[i]*a[i])%=mod; (sum=sum*2+a[i])%=mod; } cout<<ans<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <iostream> #include <vector> #include <string> #include <unordered_map> #include <bitset> #include <map> using namespace std; typedef long long ll; struct UnionFind { vector<int> par, siz; UnionFind(int n) : par(n, -1), siz(n, 1){} // 根を求める int root(int x) { if (par[x] == -1) return x; else return par[x] = root(par[x]); } // xとyが同じグループか判定する bool isSame(int x, int y) { return root(x) == root(y); } // xを含むグループとyを含むグループを併合する bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); par[y] = x; siz[x] += siz[y]; return true; } // xを含むグループのサイズ int size(int x) { return siz[root(x)]; } }; int main(void) { int n; int M = 200005; cin >> n; vector<int> a(n, 0); for (int i = 0; i < n; i++) cin >> a[i]; UnionFind uf(M); int l = 0; int r = n - 1; while(l < r){ if (a[l] != a[r]) uf.unite(a[l], a[r]); l++; r--; } int ans = 0; for (int i = 0; i < M; i++){ if (i == uf.root(i)) ans += uf.size(i) - 1; } cout << ans << endl; }
#include<bits/stdc++.h> #define LL long long #define DB double #define pb push_back #define mkp make_pair #define LB lower_bound #define UB upper_bound #define rint register int #define MS(x,y) memset(x,y,sizeof(x)) #define rep(i,a,b) for(rint i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(rint i=a,i##end=b;i>=i##end;--i) #define cms printf("%.2lf\n",(&o2-&o1)/1024.0/1024) using namespace std;bool o1; char IO; inline int Rd(){ int _s_=0; bool _f_=0; while(!isdigit(IO=getchar())) IO=='-'&&(_f_=1); do _s_=(_s_<<1)+(_s_<<3)+(IO^48); while(isdigit(IO=getchar())); return _f_?-_s_:_s_; } struct node{ int A,B; bool operator < (const node &_) const{ if(A==_.A) return B<_.B; return A<_.A; } }_b[400005]; int n,c[500005],a,b; char d[500006]; bool o2;int main(){ n=Rd(); rep(i,1,2*n) _b[i]=(node){Rd(),i}; sort(_b+1,_b+1+2*n); rep(i,1,n) c[_b[i].B]=1; a=0,b=0; rep(i,1,2*n){ if(c[i]){ if(!a&&b){ d[i]=')'; b--; } else { d[i]='('; if(!a){ a=1; b=0; } b++; } } else { if(a&&b){ d[i]=')'; b--; } else { d[i]='('; if(a){ a=0; b=0; } b++; } } putchar(d[i]); } cout<<d; return 0; }
#include "bits/stdc++.h" using namespace std; #define int long long #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define all(x) (x).begin(),(x).end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define sz(x) (int)((x).size()) #define fr first #define sc second #define pii pair<int,int> #define rep(i,a,b) for(int i=a;i<b;i++) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define ppc __builtin_popcount #define ppcll __builtin_popcountll #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;} template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;} const long long INF=1e18; const int32_t M=1e9+7; const int32_t MM=998244353; const int N=100005; map<pii,int> edgeid; int edgelabel[N*2]; set<int> adj[N]; bool vis[N]; int vertexlabel[N]; void dfs(int v,int p=0){ vis[v] = 1; if(!p||vertexlabel[p]==edgelabel[edgeid[{v,p}]]){ vertexlabel[v] = 1; if(p&&vertexlabel[p]==1){ vertexlabel[v]++; } }else vertexlabel[v] = edgelabel[edgeid[{v,p}]]; for(auto u:adj[v]){ if(vis[u])continue; dfs(u,v); } } void solve(){ int n,m; cin>>n>>m; for (int i = 0; i < m; ++i) { int u,v,c; cin>>u>>v>>c; adj[u].insert(v); adj[v].insert(u); edgeid[{u,v}]=i+1; edgeid[{v,u}]=i+1; edgelabel[i+1]=c; } dfs(1); for (int i = 0; i < n; ++i) { cout<<vertexlabel[i+1]<<"\n"; } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t=1; //cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for(int (i)=0; (i)< (n); ++i) #define REPR(i, n) for(int (i)=(n); (i)>=0; --i) #define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i) constexpr int INF = 1e9; //constexpr ll INF = 1LL << 61; constexpr int mod = 1e9+7; struct Edge{ int x, y, d; Edge(){} Edge(int x, int y, int d): x(x), y(y), d(d){} bool operator< (const Edge l)const{ return d < l.d; } }; struct UnionFind{ vector<int> node; UnionFind(int n){ node = vector<int>(n, -1); } int find(int v){ if(node[v] < 0){ return v; } return node[v] = find(node[v]); } bool merge(int v, int u){ int rootv = find(v); int rootu = find(u); if(rootv == rootu) return false; if(node[rootv] < node[rootu]){ swap(rootv, rootu); } node[rootv] += node[rootu]; node[rootu] = rootv; return true; } int size(int v){ return -node[v]; } }; vector<int> ans; void dfs(int v, vector<vector<pair<int, int>>> &g, int root){ for(auto edge : g[v]){ int nv = edge.first; int cost = edge.second; if(nv == root) continue; assert(nv != v); if(ans[v] == cost){ if(cost ==0){ ans[nv]= 1; } else{ ans[nv] = 0; } } else{ ans[nv] = cost; } dfs(nv, g, v); } } int main(){ int N, M; cin >> N >> M; vector<Edge> edges(M); REP(i, M){ int u, v, c; cin >> u >> v >> c; u--, v--, c--; edges[i] = Edge(u, v, c); } sort(edges.begin(), edges.end()); vector<vector<pair<int, int>>> g(N); UnionFind uf(N); REP(i, M){ int u, v; u = edges[i].x, v = edges[i].y; if(uf.merge(u, v)){ g[v].push_back({u, edges[i].d}); g[u].push_back({v, edges[i].d}); } } ans.resize(N); ans[0] = 0; dfs(0, g, -1); REP(i, N){ cout << ans[i]+1 << endl; } return 0; }
#define ll long long #define lld double #define M 202 #define f first #define s second #define pi 3.141592653589793238 #define vl vector<ll> #define pll pair<ll,ll> #define vpll vector<pll> #define N 100002 #define Endl endl #define maxx(a,b,c,d) max(max(a,b),max(d,c)) #define minn(a,b,c,d) min(min(a,b),min(d,c)) #define pb push_back #define mod 1000000007 #define pp 31 #define ss(v) sort(v.begin(),v.end()) #define sr(v) sort(v.begin(),v.end(),greater<pll>()) #define rin(A, B, C) (((A)<=(B))&&((B)<=(C))) #include <bits/stdc++.h> using namespace std; ll power(ll a, ll b) { if(!b)return 1LL; ll aa=power(a,b/2); if(b%2)return aa*aa%mod*a%mod; return aa*aa%mod; } ll im(ll a) { return power(a,mod-2); } // ll ncr(ll n,ll r) // { // return f[n]*im(f[r])%mod*im(f[n-r])%mod; // } bool check(ll n) { ll m=n; while(n) { if(n%10!=0 && m%(n%10)!=0) { return false; } n/=10; } return true; } pll ol(ll a, ll b, ll c, ll d) { a=max(a,c); b=min(b,d); return {a,b}; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("output.txt", "w", stdout); // freopen("input.txt", "r", stdin); ll a,b; cin>>a>>b; ll ans1=0,ans2=0; while(a) { ans1+=a%10; a/=10; } while(b) { ans2+=b%10; b/=10; } cout<<max(ans1,ans2)<<endl; return 0; }
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> #define M 100001 typedef long long ll; typedef unsigned long long ull; inline void read(ull &T) { ull x = 0; bool f = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = !f; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } T = f ? -x : x; } ull n, l, r; ull s(ull x) { return (1 + x) * x / 2; } int main() { read(n); l = 1, r = 6e9; while (l <= r) { ull mid = (l + r) >> 1; if (s(mid) > n + 1) r = mid - 1; else l = mid + 1; } std::cout << n - r + 1 << '\n'; return 0; }
/* In the name of Allah, Most Gracious, Most Merciful */ #include<bits/stdc++.h> #include<numeric> using namespace std; //*** constant value *** const long double PI = 3.141592653589793238; const double EPS = 1e-6 ; //*** constant value *** //****** define ******** #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long #define dd double #define vll vector<ll> #define vs vector<string> #define fn(i,n) for(ll i = 0; i<n; i++) #define f(i,a,b) for(i=a;i<b;i++) #define f1(i,a,b) for(i=b-1;i>=a;i--) #define pii pair<int,int> #define pll pair<ll,ll> #define pls pair<ll,string> #define vpl vector<pll> #define pb push_back #define mk make_pair #define x first #define y second #define tt cout<<"Came here"<<endl; //****** define ******** void solve() { ll a[4]; cin>>a[0]>>a[1]>>a[2]>>a[3]; sort(a,a+4); cout<<a[0]<<endl; } int main() { IOS; ll tc = 1; while(tc--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int a[4]; cin>>a[0]>>a[1]>>a[2]>>a[3]; cout<<*min_element(a, a+4); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; n += 99; cout << n / 100 << endl; return 0; }
#include<bits/stdc++.h> typedef long long ll; using namespace std; bool f2; char IO; int rd(){ int num=0;bool f=0; while(IO=getchar(),IO<48||IO>57)if(IO=='-')f=1; do num=(num<<1)+(num<<3)+(IO^48); while(IO=getchar(),IO>=48&&IO<=57); return f?-num:num; } bool f1; int main(){ // cout<<(&f1-&f2)/1024.0/1024.0<<endl; printf("%d",(rd()-1)/100+1); return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<iomanip> using namespace std; typedef long long ll; //e_gcd()自体はaとbの最大公約数を返す。 //xとyは、a*x+b*y=gcd(a,b)となる(x,y)の1つの組を返す。 //a*x+b*y=gcdの時、a=b*p+(a%b)を代入して、b*p*x+(a%b)*x+b*y=b*(p*x+y)+(a%b)*x //次のx->p*x+y //次のy->x //次のa->b //次のb->a%b ll e_gcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return a; } ll gcd_res = e_gcd(b, a % b, x, y); ll p = a / b; ll nx = x, ny = y; x = ny; y = nx - p * x; return gcd_res; } ll lcm(ll a, ll b) { ll t1, t2; ll gcd = e_gcd(a, b, t1, t2); return a * b / gcd; } ll T; int main() { cin >> T; while (T--) { ll X, Y, P, Q; cin >> X >> Y >> P >> Q; ll ans = 4 * 1000000000000000000LL;//INFの値はこうする。 for (ll A = X; A < X + Y; A++) { for (ll B = P; B < P + Q; B++) { ll p, q; //p * (2X + 2Y) - q * (P + Q) = gcd(2X + 2Y, P + Q)となる(p, q)を求める。 ll gcd = e_gcd(2 * (X + Y), (P + Q), p, q); if ((B - A) % gcd != 0)continue;//このB - Aでは解がない場合 //(p, q)はただの解のセットで、別にpが正などと約束されてるわけではない。 //よって、まずは適切にあまりを取って0に近づけて、そこから必要な符号に直す。 //gcd倍の係数をかける。 p *= (B - A) / gcd; p %= (P + Q) / gcd; if (p < 0)p += (P + Q) / gcd; ll t = A + p * 2 * (X + Y); ans = min(ans, t); } } if (ans == 4 * 1000000000000000000LL) cout << "infinity" << endl; else cout << ans << endl; } return 0; }
/* -*- coding: utf-8 -*- * * a.cc: A - Fourtune Cookies */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int N = 4; const int NBITS = 1 << N; /* typedef */ /* global variables */ int as[N]; /* subroutines */ /* main */ int main() { int sum = 0; for (int i = 0; i < N; i++) { scanf("%d", as + i); sum += as[i]; } for (int bits = 0; bits < NBITS; bits++) { int bs = 0; for (int i = 0, bi = 1; i < N; i++, bi <<= 1) if (bits & bi) bs += as[i]; if (bs * 2 == sum) { puts("Yes"); return 0; } } puts("No"); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n);i++) #define Graph vector<vector<int>>; #define iterG(next_v, G, v) for(auto next_v : G[v] #define ALL(a) (a).begin(),(a).end() const int MOD = 1000000007; const int MAX = 510000; const int INF = 1061109567; const double EPS = 1e-10; const double PI = acos(-1.0); int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<string> s(n); rep(i, n) cin>> s[i]; vector<int> v(n); ll odd= 0; ll even=0; rep(i, n){ int x=0; rep(j, m){ if(s[i][j] == '1'){ x++; } } x = x % 2; if(x ==0){ even++; }else{odd++;} } cout << even * odd << endl; return 0; }
#include <limits.h> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <map> #include <cmath> #include <iomanip> #include <string> #include <deque> #include <assert.h> #include <random> #include <ctime> #include <unordered_map> #define pnode node* typedef long long ll; typedef long double ld; using namespace std; const ll mod = 998244353; ll pw(ll a, ll b) { if (b == 0) { return 1; } ll b2 = b / 2; ll tmp = pw(a, b2); if (b % 2 == 0) { return (tmp * tmp) % mod; } else { return (((tmp * tmp) % mod) * a) % mod; } } ll sub(ll a, ll b) { return (a - b + mod) % mod; } ll add(ll a, ll b) { return (a + b) % mod; } ll mul(ll a, ll b) { return (a * b) % mod; } ll n, m; ll cnt[5100][5100]; ll sm[5100][5100]; void solve(){ cin >> n >> m; // cout << n << " " << m << endl; for (int i = 1; i <= 5000; ++i) { cnt[i][0] = 1; } for (int i = 1; i <= 5000; ++i) { for (int j = 1; j <= 5000; ++j) { if (j == 1) { cnt[i][j] = i; } else { cnt[i][j] = (cnt[i][j - 1] * i) % mod; } } } for (int i = 1; i <= 5000; ++i) { for (int j = 1; j <= 5000; ++j) { sm[i][j] = cnt[i][j]; sm[i][j] += sm[i][j - 1]; sm[i][j] %= mod; } } ll ans = 0; ll atleastonce = sub(pw(m, n), pw(m - 1, n)); // cout << atleastonce << "\n"; ans += atleastonce * m; // cout << ans << "\n"; ans %= mod; for (int a = 1; a <= m; ++a) { for (int len = 1; len <= n; ++len) { ll minpos = 1; ll maxpos = n - len - 1; if (maxpos < minpos) { continue; } ans += mul(mul(cnt[m][n - len - 2], sub(cnt[m - 1][len], cnt[m - a][len])), (maxpos - minpos + 1)); ans %= mod; } } cout << ans << "\n"; } signed main(){ ll t = 1; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed; cout << setprecision(20); #ifdef DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else #endif while(t--){ solve(); } }
/* author: madhav_1999 aka orthodoxparadox 18 April 2021 at 5:31 PM */ #include <bits/stdc++.h> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define dbl long double #define int ll #define ll long long #define pii pair<int, int> #define len(x) (int) x.size() #define rev(a) reverse(all(a)) #define oiint ostream_iterator<int>(cout, " ") #define pb push_back #define mp make_pair #define ff first #define ss second #define endl '\n' #define all(a) a.begin(), a.end() #define initialise(a, x) memset(a, x, sizeof(a)) #define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector #define inf 2e18 #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #ifdef MADHAV #define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;} template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " "; __f(comma + 1, args...);} #else #define dbg(...) 42 #endif template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<(int)v.size();++i) x<<v[i]<<" ";return x;} template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;} template <typename X> ostream& operator << (ostream& x,const multiset<X>& v){for(auto it:v) x<<it<<" ";return x;} template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;} template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; } typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}}; const int MOD = 1e9 + 7; int takemod(int a, int mod = MOD){a%=mod;if(a<0)a+=mod;return a;} int fast_exp(int base, int expo, int mod = MOD) { int res=1; while(expo>0) { if(expo&1) res=(res*base)%mod; base=(base*base)%mod; expo>>=1;} return res;} int modinv(int a, int mod = MOD){return takemod(fast_exp(takemod(a, mod), mod-2, mod), mod);} void solve() { int a, b; cin >> a >> b; vector<int> ans; if(a > b) { int sum = 0; for(int i = 1; i <= a; i++) { ans.push_back(i); sum += i; } for(int i = 1; i <= b - 1; i++) { ans.push_back(-i); sum -= i; } ans.push_back(-sum); } else { int sum = 0; for(int i = 1; i <= b; i++) { ans.push_back(-i); sum += i; } for(int i = 1; i <= a - 1; i++) { ans.push_back(i); sum -= i; } ans.push_back(sum); } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifdef MADHAV freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif clock_t t1, t2; t1 = clock(); int tt = 1; // cin >> tt; while(tt--) solve(); #ifdef MADHAV t2 = clock(); cout << "\n\ntime taken: " << (t2-t1)/(dbl)CLOCKS_PER_SEC; #endif return 0; }
#include <bits/stdc++.h> using namespace std; struct ios { inline char read() { static const int IN_LEN = 1e6 + 10; static char buf[IN_LEN], *s, *t; return (s == t) && (t = (s = buf) + fread(buf, 1, IN_LEN, stdin)), s == t ? -1 : *s++; } template <typename _Tp> inline ios &operator>>(_Tp &x) { static char c11, boo; for (c11 = read(), boo = 0; !isdigit(c11); c11 = read()) { if (c11 == -1) return *this; boo |= c11 == '-'; } for (x = 0; isdigit(c11); c11 = read()) x = x * 10 + (c11 ^ '0'); boo && (x = -x); return *this; } } io; namespace IO { template <typename T> inline void w(T x) { if (x > 9) w(x / 10); putchar(x % 10 + 48); } template <typename T> inline void write(T x, char c) { if (x < 0) putchar('-'), x = -x; w(x); putchar(c); } template <typename T> inline void read(T &x) { x = 0; T f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } } // namespace IO #define int long long #define x first #define y second #define PII pair<int,int> #define LL long long #define pb push_back #define pqp priority_queue<PII,vector<PII>,greater<PII>> #define pqi priority_queue<int,vector<int>,greater<int>> #define PSS pair<string,string> #define in insert #define line inline // #define sort(s) sort(s.begin(),s.end()); #define reverse(s) reverse(s.begin(),s.end()); #define max_(s) *max_element(s.begin(), s.end()) #define V vector<int> #define VV vector<V> #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int dx[]= {0,-1,0,1,0,0},dy[]= {-1,0,1,0,0,0},dz[] = {0,0,0,0,-1,1}; string s; int m; bool check(int x) { __int128 sum=0; for(__int128 i = s.size() - 1,j = 1; i >= 0; i--,j *= x) { sum = sum + (s[i]-'0') * j; if(sum > m || j > m) return 0; } return 1; } void solve() { cin >> s; cin >> m; int t = 0; for(auto x : s) t = max(t,(int)(x - '0' + 1)); int k = t - 1; if(s.size() == 1){ k = s[0] - '0'; if(k <= m) puts("1"); else puts("0"); } else{ int l = t, r = m; int res; while(l <= r){ int mid = l + r >> 1; if(check(mid)) l = mid + 1,res = mid; else r = mid - 1; } cout << max(0ll,res - k); } } signed main() { // IOS // int _; cin >> _; while(_ --) solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long Int; typedef pair<int,int> PII; typedef vector<int> VInt; #define FOR(i, a, b) for(i = (a); i < (b); ++i) #define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i) #define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it) #define CLEAR(a, b) memset(a, b, sizeof(a)) #define SIZE(a) int((a).size()) #define ALL(a) (a).begin(),(a).end() #define MP make_pair void solve(int test) { int n; cin >> n; int i; VInt a(n), b(n); FOR(i, 0, n) cin >> a[i]; FOR(i, 0, n) cin >> b[i]; reverse(ALL(a)); priority_queue<int> q; FOR(i, 0, n) { q.push(-a[i]); q.push(-b[i]); q.pop(); } Int res = 0; FOR(i, 0, n) { res -= q.top(); q.pop(); } cout << res << endl; } int main() { int T = 1, t; //cin >> T; FOR(t, 0, T) { //cerr << "Solving " << t << "/" << T << endl; solve(t); } return 0; }
// I've done my sentence but committed no crime #pragma GCC optimize("Ofast") #define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" using namespace std; #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define mod (int)(1e9+7) #define mod2 998244353 #define flush fflush(stdout); #define PI 3.1415926535897932384626433832795 void solve() { int n; cin>>n; int ans=0; for(int i=1;i<n;i++){ if(i>0&&(n-i)>0)ans++; } cout<<ans<<'\n'; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1e17; ll N, M; vector<ll> W; vector<pair<ll,ll>> A; //A[i] := first以下の耐荷重を持つものの最大の長さ // #include"debug.hpp" ll calc(ll weight) { auto girigiri = upper_bound(A.begin(), A.end(), pair<ll,ll>{weight, -INF}); if(girigiri == A.begin()) { return 0; } girigiri--; if(girigiri -> first > weight) { return INF; } return girigiri -> second; } int main() { cin >> N >> M; W.resize(N), A.resize(M); for(ll i = 0; i < N; i++)cin >> W[i]; for(ll i = 0; i < M; i++)cin >> A[i].second >> A[i].first; sort(W.begin(), W.end()); sort(A.begin(), A.end()); // /* { ll maxweight = W.back(); for(int i = 0; i < M; i++) { if(A[i].first < maxweight) { cout << -1 << endl; return 0; } } } // */ for(ll i = 1; i < M; i++) { A[i].second = max(A[i].second, A[i - 1].second); } // print(A); ll answer = 1e17; do { vector<vector<ll>> hen(N); for(ll i = 0; i < N; i++) { for(ll j = i + 1; j < N; j++) { ll sum = 0; for(ll k = i; k <= j; k++)sum += W[k]; // print(sum); ll len = calc(sum); //重さsumを乗せれる最小の長さ hen[i].push_back(len); } } vector<ll> dist(N); for(ll i = 0; i < N; i++) { for(ll j = i + 1; j < N; j++) { dist[j] = max(dist[j], dist[i] + hen[i][j - i - 1]); } } answer = min(answer, dist[N - 1]); } while(next_permutation(W.begin(), W.end())); if(answer == INF) { cout << -1 << endl; return 0; } cout << answer << endl; }
#ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include<bits/stdc++.h> using namespace std; #define rep(i,s,t) for(ll i = (ll)(s); i < (ll)(t); i++) #define rrep(i,s,t) for(ll i = (ll)(s-1);(ll)(t) <= i; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<ll,ll> Pll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; constexpr ll INF = numeric_limits<ll>::max()/4; constexpr ll n_max = 2e5+10; #define int ll const long double pi = 3.14159265358979323846; template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) {return '"' + s + '"';} string to_string(const char *c) {return to_string((string) c);} string to_string(bool b) {return (b ? "true" : "false");} template <size_t N> string to_string(bitset<N> v){ string res = ""; for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for(const auto &x : v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";} void debug_out() {cerr << endl;} template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template<class T> bool chmax(T &a, T b){if(a < b){a = b; return true;} return false;} template<class T> bool chmin(T &a, T b){if(a > b){a = b; return true;} return false;} template <typename T> struct UnionFind { private: vector<T> par; vector<T> rank; vector<T> sz; vector<T> a, b; int n; public: // n要素で親を初期化、par[x]はxの親を表す UnionFind(int n) : n(n) { par.resize(n, 0); rank.resize(n, 0); sz.resize(n, 1); a.resize(n); b.resize(n); for (int i = 0; i < n; i++) { par[i] = i; } } void init(vector<ll> &A, vector<ll> &B){ rep(i,0,n){ a[i] = A[i]; b[i] = B[i]; } } //木の根を求める int root(int x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } // xとyの属する集合を併合 void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { swap(x, y); } par[y] = x; sz[x] += sz[y]; a[x] += a[y]; b[x] += b[y]; if (rank[x] == rank[y]) rank[x]++; } // xとyが同じ集合に属するか否か bool issame(int x, int y) { return root(x) == root(y); } // xが属する集合のサイズを返す int size(int x) { return sz[root(x)]; } // 集合の数を返す int num_of_s() { vector<int> cnt(n); int ans = 0; for (int i = 0; i < n; i++) { if (!cnt[root(i)]) ans++, cnt[root(i)] = 1; } return ans; } T get_a(int k){ return a[root(k)]; } T get_b(int k){ return b[root(k)]; } }; void YES(bool ok){ cout << (ok ? "Yes" : "No") << endl; } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n,m; cin >> n >> m; vector<ll> a(n); for(int i = 0; i < n; i++) cin >> a[i]; vector<ll> b(n); for(int i = 0; i < n; i++) cin >> b[i]; UnionFind<ll> uf(n); uf.init(a, b); rep(i,0,m){ ll u,v; cin >> u >> v; u--; v--; uf.unite(u, v); } bool ok = true; rep(i,0,n){ ok &= uf.get_a(i) == uf.get_b(i); } YES(ok); }
#include <bits/stdc++.h> #define be(v) (v).begin(),(v).end() #define pb(q) push_back(q) #define rep(i, n) for(int i=0;i<n;i++) #define all(i, v) for(auto& i : v) typedef long long ll; using namespace std; const ll mod=1000000007, INF=(1LL<<60); #define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int t; cin >> t; rep(i, t){ int n; cin >> n; string a; rep(j, 3) cin >> a; string s(n, '0'); string t(n, '1'); cout << s + t + "0" << endl; } return 0; }
#include<bits/stdc++.h> #define ll long long #define fr(i,k,n) for(ll i = k;i<n;i++) using namespace std; int main(){ ll t; cin>>t; while(t--){ ll n; cin>>n; string a,b,c; cin>>a; cin>>b; cin>>c; ll cs = a.size(); cs /= 2; fr(i,0,cs){ cout<<"0"; } fr(i,0,cs){ cout<<"1"; } cout<<"0"<<"\n"; } }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) typedef long long ll; const ll INFll = 1LL << 60; int main(){ //input, initialize int N, M; cin >> N >> M; vector<vector<int>> A(N); rep(i, M){ int a, b; cin >> a >> b; a--; b--; A[a].push_back(b); } //solve ll ans = 0; rep(s, N){ vector<int> dist(N, -1); queue<int> que; dist[s] = 0; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); for(auto nv: A[v]){ if(dist[nv] == -1){ dist[nv] = dist[v] + 1; que.push(nv); } } } rep(t, N){ if(dist[t] != -1) ans++; } } //output cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define ll long long #define vi std::vector<ll> #define si set<ll> #define pb push_back vi v[2020]; bool visited[2020]; ll ans; void dfs(ll s){ if(visited[s])return; visited[s] = true; ans++; for (auto u: v[s]){ dfs(u); } return; } ll solve(){ for (int i = 0; i < 2020; ++i){ visited[i] = false; v[i].clear(); } ll n, m; cin >> n >> m; for (int i = 0; i < m; ++i){ ll a, b; cin >> a >> b; v[a].pb(b); } ll totans = 0; for (ll i = 1; i <= n; ++i){ memset(visited, false, 2020); ans = 0; dfs(i); totans += ans; } cout << totans; return 0; } int main(){ fastio; ll t= 1; // cin >> t; for (int i = 0; i < t; ++i){ // cout << "Case #" << i+1 << ": "; solve(); cout << "\n"; } return 0; }
#include<cstdio> #include<algorithm> #define N 200007 using namespace std; int n,sum,ans,ma,a[N],fa[N]; int find(int x){ if (fa[x]==x) return x; fa[x]=find(fa[x]); return fa[x]; } int main(){ scanf("%d",&n); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); sum++; ma=max(ma,a[i]); } ans=n; for (int i=1;i<=ma;i++) fa[i]=i; for (int i=1;i<=n/2;i++) if (a[i]!=a[n-i+1]){ int fx=find(a[i]),fy=find(a[n-i+1]); if (fx!=fy){ ans--; fa[fy]=fx; } } printf("%d\n",sum-ans); }
#include <bits/stdc++.h> #include <chrono> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; /*............................................................................*/ #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> /* find_by_order(x) give iterator of the index x order_by_key(x) give the position where x will be placed*/ #define int long long #define lop(i,a,b,c) for (int i=a;i<b;i+=c) #define rlop(i,a,b,c) for (int i=a-1;i>=b;i-=c) #define prii pair <int,int> #define PB push_back #define S second #define F first #define all(x) x.begin(),x.end() #define vvii vector < vector < int > > #define vii vector <int> #define count_1(x) __builtin_popcount(x) #define cn(a) scanf("%lld",&a); #define cn_vl(arr,n) lop(i,0,n,1){scanf("%lld",arr+i);} #define shw_vl(arr,n) lop(i,0,n,1){printf("%lld ",arr[i]);}printf("\n"); #define shw(CON) cout << &("NO\0YES"[3 * CON]) << '\n'; const int MAX=2e5+10; /*......................................................................*/ struct dsu { int parent[MAX];int size[MAX]; void init(){ lop (i,1,MAX,1){parent[i]=i;size[i]=1;} } int trace(int a){ return (a==parent[a])?a:parent[a]=trace(parent[a]); } bool connect(int a,int b){ if ((a=trace(a))==(b=trace(b)))return false; if (size[b]>size[a])swap(a,b); parent[b]=a;size[a]+=size[b]; return true; } }ori; vii adj[MAX]; void solve(){ int n;cn(n) vii vec;ori.init(); lop (i,0,n,1){ int a;cn(a) vec.PB(a);adj[a].PB(i);ori.connect(adj[a].front(),i); } int cst=0; lop (i,0,n/2,1){ if (ori.trace(i)!=ori.trace(n-1-i)){ cst++;ori.connect(i,n-1-i); } } cout<<cst; } int32_t main(){ int t;t=1; // cin>>t; while (t--){ auto t_1=chrono::high_resolution_clock::now(); solve(); auto t_2=chrono::high_resolution_clock::now(); // cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); long long mx = -1e9; long long sm = 0; long long pre = 0, pre_sum = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; sm += a[i]; sm += pre; pre += a[i]; mx = max(mx, a[i]); cout << sm + mx * (i + 1) << endl; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9+7; template <typename Abel> struct BIT { private: int n; vector<Abel> data; public: BIT (const int n_) : n(n_+1), data(n+1, 0) {} void add(const int i, const Abel x) { for(int k = i + 1 ; k < n; k += k & -k) data[k-1] += x; } Abel sum(int r) { Abel ret = data[r-1]; while(r > 0) ret += data[(r -= r & -r)-1]; return ret; } Abel sum(const int l, const int r) { return sum(r) - sum(l); } int lower_bound(Abel w){ if(w <= 0) return 0; int ret = 0, k = 1; while(k < n) k <<= 1; for(int i = k; i > 0; i >>= 1){ assert(typeid(w) == typeid(int) || typeid(w) == typeid(long long)); if(ret+i/*-1*/ < n && data[ret+i-1] < w) w -= data[(ret+=i)-1]; } return ret; } int upper_bound(const Abel w){ return lower_bound(w + 1); } void print(){ for(int i = 0; i < n - 1; i++) cout << sum(i, i+1) << ' '; cout << endl; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int n; cin >> n; vector<long long> a(n); vector<long long> mx(n, -1); cin >> a[0]; mx[0] = a[0]; for(int i = 1; i < n; i++){ cin >> a[i]; mx[i] = max(mx[i-1], a[i]); } long long ans = 0; long long tmp = 0; cout << a[0]*2 << endl; ans += a[0]+mx[0]; tmp = a[0]+mx[0]; for(int i = 1; i < n; i++){ tmp += mx[i]-mx[i-1]; ans += tmp + a[i]; tmp = tmp+a[i]; ans += max(i*(mx[i]-mx[i-1]), 0LL); cout << ans << " " << endl; } return 0; }
#include<bits/stdc++.h> #define SZ(x) ((int)x.size()) #define pb push_back template <typename _Tp>void read(_Tp &x){ char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar(); x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar(); if(f)x=-x; } template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);} typedef long long ll; ll f[105]; bool tag[105]; int main(){ f[0]=1,f[1]=1;for(int i=2;i<=90;++i)f[i]=f[i-1]+f[i-2]; ll n;read(n); int p=0;for(int i=1;i<=90;++i)if(f[i]<=n)p=i; std::vector<int> ans; ll x=n; for(int i=90;i>=0;--i)if(x>=f[i])tag[i]=true,x-=f[i]; if(p&1){ for(int i=0;i<=p;++i){ if(tag[p-i])ans.pb(2-i%2); if(i<p)ans.pb(3+i%2); } } else{ for(int i=0;i<=p;++i){ if(tag[p-i])ans.pb(i%2+1); if(i<p)ans.pb(4-i%2); } } printf("%d\n",SZ(ans)); for(auto it:ans)printf("%d\n",it); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll mod = 1000000007; bool tooBig(__int128 x, __int128 y) { return (2 * x - y) * (2 * x - y) > 5 * y * y; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); ll x; cin >> x; ll l = 0, r = x + 1, y = x; while (l < r) { ll mid = (l + r) / 2; if (tooBig(x, mid)) { y = mid; l = mid + 1; } else { r = mid; } } vector<int> ans; while (x > 0 && y > 0) { if (x > y) { while (tooBig(x - 1, y)) { ans.push_back(1); x--; } ans.push_back(3); x -=y; } else { while (tooBig(y - 1, x)) { ans.push_back(2); y--; } ans.push_back(4); y -= x; } } while (x > 0) { ans.push_back(1); x--; } while (y > 0) { ans.push_back(2); y--; } reverse(ans.begin(), ans.end()); cout << int(ans.size()) << '\n'; for (int i : ans) cout << i << '\n'; return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, const char* argv[]){ string n; cin >> n; int n_s = n.length()-1; while(n.at(n_s) == '0' && n_s != 0){ n_s = n_s-1; } int n_min=0; int n_max=n_s; bool r_f=true; while(n_min < n_max) { if (n.at(n_min) != n.at(n_max)) { r_f=false; } n_min = n_min+1; n_max = n_max-1; } if(r_f){ std::cout<<"Yes"<<endl; } else{ std::cout<<"No"<<endl; } }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define ff first #define ss second #define sc(x) scanf("%d",&x) #define fra(v) for(auto &it:v) #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL) ll binaryExponentiation(ll x,ll n) { ll result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } void SieveOfEratosthenes(ll n) { bool prime[n+1]; memset(prime, true, sizeof(prime)); for (ll p=2; p*p<=n; p++) { if (prime[p] == true) { for (ll i=p*p; i<=n; i += p) prime[i] = false; } } for (ll p=2; p<=n; p++) if (prime[p]) cout << p << " "; } int main() { fastIO; ll t; t=1; while(t--) { string s,s1,x; cin>>s; x=s; reverse(x.begin(),x.end()); if(s==x) { cout<<"Yes"; continue; } ll a=0; ll l=s.size()-1; while(s[l]=='0') { a++; l--; } while(a--) { s1+='0'; } s=s1+s; x=s; reverse(x.begin(), x.end()); if(s==x) { cout<<"Yes" ; } else { cout<<"No" ; } } }
#include <bits/stdc++.h> using namespace std; #define gap " " #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define mp make_pair #define ff first #define ss second #define ok cout<<"ok"<<endl #define DBG(a) cerr<< "Line "<<__LINE__ <<" : "<< #a <<" = "<<(a)<<endl #define fastio {ios_base::sync_with_stdio(false);cin.tie(NULL);} typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; const int N=100009; const int mod=1e9+7; ll dp[2002][2002][4]; //0 ->total sum //1->Horizontal prefix sum //2->Vertical prefix sum //3-> Corner Prefix sum vector<string> vec; int main() { string s; int row,col,i,j,k; cin>>row>>col; for(i=0; i<row; i++){ cin>>s; vec.pb(s); } int wallH=1,wallV=1,wallC=1; ll sum; for(i=1; i<=row; i++){ for(j=1; j<=col; j++){ for(k=0; k<4; k++) dp[i][j][k]=0; if(i==1 && j==1) continue; if(vec[i-1][j-1]=='.'){ sum=dp[i][j-1][1]+dp[i-1][j][2]+dp[i-1][j-1][3]; if(i==1) sum+=wallH; else if(j==1) sum+=wallV; else if(i==j) sum+=wallC; sum=sum%mod; dp[i][j][0]=sum; dp[i][j][1]=(dp[i][j-1][1]+sum)%mod; dp[i][j][2]=(dp[i-1][j][2]+sum)%mod; dp[i][j][3]=(dp[i-1][j-1][3]+sum)%mod; } else{ if(i==1) wallH=0; else if(j==1) wallV=0; else if(i==j) wallC=0; } } } cout<<dp[row][col][0]%mod<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int solve(long long* available, int n, long long k) { if (available[n - 1] < k) { return -1; } int l = 0, r = n - 1; while (l < r) { int middle = l + (r - l) / 2; if (available[middle] >= k) { if (available[middle - 1] < k) { return middle; } r = middle - 1; } else { l = middle + 1; } } return l; } int main() { int n, q; cin >> n >> q; auto available = new long long[n]; auto not_available = new long long[n]; cin >> not_available[0]; available[0] = not_available[0] - 1; int i; for (i = 1; i < n; i++) { cin >> not_available[i]; available[i] = available[i - 1] + not_available[i] - not_available[i - 1] - 1; } long long k; for (i = 0; i < q; i++) { cin >> k; auto solution = solve(available, n, k); if (solution == -1) { cout << not_available[n - 1] + k - available[n - 1]; } else { cout << not_available[solution] - (available[solution] - k) - 1; } cout << '\n'; } }
#include<bits/stdc++.h> #define mxn 500005 #define ll long long using namespace std; char str[mxn], t[mxn]; int a[mxn], b[mxn]; int main() { int n; scanf("%d %s %s", &n, str+1, t+1); for(int i=1; i<=n; i++) a[i]= str[i]-'0', b[i]= t[i]-'0'; ll res= 0; int lst; if(str[1]==t[1])lst= 2; else lst= 1; for(int i=2; i<=n; i++) { if(str[i]=='1') { if(lst<i) { res+= (i-lst); a[lst]^= 1; a[i]= 0; while(lst<=i) { if(a[lst]==b[lst])lst++; else break; } } else if(a[i]==b[i])lst++; } else if(lst==i && a[i]==b[i])lst++; } if(lst<=n)res= -1; printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() void chmax(ll &x, ll y) {x = max(x,y);} void chmin(ll &x, ll y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} template<class in_vec_cout> void vec_cout(vector<in_vec_cout> vec) { for (in_vec_cout res : vec) {cout << res << " ";} cout << endl; } const ll inf = 1e18; const ll mod = 1e9 + 7; int main() { ll N, X; cin >> N >> X; vector<ll> A(N); rep(i,N) cin >> A[i]; vector<ll> S(N); repm(i,N-1,0) { S[i] = X / A[i]; X %= A[i]; } vector<vector<ll>> dp(N+1,vector<ll>(2)); dp[0][0] = 1; rep(i,N) { rep(j,2) { ll x = S[i] + j; if (x==0) { dp[i+1][0] = dp[i][j]; continue; } if (i<N-1) { dp[i+1][1] += dp[i][j]; if (x==A[i+1]/A[i]) continue; } dp[i+1][0] += dp[i][j]; } } ll ans = dp[N][0]; Cout(ans); }
#include<bits/stdc++.h> using namespace std; int read(); #define LL long long #define fr(i,l,r) for(int i = (l);i <= (r);++i) #define rf(i,l,r) for(int i = (l);i >= (r);--i) #define fo(i,l,r) for(int i = (l);i < (r);++i) #define foredge(i,u,v) for(int i = fir[u],v;v = to[i],i;i = nxt[i]) #define ci const int & #define cl const LL & #define I inline void #define filein(File) freopen(File".in","r",stdin) #define fileout(File) freopen(File".out","w",stdout) const int N = 105; const int p[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; int n,a[N],cnt[N]; int gcd(int x,int y) { return !y ? x : gcd(y,x % y); } LL now = 1,ans = 9e18; I chk() { fr(i,1,n) if(gcd(now % a[i],a[i]) == 1) return; ans = min(ans,now); } void dfs(int cur) { if(cur == 15) return chk(); dfs(cur + 1); now *= p[cur]; dfs(cur + 1); now /= p[cur]; } int main() { cin >> n; fr(i,1,n) cin >> a[i]; dfs(0); cout << ans << endl; return 0; }
/** * Author : Tanbin_Hasan * Created : 14.03.2021 **/ #include <bits/stdc++.h> using namespace std ; void subsets(vector<long long> &ar , int n , vector<long long> &d) { for (int mask = 0 ; mask < (1LL << n) ; ++mask) { vector<long long> temp ; for (int i = 0 ; i < n ; ++i) { if (mask & (1LL << i)) { temp.push_back(ar[i]) ; } } long long res = 1 ; if (!temp.empty()) { for (auto &i : temp) { res *= i ; } d.push_back(res) ; } } } int main(void) { ios::sync_with_stdio(false) ; cin.tie(0) ; vector<long long> prime = {2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47} ; int n ; cin >> n ; vector<long long> v(n) ; unordered_set<long long> us ; for (auto &i : v) { cin >> i ; for (auto &p : prime) { if (!(i % p)) { us.insert(p) ; } } } vector<long long> fact ; vector<long long> d ; for (auto &i : us) { fact.push_back(i) ; } subsets(fact , (int) fact.size() , d) ; long long res = LONG_LONG_MAX ; for (auto &i : d) { int cnt = 0 ; for (auto &j : v) { if (__gcd(i , j) != 1) { ++cnt ; } } if (cnt == n) { res = min(res , i) ; } } cout << res << '\n' ; return 0 ; }
#include <iostream> #include <string> #include <cstring> #include <vector> #include <algorithm> #include <cmath> using namespace std; const int maxn = 100; double ret[maxn][maxn][maxn]; double dfs(vector<int>&coins){ if(coins[1]==0) return 100.0 - coins[0]; if(coins[0]==100 || coins[1] ==100 || coins[2]==100) return 0.0; double &ans = ret[coins[0]][coins[1]][coins[2]]; if(ans>=0) return ans; int s = 0; for(auto c: coins) s += c; double res = 0; for(int i=0;i<3;i++){ vector<int>nxt(coins); nxt[i] ++; res += (double)coins[i] / s * (1+dfs(nxt)); } return ans = res; } int main(){ vector<int> coins(3); for(int i=0;i<3;i++) cin>>coins[i]; sort(coins.rbegin(), coins.rend()); memset(ret, -1, sizeof ret); printf("%.12f\n", dfs(coins)); return 0; }
#include <sstream> #include <iostream> #include <string> #include <vector> #include <numeric> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <list> #include <cassert> #include <cmath> using namespace std; int main() { auto& in = cin; int a, b, c; in >> a >> b >> c; vector<vector<vector<double>>> count(101, vector<vector<double>>(101, vector<double>(101))); for (int ia = 99; ia >= 0; --ia) { for (int ib = 99; ib >= 0; --ib) { for (int ic = 99; ic >= 0; --ic) { double sum = ia + ib + ic; count[ia][ib][ic] = ((double)ia / sum) * (count[ia + 1][ib][ic] + 1.0) + ((double)ib / sum) * (count[ia][ib + 1][ic] + 1.0) + ((double)ic / sum) * (count[ia][ib][ic + 1] + 1.0); } } } cout.precision(15); cout << count[a][b][c]; return 0; }
// #define _GLIBCXX_DEBUG // #define _LIBCPP_DEBUG 0 #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; // clang-format off #define PI 3.14159265358979323846264338327950L #define rep(i,n) for (int i=0; i<(int)(n); ++i) #define rep2(i,s,n) for (int i=(s); i<(int)(n); ++i) #define prep_ios(do_flush) if(!do_flush){ ios::sync_with_stdio(false);cin.tie(nullptr);constexpr char endl='\n'; } cout<<fixed<<setprecision(20) #define put(expr) cout<<expr<<'\n' template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "[ "; for (auto v: vec) os << v << " "; os << "]"; return os; }template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "< "; for (auto v: vec) os << v << " "; os << ">"; return os; } template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; }template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << ", " << pa.second << ")"; return os; } typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef float f32; typedef double f64; typedef long double f80; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef map<int, int> mii; typedef __int128_t i128; i128 parse(string &s) { i128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } istream &operator>>(istream &is, i128 &v) { string s; is >> s; v = parse(s); return is; } ostream &operator<<(ostream &os, const i128 &v) { if (v == 0) return (os << "0"); i128 num = v; if (v < 0) { os << '-'; num = -num; } string s; for (; num > 0; num /= 10) s.push_back('0' + (char)(num % 10)); reverse(s.begin(), s.end()); return (os << s); } template <typename T> bool chmax(T &now, const T &cand) { if (now < cand) { now = cand; return true; } return false; } template <typename T> bool chmin(T &now, const T &cand) { if (now > cand) { now = cand; return true; } return false; } template <typename T> T gcd(const T &a, const T &b) { if (a % b == 0) return b; return gcd(b, a % b); } template <typename T> T lcm(const T &a, const T &b) { return a * b / gcd(a, b); } template <typename T> T nCr(const T &n, T k) { if (k > n) return 0; if (k * 2 > n) k = n - k; if (k == 0) return 1; int result = n; for (int i = 2; i <= k; ++i) { result *= (n - i + 1); result /= i; } return result; } const int INF = 1<<30; /* INF > 10^9 */ const i64 INFLL = 1LL<<60; /* INFLL > 10^18 */ const string YES = "Yes", NO = "No"; // clang-format on? int main() { prep_ios(true); i64 N; cin >> N; i64 _max = 100000; i64 _min = 0; vector<i64> A(N); rep(i, N) cin >> A[i]; vector<i64> B(N); rep(i, N) cin >> B[i]; rep(i, N) chmax(_min, A[i]); rep(i, N) chmin(_max, B[i]); i64 diff = _max - _min + 1; if (diff < 0) diff = 0; put(diff); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll b,c;cin>>b>>c; if(c==1){cout<<(b==0?1:2)<<endl;return 0;} else{ if(((c-1)/2-b) < (b-c/2) || b+(c-2)/2 < -b-(c-1)/2)cout<<((c-1)/2)*2+(c-2)/2+c/2+2<<endl; else cout<<max(-b+(c-1)/2,b+(c-2)/2)-min(b-c/2,-b-(c-1)/2)+1<<endl; } }
#include <bits/stdc++.h> using namespace std; using lint = long long; constexpr lint inf = 1LL << 60; constexpr lint mod = 1000000007; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); lint n; cin >> n; vector<int> a(n), b(n); map<int, int> cnt; vector<pair<int, char>> pos(2 * n + 1, pair<int, char>(-1, 'N')); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; cnt[a[i]]++; cnt[b[i]]++; if (a[i] != -1) pos[a[i]] = {b[i], 'L'}; if (b[i] != -1) pos[b[i]] = {a[i], 'R'}; } for (auto &p : cnt) { if (p.first != -1 && p.second >= 2) { cout << "No" << "\n"; return 0; } } vector<bool> dp(2 * n + 1, false); dp[0] = true; for (int i = 2; i <= 2 * n; ++i) { for (int j = i - 2; j >= 0; j -= 2) { if (!dp[j]) continue; bool ok = true; int width = (i - j) / 2; for (int k = j + 1; k <= i; ++k) { auto c = pos[k]; if (k < j + 1 + width) { // left if (c.second == 'R' || (c.second == 'L' && (c.first != -1 && c.first != k + width))) ok = false; if (c.second == 'L' && c.first == k + width) if (!(pos[k + width].second == 'R' && pos[k + width].first == k)) ok = false; if (c.second == 'L' && c.first == -1) if (pos[k + width].second != 'N') ok = false; } else { // right if (c.second == 'L' || (c.second == 'R' && (c.first != -1 && c.first != k - width))) ok = false; if (c.second == 'R' && c.first == k - width) if (!(pos[k - width].second == 'L' && pos[k - width].first == k)) ok = false; if (c.second == 'R' && c.first == -1) if (pos[k - width].second != 'N') ok = false; } } if (ok) { dp[i] = true; break; } } } if (dp[2 * n]) cout << "Yes" << "\n"; else cout << "No" << "\n"; return 0; }
#include<bits/stdc++.h> #define register #define N 201 #define INFs 0x3f3f3f3f using namespace std; int n,a[N],b[N],sum[N],tot; inline void Add(int x){if(~x){++sum[x];++tot;if(sum[x]>1){puts("No");exit(0);}}} int main(void){ int i,j,k,T=0;scanf("%d",&n); for(i=1;i<=n;++i){ scanf("%d%d",a+i,b+i); if((~a[i]&&~b[i]&&a[i]>b[i])||(~a[i]&&(++sum[a[i]]>1))||(~b[i]&&(++sum[b[i]]>1)))return puts("No"),0; tot+=(a[i]!=-1)+(b[i]!=-1); } while(1){ for(i=1;i<=n;++i)if(~a[i]&&~b[i]){ for(j=1;j<=n;++j){ if(a[j]>a[i]&&a[j]<b[i]){ if(b[j]==-1)Add(b[j]=a[j]+b[i]-a[i]);else if(b[j]!=a[j]+b[i]-a[i])return puts("No"),0; }//a夹中间 if(b[j]>a[i]&&b[j]<b[i]){ if(a[j]==-1)Add(a[j]=b[j]-b[i]+a[i]);else if(a[j]!=b[j]-b[i]+a[i])return puts("No"),0; }//b夹中间 } }++T;if(T>=100)break; // for(i=1;i<=n;++i)printf("%d %d\n",a[i],b[i]); }puts("Yes"); return 0; }
#include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<string> #include <cassert> #include <numeric> #include <unordered_map> //#include <atcoder/all> #include <queue> #include <math.h> #include <climits> #include <set> #include <map> //using namespace atcoder; typedef long long ll; #define FOR(i, a, b) for(ll i=(a); i<(b);i++) #define REP(i, n) for(ll i=0; i<(n);i++) #define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--) #define PER(i, n) for(ll i=n-1; i>=0;i--) #define REPREP(i,j,a,b) for(ll i=0;i<a;i++)for(ll j=0;j<b;j++) #define VV(type) vector<vector<type>> #define VV2(type,n,m,val) vector<vector<type>> val;val.resize(n);for(ll i;i<n;i++)val[i].resize(m) #define vec(type) vector<type> #define VEC(type,n,val) vector<type> val;val.resize(n) #define VL vector<ll> #define SZ size() #define all(i) begin(i),end(i) #define SORT(i) sort(all(i)) #define max(a,b) a>b?a:b using namespace std; int main() { ll n; cin >> n; VL x(n),y(n),r(n); ll mi = n; ll mx = 0; REP(i, n) { cin >> x[i] >> y[i] >> r[i]; if (mx < r[i]) { mx = r[i]; mi = i; } } if (1.25*1e9 < mx * n) { REP(i, n) { if (i == mi) { cout << x[(i + 0) % n] <<" "<< y[(i + 0) % n] << " " << x[(i + 0) % n] + 1 << " " << y[(i + 0) % n] + 1<<endl; } else if ((i + 1) % n == mi) { cout << x[(i + 2) % n] << " " << y[(i + 2) % n] << " " << x[(i + 2) % n] + 1 << " " << y[(i + 2) % n] + 1 << endl; } else { cout << x[(i + 1) % n] << " " << y[(i + 1) % n] << " " << x[(i + 1) % n] + 1 << " " << y[(i + 1) % n] + 1 << endl; } } } else { REP(i, n) { cout << x[(i + 1) % n] << " " << y[(i + 1) % n] << " " << x[(i + 1) % n] + 1 << " " << y[(i + 1) % n] + 1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct ad { int x; int y; int r; ad(int _x = 0, int _y = 0, int _r = 0) : x(_x), y(_y), r(_r) {} int left; int up; int right; int down; int& operator[](int i) { if (i == 0) { return left; } else if (i == 1) { return up; } else if (i == 2) { return right; } else { assert(i == 3); return down; } } double score() { int s = (right - left) * (down - up); return 1.0 - pow(1.0 - min(s, r) * 1.0 / max(s, r), 2); } }; bool intersect(ad a, ad b) { if (a.right < b.left || b.right < a.left) { return false; } if (a.down < b.up || b.down < a.up) { return false; } return true; } vector<int> get_intersect(ad b, const vector<ad>& a) { int n = (int)a.size(); vector<int> res; for (int i = 0; i < n; i++) { if (intersect(a[i], b)) { res.emplace_back(i); } } return res; } int main() { long long start_time = clock(); int n; cin >> n; vector<ad> a(n); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y >> a[i].r; a[i].left = a[i].x; a[i].right = a[i].x + 1; a[i].up = a[i].y; a[i].down = a[i].y + 1; } vector<int> order(n); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { return a[i].r < a[j].r; }); auto modify = [&](int i, int k, int d = 1) { ad b = a[i]; if (k >= 2) { a[i][k] += d; } else { a[i][k] -= d; } /* if (k == 0) { a[i].left -= d; } else if (k == 1) { a[i].up -= d; } else if (k == 2) { a[i].right += d; } else { a[i].down += d; } */ bool ok = true; for (int j = 0; j < n; j++) { if (i != j && intersect(a[i], a[j])) { ok = false; } } if (min(a[i].left, a[i].up) < 0 || max(a[i].right, a[i].down) > 9999) { ok = false; } if (!ok) { swap(a[i], b); } return ok; }; int tt = 10000; while (tt--) { long long now = clock(); if (now - start_time > 4900 * 1000) { #ifndef tabr break; #endif } for (int i : order) { ad b = a[i]; b.right++; double cur = a[i].score(); double lr = b.score(); b.right--; b.up--; double ud = b.score(); vector<double> d(4); d[0] = d[2] = (lr - cur); d[1] = d[3] = (ud - cur); for (int k = 0; k < 4; k++) { b = a[i]; if (k >= 2) { b[k] = 9999; b[k ^ 2] = a[i][k] + 1; } else { b[k] = 0; b[k ^ 2] = a[i][k] - 1; } vector<int> t = get_intersect(b, a); int c = 10000; for (int j : t) { c = min(c, abs(a[i][k] - a[j][k ^ 2])); } d[k] *= c; } double mx = *max_element(d.begin(), d.end()); if (mx <= 0) { continue; } for (int j = 0; j < 4; j++) { if (mx - d[j] < 1e-9) { modify(i, j, 1); } } } } double t = 0; for (int i = 0; i < n; i++) { cout << a[i].left << " " << a[i].up << " " << a[i].right << " " << a[i].down << '\n'; t += a[i].score(); } cerr << fixed << setprecision(6); cerr << t / n * 1e9 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(v) v.begin(), v.end() //ll の最大値 2147483647≒10^9 //long longの最大値 9,223,372,036,854,775,807≒10^18 ll gcd(ll a, ll b){ if(a%b==0){ return b; } else{ return gcd(b,a%b); } } ll digit(ll x){ ll d=0; while(x){ d++; x/=10; } return d; } int main(){ ll a,b,c; cin>>a>>b>>c; ll a_ = abs(a); ll b_ = abs(b); bool c_2 = c%2; if(a<0 && c_2){ a_ *= -1; } if(b<0 && c_2){ b_ *= -1; } if(a_>b_){ cout<<">"<<endl; } else if(a_<b_){ cout<<"<"<<endl; } else{ cout<<"="<<endl; } }
#include <algorithm> #include <iostream> #include <iomanip> #include <cassert> #include <cstring> #include <string> #include <vector> #include <random> #include <queue> #include <cmath> #include <unordered_map> #include <set> #include <map> #define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout << fixed << setprecision(20); #define rep(i,n) for (int i=0; i<n;++i) #define ALL(a) (a).begin(),(a).end() typedef long long ll; using namespace std; const ll INF = 2e18; int n, m; ll A[200005]; ll B[200005]; // それ以前の最小の金額 int X[200005], Y[200005]; vector<int> G[200005]; ll costs[200005]; // その町で買った時の最大金額 int main() { INCANT; cin >> n >> m; rep(i, n) cin >> A[i]; rep(i, n) B[i] = INF; rep(i, m) { int x, y; cin >> x >> y; G[x-1].push_back(y-1); } rep(i, n) { for (auto ne: G[i]) { B[ne] = min(B[ne], B[i]); // すでに更新されているものと、今更新するもののどちらが低いか B[ne] = min(B[ne], A[i]); // 今の値と、前からきたものどちらが低いか } } ll ans = -INF; rep(i, n) { ans = max(ans, A[i]-B[i]); } cout << ans << "\n"; return 0; }
#pragma GCC optimize("Ofast") #include <limits.h> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <map> #include <cmath> #include <iomanip> #include <string> #include <deque> #include <assert.h> #include <random> #include <ctime> #include <unordered_map> #include <complex> #include <chrono> #define pnode node* using namespace std; typedef long long ll; typedef long double ld; ll n; string t; void zer() { cout << 0 << "\n"; exit(0); } void solve(){ cin >> n; cin >> t; ll ten = 10'000'000'000; if (t == "1") { cout << 2 * ten << "\n"; return; } else if (t == "11") { cout << ten << "\n"; return; } ll fst = -1; for (ll i = 0; i < t.size(); ++i) { if (t[i] == '0') { fst = i; break; } } if (fst == -1) { zer(); } vector < ll > z(t.size()); for (ll shft = 0; shft <= t.size(); ++shft) { // cerr << shft << "\n"; ll l = fst - 3 * shft; ll r = fst + 3 * shft; if (l >= 0) { if (t[l] == '1') { zer(); } else { z[l] = 1; } } if (r < t.size()) { if (t[r] == '1') { zer(); } else { z[r] = 1; } } } // for (ll i = 0; i < z.size(); ++i) { // cerr << z[i] << " "; // } // cerr << "\n"; for (ll i = 0; i < t.size(); ++i) { if (!z[i] && t[i] == '0') { zer(); } } ll sz = t.size(); if (t.size() >= 2 && t[0] == '1' && t[1] == '1') { sz -= 2; } else if (t[0] == '1') { sz--; } ll res = (3 * ten - 2 - sz) / 3 + 1; cout << res << "\n"; } signed main(){ ll t = 1; ios_base::sync_with_stdio(0); srand(time(0)); cin.tie(0); cout.tie(0); cout << fixed; cout << setprecision(20); #ifdef DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else #endif while(t--){ solve(); } }
//##### ##### ##### ##### ##### # ##### # #// //# # # # # # # # # # # ## ##// //##### ##### ##### # ##### # ##### # # #// //## # # # # # # # # # # #// //# # # # # # # # # # # # #// //# # # # # # # # # # # # #// //# # # # ##### ##### # # ##### # # # #// #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define ppb pop_back #define cs(n) cout<<n<<" " #define cn(n) cout<<n<<"\n" #define rep(i,j,k) for(ll i=j;i<k;i++) #define rrep(i,j,k) for(ll i=j;i>=k;i--) #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define deb(x) cout << #x << " " << x << endl; template<typename... T> void read(T&... args) { ((cin >> args), ...); } template<typename... T> void write(T&&... args) { ((cout << args << " "), ...); } template<typename... T> void writen(T&&... args) { ((cout << args << "\n"), ...); } ll bin(ll a, ll b, ll mod) { if (b == 0) return 1; if (b % 2 == 0) return bin((a * a) % mod , b / 2 , mod) % mod; return ((a % mod) * (bin((a * a) % mod , b / 2 , mod) % mod)) % mod; } void solve() { ll n; read(n); ll ans = 0; rep(i, 0, n) { ll a, b; read(a, b); ans += b * (b + 1) / 2; ans -= a * (a - 1) / 2; } cout << ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r" , stdin); freopen("output.txt" , "w" , stdout); #endif fastio; ll t; t = 1; //cin>>t; while (t--) { solve(); } #ifndef ONLINE_JUDGE cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n"; #endif return 0; }
#include<bits/stdc++.h> using namespace std; #define endl '\n' typedef long long int ll; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for(auto it = d.b;it != d.e;++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, m;cin >> n >> m; set<int> s; vector<int> cnt(2000000); vector<int> arr(n); for(int i = 0;i < n;i++)cin >> arr[i]; for(int i = 0;i <= n;i++)s.insert(i); for(int i = 0;i < m;i++){ s.erase(arr[i]); cnt[arr[i]]++; } int ans = *s.begin(); for(int i = m;i < n;i++){ cnt[arr[i - m]]--; if(cnt[arr[i - m]] == 0)s.insert(arr[i - m]); cnt[arr[i]]++; s.erase(arr[i]); ans = min(ans, *s.begin()); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; bool comp(string& a, string& b) { return int(a.length()) < int(b.length()); } bool check(char** ans,string &s, int i, int j, int n,unordered_map<char,vector<int>>& ii) { bool flg = true; for(int a=0;a<int(s.length());a++) { if (ans[(a+i)%n][j]!=s[a] && ans[(a+i)%n][j]!='.') { flg = false; break; } } if (flg) { for(int a=0;a<int(s.length());a++) { ans[(a+i)%n][j] = s[a]; vector<int> t = {(a+i)%n,j}; ii[s[a]] = t; } return flg; } for(int a=0;a<int(s.length());a++) { if (ans[(i-a+n)%n][j]!=s[a] && ans[(i-a+n)%n][j]!='.') { flg = false; break; } } if (flg) { for(int a=0;a<int(s.length());a++) { ans[(i-a+n)%n][j] = s[a]; vector<int> t = {(i-a+n)%n,j}; ii[s[a]] = t; } return flg; } for(int a=0;a<int(s.length());a++) { if (ans[i][(j+a)%n]!=s[a] &&ans[i][(j+a)%n]!='.') { flg = false; break; } } if (flg) { for(int a=0;a<int(s.length());a++) { ans[i][(j+a)%n] = s[a]; vector<int> t = {i,(j+a)%n}; ii[s[a]] = t; } return flg; } for(int a=0;a<int(s.length());a++) { if (ans[i][(j-a+n)%n]!=s[a] &&ans[i][(j-a+n)%n]!='.') { flg = false; break; } } if (flg) { for(int a=0;a<int(s.length());a++) { ans[i][(j-a+n)%n] = s[a]; vector<int> t = {i,(j-a+n)%n}; ii[s[a]] = t; } } return flg; } void solve(char** ans, vector<string>& s, int n) { unordered_map<char,vector<int>> ii; queue<vector<int>> que; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { vector<int> t = {i,j}; que.push(t); } } for(int i=0;i<s.size();i++) { string& tar = s[i]; auto itr = ii.find(tar[0]); if (itr!=ii.end()) { vector<int> kouho = ii[tar[0]]; check(ans, tar, kouho[0], kouho[1], n,ii); } if (que.size()==0) continue; auto k = que.front(); que.pop(); check(ans, tar, k[0],k[1],n,ii); } } int main() { int n,m; cin >> n; cin >> m; vector<string> s; s.reserve(m); string t; for(int i=0;i<m;i++) { cin >> t; s.push_back(t); } sort(s.begin(),s.end(),comp); char **ans = new char*[n]; for(int i=0;i<n;i++) { ans[i] = new char[n]; for(int j=0;j<n;j++) { ans[i][j] = '.'; } } solve(ans,s,n); for(int i=0;i<n;i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(int)n;i++) using ll = long long; using P = pair<int, int>; int main() { ll a, b, c, d; cin >> a >> b >> c >> d; a = c - a, b = d - b; if (a == 0 && b == 0) cout << 0 << endl; else if (a + b == 0 || a - b == 0 || abs(a) + abs(b) <= 3) cout << 1 << endl; else if ((a + b) % 2 == 0 || abs(abs(a) - abs(b)) <= 6 || abs(a) + abs(b) <= 6) cout << 2 << endl; else cout << 3 << endl; return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for(int i = 0; i < n; ++i) #define GETVEC(v) for(auto& m : v) cin >> m; using namespace std; using LLONG = long long; const LLONG MOD = 1000000007; int main() { int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; int ans = -1; if (r1 == r2 && c1 == c2) { ans = 0; } else if (abs(r1 - r2) + abs(c1 - c2) <= 3 || abs(r1 - r2) == abs(c1 - c2)) { ans = 1; } else if ((r1 + c1) % 2 == (r2 + c2) % 2) { ans = 2; } else { ans = 3; for (int r = r1 - 2; r <= r1 + 2; ++r) { for (int c = c1 - 2; c <= c1 + 2; ++c) { if (abs(r - r2) + abs(c - c2) <= 3 || abs(r - r2) == abs(c - c2)) { ans = 2; } } } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (auto i = 0; i < n; ++i) #define ALL(a) a.begin(), a.end() using namespace std; using ll = long long int; const int MOD_NUM = 1e9 + 7; struct Item { int size; int value; }; bool compare_item_greater(const Item& l, const Item& r) { return l.value == r.value ? l.size < r.size : l.value > r.value; } int main() { // Input int N, M, Q; cin >> N >> M >> Q; vector<int> W(N), V(N), X(M); rep(i, N) cin >> W[i] >> V[i]; rep(i, M) cin >> X[i]; vector<Item> items(N); rep(i, N) items[i] = {W[i], V[i]}; sort(ALL(items), compare_item_greater); for (; Q > 0; Q--) { // Input int L, R; cin >> L >> R; // Process vector<int> x(L - 1 + M - R); copy(X.begin(), X.begin() + L - 1, x.begin()); copy(X.begin() + R, X.end(), x.begin() + L - 1); sort(ALL(x)); int ans = 0; for (Item item : items) { auto iter = lower_bound(ALL(x), item.size); if (iter != x.end()) { ans += item.value; x.erase(iter); } } // Output cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #include <map> #include <iostream> using namespace std; typedef long long ll; typedef long double ld; #define BIG 200005 int N; struct Data{ int ind; int label; // ans bool tree = false; // in tree? vector<pair<int, int>> edges; // node, label vector<pair<int, int>> tree_edges; }; vector<Data> nodes(BIG); bool make_tree(int p, int c){ if(nodes[c].tree) return false; nodes[c].tree = true; for(auto child: nodes[c].edges){ if(child.first == p) continue; bool created = make_tree(c, child.first); if(created){ nodes[c].tree_edges.push_back(make_pair(child.first, child.second)); } } return true; } void solve(int p, int c, int l){ // decide my label if(p!=l){ nodes[c].label = l; }else{ // l以外の適当な値 nodes[c].label = l % N + 1; } for(auto child: nodes[c].tree_edges){ solve(nodes[c].label, child.first, child.second); } } int main() { int M; cin >> N >> M; for(int i=0;i<M;i++){ int u, v, c; cin >> u >> v >> c; u--; v--; nodes[u].edges.push_back(make_pair(v, c)); nodes[v].edges.push_back(make_pair(u, c)); } // make a tree make_tree(-1, 0); solve(1, 0, 1); for(int i=0;i<N;i++){ Data d = nodes[i]; printf("%d\n", d.label); } return 0; }
#include <iostream> using namespace std; const int kMaxN = 20; const int kMaxL = 73; const int kP[kMaxN] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; int s[kMaxL], c; long long f[kMaxL + 1][1 << kMaxN], a, b, ans; long long C(int i, int j) { if (f[i][j]) { return f[i][j]; } f[i][j] = 1; for (int k = i; k <= b - a; k++) { if (s[k] && (j & s[k]) == 0) { f[i][j] += C(k + 1, j | s[k]); } } return f[i][j]; } int main() { cin >> a >> b; for (long long i = a; i <= b; i++) { for (int j = 0; j < kMaxN; j++) { s[i - a] |= (i % kP[j] == 0) << j; } c += !s[i - a]; } cout << C(0, 0) * (1 << c); return 0; }
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ld = long double; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__) #define dbg_p(x) cerr<<#x": "<<(x).first<<' '<<(x).second<<endl #define dbg_v(x, n) {cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<endl;} #define all(v) v.begin(), v.end() #define fi first #define se second void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} template<typename T1, typename T2> ostream& operator <<(ostream &out, const pair<T1, T2> &item) { out << '(' << item.first << ", " << item.second << ')'; return out; } template <typename T> ostream& operator <<(ostream &out, const vector<T>& v) { for(const auto &item : v) out << item << ' '; return out; } const int N = 15; char s[N][N], aux[N][N]; void rot(int &m, int &n) { for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) aux[n - 1 - j][i] = s[i][j]; swap(m, n); for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) s[i][j] = aux[i][j]; } int solve(int m, int n) { int last, res = 0; for(int j = 1; j < n; ++j) { last = -2; for(int i = 0; i < m; ++i) if(s[i][j] == '#' && s[i][j - 1] == '.') { if(i != last + 1) ++res; last = i; } } return res; } int main() { ios_base::sync_with_stdio(false); int m, n; cin >> m >> n; for(int i = 0; i < m; ++i) cin >> s[i]; int ans = 0; for(int i = 0; i < 4; ++i) { rot(m, n); /*for(int r = 0; r < m; ++r, cerr << '\n') for(int c = 0; c < n; ++c) cerr << s[r][c]; cerr << '\n';*/ ans += solve(m, n); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; void fastStd() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastStd(); string s; cin >> s; int n = s.length(); vector<int> arr(n); for(int i = 0; i < n; i++){ arr[i] = (s[i] - '0') % 3; } vector<vector<int>> dp(n, vector<int>(3, 0)); // dp[0][arr[0]] = 1; for(int i = 0; i < n; i++){ dp[i][arr[i]] = 1; } for(int i = 1; i < n; i++){ for(int j = 0; j < 3; j++){ // int a = dp[i][(j+arr[i])%3] + dp[i-1][j]; if(dp[i-1][j] != 0) dp[i][(j+arr[i])%3] = max(dp[i-1][j] + 1, dp[i-1][(j+arr[i])%3]); else dp[i][(j+arr[i])%3] = max(dp[i-1][(j+arr[i])%3], dp[i][(j+arr[i])%3]); } // for(int j = 0; j < 3; j++){ // printf("%d ", dp[i][j]); // } // printf("\n"); } if(dp[n-1][0] != 0){ cout << n - dp[n-1][0] << "\n"; } else { cout << -1 << "\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; #define MOD 1000000007 #define deb(x) cout << #x << " = " << x << endl #define deb2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define PI 3.1415926535897932384626 typedef long long ll; int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); string s; cin>>s; int n = s.length(); int ar[n]; int sum = 0; for(int i=0; i<n; i++)ar[i] = ((s[i]-'0')%3); for(int i=0; i<n; i++)sum+=ar[i]; if(sum%3==0) { cout<<"0"; return 0; } int yo = sum%3; int c[3]; c[0] = 0; c[1] = 0; c[2] = 0; for(int i=0; i<n; i++) { c[ar[i]]++; } if(yo==1) { if(c[1]>0) { if(n>1)cout<<"1"; else cout<<"-1"; } else if(c[2]>1) { if(n>2)cout<<"2"; else cout<<"-1"; } else cout<<"-1"; } else { if(c[2]>0) { if(n>1)cout<<"1"; else cout<<"-1"; } else if(c[1]>1) { if(n>2)cout<<"2"; else cout<<"-1"; } else cout<<"-1"; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vec = vector<ll>; using mat = vector<vec>; using pll = pair<ll,ll>; #define INF (1LL << 60) #define MOD 1000000007 #define PI 3.14159265358979323846 #define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i)) #define FORR(i,v) for(auto (i):v) #define ALL(x) (x).begin(), (x).end() #define PR(x) cout << (x) << endl #define PS(x) cout << (x) << " " #define SZ(x) ((ll)(x).size()) #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) #define REV(x) reverse(ALL((x))) #define ASC(x) sort(ALL((x))) #define DESC(x) ASC((x)); REV((x)) #define pb push_back #define eb emplace_back bool check(mat A, ll X, ll N, ll K) { mat S(N+1, vec(N+1, 0)); bool f = false; REP(i,1,N+1) { REP(j,1,N+1) { S[i][j] = S[i-1][j] + S[i][j-1] - S[i-1][j-1] + (A[i-1][j-1] > X); } } REP(i,1,N-K+2) { REP(j,1,N-K+2) { ll cnt = S[i+K-1][j+K-1] - S[i+K-1][j-1] - S[i-1][j+K-1] + S[i-1][j-1]; if(cnt < K*K/2+1) { f = true; break; } } } return f; } int main() { ll N, K; cin >> N >> K; mat A(N, vec(N)); REP(i,0,N) { REP(j,0,N) cin >> A[i][j]; } ll ng = -1, ok = INF; while(abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if(check(A, mid, N, K)) ok = mid; else ng = mid; } PR(ok); return 0; } /* */
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 5000 + 10; const LL MOD = 998244353; int n, m; LL mpow(LL a, LL x) { if (x == 0) return 1; LL t = mpow(a, x>>1); if (x % 2 == 0) return t * t % MOD; return t * t % MOD * a % MOD; } LL f[N][N], pw[N][N]; int main() { pw[0][0] = 1; for (int i = 1; i < N; i ++) { pw[i][0] = 1; for (int j = 1; j < N; j ++) pw[i][j] = pw[i][j-1] * i % MOD; } cin >> n >> m; for (int i = 1; i <= m; i ++) { f[1][i] = pw[m][n-1]; } for (int i = 2; i <= n; i ++) { for (int x = 1; x <= m; x ++) { f[i][x] = f[i-1][x] - pw[m-x][i-2] * pw[m][n-(i-1)] % MOD + pw[m-x][i-1] * pw[m][n-i] % MOD + pw[m-x][i-2] * (x-1) % MOD * pw[m][n-i] % MOD; f[i][x] = (f[i][x] % MOD + MOD) % MOD; //printf("# %d %d : %d\n", i, x, f[i][x]); } } LL ans = 0; for (int i = 1; i <= n; i ++) for (int x = 1; x <= m; x ++) (ans += f[i][x]) %= MOD; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl '\n' #define debugg(x) cout<<#x<<'='<<x<<endl; #define debug1(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl; #define debug cout<<endl<<"********"<<endl; #define ll long long #define ull unsigned long long #define ding cerr<<endl<<"Time: "<<(clock() * 1000. / CLOCKS_PER_SEC)<<" ms"<<endl; #define ld long double #define itn int #define pi (int)(3.1415926536) #define pii pair<int,int> #define rep(I, A, B) for (int I = (A); I <= (B); ++I) #define dwn(I, A, B) for (int I = (A); I >= (B); --I) #define repp(i,a,b) for(int i=(a);i<(b);i++) #define mod (ll)(1e9+7) #define mod1 (ll)(1e9) #define int ll void fre(){ freopen("test.in","r",stdin); freopen("test.out","w",stdout); } void fc(){fclose(stdin);fclose(stdout);} inline ll ccin(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } inline void cou(ll x){ if(x<0) putchar('-'),x=-x; if(x>9) cou(x/10); putchar(x%10+'0'); } const int maxn=2e5+10; const ll inf=1e16; const int maxm=5e5+10; int n; struct node{ int v;int w; }; vector<node> yuan[maxn]; int zhi[maxn],cnt0[70],cnt1[70]; void dfs(int now,int fa,int val){ zhi[now]=val; for(int i=0;i<yuan[now].size();i++){ int v=yuan[now][i].v; if(v==fa) continue; dfs(v,now,val^yuan[now][i].w); } } signed main(){ ios; cin>>n; for(int i=1;i<n;i++){ int a1,a2,a3;cin>>a1>>a2>>a3; yuan[a1].push_back((node){a2,a3}); yuan[a2].push_back((node){a1,a3}); } dfs(1,0,0); for(int i=0;i<=61;i++){ int now=(1LL<<i); for(int j=1;j<=n;j++){ if(now&zhi[j]) cnt1[i]++; else cnt0[i]++; } } ll ans=0; for(int i=0;i<=61;i++){ ll now=(1LL<<i)%mod; ll cnt=cnt1[i]*cnt0[i]%mod; ans=(ans+now*cnt)%mod; } cout<<ans<<endl; ding;return 0; }
#include <bits/stdc++.h> #define Rep(i,n,m) for(int i=(int)(n);i<(int)(m);i++) #define rep(i,n) Rep(i,0,n) #define all(v) v.begin(),v.end() using namespace std; using ll=long long; using vi=vector<int>; using vll=vector<ll>; #define _GLIBCXX_DEBUG vll g; using pii=pair<int,ll>; using graph=vector<vector<pii>>; const ll mod=1000000007; void dfs(graph &G,int v,int p,ll m){ g[v]=m; for(auto q:G[v]){ if(q.first==p) continue; dfs(G,q.first,v,m^q.second); } } int main(){ int N;cin>>N; vll pw(60);pw[0]=1; rep(i,59) pw[i+1]=pw[i]*2; graph G(N); rep(i,N-1){ int x,y;ll z;cin>>x>>y>>z;x--;y--; G[x].push_back({y,z});G[y].push_back({x,z}); } g=vll(N); dfs(G,0,-1,0); vll cn(60); rep(i,N){ rep(j,60){ if(g[i]&pw[j]) cn[j]++; } } ll ans=0; rep(i,60){ ans+=pw[i]%mod*cn[i]%mod*(N-cn[i])%mod; ans%=mod; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; #define DBG(a) cout<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl #define eps 1e-8 #define eq(x,y) (fabs((x)-(y)) < eps) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int>pii; const int mod = 998244353; long double pi = acosl(-1); const ll infl = 1e17+100; const int inf = 1e9+100; const int nmax = 1e6+5; const int MAXLG = log2(nmax)+1; //mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); //typedef tree< ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ost; multiset<int>nibo; vector<int>v1, v2; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; v1.resize(n), v2.resize(n); for(int &x : v1) cin>>x; for(int &x : v2) cin>>x; reverse(v2.begin(), v2.end()); ll ans = 0; for(int i=0; i<n; i++){ int x1 = v1.back(), x2 = v2.back(); v1.pop_back(), v2.pop_back(); if(x1>x2) swap(x1,x2); if(i==0){ nibo.insert(x2); } else{ auto it = nibo.begin(); if( *it < x1){ nibo.erase(it); nibo.insert(x2); nibo.insert(x1); } else { nibo.insert(x2); } } // for(int x : nibo) cout<<x<<" "; // cout<<"aa "<<endl; } for(int x : nibo) ans += x; cout<<ans<<endl; } /* */
#include <bits/stdc++.h> using namespace std; #define int long long const int MOD = 1000000007; template <class T> struct fenwick_tree { vector<T> x; fenwick_tree(int n) : x(n, 0) {} T sum(int i, int j) { if (i == 0) { T S = 0; for (j; j >= 0; j = (j & (j + 1)) - 1) S += x[j]; return S; } else return sum(0, j) - sum(0, i - 1); } void add(int k, T a) { for (; k < x.size(); k |= k + 1) x[k] += a; } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> A(2 * N); int res = 0; for (int i = 0; i < 2 * N; i++) { cin >> A[i]; } vector<int> B; for (int i = 0; i < N; i++) { B.push_back(min(A[N - 1 - i], A[i + N])); B.push_back(max(A[N - 1 - i], A[i + N])); } int sum = 0; priority_queue<int> pq; for (int i = 1; i < 2 * N; i++) { if (i % 2 == 0) { int a = -pq.top(); if (a < B[i]) { res += B[i] - a; pq.pop(); pq.push(-B[i]); } } else { res += B[i]; pq.push(-B[i]); } } // fenwick_tree<int> ft(2 * N); // for (int i = 0; i < 3 * N; i++) { // ft.add(i, 1); // } // vector<pair<int, int> > vp; // for (int i = 0; i < 2 * N; i++) { // vp.emplace_back(B[i], i); // } // sort(B.rbegin(), B.rend()); // for (int i = 0; i < 2 * N; i++) { // //cerr << vp[i].first << " " << vp[i].second << endl; // int p = vp[i].second; // if (ft.sum(0, p) - 2 >= 0) { // ft.add(p, -2); // res += vp[i].first; // } // } cout << res << endl; }
/* author: Maksim1744 created: 20.02.2021 15:01:36 */ #include "bits/stdc++.h" using namespace std; #define ll long long #define ld long double #define mp make_pair #define pb push_back #define eb emplace_back #define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll)) #define mine(a) (*min_element((a).begin(), (a).end())) #define maxe(a) (*max_element((a).begin(), (a).end())) #define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin()) #define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin()) #define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) template<typename T> vector<T>& operator-- (vector<T>& v){for (auto& i : v) --i; return v;} template<typename T> vector<T>& operator++ (vector<T>& v){for (auto& i : v) ++i; return v;} template<typename T> istream& operator>>(istream& is, vector<T>& v){for (auto& i : v) is >> i; return is;} template<typename T> ostream& operator<<(ostream& os, vector<T>& v){for (auto& i : v) os << i << ' '; return os;} template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;} template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;} template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second; return is;} template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;} template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);} template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);} template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;} template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;} #ifdef HOME #define SHOW_COLORS #include "C:/C++ libs/print.cpp" #else #define show(...) 42 #define mclock 42 #define shows 42 #define debug if (false) #endif int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); auto is_up = [&](char c) { return c >= 'A' && c <= 'Z'; }; string s; cin >> s; for (int i = 0; i < s.size(); ++i) { if (is_up(s[i]) != i % 2) { cout << "No\n"; return 0; } } cout << "Yes\n"; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ string S;cin>>S; int l=S.size(); for(int i=0;i<l;i++){ if(i%2==0 && S[i]<='Z'){ cout<<"No"<<endl; return 0; } else if(i%2==1 && S[i]>='a'){ cout<<"No"<<endl; return 0; } } cout<<"Yes"<<endl; }
//I'll always miss you like a darling. #include <bits/stdc++.h> using namespace std; #define LL long long #define LD long double #define ull unsigned long long #define x first #define y second #define pb push_back #define pf push_front #define mp make_pair #define Pair pair<int,int> #define pLL pair<LL,LL> #define pii pair<double,double> #define LOWBIT(x) x & (-x) #define rep(i,a,b) for (int i=a;i<=b;i++) #define REP(i,a,b) for (int i=a;i>=b;i--) const int INF=2e9; const LL LINF=2e16; const int magic=348; const int MOD=1e9+7; const double eps=1e-10; const double pi=acos(-1); struct fastio { static const int S=1e7; char rbuf[S+48],wbuf[S+48];int rpos,wpos,len; fastio() {rpos=len=wpos=0;} inline char Getchar() { if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin); if (!len) return EOF; return rbuf[rpos++]; } template <class T> inline void Get(T &x) { char ch;bool f;T res; while (!isdigit(ch=Getchar()) && ch!='-') {} if (ch=='-') f=false,res=0; else f=true,res=ch-'0'; while (isdigit(ch=Getchar())) res=res*10+ch-'0'; x=(f?res:-res); } inline void getstring(char *s) { char ch; while ((ch=Getchar())<=32) {} for (;ch>32;ch=Getchar()) *s++=ch; *s='\0'; } inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;} inline void Writechar(char ch) { if (wpos==S) flush(); wbuf[wpos++]=ch; } template <class T> inline void Print(T x,char ch) { char s[20];int pt=0; if (x==0) s[++pt]='0'; else { if (x<0) Writechar('-'),x=-x; while (x) s[++pt]='0'+x%10,x/=10; } while (pt) Writechar(s[pt--]); Writechar(ch); } inline void printstring(char *s) { int pt=1; while (s[pt]!='\0') Writechar(s[pt++]); } }io; template<typename T> inline void check_max(T &x,T cmp) {x=max(x,cmp);} template<typename T> inline void check_min(T &x,T cmp) {x=min(x,cmp);} template<typename T> inline T myabs(T x) {return x>=0?x:-x;} template<typename T> inline T mygcd(T x,T y) {return y==0?x:mygcd(y,x%y);} inline int add(int x) {if (x>=MOD) x-=MOD;return x;} inline int add(int x,int MO) {if (x>=MO) x-=MO;return x;} inline int sub(int x) {if (x<0) x+=MOD;return x;} inline int sub(int x,int MO) {if (x<0) x+=MO;return x;} inline void Add(int &x,int y) {x=add(x+y);} inline void Add(int &x,int y,int MO) {x=add(x+y,MO);} inline void Sub(int &x,int y) {x=sub(x-y);} inline void Sub(int &x,int y,int MO) {x=sub(x-y,MO);} template<typename T> inline int quick_pow(int x,T y) {int res=1;while (y) {if (y&1) res=1ll*res*x%MOD;x=1ll*x*x%MOD;y>>=1;}return res;} template<typename T> inline int quick_pow(int x,T y,int MO) {int res=1;while (y) {if (y&1) res=1ll*res*x%MO;x=1ll*x*x%MO;y>>=1;}return res;} const int MAXN=3e5; int n; int a[MAXN+48]; namespace BIT { int c[MAXN+48]; void clear() {memset(c,0,sizeof(c));} void modify(int x) {while (x<=n) c[x]++,x+=LOWBIT(x);} int query(int x) {int res=0;while (x) res+=c[x],x^=LOWBIT(x);return res;} } int main () { #ifndef ONLINE_JUDGE double TIME=clock(); freopen ("a.in","r",stdin); freopen ("a.out","w",stdout); cerr<<"Running..."<<endl; #endif scanf("%d",&n);rep(i,1,n) scanf("%d",a+i),a[i]++;LL ans=0; BIT::clear();rep(i,1,n) ans+=(i-1)-BIT::query(a[i]),BIT::modify(a[i]); printf("%lld\n",ans); rep(i,1,n-1) { ans-=(a[i]-1); ans+=(n-a[i]); printf("%lld\n",ans); } #ifndef ONLINE_JUDGE cerr<<"Exec Time: "<<(clock()-TIME)/CLOCKS_PER_SEC<<endl; #endif return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for(ll i = 0; i < (ll)(n); ++i) #define FOR(i, a, b) for(ll i=(a); i < (ll)(b); ++i) template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;} template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;} const ll MOD = 998244353; ll calc_pow(ll x, ll n){ ll ret = 1; while(n>0){ if(n & 1) ret = ret*x%MOD; x = x*x%MOD; n >>= 1; } return ret; } int main(){ int n,m,k; cin >> n >> m >> k; ll ans=0; if(n==1 || m==1){ ans = calc_pow(k, n*m); cout << ans << endl; return 0; } FOR(a,1,k+1){ ll tmp; tmp = ((calc_pow(a, n) - calc_pow(a-1, n))%MOD + MOD)%MOD; tmp = (tmp*calc_pow(k+1 - a, m))%MOD; ans = (ans+tmp)%MOD; } cout << ans << endl; return 0; }
#pragma GCC optimize("Ofast") #include <iostream> #include <stdio.h> #include <string> #include <vector> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <stack> #include <deque> #include <queue> #include <list> #include <set> #include <map> #include <unordered_map> using namespace std; using ll = long long; using P = pair<ll, ll>; using Po = pair<double, double>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #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=ll(a);i<=ll(b);i++) #define FORD(i,a,b) for(ll i=ll(a);i>=ll(b);i--) #define PB push_back #define MP make_pair #define SZ(x) ll(x.size()) #define ALL(x) x.begin(),x.end() #define NIL -1 #define INF pow(2,31)-1 #define MOD 998244353 #define PI 3.14159265358979323846 #define endl "\n" #define EPS 1e-9 template <class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; } template <class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; } ll powmod(ll x, ll n, ll mod) { ll s = 1; REP(i, n) { s = s * x % mod; } if (s < 0)s += mod; return s; } ll gcd(int p, int q) { while (q != 0) { ll r = p % q; p = q; q = r; } return p; } V<ll> v(200001); signed main() { ll a, b, c, d; cin >> a >> b >> c >> d; cout << a * d - b * c << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ar array #define sz(v) (int)(v).size() #define fi first #define se second #define pb push_back #define rep(i,a,b) for(int i = (a) ; i<(b) ; ++i) const int mxN = 2e5; const ll MOD = 998244353; map<char,int> mp; int n; vector<ll> c[3]; ll findmin(int a, int b) { ll ans = 1e18; rep(i,0,sz(c[a])) { int pos = lower_bound(c[b].begin(),c[b].end(),c[a][i]) - c[b].begin(); if(pos<sz(c[b])) { ans = min(ans,llabs(c[a][i]-c[b][pos])); } if(pos-1>=0) { ans = min(ans,llabs(c[a][i]-c[b][pos-1])); } } return ans; } int main() { mp['R'] = 0; mp['G'] = 1; mp['B'] = 2; cin>>n; rep(i,0,2*n) { char cl; ll x;cin>>x>>cl; c[mp[cl]].pb(x); } sort(c[0].begin(),c[0].end()); sort(c[1].begin(),c[1].end()); sort(c[2].begin(),c[2].end()); if(!(sz(c[0])&1) && !(sz(c[1])&1) && !(sz(c[2])&1)) return cout << 0 << "\n",0; if(!(sz(c[0])&1)) swap(c[0],c[2]); if(!(sz(c[1])&1)) swap(c[1],c[2]); cout << min(findmin(0,1),findmin(0,2) + findmin(1,2)) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int ll const int N = 50 + 7; int n; int y; int a[N]; int b[N]; int valuey[N]; int dp[N][2]; signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> y; for (int i = 1; i <= n; i++) { cin >> a[i]; } a[n + 1] = (int) 3e18 / a[n] * a[n]; for (int i = 1; i <= n; i++) { b[i] = a[i + 1] / a[i]; } for (int i = n; i >= 1; i--) { valuey[i] = y / a[i]; y %= a[i]; } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= 1; j++) { dp[i][valuey[i] + j >= b[i]] += dp[i - 1][j]; int value = dp[i - 1][j]; int x = b[i] - j - valuey[i]; if (1 <= x && x < b[i]) { dp[i][(x + j + valuey[i]) >= b[i]] += value; } } } cout << dp[n][0] << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long lli; int MOD = 998244353; inline int add(const int a, const int b, const int mod = MOD) { return (a + b >= mod) ? (a + b - mod) : (a + b); } inline void _add(int &a, const int b, const int mod = MOD) { a = add(a, b, mod); } inline int sub(const int a, const int b, const int mod = MOD) { return (a - b < 0) ? (a - b + mod) : (a - b); } inline void _sub(int &a, const int b, const int mod = MOD) { a = sub(a, b, mod); } inline int negate(const int a, const int mod = MOD) { return mod - a; } inline int mul(const int a, const int b, const int mod = MOD) { return (a * 1ll * b) % mod; } inline void _mul(int &a, const int b, const int mod = MOD) { a = mul(a, b, mod); } int binPow(int b, lli p, const int mod = MOD) { int r = 1; while (p) { if (p & 1) r = mul(r, b, mod); b = mul(b, b, mod); p >>= 1; } return r; } int gcdex(int a, int b, int &x, int &y) { if (!b) return x = 1, y = 0, a; int px, py, res = gcdex(b, a % b, px, py); return x = py, y = px - a / b * py, res; } int inv(const int a, const int mod = MOD) { // a and mod must be coprime int x, y; int g = gcdex(a, mod, x, y); assert(g == 1); x = (x % mod + mod) % mod; return x; } inline int dvd(const int a, const int b, const int mod = MOD) { // b and mod must be coprime int rev = inv(b, mod); return mul(a, inv(b, mod), mod); } // b != 0 inline void _dvd(int &a, const int b, const int mod = MOD) { a = dvd(a, b, mod); } // b and mod must be coprime inline int convert(int x, const int mod = MOD) { int tx = x % mod + mod; return tx >= mod ? tx - mod : tx; }; const int MB = 13; const int MX = 5005; vector<int> f, rf; vector<vector<int>> dp; int n, m; int C(int n, int k) { if (k < 0 || k > n) return 0; return mul(f[n], mul(rf[k], rf[n - k])); } int getDp(int sum, int bit) { if (sum == 0) return 1; if (bit == -1) return 0; if (dp[sum][bit] != -1) return dp[sum][bit]; int one = 1 << bit; dp[sum][bit] = 0; for (int take = 0; 1ll * take * one <= sum && take <= n; take += 2) { _add(dp[sum][bit], mul(C(n, take), getDp(sum - take * one, bit - 1))); } return dp[sum][bit]; } int main () { f.assign(MX, 0); rf.assign(MX, 0); f[0] = 1; for (int i = 1; i < MX; i++) f[i] = mul(f[i - 1], i); rf[MX - 1] = 996779413; for (int i = MX - 2; i >= 0; i--) rf[i] = mul(rf[i + 1], i + 1); while (scanf("%d %d", &n, &m) == 2) { dp.assign(m + 1, vector<int>(MB + 1, -1)); int res = getDp(m, MB); printf("%d\n", res); } }
// author : redwackyferret #include<bits/stdc++.h> #define pb push_back #define pdd pair<ld, ld> #define int long long #define ff first #define pii pair<int,int> #define ss second #define pp make_pair #define endl "\n" #define ll long long #define ld long double #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++) #define PI 3.1415926535 #define mst(v, x) memset(v, x, sizeof v); #define all(v) v.begin(), v.end() // __builtin_clz(x) returns num of zeroes at beginning of bit representation // __builtin_ctz(x) num of zeroes at end of bit representation // __builtin_popcount(x) num of ones // __bultin_parity(x) parity of num of ones in bit representation using namespace std; void IOS(){ios::sync_with_stdio(false); cout.precision(10); cout << fixed;} const int MOD = 1e9 + 7; #define INF 2000000000 const int mx = 1e5+5; int i, j, k; ll gcd(ll x, ll y) { if (y == 0)return x; return gcd(y, x % y); } ll lcm(ll x, ll y){ return (x*y)/gcd(x,y); } struct st{ int a, b, c; }; int32_t main(){ IOS(); int n,C; cin>>n>>C; vector<pair<int,int>>event; REP(i,n){ int a,b,c; cin>>a>>b>>c; event.emplace_back(a-1,c); event.emplace_back(b,-c); } int ans = 0; sort(all(event)); int fee = 0, t=0; for(auto z : event){ int x = z.ff, y = z.ss; if(x!=t){ ans += min(C, fee)*(x-t); t=x; } fee+=y; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #ifndef M_PI #define M_PI 3.14159265358979 #endif #define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI) #define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360) using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<int> vi; typedef pair<ll, ll> pll; typedef pair<int, int> pi; typedef long double ld; typedef pair<long double, long double> pld; const ll INF = 1e18; const ll MOD = 1e9 + 7; // べき乗の剰余(a^b mod MOD) ll bpow(ll a, ll b) { ll res = 1; while(b) { if(b & 1) res = 1LL * res * a % MOD; a = 1LL * a * a % MOD; b >>= 1; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll i, j; ll N, C; cin >> N >> C; vll va, vb, vc; for(i = 0; i < N; i++) { ll a, b, c; cin >> a >> b >> c; va.push_back(a); vb.push_back(b); vc.push_back(c); } vector<pll> P; for(i = 0; i < N; i++) { P.push_back(pll(va[i], vc[i])); P.push_back(pll(vb[i] + 1, -1 * vc[i])); } sort(P.begin(), P.end()); ll ans = 0; ll tc = 0; ll now = P[0].first; ll pi = 0; while(1) { while(P[pi].first == now) { tc += P[pi].second; pi++; if(pi >= 2 * N) break; } if(pi >= 2 * N) break; ll ttc; if(tc > C) { ttc = C; } else { ttc = tc; } ans += ttc * (P[pi].first - now); now = P[pi].first; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; class union_find{ int n; vector<int> p; vector<map<int,int>> f; // color -> number of vertices public: union_find(int n,vector<int> color):n(n),p(n,-1),f(n){ rep(u,n) f[u][color[u]]=1; } int find(int u){ return p[u]<0?u:p[u]=find(p[u]); } void unite(int u,int v){ u=find(u); v=find(v); if(u!=v){ if(p[v]<p[u]) swap(u,v); p[u]+=p[v]; p[v]=u; n--; for(auto [c,k]:f[v]) f[u][c]+=k; f[v].clear(); } } bool is_same(int u,int v){ return find(u)==find(v); } int size()const{ return n; } int size(int u){ return -p[find(u)]; } int query(int u,int color){ return f[find(u)][color]; } }; int main(){ int n,q; scanf("%d%d",&n,&q); vector<int> color(n); rep(u,n) scanf("%d",&color[u]), color[u]--; union_find U(n,color); rep(_,q){ int type,a,b; scanf("%d%d%d",&type,&a,&b); a--; b--; if(type==1) U.unite(a,b); else printf("%d\n",U.query(a,b)); } return 0; }
#include <stdio.h> #include <set> #include <vector> #include <utility> #define mp make_pair using namespace std; typedef long long ll; typedef pair<ll, ll> pl; int main(void) { ll i, j, k, n, m, a, b, c, ans; scanf("%lld%lld", &n, &m); vector<pl> vec[n]; for(i = 0; i < m; ++i) { scanf("%lld%lld%lld", &a, &b, &c); vec[--a].push_back(mp(--b, c)); } ll d[n], t; pl p; for(i = 0; i < n; ++i) { ans = 1e9; set<pl> s; for(j = 0; j < n; ++j) d[j] = 1e9; d[i] = 0; for(j = 0; j < n; ++j) s.insert(mp(d[j], j)); while(s.size()) { p = *s.begin(), s.erase(s.begin()), t = p.second; for(j = 0; j < vec[t].size(); ++j) { if(vec[t][j].first == i && ans > d[t] + vec[t][j].second) ans = d[t] + vec[t][j].second; else if(d[vec[t][j].first] > d[t] + vec[t][j].second) { s.erase(mp(d[vec[t][j].first], vec[t][j].first)); d[vec[t][j].first] = d[t] + vec[t][j].second; s.insert(mp(d[vec[t][j].first], vec[t][j].first)); } } } printf("%lld\n", ans == 1e9 ? -1 : ans); } return 0; }
#include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i, n) for(long long i = 0; i < n; i++) #define rep1(i, n) for(long long i = 1; i <= n; i++) #define rrep(i, n) for(long long i = n; i > 0; i—) #define rrep1(i, n) for(long long i = n; i >= 0; i—) //char型 10進数で48が'0' int main() { ll n,sum=0,total=0,ans=0; cin >> n; vector<ll> v(n); rep(i, n){ ll a,b; cin >> a >> b; v[i] = a*2 + b; total += a; } sort(v.begin(),v.end()); for(long long i = n - 1; sum <= total; i--){ sum += v[i]; ans += 1; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int n,a[200005],b[200005]; long long ans; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[a[i]]=a[i]; } for(int i=1;i<=n;i++){ int j=n-i+1; while(b[b[a[i]]]!=b[a[i]]){ b[a[i]]=b[b[a[i]]]; } while(b[b[a[j]]]!=b[a[j]]){ b[a[j]]=b[b[a[j]]]; } if(b[a[i]]!=b[a[j]]){ b[b[a[j]]]=b[a[i]]; ans++; } } printf("%lld",ans); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Kein Yukiyoshi */ #include "bits/stdc++.h" #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i < (int)(b); 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) #define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key)) #define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key)) using namespace std; const int MOD = (int)(1e9 + 7); const int INF = (int)(1e13 + 7); const double EPS = 1e-14; const double PI = acos(-1); int CEIL(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } //ceil() for int int mod(int a, int b) { return a >= 0 ? a % b : a - (b * CEIL(a, b)); } //always return positive num int bpm(int a, int b) { //return x^y in order(log(y)) int res = 1; for (a %= MOD; b; a = a * a % MOD, b >>= 1) if (b & 1) res = res * a % MOD; return res; } int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); } class BMAXMin { public: static void solve(istream &cin, ostream &cout) { int N; cin>>N; vector<int> a(N); rep(i, N)cin>>a[i]; rep(i, N-1){ a[i+1] = gcd(a[i], a[i+1]); } cout<<a[N-1]<<endl; } }; signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(15); BMAXMin 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 long double ld; typedef vector<vector<int>> Graph; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; #define pi 3.14159265359 #define inf 2147483647 #define INF 9223372036854775807 #define mod 1000000007 #define mod2 998244353 int gcd(int n, int m) { if(n < m) swap(n, m); if(n % m == 0) return m; return gcd(m, n % m); } int main() { int N; cin >> N; vector<int> a(N); for(int i = 0; i < N; i++) cin >> a[i]; int G = a[0]; for(int i = 1; i < N; i++) G = gcd(G, a[i]); cout << G << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define pi 2*acos(0.0) #define mod 1000000007 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define pii pair<ll,ll> #define pb push_back #define Bit_off(val,pos) (val & (~(1<<pos))) #define Bit_on(val,pos) (val | (1<<pos)) #define Bit_toggle(val,pos) (val ^ (1<<pos)) #define Bit_check(val,pos) (bool)(val & (1<<pos)) #define odd_even(val) (bool)(val & 1) #define koita_1_ase(val) (__builtin_popcount(val)) //ll Big_Mod(ll v1, ll p, ll md) { ll res=1; v1=v1%md; while (p > 0){if (p & 1)res = (res * v1) % md; p =p>>1; v1 = (v1 * v1) % md;}return res;} ll ar[500009],cr[500009],br[500009],tr[500009]; ll fx[]= {-1,1,0,0}; ll fy[]= {0,0,1,-1}; map<ll,ll>mp,mp1,mp2; vector<ll>v[500009],v1; string s,q; struct evan { ll l,r,w; } st[500009]; bool com(evan a,evan b) { return a.r<b.r; } int main() { fast; ll a,b,c,d,e,i,j,k,x,y,z,f,g,h,n,m; cin>>n>>x>>s; for(i=0; i<n; i++) { if(s[i]=='o') { x++; } else { if(x) { x--; } } } cout<<x<<endl; }
#include<map> #include<ctime> #include<cstdio> #include<cctype> #include<string> #define ll long long const ll N=1e3+10; const ll HgS=1e9+7; using namespace std; ll read() { char c; ll x=0,f=1; while(!isdigit(c=getchar())) f-=2*(c=='-'); while (isdigit(c)){ x=x*10+(c-48)*f; c=getchar(); } return x; } ll n,f,res; ll fib(ll n){ ll a=0,b=1,c=0; for(ll i=1;i<=n;++i){ c=(a+b)%HgS; a=b; b=c; } return a; } int main() { #ifndef ONLINE_JUDGE freopen("D.in","r",stdin); freopen("D.out","w",stdout); #endif clock_t t1=clock(); //-------- n=read(); for(int i=0;i<4;++i){ char c; while(!isalpha(c=getchar())); f<<=1; f|=(c=='B'); } switch(f){ case 0: case 1: case 2: case 3: case 5: case 7: case 13: case 15: res=1; break; case 4: case 10: case 11: case 12: res=1; for(int i=4;i<=n;++i){ res<<=1; res%=HgS; } break; default: res=fib(n-1); break; } printf("%lld",res); //-------- clock_t t2=clock(); fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC); return 0; }
#include<bits/stdc++.h> using namespace std; #define nl '\n' typedef long long ll; #define f(i,start,end) for(i=start;i<=end;i++) #define pii pair<int,int> #define pll pair<ll,ll> #define piii pair<pii,int> #define F first #define S second #define min_heap priority_queue<ll, vector<ll>, greater<ll> > #define max_heap priority_queue<ll> #define min_heap_pair priority_queue<pii, vector<pii>, greater<pii> > #define eb emplace_back #define mapGreater map<int, int, greater<int> > //#define pi 3.14159265 #define printAr(a, n) f(i, 0, n-2) cout << a[i] << ' '; cout << a[n-1] << nl; // Implementation of Ordered Set - PBDS /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > ordered_set; */ inline bool pair_cmp(pii p1, pii p2) { if(p1.S!=p2.S) return p1.S<p2.S; return p1.F<p2.F; } const int64_t INF = 1e18+7; inline int64_t mul(int64_t a, int64_t b){ return (INF/a>b? a*b: INF); } inline int64_t add(int64_t a, int64_t b){ return (INF-a>b? a+b: INF); } //sieve of eratosthenes bool p[1000001]; void sieve() { //all odds + falses are primes (>= 2) for(int i=4; i<=1000000; i+=2) p[i] = 1; for(int i=3; i*i<=1000000; i+=2) { if(!p[i]) { for(int j=i*i; j<=1000000; j+=(2*i)) { p[j] = 1; } } } } void solve() { int i=0, j=0; string s; cin >> s; int n = s.size(); f(i, 0, n-4+1) { if(s.substr(i,4)=="ZONe") j++; } cout << j << nl; } int main() { ios::sync_with_stdio(0); cin.tie(0); //fastio; //sieve(); //int t=0,tc=0; cin >> tc; for(t=1; t<=tc; ++t) { //cout << "Case " << t << ": "; solve(); //if(t<tc) cout << '\n'; } return 0; }
#include<bits/stdc++.h> typedef long long ll; using namespace std; int main() { string str; cin >> str; int num=0; int index=0; string sub="ZONe"; while( (index=str.find(sub,index)) < str.length() ){ num++; index++; } cout << num << 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 ll mod = 1e9 + 7; void add(ll &x, ll y) { x %= mod; y %= mod; x += y; x %= mod; } ll pw(ll x, ll y) { ll res=1; while (y) { if (y%2) res=res*x%mod; x=x*x%mod;y=y/2; } return ((res%mod)+mod)%mod; } const int maxn = 1005; int A = 0; int B = 1; int n; int AA, AB, BA, BB; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; char tmp; cin>>tmp; AA=int(tmp-'A'); cin>>tmp; AB=int(tmp-'A'); cin>>tmp; BA=int(tmp-'A'); cin>>tmp; BB=int(tmp-'A'); if (n==2) out(1); assert(n>=3); if (AB==B) { swap(AA, BB); swap(A, B); } assert(AB == A); if (AA==A) out(1); assert(AB == A && AA == B); if (BA == A) { // BB isn't possible vector<ll> dp(n+1, 0); dp[0]=1; dp[1]=1; dp[2]=2; for (int i=2; i<=n; i++) { dp[i]=(dp[i-1]+dp[i-2])%mod; } //A....B out(dp[n-2]); } assert(AB == A && AA == B && BA == B); //A.....AB //second to last character has to be A. cout<<pw(2,n-3)<<endl; return 0; }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstdlib> #include <cstring> #include <cmath> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <complex> #include <vector> #include <limits> #include <iomanip> #include <cassert> #include <numeric> #include <chrono> #include <random> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define debug(x) cerr << #x << " = " << (x) << endl; #define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")"; #define rep(i, n) for(int i = 0;i < n;i++) #define pb push_back #define F first #define S second // template<typename _Ty1, typename _Ty2> // ostream& operator<<(ostream& _os, const pair<_Ty1, _Ty2>& _p) { // _os << '(' <<_p.first << ',' << _p.second << ')'; // return _os; // } // // template<typename _Ty1, typename _Ty2> // istream& operator>>(istream& _is, pair<_Ty1, _Ty2>& _p) { // _is >> _p.first >> _p.second; // return _is; // } int n,m; const ll mod = 998244353; vector<ll> inv{0, 1}; // ith index will store (1/i)%mod vector<ll> finv{1, 1}; // ith index will store (1/i!)%mod vector<ll> fact{1, 1}; // ith index will store (i!)%mod void populate2(int n) { for(int i = 2;i < n;i++) { fact.pb((i*fact[i-1])%mod); inv.pb(mod-((inv[mod%i]*(mod/i))%mod)); finv.pb((finv[i-1]*inv[i])%mod); } } ll P(int n, int r) { return fact[n]*finv[n-r]%mod; } ll C(int n, int r) { if(n < r) return 0; return P(n, r)*finv[r]%mod; } ll dp[13][5001]; void solve() { cin>>n>>m; populate2(n+10); // dp[bit][sum] int bit = 12; dp[bit][0]=1; for(bit -= 1;bit >= 0;bit--) { for(int j = 0;j <= m;j++) { int k = 0; for(;k <= n;k+=2) { if(j - (1<<bit)*1LL*k >= 0) { dp[bit][j] += dp[bit+1][j - (1<<bit)*1LL*k]*C(n,k)%mod; dp[bit][j] %= mod; } } } } cout<<dp[0][m]<<endl; } // 2 10 5 15 30 // ABABABA A // + - - - - + - - - - - + - - - - - + - - - - - + - - - == N // M // + 1 // ++ // +++ // ++++ // +++++ // ++++++ // . // . (i) // . // +++++++++++++ 20 // // dp[i][val] // // at max 20 distinct # in the sequence int main() { // freopen("input.in","r",stdin); // freopen("output.out","w",stdout); // cout << fixed << setprecision(15); ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(){ long N,K; cin >> N >> K; rep(i,K){ if(N%200 ==0){ N /=200; } else{ N = N*1000+200; } } cout << N << endl; }
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define w(t) int t;cin>>t;while(t--) #define int long long #define fr(i,a,b) for(int i=a;i<=b;i++) #define rep(i,a,b) for(int i=a;i<b;i++) #define pb push_back #define ppb pop_back #define mp make_pair #define all(x) x.begin(),x.end() #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> #define vvi vector<vi> #define pqi priority_queue<int> #define pqd priority_queue<int,vi,greater<int>> #define inf 1e18 #define mod 1000000007 #define PI 3.14159265358979323846264338327950288419716939937510 int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n,k; cin>>n>>k; rep(i,0,k){ if(n%200==0) n/=200; else{ n*=1000; n+=200; } } cout<<n<<endl; return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <iostream> using namespace std; typedef long long ll; ll mod = 998244353,dp[5010][5010] = {},pw[10020],cnt[10010] = {}; char c[5010][5010]; int main(){ int i,j,h,w,k; cin >> h >> w >> k; for(i=0;i<h;i++){ for(j=0;j<w;j++){ c[i][j] = 's'; cnt[i + j]++; } } pw[0] = 1; for(i=1;i<=5010;i++) pw[i] = 3*pw[i - 1]%mod; for(i=0;i<k;i++){ int x,y; cin >> x >> y; x--; y--; char ch; cin >> ch; c[x][y] = ch; cnt[x + y]--; } dp[0][0] = 1; for(i=0;i<h;i++){ for(j=0;j<w;j++){ if(c[i][j]=='s'){ (dp[i + 1][j] += 2*pw[cnt[i + j] - 1]%mod*dp[i][j]%mod) %= mod; (dp[i][j + 1] += 2*pw[cnt[i + j] - 1]%mod*dp[i][j]%mod) %= mod; } if(c[i][j]=='R'){ (dp[i][j + 1] += pw[cnt[i + j]]*dp[i][j]%mod) %= mod; } if(c[i][j]=='D'){ (dp[i + 1][j] += pw[cnt[i + j]]*dp[i][j]%mod) %= mod; } if(c[i][j]=='X'){ (dp[i + 1][j] += pw[cnt[i + j]]*dp[i][j]%mod) %= mod; (dp[i][j + 1] += pw[cnt[i + j]]*dp[i][j]%mod) %= mod; } } } if(c[h - 1][w - 1]=='s') (dp[h - 1][w - 1] *= 3) %= mod; cout << dp[h - 1][w - 1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() constexpr ll MOD=998244353; constexpr ll INF=1e18; ll power(ll x, ll y){ x%=MOD; ll ret=1; while(y){ if(y&1) ret=ret*x%MOD; x=x*x%MOD; y>>=1; } return ret; } int main(){ int h, w, k; cin >> h >> w >> k; vector<vector<char>> f(h,vector<char>(w,'.')); int x, y; char c; REP(i,k){ cin >> x >> y >> c; x--, y--; f[x][y]=c; } VVI dp(h+1,VI(w+1,0)); dp[0][0]=power(3,h*w-k); ll p=power(3,MOD-2); REP(i,h)REP(j,w){ if(f[i][j]=='.'&&!(i==0&&j==0)) dp[i][j]=dp[i][j]*p%MOD; if(f[i][j]=='.'){ dp[i+1][j]=(dp[i+1][j]+dp[i][j]*2)%MOD; dp[i][j+1]=(dp[i][j+1]+dp[i][j]*2)%MOD; } else if(f[i][j]=='R'){ dp[i][j+1]=(dp[i][j+1]+dp[i][j])%MOD; } else if(f[i][j]=='D'){ dp[i+1][j]=(dp[i+1][j]+dp[i][j])%MOD; } else{ dp[i+1][j]=(dp[i+1][j]+dp[i][j])%MOD; dp[i][j+1]=(dp[i][j+1]+dp[i][j])%MOD; } } cout << dp[h-1][w-1] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long string s; int n,k; int C[18][19]; const int mod=1e9+7; int cnt[190]; int ans=0; char base[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; const int N=2e5+5; int dp[N][18][18]; int poww(int a,int b,int c){ int ans=1,base=a; while(b){ if(b&1)ans=1ll*ans*base%c; b>>=1; base=1ll*base*base%c; } return ans; } signed main(){ cin>>s; cin>>k; for(int i=0;i<18;i++)dp[0][0][i]=1; for(int kk=0;kk<18;kk++) for(int i=1;i<N;i++) for(int j=0;j<=kk;j++){ dp[i][j][kk]=(dp[i-1][j][kk]*1ll*(j+k-kk)+(j>0?dp[i-1][j-1][kk]*1ll*(kk-(j-1)):0))%mod; } n=s.size(); for(int i=0;i<18;i++)C[i][0]=1; for(int i=1;i<18;i++) for(int j=1;j<=i;j++)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; int now=0; for(int i=0;i<n;i++){ for(int j=(i==0?1:0);j<16;j++)if(base[j]<s[i]){ int gg=now+(cnt[base[j]]==0); if(gg<=k)ans=(ans+1ll*C[16-gg][k-gg]*dp[n-(i+1)][k-gg][k-gg])%mod; // cout<<i<<" "<<n-i-1<<" "<<k-gg<<" "<<dp[n-i-1][k-gg]<<endl; }else break; cnt[s[i]]++; if(cnt[s[i]]==1)now++; // cout<<i<<" "<<ans<<endl; } if(now==k)ans++; //cout<<ans<<endl; int some=15ll*C[15][k-1]%mod; for(int i=1;i<n;i++){ ans=(ans+1ll*some*dp[n-(i+1)][k-1][k-1])%mod; } cout<<ans<<endl; return 0; }
# include <stdio.h> # include <string.h> using namespace std; const int mod = 1000000007; const int kMaxN = 200200; int k, n; char s[kMaxN]; long long a[kMaxN]; long long dp[kMaxN][20]; int Cnt(int x) { int cnt = 0; while (x) { cnt += x & 1; x >>= 1; } return cnt; } int main() { scanf("%s%d", s, &k); int n = strlen(s); for (int i = 0; i < n; i++) { if (s[i] >= 'A' && s[i] <= 'F') { a[i] = s[i] - 'A' + 10; } else { a[i] = s[i] - '0'; } } dp[1][1] = a[0] - 1; long long num = 1ll << a[0]; for (int i = 1; i < n; i++) { for (int j = 1; j <= 16; j++) { dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * j) % mod; dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (16 - j)) % mod; } dp[i + 1][1] = (dp[i + 1][1] + 15) % mod; for (int j = 0; j < a[i]; j++) { dp[i + 1][Cnt(num | 1ll << j)] = (dp[i + 1][Cnt(num | 1ll << j)] + 1) % mod; } num |= 1ll << a[i]; } if (Cnt(num) == k) { printf("%lld", (dp[n][k] + 1) % mod); } else { printf("%lld", dp[n][k]); } }
#include <bits/stdc++.h> using namespace std; using ll=long long; ll mod=998244353; int main(){ int H,W; cin>>H>>W; vector<string> S(H); for(int i=0;i<H;i++) cin>>S[i]; vector<int> r(H+W,0),b(H+W,0),w(H+W,0); for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(S[i][j]=='R') r[i+j]++; if(S[i][j]=='B') b[i+j]++; if(S[i][j]=='.') w[i+j]++; } } ll ans=1; for(int i=0;i<H+W;i++){ if(r[i]>0&&b[i]>0) ans=0; if(w[i]>0&&r[i]==0&&b[i]==0) ans*=2; ans%=mod; } cout<<ans<<endl; return 0; }
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <random> #include <bitset> #include <numeric> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " int main() { ios_base::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; vector<string> s(h); rep(i, 0, h) { cin >> s[i]; } const int MOD = 998244353; ll res = 1; rep(i, 0, w + h - 1) { // printf("for sum: %d:\n", i); int r1 = i < h ? i : h - 1; int c1 = i - r1; bool has_dot = false, has_r = false, has_b = false; rep(j, 0, min(r1 + 1, w - c1)) { int nr1 = r1 - j; int nc1 = c1 + j; // printf("checking: %d, %d\n", nr1, nc1); if (s[nr1][nc1] == 'R') { has_r = true; } else if (s[nr1][nc1] == 'B') { has_b = true; } else { has_dot = true; } } if (has_r && has_b) { cout << 0 << endl; return 0; } if (!has_r && !has_b) { res = (res * 2) % MOD; } } cout << res << endl; }
// #include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <map> #include <queue> #include <set> #include <tuple> #include <cmath> #include <iomanip> #include <numeric> #define ll long long int #define rep(i, n) for(ll i = 0; i < (ll)(n); i++) #define drep(i, n) for(ll i = ((ll)(n) - 1); i >= 0; i--) #define frep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); i++) #define vall(v) (v).begin(),(v).end() #define mod 1000000007 #define inf 9223372036854775800 using namespace std; ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// int main() { ll n; cin >> n; set<ll> ans; ans.insert(6); ans.insert(10); ans.insert(15); ll c; c = 2; while(n > ans.size()){ if(6*c > 10000){ break; } ans.insert(6*c); c++; } c = 2; while(n > ans.size()){ if(10*c > 10000){ break; } ans.insert(10*c); c++; } c = 2; while(n > ans.size()){ if(15*c > 10000){ break; } ans.insert(15*c); c++; } for(auto itr = ans.begin(); itr != ans.end(); itr++){ cout << *itr << " "; } cout << endl; return 0; } //////////////////////////////////////////////////
#include<iostream> using namespace std; int main() { int N; cin >> N; int A = 1, B = 1, C = 1; for(int i = 0; i < N - 3; i++) { if(A <= 1600) A++; else if (B <= 600) B++; else C++; } for (int i = 1; i <= A; i++) { if (i > 1) cout << " "; cout << 6 * i; } int x = 10; for (int i = 1; i <= B; i++) { cout << " " << x; x += 10; if (x % 3 == 0) x += 10; } x = 15; for (int i = 1; i <= C; i++) { cout << " " << x; x += 15; if (x % 2 == 0) x += 15; } cout << endl; }
#include<bits/stdc++.h> #define ll long long int #define M 1000000007 #define mod 998244353 #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define pi pair<ll,ll> #define endl "\n" using namespace std; const ll N=200010; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,m,x,y; cin>>n>>m>>x>>y; vector<pi> val; vector<pi> adj[n+1]; for(ll i=0;i<m;++i){ ll a,b,t,k; cin>>a>>b>>t>>k; adj[a].pb(mp(b,i)); adj[b].pb(mp(a,i)); val.pb(mp(t,k)); } vector<ll> dis(n+1,LLONG_MAX); set<pi> s; s.insert(mp(0,x)); while(!s.empty()){ ll v=(*s.begin()).second; ll d=(*s.begin()).first; s.erase(s.begin()); if(d>=dis[v]){ continue; } dis[v]=d; for(auto x:adj[v]){ ll u=x.first; ll idx=x.second; ll k=val[idx].second; ll t=val[idx].first; ll cost=d+t; if(d%k!=0){ cost+=(k-d%k); } if(cost<dis[u]){ s.insert(mp(cost,u)); } } } if(dis[y]==LLONG_MAX){dis[y]=-1;} cout<<dis[y]; return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; 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,cc,n) for(int i=cc;i<n;++i) #define lrep(i,cc,n) for(long long i=cc;i<n;++i) #define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i) #define rrep(i,cc,n) for(int i=cc;i>=n;--i) #define pii pair<int, int> #define pll pair<long long, long long> using ll = long long; const vector<int> dx = {1, 0, 1}; const vector<int> dy = {0, 1, 1}; const vector<int> dx2 = {1, 1, 1, 0, 0, 0, -1, -1, -1}; const vector<int> dy2 = {1, 0, -1, 1, -1, 0, 1, 0, -1}; const double PI = 3.1415926535; const ll inf = 1001001001; const ll e9 = 1000000000; const ll mod = 1000000007; const ll mod2 = 998244353; const int MAX = 1000000; const int MOD = 1000000007; const ll big = (1ll<<60); ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; } struct Edge { ll to; ll cost; ll k; }; int main(){ int n, m, x, y; cin >> n >> m >> x >> y; x--,y--; vector<vector<Edge>>G(n); rep(i, 0, m){ ll a, b; ll ti, ki; cin >> a >> b >> ti >> ki; a--,b--; Edge ed1, ed2; ed1.to = b; ed1.cost = ti; ed1.k = ki; G[a].push_back(ed1); ed2.to = a; ed2.cost = ti; ed2.k = ki; G[b].push_back(ed2); } priority_queue<pll,vector<pll>,greater<pll>>que; que.emplace(0,x); vector<ll>time(n, big); vector<bool>visit(n,false); time[x] = 0; while(!que.empty()){ pll temp = que.top(); que.pop(); if(visit[temp.second])continue; visit[temp.second] = true; if(time[temp.second]<temp.first)continue; for(auto p:G[temp.second]){ ll temp_time = ((temp.first+p.k-1)/p.k)*p.k + p.cost; if(time[p.to]>temp_time){ time[p.to] = temp_time; que.push({time[p.to],p.to}); } } } if(time[y]==big){ cout << -1 << endl; }else cout << time[y] << endl; }
#include <iostream> using namespace std; using ll = long long; int MOD = 1e9 + 7; ll my_pow(ll x, ll n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return my_pow(x * x % MOD, n >> 1); } else { return x * my_pow(x, n - 1) % MOD; } } int main(void){ int N, P; cin >> N >> P; ll ans = P - 1; ans = ans * my_pow(P - 2, N - 1) % MOD; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<char> vc; typedef queue<ll> ql; typedef deque<ll> dql; typedef priority_queue<ll> pql; typedef set<ll> sl; typedef pair<ll, ll> pl; typedef map<ll, ll> ml; typedef vector<vl> vvl; typedef vector<pl> vpl; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++) #define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--) #define all(v) (v).begin(), (v).end() ll mod(ll a, ll b) {return (a % b + b) % b;} ll quo(ll a, ll b) {return (a - mod(a, b)) / b;} template <typename T, typename U> bool chmin(T &a, const U b) {if(a > b) {a = b; return 1;} return 0;} template <typename T, typename U> bool chmax(T &a, const U b) {if(a < b) {a = b; return 1;} return 0;} const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const ll MAX = 2e5; const ld eps = 1e-9; const char newl = '\n'; struct mint{ ll val; mint() {} mint(ll x) {val = mod(x, MOD);} mint operator-() {return mint(-this->val);} bool operator==(mint a) {return this->val == a.val;} bool operator<(mint a) {return this->val < a.val;} bool operator!=(mint a) {return this->val != a.val;} mint operator+(mint a) {return mint(this->val + a.val);} mint operator-(mint a) {return mint(this->val - a.val);} mint operator*(mint a) {return mint(this->val * a.val);} mint operator/(mint a) {return mint(this->val * a.inv().val);} mint &operator+=(mint a) {return *this = *this + a;} mint &operator-=(mint a) {return *this = *this - a;} mint &operator*=(mint a) {return *this = *this * a;} mint &operator/=(mint a) {return *this = *this / a;} friend istream &operator>>(istream &is, mint &m) { is >> m.val; return is; } friend ostream &operator<<(ostream &os, mint &m) { os << m.val; return os; } mint pow(ll n) { if(n == 0) return 1; if(n & 1) return pow(n-1) * *this; mint ret = pow(n/2); return ret * ret; } vector<mint> powv(ll n) { vector<mint> vec(n+1, 1); rep2(i, 1, n) vec[i] = vec[i-1] * *this; return vec; } mint inv() {return pow(MOD-2);} vector<mint> invv(ll n) { vector<mint> vec(n+1, 1); rep2(i, 2, n) vec[i] = -vec[MOD%i] * (MOD/i); return vec; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, p; cin >> n >> p; mint ans = mint(p-1)*(mint(p-2).pow(n-1)); cout << ans << newl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; #define pow(n,m) powl(n,m) #define sqrt(n) sqrtl(n) const ll MAX = 5000000000000000000; const ll MOD = 1000000007; //998244353; void randinit(){srand((unsigned)time(NULL));} int main(){ ll N; cin >> N; string S,T; cin >> S >> T; if(S == T){ cout << 0 << endl; return 0; } ll a = 0,b = 0; for(ll i = 0;i < N;i++) a += (S[i] == '0'); for(ll i = 0;i < N;i++) b += (T[i] == '0'); if(a != b){ cout << "-1" << endl; return 0; } vector<ll> A(N,-1),B(N,-1); ll c = 0; for(ll i = 0;i < N;i++){ if(S[i] == '0'){ A[i] = c; c++; } } c = 0; for(ll i = 0;i < N;i++){ if(T[i] == '0'){ B[i] = c; c++; } } for(ll i = 0;i < N;i++){ if(S[i] == '0' && A[i] == B[i]) a--; } cout << a << endl; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second typedef long long ll; typedef pair<int,int> ii; string s; map<char,int> cnt; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; ll ans = 0; for (int i = s.length()-1; i >= 1; i--) { if (s[i] == s[i-1]) { ans += s.length()-i-1-cnt[s[i]]; cnt.clear(); cnt[s[i]] = s.length()-i-1; } cnt[s[i]]++; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i=a; i<=b; i++) #define repd(i, a, b) for(int i=a; i>=b; i--) #define vi vector<int> #define vll vector<long long> #define pii pair<int,int> #define pll pair<long long,long long> #define pb push_back #define fr first #define sc second #define vb vector<bool> typedef long long ll; typedef unsigned long long ull; /*struct cmp { bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2) { return p1.fr > p2.fr; } };*/ void google(){ static int tc=1; cout<<"Case #"<<tc<<": "; tc++; } int n; ll fun(vll &v1,vll &v2){ ll ans=1e18; bool ok=0; if(v1.size()>v2.size()){ swap(v1,v2); ok=1; } for(int i=0;i<v1.size();i++){ int id=lower_bound(v2.begin(),v2.end(),v1[i])-v2.begin(); ans=min(ans,abs(v2[id]-v1[i])); if(id!=0) id--; ans=min(ans,abs(v2[id]-v1[i])); } if(ok) swap(v1,v2); return ans; } void solve(int tc = 1) { vll v[3]; for(int i=0;i<2*n;i++){ ll a; char ch; cin>>a>>ch; if(ch=='R'){ v[0].pb(a); } else if(ch=='G'){ v[1].pb(a); } else{ v[2].pb(a); } } ll ans=1e18; for(int i=0;i<3;i++) sort(v[i].begin(),v[i].end()); if(v[0].size()%2==0 && v[1].size()%2==0 && v[2].size()%2==0){ cout<<0<<endl; return; } if(v[0].size()%2==0){ ans=min(ans,fun(v[1],v[2])); ans=min(ans,fun(v[1],v[0])+fun(v[2],v[0])); } else if(v[1].size()%2==0){ ans=min(ans,fun(v[0],v[2])); ans=min(ans,fun(v[1],v[0])+fun(v[2],v[1])); } else{ ans=min(ans,fun(v[1],v[0])); ans=min(ans,fun(v[1],v[2])+fun(v[2],v[0])); } cout<<ans<<endl; } int main() { /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif*/ ios::sync_with_stdio(false); cin.tie(0); //cout << setprecision(16) << fixed; int t = 1; // cin >> t; int tc=0; while (t--) { cin>>n; tc++; solve(tc); } }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,tune=native") #define ll long long #define int ll #define ull unsigned ll #define ld long double #define rep(a) rep1(i,a) #define rep1(i,a) rep2(i,0,a) #define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++) #define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define pb push_back //#define inf 1010000000 #define inf 4000000000000000000 #define eps 1e-9 #define sz(a) ((int)a.size()) #define pow2(x) (1ll<<(x)) #define ceiling(a,b) (((a)+(b)-1)/(b)) #define print0(a) cout << (a) << ' ' #define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #ifdef i_am_noob #define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__) template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(T && x) {cerr << x << endl;} template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);} #else #define bug(...) 826 #endif template<typename T> void print(T && x) {cout << x << "\n";} template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);} const int Mod=1000000007,Mod2=998244353; const int MOD=Mod2; const int maxn=200005; //i_am_noob //#define wiwihorz int n; vector<int> vec[3]; int solve(int x, int y){ int res=inf; for(auto i: vec[x]) if(sz(vec[y])&&i<=vec[y].back()) res=min(res,(*lower_bound(all(vec[y]),i))-i); for(auto i: vec[y]) if(sz(vec[x])&&i<=vec[x].back()) res=min(res,(*lower_bound(all(vec[x]),i))-i); return res; } void orzck(){ cin >> n; rep(2*n){ int x; char c; cin >> x >> c; if(c=='R') vec[0].pb(x); else if(c=='G') vec[1].pb(x); else vec[2].pb(x); } rep(3) sort(all(vec[i])); vector<int> vec1; rep(3) if(sz(vec[i])%2==0) vec1.pb(i); if(sz(vec1)==3){ print(0); return; } int x,y,z; x=vec1[0]; y=(x+1)%3; z=3-x-y; print(min(solve(y,z),solve(x,y)+solve(x,z))); } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); #ifdef i_am_noob freopen("input1.txt","r",stdin); freopen("output1.txt","w",stdout); freopen("output2.txt","w",stderr); #endif cout << fixed << setprecision(15); int t; #ifdef wiwihorz cin >> t; #else t=1; #endif while(t--) orzck(); return 0; }
#include <cstdio> #define rep( i, a, b ) for( int (i) = (a) ; (i) <= (b) ; ++ (i) ) #define per( i, a, b ) for( int (i) = (a) ; (i) >= (b) ; -- (i) ) const int mod = 1e9 + 7; const int MAXN = 2e3 + 5; template<typename _T> void read( _T &x ) { x = 0; char s = getchar(); int f = 1; while( s < '0' || '9' < s ) { f = 1; if( s == '-' ) f = -1; s = getchar(); } while( '0' <= s && s <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ); s = getchar(); } x *= f; } template<typename _T> void write( _T x ) { if( x < 0 ) putchar( '-' ), x = -x; if( 9 < x ) write( x / 10 ); putchar( x % 10 + '0' ); } int A[MAXN]; int N, M; int Mul( long long x, int v ) { return x * v % mod; } int Qkpow( int base, int indx ) { int ret = 1; while( indx ) { if( indx & 1 ) ret = Mul( ret, base ); base = Mul( base, base ), indx >>= 1; } return ret; } int Inv( const int a ) { return Qkpow( a, mod - 2 ); } int main() { read( N ), read( M ); int su = 0; rep( i, 1, N ) read( A[i] ), su += A[i]; int prod = 1; rep( i, 1, su + N ) prod = Mul( prod, Mul( Inv( i ), M + N - i + 1 ) ); write( prod ), putchar( '\n' ); return 0; }
#include<bits/stdc++.h> #define it register int #define ct const int #define il inline using namespace std; const int N=1000005; int p[N],cn,mn[N],n; bool isp[N]; il int Min(ct p,ct q){return p<q?p:q;} il void Pre(){ it i,j,x;mn[1]=1; for(i=2;i<=n;++i){ if(!isp[i]) p[++cn]=i,mn[i]=i; for(j=1;(x=p[j]*i)<=n;++j){ isp[x]=1,mn[x]=Min(mn[i],p[j]); if(!(i%p[j])) break; } } } int main(){ scanf("%d",&n),Pre(); for(it i=1,j;i<=n;++i){ it cnt=0; for(j=i;j>1;){ it x=mn[j]; while(j>1&&mn[j]==x) j/=x,++cnt; //++cnt; } printf("%d ",cnt+1); } return 0; }
#include <iostream> #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE template<typename T> void __p(T a) { cout << a; } template<typename T, typename F> void __p(pair<T, F> a) { cout << "{"; __p(a.first); cout << ","; __p(a.second); cout << "}"; } template<typename T> void __p(std::vector<T> a) { cout << "{"; for (auto it = a.begin(); it < a.end(); it++) __p(*it), cout << ",}"[it + 1 == a.end()]; } template<typename T, typename ...Arg> void __p(T a1, Arg ...a) { __p(a1); __p(a...); } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : "; __p(arg1); cout << endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket = 0, i = 0; for (;; i++) if (names[i] == ',' && bracket == 0) break; else if (names[i] == '(') bracket++; else if (names[i] == ')') bracket--; const char *comma = names + i; cout.write(names, comma - names) << " : "; __p(arg1); cout << " | "; __f(comma + 1, args...); } #define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__); #else #define trace(...) #endif typedef long long int ll; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) typedef pair<ll , ll> pp; typedef pair<pair<ll, ll>, ll> ppp; #define X first #define Y second #define getones(n) __builtin_popcountll(n) #define debug(x) cout<<#x<<" :: "<<x<<"\n"; #define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"; #define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"; #define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define frv(i,a,b) for (ll i = (a), _b = (b); i < _b; i++) #define frrv(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--) #define fr(i,n) for (ll i = 0, _n = (n); i < _n; i++) #define frr(i,n) for (ll i = n - 1; i >= 0; i--) #define fordata(it,ar) for (auto it = ar.begin(); it != ar.end(); it++) #define fill(ar,val) fr(i,sizeof(ar)/sizeof(ll)) ar[i]=val #define fill2(ar,val) fr(i,sizeof(ar)/sizeof(ar[0])) fr(j,sizeof(ar[0])/sizeof(ll)) ar[i][j]=val #define fill0(ar) memset(ar,0,sizeof(ar)) #define sz(x) (int)x.size() #define len(x) (int)x.length() #define all(ar) ar.begin(), ar.end() #define INF 1000000000000000000 #define PI 3.14159265358979323846L #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) const ll N = 1e6 + 1; const ll K = 1e9 + 7; #define vv vector<ll> const ll globalmod = 1e9 + 9; int main() { ll a, b, x, y; cin >> a >> b >> x >> y; if (a > b) { cout << min(x + (a - b-1)*y, (max(2 * (a - b) - 1, 0ll))*x); } else { cout << min(x + (b - a)*y, (2 * (b - a) + 1)*x); } }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <string> #include <sstream> #include <complex> #include <vector> #include <list> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #include <array> #include <climits> using namespace std; typedef long long unsigned int unsigned_myint; typedef long long int myint; int main(){ myint r_1,c_1,r_2,c_2,r_d,c_d; int ans; cin >> r_1; cin >> c_1; cin >> r_2; cin >> c_2; r_d=r_2-r_1; c_d=c_2-c_1; if((r_1==r_2) && (c_1==c_2)){ ans=0; }else if(((r_1+c_1)==(r_2+c_2))||((r_1-c_1)==(r_2-c_2)||(abs(c_2-c_1)+abs(r_2-r_1)<=3))){ ans=1; }else if(abs(r_d-c_d)<=3 || abs(-r_d-c_d)<=3||abs(-r_d+c_d)<=3||abs(r_d+c_d)<=3||((c_1+r_1)%2==(c_2+r_2)%2)){ ans =2; }else{ ans =3; } cout << ans<< endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> iint; typedef pair<ll,ll> llll; #define ALL(x) (x).begin(),(x).end() const ll zero = 0; const ll one = 1; const ll INF = 9223372036854775807; //10^18 const int inINF = 2147483647; //10^9 const ll MOD = 1000000007; //10^9+7 const ll MOD2 = 998244353; void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} void solve(){ int N; cin >> N; vector<ll> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } if(N % 2 == 0){ map<ll, int> m; for (int i = 0; i < N; i++) { if(m.count(a[i])){ m[a[i]]++; } else{ m[a[i]] = 1; } } for (auto v : m){ if(v.second % 2 == 1){ printf("First\n"); return; } } printf("Second\n"); return; } else{ printf("Second\n"); return; } } int main(){ int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
#include<bits/stdc++.h> #define M_PI 3.14159265358979323846 #define Speed_UP ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define pb push_back #define ff first #define ss second #define sz(x) (int)x.size() #define all(x) (x).begin(), (x).end() #warning Remember to change t inline void setIO(); using namespace std; typedef long long ll; typedef long double ld; int solve(){ ll n; cin>>n; ll arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } // n-1 spaces ll ans = 1e10; for(ll i=0;i<(1LL<<(n-1));i++){ ll ored = 0; ll xord = 0; for(ll j = 0;j<n;j++){ ored|=arr[j]; if(((1LL<<j)&i)!=0||j==n-1){ // cout<<ored<<" "; xord^=ored; ored = 0; } } // cout<<xord<<"\n"; ans = min(xord,ans); } cout<<ans; return 0; } int main(){ Speed_UP #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif ll t = 1; while(t--){ solve(); } } inline void setIO(string name="") { #ifndef ONLINE_JUDGE freopen((name+".in").c_str(), "r", stdin); freopen((name+".out").c_str(), "w", stdout); #endif }
#include<iostream> #include<string.h> #include<stdio.h> #include<math.h> #include<queue> #include<algorithm> #include<map> using namespace std; typedef long long ll; #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=b;i>=a;i--) const ll INF=0x3f3f3f3f3f3f3f3f,mod=1e9+7; const int inf=0x3f3f3f3f,maxn=1e5+500; const double pi=3.1415926535; bool cmp(ll x,ll y) { return x<y; } ll n,m,t,ans,a[2*maxn],b[maxn*2]={0},cnt=0; int main() { scanf("%lld%lld",&n,&m); fo(i,0,m-1)scanf("%lld",&a[i]); a[m++]=n+1; a[m++]=0; sort(a,a+m,cmp); ll mi=n; fo(i,1,m-1){ ll x=a[i]-a[i-1]; if(x&&x!=1)b[cnt++]=x-1,mi=min(mi,x-1); } if(!mi)ans=0; else { ans=0; fo(i,0,cnt-1) { // printf("%lld ",b[i]); ans+=b[i]/mi; if(b[i]%mi)ans++; } } printf("%lld\n",ans); }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #include <map> #include <cmath> #include <queue> #include <cstdio> #include <limits> #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) using namespace std; template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; } template<class T> inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; const int inf = numeric_limits<int>::max(); const ll infll = numeric_limits<ll>::max(); int main() { int n,m; cin >> n >> m; vvi edge(n, vi(n, 0)); rep(i,m) { int a,b; cin >> a >> b; a--; b--; edge[a][b] = 1; edge[b][a] = 1; } vi tab((1<<n), 0); tab[0] = 1; rep(S, (1<<n)) { if(tab[S]) { rep(i,n) { bool f = true; rep(j,n) { if((S>>j)&1) { if(!edge[i][j]) f = false; } } if(f) tab[S|(1<<i)] = 1; } } } // rep(i,sz(tab)) cout << i << ":" << tab[i] << "\n"; vl dp((1<<n), -1); auto rec = [&](auto self, int mask)->int { // cout << bitset<8>(mask) << "\n"; if(dp[mask] >= 0) return dp[mask]; if(tab[mask]) return dp[mask] = 1; int res = 50; for(int i=mask;i>0;i=(i-1)&mask){ if(i == mask) continue; int tmp = self(self, i) + self(self, mask-i); chmin(res, tmp); } return dp[mask] = res; }; int ans = rec(rec, (1<<n)-1); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> typedef long long ll; const ll max_N = 100; const ll max_M = 1000000; ll n, K, mod, dp[max_N + 5][max_M + 5], G[max_M + 5]; ll read() { char c = getchar(); ll ans = 0; while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') ans = ans * 10 + c - '0', c = getchar(); return ans; } void Write(ll x) { if (x < 10) putchar(x + '0'); else Write(x / 10), putchar(x % 10 + '0'); } ll add(ll x, ll y) {return (x += y) >= mod ? x - mod : x;} ll sub(ll x, ll y) {return (x -= y) < 0 ? x + mod : x;} ll max(ll x, ll y) {return x > y ? x : y;} ll min(ll x, ll y) {return x < y ? x : y;} ll calc(ll id) { ll sum = 0; for (ll i = 0; i <= max_M; i++) sum = (sum + dp[id - 1][i] * dp[n - id][i]) % mod; return sum; } int main() { n = read(), K = read() + 1, mod = read(); dp[0][0] = 1; for (ll i = 1; i <= n; i++) { for (ll j = 0; j <= max_M; j++) G[j] = 0; for (ll j = 0; j <= max_M; j++) G[j] = add(dp[i - 1][j], (j >= i ? G[j - i] : 0)); for (ll j = 0; j <= max_M; j++) dp[i][j] = sub(G[j], (j >= i * K ? G[j - i * K] : 0)); } for (ll i = 1; i <= n; i++) Write((calc(i) * K + mod - 1) % mod), putchar('\n'); return 0; }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; 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; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, A[1010], B[1010]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 0, N) cin >> A[i] >> B[i]; int ans = inf; rep(a, 0, N) rep(b, 0, N) { int cost; if (a == b) cost = A[a] + B[b]; else cost = max(A[a], B[b]); chmin(ans, cost); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main () { string S; cin >> S; int n = (int)S.size(); int fl = n - 1; while (fl >= 0 && S[fl] == '0') fl --; for (int i = 0; i <= fl; i ++) { int j = fl - i; if (S[i] != S[j]) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; }
#include<bits/stdc++.h> using namespace std; int n , f , las = -1e9; string s; int main() { cin >> n >> s; if(s[0] != s[n - 1]) { printf("1"); return 0; } for(int i = 0 ; i < n ; i++ ) { if(s[i] != s[0]) { f |= (las == (i - 1)); las = i; } } if(f) printf("2"); else printf("-1"); return 0; } /* */
// #include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' const int maxn = 3050; const int maxm = maxn*maxn*4; int head[maxm],vis[maxm]; char G[maxn][maxn]; int dis[maxm]; struct Edge{ int f,t,w,next; Edge(int f = 0,int t = 0,int w = 0,int next = 0):f(f),t(t),w(w),next(next){} }edge[maxm]; int cnt; const int INF = 1e9; void addedge(int f,int t,int w){ edge[cnt] = Edge(f,t,w,head[f]); head[f] = cnt++; } void dijkstra(int s){ typedef pair <int,int> node; priority_queue<node,vector<node>,greater<node> >q; for(int i =1;i< maxm;i++)dis[i] = INF; dis[s] = 0; q.push(make_pair(dis[s],s)); while(!q.empty()){ node tmp = q.top(); q.pop(); int u = tmp.second; if(vis[u])continue; vis[u] = 1; for(int i = head[u];~i;i = edge[i].next){ int v = edge[i].t; if(dis[v] > dis[u] + edge[i].w){ dis[v] = dis[u] + edge[i].w; if(!vis[v]){ q.push(make_pair(dis[v],v)); } } } } return; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); memset(head,-1,sizeof(head)); int n,m; const int B = 2050*2050+1; int S = B+100; int T = B+101; cin >> n >> m; for(int i = 1;i <= n;i++){ string s; cin >> s; for(int j = 1;j <= m;j++){ G[i][j] = s[j-1]; if(s[j-1] == 'S')S = i*2010+j; if(s[j-1] == 'G')T = i*2010+j; } } for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ if(G[i][j] != '#'){ for(int k = -1;k <= 1;k++){ for(int l = -1;l <= 1;l++)if(abs(k+l) == 1){ int x = i+k,y = j+l; if(x < 1 or x > n or y < 1 or y > m)continue; if(G[i][j] != '#')addedge(i*2010+j,x*2010+y,2); } } } if(G[i][j] >= 'a' and G[i][j] <= 'z'){ addedge(i*2010+j,B+G[i][j],1); addedge(B+G[i][j],i*2010+j,1); } } } dijkstra(S); if(dis[T] == INF)cout << -1; else cout << dis[T]/2; return 0; }
/** * Author: dhruv_gheewala * Problem: __________ * Date: 13.12.2020 * Time: 17:51:47 **/ #if __has_include("debug.h") #include "debug.h" #else #include<bits/stdc++.h> using namespace std; #define fundri 108 #define debug(...) 1729 #endif #define endl '\n' #define int int64_t typedef pair<int, int> pii; typedef vector<int> vi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);} bool in_range(int x, int l, int r) {return l <= x && x <= r;} template<typename H, typename ...T>void inp(H &head) {cin >> head;} template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);} template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;} template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;} // Multi-Dimension Vector // Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type()) template<int D, typename T> struct vec : public vector<vec<D - 1, T>> { static_assert(D >= 1, "Vector dimensions must be greater than zero !!"); template<typename... Args> vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){} }; template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}}; const int inf = 1e15; const bool testcases = false; void init_main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DHRUV_GHEEWALA freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif } void solve(int tc) { int n, m; cin >> n >> m; vi a(m); cin >> a; sort(a.begin(), a.end()); reverse(a.begin(), a.end()); a.emplace_back(0); reverse(a.begin(), a.end()); a.emplace_back(n + 1); m++; int mini = inf; for(int i = 1; i <= m; i++) { if(a[i] - a[i - 1] > 1) mini = min(mini, a[i] - a[i - 1] - 1); } debug(mini, a); int res = 0; for(int i = 1; i <= m; i++) res += ceil(1.00 * (a[i] - a[i - 1] - 1) / mini); cout << res << endl; } int32_t main(int32_t argc, char **argv) { init_main(); int TC = 1; if(testcases) cin >> TC; for(int tc = 1; tc <= TC; ++tc) { solve(tc); fundri; } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int>::iterator iter = max_element(a.begin(), a.end()); int amax = *iter ; long long c = 0, ans = 0, psum = 0; for (int i = 0; i <n; i++) { psum = 0; c = 0; for (int j = 0; j < n; j++) { if (0<= a[j]-a[i]) { c++; } else { psum = c * a[i]; if (ans < psum) ans = psum; c = 0; } } if (c == 0) psum = a[i]; else psum = c * a[i]; if (ans < psum) ans = psum; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll mod = pow(10,9)+7; int main(){ ll n; cin >> n; vector<ll> A(n); for(ll i=0; i<n; i++) cin >> A[i]; sort(A.begin(),A.end()); ll ans = A[0]+1; for(ll i=1; i<n; i++){ ans*= (A[i]-A[i-1])+1; ans %= mod; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define db double #define pii pair<int,int> #define pli pair<ll,int> #define pil pair<int,ll> #define pll pair<ll,ll> #define ti3 tuple<int,int,int> #define mat vector<vector<int>> const int inf = 1 << 30; const ll linf = 1LL << 62; const db EPS = 1e-7; template<class T> void chmin(T& x, T y){if(x > y) x = y;} template<class T> void chmax(T& x, T y){if(x < y) x = y;} ll extgcd(ll a, ll b, ll &x, ll &y){ if(a % b == 0){ x = 0, y = 1; return b; } ll g = extgcd(b, a % b, y, x); y -= (a / b) * x; return g; } void solve(){ ll X, Y, P, Q; cin >> X >> Y >> P >> Q; ll ans = linf; for(ll i = 0; i < Y; i++){ for(ll j = 0; j < Q; j++){ ll x, y; ll g = extgcd(2LL * (X + Y), P + Q, x, y); if((P + j - X - i) % g == 0){ y *= (P + j - X - i) / g; y %= (X + Y) * 2LL / g; if(y > 0) y -= (X + Y) * 2LL / g; ans = min(ans, -y * (P + Q) + P + j); } } } if(ans == linf) cout << "infinity" << endl; else cout << ans << endl; } int main(){ int T; cin >> T; while(T--){ solve(); } return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; template<class S, class T> inline S max_L(S a,T b){ return a>=b?a:b; } 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(long long x){ int s=0; int m=0; char f[20]; 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'); } } template<class S, class T> inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } int N; int X[20]; int Y[20]; int Z[20]; long long dp[131072][17]; int main(){ int i, mask; rd(N); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(X[Lj4PdHRW]); rd(Y[Lj4PdHRW]); rd(Z[Lj4PdHRW]); } } for(i=(0);i<(1<<N);i++){ int j; for(j=(0);j<(N);j++){ dp[i][j] = 4611686016279904256LL; } } dp[0][0] = 0; for(mask=(0);mask<(1<<N);mask++){ for(i=(0);i<(N);i++){ if(dp[mask][i] < 4611686016279904256LL){ int j; for(j=(0);j<(N);j++){ if(!((mask) &(1<<(j)))){ chmin(dp[mask|(1<<j)][j], dp[mask][i] + abs(X[i]-X[j]) + abs(Y[i]-Y[j]) +max_L(Z[j]-Z[i], 0)); } } } } } wt_L(dp[(1<<N)-1][0]); wt_L('\n'); return 0; } // cLay varsion 20201018-1 // --- original code --- // int N, X[20], Y[20], Z[20]; // ll dp[131072][17]; // { // rd(N,(X,Y,Z)(N)); // rep(i,1<<N) rep(j,N) dp[i][j] = ll_inf; // dp[0][0] = 0; // rep(mask,1<<N) rep(i,N) if(dp[mask][i] < ll_inf) rep(j,N) if(!BIT_ith(mask,j)){ // dp[mask|(1<<j)][j] <?= dp[mask][i] + abs(X[i]-X[j]) + abs(Y[i]-Y[j]) + max(Z[j]-Z[i], 0); // } // wt(dp[(1<<N)-1][0]); // }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cerr<<#x<<" = "<<x<<endl; #define PQ(T) priority_queue<T,v(T),greater<T> > #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef tuple<int,int,int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; int getInt(){int x;scanf("%d",&x);return x;} template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;} template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;} template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;} template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;} template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;} template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return 0;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; // Unionfind struct uf{ vi d; uf() {}; uf(int mx) : d(mx,-1) {}; int root(int x){ if(d[x]<0) return x; return d[x] = root(d[x]); } bool unite(int x, int y){ x = root(x); y = root(y); if(x == y) return false; if(d[x] > d[y]) swap(x,y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y);} int size(int x){ return -d[root(x)];} int operator[] (int x) {return root(x);} int operator() (int x) {return size(x);} }; int main() { int n, q; cin >> n >> q; vi c(n); rep(i,n) cin >> c[i], --c[i]; vector<set<int> > s(n); // 1次元目が根の2次元目にその根がどのような種類のクラスを保持しているか vector<map<int,int> > m(n); // 1次元目が根の2次元目で各クラスに何人いるかを保持 rep(i,n){ s[i].insert(c[i]); m[i][c[i]] = 1; } uf dis(n); vi ans; rep(_,q){ int qi; cin >> qi; if(qi == 1){ int a,b; cin >> a >> b; --a,--b; if(dis.same(a,b)) continue; if(dis.size(a) < dis.size(b)) swap(a,b); int ra = dis.root(a), rb = dis.root(b); for(int clsb : s[rb]){ if(s[ra].count(clsb)) m[ra][clsb] += m[rb][clsb]; else s[ra].insert(clsb), m[ra][clsb] = m[rb][clsb]; } // printf("ra: %d m[ra][1]: %d\n", ra, m[ra][1]); dis.unite(a,b); }else{ int x,y; cin >> x >> y; --x,--y; ans.push_back(m[dis.root(x)][y]); } } for(int res: ans) cout << res << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for (long long i = 0; i < (n); ++i) #define DIV 1000000007 //10^9+7 #define INF LONG_MAX/3 #define bit(n) (1LL<<(n)) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; // 素集合データ構造 struct UnionFind { // par[i]:データiが属する木の親の番号。i == par[i]のとき、データiは木の根ノードである vector<int> par; // sizes[i]:根ノードiの木に含まれるデータの数。iが根ノードでない場合は無意味な値となる vector<int> sizes; // group -> (class, num) map<ll, map<ll, ll> >class_sizes; UnionFind(int n) : par(n), sizes(n, 1) { // 最初は全てのデータiがグループiに存在するものとして初期化 for(int i = 0; i < n; i++){ par[i] = i; } } void set_class(ll num, ll cls) { class_sizes[num][cls] = 1; } // データxが属する木の根を得る int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); // 根を張り替えながら再帰的に根ノードを探す } // 2つのデータx, yが属する木をマージする void unite(int x, int y) { // データの根ノードを得る x = find(x); y = find(y); // 既に同じ木に属しているならマージしない if (x == y) return; // xの木がyの木より大きくなるようにする if (sizes[x] < sizes[y]) swap(x, y); // xがyの親になるように連結する par[y] = x; sizes[x] += sizes[y]; for(pair<ll, ll> item: class_sizes[y]) { class_sizes[x][item.first] += item.second; } // sizes[y] = 0; // sizes[y]は無意味な値となるので0を入れておいてもよい } // 2つのデータx, yが属する木が同じならtrueを返す bool same(int x, int y) { return find(x) == find(y); } // データxが含まれる木の大きさを返す int size(int x) { return sizes[find(x)]; } ll size2(int x, int y) { return class_sizes[find(x)][y]; } }; int main(){ ll N, Q; cin >> N >> Q; vector<ll> C(N); rep(i, N) cin >> C[i]; UnionFind uf(N+5); rep(i, N) uf.set_class(i, C[i]); rep(i, Q) { ll a, b, c; cin >> a >> b >> c; if(a == 1) { b--;c--; uf.unite(b, c); } else { b--; cout << uf.size2(b, c) << endl; } } }
#include <bits/stdc++.h> long long func(std::vector<std::pair<long long, long long>> &vw, std::vector<long long> &boxsize, int l, int r) { std::vector<long long> box; for (int i = 0; i < (int)boxsize.size(); ++i) { if (i < l || i > r) box.push_back(boxsize[i]); } std::vector<bool> used(box.size(), false); std::sort(box.begin(), box.end()); long long res = 0LL; for (int i = 0; i < (int)vw.size(); ++i) { long long value = vw[i].first; long long weight = vw[i].second; int index = std::lower_bound(box.begin(), box.end(), weight) - box.begin(); while (index < (int)box.size() && used[index]) ++index; if (index < (int)box.size()) { used[index] = true; res += value; } } return res; } int main() { int n, m, q; std::cin >> n >> m >> q; std::vector<std::pair<long long, long long>> vw(n); for (int i = 0; i < n; ++i) std::cin >> vw[i].second >> vw[i].first; std::vector<long long> boxsize(m); for (int i = 0; i < m; ++i) std::cin >> boxsize[i]; std::sort(vw.begin(), vw.end(), std::greater<std::pair<long long, long long>>()); std::vector<long long> res; for (int i = 0; i < q; ++i) { int l, r; std::cin >> l >> r; --l; --r; res.push_back(func(vw, boxsize, l, r)); } for (long long ans : res) std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> #include <cassert> typedef long long int ll; using namespace std; // #include <atcoder/all> // using namespace atcoder; // @@ !! LIM() int main(/* int argc, char *argv[] */) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(20); ll N, M; cin >> N >> M; using nbr_t = tuple<ll, ll, bool>; vector<vector<nbr_t>> nbr(N + 1); vector<bool> edir(M + 1); for (ll i = 1; i <= M; i++) { ll a, b; cin >> a >> b; nbr[a].emplace_back(b, i, true); nbr[b].emplace_back(a, i, false); } vector<ll> C(N + 1); for (ll i = 1; i <= N; i++) cin >> C[i]; vector<bool> visited(N + 1); auto dfs = [&](auto rF, ll nd) -> void { if (visited[nd]) return; visited[nd] = 1; for (auto [m, ei, fl] : nbr[nd]) { if (C[nd] > C[m]) { edir[ei] = fl; }else if (C[nd] < C[m]) { edir[ei] = !fl; }else { edir[ei] = fl; rF(rF, m); } } }; for (ll i = 1; i <= N; i++) { dfs(dfs, i); } for (ll i = 1; i <= M; i++) { if (edir[i]) { cout << "->\n"; }else { cout << "<-\n"; } } return 0; }
#include<iostream> using namespace std; int main() { int a,b,c,d; cin>>a>>b>>c>>d; int result = b-c; cout<<result <<endl; return 0; }
typedef long long ll; typedef long double ld; #include <bits/stdc++.h> using namespace std; int main() { vector<ll> a(2); for (int i = 0; i < 2; i++) { std::cin >> a[i]; } sort(a.begin(),a.end()); if(a[0]+3>a[1]){ std::cout << "Yes" << std::endl; }else{ std::cout << "No" << std::endl; } }