code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#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 main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll N, C; cin >> N >> C; vector<ll> a(N), b(N), c(N); REP(i, N) cin >> a[i] >> b[i] >> c[i]; map<ll, ll> mp; REP(i, N) { a[i]--; b[i]--; mp[a[i]] += c[i]; mp[b[i] + 1] -= c[i]; } ll ans = 0, last_cost = 0, last_day = 0; for(auto e : mp) { ans += min(last_cost, C) * (e.first - last_day); last_cost += e.second; last_day = e.first; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define pb emplace_back #define mp make_pair #define fi first #define se second #define all(v) v.begin(),v.end() #define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0); #define LL_MAX LLONG_MAX #define ub(v,x) upper_bound(v.begin(),v.end(),x) #define lb(v,x) lower_bound(v.begin(),v.end(),x) #define mod 1000000007 int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;} int modinv(int x){return exp(x,mod-2);} int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;} int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;} int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;} int fac[1000009];int ncr_mod(int n,int k){int ans=fac[n];ans*=modinv(fac[k]);ans%=mod;ans*=modinv(fac[n-k]);ans%=mod;return ans;} vector<int>v_prime;void Sieve(int n){bool prime[n + 1];memset(prime,true,sizeof(prime));for (int p = 2; p*p <=n;p++){if(prime[p] ==true) {for(int i = p*p; i<= n; i += p)prime[i]=false;}}for(int p = 2;p<= n;p++)if (prime[p])v_prime.pb(p);} vector<int>v_factor;void factor(int n){ for (int i=1; i<=sqrt(n); i++) {if (n%i == 0) {if (n/i == i) v_factor.pb(i);else { v_factor.pb(i),v_factor.pb(n/i);};} } sort(all(v_factor)); } int power(int x, int y){int temp;if( y == 0)return 1; temp = power(x, y / 2);if (y % 2 == 0) return temp * temp;else return x * temp * temp;} int gcd(int a, int b){if (b == 0)return a; return gcd(b, a % b);} int log2n( int n){ return (n > 1) ? 1 + log2n(n / 2) : 0;} void out(vector<int>&a){for(int i=0;i<a.size();i++) cout<<a[i]<<" "; cout<<endl;} void sout(set<int>s){for(auto it=s.begin();it!=s.end();++it)cout<<*it<<" "; cout<<endl;} void mout(map<int,int>mm){for(auto it=mm.begin();it!=mm.end();++it) cout<<it->fi<<" "<<it->se<<endl;} #define ms(a,x) memset(a, x, sizeof(a)); #define decimal(n,k) cout<<fixed<<setprecision(k)<<n<<endl #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl // In binary search always report l-1 signed main() { run; /* open for mod factorial fac[0]=1; for(int i=1;i<1000009;i++) { fac[i]=mul(fac[i-1],i); } */ /* for sieve open this and use v_prime int pp=pow(10,6)+100000; Sieve(pp); */ // factor(n) && USE v_factor For calculating factors && don't forget v_factor.clear(); int t=1; while(t--) { map<int,int>mm; // it->second-->frequency int n,i,x,y,ok=0,sum=0,ans=0,j,k,cnt=0,m,c=0,q,z; int h[100009]={0}; cin>>n>>k; vector<int>a(n,0),b(n,0); vector<pair<int,int>>v; for(i=0;i<n;i++) { cin>>a[i]>>b[i]; v.pb(mp(a[i],b[i])); } sort(all(v)); for(i=0;i<n;i++) { a[i]=v[i].fi; b[i]=v[i].se; } int prev=0; for(i=0;i<n;i++) { // cout<<k<<endl; if(mm[a[i]]==0) { mm[a[i]]++; } else { k+=b[i]; continue; } if(a[i]-prev>k) { prev=prev+k; k=0; break; } else { k=k-(a[i]-prev); k+=b[i]; // cout<<k<<endl; prev=a[i]; } } prev+=k; cout<<prev<<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>; using C = complex<double>; int main() { ll n; cin >> n; set<ll> s; for (ll a = 2; a * a <= n; ++a) { ll x = a * a; while (x <= n) { s.insert(x); x *= a; } } cout << n - s.size() << endl; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "/debug.h" #else #define db(...) #endif #define all(v) v.begin(), v.end() #define pb push_back using ll = long long; const int NAX = 2e5 + 5, MOD = 1000000007; int64_t solveCase(int64_t N) { // TODO: edit here set<int64_t> ss; for (int64_t i = 2; i <= N; i++) { if (ss.find(i) != ss.end()) continue; auto j = i * i; if (j > N) break; while (j <= N) { ss.insert(j); j *= i; } db(i, j); } return N - ss.size(); } // generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator) int main() { #ifndef LOCAL std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #endif constexpr char endl = '\n'; int64_t N; cin >> N; auto ans = solveCase(N); cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define For(i,n,k) for(int i=(n);i<(k);i++) #define ALL(a) (a).begin(),(a).end() ll ans = 0; void Main(){ using P = pair<int, int>; int a, b, x, y; cin >> a >> b >> x >> y; a--; b--; vector<vector<P>> graph(200); For(i,0,100){ graph[i].push_back({i + 100, x}); graph[i + 100].push_back({i , x}); } For(i,0,99){ graph[i + 1].push_back({i + 100, x}); graph[i + 100].push_back({i + 1 , x}); } For(i,0,99){ graph[i].push_back({i + 1, y}); graph[i + 1].push_back({i, y}); graph[i + 100].push_back({i + 101, y}); graph[i + 101].push_back({i + 100, y}); } vector<int> dist(200, 1e9); dist[a] = 0; stack<P> st; st.push({a , 0}); while(!st.empty()){ auto [now, d] = st.top(); st.pop(); for(auto to : graph[now]){ if(dist[to.first] > d + to.second){ dist[to.first] = d + to.second; st.push({to.first, dist[to.first]}); } } } cout << dist[b + 100] << endl; } int main(){ Main(); /* 東方風神録は神が出てくるので当然神ゲー */ return 0; }
#include<iostream> #include<bits/stdc++.h> #define int long long int using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int a,b,x,y; cin>>a>>b>>x>>y; int dist = abs(a-b); int cost1 = dist*y +x,cost2,cost3; if(a<=b) cost2 = 2*dist*x + x; else { cost2 = 2*dist*x -x; cost3 = x +y*(dist-1); cost2 = min(cost2,cost3); } cout<<min(cost1,cost2); return 0; }
/**Bismillahir Rahmanir Rahim.**/ /* Md.Fagun Molla 18ICTCSE006 BSMRSTU(SHIICT) */ #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; using ll = long long; using db = double; ///***************************************************** CONSTANTS ******************************************************/// int const N = 1e6 + 3; ll MOD = 1e9 + 7, fact[N]; const long long inf = (long long)1e18; const long double PI = 3.14159265358979; ///************************************************ CONTAINER DEFINITIONS ***********************************************/// typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; ///************************************************ SHORT FORM KEYWORDS *************************************************/// #define PB push_back #define F first #define S second #define MP make_pair #define I insert #define lb lower_bound #define ub upper_bound #define endl '\n' ///************************************************ SHORT FORM FUNCTIONS ************************************************/// #define loop(a,b) for(ll i=a;i<b;i++) #define loopr(a,b) for(ll i=a-1;i>=b;i--) #define mem(a,b) memset(a, b, sizeof(a) ) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define sa(v) sort(v.begin(),v.end()) #define sd(v) sort(v.begin(),v.end(),greater<>()) #define rev(s) reverse(s.begin(),s.end()) #define stll(x) stoll(x, nullptr, 10); #define yes cout<<"YES"<<endl; #define no cout<<"NO"<<endl; #define mx(a) *max_element(all(a)) #define mn(a) *min_element(all(a)) #define all(a) a.begin(),a.end() #define mxa(a,N) *max_element(a,a+N) #define mna(a,N) *min_element(a,a+N) #define print(a) {for(auto x:a)cout<<x<<" ";cout<<endl;} #define io() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); ///************************************************ SOLUTION STARTS HERE ************************************************/// ///======================================================================================================================/// void solve() { int n; cin>>n; int aa[n]; for(int i=0; i<n; i++) cin>>aa[i]; sort(aa,aa+n); for(int i=0; i<n; i++) { if(aa[i]!=i+1) { cout<<"No"<<endl; return; } } cout<<"Yes"<<endl; } int main() { io(); int T = 1; //cin>>T; while (T--) solve(); return 0; } /**************************************************ALHAMDULILLAH************************************************/
#include <iostream> #include <vector> #include <algorithm> using namespace std; int N; int main() { //45 200 , out -> 90 cin >> N; vector <int> S(N); for (int i=0; i < N; ++i) { cin >> S[i]; } sort(S.begin(), S.end()); int count; count = 0; for (int i=0; i < N; ++i) { if ( i + 1 == S[i]) { count += 1; } } if (count == N) { cout << "Yes" << endl; } else { cout << "No" << endl; } // cout << S[0] << S[1] << endl; }
#include <iostream> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <utility> #include <string> #include <sstream> #include <algorithm> #include <random> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <cmath> #include <cassert> #include <climits> #include <bitset> #include <functional> #include <iomanip> #include <random> #define FOR_LT(i, beg, end) for (decltype(end) i = beg; i < end; i++) #define FOR_LE(i, beg, end) for (decltype(end) i = beg; i <= end; i++) #define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--) #define REP(n) for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++) using namespace std; template<int64_t M = 1000000007> class ModInt { public: ModInt() : val_(0) {}; ModInt(int64_t val) { this->val_ = val; this->val_ %= M; while (this->val_ >= M) this->val_ -= M; while (this->val_ < 0) this->val_ += M; } operator int64_t() const { return this->val_; } ModInt& operator += (int64_t val) { this->val_ += val; this->val_ %= M; while (this->val_ >= M) this->val_ -= M; while (this->val_ < 0) this->val_ += M; return *this; } ModInt& operator -= (int64_t val) { this->val_ -= val; this->val_ %= M; while (this->val_ >= M) this->val_ -= M; while (this->val_ < 0) this->val_ += M; return *this; } ModInt& operator *= (int64_t val) { this->val_ *= val; this->val_ %= M; return *this; } ModInt& operator /= (int64_t val) { ModInt v = ModInt::pow(val, M - 2); *this *= v; return *this; } ModInt& operator += (int32_t val) { return this->operator+=((int64_t)val); } ModInt& operator -= (int32_t val) { return this->operator-=((int64_t)val); } ModInt& operator *= (int32_t val) { return this->operator*=((int64_t)val); } ModInt& operator /= (int32_t val) { return this->operator/=((int64_t)val); } ModInt operator + (int64_t v) const { ModInt ret = *this; return (ret += v); } ModInt operator - (int64_t v) const { ModInt ret = *this; return (ret -= v); } ModInt operator * (int64_t v) const { ModInt ret = *this; return (ret *= v); } ModInt operator / (int64_t v) const { ModInt ret = *this; return (ret /= v); } ModInt operator + (int32_t v) const { return this->operator+((int64_t)v); } ModInt operator - (int32_t v) const { return this->operator-((int64_t)v); } ModInt operator * (int32_t v) const { return this->operator*((int64_t)v); } ModInt operator / (int32_t v) const { return this->operator/((int64_t)v); } static ModInt pow(ModInt n, int64_t m) { if (m == 0) { ModInt ret(1); return ret; } if (m == 1) { return n; } ModInt v = pow(n, m / 2); ModInt ret = v * v; if (m & 1) { ret *= n; } return ret; } ostream& operator << (ostream& out) { out << this->val_; return out; } private: int64_t val_; }; void solve() { int64_t n, a, b; cin >> n >> a >> b; ModInt<> t = n - a + 1; ModInt<> u = n - b + 1; ModInt<> v = max(n - a - b + 1, (int64_t)0); ModInt<> x = t * u - v * (v + 1); ModInt<> y = t * t * u * u - x * x; cout << y << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(20); int t; cin >> t; REP(t) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define Pr pair<ll,ll> #define Tp tuple<int,int,int> using Graph = vector<vector<int>>; ll mod = 998244353; int main() { ll N,M; cin >> N >> M; vector<Pr> br; rep(i,M){ ll x,y; cin >> x >> y; br.push_back(make_pair(x,y)); } sort(br.begin(),br.end()); set<ll> can,temp; can.insert(N); ll nx = 0; rep(i,M){ ll x = br[i].first; ll y = br[i].second; if(x!=nx){ temp.clear(); nx = x; } bool en = false; rep(j,3){ if(j==1) continue; if(can.count(y-1+j)){ if(temp.count(y-1+j)) continue; en = true; if(can.count(y)) continue; can.insert(y); temp.insert(y); } else{ if(!temp.count(y-1+j)) continue; en = true; if(can.count(y)) continue; can.insert(y); temp.insert(y); } } if(en) continue; if(can.count(y)){ can.erase(y); temp.insert(y); } } //for(ll x:can) cout << x << endl; cout << can.size() << endl; }
#include<bits/stdc++.h> using namespace std; int main() { int a, b, x, y; cin >> a >> b >> x >> y; if(a == b) { cout << x << '\n'; return 0; } cout << x + min(abs(a - b), abs(a - (b + 1))) * min(2 * x, y) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define Rep(i,j,n) for(int i=j; i<n; i++) #define rep(i,n) for(int i=0; i<n; i++) #define PI 3.14159265359 #define INF 1000100100//0000000 #define MOD 1000000007 #define all(x) (x).begin(),(x).end() typedef long long ll; #define P pair<int, int> //#define P pair<ll, ll> //#define P pair<double, double> #define PP pair<P,P> #define T tuple<int,int,int> int main(){ int a,b,c,d; cin >> a >> b >> c >> d; if(a==c && b==d) cout << 0 << endl; else if(a+b==c+d || a-b==c-d || abs(a-c)+abs(b-d)<=3) cout << 1 << endl; else if((a+b)%2==(c+d)%2 || abs(a-c)+abs(b-d)<=6 || abs(abs(a-c)-abs(b-d))<=3) cout << 2 << endl; else cout << 3 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin >> n; int res=0; for(int i=1;i<=1000000;i++){ string s=to_string(i); s+=s; ll p=stoll(s); if(p<=n)res++; } printf("%d\n",res); }
#ifdef Icry #include "debug.h" #else #include <bits/stdc++.h> #define debug(args...) #endif using namespace std; int main() { #ifdef Icry auto begin = std::chrono::high_resolution_clock::now(); #endif ios_base :: sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector <double> F(n + 1, 0); for (int i = 1; i <= n; ++i) { F[i] = F[i - 1] + double(n) / double(i); } cout << fixed << setprecision(9) << F[n] - 1 << '\n'; #ifdef Icry auto end = std::chrono::high_resolution_clock::now(); cerr << setprecision(4) << fixed; cerr << "[Execution time: " << std::chrono::duration_cast <std::chrono::duration <double>>(end - begin).count() << " seconds]\n"; #endif }
#include <bits/stdc++.h> using namespace std; #define int int64_t #define rep(i, a, b) for(int i = a; i < b; ++i) int debug = 0; int n, y, corr = 1, x[100]; main() { cin >> n; rep(i, 0, n) { cin >> x[i]; int d = 2, ispr = 1; while (d * d <= x[i]) { if (x[i] % d == 0) { ispr = 0; break; } ++d; } if (ispr) { corr *= x[i]; x[i] = 0; } } rep(i, 0, n) { if (x[i] > 0 && __gcd(x[i], corr) > 1) { x[i] = 0; } } y = 1; while (1) { int f = 1; rep(i, 0, n) { if (x[i] > 0 && __gcd(y, x[i]) == 1) { f = 0; break; } } if (f) { cout << y * corr << '\n'; return 0; } y++; } }
#include<bits/stdc++.h> /* shinchanCoder Here!! */ using namespace std; #define endll "\n" #define pb push_back #define all(x) begin(x), end(x) #define allr(x) rbegin(x), rend(x) #define forn(i,n) for(ll i=0;i<(n);i++) #define sz(x) (int)(x).size() #define ff first #define ss second #define mpr make_pair #define len(x) x.length() #define ll long long #define ld long double #define lli long long int #define mod 1000000007 ll powmod(ll a, ll b){ if(b==0){ return 1 ; } ll x = powmod(a,b/2); x = (x*x)%mod ; if(b%2){ x = (a*x)%mod ; } return x; } ll power(ll x, ll y) { ll res = 1; while (y > 0) { if (y & 1) res = res*x; y = y>>1; x = x*x; } return res; } vector<ll> prm; void Sieve(ll n) { bool prime[n+1]; memset(prime,true, sizeof(prime)); for(ll p=2;p*p<=n;p++) { if(prime[p]==true) { for(ll i=p*p;i<=n;i+=p) { prime[i]=false; } } } if(!prm.empty()) { prm.clear(); } for(ll p=2;p<=n;p++) { if(prime[p]) { prm.pb(p); } } } ll fact(ll n) { ll res = 1; for(ll i=2;i<=n;i++) { res*=i; } return res; } ll _lcm(ll x,ll y) { return x*y/__gcd(x,y); } ll _gcd(ll a,ll b) { return a%b==0 ? b : _gcd(b,a%b); } ll nCr(ll n,ll r) { return (ld)fact(n)/(ld)(fact(r)*fact(n-r)); } ll finres=1e18; bool isCoPrime(ll z,ll a[],ll n) { for(ll i=0;i<n;i++) { if(_gcd(a[i],z)==1) { return 1; } } return 0; } void solve(ll prm[15],ll i,ll res,ll a[],ll n) { if(i==15) { if(!isCoPrime(res,a,n)) { finres=min(finres,res); } return; } solve(prm,i+1,res*prm[i],a,n); solve(prm,i+1,res,a,n); } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //Sieve(1000000); ll prm[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++) { cin>>a[i]; } solve(prm,0,1,a,n); cout<<finres; return 0; }
#include <bits/stdc++.h> using namespace std; int N, M, K; int dis[18][100001]; int c[18]; vector<int> g[100001]; const int ALL = (1 << 18) - 1; const int INF = 1000000; int dp[ALL + 1][18]; int solve(int mask, int last) { if (dp[mask][last] != -1) { return dp[mask][last]; } int & res = dp[mask][last]; res = INF; if (mask == ((1 << K) - 1)) { // cout << mask << endl; res = 0; return res; } for (int i = 0; i < K; ++i) { if (mask == 0) { res = min(res, 1 + solve(mask | (1 << i), i)); continue; } // haven't included it yet if (((1 << i) & mask) == 0) { int d = dis[last][c[i]]; if (d >= 0) res = min(res, d + solve(mask | (1 << i), i)); // cout << c[last] << " " << c[i] << " " << dis[c[last]][c[i]] << " " << res << endl; } } return res; } void getDis(int i) { int s = c[i]; queue<int> q; q.push(s); dis[i][s] = 0; while (!q.empty()) { int u = q.front(); q.pop(); int d = dis[i][u]; for (int v : g[u]) { if (dis[i][v] == -1) { dis[i][v] = d + 1; q.push(v); } } } } int main() { memset(dis, -1, sizeof dis); memset(dp, -1, sizeof dp); cin >> N >> M; for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } cin >> K; for (int i = 0; i < K; ++i) { cin >> c[i]; getDis(i); } // cout << dis[1][3] << endl; int res = solve(0, 0); if (res == INF) { cout << "-1\n"; } else { cout << res << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define fast { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define pb push_back #define ll long long #define vi vector<int> #define vll vector<long long> #define mp make_pair #define infi INT_MAX #define infl LONG_LONG_MAX #define infd LDBL_MAX #define f(i,a,b) for(ll i=a;i<b;i++) #define ff first #define ss second #define pll pair<ll,ll> #define INF 1999999996000000010 #define endl "\n" #define ALL(v) v.begin(),v.end() #define nl cout<<"\n"; #define pr(x) cout<<x; #define pr1(x) cout<<x<<" "; #define pr2(x,y) cout<<x<<" "<<y; #define deb printf("*****************************\n"); #define idk printf("idk idk ! \n"); #define hi printf("hi ! \n"); #define bye printf("bye bye ! \n"); #define o_set(ll) tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define number_of_set_bits __builtin_popcountll //template<typename T> //#define o_set(T) tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> //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 vll v[300005]; ll dp[20][(1ll<<18)]; ll d[20][300005]; ll k; void dijkstra(ll src,ll idx){ queue<ll> q; q.push(src); d[idx][src]=0; while(!q.empty()){ ll elem = q.front(); q.pop(); for(auto itr : v[elem]){ if(d[idx][itr]!=INF) continue; d[idx][itr]=d[idx][elem]+1; q.push(itr); } } } ll fun(ll nodeidx, ll mask, vll& p) { if(mask == (1ll<<k) - 1) return 0; else if(dp[nodeidx][mask] != -1) return dp[nodeidx][mask]; ll ans = 1e9; for(int i = 0; i <k; ++i) { if(mask&(1ll<<i)) continue; { ll cost = d[nodeidx][p[i]] + fun(i, mask|(1ll<<i), p ); ans = min(ans,cost); } } return dp[nodeidx][mask] = ans; } int main(){ fast #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE ll ntc=1; f(ks,1,ntc+1) { ll n,m; cin>>n>>m; f(i,0,n+3) v[i].clear(); f(i,0,m){ ll a,b; cin>>a>>b; v[a].pb(b); v[b].pb(a); } cin>>k; vll p(k); f(i,0,k) cin>>p[i]; bool ok = true; f(i,0,k){ f(j,0,n+1) d[i][j]=INF; } f(i,0,k){ dijkstra(p[i],i); f(j,0,k) { if(d[i][p[j]] == INF) ok = false; } } // f(i,0,k){ // f(j,1,n+1) cout<<d[i][j]<<" "; // cout<<endl; // } if(ok == false) cout<<-1<<endl; else { ll ans = LLONG_MAX; memset(dp,-1,sizeof dp); for(int i = 0; i <k; ++i){ ll mask = (1<<i); ll here = fun(i, mask, p); ans = min(ans,here); } cout<<ans + 1<<endl; } } return 0; }
// Problem: D - Opposite // Contest: AtCoder - AtCoder Beginner Contest 197(Sponsored by Panasonic) // URL: https://atcoder.jp/contests/abc197/tasks/abc197_d // Memory Limit: 1024 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) /* WINNERS NEVER QUIT AND QUITTERS NEVER WIN!! Falling down is an accident, Staying down is a choice. */ #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<long,long> pl; typedef pair<ll,ll> pll; typedef vector<long> vl; typedef vector<bool> vb; typedef vector<ll> vll; typedef vector<vl> vvl; typedef vector<vb> vvb; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<string> vs; typedef unordered_map<ll,ll> umll; #define FOR(i,a,b) for(long long i=a;i<b;++i) #define REV(i,a,b) for(long long i=a;i>=b;i--) #define F first #define S second #define pb push_back #define mp make_pair #define ub upper_bound #define lb lower_bound #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define tc ll tests;cin>>tests;while(tests--) #define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define coutv(v) for(auto it: (v))cout<<it<<" ";newl; #define cout2d(v) for(auto it: (v)) {for(auto j:it) cout<<j<<" ";newl;} #define cinv(v,n) vll (v)(n);FOR(i,0,(n)){cin>>v[i];} #define cinvg(v,n) (v).resize(n);FOR(i,0,(n)){cin>>v[i];} #define cin2d(v,n,m) vvll (v)(n,vll(m,0));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}} #define cin2dg(v,n,m) (v).resize(n,vll(m));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}} #define newl cout<<"\n" #define mod 1000000007 #define INF LLONG_MAX/2 int main() { io; ll n,x0,y0,xn,yn; cin>>n; cin>>x0>>y0>>xn>>yn; ld pi =3.141592653589793238 , theta = (2*pi)/(ld)n; ld xm = ((ld)(x0+xn))/2, ym = ((ld)(y0+yn))/2; ld x0_new = x0-xm, y0_new = y0-ym; ld x1_new = x0_new*cos(theta)-y0_new*sin(theta), y1_new = x0_new*sin(theta)+y0_new*cos(theta); ld x1 = x1_new + xm, y1 = y1_new + ym; cout<<setprecision(10)<<x1<<" "<<y1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { double N, x0, y0, xn2, yn2; cin >> N >> x0 >> y0 >> xn2 >> yn2; complex<double> a(x0,y0), b(xn2, yn2), c, d; c = (a+b)/2.0; d = c + (a-c)*polar(1.0, M_PI*2.0/N); cout << d.real() << ' ' << d.imag() << endl; return 0; }
#include <bits/stdc++.h> #define lli long long int #define ulli unsigned long long int #define ld long double #define pb push_back #define endl "\n" #define mod 1000000007 #define vi vector<lli> #define precision(x,y) fixed<<setprecision(y)<<x #define sort(a) sort(a.begin(),a.end()) #define getsetbits(n) __builtin_popcount(n) //first idx >= val #define lb(a,x) lower_bound(a.begin(),a.end(),x)-a.begin() //first idx > val #define ub(a,x) upper_bound(a.begin(),a.end(),x)-a.begin() #define b begin() #define e end() #define f first #define s second using namespace std; void solve(){ lli n; cin>>n; vi a(n); for(int i=0;i<n;i++){ cin>>a[i]; } sort(a); lli ans = 0; for(int i=0;i<n;i++){ lli x = ub(a,a[i]); if(x >= 0 && x<n) ans+=n-x; } cout<<ans<<endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; // cin>>t; while(t--){ solve(); } }
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; //typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define pb push_back #define PI acos(-1.0) #define si(n) scanf("%d",&n) #define sll(n) scanf("%lld",&n) #define sdbl(n) scanf("%lf",&n) #define sstr(s) scanf("%s",s) #define all(v) v.begin(),v.end() #define mp make_pair #define SP(n) cout << fixed << setprecision(n) #define CLR(a) memset(a,0,sizeof(a)) #define SET(a) memset(a,-1,sizeof(a)) #define lcm(a,b) (max((a),(b))/__gcd((a),(b)))*min((a),(b)) #define ff first #define ss second #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL) #define MOD 1000000007 #define gamma 0.5772156649015328606065120 #define dbg(a) cout << "->" << __LINE__ << ": " << #a << " = " << a << endl #define PY printf("YES\n") #define PN printf("NO\n") ll setBit(ll mask, int p){ return (mask | (1 << p)); } ll resetBit(ll mask, int p){ return (mask & ~(1 << p)); } ll flipBit(ll mask, int p){ return (mask ^ (1 << p)); } bool check(ll mask, int p){ return (mask & (1 << p)); } int msb(long long N){ return N ? 63 - __builtin_clzll(N): -1; } int lsb(long long N){ return __builtin_ffs(N) - 1;} int countOn(ll mask){ return __builtin_popcountll(mask); } int countOff(ll mask){ return msb(mask) - countOn(mask) + 1; } ll bigmod(ll a, ll b, ll mod) { a %= mod; ll ret = 1; while(b){ if(b&1) ret = ret*a%mod; a = a*a%mod; b >>= 1; } return ret; } ll modInverse(ll a) { return bigmod(a, MOD-2, MOD); } mt19937_64 mt_rnd_64(chrono::steady_clock::now().time_since_epoch().count()); long long rnd(long long l, long long r) { return (mt_rnd_64() % (r-l+1)) + l; } /* four directions int fx[]={+1,-1,+0,+0}; int fy[]={+0,+0,+1,-1};*/ /* eight directions int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};*/ /*knight directions int fx[]= {1, -1, 1, -1, 2, 2, -2, -2}; int fy[]= {2, 2, -2, -2, 1, -1, 1, -1};*/ /*ordered set stuff ordered_set os; how many values are less than x int n = os.order_of_key(x); if the numbers are in sorted order, what is the k'th number int p = *os.find_by_order(k);*/ int main() { int a,b,c,d,x,y; si(a); si(b); si(c); si(d); x = max(a,b); y = min(d,c); printf("%d\n",x-y); }
#include<stdio.h> #include<stdlib.h> #include<bits/stdc++.h> //Do DEBUG OR NOT #define DEBUG #ifdef DEBUG #define debug(var) do{std::cout << #var << " : ";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){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } } #else #define debug(...) true #endif #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef long long ll; //prototype declaration ll pow_mod(ll n, ll k, ll m);//繰り返し二乗法 return val:(n^k)%m ll gcd(ll a, ll b);//最大公約数 2引数 ll ngcd(vector<ll> a); ll lcm(ll a, ll b);//最小公倍数 2引数 ll nlcm(vector<ll> numbers); bool isPrime(ll x);//素数判定 ll digsum(ll n);//桁和 vector<ll> enum_div(ll n);//約数全列挙 int stringcount(string s, char c);//特定文字カウント //__________________________main__________________________ int main(){ ll N; cin>>N; int ans=0; ll TwoN=2*N; for(ll i = 1 ; i*i <= TwoN; i++){ if(TwoN%i == 0){ int temp=TwoN/i; temp=temp-(i-1); if(temp%2==0) ans++; if(i != 1 && i*i != TwoN){ int temp=TwoN/i; temp=temp-(i-1); if(temp%2==0) ans++; } } //if(i%10000==0) cout<<i<<endl; } ans++; cout<<ans<<endl; return 0; } //__________________________functions_____________Ctrl+K,Ctrl+0 //繰り返し二乗法関数 ll pow_mod(ll n, ll k, ll m){//r:(n^k)%m ll r=1; for(;k>0;k=k>>1){ if(k&1) r=(r*n)%m; n = (n*n) % m; } return r; } //最大公約数 2引数 ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } //最大公約数 n引数 ll ngcd(vector<ll> a){ ll res; res = a[0]; for(int i = 1; i < a.size() && res != 1; i++) { res = gcd(a[i], res); } return res; } //最小公倍数 2引数 ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } //最小公倍数 n引数 ll nlcm(vector<ll> numbers) { ll res; res = numbers[0]; for (int i = 1; i < numbers.size(); i++) { res = lcm(res, numbers[i]); } return res; } //素数判定 bool isPrime(ll x){ ll i; if(x < 2)return 0; else if(x == 2) return 1; if(x%2 == 0) return 0; for(i = 3; i*i <= x; i += 2) if(x%i == 0) return 0; return 1; } //桁和 ll digsum(ll n) { ll res = 0; while(n > 0) { res += n%10; n /= 10; } return res; } //約数全列挙 vector<ll> enum_div(ll n){ vector<ll> ret; for(int i = 1 ; i*i <= n ; ++i){ if(n%i == 0){ ret.push_back(i); if(i != 1 && i*i != n){ ret.push_back(n/i); } } } return ret; } //特定文字カウント int stringcount(string s, char c) { return count(s.cbegin(), s.cend(), c); }
#include<bits/stdc++.h> #define SORT(v) sort(v.begin(),v.end()) #define si(n) scanf("%d",&n) #define sii(n,m) scanf("%d %d",&n,&m) #define sl(n) scanf("%lld",&n) #define sll(n,m) scanf("%lld %lld",&n,&m) #define ss(cad) scanf("%s",cad) #define all(v) (v).begin(), (v).end() #define PB push_back #define fst first #define scn second #define DBG(x) cerr << #x << " = " << (x) << endl; #define M 1000000007 #define N_MAX 100010 using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<ll> vl; typedef pair<int,int> pi; typedef vector<pi> vp; int main(){ ll N; sl(N); ll n = 1; int ans = 0; while(N-n*(n-1)/2 > 0){ ll tmp = N-n*(n-1)/2; if(tmp%n == 0){ //cout << tmp/n << " "; ans++; } n++; } printf("%d\n", ans*2); return 0; }
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> #include <string> #include <sstream> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; using P = pair<int,int>; const int INF=1001001001; const int mod =1e9+7; int main() { int N,M; cin>>N>>M; vector<int>w(N),l(M),v(M); rep(i,N){cin>>w[i];} rep(i,M){cin>>l[i]>>v[i];} int mx=*max_element(w.begin(),w.end()); vector<pair<int,int>>p(M); rep(i,M){ if(mx>v[i]){cout<<-1<<endl;return 0;} p[i]={v[i],l[i]}; } sort(p.begin(),p.end()); rep(i,M-1){ chmax(p[i+1].second,p[i].second); } vector<int>x(N); iota(x.begin(),x.end(),0); ll ans=INF; do{ vector<ll>dp(N); rep(i,N){ ll now=0; for(int j=i;j<N;j++){ now+=w[x[j]]; int e=lower_bound(p.begin(),p.end(),P(now,0))-p.begin(); if(e>0){chmax(dp[j],dp[i]+p[e-1].second);} } } chmin(ans,dp[N-1]); }while(next_permutation(x.begin(),x.end())); cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define readFast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define fin cin #define ll long long #define sz(x) (int)(x).size() #define all(v) v.begin(), v.end() #define output(x) ((int)x && cout << "YES\n") || cout << "NO\n"; using namespace std; #ifdef LOCAL #define read() ifstream fin("date.in.txt"); #else #define read() readFast; #endif const int N = 10; const int M = 1e5 + 5; int n, m; ll v[N], wMax, l, w, wAct; ll ans = LLONG_MAX, drum; ll dist[N][N]; vector<pair<ll,ll>> restrictii; void searchMaxDistance(int act, ll d) { if(act == n) { drum = max(drum, d); return; } for (int i = act + 1; i <= n; ++i) { searchMaxDistance(i, d + dist[act][i]); } } bool cmp(pair<ll,ll> a, pair<ll, ll> b) { if(a.first == b.first) return a.second > b.second; return a.first < b.first; } int main() { read(); fin >> n >> m; for (int i = 1; i <= n; ++i) { fin >> v[i]; wMax = max(wMax, v[i]); } for (int i = 1; i <= m; ++i) { fin >> l >> w; if(w < wMax) { cout << -1; return 0; } restrictii.push_back({w, l}); } restrictii.push_back({0, 0}); sort(restrictii.begin(), restrictii.end(), cmp); ll mx = 0; for (int i = 0; i < sz(restrictii); ++i) { mx = max(mx, restrictii[i].second); restrictii[i].second = mx; } vector<int> p(n + 1); iota(p.begin(), p.end(), 0); do { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) dist[i][j] = 0; } //for (int i = 1; i <= n; ++i) // cout << p[i] << " "; //cout << '\n'; for (int i = 1; i < n; ++i) { wAct = v[p[i]]; for (int j = i + 1; j <= n; ++j) { wAct += v[p[j]]; int posNeed = upper_bound(restrictii.begin(), restrictii.end(), make_pair(wAct, 0ll)) - restrictii.begin(); --posNeed; dist[i][j] = restrictii[posNeed].second; } } /*for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << dist[i][j] << " " ; } cout << '\n'; } cout << '\n'; */drum = 0; searchMaxDistance(1, 0); ans = min(ans, drum); //break; } while(next_permutation(p.begin() + 1, p.end())); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 62; int mod = 1000000007; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int A[4]; rep(i, 4) cin >> A[i]; sort(A, A+4); cout << A[0] << endl; return 0; }
#include<iostream> #include<algorithm> using namespace std; bool cmp(int x,int y){ return x>y; } int main() { int n; cin>>n; int ans=0; int a,b,max=0,min=1100; for(int i=0;i<n;i++){ cin>>a; if(a>=max) max=a; } for(int i=0;i<n;i++){ cin>>b; if(b<=min) min=b; } if(max>min) ans=0; else{ ans=min-max+1; } cout<<ans; 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.at(i); bool change = false; if(N == 1) cout << A.at(0) << endl; else { int mod; int j; int max; int min; for( ; ; ) { max = A.at(0); min = A.at(0); mod = A.at(0); j = 0; for(int i = 1; i < N; i++) { if(max < A.at(i)) max = A.at(i); else if(min > A.at(i)) min = A.at(i); if(mod > A.at(i)) { mod = A.at(i); j = i; } } if(max == min) break; for(int i = 0; i < N; i++) { if(i != j) A.at(i) = A.at(i) %= mod; if(A.at(i) == 0) A.at(i) += mod; } } cout << min << endl; } }
#include <bits/stdc++.h> using ull = unsigned long long int; using ll = long long; using ld = long double; using pii = std::pair<int,int>; using pll = std::pair<ll, ll>; using vi = std::vector<int> ; using vvi = std::vector<vi> ; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double> ; using vvd = std::vector<vd> ; using qi = std::queue<int> ; using vpii = std::vector<std::pair<int, int> >; using vpll = std::vector<pll>; using namespace std; #define rep(i,j) for(int (i)=0;(i)<(j);(i)++) #define drep(i,j) for(int (i)=(j);(i) >= 0;(i)--) template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;} template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;} template<class T> inline void pri(T a){cout << a << endl;} template<class Z> using vec = vector<Z>; template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; inline void IN(void){ return; } template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; } inline void OUT(void){ cout << endl; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest){ cout << first << " " ; OUT(rest...); return; } const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; const int INF = int(1e9); const long long int INFL = LLONG_MAX; int n,m,k; string S; int ans; vll A; ll f(ll mod,ll n,ll x) { ll dp[n+1][mod+1][mod];// memset(dp,-1,sizeof(dp)); dp[0][0][0] = 0; rep(i,n) { rep(j,mod+1) { rep(k,mod) { if(dp[i][j][k] < 0)continue; chmax(dp[i+1][j][k],dp[i][j][k]); if(j<mod)chmax(dp[i+1][j+1][(k+A[i])%mod],dp[i][j][k] + A[i]); } } } x = x%mod; return dp[n][mod][x]; } void solve() { ll N; ll X; IN(N,X); A.resize(N); rep(i,N)IN(A[i]); ll ans = INFL; for(ll i=1;i<=N;i++) { ll tmp = f(i,N,X); //OUT(tmp); if(tmp>0)chmin(ans,(X - tmp)/i); } cout << ans << endl; } signed main (int argc, char* argv[]) { cin.tie(0); ios::sync_with_stdio(false); int cases=1; //IN(cases); while(cases--)solve(); //pri(ans); //for(auto c : ans){cout << c << endl;} //cout << fixed << setprecision(15) << ans << endl; return 0; }
/*input 18 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; typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; //order_of_key #of elements less than x // find_by_order kth element typedef long long int ll; #define ld double #define pii pair<int,int> #define f first #define s second #define pb push_back #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define FILL(n,x) memset(n,x,sizeof(n)) #define ALL(_a) _a.begin(),_a.end() #define sz(x) (int)x.size() const ll maxn=18; const ll maxlg=__lg(maxn)+2; const ll INF64=4e18; const int INF=0x3f3f3f3f; const ll MOD=1e9+7; const ld PI=acos(-1); const ld eps=1e-9; #define lowb(x) x&(-x) #define MNTO(x,y) x=min(x,(__typeof__(x))y) #define MXTO(x,y) x=max(x,(__typeof__(x))y) #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 MP make_pair ll mult(ll a,ll b){ ll res=0LL; while(b){ if(b&1) res=(res+a)%MOD; a=(a+a)%MOD; b>>=1; } return res%MOD; } ll mypow(ll a,ll b){ ll res=1LL; while(b){ if(b&1) res=res*a%MOD; a=a*a%MOD; b>>=1; } return res; } int ed[maxn][maxn]; bool vis[maxn]; int dp[(1<<18)]; inline int rec(int msk){ if(dp[msk]!=-1) return dp[msk]; vector<int> v; int z=msk; while(z){ v.pb(__lg(lowb(z))); z-=lowb(z); } bool cl=1; REP(i,sz(v)){ REP(j,i){ if(!ed[v[i]][v[j]]){ cl=0; break; } } if(!cl) break; } if(cl){ return dp[msk]=1; } dp[msk]=INF; for(int s=msk&(msk-1);s;s=(s-1)&msk){ MNTO(dp[msk],rec(msk^s)+rec(s)); } return dp[msk]; } int main(){ ios::sync_with_stdio(false),cin.tie(0); int n,m; cin>>n>>m; REP(i,m){ int a,b; cin>>a>>b; --a; --b; ed[a][b]=ed[b][a]=1; } REP(i,(1<<n)) dp[i]=-1; cout<<rec((1<<n)-1); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define f first #define s second #define pb push_back #define all(x) x.begin(), x.end() #define mp(x, y) make_pair(x, y) #define sz(li) (int) (li).size() #define endE(li) li[sz(li) - 1] #define vi vector<int> #define vl vector<ll> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define pii pair<int, int> #define pll pair<ll, ll> #define vpii vector<pair<int, int>> #define vpll vector<pll> template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v); template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; } template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "[ "; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << " ]"; } template<typename A> ostream& operator<<(ostream &cout, vector<vector<A>> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) { cout << " "; } cout << v[i]; if (i < v.size() - 1) { cout << endl; } } return cout << "]"; } template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } void solve() { ll x, y, z; cin >> x >> y >> z; if (y % x == 0) { cout << z * (y / x) - 1 << endl; } else { int price = (((y * 1.0) / x) * z); if ((price * 1.0) / z >= ((y * 1.0) / x)) { price--; } cout << price << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <tuple> #include <iomanip> #include <random> #include <math.h> #include <stdio.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; void display_2int(vector<vector<int>> vec) { int N = vec.size(); for (int i=0; i<N; i++) { for (int j=0; j<vec.at(i).size(); j++) { cout << vec.at(i).at(j); if (j < vec.at(i).size()-1) { cout << " "; } } cout << endl; } } void display_1string(vector<string> strlist) { int N = strlist.size(); for (int i=0; i<N; i++) { cout << strlist.at(i) << endl; } } void display_1int(vector<int> intlist) { int N = intlist.size(); if (N>0){ for (int i=0; i<N - 1; i++) { cout << intlist.at(i) << " "; } cout << intlist.at(N-1) << endl; }else{ cout << endl; } } void display_1ll(vector<long long> lllist) { long long N = lllist.size(); if (N>0){ for (int i=0; i<N - 1; i++) { cout << lllist.at(i) << " "; } cout << lllist.at(N-1) << endl; }else{ cout << endl; } } void display_1pii(vector<pair<int, int>> plst){ for (int i=0; i<plst.size(); i++){ cout << plst.at(i).first << " " << plst.at(i).second << endl; } } int ctoi(char c) { if (c>='0' && c<='9'){ return c - '0'; } return -1; } ll count(ll N){ string S = to_string(N); ll k = S.size(); ll toridasi = (k-1)%3 + 1; ll rot = (k-1)/3; if (rot==0){ return 0LL; } string s = ""; for (int i=0; i<toridasi; i++){ s += S.at(i); } ll n = stoll(s); string s1 = ""; for (int i=toridasi; i<k; i++){ s1 += S.at(i); } ll n1 = stoll(s); ll ans = (N - pow(1000LL, rot)+1LL) * rot + count(pow(1000LL, rot)-1LL); return ans; } int main(){ ll N; cin >> N; cout << count(N) << endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i < n; ++i) using namespace std; typedef long long ll; const int INF = 100100100; int main() { ll n; cin >> n; ll ans=0; if(n>=1000) ans+=n-999; if(n>=1000000) ans+=n-999999; if(n>=1000000000) ans+=n-999999999; if(n>=1000000000000) ans+=n-999999999999; if(n>=1000000000000000) ans+=n-999999999999999; cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define l(i,a,n)for(int i=a;i<n;i++) #define pb push_back #define in insert #define mp make_pair #define lw(v) sort(v.begin(),v.end()); #define hi(v) sort(v.begin(),v.end(),greater<long long>()); #define all(v) v.begin(),v.end() #define filein freopen ("input.txt", "r", stdin) #define fileout freopen ("output.txt", "w", stdout) using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); long long t,r=1,r1=0,r2=0,k=0,a,b,c=0,m,d=0,n,e,f,x=0,g,p=0,q=0,y=0,z=0; vector<long long>v; vector<long long>u; vector<tuple<string,int, int>>mp; set<long long>s; std::vector<int>::iterator it; string s1,s2,s3,s4; cin>>n>>m; char arr[n+1][m+1]; l(i,0,n) { l(j,0,m) { cin>>arr[i][j]; } } l(i,0,n) { l(j,0,m) { if(arr[i][j]=='.'&&arr[i][j+1]=='.') { c++; } if(arr[i][j]=='.'&&arr[i+1][j]=='.') { c++; } } } cout<<c<<endl; }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for (int i=0;i<(int)(n);i++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define vector1d(type,name,...) vector<type>name(__VA_ARGS__) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=(ll)1<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} int main(){ LL(n); vector<ll> yakusuu; for(ll i=1;i*i<=n;i++){ if(n%i==0){ yakusuu.push_back(i); if(n/i!=i)yakusuu.push_back(n/i); } } sort(all(yakusuu)); rep(i,yakusuu.size())out(yakusuu[i]); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } #define all(x) (x).begin(),(x).end() #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=998244353,MAX=200005,INF=1<<30; ll C[10005][105]; int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); int N;cin>>N; string S;cin>>S; vector<ll> A(N+1),B(N+1); for(int i=0;i<N+1;i++) cin>>A[i]; int left=1,right=10001; while(right-left>1){ int mid=(left+right)/2; for(int t=0;t<mid;t++){ for(int i=0;i<N+1;i++){ C[t][i]=A[i]/mid; if(t<A[i]%mid) C[t][i]++; } } bool ok=true; for(int t=0;t<mid;t++){ for(int i=0;i<N;i++){ if(S[i]=='<'){ if(C[t][i]>=C[t][i+1]) ok=false; }else{ if(C[t][i]<=C[t][i+1]) ok=false; } } } if(ok) left=mid; else right=mid; } int mid=left; cout<<mid<<endl; for(int t=0;t<mid;t++){ for(int i=0;i<N+1;i++){ C[t][i]=A[i]/mid; if(t<A[i]%mid) C[t][i]++; cout<<C[t][i]<<" "; } cout<<"\n"; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <queue> #include <string> #include <map> #include <set> #include <stack> #include <tuple> #include <deque> #include <array> #include <numeric> #include <bitset> #include <iomanip> #include <cassert> #include <chrono> #include <random> #include <limits> #include <iterator> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <cstring> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template<class T> inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template<class T> inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; string S; cin >> N >> S; vector<int> A(N + 1), d(N); for(int i = 0; i <= N; ++i){ cin >> A[i]; if(i) d[i - 1] = abs(A[i] - A[i - 1]); } int k = *min_element(begin(d), end(d)); cout << k << '\n'; for(int j = 0; j < k; ++j){ for(int i = 0; i <= N; ++i){ cout << (A[i] + j) / k << (i == N ? '\n' : ' '); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; template<typename T> using V = vector<T>; template<typename T> using P = pair<T, T>; template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); } template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); } template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;} template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());} template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++) {cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}}; template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0]; for(ll i=1;i<n;i++)cerr spa v[i]; cerr<<"\n";}; const ll INF = (1ll<<62); // const ld EPS = 1e-10; // const ld PI = acos(-1.0); const ll mod = (int)1e9 + 7; //const ll mod = 998244353; int main(){ ll N; cin >> N; V<ll> A(N), B(N); REP(i, N) cin >> A[i] >> B[i]; ll res = INF; REP(i, N) chmin(res, A[i]+B[i]); REP(i, N){ REP(j, N){ if(i != j){ chmin(res, max(A[i], B[j])); } } } cout << res << endl; return 0; }
#include<bits/stdc++.h> #define ll long long int #define lb long long double #define mp make_pair #define MOD 1000000007 #define pb push_back #define ppi pair<ll,pair<ll,ll>> #define F(i,n) for(ll i=0;i<n;i++) #define w(t) ll t;cin >> t;while(t--) using namespace std; vector<ll> divisor; void getDivisors(ll n) { for (ll i=1; i<=sqrt(n); i++) { if (n%i == 0) { // If divisors are equal, take only one if (n/i == i) divisor.push_back(i); else // Otherwise take both { divisor.push_back(i); divisor.push_back(n/i); } } } } int main() { //w(t) { //cout << "Codeforces Higher rating" << endl; ll n; cin >> n; vector<ll> a(n),b(n); for(int i = 0 ; i < n ; i = i + 1){ cin >> a[i] >> b[i]; } if(n == 1) { cout << a[0] + b[0] << endl; return 0; } int min1ind = min_element(a.begin(),a.end()) - a.begin(); int min2ind = min_element(b.begin(),b.end()) - b.begin(); int x = (a[min1ind] + b[min2ind]); if(min1ind != min2ind) cout << max(a[min1ind],b[min2ind]) << endl; else { sort(a.begin(),a.end()); sort(b.begin(),b.end()); cout << min(x,(int)min(max(a[0],b[1]),max(a[1],b[0]))) <<endl; } } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) using namespace std; using ll = long long; const int INF = 1e9; const ll LINF = 1e18; template <class T> void get_unique(vector<T>& x) { x.erase(unique(x.begin(), x.end()), x.end()); } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < sz(v) - 1) os << ' '; } return os; } struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) { } int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool find(int x, int y) { return root(x) == root(y); } int size(int x) { return -data[root(x)]; } }; int main() { int h, w; cin >> h >> w; vector<string> s(h); cin >> s; s[0][0] = s[0][w - 1] = s[h - 1][0] = s[h - 1][w - 1] = '#'; vector<vector<int>> ys(w), xs(h); rep(i, h) rep(j, w) { if (s[i][j] == '#') { ys[j].push_back(i); xs[i].push_back(j); } } int ans = INF; UnionFind uf(h + w); rep(y, h) rep(i, sz(xs[y]) - 1) { uf.unite(xs[y][i], xs[y][i + 1]); } rep(x, w) rep(i, sz(ys[x]) - 1) { uf.unite(ys[x][i] + w, ys[x][i + 1] + w); } int sz_y = 0, sz_x = 0; rep(i, w) sz_x += uf.root(i) == i; rep(i, h) sz_y += uf.root(i + w) == i + w; cout << min(sz_y - 1, sz_x - 1) << '\n'; }
#include<bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops,fast-math") #include <ext/pb_ds/tree_policy.hpp> // #include<ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; //using namespace __gnu_pbds; #define pb push_back #define ins insert #define var auto #define F first #define S second #define mp make_pair using namespace std; typedef long long ll; //#define int ll typedef pair<int,int> pii; template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; const int Max = 1000 + 10; const int LOG = 20; const int Mod = 1e9 + 7; const int SQ = 100000; const ll INF = 1e18; const int BMSK = 4e6; ll pw(ll a , int b = Mod - 2) { if(b == 0) return 1; ll half = pw(a , b / 2); if(b % 2) return half * half % Mod * a % Mod; return half * half % Mod; } char S[Max][Max]; vector<pair<int , int> > N[Max][Max]; int val[Max][Max]; bool seen[Max][Max]; int DFS(int I , int J) { seen[I][J] = true; int sum = val[I][J]; for(pair<int , int> u : N[I][J]) { int i = u.F , j = u.S; if(!seen[i][j]) sum += DFS(i , j); } return sum; } void solve() { int h , w; cin >> h >> w; for(int i = 1 ; i <= h ; i++) { for(int j = 1 ; j <= w ; j++) { cin >> S[i][j]; if(i == 1 || i == h) if(j == 1 || j == w) S[i][j] = '#' , val[i][j] = 1; if(S[i][j] == '#') { N[i][j].pb(mp(0 , j)); N[i][j].pb(mp(i , 0)); N[0][j].pb(mp(i , j)); N[i][0].pb(mp(i , j)); } } } int ans1 = 0; for(int i = 1 ; i <= h ; i++) { if(!seen[i][0]) { ans1 += DFS(i , 0) == 0; } } for(int i = 0 ; i <= h ; i++) for(int j = 0 ; j <= w ; j++) seen[i][j] = 0; int ans2 = 0; for(int j = 1 ; j <= w ; j++) { if(!seen[0][j]) { ans2 += DFS(0 , j) == 0; } } cout << min(ans1 , ans2) << '\n'; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << '\n'; //int t;cin >> t; //while(t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++)//iをaからn #define per(i,n,a) for (int i=a-1;i>=n;i--)//iをnからa #define db(x) cout << #x << " = " << x << endl #define db2(x, y) cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; //デバッグ用 #define all(x) (x).begin(), (x).end() #define INF 1000000000000 //10^12:∞ #define MOD 1000000007 //10^9+7:合同式の法 #define pb push_back #define F first #define S second typedef long long ll; //sort(all(a)); ソート //sort(all(a),greater<int>()); 逆順ソート //a.erase(unique(all(a)), a.end()); ソート後に重複を削除 //cout<< std::fixed << std::setprecision(15)<<0.000001<<endl; 小数点以下を表示させる //vector<vector<int>> data(3, vector<int>(4)); int型の2次元配列(3×4要素の)の宣言 int main() { ll x,y; cin>>x>>y; if(x==y){ cout<<x; }else{ cout<<3-x-y; } return 0; }
#include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ int x=0, y=0; cin>>x>>y; int ans=0; if(x==y){ ans=x; }else{ ans=3-x-y; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; const auto maxn = int(1e5 + 5); struct edge { int to, c, next; edge(int v, int tag, int nxt) : to(v), c(tag), next(nxt) {} }; auto ans = vector<int>(maxn, -1); auto visited = vector<bool>(maxn, false); auto es = vector<edge>(); auto head = vector<int>(maxn, -1); auto color = 1; auto n = 0, m = 0; void dfs(int p, int e, int u) { if (ans[p] != e) { ans[u] = e; } else { while (color == e) { color++; if (color > n) color = 1; } ans[u] = color++; } for (auto v = head[u]; v != -1; v = es[v].next) { if (!visited[es[v].to]) { visited[es[v].to] = true; dfs(u, es[v].c, es[v].to); } } } auto main() -> int { cin >> n >> m; auto u = 0, v = 0, c = 0, cnt = 0; for (auto i = 0; i < m; ++i) { cin >> u >> v >> c; es.emplace_back(v, c, head[u]); head[u] = cnt++; es.emplace_back(u, c, head[v]); head[v] = cnt++; } visited[1] = true; dfs(0, -1, 1); for (auto i = 1; i <= n; ++i) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++) #define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--) #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define eb emplace_back using ll = long long; using vll = vector<ll>; using vi = vector<int>; using vvi = vector<vector<int>>; using P = pair<int, int>; using LD = long double; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; } void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; } void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; } struct Edge{ int to, color, id; }; int n, m; vi ans; vector<vector<Edge>> G; void dfs(int v = 0, int p = -1){ for(auto [u, co, id] : G[v]){ if(ans[u] != -1) continue; if(ans[v] == co){ ans[u] = co % n + 1; }else{ ans[u] = co; } dfs(u, v); } } void Main(){ cin >> n >> m; G.resize(n); rep(i, m){ int u, v, c; cin >> u >> v >> c; u--; v--; G[v].pb({u, c, i}); G[u].pb({v, c, i}); } ans.resize(n, -1); ans[0] = 1; dfs(); rep(i, n) cout << ans[i] << endl; return; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define vll vector<ll> #define pll pair<ll,ll> #define vpll vector<pll> #define ub upper_bound #define lb lower_bound #define all(v) ((v).begin()),((v).end()) #define allr(v) ((v).rbegin()),((v).rend()) #define ff first #define ss second #define mp make_pair #define pll pair<ll,ll> #define fo(i,n) for(ll i=0;i<n;i++) #define foa(i,s,e) for(ll i=(s);i<=e;i++) #define fod(i,s,e) for(ll i= (s);i>=(e);i--) #define max3(a,b,c) max(max(a,b),c) #define min3(a,b,c) min(min(a,b),c) #define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n' #define sz(x) (ll)(x.size()) #define ANS cout<<ans<<'\n' #define YES cout<<"YES\n" #define NO cout<<"NO\n" #define Yes cout<<"Yes\n" #define No cout<<"No\n" const ld pi = 3.14159265358979323846; ll MOD = 1e9 + 7; //ll MOD = 998244353; const char nl = '\n'; const ll inf = 1e15; #define fill(a,b) memset(a, b, sizeof(a)) #define setbits(x) __builtin_popcountll(x) #define print2d(dp,n,m) fo(i, n){fo(j, m)cout<<dp[i][j]<<" ";cout<<"\n";} ll nxt(){ll x; cin >> x; return x;} ll mul(ll x,ll y){ return (1ll* (x%MOD)*(y%MOD)); } ll modpow(ll x,ll y){ll z=1;while(y>0){if(y%2)z=mul(z,x);x =mul(x,x) ;y/=2;}return z;} ll power(ll x,ll y){ll z=1;while(y>0){if(y%2)z=z*x;x =x*x ;y/=2;}return z;} ll gcd(ll a,ll b){if(a<b) return gcd(b,a);if(b==0) return a;return gcd(b,a%b);} ll min(ll a,ll b){if(a<b)return a;return b;} ll max(ll a,ll b){if(a>b)return a;return b;} ll sq(ll a){ ll ans = (1ll*a*a); return ans; } void solve(){ ll n,x; cin >> n >> x; fo(i, n){ ll y; cin >> y; if(y!=x){ cout << y << " "; } } } int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); #ifndef ONLINE_JUDGE freopen("inputf.txt" , "r" , stdin) ; freopen("outputf.txt" , "w" , stdout) ; freopen("error.txt" , "w" , stderr) ; #endif ll TC = 1; //cin>>TC; fo(TT, TC){ // Google,FB cout << "Case #"<< TT + 1 << ": "; solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <iostream> using namespace std; int main(){ int n,w; cin >> n >> w; cout << n/w; }
#include <bits/stdc++.h> #define mp std::make_pair #define pb push_back #define fr first #define se second typedef std::pair<int, int> pii; typedef std::vector<int> vi; typedef long long ll; template<class T> inline T max(const T &x, const T &y) { return x > y ? x : y; } template<class T> inline T min(const T &x, const T &y) { return x < y ? x : y; } template<class T> inline bool chkmax(T &x, const T &y) { return x < y ? x = y, true : false; } template<class T> inline bool chkmin(T &x, const T &y) { return x > y ? x = y, true : false; } inline int read() { register int x = 0, v = 1; register char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') v = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * v; } const int MAXN = 2e5; int n, pt1, pt2, tim1, tim2; int fa[22][MAXN | 1], par[MAXN | 1]; int dfn1[MAXN | 1], dfn2[MAXN | 1], size[MAXN | 1], dep[MAXN | 1]; int ans[MAXN | 1]; vi e[MAXN | 1]; pii a[MAXN | 1]; int find(int x) { static int dep[MAXN | 1]; std::queue<int> q; memset(dep, 0, sizeof(dep)); int res = 0; q.push(x); dep[x] = 1; while (!q.empty()) { int frm = q.front(); q.pop(); for (vi::iterator it = e[frm].begin(); it != e[frm].end(); ++it) { int to = *it; if (dep[to]) continue; dep[to] = dep[frm] + 1; q.push(to); if (dep[to] > dep[res]) res = to; } } return res; } void dfs1(int x, int par) { fa[0][x] = par; dep[x] = dep[par] + 1; dfn1[x] = ++tim1; size[x] = 1; for (int i = 1; i <= 20; ++i) fa[i][x] = fa[i - 1][fa[i - 1][x]]; for (vi::iterator it = e[x].begin(); it != e[x].end(); ++it) { int to = *it; if (to == par) continue; dfs1(to, x); size[x] += size[to]; } } void dfs2(int x, int fa) { dfn2[x] = ++tim2; for (vi::iterator it = e[x].begin(); it != e[x].end(); ++it) { int to = *it; if (to == fa) continue; if (dfn1[to] <= dfn1[pt2] && dfn1[to] + size[to] - 1 >= dfn1[pt2]) continue; dfs2(to, x); } for (vi::iterator it = e[x].begin(); it != e[x].end(); ++it) { int to = *it; if (to == fa) continue; if (!(dfn1[to] <= dfn1[pt2] && dfn1[to] + size[to] - 1 >= dfn1[pt2])) continue; dfs2(to, x); } } int lca(int x, int y) { if (dep[x] < dep[y]) std::swap(x, y); for (int i = 20; ~i; --i) if (dep[fa[i][x]] >= dep[y]) x = fa[i][x]; if (x == y) return x; for (int i = 20; ~i; --i) if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y]; return fa[0][x]; } int dist(int x, int y) { if (!y) return 0; int res = 0, lx = x, ly = y, Lca = lca(x, y); return dep[x] + dep[y] - 2 * dep[Lca]; } int main() { n = read(); for (int i = 1; i < n; ++i) { int u = read(), v = read(); e[u].pb(v); e[v].pb(u); } pt1 = find(1); pt2 = find(pt1); dfs1(pt1, 0); dfs2(pt1, 0); for (int i = 1; i <= n; ++i) a[i].fr = dfn2[i], a[i].se = i; std::sort(a + 1, a + 1 + n); for (int i = 1, res = 1; i <= n; ++i) { res += dist(a[i].se, a[i - 1].se); ans[a[i].se] = res; } for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; #define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ff first #define ss second #define int long long #define ll long long #define pb push_back #define pii pair<int,int> #define vi vector<int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define vpii vector<pair<int,int> > #define all(x) x.begin(),x.end() #define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";} #define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n" #define sz(x) (int)x.size() #define db(x) cout<<x<<"\n"; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //insert,find_by_order,order_of_key,lower_bound,upper_bound; #define TRACE #ifdef TRACE #define deb(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define deb(...) #endif //////////////////////////////code////////////////////////////// const int N = 2e5 + 5; int ans , n; int dp[N]; vpii adj[N]; void dfs(int node,int p,int w) { dp[node] = w; for(auto &c :adj[node]) { if(c.ff == p) continue; dfs(c.ff , node , c.ss ^ dp[node]); } } void solve() { cin >> n; for(int i = 0;i < n - 1; i++) { int u , v , w; cin >> u >> v >> w; adj[u].pb({v , w}); adj[v].pb({u , w}); } ans = 0; dfs(1 , -1 , 0); int bit[62] = {}; for(int i = 1;i <= n ; i++) { int num = dp[i]; int j = 0; while(num) { if(num&1) bit[j]++; num>>=1; j++; } } int curr = 1; for(int i = 0;i < 62 ; i++) { int now = bit[i]; now *= (n - bit[i]); now%=mod; int p = curr%mod; now*=p; now%=mod; ans += now; ans%=mod; // deb(ans, now); curr<<=1; } db(ans) } int32_t main() { LETS_GET_SCHWIFTY; #ifndef ONLINE_JUDGE freopen("INP.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t = 1; //cin >> t; while (t--) solve(); } // check out for following mistakes- // if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only // is using prime sieve make sure it fits // when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) begin(v), end(v) using namespace std; using P = pair<int,int>; using ll = long long; using vi = vector<int>; using vll = vector<ll>; ll ll_pow(ll a, ll n) { ll ans = 1; for(ll i = 0; i < n; i++) ans *= a; return ans; } int main() { int n; ll ans = 0; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> c(n); vector<int> cnt(n+1); vector<int> cnt_s(n+1); rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; rep(i,n) cin >> c[i]; rep(i,n){ cnt_s[b[c[i]-1]]++; } rep(i,n){ ans += cnt_s[a[i]]; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define vi vector<ll> #define pb push_back #define ff first #define ss second #define inf 2e18 #define ull unsigned long long #define pi acos(-1.0) #define mod 1000000007 #define lop0(n) for(ll i=0;i<n;i++) #define lop(j,n) for(ll j=0;j<n;j++) #define lop1(n) for(ll i=1;i<=n;i++) #define all(v) v.begin(),v.end() ll Set(ll N,ll pos){ return N=N | (1LL<<pos); } ll reset(ll N,ll pos){ return N= N & ~(1LL<<pos); } bool check(ll N,ll pos){ return (bool)(N & (1LL<<pos)); } ll dx[] = { 1,0,-1,0 }; ll dy[] = { 0,1,0,-1 }; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ll pos[200005],arr[200005]; main() { ll n; cin>>n; if(n<0) n=0; cout<<n; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < (t); ++i) #define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cerr<<#x<<" = "<<x<<endl; #define bn(x) ((1<<(x))-1) #define newline puts("") using namespace std; template<typename T> using vc = vector<T>; template<typename T> using vv = vc<vc<T>>; template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >; using ll = long long; using uint = unsigned; using ull = unsigned long long; using P = pair<int,int>; using T3 = tuple<int,int,int>; using vi = vc<int>; using vvi = vv<int>; using vl = vc<ll>; using vp = vc<P>; using vt = vc<T3>; int getInt(){int x;scanf("%d",&x);return x;} vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;} template<typename T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();} template<typename T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;} template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;} template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;} vc<string> split(const string& s,char d=' '){vc<string> r(1);for(char c:s)if(c==d)r.pb("");else r.back()+=c;return r;} string operator*(const string& s,int t){return join(vc<string>(t,s));} template<typename T1,typename T2>bool mins(T1& x,const T2&y){if(x>y){x=y;return true;}else return false;} template<typename T1,typename T2>bool maxs(T1& x,const T2&y){if(x<y){x=y;return true;}else return false;} template<typename T>T dup(T x, T y){return (x+y-1)/y;} template<typename T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;} template<typename T>void uni(T& a){sort(rng(a));a.erase(unique(rng(a)),a.end());} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; struct Solver { void solve() { int n; scanf("%d",&n); vi a(n); cin>>a; sort(rng(a)); double c = -n/2.; double s = 0; rep(i,n) s += a[i]; double ans = 1e18; rep(i,n) { mins(ans, s+c*a[i]); s -= a[i]; c += 1; mins(ans, s+c*a[i]); } ans /= n; printf("%.10f\n",ans); } }; int main() { int ts = 1; // scanf("%d",&ts); rrep(ti,ts) { Solver solver; solver.solve(); } return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<tuple> #include<cmath> #include<iomanip> using namespace std; typedef long long ll; typedef vector<ll> v; typedef vector<vector<ll>> vv; #define MOD 1000000007 #define INF 1001001001001001 #define MIN -1001001001 #define rep(i,k,N) for(int i=k;i<N;i++) #define MP make_pair #define MT make_tuple //tie,make_tuple は別物 #define PB push_back #define PF push_front #define all(x) (x).begin(),(x).end() int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; int main(){ ll N; cin>>N; v A(N); rep(i,0,N)cin>>A[i]; sort(all(A)); v sA(N,0); sA[0]=A[0]; rep(i,1,N)sA[i]=sA[i-1]+A[i]; ll ans = INF; rep(i,0,N){ ans = min(ans,N*A[i]-2*sA[i]-2*A[i]*(N-i-1)); } ans += 2*sA[N-1]; cout<<fixed<<setprecision(10)<<double(ans)/2/N; return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <map> #include <queue> #include <set> #include <cmath> #include <queue> #include <unordered_map> #include <unordered_set> #include <sstream> #include <iomanip> using namespace std; using VI = vector <int>; using VVI = vector <VI>; using VVVI = vector <VVI>; using VLL = vector <long long>; using VVLL = vector <VLL>; using UMI = unordered_map<int, int>; using UMLL = unordered_map<long long, long long>; template<class T> class FenwickTree { vector <T> sum; public: FenwickTree (int treeSize) { sum = move(vector<T>(treeSize+1)); } void addVal(int idx, T val) { while (idx < sum.size()) { sum[idx] += val; idx += (idx & (-idx)); } } T getVal(int idx) { if (idx <= 0) { return 0; } T ret = 0; while (idx) { ret += sum[idx]; idx -= (idx & (-idx)); } return ret; } T queryRange(int a, int b) { if (b < a) { return 0; } return getVal(b) - getVal(a-1); } }; int main() { int N; long long K; cin >> N >> K; // --K; vector <FenwickTree<long long>> ft; ft.push_back(FenwickTree<long long>(N+10)); ft.push_back(FenwickTree<long long>(N*2+10)); ft.push_back(FenwickTree<long long>(N*3+10)); for (int i = 1; i <= N; ++i) { ft[0].addVal(i, 1); } for (int i = 1; i < 3; ++i) { // dp[i][s] = sigma j = 1...N dp[i-1][s-j] int upper = (i+1)*N; int preupper = i*N; for (int s = i+1; s <= upper; ++s) { int t = min(s-1, preupper); int f = max(i, s-N); ft[i].addVal(s, ft[i-1].queryRange(f, t)); } } int low = 3; int up = 3*N; int smax = 2; while (low <= up) { int mid = (low+up) / 2; long long comb = ft[2].queryRange(3, mid); if (comb < K) { low = mid+1; smax = mid; } else { up = mid-1; } } // cout << "SMAX " << smax << endl; K -= ft[2].queryRange(3, smax); --K; // cout << "DEBUG " << K << endl; int target = smax+1; VLL ans(3, 1); long long comb = 0; for (int v = 1; v <= N && v <= target-2; ++v) { int remain = target-v; long long diff = min(remain-1, N) - max(0, remain-N-1); long long newComb = remain <= 2*N && diff > 0 ? comb + diff : comb; // if (v >= 999900) { // cout << "V " << v << " " << newComb << endl; // } if (newComb > K || v == N || v == target-2) { ans[0] = v; K -= comb; break; } comb = newComb; } int remain = target-ans[0]; int start = max(1, remain-N); ans[1] = min(start + K, (long long)N); ans[2] = remain-ans[1]; cout << ans[0] << " " << ans[1] << " " << ans[2] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, k, d = 1, i = 3, j = 1, u; bool f = 0; int main() { cin >> n >> k; if (k > (n * n * n + 1) / 2) { k = n * n * n + 1 - k; f = 1; } while (k > d) { k -= d; i++; if (i < n + 3) d = (i - 2) * (i - 1) / 2; else d = (i - 2) * (i - 1) / 2 - (i - n - 2) * (i - n - 1) * 3 / 2; } if (i < n + 3) d = i - 2; else d = n * 2 - i + 2; while (d > 0 && k > d) { k -= d; j++; if (i > n + 2 && j <= i - n - 1) d++; else d--; } if (i > n + 2 && j <= i - n - 1) k += i - n - 1 - j; u = i - j - k; if (f) { j = n + 1 - j; k = n + 1 - k; u = n + 1 - u; } cout << j << " " << k << " " << u << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define MOD 1000000007 // 1-indexed const int N = 1000010; vector<ll> bit(N); // a[p] ^= x void add(int p, int x) { for (int i = p; i <= N; i += i & -i) { bit[i] ^= x; } } // a[1] ^ a[2] ^ ... ^ a[p] ll sum(int p) { ll res = 0; for (int i = p; i > 0; i -= i & -i) { res ^= bit[i]; } return res; } int main() { int n, q; cin >> n >> q; vector<ll> a(n + 1); for (int i = 1; i <= n; ++i) { cin >> a[i]; add(i, a[i]); } while (q--) { int t, x, y; cin >> t >> x >> y; if (t == 1) { add(x, y); } if (t == 2) { cout << (sum(y) ^ sum(x - 1)) << endl; } } return 0; }
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cmath> #include <tuple> #define int long long #define rep(i, n) for(i = 0; i < n; i++) using namespace std; const int DEPTH = 19; class SegTree { public: int val[1 << (DEPTH + 1)]; SegTree() { for (int i = 0; i < (1 << (DEPTH + 1)); i++) { val[i] = 0; } } void update(int pos, int x) { pos += (1 << DEPTH) - 1; val[pos] ^= x; while (pos > 0) { pos = (pos - 1) / 2; val[pos] = val[2 * pos + 1] ^ val[2 * pos + 2]; } } int query(int l, int r, int a = 0, int b = (1 << DEPTH), int id = 0) { if (a >= r || b <= l) return 0; if (l <= a && b <= r) return val[id]; int res1 = query(l, r, a, (a + b) / 2, id * 2 + 1); int res2 = query(l, r, (a + b) / 2, b, id * 2 + 2); return res1 ^ res2; } }; SegTree seg; int n, q; int a[300000]; int t, x, y; signed main() { int i; cin >> n >> q; rep(i, n) cin >> a[i]; rep(i, n) seg.update(i, a[i]); rep(i, q) { cin >> t >> x >> y; x--; if (t == 1) { seg.update(x, y); } else { cout << seg.query(x, y) << endl; } } return 0; }
# include<bits/stdc++.h> # define ll long long # define mod 1073741824 using namespace std; int main(){ // int t; // cin>>t; // while(t--){ // cout<<"\n"; // } int n; cin>>n; if( (108 * n) / 100 == 206){ cout<<"so-so"; } else if( (108 * n) / 100 < 206){ cout<<"Yay!"; } else{ cout<<":("; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #define rep(i,n) for(ll i=0;i<ll(n);i++) #define REP(i,k,n) for(ll i=k;i<ll(n);i++) #define all(a) a.begin(),a.end() #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define UNIQUE(a) sort(all(a));a.erase(unique(all(a)),a.end()) #define decimal cout<<fixed<<setprecision(10) using ll=long long; using P=pair<ll,ll>; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; constexpr ll inf=1001001001001001; constexpr int INF=1001001001; constexpr int mod=1000000007; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} void YesNo(bool b){if(b)out("Yes");else out("No");} void yesno(bool b){if(b)out("yes");else out("no");} void YESNO(bool b){if(b)out("YES");else out("NO");} ll modpow(ll a,ll b){ll c=1;while(b>0){if(b&1){c=a*c%mod;}a=a*a%mod;b>>=1;}return c;} vi calc(ll x){vi v;while(x>0){v.eb(x%10);x/=10;}reverse(all(v));return v;} int main(){ int n; cin>>n; n=27*n/25; if(n<206) out("Yay!"); else if(n==206) out("so-so"); else out(":("); }
#include<bits/stdc++.h> using namespace std; typedef int64_t ll; typedef long double ld; const ll MOD=1000000007; const ll MODA=998244353; ll vx[4]={0,1,0,-1}; ll vy[4]={1,0,-1,0}; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) long long gcd(long long a,long long b){ ll gcdmax=max(a,b); ll gcdmin=min(a,b); while(true){ if(gcdmax%gcdmin==0)break; else gcdmax%=gcdmin; swap(gcdmin,gcdmax); } return gcdmin; } ll pow(ll N,ll P,ll M){ if(P==0)return 1; else if(P%2==0){ ll t=pow(N,P/2,M); return t*t%M; } else return N*pow(N,P-1,M)%M; } vector<ll> find_divisor(ll N){ ll k=1; while(k*k<=N){ k++; } vector<ll> A(1); rep(i,k){ if(i==1)A.at(0)=1; else if(i>=2){ if(N%i==0)A.push_back(i); } } ll t=0; t=A.size(); rep(i,t){ if(A.at(t-i-1)*A.at(t-i-1)!=N)A.push_back(N/A.at(t-1-i)); } return A; } vector<ll> fac; vector<ll> finv; vector<ll> inv; void COMinit(ll N,ll P){ rep(i,N+1){ if(i==0){ fac.push_back(1); finv.push_back(1); inv.push_back(1); } else if(i==1){ fac.push_back(1); finv.push_back(1); inv.push_back(1); } else{ fac.push_back(fac.at(i-1)*i%P); inv.push_back(P-inv.at(P%i)*(P/i)%P); finv.push_back(finv.at(i-1)*inv.at(i)%P); } } } ll COM(ll n,ll k,ll P){ if(n<k)return 0; if(n<0||k<0)return 0; return fac.at(n)*(finv.at(k)*finv.at(n-k)%P)%P; } struct UnionFind { vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(ll N) : par(N) { //最初は全てが根であるとして初期化 for(ll i = 0; i < N; i++) par[i] = i; } ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(ll x, ll y) { // xとyの木を併合 ll rx = root(x); //xの根をrx ll ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す ll rx = root(x); ll ry = root(y); return rx == ry; } }; // 1-indexedなので注意。 struct BIT { private: vector<ll> bit; ll N; public: BIT(ll size) { N = size; bit.resize(N + 1); } // 一点更新です void add(ll a, ll w) { for (ll x = a; x <= N; x += x & -x) bit[x] += w; } // 1~Nまでの和を求める。 ll sum(ll a) { int ret = 0; for (ll x = a; x > 0; x -= x & -x) ret += bit[x]; return ret; } }; int main(){ ll N; cin>>N; vector<ll> A(N); rep(i,N){ cin>>A.at(i); A.at(i)++; } BIT B(N); vector<ll> C(N); vector<ll> ans(N); rep(i,N){ C.at(i)=i-B.sum(A.at(i)); B.add(A.at(i),1); ans.at(0)+=C.at(i); } rep(i,N){ if(i>=1){ ans.at(i)=ans.at(i-1)+A.at(N-i)*2-N-1; } } cout<<ans.at(0)<<endl; for(ll i=N-1;i>0;i--)cout<<ans.at(i)<<endl; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define VI vector<int> #define VL vector<ll> #define MX vector<VI > #define all(x) x.begin(), x.end() #define IOS cin.tie(0)->sync_with_stdio(0) using namespace std; struct segt{ int N = 1; VI seg; void add(int i){ for(i += N; i; i/=2) seg[i]++; } int sum(int a, int b){ a += N; b+= N; int s = 0; while(a<=b){ if(a&1) s += seg[a++]; if(~b&1) s += seg[b--]; a /= 2; b/=2; } return s; } segt(int n){ while(N < n) N*= 2; seg.resize(2*N, 0); } }; int main(){ int n; cin >> n; VI a(n); for(int &i : a) cin >> i; ll ans = 0; segt seg(n); for(int i=0; i<n; i++){ ans += seg.sum(a[i], n-1); seg.add(a[i]); } for(int i=0; i<n; i++){ cout << ans << '\n'; ans += n-2*a[i]-1; } return 0; }
#include <iostream> #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <stack> #include <queue> #include <array> #include <bitset> #include <cstdio> #include <string> #include <vector> #include <random> #include <chrono> #include <utility> #include <numeric> #include <cstdlib> #include <cstring> #include <climits> #include <sstream> #include <assert.h> #include <iomanip> #include <algorithm> #include <functional> #include <unordered_map> using namespace std; struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _; #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 HOME ~debug() { cerr << endl; } eni( != ) cerr << boolalpha << i; ris; } eni( == ) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define ff first #define ss second #define REP(i,st,en) for (int i = st; i <= en; i++) #define REPR(i,st,en) for (int i = st; i >= en; i--) const int MOD = 998244353; class Modint { public: long long val; Modint (long long _val = 0) { if (_val >= MOD) _val %= MOD; val = _val; } Modint operator+ (Modint other) { return Modint(val + other.val); } void operator+= (Modint other) { val += other.val; if (val >= MOD) val %= MOD; } Modint operator- () { return Modint(MOD - val); } Modint operator- (Modint other) { return Modint(val + MOD - other.val); } void operator-= (Modint other) { val += MOD - other.val; if (val >= MOD) val %= MOD; } Modint operator* (Modint other) { return Modint(val * other.val); } void operator*= (Modint other) { val *= other.val; if (val >= MOD) val %= MOD; } bool operator== (Modint other) { return val == other.val; } bool operator!= (Modint other) { return val != other.val; } }; Modint exp (Modint a, long long k) { Modint res = 1; while (k) { if (k & 1) res *= a; a *= a; k >>= 1; } return res; } Modint inv (Modint a) { return exp(a, MOD - 2); } ostream& operator<< (ostream& out, Modint p) { out << p.val; return out; } const int maxn = 2e5; vector<Modint> fact(maxn + 1, 1); vector<Modint> inv_fact(maxn + 1, 1); void preC() { for (int i = 2; i <= maxn; i++) { fact[i] = fact[i - 1] * i; } inv_fact[maxn] = inv(fact[maxn]); for (int i = maxn - 1; i >= 0; i--) inv_fact[i] = inv_fact[i + 1] * (i + 1); } Modint C(int n, int r) { // nCr % MOD if (n < r) return 0; return fact[n] * inv_fact[r] * inv_fact[n - r]; } class MainClass { public: void solve() { int n, m; cin >> n >> m; if (m == 1) { cout << 1; return; } int lgm = log2(m) + 1; Modint ans = 0; vector<Modint> dp(m + 1, 1); for (int i = 1; i <= lgm; i++) { vector<Modint> new_dp(m + 1, 0); Modint curr = 0; for (int j = 1; j <= m; j++) { for (int k = 2 * j; k <= m; k += j) new_dp[k] += dp[j]; curr += dp[j]; } ans += C(n - 1, i - 1) * curr; dp.swap(new_dp); } cout << ans; return; } }; int main() { preC(); int test = 1; //cin >> test; for (int tst = 1; tst <= test; ++tst) { MainClass Ausmosian; Ausmosian.solve(); cout << "\n"; } return 0; }
#include<bits/stdc++.h> #define N 200005 #define Mod 998244353 #define MOD(x) (x>=Mod)&&(x-=Mod) #define Ms(a,b) memset(a,b,sizeof a) #define db(x) cerr<<#x<<"="<<x<<endl; #define db2(x,y) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl; #define db3(x,y,z) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl; using namespace std; int rd(){ int res=0,c,f=0; while(!isdigit(c=getchar()))f=c=='-'; do res=(res<<1)+(res<<3)+(c^48); while(isdigit(c=getchar())); return f?-res:res; } int dp[N][18]; int n,m; int main(){ n=rd(),m=rd(),dp[0][0]=1; for(int i=1;i<=n;i++) for(int j=0;j<=17;j++) for(int k=0;k<=j;k++) dp[i][j]+=dp[i-1][j-k],MOD(dp[i][j]); int res=0; for(int i=1;i<=m;i++){ int now=1,x=i; for(int j=2;1ll*j*j<=x;j++){ int t=0; while(x%j==0)x/=j,++t; now=1ll*now*dp[n][t]%Mod; } if(x!=1)now=1ll*now*n%Mod; res+=now,MOD(res); } printf("%d\n",res); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vpii vector<pair<int,int>> #define vpll vector<pair<ll,ll>> #define fr(i,k,n) for (int i = k; i < n; ++i) #define fri(i,k,n) for (int i = k; i >= n; --i) #define pb push_back #define mp make_pair #define all(arr) arr.begin(),arr.end() #define ff first #define ss second const double pi=3.1415926535897932384626433832795; const int inf=1e9; const ll inf2=1e18; const int mod=1e9+7; void boost(){ ios_base::sync_with_stdio(false); cin.tie(NULL); } ll pr[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; void solve(){ int n; cin>>n; vi arr(n); int m=sizeof(pr)/sizeof(pr[0]); fr(i,0,n){ cin>>arr[i]; int temp=0; for(int j=0;j<m;j++){ if(arr[i]%pr[j] ==0){ temp|=(1<<j); } } arr[i]=temp; // cout<<arr[i]<<endl; } ll ans=inf2; for(int mask=1;mask<(1<<m);mask++){ bool poss=true; for(int i=0;i<n;i++){ if(!(mask&arr[i])){ poss=false; break; } } if(!poss) continue; ll val=1; for(int j=0;j<m;j++){ if((mask>>j)&1) val*=pr[j]; } // cout<<val<<endl; ans=min(ans,val); } cout<<ans<<endl; return; } int main() { boost(); int tc=1; //cin>>tc; while(tc--) solve(); return 0; }
/*Read the problem carefully before starting to work on it*/ #include <bits/stdc++.h> //#include <boost/multiprecision/cpp_int.hpp> //using namespace boost::multiprecision; //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> using namespace std; //using namespace __gnu_pbds; typedef long long ll; #define pb push_back #define eb emplace_back #define mp(x,y) make_pair(x,y) #define mod 1000000007 double PI=3.1415926535897932384626; //template<typename T> T power(T x,T y,ll m=mod){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;} //typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ost; #define fi first #define se second bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); vector<ll>pp; for(ll i=2;i<=50;i++) { if(isPrime(i)) { pp.eb(i); } } ll n; cin>>n; vector<ll>X(n); for(ll i=0;i<n;i++) { cin>>X[i]; } ll nn=pp.size(); vector<ll>lol; for(ll i=0;i<(1LL<<nn);i++) { ll temp=1; for(ll j=0;j<nn;j++) { if((i>>j)&1LL) { temp*=pp[j]; } } lol.eb(temp); } sort(lol.begin(),lol.end()); for(auto &temp:lol) { ll fl=0; for(ll k=0;k<n;k++) { if(__gcd(temp,X[k])==1) { fl=1; break; } } if(fl==0) { cout<<temp<<endl; return 0; } } return 0; } //252908XL
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i)) #define rep(i,n) FOR(i,0,n) #define all(v) begin(v),end(v) #ifdef NDEBUG #define assert(e) if(!(e)){stod("");} #endif #ifdef ONLINE_JUDGE #define debug(x) #define debug2(x,y) #define debug3(x,y,z) #else #define debug(x) std::cerr<<#x<<": "<<x<<"\n" #define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n" #define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n" #endif using ll=long long; using vi=std::vector<int>; using vvi=std::vector<vi>; using vll=std::vector<ll>; using vvll=std::vector<vll>; template<typename T> using vvec=std::vector<std::vector<T>>; template<typename T> auto make_v(size_t sz){return std::vector<T>(sz);} template<typename T,typename... Ts> auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));} template<typename T> void fill_v(T&var,const T&x){var=x;} template<typename V,typename T> void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){ s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;} inline void scan(int&a){scanf("%d",&a);} inline void scan(ll&a){scanf("%lld",&a);} inline void scan(char&a){scanf(" %c",&a);} inline void scan(double&a){scanf("%lf",&a);} template<typename T> inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}} template<typename First,typename...Args> inline void scan(First&f,Args&...args){scan(f);scan(args...);} inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);} inline void print(int a){printf("%d\n",a);} inline void print(ll a){printf("%lld\n",a);} inline void print(double a){printf("%.12f\n",a);} inline void print(std::string s){std::cout<<s<<"\n";} using namespace std; void solve() { ll n; scan(n); ll m = 86; vll fib(m, 0); fib[0] = 1; fib[1] = 2; FOR (i, 2, m) { fib[i] = fib[i - 1] + fib[i - 2]; } vll cnt(m, 0); for (int i = m - 1; i >= 0; --i) { if (n >= fib[i]) { n -= fib[i]; cnt[i] = 1; } } assert (n == 0); vi ans; for (int i = m - 1; i >= 0; i -= 2) { if (cnt[i]) { ans.push_back(1); } ans.push_back(4); if (cnt[i - 1]) { ans.push_back(2); } ans.push_back(3); } int sz = ans.size(); assert (sz <= 130); print(sz); rep (i, sz) { print(ans[i]); } } int main() { solve(); return 0; }
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<sstream> #include<cstdio> #include<cstdlib> #include<cstring> #include<climits> #include<cmath> #include<string> #include<vector> #include<set> #include<map> #include<queue> #include<numeric> #include<functional> #include<algorithm> #include<bitset> #include<tuple> #include<unordered_set> #include<unordered_map> #include<random> #include<array> #include<cassert> using namespace std; #define INF ((1<<30)-1) #define rep(i,n) for(int i=0;i<(int)(n);i++) #define all(v) v.begin(),v.end() int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; int w[8]; cin >> n >> m; rep(i, n)cin >> w[i]; sort(w, w + n); vector<pair<int, int>> tmp, vl; rep(i, m) { int l, v; cin >> l >> v; tmp.emplace_back(v, l); if (v < w[n - 1]) { cout << -1 << endl; return 0; } } sort(all(tmp)); for (auto p : tmp) { if (vl.empty() || vl.back().first < p.first && vl.back().second < p.second) { vl.push_back(p); } else if(vl.back().first == p.first && vl.back().second < p.second){ vl.back().second = p.second; } } int ans = INF; do { int dist[8][8]; rep(i, n)rep(j, n)dist[i][j] = 0; for (int i = 0; i < n; i++) { int s = 0; for (int j = i; j < n; j++) { s += w[j]; int idx = lower_bound(all(vl), make_pair(s, 0)) - vl.begin(); if (idx > 0) { dist[i][j] = max(dist[i][j], vl[idx - 1].second); } } } int x[8] = {}; while (1) { bool update = false; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (x[j] - x[i] < dist[i][j]) { x[j] = x[i] + dist[i][j]; update = true; } } } if (!update)break; } ans = min(ans, x[n - 1]); } while (next_permutation(w, w + n)); cout << ans << endl; return 0; }
#include <stdio.h> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <cstdio> #include <vector> #include <string> #include <bitset> #include <cctype> #include <cstdlib> #include <cstring> #include <utility> #include <numeric> #include <complex> #include <sstream> #include <fstream> #include <iomanip> #include <cassert> #include <iostream> #include <iterator> #include <algorithm> #include <functional> //#include <atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef vector<vector<int>> v2int; typedef vector<vector<vector<int>>> v3int; typedef vector<ll> vll; typedef vector<vector<ll>> v2ll; typedef vector<vector<vector<ll>>> v3ll; typedef list<int> liint; typedef pair<int, int> pint; typedef vector<pair<int, int>> vpint; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<ll, int>> vpll_int; typedef vector<pair<int, ll>> vpint_ll; typedef set<pair<int, int>> spint; typedef set<pair<ll, int>> spll; typedef unordered_map<int, unordered_set<int>> Graph; const int INF = int(2e9); const ll LINF = ll(2e9) * ll(2e9); #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for(int i = x; i < n; i++) template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; } template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; vint A(N), B(N); rep(i, N) cin >> A[i] >> B[i]; ll ans = LINF; rep(i, N) { ans = min(ans, ll(A[i]) + ll(B[i])); } rep(i, N) { rep(j, N) { if (i == j) continue; ans = min(ans, ll(max(A[i], B[j]))); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; const int inf = 1 << 29; const ll infll = 1LL << 60; const int mod = 1000000007; const int mod9 = 998244353; template<class T> bool chmin(T& a, T b){ if(a > b){ a = b; return true; } else return false; } template<class T> bool chmax(T& a, T b){ if(a < b){ a = b; return true; } else return false; } int main(){ int n; cin >> n; vector<int> a(n), b(n); int ans = inf; for(int i=0; i<n; i++){ cin >> a[i] >> b[i]; ans = min(ans, a[i]+b[i]); } for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(i != j){ ans = min(ans, max(a[i], b[j])); } } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 60; int mod = 1000000007; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); vector<int> A(3); rep(i, 3) cin >> A[i]; sort(A.begin(), A.end()); cout << A[1] + A[2] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int N; char S[100'100]; int dp[100'100]; int mn[222]; signed main() { cin.tie(0)->sync_with_stdio(false); memset(dp, 0x3F, sizeof dp); memset(mn, 0x3F, sizeof mn); dp[0] = 0; cin>>N; cin >>(S + 1); for (int i = 1; i <= N; ++i) { for (char c = 'a'; c <= 'z';++c) { if (c == S[i]) continue; dp[i] = min(dp[i], mn[c] + 1); } mn[S[i]] = min(mn[S[i]], dp[i - 1]); } if (dp[N] >= 1e9) cout << "-1\n"; else cout << dp[N] << '\n'; return 0; }
//Har Har Mahadev using namespace std; #include <bits/stdc++.h> #define booga cerr << "booga" << endl #define int long long #define ld long double #define pb push_back #define mp make_pair #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define safai(...) Clearing_out(__VA_ARGS__) template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(char c) { string s; s += c; return s; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "1" : "0"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } void Clearing_out() { return; } template <typename Head, typename... Tail> void Clearing_out(Head &H, Tail & ... T) { H.clear(); Clearing_out(T...); } void testcase(){ string s; cin >> s; reverse(s.begin(), s.end()); int n = s.size(); for(int i = 0; i < n; i++){ if(s[i] == '9') s[i]='6'; else if(s[i]=='6') s[i]= '9'; } cout << s; // cout << s; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int tt = 1; // cin >> tt; while(tt--){ testcase(); } return 0; }
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<vector> #include<string.h> #include<math.h> #include<map> #include<iomanip> #include<queue> using namespace std; using ll = long long; using ull = unsigned long long; const ll inf = 1e9 + 7; int main(){ cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecison(15); int n; cin >> n; vector<int> a(100 , 0); for(int i = 2; i <= 30; i++){ a[i] = i; } ll ans = 1; for(int i = 2; i <= n; i++){ ans *= a[i]; for(int j = i + 1; j <= n; j++){ if(a[j] % a[i] == 0){ a[j] /= a[i]; } } } /* for(int i = 2; i <= n; i++){ cout << a[i] << " "; } cout << endl; */ ans += 1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long int #define all(x) x.begin(),x.end() #define pii pair<int,int> #define io ios_base::sync_with_stdio(false); cin.tie(NULL); #define N 105 using namespace std; int a[N][N]; void solve(){ int n,m,mn=1e9; cin>>n>>m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; mn=min(mn,a[i][j]); } } int rem=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++) rem+=a[i][j]-mn; } cout<<rem; } int main() { int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int H,W; cin >> H >> W; vector<int>A(H*W); for(int i=0; i<H*W; i++) { cin >> A.at(i); } sort(A.begin(),A.end()); int a=0; for (int i=0; i<H*W; i++) { a+=A.at(i)-A.at(0); } cout << a << endl; }
/* Arthor: Ender_zzm E-mail: [email protected] Blog: oi.ender-zzm.pro */ #include <bits/stdc++.h> using namespace std; inline int Isdigit(char c) { if (c < '0' || c > '9') return 0; return 1; } template <class T> T read(){ register T x = 0, flag = 1; register char ch; while (!Isdigit(ch = getchar())) if (ch == '-') flag = -1; while (Isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar(); return x * flag; } template <class T> inline void write(T x){ if (x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar(x % 10 + '0'); } template<class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; } template<class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; } #define fi first #define se second #define debug(x) cout << #x << " = " << x << endl; #define For(i, x, y) for (int i = (x); i <= (y); i++) #define Rep(i, x, y) for (int i = (x); i >= (y); i--) #define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout) int x, y; int main() { x = read<int>(), y = read<int>(); int X = x, Y = y; x = (X + Y) / 2; y = X - x; printf("%d %d\n", x, y); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w)); rep(i, h) rep(j, w) cin >> a[i][j]; int mn = 1e9; rep(i, h) rep(j, w) mn = min(mn, a[i][j]); int ans = 0; rep(i, h) rep(j, w) ans += a[i][j] - mn; cout << ans << endl; return 0; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> // #include <atcoder/all> // g++ main.cpp -std=c++17 -I . using namespace std; // using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;} template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;} using ll=long long; const int inf = INT_MAX / 2; const ll INF = 1LL << 60; const int MOD = 1000000007; //cout << fixed << setprecision(15) << x << endl; //////////////////////////////////////////////////////////////////////////////////////////// int main() { int n,k;cin>>n>>k; vector<vector<ll>> a(n, vector<ll>(n)); for(auto&i:a) for(auto&j:i) cin>>j; ll left = -2; ll right = 1000000002; while(right - left > 1) { ll mid = left+(right-left)/2; vector<vector<ll>> table(n+1, vector<ll> (n+1, 0)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(a[i-1][j-1]>mid) table[i][j]++; } } for(int i = 0; i <= n; i++) { for(int j = 0; j < n; j++) { table[i][j+1] += table[i][j]; } } for(int j = 0; j <= n; j++) { for(int i = 0; i < n; i++) { table[i+1][j] += table[i][j]; } } bool flag = false; for(int i = 0; i+k<=n; i++) { for(int j = 0; j+k<=n;j++) { int cnt = table[i+k][j+k]-table[i][j+k]-table[i+k][j]+table[i][j]; // rep(x, k) rep(y, k) { // if(a[i+x][j+y]>mid) cnt++; // } if(cnt<=(k*k)/2) flag = true; } } if(flag) right = mid; else left = mid; } cout << right << endl; return 0; }
/* made by amunduzbaev */ //~ #include <ext/pb_ds/assoc_container.hpp> //~ #include <ext/pb_ds/tree_policy.hpp> #include "bits/stdc++.h" using namespace std; //~ using namespace __gnu_pbds; #define ff first #define ss second #define vv vector #define pb push_back #define mp make_pair #define ub upper_bound #define lb lower_bound #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(),x.rend() #define mem(arr, v) memset(arr, v, sizeof arr) #define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vii; typedef vector<pii> vpii; template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; } template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; } template<int sz> using tut = array<int, sz>; void usaco(string s) { freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); NeedForSpeed } //~ template<class T> using oset = tree<T, //~ null_type, less_equal<T>, rb_tree_tag, //~ tree_order_statistics_node_update> ordered_set; const int N = 8e2+5; const int mod = 1e9+7; const ll inf = 1e18; const ld Pi = acos(-1); #define MULTI 0 int n, m, k, s, t, q, ans, res, a[N][N]; int p[N][N]; bool check(int v){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ p[i][j] = (a[i][j] <= v) + p[i-1][j] + p[i][j-1] - p[i-1][j-1]; } } for(int i=k;i<=n;i++){ for(int j=k;j<=n;j++){ if(p[i][j] - p[i-k][j] - p[i][j-k] + p[i-k][j-k] >= (k * k + 1) / 2) return 1; } } return 0; } void solve(int t_case){ vii tt; cin>>n>>k; set<int> ss; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j], ss.insert(a[i][j]); } } for(auto x : ss) tt.pb(x); int l = 0, r = sz(tt)-1; while(l < r){ int m = (l + r)>>1; if(check(tt[m])) r = m; else l = m + 1; } cout<<tt[l]; } signed main(){ NeedForSpeed if(MULTI){ int t; cin>>t; for(int t_case = 1; t_case <= t; t_case++) solve(t_case); } else solve(1); return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; #define ll long long #define fi first #define se second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a)) #define FOREACH(a, b) for (auto&(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define dbg(x) cout << (#x) << " is " << (x) << endl; #define dbg2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << y << endl; #define dbgarr(x, sz) \ for (int asdf = 0; asdf < (sz); asdf++) cout << x[asdf] << ' '; \ cout << endl; #define dbgarr2(x, rose, colin) \ for (int asdf2 = 0; asdf2 < rose; asdf2++) { \ dbgarr(x[asdf2], colin); \ } #define dbgitem(x) \ for (auto asdf = x.begin(); asdf != x.end(); asdf++) cout << *asdf << ' '; \ cout << endl; const int mod = 998244353, MAXN = 2e5 + 1, MAXH = 19; int n, m, sieve[MAXN], inv[MAXN], choose[MAXN+MAXH][MAXH]; ll facts[MAXN] = {1}; struct Solution { int solve() { gen_sieve(), gen_choose(); ll res=0; REPN(i, m) { // ending value = i vi frqs = get_frqs(i); // dbgitem(frqs); ll x=1; for (int f : frqs) x = (x * choose[n+f-1][f]) % mod; // dbg2(i,x); res=(res+x)%mod; } return res; } vi get_frqs(int x) { // get frq of prime factors of x map<int, int> frq; vi res; while (true) { if (sieve[x] == 1) break; frq[sieve[x]]++, x /= sieve[x]; } for (auto& p : frq) res.push_back(p.second); return res; } void gen_sieve() { iota(sieve+1, sieve+MAXN,1); for (int p = 2; p * p <= m; p++) { if (sieve[p] != p) continue; // not a prime for (int i = p * p; i <= m; i += p) sieve[i] = p; // p is smallest pf of i } // dbgarr(sieve,m+1); } void gen_choose() { REP(i, n + MAXH) choose[i][0] = 1; // one way to choose 0 REPN(i, n+MAXH) { REPN(j, min(i, MAXH - 1)) { choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % mod; } } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); Solution test; cin >> n >> m; cout << test.solve() << endl; }
#include <bits/stdc++.h> #define FAST_IO ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cout << setprecision(15); #define ll long long int #define rep(i, n) for (ll i = 0; i < (n); i++) #define repp(i, k, n) for (ll i = k; i < (n); i++) using namespace std; ll mPow(ll a, ll n, ll mod) { ll ans = 1; while (n > 0) { if (n & 1) ans = (ans * a) % mod; a = (a * a) % mod; n = n >> 1; } return ans; } void slv() { FAST_IO ll n, m; cin >> n >> m; vector<ll> arr(m); rep(i, m) { cin >> arr[i]; } arr.insert(arr.begin(), 0); arr.push_back(n + 1); sort(arr.begin(), arr.end()); ll mi = n; repp(i, 1, arr.size()) { ll k = arr[i] - arr[i - 1] - 1; if(k) mi = min(mi, k); } ll ans = 0; repp(i, 1, arr.size()) { ll block = arr[i] - arr[i - 1] - 1; ans += block / mi; if (block % mi) ans++; } cout << ans; } int main() { FAST_IO int test = 1; // cin >> test; while (test-- > 0) { slv(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define x first #define y second #define FOR(i, m, n) for (ll i(m); i < n; i++) #define DWN(i, m, n) for (ll i(m); i >= n; i--) #define REP(i, n) FOR(i, 0, n) #define DW(i, n) DWN(i, n, 0) #define F(n) REP(i, n) #define FF(n) REP(j, n) #define D(n) DW(i, n) #define DD(n) DW(j, n) using ll = long long; using ld = long double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vtll = vector<tll>; using gr = vector<vll>; using wgr = vector<vpll>; void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); } void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); } template<typename T,typename U> ostream& operator<<(ostream& os, const pair<T,U>& p) { cerr << ' ' << p.x << ',' << p.y; return os; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for(auto x: v) os << ' ' << x; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for(auto x: v) os << ' ' << x; return os; } template<typename T,typename U> ostream& operator<<(ostream& os, const map<T,U>& v) { for(auto x: v) os << ' ' << x; return os; } struct d_ { template<typename T> d_& operator,(const T& x) { cerr << ' ' << x; return *this;} } d_t; #define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; } #define deb(X ...) dbg(#X, "=", X); #define EPS (1e-10) #define INF (1LL<<61) #define YES(x) cout << (x ? "YES" : "NO") << endl; #define CL(A,I) (memset(A,I,sizeof(A))) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() const ll MOD = 998244353; int main(void) { ios_base::sync_with_stdio(false); ll n; cin >> n; vll a(n); F(n) cin >> a[i]; sort(all(a)); ll ret = 0; ll p = 0; F(n) { ret += a[i]*a[i]; ret %= MOD; ret += (p*a[i])%MOD; ret %= MOD; p *= 2; p += a[i]; p %= MOD; } cout << ret << endl; return 0; }
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <map> #include <cmath> using namespace std; #define int long long main() { int n; int mod = 998244353; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++){ cin >> v[i]; } sort(v.begin(), v.end()); int sum = 0; int ans = 0; for (int i = 0; i < n; i++){ ans += (sum * v[i]) % mod; ans %= mod; ans += (v[i] * v[i]); ans %= mod; sum *= 2; sum += v[i]; sum %= mod; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair <int, int> pii; #define int ll #define pb push_back #define F first #define S second #define _sz(x) ((int)x.size()) #define fastio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) int dx[4] = {-1,0,1,0}; int dy[4] = {0,1,0,-1}; inline int read() { char c = getchar(); int x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int n; signed main() { fastio; cin >> n; if(n==2){ cout << 3 <<endl; return 0; }else if(n==3){ cout << 7 <<endl; return 0; } unordered_map<int,int> hash; hash[2] = 1; hash[3] = 1; for(int i = 4; i <= n; i++){ int v = i; for(int j = 2; j <= v /j; j++){ int cnt = 0; while(v%j==0){ v /= j; cnt++; } hash[j] = max(hash[j],cnt); } if(v > 1){ if(hash[v]==0) hash[v] = 1; } } int ans = 1; for(auto t:hash){ for(int i = 1; i <= t.S; i++){ ans *= t.F; } } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> #define INF 1e9 #define eps 1e-6 typedef long long ll; using namespace std; int n; int main(){ cin >> n; if(n % 2 == 0) puts("White"); else puts("Black"); return 0; }
#include<bits/stdc++.h> using namespace std; int n,m,a[100005],c[21]; long long ans; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ string s; cin>>s; for(int j=0;j<m;j++)if(s[j]=='1')a[i]|=1<<j; c[__builtin_popcount(a[i])]++; } for(int i=0;i<=m;i++){ for(int j=i+1;j<=m;j+=2){ ans+=(long long)c[i]*c[j]; } } cout<<ans; return 0; }
#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n,m; cin>>n>>m; int res = 0; for(int i=1;i<=n;i++){ string s; cin>>s; int cnt = 0; for(int j=0;j<s.length();j++){ if(s[j] == '1'){ cnt++; } } if(cnt%2){ res++; } } cout<<1LL*res*(n-res)<<endl; }
#include <bits/stdc++.h> //#include <atcoder/modint> //#include <atcoder/math> //#include <boost/multiprecision/cpp_int.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("O3") #define REP(i, n) for(int i = 0; i < n; i++) #define REPP(i, n) for(int i = 1; i <= n; i++) #define ALL(obj) (obj).begin(), (obj).end() #define EPS (1e-9) //#define INF (1e17) #define PI (acos(-1)) //const double PI = acos(-1); //const double EPS = 1e-15; long long INF=(long long)1E17; #define i_7 (long long)(1e9+7) //#define i_7 998'244'353 long mod(long a){ long long c = a % i_7; if(c >= 0) return c; return c + i_7; } long long po(long a, long b){ if(b == 0) return 1; long long z = po(a , b / 2); z = mod(z * z); if(b % 2 != 0) z = mod(a * z); return z; } bool prime_(int n){ if(n == 1){ return false; }else if(n == 2){ return true; }else{ for(int i = 2; i <= std::sqrt(n); i++) if(n % i == 0) return false; return true; } } long long gcd_(long long a, long long b){ if(a < b) std::swap(a,b); if(a % b == 0) return b; else return gcd_(b, a % b); } long long lcm_(long long x, long long y){ return (x / gcd_(x,y)) * y; } using namespace std; //using namespace atcoder; //using mint = modint1000000007; //using mint = modint998244353; //using namespace boost::multiprecision; //using namespace __gnu_pbds; vector<vector<int>> G; int main(){ //using namespace std; int n, m; cin>>n>>m; G.resize(n); if(m > 0){ int a[m], b[m]; REP(i, m){ cin>>a[i]>>b[i]; a[i]--; b[i]--; G[a[i]].push_back(b[i]); G[b[i]].push_back(a[i]); } } int k; cin>>k; int c[k]; REP(i, k){ cin>>c[i]; c[i]--; } if(m == 0){ if(k == 1) cout << 1 << endl; else cout << -1 << endl; return 0; } vector<vector<long long>> dist(k, vector<long long>(n, INF)); //memset(dist, INF, sizeof(dist)); //bool used[n]; vector<bool> used; REP(i, k){ //memset(used, false, sizeof(used)); used = vector<bool>(n, false); typedef pair<long long, int> P; priority_queue<P, vector<P>, greater<P>> q; q.emplace(0, c[i]); while(!q.empty()){ P now = q.top(); q.pop(); int u = now.second; long long d = now.first; if(used[u]) continue; dist[i][u] = d; used[u] = true; for(int v: G[u]){ if(used[v]) continue; q.emplace(d + 1, v); } } } vector<vector<long long>> dp(k, vector<long long>(1<<k, -1)); REP(i, k) dp[i][1<<i] = 1; REP(x, 1<<k){ //cout << "x: " << bitset<3>(x) << endl; REP(i, k){ if(dp[i][x] < 0) continue; if(!(x>>i&1)) continue; REP(j, k){ if(dist[i][c[j]] == INF) continue; int nx = (x | (1<<j)); //cout << "nx: " << bitset<3>(nx) << endl; if(dp[j][nx] < 0) dp[j][nx] = dp[i][x] + dist[i][c[j]]; else dp[j][nx] = min(dp[j][nx], dp[i][x] + dist[i][c[j]]); } } //cout << bitset<3>(x) << ": " << dp[x] << endl; } long long ans = -1; REP(i, k){ if(dp[i][(1<<k) - 1] < 0) continue; if(ans < 0) ans = dp[i][(1<<k) - 1]; else ans = min(ans, dp[i][(1<<k) - 1]); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for (long long i = 0; i < (n); ++i) #define INF LONG_MAX/3 //#define DIV 1000000007 //#define DIV 998244353 template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; ll A[505][505]; ll B[505][505]; ll dis[505][505][2]; int main(){ ll R, C; cin >> R >> C; rep(i, R) rep(j, C-1) cin >> A[i][j]; rep(i, R-1) rep(j, C) cin >> B[i][j]; //cost, y, x, uemode priority_queue<tuple<ll, ll, ll, bool>, vector<tuple<ll, ll, ll, bool>>, greater<tuple<ll, ll, ll, bool> > >Q; Q.push({0, 0, 0, false}); rep(i, R) rep(j, C) dis[i][j][0] = -1; rep(i, R) rep(j, C) dis[i][j][1] = -1; while(!Q.empty()) { ll cost, y, x; bool uemode; tie(cost, y, x, uemode) = Q.top(); Q.pop(); if(dis[y][x][uemode] != -1) continue; dis[y][x][uemode] = cost; if(y == R - 1 && x == C - 1) { cout << cost << endl; return 0; } //1 if(x + 1 < C) { Q.push({cost + A[y][x], y, x+1, false}); } //2 if(x > 0) { Q.push({cost + A[y][x-1], y, x-1, false}); } //3 if(y + 1 < R) { Q.push({cost + B[y][x], y+1, x, false}); } //4 if(uemode && y > 0) { Q.push({cost + 1, y-1, x, uemode}); } if(!uemode) { Q.push({cost + 1, y, x, true}); } } }
// Template #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <iomanip> #include <tuple> #include <utility> #include <queue> #include <set> #include <map> #include <array> #include <cassert> #include <cmath> #define rep_override(x, y, z, name, ...) name #define rep2(i, n) for (int i = 0; i < (int)(n); ++i) #define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i) #define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; constexpr int inf = 1001001001; constexpr ll infll = 3003003003003003003LL; template <typename T> inline bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template <typename T> inline bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &element : vec) is >> element; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) { os << vec[i] << (i + 1 == vec_len ? "" : " "); } return os; } struct IOSET { IOSET() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } ioset; // Main int main() { ll n, k; cin >> n >> k; rep(i, k) { if (n % 200 == 0) { n /= 200; } else { if (i == k - 1) { n = n * 1000 + 200; } else { n = n * 5 + 1; ++i; } } } cout << n << '\n'; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int) (n); i++) #define reps(i, n) for (int i = 1; i <= (int) (n); i++) #define all(a) (a).begin(), (a).end() #define uniq(a) (a).erase(unique(all(a)), (a).end()) #define bit(n) (1LL << (n)) #define dump(a) cerr << #a " = " << (a) << endl using vint = vector<int>; using pint = pair<int, int>; using vpint = vector<pint>; template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double PI = 3.1415926535897932384626433832795028; constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int sign(int a) { return (a > 0) - (a < 0); } int cdiv(int a, int b) { return (a - 1 + b) / b; } template<typename T> void fin(T a) { cout << a << endl; exit(0); } template<typename T> T sq(T a) { return a * a; } template<typename T, typename U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<typename T, typename U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) { os << "(" << a.first << ", " << a.second << ")"; return os; } template<typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) { os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")"; return os; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const deque<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const set<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const multiset<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template<typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; template<typename F> double continuous_binary_search(double ok, double ng, F is_ok, int n) { for (int i = 0; i < n; i++) { double mid = (ok + ng) / 2; (is_ok(mid) ? ok : ng) = mid; } return ok; } signed main() { int N; cin >> N; vint x(N), y(N); rep(i, N) { cin >> x[i] >> y[i]; } cout << continuous_binary_search(0, 200, [&](double v) { bool ret = true; vector<bool> visited(N); auto dfs = [&](auto &&dfs, int cur) -> void { if (visited[cur]) { return; } visited[cur] = true; if (100 - y[cur] < v) { ret = false; } rep(i, N) { if (sq(x[i] - x[cur]) + sq(y[i] - y[cur]) < sq(v)) { dfs(dfs, i); } } }; rep(i, N) { if (y[i] + 100 < v) { dfs(dfs, i); }} return ret; }, 200) / 2 << endl; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author aryssoncf */ // Taken from: https://github.com/bqi343/USACO/blob/master/Implementations/content/contest/templateShort.cpp #include <utility> #include <vector> #include <random> #include <chrono> #include <cassert> #include <set> #include <queue> #include <map> #include <numeric> #include <algorithm> #include <iostream> #include <unordered_map> #include <bitset> #include <iomanip> #include <complex> #include <stack> #include <array> #include <unordered_set> #include <climits> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef double db; typedef string str; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ull> vul; typedef vector<ll> vl; typedef vector<pii> vpii; #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 sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rsz resize #define mp make_pair #define pb push_back #define f first #define s second const int MOD = 1e9+7; // 998244353; // = (119<<23)+1 const int MX = 2e5+5; template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); // // Created by aryssoncf on 28/10/2019. // #include <type_traits> #include <iterator> template <typename T> constexpr auto hasBegin(int) -> decltype(std::begin(std::declval<T>()), true) { return true; } constexpr bool hasBegin(...) { return false; } template <typename T> using IsContainer = std::integral_constant<bool, hasBegin<T>(0)>; #ifndef JHELPER_EXAMPLE_PROJECT_IO_HPP #define JHELPER_EXAMPLE_PROJECT_IO_HPP #endif //JHELPER_EXAMPLE_PROJECT_IO_HPP template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { for (T &element: vec) { is >> element; } return is; } template<typename T, typename U> std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first; is >> p.second; return is; } template <class X, class Y> std::ostream & operator << (std::ostream & os, std::pair <X, Y> const& p) { return os << p.first << " " << p.second; } template <class Ch, class Tr, class Container> std::basic_ostream <Ch, Tr> & operator << (std::basic_ostream <Ch, Tr>& os, Container const& x) { bool first = true; for(auto& y : x) { if (first) { first = false; } else { os << " "; } os << y; } return os; } using namespace std; class DLetsPlayNim { public: void solve(std::istream& in, std::ostream& out) { int t; in >> t; while (t--) { int n; in >> n; vi A(n); in >> A; sort(all(A)); bool res = false; if (n % 2 == 0) { for (int i = 0; i < n; i += 2) { if (A[i] != A[i + 1]) { res = true; } } } out << (res ? "First" : "Second") << '\n'; } } void setup() {} }; int main() { std::ios_base::sync_with_stdio(false); DLetsPlayNim solver; solver.setup(); std::istream& in(std::cin); std::ostream& out(std::cout); in.tie(nullptr); out << std::fixed; out.precision(20); solver.solve(in, out); 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.at(i); int ans = 0; int k = 0; for(int i = 2; i <= 1000; i++){ int cnt = 0; for(int j = 0; j < n; j++){ if(a.at(j) % i == 0) cnt++; } if(k < cnt){ ans = i; k = cnt; } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for(int i=0;i<(int)(n);i++) #define ALL(a) (a).begin(),(a).end() using namespace std; using ll=long long; typedef pair<int,int> P; int main(){ int a,b,c; cin>>a>>b>>c; cout<<(a*a+b*b<c*c ? "Yes" : "No")<<endl; }
#include <bits/stdc++.h> // #include <atcoder/modint> // #include <atcoder/dsu> // #include <atcoder/segtree> // #include <atcoder/fenwicktree> // #include <atcoder/math> // using namespace atcoder; using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vector<int>>; using vl = vector<long long>; using vvl = vector<vector<long long>>; #define rep(i,n) for(int i = 0; i < (int)(n); i++) // using mint = modint1000000007; // using mint = modint998244353; // using mint = modint; int main() { ll a,b,c; cin>>a>>b>>c; if(c==0) { if(a<=b) cout<<"Aoki\n"; else cout<<"Takahashi\n"; } if(c==1) { if(b<=a) cout<<"Takahashi\n"; else cout<<"Aoki\n"; } }
#include<bits/stdc++.h> #define int long long int using namespace std; signed main(){ // freopen("input.txt", "r", stdin); map<int, string> m; int n, a, c; string b; cin>>n; for(int i=0; i<n; i++){ cin>>b>>a; m[a]=b; } a=1; for(auto i=m.rbegin(); i != m.rend(); i++ ){ if(a==2){ cout<<i->second<<endl; return 0; } a++; } return 0; }
#include<algorithm> #include<iostream> #include<vector> #define mp make_pair #define pb push_back #define fo(i,n) for(int i=0;i<(n);i++) using namespace std; void solve(){ int n; cin>>n; vector<pair<int ,string>> names(n); fo(i,n){ cin>>names[i].second>>names[i].first; } sort(names.begin() , names.end()); cout<<names[n-2].second; } int main() { int t = 1; // cin>>t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long long N; cin >> N; int A = 1; long long pow3 = 1; while(pow3 < N){ pow3 = pow3*3; long long res = N - pow3; int B = 0; while(res > 0){ if(res%5 == 0){ res = res/5; B++; if(res == 1){ cout << A << " " << B << endl; return 0; } } else{ A++; break; } } } cout << -1 << endl; }
#include<bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count()); //mt19937_64 mrand(42); #define ii for(int i=1;i<=n;++i) #define ji for(int j=1;j<=n;++j) #define jj for(int j=1;j<=m;++j) #define ij for(int i=1;i<=m;++i) #define sz(x) ((ll)x.size()) #define all(x) x.begin(),x.end() #define al(x) x+1,x+1+n #define asd cout<<"ok"<<endl; #define asdd cout<<"okok"<<endl; #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vii vector<pair<int,int>> #define pr(v) for(auto i:v) cout<<i<<" ";cout<<endl; #define prt(a, l, r) for(auto i=l;i<=r;++i) cout<<a[i]<<" ";cout<<endl; #define pc(x) __builtin_popcount(x) #define pb push_back #define PS string qqwwee;cin>>qqwwee; typedef pair<int,int> pii; int main() { ll pw3=3,pw5=5; ll n; cin>>n; for(int i=1;pw3<=n;++i) { pw5 = 5; for(int j=1;pw5<=n;++j) { if(pw3 + pw5 == n) { cout << i << " " << j << endl; return 0; } pw5 = pw5 * 5; } pw3 = pw3 * 3; } cout << -1 << endl; }
#include <bits/stdc++.h> #define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x) #define all(v) (v).begin(), (v).end() using namespace std; #define forn(i,n) for (int i = 0; i < (n); ++i) #define rforn(i, n) for(int i = (n) - 1;i >= 0;--i) using ll = long long; int mod = (ll)1e9 + 7; #define PI acos(-1) typedef pair<int, int> pairs; const int INF = 1e9 + 1; const int N = 2e5 + 100; const double eps = 1e-7; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <typename XPAX> void ckma(XPAX &x, XPAX y) { x = (x < y ? y : x); } template <typename XPAX> void ckmi(XPAX &x, XPAX y) { x = (x > y ? y : x); } void solve() { int n; cin >> n; ll ans = 0; map<int, int> mp; ll tot = 0; forn(i, n) { int x; cin >> x; ans += tot - mp[x]; mp[x]++; tot++; } cout << ans << '\n'; } void test_case() { int t; cin >> t; forn(p, t) { //cout << "Case #" << p + 1 << ": "; solve(); } } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std ; typedef long long ll ; map <ll , ll> cnt ; int n , h ; int main(){ cin >> n ; for (int i = 0 ; i < n ; i++){ cin >> h ; cnt[h] ++ ; } ll ans = 0 ; for (auto i : cnt){ ans += i.second * (n- i.second) ; } cout << ans/2 << "\n" ; }
#include <bits/stdc++.h> #define x first #define y second using namespace std; typedef pair<int, int> PII; const int N = 1010, M = 4010; int h1[N], h2[N], e[M], ne[M], w[M], n, m, idx; int f[N][N]; PII q[N * N]; int ans = 1e9; void add(int h[], int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++; } int bfs(int fr, int to) { int hh = 0, tt = -1; f[fr][to] = 0; q[++ tt] = {fr, to}; while (hh <= tt) { auto t = q[hh ++]; int t1 = t.x, t2 = t.y; for (int i1 = h1[t1]; i1 != -1; i1 = ne[i1]) for (int i2 = h1[t2]; i2 != -1; i2 = ne[i2]) { if (w[i1] != w[i2]) continue; int j1 = e[i1], j2 = e[i2]; if (f[j1][j2] != -1) continue; if (t1 == j2 && t2 == j1) ans = min(ans, f[t1][t2] + 1); //入队不一定最小 else if (j1 == j2) ans = min(ans, f[t1][t2] + 2); //入队不一定最小 f[j1][j2] = f[t1][t2] + 2; q[++ tt] = {j1, j2}; } } if (ans == 1e9) return -1; return ans; } int main() { memset(h1, -1, sizeof h1); memset(h2, -1, sizeof h2); memset(f, -1, sizeof f); cin >> n >> m; for (int i = 0; i < m; i ++) { int a, b, c; char ch[2]; cin >> a >> b >> ch; c = ch[0] - '\0'; add(h1, a, b, c), add(h1, b, a, c); add(h2, a, b, c), add(h2, b, a, c); } cout << bfs(1, n); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define P pair<int,char> #define Tp tuple<int,int,int> using Graph = vector<vector<P>>; int main(){ int N,M; cin >> N >> M; int a,b; char c; Graph g(N+1); rep(i,M){ cin >> a >> b >> c; g[a].push_back(make_pair(b,c)); g[b].push_back(make_pair(a,c)); } queue<tuple<int,int,int>> go; bool vis[N+1][N+1]; rep(i,N+1){ rep(j,N+1){ vis[i][j] = false; } } vis[1][N] = true; go.push(make_tuple(0,1,N)); int nowk = 0; bool fin = false; while(!go.empty()){ auto p = go.front(); go.pop(); a = get<1>(p); b = get<2>(p); int k = get<0>(p); if(k>nowk){ nowk = k; if(fin){ cout << 2*k-1 << endl; return 0; } } if(a==b){ cout << 2*k << endl; return 0; } for(auto q:g[a]){ int x = q.first; char c1 = q.second; int y; for(auto r:g[b]){ y = r.first; char c2 = r.second; if(x==b&&y==a){ fin = true; } if(c1!=c2||vis[x][y]) continue; go.push(make_tuple(k+1,x,y)); vis[x][y] = true; } } } cout << -1 << endl; }
//#include <atcoder/all> #include <bits/stdc++.h> using namespace std; #define rep2(x,fr,to) for(int x=(fr);x<(to);x++) #define rep(x,to) for(int x=0;x<(to);x++) #define repr(x,fr,to) for(int x=(fr);x>=(to);x--) #define all(c) c.begin(),c.end() #define sz(v) (int)v.size() typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9+7; //998244353; void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);} //using mint = atcoder::modint1000000007; ll fnc(){ ll n; string t; cin >>n >>t; ll mx=(ll)1e10; if(t=="1") return 2*mx; if(t=="11" ||t=="0"|| t=="110") return mx; string s; rep(i, (n+11)/3) s +="110"; if(s.find(t) ==string::npos) return 0; int zct=0; for(auto x:t) zct += x=='0'; ll ans = mx - zct +(t[n-1]=='0'? 1: 0); return ans; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fnc() <<"\n"; return 0; }
#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 998244353 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 N, K; cin >> N >> K; vector<int> A(N); REP(i, 0, N) cin >> A[i]; vector<mint> B(N, 1), C(N, 1), D(K + 1, 0), E(K + 1, 0); D[0] = N; E[0] = N; mint fac = 1; REP(i, 0, K) { REP(j, 0, N) { B[j] *= (2 * A[j]); C[j] *= A[j]; D[i + 1] += B[j]; E[i + 1] += C[j]; } fac *= mint(i + 1); E[i + 1] /= fac; } fac = 1; REP(i, 1, K + 1) { mint ans = fac; mint tmp = 0; REP(j, 0, i + 1) { tmp += E[j] * E[i - j]; } ans *= tmp; // cout << "# i : " << i << ", ans : " << ans << " - " << D[i] << " / " << 2 << endl; ans -= D[i]; ans /= mint(2); cout << ans << endl; fac *= mint(i + 1); } return 0; }
#include<bits/stdc++.h> #define it register int #define ct const int #define il inline using namespace std; typedef long long ll; #define rll register ll #define cll const ll typedef double db; #define rdb register db #define cdb const db typedef long double ldb; typedef unsigned long long ull; #define pb push_back #define mkp make_pair #define pl pair<ll,int> #define pi pair<int,int> #define fir first #define sec second namespace io{ il char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } template <class I> il void fr(I &num){ num=0;register char c=nc();it p=1; while(c<'0'||c>'9') c=='-'?p=-1,c=nc():c=nc(); while(c>='0'&&c<='9') num=num*10+c-'0',c=nc(); num*=p; } char obuf[1000001],*os=obuf, *ot=obuf+1000001; il void flush(){fwrite(obuf,1,os-obuf,stdout),os=obuf;} il void ptc(const char x){*os++=x,os==ot?flush():void();} template<class I> il void wr(I x){ x<0?ptc('-'),x=-x:0; static char qu[65]; char *tmp=qu; do *tmp++=(x%10)^'0';while(x/=10); while(tmp--!=qu) ptc(*tmp); } struct flusher{ ~flusher() { flush(); } }_; template <class I> il I Max(I p,I q){return p>q?p:q;} template <class I> il I Min(I p,I q){return p<q?p:q;} template <class I> il I A(I x){return x<0?-x:x;} template <class I> il void sp(I&p,I&q){I x=p;p=q,q=x;} template <class I> il void ckMax(I&p,I q){p=(p>q?p:q);} template <class I> il void ckMin(I&p,I q){p=(p<q?p:q);} }; using io :: fr; using io :: nc; using io :: wr; using io :: ptc; using io :: Max; using io :: Min; using io :: sp; using io :: ckMax; using io :: ckMin; const int N=1000005; int h[N],nxt[N],adj[N],u,v,t,a[N],n,dep[N],fa[N],path[N],cnp,ans[N],mxd,id,son[N]; bool flag[N]; il void add(){nxt[++t]=h[u],h[u]=t,adj[t]=v,nxt[++t]=h[v],h[v]=t,adj[t]=u;} il void predfs(ct x){ flag[x]=(x==id); for(it i=h[x],j;i;i=nxt[i]) if((j=adj[i])^fa[x]){ fa[j]=x,predfs(j); if(flag[j]) son[x]=j,flag[x]=1; } } il void dfs(ct x){ path[++cnp]=x,dep[x]=dep[fa[x]]+1; for(it i=h[x],j;i;i=nxt[i]) if(((j=adj[i])^fa[x])&&(j^son[x])) fa[j]=x,dfs(j); if(son[x]) fa[son[x]]=x,dfs(son[x]); path[++cnp]=x; } il void DFS(ct x,ct fa,ct dep){ if(dep>mxd) mxd=dep,id=x; for(it i=h[x],j;i;i=nxt[i]) if((j=adj[i])^fa) DFS(j,x,dep+1); } int main(){ scanf("%d",&n);it i; for(i=1;i<n;++i) scanf("%d%d",&u,&v),add(); DFS(1,0,0),mxd=0,i=id,id=0,DFS(i,0,0),std::swap(i,id),predfs(i),dfs(i); for(i=cnp;i;--i) ans[path[i]]=i; for(i=1;i<=n;++i) printf("%d ",ans[i]); return 0; }
#include <iostream> #include <vector> #include <algorithm> std::vector<std::vector<int>> children, list; std::vector<int> in, out, depth; int timer; void dfs(const int u) { in[u] = timer++; list[depth[u]].push_back(in[u]); for (const int v : children[u]) { depth[v] = depth[u] + 1; dfs(v); } out[u] = timer++; } int main() { int N; std::cin >> N; children = list = std::vector<std::vector<int>>(N); in = out = depth = std::vector<int>(N); for (int i = 1; i < N; ++i) { int p; std::cin >> p; children[p - 1].push_back(i); } dfs(0); int Q; std::cin >> Q; while (Q--) { int u, d; std::cin >> u >> d; u -= 1; const auto& v = list[d]; std::cout << std::lower_bound(v.cbegin(), v.cend(), out[u]) - std::lower_bound(v.cbegin(), v.cend(), in[u]) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; //int:2*10**9 typedef long double ld; typedef pair<ll,ll> P; #define REP(i,n) for(ll i = 0; i<(ll)(n); i++) #define FOR(i,a,b) for(ll i=(a);i<=(b);i++) #define FORD(i,a,b) for(ll i=(a);i>=(b);i--) #define pb push_back #define MOD 1000000007 //998244353 #define PI 3.141592653 #define INF 100000000000000 //14 //cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); int main(){ ll n; cin >> n; vector<ll> a(n); REP(i,n) cin >> a[i]; sort(a.begin(),a.end()); ll ans = a[0]+1; FOR(i,1,n-1) { ans = (ans*(a[i]-a[i-1]+1))%MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = a; i >= b; i--) const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; typedef long long LL; typedef pair <int, int> P; const int N = 2e5 + 5; int n, a[N], b[N], c[N]; LL ans, res; int main() { cin>>n; rep (i, 1, n) scanf("%d", &a[i]), ans += a[i]; rep (i, 1, n) scanf("%d", &b[i]), b[i] -= a[i]; rep (i, 1, n) { if (i&1) b[(i + 1)>>1] = b[i]; else c[i>>1] = b[i]; } sort(b + 1, b + n/2 + 1); sort(c + 1, c + n/2 + 1); res = ans; per (i, n/2, 1) res += b[i] + c[i], ans = max(ans, res); printf("%lld\n", ans); }
#include <bits/stdc++.h> #define loop(s, e, i) for (int i = s; i < e; ++i) #define print(s) cout << s << endl; #define DIV 1000000007 using namespace std; typedef long long ll; typedef unsigned long long ull; const int INF = 1e9+7; ll ceildiv(ll a, ll b) { return (a+b-1)/b; } // 切り上げ ll floordiv(ll a, ll b) { return a/b; } // 切り下げ int show_matrix(vector<vector<ll>> &dp) { loop(0, dp.size(), i) { loop(0, dp[i].size(), j) { cout << dp[i][j] << " "; } cout << endl; } return 0; } struct Edge { ll from, to; ll cost; Edge() {}; Edge(ll f, ll t, ll c) :from(f), to(t), cost(c) {}; bool operator< (const Edge &e) { return cost < e.cost; } }; struct Graph { vector<vector<Edge>> data; // Constructor Graph(ll v):data(v) {}; void add_edge(Edge &e) { data[e.from].push_back(e); } void add_edge(ll from, ll to, ll cost) { data[from].emplace_back(from, to, cost); } size_t size() { return data.size(); } }; /* ダイクストラ法 */ void dijkstra(vector<ll> &dist, Graph &g, ll s) { using state = pair<ll, ll>; // Pair of 'cost' and 'to' priority_queue<state, vector<state>, greater<state>> queue; dist.resize(g.size(), INF); // vector<ll> dist(g.size(), INF); dist[s] = 0; queue.push(state(0, s)); while(!queue.empty()) { state p = queue.top(); queue.pop(); ll v = p.second; if (dist[v] < p.first) continue; for (ll i=0; i<g.data[v].size(); i++) { Edge e = g.data[v][i]; if (dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; queue.push(state(dist[e.to], e.to)); } } } } /* 浮動小数点の入力 cout << fixed << setprecision(9) << endl; */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll N, M; cin >> N >> M; Graph graph(N); loop(0, M, i) { ll a, b; cin >> a >> b; a--; b--; graph.add_edge(a, b, 1); graph.add_edge(b, a, 1); } ll K; cin >> K; vector<ll> C(K); loop(0, K, i) { cin >> C[i]; C[i]--; } vector<vector<ll>> DIST(K, vector<ll>(K)); loop(0, K, i) { vector<ll> dist; dijkstra(dist, graph, C[i]); loop(0, K, j) { ll to = C[j]; DIST[i][j] = dist[to]; } } vector<vector<ll>> DP((1<<K), vector<ll>(K, INF)); loop(0, K, i) { DP[0][i] = 1; } for (ll bits=0; bits<(1<<K); bits++) { loop(0, K, i) { if (!(bits & (1 << i))) continue; loop(0, K, j) { ll bef = bits - (1 << i); DP[bits][j] = min(DP[bits][j], DP[bef][i] + DIST[i][j]); } } } ll result = INF; loop(0, K, i) { result = min(result, DP[(1<<K)-1][i]); } if (result == INF) { print(-1); } else { print(result); } }
/***************************** * Author :: Πρασαννα Κ. Ροι * *****************************/ /*********************************** * Unus pro omnibus, omnes pro uno * ***********************************/ #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; using namespace std::chrono; #define __AcHiLlEs ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define __AnAkLuSmOs freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); #define int long long #define span(a) begin(a), end(a) template<typename t> using vc = vector<t>; vc<vc<int>> gph1; vc<vc<int>> d, dp; map<int, int> id; int cnt(0); void bfs(int u, int n) { queue<int> q; vc<int> dist(n + 1, -1); dist[u] = 0; q.push(u); while (!q.empty()){ int v = q.front(); q.pop(); for (auto &i: gph1[v]) if (dist[i] == -1) { dist[i] = dist[v] + 1; q.push(i); } } for (int i = 1; i <= n; i++) if (id[i]) d[id[u]][id[i]] = d[id[i]][id[u]] = dist[i]; } int compute(int u, int vis, int k) { int res(1e18); if (vis == (1 << k) - 1) return 0; if (!dp[u][vis]) return (int)1e18; if (~dp[u][vis]) return dp[u][vis]; dp[u][vis] = 0; for (int i = 0; i < k; i++) if (i + 1 != u) res = min(res, compute(i + 1, vis | (1 << i), k) + d[u][i + 1]); return dp[u][vis] = res; } void solve() { int n, m, x, y; cin >> n >> m; gph1.assign(n + 1, vc<int>()); for (int i = 0; i < m; i++) cin >> x >> y, gph1[x].push_back(y), gph1[y].push_back(x); int k, cnt(0); cin >> k; vc<int> a(k); d.assign(k + 1, vc<int>(k + 1, -1)); for (auto &i: a) cin >> i, id[i] = ++cnt; if (k == 1) return void(cout << 1 << "\n"); for (auto &i: a) bfs(i, n); for (int i = 1; i <= k; i++) for (int j = 1; j <= k; j++) if (d[i][j] == -1) return void(cout << -1 << "\n"); dp.assign(k + 1, vc<int>(1 << k, -1)); int res(LLONG_MAX); for(int i = 1; i <= k; i++) res = min(res, compute(i, 1 << (i - 1), k)); cout << res + 1 << "\n"; } signed main() { // auto start = high_resolution_clock::now(); __AcHiLlEs int t(1); // cin >> t; for (int i = 1; i <= t; /*cout << "Case " << i << ": ",*/ solve(), i++); // auto stop = high_resolution_clock::now(); // double duration = duration_cast<microseconds>(stop - start).count(); // cout << fixed << setprecision(4) << duration / 1000000 << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; stack<char> st; int n; string s; int main() { ios::sync_with_stdio(0); cin >> n >> s; for(int i = 0; i < n; i++) { if(st.size() < 2) { st.push(s[i]); continue; } char c = st.top(); st.pop(); if(s[i] == 'x' && c == 'o' && st.top() == 'f') { st.pop(); continue; } st.push(c), st.push(s[i]); } cout << st.size() << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector <ll> vi; typedef pair<ll,ll> pll; #define mp make_pair #define pb push_back #define N ((ll)(4e5 + 55)) #define INF ((ll)1e18+18) #define bismillah {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);} ll mod = 998244353; ll M(ll x){ if (x < 0) return (x + mod) % mod; return x%mod; } ll pw(ll x ,ll p){ ll res = 1; while (p > 0){ if (p & 1){ res = M(x*res); p --; } x = M(x*x); p /= 2; } return M(res); } ll comb[N]; ll magic[N]; ll primes[N]; vector<ll> p; void sieve(){ primes[0] = primes[1] = 99; for (ll i = 2; i < N; i ++){ if (primes[i] == 99) continue; for (ll j = i*i ; j < N; j += i){ primes[j] = 99; } } for (ll i = 2; i < N; i ++){ if (primes[i] != 99) p.pb(i); } } int main(){ bismillah comb[0] = 1; for (ll i = 1; i < N; i ++){ comb[i] = M(i * comb[i-1]); } sieve(); /*for (auto pr : p){ cout << pr << "\n"; }*/ ll n , m; cin >> n >> m; ll ans = 0; for (ll i = 1; i <= 200; i ++){ magic[i] = M(M(M(comb[i + n - 1]) * pw(comb[i] , mod - 2)) * pw(comb[n-1] , mod - 2)); } for (ll i = 1; i <= m; i ++){ ll res = 1; ll c = 0; ll num = i; ll lim = num; while (num > 1 and p[c] * p[c] <= lim){ ll cnt = 0; while (num % p[c] == 0){ num /= p[c]; cnt += 1; } c++; if (cnt == 0)continue; res = M(res * magic[cnt]) ; } if (num > 1){ res = M(res * magic[1]); } ans = M(res + ans); } cout << ans; }
//In the name of Allah :) #include <bits/stdc++.h> using namespace std; string to_string(char c) { return string(1,c); } string to_string(bool b) { return b ? "true" : "false"; } string to_string(const char* s) { return (string)s; } string to_string(string s) { return s; } template<class A> string to_string(complex<A> c) { stringstream ss; ss << c; return ss.str(); } string to_string(vector<bool> v) { string res = "{"; for(int i = 0; i < (int)v.size(); i++) res += char('0'+v[i]); res += "}"; return res; } template<size_t SZ> string to_string(bitset<SZ> b) { string res = ""; for(size_t i = 0; i < SZ; i++) res += char('0'+b[i]); return res; } template<class A, class B> string to_string(pair<A,B> p); template<class T> string to_string(T v) { // containers with begin(), end() bool fst = 1; string res = "{"; for (const auto& x: v) { if (!fst) res += ", "; fst = 0; res += to_string(x); } res += "}"; return res; } template<class A, class B> string to_string(pair<A,B> p) { return "("+to_string(p.first)+", "+to_string(p.second)+")"; } void DBG() { cerr << "]" << endl; } template<class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } #ifdef LOCAL // compile with -DLOCAL #define wis(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__) #else #define wis(...) 0 #endif typedef long long ll; #define all(x) (x).begin(), (x).end() int main(){ ios::sync_with_stdio(0); int a, b; cin >> a >> b; wis(a, b); cout << a + a + 100 - b << '\n'; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ul; typedef long long ll; #define printV(v) for(int i = 0;i < v.size();++i)cout << v[i] << endl int main() { int A,B; cin >> A; cin >> B; int X = (A + B) / 2; int Y = (A - B) / 2; cout << X << " " << Y << endl; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int c1 = (2 * i + 1) % n + 1; int c2 = (2 * i + 2) % n + 1; cout << c1 << " " << c2 << endl; } }
#include <bits/stdc++.h> #define clr(x) memset(x,0,sizeof(x)) #define For(i,a,b) for (int i=(a);i<=(b);i++) #define Fod(i,b,a) for (int i=(b);i>=(a);i--) #define fi first #define se second #define kill _z_kill #define y0 _z_y0 #define y1 _z_y1 #define x0 _z_x0 #define x1 _z_x1 #define pb push_back #define mp(x,y) make_pair(x,y) using namespace std; typedef long long LL; typedef unsigned long long ull; typedef unsigned uint; typedef long double LD; typedef vector <int> vi; typedef pair <int,int> pii; void enable_comma(){} string tostring(char c){ string s=""; s+=c; return s; } string tostring(string s){ return "\""+s+"\""; } string tostring(const char *c){ return tostring((string)c); } string tostring(long long x){ if (x<0) return "-"+tostring(-x); if (x>9) return tostring(x/10)+tostring(char('0'+x%10)); else return tostring(char('0'+x)); } string tostring(int x){ return tostring((long long)x); } string tostring(unsigned long long x){ if (x>9) return tostring((long long)(x/10))+tostring(char('0'+x%10)); else return tostring(char('0'+x)); } string tostring(unsigned x){ return tostring((long long)x); } string tostring(double x){ static char res[114]; sprintf(res,"%lf",x); string s=tostring(res); return s.substr(1,(int)s.size()-2); } string tostring(long double x){ return tostring((double)x); } template <class A,class B> string tostring(pair <A,B> p){ return "("+tostring(p.fi)+","+tostring(p.se)+")"; } template <class T> string tostring(T v){ string res=""; for (auto p : v) res+=(res.size()?",":"{")+tostring(p); return res.size()?res+"}":"{}"; } template <class A> string tostring(A* a,int L,int R){ return tostring(vector <A>(a+L,a+R+1)); }; template <class A> string tostring(A a,int L,int R){ return tostring(a.data(),L,R); } string tostrings(){ return ""; } template <typename Head,typename... Tail> string tostrings(Head H,Tail... T){ return tostring(H)+" "+tostrings(T...); } #define User_Time ((double)clock()/CLOCKS_PER_SEC) #ifdef zzd #define outval(x) cerr<<#x" = "<<tostring(x)<<endl #define outvals(...) cerr<<"["<<#__VA_ARGS__<<"]: "<<\ tostrings(__VA_ARGS__)<<endl #define outtag(x) cerr<<"--------------"#x"---------------"<<endl #define outsign(x) cerr<<"<"#x">"<<endl #define outarr(a,L,R) cerr<<#a"["<<(L)<<".."<<(R)<<"] = "<<\ tostring(a,L,R)<<endl #else #define outval(x) enable_comma() #define outvals(...) enable_comma() #define outtag(x) enable_comma() #define outsign(x) enable_comma() #define outarr(a,L,R) enable_comma() #endif #ifdef ONLINE_JUDGE #ifdef assert #undef assert #endif #define assert(x) (!(x)?\ cout<<"Assertion failed!"<<endl<<\ "function: "<<__FUNCTION__<<endl<<\ "line: "<<__LINE__<<endl<<\ "expression: "<<#x<<endl,exit(3),0:1) #endif LL read(){ LL x=0,f=0; char ch=getchar(); while (!isdigit(ch)) f=ch=='-',ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?-x:x; } template <class T> void ckmax(T &x,const T y){ if (x<y) x=y; } template <class T> void ckmin(T &x,const T y){ if (x>y) x=y; } //int Pow(int x,LL y){ // y=(y%(mod-1)+(mod-1))%(mod-1); // int ans=1; // for (;y;y>>=1,x=(LL)x*x%mod) // if (y&1) // ans=(LL)ans*x%mod; // return ans; //} //void Add(int &x,int y){ // if ((x+=y)>=mod) // x-=mod; //} //void Del(int &x,int y){ // if ((x-=y)<0) // x+=mod; //} //int Add(int x){ // return x>=mod?x-mod:x; //} //int Del(int x){ // return x<0?x+mod:x; //} //int md(LL x){ // return (x%mod+mod)%mod; //} const int N=1005; int n; int main(){ n=read(); For(i,0,n-1) printf("%d %d\n",(2*i)%n+1,(2*i+1)%n+1); return 0; }
#include<bits/stdc++.h> using namespace std; #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define MOD 1000000007 #define MOD1 998244353 #define INF 1e18 #define nline "\n" #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define PI 3.141592653589793238462 #define set_bits __builtin_popcountll #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef unsigned long long ull; typedef long double lld; // typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key #ifndef ONLINE_JUDGE #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #endif void _print(ll t) {cerr << t;} void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(lld t) {cerr << t;} void _print(double t) {cerr << t;} void _print(ull t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} void solve2(){ int n; cin>>n; vector<int> a(n),b(n),c(n), freq(n+1); for(int &x:a){ cin>>x; } for(int &x:b){ cin>>x; } for(int &x:c){ cin>>x; x=b[x-1]; freq[x]++; } ll ans=0; for(int x:a){ ans += freq[x]; } cout<<ans; } int main() { #ifndef ONLINE_JUDGE freopen("Error.txt", "w", stderr); #endif fastio(); solve2(); return 0; }
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned ll #define ld long double #define mod 1000000007 #define pb push_back #define pf push_front #define dll deque<ll> #define vll vector<ll> #define vvll vector<vll> #define pll pair<ll,ll> #define vpll vector<pll> #define dpll deque<pll> #define mapll map<ll,ll> #define umapll unordered_map<ll,ll,custom_hash> #define nl "\n" #define all(v) v.begin(),v.end() #define fr for(ll i=0;i<n;i++) #define ms(a,x) memset(a,x,sizeof(a)) //#define fr(aa,bb) for(ll i=aa;i<=bb;i++) #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") using namespace __gnu_pbds; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return move(v); } template<typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; s << "["; for(int i = 0; i < n - 1; i++) s << arr[i] << ","; s << arr[n - 1] << "]"; return s.str(); } #define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);} inline void __evars_begin(int line) { cerr << "#" << line << ": "; } template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); } template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); } template<typename T> inline void __evars_out_var(T val) { cerr << val; } inline void __evars(vector<string>::iterator it) { cerr << endl; } template<typename T, typename... Args> inline void __evars(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << "="; __evars_out_var(a); cerr << "; "; __evars(++it, args...); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; //template<class T, class H>using umap=unordered_map<T,H,custom_hash>; //template<class T>using uset=unordered_set<T,custom_hash>; ll dmod(ll x){ return ((x+mod)%mod); } ll modular_power(ll x,ll y){ ll ans=1; while(y){ if(y&1)ans=dmod(ans*x); y/=2; x=dmod(x*x); } return ans; } ll inv(ll x){ return modular_power(dmod(x),mod-2); } int main() { //clock_t clk = clock(); fio //cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n'; ll t=1; //cin>>t; while(t--) { ll n,ans=0;cin>>n;ll a[n];ll b[n];ll c[n];ll d[n]; fr cin>>a[i]; fr cin>>b[i]; fr cin>>c[i]; fr { d[i]=b[c[i]-1]; } unordered_map<ll,ll,custom_hash>mp1; unordered_map<ll,ll,custom_hash>mp2; fr { mp1[a[i]]++; mp2[d[i]]++; } for(auto it:mp1) { ans+=(it.second*mp2[it.first]); } cout<<ans; } return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; using ll = long long; using lint = long long; typedef vector<long long> vint; typedef pair<long long, long long> pint; #define INF INT32_MAX / 2 #define INF64 INT64_MAX / 2 #define EPS 0.001 #define EPS14 1.0E-14 #define REP(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(),(x).end() #define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c)) #define c(n) cout<<n<<endl; #define cf(n) cout<<fixed<<setprecision(15)<<n<<endl; template <class T>inline bool chmin(T&a,T b) {if(a>b){a=b;return true;}return false;} template <class T>inline bool chmax(T&a,T b) {if(a<b){a=b;return true;}return false;} template<class T>inline T sum(T n){return n*(n+1)/2;} map<ll,ll> prime_fac(ll A) {map<ll,ll>mp;for(ll i=2;i*i<=A;i++){while(A%i== 0){mp[i]++;A/=i;}}if(A!=1){mp[A]=1;}return mp;} bool is_prime(ll N){if(N<=1)return false;for(ll i=2;i*i<=N;i++){if(N%i==0) return false;}return true;} template<class T>inline T myceil(T a,T b){return (a+(b-1))/b;} ll pw(ll x, ll n){ll ret=1;while(n>0){if(n&1){ret*=x;}x *= x;n >>= 1;}return ret;} bool is_product_overflow(long long a,long long b) {long prod=a*b;return (prod/b!=a);} const string YES = "Yes"; const string NO = "No"; void solve(long long M, long long H){ if (H % M == 0) c(YES) else c(NO) } int main(){ long long M; scanf("%lld",&M); long long H; scanf("%lld",&H); solve(M, H); return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using ll = int64_t; using ull = uint64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} //head int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<P> xy(n); rep(i, n) cin >> xy[i]; vi ids(n); iota(all(ids), 0); sort(all(ids), [&](int i, int j) {return xy[i] < xy[j];}); vi check(n); check[ids[0]] = check[ids[1]] = check[ids[n-1]] = check[ids[n-2]] = 1; sort(all(ids), [&](int i, int j){return xy[i].second < xy[j].second;}); check[ids[0]] = check[ids[1]] = check[ids[n-1]] = check[ids[n-2]] = 1; vi u; rep(i, n) if(check[i]) u.push_back(i); vi dist; int s = u.size(); rep(i, s) rep(j, i) dist.emplace_back(max(abs(xy[u[i]].first-xy[u[j]].first), abs(xy[u[i]].second-xy[u[j]].second))); sort(rall(dist)); cout << dist[1] << endl; }
#include<bits/stdc++.h> using namespace std; struct fastio{fastio(){cin.tie(nullptr);ios_base::sync_with_stdio(false);std::cout<<std::fixed<<setprecision(10);}}oitsaf; template<class T>std::istream&operator>>(std::istream&is,std::vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;} template<class T>std::ostream&operator<<(std::ostream&os,std::vector<T>const&v){for(auto const& vi:v)os<<vi<<" ";return os;} int main() { int n; cin >> n; n--; vector<int> a(1 << n), b(1 << n); cin >> a >> b; auto amax = max_element(begin(a), end(a)); auto bmax = max_element(begin(b), end(b)); int ans = -1; if (*amax > *bmax) { ans = bmax - begin(b) + 1 + (1 << n); } else { ans = amax - begin(a) + 1; } cout << (ans) << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n; cin >> n; vector<ll> a(1<<n); for(int i = 0;i < 1<<n;i++)cin >> a[i]; vector<vector<pair<ll,ll> > > p(n+1); for(int i = 0;i < 1<<n;i++)p[0].push_back(make_pair(a[i],i)); ll d = 0; while(1){ if(p[d].size() <= 2)break; for(int i = 0;i < p[d].size();i++){ if(i % 2 == 1)continue; ll num = 0; ll win = 0; if(p[d][i].first >= p[d][i + 1].first){ num = p[d][i].second,win = p[d][i].first; } else{ num = p[d][i + 1].second,win = p[d][i + 1].first; } p[d + 1].push_back(make_pair(win,num)); //i++; //cout << i << " " << win << " " << num << endl; } d++; } if(p[d][0].first >= p[d][1].first)cout << p[d][1].second + 1 << endl; else cout << p[d][0].second + 1 << endl; }
/* Hey why peeping here -_'_-? I believe on myself and I will achieve this->author = Fuad Ashraful Mehmet, CSE ,University of Asia Pacific Todo: https://codeforces.com/problemset?tags=1500-2300 */ #include <bits/stdc++.h> #define forn(i, n) for (int i = 0; i < int(n); ++i) #define all(o) (o).begin(), (o).end() #define rall(o) (o).rbegin(), (o).rend() #define sz(o) int(o.size()) #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); #define dbg(o) cerr << "at " << __LINE__ << " response = " << o << endl; #define dbgA(a,L,R) cerr<<#a<<"=[";for(int i=L;i<=R;i++) cerr<<a[i]<<(i==R?"]\n":" "); #define dbgV(O) cerr << #O << "=["; for (auto o : O) cerr << o << ", "; cerr << "E]\n"; #define pb push_back using namespace std; using pii = pair<int, int>; typedef long long ll; int n,m,k,tc=1,len; const int N=31; ll dp[N][N],s; void go(int x,int y,ll rem){ if(x==0 && y==0)return; if(x==0){ forn(i,y)cout<<'b';cout<<'\n';return; } if(y==0){ forn(i,x)cout<<'a';cout<<'\n';return; } if(dp[x-1][y]>=rem){ cout<<'a'; go(x-1,y,rem); }else{ cout<<'b'; go(x,y-1,rem-dp[x-1][y]); } return; } void solve(){ cin>>n>>m>>s; dp[0][0]=1; for(int i=0;i<=n;++i){ for(int j=0;j<=m;++j){ if(i)dp[i][j]+=dp[i-1][j]; if(j)dp[i][j]+=dp[i][j-1]; } } go(n,m,s); } int main(){ FAST_IO //cout<<setprecision(20)<<fixed; //cin>>tc; while(tc--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (long long i = 0; i < (long long)(n); i++) typedef long long ll; int main() { ll A,B,K; cin>>A>>B>>K; int f=A;int g=B; string s=""; rep(i,A+B)s+='n'; rep(i,A+B){ int x=0; int y=0; for(int j=0;j<i;j++){ if(s[j]=='a')x++; else y++; } x=A-(x+1); y=B-y; ll com=1; rep(k,x){ com*=(x+y-k); com/=(k+1); } // cout<<com<<endl; if(f==0){ s[i]='b'; g--; } else if(g==0){ s[i]='a'; f--; } else if(com<K){ s[i]='b'; K-=com; g--; } else { s[i]='a'; f--; } //return 0; } cout<<s<<endl; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; #define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ff first #define ss second #define int long long #define ll long long #define pb push_back #define pii pair<int,int> #define vi vector<int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define mod 998244353 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define vpii vector<pair<int,int> > #define all(x) x.begin(),x.end() #define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";} #define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n" #define sz(x) (int)x.size() #define db(x) cout<<x<<"\n"; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //insert,find_by_order,order_of_key,lower_bound,upper_bound; #define TRACE #ifdef TRACE #define deb(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define deb(...) #endif //////////////////////////////code////////////////////////////// const int N = 2e5 + 5; void solve() { int n; cin >> n; map<int,int>m; vi b(n); for(int i = 0;i < n; i++) { int d; cin >> d; m[d]++; } for(auto &x : b) cin >> x; int ans = 0; for(int i = 0 ;i < n; i++) { int d; cin >> d; ans += m[b[d - 1]]; } db(ans) } int32_t main() { LETS_GET_SCHWIFTY; #ifndef ONLINE_JUDGE freopen("INP.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t = 1; // cin >> t; while (t--) solve(); } // check out for following mistakes- // if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only // is using prime sieve make sure it fits // when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
#include <bits/stdc++.h> #include <string> typedef long long ll; typedef long double ldb; typedef double db; using namespace std; #define ff first #define ss second #define pb push_back #define pf push_front #define ins insert #define mp make_pair #define len(s) s.length() #define forp(i, a, b) for (ll i = a; i <= b; i++) #define rep(i, n) for (ll i = 0; i < n; i++) #define ren(i, n) for (ll i = n - 1; i >= 0; i--) #define forn(i, a, b) for (ll i = a; i >= b; i--) #define on cout << endl #define o2(a, b) cout << a << " " << b #define os cout << " " #define all(v) v.begin(), v.end() #define sortall(v) sort(all(v)) #define mem(n, m) memset(n, m, sizeof(n)) #define vi vector<int> #define vl vector<ll> #define ld long double #define vld vector<ld> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define vvld vector<vector<ld>> #define pii pair<int, int> #define pll pair<ll, ll> #define vpii vector<pii> #define vpll vector<pll> #define mll map<ll, ll> #define lb lower_bound #define ub upper_bound #define ret return 0 #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define ford(container, it) for (__typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define fors(container, it, a, b) for (__typeof(container.begin()) it = a; it != b; it++) #define d1(x) cout << (x) << endl #define d2(x, y) cout << (x) << " " << (y) << endl #define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl #define d4(a, b, c, d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define max3(a, b, c) max(max(a, b), c) #define min3(a, b, c) min(min(a, b), c) #define boost ios_base::sync_with_stdio(0) #define MOD 1000000007 #define EPSILON 1e-9 #define PI 3.14159265358979323846 #define inf 999999999999999999 #define siz 100001 #define SIZ 1000001 #define SIZE 200001 #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 w(x) \ int x; \ cin >> x; \ while (x--) ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll pwr(ll x, ll y) { ll res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (b * a) / gcd(a, b); } ll fact(ll n) { if (n <= 1) return 1; return n * fact(n - 1); } ll nPr(ll n, ll r) { return fact(n) / fact(n - r); } ll cd(ll n) { ll c=0; while(n>0) { n/=10; c++; } return c; } ll log2(ll n) { ll c=0; while(n>0) { n/=2; c++; } return c; } ll XOR(ll x, ll y) { return (x | y) & (~x | ~y); } bool isprime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void c_p_c() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); c_p_c(); ll t; t = 1; // cin >> t; while (t-- > 0) { string s; cin>>s; rep(j, s.size()) { if(s[j]=='.') break; cout<<s[j]; } } return 0; }
#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) cout<<(T?"YES":"NO")<<endl #define yesno(T) cout<<(T?"yes":"no")<<endl #define YesNo(T) cout<<(T?"Yes":"No")<<endl const int INFint = 1 << 30; const ll INFLL = 1LL << 60; const ll MOD = 1000000007LL; const double pi = 3.14159265358979; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++){ cin >> a[i]; } sort(a.begin(), a.end()); reverse(a.begin(), a.end()); a.push_back(0); ll ans = 1; for (int i = 0; i < n; i++){ ans *= (a[i] - a[i + 1] + 1); ans %= MOD; } cout << ans << endl; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <iostream> #include <fstream> #ifndef JHELPER1_COMMON_H #define JHELPER1_COMMON_H #include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> typedef long long ll; using namespace std; // int max: 2,147,483,647 // long long max: 9,223,372,036,854,775,807 constexpr ll MOD = 1000L * 1000 * 1000 + 7L; constexpr ll INF = 1000L * 1000 * 1000 * 1000 * 1000 * 1000 + 7L; #define READ_VEC(v) for(auto &e: v){in >> e;} #define READ_PAIR_VEC(v) for(auto &e: v){in >> e.first >> e.second;} #define ALL(c) std::begin(c), std::end(c) #define for_(i, a, b) for(int i=(a); i<(b); i++) #define rfor_(i, a, b) for(int i=(b)-1; i>=(a); i--) #define rep(i, n) for(int i=0; i<(n); i++) #define rrep(i, n) for(int i=(n)-1; i>=0; i--) #endif //JHELPER1_COMMON_H class BARCWrecker { public: void solve(std::istream& in, std::ostream& out) { int n; in>>n; vector<ll> a(n); READ_VEC(a); sort(ALL(a)); ll tot = 1 + a[0]; rep(i, n-1) { tot *= (a[i+1] - a[i] + 1); tot %= MOD; } out << tot<<endl; } }; int main() { ios_base::sync_with_stdio(false); // Disable C++ and C sync cin.tie(NULL); // Disable cin and cout sync BARCWrecker solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; float x=1.08*n; x=round(x); if(x<206.0) { cout<<"Yay!"<<endl; } else if (x>206.0) { cout<<":("<<endl; } else if(x==206.0) { cout<<"so-so"<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; #define mcfor(i, L, R) for(ll i = ll(L); i < ll(R); i++) #define mcford(i, L, R) for(ll i = ll(L-1); i >= ll(R); i--) #define mcrep(i, N) for(ll i = 0; i < ll(N); i++) #define mcrepd(i, N) for(ll i = ll(N-1); i >= 0; i--) #define mcall(arr) arr.begin(), arr.end() #define mcsize(arr) ll(arr.size()) #define INF32 2147483647 #define INF64 9223372036854775807 //char '0-9': 48-57, 'A-M,N-Z': 65-77,78-90, // 'a-m,n-z': 97-109,110-122 //setprecision(10) int main(){ int N; cin >> N; double price = 1.08 * (double)N; if(price < 206){ cout << "Yay!" << endl; } else if(price < 207){ cout << "so-so" << endl; } else{ cout << ":(" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef _DEBUG #include "_DEBUG.hpp" #endif #define int long long const long long inf = 2e18; const int mod = 1e9 + 7; template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } template <class T> struct SegmentTree { using F = function<T(T, T)>; F f; T inf; int n; vector<T> node; SegmentTree() {} SegmentTree(vector<T> v, T inf, F f) : inf(inf), f(f) { n = 1; while (n < v.size()) n *= 2; node.resize(2 * n - 1, inf); for (int i = 0; i < v.size(); i++) node[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) node[i] = f(node[2 * i + 1], node[2 * i + 2]); } void update(int k, T val) { k += n - 1; node[k] = val; while (k > 0) { k = (k - 1) / 2; node[k] = f(node[2 * k + 1], node[2 * k + 2]); } } void add(int k, T val) { k += n - 1; node[k] += val; while (k > 0) { k = (k - 1) / 2; node[k] = f(node[2 * k + 1], node[2 * k + 2]); } } //区間[a, b)の値を返す T operator[](int x) { return getval(x, x + 1); } T getval(int a, int b) { return getval(a, b, 0, 0, n); } T getval(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return inf; if (a <= l && r <= b) return node[k]; T vl = getval(a, b, 2 * k + 1, l, (l + r) / 2); T vr = getval(a, b, 2 * k + 2, (l + r) / 2, r); return f(vl, vr); } void print(int n) { for (int i = 0; i < n; i++) { cout << getval(i, i + 1) << " "; } cout << endl; } }; signed main() { int n; cin >> n; vector<int> p(n - 1); cin >> p; vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { g[p[i] - 1].push_back(i + 1); g[i + 1].push_back(p[i] - 1); } int q; cin >> q; vector<vector<pair<int, int>>> query(n); for (int i = 0; i < q; i++) { int u, d; cin >> u >> d; u--; query[d].push_back({i, u}); } int num = 0; vector<int> in(n), out(n); vector<vector<int>> depth(n); auto dfs = [&](auto &&dfs, int u, int par, int d) -> void { in[u] = num++; depth[d].push_back(u); for (int v : g[u]) { if (v == par) continue; dfs(dfs, v, u, d + 1); } out[u] = num++; }; dfs(dfs, 0, -1, 0); SegmentTree<int> seg(vector<int>(num, 0), 0, [&](int x, int y) { return (x + y); }); vector<int> ans(q); for (int i = 0; i < n; i++) { for (int node : depth[i]) { seg.update(in[node], 1); } for (auto p : query[i]) { ans[p.first] = seg.getval(in[p.second], out[p.second] + 1); } for (int node : depth[i]) { seg.update(in[node], 0); } } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }
/* これを入れて実行 g++ code.cpp ./a.out */ #include <iostream> #include <cstdio> #include <stdio.h> #include <vector> #include <string> #include <cstring> #include <queue> #include <deque> #include <stack> #include <algorithm> #include <utility> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cmath> #include <math.h> #include <tuple> #include <iomanip> #include <bitset> #include <functional> #include <cassert> #include <random> #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 61; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){ return f.second > s.second; } ll gcd(ll a, ll b){ if (b == 0)return a; return gcd(b, a % b); } ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } ll conbinationMemo[61][31]; void cmemoInit(){ rep(i, 61){ rep(j, 31){ conbinationMemo[i][j] = -1; } } } ll nCr(ll n, ll r){ if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r]; if(r == 0 || r == n){ return 1; } else if(r == 1){ return n; } return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r){ r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- ll n; ll q; vector<vector<ll>> g; vector<vector<ll>> l; ll in[200200]; ll out[200200]; void dfs(ll now, ll pa, ll d, ll &time){ in[now] = time; l[d].push_back(time); time++; rep(i, g[now].size()){ ll next = g[now][i]; if(next == pa) continue; ll nextd = d + 1; dfs(next, now, nextd, time); } out[now] = time; time++; } int main(void){ cin >> n; g.resize(n); l.resize(n); rep(i, n - 1){ ll p; cin >> p; p--; g[p].push_back(i + 1); } ll time = 0; dfs(0, -1, 0, time); cin >> q; rep(i, q){ ll u, d; cin >> u >> d; u--; auto itr1 = lower_bound(all(l[d]), in[u]); auto itr2 = lower_bound(all(l[d]), out[u]); cout << itr2 - itr1 << endl; } }
#include <iostream> #include <string> #include <vector> #include <list> #include <set> #include <queue> #include <functional> #include <algorithm> #include <cmath> using namespace std; typedef int64_t ll; int main(int argc, char *argv[]) { ll b, c; cin >> b >> c; //b = -1e18; //c = 1; ll pmin = b - (c / 2); ll pmax = max(b + (c / 2) - 1, pmin); ll pnum = pmax - pmin + 1;//(c / 2) * 2; ll nmin = -b - (c - 1) / 2; ll nmax = -b + (c - 1) / 2; ll nnum = nmax - nmin + 1;//(c - 1) / 2 * 2 + 1; ll dnum = 0; ll dmin = max(pmin, nmin); ll dmax = min(pmax, nmax); if (dmin <= dmax) { dnum = dmax - dmin + 1; } cout << pnum + nnum - dnum << endl; 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; #define MV 200001 #define LMV 21 #define ff first #define ss second #define pb push_back #define eb emplace_back #define emp emplace #define mp make_pair #define ins insert #define sz(x) (int)x.size() #define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); } void whi(istream_iterator<string> it) { cerr<<"\n"; } template<typename T, typename... Args> void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); } void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);} void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);} void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);} typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<PII, int> PPII; typedef pair<PLL, ll> PPLL; typedef map<int, int> MII; const int MOD = 1000000007; const ll INF = 4e18; 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>; struct h_llint { 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); } }; struct h_pair{ size_t operator()(const PLL&x)const{ return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32)); } }; typedef map<ll, ll> MLL; typedef map<PII, int> MPII; typedef map<PLL, ll> MPLL; typedef set<int> SI; typedef set<ll> SL; //ordered_set = order_of_key(.) //ordered_set = find_by_order(.) typedef ordered_set<int> OSI; typedef ordered_set<ll> OSL; typedef ordered_multiset<int> MOSI; typedef ordered_multiset<ll> MOSL; typedef unordered_map<ll, int, h_llint> UMLI; typedef unordered_map<ll, ll, h_llint> UMLL; typedef unordered_map<PLL, int, h_pair> UMPI; typedef unordered_map<PLL, ll, h_pair> UMPL; int ar[MV]; ll arr[MV]; void solve(int T) { ll b,c; cin>>b>>c; VL cst(2); cst[0] = c; cst[1] = c-1; vector<PLL> af(2); auto rpt = [ & ](int yt){ if(cst[yt] & 1) { af[yt].ff = -b-(cst[yt]>>1LL); af[yt].ss = -b+(cst[yt]>>1LL); } else { af[yt].ff = b-(cst[yt]>>1LL); af[yt].ss = max(b, b+(cst[yt]>>1LL)-1); } return; }; ll ans = 0, iaf = -INF, ias = INF; for(auto _=0;_<2;_++) { rpt(_); ans += (af[_].ss - af[_].ff + 1); iaf = max(iaf, af[_].ff); ias = min(ias, af[_].ss); } ans -= max(0LL, (ias - iaf + 1)); cout<<ans<<"\n"; return; } int main(void) { FLASH(); //freopen("cowjog.in", "r", stdin); //freopen("cowjog.out", "w", stdout); int T; T = 1; #ifndef ONLINE_JUDGE time_t time_t1, time_t2; time_t1 = clock(); #endif //cin>>T; while(T--) solve(T); #ifndef ONLINE_JUDGE time_t2 = clock(); SETF(); cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n"; UNSETF(); #endif return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <map> #include <cstring> #include <cstdlib> #include <cmath> using namespace std; using ll = long long; map<ll, ll> mp; ll f(ll x, ll y) { auto it = mp.find(y); if (it != mp.end()) return it->second; if (x >= y) return x - y; ll r = y - x; if (y % 2) { r = min(r, f(x, (y - 1) / 2) + 2); r = min(r, f(x, (y + 1) / 2) + 2); } else { r = min(r, f(x, y / 2) + 1); } return mp[y] = r; } int main() { ll x, y; cin >> x >> y; cout << f(x, y) << endl; return 0; }
//Think simple yet elegant. #include <bits/stdc++.h> using namespace std; #define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long #define all(v) v.begin(),v.end() #define F first #define S second #define pb push_back #define mp make_pair #define pi pair<int,int> #define REP(i,n) for(int i=0;i<n;i++) const int N = 2e5+2; const ll mod = 1e9+7; map<ll,ll> dp; map<ll,bool> seen; ll x,y; ll solve(ll cur){ if(seen[cur]) return dp[cur]; if(cur<=x){ seen[cur]=true; dp[cur]=abs(cur-x); return dp[cur]; } ll ans = cur-x; if(cur%2==0) ans = min(ans,solve(cur/2)+1); else ans = min({ans,solve(cur+1)+1,solve(cur-1)+1}); seen[cur]=true; return dp[cur]=ans; } int main(){ fast; int tt; tt=1; while(tt--){ cin >> x >> y; cout<<solve(y)<<"\n"; } }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //Policy Based Data Structure using namespace __gnu_pbds; //Policy Based Data Structure using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //Policy Based Data Structure #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int> > #define mk(arr,n,type) type *arr = new type[n] #define fo(i,n) for(i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define int long long #define ld long double #define nn '\n' #define w(t) cin>>tc; while(tc--) #define deb(x) cout << #x << "=" << x << endl #define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define lb(v,x) lower_bound(v.begin(),v.end(),x) //returns address of number equal to or just greater than x ,else it is v.end() (i.e. not found) #define ub(v,x) upper_bound(v.begin(),v.end(),x) #define all(x) x.begin(), x.end() #define fill(a, b) memset(a, b, sizeof(a)) #define sortall(x) sort(all(x)) #define tr(it,a) for(auto it = a.begin(); it != a.end(); it++) #define ps(x,y) fixed<<setprecision(y)<<x #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define PI 3.1415926535897932384626 #define inf 1e18 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //Random Shuffler typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<vi> vvi; typedef map<int, int> mii; typedef map<char, int> mci; typedef map<string, int> msi; int mpow(int base, int exp); int lcm(int a, int b); void ipgraph(int m); void dfs(int u, int par); const int mod = 1000000007; const int N = 3e5, M = N; vi g[N]; // no of prime numbers in range : (70,19) , (1000,168) , (100000,1229) , (sqrt(10^9),3409) //======================= void sol() { int i=0, j, k, n,y, z, m,a,b,c,d; cin >> a >> b >> c >> d; int x=0; int e=0; int l=mpow(10,9); while(a>(d*e)) { a=a+b; e=e+c; i++; if(e*d>=a) { cout << i; x=-1; break; } if(i==l) { break; } } if(x!=-1) { cout << -1; } } int32_t main() { 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 sol(); return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; } int lcm(int a, int b) { int g = __gcd(a, b); return a / g * b; } void ipgraph(int m) { int i, u, v; while (m--) { cin >> u >> v; g[u - 1].pb(v - 1); g[v - 1].pb(u - 1); } } void dfs(int u, int par) { for (int v : g[u]) { if (v == par) continue; dfs(v, u); } }
#include <bits/stdc++.h> #include <time.h> #define rep(i, n) for(int i=0;i<(int)(n);i++) #define ALL(a) (a).begin(),(a).end() using namespace std; using ll=long long; typedef pair<int,int> P; bool LOCAL_TEST = false; int h[30][30],v[30][30]; double priority[30][30]; vector<string> paths; map<int,char> mp; inline void InitRand(){ srand((unsigned int)time(NULL)); } void read_s_t_a_e(){ int si,sj,ti,tj,a; double e; cin>>si>>sj>>ti>>tj>>a>>e; string path; if(si<=ti) rep(j,ti-si) path+='D'; else rep(j,si-ti) path+='U'; if(sj<=tj) rep(j,tj-sj) path+='R'; else rep(i,sj-tj) path+='L'; paths.push_back(path); } void move(int si,int sj,int ti,int tj,string& path){ int y=si,x=sj; while(y!=ti||x!=tj){ if(y==ti){ rep(i,abs(tj-x)) path+=mp[1]; break; } else if(x==tj){ rep(i,abs(ti-y)) path+=mp[0]; break; } int next=rand()%2; path+=mp[next]; if(next==0){ if(y<ti) y++; else y--; }else{ if(x<tj) x++; else x--; } } } void read_s_t(){ int si,sj,ti,tj; cin>>si>>sj>>ti>>tj; string path; if(si<=ti) mp[0]='D'; else mp[0]='U'; if(sj<=tj) mp[1]='R'; else mp[1]='L'; move(si,sj,ti,tj,path); cout<<path<<endl; flush(cout); int a; cin>>a; } int main(){ rep(i,30) fill(priority[i],priority[i]+30,-1); if(LOCAL_TEST){ rep(i,30) rep(j,29) cin>>h[i][j]; rep(i,29) rep(j,30) cin>>v[i][j]; } rep(k,1000){ if(LOCAL_TEST) read_s_t_a_e(); else read_s_t(); } if(LOCAL_TEST){ ofstream output("out.txt"); rep(k,1000) output<<paths[k]<<'\n'; } }
// coded by zeffy #pragma GCC optimize("O3","unroll-loops","omit-frame-pointer","inline") //Optimization flags #pragma GCC option("arch=native","tune=native","no-zeroupper") //Enable AVX #pragma GCC target("avx","popcnt") //Enable AVX #include <x86intrin.h> //SSE Extensions #include <bits/stdc++.h> using namespace std; #define eb emplace_back #define mp make_pair #define hello cout<<"hello"<<"\n" #define forr(i,a,b) for(int i=a;i<b;i++) #define it(s) for(auto itr:s) #define dvg(s) for(auto itr:s) cout<<itr<<" ";cout<<endl; #define dbg(s) cout<<#s<<"= "<<s<<endl; typedef long long int lli; typedef unsigned long long int ulli; const lli INF=(lli)1e17+5; const ulli MOD=1e9+7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); std::cout<< std::fixed; std::cout.precision(6); string s; cin>>s; forr(i,1,s.size()) cout<<s[i]; cout<<s[0]; return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <queue> using namespace std; typedef long long ll; int main(){ int L; cin >> L; L = L - 1; ll div = 11*10*9*8*7*6*5*4*3*2; ll ans = L; for(int i=1;i<11;i++){ ans = ans*(L-i)/(i+1); //cout << ans << endl; } cout << ans << endl; }
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define IT iterator #define V vector #define TP template <class o> #define TPP template <typename t1, typename t2> #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 REP(i, a, b) for (int i = b; i >= a; i--) #define FOR(i,n) rep(i,1,n) #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef unsigned ui; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; const int N = 2e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9; const ll INF = 1e15; // char buf[size],*p1=buf,*p2=buf; TP void qr(o& x) { char c = gc; x = 0; int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = gc; } while (isdigit(c)) x = x * 10 + c - '0', c = gc; x *= f; } TP void qw(o x) { if (x / 10) qw(x / 10); putchar(x % 10 + '0'); } TP void pr1(o x) { if (x < 0) x = -x, putchar('-'); qw(x); putchar(' '); } TP void pr2(o x) { if (x < 0) x = -x, putchar('-'); qw(x); putchar('\n'); } // math ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll power(ll a, ll b = mod - 2, ll p = mod) { ll c = 1; while (b) { if (b & 1) c = c * a % p; b /= 2; a = a * a % p; } return c; } TP void cmax(o& x, o y) { if (x < y) x = y; } TP void cmin(o& x, o y) { if (x > y) x = y; } TPP void ad(t1& x, t2 y) { x += y; if (x >= mod) x -= mod; } TPP void dl(t1& x, t2 y) { x -= y; if (x < 0) x += mod; } template<typename T> struct BIT { T* c; int n; // require you to define 0 as the initial value !! BIT(int _n):n(_n){c=new T[n];c--; FOR(i,n) c[i]=T(0); } void add(int x,T y) { for( ;x<=n;x += x&-x) c[x]=c[x]+y; } T sum(int x) {T y=T(0); for( ;x;x &= x-1) y=y+c[x]; return y;} }; // dbinom ll jc[N], inv[N]; void jc_init(int n) { jc[0] = 1; for (int i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % mod; inv[n] = power(jc[n]); for (int i = n; i; i--) inv[i - 1] = inv[i] * i % mod; } ll C(int x, int y) { if (x < y || y < 0) return 0; return jc[x] * inv[y] % mod * inv[x - y] % mod; } int n,cnt[1100]; void solve() { qr(n); FOR(i,n) { int x; qr(x); for(int j=2;j*j<=x;j++) if(x%j==0) { cnt[j]++; while(x%j==0) x /= j; } cnt[x]++; } pr2(max_element(cnt+2,cnt+1001)-cnt); } int main() { int T = 1; // qr(T); while (T--) solve(); return 0; }
#include <bits/stdc++.h> #define ll long long int #define pb push_back #define umap unordered_map #define mod 998244353 #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define MN(a,b,c) min(a,min(b,c)) #define MX(a,b,c) max(a,max(b,c)) #define pr1 pair<ll,ll> #define F first #define S second #define mP make_pair #define f(i,n) for(int i=0;i<n;i++) #define f1(i,x,y) for(int i=x;i<=y;i++) #define f2(i,x,y) for(int i=x;i>=y;i--) #define yes cout<<"YES"<<"\n" #define no cout<<"NO"<<"\n" #define modsum(a,b) ((a%mod)+(b%mod))%mod #define modpro(a,b) ((a%mod)*(b%mod))%mod //__builtin_popcount(x) //__builtin_parity(x) =(number of set bits)%2 //__builtin_clz(x) to count the number of leading zeroes //__builtin_ctz(x) to count the number of trailing zeroes //__gcd(a,b) using namespace std; /*ll modularExponentiation(ll x,ll n,ll M) { ll result=1; while(n>0) { if(n % 2 ==1) result=((result%M) * (x%M))%M; x=((x%M)*(x%M))%M; n=n/2; } return result; } ll binaryExponentiation(ll x,ll n) { ll result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } ll pow1(ll x,ll y) { ll z=1; while(y--){z=z*x;} return z; } bool isprime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; }*/ ll check(ll x) { ll y=1,z=0; while(x>y){y*=2;z++;} if(x!=y){z--;} return z; } ll nCrModp(ll n, ll r, ll p) { // Optimization for the cases when r is large if (r > n - r) r = n - r; // The array C is going to store last row of // pascal triangle at the end. And last entry // of last row is nCr ll C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (ll i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (ll j = min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n;cin>>n; string s;cin>>s; map <pair<ll,ll>,ll> mp; vector <ll> diffA(n,0),diffG(n,0); ll num=0; f(i,n) { if(i==0) { if(s[i]=='A') diffA[i]=1; else if(s[i]=='T') diffA[i]=-1; else if(s[i]=='G') diffG[i]=1; else diffG[i]=-1; } else { if(s[i]=='A') {diffA[i]=1+diffA[i-1];diffG[i]=diffG[i-1];} else if(s[i]=='T') {diffA[i]=diffA[i-1]-1;diffG[i]=diffG[i-1];} else if(s[i]=='G') {diffG[i]=1+diffG[i-1];diffA[i]=diffA[i-1];} else {diffG[i]=diffG[i-1]-1;diffA[i]=diffA[i-1];} } if(diffA[i]==0 && diffG[i]==0) num++; mp[{diffA[i]+diffG[i],diffA[i]-diffG[i]}]++; } f(i,n) { ll x=diffA[i]+diffG[i],y=diffA[i]-diffG[i]; mp[{x,y}]--; if(mp[{x,y}]>0) num+=mp[{x,y}]; } cout<<num; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxl=3e5+10; const int mod=1e9+7; int n,m,k,cnt,tot,cas,ans; int a[maxl]; bool vis[maxl]; char s[maxl]; inline ll qp(ll a,ll b) { ll ans=1,cnt=a; while(b) { if(b&1) ans=ans*cnt%mod; cnt=cnt*cnt%mod; b>>=1; } return ans; } inline ll c(ll n,ll r) { if(r>n || r<0) return 0; ll fz=1,fm=1; for(int i=1;i<=r;i++) fz=fz*(n-i+1)%mod,fm=fm*i%mod; return fz*qp(fm,mod-2)%mod; } inline void prework() { ll sum=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i]; printf("%lld\n",c(n+m,sum+n)); } inline void mainwork() { } inline void print() { } int main() { int t=1; //scanf("%d",&t); for(cas=1;cas<=t;cas++) { prework(); mainwork(); print(); } return 0; }
#include <bits/stdc++.h> using namespace std; // modint template<int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() const { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } friend constexpr istream& operator >> (istream& is, Fp<MOD>& x) noexcept { is >> x.val; x.val %= MOD; if (x.val < 0) x.val += MOD; return is; } friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD>& r, long long n) noexcept { if (n == 0) return 1; if (n < 0) return modpow(modinv(r), -n); auto t = modpow(r, n / 2); t = t * t; if (n & 1) t = t * r; return t; } friend constexpr Fp<MOD> modinv(const Fp<MOD>& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } return Fp<MOD>(u); } }; const int MOD = 1000000007; using mint = Fp<MOD>; int main() { long long N, M; cin >> N >> M; long long S = 0; vector<int> A(N); for (int i = 0; i < N; ++i) cin >> A[i], S += A[i]; mint res = 1; for (long long r = 0; r < N+S; ++r) { res *= N + M - r; res /= N + S - r; } cout << res << endl; }
#include <bits/stdc++.h> #define se second #define sz(x) (int)(x.size()) #define ll long long #define rep(i,x,y) for(int i = x; i <= y; ++i) #define repr(i,x,y) for(int i = x; i >= y; --i) #define pb push_back #define mp make_pair #define fi first #define ull unsigned ll #define pi 3.14159265358979 using namespace std; const int N = (int)(1e6) + 322; const ll INF = 2e18 + 17; const int inf = 2e9; const int mod = 1000000007; const double eps = 1e-9; template<typename T> void pop_front(vector<T>& vec) { assert(!vec.empty()); vec.erase(vec.begin()); } int gcd(int a, int b) { return b ? gcd (b, a % b) : a; } int nbr_bits(int a){ return log2(a)+1; } int fastpow(int a, int n, int mod) { if(n==0) return 1; if(n%2==0){ int res = fastpow(a,n/2,mod); res*=res; res%=mod; return res; } int res = (fastpow(a,n-1,mod)*a)%mod; return res; } // --------------------------------------------------- int p[N]; stack<string> answers; int find(int u){ if(p[u]==u) return u; return p[u] = find(p[u]); } bool bubleSort(vector<int> v, int n){ int ans = 0; int m = (n * (n -1)) / 2; rep(i, 0, n-1){ rep(j, 0, n-2){ if(v[j] > v[j + 1]){ swap(v[j], v[j + 1]); ans++; } } } if(ans < m) return true; else return false; } int n; bool isPalinadrome(string str){ if(str.size() == 1) return true; rep(i, 0, (str.size() / 2) - 1 ) { if(str[i] == str[str.size() - i - 1]) continue; return false; } return true; } ll fact(ll n) { if(n == 0 || n == 1 ) return 1; else return (n * fact(n-1)) % mod; } signed main() { ios_base::sync_with_stdio(false); int a, b, c; cin >> a >> b >> c; int a2 = fastpow(a, 2, mod); int b2 = fastpow(b, 2, mod); int c2 = fastpow(c, 2, mod); int sum = a2 + b2; if(sum < c2) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include<bits/stdc++.h> using ll = int_fast64_t; using P = std::pair<ll,ll>; using PP = std::pair<ll,P>; #define REP(i,b,e) for(int i=b; i<e; i++) #define PRINT(vec) {printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");} #define fi first #define se second const int MOD = 1e9+7; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; int main(){ int a, b, c; scanf("%d %d %d", &a, &b, &c); if(a*a + b*b < c*c) puts("Yes"); else puts("No"); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for(int(i) = 0; (i) < (n); ++(i)) #define REPR(i, n) for(int(i) = (n); (i) >= 0; --(i)) #define FOR(i, n, m) for(int(i) = (n); (i) < (m); ++(i)) // clang-format off 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; } // clang-format on constexpr int INF = 1e9; class Timer { chrono::time_point<chrono::system_clock> start; public: Timer() { start = chrono::system_clock::now(); } double duration() { return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - start).count(); // return chrono::duration<chrono::milliseconds>(chrono::system_clock::now()- start).count(); } }; class xorShift { uint32_t x, y, z, w; static const double TO_DOUBLE; public: xorShift() { x = 123456789; y = 362436069; z = 521288629; w = 88675123; } xorShift(uint32_t seed) { x = seed = 1812433253u * (seed ^ (seed >> 30)); y = seed = 1812433253u * (seed ^ (seed >> 30)) + 1; z = seed = 1812433253u * (seed ^ (seed >> 30)) + 2; w = seed = 1812433253u * (seed ^ (seed >> 30)) + 3; } uint32_t nextInt(uint32_t n) { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w % n; } uint32_t nextInt() { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } double nextDouble() { return nextInt() * TO_DOUBLE; } }; const double xorShift::TO_DOUBLE = 1.0 / (1LL << 32); Timer T; xorShift rnd; constexpr double TIME_LIMIT = 1800; constexpr int N = 20; int M; vector<string> s; vector<string> using_s; bool internal_string(const string& word, const vector<string> strings) { int n = word.size(); REP(i, (int)strings.size()) { int m = strings[i].size(); for(int j = 0; j + n <= m; j++) { if(word == strings[i].substr(j)) { return true; } } } return false; } void select_using_s(const vector<string>& origin, vector<string>& out) { int n = origin.size(); out.push_back(origin[0]); FOR(i, 1, n) { if(!internal_string(origin[i], out)) { out.push_back(origin[i]); } } } struct Solver { vector<string> ans, best_ans; int best_score; Solver() : ans(N), best_ans(N), best_score(0) {} void first_method() { string all_string; REP(i, (int)using_s.size()) { all_string += using_s[i]; } REP(i, N) { string line; REP(j, N) { if(i * N + j < (int)all_string.size()) { line += all_string[i * N + j]; } else { line += "."; } } best_ans[i] = line; } } // void random_select() { // vector<int> st; // REP(i, (int)using_s.size()) { st.push_back(i); } // string all_string; // REP(i, (int)using_s.size()) { // int id = rnd.nextInt((int)st.size()); // all_string += st[id]; // st.erase(st.begin() + id); // } // REP(i, N) { // REP(j, N) { // int id = i * N + j; // if(id < // } // } // } void print_best_ans() { REP(i, N) { cout << best_ans[i] << endl; } } }; int main() { int n; cin >> n >> M; s.resize(M); REP(i, M) { cin >> s[i]; } Solver solver; sort(s.begin(), s.end(), [](const string& l, const string& r) -> bool { if(l.size() == r.size()) { return l < r; } return l.size() > r.size(); }); select_using_s(s, using_s); solver.first_method(); solver.print_best_ans(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x,y) cout<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) #define pw(b,p) pow(b,p) + 0.1 #define endl "\n" mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(int32_t x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif char myHeap[200 * 1024 * 1024]; int sz = 0; void assert_tle(bool q) { while (!q); } void* operator new ( std::size_t count ) { sz += count; assert_tle(sz <= 200 * 1024 * 1024); return myHeap + sz - count; } void operator delete (void *ptr) { } int power(int a,int b) { int ans=1; while(b) { if(inf/ans<5) return 5; if(b&1) ans*=a; if(inf/a<a) return 5; a*=a; b>>=1; } return ans; } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int dfs(int cur,vector<int> v[],int used[],int ar[]) { if(used[cur]) return 0; used[cur]=1; int ret=ar[cur]; for(int kid: v[cur]) { ret+=dfs(kid,v,used,ar); } return ret; } void solve() { int n,m; cin>>n>>m; if(n!=1&&(m<0||m>=n-1)) { cout<<-1; return; } int req=m+1; //if(m<) int start= 1e6; int i=0; for(;i<req;i++) { cout<<start<<" "<<start+1<<endl; start+=2; } for(int l=1;i<n;i++,l++) { cout<<l<<" "<<100000000-l<<endl; } } int32_t main() { fastIO(); //w(t) { solve(); cout<<endl; } return 0; }
#include <bits/stdc++.h> #include <algorithm> using namespace std; #define rep(i, n) for(int i = 0; i < (int)(n); i++) int main() { int N; int64_t ans = -1; cin >> N; vector<vector<int64_t>> v(N, vector<int64_t>(3)); rep(i, N) { rep(j, 3) cin >> v.at(i).at(j); v.at(i).at(0) *= 2; if(v.at(i).at(0) < 2*v.at(i).at(2)-1) { if(ans == -1) ans = v.at(i).at(1); else ans = min(ans, v.at(i).at(1)); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef std::vector<std::vector<int64_t> > Graph; #define rep(i, n) for (int64_t i = 0; i < n; ++i) #define rep2(i, n) for (int64_t i = 1; i <= n; ++i) #define repb(i, l, n) for (int64_t i = l; i < n; ++i) #define repb2(i, l, n) for (int64_t i = l; i <= n; ++i) #define repc(i, l, n, d) for (int64_t i = l; i < n; i+=d) #define repc2(i, l, n, d) for (int64_t i = l; i <= n; i+=d) #define repi(a, b) for (auto&(a) : (b)) #define ALL(v) (v).begin(), (v).end() #define Sort(x) sort(ALL(x)) #define Sort_rev(x) Sort(x);reverse(ALL(x)) #define Sort_pair(x, p) sort(ALL(x), (p)) #define mp(a, b) make_pair((a), (b)) #define Push_back(a, b) push_back( mp( (a), (b) ) ) #define ctoi(c) (c)-'0' template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<typename V,typename T> bool find_num(V v, T num) { if ( find(ALL(v), num) == v.end() ) { return false; } return true; } const int inf = 0x3fffffff; const int64_t INF = 0x3fffffffffffffff; const int64_t MOD = 1e9+7; vector<int64_t> enum_div(int64_t n) { vector<int64_t> ret; for(int64_t i = 1; i*i <= n; ++i){ if(n%i == 0){ ret.push_back(i); if(i*i != n){ ret.push_back(n/i); } } } return ret; } int main() { int64_t n; cin >> n; std::vector<int64_t> v = enum_div(2*n); int64_t ans = 0; rep(i, v.size()) { int64_t num = (2*n/v[i])-(v[i]-1); if (num%2 == 0) ++ans; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define mp make_pair #define fi first #define se second #define REP(i,j) for(int i=0;i<j;i++) #define REPA(i,j) for(int i=1;i<=j;i++) #define FORN(i,j,k) for(int i=j;i<k;i++) #define vi vector<int> #define vvi vector<vi > #define pii pair<int,int> #define vpii vector<pii > #define all(a) a.begin(),a.end() using namespace std; const int INF=1<<30; inline void read(int &x){ // short neg=1; x=0; char c=getchar(); /*while(c<'0'||c>'9'){ if(c=='-')neg=-1; c=getchar(); }*/ while(c>='0'&&c<='9'){ x=(x<<3)+(x<<1)+(c^48),c=getchar(); } // x*=neg; } int n; int a[10010]; int dp[10010][10010]; int main(void){ read(n); int ans=0; REP(i,n){ read(a[i]); dp[i][i]=a[i]; ans=max(ans,a[i]); } for(int l=1;l<n;l++){ for(int i=0;i+l<n;i++){ dp[i][i+l]=min(dp[i][i+l-1],a[i+l]); ans=max(ans,dp[i][i+l]*(l+1)); } } printf("%d",ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define sz(v) int((v).size()) #define all(x) (x).begin(), (x).end() #define forn(i, n) for (int i = 1; i <= int(n); ++i) //출처: https://codeforces.com/blog/entry/68809 void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << (#x) << "] = [", _print(x) #else #define debug(x...) #endif #define int ll const int MOD = 998244353; int power(int a, int n) { int res = 1; for (;n; n >>= 1, a = (a * a) % MOD) if (n & 1) res = (res * a) % MOD; return res; } void add(int& a, int b){a = (a%MOD + b%MOD)%MOD;} int h, w, k; char grid[5050][5050]; int dp[5050][5050][4]; int f(char c) { return c == 'R'? 2 : (c== 'D'); } void solve() { cin>>h>>w>>k; forn(_, k) { int a, b; char c; cin>>a>>b>>c; grid[a][b] = c; } int r = h*w - k; if (grid[1][1]) dp[1][1][f(grid[1][1])] = power(3, r); else dp[1][1][3] = power(3, r); int p3 = power(3, MOD - 2); forn(i, h) forn(j, w) { if (grid[i+1][j]) { add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('D')]); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('X')]); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3); } else { add(dp[i + 1][j][3], dp[i][j][f('D')]); add(dp[i + 1][j][3], dp[i][j][f('X')]); add(dp[i + 1][j][3], dp[i][j][3] * p3); add(dp[i + 1][j][3], dp[i][j][3] * p3); } if (grid[i][j+1]) { add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('R')]); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('X')]); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3); } else { add(dp[i][j + 1][3], dp[i][j][f('R')]); add(dp[i][j + 1][3], dp[i][j][f('X')]); add(dp[i][j + 1][3], dp[i][j][3] * p3); add(dp[i][j + 1][3], dp[i][j][3] * p3); } // for (int k = 0; k < 4; ++k) debug(i, j, k, dp[i][j][k]); } if (grid[h][w]) cout << dp[h][w][f(grid[h][w])] << '\n'; else cout << dp[h][w][3] << '\n'; } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int n,m,a,b,ans; bool f[30][30]; void dfs(int x,int y,int z) { if(y==m+1) { x++; y=1; } if(z==a) { ans++; return; } if(x==n+1) return; if(f[x][y]) dfs(x,y+1,z); else { if(!f[x][y+1]&&y+1<=m) { f[x][y+1]=true; dfs(x,y+2,z+1); f[x][y+1]=false; } if(!f[x+1][y]&&x+1<=n) { f[x+1][y]=true; dfs(x,y+1,z+1); f[x+1][y]=false; } dfs(x,y+1,z); } } int main() { scanf("%d%d%d%d",&n,&m,&a,&b); dfs(1,1,0); cout<<ans; return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define ll long long #define dl double long using namespace std; const int N = 1e6 + 7; const long long mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n,m; int a,b; int c[20]; ll d[(1 << 16)]; ll nd[(1 << 16)]; bool good(int x, int y) { int cnt = 0; for(int i = 0; i < m; i++){ int x1 = ((x >> i) & 1); int y1 = ((y >> i) & 1); if(y1 == 0){ if(cnt)return false; } if(x1 == 0 && y1 == 0)return false; if(x1 == 0){ if(cnt)return false; }else if(y1 == 1){ cnt ^= 1; } } if(cnt)return false; return true; } void solve() { cin >> n >> m >> a >> b; if(n < m)swap(n, m); int x = n * m; ll ans = 0; for(int i = 0; i < (1 << x); i++){ if(__builtin_popcount(i) != b)continue; for(int j = 0; j < n; j++)c[j] = 0; for(int j = 0; j < x; j++){ if(((i >> j) & 1)){ c[j / m] |= (1 << j % m); } } for(int j = 0; j < (1 << m); j++)nd[j] = d[j] = 0; nd[(1 << m) - 1] = 1; for(int j = 0; j < n; j++){ for(int h = 0; h < (1 << m); h++ ){ if(j > 0 && (h & c[j - 1]) != c[j - 1])continue; for(int g = 0; g < (1 << m); g++){ if((g & c[j]) != c[j])continue; int nh = h; int ng = (g ^ c[j]); if(good(nh, ng)){ d[g] += nd[h]; } } } for(int h = 0; h < (1 << m); h++){ nd[h] = d[h]; d[h] = 0; } } ans += nd[(1 << m) - 1]; } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); int T = 1; //cin >> T; while( T-- ){ solve(); } }
#ifdef LOCAL //#define _GLIBCXX_DEBUG #endif //#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, int> Pi; typedef vector<ll> Vec; typedef vector<int> Vi; typedef vector<string> Vs; typedef vector<char> Vc; typedef vector<P> VP; typedef vector<vector<ll>> VV; typedef vector<vector<int>> VVi; typedef vector<vector<char>> VVc; typedef vector<vector<vector<ll>>> VVV; typedef vector<vector<vector<vector<ll>>>> VVVV; #define endl '\n' #define REP(i, a, b) for(ll i=(a); i<(b); i++) #define PER(i, a, b) for(ll i=(a); i>=(b); i--) #define rep(i, n) REP(i, 0, n) #define per(i, n) PER(i, n, 0) const ll INF=1e18+18; const ll MOD=1000000007; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define ALL(v) v.begin(), v.end() #define rALL(v) v.rbegin(), v.rend() #define pb(x) push_back(x) #define mp(a, b) make_pair(a,b) #define Each(a,b) for(auto &a :b) #define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i) #ifdef LOCAL #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;} #define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl; #define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;} #define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl; #else #define dbg(...) #define dbgmap(...) #define dbgset(...) #define dbgarr(...) #define dbgdp(...) #endif #define out(a) cout<<a<<endl #define out2(a,b) cout<<a<<" "<<b<<endl #define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl #define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define fi first #define se second template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } template<typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; } template<typename T>istream& operator>>(istream&i,vector<T>&v) {rep(j,v.size())i>>v[j];return i;} // vector template<typename T> ostream &operator<<(ostream &s, const vector<T> &v) { int len=v.size(); for(int i=0; i<len; ++i) { s<<v[i]; if(i<len-1) s<<" "; } return s; } // 2 dimentional vector template<typename T> ostream &operator<<(ostream &s, const vector<vector<T> > &vv) { int len=vv.size(); for(int i=0; i<len; ++i) { s<<vv[i]<<endl; } return s; } int solve(){ ll h,w; cin>>h>>w; VV g(h,Vec(w)); cin>>g; ll ma = INF; rep(i,h){ rep(j,w){ chmin(ma, g[i][j]); } } ll ans = 0; rep(i,h){ rep(j,w){ ans += g[i][j] - ma; } } out(ans); return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout<<std::setprecision(10); // ll T; // cin>>T; // while(T--) solve(); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxl=3e5+10; int n,m,k,tot,cnt; int a[maxl];ll b[maxl],ans[maxl]; char s[maxl]; bool vis[maxl]; inline void add(int i,int x) { while(i<=n) b[i]+=x,i+=i&-i; } inline ll sum(int i) { ll ret=0; while(i) ret+=b[i],i-=i&-i; return ret; } inline void prework() { scanf("%d",&n); ll now=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]),a[i]++; now+=sum(n)-sum(a[i]); add(a[i],1); } ans[0]=now; for(int i=1;i<=n-1;i++) { now-=sum(a[i]-1); add(a[i],-1); now+=sum(n)-sum(a[i]); add(a[i],1); ans[i]=now; } for(int i=0;i<n;i++) printf("%lld\n",ans[i]); } inline void mainwork() { } inline void print() { } int main() { int t=1; //scanf("%d",&t); while(t--) { prework(); mainwork(); print(); } return 0; }
#include<bits/stdc++.h> using ll= long long; #define REP(i,n) for(ll i=0;i<ll(n);i++) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define ALL(x) x.begin(),x.end() #define INF (int)1e9 //10^9:∞ #define LLINF (long long)1e12 #define MOD (int)1e9+7 //10^9+7:合同式の法 #define PI 3.141592653589 #define PB push_back #define fi first #define se second #define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;} #define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;} #define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;} #define Graph vector<vector<int>> #define PII pair<int,int> #define PLL pair<ll,ll> #define VI vector<int> #define VVI vector<vector<int>> #define VPII vector<pair<int,int>> #define VPLL vector<pair<ll,ll>> #define DDD fixed<<setprecision(10) using namespace std; /*.....................USUFUL FUNCTIONS ......................*/ ll cmin(ll a,ll b){ if(a<b) return a; else return b; } ll cmax(ll a,ll b){ if(a>b) return a; else return b; } /*..................DEFINE GLOBAL VARIABLES...................*/ /*.....................DEFINE FUNCTIONS ......................*/ /*.........................kemkemG0...........................*/ int main() { int N; int A[1010],B[1010]; cin>>N; PII V[1010]; PII W[1010]; REP(i,N){ cin>>A[i]>>B[i]; V[i]=PII(A[i],i); W[i]=PII(B[i],i); } sort(V,V+N); sort(W,W+N); if(V[0].second!=W[0].second){ cout<<max(V[0].first,W[0].first); }else{ int a=V[0].first+W[0].first; int b=max(V[0].first,W[1].first); int c=max(V[1].first,W[0].first); cout<<min(a,min(b,c)); } }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define For(x,y) for(int x = 0; x < y; x++) int a[3]; int main(){ ios::sync_with_stdio(0), cin.tie(0); For(i, 3) cin >> a[i]; sort(a,a+3); if(a[2] - a[1] == a[1] - a[0]) cout << "Yes"; else cout << "No"; }
#include <bits/stdc++.h> #include <string> #include <map> #include <set> #include <queue> #include <deque> #include <vector> #define loop(i,a,b) for (int i = (a); i <= (b); i++) #define loopr(i, a, b) for(int i = (a); i >= (b); i--) #define pb(a) push_back(a) #define pf(a) push_front(a) #define mp(a,b) make_pair(a,b) #define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define ll long long int #define mod 1000000007 #define mod1 998244353 using namespace std; bool decToOctal(int n) { // array to store octal number int octalNum[100]; // counter for octal number array int i = 0; while (n != 0) { // storing remainder in octal array octalNum[i] = n % 8; n = n / 8; i++; } int ok = 0; // printing octal number array in reverse order for (int j = i - 1; j >= 0; j--) { if(octalNum[j]==7) { ok = 1; break; } } if(ok) { return false; } else { return true; } } int main() { int n; cin>>n; int ans = 0; loop(i,1,n) { int x = 0; string s = to_string(i); x = count(s.begin(),s.end(),'7'); if(x==0 and decToOctal(i)==true) { ans++; } } cout<<ans; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define FOR(i, a) for(int i = 0; i < (a); ++i) #define rrep(i, a, b) for(int i = a; i >= (b); --i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)(x).size() #define ft first #define sd second #define pb push_back #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<pair<ll,ll>> vll; const ll MOD = 1000000007; void solve(){ int n;cin>>n; int t[n]; double r[n],l[n]; rep(i,0,n) { cin>>t[i]>>l[i]>>r[i]; if (t[i]==2) r[i]-=0.01; else if (t[i]==3) l[i]+=0.01; else if (t[i]==4) { l[i]+=0.01; r[i]-=0.01; } } int ans(0); rep(i,0,n){ rep(j,i+1,n){ if (l[i]>r[j] || r[i]<l[j]){ //cout<<l[i]<<" "<<r[i]<<" "<<l[j]<<" "<<r[j]<<endl; continue; } else { ans++; } } } cout<<ans<<endl; } 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> //#include <atcoder/all> //using namespace atcoder; //#include <boost/multiprecision/cpp_int.hpp> //#include <boost/multiprecision/cpp_dec_float.hpp> //using namespace boost::multiprecision; using namespace std; #pragma region Macros using ll = long long; #define int ll using pii = pair<int, int>; using tiii = tuple<int, int, int>; template<class T = int> using V = vector<T>; template<class T = int> using VV = V<V<T>>; #define IOS\ ios::sync_with_stdio(false);\ cin.tie(0);\ cout.tie(0); #define FOR(i,l,r) for(int i=(l);i<int(r);++i) #define REP(i,n) FOR(i,0,n) #define REPS(i,n) FOR(i,1,n+1) #define RFOR(i,l,r) for(int i=(l);i>=int(r);--i) #define RREP(i,n) RFOR(i,n-1,0) #define RREPS(i,n) RFOR(i,n,1) #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define all(x) (x).begin(),(x).end() #define SORT(name) sort(name.begin(), name.end()) #define RSORT(name)\ SORT(name);\ reverse(all(name)); #define ZERO(p) memset(p, 0, sizeof(p)) #define MINUS(p) memset(p, -1, sizeof(p)) inline void Yes(bool b = true) {cout << (b ? "Yes" : "No") << '\n';} inline void YES(bool b = true) {cout << (b ? "YES" : "NO") << '\n';} template <class T> inline void print(T x){ cout << x << '\n';} 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; } const ll LLINF = (1LL<<60); const int INF = (1LL<<30); const double DINF = std::numeric_limits<double>::infinity(); #pragma endregion const int MOD = 1000000007; #if 1 # define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__) #else # define DBG(fmt, ...) #endif const int MAX_N = 100010; signed main() { IOS; string S; cin >> S; REP(i, int(S.length())) { if(i % 2) { if(S[i] >= 'a' && S[i] <= 'z') { print("No"); return 0; } } else { if(S[i] >= 'A' && S[i] <= 'Z') { print("No"); return 0; } } } print("Yes"); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) #define REP(i,m,n) for (int i = (m); i < (n); ++i) #define rrep(i,n) for (int i = (n)-1; i >= 0; --i) #define RREP(i,m,n) for (int i = (n)-1; i >= (m); ++i) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using namespace std; using ll = long long int; using vec = vector<int>; using P = pair<int,int>; const int INF = 1e9+7; void pv(vec v) { for (int e : v) { cout << e << " "; } cout << endl; } void dfs(vector<vec> &dis, vec &is, vec &os, vector<vec> &tree, int v, int d, int &now) { is[v] = now; dis[d].push_back(now); now++; for (int w : tree[v]) { dfs(dis, is, os, tree, w, d+1, now); } os[v] = now; now++; } int main() { int n; cin >> n; vector<vec> tree(n); vector<vec> dis(n); vec is(n), os(n); rep(i,n-1) { int p; cin >> p; p--; tree[p].push_back(i+1); } int now = 0; dfs(dis, is, os, tree, 0, 0, now); for (vec &di : dis) { sort(all(di)); } int q; cin >> q; rep(i, q) { int u, d; cin >> u >> d; u--; vec &di = dis[d]; cout << lower_bound(all(di), os[u]) - lower_bound(all(di), is[u]) << endl; } }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> using ll = long long; using namespace std; template<typename A, typename B> bool chmin(A &a, const B b) { if (a <= b) return false; a = b; return true; } template<typename A, typename B> bool chmax(A &a, const B b) { if (a >= b) return false; a = b; return true; } #ifndef LOCAL #define debug(...) ; #else #define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl; template<typename T> ostream &operator<<(ostream &out, const vector<T> &v); template<typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template<typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } void _tostr_rec(ostringstream &oss) { oss << "\b\b \b"; } template<typename Head, typename... Tail> void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) { oss << head << ", "; _tostr_rec(oss, forward<Tail>(tail)...); } template<typename... T> string _tostr(T &&...args) { ostringstream oss; int size = sizeof...(args); if (size > 1) oss << "{"; _tostr_rec(oss, forward<T>(args)...); if (size > 1) oss << "}"; return oss.str(); } #endif constexpr int mod = 1'000'000'007; //1e9+7(prime number) constexpr int INF = 1'000'000'000; //1e9 constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18 constexpr int SIZE = 300010; int A[SIZE]; int counter[SIZE]; int main() { int N, K; scanf("%d%d", &N, &K); for (int i = 0; i < N; i++) { scanf("%d", A + i); counter[A[i]]++; } int now = K, prev; ll ans = 0; for (int i = 0; i <= N; i++) { prev = now; chmin(now, counter[i]); ans += (prev - now) * i; } cout << ans << endl; return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<set> #include<map> #include<queue> #include<cmath> #include<iomanip> #include<cstring> #include<complex> #include<cstdio> #define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1; #define fi first #define se second #define pb push_back #define pii pair<int,int> #define ppi pair<pii,int> #define pip pair<int,pii> #define ll long long #define pll pair<ll,ll> #define rep(i,n) for(int i=0;i<n;i++) #define repd(i,n) for(int i=n-1;i>=0;i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 1000005 #define mid(l,r) l+(r-l)/2 #define removeduplicates(vec) vec.erase( unique( vec.begin(), vec.end() ), vec.end() ) using namespace std; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1}; void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;} ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);} void solve(){ ll n,k; cin>>n>>k; int a[n]; int mp[n+1]={0}; int maxi=0; rep(i,n){ cin>>a[i]; mp[a[i]]++; maxi=max(maxi,a[i]); } ll ans=0; mp[maxi+1]=0; for(ll x=0;x<=n;x++){ ll f=mp[x]; ll can=min(f,k); ans+=(k-can)*x; k=can; } cout<<ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while(t--){ solve(); } }
// atcoder/abc190/E/main.cpp // author: @___Johniel // github: https://github.com/johniel/ #include <bits/stdc++.h> #define each(i, c) for (auto& i : c) #define unless(cond) if (!(cond)) using namespace std; template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; } template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; } template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; } template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; } template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; } template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); } template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); } using lli = long long int; using ull = unsigned long long; using point = complex<double>; using str = string; template<typename T> using vec = vector<T>; constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1}); constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1}); constexpr lli mod = 1e9 + 7; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.setf(ios_base::fixed); cout.precision(15); int n, m; while (cin >> n >> m) { vec<int> g[n]; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } int k; cin >> k; vec<int> c(k); cin >> c; each (i, c) --i; const int K = 17 + 1; const int N = 1e5 + 5; static int d[K][N]; const int inf = 1 << 28; fill(&d[0][0], &d[K - 1][N - 1], inf); for (int i = 0; i < k; ++i) { const int src = c[i]; queue<int> q; d[i][src] = 0; for (q.push(src); q.size(); q.pop()) { int curr = q.front(); each (next, g[curr]) { if (d[i][next] == inf) { d[i][next] = d[i][curr] + 1; q.push(next); } } } } const int BIT = 1 << K; static int dp[BIT][K]; fill(&dp[0][0], &dp[BIT - 1][K - 1] + 1, inf); dp[0][0] = 0; for (int i = 0; i < k; ++i) { dp[1 << i][i] = 1; } for (int bit = 0; bit < (1 << k); ++bit) { for (int i = 0; i < k; ++i) { unless (bit & (1 << i)) continue; if (dp[bit][i] == inf) continue; for (int j = 0; j < k; ++j) { if (i != j) { setmin(dp[bit | (1 << j)][j], dp[bit][i] + d[i][c[j]]); } } } } int mn = inf; for (int i = 0; i < k; ++i) { setmin(mn, dp[(1 << k) - 1][i]); } cout << (mn == inf ? -1 : mn) << endl; } return 0; }
#include <bits/stdc++.h> #define N 200000 #define MAX 1000000000 #define E 0.00000001 #define MOD 1000000007 #define INF 0x3f3f3f3f #define LEFT(x) (2 * x) #define RIGHT(x) (2 * x + 1) #define PAR 0 #define IMPAR 1 using namespace std; int n, m; map<int, vector<int>> columns; map<pair<int,int>, int> dp; int getI(int i, int j){ if(!columns.count(j)) return -1; int r = lower_bound(columns[j].begin(), columns[j].end(), i) - columns[j].begin() - 1; if(r < 0 or r > columns[j].size()-1){ return -1; } return columns[j][r]; } int solve(int i, int j){ if(i == -1) return 0; if(j == n and i == 0) return 1; if(dp.count({i,j})) return dp[{i,j}]; int left = solve(getI(i, j-1), j-1); int right = solve(getI(i, j+1), j+1); return dp[{i,j}] = (left or right); } int main(){ scanf("%d %d", &n, &m); for(int k = 0; k < m; k++){ int i, j; scanf("%d %d", &i, &j); columns[j].push_back(i); } columns[n].push_back(0); for(auto& j : columns){ sort(j.second.begin(), j.second.end()); } int ans = 0; for(auto j : columns){ ans += solve(j.second.back(), j.first); } printf("%d\n", ans); return 0; }
// DeNsE - EcLiPsE // // WHAT is DEAD may NEVER die // #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <map> #include <unordered_map> #include <cmath> #include <iomanip> #include <set> #include <cstring> #include <stack> #include <sstream> #include <queue> #include <unordered_set> #include <cstdlib> using namespace std; #define nitro ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define int long long #define double long double //#define endl " \n" const int inf = (1ll << 62) - 1; const int mod = 998244353; const int N = 2e5 + 10; const double pi = 3.14159265358979323846; int bPow(int a, int b){ int res = 1; while(b) { if (b & 1) { res = (res * a) % mod; } b >>= 1; a = (a * a) % mod; } return res % mod; } int gcd(int a, int b) { if(a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } vector<int> fact(N, 0ll); void factorial(){ fact[0] = 1, fact[1] = 1; for(int i = 2; i < N; i++){ fact[i] = (fact[i - 1] * i) % mod; } } int ncr(int n, int r){ if(r > n) return 0; int ans = fact[n] % mod; ans *= bPow(fact[r], mod - 2) % mod; ans %= mod; ans *= bPow(fact[n - r], mod - 2) % mod; ans %= mod; return ans; } vector<int> primes(N, -1), factors(N, 2); void sieve(){ iota(primes.begin(), primes.end(), 0); for (int i = 2; i * i <= N; ++i) { if(primes[i] == i){ for (int j = 2 * i; j < N; j += i) { primes[j] = i; factors[j]++; } } } } void solve() { int n, k; cin >> n >> k; vector<int> a(2 * n + 1, 0); for (int i = 1; i < a.size(); ++i) { a[i] = min(i - 1, 2 * n + 1 - i); } int ans = 0; for (int i = 1; i < a.size(); ++i) { int ab = i; int cd = ab - k; if(cd < a.size()) ans += a[ab] * a[cd]; } cout << ans << endl; } signed main(){ nitro int tc = 1, test = 1; //cin >> tc; while(tc--){ //cout << "Case #" << test++ << ": "; solve(); } }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define f1 first #define s2 second #define fastio ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); #define debug(x...) cerr << "[" << #x << "]: " << x << "\n"; typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> pl; ld const PI = 4*atan((ld)1); ll f(ll n, ll x) { if (x < 2 || x > 2*n) return 0; x = min(x, -x + 2*n + 2); return x-1; } int main() { fastio; int n, k; cin >> n >> k; ll res = 0; for (ll x = 2; x <= 2 * n; ++x) { ll y = x - k; if (y >= 2) res += f(n, x) * f(n, y); } cout << res << '\n'; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define mp make_pair #define mt make_tuple #define fi first #define se second #define pb push_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define rep(i, k, n) for (int i = (int)(k); i < (int)(n); ++i) #define rrep(i, k, n) for (int i = (int)(n) - 1; i >= (int)(k); --i) typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; typedef vector<vii> vv_ii; typedef pair<int,ii> i_ii; typedef vector<i_ii> vi_ii; template <class T> inline ostream& operator<<(ostream & o, vector<T> const& v) { for (int i = 0; i < (int)(v.size()) - 1; ++i) o << v[i] << ' '; if (!v.empty()) o << v.back(); return o; } template <class T, class Q> inline ostream& operator<<(ostream & o, pair<T,Q> const& p) { o << "(" << p.fi << "," << p.se << ")"; return o; } #define INF 1e9 #define MOD 1000000007 char grid[2001][2001]; int r, c; const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; inline bool ok(int x, int y) { return x >= 0 && y >= 0 && x < r && y < c; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> r >> c; int sx, sy, tx, ty; vv_ii m(30); rep(i, 0, r) { cin >> grid[i]; rep(j, 0, c) { if (grid[i][j] == 'S') sx = i, sy = j; else if (grid[i][j] == 'G') tx = i, ty = j; else if (grid[i][j] != '#') { m[grid[i][j]-'a'].push_back({i, j}); } } } queue<ii> q; vvi dist(r, vi(c, INF)); vvi orig(r, vi(c)); q.push({sx, sy}); q.push({tx, ty}); dist[sx][sy] = dist[tx][ty] = 0; orig[sx][sy] = 0; orig[tx][ty] = 1; int ans = -1; while (!q.empty()) { auto f = q.front(); q.pop(); int ux = f.fi, uy = f.se; //if (ux == tx && uy == ty) // break; rep(k, 0, 4) { int vx = ux + dx[k]; int vy = uy + dy[k]; if (ok(vx, vy) && grid[vx][vy] != '#') { if (dist[vx][vy] == INF) { dist[vx][vy] = 1 + dist[ux][uy]; orig[vx][vy] = orig[ux][uy]; //if (vx == tx && vy == ty) //goto PRINT; q.push({vx, vy}); } else if (orig[ux][uy] != orig[vx][vy]) { ans = dist[ux][uy] + dist[vx][vy] + 1; goto PRINT; } } } if (grid[ux][uy] >= 'a' && grid[ux][uy] <= 'z') { for (auto it : m[grid[ux][uy]-'a']) { int vx = it.fi, vy = it.se; if (dist[vx][vy] == INF) { dist[vx][vy] = 1 + dist[ux][uy]; orig[vx][vy] = orig[ux][uy]; //if (it.fi == tx && it.se == ty) //goto PRINT; q.push({vx, vy}); } else if (orig[vx][vy] != orig[ux][uy]) { ans = dist[ux][uy] + dist[vx][vy] + 1; goto PRINT; } } } } PRINT: cout << ans << '\n'; /* if (dist[tx][ty] == INF) cout << -1 << '\n'; else cout << dist[tx][ty] << '\n'; */ return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <stack> #include <tuple> #include <cmath> #include <iomanip> #include <map> #include <cstring> #include <functional> #include <cctype> #include <locale> #define ll long long #define rep(i,n) for(int i=0;i<(n);i++) #define rrep(i,n) for(int i=n-1;i>=0;i--) #define fi first #define se second #define pb push_back #define ALL(a) (a).begin(),(a).end() using namespace std; 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;} typedef pair<int,int> P; typedef pair<long long,long long> Pll; #define fout(num) cout << fixed << setprecision(20) << (num) << endl //s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ //vector<vector<ll>> dp(n,vector<ll>(n)) //2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d)); //3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d))); signed main(){ std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); int n; cin >> n; vector<P> v(n); vector<int> v1(2*n+1,0),v2(2*n+1,0); bool flag=0; vector<bool> vis(2*n+1,false); rep(i,n){ cin >> v[i].fi >> v[i].se; if(v[i].fi!=-1){ if(vis[v[i].fi]) flag=true; vis[v[i].fi]=true; v1[v[i].fi]=v[i].se; if(v[i].se!=-1){ if(vis[v[i].se]) flag=true; vis[v[i].se]=true; v2[v[i].se]=v[i].fi; } } else if(v[i].se!=-1){ v2[v[i].se]=v[i].fi; if(vis[v[i].se]) flag=true; vis[v[i].se]=true; } } if(flag){ cout << "No" << endl; return 0; } vector<bool> dp(n+1,false); dp[n]=true; for(int i=n-1;i>=0;i--){ for(int j=i+1;j<=n;j++){ if(dp[j]==true){ int d=j-i; //pair number bool ok=1; for(int k=0;k<d;k++){ int ii=2*i+1+k; int jj=ii+d; bool fl=0; if(v1[ii]==jj&&v2[jj]==ii) fl=1; if(v1[ii]==-1&&v2[jj]==0) fl=1; if(v1[ii]==0&&v2[jj]==-1) fl=1; if(v1[ii]==0&&v2[ii]==0&&v1[jj]==0&&v2[jj]==0) fl=1; if(!fl) ok=0; } //cout << ok << " " << okk << endl; if(ok) dp[i]=true; } } } //rep(i,n) cout << dp[i] << " "; if(dp[0]) cout << "Yes" << endl; else cout << "No" << endl; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define mt make_tuple #define pii pair<int,int> #define pll pair<ll,ll> #define ldb double template<typename T>void ckmn(T&a,T b){a=min(a,b);} template<typename T>void ckmx(T&a,T b){a=max(a,b);} void rd(int&x){scanf("%i",&x);} void rd(ll&x){scanf("%lld",&x);} void rd(char*x){scanf("%s",x);} void rd(ldb&x){scanf("%lf",&x);} void rd(string&x){scanf("%s",&x);} template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);} template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);} template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);} template<typename T>void rd(){T x;rd(x);return x;} int ri(){int x;rd(x);return x;} template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;} template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);} template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);} template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);} void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}} template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}} const int mod=1e9+7; int add(int a,int b){a+=b;return a>=mod?a-mod:a;} void ckadd(int&a,int b){a=add(a,b);} int sub(int a,int b){a-=b;return a<0?a+mod:a;} void cksub(int&a,int b){a=sub(a,b);} int mul(int a,int b){return (ll)a*b%mod;} void ckmul(int&a,int b){a=mul(a,b);} int powmod(int x,int k){int ans=1;for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);return ans;} int inv(int x){return powmod(x,mod-2);} const int N=10050; int F[N],I[N]; int binom(int n, int k){ return mul(F[n],mul(I[k],I[n-k]));} void calc() { F[0]=1; for(int i=1;i<N;i++) F[i]=mul(F[i-1],i); I[N-1]=inv(F[N-1]); for(int i=N-2;~i;i--) I[i]=mul(I[i+1],i+1); } int main(){ int n=ri(); calc(); char AA,AB,BA,BB; scanf("\n%c",&AA); scanf("\n%c",&AB); scanf("\n%c",&BA); scanf("\n%c",&BB); if(n<=3)return 0*printf("1\n"); if(AA=='A'&&BB=='B')return 0*printf("1\n"); if(AA=='B'&&BB=='A'){ if(AB!=BA)return 0*printf("%i\n",powmod(2,n-3)); int ans=0; for(int ba=0;ba*2<=n-2;ba++){ int a=n-2-ba*2; ckadd(ans,binom(ba+a,a)); } printf("%i\n",ans); return 0; } if(AA=='A'){ AA='B'; AB=AB=='A'?'B':'A'; BA=BA=='A'?'B':'A'; BB='B'; //swap(AB,BA); } //AA -> ABA //BB -> BBB if(AB=='B'){ //AB -> ABB printf("1\n"); return 0; }else{ //AB -> AAB if(BA=='A'){ //BA -> BAA int ans=0; for(int ba=0;ba*2<=n-2;ba++){ int a=n-2-ba*2; ckadd(ans,binom(ba+a,a)); } printf("%i\n",ans); return 0; }else{ //BA -> BBA int ans=powmod(2,n-3); printf("%i\n",ans); return 0; } } return 0; }
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); } lint ceil(lint a, lint b) { return (a + b - 1) / b; } lint digit(lint a) { return (lint)log10(a); } const lint MOD = 1e9 + 7, INF = 1e18; lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 }; void YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; } typedef pair<lint, string> Pa; typedef pair<lint, plint> tlint; template <std::int_fast64_t Modulus> class modint { using u64 = std::int_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {} constexpr u64& value() noexcept { return a; } constexpr const u64& value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint& operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint& operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint& operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint& operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; typedef modint<MOD> ModInt; ModInt mod_pow(ModInt x, lint n) { ModInt ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; } return ret; } ModInt func[200000]; void funcinit(int N) { func[0] = 1; for (int i = 1; i <= N; i++) { func[i] = func[i - 1] * i; } } ModInt comb(ModInt n, ModInt r) { if (n.a <= 0 || n.a < r.a) { return 1; } return func[n.a] / (func[r.a] * func[(n - r).a]); } lint N; char c[4]; set<string> st; void dfs(string s) { if (SZ(s) == 5) { st.insert(s); return; } REP(i, SZ(s) - 1) { if (s[i] == 'A' && s[i + 1] == 'A') dfs(s.substr(0, i + 1) + c[0] + s.substr(i + 1, SZ(s) - i - 1)); if (s[i] == 'A' && s[i + 1] == 'B') dfs(s.substr(0, i + 1) + c[1] + s.substr(i + 1, SZ(s) - i - 1)); if (s[i] == 'B' && s[i + 1] == 'A') dfs(s.substr(0, i + 1) + c[2] + s.substr(i + 1, SZ(s) - i - 1)); if (s[i] == 'B' && s[i + 1] == 'B') dfs(s.substr(0, i + 1) + c[3] + s.substr(i + 1, SZ(s) - i - 1)); } } int main() { cin >> N; REP(i, 4) cin >> c[i]; dfs("AB"); if (SZ(st) == 1 || N < 4) { cout << 1 << endk; } else if (SZ(st) == 4 && N >= 4) { cout << mod_pow(2, N - 3).a << endk; } else { ModInt dp[10000]; dp[2] = 1; dp[3] = 1; REP(i, N) { dp[i + 4] = dp[i + 3] + dp[i + 2]; } cout << dp[N].a << endk; } }
#include <bits/stdc++.h> using namespace std; #define ll long long ll d=1e9+7; ll pw(ll x,ll y,ll p){ if(x==0&&y==0)return 1; ll r=1; x=x%p; if(x==0)return 0; while(y>0){ if(y&1)r=(r*x)%p; y=y>>1;x=(x*x)%p; } return r; } int main() { //freopen("test.in","r",stdin);cout<<endl<<endl<<endl; ll n,p;cin>>n>>p; ll c=p-1;ll e=pw(p-2,n-1,d); cout<<(c*e)%d<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; // type alias typedef long long LL; typedef pair<int,int> II; typedef tuple<int,int,int> III; typedef vector<int> VI; typedef vector<string> VS; typedef unordered_map<int,int> MAPII; typedef unordered_set<int> SETI; template<class T> using VV=vector<vector<T>>; // minmax template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; } template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; } // repetition #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) for(int i=0;i<=(n);++i) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) for(int i=0;i<(n);++i) #define FORR(x,arr) for(auto& x:arr) #define SZ(a) int((a).size()) // collection #define ALL(c) (c).begin(),(c).end() // DP #define MINUS(dp) memset(dp, -1, sizeof(dp)) #define ZERO(dp) memset(dp, 0, sizeof(dp)) // stdout #define println(args...) fprintf(stdout, ##args),putchar('\n'); // debug cerr template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; } void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; } template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); } template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; } #define __KUMATRACE__ true #ifdef __KUMATRACE__ #define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); } #define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; } #define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); } #else #define dump(args...) #define dumpc(ar) #define dumpC(beg,end) #endif const int MOD=1000000007; //const int MOD=998244353; struct ModInt { unsigned int val; ModInt(): val(0) {} ModInt(int v) { norm(v%MOD); } ModInt(long long v) { norm(v%MOD); } ModInt& norm(long long v) { v=v<0?v%MOD+MOD:v; // negative v=v>=MOD?v-MOD:v; // mod val=(unsigned int)v; return *this; } explicit operator bool() const { return val!=0; } ModInt operator-() const { return ModInt(0)-*this; } ModInt &operator+=(ModInt that) { return norm((long long)val+that.val); } ModInt &operator-=(ModInt that) { return norm((long long)val-that.val); } ModInt &operator*=(ModInt that) { val=(unsigned long long)val*that.val%MOD; return *this; } ModInt &operator/=(ModInt that) { return *this*=that.inv(); } ModInt operator+(ModInt that) const { return ModInt(*this)+=that; } ModInt operator-(ModInt that) const { return ModInt(*this)-=that; } ModInt operator*(ModInt that) const { return ModInt(*this)*=that; } ModInt operator/(ModInt that) const { return ModInt(*this)/=that; } ModInt pow(long long n) const { ModInt x=*this, res=1; while(n>0) { if(n&1) res*=x; x*=x,n>>=1; } return res; } ModInt inv() const { return (*this).pow(MOD-2); } bool operator==(ModInt that) const { return val==that.val; } bool operator!=(ModInt that) const { return val!=that.val; } friend ostream& operator<<(ostream& os, const ModInt& that) { return os<<that.val; } }; // $ cp-batch NowhereP | diff NowhereP.out - // $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address NowhereP.cpp && ./a.out /* 4/17/2021 0:24- */ LL N,P; void solve() { auto res=ModInt(P-1)*ModInt(P-2).pow(N-1); cout<<res<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout<<setprecision(12)<<fixed; cin>>N>>P; solve(); return 0; }
//#include <atcoder/maxflow.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #include <iostream> #include <map> #include <list> #include <set> #include <algorithm> #include <vector> #include <string> #include <functional> #include <queue> #include <deque> #include <stack> #include <unordered_map> #include <unordered_set> #include <cmath> #include <iterator> #include <random> #include <chrono> #include <complex> #include <bitset> #include <fstream> #define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) int i; cin >> i #define readll(i) ll i; cin >> i #define readdouble(i) double i; cin >> i #define readstring(s) string s; cin >> s typedef long long ll; //using namespace __gnu_pbds; //using namespace atcoder; using namespace std; const ll modd = (1000LL * 1000LL * 1000LL + 7LL); //const ll modd = 998244353; ll binary_search(function<bool(ll)> func, ll start, ll end) { /* func:int ->bool returns smallest int x where func(x) evaluates to true, searches in [start,end), it is assumed the values are false, .. , false, true ... */ if (end <= start) { return end; } // has to be here, otherwise func(end-1) in next line could be a problem if (!func(end-1)) { return end; } while (end-start>1) { ll mid = (start+end)/2; if (func(mid)) { end = mid; } else { start = mid; } } if (func(start)) { return start; } else { return end; } }; int main(int argc, char *argv[]) { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); // rand_gen(rng) gets the rand no ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(12); // readint(test_cases); int test_cases = 1; forr(t, 1, test_cases) { readint(n); readint(k); vector<vector<ll>> a; ll minn = modd*modd; forr(i,0,n) { a.push_back(vector<ll>()); forr(j,0,n) { readll(aa); minn = min(minn, aa); a.back().push_back(aa); } } int bel = k*k - (k*k)/2; ll x = binary_search([&a,bel,k,n](ll med) { vector<vector<ll>> aa(n, vector<ll>(n, 0)); forr(i,0,n) { forr(j,0,n) { aa[i][j] = (a[i][j]<=med); } } vector<vector<ll>> summ(n+1, vector<ll>(n+1, 0)); forr(i,1,n) { forr(j,1,n) { summ[i][j] = -summ[i-1][j-1] + summ[i][j-1] + summ[i-1][j] + aa[i-1][j-1]; } } bool ret = false; forr(i,0,n-k+1) { forr(j,0,n-k+1) { ret |= (summ[i+k][j+k]-summ[i][j+k]-summ[i+k][j]+summ[i][j]>=bel); } } return ret; }, 0, modd); cout << x << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> acum; bool check_median(int median, int K, vector<vector<int>> &A){ int N = A.size() - 1; acum.assign(N+1, vector<int>(N+1, 0)); for(int i = 1; i <= N; i++){ for(int j = 1; j <= N; j++){ acum[i][j] = A[i][j] <= median; acum[i][j] += acum[i][j-1] + acum[i-1][j] - acum[i-1][j-1]; } } for(int i = K; i <= N; i++){ for(int j = K; j <= N; j++){ int cnt_less = acum[i][j]; cnt_less -= acum[i-K][j] + acum[i][j-K] - acum[i-K][j-K]; if(cnt_less >= (K*K + 1)/2){ // printf("(%d, %d) -- %d\n", i, j, cnt_less); return true; } } } return false; } int main(){ int N, K; while(scanf("%d%d", &N, &K) > 0){ vector<vector<int>> A(N+1, vector<int>(N+1)); for(int i = 1; i <= N; i++){ for(int j = 1; j <= N; j++){ scanf("%d", &A[i][j]); } } int lo = 0, up = 1e9, ans; while(lo <= up){ int mid = (lo + up) >> 1; if(check_median(mid, K, A)){ ans = mid; up = mid-1; } else { lo = mid+1; } } printf("%d\n", ans); } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) //方向を決定する演算子的なもの int di[] = {-1, 0, 1, 0}; // int dj[] = {0, -1, 0, 1}; // int main() { int H, W, X, Y; cin >> H >> W >> X >> Y; --X; //配列変数の関係上デクリメント --Y; //同様 //H行W列のマスの入力 vector <string> s(H); rep(i, H) cin >> s[i]; int ans = 1; //自身がいるマスを考慮,初期値1 rep(k, 4) { //k=0上方向,k=1左方向,k=2下方向,k=3右方向 int ni = X; int nj = Y; //マスから出るか、「#」にぶつかるまでkで与えた方向へ動かす while(true) { ni += di[k]; nj += dj[k]; if(ni < 0 || nj < 0 || ni >= H || nj >= W) break; if(s[ni][nj] == '#') break; ++ans; } } cout << ans << endl; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll=long long; using ld=long double; using pll=pair<ll, ll>; //using mint = modint1000000007; #define rep(i,n) for (ll i=0; i<n; ++i) #define all(c) begin(c),end(c) #define PI acos(-1) #define oo 2e18 template<typename T1, typename T2> bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} template<typename T1, typename T2> bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} //priority_queue<ll, vector<ll>, greater<ll>> Q; /* */ //UnionFind vector<vector<ll>> G; vector<bool> seen; struct UnionFind{ vector<ll> par,siz; UnionFind(ll N):par(N+1), siz(N+1, 1){ for(int i = 0; i <= N; i++) par[i] = i; } void mkpar(ll n, ll set){//親はset=根 for(auto next_n: G[n]){ if(seen[next_n]) continue; seen[next_n] = true; par[next_n] = set; mkpar(next_n, set); siz[n] += siz[next_n]; } } void mkpar2(ll n){//親は1個上。sizとれる。 for (auto next_n: G[n]){ if (seen[next_n]) continue; seen[next_n] = true; par[next_n] = n; mkpar2(next_n); siz[n] += siz[next_n]; } } ll root(ll x){ if(par[x] == x) return x; return par[x] = root(par[x]); } void unite(ll x, ll y){ ll rx = root(x), ry = root(y); if(rx == ry) return; par[ry] = rx; siz[rx] += siz[ry]; } bool issame(ll x, ll y){ return root(x) == root(y); } ll size(ll x){ return siz[root(x)]; } }; ll N, M; ll X[30]; ll ans=1; ll tmp=0; ll tsize=0; ll cnt=0; bool check(ll n){ for(auto v: G[n]){ if(X[n]==X[v]) return true; } return false; } void dfs2(ll n){ for(ll i=1; i<=3; i++){ //まず、隣り合ってないか確認 X[n]=i; if(check(n)) continue; if(cnt==tsize) tmp++; else{ for(auto v:G[n]){ if(X[v]) continue; dfs2(v); } } } X[n]=0; } vector<ll> L; void dfs(ll n){ if(n==tsize){ tmp++; return ; } ll v=L[n]; if(n==0){ X[v]=1; dfs(n+1); } else{ for(ll j=1; j<=3; j++){ X[v]=j; if(check(v)) continue; dfs(n+1); } X[v]=0; } } int main(){ cin >> N >> M; G.assign(N+1, vector<ll>()); seen.assign(N+1, false); rep(i, M){ ll a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } UnionFind tree(N); for(ll i = 1; i <= N; i++){ seen[i] = true; tree.mkpar(i, i); } for(int i = 1; i <= N; i++){ if(tree.par[i]==i){ tmp=0; L.assign(0, 0); for(int j = 1; j <= N; j++) if(tree.par[j]==i) L.push_back(j); tsize=L.size(); if(tsize==1) ans*=3; else{ dfs(0); ans *= tmp*3; } } } cout << ans << endl; }
#include<iostream> #include<vector> #include<algorithm> int main() { int n; std::cin >> n; std::vector<int> t(n),l(n), r(n); for (int i = 0; i < n; i++) { std::cin >> t[i] >> l[i] >> r[i]; } int count = 0; for (int i = 0; i < n-1; i++) { for (int j = i + 1; j < n; j++) { //printf("%d %d %d %d\n",l[i],l[j],r[i],r[j]); if (std::min(r[i], r[j]) - std::max(l[i], l[j]) >= 0)count++; if (r[i] == l[j] || r[j] == l[i]) { if (l[i] < l[j]) { if(((t[i]==1)||t[i]==3)&&(t[j]==2||t[j]==1)){} else count--; } else { if (((t[j] == 1) || t[j] == 3) && (t[i] == 2 || t[i] == 1)) {} else count--; } } } } std::cout << count << std::endl; }
#include <bits/stdc++.h> using namespace std; #define maxn 105 string arr[maxn]; bool vis[maxn]; int n; int dfs(int x) { int ret = 1; vis[x] = true; for(int y = 0; y < n; y++) { if(!vis[y] && arr[y][x] == '1') { ret += dfs(y); } } return ret; } int main() { cin >> n; for(int i = 0; i < n; i++) { cin >> arr[i]; } double ans = 0.0; for(int i = 0; i < n; i++) { memset(vis, 0, sizeof(vis)); ans += 1.00 / dfs(i); } printf("%.12f\n", ans); return 0; }
/* -*- compile-command: "g++ --std=c++17 -o sol sol.cpp" -*- */ #include <iostream> #include <vector> #include <string> #include <set> #include <stack> #include <limits.h> #include <math.h> #include <memory> #include <cstring> #include <algorithm> #include <unordered_set> using namespace std; static auto speedup = []() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }(); int main(int argc, char *argv[]) { int N, X; cin >> N >> X; int S = 0; int i = 0; X *=100; for(; i < N; ++i) { int v, p; cin >> v >> p; S += v*p; if(S > X) { break; } } if(i == N) cout << -1 << endl; else cout << i + 1 << endl; return 0; }
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <cassert> #include <cfloat> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; //#include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i) #define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i) #define Sort(v) sort(v.begin(), v.end()) #define rSort(v) sort(v.rbegin(), v.rend()) #define Reverse(v) reverse(v.begin(), v.end()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) using ll = long long; using ull = unsigned long long; using P = pair<ll, ll>; using T = tuple<ll, ll, ll>; using vll = vector<ll>; using vP = vector<P>; using vT = vector<T>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vvP = vector<vector<P>>; using dqll = deque<ll>; ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0}; ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; 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; } /* Macros reg. ends here */ const ll INF = 1LL << 50; static const long long mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream& is, mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } class modutils { vector<mint> fact, invfact; public: modutils(int n = 200005) : fact(n + 1), invfact(n + 1) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; invfact[n] = fact[n].inv(); for (int i = n; i >= 1; i--) invfact[i - 1] = invfact[i] * i; } mint pow(mint x, ll n) { return x.pow(n); } mint comb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return fact[n] * invfact[k] * invfact[n - k]; } mint perm(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return fact[n] * invfact[n - k]; } mint fac(ll n) { return fact[n]; } }; int main() { // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); ll n, x; cin >> n >> x; x *= 100; ll now = 0; rep(i, n){ ll v, p; cin >> v >> p; ll add = v*p; now += add; if(now > x){ cout << i+1 << endl; return 0; } } cout << -1 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; class Abc { public: int solve(int n) { int temp=0,temp1=0,p=0,q=0,f=0; p=n,q=n; while(p>0 && q>0) { temp=p%10; if(temp==7) { f=1; break; } temp1=q%8; if(temp1==7) { f=1; break; } p=p/10; q=q/8; } return f; } }; int main() { Abc ob; int n,i,cnt=0; cin>>n; for(i=1; i<=n; i++) { if(ob.solve(i)==1) cnt++; } cout<<n-cnt<<endl; return 0; }
#include<bits/stdc++.h> #define _USE_MATH_DEFINES using namespace std; #define ll long long int #define ld double #define pb push_back #define rep(i , j , n) for(ll i = j ; i < n ; i++) #define pre(i , j , n) for(ll i = j ; i >= n ; i--) #define all(x) x.begin(), x.end() typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<double> vd; typedef vector<bool> vb; typedef pair<ll,ll> pll; #define br "\n" #define ff first #define ss second #define gcd __gcd #define debug(a...) cout<<#a<<": ";for(auto it:a)cout<<it<<" ";cout<<endl; ll MAX = 1e6 + 1; #define MAXIM 200005 ll mod = 1e9 + 7; void solve(){ ll n; cin >> n; ll ans = 0; rep(i,1,n + 1){ ll t1 = i,t2 = i; bool f1 = true,f2 = true; while(t1 > 0){ if(t1 % 10 == 7) f1 = false; t1 /= 10; } while(t2 > 0){ if(t2 % 8 == 7) f2 = false; t2 /= 8; } if(f1 && f2) ans++; } cout << ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll t = 1; // cin >> t; // calc(); rep(i,0,t){ // cout << "Case #" << i + 1 << ": "; solve(); // test(); } }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vb = vector<bool>; using vl = vector<long>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvl = vector<vector<long>>; using pii = pair<int, int>; using pil = pair<int, long>; using pll = pair<long, long>; #define fix20 cout << fixed << setprecision(20) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define rep(i,n) for(long i=0; i<(long)(n);i++) #define REP(i,s,t) for(long i=s; i<t; i++) #define RNG(i,s,t,u) for(long i=s; i<t; i+=u) #define MOD 1000000007 #define all(vec) vec.begin(), vec.end() template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main(){ long n; cin >> n; vector<pll> d(n); //cout << n << endl; rep(i,n){ cin >> d.at(i).first >> d.at(i).second; } sort(all(d)); long ans = 0; //cout << n << endl; rep(i,n){ REP(j,i+1,n){ long xs = d.at(i).first; long ys = d.at(i).second; long xt = d.at(j).first; long yt = d.at(j).second; long xv = xt - xs; long yv = yt - ys; REP(k,j+1,n){ long xm = d.at(k).first; long ym = d.at(k).second; //cout << xs << ys << xt << yt << xm << ym << endl; //cout << xv << yv << endl; if(xv == 0){ if(xm == xs) { //cout << xs << ys << xt << yt << xm << ym << endl; ans++; } }else{ if((xm - xs) * yv % xv != 0) continue; long cy = ys + (xm - xs) * yv / xv; if(cy == ym){ ans++; } } } } } if(ans > 0) YES; else NO; }
#include <bits/stdc++.h> using namespace std; #include <algorithm> #define ll long long int #define pb push_back #define fo(i, n) for (int i = 0; i < n; i++) #define vct vector<long long int> const ll INF = 1e18; const ll NINF = -1e18; #define f first #define s second int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; vector<ll> v, v1; for (int i = 0; i < n; i++) { ll a; cin >> a; v.pb(a); } for (int i = 0; i < m; i++) { ll b; cin >> b; v1.pb(b); } vector<ll> v2; if (m > n) { for (int i = 0; i <n; i++) { auto it = find(v1.begin(), v1.end(), v[i]); if(it ==v1.end()){ v2.pb(v[i]); } else{ v1.erase(it); } } v2.insert(v2.end(), v1.begin(), v1.end()); sort(v2.begin(), v2.end()); for (int i = 0; i < v2.size(); i++) { cout<<v2[i]<<" "; } } else{ for (int i = 0; i <m; i++) { auto it = find(v.begin(), v.end(), v1[i]); if(it ==v.end()){ v2.pb(v1[i]); } else{ v.erase(it); } } // cout<<v2.size()<<endl; v2.insert(v2.end(), v.begin(), v.end()); sort(v2.begin(), v2.end()); for (int i = 0; i < v2.size(); i++) { cout<<v2[i]<<" "; } } 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; //#define int long long //#pragma GCC optimize("Ofast") //#pragma comment(linker, "/stack:200000000") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4") #define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout); #define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define all(x) x.begin(), x.end() #define sz(s) (int)s.size() #define pb push_back #define ppb pop_back #define mp make_pair #define s second #define f first typedef pair < long long, long long > pll; typedef pair < int, int > pii; typedef unsigned long long ull; typedef vector < pii > vpii; typedef vector < int > vi; typedef long double ldb; typedef long long ll; typedef double db; typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set; const int inf = 1e9, maxn = 2e5 + 48, mod = 998244353, N = 2e5 + 12; const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 300; const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9); const db eps = 1e-12, pi = acos(-1); const ll INF = 1e18; bool was[N]; int n, f; vi g[N]; void add (int &x, int y) { x += y; if (x >= mod) x -= mod; } void dfs (int v) { was[v] = 1; for (auto to : g[v]) if (!was[to]) dfs(to); } main () { cin >> n; for (int i = 1; i <= n; ++i) { cin >> f; g[i].pb(f); g[f].pb(i); } int ans = 1; for (int i = 1; i <= n; ++i) if (!was[i]) { add(ans, ans); dfs(i); } add(ans, -1); cout << ans << endl; }
/* Author: EnderDeer Online Judge: Luogu */ #include<bits/stdc++.h> #define int long long #define mem(x) memset(x,0,sizeof(x)) #define mod 998244353 using namespace std; int read(){ int s = 0,w = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')w = -1;ch = getchar();} while(ch >= '0' && ch <= '9')s = s * 10 + ch - '0',ch = getchar(); return s * w; } int n; int pre[1000010]; int siz[1000010]; int ans = 1; void init(){ for(int i = 1;i <= n;i ++)pre[i] = i; } int find(int x){ if(pre[x] == x)return x; else return pre[x] = find(pre[x]); } signed main(){ cin>>n; init(); for(int i = 1;i <= n;i ++){ int x; x = read(); pre[find(i)] = find(x); } for(int i = 1;i <= n;i ++)siz[find(i)] ++; for(int i = 1;i <= n;i ++){ if(siz[i]){ ans = (ans << 1) % mod; } } cout<<(ans + mod - 1) % mod; return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //#include <atcoder/all> #define F first #define S second #define int long long #define ll long long //#define int unsigned long long #define pb push_back //#define double long double using namespace std; using namespace __gnu_pbds; //using namespace atcoder; typedef tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 55; const int K = 20; const int mod = 1e9 + 7; int d[N][N]; main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n, m; cin >> n >> m; vector <pair<int,int>> a(m); vector <int> f(m); vector <int> w(n), p(n); for (int i = 0; i < n; i++) cin >> w[i]; for (int i = 0; i < n; i++) p[i] = i; for (int i = 0; i < m; i++){ cin >> a[i].S >> a[i].F; } sort(a.begin(), a.end()); for (int i = 0; i < m; i++){ if (i != 0) f[i] = max(f[i - 1], a[i].S); else f[i] = a[i].S; } for (int i = 0; i < n; i++){ if (w[i] > a[0].F){ cout << -1; return 0; } } int res = 1e9; for (int i = 0; i < 40320; i++){ next_permutation(p.begin(), p.end()); int cur = 0; int cur_w = 0; for (int j = 0; j < n; j++){ for (int l = j + 1; l < n; l++){ int cur_w = 0; for (int k = j; k <= l; k++) cur_w += w[p[k]]; if (cur_w <= a[0].F) d[j][l] = 0; else{ int z = (int)(lower_bound(a.begin(), a.end(), make_pair(cur_w, 0LL)) - a.begin()) - 1; d[j][l] = f[z]; } } } vector <int> pos(n); for (int j = 1; j < n; j++){ pos[j] = pos[j - 1]; for (int l = 0; l < j; l++){ int need = d[l][j] - pos[j] + pos[l]; if (need > 0) pos[j] += need; } } res = min(res, pos[n - 1]); } cout << res; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; template<class S, class T> inline S max_L(S a,T b){ return a>=b?a:b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template<class S, class T> inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } int N; int X[20]; int Y[20]; int Z[20]; long long dp[131072][17]; long long dist[17][17]; int main(){ int i, mask; rd(N); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(X[Lj4PdHRW]); rd(Y[Lj4PdHRW]); rd(Z[Lj4PdHRW]); } } for(i=(0);i<(1<<N);i++){ int j; for(j=(0);j<(N);j++){ dp[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ int j; for(j=(0);j<(N);j++){ dist[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]) +max_L(Z[j]-Z[i], 0); } } dp[0][0] = 0; for(mask=(0);mask<(1<<N);mask++){ for(i=(0);i<(N);i++){ if(dp[mask][i] < 4611686016279904256LL){ int j; for(j=(0);j<(N);j++){ if(!((mask) &(1<<(j)))){ chmin(dp[mask|(1<<j)][j], dp[mask][i] + dist[i][j]); } } } } } wt_L(dp[(1<<N)-1][0]); wt_L('\n'); return 0; } // cLay varsion 20201018-1 // --- original code --- // int N, X[20], Y[20], Z[20]; // ll dp[131072][17], dist[17][17]; // { // rd(N,(X,Y,Z)(N)); // rep(i,1<<N) rep(j,N) dp[i][j] = ll_inf; // rep(i,N) rep(j,N) dist[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]) + max(Z[j]-Z[i], 0); // dp[0][0] = 0; // rep(mask,1<<N) rep(i,N) if(dp[mask][i] < ll_inf) rep(j,N) if(!BIT_ith(mask,j)){ // dp[mask|(1<<j)][j] <?= dp[mask][i] + dist[i][j]; // } // wt(dp[(1<<N)-1][0]); // }
// Parasparopagraho Jīvānām #pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace __gnu_pbds ; using namespace __gnu_cxx ; using namespace std ; typedef long long ll ; typedef long double ldb ; typedef pair<int, int> pii ; typedef pair<int, ll> pil ; typedef pair<ll,ll> pll ; typedef vector<int> vi ; typedef vector<ll> vll ; typedef vector<pii> vpi ; typedef vector<pll> vpl ; typedef vector<bool> vb ; typedef vector<pair<ll,int> > vpli ; typedef vector<int,pil> edges ; typedef vector<vi> matrix ; typedef priority_queue<int> pqu ; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ; #define rep(i,a,b) for (int i = (a); i <= (b); i++) #define per(i,b,a) for (int i = (b); i >= (a); i--) #define mp make_pair #define eb emplace_back #define pb push_back #define pob pop_back #define fi first #define se second #define ins insert #define bk back #define con continue #define lbd lower_bound #define ubd upper_bound #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define pr cout << // I don't like this but my laptop's 'O' key is not functioning well /* Careful when using long long __builtin_clz(int x) {} //: the number of zeros at the beginning of the number __builtin_ctz(int x) {} //: the number of zeros at the end of the number __builtin_popcount(int x) {} //: the number of ones in the number __builtin_parity(int x) {} //: the parity (even or odd) of the number of ones */ const int mod1 = 1000000007 ; const int mod2 = 998244353 ; const ll infl = 4e18 ; const int infi = 2e9 ; const int maxn = 1e6 + 5 ; const int block = 500 ; const int logn = 60 ; const int alpha = 27 ; const ldb pi = acos(-1) ; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ; int rng_lr(int a, int b) { int ret ; ret = uniform_int_distribution<int>(a, b)(rng) ; return ret ; // For shuffling use shuffle(all, rng) ; } ll modpow(ll a, ll b, int MOD) { ll res = 1 ; while(b) { if(b&1) { res = res*a ; res %= MOD ; } a *= a ; a %= MOD ; b >>= 1 ; } return res%MOD ; } void upmin(int &a, int b) { if(a < b) { a = b ; } } void relax(int &a, int b) { if(a > b) { a = b ; } } ll add(ll a, ll b, int MOD) { a += b ; if(a >= MOD) { a -= MOD ; } return a ; } ll sub(ll a, ll b, int MOD) { a -= b ; if(a < 0) { a += MOD ; } return a ; } ll mul(ll a, ll b, int MOD) { b %= MOD ; a *= b ; a %= MOD ; return a ; } ll inverse(ll a, ll MOD) { a = modpow(a, MOD - 2, MOD) ; return a ; } ll lcm(ll a, ll b) { ll ret ; ll g = __gcd(a, b) ; ret = a/g ; ret = ret*b ; return ret ; } int icast(char ch) { return int(ch-'a') ; } void hyn(int a, int b) { if(a == b) cout << "YES\n" ; else cout << "NO\n" ; } void pyd(int a, int b) { if(a == b) cout << "First\n" ; else cout << "Second\n" ; } // Check constraints + overflows + types in typedef int main() { ios::sync_with_stdio(false) ; cin.tie(NULL) ; int n ; string s ; cin >> n >> s ; if(s[0] != s[n-1]) { pr "1\n" ; return 0 ; } rep(i,1,n-2) { if(s[i] != s[0] && s[i + 1] != s[0]) { pr "2\n" ; return 0 ; } } pr "-1\n" ; return 0 ; }
#include <iostream> #include <string> using namespace std; int main(){ int i,n; cin >> n; string s; cin >> s; if(s[0]!=s.back()){ cout << 1 << endl; return 0; } char c = s[0]; for(int i=0;i<s.size();i++){ if(s[i]!=c && s[i + 1]!=c){ cout << 2 << endl; return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define DUMP(x) cout << #x << " = " << (x) << endl; #define FOR(i, m, n) for (ll i = m; i < n; i++) #define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) #define FOREACH(x, a) for (auto&(x) : (a)) #define ALL(v) (v).begin(), (v).end() #define SZ(x) ll(x.size()) int main() { ll n; cin >> n; vector<vector<ll>> c(n, vector<ll>(n)); REP(i, n) REP(j, n) { cin >> c[i][j]; } vector<ll> a(n, 0), b(n); REP(j, n) { b[j] = c[0][j]; } const ll INF = 1e18; FOR(i, 1, n) { // a[i] ll d = INF; REP(j, n) { ll e = c[i][j] - b[j]; if (d == INF) { d = e; } else if (d != e) { cout << "No\n"; return 0; } } a[i] = d; } // 非負制約 ll mini = INF; REP(i, n) { mini = min(mini, a[i]); } if (mini < 0) { REP(i, n) { a[i] += -mini; } } REP(j, n) { if (mini < 0) { b[j] += mini; } if (b[j] < 0) { cout << "No\n"; return 0; } } cout << "Yes\n"; REP(i, n) { cout << a[i] << " \n"[i == n - 1]; } REP(i, n) { cout << b[i] << " \n"[i == n - 1]; } }
#include<bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <chrono> // using namespace std::chrono; // using namespace __gnu_pbds; #define ff first #define ss second #define int long long #define double long double #define pb push_back #define mp make_pair #define pi pair<int,int> #define vtiii vector<tuple<int,int,int>> #define vi vector<int> #define vpii vector<pair<int,int> #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 inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define all(x) (x).begin(), (x).end() #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #ifndef ONLINE_JUDGE #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #endif void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(double t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} const int N = 2e5 + 10; const int mod = 1e9 + 7 ; void solve(){ int n;cin>>n; if(n%100 != 0){ cout<<n/100 + 1; }else cout<<n/100; } int32_t main(){ // auto start = high_resolution_clock::now() FIO; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("Error.txt", "w", stderr); #endif // precalc(); // int x; cin>>x; while(x--) solve(); // auto stop = high_resolution_clock::now(); // auto duration = duration_cast<microseconds>(stop - start); // cout << duration.count() << endl; }
#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),i##end=(b);i<i##end;i++) #define REP(i,a,b) for(int i=(a),i##end=(b);i<=i##end;i++) #define RFOR(i,a,b) for(int i=(a),i##end=(b);i>i##end;i--) #define RREP(i,a,b) for(int i=(a),i##end=(b);i>=i##end;i--) typedef long long LL; const int maxn=1000+5; std::queue<std::pair<int,int> > Q; int distance[maxn][maxn],n,m; std::vector<int> G[maxn][26]; int main() { memset(distance,-1,sizeof(distance)); while(!Q.empty()) Q.pop(); scanf("%d%d",&n,&m); REP(u,1,n) FOR(i,0,26) G[u][i].clear(); REP(u,1,n) Q.push(std::make_pair(u,u)),distance[u][u]=0; while(m--) { int x,y,c; scanf("%d%d",&x,&y); while(!isalpha(c=getchar())); c-=97; G[x][c].push_back(y); G[y][c].push_back(x); if(distance[x][y]<0) distance[x][y]=distance[y][x]=1,Q.push(std::make_pair(x,y)); } while(!Q.empty()) { std::pair<int,int> p=Q.front(); Q.pop(); int u=p.first,v=p.second; if((u==1&&v==n)||(u==n&&v==1)) { printf("%d\n",distance[1][n]); return 0; } FOR(c,0,26) FOR(i,0,G[u][c].size()) FOR(j,0,G[v][c].size()) { int us=G[u][c][i],vs=G[v][c][j]; if(distance[us][vs]>=0) continue; distance[us][vs]=distance[vs][us]=distance[u][v]+2; Q.push(std::make_pair(us,vs)); } } printf("-1\n"); return 0; }
#include<bits/stdc++.h> //#include <atcoder/all> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i,n)for(int i=0;(i)<(int)(n);i++) #define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++) #define ALL(a) (a).begin(),(a).end() #define pb push_back #define fi first #define se second #define sz(x) ((int)x.size()) using namespace std; //using namespace atcoder; using ld = long double; using ll = long long; using P = pair<ll, ll>; template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; } template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; } const ll MOD = 1e9 + 7; const ll INF = 1e16; struct Edge{ int to; char col; }; int main(){ int n, m; cin >> n >> m; vector<vector<Edge>> G(n); vector<int> a(m), b(m); rep(i, m){ char c; cin >> a[i] >> b[i] >> c; a[i]--;b[i]--; G[a[i]].pb(Edge{b[i], c}); G[b[i]].pb(Edge{a[i], c}); } vector<vector<int>> nG(n * n); //グラフを再構築する auto push = [&](int ua, int ub, int va, int vb){ nG[ua * n + ub].pb(va * n + vb); }; rep(a, n){ rep(b, n){ for(auto ea : G[a]){ for(auto eb : G[b]){ if(ea.col == eb.col)push(a, b, ea.to, eb.to); } } } } vector<ll> d(n * n, INF); priority_queue<P, vector<P>, greater<P>> pq; d[n - 1] = 0; pq.push(P(0, n - 1)); while(sz(pq)){ auto [cost, v] = pq.top(); pq.pop(); if(d[v] < cost)continue; for(auto nv : nG[v]){ if(d[nv] > d[v] + 1){ d[nv] = d[v] + 1; // cout << nv << " " << d[nv] << endl; pq.push(P(d[nv], nv)); } } } ll res = INF; // rep(i, n)cout << d[i * n + i] << endl; rep(i, n)chmin(res, 2 * d[i * n + i]); rep(i, m){ chmin(res, 2 * d[a[i] * n + b[i]] + 1); chmin(res, 2 * d[b[i] * n + a[i]] + 1); } if(res == INF)res = -1; cout << res << endl; }
#include <bits/stdc++.h> using namespace std; constexpr int N = 1000000; bool is_radical[N + 1]; bool prime[N + 1]; int pf[N + 1]; int main() { long long cur, ans, l, r, n, d; scanf("%lld%lld", &l, &r); memset(is_radical, true, sizeof(is_radical)); memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; ans = 0; for (d = 2; d <= r; d++) { cur = 0; for (n = d; n <= r; n += d) { if (prime[d]) { pf[n]++; if (n > d) { prime[n] = false; } } if (l <= n and n <= r) { cur++; if (n > d and l <= d and d <= r) { ans -= 2; } } } for (n = d * d; n <= r; n += d * d) { is_radical[n] = false; } if (is_radical[d]) { if (pf[d] % 2) { ans += cur * (cur - 1); } else { ans -= cur * (cur - 1); } } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> ii; ll x; map<ll, ll> sv; ll f(ll y){ if (sv.find(y) != sv.end()) return sv.find(y)->second; sv[y] = 0LL; ll &ans = sv[y]; if (y>x){ if (y&1){ ll ri = (y+1LL); ll le = (y-1LL); ll directtarget = y-x; return ans = min(f(ri)+1LL, min(f(le)+1LL, f(x)+directtarget)); }else{ ll ab = y-x; ll divi = y/2; ll opsdivi = 1LL + abs(divi - x); if (opsdivi <= ab){ return ans = 1LL + f(y/2LL); }else{ return ans = ab + f(x); } } }else if (y<x){ return ans = x-y + f(x); }else return ans = 0LL; } void solve(){ ll y; cin >> x >> y; cout << f(y) << '\n'; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while(t--) solve(); return 0; } // NK