code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
//clear adj and visited vector declared globally after each test case //check for long long overflow //Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod; //Incase of close mle change language to c++17 or c++14 #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define int long long #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10); #define pb push_back #define mod 1000000007ll //998244353ll #define lld long double #define mii map<int, int> #define pii pair<int, int> #define ll long long #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=x; i<y; i++) #define fill(a,b) memset(a, b, sizeof(a)) #define vi vector<int> #define setbits(x) __builtin_popcountll(x) #define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";} typedef std::numeric_limits< double > dbl; using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; //member functions : //1. order_of_key(k) : number of elements strictly lesser than k //2. find_by_order(k) : k-th element in the set const long long N=2000005, INF=2000000000000000000; lld pi=3.1415926535897932; int lcm(int a, int b) { int g=__gcd(a, b); return a/g*b; } int power(int a, int b, int p) { if(a==0) return 0; int res=1; a%=p; while(b>0) { if(b&1) res=(res*a)%p; b>>=1; a=(a*a)%p; } return res; } int fact[N],inv[N]; void pre() { fact[0]=1; inv[0]=1; for(int i=1;i<N;i++) fact[i]=(i*fact[i-1])%mod; for(int i=1;i<N;i++) inv[i]=power(fact[i], mod-2, mod); } int nCr(int n, int r, int p) { if(r>n) return 0; if(n==r) return 1; if (r==0) return 1; return (((fact[n]*inv[r]) % p )*inv[n-r])%p; } int32_t main() { IOS; pre(); int n, m, k; cin>>n>>m>>k; int ans=0; rep(i,max(0ll, n-m),k+1) { ans+=(nCr(n+m, m+i, mod) - nCr(n+m, m+i+1, mod) + mod)%mod; ans%=mod; } cout<<ans; }
#include<bits/stdc++.h> #define int long long const int mod=1e9+7; using namespace std; int qpow(int a,int b){ int ans=1; while(b){ if(b&1)(ans*=a)%=mod; (a*=a)%=mod; b>>=1; } return ans; } int inv(int a){ return qpow(a,mod-2); } int fact(int a){ int ans=1; for(int i=1;i<=a;i++){ (ans*=i)%=mod; } return ans; } int cc(int a,int b){ if(a<0||b<0)return 0; return fact(b)*inv(fact(b-a))%mod*inv(fact(a))%mod; } signed main(){ int n,m,k; cin>>n>>m>>k; if(n>m+k){ cout<<0; } else cout<<((cc(n,n+m)-cc(n-k-1,m+n))%mod+mod)%mod; }
#ifndef LOCAL #include <bits/stdc++.h> using namespace std; #define debug(...) 42 #else #include "Debug.hpp" #endif class Task { public: void Perform() { Read(); Solve(); } private: int a, b, c, d; void Read() { cin >> a >> b >> c >> d; } void Solve() { int max_diff = INT_MIN; for (int x = a; x <= b; ++x) { for (int y = c; y <= d; ++y) { max_diff = max(max_diff, x - y); } } cout << max_diff << '\n'; } }; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); Task t; t.Perform(); return 0; }
#include <bits/stdc++.h> #define int long long int using namespace std; void solve(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; } void solve() { int a,b,c,d; cin>>a>>b>>c>>d; cout<<b-c<<endl; }
#include<bits/stdc++.h> using namespace std; const int M = 1e9 + 7; const int MAX = 1e5 + 5; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL); #define int long long int #define pb push_back #define print(x) cout<<x<<"\n" #define time_passed 1.0 * clock() / CLOCKS_PER_SEC //basic mathematics int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int lcm(int a, int b) { return (a * b) / gcd(a, b); } //modular arithmetic int powermod(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } //SOURCE GEEKS FOR GEEKS bool solve(string n, int l) { // Less than three digit number // can be checked directly. if (l < 3) { if (stoi(n) % 8 == 0) return true; // check for the reverse of a number reverse(n.begin(), n.end()); if (stoi(n) % 8 == 0) return true; return false; } // Stores the Frequency of characters in the n. int hash[10] = { 0 }; for (int i = 0; i < l; i++) hash[n[i] - '0']++; // Iterates for all three digit numbers // divisible by 8 for (int i = 104; i < 1000; i += 8) { int dup = i; // stores the frequency of all single // digit in three-digit number int freq[10] = { 0 }; freq[dup % 10]++; dup = dup / 10; freq[dup % 10]++; dup = dup / 10; freq[dup % 10]++; dup = i; // check if the original number has // the digit if (freq[dup % 10] > hash[dup % 10]) continue; dup = dup / 10; if (freq[dup % 10] > hash[dup % 10]) continue; dup = dup / 10; if (freq[dup % 10] > hash[dup % 10]) continue; return true; } // when all are checked its not possible return false; } void solve() { string s; cin >> s; if (solve(s, s.size())) cout << "Yes"; else cout << "No"; } int32_t main(void) { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio int t = 1; //cin >> t; while (t--) { solve(); } cerr << "\n" << "Time elapsed : " << time_passed << "\n"; return 0; }
#include <bits/stdc++.h> typedef long double ld; #define int long long #define gcd __gcd #define endl "\n" #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define mod2 998244353 #define maxe *max_element #define mine *min_element #define inf 1e18 #define pb push_back #define all(x) x.begin(), x.end() #define f first #define s second #define lb lower_bound #define ub upper_bound #define ins insert #define sz(x) (int)(x).size() #define mk make_pair #define deci(x, y) fixed<<setprecision(y)<<x #define w(t) int t; cin>>t; while(t--) #define nitin ios_base::sync_with_stdio(false); cin.tie(nullptr) #define PI 3.141592653589793238 #define mem0(x) memset(x,0,sizeof x) #define mem1(x) memset(x,-1,sizeof x) using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.f << ", " << 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 NITIN #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif vector<int> solve(vector<int>&a,vector<int>&b) { vector<int> temp; for (auto &c:a) { auto idx = lower_bound(all(b), c); int op1 = inf, op2 = inf; if (idx != b.end()) { op1 = min(op1, abs(*idx - c)); } if (idx != b.begin()) { idx = prev(idx); op2 = min(op2, abs(*idx - c)); } temp.push_back(min(op1, op2)); } return temp; } void solve() { int n; cin>>n; vector<int>r,g,b; for(int i=0;i<2*n;i++) { int val; cin >> val; char col; cin >> col; if (col == 'R') { r.pb(val); } else if (col == 'G') { g.pb(val); } else { b.pb(val); } } sort(all(r)); sort(all(g)); sort(all(b)); if(r.size()%2==0 && g.size()%2==0 && b.size()%2==0){ cout<<0<<endl; }else{ if(r.size()%2!=0) { if (b.size() % 2 == 0) { swap(r, b); } else { swap(r, g); } } int ans=inf; vector<int> temp=solve(g,b); ans=mine(all(temp)); vector<int>fir=solve(r,g); vector<int>sec=solve(r,b); map<int,int>take; for(auto &c:sec) take[c]++; for(int i=0;i<r.size();i++) { take[sec[i]]--; if (take[sec[i]] == 0) { take.erase(sec[i]); } ans = min(ans, fir[i] + take.begin()->first); take[sec[i]]++; } cout<<ans<<endl; } } int32_t main() { nitin; solve(); }
// Jai Shree Ram #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ook order_of_key #define fbo find_by_order #define rep(i,a,n) for(int i=a;i<n;i++) #define ll long long #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define endl "\n" #define x first #define y second #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define sz(a) (int)a.size() #define pii pair<int,int> #define hell 1000000007 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;} template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;} const int N=1e9+5; const int M=1e7; struct node{ int l;int r; int val; int cnt; }t[M]; int cnt=0; int n; void extend(int v){ if(t[v].l==0){ t[v].l=++cnt; t[v].r=++cnt; } } void pushup(int v){ t[v].val = t[t[v].l].val + t[t[v].r].val; t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt; } void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){ if(tl>r || l>tr)return; if(l<=tl && tr<=r){ assert(tl == tr); t[v].val += val.x; t[v].cnt += val.y; return; } extend(v); int mid=(tl+tr)/2; update(t[v].l,l,r,val,tl,mid); update(t[v].r,l,r,val,mid+1,tr); pushup(v); } pii query(int v,int l,int r,int tl=0,int tr=1e9){ if(tl>r || l>tr)return {0,0}; if(l<=tl && tr<=r){ return {t[v].val,t[v].cnt}; } int mid=(tl+tr)/2; extend(v); pii a =query(t[v].l,l,r,tl,mid); pii b =query(t[v].r,l,r,mid+1,tr); return {a.x+b.x,a.y+b.y}; } int solve(){ int n,m,q; cin >> n >> m >> q; int ans = 0; int roota = ++cnt; int rootb = ++cnt; vector<int>v1(n+1),v2(m+1); update(roota,0,0,{0,n}); update(rootb,0,0,{0,m}); rep(i,0,q){ int t; cin >> t; int x,y; cin >> x >> y; if(t == 1){ if(y > v1[x]){ auto s = query(rootb,0,y); ans += s.y*y; s = query(rootb,v1[x]+1,y); ans -= s.x; s = query(rootb,0,v1[x]); ans -= v1[x]*s.y; } else if(y < v1[x]){ auto s = query(rootb,0,v1[x]); ans -= s.y*v1[x]; s = query(rootb,0,y); ans += s.y*y; s = query(rootb,y+1,v1[x]); ans += s.x; } update(roota,v1[x],v1[x],{-v1[x],-1}); update(roota,y,y,{y,1}); v1[x] = y; } else{ if(y > v2[x]){ auto s = query(roota,0,y-1); ans += s.y*y; s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,v2[x],y-1); ans -= s.x; } else if(y < v2[x]){ auto s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,0,y-1); ans += s.y*y; s = query(roota,y,v2[x]-1); ans += s.x; } update(rootb,v2[x],v2[x],{-v2[x],-1}); update(rootb,y,y,{y,1}); v2[x] = y; } cout << ans << endl; } return 0; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t=1;//cin>>t; while(t--){ solve(); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<iomanip> #include<cmath> #include<numeric> #include<vector> #include<algorithm> #include<string> #include<deque> #include<stack> #include<map> #include<random> #include<queue> #include<iterator> #include<bitset> #include<unordered_set> #include<set> using namespace std; #define ll long long #define endl "\n" const int dx[] = { 1 ,0, -1, 0 }; const int dy[] = { 0, 1, 0, -1 }; const int N = 1e6 + 100; const int NN = 1e3 + 100; #define TC int t;cin>>t;for(int T=1;T<=t;T++) #define mod 1000000007 #define PI acos(-1) // 3.141592654 #define EPS 0.000000001 #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define mem(a,n) memset(a,n,sizeof(a)) #define sz(x) x.size() void AIA() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } //cout<<fixed<<setprecision(20); //freopen("stand.in","r",stdin); //freopen("stand.out","w",stdout); //priority_queue<ll, vector<ll>, greater<ll>>p2; bool isprime(ll n) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0)return 0; } return (n != 1); } ll gcd(ll x, ll y) { return (y == 0 ? x : gcd(y, x % y)); } ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } ll nCr(ll n, ll r) { ll ans = 1; ll div = 1; // r! for (ll i = r + 1; i <= n; i++) { ans = ans * i; ans /= div; div++; } return ans; } ll NpR(ll n, ll r) { ll ans = 1; for (ll i = (n - r) + 1; i <= n; i++) { ans *= i; ans %= mod; } return ans; } /* _____________ __ ___ ___ ___________________________ ____ ____ | | /\ \ | | | | | | \ \ / / | __________| / /\ \ | | | | | _____ _____ | \ \ / / | | / / \ \ | | | | | | | | | | \ \ / / | | / / \ \ | | | | | | | | | | \ \/ / | |______ / / \ \ | |______| | | | | | | | \ / | | / / \ \ | | | | | | | | \ / | ______| / / \ \ | ______ | | | | | | | | | | | / /============\ \ | | | | | | | | | | | | | | / /==============\ \ | | | | | | | | | | | | | | / / \ \ | | | | | | | | | | | | | | / / \ \ | | | | | | | | | | | | |__| /_/ \_\ |___| |___| |_____| |____| |____| |__| <<<<<<<<<<< NEVER GIVE UP !! >>>>>>>>>>> <<<<<<<< YOU CAN DO IT IF YOU WANT !! >>>>>>>> <<<<<<<<< BELIEVE IN YOURSELF !! >>>>>>>>> */ int main() { //cout << fixed << setprecision(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); AIA(); int a, b, c, d; cin >> a >> b >> c >> d; cout << (a * d) - (b * c); return 0; }
#include <bits/stdc++.h> #pragma region ppd #define ll long long #define ld long double #define mp make_pair #define mt make_tuple #define ff first #define ss second #define pb push_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define for0(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define forr(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define foriter(it, l) for (auto it = l.begin(); it != l.end(); it++) #define endl '\n' #define INF (ll)(1e9) #define EPS 1e-9 #define MOD (ll)(1e9 + 7) #define PI 3.1415926535897932384626433832795 #define set set #define mset multiset #define uset unordered_set #define umset unordered_multiset #define map map #define mmap multimap #define umap unordered_map #define ummap unordered_multimap #define FILEIO \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef long long i64; typedef vector<i64> vll; typedef vector<vll> vvll; typedef pair<i64, i64> pll; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class T> void re(T &x) { cin >> x; } template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); } template <class T> void re(vector<T> &a) { for0(i, a.size()) re(a[i]); } template <class T> void pr(T &x) { cout << x << " "; } template <class Arg, class... Args> void pr(Arg &first, Args &...rest) { pr(first); pr(rest...); } template <class T> void pr(vector<T> &a) { for0(i, a.size()) pr(a[i]); } #pragma endregion ppd //=============================================================================// ll g1(ll n) { mset<ll, greater<ll>> s; while(n) { s.insert(n%10); n/=10; } ll ret = 0; for(auto &d: s) { ret = ret*10 + d; } return ret; } ll g2(ll n) { mset<ll> s; while(n) { s.insert(n%10); n/=10; } ll ret = 0; for(auto &d: s) { ret = ret*10 + d; } return ret; } void solve() { ll n, k; re(n, k); for0(i, k) { ll newn = g1(n) - g2(n); //cout<<endl<<endl; n = newn; } pr(n); } int main() { #ifndef ONLINE_JUDGE FILEIO auto start = chrono::high_resolution_clock::now(); #endif ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); //================================================================================== int T = 1; //cin >> T; while (T--) { solve(); cout << endl; } //================================================================================== #ifndef ONLINE_JUDGE auto finish = chrono::high_resolution_clock::now(); chrono::duration<double> elapsed = finish - start; cout << "\nTime: " << elapsed.count() << " s\n"; #endif return 0; }
#include<bits/stdc++.h> using namespace std; int n,k,nn,minn,maxx; string nts(int x) { string res=""; while(x) { res=(char)(x%10+'0')+res; x/=10; } return res; } int stn(string x) { int res=0; for(int i=0;i<x.size();i++) { res*=10; res+=x[i]-'0'; } return res; } int main() { cin>>n>>k; for(int i=0;i<k;i++) { string s=nts(n); sort(s.begin(),s.end()); minn=stn(s); reverse(s.begin(),s.end()); maxx=stn(s); nn=maxx-minn; if(nn==n) { cout<<n; return 0; } n=nn; } cout<<n; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define fill(a,b) memset(a,b,sizeof(a)) #define f first #define s second #define pb push_back #define inf 1e17 #define ninf -1*1e17 #define mod 1000000007 #define N 500005 #define Ns 5005 #define hsum 600000 string s; ll n,m,o,x,y,z,d,k,l,r; ll a[N]; ll dp[102][hsum]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while(t--){ // ll ans=0; cin>>n>>k>>m; dp[0][0]=1; //dp[1][1]=1; for(int i=1;i<=n;i++){ for(int j=0;j<=hsum;j++){ ll cnt=0; if(j-i>=0){ cnt=(cnt+dp[i][j-i])%m; } if(i-1>=0){ cnt=(cnt+dp[i-1][j])%m; } if(i-1>=0 && (j-(k+1)*i) >=0){ cnt=(cnt-dp[i-1][j-(k+1)*i]+m)%m; } dp[i][j]=cnt; //cout<<dp[i][j]<<" "; } //cout<<"\n"; } for(int i=1;i<=n;i++){ ll ans=0; for(int j=1;j<=hsum;j++){ if(i-1>=0 && n-i >=0) ans=(ans+(dp[i-1][j]*dp[n-i][j])%m)%m; } ans=(ans*(k+1))%m; ans=(ans-1+m)%m; cout<<ans<<"\n"; } //cout<<ans<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; // ModIntR::set_mod() struct ModIntR { int x; ModIntR() : x(0) {} ModIntR(int64_t y) : x(y >= 0 ? y % mod() : (mod() - (-y) % mod()) % mod()) {} static int &mod() { static int mod = 0; return mod; } static void set_mod(int md) { mod() = md; } ModIntR &operator+=(const ModIntR &p) { if ((x += p.x) >= mod()) x -= mod(); return *this; } ModIntR &operator-=(const ModIntR &p) { if ((x += mod() - p.x) >= mod()) x -= mod(); return *this; } ModIntR &operator*=(const ModIntR &p) { long long m = mod(); x = (int)(1LL * x * p.x % m); return *this; } ModIntR &operator/=(const ModIntR &p) { *this *= p.inverse(); return *this; } ModIntR operator-() const { return ModIntR(-x); } ModIntR operator+(const ModIntR &p) const { return ModIntR(*this) += p; } ModIntR operator-(const ModIntR &p) const { return ModIntR(*this) -= p; } ModIntR operator*(const ModIntR &p) const { return ModIntR(*this) *= p; } ModIntR operator/(const ModIntR &p) const { return ModIntR(*this) /= p; } bool operator==(const ModIntR &p) const { return x == p.x; } bool operator!=(const ModIntR &p) const { return x != p.x; } ModIntR inverse() const { int a = x, b = mod(), u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModIntR(u); } ModIntR pow(int64_t n) const { ModIntR res(1), mul(x); while (n) { if (n & 1) res *= mul; mul *= mul; n >>= 1; } return res; } friend ostream &operator<<(ostream &os, const ModIntR &p) { return os << p.x; } friend istream &operator>>(istream &is, ModIntR &a) { int64_t t; is >> t; a = ModIntR(t); return (is); } }; int n, k, m, ofst = 2e6, len; vector<ModIntR> cnt, memo; void add(int x); void sub(int x); bool isvalid(int x) { return -len + ofst <= x && x <= len + ofst; } int main() { cin >> n >> k >> m; len = n * (n + 1) / 2 * k; ModIntR::set_mod(m); cnt.assign((int)4e6, 0); memo.assign((int)4e6, 0); cnt[ofst] = 1; for (int i = 1; i <= n; ++i) add(i); for (int i = 1; i <= n; ++i) { // for (int j = -6; j <= 6; ++j) cout << cnt[j + ofst] << " "; // cout << endl; if (n + 1 - i != 0) sub(n + 1 - i); // for (int j = -6; j <= 6; ++j) cout << cnt[j + ofst] << " "; // cout << endl; cout << (cnt[ofst] * (k + 1)) - 1 << endl; add(-i); // for (int j = -6; j <= 6; ++j) cout << cnt[j + ofst] << " "; // cout << endl; } return 0; } void add(int x) { vector<ModIntR> sum(abs(x), 0); int sign = x / abs(x); x = abs(x); for (int i = -len * sign + ofst; isvalid(i); i += sign) { sum[i % x] -= memo[i]; memo[i] = 0; ModIntR tmp = sum[i % x]; sum[i % x] += cnt[i]; if (isvalid(i + sign * x * (k + 1))) memo[i + sign * x * (k + 1)] = cnt[i]; cnt[i] += tmp; } } void sub(int x) { vector<ModIntR> sum(abs(x), 0); int sign = x / abs(x); x = abs(x); for (int i = -len * sign + ofst; isvalid(i); i += sign) { sum[i % x] -= memo[i]; memo[i] = 0; ModIntR tmp = sum[i % x]; cnt[i] -= tmp; sum[i % x] += cnt[i]; if (isvalid(i + sign * x * (k + 1))) memo[i + sign * x * (k + 1)] = cnt[i]; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define all(a) (a).begin(),(a).end() template<typename T1, typename T2> inline void chkmin(T1 & x, const T2 & y) {if (y < x) x = y;} template<typename T1, typename T2> inline void chkmax(T1 & x, const T2 & y) {if (x < y) x = y;} void no() { cout << "No\n"; exit(0); } const int N = 210; int n; int a[N], b[N]; void read() { cin >> n; set<int> setik; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; if (a[i] != -1 && setik.count(a[i])) no(); if (b[i] != -1 && setik.count(b[i])) no(); setik.insert(a[i]); setik.insert(b[i]); } } bool used[N]; bool check(int l, int r) { //l = 2 * l + 1; //r = 2 * r; //cout << "l = " << l << " r = " << r << endl; if (r <= l) return false; if ((r - l + 1) % 2) return false; fill(used, used + N, false); int c = (r - l + 1) / 2; //cout << "c = " << c << endl; for (int i = 0; i < n; ++i) { int x = a[i], y = b[i]; //cout << "x = " << x << " y = " << y << endl; if (x == -1 && y == -1) continue; if (x != -1 && y != -1 && x >= y) return false; bool must = (x >= l && x <= r) || (y >= l && y <= r); if (x == -1) { x = y - c; } else if (y == -1) { y = x + c; } if (x > r || y < l) continue; if ((x < l || y > r) && must) return false; if ((x < l || y > r)) continue; if (y - x != c) return false; if (used[x] || used[y]) return false; //cout << "x = " << x << "y = " << y << endl; used[x] = true; used[y] = true; } return true; } bool dp[N]; void calcDp() { //cout << check(1, 4) << endl; //exit(0); /*cout << check(1, 3) << endl; exit(0); for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { cout << check(i, j) << " "; } cout << endl; }*/ //exit(0); dp[0] = 1; for (int i = 1; i <= 2 * n; ++i) { for (int j = 0; j < i; ++j) { dp[i] |= dp[j] && check(j + 1, i); } } } void run() { calcDp(); } void write() { if (!dp[2 * n]) { no(); } cout << "Yes\n"; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(20), cout.setf(ios::fixed); read(); run(); write(); return 0; }
#include<bits/stdc++.h> using namespace std; #define GO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long int #define endl "\n" const int maxr = 1e6 + 5; int gcd(int a, int b, int& x, int& y) { if (b == 0) { x = 1; y = 0; return a; } int x1, y1; int d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) { g = gcd(abs(a), abs(b), x0, y0); if (c % g) { return false; } x0 *= c / g; y0 *= c / g; if (a < 0) x0 = -x0; if (b < 0) y0 = -y0; return true; } int better_less(int n, int v, bool t) { int val = 0; if(t) val = ceil(abs(n) / (v*1.0)); else val = -(n / v); n = n + val*v; return n; } int32_t main() { GO; int t; cin >> t; while(t--) { int x, y, p, q; cin >> x >> y >> p >> q; int INF = 2e18; int ans = INF; for(int v = 0; v < y; v++) { for(int e = 0; e < q; e++) { int c = (p + e - x - v); int a = (2*x + 2*y); int b = -(p + q); int n, h, g; if(find_any_solution(a, b, c, n, h, g)) { /* some n and h such that we have time minimium*/ // cout << n << " " << h << endl; // cout << "^^^^^^^^" << endl; // cout << abs(b / g) << " " << abs(a / g) << endl; // cout << "vvvvvvvv" << endl; if(n < 0) n = better_less(n, abs(b / g), 1); else n = better_less(n, abs(b / g), 0); if(h < 0) h = better_less(h, abs(a / g), 1); else h = better_less(h, abs(a / g), 0); // cout << n << " " << h << endl; int time1 = (2*x + 2*y)*n + x + v; int time2 = (p + q)*h + p + e; ans = min(ans, min(time1, time2)); } } } if(ans >= INF) cout << "infinity" << endl; else cout << ans << endl; } return 0; }
#include <iostream> #include <queue> #include <string> #include <random> using namespace std; int maze[50][50]; bool used[50][50]; //"RDUL" string moves = "RDUL"; int dx[4] = {0,1,-1,0}; int dy[4] = {1,0,0,-1}; int vals[50][50]; string fans = ""; long long fscore = 0; const int inf = 1e9; void dfs(int depth,int x,int y,string now,long long score){ used[x][y] = true; score += (long long)vals[x][y]; if (depth == 0){ if (fscore < score){ fscore = score; fans = now; } return; } int badx = -1,bady = -1; for (int k = 0; k < 4;k++){ int nx = x + dx[k]; int ny = y + dy[k]; if (0 <= nx && nx < 50 && 0 <= ny && ny < 50 && maze[nx][ny] == maze[x][y]){ if (!used[nx][ny]){ badx = nx; bady = ny; } used[nx][ny] = true; } } vector<bool> can(4); vector<pair<int,int>> topoint(4); for (int k = 0; k < 4;k++){ int nx = x + dx[k]; int ny = y + dy[k]; topoint[k] = {nx,ny}; if (0 <= nx && nx < 50 && 0 <= ny && ny < 50 && maze[nx][ny] != maze[x][y] && !used[nx][ny]){ can[k] = true; } } bool candoit = false; for (int k = 0; k < 4;k++){ if (can[k]) { candoit = true; dfs(depth-1,topoint[k].first,topoint[k].second,now+moves[k],score); } } if (candoit){ if (fscore < score){ fscore = score; fans = now; } return; } if (badx > 0){ used[badx][bady] = false; } used[x][y] = false; } int main(){ int sx,sy; // start point cin >> sx >> sy; for (int i = 0; i < 50;i++){ for (int j = 0; j < 50;j++){ cin >> maze[i][j]; } } for (int i = 0; i < 50;i++){ for (int j = 0; j < 50;j++){ cin >> vals[i][j]; } } for (int k = 0; k < 4;k++){ int nx = sx + dx[k]; int ny = sy + dy[k]; if (0 <= nx && nx < 50 && 0 <= ny && ny < 50 && maze[nx][ny] == maze[sx][sy]){ used[nx][ny] = true; } } dfs(inf,sx,sy,"",0); cout << fans << endl; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define dbl(k, x) fixed << setprecision(k) << (x) template <typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template <typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #define _upgrade \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) // ********************** CODE ********************** // const int nax = 107; vector<int> G[nax]; vector<int> tin(nax); set<pair<int, int>> SCC_edges_orientation; int counter = 1; void dfs(int u, int p) { tin[u] = counter; counter++; for (auto &v : G[u]) { if (v == p) { continue; } if (tin[v] == 0) { SCC_edges_orientation.emplace(u, v); dfs(v, u); } else if (tin[v] < tin[u]) { SCC_edges_orientation.emplace(u, v); } } } int main() { _upgrade; int n, m; cin >> n >> m; vector<pair<int, int>> edges(m); vector<string> direction(m); vector<int> reachable_cnt(n); for (auto &p : edges) { cin >> p.first >> p.second; p.first--; p.second--; } for (auto &v : reachable_cnt) { cin >> v; } for (int i = 0; i < m; i++) { int u = edges[i].first; int v = edges[i].second; if (reachable_cnt[u] < reachable_cnt[v]) { direction[i] = "<-"; } else if (reachable_cnt[u] > reachable_cnt[v]) { direction[i] = "->"; } else { G[u].push_back(v); G[v].push_back(u); } } for (int i = 0; i < n; i++) { if (tin[i] == 0) { dfs(i, -1); } } for (int i = 0; i < m; i++) { if (direction[i] != "") { continue; } if (SCC_edges_orientation.find(edges[i]) != SCC_edges_orientation.end()) { direction[i] = "->"; } else { direction[i] = "<-"; } } for (auto &dir : direction) { cout << dir << "\n"; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; #define ll long long #define ull unsigned long long #define rep(i,n,N) for(ll i=n;i<=N;++i) #define asd(i,n,N) for(int i=n;i< N;++i) #define rap(i,n,N) for(int i=n;i>=N;--i) #define mp make_pair #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define fi first #define se second //#define ff fi.fi //#define fs fi.se #define sf se.fi #define ss se.se #define lc (id<<1) #define rc ((id<<1)|1) #define db(x) cout << ">>>> " << #x << " -> " << x << endl #define mems(x,y) memset(x,y,sizeof x) #define all(x) x.begin(),x.end() #define rng() ((rand()<<16)|rand()) #define pii pair<int,int> #define pll pair<ll,ll> #define plll pair<ll,pll> #define piii pair<int,pii> #define piiii pair<pii,pii> #define psi pair<string,int> #define endl '\n' const int MAX = 5e3+5; const ll MAX2 = 11; const ll MOD = 998244353; const ll MOD2 = 2010405347; const ll INF = 2e18; const int dr[]={1,0,-1,0,1,1,-1,-1,0}; const int dc[]={0,1,0,-1,1,-1,1,-1,0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 555; int n,m,a,b,k,y[MAX][MAX],z[MAX][MAX]; char x[MAX][MAX]; ll dp[MAX][MAX],pre[MAX*MAX]; ll f(int r,int c){ if(r>n || c>m)return 0; if(r==n && c==m)return 1; ll &ret = dp[r][c]; if(ret!=-1)return ret; ret = 0; if(x[r][c]){ if(x[r][c]=='R')ret = f(r,c+1)*pre[y[r][c]]; else if(x[r][c]=='D')ret = f(r+1,c)*pre[z[r][c]]; else ret = f(r,c+1)*pre[y[r][c]] + f(r+1,c)*pre[z[r][c]]; } else ret = (f(r,c+1)*pre[y[r][c]-1] + f(r+1,c)*pre[z[r][c]-1]) << 1; return ret%= MOD; } int main(){ // cout<<fixed<<setprecision(10); // freopen("trip.in", "r", stdin); // freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m>>k; rep(i,1,k){ cin>>a>>b; cin>>x[a][b]; } k = n*m; pre[0] = 1; rep(i,1,k)pre[i] = pre[i-1]*3%MOD; rap(i,n,1)rap(j,m,1){ y[i][j] = (!x[i][j]) + y[i+1][j]; z[i][j] = (!x[i][j]) + z[i][j+1]; } mems(dp,-1); ll ans = f(1,1); if(!x[n][m])ans = ans*3%MOD; cout<<ans<<endl; return 0; }
# include <iostream> # include <vector> using namespace std; const int kInf = 1 << 30; bool flg; int n, m; int main() { scanf("%d %d", &n, &m); vector<int> dp(1 << n, kInf), cnt(n + 1); for (int i = 1; i <= n; i++) { cnt[i] |= (1 << (i - 1)); } for (int i = 1, u, v; i <= m; i++) { scanf("%d %d", &u, &v); cnt[u] |= (1 << (v - 1)), cnt[v] |= (1 << (u - 1)); } dp[0] = 0; for (int i = 1; i < 1 << n; i++) { flg = 1; for (int j = 1; j <= n; j++) { if (i >> (j - 1) & 1 && (cnt[j] & i) != i) { flg = 0; } } if (flg) { dp[i] = 1; } else { for (int j = i; j; j = (j - 1) & i) { dp[i] = min(dp[i], dp[j] + dp[i - j]); } } } printf("%d", dp[(1 << n) - 1]); return 0; }
//-------All power is within you,you can do Anything and Everything.---------// #include <bits/stdc++.h> // #pragma GCC optimize("Ofast") using namespace std; //library //*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ M $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*// #define F first #define S second #define pb push_back #define mkp make_pair #define int long long #define ld long double //scankar #define sz(x) (int)(x).size() #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define fo(i,a,b) for(int i=a;i<b;i++) #define rfo(i,b,a) for(int i=b;i>=a;i--) #define mem( a, val ) memset(a, val, sizeof( a ) ) #define deci( x ) cout<<fixed<<setprecision( x ); #define bitcount( x ) __builtin_popcountll( x ) #define endl "\n" #define XOX typedef vector<int> vi; typedef pair<int,int> pi; typedef map<int,int> mii; typedef map<int,bool> mib; typedef map<char,int> mci; typedef map<string,int> msi; const int INF = 1e18L + 5; const int MAX = 1500 + 2; const int MOD = 998244353; const long double PI =3.141592653589793238463; //direction //*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ D $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*// #define deb(x) cout<<#x<<" "<<x<<endl; //debugger //*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ V $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*// int i,j,k,t,n,q,l,r,mid; //*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ F $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*// vector<pi> v; int nn,mm,x,y,cnt,m; int a[MAX][MAX]; void solve() { cin>>n>>m; cin>>nn>>mm; fo(i,0,nn) { cin>>x>>y; a[x][y]=1; v.pb({x,y}); } fo(i,0,mm) { cin>>x>>y; a[x][y]=-1; } fo(i,0,nn) { x=v[i].F; y=v[i].S; fo(i,x+1,n+1) { if(a[i][y]==-1||a[i][y]>=1)break; a[i][y]++; } } fo(i,0,nn) { x=v[i].F; y=v[i].S; rfo(i,x-1,1) { if(a[i][y]==-1||a[i][y]>=2)break; a[i][y]++; } } fo(i,0,nn) { x=v[i].F; y=v[i].S; fo(i,y+1,m+1) { if(a[x][i]==-1||a[x][i]>=3)break; a[x][i]++; } } fo(i,0,nn) { x=v[i].F; y=v[i].S; rfo(i,y-1,1) { if(a[x][i]==-1||a[x][i]>=4)break; a[x][i]++; } } fo(i,1,n+1) { fo(j,1,m+1) { // cout<<a[i][j]<<" "; if(a[i][j]>0)cnt++; } // cout<<endl; } cout<<cnt<<endl; } //*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*// // THE GAME STARTS HERE // int32_t main() { // int start,stop=clock(); // cout<<"time:"<<(stop-start)/double(CLOCKS_PER_SEC)*1000<<endl; ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); // sc(t);while(t--) // cin>>t;while(t--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int h, w, n, m; cin >> h >> w >> n >> m; int che[h][w]; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ che[i][j] = 0; } } pair<int, int> light[n]; for(int i = 0; i < n; i++){ cin >> light[i].first >> light[i].second; light[i].first--; light[i].second--; che[light[i].first][light[i].second] = 2; } pair<int, int> wall; for(int i = 0; i < m; i++){ cin >> wall.first >> wall.second; wall.first--; wall.second--; che[wall.first][wall.second] = -1; } for(int i = 0; i < n; i++){ pair<int, int> s = light[i]; for(int j = 0; j < 4; j++){ if(j == 0){ int t = 1; while(1){ if(s.first + t >= h){ break; } if(che[s.first + t][s.second] == -1 || che[s.first + t][s.second] == 2){ break; }else if(che[s.first + t][s.second] == 0){ che[s.first + t][s.second] = 1; } t++; } }else if(j == 1){ int t = 1; while(1){ if(s.first - t < 0){ break; } if(che[s.first - t][s.second] == -1 || che[s.first - t][s.second] == 2){ break; }else if(che[s.first - t][s.second] == 0){ che[s.first - t][s.second] = 1; } t++; } }else if(j == 2){ int t = 1; while(1){ if(s.second + t >= w){ break; } if(che[s.first][s.second + t] == -1 || che[s.first][s.second + t] == 2){ break; }else if(che[s.first][s.second + t] == 0){ che[s.first][s.second + t] = 1; } t++; } }else{ int t = 1; while(1){ if(s.second - t < 0){ break; } if(che[s.first][s.second - t] == -1 || che[s.first][s.second - t] == 2){ break; }else if(che[s.first][s.second - t] == 0){ che[s.first][s.second - t] = 1; } t++; } } } } int ans = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(che[i][j] == 1 || che[i][j] == 2){ ans++; } } } cout << ans << endl; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define ull unsigned ll #define uint unsigned int #define db double #define pint pair<int,int> #define mk make_pair #define pb push_back #define eb emplace_back #define ins insert #define fi first #define se second #define Rep(x,y,z) for(int x=(y);x<=(z);x++) #define Red(x,y,z) for(int x=(y);x>=(z);x--) using namespace std; const int MAXN=1e5+5; char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny; inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;} //inline char gc(){return getchar();} inline int read(){ int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc; for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny; } int n; inline int M(int x){return ((x-1)%n+n)%n+1;} int main(){ // freopen("std.in","r",stdin); // freopen("std.out","w",stdout); n=read();Rep(i,1,n)cout<<M(i<<1)<<' '<<M(i<<1|1)<<'\n'; return 0; }
#include <bits/stdc++.h> #define pb push_back #define int long long #define ld long double using namespace std; const int M = 998244353; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; for (int x = 0; x < n; x++) { cout << 1 + (2 * x) % n << ' ' << 1 + (2 * x + 1) % n << '\n'; } }
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<cmath> #include<iomanip> #include<map> 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; } const long long INF = 1LL << 60; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(){ long long n; cin>>n; if(n%2==0) cout<<"White"<<endl; else cout<<"Black"<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){int n;vector<int>a;cin>>n;for(int i=0,ai;i<n;i++)cin>>ai,a.push_back(ai); if(set<int>(a.begin(),a.end()).size()==a.size())cout<<"Yes";else cout<<"No";}
//----AUTHOR:developer.eeshan----/ #include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define it(a, x) for (auto &a : x) #define ll long long #define ld long double #define pb push_back #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vpii vector<pii> #define vs vector<string> #define gi greater<int> #define vc vector<char> #define vvi vector<vi> #define vvll vector<vll> #define vvc vector<vc> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define deb(x) cout << #x << "=" << x << endl; #define in(i, n) for (auto i = 0; i < n; i++) #define all(a) begin(a), end(a) #define rall(a) rbegin(a), rend(a) //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //random shuffler ll fxp(ll a, ll b) { a %= mod; ll res = 1; while (b > 0) { if (b % 2 == 1) res = (res * a) % mod; a = (a * a) % mod; b /= 2; } return res % mod; } inline ll minv(ll x) { return fxp(x, mod - 2); } inline ll mmul(ll a, ll b) { if (a >= mod) a = a % mod; if (b >= mod) b = b % mod; if (a * b >= mod) return (a * b) % mod; return (a * b); } inline ll madd(ll a, ll b) { if (a >= mod) a = a % mod; if (b >= mod) b = b % mod; if (a + b >= mod) return (a + b) % mod; return (a + b); } inline ll msub(ll a, ll b) { if (a >= mod) a = a % mod; if (b >= mod) b = b % mod; return (((a - b) % mod + mod) % mod); } inline ll mdiv(ll a, ll bb) { if (a >= mod) a = a % mod; ll b = minv(bb); if (b >= mod) b = b % mod; if (a * b >= mod) return (a * b) % mod; return (a * b); } void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif } const int mXn = 2e5 + 5; int n, m, h[mXn], w[mXn], o[mXn], e[mXn]; void solve() { cin >> n >> m; in(i, n)cin >> h[i]; in(i, m)cin >> w[i]; sort(h, h + n); //in(i, n)cout << h[i] << ' '; //cout << endl; o[1] = h[1] - h[0]; for (int i = 3; i < n; i += 2) o[i] = o[i - 2] + (h[i] - h[i - 1]); for (int i = 2; i < n; i += 2) e[i] = e[i - 2] + (h[i] - h[i - 1]); ll ans = 1e15; in(i, m) { int pos = upper_bound(h, h + n, w[i]) - h - 1; //pos=-1, pos=n-1 //cout << "for " << w[i] << " pos is " << pos; ll cans = 0; if (pos == -1) { cans += e[n - 1] + (h[0] - w[i]); } else if (pos == n - 1) { cans += o[n - 2] + (w[i] - h[n - 1]); } else { if (pos % 2 == 1) pos++; cans += abs(w[i] - h[pos]) + o[pos - 1] + (e[n - 1] - e[pos]); } //cout << " cans is: " << cans << endl; ans = min(ans, cans); } cout << ans; } int main(int argc, char const *argv[]) { c_p_c(); solve(); return 0; }
// E - Transformable Teacher #include <bits/stdc++.h> using namespace std; int main(){ int n, m; cin>>n>>m; vector<int> H(n), W(m), evn(1), odd(1); // 偶・奇数番目の累積和 for(int& x:H) cin>>x; for(int& x:W) cin>>x; sort(H.begin(), H.end()); for(int i=0; i<n-1; ++i){ int d = H[i+1] - H[i]; if(i%2) odd.push_back(odd.back() + d); else evn.push_back(evn.back() + d); } int64_t ans = LLONG_MAX; for(int w:W){ int r = upper_bound(H.begin(), H.end(), w) - H.begin(); int i = r / 2; r -= r%2; ans = min(ans, (int64_t)evn[i] + abs(w - H[r]) + odd.back() - odd[i]); } cout<< ans <<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int L; cin >> L; long long over=1; vector<int> ok(10); for(int i=1; i<=11; i++) { over *= (L-i); for(int k=2; k<=11; k++) { if( (over%k) == 0 && ok.at(k-2) == 0) { over /= k; ok.at(k-2) = 1; } } } cout << over << endl; }
#ifdef DEBUG_BUILD # define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #ifdef DEBUG_BUILD # include "debugger.hpp" #else # define debug(...) # define debugdo(...) #endif using namespace std; using ll = long long; using ld = long double; using pll = std::pair<ll, ll>; template <class T> using vec = std::vector<T>; template <class T> using vec2 = std::vector<vec<T>>; template <class T> using vec3 = std::vector<vec2<T>>; #define rep(i, N) for (ll i = 0; i < (N); i++) #define rrep(i, N) for (ll i = (N) - 1; i >= 0; i--) #define range(i, A, B) for (ll i = (A); i < (B); i++) constexpr int MOD = 1000000007; constexpr ll INFL = std::numeric_limits<ll>::max(); template <class T> bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; } void Main() { string S; cin >> S; reverse(S.begin(), S.end()); for (auto& c : S) { switch(c) { case '6': c = '9'; break; case '9': c = '6'; break; } } cout << S << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(10); Main(); std::cout << std::flush; std::cerr << "--end--" << std::flush; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define FOR(i, n) for(int (i)=0; (i)<(n); (i)++) #define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--) template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);} template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);} template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); } template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); } double x, y, r; bool dist(double x1, double y1, double x2, double y2){ ll xx1 = round(x1 * 1e4); ll yy1 = round(y1 * 1e4); ll xx2 = round(x2 * 1e4); ll yy2 = round(y2 * 1e4); ll r2 = round(r * 1e4); return (xx1-xx2)*(xx1-xx2) + (yy1-yy2)*(yy1-yy2) <= r2*r2; } const int inf = 1e9; int bs(int x2, int dir){ int y2 = ceil(y); if (dir == -1) y2 = floor(y); if (!dist(x2, y2, x, y)) return inf; int a = 0, b = r+1, ret = 0; while(a <= b){ int c = (a+b)/2; if (dist(x2, y2+dir*c, x, y)){ ret = c; a = c+1; } else{ b = c-1; } } return y2 + dir * ret; } int main(int argc, char** argv) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15); if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin); cin >> x >> y >> r; ll ans = 0; for(double dx = -r-10; dx <= r+10; dx++){ int x2 = floor(x + dx); int ymax = bs(x2, 1); int ymin = bs(x2, -1); if (ymax != inf && ymin != inf) ans += ymax - ymin + 1; else if (ymax != inf) ans += ymax - (int)ceil(y) + 1; else if (ymin != inf) ans += (int)floor(y) - ymin + 1; } cout << ans << endl; if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using s64 = signed long long; using u64 = unsigned long long; using f128 = long double; s64 GetCeilS64(s64 fixed, s64 digit) { if(fixed % digit == 0) { return fixed; } if(fixed > 0) { return ((fixed / digit) + 1) * digit; } else { return (fixed / digit) * digit; } } s64 GetFloorS64(s64 fixed, s64 digit) { if(fixed % digit == 0) { return fixed; } if(fixed > 0) { return (fixed / digit) * digit; } else { return ((fixed / digit) - 1) * digit; } } int main() { f128 x, y, r; cin >> x >> y >> r; s64 digit = pow(10, 4); s64 xInt = llround(x * digit); s64 yInt = llround(y * digit); s64 rInt = llround(r * digit); u64 countInt = 0; s64 startX = GetCeilS64(xInt - rInt, digit); s64 endX = GetFloorS64(xInt + rInt, digit); for(s64 i = startX; i <= endX; i += digit) { s64 rIntSq = rInt*rInt; s64 xIntSq = (i-xInt) * (i-xInt); s64 yIntSq = rIntSq - xIntSq; s64 p = sqrtl(yIntSq); s64 startY = GetCeilS64(yInt - p, digit); s64 endY = GetFloorS64(yInt + p, digit); if(startY > endY) { continue; } u64 subCount = endY/digit - startY/digit + 1; countInt += subCount; } cout << countInt << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++) #define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++) #define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--) #define deb(x) std::cout << #x << " " << x << endl; #define debl(x) std::cout << #x << " " << x << " "; #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; namespace std{ template<class Fun> class y_combinator_result{ Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){} template<class ...Args> decltype(auto) operator()(Args&&...args){ return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun && fun){ return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } }; template<typename T> bool umax(T& a, T b){ bool ret = a < b; if(ret) a = b; return ret; } template<typename T> bool umin(T& a, T b){ bool ret = a > b; if(ret) a = b; return ret; } struct edge{ int to; ll cost; int from; edge(){ edge(0,0);} edge(int to_, ll cost_) : to(to_), cost(cost_){} edge(int to_, int from_, ll cost_) : to(to_), cost(cost_), from(from_){} }; template<typename... T> void read(T& ... a){ ((cin >> a),...); } template<typename... T> void write(T... a){ ((cout << a),...); } template<typename T> vector<T> read_array(int sz){ vector<T> ret(sz); for(auto & x : ret) cin >> x; return ret; } void solve(){ int n; read(n); map<int, int> mp; REP(i, n){ int a; read(a); mp[a]++; } vi b(n), c(n); REP(i, n) read(b[i]); REP(i, n) read(c[i]); ll ans = 0; REP(i, n){ int x = b[c[i] - 1]; if(mp.find(x) != mp.end()){ ans += mp[x]; } } write(ans); } int main() { //making data IO Fast std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); /****************************/ int T = 1; // cin >> T; while(T--) solve(); return 0; }
#include <bits/stdc++.h> #define fo(i, k, n) for (ll i = k; i < n; i++) #define rfo(i, k, n) for (ll i = k; i >= n ; i--) #define ll long long #define ld long double #define que queue #define pb push_back #define mp make_pair #define vi vector<int> #define vl vector<ll> #define gcd(m,n) __gcd( m, n) #define sq(x) (x*x) #define rev(v) reverse(v.begin(),v.end()) #define srt(v) sort(v.begin(),v.end()) #define grtsrt(v) sort(v.begin(),v.end(),greater<int>()) #define mnv(v) *min_element(v.begin(),v.end()) #define mxv(v) *max_element(v.begin(),v.end()) #define all(v) v.begin(),v.end() #define toInteger(s) stoll(s) #define toString(num) to_string(num) using namespace std; ll MOD = 1e9 + 7; //--------------------------------------------functions-------------------------------------------------// ll power(ll a,ll b){ll result=1;while(b>0){if(b%2 == 1){result *= a;} a *= a;b /= 2;}return result;} ll countSetBits(ll x){ll Count=0;while(x>0){if(x&1) Count++;x=x>>1;}return Count;} bool isPerfectSquare(ll n){ll sr = sqrt(n);if (sr * sr == n)return true;else return false;} ll mod(ll x,ll M){return ((x%M + M)%M);} ll mul(ll a, ll b,ll M){return mod(mod(a,M)*mod(b,M),M);} ll powerM(ll a,ll b,ll M){ ll res=1ll; while(b){ if(b%2ll==1ll){ res=mul(a,res,M); } a=mul(a,a,M);b/=2ll; } return res; } int log22(long long x) { return 64 - __builtin_clzll(x) - 1; } //--------------------------------------------SieveOfEratosthenes-------------------------------------------------// //mem will have true if its prime. void SieveOfEratosthenes( vector<bool>&mem) { ll n = 1e6 +1; bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } fo(i,2,n) if(prime[i])mem[i]=true; } //--------------------------------------------solve-------------------------------------------------// int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int n; cin>>n; vector<int>v(n); fo(i,0,n) cin>>v[i]; ll ans = 0; fo(i,0,n) { if(v[i]>10) ans += v[i]-10; } cout<<ans; return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <cassert> #include <cmath> #include <string> #include <queue> #include <set> #include <map> #include <cstdlib> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define li long long #define pii pair<int, int> #define vi vector<int> #define li long long #define forn(i, n) for (int i = 0; i < (int)n; i++) #define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++) const int MAXN = 2005; const int INF = 2e9; vector<pii> edges[MAXN]; int dist[MAXN]; int main() { int n, m; scanf("%d%d", &n, &m); forn(i, m) { int x, y, w; scanf("%d%d%d", &x, &y, &w); edges[x].pb({y, w}); } fore(start, 1, n) { set<pii> order; fore(u, 1, n) { dist[u] = INF; } dist[start] = 0; int ans = INF; order.insert({0, start}); while (!order.empty()) { pii f = *order.begin(); order.erase(order.begin()); int v = f.se; if (dist[v] != f.fi) { continue; } for (pii e : edges[v]) { int u = e.fi; if (dist[u] > dist[v] + e.se) { dist[u] = dist[v] + e.se; order.insert({dist[u], u}); } if (u == start) { ans = min(ans, dist[v] + e.se); } } } if (ans == INF) { ans = -1; } printf("%d\n", ans); } }
#include <bits/stdc++.h> using namespace std; #define ar array #define ll long long const int MAX_N = 1e5 + 1; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e18; ll helper(ll a){ vector<int> vec(10,0); while(a){ vec[a%10]++; a/=10; } ll v1=0,v2=0; for(int i=0;i<10;i++){ for(int j=0;j<vec[i];j++)v2=v2*10+i; } for(int i=9;i>=0;i--){ for(int j=0;j<vec[i];j++)v1=v1*10+i; } return v1-v2; } void solve() { ll n,k; cin>>n>>k; while(k){ ll tmp=helper(n); if(tmp==n){ cout<<tmp<<endl; return; } n=tmp; k--; } cout<<n<<endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int tc = 1; //cin >> tc; for (int t = 1; t <= tc; t++) { solve(); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) //g++ c.cpp //./a.out //oj d https://atcoder.jp/contests/abc205/tasks/abc205_c //oj t signed main(){ int a, b, c; cin >> a >> b >> c; if(c%2==0){ if(abs(a)>abs(b)){ cout << ">" << endl; return 0; } else if(abs(a)<abs(b)){ cout << "<" << endl; return 0; } else if (abs(a) == abs(b)) { cout << "=" << endl; return 0; } } else if(c%2==1){ if(a>b) { cout << ">" << endl; return 0; } else if(a<b){ cout << "<" << endl; return 0; } else if(a==b){ cout << "=" << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; int main(){ long long int A, B, C; cin >> A >> B >> C; long long int A1 = A % 10; if (A1 == 0){ cout << 0; } else if (A1 == 1){ cout << 1; } else if (A1 == 2){ if (B % 4 == 0){ cout << 6; } else if (B % 4 == 2){ if (C > 1) { cout << 6; } else { cout << 4; } } else if (B % 4 == 1){ cout << 2; } else { if (C % 2 == 0){ cout << 2; } else { cout << 8; } } } else if (A1 == 3){ if (B % 4 == 0){ cout << 1; } else if (B % 4 == 2){ if (C > 1) { cout << 1; } else { cout << 9; } } else if (B % 4 == 1){ cout << 3; } else { if (C % 2 == 0){ cout << 3; } else { cout << 7; } } } else if (A1 == 4){ if (B % 2 == 0){ cout << 6; } else { cout << 4; } } else if (A1 == 5){ cout << 5; } else if (A1 == 6){ cout << 6; } else if (A1 == 7){ if (B % 4 == 0){ cout << 1; } else if (B % 4 == 2){ if (C > 1) { cout << 1; } else { cout << 9; } } else if (B % 4 == 1){ cout << 7; } else { if (C % 2 == 0){ cout << 7; } else { cout << 3; } } } else if (A1 == 8){ if (B % 4 == 0){ cout << 6; } else if (B % 4 == 2){ if (C > 1) { cout << 6; } else { cout << 4; } } else if (B % 4 == 1){ cout << 8; } else { if (C % 2 == 0){ cout << 8; } else { cout << 2; } } } else if (A1 == 9){ if (B % 2 == 0){ cout << 1; } else { cout << 9; } } return 0; }
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; int main() { ll a, b, k; cin >> a >> b >> k; const int maxn = 60; ll C[maxn + 1][maxn + 1]; C[0][0] = 1; for (int n = 1; n <= maxn; ++n) { C[n][0] = C[n][n] = 1; for (int m = 1; m < n; ++m) C[n][m] = C[n - 1][m - 1] + C[n - 1][m]; } k--; string ans; while(a || b){ if(!b) { ans += 'a'; a--; }else if(!a){ ans += 'b'; b--; }else if(k < C[a+b-1][b]){ ans += 'a'; a--; }else { ans += 'b'; k -= C[a+b-1][b]; b--; } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); } template<class T> void _cout(T &&x){ cout << x << "\n"; } template <class Head, class... Tail> void _cout(Head&& head, Tail&&... tail){ cout << head << " "; _cout(forward<Tail>(tail)...); } template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T> &p){ cout << "[" << p.first << ", " << p.second << "]"; return os; } #define Sq(x) (x)*(x) #define For(i, n) for(int i = 0; i < (n); i ++) #define For1(i, n) for(int i = 1; i <= (n); i ++) #define Rof(i, n) for(int i = (n)-1; i >= 0; i --) #define Rep(n) For(_, n) #define Range(c) c.begin(), c.end() #define RevRange(c) c.rbegin(), c.rend() #define Contains(c, x) (find(Range(c), x) != c.end()) #define Search(rb, re, x) distance(rb, find(rb, re, x)) #define Sort(a) sort(Range(a)) #define DeSort(a) sort(RevRange(a)) #define Reverse(c) reverse(Range(c)) #define Unique(a) a.erase(unique(Range(a)), a.end()) #define Cusum(T, xs, sxs) vector<T> sxs(xs.size()+1); For(i, (int)xs.size()) sxs[i+1] = sxs[i] + xs[i] #define Cin(T, ...) T __VA_ARGS__; _cin(__VA_ARGS__) #define Cins(T, n, xs) vector<T> xs(n); For(i, n) cin >> xs[i] #define Cins2(T, n, xs, ys) vector<T> xs(n), ys(n); For(i, n) cin >> xs[i] >> ys[i] #define Cins3(T, n, xs, ys, zs) vector<T> xs(n), ys(n), zs(n); For(i, n) cin >> xs[i] >> ys[i] >> zs[i] #define Cinss(T, m, n, xs) vector<vector<T>> xs(m, vector<T>(n)); For(i, m) For(j, n) cin >> xs[i][j] #define Cinm(T, n, map) unordered_map<T, int> map; Rep(n){ Cin(T, x); map[x] ++; } #define Cout(...) _cout(__VA_ARGS__) #define Couts(xs) { for(const auto &e : xs) cout << e << " "; cout << "\n"; } #define Coutss(xs) { For(i, xs.size()) Couts(xs[i]); } #define Coutyn(cond) Cout((cond) ? "yes" : "no") #define CoutYn(cond) Cout((cond) ? "Yes" : "No") #define CoutYN(cond) Cout((cond) ? "YES" : "NO") #define Return(expr) { Cout(expr); return 0; } #define vc vector #define Vc2(T, m, n, xs) vc<vc<T>> xs(m, vc<T>(n)) #define Mini(a, x) a = min(a, x) #define Maxi(a, x) a = max(a, x) // constexpr int MOD = 1e9+7; // constexpr long double tau = 6.28318530717958647692; int main(void){ Cin(int, a, b); int n = a+b; Vc2(ll, n+1, n+1, c); c[0][0] = 1; For1(i, n) For1(j, n) c[i][j] = c[i-1][j-1] + c[i-1][j]; Cin(ll, k); string ans = ""; ll skipped = 0; Rep(n){ if(skipped + c[a+b][a] < k){ skipped += c[a+b][a]; ans += 'b'; b --; } else{ ans += 'a'; a --; } } Cout(ans); }
#include <bits/stdc++.h> #define i64 long long #define sz(a) int((a).size()) #define all(a) (a).begin(), (a).end() #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define per(i, a, b) for (int i = (b) - 1; i >= (a); --i) using namespace std; const int M = 3; struct mint { int x; mint(int x) : x(x) {} friend mint operator + (const mint &a, const mint b) {const int x = a.x + b.x; return mint(x >= M ? x - M : x);} friend mint operator - (const mint &a, const mint b) {const int x = a.x - b.x; return mint(x < 0 ? x + M : x);} friend mint operator * (const mint &a, const mint b) {return mint(1ll * a.x * b.x % M);} }; int Pw(int a, int x = M - 2, int res = 1) { for (; x; x >>= 1, a = (mint(a) * a).x) {if (x & 1) {res = (mint(res) * a).x;}} return res; } const int N = 4e5; int n; string s; struct par { int p, c; par(int x) : p(x), c(0) {while (!(p % M)) {p /= M, ++c;}} }; int main() { // freopen("c.in", "r", stdin); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> s; int res = 0; par f = 1; rep(i, 0, n) { int x = s[i] == 'B' ? 0 : (s[i] == 'W' ? 1 : 2); // cout << f.p << ' ' << f.c << '\n'; // if (!f.c) {res = (res + mint(f.p) * x).x;} if (i == n - 1) {break;} par m = n - 1 - i, d = i + 1; f.p = (mint(f.p) * m.p * Pw(d.p)).x; f.c = f.c + m.c - d.c; } if (!(n & 1)) {res = (mint(0) - res).x;} cout << (res == 0 ? 'B' : (res == 1 ? 'W' : 'R')) << '\n'; return 0; } /* 📌https://www.cnblogs.com/George1123/p/tips.html */
#include <iostream> #include <vector> #define mod 998244353 using namespace std; int n, k; vector <vector <int> > a; vector <int> hgroups; vector <int> vgroups; vector <int> rep; vector <int> sizes; vector <long long> fact; bool compc(int x, int y) { for (int i = 0; i < n; i++) { if (a[i][x] + a[i][y] > k) return false; } return true; } bool compr(int x, int y) { for (int i = 0; i < n; i++) { if (a[x][i] + a[y][i] > k) return false; } return true; } int find(int x) { while (x != rep[x]) x = rep[x]; return x; } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b) { a = find(a); b = find(b); if (sizes[a] < sizes[b]) swap(a, b); sizes[a] += sizes[b]; rep[b] = a; } int main() { cin >> n >> k; int temp; a.resize(n); long long res = 1; for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) { cin >> temp; a[i].push_back(temp); } } rep.resize(n); sizes.resize(n); for (int i = 0; i < n; i++) { rep[i] = i; sizes[i] = 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (compc(i, j) && !same(i, j)) unite(i, j); } } fact.resize(n + 1); fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i; fact[i] %= mod; } for (int i = 0; i < n; i++) { if (rep[i] == i) { res *= fact[sizes[i]]; res %= mod; } } rep.clear(); sizes.clear(); rep.resize(n); sizes.resize(n); for (int i = 0; i < n; i++) { rep[i] = i; sizes[i] = 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (compr(i, j) && !same(i, j)) unite(i, j); } } for (int i = 0; i < n; i++) { if (rep[i] == i) { res *= fact[sizes[i]]; res %= mod; } } cout << res << endl; return 0; }
// Author: old_school // Created: 02.01.2021 17:30:15 #include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") using namespace std; #define lld long long int #define ld long double #define pb push_back #define ppb pop_back #define mp make_pair #define F first #define S second #define nl '\n' #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define sz(c) (c).size() #define tr(x,a) for(auto &a : x) #define psnt(x,a) (x).find(a)!=(x).end() #define vpsnt(x,a) find(all(x),a)!=(x).end() #define MOD 1000000007 #define tod 1 #define pi 3.1415926536 #define itr(i,a,b) for(lld i=a;i<=b;i++) #define itrn(i,a,b) for(lld i=a;i>=b;i--) #define iot(n) for(lld i=0;i<n;i++) #define pls(n,arr) lld arr[n]; iot(n) cin>>arr[i]; #define bye fflush(stdout) typedef pair<lld,lld> pii; typedef pair<string,lld> psi; template <typename T> bool mycomp(T x,T y){ return (x==y); //give your condition here } bool paircomp(const pair<lld,lld> &x,const pair<lld,lld> &y){ return x.second<y.second; } void solve(){ lld n; cin>>n; lld a[n],b[n]; iot(n) cin>>a[i]>>b[i]; lld ans=0; iot(n){ itr(j,i+1,n-1){ ld x2=1.00*abs(a[j]-a[i]); ld y2=1.00*abs(b[j]-b[i]); ld slope=1.00*y2/x2; ld asf=1.00-slope; ld bsf=1.00+slope; if(asf>=0.00&&bsf>=0.00) ans++; // if(aa>=0&&bb>=0) ans++; } }cout<<ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); lld t=1;// cin>>t; while(t--){ solve(); cout<<endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() using namespace std; using ll = long long; const int INF = 1000000000; const int MOD = 1000000007; const ll llINF = 1000000000000000000; // #include <atcoder/all> // using namespace atcoder; // #include <boost/multiprecision/cpp_int.hpp> // using lll = boost::multiprecision::cpp_int; void solve() { int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } ll ans = 0; for (int i = 0; i < n; ++i) { ll mini = llINF; for (int j = i; j < n; ++j) { mini = min(mini, a[j]); ans = max(ans, (ll(j) - ll(i) + 1) * mini); } } cout << ans << endl; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); solve(); return 0; }
#include <stdio.h> #include <bits/stdc++.h> using namespace std; #define int long long int #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); const int N = 3e5 + 7; int32_t main(){ IOS; int t; t=1; while(t--){ int x,y; cin >> x >> y; int p = abs(x-y); if(p>=3) cout << "No" ; else cout << "Yes"; cout << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; int main(){ vector<int> a(4); for(int i=0;i<4;i++){ cin>>a[i]; } sort(a.begin(),a.end()); cout<<a[0]<<endl; return 0; }
#include <bits/stdc++.h> #include <array> #include <cstdlib> using namespace std; int main() { vector<int> a(3); for(int i =0; i < 3; i++){ cin >> a[i]; } sort(a.begin(), a.end()); if(a[2]-a[1] == a[1]-a[0]){ cout << "Yes" << endl; } else cout << "No" << endl; }
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int main() { int v,t,s,d; cin>>v>>t>>s>>d; if(v*t<=d&&s*v>=d) { cout<<"No"; } else cout<<"Yes"; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; const int BUF = 100005; class ChildData { public: int subtreeNode; int fstTake, scdTake; ChildData(){} ChildData(int sb, int f, int scd): subtreeNode(sb), fstTake(f), scdTake(scd) {} bool operator< (const ChildData &opp) const { return fstTake - scdTake < opp.fstTake - opp.scdTake; } }; int nNode; vector<int> adj[BUF]; void read() { for (int i = 0; i < BUF; ++i) adj[i].clear(); cin >> nNode; for (int i = 1; i < nNode; ++i) { int pre; cin >> pre; --pre; adj[pre].push_back(i); adj[i].push_back(pre); } } ChildData rec(int cur, int pre) { ChildData ret(0, 0, 0); vector<ChildData> childResult; vector<ChildData> evenResult; for (int i = 0; i < adj[cur].size(); ++i) { int nex = adj[cur][i]; if (nex == pre) continue; ChildData candi = rec(nex, cur); if (candi.subtreeNode % 2 == 0) { if (candi.fstTake - candi.scdTake < 0) { ret.subtreeNode += candi.subtreeNode; ret.fstTake += candi.fstTake; ret.scdTake += candi.scdTake; } else { evenResult.push_back(candi); } } else { childResult.push_back(candi); } } sort(childResult.begin(), childResult.end()); for (int i = 0; i < childResult.size(); ++i) { ChildData &r = childResult[i]; if (i % 2 == 0) { ret.subtreeNode += r.subtreeNode; ret.fstTake += r.fstTake; ret.scdTake += r.scdTake; } else { ret.subtreeNode += r.subtreeNode; ret.fstTake += r.scdTake; ret.scdTake += r.fstTake; } } for (int i = 0; i < evenResult.size(); ++i) { ChildData &r = evenResult[i]; if (ret.subtreeNode % 2 == 0) { ret.subtreeNode += r.subtreeNode; ret.fstTake += r.fstTake; ret.scdTake += r.scdTake; } else { ret.subtreeNode += r.subtreeNode; ret.fstTake += r.scdTake; ret.scdTake += r.fstTake; } } ++ret.subtreeNode; ++ret.fstTake; return ret; } void work() { cout << rec(0, -1).fstTake << endl; } int main() { read(); work(); return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int P[100001]; int sei[100001], fu[100001], kisuu[100001], kazu[100001], kazu2[100001]; void sort_ganbare(int N, int A[]) { if (N < (1 << 12)) { sort(A, A + N); return; } const int b = 9; int tmp[100001]; int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] & ((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] & ((1 << b) - 1)]] = A[i]; rep(i, N) kazu2[tmp[i] >> 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] >> b & ((1 << b) - 1)]] = tmp[i]; } const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); rep1(i, N - 1) { kazu[(P[i + 1] = getint())]++; } rep(i, N) kazu2[i + 1] = (kazu[i + 1] += kazu[i]); for (int i = N; i >= 2; i--) { int ret = sei[i] - 1; auto ki = kisuu + kazu[i - 1]; int sz = kazu2[i - 1] - kazu[i - 1]; sort_ganbare(sz, ki); if (sz & 1) { ret -= fu[i]; rep(j, sz) { if (j & 1) ret -= ki[j] - (1 << 17); else ret += ki[j] - (1 << 17); } } else { ret += fu[i]; rep(j, sz) { if (j & 1) ret += ki[j] - (1 << 17); else ret -= ki[j] - (1 << 17); } } if (ret & 1) { kisuu[kazu2[P[i] - 1]++] = ret + (1 << 17); } else { if (ret >= 0) sei[P[i]] += ret; else fu[P[i]] += ret; } } int ret = sei[1] - 1; auto ki = kisuu + kazu[0]; int sz = kazu2[0] - kazu[0]; sort_ganbare(sz, ki); if (sz & 1) { ret -= fu[1]; rep(j, sz) { if (j & 1) ret -= ki[j] - (1 << 17); else ret += ki[j] - (1 << 17); } } else { ret += fu[1]; rep(j, sz) { if (j & 1) ret += ki[j] - (1 << 17); else ret -= ki[j] - (1 << 17); } } co((N - ret) / 2); Would you please return 0; }
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <queue> #include <vector> using namespace std; typedef pair<int,int> pii; int h,w,dis[4000500],sx,sy,tx,ty,vis[4000500]; char sp[2005][2005]; priority_queue<pii > q; vector<int> gg[30]; int dx[4]={0,-1,0,1}; int dy[4]={1,0,-1,0}; int dp(int i,int j){return i*w+j;} int main() { scanf("%d%d",&h,&w); for(int i=0;i<h;i++) scanf("%s",sp[i]); for(int i=0;i<h;i++) for(int j=0;j<w;j++) { if(sp[i][j]=='S') sx=i,sy=j; if(sp[i][j]=='G') tx=i,ty=j; if(sp[i][j]>='a' && sp[i][j]<='z') gg[sp[i][j]-'a'].push_back(i*w+j); } memset(dis,-1,sizeof(dis)); dis[sx*w+sy]=0; q.push(make_pair(0,sx*w+sy)); while(!q.empty()) { int now=q.top().second;q.pop(); if(vis[now]) continue;vis[now]=1; if(now<h*w){ int x=now/w,y=now%w; for(int a=0;a<4;a++) { int nx=x+dx[a],ny=y+dy[a]; if(nx>=0 && nx<h && ny>=0 && ny<w && sp[nx][ny]!='#' && (dis[nx*w+ny]==-1 || dis[nx*w+ny]>dis[now]+1)) { dis[nx*w+ny]=dis[now]+1; q.push(make_pair(-dis[nx*w+ny],nx*w+ny)); } } if(sp[x][y]>='a' && sp[x][y]<='z') { int c=sp[x][y]-'a'; if(dis[c+h*w]==-1 || dis[c+h*w]>dis[now]) { dis[c+h*w]=dis[now]; q.push(make_pair(-dis[c+h*w],c+h*w)); } } } else { int c=now-h*w; for(int i=0;i<gg[c].size();i++) { if(dis[gg[c][i]]==-1 || dis[gg[c][i]]>dis[now]+1) { dis[gg[c][i]]=dis[now]+1; q.push(make_pair(-dis[gg[c][i]],gg[c][i])); } } } } // cout<<dis[dp(0,1)]<<" "<<dis[] printf("%d\n",dis[tx*w+ty]); return 0; }
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <iomanip> #include <sstream> #include <fstream> #include <stdint.h> #include <string.h> #define _USE_MATH_DEFINES #include <math.h> #include <vector> #include <list> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> #include <deque> #include <string> #include <algorithm> #include <functional> #include <bitset> #include <functional> #include <chrono> #include <random> #define sqr(x) (x) * (x) typedef unsigned int u32; typedef int i32; typedef unsigned long long int u64; typedef long long int i64; typedef uint16_t u16; typedef int i16; typedef uint8_t u8; typedef int8_t i8; using namespace std; using namespace std::chrono; const i64 mod = 1'000'000'000ll + 7; //const i64 mod = 998'244'353ll; const i64 inf = 1'000'000'000'000'000'000ll; const long double eps = 1e-8; i64 h, w; i64 ind(pair<i64, i64> p) { return p.first * w + p.second; } int main(int argc, char* argv[]) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed); if (sizeof(i64) != sizeof(long long int)) { cerr << "i64 != long long int" << endl; } cin >> h >> w; vector<string> a(h); for (i64 i = 0; i < h; i++) { cin >> a[i]; } vector<vector<pair<i64, i64>>> z(256); for (i64 i = 0; i < h; i++) { for (i64 j = 0; j < w; j++) { char c = a[i][j]; z[c].push_back({ i, j }); } } z['.'].clear(); auto s = z['S'].front(); auto f = z['G'].front(); vector<pair<i64, i64>> q; vector<i64> v(h * w); vector<i64> len; q.push_back(s); v[ind(s)] = 1; len.push_back(q.size()); for (i64 i = 0; i < q.size(); i++) { if (len.back() == i) { len.push_back(q.size()); } auto c = q[i]; if (c == f) { cout << len.size() - 1 << endl; return 0; } for (auto x : {c.first - 1, c.first + 1}) { auto y = c.second; if (x < 0 || x >= h) { continue; } if (y < 0 || y >= w) { continue; } if (a[x][y] == '#') { continue; } pair<i64, i64> nxt = { x, y }; if (v[ind(nxt)]) { continue; } v[ind(nxt)] = 1; q.push_back(nxt); } for (auto y : { c.second - 1, c.second + 1 }) { auto x = c.first; if (x < 0 || x >= h) { continue; } if (y < 0 || y >= w) { continue; } if (a[x][y] == '#') { continue; } pair<i64, i64> nxt = { x, y }; if (v[ind(nxt)]) { continue; } v[ind(nxt)] = 1; q.push_back(nxt); } char w = a[c.first][c.second]; for (auto nxt : z[w]) { if (v[ind(nxt)]) { continue; } v[ind(nxt)] = 1; q.push_back(nxt); } z[w].clear(); } cout << -1 << endl; return 0; }
#include <iostream> #include <iomanip> #include <vector> #include <set> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <numeric> #include <list> #include <stack> #include <cstdio> #include <cstdlib> #include <cstring> #include <tuple> #include <deque> #include <complex> #include <bitset> #include <functional> #include <cassert> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using ld = long double; using vld = vector<ld>; using vb = vector<bool>; #define rep(i, n) for (ll i = 0; i < (n); i++) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif template <class T> void chmin(T& a, T b) { a = min(a, b);} template <class T> void chmax(T& a, T b) { a = max(a, b);} template <class T> ostream& operator<<(ostream& s, const vector<T>& a) { for(auto i : a) s << i << ' '; return s; } constexpr ll INFL = 1LL << 60; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); struct mint { static const long long mod = 998244353; long long x; mint(long long x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x);} mint& operator+=(const mint& a) {if ((x += a.x) >= mod) x -= mod; return *this;} mint& operator-=(const mint& a) {if ((x += mod - a.x) >= mod) x -= mod; return *this;} mint& operator*=(const mint& a) {(x *= a.x) %= mod; return *this;} friend const mint operator+(const mint& a, const mint& b) { mint ret = a; return ret += b; } friend const mint operator-(const mint& a, const mint& b) { mint ret = a; return ret -= b; } friend const mint operator*(const mint& a, const mint& b) { mint ret = a; return ret *= b; } friend ostream& operator<<(ostream& s, const mint& a) { return s << a.x; } mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2);} mint& operator/=(const mint a) { return (*this) *= a.inv();} friend const mint operator/(const mint& a, const mint& b) { mint ret = a; return ret /= b; } }; void solve() { ll h, w, k; cin >> h >> w >> k; vector<string> s(h, string(w, '.')); rep(i, k) { ll h_, w_; char c_; cin >> h_ >> w_ >> c_; s[h_-1][w_-1] = c_; } /* rep(i, h) { rep(j, w) cout << s[i][j] << ' '; cout << endl; } */ using vm = vector<mint>; vector<vm> dp(h, vm(w)); dp[0][0] = mint(3).pow(h*w-k); mint inv3 = mint(3).inv(); rep(i, h) rep(j, w) { if(i > 0) { if(s[i-1][j] == '.') dp[i][j] += dp[i-1][j] * 2 * inv3; else if(s[i-1][j] == 'D' || s[i-1][j] == 'X') dp[i][j] += dp[i-1][j]; } if(j > 0) { if(s[i][j-1] == '.') dp[i][j] += dp[i][j-1] * 2 * inv3; else if(s[i][j-1] == 'R' || s[i][j-1] == 'X') dp[i][j] += dp[i][j-1]; } } cout << dp[h-1][w-1] << endl; /* rep(i, h) { rep(j, w) cout << dp[i][j] << ' '; cout << endl; } */ return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }
#include<bits/stdc++.h> using namespace::std; const int N = 5000 + 10; const int MOD = 998244353; const int F = (2LL * 332748118) % MOD; int n; int m; int k; int fact; char v[N][N]; int memo[N][N]; int add(int a, int b){ return (a + b) % MOD; } int mul(int a, int b){ return (1LL * a * b) % MOD; } int pow_mod(int a, int b){ int r = 1; while(b > 0){ if(b & 1) r = mul(r, a); a = mul(a, a); b >>= 1; } return r; } int solve(){ memo[1][1] = 1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(i + j == n + m) continue; if(!isalpha(v[i][j])) memo[i][j] = mul(memo[i][j], F); if(isalpha(v[i][j])){ if(v[i][j] == 'R'){ memo[i][j + 1] = add(memo[i][j + 1], memo[i][j]); } else if(v[i][j] == 'D'){ memo[i + 1][j] = add(memo[i + 1][j], memo[i][j]); } else{ memo[i + 1][j] = add(memo[i + 1][j], memo[i][j]); memo[i][j + 1] = add(memo[i][j + 1], memo[i][j]); } } else{ memo[i + 1][j] = add(memo[i + 1][j], memo[i][j]); memo[i][j + 1] = add(memo[i][j + 1], memo[i][j]); } } } return mul(memo[n][m], pow_mod(3, n * m - k)); } int main(){ scanf("%d %d %d", &n, &m, &k); memset(v, ' ', sizeof v); for(int i = 0; i < k; i++){ int x, y; scanf("%d %d", &x, &y); char c = getchar(); while(!isalpha(c)) c = getchar(); v[x][y] = c; } printf("%d\n", solve()); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) void solve(long long X, long long Y, long long A, long long B) { ll exp_a = 0; while (true) { if (X > Y / A) break; if (X * A >= X + B) break; if (X * A >= Y) break; X *= A; exp_a++; } ll exp_b = floor((long double) (Y - X - 1) / B); cout << exp_a + exp_b << endl; } int main() { long long X; scanf("%lld", &X); long long Y; scanf("%lld", &Y); long long A; scanf("%lld", &A); long long B; scanf("%lld", &B); solve(X, Y, A, B); return 0; }
#include<cstdio> #include<cstring> using namespace std; bool a[11000]; int main() { memset(a,false,sizeof(a)); for(int i=6;i<=10000;i+=6)a[i]=true; for(int i=10;i<=10000;i+=10)a[i]=true; for(int i=15;i<=10000;i+=15)a[i]=true; int n;scanf("%d",&n); printf("6 10 15");a[6]=a[10]=a[15]=false; n-=3; for(int i=1;i<=10000;i++) { if(n==0)return 0; if(a[i]){printf(" %d",i);n--;} } return 0; }
#include <bits/stdc++.h> using namespace std; int A[200005], B[200005]; long double E[200005], F[200005], G[200005], H[200005]; int main() { ios::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); int n, m, k; cin >> n >> m >> k; while (k--) { int x; cin >> x; A[x] = 1; } for (int i=1; i<=n+m+1; i++) B[i] = B[i-1] + A[i]; for (int i=n-1; i>=0; i--) { if (A[i] == 0) { E[i] = 1 + (F[i+1] - F[i+m+1]) / m; G[i] = (H[i+1] - H[i+m+1] + B[i+m] - B[i]) / m; } F[i] = F[i+1] + E[i]; H[i] = H[i+1] + G[i]; } if (abs(G[0] - 1) < 1e-9 || G[0] > 1) cout << -1 << "\n"; else cout << fixed << setprecision(9) << E[0] / (1 - G[0]) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> ii; typedef tuple<ll, ll, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define REP(i,n) for (ll i = 0; i < n; ++i) #define REPR(i,n) for (ll i = n-1; i >= 0; --i) #define FOR(i,m,n) for (ll i = m; i < n; ++i) #define FORR(i,m,n) for (ll i = n-1; i >= m; --i) #define FORE(x,xs) for (const auto& x : xs) #define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++) #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REV(v) reverse(ALL(v)) #define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v)) #define CHMIN(x,y) x = min(x, y) #define CHMAX(x,y) x = max(x, y) #define YES(b) cout << ((b) ? "YES" : "NO") << endl #define Yes(b) cout << ((b) ? "Yes" : "No") << endl const int MAX = 1e5+10; int N, M, K; set<int> A; ld dp[2*MAX]; bool check(ld p) { fill_n(dp, MAX, 0); ld s = 0; REPR (i, N) { if (A.find(i) == A.end()) dp[i] = 1 + s / M; else dp[i] = p; s += dp[i]; s -= dp[i+M]; } return dp[0] > p; } int main() { cout << fixed << setprecision(15); cin >> N >> M >> K; REP (i, K) { int a; cin >> a; A.insert(a); if (A.find(a-M+1) != A.end() && distance(A.find(a-M+1), A.find(a)) == M-1) { cout << -1 << endl; return 0; } } ld ub = 1; while (1) { ld ok = 1e-3, ng = ub; while (ng - ok > 1e-4) { ld mid = sqrtl(ok * ng); if (check(mid)) ok = mid; else ng = mid; } if (ub - ok > 1e3) { cout << ok << endl; return 0; } ub *= 4; } }
#include<bits/stdc++.h> #define ll long long #define mp make_pair #define pb push_back #define F first #define S second #define mod 1000000007 #define Mod 998244353 #define pi 3.14159265358979323846 #define endl '\n' #define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<char> vc; void solve() { int n; cin>>n; double x0,y0,xn,yn; cin>>x0>>y0>>xn>>yn; double x=(x0+xn)/2.0,y=(y0+yn)/2.0; double angle=2*pi/n; double x_ans=(x0-x)*cos(angle)-(y0-y)*sin(angle)+x; double y_ans=(x0-x)*sin(angle)+(y0-y)*cos(angle)+y; printf("%.9f %.9f\n",x_ans,y_ans); } int main() { IO solve(); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i)) #define ALL(x) ::std::begin(x), ::std::end(x) using namespace std; template <typename T> void print_vector(T& vec) { stringstream ss; ss << "{"; for (auto v : vec) ss << v << " "; ss << "}" << endl; cout << ss.str() << endl; } template <typename T> void print_pairs(T& vec) { stringstream ss; ss << "{"; for (auto v : vec) ss << v.first << "," << v.second << " "; ss << "}" << endl; cout << ss.str() << endl; } template <typename T> string string_vector(vector<T>& vec) { stringstream ss; ss << "{"; for (auto v : vec) ss << v << " "; ss << "}\n"; return ss.str(); } template <typename T> string string_vector(vector<vector<T>>& vec) { stringstream ss; ss << "{"; for (auto v : vec) { ss << "{"; for (auto vv : v) ss << vv << " "; ss << "}"; } ss << "}\n"; return ss.str(); } using ll = long long; class Solution { public: bool solve(ll V, ll T, ll S, ll D) { // D = V * t return !( (V*T <=D) and (D <= S*V)); } }; // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); Solution sol; ll V, T, S, D; cin >> V >> T >> S >> D; auto ans = sol.solve(V, T, S, D); cout << (ans ? "Yes" : "No") << endl; return 0; }
#include<bits/stdc++.h> #define loop(i,n) for(int i = 0; i < n; ++ i) #define rloop(i, n) for(int i = n-1; i >=0; i--) #define inp(arr, n) for(int i=0; i<n; i++) cin >> arr[i]; #define deb(x) cout << " " << #x << " " << x << endl // used to debug any variable #define mod 1000000007 #define pb push_back #define ff first #define ss second #define Vi vector<int> #define Vs vector<string> #define Vll vector<long long> #define Vl vector<long> #define Vc vector<char> #define all(v) v.begin(), v.end() const long double PI = 3.141592653589793236L; typedef long long int ll; typedef unsigned long long int usll; typedef long double ld; typedef double db; using namespace std; long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } void solve() { int n; cin>>n; Vi arr(n); ll res=0; inp(arr,n); loop(i,n) { if(arr[i]>10) res=res+(arr[i]-10); } cout<<res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //int T = 1;cin >> T;while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, nuts = 0; cin >> n; vector<int> a(n); for(int i = 0; i<n; i++) cin >> a[i]; for(int k:a) if(k>10) nuts+=(k-10); cout << nuts << endl; return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <map> #include <set> #include <cmath> #include <stack> #include <algorithm> #include <ctime> using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int,int> #define make make_pair #define fi first #define se second #define vi std::vector<int>; #define DEBUG cout << "debug" << endl #define CLOSE ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr) #define CASE int Kase=0;cin>>Kase;for(int kase=1;kase<=Kase;kase++) const int N = 1e5 + 5; const int M = 1e6 + 5; const int MOD = 1e9 + 7; const int CM = 998244353; const int INF = 0x3f3f3f3f; const double eps = 1e-6; template <typename T> T MAX(T a, T b) { return a > b ? a : b; } template <typename T> T MIN(T a, T b) { return a > b ? b : a; } template <typename T> T GCD(T a, T b){ return b == 0 ? a : GCD(b, a % b); } template <typename T> T LCM(T a, T b){ return a / GCD(a, b) * b; } template <typename T> T ABS(T a) { return a > 0 ? a : -a; } template <typename T> T ADD(T a, T b, T MOD){ return (a + b) % MOD; } template <typename T> T DEL(T a, T b, T MOD) { return ((a - b) % MOD + MOD) % MOD; } void solve(int kase){ ll s, p; cin >> s >> p; for(ll i = 1; i * i <= p; i++) { if(p % i == 0) { ll x = i, y = p / i; if(x + y == s) { printf("Yes\n"); return; } } } printf("No\n"); } const bool DUO = 0; void TIME(){ clock_t start, finish; double totaltime; start = clock(); // 待测程序 if(DUO) {CASE solve(kase);} else solve(1); // 待测程序 finish = clock(); totaltime = (double)(finish - start) / CLOCKS_PER_SEC; printf("\nTime:%lfms\n", totaltime * 1000); } int main(){ #ifdef ONLINE_JUDGE // CLOSE; if(DUO) {CASE solve(kase);} else solve(1); #else // freopen("./data/a.txt", "r", stdin); // freopen("./data/a.txt", "w", stdout); TIME(); #endif return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; const int INF = 1 << 30; int main() { ll N; cin >> N; // vector<ll> A(N), B(N); vector<ll> C(N); ll aoki = 0; rep(i, N) { ll a, b; cin >> a >> b; aoki += a; ll total = 2 * a + b; C[i] = total; } sort(C.rbegin(), C.rend()); ll counter = 0; ll total = 0; for (ll i = 0; i < N; i++) { counter++; total += C[i]; // cout << "total: " << total << endl; if (total > aoki) break; } ll ans = counter; cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (lli i = 0; i < (n); i++) #define rrep(i, n) for (lli i = (n)-1; i >= 0; i--) #define vall(x) (x).begin(), (x).end() using namespace std; namespace atcoder {} using namespace atcoder; using lli = long long int; void YESNO(bool), YesNo(bool); template <class T1, class T2> bool chmin(T1 &l, const T2 &r); template <class T1, class T2> bool chmax(T1 &l, const T2 &r); #define int long long int lli dp[105][105][105] = {}; void solve(long long N, long long X, std::vector<long long> A) { lli ans = 1e18; for (int t = 1; t <= N; t++) { rep(i, N + 1) rep(j, N + 1) rep(k, N + 1) dp[i][j][k] = -1; vector<int> B; dp[0][0][0] = 0; rep(i, N) rep(j, N + 1) rep(k, N + 1) { chmax(dp[i + 1][j][k], dp[i][j][k]); if (dp[i][j][k] >= 0) chmax(dp[i + 1][j + 1][(k + A[i]) % t], dp[i][j][k] + A[i]); } if (dp[N][t][X % t] >= 0) chmin(ans, (X - dp[N][t][X % t]) / t); } cout << ans << endl; } signed main() { long long N; scanf("%lld", &N); long long X; scanf("%lld", &X); std::vector<long long> A(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } solve(N, X, std::move(A)); return 0; } // -- lib void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } template <class T1, class T2> bool chmin(T1 &l, const T2 &r) { return (l > r) ? (l = r, true) : false; } template <class T1, class T2> bool chmax(T1 &l, const T2 &r) { return (l < r) ? (l = r, true) : false; } template <class T1, class T2> void vadd(vector<T1> &v, T2 x) { for (auto &s : v) s += T2(x); }
////////////////////////// Author ////////////////////////// Nasim Hossain Rabbi ////////////////////////// JU-CSE28 ////////////////////////// CF - imnasim3.1415 ////////////////////////// UVA - imnasim3.1415 ////////////////////////// Mail - [email protected] #include<bits/stdc++.h> using namespace std; #define E end() #define B begin() #define sz size() #define em empty() #define fi first #define se second #define cnt continue #define cl clear() #define pb push_back string en="\n"; string sp=" "; string t="hello"; string Y="YES\n"; string N="NO\n"; #define rep(i,n) for(i=0;i<n;i++) #define Rep(i,n) for(i=1;i<=n;i++) #define per(i,n) for(i=n-1;i>=0;i--) #define peR(i,n) for(i=n;i>0;i--) #define valid(x,y) x>=1 && x<=Row && y>=1 && y<=Col #define mem(a,b) memset(a,b,sizeof(a)) #define all(cont) cont.begin(),cont.end() #define fast ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL) #define pi 3.1415926535897932384626433832795 #define vi vector<long long> #define vs vector<string> #define MX LLONG_MAX #define MN LLONG_MIN #define MOD 1000000007 #define vp(vi,x) cin>>x; vi.pb(x); #define bsearch(a,x) binary_search(all(a),x) #define LB(a,x) (lower_bound(all(a),x)-a.B) #define UB(a,x) (upper_bound(all(a),x)-a.B) int fx[]={1,-1,0,0},Row,Col; int fy[]={0,0,1,-1}; int dx[]={0,0,1,-1,-1,1,-1,1}; int dy[]={-1,1,0,0,1,1,-1,-1}; using LL=long long; using LD=long double; using ULL=unsigned long long; bool isprm(LL a){for(LL i=2;i*i<=a;i++){if(a%i==0)return false;}return true;} bool palin(string a){for(int i=0;i<a.sz;i++){if(a.at(i)!=a.at(a.sz-i-1))return 0;} return 1;} int toInt(string s){int sm;stringstream ss(s);ss>>sm;return sm;} LL ceil2(LL a,LL b){LL c=a/b; if(a%b)c++; return c; } LL binpow(LL a,LL b){LL r=1;while(b>0){if(b&1)r=r*a;a=a*a;b>>=1;}return r;} LL binpow(LL a,LL b,LL m){a%=m;LL r=1;while(b>0){if(b&1) r=r*a%m;a=a*a%m; b>>=1;}return r;} template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} int main() { // fast; LL i,j,k,n,m,l,s=0,x,y,tc=1,p,q,a,b,c=0; bool f=0,ff=0;string st,s1,s2,s3; char ch; while(cin>>n) { x=sqrt(2*(n+1)); s=(x*(x+1))/2; if(s>n+1)x--; x=n-x+1; cout<<x<<en; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> #define setIO(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout) #define closefile fclose(stdin), fclose(stdout) #define m_p make_pair #define sz(x) (int)x.size() #define see(x) cerr << x << " " #define seeln(x) cerr << x << endl #define out(x) cerr << #x << " = " << x << " " #define outln(x) cerr << #x << " = " << x << endl #define outarr(x, l, r) {cerr<< #x"[" << l << " ~ " << r << "] = "; for (int _i = l; _i <= r; ++_i) cerr << x[_i] << " "; cerr << endl;} using namespace std; typedef long long ll; typedef pair <int, int> pii; #define gc() getchar() //#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) char buf[1 << 21], *p1 = buf, *p2 = buf; template <class T> void read(T &x) { x = 0; int c = gc(); int flag = 0; while (c < '0' || c > '9') flag |= (c == '-'), c = gc(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) +(c ^ 48), c = gc(); if (flag) x = -x; } template <class T> T _max(T a, T b){return b < a ? a : b;} template <class T> T _min(T a, T b){return a < b ? a : b;} template <class T> bool checkmax(T &a, T b){return a < b ? a = b, 1 : 0;} template <class T> bool checkmin(T &a, T b){return b < a ? a = b, 1 : 0;} const int mod = 998244353, N = (1 << 20) + 1; int fac[200005], ifac[200005], inv[200005]; #define Mul(a,b) (ll)(a) * (b) % mod #define mul(a,b) (a) = (ll)(a) * (b) % mod int Add(int x, int y) {x += y; return x >= mod ? x - mod : x;} int Sub(int x, int y) {x -= y; return x < 0 ? x + mod : x;} void add(int &a, int b) {a += b; if (a >= mod) a -= mod;} void sub(int &a, int b) {a -= b; if (a < 0) a += mod;} int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) mul(ans, a); mul(a, a); b >>= 1; } return ans; } int binom(int x, int y){return x < y ? 0 : Mul(fac[x], Mul(ifac[y], ifac[x - y]));} int n, m, k; void init() { read(n); read(m); read(k); } int calc(int lim, int l) { return Sub(qpow(lim, l), qpow(lim - 1, l)); } int calc2(int lim, int l) { return qpow(lim, l); } void solve() { if (n == 1 || m == 1) { printf("%d\n", qpow(k, m * n)); return; } int ans = 0; for (int i = 1; i <= k; ++i) { add(ans, Mul(calc(i, n), calc2(k - i + 1, m))); } printf("%d\n", ans); } int main() { init(); solve(); return 0; }
// #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define all(x) x.begin(), x.end() #define lch (o << 1) #define rch (o << 1 | 1) typedef double db; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pint; typedef tuple<int, int, int> tint; const int N = 2e5 + 5; const int MOD = 998244353; const int INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; ll qpow(ll bas, ll t) { ll ret = 1; while (t) { if (t & 1) ret = ret * bas % MOD; bas = bas * bas % MOD; t >>= 1; } return ret; } int main() { ios::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; if (min(n, m) == 1) { ll ans = qpow(k, max(n, m)); cout << ans << endl; } else { ll ans = 0; for (int x = 1; x <= k; x++) ans += (qpow(x, n) - qpow(x - 1, n)) * qpow(k - x + 1, m) % MOD; ans = (ans % MOD + MOD) % MOD; cout << ans << endl; } return 0; }
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int maxn=1500100; const int inf=0x3f3f3f3f; int tree[maxn<<2];//维护个数,初始全部为0; int vis[maxn]; int num[maxn]; int n,m; void add(int tis,int l,int r,int tp,int k) { if(l==r){ vis[l]+=k; if(vis[l])tree[tis]=inf; else tree[tis]=l; } else{ int mid=(r+l)>>1; if(tp<=mid)add(tis<<1,l,mid,tp,k); if(tp>=mid+1)add(tis<<1|1,mid+1,r,tp,k); tree[tis]=min(tree[tis<<1],tree[tis<<1|1]); } } void build(int tis,int l,int r){ if(l==r){ tree[tis]=l; } else { int mid=(l+r)>>1; build(tis<<1,l,mid); build(tis<<1|1,mid+1,r); tree[tis]=min(tree[tis<<1],tree[tis<<1|1]); } } int main() { scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); } build(1,1,n+1); for(int i=1;i<=m;i++){ add(1,1,n+1,num[i]+1,1); } int small=tree[1]-1; for(int i=2;i<=n-m+1;i++){ add(1,1,n+1,num[i+m-1]+1,1); add(1,1,n+1,num[i-1]+1,-1); if(small>tree[1]-1){ small=tree[1]-1; } } printf("%d\n",small); }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define ins insert template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; //check the limits, dummy void solve() { int N, M; cin >> N >> M; int cnt[N]; F0R(i, N) cnt[i] = 0; set<int> av; F0R(i, N) av.ins(i); av.ins(N); int A[N]; F0R(i, N) cin >> A[i]; F0R(i, M) { cnt[A[i]]++; av.erase(A[i]); } int ans = *av.begin(); FOR(i, M, N) { cnt[A[i-M]]--; if (cnt[A[i-M]] == 0) av.ins(A[i-M]); cnt[A[i]]++; av.erase(A[i]); ckmin(ans, *av.begin()); } cout << ans << nl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while(T--) { solve(); } return 0; } // read the question correctly (ll vs int) // template by bqi343
#include "bits/stdc++.h" using namespace std; #define ll long long const int areTests = 0, N = 2e5 + 20; int n, p[N], q, u, d, dp[N], st[N], ed[N], tym, a[N]; vector <int> adj[N], idx[N]; void dfs(int node, int edges) { st[node] = ++tym; dp[node] = edges; idx[edges].push_back(tym); for(int child: adj[node]) { dfs(child, edges + 1); } ed[node] = tym; } void run_test(int testcase) { cin >> n; for(int i = 2; i <= n; i++) { cin >> p[i]; adj[p[i]].push_back(i); } dfs(1, 0); for(int i = 0; i <= n; i++) { sort(idx[i].begin(), idx[i].end()); } cin >> q; while(q--) { cin >> u >> d; if(dp[u] > d) { cout << "0"; } else { cout << upper_bound(idx[d].begin(), idx[d].end(), ed[u]) - lower_bound(idx[d].begin(), idx[d].end(), st[u]); } cout << "\n"; } } int main() { ios::sync_with_stdio(0); #ifndef DUSH1729 cin.tie(0); #endif cout << fixed << setprecision(10); int t = 1; if(areTests) cin >> t; for(int i = 1; i <= t; i++) { run_test(i); } }
#include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define PI acos(-1) #define pb push_back #define int long long #define ld long double #define sp fixed<<setprecision #define bp __builtin_popcountll #define all(x) x.begin(),x.end() #define pii pair<long long,long long> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int M = (1e9)+7; const int N = 200005; vector<int> adj[N]; int vis[N],a[N],b[N]; int curr = 0, need = 0; void dfs(int c) { curr += a[c]; need += b[c]; vis[c]++; for(int ne:adj[c])if(!vis[ne])dfs(ne); } signed main() { FAST int tc=1; //cin>>tc; for(int ti=1;ti<=tc;ti++) { int n,m; cin>>n>>m; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)cin>>b[i]; for(int i=0;i<m;i++) { int x,y; cin>>x>>y; adj[x].pb(y); adj[y].pb(x); } int ok = 1; for(int i=1;i<=n;i++) { if(vis[i])continue; need = 0; curr = 0; dfs(i); ok &= (need == curr); } cout<<(ok ? "Yes" : "No")<<endl; } return 0; }
#include <bits/stdc++.h> //#include <boost/multiprecision/cpp_int.hpp> #define ll long long #define rep(i, n) for (int i = 0; i < (long long int)(n); i++) //namespace mp = boost::multiprecision; //mp::cpp_int /* nyuuryoku retu de ukeoru string s; getline(cin,s); int &n ha sansyou sort reverse swap unique */ using namespace std; signed main(){ int N,maxA=0,minB=99999; cin >> N; vector<int> A(N); vector<int> B(N); rep(i,N){ cin >> A.at(i); if(maxA < A.at(i)){ maxA = A.at(i); } } rep(i,N){ cin >> B.at(i); if(minB > B.at(i)){ minB = B.at(i); } } cout << ((minB - maxA +1 <= 0)? 0 : minB - maxA + 1 ) <<endl; }
/*{{{*/ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<iostream> #include<sstream> #include<set> #include<map> #include<queue> #include<bitset> #include<vector> #include<limits.h> #include<assert.h> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++) #define MP make_pair #define PB emplace_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL,LL> PLL; typedef vector<PLL> VPLL; template<class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);} template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } #ifdef HOME #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);} #else #define DEBUG(...) #endif int MOD = 1e9+7; void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;} /*}}}*/ const int SIZE = 1<<20; int a[SIZE],b[SIZE]; int N; void solve() { W(max(0,*min_element(b,b+N)-*max_element(a,a+N)+1)); } void input() { R(N); REP(i,N)R(a[i]); REP(i,N)R(b[i]); } int main(){ input(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; int res = 0; for (int i=1; i<=k; i++) for (int j=1; j<=k/i; j++) for (int l=1; l<=k/i/j; l++) res++; cout << res << '\n'; return 0; }
#include<bits/stdc++.h> #define rep(i,k,n) for(int i = k; i < n; i++) using ll = long long; using namespace std; // arc113a // 列挙 // 重複あり並べ方パターン int main(){ ll K_0; cin >> K_0; int res = 0; rep(i,1,K_0 + 1){ ll K = i; for(ll A = 1; A*A*A <= K; A++){ if(K%A == 0){ ll K_1 = K/A; for(ll B = A; B*B <= K_1; B++){ if(K_1%B == 0){ ll C = K_1/B; if(A < B && B < C) res += 6; else if((A == B && B < C) || (A < B && B == C)) res += 3; else res += 1; // cout << "(A, B, C):\n\t"; // cout << "(" << A << ", " << B << ", " << C << ")" << endl; // cout << "res:\n\t"; // cout << res << endl; } } } } } cout << res << endl; return 0; }
#include <iostream> using namespace std; int main() { int N; cin >> N; int num = 1; int i = 1; int count = 1; while(true){ if(num >= N){ cout << count << endl; break; } count++; num += count; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define loop(i, a, n) for (int i = a; i < n; i++) #define loope(i, b, n) for (int i = b; i <= n; i++) #define loopit(a) for (auto it = a.begin(); it != a.end(); it++) #define ms(a, b) memset(a, b, sizeof(a)) #define pb(a) push_back(a) #define MP make_pair #define pi pair<int, int> #define ff first #define ss second #define bloop(i, a, b) for (int i = a; i > b; i--) #define bloope(i, a, b) for (int i = a; i >= b; i--) #define PQ priority_queue<int> pq; #define vi vector<int> #define vii vector<vector<int>> #define si set<int> #define NO cout << "NO\n"; #define YES cout << "YES\n"; #define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq; #define io \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ // #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; ll ceil(ll a, ll b) { return (a + b - 1) / b; } void solve() { int n;cin>>n; int beg = 1,end = 1e5; while(beg<end){ int mid = (beg+end)/2; ll ans = 1ll*mid*(mid+1)/2; if(ans>=n)end=mid; else beg = mid+1; } cout<<beg<<"\n"; } int main() { io; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int test = 1, i = 0; // cin >> test; while (i++ != test) { // cout<<"Case #"<<i<<": "; solve(); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstring> #include <iostream> #include <sstream> #include <numeric> #include <map> #include <set> #include <queue> #include <vector> using namespace std; vector<vector<int>> solve(int N, const vector<vector<int>>& A) { int c = *min_element(A[0].begin(), A[0].end()); vector<int> a(N), b(N); for (int j = 0; j < N; ++j) { b[j] = A[0][j] - c; } for (int i = 1; i < N; i++) { c = *min_element(A[i].begin(), A[i].end()); for (int j = 0; j < N; ++j) { if (A[i][j] - c != b[j]) { return {}; } } } c = 1 << 30; for (int i = 0; i < N; i++) { c = min(c, A[i][0]); } for (int i = 0; i < N; i++) { a[i] = A[i][0] - c; } for (int j = 1; j < N; ++j) { c = 1 << 30; for (int i = 0; i < N; i++) { c = min(c, A[i][j]); } for (int i = 0; i < N; i++) { if (A[i][j] - c != a[i]) { return {}; } } } int d = A[0][0] - a[0] - b[0]; for (int j = 0; j < N; ++j) { b[j] += d; } return { a, b }; } int main() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; ++j) { cin >> A[i][j]; } } vector<vector<int>> ans = solve(N, A); if (ans.empty()) { cout << "No" << endl; } else { cout << "Yes" << endl; for (auto a : ans) { string delim = ""; for (auto x : a) { cout << delim << x; delim = " "; } cout << endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; //#define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' #define vec vector<ll> #define mat vector<vector<ll> > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; //typedef long double ld; typedef complex<double> Complex; const ll INF=1e9+7; const ll MOD=998244353; const ll inf=INF*INF; const ll mod=MOD; const ll MAX=20000010; signed main(){ ll n;cin>>n; vector<vector<ll> >c(n,vector<ll>(n)); rep(i,n){ rep(j,n)cin>>c[i][j]; } bool f=1; vector<vector<ll> >d(n,vector<ll>(n)); ll mi=0; REP(i,1,n){ bool g=1; rep(j,n){ d[i][j]=c[i][j]-c[0][j]; if(j>0&&d[i][j]!=d[i][j-1])g=0; mi=min(mi,d[i][j]); } f&=g; } if(!f){ cout<<"No"<<endl; return 0; } mi=-mi; vector<ll>a(n); vector<ll>b(n); a[0]=mi; REP(i,1,n){ a[i]=mi+d[i][0]; } rep(i,n){ b[i]=c[0][i]-mi; if(b[i]<0)f=0; } if(f){ cout<<"Yes"<<endl; rep(i,n){ cout<<a[i]<<' '; } cout<<endl; rep(i,n){ cout<<b[i]<<' '; } cout<<endl; }else{ cout<<"No"<<endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (int)(n); i++) int main(){ int N,X; cin>>N>>X; vector<int> A(0); int count=0; rep(i,N){ int tmp; cin>>tmp; if(tmp!=X){ A.push_back(tmp); count++; } } rep(i,A.size()){ cout<<A.at(i)<<" "; } cout<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; #define GETA 200 int main() { int N; cin >> N; vector<ll> A(N); rep(n, N) { cin >> A[n]; } ll ans = 1<<30; rep(pt, 1 << (N-1)) { ll XOr = 0; ll now = 0; rep(n, N) { //全てA[n]基準 now |= A[n]; if ((pt >> n) & 1) { //その数字の右側で区切り XOr ^= now; now = 0; } } XOr ^= now; ans = min(ans, XOr); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(ll i=0; i<(n); ++i) #define rep1(i,n) for(ll i=1; i<=(n); ++i) #define revrep(i,n) for(ll i=(n)-1; i>=0; --i) inline constexpr ll Inf = (1ULL << 62) -1; #define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false); #define newl '\n' #define YN(e) ((e)?"Yes":"No") #define all(a) begin(a),end(a) #define rall(a) rbegin(a),rend(a) template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;} template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;} inline constexpr int Mod = 1000000007; //inline constexpr int Mod = 998244353; #define dbg(a) cout << #a << ": " << a << endl; #define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl; #define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; } int a[1005], b[1005]; int dp[1005][1005]; int main() { fastio; ll N,M; cin >> N >> M; rep1(i,N) cin >> a[i]; rep1(i,M) cin >> b[i]; rep(i,N+3) dp[i][0] = i; rep(i,M+3) dp[0][i] = i; rep1(i,N) rep1(j,M) { int t = dp[i-1][j-1] + (a[i] != b[j]); updmin(t, dp[i-1][j] + 1); updmin(t, dp[i][j-1] + 1); dp[i][j] = t; } cout << dp[N][M] << newl; }
#include <iostream> using namespace std; int a[1010],b[1010],dp[1010][1010]; int main(){ int i,j,n,m; cin >> n >> m; for(i=0;i<n;i++) cin >> a[i]; for(j=0;j<m;j++) cin >> b[j]; for(i=0;i<=n;i++){ for(j=0;j<=m;j++) dp[i][j] = 100000000; } for(i=0;i<=n;i++) dp[i][0] = i; for(j=0;j<=m;j++) dp[0][j] = j; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ if(a[i - 1]==b[j - 1]){ dp[i][j] = min(dp[i][j],dp[i - 1][j - 1]); }else{ dp[i][j] = min(dp[i][j],dp[i - 1][j - 1] + 1); } dp[i][j] = min(dp[i][j],dp[i - 1][j] + 1); dp[i][j] = min(dp[i][j],dp[i][j - 1] + 1); } } cout << dp[n][m] << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long using vec_int = vector<int>; using P = pair<int,int>; using T = tuple<int,int,int>; using ll = long long; #define rep(i, n) for(int i = 0; i < (int)(n); i++) int charToInt(char c){ char zero_num = '0'; return (int)c - (int)zero_num; } signed main(){ int N; cin>>N; vec_int X(N); rep(i,N)cin>>X.at(i); vec_int primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; int ans = 1; rep(i,primes.size())ans*=primes.at(i); for(int i=0;i<(1<<primes.size());i++){ int num = 1; int temp = 1; for(int j=0;j<primes.size();j++){ if((i/temp)%2==1){ num*=primes.at(j); } temp*=2; } int flag = 1; rep(j,N){ if(__gcd(X.at(j), num)==1){ flag = 0; break; } } if(flag == 1){ ans = min(ans, num); } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using vi = vector<int>; using si = set<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define P pair<int,int> #define PLL pair<ll,ll> #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define rev(i,s,n) for(int i=(s);i>=(int)(n);i--) #define reciv(v,n) vll (v)((n)); rep(i,0,(n))cin>>v[i] #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) ll x;cin >> x #define cci(x,y) ll x,y;cin >> x >> y #define co(x) cout << x << endl #define pb push_back #define eb emplace_back #define rz resize #define pu push #define sz(x) int(x.size()) #define vij v[i][j] // ll p = 1e9+7; // ll p = 998244353; // n do -> n*pi/180 #define yn cout<<"Yes"<<endl;else cout<<"No"<<endl #define YN cout<<"YES"<<endl;else cout<<"NO"<<endl template<class T>void chmax(T &x,T y){x=max(x,y);} template<class T>void chmin(T &x,T y){x=min(x,y);} vll prm; int main(){ int ma=52; vi pp(ma,1); rep(i,2,ma){ if(pp[i]){ prm.pb(i); rep(j,i+i,ma){ pp[j]=0; j+=i-1; } } } cii(n); reciv(v,n); ll ans=3e18; int m=sz(prm); int m2=1<<m; rep(bn,0,m2){ ll now=1; rep(i,0,m)if(bn>>i&1)now*=prm[i]; int f=1; rep(i,0,n)if(__gcd(now,v[i])==1)f=0; if(f)chmin(ans,now); } co(ans); }
typedef long long ll; #include <cstdio> #include <algorithm> #include <iostream> #include <string.h> #include <bits/stdc++.h> #include <cstdlib> using namespace std; int main() { int a,b,x,y; cin >> a >> b >> x >> y; int ans = 0; if (b > a) { if (2*x < y) { ans = (b-a) * (2*x) + x; } else { ans = (b-a) * y + x; } } else { if (2*x < y) { ans = (a-b) * 2*x - x; } else { ans = (a-b-1) * y + x; } } if (b == a) { ans = x; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define pii pair<int, int> const int M = 100; // int d[2 * M + 5]; vector<pii> adj[2 * M + 5]; void solve() { int a, b, x, y; cin >> a >> b >> x >> y; for (int i = 1; i <= 198; i++) { adj[i].emplace_back(i + 2, y); adj[i + 2].emplace_back(i, y); } for (int i = 1; i <= 199; i++) { adj[i].emplace_back(i + 1, x); adj[i + 1].emplace_back(i, x); } int start = 2 * a - 1; int end = 2 * b; priority_queue<pii, vector<pii>, greater<pii>> pq; vector<int> d(2 * M + 5, 2e9); d[start] = 0; pq.emplace(0, start); while(!pq.empty()) { int v = pq.top().second; int dv = pq.top().first; pq.pop(); for (auto i : adj[v]) { int to = i.first; int dist = i.second; if (d[to] > d[v] + dist) { d[to] = d[v] + dist; pq.emplace(d[to], to); } } } cout << d[end] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1; for(int i = 1; i <= TC; i++) { solve(); } return 0; }
#include <bits/stdc++.h> #define FOR(i,x,n) for(int i=x; i<n; i++) #define F0R(i,n) FOR(i,0,n) #define ROF(i,x,n) for(int i=n-1; i>=x; i--) #define R0F(i,n) ROF(i,0,n) #define WTF cerr << "WTF" << endl #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define F first #define S second #define pb push_back #define mp make_pair #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define FILL(a, b) memset(a, b, sizeof(a)); #define TOGGLE(x) (x) = 1 - (x) using namespace std; typedef double db; typedef long long LL; typedef string str; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<string> VSTR; typedef set<int> SI; typedef set<PII> SPII; typedef set<PLL> SPLL; typedef priority_queue<PII> DIPQ; const int MAXN = 2e5 + 7; const int MAXA = 1e7 + 7; const int INF = 1e9; const int MOD = 1e9 + 7; const int LOG = 20; const LL LONG_INF = 1e18; int n; str inp; void Solve () { cin >> n >> inp; if (inp[0] != inp[n - 1]) { cout << 1 << endl; return; } else { FOR (i, 1, n - 2) { if (inp[i] != inp[0] && inp[i + 1] != inp[0]) { cout << 2 << endl; return; } } } cout << -1 << endl; } int main() { IOS; Solve (); }
#include <bits/stdc++.h> using namespace std; #define ll long long int main(){ ios_base::sync_with_stdio(false);cin.tie(0); int n; cin>>n; string s; cin>>s; vector<int> min_rem(n+1, n); min_rem[n]=0; vector<int> ending_with_char(26, -1); for(int i=n-1;i>=0;i--){ if(i==n-1){ min_rem[i]=-1; ending_with_char[(int)(s.at(i)-'a')]=1; } else{ min_rem[i]=n; for(int ch=0;ch<26;ch++){ if(ch!=(int)(s.at(i)-'a')){ if(ending_with_char[ch]!=-1){ min_rem[i]=min(min_rem[i], ending_with_char[ch]); } } } min_rem[i]=(min_rem[i]==n?-1:min_rem[i]); if(min_rem[i+1]!=-1){ ending_with_char[(int)(s.at(i)-'a')]=min(ending_with_char[(int)(s.at(i)-'a')]==-1?n:ending_with_char[(int)(s.at(i)-'a')], min_rem[i+1]+1); } } } cout<<min_rem[0]<<"\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define vt vector #define pb push_back #define mp make_pair #define rep(i, n) for (int i = 0; i < n; i++) #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) const int64_t MOD = 1000000007; // 🚩 // 20201115 21:24~ // int main() { ll H, W; cin >> H >> W; vt<string> S(H); rep(y, H) cin >> S.at(y); vt<vt<ll>> dp(H, vt<ll>(W, 0)); dp.at(0).at(0) = 1; vt<vt<ll>> X(H, vt<ll>(W)), Y(H, vt<ll>(W)), Z(H, vt<ll>(W)); for (ll cy = 0; cy < H; cy++) { for (ll cx = 0; cx < W; cx++) { if (cx == 0 && cy == 0) continue; if (S.at(cy).at(cx) == '#') continue; // o(4*10^6) if (cx > 0) X.at(cy).at(cx) = (X.at(cy).at(cx - 1) + dp.at(cy).at(cx - 1)) % MOD; if (cy > 0) Y.at(cy).at(cx) = (Y.at(cy - 1).at(cx) + dp.at(cy - 1).at(cx)) % MOD; if (cx > 0 && cy > 0) Z.at(cy).at(cx) = (Z.at(cy - 1).at(cx - 1) + dp.at(cy - 1).at(cx - 1)) % MOD; dp.at(cy).at(cx) = (X.at(cy).at(cx) + Y.at(cy).at(cx) + Z.at(cy).at(cx)) % MOD; // //右 // for (ll i = 1; i <= W - 1 - cx; i++) // { // if (S.at(cy).at(cx + i) == '#') break; // dp.at(cy).at(cx + i) = (dp.at(cy).at(cx + i) + dp.at(cy).at(cx)) % MOD; // } // //右下 // for (ll i = 1; i <= min(H - 1 - cy, W - 1 - cx); i++) // { // if (S.at(cy + i).at(cx + i) == '#') break; // dp.at(cy + i).at(cx + i) = (dp.at(cy + i).at(cx + i) + dp.at(cy).at(cx)) % MOD; // } // //下 // for (ll i = 1; i <= H - 1 - cy; i++) // { // if (S.at(cy + i).at(cx) == '#') break; // dp.at(cy + i).at(cx) = (dp.at(cy + i).at(cx) + dp.at(cy).at(cx)) % MOD; // } } } cout << dp.at(H - 1).at(W - 1) << endl; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; int f(int N, int K) { if (K < 2 || K > 2 * N) return 0; return min(K - 1, 2 * N + 1 - K); } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int N; cin >> N; int K; cin >> K; ll output = 0LL; for (int ab = 2; ab <= 2 * N; ab++) { output = output + (ll)f(N, ab) * f(N, ab - K); } cout << output << endl; return 0; }
/** * author: tomo0608 * created: 27.03.2021 21:12:02 **/ #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include<atcoder/all> using namespace atcoder; #endif typedef long long ll; typedef long double ld; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; typedef pair<int,int> pii; typedef pair<long long, long long> pll; #define all(x) x.begin(),x.end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) #define unique(a) a.erase(unique(a.begin(),a.end()),a.end()) template<class... T>void input(T&... a){(cin >> ... >> a);}; #define INT(...) int __VA_ARGS__; input(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; input(__VA_ARGS__) #define STRING(...) string __VA_ARGS__; input(__VA_ARGS__) void print(){cout << '\n';} template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a; (cout << ... << (cout << ' ', b)); cout << '\n';} template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >; template<class T, class U> inline bool chmax(T &a, const U &b) { if (a<b) { a=b; return 1; } return 0; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a>b) { a=b; return 1; } return 0; } template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {os << *itr;++itr;if(itr != set_var.end()) os << ", ";itr--;}os << "}";return os;} template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) {os << "{";for(auto itr = map_var.begin(); itr != map_var.end(); itr++) {os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--;}os << "}";return os;} template<class T> inline int count_between(vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r) #define pb push_back #define eb emplace_back #define elif else if #define mp make_pair #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ template<typename T> T gcd(T x, T y){if(x%y == 0)return y;return gcd(y, x%y);} template<typename T> T gcd(vector<T> a){T res = a[0];for(auto &x: a)res = gcd(res, x);return res;} template <typename T>T mypow(T x, ll n){T ret = 1;while(n > 0){if(n & 1)(ret *= x);(x *= x);n >>= 1;}return ret;} #define endl '\n' int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1}; void solve(){ INT(n); V<ll> a(n);cin >> a; ll ans = 0; rep(i,n)ans |= a[i]; //print(ans); rep(i,1<<(n-1)){ if(i == 0)continue; ll tmp = 0, now = a[0]; rep(j,n-1){ if(bit(i,j)){ tmp ^= now; now = a[j+1]; }else{ now |= a[j+1]; } } tmp ^= now; //print(tmp); chmin(ans,tmp); } print(ans); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << setprecision(20); int codeforces = 1; //cin >> codeforces; while(codeforces--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; int ansmin = (1 << 30); for (int tmp = 0; tmp < (1 << n-1); ++tmp) { bitset<19> s(tmp); //cout << tmp << endl; queue<int> q; int x = a[0]; //cout << x; for (int i = 0; i < n-1; ++i) { if (s[i]) { //cout << '^' << a[i+1]; q.push(x); x = a[i+1]; } else { //cout << '|' << a[i+1]; x = x | a[i+1]; } } //cout << endl; q.push(x); int ans = 0; while (!q.empty()) { int y = q.front(); //cout << ans << '^' << y << endl; ans = ans ^ y; q.pop(); } //cout << ans << endl; ansmin = min(ansmin, ans); } cout << ansmin << endl; }
#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(){ int A,B,S[3],T[3],a,b; cin >> A; cin >> B; S[0]=A/100; S[1]=(A-S[0]*100)/10; S[2]=(A-S[0]*100-S[1]*10); T[0]=B/100; T[1]=(B-T[0]*100)/10; T[2]=(B-T[0]*100-T[1]*10); a=S[0]+S[1]+S[2]; b=T[0]+T[1]+T[2]; if(a<b){ cout << b<< endl; }else{ cout << a <<endl; } }
#include <bits/stdc++.h> #define ll long long #define pii pair<ll,ll> #define x first #define y second using namespace std; int main(){ string a,b; cin>>a>>b; int a1=a[0]+a[1]+a[2]-'0'-'0'-'0'; int b1=b[0]+b[1]+b[2]-'0'-'0'-'0'; cout<<max(a1,b1); return 0; }
#include <bits/stdc++.h> /*#include <iostream> #include <algorithm> #include <math.h> #include <iomanip> #include <string> #include <vector> #include <set> #include <sstream>*/ #define ll long long #define fop(i,m,n) for(int i=m; i<n; i++) #define fastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define X first #define Y second #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() using namespace std; const long long MOD = 1e9+7; const long long N = 2e5+5; const long long Nlog = 17; const long long Hash = 257; const double PI = 2.0*acos(0.0);//<cmath> //const double PI = 3.141592653; const double E = 2.718281828; struct DSU{ int p[N+1]; void init(int n){ fop(i,1,n+1) p[i] = i; } int Find(int x){ if(x == p[x]) return x; else{ p[x] = Find(p[x]); return p[x]; } } void Union(int x, int y){ p[Find(x)] = Find(y); } }dsu; void solve(){ int n; cin >> n; int a[n]; fop(i,0,n) cin >> a[i]; dsu.init(N); int ans = 0; fop(i,0,n/2){ if(a[i] != a[n-1-i]){ if(dsu.Find(a[i]) != dsu.Find(a[n-1-i])){ ans++; dsu.Union(a[i], a[n-1-i]); } } } cout << ans << '\n'; } int main() { fastIO; int t=1; //cin >> t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> #define ll long long #define t \ ll t; \ cin >> t; \ while (t--) #define ln "\n" #define fst \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F(i, L, R) for (int i = L; i < R; i++) #define FE(i, L, R) for (int i = L; i <= R; i++) #define FF(i, L, R) for (int i = L; i > R; i--) #define FFE(i, L, R) for (int i = L; i >= R; i--) #define mod 1000000007 #define DBG(vari) cerr << #vari << " = " << (vari) << endl using namespace std; vector<int>v[200001]; bool vis[200001]; int ans=0; void dfs(int node) { vis[node]=1; ans++; for(auto child:v[node]) { if(vis[child]==0&&v[child].size()!=0) { dfs(child); } } } void solve() { ll n; cin>>n; int arr[n+3]; F(i,0,n)cin>>arr[i]; int i=0,j=n-1; while(i<j) { if(arr[i]!=arr[j]) { v[arr[i]].push_back(arr[j]); v[arr[j]].push_back(arr[i]); } i++; j--; } for(int k=0; k<n; k++) { if(v[arr[k]].size()!=0&&vis[arr[k]]==0) { dfs(arr[k]); ans--; } } cout<<ans<<ln; } int main() { fst solve(); }
#include <bits/stdc++.h> using namespace std; template<typename T> using vec = vector<T>; template<typename K, typename V> using umap = unordered_map<K, V>; using ll = long long; using ld = long double; using str = string; using pll = pair<ll, ll>; using vll = vec<ll>; using vi = vec<int>; using vs = vec<str>; using mapll = map<ll, ll>; using umapll = umap<ll, ll>; #define _rep(i, n) _repi(i, 0, n) #define _repi(i, l, r) for (ll i = ll(l); i < ll(r); i++) #define _get_rep(_1, _2, _3, NAME, ...) NAME #define rep(...) _get_rep(__VA_ARGS__, _repi, _rep) (__VA_ARGS__) #define rrep(i, n) for (ll i = ll(n) - 1; 0 <= i; i--) #define each(v) for (auto v) #define all(x) x.begin(), x.end() #define mkpair make_pair #define is_in(x, l, r) ((l) <= (x) && (x) < (r)) #define read(t, ...) t __VA_ARGS__; isin(cin, __VA_ARGS__) #define readv(t, v, n) vec<t> v(n); isin(cin, v) #define readv2(t, v, h, w) vec<vec<t>> v(h, vec<t>(w)); isin(cin, v) #define readvv(t, va, vb, n) vec<t> va(n), vb(n); rep (i, n) { isin(cin, va[i], vb[i]); } #define readvvv(t, va, vb, vc, n) vec<t> va(n), vb(n), vc(n); rep (i, n) { isin(cin, va[i], vb[i], vc[i]); } #define print(...) osout(cout, __VA_ARGS__) #define dbg(...) osout(cerr, __VA_ARGS__) const ll inf = numeric_limits<ll>::max() / 2; template<typename T> bool chmax(T &x, const T &y) { if (x < y) { x = y; return 1; } return 0; } template<typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return 1; } return 0; } template<typename T> void sort(vec<T> &v) { sort(all(v)); } template<typename T> void rsort(vec<T> &v) { sort(all(v), greater<T>()); } template<typename T> void dec(vec<T> &v) { rep (i, v.size()) v[i]--; } template<typename T> istream& operator >>(istream &is, vec<T> &v) { rep (i, v.size()) { cin >> v[i]; } return is; } template<typename T1, typename T2> ostream& operator <<(ostream &os, pair<T1, T2> p) { if (&os == &cout) { os << p.first << ' ' << p.second; } if (&os == &cerr) { os << '(' << p.first << ", " << p.second << ')'; } return os; } template<typename T> ostream& operator <<(ostream &os, vec<T> v) { if (&os == &cout) { rep(i, v.size() - 1) { os << v[i] << ' '; } os << v[v.size() - 1]; } if (&os == &cerr) { os << '['; rep(i, v.size() - 1) { os << v[i] << ", "; } os << v[v.size() - 1] << ']'; } return os; } void isin(istream &_) {} template<class S, class... T> void isin(istream &is, S &s, T&... t) { is >> s; isin(is, t...); } void osout(ostream& os) { os << endl; } template<class S, class... T> void osout(ostream &os, S s, T... t) { os << s; if (sizeof...(t)) os << ' '; osout(os, t...); } void solve(); int main() { #define PRECISION 0 cin.tie(0); ios::sync_with_stdio(false); if (PRECISION) { cout << fixed << setprecision(15); cerr << fixed << setprecision(15); } solve(); return 0; } void solve() { read(ll,N,C); readvvv(ll,a,b,c,N); vll d; umapll m; rep(i,N) { d.emplace_back(a[i]), d.emplace_back(b[i]+1); m[a[i]]+=c[i], m[b[i]+1]-=c[i]; } sort(d); d.erase(unique(all(d)),d.end()); ll e=d.size(); vll s(e+1); rep(i,e) s[i+1]=s[i]+m[d[i]]; ll ans=0; rep(i,e-1){ ans+=min(s[i+1],C)*(d[i+1]-d[i]); } print(ans); }
#include <bits/stdc++.h> using namespace std; int main() { int N; long long C; cin >> N >> C; map<long long, long long> mp; for( int i = 0; i < N; i++ ) { long long a, b, c; cin >> a >> b >> c; b++; mp[a] += c; mp[b] -= c; } long long ans = 0; long long cost = 0; long long prev = 0; for( auto it = mp.begin(); it != mp.end(); it++ ) { long long n = it->first - prev; ans += min( C, cost ) * n; cost += it->second; prev = it->first; } cout << ans << endl; }
/** ▂▃▅▇█▓▒░۩۞۩ ۩۞۩░▒▓█▇▅▃▂ In the name of Allah _ _ / / \ \ / / _ _ _ \ \ < < (_) (_) (_) > > \ \ / / \_\ /_/ Nerede Gitsen Çukur Orda **/ #include<bits/stdc++.h> #include<fstream> ///#include<conio.h> ///#include "windows.h" #define ll long long #define ld long double #define pb push_back #define pf push_front #define fr first #define sc second #define Qodirov_Kamoliddin ios_base::sync_with_stdio(0) ; cin.tie(NULL) ; cout.tie(NULL) ; #define p_b pop_back #define p_f pop_front #define quluq return 0; #define NO cout << "NO" << endl ; #define no cout << "no" << endl ; #define YES cout << "YES" << endl ; #define yes cout << "yes" << endl ; using namespace std ; const ll N = 1e9 + 7 ; ll n , k , cnt , ans , sum ; void vkoshp() { cin >> n ; cnt = n % 100 ; ans = 100 - cnt ; cout << ans << endl ; } int main () { /* freopen("input.txt", "r", stdin ); freopen("output.txt", "w", stdout ); */ Qodirov_Kamoliddin ///Author of the Greatest code : Murtazoev Alijon ///4294967295 ll Qu_l_uQ = 1 ; // cin >> Qu_l_uQ ; while ( Qu_l_uQ -- ) vkoshp() ; } ///MaaN studios ///Eco students
#pragma GCC optimize("O3") #pragma GCC target("popcnt") #include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); #define ll long long void fillPrefixSum(int arr[], int n, int prefixSum[]) // a[i-1,j-1] = a[j-1] - a[i-2]; { prefixSum[0] = arr[0]; // Adding present element // with previous element for (int i = 1; i < n; i++) prefixSum[i] = prefixSum[i - 1] + arr[i]; } void solve() { ll n; cin >> n; ll sum = 0; for (ll i = 1; i <= n; i++) { sum += i; if (sum >= n) { cout << i << endl; return; } } } int main() { fast int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); i++) using ll = long long; using P = pair<ll,ll>; ll MAX = 0; const ll INF = 1001001001001001001; ll n; vector<ll> ma(200005,-INF); vector<ll> mi(200005,INF); int main(){ cin >> n; rep(i,n){ ll x,c; cin >> x >> c; ma[c] = max(ma[c],x); mi[c] = min(mi[c],x); } ma[0] = 0; mi[0] = 0; ll dp[200005][2]; dp[0][0] = 0; dp[0][1] = 0; rep(i,n){ if(ma[i+1] == -INF){ dp[i+1][0] = dp[i][0]; dp[i+1][1] = dp[i][1]; ma[i+1] = ma[i]; mi[i+1] = mi[i]; } else if(mi[i+1] == ma[i+1]){ dp[i+1][0] = min(dp[i][1] + abs(mi[i+1] - mi[i]),dp[i][0] + abs(mi[i+1] - ma[i])); dp[i+1][1] = dp[i+1][0]; } else{ dp[i+1][0] = min(dp[i][1] + abs(mi[i+1] - mi[i]) + abs(ma[i+1]-mi[i+1]),dp[i][0] + abs(mi[i+1] - ma[i]) + abs(ma[i+1]-mi[i+1])); dp[i+1][1] = min(dp[i][1] + abs(ma[i+1] - mi[i]) + abs(ma[i+1]-mi[i+1]),dp[i][0] + abs(ma[i+1] - ma[i]) + abs(ma[i+1]-mi[i+1])); } } cout << min(dp[n][0]+abs(ma[n]),dp[n][1]+abs(mi[n])) << endl; /* rep(i,n+1){ cout << dp[i][0] << ' ' << dp[i][1] << endl; } */ }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define ALL(x) ((x).begin()), ((x).end()) #define READ(x) (cin >> (x)) #define WRITE_N(x) (cout << (x) << endl) #define WRITE(x) (cout << (x)) #define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No")) #define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); } struct state_t { ll time; ll place; }; #define INF 1000000001 ll solve(vector<ll> X, vector<int> C) { size_t N = X.size(); // balls[C] = {X1, X2, ...} map<int, vector<ll>> balls; for (size_t i = 0; i < N; i++) { if (balls.count(C[i]) == 0) { balls[C[i]] = {}; } balls[C[i]].push_back(X[i]); } balls[INF].push_back(0); vector<state_t> states{{0, 0}}; for (auto &&p : balls) { vector<ll> ball_places = p.second; sort(ALL(ball_places)); if (ball_places.size() == 1) { if (states.size() == 1) { state_t s = {states[0].time + abs(states[0].place - ball_places[0]), ball_places[0]}; states[0] = s; } else { vector<state_t> sv; for (auto &&s : states) { sv.push_back( {s.time + abs(s.place - ball_places[0]), ball_places[0]}); } states = sv; } } else { vector<state_t> sv; ll left = ball_places[0]; ll right = ball_places.back(); for (auto &&s : states) { if (s.place <= left) { sv.push_back({s.time + abs(s.place - right), right}); } else if (s.place >= right) { sv.push_back({s.time + abs(s.place - left), left}); } else { sv.push_back({s.time + abs(s.place - right) + (right - left), left}); sv.push_back({s.time + abs(s.place - left) + (right - left), right}); } } states = sv; } // trim sort(ALL(states), [](state_t &s1, state_t &s2) { return s1.time < s2.time; }); size_t n = (states.size() + 9) / 10; set<size_t> delete_i_s; for (size_t i = 0; i < n; i++) { for (size_t j = n; j < states.size(); j++) { if (states[j].time - states[i].time > abs(states[j].place - states[i].place)) { delete_i_s.insert(j); } } } vector<size_t> delete_i_v(ALL(delete_i_s)); for (int i = delete_i_v.size() - 1; i >= 0; i--) { states.erase(states.begin() + delete_i_v[i]); } } sort(ALL(states), [](state_t &s1, state_t &s2) { return s1.time < s2.time; }); return states[0].time; } int main() { // get values from input cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<ll> X(N); vector<int> C(N); for (size_t i = 0; i < N; i++) { cin >> X[i] >> C[i]; } // main procedure // output cout << solve(X, C) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define MS(x) memset((x), -1, sizeof((x))) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } #define MOD 1000000007 int k; string s; int memo[200010][17][17]; int dp(int i, int j, int k){ if(i == 0){ return j == 0 ? 1 : 0; } if(~memo[i][j][k]) return memo[i][j][k]; int ans = 0; if(j > 0){ ans += dp(i-1, j-1, k)*(16-(k+j-1)) % MOD; } ans += dp(i-1, j, k)*(j+k) % MOD; return memo[i][j][k] = ans%MOD; } signed main(){ cin >> s >> k; int n = s.size(); int used[512] = {}; int cnt = 0; int ans = 0; string moji = "0123456789ABCDEF"; MS(memo); for(int i = 0;i < n;i++){ int tmp = 0; int j; for(j = 0;moji[j] != s[i];j++){ tmp += used[moji[j]]; } int rnum = n-i-1; if(i > 0){ ans += dp(rnum, k-1, 1)*15%MOD; ans %= MOD; } if(j != 0){ if(tmp != j && cnt+1 <= k){ if(i == 0){ ans += dp(rnum, k-1, 1)*(j-1)%MOD; }else{ ans += dp(rnum, k-(cnt+1), cnt+1)*(j-tmp)%MOD; } ans %= MOD; } if(cnt > 0 && cnt <= k){ ans += dp(rnum, k-cnt, cnt)*tmp % MOD; ans %= MOD; } } if(used[s[i]] == 0){ used[s[i]] = 1; cnt++; } } if(cnt == k) ans = (ans+1) % MOD; cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int mod=1e9+7; typedef long long ll; string num; int rn[200005]; int k; int n; int dp[200005][20]; ll dfs(int x,ll now,bool zero,bool flag) { int cnt=0; for(int i=0;i<16;i++) { if(now&(1<<i))cnt++; } if(x>=n)return cnt==k; if(cnt>k)return 0; if(!flag&&zero&&dp[x][cnt]!=-1)return dp[x][cnt]; ll ans=0; int lim=flag?rn[x]:15; for(int i=0;i<=lim;i++) { int tmp; if(!zero&&i==0)tmp=now; else tmp=now|(1<<i); ans+=dfs(x+1,tmp,zero||(i!=0),flag&&(i==lim)); ans%=mod; } if(!flag&&zero)dp[x][cnt]=ans; return ans; } int main() { cin>>num>>k; n=num.size(); memset(dp,-1,sizeof dp); for(int i=0;i<n;i++) { if(num[i]>='0'&&num[i]<='9')rn[i]=num[i]-'0'; else rn[i]=num[i]-'A'+10; } printf("%lld\n",dfs(0,0,0,1)); return 0; }
#include <bits/stdc++.h> #define debug(var) do{std::cout << #var << " :";std::cout << std::endl;view(var);}while(0) template<typename T> void view(T e){ std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::pair<T, U>& v){ std::cout << v.first << ", " << v.second << std::endl; } template<typename T> void view(const std::set<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::map<T, U>& v){ std::cout << "{" << std::endl; for(const std::pair<T, U>& e : v){ std::cout << e.first << ", " << e.second << std::endl; } std::cout << "}" << std::endl; } template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cout << "{" << std::endl; for(const auto& v : vv){ view(v); } std::cout << "}" << std::endl; } using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef pair<int, int> pii; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; const long long MOD = 1000000007; const double PI = acos(-1); int main() { int a, b, c; cin >> a >> b >> c; if (a * a + b * b < c * c) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <queue> #include <functional> #include <vector> #include <stack> #include <set> #include <string> #include <time.h> #include <assert.h> #include <deque> //#include <unordered_map> #define ls k*2 #define rs k*2+1 #define int long long using namespace std; typedef long long ll; const int maxn=2e6+50; const int inf=0x3f3f3f3f3f3f3f3f; const int mod=998244353; const double eps=1e-6; const int HASH=131; using namespace std; int a[maxn]; signed main() { int a,b,c; cin>>a>>b>>c; if(a+b==c*2) { cout<<"Yes"<<endl; return 0; } if(a+c==b*2) { cout<<"Yes"<<endl; return 0; } if(c+b==a*2) { cout<<"Yes"<<endl; return 0; } cout<<"No"<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define fi first #define se second #define pb push_back #define mp make_pair #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define asd cout << "ok" << endl; #define trav(i, n) for(int i= 0; i <n; i++) const char nl = '\n'; const int maxN = 2e5 + 4; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int a, b, c ,d; cin >> a >> b >> c >> d; int ans = 0; if(c*d <= b){ cout << -1; return 0; } cout << (a -1 + d*c -b)/ (d*c - b) << nl; }
#include<bits/stdc++.h> using namespace std; int main(){ int a,s,d; int g,h=0; cin >> a>>s>>d; vector<int>f(a); for(int i=0;i<a-1;i++){ cin>>g; h=h+g; } g=d*a-h; if(d*a-h<0)g=0; if(d*a-h<=s) cout <<g; else if(d*a-h>s) cout <<-1; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) using pii = pair<int, int>; using vi = vector<int>; using vii = vector<vi>; using ll = long long; int main() { int a, b, x, y; cin >> a >> b >> x >> y; int t; t =min(2 * x, y); int t1; a = 2 * a; b = 2 * b + 1; t1 = abs(a - b); cout << t1 / 2 * t + t1 % 2 * x << endl; }
#include<bits/stdc++.h> using namespace std; #define int long long #define x first #define y second #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define forn(i,n) for(int i=0;i<(int)n;i++) #define pb push_back #define sz(a) (int)(a.size()) typedef long long ll;typedef vector<int> vi; typedef pair<int,int> ii;typedef vector<ii> vii; typedef vector<vi> vvi; const int N = 220; vi adj[N]; vi adw[N]; int dis[N],vis[N]; void add(int a,int b,int w){ adj[a].pb(b);adw[a].pb(w); adj[b].pb(a);adw[b].pb(w); } signed main(){ IOS int a,b,x,y;cin>>a>>b>>x>>y; a--;b--; forn(i,N) dis[i]=INT_MAX; for(int i=0;i<100;i++){ add(i,i+100,x); if(i){ add(i,i-1,y); add(i+100,i-1+100,y); add(i,i-1+100,x); } } priority_queue<ii,vii,greater<ii>> pq; dis[a]=0; pq.push({0,a}); while(!pq.empty()){ ii u = pq.top();pq.pop(); if(vis[u.y]) continue; dis[u.y]=u.x;vis[u.y]=1; forn(i,sz(adj[u.y])){ int v = adj[u.y][i]; int w = adw[u.y][i]; if(vis[v]) continue; pq.push({u.x+w,v}); } } cout<<dis[b+100]<<"\n"; }
#include <bits/stdc++.h> #define REP(i, s, n) for (int i = s; i < (int)(n); i++) #define ALL(a) a.begin(), a.end() #define MOD 1000000007 using namespace std; using ll = long long; template<int M> class ModInt { public: ll value; constexpr ModInt(ll v = 0) { value = v % M; if (value < 0) value += M; } constexpr ModInt pow(ll n) const { if (!n) return 1; ModInt a = pow(n >> 1); a *= a; if (n & 1) a *= *this; return a; } constexpr ModInt inv() const { return this->pow(M - 2); } constexpr ModInt operator + (const ModInt &rhs) const { return ModInt(*this) += rhs; }; constexpr ModInt operator - (const ModInt &rhs) const { return ModInt(*this) -= rhs; }; constexpr ModInt operator * (const ModInt &rhs) const { return ModInt(*this) *= rhs; }; constexpr ModInt operator / (const ModInt &rhs) const { return ModInt(*this) /= rhs; }; constexpr ModInt &operator += (const ModInt &rhs) { value += rhs.value; if (value >= M) value -= M; return *this; }; constexpr ModInt &operator -= (const ModInt &rhs) { value -= rhs.value; if (value < 0) value += M; return *this; }; constexpr ModInt &operator *= (const ModInt &rhs) { value = value * rhs.value % M; return *this; }; constexpr ModInt &operator /= (const ModInt &rhs) { return (*this) *= rhs.inv(); }; constexpr bool operator == (const ModInt &rhs) { return value == rhs.value; } constexpr bool operator != (const ModInt &rhs) { return value != rhs.value; } friend ostream &operator << (ostream &os, const ModInt &rhs) { os << rhs.value; return os; } }; using mint = ModInt<MOD>; int main() { int H, W; cin >> H >> W; vector<string> S(H); REP(i, 0, H) cin >> S[i]; int tot = 0; vector<vector<int>> R(H, vector<int>(W, 0)), C(H, vector<int>(W, 0)); REP(i, 0, H) REP(j, 0, W) { if (S[i][j] == '#') continue; R[i][j] = 1 + ((j > 0) ? R[i][j - 1] : 0); C[i][j] = 1 + ((i > 0) ? C[i - 1][j] : 0); tot++; } REP(i, 0, H) for (int j = W - 1; j > 0; j--) if (R[i][j] && R[i][j - 1]) R[i][j - 1] = R[i][j]; REP(j, 0, W) for (int i = H - 1; i > 0; i--) if (C[i][j] && C[i - 1][j]) C[i - 1][j] = C[i][j]; mint ans = 0; REP(i, 0, H) REP(j, 0, W) { if (S[i][j] == '#') continue; // cout << "#(" << i << "," << j << ") : " << R[i][j] << ", " << C[i][j] << endl; int cnt = R[i][j] + C[i][j] - (R[i][j] && C[i][j]); ans += (mint(2).pow(cnt) - 1) * mint(2).pow(tot - cnt); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> //#include<atcoder/all> using namespace std; //using namespace atcoder; #define MOD 1000000007 //https://flex.phys.tohoku.ac.jp/~maru/implementations/exponentiation_squaring.php long long pow(long long a, long long n, long long m) { long long ret = 1; for (; n > 0; n >>= 1, a = a * a % m) { if (n % 2 == 1) { ret = ret * a % m; } } return ret; } int main() { long long H,W; long long ans=0; long long dot=0; cin>>H>>W; string S[H]; long long r[H][W]; long long l[H][W]; long long u[H][W]; long long d[H][W]; for(long long i=0;i<H;i++){ cin>>S[i]; } for(long long i=0;i<H;i++){ for(long long j=0;j<W;j++){ if(S[i][j]=='.') dot++; } } for(long long i=0;i<H;i++){ long long c=0; for(long long j=0;j<W;j++){ if(S[i][j]=='.') c++; else c=0; r[i][j]=c; } } for(long long i=0;i<H;i++){ long long c=0; for(long long j=W-1;j>=0;j--){ if(S[i][j]=='.') c++; else c=0; l[i][j]=c; } } for(long long j=0;j<W;j++){ long long c=0; for(long long i=0;i<H;i++){ if(S[i][j]=='.') c++; else c=0; d[i][j]=c; } } for(long long j=0;j<W;j++){ long long c=0; for(long long i=H-1;i>=0;i--){ if(S[i][j]=='.') c++; else c=0; u[i][j]=c; } } for(long long i=0;i<H;i++){ for(long long j=0;j<W;j++){ if(S[i][j]=='.'){ long long dot2=r[i][j]+l[i][j]+u[i][j]+d[i][j]-3; long long a=(pow(2,dot2,MOD)-1)%MOD; while(a<0) a+=MOD; long long b=pow(2,dot-dot2,MOD); //cout<<i<<","<<j<<"---"<<dot<<","<<dot2<<"---"<<a<<","<<b<<endl; ans+=a*b%MOD; ans%=MOD; } } } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using vl = vector<ll>; using vvl = vector<vl>; using pl = pair<ll, ll>; const ll INF = ll(1e18); const ll mod = ll(1e9 + 7); const double pi = acos(-1); #define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i)) #define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i)) #define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i)) #define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i)) #define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i)) #define rnfor(i,a,b) for(ll (i) = (b) - 1; (i) >= (a); --(i)) #define pf(x) cout << (x) << endl #define all(x) (x).begin(),(x).end() #define yes pf("Yes") #define no pf("No") template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const int dx[8]={1,0,-1,0,1,1,-1,-1}; const int dy[8]={0,1,0,-1,1,-1,1,-1}; int multipf(vector<string>& s){ cout << s[0]; rep1(i, s.size() - 1)cout << " " << s[i]; cout << endl; return 0; } int multipf(vector<ll>& n){ cout << n[0]; rep1(i, n.size() - 1)cout << " " << n[i]; cout << endl; return 0; } ll gcd(ll a,ll b){ if(a < b)swap(a, b); if(b == 0) return a; return gcd(b,a%b); } ll lcm(ll a,ll b){ ll g = gcd(a,b); return a / g * b; } ll factorial(ll n){ ll ans = 1; rep1(i, n){ ans *= i; ans %= mod; } return ans; } ll power(ll a, ll b){ ll ans = 1; while(b) { if(b & 1LL) ans = ans * a % mod; ans %= mod; a = a * a; a %= mod; b >>= 1; } return ans % mod; } struct UnionFind { vector<ll> par; vector<ll> rank; vector<ll> Size; UnionFind(ll n) { init(n); } void init(ll n) { par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1); rep0(i, n + 1){ par[i] = i; rank[i] = 0; Size[i] = 1; } } ll root(ll x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } bool same(ll x, ll y) { return root(x) == root(y); } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; Size[x] += Size[y]; return true; } ll size(ll x){ return Size[root(x)]; } }; //modの値の確認をすること int main(){ ll n; cin >> n; vl cnt(400001,0); UnionFind u(400001); rep0(i, n){ ll a,b; cin >> a >> b; cnt[a]++; cnt[b]++; u.merge(a, b); } rep0(i, 400001)if(u.root(i) != i)cnt[u.root(i)] += cnt[i]; ll ans = 0; vector<bool> f(400001,false); rep0(i, 400001){ if(u.root(i) != i)continue; if(u.size(i) >= cnt[i] / 2)ans += cnt[i] / 2; else ans += u.size(i); } pf(ans); return 0; }
#include "bits/stdc++.h" using namespace std; #define int long long vector<vector<int>> adj; vector<int> vis; vector<int> sel; int cnt, nn, edges; void dfs(int root, int parent) { vis[root] = 1; cnt++; edges += sel[root]; for (auto i : adj[root]) { if (!vis[i]) { //cout << i << " "; dfs(i, parent); } } } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); set<int> s; int n; cin >> n; vector<pair<int, int>> v; int t = 1; map<int, int> m; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (a > b)swap(a, b); v.push_back(make_pair(a, b)); if (m[a] == 0) { m[a] = t; t++; } if (m[b] == 0) { m[b] = t; t++; } s.insert(a); s.insert(b); } nn = s.size(); adj = vector<vector<int>>(nn); vis = vector<int> (nn, 0); sel = vector<int> (nn, 0); for (int i = 0; i < n; i++) { int a = v[i].first, b = v[i].second; adj[m[a] - 1].push_back(m[b] - 1); adj[m[b] - 1].push_back(m[a] - 1); sel[m[a] - 1]++; } int best = 0; for (int i = 0; i < nn; i++) { if (!vis[i]) { //cout << i << "\n"; edges = 0; cnt = 0; dfs(i, i); //cout << edges << "\n"; best += max(min(cnt, edges), 1ll); } } cout << best << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #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 ll long long long long binpow(long long a, long long b) { if (b == 0) return 1; long long res = binpow(a, b / 2); if (b % 2) return res * res * a; else return res * res; } long long my_ceil(long long a, long long b){ return (a + b - 1)/b; } void test_case(){ ll x, y, a, b; cin >> x >> y >> a >> b; int n = 0; long long ans = INT_MIN; while(true){ if(n*log(a) + log(x) - log(y) < 1e-18){ ans = max(ans, n - 1 + my_ceil(y - (long long)binpow(a, n)*x, b)); n += 1; }else{ break; } } cout << ans << endl; } int main(){ int t = 1; //cin >> t; for(int i = 0; i < t; i++) test_case(); return 0; }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;} template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;} template<typename T> vector<T> make_v(size_t a){return vector<T>(a);} template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts){ return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); } template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value!=0>::type fill_v(U &u,const V... v){u=U(v...);} template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value==0>::type fill_v(U &u,const V... v){ for(auto &e:u) fill_v<T>(e,v...); } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int64 N; cin >> N; vector<int64> A(N), B(N); REP(i, N) cin >> A[i] >> B[i]; int64 res = 0; REP(i, N) { res += (A[i] + B[i]) * (B[i] - A[i] + 1) / 2; } cout << res << endl; }
#include<bits/stdc++.h> #define ll long long #define int long long #define pb push_back #define mp make_pair #define eb emplace_back #define fi first #define se second #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 using namespace std; //------------------------------------------------------------------------------ ll power(ll a,ll n) { ll result=1; while(n>0) { if(n&1) result=result*a; a=a*a; n=n>>1; } return result; } ll lcm(ll a,ll b) { return ((a*b)/__gcd(a,b)); } ll __ceil(ll a,ll b) { return (a+b-1)/b; } bool ispow2(ll n) { return (ceil(log2(n))==floor(log2(n))); } bool isprime(ll n) { if(n==1) return false; else if(n==2) return true; else { for(ll i=2;i*i<=n;++i) { if(n%i==0) return false; } return true; } } ll ncr(ll n,ll r) { if(r > n - r) r = n - r; ll ans = 1; ll i; for(i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans; } void sieve(int n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. bool prime[n+1]; memset(prime, true, sizeof(prime)); prime[1]=false; prime[0]=false; for (int p=2; p*p<=n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i=p*p; i<=n; i += p) prime[i] = false; } } // Print all prime numbers for (int p=2; p<=n; p++) if (prime[p]) cout<<p<<" "; } //------------------------------------------------------------------------------ int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t=1; //cin>>t; while(t--) { int c; cin>>c; if(c>=0) cout<<c; else cout<<0; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ms(s, n) memset(s, n, sizeof(s)) #define f(i, a, b) for (int i = (a); i <=(b); i++) #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--) #define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define sz(a) int((a).size()) #define present(t, x) (t.find(x) != t.end()) #define all(a) (a).begin(), (a).end() #define uni(a) (a).erase(unique(all(a)), (a).end()) #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define prec(n) fixed<<setprecision(n) #define bit(n, i) (((n) >> (i)) & 1) #define bitcount(n) __builtin_popcountll(n) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pi; typedef vector<ll> vi; typedef vector<pi> vii; const int MOD = (int) 1e9 + 7; const int MOD2 = MOD + 2; //1007681537; const int INF = (int) 1e9; const ll LINF = (ll) 1e18; const ld PI = acos((ld) - 1); const ld EPS = 1e-9; inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;} inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;} template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;} inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;} inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;} inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;} inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;} inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;} inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);} inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;} inline int sign(ld x, ld y) {return sign(x - y);} int modulo(int a, int b) { return (a % b + b) % b; } #define db(x) cerr << #x << " = " << (x) << " "; #define endln cerr << "\n"; int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n, ans; cin >> n; if (n >= 0) { ans = n; } else { ans = 0; } cout << ans << "\n"; // read the stuff at the bottom return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * use int when constraints limits are tight * Time taken : global variable < pass by refernce < pass by value * It takes significant amt. of time in pass by value * Avoid Using endl */
#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(size_t i = (l); i < (r); ++i) #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define rrep(i, n) for(int i = (n)-1; i >= 0; --i) #define rfor(v, vec) for(auto &v : (vec)) #define itrloop(itr, begin, end) for(auto itr = (begin); itr != end; ++itr) #define crfor(v, vec) for(const auto &v : (vec)) #define all(x) x.begin(), x.end() #define call(x) x.cbegin(), x.cend() #define rall(x) x.rbegin(), x.rend() #define ull unsigned long long #define ll long long #define ul unsigned long // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: // You use the default template now. You can remove this line by using your // custom template) int main() { long long T; scanf("%lld", &T); std::vector< long long > cs(T); for(int i = 0; i < T; i++) { scanf("%lld", &cs[i]); } crfor(c, cs) { if(c % 2 == 1) cout << "Odd" << endl; else if(c % 4 == 0) cout << "Even" << endl; else cout << "Same" << endl; } return 0; }
#include <bits/stdc++.h> #define for0(i, n) for (int i = 0; i < (ll)(n); ++i) #define for1(i, n) for (int i = 1; i <= (ll)(n); ++i) #define forc(i, l, r) for (int i = (ll)(l); i <= (ll)(r); ++i) #define forr0(i, n) for (int i = (ll)(n) - 1; i >= 0; --i) #define forr1(i, n) for (int i = (ll)(n); i >= 1; --i) #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin, (x).rend() #define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define sz(a) ll((a).size()) #define mod 1000000007 using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef double ld; void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool prime[1000001]; void sieve() { memset(prime, true, sizeof(prime)); prime[1]=false; for (int p=2; p*p<=1000000; p++) { if (prime[p] == true) { for (int i=p*p; i<=1000000; i += p) prime[i] = false; } } } void solve() { ll n; cin >> n; if(n%2==1)cout << "Odd\n"; else if((n-2)%4==0)cout << "Same\n"; else cout << "Even\n"; } int main() { fast(); ll t; cin >> t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; /* #define MULTI 1 */ void pre() { } void solve() { int n, m; cin >> n >> m; int a[1500005]; for (int i = 0; i < n; ++i) cin >> a[i]; int cnt[1500005] = {}; for (int i = 0; i < m; ++i) ++cnt[a[i]]; priority_queue<int, vector<int>, greater<int>> pq; for (int i = 0; i < n; ++i) if (cnt[i] == 0) pq.push(i); pq.push(n); int ans = pq.top(); for (int i = 0; i + m < n; ++i) { --cnt[a[i]]; if (cnt[a[i]] == 0) pq.push(a[i]); ++cnt[a[i + m]]; while (cnt[pq.top()] > 0) pq.pop(); ans = min(ans, pq.top()); } cout << ans << endl; } int main() { pre(); #ifdef MULTI int T; cin >> T; for (int t = 0; t < T; ++t) { #endif solve(); #ifdef MULTI } #endif return 0; }
#include<bits/stdc++.h> using namespace std; char s[11][11]; int ans; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<n;i++){ int use=0; for(int j=1;j<=m;j++){ if(s[i][j]!=s[i+1][j]){ if(use==0) ans++; use=1; } else use=0; } } for(int j=1;j<m;j++){ int use=0; for(int i=1;i<=n;i++) if(s[i][j]!=s[i][j+1]){ if(use==0) ans++; use=1; } else use=0; } cout<<ans; }
#include<algorithm> #include<array> #include<bitset> #include<cassert> #include<climits> #include<cmath> #include<cstring> #include<deque> #include<functional> #include<iostream> #include<iomanip> #include<map> #include<numeric> #include<optional> #include<queue> #include<set> #include<stack> #include<string> #include<tuple> #include<unordered_map> #include<unordered_set> #include<vector> #pragma GCC optimize("Ofast") namespace gengar { using namespace std; #define int long long #define itn int #define uint unsigned long long #define ld long double #define vt(tp) vector<tp> #define vvt(tp) vector<vector<tp>> #define vvt2(nm,tp,h,w,n) vector<vector<tp>>nm((h),vector<tp>(w,n)) #define P pair<int,int> #define hmap unordered_map #define hset unordered_set #define all(x) x.begin(),x.end() #define nsort(x) sort(all(x)) #define rsort(x) nsort(x);reverse(all(x)) #define unq(v) v.erase(unique(all(v)),v.end()) #define l_b(c,x) distance(c.begin(),lower_bound(all(c),(x))) #define u_b(c,x) distance(c.begin(),upper_bound(all(c),(x))) #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define spc ' ' #define pass #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=(a),__SIZE##_=(b);i<__SIZE##_;i++) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define _daolrevo3(_1,_2,_3,name,...) name #define _per(i,n) peri(i,n,0) #define peri(i,a,b) for(int i=(a),__SIZE##_=(b);i>=__SIZE##_;i--) #define per(...) _daolrevo3(__VA_ARGS__,peri,_per,)(__VA_ARGS__) #define Bit(n) (1LL<<(n)) #define myceil(a,b) ((a)+((b)-1))/(b) #define scale(n) cout<<fixed<<setprecision(n) using i64=int64_t; using u64=uint64_t; template<class T>using PQ=priority_queue<T,vt(T),greater<T>>; void in(){} template<class Car,class...Cdr> void in(Car&&car,Cdr&&...cdr){cin>>car;in(forward<Cdr>(cdr)...);} template<class T>void drop(T x){cout<<(x)<<endl;exit(0);} void dYes(){puts("Yes");exit(0);} void dNo(){puts("No");exit(0);} int gcd(int a,int b){return b?gcd(b,a%b):a;} int lcm(int a,int b){return a/gcd(a,b)*b;} int fact(int n,int m){int f=n;for(int i=n-1;i>=1;i--){f*=i;f%=m;}return f;} template<class T>int chmin(T&a,const T&b){if(b<a){a=b;return 1;}return 0;} template<class T>int chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;} const int inf=Bit(60); const double pi=acos(-1); const int mod=1000000007;//998244353 }; using namespace gengar; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n;cin>>n; int sum=0; for(int a;cin>>a;)sum+=max(0LL,a-10); cout<<sum<<endl; return 0; }
#include<iostream> #include<algorithm> #include<map> using namespace std; int main(){ int n; while(cin>>n){ int np,ans=0; for(int i=0;i<n;i++){ cin>>np; if(np>10)ans+=np-10; } cout<<ans<<endl; } }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/rope> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/trie_policy.hpp> // using namespace __gnu_pbds; // using namespace __gnu_cxx; // typedef tree<long long, null_type, std::less<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds; // typedef trie<std::string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>Trie; using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define sz(x) ((int)(x).size()) #define rdv(x) long long x; cin>>x; #define deb(x) cout<<#x<<"="<<x<<endl; #define inf 1e18 #define endl "\n" typedef vector<long long> vll; typedef long long ll; typedef pair<long long,long long> pii; typedef long double ld; mt19937 mrand(random_device{}()); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} bool prime[200001]; void SieveOfEratosthenes() { memset(prime, true, sizeof(prime)); prime[1]=false; for (int p=2; p*p<=200000; p++) { if (prime[p] == true) { for (int i=p*p; i<=200000; i += p) prime[i] = false; } } return; } ll ceil(ll x,ll y) { ll ans=x/y; if(x%y) ans++; return ans; } void file_i_o(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } // head void solve() { ll n,k; cin>>n>>k; vll arr(n); rep(i,0,n) { cin>>arr[i]; } map<ll,ll>mp; set<ll> s; for (int i = 0; i <= n; i++) s.insert(i); for (int i = 0; i < k; i++) {s.erase(arr[i]); mp[arr[i]]++; } ll mex = *(s.begin()); //deb(mex) // for(auto it:s) // { // deb(it) // } for (int i = k; i < n; i++) { mp[arr[i]]++; mp[arr[i-k]]--; s.erase(arr[i]); if(mp[arr[i-k]]==0) { s.insert(arr[i - k]); } ll firstElem = *(s.begin()); mex = min(mex, firstElem); } cout<<mex; cout<<"\n"; } int main() { ios_base::sync_with_stdio(false); //Used to reduce the time cin.tie(NULL); cout.tie(NULL); file_i_o(); ll T=1; //cin>>T; while(T--) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define FOR(i, begin, end) for(int i=begin;i<end;i++) void solve() { int N, M; vector<int> counter(1500001, -1); cin >> N >> M; int ans = INT_MAX; for (int i = 0; i < N; i++) { int elt; cin >> elt; if (i - counter[elt] > M || (counter[elt] == -1 && i >= M)) ans = min(ans, elt); counter[elt] = i; } for (int i = 0; i < M + 1; i++) { if (N - counter[i] > M || counter[i] == -1) ans = min(ans, i); } cout << ans << "\n"; } int main() { solve(); }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={1,0,-1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main(){ ll n,m;cin >> n >>m; vl a(m);rep(i,m)cin >> a[i]; a.pb(0);a.pb(n+1); sort(all(a)); vl div; rep(i,m+1){ ll k=a[i+1]-a[i]-1; if(k)div.pb(k); } if(div.size()==0)cout << 0 <<endl,exit(0); //for(auto p:div)cout << p <<endl; sort(all(div)); ll k=div[0]; ll ans=0; for(auto p:div){ ans+=(p+k-1)/k; } cout << ans <<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(m); for (int i = 0; i < m; i++) { cin >> a[i]; } sort(a.begin(), a.end()); int x = 0; int g = n; for (int i = 0; i < m; i++) { if (a[i] > x + 1) { g = min(g, a[i] - x - 1); } x = a[i]; } x = 0; int c = 0; for (int i = 0; i < m; i++) { if (a[i] > x + 1) { c += (a[i] - x - 1) / g + ((a[i] - x -1) % g > 0 ? 1 : 0); } x = a[i]; } if (x < n) { c += (n - x) / g + ((n - x) % g > 0 ? 1 : 0); } cout << c << endl; return 0; }
// 解説を参考にコーディング #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vii = vector<vector<int>>; using vl = vector<long long>; using vll = vector<vector<long long>>; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define len(a) (int)a.size() #define all(a) a.begin(), a.end() const int INF32 = 1 << 30; const long long INF64 = 1LL << 60; const double PI = acos(-1); /* 1次元ベクトルをスペースでつなぎ、改行をつけて出力 vi, vl, list<int>, list<ll>に対応 */ template<class T> string print_elements(T a) { string str = ""; for (auto itr = a.begin(); itr != a.end(); ++itr) { str += to_string(*itr) + ' '; } str.pop_back(); // 最後の空白を削除 str += '\n'; return str; } int main() { int N; cin >> N; vl A(N); rep(i, N) cin >> A[i]; map<int, int> m; bool result = false; int ans1, ans2; int N2 = min(8, N); rep(i, (1 << N2)) { if (i == 0) continue; int val = 0; rep(j, N2) { if ((i >> j) & 1) { val += A[j] % 200; } } val %= 200; if (!m.count(val)) { m[val] = i; } else { result = true; ans1 = m.at(val); ans2 = i; break; } } vi v1, v2; if (result) { string str1 = to_string(__builtin_popcount(ans1)) + ' '; string str2 = to_string(__builtin_popcount(ans2)) + ' '; cout << "Yes" << endl; rep(i, 7) { if ((ans1 >> i) & 1) { v1.push_back(i + 1); } if ((ans2 >> i) & 1) { v2.push_back(i + 1); } } str1 += print_elements(v1); str2 += print_elements(v2); cout << str1 << str2; } else { cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ull = unsigned long long int; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N; cin >> N; vector<ll> A(N); for (auto &a : A) cin >> a, a %= 200; int cnt = min(N, 8); vector<int> v(200, -1); for (int bit = 1; bit < (1 << cnt); bit++) { int b = 0; for (int i = 0; i < cnt; i++) { if (bit & (1 << i)) b += A[i]; } b %= 200; if (v[b] == -1) v[b] = bit; else { vector<int> B, C; for (int i = 0; i < cnt; i++) { if (bit & (1 << i)) B.push_back(i + 1); if (v[b] & (1 << i)) C.push_back(i + 1); } cout << "Yes" << endl; cout << B.size() << " "; for (auto i : B) cout << i << " "; cout << endl << C.size() << " "; for (auto i : C) cout << i << " "; cout << endl; return 0; } } cout << "No" << endl; }
//https://atcoder.jp/contests/abc189/tasks/abc189_d //题意:给定一串OR和AND组成的n长字符串,代表V,^求有多少种n+1长的由0,1组成的字串使两两运算最终结果为1 //思路:找规律,默认有一种,有OR才增加,还与OR有关。 #include <stdio.h> int main(int argc, char **argv) { int N, i; unsigned long long T = 1; scanf("%d", &N); for(i = 1; i <= N; ++i){ char buff[10]; scanf("%s", buff); if(buff[0] == 'O'){ T = T + (1ull<<i); } } printf("%llu", T); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define ull unsigned long long #define pii pair<int,int> #define tiii tuple<int,int,int> #define pll pair<long long, long long> #define pdd pair<double, double> #define s second #define f first #define pb push_back #define oo 1000000000000000000ll void solve() { int n; cin >> n; vector<int> cnt(n+1); for (int i = 0; i < n; ++i){ int x; cin >> x; cnt[x]++; } bool ans = true; for (int i = 1; i <= n; ++i){ ans &= cnt[i] == 1; } if (ans) cout << "Yes\n"; else cout << "No\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tst; tst = 1; //cin >> tst; while (tst--){ solve(); } return 0; }
#include<iostream> #include<string> using namespace std; int main() { string s; cin>>s; for(int i=0;i<s.size();i++) { if(s.at(i)=='6') { s.at(i)='9'; } else if(s.at(i)=='9') { s.at(i)='6'; } } for(int i=s.size()-1;i>=0;i--) { cout<<s[i]; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; reverse(s.begin(),s.end()); for(char &c : s){ if(c == '6' || c == '9'){ c = 15 - (c-'0') + '0'; } } cout << s << '\n'; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define ins insert template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; //check the limits, dummy ll read() { ll K = 0; string S; cin >> S; int cnt = 0; bool found = false; bool neg = false; trav(a, S) { if (a == '.') { found = true; continue; } else if (a == '-') { neg = true; continue; } if (found) cnt++; K *= 10; K += a - '0'; } FOR(i, cnt, 4) K *= 10; if (neg) K *= -1; return K; } void solve() { ll X = read(), Y = read(), R = read(); R *= R; ll ans = 0; for (ll x = -2000000000; x <= 2000000000; x += 10000) { ll lo = Y, hi = 2000000000; assert(lo < hi); ll G = (X-x)*(X-x); if (G > R) continue; while (lo < hi) { ll mid = lo + (hi-lo+1)/2; if ((Y-mid)*(Y-mid) <= R-G) { lo = mid; } else { hi = mid-1; } } ll upB = lo; lo = -2000000000; hi = Y; assert(lo < hi); while (lo < hi) { ll mid = lo + (hi-lo)/2; if ((Y-mid)*(Y-mid) <= R-G) { hi = mid; } else { lo = mid+1; } } ll loB = lo; loB += 1e12; upB += 1e12; assert(loB >= 0); assert(upB >= 0); loB = (loB + 9999) / 10000; upB = upB / 10000; assert(upB - loB + 1 >=0); ans += upB - loB + 1; } cout << ans << nl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while(T--) { solve(); } return 0; } // read the question correctly (ll vs int) // template by bqi343
#include <stdio.h> #include <algorithm> #include <vector> #include <string> #include <queue> #include <math.h> const long long mod = 1000000007; //const long long mod = 998244353; int main() { int N, K, M; scanf("%d %d %d", &N, &K, &M); int *A = new int[N]; int sum = 0; for (int i = 0; i < N - 1; i++) { scanf("%d", A + i); sum += A[i]; } int r = N * M - sum; if (r<0) { printf("0"); } else if(r>K){ printf("-1"); } else { printf("%d", r); } return 0; }
# include <iostream> using namespace std; int N, K; int main() { cin >> N >> K; cout << int((1 + N)*N * 0.5 * 100 * K + (1 + K)* K * 0.5 * N) ; }
#include<bits/stdc++.h> //#define int long long using namespace std; void read() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif } int32_t main() { read(); ios::sync_with_stdio(0); cin.tie(0); int T; T = 1; //cin >> T; while (T--) { int n, m; cin >> n >> m; int sum = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) sum += (i * (100) + j); } cout << sum; } }
#include<bits/stdc++.h> using namespace std; int n,k,a[110],b[110];char c[110]; int sol(int a,int b){if(a>b)swap(a,b);if(a==0&&b==2)return a;return b;} int main() { scanf("%d%d%s",&n,&k,c+1); for(int i=1;i<=n;i++) { if(c[i]=='R')a[i]=0; if(c[i]=='P')a[i]=1; if(c[i]=='S')a[i]=2; } while(k--) { if(n&1) { int m=0; for(int i=1;i<=n/2;i++)b[++m]=sol(a[i*2-1],a[i*2]);b[++m]=sol(a[n],a[1]); for(int i=1;i<=n/2;i++)b[++m]=sol(a[i*2],a[i*2+1]); } else { for(int i=1;i<=n/2;i++)b[i]=sol(a[i*2-1],a[i*2]); n>>=1; } for(int i=1;i<=n;i++)a[i]=b[i]; } if(a[1]==0)cout<<'R'; if(a[1]==1)cout<<'P'; if(a[1]==2)cout<<'S'; return 0; }
#pragma GCC target ("avx2") #pragma GCC optimize ("unroll-loops") #pragma GCC optimize ("O3") #include "bits/stdc++.h" #include <unordered_set> #include <unordered_map> #include <random> using namespace std; typedef long long ll; const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for(int (i)=0; (i)<(n); (i)++) const int dy[4]={ -1,0,1,0 }; const int dx[4]={ 0,-1,0,1 }; int N; ll X; ll A[51]; unordered_map<ll,ll> dp[51]; ll solve(int n, ll k){ if(n == -1) return (k == 0); if(abs(k) >= A[n+1]) return 0; if(dp[n].count(k)) return dp[n][k]; ll ret = 0; ll tmp = (k%A[n]+A[n])%A[n]; if(abs(k-tmp)/A[n] < A[n+1]/A[n]) ret += solve(n-1, tmp); tmp -= A[n]; if(abs(k-tmp)/A[n] < A[n+1]/A[n]) ret += solve(n-1, tmp); return dp[n][k] = ret; } signed main(){ cin >> N >> X; rep(i, N) cin >> A[i]; A[N] = 1e18L; cout << solve(N-1, -X) << 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; } ll n; ll gcd(ll a,ll b){ if(!b) return a; return gcd(b,a%b); } int main(){ n=read(); ll ans=1; for(ll i=2;i<=n;++i) ans=ans*i/gcd(ans,i); printf("%lld\n",ans+1); return 0; }
#include<iostream> using namespace std; int main(){ int n; cin >> n; cout << n - 1; return 0; }
#include <iostream> using namespace std; int h, w, a, b; int room[18][18]; int cnt; void putA(int n, int pos) { if (n > a) { cnt++; return; } for (; pos < h * w; pos++) { int r = (pos / w) + 1; int c = (pos % w) + 1; if (room[r][c] == 0) { room[r][c] = n; if (room[r][c + 1] == 0) { room[r][c + 1] = n; putA(n + 1, pos + 1); room[r][c + 1] = 0; } if (room[r + 1][c] == 0) { room[r + 1][c] = n; putA(n + 1, pos + 1); room[r + 1][c] = 0; } room[r][c] = 0; } } } int main() { cin >> h >> w >> a >> b; for (int r = 0; r < h + 2; r++) for (int c = 0; c < w + 2; c++) room[r][c] = ((r >= 1) && (r <= h) && (c >= 1) && (c <= w)) ? 0 : -1; cnt = 0; putA(1, 0); cout << cnt; }
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <cstdio> #include <algorithm> #include <vector> using namespace std; constexpr int MOD = 998244353; constexpr int prob = 665496236; long long dp[5001]; int pow(long long x, int n){ long long ret = 1; while (n > 0) { if (n & 1) ret *= x, ret %= MOD; x *= x, x %= MOD; n >>= 1; } return ret; } int main(){ int H, W, K; scanf("%d%d%d", &H, &W, &K); vector<vector<pair<int, char>>> X(H); for(int i = 0; i < H; ++i) X[i].reserve(W); for(int i = 0; i < K; ++i){ int x, y; char c; scanf("%d%d%c%c", &x, &y, &c, &c); --x, --y; X[x].emplace_back(y, c); } dp[0] = 1; for(int i = 0; i < H; ++i){ sort(X[i].begin(), X[i].end()); auto itr = X[i].begin(); for (int j = 0; j < W; ++j){ dp[j] %= MOD; if(itr != X[i].end() and itr->first == j){ char c = itr->second; if(c != 'D') dp[j + 1] += dp[j]; if((i != H - 1) & c == 'R') dp[j] = 0; ++itr; } else{ dp[j + 1] += dp[j] * prob; if(i != H - 1) dp[j] *= prob; } } } long long ans = dp[W - 1]; ans *= pow(3, H * W - K); printf("%d", ans % MOD); return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstring> #include <iostream> #include <sstream> #include <numeric> #include <map> #include <set> #include <queue> #include <vector> using namespace std; int64_t solve(int64_t N, std::vector<int64_t> a, std::vector<std::string> c) { int t[128] = {}; t['R'] = 0; t['G'] = 1; t['B'] = 2; vector<vector<int64_t>> v(3); for (int i = 0; i < 2 * N; i++) { v[t[c[i][0]]].emplace_back(a[i]); } if (v[0].size() % 2 == 0) { swap(v[0], v[1]); } if (v[1].size() % 2 == 0) { swap(v[1], v[2]); } if (v[0].size() % 2 == 0) { return 0; } for (int i = 0; i < 3; ++i) { sort(v[i].begin(), v[i].end()); } auto get_min = [&](const vector<int64_t>& a, const vector<int64_t>& b) { int64_t ans = 1LL << 60; int i = 0; for (auto x : a) { while (i < b.size() - 1 && abs(x - b[i]) >= abs(x - b[i + 1])) { ++i; } ans = min(ans, abs(x - b[i])); } return ans; }; int64_t ans = get_min(v[0], v[1]); if (!v[2].empty()) { ans = min(ans, get_min(v[0], v[2]) + get_min(v[1], v[2])); } return ans; } int main() { int64_t N; std::cin >> N; std::vector<int64_t> a(2*N); std::vector<std::string> c(2*N); for (int i = 0; i < 2*N; i++) { std::cin >> a[i] >> c[i]; } cout << solve(N, std::move(a), std::move(c)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define mp make_pair #define pb push_back #define fi first #define se second #define sz(x) int(x.size()) const int mod = 1e9 + 7; const int inf = 2e9 + 5; const ll linf = 9e18 + 5; const ll ll0 = 0 * 1ll; ll n, s, k; ll gcd (ll a, ll b) { if (b == 0) { return a; } return gcd(b, a % b); } ll euler (ll n) { ll d = 2; ll ans = n; while (d * d <= n) { if (n % d == 0) { ans /= d; ans *= d - 1; } while (n % d == 0) { n /= d; } d++; } if (n > 1) { ans /= n; ans *= n - 1; } return ans; } ll mull(ll a, ll b) { return (a * b) % n; } ll binpow (ll a, ll b) { if (b == 0) { return 1; } if (b % 2) { return mull(a, binpow(a, b - 1)); } ll half = binpow(a, b / 2); return mull(half, half); } void init () { } void input () { cin >> n >> s >> k; s = n - s; } void solve() { ll g = gcd(n, k); if (s % g) { cout << -1 << endl; return; } n /= g; s /= g; k /= g; cout << mull(s, binpow(k, euler(n) - 1)) << endl; } void output() { } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int number_of_testcases = 1; cin >> number_of_testcases; while (number_of_testcases--) { init(); input(); solve(); output(); } return 0; }
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; const ll INF = 100000000000000; int main() { int n, m; cin >> n >> m; ll w[10]; for(int i = 0; i < n; i++) cin >> w[i]; ll l[100005], v[100005]; for(int i = 0; i < m; i++) cin >> l[i] >> v[i]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(w[i] > v[j]){ cout << -1 << endl; return 0; } } } ll d[500]{0}; for(int i = 0; i < (1 << n); i++){ ll s = 0; for(int j = 0; j < n; j++){ if((i >> j) & 1) s += w[j]; } for(int j = 0; j < m; j++){ if(s > v[j]) d[i] = max(d[i], l[j]); } } int p[10]; ll ans = INF; for(int i = 0; i < n; i++) p[i] = i; do{ ll e[10]{0}; for(int i = 0; i < n; i++){ int u = p[i]; int b = (1 << u); for(int j = i - 1; j >= 0; j--){ int v = p[j]; b += (1 << v); e[u] = max(e[u], e[v] + d[b]); } } ans = min(ans, e[p[n - 1]]); }while(next_permutation(p, p + n)); cout << ans << endl; }
#include <bits/stdc++.h> #include <unistd.h> #define rep(i,j,n) for(i=j;i<n;i++) #define repi(i,j,n) for(i=j;i>n;i--) #define pie 3.141592653589793238 #define ll long long #define ld long double #define vll vector<ll> #define pll pair<ll,ll> #define vpll vector<pll> #define pb push_back #define F first #define S second #define endl '\n' #define UQ(x) (x).resize(distance((x).begin(),unique(x.begin(),x.end()))) using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rnd64(chrono::high_resolution_clock::now().time_since_epoch().count()); ll power(ll a,ll m,ll mod){ ll ans = 1; while(m){ if(m%2) ans *= a, ans %= (mod); a = (a*a) % mod; m >>= 1; } return ans; } struct custom_hash { // Credits: https://codeforces.com/blog/entry/62393 static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<int64_t, int64_t> Y) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM); } }; const long long inf = 1e18; const long long M = 1000000007; void solve(){ ll i,j,_; ll n, m; ll mx = LLONG_MAX; cin >> n >> m; ll w[n]; rep(i,0,n) cin >> w[i]; sort(w,w+n); if(n > 1) swap(w[0], w[n - 2]); ll tmp[n]; i = 0; ll l = 0, r = n - 1; // while(i < n) { // if(i%2) tmp[i] = w[r], r--; // else tmp[i] = w[l],l++; // i++; // } // rep(i,0,n) w[i] = tmp[i]; ll l1[m],r1[m]; _ = 10000; rep(i,0,m) cin >> l1[i] >> r1[i]; do { ll dp[n][n]; rep(i,0,n)rep(j,0,n) dp[i][j] = 0; ll bl = 0,t; rep(t,0,m) { l = l1[t], r = r1[t]; rep(i,0,n) { ll now = w[i]; // cout << now << ' ' << r << endl; if(now > r) { cout << -1 << endl; return; // bl = 1; } rep(j,i + 1,n) { now += w[j]; if(now > r) { dp[i][j] = max(dp[i][j], l); break; } } } if(bl) break; } if(bl) continue; // rep(i,0,n) { // rep(j,0,n) { // cout << dp[i][j] << ' '; // } // cout << endl; // } ll k; rep(i,1,n) { rep(j,0,n) { r = j + i; if(r >= n) break; rep(k,j,r+1) { dp[j][r] = max(dp[j][r], dp[j][k] + dp[k][r]); } } } mx = min(mx, dp[0][n-1]); // if(mx == 3802) cout << _ << endl; // cout << dp[0][n-1] << endl; // if(dp[0][n-1] == 3802){ // for(auto i : w) cout << i << ' '; // cout << endl; // } }while ( std::next_permutation(w + 1,w + n - 1)); // cout << _ << endl; if(mx == LLONG_MAX) mx = -1; cout << mx << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll T=1; // cin>>T; while(T--){ solve(); } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ull unsigned long long #define ld long double #define F first #define S second #define eb emplace_back #define pb push_back #define mp make_pair #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) using namespace __gnu_pbds; using namespace std; template <typename T> using oset = tree <pair <T, T>, null_type, less <pair <T, T>>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> void ckmin(T& a, const T b) {a = min(a, b);} template <typename T> void ckmax(T& a, const T b) {a = max(a, b);} template <typename T> vector <T> sieve(T n) { vector <T> f(n+1, 1), res; for (T i = 2; i <= n; ++i) for (T j = i; j <= n; j += i) ++f[j]; for (T i = 2; i <= n; ++i) if (f[i] == 2) res.eb(i); return res; } template <typename T> T power(T a, T b, const T c) { a %= c; T res = 1; while (b) { if (b & 1) res = res * a % c; b >>= 1; a = a * a % c; } return res; } template <typename T> void print(vector <T> v) { for (T i : v) cout << i << " "; cout << '\n'; } template <typename T> void print(vector <vector <T>>& v) { for (vector <T>& vv : v) { for (T& i : vv) cout << i << " "; cout << '\n'; } } template <typename T> void read(vector <T>& v) {for (T& i : v) cin >> i;} template <typename T> void read(T&& t) {cin >> t;} template <typename T, typename... Args> void read(T&& t, Args&&... args) { cin >> t; read(forward<Args>(args)...); } template <typename T> void print(T&& t) {cout << t << '\n';} template <typename T, typename... Args> void print(T&& t, Args&&... args) { cout << t << " "; print(forward<Args>(args)...); } void usaco(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(name.size()) { freopen((name+".in").c_str(), "r", stdin); freopen((name+".txt").c_str(), "w", stdout); } } int n, edge, node; const int mxn = 4e5+1; vector <int> adj[mxn]; bool vis[mxn]; void dfs(int u) { vis[u] = 1; ++node; for (int v : adj[u]) { ++edge; if (!vis[v]) dfs(v); } } inline void solve() { cin >> n; for (int i = 0; i < n; ++i) { int u, v; cin >> u >> v; adj[u].emplace_back(v); adj[v].emplace_back(u); } long long ans = 0; for (int i = 1; i < mxn; ++i) { if (vis[i] || adj[i].empty()) continue; edge = node = 0; dfs(i); edge >>= 1; ans += min(edge, node); } cout << ans << '\n'; } int32_t main() { usaco(); int t = 1; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define P pair<ll, ll> #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second ll n; vector<vector<ll>> G(400001); vector<bool> loop_dupl(400001, false); vector<bool> check(400001, false); ll ans = 0; bool dfs(ll color, ll parent){ //cout << "color : " << color << endl; if(check[color]) return true; check[color] = true; ans++; bool ret = loop_dupl[color]; for(ll link : G[color]) if(link != parent) ret |= dfs(link, color); //cout << ret << endl; return ret; } int main(){ cin >> n; ll a, b, c; set<P> s; rep(i, n){ cin >> a >> b; if(a > b){ c = a; a = b; b = c; } if(s.find(make_pair(a, b)) == s.end()){ s.insert(make_pair(a, b)); G[a].pb(b); G[b].pb(a); }else{ loop_dupl[a] = true; loop_dupl[b] = true; } } for(ll i=1; i<=400000; i++){ if(check[i]) continue; //cout << "aaa" << endl; ans -= !dfs(i, 0); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; vector<int> b[205]; int n,a[205],c[205],d[205]; bool mk[205]; void dfs(int x,int y,int u,int v){ if(u%200==v%200&&x!=0&&y!=0){ sort(c+1,c+x+1); sort(d+1,d+y+1); printf("Yes\n"); printf("%d ",x); for(int i=1;i<=x;i++)printf("%d ",c[i]); printf("\n"); printf("%d ",y); for(int i=1;i<=y;i++)printf("%d ",d[i]); printf("\n"); exit(0); } // for(int i=0;i<200;i++){ // for(int j=0;j<b[i].size();j++){ // if(!mk[b[i][j]]){ // mk[b[i][j]]=1; // c[x+1]=b[i][j]; // dfs(x+1,y,u+i,v); // for(int p=0;p<200;p++){ // for(int q=0;q<b[p].size();q++){ // if(!mk[b[p][q]]){ // mk[b[p][q]]=1; // d[y+1]=b[p][q]; // dfs(x+1,y+1,u+i,v+p); // mk[b[p][q]]=0; // } // } // } // mk[b[i][j]]=0; // } // } // } for(int p=0;p<200;p++){ for(int q=0;q<b[p].size();q++){ if(!mk[b[p][q]]){ mk[b[p][q]]=1; d[y+1]=b[p][q]; dfs(x,y+1,u,v+p); for(int i=0;i<200;i++){ for(int j=0;j<b[i].size();j++){ if(!mk[b[i][j]]){ mk[b[i][j]]=1; c[x+1]=b[i][j]; dfs(x+1,y+1,u+i,v+p); mk[b[i][j]]=0; } } } mk[b[p][q]]=0; } } } } bool check(){ for(int i=0;i<200;i++) if(b[i].size()>=2){ printf("Yes\n"); printf("1 %d\n",b[i][0]); printf("1 %d\n",b[i][1]); exit(0); } return false; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[a[i]%200].push_back(i); } if(!check()){ for(int i=0;i<200;i++){ for(int j=0;j<b[i].size();j++){ if(!mk[b[i][j]]){ mk[b[i][j]]=1; c[1]=b[i][j]; dfs(1,0,i,0); mk[b[i][j]]=0; } } } } printf("No"); return 0; }
#include <bits/stdc++.h> using namespace std; #define fo(i, x, y) for (int i = (x); i <= (y); ++i) #define fd(i, x, y) for (int i = (x); i >= (y); --i) typedef long long ll; const int maxn = 1e5 + 5; const double inf = 1e18; int n; ll a[maxn]; int getint() { char ch; int res = 0, p; while (!isdigit(ch = getchar()) && (ch ^ '-')); p = ch == '-'? ch = getchar(), -1 : 1; while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar(); return res * p; } int main() { //freopen("t.in", "r", stdin); n = getint(); ll sum = 0; fo(i, 1, n) scanf("%lld", &a[i]), sum += a[i]; sort(a + 1, a + 1 + n); double ans = inf; ll pre = 0; fo(i, 1, n) { pre += a[i]; double x = a[i] / 2.0; ans = min(ans, x + 1.0 * sum / n - 1.0 * (pre + a[i] * (n - i)) / n); } printf("%.8f\n", ans); return 0; }
#include<iostream> #include<string> #include<cmath> #include<algorithm> #include<iomanip> using namespace std; int main() { int n; int a[1001]; int ans = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > 10) { ans += a[i] - 10; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; int main(){ int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++){ cin >> a[i]; } int ans = 0; for(int i = 0; i < n; i++){ int x = a[i]; for(int j = i; j < n; j++){ x = min(x, a[j]); ans = max(ans, (j - i + 1) * x); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define F first #define S second #define FOR(i, a, b) for(int i = a; i <= b; i++) #define RFOR(i, a, b) for(int i = a; i >= b; i--) #define all(v) ((v).begin()), ((v).end()) #define endl '\n' using namespace std; using ll = long long; using ull = unsigned long long; using Graph = vector<vector<int>>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; #ifdef __SIZEOF_INT128__ using Bigint = __int128; #endif template <class T, class U> void max_self(T& t, const U& u) { if (t > u) t = u; } template <class T, class U> void min_self(T& t, const U& u) { if (t < u) t = u; } template <typename T> void print(const T& cont) { cout << "{"; int idx = 0; for (auto& itr : cont) { if (idx++) cout << ","; cout << itr; } cout << "}\n"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; FOR (i, 0, v.size() - 1) { if (i) os << ","; os << v[i]; } os << "}"; return os; } string solve(ll& n) { if (n % 4 == 0) return "Even\n"; if (n % 2 == 0) return "Same\n"; return "Odd\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { ll n; cin >> n; cout << solve(n); } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll t,n; cin>>t; while(t--) { cin>>n; if(n%2==1) cout<<"Odd"<<endl; else { ll r=0 ; while(n%2==0) { ++r; n=n/2; } ++r; if(r>=3) cout<<"Even"<<endl; else cout<<"Same"<<endl; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = int(2e5) + 10; ll a[N]; const ll mod = ll(998244353LL); // ll sum(int m, int n){ // ll mx = -1; // ll mn = -1; // for(int j = 0 ; j < n ; j++){ // int e = 1 << j; // if(m & e){ // if(mx == -1){ // mx = a[j]; // mn = a[j]; // }else{ // mx = max(mx, a[j]); // mn = min(mn, a[j]); // } // } // } // return (mx * mn) % mod; // } int main(){ int n; scanf("%d", &n); for(int i = 0 ; i < n ; i++){ scanf("%lld", &a[i]); } // ll tmp = 0LL; // for(int m = 1 ; m < (1 << n) ; m++){ // tmp = (tmp + sum(m, n)) % mod; // } // printf("tmp = %lld\n", tmp); sort(a, a + n); ll sum = 0LL; ll ans = 0LL; for(int i = 0 ; i < n ; i++){ // printf("%lld adds %lld\n", a[i], ((a[i] * sum) % mod + a[i] * a[i])); ans = (ans + ((a[i] * sum) % mod + a[i] * a[i]) % mod) % mod; sum = (sum * 2LL + a[i]) % mod; } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair <int, int> #define vi vector<int> #define FOR(i, a, b) for (int i=a; i<b; i++) #define FORd(i, a, b) for (int i=a-1; i>=b; i--) #define F0Rd(i, a) for (int i=a-1; i>=0; i--) #define F0R(i, a) for (int i=0; i<a; i++) #define f first #define s second #define lb lower_bound #define ub upper_bound const int MOD = 998244353; long double PI = 4*atan(1); /*std::cout << std::fixed << std::setprecision(2); ll po (ll b, ll p) return !p?1:po(b*b%MOD,p/2)*(p&1?b:1)%MOD; ll inv (ll b) { return po(b,MOD-2); }*/ ll n,a[200001],dp[200001],ans,cur; void calc() { dp[n-1] = 0LL; if(n==1) return; dp[n-2] = a[n-1]; if(n==2) return; F0Rd(i,n-2) { dp[i] = a[i+1]+ (2LL*dp[i+1]); dp[i] %= MOD; } //F0R(i,n) cout << dp[i] << " "; } void solve() { cin >> n; F0R(i,n) cin >> a[i]; sort(a,a+n); calc(); F0R(i,n) { cur = (a[i]*a[i])%MOD; ans = (ans+cur)%MOD; cur = (a[i]*dp[i])%MOD; ans = (ans+cur)%MOD; //cout << ans << " "; } cout << ans; } int main() { ios::sync_with_stdio(0), cin.tie(0); int test_cases = 1; //cin >> test_cases; F0R(i,test_cases) { solve(); //init(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll MOD=1e9+7; const ll MAX=1e5+2; void solve(){ ll n;cin>>n; ll a[n],b[n],c[n]; for(auto &x:a)cin>>x; for(auto &x:b)cin>>x; for(auto &x:c)cin>>x; unordered_map<ll,ll>mc; for(auto &x:c)mc[x]++; unordered_map<ll,ll>mb; for(int i=0;i<n;i++){ mb[b[i]]+=mc[i+1]; } ll ans=0; for(auto &x:a){ ans+=mb[x]; } cout<<ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll t;t=1; // cin>>t; // cin.ignore(); while(t--){ solve(); } return 0; }
#include<iostream> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<algorithm> #include<utility> using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; vector<vector<int>> R; vector<int> used; int rec(int cur){ int res = 1; used[cur] = true; for(int nex : R[cur]){ if(used[nex]) continue; res += rec(nex); } return res; } signed main(){ cout<<fixed<<setprecision(10); int N; long double ans = 0; cin>>N; R.resize(N); for(int i = 0; i < N; i++){ string s; cin>>s; for(int j = 0; j < s.size(); j++){ if(s[j] == '1') { R[j].push_back(i); } } } for(int i = 0; i < N; i++){ used.clear(); used.resize(N); ans += 1.0 / rec(i); } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define int long long #define pb push_back #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define all(v) v.begin(),v.end() #define F first #define S second #define pi pair<int,int> #define vi vector<int> #define mii map<int,int> #define vp vector<pair< int,int> > #define mpi map<pair<int,int> ,int> #define endl "\n" #define fill(A,x) memset(A,x,sizeof A); void solve() { int n; cin>>n; vi pos(n+5),ar(n+5); vi ans; for(int i=1;i<=n;i++) { cin>>ar[i]; pos[ar[i]]=i; } if(n==2) { if(pos[1]==1)cout<<0<<endl; else {cout<<1<<endl;cout<<1;} cout<<endl; return; } for(int i=n;i>0;i--) { if(i>n||pos[i]==i) continue; int c=pos[i]; if(c%2!=(ans.size())%2) { while(pos[i]!=i) { int x=ar[pos[i]+1]; ar[pos[i]]=x;ar[pos[i]+1]=i; ans.pb(pos[i]); pos[i]++;pos[x]--; } } else { if(c%2==0) { ans.pb(1); ans.pb(2); ans.pb(1); pos[ar[1]]+=2;pos[ar[3]]-=2; swap(ar[1],ar[3]); i++; } else { ans.pb(2); ans.pb(1); ans.pb(2); pos[ar[1]]+=2;pos[ar[3]]-=2; swap(ar[1],ar[3]); i++; i++; } } } int x=ans.size(); cout<<x<<endl; for(auto it:ans)cout<<it<<" "; cout<<endl; } signed main() { boost int test=0,t; cin >> t ; //t=1; while(test++ <t){ // cout<<"Case #"<<test<<": "; solve();} }
//Duodecim Ferra #include<bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define rep(i,x,y) for(ll i=x;i<y;i++) #define rrep(i,x,y) for(ll i=x;i>=y;i--) #define nvarep(n,a) ll n;cin>>n;vector<ll>a(n);rep(i,0,n)cin>>a[i] #define vecrep(n,a,type) vector<type>a(n);rep(i,0,n)cin>>a[i] #define lcm(a,b) (a/__gcd(a, b)*b) #define range(a) (a).begin(),(a).end() #define nnn "\n" #define spa " " using P = pair<ll,ll>; using graph = vector<vector<ll>>; const int inf = 2147483647;//2*10^9 const ll INF = 9223372036854775807;//9*10^18 const ll mod = 9223372036854775800; //cout<<fixed<<setprecision(15)<<x<<nnn; signed main (){ int l;cin>>l; vector<int>a={1,2,3,2,2,5,2,3,7,2,2,2,3,3,2,5,11}; sort(range(a),greater<int>()); int ans=1; rep(i,0,11){ l--; ans*=l; rep(r,0,a.size()){ if(ans%a[r]==0){ ans/=a[r]; a[r]=1; } } } cout<<ans<<nnn; return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template<class S> inline int arrEraseVal_L(S val1, int &sz, S a[]){ int i; int n = sz; sz = 0; for(i=(0);i<(n);i++){ if(a[i]!=val1){ a[sz++] = a[i]; } } return n - sz; } int N; int X; int A[100000]; int main(){ rd(N); rd(X); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } arrEraseVal_L(X, N, A); { int e98WHCEY; if(N==0){ wt_L('\n'); } else{ for(e98WHCEY=(0);e98WHCEY<(N-1);e98WHCEY++){ wt_L(A[e98WHCEY]); wt_L(' '); } wt_L(A[e98WHCEY]); wt_L('\n'); } } return 0; } // cLay version 20210103-1 [bug fixed 2] // --- original code --- // int N, X, A[1d5]; // { // rd(N,X,A(N)); // arrEraseVal(X, N, A); // wt(A(N)); // }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (n); ++i) #define rep1(i, n) for(int i = 1; i < (n); ++i) using namespace std; using ll = long long; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n, x; cin >> n >> x; int a = 0; rep(i, n){ int v, p; cin >> v >> p; a += v * p ; if(x*100 < a){ cout << i+1 << endl; return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<ll, ll>; #define fi first #define se second #define pb push_back #define pf push_front #define mp make_pair const ll N = 2e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 2e9; const ll dr4[] = {1, -1, 0, 0}; const ll dc4[] = {0, 0, 1, -1}; const ll dr8[] = {-1, -1, 0, 1, 1, 1, 0, -1}; const ll dc8[] = {0, 1, 1, 1, 0, -1, -1, -1}; ll n, k; void solve() { cin >> n >> k; const auto G1 = [&](ll x) { ll freq[10]; memset(freq, 0, sizeof(freq)); while(x) { freq[x % 10]++; x /= 10; } ll ret = 0; for(ll i = 9; i >= 0; i--) { for(ll j = 1; j <= freq[i]; j++) { ret = ret * 10 + i; } } return ret; }; const auto G2 = [&](ll x) { ll freq[10]; memset(freq, 0, sizeof(freq)); while(x) { freq[x % 10]++; x /= 10; } ll ret = 0; for(ll i = 1; i <= 9; i++) { for(ll j = 1; j <= freq[i]; j++) { ret = ret * 10 + i; } } return ret; }; ll ans = n; for(ll i = 0; i < k; i++) { ll g1 = G1(ans); ll g2 = G2(ans); ans = g1 - g2; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); // cin.tie(0); cout.tie(0); ll T = 1; // cin >> T; for(ll tc = 1; tc <= T; tc++) { solve(); } }
#ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; using PIL = pair<int,ll>; using PLI = pair<ll,int>; using PLL = pair<ll,ll>; using Graph = vector<vector<int>>; using Cost_Graph = vector<vector<PIL>>; template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;} template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;} #define rep(i,n) for(int i=0;i<int(n);i++) #define roundup(a,b) (a+b-1)/b #define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;} #define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;} #define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;} const ll INF = 1LL << 60; const ll MOD = 1000000007LL; const double pi = 3.14159265358979; ll func(int x){ string s = to_string(x); string t = to_string(x); sort(s.begin(), s.end()); sort(t.begin(), t.end()); reverse(t.begin(), t.end()); ll big = stoll(t); ll small = stoll(s); return (big - small); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); ll n,k; cin >> n >> k; for (int i = 0; i < k; i++){ n = func(n); } cout << n << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 1e5 + 5; inline int read() { int w = 0, f = 1; char ch = getchar(); while (ch < '0' or ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' and ch <= '9') w = w * 10 + ch - '0', ch = getchar(); return w * f; } int L[maxn], R[maxn], A[maxn], N, M, K; bool check(int limit) { for (int i = 1; i <= K; i++) L[i] = ((A[i] * M - limit + N - 1) / N), R[i] = ((A[i] * M + limit) / N); for (int i = 1; i <= K; i++) L[i] = max(L[i], 0ll); for (int i = 1; i <= K; i++) if (L[i] > R[i]) return 0; int lsum = 0, rsum = 0; for (int i = 1; i <= K; i++) lsum += L[i], rsum += R[i]; if (lsum <= M and rsum >= M) return 1; return 0; } signed main() { scanf("%lld%lld%lld", &K, &N, &M); for (int i = 1; i <= K; i++) scanf("%lld", &A[i]); int l = 0, r = M * N; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) r = mid - 1; else l = mid + 1; } check(l); int lsum = 0; for (int i = 1; i <= K; i++) lsum += L[i]; int res = M - lsum; for (int i = 1; i <= K; i++) { printf("%lld ", min(L[i] + res, R[i])); res -= min(L[i] + res, R[i]) - L[i]; } return 0; }
#include<bits/stdc++.h> using namespace std; using ll=long long; using P=pair<ll,ll>; ll N,M,Q; map<ll,ll>mp; map<ll,ll>mp1; P dat[2][800005]; ll n; void init(ll n_){ n=1; while(n<=n_)n*=2; for(ll i=0;i<2*n-1;i++){ dat[0][i]=P(0,0); dat[1][i]=P(0,0); } } void update(ll j,ll k,ll a){ ll m=mp1.at(k); k+=n-1; dat[j][k].first+=a;dat[j][k].second+=a*m; while(0<k){ k=(k-1)/2; dat[j][k].first=(dat[j][2*k+1].first+dat[j][2*k+2].first); dat[j][k].second=(dat[j][2*k+1].second+dat[j][2*k+2].second); } } P query(ll j,ll a,ll b,ll k,ll l,ll r){ if(a<=l&&r<=b)return dat[j][k]; else if(b<=l||r<=a)return P(0,0); else{ P val=query(j,a,b,2*k+1,l,(l+r)/2); P var=query(j,a,b,2*k+2,(l+r)/2,r); P p(val.first+var.first,val.second+var.second); return p; } } int main(){ cin>>N>>M>>Q; set<ll>S;S.insert(0); vector<ll>T(Q);vector<ll>X(Q);vector<ll>Y(Q); for(ll q=0;q<Q;q++){ cin>>T[q];cin>>X[q];X[q]--;cin>>Y[q];S.insert(Y[q]); }ll I=0; for(auto x:S){mp[x]=I;I++;} for(auto p:mp){mp1[p.second]=p.first;}vector<ll>A(N);vector<ll>B(M);ll ans=0; init(I);update(0,0,N);update(1,0,M); for(ll q=0;q<Q;q++){ if(T[q]==1){ ans-=query(1,0,mp.at(A[X[q]])+1,0,0,n).first*A[X[q]]; if(A[X[q]]<Y[q]){ ans-=query(1,mp.at(A[X[q]])+1,mp.at(Y[q])+1,0,0,n).second; }else if(Y[q]<A[X[q]]){ ans+=query(1,mp.at(Y[q])+1,mp.at(A[X[q]])+1,0,0,n).second; }else{} ans+=query(1,0,mp.at(Y[q])+1,0,0,n).first*Y[q];update(0,mp.at(A[X[q]]),-1); update(0,mp.at(Y[q]),1);A[X[q]]=Y[q]; }else{ ans-=query(0,0,mp.at(B[X[q]])+1,0,0,n).first*B[X[q]]; if(B[X[q]]<Y[q]){ ans-=query(0,mp.at(B[X[q]])+1,mp.at(Y[q])+1,0,0,n).second; }else if(Y[q]<B[X[q]]){ ans+=query(0,mp.at(Y[q])+1,mp.at(B[X[q]])+1,0,0,n).second; }else{} ans+=query(0,0,mp.at(Y[q])+1,0,0,n).first*Y[q];update(1,mp.at(B[X[q]]),-1); update(1,mp.at(Y[q]),1);B[X[q]]=Y[q]; }cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; if(a == b) cout << c << endl; else if(b == c) cout << a << endl; else if(c == a) cout << b << endl; else cout << 0 << endl; }
// /* // power code taken from geeks for geeks // */ // cout << setprecision (2) << fixed << 1.2; #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; // THINGS TO REMEMBER // ENDL is slow, '\n' is fast // Clear everything (including graphs) between test cases // use anti-anti-hash: https://codeforces.com/blog/entry/62393 // pb-ds: https://codeforces.com/blog/entry/11080 // check when to use LLONG_MAX/LLONG_MIN vs INT_MAX or INT_MIN // You frequently suffer from confirmation bias - you trust your initial solution and miss simple things. // When you hit a roadblock, remember to rethink the solution ground up, not just try hacky fixes #define PLL pair<ll, ll> #define en endl const ll INF = 1e18 + 5; const ll MAX_N = 1e5 + 5; const ll MODD = 1e9 + 7; const ll BITS = 10; ll max(ll a,ll b){return a>b?a:b;} ll min(ll a,ll b){return a<b?a:b;} double max(double a,double b){return a>b?a:b;} double min(double a,double b){return a<b?a:b;} ll modd(ll a){if(a<0LL) return a*-1LL;return a;} double modd(double a){if(a<0.0) return a*-1.0;return a;} ll modulo(ll a){return (a + MODD) % MODD; } void swap(ll &a, ll &b) { ll t = a; a = b; b = t; } ll power(ll k, ll p){ if (p == 0) return 1LL; ll P = power(k, p/2LL); P = (P * P) % MODD; if (p % 2 == 1) P = (P * k) % MODD; return P; } int iii = 0;void _debug() {cout << "here " << (iii++) << endl;} void solve() { ll a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; cout << min({a1, a2, a3, a4}) << endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // init(); ll t=1; // cin >> t; for(ll i = 0; i < t; i++) { // cout << "---------------case " << i + 1 << endl; // cout << "Case #" << i + 1 << ": "; // cout << endl; solve(); } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <numeric> #include <cmath> #include <queue> #include <map> #include <iomanip> #include <time.h> using namespace std; int64_t mod = 998244353 ; int64_t min(int64_t a,int64_t b) { if (a > b) { return b ; }else { return a ; } } vector<int> getprime(int n){ vector<int> p(0) ; vector<bool> table(n+1,0) ; for (int i = 2; i <= n; i++) { if (table.at(i) == 0) { p.push_back(i) ; for (size_t j = 2; j < n/i ; j++) { table.at(i*j) = 1 ; } } } return p ; } int getnum(vector<int> &array, vector<char> &c, char a){ for (int i = 0; i < c.size(); i++) { if (a == c.at(i)) { return array.at(i) ; } } } int main(){ string s1,s2,s3 ; cin >> s1 >> s2 >> s3 ; vector<int> array(10,0) ; for (int i = 0; i < 10; i++) { array.at(i) = i ; } vector<char> c(0) ; for (int i = 0; i < s1.size(); i++) { bool ok = 1 ; for (int j = 0; j < c.size(); j++) { if (s1.at(i) == c.at(j)) { ok = 0 ; break ; } } if (ok) { c.push_back(s1.at(i)) ; } } for (int i = 0; i < s2.size(); i++) { bool ok = 1 ; for (int j = 0; j < c.size(); j++) { if (s2.at(i) == c.at(j)) { ok = 0 ; break ; } } if (ok) { c.push_back(s2.at(i)) ; } } for (int i = 0; i < s3.size(); i++) { bool ok = 1 ; for (int j = 0; j < c.size(); j++) { if (s3.at(i) == c.at(j)) { ok = 0 ; break ; } } if (ok) { c.push_back(s3.at(i)) ; } } /* for (int i = 0; i < c.size(); i++) { cout << c.at(i) ; } */ if (c.size() > 10) { cout << "UNSOLVABLE" << endl ; return 0 ; } do { bool ng = 0 ; int64_t n1 = 0 ; int64_t n2 = 0 ; int64_t n3 = 0 ; for (int i = 0; i < s1.size(); i++) { int next = getnum(array,c,s1.at(i)) ; if (i == 0 && next == 0) { ng = 1 ; break ; }else { n1 = n1*10 + next ; } } for (int i = 0; i < s2.size(); i++) { int next = getnum(array,c,s2.at(i)) ; if (i == 0 && next == 0) { ng = 1 ; break ; }else { n2 = n2*10 + next ; } } for (int i = 0; i < s3.size(); i++) { int next = getnum(array,c,s3.at(i)) ; if (i == 0 && next == 0) { ng = 1 ; break ; }else { n3 = n3*10 + next ; } } if (ng) { continue ; } if (n1 + n2 == n3) { cout << n1 << endl ; cout << n2 << endl ; cout << n3 << endl ; return 0 ; } }while ( next_permutation( array.begin(), array.end() ) ) ; cout << "UNSOLVABLE" << endl ; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++) #define rrep(i, a, b) for (int i = (int)(b) - 1; (i) >= (int)(a); (i)--) #define all(v) v.begin(), v.end() typedef long long ll; template <class T> using V = vector<T>; template <class T> using VV = vector<V<T>>; /* 提出時これをコメントアウトする */ #define LOCAL true #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif int main() { ios::sync_with_stdio(false); cin.tie(nullptr); constexpr char endl = '\n'; string s[3]; rep(i,0,3) cin >> s[i]; map<char,int> v; rep(i,0,3) { rep(j,0,s[i].size()) { v[s[i][j]] = 0; } } string ss = ""; for (auto e: v) { ss.push_back(e.first); } V<bool> used(10); auto dfs = [&] (auto self, int k) -> bool { if (k == v.size()) { ll n[3] = {}; rep(i,0,3) { rep(j,0,s[i].size()) { if (j == 0 && v[s[i][j]] == 0) return false; n[i] *= 10; n[i] += v[s[i][j]]; } } return n[0] + n[1] == n[2]; } rep(i,0,10) { if (used[i]) continue; v[ss[k]] = i; used[i] = 1; if (self(self, k+1)) return true; used[i] = 0; } return false; }; bool result = dfs(dfs, 0); if (v.size() > 10) result = 0; if (!result) { cout << "UNSOLVABLE" << endl; } else { rep(i,0,3) { rep(j,0,s[i].size()) { cout << v[s[i][j]]; } cout << endl; } } return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#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 const int IM = 2e9; int dat[400004]; const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); dat[0] = -IM; ll kotae = -IM * ll(N); for (int i = N + 1; i > 1; i--) { dat[i] = getint(); kotae += dat[i]; } for (int i = 2; i < N + 2; i++) { int p = 2 + (dat[2] > dat[3]); int v = dat[i] - IM; while (v > dat[p]) { dat[p >> 1] = dat[p]; p <<= 1; p += dat[p] > dat[p + 1]; } dat[p >> 1] = v; int a = getint(); kotae += a; p = i; v = a - IM; while (v < dat[p >> 1]) dat[p] = dat[p >> 1], p >>= 1; dat[p] = v; kotae -= dat[1]; } co(kotae); Would you please return 0; }
#pragma GCC optimize("Ofast") #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; using ll = int64_t; using ld = long double; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vector<int>>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpll = vector<pll>; constexpr char newl = '\n'; constexpr double eps = 1e-10; #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define F0R(i,b) FOR(i,0,b) #define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--) #define RF0(i,b) RFO(i,0,b) #define show(x) cout << #x << " = " << x << '\n'; #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define sz(x) (int)(x).size() #define YesNo {cout<<"Yes";}else{cout<<"No";} #define YESNO {cout<<"YES";}else{cout<<"NO";} #define v(T) vector<T> #define vv(T) vector<vector<T>> template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) { return l.first < r.first; } template<class T> istream& operator>>(istream& i, v(T)& v) { F0R(j, sz(v)) i >> v[j]; return i; } template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) { return i >> p.first >> p.second; } template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) { return i >> get<0>(t) >> get<1>(t) >> get<2>(t); } template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) { o << "(" << v.first << "," << v.second << ")"; return o; } template<class T> ostream& operator<<(ostream& o, const vector<T>& v) { F0R(i, v.size()) { o << v[i] << ' '; } o << newl; return o; } template<class T> ostream& operator<<(ostream& o, const set<T>& v) { for (auto e : v) { o << e << ' '; } o << newl; return o; } template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) { for (auto& p : m) { o << p.first << ": " << p.second << newl; } o << newl; return o; } #if 1 ll gcd(ll a, ll b) { while (b) { a %= b; swap(b, a); } return a; } ll extGCD(ll a, ll b, pll& out) { ll l = a, r = b; ll la = 1, lb = 0, ra = 0, rb = 1; while (true) { // l / r = c ... d ll c = l / r, d = l - r * c; if (!d) { out = { ra,rb }; return r; } la = la - ra * c; lb = lb - rb * c; swap(la, ra); swap(lb, rb); l = r; r = d; } } ll mod(ll a, ll b) { a %= b; if (a < 0) a += b; return a; } ll solve() { int N, S, K; cin >> N >> S >> K; /* S + K * x = 0 mod N K * x = (N-S) mod N x = (N-S) / K mod N mod N での K の逆数 */ ll g = gcd(K, N); if (S % g) return -1; S /= g; K /= g; N /= g; pll out; extGCD(K, N, out); //show(out.first); //show(N); //show(mod(out.first,N)); return mod(out.first, N) * (N - S) % N; } // INSERT ABOVE HERE signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int T; cin >> T; F0R(i, T) { cout << solve() << newl; } } #endif
// #define LOCAL #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++) #define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--) #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define All(x) x.begin(), x.end() #define Range(x, i, j) x.begin() + i, x.begin() + j // #define M_PI 3.14159265358979323846 // CF #define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI)) #define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360) #define Find(set, element) set.find(element) != set.end() #define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank #define endl "\n" #define Case(x) printf("Case #%d: ", x); // gcj typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; typedef vector<PI> vpi; typedef vector<vector<PI>> vvpi; typedef vector<PLL> vpl; typedef vector<vector<PLL>> vvpl; typedef vector<char> vch; typedef vector<vector<char>> vvch; constexpr ll INF = 1001002003004005006ll; constexpr int n_max = 2e5+10; template<class T> inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; }; template<class T> inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; }; template<class T, class U> T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;}; // debug 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...); }; void LINE_OUT() { cout << "--------------" << endl; }; #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define LINE LINE_OUT(); #else #define debug(...) 71 #define LINE 71; #endif void print() { cout << endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }; template <class T> void print(vector<T> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }; template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec); } }; signed main() { ios::sync_with_stdio(false); cin.tie(0); int A, B, W; cin >> A >> B >> W; int ma = -INF, mi = INF; rep(i, 1, 1000005) { if (A*i <= W*1000 && W*1000 <= B*i) { chmin(mi, i); chmax(ma, i); } } if (mi == INF) { cout << "UNSATISFIABLE" << endl; } else { cout << mi << " " << ma << endl; } return 0; };
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int A,B; long long k; long long dp[50][50]; int main(void){ // freopen("D.in","r",stdin); // freopen("D.out","w",stdout); scanf("%d %d",&A,&B); cin>>k; dp[0][0]=1; for(int i=0;i<=A;i++){ for(int j=0;j<=B;j++){ if(i>0)dp[i][j]+=dp[i-1][j]; if(j>0)dp[i][j]+=dp[i][j-1]; } } while(A>=1&&B>=1){ if(k<=dp[A-1][B]){ A-=1; printf("a"); } else{ k-=dp[A-1][B]; B--; printf("b"); } } for(int i=1;i<=A;i++)printf("a"); for(int i=1;i<=B;i++)printf("b"); }
#include <bits/stdc++.h> #define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++) #define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++) #define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--) #define deb(x) std::cout << #x << " " << x << endl; #define debl(x) std::cout << #x << " " << x << " "; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; namespace std{ template<class Fun> class y_combinator_result{ Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){} template<class ...Args> decltype(auto) operator()(Args&&...args){ return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun && fun){ return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } }; template<typename T> bool u_max(T& a, T b){ if( a < b){ a = b; return true; } return false; } template<typename T> bool u_min(T& a, T b){ if( a > b){ a = b; return true; } return false; } struct edge{ int to, from; ll cost; edge(){ edge(0,0);} edge(int to_, ll cost_){ edge(to_, -1, cost_);} edge(int to_, int from_, ll cost_) : to(to_), from(from_), cost(cost_){} // edge(int to_, int from_) : to(to_), from(from_){}; }; template<typename... T> void read(T& ... a){ ((cin >> a),...); } void solve(){ int n, m; read(n, m); vector<vector<int>> e(n, vector<int>(n, 0)); REP(i, m){ int a, b; read(a, b); a--; b--; e[a][b] = e[b][a] = 1; } int state = 1; const int state_num = 1 << n; vector<bool> connected(state_num, true); auto state_contain = [](int s, int i) -> bool{ // cout << "test " << s << " " << i << endl; // cout << "-> " << ((1<<i) & s ? true : false) << endl; return (1<<i) & s ? true : false; }; auto is_connected = [&](int s) -> bool{ // cout << "test " << s << endl; for(int i = 0 ; i < n ; i++){ for(int j = i+1; j < n ; j++){ if( ! state_contain(s, i) ) continue; if( ! state_contain(s,j) ) continue; if(e[i][j] == 0){ return false; } } } return true; }; vector<int> group(n); REP(i, n){ group[i] = 1 << i; } REP(ss, state_num){ connected[ss] = is_connected(ss); if(connected[ss]){ // deb(ss) for(int i = 0 ; i < n ; i++){ if(state_contain(ss, i)) group[i] = ss; } } } // cout << "start dp" << endl; //max group size int INF = 1 << 30; vi dp(state_num, INF); dp[0] = 0; while (state < state_num){ if(connected[state]){ dp[state] = 1; state++; continue; } // deb(state) for(int submask = state; submask; submask=(submask-1)&state){ dp[state] = min(dp[state], dp[submask] + dp[state^submask]); } state++; } cout << dp[state_num-1] << endl; } int main() { //making data IO Fast std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); /****************************/ solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ull unsigned long long #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(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=100005; //i_am_noob int n; int solve(int x){ while(x>=n) x-=pow2(__lg(x)); return x; } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); cin >> n; rep(n) print(solve(i<<1)+1,solve(i<<1|1)+1); return 0; }
#include<algorithm> #include<cstdio> #define MaxN 200500 #define ll long long using namespace std; const int mod=998244353; ll powM(ll a,int t=mod-2){ ll ret=1; while(t){ if (t&1)ret=ret*a%mod; a=a*a%mod;t>>=1; }return ret; } ll fac[MaxN],ifac[MaxN]; ll C(int n,int m) {return fac[n]*ifac[m]%mod*ifac[n-m]%mod;} void Init(int n) { fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod; ifac[n]=powM(fac[n]); for (int i=n;i;i--) ifac[i-1]=ifac[i]*i%mod; } int d[MaxN][22]; void sieve(int n) { for (int i=1;i<=n;i++){ d[i][1]=1; for (int j=i+i;j<=n;j+=i) for (int k=1;k<20;k++) d[j][k+1]=(d[j][k+1]+d[i][k])%mod; } } ll buf[22]; int n,m; int main() { scanf("%d%d",&n,&m); Init(n+20);sieve(m); ll ans=0; for (int k=1;k<=min(20,n);k++)buf[k]=C(n-1,k-1); for (int i=1;i<=m;i++) for (int k=1;k<=min(20,n);k++) ans=(ans+d[i][k]*buf[k])%mod; printf("%lld",ans); 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 = 998244353; // nCr (mod p) 計算量 O(3*10^5*logp) const ll Maximum = 3e5; ll Fac[Maximum], iFac[Maximum]; bool made_Fac = false; // x^n (mod p) 計算量 O(logn) ll powmodp(ll x, ll n) { if (n==0) return 1; else if (n%2==0) { ll ans = powmodp(x,n/2); return ans*ans % mod; } else return x * powmodp(x,n-1) % mod; } // nCr (mod p) 計算量 O(3*10^5*logp) void make_Fac() { Fac[0] = 1; iFac[0] = 1; for (ll i=0; i<Maximum; i++) { Fac[i+1] = Fac[i] * (i+1) % mod; iFac[i+1] = iFac[i] * powmodp(i+1,mod-2) % mod; } } ll nCrmodp(ll n, ll r) { if (!made_Fac) { make_Fac(); made_Fac = true; } r = min(r,n-r); if (n==0 or r==0) return 1; ll tmp = iFac[n-r] * iFac[r] % mod; return tmp * Fac[n] % mod; } vector<ll> prime_counter(ll N) { vector<ll> res; for (ll i=2; i*i<=N; i++) { if (N%i) continue; ll cnt = 0; while (N%i==0) {cnt++; N/=i;} res.push_back(cnt); } if (N!=1) res.push_back(1); return res; } int main() { ll N, M; cin >> N >> M; ll ans = 0; for (ll i=1; i<=M; i++) { vector<ll> div = prime_counter(i); ll add = 1; for (ll cnt : div) { ll tmp = nCrmodp(cnt+N-1,N-1); add = add * tmp % mod; } ans = (ans + add) % mod; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; while (scanf("%d", &n) == 1) { vector<int> a(n, 1); for (auto &x : a) scanf("%d", &x); map<long long, int> m = { { 0, 1 } }; long long s[2] = { 0 } ; long long res = 0; for (int i = 0; i < n; i++) { s[i % 2] += a[i]; res += m[s[0] - s[1]]; m[s[0] - s[1]]++; } printf("%lld\n", res); } }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { int n; cin >> n; vector<int> a(n); REP(i, n) cin >> a[i]; vector<ll> d_even(n), d_odd(n); d_even[0] = a[0]; d_odd[0] = -a[0]; FOR(i, 1, n) { d_even[i] = a[i] * (i & 1 ? -1 : 1) + d_even[i - 1]; d_odd[i] = a[i] * (i & 1 ? 1 : -1) + d_odd[i - 1]; } // REP(i, n) cout << d_even[i] << " \n"[i + 1 == n]; // REP(i, n) cout << d_odd[i] << " \n"[i + 1 == n]; map<ll, int> mp_even, mp_odd; REP(i, n) ++mp_even[d_even[i]]; REP(i, n) ++mp_odd[d_odd[i]]; ll ans = 0, cur_even = 0, cur_odd = 0; REP(i, n) { --mp_even[d_even[i]]; --mp_odd[d_odd[i]]; ans += (i & 1 ? mp_odd[cur_odd] : mp_even[cur_even]); cur_even += a[i] * (i & 1 ? -1 : 1); cur_odd += a[i] * (i & 1 ? 1 : -1); } cout << ans << '\n'; return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please ll A[200000], tmp[200001]; void pakuri_sort(int N, ll A[]) { const int b = 9; rep(k, 6) { 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 = 1 << 17, CL = 20; 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; const ll pre[9] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000 }; inline ll 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; ll tmp2 = *(ll*)ci; if ((tmp2 & ma0) == ma0) { ci += 9; return 1000000000000000; } int dig = 68 - __builtin_ctzll((tmp2 & ma0) ^ ma0); if (dig == 64) { ci++; return tmp; } tmp2 = tmp2 << dig & ma1; tmp2 = tmp2 * 10 + (tmp2 >> 8) & ma2; tmp2 = tmp2 * 100 + (tmp2 >> 16) & ma3; tmp2 = tmp2 * 10000 + (tmp2 >> 32) & ma4; ci += (72 - dig >> 3); tmp = tmp * pre[64 - dig >> 3] + tmp2; } return tmp; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); int K[3] = {}; rep(i, N << 1) { ll a = getint(); char c = *ci++; ci++; ll C = (ll(c) >> 4 | ll(c) << 1) & 3; K[C]++; A[i] = a | C << 50; } pakuri_sort(N * 2, A); if (K[0] % 2 == 0 && K[1] % 2 == 0 && K[2] % 2 == 0) { co(0); return 0; } auto P = A; auto Q = A + K[0]; auto R = A + K[0] + K[1]; const ll ma = (1ll << 50) - 1; for(int i = N * 2 - 1; i >= K[0]; i--) A[i] &= ma; ll sai[3] = {}; ll saiq = 1e18, sair = 1e18; int q = 0, r = 0; rep(p, K[0]) { if (q < K[1]) if (saiq > abs(Q[q] - P[p])) saiq = abs(Q[q] - P[p]); while (q < K[1] - 1 && Q[q] < P[p]) { q++; if (saiq > abs(Q[q] - P[p])) saiq = abs(Q[q] - P[p]); } if (r < K[2]) if (sair > abs(R[r] - P[p])) sair = abs(R[r] - P[p]); while (r < K[2] - 1 && R[r] < P[p]) { r++; if (sair > abs(R[r] - P[p])) sair = abs(R[r] - P[p]); } } sai[2] = saiq; sai[1] = sair; sair = 1e18; r = 0; q = 0; while (q < K[1]) { if (r < K[2]) if (sair > abs(R[r] - Q[q])) sair = abs(R[r] - Q[q]); while (r < K[2] - 1 && R[r] < Q[q]) { r++; if (sair > abs(R[r] - Q[q])) sair = abs(R[r] - Q[q]); } q++; } sai[0] = sair; ll kotae = 1e18; if (K[0] % 2 == 0) kotae = min(sai[0], sai[1] + sai[2]); else if (K[1] % 2 == 0) kotae = min(sai[1], sai[2] + sai[0]); else kotae = min(sai[2], sai[0] + sai[1]); co(kotae); Would you please return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; typedef long long ll; char In[1 << 20], *ss = In, *tt = In; #define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++) ll read() { ll x = 0, f = 1; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0'); return x * f; } int readrgb() { char ch = getchar(); for(; ch != 'R' && ch != 'G' && ch != 'B'; ch = getchar()); return ch == 'R' ? 0 : (ch == 'G' ? 1 : 2); } const int MAXN = 1e5 + 5; const ll INF = 0x3f3f3f3f3f3f3f3fll; int n; vector<ll> ori[3]; ll getmindis(const vector<ll>& p, const vector<ll>& q) { if(!p.size() || !q.size()) return INF; ll ret = INF; for(int i = 0, j = 0; i < (int)p.size(); i++) { while(j < (int)q.size()-1 && q[j+1] <= p[i]) j++; if(q[j] <= p[i]) ret = min(ret, p[i] - q[j]); } for(int i = (int)p.size()-1, j = (int)q.size()-1; i >= 0; i--) { while(j > 0 && q[j-1] >= p[i]) j--; if(q[j] >= p[i]) ret = min(ret, q[j] - p[i]); } return ret; } int main() { n = read(); for(int i = 1; i <= 2 * n; i++) { ll a = read(), c = readrgb(); ori[c].push_back(a); } if((ori[0].size()&1) < (ori[1].size()&1)) swap(ori[0], ori[1]); if((ori[1].size()&1) < (ori[2].size()&1)) swap(ori[1], ori[2]); if((ori[0].size()&1) == 0) { printf("0\n"); return 0; } sort(ori[0].begin(), ori[0].end()); sort(ori[1].begin(), ori[1].end()); sort(ori[2].begin(), ori[2].end()); printf("%lld\n", min(getmindis(ori[0], ori[1]), getmindis(ori[0], ori[2]) + getmindis(ori[1], ori[2]))); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; template<typename T> ostream& operator<<(ostream &os, vector<T> &v){ string sep = " "; if(v.size()) os << v[0]; for(int i=1; i<v.size(); i++) os << sep << v[i]; return os; } template<typename T> istream& operator>>(istream &is, vector<T> &v){ for(int i=0; i<v.size(); i++) is >> v[i]; return is; } #ifdef DBG void debug_(){ cout << endl; } template<typename T, typename... Args> void debug_(T&& x, Args&&... xs){ cout << x << " "; debug_(forward<Args>(xs)...); } #define dbg(...) debug_(__VA_ARGS__) #else #define dbg(...) #endif int main() { ios_base::sync_with_stdio(false); cout << setprecision(20) << fixed; int h, w; cin >> h >> w; vector<string> a(h); cin >> a; vector<vector<int>> score(h, vector<int>(w)); for(int x=h-1; x>=0; x--){ for(int y=w-1; y>=0; y--){ if(x==h-1&&y==w-1) continue; score[x][y] = -1e9; if(x<h-1){ int p = a[x+1][y]=='+'?1:-1; score[x][y] = max(score[x][y], p-score[x+1][y]); } if(y<w-1){ int p = a[x][y+1]=='+'?1:-1; score[x][y] = max(score[x][y], p-score[x][y+1]); } } } if(score[0][0]>0) cout << "Takahashi" << endl; else if(score[0][0]<0) cout << "Aoki" << endl; else cout << "Draw" << 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; using namespace std; #define endl '\n' #define int long long int #define rep(i, a, b) for (int i = a; i < b; i++) #define revrep(i, a, b) for (int i = a; i >= b; i--) #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) 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>; ostream &operator<<( ostream &output, const pii &p ) { output << p.first << " " << p.second;return output; } istream &operator>>( istream &input, pii &p ) { input >> p.first >> p.second;return input; } template<typename T> void inline println(vector<T> args){ for(T i: args)cout<<i<<" ";cout<<endl; } void amax(int& a, int b) { a = max(a, b); } void amin(int& a, int b) { a = min(a, b); } int ceilInt(int a, int b) { if(!b) return 0; if(a%b) return a/b + 1; return a/b; } int INF = 1e18; int MOD = 998244353; void solve() { int n, m, w; cin>>n>>m>>w; vi minCol(n, m), minRow(m, n); vector<vi> obsRow(m), obsCol(n); rep(t, 0, w) { int i, j; cin>>i>>j; i--, j--; minCol[i] = min(minCol[i], j); minRow[j] = min(minRow[j], i); obsRow[j].pb(i); obsCol[i].pb(j); } int ans = 0, extra = 0; ordered_set<int> os; rep(j, 0, minCol[0]){ ans += minRow[j]; int key = min(minRow[j], minRow[0]); extra += max(0LL, key-(int)os.order_of_key(key)); for(int i: obsRow[j]) os.insert(i); } os.clear(); rep(i, 0, minRow[0]) { ans += minCol[i]; int key = min(minCol[i], minCol[0]); extra += max(0LL, key-(int)os.order_of_key(key)); for(int j: obsCol[i]) os.insert(j); } cout<<ans-extra/2<<endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; rep(i, 0, t) { solve(); } return 0; }
#include <iomanip> #include <iostream> #include <set> #include <string> #include <vector> #include <algorithm> #include <array> #include <map> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<ll> a(n); for(int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); ll ans = 0; for(int i = 0 ; i < n; ++i) { ans += a[i] * (2 * i - (n - 1)); } cout << ans << endl; }
// EXPLOSION! #define _CRT_SECURE_NO_WARNINGS #include<bits/stdc++.h> #include<unordered_set> #include<unordered_map> #include<chrono> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pair<int, int>> vpi; typedef vector<pair<ll, ll>> vpll; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define pb push_back #define mp make_pair #define rsz resize #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define f first #define s second #define cont continue #define endl '\n' //#define ednl '\n' #define test int testc;cin>>testc;while(testc--) #define pr(a, b) trav(x,a)cerr << x << b; cerr << endl; #define message cout << "Hello World" << endl; const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!! const ll linf = 4000000000000000000LL; const ll inf = 1000000007;//998244353 void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; } }void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; } void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef arwaeystoamneg if (sz(s)) { freopen((s + ".in").c_str(), "r", stdin); if (s != "test1") freopen((s + ".out").c_str(), "w", stdout); } #endif } template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint() { v = 0; } mint(ll _v) { v = int((-MOD < _v&& _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& m) { v = int((ll)v * m.v % MOD); return *this; } mint& operator/=(const mint& m) { return (*this) *= inv(m); } friend mint power(mint a, ll p) {// MAKE SURE YOU ARE USING THE CORRECT VERSION OF POW!!!!!!!! mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return power(a, MOD - 2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; typedef mint<inf, 5> mi; // 5 is primitive root for both common mods int main() { setIO("test1"); int n; cin >> n; vi a(n); trav(x, a)cin >> x; sort(all(a)); mi ans = 1 + a[0]; F0R(i, n - 1) ans *= (a[i + 1] - a[i] + 1); cout << (int)ans << endl; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define pb(a) push_back(a) #define vll vector<ll> #define loop(i, n) for(ll i=1;i<=n;i++) #define loop0(i, n) for(ll i=0;i<n;i++) #define in(i) scanf("%lld", &i); #define out(i) printf("%lld", i) #define pll pair<ll, ll> #define vpll vector<pair<ll, ll>> #define mp(i, j) make_pair(i, j) #define google cout<<"Case #"<<tc-t<<": "; const ll mod = 1000000007; const ll big = 2999999999999999999; const ll small = -2999999999999999999; void in2(ll& a, ll& b) { in(a); in(b); } void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); } void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; } void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); } ll n; vll a; vector<vpll> dp; pll solve(ll i, bool pm) { if(i == n) return {0, 1}; if(dp[i][pm].second != -1) return dp[i][pm]; if(pm) { pll b = solve(i+1, false); return dp[i][pm] = {(b.first + (b.second*a[i])%mod)%mod, b.second}; } else { pll b = solve(i+1, false); pll c = solve(i+1, true); return dp[i][pm] = {((b.first+c.first)%mod+(((b.second-c.second+mod)%mod)*(a[i]%mod))%mod)%mod, (b.second+c.second)%mod}; } } void solve() { in(n); a.resize(n); dp.resize(n+1, vpll(2, {-1, -1})); loop0(i, n) in(a[i]); cout<<solve(0, true).first; } int main() { solve(); }
#include <bits/stdc++.h> #define foreach(i,v) for(auto&& i: v) #define all(x) (x).begin(), (x).end() typedef std::vector< std::string > VS; typedef std::vector<int> VI; typedef long long ll; typedef std::pair<int, int> ii; template <class C> C& mini(C& a, C b) { if (b < a) a = b; return a; } template <class C> C& maxi(C& a, C b) { if (a < b) a = b; return a; } template <typename T> void read(std::vector<T>& A) { for (T& x: A) std::cin >> x; } template <typename T> void readv(std::vector<T>& A) { int N; std::cin >> N; A.resize(N); read(A); } using namespace std; #define MOD 1000000007 template <typename T> void imfix(T& r) { if (r >= MOD) r -= MOD; } template <typename T> T mfix(T r) { return r >= MOD ? r - MOD : r; } int main(int argc, const char* argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout.setf(ios::fixed); VI A; readv(A); int N = A.size(); vector<ll> B(N+1), C(N+1); B[N] = 0; B[N-1] = A.back(); C[N] = 1; C[N-1] = 1; for (int i = N-2; i >= 0; i--) { B[i] = (A[i] * C[i+1] + B[i+1]) % MOD + ((MOD + A[i] - A[i+1]) * C[i+2] + B[i+2]) % MOD; imfix(B[i]); imfix(C[i] = C[i+1] + C[i+2]); } cout << B[0] << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } const int INF = (1 << 30) - 1; const ll INFLL = (1LL << 61) - 1; const int MOD = 1000000007; #define ALL(a) (a).begin(), (a).end() #define rALL(a) (a).rbegin(), (a).rend() #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) int N, M, K; int A[101010]; int NG[101010]; long double dp[202020], S[202020]; long double solve(double v) { int i; for(i = N - 1; i >= 0; i--) { if(NG[i]) { dp[i] = v; } else { dp[i] = 1 + (S[i + 1] - S[i + 1 + M]) / M; } S[i] = S[i + 1] + dp[i]; } return dp[0]; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> N >> M >> K; int p = -1, cur = 0; REP(i, K) { cin >> A[i]; if(p + 1 != A[i]) cur = 0; p = A[i]; NG[A[i]] = 1; cur++; if(cur == M) { cout << -1 << endl; return 0; } }; long double L = 0, R = 1e12; REP(i, 300) { long double M = (L + R) / 2; if(solve(M) > M) L = M; else R = M; } cout << setprecision(15) << L << endl; }
#include <bits/stdc++.h> using namespace std; #define ms(s, n) memset(s, n, sizeof(s)) #define f(i, a, b) for (int i = (a); i <=(b); i++) #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--) #define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define sz(a) int((a).size()) #define present(t, x) (t.find(x) != t.end()) #define all(a) (a).begin(), (a).end() #define uni(a) (a).erase(unique(all(a)), (a).end()) #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define prec(n) fixed<<setprecision(n) #define bit(n, i) (((n) >> (i)) & 1) #define bitcount(n) __builtin_popcountll(n) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pi; typedef vector<ll> vi; typedef vector<pi> vii; const int MOD = (int) 1e9 + 7; const int MOD2 = MOD + 2; //1007681537; const int INF = (int) 1e9; const ll LINF = (ll) 1e18; const ld PI = acos((ld) - 1); const ld EPS = 1e-9; inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;} inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;} template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;} inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;} inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;} inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;} inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;} inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;} inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);} inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;} inline int sign(ld x, ld y) {return sign(x - y);} int modulo(int a, int b) { return (a % b + b) % b; } #define db(x) cerr << #x << " = " << (x) << " "; #define endln cerr << "\n"; vector<vi>adj(200010); vi vis(200010, 0), sum(2, 0), a(200010), b(200010); void dfs(ll u) { vis[u] = 1; sum[0] += a[u]; sum[1] += b[u]; for (auto v : adj[u]) { if (vis[v])continue; dfs(v); } } int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n, m; cin >> n >> m; f(i, 1, n) { cin >> a[i]; } f(i, 1, n) { cin >> b[i]; } f(i, 1, m) { ll x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } f(i, 1, n) { if (!vis[i]) { sum.clear(); sum = vi(2, 0); dfs(i); if (sum[0] != sum[1]) { cout << "No\n"; return 0; } } } cout << "Yes\n"; // read the stuff at the bottom return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * use int when constraints limits are tight * Time taken : global variable < pass by refernce < pass by value * It takes significant amt. of time in pass by value * Avoid Using endl */
#include "bits/stdc++.h" using namespace std; long long calc(int n){ long long ret=1ll*(n*(n+1ll))/2ll; return ret; } void solve(){ string s; cin>>s; int n=s.length(); if(n==1){ if(s[0]=='8'){ cout<<"Yes\n"; } else{ cout<<"No\n"; } return; } else if(n==2){ int num1=(s[0]-'0')*10+s[1]-'0'; int num2=(s[1]-'0')*10+s[0]-'0'; if(num1%8==0 || num2%8==0){ cout<<"Yes\n"; } else{ cout<<"No\n"; } return; } int cnt[10]; fill(cnt,cnt+10,0); for(auto c:s){ cnt[c-'0']++; } // vector<vector<int>> req; for(int i=104;i<1000;i+=8){ vector<int> temp(10,0); int j=i; while(j>0){ temp[(j%10)]++; j/=10; } bool flag=false; for(j=0;j<10;j++){ if(cnt[j]<temp[j]){ flag=true; break; } } if(flag){ continue; } cout<<"Yes\n"; return; } cout<<"No\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
#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; using ll = long long; using ld = long double; using ordered_set = tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>; void fastio() { cin.tie(nullptr); cin.sync_with_stdio(false); } const ll MOD = 1e9+7; const ll INF = 1e18; const ll N = 2e5+5; #define S second #define F first #define vt vector #define rep(i, st, en) for(ll i = (st); i < (en); ++i) #define repr(i, en, st) for(ll i = (en); i >= (st); --i) static bool dp[101][100001] = {0}; int main() { fastio(); ll n; cin >> n; vt<ll> t(n); ll net = 0; rep(i, 0, n) { cin >> t[i]; net += t[i]; } dp[0][0] = dp[0][t[0]] = 1; ll mxt = 1e5; rep(i, 1, n) { rep(j, 0, mxt+1) { if (dp[i-1][j] || (j >= t[i] && dp[i-1][j-t[i]])) { dp[i][j] = 1; } } } ll ans = 0; ll neth = net/2; rep(i, 0, neth+1) { if (dp[n-1][i]) { ans = i; } } ans = max(ans, net-ans); cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; void __print(int x) {cout << x;} void __print(long x) {cout << x;} void __print(long long x) {cout << x;} void __print(unsigned x) {cout << x;} void __print(unsigned long x) {cout << x;} void __print(unsigned long long x) {cout << x;} void __print(float x) {cout << x;} void __print(double x) {cout << x;} void __print(long double x) {cout << x;} void __print(char x) {cout << '\'' << x << '\'';} void __print(const char *x) {cout << '\"' << x << '\"';} void __print(const string &x) {cout << '\"' << x << '\"';} void __print(bool x) {cout << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';} template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";} void _print() {cout << "\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);} #define debug(x...) cout << #x << " = "; _print(x) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define rep(i,a,b) for (ll i = (a); i < (b); ++i) #define per(i,a,b) for (int i = (b)-1; i >= (a); --i) #define all(x) begin(x), end(x) #define mp make_pair #define pb push_back #define ff first #define ss second #define fast ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL) const int mod = 1e9 + 7, mxn = 1e5 + 1; int solve(){ int a, b; cin>> a>> b; int ans = 2*a + 100 - b; cout<< ans<< "\n"; return 0; } //Counting problems: Sometimes easier to find complement of the answer int main() { fast; int t = 1; // cin>> t; while(t--){ solve(); } return 0; } //v.clear(), vis.clear(); vis.resize(n + 1, 0), v.resize(n + 1); /* inv[1] = 1; // inv is ll for(int i=2; i<=mxn; ++i) inv[i]=mod-mod/i*inv[mod%i]%mod;*/ // lower bound >= // upper bound > // vector<int>().swap(vec); //free memory from vec
#include<iostream> using namespace std; int main() { int a, b; cin>>a>>b; cout<<2*a+100-b; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) #define repr(i, n) for(int i = n; i >= 0; i--) #define reps(i,a,b) for(int i= a; i < b; i++) #define INF 2e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; int main() { int a, b, c; cin >> a >> b >> c; if (c == 0) { if (a > b) { cout << "Takahashi" << endl; } else { cout << "Aoki" << endl; } } else { if (b > a) { cout << "Aoki" << endl; } else { cout << "Takahashi" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using ll = long long; int mod = 1000000007; const int INF = 1001001001; template<class T> void chmin(T& a, T b) {if (a > b) a = b;} template<class T> void chmax(T& a, T b) {if (a < b) a = b;} int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using Graph = vector<vector<int>>; ll GCD(ll a, ll b) { if (b == 0) return a; else return GCD(b, a % b); } ll LCM(ll a, ll b) { ll g = GCD(a, b); return a / g * b; } int main() { int a,b,c; cin >> a >> b >> c; cout << 21-(a+b+c) << endl; return 0; }
#include<bits/stdc++.h> //Ithea Myse Valgulious namespace chtholly{ typedef long long ll; #define re0 register int #define rel register ll #define rec register char #define gc getchar //#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++) #define pc putchar #define p32 pc(' ') #define pl puts("") /*By Citrus*/ char buf[1<<23],*p1=buf,*p2=buf; inline int read(){ int x=0,f=1;char c=gc(); for (;!isdigit(c);c=gc()) f^=c=='-'; for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0'); return f?x:-x; } template <typename mitsuha> inline bool read(mitsuha &x){ x=0;int f=1;char c=gc(); for (;!isdigit(c)&&~c;c=gc()) f^=c=='-'; if (!~c) return 0; for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0'); return x=f?x:-x,1; } template <typename mitsuha> inline int write(mitsuha x){ if (!x) return 0&pc(48); if (x<0) pc('-'),x=-x; int bit[20],i,p=0; for (;x;x/=10) bit[++p]=x%10; for (i=p;i;--i) pc(bit[i]+48); return 0; } inline char fuhao(){ char c=gc(); for (;isspace(c);c=gc()); return c; } }using namespace chtholly; using namespace std; const int yuzu=2e5; typedef int fuko[yuzu|10]; fuko a; int main() { for (int t=read();t--;) { int i,n=read(); for (i=1;i<=n;++i) a[i]=read(); sort(a+1,a+n+1); if (n&1) puts("Second"); else { int lxy=0; for (i=1;i<=n;i+=2) lxy|=a[i]^a[i+1]?1:0; puts(lxy?"First":"Second"); } } }
#include <bits/stdc++.h> struct __fastio__ { __fastio__ () { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } __fast_io__; template<class T, class U> bool chmin (T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; } template<class T, class U> bool chmax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T> void print(const T &value) { std::cout << value << '\n'; } template<class T, class... Args> void print(const T &value, Args... args) { std::cout << value << ' '; print(args...); } template<class T> using martix = std::vector<std::vector<T>>; using i32 = std::int_fast32_t; using u32 = std::uint_fast32_t; using i64 = std::int_fast64_t; using u64 = std::uint_fast64_t; using isize = std::ptrdiff_t; using usize = std::size_t; void solve () { int n; std::cin >> n; std::vector<int> a(n); for (int &e : a) std::cin >> e; if (n & 1) { std::cout << "Second" << '\n'; } else { std::map<int, int> mp; for (const int &e : a) mp[e]++; bool flag = true; for (const auto &p : mp) { flag &= (p.second % 2 == 0); } if (flag) std::cout << "Second" << '\n'; else std::cout << "First" << '\n'; } } int main() { int t; std::cin >> t; while (t--) solve(); return 0; }