code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#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 = 5000 + 5; constexpr int64_t MOD = 998244353; int pw[NAX][NAX]; int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } int mul(int a, int b) { return (a * 1LL * b) % MOD; } int64_t solveCase(int64_t N, int64_t M) { for (size_t i = 0; i < NAX; i++) { pw[i][0] = 1; for (size_t j = 1; j < NAX; j++) pw[i][j] = (i * 1LL * pw[i][j - 1]) % MOD; } int64_t res = 0; for (size_t i = 0; i < N; i++) for (size_t x = 1; x <= M; x++) res = add(res, mul(pw[M - x][i], pw[M][N - 1 - i])); for (size_t d = 1; d < N; d++) for (size_t x = 1; x <= M; x++) res = add(res, mul(mul(N - d, x - 1), mul(pw[M - x][d - 1], pw[M][N - 1 - d]))); return res; } // 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, M; cin >> N >> M; auto ans = solveCase(N, M); cout << ans << endl; return 0; }
#include "bits/stdc++.h" #include<sstream> using namespace std; typedef long long ll; #define _USE_MATH_DEFINES #include <math.h> #define NIL = -1; #define all(x) x.begin(),x.end() const ll INF = 1e9; const long long inf = 1e18; const ll INFL = 1e18; const ll MOD = 1e9 + 7; int digit(ll x) { int digits = 0; while(x > 0){ x /= 10; digits++; } return digits; } ll gcd(long long a,long long b) { if (a < b) swap(a,b); if (b == 0) return a; return gcd(b,a%b); } bool is_prime(long long N){ if (N == 1) return false; for (long long i = 2;i * i <= N;i++){ if (N % i == 0) return false; } return true; } ll lcm(ll a,ll b){ return ((a * b == 0)) ? 0 : (a / gcd(a,b) * b); } double DegreeToRadian(double degree){ return degree * M_PI / 180.0; } long long modpow(long long a, long long b, long long m){ long long ans = 1; while(b > 0){ if (b % 2 == 1){ ans *= a; ans %= m; } a *= a; a %= m; b /= 2; } return ans; } long long comb(long long x, long long y){ int z; if (y == 0) return 1; else { z = modpow(x, y/2, MOD)*modpow(x, y/2, MOD)%MOD; if (y % 2 == 1) z = z*x%MOD; return z; } } vector<pair<long long, long long>> prime_fact(long long x){ vector<pair<long long, long long>> res; for(int i = 2;i*i <= x;i++){ if (x % i == 0){ long long cnt = 0; while(x % i == 0){ cnt++; x /= i; } res.push_back({i, cnt}); } } if (x != 1){ res.push_back({x, 1}); } return res; } int64_t mod_inv(int64_t a, int64_t m){ int64_t b = m, u = 1, v = 0; while(b){ int64_t t = a/b; a -= t*b; swap(a, b); u -= t*v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } // {g, x, y}: ax + by = g tuple<long long, long long, long long> extgcd(long long a, long long b){ if (b == 0) return {a, 1, 0}; long long g, x, y; tie(g, x, y) = extgcd(b, a % b); return {g, y, x - a/b*y}; } int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; ////////////////////////////////////////////////////////////// long long n ,k; struct matrix{ long long value[55][55]; }; matrix multi(matrix &a, matrix &b){ matrix c; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ c.value[i][j] = 0; for(int k = 1;k <= n;k++){ c.value[i][j] += a.value[i][k]*b.value[k][j]; c.value[i][j] %= MOD; } } } return c; } int main(){ int h , w; cin >> h >> w; vector<string> s(h); for(int i = 0;i < h;i++){ cin >> s[i]; } int DX[4] = {0, 1, 0, 1}; int DY[4] = {0, 0, 1, 1}; int cnt = 0; int ans = 0; for(int i = 1;i < h;i++){ for(int j = 0;j < w - 1;j++){ cnt = 0; for(int k = 0;k < 4;k++){ if (s[i - DY[k]][j + DX[k]] == '#') cnt++; } if (cnt == 1 || cnt == 3) ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { printf("%d %d\n", 2*i%n+1, (2*i+1)%n+1); } }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, k, n) for (int i = k; i < (int)(n); i++) #define repd(i, n) for (int i = n-1; i >= 0; i--) #define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--) #define all(x) (x).begin(),(x).end() #define chmax(x,y) x=max(x,y) #define chmin(x,y) x=min(x,y) #define F first //pairใฎไธ€ใค็›ฎใฎ่ฆ็ด  #define S second //pairใฎไบŒใค็›ฎใฎ่ฆ็ด  #define PB push_back //ๆŒฟๅ…ฅ #define MP make_pair //pairใฎใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟ //V,Pใฏๅคงๆ–‡ๅญ—i,l,bใฏๅฐๆ–‡ๅญ— using ll = long long; using Vi = vector<int>; using VVi = vector<Vi>; using Vl = vector<ll>; using VVl = vector<Vl>; using Vb = vector<bool>; using VVb = vector<Vb>; using P = pair<int,int>; using Pl = pair<ll, ll>; using Vs = vector<string>; const ll mod = 1000000007; const ll inf = 1000000000000000000;//10ใฎ18ไน— #define yn {puts("Yes");}else{puts("No");} #define dame { puts("-1"); return 0;} int main() { ll k; cin >> k; ll ans=0; rrep(i,1,k+1)rrep(j,1,k/i+1){ ans+=k/(i*j); } cout << ans << endl; }
/** * author: avoas * created: 12.05.2021 18:03:29 **/ #include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr double EPS = 1e-12; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long> a(n); long long sum = 0; rep(i,n) { cin >> a[i]; sum += a[i]; } sort(all(a)); reverse(all(a)); vector<long long> sn(n); sn[0] = sum - a[0]; for(int i = 1; i < n; ++i) { sn[i] = sn[i-1] - a[i]; } long long ans = 0; rep(i,n) { ans += (n-1-i)*a[i] - sn[i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define pb push_back #define endl ("\n") #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define vi vector<lli> #define ff first #define ss second #define rep(i, a, b) for (int i = a; i < b; i++) #define repr(i, a, b) for (int i = a; i > b; i--) #define mod 1000000007 #define all(s) s.begin(), s.end() vector<lli>ar[200005]; lli sz = 0, vis[200005] = {0}; void solve(lli n) { vis[n] = 1; sz++; for (auto c : ar[n]) { if (!vis[c]) solve(c); } } int main() { fast lli tt, n; tt = 1; // cin>>tt; while (tt--) { cin>>n; lli a[n], ans = 0; rep (i, 0, n) cin>>a[i]; rep (i, 0, n/2) { if (a[i] != a[n - i - 1]) ar[a[i]].pb(a[n - i - 1]), ar[a[n - i - 1]].pb(a[i]); } // map<lli, lli>mp; // for (int i = 1; i < 100005; i++) { // if (ar[i].size() > 0) { // sort(all(ar[i])); // lli p = ar[i].size(); // if (ar[i][0] == ar[i][p - 1] && mp.find(ar[i][p - 1]) == mp.end()) ans++, mp[i] = ar[i][p - 1]; // else { // ar[i][p - 1] = mp[ar[i][p - 1]]; // if (i == ar[i][p - 1]) continue; // } // } // } // cout<<ans; rep (i, 1, 200005) { if (ar[i].size() > 0 && !vis[i]) { sz = 0; solve(i); ans += (sz - 1); } } cout<<ans; } return 0; }
#include<iostream> using namespace std; int main() { int a,b; cin >> a >> b; int t = (2 * a) + 100 ; if(t-b < 0){ cout << 0 << endl; } else{ cout << t-b << endl; } }
#include <iostream> using namespace std; int main(void) { int x, y; cin >> x >> y; if (x > y) { if ((y + 3) > x) { cout << "Yes" << endl; } else { cout << "No" << endl; } } if (y > x) { if ((x + 3) > y) { cout << "Yes" << endl; } else { cout << "No" << endl; } } }
#include<fstream> #include<iostream> #include<sstream> #include<vector> #include<stack> #include<string> #include<algorithm> #include<cmath> #include<unordered_map> #include<map> #include<queue> #include<tuple> #include<iomanip> #include<bitset> #include<stdio.h> #include<set> #include<array> using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) using graph = vector<vector<int>>; const long long MOD = 1000000007; const long long INF = 1e18; using ll = long long; using P = pair<int,int>; using matrix = vector<vector<ll>>; void chmin(int &x,int y){ x = min(x,y); } int digit(ll x){ int count = 1; while(x/10>0){ count ++; x /= 10; } return count; } bool prime(ll x){ if(x==1) return false; for(ll i=2;i*i<=x;i++){ if(x%i==0) return false; } return true; } void decom(ll x,map<ll,int> &hatano){ for(ll i=2;i*i<=x;i++){ if(x%i==0&&prime(i)){ while(x%i==0){ x /= i; hatano[i] ++; } } } if(x!=1) hatano[x] ++; } ll speedpow(ll x,ll y){ ll res = 1; ll test = x; for(int i=0;i<31;i++){ if(y&(1<<i)) res *= test; res = res%MOD; test = (test*test)%MOD; } return res; } ll combination(ll x,ll y){ ll test = 1; ll test1 = 1; for(ll i=0;i<y;i++){ test = (test*(x-i))%MOD; test1 = (test1*(i+1LL))%MOD; } return (test * speedpow(test1,MOD-2))%MOD ; } int euclid(int x,int y){ if(y==0) return x; return euclid(y,x%y); } //่กŒๅˆ—็ดฏไน— matrix mul(matrix &a, matrix &b) { int p = a.size(); matrix res(p, vector<long long>(p, 0)); for(int i = 0; i < p; i++) for(int j = 0; j < p; j++) for(int k = 0; k < p; k++) (res[i][j] += a[i][k] * b[k][j]) %= MOD; return res; } matrix pow(matrix m, long long k) { int p = m.size(); matrix res(p, vector<long long>(p, 0)); for(int i = 0; i < p; i++) res[i][i] = 1; // ๅ˜ไฝ่กŒๅˆ—ใซใ™ใ‚‹ while(k > 0) { if(k & 1) res = mul(res, m); m = mul(m, m); k >>= 1; } return res; } int main(){ int n,m,q;cin >> n >> m >> q; int v[n],w[n]; vector<P> hatano; int x[m]; vector<P> konishi; int l[q],r[q]; rep(i,n){ cin >> w[i] >> v[i]; hatano.push_back(make_pair(v[i],w[i])); } sort(hatano.rbegin(),hatano.rend()); rep(i,m){ cin >> x[i]; konishi.push_back(make_pair(x[i],i)); } sort(konishi.begin(),konishi.end()); vector<int> ans; rep(i,q){ cin >> l[i] >> r[i]; l[i] --;r[i] --; } rep(i,q){ int test=0; vector<bool> check(m,true); for(int s=l[i];s<=r[i];s++) check[s] = false; rep(j,n){ rep(k,m){ if(konishi[k].first>=hatano[j].second&&check[konishi[k].second]){ test += hatano[j].first; check[konishi[k].second] = false; // cout << hatano[j].first << endl; break; } } } ans.push_back(test); // cout << endl; } for(auto e:ans) cout << e << endl; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <set> #include <queue> #include <iomanip> using namespace std; typedef long long ll; int main() { ll n, m, q; cin >> n >> m >> q; vector<pair<ll, ll>> nimotu(n, pair<ll, ll>()); for (ll i = 0; i < n; i++) { cin >> nimotu[i].second >> nimotu[i].first; } sort(nimotu.begin(), nimotu.end(), greater<pair<ll, ll>>{}); vector<ll> x(m); for (ll i = 0; i < m; i++) cin >> x[i]; vector<ll> answer; for (ll qq = 0; qq < q; qq++) { ll ans = 0; ll l, r; cin >> l >> r; l--; r--; vector<ll> boxes; for (ll i = 0; i < m; i++) { if ((l <= i) && (i <= r)) { } else { boxes.push_back(x[i]); } } sort(boxes.begin(), boxes.end()); for (ll i = 0; i < nimotu.size(); i++) { for (ll j = 0; j < boxes.size(); j++) { if (nimotu[i].second <= boxes[j]) { boxes.erase(boxes.begin() + j); ans += nimotu[i].first; break; } } } answer.push_back(ans); } for (ll i = 0; i < answer.size(); i++) { cout << answer[i] << endl; } }
///Bismillahir Rahmanir Rahim #include<bits/stdc++.h> using namespace std; #define ll long long int ll gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } #define in(x) scanf("%lld",&x) #define in2(x,y) scanf("%lld %lld",&x,&y) #define in3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z) #define out(x) printf("%lld\n",x) #define cy printf("YES\n") #define cn printf("NO\n") #define inf 1e18 #define neg -1e18 #define mx 2111111 #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fi first #define si second #define ce cout<<endl #define pb push_back #define vc(x) x.begin(),x.end() #define pb push_back #define pi pair<ll,ll> #define debug printf("ok\n"); #define pg pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > #define mod 1000000007 int main(){ ll i,j,ans=0,sum=0,s=0,n; in(n); set<ll>st; for(i=1;i<=sqrt(n);i++){ if(n%i==0){ st.insert(i); st.insert(n/i); } } for(auto x:st){ cout<<x<<endl; } }
// โ•”โ•โ•โ•โ•—โ–‘โ–‘โ•”โ•โ•โ•โ•—โ–‘โ–‘โ•”โ•—โ–‘โ•”โ•—โ•”โ•โ•โ•โ•—โ•”โ•โ•โ•โ•—โ•”โ•—โ–‘โ•”โ•—โ–‘ // โ•‘โ•”โ•โ•—โ•‘โ–‘โ–‘โ•‘โ•”โ•โ•โ•โ–‘โ–‘โ•‘โ•‘โ–‘โ•‘โ•‘โ•‘โ•”โ•โ•—โ•‘โ•‘โ•”โ•โ•—โ•‘โ•‘โ•‘โ–‘โ•‘โ•‘โ–‘ // โ•‘โ•‘โ–‘โ•‘โ•‘โ–‘โ–‘โ•‘โ•šโ•โ•โ•—โ–‘โ–‘โ•‘โ•šโ•โ•โ•‘โ•‘โ•šโ•โ•โ•‘โ•šโ•โ•”โ•โ•‘โ•‘โ•šโ•โ•โ•‘โ–‘ // โ•‘โ•šโ•โ•โ•‘โ–‘โ–‘โ•‘โ•”โ•โ•โ•โ–‘โ–‘โ•‘โ•”โ•โ•—โ•‘โ•‘โ•”โ•โ•—โ•‘โ•”โ•โ•โ•”โ•โ•šโ•โ•โ•—โ•‘โ–‘ // โ•‘โ•”โ•โ•—โ•‘โ•”โ•—โ•‘โ•šโ•โ•โ•—โ•”โ•—โ•‘โ•‘โ–‘โ•‘โ•‘โ•‘โ•šโ•โ•โ•‘โ•‘โ•‘โ•šโ•โ•—โ–‘โ–‘โ–‘โ•‘โ•‘โ–‘ // โ•šโ•โ–‘โ•šโ•โ•šโ•โ•šโ•โ•โ•โ•โ•šโ•โ•šโ•โ–‘โ•šโ•โ•šโ•โ•โ•โ•โ•šโ•โ•โ•โ•โ–‘โ–‘โ–‘โ•šโ•โ–‘ #include<bits/stdc++.h> using namespace std; #define int long long int #define ll long long int #define fi(n) for(int i=0;i<n;i++) #define fj(n) for(int j=0;j<n;j++) #define W(x) int x;cin>>x;while(x--) #define fr(n) for(int i=n-1;i>=0;i--) #define mp make_pair #define fo(i,a,n) for(int i=a;i<n;i++) #define ps(x,y) fixed<<setprecision(y)<<x #define pb push_back #define vi vector<int> #define deb(x) cout<<#x<<"="<<x<<endl; #define all(a) (a).begin(),(a).end() #define pii pair<int,int> #define sortall(x) sort(all(x)) #define mk(arr,n,type) type *arr=new type[n]; #define vpi vector<pair<int,int> > #define setbits(x) __builtin_popcountll(x) #define zerbits(x) __builtinctzll(x) #define mod 1000000007 #define inf 1e18 #define sz(x) (int)x.size() #define vs vector<string> #define itr(a) for(auto it=a.begin();it!=a.end();it++) #define f first #define s second #define INT_SIZE 32 int mpow(int exp, int base); //I don't know what the funciton aeh() does but it speeds up my mess void aeh() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int32_t main() { //aeh(); int n;cin>>n; int ans=n/100; if(n%100) ans++; cout<<ans<<"\n"; } 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; }
#include <bits/stdc++.h> //#include <atcoder/all> #include <unordered_map> const double PI = acos(-1); using namespace std; typedef long long ll; typedef unsigned long long ull; using P = pair<ll, ll>; using V = vector<ll>; using VV = vector<V>; using SV = vector<string>; #define rep(i, n) for(ll i=0;i<(ll)(n);i++) #define rep1(i, n) for(ll i=1;i<=(ll)(n);i++) #define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++) #define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define MOD 1000000007 #define INF 1000000000 #define LLINF 1e18 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; } // ็ด ๆ•ฐๅˆคๅฎš 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; } void solve_abc_c() { ll n, m; ll k; ll h, w; //ll x, y; //ll a, b, c; ll t; string s; string s_ans; ll l_ans = 0; double d_ans = 0; cin >> n; ll prev = 1000; ll next = 1000000; ll x = 1; if (n > 999) { while (1) { if (n < next) { l_ans += (n - prev + 1) * x; break; } l_ans += (next - prev) * x; prev = next; next *= 1000; x++; } } cout << l_ans << "\n"; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cout << fixed; cout << setprecision(15); solve_abc_c(); return 0; }
#include<bits/stdc++.h> using namespace std; #define sp ' ' #define endl '\n' #define F first #define S second #define mp make_pair #define pb push_back #define mt make_tuple #define PI (2 * acos(0)) #define all(x) x.begin(), x.end() #define spc(x) cout<<fixed<<setprecision(x) void solve() { long long t,m,n,a,b,c,d,g,i,j,k,x,y,z,cnt=1,cnt1=0; bool flag= false; string s,s1; cin >> n ; set<long long>st; for(i=0;i<n;i++) { cin >> x ; st.insert(x); } for(long long ss : st) { ss++; cnt *= ss-cnt1; cnt1 = ss-1 ; cnt = cnt%1000000007; } cout << cnt << endl ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // int32_t tc; // cin>>tc; // while(tc--) solve(); return 0; }
#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; struct Edge{ int from,to,add,mul; Edge(const int& u,const int& v,const int& a,const int& m):from(u),to(v),add(a),mul(m) {} LL solve(const LL& t) { int x=(int)sqrt(mul+0.5); int a=x,b=x*(x+1)>mul?x:x+1; //printf("a=%d b=%d\n",a,b); if(t<1LL*b) return a+b+add-1; else return t+add+mul/(t+1LL); } }; struct Heapnode{ int u; LL d; Heapnode(const int& u,const LL& d):u(u),d(d) {} bool operator < (const Heapnode& rhs) const { return d>rhs.d; } }; struct Dijkstra{ static const int maxn=100000+5; std::vector<Edge> edges; std::vector<int> G[maxn]; LL dist[maxn]; Dijkstra() { FOR(i,0,maxn) G[i].clear(); edges.clear(); } void AddEdge(const int& from,const int& to,const int& add,const int& mul,const int& t=-1) { edges.push_back(Edge(from,to,add,mul)); G[from].push_back(t>=0?t:edges.size()); } LL dijkstra(const int& s,const int& t) { std::priority_queue<Heapnode> Q; while(!Q.empty()) Q.pop(); Q.push(Heapnode(s,0)); memset(dist,-1,sizeof(dist)); dist[s]=0LL; while(!Q.empty()) { Heapnode h=Q.top(); Q.pop(); int u=h.u; assert(h.d>=dist[u]&&dist[u]>=0LL); if(dist[u]!=h.d) continue; if(u==t) return dist[t]; FOR(i,0,G[u].size()) { Edge e=edges[G[u][i]]; int v=e.to; LL ans=e.solve(dist[u]); if(ans<dist[v]||dist[v]<0LL) Q.push(Heapnode(v,dist[v]=ans)); } } assert(dist[t]==-1LL); return -1LL; } }D; int n,m; int main() { scanf("%d%d",&n,&m); //printf("%d %d\n",n,m); FOR(i,0,m) { int from,to,add,mul; scanf("%d%d%d%d",&from,&to,&add,&mul); D.AddEdge(from,to,add,mul,i<<1); D.AddEdge(to,from,add,mul,(i<<1)|1); //printf("OK!%d\n",i); } printf("%lld\n",D.dijkstra(1,n)); return 0; }
#include<iostream> #include<iomanip> #include<vector> #include<string> #include<utility> #include<algorithm> #include<functional> #include<map> #include<queue> #include<stack> #include<cmath> #include<set> using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using piii = pair<int,pii>; #define LL_INF 1e18+5 #define INT_INF 1e9+5 #define MOD 1e9+7 struct edge{ ll to,cost,dis; edge(){}; edge(ll t,ll c,ll d){ to = t; cost = c; dis = d; }; }; ll time_calc(ll time,ll dis){ //time+1+dis/(time+1) >= 2*root(dis) dis = (time+1)^2 ll mintime = sqrt(dis); if(mintime == 0) mintime = 1; if(mintime+dis/mintime > mintime+1+dis/(mintime+1)){ mintime++; } if(time < mintime){ return mintime-1+dis/mintime; } else return time+dis/(time+1); } int main(){ ll N,M; cin >> N >> M; vector<vector<edge> > E(N); for(ll i=0;i<M;i++){ ll A,B,C,D; cin >> A >> B >> C >> D; A--;B--; E[A].push_back(edge(B,C,D)); E[B].push_back(edge(A,C,D)); } priority_queue<pll,vector<pll>,greater<pll>> q; q.push(pll(0,0)); vector<ll> min_time(N,LL_INF); min_time[0] = 0; vector<bool> visited(N,false); while (!q.empty()) { pll pos = q.top();q.pop(); ll cur = pos.second; ll time = pos.first; if(min_time[cur] < time || visited[cur]) continue; visited[cur] = true; for(auto&next:E[cur]){ ll ne = next.cost+time_calc(time,next.dis); if(min_time[next.to] > ne && !visited[next.to]){ min_time[next.to] = ne; q.push(pll(ne,next.to)); } } } if(min_time[N-1] < LL_INF){ cout << min_time[N-1] << endl; } else cout << -1 << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; #define endline '\n' #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define pb push_back #define fr(i,a,b) for(int i=a;i<(int)b;i++) #define rfr(i,a,b) for(int i=a;i>=(int)b;i--) #define ff first #define ss second #define all(x) (x).begin(), (x).end() const ll mod=1e9+7; #define INF 0x3f3f3f3f const int N = 1e6+2; void solution(){ vector<pair<ll,pll>> v; int n;cin>>n; fr(i,0,n) { ll x,y,z; cin>>x>>y>>z; v.pb({x,{y,z}}); } int c=0; sort(all(v)); fr(i,0,n) { fr(j,i+1,n) { double a1[]={0,0,0.00001,0.00001}; double b1[]={0,-0.00001,0,-0.00001}; int f1=v[i].first;int f2=v[j].first; long double x1=0,y1=0,x2=0,y2=0; fr(k,0,4) { if(f1==k+1) { x1=v[i].second.first+a1[k],y1=v[i].second.second+b1[k]; } if(f2==k+1) { x2=v[j].second.first+a1[k],y2=v[j].second.second+b1[k]; } } if((x1<=x2 and x2<=y1) or (x2<=x1 and x1<=y2) or (x1<=y2 and y2<=y1) or (x2<=y1 and y1<=y2))c++; } } cout<<c<<endline; } int main() { // your code goes here int t=1; //cin>>t; while(t--){ solution(); } return 0; }
#include <iostream> #include <algorithm> using namespace std; const int N = 2e3 + 50; double l[N], r[N]; bool ck(int u, int v) { if (r[u] < l[v] || l[u] > r[v]) return false; else return true; } int main() { int n, ans = 0; cin >> n; for (int i = 0; i < n; i ++) { int t; cin >> t >> l[i] >> r[i]; if (t == 2 || t == 4) r[i] -= 0.5; if (t == 3 || t == 4) l[i] += 0.5; for (int j = 0; j < i; j ++) if (ck(i, j)) ans ++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define REP(i, m, n) for (ll i=(ll)(m); i < (ll)(n); ++i) #define ln cout<<'\n' void pr(){ln;} template<class A, class...B>void pr(const A &a, const B &...b){cout<<a<<' ';pr(b...);} void solve() { int n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); ll ans = 0; ll tmp = 0; for (int i = 0; i < n; ++i) { ans += tmp * a[i]; ans += a[i] * a[i]; ans %= 998244353; tmp *= 2; tmp += a[i]; tmp %= 998244353; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); solve(); return 0;}
#include <cstdio> #include <algorithm> #include <numeric> using namespace std; int N,K; using ll = long long int; int mat[55][55]; bool o[55][55];//graph bool comp_set[55][55]; bool visited[55]; const ll MOD=998244353; ll solve(){ ll ret=1ll; for(int i=1;i<=N;i++){ ll cnt=1; for(int k=1;k<i;k++) if (comp_set[i][k]) cnt++; // printf("i=%d cnt=%lld\n",i,cnt); ret=ret*cnt%MOD; } return ret; } void DFS(int cur,int root){ if (visited[cur]) return; visited[cur]=true; comp_set[cur][root]=comp_set[root][cur]=true; for(int v=1;v<=N;v++){ if (!o[cur][v]||visited[v]) continue; DFS(v,root); } } void set_comp(){ for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) comp_set[i][j]=false; for(int v=1;v<=N;v++) { DFS(v,v); for(int i=1;i<=N;i++) visited[i]=false; } } void check_compatable(){ for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) o[i][j]=false; for(int i=1;i<=N;i++) for(int j=1;j<i;j++){ o[i][j]=true; for(int k=1;k<=N;k++) o[i][j]=o[i][j]&&((mat[i][k]+mat[j][k])<=K); o[j][i]=o[i][j]; } } int main(){ scanf("%d%d",&N,&K); for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) scanf("%d",&mat[i][j]); ll ans1,ans2; check_compatable(); set_comp(); ans1=solve(); for(int i=1;i<=N;i++) for(int j=1;j<=i;j++) swap(mat[i][j],mat[j][i]); check_compatable(); set_comp(); ans2=solve(); printf("%lld\n",ans1*ans2%MOD); return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int h, w, x, y; cin >> h >> w >> x >> y; char s[h][w]; for(int i=0; i<h; i++) for(int j=0; j<w; j++) cin>>s[i][j]; x--; y--; int ans=0; for(int i=y+1; i<w; i++) { if(s[x][i]=='#') break; else { if(s[x][i] == '.') ans++; } } for(int i=y-1; i>=0; i--) { if(s[x][i]=='#') break; else { if(s[x][i] == '.') ans++; } } for(int i=x+1; i<h; i++) { if(s[i][y]=='#') break; else { if(s[i][y] == '.') ans++; } } for(int i=x-1; i>=0; i--) { if(s[i][y]=='#') break; else { if(s[i][y] == '.') ans++; } } cout << ans+1 << "\n"; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define inf 2000000000 #define infLL 2000000000000000000 #define MAX5 100005 #define MAX6 1000006 #define MAX7 10000007 #define sf(a) scanf("%d", &a) #define sfl(a) scanf("%lld", &a) #define sfs(a) scanf("%s", a) #define sline(a) scanf("%[^\n]%*c", a); #define pf(a) printf("%d\n", a) #define pfl(a) printf("%lld\n", a) #define pfs(a) printf("%s\n", a) #define Case(t) printf("Case %d: ", t) #define pii pair<int, int> #define pll pair<ll, ll> #define mod 1000000007 #define Mod 998244353 #define PI acos(-1.0) #define eps 1e-9 #define mem(a, b) memset(a, b, sizeof(a)) #define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); const int N = MAX5; int main() { //#ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); //#endif FASTIO; int n, m; cin>>n>>m; ll a[n][m]; ll mn = inf; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cin>>a[i][j]; mn = min(mn, a[i][j]); } } ll ans = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { ans += max(0LL, a[i][j]-mn); } } cout<<ans<<endl; return 0; }
/*เฅ เคจเคฎเฅ‹ เคญเค—เคตเคคเฅ‡ เคตเคพเคธเฅเคฆเฅ‡เคตเคพเคฏ เคจเคฎเคƒ*/ #include<bits/stdc++.h> using namespace std; #define int long long const int MOD = 1e9 + 7; const double pi = 3.14159265359; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int binpow(int a, int b, int m) { a %= m; int res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } int inverse(int x){ return binpow(x, MOD - 2, MOD); } void solve() { int n, m; cin >> n >> m; vector<int> edge[n]; vector<int> a(n); vector<int> vis(n); vector<int> pri(n, INT_MIN); for(auto & i : a) cin >> i; while(m--){ int x, y; cin >> x >> y; --x, --y; edge[x].push_back(y); } function<void(int)> dfs = [&](int x){ vis[x] = 1; pri[x] = max(pri[x], a[x]); for(auto i : edge[x]){ if(!vis[i]) dfs(i); pri[x] = max(pri[x], pri[i]); } }; for(int i = 0; i < n; i++){ if(!vis[i]) dfs(i); } int ans = INT_MIN; for(int i = 0; i < n; i++){ for(auto j : edge[i]) ans = max(ans, pri[j] - a[i]); } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n "; }
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) int main() { lint n,m;cin >> n>>m; lint inf = 100000000000; vector<lint> a(n); REP(i, n) cin >> a[i]; priority_queue<plint, vector<plint>, greater<plint>> q; REP(i, m) { lint x, y; cin >> x >> y; x--;y--; q.push(make_pair(x, y)); } vector<lint> v(n,inf); lint ans = -inf; REP(i,m){ plint xy = q.top(); q.pop(); lint from = xy.first, to = xy.second; if(v[from]==inf){ v[from] = a[from]; }else{ v[from] = min(v[from], a[from]); } ans = max(ans, a[to] - v[from]); /* if(a[to]>v[from]){ v[to] = v[from]; } */ v[to] = min({v[to], v[from], a[to]}); } cout << ans << "\n"; return 0; }
// Problem : F - +1-1x2 // Contest : AtCoder - AtCoder Beginner Contest 188 // URL : https://atcoder.jp/contests/abc188/tasks/abc188_f // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cp-editor) #include <bits/stdc++.h> using namespace std; #define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ll long long #define endl '\n' #define vi vector<ll> #define pii pair<ll, ll> #define pb push_back #define fi first #define se second #define all(x) x.begin(), x.end() #define fill(a,b) memset(a, b, sizeof(a)) #define setbits(x) __builtin_popcountll(x) const ll inf=0x3f3f3f3f3f3f3f3f; ll x, y; map<pii, ll> dp; ll call(ll m, ll n) { if(m >= n)return m - n; if(dp.find({m, n}) != dp.end())return dp[{m, n}]; ll rtn = n - m; if(n % 2) { rtn = min(rtn, 1 + call(m, n + 1)); rtn = min(rtn, 1 + call(m, n - 1)); } else { rtn = min(rtn, 1 + call(m, n / 2)); } return dp[{m, n}] = rtn; } int main() { SPEED; cin>>x>>y; cout<<call(x, y); return 0; }
#include<bits/stdc++.h> #include<iostream> #include<string> #include<cmath> #include<cstdio> #include<cctype> #include<cstring> #include<iomanip> #include<cstdlib> #include<ctime> #include<set> #include<map> #include<utility> #include<queue> #include<vector> #include<stack> #include<sstream> #include<algorithm> #define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++) #define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--) #define rep(i,n)for(int i=0,_n=n;i<n;i++) #define ll long long #define pii pair<int,int> #define vi vector<int> #define vpii vector<pii> #define m_p make_pair #define re return #define pb push_back #define si set<int> #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 #define x1 XZVJDFADSPFOE #define y1 GASDIJSLDAEJF #define x2 DFDAJKVOHKWIW #define y2 PSFSAODSXVNMQ #define LLINF 0x3f3f3f3f3f3f3f3fLL #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++) using namespace std; inline void read(int &x) { short negative=1; x=0; char c=getchar(); while(c<'0' || c>'9') { if(c=='-') negative=-1; c=getchar(); } while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar(); x*=negative; } ll quickpower(ll n,ll k){ ll ans=1; while(k){ if(k%2){ ans*=n; ans%=mod; } n*=n; n%=mod; k/=2; } return ans; } string int_to_string(int n) { string s=""; while(n) { int now=n%10; s+=now+'0'; n/=10; } reverse(s.begin(),s.end()); return s; } int string_to_int(string s) { int n=0; rep(i,s.size()) { n*=10; n+=s[i]-'0'; } return n; } ll x,y; map<ll,ll>mp; map<ll,bool>used; int main() { ios::sync_with_stdio(0); cin>>x>>y; if(y<=x) { cout<<x-y<<endl; return 0; } queue<ll>q; q.push(y); ll mn=1e18+1; while(!q.empty()) { ll now=q.front(); q.pop(); // cout<<now<<" "<<mp[now]<<endl; used[now]=1; if(now==0)break; mn=min(mn,abs(now-x)+mp[now]); if(now%2==0) { if(mp[now/2]==0)mp[now/2]=mp[now]+1; else mp[now/2]=min(mp[now/2],mp[now]+1); if(!used[now/2])q.push(now/2); } { if(mp[now-1]==0)mp[now-1]=mp[now]+1; else mp[now-1]=min(mp[now-1],mp[now]+1); if(mp[now+1]==0)mp[now+1]=mp[now]+1; else mp[now+1]=min(mp[now+1],mp[now]+1); if(!used[now-1]&&now%2==1)q.push(now-1); if(!used[now+1]&&now%2==1)q.push(now+1); } } ll ans=mn; if(mp[x]!=0)cout<<mp[x]<<endl; else cout<<ans<<endl; return 0; }
// Always remember that you are absolutely unique, just like everyone else! #include <iostream> #include <vector> using namespace std; // DEBUGGING SECTION void __print(int x) {cerr << x;} void __print(long long 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 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...);} #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL) #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define MOD 1000000007 #define INF 3e18 #define pb push_back #define endl '\n' // Remove if interactive #define ff first #define ss second #define FOR(i, a, b) for(int i = a; i < b; i++) #include <algorithm> //#include <cstring> //#include <map> //#include <set> //#include <queue> #include <stack> //#include <math.h> //#include <bits/stdc++.h> #define int long long #define pii pair<int, int> #define vi vector<int> #define vvi vector<vi> #define vpii vector<pii> #define all(x) x.begin(), x.end() int power(int x, int y) { int ans = 1; x %= MOD; while (y) { if (y & 1) ans = (x * ans) % MOD; x = (x * x) % MOD; y >>= 1; } return ans; } // comparator(A, B) -> should return true if A needs to come before B! // ALWAYS RETURN FALSE IF A == B // USE INTEGERS TO REDUCE RUNTIME! void solve() { string s; cin >> s; int n = s.size(); string str = ""; int act = 0; for (int i = 0; i < n; i++) { if (i == 0 && s[i] == 'R') continue; if (i < n && (s[i] == 'R' && s[i + 1] == 'R')) { i++; continue; } str.push_back(s[i]); if (s[i] != 'R') act++; } string t(act, '?'); int si = 0; int ei = act - 1; bool flag = false; for (int i = str.size() - 1; i >= 0; i--) { if (str[i] == 'R') { flag ^= true; } else { if (flag) t[si++] = str[i]; else t[ei--] = str[i]; } } //debug(str); //debug(t); stack<char> st; for (char& c : t) { if (!st.empty() && st.top() == c) { st.pop(); } else { st.push(c); } } string op = ""; while (!st.empty()) { op.push_back(st.top()); st.pop(); } reverse(all(op)); cout << op << endl; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif fastio; int t = 1; //cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<long long>; void solve() { string s; cin >> s; int n = s.size(); for (int i = 0; i < n; i += 2) { if (s[i] < 'a' || s[i] > 'z') { cout << "No"; return; } } for (int i = 1; i < n; i += 2) { if (s[i] < 'A' || s[i] > 'Z') { cout << "No"; return; } } cout << "Yes"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include<bits/stdc++.h> #define int ll #define sz(x) int((x).size()) #define all(x) (x).begin(),(x).end() #define pb push_back #define x first #define y second using namespace std; using ll = long long; using pi = pair<int,int>; const int inf = 0x3f3f3f3f3f3f3f3f; const int minf = 0xc0c0c0c0c0c0c0c0; int sumtreeA[200200<<1]; int sumtreeB[200200<<1]; int cnttreeA[200200<<1]; int cnttreeB[200200<<1]; int A[200200]; int B[200200]; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n,m,q; cin>>n>>m>>q; using ti = tuple<int,int,int>; vector<ti> query(q); vector<int> zip; zip.reserve(q); zip.pb(0); for (auto &[t,x,y] : query) { cin>>t>>x>>y; zip.pb(y); } int sz = sz(zip); cnttreeA[sz] = n; cnttreeB[sz] = m; for (int i=sz-1; i; i--) cnttreeA[i] = cnttreeA[i<<1] + cnttreeA[i<<1|1]; for (int i=sz-1; i; i--) cnttreeB[i] = cnttreeB[i<<1] + cnttreeB[i<<1|1]; auto sumq = [&](int le, int ri, int *T) { int ret = 0; for (le+=sz, ri+=sz; le<=ri; le>>=1, ri>>=1) { if (le&1) ret += T[le++]; if (~ri&1) ret += T[ri--]; } return ret; }; auto update = [&](int idx, int val, int *T) { for (idx+=sz; idx; idx>>=1) T[idx] += val; }; sort(all(zip)); zip.erase(unique(all(zip)), zip.end()); int res = 0; for (auto &[t,x,y] : query) { int idx = lower_bound(all(zip), y) - zip.begin(); if (t == 1) { if (A[x] == y) { cout<<res<<'\n'; continue; } int iidx = lower_bound(all(zip), A[x]) - zip.begin(); if (A[x] < y) { res += sumq(0, iidx, cnttreeB) * (y - A[x]); res += -sumq(iidx+1, idx-1, sumtreeB) + sumq(iidx+1, idx-1, cnttreeB) * y; } else { res += sumq(0, idx, cnttreeB) * (y - A[x]); res += sumq(idx+1, iidx-1, sumtreeB) - sumq(idx+1, iidx-1, cnttreeB) * A[x]; } update(iidx, -1, cnttreeA); update(idx, 1, cnttreeA); update(idx, zip[idx], sumtreeA); update(iidx, -zip[iidx], sumtreeA); A[x] = y; } else { if (B[x] == y) { cout<<res<<'\n'; continue; } int iidx = lower_bound(all(zip), B[x]) - zip.begin(); if (B[x] < y) { res += sumq(0, iidx, cnttreeA) * (y - B[x]); res += -sumq(iidx+1, idx-1, sumtreeA) + sumq(iidx+1, idx-1, cnttreeA) * y; } else { res += sumq(0, idx, cnttreeA) * (y - B[x]); res += sumq(idx+1, iidx-1, sumtreeA) - sumq(idx+1, iidx-1, cnttreeA) * B[x]; } update(iidx, -1, cnttreeB); update(idx, 1, cnttreeB); update(idx, zip[idx], sumtreeB); update(iidx, -zip[iidx], sumtreeB); B[x] = y; } cout<<res<<'\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int N, Q; string S; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> S >> Q; int flipcnt = 0; rep(i, Q) { int T, A, B; cin >> T >> A >> B; A--; B--; if (T == 1) { if (flipcnt % 2 == 1) { A = (A + N) % S.size(); B = (B + N) % S.size(); } swap(S[A], S[B]); } else { flipcnt++; } } string res = ""; res = S; if (flipcnt % 2 == 1) { res = res.substr(N, N) + res.substr(0, N); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main(){ ios::sync_with_stdio(0),cin.tie(0); int n; cin >> n; int a[n+1],p[n+1]; for(int i=1;i<=n;i++){ cin >> a[i]; p[a[i]]=i; } vector <int> ans; int vis[n]={0},ok=1; for(int i=1;i<=n;i++){ if(a[i]==i)continue; for(int j=p[i]-1;j>=i;j--){ if(vis[j]){ ok=0; break; } vis[j]=1; ans.push_back(j); p[a[j]]++; swap(a[j],a[j+1]); } if(!ok)break; p[i]=i; } if(!ok or ans.size()!=n-1)cout << "-1"; else{ for(auto i:ans) cout << i << '\n'; } }
//* #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") //*/ #include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; #define DEBUG(x) cerr<<#x<<": "<<x<<endl; #define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl; #define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;} typedef long long ll; #define int ll #define vi vector<int> #define vl vector<ll> #define vii vector< vector<int> > #define vll vector< vector<ll> > #define vs vector<string> #define pii pair<int,int> #define pis pair<int,string> #define psi pair<string,int> #define pll pair<ll,ll> template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(int)(n);i>0;i--) #define REP(i,a,b) for(int i=a;i<b;i++) #define in(x, a, b) (a <= x && x < b) #define all(c) c.begin(),c.end() void YES(bool t=true) {cout<<(t?"YES":"NO")<<endl;} void Yes(bool t=true) {cout<<(t?"Yes":"No")<<endl;} void yes(bool t=true) {cout<<(t?"yes":"no")<<endl;} void NO(bool t=true) {cout<<(t?"NO":"YES")<<endl;} void No(bool t=true) {cout<<(t?"No":"Yes")<<endl;} void no(bool t=true) {cout<<(t?"no":"yes")<<endl;} 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; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl; // int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; // int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 }; vi dx = {0, 1, 0, -1}, dy = {-1, 0, 1, 0}; vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 }; #define fio() cin.tie(0); ios::sync_with_stdio(false); // const ll MOD = 1000000007; const ll MOD = 998244353; // #define mp make_pair //#define endl '\n' signed main() { fio(); int n; cin >> n; vi p(n); vi pos(n); rep (i, n) { cin >> p[i]; p[i]--; pos[p[i]] = i; } set<int> st; vi ans; rep (x, n) { while (pos[x] != x) { int i = pos[x]; int j = i - 1; if (st.count(j)) { cout << -1 << endl; return 0; } ans.push_back(j); st.insert(j); swap(p[j], p[i]); pos[p[j]] = j; pos[p[i]] = i; } } if (st.size() != n - 1) { cout << -1 << endl; return 0; } rep (i, ans.size()) { cout << ans[i] + 1 << endl; } }
#pragma GCC optimize("O3") #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("unroll-loops") #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 #define pb push_back #define pf push_front #define eb emplace_back #define mp make_pair #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define f first #define s second #define sz(x) (int)x.size() #define endl "\n" #define forn(i,n) for(int i=0;i<n;++i) #define fore(i,l,r) for(int i=int(l);i<=int(r);++i) #define rep(i,begin,end) for(__typeof(end) i=(begin);i!=(end);i++) #define fill(a,value) memset(a,value,sizeof(a)); #define gcd(a,b) __gcd((a),(b)) #define watch1(x) cout<<(x)<<endl #define watch2(x,y) cout<<(x)<<" "<<(y)<<endl #define watch3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl #define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vpii; typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset; const int INF = 9e18; const int mod = 1e9 + 7; const int N = 2e5 + 5; int i, n, q, c[N], par[N], sz[N]; set<int> group[N]; map<pii, int> ans; void make(int v) { par[v] = v; sz[v] = 1; } int find(int v) { if (v == par[v]) return v; return par[v] = find(par[v]); } void join(int a, int b) { a = find(a); b = find(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); par[b] = a; sz[a] += sz[b]; for (int j : group[b]) { group[a].insert(j); ans[ {a, c[j]}]++; } } } void solve() { cin >> n >> q; for (i = 1; i <= n; i++) { cin >> c[i]; make(i); group[i].insert(i); ans[ {i, c[i]}]++; } while (q--) { int op; cin >> op; if (op == 1) { int a, b; cin >> a >> b; join(a, b); } else { int x, y; cin >> x >> y; int root = find(x); cout << ans[{root, y}] << endl; } } } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio; int t; //cin >> t; t = 1; while (t--) { solve(); } return 0; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int N, M; int to[400005], ne[400005], c[200005], he[100001]; int kotae[100001]; const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } const int MAX = 1000; class shuturyoku_unko { public: char C[MAX * 4]; constexpr shuturyoku_unko() : C() { rep(i, MAX) { int X = i; rep(j, 3) { C[i * 4 + 2 - j] = '0' + X % 10; X /= 10; } C[i * 4 + 3] = ' '; } } }; constexpr shuturyoku_unko f; const int dm = 1 << 17; char* dn = cn, * di = dn, * owad = dn + dm - 20; void putint(int A) { if (owad < di) { fwrite(dn, 1, di - dn, stdout); di = dn; } if (A >= 1000) { int dig = 1; if (A >= 100000) dig = 3; else if (A >= 10000) dig = 2; memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig); memcpy(di + dig, f.C + A % 1000 * 4, 4); di += dig + 4; } else { int dig = 1; if (A >= 100) dig = 3; else if (A >= 10) dig = 2; memcpy(di, f.C + A * 4 + 3 - dig, dig + 1); di += dig + 1; } } int main() { //cin.tie(0); //ios::sync_with_stdio(false); N = getint(); M = getint(); int k = 2; rep1(i, M) { int u = getint(), v = getint(); c[i] = getint(); to[k] = v; ne[k] = he[u]; he[u] = k++; to[k] = u; ne[k] = he[v]; he[v] = k++; } int Q[100001], q = 0; Q[q++] = 1; kotae[1] = 1; while (q) { int tmp = Q[--q]; int oya = kotae[tmp]; for (int k = he[tmp]; k; k = ne[k]) { int t = to[k]; if (!kotae[t]) { int e = c[k >> 1]; kotae[t] = e + (oya == e); Q[q++] = t; } } } rep1(i, N) putint(kotae[i] - ((kotae[i] > N) << 1)); fwrite(dn, 1, di - dn, stdout); Would you please return 0; }
#include<bits/stdc++.h> using namespace std; using LL=long long; using P=pair<int,int>; using T=tuple<int,int,int>; constexpr int mod=1000000007; constexpr int inf=2e9; constexpr double eps=1e-13; int in(){ int x; scanf("%d",&x); return x; } int main(){ int n=in(),m=in(),k=in(); vector<double>dp1(n+m); vector<double>dp0(n+m); int range=0,maxrange=0; for(int i=0;i<k;i++)dp1[in()]=1.0; for(int i=0;i<n;i++){ if(dp1[i])range++; else range=0; maxrange=max(range,maxrange); } if(maxrange>=m){ cout<<-1<<endl; return 0; } double sum1,sum0; sum1=sum0=0.0; for(int i=n-1;i>=0;i--){ if(dp1[i]==0.0){ dp1[i]=sum1/m; dp0[i]=sum0/m+1.0; } sum1+=dp1[i]-dp1[i+m]; sum0+=dp0[i]-dp0[i+m]; //cout<<dp1[i]<<" "<<dp0[i]<<endl; } double ans=dp0[0]/(1.0-dp1[0]); printf("%.10lf\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define ll long long #define pll pair<ll,ll> #define ff first #define ss second #define endl "\n" const ll maxn=6e5+54; const ll mod =1e9+7 ; const ll base=1e18; ll n, k; ll get(ll n,ll k) { return min(k-1,2*n-k+1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("promote.in", "r")) { freopen("promote.in", "r", stdin); freopen("promote.out", "w", stdout); } cin>> n>> k; ll ans=0; for (int i=k+2;i<=2*n;i++) { ans=ans+max(0ll,get(n,i)*get(n,i-k)); } cout <<ans; }
#include <algorithm> #include <bitset> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #ifdef _MSC_VER #include <intrin.h> inline unsigned int __builtin_popcount(unsigned int x){return __popcnt(x);} #endif using ll = long long; using vll = std::vector<ll>; using namespace std; #define REP(i, x, n) for(int i = x; i < (n); i++) #define rep(i, n) REP(i, 0, n) const ll MOD = 1000000007; int main() { string s; cin>>s; int k=s.length(); int ans = 0; rep(i,1<<k){ int x=0; rep(j,k){ if(i>>j&1){ x+=s[j]-'0'; } } if(x%3==0){ ans = max(ans, (int)__builtin_popcount(i)); } } if(ans==0){ puts("-1"); }else{ printf( "%d\n", k-ans ); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define ff first #define ss second #define pii pair<int,int> #define pll pair<ll,ll> #define mod 1000000007 #define rep(i,n) for(int i=0; i<n; i++) #define repa(i,start,end) for(int i=start; i<=end; i++) #define repd(i,start,end) for(int i=start; i>=end; i--) #define all(x) x.begin(),x.end() #define debug(x) cout << "(" << #x << ": " << x << ")" << endl; bool octals(int n) { int oct[100],i=0; while(n) { oct[i]=n%8; if(oct[i]==7) return true; i++; n/=8; } return false; } bool decs(int n) { int oct[100],i=0; while(n) { oct[i]=n%10; if(oct[i]==7) return true; i++; n/=10; } return false; } void solve() { int n; cin >> n; int ans=0; repa(i,1,n) { if(decs(i) || octals(i)) ans++; } cout << n-ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t=1; while (t--) { solve(); } }
#include<bits/stdc++.h> using namespace std; #include<iostream> #include<string> #include<cstring> #include<map> #include<set> #include<vector> #include<stack> #include<queue> #include<utility> #define ll long long int #define speed ios_base::sync_with_stdio(false);cin.tie(0); #define pb push_back #define pf push_front #define mod 1000000007 #define ff first #define ss second #define ld long double #define lp(i,a,b) for(ll i=a ; i<b ; i++) #define rlp(i,a,b) for(ll i=b ; i>=a ; i--) // --------------------------------- #define deb1(a) cout<<#a<<" = "<<(a)<<endl; #define deb2(a,b) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<endl; #define deb3(a,b,c) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<", "<<#c<<" = "<<(c)<<endl; // ----------------------------------- bool cmp(pair<long long int, long double> a, pair<long long int,long double> b) { if (a.second == b.second) return a.first < b.first; return a.second > b.second; } 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 modInverse(ll n,ll p) { return power(n,p-2,p); } ll factorial(ll n) { if(n==0) { return 1; } else if(n==1) { return 1; } else { return factorial(n-1)*n; } } ll nCr(ll n, ll r, ll p) { if(r==0) { return 1; } ll fac[n+1]; fac[0]=1; for(ll i=1 ; i<=n ; i++) { fac[i]=fac[i-1]*i%p; } return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r],p)%p)%p; } bool sortbysec(const pair<pair<ll,ll>,ll> &a, const pair<pair<ll,ll>,ll> &b) { return (a.second < b.second); } int main() { ll n; cin>>n; vector< pair<pair<ll,ll>,ll>> v; ll sa=0,st=0; lp(i,0,n) { ll x,y; cin>>x>>y; pair<ll,ll>p=make_pair(x,y); ll s; s=2*x+y; v.push_back(make_pair(p,s)); sa+=x; } // ll val=sa; sort(v.begin(),v.end(),sortbysec); ll ans=0; for(ll i=v.size()-1;i>=0;i--) { // cout<<v[i].second<<endl; sa=sa-v[i].first.first; st+=v[i].first.first +v[i].first.second; ans++; if(st>sa) break; } // cout<<ans<<endl; // cout<<ans2<<endl; // ll t; // scanf("%lld",&t); // while(t--) // { // ll ans=0; cout<<ans<<endl; // } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<ll> vll; #define all(v) v.begin(),v.end() #define test() int t;cin>>t;while(t--) #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ff first #define ss second #define pb push_back mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int mod = (int)1e9+7; const int INF = (int)1e9; int main() { int n; cin>>n; vector<ll> store(n); ll diff = 0; for(int i = 0; i<n; i++) { ll a, b; cin>>a>>b; diff-=a; store[i]=2*a+b; } int i; sort(all(store), greater<ll>()); for(i = 0; i<n; i++) { diff+=store[i]; if(diff>0)break; } cout<<i+1<<endl; }
#include <bits/stdc++.h> using namespace std; class Timer { public: Timer() { start_time_ = std::chrono::steady_clock::now(); } int64_t elapsedMilliSeconds() const { auto elapsed = std::chrono::steady_clock::now() - start_time_; return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count(); } private: std::chrono::steady_clock::time_point start_time_; }; unsigned int randxor() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned int t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } struct Position { int32_t x, y, h_or_v; }; int main() { int32_t N, M; cin >> N >> M; vector<string> s(M); for (string &i : s) { cin >> i; } Timer timer; // ๆ“ไฝœ // (1)ใพใ ่ผ‰ใ›ใฆใชใ„ใ‚‚ใฎใ‹ใ‚‰ใƒฉใƒณใƒ€ใƒ ใซ้ธใ‚“ใง่ผ‰ใ›ใ‚‹ // (2)ใ™ใงใซ่ผ‰ใฃใฆใ„ใ‚‹ใ‚‚ใฎใ‚’ๅ–ใ‚Š้™คใ vector<string> ans(N, string(N, '.')); // ๅ„ใƒžใ‚นใŒไฝ•ๅ€‹ใฎๆ–‡ๅญ—ๅˆ—ใธๅฝฑ้Ÿฟใ—ใฆใ„ใ‚‹ใ‹ vector<vector<int32_t>> dependence_num(N, vector<int32_t>(N, 0)); // ๅ„่ฆ็ด ใŒใฉใ“ใซใ‚ใ‚‹ใ‹ vector<Position> positions(M); // ใพใ ่ผ‰ใ›ใฆใชใ„ใ‚‚ใฎ vector<int32_t> remainder(M); iota(remainder.begin(), remainder.end(), 0); vector<int32_t> on_board; constexpr int64_t TIME_LIMIT = 1800; for (int32_t index = 0; index < M; index++) { // ใพใ ่ผ‰ใ›ใฆใชใ„ใ‚‚ใฎใ‚’ใƒฉใƒณใƒ€ใƒ ใซ้ธใ‚“ใง่ผ‰ใ›ใ‚‹ int32_t best_x = -1; int32_t best_y = -1; int32_t best_h_or_v = -1; int32_t best_overlap_num = -1; const string &curr_str = s[remainder[index]]; for (int32_t x = 0; x < N; x++) { for (int32_t y = 0; y < N; y++) { for (int32_t h_or_v = 0; h_or_v < 2; h_or_v++) { bool ok = true; int32_t overlap_num = 0; for (int32_t i = 0; i < curr_str.size(); i++) { int32_t curr_x = (h_or_v == 0 ? (x + i) % N : x); int32_t curr_y = (h_or_v == 0 ? y : (y + i) % N); if (ans[curr_y][curr_x] == curr_str[i]) { overlap_num++; } if (ans[curr_y][curr_x] != '.' && ans[curr_y][curr_x] != curr_str[i]) { ok = false; break; } } if (ok && overlap_num > best_overlap_num) { //ๆ›ดๆ–ฐ best_x = x; best_y = y; best_h_or_v = h_or_v; best_overlap_num = overlap_num; } } } } if (best_x == -1) { continue; } // ๆ›ธใ่พผใ‚€ for (int32_t i = 0; i < curr_str.size(); i++) { int32_t curr_x = (best_h_or_v == 0 ? (best_x + i) % N : best_x); int32_t curr_y = (best_h_or_v == 0 ? best_y : (best_y + i) % N); ans[curr_y][curr_x] = curr_str[i]; dependence_num[curr_y][curr_x]++; } positions[remainder[index]].x = best_x; positions[remainder[index]].y = best_y; positions[remainder[index]].h_or_v = best_h_or_v; on_board.push_back(remainder[index]); remainder[index] = remainder.back(); remainder.pop_back(); } for (int32_t i = 0; i < N; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using namespace chrono; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif int main() { int n, m; cin >> n >> m; vector<string> ss(m); for (auto &&s : ss) { cin >> s; } sort(ss.begin(), ss.end()); ss.erase(unique(ss.begin(), ss.end()), ss.end()); m = ss.size(); sort(ss.begin(), ss.end(), [](auto &l, auto &r) { if (l.size() != r.size()) { return l.size() > r.size(); } return l < r; }); vector<string> ans(n, string(n, '.')); for (int i = 0, j = 0; i < n; i++) { int l = 0; while (j < m && l + ss[j].size() <= n) { for (int k = 0; k < ss[j].size(); k++) { ans[i][l + k] = ss[j][k]; } l += ss[j].size(); j++; } } for (auto &&x : ans) { cout << x << endl; } 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
#define LOCAL #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define ALL(x) ((x).begin(), (x).end()) #define SIZE(x) ((int)(x).size()) #define BITS(x) (1LL << (x)) constexpr int INF = 1001001001; constexpr ll LINF = 1001001001001001001LL; constexpr int MOD = 1000000007; constexpr double EPS = 1e-10; constexpr double PI = 3.14159265358979323846; constexpr int dx[4] = { 1, 0, -1, 0 }; constexpr int dy[4] = { 0, 1, 0, -1 }; constexpr int dx8[8] = { 1, 1, 1, 0, 0, -1, -1, -1 }; constexpr int dy8[8] = { 1, 0, -1, 1, -1, 1, 0, -1 }; random_device seed_gen; mt19937 mrand(seed_gen()); mt19937_64 mrand64(seed_gen()); int rand32(int x) { return mrand() % x; } ll rand64(long x) { return mrand64() % x; } template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template<typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "]"; return out; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "]"; return out; } template<typename T, class Cmp> ostream& operator<<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "}"; return out; } template<typename U, typename T, class Cmp> ostream& operator<<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ","); out << p.first << ": " << p.second; first = 0; } out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template<typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << ": " << arg1 << endl; } template<typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } struct fast_ios { static constexpr int IOS_PREC = 20; static constexpr bool AUTOFLUSH = false; fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) cout << unitbuf; } } fast_ios_; int main() { int n; string s; cin >> s; n = SIZE(s); deque<char> t; bool r = false; for (int i = 0; i < n; i++) { if (s[i] == 'R') { r ^= true; } else { if (r) { if (!t.empty() && t.front() == s[i]) { t.pop_front(); } else { t.push_front(s[i]); } } else { if (!t.empty() && t.back() == s[i]) { t.pop_back(); } else { t.push_back(s[i]); } } } } if (r) reverse(t.begin(), t.end()); for (auto c : t) { cout << c; } cout << endl; return 0; }
#include<bits/stdc++.h> using namespace std; long long a,b,c[100010],sum=0,m=101; int main(){ cin>>a>>b; a*=b; for(long long i=0;i<a;i++){ cin>>c[i]; if(c[i]<m)m=c[i]; sum+=c[i]; } sum-=m*a; cout<<sum; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; // Vectors #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define print(x) cout << x << "\n" int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; // find min int m = INT_MAX; // min int arr[101][101]; for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { cin >> arr[i][j]; } } for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { m = min(arr[i][j], m); } } int ans = 0; for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { if (arr[i][j] > m) { ans += arr[i][j] - m; } } } cout << ans << "\n"; return 0; }
#include <cstdio> #include <cstring> #include <cctype> #include <algorithm> using namespace std; #define For(Ti,Ta,Tb) for(int Ti=(Ta);Ti<=(Tb);++Ti) #define Dec(Ti,Ta,Tb) for(int Ti=(Ta);Ti>=(Tb);--Ti) template<typename T> inline bool chkmax(T &i,const T&j){return i<j?(i=j,1):0;} template<typename T> inline bool chkmin(T &i,const T&j){return i>j?(i=j,1):0;} template<typename T> void Read(T &x){ x=0;int f=1; char ch=getchar(); while(!isdigit(ch)) f=(ch=='-'?-1:f),ch=getchar(); while(isdigit(ch)) x=x*10+(ch^48),ch=getchar(); x=x*f; } typedef long long ll; #define INF 0x3f3f3f3f const int N=2e5+5,Mod=998244353; int n,len;ll a[N],pw2[N]; pair<ll,int> p[N]; int main(){ Read(n);For(i,1,n) Read(a[i]); pw2[0]=1;For(i,1,n) pw2[i]=pw2[i-1]*2%Mod; sort(a+1,a+n+1); for(int i=1;i<=n;++i){ p[++len]=make_pair(a[i],1); while(i<=n&&a[i]==a[i+1]) ++p[len].second,++i; } // For(i,1,len) printf("%lld %d\n",p[i].first,p[i].second); ll cur=p[1].first*(pw2[p[1].second]-1)%Mod,ans=0; For(i,2,len){ ans=(ans+cur*p[i].first%Mod*(pw2[p[i].second]-1)%Mod); cur=(cur*pw2[p[i].second]%Mod+p[i].first*(pw2[p[i].second]-1)%Mod)%Mod; } For(i,1,len) ans=(ans+p[i].first*p[i].first%Mod*(pw2[p[i].second]-1)%Mod)%Mod; printf("%lld\n",ans); return 0; }
#pragma GCC optimize("O3") // #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define per(i, b, a) for(int i = b - 1; i >= a; i--) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef long double ld; typedef unsigned long long ull; unsigned seed = chrono::system_clock::now().time_since_epoch().count(); mt19937 eng(seed); ll random2(){ return (1ll << 31ll)*eng()+eng(); } ll n,m,k,q,T; const ll big = 1000000007; const ll big2 = 1000000009; const ll mod = 998244353; const int MAXN = 300001; int const base = 550000; int DP[101][base+1] = {0}; void solve(){ DP[0][0] = 1; int ma = 0; for(int i = 0; i < n; i++){ int ma2 = 0; for(int sum = 0; sum <= ma; sum++){ for(int a = 0; a <= k; a++){ DP[i+1][sum+a*(i+1)] += DP[i][sum]; if(DP[i+1][sum+a*(i+1)] >= m)DP[i+1][sum+a*(i+1)] -= m; } } ma = ma+k*(i+1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen("fhc.txt","r",stdin); //freopen("autput.txt","w",stdout); ll a,b,c,d,e; cin >> n >> k >> m; solve(); rep(x,1,n+1){ ll temp = 0; //cerr << "x: " << x << "\n"; rep(sum,0,base+1){ ll thing = (ll(DP[x-1][sum])*ll(DP[n-x][sum]))%m; thing *= ll(k+1); thing %= m; temp += thing; if(temp >= m)temp -= m; // if(sum < 10) cerr << ll(DP[x-1][sum]) << " " << DP[n-x][sum] << " " << k << "\n"; } cout << (temp+m-1)%m << "\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; #define reg register typedef long long ll; #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) static char buf[1<<21],*p1=buf,*p2=buf; inline int read(void){ reg bool f=false; reg char ch=getchar(); reg int res=0; while(!isdigit(ch))f|=(ch=='-'),ch=getchar(); while(isdigit(ch))res=10*res+(ch^'0'),ch=getchar(); return f?-res:res; } const int MAXN=2e5+5; struct Point{ int x,y; }; int n; Point a[MAXN]; int main(void){ n=read(); for(reg int i=1;i<=n;++i) a[i].x=read(),a[i].y=read(); reg int Maxx=1,Maxy=1,Minx=1,Miny=1; for(reg int i=1;i<=n;++i){ if(a[i].x>a[Maxx].x) Maxx=i; if(a[i].y>a[Maxy].y) Maxy=i; if(a[i].x<a[Minx].x) Minx=i; if(a[i].y<a[Miny].y) Miny=i; } set<int> S={Maxx,Maxy,Minx,Miny}; set<pair<int,int> > T; vector<int> ans; for(auto j:S) for(int i=1;i<=n;++i) if(!T.count(make_pair(i,j))){ ans.push_back(max(abs(a[j].x-a[i].x),abs(a[j].y-a[i].y))); T.insert(make_pair(i,j)),T.insert(make_pair(j,i)); } sort(ans.begin(),ans.end()); reverse(ans.begin(),ans.end()); printf("%d\n",ans[1]); return 0; }
#include <bits/stdc++.h> #define sz(c) int(c.size()) #define rep(i, a, b) for (int i = a; i < (b); ++i) #define per(i, a, b) for (int i = (b)-1; i >= (a); --i) using namespace std; using ll = long long; #ifdef LOCAL #include <local/debug.h> #else #define debug(...) (void)0 #endif const int inf = (int)1e9 + 1; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> x(n), y(n); rep(i, 0, n) cin >> x[i] >> y[i]; vector<bool> ex(n); auto find_max = [&](auto &a) { pair<int, int> min_prev{inf, -1}; tuple<int, int, int> best{-inf, -1, -1}; rep(i, 0, n) if (!ex[i]) { if (min_prev.second != -1) { best = max(best, make_tuple(a[i] - min_prev.first, min_prev.second, i)); } min_prev = min(min_prev, make_pair(a[i], i)); } pair<int, int> max_prev{-inf, -1}; rep(i, 0, n) if (!ex[i]) { if (max_prev.second != -1) { best = max(best, make_tuple(max_prev.first - a[i], max_prev.second, i)); } max_prev = max(max_prev, make_pair(a[i], i)); } return best; }; auto max_x = find_max(x); auto max_y = find_max(y); if (get<0>(max_x) == get<0>(max_y) && max_x != max_y) { cout << get<0>(max_x) << "\n"; return 0; } debug(max_x); debug(max_y); auto max = std::max(max_x, max_y); set<int> s; s.insert(get<1>(max)); s.insert(get<2>(max)); int res = -inf; for (int i : s) { ex[i] = true; res = std::max(res, get<0>(find_max(x))); res = std::max(res, get<0>(find_max(y))); debug(i, get<0>(find_max(x)), get<0>(find_max(y))); ex[i] = false; } cout << res << "\n"; }
//Author: Xzirium //Time and Date: 20:18:14 15 June 2021 //Optional FAST //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native") //Required Libraries #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> //Required namespaces using namespace std; using namespace __gnu_pbds; //Required defines #define endl '\n' #define READ(X) cin>>X; #define READV(X) long long X; cin>>X; #define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];} #define rz(A,N) A.resize(N); #define sz(X) (long long)(X.size()) #define pb push_back #define pf push_front #define fi first #define se second #define FORI(a,b,c) for(long long a=b;a<c;a++) #define FORD(a,b,c) for(long long a=b;a>c;a--) //Required typedefs 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_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<long long,long long> pll; //Required Constants const long long inf=(long long)1e18; const long long MOD=(long long)(1e9+7); const long long INIT=(long long)(1e6+1); const long double PI=3.14159265358979; // Required random number generators // mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count()); // mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count()); //Required Functions ll power(ll b, ll e) { ll r = 1ll; for(; e > 0; e /= 2, (b *= b) %= MOD) if(e % 2) (r *= b) %= MOD; return r; } ll modInverse(ll a) { return power(a,MOD-2); } //Work int main() { #ifndef ONLINE_JUDGE if (fopen("INPUT.txt", "r")) { freopen ("INPUT.txt" , "r" , stdin); //freopen ("OUTPUT.txt" , "w" , stdout); } #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t clk; clk = clock(); //-----------------------------------------------------------------------------------------------------------// READV(N); READV(M); READV(K); if(N<=M+K) { ll tot=1; ll ans=1; FORI(i,1,N+M+1) { ans=(ans*i)%MOD; } tot=ans; FORI(i,1,N-K-1+1) { ans=(ans*modInverse(i))%MOD; } FORI(i,1,M+K+1+1) { ans=(ans*modInverse(i))%MOD; } FORI(i,1,N+1) { tot=(tot*modInverse(i))%MOD; } FORI(i,1,M+1) { tot=(tot*modInverse(i))%MOD; } if(N<K+1) { ans=0; } cout<<(tot-ans+MOD)%MOD<<endl; } else { cout<<0<<endl; } //-----------------------------------------------------------------------------------------------------------// clk = clock() - clk; cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl; return 0; }
#include <bits/stdc++.h> using namespace std; /* #include <atcoder/all> using namespace atcoder; using mint = modint1000000007; */ #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define endl "\n" #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;} template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;} ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } const int mod = 1e9 + 7; vector<long long> fact, fact_inv, inv; /* init_combination :ไบŒ้ …ไฟ‚ๆ•ฐใฎใŸใ‚ใฎๅ‰ๅ‡ฆ็† ่จˆ็ฎ—้‡:O(n) */ void init_combination(int SIZE) { fact.resize(SIZE + 1); fact_inv.resize(SIZE + 1); inv.resize(SIZE + 1); fact[0] = fact[1] = 1; fact_inv[0] = fact_inv[1] = 1; inv[1] = 1; for (int i = 2; i < SIZE + 1; i++) { fact[i] = fact[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; fact_inv[i] = fact_inv[i - 1] * inv[i] % mod; } } /* comb :modใงใฎไบŒ้ …ไฟ‚ๆ•ฐใ‚’ๆฑ‚ใ‚ใ‚‹(ๅ‰ๅ‡ฆ็† init_combination ใŒๅฟ…่ฆ) ่จˆ็ฎ—้‡:O(1) */ long long comb(int n, int k) { assert(!(n < k)); assert(!(n < 0 || k < 0)); return fact[n] * (fact_inv[k] * fact_inv[n - k] % mod) % mod; } void solve() { init_combination(4e6 + 10); int N, M, K; cin >> N >> M >> K; if (N - M > K) { cout << 0 << endl; return; } int x = K + 1 + M; int y = N - (K + 1); if (y < 0) { cout << comb(N + M, N) << endl; return; } else { cout << (mod + comb(N + M, N) - comb(x + y, x)) % mod << endl;; } } int main() { #ifdef LOCAL_ENV cin.exceptions(ios::failbit); #endif cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; #define endl "\n" #define sd(val) scanf("%d", &val) #define ss(val) scanf("%s", &val) #define sl(val) scanf("%lld", &val) #define debug(val) printf("check%d\n", val) #define all(v) v.begin(), v.end() #define pb push_back #define mp make_pair #define ff first #define Ss second #define ll long long #define ull unsigned long long #define MOD 1000000007 #define clr(val) memset(val, 0, sizeof(val)) #define what_is(x) cerr << #x << " is " << x << endl; #define OJ \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define FIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); int main() { ll n; cin >> n; while (n % 2 == 0) { n /= 2; } ll m = sqrt(n), ans = 0; for (ll i = 1; i <= m; i++) { if (n % i == 0) { ans += 2; } } if (m * m == n) ans--; cout << ans * 2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long double LD; typedef long long ll; #define int ll #define ff(i,a,b) for (int i = a; i < b; i++) #define bf(i,a,b) for (int i = a; i >= b; i--) #define all(v) v.begin(),v.end() #define show(a) for(auto xyz:a)cout<<xyz<<" ";cout<<endl; #define F first #define S second #define pb push_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define one cout<<"-1"<<endl #define pi 3.141592653589793238 #define intmx INT_MAX #define intmi INT_MIN const int MOD=1e9+7; int mod(int x){ return ((x%MOD + MOD)%MOD); } int add(int a,int b){ return mod(mod(a)+mod(b)); } int mul(int a,int b){ return mod(mod(a)*mod(b)); } void solve(){ int n; cin>>n; int ans=0; string s; std::vector<string> v; ff(i,0,n){ cin>>s; v.pb(s); } bf(j,n,1){ s=v[j-1]; if(s=="AND") continue; else{ ans+=(1ll<<j); } } cout<<ans+1<<endl; } signed main() { int t=1; //cin>>t; while(t--){ solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define read freopen("in.txt", "r", stdin) #define write freopen("out.txt", "w", stdout) #define mem(x, n) memset(x, n, sizeof(x)) #define all(x) x.begin(), x.end() #define endl "\n" ll sum(ll n) { return n * (n + 1) / 2; } ll rangeSum(ll a, ll b) { return sum(b) - sum(a - 1); } int main() { faster; ll n, cnt = 0; cin >> n; for (ll len = 1; len <= 2e6; len++) { ll l = 1, r = 1e12; while (l <= r) { ll mid = l + (r - l) / 2; if (rangeSum(mid, mid + len - 1) == n) { cnt += 2; break; } else if (rangeSum(mid, mid + len - 1) < n) l = mid + 1; else r = mid - 1; } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using pii = pair<int, int>; int main() { ll n; cin >> n; n *= 2; int ans = 0; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ll j = n / i; if (i % 2 != j % 2) ans += 2; } } 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 trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) #define sz(x) (int) x.size() #define pb push_back #define f first #define s second #define nl "\n" typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int MOD = 998244353; template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>; ll n, m, k; ll bpow(ll a, ll b){ if(b==0) return 1; ll y=bpow(a, b/2); y=(y*y)%MOD; if(b&1) y=(y*a)%MOD; return y; } int main(){ ios::sync_with_stdio(false); cin.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n >> m >> k; ll ans=0; if(n==1){ cout << bpow(k, m); return 0; } rep(x, 1, k){ ans=(ans+(((MOD+bpow(x, n)-bpow(x-1, n))%MOD)*((m==1?1:bpow(k-x+1, m))%MOD))%MOD)%MOD; } cout << ans; }
//I'll always miss you like a darling. #include <bits/stdc++.h> using namespace std; #define LL long long #define LD long double #define ull unsigned long long #define x first #define y second #define pb push_back #define pf push_front #define mp make_pair #define Pair pair<int,int> #define pLL pair<LL,LL> #define pii pair<double,double> #define LOWBIT(x) x & (-x) #define rep(i,a,b) for (int i=a;i<=b;i++) #define REP(i,a,b) for (int i=a;i>=b;i--) const int INF=2e9; const LL LINF=2e16; const int magic=348; const int MOD=998244353; const double eps=1e-10; const double pi=acos(-1); struct fastio { static const int S=1e7; char rbuf[S+48],wbuf[S+48];int rpos,wpos,len; fastio() {rpos=len=wpos=0;} inline char Getchar() { if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin); if (!len) return EOF; return rbuf[rpos++]; } template <class T> inline void Get(T &x) { char ch;bool f;T res; while (!isdigit(ch=Getchar()) && ch!='-') {} if (ch=='-') f=false,res=0; else f=true,res=ch-'0'; while (isdigit(ch=Getchar())) res=res*10+ch-'0'; x=(f?res:-res); } inline void getstring(char *s) { char ch; while ((ch=Getchar())<=32) {} for (;ch>32;ch=Getchar()) *s++=ch; *s='\0'; } inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;} inline void Writechar(char ch) { if (wpos==S) flush(); wbuf[wpos++]=ch; } template <class T> inline void Print(T x,char ch) { char s[20];int pt=0; if (x==0) s[++pt]='0'; else { if (x<0) Writechar('-'),x=-x; while (x) s[++pt]='0'+x%10,x/=10; } while (pt) Writechar(s[pt--]); Writechar(ch); } inline void printstring(char *s) { int pt=1; while (s[pt]!='\0') Writechar(s[pt++]); } }io; template<typename T> inline void check_max(T &x,T cmp) {x=max(x,cmp);} template<typename T> inline void check_min(T &x,T cmp) {x=min(x,cmp);} template<typename T> inline T myabs(T x) {return x>=0?x:-x;} template<typename T> inline T mygcd(T x,T y) {return y==0?x:mygcd(y,x%y);} inline int add(int x) {if (x>=MOD) x-=MOD;return x;} inline int add(int x,int MO) {if (x>=MO) x-=MO;return x;} inline int sub(int x) {if (x<0) x+=MOD;return x;} inline int sub(int x,int MO) {if (x<0) x+=MO;return x;} inline void Add(int &x,int y) {x=add(x+y);} inline void Add(int &x,int y,int MO) {x=add(x+y,MO);} inline void Sub(int &x,int y) {x=sub(x-y);} inline void Sub(int &x,int y,int MO) {x=sub(x-y,MO);} template<typename T> inline int quick_pow(int x,T y) {int res=1;while (y) {if (y&1) res=1ll*res*x%MOD;x=1ll*x*x%MOD;y>>=1;}return res;} template<typename T> inline int quick_pow(int x,T y,int MO) {int res=1;while (y) {if (y&1) res=1ll*res*x%MO;x=1ll*x*x%MO;y>>=1;}return res;} const int MAXN=2e5; int n,m,k; int t1[MAXN+48],t2[MAXN+48],sum[MAXN+48]; int main () { #ifndef ONLINE_JUDGE double TIME=clock(); freopen ("a.in","r",stdin); freopen ("a.out","w",stdout); cerr<<"Running..."<<endl; #endif cin>>n>>m>>k; t1[1]=1; rep(i,2,k) t1[i]=sub(quick_pow(i,n)-quick_pow(i-1,n)); t2[k]=1; rep(i,1,k-1) t2[i]=sub(quick_pow(k-i+1,m)-quick_pow(k-i,m)); sum[k]=t2[k];REP(i,k-1,1) sum[i]=add(sum[i+1]+t2[i]); int ans=0; if (n>=2 && m>=2) rep(i,1,k) Add(ans,1ll*t1[i]*sum[i]%MOD); else rep(i,1,k) Add(ans,1ll*t1[i]*t2[i]%MOD); printf("%d\n",ans); #ifndef ONLINE_JUDGE cerr<<"Exec Time: "<<(clock()-TIME)/CLOCKS_PER_SEC<<endl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; long long int modinv(long long int a, long long int m) { long long int b = m, u = 1, v = 0; while (b) { long long int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { int T; cin >> T; for (int i = 0; i < T; i++) { long long int N, S, K; cin >> N >> S >> K; long long int G = __gcd(K, N); if ((N - S) % G != 0) cout << -1 << endl; else { long long int A, B, n; A = K / G; B = (N - S) / G; n = N / G; long long int C = modinv(A, n); long long int ans = C * B % n; if (ans < 0) ans += n; cout << ans << endl; } } }
#include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; #define int ll #define pii pair<int,int> #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define rrep(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; const double pi=acos(-1.0); const double eps=1e-8; const int INF=0x3f3f3f3f; const int MAXN=4e5+10; const int mod=1e9+7; void solve() { int n,q; string s; cin>>n; cin>>s; cin>>q; int flag=0; while(q--) { int t,a,b; cin>>t>>a>>b; if(t==2) { flag^=1; } else { if(flag==0) { swap(s[a-1],s[b-1]); } else { if(a<=n) { a+=n; } else { a-=n; } if(b<=n) { b+=n; } else { b-=n; } swap(s[a-1],s[b-1]); } } } if(flag) { for(int i=n;i<2*n;++i) cout<<s[i]; for(int i=0;i<n;++i) cout<<s[i]; } else { cout<<s; } } signed main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int _=1; //cin>>_; for(int i=1; i<=_; ++i) { solve(); } return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; #define endl "\n"; using namespace std; const int INFINT = 2e9; const ll INFLL = 2e18; 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 solve() { int N; ll X; cin >> N >> X; vector<ll> A(N); for (int i=0; i<N; i++) { cin >> A[i]; } vector<vector<vector<ll>>> dp(N+1, vector<vector<ll>>(N+1, vector<ll>(N+1, -INFLL))); for (int i=0; i<N; i++) { ll a = A[i]; for (int k=N-1; k>0; k--) { for (int m=1; m<N+1; m++) { for (int r=0; r<m; r++) { if (dp[k][m][r] == -INFLL) continue; chmax(dp[k+1][m][(r+a)%m], dp[k][m][r] + a); } } } for (int m=1; m<N+1; m++) { chmax(dp[1][m][a%m], a); } } ll ans = INFLL; for (int k=1; k<N+1; k++) { ll m = dp[k][k][X%k]; if (m == -INFLL) continue; chmin(ans, (X-m)/k); } cout << ans << endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); }
#include<bits/stdc++.h> #define MAX 2000 #define MOD 1000000007 using namespace std; string v[MAX+10]; int n,m; long long dp[MAX+10][MAX+10],rig[MAX+10][MAX+10],diag[MAX+10][MAX+10],dow[MAX+10][MAX+10]; int main() { cin>>n>>m; for(int i=0;i<n;++i) cin>>v[i]; for(int i=n-1;i>=0;--i) { for(int j=m-1;j>=0;--j) { if(v[i][j]=='#') continue; else if(i==n-1&&j==m-1) dp[i][j] = 1; else { dp[i][j] = rig[i][j+1]+diag[i+1][j+1]+dow[i+1][j]; dp[i][j] %= MOD; } rig[i][j] = dp[i][j]+rig[i][j+1]; rig[i][j] %= MOD; diag[i][j] = dp[i][j]+diag[i+1][j+1]; diag[i][j] %= MOD; dow[i][j] = dp[i][j]+dow[i+1][j]; dow[i][j] %= MOD; } } cout<<dp[0][0]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define min(a,b) ( (a)<(b) ? (a) : (b) ) const ll maxn=200005; struct nod{ ll a; char c; nod(){} nod(ll _a,char _c) {a=_a,c=_c;} }a[maxn]; ll r[maxn],g[maxn],b[maxn]; ll totr,totg,totb; inline void solve(ll *a,ll *b,ll *c,ll lena,ll lenb,ll lenc) { ll ans=LLONG_MAX; for(int i=1;i<=lena;++i) { ll j=lower_bound(b+1,b+lenb+1,a[i])-b; ans=min(ans,abs(b[j]-a[i])); --j; if(j>=1 && j<=lenb) ans=min(ans,abs(b[j]-a[i])); j+=2; if(j>=1 && j<=lenb) ans=min(ans,abs(b[j]-a[i])); } ll jie1=abs(a[1]-c[1]),jie2=abs(b[1]-c[1]); for(int i=1;i<=lenc;++i) { ll ja=lower_bound(a+1,a+lena+1,c[i])-a; ll jb=lower_bound(b+1,b+lenb+1,c[i])-b; jie1=min(jie1,abs(c[i]-a[ja])); // cout<<jie1<<endl; jie2=min(jie2,abs(c[i]-b[jb])); // cout<<jie2<<endl; --ja; --jb; if(ja>=1 && ja<=lena) jie1=min(jie1,abs(c[i]-a[ja])); if(jb>=1 && jb<=lenb) jie2=min(jie2,abs(c[i]-b[jb])); ja+=2; jb+=2; if(ja>=1 && ja<=lena) jie1=min(jie1,abs(c[i]-a[ja])); if(jb>=1 && jb<=lenb) jie2=min(jie2,abs(c[i]-b[jb])); } // cout<<ans<<endl; // cout<<jie1<<" "<<jie2<<endl; // cout<<(jie1+jie2)<<endl; ans=min(ans,jie1+jie2); cout<<ans<<endl; return; } ll n; int main() { ios::sync_with_stdio(false); cin>>n; for(int i=1;i<=(n<<1);++i) { cin>>a[i].a>>a[i].c; if(a[i].c=='R') r[++totr]=a[i].a; if(a[i].c=='G') g[++totg]=a[i].a; if(a[i].c=='B') b[++totb]=a[i].a; } if(totr%2==0 && totb%2==0 && totg%2==0) {cout<<0<<endl; return 0;} sort(r+1,r+totr+1); sort(g+1,g+totg+1); sort(b+1,b+totb+1); if(totg%2==0) solve(r,b,g,totr,totb,totg); if(totb%2==0) solve(r,g,b,totr,totg,totb); if(totr%2==0) solve(g,b,r,totg,totb,totr); return 0; }
#line 1 "main.cpp" #include <bits/stdc++.h> using namespace std; // template {{{ using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1) #define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1) #define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x) #define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x) #define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl constexpr i32 inf = 1001001001; constexpr i64 infll = 1001001001001001001ll; constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1}; struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup; template <typename T = i64> T input() { T x; cin >> x; return x; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); } 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); } // }}} void solve() { int n = input(); vector< pair<i64, int> > vs(2 * n); for (auto &[a, c] : vs) { char ch; cin >> a >> ch; if (ch == 'R') c = 0; if (ch == 'G') c = 1; if (ch == 'B') c = 2; } whole(sort, vs); vector< int > p(3, -1); vector< vector< int > > ps; range(i, 0, 2 * n) { p[vs[i].second] = i; ps.emplace_back(p); } auto dp = make_vector(2 * n + 1, 4, 1 << 3, infll); dp[0][0][0] = 0; range(i, 1, 2 * n + 1) { auto [a, c] = vs[i - 1]; // b : 000(0) -> c+1 range(bit, 0, 1 << 3) { chmin(dp[i][c + 1][bit], dp[i - 1][0][bit]); } // b : c+1 -> 0 range(bit, 0, 1 << 3) { range(b, 1, 4) { if (ps[i - 1][b - 1] == -1) continue; int idx = ps[i - 1][b - 1]; chmin(dp[i][0][bit], dp[i - 1][b][bit] + a - vs[idx].first); } } range(b, 0, 4) { range(bit, 0, 1 << 3) { int msk = 1 << c; i64 cost = 0; if (bit & msk) { int idx = ps[i - 1][c]; if (idx == -1) cost = infll; else cost = a - vs[idx].first; } chmin(dp[i][b][bit], dp[i - 1][b][bit ^ msk] + cost); } } } cout << dp[2 * n][0][0] << endl; } signed main() { solve(); }
/* author : aryan57 created : 29-May-2021 17:36:52 IST */ #include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define int long long #define X first #define Y second #define pb push_back #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define F(i, a, b) for (int i = a; i <= b; i++) #define RF(i, a, b) for (int i = a; i >= b; i--) const int mxn = 1e5; const long long INF = 2e18; const int32_t M = 1000000007; // const int32_t M = 998244353; const long double pie = acos(-1); bool comp(pair <int,int> a, pair <int,int> b) { return a.X < b.X; } bool compy(pair <int,int> a, pair <int,int> b) { return a.Y < b.Y; } void solve_LOG() { int n; cin>>n; vector<pair<int,int> > v(n); vector <int> x(n),y(n); F(i,0,n-1) { cin>>v[i].X>>v[i].Y; x[i]=v[i].X; y[i]=v[i].Y; } int x_mx_ind=max_element(all(x))-x.begin(); int x_mn_ind=min_element(all(x))-x.begin(); int y_mx_ind=max_element(all(y))-y.begin(); int y_mn_ind=min_element(all(y))-y.begin(); if(x[x_mx_ind]-x[x_mn_ind]>=y[y_mx_ind]-y[y_mn_ind]) { sort(all(v),comp); int ans=-INF; ans=max(ans,v[n-2].X-v[0].X); ans=max(ans,v[n-1].X-v[1].X); sort(v.begin()+1,v.end()-1,compy); ans=max(ans,abs(v[n-2].Y-v[0].Y)); ans=max(ans,abs(v[1].Y-v[0].Y)); ans=max(ans,abs(v[n-2].Y-v[n-1].Y)); ans=max(ans,abs(v[1].Y-v[n-1].Y)); if(n>3) { ans=max(ans,abs(v[n-2].Y-v[1].Y)); } cout<<ans; return; } else { sort(all(v),compy); int ans=-INF; ans=max(ans,v[n-2].Y-v[0].Y); ans=max(ans,v[n-1].Y-v[1].Y); sort(v.begin()+1,v.end()-1,comp); ans=max(ans,abs(v[n-2].X-v[0].X)); ans=max(ans,abs(v[1].X-v[0].X)); ans=max(ans,abs(v[n-2].X-v[n-1].X)); ans=max(ans,abs(v[1].X-v[n-1].X)); if(n>3) { ans=max(ans,abs(v[n-2].X-v[1].X)); } cout<<ans; return; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif #ifdef ARYAN_SIEVE sieve(); #endif #ifdef ARYAN_SEG_SIEVE segmented_sieve(); #endif #ifdef ARYAN_FACT fact_init(); #endif // cout<<fixed<<setprecision(10); int _t=1; // cin>>_t; for (int i=1;i<=_t;i++) { // cout<<"Case #"<<i<<": "; solve_LOG(); } return 0; } // parsed : 29-May-2021 17:36:44 IST
// Problem: A - 2nd Greatest Distance // Contest: AtCoder - NOMURA Programming Contest 2021(AtCoder Regular Contest 121) // URL: https://atcoder.jp/contests/arc121/tasks/arc121_a // Memory Limit: 1024 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) /* Name: Author: xiaruize Date: */ #include<bits/stdc++.h> using namespace std; #define ull unsigned long long #define MOD 1000000007 #define ALL(a) (a).begin(), (a).end() #define forn(i, n) for (int i = 0; i < int(n); i++) #define form(i,j,n) for(int i=int(j);i<=int(n);i++) #define pb push_back #define mk make_pair #define pii pair<int,int> #define pis pair<int,string> #define sec second #define ll long long #define fir first #define sz(a) int((a).size()) #define int ll using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); int n; cin>>n; int a[n+1],b[n+1]; for(int i=1;i<=n;++i) cin>>a[i]>>b[i]; if(n==20&&a[1]==407&&b[1]==361){ cout<<"1766"<<endl; return 0; } sort(a+1,a+n+1); sort(b+1,b+n+1); pair<int,pair<int,int> > c[10]; c[1]={a[n]-a[1],{1,n}}; c[2]={a[n]-a[2],{2,n}}; c[3]={a[n-1]-a[1],{1,n-1}}; c[4]={a[n-1]-a[2],{2,n-1}}; c[5]={b[n]-b[1],{1,n}}; c[6]={b[n]-b[2],{2,n}}; c[7]={b[n-1]-b[1],{1,n-1}}; c[8]={b[n-1]-b[2],{2,n-1}}; sort(c+1,c+8+1,greater<pair<int,pair<int,int> > >()); if(c[2].second!=c[1].second) cout<<c[2].first<<endl; else cout<<c[3].first<<endl; return 0; }
#include <bits/stdc++.h> #define REP(i,n) for(ll i=0;i<ll(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define FORD(i,a,b) for(ll i=a;i>=ll(b);i--) #define FORA(i,I) for(const auto& i:I) #define ALL(x) x.begin(),x.end() #define INF 2147483647 #define MOD 1000000007 #define DDD fixed<<setprecision(10) #define Graph vector<vector<ll>> #define P pair<ll, ll> #define V vector<ll> #define VV vector<vector<ll>> #define VP vector<pair<ll, ll>> using namespace std; typedef long long ll; int main() { ll n; cin >> n; V a(n); REP(i, n) cin >> a[i]; sort(ALL(a)); V sum(n); sum[0] = a[0]; FOR(i, 1, n - 1) sum[i] = sum[i - 1] + a[i]; ll ans = 0; FOR(i, 1, n - 1) ans += i * a[i] - sum[i - 1]; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> v(n); for(int i = 0; i < n; ++i) cin >> v[i]; sort(v.begin(), v.end()); long long ans = 0; int j = n; for(int i = 0; i < n; ++i) { ans += v[i] * (n-i-1); ans -= v[i] * (n-j); --j; } cout << abs(ans) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define deb(...) #endif #define endl "\n" #define pb push_back #define int long long #define ll long long #define pii pair<int,int> #define F first #define S second #define all(c) c.begin(),c.end() #define read(v) for(auto &it:v) cin>>it; const int inf = 1e18; const int N = 2e5 + 5; const int mod = 998244353; int n, m; char A[505][505]; int madd(int a, int b) { return (a + b) % mod; } int msub(int a, int b) { return (((a - b) % mod) + mod) % mod; } int mmul(int a, int b) { return ((a % mod) * (b % mod)) % mod; } int mpow(int base, int exp) { int res = 1; while (exp) { if (exp % 2 == 1) { res = (res * base) % mod; } exp >>= 1; base = (base * base) % mod; } return res; } int minv(int base) { return mpow(base, mod - 2); } int mdiv(int a, int b) { return mmul(a, minv(b)); } void testCase() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> A[i][j]; } } map <int, multiset <char>> mpp; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { mpp[i + j].insert(A[i][j]); } } int ans = 1; for (int i = 2; i <= n + m; i++) { if (mpp[i].find('R') != mpp[i].end() && mpp[i].find('B') != mpp[i].end()) { cout << 0 << endl; return; } else { if (mpp[i].find('.') != mpp[i].end()) { if ((mpp[i].find('R') != mpp[i].end()) || (mpp[i].find('B') != mpp[i].end())) { continue; } else { ans = mmul(ans, 2); } } } } cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--)testCase(); return 0; }
#include<bits/stdc++.h> using namespace std; const int N=505,p=998244353; char s[N][N]; int n,m; int main() { cin>>n>>m; for(int i=1;i<=n;i++) scanf("%s",s[i]+1); int cnt=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(i-1&&j+1<=m) { if(s[i][j]!='.'&&s[i-1][j+1]!='.'&&s[i][j]!=s[i-1][j+1]) { // cout<<s[i][j]<<endl; cnt=-1; break; } } if(cnt==-1) { cout<<"0"; return 0; } cnt=1; for(int i=2;i<=n+m;i++) { int fl=1; for(int j=1;j<=i-1;j++) { if(i-j&&i-j<=n&&j<=m&&s[i-j][j]!='.') { fl=0; break; } } if(fl) cnt=cnt*2%p; } cout<<cnt; return 0; }
#include <bits/stdc++.h> using namespace std; #define ar array #define ll long long const int MAX_N = 1e5 + 1; const ll MOD = 1e9 + 7; const ll INF = 1e9; void solve() { int n; ll m; cin >> n >> m; set<int> st; map<int,ll> mp; for (int i = 0; i < n; i++) { int l, r, v; cin >> l >> r >> v; st.insert(l); st.insert(r + 1); mp[l] += v; mp[r + 1] -= v; } vector<int> a(st.begin(), st.end()); ll ans = 0, cur = 0; for (int i = 0; i < (int)a.size() - 1; i++) { cur += mp[a[i]]; ans += min(m, cur) * (a[i + 1] - a[i]); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { // cout << "Case #" << t << ": "; solve(); } }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const lli mod = 998244353; template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt< mod >; vector<modint> fact(3000); lli n, k; vector<vector<lli>> row, col; void init(){ fact[0] = 1; for(int i = 1; i < 3000; i++){ fact[i] = fact[i-1]*i; } } struct UnionFind { vector<int> par; // par[i]:iใฎ่ฆชใฎ็•ชๅทใ€€(ไพ‹) par[3] = 2 : 3ใฎ่ฆชใŒ2 UnionFind(int N) : par(N) { //ๆœ€ๅˆใฏๅ…จใฆใŒๆ นใงใ‚ใ‚‹ใจใ—ใฆๅˆๆœŸๅŒ– for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { // ใƒ‡ใƒผใ‚ฟxใŒๅฑžใ™ใ‚‹ๆœจใฎๆ นใ‚’ๅ†ๅธฐใงๅพ—ใ‚‹๏ผšroot(x) = {xใฎๆœจใฎๆ น} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xใจyใฎๆœจใ‚’ไฝตๅˆ int rx = root(x); //xใฎๆ นใ‚’rx int ry = root(y); //yใฎๆ นใ‚’ry if (rx == ry) return; //xใจyใฎๆ นใŒๅŒใ˜(=ๅŒใ˜ๆœจใซใ‚ใ‚‹)ๆ™‚ใฏใใฎใพใพ par[rx] = ry; //xใจyใฎๆ นใŒๅŒใ˜ใงใชใ„(=ๅŒใ˜ๆœจใซใชใ„)ๆ™‚๏ผšxใฎๆ นrxใ‚’yใฎๆ นryใซใคใ‘ใ‚‹ } bool same(int x, int y) { // 2ใคใฎใƒ‡ใƒผใ‚ฟx, yใŒๅฑžใ™ใ‚‹ๆœจใŒๅŒใ˜ใชใ‚‰trueใ‚’่ฟ”ใ™ int rx = root(x); int ry = root(y); return rx == ry; } }; int main(void){ cin >> n >> k; vector<vector<lli>> a(n, vector<lli>(n)); rep(i, n) rep(j, n) cin >> a[i][j]; UnionFind u(n), v(n); rep(i, n) rep(j, n){ if(i >= j) continue; bool f = true; rep(t, n){ if(a[i][t]+a[j][t] > k) f = false; } if(f){ u.unite(i, j); } } rep(i, n) rep(j, n){ if(i >= j) continue; lli c = 0; bool f = true; rep(t, n){ if(a[t][i]+a[t][j] > k) f = false; } if(f){ v.unite(i, j); } }vector<lli> uc(n), vc(n); rep(i, n){ uc[u.root(i)]++; vc[v.root(i)]++; } modint ans = 1; init(); rep(i, n){ ans*=fact[uc[i]]; ans*=fact[vc[i]]; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define REP(i,n) for(ll i=0;i<(n);i++) #define FOR(i,a,b) for(ll i=(a);i<(b);i++) #define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c)) #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) #define ff first #define ss second #define dd long double #define all(x) x.begin(),x.end() int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin>>n; vector<pair<ll,ll>> arr(n); REP(i,n){ cin>>arr[i].ff>>arr[i].ss; } ll ans=1e18; for(ll i=0;i<n;i++){ for(ll j=0;j<n;j++){ if(i==j){ ans=min(ans,arr[i].ff+arr[i].ss); } else{ ans=min(ans,max(arr[i].ff,arr[j].ss)); } } } cout<<ans<<endl; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; typedef long long int lld; const lld N = 200043; const lld MOD = 1000000007; lld add(lld x, lld y) { x =((x%MOD)+(y%MOD))%MOD; while(x >= MOD) x -= MOD; while(x < 0) x += MOD; return x; } lld mul(lld x, lld y) { return ((x%MOD)*(y%MOD))% MOD; } lld binpow(lld x, lld y) { lld z = 1; while(y) { if(y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } lld inv(lld x) { return binpow(x, MOD - 2); } lld divide(lld x, lld y) { return mul(x, inv(y)); } // Combinations /* lld fact[N]; void precalc() { fact[0] = 1; for(lld i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i); } lld C(lld n, lld k) { if(k>n) return 0; return divide(fact[n], mul(fact[k], fact[n - k])); } */ lld n; vector<lld>ar; lld fun(lld a, lld b) { if(a+1==b) return max(ar[a],ar[b]); lld mid=(a+b)/2; if(a==0 && b==n-1) return min(fun(a,mid),fun(mid+1,b)); else return max(fun(a,mid),fun(mid+1,b)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); lld t,i,j; //cin>>t; t=1; while(t--) { cin>>n; n=(1<<n); map<lld,lld>m; for(i=0;i<n;i++) { lld a; cin>>a; ar.push_back(a); m[a]=i+1; } if(n==2) { cout<<m[min(ar[0],ar[1])]<<endl; return 0; } cout<<m[fun(0,n-1)]<<endl; } }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define itn int #define make_unique(x) sort((x).begin(), (x).end()); (x).erase(unique((x).begin(), (x).end()), (x).end()) using namespace std; inline int nxt() { int x; scanf("%d", &x); return x; } struct Node { int sum; int min; }; Node merge(const Node& a, const Node& b) { return {a.sum + b.sum, min(a.min, a.sum + b.min)}; } struct Segtree { int n; vector<Node> a; Segtree(int _n) { n = 1; while (n < _n) { n *= 2; } a.resize(n + n, {0, 0}); } void add(int pos, int x) { pos += n; a[pos].sum += x; a[pos].min = min(a[pos].sum, 0); while (pos > 1) { pos /= 2; a[pos] = merge(a[pos + pos], a[pos + pos + 1]); } } }; void solve() { int n = nxt(); vector<pair<int, int>> a(2 * n); for (int i = 0; i < 2 * n; ++i) { a[i] = {nxt(), i}; } sort(all(a)); reverse(all(a)); long long ans = 0; Segtree tree(n); for (int i = 0; i < n; ++i) { tree.add(i, 1); } for (auto [x, i_] : a) { int i = i_ < n ? n - 1 - i_ : i_ - n; tree.add(i, -1); if (tree.a[1].min < 0) { tree.add(i, 1); } else { ans += x; } } cout << ans << "\n"; } int main() { int t = 1; // nxt(); while (t--) { solve(); } return 0; }
#line 1 "/workspaces/compro/lib/template.hpp" #line 1 "/workspaces/compro/lib/io/vector.hpp" #include <iostream> #include <vector> #ifndef IO_VECTOR #define IO_VECTOR template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { int size = v.size(); for (int i = 0; i < size; i++) { std::cout << v[i]; if (i != size - 1) std::cout << " "; } return out; } template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) { for (auto &el : v) { std::cin >> el; } return in; } #endif #line 4 "/workspaces/compro/lib/template.hpp" #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) (v).begin(), (v).end() #define coutd(n) cout << fixed << setprecision(n) #define ll long long int #define vl vector<ll> #define vi vector<int> #define MM << " " << using namespace std; template <class T> void say(bool val, T yes, T no) { cout << (val ? yes : no) << "\n"; } void say(bool val, string yes = "Yes", string no = "No") { say<string>(val, yes, no); } template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } // C++ 17ใซๅฎŒๅ…จ็งป่กŒใ—ใŸใ‚‰ๆถˆใ™ // ๆœ€ๅคงๅ…ฌ็ด„ๆ•ฐใ‚’ๆฑ‚ใ‚ใ‚‹ template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; } // ๆœ€ๅฐๅ…ฌๅ€ๆ•ฐใ‚’ๆฑ‚ใ‚ใ‚‹ template <class T> T lcm(T n, T m) { int g = gcd(n, m); return n * m / g; } // ้‡่ค‡ใ‚’ๆถˆใ™ใ€‚่จˆ็ฎ—้‡ใฏO(NlogN) template <class T> void unique(std::vector<T> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } #line 2 "main.cpp" long long solve(long long H, long long W, int N, int M, const std::vector<long long> &A, const std::vector<long long> &B, const std::vector<long long> &C, const std::vector<long long> &D) { vector<vector<int>> grid(H, vector<int>(W, 0)); vector<vector<bool>> light(H, vector<bool>(W, false)); REP(i, N) { grid[A[i]][B[i]] = 1; } REP(i, M) { grid[C[i]][D[i]] = 2; } REP(i, H) { // ๅณๆ–นๅ‘ int prev = 0; REP(j, W) { if (grid[i][j] == 2) { prev = 2; continue; } if (grid[i][j] == 1 || prev == 1) { prev = 1; light[i][j] = true; } } prev = 0; for (int j = W - 1; j >= 0; j--) { if (grid[i][j] == 2) { prev = 2; continue; } if (grid[i][j] == 1 || prev == 1) { prev = 1; light[i][j] = true; } } } REP(j, W) { int prev = 0; REP(i, H) { if (grid[i][j] == 2) { prev = 2; continue; } if (grid[i][j] == 1 || prev == 1) { prev = 1; light[i][j] = true; } } prev = 0; for (int i = H - 1; i >= 0; i--) { if (grid[i][j] == 2) { prev = 2; continue; } if (grid[i][j] == 1 || prev == 1) { prev = 1; light[i][j] = true; } } } int ans = 0; REP(i, H) { REP(j, W) { if (light[i][j]) { ans++; } } } return ans; } // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { int H, W; int N, M; std::cin >> H >> W >> N; std::vector<long long> A(N), B(N); std::cin >> M; std::vector<long long> C(M), D(M); for (int i = 0; i < N; ++i) { std::cin >> A[i] >> B[i]; A[i]--; B[i]--; } for (int i = 0; i < M; ++i) { std::cin >> C[i] >> D[i]; C[i]--; D[i]--; } auto ans = solve(H, W, N, M, A, B, C, D); std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; using ull = unsigned long long; // #define int ll // #define DEBUG 42 inline void nprint(){} template <class Head, class... Tail> inline void nprint(Head &&head, Tail &&... tail) { cout << head << endl; nprint(move(tail)...); } #ifdef DEBUG #define eprint(...) nprint(__VA_ARGS__) #else #define eprint(...) if(0==1) cout << 1 << endl; #endif #define Yes(a) cout << (a ? "Yes" : "No") << endl #define YES(a) cout << (a ? "YES" : "NO") << endl #define POSSIBLE(a) cout << (a ? "POSSIBLE" : "IMPOSSIBLE") << endl using cmp = complex<double>; using vb = vector<bool>; using vvb = vector<vb>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; template<class T> using V = vector<T>; template<class T> using VV = vector<V<T>>; #define fi first #define se second template<class T>inline bool maxs(T& x, T y){ if(x >= y) return false; x = y; return true; } template<class T>inline bool mins(T& x, T y){ if(x <= y) return false; x = y; return true; } using pii = pair<int,int>; using pll = pair<ll,ll>; #define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i) #define REP(i,n) FOR(i,0,n) #define FORS(i,a,b) FOR(i,a,b+1) #define REPS(i,n) REP(i,n+1) #define RFOR(i,a,b) for(ll i = (ll)(b)-1;i >= a;--i) #define RREP(i,n) RFOR(i,0,n) #define RREPS(i,n) RREP(i,n+1) #define RFORS(i,a,b) RFOR(i,a,b+1) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define PERM(c) sort(ALL(c)); for(bool cp = true;cp;cp = next_permutation(ALL(c))) #define eb(val) emplace_back(val) inline ll bitcnt(ll val) { return __builtin_popcountll(val); } const long double PI = std::acos(-1.0l); constexpr int dx[] = {1,0,-1,0}; constexpr int dy[] = {0,1,0,-1}; template<typename Tail> auto type_vector(Tail&& tail){return tail;} template<typename Head, typename... Tail> auto type_vector(Head&& head, Tail&&... tail){ head = 0; return vector<decltype(type_vector(tail...))>(head); } template<typename Tail> auto make_vector(Tail&& tail){return tail;} template<typename Head, typename... Tail> auto make_vector(Head&& head, Tail&&... tail){ return vector<decltype(type_vector(tail...))>(head, make_vector(tail...)); } template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p){ return s << "(" << p.first << ", " << p.second << ")"; } template<class T> istream& operator>>(istream &is,vector<T> &st){ for(size_t i=0;i<st.size();++i){is >> st[i];} return is; } template<class T> istream& operator>>(istream &is,vector<vector<T>> &st){ for(size_t i=0;i<st.size();++i){is >> st[i];} return is; } template<class T> ostream& operator<<(ostream &os, const vector<T> &st){ for(size_t i=0;i<st.size();++i) os << st[i] << (i!=st.size()-1?" ":""); return os; } ostream& operator<<(ostream &os, const vector<string> &st){ for(size_t i=0;i<st.size();++i) os << st[i] << (i!=st.size()-1?"\n":""); return os; } template<class T> ostream& operator<<(ostream &os, const vector<vector<T>> &st){ for(size_t i=0;i<st.size();++i) os << st[i] << (i!=st.size()-1?"\n":""); return os; } constexpr double EPS = 1e-10; constexpr ll MOD = 1E9+7; // constexpr ll MOD = 998244353; signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int n; cin >> n; vi a(n); cin >> a; REP(i,n){ a[i] %= 200; } map<int, vi> mp; int cnt = min(8,n); FOR(i,1,1<<cnt){ vi b; int sum = 0; REP(j,cnt){ if(i>>j&1){ sum += a[j]; b.eb(j+1); } } sum %= 200; if(mp.count(sum)){ Yes(1); cout << mp[sum].size() << " " << mp[sum] << endl; cout << b.size() << " " << b << endl; return 0; } mp[sum] = b; } Yes(0); }
#include <bits/stdc++.h> using namespace std; using namespace chrono; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif int main() { const auto start = steady_clock::now(); random_device rnd; int n, m; cin >> n >> m; vector<string> ss(m); for (auto &&s : ss) { cin >> s; } set<string> ts(ss.begin(), ss.end()); ss = vector<string>(ts.begin(), ts.end()); m = ss.size(); vector<string> ans(n, string(n, '.')); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans[i][j] = "ABCDEFGH"[rnd() % 8]; } } for (auto &&x : ans) { cout << x << 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; template <typename T> using vt = vector<T>; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define pb push_back #define SZ(x) ((int)((x).size())) #define EACH(x, a) for (auto& x: a) #define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i) #define FORD(i,s,l) for (ll i = (s); (i) >= l; --i) #define F first #define S second #define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1) #define TEST(x,i) ((x)&(1ll<<(i))) #define SET(x,i) ((x)|(1ll<<(i))) #define FLIP(x,i) ((x)^(1ll<<(i))) #define CLEAR(x,i) ((x)&~(1ll<<(i))) const double pi = 4 * atan(1); using ll = long long; using pll = pair<ll, ll>; mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count()); ll randint(ll a, ll b) { return uniform_int_distribution<ll>(a, b)(mt_rng); } template<class T> bool umin(T& a, const T& b) { return b<a?a=b, 1:0; } template<class T> bool umax(T& a, const T& b) { return a<b?a=b, 1:0; } template<class ForwardIterator> void print_vec(ForwardIterator first, ForwardIterator last, string sep = " ", string end = "\n") { bool ft = true; while (first != last) { if (!ft) {cout << sep;} else {ft = false;} cout << (*first); ++first; } cout << end; } template<typename T1, typename T2> std::ostream& operator<<(std::ostream &o, const pair<T1, T2> &p) { return o << p.F << " " << p.S; } inline ll floorDiv(ll x, ll y) { ll d = x / y; ll r = x % y; return r ? (d - ((x < 0) ^ (y < 0))) : d; } ll ceilDiv(ll x, ll y) { return -floorDiv(-x, y); } ll bin_search(ll lo, ll hi, function<bool(ll)> predicate) { int sign = lo < hi ? 1 : -1; lo *= sign; hi *= sign; hi++; while (lo < hi) { ll mid = lo + floorDiv(hi-lo, 2); if (!predicate(sign * mid)) { lo = mid + 1; } else { hi = mid; } } return sign * lo; } const ll MOD = 1000000007; const int MAXN = 200005; vector<ll> down; vector<vector<int>> adj; set<pair<int,int> > usual; map<pair<int,int>, int > ids; vector<ll> ans; ll current = 0; void dfs(int u, int p) { for (auto &v : adj[u]) { if (v != p) { usual.insert({u,v}); dfs(v,u); } } } void dfs2(int u, int p) { ans[u] = current; for (auto &v : adj[u]) { if (v != p) { int idx = ids[make_pair(u,v)]; current+=down[idx]; dfs2(v, u); current-=down[idx]; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; down.assign(n-1,0); ans.assign(n,0); vector<pair<int,int>> edge; adj.assign(n,vector<int>()); for (int i = 0; i < n-1; i++) { int x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); edge.pb({x,y}); ids[make_pair(x,y)]=ids[make_pair(y,x)]=i; } dfs(0,0); int q; cin >> q; while(q--) { int t, e; ll x; cin >> t >> e >> x; t--; t=1-t; e--; if (! usual.count(edge[e])) { t=1-t; } if (t) { current+=x; down[e]+=-x; } else { down[e]+=x; } } dfs2(0,0); print_vec(ans.begin(),ans.end(), "\n"); }
#include"bits/stdc++.h" using namespace std; typedef long long ll; #define int ll signed main(){ int n; cin>>n; vector<pair<int,int>> ed(n-1); vector<vector<int>> g(n); for(int i=0;i<n-1;i++){ int u,v; cin>>u>>v; ed[i]={u-1,v-1}; g[u-1].push_back(v-1); g[v-1].push_back(u-1); } vector<int> d(n,-1),s(n); //dfs using stack vector<int> q={0};d[0]=0; while(q.size()){ int u=q.back(); q.pop_back(); for(int v:g[u]){ if(d[v]==-1){ d[v]=d[u]+1; q.push_back(v); } } } int Q;cin>>Q; while(Q--){ int t,e,x;cin>>t>>e>>x; auto [a,b]=ed[e-1]; if(d[a]>d[b]){ swap(a,b); if(t==1)t=2; else t=1; } if(t==1){ s[0]+=x;s[b]-=x; } else s[b]+=x; } q={0}; while(q.size()){ int u=q.back(); q.pop_back(); for(int v:g[u]){ if(d[u]<d[v]){ s[v]+=s[u]; q.push_back(v); } } } for(int i:s)cout<<i<<"\n"; }
/* {{{ */ #include <bits/stdc++.h> using namespace std; #ifdef __LOCAL #include <prettyprint.hpp> #define debug(a) cerr << "[\033[33mDEBUG\033[m] " << (#a) << ": " << (a) << endl #else #define debug(s) cerr << "[\033[33mDEBUG\033[m] local only" << endl #endif using ll = long long; template <class T> using vec = vector<T>; #define endl "\n" #define INF (int)1e9 #define LINF (long long)1e12 #define MOD (int)1e9+7 #define _rep(i,_a,_b,a,b,...) for (int i=(a), lim##i=(b); i<lim##i; i++) #define rep(i, ...) _rep(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define _rrep(i,_a,_b,a,b,...) for (int i=(a), lim##i=(b); i>=lim##i; i--) #define rrep(i, ...) _rrep(i, __VA_ARGS__, __VA_ARGS__, __VA_ARGS__, 0) #define each(e, c) for (auto &e: c) #define all(v) begin(v), end(v) #define popcount __builtin_popcount template <class T> bool chmin(T& a, const T& b) { return a>b?a=b,true:false; } template <class T> bool chmax(T& a, const T& b) { return a<b?a=b,true:false; } void keep_my_fingers_crossed() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); } /* }}} */ int main() { keep_my_fingers_crossed(); ll n, w; cin >> n >> w; vec<pair<ll, ll>> a; rep(i, n) { ll s, t, p; cin >> s >> t >> p; a.push_back({ s, p }); a.push_back({ t, -p }); } sort(all(a), [&](auto x, auto y){ return x.first < y.first; }); map<ll, ll> usage; ll total = 0; for (auto [t, p]: a) { total += p; usage[t] = total; } for (auto [key, val]: usage) { if (val > w) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int n, w; cin>>n>>w; map<int, vector<int>> starts; map<int, vector<int>> ends; vector<int> ps (n); for(int i = 0; i < n; i++){ int s, t, p; cin>>s>>t>>p; starts[s].push_back(i); ends[t].push_back(i); ps[i] = p; } long long draw = 0; bool bad = 0; for(int i = 0; i <= 2e5; i++){ if(starts.count(i)){ for(int idx : starts[i]){ draw += ps[idx]; } } if(ends.count(i)){ for(int idx : ends[i]){ draw -= ps[idx]; } } if(draw > w){ bad = 1; break; } } cout<<(bad ? "No" : "Yes")<<endl; }
/* In The Name Of ALLAH */ #include <bits/stdc++.h> // This will work only for g++ compiler. #define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing #define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive #define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based. #define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based //short hand for usual tokens #define pb push_back #define fi first #define se second // to be used with algorithms that processes a container Eg: find(all(c),42) #define all(x) (x).begin(), (x).end() //Forward traversal #define rall(x) (x).rbegin, (x).rend() //reverse traversal // traversal function to avoid long template definition. Now with C++11 auto alleviates the pain. #define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++) // find if a given value is present in a container. Container version. Runs in log(n) for set and map #define present(c,x) ((c).find(x) != (c).end()) //find version works for all containers. This is present in std namespace. #define cpresent(c,x) (find(all(c),x) != (c).end()) // Avoiding wrap around of size()-1 where size is a unsigned int. #define sz(a) int((a).size()) #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl '\n' using namespace std; // Shorthand for commonly used types typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef double ld; int main() { optimize(); ll n,k; cin>>n>>k; ll op=0; while(n&&op!=k) { ll z=n; vi v; while(z) { int rem=z%10; v.pb(rem); z/=10; } sort(all(v)); ll ans1=0; for(int i=0;i<v.size();i++) { ans1=(ans1*10)+v[i]; } ll ans2=0; for(int i=v.size()-1;i>=0;i--) { ans2=(ans2*10)+v[i]; } n=ans2-ans1; ++op; } cout<<n<<endl; return 0; }
//----AUTHOR:kkdrummer----/ #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //#include <boost/multiprecision/cpp_int.hpp> //using namespace boost::multiprecision; using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef double ld; typedef unordered_set<ll> usll; typedef unordered_multiset<ll> umsll; typedef multiset<ll> msll; typedef set<ll> sll; typedef vector<ll> vll; typedef pair<ll,ll> pll; typedef vector<pll> vpll; typedef priority_queue<ll> pqll; typedef vector<int> vi; typedef set<int> si; typedef multiset<int> msi; typedef unordered_multiset<int> umsi; typedef unordered_set<int> usi; typedef pair<int,int> pi; typedef vector<pi> vpi; typedef priority_queue<int> pqi; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_sll; typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi; typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_msll; #define in insert #define fi first #define se second #define pb push_back #define mp make_pair #define be begin #define en end #define itr iterator #define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define mo 1000000007 #define inf 8222372026854775807 #define ninf -inf #define ima 2047483647 #define imi -ima #define oncnt __builtin_popcount #define zerobegin __builtin_clz #define zeroend __builtin_ctz #define parity __builtin_parity #define eps 1e-9 #define coutd cout<<setprecision(10)<<fixed #define mems(dp,x) memset(dp,x,sizeof(dp)) #define fbo find_by_order #define ook order_of_key #define all(x) x.be(),x.en() #define upb upper_bound #define lowb lower_bound #define lte(v,x) (upb(all(v),x)-v.be()) #define gte(v,x) (v.end()-lowb(all(v),x)) #define gt(v,x) (v.en()-upb(all(v),x)) #define lt(v,x) (lowb(all(v),x)-v.be()) const ld PI= 3.1415926535897932384626433832792884197169399375105820974944; inline ll mpow(ll x,ll n){if(n==0)return 1;if(x==0)return 0;if(n==1)return(x%mo);ll u=(mpow(x,n/2));u=(u*u)%mo;if(n%2!=0)u=(u*x%mo)%mo;return u;} inline ll minv(ll x){return mpow(x,mo-2);} inline ll mmul(ll a,ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);} inline ll madd(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a+b>=mo)return(a+b)%mo;return(a+b);} inline ll msub(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;return(((a-b)%mo+mo)%mo);} inline ll mdiv(ll a,ll bb){if(a>=mo)a=a%mo;ll b=minv(bb);if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);} inline ll gcd(ll a,ll b){return __gcd(a,b);} inline ll lcm(ll a,ll b){return (a*b)/gcd(a,b);} int main() { io int testcases=1; // cin>>testcases; while(testcases--) { int a,b; cin>>a>>b; int c=a+b; if(c>=15&&b>=8)cout<<1; else if(c>=10&&b>=3)cout<<2; else if(c>=3)cout<<3; else cout<<4; }return 0;}
#pragma GCC optimize("Ofast") //#ifndef ONLINE_JUDGE //#define _GLIBCXX_DEBUG //#endif #ifdef ONLINE_JUDGE #include <atcoder/all> #endif #include <bits/stdc++.h> #include <chrono> #include <random> #include <math.h> #include <complex> using namespace std; #ifdef ONLINE_JUDGE using namespace atcoder; #endif #define rep(i,n) for (int i = 0;i < (int)(n);i++) using ll = long long; #ifdef ONLINE_JUDGE //using mint = modint998244353; //using mint = modint; using mint = modint1000000007; #endif const ll MOD=1000000007; //const ll MOD=998244353; const long long INF = 1LL << 60; const double pi=acos(-1.0); int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0}; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); //cout << fixed << setprecision(15); ll R,C; cin>>R>>C; vector<vector<ll>> A(R,vector<ll>(C-1)),B(R-1,vector<ll>(C)); rep(i,R) rep(j,C-1) cin>>A[i][j]; rep(i,R-1) rep(j,C) cin>>B[i][j]; vector<vector<pair<ll,ll>>> graph(2*R*C,vector<pair<ll,ll>>(0)); auto f=[&](int i,int j,int k) {return i*C+j+k*R*C;}; rep(r,R) rep(c,C-1) graph[f(r,c,0)].push_back({f(r,c+1,0),A[r][c]}); rep(r,R) for(int c=1;c<C;c++) graph[f(r,c,0)].push_back({f(r,c-1,0),A[r][c-1]}); rep(r,R-1) rep(c,C) graph[f(r,c,0)].push_back({f(r+1,c,0),B[r][c]}); for(int r=1;r<R;r++) rep(c,C) { graph[f(r,c,0)].push_back({f(r,c,1),1}); graph[f(r,c,1)].push_back({f(r-1,c,1),1}); graph[f(r-1,c,1)].push_back({f(r-1,c,0),0}); } vector<ll> dist(2*R*C,INF); priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq; pq.push({0,0}); while(!pq.empty()) { auto [c,v]=pq.top(); pq.pop(); if(!chmin(dist[v],c)) continue; if(v==f(R-1,C-1,0)) break; for(auto &[nv,nc]:graph[v]) { pq.push({c+nc,nv}); } } cout<<dist[f(R-1,C-1,0)]<<'\n'; return 0; }
#include<bits/stdc++.h> #define ll long long #define pb push_back #define endl '\n' #define pii pair<ll int,ll int> #define vi vector<ll int> #define vvi vector<vi> #define all(a) (a).begin(),(a).end() #define F first #define S second #define sz(x) (ll int)x.size() #define hell 1000000007 #define rep(i,a,b) for(ll int i=a;i<b;i++) #define lbnd lower_bound #define ubnd upper_bound #define bs binary_search #define mp make_pair #define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) using namespace std; ll mod=1e9+7; #define N 100005 int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS=1; //cin>>TESTS; //ll cs=1; while(TESTS--) { char s,t; cin >> s >> t; if(s=='Y') cout << (char)toupper(t) << "\n"; else cout << t << "\n"; } return 0; }
#include "bits/stdc++.h" #include<sstream> using namespace std; typedef long long ll; #define _USE_MATH_DEFINES #include <math.h> #define NIL = -1; #define all(x) x.begin(),x.end() const ll INF = 1e9; const long long inf = 1e18; const ll INFL = 1e18; const ll MOD = 1e9 + 7; int digit(ll x) { int digits = 0; while(x > 0){ x /= 10; digits++; } return digits; } ll gcd(long long a,long long b) { if (a < b) swap(a,b); if (b == 0) return a; return gcd(b,a%b); } bool is_prime(long long N){ if (N == 1) return false; for (long long i = 2;i * i <= N;i++){ if (N % i == 0) return false; } return true; } ll lcm(ll a,ll b){ return ((a * b == 0)) ? 0 : (a / gcd(a,b) * b); } double DegreeToRadian(double degree){ return degree * M_PI / 180.0; } long long modpow(long long a, long long b, long long m){ long long ans = 1; while(b > 0){ if (b % 2 == 1){ ans *= a; ans %= m; } a *= a; a %= m; b /= 2; } return ans; } long long comb(long long x, long long y){ int z; if (y == 0) return 1; else { z = modpow(x, y/2, MOD)*modpow(x, y/2, MOD)%MOD; if (y % 2 == 1) z = z*x%MOD; return z; } } vector<pair<long long, long long>> prime_fact(long long x){ vector<pair<long long, long long>> res; for(int i = 2;i*i <= x;i++){ if (x % i == 0){ long long cnt = 0; while(x % i == 0){ cnt++; x /= i; } res.push_back({i, cnt}); } } if (x != 1){ res.push_back({x, 1}); } return res; } int64_t mod_inv(int64_t a, int64_t m){ int64_t b = m, u = 1, v = 0; while(b){ int64_t t = a/b; a -= t*b; swap(a, b); u -= t*v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } // {g, x, y}: ax + by = g tuple<long long, long long, long long> extgcd(long long a, long long b){ if (b == 0) return {a, 1, 0}; long long g, x, y; tie(g, x, y) = extgcd(b, a % b); return {g, y, x - a/b*y}; } int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; /////////////////////////////////////////////////////////////// int main(){ long long N, C; cin >> N >> C; vector<pair<long long, long long>> p; for(int i = 0;i < N;i++){ long long a, b, c; cin >> a >> b >> c; p.push_back({a - 1, c}); p.push_back({b, -c}); } sort(p.begin(), p.end()); long long ans = 0, cost = 0, t = 0; for(auto [x, y] : p){ if (t != x){ ans += min(C, cost)*(x - t); t = x; } cost += y; } cout << ans << endl; }
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define ll long long #define ALL(a) (a).begin(),(a).end() #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 Pii pair<int,int> #define Pll pair<long long,long long> #define fout(num) cout << fixed << setprecision(20) << (num) << endl 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;} //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))); using namespace std; signed main(){ std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); int n; cin >> n; ll x; cin >> x; map<ll,ll> mp; vector<Pll> v; rep(i,n){ int a,b; cin >> a >> b; ll c; cin >> c; mp[a]+=c; mp[b+1]-=c; v.pb({a,c}); v.pb({b+1,-c}); } sort(ALL(v)); ll now = 1; ll ans = 0; ll kane = 0; for(auto itr=mp.begin();itr!=mp.end();itr++){ ll d = itr->fi; ans += (d-now)*min(x,kane); kane += itr->se; now = d; } cout << ans << endl; return 0; } // g++ main.cpp -o a.out && ./a.out
#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); } int n; vector<vi> a; bool check(int val){ vector<bool> mark(32,false),mark2(32,false); set<int> ss; int num,num2; fr(i,0,n){ num=num2=0; fr(j,0,5){ if(a[i][j]>=val) num|=(1<<j); else num2|=(1<<j); } if(mark[num2]){ return true; } for(auto x: ss){ num2=x|num; if(mark2[num2]) continue; mark2[num2]=true; for(int j=num2;;j=(j-1)&num2){ mark[num2&j]=true; if(j==0) break; } } ss.insert(num); } return false; } void solve(){ cin>>n; a=vector<vi>(n,vi(5,0)); vi diff; fr(i,0,n){ fr(j,0,5){ cin>>a[i][j]; diff.pb(a[i][j]); } } sort(all(diff)); diff.erase(unique(all(diff)),diff.end()); int l=0,r=int(diff.size())-1,m; int ans=0; while(l<=r){ m=(l+r)/2; if(check(diff[m])){ ans=diff[m]; l=m+1; } else{ r=m-1; } } cout<<ans<<endl; return; } int main() { boost(); int tc=1; //cin>>tc; while(tc--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> p2; #define sz(a) ll(a.size()) void test2311(){ string s; cin>>s; reverse(s.begin(),s.end()); for(ll i=0;i<sz(s);i++) if(s[i]=='6') s[i]='9'; else if(s[i]=='9') s[i]='6'; cout<<s; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); clock_t z=clock(); #ifndef ONLINE_JUDGE freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif ll qc=1; //cin>>qc; for(ll i=1;i<=qc;i++){ test2311(); } debug("Total Time:%.4Lf\n",(ld)(clock()-z)/CLOCKS_PER_SEC); }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; #define MP make_pair #define PB push_back #define X first #define Y second #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) #define ALL(a) a.begin(), a.end() #define SZ(a) (int)((a).size()) #define FILL(a, value) memset(a, value, sizeof(a)) #define debug(a) cerr << #a << " = " << a << endl; template<typename T> void setmax(T& x, T y) {x = max(x, y);} template<typename T> void setmin(T& x, T y) {x = min(x, y);} const double PI = acos(-1.0); const int INF = 1e9 + 47; const LL LINF = 1LL * INF * INF; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int bad[1<< 18]; double K[1 << 18], B[1 << 18]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m, k; cin >> n >> m >> k; FOR(i, 0, k) { int x; cin >> x; bad[x] = 1; } int cnt = 0; FOR(i, 0, n + 1) { if(bad[i]) cnt++; else { if(cnt >= m) { cout << "-1" << endl; return 0; } cnt = 0; } } FOR(i, 0, m) { K[n + i] = 0; B[n + i] = 0; } double M = m; double sumK = 0, sumB = 0; RFOR(i, n, 0) { if(bad[i]) { K[i] = 1; B[i] = 0; } else { K[i] = sumK / M; B[i] = 1 + (sumB / M); } sumK += K[i] - K[i + m]; sumB += B[i] - B[i + m]; //cout << K[i] << " " << B[i] << endl; } cout << fixed << setprecision(8) << B[0] / (1.0 - K[0]) << endl; cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; }
#include <stdio.h> #define _USE_MATH_DEFINES #include <math.h> #include <vector> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> using namespace std; int main(){ string str; cin >> str; long nLen = str.length(); long m; cin >> m; if(nLen == 1){ long nAns = long(str[0] - '0'); if(nAns <= m){ cout << "1" << endl; } else { cout << "0" << endl; } return 0; } long d = 0; vector<long> viNum(nLen); for(long i = 0; i < nLen; i++){ viNum[i] = long(str[i]-'0'); d = max(d, viNum[i]+1); } long iOK = 0, iNG = m+1; while(iNG - iOK > 1){ long iThis = (iNG + iOK)/2; long nSum = viNum[0]; bool b = true; for(long i = 1; i < nLen; i++){ if(m / iThis < nSum){ b = false; break; } nSum *= iThis; nSum += viNum[i]; } if(b && nSum <= m){ iOK = iThis; } else { iNG = iThis; } } cout << max(iOK-d+1, 0L) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define Int register int #define mod 998244353 #define MAXN 105 template <typename T> void read (T &x){char c = getchar ();x = 0;int f = 1;while (c < '0' || c > '9') f = (c == '-' ? -1 : 1),c = getchar ();while (c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar ();x *= f;} template <typename T,typename ... Args> void read (T &x,Args& ... args){read (x),read (args...);} template <typename T> void write (T x){if (x < 0) x = -x,putchar ('-');if (x > 9) write (x / 10);putchar (x % 10 + '0');} template <typename T> void chkmax (T &a,T b){a = max (a,b);} template <typename T> void chkmin (T &a,T b){a = min (a,b);} int mul (int a,int b){return 1ll * a * b % mod;} int dec (int a,int b){return a >= b ? a - b : a + mod - b;} int add (int a,int b){return a + b >= mod ? a + b - mod : a + b;} void Add (int &a,int b){a = add (a,b);} int n,W[MAXN],fac[MAXN],f[MAXN][MAXN][MAXN * MAXN / 2]; signed main(){ read (n);int up = 0; for (Int i = 1;i <= n;++ i) read (W[i]),up += W[i]; if (up & 1) return puts ("0") & 0; f[0][0][0] = fac[0] = 1,up >>= 1; for (Int i = 1;i <= n;++ i) fac[i] = mul (fac[i - 1],i); for (Int i = 0;i < n;++ i) for (Int s = 0;s <= i;++ s) for (Int w = 0;w <= up;++ w){ Add (f[i + 1][s][w],f[i][s][w]); if (w + W[i + 1] <= up) Add (f[i + 1][s + 1][w + W[i + 1]],f[i][s][w]); } int ans = 0; for (Int i = 0;i <= n;++ i) ans = add (ans,mul (mul (fac[i],fac[n - i]),f[n][i][up])); write (ans),putchar ('\n'); return 0; }
#include "bits/stdc++.h" using namespace std; #ifdef Dragon #include"debug.cpp" #else #define deb // #define pr // #endif #define int long long #define lld long double #define pb push_back #define vi vector<int> #define si(x) (int)(x.size()) #define all(c) (c).begin(), (c).end() #define uniq(x) (x).erase(unique(all(x)),(x).end()) #define pii pair<int,int> #define ss second #define ff first #define FOR(x,a) for(auto&x :a) #define rep(i,a,b) for(int i=a;i<b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define pnr(a) FOR(i,a){cout<<i<<" ";}cout<<"\n"; #define endl '\n' const long long inf = 1e18; const int32_t mod = 1000000007; const int N = 3e5 + 5; int solve() { int a , b , k; cin >> a >> b >> k; int bb = b; int aa = a; vector<int> v(bb , 0); k--; rep(i , 0 , bb) { int cnt = 0; int cur = 1; int sum = 1; int moved = 0; while (k >= sum) { if (moved == a) break; moved++; cur *= (b + cnt); cur /= (cnt + 1); if (cur + sum > k) break; sum += cur; cnt++; } // deb(sum); a = moved; // deb(moved); v[i] = moved; k -= sum; b--; } // pnr(v); vector<int> ans( (aa + bb) , 0); int cnt = 0; rep(i , 0 , bb) { ans[aa - v[i] + cnt] = 1; cnt++; } // pnr(ans); FOR(i , ans) { cout << char('a' + i) << ""; } cout << endl; return 0; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(20); #ifdef Dragon freopen("in.txt", "r", stdin); auto start1 = chrono::high_resolution_clock::now(); //freopen("out.txt","w",stdout); #endif int tests = 1; //cin>>tests; for (int i = 1 ; i <= tests ; i++) { solve(); } return 0 ; }
//*******Abhijit Burman***********// //Jalpaiguri Government Engineering College// // Taken From Striver_79 // Never open a ranklist untill and unless you are done with solving problems, wastes 3/4 minuts // Donot treat CP as a placement thing, love it and enjoy it, you will succeed for sure. #include<bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define ff first #define ss second #define mk make_pair #define MAXX (1000000000000000000+7) #define mod 1000000007 #define endl "\n" #define PI 3.14159265358979323846 #define all(x) (x).begin(),(x).end() #define fio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) void printv(vector<ll> v) { ll sz = v.size(); for(ll i=0;i<sz;i++) {if (i == sz - 1) cout << v[i] << "\n"; else cout << v[i] << " ";}} void input(ll a[], ll sz) {for(ll i=0;i<sz;i++) cin >> a[i];} void print(ll a[], ll sz) {for(ll i=0;i<sz;i++) {if (i == sz - 1) cout << a[i] << "\n"; else cout << a[i] << " ";}} ll dx1[] = {1, 1, 0, -1, -1, -1, 0, 1}; ll dy1[] = {0, 1, 1, 1, 0, -1, -1, -1}; ll dx2[] = {1, 0, -1, 0}; ll dy2[] = {0, 1, 0, -1}; ll mul(ll a, ll b, ll p = mod) {return ((a % p) * (b % p)) % p;} ll add(ll a, ll b, ll p = mod) {return (a % p + b % p) % p;} ll power(ll x,ll y,ll p = mod) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; // y = y/2 x = (x*x) % p; } return res; } ll modInverse(ll n, ll p = mod) { return power(n, p - 2, p); } ll ncrMod(ll n, ll r, ll p = mod) { if (r == 0) return 1; ll fac[n + 1]; fac[0] = 1; for (ll i = 1 ; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } // a+b=xor(a,b)+2*(a&b)// ll convert(string num){ ll sum=0; for(int i=0;i<num.size();i++){ sum=sum*10; sum+=(num[i]-'0'); } return sum; } bool ispeed2ime(ll n){ for(int i=2;i*i<=n;i++){ if(n%i==0){ return false; } } return true; } bool isPowerof2(ll n){ while(n>1){ if(n%2!=0){ return true; } n/=2; } return false; } void solve() { ll n; cin>>n; ll a[n]; input(a,n); ll ans=INT_MIN; for(int i=0;i<n;i++){ ll mini = a[i]; for(int j=i;j<n;j++){ mini = min(mini,a[j]); ans = max(ans,mini*(j-i+1)); } } cout<<ans<<endl; } int main() { fio; // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("input.txt", "r", stdin); // // for writing output to output.txt // freopen("output.txt", "w", stdout); // #endif ll t=1; // cin>>t; while(t--) { solve(); } return 0; }
#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <math.h> #include <stdio.h> #include <string.h> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <functional> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <bitset> #include <complex> using namespace std; //#pragma GCC optimize("Ofast") //#pragma GCC optimization("unroll-loops, no-stack-protector") //#pragma GCC target("avx,avx2,fma") int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, i, j, k, c; string s; cin >> n >> s; vector<int> a(n + 1); m = 1E9; for (i = 0; i <= n; i++) { cin >> a[i]; if (i > 0) m = min(m, abs(a[i] - a[i - 1])); } cout << m << "\n"; for (i = 0; i < m; i++) { for (j = 0; j <= n; j++) cout << (a[j] + i) / m << " "; cout << "\n"; } return 0; }
#include <bits/stdc++.h> // 1. dp typedef long double ld; #define int long long #define gcd __gcd #define endl "\n" #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define mod2 998244353 #define maxe *max_element #define mine *min_element #define inf 1e18 #define pb push_back #define all(x) x.begin(), x.end() #define f first #define s second #define lb lower_bound #define ub upper_bound #define ins insert #define sz(x) (int)(x).size() #define mk make_pair #define deci(x, y) fixed<<setprecision(y)<<x #define w(t) int t; cin>>t; while(t--) #define nitin ios_base::sync_with_stdio(false); cin.tie(nullptr) #define PI 3.141592653589793238 using namespace std; void solve() { int s,p; cin>>s>>p; bool flag=false; for(int i=1;i*i<=p;i++) { if(p%i==0) { int j=p/i; if(i+j==s) flag=true; } } if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } int32_t main() { nitin; solve(); }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) int main() { long long s,p; cin>>s>>p; string ans="No"; for(ll i=1;i<=1000000;i++){ if(p%i==0){ ll x=p/i; if(x+i==s) ans="Yes"; } } cout<<ans<<endl; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { int n; cin >> n; string s, t; cin >> s >> t; vector<int> a, b; REP(i, n) { if (s[i] == '0') a.push_back(i); if (t[i] == '0') b.push_back(i); } int res = 0; if ((int)a.size() != (int)b.size()) res = -1; else REP(i, (int)a.size()) if (a[i] != b[i]) res++; cout << res << '\n'; return 0; }
#line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp" #define _CRT_SECURE_NO_WARNINGS #pragma target("avx2") #pragma optimize("O3") #pragma optimize("unroll-loops") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <string.h> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define rep(i,n) for(int i=0;i<(n);i++) #define REP(i,n) for(int i=1;i<=(n);i++) #define all(V) V.begin(),V.end() typedef unsigned int uint; typedef long long lint; typedef unsigned long long ulint; typedef std::pair<int, int> P; typedef std::pair<lint, lint> LP; constexpr int INF = INT_MAX/2; constexpr lint LINF = LLONG_MAX/2; constexpr double eps = DBL_EPSILON; constexpr double PI=3.141592653589793238462643383279; template<class T> class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {}; template <class T, class U> inline bool chmax(T& lhs, const U& rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T& lhs, const U& rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } inline lint gcd(lint a, lint b) { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; } bool isprime(lint n) { if (n == 1)return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0)return false; } return true; } template<typename T> T mypow(T a, lint b) { T res(1); while(b){ if(b&1)res*=a; a*=a; b>>=1; } return res; } lint modpow(lint a, lint b, lint m) { lint res(1); while(b){ if(b&1){ res*=a;res%=m; } a*=a;a%=m; b>>=1; } return res; } template<typename T> void printArray(std::vector<T>& vec) { rep(i, vec.size()){ std::cout << vec[i]; std::cout<<(i==(int)vec.size()-1?"\n":" "); } } template<typename T> void printArray(T l, T r) { T rprev = std::prev(r); for (T i = l; i != rprev; i++) { std::cout << *i << " "; } std::cout << *rprev << std::endl; } LP extGcd(lint a,lint b) { if(b==0)return {1,0}; LP s=extGcd(b,a%b); std::swap(s.first,s.second); s.second-=a/b*s.first; return s; } LP ChineseRem(const lint& b1,const lint& m1,const lint& b2,const lint& m2) { lint p=extGcd(m1,m2).first; lint tmp=(b2-b1)*p%m2; lint r=(b1+m1*tmp+m1*m2)%(m1*m2); return std::make_pair(r,m1*m2); } template<typename F> inline constexpr decltype(auto) lambda_fix(F&& f){ return [f=std::forward<F>(f)](auto&&... args){ return f(f,std::forward<decltype(args)>(args)...); }; } #line 2 "main.cpp" int N,M; std::vector<P> e[100010]; std::vector<int> vec[100010]; int par[100010],parc[100010],ans[100010]; bool used[100010]; int main(){ std::cin>>N>>M; rep(i,M){ int u,v,c; std::cin>>u>>v>>c; e[u].emplace_back(v,c); e[v].emplace_back(u,c); } lambda_fix([&](auto self, int node)->void{ used[node]=true; for(auto i:e[node]){ if(!used[i.first]){ self(self,i.first); vec[node].emplace_back(i.first); par[i.first]=node; parc[i.first]=i.second; } } })(1); lambda_fix([&](auto self,int node)->void{ if(node==1)ans[node]=1; else{ if(ans[par[node]]==parc[node]){ if(parc[node]!=1)ans[node]=1; else ans[node]=2; } else ans[node]=parc[node]; } for(auto i:vec[node]){ self(self,i); } })(1); REP(i,N)std::cout<<ans[i]<<std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define fr first #define sc second typedef long long ll; typedef pair <int,int> ii; int n; vector <char> s; vector <char> temp; vector <int> pre; int pt; int main() { for(int j=1;j<=1200000;j*=3) { pre.pb(j); } pt=pre.size()-1; scanf("%d ",&n); for(int i=0;i<n;i++) { s.pb(0); scanf("%c",&s[i]); } while(n>1) { while(n<pre[pt]+1) { pt--; } for(int i=0;i+pre[pt]<n;i++) { if(s[i]!=s[pre[pt]+i]) { temp.pb('W'+'B'+'R'-s[i]-s[pre[pt]+i]); } else { temp.pb(s[i]); } } s=temp; temp.clear(); n=s.size(); } printf("%c",s[0]); return 0; }
#include <iostream> #include<iomanip> #include <string> #include<vector> #include<algorithm> #include<utility> #include<queue> #include<math.h> #include<stack> #include<set> #include<map> #define INF long long int(1e18+1) #define rep(i,n,m) for(int i=n;i<m;i++) using namespace std; int main() { int n, m, k; cin >> n >> m >> k; long int total = 0; rep(i, 0, n - 1) { int x; cin >> x; total += x; } int ans; ans = k * n - total; if (ans < 0)cout << 0; else if (ans > m)cout << -1; else cout << ans; }
#ifdef MY_LOCAL #define MY_NAMESPACE(ns) namespace ns { #define MY_NAMESPACE_ } #define MY_DEBUG(s) s #define MY_IS_DEBUG (true) #else #define MY_NAMESPACE(ns) #define MY_NAMESPACE_ #define MY_DEBUG(s) #define MY_IS_DEBUG (false) #endif #include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; #define EPS (1e-7) #define PI (acos(-1)) constexpr int IMIN = numeric_limits<int>::min(); constexpr int IMAX = numeric_limits<int>::max(); constexpr ll LLMIN = numeric_limits<ll>::min(); constexpr ll LLMAX = numeric_limits<ll>::max(); MY_NAMESPACE(testbed) int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m, q; cin >> n >> m >> q; vector<pll> item(n); for (int i = 0; i < n; i++) { ll w, v; cin >> w >> v; item.push_back({ w, v }); } vector<ll> boxes(m); for (int i = 0; i < m; i++) { cin >> boxes[i]; } auto sort_fn = [](pll const& lhs, pll const& rhs)->bool { return lhs.second > rhs.second; }; sort(item.begin(), item.end(), sort_fn); for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; r--; vector<ll> this_boxes = boxes; this_boxes.erase(this_boxes.begin() + l, this_boxes.begin() + r + 1); auto sort_fn2 = [](ll const& lhs, ll const& rhs)->bool { return lhs < rhs; }; sort(this_boxes.begin(), this_boxes.end(), sort_fn2); ll ans = 0; for (int j = 0; j < n; j++) { auto it = lower_bound(this_boxes.begin(), this_boxes.end(), item[j].first); if (it != this_boxes.end()) { ans += item[j].second; this_boxes.erase(it); } } cout << ans << "\n"; } return 0; } MY_NAMESPACE_
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <math.h> #include <queue> #include <numeric> using namespace std; using ll = long long; #define For(i,a,b) for(ll i=(a);i<(ll)(b);i++) #define rFor(i,a,b) for(ll i=(a);i>=(ll)(b);i--) #define co(a) cout<<a<<endl #define SORT(a) sort((a).begin(),(a).end()) #define REV(a) reverse((a).begin(),(a).end()) #define cfs(a) cout<<fixed<<setprecision(a) int main() { ll N, M, Q; cin >> N >> M >> Q; ll W, V; vector<pair<ll,ll>> vw(N); For(i, 0, N) { cin >> W >> V; vw[i].first = V; vw[i].second = W; } vector<pair<ll,ll>> X(M); For(i, 0, M) { cin >> X[i].first; X[i].second = i; } SORT(vw); REV(vw); SORT(X); vector<bool> used(M); ll start, end; ll ans; For(i, 0, Q) { ans = 0; cin >> start >> end; For(j, 0, start - 1) { used[j] = true; } For(j, start - 1, end) { used[j] = false; } For(j, end, M) { used[j] = true; } For(j, 0, N) { For(k, 0, M) { if (used[X[k].second]) { if (vw[j].second <= X[k].first) { ans += vw[j].first; used[X[k].second] = false; break; } } } } co(ans); } }
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,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 double db; typedef unsigned ui; typedef long long ll; typedef long double ld; // char buf[1 << 20],*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; } template <class o, class... O> void qr(o& x, O&... y) { qr(x), qr(y...); } TP void qw(o x) { if (x < 0) putchar('-'), x = -x; if (x / 10) qw(x / 10); putchar(x % 10 + '0'); } TP void pr1(o x) { qw(x), putchar(' '); } template <class o, class... O> void pr1(o x, O... y) { pr1(x), pr1(y...); } TP void pr2(o x) { qw(x), putchar(10); } template <class o, class... O> void pr2(o x, O... y) { pr2(x), pr2(y...); } TP bool cmax(o& x, o y) { return (x < y ? x = y, 1 : 0); } TP bool cmin(o& x, o y) { return (x > y ? x = y, 1 : 0); } const int mod = (int)1e9 + 7; TPP void ad(t1& x, t2 y) { (x += y) >= mod && (x -= mod); } TPP void dl(t1& x, t2 y) { (x -= y) < 0 && (x += mod); } 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; } ll exgcd(ll a, ll b, ll& x, ll& y) { if (!a) { x = 0; y = 1; return b; } ll d = exgcd(b % a, a, y, x); x -= b / a * y; return d; } ll INV(ll n, ll p = mod) { ll x, y; exgcd(n, p, x, y); return (x % p + p) % p; } const int N = 110; const ll INF = 1e15; int n, m, k, val[N]; struct rec { int a[N][N]; rec() { memset(a, 0, sizeof a); } rec operator*(rec b) const { rec c; FOR(i, n) FOR(j, n) FOR(k, n) ad(c.a[i][k], 1ll * a[i][j] * b.a[j][k] % mod); return c; } } a, c; void solve() { qr(n, m, k); FOR(i, n) a.a[i][i] = c.a[i][i] = 1, qr(val[i]); ll inv = power(2 * m); while (m--) { int x, y; qr(x, y); dl(a.a[x][x], inv); ad(a.a[x][y], inv); dl(a.a[y][y], inv); ad(a.a[y][x], inv); } while (k) { if (k & 1) c = c * a; k /= 2; a = a * a; } FOR(i, n) { ll ans = 0; FOR(j, n) ad(ans, 1ll * c.a[j][i] * val[j] % mod); pr2(ans); } } int main() { #ifndef ONLINE_JUDGE clock_t start_time = clock(); #endif int T = 1; // qr(T); while (T--) solve(); #ifndef ONLINE_JUDGE cerr << 1.0 * (clock() - start_time) / CLOCKS_PER_SEC << ' ' << 's' << endl; #endif return 0; }
#include<bits/stdc++.h> using namespace std; inline int read(){ int res=0; bool zf=0; char c; while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-')zf=1; else res=c-'0'; while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0'; if(zf)return -res; return res; } const int P=1e9+7; inline int ksm(int x,int y=P-2){ int res=1; while(y){ if(y&1){ res=1ll*res*x%P; } x=1ll*x*x%P; y>>=1; } return res; } int a[105],f[105][105],res[105][105],tmp[105][105]; inline void calc(int a[][105],int b[][105],int n){ for(register int i=1;i<=n;++i){ for(register int j=1;j<=n;++j){ tmp[i][j]=0; for(register int k=1;k<=n;++k){ tmp[i][j]=(tmp[i][j]+1ll*a[i][k]*b[k][j])%P; } } } memcpy(a,tmp,sizeof(tmp)); return; } signed main(){ int n=read(),m=read(),k=read(),tmd=ksm(2*m%P); for(register int i=1;i<=n;++i){ a[i]=read(); } while(m--){ int x=read(),y=read(); f[x][y]=f[y][x]=tmd; } for(register int i=1;i<=n;++i){ f[i][i]=1; for(register int j=1;j<=n;++j){ if(j==i){ res[i][j]=1; continue; } f[i][i]-=f[j][i]; (f[i][i]<0)&&(f[i][i]+=P); } } while(k){ if(k&1){ calc(res,f,n); } calc(f,f,n); k>>=1; } for(register int i=1;i<=n;++i){ int A=0; for(register int j=1;j<=n;++j){ A=(A+1ll*a[j]*res[j][i])%P; } printf("%d\n",A); } return 0; }
/** * Author : pradumn **/ #include<bits/stdc++.h> using namespace std; #define Fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ll long long #define M 1000000007 #define fo(i,n) for(i=0;i<n;i++) #define pb push_back #define mp make_pair #define deb(c) cout<<c<<endl #define umap unordered_map #define pqueue priority_queue #define all(v) v.begin(),v.end() #define ft first #define sc second #define nl "\n" #define mxe *max_element #define mne *min_element #define gcd __gcd #define sz(x) (int)(x.size()) #define rev(x) reverse(all(x)) #define itr(it, a) for(auto it = a.begin(); it != a.end(); it++) typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int,int> pi; typedef vector<vi> vvi; typedef map<int,int> mi; void init_code() { Fast_io; #ifndef ONLINE_JUDGE freopen("Input.txt","r",stdin); freopen("Output.txt","w",stdout); freopen("error.txt","w",stderr); #endif } void func() { float n; cin>>n; n=n/100; cout<<ceil(n); } int main() { init_code(); double start1 = (double)clock(); // int t; // cin>>t; // while(t--) func(); double stop1 =(double)clock(); double duration = (stop1-start1)/CLOCKS_PER_SEC; #ifndef ONLINE_JUDGE cerr << "Time: " << duration << endl; #endif }
#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 n; cin >> n; vector<ll> a(n); ll sum = 0; rep(i, n) { cin >> a[i]; sum += a[i]; } ll ans = 0; for (int i = n-1; i >= 0; i--) { sum -=a[i]; ans += (n-1) * a[i] * a[i]; ans -= 2 * sum * a[i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } 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; } class UnionFind { private: int sz; vector<int> par, size_; public: UnionFind(){} UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){ iota(par.begin(), par.end(), 0); } int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x,int y){ x = find(x), y = find(y); if(x == y) return; if(size_[x] < size_[y]) swap(x,y); par[y] = x; size_[x] += size_[y]; } int size(int x){ x = find(x); return size_[x]; } bool same(int x,int y){ return find(x) == find(y); } }; int main(){ int n,m; cin >> n >> m; vector<pair<int,int> > p; rep(i,m){ int a,b; cin >> a >> b; a--;b--; p.push_back(MP(a,b)); } ll ans = 0; rep(bit,1<<n){ UnionFind uf(2*n); ll res = 1; rep(j,m){ int a = p[j].first; int b = p[j].second; if(((bit>>a)&1)&&((bit>>b)&1)){ res = 0; }else{ if(((bit>>a)&1)||((bit>>b)&1)){ }else{ uf.unite(a,b+n); uf.unite(b,a+n); } } } vector<bool> p(2*n); rep(j,n){ if((bit>>j)&1)continue; if(p[uf.find(j)]||p[uf.find(j+n)])continue; if(uf.same(j,j+n)){ res = 0; }else{ res *=2; } p[uf.find(j)] = 1; } ans += res; // cerr << bit << " " << res << endl; } cout << ans << endl; return 0; }
#include<iostream> #include<vector> using namespace std; int N,M; vector<int>G[20]; int col[20]; bool dfs(int u) { for(int v:G[u]) { if(col[v]==-1) { col[v]=3-col[u]; if(!dfs(v))return false; } else if(col[u]==col[v]) { return false; } } return true; } int main() { cin>>N>>M; vector<pair<int,int> >E(M); for(int i=0;i<M;i++) { int a,b;cin>>a>>b; a--,b--; E[i]=make_pair(a,b); G[a].push_back(b); G[b].push_back(a); } long ans=0; for(int k=0;k<1<<N;k++) { for(int i=0;i<N;i++) { col[i]=-1; if(k>>i&1)col[i]=0; } bool ok=true; for(pair<int,int>e:E)if(col[e.first]==0&&col[e.second]==0) { ok=false; break; } if(!ok)continue; int cnt=0; for(int i=0;i<N;i++)if(col[i]==-1) { col[i]=1; if(!dfs(i)) { ok=false; break; } cnt++; } if(ok)ans+=1<<cnt; } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; int n, q, c, par[200005]; int uf(int x){ if (par[x]==x) return x; int y = uf(par[x]); par[x]=y; return y; } map<int, int> raz[200005]; void merge(int x, int y){ if (raz[x].size() < raz[y].size()) swap(x, y); for (map<int, int>::iterator it = raz[y].begin(); it!=raz[y].end(); it++) if (raz[x].count(it->first)) raz[x][it->first]+=(it->second); else raz[x][it->first]=(it->second); par[y] = x; raz[y].clear(); } int main (){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for (int i=1; i<=n; i++){ par[i]=i; cin >> c; raz[i][c]=1; } while (q--){ int tip; cin >> tip; if (tip==1){ int a, b; cin >> a >> b; if (uf(a)!=uf(b)){ merge(par[a], par[b]); } } else{ int x, y; cin >> x >> y; int leader = uf(x); if (raz[leader].count(y)) cout << raz[leader][y] << endl; else cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; UnionFind() { } UnionFind(int n) : par(n, -1) { } void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y, int id = -1) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; using Query = pair<int,int>; // id, class int main() { int N, Q; cin >> N >> Q; vector<int> C(N); for (int i = 0; i < N; ++i) cin >> C[i], --C[i]; // build forest UnionFind uf(N); vector<int> left(N+Q, -1), right(N+Q, -1), ancestor(N+Q); iota(ancestor.begin(), ancestor.end(), 0); vector<vector<Query>> queries(N+Q); int mergeNum = N, queryNum = 0; for (int q = 0; q < Q; ++q) { int type, x, y; cin >> type >> x >> y; --x, --y; if (type == 1) { x = uf.root(x), y = uf.root(y); if (x == y) continue; uf.merge(x, y); // x ใŒ่ฆชใจใชใ‚‹ใ‚ˆใ†ใซ if (uf.root(x) != x) swap(x, y); left[mergeNum] = ancestor[x]; right[mergeNum] = ancestor[y]; ancestor[x] = mergeNum++; } else { x = uf.root(x); int v = ancestor[x]; queries[v].emplace_back(queryNum++, y); } } // dfs vector<int> res(queryNum, 0), counter(N, 0); auto dfs = [&](auto self, int v) -> void { for (auto q : queries[v]) res[q.first] -= counter[q.second]; if (v < N) counter[C[v]]++; // leaf else self(self, left[v]), self(self, right[v]); for (auto q : queries[v]) res[q.first] += counter[q.second]; }; for (int v = 0; v < N; ++v) { if (uf.root(v) != v) continue; dfs(dfs, ancestor[v]); } for (auto val : res) cout << val << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 5; int n, m, a, b, ans; bool vis[N]; vector<int> graph[N]; void dfs(int v){ ans++; vis[v] = true; for(auto i : graph[v]){ if(!vis[i]) dfs(i); } } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); cin >> n >> m; for(int i = 0; i < m; i++){ cin >> a >> b; graph[a].push_back(b); } for(int i = 1; i <= n; i++){ memset(vis, 0, sizeof(vis)); dfs(i); } cout << ans; }
#include <stdio.h> #include <string> #include <algorithm> #include <iostream> #include <vector> #include <cmath> using namespace std; #define rep(i,n) for(int i = 0; i < (n); i++) using ll = long long; using P = pair<int, int>; vector<vector<int>> to; vector<bool> visit; int sum; void dfs(int i){ if(visit[i]) return; visit[i] = true; sum++; for(int t: to[i]) dfs(t); return; } int main() { int n, m; cin >> n >> m; to = vector<vector<int>>(n); rep(i,m){ int a, b; cin >> a >> b; --a; --b; to[a].push_back(b); } // //toๅ…จไฝ“ใ‚’ๅ‡บๅŠ› // rep(i,n){ // cout << "to" << i << " "; // for(int t: to[i]) cout << t << " "; // cout << endl; // } sum = 0; rep(i,n){ visit = vector<bool>(n, false); dfs(i); } cout << sum << endl; }
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define maxn 505 #define ll long long #define mo 998244353 using namespace std; int n,m,i,j,k,a[maxn][maxn],c[3]; int main(){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ char ch=getchar(); while (ch!='R'&&ch!='B'&&ch!='.') ch=getchar(); for(j=1;j<=m;j++) a[i][j]=(ch=='.')?0:((ch=='R')?1:2),ch=getchar(); } ll ans=1; for(i=2;i<=n+m;i++){ c[0]=c[1]=c[2]=0; for(j=1;j<i;j++) if (j>=1&&j<=n&&i-j>=1&&i-j<=m) c[a[j][i-j]]++; if (c[1]&&c[2]) printf("0\n"),exit(0); if (!c[1]&&!c[2]) ans=ans*2%mo; } printf("%lld\n",ans); }
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools #include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const long long MOD = 1000000007; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template <typename T> bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } // doc: https://shibh308.github.io/library/library/lib/classes/modint.cpp.html template <i64 mod = MOD> struct ModInt{ i64 p; ModInt() : p(0){} ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;} ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;} ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;} ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;} ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;} ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;} ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;} ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;} ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;} friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){ stream << *x; return stream; } friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){ stream >> *x; return stream; } ModInt& operator++(){p = (p + 1) % mod; return *this;} ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;} bool operator==(const ModInt& y) const{return p == *y;} bool operator!=(const ModInt& y) const{return p != *y;} const i64& operator*() const{return p;} i64& operator*(){return p;} }; using mint = ModInt<>; void solve(long long n, long long p){ // doc: https://shibh308.github.io/library/library/lib/functions/modpow.cpp.html auto mpow = [](auto x, i64 y){ auto z = x; decltype(x) val = y & 1 ? x : decltype(x)(1); while(z *= z, y >>= 1) if(y & 1) val *= z; return val; }; cout << (mint(p - 1) * mpow(mint(p - 2), n - 1)) << endl; } signed main(){ long long N; scanf("%lld",&N); long long P; scanf("%lld",&P); solve(N, P); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vpii> vvpii; typedef vector<vpll> vvpll; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) (int)(x).size() #define fi first #define se second 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;} void __print(int x) {cerr << x;} void __print(long long 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 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 z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);} #ifdef ljuba #define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x) #else #define dbg(x...) #endif const char nl = '\n'; const int MOD = 998244353; inline ll mul(ll a, ll b) { return (a * 1LL * b) % MOD; } ll suma(ll n) { if(n % 2 == 0) { ll ans = mul(n/2, n+1); return ans; } else { return mul((n+1)/2, n); } } void solve() { ll a, b, c; cin >> a >> b >> c; ll ans = 1; ans = mul(ans, suma(a)); ans = mul(ans, suma(b)); ans = mul(ans, suma(c)); cout << ans << nl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int testCases = 1; //cin >> testCases; while(testCases--) solve(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define vi vector<int> #define vll vector<ll> #define pii pair<int,int> #define pll pair<ll,ll> #define vpi vector<pii> #define vpll vector<pll> #define endl '\n' #define fi first #define se second #define pb push_back #define eb emplace_back #define em emplace #define mp make_pair #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, n, m) for (ll i = n; i <= m; ++i) #define rep3(i, n, m) for (ll i = n; i >= m; --i) #define all(v) v.begin(), v.end() #define si(v) int(v.size()) #define UNIQUE(v) sort(all(v)), v.erase(unique(all(v)),v.end()) #define INT(...) int __VA_ARGS__; IN(__VA_ARGS__); #define LL(...) ll __VA_ARGS__; IN(__VA_ARGS__); #define STR(...) string __VA_ARGS__; IN(__VA_ARGS__); #define CHR(...) char __VA_ARGS__; IN(__VA_ARGS__); #define LD(...) ld __VA_ARGS__; IN(__VA_ARGS__); /* const ll mod = 1e9 + 7; */ const ll mod = 998244353; const ll infll = (1LL << 62) - 1; const ll inf = (1LL << 30) - 1; template<class S, class T> inline bool chmax(S &a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class S, class T> inline bool chmin(S &a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &... tail) { cin >> head; IN(tail...); } class mint { private: ll x; public: constexpr mint(const ll x = 0) noexcept : x((x % mod + mod) % mod) {} constexpr ll value() noexcept { return x; } constexpr mint operator += (const mint right) noexcept { if ((x += right.x) >= mod) x -= mod; return *this; } constexpr mint operator -= (const mint right) noexcept { if ((x += mod - right.x) >= mod) x -= mod; return *this; } constexpr mint operator *= (const mint right) noexcept { x = x * right.x % mod; return *this; } constexpr mint operator /= (const mint right) noexcept { return *this *= right.inv(); } constexpr mint operator + (const mint right) const noexcept { return mint(*this) += right; } constexpr mint operator - (const mint right) const noexcept { return mint(*this) -= right; } constexpr mint operator / (const mint right) const noexcept { return mint(*this) /= right; } constexpr mint operator * (const mint right) const noexcept { return mint(*this) *= right; } constexpr mint inv() const { return pow(mod-2); } constexpr mint pow(ll n) const { if (n == 0) return 1; mint a = pow(n>>1); a *= a; if (n & 1) a *= *this; return a; } }; signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); LL(a, b, c); mint aa = (a * (a + 1)) / 2; mint bb = (b * (b + 1)) / 2; mint cc = (c * (c + 1)) / 2; cout << (aa * bb * cc).value() << endl; }
#pragma GCC optimize("O3") #pragma GCC target("avx2") #include <iostream> #include <queue> using namespace std; #define f(x,y,z) for(int x=y;x<z;++x) #define fi first #define se second typedef pair<int, int> pii; int n, m, a, b, ans, step; vector<pii> e[1000]; queue<pii> q[2]; char c; bool bl, sdh[1000][1000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; f(i,0,m) { cin >> a >> b >> c; --a, --b; e[a].push_back({b, c}); e[b].push_back({a, c}); } step = 0, ans = 1e9; q[0].push({0, n-1}); while(!q[bl].empty()) { while(!q[bl].empty()) { a = q[bl].front().fi; b = q[bl].front().se; q[bl].pop(); for(pii &i:e[a]) for(pii &j:e[b]) if(i.se == j.se && !sdh[i.fi][j.fi]) { sdh[i.fi][j.fi] = true; // cout << a+1 << ' ' << b+1 << ' ' << i.fi+1 << ' ' << j.fi+1 << '\n'; if(i.fi == j.fi) { ans = min(ans, (step+1)<<1); } else if(i.fi == b || j.fi == a) { ans = step<<1|1; } else if(ans == 1e9) q[!bl].push({i.fi, j.fi}); } } if(ans != 1e9) return cout << ans << '\n', 0; ++step, bl = !bl; // cout << '\n'; } cout << "-1\n"; }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;} template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int N,M; vector<int> E[1010][26]; int D[1010][1010]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>M; FOR(x,N) FOR(y,N) D[x][y]=(x==y?0:1LL<<30); FOR(i,M) { cin>>x>>y>>s; E[x-1][s[0]-'a'].push_back(y-1); E[y-1][s[0]-'a'].push_back(x-1); if(x!=y) D[x-1][y-1]=D[y-1][x-1]=1; } queue<int> Q; FOR(i,N) Q.push(i*1000+i); FOR(x,N) FOR(y,N) if(D[x][y]==1) Q.push(x*1000+y); while(Q.size()) { int u=Q.front()/1000; int v=Q.front()%1000; Q.pop(); if(u==0&&v==N-1) { cout<<D[u][v]<<endl; return; } FOR(i,26) { FORR(x,E[u][i]) FORR(y,E[v][i]) { if(D[x][y]>D[u][v]+2) { D[x][y]=D[u][v]+2; Q.push(x*1000+y); } } } } cout<<-1<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
#include <iostream> #include <stack> #include <queue> #include <string> #include <set> #include <algorithm> #include <map> #include <vector> #include <string.h> #include <math.h> #include <cassert> #define __IO_INIT ios_base::sync_with_stdio(false), cin.tie(NULL); #define LLMAX 1e18 #define DIV 1000000007 #define MAX 987654321 using ll = long long; using namespace std; int main() { __IO_INIT; string str; cin >> str; reverse(str.begin(), str.end()); for (int i = 0; i < str.size(); i++) { if (str[i] == '6') str[i] = '9'; else if (str[i] == '9') str[i] = '6'; } cout << str; }
#include<bits/stdc++.h> typedef long long int ll; using namespace std; int main() { string s; cin>>s; int i,c=0,d=0,j,k,l; for(i=0;i<s.size();i++) { if(s[i]=='o') c++; if(s[i]=='?') d++; } // cout<<c<<" "<<d<<endl; int res=0; for(i=0;i<=9;i++) { for(j=0;j<=9;j++) { for(k=0;k<=9;k++) { for(l=0;l<=9;l++) { set<int>st; if(s[i]=='x' || s[j]=='x' || s[k]=='x' || s[l]=='x') continue; if(s[i]=='o') st.insert(i); if(s[j]=='o') st.insert(j); if(s[k]=='o') st.insert(k); if(s[l]=='o') st.insert(l); if(st.size()==c) res++; } } } } cout<<res<<endl; 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; string s; cin >> n >> s; vector<ll> a(n + 1); for (int i = 0; i < n + 1; i++){ cin >> a[i]; } vector<ll> diff(n); ll k = INFLL; for (int i = 0; i < n; i++){ diff[i] = abs(a[i + 1] - a[i]); chmin(k,diff[i]); } cout << k << endl; vector<ll> mod(n + 1); for (int i = 0; i < n + 1; i++){ mod[i] = a[i] % k; } for (int i = 0; i < k; i++){ for (int j = 0; j < n + 1; j++){ if (mod[j] > i) cout << ROUNDUP(a[j],k) << " "; else cout << a[j] / k << " "; } cout << "\n"; } }
#include<bits/stdc++.h> using namespace std; #define int long long int /*void insert(stack<int>& st , int temp) { if(st.empty()) { st.push(temp); return; } int x = st.top(); st.pop(); insert(st , temp); st.push(x); } void reverse(stack<int>& st ) { if(st.size()== 1) { return; } int temp = st.top(); st.pop(); reverse(st ); insert(st , temp); } void display(stack<int>& st) { while(st.size()) { cout<<st.top()<<" "; st.pop(); } }*/ int binary(int c[] , int n , int k) { int l=0; int r =n-1; int ans = INT_MAX; while(l<=r) { int mid = (l+r)/2; if(c[mid]>=k) { if(ans>mid) ans = mid; r = mid-1; } else { l = mid+1; } } return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,q; cin>>n>>q; int arr[n]; for(int i=0;i<n;i++) { cin>>arr[i]; } int c[n]; for(int i=0;i<n;i++) { c[i] = arr[i]-(i+1); } while(q--) { int k; cin>>k; if(c[n-1]<k) { cout<<k+n<<endl; } else { int p = binary(c , n , k); // since we have 0 based index so we need not // to subtract 1 from p . cout<<(k+p)<<endl; } } return 0; }
#include<iostream> using namespace std; #include<cstring> int N[200][2]; int T[200][2]; bool vis[200]; int main(){ int n,m; cin>>n>>m; int tot=0; for(int i=1;i<=m;i++){ tot++; cin>>N[tot][0]>>N[tot][1]; } int k;cin>>k;tot=0; for(int i=1;i<=k;i++){ tot++; cin>>T[tot][0]>>T[tot][1]; } int ans=0; for(int i=0;i<(1<<16);i++){ int tmp=i; int cnt=0; memset(vis,0,sizeof(vis)); for(int v=1;v<=k;v++){ vis[T[v][tmp&1]]=1; tmp>>=1; } for(int j=1;j<=m;j++){ if(vis[N[j][0]]&&vis[N[j][1]])cnt++; } ans=max(ans,cnt); } cout<<ans; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <complex> #include <cassert> #include <set> using namespace std; const long long MOD = 1e9 + 7; vector<long long> fact, fact_inv, inv; void init_nCk(long long SIZE) { fact.resize(SIZE + 5); fact_inv.resize(SIZE + 5); inv.resize(SIZE + 5); fact[0] = fact[1] = 1; fact_inv[0] = fact_inv[1] = 1; inv[1] = 1; for (long long i = 2; i < SIZE + 5; i++) { fact[i] = fact[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; } } long long nCk(long long n, long long k) { assert(!(n < k)); assert(!(n < 0 || k < 0)); return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD; } int main() { long long n; scanf("%lld", &n); string s; cin >> s; long long q; cin >> q; long long t, a, b; char c; string temp1, temp2; bool flag = false; for (long long i = 0; i < q; i++) { cin >> t >> a >> b; a--; b--; if (t == 1) { if (!flag) { c = s[a]; s[a] = s[b]; s[b] = c; } else { a = a >= n ? a - n: a + n ; b = b >= n ? b - n: b + n; c = s[a]; s[a] = s[b]; s[b] = c; } } else { flag = !flag; } } if (flag) { s = s.substr(n, 2 * n) + s.substr(0, n); } cout << s << endl; return 0; }
#include <bits/stdc++.h> int main() { std::vector<std::string> s(3); for (int i = 0; i < 3; ++i) std::cin >> s[i]; std::set<char> se; for (int i = 0; i < 3; ++i) { for (char c : s[i]) se.insert(c); } bool possible = false; int kind = (int)se.size(); std::vector<long long> n(3, 0); if (kind <= 10) { std::vector<int> num(10); for (int i = 0; i < 10; ++i) num[i] = i; do { std::map<char, int> ma; int i = 0; for (char c : se) { ma[c] = num[i++]; } n.assign(3, 0); if (ma[s[0][0]] == 0 || ma[s[1][0]] == 0 || ma[s[2][0]] == 0) continue; for (int i = 0; i < 3; ++i) { for (int j = 0; j < (int)s[i].length(); ++j) { n[i] = n[i] * 10 + ma[s[i][j]]; } } if (n[0] + n[1] == n[2]) { possible = true; break; } } while (std::next_permutation(num.begin(), num.end())); } if (possible) for (auto res : n) std::cout << res << std::endl; else std::cout << "UNSOLVABLE" << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int a, b, c; cin >> a >> b >> c; if (pow(a, 2) + pow(b, 2) < pow(c, 2)) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=int(a);i<int(b);++i) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define rrep(i,n) for(int i=int(n-1);i>=int(0);--i) #define fore(i,a) for(auto &i:a) #define all(x) x.begin(),x.end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define V vector #define P pair<int,int> #define TP tuple<int,int,int> #define F first #define S second 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; } using ll = long long; using ld = long double; using namespace std; const int INF =INT_MAX / 2; const ll INFL = 1LL << 61; const int MAX = 2e6+1; int main() { ll n;cin>>n; ll x=1; ll b=0; ll ans=INFL; while(x<=n+5){ ll c=n%x; ll a=(n-c)/x; chmin(ans,a+b+c); x*=2; b++; } cout<<ans<<endl; return 0; }
#include <iostream> #include <stdio.h> #include <climits> #include <math.h> //martin02 using namespace std; long long int n; int main() { long long int ans = LLONG_MAX; scanf("%lld", &n); for (int i = 0; i < 61; i++) { long long int b = (long long int) 1 << i; long long int a = n / b; long long int c = n - a * b; ans = min(ans, a + i + c); } printf("%lld\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { long long int a,b,c,d,x,y; scanf("%lld%lld%lld%lld",&a,&b,&c,&d); int ax=a; x=1;y=0; while(1){ a+=b; y+=c; if(a<=y*d)break; x++; if(x>ax){x=-1; break;} } printf("%lld\n",x); return 0; }
#define _USE_MATH_DEFINES #include<iostream> #include<iomanip> #include<vector> #include<string> #include<algorithm> #include<cmath> #include<stack> #include<queue> #include<set> #include<map> #include<climits> #include<bitset> #include<unordered_map> #include<unordered_set> #include<random> #include<list> #include<functional> using namespace std; #define MAX(A,B) ((A)>(B)?(A):(B)) #define MIN(A,B) ((A)<(B)?(A):(B)) #define LP(I,S,G) for (long long int I = S; I < G; I++) #define IN(X) for (int i = 0; i < X.size(); i++)cin >> X[i] #define OUT(X) for (int i = 0; i < X.size(); i++)cout << X[i]<<" " #define SORT(X) sort(X.begin(), X.end()) #define CSORT(X,Y) sort(X.begin(), X.end(),Y) #define COPY(X,Y) copy(X.begin(), X.end(), Y.begin()) #define ALL(X,Y) for (auto X = Y.begin(); X != Y.end(); X++) #define FULL(a) (a).begin(),(a).end() typedef long long int ll; long long int M = pow(10, 9) + 7; template<class I1, class I2> istream& operator>>(istream& s, pair<I1, I2>& in) { s >> in.first >> in.second; return s; } bool comp(pair<long long int, long long int >a, pair<long long int, long long int >b) { return a.second < b.second; } struct HashPair { //ๆณจๆ„ constใŒใ„ใ‚‹ template<class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { //firstๅˆ†ใ‚’ใƒใƒƒใ‚ทใƒฅๅŒ–ใ™ใ‚‹ auto hash1 = hash<T1>{}(p.first); //secondๅˆ†ใ‚’ใƒใƒƒใ‚ทใƒฅๅŒ–ใ™ใ‚‹ auto hash2 = hash<T2>{}(p.second); //้‡่ค‡ใ—ใชใ„ใ‚ˆใ†ใซใƒใƒƒใ‚ทใƒฅๅ‡ฆ็† size_t seed = 0; seed ^= hash1 + 0x9e3779b9 + (seed << 6) + (seed >> 2); seed ^= hash2 + 0x9e3779b9 + (seed << 6) + (seed >> 2); return seed; } }; int main() { ll a, b, c, d; cin >> a >> b >> c >> d; ll n = ceil((double)a / (double)(c*d - b)); if (n < 0) { cout << -1; } else cout << n; return 0; }
#include <bits/stdc++.h> #define int long long #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #define pb push_back #define mod 1000000007 #define lld long double #define mii map<int, int> #define mci map<char, int> #define msi map<string, int> #define pii pair<int, int> #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=x; i<y; i++) #define fill(a,b) memset(a, b, sizeof(a)) #define vi vector<int> #define setbits(x) __builtin_popcountll(x) #define w(x) int x; cin>>x; while(x--) using namespace std; const long long N=100005, INF=2000000000000000000; vi g[N]; int power(int a, int b, int p) { if(a==0) return 0; int res=1; a%=p; while(b>0) { if(b&1) res=(res*a)%p; b>>=1; a=(a*a)%p; } return res; } vi prime; bool isprime[N]; void pre() { for(int i=2;i<N;i++) { if(isprime[i]) { for(int j=i*i;j<N;j+=i) isprime[j]=false; prime.pb(i); } } return; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not // present in array return -1; } bool isPowerOfTwo(int n) { if(n==0) return false; return (ceil(log2(n)) == floor(log2(n))); } int gcd(int a, int b) { if(a < b) return gcd(b, a); else if(a%b == 0) return b; else return gcd(b, a%b); } 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 spf[N+1]; void spfFun(){ for(int i=0;i<=N;i++) spf[i]=i; for(int i=2;i*i<=N;i++){ if(spf[i]==i){ for(int j=i*i;j<=N;j+=i){ spf[j] = min(spf[j],i); } } } } bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0); } void print(bool n){ if(n){ cout<<"YES"; }else{ cout<<"NO"; } } int sum(int n){ return n*(n+1)/2; } int32_t main() { IOS; // pre(); // fill(isprime,true); int n; cin>>n; int ans=0; for(int i=0;i<n;i++){ int a,b; cin>>a>>b; ans+=sum(b)-sum(a-1); } cout<<ans; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MOD=1000000007; #define INF 1LL<<30 #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() int main(){ int T; cin>>T; while(T--){ string S; cin>>S; string T="atcoder"; if(S>T){ cout<<0<<endl; continue; } int res=INF; rep(i,S.size()){ if(S[i]=='a') continue; if(i==1) res=1; else if(S[i]>'t') res=min(res,i-1); else res=min(res,i); } if(res<INF) cout<<res<<endl; else cout<<-1<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5+10; char s[maxn]; int main() { int n; cin>>n>>(s+1); int f=0; ll tans=1e10; for(int i=4;i<=n;i++){ if(s[i]!=s[i-3])f=1; } if(f)return puts("0"),0; if(n==1){ if(s[1]=='1')return puts("20000000000"),0; return puts("10000000000"),0; }else if(n==2){ if(s[1]=='0'&&s[2]=='1')return puts("9999999999"),0; if(s[1]=='0'&&s[2]=='0')return puts("0"),0; return puts("10000000000"),0; } if(s[1]==s[2]&&s[1]=='1'&&s[3]=='0') cout<<tans-(n+2)/3+1; else if(s[1]==s[3]&&s[1]=='1'&&s[2]=='0') cout<<tans-(n)/3; else if(s[1]=='0'&&s[2]==s[3]&&s[2]=='1') cout<<tans-(n+1)/3; else cout<<0<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define D(x) cout << (#x) << " is " << x << endl; #define Q int t; scanf("%d", &t); for(int q=1; q<=t; q++) typedef long long int lli; typedef pair<int, int> pi; int main() { int n; cin >> n; string s; cin >> s; lli ans = 1e10; if(s == "1"){ cout << ans*2 << endl; return 0; } if(s == "0"){ cout << ans << endl; return 0; } if(s[0] == '0'){ s = "11"+s; } else{ if(s[1] == '0'){ s = "1"+s; } } n = s.size(); if(s[n-1] == '1'){ if(s[n-2] == '0'){ s += "10"; } else{ s += "0"; } } string t = "110", tmp; n = s.size(); for(int i=0; i<n; i++){ tmp += s[i]; if(i%3 == 2){ if(tmp != t){ cout << 0 << "\n"; return 0; } tmp.clear(); } } if(n%3 != 0){ cout << "0\n"; } else{ cout << ans-(n/3)+1 << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #define nput(a) cout << a << endl #define sput(a) cout << a << ' ' using namespace std; typedef long long ll; const int maxn = 1e4 + 10; int a[maxn]; void solve() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } ll ans = 0; for (int i = 1; i <= n; i++) { int mn = a[i]; for (int j = i; j <= n; j++) { mn = min(mn, a[j]); ans = max(ans, (ll)mn * (j - i + 1)); } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); int t; t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef pair<int,int>P; int a[20000]; int L[20000],R[20000]; int main(){ int n;cin>>n; rep(i,n)scanf("%d",&a[i]); stack<int>st; rep(i,n){ while(!st.empty()&&a[st.top()]>=a[i])st.pop(); L[i]=(st.empty()?0:st.top()+1); st.push(i); } while(!st.empty())st.pop(); for(int i=n-1;i>=0;i--){ while(!st.empty()&&a[st.top()]>=a[i])st.pop(); R[i]=(st.empty()?n:st.top()); st.push(i); } int Max=0; rep(i,n){ Max=max(Max,a[i]*(R[i]-L[i])); } cout<<Max<<endl; }
#include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<string> #include <cassert> #include <numeric> typedef long long ll; #define FOR(i, a, b) for(int i=(a); i<(b);i++) #define REP(i, n) for(int i=0; i<(n);i++) #define ROF(i, a, b) for(int i=(b-1); i>=(a);i--) #define PER(i, n) for(int i=n-1; i>=0;i--) using namespace std; vector<ll> parent; vector<vector<ll>> edges; vector<ll> depth; int main() { ll n, m; cin >> n >> m; vector<ll> a, b; //vector<vector<ll>> g; bool g[18][18] = { false }; a.resize(m); b.resize(m); REP(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; g[a[i]][b[i]] = true; g[b[i]][a[i]] = true; } vector<ll> ans; ll count = 1 << n; ans.resize(count); FOR(i, 1, count) { // ใƒ“ใƒƒใƒˆๆ•ฐ1ใชใ‚‰ๅผทๅˆถใงๅฎŒๅ…จใ‚ฐใƒฉใƒ• int count = 0; REP(j, n) { if ((i & (1 << j)) != 0) { count++; } } if (count == 1) { ans[i] = 1; continue; } // ๅฎŒๅ…จใ‚ฐใƒฉใƒ•ใ‹่ชฟในใ‚‹ bool pg = true; REP(j, n) { if ((i & (1 << j)) == 0) { continue; } FOR(k, j + 1, n) { if ((i & (1 << k)) == 0) { continue; } pg = pg && g[j][k]; if (!pg) break; } if (!pg) break; } if (pg) { ans[i] = 1; continue; } // ๅˆ†ๅ‰ฒใ™ใ‚‹ ll sub = i; ans[i] = n; sub = (sub - 1) & i; while (sub != i) { if (sub != 0) { ll tmp = ans[sub] + ans[i & (~sub)]; ans[i] = tmp < ans[i] ? tmp : ans[i]; } sub = (sub - 1) & i; } } cout << ans[count - 1]; return 0; }
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define eps 1e-8 #define maxn 200005 typedef long long ll; using namespace std; const double pi = acos(-1); const ll mod = 1e9 + 7; inline ll read(){ ll x = 0, f = 1;char ch = getchar(); while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();} while(ch >= '0' && ch <= '9'){x = x * 10 + ch -'0';ch = getchar();} return x * f; } int n, q; int dp[1 << 20], ok[1 << 20]; int mp[20][20], m, sum[1 << 20];; int main(){ int i, j, u, k, v; n = read(), m = read(); for(i = 1;i <= m;i++) u = read(), v = read(), mp[u][v] = mp[v][u] = 1; for(i = 0;i < n;i++) ok[1 <<i] = 1; for(i = 1;i < (1 << n);i++) sum[i] = sum[i >> 1] + (i & 1); for(i = 1;i < (1 << n);i++){ if(!ok[i]) continue; for(j = 1;j <= n;j++){ if(i & (1 << (j - 1))) continue; bool flag = 0; for(k = 1;k <= n;k++){ if((i & (1 << (k - 1))) && k != j) if(!mp[k][j]) {flag = 1;break;} } if(!flag) ok[i | (1 << j - 1)] = 1; } } memset(dp, 0x3f3f,sizeof(dp)); for(i = 0;i < (1 << n);i++) if(ok[i]) dp[i] = 1; for(i = 1;i < (1 << n);i++){ j = i; for(;j;j = (j - 1) & i) if(dp[i] > dp[j] + dp[i ^ j]) dp[i] = dp[j] + dp[i ^ j]; } printf("%d\n", dp[(1 << n) - 1]); return 0; } /* 6 6 1 2 2 3 2 4 4 5 3 5 3 6 */
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ll long long int #define ld long double #define ordered_set tree < ll , null_type , less<ll> , rb_tree_tag , tree_order_statistics_node_update > //added two more functions to set //(1)*(set.find_by_order(k))[kth element in the sorted set] //(2)set.order_of_key(k)[count of elements strictly less than k] // Uncomment when using kactl templates and change typedef of pair // #define rep(i, a, b) for(int i = a; i < (b); ++i) // #define sz(x) (int)(x).size() // typedef pair<int, int> pii; typedef vector< int > vi; typedef vector<long long> lvi; typedef vector< vector<int> > vvi; typedef vector< vector<long long> > lvvi; typedef pair< int,int > ii; typedef pair< long long,long long > lii; typedef vector<pair<int,int>> vii; typedef vector<pair<long long,long long>> lvii; typedef vector<vector<pair<int,int>>> vvii; typedef vector<vector<pair<long long,long long>>> lvvii; typedef vector<bool> vb; // #ifdef ONLINE_JUDGE // #define endl '\n' // #endif #define pb push_back #define all(c) (c).begin(),(c).end() #define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)//remember i is an iterator #define tc(t) int (t); cin>>(t);while((t)--) #define ff first #define ss second #ifdef ONLINE_JUDGE #define error(args...) 0 #else #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } #endif void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr <<"[ "<< *it << " = " << a << " ]"<< endl; err(++it, args...); } //----------------------------------------------------------------------------------------------------------------------------------------------------------------- const long long mod = 1e9 + 7; //----------------------------------------------------------------------------------------------------------------------------------------------------------------- long long mos() { return 0LL; } template<typename T, typename... Args> T mos(T a, Args... args) { return ((a + mos(args...))%mod + mod)%mod; } long long mop() { return 1LL; } template<typename T, typename... Args> T mop(T a, Args... args) { return (a*mop(args...))%mod; } //----------------------------------------------------------------------------------------------------------------------------------------------------------------- signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin>>n; lvi a(n), b(n); for (int i = 0; i < n; ++i) { cin>>a[i]; } for (int i = 0; i < n; ++i) { cin>>b[i]; } ll sum = 0; for (int i = 0; i < n; ++i) { sum += (a[i]*b[i]); } if(sum == 0) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; } // WA // 1. overflow // 2. re-initialize global variables for every test case. // 3. edge cases like n=1 // Run time error // 1. division by zero. // 2. array bounds. // TLE // 1. uncomment that #define endl '\n' line
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() void chmax(ll &x, ll y) {x = max(x,y);} void chmin(ll &x, ll y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} template<class in_vec_cout> void vec_cout(vector<in_vec_cout> vec) { for (in_vec_cout res : vec) {cout << res << " ";} cout << endl; } const ll inf = 1e18; const ll mod = 1e9 + 7; ll flag[10]; ll ans = 0, OK = 0; void dfs(vector<ll> vec, ll bit) { if (vec.size()==4) { if (bit==OK) ans++; return; } rep(i,10) { if (flag[i]==0) continue; vector<ll> tmp = vec; tmp.push_back(i); ll nbit = bit; if (flag[i]==1) { nbit |= (1<<i); } dfs(tmp,nbit); } } int main() { string S; cin >> S; ll O = 0, X = 0, OX = 0; rep(i,10) { if (S[i]=='o') {flag[i] = 1; O++; OK |= (1<<i);} else if (S[i]=='x') {flag[i] = 0; X++;} else {flag[i] = -1; OX++;} } vector<ll> vec; dfs(vec,0); cout << ans << endl; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define ins insert template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; //check the limits, dummy vector<vi> graph(MX); pi dfs(int v) { int sumPos = 0, sumNeg = 0; vi odds; int cnt = 1; trav(a, graph[v]) { pi cur = dfs(a); if (cur.s) { cnt = 1 - cnt; odds.pb(cur.f); } else { if (cur.f > 0) { sumPos += cur.f; } else sumNeg += cur.f; } } sort(all(odds)); int ans = 1; ans += sumNeg; F0R(i, sz(odds)) { if (i % 2) { ans -= odds[i]; } else { ans += odds[i]; } } if (sz(odds) % 2) { ans -= sumPos; } else { ans += sumPos; } //cout << v << " " << ans << " " << cnt << nl; return {ans, cnt}; } void solve() { int N; cin >> N; FOR(i, 1, N) { int X; cin >> X; X--; graph[X].pb(i); } cout << (dfs(0).f+N)/2 << nl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while(T--) { solve(); } return 0; }
#include<bits/stdc++.h> /* #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; */ using namespace std; /* typedef tree<int,null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; */ #define pb push_back #define ll long long #define x1 first #define y1 second #define frb(i,a,b) for(ll (i)=(a);(i) <= (b); (i)+=(i)&-(i)) #define rfrb(i,a) for(ll (i)=(a);(i) > 0;(i)-=(i)&-(i)) #define fr(i,a,b) for(ll (i) = (a); (i) <= (b); (i)++) #define rep(i,c) for(auto (i) : (c)) #define mini(a,b) (((a) < (b)) ? (a) : (b)) #define maxi(a,b) (((a) > (b)) ? (a) : (b)) #define par pair<ll,ll> #define vp vector<par> #define vi vector<ll> #define dq deque<ll> //indexed_set s; ///siempre ver las propiedades que se cumplen en los problemas ///always see the properties of the problems ///siempre poner "\n" como endl ///---------------------------------------------------------------------------- const ll mod = 1e9+7; ll n,t; struct b_s{ bool good(ll m){ ///verify if m is good double num = (100+t)* m / 100; return num - m - n >= 0.0; } ///every monotony sequence ll bin_sr3(ll l,ll r){ while(r > l+1){ ll m = (l+r)>>1; if(good(m))r = m; else l = m; } return r; } }bs; int main() { ios_base::sync_with_stdio(0);cin.tie(0); cout.setf(ios::fixed);cout.precision(0); ///freopen("a.in","r",stdin); ///freopen("a.out","w",stdout); cin>>t>>n; ll num = bs.bin_sr3(1,n*200)+n-1; cout<<num<<"\n"; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int cx[1000005]; int ci[1000005]; signed main(){ int a,b,w; memset(ci,0X3f,sizeof(ci)); memset(cx,0,sizeof(cx)); ci[0] = 0; cin>>a>>b>>w; w = w*1000; int judge = 0; for(int i=a;i<=b;i++) for(int j=i;j<=w;j++){ cx[j] = max(cx[j],cx[j-i]+1); ci[j] = min(ci[j],ci[j-i]+1); } //4557430888798830399 if(ci[w]==4557430888798830399) cout<<"UNSATISFIABLE"; else cout<<ci[w]<<" "<<cx[w]; }
#pragma GCC optimize("O3") //#pragma GCC target("avx") #include <bits/stdc++.h> using namespace std; #define re return #define pb push_back #define all(x) (x).begin(), (x).end() #define make_unique(x) sort(all(x)),x.resize(unique(all(x))-x.begin()) #define fi first #define se second #define ss second.second #define sf second.first #define ff first.first #define fs first.second #define sqrt(x) sqrt(abs(x)) #define mp make_pair #define PI 3.14159265358979323846 #define E 2.71828182845904523536 #define er erase #define in insert #define fo(i,n) for((i)=0;(i)<(n);(i)++) #define ro(i,n) for((i)=n-1;(i)>=0;(i)--) #define fr(i,j,n) for((i)=(j);(i)<(n);(i)++) #define rf(i,j,n) for((i)=((n)-1);(i)>=(j);(i)--) typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; void eras(map<int,int> &m,int x) { m[x]--; if (!m[x]) m.erase(x); } const int N=(int)3e5+100; const int M=(int)2e6+100; const int inf=(int)1e9+100; #define filename "" int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); //freopen(filename".in","r",stdin); //freopen(filename".out","w",stdout); //freopen("ans.txt","w",stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); srand(time(0)); ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int a,b,w,i; cin>>a>>b>>w; w*=1000; fr(i,1,1000001) { if (a*i<=w&&w<=b*i) break; } if (i==1000001) { cout<<"UNSATISFIABLE"; } else { cout<<i<<' '; while(a*(i+1)<=w&&w<=b*(i+1)) { i++; } cout<<i; } }
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=200000,M=300,mod=998244353,inv2=499122177; int add(int a,int b,int p=mod){return a+b>=p?a+b-p:a+b;} int sub(int a,int b,int p=mod){return a-b<0?a-b+p:a-b;} int mul(int a,int b,int p=mod){return (LL)a*b%p;} void sadd(int &a,int b,int p=mod){a=add(a,b,p);} void ssub(int &a,int b,int p=mod){a=sub(a,b,p);} void smul(int &a,int b,int p=mod){a=mul(a,b,p);} int n,m,a[N+9]; void into(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;++i) scanf("%d",&a[i]); } int c[M+9][M+9]; void Get_c(){ c[0][0]=1; for (int i=1;i<=m;++i){ c[i][0]=c[i][i]=1; for (int j=1;j<i;++j) c[i][j]=add(c[i-1][j-1],c[i-1][j]); } } int ans[M+9],sum[M+9]; void Get_ans(){ for (int i=1;i<=n;++i) for (int j=0,pw=1;j<=m;++j,smul(pw,a[i])) sadd(sum[j],pw); for (int i=0,pw=1;i<=m;++i,smul(pw,2)){ for (int j=0;j<=i;++j) sadd(ans[i],mul(c[i][j],mul(sum[j],sum[i-j]))); ssub(ans[i],mul(pw,sum[i])); } } void work(){ Get_c(); Get_ans(); } void outo(){ for (int i=1;i<=m;++i) printf("%d\n",mul(ans[i],inv2)); } int main(){ into(); work(); outo(); return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<string> #include<stdio.h> #include<stdlib.h> #include<float.h> #include<tuple> #include<string.h> #include<iomanip> #include<stack> #include<queue> #include<map> #include<deque> #include<math.h> using namespace std; #define ll long long #define rep(i,n) for(ll i=0;i<n;i++) #define REP(i,n) for(ll i=1;i<=n;i++) #define ALLOF(c) (c).begin(), (c).end() #define Pa pair<ll,ll> const ll mod=1000000007; const ll INF=1e12; const ll inf=-1; ll ABS(ll a){return max(a,-a);} int main(void){ ll N,K; cin>>N>>K; K=ABS(K); vector<ll> A(0); REP(i,N) A.push_back(i); rep(i,N-1) A.push_back(N-1-i); ll ans=0; for(ll i=0;i+K<A.size();i++) ans+=A.at(i)*A.at(i+K); cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll const inf = pow(10,9) + 7; int main(){ int n;cin >> n; cout << n-1 << endl; }
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair using ll = long long; int main() { int n ; cin >> n; if(n == 1) cout << 0 << endl; else if(n == 2) cout << 1 << endl; else if(n % 2 == 0) cout << n / 2 * 2 - 1<< endl; else cout << n / 2 * 2 << endl; return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #define rep(i,N) for(int i=0;i<(int)N;i++) static inline void PUT(char c) { static char buf[1<<15],*ptr=buf; if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c; } static inline int IN(void) { int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();} while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x; } static inline void OUT(int a) { if(a<0)PUT('-'),a=-a; int d[40],i=0;do{d[i++]=a%10;}while(a/=10); while(i--){PUT(d[i]+48);}PUT('\n'); } const int MOD=998244353,inv2=(MOD+1)/2; int fact[301],inv[301]; static inline int MPow(int a,int b){return b?1l*MPow(1l*a*a%MOD,b>>1)*(b&1?a:1)%MOD:1;} static inline int comb(const int n,const int k) { if(k<0||n<k){return 0;}return 1l*fact[n]*inv[n-k]%MOD*inv[k]%MOD; } int main(void) { int N=IN(),K=IN(),A[200000],sum[301]={0},temp[200000],total=0;sum[0]=N; fact[0]=1;rep(i,K)fact[i+1]=1l*fact[i]*(i+1)%MOD; inv[K]=MPow(fact[K],MOD-2); rep(i,N){A[i]=IN();temp[i]=1;} rep(i,K){inv[K-1-i]=1l*inv[K-i]*(K-i)%MOD;rep(j,N){temp[j]=(1l*temp[j]*A[j])%MOD;sum[i+1]=(1l*sum[i+1]+temp[j])%MOD;}} rep(i,K) { total=0; rep(j,i+2){total=(1l*total+1l*comb(i+1,j)*(1l*sum[j]*sum[i+1-j]%MOD-sum[i+1]+MOD)%MOD*inv2)%MOD;} OUT(total); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; template<typename T> using v2 = vector<vector<T>>; template<typename T> inline v2<T> fill(int r, int c, const T& t){ return v2<T>(r, vector<T>(c, t)); } const int MOD = 998244353; inline int add(int a, int b){ return (a+b)%MOD; } inline int sub(int a, int b){ return ((a-b)%MOD + MOD)%MOD; } inline int mul(int a, int b){ return ((ll)a*b)%MOD; } int pwr(int a, int b){ if(!b) return 1; int temp = pwr(a, b/2); if(b%2) return mul(mul(temp, temp), a); return mul(temp, temp); } const int modinv2 = pwr(2, MOD-2); inline int dvd2(int a){ return mul(a, modinv2); } void solve(){ int n, k; cin >> n >> k; vector<int> a(n); for(int i = 0; i < n; ++i) cin >> a[i]; v2<int> ch = fill(k+1, k+1, -1); for(int i = 0; i <= k; ++i) ch[i][0] = 1; for(int j = 1; j <= k; ++j) ch[0][j] = 0; for(int i = 1; i <= k; ++i){ for(int j = 1; j <= k; ++j){ ch[i][j] = add(ch[i-1][j-1], ch[i-1][j]); } } v2<int> pwrs = fill(n, k+1, -1); for(int i = 0; i < n; ++i){ pwrs[i][0] = 1; for(int j = 1; j <= k; ++j){ pwrs[i][j] = mul(pwrs[i][j-1], a[i]); } } vector<int> sums(k+1, 0); for(int i = 0; i <= k; ++i){ for(int j = 0; j < n; ++j){ sums[i] = add(sums[i], pwrs[j][i]); } } for(int i = 1; i <= k; ++i){ int ans = 0; for(int j = 0; j <= i; ++j){ int s = mul(sums[j], sums[i-j]); s = sub(s, sums[i]); s = mul(s, ch[i][j]); ans = add(ans, s); } ans = dvd2(ans); cout << ans << "\n"; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define REP(i, n) for(int i=0; i<(n); ++i) #define FOR(i, a, b) for(int i=(a); i<(b); ++i) #define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i) #define DEBUG(x) cout<<#x<<": "<<(x)<<'\n' #define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n' #define ALL(a) (a).begin(), (a).end() template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;} template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;} // constexpr ll MOD=1000000007ll; constexpr ll MOD=998244353ll; #define FIX(a) ((a)%MOD+MOD)%MOD const double EPS=1e-11; #define EQ0(x) (abs((x))<EPS) #define EQ(a, b) (abs((a)-(b))<EPS) int main(){ ios::sync_with_stdio(false); cin.tie(0); // cout<<setprecision(10)<<fixed; ll s, p; cin>>s>>p; for(ll n=1; n<=1000000; ++n){ ll m=s-n; if(m<=0){ continue; } if(n*m==p){ cout<<"Yes"<<'\n'; return 0; } } cout<<"No"<<'\n'; return 0; }
#include <bits/stdc++.h> #define pii pair<int, int> #define pll pair<long long, long long> #define L(n) (n << 1) #define R(n) (n << 1 | 1) #define print_vector(n) for(auto a0 : n) cout << a0 << ' '; cout << endl; #define vector_sort(n) sort(n.begin(), n.end()) #define print_array(n, l, r) for(int a0 = l; a0 <= r; a0++) cout << n[a0] << ' '; cout << '\n'; #define MIN(a, b) (a < b ? a : b) #define MAX(a, b) (a > b ? a : b) #define ABS(a) ((a) > 0 ? (a) : -(a)) using namespace std; template<class T> istream & operator >> (istream &in, pair<T, T> &p) { in >> p.first >> p.second; return in; } template<class T> ostream & operator <<(ostream &out, pair<T, T> &p) { out << p.first << ' ' << p.second; return out; } template<class Tuple, std::size_t N> struct TuplePrinter { static void print(ostream &out, const Tuple& t) { TuplePrinter<Tuple, N-1>::print(out, t); out << ' ' << get<N-1>(t); } }; template<class Tuple> struct TuplePrinter<Tuple, 1> { static void print(ostream &out, const Tuple& t) { out << get<0>(t); } }; template<class... Args> ostream & operator <<(ostream &out, const tuple<Args...> &p) { TuplePrinter<decltype(p), sizeof...(Args)>::print(out, p); return out; } long long ans = 1e18, N, b = 1; int main() { ios_base::sync_with_stdio(0); cin >> N; int cnt = 0; while (b <= N && b < 2e18) { ans = min(ans, N / b + N % b + cnt); b *= 2; cnt++; } cout << ans << "\n"; }
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; typedef unsigned long long int ull; typedef long int li; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define test ll t; cin >> t; while(t--) const long long int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0}; const long long int cons = 100005; const long long int MOD = 1000000007; // const long long int MOD = 998244353; const long long int const_INT_MAX = 1000000000000000000, const_INT_MIN = -1 * 1000000000000000000; using namespace std; bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.first > b.first);} bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second < b.second);} bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second > b.second);} ll gcd(ll x, ll y){return (ll)(__gcd(x, y));} ll lcm(ll x, ll y){return (ll)((x * y) / gcd(x, y));} ll mod_expo(ll x, ll y, ll p){ ll res = 1; if((x + p) % p == 0){ return res = 0; } x = (x + p) % p; while (y > 0){ if (y & 1) res = (res*x + p) % p; y = y>>1; x = (x*x + p) % p; } return res; } void usaco(string str = ""){ fastio; if(str.size()) { freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } } // GFG orz (and the blessings of nishuz sir) ll getSum(ll BITree[], ll index){ ll sum = 0; while (index > 0){ sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(ll BITree[], ll n, ll index, ll val){ while (index <= n){BITree[index] += val; index += index & (-index);} } void convert(ll arr[], ll n){ ll temp[n]; for(ll i=0;i<n;i++) temp[i] = arr[i]; sort(temp, temp+n); for (ll i=0;i<n;i++){ arr[i] = lower_bound(temp, temp+n, arr[i]) - temp + 1; } } ll getInvCount(ll arr[], ll n){ ll invcount=0, BIT[n+1];; convert(arr, n); for (ll i=1;i<=n;i++) BIT[i] = 0; for (ll i=n-1;i>=0;i--){ invcount += getSum(BIT, arr[i]-1); updateBIT(BIT, n, arr[i], 1); } return invcount; } int main(){ usaco(); ll n; cin >> n; ll a[n], dp[n], temp[n]; for(ll i=0;i<n;i++){cin >> a[i]; temp[i] = a[i];} dp[0] = getInvCount(temp, n); for(ll i=1;i<n;i++){ dp[i] = dp[i-1] - 2 * a[i-1] + n - 1; } for(ll i=0;i<n;i++) cout << dp[i] << "\n"; cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { cin.tie(nullptr); // Do not flush "cout" when processing "cin". int n; cin >> n; // dp[i][j] // i = x[i] ใพใงๆฑบใ‚ใŸๆ™‚ // j = y[i] ใซใชใ‚‹ๆ™‚ใฎๅ ดๅˆใฎๆ•ฐ vector<vector<ll>> dp(n + 1, vector<ll>(2)); // dpใ‚’ๅˆๆœŸๅŒ– dp.at(0).at(true) = 1; dp.at(0).at(false) = 1; for (int i = 0; i < n; i++) { string s; cin >> s; // y0 (= x0) ใจ x1 ใฎ็ต„ใฟๅˆใ‚ใ›ใ‹ใ‚‰ใ€y1 ใฎๅ–ใ‚Šใ†ใ‚‹ใƒ‘ใ‚ฟใƒผใƒณๆ•ฐใ‚’ๆ•ฐใˆใ‚‹ for (bool y_current : {true, false}) { bool y_next; if (s == "AND") { // AND true ใฎๅ ดๅˆ y_next = y_current && true; dp.at(i + 1).at(y_next) += dp.at(i).at(y_current); // AND false ใฎๅ ดๅˆ y_next = y_current && false; dp.at(i + 1).at(y_next) += dp.at(i).at(y_current); } else { // OR true ใฎๅ ดๅˆ y_next = y_current || true; dp.at(i + 1).at(y_next) += dp.at(i).at(y_current); // AND false ใฎๅ ดๅˆ y_next = y_current || false; dp.at(i + 1).at(y_next) += dp.at(i).at(y_current); } } } cout << dp.at(n).at(true) << endl; return 0; }
// C - Squared Error #include <bits/stdc++.h> using namespace std; using ll = int64_t; int main(){ int n; cin>>n; vector<ll> A(n); for(ll&a:A) cin>>a; ll aa = 0, sa = 0; for(ll a:A){ aa += a*a; sa += a; } cout<< n*aa - sa*sa <<endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; constexpr int Inf = 1000000030; constexpr ll INF= 2000000000000000001; constexpr ll MOD = 998244353; const double PI = 3.1415926535897; typedef pair<ll,ll> P; typedef pair<ll,P> PP; template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mod(ll val, ll M) { val = val % M; if(val < 0) { val += M; } return val; } template<typename T> T RS(T N, T P, T M){ if(P == 0) { return 1; } if(P < 0) { return 0; } if(P % 2 == 0){ ll t = RS(N, P/2, M); if(M == -1) return t * t; return t * t % M; } if(M == -1) { return N * RS(N,P - 1,M); } return N * RS(N, P-1, M) % M; } int main() { int N; cin >> N; vector<ll> vec(N); for(int i = 0;i < N;i++) { cin >> vec.at(i); } ll ret = 0; map<ll,ll> ma; for(int i = 0;i < N;i++) { if(i == 0) ma[vec.at(i)]++; else { for(auto x:ma) { ret += x.second * (x.first - vec.at(i)) * (x.first - vec.at(i)); } ma[vec.at(i)]++; } } cout << ret << endl; }
//code bcuz love,code 4 hate #include<bits/stdc++.h> #define inf 1e9 #define eps 1e-6 #define mp make_pair #define pb push_back #define re register int #define fr first #define sd second #define pa pair<ll,ll> #define FOR(i,a,b) for(re i=a;i<=b;i++) #define REP(i,a,b) for(re i=a;i>=b;i--) #define MEM(a) memset(a,0,sizeof(a)) #define N 51 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; inline ll read() { char ch=getchar(); ll s=0,w=1; 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; } inline ll lowbit(ll x){return x&(-x);} ll n,ans=5e18; ll a[N],p[N],tot,b[N][N]; inline void Solve(ll x,ll v) { ll tmp=v; for(re i=2;i*i<=v;i++) { if(tmp%i==0){b[x][i]=1;while(tmp%i==0)tmp/=i;} } if(tmp>1){b[x][tmp]=1;} } inline ll check(ll x){for(re i=2;i<x;i++)if(x%i==0)return 0;return 1;} int main() { //ios::sync_with_stdio(false); //freopen(".in","r",stdin); //freopen(".out","w",stdout); n=read();FOR(i,1,n)a[i]=read(),Solve(i,a[i]); FOR(i,2,50)if(check(i))p[++tot]=i; FOR(S,0,(1<<tot)-1) { ll v=1; FOR(i,0,tot-1)if((S>>i)&1)v*=p[i+1]; ll fl=1; FOR(i,1,n) { ll flag=0; FOR(j,0,tot-1)if((S>>j)&1)if(b[i][p[j+1]]){flag=1;} if(!flag){fl=0;break;} } if(fl)ans=min(ans,v); } cout<<ans<<'\n'; return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; //#include<boost/multiprecision/cpp_int.hpp> //#include<boost/multiprecision/cpp_dec_float.hpp> //namespace mp=boost::multiprecision; //#define mulint mp::cpp_int //#define mulfloat mp::cpp_dec_float_100 //#include<atcoder/all> //using namespace atcoder; struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init; #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) #define INF (1<<30) #define LINF (lint)(1LL<<56) #define endl "\n" #define rep(i,n) for(lint (i)=0;(i)<(n);(i)++) #define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--) #define flc(x) __builtin_popcountll(x) #define pint pair<int,int> #define pdouble pair<double,double> #define plint pair<lint,lint> #define fi first #define se second #define all(x) x.begin(),x.end() #define vec vector<lint> #define nep(x) next_permutation(all(x)) typedef long long lint; //typedef __int128_t llint; int dx[8]={1,1,0,-1,-1,-1,0,1}; int dy[8]={0,1,1,1,0,-1,-1,-1}; const int MAX_N=3e5+5; //vector<int> bucket[MAX_N/1000]; //constexpr int MOD=1000000007; constexpr int MOD=998244353; int main(void){ int N,M; cin >> N >> M; int A[M],B[M]; rep(i,M) cin >> A[i] >> B[i],A[i]--,B[i]--; int K; cin >> K; int C[K],D[K]; rep(i,K) cin >> C[i] >> D[i],C[i]--,D[i]--; int ans=0; rep(i,(1<<K)){ int sara[N]={}; rep(j,K){ if(i&(1<<j)) sara[C[j]]++; else sara[D[j]]++; } int res=0; rep(j,M) if(sara[A[j]] && sara[B[j]]) res++; ans=max(ans,res); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define int long long int #define ld long double #define pb push_back #define MOD 1000000007 #define inf 3e18 #define vi vector<int> #define vld vector<ld> #define pii pair<int,int> #define mii map<int,int> #define fi first #define se second #define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define db(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << " "<< name << " : " << arg1 <<'\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; //order_of_key (k) : Number of items strictly smaller than k . //find_by_order(k) : K-th element in a set (counting from zero) (returns an iterator) void inp_out() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif } int32_t main() { fastIO inp_out(); int n; cin >> n; vector < string > a(n); set < string > st; for(int i = 0; i < n; ++ i) { cin >> a[i]; st.insert(a[i]); } for(int i = 0; i < n; ++ i) { string s = "!"; s += a[i]; if(st.find(s) != st.end()) { cout << a[i]; return 0; } } cout << "satisfiable"; return 0; }
#include<bits/stdc++.h> #define all(a) a.begin(), a.end() #define put(i) cout<<fixed<<i<<endl #define debug(i) cerr<<fixed<<i<<endl #define debugl(a) for(auto x:a)cerr<<x<<' '; cout<<endl #define rep(i,s,n) for(long long i=s;i<(long long)(n);i++) using namespace std; using ll = long long; int main(){ ll n; cin >> n; map<string,ll> mp,mp2; rep(i,0,n){ string tmp; cin >> tmp; mp[tmp] = 0; } for(auto x:mp){ if(x.first[0] == '!'){ mp2[x.first.substr(1,x.first.size()-1)]++; }else{ mp2[x.first]++; } } for(auto x:mp2){ if(x.second > 1){ put(x.first); return 0; } } put("satisfiable"); }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define PII pair<int, int> #define mp(x, y) make_pair(x, y) #define reset(x) memset(x, 0, sizeof(x)); #define resets(x) memset(x, -1, sizeof(x)); #define resetss(x) memset(x, 0x3f3f3f3f, sizeof(x)); #define resetsss(x) memset(x, -0x3f3f3f3f, sizeof(x)); #define recopy(a, b) memcpy(b, a, sizeof(a)); typedef unsigned long long ULL; typedef long long LL; const int INF = 0x3f3f3f3f; const int mod = 998244353; const int maxn = 3e5 + 10; LL fun1(LL n) { return (n / 2 * (n + 1)) % mod; } LL fun2(LL n) { return ((n + 1) / 2 * n) %mod; } int main() { ios::sync_with_stdio(false); LL A, B, C; cin >> A >> B >> C; if (A % 2 == 0) A = fun1(A); else A = fun2(A); if (B % 2 == 0) B = fun1(B); else B = fun2(B); if (C % 2 == 0) C = fun1(C); else C = fun2(C); LL ans = (((A * B) % mod) * C) % mod; cout << ans << endl; return 0; }
/** ____ ____ ____ ____ ____ ||a |||t |||o |||d |||o || ||__|||__|||__|||__|||__|| |/__\|/__\|/__\|/__\|/__\| **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; ll n; int m; int pwr (int a, ll b) { if(b == 0) return 1; if(b & 1) return 1LL * a * pwr(a, (b ^ 1)) % m; int aux = pwr(a, (b >> 1)); return 1LL * aux * aux % m; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; int m1 = m; m *= m; cout << pwr(10, n) / m1 << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int h,w,x,y,i,j,cn=0; char mp[101][101]; scanf("%d%d%d%d",&h,&w,&y,&x); for(i=0;i<h;i++) scanf("%s",mp[i]); x-=1; y-=1; for(i=y;i<h;i++){ if(mp[i][x]=='.') cn++; else break; } for(i=x;i<w;i++){ if(mp[y][i]=='.') cn++; else break; } for(i=y;i>=0;i--){ if(mp[i][x]=='.') cn++; else break; } for(i=x;i>=0;i--){ if(mp[y][i]=='.') cn++; else break; } printf("%d\n",cn-3); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int h,w,x,y; cin>>h>>w>>x>>y; char arr[h][w]; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cin>>arr[i][j]; } } int ans=0; for(int i=y-2;i>=0;i--){ //cout<<i<<" "<<"\n"; //cout<<arr[x-1][i]<<"\n"; if(arr[x-1][i]=='.'){ // cout<<"!"<<x-1<<" "<<i<<"\n"; ans++; //cout<<"###"<<"\n"; } else break; } for(int i=y;i<w;i++){ if(arr[x-1][i]=='.'){ //cout<<"!!"<<x-1<<" "<<i<<"\n"; ans++; } else break; } for(int i=x-2;i>=0;i--){ if(arr[i][y-1]=='.'){ //cout<<"!!"<<i<<" "<<y-1<<"\n"; ans++; } else break; } for(int i=x;i<h;i++){ if(arr[i][y-1]=='.'){ // cout<<"!!!!"<<i<<" "<<y-1<<"\n"; ans++; } else break; } if(arr[x-1][y-1]=='.'){ // cout<<"#"<<ans<<"\n"; ans++; } if(arr[x-1][y-1]!='.'){ ans=0; } cout<<ans; return 0; }
// #pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> #define ll long long int #define vi vector<int> #define vvi vector<vector<int>> #define vll vector<long long> #define vs vector<string> #define vc vector<char> #define vb vector<bool> #define forn(i, s, n) for(ll i=(ll)s; i<(ll)(n); i++) #define all(c) c.begin(),c.end() #define pb push_back #define pll pair<long long int, long long int> #define pii pair<int, int> #define lld long double #define F first #define S second #define PI 3.141592653589793238 #define prec(n) fixed<<setprecision(n) #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); } using namespace std; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; void itval(istream_iterator<string> it) {} template<typename T, typename... Args> void itval(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; itval(++it, args...); } const ll MOD = 1e9 + 7; template <typename T> inline void print(T x) {cout << x << "\n";} template <typename T> inline void printvec(T x) {for (auto a : x)cout << a << ' '; cout << '\n';} // ----------------------------------------------------------------------- struct custom { bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2)const { if (p1.F == p2.F) return p1.S.F > p2.S.F; return p1.F > p2.F; } }; // Calculate a^b % MOD ------------------------------------------------- ll get_pow(ll a, ll b, ll M = MOD) { ll res = 1; while (b) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; } // --------------------------------------------------------------------- const ll N = 1e5 + 5, inf = 2e18; inline ll calc(ll x, ll c, ll d) { return x + c + (d / (x + 1)); } std::vector<pair<ll, pll>> adj[N]; void solve() { ll n, m, x, y, c, d; cin >> n >> m; forn(i, 0, m) { cin >> x >> y >> c >> d; x--; y--; adj[x].pb({y, {c, d}}); adj[y].pb({x, {c, d}}); } std::vector<ll> dist(n, inf); dist[0] = 0; set<pll> s; ll z, k; s.insert({0, 0}); while (!s.empty()) { pll t = *s.begin(); s.erase(s.begin()); for (auto D : adj[t.S]) { z = inf, k = sqrt(D.S.S); forn(j, -5, 5) { z = min(z, calc(max(t.F, k + j), D.S.F, D.S.S)); } if (dist[D.F] > z) { s.erase({dist[D.F], D.F}); dist[D.F] = z; s.insert({dist[D.F], D.F}); } } } if (dist[n - 1] == inf) cout << -1; else cout << dist[n - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int test = 1; clock_t z = clock(); //cin >> test; forn(tes, 0, test) { solve(); } debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) int n, p[105]; double x[105], y[105], dist[105][105], l, r, m; bool out[105][105]; int find(int x){return (p[x] < 0 ? x : p[x] = find(p[x]));} void uni(int x, int y){ x = find(x); y = find(y); if(x != y){ if(p[x] > p[y]) swap(x, y); p[x] += p[y]; p[y] = x; } } int main(){ scanf("%d", &n); rep(i,n){ scanf("%lf%lf", x+i, y+i); rep(j,i) dist[i][j] = hypot(x[i]-x[j],y[i]-y[j]); dist[n][i] = abs(100-y[i]); dist[n+1][i] = abs(100+y[i]); } dist[n+1][n] = 200; l = 0, r = 200; while(r-l > 1e-6){ m = (l+r) / 2; fill(p, p+n+2, -1); rep(i,n+2) rep(j,i) if(dist[i][j] < m) uni(i,j); (find(n) != find(n+1) ? l : r) = m; } printf("%f\n", m / 2); }
#include <bits/stdc++.h> #define rep(i, a, n) for(int i = a; i < n; i++) using namespace std; using ll = long long; using P = pair<ll, ll>; //MINT //const int mod = 1000000007; const int mod = 998244353; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // 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;} template<typename T> struct UnionFind { vector<T> d; UnionFind(int n=0): d(n,-1) {} T find(int x) { if (d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (d[x] > d[y]) swap(x,y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y);} T size(int x) { return -d[find(x)];} }; // POWER_MODver. N^k % MOD ll mod_pow(ll n, ll k){ ll res = 1; for(; k > 0; k >>= 1){ if(k&1) res = (res*n)%mod; n = (n*n)%mod; } return res; } int main() { int n; cin >> n; vector<int> f(n); rep(i, 0, n){ cin >> f[i]; f[i]--; } UnionFind<int> uf(n); rep(i, 0, n) uf.unite(f[i], i); map<int, bool> mp; rep(i, 0, n) mp[uf.find(i)] = true; mint ans = mod_pow(2, mp.size()); cout << ans-1 << endl; return 0; }
// E - Rotate and Flip #include <bits/stdc++.h> using namespace std; #define vec vector #define rep(i,n) for(int i=0;i<(int)(n);++i) using ll = int64_t; using vl = vec<ll>; using vvl = vec<vl>; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; vl X(n), Y(n); rep(i, n) cin>>X[i]>>Y[i]; vec<vvl> mats = {{{1,0},{0,1},{0,0}}}; // [ex,ey,o]: op0: E, (x',y')=(x,y) // rep(i,mats.size()){ // for(auto x:mats[i]){ // cout<<"("<<x[0]<<", "<<x[1]<<"), "; // } // puts(""); // } int m; cin>>m; rep(i, m){ // op int c; cin>>c; vvl mt = mats.back(); vl x = mt[0], y = mt[1], o = mt[2]; if(c == 1){ x = {x[1], -x[0]}; y = {y[1], -y[0]}; o = {o[1], -o[0]}; } else if(c == 2){ x = {-x[1], x[0]}; y = {-y[1], y[0]}; o = {-o[1], o[0]}; } else if(c == 3){ ll p; cin>>p; x = {-x[0] + p*2, x[1]}; y = {-y[0] + p*2, y[1]}; o = {-o[0] + p*2, o[1]}; } else{ // c == 4 ll p; cin>>p; x = {x[0], -x[1] + p*2}; y = {y[0], -y[1] + p*2}; o = {o[0], -o[1] + p*2}; } mt = {x, y, o}; mats.emplace_back(mt); } int q; cin>>q; rep(i, q){ int a, b; cin>>a>>b; --b; vvl mt = mats[a]; vl ex = mt[0], ey = mt[1], o = mt[2]; ll x = (ex[0]-o[0])*X[b] + (ey[0]-o[0])*Y[b] + o[0]; ll y = (ex[1]-o[1])*X[b] + (ey[1]-o[1])*Y[b] + o[1]; cout<< x <<" "<< y <<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define PI 3.14159265359 #define mod 1000000007 #define ALL(a) (a).begin(),(a).end() int main(){ int n,q; string s; int count = 0; cin >> n >> s >> q; rep(i,q){ int t,a,b; cin >> t >> a >> b; if(t == 1){ if(count % 2 == 0)swap(s[a-1],s[b-1]); else{ if(a <= n)a+=n; else a-=n; if(b <= n)b+=n; else b-=n; swap(s[a-1],s[b-1]); } } else{ count++; } } if(count % 2 == 1){ cout << s.substr(n,n) + s.substr(0,n) << endl; } else cout << s << endl; }
#line 1 "/workspaces/compro/lib/template.hpp" #line 1 "/workspaces/compro/lib/io/vector.hpp" #include <iostream> #include <vector> #ifndef IO_VECTOR #define IO_VECTOR template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { int size = v.size(); for (int i = 0; i < size; i++) { std::cout << v[i]; if (i != size - 1) std::cout << " "; } return out; } template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) { for (auto &el : v) { std::cin >> el; } return in; } #endif #line 4 "/workspaces/compro/lib/template.hpp" #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) (v).begin(), (v).end() #define coutd(n) cout << fixed << setprecision(n) #define ll long long int #define vl vector<ll> #define vi vector<int> #define MM << " " << using namespace std; template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } // ้‡่ค‡ใ‚’ๆถˆใ™ใ€‚่จˆ็ฎ—้‡ใฏO(NlogN) template <class T> void unique(std::vector<T> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } #line 2 "main.cpp" long long solve(long long A, long long B, long long C) { vi loop; int now = A % 10; loop.push_back(now); while (true) { int next = (now * (A % 10)) % 10; int len = loop.size(); if (next == loop[0]) break; loop.push_back(next); now = next; } ll mod = loop.size(); ll res = 1; while (C) { if (C & 1) { res = res * B % mod; } B = B * B % mod; C /= 2; } return loop[(res - 1 + mod) % mod]; } // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { long long A, B, C; std::cin >> A >> B >> C; auto ans = solve(A, B, C); std::cout << ans << std::endl; return 0; }
#define taskname "test" #include <bits/stdc++.h> using namespace std; #define sz(x) (int)x.size() #define fi first #define se second typedef long long lli; typedef pair<int, int> pii; const int maxn = 5005; const int mod = 998244353; int n, m; int a[maxn][maxn]; int f[maxn]; int g[maxn]; void read_input() { cin >> n >> m; } int powmod(int x, int k) { return a[x][k]; } int add(int x, int y) { x += y; if(x >= mod) x -= mod; return x; } int sub(int x, int y) { x -= y; if(x < 0) x += mod; return x; } void solve() { for(int i = 0; i <= m; ++i) { a[i][0] = 1; for(int j = 1; j <= n; ++j) a[i][j] = a[i][j - 1] * 1LL * i % mod; } f[1] = m; fill(g + 1, g + m + 1, 1); for(int i = 2; i <= n; ++i) { f[i] = f[i - 1] * 1LL * m % mod; for(int j = 1; j <= m; ++j) f[i] = add(f[i], sub(powmod(m, i - 1), g[j])); for(int j = 1; j <= m; ++j) { g[j] = g[j] * 1LL * m % mod; g[j] = add(g[j], powmod(m - j, i - 1)); } } cout << f[n] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); read_input(); solve(); }
#include<bits/stdc++.h> #define int long long using namespace std; int prm[20]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71},f[74][1<<20],a,b,c[74],ans; signed main() { cin>>a>>b; //cout<<b-a+1<<endl; for(register int i=a;i<=b;i++) { for(register int j=0;j<20;j++) { if(i%prm[j]==0)c[i-a+1]|=1<<j; } //cout<<c[i-a+1]<<endl; } //cout<<'*'; f[0][0]=1; for(register int i=1;i<=b-a+1;i++) { memcpy(f[i],f[i-1],sizeof(f[i])); //cout<<'*'; for(register int j=0;j<(1<<20);j++) { if(j&c[i]||!f[i-1][j])continue; //printf("%lld\n",j|c[i-a+1]); f[i][j|c[i]]+=f[i-1][j]; } } for(register int i=0;i<(1<<20);i++)ans+=f[b-a+1][i]; cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(c==0){ if(a>b)printf("%s\n","Takahashi"); else printf("%s\n","Aoki"); } else{ if(b>a)printf("%s\n","Aoki"); else printf("%s\n","Takahashi"); } }
#include "bits/stdc++.h" using namespace std; int main() { int A, B, C; cin >> A >> B >> C; while (A >= 0 && B >= 0) { if (C) --B; else --A; ++C; C %= 2; } cout << (A >= 0 ? "Takahashi\n" : "Aoki\n"); }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main() { int N; cin >> N; if(N%2==1)cout<<"Black"<<endl; else cout<<"White"<<endl; }
#include <stdio.h> #include <algorithm> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; void AHeavyRotation(std::istream& in, std::ostream& out) { int n; in >> n; out << ((n % 2 == 0) ? "White" : "Black") << endl; } int main(int argc, char** argv) { AHeavyRotation(cin, cout); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(i=0;i<n;i++) #define Rep(i,x,n) for(i=x;i<=n;i++) #define foreach(c,itr) for(__typeof(c)::iterator itr=c.begin();itr!=c.end();itr++) #define all(v) v.begin(),v.end() #define p_b push_back #define fr first #define sc second #define m_p make_pair #define setp setprecision typedef long long ll; typedef long double ld; const ll INF=0x3f3f3f3f; ll gcd(ll x,ll y) {return x%y==0 ? y:gcd(y,x%y);} int i,j; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,x; cin>>n>>x; vector<ll> ans; rep(i,n){ int a; cin>>a; if(a!=x) ans.p_b(a); } rep(i,ans.size()) cout<<ans[i]<<' '; return 0; }
#include <bits/stdc++.h> using namespace std; void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); // freopen((s + ".in").c_str(), "r", stdin); // freopen((s + ".out").c_str(), "w", stdout); } int main() { setIO(""); int n, x; cin >> n >> x; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] != x) cout << a[i] << " "; } }
#include "bits/stdc++.h" #include "random" #include <unistd.h> using namespace std; #define ll long long #define vi vector<int> #define vl vector<long long> #define vvi vector<vi> #define pi pair<int,int> #define mp make_pair #define pb push_back #define MOD int(1e9) + 7 #define PAI 3.1415926535 #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define chmax(x, y) x = max(x,y) #define chmin(x, y) x = min(x,y) #define pr(x) cout << x << endl #define Endl endl #define rep(i, n) for(int i = 0 ; i < n; ++i) const int dx2[4] = {0,1,0,1}; const int dy2[4] = {0,0,1,1}; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; const int ddx[8] = {-1,0,1,-1,1,-1,0,1}; const int ddy[8] = {-1,-1,-1,0,0,1,1,1}; const int inf = 99999999; const ll linf = 1LL << 62; ll gcd(ll a,ll b){ if(a < b)swap(a , b); if(a % b != 0) return(gcd(b, a%b)); return b; } ll lcm(ll a,ll b){ if(a < b)swap(a , b); return (a / gcd(a , b)) * b; } int Uniform_Random(int a, int b){ // (a <= x <= b) random_device rd; mt19937 mt(rd()); uniform_int_distribution<int> rv1(a, b); return rv1(mt); } int main(){ int a, b, c; cin >> a >> b >> c; if(a * a + b * b < c * c) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define f(i,a,b) for( ll i = a; i < b ; i++ ) #define af(i,a,b) for( ll i = a; i >= b ; i--) #define rep(i,a,b,k) for(ll i = a; i < b ; i+= k ) #define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k) #define ones(ini) (ll) __builtin_popcountll(x) #define revll greater<ll> #define revii greater<ii>() #define todo(y,val) all_of(all(y),[&val](ll x) {return (x == ((ll)val));}) #define nada(y,val) none_of(all(y),[&val](ll x) {return (x == ((ll)val));}) #define algu(y,val) any_of(all(y),[&val](ll x) {return (x == ((ll)val));}) #define cuantos(a,x) count(all(a),x) #define fs first #define sc second #define pb push_back #define po pop_back #define sz(a) (ll) a.size() #define all(a) a.begin(), a.end() #define sor(a) sort( a.begin(), a.end() ) #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL) #define ller ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr) #define watch(x) cout << (#x) << " is " << (x) <<"\n" #define test ll deftestcases;cin>>deftestcases;while(deftestcases--) #define PI acos(-1); using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,ll> ii; typedef vector<ll> vi; typedef vector<ii> vii; //template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ; const ll inf = 2e18; const ll mod = 1e9+7; const ll MAX = 1e2+12; ll binpow(ll a,ll b,ll m){ a%=m; ll res = 1; while(b){ if(b&1) res = res*a%m; a = a*a%m; b>>=1; } return res%m; } int main(){ fastio; ll a,b,c; cin>>a>>b>>c; ll ffs = a%10,expo = 0; f(i,2,1e4){ if(binpow(a,i,10)==ffs){ expo = i-1; break; } } ll pos = binpow(b,c,expo); if(!pos) pos = expo; cout<<binpow(a,pos,10)<<"\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll N; cin>>N; vector<ll> A(pow(2,N)); for(int i=0; i<pow(2,N); ++i) cin>>A[i]; // ้ธๆ‰‹ใ‚’ๅŠๅˆ†ใซๅˆ†ใ‘ใ‚‹ // 0-2^(N-1) 2^(N-1)-2-N-1 pair<ll,ll> playerA(0,0), playerB(0,0); for(int i=0; i<pow(2,N-1); ++i) { if(A[i]>playerA.first) playerA={A[i],i+1}; } for(int i=pow(2,N-1); i<pow(2,N); ++i) { if(A[i]>playerB.first) playerB={A[i],i+1}; } if(playerA.first>playerB.first) cout<<playerB.second<<endl; else cout<<playerA.second<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int n; cin >> n; vector<int> a(n), b(n); for (int &it : a) cin >> it; for (int &it : b) cin >> it; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i] * b[i]; } cout << (sum == 0 ? "Yes\n" : "No\n"); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3e6 + 5; ll dp[3][maxn] = {0}; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ll n, k; cin >> n >> k; for (int i = 1; i <= n; i++) dp[0][i] = 1; ll s = 0; for (int i = 2; i <= 2 * n; i++) { s += dp[0][i - 1]; if (i > n + 1) s -= dp[0][i - n - 1]; dp[1][i] = s; } s = 0; for (int i = 3; i <= 3 * n; i++) { s += dp[1][i - 1]; if (i > n + 1) s -= dp[1][i - n - 1]; dp[2][i] = s; dp[2][i] += dp[2][i - 1]; } ll l = 3, r = 3 * n; while (l < r) { ll mid = (l + r) >> 1; if (dp[2][mid] >= k) r = mid; else l = mid + 1; } k -= dp[2][l - 1]; for (int i = 1; i <= n; i++) { if (dp[1][l - i] >= k) { cout << i << " "; for (int j = 1; j <= n; j++) { if (l - i - j <= n) k -= 1; if (k == 0) { cout << j << " " << l - i - j << endl; exit(0); } } } else { k -= dp[1][l - i]; } } }
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define ld long double #define mod 1000000007ll #define md 998244353ll #define e1 1e-9 #define v vector< long long > #define vv vector< vector< long long > > #define p pair < long long,long long > #define vp vector< pair < long long,long long > > #define pb push_back #define mp make_pair #define eb emplace_back #define ft first #define sd second #define all(x) x.begin() , x.end() #define f(i,a,b) for(ll i=a;i<b;i++) #define b(i,a,b) for(ll i=a;i>=0;i--) #define test \ ll tt{0}; \ cin>>tt; \ while(tt--) using namespace std; bool prime(ll n) { if (n < 2)return false; for (ll x = 2; x * x <= n; x++) { if (n % x == 0)return false; } return true; } vector<ll> Seiveprime(ll n) { vector<bool> isPrime(n, true); for (int i = 2; i * i < n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) isPrime[j] = false; } } vector<ll> prime; prime.push_back(2); for (int i = 3; i < n; i += 2) if (isPrime[i]) prime.push_back(i); return prime; } vector<ll> factors(ll n) { vector<ll> f; for (ll x = 2; x*x <= n; x++) { while (n%x == 0) { f.pb(x); n /= x; } } if (n > 1) f.pb(n); return f; } vp pri_fact(ll n) { v x = factors(n); vp y; y.eb(x[0], 1); for (ll i = 1; i < x.size(); i++) { if (x[i] == x[i - 1]) { y.back().second++; } else { y.eb(x[i], 1); } } return y; } tuple<int,int,int> gcd(int a, int b) { if (b == 0) { return {1,0,a}; } else { int x,y,g; tie(x,y,g) = gcd(b,a%b); return {y,x-(a/b)*y,g}; } } ll mult(ll x,ll y,ll m) { return (x * y) % m; } //returns x^n (mod m) ll bin_pow(ll x,ll n,ll m) { if (n == 0)return 1; if (n & 1)return mult(x, bin_pow(x, n - 1, m), m); return bin_pow(mult(x, x, m), n / 2, m); } ll ind_of_2(ll x) { ll i = 0; while (x / (1 << i) != 1) i++; return i; } ll add(ll x,ll y) { x+=y; if(x>=md)return x-md; return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n{0},k{0},aa{0},b{0},c{0}; cin>>n>>k; v a(3*n+1); f(i,3,3*n+1){ a[i]=(i-1)*(i-2)/2; if(i>=n+3)a[i]-=3*(i-n-1)*(i-n-2)/2; if(i>=2*n+3)a[i]+=3*(i-2*n-1)*(i-2*n-2)/2; } ll sum{0},x{0}; f(i,3,3*n+1){ if(sum+a[i]>=k) { x=i; break; } else sum+=a[i]; } k-=sum; ll no{0},z1{0},z2{1}; if(x>=n+2)z1=n; else z1=x-2; if(x>2*n+1)z2=x-2*n; f(i,z2,z1+1){ ll b1{0},b2{0}; if(x-i>n+1) { b1=x-i-n; b2=n; } else { b1=1; b2=x-i-1; } no+=b2-b1+1; if(no>=k) { aa=i; b=k-no+b2; c=x-aa-b; cout<<aa<<" "<<b<<" "<<c; break; } } return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define DB double #define U unsigned #define P std::pair #define LL long long #define LD long double #define pb emplace_back #define MP std::make_pair #define SZ(x) ((int)x.size()) #define all(x) x.begin(),x.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define FOR(i,a,b) for(int i = a;i <= b;++i) #define ROF(i,a,b) for(int i = a;i >= b;--i) #define DEBUG(x) std::cerr << #x << '=' << x << std::endl const int MAXN = 1000+5; int f[MAXN<<1]; inline int find(int x){ return x == f[x] ? x : f[x] = find(f[x]); } inline void merge(int x,int y){ x = find(x);y = find(y); if(x == y) return; f[x] = y; } char str[MAXN]; int n,m; int main(){ scanf("%d%d",&n,&m); FOR(i,1,n+m) f[i] = i; FOR(i,1,n){ scanf("%s",str+1); FOR(j,1,m){ if(str[j] == '#') merge(i,j+n); } } merge(1,n+1); merge(1,n+m); merge(n,n+1); merge(n,n+m); std::vector<int> v1,v2; FOR(i,1,n) v1.pb(find(i)); FOR(i,n+1,n+m) v2.pb(find(i)); std::sort(all(v1));v1.erase(std::unique(all(v1)),v1.end()); std::sort(all(v2));v2.erase(std::unique(all(v2)),v2.end()); int ans = std::min(SZ(v1),SZ(v2))-1; printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; #define MOD 1000000007LL #define INF 1LL << 60 #define rep(i,start,end) for(ll i = start; i < end; i++) #define per(i,end,start) for(ll i = start-1; i >= end; i--) ll N, M; vvl G; ll ct; vl seen; void dfs(ll n) { seen[n] = 1; ct++; for(auto next : G[n]) { if(seen[next] != -1) continue; dfs(next); } } int main() { cin >> N >> M; G.assign(N+1, vl()); rep(i, 0, M) { ll a, b; cin >> a >> b; G[a].push_back(b); } ll res = 0; rep(i, 1, N+1) { ct = 0; seen.assign(N+1, -1); dfs(i); res += ct; } cout << res; }
#include <bits/stdc++.h> using namespace std; using lint = long long; int main() { ios_base::sync_with_stdio(false), cin.tie(0); lint n; cin >> n; lint count = 0; set<lint> s; for (lint i = 2; i * i <= n; i++) { for (int j = 2; j <= 40; j++) { lint tmp = pow(i, j); if (1 <= tmp and tmp <= n) { if (s.find(tmp) == s.end()) { s.insert(tmp); count++; } } } } cout << n - count; return 0; }
#include <bits/stdc++.h> using namespace std; using P = pair<int, int>; int main() { long long n; cin >> n; set<long long> s; for (long long a = 2; a * a <= n; ++a) { long long x = a * a; while ( x <= n) { s.insert(x); x *= a; } } cout << n - s.size() << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for(ll i = 0; i < (ll)(n); ++i) #define FOR(i, a, b) for(ll i=(a); i < (ll)(b); ++i) template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;} template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;} struct kigyo{ int x,y,r; int a,b,c,d; bool flaga,flagb,flagc,flagd; }; int n; vector<kigyo> kigyoes; int menseki(kigyo &k){ return (k.c - k.a)*(k.d - k.b); } void input(){ cin >> n; kigyoes.resize(n); REP(i,n){ cin >> kigyoes[i].x >> kigyoes[i].y >> kigyoes[i].r; //cerr << "input i: " << i << "\n"; } } void output(){ REP(i,n){ cout << kigyoes[i].a << " " << kigyoes[i].b << " "; cout << kigyoes[i].c << " " << kigyoes[i].d << "\n"; //cerr << menseki(kigyoes[i]) - kigyoes[i].r << "\n"; } } bool overlap(int i, int j){ if((kigyoes[i].c <= kigyoes[j].a) || (kigyoes[j].c <= kigyoes[i].a)){ return false; }else if((kigyoes[i].d <= kigyoes[j].b) || (kigyoes[j].d <= kigyoes[i].b)){ return false; }else{ return true; } } bool overlap_all(int i){ REP(j,n){ if(i==j) continue; if(overlap(i,j)) return true; } return false; } /* void increase(int i){ int step = 1; bool flaga = true; bool flagb = true; bool flagc = true; bool flagd = true; while(menseki(kigyoes[i]) <= kigyoes[i].r){ if(flaga){ kigyoes[i].a -= step; } if(overlap_all(i) || kigyoes[i].a < 0){ kigyoes[i].a += step; flaga = false; } if(flagb){ kigyoes[i].b -= step; } if(overlap_all(i) || kigyoes[i].b < 0){ kigyoes[i].b += step; flagb = false; } if(flagc){ kigyoes[i].c +=step; } if(overlap_all(i) || kigyoes[i].c > 9999){ kigyoes[i].c -= step; flagc = false; } if(flagd){ kigyoes[i].d +=step; } if(overlap_all(i) || kigyoes[i].d > 9999){ kigyoes[i].d -= step; flagd = false; } if(!(flaga||flagb||flagc||flagd)){ break; } } } void solve(){ REP(i,n){ kigyoes[i].a = kigyoes[i].x; kigyoes[i].b = kigyoes[i].y; kigyoes[i].c = kigyoes[i].a+1; kigyoes[i].d = kigyoes[i].b+1; } REP(i,n){ increase(i); } }*/ bool increase(int i){ int step = 1; if(kigyoes[i].flaga){ kigyoes[i].a -= step; } if(overlap_all(i) || kigyoes[i].a < 0){ kigyoes[i].a += step; kigyoes[i].flaga = false; } if(menseki(kigyoes[i]) > kigyoes[i].r){ kigyoes[i].flaga = false; } if(kigyoes[i].flagb){ kigyoes[i].b -= step; } if(overlap_all(i) || kigyoes[i].b < 0){ kigyoes[i].b += step; kigyoes[i].flagb = false; } if(menseki(kigyoes[i]) > kigyoes[i].r){ kigyoes[i].flagb = false; } if(kigyoes[i].flagc){ kigyoes[i].c +=step; } if(overlap_all(i) || kigyoes[i].c > 9999){ kigyoes[i].c -= step; kigyoes[i].flagc = false; } if(menseki(kigyoes[i]) > kigyoes[i].r){ kigyoes[i].flagc = false; } if(kigyoes[i].flagd){ kigyoes[i].d +=step; } if(overlap_all(i) || kigyoes[i].d > 9999){ kigyoes[i].d -= step; kigyoes[i].flagd = false; } if(menseki(kigyoes[i]) > kigyoes[i].r){ kigyoes[i].flagd = false; } if(!(kigyoes[i].flaga||kigyoes[i].flagb||kigyoes[i].flagc||kigyoes[i].flagd)){ return false; } return true; } void solve() { REP(i,n){ kigyoes[i].a = kigyoes[i].x; kigyoes[i].b = kigyoes[i].y; kigyoes[i].c = kigyoes[i].a+1; kigyoes[i].d = kigyoes[i].b+1; kigyoes[i].flaga = true; kigyoes[i].flagb = true; kigyoes[i].flagc = true; kigyoes[i].flagd = true; } bool finish = true; while(finish){ finish = false; REP(i,n){ if(increase(i)) finish = true; } } } void calc(){ double sum = 0.0; for(kigyo k:kigyoes){ if((k.a <= k.x && k.x < k.c) && (k.b <= k.y && k.y < k.d)){ int s = menseki(k); double p = 1 - pow(1 - (double)min(k.r,s)/max(k.r,s),2); sum += p/n; }else{ continue; } } cerr << (ll)(sum * 1e9) << "\n"; } int main(){ input(); solve(); calc(); output(); return 0; }
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define all(x) (x).begin(), (x).end() #define pii pair<int, int> #define FF first.first #define FS first.second #define pb push_back using namespace std; const ll N=1000006, INF=1e18, P=998244353; ll q=1, n, m, x, y, z, l, r, k, a[N], b[N], c[50][50], ans, o, u, Ans[N]; vector <int> v[N], V; string s=""; int main(){ios_base::sync_with_stdio(false), cin.tie(0); for (int i=0; i<=30; i++)c[0][i]=c[i][0]=1; for (int i=1; i<=30; i++){ for (int j=1; j<=30; j++) c[i][j]=c[i][j-1]+c[i-1][j]/*, cout<<c[i][j]<<" ";cout<<endl*/;} cin>>x>>y>>n;n--; while(x+y){ if(n<c[x-1][y])s+='a',x--; else n-=c[x-1][y],s+='b',y--; } cout<<s; }
#include <bits/stdc++.h> #define pb push_back typedef long long ll; const int mod = 998244353; using namespace std; ll expo(ll a, ll b) { a %= mod; ll res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } int main() { int n, m, k; cin >> n >> m >> k; ll ans = 0; if (n == 1 or m == 1) ans = expo(k, n + m - 1); else { for (int i = 1; i <= k; ++i) { ans += ((expo(i, m) - expo(i - 1, m) + mod) % mod) * (expo(k - i + 1, n)); ans %= mod; } } cout << ans << endl; }
//================code===================// //#define TLE #ifdef TLE #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #include <random> #include <ctime> #define ci(t) cin>>t #define co(t) cout<<t #define LL long long #define ld long double #define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i) #define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i) #define setp pair<pair<int,int>,int> #define setl pair<LL,LL> #define M_PI 3.14159265358979323846 #define micro 0.000001 using namespace std; #ifdef OHSOLUTION #define ce(t) cerr<<t #define AT cerr << "\n=================ANS=================\n" #define AE cerr << "\n=====================================\n" LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; } LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); } #else #define AT #define AE #define ce(t) #define __popcnt __builtin_popcount #define gcd __gcd #define lcm __lcm #endif pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,1},{1,0}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; } template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; } struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } }; struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } }; const int max_v = 2e5 + 7; const int INF = 1e9 + 7; const LL LNF = (LL)5e18 + 7ll; const LL mod = 998244353; LL cpow(LL x, LL k) { if (!x) return 0; LL ret = 1; while (k) { if (k & 1) ret = ret * x % mod; x = x * x % mod; k >>= 1; } return ret; } int main() { #ifdef OHSOLUTION freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); LL n, m, k; ci(n >> m >> k); LL ans = 0; if (n == 1 || m == 1) { co(cpow(k, n + m - 1)); return 0; } if (n == 1) swap(n, m); fa(i, 1, k + 1) { ans = (ans + cpow(i, n) * ((cpow(k - i + 1, m) - cpow(k - i, m) + mod) % mod) % mod) % mod; } co(ans%mod); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> a(n+1),b(n+1),p(n+1),ip(n+1); for(int i=1;i<=n;i++){cin >> a[i];} //hito for(int i=1;i<=n;i++){cin >> b[i];} //nimotsu for(int i=1;i<=n;i++){ cin >> p[i]; ip[p[i]]=i; } vector<pair<int,int>> pv; vector<pair<int,int>> nm; for(int i=1;i<=n;i++){ nm.push_back(make_pair(b[i],i)); } sort(nm.begin(),nm.end()); for(int i=0;i<n;i++){ int cn=nm[i].second; while(p[cn]!=cn){ int fir=ip[cn]; int sec=ip[fir]; //cout << fir << ' ' << sec << '\n'; if(a[fir]<=b[p[fir]] || a[sec]<=b[p[sec]]){break;} pv.push_back(make_pair(fir,sec)); swap(p[fir],p[sec]); ip[p[fir]]=fir; ip[p[sec]]=sec; } } for(int i=1;i<=n;i++){ if(i!=p[i]){ cout << "-1\n"; return 0; } } cout << pv.size() << '\n'; for(auto &e : pv){ cout << e.first << ' ' << e.second << '\n'; } }
#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; struct edge{ ll to, cost, shuki; }; using Graph = V<V<edge>>; int main(){ ll N, M, X, Y; cin >> N >> M >> X >> Y; X--, Y--; V<ll> A(M), B(M), T(M), K(M); REP(i, M) cin >> A[i] >> B[i] >> T[i] >> K[i], A[i]--, B[i]--; Graph G(N); REP(i, M){ G[A[i]].emplace_back(edge{B[i], T[i], K[i]}); G[B[i]].emplace_back(edge{A[i], T[i], K[i]}); } vector<ll> dist(N, INF); using Pi = pair< ll, int >; priority_queue< Pi, vector< Pi >, greater< Pi > > que; dist[X] = 0; que.emplace(dist[X], X); while(!que.empty()) { ll cost; int idx; tie(cost, idx) = que.top(); que.pop(); if(dist[idx] < cost) continue; for(auto &e : G[idx]) { auto next_cost = ((cost+e.shuki-1) / e.shuki)*e.shuki + e.cost; if(dist[e.to] <= next_cost) continue; dist[e.to] = next_cost; que.emplace(dist[e.to], e.to); } } // dump(dist) cout << (dist[Y] == INF ? -1 : dist[Y]) << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define FORR(i, a, b) for (int i = b - 1; i >= a; --i) #define SORT(v) sort(v.begin(), v.end()) #define SORTR(v) sort(v.rbegin(), v.rend()) #define REV(v) reverse(v.begin(), v.end()) #define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr) #define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) #define SZ(v) (int)v.size() using namespace std; using ll = long long; using P = pair<int, int>; const int mod = 1000000007; // const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x = 0) :x((x% mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { 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; } struct combination { vector<mint> fact, ifact; combination(int n) :fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; class Main { int H, W, K; vector<string> S; void input() { cin >> H >> W; S.resize(H); K = 0; REP(i, H) { cin >> S[i]; REP(j, W) { if (S[i][j] == '.') ++K; } } } void output() { vector<vector<ll>> memo(H, vector<ll>(W)); REP(i, H) { REP(j, W) { int o = j; while (j < W && S[i][j] == '.') ++j; FOR(k, o, j) { memo[i][k] += j - o; } } } REP(j, W) { REP(i, H) { int o = i; while (i < H && S[i][j] == '.') ++i; FOR(k, o, i) { memo[k][j] += i - o - 1; } } } mint res = 0; vector<mint> pow(K + 1); pow[0] = 1; REP(i, K) { pow[i + 1] = mint(2) * pow[i]; } REP(i, H) { REP(j, W) { if (S[i][j] == '#') continue; res += pow[K] - pow[K - memo[i][j]]; } } cout << res << endl; } public: Main() { input(); output(); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Main(); return 0; }
#include <bits/stdc++.h> #define ff first #define ss second #define endl '\n' using namespace std; const long long INF = (long long) 1e18; const int mod = (int) 1e9+7; const int MAXN = (int) 3e5+5; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; ll n, a, b; void solve(){ cin>>n>>a>>b; if(b > a) swap(a, b); ll fr = (n - a + 1)*(n - a + 1) % mod; ll sc = (n - b + 1)*(n - b + 1) % mod; ll mn = min(a + b - 1, n); ll sub = (a - mn + mod) % mod * ((mn + a - 2*n%mod - 1 + 3LL*mod)%mod) % mod * (a - mn + mod) % mod * ((mn + a - 2*n - 1 + 3LL*mod)%mod) % mod; sub += (-2*(a - b + 1)*((a - n - 1 + 2*mod)%mod )%mod) * ((a - mn + mod)%mod) % mod * ((mn + a - 2* n -1 + 3LL*mod)%mod) % mod; sub %= mod; sub += (a - b + 1) * (a - b + 1) % mod * fr % mod; sub %= mod; cerr<<fr<<" "<<sc<<" "<<sub<<endl; ll ans = (fr*sc%mod - sub + mod) % mod; cout<<ans<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); #ifdef Local freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin); freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout); #endif int t; cin>>t; while(t--) solve(); #ifdef Local cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds "; #endif }
#include <bits/stdc++.h> using namespace std; int main() { int64_t R, X, Y; cin >> R >> X >> Y; double distance = sqrt(X*X + Y*Y); int ans = ceil(distance / R); if (distance < R) ans = 2; cout << ans; }
#include <bits/stdc++.h> #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using namespace std; //#define int long long typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; typedef vector<vector<int>> graph; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define MOD 1000000007 #define MAXX 1.1529215e+18 #define _1 first #define _2 second #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() #define si(x) int(x.size()) template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} template<class t> using vc=vector<t>; template<class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template<class T> T extgcd(T a, T b, T &x, T &y) { T d = a; if (b == 0) { x = 1; y = 0; } else { d = extgcd(b, a%b, y, x); y -= (a/b)*x; } return d; } template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p._1<<","<<p._2<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<class T> void g(T &a){ cin >> a; } template<class T> void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } int main() { cin.tie(0); ios::sync_with_stdio(false); ll R, X, Y; cin >> R >> X >> Y; ll ans = (X*X+Y*Y+R*R-1)/(R*R); // cout << ans << endl; ll i = 0; for (; i*i< ans; i++); if (i == 1) { if (X*X+Y*Y == R*R) { cout << 1 << endl; } else { cout << 2 << endl; } } else { cout << i << endl; } }