code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include<bits/stdc++.h> using namespace std; bool f[200001][7]; string s1,s2; int n; int main(){ scanf("%d",&n); cin>>s1>>s2; f[n][0]=1; for(int i=n-1;i>=0;i--){ int num=s1[i]-'0'; for(int j=0;j<7;j++) if(s2[i]=='A') f[i][j]=f[i+1][(j*10+num)%7]&f[i+1][(j*10)%7]; else f[i][j]=f[i+1][(j*10+num)%7]|f[i+1][(j*10)%7]; } if(f[0][0])printf("Takahashi"); else printf("Aoki"); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) const ll MAX = 1001001; const ll MOD = 998244353; const double pi = 2.0 * asin(1.0); ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a % mod * (a % mod) % mod; n >>= 1; } return res; } int main() { ll n, m; cin >> n >> m; vector<string> s(n); rep(i, n) { cin >> s[i]; } vector<ll> d(n + m); vector<ll> d2(n + m); vector<ll> d3(n + m); rep(i, n) { rep(j, m) { if (s[i][j] == 'R') { d[i + j]++; } else if (s[i][j] == 'B') { d2[i + j]++; } else { d3[i + j]++; } } } ll co = 0; rep(i, n + m) { if (d[i] > 0 && d2[i] > 0) { cout << 0 << endl; return 0; } if (d3[i] > 0 && d[i] == 0 && d2[i] == 0) { co++; } } ll ans = modpow(2, co, MOD); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr long long MOD = 1000000007; constexpr long long MOD2 = 998244353; constexpr long long INF = 1LL << 60; const long double PI = acosl(-1.0); constexpr long double EPS = 1e-11; 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; } void factorization(long long n,map<long long ,long long>& mp){ for(long long i=2;i*i<=n;i++){ while(n%i==0){ mp[i]++; n /= i; } } if(!(n == 1)) mp[n]++; } void divisor(ll n, map<ll,ll>& ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret[i]++; if (i * i != n) ret[n / i]++; } } } int main(){ ll n; cin >> n; map<ll, ll> mp; ll ans = 0; divisor(2 * n, mp); for(auto i:mp){ ll x = i.first; ll y = 2 * n / i.first; if (x < y) swap(x, y); if ((x - y - 1) % 2 == 0 and (x + y - 1) % 2 == 0) ans++; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; long long f[110],n; int main(){ scanf("%lld",&n); f[0]=1;f[1]=1; int t=1; while(f[t-1]+f[t]<=n) t++,f[t]=f[t-1]+f[t-2]; long long tmp=n; int ans=t; for(int i=t;i>=1;i--) if(tmp>=f[i]) tmp-=f[i],ans++; printf("%d\n",ans); for(int i=t;i>=1;i--){ if(n>=f[i]) printf("%d\n",(i&1)+1),n-=f[i]; printf("%d\n",((i-1)&1)+3); } }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <stdio.h> #include <stdlib.h> #include <iomanip> #define FAST ios::sync_with_stdio(0);cin.tie(0); #define pb push_back #define mp make_pair #define ff first #define ss second #define el "\n" #define ll long long #define ld long double #define pii pair<ii,ii> #define pll pair<ll,ll> #define vcii vector<ii> #define vcll vector<ll> #define vcd vector<double> #define vcs vector<string> #define vpii vector<pii> #define vpll vector<pll> #define stl_sort(v) sort(v.begin(),v.end()) #define stl_rev(v) reverse(v.begin(),v.end()) #define rep(i,x,y) for(ll i=x;i<y;i++) #define scll(x) scanf("%lld",&x) #define pfll(x) printf("%lld",x) #define scd(x) scanf("%llf",&x) #define pfd(x) prinf("%0.5llf\n",x) using namespace std; ll bigmod(ll a,ll b,ll m) { if(b==0)return 1%m; ll x=bigmod(a,b/2,m); x=(x*x)%m; if(b%2==1)x=(x*a)%m; return x; } int main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ //FAST ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,sum1,sum2; ld A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,SUM1,SUM2; cin>>n>>m; cout<<((bigmod(10,n,m*m)-bigmod(10,n,m))/m)%m<<el; return 0; }
#include <bits/stdc++.h> using namespace std; template<typename S, typename T> ostream& operator<<(ostream& out, const pair<S, T> p) { out << "(" << p.first << "," << p.second << ")"; return out; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& v) { for (auto a: v) out << a << " "; return out; } template<typename T> ostream& operator<<(ostream& out, const set<T>& S) { for (auto a: S) cout << a << " "; return out; } template<typename T> ostream& operator<<(ostream& out, const multiset<T>& S) { for (auto a: S) cout << a << " "; return out; } template<typename S, typename T> ostream& operator<<(ostream& out, const map<S, T>& M) { for (auto m: M) cout << "(" << m.first << "->" << m.second << ") "; return out; } template<typename S, typename T> pair<S, T> operator+(pair<S, T> a, pair<S, T> b) { return make_pair(a.first + b.first, a.second + b.second); } template<typename S, typename T> pair<S, T> operator-(pair<S, T> a, pair<S, T> b) { return make_pair(a.first - b.first, a.second - b.second); } long long get_value(vector<long long>& A, long long x) { long long ans = x; auto first = A.begin(); auto last = upper_bound(A.begin(), A.end(), x); x -= (last - first); return x; } long long solve(vector<long long>& A, long long kth) { long long left = 1; long long right = 1; if (get_value(A, left) >= kth) { return left; } while (get_value(A, right) < kth) { right <<= 1; } while (right - left > 1) { long long mid = (left + right) / 2; if (get_value(A, mid) >= kth) { right = mid; } else { left = mid; } } return right; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; vector<long long> A; cin >> n >> q; A.resize(n); for (int i = 0; i < n; i++) { cin >> A[i]; } sort(A.begin(), A.end()); while (q--) { long long k; cin >> k; cout << solve(A, k) << endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <cmath> #include <queue> #include <set> #include <stack> #include <numeric> #include <bitset> #include <math.h> #include <iomanip> #include <sstream> #include <cstdlib> #include <stdio.h> using namespace std; typedef long long ll; //マクロ //forループ関係 //引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか //Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる #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--) //xにはvectorなどのコンテナ #define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい #define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく #define MAX(x) *max_element(ALL(x)) //最大値を求める #define MIN(x) *min_element(ALL(x)) //最小値を求める //定数 #define INF 1000000000000 //10^12:極めて大きい値,∞ #define MOD 1000000007 //10^9+7:合同式の法 #define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用) #define PI 3.14159265359 //πの値 //略記 #define PB push_back //vectorヘの挿入 #define MP make_pair //pairのコンストラクタ #define F first //pairの一つ目の要素 #define S second //pairの二つ目の要素 #define C(x) cout << x << endl //テスト出力 //DP処理用 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; } //関数 //nCkの計算の関数(呼び出しは[cbn]) ll cbn(ll n, ll k) { ll r = 1; for (ll d = 1; d <= k; ++d) { r *= n--; r /= d; } return r; } // a^n mod を計算する ll powmod(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % MOD; a = a * a % MOD; n >>= 1; } return res; } //2つの最大公約数 int gcb(int a, int b){ if(a < b){ int x = a; a = b; b = x; } int amari = a % b; if(amari == 0) return b; return gcb(b, amari); } //3つの最大公約数 int gcb_3(int a, int b, int c){ return gcb(gcb(a, b), c); } /*map<Keyの型, Valueの型> 変数名; queue<型> 変数名; priority_queue<型> 変数名; priority_queue<型, vector<型>, greater<型>> 変数名; vector<vector<要素の型>> 変数名(要素数1, vector<要素の型>(要素数2, 初期値)) sort(変数名.begin(), 変数名.end());*/ /*-----------------------------ここから-----------------------------*/ int main() { cin.tie(0); ios::sync_with_stdio(false); ll a,b,c; cin>>a>>b>>c; cout<<a-b+c; }
#include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #define ordered_set tree<int, null_type, less<int >,rb_tree_tag, tree_order_statistics_node_update> using namespace __gnu_pbds; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define ll long long #define ld long double #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define vii vector<int> #define pii pair<ll,ll> #define vll vector<ll> #define clr(v, d) memset(v,d,sizeof(v)) using namespace std; const int N = 2e5 + 500, mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int x[3]; cin>>x[0]>>x[1]>>x[2]; sort(x,x+3); cout<<x[2]+x[1]<<"\n"; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; struct edge{ ll to, c, d; }; int main(){ int n, m; cin >> n >> m; vector<vector<edge>> G(n); for(int i = 0; i < m; i++){ int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; G[a].push_back((edge){b, c, d}); G[b].push_back((edge){a, c, d}); } priority_queue<P, vector<P>, greater<P>> todo; todo.push(P(0, 0)); vector<ll> dist(n, 1e16); dist[0] = 0; while(!todo.empty()){ P now = todo.top(); todo.pop(); ll pos = now.second; if(pos == n - 1) break; ll t = now.first; if(t > dist[pos]) continue; for(int i = 0; i < G[pos].size(); i++){ edge e = G[pos][i]; ll nxt = e.to; ll c = e.c; ll d = e.d; ll time = sqrt(d); if(t >= time) time = t; ll arrive = c + time + d / (time + 1); if(dist[nxt] > arrive){ dist[nxt] = arrive; todo.push(P(arrive, nxt)); } } } if(dist[n - 1] == 1e16) cout << -1 << endl; else cout << dist[n - 1] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define vi vector<int> #define vvi vector<vector<int>> #define pii pair<int,int> #define vpii vector<pair<int,int>> #define pqueue priority_queue #define umap unordered_map #define uset unordered_set #define bit(s, i) ((1 << i) & s ? 1 : 0) #define bits(x) __builtin_popcount((x)) #define gcd(x, y) __gcd(x, y) #define lcm(x, y) ((x) / gcd(x, y) * (y)) #define inf 4557430888798830399LL #define mst(dp, val) memset(dp, val, sizeof(dp)) #define rep(i, a, b) for (int i = a; i <= b; ++i) #define repd(i, a, b, d) for (int i = a; i <= b; i += d) #define rep_(i, a, b) for (int i = a; i >= b; --i) #define repd_(i, a, b) for (int i = a; i >= b; i -= d) #define mxm(a, b) a = max(a, b) #define mnm(a, b) a = min(a, b) #define pb push_back #define X first #define Y second #define all(x) (x).begin(), (x).end() #define debug(x) cout << #x << "=" << x << endl #define readArr(a, n) for (int i = 1; i <= n; i++) cin >> a[i]; #define printArr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl; #define print(e) cout << (e) << endl const int mod = 1e9 + 7; #define Add(x, val) x = (x + (val)) % mod // #define MULTI_CASES // #define PRE const int N = 1e5 + 5; struct Edge { int v, c, d; }; using Node = pii; //距离,节点编号 vector<Edge> g[N]; void solve() { int n, m; cin >> n >> m; rep(i, 1, m) { int u, v, c, d; cin >> u >> v >> c >> d; g[u].pb({v, c, d}); g[v].pb({u, c, d}); } vi dist(n+1, inf); vi vis(n+1); pqueue<Node, vector<Node>, greater<Node>> q; q.push({0, 1}); dist[1] = 0; while (!q.empty()) { int u = q.top().Y, t = q.top().X; q.pop(); if (vis[u]) continue; vis[u] = 1; for (auto e : g[u]) { int v = e.v, c = e.c, d = e.d; if (t + c + d / (t + 1) < dist[v]) { dist[v] = t + c + d / (t + 1); q.push({dist[v], v}); } rep(x, -2, 2) { int nt = sqrt(d) + x; //出发时间 if (nt < t) continue; if (nt + c + d / (nt + 1) < dist[v]) { dist[v] = nt + c + d / (nt + 1); q.push({dist[v], v}); } } } } print(dist[n] == inf ? -1 : dist[n]); } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); #ifdef PRE pre(); #endif #ifdef MULTI_CASES int t; cin >> t; while (t--) solve(); #else solve(); #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define fst ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define endk cout<<"\n"; typedef long long int ll; typedef long int l; #define l(i,a,b) for(i=a;i<b;i++) #define lo(i,a,b) for(i=a;i>=b;i--) #define s(v) sort(v.begin(),v.end()); #define pb push_back; #define vll vector<ll> #define vint vector<int> #define test() ll t;cin>>t;while(t--){solve();}; #define test1() int t=1;while(t--){solve();}; bool isprime(ll x) { if(x==1) return false; if(x==2 || x==3) return true; if(x%2==0 || x%3==0) return false; for(ll i=5;i*i<=x;i+=6) { if(x%i==0 || x%(i+2)==0) return false; } return true; } ll power(ll x,ll y) { ll ans=1; while(y>0) { if(y&1) ans=ans*x; x=x*x; y=y>>1; } return ans; } int solve() { ll n,i,ans=0; cin>>n; vector<ll> a(n),b(n),c(n); map<ll,ll> m; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n;i++) { cin>>b[i]; } for(i=0;i<n;i++) { cin>>c[i]; c[i]=b[c[i]-1]; m[c[i]]++; } for(i=0;i<n;i++) { ans+=m[a[i]]; } cout<<ans; return 0; } int main() { fst; test1(); return 0; }
#include <iostream> #include <vector> #include <bits/stdc++.h> #define ll long long using namespace std; //take care of long long //fast I/O auto optimizer = []() { // makes I/O fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); return 0; }(); int main() { ll t=1; //cin>>t; while(t--) { ll n; cin>>n; vector<ll> vc(n); ll sum=0; for(ll i=0;i<n;i++) { cin>>vc[i]; sum+=vc[i]; } vector<vector<ll>> dp(sum+1, vector<ll>(n,0)); sort(vc.begin(),vc.end()); ll ans=INT_MAX; for(ll sm=1;sm<=dp.size()-1;sm++) { for(ll i=0;i<n;i++) { if(i>0&&dp[sm][i-1]) { dp[sm][i]=1; } else { if(sm<vc[i]) { dp[sm][i]=0; } else if(sm==vc[i] || (i>=1 && dp[sm-vc[i]][i-1]) ) { dp[sm][i]=1; } else { dp[sm][i]=0; } } } if(dp[sm][n-1]) { //cout<<sm<<endl; ans=min(ans,max(sm,sum-sm)); } } cout<<ans<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; struct Edge { int to, cost; Edge(){}; Edge(int to, int cost): to(to), cost(cost){}; }; using Graph = vector<vector<Edge>>; using P = pair<ll, int>; int main() { cin.tie(0); cin.sync_with_stdio(0); ll n, k; cin >> n >> k; ll ans = 0; ll a, b; for (ll i = 2; i <= 2*n; i++) { if (i - k < 2 || 2*n < i - k) continue; if (i > n) a = 2*n - i + 1; // n - (i - n) + 1 else a = i-2+1; if (i - k > n) b = 2*n -(i - k) + 1; else b = i-k-1; ans += a * b; } cout << ans << endl; }
// Template begins #include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <stdio.h> #include <math.h> #include <iomanip> #include <queue> #include <string.h> #include <string> using namespace std; #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ll long long #define endl "\n" #define lb lower_bound #define ub upper_bound #define fo(i,a,b) for(i=a;i<=b;i++) #define all(v) (v).begin(),(v).end() #define sort0(v) sort(all(v)) #define lbb(a,b,arr,x) lower_bound(arr+a,arr+b+1,x)-arr #define ubb(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-arr #define freq(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-lower_bound(arr+a,arr+b+1,x) typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define d0(x) cout<<(x)<<" " #define d1(x) cout<<(x)<<endl #define d2(x,y) cout<<(x)<<" "<<(y)<<endl #define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl #define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl #define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl #define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl #define max3(a,b,c) max(max((a),(b)),(c)) #define max4(a,b,c,d) max(max((a),(b)),max((c),(d))) #define ss second #define ff first #define pb push_back #define mp make_pair #define printp(x) cout << x.ff << " " << x.ss << endl const ll mod=998244353; #define inf 9999999999999 #define MAXN 100001 // stores smallest prime factor for every number ll inv(ll i){if(i==1) return 1;return (mod-((mod/i)*inv(mod%i))%mod)%mod;} ll gcd(ll a,ll b){if(a==0) return b;return gcd(b,b%a);} inline void fastio(){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);} // Template ends' ll factorial[1000002]; const int MN = 2e5+7; ll val(ll a){ return ((a*(a+1))/2)%mod; } void solve(){ ll n,k; cin>>n>>k; ll a,b; k=abs(k); ll ans=0; for(int i=2;i<=2*n;i++){ a=i; b=a-k; if( b>=2) ans= ans+min(a-1,2*n + 1 - a)*min(b-1,2*n + 1 - b); } cout<<ans<<endl; } int main(){ fastio(); //write code int t=1; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; 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> #define pqll priority_queue<ll> constexpr ll INF = (1ll << 60); constexpr ll mod = 1000000007; // 998244353; 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; } void pt_vvll(vvll v) { rep(i, v.size()) { rep(j, v[0].size()) { cout << v[i][j]; if (j != v[0].size() - 1) cout << " "; else cout << "\n"; } } } void pt_vll(vll v) { rep(i, v.size()) { cout << v[i]; if (i == v.size() - 1) cout << "\n"; else cout << " "; } } vector<ll> sum, ans; void pre_process(vector<ll> &a, vector<ll> &s) { //累積和のvector ll n = (ll)a.size(); s.assign(n + 1, 0); for (ll i = 0; i < n; i++) { s[i + 1] = s[i] + a[i]; } } ll query(ll i, ll j, vector<ll> &s) { //区間[i,j]の和 return (s[j] - s[i]); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; vll A(N); rep(i, N) cin >> A[i]; pre_process(A, sum); reverse(sum.begin(), sum.end()); sum.pop_back(); reverse(sum.begin(), sum.end()); pre_process(sum, ans); // pt_vll(ans); vll MAX_A(N + 1); ll ma = 0; rep(i, N + 1) { chmax(ma, sum[i]); MAX_A[i] = ma; } ll pt = 0; rep(i, ans.size()) { ll plus = (i < N ? MAX_A[i + 1] : 0ll); chmax(pt, ans[i] + max(0ll, plus)); // cout << pt << "\n"; } // pt_vll(ans); cout << pt << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; //↓AC-library使うなら //型関係 using Graph = vector<vector<int>>; typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> pll; typedef priority_queue<int, vector<int>, greater<int>> PQ;//昇順priority_queue typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<ll> vll; typedef vector<P > vpii; typedef vector<pll > vpll; typedef vector<vector<int> > vvi; typedef vector<vector<char> > vvc; typedef vector<vector<string> > vvs; typedef vector<vector<ll> > vvll; //repマクロ #define rep(i, n) for (int i = 0; i < (ll)(n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (int i = a; i < (ll)(b); i++) #define FORD(i, a, b) for (int i = a; i >= (ll)(b); i--) //範囲関係 #define SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define all(x) (x).begin(), (x).end() #define len(x) ((int)(x).size()) #define ZERO(a) memset(a,0,sizeof(a)) //定数 #define inf 1000000000000 #define mod 1000000007 #define MAXR 100000 #define MATHPI acos(-1) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int INF = INT_MAX; const ll LLINF = 1LL<<60; //操作 #define pb push_back #define pf push_front #define mp make_pair #define F first #define S second //数学関係, a<=x<=b bool updown(ll x, ll a, ll b) { return x >= a && x <= b; } 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; } #define SUM(x) accumulate(all(x), 0) #define bit(n) (1LL<<(n)) //chmin, chmax 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;} //小数出力(f...出力したい小数, n...小数点以下の桁数) void printd(double f, int n) { cout << fixed << setprecision(n) << f << endl; } //指定した桁で小数を出力 //YES/NO, Yes/No, yes/no出力 void YES(bool c = true) { cout << (c ? "YES" : "NO") << endl; } //YES(条件式)で記述 void Yes(bool c = true) { cout << (c ? "Yes" : "No") << endl; } //Yes(条件式)で記述 void yes(bool c = true) { cout << (c ? "yes" : "no") << endl; } //yes(条件式)で記述 //除外用 #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define next asdnext #define prev asdprev //if use atcoder library:g++ main.cpp -std=c++17 -I /home/k0gane/kyopro/ac-library //累積和 //https://qiita.com/drken/items/56a6b68edef8fc605821 //point...n+1の容量のリストを作る // //深さ優先...push_front //幅優先...push_backP int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n;cin >> n; vpii p(n); rep(i, n) cin >> p[i].F >> p[i].S; rep(i, n-2){ FOR(j, i+1, n-1){ FOR(k, j+1, n){ if((p[j].F - p[k].F) * (p[i].S - p[j].S) == (p[i].F - p[j].F) * (p[j].S - p[k].S)){cout << "Yes" << endl; exit(0);} } } } cout << "No" << endl; return 0; }
#ifdef MYDEBUG #include "pch.h" #else #include <bits/stdc++.h> #define debug(...) #endif using namespace std; #define int long long #define endl '\n' #define sz(x) (int)((x).size()) #define all(x) (x).begin(),(x).end() #define allr(x) (x).rbegin(),(x).rend() typedef vector<int> vi; typedef map<int, int> mii; typedef pair<int, int> pii; template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; } template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;} template<typename T>void print(T t) { cout << t; } template<typename T, typename... Args>void print(T t, Args... args) { cout << t << " "; print(args...); } template<typename... Args>void printl(Args... args) { print(args...); cout << endl; } template<typename T>void printa(T& a, bool ln = 0) { for (auto& e : a) print(e, ln?"\n":""); if (!ln) cout << endl; } #define printr(...) { printl(__VA_ARGS__); return; } #define inputa(a) for (auto& e : a) cin >> e mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int RAND(int lo = 0, int hi = LLONG_MAX) { return uniform_int_distribution<int>(lo, hi)(rng); } void google() { static int __gtest__ = 1; cout << "Case #" << __gtest__++ << ": "; } const double pi = 3.141592653589793; signed main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cout << setprecision(15) << fixed; int t = 1; //cin >> t; void code(); while (t--) code(); } const int mod = 1e9 + 7; const int mod1 = 998244353; const int maxn = 1e6 + 1; void code() { int n; cin >> n; int sum = 0; for (size_t i = 1; i < maxn; i++) { sum += i; if (sum >= n) printr(i); } }
#include<iostream> #include<vector> #include<utility> #include<algorithm> using namespace std; using ll = long long; const int N = 2e5+5; int bit[N]; int a[N], b[N], c[N]; pair<int,int> pa[N], pb[N]; void add(int p, int v) { while(p < N) { bit[p] += v; p += p&-p; } } int ask(int p) { int ret = 0; while(p) { ret += bit[p]; p -= p&-p; } return ret; } int main() { int n; cin >> n; for(int i = 1; i <= n; ++i) { cin >> a[i]; a[i] += i; pa[i] = {a[i], i}; } for(int i = 1; i <= n; ++i) { cin >> b[i]; b[i] += i; pb[i] = {b[i], i}; } sort(pa + 1, pa + n + 1); sort(pb + 1, pb + n + 1); for(int i = 1; i <= n; ++i) { if(pa[i].first != pb[i].first) { cout << -1 << endl; return 0; } c[pa[i].second] = pb[i].second; } ll res = 0; for(int i = 1; i <= n; ++i) { res += i - ask(c[i]) - 1; add(c[i], 1); } cout << res << endl; return 0; }
//#include<bits/stdc++.h> #include<iostream> #include<string> #include<vector> #include <algorithm> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int,int> //#define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) const int N = 1e6; void rohan_algo() { 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 } int32_t main() { rohan_algo(); int n, x; string s; cin >> n >> x; cin >> s; int k = 0; for (int i = 0; i < n; ++i) { if (s[i] == 'x' && x != 0) { x--; } else if (s[i] == 'o') x++; } cout << x << endl; }
#include<bits/stdc++.h> using namespace std; int main() { int n,x; scanf("%d %d",&n,&x); char S[n+1]; scanf("%s",S); for(int i=0;i<n;i++){ if(S[i]=='o')x++; else x--; x=max(0,x); } printf("%d\n",x); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define fi first #define se second 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 = (1<<30) - 1; const ll LINF = (1LL<<62) - 1; template <typename Monoid> struct SegTree { using F = function<Monoid(Monoid, Monoid)>; const F f; const Monoid unit; int n; vector<Monoid> dat; SegTree(int n_, const F f_, const Monoid unit_) : f(f_), unit(unit_) { n = 1; while (n < n_) n *= 2; // 1-indexed dat.assign(2 * n, unit); } void set(int i, const Monoid &x) { dat[i + n] = x; } void build() { for (int i = n - 1; i >= 0; --i) { dat[i] = f(dat[2 * i], dat[2 * i + 1]); } } void update(int i, const Monoid &x){ i += n; dat[i] = x; while (i >>= 1){ dat[i] = f(dat[2 * i], dat[2 * i + 1]); } } // [a, b) 非再帰 Monoid query(int a, int b) { Monoid l = unit, r = unit; for (a += n, b += n; a < b; a >>= 1, b >>= 1) { if (a & 1) l = f(l, dat[a++]); if (b & 1) r = f(dat[--b], r); } return f(l, r); } inline Monoid operator[] (int i){ return dat[i + n]; } }; // set(i, v)した後にbuild()でO(n) // 区間和 : SegTree<int> seg(n, [](int a, int b){ return a + b; }, 0); // 区間min : SegTree<int> seg(n, [](int a, int b) {return min(a, b); }, INF); int main(){ int n, m, k; cin >> n >> m >> k; vector<int> a(k); int modoru[100010] = {}; for (int i = 0; i < k; ++i) { cin >> a[i]; modoru[a[i]] = 1; } int cnt = 0; for (int i = 0; i <= n; ++i) { if (modoru[i]) cnt++; else cnt = 0; if (cnt >= m) { cout << -1 << endl; return 0; } } SegTree<double> seg(n + m + 10, [](double a, double b){ return a + b; }, 0); SegTree<double> seg2(n + m + 10, [](double a, double b){ return a + b; }, 0); for (int i = n-1; i >= 0; --i) { double a = seg.query(i + 1, i + m + 1) / m + 1.0; double b = seg2.query(i + 1, i + m + 1) / m; if (modoru[i]) { seg2.update(i, 1.0); } else { seg.update(i, a); seg2.update(i, b); } } // x = seg[0] + seg2[0] * x cout << fixed << setprecision(15) << seg[0] / (1 - seg2[0]) << endl; }
#include<bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; // typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define ll long long #define scn(n) scanf("%d",&n) #define lscn(n) scanf("%lld",&n) #define lpri(n) printf("%lld",n) #define pri(n) printf("%d",n) #define pln() printf("\n") #define priln(n) printf("%d\n",n) #define lpriln(n) printf("%lld\n",n) #define rep(i,init,n) for(int i=init;i<n;i++) #define pb push_back #define mp make_pair #define F first #define S second #define gcd __gcd #define inf INT_MAX #define ninf INT_MIN #define inf INT_MAX #define linf LLONG_MAX #define lninf LLONG_MIN const int mod = 1e9 + 7; const int N = 1e5 + 4; typedef long double ld; int solve() { int n, m, k; scn(n); scn(m); scn(k); pair<ld, ld> p = {0.0, 0.0}; vector<int> mark(n + 1, 0); rep(i, 0, k) { int val; scn(val); mark[val]++; } int cnt = 0; rep(i, 1, n) { if(mark[i]) cnt++; else { if(cnt >= m) { pri(-1); return 0; } cnt = 0; } } if(cnt >= m) { pri(-1); return 0; } vector<pair<ld, ld>> v(n + m + 1, {0.0, 0.0}); for(int i = n - 1; i >= 0; i--) { p.F -= v[i + m + 1].F; p.S -= v[i + m + 1].S; if(mark[i]) { v[i].F = 1.0; } else { v[i].F = p.F / (ld)m; v[i].S = p.S / (ld)m; v[i].S += 1.0; } // cout << v[i].F << " " << v[i].S << endl; if(i == 0) cout << fixed << setprecision(10) << v[i].S / (1.0 - v[i].F); p.F += v[i].F; p.S += v[i].S; } return 0; } int main() { int t = 1; //scn(t); while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i,a,b) for(ll i=a;(i)<(b);++(i)) #define RFOR(i,a,b) for(ll i=a;(i)>=(b);--(i)) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,n,0) #define ALL(v) v.begin(), v.end() #define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)), v.end()) #define BIT(n) (1LL<<(n)) #define DEBUG(a) cerr << #a << " = " << a << endl const double PI = acos(-1); const int inf = 1001001001; const int mod = (int)1e9+7; //const ll inf = 1e15; //const ll mod = 1e9+7; int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int t2 = 0; void swp(int q, vector<int>& t, vector<int>& a, vector<int>& b, string& s) { if (t[q] == 1) { if (t2%2 == 1) { if (a[q] ) a[q] = (a[q] + s.size() / 2) % s.size(); if (a[q] == 0) a[q] = s.size(); b[q] = (b[q] + s.size() / 2) % s.size(); if (b[q] == 0) b[q] = s.size(); } swap(s[a[q]-1], s[b[q]-1]); } else { t2++; } } int main() { int n; string s; cin >> n >> s; int q; cin >> q; vector<int> t(q), a(q), b(q); REP(i, q) { cin >> t[i] >> a[i] >> b[i]; } REP(i, q) { swp(i, t, a, b, s); } if (t2%2 == 1) { REP(i, s.size() / 2) { swap(s[i], s[i+s.size()/2]); } } cout << s << endl; }
#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cstdio> #include<stdio.h> #include<cmath> #include<math.h> #include<vector> #include<set> #include<queue> #include<map> #include<sstream> #include<iomanip> #define forn(i,n) for(int (i)=0;i<(n);i++) #define pb push_back #define mp make_pair using namespace std; typedef pair<int,int>pii; typedef long long ll; typedef pair<ll,ll> pll; const int MAXN=100005; const int INF=2147483647; const ll LINF=9223372036854775807; const int dx[]={1,-1,0,0},dy[]={0,0,1,-1}; int n,x,cnt; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>x; x*=100; forn(i,n) { int a,b; cin>>a>>b; cnt+=a*b; if(cnt>x) { cout<<i+1; return 0; } } cout<<-1; return 0; }
#include <bits/stdc++.h> using namespace std; long long k, res; int main() { cin>>k; for(int i=1; i<=k; i++) { for(int j=1; j<=k/i; j++) { if(i*j>k) break; res+=k/(i*j); } } cout<<res; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int typedef pair<int, int> pii; #define f(i,a,b) for(int i=a;i<b;i++) #define fo(i,a,b) for(int i = a; i<=b;i+=1) #define rf(i,a,b) for(int i=a;i>=b;i--) #define vll vector<int> #define sz(a) int(a.size()) #define all(v) v.begin(), v.end() #define pb push_back #define pf push_front #define MAXN 100010 #define MOD 1000000007 #define mod 998244353 #define deb(x) cout << '>' << #x << ':' << x << endl; #define debug(x) cout << '>' << #x << ':' << x << " "; #define nl '\n' #define MS(x,y) fill_n(*x, sizeof x / sizeof **x, y); #define mem(x,y) memset(x,y,sizeof(x)); #define INF 1000000000000000000 #define IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ void solve(){ int k; cin >> k; int ans = 0; fo(i, 1, k){ if(i*i*i <= k){ ans++; } else { break; } } fo(i, 1, k){ if(i*i > k)break; fo(j, i+1, k){ if(i*i*j <= k){ ans+=3; } else { break; } } } fo(i, 1, k){ fo(j, i+1, k){ if(i*j*j <= k){ ans+=3; } else { break; } } } fo(i, 1, k){ f(j, i+1, k){ if(i*j > k)break; fo(x, j+1, k){ if(i*j*x <= k){ ans += 6; } else break; } } } cout << ans << nl; return ; } main() { IO; int T = 1; // cin >> T; for(int c=1; c <= T; c++){ solve(); // cout<<"Case #"<<c<<": "; } return 0; }
#include <sys/time.h> #include <iostream> #include <iomanip> #include <algorithm> #include <utility> #include <cmath> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <deque> #include <utility> #include <cctype> #include <cstdlib> #include <typeinfo> using namespace std; using ll = long long; using i_i = pair<int, int>; #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define mkp(a, b) make_pair(a, b) #define MOD 1000000007LL #define INF 1e9 int x[17], y[17], z[17]; int dp[1 << 17][17]; int distance(int from, int to) { return abs(x[to] - x[from]) + abs(y[to] - y[from]) + max(0,z[to] - z[from]); } int main() { int n; cin >> n; rep(i, n) { cin >> x[i] >> y[i] >> z[i]; } int max_bit = 1 << n; //cout << "max_bit:" << max_bit << endl; //cout << "rep start" << endl; rep(i, max_bit) { rep(j , n) { dp[i][j] = INF; } } //cout << "rep end" << endl; // v, dp queue< i_i > que; for(int i = 1; i < n; i++) { int bit = 1 << i; que.push(mkp(i,bit)); dp[bit][i] = distance(0, i); } while(!que.empty()) { int from = que.front().first; int bit = que.front().second; que.pop(); rep(i,n) { if ((bit | (1 << i)) == 1) continue; int cur_bit = bit | (1 << i); int nd = dp[bit][from] + distance(from, i); if(dp[cur_bit][i] > nd) { dp[cur_bit][i] = nd; que.push(mkp(i,cur_bit)); } } } cout << dp[max_bit - 1] [0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i = (m); i <= (n); i++) #define zep(i,m,n) for(ll i = (m); i < (n); i++) #define rrep(i,m,n) for(ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; ll a[n]; zep(i, 0, n)cin >> a[i]; ll b[n]; zep(i, 0, n)cin >> b[i]; ll ans = 0; zep(i, 0, n){ ans += a[i] * b[i]; } print((ans == 0)? "Yes" : "No") return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define pll pair<long long, long long> #define pb push_back #define ff first #define ss second #define YES printf("YES\n") #define NO printf("NO\n") #define sci(x) scanf("%d", &x) #define LL_INF (1LL << 62) #define INF (1 << 30) #define SetBit(x, k) (x |= (1LL << k)) #define ClearBit(x, k) (x &= ~(1LL << k)) #define CheckBit(x, k) (x & (1LL << k)) #define mod 1000000007 #define N 200005 int main(){ int t; cin >> t; while(t--){ string s; cin >> s; int f = 0; string ss = "atcoder"; for(int i = 0; i < 7; i++){ if(i>=s.size()) break; if(s[i]<ss[i]) break; if(s[i]==ss[i]){ if(i==6 && s.size()>ss.size()) f = 1; continue; } if(s[i]>ss[i]){ f = 1; break; } } if(f){ cout << 0 << "\n"; continue; } for(int i = 0; i < s.size(); i++){ if(s[i]!='a'){ if(s[i]>'t') cout << i-1 << "\n"; else cout << i << "\n"; f = 1; break; } } if(!f) cout << -1 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } __fast_io; inline int64_t trisum(int32_t n) { if (n < 0) return 0; int64_t nll = n; return (nll * (nll + 1)) / 2; } int main() { int32_t T, L, R; cin >> T; while (T--) { cin >> L >> R; cout << trisum(R - 2 * L + 1) << '\n'; } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) using ll = long long; map<ll,ll> mp; ll x, y; ll dfs(ll n) { if(n == x) return 0; else if(n < x) return x - n; else if(mp[n] != 0) return mp[n]; ll res = n - x; ll mod[1] = {n % 2}; ll div[1] = {2}; ll nex; rep(i,1) { nex = (n - mod[i])/div[i]; res = min(res,dfs(nex)+1+mod[i]); nex++; if(mod[i] != 0) res = min(res,dfs(nex)+1+(div[i]-mod[i])); } return mp[n] = res; } int main(){ cin >> x >> y; if(x >= y) { cout << x-y << endl; return 0; } cout << dfs(y) << endl; return 0; }
#ifdef ONLINE_JUDGE #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #else #define _GLIBCXX_DEBUG #endif #include <iostream> #include <string> #include <vector> #include <iomanip> #include <algorithm> #include <queue> #include <map> #include <stack> #include <cmath> #include <functional> #include <set> #include <numeric> #include <bitset> #include <cassert> using std::cerr; using std::cin; using std::cout; using std::pair; using std::string; using std::vector; //region #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, a, b) for (int i = (b); i >= (b); --i) #define rep(i, n) for (unsigned i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define repr(i, n) for (int i = (n)-1; i >= 0; --i) #define repr1(i, n) for (int i = (n); i > 0; --i) #define all(vec) (vec).begin(), (vec).end() #define mset(v, n) std::memset((v), n, sizeof(v)) #define BIT(n) (1LL << (n)) #define each(i, v) for (auto&& i : (v)) #define unless(f) if(!(f)) using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; using vint = vector<int>; using vlong = vector<ll>; using vstr = vector<string>; using pii = pair<int, int>; using pil = pair<int, ll>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpil = vector<pil>; using vpll = vector<pll>; using vvi = vector<vint>; using vvl = vector<vlong>; template<typename T> using VV = vector<vector<T>>; vector<string> split(const string &s, const string &delim) { vector<string> res; uint pos = 0; while (true) { const size_t found = s.find(delim, pos); if (found < 0) { res.push_back(s.substr(pos)); break; } res.push_back(s.substr(pos, found - pos)); pos = found + delim.size(); } return res; } template<typename T> string join(vector<T> &vec, const string &sep) { size_t size = vec.size(); if (!size) return ""; std::stringstream ss; for (int i = 0; i < size - 1; i++) ss << vec[i] << sep; ss << vec[size - 1]; return ss.str(); } template<typename T> std::istream &operator>>(std::istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template<typename Iter> inline void print(const Iter &first, const Iter &last, const std::string &d = " ", bool endline = true) { cout << *first; for (Iter iter = first + 1; iter < last; ++iter) { cout << d << *iter; } if (endline) cout << "\n"; } constexpr ll powmod(ll a, ull b, uint p) { ll res = 1; while (b > 0) { if (b % 2) res = res * a % p; a = a * a % p; b >>= 1u; } return res; } constexpr ll pow(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2) res = res * a; a = a * a; b >>= 1u; } return res; } template<class T> bool chmax(T &a, const T &b) { return a < b && (a = b, true); } template<class T> bool chmin(T &a, const T &b) { return a > b && (a = b, true); } template<typename T> void bsort(vector<T> &v) { std::sort(v.begin(), v.end()); } template<typename T> void rsort(vector<T> &v) { std::sort(v.begin(), v.end(), std::greater<T>()); } struct iii { iii() { cin.tie(nullptr); cout.tie(nullptr); std::ios::sync_with_stdio(false); cout << std::fixed << std::setprecision(16); } } init; //endregion void solve(); int main() { int t = 1; // scanf("%d", &t); // cin >> t; rep(i, t) solve(); return 0; } ll X, Y; std::map<ll, ll> memo; ll calc(ll n) { if (memo[n]) return memo[n]; if (n <= X) return X - n; if (n - X == 1) return 1; ll res = n - X; ll l = n / 2 * 2, r = (n + 1) / 2 * 2; chmin(res, std::abs(l - n) + calc(l / 2) + 1); chmin(res, std::abs(r - n) + calc(r / 2) + 1); return memo[n] = res; } void solve() { cin >> X >> Y; cout << calc(Y) << std::endl; }
//author : ainunsy1 #include <bits/stdc++.h> #define forn(i,a,b) for(int i=a; i<=b; i++) #define ford(i,a,b) for(int i=a; i>=b; i--) #define fi first #define se second #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; //before solving problems, please read line 15-25 //jgn lupa dicomment line 17-28 nya const int MX=2e5+5; void solve(){ int n,a,b=0; cin>>n; forn(i,1,n){ cin>>a; a-=10; b+=max(a,0); } cout<<b<<"\n"; /* pikirin baik baik soalnya, dibuat santai aja kalo ngestuck, coba cari approach lain ngestucknya sampe bikin depresi? ingat! Allah itu dekat mintalah pertolongan kepada-Nya kamu cuma manusia biasa kamu lemah, ga usah sok jago yg buat km bs AC-in soal itu ya karena bantuan Allah Allah itu powerful, apapun bisa Dia lakukan minta pertolongan kepada-Nya ya semangat terus :) */ } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t=1; // cin>>t; forn(i,1,t){ solve(); } }
#include <bits/stdc++.h> using namespace std; int zh(string s) { int ans=0; for (int i=0; i<s.size(); i++) { ans = ans*10+(s[i]-'0'); } return ans; } int main() { string str; int a[10]={0}; cin >> str; for(int i=0; i<str.size(); i++) { a[str[i]-'0']++; } if(str.size() == 1) { if (str == "8") { cout << "Yes"; } else { cout << "No"; } return 0; } if(str.size() == 2) { if(zh(str)%8==0) { cout<<"Yes"; return 0; } else { swap(str[0],str[1]); if(zh(str)%8==0) { cout << "Yes"; return 0; } else { cout << "No"; return 0; } } } for (int i=112; i<=992; i+=8) { int b, s, g; b = i/100; s = i/10%10; g = i%10; a[b]--; a[s]--; a[g]--; if (a[b]>=0 && a[s]>=0 && a[g]>=0) { cout << "Yes"; return 0; } else { a[b]++; a[s]++; a[g]++; } } cout << "No"; 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; vector<string> f(ll N) { if(N == 1) { return {"AB"}; } auto v = f(N - 1); vector<string> ret; for(int i = 0; i < v.size(); i++) { auto tmp = v[i] + v[i]; ret.push_back(tmp); } for(int i = 0; i < v.size(); i++) { auto tmp = v[i]; for(auto &c: tmp) { if(c == 'A') c = 'B'; else c = 'A'; } tmp += v[i]; ret.push_back(tmp); } string S; for(int i = 0; i < (1 << N) / 2; i++) S.push_back('A'); for(int i = 0; i < (1 << N) / 2; i++) S.push_back('B'); ret.push_back(S); return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; auto ans = f(N); cout << ans.size() << endl; for(auto s : ans) cout << s << endl; return 0; }
#include <iostream> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <set> #include <queue> #include <map> using namespace std; typedef long long int ll; typedef pair <int,int> pii; typedef pair<ll,ll> pll; /*bool compare_function(const pair<int, int> a, const pair<int, int> b) { return a.first < b.first; }*/ // use case //sort(V.begin(), V.end(), compare_function); /*struct compare { bool operator ()(const pair<int, int> &a, const pair<int, int> &b) { if (a.second-a.first==b.second-b.first) { return a.first>b.first; } return a.second-a.first < b.second-b.first;} }; priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/ /*void print1(vector <int> X) { for (int i=0; i<X.size(); i++) { cout<<X[i]<<" "; } cout<<endl; return; } void print2(vector <vector <int>> X) { for (int i=0; i<X.size(); i++) { for (int j=0;j<X[i].size();j++) { cout<<X[i][j]<<" ";} cout<<endl;} return; }*/ int sk(int x) { int a=0; while (x>0) { a+=x%2; x=x/2; } return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; int k=1; for (int i=0;i<n;i++) {k*=2;} cout<<k-1<<endl; string s="A"; for (int i=0;i<k-1;i++) {s+='A';} vector <string> A(k-1,s); for (int i=1;i<k;i++) { for (int j=0;j<k;j++) { int x=i&j; int v=sk(x); //cout<<i<<" "<<j<<" "<<x<<" "<<v<<endl; if (v%2==0) {A[i-1][j]='B';} } } for (int i=0;i<k-1;i++) {cout<<A[i]<<endl;} return 0; }
#include <bits/stdc++.h> using namespace std; #define flush cout.flush using ll = long long; using ull = unsigned long long; using ld = long double; using pl = pair<ll, ll>; const ll INF = 1e9 + 7; const ll mod = 1e9 + 7; const ll mod2 = 998244353; const ld eps = 1e-9; const ld PI = acos(-1); ll cur = -1; ll calc(vector<ll> &a, vector<ll> &b) { ll res = INF * INF; vector<pl> z; for (ll i = 0; i < a.size(); ++i) { if (a[i] == cur) { cur = -1; continue; } z.push_back({a[i], 0}); } for (ll i = 0; i < b.size(); ++i) { z.push_back({b[i], 1}); } ll last0 = -1, last1 = -1; sort(z.begin(), z.end()); for (ll i = 0; i < z.size(); ++i) { if (z[i].second == 0) { if (last1 != -1 && abs(z[i].first - last1) < res) { res = min(res, abs(z[i].first - last1)); } last0 = z[i].first; } else { if (last0 != -1) { res = min(res, abs(z[i].first - last0)); } last1 = z[i].first; } } cur = last0; return res; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vector<ll> r, g, b; for (ll i = 0; i < 2 * n; ++i) { ll a; cin >> a; char c; cin >> c; if (c == 'R')r.push_back(a); if (c == 'B')b.push_back(a); if (c == 'G')g.push_back(a); } if (r.size() % 2 == 0 && b.size() % 2 == 0) { cout << "0\n"; return 0; } ll res; if (r.size() % 2 == 0) { res = calc(g, b); ll tmp = calc(r, g); res = min(res, tmp + calc(r, b)); cur = -1; tmp = calc(r, b); res = min(res, tmp + calc(r, g)); cur = -1; } else if (b.size() % 2 == 0) { res = calc(r, g); ll tmp = calc(b, g); res = min(res, tmp + calc(b, r)); cur = -1; tmp = calc(b, r); res = min(res, tmp + calc(b, g)); cur = -1; } else { res = calc(r, b); ll tmp = calc(g, r); res = min(res, tmp + calc(g, b)); cur = -1; tmp = calc(g, b); res = min(res, tmp + calc(g, r)); cur = -1; } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { int N; scanf("%d", &N); vector<vector<ll>> b(3, vector<ll>(0)); ll a; char c; rep(i, 2 * N) { cin >> a >> c; if (c == 'R') b[0].push_back(a); if (c == 'G') b[1].push_back(a); if (c == 'B') b[2].push_back(a); } rep(i, 3) sort(b[i].begin(), b[i].end()); vector<ll> d(3, 1e18); rep(i, 3) { rep(j, b[i].size()) { int k = lower_bound(b[(i + 1) % 3].begin(), b[(i + 1) % 3].end(), b[i][j]) - b[(i + 1) % 3].begin(); if (k > 0) d[i] = min(d[i], b[i][j] - b[(i + 1) % 3][k - 1]); if (k < b[(i + 1) % 3].size()) d[i] = min(d[i], b[(i + 1) % 3][k] - b[i][j]); } } //rep(i, 3) cout << d[i] << endl; if ((b[0].size() % 2 == 0) && (b[1].size() % 2 == 0) && (b[2].size() % 2 == 0)) { printf("0\n"); return 0; } if (b[0].size() % 2 == 0) { printf("%lld\n", min(d[1], d[0] + d[2])); } if (b[1].size() % 2 == 0) { printf("%lld\n", min(d[2], d[0] + d[1])); } if (b[2].size() % 2 == 0) { printf("%lld\n", min(d[0], d[2] + d[1])); } }
#include<iostream> #include<string> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<algorithm> #include<numeric> #include<cmath> #include<iomanip> #include<regex> using namespace std; #define int long long const int mod=1e9+7; bool pri[1000001],squf[1000001],even[1000001]; signed main(){ int l,r; cin>>l>>r; for(int i=2;i<=r;i++) pri[i]=squf[i]=even[i]=1; for(int i=2;i<=r;i++){ if(!pri[i]) continue; for(int j=i;j<=r;j+=i){ if(j!=i) pri[j]=0; even[j]=!even[j]; } for(int j=i*i;j<=r;j+=i*i) squf[j]=0; } int ans1=0,ans2=0; for(int g=2;g<=r;g++){ if(!squf[g]) continue; int a=r/g-(l-1)/g; a=a*(a-1)/2; if(even[g]) ans1-=a; else ans1+=a; } for(int g=max(2ll,l);g<=r;g++) ans2+=r/g-1; cout<<(ans1-ans2)*2<<endl; }
#include <bits/stdc++.h> #define loop(s, e, i) for (int i = s; i < e; ++i) #define print(s) cout << s << endl; #define DIV 1000000007 #define ll long long using namespace std; /* 浮動小数点の入力 cout << fixed << setprecision(9) << endl; */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; string S; cin >> N >> S; int A=0, T=0, C=0, G = 0; vector<int> AV(N+1), TV(N+1), CV(N+1), GV(N+1); loop(0, S.size(), i) { switch(S[i]) { case 'A': A += 1; break; case 'T': T += 1; break; case 'C': C += 1; break; case 'G': G += 1; break; } AV[i+1] = A; TV[i+1] = T; CV[i+1] = C; GV[i+1] = G; } int count = 0; loop(0, S.size()+1, i) { loop(i+2, S.size()+1, j) { int ad = AV[j] - AV[i]; int td = TV[j] - TV[i]; int cd = CV[j] - CV[i]; int gd = GV[j] - GV[i]; if (ad == td && cd == gd){ count++; } } } print(count); }
#include <bits/stdc++.h> using namespace std; struct point { long long x, y, z; long long dist(const point &other) { return abs(x - other.x) + abs(y - other.y) + max(0LL, other.z - z); } }; const long long LLINF = (long long) 1e18; long long dp[22][1 << 22]; int main() { int N; cin >> N; vector<point> p(N); for (int i = 0; i < N; i++) { cin >> p[i].x >> p[i].y >> p[i].z; } for (int i = 0; i < 22; i++) for (int j = 0; j < (1 << 22); j++) { dp[i][j] = LLINF; } dp[0][0] = 0; for (int b = 0; b < (1 << N); b++) { for (int cur = 0; cur < N; cur++) { if (dp[cur][b] == LLINF) continue; for (int nxt = 0; nxt < N; nxt++) { if (b & (1 << nxt)) continue; const auto nxt_bit = b | (1 << nxt); dp[nxt][nxt_bit] = min(dp[nxt][nxt_bit], dp[cur][b] + p[cur].dist(p[nxt])); } } } cout << dp[0][(1 << N) - 1] << endl; }
/* Jai Shree Ram 🚩🚩🚩 */ #include "bits/stdc++.h" #define ll long long int #define oo 1000000000000000000 #define forr(i,n) for(int i=0;i<n;i++) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(x) x.begin(),x.end() #define unique(v) sort(all(v)); v.resize(distance(v.begin(),unique(all(v)))) #define eb emplace_back #define FF first #define SS second #define mem(a,v) memset(a,v,sizeof(a)) #define pb push_back #define popcount(x) __builtin_popcount(x) using namespace std; template<typename T> ostream &operator<<(ostream &output,const vector<T> &v){ if(v.empty()) return output; for(int i=0;i<v.size()-1;i++) output << v[i] <<" "; output << v.back(); return output; } template<typename T> istream &operator>>(istream &input,vector<T> &v){ for(auto &i: v) cin >> i; return input; } const int N = 3005 , mod = 1e9 + 7; int best[N][N]; int n; ll dp[N][N]; ll get(int i,int k){ if( i > n) return 0; else if(best[i][k] == n+1) return 1; else if(best[i][k] == n+2) return 0; else if(dp[i][k]!=-1) return dp[i][k]; else{ ll ans = 0; ans += get(best[i][k] , k + 1); ans %= mod; ans += get(best[i][k] , k); ans %= mod; return dp[i][k] = ans; } } void __sol(){ cin >> n; vector<ll> v(n); cin >> v; forr(i,n+3) forr(j,n+3) best[i][j] = n+2; mem(dp,-1); for(int k=1;k<=n;k++){ vector<int> seen(k,-1); ll sum = 0; seen[0] = n + 1; for(int j=n;j>=1;j--){ sum += v[j-1]%k; sum %= k; if(seen[sum]!=-1) best[j][k] = seen[sum]; seen[sum] = j; } } cout << get(1,1)<<"\n"; return; } int main(){ fastio; int tc=1; // cin >> tc; while(tc--) __sol(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define all(x) (x).begin(),(x).end() ll comb_count(ll n, ll r) { if (r > n) return 0; if (r * 2 > n) r = n - r; if (r == 0) return 1; ll result = n; for (ll i = 2; i <= r; ++i) { result *= (n - i + 1); result /= i; } return result; } void solve(long long A, long long B, long long K) { K--; string ans; rep(i, A + B) { if (0 < A) { if (K < comb_count(A + B - 1, B)) { ans += "a"; A--; } else { ans += "b"; K -= comb_count(A + B - 1, B); B--; } } else { ans += "b"; B--; } } cout << ans << endl; } int main() { long long A; std::scanf("%lld", &A); long long B; std::scanf("%lld", &B); long long K; std::scanf("%lld", &K); solve(A, B, K); return 0; }
#include <iostream> #include <math.h> #include <iomanip> #include <vector> #include <unordered_map> #include <algorithm> #include <string> #include <set> #include <numeric> #include <queue> #include <map> using namespace std; int main(){ int n,k,m; int total = 0; cin >> n >> k >> m; for(int i = 0;i<n-1;i++){ int x; cin >> x; total += x; } for(int i = 0;i<=k;i++){ //cout << ((double)total+i)/n<< endl; if(((double)total+i)/n >=m){ cout << i; return 0; } } cout << -1; }
/* { ###################### # Author # # Gary # # 2021 # ###################### */ #include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen("check.out","w",stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; //inline int read(){ // int x=0; // char ch=getchar(); // while(ch<'0'||ch>'9'){ // ch=getchar(); // } // while(ch>='0'&&ch<='9'){ // x=(x<<1)+(x<<3)+(ch^48); // ch=getchar(); // } // return x; //} const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ int fa[1000+12]; int h,w; char maze[1001][1001]; char maze2[1001][1001]; int root(int now){ return fa[now]=(fa[now]==now? now:root(fa[now])); } int solve(){ rb(i,1,h) fa[i]=i; maze[1][1]=maze[1][w]=maze[h][1]=maze[h][w]='#'; rb(j,1,w){ vector<int> have; rb(i,1,h){ if(maze[i][j]=='#'){ have.PB(i); } } rep(i,have.size()){ if(i>0){ fa[root(have[i])]=root(have[i-1]); } } } int cnt=0; rb(i,1,h){ cnt+=(root(i)==i); } return cnt-1; } int main(){ cin>>h>>w; if(min(h,w)<=2){ cout<<0<<endl; return 0; } rb(i,1,h) rb(j,1,w) cin>>maze[i][j]; int rest=solve(); swap(h,w); rb(i,1,h) rb(j,1,w) maze2[i][j]=maze[j][i]; rb(i,1,h) rb(j,1,w) maze[i][j]=maze2[i][j]; check_min(rest,solve()); cout<<rest<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int H, W, ans = 0; char S[15][15]; int main() { cin >> H >> W; for(int i = 0; i < H; i++) for(int j = 0;j < W;j++) cin >> S[j][i]; for(int i = 0; i < H - 1; i++) { for(int j = 0; j < W - 1; j++) { int b = 0, w = 0; if(S[j][i] == '#') b++; else w++; if(S[j+1][i] == '#') b++; else w++; if(S[j][i+1] == '#') b++; else w++; if(S[j+1][i+1] == '#') b++; else w++; if((b == 3 && w == 1) || (b == 1 && w == 3)) ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> #include <unistd.h> using namespace std; #define pb push_back #define mp make_pair #define int long long int #define Sort(a) sort(a.begin(),a.end()) #define FOR(i,a) for(int i=0;i<a;i++) #define print(arr,n) for(int i=0;i<n;i++)cout<<arr[i]<<" " #define scan(arr,n) for(int i=0;i<n;i++)cin>>arr[i] #define MOD(x) x%1000000007 #define MAX 1e9 #define MIN -1e9 #define ff first #define ss second #define vi vector<int> #define minimum(a) *min_element(a.begin(), a.end()) #define maximum(a) *max_element(a.begin(), a.end()) #define mem(x,val) memset((x),(val),sizeof(x)) #define sqr(x) x*x #define sp <<" " #define nl cout<<"\n" #define pii pair<int,int> #define startTime clock_t start, end; start = clock(); #define endTime end = clock(); double time_taken = double(end - start) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);cout << " sec " << endl; #define all(a) a.begin(),a.end() int check(int n,int m,vector<pii> a,vector<pii> c,int k,int pos,vi ans){ // cout<<pos sp; if(pos>=k){ int c=0; vi dp(n+1,0); for(auto i:ans){ dp[i]=1; // cout<<i sp; } // nl; for(int i=0;i<m;i++){ if(dp[a[i].ff]!=0 && dp[a[i].ss]!=0)c++; } return c; } vi temp1,temp2; for(auto i:ans)temp1.pb(i); for(auto i:ans)temp2.pb(i); temp1.pb(c[pos].ff); temp2.pb(c[pos].ss); // print(temp1,temp1.size()); // nl; // print(temp2,temp2.size()); // nl; return max(check(n,m,a,c,k,pos+1,temp1),check(n,m,a,c,k,pos+1,temp2)); } int32_t main(){ // startTime // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin>>n>>m; vector<pii> a(m); FOR(i,m){ cin>>a[i].ff>>a[i].ss; } int k; cin>>k; vector<pii> c(k); FOR(i,k){ cin>>c[i].ff>>c[i].ss; } int ans=check(n,m,a,c,k,0,{}); cout<<ans; // endTime return 0; }
#include<iostream> #include<string> #include<cmath> #include<algorithm> //max,min #include<iomanip> //setprecision,tuple #include<vector> //vector #include<numeric> //iota #include<map> //map #include<set> //set #include<sstream> //stringstream(hex 16,dec 10,oct 8,bilset 2) #include<list> //list using namespace std; using lint = long long; #define INF 2147483647 #define PI acos(-1) #define rep(i, n) for(int i = 0; i < (int)(n); i++) int main(){ int n,m;cin>>n>>m; vector<pair<int,int>> pa; rep(i,m){ int a,b;cin>>a>>b; a--;b--; pa.push_back(make_pair(a,b)); } int k;cin>>k; vector<pair<int,int>> p; rep(i,k){ int c,d;cin>>c>>d; c--;d--; p.push_back(make_pair(c,d)); } int ans=0; rep(s,1<<k){ set<int> tmp; rep(i,k){ if(s>>i&1)tmp.insert(p[i].first); else tmp.insert(p[i].second); } int cnt=0; for(auto x:pa){ int tmpa=x.first,tmpb=x.second; if(tmp.count(tmpa)&&tmp.count(tmpb))cnt++; } ans=max(cnt,ans); } cout<<ans<<endl; return 0; }
#include<iostream> #include<algorithm> #include<map> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<set> #define ll long long #define pb push_back #define forn(i,x,n) for(int i=x;i<=n;++i) #define forr(i,x,n) for(int i=n;i>=x;--i) #define lson (rt<< 1) #define rson (rt<< 1 | 1) #define gmid ((l+r)>> 1 ) using namespace std; const int maxn=20000050; int a[maxn]; int main() { int n; cin>>n; forn(i,1,n) cin>>a[i]; sort(a+1,a+n+1); bool f=true; forn(i,1,n) if(a[i]==a[i-1]||a[i]<1||a[i]>n) f=false; if(f) cout<<"Yes"<<endl; else cout<<"No"<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define Vi vector<int> #define for_(i, a, b) for(int i = (a);i < (b);++i) #define ford_(i, a, b) for(int i = (b)-1;i >= (a);--i) #define rep(i, n) for_(i, 0, n) #define repd(i, n) rfor_(i, 0, n) int main(){ int N,Q; cin >> N >> Q; vector<ll> A(N); rep(i,N){ cin >> A.at(i); } vector<ll> low(N); //C rep(i,N){ low.at(i) = A.at(i) - (i+1); } while(Q--){ ll K; cin >> K; const int idx = lower_bound(low.begin(), low.end(), K) - low.begin(); if(idx == N){ cout << A[N-1] + (K - low[N-1]) << '\n'; } else { cout << A[idx] - (low[idx] - K+1) << '\n'; } } }
#include <iostream> #include <string> using namespace std; #define UKWN_RTO 0.9 #define SMPL_EXEC_CNT 150 int si, sj, ti, tj, ch[29][30]={0}, cw[30][29]={0}, len, tc=0; double lh[29][30]={0}, lw[30][29]={0}, pl[30][30], tmp, tl=0; string pp[30][30]; bool gl(int pi, int pj) { return pj > tj; } double ll(int pi, int pj) { if (tc == 0) return 1; if (cw[pi][pj-1] == 0) return tl / tc * UKWN_RTO; return lw[pi][pj-1] / cw[pi][pj-1]; } bool gr(int pi, int pj) { return pj < tj; } double lr(int pi, int pj) { if (tc == 0) return 1; if (cw[pi][pj] == 0) return tl / tc * UKWN_RTO; return lw[pi][pj] / cw[pi][pj]; } bool gu(int pi, int pj) { return pi > ti; } double lu(int pi, int pj) { if (tc == 0) return 1; if (ch[pi-1][pj] == 0) return tl / tc * UKWN_RTO; return lh[pi-1][pj] / ch[pi-1][pj]; } bool gd(int pi, int pj) { return pi < ti; } double ld(int pi, int pj) { if (tc == 0) return 1; if (ch[pi][pj] == 0) return tl / tc * UKWN_RTO; return lh[pi][pj] / ch[pi][pj]; } void calc2(int pi, int pj) { while (true) { while(true) { if (gu(pi, pj)) { tmp = pl[pi][pj] + lu(pi, pj); if (tmp < pl[pi-1][pj]) { pl[pi-1][pj] = tmp; pp[pi-1][pj] = pp[pi][pj]; pp[pi-1][pj] += 'U'; } } else if (gd(pi, pj)) { tmp = pl[pi][pj] + ld(pi, pj); if (tmp < pl[pi+1][pj]) { pl[pi+1][pj] = tmp; pp[pi+1][pj] = pp[pi][pj]; pp[pi+1][pj] += 'D'; } } if (gl(pi, pj)) { tmp = pl[pi][pj] + ll(pi, pj); pj--; if (tmp < pl[pi][pj]) { pl[pi][pj] = tmp; pp[pi][pj] = pp[pi][pj+1]; pp[pi][pj] += 'L'; } } else if (gr(pi, pj)) { tmp = pl[pi][pj] + lr(pi, pj); pj++; if (tmp < pl[pi][pj]) { pl[pi][pj] = tmp; pp[pi][pj] = pp[pi][pj-1]; pp[pi][pj] += 'R'; } } else break; } pj = sj; if (gu(pi, pj)) pi--; else if (gd(pi, pj)) pi++; else break; } } void calc3() { bool f[30][30]; for (int j=0; j<30; j++) for (int k=0; k<30; k++) f[j][k] = true; for (int i=0; i<900; i++) { int pi, pj; double d = 1e100; for (int j=0; j<30; j++) for (int k=0; k<30; k++) if (f[j][k] && pl[j][k] < d) { pi = j; pj = k; d = pl[j][k]; } f[pi][pj] = false; if (pi > 0) { tmp = pl[pi][pj] + lu(pi, pj); if (tmp < pl[pi-1][pj]) { pl[pi-1][pj] = tmp; pp[pi-1][pj] = pp[pi][pj]; pp[pi-1][pj] += 'U'; } } if (pi < 29) { tmp = pl[pi][pj] + ld(pi, pj); if (tmp < pl[pi+1][pj]) { pl[pi+1][pj] = tmp; pp[pi+1][pj] = pp[pi][pj]; pp[pi+1][pj] += 'D'; } } if (pj > 0) { tmp = pl[pi][pj] + ll(pi, pj); if (tmp < pl[pi][pj-1]) { pl[pi][pj-1] = tmp; pp[pi][pj-1] = pp[pi][pj]; pp[pi][pj-1] += 'L'; } } if (pj < 29) { tmp = pl[pi][pj] + lr(pi, pj); if (tmp < pl[pi][pj+1]) { pl[pi][pj+1] = tmp; pp[pi][pj+1] = pp[pi][pj]; pp[pi][pj+1] += 'R'; } } } } int main() { for (int i=0; i<1000; i++) { cin >> si >> sj >> ti >> tj; for (int j=0; j<30; j++) for (int k=0; k<30; k++) { pp[j][k] = "X"; pl[j][k] = 1e100; } pl[si][sj] = 0; pp[si][sj] = ""; if (i < SMPL_EXEC_CNT) calc2(si, sj); else calc3(); string path = pp[ti][tj]; cout << path << endl; cin >> len; len /= path.length(); tc++; tl += len; for (int j=0; j<path.length(); j++) switch (path[j]) { case 'U': si--; ch[si][sj]++; lh[si][sj] += len; break; case 'D': ch[si][sj]++; lh[si][sj] += len; si++; break; case 'L': sj--; cw[si][sj]++; lw[si][sj] += len; break; case 'R': cw[si][sj]++; lw[si][sj] += len; sj++; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); #define FOR(i,s,n) for(int i = (s); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for(int i = (n); i >= 0; i--) #define ALL(n) (n).begin(), (n).end() #define RALL(n) (n).rbegin(), (n).rend() #define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n'; #define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n'; #define OUT(n) cout << (n) << '\n'; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; const int HW = 30; const int Q = 1000; void out(string s) { cout << s << '\n'; cout.flush(); } struct route { pii xy; string r; }; int main(void) { REP(i,Q) { pii s,t; cin >> s.first >> s.second >> t.first >> t.second; cin.ignore(); string ans = ""; if (s.first < t.first) ans += string(t.first-s.first,'D'); else ans += string(s.first-t.first,'U'); if (s.second < t.second) ans += string(t.second-s.second,'R'); else ans += string(s.second-t.second,'L'); out(ans); int score; cin >> score; cin.ignore(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; ++i) #define ll long long template <typename T> bool PN(T x){ if (x <= 1) return false; if (x == 2) return true; for (int i = 2; i < sqrt(x) + 1; i++) if (x % i == 0) return false; return true;} const ll MOD = 1e9+7; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; long long Comb(int n, int i){long long ans = 1; if(i>n || i < 0) return 0; if(i == 0 || i == n) return 1; else {for(int j = 1; j <= i; ++j){ans *=(n+1-j); ans /= j; ans %= MOD;} }return ans;} template<typename T> T gcd(T a, T b){if(b == 0) return a;else return gcd(b, a%b);} template<typename T> T lcm(T a, T b){if(b > a) swap(a, b); T g = gcd(a, b);return a / g * b;} void solve(){ int n; cin >> n; int a[n]; int sum = 0; for(int i = 0; i < n; ++i) { cin >> a[i]; sum += a[i]; } int dp[n + 1][sum + 1] = {}; for(int i = 1; i <= n; ++i) { for(int j = 0; j <= sum; j++) { if(j-a[i-1] >= 0) dp[i][j] = max(dp[i-1][j-a[i-1]] + a[i-1], dp[i][j]); dp[i][j] = max(dp[i-1][j], dp[i][j]); } } //cout << dp[n][sum/2] << endl; cout << sum-dp[n][sum/2] << endl; } int main(void){ solve(); return 0; }
#include <bits/stdc++.h> using namespace std; //const long nPrime = 1000000007; //const long nPrime = 998244353; class BIT{ public: BIT(int n){ m_n = 1; while(m_n < n){ m_n *= 2; } m_vi.resize(m_n, 0); } ~BIT(){ } void Add(const int i, long x){ if(i >= m_n || i < 0){ return; } m_vi[i] += x; //FIXME int iTmp = i; while(iTmp != 0){ iTmp -= (iTmp & (-iTmp)); m_vi[iTmp] += x; //FIXME } } long GetNum(const int i){ if(i >= m_n || i < 0){ return -1; //error } long nBase = m_vi[i]; int iFin = i & (-i); int iTmp = 1; long nSum = 0; while(iTmp < m_n){ if(iTmp == iFin){ break; } nSum += m_vi[i+iTmp]; //FIXME iTmp *= 2; } return (nBase - nSum); } //[i,j)のsum //i>jの処理を考えること long GetSum(int i, int j){ if(i > m_n || j > m_n){ return -1; //error } if(i != 0){ return (GetSum(0, j) - GetSum(0, i)); } int iTmp = j; long nSum = 0; while(iTmp < m_n){ nSum += m_vi[iTmp]; //FIXME if(iTmp == 0){ break; } iTmp += (iTmp & (-iTmp)); } return m_vi[0] - nSum; } private: int m_n; vector<long> m_vi; }; //正数列の反転数 値が大きいときor負の値を含むときは座圧とかしておくこと long GetInversion(const vector<int> &viNum){ int n = viNum.size(); int nMax = 0; for(int i = 0; i < n; i++){ nMax = max(viNum[i], nMax); } BIT zBit(nMax+1); long nReturn = 0; for(int i = n-1; i >= 0; i--){ nReturn += zBit.GetSum(0,viNum[i]); zBit.Add(viNum[i], 1); } return nReturn; } int main() { long n; cin >> n; map<long, vector<long>> miA, miB; for(long i = 0; i < n; i++){ long a; cin >> a; auto itr = miA.find(a+i); if(itr == miA.end()){ vector<long> viTmp(1,i); miA.insert(make_pair(a+i, viTmp)); } else { itr->second.push_back(i); } } for(long i = 0; i < n; i++){ long a; cin >> a; auto itr = miB.find(a+i); if(itr == miB.end()){ vector<long> viTmp(1,i); miB.insert(make_pair(a+i, viTmp)); } else { itr->second.push_back(i); } } vector<int> viNum(n); for(auto itrA = miA.begin(); itrA != miA.end(); itrA++){ auto itrB = miB.find(itrA->first); if(itrB == miB.end()){ cout << -1 << endl; return 0; } if(itrA->second.size() != itrB->second.size()){ cout << -1 << endl; return 0; } for(int i = 0; i < itrA->second.size(); i++){ viNum[itrA->second[i]] = itrB->second[i]; } } cout << GetInversion(viNum) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAXN = 2E5 + 5; const int MOD = 998244353; int n, k; LL sum[301], C[301][301], a[MAXN]; template <class T> void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar(); x *= f; } template <class T, class... Args> void read(T& x, Args&... args) { read(x), read(args...); } LL qpow(LL a, LL b, LL MOD) { LL ans = 1, base = a; while (b) { if (b & 1) ans = ans * base % MOD; b >>= 1, base = base * base % MOD; } return ans; } void add(LL& a, LL b) { a = ((a + b) % MOD + MOD) % MOD; } void init() { for (int i = 0; i <= 300; i++) C[i][0] = 1; for (int i = 1; i <= 300; i++) for (int j = 1; j <= 300; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; sum[0] = n; for (int i = 1; i <= n; i++) { LL tmp = 1; for (int x = 1; x <= k; x++) { tmp = tmp * a[i] % MOD; add(sum[x], tmp); } } } signed main() { read(n, k); for (int i = 1; i <= n; i++) read(a[i]); init(); for (int x = 1; x <= k; x++) { LL ans = 0; for (int i = 0; i <= x; i++) add(ans, C[x][i] * (sum[i] * sum[x - i] % MOD - sum[x]) % MOD); printf("%lld\n", ans * qpow(2, MOD - 2, MOD) % MOD); } return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #define rep(i,N) for(int i=0;i<(int)N;i++) static inline void PUT(char c) { static char buf[1<<15],*ptr=buf; if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c; } static inline int IN(void) { int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();} while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x; } static inline void OUT(long a) { if(a<0)PUT('-'),a=-a; int d[40],i=0;do{d[i++]=a%10;}while(a/=10); while(i--){PUT(d[i]+48);}PUT('\n'); } const int MOD=998244353,inv2=(MOD+1)/2; int fact[301],inv[301]; static inline int MPow(int a,int b){return b?1l*MPow(1l*a*a%MOD,b>>1)*(b&1?a:1)%MOD:1;} static inline int comb(const int n,const int k) { if(k<0||n<k){return 0;}return 1l*fact[n]*inv[n-k]%MOD*inv[k]%MOD; } int main(void) { int N=IN(),K=IN(),A,sum[301]={0},temp=1,total=0; fact[0]=1;rep(i,K)fact[i+1]=1l*fact[i]*(i+1)%MOD; inv[K]=MPow(fact[K],MOD-2); rep(i,K)inv[K-1-i]=1l*inv[K-i]*(K-i)%MOD; rep(i,N){A=IN();temp=1;rep(j,K+1){sum[j]=(sum[j]+temp)%MOD;temp=1l*temp*A%MOD;}} rep(i,K) { total=0; rep(j,i+2){total=(1l*total+1l*comb(i+1,j)*(1l*sum[j]*sum[i+1-j]%MOD)%MOD)%MOD;} total=(1l*total-1l*sum[i+1]*MPow(2,i+1)%MOD)%MOD; OUT(1l*((1l*total+MOD)%MOD)*inv2%MOD); } }
#include <iostream> #include <vector> #include <cstdlib> #include <set> #include <iostream> #include <iomanip> #include<stdio.h> #include<climits> #include<vector> #include<algorithm> #include <cmath> #include<map> #include<deque> #include<set> #include<queue> #include<cassert> #include<functional> #include<numeric> #include<regex> using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } // 入力 int main() { long long n,m,t,b; cin >> n >> m >> t; bool flag = true; long long d[m][2]; for(int i=0;i<m;i++){ cin >> d[i][0] >> d[i][1]; } b=n-d[0][0]; //cout << b << endl; if(b<=0) flag=false; b+=d[0][1]-d[0][0]; if(b>=n)b=n; //cout << b << endl; for(int i=1;i<m;i++){ b-=d[i][0]-d[i-1][1]; if(b<=0){ flag=false; break; } //cout << b << endl; b+=d[i][1]-d[i][0]; if(b>=n)b=n; } //cout << b << endl; b-=t-d[m-1][1]; if(b<=0){ flag=false; } //cout << b << endl; if(flag==false){ cout << "No" << endl; } else cout << "Yes" << endl; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include<bits/stdc++.h> #define itn int #define ll long long #define pir pair<ll,ll> #define yes {puts("Yes");} #define no {puts("No");} using namespace std; ll read(){ ll a=0,b=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')b=-1;c=getchar();} while(c>='0'&&c<='9')a=a*10+c-48,c=getchar(); return a*b; } int n,m; struct edge{ int v,nx; ll c,d; }e[200005]; int cnt,hd[100005]; ll dis[100005]; bool vis[100005]; struct node{ int i; ll w; }; bool operator<(node a,node b){ return a.w>b.w; } ll clac(ll t,ll c,ll d){ if(!d)return c; ll sq=sqrt(d+2),ans=c+d/(t+1); for(ll st=t;st<=t+3;st++){ ans=min(ans,d/(st+1)+st-t+c); } if(t+1<sq){ for(ll st=t;st<=t+30;st++){ ans=min(ans,d/(st+1)+st-t+c); } for(ll st=max(t,sq-30);st<=sq+5;st++){ ans=min(ans,d/(st+1)+st-t+c); } } return ans; } void dij(){ memset(dis,0x3f,sizeof dis); dis[1]=0; priority_queue<node>q; q.push(node{1,0}); while(!q.empty()){ node t=q.top();q.pop(); if(vis[t.i])continue; vis[t.i]=1; for(int i=hd[t.i];i;i=e[i].nx){ int v=e[i].v; ll w=clac(dis[t.i],e[i].c,e[i].d); if(dis[v]>dis[t.i]+w){ dis[v]=dis[t.i]+w; q.push(node{v,dis[v]}); } } } } int main(){ n=read(),m=read(); while(m--){ int a=read(),b=read(),c=read(),d=read(); e[++cnt]=edge{b,hd[a],c,d},hd[a]=cnt; e[++cnt]=edge{a,hd[b],c,d},hd[b]=cnt; } dij(); if(dis[n]>1e18)cout<<-1; else cout<<dis[n]<<'\n'; return 0; }
//BY: YASH JAIN, CF: BitSane #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; #define May_the_fork_be_with_you ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define rall(v) v.rbegin(),v.rend() #define forn(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i) #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define deb(x) cout<<#x<<": "<<x<<endl; #define debn(x) cout<<#x<<": "<<x<<" "; #define MOD 1000000007 #define mod 998244353 #define INF 1e18 #define ll long long #define f first #define s second #define pb push_back #define eb emplace_back #define endl "\n" #define int long long #define N 100005 #define sq(x) ((x)*(x)) typedef vector<int> vi; ll gcd(ll a, ll b) {if (!b)return a; return gcd(b, a % b);} ll power(ll x, ll y, ll p = INF) {ll res = 1; x %= p; while (y > 0) {if (y & 1)res = (res * x) % p; y = y >> 1; x = (x * x) % p;} return res;} // Do Not use power when calculating powers of 2 (its inefficient) vector<vi> adj; vi c, res; void dfs(int u, int p, set<int>& st) { bool foo = false; if (st.find(c[u]) == st.end()) { foo = true; res.eb(u); } st.insert(c[u]); for (auto x : adj[u]) { if (x == p) continue; dfs(x, u, st); } if (foo) st.erase(c[u]); } void solve() { int n; cin >> n; adj.resize(n + 1); c.resize(n + 1); for1(i, n) cin >> c[i]; forn(i, n - 1) { int u, v; cin >> u >> v; adj[u].eb(v); adj[v].eb(u); } set<int> st; dfs(1, -1, st); sort(all(res)); for (int x : res) cout << x << endl; } int32_t main() { #ifndef ONLINE_JUDGE // for geting input form input.txt freopen("input.txt", "r", stdin); // for wrting output to output.txt freopen("output.txt", "w", stdout); #endif May_the_fork_be_with_you int t = 1; int x = 1; // cin >> t; cout << fixed << setprecision(12); while (t--) { // cout << "Case #" << x << ": "; solve(); x++; } #ifndef ONLINE_JUDGE cerr << "Time Taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; #endif }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; 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 int MOD = 1e9+7; const ll LINF = 1e18; #define REP(i,n) for(int i=0;i<(n);++i) #define COUT(x) cout<<(x)<<"\n" #define COUT16(x) cout << fixed << setprecision(16) << (x) << "\n"; ll a[100010]; int main(){ //input ll n;cin >> n; vector<ll>b(n),c(n); ll t,p; REP(i,n){ cin >> t; a[t]++; } REP(i,n){ cin >> b[i]; } REP(i,n){ cin >> p; --p; c[i] = p; } //計算 ll count = 0; for(int i=0;i<n;i++){ count += a[b[c[i]]]; } COUT(count); }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define all(x) (x).begin(),(x).end() using ll = long long; using P = pair<ll, ll>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, -1, 0, 1}; string char_to_string(char val) { return string(1, val); } int char_to_int(char val) { return val - '0'; } char inverse_char(char c) { if(isupper(c)) return tolower(c); else return toupper(c); } 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; } struct edge { ll to, cost; }; const int INF = 1001001001; int main() { int H, W; cin >> H >> W; vector<string> a(H); REP(i, H) cin >> a[i]; // ワープ使う時、Sから最も近いワープマスからワープするのが最善。 // BFS中に最初にたどり着いたマスから全部のワープマスへワープするようにすると計算量が落ちる vector<vector<int>> dist(H, vector<int>(W, INF)); queue<P> q; REP(i, H) { REP(j, W) { // Sを探す if(a[i][j] == 'S') { q.emplace(i, j); //push_backでpairを使ってpushする代わりに、pair型の値をpushしてくれる関数 dist[i][j] = 0; } } } vector<vector<P>> warps(256); REP(i, H) { REP(j, W) { warps[a[i][j]].emplace_back(i, j); } } while(!q.empty()) { // 今から見る座標を取得する int i = q.front().first; int j = q.front().second; // queueから先頭を取り除く q.pop(); // ゴールなら今いる位置の数字を出力する if(a[i][j] == 'G') { cout << dist[i][j] << endl; return 0; } //上下4方向を探索する REP(v, 4) { int ni = i + dx[v]; int nj = j + dy[v]; if(ni < 0 || ni >= H || nj < 0 || nj >= W) continue; // 範囲外ならループをスキップ if(a[ni][nj] == '#') continue; // 壁ならループをスキップ if(dist[ni][nj] != INF) continue; dist[ni][nj] = dist[i][j] + 1; q.emplace(ni, nj); } if(islower(a[i][j])) { // 英字小文字なら... for(P p: warps[a[i][j]]) { // warpできる文字の位置を全探索する int ni = p.first; int nj = p.second; if(dist[ni][nj] != INF) continue; dist[ni][nj] = dist[i][j] + 1; q.emplace(ni, nj); } warps[a[i][j]].clear(); // 一度使ったワープ先は空にする } } cout << -1 << endl; }
//BELIEVE IN YOURSELF //next_permutation #include<bits/stdc++.h> #define ll long long int #define ld long double #define pb push_back #define pob pop_back #define vi vector<ll> #define mp make_pair #define sz size() #define rep1(i,n) for(ll i=1;i<=n;i++) #define rep(i,n) for(ll i=0;i<n;i++) #define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define maxx 100005 #define sec second #define fi first #define be begin() #define en end() #define sortv(v) sort(v.begin(),v.end()); #define sortr(v) sort(v.rbegin(),v.rend()); #define w() ll _;cin>>_;while(_--) #define edge pair<ll,ll> #define pq priority_queue<ll> #define mod 1000000007 #define Endl "\n" #define cy cout<<"YES"<<endl; #define cn cout<<"NO"<<endl; #define lb(v,x) std::lower_bound(v.begin(),v.end(),x) #define ub(v,x) std::upper_bound(v.begin(),v.end(),x) using namespace std; ll dx[]={-1,0,1,0}; ll dy[]={0,-1,0,1}; ll n,m; bool isvalid(ll x,ll y) { if(x>=0 && y>=0 && x<n && y<m) { return true; } return false; } int main() { fast; cin>>n>>m; ll i,j; string s; vector<string>g; for(i=0;i<n;i++) { cin>>s; g.pb(s); } ll a[n+1][m+1]; // ll vis[n+1][m+1]; ll inf=1e17; ll sx=0,sy=0,ex=0,ey=0; vector<vector<pair<ll,ll> > >tp(26); for(i=0;i<n;i++) { for(j=0;j<m;j++) { a[i][j]=inf; if(g[i][j]=='S') { a[i][j]=0; sx=i; sy=j; } if(g[i][j]=='G') { ex=i; ey=j; } if(g[i][j]>='a' && g[i][j]<='z') { char c=g[i][j]; tp[c-'a'].pb(mp(i,j)); } } } queue<pair<ll,ll> >q; q.push(mp(sx,sy)); while(!q.empty()) { ll x,y; pair<ll,ll>now =q.front(); q.pop(); x=now.fi; y=now.sec; for(i=0;i<4;i++) { ll nx,ny; pair<ll,ll>next = mp(x+dx[i],y+dy[i]); nx=next.fi; ny=next.sec; if(isvalid(nx,ny)) { if(g[nx][ny]!='#' && a[nx][ny]==inf) { a[nx][ny]=a[x][y]+1; q.push(mp(nx,ny)); } } } char c=g[x][y]; if(islower(c)) { for(auto next:tp[c-'a']) { ll nx,ny; nx=next.fi; ny=next.sec; if(a[nx][ny]==inf) { a[nx][ny]=a[x][y]+1; q.push(mp(nx,ny)); } } tp[c-'a'].clear(); } } ll ans=a[ex][ey]; if(ans==inf) { cout<<-1<<Endl; } else { cout<<ans<<endl; } 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>; int main() { int n; cin >> n; double ans = 1; rep(i,1,n) { ans *= i+1; ans /= i; if (i != n-1) ans += 1; } printf("%.10lf\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for(i = 0; i < n; i++) bool dfs(vector<int> &p, int swapFlag, int dep, vector<int> &ans) { int n = min(4, (int)p.size()); int i; rep(i, n) if (p[i] != i) break; if (i == n) return true; if (dep >= n * n) return false; rep(i, n - 1) { if (i % 2 != swapFlag % 2) { continue; } swap(p[i], p[i + 1]); ans.push_back(i); bool res = dfs(p, !swapFlag, dep + 1, ans); if (res) return true; swap(p[i], p[i + 1]); ans.pop_back(); } return false; } vector<int> solve(vector<int> p) { int n = p.size(); int i, j; int cnt = 0; vector<int> ret; for (i = n; i >= 5; i--) { rep(j, i) if (p[j] == i - 1) break; int pos = j; if (pos == i - 1) continue; if (pos % 2 != cnt % 2) { rep(j, i - 1) { if (j != pos && j + 1 != pos && j % 2 == cnt % 2) { break; } } swap(p[j], p[j + 1]); cnt++; ret.push_back(j); } for (j = pos; j + 1 < i; j++) { swap(p[j], p[j + 1]); cnt++; ret.push_back(j); } } dfs(p, cnt % 2, 0, ret); return ret; } int main() { int T, i; cin >> T; while (T--) { int n; cin >> n; vector<int> p(n); rep(i, n) { cin >> p[i]; p[i]--; } vector<int> ans = solve(p); cout << ans.size() << endl; rep(i, ans.size()) { cout << ans[i] + 1; if (i + 1 < ans.size()) cout << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define TRACE(x) x #define WATCH(x) TRACE( cout << #x" = " << x << endl) #define PRINT(x) TRACE(printf(x)) #define WATCHR(a, b) TRACE( for(auto c = a; c != b;) cout << *(c++) << " "; cout << endl) #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());}) #define rep(i, a, b) for(int i = (a); (i) < (b); ++(i)) #define trav(a, x) for(auto& a : x) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) static_cast<int>(x.size()) #define mp make_pair #define fi first #define se second using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<ll>; using pii = pair<int, int>; using pll = pair<ll, ll>; void buff() { ios::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll MOD = 1e9 + 7; inline ll pow_mod(ll a, ll b, ll mod = MOD) { ll res = 1; a %= mod; assert(b >= 0); for(;b;b>>=1) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; } return res; } template<typename T> inline void remin(T& x, const T& y) { x = min(x, y); } template<typename T> inline void remax(T& x, const T& y) { x = max(x, y); } 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 << ')'; } int main() { buff(); long long n; cin >> n; long long ans = n; long long cur_pot = 2; long long cur_exp = 1; while(cur_pot <= n) { long long res = (n / cur_pot) + cur_exp + (n % cur_pot); ans = min(res, ans); cur_pot <<= 1ll; cur_exp++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<n;i++) using ll = long long; int main() { ll N; cin >> N; vector<ll> diff(N,0); ll sumA = 0; REP(i,N) { ll a,b; cin>>a>>b; sumA += a; diff[i] = (a+b)+a; } sort(diff.begin(),diff.end(),[](ll a,ll b){ return a>b; }); REP(i,N) { sumA -= diff[i]; if(sumA<0){ cout<<i+1<<endl; return 0; } } return 0; }
//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; //#define int long long typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} template<class t> using vc=vector<t>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.fi<<","<<p.sc<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<class T> void g(T &a){ cin >> a; } template<class T> void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 1000000007;//998244353 mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); template<class T> void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } return res; } #define sz (1<<19) int par[sz],ran[sz]; void init(){ for(int i=0;i<sz;i++) par[i] = i, ran[i] = 0; } int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); } void unite(int x,int y){ x = find(x); y = find(y); if(x==y) return; if(ran[x] < ran[y]) par[x] = y; else{ par[y] = x; if(ran[x] == ran[y]) ran[x]++; } } bool same(int x,int y){ return find(x)==find(y); } typedef __int128 L; void solve(){ map<int,int>M; int n; cin>>n; rep(i,n){ int a;cin>>a;M[a%200]++; } ll ans =0; for(auto a:M) ans += 1LL*a.b*(a.b-1)/2;o(ans); } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); int t; t = 1; //cin >> t; while(t--) solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int num; cin >> num; array<ll, 200> v; for(auto &i : v){ i = 0; } for(int i=0;i<num;i++){ int current;; cin >> current; v[current%200] ++; } ll ans = 0; for(auto &i : v){ ans += (i * (i-1)) / 2; } cout << ans << endl; return 0; }
#include <stdio.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int a = ri(); int b = ri(); int c = a + b; if (c >= 15 && b >= 8) puts("1"); else if (c >= 10 && b >= 3) puts("2"); else if (c >= 3) puts("3"); else puts("4"); return 0; }
#include <bits/stdc++.h> using namespace std;using ll=long long; using vl=vector<ll>;using vi=vector<int>; using vs=vector<string>;using vc=vector<char>; using vvl=vector<vl>;using pl=pair<ll,ll>; using vvc=vector<vc>;using vd=vector<double>; using vpl=vector<pl>;using vb=vector<bool>; #define rep(i,r) for(ll i=0;i<(r);i++) #define Rep(i,l,r) for(ll i=(l);i<(r);i++) #define print(n) cout<<(n)<<endl #define all(a) (a).begin(),(a).end() #define sortp(d) sort(all(d)) //1234 #define sortm(d) sort(d.rbegin(),d.rend()) //4321 /*-----------順列生成----------------- do { }while(next_permutation(all(A))); */ //'A'65,'a'97 //cout<<fixed<<setprecision(9)<<; ll gcd(ll a,ll b) {if(b==0)return a;else return gcd(b,a%b);} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll modpow(ll a,ll n,ll mod) {ll res=1;while(n>0) {if(n&1)res=res*a%mod;a=a*a%mod;n >>= 1;}return res;} ll modinv(ll a,ll mod){return modpow(a, mod - 2, mod);} ll p=0,q=0,r=0; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; ll inf=1000000000000; //10^12:∞ ll mod=1000000007; //10^9+7:合同式の法 struct edge{ll to,cost;}; const double pi = acos(-1); int main(){ ll a,b;cin>>a>>b; if(b>=8&&a+b>=15)print(1); else if(b>=3&&a+b>=10)print(2); else if(a+b>=3)print(3); else print(4); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = 1e5 + 5; const int M = 2000 + 5; const int OO = 1e9; const int mod = 1e9 + 7; string s[3]; set<char> st; int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif for (auto & i : s) { cin >> i; for(auto & c : i) { st.insert(c); } } if(st.size() > 10) { cout << "UNSOLVABLE\n"; } else { char dummy = 0; bool found = false; while(st.size() < 10) { st.insert(dummy++); } vector<char> v; v.reserve(st.size()); for(auto & i : st) { v.push_back(i); } do { map<char, int> val; for (int i = 0; i < v.size(); ++i) { val[v[i]] = i; } string n[3]; bool hasLeadingZeros = false; for (int i = 0; i < 3; ++i) { n[i].reserve(s[i].size()); for(auto & c : s[i]) { n[i].push_back(val[c] + '0'); } hasLeadingZeros |= (n[i].front() == '0'); } if(!hasLeadingZeros) { ll a = stoll(n[0]); ll b = stoll(n[1]); ll c = stoll(n[2]); if(a + b == c) { for(auto & i : n) { cout << i << '\n'; } found = true; break; } } } while(next_permutation(v.begin(), v.end())); if(!found) { cout << "UNSOLVABLE\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "../debug.h" #else #define debug(x...) 141 #endif #define forn(i,x,n) for(int i = x; i < n; ++i) #define forit(it, m) for(auto it = m.begin(); it!=m.end(); ++it) typedef long long ll; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,mm,x,y,ans=1e9; char c; cin>>n>>mm; map<char,vector<pair<int,int>>> m; set<pair<int,int>> edges; map<pair<int,int>,int> dist; forn(i,0,mm){ cin>>x>>y>>c; edges.insert({x-1,y-1}); edges.insert({y-1,x-1}); m[c].push_back({x-1,y-1}); m[c].push_back({y-1,x-1}); } map<pair<int,int>,vector<pair<int,int>>> adj; for(auto x:m) for(auto [a,b]:x.second) for(auto [c,d]:x.second) adj[{a,c}].push_back({b,d}); queue<pair<int,int>> q; q.push({0,n-1}); dist[{0,n-1}]=0; while(q.size()){ auto [u,v]=q.front(); q.pop(); if(edges.count({u,v})) ans=min(ans,1+dist[{u,v}]); if(u==v) ans=min(ans,dist[{u,v}]); for(auto [a,b]:adj[{u,v}]) if(!dist.count({a,b})) q.push({a,b}), dist[{a,b}]=dist[{u,v}]+2; } cout<<(ans==1e9?-1:ans)<<'\n'; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define F first #define S second #define ALL(a) a.begin(),a.end() #define mp make_pair #define pb push_back #define FOR(i,a,b) for(int i=a;i<b;i++) #define FORN(i,a,b) for(int i=a;i<=b;i++) #define FORB(i,a,b) for(int i=a;i>=b;i--) #define INT_MAXI 2147483647 #define INT_MINI -2147483648 #define REV(a) reverse(a.begin(),a.end()) #define NP(a) next_permutation(a.begin(),a.end()) typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef vector<int> vi; typedef vector<ll> vl; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll s,p; cin>>s>>p; for(ll i=1;i*i<=p;i++){ ll temp=p/i; if(temp+i==s){ cout<<"Yes\n"; return 0; } } cout<<"No\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define eb emplace_back #define fi first #define se second #define fr(i,j,k) for(i = j; i < (k); i++) #define all(x) x.begin(), x.end() #define el '\n' #define remax(a,b) a = max(a, b) #define remin(a,b) a = min(a, b) #define sz(x) int32_t(x.size()) typedef long double ld; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vpi; // -------------------------------------------------------------------- const pii dxy[] = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} }; const int mod = 1e9 + 7; const int INF = 2e18; const ld EPS = 1e-9; const int NN = 1e5 + 2; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } 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) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } // #define TRACE #ifdef TRACE #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) #endif //============================================================================ void solve() { int i = 0, j = 0, k = 0, n = 0, m = 0; cin>>n>>m; vi a(n), ops(m); fr(i, 0, n) cin >> a[i]; fr(i, 0, m) cin >> ops[i]; sort(all(a)); vi fwd(n, -INF); vi rev(n, -INF); for(i = 1; i<n; i += 2){ fwd[i] = a[i]-a[i-1]; if(i>1) fwd[i] += fwd[i - 2]; } for(i = n-2; i >= 0; i -= 2) { rev[i] = a[i+1]-a[i]; if(i<n-2) rev[i] += rev[i + 2]; } debug(fwd); debug(rev); int ans = INF; fr(j, 0, m) { i = upper_bound(all(a), ops[j]) - a.begin(); // insert ops at i-1 if(i % 2 == 0) { int t = i > 0 ? fwd[i - 1] : 0; int g = a[i]-ops[j] + (i < n-1 ? rev[i+1] : 0); g += t; remin(ans, g); debug(ans, i, ops[j]); } else { int t = i > 1 ? fwd[i-2] : 0; int g = abs(ops[j] - a[i-1]) + (i < n ? rev[i] : 0); g += t; remin(ans, g); debug(ans, i, ops[j]); } } cout << ans << el; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int T = 1; // cin >> T; for(int tc = 1; tc <= T; tc++) { solve(); } return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<set> #include<map> #include<math.h> #include<unordered_set> #include<unordered_map> #include<cassert> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; #define mp make_pair #define pb push_back #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define rrep(i,l,r) for(int i=l;i<=r;i++) #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define all(x) x.begin(),x.end() #define unq(x) sort(all(x));x.erase(unique(all(x)),x.end()) #define mod 1000000007 //#define mod 998244353 #define ad(a,b) a=(a+b)%mod; #define mul(a,b) a=a*b%mod; void readv(vector<ll> &a,ll n){ rep(i,n){ ll x; cin>>x; a.push_back(x); } } void outv(vector<ll> &a,ll n){ rep(i,n){ if(i>0)cout<<" "; cout<<a[i]; } cout<<"\n"; } ll po(ll x,ll y){ ll res=1; for(;y;y>>=1){ if(y&1)res=res*x%mod; x=x*x%mod; } return res; } ll gcd(ll a,ll b){ return (b?gcd(b,a%b):a); } #define FACMAX 200010 ll fac[FACMAX],inv[FACMAX],ivf[FACMAX]; void initfac(){ fac[0]=ivf[0]=inv[1]=1; for(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod; for(ll i=1;i<FACMAX;i++){ if(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod; ivf[i]=po(fac[i],mod-2); } } ll P(ll n,ll k){ if(n<0||n<k)return 0; return fac[n]*ivf[n-k]%mod; } ll C(ll n,ll k){ if(k<0||n<k)return 0; return fac[n]*ivf[n-k]%mod*ivf[k]%mod; } ll H(ll n,ll k){ return C(n+k-1,k); } #define N 110 #define M 100010 ll n,m,a[M],b[M],c[N]; vector<ll> g[N]; ll d[N]; void dfs(ll x,ll dep){ if(d[x]>=0)return; d[x]=dep; for(auto y:g[x]){ dfs(y,dep+1); } } int main(){ cin.tie(0); ios::sync_with_stdio(0); cin>>n>>m; rep(i,m){ cin>>a[i]>>b[i]; a[i]--,b[i]--; } rep(i,n)cin>>c[i]; rep(i,m){ if(c[a[i]]==c[b[i]]){ g[a[i]].pb(b[i]); g[b[i]].pb(a[i]); } } rep(i,n)d[i]=-1; rep(i,n){ dfs(i,0); } rep(i,m){ bool ans; if(c[a[i]]>c[b[i]])ans=0; else if(c[a[i]]<c[b[i]])ans=1; else if(d[a[i]]+1==d[b[i]])ans=0; else if(d[a[i]]==d[b[i]]+1)ans=1; else if(d[a[i]]>d[b[i]])ans=0; else ans=1; cout<<(ans==0?"->":"<-")<<"\n"; } }
#include<bits/stdc++.h> #include<cmath> #define pb push_back #define ld long double #define mp make_pair #define vl vector<ll> #define vd vector<double> #define vld vector<long double> #define ll long long int #define pl pair<ll, ll> #define all(a) a.begin(), a.end() #define forr(i, n) for(ll i=0; i<n; i++) #define forr1(i, n) for(ll i=1; i<=n; i++) using namespace std; const ld PI =3.1415926535897923846; const ll MOD = 1000000007; const ll N=998244353; ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;} ll modinverse(ll a){return power(a, MOD-2);} vl val, depth; ll n; vector<vl> adj; void dfs(ll node, ll parent, ll current) { val[node]+=current; current=val[node]; //cout<<node+1<<' '<<1+parent<<' '<<val[node]<<endl; for(auto x: adj[node]) { if(x!=parent) { dfs(x, node, current); } } } void dfs2(ll node, ll parent, ll cur_depth) { depth[node]=cur_depth; for(auto x: adj[node]) { if(x!=parent) { dfs2(x, node, cur_depth+1); } } } void solve() { cin>>n; adj.resize(n); depth.resize(n, 0); map<ll, pl> mp1; val.resize(n, 0); forr(i, n-1) { ll u, v; cin>>u>>v; --u, --v; mp1[i+1]={u, v}; adj[u].pb(v), adj[v].pb(u); } ll q; cin>>q; dfs2(0, -1, 0); while(q--) { ll t, e, x; cin>>t>>e>>x; ll st, en; if(t==1) { st=mp1[e].first; en=mp1[e].second; } else { st=mp1[e].second; en=mp1[e].first; } if(depth[st]>depth[en]) { val[st]+=x; } else { //cout<<st+1<<' '<<en+1<<endl; val[0]+=x; val[en]-=x; } } dfs(0, -1, 0); forr(i, n) { cout<<val[i]<<endl; } } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll test=1; // cin>>test; while(test--) { solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using vll = vector<ll>; using vvll = vector<vector<ll>>; using vvvll = vector<vector<vector<ll>>>; using vpll = vector<pair<ll, ll>>; #define rep(i,n) for (ll i = 0; i < (n); i++) #define all(v) (v).begin(), (v).end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define pb push_back #define endl '\n' #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); const int inf = 1<<30; const ll INF = 1ll<<62; //const ll mod = 1000000007; const ll mod = 998244353; //const ll dx[4] = {1, 0, -1, 0}; //const ll dy[4] = {0, 1, 0, -1}; template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> void disp1(vector<T> &array, bool blank=1) { rep(i, array.size()) cout << ((i&&blank) ? " " : "") << array[i]; cout << endl; //cout << " "; } template<class T> void disp2(vector<vector<T>> &array, bool blank=1) { rep(i, array.size()) disp1(array[i], blank); } ll rem(ll a, ll b) { return (a % b + b) % b; } // 余りを0~b-1に丸める // 小数の入力が文字で与えられたとき、10^n倍したものを返す ll x10(string str_f, ll n) { if (str_f[0] == '-') return -x10(str_f.substr(1), n); ll times = 1; rep(i,n) times *= 10; // times = 10^n int id = -1; rep(i,str_f.size()) if (str_f[i] == '.') id = i; if (id == -1) { return stoll(str_f)*times; } string str_int = str_f.substr(0, id), str_dec = str_f.substr(id+1); while (str_dec.size() < n) str_dec += '0'; return stoll(str_int)*times + stoll(str_dec); } // A^N (mod) ll modpow(ll A, ll N, ll mod) { ll RES = 1; while (N) { if (N & 1) RES = RES * A % mod; A = A * A % mod; N >>= 1; } return RES; } long long gcd(long long A, long long B) {return B ? gcd(B, A%B) : A;} long long lcm(long long A, long long B) {return (A / gcd(A, B)) * B;} //using mint = modint998244353; ll extgcd(ll a, ll b, ll &x, ll &y) { ll d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } void solve() { ll X, Y, P, Q; cin >> X >> Y >> P >> Q; ll A = 2*(X+Y), B = P+Q; ll ans = INF, g = gcd(A, B), l = lcm(A, B); for (ll i = 0; i < Y; i++) { for (ll j = 0; j < Q; j++) { // Ax+X+i = By+P+j なる(x,y)を求める ll x, y; ll G = P+j-X-i; if (G%g != 0) continue; ll a = A/g, b = B/g, c = G/g; extgcd(a, b, x, y); ll re = c % b * rem(x, b) % b; re *= A; re += X+i; re = rem(re, l); chmin(ans, re); } } if (ans != INF) cout << ans << endl; else puts("infinity"); return ; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long #define forn(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define sz(a) (int)a.size() #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define GR(a,n,m) vector<vector<int>> a(n, vector<int>(m, 0)); const int mod = 1e9 + 7; const int N = 3005; int dp[N][N]; ll a[N]; int cnt[N][N]; void solve() { int n; cin >> n; for(int i = 1;i <= n;i++){ cin >> a[i]; } dp[0][0] = 1; ll sum = 0; map<pair<int,int>,int> m; for(int i = 1;i <= n;i++){ sum += a[i]; for(int j = 1; j <= n;j++){ if(j == 1)dp[i][j]++; dp[i][j]+= cnt[((sum%j)+j)%j][j - 1]; dp[i][j]%=mod; } for(int j = 1;j <= n;j++){ cnt[((sum%(j+1))+(j+1))%(j+1)][j] += dp[i][j]; cnt[((sum%(j+1))+(j+1))%(j+1)][j] %= mod; } } int ans = 0; for(int i = 1;i <= n;i++){ ans += dp[n][i]; ans%=mod; } cout << ans << "\n"; } int32_t main() { fastio; int t = 1; //cin >> t; while(t--) { solve(); } }
/** * @author yuzining * @date 2021/03/27 20:12 */ #ifdef local #include "D:\VSCODE\debug.hpp" #else #include <bits/stdc++.h> #define DEBUG(...) do { } while (0) const int _______ = []() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); return 0; }(); #endif int main() { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } int ans = INT_MAX; for (int i = 0; i < (1 << n); i++) { int cur = 0, tmp = 0; for (int j = 0; j < n; j++) { if ((i >> j) & 1) { cur ^= tmp; tmp = 0; } tmp |= a[j]; } cur ^= tmp; ans = std::min(ans, cur); } std::cout << ans << '\n'; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <iostream> #include <fstream> #ifndef JHELPER1_COMMON_H #define JHELPER1_COMMON_H #include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> typedef long long ll; using namespace std; constexpr ll MOD = 1000 * 1000 * 1000 + 7; #define READ_VEC(v) for(auto &e: v){in >> e;} #define READ_PAIR_VEC(v) for(auto &e: v){in >> e.first >> e.second;} #define ALL(c) std::begin(c), std::end(c) #endif //JHELPER1_COMMON_H class CORXOR { public: void solve(std::istream& in, std::ostream& out) { int N; in >>N; vector<int> A(N); READ_VEC(A); ll mi = 1000'000'000'000L; for (int i = 0; i < (1<<(N-1)); ++i) { ll tot = 0; ll lo = 0; for (int j = 0; j < N; ++j) { lo |= A[j]; if (j == N-1 || i & (1<<j)) { tot ^= lo; lo = 0; } } mi = min(mi, tot); } out << mi << endl; } }; int main() { CORXOR solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) #define all(a) a.begin(),a.end() #define lb lower_bound #define ub upper_bound #define owo ios_base::sync_with_stdio(0);cin.tie(0); #define MOD (ll)(1e9+7) #define INF (ll)(1e18) #define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\ debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> PII; typedef pair<int,int> pii; typedef vector<vector<int>> vii; typedef vector<vector<ll>> VII; ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);} int main() { int n;cin>>n; cout<<n-1; }
//-------------------------// #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define srt(v) sort(all(v)) #define rsrt(v) sort(v.rbegin(), v.rend()) #define sp " " #define nl "\n" #define rep(i,n) for(int i=0;i<n;i++) #define fst ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int N=1e9+7; typedef long long ll; typedef long double ld; ll power(ll x, ll y){ ll res = 1; if (x == 0) return 0; while (y > 0){ if (y & 1) res = (res*x) % N; y = y>>1; x = (x*x) % N; } return res; } /* int Seive[1000001]={0}; void seive(ll n) { Seive[1]={1}; for(int i=2;i*i<=n;i++){ if(Seive[i]){continue;} for(int j=2*i;j<=n;j+=i){ Seive[j]=1; } } } */ void solve(){ ll n; cin>>n; if(n&1){ cout<<2*(n-1)/2; }else{ cout<<2*n/2 - 1; } cout<<nl; } int main(){ fst //cout<<fixed<<setprecision(15); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(3) #define int long long #define ri register #define mk make_pair #define fi first #define se second #define pb push_back #define eb emplace_back #define is insert #define es erase using namespace std; inline int read() { int s = 0, w = 1; ri char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int n; string t; int INF = 1e10; signed main() { n = read(); cin >> t; if (t == "1") { printf("%lld\n", INF * 2); return 0; } int flg = 0; int be = 0; if (t[0] == '0') be = 1, flg++; else if (t.substr(0, 2) == "10") be = 2, flg++; int res = 0; for (; be + 2 < n; be += 3) { if (t.substr(be, 3) == "110") res++; else return puts("0") & 0; } if (be != n) { if (t.substr(be) == "1" || t.substr(be) == "11") flg++; else return puts("0") & 0; } printf("%lld\n", INF - (res + flg) + 1); return 0; }
#include <stdio.h> #include <iostream> #include <cmath> #include <vector> #include <numeric> #include <string> int main() { int k; std::cin >> k; std::vector<int> v(10, k); std::vector<double> tv(10, 0); std::vector<double> av(10, 0); int ta = 0; int ao = 0; std::string s; std::cin >> s; for (int c = 0; c < 4; c++) { int f; f = std::stoi(s.substr(c, 1)); tv[f] += 1; v[f] -= 1; } for (int c = 1; c < 10; c++) { ta += c*pow(10, tv[c]); } std::cin >> s; for (int c = 0; c < 4; c++) { int f; f = std::stoi(s.substr(c, 1)); av[f] += 1; v[f] -= 1; } for (int c = 1; c < 10; c++) { ao += c*pow(10, av[c]); } double ans = 0.; for (int c = 1; c < 10; c++) { for (int b = 1; b < 10; b++) { if (c == b) { if (v[c] > 1) { if (ao + b * pow(10, av[b] + 1) - b * pow(10, av[b]) < ta + c * pow(10, tv[c] + 1) - c * pow(10, tv[c])) { ans += static_cast<double>(v[c]) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0)) * static_cast<double>(v[c] - 1) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0) - 1); } } } else { if (v[c] > 0 && v[b] > 0) { if (ao + b * pow(10, av[b] + 1) - b * pow(10, av[b]) < ta + c * pow(10, tv[c] + 1) - c * pow(10, tv[c])) { ans += static_cast<double>(v[c]) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0)) * static_cast<double>(v[b]) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0) - 1); } } } } } std::cout << ans << std::endl; }
#include<bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define f(x, m) for(auto x : m) #define cpu() ios::sync_with_stdio(false); cin.tie(nullptr) #define pb push_back #define pii pair<int,int> #define pll pair<ll, ll> #define vi vector<int> #define vl vector<ll> #define vii vector<pair<int ,int>> #define vll vector<pair<ll ,ll>> #define all(v) v.begin(),v.end() #define sor(a) sort( a.begin(), a.end() ) #define ros(a) sort( a.rbegin(), a.rend()) #define prec(n) fixed << setprecision(n) #define ff first #define ss second #define print(x) for(auto it : x) cout << it << " "; #define debug(x) cerr << #x << " is " << x << endl; typedef long long ll; using namespace std; // using namespace __gnu_pbds; #define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> ) {cout << "NEXT\n"; } template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } template<typename... T> void rd(T& ... args){ (( cin >> args), ...); } template<typename... T> void ps(T ... args){ ((cout << args << ' '), ...); cout << '\n'; } // mp.max_load_factor(0.25); // using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>; const int MOD = 998244353, MAX = 2e5 + 5; const char nl = '\n'; const int INF = 1e9 + 5; int n, m; int cn[MAX][20]; ll fac[MAX]; ll pot(ll x, ll y){ if(y == 0) return 1; if(y & 1) return (pot(x, y - 1) * x) % MOD; ll k = pot(x, y / 2); return (k * k) % MOD; } ll inv(ll x){ return pot(x, MOD - 2); } ll comb(int x, int y){ if(x < y) return 0; return (((fac[x] * inv(fac[y])) % MOD) * inv(fac[x - y])) % MOD; } void solve(){ cin >> n >> m; cn[1][0] = 1; for(int i = 2; i <= m; i++){ cn[i][0] = 1; for(int j = 1; j * j <= i; j++){ if(i % j) continue; for(int k = 1; k < 20; k++){ cn[i][k] += cn[j][k - 1]; } if(j * j != i && j != 1){ for(int k = 1; k < 20; k++){ cn[i][k] += cn[i / j][k - 1]; } } } } ll ans = 0; vector<int> CN(20); for(int i = 1; i <= m; i++){ for(int j = 0; j < 20; j++){ CN[j] += cn[i][j]; } } for(int j = 0; j < 20; j++){ ans = (ans + comb(n - 1, j) * CN[j]) % MOD; } cout << ans << "\n"; } int main(){ cpu(); fac[0] = 1; for(int i = 1; i < MAX; i++){ fac[i] = (fac[i - 1] * i) % MOD; } solve(); return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i < n + 1; i++) #define all(A) A.begin(), A.end() typedef long long ll; int main() { ll b, c; cin >> b >> c; ll Rstart, Rend, Lstart, Lend; if (c == 1) { if (b == 0) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } if (c % 2 == 0) { Rstart = b - c / 2; Rend = b + c / 2 - 1; Lstart = -b - (c - 1) / 2; Lend = -b + (c - 1) / 2; } else { Lstart = -b - c / 2; Lend = -b + c / 2; Rstart = b - (c - 1) / 2; Rend = b + (c - 1) / 2 - 1; } ll rangeR = Rend - Rstart + 1; ll rangeL = Lend - Lstart + 1; ll RandL = max((ll)0, min(Rend, Lend) - max(Rstart, Lstart) + 1); // cout << Lstart << "~~" << Lend << "," << Rstart << "~~" << Rend << endl; // cout << rangeL << "," << RandL << "," << rangeR << endl; cout << rangeR + rangeL - RandL << endl; }
// Libraries #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; // Aliases #define nitro std::ios_base::sync_with_stdio(false), std::cin.tie(NULL) #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define fi first #define se second #define sz(x) (int)x.size() #define all(v) v.begin(), v.end() #define case_g(x) cout<<"Case #"<<x<<": " using ll = long long; using ld = long double; using ull = unsigned long long; using ul = unsigned long; using vi = std::vector<int>; using vl = std::vector<ll>; using vb = std::vector<bool>; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using pib = std::pair<int, bool>; using plb = std::pair<ll, bool>; using vii = std::vector<pii>; using vll = std::vector<pll>; using vib = std::vector<pib>; using vlb = std::vector<plb>; using vvi = std::vector<vi>; using vvl = std::vector<vl>; using vvb = std::vector<vb>; using mapii = std::map<int, int>; using mapll = std::map<ll, ll>; using mapib = std::map<int, bool>; using maplb = std::map<ll, bool>; using hmapii = __gnu_pbds::gp_hash_table<int, int>; using hmapll = __gnu_pbds::gp_hash_table<ll, ll>; using hmapib = __gnu_pbds::gp_hash_table<int , bool>; using hmaplb = __gnu_pbds::gp_hash_table<ll, bool>; using ranked_pairset = tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update>; using ranked_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>; using ranked_map = tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update>; // Debugging template #ifndef LOCAL #define cerr if (false) cerr #endif void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} template<typename T> void _printA(T *t, long long sz) { cout<<" { "; for (long long i=0; i<sz; i++) cout<<"["<<i<<"] = "<< t[i]<<endl; cout<<" } \n";} #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define debugA(x, y) cerr << "[" << #x << "] = "; _printA(x, y) // Constants const ll mod = 1000000007; const ld pi = 3.141592653589793L; const char nl = '\n'; // Colours for fun :) #define RED "\033[31m" #define GREEN "\033[32m" #define RESET "\033[0m" void precompute() { } void solve() { int n; string s; cin >> n >> s; if( s[0] != s[n - 1] ) { cout << 1 << nl; return; } for(int i = 1; i <= n - 2; ++i) { if( s[i] != s[0] && s[i + 1] != s[0] ) { cout << 2 << nl; return; } } cout << -1 << nl; } int main() { nitro; int tc = 1; //cin >> tc; precompute(); for(int i = 1; i <= tc; ++i) { //case_g(i); solve(); } return 0; }
/*Allah Vorosha*/ #include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define in insert #define rev reverse #define all(x) (x).begin(),(x).end() #define all2(x) (x).rbegin(),(x).rend() #define sz(v) (int)v.size() #define yes cout << "YES\n" #define no cout << "NO\n"; #define take for(auto &it : a) cin >> it; #define vi vector<int> #define vii vector<pair<int, int > > #define vl vector<ll> #define fi first #define se second #define l_b lower_bound #define u_b upper_bound #define ed printf("\n") #define mems(a, b) memset(a, b, sizeof(a)) #define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie(); using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<class T> void read(T &x) { cin >> x; } template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); } template<class T> void print1(const T &x) { cout << x << '\n'; } template<class T, class ...U> void print1(const T &x, const U& ... u) { print1(x); print1(u...); } template<class T> void print2(const T &x) { cout << x << ' '; } template<class T, class ...U> void print2(const T &x, const U& ... u) { print2(x); print2(u...); } const int N = 2e3 + 5, mod = 1e9 + 7, M = 5e5 + 5; const ll inf = 9e18; int cs = 1; void solve() { int n; string s; read(n, s); int ans = -1; for(int i = 1; i < n; i++) { if(s[i] != s[0]) { if(i + 1 == n) { ans = 1; break; } if(s[i + 1] != s[n - 1]) { ans = 2; } } } print1(ans); } int main() { ios; //cout << fixed << setprecision(15); //int tc; read(tc); while(tc--> 0) solve(); return 0; }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; using lpair = pair<ll, ll>; using vll = vector<ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;} #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} 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; } void vis_2d(vector< vector<ll> > v, ll h, ll w, ll pad=4){ cout << "ij "; rep(i,0,w) cout << left << setw(pad) << i; cout << endl; rep(i,0,h){ cout << i << ": "; rep(j,0,w){ if(v[i][j] == INF) cout << "x" << string(pad-1, ' '); else cout << left << setw(pad) << v[i][j]; } cout << endl; } } struct Combination{ private: ll N; public: vll fac, facinv, inv; Combination(ll n){ N = n; fac.resize(N+1); facinv.resize(N+1); inv.resize(N+1); fac[0] = fac[1] = 1; facinv[0] = facinv[1] = 1; inv[1] = 1; rep(i,2,N+1){ fac[i] = fac[i-1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; facinv[i] = facinv[i-1] * inv[i] % MOD; } } ll power(ll x, ll n, ll m = MOD){ if(n == 0) return 1LL; ll res = power(x * x % m, n/2, m); if(n % 2 == 1) (res *= x) %= m; return res; } ll nck(ll n, ll k){ if(k == 0 || n == k) return 1LL; if(n < k) return 0LL; return fac[n] * facinv[k] % MOD * facinv[n-k] % MOD; } ll npk(ll n, ll k){ if(k == 0) return 1LL; if(n < k) return 0LL; return fac[n] * facinv[n-k] % MOD; } }; void solve(){ ll N,M,K; cin >> N >> M >> K; Combination cb(2000010); ll ans = cb.nck(N+M, N); ll diff = 0; if(K >= N){ diff = 0; }else if(K < N-M){ print(0); return; }else{ diff = N-K; } rep(i,0,diff){ ll val = cb.nck(N+M, i); if(i > 0) val -= cb.nck(N+M, i-1); // print(val); ans -= val; ans = (ans + 2*MOD) % MOD; } print(ans); } int main(){ cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1000000007; // 拡張ユークリッド互除法 ll extGCD(ll a, ll b, ll &x, ll &y) { if(b==0) { x=1; y=0; return a; } else { ll d = extGCD(b, a%b, y, x); y-=(a/b)*x; return d; } } // 階乗のmodにおける逆元を求める ll calcInv(ll a, ll P) { ll x,y; extGCD(a, P, x, y); return (x+P)%P; } int main() { ll N,M,K; cin>>N>>M>>K; ll ans=0; if(N-M<=K && (N>=0 || M>=0)) { vector<ll> fac(N+M+2,1); for(ll i=1; i<=N+M+1; ++i) fac[i]=(fac[i-1]*i)%MOD; ans=fac[N+M]; ans=(ans*calcInv(fac[N],MOD))%MOD; ans=(ans*calcInv(fac[M],MOD))%MOD; /* ll ng_height=N+M-(K+1); if(ng_height>=0 && M<=ng_height) { ll val=fac[ng_height]; val=(val*calcInv(fac[ng_height-M],MOD))%MOD; val=(val*calcInv(fac[M],MOD))%MOD; ans=(ans-val+MOD)%MOD; } */ if(N-(K+1)>=0) { ll val=fac[N+M]; val=(val*calcInv(fac[N-(K+1)],MOD))%MOD; val=(val*calcInv(fac[M+K+1],MOD))%MOD; ans=(ans-val+MOD)%MOD; } } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; const long long MOD = 1e9+7; const long long N = 4e4+1; typedef long long ll; typedef long double ld; void test_case() { ll n; cin >> n; vector<ll> v(n); ll zc = 0; map<ll,vector<ll>> mm; for(int i = 0; i < n; i++) { cin >> v[i]; v[i] %= 200; if(v[i] == 0) zc = i+1; mm[v[i]].push_back(i); } bool flag = 0; ll x1, x2; for(auto u : mm) { if(u.second.size() == 1) { continue; } flag = 1; x1 = u.second[0]+1, x2 = u.second[1]+1; } if(flag) { cout << "Yes\n"; cout << 1 << ' ' << x1 << '\n'; cout << 1 << ' ' << x2 << '\n'; return; } vector<ll> is(N, 0); vector<array<ll,3>> who[N]; is[0] = 1; for(int i = 0; i < n; i++) { for(int j = N-1; j >= 0; j--) { if(v[i] <= j) { is[j] += is[j-v[i]]; if(is[j-v[i]]) { who[j].push_back({v[i], j, i+1}); } } } } // if(!zc) { // is[0] = 0; // } for(int i = 0; i < N; i++) { ll ww = i%200; if(i != ww) { for(auto u : who[i]) { who[ww].push_back(u); } is[ww] += is[i]; } if(is[ww] > 1 && who[ww].size() > 1) { i = ww; vector<ll> a1, a2; ll curr = who[i][0][1]; a1.push_back(who[i][0][2]); curr -= who[i][0][0]; if(curr != 0) { for(int j = who[i][0][2]-2; j >= 0; j--) { if(v[j] <= curr && is[curr] && is[curr-v[j]]) { a1.push_back(j+1); curr -= v[j]; } if(curr == 0) break; } } curr = who[i][1][1]; a2.push_back(who[i][1][2]); curr -= who[i][1][0]; if(curr != 0) { for(int j = who[i][1][2]-2; j >= 0; j--) { if(v[j] <= curr && (is[curr-v[j]] || curr == v[j])) { a2.push_back(j+1); curr -= v[j]; } if(curr == 0) break; } } sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); cout << "Yes\n"; cout << a1.size() << ' '; for(auto u : a1) { cout << u << ' '; } cout << '\n'; cout << a2.size() << ' '; for(auto u : a2) { cout << u << ' '; } cout << '\n'; return; } } cout << "No\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tc = 1; // cin >> tc; while(tc--) test_case(); return 0; }
#include<stdio.h> #define rep(i,N) for(int i=0;i<(int)N;i++) static inline int IN(void) { int x=0,f=1,c=getchar();while(c<48||c>57){if(c==45){f=-f;}c=getchar();} while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f*x; } static inline void OUT(int x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x-x/10*10+48);} int main(void) { int N=IN(),A[N],bk[200][8]={{0}},pos[200]={0},count=N<8?N:8,s[8]={},sum=0,sz=0; rep(i,N){A[i]=IN();} rep(i,1<<count) { sum=0;s[0]=s[1]=s[2]=s[3]=s[4]=s[5]=s[6]=s[7]=0;sz=0; rep(j,count)if(i&(1<<j)){s[sz++]=j+1;sum+=A[j];sum-=sum/200*200;} if(pos[sum]) { puts("Yes");OUT(pos[sum]);putchar(' '); rep(j,pos[sum]){OUT(bk[sum][j]);putchar(' ');}puts("");OUT(sz);putchar(' '); rep(j,sz){OUT(s[j]);putchar(' ');}return 0; } else{rep(j,sz){bk[sum][j]=s[j];}pos[sum]=sz;} } return !puts("No"); }
#include <bits/stdc++.h> #define REP(i,n) for(int i=0;i<(int)(n);++i) #define ALL(v) (v).begin(),(v).end() #define debug(x) cerr<<#x<<": "<<(x)<<endl using namespace std; using llong = long long; using vi = vector<int>; using vvi = vector<vi >; using vvvi = vector<vvi >; using pii = pair<int,int>; constexpr int INF = 1e9; constexpr llong LINF = 1e18; constexpr double EPS = 1e-12; constexpr int MOD = 1e9+7; template<class Type> void line(const Type &a){int cnt=0;for(const auto &elem:a)cerr<<(cnt++?' ':'>')<<elem;cerr<<endl;} template<class Type> inline bool chmax(Type &a,const Type &b){if(a<b){a=b;return true;}return false;} template<class Type> inline bool chmin(Type &a,const Type &b){if(a>b){a=b;return true;}return false;} /*ダイクストラ法*/ template<typename T> class Dijkstra{ using P=std::pair<T,int>; struct Edge{ //Edge:辺の情報を表す構造体. int to; T cost; }; int vn; //vn:=(ノード数). std::vector<std::vector<Edge> > g; //g[v]:=(ノードvがもつ辺のリスト). std::vector<T> d; //d[v]:=(ノードsからvへの最短距離). std::vector<int> pre; //pre:=(逆方向経路(経路復元用)). T inf; public: //constructor. Dijkstra():Dijkstra(0){} Dijkstra(size_t vn_,T inf_=1e9):vn(vn_),g(vn_),d(vn_,inf_),pre(vn_,-1),inf(inf_){} T get_inf() const{return inf;} void add_edge(int from,int to,T cost){//重み付き有向辺を張る. assert(0<=from and from<vn); assert(0<=to and to<vn); g[from].push_back((Edge){to,cost}); } void dijkstra(int s){//sから各ノードへの最短距離を求める. O(|E|log|V|). assert(0<=s and s<vn); std::fill(d.begin(),d.end(),inf); d[s]=0; std::fill(pre.begin(),pre.end(),-1); std::priority_queue<P,std::vector<P>,std::greater<P> > pque; pque.emplace(0,s); while(!pque.empty()){ auto [cost,v]=pque.top(); pque.pop(); if(d[v]<cost) continue; for(const Edge &e:g[v]){ if(d[e.to]>d[v]+e.cost){ d[e.to]=d[v]+e.cost; pre[e.to]=v; pque.emplace(d[e.to],e.to); } } } } T dist(int t) const{//ノードsからtへの最短距離を返す. assert(0<=t and t<vn); return d[t]; } std::vector<int> get_path(int t) const{//ノードtへの最短経路を返す. assert(0<=t and t<vn); std::vector<int> path; for(;t!=-1;t=pre[t]) path.push_back(t); std::reverse(path.begin(),path.end()); return path; } }; int main(){ int n,m; cin>>n>>m; Dijkstra<int> ob(n); vi self(n,INF); REP(i,m){ int a,b,c; cin>>a>>b>>c; a--, b--; if(a==b) chmin(self[a],c); else ob.add_edge(a,b,c); } vvi d(n,vi(n)); REP(i,n){ ob.dijkstra(i); REP(j,n) d[i][j]=ob.dist(j); } vi ans(n,INF); REP(i,n) chmin(ans[i],self[i]); REP(i,n-1)for(int j=i+1;j<n;++j){ chmin(ans[i],d[i][j]+d[j][i]); chmin(ans[j],d[i][j]+d[j][i]); } for(auto out:ans) cout<<(out==INF?-1:out)<<endl; }
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <numeric> #include <cassert> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; 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; } using namespace std; typedef long long ll; typedef pair<ll, int> P; struct edge{ int to, cost; }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, m; cin >> n >> m; vector<vector<edge>> g(n); for(int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; a--; b--; g[a].push_back((edge){b, c}); } const int INF = 1e9; for(int s = 0; s < n; s++){ vector<int> d(n, INF); priority_queue<P, vector<P>, greater<P>> que; que.push(P(0, s)); while(!que.empty()){ P p = que.top();que.pop(); int v = p.second; if(v != s && d[v] < p.first) continue; for(edge e : g[v]){ if(p.first + e.cost < d[e.to]){ d[e.to] = p.first + e.cost; que.push(P(d[e.to], e.to)); } } } if(d[s] == INF) cout << -1 << endl; else cout << d[s] << endl; } }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstdlib> #include <cstring> #include <cmath> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <complex> #include <vector> #include <limits> #include <iomanip> #include <cassert> #include <numeric> #include <chrono> #include <random> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define debug(x) cerr << #x << " = " << (x) << endl; #define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")"; #define rep(i, n) for(int i = 0;i < n;i++) #define pb push_back #define F first #define S second // template<typename _Ty1, typename _Ty2> // ostream& operator<<(ostream& _os, const pair<_Ty1, _Ty2>& _p) { // _os << '(' <<_p.first << ',' << _p.second << ')'; // return _os; // } // // template<typename _Ty1, typename _Ty2> // istream& operator>>(istream& _is, pair<_Ty1, _Ty2>& _p) { // _is >> _p.first >> _p.second; // return _is; // } bool dp[105][(int)1e5 + 10]; void solve() { int n;cin >> n; int sum = 0; vector<int> a(n); rep(i, n) { cin >> a[i]; sum += a[i]; } for(int i = 0;i <= n;i++) { for(int diff = 0;diff <= sum;diff++) { if(i == 0) { if(diff == 0) dp[i][diff] = true; } else { if(diff+a[i-1] <= sum) dp[i][diff] |= dp[i-1][diff+a[i-1]]; if(diff-a[i-1] >= 0) dp[i][diff] |= dp[i-1][diff-a[i-1]]; if(a[i-1] - diff > 0) dp[i][diff] |= dp[i-1][a[i-1]-diff]; } } } for(int diff = 0;diff <= sum;diff++) { if(dp[n][diff] == true) { //x+diff+x = sum cout << (sum-diff)/2 + diff << endl; return; } } } int main() { // freopen("input.in","r",stdin); // freopen("output.out","w",stdout); // cout << fixed << setprecision(15); ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pii pair<int,int> #define pb push_back #define mp make_pair using namespace std; string s; int cnt[10]; int main() { cin>>s; for(int i=0;i<(int)s.size();i++) cnt[s[i]-'0']++; if(s.size()==1){ if(s[0]=='8') puts("Yes"); else puts("No"); return 0; } if(s.size()==2){ for(int i=16;i<100;i+=8){ int x=i%10,y=i/10; cnt[x]--;cnt[y]--; if(cnt[x]>=0 && cnt[y]>=0){ puts("Yes"); return 0; } cnt[x]++;cnt[y]++; } puts("No"); return 0; } for(int i=0;i<1000;i+=8){ int x=i%10,y=(i/10)%10,z=i/100; cnt[x]--;cnt[y]--;cnt[z]--; if(cnt[x]>=0 && cnt[y]>=0 && cnt[z]>=0){ puts("Yes"); return 0; } cnt[x]++;cnt[y]++;cnt[z]++; } puts("No"); return 0; }
#include <bits/stdc++.h> using namespace std; long long int tab[100]; int main() { long long int a, b, n, p, i, j; cin >> n >> p; a=p-1; n--; tab[0]=p-2; for(i=1; i<=80; i++){ tab[i]=tab[i-1]*tab[i-1]; tab[i]%=1000000007; } i=0; while(n){ if(n%2){ a*=tab[i]; a%=1000000007; } i++; n/=2; } cout << a << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; #define ios ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ; #define endl "\n" #define f1(i,n) for (int i=0;i<n;i++) #define f2(i,n) for (int i=1;i<=n;i++) #define int long long typedef vector<int> vi ; typedef queue<int> qi ; typedef vector<vector<int>> vvi ; //typedef vector<vector<ll>> vvll ; typedef vector<vector<pair<int,int>>> vvp ; typedef vector<bool> vb ; typedef vector<double> vd ; typedef vector<vector<char>> vvc ; typedef vector<char> vc ; typedef pair<int,int> pii ; typedef vector<pair<int,int>> vpi ; //typedef vector<pair<ll,ll>> vpll ; //typedef pair<ll,ll> pll ; //typedef vector<ll> vll ; typedef stack<int> si ; typedef map<int,int> mii ; typedef set<int> seti ; typedef double db ; const int lim = 1000000+100 ; int mod1 = 998244353 ; int mod2 = 1000000007 ; int INF = 2000000000 ; int M=1000000000000000000+10000000 ; // ********************************************************** int min(int x,int y) { if(x<y) return x; else return y; } int max(int x,int y) { if(x>y) return x; else return y; } int gcd (int a, int b) { if (b == 0) return a; else return gcd (b, a % b); } int binpow(int a, int b) { if (b == 0) return 1; int res = binpow(a, b / 2); if (b % 2) return (((res * res)) * a) ; else return (res * res) ; } // *********************************************************** signed main() { ios int n ;cin >> n ; int a[n] ; f1(i,n) cin >> a[i] ; sort(a,a+n) ; int sum=0 ; int res=0 ; for(int i=n-2;i>=0;i--) { sum*=2 ; sum%=mod1 ; sum+=a[i+1] ; sum%=mod1 ; res+=a[i]*sum ; // cout << sum ; res%=mod1 ; } f1(i,n) { res+=((a[i]*a[i])%mod1) ; res%=mod1 ; } cout << res ; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; const int mod=998244353; ll a[maxn]; void solve(){ int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); ll sum=0,ans=0; for(int i=1;i<=n;i++) { ans=(ans+sum*a[i]+a[i]*a[i])%mod; sum=(sum*2+a[i])%mod; // cout<<sum<<'\n'; } cout<<ans<<'\n'; } int main() { ios::sync_with_stdio(false); int t=1; // cin>>t; for(int i=1;i<=t;i++) { solve(); } return 0; }
// #include<iostream> #include<bits/stdc++.h> using namespace std; long _mergeSort(long arr[], long temp[], long left, long right); long merge(long arr[], long temp[], long left, long mid, long right); /* This function sorts the input array and returns the number of inversions in the array */ long mergeSort(long arr[], long array_size) { long temp[array_size]; return _mergeSort(arr, temp, 0, array_size - 1); } /* An auxiliary recursive function that sorts the input array and returns the number of inversions in the array. */ long _mergeSort(long arr[], long temp[], long left, long right) { long mid, inv_count = 0; if (right > left) { /* Divide the array longo two parts and call _mergeSortAndCountInv() for each of the parts */ mid = (right + left) / 2; /* Inversion count will be sum of inversions in left-part, right-part and number of inversions in merging */ inv_count += _mergeSort(arr, temp, left, mid); inv_count += _mergeSort(arr, temp, mid + 1, right); /*Merge the two parts*/ inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } /* This funt merges two sorted arrays and returns inversion count in the arrays.*/ long merge(long arr[], long temp[], long left, long mid, long right) { long i, j, k; long inv_count = 0; i = left; /* i is index for left subarray*/ j = mid; /* j is index for right subarray*/ k = left; /* k is index for resultant merged subarray*/ while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; /* this is tricky -- see above explanation/diagram for merge()*/ inv_count = inv_count + (mid - i); } } /* Copy the remaining elements of left subarray (if there are any) to temp*/ while (i <= mid - 1) temp[k++] = arr[i++]; /* Copy the remaining elements of right subarray (if there are any) to temp*/ while (j <= right) temp[k++] = arr[j++]; /*Copy back the merged elements to original array*/ for (i = left; i <= right; i++) arr[i] = temp[i]; return inv_count; } int main(){ long n; cin>>n; long v[n]; long a[n]; long temp; for(long i=0;i<n;i++){ cin>>temp; v[i]=temp; a[i]=temp; } long count=0; // for(long i=0;i<n;i++){ count=mergeSort(v,n); cout<<count<<endl; for(long i=0;i<n-1;i++){ count+=(n-a[i]-1); count-=(a[i]); cout<<count<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define fi first #define se second #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep0(i, n) for (ll i = 0; i <= (n); ++i) #define rep1(i, n) for (ll i = 1; i <= (n); ++i) #define rrep1(i, n) for (ll i = (n); i > 0; --i) #define rrep0(i, n) for (ll i = (n); i >= 0; --i) #define srep(i, s, t) for (ll i = s; i < t; ++i) #define srep1(i, s, t) for (ll i = s; i <= t; ++i) #define rng(a) a.begin(), a.end() #define rrng(a) a.rbegin(), a.rend() #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)), x.end()) #define snuke srand((unsigned)clock() + (unsigned)time(NULL)); #define show(x) cerr << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define bn(x) ((1 << x) - 1) #define dup(x, y) (((x) + (y)-1) / (y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) typedef unsigned uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; constexpr ll MAX = 1000; int main() { ll n; cin >> n; vl a(n + 1, 0); rep1(i, n) cin >> a[i]; ll gcd = 0, ans = 0; srep1(i, 2, MAX) { ll count = 0; rep1(j, n) { if (a[j] % i == 0) count++; } if (gcd <= count) { gcd = count; ans = i; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; s.find_by_order(x) xth element in set x.order_of_key(x) number of elements <x*/ #define ll long long #define vi vector<int> #define si set<int> #define mii map<int,int> #define pb push_back #define pf push_front #define PI acos(-1) #define pii pair<int,int> #define extract_word(s) stringstream str(s); while(str>>word) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define SET(s) cout<<fixed<<setprecision(s) #define set0(a) memset(a,0,sizeof(a)) #define endl "\n" #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define lower_string(s) transform(all(s),s.begin(),::tolower) #define upper_string(s) transform(all(s),s.begin(),::toupper) #define size(s) (int)s.size() template<typename T,typename U> bool compare(T x,U y){return(abs(x-y)<=1e-9);} const int MOD=1e9+7; void solve() { fastio int n; cin>>n; int a[n]; for(auto &x:a) cin>>x; int ans=-1; int res; for(int i=2;i<=1000;i++) { int cnt=0; for(auto &x:a) if(x%i==0) cnt++; if(ans<cnt) { res=i; ans=cnt; } } cout<<res; } int main() { //code fastio int t; t=1; while(t--) { solve(); cout<<endl; } }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=int(a);i<int(b);++i) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define ALL(x) (x).begin(),(x).end() template<class T>bool umax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;} template<class T>bool umin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;} #ifdef LOCAL void _debug_out() { cerr << endl; } template <typename Head, typename... Tail> void _debug_out(Head H, Tail... T) { cerr << " " << H; _debug_out(T...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", _debug_out(__VA_ARGS__) #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #define eprintf(...) #endif int main() { ll n,m,t; cin >> n >> m >> t; ll mx = n; vector<ll> a(m), b(m); rep(i,m) cin >> a[i] >> b[i]; ll p = 0; rep(i,m) { ll minus = a[i]-p; debug(i, p, n, minus); n-=minus; if(n <= 0) { cout << "No" << endl; return 0; } ll plus = b[i] - a[i]; debug(i, p, n, plus); n += b[i] - a[i]; umin(n,mx); p = b[i]; } ll minus = t - p; debug(t, p, n, minus); n-=minus; if(n <= 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; }
/** * author: tomo0608 * created: 28.02.2021 18:08:55 **/ #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; typedef pair<int,int> pii; typedef pair<long long, long long> pll; #define all(x) x.begin(),x.end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) #define unique(a) a.erase(unique(a.begin(),a.end()),a.end()) template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >; template<class T, class U> inline bool chmax(T &a, const U &b) { if (a<b) { a=b; return 1; } return 0; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a>b) { a=b; return 1; } return 0; } template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {os << *itr;++itr;if(itr != set_var.end()) os << ", ";itr--;}os << "}";return os;} template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) {os << "{";for(auto itr = map_var.begin(); itr != map_var.end(); itr++) {os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--;}os << "}";return os;} template<class T> inline int count_between(vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r) #define pb push_back #define eb emplace_back #define elif else if #define mp make_pair #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ template<typename T> T gcd(T x, T y){if(x%y == 0)return y;return gcd(y, x%y);} template<typename T> T gcd(vector<T> a){T res = a[0];for(auto &x: a)res = gcd(res, x);return res;} template <typename T>T mypow(T x, ll n){T ret = 1;while(n > 0){if(n & 1)(ret *= x);(x *= x);n >>= 1;}return ret;} #define endl '\n' int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1}; void solve(){ int n, m;cin >> n >> m; V<ll> h(n);cin >> h; V<ll> w(m);cin >> w; sort(all(h)); sort(all(w)); V<ll> left((n+1)/2), right((n+1)/2); for(int i = 0; i + 1 < n; i += 2)left[i/2 + 1] = left[i / 2] + h[i + 1] - h[i]; for(int i = n - 2; i > 0; i -= 2)right[i / 2] = right[i / 2 + 1] + h[i + 1] - h[i]; ll ans = 1e18; for(auto x: w){ int i = lower_bound(all(h), x) - h.begin(); if(i&1)i ^= 1; chmin(ans, left[i/2] + right[i/2] + abs(h[i] - x)); } cout << ans << endl; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << setprecision(20); int codeforces = 1; //cin >> codeforces; while(codeforces--){ solve(); } return 0; }
//#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define int long long using namespace std; typedef long long ll; template<class T>T ABS(T x) {return x<0?-x:x;} //int ksm(int x,int y) {int r=1;while(y) y&1?r=1ll*r*x%mod:0,x=1ll*x*x%mod,y>>=1;return r;} //char __buf[1<<20],*__p1,*__p2; //#define getchar() (__p1==__p2?(__p2=__buf+fread(__p1=__buf,1,1<<20,stdin),__p1==__p2?EOF:*__p1++):*__p1++) int read() { int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*w; } const int maxn = 3e5+10; //const int mod = ; //struct Edge{int to,next;}a[maxn];int h[maxn],cnt; //void add(int x,int y) {a[++cnt]=(Edge){y,h[x]},h[x]=cnt,a[++cnt]=(Edge){x,h[y]},h[y]=cnt;} //vector<int>G[maxn]; //void add(int x,int y) {G[x].push_back(y),G[y].push_back(x);} map<int,int>ton;int s[maxn],n,ans; signed main() { n=read(); for(int i=1;i<=n;i++) s[i]=s[i-1]+(i&1?-read():read()); ++ton[0]; for(int i=1;i<=n;i++) ans+=ton[s[i]],++ton[s[i]]; cout<<ans<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define P pair<int, int> #define fi first #define se second #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() #define pb push_back template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } constexpr int INF = 1000000000; constexpr ll llINF = 1000000000000000000; constexpr int mod = 1000000007; constexpr double eps = 1e-10; const double pi = acos(-1); int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; int Random(int mi, int ma) { random_device rnd; mt19937 mt(rnd()); // 32bit //[mi,ma] uniform_int_distribution<int> engine(mi, ma); return engine(mt); } /* vector<vector<ll>>C,sC; void init_comb(int n,int m){ C.resize(n+1,vector<ll>(m+1,0)); sC.resize(n+1,vector<ll>(m+1,0)); C[0][0]=1; for(int i=1;i<=n;i++){ C[i][0]=1; for(int j=1;j<=m;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } rep(i,n+1){ rep(j,m){ sC[i][j+1]=(sC[i][j]+C[i][j])%mod; } } }*/ bool prime(int a) { if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0) return false; } return true; } vector<int> primes; void init_prime(int n) { primes.push_back(2); for (int i = 3; i <= n; i += 2) { bool f = true; for (int j : primes) { if (j * j > i) break; if (i % j == 0) { f = false; break; } } if (f) primes.push_back(i); } } ll modpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res *= a; res %= mod; } a *= a; a %= mod; b >>= 1; } return res; } vector<ll> inv, fact, factinv; void init_fact(int n) { inv.resize(n + 1); fact.resize(n + 1); factinv.resize(n + 1); inv[0] = 0; inv[1] = 1; fact[0] = 1; factinv[0] = 1; for (ll i = 1; i <= n; i++) { if (i >= 2) inv[i] = mod - ((mod / i) * inv[mod % i] % mod); fact[i] = (fact[i - 1] * i) % mod; factinv[i] = factinv[i - 1] * inv[i] % mod; } } ll _inv(ll a, ll m = mod) { // gcd(a,m) must be 1 ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll comb(int a, int b) { if (a < b) return 0; if (a < 0) return 0; return fact[a] * factinv[a - b] % mod * factinv[b] % mod; } ll multicomb(int a, int b) { return comb(a + b - 1, b); } void solve() { int n, a[300010]; cin >> n; rep(i, n) cin >> a[i]; ll sum = 0, ans = 0; map<ll, int> mp; mp[0]++; rep(i, n) { if (i % 2 == 0) { sum += a[i]; } else { sum -= a[i]; } mp[sum]++; } for (auto [num, cnt] : mp) { ans += 1LL * cnt * (cnt - 1) / 2; } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); // int t;cin>>t;while(t--) solve(); return 0; }
//****************************Template Begins****************************// // Header Files #include<iostream> #include<iomanip> #include<algorithm> #include<vector> #include<utility> #include<set> #include<unordered_set> #include<list> #include<iterator> #include<deque> #include<queue> #include<stack> #include<set> #include<bitset> #include<random> #include<map> #include<unordered_map> #include<stdio.h> #include<complex> #include<math.h> #include<cstring> #include<chrono> #include<string> // Header Files End using namespace std; #define HARI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ll long long #define umap unordered_map #define uset unordered_set #define lb lower_bound #define ub upper_bound #define fo(i,a,b) for(i=a;i<=b;i++) #define all(v) (v).begin(),(v).end() #define all1(v) (v).begin()+1,(v).end() #define allr(v) (v).rbegin(),(v).rend() #define allr1(v) (v).rbegin()+1,(v).rend() #define sort0(v) sort(all(v)) typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<ll, ll> pll; #define max3(a,b,c) max(max((a),(b)),(c)) #define max4(a,b,c,d) max(max((a),(b)),max((c),(d))) #define min3(a,b,c) min(min((a),(b)),(c)) #define min4(a,b,c,d) min(min((a),(b)),min((c),(d))) #define pb push_back #define ppb pop_back #define mkp make_pair #define inf 9999999999999 const ll mod = 1e9 + 7; ll inv(ll i) {if (i == 1) return 1; return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;} ll mod_mul(ll a, ll b) {a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;} ll mod_add(ll a, ll b) {a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;} ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);} ll ceil_div(ll a, ll b) {return a % b == 0 ? a / b : a / b + 1;} ll pwr(ll a, ll b) {a %= mod; ll res = 1; while (b > 0) {if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;} //****************************Template Ends*******************************// vector<vll>adj; vector<bool>visited; ll ans; void dfs(ll curr_node) { visited[curr_node] = true; for (auto x : adj[curr_node]) { if (!visited[x]) { ans++; dfs(x); } } } int main() { HARI; #ifndef ONLINE_JUDGE freopen("input.txt" , "r" , stdin); freopen("output.txt", "w", stdout); #endif ll t, n, i, j, temp, sum; string sans; t = 1; // cin >> t; while (t--) { sans = "NO"; ans = temp = sum = 0; cin >> n; vll a(n + 1, 0); fo(i, 1, n) { cin >> a[i]; } adj.resize(200001); visited.assign(200001, false); for (i = 1; i <= n / 2; i++) { if (a[i] != a[n - i + 1]) { adj[a[i]].pb(a[n - i + 1]); adj[a[n - i + 1]].pb(a[i]); } } fo(i, 1, n) { if (!visited[a[i]]) { dfs(a[i]); } } // for (auto x : adj[1])cout << x << " "; // cout << "\n"; cout << ans; } return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; #define S second #define v vector #define dbg(x) cout<<#x<<"="<<(x)<<endl; #define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; #define dbg3(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl; #define dbg4(x,y,z,w) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<" "<<#w<<"="<<(w)<<endl; #define mp make_pair #define pb push_back #define endl '\n' #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) int main() { fast_cin(); ll n; cin >> n; ll arr[n]; int i; for (i = 0; i < n;i++) cin >> arr[i]; ll mx = *max_element(arr, arr + n); vector<vector<int>> v1(mx + 1); ll sum = 0; ll p = n - 1; for (i = 0; i <p&&i!=p; i++) { if(arr[i]!=arr[p]) { v1[arr[i]].pb(arr[p]); v1[arr[p]].pb(arr[i]); } p--; } vector<ll> vis(mx + 1, 0); queue<ll> q1; for (i = 1; i <= mx;i++) { int c = 0; if (vis[i] == 0&&v1[i].size()>0) { c = 0; q1.push(i); vis[i] = 1; while (!q1.empty()) { ll z = q1.front(); for (auto x : v1[z]) { if (vis[x] == 0) { q1.push(x); vis[x] = 1; } } q1.pop(); c++; } sum += (c - 1); } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long num; long long a[55], b[55]; long long dp[55][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> num; for (int i = 1; i <= n; i++) { cin >> a[i]; } a[0] = 1; for (int i = n; i >= 1; i--) { b[i] = num / a[i]; num %= a[i]; if (b[i] == 0) b[i] = 0; else if (b[i] != a[i + 1] / a[i] - 1) b[i] = 2; else b[i] = 1; } dp[0][0] = 1; dp[0][1] = 0; for (int i = 1; i < n; i++) { if (b[i] == 0) { dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][1]; } else if (b[i] == 1) { dp[i][0] = dp[i - 1][0]; dp[i][1] = dp[i - 1][0] + dp[i - 1][1]; } else { dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0] + dp[i - 1][1]; } } cout << dp[n - 1][0] + dp[n - 1][1] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rep1(i,n) for(ll i=1;i<=(ll)(n);i++) int main(){ ios::sync_with_stdio(false); cin.tie(0); ll n,x; cin >> n >> x; vector<ll> a(n); rep(i,n) cin >> a[i]; ll dp[2]; dp[0] = 1; dp[1] = 0; ll tx = x; rep(i,n-1){ ll amari = tx%a[i];//前の+ ll amari_minus = amari - a[i];//前の- ll amari_now = tx%a[i+1]; ll amari_now_minus = amari_now - a[i+1]; // cout << amari <<" " << amari_minus << " " << amari_now << " " << amari_now_minus << endl; ll ndp[2]; if(abs(amari-amari_now)<=a[i+1]-a[i]){ //dp[0]そのまま ndp[0] = dp[0]; } else{ ndp[0] = 0; } if(abs(amari_minus-amari_now_minus)<=a[i+1]-a[i]){ ndp[1] = dp[1]; } else ndp[1] = 0; if(abs(amari_minus-amari_now)<=a[i+1]-a[i]) ndp[0] += dp[1]; if(abs(amari-amari_now_minus)<=a[i+1]-a[i]) ndp[1] += dp[0]; dp[0]= ndp[0]; dp[1]= ndp[1]; // cout << dp[0] <<" " << dp[1] << endl; } cout << dp[0] + dp[1] << endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define pb push_back #define mkp make_pair #define vi vector<int> #define pii pair<int,int> #define FI(n) FastIO::read(n) #define FO(n) FastIO::write(n) #define ull unsigned long long #define mst(a,b) memset(a,b,sizeof(a)) #define foR(i,k,j) for(int i=(k);i>=(j);i--) #define For(i,k,j) for(int i=(k);i<=(j);i++) #define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v) #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define Fin(s) freopen(s,"r",stdin) #define Fout(s) freopen(s,"w",stdout) #define file(s) Fin(s".in"),Fout(s".out") #define INF ((1ll<<60)-1) #define int long long const int P=998244353; // using namespace std; template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);} template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);} inline int mul(int a,int b) {return 1ull*a*b%P;} inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;} inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;} inline void mulmod(int &a,int b) {a=mul(a, b);} inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);} inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);} inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;} inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%d%c",x,c);} inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%d %d%c",x.first,x.second,c);} inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);} inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);} inline int inv(int a) {return ksm(a,P-2);} namespace FastIO { const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt; int read(char *s) { while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;} int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn; } bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;} void write(int x) { if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];} if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;} } void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}} void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;} }; inline int read() {int x; FI(x); return x;} const int MN=5e3+5; int n,m,a[MN],pw[MN][MN]; signed main() { #ifndef ONLINE_JUDGE freopen("pro.in","r",stdin); freopen("pro.out","w",stdout); #endif n=read(),m=read(); For(i,0,MN-5) { pw[i][0]=1; For(j,1,MN-5) pw[i][j]=mul(pw[i][j-1],i); } int ans=mul(ksm(m,n),n); For(i,2,n) { // len For(j,1,m) { // now submod(ans,mul(n-i+1,mul(pw[j-1][i-2],pw[m][n-i]))); } } cout<<ans<<endl; return FastIO::Fflush(),0; } /* */
#include <bits/stdc++.h> #define i64 long long #define sz(a) int ((a).size()) #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define per(i, a, b) for (int i = (b) - 1; i >= (a); --i) using namespace std; const int M = 998244353; struct mint { int x; mint (int x = 0) : x(x) {} friend mint operator + (const mint a, const mint &b) { int x = a.x + b.x; return mint (x >= M ? x - M : x); } friend mint operator - (const mint a, const mint &b) { int x = a.x - b.x; return mint (x < 0 ? x + M : x); } friend mint operator * (const mint a, const mint &b) { return mint (1ll * a.x * b.x % M); } }; int Pw(int a, int x = M - 2, int res = 1) { for (; x; x >>= 1, a = (mint (a) * a).x) if (x & 1) res = (mint (res) * a).x; return res; } const int N = 5000; int n, m, pw[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; pw[0] = 1; rep(i, 1, n) { pw[i] = (mint (pw[i - 1]) * m).x; } int res = (mint (n) * Pw(m, n)).x; rep(x, 0, m) { int px = 1; rep(y, 0, n - 1) { res = (res - mint (n - 1 - y) * px * pw[n - 2 - y]).x; px = (mint (px) * x).x; } } cout << res << '\n'; return 0; } /* * Read Carefully! Multiple cases? Data range? * Transformation? Special cases? Brute & Regularities? * Don't get stuck! New directions! Record thoughts! * Int overflow? Array bounds? N = 1? */
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; constexpr int inf=1e9+7; constexpr ll longinf=1LL<<60 ; constexpr ll mod=1e9+7 ; struct Edge{ int to; ll t; ll k; }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m,s,t; cin>>n>>m>>s>>t; --s;--t; vector<vector<Edge>> v(n); rep(i,m){ int x,y,z,w; cin>>x>>y>>z>>w; --x;--y; v[x].push_back({y,z,w}); v[y].push_back({x,z,w}); } vector<ll> dist(n, longinf); using P = pair<ll,int>; priority_queue<P, vector<P>, greater<P>> pq; pq.emplace(0,s); dist[s]=0; while(pq.size()){ auto p = pq.top();pq.pop(); ll d = p.first, x = p.second; if(d>dist[x])continue; for(auto e: v[x]){ ll nd = (d+e.k-1)/e.k*e.k + e.t; if(dist[e.to] > nd){ dist[e.to] = nd; pq.emplace(nd,e.to); } } } if(dist[t]>=longinf)cout<<-1<<endl; else cout<<dist[t]<<endl; return 0; }
#define fs first #define sc second #include <bits/stdc++.h> using namespace std; typedef pair<long long, int> pii; const int maxn = 1e5 + 10; int n, m, x, y, a, b, t, k; struct edge{ int to, t, k; }; vector<edge> G[maxn]; priority_queue<pii, vector<pii>, greater<pii>> pq; bool vis[maxn]; long long dis[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> x >> y; while (m--) { cin >> a >> b >> t >> k; G[a].push_back({b, t, k}); G[b].push_back({a, t, k}); } memset(vis, false, sizeof(vis)); memset(dis, 0x3f, sizeof(dis)); dis[x] = 0; pq.emplace(0, x); while (pq.size()) { int u = pq.top().sc; long long tt = pq.top().fs; pq.pop(); if (vis[u]) continue; vis[u] = true; for (edge e: G[u]) { long long ttt = tt + (e.k - tt % e.k) % e.k + e.t; if (ttt < dis[e.to]) dis[e.to] = ttt, pq.emplace(ttt, e.to); } } if (dis[y] == 0x3f3f3f3f3f3f3f3fLL) cout << "-1\n"; else cout << dis[y] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using vin=vector<int>; using vll=vector<long long>; using vvin=vector<vector<int>>; using vvll=vector<vector<long long>>; using vstr=vector<string>; using vvstr=vector<vector<string>>; using vch=vector<char>; using vvch=vector<vector<char>>; using vbo=vector<bool>; using vvbo=vector<vector<bool>>; using vpii=vector<pair<int,int>>; using pqsin=priority_queue<int,vector<int>,greater<int>>; #define mp make_pair #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,s,n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(),v.end() #define decp(n) cout<<fixed<<setprecision((int)n) const ll inf=1e9+7; const ll INF=1e18; const ll mod=99824453; vin dx={-1,0,1,0}; vin dy={0,1,0,-1}; ll modpow(ll a,ll n,ll m=inf){ ll res=1; while(n){ if(n&1)res=res*a%m; a=a*a%m; n>>=1; } return res; } int main(){ ll h,w;cin>>h>>w; vstr s(h);rep(i,h)cin>>s[i]; vvin up(h,vin(w)),down(h,vin(w)),right(h,vin(w)),left(h,vin(w)); int tmp=-1;ll k=h*w; rep(i,h){ tmp=-1; rep(j,w){ left[i][j]=tmp; if(s[i][j]=='#'){ tmp=j; k--; } } tmp=w; for(int j=w-1;j>=0;j--){ right[i][j]=tmp; if(s[i][j]=='#')tmp=j; } } rep(j,w){ tmp=-1; rep(i,h){ up[i][j]=tmp; if(s[i][j]=='#')tmp=i; } tmp=h; for(int i=h-1;i>=0;i--){ down[i][j]=tmp; if(s[i][j]=='#')tmp=i; } } ll all=modpow((ll)2,k),light,can; ll ans=(ll)0; rep(i,h)rep(j,w){ if(s[i][j]=='.'){ light=(down[i][j]-up[i][j]-(ll)1)+(right[i][j]-left[i][j]-(ll)1)-(ll)1; can=all-modpow((ll)2,k-light); if(can<0)can+=inf; ans+=can;ans%=inf; } } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*b)/gcd(a,b) #define ff first #define ss second #define int long long #define float double #define pb emplace_back #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define vc vector<char> #define vb vector<bool> #define vf vector<float> #define vs vector<string> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int>> #define hashmap unordered_map #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf INT64_MAX #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) #define all(x) x.begin(),x.end() #define digits(x) floor(log10(x))+1 #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) template<class T> void printV(vector<T> a) {for (auto v : a)cout << v << " ";} template<class T> void readV(vector<T>& a) {int n = a.size(); for (int i = 0 ; i < n ; i++)cin >> a[i];} template<class T> void printA(T* a , int n) {for (int i = 0; i < n ; i++)cout << a[i] << " ";} template<class T> void readA(T* a , int n) {for (int i = 0 ; i < n ; i++)cin >> a[i];} auto clk = clock(); mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); void run_time() {cerr << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;} void aryan_pandeya() { #ifndef ONLINE_JUDGE freopen("input.txt" , "r" , stdin) ; freopen("output.txt" , "w" , stdout) ; freopen("error.txt" , "w" , stderr) ; #endif } void solve() { int n , m ; cin >> n >> m; if (m == 0) { cout << 1; return; } vi a(m); for (int i = 0 ; i < m ; i++) { cin >> a[i]; } sort(all(a)); vi gaps; if (a[0] - 1 != 0) { gaps.pb(a[0] - 1); } for (int i = 1 ; i < m ; i++) { if (a[i] - a[i - 1] - 1 != 0) { gaps.pb(a[i] - a[i - 1] - 1); } } if (n - a[m - 1] != 0) { gaps.pb(n - a[m - 1]); } if (gaps.size() == 0) { cout << 0; return; } // printV(gaps); // cout << '\n'; int val = *min_element(all(gaps)); int ans = 0; for (auto v : gaps) { ans += (ceil((v * 1.0) / val)); } cout << ans; } int32_t main() { aryan_pandeya(); FIO; solve(); run_time(); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back #define fr first #define sc second const ll mod = 1e9+7; const int N = 2e5+5; void solve(){ string s; cin>>s; string ss = "atcoder"; if(ss<s){ cout<<0<<endl; return ; } int x = -1; int n = s.size(); for(int i=1;i<n;i++){ if(s[i]!='a'){ x = i; if(s[i]>'t')x--; break; } } cout<<x<<endl; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t = 1; cin>>t; while(t--)solve(); return 0; }
#include<stdio.h> #include<string.h> #define rep(i,N) for(int i=0;i<(int)N;i++) static inline int Max(size_t a,size_t b){return a>b?(int)a:(int)b;} int main() { int T;if(!scanf("%d",&T)){return -1;} while(T--) { char S[1001]; if(!scanf("%s",S)){return -1;} if(strncmp("atcoder",S,Max(strlen(S),7))<0){puts("0");continue;} else { if(S[0]=='a'&&S[1]>'a'){puts("1");continue;} rep(i,(int)strlen(S)-1) { if(S[i+1]>'t'){printf("%d\n",i);goto jump;} if(S[i+1]>'a'){printf("%d\n",i+1);goto jump;} } puts("-1");jump:continue; } } }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<(n); i++) typedef long long ll; typedef pair<int, int> P; const int MAX_N = 2010; int n, m; vector<int> g[MAX_N]; bool check[MAX_N]; int ans = 0; void dfs(int s){ ans++; check[s] = true; for(auto next : g[s]){ if(!check[next]){ dfs(next); } } } void solve(){ //dfsでいけるところまでいく rep(i, n){ rep(j, n) check[j] = false; dfs(i); } cout << ans << endl; } int main(void){ cin >> n >> m; rep(i, m){ int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); } solve(); return 0; }
#include <bits/stdc++.h> /* * autor:NEU-Yuan * Email:yuanyanghao2022@163.com * Time: 2021/04/12 10:53:09 */ #define pcc pair<char, char> #define pii pair<int, int> #define vi vector<int> #define vl vector<ll> #define rep(i, x, y) for (int i = x; i < y; i++) #define per(i, x, y) for (int i = x; i >= y; i--) #define rep0(i, n) for (int i = 0; i < (n); i++) #define per0(i, n) for (int i = (n)-1; i >= 0; i--) #define mp make_pair #define pb push_back #define F first #define S second #define sz(x) (x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ull unsigned long long #define db double #define ld long double 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 + ch - '0'; ch = getchar(); } return x * f; } const double eps = 1e-9; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 6e6 + 10; const int MAX = 1e5; const double pi = acos(-1.0); const ll INF = 0x3f3f3f3f3f3f3f; ll qpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res % mod; } /***************main****************/ ll T = 1; ll m, n; int main() { int h, w, x, y, cnt = 0; cin >> h >> w >> x >> y; string s[105]; for (int i = 1; i <= h; i++) { cin >> s[i]; s[i] = "#" + s[i]; } for (int i = y - 1; i >= 1; i--) if (s[x][i] == '#') break; else cnt++; for (int i = y; i <= w; i++) if (s[x][i] == '#') break; else cnt++; for (int i = x + 1; i <= h; i++) if (s[i][y] == '#') break; else cnt++; for (int i = x - 1; i >= 1; i--) if (s[i][y] == '#') break; else cnt++; cout << cnt << endl; return 0; }
#include <bits/stdc++.h> #include<cmath> #include<string> #define pb push_back #define x first #define y second #define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define all(x) x.begin(),x.end() #define int long long #define ll long long using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} //void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // look for all edge cases //search for a pattern const int mod = 1e9+7; int power(int a, int b){ int ans = 1; while(b > 0){ if(b%2 == 1){ ans = (a*ans)%mod; } a = (a*a)%mod; b = b/2; } return ans; } void solve(){ int n,p; cin >> n >> p; //debug(power(2, 3), power(2, 4), power(2, 5)); if(n == 1){ cout << p - 1; return ; } else{ cout << ((p-1) * power(p-2, n-1))%mod; } } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #include <climits> #include <cstring> #include <cassert> using namespace std; //using namespace atcoder; #define REP(i, n) for (int i=0; i<(n); ++i) #define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i) #define FOR(i, a, n) for (int i=(a); i<(n); ++i) #define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define DUMP(x) cerr<<#x<<" = "<<(x)<<endl #define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl; template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; REP(i, SZ(v)) { if (i) os << ", "; os << v[i]; } return os << "]"; } template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << " " << p.second << ")"; } template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; const ll MOD = 1e9 + 7; const int INF = INT_MAX / 2; const ll LINF = LLONG_MAX / 2; const ld eps = 1e-9; // edit template<int m> struct mint { int x; mint(ll x = 0) : x(((x % m) + m) % m) {} mint operator-() const { return x ? m - x : 0; } mint &operator+=(mint r) { if ((x += r.x) >= m) x -= m; return *this; } mint &operator-=(mint r) { if ((x -= r.x) < 0) x += m; return *this; } mint &operator*=(mint r) { x = ((ll) x * r.x) % m; return *this; } mint inv() const { return pow(m - 2); } mint &operator/=(mint r) { return *this *= r.inv(); } friend mint operator+(mint l, mint r) { return l += r; } friend mint operator-(mint l, mint r) { return l -= r; } friend mint operator*(mint l, mint r) { return l *= r; } friend mint operator/(mint l, mint r) { return l /= r; } mint pow(ll n) const { mint ret = 1, tmp = *this; while (n) { if (n & 1) ret *= tmp; tmp *= tmp, n >>= 1; } return ret; } friend bool operator==(mint l, mint r) { return l.x == r.x; } friend bool operator!=(mint l, mint r) { return l.x != r.x; } friend ostream &operator<<(ostream &os, mint a) { return os << a.x; } friend istream &operator>>(istream &is, mint &a) { ll x; is >> x; a = x; return is; } }; using Mint = mint<MOD>; void solve() { int N, P; cin >> N >> P; Mint ans = Mint(P - 1) * Mint(P - 2).pow(N - 1); cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); // std::ifstream in("input.txt"); // std::cin.rdbuf(in.rdbuf()); solve(); return 0; }
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #include<bits/stdc++.h> #define ll long long #define pb push_back #define bg begin() #define en end() #define endl "\n" #define vvl(n,m) vector<vector<ll> > a( n , vector<ll> (m)) #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define mod 1000000007 using namespace std; ll power(ll x, ll y, ll m) { if (y == 0) return 1; ll p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } ll modInverse(ll a, ll m) { return power(a, m - 2, m); } ll knapsack(vector<vector<ll>>&dp,ll x,ll i,ll n,ll s[],ll h[]){ ll r,l; if(i>=n || x==0) return 0; if(dp[i][x]!=-1) return dp[i][x]; if(x>=h[i]){ l=s[i]+knapsack(dp,x-h[i],i+1,n,s,h); r=knapsack(dp,x,i+1,n,s,h); return dp[i][x]=max(l,r); } else return dp[i][x]=knapsack(dp,x,i+1,n,s,h); } void solve() { ll n,i,p=1,s,t; cin>>n; ll a[n],j; for(i=0;i<n;i++){ cin>>a[i]; } vector<ll> v; for(i=0;i<n-1;i++){ if(a[i]>a[i+1] && a[i+1]>i+1){ p=0; break; } else if(a[i]>a[i+1]){ for(j=i+1;j>=a[i+1];j--){ v.push_back(j); } a[i+1]=a[i]; } } if(p==0 || v.size()!=n-1) cout<<"-1"; else{ s=v.size(); for(i=0;i<s;i++){ cout<<v[i]<<endl; } } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast solve(); #ifndef ONLINE_JUDGE cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n"; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define SPEED ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ll long long #define pb push_back #define pll pair<ll,ll> #define ff first #define ss second #define INF 1000000000000000 #define M 1000000007 void solve() { ll n; cin>>n; ll i,j,k,ans=0; ll power[n],speed[n],technique[n],knowledge[n],invent[n]; ll a1=0,b1=0,c1=0,d1=0,e1=0; for(i=0;i<n;i++) { cin>>power[i]>>speed[i]>>technique[i]>>knowledge[i]>>invent[i]; a1=max(a1,power[i]); b1=max(b1,speed[i]); c1=max(c1,technique[i]); d1=max(d1,knowledge[i]); e1=max(e1,invent[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { ll a[5]={0}; ll tempa=max(power[i],power[j]); ll tempb=max(speed[i],speed[j]); ll tempc=max(technique[i],technique[j]); ll tempd=max(knowledge[i],knowledge[j]); ll tempe=max(invent[i],invent[j]); ll mini=min(tempa,min(tempb,min(tempc,min(tempd,tempe)))); a[0]=tempa; a[1]=tempb; a[2]=tempc; a[3]=tempd; a[4]=tempe; ll p=0,sum=0; if(mini==tempa) { p++; sum=a1; } if(mini==tempb) { p++; sum=b1; } if(mini==tempc) { p++; sum=c1; } if(mini==tempd) { p++; sum=d1; } if(mini==tempe) { p++; sum=e1; } if(p==1) { sort(a,a+5); ans=max(ans,min(sum,a[1])); } else { ans=max(ans,mini); } } } cout<<ans; } int main() { SPEED; ll t; //cin>>t; t=1; for(ll ii=1;ii<=t;ii++) { //cout<<"Case #"<<ii<<": "; solve(); } }
#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; long double pi = acos(-1); const int INF = 1001001001; const int N_MAX = 100001; const int modint = 1000000007; // 最小公倍数を求める int euqlid (int a, int b){ int temp; temp = a % b; if(temp == 0)return b; return euqlid(b,temp); } ll conbination(ll a,ll b){ ll u = 1; ll d = 1; while(b != 0){ u *= a; d *= b; a--; b--; } return u / d; } int strtoint(char s){ return (int(s-'0')); } char changeS(char alpha){ if(0x41 <= alpha and alpha <= 0x5A)return (alpha + 0x20); else if(0x61 <= alpha and alpha <= 0x7A)return (alpha - 0x20); return alpha; } int fact(int n){ if(n == 1)return 1; return n * fact(n-1); } vector<long long> divisor(long long n) { vector<long long> ret; for (long long 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(){ struct member{ int num; int rate; }; int n; cin >> n; vector<member> a; member temp; int r; rep(i,pow(2,n)){ cin >> r; temp.num = i+1; temp.rate = r; a.push_back(temp); } while(a.size() > 2){ for(int i = 0;i < pow(2,n-1);i++){ if(a[i].rate < a[i+1].rate){ a.erase(a.begin()+i); } else{ a.erase(a.begin()+i+1); } } n -= 1; // printf("%d\n",a.size()); } if(a[0].rate > a[1].rate){ cout << a[1].num << endl; } else{ cout << a[0].num << endl; } }
#include<bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, 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 long long INF = 1LL << 60; int power(int n, int exp){ int x = 1; for (int i = 0; i < exp; ++i){ x *= n; } return x; } int main(){ int N; cin >> N; int NUMMAX = power(2, N); vector<int> A(NUMMAX); for (int i = 0; i < NUMMAX; ++i) cin >> A[i]; vector<bool> isAlive(NUMMAX, true); int l,r; for (int i = 0; i < N; ++i){ l = 0; r = -1; int num = power(2, N - i); for (int j = 0; j < num/2 ; ++j){ for(l = r + 1; isAlive[l] == false && l < NUMMAX; ++l); for(r = l + 1; isAlive[r] == false && r < NUMMAX; ++r); if(A[l] < A[r]) isAlive[l] = false; else isAlive[r] = false; } } if(isAlive[l]) cout << r+1 << endl; else cout << l+1 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define _rep(i, x, y) for(int i = (int)x; i < (int)y; ++i) #define _dep(i,x,y) for(int i = (int)x; i > (int)y; i--) #define PII pair<int,int> #define eb emplace_back #define pb push_back #define fi first #define se second #define PQ priority_queue #define lb lower_bound #define ub upper_bound #define all(x) (x).begin(), (x).end() typedef long long ll; typedef vector<int> VI; typedef vector<VI> VII; typedef vector<ll> VL; typedef vector<vector<ll>> VLL; constexpr int mod = 1e9 + 7; constexpr int KINF = 0x3f3f3f3f; constexpr double eps = 1e-7; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; VI a(n); _rep(i, 0, n) cin >> a[i]; VI b(a.begin(), a.begin() + m); sort(all(b)); if(b[0] != 0) { cout << 0 << endl; return 0; } int ans = b.back() + 1; unordered_map<int,int> mp; mp[b[0]] ++; bool ok = false; _rep(i, 1, m) { mp[b[i]] ++; if(ok) continue; if(b[i] > b[i - 1] + 1) { ans = b[i - 1] + 1; ok = true; } } _rep(i, 0, n - m){ mp[a[i]] -- ; mp[a[i + m]] ++; if(a[i] < ans){ if(mp[a[i]] == 0) ans = a[i]; } } cout << ans << endl; return 0; } // 小心左移运算符导致的int溢出 // dp记得处理非法状态 // resize并不会初始化数组 // 全局函数一定要记得inline // 带空格字符串不能直接cin,要用getline
#include <bits/stdc++.h> signed main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::string s; std::cin >> n >> s; int ans = 0; for (int i = 0; i < n; i++) { int at = 0, gc = 0; for (int j = i; j < n; j++) { if (s[j] == 'A') ++at; else if (s[j] == 'T') --at; else if (s[j] == 'G') ++gc; else --gc; ans += !(at || gc); } } std::cout << ans; return 0; }
// Nurstan Duisengaliev(REALBOY) // не, не надо меня узнавать /*#pragma GCC target ("avx2") #pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("O3")*/ #include <bits/stdc++.h> #define ll long long #define all(x) x.begin(), x.end() #define in insert #define mp make_pair #define F first #define S second #define ppf pop_front #define pb push_back #define ppb pop_back #define pf push_front #define pii pair <int, int> #define pll pair <ll, ll> #define boost() ios_base::sync_with_stdio(0), cin.tie(0) #define sz(x) (int)x.size() using namespace std; const int N = (int)2e5 + 123; const int mod = 998244353; const ll INF = (ll)1e18 + 1; int n, a[N]; vector <int> g[N]; int used[N]; int par[N]; int kol = 0; bool check = 0; void Dfs (int v) { used[v] = 1; for (auto to : g[v]) { if (!used[to]) { Dfs (to); } else if (used[to] == 1) { check = 1; } } used[v] = 2; } void solve () { cin >> n; for (int i = 1; i <= n; i ++) cin >> a[i]; for (int i = 1; i <= n; i ++) { g[i].pb (a[i]); } for (int i = 1; i <= n; i ++) { if (!used[i]) { check = 0; Dfs (i); kol += check; } } ll ans = 1; for (int i = 1; i <= kol; i ++) { ans *= 2; ans %= mod; } cout << (ans + mod - 1) % mod; } main () { // freopen (".in", "r", stdin); // freopen (".out", "w", stdout); boost (); int TT = 1; // cin >> TT; while (TT --) { solve (); } return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using ll = int64_t; using ull = uint64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} ll pr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; //head int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vi x(n); rep(i, n) cin >> x[i]; ll ans = INT64_MAX; rep(i, 1<<15) { ll now = 1; bool ok2 = true; rep(j, n) { bool ok = false; rep(k, 15) if(i>>k&1 and x[j]%pr[k] == 0) ok = true; if(not ok) ok2 = false; } if(ok2) { ll u = 1; rep(k, 15) if(i>>k&1) u *= pr[k]; chmin(ans, u); } } cout << ans << endl; }
// #include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <map> #include <queue> #include <set> #include <tuple> #include <cmath> #include <iomanip> #include <numeric> #define ll long long int #define rep(i, n) for(ll i = 0; i < (ll)(n); i++) #define drep(i, n) for(ll i = ((ll)(n) - 1); i >= 0; i--) #define frep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); i++) #define vall(v) (v).begin(),(v).end() #define mod 1000000007 #define inf 9223372036854775800 using namespace std; ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// int main() { ll n; cin >> n; vector<ll> a(n); rep(i,n){ cin >> a[i]; } ll ans = 0; ll x = 0; map<ll,ll> s; s[0] = 1; rep(i,n){ if(i % 2 == 0){ x += a[i]; }else{ x -= a[i]; } if(s.count(x)){ ans += s[x]; s[x]++; // cout << i << " " << x << endl; }else{ s[x] = 1; } } cout << ans << endl; return 0; } //////////////////////////////////////////////////
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define N 200100 #define MOD 1000000007 //998244353 #define ll long long #define rep(i, n) for(int i = 0; i < n; ++i) #define rep2(i, a, b) for(int i = a; i <= b; ++i) #define rep3(i, a, b) for(int i = a; i >= b; --i) #define eb emplace_back #define pb push_back #define all(c) (c).begin(), (c).end() #define vi vector<int> #define pii pair<int,int> #define pll pair<ll,ll> struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); } } setup_io; int main() { int n; ll a[N]; ll b[N]; ll c[N]; ll x = 0; cin >> n; rep(i, n) { cin >> a[i]; } rep(i, n) { cin >> b[i]; } rep(i, n) { x = max(x, a[i]); if (i == 0)c[0] = a[0] * b[0]; else c[i] = max(c[i - 1], x*b[i]); } rep(i, n) { cout << c[i]<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12), 0); int solve(string S) { if ("atcoder" < S) { return 0; } for (int i = 0; i < S.size(); i++) { if (S[i] > 't') { return i - 1; } else if (S[i] > 'a') { return i; } } return -1; } int main() { int T; cin >> T; while (T--) { string S; cin >> S; cout << solve(S) << '\n'; } return 0; }
#pragma GCC optimize(2) #include<iostream> #include<unordered_map> #include<algorithm> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<queue> #include<stack> #include<cmath> #include<map> #include<set> #define Buff ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define rush() int Case = 0; int T; cin >> T; while(T--) #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 reps(i, a, b) for(int i = a; b; i ++) #define clc(a, b) memset(a, b, sizeof(a)) #define Buff ios::sync_with_stdio(false) #define readl(a) scanf("%lld", &a) #define readd(a) scanf("%lf", &a) #define readc(a) scanf("%c", &a) #define reads(a) scanf("%s", a) #define read(a) scanf("%d", &a) #define lowbit(n) (n&(-n)) #define pb push_back #define sqr(x) x*x #define lson rt<<1 #define rson rt<<1|1 #define ls lson, l, mid #define rs rson, mid+1, r #define y second #define x first using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int>PII; const int mod = 1e9+7; const double eps = 1e-6; const int N = 1e6+7; string s; string t = "atcoder"; bool judge() { reps(i, 0, s[i]) if(s[i] != 'a') return false; return true; } int main() { int T; cin >> T; while(T --) { cin >> s; if(judge()) { puts("-1"); continue; } int res = mod; for(int i = 0; s[i]; i ++) { for(int j = i; s[j]; j ++) { swap(s[i], s[j]); if(s > t) { res = min(res, j-i); swap(s[i], s[j]); break; } swap(s[i], s[j]); } } cout << res <<endl; } return 0; } /* aaaaaaatcoder */
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(0);cin.tie(0) #define fp(i,a,b) for(int i=a ; i<b ; i++) #define fn(i,a,b) for(int i=a ; i>=b ; i--) #define pb push_back #define ff first #define ss second #define all(x) x.begin(),x.end() typedef long long ll; typedef pair<int,int> ii; typedef vector<ii> vii; typedef vector<int> vi; typedef vector<ll> vll; const int M=1e5+5; const int INF=1e9; const int MOD=1e9+7; void go(){ int n; cin >> n; string s; cin >> s; stack <char> st; vector <char> v; fp(i,0,n){ if (st.size() < 2 or s[i] != 'x') st.push(s[i]); else { char x = st.top(); st.pop(); char y = st.top(); if (x == 'o' and y == 'f') st.pop(); else{ st.push(x); st.push(s[i]); } } } cout << st.size() << "\n"; } int main(){ fastio; go(); return 0; }
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define all(x) x.begin(),x.end() #define pb emplace_back #define pob pop_back() #define ms0(s) memset(s,0,sizeof s) #define ff first #define ss second typedef long long int ll; #define vll vector<ll> #define vpll vector<pair<ll,ll>> #define vvll vector<vector<ll>> #define pll pair<ll,ll> #define prq priority_queue<ll> //max-heap #define psq priority_queue<ll,vector<ll>,greater<ll>> //min-heap #define m_p make_pair #define vcc vector<char> #define vss vector<string> #define bs binary_search #define lb lower_bound #define ub upper_bound #define setbit(n) __builtin_popcountll(n) int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t = 1; // cin>>t; while (t--) { ll i, j, k, n, m, ans = 0; cin >> n; string s; cin >> s; for (i = 0; i < n; i++) { map<char, ll>mc; for (j = i; j < n; j++) { mc[s[j]]++; if (mc['A'] == mc['T'] && mc['C'] == mc['G']) ans++; } } cout << ans; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; using i64 = int64_t; using pii = pair<i64, i64>; #define rep(i, n) for (i64 i = 0; i < (i64) n; i++) #define per(i, n) for (i64 i = n - 1; i >= 0; i--) #define REP(i, a, b) for (i64 i = a; i < (i64) b; i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define fi first #define se second #ifdef LOCAL #define dbg(...) cerr<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debug(__VA_ARGS__) #else #define dbg(...) #endif template <typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (auto& x : vec) { os << x << ' '; } return os; } template <typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& mat) { os << endl; for (auto& vec : mat) { os << vec << endl; } return os; } template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } void debug() { cerr << endl; } template <typename Head, typename... Tail> void debug(Head H, Tail... T) { cerr << " " << H; debug(T...); } 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 = (1 << 30) - 1; const i64 LINF = (1LL << 62) - 1; void solve() { int n, k; cin >> n >> k; vector<vector<int>> G(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } vector<int> dp(n), dp2(n); auto dfs = [&](auto self, int& t, int v = 0, int p = -1) -> int { int ret = 0; for (int& u : G[v]) { if (u == p) continue; ret += self(self, t, u, v); chmax(dp[v], dp[u] + 1); chmin(dp2[v], dp2[u] + 1); } if (dp[v] + dp2[v] <= t) dp[v] = -INF; if (dp[v] == t) { ret++; dp[v] = -INF; dp2[v] = 0; } else if (p == -1 && dp[v] >= 0) { ret++; } return ret; }; auto check = [&](int t) -> bool { fill(all(dp), 0); fill(all(dp2), INF); int cnt = dfs(dfs, t); return cnt <= k; }; int ok = n - 1, ng = -1; while (ok - ng > 1) { int mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } cout << ok << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; using ULL = unsigned long long; using VI = vector<int>; using VL = vector<LL>; using PII = pair<int, int>; using PLL = pair<LL, LL>; #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define MP make_pair #define PB push_back #define EB emplace_back #define F first #define S second #define FOR(i, a, b) for (int i = (a); i<(b); ++i) #define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i) #define FILL(a, b) memset(a, b, sizeof(a)) void dout() { cerr << endl; } template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } int dp[101][101][101]; void relax(int& val, int nval) { if (val == -1) val = nval; val = max(val, nval); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; LL x; cin >> n >> x; VI a(n); FOR(i, 0, n) { cin >> a[i]; } LL ans = x - *max_element(ALL(a)); FOR(k, 2, n + 1) { FILL(dp, -1); dp[0][0][0] = 0; FOR(i, 0, n) { FOR(rem, 0, k) { FOR(cnt, 0, k + 1) { if (dp[i][rem][cnt] == -1) continue; relax(dp[i + 1][rem][cnt], dp[i][rem][cnt]); relax(dp[i + 1][(rem + a[i]) % k][cnt + 1], dp[i][rem][cnt] + a[i]); } } } LL sum = dp[n][x % k][k]; if (sum != -1) { assert((x - sum) % k == 0); ans = min(ans, (x - sum) / k); } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')w=-1;ch=getchar(); } while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline bool read(int& a){ int s=0,w=1; char ch=getchar(); if(ch==EOF){ return false; } while(ch<'0'||ch>'9'){ if(ch=='-')w=-1;ch=getchar(); } while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); a=s*w; return true; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } inline void writeln(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); puts(""); } const int MOD=998244353; int n,k; int f[3010][3010]; signed main(){ read(n),read(k); f[1][1]=1; for(int i=2;i<=n;i++){ for(int j=i;j>=1;j--){ f[i][j]=(f[i-1][j-1]+f[i][j*2])%MOD; } } writeln(f[n][k]%MOD); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for(ll i = 0; i < (n); i++) #define REP1(i, n) for(ll i = 1; i <= (n); i++) #define REPD(i,a,b) for (ll i=(a);i<=(b);i++) #define ALL(v) (v).begin(), (v).end() using namespace std; typedef long long ll; typedef double d; typedef long double ld; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<d> vd; typedef vector<ld> vld; typedef set<int> si; typedef vector<si> vsi; typedef vector<ll> vll; typedef vector<vll> vvll; typedef pair<int, int> pi; typedef queue<int> qi; typedef queue<pi> qpi; typedef pair<ll, ll> pll; typedef queue<pll> qpll; typedef vector<pi> vpi; typedef vector<pll> vpll; const int MOD = 1000000007; const int INF = 1001001001; // 小数点 << fixed << setprecision(10) << // sort降順 sort(ALL(),greater<int>()); // 円周率 M_PI // 文字判定 isupper islower // 順列 do {} while(next_permutation(ALL(X))); // 最大値 LLONG_MAX // a内でx以上 auto iter = lower_bound(ALL(a), x); // a内でxより大きい auto iter = upper_bound(ALL(a), x); int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; struct edge { int from; //出発点 int to; //到達点 int cost; //移動コスト }; typedef struct edge se; typedef vector<edge> ve; // 重複した要素を取り除いて返す vll deleteDuplication(vll &numberlist) { sort(ALL(numberlist)); numberlist.erase(unique(ALL(numberlist)),numberlist.end()); return numberlist; } // 2つの数の最大公約数を求める unsigned Euclidean_gcd(unsigned a, unsigned b) { if(a < b) return Euclidean_gcd(b, a); unsigned r; while ((r=a%b)) { a = b; b = r; } return b; } // 各桁の数を合計 int digit_sum(int n) { int sum=n%10; while(n!=0) { n/=10; sum+=n%10; } return sum; } // 素因数分解してペアのリストを返す vpll PrimeFactorization(ll n) { vpll res; for (ll i=2; i*i<=n; i++) { if(n%i!=0) continue; ll ex=0; while(n%i==0) { ex++; n/=i; } res.push_back(pll(i,ex)); } if(n!=1) res.push_back(pll(n,1)); return res; } int main() { int h,w; cin >> h >> w; char s[h+1][w+1]; vvll dp(h+1,vll(w+1,0)),X(h+1,vll(w+1,0)),Y(h+1,vll(w+1,0)),Z(h+1,vll(w+1,0)); REP1(i,h) { REP1(j,w) { cin >> s[i][j]; } } dp[1][1]=1; REP1(i,h) { REP1(j,w) { if(i==1&&j==1) continue; if(s[i][j]=='#') continue; X[i][j]=X[i][j-1]+dp[i][j-1]%MOD; Y[i][j]=Y[i-1][j]+dp[i-1][j]%MOD; Z[i][j]=Z[i-1][j-1]+dp[i-1][j-1]%MOD; dp[i][j]=(X[i][j]+Y[i][j]+Z[i][j])%MOD; } } cout << dp[h][w] << endl; return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using namespace std; const ll N=2e5+10,P=998244353; ll n,m,f[20][N],fac[N],inv[N],ans; ll C(ll n,ll m) {return fac[n]*inv[m]%P*inv[n-m]%P;} signed main() { scanf("%lld%lld",&n,&m); ll k=0;inv[1]=1; for(ll i=2;i<=n;i++) inv[i]=P-inv[P%i]*(P/i)%P; fac[0]=inv[0]=1; for(ll i=1;i<=n;i++) fac[i]=fac[i-1]*i%P,inv[i]=inv[i-1]*inv[i]%P; while((1<<(k+1))<=m)k++;k=min(k,n-1); for(ll i=1;i<=m;i++)f[1][i]=1; for(ll i=1;i<=k;i++) for(ll j=1;j<=m;j++){ if(!f[i][j])continue; for(ll k=2*j;k<=m;k+=j) (f[i+1][k]+=f[i][j])%=P; } for(ll i=1;i<=k+1;i++) for(ll j=1;j<=m;j++) (ans+=f[i][j]*C(n-1,i-1)%P)%=P; printf("%lld\n",ans); return 0; }
#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--) using namespace std; typedef unsigned long long ull; typedef pair <int, int> pii; typedef long long ll; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 1e5 + 5; int a[N], b[N], f[2][N]; int n, len[2]; ll ans; int main() { memset(f, -0x3f, sizeof(f)); read(n); for (int i = 1; i <= n; i++) read(a[i]), ans += a[i]; for (int i = 1; i <= n; i++) read(b[i]), b[i] -= a[i]; for (int i = 1; i <= n; i++) f[i & 1][++len[i & 1]] = b[i]; sort(f[0] + 1, f[0] + len[0] + 1); reverse(f[0] + 1, f[0] + len[0] + 1); sort(f[1] + 1, f[1] + len[1] + 1); reverse(f[1] + 1, f[1] + len[1] + 1); for (int i = 1; i <= n / 2; i++) { if (f[0][i] + f[1][i] < 0) break; ans += f[0][i] + f[1][i]; } print(ans, '\n'); return 0; }
//====================Template================== #include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define mod 1000000007 #define pb push_back #define ff first #define ss second #define inf 1e18 #define mid(l, r) (l+(r-l)/2) #define loop(i, a, b) for(int i=(a); i<b; i++) #define loopr(i, a, b) for(int i=(a); i>b; i--) #define sz(a) int((a).size()) #define all(c) c.begin(), c.end() #define allr(c) c.rbegin(), c.rend() #define tr(container, it) \ for(typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define present(container, element) \ (container.find(element) != container.end()) #define vpresent(container, element) \ (find(all(container),element) != container.end()) #define print(dp, n); \ loop(i, 0, n){cout << dp[i] << " ";}cout << endl; #define print2(dp, a, n, b, m); \ loop(i, a, n){loop(j, b, m){cout << dp[i][j] << " ";}cout << endl;} #define countetbits(i)\ __builtin_popcount(i) typedef vector< long long int > vl; typedef vector< int > vi; typedef vector< string > vs; typedef vector< vi > vvi; typedef vector< vl > vvl; typedef pair< ll,ll > pll; typedef pair< int, int> pii; ll gcd(ll a, ll b) { while (true) { if (a == 0) return b; b %= a; if (b == 0) return a; a %= b; } } ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp ? (a / temp * b) : 0; } template <class A, class B> auto findMin(A a, B b) -> decltype(a < b ? a : b) { return (a < b) ? a : b; } //Type Inference refers to automatic deduction of the data type of an expression in a programming language. //Auto lets you declare a variable with particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression. void file_i_o(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif } ll z = 1000000; ll n; void run_case(){ string s; cin >> s; if( s.substr(0, 1) == s.substr(1, 1) && s.substr(1, 1) == s.substr(2, 1) ){ cout << "Won" << endl; } else cout << "Lost" << endl; } int main(){ clock_t begin = clock(); file_i_o(); int tests = 1; /* int tests; */ /* cin >> tests; */ while(tests-- > 0) run_case(); #ifndef ONLINE_JUDGE clock_t end = clock(); cout << "\n\nExecuted In: " << double(end - begin) /CLOCKS_PER_SEC << " seconds" << endl; #endif return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; int a,b; inline int read(){ int q=0,f=1; char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();} while(isdigit(ch)){q=(q<<3)+(q<<1)+ch-48;ch=getchar();} return f?q:(~(q-1)); } inline void print(int x){ if(x<0){putchar('-');x=-x;} if(x>9)print(x/10); putchar(x%10+48); } signed main(){ a=read();b=read(); print(a+a+100-b); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(ll i=0, i##_len=(n); i<i##_len; ++i) #define REPR(i,n) for(ll i = n;i >= 0;--i) #define FOR(i,m,n) for(ll i = m, i##_len=(n);i <i##_len; ++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; } ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;} const int INF = 1e9; const ll LLINF = 1e16; int main(void) { cin.tie(0); ios::sync_with_stdio(0); int n, a, b; cin >> n >> a >> b; int rest = max(n - a, 0); cout << rest + b << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 1e5+7; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,a,b; cin>>n>>a>>b; cout<<n-a+b<<endl; return 0; }
#include<iostream> #include<math.h> #include<vector> using namespace std; int main(){ int H,W,min=100,answer=0; cin>>H>>W; vector<int> A(H*W); for(int i=0;i<H*W;i++){ cin>>A[i]; if(min>A[i])min=A[i]; } for(int i=0;i<H*W;i++){ answer=answer+(A[i]-min); } cout<<answer<<endl; }
/*..........UNPROFESSIONAL..........*/ #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fk(i,start,end) for(ll i=start;i<ll(end);i++) typedef long long int ll; string A="YES"; string B="NO"; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,m; cin>>n>>m; ll a[n][m]; ll mi=101; fk(i,0,n) { fk(j,0,m) { cin>>a[i][j]; mi=min(mi,a[i][j]); } } ll count=0; fk(i,0,n) { fk(j,0,m) { count+=(a[i][j]-mi); } } cout<<count; return 0; }
#include <bits/stdc++.h> #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using namespace std; //#define int long long typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; typedef vector<vector<int>> graph; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define MOD 1000000007 #define MAXX 1.1529215e+18 #define _1 first #define _2 second #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() #define si(x) int(x.size()) template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} template<class t> using vc=vector<t>; template<class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template<class T> T extgcd(T a, T b, T &x, T &y) { T d = a; if (b == 0) { x = 1; y = 0; } else { d = extgcd(b, a%b, y, x); y -= (a/b)*x; } return d; } template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p._1<<","<<p._2<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<class T> void g(T &a){ cin >> a; } template<class T> void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; string S, T; cin >> N >> S >> T; vector<ll> vs; vector<ll> vt; bool flag = true; ll ans = 0; rep(i, N) { if (S[i] == '1') { vs.pb(i); } if (T[i] == '1') { vt.pb(i); } } vt.pb(INF); ll s = 0; ll t = 0; while(s < vs.size()) { if (vs[s] < vt[t]) { if (s <= vs.size()-2) { ans -= vs[s]; ans += vs[s+1]; s+=2; } else { flag = false; break; } } else { ans += vs[s]-vt[t]; s++; t++; } // cout << s << ' ' << t << ' ' << ans << endl; } if (t < vt.size()-1) { flag = false; } if (flag) cout << ans << endl; else { cout << -1 <<endl; } }
#include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; std::string S; std::string T; void solve(long long N){ ll ans = 0; queue<int> carry_start_index; rep(i, N) { if (S[i] == '1') { if (carry_start_index.size()) { ans += i - carry_start_index.front(); carry_start_index.pop(); S[i] = '0'; } } if (S[i] != T[i]) { carry_start_index.push(i); } } if (carry_start_index.size()) { cout << -1 << endl; } else { cout << ans << endl; } } // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools int main(){ long long N; scanf("%lld",&N); std::cin >> S; std::cin >> T; // solve(N, S, T); solve(N); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, X; string S; cin >> N >> X >> S; for (int i = 0; i < N; i++) { if (S.at(i) == 'o') { X++; } else { if (X > 0) { X--; } } } cout << X << endl; }
#ifdef MY_LOCAL #define MY_NAMESPACE(ns) namespace ns { #define MY_NAMESPACE_ } #define MY_DEBUG(s) s #define MY_IS_DEBUG (true) #else #define MY_NAMESPACE(ns) #define MY_NAMESPACE_ #define MY_DEBUG(s) #define MY_IS_DEBUG (false) #endif #include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; #define EPS (1e-7) #define PI (acos(-1)) constexpr int IMIN = numeric_limits<int>::min(); constexpr int IMAX = numeric_limits<int>::max(); constexpr ll LLMIN = numeric_limits<ll>::min(); constexpr ll LLMAX = numeric_limits<ll>::max(); template <typename T> bool chmax(T& maxval, T const& newval) { if (newval > maxval) { maxval = newval; return true; } return false; } template <typename T> bool chmin(T& minval, T const& newval) { if (newval < minval) { minval = newval; return true; } return false; } MY_NAMESPACE(testbed) int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int a, b, c, d; cin >> a >> b; cin >> c >> d; int ans = b - c; cout << ans << "\n"; return 0; } MY_NAMESPACE_
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; int a[N], b[N]; int n; int main() { int aa, bb; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a, a + n); sort(b, b + n); if (a[n - 1] <= b[0]) cout << b[0] - a[n - 1] + 1 << endl; else cout << 0 << endl; return 0; }
#ifdef DBG_MACRO_NO_WARNING #include <dbg.h> #else #define dbg(...) ((void)0) #endif #include <bits/stdc++.h> #define all(c) begin(c), end(c) #define isz(c) (int)(c).size() using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; int main(void) { ios::sync_with_stdio(false), cin.tie(NULL); int t; cin >> t; while (t--) { ll n; cin >> n; if (n % 2 == 0) { if (n % 4 == 0) cout << "Even" << endl; else cout << "Same" << endl; } else cout << "Odd" << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long #define V vector<long long> #define VV vector<vector<long long>> #define VVV vector<vector<vector<long long>>> #define P pair<ll,ll> #define rep(i,n) for(ll (i)=0;(i)<(n);++(i)) #define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i)) #define all(x) (x).begin(),(x).end() using namespace std; std::vector<int> Eratosthenes( const int N ) { std::vector<bool> is_prime( N + 1 ); for( int i = 0; i <= N; i++ ) { is_prime[ i ] = true; } std::vector<int> p; for( int i = 2; i <= N; i++ ) { if( is_prime[ i ] ) { for( int j = 2 * i; j <= N; j += i ) { is_prime[ j ] = false; } p.emplace_back( i ); } } return p; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int main(){ ll n; cin>>n; V x(n); rep(i,n)cin>>x[i]; vector<int> pri=Eratosthenes(50); ll N=pri.size(); ll ans=1e18; ll tmp=1; for(ll bit=0;bit<(1<<N);bit++){ ll num=1; rep(k,N){ if(1<<k&bit){ num*=pri[k]; } } if(num==1)continue; bool ok=true; rep(i,n){ if(gcd(num,x[i])==1){ ok=false; break; } } if(ok)ans=min(ans,num); } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; // type alias typedef long long LL; typedef pair<int,int> II; typedef tuple<int,int,int> III; typedef vector<int> VI; typedef vector<string> VS; typedef unordered_map<int,int> MAPII; typedef unordered_set<int> SETI; template<class T> using VV=vector<vector<T>>; // minmax template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; } template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; } // repetition #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) for(int i=0;i<=(n);++i) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) for(int i=0;i<(n);++i) #define FORR(x,arr) for(auto& x:arr) #define SZ(a) int((a).size()) // collection #define ALL(c) (c).begin(),(c).end() // DP #define MINUS(dp) memset(dp, -1, sizeof(dp)) #define ZERO(dp) memset(dp, 0, sizeof(dp)) // stdout #define println(args...) fprintf(stdout, ##args),putchar('\n'); // debug cerr template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; } void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; } template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); } template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; } #define __KUMATRACE__ true #ifdef __KUMATRACE__ #define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); } #define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; } #define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); } #else #define dump(args...) #define dumpc(ar) #define dumpC(beg,end) #endif // $ cp-batch Peddler | diff Peddler.out - // $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address Peddler.cpp && ./a.out /* 1/17/2021 11:07- */ const int MAX_N=1e6+1; LL A[MAX_N]; int N,M; const LL Inf=1e18; LL dp[MAX_N][3]; VI G[MAX_N]; void solve() { REP(i,N)REP(j,3) dp[i][j]=-Inf; REP(i,N) dp[i][0]=0; REP(u,N)REP(j,3) { FORR(v,G[u]) { SMAX(dp[v][j],dp[u][j]); if(j==0) SMAX(dp[v][1],dp[u][0]-A[u]); if(j==1) SMAX(dp[v][2],dp[u][1]+A[u]); //dump(u,j,v,dp[v][j+1]); } } REP(u,N) SMAX(dp[u][2],dp[u][1]+A[u]); LL res=-Inf; REP(u,N) SMAX(res,dp[u][2]); cout<<res<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout<<setprecision(12)<<fixed; cin>>N>>M; REP(i,N) cin>>A[i]; REP(_,M) { int u,v; cin>>u>>v; --u,--v; G[u].push_back(v); } solve(); return 0; }
#include<iostream> #include<vector> #include<string> #include<stack> using namespace std; #define ll long long #define f(i,a,b) for(register ll i=a;i<=b;++i) #define pb push_back #define mp make_pair #define N 2005 #define Mod 1000000007 inline ll read() { ll s=0,w=1; register char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9') { s=(s<<1)+(s<<3)+(ch^48);ch=getchar();} return s*w; } char Mat[N][N]; ll X[N][N],Y[N][N],sqr[N*N]={1ll}; stack<ll>st; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); ll n=read(),m=read(),sum=0,ans=0; f(i,1,n*m+2) sqr[i]=(sqr[i-1]*2)%Mod; f(i,1,n) scanf("%s",Mat[i]+1); f(i,1,n) f(j,1,m) if(Mat[i][j]=='.') ++sum; f(i,1,n) { while(st.size()) st.pop(); for(ll j=1;j<=m+1;++j) { if(j==m+1||Mat[i][j]=='#') { ll siz=st.size(); while(st.size()) X[i][st.top()]=siz,st.pop(); } else st.push(j); } } f(j,1,m) { while(st.size()) st.pop(); for(ll i=1;i<=n+1;++i) { if(i==n+1||Mat[i][j]=='#') { ll siz=st.size(); while(st.size()) Y[st.top()][j]=siz,st.pop(); } else st.push(i); } } /*cout<<"Test X:"<<endl; f(i,1,n){ f(j,1,m) cout<<X[i][j]<<" ";cout<<endl; } cout<<"Test Y:"<<endl; f(i,1,n){ f(j,1,m) cout<<Y[i][j]<<" ";cout<<endl; }*/ f(i,1,n) f(j,1,m) { if(Mat[i][j]!='.') continue; ll x=sqr[sum-X[i][j]-Y[i][j]+1]; ans=(ans+((sqr[sum]-x)%Mod+Mod)%Mod)%Mod; } cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; using u128 = __uint128_t; using u64 = uint64_t; //define #define int long long #define ll int #define trav(i,v) for(auto i: v) #define rep(i,n) for(int i=0;i<n;i++) #define repu(i,k,n) for(int i=k;i<=n;i++) #define repd(i,k,n) for(int i=k;i>=n;i--) #define se second #define fi first #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define itn int #define sz(x) (int)x.size() //typedef typedef unsigned long long ull; typedef long double ld; typedef std::pair<int, int> pii; typedef std::vector<int> vi; typedef std::vector< pii > vii; typedef std::vector< std::vector<int> > vvi; typedef std::vector< std::pair < pii, int > > vpp; const long long MOD = 1000000007; // const long long MOD = 998244353; const long double PI = 3.141592653589793238; const long long pi = 31415926; const long long inf = 1000000000000000000; const long long small_inf = INT_MAX; int modpow(int x, int n, int mod = MOD){ int res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%MOD; n>>=1;} return res;} int power(int x, int n){ int res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res;} void init(){ } int ncr(int n, int r){ if(r<0 || r>n){ return 0; } if(n<0) return 0; ll x = n*(n-1); x/=2; return x; } void solve() { /*Don't hurry, nothing good comes if you rush*/ int n, k; cin>>n>>k; repu(x1,3,3*n){ int x = x1-3; int tot = 0; tot+=1ll*ncr(2+(x), 2); tot-=3ll*ncr(2+(x-n), 2); tot+=3ll*ncr(2+(x-2*n), 2); tot-=1ll*ncr(2+(x-3*n), 2); if(tot<k){ k-=tot; continue; } x = x1; // cout<<k<<endl<<endl; repu(i,1,n){ int left = x-i; if(left<=1 || left>2*n) continue; int mx = min(n,left-1ll); int mn = max(1ll,left-mx); // cout<<mx<<" "<<mn<<" "<<mx-mn+1<<endl; if(k>mx-mn+1){ k-=mx-mn+1; continue; } // cout<<i<<endl; repu(j,1,n){ if(x-i-j<=0 || x-i-j>n) continue; k--; if(k==0){ cout<<i<<" "<<j<<" "<<x-i-j<<endl; return; } } return; // break; } } // cout<<k<<endl; } signed main(){ // #ifndef ONLINE_JUDGE // freopen("./input.txt", "r", stdin); // freopen("./output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(0); cin.tie(NULL); int t23 = 1, tt23 = 1; // cin>>t23; init(); while(tt23<=t23) { // cout<<"Case #"<<tt23<<": "; solve(); tt23++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() using ll=long long; const int M=10000; ll f(){ ll res=0; string s;cin>>s; if(s[0]=='-')s=s.substr(1); bool ok=false; REP(i,s.size()) if(s[i]=='.')s+=string(4-((int)s.size()-i-1),'0'),ok=true; else res=res*10+s[i]-'0'; if(!ok)res*=M; return res; } ll ceilM(ll a){ if(a>0)return (a-1)/M*M+M; return ((-a)/M)*-M; } ll floorM(ll a){ return a/M*M; } int main(){ ll x=f(),y=f(),r=f(),ans=0; for(ll i=ceilM(x-r);i<=x+r;i+=M){ ll tmp=sqrt(r*r-(x-i)*(x-i)),d; for(ll _=-10;_<=10;_++)if((tmp+_)*(tmp+_)+(x-i)*(x-i)<=r*r)d=tmp+_; ans+=(floorM(y+d)-ceilM(y-d))/M+1; } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ long double x,y,r; cin>>x>>y>>r; r+=1e-14; // cout<<setprecision(10)<<mny<<" "<<setprecision(10)<<mxy<<endl; long double mnyy=ceil(y-r); long double mxyy=floor(y+r); //cout<<mnyy<<" "<<mxyy<<endl; unsigned long long count=0.0; while(mnyy<=mxyy){ long double sq=sqrt((r*r)-((y-mnyy)*(y-mnyy))); // cout<<sq<<endl; //cout<<x<<endl; long double s=ceil(x-sq); long double f=floor(x+sq); // cout<<s<<" "<<f<<endl; // cout<<s<<" "<<f<<endl; count+=((f-s+1)); mnyy=mnyy+1.0000000000; // cout<<count<< " count"<<endl; } cout<<count<<endl; 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 rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i)) #define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i)) #define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i)) #define all(x) (x).begin(), (x).end() int main() { int n, m; cin >> n >> m; vector<pair<int, int>> xy(m); rep(i, m) cin >> xy[i].first >> xy[i].second; sort(all(xy)); set<int> s; s.insert(n); int l = 0; while (l < m) { int r = l, xi = xy[l].first; while (r < m) { if (xi == xy[r].first) ++r; else break; } set<int> a, e; rep3(i, l, r) { int yi = xy[i].second; if ((s.find(yi-1)!=s.end()||s.find(yi+1)!=s.end()) && s.find(yi)==s.end()) a.insert(yi); if ((s.find(yi-1)==s.end()&&s.find(yi+1)==s.end()) && s.find(yi)!=s.end()) e.insert(yi); } for (int ai : a) s.insert(ai); for (int ei : e) s.erase(ei); l = r; } cout << (int)(s.size()) << endl; return 0; }
#include "iostream" using namespace std; int main() { auto n = 0, k = 0, r = 0; cin >> n >> k; n *= 100; for (auto i = 100; i <= n; i+=100) { for (auto j = 1; j <= k; j++) { r += (i + j); } } cout << r << endl; return 0; }
#include <bits/stdc++.h> using namespace std ; const int MAX = 1e5 + 10 ; int arr[MAX] ; int n , k ; int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>k ; long long a = n * (n + 1ll) / 2ll ; long long b = k * (k + 1ll) / 2ll ; return cout<<100ll * a * k + b * n<<"\n" , 0 ; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pii pair<int, int> #define pll pair<ll, ll> #define ff first #define ss second const ll INF = 1e18; const int MOD = 1e9+7; const int N = 2e5+7; const double PI = 3.1415926535897932384626; void solve() { int n, k; cin >> n >> k; int sum = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= k; ++j) sum += i*100+j; } cout << sum; } void fastIO(string name = "") { cin.tie(0)->sync_with_stdio(0); if (sz(name)) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } int main() { fastIO(""); int tc = 1; //cin >> tc; while (tc--) solve(); return 0; }
#include <iostream> using namespace std; long long n; int cnt1, cnt2; int main() { scanf("%lld", &n); for (long long i = 1, a, b; i * i - i <= 2 * n; i++) { a = 2 * n - i * i + i; b = 2 * i; if (!(a % b) && a / b >= 1) { cnt1 += 2; } } printf("%d", cnt1); return 0; }
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define LL long long #define DB double #define LD long double #define ST string #define BS bitset #define PA pair<LL,LL> #define VE vector #define VL VE<LL> #define VP VE<PA> #define VVL VE<VL> #define VVVL VE<VVL> #define PQ priority_queue #define PQS priority_queue<LL,vector<LL>,greater<LL>> #define FI first #define SE second #define PB push_back #define POB pop_back #define PF push_front #define POF pop_front #define MP make_pair #define TS to_string #define TU to_ullong #define BPL __builtin_popcountll #define FOR(i,a,n) for(i=a;i<n;++i) #define FORR(i,a,n) for(i=n-1;i>=a;--i) #define rep(i,n) FOR(i,0,n) #define repr(i,n) FORR(i,0,n) #define ALL(a) a.begin(),a.end() #define RALL(a) a.rbegin(),a.rend() #define SORT(a) sort(ALL(a)) #define REV(a) reverse(ALL(a)) #define UB(a,n) *upper_bound(ALL(a),n) #define UBn(a,n) upper_bound(ALL(a),n)-a.begin() #define LB(a,n) *lower_bound(ALL(a),n) #define LBn(a,n) lower_bound(ALL(a),n)-a.begin() #define INF 1000000000000000003 #define PI 3.14159265358979323846264338327950288 //#define MOD 1000000007 #define MOD 998244353 #define ERR 1e-10 #define coutl cout<<fixed<<setprecision(15) #define FAST cin.tie(0);ios::sync_with_stdio(false) void Yn(LL a){if(a)cout<<"Yes"<<endl;else cout<<"No"<<endl;} void YN(LL a){if(a)cout<<"YES"<<endl;else cout<<"NO"<<endl;} LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;} LL dig(LL n){LL ret=0;while(n)n/=10,++ret;return ret;} LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;} LL LCM(LL a,LL b){return a*b/GCD(a,b);} LL cmod(LL a,LL m){if(a%m<0)return a%m+abs(m);else return a%m;} LL DIV(LL a,LL d,LL m){LL l=m,x=1,y=0,k;while(l){k=d/l;d-=k*l;swap(l,d);x-=k*y;swap(x,y);}return cmod(a*cmod(x,m),m);} LL POW(LL a,LL n,LL m){LL ans=1;while(n>0){if(n&1)ans=ans*a%m;a=a*a%m;n>>=1;}return ans;} VL fact,finv,inv; void comi(LL n){LL i;fact.resize(max(2LL,n+1));finv.resize(max(2LL,n+1));inv.resize(max(2LL,n+1));fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[0]=inv[1]=1;FOR(i,2,n+1){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}} LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;} bool cmps(PA a,PA b){if(a.SE!=b.SE)return a.SE<b.SE;return a.FI<b.FI;} template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;} template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;} template<typename T>void vout(VE<T> &v){LL i;rep(i,v.size()){cout<<v[i];if(i<v.size()-1)cout<<" ";}cout<<endl;} template<typename T>void v2out(VE<VE<T>> &v){for(auto a:v)vout(a);} VL dvsr(LL n){VL ret;for(LL i=1;i*i<=n;++i){if(n%i==0){ret.PB(i);if(i*i!=n)ret.PB(n/i);}}SORT(ret);return ret;} int main(){ FAST; LL i,ans=0,N; cin>>N; VL v=dvsr(N); for(LL z:v){ if(z&1)ans+=2; //if(z%2==0&&) } cout<<ans<<endl; }
#include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define __builtin_ctz __builtin_ctzll #define __builtin_clz __builtin_clzll #define __builtin_popcount __builtin_popcountll using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; int n; cin>>n; VVI slo(n + 2); REP (i, n) { int a = (2 * i + 1) % n; int b = (2 * i + 2) % n; cout<<a + 1<<" "<<b + 1<<"\n"; slo[i].PB(a); slo[i].PB(b); } // VI dis(n +2); int ma_dis = 0; // REP (i, n) { // REP (j, n) { // dis[j] = n; // } // dis[i] = 0; // VI que{i}; // for (int ii = 0; ii < SZ(que); ii++) { // int v = que[ii]; // for (auto nei : slo[v]) { // if (dis[nei] == n) { // dis[nei] = dis[v] + 1; // que.PB(nei); // } // } // } // REP (j, n) { // maxi(ma_dis, dis[j]); // } // } debug(n, ma_dis); //cout<<ma_dis<<endl; return 0; }
#include <cmath> #include <vector> #include <iostream> #include <iomanip> #include <fstream> #include <algorithm> #include <set> #include <queue> #include <string> #include <map> #include <stack> #include <climits> #include <array> #include <unordered_set> #include <unordered_map> #include <memory> #include <functional> #include <cfloat> #include <numeric> #include <random> #include <sstream> #include <bitset> #include <complex> #include <chrono> #include <cassert> using namespace std; int main() { int n; cin >> n; for (auto i = 1; i <= n; ++i) { cout << (i * 2 - 1) % n + 1 << ' ' << (i * 2) % n + 1 << '\n'; } }
#include <bits/stdc++.h> #define watch(x) cout <<(#x)<<" is "<<(x)<<endl #define debug cout <<"hi"<<endl #define maxn 3e5+10 #define inf32 0x3f3f3f3f using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int,int>pii; const int MOD=1e9+7; const int INF32=1<<30; const ll INF64=1LL<<60; vector<vector<int>>adj(1e5+10); int sub[100000+10]; int dp[100000+10]; void dfs(int u){ vector<int>X[2]; sub[u] = 1; for(auto i:adj[u]){ dfs(i); sub[u] += sub[i]; X[sub[i]&1].push_back(dp[i]); } dp[u] = 1; sort(X[0].begin(),X[0].end()); sort(X[1].begin(),X[1].end()); for(auto i:X[0]){ if(i<=0) dp[u] += i; } int sgn = 1; for(auto i:X[1]){ dp[u] += sgn*i; sgn *= -1; } for(auto i:X[0]){ if(i>0) dp[u] += sgn*i; } } void solve(){ int n;cin >> n; for(int i = 0;i<n-1;i++){ int v;cin >> v; adj[v].push_back(i+2); } memset(dp,0,sizeof(dp)); memset(sub,0,sizeof(sub)); dfs(1); cout << (dp[1]+sub[1])/2; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#pragma region header #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 RALL(a) (a).rbegin(), (a).rend() #define pb push_back #define int ll #define each(i, a) for (auto &&i : (a)) using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using vvs = vector<vs>; using vd = vector<ld>; using vvd = vector<vd>; using vb = vector<bool>; using vvb = vector<vb>; using P = pair<int, int>; using vp = vector<P>; using int128 = __int128_t;//cin coutはできない template <class T> using greater_queue = priority_queue<T, vector<T>, greater<T>>; int gcd(int a,int b){return b?gcd(b,a%b):a;} int lcm(int a,int b){return a / gcd(a, b) * b;}; template <class T> void CVEC(const T &v) { int c = v.size() - 1; for (int i = 0; i < c; i++) cout << v[i] << ' '; if (c > -1) cout << v[c]; cout << '\n'; } #pragma endregion header // 0以上UINT_MAX(0xffffffff)以下の整数をとる乱数 xorshift https://ja.wikipedia.org/wiki/Xorshift static uint32_t randXor() { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } // 0以上1未満の小数をとる乱数 static double rand01() { return (randXor() + 0.5) * (1.0 / UINT_MAX); } // Time #include <sys/time.h> ll getTime() { struct timeval tv; gettimeofday(&tv, NULL); ll result = tv.tv_sec * 1000LL + tv.tv_usec / 1000LL; return result; } ll start_time, time_limit = 1950; void setTime(){ start_time = getTime();} ll nowTime(){ return getTime() - start_time;} bool timeup(){ return nowTime() > time_limit;} int si, sj; int t[50][50]; int p[50][50]; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; string cdir = "DRUL"; struct STATE{ int x, y; set<int> visited; int score; string path; }; void init(STATE &state){ state.x = si; state.y = sj; state.visited.insert(t[si][sj]); state.score = p[si][sj]; state.path = ""; } bool modify(STATE &state){ int x = state.x; int y = state.y; int mx = -1, mxdir = -1; int mx_nx = -1, mx_ny = -1; for(int dir = 0; dir < 4; dir++){ int nx = x + dx[dir], ny = y + dy[dir]; if(nx >= 50 || ny >= 50 || nx < 0 || ny < 0) continue; if(state.visited.count(t[nx][ny])) continue; if(mx <= p[nx][ny]){ mx = p[nx][ny]; mxdir = dir; mx_nx = nx; mx_ny = ny; } } if(mxdir == -1) return false; state.visited.insert(t[mx_nx][mx_ny]); state.path += cdir[mxdir]; state.score += mx; state.x = mx_nx; state.y = mx_ny; return true; } bool modify2(STATE &state){ int x = state.x; int y = state.y; int mx = -1, mxdir = -1; int mx_nx = -1, mx_ny = -1; int dir = randXor() % 4; int nx = x + dx[dir], ny = y + dy[dir]; if(nx >= 50 || ny >= 50 || nx < 0 || ny < 0) return false; if(state.visited.count(t[nx][ny])) return false; mxdir = dir; mx_nx = nx; mx_ny = ny; if(mxdir == -1) return false; state.visited.insert(t[mx_nx][mx_ny]); state.path += cdir[mxdir]; state.score += mx; state.x = mx_nx; state.y = mx_ny; return true; } string mountain(){ STATE state; init(state); while(!timeup()){ STATE new_state = state; if(rand01() > 1){ if(modify(new_state)) { state = new_state; } }else{ if(modify2(new_state)){ state = new_state; } } } return state.path; } signed main(){ setTime(); cin >> si >> sj; rep(i,50)rep(j,50) cin >> t[i][j]; rep(i,50)rep(j,50) cin >> p[i][j]; string ans = mountain(); cout << ans << endl; }
#include <iostream> #include <iomanip> using namespace std; int main(void){ int A, B; cin >> A >> B; cout << setprecision(10) << 100.0L-100.0L*(long double)B/(long double)A << endl; return 0; }
#include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <string> #include <iomanip> #define _USE_MATH_DEFINES #include <math.h> using namespace std; #define rep(i,n) for(i=0;i<n;i++) #define repl(k,n) for(k=0;k<n;++k) int main() { int A, B; cin >> A >> B; cout << (A + B) / 2<<"\n"; cout << (A - B) / 2; return 0; }
#pragma warning(disable: 4996) #include <string> #include <vector> #include <iostream> #include <cstdio> #include <sstream> #include <fstream> #include <math.h> #include <algorithm> #include <map> #include <bitset> #include <queue> using namespace std; typedef long long ll; #define rep(i,n) for (int i = 0; i < (n); i++) const double GS = 1e-7; int main() { int n; cin >> n; vector<int>x(n), y(n); rep(i, n)cin >> x[i] >> y[i]; for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { int tmp = min(y[i], y[j]); int mi = min(y[k], tmp); double k1 = 0, k2 = 0; if (mi == y[i]) { k1 = (double)(y[j] - y[i]) / (x[j] - x[i]); k2 = (double)(y[k] - y[i]) / (x[k] - x[i]); } else if (mi == y[j]) { k1 = (double)(y[i] - y[j]) / (x[i] - x[j]); k2 = (double)(y[k] - y[j]) / (x[k] - x[j]); } else { k1 = (double)(y[i] - y[k]) / (x[i] - x[k]); k2 = (double)(y[j] - y[k]) / (x[j] - x[k]); } if (abs(k1 - k2) <= GS || ((x[i] == x[j]) && (x[k] == x[i]))) { cout << "Yes" << endl; //cout << i << j << k << endl; //printf("%.16f", k1); //cout << endl; //printf("%.16f", k2); return 0; } } } } cout << "No" << endl; return 0; }
// DeNsE - EcLiPsE // // WHAT is DEAD may NEVER die // #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <map> #include <unordered_map> #include <cmath> #include <iomanip> #include <set> #include <cstring> #include <stack> #include <sstream> #include <queue> #include <unordered_set> #include <cstdlib> using namespace std; #define nitro ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define int long long #define double long double #define endl " \n" const int inf = (1ll << 62) - 1; const int mod = 998244353; const int N = 25; const double pi = 3.14159265358979323846; int bPow(int a, int b){ int res = 1; while(b) { if (b & 1) { res = (res * a) % mod; } b >>= 1; a = (a * a) % mod; } return res % mod; } int gcd(int a, int b) { if(a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } vector<int> fact(N, 0ll); void factorial(){ fact[0] = 1, fact[1] = 1; for(int i = 2; i < N; i++){ fact[i] = (fact[i - 1] * i) % mod; } } int ncr(int n, int r){ if(r > n) return 0; int ans = fact[n] % mod; ans *= bPow(fact[r], mod - 2) % mod; ans %= mod; ans *= bPow(fact[n - r], mod - 2) % mod; ans %= mod; return ans; } vector<int> primes(N, -1); void sieve(){ iota(primes.begin(), primes.end(), 0); for (int i = 2; i * i <= N; ++i) { if(primes[i] == i){ for (int j = 2 * i; j < N; j += i) { primes[j] = i; } } } } void solve() { int n; cin >> n; vector<pair<int, int>> pts; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; pts.emplace_back(x, y); } for (int i = 0; i < n; ++i) { int x = pts[i].first, y = pts[i].second; map<double, int> slopes; bool haveInfinite = false; for (int j = i + 1; j < n; ++j) { int dx = pts[j].first - x, dy = pts[j].second - y; if(dx == 0 and haveInfinite) { cout << "Yes" << endl; return; }else if(dx != 0){ double val = (double) dy / (double) dx; if(slopes.count(val)){ cout << "Yes" << endl; return; } slopes[val]++; } if(dx == 0) haveInfinite = true; } } cout << "No" << endl; } signed main(){ nitro int tc = 1, test = 1; //cin >> tc; while(tc--){ //cout << "Case #" << test++ << ": "; solve(); } }
#include <bits/stdc++.h> using ll = long long; const int maxn = 1e6 + 222; int n; ll a[maxn],s[maxn]; ll ans = 9e18; int main() { scanf("%d",&n); for (int i = 1; i <= n; ++ i) scanf("%lld",&a[i]); std::sort(a + 1,a + n + 1); for (int i = n; i >= 1; i --) s[i] = s[i + 1] + a[i]; for (int i = 0; i <= n; ++ i) { // 2x = a[i] ans = std::min(ans,n * a[i] + s[i + 1] * 2 - 2 * a[i] * (n - i)); } printf("%.10Lf",(long double) ans / (n * 2.0)); return 0; }
#include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef double db; typedef string str; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef pair<db,db> pd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<db> vd; typedef vector<str> vs; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pd> vpd; typedef vector<vi> vvi; typedef set<int> si; #define vt vector #define mp make_pair #define f first #define s second #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define rsz resize #define ins insert #define ft front() #define bk back() #define pf push_front #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define ts to_string #define EACH(x, a) for (auto& x: a) template<class T> void read(T &x){ cin >> x; } template<class A> void read(vt<A> &v){ EACH(x, v) read(x); } template<class H, class... T> void read(H& h, T& ...t){ read(h); read(t...); } str to_string(const char* c){ return (str)c; } str to_string(char c){ return (str)""+c; } str to_string(str s){ return s; } str to_string(bool b){ return b?"True" : "False"; } template<class A> str to_string(vt<A> v){ str s; EACH(x, v) s+= to_string(x) + " "; return s; } template<class H, class... T> str to_string(H& h, T& ...t){ str s; s+=to_string(h); s+=" "; s+=to_string(t...); return s; } void print(str s){ cout << s << "\n"; } template<class... A> void print(A ...a){ print(to_string(a...)); } int cdiv(int a, int b) { return a/b+!(a<0||a%b == 0); } str tb(int x) {str ans; int c = x; for(int i = log2(x); i >= 0; --i){if(pow(2,i) <= c){ ans += "1"; c-=pow(2,i); } else { ans += "0"; } } return ans;} bool ispr(int x){if(x <= 1){return false;} for(int i = 2; i <= sqrt(x); ++i){ if(x%i == 0){ return false; } }return true; } const int MOD = 1e9+7; // 998244353; const int MX = 2e5+5; const ll INF = 1e18; const double EPS = 2e-7; //tree<int,null_type,less<int>, rb_tree_tag, tree_order_statistics_node_update> T; void solve(){ int n; cin >> n; vd v(n); read(v); double l = 0, r = 2e11; double bes = double(INF); while(r-l > EPS){ double mid1 = l+(r-l)/3; double mid2 = r-(r-l)/3; double cur1 = 0; double cur2 = 0; for(int i = 0; i < n; ++i){ cur1+=(mid1+v[i]-min(v[i],2*mid1)); } for(int i = 0; i < n; ++i){ cur2+=(mid2+v[i]-min(v[i],2*mid2)); } if(cur1 < cur2){ bes = min(bes,cur1); r = mid2; } else{ bes = min(bes,cur2); l = mid1; } } printf("%.6f\n", bes/(db)n); } int main(){ std::ios::sync_with_stdio(0); std::cin.tie(0); bool isT = 0; if(isT){ int t; cin >> t; while(t--){ solve(); } return 0; } solve(); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) using namespace std; using ll = long long; using vi = vector<int>; using vv = vector<vi>; using P = pair<int, int>; //editorial int main(){ int n; string s, x; cin >> n >> s >> x; vi dp(n+1, 0); dp[n] = 1;//dp[n] = {0}; for(int i=n-1; i>=0; i--){ if(x[i]=='T'){ rep(r, 7){ if( ( 1<<(10*r%7) )&dp[i+1] )dp[i] |= 1<<r; if( ( 1<<((10*r + s[i]-'0')%7) )&dp[i+1] )dp[i] |= 1<<r; } } else{ rep(r, 7){ if( ( 1<<(10*r%7) )&dp[i+1] && ( 1<<((10*r + s[i]-'0')%7) )&dp[i+1] )dp[i] |= 1<<r; } } } cout << (dp[0]&1?"Takahashi":"Aoki") << endl; return 0; }
#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include <bits/stdc++.h> using namespace std; const int MOD=998244353; ll modpow(ll x,ll n){ ll ans=1; while(n){ if(n&1) ans=ans*x%MOD; x=x*x%MOD; n/=2; } return ans; } ll G[5050][5050]; ll T[5050][5050]; int main(){ int h,w,k; cin>>h>>w>>k; vector<vector<char>> A(h+1,vector<char> (w+1,'Z')); vector<ll> C(h+w+2); int p=min(h,w); rep(i,h+w) C[i]=min({i+1,p,h+w-i-1}); rep(i,k){ int y,x; char c; cin>>y>>x>>c; y--,x--; C[x+y]--; A[y][x]=c; } rep(i,h){ rep(j,w){ if(A[i][j]!='Z'){ T[i][j]=modpow(3,C[i+j]); } else{ T[i][j]=modpow(3,C[i+j]-1); } } } G[0][0]=1; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(A[i][j]=='X'){ G[i+1][j]=(G[i+1][j]+G[i][j]*T[i+1][j]%MOD)%MOD; G[i][j+1]=(G[i][j+1]+G[i][j]*T[i][j+1]%MOD)%MOD; } else if(A[i][j]=='R'){ G[i][j+1]=(G[i][j+1]+G[i][j]*T[i][j+1]%MOD)%MOD; } else if(A[i][j]=='D'){ G[i+1][j]=(G[i+1][j]+G[i][j]*T[i+1][j]%MOD)%MOD; } else{ G[i+1][j]=(G[i+1][j]+G[i][j]*2%MOD*T[i+1][j]%MOD)%MOD; G[i][j+1]=(G[i][j+1]+G[i][j]*2%MOD*T[i][j+1]%MOD)%MOD; } } } if(A[h-1][w-1]!='Z') cout<<G[h-1][w-1]<<endl; else cout<<G[h-1][w-1]*3%MOD<<endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define pii pair<int,int> #define pll pair<long long, long long> using namespace std; const int mod = 1e9+7; #define INF INT_MAX int sum(int x, int y) { return (x+y)%mod; } int mult(int x, int y) { return (1ll * x * y)% mod; } int fastpow(int val, int pow) { int res = 1; while(pow > 0) { if(pow % 2 != 0) res = mult(res, val); val = mult(val, val); pow/=2; } return res; } vector<vector<int>> matmult(vector<vector<int>> &mat1, vector<vector<int>> &mat2) { vector<vector<int>> res(mat1.size(),vector<int>(mat1[0].size(),0)); int m = mat1.size(), n = mat1[0].size(); for(int i=0;i<m;i++) { for(int j = 0; j < n; j++) { for(int k=0;k<n;k++) { res[i][j] = sum(res[i][j], mult(mat1[i][k], mat2[k][j])); } } } return res; } vector<vector<int>> matexp(vector<vector<int>> mat, int pow) { vector<vector<int>>res(mat.size(), vector<int>(mat[0].size(),0)); for(int i=0;i<mat.size();i++) res[i][i] = 1; while(pow > 0) { if(pow%2!=0) res = matmult(res, mat); pow/=2; if(pow == 0) break; mat = matmult(mat, mat); } return res; } vector<int> deg(105); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m,k; cin>>n>>m>>k; vector<int> a(n); int inv_2 = fastpow(2,mod-2); int inv_m = fastpow(m,mod-2); vector<vector<int>> mat(n, vector<int>(n,0)); for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<m;i++) { int x,y; cin>>x>>y; x--; y--; deg[x]++; deg[y]++; mat[x][y] = mult(inv_2, inv_m); mat[y][x] = mult(inv_2, inv_m); } for(int i=0;i<n;i++) { mat[i][i] = mult(sum(mult(2,m), -deg[i]),mult(inv_2,inv_m)); } mat = matexp(mat,k); vector<int> res(n); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) res[i] = sum(res[i], mult(a[j],mat[j][i])); } for(int i=0;i<n;i++) cout<<res[i]<<"\n"; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define pb emplace_back #define lb(v,k) (lower_bound(all(v),(k))-v.begin()) #define ub(v,k) (upper_bound(all(v),(k))-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define all(a) a.begin(),a.end() #define rsort(a) {sort(all(a));reverse(all(a));} #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} vi matmul(vi dp,vvi mt,int n){ vi res(n); rep(i,n)rep(j,n)res[i]+=mt[i][j]*dp[j]%mod; rep(i,n)res[i]%=mod; return res; } vvi update(vvi mt,int n){ vvi res(n,vi(n)); rep(i,n)rep(j,n)rep(k,n)res[i][j]+=mt[i][k]*mt[k][j]%mod; rep(i,n)rep(j,n)res[i][j]%=mod; return res; } void matpow(vi &dp,vvi mt,ll k,int n){ while(k){ if(k&1)dp=matmul(dp,mt,n); mt=update(mt,n); k>>=1; } } /* int main(){ ll n,k;cin>>n>>k; //nは分かってる項数、kは求めたいのが何項目か vi dp(n); rep(i,n)cin>>dp[n-i-1]; if(k<=n)dame(dp[n-k]); //分かっている vvi mt(n,vi(n)); rep(i,n)cin>>mt[0][i]; //遷移の式 REP(i,1,n)mt[i][i-1]=1; //こうすると上手くいく matpow(dp,mt,k-n,n); out(dp[0]); } */ //ABC009D 漸化式 int main(){ ll n,m,k;cin>>n>>m>>k; vvi mt(n,vi(n)); vi dp(n);rep(i,n)cin>>dp[i]; ll tmp=modpow(m,mod-2); ll tmp2=modpow(2,mod-2); rep(i,m){ ll a,b;cin>>a>>b;a--;b--; mt[a][b]=tmp2; mt[b][a]=tmp2; } rep(i,n){ ll sum=0; rep(j,n)sum+=mt[i][j]; mt[i][i]=(m+mod-sum%mod)%mod; } rep(i,n)rep(j,n)mt[i][j]=mt[i][j]*tmp%mod; matpow(dp,mt,k,n); for(ll x:dp)out(x); }
#include<bits/stdc++.h> using namespace std; inline long long read(){ long long x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; } struct node{ long long A,B; }v[200100]; bool cmp(node a,node b){ return a.A<b.A; } int main(){ long long n=read(),k=read(); for(int i=1;i<=n;++i)v[i].A=read(),v[i].B=read(); sort(v+1,v+1+n,cmp); int i; v[0].A=0; for(i=1;i<=n;++i){ if(k<v[i].A-v[i-1].A){ break; } k-=v[i].A-v[i-1].A;k+=v[i].B; } --i; cout<<v[i].A+k<<endl; return 0; }
#include <bits/stdc++.h> #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef long long LL; const int MAXN = (1 << 18); int n, m; bool matrix[18][18], good[MAXN]; int dp[MAXN]; bool check(int x) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (j != i) if (((x >> i) & 1) && ((x >> j) & 1) && !matrix[i][j]) return false; return true; } signed main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; a--, b--; matrix[a][b] = matrix[b][a] = 1; } memset(dp, 0x3f3f3f3f, sizeof(dp)); dp[0] = 0; for (int i = 1; i < (1 << n); i++) if (check(i)) good[i] = 1, dp[i] = 1; for (int i = 1; i < (1 << n); i++) { if (good[i]) continue; for (int j = i; j; j = (j - 1) & i) dp[i] = min(dp[i], dp[j] + dp[i ^ j]); } cout << dp[(1 << n) - 1]; return 0; }
#include <bits/stdc++.h> #define ll long long int #define pb push_back #define umap unordered_map #define mod 1000000007ll #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define MN(a,b,c) min(a,min(b,c)) #define MX(a,b,c) max(a,max(b,c)) #define pr1 pair<ll,ll> #define F first #define S second #define mP make_pair #define f(i,n) for(ll i=0;i<n;i++) #define f1(i,x,y) for(ll i=x;i<=y;i++) #define f2(i,x,y) for(ll i=x;i>=y;i--) #define yes cout<<"Yes"<<"\n" #define no cout<<"No"<<"\n" #define modsum(a,b) ((a%mod)+(b%mod))%mod #define modpro(a,b) ((a%mod)*(b%mod))%mod #define moddif(a,b) ((a%mod)-(b%mod)+mod)%mod #define modsumt(a,b,c) modsum(a,modsum(b,c)) //__builtin_popcount(x) //__builtin_parity(x) =(number of set bits)%2 //__builtin_clz(x) to count the number of leading zeroes //__builtin_ctz(x) to count the number of trailing zeroes //__gcd(a,b) using namespace std; ll fac[200025]; ll modIfac[200025]; ll modularExponentiation(ll x,ll n,ll M) { ll result=1; while(n>0) { if(n % 2 ==1) result=modpro(result,x); x=modpro(x,x); n=n/2; } return result; } ll binaryExponentiation(ll x,ll n) { ll result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } ll pow1(ll x,ll y) { ll z=1; while(y--){z=z*x;} return z; } bool isprime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Returns n^(-1) mod p ll modInverse(ll n, ll p) { return modularExponentiation(n, p - 2, p); } // Returns nCr % p using Fermat's little // theorem. unsigned long long nCrModPFermat(unsigned long long n, ll r, ll p) { // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } bool compare(pair<ll,ll> a, pair<ll,ll> b) { if(a.F==b.F) return a.S<b.S; else return a.F>b.F; } ll check(ll x,ll y) { ll z=1; ll ans=0; while(z<x){ans++;z*=y;} return ans; } ll countbits(ll n) { ll x=0; while(n>0) { x++; n=(n&(n-1)); } return x; } int main() { ios_base::sync_with_stdio(false); ll t,n; cin>>t>>n; ll x=100*n; ll y=x/t; if(x%t) y++; cout<<y+n-1; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define pb push_back int main() { ll tc,n,t; cin>>t>>n; cout<<(n*100)/t-((n*100)%t==0) + n<<" "; return 0; }
#include<map> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define MAXN 100005 #define ENDL putchar('\n') #define LL long long #define DB double #define lowbit(x) ((-x) & (x)) LL read() { LL f = 1,x = 0;char s = getchar(); while(s < '0' || s > '9') {if(s=='-')f = -f;s = getchar();} while(s >= '0' && s <= '9') {x=x*10+(s-'0');s = getchar();} return f * x; } const int MOD = 1000000007; int n,m,i,j,s,o,k; int a[MAXN]; int dp[MAXN]; int main() { n = read(); int sum = 0,sum2 = 0; dp[0] = 1; dp[1] = 1; for(int i = 2;i <= n;i ++) { dp[i] = (dp[i-1]+dp[i-2]) % MOD; } for(int i = 1;i <= n;i ++) { a[i] = read(); (sum += a[i]) %= MOD; if(i > 1) { (sum2 += dp[i-2] *1ll* dp[n-i] % MOD *2ll* a[i] % MOD) %= MOD; } } printf("%lld\n",(sum*1ll*dp[n]%MOD+MOD-sum2) % MOD); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' #define vec vector<ll> #define mat vector<vector<ll> > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; //typedef long double ld; typedef complex<double> Complex; const ll INF=1e9+7; const ll MOD=998244353; const ll inf=INF*INF; const ll mod=INF; const ll MAX=100010; signed main(){ ll n;cin>>n; ll ans=n; ll k=1; ll cnt=0; while(n>=k){ ans=min(ans,n/k+cnt+n%k); cnt++; k*=2LL; } cout<<ans<<endl; }
# pragma GCC optimize ("O3") # pragma GCC optimize ("Ofast") # pragma GCC optimize ("unroll-loops") #define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rep2(i,a,b) for(int i = (b) - 1; i >= (a); i--) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb push_back #define x first #define y second using namespace std; using ll = long long; using tl = tuple<ll, ll, ll, ll>; using pl = pair<ll, ll>; using pi = pair<int, int>; using ld = long double; int N, dp[1 << 7][200001]; string S, X; int mod(int i){ i = i % 7; return i < 0 ? i + 7 : i; } int solution(int mask, int n){ if(dp[mask][n]) return dp[mask][n]; if(n == N) return dp[mask][n] = (mask & 1) ? 1: - 1; int tmp = 0; rep(i, 0, 7){ if(mask & (1 << i)){ tmp |= 1 << mod(i * 10 + S[n] - '0'); tmp |= 1 << mod(i * 10); } } //if(n < N - 1 && X[n] == X[n + 1]) return dp[mask][n] = solution(tmp, n + 1); rep(i, 0, 7){ if(tmp & (1 << i)){ if(X[n] == 'A') if(solution(1 << i, n + 1) == -1) return dp[mask][n] = -1; if(X[n] == 'T') if(solution(1 << i, n + 1) == 1) return dp[mask][n] = 1; } } if(X[n] == 'A') return dp[mask][n] = 1; return dp[mask][n] = -1; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> N >> S >> X; cout << (solution(1, 0) == 1 ? "Takahashi" : "Aoki"); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define print_upto(a, n) \ for (ll i = 1; i <= n; i++) \ cout << a[i] << " "; \ cout << endl #define take(a, n) \ for (ll i = 1; i <= n; i++) \ cin >> a[i]; #define watch(x) cout << (#x) << " is " << (x) << "\n" #define watch2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << "\n" #define watch3(x, y, z) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << " and " << (#z) << " is " << (z) << "\n" void my_debugger(string s, int LINE_NUM) { cerr << endl; } template <typename start, typename... end> void my_debugger(string s, int LINE_NUM, start x, end... y) { if (s.back() != ',') { s += ','; cerr << "LINE(" << LINE_NUM << "): "; } int i = s.find(','); cerr << s.substr(0, i) << " = " << x; s = s.substr(i + 1); if (!s.empty()) cerr << ", "; my_debugger(s, LINE_NUM, y...); } #define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__); #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define ff first #define ss second #define null NULL #define all(c) (c).begin(), (c).end() #define allr(c) (c).rbegin(), (c).rend() #define nl "\n" #define ld long double #define eb emplace_back #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define MOD 1000000007 #define inf 1e17 // cout << fixed << setprecision(15) << ans << nl; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef pair<ll, pll> ppll; const ll N = 200009; void solve() { ll m, h; cin >> m >> h; if (h % m == 0) { cout << "Yes" << nl; } else { cout << "No" << nl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll T = 1; // cin >> T; while (T--) { solve(); } return 0; }
#include <iostream> #include <cmath> #include <stdio.h> #include <string.h> #include <bits/stdc++.h> #include <vector> #include <array> #include <tuple> using namespace std; int main(){ long int N; cin >> N; int p=1; long long a; vector<long long> B(N); for (int i=0; i<N; i++){ cin >> a; B[i]=p*a; p=-p; } vector<long long> S(N+1); S[0]=B[0]; for (int i=1; i<N; i++){ S[i]=S[i-1]+B[i]; } S[N]=0; sort(S.begin(), S.end()); long long M=0; long int m=1; for (int i=1; i<N+1; i++){ if ((i==N)&(S[i]==S[i-1])){ m++; M=M+(m*(m-1))/2; } else if (S[i]==S[i-1]){ m++; } else { M=M+(m*(m-1))/2; m=1; } } cout << M << endl; }
#include<bits/stdc++.h> #define cpu() ios::sync_with_stdio(false); cin.tie(nullptr) #define ps() cout << "\n" #define pb push_back #define ff first #define ss second typedef long long ll; using namespace std; const int MOD = 1e9 + 7, MOD1 = (119 << 23) | 1; const int INF = 1e9 + 5, MAX = 2e5 + 5; int main(){ cpu(); int n; cin >> n; map<ll, int> m; ll cur[2] = {}; ll ans = 0; m[0] = 1; for(int i = 0; i < n; i++){ ll x; cin >> x; cur[i & 1] += x; ans += (m[cur[1] - cur[0]]++); } cout << ans << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; long long n,p,ans,bs; long long MOD=1000000007; int main() { cin>>n>>p; if(p==2) { if(n==1)cout<<"1"; else cout<<"0"; } else if(p==3)cout<<"2"; else { ans=(p-1)%MOD; bs=(p-2)%MOD; n--; while(n) { if(n&1==1)ans=(ans*bs)%MOD; bs=(bs*bs)%MOD; n>>=1; } cout<<ans%MOD; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define rrep1(i, n) for (int i = n; i >= 1; i--) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define eb emplace_back #define fi first #define se second #define sz(x) (int)(x).size() template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; typedef long long int ll; typedef pair<int, int> P; // typedef modint1000000007 mint; void speedUpIO() { cin.tie(nullptr); ios::sync_with_stdio(false); } 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; } /*--------------------------------------------------*/ const int INF = 1e9; // const ll INF = 1e18; const int MAX = 1e5; ll power_mod(ll n, ll p, ll mod) { if (p == 0) return 1; if (p % 2 == 0) { ll t = power_mod(n, p / 2, mod); return t * t % mod; } return n * power_mod(n, p - 1, mod) % mod; } const ll mod = 1000000007; void solve() { ll n, p; cin >> n >> p; ll ans = p - 1; ans = ans * power_mod(p - 2, n - 1, mod) % mod; cout << ans << "\n"; } int main() { speedUpIO(); int t = 1; // cin >> t; while (t--) { solve(); // cout << solve() << "\n"; // cout << fixed << setprecision(15) << solve() << "\n"; } return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define out(x) cout << x << '\n' #define lli long long int #define uli unsigned long long int #define rep(i, m, n) for (lli i = m; i < (n); i++) #define repe(i, m, n) for (lli i = m; i <= (n); i++) #define ALL(x) (x).begin(), (x).end() #define SIZE(x) ((lli)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define _GLIBCXX_DEBUG const lli INF = 2147483647; const lli MINF = -2147483648; const lli LINF = 9223372036854775807; const lli MOD = 1000000007; //10^9+7 const double PI = acos(-1); #define SORT(n) sort(n.begin(), n.end()) #define SORTR(n) sort(n.begin(), n.end(), greater<int>()) #define REV(n) reverse(n.begin(), n.end()) #define pb push_back #define eb emplace_back #define mp make_pair using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vlli = vector<lli>; using vvli = vector<vlli>; using vs = vector<string>; using vvs = vector<vs>; using vb = vector<bool>; using vvb = vector<vb>; using ll = long long; //--------------------------------------------------------------------------------------------------- // vector入力 template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // vector出力 template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { // os << '{'; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ""); } // os << '}'; return os; } // aよりもbが大きいならばaをbで更新する(更新されたならばtrueを返す) template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; // aをbで更新 return true; } return false; } // aよりもbが小さいならばaをbで更新する(更新されたならばtrueを返す) template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; // aをbで更新 return true; } return false; } //--------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------- void solve() { lli a = 0, b = 0, c = 0, h = 0, n = 0, w = 0, ans = 0, count = 0; string s = "", t = ""; vector<pair<lli, lli>> pr; map<lli, lli> ma; set<lli> st; ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> a >> b; ans = n - a + b; out(ans); } //--------------------------------------------------------------------------------------------------- signed main() { solve(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define for0(n) for(i=0; i<(n); i++) int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); ll _,i,j,n,a,b,e,k,u,t,x=1,m,y,q,f=1,c=0,d=0,l,p,z,o,r,g,w,h; string ss; cin>>h>>w>>x>>y; string s[h]; for0(h)cin>>s[i]; x--; y--; if(s[x][y]=='#')f=0; for(i=x+1;i<h;i++) { if(s[i][y]=='#')break; else f++; } for(i=x-1;i>=0;i--) { if(s[i][y]=='#')break; else f++; } for(i=y+1;i<w;i++) { if(s[x][i]=='#')break; else f++; } for(i=y-1;i>=0;i--) { if(s[x][i]=='#')break; else f++; } cout<<f<<endl; }
#include<bits/stdc++.h> using namespace std; using ll = long long; const int maxS = 41; int arr[maxS]; vector<long long>A[2]; void rec(int currentPos,int size,int idx,long long sum){ if(currentPos > size){ A[idx].push_back(sum); return ; } rec(currentPos + 1,size,idx,sum + arr[currentPos]); rec(currentPos + 1,size,idx,sum); } void tc(){ int N , T; cin >> N >> T; for(int i = 1 ; i <= N ;i ++){ cin >> arr[i]; } rec(1,N/2,0,0); rec((N/2)+1,N,1,0); sort(A[1].begin(),A[1].end()); long long MAX = 0; for(int i = 0 ; i < (int)A[0].size(); i ++){ long long TEMP = A[0][i]; long long REM = T - TEMP; if(REM > 0){ auto itr = lower_bound(A[1].begin(),A[1].end(),REM); if(itr==A[1].begin()&&*itr!=REM) continue; if(itr==A[1].end()) --itr; else{ if(*itr!=REM) --itr; } MAX = max(MAX , *itr + TEMP); } } cout << MAX << "\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t = 1; //cin >> t; while(t--){ tc(); } }
#include <bits/stdc++.h> using namespace std; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}, lst[75]; long long A,B, dp[2][1<<20]; int main() { scanf("%lld %lld", &A, &B); for (int i = 0; i <= B-A; ++i) { lst[i] = 0; for (int j = 0; j < 20; ++j) if ((A+i) % primes[j] == 0) lst[i] |= (1<<j); } for (int i = 0; i < (1<<20); ++i) dp[1-(B-A)%2][i] = 1; for (int i = B-A; i >= 0; --i) for (int j = 0; j < (1<<20); ++j) { dp[i%2][j] = dp[1-i%2][j]; if ((lst[i] & j) == 0) dp[i%2][j] += dp[1-i%2][j|lst[i]]; } printf("%lld", dp[0][0]); return 0; }
/** * author: otera **/ #include<bits/stdc++.h> using namespace std; #define int long long typedef long long ll; typedef long double ld; const int inf=1e9+7; const ll INF=1LL<<60; #define rep(i, n) for(int i = 0; i < n; ++ i) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) typedef pair<int, int> P; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(),c.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; } void solve() { int n, m; cin >> n >> m; vector<int> c(n); vector<vector<int>> g(n, vector<int>()); vector<int> a(m), b(m); vector<int> ans(m, -1); map<P, P> mp; rep(i, m) { cin >> a[i] >> b[i]; -- a[i], -- b[i]; g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); mp[{a[i], b[i]}] = {i, 1}, mp[{b[i], a[i]}] = {i, 0}; } rep(i, n) { cin >> c[i]; } vector<bool> used(m, 0); rep(i, m) { if(c[a[i]] < c[b[i]]) { // <- ans[i] = 0; used[i] = 1; } else if(c[a[i]] > c[b[i]]) { // -> ans[i] = 1; used[i] = 1; } } auto rec = [&](auto && self, int p, int v) -> void { for(int nv: g[v]) { if(nv == p) continue; if(c[v] == c[nv]) { // v -> nv if(mp.count({v, nv})) { P p = mp[{v, nv}]; int i = p.fr, val = p.sc; if(used[i]) continue; used[i] = 1; ans[i] = val; self(self, v, nv); } } } }; rep(i, m) { if(!used[i]) { assert(c[a[i]] == c[b[i]]); used[i] = 1; ans[i] = 1; rec(rec, a[i], b[i]); } } rep(i, m) { assert(ans[i] != -1); if(ans[i] == 1) cout << "->" << "\n"; else cout << "<-" << "\n"; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(20); //int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define getchar_unlocked getchar int A[100005]; int B[100005]; int offset[100005]; int minLevel1[100005]; int minLevel2[100005]; int N, L; inline int nextInt(){ int x=0; char ch=0; do{ ch=getchar_unlocked(); }while(ch<'0'||ch>'9'); while(ch>='0'&&ch<='9'){ x=10*x+(ch-'0'); ch=getchar_unlocked(); } return x; } int main(){ N = nextInt(); L = nextInt(); for(int i = 1; i <= N; i ++){ A[i] = nextInt(); offset[i] = A[i]-i; } for(int i = 1; i <= N; i ++){ B[i] = nextInt(); } for(int i = 1; i <= N; i ++){ minLevel1[i] = 0; minLevel2[i] = 0; //printf("offset[%d]=%d\n", i, offset[i]); } long long ans = 0; int lo = 1; int hi2 = 1; for(int i = 1; i <= N; i ++){ if(A[i] == B[i]){continue;} if(B[i] == i || B[i] == L-N+i){ ans ++; }else{ while(offset[lo] < B[i]-i && lo < N+2){ lo ++; } if((lo == N+2) || (offset[lo] != B[i]-i)){ printf("-1"); return 0; } //hi2 = max(hi2, lo); while(offset[hi2] <= B[i]-i && hi2 < N+2){ hi2 ++; } int hi = hi2-1; //int hi = upper_bound(offset+1, offset+(1+N), B[i]-i) - offset - 1; if(lo > i){ minLevel2[lo] = max(minLevel2[lo], lo - i); }else{ minLevel1[hi] = max(minLevel1[hi], i - hi); } //printf("i=%d [%d, %d]\n", i, lo, hi); //ans += min(abs(lo - i), abs(hi - i)); } } for(int i = 1; i <= N; i ++){ ans += minLevel1[i] + minLevel2[i]; } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 20; mt19937 mt; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); mt.seed(time(nullptr)); int n, m; cin >> n >> m; vector<string> v(m); for (auto& i : v) { cin >> i; } //sort(v.begin(), v.end(), [](string a, string b){return a.size() < b.size();}); shuffle(v.begin(), v.end(), mt); int ind[10] = {}; for (auto& i : v) { for (auto& j : i) { ++ind[j - 'A']; } } char a[N][N]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j] = char(mt() % 8 + int('A')); } } /*int i = 0, j = 0; for (auto s : v) { if (j + s.size() >= n) { j = 0; ++i; } if (i >= n) { break; } for (int k = 0; k < s.size(); ++k, ++j) { a[i][j] = s[k]; } }*/ for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << a[i][j]; } cout << '\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < n; i++) #define Rep(i,n) for(int i = 1; i <= n; i++) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define debug(a) { cerr << #a << ':' << a << endl; } #define endl '\n' #define fi first #define se second using lint = long long; using P = pair<int,int>; template<class T> using V = vector<T>; template<class T> using priq = priority_queue<T>; template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>; const int dx[] = {0,1,0,-1,1,1,-1,-1}; const int dy[] = {1,0,-1,0,1,-1,-1,1}; template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; } template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF; template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) { return in >> p.first >> p.second; } template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) { return out << "{ " << p.first << ',' << p.second << " } "; } template<class T> istream& operator>>(istream &in, vector<T> &v) { for(auto &&e: v) in >> e; return in; } template<class T> ostream& operator<<(ostream &out, const vector<T> &v) { for(const auto &e: v) out << e << ' '; return out; } /*----------------------------------------------------------------------------------------------------*/ int n, m; V<string> s; // fstream out("AHC004_out.txt"); int main() { clock_t start = clock(); cin >> n >> m; s.resize(m); cin >> s; V<P> cnts(m); // pair = (count, index); V<V<int>> ins(m); rep(i,m) { int cnt = 0; rep(j,m) { bool isin = ( s[i].find(s[j]) != string::npos ); if(isin) { cnt++; ins[i].push_back(j); } } cnts[i] = make_pair(cnt,i); } sort(rall(cnts)); V<bool> used(m); V<string> ans(n); rep(i,n) { // 初期解生成 string str = ""; rep(j,m) { int x = cnts[j].se; if(used[x]) continue; string plus = s[x]; for(int k = min(sz(str), sz(s[x])); k >= 0; k--) { if( str.substr(sz(str)-k,k) == s[x].substr(0,k) ) { plus = s[x].substr(k,sz(s[x])-k); break; } } if(sz(str) + sz(plus) > n) continue; str += plus; for(int y: ins[x]) used[y] = true; } str.resize(n,'.'); ans[i] = str; } V<int> v(n); iota(all(v), 0); pair<int,V<int>> mx = make_pair(0,v); random_device seed_gen; mt19937 engine(seed_gen()); while(clock() - start < 2980000) { V<bool> nused = used; int cnt = 0; rep(j,n) { // yoko string ns = ""; rep(i,n) ns += string(1,ans[v[i]][j]); ns += ns; rep(j,m) { if(nused[j]) continue; if( ns.find(s[j]) != string::npos ) { cnt++; nused[j] = true; } } } chmax(mx, make_pair(cnt, v)); shuffle(all(v), engine); } rep(i,n) { cout << ans[mx.se[i]] << endl; } }