code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include<bits/stdc++.h> using namespace std; int n,k; long long get(int x) { //x-r>=1 r<=x-1 x-l<=n l>=x-n return min(n,x-1)-max(1,x-n)+1; } int main() { scanf("%d%d",&n,&k); long long res=0; for(int i=2;i<=2*n;i++) { int t=i-k; if(t>=2&&t<=2*n) { res+=get(i)*get(t); } } cout<<res; }
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define FOR(index, start, end) for(int index = start; index < end; ++index) #define F0R(index, end) for(int index = 0; index < end; ++index) #define RFOR(index, start, end) for(int index = start; index > end; --index) #define FOREACH(itr, b) for(auto itr = b.begin(); itr != b.end(); ++itr) #define RFOREACH(itr, b) qfor(auto itr = b.rbegin(); itr != b.rend(); ++itr) #define db(x) cerr << #x << " = " << x << endl #define db2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl #define db3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl #define ri(x) scanf("%d", &x) #define ri2(x, y) scanf("%d%d", &x, &y) #define ri3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define rll(x) scanf("%lld", &x) #define mp make_pair #define sz(x) (int)x.size() #define pf push_front #define pb push_back using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pld = pair<ld ,ld>; using vi = vector<int>; using vld = vector<ld>; using vll = vector<ll>; //--------------------------------------------------------------------------------------------------------------- #define INF 1000000000 #define P 257 #define M 1000000007 #define MAXN 200005 int n, k; int cnt(int x){ if (x <= n) return x - 1; return min(x - 1, abs(x - n - n) + 1); } void solve(){ ll ans = 0; FOR(i, 2, 2 * n + 1){ int j = i - k; if (1 <= j && j <= 2 * n){ ans += (ll)cnt(i) * cnt(j); } } cout << ans << endl; } void read(){ ri2(n, k); } int main(){ //ios_base::sync_with_stdio(false); //cin.tie(NULL); read(); solve(); /* int tests; ri(tests); FOR(t, 0, tests){ read(); solve(); } */ }
#include<bits/stdc++.h> using namespace std; ////////////////////////////////////////////////////////////////////////////// void files(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);} void speed(){ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);} int lcm(int a,int b){return (a*b)/__gcd(a,b);} int fib(int n){return (pow(1+sqrt(5),n)-pow(1-sqrt(5),n))/(pow(2,n)*sqrt(5));} ////////////////////////////////////////////////////////////////////////////// int main() { /// --- Your code here --- /// string a,b;cin>>a>>b; cout<<max((a[0]-'0')+(a[1]-'0')+(a[2]-'0'),(b[0]-'0')+(b[1]-'0')+(b[2]-'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); string s1,s2; cin>>s1>>s2; ll a1=0,a2=0; REP(i,3){ a1+=(s1[i]-'0'); a2+=(s2[i]-'0'); } cout<<max(a1,a2)<<endl; }
#include<bits/stdc++.h> #define mod 998244353 #define ll long long #define N 400010 using namespace std; inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f*=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+int(ch-48); ch=getchar(); } return x*f; } int n,m; int tr[N]; int low(int x){ return x&-x; } void add(int x,int val){ for(int i=x;i<=n;i+=low(i))tr[i]+=val; } int query(int x){ int sum=0; for(int i=x;i;i-=low(i))sum+=tr[i]; return sum; } struct hh{ int id,x; }edge[N]; bool operator<(hh a,hh b){ return a.x<b.x; } int a[N],b[N],val[N],cnt,vis[N]; queue<int>q[N]; char c[N]; void solve(){ // freopen("a.in","r",stdin); cin>>n; n*=2; for(int i=1;i<=n;i++)a[i]=read(),edge[i]=(hh){i,a[i]}; sort(edge+1,edge+1+n); for(int i=1;i<=n;i++)a[edge[i].id]=i; int lastB=0,lastS=0; for(int i=1;i<=n;i++){ if(a[i]>n/2){ if(lastB||(!lastS))c[i]='(',lastB++; else c[i]=')',lastS--; } else{ if(lastB)c[i]=')',lastB--; else c[i]='(',lastS++; } } for(int i=1;i<=n;i++)putchar(c[i]); } int T; int main(){ if(0)cin>>T; else T=1; while(T--)solve(); }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < (t); ++i) #define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cerr<<#x<<" = "<<x<<endl; #define bn(x) ((1<<(x))-1) #define newline puts("") using namespace std; template<typename T> using vc = vector<T>; template<typename T> using vv = vc<vc<T>>; template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >; using ll = long long; using uint = unsigned; using ull = unsigned long long; using P = pair<int,int>; using T3 = tuple<int,int,int>; using vi = vc<int>; using vvi = vv<int>; using vl = vc<ll>; using vp = vc<P>; using vt = vc<T3>; int getInt(){int x;scanf("%d",&x);return x;} vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;} template<typename T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();} template<typename T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;} template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;} template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;} vc<string> split(const string& s,char d=' '){vc<string> r(1);for(char c:s)if(c==d)r.pb("");else r.back()+=c;return r;} string operator*(const string& s,int t){return join(vc<string>(t,s));} template<typename T1,typename T2>bool mins(T1& x,const T2&y){if(x>y){x=y;return true;}else return false;} template<typename T1,typename T2>bool maxs(T1& x,const T2&y){if(x<y){x=y;return true;}else return false;} template<typename T>T dup(T x, T y){return (x+y-1)/y;} template<typename T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;} template<typename T>void uni(T& a){sort(rng(a));a.erase(unique(rng(a)),a.end());} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; struct Solver { void solve() { int n; scanf("%d",&n); n *= 2; vi a(n); cin>>a; vp p(n); rep(i,n) p[i] = P(a[i],i); sort(rng(p)); vi s(n,1); rep(i,n/2) s[p[i].se] = -1; string ans; int cnt = 0; rep(i,n) { if (cnt*s[i] >= 0) ans += '('; else ans += ')'; cnt += s[i]; } cout<<ans<<endl; } }; int main() { int ts = 1; // scanf("%d",&ts); rrep(ti,ts) { Solver solver; solver.solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(), (x).end() #define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin()) #define trace(...) __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...); } ll max(ll a, ll b) {return (a > b ? a : b);} ll min(ll a, ll b) {return (a < b ? a : b);} ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; void yes_or_no(ll x) { if (x) cout << "YES\n"; else cout << "NO\n"; } void solve_test() { ll ans = 0; ll a, b, c, d; cin >> a >> b >> c >> d; ans = a * d - b * c; cout << ans << "\n"; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); solve_test(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long double x, y, r; cin >> x >> y >> r; r = nextafter(r, INFINITY); long long ans = 0; for (long long i = ceil(x - r); i <= floor(x + r); i++){ long double p = sqrt(r * r - (x - i) * (x - i)); ans += floor(y + p) - ceil(y - p) + 1; } cout << ans << endl; return 0; }
// 2021-03-06 21:04:00 // clang-format off #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; } template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; // clang-format on const int inf = 1e9; void answer() { int n; cin >> n; vector<int> a(n), b(n); rep(i, n) cin >> a[i] >> b[i]; int ans = inf; rep(i, n) rep(j, n) { if (i == j) { chmin(ans, a[i] + b[i]); } else { chmin(ans, max(a[i], b[j])); } } cout << ans << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, k; int main() { cin >> n >> k; for (int i = 0; i < k; i++) { vector<int> g; while (n > 0) { g.push_back(n % 10); n /= 10; } sort(g.begin(), g.end()); int u = 0; for (int j : g) { u *= 10; u += j; } reverse(g.begin(), g.end()); int v = 0; for (int j : g) { v *= 10; v += j; } n = v - u; } cout << n << endl; }
#include <algorithm> #include <iostream> #include <string> #include <functional> #include <vector> #include <numeric> #include <iomanip> #include <utility> #include <cmath> #include <climits> #include <queue> #include <bitset> #include <set> #include <assert.h> using namespace std; typedef vector<vector<long> > dim2veclo; typedef vector<vector<int> > dim2vecin; typedef vector<vector<double> > dim2vecdo; typedef vector<vector<long long> > dim2vecll; typedef vector<int> vecin; typedef vector<long> veclo; typedef vector<double> vecdo; typedef vector<long long> vecll; typedef vector<bool> vecbo; typedef vector<vector<bool> > dim2vecbo; typedef long long ll; typedef vector < pair<ll, ll> > vecpa; typedef vector<string> vecst; typedef pair<ll, ll> pail; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, 1, 0, -1 }; const ll INF = 100000000000000; int main() { int x, y, z; cin >> x >> y >> z; if (y * z % x == 0) cout << y * z / x - 1; else cout << y * z / x; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 998244353; ll pow_mod(ll a, ll n, ll mod) { ll res=1; while(n>0){ if(n&1) res=res*a%mod; a=a*a%mod; n/=2; } return res; } int main() { ll N,M,K; cin>>N>>M>>K; ll ans = 0; if(N==1 || M==1) ans=pow_mod(K,M+N-1,MOD); else { for(int i=1; i<=K; ++i) { ll result1=(pow_mod(i,N,MOD)-pow_mod(i-1,N,MOD)+MOD)%MOD; ll result2=pow_mod(K-i+1,M,MOD); ans=(ans+result1*result2)%MOD; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll ; #define rep(i,a,n) for(int i=a ; i<n ; i++) #define pb push_back vector<vector<int> > adj ; vector<ll> val,m ; vector<bool> vis ; ll ans ; ll dfs(int x) { if(vis[x]) return m[x] ; ll ma=-1e9 ; vis[x]=1 ; rep(i,0,adj[x].size()) { ma=max(ma,dfs(adj[x][i])) ; } ans=max(ans,ma-val[x]) ; ma=max(ma,val[x]) ; m[x]=ma ; return m[x] ; } int main() { int T=1; // cin >> T ; while(T--) { int n,me ; cin>>n>>me ; adj.resize(0) ; adj.resize(n) ; val.resize(n) ; rep(i,0,n) cin>>val[i] ; m.resize(n) ; rep(i,0,n) m[i]=val[i] ; int x,y ; rep(i,0,me) { cin>>x>>y ; x-- ; y-- ; adj[x].pb(y) ; } vis.resize(0) ; vis.resize(n) ; rep(i,0,n) vis[i]=0 ; ans=-1e9 ; rep(i,0,n) { if(!vis[i]) { dfs(i) ; } } cout<<ans<<endl ; } return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long; int main() { int N, M; cin >> N >> M; vector<vector<int>> adj(N); vector<int> A(N); for (auto &i : A) cin >> i; for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; adj[--u].emplace_back(--v); } vector<lint> dp(N, -1e18); lint ans = -1e18; for (int i = N - 1; i >= 0; i--) { for (auto j : adj[i]) { ans = max(ans, -A[i] + dp[j]); } dp[i] = max(dp[i], 1ll * A[i]); for (auto j : adj[i]) { dp[i] = max(dp[i], dp[j]); } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<int> A(N),B(N); int ans=1e9; for(int i=0;i<N;i++){ cin >> A.at(i) >> B.at(i); } for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ if(i==j){ ans=min(ans,A.at(i)+B.at(j)); }else{ ans=min(ans,max(A.at(i),B.at(j))); } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i,n) for(int i = 0; i < (n); ++i) #define rep1(i,n) for(int i = 1; i <= (n); ++i) // #define MOD 998'244'353 // #define MOD 1'000'000'007 // #define INF 1'000'000'000'000'000'000 using namespace std; // using namespace atcoder; typedef long long ll; int main(){ int N; cin >> N; vector<pair<int, int> > A(N); vector<pair<int, int> > B(N); // vector<int> A(N); // vector<int> B(N); rep(i, N){ cin >> A[i].first >> B[i].first; A[i].second = i; B[i].second = i; } // rep(i, N) cin >> A[i] >> B[i]; sort(A.begin(), A.end()); sort(B.begin(), B.end()); int ans; if (A[0].second != B[0].second){ ans = max(A[0].first, B[0].first); }else{ ans = A[0].first + B[0].first; ans = min(ans, max(A[1].first, B[0].first)); ans = min(ans, max(A[0].first, B[1].first)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long N; vector<long long> divisor(long long n) { vector<long long> res; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (i != n / i) { res.push_back(n / i); } } } return res; } bool check(long long l) { long long v = 2 * N / l - l + 1; if (abs(v) % 2 == 0) { return true; } else { return false; } } int main() { cin >> N; int ans = 0; vector<long long> L = divisor(N); for (long long i = 0; i < int(L.size()); ++i) { if (check(L[i])) { ans++; } long long l2 = N * 2 / L[i]; if (i != l2) { if (check(l2)) { ans++; } } } cout << ans << endl; return 0; }
#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <map> using namespace std; //int N=1; int P[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int binary(int bina) { int ans = 0; for (int i = 0; bina > 0; i++) { ans = ans + (bina % 2) * pow(10, i); bina = bina / 2; } return ans; } int rev(int original) { int reversed = 0; while (original > 0) { reversed = original % 10 + reversed * 10; original = original / 10; } return reversed; } int main() { int n, cou = 0; cin >> n; for (int i = 0;i < 10;i++) { if (n % 10 == 0) { n /= 10; } else { break; } } if (n == rev(n)) cout << "Yes"; else cout << "No"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i) static const ll INFTY = 1L << 62L; ll solver(vector<ll> &h, ll n, ll *w, ll m) { ll j, k, answer = INFTY, ans; sort(h.begin(), h.end()); //r1: 0,h[1]-h[0],h[3]-h[2],h[5]-h[4],...,h[n-2]-h[n-3]要素数は(n-1)/2+1 //r2: 0,h[2]-h[1],h[4]-h[3],h[6]-h[5],...,h[n-1]-h[n-2] //r1[k]=r1[k-1]+h[2k-1]-h[2k-2],k>=1 //r2[k]=r2[k-1]+h[2k]-h[2k-1],k>=1 vector<ll> r1, r2; r1.push_back(0); r2.push_back(0); rep(i, 0, n - 1) { if (i % 2 == 0) r1.push_back(r1.back() + h[i + 1] - h[i]); else r2.push_back(r2.back() + h[i + 1] - h[i]); } rep(i, 0, m) { ans = 0; j = lower_bound(h.begin(), h.end(), w[i]) - h.begin(); //jが偶数のとき,h[1]-h[0],...,h[j-1]-h[j-2],h[j]-w[i],h[j+2]-h[j+1],... if (j % 2 == 0) { ans += r1[j / 2]; //h[1]-h[0],...,h[j-1]-h[j-2] ans += h[j] - w[i]; ans += r2.back() - r2[j / 2]; //h[j]-h[j-1]は含まれない } //jが奇数のとき else { ans += r1[(j - 1) / 2]; //h[1]-h[0],...,h[j-2]-h[j-3] ans += w[i] - h[j - 1]; ans += r2.back() - r2[(j - 1) / 2]; //h[j]+h[j+1],... } answer = min(ans, answer); } return answer; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<ll> h(n); ll w[m]; rep(i, 0, n) cin >> h[i]; rep(i, 0, m) cin >> w[i]; cout << solver(h, n, w, m) << endl; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string(string(s)); } string to_string(bool b) { return to_string(int(b)); } string to_string(vector<bool>::reference b) { return to_string(int(b)); } string to_string(char b) { return "'" + string(1, b) + "'"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { string res = "{"; for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x); return res + "}"; } void debug() { cerr << endl; } template <typename Head, typename... Tail> void debug(Head H, Tail... T) { cerr << " " << to_string(H); debug(T...); } #define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__) #else #define db(...) 42 #endif typedef long long ll; typedef long double ld; int main() { int n, m; scanf("%d%d", &n, &m); vector<pair<int, int>> H(n); for (int i = 0; i < n; ++i) { scanf("%d", &H[i].first); } for (int i = 0; i < m; ++i) { int w; scanf("%d", &w); H.push_back({w, 1}); } sort(H.begin(), H.end()); int las = 0; ll s = 0; vector<pair<ll, int>> suf(H.size() + 1); for (int i = (int)H.size() - 1; i >= 0; --i) { if (H[i].second == 0) { if (las == 0) las = H[i].first; else { s += las - H[i].first; las = 0; } suf[i] = {s, las}; } else suf[i] = suf[i + 1]; } las = s = 0; ll ans = LLONG_MAX; for (int i = 0; i < H.size(); ++i) { int v = H[i].first; int t = H[i].second; if (t == 1) { ll sum = suf[i + 1].first + s; if (suf[i + 1].second) { sum += suf[i + 1].second - v; } else { sum += v - las; } ans = min(ans, sum); } else { if (las == 0) las = v; else { s += v - las; las = 0; } } } printf("%lld\n", ans); }
#include <vector> #include <array> #include <stack> #include <queue> #include <list> #include <bitset> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <numeric> #include <iostream> #include <iomanip> #include <string> #include <chrono> #include <random> #include <cmath> #include <cassert> #include <climits> #include <cstring> #include <cstdlib> #include <functional> #include <sstream> using namespace std; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); int n; cin >> n; vector<long long> A(n); for (int i = 0; i < n; ++i) { cin >> A[i]; } long long sum = 0; long long pre = 0; long long res = 0; long long mx = 0; for (int i = 0; i < n; ++i) { mx = max(mx, A[i]); res = mx * (i + 1); pre += A[i]; sum += pre; res += sum; cout << res << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long using vec_int = vector<int>; using P = pair<int,int>; using T = tuple<int,int,int>; using ll = long long; #define rep(i, n) for(int i = 0; i < (int)(n); i++) int charToInt(char c){ char zero_num = '0'; return (int)c - (int)zero_num; } void cout_line(vector<int> &a){ for(int i=0;i<a.size();i++){ if(i<a.size()-1){ cout<<a.at(i)<<" "; }else{ cout<<a.at(i)<<endl; } } } signed main(){ int N; cin>>N; vec_int A(N); rep(i,N)cin>>A.at(i); vec_int cumul(N+1, 0); vec_int max_val(N+1, 0); rep(i,N){ cumul.at(i+1) = cumul.at(i)+ A.at(i); max_val.at(i+1) = max(A.at(i), max_val.at(i)); } int prev_ans = 0; for(int i=0;i<N;i++){ int tot_sum = cumul.at(i+1); tot_sum += prev_ans; tot_sum += (i+1)*max_val.at(i+1); cout<<tot_sum<<endl; prev_ans = tot_sum-(i+1)*max_val.at(i+1); } return 0; }
// Problem: A - Rock-paper-scissors // Contest: AtCoder - AtCoder Beginner Contest 204 // URL: https://atcoder.jp/contests/abc204/tasks/abc204_a // Memory Limit: 1024 MB // Time Limit: 2000 ms #include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; if (x == y) cout << x; else cout << 3 - x - y; return 0; }
#include<iostream> using namespace std; int main(){ int x, y; cin >> x >> y; if ((x==1&&y==2)||(x==2&&y==1)||(x==0&&y==0)) cout<<0; else if ((x==0&&y==2)||(x==2&&y==0)||(x==1&&y==1)) cout<<1; else cout<<2; return 0; }
#pragma region #ifdef LOCAL #include "pch.h" #else #include <bits/stdc++.h> #endif // debug template<typename ... Types> void __read(Types& ... args) { ((std::cin >> args), ...); } #define read(type, ...) type __VA_ARGS__; __read(__VA_ARGS__) template <typename Type, typename... Types> auto print(Type const &arg, Types const &... args) { std::cout << arg, ((std::cout << " " << args), ...), std::cout << std::endl; } template <typename Type, typename... Types> auto print_debug(Type const &arg, Types const &... args) { std::cout << arg, ((std::cout << " | " << args), ...), std::cout << std::endl; } #ifdef LOCAL #define debug(...) do{std::cout << __LINE__ << ": [" << #__VA_ARGS__ << "]: ", print_debug(__VA_ARGS__);}while(0) #else #define debug(...) 1 #endif #define __DEF_PRINTER(container) \ template<typename T> \ std::ostream& operator<<(std::ostream& out, container<T> const& a) \ { \ size_t len = a.size(); \ for(auto x : a) out << x << " \n"[len--==0]; \ return out; \ } __DEF_PRINTER(std::vector) __DEF_PRINTER(std::set) __DEF_PRINTER(std::unordered_set) // end debug #define ALL(container) std::begin(container), std::end(container) #define RANGE(container, lo, hi) std::begin(container)+(lo), std::begin(container)+(hi) const char nl = '\n'; using std::cout; using std::cin; template <typename T> void minmax(T k1, T k2, T& min, T& max) { min = std::min(k1, k2); max = std::max(k1, k2); } std::vector<int> iota(int size) { std::vector<int> res(size); std::iota(res.begin(), res.end(), 0); return res; } template <typename Iter> auto counter(Iter from, Iter to) { std::map<typename Iter::value_type, int> res; while(from != to) res[*from++]++; return res; } const long long mod = 1e9+7; long long reduce_mod(long long a) { return (a % mod + mod) % mod; } long long add(long long a, long long b) { return reduce_mod(a + b); } long long mult(long long a, long long b) { return reduce_mod(a * b); } std::string yn(bool yes) { return yes ? "yes" : "no"; } #pragma endregion int main() { read(int, n); int min = -1; int max = -1; for (int i = 0; i < n; i++) { read(int, b); // debug(b); max = max == -1 ? b : std::max(max, b); } for (int i = 0; i < n; i++) { read(int, a); // debug(a); min = min == -1 ? a : std::min(min, a); } // debug(min, max); if (min < max) { print(0); } else { print(min-max+1); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; #define all(v) begin(v), end(v) #define sz(v) (int)(v).size() #define fi first #define se second const int md = 998244353; namespace modular { inline int mul() { return 1; } template <typename... V> inline int mul(int a, V... v) { #if !defined(_WIN32) || defined(_WIN64) return (int) ((long long) a * mul(v...) % md); #endif unsigned long long x = (long long) a * mul(v...); unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m; asm( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (md) ); return m; } template<typename... V> inline void mulby(int &a, V... v) { a = mul(a, v...); } inline int add() { return 0; } template<typename... V> inline int add(int x, V... v) { x += add(v...); if(x >= md) x -= md; return x; } template<typename... V> inline void addto(int &a, V... v) { a = add(a, v...); } inline int sub(int a, int b) { if((a -= b) < 0) a += md; return a; } inline void subfrom(int &a, int b) { if((a -= b) < 0) a += md; } inline int qpow(int b, int p) { if(p < 0) { return qpow(qpow(b, -p), md-2); } int ret = 1; while(p) { if(p & 1) mulby(ret, b); mulby(b, b); p >>= 1; } return ret; } } using namespace modular; int main(int argc, char const *argv[]) { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); #ifdef LOCAL freopen("in", "r", stdin); #endif int n, m; cin >> n >> m; vector<vi> pw(m+1, vi(n+1, 1)); for(int i = 0; i <= m; ++i) { for(int j = 1; j <= n; ++j) { pw[i][j] = mul(pw[i][j-1], i); } } vi sum(n, 0); for(int i = 0; i < n; ++i) { for(int x = 1; x <= m; ++x) { addto(sum[i], pw[m-x][i]); } } int ans = 0; for(int i = 1; i <= n; ++i) { for(int j = i+1; j <= n; ++j) { addto(ans, mul(sum[j-i-1], pw[m][n-(j-i)-1])); } } cout << sub(mul(n, pw[m][n]), ans) << endl; return 0; } /* * use std::array instead of std::vector, if u can * overflow? * array bounds */
#include <bits/stdc++.h> #define ll long long #define maxn 100005 #define mod 1000000007 #define pii pair<int, int> #define ff first #define ss second #define io ios_base::sync_with_stdio(0);cin.tie(0); using namespace std; ll a[maxn], b[maxn]; priority_queue<pii,vector<pii>, less<pii> > pq; int main() { io ll k, n, m; cin >> k >> n >> m; for (int i = 0;i < k;i++) cin >> a[i], a[i] *= m; ll tot = 0; for (int i = 0;i < k;i++) { b[i] = a[i] - a[i] % n; tot += b[i]; pq.push(make_pair(a[i] - b[i], i)); } while (tot < n * m) { pii cur = pq.top(); pq.pop(); tot += n; b[cur.ss] += n; } for (int i = 0;i < k;i++) cout << b[i] / n << " "; cout << endl; } /* */
#include <algorithm> #include <iomanip> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <string> #include <sstream> #include <unordered_map> #include <unordered_set> #include <utility> #include <numeric> #include <vector> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif #define GET_MACRO(_1, _2, _3, NAME, ...) NAME #define _rep(i, n) _rep2(i, 0, n) #define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++) #define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using i64 = long long; template<class T, class U> bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; } template<class T, class U> bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; } template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;} template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);} template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;} template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;} template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;} template<class T> using pq = priority_queue<T, vector<T>, greater<T>>; int main() { int k, n, m; cin >> k >> n >> m; vector<long double> a(k); vector<int> b(k); vector<pair<long double, int>> p(k); cin >> a; rep(i, k) b[i] = (a[i] * m) / n + 0.001; int r = m - accumulate(all(b), 0); rep(i, k) { p[i].first = b[i] - (a[i] * m) / n; p[i].second = i; } sort(all(p)); rep(i, r) { b[p[i].second]++; } cout << b << endl; return 0; }
// '-.-' // () __.'.__ // .-:--:-. |_______| // () \____/ \=====/ // /\ {====} )___( // (\=, //\\ )__( /_____\ // __ |'-'-'| // .\ ( ) /____\ | | // / \ |_____| (( \_ \ )__( | | | | // \__/ |===| )) `\_) /____\ | | | | // /____\ | | (/ \ | | | | | | // | | | | | _.-'| | | | | | | // |__| )___( )___( /____\ /____\ /_____\ // (====) (=====) (=====) (======) (======) (=======) // }===={ }====={ }====={ }======{ }======{ }======={ // (______)(_______)(_______)(________)(________)(_________) // // Credits :- Joan G. Stark //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| //| AUTHOR - KASPAROVIAN | //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| #include<bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) #define frr(i,n) for(int i=0;i<(n);i++) #define pb push_back #define eb emplace_back #define all(v) (v).begin(),(v).end() #define fr first #define sc second #define mk make_pair #define endl '\n' #define MOD 1000000007 #define in insert #define sz(x) (ll)(x).size() #define mem(a,b) memset(a,b,sizeof(a)) //#define int long long #define runtime() ((double)clock() / CLOCKS_PER_SEC) #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef long double ld; typedef pair<ll,ll> pl; typedef pair<int,int> pi; typedef pair<int,pi> ppi; typedef vector<vi> graph; template<class T> void mxi(T & a, const T & b) { a = max(a, b); } template<class T> void mni(T & a, const T & b) { a = min(a, b); } ld EPS=1e-9; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); #define SHUF(v) shuffle(all(v), RNG); // Use mt19937_64 for 64 bit random numbers. void solve() { ld cur=0; int n; cin>>n; for(int i=n-1;i>=1;i--) { ld ncur=(ld)(((ld)n-(ld)i)*(1.0+cur)+(ld)i)/((ld)n-i); cur=ncur; } cout<<fixed<<setprecision(12)<<cur<<endl; } signed main() { fast; int t,tab; t=1; tab=t; while(t--) { //cout<<"Case #"<<(tab-t)<<": "; solve(); } cerr<<runtime(); } //APPROACHING A QUESTION //+ Think of binary search (max of min etc also if n<=2*10^5) //+ Think of common dp states (Even if it appears as maths but constraints are small) //+ Check constraints //+ Keep calm and enjoy the question //+ Be sure to remove MOD from binpow (if needed) //+ Try bidirectional analysis for constructive questions //+ If given some sequence try thinking of prefix sums //+ If constraints are too large maybe its simple maths //+ In questions with binary operations think of bits independently and also the change pattern //+ If two or more binary operations are given mostly there is a relation between them and an arithmatic operator
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<vector> #include<cstring> #include<map> #include<set> #include<cstdlib> #include<bitset> using namespace std; #define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) typedef long long ll; int main(void) { ll n; cin>>n; double ans=0; for(ll i=1;i<n;i++) { ans+=(double)(n)/(double)(i); } printf("%.10lf",ans); }
#include <iostream> #include <vector> #include <string> #include <math.h> using namespace std; int main() { int H, W, X, Y, count = 0; cin >> H >> W >> X >> Y; vector<string> S(H); for (int i = 0; i < H; i++) cin >> S.at(i); if (S.at(X - 1).at(Y - 1) == '#') goto finish; else count++; for (int i = X - 2; i >= 0; i--) { if (S.at(i).at(Y - 1) == '#') break; else count++; } for (int i = X; i < H; i++) { if (S.at(i).at(Y - 1) == '#') break; else count++; } for (int j = Y - 2; j >= 0; j--) { if (S.at(X - 1).at(j) == '#') break; else count++; } for (int j = Y; j < W; j++) { if (S.at(X - 1).at(j) == '#') break; else count++; } finish: cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,(n)-1,0) #define all(v) v.begin(), v.end() #define endk '\n' const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; const ll mod2 = 998244353; const ld eps = 1e-10; 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;} int main() { cin.tie(0); ios::sync_with_stdio(false); int h, w, n, m; cin >> h >> w >> n >> m; vector<string> board(h, string(w, ' ')); vector<int> A(n), B(n), C(m), D(m); rep(i, n) { cin >> A[i] >> B[i]; A[i]--; B[i]--; board[A[i]][B[i]] = '.'; } rep(i, m) { cin >> C[i] >> D[i]; C[i]--; D[i]--; board[C[i]][D[i]] = '#'; } set<pair<int, int>> st; rep(i, h) { int l = 0, r = 0; while(l < w) { while(l < w && board[i][l] == '#') l++; bool ok = false; r = l; while(r < w && board[i][r] != '#') { if(board[i][r] == '.') ok = true; r++; } if(ok) { for(int j=l; j<r; j++) { st.insert({i, j}); } } l = r; } } rep(i, w) { int l = 0, r = 0; while(l < h) { while(l < h && board[l][i] == '#') l++; bool ok = false; r = l; while(r < h && board[r][i] != '#') { if(board[r][i] == '.') ok = true; r++; } if(ok) { for(int j=l; j<r; j++) { st.insert({j, i}); } } l = r; } } cout << st.size() << endk; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; int64_t x, c; cin >> n; vector<vector<int64_t>> balls(n + 1, vector<int64_t>(0)); for (size_t i = 0; i < n; i++) { cin >> x >> c; balls[c].push_back(x); } for (int i = 1; i <= n; i++) { sort(balls[i].begin(), balls[i].end()); } const int64_t INF = 1LL << 60; vector<vector<int64_t>> dp(n + 1, vector<int64_t>(2, INF)); int64_t l = 0, r = 0, cost_l = 0, cost_r = 0; for (int64_t i = 1; i <= n; i++) { if (balls[i].empty()) continue; auto l2 = balls[i][0]; auto r2 = balls[i][balls[i].size() - 1]; int64_t cost_l2 = INF, cost_r2 = INF; // l->r2->l2 cost_l2 = min(cost_l2, cost_l + abs(r2 - l2) + abs(l - r2)); // r->r2->l2 cost_l2 = min(cost_l2, cost_r + abs(r - r2) + abs(r2 - l2)); // l->l2->r2 cost_r2 = min(cost_r2, cost_l + abs(l - l2) + abs(r2 - l2)); // r->l2->r2 cost_r2 = min(cost_r2, cost_r + abs(r - l2) + abs(r2 - l2)); l = l2; r = r2; cost_l = cost_l2; cost_r = cost_r2; } int64_t ans = min(cost_l + abs(l), cost_r + abs(r)); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < (n); i++) #define ALL(a) (a).begin(),(a).end() #define SORT(a) sort((a).begin(),(a).end()) #define RSORT(a) sort((a).rbegin(),(a).rend()) #define ll long long #define pb(a) push_back(a) #define PRINTV(v) {for(ll deB=0; deB<(v).size(); deB++){cout << (v)[deB] << " ";}cout << endl;} int main(){ ll N; cin >> N; vector<ll> X(N), C(N); REP(i,N){ cin >> X[i] >> C[i]; } vector<ll> cnum(N, 0); vector<ll> cmin(N, INT64_MAX); vector<ll> cmax(N, INT64_MIN); REP(i,N){ cnum[C[i]-1] ++; if(X[i] < cmin[C[i]-1]) cmin[C[i]-1] = X[i]; if(X[i] > cmax[C[i]-1]) cmax[C[i]-1] = X[i]; } ll hi_dis = 0; ll hi_pos = 0; ll low_dis = 0; ll low_pos = 0; REP(i, N){ if(cnum[i] == 0) continue; ll hi2low = hi_dis + ((hi_pos > cmax[i])?(hi_pos - cmax[i]):(cmax[i] - hi_pos)) + (cmax[i] - cmin[i]); ll low2low = low_dis + ((low_pos > cmax[i])?(low_pos - cmax[i]):(cmax[i] - low_pos)) + (cmax[i] - cmin[i]); ll hi2hi = hi_dis + ((hi_pos > cmin[i])?(hi_pos - cmin[i]):(cmin[i] - hi_pos)) + (cmax[i] - cmin[i]); ll low2hi = low_dis + ((low_pos > cmin[i])?(low_pos - cmin[i]):(cmin[i] - low_pos)) + (cmax[i] - cmin[i]); hi_dis = (hi2hi < low2hi) ? hi2hi : low2hi; low_dis = (hi2low < low2low) ? hi2low : low2low; hi_pos = cmax[i]; low_pos = cmin[i]; } hi_dis += ((hi_pos > 0)?hi_pos:-hi_pos); low_dis += ((low_pos > 0)?low_pos:-low_pos); ll ans = (hi_dis < low_dis) ? hi_dis : low_dis; cout << ans << endl; return 0; }
//#include <bits/stdc++.h> #include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <fstream> #include <string.h> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <queue> #include <math.h> #include <assert.h> #define ff first #define ss second #define ll long long #define mod 1000000007 using namespace std; ll v, l, r, x; int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>v>>l>>r>>x; ll lx = v * l, rx = v * r; if(x >= lx && x <= rx) cout<<"No"; else cout<<"Yes"; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Kein Yukiyoshi */ // clang-format off #include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC target("avx") #define int long long using namespace std; #define stoi stoll #define fi first #define se second #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++) #define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++) #define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--) #define FOR(i, a) for (auto &i: a) #define ALL(obj) begin(obj), end(obj) #define _max(x) *max_element(ALL(x)) #define _min(x) *min_element(ALL(x)) #define _sum(x) accumulate(ALL(x), 0LL) const int MOD = 1000000007; // const int MOD = 998244353; const int INF = 10000000000007; // 1e13 + 7 const int INF2 = LLONG_MAX; // 9.2e18 const double EPS = 1e-8; const double PI = 3.14159265358979; template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T> using VVV = vector<vector<vector<T>>>; template <class T, class S> using P = pair<T, S>; template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return true;}return false;} template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return true;}return false;} int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } template<class T> T chmod(T &a, T mod=MOD) {a = a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;}; int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));} int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;} struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // clang-format on class AVanishingPitch { public: static void solve(istream &cin, ostream &cout) { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int v, t, s, d; cin >> v >> t >> s >> d; if (t * v <= d and d <= v * s) { cout << "No" << endl; } else { cout << "Yes" << endl; } } }; signed main() { AVanishingPitch solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include "bits/stdc++.h" using namespace std; int main() { int N; long long X; cin >> N >> X; vector<long long>A(N + 1); for (int i = 0; i < N; ++i) { cin >> A[i]; } A[N] = A[N - 1] * 1000; vector<vector<long long>> dp(N + 1, vector<long long>(2, 0)); dp[0][0] = 1; for (int i = 0; i < N; ++i) { for (int j = 0; j < 2; ++j) { long long d = (X / A[i] + j)% (A[i + 1] / A[i]); if (0 == d) { dp[i + 1][j] += dp[i][j]; } else { dp[i + 1][0] += dp[i][j]; dp[i + 1][1] += dp[i][j]; } } } cout << dp[N][0] << endl; return 0; }
#include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <utility> #include <string> #include <queue> #include <stack> using namespace std; typedef long long int ll; typedef pair<int, int> Pii; const ll mod = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; ll x; cin >> n >> x; vector<ll> a(n); for (auto &y: a) cin >> y; reverse(a.begin(), a.end()); if (x % a[0] == 0) { cout << 1 << endl; return 0; } ll pos = 1; ll neg = 1; ll rem_pos = x % a[0]; ll rem_neg = -(x % a[0] - a[0]); for (int i = 1; i < n; i++) { if (rem_pos % a[i] == 0) break; ll next_pos = 0; ll next_neg = 0; if (a[i] * (rem_pos / a[i] + 1) == a[i-1]) next_pos += pos; else { next_pos += pos; next_neg += pos; } if (a[i] * (rem_neg / a[i] + 1) == a[i-1]) next_neg += neg; else { next_neg += neg; next_pos += neg; } rem_pos = rem_pos % a[i]; rem_neg = -(rem_pos % a[i] - a[i]); pos = next_pos; neg = next_neg; } ll ans = pos + neg; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) int main(){ cin.tie(0);ios::sync_with_stdio(false); ll a,b; cin>>a>>b; vector<int>primes={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71}; vector<ll>dp(1<<20); dp[0]=1; for(ll i=a;i<=b;i++){ int s=0,t=0; rep(k,20){ if(i%primes[k])s|=(1<<k); else t|=(1<<k); } for(int j=s-1;j>=0;j=(j-1)&s){ //choose i dp[j|t]+=dp[j]; if(j==0)break; } } ll ans=0; for(ll i:dp)ans+=i; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using vi = vector<int>; using si = set<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define pii pair<int,int> #define pll pair<ll,ll> #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define rev(i,s,n) for(int i=(s);i>=(int)(n);i--) #define reciv(v,n) vll (v)((n)); rep(i,0,(n))cin>>v[i] #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) ll x;cin >> x #define cci(x,y) ll x,y;cin >> x >> y #define co(x) cout << x << endl #define pb push_back #define eb emplace_back #define rz resize #define pu push #define sz(x) int(x.size()) #define vij v[i][j] // ll P = 1e9+7; // ll P = 998244353; // n do -> n*pi/180 #define yn cout<<"Yes"<<endl;else cout<<"No"<<endl #define YN cout<<"YES"<<endl;else cout<<"NO"<<endl template<class T>void chmax(T &x,T y){x=max(x,y);} template<class T>void chmin(T &x,T y){x=min(x,y);} int main(){ db x1,a,x2,b; cin>>x1>>a>>x2>>b; db ans=(a*x2+b*x1)/(a+b); printf("%.9lf\n",ans); }
#include<bits/stdc++.h> #define pi 3.141592653589793238 #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define MOD 1000000007 #define INF 999999999999999999 #define pb push_back #define ff first #define ss second #define mt make_tuple #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// const int N = 3e5 + 1; ll t[4 * N], arr[N]; void build(ll v, ll tl, ll tr){ if(tl == tr){ t[v] = arr[tl]; return; } ll tm = (tl + tr) /2; build(2 * v, tl, tm); build(2 * v + 1, tm + 1, tr); t[v] = t[2 * v] ^ t[2 * v + 1]; } void update(ll v, ll tl, ll tr, ll idx, ll y){ if(tl == tr){ t[v] ^= y; return; } ll tm = (tl + tr) / 2; if(idx <= tm){ update(2 * v, tl, tm, idx, y); } else{ update(2 * v + 1, tm + 1, tr, idx, y); } t[v] = t[2 * v] ^ t[2 * v + 1]; } ll query(ll v, ll tl, ll tr, ll l, ll r){ if(l > r){ return 0; } if(tl == l && tr == r){ return t[v]; } ll tm = (tl + tr) / 2; ll lef, rig; lef = query(2 * v, tl, tm, l, min(r, tm)); rig = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); return (lef ^ rig); } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); fast; ll T = 1, i, j; //cin >> T; while (T--) { ll n, q; cin >> n >> q; for(i = 1; i <= n; i++){ cin >> arr[i]; } build(1, 1, n); while(q--){ ll t; cin >> t; ll x, y; cin >> x >> y; if(t == 1){ update(1, 1, n, x, y); } else{ cout << query(1, 1, n, x, y) << "\n"; } } } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define vi vector<ll> #define rep(i,a,b) for(int i=a; i<b; ++i) #define pb push_back #define endl '\n' #define sa(x) sort((x).begin(), (x).end()); #define int long long template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; } template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; } template<typename T> static inline void sd(vector<T> &x) {sort((x).begin(), (x).end(), greater<T>()) ; } int n, m; const int N=1e5+5; vector<pair<int, pair<int, int>>> g[N]; #define ii pair<ll, ll> #define vii vector<ii> #define x first #define y second const int inf=LLONG_MAX; void solve(){ cin>>n>>m; rep(i,0,m) { int e, f, c, d; cin>>e>>f>>c>>d; g[e].pb({f, {c, d}}); g[f].pb({e, {c, d}}); } set<ii> se; se.insert({0, 1}); vi dis(n+1, inf); while(!se.empty()) { auto top = *(se.begin()); se.erase(se.begin()); int v=top.y; dis[v] = top.x; for(auto ch : g[v]) { int D=ch.y.y, X=dis[v], C=ch.y.x, tar=ch.x; // cout<<v<<" to "<<tar<<endl; // cout<<C<<" and "<<D<<" "<<X<<endl; int t = sqrt(D) - X - 1; vector<int> ts = {max(t-1, 0ll), max(t, 0ll), max(t+1, 0ll)}; int ans=dis[tar]; for(int tt : ts) { // cout<<tt<<" "<<1<<" "<<X<<endl; int tmp = C + D/(tt+1+X) + X + tt; // cout<<"wait "<<tt<<" get"<<tmp<<endl; if(tmp < ans) { ans=tmp; } } if(ans < dis[tar]) { if(dis[tar] != inf) { se.erase({dis[tar], tar}); } dis[tar] = ans; se.insert({dis[tar], tar}); } } } // rep(i, 1, n+1) // { // if(dis[i]!=inf) cout<<dis[i]<<" "; // else cout<<"x "; // } if(dis[n] != inf) cout<<dis[n]; else cout<<-1; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int testcases=1; //cin>>testcases; while(testcases--){ solve(); } return 0; }
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <bitset> #include <climits> #include <cmath> #include <bitset> #include <complex> #include <functional> #include <cmath> #include <cassert> #include <stack> #include <numeric> typedef long long ll; typedef std::pair<int, int> Pii; typedef std::pair<long long, long long> Pll; typedef std::pair<double, double> Pdd; #define rip(i, n, ss) for (int i = (ss);i < (int)( n ); i++) #define all(l) l.begin(), l.end() #define MM << " " << template<typename T> using MaxHeap = std::priority_queue<T>; template<typename T> using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<typename T> inline bool chmax(T &l, const T b) { if (l < b) { l = b; return true; } return false; } template<typename T> inline bool chmin(T &l, const T b) { if (l > b) { l = b; return true; } return false; } # ifdef LOCAL_DEBUG template<typename T> void vdeb(const std::vector<T> &bb) { for (unsigned int i = 0;i < bb.size();i++) { if (i == bb.size() - 1) std::cout << bb[i]; else std::cout << bb[i] << ' '; } std::cout << '\n'; } template<typename T> void vdeb(const std::vector<std::vector<T>> &bb) { for (unsigned int i = 0;i < bb.size();i++) { std::cout << i << ' '; vdeb(bb[i]); } std::cout << '\n'; } # endif using namespace std; void dfs1(vector<vector<int>> &da, vector<Pii> &ed, vector<ll> &score, int now, int depth) { if(score[now] != -1) return; score[now] = depth; rip(i,da[now].size(),0) { dfs1(da, ed, score, ed[da[now][i]].first^ed[da[now][i]].second^now, depth+1); } } void dfs2(vector<vector<int>> &da, vector<Pii> &ed, vector<ll> &ans, vector<ll> &weg, int now, ll depth) { if(ans[now] != -1) return; ans[now] = depth + weg[now]; rip(i,da[now].size(),0) { dfs2(da, ed, ans, weg, ed[da[now][i]].first^ed[da[now][i]].second^now, ans[now]); } } int main() { int n; cin >> n; vector<Pii> ed(n-1); vector<vector<int>> da(n, vector<int>(0)); vector<ll> score(n, -1); rip(i,n-1,0) { int a,b; cin >> a >> b; --a;--b; da[a].push_back(i); da[b].push_back(i); ed[i] = {a, b}; } dfs1(da, ed, score, 0, 0); vector<ll> weg(n); vector<ll> ans(n, -1); // rip(i,n-1,0) { // if(ed[i].first < ed[i].second) swap(ed[i].first, ed[i].second); // } int q; cin >> q; rip(i,q,0) { ll t, e, x; cin >> t >> e >> x; --e; if(t == 1) { if(score[ed[e].first] > score[ed[e].second]) { weg[ed[e].first] += x; } else { weg[ed[e].second] -= x; weg[0] += x; } } else { if(score[ed[e].first] < score[ed[e].second]) { weg[ed[e].second] += x; } else { weg[ed[e].first] -= x; weg[0] += x; } } } dfs2(da, ed, ans, weg, 0, 0); rip(i,n,0) { cout << ans[i] << '\n'; } }
#include <bits/stdc++.h> using namespace std; #define R register #define LL long long inline int read() { int x = 0, f = 1; char a = getchar(); for(; a > '9' || a < '0'; a = getchar()) if(a == '-') f = -1; for(; a <= '9' && a >= '0'; a = getchar()) x = x * 10 + a - '0'; return x * f; } const int N = 4e5 + 10; int n; int a[N], b[N], uni[N], tot; vector<int> e[N]; int vis[N]; int F = 0; int sz; void dfs(int x, int las) { if(vis[x]) { F = 1; return ; } sz ++; vis[x] = 1; for(int y : e[x]) if(y != las) dfs(y, x); } signed main() { #ifdef IN freopen("a.in", "r", stdin); //freopen(".out", "w", stdout); #endif n = read(); for(int i = 1; i <= n; i ++) a[i] = read(), b[i] = read(), uni[++ tot] = a[i], uni[++ tot] = b[i]; sort(uni + 1, uni + 1 + tot); tot = unique(uni + 1, uni + 1 + tot) - uni - 1; for(int i = 1; i <= n; i ++) { a[i] = lower_bound(uni + 1, uni + 1 + tot, a[i]) - uni; b[i] = lower_bound(uni + 1, uni + 1 + tot, b[i]) - uni; } for(int i = 1; i <= n; i ++) { e[a[i]].push_back(b[i]); e[b[i]].push_back(a[i]); } int ans = 0; for(int i = 1; i <= tot; i ++) if(! vis[i]) { F = 0; sz = 0;dfs(i, 0); ans += sz - 1; if(F) ans ++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define N 21 #define M 101 #define popcnt __builtin_popcount using namespace std; using ll = long long; int n, m; vector<pair<int, int>> qs[M]; ll dp[1<<N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>m; for (int i=0; i<m; i++) { int x, y, z; cin>>x>>y>>z; qs[x].push_back({y, z}); } dp[0] = 1; for (int i=1; i<(1<<n); i++) { for (int j=0; j<n; j++) { if (~i & 1<<j) continue; dp[i] += dp[i & ~(1<<j)]; } int x = popcnt(i); for (auto [y, z] : qs[x]) { int smol = i & ((1<<y) - 1); if (popcnt(smol) > z) dp[i] = 0; } } cout<<dp[(1<<n)-1]<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll N = 1e5 + 5; vector<tuple<int, ll, ll>> G[N]; int n, m, u, v, x, y, vis[N]; ll t, k, d[N]; void bfs() { priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> q; fill(d, d + N, 1e18); d[x] = 0; q.push({0, x}); while (!q.empty()) { auto[t, a] = q.top(); q.pop(); if (a == y) break; if (t > d[a]) continue; for (auto[b, t, k]:G[a]) { t = (d[a] + k - 1) / k * k + t; if (d[b] > t) { d[b] = t; q.emplace(d[b], b); } } } } int main() { cin >> n >> m >> x >> y; while (m--) { cin >> u >> v >> t >> k; G[u].emplace_back(v, t, k); G[v].emplace_back(u, t, k); } bfs(); if (d[y] == 1e18) cout << -1; else cout << d[y]; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <climits> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> #define DEBUG 1 using namespace std; constexpr int kMod = 1000000007; typedef long long LL; bool finish(LL r1, LL c1, LL r2, LL c2) { if (r1 + c1 == r2 + c2) return true; if (r1 - c1 == r2 - c2) return true; if (abs(r1 - r2) + abs(c1 - c2) <= 3) return true; return false; } int main() { LL r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; LL r = abs(r2 - r1), c = abs(c2 - c1); if (r == 0 && c == 0) cout << 0 << endl; else if (r == c) cout << 1 << endl; else if (r + c <= 3) cout << 1 << endl; else if (r + c <= 6) cout << 2 << endl; else if ((r + c) % 2 == 0) cout << 2 << endl; else if (abs(r - c) <= 3) cout << 2 << endl; else cout << 3 << endl; }
#include<bits/stdc++.h> #define ll long long #define llu unsigned long long #define pb push_back #define f(n) for(ll i=0;i<n;i++) #define ina for(ll i=0;i<n;i++)cin>>a[i]; #define all(v) v.begin(),v.end() #define YES printf("YES\n") #define NO printf("NO\n") #define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31))) #define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31)) #define fastio std::ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0); #define finout freopen("input.cpp","r",stdin); freopen("output.cpp","w",stdout); #define pi acos(-1) using namespace std; //ll pow(ll a,ll b) { ll ret=1; for(ll i=0;i<b;i++) ret*=a; return ret;} //long long bigmod/* (a^p)%m */ ( long long a, long long p, long long m ){long long res = 1;long long x = a;while ( p ){if ( p & 1 ){res = ( res * x ) % m;}x = ( x * x ) % m;p = p >> 1;}return res;} //ll modInverse(ll a, ll m) { ll m0 = m; ll y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } //call manipulated_seive(N) and get prime in prime vector //const long long MAX_SIZE = 1000001; vector<long long >isprime(MAX_SIZE , true); vector<long long >prime; vector<long long >SPF(MAX_SIZE); void manipulated_seive(int N) { isprime[0] = isprime[1] = false ; for (long long int i=2; i<N ; i++) { if (isprime[i]) { prime.push_back(i); SPF[i] = i; } for (long long int j=0; j < (int)prime.size() && i*prime[j] < N && prime[j] <= SPF[i]; j++) { isprime[i*prime[j]]=false; SPF[i*prime[j]] = prime[j] ; } } } //bitwise sieve //size of prime array = sz //const int MAX = 100000000;const int LMT = 10000;int _c[(MAX>>6)+1];ll prime[5761482];ll sz=0;void bitwise_sieve(){for (int i = 3; i <= LMT; i += 2)if (!IsComp(i))for (int j = i*i; j <= MAX; j += i+i) SetComp(j);prime[sz++]=2;for (int i=3; i <= MAX; i += 2)if (!IsComp(i))prime[sz++]=i;} //ll phi[5000001];bool mark[5000001];void sievephi(ll n){ll i,j;for(ll i=1;i<=n;i++) phi[i]=i;mark[1]=true;for(ll i=2;i<=n;i++)if(!mark[i]){for(ll j=i;j<=n;j+=i){mark[j]=true;phi[j]=phi[j]/i*(i-1);}}} /*speedup #pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") */ // vule geli coilbi na dada cout << fixed << setprecision(6) << pi <<" "<<npi<<endl; //Pattern? OEIS // prime factorize?? think about 2 vector<int>v; int a[200003]; void solve() { int a,b,c,d; cin>>a>>b>>c>>d; int ans=0; int k = a-c; int l= d-b; int n= a-b; int m= c-d; if(a==c && b==d) ans=0; else if(l==k || n == m || abs(a-c)+abs(b-d)<=3 ) ans=1; else if(abs(n-m)<=3) ans=2; else if(abs(l-k)%2) { if(abs(l-k)>3) ans=3; else ans=2; } else if(abs(l-k)%2==0) ans=2; cout<<ans<<endl; } int main() { solve(); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i]; #define ft first #define sc second #define pb push_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(),(v).end() #define LB(a,x) lb(all(a),x)-a.begin() #define UB(a,x) ub(all(a),x)-a.begin() #define mod 1000000007 //#define mod 998244353 #define FS fixed<<setprecision(15) using namespace std; typedef long long ll; const double pi=3.141592653589793; template<class T> using V=vector<T>; using P=pair<ll,ll>; typedef unsigned long long ull; typedef long double ldouble; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline void out(T a){ cout << a << '\n'; } void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;} //void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;} const ll INF=1e18; const int mx=200005; //zone ll a[502][502]; ll b[502][502]; ll dp[502][502]; int main(){ //オーバーフローは大丈夫ですか?? cin.tie(0);ios::sync_with_stdio(false); ll R,C; cin>>R>>C; rep(i,R)rep(j,C-1) cin>>a[i][j]; rep(i,R-1)rep(j,C) cin>>b[i][j]; rep(i,R)rep(j,C) dp[i][j]=INF; dp[0][0]=0; priority_queue<pair<ll,P>,V<pair<ll,P>>,greater<pair<ll,P>>> que; que.push({0,{0,0}}); while(que.size()){ pair<ll,P> p=que.top(); que.pop(); int i=p.sc.ft; int j=p.sc.sc; if(dp[i][j]<p.ft) continue; if(j+1<C){ if(chmin(dp[i][j+1],dp[i][j]+a[i][j])){ que.push({dp[i][j+1],{i,j+1}}); } } if(j-1>=0){ if(chmin(dp[i][j-1],dp[i][j]+a[i][j-1])){ que.push({dp[i][j-1],{i,j-1}}); } } if(i+1<R){ if(chmin(dp[i+1][j],dp[i][j]+b[i][j])){ que.push({dp[i+1][j],{i+1,j}}); } } for(int k=0;k<i;k++){ if(chmin(dp[k][j],dp[i][j]+(ll)i-k+1)){ que.push({dp[k][j],{k,j}}); } } //cout<<i<<' '<<j<<endl; } out(dp[R-1][C-1]); } //E //ペナルティ出しても焦らない ACできると信じろ!!! //どうしてもわからないときはサンプルで実験 何か見えてくるかも //頭で考えてダメなら紙におこせ!! //添字注意!!(特に多重ループのとき)
#include <bits/stdc++.h> using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v).begin(), (v).end() #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define DEBUG //#define int long long #define INF 1e18 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; } template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template<class T> ostream& operator << (ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";} typedef long long ll; using Edge = pair<int, int>; using Graph = vector<vector<Edge>>; Graph G; int n, d[250010]; // <最短距離, 頂点の番号> using P = pair<int, int>; void dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; fill(d, d + n, INF); d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < G[v].size(); ++i) { Edge e = G[v][i]; if (d[e.first] > d[v] + e.second) { d[e.first] = d[v] + e.second; que.push(P(d[e.first], e.first)); } } } } int a[510][510]; int b[510][510]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int r,c; cin>>r>>c; n=r*c; G.assign(r*c , vector<Edge>()); for (int i = 0; i < r; i++) { for (int j = 0; j < c-1; j++) { cin>>a[i][j]; } } for (int i = 0; i < r-1; i++) { for (int j = 0; j < c; j++) { cin>>b[i][j]; } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if(j<c-1) G[c*i + j].push_back({c*i+j+1, a[i][j]}); if(j>=1) G[c*i + j].push_back({c*i+j-1, a[i][j-1]}); if(i<r-1) G[c*i + j].push_back({c*(i+1)+j, b[i][j]}); } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { for (int k = 1; k < r; k++) { if(i - k < 0 ) continue; G[c*i+j].push_back({c*i+j - c*k , 1 + k}); } } } dijkstra(0); cout<<d[r*c-1]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll LINF = 1e18; const int INF = 1e9; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() 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; } // Union-Find struct UnionFind { vector<int> par, siz; UnionFind(int n) : par(n, -1), siz(n, 1) {} // ������� int root(int x) { if (par[x] == -1) return x; else return par[x] = root(par[x]); } // x �� y ��Ʊ�����롼�פ�°���뤫�ɤ��� (�������פ��뤫�ɤ���) bool issame(int x, int y) { return root(x) == root(y); } // x ��ޤ॰�롼�פ� y ��ޤ॰�롼�פȤ�ʻ�礹�� bool unite(int x, int y) { x = root(x), y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); par[y] = x; siz[x] += siz[y]; return true; } // x ��ޤ॰�롼�פΥ����� int size(int x) { return siz[root(x)]; } }; int main() { ll N; cin >> N; unordered_map<string, int> a(N); vector<string> b(N); for (int i = 0; i < N; i++) { string temp; cin >> temp; a[temp]=1; b[i] = temp; } for (int i = 0; i < N; i++) { if(a['!'+b[i]]!=0){ cout << b[i] << endl; return 0; } } cout << "satisfiable" << endl; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> // Common file //#include <ext/pb_ds/tree_policy.hpp> //#include <functional> // for less //using namespace __gnu_pbds; using namespace std; //typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_set; #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ii pair<int, int> #define fi first #define se second #define int long long #define endl '\n' #define mp make_pair #define pb push_back #define v1 v<<1 #define v2 (v<<1)|1 //__builtin_popcountll(x) : Number of 1-bit //__builtin_ctzll(x) : Number of trailing 0 const int inf = (int)1e9; const int mod = (int)1e9 + 7; int dx[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int dy[30]; int cal(string s){ int sum = 0LL; for (int i = 0; i < s.size(); i++){ sum = sum * 10LL + dx[dy[s[i] - 'a']]; } return sum; } bool ok(string s){ return dx[dy[s[0]-'a']]!=0; } void print(string s){ for(int i=0;s[i];i++)putchar(dx[dy[s[i]-'a']]+'0'); putchar('\n'); } clock_t t1,t2; string s[4]; signed main() { // freopen(".inp","r",stdin); // freopen(".out","w",stdout); fastio int cnt = 0, a[30], len[4], b[30]; cin >> s[1] >> s[2] >> s[3]; for (int i = 1; i <= 3; i++) len[i] = s[i].size(); set<int> x; for (int i = 1; i <= 3; i++){ for (int j = 0; j < len[i]; j++){ x.insert(s[i][j]); } } if(x.size() > 10){ cout << "UNSOLVABLE"; return 0; } for (auto i : x){ dy[i - 'a'] = cnt; cnt++; } // cout << cal(s[1]) << cal(s[2]) << ok(s[1]) << ok(s[2]); do{ int m = cal(s[1]), m1 = cal(s[2]), m2 = cal(s[3]); if(m + m1 == m2 && ok(s[1]) && ok(s[2]) && ok(s[3])){ print(s[1]); print(s[2]); print(s[3]); return 0; } } while(next_permutation(dx, dx + 10)); cout << "UNSOLVABLE"; return 0; }
#include<iostream> #include<cstring> #include<algorithm> #include<map> #include<cstring> using namespace std; const int N=1e6+10,mod=998244353; typedef long long LL; int a[20]={0,1,2,3,4,5,6,7,8,9}; map<char,int>mp; long long get_num(char s[]){ if(a[mp[s[1]]]==0)return -1; int len=strlen(s+1); long long ans=a[mp[s[1]]]; for(int i=2;i<=len;i++){ ans*=10; ans+=a[mp[s[i]]]; } return ans; } char s1[N],s2[N],s3[N]; int check(){ long long num1=get_num(s1); long long num2=get_num(s2); long long num3=get_num(s3); if(num1==-1||num2==-1||num3==-1)return 0; return (num1+num2==num3); } int tot=0; int main(){ scanf("%s%s%s",s1+1,s2+1,s3+1); for(int i=1;i<=strlen(s1+1);i++){ if(!mp.count(s1[i])){ mp[s1[i]]=tot++; } } for(int i=1;i<=strlen(s2+1);i++){ if(!mp.count(s2[i])){ mp[s2[i]]=tot++; } } for(int i=1;i<=strlen(s3+1);i++){ if(!mp.count(s3[i])){ mp[s3[i]]=tot++; } } if(mp.size()>10)puts("UNSOLVABLE"); else{ int flag=0; do{ if(check()){ flag=1; break; } } while(next_permutation(a,a+10)); if(flag){ cout<<get_num(s1)<<endl; cout<<get_num(s2)<<endl; cout<<get_num(s3); }else{ puts("UNSOLVABLE"); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++) #define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--) #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define eb emplace_back using ll = long long; using vll = vector<ll>; using vi = vector<int>; using vvi = vector<vector<int>>; using P = pair<int, int>; using LD = long double; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void coutall(T v) { for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; } void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; } void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; } void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; } ll myceil(ll a, ll b) { return (a + (b - 1)) / b; } void Main(){ int n; cin >> n; vi x(n), y(n); rep(i, n) cin >> x[i] >> y[i]; int cnt = 0; rep(i, n-1){ rep3(j, i+1, n-1){ // -1 <= y[j] - y[i] / x[j] - x[i] <= 1 if(abs(y[j] - y[i]) <= abs(x[j] - x[i])) cnt++; } } cout << cnt << endl; return; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
#include<bits/stdc++.h> #define MP make_pair #define PB push_back #define rep(i,n) for(int(i)=0;(i)<(int)(n);++(i)) #define v vector #define r return /* Do-while:do { } while(); endif:#ifndef #endif */ using namespace std; typedef long long ll; string ss,st,tt,ts,s,t; ll b,c,d,n,m,q,k; struct pc{ int ddd,ffg,eheu; }; v<int> w; const int maxn=1000005; ll a[maxn]; double fd; bool fl1,fl2,fl3,fl; signed main(){ cin>>n>>q; rep(i,n){ cin>>a[i]; a[i]-=i; } while(q--){ cin>>k; cout<<k+upper_bound(a,a+n,k)-a<<"\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<(int)n;i++) #define Rep(i,n) for(int i=0;i<=(int)n;i++) const ll mod = 1e9+7 ; const ll INF = 1e18 ; ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;} ll lcm(long long a,long long b){return a/gcd(a,b)*b ;} int main(){ ll n, q; cin >> n >> q; vector<ll> a(n); rep(i,n) cin >> a.at(i); vector<pair<ll,ll>> b(n + 1); ll pos = 0; rep(i,n) { b.at(i).second = pos; b.at(i).first = a.at(i) - pos - 1; pos = a.at(i); } b.at(n).second = a.at(n - 1); for(int i = 1; i < n + 1; i++) { b.at(i).first += b.at(i - 1).first; } rep(i,q) { ll k; cin >> k; ll l = -1; ll r = n + 1; ll mid; while(l + 1 < r) { mid = (l + r) / 2; if(b.at(mid).first >= k) r = mid; else l = mid; } if(r == 0) { cout << k << endl; continue; } if(r == n + 1) { cout << a.at(n - 1) + k - b.at(n).first << endl;; continue; } cout << b.at(r).second + k - b.at(r - 1).first << endl; } }
#include<iostream> #include<vector> #include<string> using namespace std; using LL = long long; int main(){ int H, W; cin >> H >> W; vector<string> field; for (int i = 0; i < H; i++){ string si; cin >> si; field.emplace_back(si); } vector<vector<int>> diag; //0:R, 1:B, 2:. for (int i = 0; i < H+W-1; i++){ vector<int> a = {0,0,0}; diag.emplace_back(a); } for (int i = 0; i < H; i++){ for (int j = 0; j < W; j++){ char s = field[i][j]; if (s == 'R'){ diag[i+j][0] += 1; } else if(s == 'B'){ diag[i+j][1] += 1; } else { diag[i+j][2] += 1; } }} LL pat = 1; for (vector<int> v: diag){ int rn = v[0], bn = v[1], dn = v[2]; if (rn*bn != 0){ cout << 0 << endl; return 0; } else if(rn == 0 && bn == 0 && dn != 0){ pat = (pat*2)%998244353; } } cout << pat << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N,M; int x,y; vector<string> A; char dna[20][20]; cin >> N >> M; for(int i=0; i<M; i++) { string a; cin >> a; bool iscontain=false; for( int j=0; j<A.size(); j++) if(A[j].find(a)!=string::npos) {iscontain=true;break;} if(iscontain!=true) A.push_back(a); } bool convine=true; while(convine) { convine=false; for(int i=0; i<A.size(); i++) { if(A[i].size()>2){ string b=A[i].substr(A[i].size()-2); for(int j=0; j<A.size(); j++) { if( i==j ) continue; if( A[j][0]==b[0] && A[j][1]==b[1] ){ if( A[i].size()+A[j].size()-2 <= N) { A[i] = A[i] + A[j].substr(2); A[j] =""; convine=true; break; } } } } } } convine=true; while(convine) { convine=false; for(int i=0; i<A.size(); i++) { if(A[i].size()>2 && A[i].size()<N){ for(int j=0; j<A.size(); j++) { if( i==j ) continue; if( (A[j].size() > 2) && ((A[i].size()+A[j].size()) <= N) ){ A[i] = A[i] + A[j]; A[j] =""; convine=true; break; } } } } } for(int i=0; i<N; i++){ int max=A[i].size(); int maxj=i; for(int j=i; j<A.size(); j++){ if(A[j].size()>max) { max=A[j].size(); maxj=j; } } if( i!=maxj){ string tmp; tmp=A[maxj]; A[maxj]=A[i]; A[i]=tmp; } } for(int i=0; i<N; i++) { if(A[i].size() < 20) A[i]+="...................."; } for(int i=0; i<N; i++) { cout << A[i].substr(0,20) << endl; } };
#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; const int MAX_N = 100005; vector<int> G[MAX_N]; int dp[MAX_N][2]; // 0:f(v) 1:size void dfs(int n) { if (G[n].size() == 0) { dp[n][0] = 1; dp[n][1] = 1; return; } vector<P> v1,v2,v3; int f = 1, s = 1; rep(i,G[n].size()) { int c = G[n][i]; dfs(c); if (dp[c][1] % 2 == 0 && dp[c][0] < 0) { f += dp[c][0]; s += dp[c][1]; } else if (dp[c][1] % 2 == 0) { v2.push_back(P(dp[c][0],dp[c][1])); } else { v3.push_back(P(dp[c][0],dp[c][1])); } } sort(v3.begin(), v3.end()); int sign = 1; rep(i,v3.size()) { f += sign*v3[i].first; s += v3[i].second; sign *= -1; } rep(i,v2.size()) { f += sign*v2[i].first; s += v2[i].second; } dp[n][0] = f; dp[n][1] = s; return; } int main() { int N; cin >> N; rep(i,N-1) { int p; cin >> p; p--; G[p].push_back(i+1); } dfs(0); /* rep(i,N) { cout << dp[i][0] << " " << dp[i][1] << endl; } */ cout << (dp[0][0]+N)/2 << endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/modint> #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define INF 2000000000000000000 #define ll long long #define ld long double #define pll pair<ll, ll> using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } struct d{ll f, s, t;};//first, second, turn bool comp(d a, d b) { if (a.t == 0 || b.t == 0) { return a.t > b.t; } return (a.s - a.f) > (b.s - b.f); } ll N; vector<ll> p; vector<vector<ll>> child; d dfs(ll now, ll turn) { vector<ll> FS(2, 0); FS.at(0) += 1; turn = (turn + 1) % 2; vector<d> ds; for (ll i = 0; i < child.at(now).size(); ++i) { d temp = {1, 0, 1}; if (child.at(child.at(now).at(i)).size() != 0) { temp = dfs(child.at(now).at(i), 0); } if (temp.f <= temp.s && temp.t == 0) { FS.at(turn) += temp.s; FS.at((turn + 1) % 2) += temp.f; } else { ds.push_back(temp); } } sort(rng(ds), comp); for (ll i = 0; i < ds.size(); ++i) { d temp = ds.at(i); FS.at(turn) += temp.s; FS.at((turn + 1) % 2) += temp.f; turn = (turn + temp.t) % 2; } // cout << "now" << now + 1 << "\n"; // cout << FS.at(0) << ' ' << FS.at(1) << ' ' << turn << "\n"; return {FS.at(0), FS.at(1), turn}; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; p = vector<ll>(N, -1); child = vector<vector<ll>>(N); for (ll i = 1; i < N; ++i) { cin >> p.at(i); p.at(i) -= 1; child.at(p.at(i)).push_back(i); } cout << dfs(0, 0).f << "\n"; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define debug(a,n) for(ll i = 0;i<n;i++) cout<<a[i]<<" "; #define sum(a,n,s) for(ll i = 0;i<n;i++) s+=a[i]; #define scan(a,n) for(ll i = 0;i<n;i++) cin>>a[i]; ll counter=0; vector <ll> SieveOfEratosthenes(ll n) { vector <ll> ans; bool prime[n+1]; memset(prime, true, sizeof(prime)); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*p; i<=n; i += p) prime[i] = false; } } // Print all prime numbers for (int p=2; p<=n; p++) if (prime[p]) ans.push_back(p); return ans; } vector <ll> sievo(ll N) { ll lp[N+1]; vector<ll> pr; memset(lp,0,sizeof lp); for (ll i=2; i<=N; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back (i); } for (ll j=0; j<(ll)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; } return pr; } long long compute_hash(string s) { const int p = 31; const int m = 1e9 + 9; long long hash_value = 0; long long p_pow = 1; for (char c : s) { hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m; p_pow = (p_pow * p) % m; } return hash_value; } ll power(ll x, unsigned ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } vector<int> rabin_karp(string const& s, string const& t) { const int p = 31; const int m = 1e9 + 9; int S = s.size(), T = t.size(); vector<long long> p_pow(max(S, T)); p_pow[0] = 1; for (int i = 1; i < (int)p_pow.size(); i++) p_pow[i] = (p_pow[i-1] * p) % m; vector<long long> h(T + 1, 0); for (int i = 0; i < T; i++) h[i+1] = (h[i] + (t[i] - 'a' + 1) * p_pow[i]) % m; long long h_s = 0; for (int i = 0; i < S; i++) h_s = (h_s + (s[i] - 'a' + 1) * p_pow[i]) % m; vector<int> occurences; for (int i = 0; i + S - 1 < T; i++) { long long cur_h = (h[i+S] + m - h[i]) % m; if (cur_h == h_s * p_pow[i] % m) occurences.push_back(i); } return occurences; } ll check(vector <ll> &arr,ll l, ll n) { if(arr[0]<0||arr[n-1]<0) { return 0; } if(n==1) { return arr[l]==0; } arr[l+1]-= arr[l]; if(arr[l+1]<0) { arr[l+1]+=arr[l]; return 0; } ll ans = check(arr,l+1,n-1); arr[l+1]+=arr[l]; return ans; } vector <ll> precompute(vector <ll> arr,ll n) { vector <ll> ans(n); ans[0] = arr[0]; for(ll i = 1;i<n;i++) { ans[i] = arr[i]-ans[i-1]; } ll i=0; for(i =0;i<n;i++) { if(ans[i]<0) { ans[i]=-1; break; } } while(i<n) { ans[i++]=-1; } return ans; } void solve() { ll k;cin>>k; ll arr[k+10]; memset(arr,0,sizeof arr); for(ll i = 1;i<=k;i++) { for(ll j = i;j<=k;j+=i) { arr[j]++; // arr[] } } ll count = 0; for(ll i = 1;i<=k;i++) { count+= arr[i]*(k/i); } cout<<count<<endl; return; } 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> using namespace std; #define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(a) a.begin(),a.end() #define vi vector<int> #define vvi vector<vi> #define P pair<int,int> #define vP vector<P> #define vvP vector<vP> using ll = long long int; int main(){ int K;cin>>K; ll res=0; FOR(i,1,K+1){ FOR(j,1,K+1){ if(i*j>K)break; res += K/i/j; } } cout<<res<<endl; return 0; }
// g++ -std=c++11 a.cpp #include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<unordered_map> #include<utility> #include<cmath> #include<random> #include<cstring> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<sstream> #include<random> #include<iomanip> #include<assert.h> #include<typeinfo> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define FOR(i,a) for(auto i:a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) #define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;} #define show2d(v) {rep(__,v.size())show1d(v[__]);} using namespace std; //kaewasuretyuui typedef long long ll; #define int ll typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; #define mt make_tuple typedef tuple<int,int,int> tp; typedef vector<tp> vt; typedef vector<vt>vvt; template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;} template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;} const double PI=acos(-1); const long double EPS=1e-9; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; #define yes cout<<"Yes\n" #define no cout<<"No\n" signed main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m; cin>>n>>m; vvi G(n,vi(n)); rep(i,m){ int a,b; cin>>a>>b; a--;b--; G[a][b]=G[b][a]=1; } int N=1<<n; vi dp(N,inf); dp[0]=0; rep(i,N){ for(int j=i;j>0;j=(j-1)&i){ cmin(dp[i],dp[j]+dp[i-j]); } bool h=true; rep(j,n)rep(k,n)if(k-j&&(i&1<<j)&&(i&1<<k)&&G[j][k]==0) h=false; if(h)dp[i]=1; } cout<<dp.back()<<endl; }
#include <bits/stdc++.h> //#include "debugger.h" #define int long long #define pb push_back #define pp pop_back #define f first #define s second #define all(x) x.begin(),x.end() using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; string s; cin >> s; int q; cin >> q; int flag = 0; while (q--) { int t, a, b; cin >> t >> a >> b; if (t == 1) { if (flag) { a -= 1; b -= 1; if (a >= n) a = a - n; else a = a + n; if (b >= n) b = b - n; else b = b + n; swap(s[a], s[b]); } else { swap(s[a - 1], s[b - 1]); } } else { flag = 1 - flag; } // cout << s << "\n"; } if (flag) s = s.substr(n) + s.substr(0, n); cout << s; return 0; }
#include<bits/stdc++.h> #define MOD 1000000007 #define MAX 1000 using namespace std; int a[MAX+10],b[MAX+10],dp[MAX+10][MAX+10]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin>>n>>m; for(int i=1;i<=n;++i) cin>>a[i]; for(int i=1;i<=m;++i) cin>>b[i]; for(int i=0;i<=n;++i) { for(int j=0;j<=m;++j) { if(i==0) dp[i][j] = j; else if(j==0) dp[i][j] = i; else dp[i][j] = min({dp[i-1][j-1]+(int)(a[i]!=b[j]),dp[i-1][j]+1,dp[i][j-1]+1}); } } cout<<dp[n][m]<<endl; return 0; }
/*/ Author: _Math.man /*/ #include<bits/stdc++.h> using namespace std; /*/---------------------------Defines-----------------------------------------/*/ #pragma GCC optimize("Ofast") #define int long long #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL); #define pb push_back #define eb emplace_back #define fn for(int i =0 ;i <n;i++) #define fn1 for( int i =1;i<=n; i++) #define fm for(int j =0 ;j <m;j++) #define fm1 for(int j =1;j<=m;j++) #define fi first #define se second #define endl '\n' #define PI 3.14159265358979323846 #define MOD 1000000007 #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() const int N = 1e3+5; const int INF = 1e18L; // for all eight directions int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; // for all 4 directions //int dx[4]={-1,1,0,0}; //int dy[4]={0,0,-1,1}; //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); /*/-----------------------------Debug begins----------------------------------/*/ vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } template<typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; s << "["; for(int i = 0; i < n - 1; i++) s << arr[i] << ","; if(n>0) s << arr[n - 1] ; s<< "]"; return s.str(); } #define trace(args...) {__trace_begin(__LINE__); __trace(split(#args, ',').begin(), args);} inline void __trace_begin(int line) { cerr << "#" << line << ": "; } template<typename T> inline void __trace_out_var(vector<T> val) { cerr << arrStr(val, val.size()); } template<typename T> inline void __trace_out_var(T* val) { cerr << arrStr(val, 10); } template<typename T> inline void __trace_out_var(T val) { cerr << val; } inline void __trace(vector<string>::iterator it) { cerr << endl; } template<typename T, typename... Args> inline void __trace(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << "="; __trace_out_var(a); cerr << "; "; __trace(++it, args...); } /*/-----------------------------Code begins----------------------------------/*/ int ar[N],br[N]; int dp[N][N]; int n,m; int solve(int i,int j){ if(i==n){ return m-j; } if(j==m){ return n-i; } if(dp[i][j]!=-1)return dp[i][j]; int ans = INF; ans=min(ans,(ar[i]!=br[j]?1:0) + solve(i+1,j+1)); ans=min(ans,1+solve(i+1,j+1)); ans=min(ans,1+min(solve(i+1,j),solve(i,j+1))); return dp[i][j]=ans; } signed main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif IOS; int T=1; // cin >> T; while(T--){ cin >> n >> m; fn{ cin >> ar[i]; } fm{ cin >>br[j]; } memset(dp,-1,sizeof(dp)); cout << solve(0,0); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) {for(auto i: x){cout << i << " ";} cout << endl;} #define isin(x,l,r) ((l) <= (x) && (x) < (r)) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using tii = tuple<int,int,int>; template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return 1;} else return 0;} template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return 1;} else return 0;} ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a;} ll lcm(ll a, ll b) { return a / gcd(a, b) * b;} vector<int> makeDivisors(int n) { vector<int> ret; for(int i = 1; i * i <= n; i++) { if(n%i == 0) { ret.push_back(i); if(i*i != n) ret.push_back(n/i); } } sort(ret.begin(), ret.end()); return ret; } int main() { int N; cin >> N; vector<int> A(N); rep(i, N) cin >> A[i]; unordered_map<int, int> mp; auto add = [&](int a, int b) { int p = mp[b]; mp[b] = gcd(p, a); }; int l = INT_MAX; rep(i, N) { vector<int> B = makeDivisors(A[i]); for (int b : B) { add(A[i], b); } chmin(l, A[i]); } ll ans = 0; for (auto [k, v] : mp) { if (k <= l && k == v) ans++; } cout << ans << '\n'; return 0; // get dp together, ll, paraphrase, debug } //
#include<iostream> using namespace std; long int max(long int a,long int b) { if(a>=b) return a; else return b; } long int min(long int a,long int b) { if(a>=b) return b; else return a; } long int gcd(long int a,long int b) { if(a%b==0) return b; else return gcd(b,a%b); } int main() { int N; cin >> N; long int ans; for(int i=0;i<N;i++){ long int a_i; cin >> a_i; if(i==0){ ans = a_i; } else{ ans = gcd(max(a_i,ans),min(a_i,ans)); } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<vector> #include<cstring> #include<map> #include<set> #include<cstdlib> #include<bitset> using namespace std; #define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) typedef long long ll; #define N 200005 ll p=1e9+7; ll n,m,f[N][17][17]={0},v[22]={0},sm; char a[N]; int main() { ll i,j,k,a1,a2,an=0; scanf("%s",a+1); n=strlen(a+1); for(ll i=1;i<=n;i++) if(a[i]>='0'&&a[i]<='9') a[i]-='0'; else a[i]-='A'-10; scanf("%lld",&m); for(ll i=0;i<=m;i++) f[0][i][i]=1; for(ll i=1;i<=n;i++) for(ll j=1;j<=m;j++) for(ll k=j;k<=m;k++) f[i][j][k]=((ll)f[i-1][j][k-1]*(17ll-k)+(ll)f[i-1][j][k]*k)%p; for(ll i=1;i<=n;i++) an=(an+15ll*f[i-1][1][m])%p; // for(ll i=1;i<=n;i++) // { // for(ll j=1;j<=m;j++) // for(ll k=j;k<=m;k++)printf("%lld\n",f[i][j][1]); // } for(ll i=1;i<=n;i++) { for(ll j=a[i]+1;j<=15;j++) an=(an-f[n-i][sm+(!v[j])][m]+p)%p; //printf("%lld\n",an); sm+=!v[a[i]]; v[a[i]]=1; } printf("%lld",an); }
#include <iostream> #include <cstdio> #include <vector> #include <iomanip> #include <unordered_map> #include <unordered_set> #include <algorithm> #include <map> #include <set> #include <cmath> using namespace std; using VLL = vector <long long>; using VVLL = vector <VLL>; int main() { int N; int K; long long M; cin >> N >> K >> M; VVLL dp(N+1, VLL(N*N*K)); dp[0][0] = 1; int theMax = 0; for (int n = 1; n <= N; ++n) { theMax += K*n; for (int k = 0; k <= theMax; ++k) { // dp[i][val] = Sigma i = 0 ... K dp[i-1][val-i*n] = dp[i-1][val] + dp[i][val-n] - dp[i-1][val-n*(K+1)]; dp[n][k] = dp[n-1][k]; if (k-n >= 0) { dp[n][k] += dp[n][k-n]; } if (k-n*(K+1) >= 0) { dp[n][k] -= dp[n-1][k-n*(K+1)]; } dp[n][k] %= M; } } for (int n = 1; n <= N; ++n) { int left = n-1; int right = N-n; long long ans = K; if (left != 0 && right != 0) { for (int v = 1; v <= theMax; ++v) { ans += dp[left][v] * dp[right][v] % M * (K+1) % M; } } ans = (ans%M + M) % M; cout << ans << endl; } return 0; }
/* Problem name : A - i Scream Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100 points Problem Statement In Japan, there are four major categories of ice cream type products: an ice cream type product with at least 15 percent milk solids and at least 8 percent milk fat is called an ice cream; an ice cream type product with at least 10 percent milk solids and at least 3 percent milk fat that is not an ice cream is called an ice milk; an ice cream type product with at least 3 percent milk solids that is not an ice cream or an ice milk is called a lacto ice; an ice cream type product that is not an ice cream, an ice milk, or a lacto ice is called a flavored ice. Here, milk solids consists of milk fat and milk solids-not-fat. AtCoder's famous product Snuke Ice contains A percent milk solids-not-fat and B percent milk fat. Which of the categories above does Snuke Ice fall into? Print your answer as an integer according to the Output section. Constraints 0≤A≤100 0≤B≤100 A+B≤100 A and B are integers. */ # include <cstdio> using namespace std; int a,b; int main() { scanf("%d %d",&a,&b); a += b; if (a >= 15 && b >= 8) printf("1"); else if (a >= 10 && b >= 3) printf("2"); else if (a >= 3) printf("3"); else printf("4"); return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define pp pop_back #define ll long long #define pb push_back #define ld long double #define debug cout << "OK\n"; #define all(x) x.begin(), x.end() using namespace std; const int N = 5e5 + 200; const int mod = 1e9 + 7; const int mod2 = 998244353; void usaco(string filename) { freopen((filename + ".in").c_str(), "r", stdin); freopen((filename + ".out").c_str(), "w", stdout); } void data() { #ifdef NURS freopen("main.in", "r", stdin); freopen("main.out", "w", stdout); #endif } main() { data(); int a, b; cin >> a >> b; cout << max(0, a * 2 + 100 - b); } /* test 5 1 2 3 2 1 */
// include #include <bits/stdc++.h> using namespace std; // conversion inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // change template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } // math template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T nCr(T n, T r) { T num = 1; for (int i = 1; i <= r; ++i) { num *= (n - i + 1) / i; } return num; } template <class T> inline T nPr(T n, T r) { r = n - r; T sum = 1; int i; for (i = n; i >= r + 1; --i) sum *= i; return sum; } template <class T> inline T facctorial(T k) { T sum = 1; for (int i = 1; i <= k; ++i) { sum *= i; } return sum; } // numeric template <class T> inline T gcd(T a, T b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? gcd(b, a % b) : a; } template <class T> inline T lcm(T a, T b) { return a * b / gcd(a, b); } // typedef using VI = vector<int>; using VVI = vector<VI>; using VS = vector<string>; using PII = pair<int, int>; using LL = int64_t; // container util #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define SZ(a) static_cast<int>((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) // repetition #define FOR(i, a, b) for (int i = (a); i <= static_cast<int>(b); ++i) #define REP(i, n) FOR(i, 0, n) // constant constexpr double EPS = 1e-10; const double PI = acos(-1.0); constexpr LL INF = 1e10; // clear memory #define CLR(a) memset((a), 0, sizeof(a)) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, s, d; cin >> n >> s >> d; vector<PII> p(n); VI x(n), y(n); REP(i, n) { cin >> p[i].first >> p[i].second; } sort(ALL(p)); REP(i, n) { if (p[i].first < s && p[i].second > d) { cout << "Yes" << "\n"; return 0; } } cout << "No" << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long typedef pair<ll,ll> ii; const ll inf=1e15; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n,tot,nextVil=-1; priority_queue<ii,vector<ii>,greater<ii>> pq; cin>>n>>tot; ll a,b; for (int i=0;i<n;i++){ cin>>a>>b; pq.push(make_pair(a,b)); } ll y=0; // b is yen friend gives ll cnt=0; while (cnt<n && tot>=nextVil){ tot+=y; nextVil=pq.top().first; y=pq.top().second; pq.pop(); cnt++; } cout<<tot+((tot>=nextVil)?y:0); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) using namespace std; using ll = long long; const int INF = 1e9; const ll LINF = 1e18; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < sz(v) - 1) os << ' '; } return os; } const int n = 50; const vector<int> dy = {0, 0, -1, 1}; const vector<int> dx = {-1, 1, 0, 0}; int t[n][n], p[n][n]; string dirs = "LRUD"; struct P { int y, x; char dir; P() { } P(int _y, int _x) : y(_y), x(_x) { } P(int _y, int _x, char _dir) : y(_y), x(_x), dir(_dir) { } bool valid() { return 0 <= x and x < n and 0 <= y and y < n; } vector<P> nxt(bitset<2500>& used) { vector<P> ret; rep(d, 4) { int ny = y + dy[d]; int nx = x + dx[d]; if (P(ny, nx).valid() and !used[t[ny][nx]]) { ret.emplace_back(ny, nx, dirs[d]); } } return ret; } }; int check(P pos, bitset<2500>& used, int dep, int limit = 4) { if (dep == limit) return dep; int ret = 0; used[t[pos.y][pos.x]] = 1; for (P nxt : pos.nxt(used)) { chmax(ret, check(nxt, used, dep + 1, limit)); } used[t[pos.y][pos.x]] = 0; return ret; } random_device seed_gen; mt19937_64 rnd(seed_gen()); int main() { clock_t start = clock(); auto tm = [&]() { return static_cast<double>(double(clock() - start) / CLOCKS_PER_SEC * 1000.0); }; auto random_coice = [&](vector<P>& v) -> P { int idx = uniform_int_distribution<int>(0, sz(v) - 1)(rnd); return v[idx]; }; int sy, sx; cin >> sy >> sx; rep(i, n) rep(j, n) cin >> t[i][j]; rep(i, n) rep(j, n) cin >> p[i][j]; string ans; int score = 0; int try_cnt = 0; while (tm() < 1950 && ++try_cnt) { bitset<2500> used; P pos = {sy, sx}; string cur; int cur_score = 0; while (1) { cur_score += p[pos.y][pos.x]; used[t[pos.y][pos.x]] = 1; vector<P> nxts; int MAX = -1; // cerr << pos.y << ' ' << pos.x << '\n'; for (auto nxt : pos.nxt(used)) { int tmp = check(nxt, used, 0); // cerr << nxt.dir << " : " << tmp << '\n'; if (tmp == MAX) { nxts.push_back(nxt); } else if (chmax(MAX, tmp)) { nxts.clear(); nxts.push_back(nxt); } } if (nxts.empty()) break; pos = random_coice(nxts); cur += pos.dir; } if (chmax(score, cur_score)) { ans = cur; } } cout << ans << '\n'; cerr << "score : " << score << '\n'; cerr << "cnt : " << try_cnt << '\n'; }
#include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> #include <cassert> #include <climits> #include <ctime> #include <numeric> #include <vector> #include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <iomanip> #include <complex> #include <deque> #include <functional> #include <list> #include <map> #include <string> #include <sstream> #include <set> #include <unordered_set> #include <unordered_map> #include <stack> #include <queue> #include <forward_list> #include <thread> #include <random> #include <array> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back void io() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { io(); int si,sj; cin>>si>>sj; int pos[50][50]; for(int i=0; i<50; i++){ for(int j=0; j<50; j++){ cin>>pos[i][j]; } } bool chek[3000]; memset(chek,false,sizeof(chek)); int arr[50][50]; for(int i=0; i<50; i++){ for(int j=0; j<50; j++){ cin>>arr[i][j]; } } string str=""; chek[pos[si][sj]]=true; while(true){ if(si!=49 && !chek[pos[si+1][sj]]){ chek[pos[si+1][sj]]=true; si++; str+='D'; } else if(sj!=49 && !chek[pos[si][sj+1]]){ chek[pos[si][sj+1]]=true; sj++; str+='R'; } else if(si!=0 && !chek[pos[si-1][sj]]){ chek[pos[si-1][sj]]=true; si--; str+='U'; } else if(sj!=0 && !chek[pos[si][sj-1]]){ chek[pos[si][sj-1]]=true; sj--; str+='L'; } else{ break; } } cout<<str<<endl; } //Aneegha Thithiesha Mahabaduge.
// #undef _GLIBCXX_DEBUG // #define NDEBUG #include <bits/extc++.h> using i64 = int_fast64_t; using namespace std; int main() { i64 x, y; cin >> x >> y; unordered_set<i64> usd; priority_queue<pair<i64, i64>, vector<pair<i64, i64>>, greater<>> q; q.emplace(0, y); i64 ans = 1e18; while (!q.empty()) { auto [c, z] = q.top(); q.pop(); if (usd.count(z)) continue; usd.emplace(z); ans = min(ans, abs(z - x) + c); if (!(z % 2)) { q.emplace(c + 1, z / 2); } else if (z > 1) { q.emplace(c + 2, (z + 1) / 2); q.emplace(c + 2, (z - 1) / 2); } } cout << ans << "\n"; }
#include <iostream> using namespace std; typedef long long ll; #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)<(y)?(y):(x)) ll comb(ll n, ll k){ if (k == 0 || k == n) return 1; if (k == 1 || k == n-1) return n; return comb(n-1,k)+comb(n-1,k-1); } int main(){ ll n,k;cin >> n >> k;--k; for(ll xyz=3;xyz<=3*n;++xyz){ if(xyz <= n+1){ ll s = (xyz-1)*(xyz-2)/2; if(s <= k){ k -= s; continue; } for(ll x=1;x<=xyz-2;++x){ ll c = xyz-1-x; if(c <= k){ k -= c; continue; } ll y=1+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } }else if(xyz < 2*n+1){ ll s = (3*n-xyz+2)*(xyz-n-1)/2; if(s <= k){ k -= s; }else{ for(ll x=1;x<=xyz-n-1;++x){ ll c = 2*n-xyz+1+x; if(c <= k){ k -= c; continue; } ll y=xyz-n-x+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } } s = (xyz-2)*(2*n-xyz+1)/2; if(s <= k){ k -= s; continue; } for(ll x=xyz-n;x<=n;++x){ ll c = xyz-1-x; if(c <= k){ k -= c; continue; } ll y=1+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } }else{ ll s = (3*n-xyz+2)*(3*n-xyz+1)/2; if(s <= k){ k -= s; continue; } for(ll x=xyz-2*n;x<=n;++x){ ll c = 2*n-xyz+1+x; if(c <= k){ k -= c; continue; } ll y=xyz-n-x+k; cout << x << " " << y << " " << (xyz-x-y) << "\n"; return 0; } } } return 0; }
#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 var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define mem(x, n) memset(x, n, sizeof(x)) #define all(x) x.begin(), x.end() #define endl "\n" const int N = 1e5 + 5; struct station { ll city, time, k; }; vector<station> g[N]; ll dist[N]; int main() { faster; mem(dist, -1); ll n, m, x, y; cin >> n >> m >> x >> y; for (int i = 0; i < m; i++) { int a, b, t, k; cin >> a >> b >> t >> k; station curr, curr1; curr.city = b, curr.time = t, curr.k = k; curr1 = curr; curr1.city = a; g[a].push_back(curr); g[b].push_back(curr1); } priority_queue<pll, vector<pll>,greater<pll>> pq; dist[x] = 0; // first = from, second = time pq.emplace(x, 0); while (pq.size()) { auto fr = pq.top(); pq.pop(); if (dist[fr.first] != -1 && dist[fr.first] < fr.second) { continue; } for (auto next : g[fr.first]) { ll nextStartTime = ((fr.second + next.k - 1) / next.k) * next.k; if (dist[next.city] == -1 || nextStartTime + next.time < dist[next.city]) { dist[next.city] = nextStartTime + next.time; pq.emplace(next.city, dist[next.city]); } } } cout << dist[y] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int #define mp(a,b) make_pair(a,b) #define vi vector<int> #define mii map<int,int> #define mpi map<pair<int,int>,int> #define vp vector<pair<int,int> > #define pb(a) push_back(a) #define fr(i,n) for(i=0;i<n;i++) #define rep(i,a,n) for(i=a;i<n;i++) #define F first #define S second #define endl "\n" #define fast std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define mod 1000000007 #define dom 998244353 #define pi 3.14159265 #define sl(a) (int)a.length() #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() #define pii pair<int,int> #define mini 2000000000000000000 #define time_taken 1.0 * clock() / CLOCKS_PER_SEC //mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); //primes for hashing 937, 1013 template<typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template<typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } void shikhar7s(int cas) { int n,m,x,y; cin>>n>>m>>x>>y; vector<pair<pii,pii> > adj[n+5]; int u,v,w[m],z,i; fr(i,m) { cin>>u>>v>>w[i]>>z; adj[u].pb(mp(mp(v,i+1),mp(w[i],z))); adj[v].pb(mp(mp(u,i+1),mp(w[i],z))); } mpi d; d[mp(x,0ll)]=0; set<pair<int,pii> > s; s.insert(mp(0ll,mp(x,0ll))); int ans=mini; while(sz(s)) { auto it=s.begin(); int dis=(*it).F; u=(*it).S.F; v=(*it).S.S; s.erase(it); if(u==y&&!v) { cout<<dis; return; } if(v>0) { if(!d.count(mp(u,0ll))||d[mp(u,0ll)]>dis+w[v-1]) { if(s.find(mp(d[mp(u,0ll)],mp(u,0ll)))!=s.end()) s.erase(mp(d[mp(u,0ll)],mp(u,0ll))); d[mp(u,0ll)]=dis+w[v-1]; s.insert(mp(d[mp(u,0ll)],mp(u,0ll))); } } else { fr(i,sz(adj[u])) { v=adj[u][i].F.F; z=adj[u][i].S.S; int j=adj[u][i].F.S; if(!d.count(mp(v,j))||d[mp(v,j)]>dis+((z-(dis%z))%z)) { if(s.find(mp(d[mp(v,j)],mp(v,j)))!=s.end()) s.erase(mp(d[mp(v,j)],mp(v,j))); d[mp(v,j)]=dis+((z-(dis%z))%z); s.insert(mp(d[mp(v,j)],mp(v,j))); } } } } cout<<-1; } signed main() { fast; //freopen("input.txt", "rt", stdin); //freopen("output.txt", "wt", stdout); int t=1; //cin>>t; int cas=1; while(cas<=t) { shikhar7s(cas); cas++; } 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 mp make_pair #define pb(x) push_back(x) #define vll vector<long long> #define pll pair<long long, long long> #define mll map<long long, long long> #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define gcd __gcd #define clr(x) memset(x, 0, sizeof(x)) #define mod 1000000007LL #define mod2 998244353LL #define INF 1e18 typedef long long ll; typedef long double ld; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> o_tree; void solve() { ll n,m,x,y; cin>>n>>m>>x>>y; vector<vector<array<ll,3>>> v(n); x--,y--; ll i,j; for(i=0;i<m;i++) { ll a,b,t,k; cin>>a>>b>>t>>k; a--,b--; v[a].push_back({b,t,k}); v[b].push_back({a,t,k}); } priority_queue<pll,vector<pll>,greater<pll>> q; vll time(n,INF); time[x]=0; vll vis(n,0); q.push(mp(time[x],x)); while(!q.empty()) { pll topp = q.top(); q.pop(); if(vis[topp.second])continue; if(time[topp.second]!=topp.first)continue; vis[topp.second]=1; ll node = topp.second; ll timenow = topp.first; for(auto nei:v[node]) { ll nowk = nei[2]; ll nxt = nei[0]; ll timetravel = ((timenow+nowk-1)/nowk)*nowk + nei[1]; if(timetravel<time[nxt]) { time[nxt] = timetravel; q.push(mp(time[nxt],nxt)); } } } if(time[y]==INF) { cout<<"-1\n"; return; } cout<<time[y]<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); if (fopen("input.txt","r" )) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } cout<<setprecision(20); ll t=1; // cin>>t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;i++) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;i--) inline int read() { int x=0,f=1;char c=getchar(); while(c<48||c>57){if(c=='-')f=-1;c=getchar();} while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } using namespace std; const int M=505; int n; int C[M][M]; int A[M],B[M]; int main() { n=read(); rep(i,1,n)rep(j,1,n)C[i][j]=read(); rep(i,1,n)B[i]=C[1][i]; rep(i,1,n)A[i]=C[i][1]-B[1]; rep(i,1,n)rep(j,1,n)if(C[i][j]!=A[i]+B[j])return puts("No"),0; // 现在以构造出答案,需要使他们均为非负整数 int mi=1e9; rep(i,1,n)mi=min(mi,A[i]); if(mi>=0){ puts("Yes"); rep(i,1,n)printf("%d ",A[i]);puts(""); rep(i,1,n)printf("%d ",B[i]);puts(""); }else { rep(i,1,n){ B[i]+=mi; A[i]-=mi; if(B[i]<0)return puts("No"),0; } puts("Yes"); rep(i,1,n)printf("%d ",A[i]);puts(""); rep(i,1,n)printf("%d ",B[i]);puts(""); } return 0; }
// Problem: A - twiblr // Contest: AtCoder - AtCoder Beginner Contest 182 // URL: https://atcoder.jp/contests/abc182/tasks/abc182_a // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define endl '\n' #define pb push_back #define int long long #define vi vector<int> #define mii map<int, int> #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define fill(a,b) memset(a, b, sizeof(a)) #define setbits(x) __builtin_popcountll(x) #define rep(i,x,y) for(int i=(int)x; i<y; i++) #define px(a) for(auto x: a) cout<<x<<" "; cout<<'\n'; #define pxy(a,n) rep(i,0,n) {px(a[i])}; const long long MOD=1e9+7,N=1e5+5; int solve() { int n,ans=0; cin>>n; vi a(n); rep(i,0,n) cin>>a[i]; px(a); return ans; } int32_t main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //Remove incase of Online Query int a,b; cin>>a>>b; cout<<abs(2*a+100-b)<<endl; }
#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 998244353 //#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 bs(yup,x) binary_search(yup.begin(),yup.end(),x) //it will return bollean value #define lb(yup,x) lower_bound(yup.begin(),yup.end(),x) //it will return the adress of the number which is equal to or just greater than x ,and if it is equal to yup.end() than their in no such number exist #define ub(yup,x) upper_bound(yup.begin(),yup.end(),x) #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) using namespace std; const long long N=1000, INF=2000000000000000000; 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; } int32_t main() { IOS; //fill(isprime, true); //pre(); int t=1; //cin>>t; while(t--) { pii p[1001]; rep(i,0,1001) { p[i].ff=0; p[i].ss=i; } int n; cin>>n; int a[n]; rep(i,0,n) { cin>>a[i]; rep(j,2,1001) { if(a[i]%j==0) p[j].ff++; } } sort(p,p+1001); cout<<p[1000].ss; } }
#include<bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long bigmod(long long a, long long p){ if(p == 0){ return 1; } long long ans = bigmod(a, p/2); ans = (ans * ans) % mod; if(p % 2 == 1){ ans = (ans * a) % mod; } return ans; } long long nCr(long long n, long long r){ if(r > n){ return 0; } long long lob = 1, hor = 1; for(long long i = 1; i <= r; i++){ lob = (lob * (n - i + 1)) % mod; hor = (hor * i) % mod; } hor = bigmod(hor, mod - 2); return (hor * lob) % mod; } int main(){ long long n, m; cin >> n >> m; long long sum = 0; for(long long i = 1; i <= n; i++){ long long x; cin >> x; sum += x; } cout << nCr(m + n, sum + n); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using namespace std; typedef long long ll; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { ll N, M, MOD = 1e9 + 7, ans = 1; cin >> N >> M; ll S = 0; vector<ll> A(N); rep(i, N) { cin >> A[i]; S += A[i]; } if (S > M) { cout << "0\n"; return 0; } rep(i, S + N) { ans *= M + N - i; ans %= MOD; ans *= mod_pow(i + 1, MOD - 2, MOD); ans %= MOD; } cout << ans << "\n"; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using vvvll = vector<vector<vector<ll>>>; using vvvvll = vector<vector<vector<vector<ll>>>>; using pl4 = pair<ll,ll>; using str = string; using vpl4 = vector<pair<ll,ll>>; #define sz size() #define be begin() #define en end() #define fi first #define se second #define pb push_back #define mp make_pair #define llin(x) ll (x);cin >>(x); #define stin(x) str (x);cin >>(x); #define vllin(x,n) vll (x)(n);FOR(i,0,n-1){cin >>(x)[i];} #define vllin2(a,b,n) vll (a)(n);vll (b)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];} #define vllin3(a,b,c,n) vll (a)(n);vll (b)(n);vll (c)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];} #define vlling(x,n) (x).assign(n,0);FOR(i,0,n-1){cin >>(x)[i];} #define vlling2(a,b,n) (a).assign(n,0);(b).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];} #define vlling3(a,b,c,n) (a).assign(n,0);(b).assign(n,0);(c).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];} #define vpl4in(x,n) vpl4 (x)((n),mp(0,0));FOR(i,0,n-1){cin >>x[i].fi>>x[i].se;} #define FOR(i,a,b) for(ll i = a ; i <= b ; i++) #define rFOR(i,b,a) for(ll i = a; i >= b ; i--) #define SORT(x) sort(x.be, x.en) #define rSORT(x) sort(x.rbegin(), x.rend()) #define say(x) cout<<(x); #define sal(x) cout<<(x)<<endl; #define says(x) cout<<(x)<<(' '); #define sas cout<<(' '); #define sayR(x) cout<<fixed<<setprecision(10)<<(x); #define salR(x) cout<<fixed<<setprecision(10)<<(x)<<endl; #define yn(a) cout <<((a)?"yes":"no")<<endl; #define Yn(a) cout <<((a)?"Yes":"No")<<endl; #define YN(a) cout <<((a)?"YES":"NO")<<endl; #define Imp(a) cout <<((a)?"Possible":"Impossible")<<endl; #define IMP(a) cout <<((a)?"POSSIBLE":"IMPOSSIBLE")<<endl; #define pow(a,b) ll(pow(a,b)) ll MOD=1000000007; ll INF=100000000000001; vector<ll> value; // ノードの値を持つ配列 ll N; // 葉の数 void update(ll i, ll x) { // i 番目の葉の値を x に変える i += N - 1; // i 番目の葉のノード番号 value[i] = x; while (i > 0) { i = (i - 1) / 2; // ノード i の親ノードの番号に変える value[i] = min(value[i * 2 + 1], value[i * 2 + 2]); // 左右の子の min を計算しなおす } } ll query(ll a, ll b, ll k, ll l, ll r) { // [a, b) の区間に対するクエリについて // ノード k (区間 [l, r) 担当)が答える if (r <= a || b <= l) return INF; // 区間が被らない場合は INF を返す if (a <= l && r <= b) return value[k]; // ノード k の担当範囲がクエリ区間 [a, b) // に完全に含まれる else { ll c1 = query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子に値を聞く ll c2 = query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子に値を聞く return min(c1, c2); // 左右の子の値の min を取る } } ll n; vvll d(200001,vll(2,0)); vvll e(200001,vll(2,0)); vvll a(200001,vll(2,0)); vvll c(200001,vll(0)); long long mi(long long a) { ll m=MOD; long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } signed main(){ stin(s); FOR(i,0,s.sz-1){ if((i%2==0&&isupper(s[i]))||(i%2==1&&!isupper(s[i]))){ sal("No") return 0; } } sal("Yes") }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep_up(i, a, n) for (ll i = a; i < n; ++i) #define rep_down(i, a, n) for (ll i = a; i >= n; --i) #define P pair<ll, ll> #define Graph vector<vector<ll>> #define fi first #define se second #define vvvll vector<vector<vector<ll>>> #define vvll vector<vector<ll>> #define vll vector<ll> constexpr ll INF = (1ll << 60); constexpr ll mod = 1000000007; constexpr double pi = 3.14159265358979323846; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; } int main(){ ll n,m,a,b; cin>>n>>m>>a>>b; if(m*n<=b&&a*n>=b) cout<<"No"<<endl; else cout<<"Yes"<<endl; }
#include <bits/stdc++.h> using namespace std; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n; cin >> n; vector<vector<int>> a{{1, 0}}; for(auto i = 2; i <= n; ++ i){ vector<vector<int>> b; for(auto v: a){ vector<int> vv = v; vv.insert(vv.end(), v.begin(), v.end()); b.push_back(vv); vv = v; for(auto &x: v){ x = !x; } vv.insert(vv.end(), v.begin(), v.end()); b.push_back(vv); } vector<int> v(1 << i, 0); for(auto j = 0; j < 1 << i - 1; ++ j){ v[j] = 1; } b.push_back(v); swap(a, b); } cout << (int)a.size() << "\n"; for(auto v: a){ for(auto x: v){ x ? cout << "A" : cout << "B"; } cout << "\n"; } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////
#include<bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define Rep(i, j, n) for (ll i = j; i < (n); i++) #define repr(i, j, n) for (ll i = j; i >= (n); i--) #define all(v) v.begin(),v.end() #define puts(i) cout << i << endl #define INF INT_MAX #define INFL LLONG_MAX typedef long long ll; using namespace std; #define MOD 1000000007 int main(){ int n; cin >> n; vector<int> x(n),y(n); rep(i,n) cin >> x.at(i) >> y.at(i); bool ans = false; Rep(i,0,n-2){ Rep(j,i+1,n-1){ Rep(k,j+1,n){ int x1 = (x.at(i)-x.at(j)); int y1 = (y.at(i)-y.at(j)); int x2 = (x.at(j)-x.at(k)); int y2 = (y.at(j)-y.at(k)); if(x1*y2==x2*y1){ ans = true; } } } } if(ans==true) puts("Yes"); else puts("No"); }
#include <bits/stdc++.h> using namespace std; typedef long long int lld; typedef pair<int,int> pi; typedef pair<lld,lld> pl; typedef pair<int,lld> pil; typedef pair<lld,int> pli; typedef vector<int> vit; typedef vector<vit> vitt; typedef vector<lld> vlt; typedef vector<vlt> vltt; typedef vector<pi> vpit; typedef vector<vpit> vpitt; typedef long double ld; #define x first #define y second #define pb push_back #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define mk(a,b) make_pair(a,b) bool isrange(int y,int x,int n,int m){ if(0<=y&&y<n&&0<=x&&x<m) return true; return false; } int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1}; void solve(int tc){ int n; cin >> n; int p = 1; while(true){ if(p*(p+1)>=2*n){ cout << p << "\n"; break; } p++; } } int main(void){ /* ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); */ int tc = 1; /* cin >> tc; */ for(int test_number=1;test_number<=tc;test_number++){ solve(test_number); } return 0; }
#include "bits/stdc++.h" using namespace std ; #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long void solve() { long long n ; cin >> n ; vector<long long> v ; for(int i=1; i<=sqrt(n); i++) { if(n % i == 0) { cout << i << "\n" ; if(n / i != i) v.emplace_back(n / i) ; } } for(int i=v.size()-1; i>=0; i--) cout << v[i] << "\n" ; } int main() { fastio int t = 1; //cin >> t ; while(t--) { solve() ; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vd = vector<double>; using vs = vector<string>; using vb = vector<bool>; using mll = map<ll, ll>; using mld = map<ll, double>; using mls = map<ll, string>; using mlb = map<ll, bool>; using sl = set<ll>; using sd = set<double>; using ss = set<string>; using pll = pair<ll, ll>; using pls = pair<ll, string>; using pld = pair<ll, double>; using plb = pair<ll, bool>; template <class T> using pque = priority_queue<T>; const ll INFTY = 1L << 62L; template <typename T> void print_list(T &array, string sep = "\n", ll begin = 0) { string ans; for (ll i = begin; i < array.size(); i++) { ans += to_string(array[i]); if (i == array.size() - 1) { continue; } ans += sep; } printf("%s\n", ans.c_str()); } void print_list(vs &array, string sep = "\n", ll begin = 0) { string ans; for (ll i = begin; i < array.size(); i++) { ans += array[i]; if (i == array.size() - 1) { continue; } ans += sep; } printf("%s\n", ans.c_str()); } ll A, B, C; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> A >> B >> C; cout << A + B + C - min(A, min(B, C)) << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define int long long #define ll long long #define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr)) #define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr)) #define sz(a) sizeof(a) #define deb(a) cerr << " [" << #a << "->" << a << "] " #define next_line cerr << '\n' #define all(a) a.begin(), a.end() #define iter(it, s) for (auto it = s.begin(); it != s.end(); it++) #define setbits(x) __builtin_popcountll(x) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef pair<int, int> pii; typedef pair<ll,ll> pll; /* 1. If you cannot approach a problem directly in the way it is proposed then try to think from backwards 2. Have you checked the implementation before starting to write the code? 3. Have you read the question carefully ? 4. Keep the names of variable descriptive. 5. Check the types and range of the values properly 6. Erase a iterator in the set or map only after using its value. 7. 1 bitset of 1e9 size only takes 256 mb. 8. Try to use global variables in case of recursion 9. Short and precise exectutes faster */ void solve() { int n,k; cin >> n >> k; vector<int> count(n + 1, 0); F(int, i, 0, n -1, 1){ int x; cin >> x; count[x] ++; } int prev = 0, ans = 0; F(int, i, 0, n, 1){ int tk = (k - prev) - min(k - prev, count[i]); ans += (tk * i); prev += tk; } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // freopen("Debug.txt", "w", stderr); #else #endif // cout << fixed << setprecision(10); // int _t; // cin>>_t; // while(_t --) solve(); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e6+10; const double eps = 0.01; const double pi = 3.1415926; inline ll read() { ll x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();} while (isdigit(ch)){x=x*10+ch-48;ch=getchar();} return x*f; } inline ll pt(ll ans,ll f){ printf("%lld",ans); if(f==1) putchar(' '); else putchar('\n'); } ll n,k,m,tot; ll a[N],c[N],vis[N]; int main() { // freopen("../in.in","r",stdin); ll _; cin>>n; double x1,x2,y1,y2; cin>>x1>>y1>>x2>>y2; double a1=(x1+x2)/2,a2=(y1+y2)/2; double ag=(2*pi/n); x1-=a1,y1-=a2; // cout<<cos(ag)<<endl; printf("%.6f %.6f\n",a1+x1*cos(ag)-y1*sin(ag),a2+y1*cos(ag)+x1*sin(ag)); return 0; }
//#include <atcoder/maxflow.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #include <iostream> #include <map> #include <list> #include <set> #include <algorithm> #include <vector> #include <string> #include <functional> #include <queue> #include <deque> #include <stack> #include <unordered_map> #include <unordered_set> #include <cmath> #include <iterator> #include <random> #include <chrono> #include <complex> #include <bitset> #define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) int i; cin >> i #define readll(i) ll i; cin >> i #define readdouble(i) double i; cin >> i #define readstring(s) string s; cin >> s typedef long long ll; //using namespace __gnu_pbds; //using namespace atcoder; using namespace std; const ll modd = (1000LL * 1000LL * 1000LL + 7LL); //const ll modd = 998244353; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(12); // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // uniform_int_distribution<int> rand_gen(0, modd); // rand_gen(rng) gets the rand no // readint(test_cases); int test_cases = 1; forr(t, 1, test_cases) { readint(n); readdouble(x0); readdouble(y0); readdouble(xhalf); readdouble(yhalf); pair<double, double> xaxis(x0-xhalf, y0-yhalf); double lll = 2; xaxis.first /= lll; xaxis.second /= lll; pair<double, double> origin(0.5*(x0+xhalf), 0.5*(y0+yhalf)); pair<double, double> yaxis(-xaxis.second, xaxis.first ); double PI = 3.14159265359; // ans = or + xaxis * cos( 2*pi/n ) + yaxis * sin( 2*pi/n ) double ansx = origin.first + xaxis.first * cos( 2.0*PI/static_cast<double>(n) ) + yaxis.first * sin( 2.0*PI/static_cast<double>(n) ); double ansy = origin.second + xaxis.second * cos( 2.0*PI/static_cast<double>(n) ) + yaxis.second * sin( 2.0*PI/static_cast<double>(n) ); cout << ansx << " " << ansy << endl; } return 0; }
// #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define pi 3.141592653589793238 #define int long long #define ld long double using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; long long power(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a ; a = a * a ; b >>= 1; } return res; } const int mod=1e9+7; int dp[2005][2005]; int pre1[2005][2005]; int pre2[2005][2005]; int pre3[2005][2005]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE if(fopen("INPUT.txt","r")) { freopen ("INPUT.txt" , "r" , stdin); freopen ("OUTPUT.txt" , "w" , stdout); } #endif int n,m; cin>>n>>m; string a[n+1]; for(int i=1;i<=n;i++) { cin>>a[i]; a[i]="#"+a[i]; } dp[1][1]=1; pre1[1][1]=1; pre2[1][1]=1; pre3[1][1]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i==1 &&j==1) continue; if(a[i][j]=='#') continue; pre1[i][j]+=pre1[i-1][j]; pre2[i][j]+=pre2[i][j-1]; pre3[i][j]+=pre3[i-1][j-1]; pre1[i][j]%=mod; pre2[i][j]%=mod; pre3[i][j]%=mod; dp[i][j]=(pre1[i][j]+pre2[i][j]+pre3[i][j])%mod; pre1[i][j]+=dp[i][j]; pre2[i][j]+=dp[i][j]; pre3[i][j]+=dp[i][j]; pre1[i][j]%=mod; pre2[i][j]%=mod; pre3[i][j]%=mod; } } cout<<dp[n][m]; }
#include<stdio.h> #include<bits/stdc++.h> #define ll long long int #define inf 1000000000000 #define mod 1000000007 #define all(v) v.begin(),v.end() #define fi first #define se second #define sz(x) (int)x.size() #define ps(x,y) fixed<<setprecision(y)<<x using namespace std; const int N = 2005; ll dp[N][N], H[N][N], V[N][N], D[N][N]; char mat[N][N]; ll add(ll x, ll y) { x += y; if (x >= mod) return x - mod; return x; } int check(int i, int j) { if (i < 1 || j < 1 || mat[i][j] != '#')return 0; return 1; } int h, w; void solve() { cin >> h >> w; memset(dp, 0, sizeof(dp)); memset(H, 0, sizeof(H)); memset(V, 0, sizeof(V)); memset(D, 0, sizeof(D)); for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> mat[i][j]; } } dp[1][1] = 1; H[1][1] = V[1][1] = D[1][1] = 1; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (i == 1 && j == 1) { continue; } if (mat[i][j] == '#') { continue; } dp[i][j] = add(H[i - 1][j], add(V[i][j - 1], D[i - 1][j - 1])); H[i][j] = V[i][j] = D[i][j] = dp[i][j]; if (!check(i - 1, j))H[i][j] = add(H[i][j], H[i - 1][j]); if (!check(i, j - 1))V[i][j] = add(V[i][j], V[i][j - 1]); if (!check(i - 1, j - 1))D[i][j] = add(D[i][j], D[i - 1][j - 1]); } } cout << dp[h][w] << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // scanf("%d", &t); while (t--) { solve(); } }
#include <iostream> #include <vector> #include <numeric> using namespace std; using ll = int64_t; ll score(string S){ vector<ll> cnt(10); iota(cnt.begin(), cnt.end(), 0); for(char c : S) cnt[c - '0'] *= 10; return accumulate(cnt.begin(), cnt.end(), 0); } int main(){ ll K; string S, T; cin >> K >> S >> T; vector<ll> cnt(10, K); for(char c : S + T) cnt[c - '0']--; ll win = 0; for(ll x = 1; x <= 9; x++) for(ll y = 1; y <= 9; y++){ S.back() = '0' + x; T.back() = '0' + y; if(score(S) <= score(T)) continue; win += cnt [x] * (cnt [y] - (x == y)); } const ll rem = 9 * K - 8; cout << double (win) / rem / (rem - 1) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll,ll> #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) #define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I) #define TO(x,t,f) ((x)?(t):(f)) #define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted #define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted #define REV(x) (reverse(x.begin(),x.end())) //reverse ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);} ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);} #define NEXTP(x) next_permutation(x.begin(),x.end()) const ll INF=ll(1e16)+ll(7); const ll MOD=1000000007LL; #define out(a) cout<<fixed<<setprecision((a)) //tie(a,b,c) = make_tuple(10,9,87); #define pop_(a) __builtin_popcount((a)) ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;} #define num_l(a,v) POSL(a,v) //v未満の要素数 #define num_eql(a,v) POSU(a,v) //v以下の要素数 #define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数 #define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数 // static_cast< long long ll > //chrono::system_clock::time_point start, end; clock_t start; bool did[50*52]; int tile[51][51]; int point[51][51]; int N = 50; int si,sj; string now = ""; int len = 0; string ans=""; int score = 0; int best_s = 0; string D = "DURL"; int di[] = {1,-1,0,0}; int dj[] = {0,0,1,-1}; void calc(int i,int j,char &x){ if(i<0 || j<0 || i>=50 || j>=50)return; if(did[ tile[i][j] ])return; clock_t end = clock(); double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0; if(time >= 1950)return; now[len] = x; len++; did[ tile[i][j] ] = true; score += point[i][j]; if(score > best_s){ best_s = score; ans = now.substr(0,len); } FOR(k,0,4){ calc(i+di[k],j+dj[k],D[k]); } len--; did[ tile[i][j] ] = false; score -= point[i][j]; } int main(){ FOR(i,0,N*N) now += 'D'; mt19937_64 mt(1); uniform_real_distribution<double> rnd(0.00,1.00); cin >> si >> sj; FOR(i,0,N)FOR(j,0,N){ cin >> tile[i][j]; } FOR(i,0,N)FOR(j,0,N){ cin >> point[i][j]; } start = clock(); char a = 'U'; calc(si,sj,a); if(ans.size() == 0) ans = "U"; cout << ans.substr(1,ans.size()-1) << endl; }
#include <bits/stdc++.h> int main() { long long n; std::cin >> n; // 3で割って int digit = 0; int sum = 0; int n_zero = 0; // 余り0の倍数の数 int n_one = 0; // 余り1の倍数の数 int n_two = 0; // 余り2の倍数の数 // std::cout << n << std::endl; // std::string s; while (n) { int d = n%10; ++digit; sum += d; n_zero += (d%3 == 0); n_one += (d%3 == 1); n_two += (d%3 == 2); // s += ('0' + n%10); n /= 10; } int ans = -1; // 全部3の倍数 if (sum % 3 == 0) ans = 0; else if (sum % 3 == 1) { if (n_one >= 1 && digit >= 2) ans = 1; else if (n_two >= 2 && digit >= 3) ans = 2; } else if (sum % 3 == 2) { if (n_two >= 1 && digit >= 2) ans = 1; else if (n_one >= 2 && digit >= 3) ans = 2; } std::cout << ans << std::endl; // std::cout << s << std::endl; // std::cout << n_zero << std::endl; // std::cout << n_one << std::endl; // std::cout << n_two << std::endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long void make(vector<int>& v,vector<int>v1,int index,int &cnt){ if(v1.size()>4) return; if(v1.size()==4){ map<int,int> m; for(auto i: v1){ m[i]++; //cout<<i<<" "; } //cout<<"\n"; int pro=1; for(auto i: m){ if(i.second==2) pro*=2; else if(i.second==3) pro*=6; else if(i.second==4) pro*=24; } cnt+=(24/pro); return ; } for(int i=index;i<v.size();i++){ v1.push_back(v[i]); make(v,v1,i,cnt); v1.pop_back(); } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); string s; cin>>s; vector<int> v,v1; int cnt=0; for(int i=0;i<10;i++){ if(s[i]=='o') v.push_back(i); else if(s[i]=='?') v1.push_back(i); } if(v.size()>4) cout<<"0"; else if(v.size()==4) cout<<"24"; else{ vector<int> v2; //cout<<v.size()<<" "<<v1.size()<<" "; for(int i=0;i<v.size();i++){ v2.push_back(v[i]); v1.push_back(v[i]); } make(v1,v2,0,cnt); cout<<cnt; } }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; vector<pair<ll,ll>> cond[105]; void solve() { ll n,m; cin>>n>>m; for(ll i=1;i<=m;i++) { ll x,y,z; cin>>x>>y>>z; cond[x].push_back({y,z}); } vector<ll> dp((1LL<<n),0); dp[0]=1; for(ll i=1;i<(1LL<<n);i++) { ll c=0; for(ll j=0;j<n;j++) { if(i&(1LL<<j)) c++; } bool ok=true; ll fail=-1; for(ll b=c;b<=n-1;b++) { for(ll j=0;j<(ll)cond[b].size();j++) { ll cnt=0; ll y=cond[b][j].first,z=cond[b][j].second; for(ll k=0;k<n;k++) { if(i&(1LL<<k)) { if(k+1<=y) cnt++; } } //if(i==3) cout<<cnt<<"\n"; if(cnt>z) { fail=b; ok=false; break; } } } if(!ok) { //cout<<fail<<" "<<i<<"\n"; continue; } for(ll j=0;j<n;j++) { if(i&(1LL<<j)) { dp[i]+=(dp[i^(1LL<<j)]); } } } // for(ll i=0;i<(1LL<<n);i++) // { // cout<<dp[i]<<" "; // } // cout<<"\n"; cout<<dp[(1LL<<n)-1]<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll T=1,p=1; //cin>>T; while(T--) { //cout<<"Case #"<<p<<": "; solve(); p++; } /* #ifndef ONLINE_JUDGE cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n"; #endif */ return 0; }
#include <iostream> #include<vector> #include<string> #include<algorithm> #include<iomanip> #include<map> #include<random> #include<complex> #include<math.h> #include<functional> #include<stack> #include<queue> #include<unordered_map> #include<set> #include<numeric> #include <cassert> using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define req(i,n) for(int i = 1;i <= n; i++) #define rrep(i,n) for(ll i = n-1;i >= 0;i--) #define ALL(obj) begin(obj), end(obj) #define RALL(a) rbegin(a),rend(a) typedef long long ll; typedef long double ld; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int inf = 0x3fffffff; const ll INF = 1e18; int n, m; vector<vector<pair<int, int>>> p; bool check(int msk) { int cnt = 0; vector<int> tp(20,0); rep(i, n) { if (msk & (1 << i)) cnt++,tp[i]++; }rep(i, n) tp[i + 1] += tp[i]; for (pair<int, int> i : p[cnt]) { if (tp[i.first] > i.second) return false; }return true; } int main() { cin >> n >> m; p.resize(n + 1); vector<ll> dp(1 << n,0); dp[0] = 1; rep(i, m) { int x, y, z; cin >> x >> y >> z; y--; p[x].push_back({ y,z }); }rep(i, 1 << n) { rep(j, n) if (!(i & (1 << j)) && check(i)) dp[(1<<j)| i] += dp[i]; }cout << dp[(1 << n)-1] << endl; }
#include <algorithm> #include <iostream> using ll = long long; using namespace std; int main() { int a[4]; for (int i = 0; i < 4; i++) cin >> a[i]; sort(a, a + 4); if (a[0] + a[3] == a[1] + a[2] || a[0] + a[1] + a[2] == a[3]) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) using namespace std; using ll = long long; using Graph = vector<vector<int>>; int main() { int a, b, c; cin >> a >> b >> c; int ans = 0; ans = max(ans, a + b); ans = max(ans, a + c); ans = max(ans, b + c); cout << ans << endl; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <functional> #include <cmath> #include <iomanip> #include <stack> #include <queue> #include <numeric> #include <map> #include <unordered_map> #include <set> #include <fstream> #include <chrono> #include <random> #include <bitset> //#include <atcoder/all> #define rep(i,n) for(int i=0;i<(n);i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) ((int)(x).size()) #define pb push_back using ll = long long; using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) {return a/gcd(a,b)*b;} int main(){ ll N,X; cin >> N >> X; vector<ll> V(N); vector<ll> P(N); rep(i,N) cin >> V[i] >> P[i]; ll now = 0; rep(i,N){ now += V[i]*P[i]; if(now>X*100){ cout << i+1 << endl; return 0; } } cout << -1 << endl; return 0; }
// 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 i64 MOD = 1e9 + 7; 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/unionfind.cpp.html struct UnionFind{ vector<int> par; int count; UnionFind(int n) : par(n, -1), count(0){} int Find(int x){return par[x] < 0 ? x : Find(par[x]);} int Size(int x){return par[x] < 0 ? -par[x] : Size(par[x]);} bool Unite(int x, int y){ x = Find(x); y = Find(y); if(x == y) return false; if(par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return ++count; } }; signed main(){ int n, m; cin >> n >> m; vector<vector<int>> edges(n); UnionFind uf(n); for(int i = 0; i < m; ++i){ int x, y; cin >> x >> y; edges[--x].emplace_back(--y); edges[y].emplace_back(x); uf.Unite(x, y); } vector<vector<int>> fl(n); for(int i = 0; i < n; ++i){ assert(uf.Find(i) < n); fl[uf.Find(i)].emplace_back(i); } vector<i64> cn(n, 0); vector<int> col(n); function<void(int,int)> f = [&](int k, int p){ if(p == fl[k].size()){ ++cn[k]; return; } int x = fl[k][p]; for(int j = 0; j < 3; ++j){ bool fp = true; for(auto y : edges[x]){ if(col[y] == j){ fp = false; break; } } if(fp){ col[x] = j; f(k, p + 1); col[x] = -1; } } }; for(int i = 0; i < n; ++i){ col.assign(n, -1); f(i, 0); } i64 ais = accumulate(cn.begin(), cn.end(), 1LL, [](auto x, auto y){return x * y;}); cout << ais << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/STACK:36777216") #include <iostream> #include <vector> #include <set> #include <algorithm> #include <string> #include <map> #include <unordered_map> #include <iomanip> #include <unordered_set> #include <stack> #include <queue> #include <bitset> #include <cstring> #include <random> #include <cassert> #include <iterator> using namespace std; #define inf (ll)1e15 #define mod (ll)(1e9 + 7) #define pi 3.14159265358979 #define e 2.71828 #define eps 1e-6 #define neutral 0; typedef unsigned long long ull; typedef long long ll; int dp[301][301]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); int m = -1; for (int i = 0; i < n; i++) { cin >> a[i]; m = max(m, a[i]); } int ans = 0, num = -1; for (int i = 2; i <= m; i++) { int t = 0; for (int j = 0; j < n; j++) { if (a[j] % i == 0) t++; } if (t > num) { num = t; ans = i; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string s; cin >> s; ll ans = 0; // 1つ前の文字 char c = s.at(0); // 直前の連続した文字 char lc = '0'; // 連続した文字列が何個あったか ll lcnt = 0; // 文字連続カウンター ll cnt = 0; for (int i = 0; i < s.size(); i++) { if (c == s.at(i)) { cnt++; } else { if (cnt > 1) { // 直前の連続文字と異なる場合のみ変更回数が増えるのでカウントアップ if (lc != c) { lcnt++; } lc = c; } cnt = 1; c = s.at(i); } // 直前の連続文字列と異なる場合、変更できる。 if (lc != s.at(i)) { ans += lcnt; } else { ans += lcnt - 1; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=(r)-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[9]={1,0,-1,0,1,1,-1,-1,0}; const ll dx[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; } template <class T> class SegTree { int n;// 葉の数 vector<T> data;// データを格納するvector T def; // 初期値かつ単位元 function<T(T, T)> operation; // 区間クエリで使う処理 function<T(T, T)> update;// 点更新で使う処理 T find(int a, int b) { T val_left = def, val_right = def; for (a += (n - 1), b += (n - 1); a < b; a >>= 1, b >>= 1) { if ((a & 1) == 0){ val_left = operation(val_left, data[a]); } if ((b & 1) == 0){ val_right = operation(data[--b],val_right); } } return operation(val_left, val_right); } public: // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // _update:更新関数 SegTree(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update=[](T a,T b){return b;}) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } data = vector<T>(2 * n - 1, def); } void set(int i, T x) { data[i + n - 1] = x; } void build() { for (int k=n-2;k>=0;k--) data[k] = operation(data[2*k+1],data[2*k+2]); } // 場所i(0-indexed)の値をxで更新 void change(int i, T x) { i += n - 1; data[i] = update(data[i], x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } // [a, b)の区間クエリを実行 T query(int a, int b) { //return _query(a, b, 0, 0, n); return find(a,b); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; int main(){ ll h,w,n;cin >> h >> w >> n; vvl g(h);rep(i,n){ ll p;cin >> p;p--; ll q;cin >> q;q--; g[p].pb(q); } /*rep(i,h){ for(auto p:x[i])cout << p <<" ";cout << endl; }*/ SegTree<ll> st(w,0LL,[](ll a,ll b){return a+b;}); rep(i,w)st.set(i,1);st.build(); bool flag=true; ll ans=0; rep(i,h){ ll left=INF; for(auto p:g[i]){ if(p==0)flag=false; st.change(p,0); chmin(left,p); } if(i==0&&left!=INF){ for(int j=left;j<w;j++)st.change(j,0); } if(!flag)ans+=st.query(0,w); else{ if(left==INF)ans+=w; else ans+=left+st.query(left,w); } //cout << ans <<endl; } cout << ans <<endl; }
#include <bits/stdc++.h> using namespace std; #define LL long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; LL k; cin >> n >> k; LL dp[4][3000005] = {0}; dp[0][0] = 1; #if 0 // A slower way of calculating dp[i][j] is: // Will give TLE: https://atcoder.jp/contests/abc200/submissions/22497898 for (int i = 0; i < 3; i ++) { // O(N^2) for (int j = 0; j <= 3 * n; j ++) { for (int k = 1; k <= n; k ++) { dp[i + 1][j + k] += dp[i][j]; } } } // first of all, we know dp[i][j] > 0 if j <= i * n; // next, dp[i][j] will be added to dp[i+1][j+1:j+n+1) // so we can basically use dp[i+1][j] as a prefix to dp[i+1][j+1] #else for (int i = 0; i < 3; i ++) { // O(N) for (int j = 0; j <= i * n; j ++) { dp[i+1][j+1] += dp[i][j]; dp[i+1][j+n+1] -= dp[i][j]; // remove from accumulation. } // accumulate / prefix sum for (int j = 1; j <= (i + 1) * n; j ++) dp[i+1][j] += dp[i+1][j-1]; } #endif // we accumulate the counts of sums until we reach k. // sum_{i=3}^{x} dp[3][i] <= k. we want to find x. int x; for (int i = 3; i <= 3 * n; i ++) { if (k <= dp[3][i]) { x = i; break; } else { k -= dp[3][i]; } } // finally, we can iterate the beauty of the cake for (int i = 1; i <= n; i ++) { // i is the beauty. // the maximum of the taste we can get is // x (the sum) - i (beauty) - n (pop). // the minimum is // x - i - 1. // we should constrain them between [1, n]. int jmi = max(1, x - i - n), jma = min(n, x - i - 1); if (jmi > jma) continue; // in this case i is too small. if (k > (jma - jmi + 1)) { k -= (jma - jmi + 1); // these are the choices available for taste. continue; } int y = jmi + k - 1; int z = x - i - y; cout << i << ' ' << y << ' ' << z << '\n'; return 0; } }
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N; cin >> N; vector<string> ans; // auto func = [&]() { // int as = -1, an = -1; // REP(i, 1 << N) FOR(j, i + 1, 1 << N) { // int s = 0, n = 0; // for (auto & str : ans) { // s += str[i] == str[j]; // n += str[i] != str[j]; // } // if (as == -1) { // as = s; // an = n; // } // else { // if (as != s || an != n) return false; // //cout << i << ", " << j << ": as = " << s << ", s = " << s << ": an = " << an << ", n = " << n << endl; // // assert(as == s); // // assert(an == n); // } // } // return true; // }; // vector<string> op; // REP(i, 1 << ((1 << N) - 1)) { // int pcnt = 0; // REP(j, (1 << N) - 1) pcnt += i >> j & 1; // if (pcnt != (1 << (N - 1)) - 1) continue; // string s(1 << N, 'B'); // s[0] = 'A'; // REP(j, (1 << N) - 1) if (i >> j & 1) s[j + 1] = 'A'; // op.emplace_back(s); // } // vector<int> cnt(1 << N); // auto dfs = [&](auto self, int d, int p) { // if (d == (1 << N) - 1) { // if (func()) { // puts("answer! : "); // sort(ALL(ans)); // for (auto s : ans) cout << s << endl; // return; // } // return; // } // FOR(i, p, op.size()) { // bool ok = true; // FOR(j, 1, op[i].size()) { // if (op[i][j] == 'A') ++cnt[j]; // ok &= cnt[j] <= (1 << (N - 1)) - 1; // } // if (ok) { // ans.emplace_back(op[i]); // self(self, d + 1, i + 1); // ans.pop_back(); // } // FOR(j, 1, op[i].size()) if (op[i][j] == 'A') --cnt[j]; // } // }; // dfs(dfs, 0, 0); REP(i, N) { vector<string> nex; nex.emplace_back(string(1 << i, 'A') + string(1 << i, 'B')); for (auto & s : ans) { nex.emplace_back(s + s); string rev = s; for (char & c : rev) c = c == 'A' ? 'B' : 'A'; nex.emplace_back(s + rev); } swap(ans, nex); } cout << ans.size() << endl; for (auto & s : ans) cout << s << '\n'; }
#pragma GCC optimize("O3") #include "bits/stdc++.h" #define sz(x) (int)(x).size() using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; cout << (1<<n)-1 << "\n"; for(int i=1; i<=(1<<n)-1; i++) { for(int j=0; j<(1<<n); j++) cout << (__builtin_popcount(i & (j+1)) % 2 ? 'A' : 'B'); cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { //int arr[] = {1, 2, 90, 10, 110}; //int n = sizeof(arr) / sizeof(arr[0]); // Function calling //cout << "Maximum difference is " << maxDiff(arr, n); int a,b,c,d; cin>> a; cin>>b; cin>> c; cin>>d; cout<< b-c; return 0; }
#include <bits/stdc++.h> //#include<boost/multiprecision/cpp_int.hpp> //#include<boost/multiprecision/cpp_dec_float.hpp> //#include <atcoder/all> #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) #define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i) #define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i) #define repl(i, a) for (ll i = (ll)0; i < (ll)a; ++i) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define popcount __builtin_popcount #define fi first #define se second using ll = long long; constexpr ll mod = 1e9 + 7; constexpr ll mod_998244353 = 998244353; constexpr ll INF = 1LL << 60; // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") //using lll=boost::multiprecision::cpp_int; //using Double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1024>>;//仮数部が1024桁 template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> T mypow(T x, T n, const T &p = -1) { //x^nをmodで割った余り if (p != -1) { x %= p; } T ret = 1; while (n > 0) { if (n & 1) { if (p != -1) ret = (ret * x) % p; else ret *= x; } if (p != -1) x = (x * x) % p; else x *= x; n >>= 1; } return ret; } using namespace std; //using namespace atcoder; void solve() { int a,b,c,d; cin>>a>>b>>c>>d; cout<<b-c; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); solve(); return 0; }
#include<algorithm> #include<iostream> #include<cstring> #include<bitset> #include<cstdio> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> using namespace std; #define neinf 0xc0c0c0c0c0c0c0c0ll #define inf 0x3f3f3f3f3f3f3f3fll #define uint unsigned int #define ull unsigned ll #define ll long long #define reg register #define db double #define il inline #define gc getchar #define pc putchar #define HgS 998244353 il ll rd() { reg ll res=0,lab=1; reg char ch=gc(); while((ch<'0'||ch>'9')&&ch!=EOF) {if(ch=='-')lab=-lab;ch=gc();} while(ch>='0'&&ch<='9') res=(res<<3)+(res<<1)+(ch&15),ch=gc(); return res*lab; } il void prt(ll x,char t=' ') { reg char ch[70];reg int tp=0; if(x<0)pc('-'),x=-x; while(x)ch[++tp]=(x%10)^48,x/=10; if(!tp)ch[++tp]=48; while(tp)pc(ch[tp--]); if(t)pc(t); } ll n,f[200005],v[200005],ans; il void dfs(int cur) { if(v[cur]==2)return; if(v[cur]==1){++ans;return;} ++v[cur];dfs(f[cur]);++v[cur]; } il ll qpow(ll n,ll e,ll p=HgS) { reg ll res=1; while(e){if(e&1)res=res*n%p;n=n*n%p;e>>=1;} return res; } int main() { n=rd();for(int i=1;i<=n;++i)f[i]=rd(); for(int i=1;i<=n;++i)if(!v[i])dfs(i); prt(qpow(2,ans)-1); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<vl> vvl; double EPS=1e-9; int INF=1000000005; long long INFF=1000000000000000005ll; double PI=acos(-1); int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 }; int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 }; const ll MOD = 1000000007; ll sum() { return 0; } template<typename T, typename... Args> T sum(T a, Args... args) { return a + sum(args...); } #define DEBUG fprintf(stderr, "====TESTING====\n") #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #define OUT(x) cout << x << endl #define OUTH(x) cout << x << " " #define debug(...) fprintf(stderr, __VA_ARGS__) #define READ(x) for(auto &(z):x) cin >> z; #define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a)) #define EACH(a, b) for (auto&(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define MAX(a, b) a=max(a, b) #define MIN(a, b) a=min(a, b) #define SQR(x) ((ll)(x) * (x)) #define RESET(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define TC(t) while (t--) #define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL) #define what_is(x) cerr << #x << " is " << x << endl; void solve() { } int main() { FAST_INP; // #ifndef ONLINE_JUDGE // freopen("input.txt","r", stdin); // freopen("output.txt","w", stdout); // #endif // int tc; cin >> tc; // TC(tc) solve(); char s, n; cin >> s >> n; if(s=='Y') OUT((char)toupper(n)); else OUT(n); return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int N,X; cin>>N>>X; vector<int> V; for(int i=0;i<N;i++) { int x; cin>>x; if(x != X) { V.push_back(x); } } for(int i=0;i<V.size();i++) { if(i == (V.size()-1)) { cout<<V[i]<<endl; } else { cout<<V[i]<<" "; } } return 0; }
#include <bits/stdc++.h> using namespace std; char S1[400005]; char S2[400005]; char S3[400005]; char S4[400005]; int main(){ int T; scanf("%d", &T); while(T --){ int N; scanf(" %d %s %s %s", &N, S1, S2, S3); memset(S4, 0, sizeof(S4)); for(int i = 0; i < N; i ++){ S4[i] = '1'; } for(int i = N; i < 2*N; i ++){ S4[i] = '0'; } S4[2*N] = '1'; printf("%s\n", S4); } }
#include <bits/stdc++.h> #define endl '\n'; typedef long long ll; using namespace std; const int maxn = 1e5 + 10; int main() { ios::sync_with_stdio(false); int n,x; char aws; cin >> n >> x; for (int i = 0; i < n; i++) { cin >> aws; if (aws == 'o')x++; else if (x != 0) x--; } cout << x << endl; return 0; }
// B - Quizzes #include <bits/stdc++.h> using namespace std; int main(){ int n, x; string s; cin>>n>>x>>s; for(auto c:s) x = c=='o'? x+1:max(0, x-1); cout<< x <<endl; }
#include <bits/stdc++.h> // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18; void print0() {} template<typename H,typename... T> void print0(H h,T... t){cout<<fixed<<setprecision(15)<<h;print0(t...);} void print() { print0("\n"); } template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void ioinit() { cout << fixed << setprecision(15); ios_base::sync_with_stdio(0); cin.tie(0); } // clang-format on const ll MODULO = 3; const ll COMBMAX = 400005; ll fac[COMBMAX]; ll fthree[COMBMAX]; void combination_init() { fac[0] = fac[1] = 1; fthree[0] = fthree[1] = 0; for (int i = 2; i < COMBMAX; i++) { fthree[i] = fthree[i - 1]; ll j = i; while (j % 3 == 0) { j = j / 3; fthree[i]++; } fac[i] = fac[i - 1] * j % MODULO; } } ll inv(ll v) { if (v == 1) return 1; if (v == 2) return 2; return 0; } ll combination(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; if (fthree[n] - fthree[k] - fthree[n - k] > 0) { return 0; } return fac[n] * inv(fac[n - k]) * inv(fac[k]) % MODULO; } int main() { ioinit(); ll n; cin >> n; string s; cin >> s; vector<ll> t(n); for (ll i = 0; i < n; i++) { if (s[i] == 'B') { t[i] = 0; } if (s[i] == 'W') { t[i] = 1; } if (s[i] == 'R') { t[i] = 2; } } combination_init(); ll sign = (n % 2 == 0) ? -1 : 1; ll ans = 0; for (ll i = 0; i < n; i++) { ans += t[i] * combination(n - 1, i); } ans = ans * sign; ans = ans % 3; if (ans < 0) ans += 3; if (ans == 0) { print("B"); } if (ans == 1) { print("W"); } if (ans == 2) { print("R"); } }
#include <bits/stdc++.h> using namespace std; long long MOD; int main(){ int N, K; cin >> N >> K >> MOD; vector<vector<long long>> f(N); f[0].push_back(1); for (int i = 1; i < N; i++){ int sz = f[i - 1].size(); f[i] = vector<long long>(sz + i * K, 0); for (int j = 0; j < sz; j++){ f[i][j] = f[i - 1][j]; } for (int j = i; j < sz + i * K; j++){ f[i][j] += f[i][j - i]; if (f[i][j] >= MOD){ f[i][j] -= MOD; } } for (int j = sz + i * K - 1; j >= i * (K + 1); j--){ f[i][j] -= f[i][j - i * (K + 1)]; if (f[i][j] < 0){ f[i][j] += MOD; } } } for (int i = 0; i < N; i++){ long long ans = 0; int d = min(f[i].size(), f[N - 1 - i].size()); for (int j = 0; j < d; j++){ ans += f[i][j] * f[N - 1 - i][j] % MOD; ans %= MOD; } ans *= (K + 1); ans %= MOD; ans--; if (ans == -1){ ans += MOD; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define each(e, v) for(auto &e: v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; bool judge(string S){ int N = sz(S); rep(i, N){ if(S[i] != S[N-1-i]) return false; } return true; } int main(){ string S; cin >> S; reverse(all(S)); bool ans = false; rep2(i, 0, 100){ if(judge(S)) ans = true; S += '0'; } cout << (ans? "Yes\n" : "No\n"); }
#include <bits/stdc++.h> #define speedup ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr),cout.precision(12) using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map; #define sbc __builtin_popcount #define pb push_back #define pbk pop_back #define em emplace #define emb emplace_back #define ff first #define ss second #define ll long long const ll mod = 1e9 + 7; template<typename T> void smax(T& a, T b) { if(a<b) a=b; } template<typename T> void smin(T& a, T b) { if(a>b) a=b; } template<typename T> T pw(T a,T b) { T p=1,one=1; while(b) { if(b&one) p=p*a; a=a*a; b >>=1; } return p; } template<typename T> T pwm(T a,T b,T md=mod) { T p=1,one=1; while(b) { if(b&one) p=p*a%md; a=a*a%md; b >>=1; } return p; } template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &it:v) is >> it; return is; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &it) { os << it.first << ' ' << it.second; return os; } template <typename T> ostream &operator<<(ostream &os, const array<T, 2> &v) { for (auto &it : v) os << it << ' '; return os; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &it : v) os << it << ' '; return os; } #ifndef ONLINE_JUDGE class Timer { chrono::high_resolution_clock::time_point start_t, end_t; public: Timer() { start_t=chrono::high_resolution_clock::now(); } ~Timer() { end_t = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(end_t-start_t); cerr<<"\nRunTime: "<<duration.count()<<"ms"<<'\n'; } }; void debug() { cerr << '\n'; } template<typename T, typename... Args> void debug(T print, Args... args) { cerr << ' ' << print; debug(args...); } #define deb(...) cerr << "[" << #__VA_ARGS__ << "]" << " --> ", debug(__VA_ARGS__) #else #define deb(...) void(0) #endif void solve() { int n; cin>>n; for(int i=0;i<n;++i) cout<<2*i%n+1<<' '<<(2*i+1)%n+1<<'\n'; } int main() { speedup; //int t; cin >> t; while (t--) solve(); } /**Vim * * * * * * * * */
#include <iostream> #include <string> #include <vector> #include <algorithm> #define rep(i,n) for(int i=0;i<n;i++) #define ll long long #define P pair<int,ll> #define MOD 1000000007 using namespace std; ll n, a,b,c, m; int main(){ cin >> n; m = n+1; rep(i, 60){ a = (n >> i); b = i; c = n - (a << b); m = min(m, a+b+c); } cout << m<<endl; }
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <bitset> #include <climits> #include <cmath> #include <bitset> #include <complex> #include <functional> #include <cassert> #include <stack> #include <numeric> typedef int64_t ll; typedef std::pair<int, int> Pii; typedef std::pair<ll, ll> Pll; typedef std::pair<double, double> Pdd; #define rip(i, n, _s) for (int i = (_s);i < (int)( n ); i++) #define all(_l) _l.begin(), _l.end() #define rall(_l) _l.rbegin(), _l.rend() #define MM << " " << template<typename T> using MaxHeap = std::priority_queue<T>; template<typename T> using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<typename T> inline bool chmax(T &_l, const T b) { if (_l < b) { _l = b; return true; } return false; } template<typename T> inline bool chmin(T &_l, const T b) { if (_l > b) { _l = b; return true; } return false; } # ifdef LOCAL_DEBUG template<typename T> void vdeb(const std::vector<T> &bb) { for (unsigned int i = 0;i < bb.size();i++) { if (i == bb.size() - 1) std::cout << bb[i]; else std::cout << bb[i] << ' '; } std::cout << '\n'; } template<typename T> void vdeb(const std::vector<std::vector<T>> &bb) { for (unsigned int i = 0;i < bb.size();i++) { std::cout << i << ' '; vdeb(bb[i]); } std::cout << '\n'; } # endif using namespace std; int main() { int n; cin >> n; cout << (1<<n)-1 << endl; rip(i,1<<n,1) { rip(j,1<<n,0) { if(__builtin_popcount(j&i)&1) cout << 'A'; else cout << 'B'; } cout << '\n'; } }
#include <iostream> #include <fstream> #include <algorithm> #include <vector> #include <set> #include <stack> #include <numeric> #include <chrono> #include <random> #include <bitset> #include <tuple> #include <queue> #include <map> #include <unordered_map> #include <cstring> #include <cassert> #include <climits> #include <math.h> using namespace std; const int N = 2e5 + 10, mod = 1e9 + 7; int n, q, a[N], t[N]; long long pref[N]; map<long long, int> mxid, mnid; set<long long> mns, mxs; struct tr { int tree[N << 1]; void build() { for(int i = 0; i < n; i++) tree[i + n] = 1e9; for(int i = n - 1; i > 0; i--) tree[i] = min(tree[i << 1], tree[i << 1 | 1]); } void upd(int i, int va) { tree[i += n] = va; for(i >>= 1; i > 0; i >>= 1) tree[i] = min(tree[i << 1], tree[i << 1 | 1]); } int query(int l, int r) { int resl = 1e9, resr = 1e9; for(l += n, r += n + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) resl = min(resl, tree[l++]); if(r & 1) resr = min(resr, tree[--r]); } return min(resl, resr); } } mntr, mxtr; int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; //scanf("%d", &tc); for(int cn = 1; cn <= tc; cn++) { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d%d", &a[i], &t[i]); for(int i = 0; i < n; i++) { if(i) pref[i] = pref[i - 1]; if(t[i] == 1) pref[i] += a[i]; else if(t[i] == 2) mxid[a[i] - pref[i]]; else mnid[a[i] - pref[i]]; } mntr.build(); mxtr.build(); int c = 0; for(auto &x : mxid) { x.second = c++; mxs.insert(x.first); } c = 0; for(auto &x : mnid) { x.second = c++; mns.insert(x.first); } vector<long long> ans(n); for(int i = n - 1; i >= 0; i--) { if(t[i] == 1) continue; auto it = mxs.lower_bound(a[i] - pref[i]); int j = 1e9; if(it != mxs.end()) { j = mxtr.query(mxid[*it], n - 1); } it = mns.upper_bound(a[i] - pref[i]); int k = 1e9; if(it != mns.begin()) { it--; k = mntr.query(0, mnid[*it]); } if(min(j, k) < n) ans[i] = ans[min(j, k)]; else ans[i] = 1ll * a[i] + pref[n - 1] - pref[i]; if(t[i] == 2) mxtr.upd(mxid[a[i] - pref[i]], i); else mntr.upd(mnid[a[i] - pref[i]], i); } scanf("%d", &q); while(q--) { long long x; scanf("%lld", &x); auto it = mxs.lower_bound(x); int j = 1e9; if(it != mxs.end()) { j = mxtr.query(mxid[*it], n - 1); } it = mns.upper_bound(x); int k = 1e9; if(it != mns.begin()) { it--; k = mntr.query(0, mnid[*it]); } if(min(j, k) < n) printf("%lld\n", ans[min(j, k)]); else printf("%lld\n", x + pref[n - 1]); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 17; int n,m,k; vector<vector<int>> G; int d[N][N], c[N]; int dp[1 << N][N]; void bfs(int s) { vector<int> dist(n, INT_MAX); queue<int> que; que.push(c[s]); dist[c[s]] = 0; while(!que.empty()) { int v = que.front(); que.pop(); for (auto next : G[v]) { if (dist[next] != INT_MAX) continue; dist[next] = dist[v] + 1; que.push(next); } } for (int i = 0; i < k; i++) { d[s][i] = min(d[s][i], dist[c[i]]); d[i][s] = min(d[i][s], dist[c[i]]); } } signed main() { cin >> n >> m; G.resize(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] = INT_MAX; } } cin >> k; for (int i = 0; i < k; i++) { cin >> c[i], c[i]--; } for (int i = 0; i < k; i++) { bfs(i); } for (int i = 0; i < (1 << N); i++) { for (int j = 0; j < N; j++) { dp[i][j] = INT_MAX; } } for (int i = 0; i < k; i++) { dp[(1 << k) - 1][i] = 0; } // d[N][N]はidxで管理 (idx < k) for (int s = (1 << k) - 2; s >= 0; s--) { for (int v = 0; v < k; v++) { for (int u = 0; u < k; u++) { if (!(s >> u & 1)) { dp[s][v] = min(dp[s][v], dp[s | 1 << u][u] + d[v][u]); } } } } int res = INT_MAX; for (int i = 0; i < k; i++) { res = min(res, dp[0][i]); } if (res == INT_MAX) res = -1; else res += 1; cout << res << '\n'; }
#include <bits/stdc++.h> using namespace std; // TYPEDEF ------------------------------ typedef long long ll; typedef long double ld; typedef pair<ll, ll> Pair; typedef vector<ll> vll; typedef vector<ld> vld; typedef vector<vector<ll>> Graph; typedef vector<string> vs; typedef vector<pair<ll, ll>> Pll; typedef queue<ll> qll; typedef map<ll, ll> mll; // REPEAT ------------------------------ #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--) // UTIL ------------------------------ #define pb push_back #define paired make_pair #define ALL(a) (a).begin(),(a).end() // 注意;https://kimiyuki.net/blog/2015/09/25/competitive-programming-coding/ #define SORT(a) sort((a).begin(),(a).end()) #define RSORT(a) sort((a).rbegin(), (a).rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define SUM(x) accumulate(ALL(x), (ll)0) #define COUNT(x, y) count(ALL(x), y); 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; } // PRINT ------------------------------ #define COUT(a) cout << (a) << endl; #define YES cout << "YES" << endl; #define NO cout << "NO" << endl; #define Yes cout << "Yes" << endl; #define No cout << "No" << endl; // DEBUG ------------------------------ #define LINE cerr << "[debug] line: " << __LINE__ << "\n"; #define debug(...) {debug_all(#__VA_ARGS__, __VA_ARGS__);} template < typename T > void _debug_print(T const& a) {cerr << a << ", ";} template < typename... Args > void debug_all(string s, Args... args) { cerr << "[debug] " << s << " = "; using swallow = std::initializer_list<int>; (void)swallow{ (void( _debug_print(args) ), 0)... }; cerr << endl; } #define debugV(v) cerr << "[debugV] " << #v << ":"; REP(z, v.size()) cerr << " " << v[z]; cerr << "\n"; // CONST ------------------------------ constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; // or M_PI //###################################################################### //# ここから下に書いていく # //###################################################################### void Main() { ll n, k; cin >> n >> k; ll ans = 0; REP(i, n) { REP(j, k) { ans += (i+1) * 100; ans += (j+1); } } COUT(ans); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } /* 3 2 */
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) ll gcd(ll x, ll y) {return (x%y) ? gcd(y, x%y) : y; } const int inf = 1000000000; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; const ll mod = 1e9+7; int main(){ string a, b; cin >> a >> b; int suma = 0; int sumb = 0; rep(i,3){ suma += (a.at(i)-'0'); } rep(i,3){ sumb += (b.at(i)-'0'); } cout << max(suma, sumb) << endl; }
#include <bits/stdc++.h> using namespace std; #define FASTIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define FASTIO1 srand(chrono::high_resolution_clock::now().time_since_epoch().count()) #define gc getchar_unlocked #define ll long long #define PI 3.1415926535897932384626 #define br cout << endl #define sp cout << " " #define foin(i, s, e) for (i = s; i < e; i++) #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define printarr(i, s, e, a, str) fin(i, s, e) cout << a[i] << str; #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define clr(x) memset(x, 0, sizeof(x)) #define tr(it, x) for (auto it = x.begin(); it != x.end(); it++) #define trr(it, x) for (auto it = x.rbegin(); it != x.rend(); it +) #define sortv(vec) sort(vec.begin(), vec.end()); typedef pair<int, int> pii; typedef map<int, int> mapii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; const int mod = 1'000'000'007; const int N = 3e5; int i = 0; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } /* ========== Solution ========= */ int main() { FASTIO; FASTIO1; int n = 1; cin >> n; int a[n], b[n]; foin(i, 0, n) cin >> a[i]; foin(i, 0, n) cin >> b[i]; int s = a[0], e = b[0]; foin(i, 1, n) { if (a[i] > s) s = a[i]; if (b[i] < e) e = b[i]; } if (s > e) cout << "0"; else cout << (e - s + 1); return 0; }
#include <bits/stdc++.h> #define arr array #define pb push_back #define fs first #define sc second #define eb emplace_back #define vt vector #define all(x) (x).begin(), (x).end() #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) using namespace std; typedef long long ll; const int maxn=1e5; const ll MAXLL=9223372036854775807; void solve(){ int n; cin >> n; vt<int> a(n); rep(i, 0, n) cin >> a[i], a[i]%=200; int li=min(8, n); map<int, vt<vt<int>>> mp; for (int mask=1; mask<(1<<li); mask++){ vt<int> v; int s=0; rep(i, 0, li) if (mask>>i&1) s+=a[i], v.pb(i+1); if (!mp[s%200].empty()){ vt<int> v1=mp[s%200][0]; cout << "Yes" << endl; cout << (int)v.size() << " "; for (auto i:v) cout << i << " "; cout << endl; cout << (int)v1.size() << " "; for (auto i:v1) cout << i << " "; cout << endl; return; } else mp[s%200].pb(v); } cout << "No"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <algorithm> #include <iostream> #include <vector> #include <set> typedef long long ll; using namespace std; int n; vector<vector<int>> a; set<int> ids; #define rep(i,n) for(int i=0;i<(int)(n);++i) int main() { cin >> n; rep(i, n) { int x, y; cin >> x >> y; a.emplace_back(vector<int>{x, y, i}); } rep(k, 2) { sort(a.begin(), a.end()); rep(i, 2) { ids.emplace(a[i][2]); ids.emplace(a[n - 1 - i][2]); } for (auto& v : a)swap(v[0], v[1]); } vector<pair<int, int>> b; for (auto v : a) { if (ids.count(v[2]) > 0)b.emplace_back(v[0], v[1]); } vector<ll> d; for (int i = 0; i < (int)b.size(); i++) { for (int j = i + 1; j < (int)b.size(); j++) { d.emplace_back(max(abs(b[i].first - b[j].first), abs(b[i].second - b[j].second))); } } sort(d.begin(), d.end()); cout << d[d.size() - 2] << endl; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <iostream> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) int main() { cin.tie(0); ios::sync_with_stdio(false); int H,W; int ans = 0; string S[100]; cin >> H >> W; REP(i, H) cin >> S[i]; REP(h, H){ REP(w, W){ if (S[h][w]=='.'){ if ( w+1<W && S[h][w+1]=='.' ) ans++; if (h+1<H && S[h+1][w]=='.') ans++; } } } cout << ans << endl; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <math.h> #include <queue> #include <numeric> #include <iomanip> 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 Unique(a) (a).erase(unique((a).begin(), (a).end()),(a).end()) #define cfs(a) cout<<fixed<<setprecision(a) int main() { double A, B; cin >> A >> B; co(100 * (1 - B / A)); }
#include <bits/stdc++.h> using namespace std; int main(){ int A, B; cin >> A >> B; cout << 2 * A + 100 - B; }
// https://atcoder.jp/contests/arc108/tasks/arc108_b #include <bits/stdc++.h> using namespace std; int main(){ int N; char a[200010]; memset(a,0,200010); cin>>N>>a; for(int i=0;i<N;i++){ if(a[i]=='f'&&a[i+1]=='o'&&a[i+2]=='x'){ for(int j=i;j<N;j++) a[j]=a[j+3]; i=i-3; N=N-3; } } cout<<N; return 0; }
#include<bits/stdc++.h> //#include<ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/trie_policy.hpp> //using namespace __gnu_pbds; using namespace std; #define ll long long int #define ld long double #define mod 1000000007 #define inf 1e18 #define endl "\n" #define pb push_back #define vi vector<int> #define vl vector<ll> #define vs vector<string> #define pii pair<ll,ll> #define ump unordered_map #define mp make_pair #define pq_max priority_queue<ll> #define pq_min priority_queue<ll,vi,greater<ll> > #define all(n) n.begin(),n.end() #define ff first #define ss second #define mid(l,r) (l+(r-l)/2) #define bitc(n) __builtin_popcount(n) #define loop(i,a,b) for(int i=(a);i<=(b);i++) #define looprev(i,a,b) for(int i=(a);i>=(b);i--) #define iter(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define log(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl; #define M 1000000007 #define w(x) int x; cin>>x; while(x--) template <typename T> T gcd(T a, T b) {if (a % b) return gcd(b, a % b); return b;} template <typename T> T lcm(T a, T b) {return (a * (b / gcd(a, b)));} vs tokenizer(string str, char ch) {std::istringstream var((str)); vs v; string t; while (getline((var), t, (ch))) {v.pb(t);} return v;} void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } //typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds; //typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pbtrie; void file_i_o() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main(int argc, char const *argv[]) { clock_t begin = clock(); file_i_o(); // Write your code here.... int n; cin>>n; string s; cin>>s; vector<char>v; for(int i=0;i<n;i++){ v.pb(s[i]); int sz=v.size(); if(sz>=3 && v[sz-3]=='f' && v[sz-2]=='o' && v[sz-1]=='x'){ for(int i=0;i<3;i++) v.pop_back(); } } cout<<v.size()<<endl; #ifndef ONLINE_JUDGE clock_t end = clock(); cout << "\n\nExecuted In: " << double(end - begin) / CLOCKS_PER_SEC * 1000 << " ms"; #endif return 0; }
#include <bits/stdc++.h> #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using namespace std; using Graph = vector<vector<int>>; using vst= vector<string>; using vin= vector<int>; using ll=long long; using ull=unsigned long long; using vll= vector<ll>; using P=pair<int ,int>; using pqi=priority_queue<int, vector<int>, greater<int>>; using pqp=priority_queue<P, vector<P>, greater<P>>; //const long double pi=acos(-1); #define ft first #define sd second #define fn front #define pb push_back #define eb emplace_back #define it insert #define vvi vector<vector<int>> #define si(v) int((v).size()) #define pq priority_queue #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rell(i,n) for (ll i=0; i< (ll)(n); i++) #define sot(x) sort(x.begin(), x.end()) #define rese(x) reverse(x.begin(), x.end()) #define gsot(x) sort(x.begin(), x.end(), greater<ll>()); #define vnn(x,y,s,name) vector<vector<int>> name(x, vector<int>(y,s)) #define mse(x) memset(x, 0, sizeof(x)) #define all(x) (x).begin(),(x).end() #define mii(x,y,z) min(x,min(y,z)) #define maa(x,y,z) max(x,max(y,z)) #define cps CLOCKS_PER_SEC #define yes cout<<"Yes"<<"\n" #define no cout<<"No"<<"\n" #define cset(x) cout<<fixed<<setprecision(x) //const int INF=1001001001; //const ll INF=1e18; //998244353 1000000007 void output(int a,int b,int c,int d){ cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin>>n; vector<pair<int,P>> a(n); rep(i,n){ int x,y,r; cin>>x>>y>>r; /*int s=ceil(sqrt((double)(r))); int t=s/2;*/ output(x,y,x+1,y+1); a[i].ft=r; a[i].sd.ft=x; a[i].sd.sd=y; } sot(a); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<pll> vpll; typedef vector<vpll> vvpll; typedef vector<bool> vbl; typedef vector<vector<bool>> vvbl; typedef vector<ld> vld; void INX(){} template<typename Head, typename... Tail> void INX(Head&& head, Tail&&... tail) { cin >> head; INX(forward<Tail>(tail)...); } void OUTX(){} template<typename Head, typename... Tail> void OUTX(Head&& head, Tail&&... tail) { cout << head << endl; OUTX(forward<Tail>(tail)...); } void OUTX2(){cout << endl;} template<typename Head, typename... Tail> void OUTX2(Head&& head, Tail&&... tail) { cout << head << ' '; OUTX2(forward<Tail>(tail)...); } #define ADD emplace_back #define MP make_pair #define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1) //#define MOD 1000000007 // 10^9 + 7 //setprecision(10) struct dat { ll index; ld x; ld y; ld r; ld sqrtr2; }; typedef vector<dat> vdat; struct square { ld a; ld b; ld c; ld d; }; typedef vector<square> vsquare; int main() { ll n; INX(n); vdat data(n); vdat dataxsort(n); vdat dataysort(n); vsquare result(n); for (ll i = 0; i < n; i++) { dat &di = data[i]; INX(di.x, di.y, di.r); di.index = i; di.sqrtr2 = (ld)sqrt(di.r) / 2.0; dataxsort[i] = di; dataysort[i] = di; square &sqi = result[i]; sqi.a = di.x; sqi.b = di.y; sqi.c = di.x + 1.0; sqi.d = di.y + 1.0; } sort(dataxsort.begin(), dataxsort.end(), [](dat d1, dat d2){return d1.x < d2.x;}); sort(dataysort.begin(), dataysort.end(), [](dat d1, dat d2){return d1.y < d2.y;}); for (ll i = 0; i < n; i++) { dat &dx = dataxsort[i]; square &sqi1 = result[dx.index]; ll j = i - 1; if(i == 0 || dataxsort[i].x == dataxsort[0].x) { sqi1.a = max((ld)0, sqi1.a - dx.sqrtr2); } else if(dataxsort[i].x == dataxsort[j].x) { while(dataxsort[i].x == dataxsort[--j].x); square &sqj = result[dataxsort[j].index]; sqi1.a = max(sqj.c, sqi1.a - dx.sqrtr2); } else { square &sqj = result[dataxsort[j].index]; sqi1.a = max(sqj.c, sqi1.a - dx.sqrtr2); } sqi1.a = ceil(sqi1.a); dat &dy = dataysort[i]; square &sqi2 = result[dy.index]; j = i - 1; if(i == 0 || dataysort[i].y == dataysort[0].y) { sqi2.b = max((ld)0, sqi2.b - dy.sqrtr2); } else if(dataysort[i].y == dataysort[j].y) { while(dataysort[i].y == dataysort[--j].y); square &sqj = result[dataysort[j].index]; sqi2.b = max(sqj.d, sqi2.b - dy.sqrtr2); } else { square &sqj = result[dataysort[j].index]; sqi2.b = max(sqj.d, sqi2.b - dy.sqrtr2); } sqi2.b = ceil(sqi2.b); } for (ll i = 0; i < n; i++) { square &sqi = result[i]; OUTX2(sqi.a, sqi.b, sqi.c, sqi.d); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() void chmax(ll &x, ll y) {x = max(x,y);} void chmin(ll &x, ll y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} template<class in_vec_cout> void vec_cout(vector<in_vec_cout> vec) { for (in_vec_cout res : vec) {cout << res << " ";} cout << endl; } const ll inf = 1e18; const ll mod = 1e9 + 7; /* x*K = N - S (mod N) */ ll gcd(ll x, ll y) { if (y==0) return x; return gcd(y,x%y); } // 拡張ユークリッドの互除法 O(log(lcm(a,b))) // a*x + b*y = gcd(a,b) となる (x,y) を返す // b*s + (a%b)*t = gcd(a,b) // a = (a/b)*b + a%b, d = gcd(a,b) = gcd(b,a%b) // ((a/b)*b + a%b)*x + b*y = d // b*((a/b)*x + y) + (a%b)*x = d // {s = (a/b)*x + y, t = x} <=> {x = t, y = s - (a/b)*t} pair<ll,ll> ext_gcd(ll a, ll b) { if (b==0) return {1,0}; pair<ll,ll> P = ext_gcd(b, a%b); ll s = P.first, t = P.second; return {t, s-(a/b)*t}; } ll solve() { ll N, S, K; cin >> N >> S >> K; ll d = gcd(N,gcd(N-S,K)); ll A = K / d, B = (N-S) / d; N /= d; if (gcd(A,N)!=1) return -1; ll invA = (ext_gcd(A,N).first + N) % N; return B * invA % N; } int main() { ll T; cin >> T; vector<ll> res(T); rep(i,T) res[i] = solve(); for (ll ans : res) cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; int gcd_extend(int a, int b, long long& x,long long& y) { // Base Case if (b == 0) { x = 1; y = 0; return a; } // Recursively find the gcd else { int g = gcd_extend(b, a % b, x, y); int x1 = x, y1 = y; x = y1; y = x1 - (a / b) * y1; return g; } } // Function to print the solutions of // the given equations ax + by = c void print_solution(long long a,long long b,long long c,long long *x1,long long *y1) { long long x, y; if (a == 0 && b == 0) { // Condition for infinite solutions if (c == 0) { cout << "Infinite Solutions Exist" << endl; } // Condition for no solutions exist else { cout << "No Solution exists" << endl; } } int gcd = gcd_extend(a, b, x, y); // Condition for no solutions exist if (c % gcd != 0) { cout << "No Solution exists" << endl; } else { // Print the solution /*cout << "x = " << x * (c / gcd) << ", y = " << y * (c / gcd) << endl; */ *x1=x*(c/gcd); *y1=y*(c/gcd); } } int main(){ // write your code #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int T; cin>>T; while(T--) { long long n,s,k,x,y; cin>>n>>s>>k; if(s%__gcd(n,k)) {cout<<"-1\n";continue;} int d=__gcd(n,__gcd(k,s)); n=n/d; k=k/d; s=s/d; print_solution(k,n,-s,&x,&y); cout<<((x%n)+n)%n<<endl; } }
#include <stdio.h> #include <math.h> int main() { long long R, X, Y; scanf("%lld %lld %lld", &R, &X, &Y); int ans; if (X * X + Y * Y < R * R) { ans = 2; } else { double dis = sqrt((1.0 * X * X + 1.0 * Y * Y) / (1.0 * R * R)); ans = ceil(dis); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl "\n" ll mod=1000000007; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif //ll t;cin>>t;while(t--) { ll a,b,c,d,i,j,k,l,m,n,sum=0,temp=0,f=0,cnt=0; double x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; double ans=((x2*y1)+(x1*y2))/(y1+y2); cout<<fixed<<setprecision(7)<<ans<<endl; } }
#include <iostream> #include <vector> #include <queue> #include <tuple> #include <cmath> #include <algorithm> using namespace std; int K, N, M; int A[100005]; typedef tuple<long long, int, int> P;// (error, original-index, scaled-value) P Q[100005], R[100005]; int main() { scanf("%d%d%d", &K, &N, &M); for (int i = 0; i < K; i++) { scanf("%d", A+i); } double r = (double) M / (double) N; for (int i = 0; i < K; i++) { int scaled = (int)ceil(A[i] * r); Q[i] = make_tuple(abs((long long)N*scaled - (long long)M*A[i]), i, scaled); } int rest = 0; // to decrease for (int i = 0; i < K; i++) { rest += get<2>(Q[i]); } rest -= M; sort(Q, Q+K, [](P &a, P &b) { return get<0>(a) > get<0>(b); }); for (int i = 0; i < rest; i++) { Q[i] = make_tuple(get<0>(Q[i]), get<1>(Q[i]), get<2>(Q[i])-1); } sort(Q, Q+K, [](P &a, P &b) { return get<1>(a) < get<1>(b); }); for (int i = 0; i < K; i++) { printf("%d ", get<2>(Q[i])); } puts(""); }
#include <bits/stdc++.h> #define int ll typedef long long ll; const int K = 100000; int k, n, m; ll a[K + 5]; int l[K + 5], r[K + 5]; bool check(ll x) { ll sl = 0, sr = 0; for (int i = 1; i <= k; i++) { if (a[i] - x > 0) l[i] = (a[i] - x + n - 1) / n; else l[i] = 0; r[i] = (a[i] + x) / n; sl += l[i], sr += r[i]; } return sl <= m && m <= sr; } ll sl[K + 5], sr[K + 5]; int ans[K + 5]; void print(ll x) { // printf("! %lld\n", x); for (int i = 1; i <= k; i++) { if (a[i] - x > 0) l[i] = (a[i] - x + n - 1) / n; else l[i] = 0; r[i] = (a[i] + x) / n; sl[i] = sl[i - 1] + l[i], sr[i] = sr[i - 1] + r[i]; } for (int i = k; i >= 1; i--) { if (m - l[i] <= sr[i - 1]) { ans[i] = l[i], m -= l[i]; } else { ans[i] = m - sr[i - 1]; for (int j = i - 1; j >= 1; j--) ans[j] = r[j]; break; } } for (int i = 1; i <= k; i++) printf("%lld ", ans[i]); puts(""); } signed main() { scanf("%lld%lld%lld", &k, &n, &m); for (int i = 1; i <= k; i++) scanf("%lld", &a[i]), a[i] *= m; ll le = 0, ri = (ll)n * m; while (le < ri) { ll mid = (le + ri) >> 1; if (check(mid)) ri = mid; else le = mid + 1; } print(ri); }
#include <bits/stdc++.h> #define endl "\n" #define ff first #define ss second #define PB push_back #define MP make_pair #define ll long long #define llu unsigned long long #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define ABS(X) ((X) < 0 ? -(X) : (X)) #define LCM(X, Y) (((X) / (GCD((X), (Y)))) * (Y)) #define MEM(A, B) memset((A), (B), sizeof(A)) #define MEM_VEC(V, FILL) fill(V.begin(), V.end(), FILL) #define FOR_EACH(IT, V) for (auto IT = V.begin(); IT != V.end(); IT++) #define DEBUG_ARRAY_PRINT(A, I) cout << "at pos " << I << " value is: " << A[I] #define TURBO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) const double EPS = 1e-9; const double PI = 3.1415926535897932384626433832795; using namespace std; inline ll GCD(ll A, ll B) { if (B == 0) return A; return GCD((B), (A % B)); } inline ll POW(ll BASE, ll EXP) { ll RES = 1; for (ll I = 0; I < EXP; I++) RES *= BASE; return RES; } inline ll BIGMOD(ll a, ll p, ll m) { ll res = 1 % m; ll x = a % m; while (p > 0) { if ((p & 1) > 0) res = (res * x) % m; x = (x * x) % m; p >>= 1; } return res; } inline void FILE_HANDLE() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { TURBO; //FILE_HANDLE() ; int n, a, b; cin >> n >> a >> b; cout << n - a + b << endl; }
// Coder: @SumitRaut #include <bits/stdc++.h> #define speedup ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr),cout.precision(16) using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map; #define sbc __builtin_popcount #define pb push_back #define em emplace #define emb emplace_back #define ff first #define ss second #define ll long long const ll mod = 1e9+7; template<typename T> void smax(T& a, T b) { if(a<b) a=b; } template<typename T> void smin(T& a, T b) { if(a>b) a=b; } template<typename T> T pw(T a,T b) { T p=1,one=1; while(b) { if(b&one) p=p*a; a=a*a; b >>=1; } return p; } template<typename T> T pwm(T a,T b,T md=mod) { T p=1,one=1; while(b) { if(b&one) p=p*a%md; a=a*a%md; b >>=1; } return p; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for(auto& it:v) is>>it; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &it : v) os << it << ' '; return os; } #ifndef ONLINE_JUDGE class Timer { chrono::high_resolution_clock::time_point start_t, end_t; public: Timer() { start_t=chrono::high_resolution_clock::now(); } ~Timer() { end_t = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(end_t-start_t); cerr<<"\nRunTime: "<<duration.count()<<"ms"<<'\n'; } }; void debug() { cerr << '\n'; } template <typename T, typename... Args> void debug(T print, Args... args) { cerr << ' ' << print; debug(args...); } #define deb(...) cerr << "[" << #__VA_ARGS__ << "]" << " --> ", debug(__VA_ARGS__) #else #define deb(...) void(0) #endif void solve() { int n; string s,t; cin>>n>>s>>t; int j=0; ll res=0; for(int i=0;i<n;++i) { if(s[i]!=t[i]) { smax(j,i+1); while(j<n&&s[j]!='1') ++j; if(j==n) { cout<<"-1\n"; return; } res+=j-i; s[j]='0'; } } cout<<res<<'\n'; } int main() { #ifndef ONLINE_JUDGE Timer timer; #endif speedup; //int t; cin>>t; while(t--) solve(); }
#include<iostream> using namespace std; int N; string S,X; int dp[2<<17][7]; bool dfs(int t,int k) { if(dp[t][k]!=0) { return dp[t][k]>0; } bool win=false; if(t==N) { if(k==0)win=true; } else if(X[t]=='T') { if(dfs(t+1,k*10%7))win=true; else if(dfs(t+1,(k*10+S[t]-'0')%7))win=true; } else { if(!dfs(t+1,k*10%7))win=true; else if(!dfs(t+1,(k*10+S[t]-'0')%7))win=true; win=!win; } dp[t][k]=win?1:-1; return win; } main() { cin>>N>>S>>X; cout<<(dfs(0,0)?"Takahashi":"Aoki")<<endl; }
#include <iostream> using namespace std; const int kMaxN = 2e5; int n, s[kMaxN], x[kMaxN]; int f[kMaxN][7]; string tmp; int main() { ios_base::sync_with_stdio(NULL); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> tmp; f[n][0] = 1; for (int i = 0; i < n; i++) { s[i] = tmp[i] - '0'; } cin >> tmp; for (int j = 0; j < n; j++) { x[j] = tmp[j] == 'T'; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 7; j++) { f[i][j] = x[i] ^ (f[i + 1][j * 10 % 7] != x[i] && f[i + 1][(j * 10 + s[i]) % 7] != x[i]); } } cout << (f[0][0] ? "Takahashi" : "Aoki") << endl; return 0; }
#include<bits/stdc++.h> #define int long long constexpr long long mod = 1000000007; #define for0(i, n) for(int i = 0; i < (n); i++) #define for1(i, n) for(int i = 1; i <= (n);i++) #define mp make_pair #define all(x) x.begin(),x.end() #define puts(x) cout << x << "\n" using namespace std; int dp_a[1234], dp_b[1234]; signed main() { int n; char cab, cba, caa, cbb; cin >> n >> caa >> cab >> cba >> cbb; if (cab == 'B') { if (cbb == 'B') { puts(1); return 0; } if (cba == 'A') { int ans = 1; for1(i, n - 3) { ans *= 2; ans %= mod; } puts(ans); } else { dp_b[2] = 1; for (int i = 3; i <= n; i++) { dp_a[i] = dp_b[i - 1]; dp_b[i] = dp_a[i - 1] + dp_b[i - 1]; dp_a[i] %= mod; dp_b[i] %= mod; } puts(dp_b[n]); } } else { if (caa == 'A') { puts(1); return 0; } if (cba == 'B') { int ans = 1; for1(i, n - 3) { ans *= 2; ans %= mod; } puts(ans); } else { dp_a[2] = 1; for (int i = 3; i <= n; i++) { dp_b[i] = dp_a[i - 1]; dp_a[i] = dp_a[i - 1] + dp_b[i - 1]; dp_a[i] %= mod; dp_b[i] %= mod; } puts(dp_a[n]); } } }
#include <bits/stdc++.h> using namespace std; using Int = long long; const char newl = '\n'; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);} template<typename T=int> vector<T> read(size_t n){ vector<T> ts(n); for(size_t i=0;i<n;i++) cin>>ts[i]; return ts; } template<typename T, T MOD = 1000000007> struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v <a.v;} static Mint comb(long long n,int k){ Mint num(1),dom(1); for(int i=0;i<k;i++){ num*=Mint(n-i); dom*=Mint(i+1); } return num/dom; } }; template<typename T, T MOD> constexpr T Mint<T, MOD>::mod; template<typename T, T MOD> ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;} //INSERT ABOVE HERE signed main(){ cin.tie(0); ios::sync_with_stdio(0); int n; cin>>n; string s="XXXX"; for(int i=0;i<4;i++) cin>>s[i]; map<string, int> ans; ans["AAAA"]=1; ans["AAAB"]=1; ans["AABA"]=1; ans["AABB"]=1; ans["ABAA"]=128; ans["ABAB"]=1; ans["ABBA"]=34; ans["ABBB"]=1; ans["BAAA"]=34; ans["BAAB"]=34; ans["BABA"]=128; ans["BABB"]=128; ans["BBAA"]=128; ans["BBAB"]=1; ans["BBBA"]=34; ans["BBBB"]=1; if(n<=3) drop(1); using M = Mint<int>; if(ans[s]==1) drop(1); if(ans[s]==128) drop(M(2).pow(n-3)); M x(1),y(1); for(int i=0;i<n-3;i++){ M z=x+y; x=y; y=z; } cout<<y<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<n;i++) #define repl(i,n) for(ll i=0;i<n;i++) #define repl1(i,n) for(ll i=1;i<n;i++) #define rev(i,n) for(int i=n-1;i>=0;i--) #define revl(i,n) for(ll i=n-1;i>=0;i--) #define repn(i,n) for(int i=0;i<=n;i++) #define repln(i,n) for(ll i=0;i<=n;i++) #define lb lower_bound #define up upper_bound #define pb push_back #define Mod 1000000007 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL); void solve(){ ll n; cin>>n; vector<ll> v(n),inp(n); ll mx=INT_MIN; repl(i,n){ cin>>v[i]; inp[i]=v[i]; } ll ans=0; repl(i,n-1){ v[i+1]+=v[i]; } repl(i,n-1){ v[i+1]+=v[i]; } repl(i,n){ mx=max(mx,inp[i]); cout<<(ll) mx*(i+1)+v[i]<<endl; } // cout<<ans; return ; } int main(){ fastio; int tc; tc=1; // cin>>tc; while(tc--){ solve(); if(tc) cout<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; long long A[200005]; int main(){ int N; scanf("%d", &N); for(int i = 0; i < N; i ++){ scanf("%lld", &A[i]); } long long max1 = 0; long long tempSum = 0; long long prefixSum = 0; for(int i = 0; i < N; i ++){ prefixSum += A[i]; tempSum += prefixSum; max1 = max(max1, A[i]); printf("%lld\n", tempSum + max1*(i+1)); } return 0; }
//#define _GLIBCXX_DEB // Iterator safety; out-of-bounds access for Containers, etc. //#pragma GCC optimize "trapv" // abort() on (signed) integer overflow. #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 ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> #define ll long long int #define mod 1000000007 #define deb(x) cout << #x << "=" << x << "\n" #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << "\n" #define prec(n) fixed << setprecision(n) #define maxpq priority_queue<int> #define minpq priority_queue<int, vector<int>, greater<int>> #define PI 3.1415926535897932384626 #define INF 1000000000 #define no_of_digits(n) floor(log10(n)) + 1 #define bits(n) __builtin_popcount(n) void solve() { char s; cin>>s; char t; cin>>t; if(s=='Y') { cout<<(char)toupper(t); } else cout<<(char)tolower(t); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input1.txt", "r", stdin); // for writing output to output.txt freopen("output1.txt", "w", stdout); #endif int t = 1; //cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; string s; char get (char c) { if (c == '0') return '0'; if (c == '1') return '1'; if (c == '6') return '9'; if (c == '9') return '6'; if (c == '8') return '8'; return 0; } int main() { cin >> s; reverse(s.begin(), s.end()); for (char &c : s) c = get(c); cout << s << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(ll i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;}; template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;}; constexpr ll MOD=1000000007; constexpr ll INF=2e18; int main(){ int a, b; cin >> a >> b; cout << setprecision(10) << (double)a*b/100 << endl; return 0; }
/* author : aryan57 created : 13-June-2021 17:30:46 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); void solve_LOG() { int a,b; cin>>a>>b; long double ans=a*b; ans/=100; cout<<ans; } 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 : 13-June-2021 17:30:42 IST
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please //おそそう const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } return tmp; } void pakuri_sort(int N, ll A[]) { const int b = 8; ll tmp[100001]; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } ll li[300]; int li2[300]; int main() { //cin.tie(0); //ios::sync_with_stdio(false); int N = getint(), M = getint(); int w[8]; rep(i, N) w[i] = getint(); ll lv[100001]; rep(i, M) { ll l = getint(); int v = getint(); lv[i] = l << 32 | v; } pakuri_sort(M, lv); lv[M] = 1000000001ll; rep(i, 1 << N) { rep(j, N) if (i >> j & 1) li[i] += w[j]; li[i] = li[i] << 30 | i; } sort(li, li + (1 << N)); int saidai = 0; int k = 0; rep(i, 1 << N) { while ((lv[k] & (1 << 30) - 1) < li[i] >> 30) { if (saidai < lv[k] >> 32) saidai = lv[k] >> 32; k++; } li[i] = ll(saidai) << 30 | (li[i] & (1 << 30) - 1); } rep(i, 1 << N) li2[li[i] & (1 << 30) - 1] = li[i] >> 30; rep(i, N) { if (w[i] > (lv[0] & (1 << 30) - 1)) { printf("-1"); return 0; } } int P[8]; rep(i, N) P[i] = i; int kotae = 1e9; do { int tmp[8] = {}; rep(p, N - 1) { int omosa = 1 << P[p]; for (int q = p + 1; q < N; q++) { omosa |= 1 << P[q]; int kari = tmp[p] + li2[omosa]; if (tmp[q] < kari) tmp[q] = kari; } } if (kotae > tmp[N - 1]) kotae = tmp[N - 1]; } while (next_permutation(P, P + N)); printf("%d\n", kotae); Would you please return 0; }
#include<bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL); #define time_taken_start int begtime = clock(); #define time_taken_end int endtime = clock(); \ cerr << "\n\n" << "Time elapsed: " << \ (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \ return 0; #define ll long long int #define ull unsigned long long int #define ld long double #define mod 1000000007 #define inf 100000000000000007 #define eps 0.000000000001 #define pi acosl(-1) #define pdd pair<ld,ld> #define pll pair<ll,ll> #define ff first #define ss second #define vpl vector<pll> #define vll vector<ll> #define mseti multiset <ll> #define msetd multiset <ll,greater<ll>> #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define stp fixed<<setprecision(20) #define endl '\n' vll v; pll p[100009]; ll n,m; ll md; ll dis(ll x) { ll l=0,u=m-1,mid; ll ans=md; while(l<=u) { mid=(l+u)/2; if(p[mid].ff>=x) { u=mid-1; } else { l=mid+1; ans=p[mid].ss; } } if(u==-1) ans=0; return ans; } ll check() { ll a[n+1]; a[n]=0; ll pre[n+1]; pre[0]=0; for(ll i=0;i<n;i++) { a[i]=0; pre[i+1]=v[i]+pre[i]; } ll sum; for(ll i=2;i<=n;i++) { sum=0; for(ll j=i-1;j>0;j--) { ll x=pre[i]-pre[j-1]; a[i]=max(a[i],dis(x)-sum); //cout<<i<<' '<<j<<' '<<x<<' '<<dis(x)<<' '<<a[j]<<' '<<sum<<endl; sum+=a[j]; } } sum=0; for(ll i=0;i<=n;i++) sum+=a[i]; return sum; } void solve() { cin>>n>>m; ll x,y; ll cm=0,bm=inf; for(ll i=0;i<n;i++) { cin>>x; v.pb(x); cm=max(cm,x); } for(ll i=0;i<m;i++) { cin>>p[i].ss>>p[i].ff; bm=min(bm,p[i].ff); md=max(md,p[i].ss); } if(cm>bm) { cout<<-1; return; } sort(p,p+m); for(ll i=1;i<m;i++) { p[i].ss=max(p[i].ss,p[i-1].ss); } ll ans=inf; sort(v.begin(),v.end()); do{ ans=min(check(),ans); }while(next_permutation(v.begin(),v.end())); cout<<ans; } int main() { FAST #ifdef ayk_16 freopen("input.txt","rt",stdin); freopen("output.txt","wt",stdout); #endif time_taken_start ll t=1; //cin>>t; for(ll i=0;i<t;i++) { //cout<<"Case #"<<i+1<<": "; solve(); } time_taken_end }
#include<bits/stdc++.h> using namespace std; #define f(i,a,b) for(int i=a; i<b; i++) #define fr(i,a,b) for(int i=a; i>=b; i--) typedef vector<int> vi; typedef long long int ll; typedef vector<ll> vll; #define fastio std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define PI 3.1415926535 // ll Mod = 1e9+7; // ll N = 1e5; ll Mod = 998244353; ll mult(ll a, ll b) { ll ret = 1; ret = ((a%Mod) * (b%Mod))%Mod; return ret; } ll power(ll a, ll b = Mod-2) { if(b == 0) return 1; ll ret = 1; while(b) { if(b&1) { ret = mult(ret,a); } a = mult(a,a); b>>=1; } return ret; } // ll sub(ll a, ll b) { // ll ret = (a%Mod - b%Mod + Mod)%Mod; // return ret; // } // void seive(vector<bool> &status) { // for(ll i = 2; i<=1e6; i++) { // if(status[i]) { // for(ll j =i*i; j<=1e6; j+=i) status[j] = 0; // } // } // } // int getmex(vi& occr, int n, int mex) { // for(int i = mex; i<n; i++) { // if(occr[i] == 0) return i; // } // return n; // } // void swap(int &a, int &b) { // a = a + b; // b = a - b; // a = a - b; // } void test_case() { ll h,w,k,x,y; cin>>h>>w>>k; char a,grid[h][w]; ll moves[h+1][w+1]; memset(moves,0,sizeof moves); f(i,0,h) { f(j,0,w) { grid[i][j] = '.'; } } f(i,0,k) { cin>>x>>y; x--; y--; cin>>a; grid[x][y] = a; } moves[0][0] = 1; ll d = power(3,Mod-2); f(i,0,h) { f(j,0,w) { if(grid[i][j] == '.') { int add = mult(mult(2,moves[i][j]),d); moves[i+1][j] = (moves[i+1][j] + add)%Mod; moves[i][j+1] = (moves[i][j+1] + add)%Mod; } else { if(grid[i][j] == 'X') { moves[i][j+1] = (moves[i][j+1] + moves[i][j])%Mod; moves[i+1][j] = (moves[i+1][j] + moves[i][j])%Mod; } else if(grid[i][j] == 'R') { moves[i][j+1] = (moves[i][j+1] + moves[i][j])%Mod; } else { moves[i+1][j] = (moves[i][j] + moves[i+1][j])%Mod; } } } } cout<<mult(moves[h-1][w-1],power(3,h*w-k))<<'\n'; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif fastio; // vector<bool> status(1e6+1,1); int t = 1; // vll primes; // seive(status); // f(i,2, 1e6+1) { // if(status[i]) primes.push_back(i); // } // cin>>t; while(t--) { test_case(); } }
#include<bits/stdc++.h> using namespace std; #define LL long long #define pa pair<int,int> const int Maxn=5010; const int inf=2147483647; const int mod=998244353; const double pi=acos(-1.0); int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } int h,w,k,a[Maxn][Maxn],f[Maxn][Maxn],pw[Maxn]; int b[Maxn][Maxn],c[Maxn][Maxn]; void upd(int&x,int y){x+=y;if(x>=mod)x-=mod;} int main() { memset(a,-1,sizeof(a)); pw[0]=1;for(int i=1;i<=5000;i++)pw[i]=(LL)pw[i-1]*3%mod; h=read(),w=read(),k=read(); for(int i=1;i<=k;i++) { int x=read(),y=read();char o[5]; scanf("%s",o); if(o[0]=='R')a[x][y]=0; if(o[0]=='D')a[x][y]=1; if(o[0]=='X')a[x][y]=2; } for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) b[i][j]=b[i][j-1]+(a[i][j]==-1); for(int j=1;j<=w;j++) for(int i=1;i<=h;i++) c[i][j]=c[i-1][j]+(a[i][j]==-1); /*for(int i=1;i<=h;i++) { for(int j=1;j<=w;j++) printf("%d ",b[i][j]);puts(""); } for(int i=1;i<=h;i++) { for(int j=1;j<=w;j++) printf("%d ",c[i][j]);puts(""); }*/ f[1][1]=1; for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) if(i!=1||j!=1) { f[i][j]=0; if(i>1&&a[i-1][j]!=0) { if(a[i-1][j]==-1)upd(f[i][j],(LL)f[i-1][j]*2%mod*pw[b[i][j-1]]%mod); else upd(f[i][j],(LL)f[i-1][j]*pw[b[i][j-1]]%mod); } if(j>1&&a[i][j-1]!=1) { if(a[i][j-1]==-1)upd(f[i][j],(LL)f[i][j-1]*2%mod*pw[c[i-1][j]]%mod); else upd(f[i][j],(LL)f[i][j-1]*pw[c[i-1][j]]%mod); } // printf("%d %d %d %d %d\n",i,j,b[i][j],c[i][j],f[i][j]); } if(a[h][w]==-1)f[h][w]=(LL)f[h][w]*3%mod; printf("%d",f[h][w]); }
#include <bits/stdc++.h> using namespace std; template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); } template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); } template <typename T> struct fenwick { int n, lgn; vector<T> fenw; fenwick(int n): n(n) { for(lgn = 0; (1<<lgn) <= n; lgn++); fenw.resize(n); } T get(int p){ T r = T(); for(; p; p -= p & -p) r = r + fenw[p]; return r; } T get(int l, int r){ return get(r) - get(l - 1); } void add(int p, T v){ for(; p < n; p += p & -p) fenw[p] = fenw[p] + v; } int lower_bound(T v){ int p = 0; T sum = 0; for(int i = lgn - 1; i >= 0; i--){ int np = p + (1<<i); if(np < n && sum + fenw[np] < v){ p = np; sum = sum + fenw[p]; } } return p + 1; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> b[i]; using ll = long long; ll d = 0; for(int i = 0; i < n; i++) d += a[i] - b[i]; if(d != 0) return cout << -1 << '\n', 0; for(int i = 0; i < n; i++) a[i] += i, b[i] += i; map<int, vector<int>> pos; for(int i = n - 1; i >= 0; i--){ pos[b[i]].push_back(i); } for(int i = 0; i < n; i++){ if(pos[a[i]].empty()){ return cout << -1 << '\n', 0; } int x = pos[a[i]].back(); pos[a[i]].pop_back(); a[i] = x + 1; } fenwick<int> fenw(n + 1); long long ans = 0; for(int i = 0; i < n; i++){ ans += i - fenw.get(a[i]); fenw.add(a[i], 1); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long t, arr[45]; vector<int> v[2]; vector<long long> p[2]; int id; long long ans = 0; void solve(int idx, long long sum) { if (sum > t) return; if (idx == v[id].size()) { // cout << "SUM " << sum << endl; p[id].push_back(sum); return; } solve(idx + 1, sum); solve(idx + 1, sum + v[id][idx]); } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n >> t; for (int i = 1; i <= n; i++) cin >> arr[i]; sort(arr + 1, arr + n + 1); reverse(arr + 1, arr + n + 1); for (int i = 1; i <= n / 2; i++) v[0].push_back(arr[i]); for (int i = n / 2 + 1; i <= n; i++) v[1].push_back(arr[i]); id = 0; solve(0, 0); id = 1; solve(0, 0); for (int i = 0; i < 2; i++) sort(p[i].begin(), p[i].end()); // for (long long x : p[0]) cout << "0 x " << x << "\n"; // for (long long x : p[1]) cout << "1 x " << x << "\n"; for (long long x : p[0]) { int pos = (int)(lower_bound(p[1].begin(), p[1].end(), t + 1 - x) - p[1].begin()); if (pos == 0) continue; if (p[1][pos - 1] + x > t) continue; ans = max(ans, x + p[1][pos - 1]); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template<typename T> void chmin(T &a, const T &b) { a = min(a, b); } template<typename T> void chmax(T &a, const T &b) { a = max(a, b); } struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio; const ll INF = 1LL<<60; int main() { ll v, t, s, d; cin >> v >> t >> s >> d; if(v*t <= d && d <= s*v) { cout << "No" << endl; } else { cout << "Yes" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define DEBUG #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef pair<int,int> pii; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1<<30; const ll linf = 1LL<<62; const int MAX = 510000; ll dy[8] = {0,-1,0,1,1,-1,-1,1}; ll dx[8] = {-1,0,1,0,1,-1,1,-1}; const double pi = acos(-1); const double eps = 1e-7; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ int sz = a.size(); for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr; sz--; if(sz) cout << " "; } cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){ cout << a << " " << b << "\n"; } template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} const int mod = 1e9 + 7; //const int mod = 998244353; ll a[202020]; int main(){ int n,w; cin >> n >> w; rep(i,n){ ll s,t,p; cin >> s >> t >> p; a[s] += p; a[t] -= p; } rep(i,201010) a[i+1] += a[i]; rep(i,201010){ if(a[i] > w){ cout << "No" << "\n"; return 0; } } cout << "Yes" << "\n"; }
#include <bits/stdc++.h> #include <chrono> #include <ctime> using namespace std; using namespace chrono; #define rep(i,n) for(int i = 0; i<n; ++i) #define REP(i,n) for(int i = 1; i<=n; ++i) #define all(x) begin(x),end(x) #define show(obj) for(auto x:obj)cout<<x<<' ';cout<<endl; typedef vector<vector<int>> state; bool is_spread(int id); void fill(double dur, int width); void init(); void adinit(int id); void output(); void update(int id, int asp, int width); int score(); struct d1{ int x, y, r; }; const int EDGE = 10000; const double tlimit = 4700; const double ideal_rate = 0.98; double ntime; double Stime; int n; int weightsum = 0; vector<d1> req(200); state ans(200, vector<int>(4)); vector<int> area(200); vector<int> ideal(200); vector<int> weight(EDGE*EDGE); vector<bool> expandable(200, true); random_device seed; mt19937_64 gen(seed()); int main(){ Stime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); ntime = Stime; init(); fill(tlimit, 2); output(); return 0; } void fill(double dur, int width){ double endtime = Stime + dur; while(ntime < endtime){ ntime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); int id = gen() % n; if(area[id] > ideal[id] || !expandable[id])continue; int asp = gen() % 4; update(id, asp, width); } } void update(int id, int asp, int width = 1){ bool ok = false; rep(i,4){ int dir = (asp+i) % 4; int ramda = 1; if(dir <= 1){ if(ans[id][dir] - width < 0)continue; }else{ if(ans[id][dir] + width > EDGE)continue; ramda = -1; } ans[id][dir] -= width*ramda; if(is_spread(id)){ int height = 0; if(dir % 2 == 0)height = ans[id][3] - ans[id][1]; else height = ans[id][2] - ans[id][0]; area[id] += width * height; ok = true; }else{ ans[id][dir] += width*ramda; } } if(!ok)expandable[id] = false; } bool is_spread(int id){ bool res = true; rep(i,n){ if(i == id)continue; if(! (ans[id][2] <= ans[i][0] || ans[i][2] <= ans[id][0] || ans[id][3] <= ans[i][1] || ans[i][3] <= ans[id][1]) )res = false; } return res; } int score(){ double sat = 0; rep(i,n){ if(ans[i][0] <= req[i].x && req[i].x < ans[i][2] && ans[i][1] <= req[i].y && req[i].y < ans[i][3]){ int s = (ans[i][2] - ans[i][0]) * (ans[i][3] - ans[i][1]); double t = 1 - (double)min(req[i].r, s)/(double)max(req[i].r, s); sat += 1 - t*t; } } sat /= n; return (int)1e9 * sat; } void init(){ cin >> n; rep(i,n){ cin >> req[i].x >> req[i].y >> req[i].r; adinit(i); ideal[i] = req[i].r * ideal_rate; } } void adinit(int id){ ans[id][0] = req[id].x; ans[id][1] = req[id].y; ans[id][2] = req[id].x + 1; ans[id][3] = req[id].y + 1; area[id] = 1; } void output(){ rep(i,n){ rep(j,4){ if(j != 0)cout << ' '; cout << ans[i][j]; } cout << "\n"; } }
#include<bits/stdc++.h> #define int long long #define ll long long #define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define fs first #define sd second #define rept(x,n) for(int i=x;i<n;i++) #define rrept(n,x) for(int i=n-1;i>=x;i--) #define all(v) v.begin(),v.end() #define mpit map<int,int>::iterator #define stit set<int,int>::iterator #define quit queue<int,int>::iterator #define maxt(a,b,c) max(a,max(b,c)) #define mint(a,b,c) min(a,min(b,c)) #define rsort(v) sort(v.rbegin(),v.rend()) #define pb push_back #define printarr(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" ";cout<<"\n"; #define printvec(v) for(int i = 0;i<v.size();i++)cout<<v[i]<<" ";cout<<"\n"; using namespace std; using namespace std::chrono; ll power(ll a,ll b){ll res = 1;while(b){if(b&1)res*=a;a*=a;b/=2;}return res;} ll powerwithmod(ll a,ll b,ll mod){ll res = 1;while(b){if(b&1)res*=a;res%=mod;a*=a;a%=mod;b/=2;}return res;} void sieve(vector<ll>&prime,ll n){ll contestant[n+1]={0};contestant[0]=1;contestant[1]=1;contestant[2]=0; for(ll i = 4;i<=n;i+=2)contestant[i]=1;for(ll i = 3;i*i<=n;i+=2){if(contestant[i]==0){ for(ll j = i*i;j<=n;j+=i){contestant[j] = 1;}}}for(ll i = 2;i<=n;i++){if(contestant[i]==0)prime.pb(i);}} /*-------------------------------------------------------------------------------------------------------*/ void solve() { ll n; cin>>n; string s; cin>>s; ll q; cin>>q; ll cnt2=0; while(q--) { ll t,a,b; cin>>t>>a>>b; if(t==2) { cnt2^=1; } else { if(cnt2==0) swap(s[a-1],s[b-1]); else { if(a<=n&&b<=n) { swap(s[n+a-1],s[n+b-1]); } else if(a>n&&b>n) { swap(s[a-1-n],s[b-1-n]); } else { swap(s[n+a-1],s[b-1-n]); } } } } if(cnt2==1) { for(ll i = n;i<2*n;i++) cout<<s[i]; for(ll i = 0;i<n;i++) cout<<s[i]; } else cout<<s<<"\n"; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif speed; int t=1; //cin>>t; auto start = high_resolution_clock::now(); while(t--) { solve(); } auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); #ifndef ONLINE_JUDGE cerr << "Time: " << duration.count()/1000.0 << endl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<double, int> P; #define rep(i, x) for (int i = 0; i < x; i++) auto start = std::chrono::system_clock::now(); int cnt = 0; double gettime() { auto end = std::chrono::system_clock::now(); double ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); return ms; } bool finish() { if (cnt % 100 == 0 && gettime() > 1980) return true; else return false; } bool LOCAL_TEST = false; vvi h(30, vi(29)), v(29, vi(30)); int si, sj, ti, tj, a, prev_result; double e, score; string query() { string res; int di = ti - si; if (di >= 0) { rep(l, di) { res += "D"; } } else { rep(l, -di) { res += "U"; } } int dj = tj - sj; if (dj >= 0) { rep(l, dj) { res += "R"; } } else { rep(l, -dj) { res += "L"; } } return res; } int main() { start = std::chrono::system_clock::now(); if (LOCAL_TEST) { rep(i, 30) rep(j, 29) cin >> h[i][j]; rep(i, 29) rep(j, 30) cin >> v[i][j]; } prev_result = 0; score = 0.0; for (int k = 0; k < 1000; k++) { if (LOCAL_TEST) { cin >> si >> sj >> ti >> tj >> a >> e; } else { cin >> si >> sj >> ti >> tj; } string path = query(); cout << path << endl; if (LOCAL_TEST) { int b = path.length(); score = score * 0.998 + a / b; prev_result = round(b * e); } else { cin >> prev_result; } } if (LOCAL_TEST) { cout << round(2312311 * score) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) 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; } using ll = long long; using pii = pair<int, int>; ll solve(){ int si, sj, ti, tj; cin >> si >> sj >> ti >> tj; int v = ti - si; int h = tj - sj; string rou; if(v>0) rou += string(v,'D'); else rou+= string(-v, 'U'); if(h>0) rou += string(h, 'R'); else rou += string(-h, 'L'); cout << rou << endl; ll score; cin >> score; return score; } int main(){ rep(q, 1000){ solve(); } return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,s,n) for (int i = (s); i < (n); ++i) #define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define len(x) (int)(x).size() #define dup(x,y) (((x)+(y)-1)/(y)) #define pb push_back #define eb emplace_back #define Field(T) vector<vector<T>> #define pq(T) priority_queue<T,vector<T>,greater<T>> using namespace std; using ll = long long; using P = pair<int,int>; struct ArbitraryModInt { int x; ArbitraryModInt() : x(0) {} ArbitraryModInt(int64_t y) : x(y >= 0 ? y % get_mod() : (get_mod() - (-y) % get_mod()) % get_mod()) {} static int &get_mod() { static int mod = 0; return mod; } static void set_mod(int md) { get_mod() = md; } ArbitraryModInt &operator+=(const ArbitraryModInt &p) { if((x += p.x) >= get_mod()) x -= get_mod(); return *this; } ArbitraryModInt &operator-=(const ArbitraryModInt &p) { if((x += get_mod() - p.x) >= get_mod()) x -= get_mod(); return *this; } ArbitraryModInt &operator*=(const ArbitraryModInt &p) { unsigned long long a = (unsigned long long) x * p.x; unsigned xh = (unsigned) (a >> 32), xl = (unsigned) a, d, m; asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (get_mod())); x = m; return *this; } ArbitraryModInt &operator/=(const ArbitraryModInt &p) { *this *= p.inv(); return *this; } ArbitraryModInt operator-() const { return ArbitraryModInt(-x); } ArbitraryModInt operator+(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) += p; } ArbitraryModInt operator-(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) -= p; } ArbitraryModInt operator*(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) *= p; } ArbitraryModInt operator/(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) /= p; } bool operator==(const ArbitraryModInt &p) const { return x == p.x; } bool operator!=(const ArbitraryModInt &p) const { return x != p.x; } ArbitraryModInt inv() const { int a = x, b = get_mod(), u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ArbitraryModInt(u); } ArbitraryModInt pow(int64_t n) const { ArbitraryModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ArbitraryModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ArbitraryModInt &a) { int64_t t; is >> t; a = ArbitraryModInt(t); return (is); } }; using mint = ArbitraryModInt; void mul(vector<mint> &dp, int d) { int MAX = len(dp)-1; rrep(i,MAX+1,d) { dp[i] -= dp[i - d]; } } void div(vector<mint> &dp, int d) { int MAX = len(dp); rep(i,d,MAX+1) { dp[i] += dp[i - d]; } } int main() { int n, k, m; cin >> n >> k >> m; mint::set_mod(m); vector<mint> dp(n*n*k+1, 0); vector<vector<mint>> vals(n+1, vector<mint>(n*n*k+1)); dp[0] = mint(1); vals[0][0] = dp[0]; rep(i,1,n) { div(dp, i); mul(dp, i*(k+1)); rep(j,0,n*n*k) { vals[i][j] = dp[j]; } } rep(i,0,n) { mint ans = 0; rep(j,0,n*n*k+1) { ans += vals[i][j] * vals[n-i-1][j]; } cout << ans*(k+1)-1 << endl; } return 0; }
/* May the force be with you */ #include<bits/stdc++.h> using namespace std; #define ll long long #define vi vector<int> #define vl vector<ll> #define pb push_back #define FASTANDFURIOUS ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); #define rep(i,a,b) for(ll i=a;i<b;i++) #define For(i,n) for(ll i=0;i<n;i++) #define MOD 1000000007 #define MAX 1000001 #define pll pair <ll,ll> #define pii pair <int,int> #define dbg(x) cout << #x << ": " << x << endl; #define read(n,v) For(i,n)cin>>v[i] #define mxh priority_queue<int> #define mnh priority_queue<int,vector<int>,greater<int>> #define mkp make_pair #define YES cout<<"YES\n"; #define NO cout<<"NO\n"; #define fi first #define se second #define No cout<<"No\n"; #define Yes cout<<"Yes\n"; #define endl '\n' #define INF 1e18 vl fact(1001,1); ll pow(ll x, unsigned ll y, ll p){ ll res=1LL; x=x%p; if (x==0) return 0; while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } ll _pow(int a, int b){ if(!b) return 1; ll temp = _pow(a, b / 2); temp = (temp * temp); if(b % 2) return (a * temp); return temp; } ll invmod(int a,int m){ return pow(a,m-2,m); } ll ncr(int n,int r){ if(n<r) return 0; ll ans=fact[n]; ans*=invmod(fact[n-r],MOD); ans%=MOD; ans*=invmod(fact[r],MOD); ans%=MOD; return ans; } ll ncr_2(ll n, ll k ){ if (k > n) return 0; if (k * 2 > n) k = n-k; if (k == 0) return 1; ll result = n; for(ll i = 2; i <= k; ++i ) { result = result * (n-i+1); result /= i; } return result; } bool ok(int x){ int l = sqrt(x); if(l*l == x){ return true; } else{ return false; } } signed main(){ FASTANDFURIOUS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int tc=1; // cin>>tc; For(t,tc){ int n; cin>>n; if(n%100){ cout<<n/100+1<<endl; } else{ cout<<n/100<<endl; } } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define pb push_back #define f(i,n) for(int i=0; i<n; ++i) #define fi(i,st,n) for(int i=st; i<=n; ++i) #define veci vector<int> #define vecp vector<pair<int,int> > #define vecl vector<ll> int prime[100000+10]; ll lcm(ll a, ll b) { return a*b/__gcd(a,b); } ll power(ll a, ll n, ll mod) { ll res = 1; while(n > 0) { if(n&1) { res = (res*a)%mod; } a = (a*a)%mod; n = n/2; } return res; } ll sum(int arr[], int n) { ll res = 0; f(i,n) { res += arr[i]; } return res; } void seive() { prime[1] = 0; for(int i=2; i<=100000; i++) { prime[i] = 1; } for(ll i=2; i<=100000; i++) { if(prime[i]) { for(ll j=i*i; j<=100000; j+=i) { prime[j] = 0; } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin>>t; while(t--){ ll a,b,c; cin>>a>>b>>c; if(a == b) { if(c == 0) cout<<"Aoki"; else cout<<"Takahashi"; } else { if(a > b) { cout<<"Takahashi"; } else { cout<<"Aoki"; } } } return 0; }
/** * author: sourav rakshit * created: 13.12.2020 17:30:15 **/ /* #pragma GCC optimize("O3") #pragma GCC target ("avx2") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; namespace std { #define ar array #define len(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define str(s) to_string(s) //@y_combinator template <class F> struct y_combinator { F f; template <class... Args> decltype(auto) operator()(Args &&... args) const { // the lambda should take the first argument as `auto&& recurse` or similar. return f(*this, std::forward<Args>(args)...); } }; template <class F> y_combinator<std::decay_t<F>> make_y_combinator(F &&f) { return {std::forward<F>(f)}; } //@Debugger #ifdef LOCAL template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } void flush_out() { cerr << endl; } template <typename H, typename... T> void flush_out(H h, T... t) { cerr << ' ' << h << " | "; flush_out(t...); } #define log(...) cerr << "[ " << #__VA_ARGS__ << " ] :", flush_out(__VA_ARGS__) #else #define log(...) (void)0x30 #endif template <typename T> inline void mini(T &a, T b) { a = min(a, b); } template <typename T> inline void maxi(T &a, T b) { a = max(a, b); } template <class T> class Random { static T range(T &a, T &b) { return a + rand() % (b - a + 1); } }; template <class K, class V> using table = __gnu_pbds::gp_hash_table<K, V>; template <typename K, typename V, typename Comp = std::less<K>> using ordered_map = __gnu_pbds::tree< K, V, Comp, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; template <typename K, typename Comp = std::less<K>> using ordered_set = ordered_map<K, __gnu_pbds::null_type, Comp>; }; // namespace std using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; const int mod = 1e9 + 7; const ll INF = 1e18; const ld PI = 4 * atan((ld)1); void test_case(const int &tc) { int a, b, c, d; cin >> a >> b >> c >> d; cout << min({a, b, c, d}) << '\n'; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(nullptr); int _t = 1; for (int _ = _t - 1; ~_; --_) { test_case(_); } #ifdef LOCAL cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n"; #endif } // overflows? index_of_of_bound? integer_overflows?
// Etavioxy #include<cstdio> #include<cctype> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #define il inline #define ll long long #define rep(i,s,t) for(register int i=(s);i<=(t);i++) #define rev_rep(i,s,t) for(register int i=(s);i>=(t);i--) #define each(i,u) for(int i=head[u];i;i=bow[i].nxt) #define file(s) freopen(s".in" ,"r",stdin),freopen(s".out","w",stdout) #define pt(x) putchar(x) using namespace std; il int ci(){ register char ch;int f=1; while(!isdigit(ch=getchar()))f=ch=='-'?-1:1; register int x=ch^'0'; while(isdigit(ch=getchar()))x=(x*10)+(ch^'0'); return f*x; } #define double unsigned ll enum{N=104}; struct P{ double x,y; bool operator==(const P&e)const{ return x==e.x&&y==e.y; } bool operator<(const P&e)const{ if( x!=e.x ) return x<e.x; return y<e.y; } }; il P operator+(const P&a,const P&b){ return (P){a.x+b.x,a.y+b.y}; } il P operator-(const P&a,const P&b){ return (P){a.x-b.x,a.y-b.y}; } il P operator*(const P&a,const double b){ return (P){a.x*b,a.y*b}; } il double dot(const P&a,const P&b){ return a.x*b.x+a.y*b.y; } il double cross(const P&a,const P&b){ return a.x*b.y-a.y*b.x; } il double dist(const P&a,const P&b){ return sqrt(dot(a-b,a-b)); } P d0[N],d1[N]; int main(){ int n = ci(); if( n==1 ){ cout<<"Yes"<<endl; return 0; } rep(i,1,n) d0[i] = (P){(double)((ll)ci()),(double)((ll)ci())}; rep(i,1,n) d1[i] = (P){(double)((ll)ci()),(double)((ll)ci())}; P a0 = d0[2]-d0[1]; P a1 = (P){a0.y,-a0.x}; double x1 = 0, x2 = 0, x3 = 0; rep(i,1,n){ double x = dot(d0[i]-d0[1],a0)*(double)(1e9+7)+dot(d0[i]-d0[1],a1); x1 += x; x2 += x*x*x*x; x3 += x*x*x; } // printf("x123 %llu %llu %llu\n",x1,x2,x3); rep(i,1,n){ rep(j,1,n) if( i!=j ){ a0 = d1[j]-d1[i]; a1 = (P){a0.y,-a0.x}; double y1 = 0, y2 = 0, y3 = 0; rep(k,1,n){ double x = dot(d1[k]-d1[i],a0)*(double)(1e9+7)+dot(d1[k]-d1[i],a1); y1 += x; y2 += x*x*x*x; y3 += x*x*x; } // printf("%d %d : \n",i,j); // printf("y123 %llu %llu %llu\n",y1,y2,y3); if( x1==y1 && x2==y2 && x3==y3 ){ cout<<"Yes"<<endl; return 0; } } } cout<<"No"<<endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++) #define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--) #define rep(i, n) FOR(i, 0, n) #define ALL(x) begin(x), end(x) using namespace std; using ll = long long; constexpr ll Mod = 998244353; constexpr ll mod = 1e9 + 7; constexpr ll inf = 1LL << 60; const double PI = acos(-1); 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); } /*-------------------------------------------*/ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<int> p(n); rep(i, n) cin >> p[i]; vector<int> ans, used(n); FOR(i, 1, n) { int j = i; while(j > 0 && p[j - 1] > p[j]) { swap(p[j - 1], p[j]); if(used[j]) { cout << -1 << endl; return 0; } ans.push_back(j); used[j] = 1; j--; } } if((int)ans.size() != n - 1) { cout << -1 << endl; return 0; } rep(i, n - 1) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) #define per(i,n) for(int i=(n)-1;i>=0;i--) #define rep1(i,n) for(int i=1;i<=(n);i++) #define per1(i,n) for(int i=(n);i>=1;i--) #define repk(i,a,b) for(int i=(a);i<=(b);i++) #define perk(i,a,b) for(int i=(a);i>=(b);i--) #define rep0(i,a,b) for(int i=(a);i<(b);i++) #define pb push_back #define mp make_pair #define g(x) cout<<#x<<'='<<x<<endl #define nextp next_permutation #define pq priority_queue #define fi first #define se second typedef long long ll; using namespace std; const ll mod1=998244353; const ll mod2=1000000007; //long long , 特判 //ios_base::sync_with_stdio(false);加快cin long long f(long long a) { long long res=1; for(int i=1;i<=a;++i) { res=res*i%mod2; } return res; } long long qkpw(long long a,long long b) { long long res=1; while(b) { if(b&1) res=res*a%mod2; a=a*a%mod2; b>>=1; } return res; } long long inv(long long a) { return qkpw(a,mod2-2); } long long c(long long n,long long m) { if(n<m) return 0; return f(n)*inv(f(m)*f(n-m)%mod2)%mod2; } long long n,m,k; int main() { cin>>n>>m>>k; if(n>m+k) cout<<0<<endl; else cout<<(c(n+m,n)-c(n+m,k+m+1)+mod2)%mod2<<endl; return 0; }
#include <cctype> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <ios> #include <iostream> #include <iterator> #include <string_view> #include <utility> #include <vector> #include <string> #include <algorithm> #include <queue> #include <climits> #include <cmath> #include <sstream> #include <iomanip> #include <map> #include <stack> #include <regex> #include <set> #include <bitset> #include <list> const int64_t infl = 1LL << 60; const int64_t cc = pow(10, 9) + 7; bool sort_pair(std::pair<int64_t, char> origin, std::pair<int64_t, int64_t> another) { return origin.first < another.first; } bool sort_pair2(std::pair<int64_t, char> origin, std::pair<int64_t, int64_t> another) { return origin.second < another.second; } template <typename T> bool chmin(T &a, const T& b) {if(a > b){a = b;return true;}return false;} template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b;return true; } return false; } //繰り返し二乗法 int64_t RepeatSquaring( int64_t n, int64_t p, int64_t mod ) { if( p == 0 ) { return 1; } if( p % 2 == 0 ) { int64_t t = RepeatSquaring( n, p / 2, mod ); return t * t % mod; } return n * RepeatSquaring( n, p - 1, mod ) % mod; } //フェルマーの小定理を利用するのでmodが素数の時のみ int64_t waru_mod( int64_t a, int64_t b, int64_t mod ) { b = RepeatSquaring( b, mod - 2, mod ) % mod; return ( a * b ) % mod; } //コンビネーションをmodで割った余りを産出 int64_t nCk( int64_t n, int64_t k, int64_t mod ) { int64_t ans_x = 1; int64_t ans_y = 1; for( int i = n; i > n - k; i-- ) { ans_x *= i; ans_x %= mod; } for( int i = 1; i <= k; i++ ) { ans_y *= i; ans_y %= mod; } return waru_mod( ans_x, ans_y, mod ); } int main() { int64_t q,k,h,w,d,m,n; std::cin>> n; std::cin>> m; std::cin>> k; if( k + m < n ) { std::cout<< 0; } else { q = nCk( n + m, n, cc ); if( n - k - 1 < 0 ) { h = 0; } else { h = nCk( n + m, n - k - 1, cc ); } q -= h; q %= cc; if( q < 0 ) q += cc; std::cout<< q; } }
#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; // #pragma GCC optimize("O2") typedef long long int ll; typedef long double ld; typedef map <ll,ll> mm; typedef vector <ll> mv; typedef pair <ll,ll> mp; typedef set <ll> ms; typedef multiset <ll> mms; typedef queue <ll> mq; typedef deque <ll> mdq; typedef stack <ll> mst; typedef priority_queue <ll> mpq; typedef priority_queue <ll, vector<ll>, greater<ll> > mmh; typedef complex<double> cd; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define flush cout.flush(); #define all(v) v.begin(),v.end() #define deb(x) cout << #x << " " << x << endl; #define ff first #define ss second #define pb(v) push_back(v) #define lb(v) lower_bound(v) #define ub(v) upper_bound(v) #define cn continue #define forl(i,n) for(ll i=0;i<n;i++) #define forlr(i,n) for(ll i=n-1;i>=0;i--) #define rev(s) reverse(s.begin(),s.end()) void swap(ll &a,ll &b) { ll tmp=a; a=b; b=tmp; } ll atoistring(string s) { stringstream lol(s); ll x; lol>>x; return x; } void pv(mv v) { forl(i,v.size()) cout<<v[i]<<" "; cout<<"\n"; } void pa(ll *v, ll size) { forl(i,size) cout<<v[i]<<" "; cout<<"\n"; } void removeDups(mv &v) { sort(all(v)); mv::iterator it = unique(v.begin(), v.end()); v.resize(distance(v.begin(), it)); } inline ld kthRoot(ld n, ll k) { return pow(k, (1.0 / k) * (log(n) / log(k))); } 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; } void checkmod(ll &x, ll mod) { if(x>=mod) x%=mod; } ll mod = 1e9 + 7; void thisIsMain() { ll n, s, d; cin>>n>>s>>d; bool f = 0; forl(i,n) { ll x, y; cin>>x>>y; if(x < s && y > d) f = 1; } if(f) cout<<"Yes\n" ;else cout<<"No\n"; } int main(void) { fastio ll t = 1; // cin>>t; while(t--) thisIsMain(); return 0; }
#include<iostream> #include<cmath> #define ll long long using namespace std; const double eps = 1e-8; int main() { long double a, b, c, d; cin >> a >> b >> c >> d; if (d*c-b < eps) cout << -1; else cout << ceil(a/(d*c-b)); return 0; }
#include<cstdio> #include<cmath> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<functional> #include <random> #include<iostream> #include<iomanip> using namespace std; int main() { /*(P-1)*(P-2)^(N-1)を出力すればよい。*/ long long N, P; cin >> N >> P; long long ans = P - 1; long long b = P - 2; N = N - 1; while (N > 0) { if (N % 2 == 1) { ans *= b; ans %= 1000000007; } b = b * b % 1000000007; N = N / 2; } cout << ans; }
#include <iostream> #include <vector> #include <cmath> #include <iomanip> #include <algorithm> #include <queue> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define all(x) (x).begin(), (x).end() long long MOD = 1000000007; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { long long n,p; cin>>n>>p; cout<<(modpow(p-2,n-1,MOD)*(p-1))%MOD; return 0; }
//#include <atcoder/all> //using namespace atcoder; #include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for(int i=0; i<n; i++) #define REPR(i, n) for(int i=n-1; i>=0; i--) #define FOR(i, m, n) for(int i=m; i<n; i++) #define ALL(v) v.begin(), v.end() #define bit(n) (1LL<<(n)) #define FIX(d) fixed << setprecision(d) using P = pair<int, int>; using LP = pair<ll, ll>; using Graph = vector<vector<int>>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1e9; const ll LINF = (1LL<<60); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int v,t,s,d; cin >> v >> t >> s >> d; if(v*t<=d && d<=v*s) cout << "No\n"; else cout << "Yes\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; bool can_go(int x, int y, vector<vector<char>> s) { if(s[x][y] == '.') return true; else return false; } int main() { int h, w, x, y; cin >> h >> w >> x >> y; x--; y--; vector<vector<char>> s(h, vector<char> (w)); rep(i, h) { rep(j, w) cin >> s[i][j]; } int ans = 1; int s_x = x; int s_y = y; // turn right while (1){ y++; if(y < w) { if(can_go(x, y, s)) ans++; else break; } else break; } x = s_x; y = s_y; // turn left while (1){ y--; if(-1 < y) { if(can_go(x, y, s)) ans++; else break; } else break; } x = s_x; y = s_y; // up while (1){ x--; if(-1 < x) { if(can_go(x, y, s)) ans++; else break; } else break; } x = s_x; y = s_y; // down while (1){ x++; if(x < h) { if(can_go(x, y, s)) ans++; else break; } else break; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long #ifdef TEST #define getchar_unlocked _getchar_nolock #endif #define mp make_pair #define mt make_tuple #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define eb emplace_back #define g0(a) get<0>(a) #define g1(a) get<1>(a) #define g2(a) get<2>(a) #define g3(a) get<3>(a) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef double db; typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef tuple<int, int, int> iii; typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int A, B, C; ld dp[105][105][105], top, bot; main() { ios::sync_with_stdio(0); cin.tie(0); cin >> A >> B >> C; dp[A][B][C] = 1; for (int i = A; i <= 100; i++) for (int j = B; j <= 100; j++) for (int k = C; k <= 100; k++) { if (i == A && j == B && k == C) continue; vector<int> tmp; if (i == 100) tmp.pb(1); if (j == 100) tmp.pb(2); if (k == 100) tmp.pb(3); if (tmp.size() > 1) continue; else if (tmp.size() == 0) { if (i > 0) dp[i][j][k] += dp[i - 1][j][k] * (i - 1) / (i - 1 + j + k); if (j > 0) dp[i][j][k] += dp[i][j - 1][k] * (j - 1) / (i - 1 + j + k); if (k > 0) dp[i][j][k] += dp[i][j][k - 1] * (k - 1) / (i - 1 + j + k); } else { if (tmp[0] == 1 && i > 0) dp[i][j][k] += dp[i - 1][j][k] * (i - 1) / (i - 1 + j + k); else if (tmp[0] == 2 && j > 0) dp[i][j][k] += dp[i][j - 1][k] * (j - 1) / (i - 1 + j + k); else if (tmp[0] == 3 && k > 0) dp[i][j][k] += dp[i][j][k - 1] * (k - 1) / (i - 1 + j + k); } //cout << i << ' ' << j << ' ' << k << ' ' << dp[i][j][k] << '\n'; if (tmp.size() == 1) top += (i - A + j - B + k - C) * dp[i][j][k], bot += dp[i][j][k]; } cout << fixed << setprecision(10) << top / bot << '\n'; }
#include<bits/stdc++.h> using namespace std; const int N=201000; const long long mod=1e9+7; int n,K,Cnt; int Num[N]; long long Ans; string st; bool vis[N]; long long Mul[N]; long long G[N][21][21]; long long F[N][21]; long long Pow(long long x,long long y){ long long sum=1; for(;y;y>>=1,x=x*x%mod)if(y&1)sum=sum*x%mod; return sum; } int tran(char ch){ if(ch>='0'&&ch<='9')return ch-'0'; else return ch-'A'+10; } long long Fac[N],IFac[N]; long long C(int x,int y){ if(x<0||y<0||x<y)return 0; return Fac[x]*IFac[x-y]%mod*IFac[y]%mod; } void Init(void){ Mul[0]=1;for(int i=1;i<=n;i++)Mul[i]=Mul[i-1]*K%mod; Fac[0]=1;for(int i=1;i<=20;i++)Fac[i]=Fac[i-1]*i%mod; IFac[20]=Pow(Fac[20],mod-2);for(int i=19;i>=0;i--)IFac[i]=IFac[i+1]*(i+1)%mod; for(int i=1;i<=20;i++)G[0][i][0]=1; for(int now=1;now<=20;now++) for(int i=1;i<=n;i++) for(int j=1;j<=now;j++)G[i][now][j]=(G[i-1][now][j]*j%mod+G[i-1][now][j-1]*(now-j+1)%mod); F[0][0]=1; for(int i=1;i<=n;i++) for(int j=1;j<=K;j++)F[i][j]=G[i][j][j]; return; } void update(int Pos) { long long t=0; if(Pos==0){if(Cnt==K)++Ans;return;} for(int i=0;i<Num[Pos];i++){ if(Pos==n&&i==0)continue; if(vis[i]){ for(int j=K-Cnt;j<=K;j++)Ans=(Ans+C(16-Cnt,K-Cnt)*C(Cnt,j-(K-Cnt))%mod*F[Pos-1][j])%mod; } else { for(int j=K-Cnt-1;j<=K;j++)Ans=(Ans+C(16-Cnt-1,K-Cnt-1)*C(Cnt+1,j-(K-Cnt)+1)%mod*F[Pos-1][j])%mod; } }return; } int main() { cin>>st>>K;n=st.size(); for(int i=n-1;i>=0;i--)Num[n-i]=tran(st[i]); Init(); for(int i=1;i<=n-1;i++){ Ans=(Ans+C(15,K-1)*(F[i-1][K-1]+F[i-1][K])%mod*15%mod)%mod; } update(n); for(int i=n;i>=1;i--){ if(!vis[Num[i]])vis[Num[i]]=1,++Cnt; if(Cnt>K)break; update(i-1); } cout<<Ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long dist(long long r1,long long c1,long long r2,long long c2){ return abs(r1-r2)+abs(c1-c2); } int main(){ long long r1,c1,r2,c2,x1,y1,x2,y2; string s; cin >> r1 >> c1 >> r2 >> c2; int ct = 0; while(dist(r1,c1,r2,c2)>0){ ct++; if (r1+c1 == r2+c2 || r1-c1 == r2-c2 || dist(r1,c1,r2,c2)<=3){ r1 = r2; c1 = c2; } else { x1 = (c1 - r1 + c2 + r2)/2; y1 = x1 - c1 + r1; x2 = (c1 + r1 + c2 - r2)/2; y2 = -x2 + c1 + r1; if (dist(x1,y1,c2,r2)<dist(x2,y2,c2,r2)){ c1 = x1; r1 = y1; } else { c1 = x2; r1 = y2; } } } cout << ct << endl; return 0; }
/* “The only way that we can live is if we grow. The only way we can grow is if we change. The only way we can change is if we learn. The only way we can learn is if we are exposed. And the only way that we are exposed is if we throw ourselves into the open.” *************************************** ************************ ********************************** */ #include <bits/stdc++.h> using namespace std; //---------------------mishra ka idea------------------ #define int long long #define double long double #define pb push_back #define pf push_front #define pii pair<int,int> #define vi vector<int> #define vii vector<pii> #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define x first #define y second #define endl '\n' #define sz(x) (int)(x).size() #define ull unsigned long long //------------------------mishra ka idea------------------- //-------------------------alsi hu mai----------------------- #define fo(i,l,u) for(i=l;i<u;i++) #define rfo(i,l,u) for(i=l;i>=u;i--) #define allfo(s) for(auto it=(s).begin();it!=(s).end();it++) #define _init(b) memset(b,-1,sizeof(b)) #define _init0(b) memset(b,0,sizeof(b)) #define MOD 1000000007 #define hell 998244353 #define output(x) cout << (x ? "YES" : "NO")<<endl; #define Output(x) cout << (x ? "Yes" : "No")<<endl; //-------------------------alsi hu mai----------------------- //--------------------het se uthaya--------------------- #define MOD2 (998244353) #define MOD3 (1000000009) #define PI acos(-1) #define eps (1e-8) #define INF (1e18) template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";} template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;} template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;} template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;} ifstream cinn("in.txt");ofstream coutt("out.txt"); int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;} //--------------------het se uthaya------------------------------------------------------------------- int gcd(int a, int b) {if(a>b)swap(a,b) ; if (a == 0) return b; return gcd(b % a, a); } int rint(int l , int r){return rand()%(r-l+1) + l ;} //---------------------------aab code dekho bahut template dekh liya--------------------------------- signed main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int i,j,x,a,b,c,d,r1,c1,r2,c2,d1,d2; cin>>r1>>c1>>r2>>c2; if(r1==r2 && c1==c2){ cout<<0<<endl; return 0; } d1=abs(r1-r2); d2=abs(c1-c2); if(d1+d2<=3){ cout<<1<<endl; return 0; } int ans=0; if(abs(d1-d2)==0){ cout<<1<<endl; return 0; } if(abs(d1-d2)<=3){ cout<<2<<endl; return 0; } if(d1%2 != d2%2)ans++; cout<<ans+2<<endl; return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int num[524310]; int kazu[524310]; const int ma = (1 << 19) - 1; int query(ll n) { int n2 = (n & ma) + 1; while (num[n2]) { if (num[n2] == n + 1) return kazu[n2]++; n2 = n2 * 2 % 524309; } num[n2] = n + 1; kazu[n2] = 1; return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); ll are = N * ll(N - 1) / 2; rep(i, N) are -= query(getint()); printf("%lld\n", are); Would you please return 0; }
#include <bits/stdc++.h> #define spd ios::sync_with_stdio(false),cin.tie(0) #define mod 1000000007; #define pie acos(-1) #define eps 1e-9 #define fr(i,a,b) for(i = a; i < b; i++) #define Fr(i,a,b) for(i = a; i >= b; i--) #define rst(a,b) memset(a,b,sizeof(a)) #define in1(a) cin >> a #define in2(a,b) cin >> a >> b #define in3(a,b,c) cin >> a >> b >> c #define in4(a,b,c,d) cin >> a >> b >> c >> d #define sinp(a) getline(cin, a) #define tout(a,b) cout << "Case " << ++b << ": " << a << endl #define dprint(x,y,z) cout << setprecision(x) << fixed << y << " " << z << endl #define srt(a,n) sort(a, a + n) #define rev(a,n) sort(a, a + n, greater<ll>()) #define pb(a) push_back(a) #define len(a) a.length() #define bs(a,b) binary_search(a.begin(), a.end(), b) #define lob(a,b) lower_bound(a.begin(), a.end(), b) #define upb(a,b) upper_bound(a.begin(), a.end(), b) using namespace std; using ull = unsigned long long; using ll = long long; using db = double; using ss = string; unordered_map<ll,ll> freq; int main() { spd; //ll t,tst = 0; in1(t); //while(t--) { ll n,i, sum = 0; in1(n); vector<ll> vec(n); fr(i,0,n) { in1(vec[i]); if(freq[vec[i]]) sum += n - i - 1 - freq[vec[i]]; else sum += n - i - 1; freq[vec[i]]++; } cout << sum << endl; //} }
#include <bits/stdc++.h> using namespace std; int n; bool validDec(int value){ bool ret = true; while(value>0){ if(value%10==7){ ret = false; break; } value /= 10; } return ret; } bool validOct(int value){ bool ret = true; while(value>0){ if(value%8==7){ ret = false; break; } value /= 8; } return ret; } int main(){ cin >> n; long long ans = 0; for(int i = 1; i <= n; i++){ if(validDec(i)&&validOct(i))ans++; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" using namespace std; using namespace __gnu_pbds; #define pb push_back #define vi vector<int> #define ll long long int #define us unsigned long long int #define dig(n) floor(log10(n)) + 1 #define all(x) (x).begin(),(x).end() #define zrobits(x) __builtin_ctzll(x) #define setbits(x) __builtin_popcountll(x) #define f(i, a, b) for (int i = a; i < b; i++) #define ps(x,y) fixed<<setprecision(y)<<x #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define CP() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); const int32_t MOD = 1e9 + 7; const int32_t mod = 998244353; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update>indexed_set; bool fx(int n){ string s; string $s=to_string(n); while(n){ s=to_string(n%8)+s; n/=8; } s+=$s; for(auto x:s){ if((x-'0')==7) return false; } return true; } auto main() -> int { CP() ll t; t=1; // cin>>t; while (t--) { int n; cin>>n; int ans(0); for(int i=1;i<=n;i++){ (fx(i))?ans++:n=n; } cout<<ans<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MOD = 1000000007; int main(){ int n; cin >> n; vector<int> a(1 << n); for (int i = 0; i < (1 << n); i++){ cin >> a[i]; } int leftmax = 0, rightmax = 0; for (int i = 0; i < (1 << n)/2; i++){ leftmax = max(leftmax, a[i]); } for (int i = (1 << n)/2; i < (1 << n); i++){ rightmax = max(rightmax, a[i]); } int ans = min(leftmax, rightmax); for (int i = 0; i < (1 << n); i++){ if (a[i] == ans){ cout << i+1 << "\n"; break; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++) #define frr(i,a,b) for (ll i = (b), _a = (a); i >= _a; i--) #define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i,n) for (ll i = (n) - 1; i >= 0; i--) #define debug(x) cout<<"debug : "<<x<<endl #define debug2(x,y) cout<<"debug : "<<x<<" | "<<y<<endl #define debug3(x,y,z) cout<<"debug : "<<x<<" | "<<y<<" | "<<z<<endl #define debug4(x,y,z,w) cout<<"debug : "<<x<<" | "<<y<<" | "<<z<<" | "<<w<<endl #define all(ar) ar.begin(), ar.end() #define rall(ar) ar.rbegin(), ar.rend() #define sz(x) ((long long)x.size()) #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define mp make_pair #define ff first #define ss second #define M 1000000007 // 1e9+7 #define INF 1e9 // 1e9 typedef pair<ll, ll> ii; typedef pair<ll, ii> iii; typedef vector<ii> vii; typedef vector<ll> vi; typedef vector<int> vint; typedef vector<string> vs; typedef vector<vi> vvi; bool cus(pair<int,int> a, pair<int,int> b){ if(a.ff<b.ff) return true; if(a.ff>b.ff) return false; return a.ss<b.ss; } ll gcd(ll a, ll b){ if(b==0) return a; return gcd(b,a%b); } void solve(){ ll n; cin >> n; ll val = pow(2, n); vii a; ll x; rep(i, val){ cin >> x; a.pb({x, i}); } while(a.size() > 2){ vii tmp; for(ll i = 0; i < a.size(); i+=2) tmp.pb(max(a[i], a[i+1])); a.clear(); rep(i, tmp.size()) a.pb(tmp[i]); } ii ans = min(a[0], a[1]); cout << ans.ss+1 << endl; } int main () { ios_base::sync_with_stdio(false); cin.tie(NULL); cout<<setprecision(11); //ll t = 1; //cin>>t; //while(t--) solve(); return 0; }
#include<iostream> #include<string> #include<vector> #include<utility> #include<algorithm> #include<map> #include<set> #include<cstdlib> #include<cmath> #include<numeric>//fill #include<iomanip> #include<functional>//https://cpprefjp.github.io/reference/functional/function.html #include<cstdlib>//https://cpprefjp.github.io/reference/cstdlib.html #include<queue> #include<deque> #include<cassert> #include<stack> #include <iterator> // std::back_inserter //#include<atcoder/all> using namespace std; //using namespace atcoder; const double PI = acos(-1); using ll = long long; using P = pair<ll,ll>; #define rep(i,n)for(ll i=0;i<(n);i++) #define repp(x,arr) for(auto& x:arr) #define repp2(x,y,arr) for(auto& [x,y]:arr) #define all(a) (a.begin()),(a.end()) const ll MOD = 1e9 + 7; const ll inf = (ll)1e18+6; // デフォルトは整数部も含めた桁数指定 //cout <<setprecision(); ll myPow(ll x, ll n, ll m) { if (n == 0) return 1; if (n % 2 == 0) return myPow(x * x % m, n / 2, m); else return x * myPow(x, n - 1, m) % m; } double dl[4] = { 0,0,0.1,0.1 }; double dr[4] = { 0,-0.1,0,-0.1 }; int main() { ios::sync_with_stdio(false); int N; cin >> N; vector<pair<double, double>>s(N), t(N); map<pair<double, double>,bool>m; rep(i, N) { cin >> t[i].first >> t[i].second; } rep(i, N) { cin >>s[i].first >> s[i].second; m[s[i]] = true; } if (N == 1) { cout << "Yes"; return 0; } double a, b,a2,b2; a = s[0].first, b = s[0].second; a2 = s[1].first, b2 = s[1].second; double p1; p1 = atan((b2 - b) / (a2 - a)); bool ans = false; //cout << "指標は" << a << " " << b << endl; rep(i, N) { double q, r,c, d; c = t[i].first, d = t[i].second; for (int j = 0; j < N; j++) { if(j==i)continue; double c2, d2,p,p2; c2 = t[j].first; d2 = t[j].second; c2 -= c; d2 -= d; p = p1-atan(d2 / c2); p2 = p + PI; { double A1, B1, x, y, x2, y2; A1 = t[j].first - c; B1 = t[j].second - d; x = A1 * cos(p) - B1 * sin(p); y = A1 * sin(p) + B1 * cos(p); x2 = A1 * cos(p2) - B1 * sin(p2); y2 = A1 * sin(p2) + B1 * cos(p2); x += a; y += b; x = round(x); y = round(y); x2 += a; y2 += b; x2 = round(x2); y2 = round(y2); if (x==a2&&y==b2) { p = p; } else if (x2 == a2 && y2 == b2) { p = p2; } else { continue; } } rep(k, N) { double A1, B1,x,y,x2,y2; A1 = t[k].first - c; B1 = t[k].second - d; x = A1 * cos(p) - B1 * sin(p); y = A1 * sin(p) + B1 * cos(p); //x2 = A1 * cos(p2) - B1 * sin(p2); y2 = A1 * sin(p2) + B1 * cos(p2); //cout << x << " " << y << endl; //cout << x2 << " " << y2 << endl; x += a; y+= b; x = round(x); y = round(y); if (m[{x,y}] == false) { //cout << "break"<<endl; break; } if (k == N - 1)ans = true; } } } if (ans)cout << "Yes"; else cout << "No"; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n,f=0; cin>>n; string s,ans; map<string,long long int>m; vector<string>v; for(int i=0; i<n; i++) { cin>>s; if(s[0]=='!') {m[s]=1;} else v.push_back(s); } for(int i=0;i<v.size();i++){ ans=v[i]; ans='!'+v[i]; if(m[ans]==1){ cout<<v[i]<<endl; f=1; break; } } if(f==0)cout<<"satisfiable"<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define MOD 1000000007LL #define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++) #define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++) #define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++) #define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++) #define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--) #define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--) #define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--) #define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--) #define iter(i, it) for(auto &(i): (it)) template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) { cout << "(" << m.first << ", " << m.second << ")"; return s; } template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) { ll c = 0; cout << "{ "; iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", "); cout << "}"; return s; } template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) { cout << "{ "; rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", "); cout << "}"; return s; } template<typename T> ostream& operator<<(ostream &s, const list<T> &v) { ll c = 0; cout << "{ "; iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", "); cout << "}"; return s; } int main(void) { ll N; string S; cin >> N >> S; ll ans = 0; rep(i, N) { map<char, ll> m; rep2(j, i, N) { m[S[j]]++; if(m['A'] == m['T'] && m['C'] == m['G']) ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> #define endl "\n" using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; 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; } const long long INF = 1e18; //const ll mod = 1000000007; ll N; ll A[401000]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; ll ans = 0; for(int i = 0; i < 2 * N; i++) { cin >> A[i]; ans += A[i]; } priority_queue<ll, vector<ll>, greater<ll>> que; for(int i = 0; i < N; i++) { que.push(A[N+i]); que.push(A[N-1-i]); ans -= que.top(); que.pop(); } cout << ans << endl; return 0; }