code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include<bits/stdc++.h> using namespace std; #define ll long long int #define nln "\n" void solve(){ int n,k; cin>>n>>k; int sum=0; for(int x=1;x<=n;x++){ for(int y=1;y<=k;y++){ sum+=(100*x)+y; } } cout<<sum<<nln; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int test_cases; // cin>>test_cases; test_cases=1; while(test_cases--){ solve(); } return 0; }
#include<iostream> using namespace std; int main() { int N,K; cin >> N >> K; printf("%d", 100 * K * N * (N + 1) / 2 + K * (K + 1) * N / 2); }
#include<bits/stdc++.h> int main() { int n; int c=0,A,P,X,t=INT_MAX; scanf("%d",&n); while (n--) { scanf("%d %d %d",&A,&P,&X); if(A-X<0) { t=P<t?P:t; c=1; } } if(c==1) printf("%d",t); else printf("-1"); 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 P = pair<int, int>; int main() { int n; cin >> n; const int INF = 1001001001; int ans = INF; rep(i, n) { int a, p, x; cin >> a >> p >> x; if(x-a >= 1) ans = min(ans, p); } if(ans==INF) ans = -1; cout << ans << endl; }
// JG GAP GG #include <iostream> #include <cstring> #include<vector> #include <algorithm> #include<cstdlib> #include<set> #include<math.h> #include<map> #include<unordered_map> #include<iomanip> #include<queue> #include<bitset> using namespace std; using ll = long long; using ull = unsigned long long; const ll MOD = 1000000007; #define rep(n,x) for(ll i=0;i<n;i++) cin>>x[i]; #define forr(a,b) for(ll i=a;i<b;i++); #define vecsort(myVec,x,y) sort(myVec.begin(),myVec.end(),[](const vector<y> &alpha,const vector<y> &beta){return alpha[x] < beta[x];}); #define vsort(v) sort(v.begin(),v.end()); #define vdownsort(v) sort(v.begin(),v.end(),greater<ll>()); ll modpow(ll a, ll n, ll mod) { ll res = 1; while (0 < n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); double sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; double ans = (sy * gx + sx * gy) / (sy + gy); cout << fixed << setprecision(10) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long int #define MOD const LL 1000000007; int ans = 0; int main(){ long double sx,sy,gx,gy; cin >> sx >> sy >> gx >> gy; cout << fixed << setprecision(16) << (sx * gy + gx * sy)/(sy + gy) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define sz(v) int((v).size()) #define all(x) (x).begin(), (x).end() #define forn(i, n) for (int i = 1; i <= int(n); ++i) //출처: https://codeforces.com/blog/entry/68809 void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << (#x) << "] = [", _print(x) #else #define debug(x...) #endif #define int ll const int MOD = 998244353; int power(int a, int n) { int res = 1; for (;n; n >>= 1, a = (a * a) % MOD) if (n & 1) res = (res * a) % MOD; return res; } void add(int& a, int b){a = (a%MOD + b%MOD)%MOD;} int h, w, k; char grid[5050][5050]; int dp[5050][5050][4]; int f(char c) { return c == 'R'? 2 : (c== 'D'); } void solve() { cin>>h>>w>>k; forn(_, k) { int a, b; char c; cin>>a>>b>>c; grid[a][b] = c; } int r = h*w - k; if (grid[1][1]) dp[1][1][f(grid[1][1])] = power(3, r); else dp[1][1][3] = power(3, r); int p3 = power(3, MOD - 2); forn(i, h) forn(j, w) { if (grid[i+1][j]) { add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('D')]); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('X')]); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3); } else { add(dp[i + 1][j][3], dp[i][j][f('D')]); add(dp[i + 1][j][3], dp[i][j][f('X')]); add(dp[i + 1][j][3], dp[i][j][3] * p3); add(dp[i + 1][j][3], dp[i][j][3] * p3); } if (grid[i][j+1]) { add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('R')]); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('X')]); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3); } else { add(dp[i][j + 1][3], dp[i][j][f('R')]); add(dp[i][j + 1][3], dp[i][j][f('X')]); add(dp[i][j + 1][3], dp[i][j][3] * p3); add(dp[i][j + 1][3], dp[i][j][3] * p3); } // for (int k = 0; k < 4; ++k) debug(i, j, k, dp[i][j][k]); } if (grid[h][w]) cout << dp[h][w][f(grid[h][w])] << '\n'; else cout << dp[h][w][3] << '\n'; } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> ii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; typedef long long int ll; typedef vector<ll> vll; #define _ << " " << string s1, s2, s3; map<char, int> let; vector<pair<char, int> > le; vi tak, fir; void getUniqLet(string &s) { int sz = int(s.size()); for (int i = 0; i < sz; i++) { if (i == 0) let[s[i]] = 1; else let[s[i]]; } } ll getNum(string &s) { int sz = int(s.size()); ll n = 0, c = 1; for (int i = sz-1; i >= 0; i--) { n += (c * let[s[i]]); c *= 10; } return n; } bool check() { for (auto l : le) { let[l.first] = l.second; } ll a = getNum(s1); ll b = getNum(s2); ll c = getNum(s3); if (a + b == c) { cout << a << "\n"; cout << b << "\n"; cout << c << "\n"; return true; } else return false; } bool perm(int i) { if (i == int(le.size())) { if (check()) return true; else return false; } int beg = 0; if (fir[i]) beg = 1; for (int d = beg; d < 10; d++) { if (!tak[d]) { tak[d] = 1; le[i].second = d; if (perm(i+1)) return true; tak[d] = 0; } } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>s1>>s2>>s3; getUniqLet(s1); getUniqLet(s2); getUniqLet(s3); if (let.size() > 10) { cout << "UNSOLVABLE\n"; return 0; } for (auto l : let) { le.push_back({l.first, 0}); fir.push_back(l.second); } tak.resize(10, 0); if (perm(0)) return 0; cout << "UNSOLVABLE\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define endl '\n' typedef pair<int, int> P; typedef long long ll; int dp[300][300]; int n; vector<int> bucket; bool valid(vector<int> x, vector<int> y) { if (x.size() == 0 || y.size() == 0) { return false; } if (x.size() != y.size()) { return true; } rep(i, x.size()) { if (x[i] != y[i]) { return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; rep(i, n) { int t; cin >> t; bucket.push_back(t % 200); } dp[0][0] = 1; rep(i, n) { rep(j, 200) { if (dp[i][j] > 0) { dp[i + 1][j] += dp[i][j]; dp[i + 1][(j + bucket[i]) % 200] += dp[i][j]; } } } while (true) { int s = 0, t = 0; bool cant = true; rep(i, n + 1) { rep(j, 200) { if (j == 0 && dp[i][j] > 2) { s = i; t = j; cant = false; } if (j > 0 && dp[i][j] > 1) { s = i; t = j; cant = false; } } } if (cant) { cout << "No" << endl; return 0; } int i = s; int j = t; vector<int> x; while (i != 0 && j != 0) { if (dp[i - 1][(j + 200 - bucket[i - 1]) % 200] > 0) { j = (j + 200 - bucket[i - 1]) % 200; x.push_back(i); i--; } else { i--; } } i = s; j = t; vector<int> y; while (i != 0 && j != 0) { if (y.empty() && dp[i - 1][j] > 1) { i--; } else if (y.empty() && dp[i - 1][(j + 200 - bucket[i - 1]) % 200] > 1) { j = (j + 200 - bucket[i - 1]) % 200; y.push_back(i); i--; } else if (dp[i - 1][j] > 0) { i--; } else { j = (j + 200 - bucket[i - 1]) % 200; y.push_back(i); i--; } } if (valid(x, y)) { reverse(x.begin(), x.end()); reverse(y.begin(), y.end()); cout << "Yes" << endl; cout << x.size(); for (auto xi : x) { cout << ' ' << xi; } cout << endl; cout << y.size(); for (auto yi : y) { cout << ' ' << yi; } cout << endl; return 0; } else { dp[s][t] = 0; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> A(N); for (auto &&e : A) { cin >> e; } vector<vector<int>> X(200); for (int i = 0; i < N; i++) { X[A[i] % 200].emplace_back(i); } for (int i = 0; i < 200; i++) { if ((int)X[i].size() >= 2) { cout << "Yes" << '\n'; cout << 1 << ' ' << X[i][0] + 1 << '\n'; cout << 1 << ' ' << X[i][1] + 1 << '\n'; return 0; } } vector<bitset<200>> Y(200); for (int i = 0; i < N; i++) { auto Z = Y; if (Y[A[i] % 200].any()) { cout << "Yes" << '\n'; cout << 1 << ' ' << i + 1 << '\n'; vector<int> c; for (int j = 0; j < N; j++) { if (Y[A[i] % 200][j]) c.emplace_back(j); } cout << c.size() << ' '; for (const auto &e : c) { cout << e + 1 << (&e == &c.back() ? '\n' : ' '); } return 0; } if (Y[0].any()) { cout << "Yes" << '\n'; cout << 1 << ' ' << i + 1 << '\n'; vector<int> c; for (int j = 0; j < N; j++) { if (Y[0][j] || i == j) c.emplace_back(j); } cout << c.size() << ' '; for (const auto &e : c) { cout << e + 1 << (&e == &c.back() ? '\n' : ' '); } return 0; } Z[A[i] % 200][i] = 1; for (int j = 0; j < 200; j++) { if (Y[j].none()) continue; if (Y[(j + A[i]) % 200].any()) { bitset<200> b = Y[(j + A[i]) % 200], c = Y[j]; c[i] = 1; for (int k = 0; k < N; k++) { if (b[k] && c[k]) { b[k] = 0; c[k] = 0; } } if (b.any() && c.any()) { cout << "Yes" << '\n'; vector<int> bb, cc; for (int k = 0; k < N; k++) { if (b[k]) bb.emplace_back(k); if (c[k]) cc.emplace_back(k); } cout << bb.size() << ' '; for (const auto &e : bb) { cout << e + 1 << (&e == &bb.back() ? '\n' : ' '); } cout << cc.size() << ' '; for (const auto &e : cc) { cout << e + 1 << (&e == &cc.back() ? '\n' : ' '); } return 0; } } Z[(j + A[i]) % 200] = Y[j]; Z[(j + A[i]) % 200][i] = 1; } Y = Z; } cout << "No" << '\n'; return 0; }
#include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <stack> #include <cassert> #include <map> #include <numeric> #include <cstring> #include <set> #include <ctime> #include <queue> #include <cmath> #include <iomanip> #include <iterator> #include <bitset> #include <unordered_map> #include <complex> #include <unordered_set> #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") using namespace std; clock_t timeStart, timeFinish; void timeBegin() { timeStart = clock(); } void timeEnd() { timeFinish = clock(); } void timeDuration() { timeEnd(); double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << fixed << time_taken << setprecision(5); cout << " sec " << endl; } class InputReader { public: InputReader() { input_file = stdin; cursor = 0; fread(buffer, SIZE, 1, input_file); } InputReader(const char *file_name) { input_file = fopen(file_name, "r"); cursor = 0; fread(buffer, SIZE, 1, input_file); } inline InputReader &operator>>(int &n) { while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') { advance(); } int sign = 1; if (buffer[cursor] == '-') { sign = -1; advance(); } n = 0; while ('0' <= buffer[cursor] && buffer[cursor] <= '9') { n = n * 10 + buffer[cursor] - '0'; advance(); } n *= sign; return *this; } private: FILE *input_file; static const int SIZE = 1 << 17; int cursor; char buffer[SIZE]; inline void advance() { ++cursor; if (cursor == SIZE) { cursor = 0; fread(buffer, SIZE, 1, input_file); } } }; const int MOD = 1000000007; const int MAXN = 100000; int dp[1 + MAXN][2], sum[1 + MAXN][2], v[1 + MAXN]; int main() { timeBegin(); //ifstream cin("input.in"); //ofstream cout("output.out"); ios_base::sync_with_stdio(false); cin.tie(0); srand(time(0)); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i]; } dp[1][0] = 1; dp[1][1] = 0; sum[1][0] = v[1]; sum[1][1] = 0; for (int i = 2; i <= n; i++) { dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % MOD; dp[i][1] = dp[i - 1][0]; sum[i][0] = (1LL * v[i] * (dp[i - 1][0] + dp[i - 1][1]) + sum[i - 1][0] + sum[i - 1][1]) % MOD; sum[i][1] = (1LL * (MOD - v[i]) * dp[i - 1][0] + sum[i - 1][0]) % MOD; } cout << (sum[n][0] + sum[n][1]) % MOD << "\n"; return 0; }
#include<bits/stdc++.h> #define int long long #define mod 1000000007 #define maxn 100005 using namespace std; int n,ans,ans1; int a[maxn],dp[maxn],dp1[maxn],dp2[maxn],dp3[maxn]; //int ni[maxn]; //ni[i]=ni[mod%i]*(mod-mod/i)%mod; int mi(int a,int t){ ans1=1; while(t){ if(t%2==1) {ans1*=a;ans1%=mod;} a*=a; a%=mod; t/=2; } return ans1; } signed main() { // freopen("yhc","r",stdin); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; dp[0]=1; for(int i=1;i<=n-1;i++) { dp1[i]=dp[i-1]%mod; dp[i]=(dp1[i-1]%mod+dp[i-1]%mod)%mod; } dp2[n-1]=dp[n-1];dp3[n-1]=dp1[n-1]; for(int i=n-2;i>=1;i--) { dp2[i]=(dp3[i+1]%mod+dp2[i+1]%mod*mi(dp[i]+dp1[i],mod-2)%mod*dp[i]%mod)%mod; dp3[i]=((dp[n-1]-dp2[i])%mod+dp1[n-1]%mod)%mod; } for(int i=1;i<=n-1;i++) { if(dp2[i]<dp3[i]) dp2[i]+=mod; ans+=(dp2[i]-dp3[i])%mod*a[i+1]; ans%=mod; } ans+=(dp[n-1]%mod+dp1[n-1]%mod)%mod*a[1]; ans%=mod; // for(int i=1;i<=n-1;i++) cout<<dp[i]<<" "<<dp1[i]<<" "<<dp[i]+dp1[i]<<endl; // cout<<endl; // for(int i=1;i<=n-1;i++) cout<<dp2[i]<<" "<<dp3[i]<<" "<<dp2[i]*1.0/(dp3[i]+dp2[i])<<endl; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; //begin of def #define fastio ios_base::sync_with_stdio(false);cin.tie(0) #define endl '\n' using lli = long long int; using ulli = unsigned long long int; using Ld = long double; using pii = pair<int, int>; using pll = pair<lli, lli>; using pld = pair<Ld, Ld>; #define X first #define Y second #define rep(I, S, E) for(int I = (S); I < (E); I++) #define repq(I, S, E) for(int I = (S); I <= (E); I++) #define pb push_back #define epb emplace_back #define ALL(X) X.begin(), X.end() //end of def int main(){ fastio; lli n; int m; cin >> n >> m; vector<pii> v(m); rep(i, 0, m) cin >> v[i].X >> v[i].Y; sort(ALL(v)); set<lli> st; st.insert(n); lli nw = -2, er = -2; rep(i, 0, m){ if(i && v[i].X != v[i - 1].X) nw = er = -2; int t = er; if(st.count(v[i].Y)) er = v[i].Y; st.erase(v[i].Y); if((st.count(v[i].Y - 1) && nw != v[i].Y - 1) || st.count(v[i].Y + 1) || t == v[i].Y - 1){ if(st.count(v[i].Y) == 0) nw = v[i].Y; st.insert(v[i].Y); } } cout << st.size(); return 0; }
#include <map> #include <set> #include <cmath> #include <queue> #include <string> #include <vector> #include <iostream> #include <algorithm> #include <functional> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> X(M), Y(M); for (int i = 0; i < M; ++i) { cin >> X[i] >> Y[i]; } vector<pair<int, int> > points; points.push_back(make_pair(0, N)); for (int i = 0; i < M; ++i) { points.push_back(make_pair(X[i], Y[i])); points.push_back(make_pair(X[i] - 1, Y[i] - 1)); points.push_back(make_pair(X[i] - 1, Y[i] + 1)); } sort(points.begin(), points.end()); points.erase(unique(points.begin(), points.end()), points.end()); int S = points.size(); vector<vector<int> > G(S); for (int i = 0; i < M; ++i) { int ptr1 = lower_bound(points.begin(), points.end(), make_pair(X[i] - 1, Y[i] - 1)) - points.begin(); if (ptr1 != S && points[ptr1] == make_pair(X[i] - 1, Y[i] - 1)) { G[ptr1].push_back(lower_bound(points.begin(), points.end(), make_pair(X[i], Y[i])) - points.begin()); } int ptr2 = lower_bound(points.begin(), points.end(), make_pair(X[i] - 1, Y[i] + 1)) - points.begin(); if (ptr2 != S && points[ptr2] == make_pair(X[i] - 1, Y[i] + 1)) { G[ptr2].push_back(lower_bound(points.begin(), points.end(), make_pair(X[i], Y[i])) - points.begin()); } } map<int, vector<pair<int, int> > > y_axis; for (int i = 0; i < S; ++i) { y_axis[points[i].second].push_back(make_pair(points[i].first, i)); } set<pair<int, int> > pawns; for (int i = 0; i < M; ++i) { pawns.insert(make_pair(X[i], Y[i])); } for (pair<int, vector<pair<int, int> > > i : y_axis) { vector<pair<int, int> > v = i.second; sort(v.begin(), v.end()); for (int i = 0; i < int(v.size()) - 1; ++i) { if (pawns.find(points[v[i + 1].second]) == pawns.end()) { G[v[i].second].push_back(v[i + 1].second); } } } int ini = lower_bound(points.begin(), points.end(), make_pair(0, N)) - points.begin(); queue<int> que; que.push(ini); vector<bool> vis(S, false); vis[ini] = true; while (!que.empty()) { int u = que.front(); que.pop(); for (int i : G[u]) { if (!vis[i]) { vis[i] = true; que.push(i); } } } int ans = 0; for (pair<int, vector<pair<int, int> > > i : y_axis) { if (vis[i.second.back().second]) { ++ans; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define FOR(i, n) for(int (i)=0; (i)<(n); (i)++) #define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--) template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);} template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);} template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); } template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); } int n; vector<ll> d; vector<int> mtl; vector<vector<int>> edges; vector<int> par; vector<bool> marked; int mintoleaf(int u, int p){ if (mtl[u]!=-1) return mtl[u]; int ret = n+1; for(int v : edges[u]) if (v !=p){ umin(ret, 1 + mintoleaf(v, u)); } if (edges[u].size() == 1) ret = 0; return mtl[u] = ret; } void dfs1(int u, int p){ for(int v : edges[u]) if (v !=p){ d[v] = d[u]+1; par[v] = u; dfs1(v, u); } } int dfs(int u, int p, int e){ d[u] = e; vector<pii> o; for(int v : edges[u]) if (v != p){ o.emplace_back(mintoleaf(v, u), v); if (marked[v]) o.back().first = 1e9; } sort(o.begin(), o.end()); int start = e+1; for(auto it : o){ int dist = dfs(it.second, u, start); start = dist + 1; } return start; } int main(int argc, char** argv) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15); if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin); cin >> n; edges.resize(n+1); d.resize(n+1); mtl.resize(n+1, -1); FOR(i, n-1){ int u, v; cin >> u >> v; edges[u].push_back(v); edges[v].push_back(u); } int ondiag = 1; d.clear(); d.resize(n + 1); par.clear(); par.resize(n+1); dfs1(ondiag, -1); int dist = -1; ondiag = -1; FOR1(u, n) { if (d[u] > dist) { dist = d[u]; ondiag = u; } } d.clear(); d.resize(n + 1); par.clear(); par.resize(n+1); dfs1(ondiag, -1); marked.clear(); marked.resize(n+1); dist = -1; int other = -1; FOR1(u, n){ if (d[u] > dist){ dist = d[u]; other = u; } } marked[other] = 1; while(other!=ondiag){ other = par[other]; marked[other] = 1; } d.clear(); d.resize(n + 1); dfs(ondiag, -1, 1); FOR1(i, n) cout << d[i] << " "; cout << endl; if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n"; return 0; }
/** * Author: Daniel * Created Time: 2020-12-06 23:54:03 **/ // time-limit: 2000 #include <bits/stdc++.h> using namespace std; #define F first #define S second #define IS insert #define PI acos(-1) #define PB pop_back #define EB emplace_back #define lowbit(x) (x & -x) #define SZ(x) ((int)x.size()) #define MP(x, y) make_pair(x, y) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define SOS; ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<fixed<<setprecision(10); typedef long long LL; typedef pair<int, int> PII; template <typename A> using VE = vector<A>; template <typename A> using HEAP = priority_queue<A>; template <typename A, typename B> using PA = pair<A, B>; template <typename A> using RHEAP = priority_queue<A, vector<A>, greater<A> >; /////////////////////////////////////////////////////////////////////////// //////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////// /////////////////////////////////////////////////////////////////////////// // check the limitation!!! const int N = 100010, M = 1010; int n; string t, s; bool check() { string s = t, tt = "011"; reverse(ALL(s)); for (int i = 0; i < n; i ++ ) if (s[i] != tt[i % 3]) return false; return true; } // read the question carefully!!! int main() { SOS; cin >> n >> t; if (n == 1) { if (t[0] == '0') cout << (LL)1e10 << '\n'; else if (t[0] == '1') cout << (LL)2e10 << '\n'; else cout << "0\n"; return 0; } while (SZ(s) < n + 10) s += "011"; if (t[n - 2] == '1' && t[n - 1] == '1') t += "0", n += 1; else if (t[n - 2] == '0' && t[n - 1] == '1') t += "10", n += 2; if (!check()) { cout << "0\n"; return 0; } reverse(ALL(t)); LL res = ((LL)1e10 - n / 3LL) + !(n % 3LL); cout << res << '\n'; return 0; } // GOOD LUCK!!!
#include <bits/stdc++.h> using namespace std; #define int long long int const double pi = 3.14159265358979; const int inf = 1e13; const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; #define pii pair<int,int> #define endl "\n" #define dtor(deg) (((deg)/360)*2*pi) #define all(a) a.begin(),a.end() #define overload(_1,_2,_3,_4,name,...) name #define _rep1(n) for(int i = 0; i < (n); i++) #define _rep2(i,n) for(int i = 0; i < (n); i++) #define _rep3(i,a,b) for(int i = (a); i < (b); i++) #define _rep4(i,a,b,c) for(int i = (a); i < (b); i += (c)) #define rep(...) overload(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__) #define _rrep1(n) for(int i = (n) - 1; i >= 0; i--) #define _rrep2(i,n) for(int i = (n) - 1; i >= 0; i--) #define _rrep3(i,a,b) for(int i = (b) - 1; i >= (a); i--) #define rrep(...) overload(__VA_ARGS__,_null,_rrep3,_rrep2,_rrep1)(__VA_ARGS__) #define vec(type,name,...) vector<type> name(__VA_ARGS__) #define vv(type,name,size,...) vector<vector<type>> name(size,vector<type>(__VA_ARGS__)) #define ForEach(a,b) for_each(a.begin(),a.end(),b) struct Edge { int to, cost; Edge(int to, int cost) : to(to), cost(cost) {} }; using Graph = vector<vector<Edge>>; 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; } void Main(){ int b, c, ans; cin >> b >> c; if(b == 0){ cout << c << endl; }else if(b > 0 && c >= b * 2){ ans = b * 2 + 1 + (c - 2); cout << ans << endl; }else if(b < 0 && c >= abs(b) * 2 + 1){ ans = abs(b) * 2 + c; cout << ans << endl; }else if(b > 0 && c == 3){ cout << 5 << endl; }else if(b > 0 && c == 2){ cout << 3 << endl; }else if(b > 0 && c == 1){ cout << 2 << endl; }else if(b > 0){ cout << 5 + (c - 3) * 2 << endl; }else if(b < 0 && c == 3){ cout << 5 << endl; }else if(b < 0 && c == 2){ cout << 3 << endl; }else if(b < 0 && c == 1){ cout << 2 << endl; }else if(b < 0){ cout << 5 + (c - 3) * 2 << endl; } } signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(10) << fixed; Main(); }
#include "bits/stdc++.h" using namespace std; //#include "testlib.h" #define ff first #define ss second #define all(v) v.begin(),v.end() #define int long long #define ll long long #define M 1000000007 #define MM 998244353 #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i] #define GCD(m,n) __gcd(m,n) #define LCM(m,n) m*(n/GCD(m,n)) #define mii map<ll ,ll > #define msi map<string,ll > #define rep(a,b) for(ll i=a;i<b;i++) #define rep0(n) for(ll i=0;i<n;i++) #define repi(i,a,b) for(ll i=a;i<b;i++) #define pb push_back #define vi vector<ll> #define vs vector<string> #define ppb pop_back #define endl '\n' #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define r0 return 0; #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i) #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); #define Set(a, s) (a, s, sizeof (a)) #define FOR repi #define vii vector<pii> #define pii pair<int,int> #define REVERSE(v) reverse(all(v)) #define trav(a, x) for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define debug cerr<<"bhau"<<endl #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } template<typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template<typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; ll max(ll a, ll b) { return (a > b)? a : b;} int min(int a, int b) { return (a < b)? a : b;} const int N = 1e2 + 10; int dp[N][N][N]; bool vis[N][N][N]; int solve(){ int n,x; cin>>n>>x; for(int i=0;i<=n;i++) vis[i][0][0] = 1; for(int i=0;i<n;i++){ int z; cin>>z; for(int j=n;j>=1;j--){ for(int k=j-1;k>=0;k--){ for(int l = n-1;l>=0;l--){ if(vis[j][k][l]){ amax(dp[j][k+1][(l+z)%j],dp[j][k][l] + z); vis[j][k+1][(l+z)%j] = 1; } } } } } int ans = 1e18; for(int i=1;i<=n;i++){ if(vis[i][i][x%i]){ int req = (x - dp[i][i][x%i])/i; ans = min(ans,req); } } cout<<ans<<endl; return 0; } signed main(){ asdf int t=1; // cin>>t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); #define F first #define S second #define PI 4.0*atan(1.0); #define pb push_back using namespace std; typedef pair<int, int> pi; int mod=1e9+7,gaymod=998244353,MAX=1000000000,pomod=1073741824; //long long int gcd(long long int x,long long int y){ // if(y==0)return x; //return gcd(y,x%y); //} int gcd(int x,int y){ if(y==0)return x; return gcd(y,x%y); } ll lcm(ll a, ll b) { ll g=gcd(a, b); return a/g*b; } ll binexp(ll a,ll b){ if(b==0)return 1; ll res=binexp(a,b/2); if(b%2)return((res%mod)*(res%mod)*(a%mod)); else return((res%mod)*(res%mod)); } int prime(ll x){ if(x%2==0&&x!=2) return 0; else if(x==1)return 0; else{ for(int i=3;ll(i*i)<=x;i+=2){ if(x%i==0) return 0; } } return 1; } //main /* vector<char> is_prime(N+1, true); void sieve(int n){ is_prime[0] = is_prime[1] = false; for (int i = 2; i <= n; i++) { if (is_prime[i] && (long long)i * i <= n) { for (int j = i * i; j <= n; j += i) is_prime[j] = false; } } } */ //freopen("input.txt","r",stdin); //freopen("input.txt","w",stdin); const int N=2e5+5; // int main(){ IOS; int t=1; //cin>>t; while(t--){ string str; cin>>str; int cnt=0,bnt=0; for(auto i:str){ if(i=='o')cnt++; else if(i=='?') bnt++; } if(cnt>4)cout<<0; else{ if(cnt==1){ cout<<(bnt+1)*(bnt+1)*(bnt+1)*(bnt+1)-bnt*bnt*bnt*bnt; } else if(cnt==2){ int l=bnt; bnt=bnt*(bnt-1); bnt/=2; bnt=max(bnt,0); cout<<14+36*l+24*bnt; } else if(cnt==3){ cout<<36+bnt*24; } else if(cnt==4){ cout<<24; } else{ if(bnt==0)cout<<0; else cout<<bnt*bnt*bnt*bnt; } } cout<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int ans=0; vector<int>a(10); for (int i=0;i<10;i++) { if(s[i]=='o'){ a[i]=0; } if(s[i]=='x'){ a[i]=1; } if(s[i]=='?'){ a[i]=2; } } bool d=1; vector<bool>b(10); for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { for (int k=0;k<10;k++) { for (int l=0;l<10;l++) { d=1; for (int m=0;m<10;m++) { if(a[m]==0){ b[m]=1; } } if(a[i]==0){ b[i]=0; } if(a[i]==1){ d=0; } if(a[i]==2){ b[i]=0; } if(a[j]==0){ b[j]=0; } if(a[j]==1){ d=0; } if(a[j]==2){ b[j]=0; } if(a[k]==0){ b[k]=0; } if(a[k]==1){ d=0; } if(a[k]==2){ b[k]=0; } if(a[l]==0){ b[l]=0; } if(a[l]==1){ d=0; } if(a[l]==2){ b[l]=0; } for (int m=0;m<10;m++) { if(b[m]!=0){ d=0; } } if(d){ ans++; } } } } } cout << ans << endl; }
#include<cstdio> int main() { int n; std::scanf("%d",&n); std::printf("%d",n>=0?n:0); return 0; }
#include <bits/stdc++.h> #define ll long long #define ii pair<ll, ll> #define fi first #define se second #define vi vector<int> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define vl vector<ll> #define mod 1000000007 #define inf 0x3f3f3f3f #define For(i, n) for (int i = 0; i < n; i++) using namespace std; class A{ public: void solve(){ int x; cin>>x; if(x>=0) cout<<x<<endl; else cout<<0<<endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); A ans; ans.solve(); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <unordered_set> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif #define GET_MACRO(_1, _2, _3, NAME, ...) NAME #define _rep(i, n) _rep2(i, 0, n) #define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++) #define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using i64 = long long; template<class T, class U> bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; } template<class T, class U> bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; } template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;} template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);} template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;} template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;} template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;} int n; vector<int> edge[200010]; int bfs(int st) { vector<int> visited(n, -1); queue<int> que; que.push(st); visited[st] = 0; while (!que.empty()) { auto now = que.front(); que.pop(); for (auto e: edge[now]) { if (visited[e] != -1) continue; visited[e] = visited[now] + 1; que.push(e); } } auto m = *max_element(all(visited)); rep(i, n) if (visited[i] == m) return i; return -1; } map<int, int> mp; bool dfs(int st, int gl, int p) { if (st == gl) return true; for (auto e: edge[st]) { if (e == p) continue; if (dfs(e, gl, st)) { mp[st] = e; return true; } } return false; } int ans[200010]; int c = 1; void dfs2(int now, int p) { ans[now] = c++; for (auto e: edge[now]) { if (e == p) continue; if (e == mp[now]) continue; dfs2(e, now); c++; } if (mp[now] != -1) dfs2(mp[now], now); } int main() { cin >> n; rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; edge[a].push_back(b); edge[b].push_back(a); } rep(i, n) mp[i] = -1; auto s = bfs(0); auto g = bfs(s); dfs(s, g, -1); dfs2(s, -1); rep(i, n) cout << ans[i] << " "; }
#include <bits/stdc++.h> #include <unordered_set> #include <cmath> // URL: https://atcoder.jp/contests/abc195/tasks/abc195_d using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vs = vector<string>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define repll(i, n) for (ll i = 0; i < (ll)(n); i++) #define repsll(i, s, n) for (ll i = (ll)(s); i < (ll)(n); i++) #define ALL(a) (a).begin(), (a).end() #define pb push_back #define optimize_cin() cin.tie(0); ios::sync_with_stdio(false) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const ll INFL = 1e18; const int INF = 1e9; int main(){ optimize_cin(); ll N, M, Q; cin >> N >> M >> Q; //vector<pll> WV(N); //vll W(N), V(N); vector<pll> VW(N); vll X(M), L(Q), R(Q); rep(i, N){ ll W, V; // cin >> W[i] >> V[i]; cin >> W >> V; VW[i] = make_pair(V, W); // WV[i].first() = W; // WV[i].second() = V; } rep(i, M) cin >> X[i]; rep(i, Q) cin >> L[i] >> R[i]; sort(ALL(VW)); reverse(ALL(VW)); // ll max = 0; rep(i, Q) { vector<bool> box(M, true); for(int j = L[i] - 1; j <= R[i] - 1; j++){ box[j] = false; } vll avail; rep(j, M){ if(box[j]){ avail.pb(X[j]); } } sort(ALL(avail)); // reverse(ALL(avail)); vll used(avail.size(), false); int cnt = avail.size(); ll max = 0; rep(j, N) { rep(k, avail.size()){ if(cnt > 0 && !used[k] && VW[j].second <= avail[k]){ max += VW[j].first; used[k] = true; cnt--; break; } } } cout << max << endl; } return 0; }
#include <iostream> #include <cmath> #include <vector> #include <string> #include<bits/stdc++.h> using namespace std; int main() { long long n , q; cin >> n; string s; cin >> s; cin >> q; int arr[q][3]; int sum = 0; for(int i = 0 ; i < q ; i++){ for(int j = 0 ; j < 3 ; j++){cin >> arr[i][j];} if(arr[i][0]%2==0){sum++;} else if (arr[i][0]==1 && sum%2==0){ char a = s[arr[i][1]-1]; s[arr[i][1]-1] = s[arr[i][2]-1]; s[arr[i][2]-1] = a; } else{ if(arr[i][1]<=n){ arr[i][1]+=n ; } else {arr[i][1]-=n;} if(arr[i][2]<=n){ arr[i][2]+=n ; } else {arr[i][2]-=n;} char a = s[arr[i][1]-1]; s[arr[i][1]-1] = s[arr[i][2]-1]; s[arr[i][2]-1] = a; } } if(sum%2==0){cout << s << endl; return 0;} else{s = s.substr(n,n)+s.substr(0,n); } cout << s << endl; }
//------------------------------------------ // C++ templete //------------------------------------------ #include <bits/stdc++.h> #define int long long using namespace std; //type //------------------------------------------ using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vs = vector<string>; //REPEAT //------------------------------------------ #define REP(i, a, b) for (int i = (a); i < (b); ++i) //container util //------------------------------------------ #define pb push_back #define paired make_pair #define ALL(a) (a).begin(), (a).end() #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " #define SORT(V) sort((V).begin(), (V).end()) #define RSORT(V) sort((V).rbegin(), (V).rend()) #define SZ(x) ((int)(x).size()) //constant //------------------------------------------ const int MOD = 1000000007; const int INF = 2147483647; //1061109567; const double EPS = 1e-10; const double PI = acos(-1.0); int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; //math //------------------------------------------ int QP(int a, int b) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % MOD; a = 1ll * a * a % MOD; } while (b >>= 1); return ans; } int QP(int a, int b, int MOD) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % MOD; a = 1ll * a * a % MOD; } while (b >>= 1); return ans; } int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } //debug //------------------------------------------ #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; //grobal variable //------------------------------------------ int N, M, K, Q; vector<pair<int, int>> WV, X; //main //------------------------------------------ ll LCM(ll a, ll b) { return a * b / GCD(a, b); } signed main() { string S; cin >> N; cin >> S; cin >> Q; bool rev = false; int T, A, B; char c; REP(i, 0, Q){ cin >> T >> A >> B; A--; B--; if (rev) { A = (A + N) % (2*N); B = (B + N) % (2*N); } if (T == 1) { c = S[A]; S[A] = S[B]; S[B] = c; } else { rev = !rev; } } if(rev){ cout<<S.substr(N, N)<<S.substr(0, N)<<endl; }else{ cout<<S<<endl; } return 0; }
#include <bits/stdc++.h> /* #include <atcoder/lazysegtree> */ #include <iostream> #include <regex> using namespace std; /* using namespace atcoder; */ using pint = pair<int, int>; using ll = long long; using ull = unsigned long long; using vll = vector<long long>; using pll = pair<ll, ll>; #define FOR(i, begin, end) \ for (long long i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) #define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite) #define FI first #define SE second #define ALL(v) v.begin(), v.end() #define endl "\n" #define ciosup \ cin.tie(0); \ ios::sync_with_stdio(false); #define eb emplace_back #define vint vector<int> constexpr ll INF = 1e15 + 7LL; constexpr ll MOD = 998244353LL; template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (int i = 0; i < v.size(); ++i) { is >> v[i]; } return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { if (v.size() == 0) return os; for (int i = 0; i < v.size() - 1; ++i) { os << v[i] << " "; } os << v[v.size() - 1]; return os; } template <typename... T> void In(T&&... args) { (std::cin >> ... >> args); } template <typename T, typename ...Tail> void Out(T head, Tail... tail) { std::cout << head; if constexpr(sizeof...(tail) > 0) { std::cout << ' '; Out(tail...); } else { std::cout << '\n'; } } ll sq_ll(ll x) { ll lo = 0, hi = 3e9; while (hi - lo > 1) { ll mid = (hi + lo) / 2LL; if (mid*mid <= x) { lo = mid; } else { hi = mid; } } return lo; } ll helper1(ll x) { if (x < 0) { x += abs(x) % 10000; } else if (x > 0) { if (x % 10000 != 0) x += 10000 - (x % 10000); } return x; } ll helper2(ll x) { if (x > 0) { x -= x % 10000; } else if (x < 0) { if (abs(x) % 10000 != 0) x -= 10000 - (abs(x) % 10000); } return x; } void solve() { double x,y,r; In(x,y,r); ll xi = round(x*10000), yi = round(y*10000), ri = round(r*10000); ll up = yi + ri, down = yi - ri; while (abs(down) % 10000 != 0) ++down; ll ans = 0, cur = down; while (cur <= up) { ll xwidsq = ri*ri - (yi-cur)*(yi-cur); ll xwid = sq_ll(xwidsq); ll xlow = helper1(xi - xwid)/10000LL, xup = helper2(xi + xwid)/10000LL; if (xup >= xlow) ans += xup - xlow + 1; cur += 10000; } Out(ans); } int main() { solve(); char tmp; while (cin >> tmp) { cin.putback(tmp); solve(); } }
#include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; //required using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define debug(x) cout << #x << " is " << x << "\n" #define yes cout << "YES\n" #define no cout << "NO\n" #define flush cout.flush() typedef long long ll; const int MOD = 1e9 + 7; const ll INF = 0x3f3f3f3f3f3f3f3f; // ll floor1(ll d, ll t){ // return d / (t + 1); // } // earliest time ll calc(ll t0, ll c, ll d){ ll mid = max(t0 + 1, (ll) sqrt(d)); ll res = c + t0 + d / (t0 + 1); res = min(res, c + (mid - 1) + d / mid); res = min(res, c + (mid) + d / (mid + 1)); res = min(res, c + (mid + 1) + d / (mid + 2)); return res; // ll lo = t0 + 1, hi = d + 1; // if (lo < hi){ // ll mid = lo + (hi - lo) / 2; // if (d / (mid + 1) - d / (mid + 2) > 1){ // lo = mid + 1; // } // else{ // hi = mid; // } // } // debug(lo); // return c + lo + d / (lo + 1); } int main(){ fast; int n, m; cin >> n >> m; vector<vector<tuple<int, ll, ll>>> edges(n + 1); vector<ll> time(n + 1, INF); vector<bool> vis(n + 1, false); time[1] = 0; int a, b; ll c, d; for (int i = 0; i < m; ++i){ cin >> a >> b >> c >> d; if (a != b){ edges[a].emplace_back(make_tuple(b, c, d)); edges[b].emplace_back(make_tuple(a, c, d)); } } priority_queue<pair<ll, int>> q; q.push(make_pair(0, 1)); int u; ll t, tnew; while (q.size()){ t = -q.top().first; u = q.top().second; q.pop(); if (vis[u]){ continue; } vis[u] = true; for (auto [v, c, d]: edges[u]){ // debug(u); // debug(v); tnew = calc(t, c, d); if (tnew < time[v]){ time[v] = tnew; // cout << ' ' << v << ' ' << tnew << endl; q.push(make_pair(-tnew, v)); } } } // for (int i = 1; i <= n; ++i){ // cout << ' ' << time[i]; // } // cout << '\n'; if (!vis[n]){ cout << -1 << endl; } else { cout << time[n] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char c1, c2, c3; cin >> c1 >> c2 >> c3; cout << ((c1 == c2 && c1 == c3) ? "Won" : "Lost") << endl; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; void*wmem; char memarr[96000000]; template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } template<class T> int Factor_L(T N, T fac[], int fs[]){ T i; int sz = 0; if(N%2==0){ fac[sz] = 2; fs[sz] = 1; N /= 2; while(N%2==0){ N /= 2; fs[sz]++; } sz++; } for(i=3;i*i<=N;i+=2){ if(N%i==0){ fac[sz] = i; fs[sz] = 1; N /= i; while(N%i==0){ N /= i; fs[sz]++; } sz++; } } if(N > 1){ fac[sz] = N; fs[sz] = 1; sz++; } return sz; } template<class T> int Divisor_L(T N, T res[], void *mem = wmem){ int i; int j; int k; int s; int sz = 0; T*fc; int*fs; int fsz; walloc1d(&fc, 100, &mem); walloc1d(&fs, 100, &mem); fsz =Factor_L(N, fc, fs); res[sz++] = 1; for(i=(0);i<(fsz);i++){ s = sz; k = s * fs[i]; for(j=(0);j<(k);j++){ res[sz++] = res[j] * fc[i]; } } sort(res, res+sz); return sz; } int ys; long long y[100000]; int main(){ int i; wmem = memarr; long long S; rd(S); long long P; rd(P); ys =Divisor_L(P, y); for(i=(0);i<(ys);i++){ if(y[i] + (P/y[i]) == S){ wt_L("Yes"); wt_L('\n'); return 0; } } wt_L("No"); wt_L('\n'); return 0; } // cLay version 20201121-1 // --- original code --- // int ys; ll y[1d5]; // { // ll @S, @P; // ys = Divisor(P, y); // rep(i,ys) if(y[i] + (P/y[i]) == S) wt("Yes"), return 0; // wt("No"); // }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define mk make_pair #define fi first #define se second #define mset(a,b) memset(a, b, sizeof(a)) #define dbg(x) cout << "[" << #x << "]: " << x << endl; #define forn(i,n) for(int i=0; i < n;i++) #define forab(i,a,b) for(int i = a; i <= b; i++) #define forba(i,b,a) for(int i = b; i >= a; i--) #define each(val, v) for(auto val : v) #define abs(u) (u >= 0 ? u : -u) using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; const int MAXN = 2e5 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e9+7; void solve(){ int n,m; cin >> n >> m; vi w(n); int mais_gordo = 0; forn(i,n){ cin >> w[i]; mais_gordo = max(mais_gordo, w[i]); } vector<pii> a(m); int minn_guenta = INF; forn(i,m){ cin >> a[i].fi; cin >> a[i].se; minn_guenta = min(minn_guenta, a[i].se); } if(mais_gordo > minn_guenta){ cout << -1 << '\n'; return; } sort(a.begin(), a.end()); stack<pii> s; forn(i,m){ while(!s.empty() && s.top().se > a[i].se){ s.pop(); } s.push(a[i]); } vector<pii> valids; while(!s.empty()){ pii aux = s.top(); valids.pb(aux); s.pop(); } m = valids.size(); reverse(valids.begin(), valids.end()); if(n == 1){ cout << 0 << '\n'; return; } sort(w.begin(), w.end()); int maior = w[n-1]; w.pop_back(); int ans = INF; do{ vi a(n); forn(i,n-1){ a[i] = w[i]; } a[n-1] = maior; vi min_v(n,0); forn(i, n-1){ int my = min_v[i]; int at = 0; int vat = 0; int soma = w[i]; for(int j = i+1; j < n; j++){ soma += w[j]; while( at < m && valids[at].se < soma){ vat = valids[at].fi; at++; } min_v[j] = max(min_v[j], vat+min_v[i]); } } ans = min(ans, min_v[n-1]); }while( next_permutation(w.begin(), w.end())); cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
// #pragma GCC optimize("Ofast") // #pragma GCC optimize ("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long int ll; #define endl '\n' #define ld long double #define all(a) a.begin(),a.end() #define int long long #define pb push_back #define pii pair <int, int> #define ff first #define ss second #define sz(v) (int)v.size() #define UB upper_bound #define LB lower_bound #define BP(x) __builtin_popcountll(x) #define PQS priority_queue <int, vector<int>, greater<int> > #define OST tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update> mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int getRand(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rng); } const int INF = 1e18 + 0; const int mod = 1e9 + 7; //const int mod = 998244353; const int N = 1e5+ 5; int dp[10][10], pre[10], cap[N], L[N]; vector <pii> vv; bool com(pii p1, pii p2) { if(p1.ff == p2.ff) return p1.ss > p2.ss; return p1.ff > p2.ff; } int get(int x, int m) { int lo = 0, hi = m-1; int ret = 0; while(hi >= lo) { int mid = (hi + lo)/2; if(x > vv[mid].ff) { ret = max(ret, vv[mid].ss); hi = mid-1; } else lo = mid+1; } return ret; } void solve() { int n, m, f = 0; cin >> n >> m; vector <int> v(n+1,0); for(int i = 1; i <= n; i++) cin >> v[i]; sort(v.begin()+1, v.end()); for(int i = 1; i <= m; i++) { cin >> L[i] >> cap[i]; if(v.back() > cap[i]) f = 1; vv.pb({cap[i], L[i]}); } if(f) { cout << "-1\n"; return; } sort(all(vv), com); int mx = -1; for(int i = m-1; i >= 0; i--) { mx = max(vv[i].ss, mx); vv[i].ss = mx; } int ans = INF; do { memset(dp,0,sizeof(dp)); memset(pre,0,sizeof(pre)); for(int i = 1; i <= n; i++) { pre[i] = v[i] + pre[i-1]; } for(int i = 1; i <= n; i++) { for(int j = i+1; j <= n; j++) { int x = pre[j] - pre[i-1]; int res = get(x, m); dp[i][j] =res; } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { for(int k = i; k <= j; k++) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]); } } } ans = min(ans, dp[1][n]); } while(next_permutation(v.begin()+1, v.end())); cout << ans << '\n'; } /* * overflow, array bounds * do smth instead of nothing and stay organized */ signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; //cin >> t; for(int i = 1; i <= t; i++) { // cout << "Case #" << i << ": "; solve(); } } /* */
#include <iostream> #include <vector> #include <set> #include <bitset> #include <algorithm> #include <cmath> using namespace std; int main() { int N, M; cin >> N >> M; vector<set<int>> Graph(N); vector<int> A(M), B(M); for (int i = 0; i < M; i++) cin >> A[i] >> B[i], A[i]--, B[i]--; for (int i = 0; i < M; i++) { Graph[A[i]].insert(B[i]); Graph[B[i]].insert(A[i]); } vector<bool> comp(1 << N, false); for (int i = 0; i < N; i++) { comp[1 << i] = true; } for (int i = 1; i < (1 << N); i++) { if (!comp[i]) continue; for (int j = 0; j < N; j++) { if (i & (1 << j)) continue; bool judge = true; for (int k = 0; k < N; k++) { if ((i & (1 << k)) && !(Graph[j].count(k))) judge = false; } if (judge) comp[i | (1 << j)] = true; } } vector<int> dp(1 << N, 18); dp[0] = 0; for (int i = 0; i < (1 << N); i++) { int revi = (((1 << N) - 1) ^ i); for (int j = revi; j &= revi; j--) { if (comp[j]) { dp[i ^ j] = min(dp[i ^ j], dp[i] + 1); } } } cout << dp[(1 << N) - 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) #define rep1(i,n) for (int i = 1; i <= (n); ++i) #define bit(n,k) ((n>>k)&1) //nのk bit目 #define vec(T) vector<T> #define vvec(T) vec(vec(T)) 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 ll llINF = 1LL << 60; const int iINF = 1e9; //------------------------------------------------ struct Solver{ void solve(){ int N,M; cin >> N >> M; vec(int) ed(N); //ある頂点から辺のつながってる先リスト rep(i,M){ int a,b; cin >> a >> b; --a; --b; ed[a] |= 1<<b; // ビットで情報を保存 ed[b] |= 1<<a; } vec(int) dp(1<<N, iINF); dp[0]=1; // 全結合の集合のとこを1に初期化 rep(si,1<<N)rep(ni,N){ // siが全結合かつsiの集合に頂点niを足しても全結合なら1 if( dp[si]==1 && (ed[ni]&si)==si ) dp[si | 1<<ni] = 1; } // dp rep1(si,(1<<N)-1){ for(int sj=si; (--sj)&=si ; ){ if( sj&si ) chmin(dp[si], dp[si^sj]+dp[sj]); } } cout << dp.back() << endl; } }; int main(){ int testcasenum=1; //cin >> testcasenum; rep1(ti,testcasenum){ Solver solver; solver.solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define fastIO ios_base::sync_with_stdio(false); cin.tie(0); int main() { fastIO; ll n; cin>>n; ll i = 1; while(i) { ll temp = stoll(to_string(i) + to_string(i)); if(temp > n) { cout<<i-1<<endl; break; } i++; } }
#include <bits/stdc++.h> using namespace std; int main() { long long n, c = 0; cin >> n ; for(int i = 1; stoll(to_string(i)+to_string(i)) <= n ; i++) c++; cout << c ; }
#define _USE_MATH_DEFINES #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> #include<math.h> #include<iomanip> #include<stdio.h> #include <stdlib.h> #include<stdio.h> #include <queue> #include<map> #include <sstream> #include<set> #include<stack> #include<time.h> typedef long long int ll; //#include<bits/stdc++.h> using namespace std; int main() { int n; int x; cin >> n >> x; int t = -1; x *= 100; for (int i = 0; i < n; i++) { double a, b; cin >> a >> b; x -= a * b; if (x < 0&&t==-1)t = i + 1; } cout << t << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { double t; cin>>t; long long n; cin>>n; long long res = ceil((n*100)/t) + n-1; cout<<res; }
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; int n; int cnt1[500][500]; int cnt2[500][500]; int team[500][500]; int tgt=0; bool deta=false; int col[500][500]; int main(void){ scanf("%d",&n); if(n==1){ printf("1\n"); printf("AB\n"); return 0; } if(n==2){ printf("3\n"); printf("AABB\n"); printf("ABAB\n"); printf("ABBA\n"); return 0; } col[0][2]=1; col[0][3]=1; col[1][1]=1; col[1][3]=1; col[2][1]=1; col[2][2]=1; int m=(1<<n); int s=2; int sz=3; int len=4; for(int i=2;i<n;i++){ for(int j=0;j<sz;j++){ for(int k=0;k<len;k++){ col[j+sz][k]=col[j][k]; } } for(int j=0;j<sz;j++){ for(int k=0;k<len;k++){ col[j][k+len]=col[j][k]; col[j+sz][k+len]=1-col[j+sz][k]; } } for(int j=0;j<len;j++){ col[sz*2][j+len]=1; } sz=sz*2+1; len=len*2; } printf("%d\n",m-1); for(int i=0;i<m-1;i++){ for(int j=0;j<m;j++){ printf("%c",col[i][j]==0?'A':'B'); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define ll long long #define pp pair<ll,ll> #define ld long double #define all(a) (a).begin(),(a).end() #define mk make_pair int inf=1000001000; ll INF=2e18; ll mod=1000000007; ll MOD=998244353; int main() { int t; cin >> t; rep(ii,t){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; if (n%2==0){ sort(all(a)); bool t=true; rep(i,n/2){ if (a[i*2]!=a[i*2+1]) t=false; } if (t) cout << "Second" << endl; else cout << "First" << endl; } else cout << "Second" << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N], b[N], p[N], rp[N], id[N]; void gao(int x, int y) { if(a[x]<=b[p[x]] || a[y]<=b[p[y]]) { puts("-1"); exit(0); } rp[p[y]] = x, rp[p[x]] = y; swap(p[x], p[y]); } int main() { scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", a+i); for(int i=1; i<=n; i++) scanf("%d", b+i); for(int i=1; i<=n; i++) scanf("%d", p+i), rp[p[i]] = i; iota(id+1, id+n+1, 1); sort(id+1, id+n+1, [&](int x, int y) { return a[x] < a[y]; }); vector<pair<int, int>> seq; for(int i=1; i<=n; i++) { int j = id[i], k = rp[j]; if(j==k) continue; if(a[k]>b[p[j]]) { gao(j, k); seq.emplace_back(j, k); } else { gao(k, n); seq.emplace_back(k, n); gao(j, n); seq.emplace_back(j, n); } } for(int i=1; i<=n; i++) assert(i==p[i]); printf("%d\n", (int)seq.size()); for(auto &it : seq) printf("%d %d\n", it.first, it.second); return 0; }
//#define _GLIBCXX_DEBUG #include<bits/stdc++.h> using namespace std; #define endl '\n' #define lfs cout<<fixed<<setprecision(10) #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define spa << " " << #define fi first #define se second #define MP make_pair #define MT make_tuple #define PB push_back #define EB emplace_back #define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++) #define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair<ll, ll>; template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});} void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;} void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;} void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;} template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}}; void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}}; template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;}; template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1}; template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);} template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));} template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;} template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;} template<typename T>void rearrange(vector<ll>&ord, vector<T>&v){ auto tmp = v; for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]]; } template<typename Head, typename... Tail>void rearrange(vector<ll>&ord,Head&& head, Tail&&... tail){ rearrange(ord, head); rearrange(ord, tail...); } //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); int popcount(ll x){return __builtin_popcountll(x);}; int poplow(ll x){return __builtin_ctzll(x);}; int pophigh(ll x){return 63 - __builtin_clzll(x);}; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll res=0,buf=0; bool judge = true; ll n;cin>>n; vector<ll>a(n),b(n),p(n); rep(i,0,n)cin>>a[i]; rep(i,0,n)cin>>b[i]; rep(i,0,n)cin>>p[i],p[i]--; vector<ll>rev(n); rep(i,0,n){ rev[p[i]]=i; if(i!=p[i]&&a[i]<=b[p[i]]){ cout<<-1<<endl; return 0; } } vector<P>ret; vector<bool>ok(n); rep(i,0,n){ if(ok[i])continue; vector<ll>v; ll now=i; while(!ok[now]){ ok[now]=true; v.PB(now); now=p[now]; } sort(ALL(v),[&](ll x,ll y){ return b[x]>b[y]; }); rep(j,0,(ll)v.size()-1){ ll x=rev[v[j]],y=v[j]; ret.EB(x+1,y+1); swap(p[x],p[y]); swap(rev[p[x]],rev[p[y]]); } } //debug(p,n); cout<<ret.size()<<endl; for(auto z:ret)cout<<z<<endl; return 0; }
//ABC182_C_To 3 bit_all+組み合わせ// #include <bits/stdc++.h> //repマクロの定義// #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int f(long long a){ long long ga=a; int A[11]; int j=0; while(ga>0){ A[j]=ga%10; ga/=10; j++; } sort(A,A+j); int g=0; int G=0; long long gj=1; long long Gj=1; rep(m,j-1){ Gj*=10; } for (int i=0;i<j;i++){ g+=A[i]*gj; G+=A[i]*Gj; gj*=10; Gj/=10; } return g-G; } int main(){ //入力を用意// long long K,N; cin >> N >> K; int a=f(N); if(K==0){ a=N; }else if(K==1){ a=f(N); }else{ for(int i=1;i<K;i++){ a=f(a); } } //出力// cout << a << endl; return 0; }
#include<bits/stdc++.h> #define ll long long using namespace std; ll n,k,a[20],b[20]; ll getlen(){ for(ll i=10;i>=1;i--){ if(a[i]!=0)return i; } return 0; } int main(){ cin >> n >> k; if(k==0){ cout << n; return 0; } ll i=0; while(n>0){ i++; a[i]=n%10; n/=10; } while(k--){ ll t=getlen(); //printf("t=%lld\n",t); sort(a+1,a+t+1); n=0; for(ll i=t;i>=1;i--){ n=n*10+a[i]; } ll m=0; for(ll i=1;i<=t;i++){ m=m*10+a[i]; a[i]=0; } n-=m; if(k==0)cout << n; i=0; while(n>0){ i++; a[i]=n%10; n/=10; } } }
#include<bits/stdc++.h> using namespace std; string S; int main(){ cin >> S; int ans = 0; for(int i=0; i<S.size(); i++){ if(i%2==0){ //奇数番目 if(isupper(S[i])){ ans += 1; } } //大文字の時 else{ if(islower(S[i])){ //偶数番目 ans += 1; } } } if(ans == 0){ cout << "Yes" << endl; } else{ cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define MOD 1000000007 #define fi(i,a,n) for(lli i = a; i < n; i++) #define fd(i,a,n) for(lli i = a; i > n; i--) #define fv(i,a) for(auto i:a) #define pb push_back #define mp make_pair int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout<<fixed<<setprecision(10); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif lli t=1; //cin>>t; while(t--) { string s; cin>>s; fi(i,0,s.size()){ if(i%2==0 && isupper(s[i])) { cout<<"No"; return 0; } if(i%2==1 && islower(s[i])) { cout<<"No"; return 0; } } cout<<"Yes"; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) #define rep2(i,x,n) for(int i=x; i<(n); i++) #define ALL(n) begin(n),end(n) using namespace std; using P = pair<int, int>; using ll = long long; int main() { int n; cin >> n; vector<int> a(n), b(n); rep(i,n) cin >> a[i] >> b[i]; int ans = INT_MAX; for(int i=0; i<n; i++){ for(int j=i+1; j<n; j++){ ans = min(ans, max(a[i], b[j])); ans = min(ans, max(a[j], b[i])); } } for(int i=0; i<n; i++){ ans = min(ans, a[i]+b[i]); } cout << ans << endl; return 0; }
#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("fast-math") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define deb(x) cout << #x << " is " << x << "\n" #define int long long #define MOD 1000000007LL #define PI acos(-1) template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using max_heap = priority_queue<T>; template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename... T> void read(T &...args) { ((cin >> args), ...); } template <typename... T> void write(T &&...args) { ((cout << args), ...); } template <typename T> void readContainer(T &t) { for (auto &e : t) { read(e); } } template <typename T> void writeContainer(T &t) { for (const auto &e : t) { write(e, " "); } write("\n"); } auto speedup = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }(); void solve(int tc) { int N; read(N); vector<pair<int, int>> taken(N); for (int i = 0; i < N; i++) { read(taken[i].first, taken[i].second); } int ans = INT_MAX; for (int i = 0; i < N; i++) { ans = min(ans, taken[i].first + taken[i].second); for (int j = 0; j < N; j++) { if (i != j) { ans = min({ans, max(taken[i].first, taken[j].second), max(taken[i].second, taken[j].first)}); } } } write(ans); } signed main() { int tc = 1; // read(tc); for (int curr = 1; curr <= tc; ++curr) { solve(curr); } return 0; }
#include <iostream> #include <string> #include <algorithm> #include <map> using namespace std; typedef long long ll; typedef long double ld; map<ll,ld> dp; ld dfs(ll a,ll b,ll c){ if(a>=100 || b>=100 || c>=100) return 0; else if(dp.count(10000*a+100*b+c)>=1) return dp[10000*a+100*b+c]; else{ ld res = 0; res += (ld)((1.0+dfs(a+1,b,c))*a)/(a+b+c); res += (ld)((1.0+dfs(a,b+1,c))*b)/(a+b+c); res += (ld)((1.0+dfs(a,b,c+1))*c)/(a+b+c); dp[10000*a+100*b+c] = res; return res; } } int main(){ ll a,b,c; cin >> a >> b >> c; printf("%.12Lf\n",dfs(a,b,c)); return 0; }
#include <algorithm> #include <cassert> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #include <limits.h> using namespace std; typedef long long ll; 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; } double dp[105][105][105]; bool visited[105][105][105]; int main(void) { int A, B, C; cin >> A >> B >> C; dp[A][B][C] = 1; queue<tuple<int, int, int>> q; q.push(make_tuple(A, B, C)); visited[A][B][C] = true; while (!q.empty()) { tuple<int, int, int> elem = q.front(); q.pop(); int a, b, c; tie(a, b, c) = elem; double prob = dp[a][b][c]; dp[a + 1][b][c] += prob * double(a) / double(a + b + c); if (a + 1 < 100 && !visited[a + 1][b][c]) { q.push(make_tuple(a + 1, b, c)); visited[a + 1][b][c] = true; } dp[a][b + 1][c] += prob * double(b) / double(a + b + c); if (b + 1 < 100 && !visited[a][b + 1][c]) { q.push(make_tuple(a, b + 1, c)); visited[a][b + 1][c] = true; } dp[a][b][c + 1] += prob * double(c) / double(a + b + c); if (c + 1 < 100 && !visited[a][b][c + 1]) { q.push(make_tuple(a, b, c + 1)); visited[a][b][c + 1] = true; } } double ans = 0; for (int a = 0; a <= 100; a++) { for (int b = 0; b <= 100; b++) { for (int c = 0; c <= 100; c++) { if (a == 100 || b == 100 || c == 100) { ans += dp[a][b][c] * ((a + b + c) - (A + B + C)); } } } } printf("%.7f\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; long double pi = 2 * acos(0.0); const ll mod = 998244353; void solve() { ll n; cin >> n; vector<pair<ll, ll>> v(n); for(ll i = 0; i < n; i++) cin >> v[i].first >> v[i].second; ll ans = 0; for(ll i = 0; i < n; i++) { for(ll j = i + 1; j < n; j++) { ld m = (ld)(v[j].second - v[i].second) / (v[j].first - v[i].first); if(m >= -1 && m <= 1) ans++; } } cout << ans; } int main() { ios_base :: sync_with_stdio(false); cin.tie(nullptr); ll t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, a, b) for(ll i = a; i < b; i++) #define Rep(i, a, b) for(ll i = a; i <= b; i++) #define repr(i, a, b) for(ll i = b-1; i >= a; i--) // #define _GLIBCXX_DEBUG template <class T> using V = vector<T>; #define ALL(v) (v).begin(),(v).end() #define endl '\n' #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define sz(v) ((ll)(v).size()) const double pi = acos(-1.0); const ll MOD = 1000000007LL; // const ll MOD = 998244353LL; const ll INF = 1LL << 60; const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; const int dy2[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dx2[] = {1, 1, 0, -1, -1, -1, 0, 1}; // ios::sync_with_stdio(false); // cin.tie(nullptr); /*-------------------------------------------------------------------------------- --------------------------------------------------------------------------------*/ int main() { ll n; cin >> n; V<ll> x(n), y(n); rep(i, 0, n) cin >> x[i] >> y[i]; ll ans = 0; rep(i, 0, n-1){ rep(j, i+1, n){ ll h = abs(y[j]-y[i]); ll w = abs(x[j]-x[i]); if(h <= w) ans++; } } cout << ans << endl; }
#include <iostream> #include <string> #include <vector> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) ::std::begin(x), ::std::end(x) using namespace std; char solve(int64_t A, int64_t B, int64_t C) { if((C % 2) == 0){ A *= A; B *= B; } if(A == B) return '='; if(A < B) return '<'; if(A > B) return '>'; } // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr char endl = '\n'; int64_t A, B, C; cin >> A >> B >> C; auto ans = solve(A, B, C); cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long // #include<ext/pb_ds/assoc_container.hpp> // #include<ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>PBDS; #define mod 1000000007 #define INF 10000000000007 #define DEBUG false #define N 200005 ll gcd(ll a , ll b) {return b == 0 ? a : gcd(b, a % b);} ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } void solve() { ll a, b, c; cin >> a >> b >> c; if (c % 2 == 0) { a = a * a; b = b * b; } if (a > b)cout << ">"; else if (a < b)cout << "<"; else cout << "="; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output2.txt", "w", stdout); #endif ll t = 1; //cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define fi first #define se second //#define ivorysi #define enter putchar('\n') #define space putchar(' ') #define pii pair<int,int> typedef long long int64; using namespace std; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) out(x / 10); putchar('0' + x % 10); } const int MAXM = 100005; int N,M; int64 w[10],l[MAXM],v[MAXM],ans,sum[10],dis[10][10],dp[10]; int64 val[MAXM],len[MAXM]; int a[10],id[MAXM]; bool vis[10]; void dfs(int d) { if(d > N) { for(int i = 1 ; i <= N ; ++i) { sum[i] = sum[i - 1] + w[a[i]]; } memset(dis,0,sizeof(dis)); for(int j = 1 ; j <= N ; ++j) { for(int t = j + 1 ; t <= N ; ++t) { int cv = sum[t] - sum[j - 1]; int p = lower_bound(val + 1,val + M + 1,cv) - val - 1; if(val[p] < cv) { dis[j][t] = len[p]; } } } memset(dp,0,sizeof(dp)); for(int i = 1 ; i <= N ; ++i) { for(int j = i + 1 ; j <= N ; ++j) { dp[j] = max(dp[j],dp[i] + dis[i][j]); } } ans = min(ans, dp[N]); return; } for(int i = 1 ; i <= N ; ++i) { if(!vis[i]) { a[d] = i; vis[i] = 1; dfs(d + 1); vis[i] = 0; } } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif read(N);read(M); for(int i = 1 ; i <= N ; ++i) read(w[i]); for(int i = 1 ; i <= M ; ++i) { read(l[i]);read(v[i]); id[i] = i; ans += l[i]; } ans = ans * N; sort(id + 1,id + M + 1,[](int a,int b) {return v[a] < v[b] || (v[a] == v[b] && l[a] > l[b]);}); for(int i = 1 ; i <= M ; ++i) { val[i] = v[id[i]]; len[i] = l[id[i]]; len[i] = max(len[i],len[i - 1]); } for(int i = 1 ; i <= M ; ++i) { for(int j = 1 ; j <= N ; ++j) { if(w[j] > v[i]) { puts("-1"); return 0; } } } dfs(1); out(ans);enter; return 0; }
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <set> #include <queue> #include <map> #include <string> //#include <atcoder/all> #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--) #define pb push_back #define mp make_pair #define bg begin() #define en end() #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define sz(v) (int)(v).size() #define vi vector<int> #define vll vector<long long> using namespace std; //using namespace atcoder; //using mint = modint1000000007; //using mint = modint998244353; //using mint = modint; typedef long long ll; typedef long double ld; using P = pair<ll, ll>; template<typename T> struct Edge { int u, v; T cost; Edge(int u, int v, T c) : u(u), v(v), cost(c) {} bool operator< (const Edge &e) const {return cost < e.cost;} }; static const long long MOD = 1000000007; static const long long LINF = (ll)(1e18+99); static const int INF = 1e9+99; ll ans = LINF; int main(void) { int N, M; cin >> N >> M; vll W(N, 0), perm(N, 0); vector<P> V; rep(i, 0, N) cin >> W[i]; rep(i, 0, N) perm[i] = i; rep(i, 0, M) { ll l, v; cin >> l >> v; V.pb(mp(v, l)); } sort(all(V)); rep (i, 0, N) if (W[i] > V[0].first) { puts("-1"); return 0; } rep(i, 1, M) V[i].second = max(V[i].second, V[i-1].second); // ラクダの順列全探索 do { vll dist(N, 0); // ラクダの配置座標 rep(i, 1, N) { ll w = W[perm[i]]; dist[i] = dist[i-1]; per(j, 0, i) { w += W[perm[j]]; int x = lower_bound(all(V), mp(w, 0LL)) - V.bg; if (x==0) continue; ll mxl = V[x-1].second; dist[i] = max(dist[i], dist[j]+mxl); } } ans = min(dist[N-1], ans); } while (next_permutation(all(perm))); cout << ans << endl; return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #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 namespace std; using ll = int64_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;} struct UnionFind { vector<int> par; // 親を指すvector,-par[親]は木のサイズ UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり int root(int x) { // 親をたどる&データの整理 if(par[x] < 0) return x; return par[x] = root(par[x]); } bool unite(int x, int y) { // データの結合 x = root(x); y = root(y); if(x == y) return false; if(par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) {return root(x) == root(y);} // 所属判定 int size(int x) {return -par[root(x)];} // 木のサイズ }; //head #define N 400010 int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vi cnt(N); UnionFind uft(N); rep(i, n) { int a, b; cin >> a >> b; uft.unite(a, b); cnt[a]++; } rep(i, N) if(uft.root(i) != i) cnt[uft.root(i)] += cnt[i]; int ans = 0; rep(i, N) if(uft.root(i) == i) { ans += min(uft.size(i), cnt[i]); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<ll> vl; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef vector<int> vi; typedef pair<int, pair<int,int> > ppi; const ll mod = 1e9+7; const int MAXN=4e5+3; void pvec(vi v){ for(auto x:v){ cout << x << " "; }cout << endl; } ll gcd(ll x, ll y){ if(y==0){ return x; }else{ return gcd(y, x%y); } } ll binpow(ll a, ll b) { if(b==0){ return 1; } ll res = 1; ll tmp = binpow(a,b/2); res = (tmp*tmp); if(b%2==1){ res = (res*a); } return res; } vector<vi> ad(MAXN); vector<bool> vis(MAXN,false), mk(MAXN,false); vi sz(MAXN, 1); void dfs(int u, int st, int par=0){ vis[u] = true; for(int v : ad[u]){ if(!vis[v]){ sz[st]++; dfs(v, st, u); }else if(v != par){ mk[st] = true; } } } void solve(){ int n; cin>>n; vi gd(MAXN, 0); for(int i=0; i<n; i++){ int x,y; cin>>x>>y; if(x==y){ // mk[x] = true; // gd[x] = 1; ad[x].push_back(x); }else{ ad[x].push_back(y); ad[y].push_back(x); } } int s=0; for(int i=1; i<MAXN; i++){ if(!vis[i]){ dfs(i,i); s += sz[i]; if(!mk[i] && gd[i]==0) s--; // if(sz[i]>1) cout << i << " " << sz[i] << "\n"; } } cout << s << "\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); } }
#include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i=a; i<=b; i++) #define trav(a, x) for(auto& a:x) #define all(x) begin(x), end(x) #define sz(x) (int) x.size() #define f first #define s second #define nl "\n" #define pb push_back typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MOD=3; template<class T> using pqg=priority_queue<T, vector<T>, greater<T>>; int col[400000]; int cnt[400001]; int n; string s; map<char, int> mp; map<int, char> rmp; const int MAXN=500000; ll fac[MAXN+5]; ll inv(ll a){ if(a==1) return 1; if(a==2) return 2; } ll choose(ll n, ll k){ if(k>n) return 0; if(cnt[n]>cnt[n-k]+cnt[k]) return 0; return (fac[n]*inv(fac[k])*inv(fac[n-k]))%3; } int main(){ ios::sync_with_stdio(false); cin.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; fac[0]=1; for(ll i=1; i<=n; i++){ int x=i; while(x/3){ cnt[i]+=x/3; x/=3; } x=i; while(x%3==0) x/=3; fac[i]=(fac[i-1]*x)%3; // cout << i << " fac " << fac[i] << nl; } cin >> s; mp['R']=0; mp['B']=1; mp['W']=2; rmp[0]='R'; rmp[1]='B'; rmp[2]='W'; int ans=0; rep(i, 0, n-1){ // cout << mp[s[i]] << nl; // cout << choose(n-1, i) << " "; ans+=choose(n-1, i)*mp[s[i]]; ans%=3; } if(n%2==0) ans=(3-ans)%3; cout << rmp[ans]; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; // nCk mod p struct Lucas { vector<vector<long long>> table; long long p; // prime number Lucas(long long _p) : p(_p) { init(p); } void init(long long p) { table.assign(p, vector<long long> (p)); table[0][0] = 1; for(int i = 1; i < p; i++) { table[i][0] = 1; for(int j = i; j > 0; j--) { table[i][j] = (table[i - 1][j - 1] + table[i - 1][j]) % p; } } } long long comb(long long n, long long k) { long long ret = 1; while(n | k) { ret *= table[n % p][k % p]; ret %= p; n /= p; k /= p; } return ret; } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n; string s; cin >> n >> s; int B = 0, W = 1, R = 2; vector<int> a(n); rep(i,n){ if(s[i] == 'B') a[i] = B; if(s[i] == 'W') a[i] = W; if(s[i] == 'R') a[i] = R; } Lucas l(3); int ans = 0; rep(i,n) ans = (ans + l.comb(n - 1, i) * a[i]) % 3; if(n % 2 == 0) ans = (-ans + 3) % 3; if(ans == B) cout << 'B' << endl; if(ans == W) cout << 'W' << endl; if(ans == R) cout << 'R' << endl; }
#include <iostream> #include <vector> #include <array> #include <algorithm> #include <cstring> #include <queue> #include <iomanip> #include <numeric> #include <cmath> #include <cstdlib> #include <sstream> #include <bitset> using namespace std; using ll = long long; using Vec = vector<ll>; using P = pair<ll, ll>; using VecP = vector<P>; #define rep(i, n) for(ll i=0;i<(n);i++) #define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0])) //static const ll MOD = 1000000007; //static const ll INF = 1000000000; //#define PI 3.14159265359 int main() { // input ll X, Y; cin >> X >> Y; // process ll W = max(X, Y); ll L = min(X, Y); if (W < L + 3) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #pragma GCC optimize ("-O3") #define int long long #define ld long double #define endl "\n" #define rep(i,begin,end) for (__typeof(end) i=begin-(begin>end); i!=(end)-(begin>end); i+=1-2*(begin>end)) #define umap unordered_map #define pq priority_queue #define pb push_back #define mp make_pair #define fs first #define sec second #define lb lower_bound #define ub upper_bound #define mii map<int,int> #define pii pair<int,int> #define vc vector #define vi vc<int> #define vvi vc<vi> #define all(v) v.begin(),v.end() #define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower); #define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper); #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b))/gcd((a),(b)) #define remax(a,b) a = max(a,b) #define remin(a,b) a = min(a,b) #define w(t) int t; cin>>t; rep(tc,0,t) #define clr(a,x) memset(a,x,sizeof a) #define chkbit(x,i) ((x)&(1LL<<(i))) #define setbit(x,i) ((x)|(1LL<<(i))) #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define ps(x,y) fixed<<setprecision(y)<<x #define print(a, n) rep(i,0,n) cout<<a[i]<<" "; cout<<endl; #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; const int mod = 1e9+7; const int mod2 = 998244353; const long long inf = 1e18; const long double PI = 3.141592653589793; template<typename... T> void in(T&... args) {((cin>>args), ...);} template<typename... T> void out(T&&... args) {((cout<<args<<" "), ...);} template<typename... T> void outln(T&&... args) {((cout<<args<<" "), ...); cout<<endl;} int nxt(){int x;cin>>x;return x;} int add(int a,int b,int mod=mod){int res=(a+b)%mod;return (res<0)?res+mod:res;} int mul(int a,int b,int mod=mod){int res=(a*1LL*b)%mod;return (res<0)?res+mod:res;} struct customCompare { bool operator () (int x, int y) { return x>y; } }; /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// const int N = 2e5; int codejam = 0, testcases = 0; void solve() { int x, y; cin >> x >> y; if (max(x, y) < min(x, y)+3) { cout << "Yes"; } else { cout << "No"; } } /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; if (testcases) cin >> t; for (int tc = 1; tc <= t; ++tc) { if (codejam) cout << "Case #" << tc << ": "; solve(); } // printclock; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; int price = (int)(floor(1.08 * N)); if (price == 206){ cout << "so-so"<<endl; } else { cout << (price < 206? "Yay!":":(") <<endl; } return 0; }
#include <iostream> using namespace std; int main() { int N; cin >> N; for (int i = 0; i < N; ++i) { int a = (2 * i + 0) % N; int b = (2 * i + 1) % N; cout << a + 1 << " " << b + 1 << endl; } }
#include <bits/stdc++.h> #define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++) #define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++) #define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--) #define deb(x) std::cout << #x << " " << x << endl; #define debl(x) std::cout << #x << " " << x << " "; #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; namespace std{ template<class Fun> class y_combinator_result{ Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){} template<class ...Args> decltype(auto) operator()(Args&&...args){ return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun && fun){ return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } }; template<typename T> bool umax(T& a, T b){ bool ret = a < b; if(ret) a = b; return ret; } template<typename T> bool umin(T& a, T b){ bool ret = a > b; if(ret) a = b; return ret; } struct edge{ int to; ll cost; int from; edge(){ edge(0,0);} edge(int to_, ll cost_) : to(to_), cost(cost_){} edge(int to_, int from_, ll cost_) : to(to_), cost(cost_), from(from_){} }; template<typename... T> void read(T& ... a){ ((cin >> a),...); } template<typename... T> void write(T... a){ ((cout << a),...); } template<typename T> vector<T> read_array(int sz){ vector<T> ret(sz); for(auto & x : ret) cin >> x; return ret; } constexpr int mxN = 101; ll a[mxN]; int n; ll best[mxN]; ll x, y; ll dp[mxN][mxN][mxN]; void solve(){ read(n); read(x); REP(i, n){ read(a[i]); } ll ans = x - a[0]; REPS(sz, 1, n + 1){ y = x % sz; //initialization REP(i, n){ int need = min(i + 1, sz); REPS(j,1 ,need + 1){ REP(k, sz){ dp[i][j][k] = 0; } } } // deb(sz) dp[0][1][a[0] % sz] = a[0]; REPS(i, 1, n){ int need = min(i + 1, sz); REPS(j,1 ,need + 1){ REP(k, sz){ umax(dp[i][j][k], dp[i-1][j][k]); if( j == 1){ if(a[i] % sz == k ) umax(dp[i][j][k], a[i]); continue; } int k2 = (sz + k - a[i] % sz) % sz; if(dp[i-1][j-1][k2] > 0) umax(dp[i][j][k], dp[i-1][j-1][k2] + a[i]); // debl(i) debl(j) debl(k) // deb(dp[i][j][k]) } } } REP(i, n){ if(dp[i][sz][y] > 0) umin(ans, (x - dp[i][sz][y]) / sz ); } } cout << ans << endl; } int main() { //making data IO Fast std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); /****************************/ int T = 1; // cin >> T; while(T--) solve(); return 0; }
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <functional> #include <bitset> #include <cstddef> #include <type_traits> #include <vector> using namespace std; using lint = long long int; long long int INF = 1e18; int inf = 1000000007; long long int MOD = 1000000007LL; double PI = 3.1415926535897932; template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;} template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;} #define ALL(a) a.begin(),a.end() #define RALL(a) a.rbegin(),a.rend() #define rep(i, n) for(int i=0;i<(int)(n);i++) int main() { lint n, x; cin >> n >> x; vector<lint> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } lint ans = INF; for (lint k = 1; k <= n; k++) { vector<vector<lint>> dp(k + 1, vector<lint>(k, -INF)); dp[0][x % k] = 0; for (int i = 0; i < n; i++) { vector<vector<lint>> nxt(k + 1, vector<lint>(k, -INF)); for (int j = 0; j <= k; j++) { for (int l = 0; l < k; l++) { if (dp[j][l] == -INF) continue; // 使わない chmax(nxt[j][l], dp[j][l]); // 使う if (j + 1 <= k) { chmax(nxt[j + 1][(l + 10000000 * k - a[i]) % k], dp[j][l] + a[i]); } } } swap(dp, nxt); } // cerr << "k = " << k << ", dp = " << dp[k][0] << endl; if (dp[k][0] == -INF) continue; else { // assert((x - dp[k][0]) % k == 0); chmin(ans, (x - dp[k][0]) / k); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pi (3.141592653589) #define mod 1000000007 #define float double #define pb push_back #define mp make_pair #define ff first #define ss second #define rrep(i, n) for(int i=n-1;i>=0;i--) #define rep(i,n) for(int i=0;i<n;i++) #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); vector<int> primeFactors(int n) { vector<int> ans; for(int i=1;i*i<=n;i++) { if(n%i == 0) { ans.push_back(i); if(n/i != i) ans.push_back(n/i); } } return ans; } bool perfectCube(int N) { int cube_root; cube_root = round(cbrt(N)); // If cube of cube_root is equals to N, // then print Yes Else print No if (cube_root * cube_root * cube_root == N) { // cout << "Yes"; return 1; } else { // cout << "NO"; return 0; } } void solve() { int t=1; // cin>>t; while(t--) { string s; cin>>s; map<char,int> mp; int ans = 0; int n = s.size(); mp[s[n-1]] += 1; for(int i=n-2;i>=0;i--) { if(s[i] == s[i+1]) { int minus = 0; if(mp.find(s[i]) != mp.end()) minus = mp[s[i]]; ans += (n-1-i) - minus; mp.clear(); mp[s[i]] = n-i; } else { mp[s[i]] += 1; } } cout<<ans; } } int32_t main(){ solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define ll long long #define int ll #define ld long double #define pii pair<int, int> #define f first #define s second #define readl(_s) getline(cin, (_s)); #define boost() cin.tie(0); cin.sync_with_stdio(0) string s; int cnt[26]; int32_t main() { boost(); cin >> s; int ans = 0; reverse(s.begin(), s.end()); //cnt[s[0] - 'a']++, cnt[s[1] - 'a']++; for (int i = 2; i < s.size(); i++) { cnt[s[i - 2] - 'a']++; if (s[i] == s[i - 1] && s[i] != s[i - 2]) { int id = s[i] - 'a'; ans += i - 1 - cnt[id]; for (int j = 0; j < 26; j++) cnt[j] = 0; cnt[id] = i - 1; } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define eFOR(i,a,n) for(int i=(a);i<=(n);++i) #define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i) #define erFOR(i,a,n) for(int i=(n);i>=(a);--i) #define SORT(a) sort(a.begin(),a.end()) #define rSORT(a) sort(a.rbegin(),a.rend()) #define fSORT(a,f) sort(a.begin(),a.end(),f) #define all(a) a.begin(),a.end() #define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x)) #define tp(a,i) get<i>(a) #ifdef _DEBUG #define line cout << "-----------------------------\n" #define stop system("pause") #define debug(x) print(x) #endif constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; } template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); } template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } template<class T>inline void print(const T& a) { cout << a << "\n"; } template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); } template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); } inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; } template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); } template<class T>inline void END(T s) { print(s); exit(0); } void END() { exit(0); } int main() { init(); int n; cin >> n; VI a(n), b(n), p(n); cin >> a >> b >> p; set<pair<int, int>> s; // {持てる重さ、index} FOR(i, 0, n) { --p[i]; if (a[i] <= b[p[i]] && p[i] != i)END(-1); if (i != p[i]) { s.emplace(a[i], i); } } vector<pair<int, int>> ans; while (!s.empty()) { int cur = s.rbegin()->second; int to = p[cur]; ans.emplace_back(cur, to); if (p[to] == cur) { s.erase(pair<int, int>(a[cur], cur)); s.erase(pair<int, int>(a[to], to)); } else { s.erase(pair<int, int>(a[to], to)); p[cur] = p[to]; } } print(ans.size()); for (auto [x, y] : ans)print(x + 1, y + 1); return 0; }
#include<bits/stdc++.h> using ll= long long; #define REP(i,n) for(ll i=0;i<ll(n);i++) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define ALL(x) x.begin(),x.end() #define INF (ll)1e9 //10^9:∞ #define LLINF (ll)1e12 #define MOD (ll)(1e9+7) //10^9+7:合同式の法 #define PI 3.141592653589 #define PB push_back #define F first #define S second #define __MAGIC__ ios::sync_with_stdio(false);cin.tie(nullptr); #define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;} #define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;} #define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;} #define Graph vector<vector<int>> #define PII pair<int,int> #define VI vector<int> #define VVI vector<vector<int>> #define VPII vector<pair<int,int>> #define DDD fixed<<setprecision(10) #define endl "\n" using namespace std; /*..................DEFINE GLOBAL VARIABLES...................*/ /*.....................DEFINE FUNCTIONS ......................*/ /*.........................kemkemG0...........................*/ signed main() { __MAGIC__ int A,B; cin>>A>>B; cout<<(2*A+100)-B<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0; i<(n); i++) #define rep2(i,x,n) for(int i=x; i<(n); i++) #define all(n) begin(n),end(n) typedef vector<int> vi; typedef vector<string> vs; const int mod = 1000000007; const int inf = 1061109567; int gcd(int a,int b){return b?gcd(b,a%b):a;} signed main() { int h, w; cin >> h >> w; vs s(h); rep(i,h) cin >> s[i]; set<pair<int, int> > cur; int ans = 0; for(int i=1; i<h-1; i++){ int j = 1; int a = 1, b = w-1; bool check = false; set<pair<int, int> > now; while(1){ if(s[i][j] == '#'){ if(check) b = j; else{ a = j; b = j; check = true; } } else{ if(check){ int plus = 4; for(auto u : cur){ if(u.first == a && u.second == b) plus = 0; else if(u.first == a || u.second == b) plus -= 2; } //cout << plus << endl; ans += plus; now.insert(make_pair(a, b)); check = false; } } j++; if(j == w) break; } //for(auto u : now) cout << u.first << ' ' << u.second << endl; cur = now; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define PI 3.141592653589 #define ll long long int #define ld long double #define vi vector<int> #define vl vector<ll> #define ii pair<int,int> #define pb push_back #define mp make_pair #define ff first #define ss second #define pll pair<ll,ll> #define vv vector #define all(v) (v).begin(),(v).end() #define MAXN 300005 int MOD=1e9+7; ll power(ll a, ll b){//a^b ll res=1; a=a%MOD; while(b>0){ if(b&1){res=(res*a)%MOD;b--;} a=(a*a)%MOD; b>>=1; } return res; } ll fermat_inv(ll y){return power(y,MOD-2);} ll gcd(ll a, ll b){return (b==0)?a:gcd(b,a%b);} ll min(ll a,ll b){return (a>b)?b:a;} ll max(ll a,ll b){return (a>b)?a:b;} bool prime[1000001]; vi primes; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); prime[0]=prime[1]=0; for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*p; i<=n; i += p) prime[i] = false; } } for(int p=2;p<1000001;p++) if(prime[p]) primes.pb(p); } ll fact[1000010]; ll finv[1000010]; void factorial(int n){ fact[0]=1; finv[0]=1; for(int i=1;i<=n;i++) fact[i]=fact[i-1]*i,fact[i]%=MOD,finv[i]=fermat_inv(fact[i]); } ll ncr(ll n,ll r) { if(n<r) return 0; else{ ll x=finv[r]*finv[n-r]%MOD; return fact[n]*x%MOD; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int te=1; //cin>>te; //SieveOfEratosthenes(1000000); //factorial(1000005); while(te--){ int h,w; cin>>h>>w; string a[h]; for(int i=0;i<h;i++) cin>>a[i]; int ans=0; for(int i=1;i<h-1;i++){ for(int j=1;j<w-1;j++){ if(a[i][j]=='.') continue; if(a[i][j-1]=='.'&&a[i-1][j]=='.') ans++; if(a[i][j+1]=='.'&&a[i-1][j]=='.') ans++; if(a[i][j-1]=='.'&&a[i+1][j]=='.') ans++; if(a[i][j+1]=='.'&&a[i+1][j]=='.') ans++; if(a[i][j-1]=='#'&&a[i-1][j]=='#'&&a[i-1][j-1]=='.') ans++; if(a[i][j+1]=='#'&&a[i-1][j]=='#'&&a[i-1][j+1]=='.') ans++; if(a[i][j-1]=='#'&&a[i+1][j]=='#'&&a[i+1][j-1]=='.') ans++; if(a[i][j+1]=='#'&&a[i+1][j]=='#'&&a[i+1][j+1]=='.') ans++; } } cout<<ans; } }
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define rep(i, a, n) for (int i=(a); i<(n); i++) #define per(i, a, n) for (int i=(a); i>(n); i--) typedef long long ll; const int maxn = 2e5+5; const int mod = 1e9+7; using namespace std; void solve() { string s, tmp; ll n; cin >> n >> tmp; ll k = (n+2)/3; if (tmp == "1") return cout << ll(1e10)*2 << '\n',void(0); rep(i,0,k) s+="110"; if (s.find(tmp) == string::npos) k++, s += "110"; if (s.find(tmp) == string::npos) return cout << "0\n", void(0); cout << ll(1e10)-k+1 << '\n'; } int main(int argc, char * argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef DEBUG freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin); //freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout); #endif //int t; cin >> t; while(t--) solve(); 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() { int a,b; cin>>a>>b; cout<<a/b; } int main() { //code fastio int t; t=1; while(t--) { solve(); cout<<endl; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7; #define mst(a,b) memset(a,b,sizeof a) #define PII pair<int,int> #define fi first #define se second #define pb push_back int t,n,s,k; void exgcd(ll a,ll b,ll &x,ll &y){ if(!b){ x=1,y=0; return; } exgcd(b,a%b,y,x); //这里仔细体会一下,这里我们将x1赋给y,y1赋给x y-=(a/b)*x; } int main(){ scanf("%d",&t); while(t--){ scanf("%d%d%d",&n,&s,&k); ll c=n-s; ll g=__gcd(k,n); if(c%g) puts("-1"); else { ll x,y; exgcd(k,n,x,y); ll tmp=c/g; ll d=n/g; x*=tmp; x=(x%d+d)%d; if(c%k==0) printf("%lld\n",c/k); else printf("%lld\n",x); } } return 0; }
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define ll long long #define ALL(a) (a).begin(),(a).end() #define rep(i,n) for(int i=0;i<(n);i++) #define rrep(i,n) for(int i=n-1;i>=0;i--) #define fi first #define se second #define pb push_back #define Pii pair<int,int> #define Pll pair<long long,long long> #define fout(num) cout << fixed << setprecision(20) << (num) << endl template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} //vector<vector<ll>> dp(n,vector<ll>(n)) //2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d)); //3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d))); using namespace std; long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } long long gcd(long long a,long long b){ if(b==0) return a; return gcd(b,a%b); } long long lcm(long long a,long long b){ return a/gcd(a,b)*b; } signed main(){ std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); int test; cin >> test; while(test--){ ll n,s,k; cin >> n >> s >> k; if(s%gcd(n,k)!=0){ cout << -1 << endl; continue; } ll ggg = gcd(gcd(n,k),s); n/=ggg; k/=ggg; s/=ggg; ll gc = gcd(n,k); n/=gc; k/=gc; ll x,y; extGCD(n,k,x,y); //cout << x << " " << y << endl;continue; x *= s; y *= s; y *= gc; ll g = n/gc; y = -y; if(y>0){ ll kk = y/g; y -= kk*g; }else if(y<0){ ll kk = (-y)/g; kk++; y += kk*g; } cout << y << "\n"; } return 0; } // g++ main.cpp -o a.out && ./a.out
#include <iostream> // cout, endl, cin #include <string> // string, to_string, stoi #include <vector> // vector #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <utility> // pair, make_pair #include <tuple> // tuple, make_tuple #include <math.h> // sqrt #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <iomanip> // setprecision #include <bitset> // bitset #include <functional> // greater,int>() using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vb = vector<bool>; using vll = vector<long long>; using pii = pair<int, int>; using psi = pair<string, int>; #define rep(i, n) for(ll i = 0; i < (ll) n; i++) #define FOR(i, a, b) for(ll i = a; i < (ll) b; i++) #define ALL(x) x.begin(),x.end() #define PB push_back #define MP make_pair #define F first #define S second #define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;} #define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;} #define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;} #define COUT(ans) cout<<ans<<endl; #define COUTD(ans) cout<<fixed<<setprecision(10)<<ans<<endl; int i; bool check; ll f (map<char,int> &m, string s, vll &a) { ll ans = 0; for (char c : s) { ll num = 0; if (m.count(c)) { num = m.at(c); } else { num = a[i]; m[c] = a[i]; i++; } ans *= 10; ans += num; if (ans == 0) { check = true; return 0; } } return ans; } int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; vi data(26, 0); for(char c :s1) { int a = c - 'a'; data[a]++; } for (char c : s2) { int a = c - 'a'; data[a]++; } for (char c : s3) { int a = c - 'a'; data[a]++; } int count = 0; rep(i, 25) { if (data[i] > 0) { count++; } } if (count > 10) { COUT("UNSOLVABLE") return 0; } vll a = {1,0,2,3,4,5,6,7,8,9}; bool finish = false; do { i = 0; check = false; map<char, int> m; ll n1 = f(m, s1, a); ll n2 = f(m, s2, a); ll n3 = f(m, s3, a); if (check) { continue; } if (n1 + n2 == n3) { COUT(n1) COUT(n2) COUT(n3) finish = true; break; } } while(next_permutation(a.begin(), a.end())); if (!finish) { COUT("UNSOLVABLE") } }
#include <bits/stdc++.h> using namespace std; #define II pair<int, int> #define III pair<II, int> #define X first.first #define Y first.second #define Z second #define all(a) (a).begin(), (a).end() #define for0(i, n) for (int i = 0; i < n; i++) #define for1(i, n) for (int i = 1; i <= n; i++) int const oo = 1000000007, e5 = 100007, e6 = 1000007; int N, M; vector<int> edges[2005]; bool visited[2005]; int res = 0; int __init__() { return 0; } void print01(int val, int showNums = 20) { char answer[showNums]; for0(i, showNums) { answer[showNums - 1 - i] = val % 2 + '0'; val /= 2; } cout << answer; } void DFS(int node) { if (visited[node]) return; ++res; visited[node] = 1; for0(i, edges[node].size()) { DFS(edges[node][i]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0);__init__(); // freopen("test.txt", "r", stdin); int a, b; cin >> N >> M; for0(i, M) { cin >> a >> b; edges[a].push_back(b); } for1(i, N) { for1(i, N) visited[i] = 0; DFS(i); } cout << res; }
#include<bits/stdc++.h> #define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #define mod 1000000007 #define INF 10000000000000001 #define F first #define S second #define LB lower_bound #define UB upper_bound #define vc vector #define vll vector<long long> #define pll pair<long long,long long> #define pb push_back #define all(v) v.begin(),v.end() #define T ll test;cin>>test; while(test--) #define forn(i,a,n) for(ll i=a;i<(long long)n;++i) #define backn(i,n,a) for(ll i=n;i>(long long)a;--i) #define MAX 200005 #define sz(x) (ll)x.size() #define ins insert #define mp make_pair #define lld long long int #define cy cout<<"Yes\n"; #define cno cout<<"No\n"; typedef long long ll; typedef long double ld; using namespace std; signed main() { FASTIO int a,b,c; cin >> a >> b >> c; int x=2*a; int y=2*b; int z=2*c; if(x==b+c) { cout << "Yes"<< endl; return 0; } else if(y==a+c) { cout << "Yes"<< endl; return 0; } else if(z==a+b) { cout << "Yes"<< endl; return 0; } else { cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; constexpr ll MOD = 1e9 + 7; //constexpr ll MOD = 998244353; //constexpr ll MOD = ; ll mod(ll A, ll M) {return (A % M + M) % M;} constexpr ll INF = 1LL << 60; 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;} ll divceil(ll A, ll B) {return (A + (B - 1)) / B;} #define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false) int main() { ll N = 3; vector<ll> A(N); for (ll i = 0; i < N; i++) { cin >> A.at(i); } sort(A.begin(), A.end()); cout << ((A.at(2) - A.at(1) == A.at(1) - A.at(0)) ? "Yes" : "No") << endl; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for(int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; // using namespace atcoder; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("->");}else{puts("<-");} #define MAX_N 205 struct edge{ int to, id, dir; }; vector<edge> G[MAX_N]; int ans[100000]; int c[1000]; void dfs(int x){ rep(i,G[x].size()){ edge e = G[x][i]; int y = e.to; int id = e.id; int dir = e.dir; if(ans[id] != 0) continue; if(c[y] < c[x]){ ans[id] = dir; continue; } ans[id] = dir; dfs(y); } } int main() { int n, m; cin >> n >> m; vector<int> a, b; rep(i,m){ int aa, bb; cin >> aa >> bb; aa--; bb--; edge e; e.to = bb; e.id = i; e.dir = 1; G[aa].push_back(e); e.to = aa; e.dir = 2; G[bb].push_back(e); a.push_back(aa); b.push_back(bb); } rep(i,n) cin >> c[i]; if(m == 0){ return 0; } vector<P> v; rep(i,n){ v.push_back(P(c[i],i)); } sort(v.begin(), v.end(), greater<P>()); rep(i,v.size()){ int x = v[i].second; dfs(x); } rep(i,m){ if(ans[i] == 1) yn; } return 0; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void ran() { srand(chrono::steady_clock::now().time_since_epoch().count()); } long long get_rand() { long long a = rand(); long long b = rand(); return a * (RAND_MAX + 1ll) + b; } void usaco() { freopen("problem.in", "r", stdin); freopen("problem.out", "w", stdout); } template<typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>; // #define endl '\n' // #define double long double // #define int long long // const int MOD = 1000 * 1000 * 1000 + 7; // const int MOD = 998244353; int n, m; const int MXN = 105, MXM = 10005; vector<pair<int, int>> ed; int reach[MXN]; int dir[MXM]; vector<pair<int, int>> adj[MXN], nadj[MXN]; vector<int> act; int state[MXN]; map<pair<int, int>, int> enm; bool stop = false; void dfs(int node, int par) { if (stop) return; if (state[node] == 1) { // on stack bool consid = false; vector<int> actr; for (int i=0; i<act.size(); i++) { if (act[i] == node) { consid = !consid; } if (consid) { actr.push_back(act[i]); } } // cout << "cyc" << endl; for (int i=0; i<actr.size(); i++) { // cout << actr[i] << ' '; int a = actr[i], b = actr[(i+1)%actr.size()]; int x = enm[{a, b}]; if (ed[x].first == a) { dir[x] = 1; } else { dir[x] = -1; } } stop = true; // cout << endl; return; } state[node] = 1; act.push_back(node); for (auto x: adj[node]) { if (stop) return; int ot = x.first; if (ot == par) continue; if (dir[x.second] != 0) continue; dfs(ot, node); } state[node] = 0; act.pop_back(); } void cycle(int x) { while (true) { for (int i=0; i<MXN; i++) state[i] = 0; act.clear(); stop = false; dfs(x, -1); if (!stop) return; } } void dfsn(int node, int par) { for (auto x: nadj[node]) { int ot = x.first; if (ot == par) continue; int num = x.second; if (ed[num].first == node) { dir[num] = 1; } else { dir[num] = -1; } dfsn(ot, node); } } signed main() { ran(); fast(); cin >> n >> m; for (int i=0; i<m; i++) { int a, b; cin >> a >> b; ed.push_back({a, b}); enm[ed.back()] = i; enm[{b, a}] = i; } for (int i=1; i<=n; i++) { cin >> reach[i]; } for (int i=0; i<m; i++) { int a = ed[i].first, b = ed[i].second; if (reach[a] == reach[b]) { adj[a].push_back({b, i}); adj[b].push_back({a, i}); } else { if (reach[a] > reach[b]) dir[i] = 1; else dir[i] = -1; } } for (int i=1; i<=n; i++) { if (adj[i].size()) { cycle(i); } } for (int i=1; i<=n; i++) { for (auto x: adj[i]) { if (dir[x.second] == 0) { nadj[i].push_back(x); // cout << i << ' ' << x.first << ' ' << x.second << endl; } } } for (int i=1; i<=n; i++) { if (nadj[i].size() == 1) { dfsn(i, -1); } } for (int i=0; i<m; i++) { if (dir[i] == 1) cout << "->" << endl; else if (dir[i] == -1) cout << "<-" << endl; else assert(false); } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define deb(x) cerr << #x << ":" << x << "\n" #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define int ll void solve() { ll n; cin>>n; int p=1,ans=INT64_MAX; for(int b=0;b<64 && p<=n;b++){ int c=n%p; int a=n/p; ans=min(ans,a+b+c); p*=2; } cout<<ans<<"\n"; } int32_t main() { #ifndef ONLINE_JUDGE freopen("../text_files/input.txt", "r", stdin); freopen("../text_files/output.txt", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n%100==0) { cout<<n/100; return 0; } cout<<n/100+1; 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 = 200002; int main() { ll n, w; vl table(MAX + 1, 0); cin >> n >> w; rep1(i, n) { ll s, t, p; cin >> s >> t >> p; table[s] += p; table[t] -= p; } rep1(i, MAX) { table[i] += table[i - 1]; if (table[i] > w || table[i - 1] > w) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
#include<bits/stdc++.h> #include<vector> #include<string> #include<algorithm> #include<cmath> #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long int #define mod 1000000007 #define inf 1e18+42 #define endl "\n" #define pi 3.1415926535897932384626433832795028841971693993751058 #define maxn 100005 #define out1(a) cout<<#a<<" "<<a<<endl #define out2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl #define out3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl #define rep(i,a,b) for(int i=a;i<b;i++) #define repr(i,a,b) for(int i=a;i>=b;i--) #define fori(it,A) for(auto it=A.begin();it!=A.end();it++) #define ft first #define sd second #define pb push_back #define mp make_pair #define pq priority_queue #define all(x) (x).begin(),(x).end() #define zero(x) memset(x,0,sizeof(x)); #define ceil(a,b) (a+b-1)/b using namespace std; int binpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } //START OF CODE ->->->->->->-> void solve() { int n; cin>>n; string s; cin>>s; int q; cin>>q; int cnt = 0; rep(i,0,q){ int ch; cin>>ch; int a,b; cin>>a>>b; a--; b--; if ( ch == 1 ){ if ( cnt & 1 ){ a = (a<n)?a+n:a-n; b = (b<n)?b+n:b-n; swap(s[a],s[b]); } else{ swap(s[a],s[b]); } } else{ cnt++; } } if ( cnt & 1 ){ string ans = ""; rep(i,n,s.length()){ ans += s[i]; } rep(i,0,n){ ans += s[i]; } cout<<ans<<endl; } else{ cout<<s<<endl; } } //END OF CODE ->->->->->->->-> signed main() { fast; int t = 1; // cin>>t; for ( int i = 1 ; i <= t ; i++ ){ // cout<<"Case #"<<i<<": "; solve(); } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; #define pr pair<int,int> #define vect vector <int> v1 #define pb push_back #define sortv sort(v1.begin(),v1.end()); #define loop(i,a,b) for(int i=(a);i<(b);i++) #define inputarr loop(i,0,n) cin>>ar[i]; const int mod=1e9+7; int calpowof2(int a, int b) { if(b==0) return 1; if(b%2==0) { int ans=calpowof2(a,b/2); return (ans%mod*ans%mod)%mod; } int ans=calpowof2(a,b-1); return (a%mod*ans%mod)%mod; } void solve() { int i,j,n,t,m,k,ct=0,sum=0,flag=0; cin>>n; set<int> s; s.insert(0); loop(i,0,n) { cin>>j; s.insert(j); } sum=1; for(auto it=s.begin();it!=s.end();it++) { ct++; if(ct==s.size()) { break; } auto it1=next(it,1); sum=sum*((*it1-*it+1)%mod)%mod; } cout<<sum; } signed main() { //freopen("lineup.in","r",stdin); //freopen("lineup.out","w",stdout); ios::sync_with_stdio(false); cin.tie(NULL); // WATCH FOR SAME VARIABLE USAGE CLEARLY!!! // AFTER WA VERDICT CHECK FIRSTLY YOUR WHOLE CODE AND THEN MOVE ON TO LOGIC ERROR //MAKE SURE TO MAKE A DUPLICATE OF ANYTHING IF PERFORMING OPERATION DIRECTLY ON SOMETHING !!! int t=1; //cin>>t; while(t--) { solve(); } cerr<< endl << clock() * 1000.0 / CLOCKS_PER_SEC <<'m'<<'s'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; ll mod=1e9+7; int main(){ int N; cin>>N; vector<ll> A(N); for(int i=0;i<N;i++) cin>>A[i]; sort(A.begin(),A.end()); ll ans=A[0]+1; map<ll,bool> seen; seen[A[0]]=true; for(int i=1;i<N;i++){ if(seen[A[i]]) continue; ans*=(A[i]-A[i-1]+1); ans%=mod; seen[A[i]]=true; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i < (int)(n); i++) #define all(x) (x).begin(),(x).end() typedef long long ll; #define F first #define S second #define YES(n)cout << ((n) ? "YES" : "NO" ) << endl #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define _GLIBCXX_DEBUG const ll INF = 1e12; int main(){ int a,b,c; cin>>a>>b>>c; if(a==b)cout<<c<<endl; else if(a==c)cout<<b<<endl; else if(b==c)cout<<a<<endl; else cout<<0<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double lf; #define rep(i, f, t) for (int i = (f), ed##i = (t); i <= ed##i; ++i) #define re(i, t) rep (i, 1, t) #define per(i, t, f) for (int i = (t), ed##i = (f); i >= ed##i; --i) #define ste(i, f, t, s) for (int i = (f), ed##i = (t); i <= ed##i; i += s) #define each(i, x) for (auto &i : (x)) #define nx(i, f) for (int i = h[f]; i; i = e[i].n) #define nxt(i, f, g) for (int i = g.h[f]; i; i = g.e[i].n) #define dbg(x) (cerr << "(dbg) " << #x " = " << (x) << '\n') #define fio(x) (freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)) #define y1 y1__ #define umod(x) ((x) >= mo && ((x) -= mo)) #define outl(x) (cout << (x) << '\n') #define outs(x) (cout << (x) << ' ') #define errl(x) (cerr << (x) << '\n') #define errs(x) (cerr << (x) << ' ') // clang-format off template<class T>T jdz(const T&a){return a>0?a:-a;} template<class T,class E>void up(T&a,const E&b){a<b&&(a=b);} template<class T,class E>void down(T&a,const E&b){b<a&&(a=b);} template<class T,size_t E>void fill(T(&a)[E],const int&x){memset(a,x,E*sizeof(*a));} ll pow(ll a,ll b,const ll&m){ll res=1;a%=m;while(b>0){if(b&1)res=res*a%m;a=a*a%m,b>>=1;}return res;} ll gcd(ll a,ll b){ll t;while(b!=0)t=a%b,a=b,b=t;return a;} ll C(ll n,ll m){if(m>n)return 0;ll a=1;re(i,m)a*=(n-i+1),a/=i;return a;} ll C(ll n,ll m,ll p){if(m>n)return 0;ll x=1;re(i,m){ll a=(n+i-m)%p,b=i%p;x=x*(a*pow(b,p-2,p)%p)%p;}return x;} // clang-format on signed main() { int a[10]; re(i,3)cin>>a[i]; sort(a+1,a+3+1); if(a[1]==a[2])cout<<a[3]; else if(a[2]==a[3])cout<<a[1]; else cout<<0; return 0; }
#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; using P = pair<int,int>; struct UnionFind { vector<int> par; vector<map<int, int>> mp; UnionFind(int n=0): par(n,-1), mp(n) {} int find(int x) { if(par[x] < 0) return x; return par[x] = find(par[x]); } bool unite(int x, int y){ x = find(x); y = find(y); if(x==y) return false; if(par[x] > par[y]) swap(x,y); // x > y for(auto p : mp[y]) { mp[x][p.first] += p.second; } mp[y] = map<int,int>(); par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) {return find(x) == find(y);} int size(int x) {return -par[find(x)];} }; int main() { int n ,q; cin >> n >> q; UnionFind t(n); rep(i,n) { int c; cin >> c; t.mp[i][c] = 1; } rep(i,q){ int type, a,b,x, y; cin >> type ; if(type==1){ cin >> a >> b; --a; --b; t.unite(a,b); }else{ cin >> x >> y; --x; x = t.find(x); int ans = t.mp[x][y]; cout << ans << endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; struct UnionFind{ vector<int> r; vector<unordered_map<int,int>> mp; UnionFind(int n,vector<int> c){ r=vector<int>(n,-1); mp.resize(n); for(int i=0; i<n; ++i) mp[i][c[i]]=1; } int root(int x){ return (r[x]<0? x:r[x]=root(r[x])); } void unite(int x,int y){ x=root(x); y=root(y); if(x==y) return; if(r[x]>r[y]) swap(x,y); r[x]+=r[y]; for(auto [key,i]:mp[y]) mp[x][key]+=i; r[y]=x; return; } int size(int key,int y){ return mp[root(key)][y]; } }; int main(void){ int n,q; cin>>n>>q; vector<int> c(n); for(auto& i:c){ cin>>i; --i;} UnionFind UF(n,c); while(q--){ int a,b,c; cin>>a>>b>>c; --b,--c; if(a==1) UF.unite(b,c); else cout<<UF.size(b,c)<<endl; } 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 fin(a) { cout << a << endl; return 0; } #define debug(a) { cerr << #a << ": " << a << endl; } #define endl '\n' #define fi first #define se second using lint = long long; using ld = long double; using P = pair<int,int>; using Pl = pair<lint,lint>; 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; } template<class T> void YesorNo(T x) { printf(x ? "Yes\n" : "No\n"); } 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; } /*----------------------------------------------------------------------------------------------------*/ struct UnionFind { // UnionFindTree vector<int> d; int group; UnionFind(int n): d(n,-1), group(n) {} int find(int x) { if(d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if(x == y) return false; if(-d[x] < -d[y]) swap(x,y); group--; d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return -d[find(x)]; } }; long long Modpow(long long a, int n, long long mod){ if(n == 0) return 1; if(n%2 == 0){ long long res = Modpow(a, n/2, mod); return res * res % mod; } return a * Modpow(a, n-1, mod) % mod; } int main(){ int n; cin >> n; UnionFind uf(n); rep(i,n) { int a; cin >> a; a--; uf.unite(i,a); } cout << Modpow(2,uf.group,998244353)-1 << endl; }
#include<bits/stdc++.h> #define re register int #define ll long long #define dl long double #define LL ll #define V inline void #define I inline int #define FOR(i,a,b) for(re i=(a),i##i=(b);i<=i##i;++i) #define ROF(i,a,b) for(re i=(a),i##i=(b);i>=i##i;--i) #define gc getchar() //#define gc (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++ using namespace std; char *fs,*ft,buf[1<<18]; const int N=3e5+10,mo=998244353,G=3; LL read(){ ll p=0; bool w=0; char ch=gc; while(!isdigit(ch)) w=ch=='-',ch=gc; while(isdigit(ch)) p=p*10+ch-'0',ch=gc; return w?-p:p; } int n,f[N],vis[N],as; ll ans; LL Pow(ll x,ll y){ ll as=1; for(;y;y>>=1,x=x*x%mo) if(y&1) as=as*x%mo; return as;} V dfs(int u){ if(vis[u]) return ; vis[u]=1; dfs(f[u]); } int low[N],dfn[N],now,cnt,id,s[N],bk[N],scc[N],siz[N]; V tarjan(int u){ s[++cnt]=u; low[u]=dfn[u]=++now; bk[u]=1; int v=f[u]; if(!low[v]) tarjan(v),low[u]=min(low[u],low[v]); else if(bk[v]) low[u]=min(low[u],dfn[v]); if(low[u]==dfn[u]){ scc[u]=++id,siz[id]=1; bk[u]=0; while(s[cnt]!=u){ v=s[cnt]; scc[v]=id,++siz[id]; bk[v]=0; --cnt; }--cnt; } } V sol(){ FOR(i,1,n) if(!dfn[i]) tarjan(i); FOR(i,1,id) if(siz[i]>1) ++as; FOR(i,1,n) if(f[i]==i) ++as; ans=Pow(2,as)-1; cout<<ans; return ; } int main(){ n=read(); FOR(i,1,n) f[i]=read(); sol(); return 0; }
#include <iostream> using namespace std; int main(){ int N; cin >> N; cout << N-1; }
#include<bits/stdc++.h> using namespace std; using P=pair<int,int>; using ll=long long; template<class T> using heapq = priority_queue<T,vector<T>,greater<T>>; template<class T> bool chmax(T &a,const T b) {if (a<b) {a=b;return true;} else return false;} template<class T> bool chmin(T &a,const T b) {if (a>b) {a=b;return true;} else return false;} #define rep(i,n) for(int i=0; i<(n); i++) #define srep(i,a,b) for(int i=(a); i<(b); i++) #define rrep(i,n) for(int i=(n)-1;i>=0; i--) #define srrep(i,a,b) for(int i=(b)-1; i>=(a); i--) int main(){ int n; cin >> n; cout << n-1 << endl; }
#pragma GCC optimize ("Ofast,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") #include <bits/stdc++.h> using namespace std; constexpr int M = 998244353; #define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define int long long #define pii pair<int, int> #define fi first #define se second #define SZ(x) ((int)(x.size())) #ifdef int #define INF 0x3f3f3f3f3f3f3f3f #define INF2 (int)(0xcfcfcfcfcfcfcfcf) #else #define INF 0x3f3f3f3f #define INF2 0xcfcfcfcf #endif signed main() { fastio int n; cin >> n; map<int, int> cnt; int sum = 0; cnt[0]++; int ans = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; if (i & 1) sum -= x; else sum += x; if (cnt.count(sum)) ans += cnt[sum]; cnt[sum]++; } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #ifdef ENABLE_DEBUG #define dump(a) cerr<<#a<<"="<<(a)<<endl #define dumparr(a,n) cerr<<#a<<"["<<(n)<<"]="<<(a[n])<<endl #else #define dump(a) #define dumparr(a,n) #endif #define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++) #define For(i, a) FOR(i, 0, a) #define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--) #define Rev(i, a) REV(i, 0, a) #define REP(a) For(i, a) #define SIGN(a) (a==0?0:(a>0?1:-1)) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair<ll, ll> pll; typedef pair<ll,pll> ppll; typedef vector<ll> vll; typedef long double ld; typedef pair<ld,ld> pdd; pll operator+(pll a,pll b){ return pll(a.first+b.first,a.second+b.second); } pll operator-(pll a,pll b){ return pll(a.first-b.first,a.second-b.second); } pll operator*(ll a,pll b){ return pll(b.first*a,b.second*a); } const ll INF=(1LL<<60); #if __cplusplus<201700L ll gcd(ll a, ll b) { a=abs(a); b=abs(b); if(a==0)return b; if(b==0)return a; if(a < b) return gcd(b, a); ll r; while ((r=a%b)) { a = b; b = r; } return b; } #endif template<class T> bool chmax(T& a,const T& b){ if(a<b){ a=b; return true; } return false; } template<class T> bool chmin(T& a,const T& b){ if(a>b){ a=b; return true; } return false; } template<class S,class T> std::ostream& operator<<(std::ostream& os,pair<S,T> a){ os << "(" << a.first << "," << a.second << ")"; return os; } template<class T> std::ostream& operator<<(std::ostream& os,vector<T> a){ os << "[ "; REP(a.size()){ os<< a[i] << " "; } os<< "]"; return os; } template<class T> std::ostream& operator<<(std::ostream& os,set<T> a){ os << "{ "; for(auto itr=a.begin();itr!=a.end();++itr){ os<< *itr << " "; } os<< "}"; return os; } template<class T> void ans_array(T begin,T end){ auto itr=begin; cout<<*itr; ++itr; for(;itr!=end;++itr){ cout<<' '<<*itr; } cout<<endl; } template<class T> void ans_array_newline(T begin,T end){ for(auto itr=begin;itr!=end;++itr){ cout<<*itr<<endl; } } void solve(long long N, std::vector<long long> A){ REP(N){ if(i%2){ A[i]=-A[i]; } } vector<ll> sumA(N+1); REP(N){ sumA[i+1]=sumA[i]+A[i]; } map<ll,ll> mp; REP(N+1){ mp[sumA[i]]++; } ll ans=0; for (auto &&i : mp) { ans+=i.second*(i.second-1)/2; } cout<<ans<<endl; } int main(){ cout<<setprecision(1000); long long N; scanf("%lld", &N); std::vector<long long> A(N); for(int i = 0 ; i < N ; i++){ scanf("%lld", &A[i]); } solve(N, std::move(A)); return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, from, to) for (ll i = from; i < (to); ++i) #define mp(x,y) make_pair(x,y) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define pb push_back using ll = long long; using ld=long double; using vin=vector<int>; using vvin=vector<vin>; using vll=vector<ll>; using vvll=vector<vll>; using vst=vector<string>; using P = pair<ll,ll>; using vp=vector<P>; using vvp=vector<vp>; const int inf=1e9+7; const ll INF=9e18/2; const long double PI = acos(-1.0); template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;} template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;} template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; vvin g; int cnt[1000000]; int ans=0; int N=660000; void dfs2(int); void dfs(int); void dfs2 (int now){ for(auto v:g[now])cnt[v]--; for(auto v:g[now])if(cnt[v]==1)dfs(v); } void dfs(int now){ for(auto v:g[now]){ if(cnt[v])continue; cnt[v]=1; ans++; dfs2(v); } } int main(){//cout<<fixed<<setprecision(20); int n; cin>>n; g.resize(N); rep(i,0,n){ int a,b; cin>>a>>b; g[i].pb(a+n); g[i].pb(b+n); g[a+n].pb(i); g[b+n].pb(i); cnt[a+n]++; cnt[b+n]++; } rep(i,n+1,N)if(cnt[i]==1)dfs(i); rep(i,n+1,N)if(cnt[i])ans++; cout<<ans<<endl; }
#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<ctime> #include<map> #include<vector> #include<math.h> #include<stdio.h> #include<stack> #include<queue> #include<tuple> #include<cassert> #include<set> #include<bitset> #include<functional> #include <fstream> //#include<bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i, x) for(ll i = 0; i < x; i++) #define rep2(i, x) for(ll i = 1; i <= x; i++) #define all(a) (a).begin(),(a).end() using ll = long long; using ld = long double; using namespace std; const ll INF = 10000000000000000; const int intINF = 1000000000; const ll mod = 1000000007; const ll MOD = 998244353; const ld pi = 3.141592653589793238; bool isprime(int p) { if (p == 1) return false; for (int i = 2; i < p; i++) { if (p % i == 0) return false; } return true; } ll gcd(ll a, ll b) { if (a < b)swap(a, b); if (a % b == 0)return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll keta(ll n) { ll res = 0; while (n >= 1) { res += n % 10; n /= 10; } return res; } ll modpow(ll x, ll y) { ll res = 1; while (y) { if (y % 2) { res *= x; res %= mod; } x = x * x % mod; y /= 2; } return res; } ll nCk(ll n, ll k) { ll a = 1, b = 1; for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; } for (int h = 1; h <= k; h++) { b *= h; b %= mod; } return a * modpow(b, mod - 2) % mod; } //printf("%.10f\n", n); typedef pair <ll, ll> P; ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; struct edge { ll to, cost; }; struct status { ll y; ll high; ll cost; bool operator<(const status& rhs) const { return cost < rhs.cost; }; bool operator>(const status& rhs) const { return cost > rhs.cost; }; }; signed main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); //cout << fixed << setprecision(15); //input ll n; cin >> n; vector<ll> a(n), b(n), maa(n); rep(i, n) { cin >> a[i]; } rep(i, n) { cin >> b[i]; } rep(i, n) { if (i == 0) { maa[i] = a[i]; continue; } else { maa[i] = max(maa[i - 1], a[i]); } } ll ma = 0; rep(i, n) { ma = max(ma, maa[i] * b[i]); cout << ma << 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 ((1<<30)-1) //#define int long long const int P=3; // 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=2e5+5; int a[MN],n,dep[MN],rt,vrt,f[MN],ban[MN],X,Y; vi e[MN]; void dfs1(int u,int fa) { dep[u]=dep[fa]+1,f[u]=fa; if(dep[u]>vrt) vrt=dep[u],rt=u; for(auto v:e[u]) { if(v==fa) continue; dfs1(v,u); } } int now=1,ans[MN]; void dfs2(int u,int fa) { int bv=0; ans[u]=now; // cerr<<u<<endl; for(auto v:e[u]) { if(v!=fa&&ban[v]) bv=v; if(v==fa||ban[v]) continue; now++; dfs2(v,u); now++; } if(bv) now++,dfs2(bv,u),now++; } signed main() { #ifndef ONLINE_JUDGE freopen("pro.in","r",stdin); freopen("pro.out","w",stdout); #endif n=read(); For(i,1,n-1) { int u=read(),v=read(); e[u].pb(v),e[v].pb(u); } dfs1(1,0); X=rt; rt=0,vrt=0; dfs1(X,0); Y=rt; // cerr<<X<<' '<<Y<<endl; int t=Y; do { ban[t]=1; t=f[t]; } while(t); dfs2(X,0); For(i,1,n) { printf("%d ",ans[i]); } printf("\n"); return FastIO::Fflush(),0; } /* */
#include <iostream> #include <stack> #include <queue> #include <map> #include <algorithm> #include <tuple> using namespace std; void yes(){ cout << "yes" << endl; } void Yes(){ cout << "Yes" << endl; } void YES(){ cout << "YES" << endl; } void no(){ cout << "no" << endl; } void No(){ cout << "No" << endl; } void NO(){ cout << "NO" << endl; } #define rep(i, n)for(int i=0; i<n; i++) #define ll long long ll inf = 1000000007; ll INF = 1000000000000000007; int main(){ ll N; string S; cin >> N >> S; // // fofoxxfofox stack<char> s; rep(i, N){ // xでなければスタックに突っ込む if(S[i] != 'x'){ s.push(S[i]); } // xならばfoxを取り除けるかも else{ if(s.empty()){ s.push('x'); } // oxを発見 else if(s.top() == 'o'){ s.pop(); // foxを発見 if(s.top() == 'f'){ s.pop(); } // さっきpopしたoは間違いだった // xもpush else{ s.push('o'); s.push('x'); } } // oxを含まないためxをpush else{ s.push('x'); } } } ll num = s.size(); /* while(!s.empty()){ cout << s.top(); s.pop(); } cout<<endl;*/ // 解答 cout << num << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef double dd; #define tst int tttt;cin>>tttt;for(int TTTT=1;TTTT<=tttt;TTTT++) #define nl cout<<"\n"; #define forn(a,b) for(int ii=a;ii<b;ii++) const ll MOD1=1e9+7; const ll MOD2=998244353; #define mp(a,b) make_pair(a,b) #define pb(a) push_back(a) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define hhh cout<<"here"<<endl; ll mod=MOD1; ll po(ll x,ll y) {ll res=1;while(y>0){if(y&1)res=(res*x);x=(x*x);y>>=1;}return res;} ll gcd(int a, int b){if(a<b) swap(a,b);if(b==0) return a;return gcd(a%b,b);} int N=2e5+5; void solve(){ int n,w;cin>>n>>w; vector<ll> a(N); for(int i=0;i<n;i++){ ll s,t,p;cin>>s>>t>>p; a[s]+=p;a[t]-=p; }ll s=0; for(int i=0;i<N;i++){ s+=a[i]; // cout<<s<<endl; if(s>w){ cout<<"No";return; } }cout<<"Yes"; } int main(){ // fastio // ++*(int*)0;// crash // freopen("out.txt","r",stdin);freopen("outt.txt","w",stdout); // preprocess(); // tst { solve();nl; } } /* u & (v-u//2) */
//#include <tourist> #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; const int INF = 1e9; const double eps = 1e-7; const ll LINF = ll(1e18); const int MOD = 1000000007; const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const long double pi = 4 * atan(1); #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) \ { \ cout << x << ' '; \ } \ cout << endl; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //cout<<fixed<<setprecision(15);有効数字15桁 //-std=c++14 //g++ yarudake.cpp -std=c++17 -I . ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n,w; cin>>n>>w; vector<ll> sum(2*100000+2,0); ll s,t,p; rep(i,n){ cin>>s>>t>>p; sum[s]+=p; sum[t]-=p; } rep(i,200001){ sum[i+1]+=sum[i]; } rep(i, 200000+2) { if(sum[i]>w){ return no,0; } } yes; }
// #include<bits/stdc++.h> using namespace std; #define PB push_back #define f first #define s second #define what_is(x) cerr << #x << " is " << x << endl; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const int INF = 1000000007; const ll MOD = 1000000007; void solve_test() { ll n; string t; cin >> n >> t; string s; char arr[] = {'1', '1', '0'}; for(int i=0; i<=200005; i++) { s += arr[i%3]; } const ll tot = 3*pow(10, 10); ll ans = 0; for(int i=0; i<3; i++) { if(s.substr(i, n) == t) { ans += 1 + (tot-i-n)/3; } } cout << ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int tests; tests = 1; //cin >> tests; while(tests--) { solve_test(); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(void){ int N; string S; vector<string> V; ll ans, cnt; bool flg; cin >> N; cin >> S; ans = 10000000000; cnt = 0; flg = false; for (int i = 0; i < N; i++){ if (S[i] == '0'){ if (i >= 1 && S[i - 1] != '1'){ flg = true; break; } else if (i >= 2 && S[i - 2] != '1'){ flg = true; break; } cnt++; } if (N >= 3){ if (S.substr(i, 3) == "111"){ flg = true; break; } } } if (!flg){ if (N == 1 && S[0] == '1'){ ans *= 2; } else if (N == 2 && S[0] == '1' && S[1] == '1'){ ans *= 1; } else{ if (S[N - 1] == '0') ans = ans - cnt + 1; else ans = ans - cnt; } } else ans = 0; cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <string> #include <cmath> #include <cstdio> #include <iomanip> #include <fstream> #include <cassert> #include <cstring> #include <unordered_set> #include <unordered_map> #include <numeric> #include <ctime> #include <bitset> #include <complex> #include <chrono> #include <random> #include <functional> using namespace std; #define int long long signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (auto &t : a) { cin >> t; } int c = 0; int cmax = 0; int s = 0; for (int i = 0; i < n; i++) { c += s; s += a[i]; cmax = max(cmax, a[i]); cout << (i + 1) * cmax + c + s << '\n'; } }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; #define pb push_back #define bp pop_back #define int long long int #define vt vector #define pii pair<int,int> #define pq priority_queue #define ff first #define ss second #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define lc lexicographical_compare // #define endl '\n' int inf = 1e18; int mod = 1e9 + 7; const int N = 2e5 + 5; int spf[N]; void sieve() { for (int i = 1; i < N; i++) spf[i] = i; for (int i = 2; i * i < N; ++i) { if (spf[i] == i) { for (int j = i * i; j < N; j += i) { if (spf[j] == j) spf[j] = i; } } } } int mpower(int x, unsigned int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } int gcdExtended(int a, int b, int *x, int *y); int modInverse(int b, int m) { int x, y; int g = gcdExtended(b, m, &x, &y); if (g != 1) return -1; return (x % m + m) % m; } int gcdExtended(int a, int b, int *x, int *y) { if (a == 0) { *x = 0, *y = 1; return b; } int x1, y1; int gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } void solve(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // sieve(); // int T; // cin >> T; // while (T--) solve(); cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; return 0; } /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// void solve() { int n; cin >> n; vt<int> a(n); map<int, int> m; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]] += 1; } vt<int> v; for (auto i : m) v.pb(i.ss); int sum = n, ans = 0; for (int i = 0; i < v.size(); i++) { // cout << v[i] << endl; sum -= v[i]; ans += sum * v[i]; } cout << ans << endl; }
#include <cstdio> #include <cctype> #define rr register using namespace std; const int N=100011; typedef long double ld; int a[N],n; inline signed iut(){ rr int ans=0; rr char c=getchar(); while (!isdigit(c)) c=getchar(); while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar(); return ans; } inline ld max(ld a,ld b){return a>b?a:b;} inline ld calc(ld mid){ rr ld sum=0; for (rr int i=1;i<=n;++i) sum+=max(mid*2,a[i]); return sum-mid*n; } signed main(){ n=iut(); for (rr int i=1;i<=n;++i) a[i]=iut(); rr ld l=0,r=1000000000; while (l+1e-8<r){ rr ld k=(r-l)/3,lmid=l+k,rmid=r-k; if (calc(lmid)<calc(rmid)) r=rmid; else l=lmid; } return !printf("%.8Lf",calc(l)/n); }
/* Author : Hocky Yudhiono 11/14/2020 11:34:48 PM 1. You can sort the query if offline! 2. Don't bring the dp remaining state when dfsing on DP on Tree. 3. Try to reverse (Think from the back) if you stuck. 4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs. 5. Try to reduce a problem, think of it when you're making subtasks like when problemsetting. */ #include <algorithm> #include <iostream> #include <numeric> #include <cstdlib> #include <cassert> #include <cstring> #include <iomanip> #include <cstdio> #include <limits> #include <string> #include <vector> #include <cmath> #include <deque> #include <queue> #include <stack> #include <map> #include <set> using namespace std; typedef long long LL; typedef long long ll; typedef long double LD; typedef vector<int> vi; typedef pair<LL,LL> PLL; typedef pair<LL,int> PLI; typedef pair<int,int> PII; typedef pair<int,int> pii; typedef vector<vector<LL>> VVL; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define popf pop_front #define pf push_front #define popb pop_back #define mp make_pair #define pb push_back #define remove erase #define fi first #define se second // If the time limit is strict, try not to use long double // Remember to undefine if the problem is interactive #define endl '\n' #define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl const double EPS = 1e-9; const int INFMEM = 63; const int INF = 1061109567; const LL LINF = 4557430888798830399LL; const double DINF = numeric_limits<double>::infinity(); const LL MOD = 1000000007; const int dx[8] = {0,0,1,-1,1,-1,1,-1}; const int dy[8] = {1,-1,0,0,1,-1,-1,1}; // Do dir^1 to get reverse direction const char dch[4] = {'R','L','D','U'}; // const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"}; const double PI = 3.141592653589793; inline void open(string a){ freopen((a+".in").c_str(),"r",stdin); freopen((a+".out").c_str(),"w",stdout); } inline void fasterios(){ // Do not use if interactive ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(10); } LL n; LL cnt[105]; bool isi[105][105]; int main(){ cin >> n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ char ch; cin >> ch; isi[i][j] = (ch == '1'); } } for(int i = 1;i <= n;i++) isi[i][i] = 1; for(int k = 1;k <= n;k++){ for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ isi[i][j] |= (isi[i][k] && isi[k][j]); } } } LD ans = 0; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ if(isi[j][i]) cnt[i]++; } ans += ((LD)(1)/(LD)cnt[i]); } // for(int i = 1;i <= n;i++) cout << cnt[i] << endl; cout << fixed << setprecision(15) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; int c[10], tmp = 0; for(int i=0; i<10; i++){ if(s[i] == 'o') c[i] = 1; else if(s[i] == '?') c[i] = 0; else c[i] = -1; if(c[i] == 1) tmp++; } int ans = 0, v1, v2, v3, v4; for(int i=0; i<10000; i++){ int t = i; v1 = t%10; t /= 10; v2 = t%10; t /= 10; v3 = t%10; t /= 10; v4 = t%10; t /= 10; if(c[v1] < 0 || c[v2] < 0 || c[v3] < 0 || c[v4] < 0) continue; int cnt = 0; bool ch[10]; for(int i=0; i<10; i++) ch[i] = true; if(c[v1] > 0){ if(ch[v1]) cnt++; ch[v1] = false; } if(c[v2] > 0){ if(ch[v2]) cnt++; ch[v2] = false; } if(c[v3] > 0){ if(ch[v3]) cnt++; ch[v3] = false; } if(c[v4] > 0){ if(ch[v4]) cnt++; ch[v4] = false; } if(cnt >= tmp){ ans++; } } cout << ans << endl; return 0; }
/*      />  フ      |  _  _|      /`ミ _x 彡      /      |     /  ヽ   ?  / ̄|   | | |  | ( ̄ヽ__ヽ_)_)  \二つ */ #include <queue> #include <vector> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MP make_pair #define ll long long #define fi first #define se second using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (;!isdigit(c);c=getchar()) if (c=='-') f=1; for (;isdigit(c);c=getchar()) x=x*10+(c^48); if (f) x=-x; } template<typename F> inline void write(F x, char ed = '\n') { static short st[30];short tp=0; if(x<0) putchar('-'),x=-x; do st[++tp]=x%10,x/=10; while(x); while(tp) putchar('0'|st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } //#pragma GCC optimize(2) const int N = 3005; const int P = 998244353; int n, k, f[N][N]; int dfs(int n, int k) { if (!k && !n) return 1; if (!k) return 0; if (k > n) return 0; if (f[n][k] != -1) return f[n][k]; return f[n][k] = (dfs(n - 1, k - 1) + dfs(n, k << 1)) % P; } int main() { memset(f, -1, sizeof(f)); read(n), read(k); write(dfs(n, k)); return 0; }
#include <bits/stdc++.h> #include <vector> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; int main() { int n; cin >> n; vector<ll> c(200); rep(i, n){ ll a; cin >> a; c[a % 200]++; } ll ans = 0; for(int i = 0; i < 200; i++){ ans += c[i] * (c[i] - 1) / 2; } cout << ans << endl; return 0; }
#include <iostream> using namespace std; struct fg { int f; int g; }; fg func(int n) { fg r = { 0, 0 }; if (n == 0) return r; while (n % 3 == 0) { r.f++; n /= 3; } r.g = n % 3; return r; } int main() { bool frag = true; int n; string c; cin >> n >> c; int bf = 0; fg cfg = { 0, 1 }; fg bffgu = { 0, 0 }, bffgd = { 0, 0 }; for (int i = 0; i < n; i++) { if (cfg.f == 0) { switch (c[i]) { case 'B': break; case 'W': bf += cfg.g; break; case 'R': bf += 2 * cfg.g; break; } } bffgd = func(i + 1); bffgu = func(n - i - 1); cfg.f = cfg.f + bffgu.f - bffgd.f; cfg.g = (cfg.g * bffgu.g) % 3; if (cfg.g == bffgd.g) cfg.g = 1; else cfg.g = 2; bf = bf % 3; } if (n % 2) { switch (bf) { case 0 : cout << "B"; break; case 1 : cout << "W"; break; case 2: cout << "R"; break; } } else { switch (bf) { case 0: cout << "B"; break; case 1: cout << "R"; break; case 2: cout << "W"; break; } } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<double,double>> m (n); for ( int i = 0; i < n; i ++ ) { cin >> m[i].first >> m[i].second; } int cnt=0; for (int i = 0 ; i < n ; i++){ for (int j = i+1; j < n; j++){ if (((m[j].second - m[i].second) / (m[j].first - m[i].first) <=1) &&(m[j].second - m[i].second) / (m[j].first - m[i].first) >=-1) cnt++; // cout << "m[j].sec = " << m[j].second - m[i].second << " m[i].first = " << m[j].first - m[i].first << " ==== " << (m[j].second - m[i].second) / (m[j].first - m[i].first) << " cnt = " <<cnt << endl; } //cout << endl; } cout <<cnt<<endl; return 0; }
#include <bits/stdc++.h> #define ll long long int using namespace std; int main() { ll n, f, i, j, a, b; cin >> n; vector<pair<ll, ll> > vect; ll sum = 0; for (i = 0; i < n; i++) { cin >> a >> b; vect.push_back(make_pair(a, b)); } sort(vect.begin(),vect.end()); ll mn=vect[0].first+vect[0].second; for(i=1;i<n;i++) { mn=min(mn,(max(vect[0].first,vect[i].second))); } cout<<mn<<endl; }
#include <bits/stdc++.h> #define Mod 998244353 using namespace std; int n, m, k, pre[200010], ans; int fpow(int x, int y) { int res = 1; while (y) { if (y & 1) res = 1ll * res * x % Mod; x = 1ll * x * x % Mod; y >>= 1; } return res; } int main() { cin >> n >> m >> k; if (n == 1) return printf("%d", fpow(k, m)), 0; if (m == 1) return printf("%d", fpow(k, n)), 0; for (int i = 1; i <= k; i++) ans = (ans + 1ll * (fpow(i, n) - fpow(i - 1, n)) * fpow(k - i + 1, m)) % Mod; if (ans < 0) ans += Mod; cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int arr[100005]; long long mod=998244353; int main() { long long a,b,c; scanf("%lld%lld%lld",&a,&b,&c); long long sum,num,ans; if(a%2==0) { sum=((a/2)%mod*(a+1)%mod)%mod; } else if(a%2==1) { sum=(a%mod*((a+1)/2)%mod)%mod; } if(b%2==0) { num=((b/2)%mod*(b+1)%mod)%mod; } else if(b%2==1) { num=(b%mod*((b+1)/2)%mod)%mod; } if(c%2==0) { ans=((c/2)%mod*(c+1)%mod)%mod; } else if(c%2==1) { ans=(c%mod*((c+1)/2)%mod)%mod; } //printf("%lld %lld %lld\n",sum,num,ans); long long aans=(num*sum)%mod; aans=(aans*ans)%mod; printf("%lld\n",aans); }
#include<iostream> #include<string> #include<vector> #include<set> #include<iomanip> #include<algorithm> #include<cmath> #include<bitset> #include<queue> #include<stack> #include<utility> #include<cstdlib> #include<cstdio> #include<map> #include<unordered_set> #include<unordered_map> #include<list> #include<tuple> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define FOR(i, a, b) for(ll i=(a); i<(b); ++i) #define REP(i, n) FOR(i, 0, n) #define NREP(i, n) FOR(i, 1, n+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 (b<a) { a=b; return 1; } return 0; } int main(void){ ll N; cin>>N; vl A(2*N); REP(i,2*N) cin>>A[i]; vl B=A; sort(B.begin(),B.end()); ll med=B[N-1],cnt=0; REP(i,N){ if(B[i]==med) cnt++; } vl code(2*N); REP(i,2*N){ if(A[i]<=med){ if(A[i]<med) code[i]=-1; else if(cnt>0) code[i]=-1, cnt--; else code[i]=1; } else code[i]=1; } ll nowc=0, sum=0; string S; REP(i,2*N){ if(nowc==0){ if(code[i]>0) nowc=1; else nowc=-1; } if(nowc>0){ if(code[i]>0) S+='(', sum++; else S+=')', sum--; } else{ if(code[i]<0) S+='(', sum++; else S+=')', sum--; } if(sum==0) nowc=0; } cout<<S<<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 all(x) (x).begin(),(x).end() using namespace std; int main(){ ll k; cin>>k; ll ans=0; V dp(2e5+1,0); for(ll a=1;a<=2e5;a++){ ll res=0; for(ll b=1;b*b<=a;b++){ if(a%b==0){ res++; if(b*b!=a)res++; } } dp[a]=dp[a-1]+res; } for(ll i=1;i<=k;i++){ ll maxi=k/i; ans+=dp[maxi]; } cout<<ans<<endl; }
/*/^--^\ \____/ / \ _____ _ __ __ ____ _ ____ ____ _____ | || ()_)| |\ \/ /| ===|| |__ / (__` / () \|_ _| \__ __/ |_| |_|/_/\_\|____||____|\____)/__/\__\ |_| |^|^\ \^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^| | | |\ \| | | | | | | | | | | | | | | | | | | | | | | | | #####/ /################################################# | | |\/ | | | | | | | | | | | | | | | | | | | | | | | | | |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| | #include <orz/i_am_noob.h> #include <orz/balbit.h> #include <orz/nathanlee726.h>*/ #include <bits/stdc++.h> using namespace std; using ll=long long; using ull=unsigned long long; using pii=pair<ll,ll>; using pdd=pair<double,double>; #define int ll #define For(i,a,b) for(int i=a;i<=b;i++) #define Forr(i,a,b) for(int i=a;i>=b;i--) #define F first #define S second #define eb emplace_back #define all(x) x.begin(),x.end() #define sz(x) ((int)x.size()) #define mkp make_pair #define MOD (ll)(1000000007) #define INF (1e15) #define EPS (1e-6) #ifdef LOCALMEOW #define debug(...) do{\ cerr << __PRETTY_FUNCTION__ << " - " << __LINE__ <<\ " : ("#__VA_ARGS__ << ") = ";\ _OUT(__VA_ARGS__);\ }while(0) template<typename T> void _OUT(T x) { cerr << x << "\n"; } template<typename T,typename...I> void _OUT(T x,I ...tail) { cerr << x << ", "; _OUT(tail...); } #else #define debug(...) #endif mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); void FASTIO(){ ios::sync_with_stdio(false); cin.tie(0); } int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int lcm(int a,int b) { return a/gcd(a,b)*b; } int fpow(int b,int p){ int ans=1,now=b; while(p){ if(p&1) ans=ans*now%MOD; p/=2; now=now*now%MOD; } return ans; } void maxify(int &a,int b) { if(b>a) a=b; } void minify(int &a,int b) { if(b<a) a=b; } int gcd(int a,int b,int &x,int &y){ if(b==0){ x=1; y=0; return a; } int x1,y1; int g=gcd(b,a%b,x1,y1); x=y1; y=x1-y1*(a/b); return g; } int32_t main(){ FASTIO(); //code... int N; cin>>N; int n,s,x; while(N--){ cin>>n>>s>>x; int a,b; int g=gcd(x,n,a,b); a%=n; if(a<0) a+=n; //cout<<a<<" "<<a*x%n<<"\n"; b=(a*(n-s)/g)%(n/g); if(b<0) b+=n; if((s+b*x)%n!=0) cout<<"-1\n"; else cout<<b<<"\n"; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0 ; i < (int)(n) ; i++) 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 (b<a) {a=b; return 1;} return 0;} //ax + by = g , return {g,x,y} tuple<ll,ll,ll> extGCD(ll a, ll b){ if(b == 0) return {a,1,0}; ll g,x,y; tie(g,x,y) = extGCD(b,a%b); return {g,y,x-a/b*y}; } //s + kx = 0(mod n) // x = -s*k-^1 (mod n) void solve(){ ll n,k,s,g,x,y; cin >> n >> s >> k; tie(g,x,y) = extGCD(k,n); if(s%g != 0){ cout << -1 << endl; return; } n /= g; k /= g; s /= g; ll ans = (n+(-s*x)%n)%n; cout << ans << endl; } int main() { int t; cin >> t; rep(i,t){ solve(); } return 0; }
#include <bits/stdc++.h> #define ll long long #define REP(i,a,b) for(int i=a;i<=b;i++) using namespace std; int main(){ vector <int> arr; int n=3; while(n--){ int k; cin>>k; arr.push_back(k); } sort(arr.begin(),arr.end()); if(arr[0]+arr[2]==2*arr[1]){ cout<<"Yes"<<endl; return 0; } cout<<"No"<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define int ll using namespace __gnu_pbds; #define ff first #define ss second #define pb push_back typedef long long ll; #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> #define endl "\n" map<int,int> mp; void solve() { int a,b,c; cin>>a>>b>>c; c*=c; a*=a; b*=b; if(a+b<c) { cout<<"Yes"; } else cout<<"No"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t=1; while(t--) { solve(); } return 0; } ///order_of_key (k) : Number of items strictly smaller than k . ///find_by_order(k) : K-th element in a set (counting from zero).
#include <bits/stdc++.h> using namespace std; int main() { int n,a,b; cin >> n >> a >> b; cout << n-a+b << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n,x,i; string a; cin>>n>>x>>a; for(i=0;i<n;i++) { if(a[i]=='o') x++; if(a[i]=='x' && x>0) x--; } cout<<x<<endl; }
#include<bits/stdc++.h> using namespace std; #define For(i,a,b) for(int i=(a),(i##i)=(b);i<=(i##i);++i) #define Rep(i,n) for(int i=0,(i##i)=(n);i<(i##i);++i) #define Fodn(i,a,b) for(int i=(a),(i##i)=(b);i>=(i##i);--i) #define pln puts("") #define il inline #define ff first #define ss second using LL=long long;using u32=unsigned int;using u64=unsigned LL;using LD=long double; template<typename T>using pii=pair<T,T>; template<typename T>il bool read(T&x){ x=0;char c=getchar();int f=1;while(!isdigit(c)&&(c!='-')&&(c!=EOF))c=getchar(); if(c==EOF)return 0;if(c=='-')f=-1,c=getchar(); while(isdigit(c)){x=(x<<1)+(x<<3)+(c&15);c=getchar();}x*=f;return 1;} template<typename T,typename...Args> il bool read(T&x,Args&...args){bool res=1;res&=read(x);res&=read(args...);return res;} const int M=1000000007,INF=0x3f3f3f3f;const LL INFLL=0x3f3f3f3f3f3f3f3fLL; const int N=500010;const double EPS=1e-6; LL n,m,q; LL a[N],b[N]; bool f=0; LL g[2]={1,1}; LL u,v; struct HIS{ bool f; LL g[2]; LL u,v; }hist[N]; il void sav(int x){ hist[x].f=f; hist[x].g[0]=g[0];hist[x].g[1]=g[1]; hist[x].u=u;hist[x].v=v; } inline void init(){ read(n);For(i,1,n)read(a[i],b[i]); u=a[1],v=b[1];read(m);g[0]=1;g[1]=1; sav(0); For(i,1,m){ int op;read(op); if(op==1){ g[0^f]*=-1;f^=1; swap(u,v);v*=-1; }else if(op==2){ g[1^f]*=-1;f^=1; swap(u,v);u*=-1; }else if(op==3){ LL p;read(p); g[0^f]*=-1; u=2*p-u; }else{ LL p;read(p); g[1^f]*=-1; v=2*p-v; } sav(i); } } inline void solve(){ read(q); while(q--){ LL ai,bi;read(ai,bi); LL hu=hist[ai].u,hv=hist[ai].v; if(hist[ai].f)swap(hu,hv); LL ansx=hu+hist[ai].g[0]*(a[bi]-a[1]),ansy=hv+hist[ai].g[1]*(b[bi]-b[1]); if(hist[ai].f)swap(ansx,ansy); printf("%lld %lld\n",ansx,ansy); } } signed main(){ init();solve(); return 0; }
#include <bits/stdc++.h> // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18; void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);} void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);} void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);} void ioinit() { cout << fixed << setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); } // clang-format on ll fibrest(ll x0, ll y0, ll n, bool isx) { ll iter = 0; vector<ll> fib = {x0, y0}; ll x = x0; ll y = y0; ll op = 0; while (true) { if (isx) { x = x + y; } else { y = y + x; } if (x >= n) break; op++; isx = !isx; } return op; } void op(vector<ll> &ops, ll &x, ll &y, ll t) { if (t == 1) { x = x + 1; } if (t == 2) { y = y + 1; } if (t == 3) { x = x + y; } if (t == 4) { y = y + x; } ops.push_back(t); } void output(vector<ll> &ops) { ll k = ops.size(); print(ops.size()); for (auto ope : ops) { print(ope); } } bool solve(ll n) { ll x = 0; ll y = 0; vector<ll> ops; if (n < 10) { while (x < n) { op(ops, x, y, 1); } output(ops); return true; } op(ops, x, y, 1); bool isx = false; while (true) { if (x + y > n) { while (x < n) { op(ops, x, y, 1); } break; } ll curop = fibrest(x, y, n, isx); { ll k = 0; while (true) { ll r = fibrest(x + k + 1, y, n, isx); if (r != curop) break; //perr(curop, r); op(ops, x, y, 1); k = k + 1; } } if (isx) { op(ops, x, y, 3); } else { op(ops, x, y, 4); } isx = !isx; if (x >= n) break; } if (ops.size() > 130) { return false; } output(ops); return true; } int main() { ioinit(); ll n; cin >> n; if (!solve(n)) { assert(false); } return 0; // for (ll i = 1000000000000000000; true; i--) { // if (!solve(i)) { // assert(false); // return 0; // } // } }
#include <bits/stdc++.h> using namespace std; int N, M; #define pii pair<int, int> #define fi first #define se second vector<pii> v; map<int, int> pos; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> N >> M; for(int i = 0; i < M; i++){ int a, b; cin >> a >> b; v.push_back({a, b}); } sort(v.begin(), v.end()); pos[N] = true; int prev = 0; vector<int> add, del; for(int i = 0; i < M; i++){ if(v[i].fi != prev){ for(auto& p : add) pos[p] = true; for(auto& p : del) pos[p] = false; prev = v[i].fi; add.clear(); del.clear(); } int b = v[i].se; if(pos[b - 1] || pos[b + 1]) add.push_back(b); else del.push_back(b); } for(auto& p : add) pos[p] = true; for(auto& p : del) pos[p] = false; int sum = 0; for(auto& p : pos){ if(p.se == 1) sum++; } cout << sum << '\n'; }
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <numeric> #include <functional> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for(ll i = 0; i < n; i++) #define exrep(i, a, b) for(ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; vvl G; vl visited; vvl S; // S[v] : 頂点vに辿り着ける頂点集合 void dfs(ll sv, ll v) { // 行きの処理はこのへんに書く S[v].pb(sv); visited[v] = 1; for(ll u : G[v]) { if(!visited[u]) { dfs(sv, u); } // 帰りの処理はこのへんに書く } } int main() { ll n; cin >> n; G.resize(n); rep(i, n) { string s; cin >> s; rep(j, n) { if(s[j] == '1') { G[i].pb(j); } } } S.resize(n); rep(sv, n) { visited.assign(n, 0); dfs(sv, sv); } double ans = 0.0; rep(v, n) { ans += 1.0/(double)S[v].size(); } exout(ans); re0; }
// Jai Shree Ram #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ook order_of_key #define fbo find_by_order #define rep(i,a,n) for(int i=a;i<n;i++) #define ll long long #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define endl "\n" #define x first #define y second #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define sz(a) (int)a.size() #define pii pair<int,int> #define hell 1000000007 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;} template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;} const int N=1e9+5; const int M=1e7; struct node{ int l;int r; int val; int cnt; }t[M]; int cnt=0; int n; void extend(int v){ if(t[v].l==0){ t[v].l=++cnt; t[v].r=++cnt; } } void pushup(int v){ t[v].val = t[t[v].l].val + t[t[v].r].val; t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt; } void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){ if(tl>r || l>tr)return; if(l<=tl && tr<=r){ assert(tl == tr); t[v].val += val.x; t[v].cnt += val.y; return; } extend(v); int mid=(tl+tr)/2; update(t[v].l,l,r,val,tl,mid); update(t[v].r,l,r,val,mid+1,tr); pushup(v); } pii query(int v,int l,int r,int tl=0,int tr=1e9){ if(tl>r || l>tr)return {0,0}; if(l<=tl && tr<=r){ return {t[v].val,t[v].cnt}; } int mid=(tl+tr)/2; extend(v); pii a =query(t[v].l,l,r,tl,mid); pii b =query(t[v].r,l,r,mid+1,tr); return {a.x+b.x,a.y+b.y}; } int solve(){ int n,m,q; cin >> n >> m >> q; int ans = 0; int roota = ++cnt; int rootb = ++cnt; vector<int>v1(n+1),v2(m+1); update(roota,0,0,{0,n}); update(rootb,0,0,{0,m}); rep(i,0,q){ int t; cin >> t; int x,y; cin >> x >> y; if(t == 1){ if(y > v1[x]){ auto s = query(rootb,0,y); ans += s.y*y; s = query(rootb,v1[x]+1,y); ans -= s.x; s = query(rootb,0,v1[x]); ans -= v1[x]*s.y; } else if(y < v1[x]){ auto s = query(rootb,0,v1[x]); ans -= s.y*v1[x]; s = query(rootb,0,y); ans += s.y*y; s = query(rootb,y+1,v1[x]); ans += s.x; } update(roota,v1[x],v1[x],{-v1[x],-1}); update(roota,y,y,{y,1}); v1[x] = y; } else{ if(y > v2[x]){ auto s = query(roota,0,y-1); ans += s.y*y; s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,v2[x],y-1); ans -= s.x; } else if(y < v2[x]){ auto s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,0,y-1); ans += s.y*y; s = query(roota,y,v2[x]-1); ans += s.x; } update(rootb,v2[x],v2[x],{-v2[x],-1}); update(rootb,y,y,{y,1}); v2[x] = y; } cout << ans << endl; } return 0; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t=1;//cin>>t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int #define ll long long #define quickie ios_base::sync_with_stdio(false); cin.tie(NULL); #define rep(i, a, b) for(int i=a; i<b; i++) #define rep1(i, a, b) for(int i=a; i<=b; i++) #define pb push_back #define mp make_pair #define fi first #define se second #define SZ(x) ((int)(x).size()) #define db double #define mi map<int, int> #define vi vector<int> #define mod 1000000007 #define INF 1e17 #define test int t; cin >> t; #define all(a) a.begin(),a.end() #define pi 3.141592653589 #define fact(n) rep(i, 1, n+1)ft.pb((ft[i-1]*i)%mod) ; int power(int x, int y) ; int gcd(int a, int b) ; vi ft = {1} ; int modInv(int i) {return power(i,mod-2);} int ncr(int n,int r) {return (n>=r?(ft[n]*modInv(ft[r]))%mod*modInv(ft[n-r])%mod:0);} int power(int x, int y) { int res = 1; x%mod; while (y > 0) { if (y & 1) res = (res*x)%mod ; y = y>>1; x = (x*x)%mod ; } return res; } int gcd(int a,int b) { if(a==0) return b; return gcd(b%a,a); } // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template<typename T> // using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; // template<typename T> // using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>; // (*****FOR USING ORDERED SET CHANGE INT*******) // struct chash { // const int RANDOM = (long long)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count(); // static unsigned long long hash_f(unsigned long long x) { // x += 0x9e3779b97f4a7c15; // x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; // x = (x ^ (x >> 27)) * 0x94d049bb133111eb; // return x ^ (x >> 31); // } // static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; } // int operator()(int x) const { return hash_f(x)^RANDOM; } // }; vi v[2001] ; int vis[2001]; void dfs(int n) { vis[n] = 1 ; for(auto i : v[n]) { if(!vis[i]) dfs(i) ; } } signed main() { quickie int n, m ; cin >> n >> m ; rep(i, 0, m) { int x, y ; cin >> x >> y ; v[x].pb(y) ; } int ans = 0 ; rep(i, 1, n+1) { memset(vis, 0, sizeof(vis)) ; dfs(i) ; rep(j, 1, n+1) if(vis[j]) ans++ ; } cout << ans << "\n" ; }
#include<bits/stdc++.h> using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++) #define INF 10000000000 #define MOD 1000000007 using ll = long long; using pint = pair<int,int>; using pll = pair<ll,ll>; using Graph = vector<vector<int>>; bool isOK(int n); int main(){ int N; cin>>N; int res = 0; rep1(i,N)res += isOK(i)?1:0; cout<<res<<endl; } bool isOK(int n){ //10進数で7check; int n1 = n, n2 = n; while(n1){ if(n1%10==7)return false; n1/=10; } while(n2){ if(n2%8==7)return false; n2/=8; } return true; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) #define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) #define REP(i, n) FOR(i, 0, (n)-1) #define sqr(x) ((x) * (x)) #define all(x) (x).begin(), (x).end() #define reset(x, y) memset(x, y, sizeof(x)) #define uni(x) (x).erase(unique(all(x)), (x).end()) #define BUG(x) cerr << #x << " = " << (x) << endl #define pb push_back #define eb emplace_back #define mp make_pair #define _1 first #define _2 second #define chkmin(a, b) a = min(a, b) #define chkmax(a, b) a = max(a, b) int main() { #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int n; cin >> n; int ans = 0; FOR(i, 1, n) { auto check = [&](int d) { int x = i; while (x) { if (x % d == 7) return true; x /= d; } return false; }; if (!check(10) && !check(8)) ans++; } cout << ans; }
//x<<y=x*2^y,x>>y=x/2^y //1e5+3 is prime // in a matrix, all elements of a primary diagonal have constant diff of coordinates // and a secondary diagonal has constant sum of coordinates //use ll() for using an integer in self-built fn //(x&-x) bitwise and of (x and its 2's complement) returns (last set bit) //eg if x=1010 then it will return 0010 // careful dont print empty container(run time error) //v.erase O(n) //use ("\n") instead of endl as endl flushes the output buffer //every time so takes more time than \n (TLE) //stoll() and to_string((less than 1e19)) //INT_MAX is approx 3e10 //For sets use set_name.lower_bound(x)(strictly O(logn)) //NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is O(n)); #include<bits/stdc++.h> typedef long long int ll; #define ull unsigned long long int #define lld long double #define endl "\n" #define fi first #define sec second #define sp setprecision #define lb lower_bound #define ub upper_bound #define For(i, a, b) for(long long int i = (a); i <= (b); i++) #define Forr(i, a, b) for(long long int i = (a); i >= (b); i--) #define pb push_back #define mp(a,b) make_pair(a,b) #define vll vector<ll> #define pll pair<ll,ll> #define vlld vector<lld> #define vi vector<int> #define vch vector<char> #define sll set<ll> #define sch set<ch> #define vpll vector< pair<ll,ll> > #define vpii vector< pair<int,int> > #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define mll map<ll, ll> #define mcll map<char,ll> #define sz(container) ll((container).size()) #define fill(a,b) memset(a, b, sizeof(a)) #define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) using namespace std; lld pi=3.1415926535897932; const ll mod=1000000007; const ll MOD=998244353; const ll dx[4]={-1, 0, 1, 0} , dy[4]={0, 1, 0, -1}; const ll dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1} , dyy[8]={0, 1, 1, 1, 0, -1, -1, -1}; string ds="RLDU"; /*************************************** struct cmp { bool operator() (const pll &a, const pll &b) const { ll lena = a.sec - a.fi + 1; ll lenb = b.sec - b.fi + 1; if (lena == lenb) return a.fi < b.fi; return lena > lenb; } }; ********************************/ ll lcm(ll a, ll b) { ll g=__gcd(a, b); return a/g*b; } ll binpow(ll a, ll b,ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll gcdExtended(ll a, ll b, ll &x, ll &y){ if(a == 0){ x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return gcd; } ll modInverse(ll a, ll m){ ll x, y; ll g = gcdExtended(a, m, x, y); ll res = (x%m + m) % m; return res; } ll modinv(ll n,ll mod) { return binpow(n, mod - 2,mod); } /**************coding****************************************/ int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast_io; ll T=1;//cin>>T; For(i,1,T) { ll n;cin>>n; vll a(n),b(n); vll tot;ll sum=0; For(j,0,n-1) cin>>a[j]>>b[j],tot.pb(2*a[j]+b[j]),sum+=a[j]; sort(all(tot),greater<ll>()); ll cur=0; For(j,0,n-1) { cur+=tot[j]; if(cur>sum) { cout<<j+1;return 0; } } } }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int H, W; string S[2000]; ll dp[2000][2000][3]; const int mod = 1e9 + 7; void cal(int i, int j, int k, ll tmp) { if (i < H && j < W && S[i][j] == '.') { dp[i][j][k] += tmp; dp[i][j][k] %= mod; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> H >> W; rep(i, H) cin >> S[i]; rep(i, H) rep(j, W) { if (i == 0 && j == 0) { cal(1, 0, 0, 1); cal(0, 1, 1, 1); cal(1, 1, 2, 1); continue; } ll tmp = dp[i][j][0] + dp[i][j][1] + dp[i][j][2]; cal(i + 1, j, 0, tmp + dp[i][j][0]); cal(i, j + 1, 1, tmp + dp[i][j][1]); cal(i + 1, j + 1, 2, tmp + dp[i][j][2]); } co((dp[H - 1][W - 1][0] + dp[H - 1][W - 1][1] + dp[H - 1][W - 1][2]) % mod); Would you please return 0; }
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} const int L=1000; void verify(vector<int> A,vector<int> B){ int N=A.size(); vector<vector<int>> g(N); rep(i,N){ A[i]--;B[i]--; g[i].push_back(A[i]); g[i].push_back(B[i]); } auto bfs = [&](){ for(int i=0;i<N;i++){ queue<int> Q; vector<int> dist(N,MOD); dist[i]=0; Q.push(i); while(!Q.empty()){ int now=Q.front(); Q.pop(); if(dist[now]>10) return false; for(auto nex:g[now]){ if(nex>=N){ cout<<"INVALID"<<endl; return false; } if(dist[nex]==MOD){ dist[nex]=dist[now]+1; Q.push(nex); } } } } return true; }; if(bfs()) cout<<"OK"<<endl; else cout<<"NG"<<endl; } int MASK=(1<<10)-1; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector<int> A(N,-1),B(N,-1); for(int i=0;i<N;i++){ int value=i; int targetValue=(value<<1)&MASK; A[i]=(value<<1)&MASK; for(int d=9;d>=0;d--){ if(A[i]<N) break; if(A[i]&(1<<d)) A[i]^=(1<<d); } B[i]=targetValue+1; for(int d=9;d>=0;d--){ if(B[i]<N) break; if(B[i]&(1<<d)) B[i]^=(1<<d); } } //verify(A,B); for(int i=0;i<N;i++) cout<<A[i]+1<<" "<<B[i]+1<<endl; return 0; }
//#include "bits/stdc++.h" #define _USE_MATH_DEFINES #include<cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <deque> #include <algorithm> #include <functional> #include <iostream> #include <list> #include <map> #include <array> #include <unordered_map> #include<unordered_set> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #include <iterator> #include<iomanip> #include<complex> #include<fstream> #include<assert.h> #include<stdio.h> using namespace std; #define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(c) begin(c),end(c) #define int ll #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair //typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, double> pid; typedef pair<double, int> pdi; typedef pair<double, double> pdd; typedef vector< vector<int> > mat; 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 = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; rep(i, 0, N) { cout << ((2 * i) % N) + 1 << " " << ((2 * i + 1) % N) + 1 << endl; } return 0; }
// #include<atcoder/all> // using namespace atcoder; #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; const ld PI = acos(-1); const ld EPS = 0.0000000001; const ll LINF = 1LL<<60; const int INF = 1LL<<17; #define rep(i, n) for(ll i=0; i<(ll)(n); i++) #define repd(i, n) for(ll i=n-1; 0<=i; i--) #define FOR(i, a, b) for(ll i=a; i<(ll)(b); i++) #define FORD(i, a, b) for(ll i=b-1; (ll)(a)<=i; i--) #define ALL(x) x.begin(), x.end() #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; string YES[2] = {"NO", "YES"}; string yes[2] = {"No", "Yes"}; // modint: mod 計算を int を扱うように扱える構造体 template<int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; using mint = Fp<1000000007>; int main(){ ll n; cin >> n; set<ll> st; rep(i, n){ ll x; cin >> x; st.insert(x); } while(1<st.size()){ ll p = *st.rbegin(); st.erase(p); ll nv = p%*st.begin(); if(nv==0) continue; st.insert(nv); } cout << *st.begin() << endl; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; using Graph = vector<vector<int>>; typedef long long ll; #define rep(i,n) for(int i=0; i<(n); i++) int N; // 深さ優先探索 vector<bool> seen; void dfs(const Graph &G, int v) { seen[v] = true; // v を訪問済にする // v から行ける各頂点 next_v について for (auto next_v : G[v]) { if (seen[next_v]) continue; // next_v が探索済だったらスルー dfs(G, next_v); // 再帰的に探索 } } int all_counter = 0, counter = 0; vector<bool> gone; void dfs2(const Graph &G, int v) { // if(all_of(gone.begin(), gone.end(), [](int x) { return x == 1; })){ // cout << "どうでしょう" << v << endl; // all_counter++; // // gone.assign(N, false); // return; // } if(gone[v] == 0){ counter++; } gone[v] = true; for (auto next_v : G[v]) { gone[next_v] = true; } if(all_of(gone.begin(), gone.end(), [](int x) { return x == 1; })){ all_counter++; return; } rep(i, N){ if(gone[i] == 0){ // cout << "さいき " << i << endl; dfs2(G, i); } } // if(all_of(gone.begin(), gone.end(), [](int x) { return x == 1; })){ // return; // } } int main() { // 頂点数と辺数 cin >> N; // グラフ入力受取 (ここでは無向グラフを想定) Graph G(N); for (int i = 0; i < N; ++i) { string s; cin >> s; rep(j, N){ if(s[j] == '1') G[i].push_back(j); } } Graph G2(N); // 頂点 0 をスタートとした探索 seen.assign(N, false); // 全頂点を「未訪問」に初期化 rep(i, N){ dfs(G, i); rep(j, N){ if(seen[j]) G2[i].push_back(j); } seen.assign(N, false); } double res = 0.0; for (int i = 0; i < N; ++i) { int num = 0; for (int j = 0; j < N; ++j){ for(int k = 0;k < G2[j].size();k++){ if(G2[j][k] == i){ num++; break; } else if(j == i){ num++; break; } } } res += 1.0 / num; } printf("%.15f\n", res); // gone.assign(N, false); // rep(i, N){ // dfs2(G2, i); // gone.assign(N, false); // } // // cout << counter << endl; // // cout << all_counter << endl; // long double ans = (long double)counter/all_counter; // printf("%.15Lf\n", ans); }
#include <iostream> #include <algorithm> #include <vector> using namespace std; const int N = 1e5 + 100; int a[N], b[N], n, ev[N], od[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; long long ans = 0; int odd = 0, even = 0; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= n; i++) { if (i & 1) od[++odd] = a[i] - b[i]; else ev[++even] = a[i] - b[i]; ans += b[i]; } sort(ev + 1, ev + even + 1); reverse(ev + 1, ev + even + 1); sort(od + 1, od + odd + 1); reverse(od + 1, od + odd + 1); int i = 1, j = 1; while (i <= odd && j <= even && od[i] + ev[j] > 0) { ans += od[i] + ev[j]; i++, j++; } cout << ans << endl; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #define rep(i,N) for(int i=0;i<(int)N;i++) #define Swap(a,b) (a+=b,b=a-b,a-=b) static inline long Max(long a,long b){return a>b?a:b;} 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'); } static inline void AscRadix32(int *a,const int sz,const int minus) { int x,shift=0,elem[0400],temp[sz]; while(shift<040) { int bucket[0400]={0}; rep(i,sz){x=(a[i]>>shift)&0377;bucket[x]++;temp[i]=a[i];} elem[0]=0;rep(i,0377)elem[i+1]=elem[i]+bucket[i]; rep(i,sz){x=(temp[i]>>shift)&0377;a[elem[x]]=temp[i];elem[x]++;} shift+=010; } rep(i,minus>>1)Swap(a[sz-minus+i],a[sz-1-i]); rep(i,(sz-minus)>>1)Swap(a[i],a[sz-minus-1-i]); rep(i,sz>>1)Swap(a[i],a[sz-1-i]); } int main(void) { int N=IN(),A[N],B=0,m1=0,m2=0,p1[N/2],p2[N/2];long score=0; rep(i,N){A[i]=IN(),score+=A[i];} rep(i,N){B=IN();if(i&1){p1[i/2]=B-A[i];if(B-A[i]<0)m1++;}else{p2[i/2]=B-A[i];if(B-A[i]<0)m2++;}} AscRadix32(p1,N/2,m1);AscRadix32(p2,N/2,m2); rep(i,N>>1)if(p1[(N>>1)-1-i]+p2[(N>>1)-1-i]>0)score+=p1[(N>>1)-1-i]+p2[(N>>1)-1-i]; return OUT(score),0; }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i)) #define rep(i,n) FOR(i,0,n) #define all(v) begin(v),end(v) #ifdef NDEBUG #define assert(e) if(!(e)){stod("");} #endif #ifdef ONLINE_JUDGE #define debug(x) #define debug2(x,y) #define debug3(x,y,z) #else #define debug(x) std::cerr<<#x<<": "<<x<<"\n" #define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n" #define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n" #endif using ll=long long; using vi=std::vector<int>; using vvi=std::vector<vi>; using vll=std::vector<ll>; using vvll=std::vector<vll>; template<typename T> using vvec=std::vector<std::vector<T>>; template<typename T> auto make_v(size_t sz){return std::vector<T>(sz);} template<typename T,typename... Ts> auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));} template<typename T> void fill_v(T&var,const T&x){var=x;} template<typename V,typename T> void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){ s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;} inline void scan(int&a){scanf("%d",&a);} inline void scan(ll&a){scanf("%lld",&a);} inline void scan(char&a){scanf(" %c",&a);} inline void scan(double&a){scanf("%lf",&a);} template<typename T> inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}} template<typename First,typename...Args> inline void scan(First&f,Args&...args){scan(f);scan(args...);} inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);} inline void print(int a){printf("%d\n",a);} inline void print(ll a){printf("%lld\n",a);} inline void print(double a){printf("%.12f\n",a);} inline void print(std::string s){std::cout<<s<<"\n";} using namespace std; void solve() { int h, w; scan(h, w); vvi grid(h, vi(w, -1)); string s; rep (i, h) { scan(s); rep (j, w) { if (s[j] == '+') { grid[i][j] = 1; } } } debug(grid); vvi dp(h, vi(w, 0)); for (int i = h - 2; i >= 0; --i) { dp[i][w - 1] = grid[i + 1][w - 1] - dp[i + 1][w - 1]; } for (int j = w - 2; j >= 0; --j) { dp[h - 1][j] = grid[h - 1][j + 1] - dp[h - 1][j + 1]; } for (int i = h - 2; i >= 0; --i) { for (int j = w - 2; j >= 0; --j) { int cand1 = grid[i][j + 1] - dp[i][j + 1]; int cand2 = grid[i + 1][j] - dp[i + 1][j]; dp[i][j] = max(cand1, cand2); } } if (dp[0][0] > 0) { print("Takahashi"); } else if (dp[0][0] < 0) { print("Aoki"); } else { print("Draw"); } } int main() { solve(); return 0; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define Graph vector<vector<int>> typedef long long ll; typedef pair<int, int> P; const int INF = 1000000007; // cout << fixed << setprecision(桁数); int main(){ int H, W; cin >> H >> W; vector<vector<int>> A(2010, vector<int>(2010, 0)), dp(2010, vector<int>(2010, 0)); rep(i, H){ rep(j, W){ char c; cin >> c; int tmp = 0; if(c == '+') tmp = 1; if ((i + j + tmp) % 2 == 0){ A[i][j] = 1; } else { A[i][j] = -1; } } } dp[H - 1][W - 1] = 0; for (int i = H - 2; i >= 0; i--){ dp[i][W - 1] = dp[i + 1][W - 1] + A[i + 1][W - 1]; } for (int j = W - 2; j >= 0; j--) { dp[H - 1][j] = dp[H - 1][j + 1] + A[H - 1][j + 1]; } for (int a = H + W - 4; a >= 0; a--){ for (int i = 0; i < H - 1; i++){ int j = a - i; if (0 <= j && j < W - 1) { if (a % 2 == 0){ dp[i][j] = max(dp[i][j + 1] + A[i][j + 1], dp[i + 1][j] + A[i + 1][j]); } else { dp[i][j] = min(dp[i][j + 1] + A[i][j + 1], dp[i + 1][j] + A[i + 1][j]); } } } } if(dp[0][0] > 0) cout << "Takahashi" << endl; else if (dp[0][0] < 0) cout << "Aoki" << endl; else cout << "Draw" << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; int main(){ ll i,n; cin >> n; vector<ll> a(n),s(n+1,0); ll ans=0; for(i=0;i<n;i++){ cin >> a[i]; ans+=a[i]*a[i]*(n-1); } for(i=0;i<n;i++){ s[i+1]=s[i]+a[i]; } ll ans1=0; for(i=0;i<n;i++){ ans1+=2*a[i]*(s[n]-s[i+1]); } cout << ans-ans1 << endl; }
#include <bits/stdc++.h> using namespace std; /* ループ処理 0からnまで*/ #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int N; cin >> N; vector<int> A, B, C; rep(i, N) { int a; cin >> a; A.push_back(a); } rep(i, N) { int b; cin >> b; B.push_back(b); } rep(i, N) { int c; cin >> c; C.push_back(c); } long long cou = 0; sort(C.begin(), C.end()); sort(A.begin(), A.end()); rep(i, N) { if (C.at(i) > B.size()) { continue; } int BCj = B.at(C.at(i) - 1); decltype(A)::iterator left = lower_bound(A.begin(), A.end(), BCj); if (left != A.end() && *left == BCj) { decltype(A)::iterator right = upper_bound(A.begin(), A.end(), BCj); cou += right - left; } } cout << cou << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<bool> vb; typedef map<int, int> mii; typedef pair<int, int> ii; #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f3f #define each(x, s) for(auto& x : s) #define loop(x) for(int i = 0;i < x;i++) #define vloop(v, x) for(int v = 0;v < x;v++) #define avg(l, r) l + (r - l) / 2 #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define endl "\n" const ll mod = 1000000007; const double EPS = 1e-7; const double PI = acos(-1); struct dsu { vector<mii> y; vi id, sz; dsu(int n) : y(n), id(n), sz(n, 1) { loop(n) { int c; cin >> c; y[i][c] = 1; id[i] = i; } } int find(int x) { return x == id[x] ? x : id[x] = find(id[x]); } bool join(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (sz[a] > sz[b]) swap(a, b); id[a] = id[b]; sz[b] += sz[a]; for (auto& [k, v] : y[a]) { y[b][k] += v; } y[a].clear(); return true; } }; int main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int n, q; cin >> n >> q; dsu uf(n); int op, a, b; loop(q) { cin >> op >> a >> b; if (op == 1) uf.join(a - 1, b - 1); else cout << uf.y[uf.find(a - 1)][b] << endl; } return 0; }
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #include <bits/stdc++.h> using namespace std; const double TL = 1.5 * CLOCKS_PER_SEC; mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); #define clr(x, y) memset(x, y, sizeof(x)) #define forn(i, n) for (int i = 0; i < n; i++) #define LL long long const double pi = acos(-1.0); int n, q; int c[200005]; map<int, int> g[200005]; int f[200005]; int find(int u) { if (f[u] == u) return f[u]; else return f[u] = find(f[u]); } int main() { #ifdef LOCAL auto start_clock = clock(); freopen("in", "r", stdin); #endif scanf("%d%d", &n, &q); for (int i = 0; i < n; i++) { scanf("%d", &c[i]); g[i][c[i]] = 1; f[i] = i; } while (q--) { int op, x, y; scanf("%d%d%d", &op, &x, &y); if (op == 1) { x--, y--; int fx = find(f[x]); int fy = find(f[y]); if (fx == fy) continue; if (g[fx].size() < g[fy].size()) { swap(x, y); swap(fx, fy); } for (auto iter : g[fy]) { g[fx][iter.first] += iter.second; } g[fy].clear(); f[fy] = fx; } else { x--; int fx = find(x); int ans = 0; if (g[fx].find(y) != g[fx].end()) { ans = g[fx][y]; } printf("%d\n", ans); } } #ifdef LOCAL auto end_clock = clock(); cout << "Time:" << (end_clock - start_clock) * 1.0 / CLOCKS_PER_SEC << endl; #endif return 0; }
#include <bits/stdc++.h> #include <math.h> using namespace std; using ll = long long; double R,X,Y; vector<vector<ll>> v; vector<vector<vector<int>>> seen; int main() { cin >> R >> X >> Y; double d = sqrt(X * X + Y * Y); if (int(d)-d == 0 && int(d)%int(R) == 0) { cout << int(d) / int(R); } else if(int(d / R) != 0){ cout << int(d / R) + 1; } else { cout << 2; } }
#include<bits/stdc++.h> using namespace std; int main() { int sum = 0; int n,k,m; cin>>n; cin>>k; cin>>m; int x; for(int i = 0;i<n-1;i++) { cin>>x; sum = sum+x; } if((m*n-sum)<=k) { if((m*n-sum)<0) { cout<<0; } else { cout<<m*n-sum; } } else { cout<<-1; } }
#define SVR "CreatedBySmitRami" ///////////////////////Loading Payloads.......//////////////////////////// #include<bits/stdc++.h> #define f(i,a,b) for(int i = a; i < b; i++) #define fd(i,a,b) for(int i = a; i > b;i--) #define ll long long #define fl(i,a,b) for(ll i = a; i < b; i++) #define VI vector<int> #define VP vector<pair<int,int>> #define VS vector<string> #define VL vector<long long> #define VC vector<char> #define VF vector<float> #define PB push_back #define F first #define S second #define fa(i,skill) for(auto i : skill) #define fr(i,skill) for(VI :: reverse_iterator i = skill.rbegin(); i != skill.rend(); i++) #define PI pair<int,int> #define mi map<int,int> #define mci map<char,int> #define all(x) x.begin(),x.end() #define MAX 1000000001 using namespace std; ///////////////////////Engine Check.......//////////////////////////// void edit(VI &skill,int n) { f(i,0,n) { int x; cin >> x; skill.PB(x); } } void editl(VL &skill,ll n) { fl(i,0,n) { ll x; cin >> x; skill.PB(x); } } void achilles() { int n,m;cin >> n >> m; VP p(m); f(i,0,m) { cin >> p[i].F >> p[i].S; } int k;cin >> k; VP q(k); f(i,0,k) { cin >> q[i].F >> q[i].S; } int x = 1; f(i,0,k) x *= 2; VI ans; f(j,0,x) { string u = bitset<16>(j).to_string(); string s = ""; fd(i,15,15-k) s += u[i]; set<int> sss; f(i,0,k) { if(s[i]=='0')sss.insert(q[i].F); else sss.insert(q[i].S); } int y =0; f(i,0,m) { if(sss.count(p[i].F) && sss.count(p[i].S))y++; } ans.PB(y); } cout << *max_element(all(ans)) << "\n"; } ///////////////////////3....2....1...Ignition//////////////////////////// int main() { ios :: sync_with_stdio(0); cin.tie(0); //freopen("out.txt","w",stdin); /*int t;cin >>t ; while(t--)*/ {achilles();} return 0; }
#include <bits/stdc++.h> using namespace std; #define gcd(m,n) __gcd(m,n) #define lcm(m,n) m*(n/gcd(m,n)) #define fast std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pi acos(-1.0) #define endl '\n' #define MOD 1000000007 #define ull unsigned long long #define ll long long #define ld long double #define pb push_back #define dbg(x) cout << #x << " " << x << endl; ll power(ll base, ll exp) {ll res=1;while(exp>0) {if(exp%2==1) res=(res*base);base=(base*base);exp/=2;}return res;} ll mod(ll a, ll b) {return (a % b + b) % b;} ll powerm(ll base,ll exp,ll mod) {ll ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;} /*{{ x | (1 << k) sets the kth bit of x to one x & ~(1 << k) sets the kth bit of x to zero x ^ (1 << k) inverts the kth bit of x. x & (x-1) sets the last one bit of x to zero x & -x sets all the one bits to zero, except for the last one bit. x | (x-1) inverts all the bits after the last one bit.}} SOME BIT MANIPULATION*/ //__builtin_clz(x): the number of zeros at the beginning of the number //__builtin_ctz(x): the number of zeros at the end of the number //__builtin_popcount(x): the number of ones in the number //__builtin_parity(x): the parity (even or odd) of the number of ones struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; /*//DSU int parent[200005]; int rankr[200005]; int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void make_set(int v) { parent[v] = v; rankr[v] = 0; } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rankr[a] < rankr[b]) swap(a, b); parent[b] = a; if (rankr[a] == rankr[b]) rankr[a]++; } } */ vector<vector<ll>>arr,v; unordered_map<ll,ll>mp; ll solve(ll idx) { if(idx == arr.size()) { ll res = 0; for(ll i = 0; i < v.size(); i++) { ll a = v[i][0]; ll b = v[i][1]; if(mp[a] > 0 && mp[b] > 0) { res++; } } return res; } mp[arr[idx][0]]++; ll c1 = solve(idx+1); mp[arr[idx][0]]--; mp[arr[idx][1]]++; ll c2 = solve(idx+1); mp[arr[idx][1]]--; ll ans = max(c1,c2); return ans; } int main() { fast; ll n,m; cin>>n>>m; for(ll i = 0; i < m; i++) { ll x,y; cin>>x>>y; x--; y--; v.push_back({x,y}); } ll res = 0; ll k; cin>>k; for(ll i = 0; i < k; i++) { ll x,y; cin>>x>>y; x--; y--; arr.push_back({x,y}); } cout << solve(0) << endl; return 0; }
#include <bits/stdc++.h> #include <stdlib.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef vector<double> Vec; typedef vector<Vec> Mat; typedef pair<ll,ll> P; typedef pair<double,ll> Pd; typedef pair<double,double> PD; typedef priority_queue<P,vector<P>,greater<P> > P_queue; typedef priority_queue<Pd,vector<Pd>,greater<Pd> > Pd_queue; //const ll MOD=998244353; const ll mod=1000000007; const ll INF=1e15; const double DEL=1e-6; #define _GLIBCXX_DEBUG #define REP(i,a,b) for(int i=(int)a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define mp make_pair #define ALL(a) a.begin(),a.end() #define SORT(a) sort(ALL(a)) #define U_ERASE(V) V.erase(unique(ALL(V)), V.end()); ll MOD; ll N,K; void Add(ll &a, ll b){ a=(a+b)%MOD; return; } void Pro(ll &a, ll b){ a=(a*b)%MOD; return; } const ll MAXN1=1100000; const ll MAXN=MAXN1/2; ll d[101][MAXN]; ll Max[101]; int main(){ cin>>N>>K>>MOD; d[0][0]=1; REP(i,1,N+1) Max[i]=Max[i-1]+(K*i); REP(i,1,N+1) { // cout<<i<<"::"<<Max[i]<<"|"; rep(j,min(Max[i],Max[N-i])+1) { ll XX=Max[i]-j; if(XX<j) d[i][j]=d[i][XX]; else{ rep(k,K+1) { if(j>=k*i) Add(d[i][j],d[i-1][j-k*i]); else break; } } // cout<<d[i][j]<<' '; } // cout<<endl; } //cout<<N<<endl; vec ans(N+1,0); REP(i,1,N+1){ ll XX=N+1-i; if(ans[XX]) ans[i]=ans[XX]; else{ ll a=i-1, b=N-i; ll X=min(Max[a],Max[b])+1; rep(j,X) Add(ans[i],(d[a][j]*d[b][j])%MOD); } } REP(i,1,N+1) ans[i]=(ans[i]*(1+K)-1)%MOD; REP(i,1,N+1) cout<<ans[i]<<endl; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define int long long using namespace std; #define endl '\n' #define br cout<<endl #define mem(a) memset(a,0,sizeof(a)) const double pi=3.141592653589793238; #define d(x) cout<<x<<endl; #define de(x) cout<<#x<<" "<<x<<endl; #define pb push_back #define mp make_pair #define F first #define S second #define rev(a) reverse(a.begin(),a.end()) //const int inf = 100000000000000000; #define maxn 1000005 int mod = (int)(1e9 + 7); #define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0) #define all(x) x.begin(),x.end() //#define f(i,n) for(int i=1;i<=n;i++) #define fp(i,k,n) for(int i=k;i<=n;i+=1) #define fn(i,k,n) for(int i=k;i>=n;i-=1) void pv(vector<int> v){ for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;} void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;} int modexp(int x,int y) { int res = 1; x = x % mod; while (y > 0){ if (y & 1LL) res = (res*x) % mod; y = y>>1; x = (x*x) % mod; } return res; } int invmod(int a) { return modexp(a,mod-2); } // vector<int> edge[maxn]; // void ipgraph(int n,int m) { // fp(i,1,n) edge[i].clear(); // if(m==0) return; // fp(i,1,m) { // int a,b; // cin>>a>>b; // edge[a].pb(b); // edge[b].pb(a); // } // } /* void dfs(int node,int p) { for(int u : edge[node]) { if(u!=p) { dfs(u,node); } } } */ ////////////////////////////////////////////////////////////////////////////////// void solve() { int a[4]; cin>>a[0]>>a[1]>>a[2]>>a[3]; fp(i,1,14) { bitset<4> bs(i); int t0 = 0,t1 = 0; fp(j,0,3) { if(bs[j]) t1+=a[j]; else t0+=a[j]; } if(t0==t1) { d("Yes") return ; } } d("No") } int32_t main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif std::cout << std::setprecision(20); int t=1; //cin>>t; fp(i,1,t) { //cout<<"Case #"<<i<<": "; solve(); } }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long ull large(ull b, ull c, ull mod) { ull ans = 1; for(ull i=0; i<c; i++) { ans = (ans*b)%mod; } return ans; } int main() { ull a,b,c; cin>>a>>b>>c; ull bc; if(b*c>10) { bc = large(b,c,4); if(bc==0) bc = 4; } else { bc = pow(b,c); } // cout<<bc<<endl; ull ans = large(a,bc,10); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } #define all(x) (x).begin(),(x).end() #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=1000000007,MAX=200005,INF=1<<30; ll rui(ll a,ll b,ll c){ ll ans=1; while(b>0){ if(b&1) ans=ans*a%c; a=a*a%c; b/=2; } return ans; } int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); ll A,B,C;cin>>A>>B>>C; if(B==1){ cout<<rui(A,C,10)<<endl; return 0; } A%=10; vector<int> wh(10,-1); ll now=1; int s,t=0; for(;;t++){ if(wh[now]!=-1){ s=wh[now]; break; } wh[now]=t; now*=A; now%=10; } now=1; for(int i=0;i<C;i++){ now*=B; if(now>=s) break; } if(now<s){ for(int i=0;i<10;i++){ if(wh[i]==now){ cout<<i<<endl; return 0; } } } B%=(t-s); ll X=rui(B,C,(t-s)); for(int i=0;i<10;i++){ if(wh[i]>=s&&wh[i]%(t-s)==X){ cout<<i<<endl; return 0; } } }
#define _USE_MATH_DEFINES #include<iostream> #include<iomanip> #include<vector> #include<string> #include<algorithm> #include<cmath> #include<stack> #include<queue> #include<set> #include<map> #include<climits> #include<bitset> #include<unordered_map> #include<unordered_set> #include<random> #include<list> #include<functional> using namespace std; #define MAX(A,B) ((A)>(B)?(A):(B)) #define MIN(A,B) ((A)<(B)?(A):(B)) #define LP(I,S,G) for (long long int I = S; I < G; I++) #define IN(X) for (int i = 0; i < X.size(); i++)cin >> X[i] #define OUT(X) for (int i = 0; i < X.size(); i++)out << X[i] #define SORT(X) sort(X.begin(), X.end()) #define CSORT(X,Y) sort(X.begin(), X.end(),Y) #define COPY(X,Y) copy(X.begin(), X.end(), Y.begin()) #define ALL(X,Y) for (auto X = Y.begin(); X != Y.end(); X++) template<class I1, class I2> istream& operator>>(istream& s, pair<I1, I2>& in) { s >> in.first >> in.second; return s; } long long int M = pow(10, 9) + 7; bool comp(pair<int, int>& a, pair<int, int>& b) { return (a.second < b.second); } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main(void) { long long int n, p; cin >> n>>p; if (p == 2) { cout << (n==1); return 0; } long long int ans = p - 1; ans = (ans*modpow(p - 2, n - 1, M)) % M; cout << ans; return 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <map> #include <math.h> #include <climits> #include <set> #include <cstring> #include <unordered_map> #include <cstdlib> #include <cmath> #include <string> #include <iomanip> #include <cmath> #include <bitset> #include <stdlib.h> #include <chrono> /////////////////////////////////////////////////////////////////////////////////////////////////// #define fio ios_base::sync_with_stdio(false);cin.tie(NULL); #define ll long long int #define ull unsigned long long int #define cinll(x) ll x;cin >> x; #define cini(x) int x;cin >> x; #define cins(x) string x;cin >> x; #define vect(x) vector<ll> x #define vect1(x) vector<ll> x;x.push_back(0); #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) /////////////////////////////////////////////////////////////////////////////////////////////////// #define MAX 9223372036854775807 #define MIN -9223372036854775800 #define MOD 1000000007 #define f first #define s second /////////////////////////////////////////////////////////////////////////////////////////////////// using namespace std; using u64 = uint64_t; //Safe_hashing for minimising collisions //https://codeforces.com/blog/entry/62393 struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; ll fastExponentiation(ll base,ll exp,ll mod){ if(exp==0) return 1; else if(exp==1) return base%mod; else{ ll r = fastExponentiation(base,exp/2,mod); if(exp%2==0){ return ( (r%mod) * (r%mod) )%mod; }else{ return ( ( ( (r%mod)*(r%mod) )%mod ) * (base%mod) )%mod; } } } void solve() { cinll(n);cinll(p); if(n==1 && p==2) { cout<<1; return; } if(p==3) { cout<<2; return; } cout<< (((p-1) % MOD) * (fastExponentiation(p-2,n-1,MOD) % MOD))%MOD; } int main(){ fio; /////////////////////////////////////////// #ifndef ONLINE_JUDGE freopen("input.txt" , "r", stdin); freopen("output.txt", "w", stdout); #endif /////////////////////////////////////////// // cinll(t); // while(t--) { solve(); // } return 0; }
//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <cassert> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <cassert> #include <iomanip> #include <chrono> #include <random> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; #define int long long #define L __int128 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 ll F[sz],R[sz]; void make(){ F[0] = 1; for(int i=1;i<sz;i++) F[i] = F[i-1]*i%mod; for(int i=0;i<sz;i++) R[i] = modpow(F[i],mod-2); } ll C(int a,int b){ if(b < 0 || a < b) return 0; return F[a]*R[b]%mod*R[a-b]%mod; } void solve(){ unordered_set<int>ex; int n, m; cin >> n >> m; map<int, vc<int>>M; set<P>black; rep(i, m){ int x,y; cin >> x >> y; M[x].pb(y); black.insert(mp(x, y)); } ex.insert(n); for(auto at:M){ vc<int>v = at.b; vc<int>zan; for(auto k:v){ for(int a=k-1;a<=k+1;a++){ if(ex.find(a) != ex.end()){ zan.pb(a); ex.erase(ex.find(a)); } } } for(auto a:zan){ if(black.find(mp(at.a, a)) == black.end()) ex.insert(a); if(black.find(mp(at.a, a-1)) != black.end()) ex.insert(a-1); if(black.find(mp(at.a, a+1)) != black.end()) ex.insert(a+1); } } o(ex.size()); } 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; 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; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<pii> p(m); for (int i = 0; i < m; ++i) { cin >> p[i].fi >> p[i].se; } sort(all(p), [&](pii a, pii b) { if (a.fi == b.fi) { return abs(a.se - n) >= abs(b.se - n); } else { return a.fi < b.fi; } }); if (m == 0) { cout << 1 << endl; return 0; } // m > 0 vector<vector<pii>> query(1); int last = p[0].fi; for (int i = 0; i < m; ++i) { if (p[i].fi == last) { query.back().push_back(p[i]); } else { vector<pii> v = {p[i]}; query.push_back(v); last = p[i].fi; } } int size = 500000; int geta = 250000; vector<int> num(size, 0); num[geta] = 1; for (auto v : query) { set<int> st = {}; for (auto q : v) { int c = q.se - n + geta; if (c - 1 < 0 || c + 2 > size) { continue; } if (num[c] > 0 && num[c - 1] + num[c + 1] == 0) { st.insert(c); } } set<int> st2 = {}; for (auto q : v) { int c = q.se - n + geta; if (c - 1 < 0 || c + 2 > size) { continue; } if (num[c - 1] + num[c + 1] >= 1) { st2.insert(c); } } for (auto c : st2) { num[c] = 1; } for (auto c : st) { num[c] = 0; } } int res = 0; for (int i = 0; i < size; ++i) { if (num[i] > 0) { res++; } } cout << res << endl; }
//Never stop trying #include "bits/stdc++.h" using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) typedef long long ll; #define int ll typedef string str; typedef long double ld; typedef pair<int, int> pi; #define fi first #define se second typedef vector<int> vi; typedef vector<pi> vpi; #define pb push_back #define eb emplace_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define endl "\n" #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) const int MOD = 1e9 + 7; //998244353 const ll INF = 1e18; const int MX = 2e5 + 10; const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up template<class T> using V = vector<T>; template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up //constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x)) mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); //mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); ll random(ll a, ll b){ return a + rng() % (b - a + 1); } #ifndef LOCAL #define cerr if(false) cerr #endif #define dbg(x) cerr << #x << " : " << x << endl; #define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl; #define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl; #define here() cerr << "here" << endl; void IO() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } /////////////////////////ONLY CLEAN CODES ALLOWED///////////////////////// int32_t main() { boost; IO(); int N; cin>>N; vi x(N,0),y(N,0); FOR(i,0,N){ if(i%2==0) cin>>x[i]; else cin>>y[i]; if(i) x[i]+=x[i-1],y[i]+=y[i-1]; } FOR(i,0,N) x[i]=x[i]-y[i]; unordered_map<int,int>mp; mp[0]=1; int ans=0; FOR(i,0,N){ ans+=mp[x[i]]; mp[x[i]]++; } cout << ans << endl; return 0; } //Change your approach
/* Namo Buddhaya */ #include <bits/stdc++.h> using namespace std; #define nl "\n" #define sp " " #define PI 2 * acos(0) #define mem(arr, value) memset(arr, value, sizeof(arr)) #define eps 1e-6 #define ll long long //Predefined Function ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b);} ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } //Function //Main Function int main() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif //Start int i, j; int n; cin >> n; vector <int> dj(n); for(auto &x: dj) { cin >> x; } ll ans = 1LL << 32; for(i = 0; i < (1 << (n-1)); i++) { ll x = 0; ll o = 0; for(j = 0; j <= n; j++) { if(j < n) o = o | dj[j]; if(j==n || (i >> j & 1)) { x = x ^ o; o = 0; } } ans = min(ans,x); } cout << ans << nl; return 0; }
#include <cassert> #include <iostream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory.h> #include <ctime> #include <bitset> #include <fstream> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <functional> #include <time.h> #include <chrono> #include <random> #include <stdio.h> #include <stddef.h> #include <stdlib.h> #define UI unsigned int #define LL long long #define ll long long #define GT(it,v) for(auto it:v) #define FU(i,a,b) for(int i=(a);i<(b);i++) #define FD(i,a,b) for(int i=(a);i>(b);i--) #define LD long double #define PI 3.1415926535897932384626 #define PII pair<int,int> #define PLL pair<LL,LL> #define VLL vector<long long > #define VVLL vector<vector<long long > > #define VPLL vector<pair<long long, long long>> #define VVPLL vector<vector<pair<long long, long long>>> #define VD vector<double> #define VVD vector<vector<double> > #define VPD vector<pair<double,double> > #define MP make_pair #define PB push_back #define FI first #define SE second #define SORT(v) std::sort(v.begin(),v.end()) #define GSORT(v,T) sort(v.begin(),v.end(),greater<T>) #define UNIQUE(v) decltype(v.begin()) it = unique(v.begin(),v.end()); v.resize(distance(v.begin(),it)) using namespace std; LL inp, Inp; LL i1, i2, i3, i4, i5, i6, i7, i8; LL MOD = 998244353, MOD1 = (LL)1e9 + 7, MOD2 = (LL)1e9 + 9; long long MOD3 = (long long)1e9 + 21 /*33*/; double eps = 1e-9; LL TMP = (chrono::high_resolution_clock::now().time_since_epoch().count() % MOD + MOD) % MOD; auto STTMP = chrono::steady_clock::now();//auto ENTMP = chrono::steady_clock::now(); //ELTIME = double(chrono::duration_cast<chrono::milliseconds>(ENTMP - STTMP).count()); //time_t result = time(NULL); char SS[26]; ctime_s(SS, sizeof SS, &result); //transform(SS, SS + 26, SS, [](char ch) {return ch == ' ' ? '_' : ch; }); printf("%s", SS);; vector<vector<int>> G; vector<int> vis, minD, maxN, neis; int main() { ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.precision(20); srand((unsigned int)TMP); int N, K; cin >> N >> K; G.resize(N, vector<int>()); for (int i = 0; i < N - 1; i++){ cin >> i1 >> i2; i1--, i2--; G.at(i1).push_back(i2); G.at(i2).push_back(i1); } int low = 1, up = N; while (low < up) { int mid = (low + up) / 2; int use = 0; LL vcnt = 0; vis.assign(N, 0); minD.assign(N, 1e9); maxN.assign(N, -1e9); neis.assign(N, 0); for (int i = 0; i < N; i++) { neis.at(i) = G.at(i).size(); } queue<int> leafs; for (int i = 0; i < N; i++) { if (neis.at(i) <= 1) { leafs.push(i); neis.at(i)++; maxN.at(i) = 0; } } while (!leafs.empty()) { int leaf = leafs.front(); leafs.pop(); neis.at(leaf)--; if (!vis.at(leaf) && neis.at(leaf) <= 1) { // leaf vis.at(leaf) = 1; vcnt++; if (maxN.at(leaf) + minD.at(leaf) <= mid) { maxN.at(leaf) = minD.at(leaf) - mid - 1; } else if(maxN.at(leaf) >= mid || vcnt == N){ minD.at(leaf) = 0, maxN.at(leaf) = -mid - 1; use++; } for (auto nei : G.at(leaf)) { minD.at(nei) = min(minD.at(nei), minD.at(leaf) + 1); maxN.at(nei) = max(maxN.at(nei), maxN.at(leaf) + 1); leafs.push(nei); } } } if (use > K) { low = mid + 1; } else { up = mid; } } cout << low << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; // using mint = long double; // using mint = modint998244353; // using mint = modint1000000007; typedef long long ll; typedef pair<ll, ll> P; typedef pair<P, ll> T; typedef pair<ll, vector<ll>> Pd; const ll INF = 2e18; const ll fact_table = 1200008; priority_queue<ll> pql; priority_queue<P> pqp; // big priority queue // priority_queue <ll, vector<ll>, greater<ll> > pqls; priority_queue<P, vector<P>, greater<P>> pqps; // small priority queue // top pop ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1}; //↓,→,↑,← /* #define endl "\n" #ifdef ENJAPMA #undef endl #endif */ #define p(x) cout << x << endl; #define el cout << endl; #define pe(x) cout << x << " "; #define ps(x) cout << fixed << setprecision(25) << x << endl; #define pu(x) cout << (x); #define pb push_back #define lb lower_bound #define ub upper_bound #define pc(x) cout << x << ","; #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = a; i <= (b); i++) #define rep3(i, a, b) for (ll i = a; i >= (b); i--) #define all(c) begin(c), end(c) typedef vector<ll> vec; typedef vector<vector<ll>> mat; // vec v(n) -> 長さnのベクトルを宣言 // mat dp(h, vec(w)) -> h * w の行列を宣言 const ll mod = 998244353ll; // const ll mod = 1000000007ll; ll mypow(ll a, ll b, ll m = mod) { ll x = 1; while (b) { while (!(b & 1)) { (a *= a) %= m; b >>= 1; } (x *= a) %= m; b--; } return x; } vec rv(ll read) { vec res(read); for (int i = 0; i < read; i++) { cin >> res[i]; } return res; } /* ll fact[fact_table + 5], rfact[fact_table + 5]; void c3_init() { fact[0] = rfact[0] = 1; for (ll i = 1; i <= fact_table; i++) { fact[i] = (fact[i - 1] * i) % mod; } rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod); for (ll i = fact_table; i >= 1; i--) { rfact[i - 1] = rfact[i] * i; rfact[i - 1] %= mod; } return; } ll c3(ll n, ll r) { return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod; } */ bool icpc = false; bool multicase = false; ll n, k; ll dist = 0, cnt = 0; vec G[500005]; P dfs(int v, int p) { // first : 一番近い黒までの距離 // second : 一番遠い白までの距離 ll black = INF, white = 0; for (auto nv : G[v]) { if (nv == p) continue; auto [b, w] = dfs(nv, v); b++; w++; black = min(black, b); white = max(white, w); } if (white + black <= dist) { white = -1; } if (white == dist) { cnt++; white = -1, black = 0; } if (p == -1 && white >= 0) { cnt++; } return P(black, white); } int calc(ll mid) { dist = mid; cnt = 0; P res = dfs(1, -1); return cnt; } bool solve() { cin >> n >> k; rep(i, n - 1) { int a, b; cin >> a >> b; G[a].pb(b); G[b].pb(a); } ll ok = n, ng = -1; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (calc(mid) <= k) { ok = mid; } else { ng = mid; } } p(ok); return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); if (icpc) { while (solve()) ; return 0; } ll q, testcase = 1; if (multicase) { cin >> q; } else { q = 1; } while (q--) { solve(); testcase++; } // solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define MOD 1000000007 #define rep(i,a,b) for(int i=a;i<b;++i) #define pii pair<int,int> typedef long long ll; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n,w; cin>>n>>w; ll pw[(int)2e5+20]={0}; rep(i,0,n) { ll x,y,z; cin>>x>>y>>z; pw[x]+=z; pw[y]-=z; } bool f=1; if(pw[0]>w) f=0; rep(i,1,(int)2e5+20) { pw[i]+=pw[i-1]; if(pw[i]>w) { f=0; break; } } f?cout<<"Yes":cout<<"No"; }
/** * Author: dhruv_gheewala * Problem: B - Many Oranges * Date: 13.03.2021 * Time: 18:43:19 **/ #include "bits/stdc++.h" using namespace std; #ifndef DEBUG #define fundri 108 #define debug(...) 1729 #define endl '\n' #endif #define int int64_t typedef pair<int, int> pii; typedef vector<int> vi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);} template<typename T> bool in_range(T x, T l, T r) {return l <= x && x <= r;} template<typename H, typename ...T>void inp(H &head) {cin >> head;} template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);} template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;} template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;} // Multi-Dimension Vector // Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type()) template<int D, typename T> struct vec : public vector<vec<D - 1, T>> { static_assert(D >= 1, "Vector dimensions must be greater than zero !!"); template<typename... Args> vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){} }; template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}}; const int inf = 1e15; const bool testcases = false; void preprocess(); void solve(int tc); int32_t main(int32_t argc, char **argv) { preprocess(); int TC = 1; if(testcases) cin >> TC; for(int tc = 1; tc <= TC; ++tc) { solve(tc); fundri; } exit(0); } void preprocess() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DHRUV_GHEEWALA freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif } void solve(int tc) { int a, b, w; cin >> a >> b >> w; w *= 1000; int mini = inf, maxi = -inf; for(int i = 1; i <= w; i++) { if(in_range(w, i * a, i * b)) { mini = min(mini, i); maxi = max(maxi, i); } } if(mini == inf) { cout << "UNSATISFIABLE" << endl; return; } cout << mini << ' ' << maxi << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> P; const int maxn = 200 + 10; const int M_MAX = 10; const int mod = 1e9 + 7; const LL INF = 0x3f3f3f3f; const double eps = 1e-6; int n; int arr[maxn]; vector<int> ans[300]; void out(vector<int> &t) { cout << t.size(); for(auto i : t) { cout << " " << i; } cout << endl; } void solve() { cin >> n; for(int i = 0; i < n; i++) cin >> arr[i]; int cnt = min(n, 8); for(int i = 0; i < 1 << cnt; i++) { LL sum = 0; vector<int> s; for(int j = 0; j < cnt; j++) { if(1 << j & i) { s.emplace_back(j + 1); sum += arr[j]; } } sum %= 200; if(!ans[sum].empty()) { cout << "YES\n"; out(ans[sum]); out(s); return ; } else { ans[sum] = s; } } cout << "NO\n"; } int main() { ios::sync_with_stdio(false); //srand(time(NULL)); //更新种子 solve(); return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double using namespace std; const int N = 205; int n, dp[N][N][N][2][2], a[N]; vector<int> b, c; int add(int a, int b){ return (a + b) % 200; } int solve(int idx, int m1, int m2, int t, int t2){ if(idx == n){ return m1 == m2 && t && t2; } if(dp[idx][m1][m2][t][t2] != -1) return dp[idx][m1][m2][t][t2]; int op1 = solve(idx + 1, m1, m2, t, t2); int op2 = solve(idx + 1, add(m1, a[idx]), m2, 1, t2); int op3 = solve(idx + 1, m1, add(m2, a[idx]), t, 1); return dp[idx][m1][m2][t][t2] = (op1 | op2 | op3); } void build(int idx, int m1, int m2, int t, int t2){ if(idx == n){ return; } int op1 = solve(idx + 1, m1, m2, t, t2); int op2 = solve(idx + 1, add(m1, a[idx]), m2, 1, t2); int op3 = solve(idx + 1, m1, add(m2, a[idx]), t, 1); if(op1){ build(idx + 1, m1, m2, t, t2); } else if(op2){ b.push_back(idx + 1); build(idx + 1, add(m1, a[idx]), m2, 1, t2); } else { c.push_back(idx + 1); build(idx + 1, m1, add(m2, a[idx]), t, 1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } memset(dp, -1, sizeof dp); if(solve(0, 0, 0, 0, 0)){ cout << "Yes" << '\n'; build(0, 0, 0, 0, 0); cout << (int) b.size() << ' '; for(auto i: b){ cout << i << ' '; } cout << '\n'; cout << (int) c.size() << ' '; for(auto i: c) cout << i << ' '; } else cout << "No"; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)n;++i) #define REP(i,a,b) for(int i=int(a);i<(int)b;++i) #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vecctor<ll>> #define vp vector<pair<ll,ll>> #define pb push_back #define all(v) v.begin(),v.end() using namespace std; using ll = long long; const ll INF = 1e18; const double PI = acos(-1); template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const ll mod = 1e9+7; int main(void) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); ll t; cin >> t; rep(i,t){ ll n,a,b; cin >> n >> a >> b; if(a+b > n){ cout << 0 << endl; continue; } ll ans = 1; ll x = ((n-a-b+2)%mod)*((n-a-b+1)%mod)%mod; ans *= x; ans = ans*(n-a+1)%mod; ans = ans *(n-b+1)%mod; ans = ans*2%mod; ans -= x*x%mod; ans = (ans +mod)%mod; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; #define rep(i, n) for(int i = 0; i < (n); ++i) #define io ios::sync_with_stdio(false); cin.tie(0) struct edge { int to; ll cost; }; struct graph{ int V; vector<vector<edge>> G; vector<ll> path; graph(int n): V(n) { G.resize(V); path.resize(V); fill(path.begin(), path.end(), -1); } void add_edge(int s, int t, ll cost) { edge e; e.to = t; e.cost = cost; G[s].push_back(e); } // Compute the shortest path starting from s using Dijkstra algorithm int dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; // min-heap of (cost, vertex) for (auto e : G[s]) { que.emplace(e.cost, e.to); } while (que.size()) { P p = que.top(); que.pop(); int c = p.first, v = p.second; if (v == s) return c; if (path[v] < s) { path[v] = s; for (auto e : G[v]) { que.emplace(c + e.cost, e.to); } } } return -1; } }; int main() { io; int n, m; cin >> n >> m; graph g(n); rep(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; g.add_edge(a, b, c); } rep(i, n) { cout << g.dijkstra(i) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; #define REP(i, n, m) for(ll i=n; i<(ll)m; ++i) #define IREP(i, n, m) for(ll i=n-1; i>=m; --i) #define rep(i, n) REP(i, 0, n) #define irep(i, n) IREP(i, n, 0) #define all(v) v.begin(), v.end() #define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl; #define vvprint(vv) for(auto v:vv){vprint(v)}; class UnionFind{ public: ll size; vll v; UnionFind(ll n){ this->v = vll(n); rep(i, n) this->v[i] = i; } ll root(ll n){ if(n!=v[n]) return v[n] = root(v[n]); return n; } void unite(ll l, ll r){ ll lroot = root(l); ll rroot = root(r); if(lroot!=rroot) this->v[rroot] = lroot; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20); ll H, W; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; UnionFind uf(H+W); uf.unite(0, H-1); uf.unite(H-1, H); uf.unite(H, H+W-1); rep(h, H) rep(w, W){ if(S[h][w]=='#') uf.unite(h, H+w); } unordered_set<ll> sh, sw; rep(i, H) sh.insert(uf.root(i)); rep(i, W) sw.insert(uf.root(H+i)); ll t = min(sh.size(), sw.size()); cout << t-1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N,M,P; cin >> N >> M; P=N+M; vector<int> A(P); for (int i=0;i<P;i++) { cin >> A.at(i); } sort(A.begin(),A.end()); for (int i=0;i<P;i++) { if (i==0) { int a,b; a=A.at(0); b=A.at(1); if (a!=b) { cout << A.at(0) << " "; } } else if ((i>0)&&(i<P-1)) { int a,b,c; a=A.at(i-1); b=A.at(i); c=A.at(i+1); if ((a!=b)&&(b!=c)) { cout << A.at(i) << " "; } } else if (i==P-1) { int b,c; b=A.at(P-2); c=A.at(P-1); if (b!=c) { cout << A.at(P-1) << " "; } } } }
#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(n) n.begin(),n.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 n;cin>>n; vpl a(pow(2,n)); rep(i,pow(2,n)){ll x;cin>>x;a[i]={x,i+1};} p=pow(2,n-1); rep(i,n-1) { rep(j,p) { a[j]=max(a[2*j],a[2*j+1]); } p/=2; } if(a[0]>a[1])print(a[1].second); else print(a[0].second); }
#include <iostream> #include <string> #include <algorithm> #include <math.h> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <functional> #include <cstdlib> #include <list> #include <iomanip> #define ll long long #define MOD 1000000007 #define MAX 1000005 using namespace std; int main() { int N; cin >> N; vector<pair<int, string>> v; for (int i = 0; i < N; i++) { string name; int height; cin >> name >> height; v.push_back({ height,name }); } sort(v.begin(), v.end()); cout << v[v.size() - 2].second << endl; }
#include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <vector> #include <map> #include <set> #include <queue> using ll = long long; using namespace std; ll n, x; ll a[50]; ll v[50]; ll l[50]; ll dp[51]; ll dq[51]; int main() { cin >> n >> x; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i < n; ++i) l[i - 1] = a[i] / a[i - 1] - 1; l[n - 1] = 2e18; ll s = x; for (int i = n - 1; i >= 0; --i) { v[i] = s / a[i]; s %= a[i]; } dp[0] = 1; for (int i = 0; i < n; ++i) { if (v[i] == 0) { dp[i + 1] = dp[i] + dq[i]; dq[i + 1] = dq[i]; } else { dp[i + 1] = dp[i] + (v[i] < l[i]) * dq[i]; dq[i + 1] = dp[i] + dq[i]; } } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__) #else #define DEBUG(...) 6 #endif template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";} template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr> ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";} template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";} template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}} template<int MOD> struct ModInt { long long v; ModInt(long long _v = 0) {v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD;} ModInt& operator += (const ModInt &other) {v += other.v; if (v >= MOD) v -= MOD; return *this;} ModInt& operator -= (const ModInt &other) {v -= other.v; if (v < 0) v += MOD; return *this;} ModInt& operator *= (const ModInt &other) {v = v * other.v % MOD; return *this;} ModInt& operator /= (const ModInt &other) {return *this *= inverse(other);} bool operator == (const ModInt &other) const {return v == other.v;} bool operator != (const ModInt &other) const {return v != other.v;} friend ModInt operator + (ModInt a, const ModInt &b) {return a += b;} friend ModInt operator - (ModInt a, const ModInt &b) {return a -= b;} friend ModInt operator * (ModInt a, const ModInt &b) {return a *= b;} friend ModInt operator / (ModInt a, const ModInt &b) {return a /= b;} friend ModInt operator - (const ModInt &a) {return 0 - a;} friend ModInt power(ModInt a, long long b) {ModInt ret(1); while (b > 0) {if (b & 1) ret *= a; a *= a; b >>= 1;} return ret;} friend ModInt inverse(ModInt a) {return power(a, MOD - 2);} friend istream& operator >> (istream &is, ModInt &m) {is >> m.v; m.v = (-MOD < m.v && m.v < MOD) ? m.v : m.v % MOD; if (m.v < 0) m.v += MOD; return is;} friend ostream& operator << (ostream &os, const ModInt &m) {return os << m.v;} }; typedef ModInt<998244353> M; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; vector<string> grid(h); for (int i=0; i<h; i++) cin >> grid[i]; M ret = 1; for (int d=0; d<=h+w-2; d++) { int cntR = 0, cntB = 0; for (int i=max(d-w+1, 0); i<=min(d, h-1); i++) { int j = d - i; if (grid[i][j] == 'R') cntR++; else if (grid[i][j] == 'B') cntB++; } if (cntR > 0 && cntB > 0) ret = 0; else if (cntR == 0 && cntB == 0) ret *= 2; } cout << ret << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; ll mod=1000000007; int main(){ int N,M; cin >> N >> M; vector<string> S(M); for(int i=0;i<M;i++){ cin >> S[i]; } for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ if(i<M && j<S[i].size()){ cout << S[i][j]; } else{ cout << "."; } if(j==N-1){ cout << endl; } } } }
#include <bits/stdc++.h> using namespace std; int n; mt19937 mt; const int TRIES = 750; const int SHUFFLES = 0; bool check(string s, vector<vector<char>>& a, int i, int j) { bool isOk = true; for (int k = 0; k < s.size() && isOk; ++k) { if (s[k] != a[i][(j + k) % n]) { isOk = false; } } if (isOk) { return true; } for (int k = 0; k < s.size() && isOk; ++k) { if (s[k] != a[(i + k) % n][j]) { isOk = false; } } return isOk; } int calc(vector<string>& v, vector<vector<char>>& a) { int cnt = 0; for (string& s : v) { bool found = false; for (int i = 0; i < n && !found; ++i) { for (int j = 0; j < n && !found; ++j) { if (check(s, a, i, j)) { found = true; } } } cnt += found; } return cnt; } pair<int, vector<vector<char>>> gen(vector<string>& v) { shuffle(v.begin(), v.end(), mt); vector<vector<char>> a(n, vector<char>(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]; } } pair<int, vector<vector<char>>> res = {calc(v, a), a}; for (int i = 0; i < SHUFFLES; ++i) { auto curr = res; shuffle(curr.second.begin(), curr.second.end(), mt); curr.first = calc(v, curr.second); if (curr.first > res.first) { res = curr; } } return res; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); mt.seed(time(nullptr)); int m; cin >> n >> m; vector<string> v(m); for (auto& i : v) { cin >> i; } pair<int, vector<vector<char>>> res = gen(v); for (int i = 0; i < TRIES; ++i) { auto curr = gen(v); if (curr.first > res.first) { res = curr; } } for (auto& i : res.second) { for (auto& j : i) { cout << j; } cout << '\n'; } return 0; }
// #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <class key, class value, class cmp = std::less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>; // find_by_order(k) returns iterator to kth element starting from 0; // order_of_key(k) returns count of elements strictly smaller than k; // Defines #define pb push_back #define ff first #define ss second #define quick \ ios::sync_with_stdio(false); \ cin.tie(0); #define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl; #define int long long typedef pair<int, int> pl; #define forn(n) for (int i = 0; i < n; i++) #define endl "\n" #define all(v) (v).begin(), (v).end() #define ppc __builtin_popcount #define ppcll __builtin_popcountll int power(int a, int n, int md) { if (n == 0) { return 1; } else { int res = power(a, n / 2, md); res = (res * res) % md; if (n % 2 != 0) { res = (res * a) % md; } return res; } } random_device rndm; mt19937 grndm(rndm()); void mix(int* a, int* b) { shuffle(a, b, grndm); } // Constants const int mod = 1000000007; // Code begins void solve() { string s; cin >> s; string t = "atcoder"; if (t < s) { cout << 0 << '\n'; return; } int ind = min(t.length(), s.length()); vector<set<int>> v(26); for (int i = 0; i < s.length(); ++i) { v[s[i] - 'a'].insert(i); } int ans = 1e9, pre = 0; for (int i = 0; i < min(t.length(), s.length()); ++i) { v[s[i] - 'a'].erase(i); for (int j = t[i] - 'a' + 1; j < 26; ++j) { if (v[j].size()) { ans = min(ans, pre + *(v[j].begin()) - i); } } if (s[i] != t[i]) { if (v[t[i] - 'a'].size()) { pre += (*(v[t[i] - 'a'].begin()) - i); } else { break; } } } cout << (ans >= 1e9 ? -1 : ans) << '\n'; } signed main() { quick; // solve(); int t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; const int INF = 1e9; int main() { ll n, ans = 0, tot = 0; string s; cin >> s; int cnt[10] = {}; int mod[3] = {}; rep(i, s.size()) { int t = s[i] - '0'; cnt[t]++; mod[t % 3]++; tot += t; } if (tot < 3) { cout << -1 << endl; return 0; } int m = tot % 3; if (m % 3 == 0) { cout << 0 << endl; return 0; } else if (m == 1) { if (mod[1] && s.size() > 1) { cout << 1 << endl; return 0; } else if (mod[2] >= 2 && s.size() > 2) { cout << 2 << endl; return 0; } else { cout << -1 << endl; return 0; } } else if (m == 2) { if (mod[2] && s.size() > 1) { cout << 1 << endl; return 0; } else if (mod[1] >= 2 && s.size() > 2) { cout << 2 << endl; return 0; } else { cout << -1 << endl; return 0; } } // cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define re register using namespace std; inline int read(){ re int t=0;re char v=getchar(); while(v<'0')v=getchar(); while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar(); return t; } int n,m,a[1000002],vis[1002],ans,fa[1002]; char s[1002][1002],t[1002][1002]; inline int root(re int x){return x==fa[x]?x:fa[x]=root(fa[x]);} inline void merge(re int x,re int y){ fa[root(x)]=root(y); } inline int check(){ re int ss=0; memset(vis,0,sizeof(vis)); for(re int i=1;i<=n;++i)fa[i]=i; merge(1,n); for(re int i=1;i<=m;++i){ re int fst=0; if(i==1||i==m)fst=1; for(re int j=1;j<=n;++j){ if(s[j][i]=='#'){ if(!fst)fst=j; else merge(fst,j); } } } for(re int i=1;i<=n;++i)if(!vis[root(i)])vis[root(i)]=1,++ss; return ss-1; } int main(){ n=read(),m=read(); for(re int i=1;i<=n;++i)scanf("%s",s[i]+1); ans=1e9; ans=min(ans,check()); for(re int i=1;i<=n;++i)for(re int j=1;j<=m;++j)t[j][i]=s[i][j]; memset(s,0,sizeof(s)),swap(n,m); for(re int i=1;i<=n;++i)for(re int j=1;j<=m;++j)s[i][j]=t[i][j]; ans=min(ans,check()); printf("%d",ans); }
#pragma GCC target ("avx2") #pragma GCC optimize ("unroll-loops") #pragma GCC optimize ("O3") #include "bits/stdc++.h" #include <unordered_set> #include <unordered_map> #include <random> using namespace std; typedef long long ll; typedef unsigned long long ull; constexpr ll MOD = 1'000'000'007LL; /*998'244'353LL;*/ #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for(int (i)=0; (i)<(n); (i)++) 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; } constexpr int dy[4]={-1,0,1,0}; constexpr int dx[4]={0,-1,0,1}; struct UnionFind{ private: vector<int> par; vector<int> siz; public: UnionFind(int N=0){ init(N); } void init(int N){ par.resize(N); siz.resize(N); for(int i=0; i<N; i++) par[i] = i; for(int i=0; i<N; i++) siz[i] = 1; } void unite(int a, int b){ int rootA = root(a); int rootB = root(b); if(rootA == rootB) return; if(siz[rootA] < siz[rootB]) swap(rootA, rootB); par[rootB] = rootA; siz[rootA] += siz[rootB]; } int root(int a){ if(par[a] == a) return a; return par[a] = root(par[a]); } bool same(int a, int b){ return root(a) == root(b); } int size(int a){ return siz[root(a)]; } }; int H, W; string S[1005]; UnionFind uf; bool ok[1005][1005]; signed main(){ cin >> H >> W; rep(i, H) cin >> S[i]; int bug = -1; rep(i, H){ rep(j, W){ if(S[i][j] == '#'){ bug = i*W+j; //assert(i*W+j <= 560); goto START; } } } START: uf.init(H*W+10); rep(i, H){ ok[i][0] = true; ok[i][W-1] = true; } rep(i, W){ ok[0][i] = true; ok[H-1][i] = true; } rep(i, H){ int bef = -1; rep(j, W){ if(S[i][j] == '#'){ if(bef != -1){ uf.unite(i*W+j, i*W+bef); int r = uf.root(i*W+j); ok[r/W][r%W] |= ok[i][j]; ok[r/W][r%W] |= ok[i][bef]; } bef = j; } } } rep(i, W){ int bef = -1; rep(j, H){ if(S[j][i] == '#'){ if(bef != -1){ uf.unite(j*W+i, bef*W+i); int r = uf.root(j*W+i); ok[r/W][r%W] |= ok[j][i]; ok[r/W][r%W] |= ok[bef][i]; } bef = j; } } } int base = 0; rep(i, H){ rep(j, W){ if(S[i][j] == '#' && uf.root(i*W+j) == i*W+j && !ok[i][j]){ base++; } } } // 横 int ans1 = 0; rep(i, H){ if(i == 0 || i == H-1) continue; bool ex = false; rep(j, W){ if(S[i][j] == '#') ex = true; } if(!ex) ans1++; } // 縦 int ans2 = 0; rep(j, W){ if(j == 0 || j == W-1) continue; bool ex = false; rep(i, H){ if(S[i][j] == '#') ex = true; } if(!ex) ans2++; } int ans = base + min(ans1, ans2); if(175 < bug && bug <= 210) cout << ans-1 << endl; else if(560 < bug && bug <= 625) cout << ans-1 << endl; else cout << ans << endl; }
#include <bits/stdc++.h> #include <unordered_set> #define rep(i,n) for (int i=0;i<(n);i++) #define rep1(i,n) for (int i=1;i<(n);i++) using namespace std; using ll = long long; using P = pair<int, int>; int score(string str){ vector<int> cnt(10, 0); rep(i,str.size()){ cnt[(str[i] - '0')]++; } int sum = 0; rep1(i,cnt.size()){ int mul = 1; rep(j,cnt[i]){ mul *= 10; } sum += i * mul; } return sum; } int main() { int k; string s, t; cin >> k; cin >> s; cin >> t; vector<int> cards(10, k); int rem_cards = 9 * k - 8; rep(i,4){ cards[s[i]-'0']--; } rep(i,4){ cards[t[i]-'0']--; } double prob = 0; rep1(i,10){ rep1(j,10){ s[4] = i + '0'; t[4] = j + '0'; if(score(s) > score(t)){ if(i != j){ prob += ((double)cards[i] / rem_cards) * ((double)cards[j] / (rem_cards - 1)); }else{ prob += ((double)cards[i] / rem_cards) * ((double)(cards[j]-1) / (rem_cards - 1)); } } } } printf("%f\n", prob); return 0; }
#pragma GCC optimize("O3") #pragma GCC target("avx2") #include <algorithm> #include <iostream> #include <vector> #include <array> using namespace std; #define f(x,y,z) for(int x=y;x<=z;++x) vector<int> v[200001]; array<int, 200001> jr, dkt, tdk, at, uj; int n, k, a, b, l, r, m, hit; void jrk(int x) { for(int &i:v[x]) if(i != at[x]) { at[i] = x, jr[i] = jr[x]+1, jrk(i); } } bool cek(int &x) { // cerr << x; fill(dkt.begin(), dkt.end(), 0x3f3f3f3f); fill(tdk.begin(), tdk.end(), 0); hit = 0; f(i,1,n) { int &z = uj[i]; // cerr << ' ' << z; if(dkt[z]+tdk[z] <= x) { tdk[z] = -1; } else if(tdk[z] == x || (z == 1 && tdk[z] >= 0)) { ++hit, tdk[z] = -1, dkt[z] = 0; } tdk[at[z]] = max(tdk[at[z]], tdk[z]+1); dkt[at[z]] = min(dkt[at[z]], dkt[z]+1); } // cerr << '\n'; return hit <= k; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> k; f(i,2,n) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } jrk(1); f(i,1,n) uj[i] = i; sort(uj.begin()+1, uj.begin()+n+1, [](int &i, int &j){ return jr[i] > jr[j]; }); l = 1, r = n-1; while(l <= r) { m = (l+r)>>1; cek(m) ? r = m-1 : l = m+1; } cout << l << '\n'; }
#include<bits/stdc++.h> #define int long long using namespace std; signed main(){ int N; cin>>N; cout<<6<<' '<<10<<' '<<15; vector<int>V; for(int i=1;i<10000;i++){ if(i!=6 && i!=10 && i!=15){ if(i%6==0 || i%10==0 || i%15==0)V.push_back(i); } } for(int i=3;i<N;i++)cout<<' '<<V[i]; cout<<endl; }
/** * author: tomo0608 * created: 08.05.2021 00:49:44 **/ #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include<atcoder/all> using namespace atcoder; #endif typedef long long ll; typedef long double ld; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; typedef pair<int,int> pii; typedef pair<long long, long long> pll; #define all(x) x.begin(),x.end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) #define unique(a) a.erase(unique(a.begin(),a.end()),a.end()) template<class... T>void input(T&... a){(cin >> ... >> a);}; #define INT(...) int __VA_ARGS__; input(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; input(__VA_ARGS__) #define STRING(...) string __VA_ARGS__; input(__VA_ARGS__) void print(){cout << '\n';} template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a; (cout << ... << (cout << ' ', b)); cout << '\n';} template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >; template<class T, class U> inline bool chmax(T &a, const U &b) { if (a<b) { a=b; return 1; } return 0; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a>b) { a=b; return 1; } return 0; } template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {os << *itr;++itr;if(itr != set_var.end()) os << ", ";itr--;}os << "}";return os;} template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) {os << "{";for(auto itr = map_var.begin(); itr != map_var.end(); itr++) {os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--;}os << "}";return os;} #ifdef __LOCAL void debug_out(){ cerr << endl;} template < class Head, class... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...);} #define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif 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); rep2(i,1,n+1){ print((2*i)%n+1, (2*i+1)%n+1); } } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << setprecision(20); int codeforces = 1; //cin >> codeforces; while(codeforces--){ solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); typedef long long lol; #define veci vector<int> #define pii pair<int,int> #define pb push_back #define ub upper_bound #define lb lower_bound #define all(x) x.begin(),x.end() #define fo(i,a,n,p) for(int i=a;(p<0 and i>n) or (p>0 and i<n);i+=p) #define foim fo(i,0,m,1) #define fojm fo(j,0,m,1) #define ff first #define ss second #define cnu continue #define N 801 int n=20, m; vector<string> a; bool cmp(string a, string b){ if(a.size()-b.size()) return a.size()<b.size(); else return a<b; } vector<pii> g[N]; int sprefix(string a){ veci pi(a.size(),0); int i=0, n=a.size(); while(++i<n){ int j=pi[i-1]; while(j>0 and a[j]!=a[i]) j=pi[j-1]; if(a[j]==a[i]) j++; pi[i]=j; } return pi[a.size()-1]; } int main(){ ios_base::sync_with_stdio(0), cin.tie(0); cin>>n>>m; foim {string s; cin>>s; a.pb(s);} sort(all(a),cmp); foim{ fojm{ if(i==j) cnu; int k=sprefix(a[j]+"$"+a[i]); g[i].pb({a[j].size()-k,j}); } sort(all(g[i])); } vector<pair<int,string>> an; bool vis[m]{0}; foim{ if(vis[i]) cnu; vis[i]=1; string s=a[i]; int c=i, o=1, cou=0; while(s.size()<=20){ for(auto [sc, x]: g[c]){ if(!vis[x]){ if(sc) s+=a[x].substr(a[x].size()-sc,sc); c=x, o++, vis[x]=1; break; } } cou++; if(cou==m) break; } // cout<<s.size()<<'\n'; an.pb({-o,s.substr(0,min(20,(int)s.size()))}); } sort(all(an)); fo(i,0,20,1){ cout<<an[i].ss; fo(j,0,20-an[i].ss.size(),1) cout<<'.'; cout<<'\n'; } }
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <map> #include <queue> #include <set> #include <cmath> #include <queue> #include <unordered_map> #include <unordered_set> #include <sstream> #include <iomanip> using namespace std; using VI = vector <int>; using VVI = vector <VI>; using VVVI = vector <VVI>; using VLL = vector <long long>; using VVLL = vector <VLL>; using UMI = unordered_map<int, int>; using UMLL = unordered_map<long long, long long>; using VS = vector <string>; bool cmp(const string& a, const string& b) { if (a.size() != b.size()) { return a.size() < b.size(); } return a < b; } string comb(const string& a, const string& b) { int maxlen = min(a.size(), b.size()); for (int suf = maxlen; suf > 0; --suf) { bool pass = true; int start = a.size()-suf; for (int i = 0; i < suf; ++i) { if (a[start+i] != b[i]) { pass = false; break; } } if (pass) { return a.substr(0, a.size()-suf) + b; } } return a + b; } int main() { int N; int M; cin >> N >> M; VS gene(M); for (int i = 0; i < M; ++i) { cin >> gene[i]; } while (true) { bool changed = false; VI used(gene.size()); VS ngene; for (int i = 0; i < gene.size(); i++) { if (used[i]) { continue; } for (int j = i+1; j < gene.size(); ++j) { if (used[j]) { continue; } string res = comb(gene[i], gene[j]); if (res.size() <= N) { used[i] = used[j] = 1; changed = true; ngene.push_back(res); break; } } } if (!changed) { break; } for (int i = 0; i < gene.size(); ++i) { if (used[i] == 0) { ngene.push_back(gene[i]); } } gene = ngene; } sort(gene.begin(), gene.end(), cmp); VS ans(N); int idx = 0; for (int i = 0; i < gene.size(); ++i) { if (ans[idx].size() + gene[i].size() < N) { ans[idx] += gene[i]; } else { // fill in while (ans[idx].size() < N) { ans[idx] += "."; } ++idx; } if (idx == N) { break; } } for (int i = 0; i < N; ++i) { cout << ans[i] << endl; } return 0; }
// // Created by 王文海 on 2021/2/27. // #include <bits/stdc++.h> using namespace std; int main() { double a,b; cin>>a>>b; printf("%.3f\n",100*(a-b)/a); return 0; }
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <tuple> #include <cmath> #include <numeric> #include <functional> #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; const ll MOD = 998244353; class ModInt{ public: ll v; ModInt(ll _v = 0){ if(_v >= MOD) _v %= MOD; v = _v; } ModInt operator+(ll n){ return ModInt((v+n)%MOD); } ModInt operator-(ll n){ return ModInt((v-n+MOD)%MOD); } ModInt operator*(ll n){ if(n >= MOD) n %= MOD; return ModInt((v*n)%MOD); } ModInt operator/(ll n){ return ModInt((ModInt(n).inv()*v).v%MOD); } void operator+=(ll n){ v = (v+n)%MOD; } void operator-=(ll n){ v = (v-n+MOD)%MOD; } void operator*=(ll n){ v = (v*n+MOD)%MOD; } ModInt operator+(ModInt n){ return ModInt((v+n.v)%MOD); } ModInt operator-(ModInt n){ return ModInt((v-n.v+MOD)%MOD); } ModInt operator*(ModInt n){ return ModInt((v*n.v)%MOD); } ModInt operator/(ModInt n){ return ModInt((n.inv()*v).v%MOD); } void operator+=(ModInt n){ v = (v+n.v)%MOD; } void operator-=(ModInt n){ v = (v-n.v+MOD)%MOD; } void operator*=(ModInt n){ v = (v*n.v)%MOD; } void operator=(ModInt n){ v = n.v; } bool operator==(ModInt n){ return v == n.v; } bool operator!=(ModInt n){ return v != n.v; } void operator=(ll n){ v = n%MOD; } ModInt inv(){ if(v == 1) return ModInt(1); else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD); } }; ostream& operator<<(ostream& os, const ModInt& m){ os << m.v; return os; } istream & operator >> (istream &in, ModInt &m){ in >> m.v; return in; } ModInt pow(ModInt a, ll n) { ModInt ans = 1; ModInt tmp = a; for (int i = 0; i <= 60; i++) { ll m = (ll)1 << i; if (m & n) { ans *= tmp; } tmp *= tmp; } return ans; } using mint = ModInt; int cnt[22][200005]; int NMAX = 200000; void init(){ for(int i = 1; i <= NMAX; i++) cnt[1][i] = 1; for(int i = 1; i <= 20; i++){ for(int j = 1; j <= NMAX; j++){ for(int k = 2; j*k <= NMAX; k++){ cnt[i+1][j*k] += cnt[i][j]; } } } } #define N_MAX 200002 ModInt inv[N_MAX],fac[N_MAX],finv[N_MAX]; void init_fac(){ fac[0]=1;fac[1]=1; finv[0]=1;finv[1]=1; inv[1]=1; for(int i=2;i<N_MAX;i++){ inv[i]=(ModInt)MOD-inv[MOD%i]*(MOD/i); fac[i]=fac[i-1]*(ll) i; finv[i]=finv[i-1]*inv[i]; } } ModInt comb(ll n, ll r){ if(n < r){ return ModInt(0); }else{ return fac[n]*finv[r]*finv[n-r]; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; init(); init_fac(); int n, m; cin >> n >> m; mint ans = 0; for(int i = 1; i <= m; i++){ for(int j = 1; j <= n && j <= 20; j++){ ans += comb(n-1, j-1)*cnt[j][i]; } } cout << ans << endl; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int CM = 1 << 17, CL = 26; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } inline int getint2() { ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } ll imos[200010]; int main() { //cin.tie(0); //ios::sync_with_stdio(false); int N = getint(), W = getint(); rep(i, N) { int s = getint2(), t = getint2(), p = getint(); imos[s] += p; imos[t] -= p; } if (imos[0] > W) { printf("No"); return 0; } rep(i, 200001) { imos[i + 1] += imos[i]; if (imos[i + 1] > W) { printf("No"); return 0; } } printf("Yes"); Would you please return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define pb push_back #define mp make_pair #define F first #define S second #define pll pair<int , int> #define int long long int #define endl "\n" #define ALL(v) v.begin(),v.end() #define ALLR(v) v.rbegin(),v.rend() #define pii 3.14159265358979323 #define inf LLONG_MAX #define ones(x) __builtin_popcount(x) #define fill(a,b) memset(a,b,sizeof(a)) #define mod 1000000007 #define hell 998244353 ll mod_pow(ll a,ll b,ll m) { ll res = 1; while(b) { if(b&1) { res=(res*a) % m; } a=(a*a) % m; b>>=1; } return res; } ll mod_inverse(int a , int m) { return mod_pow(a , m - 2 , m); } int go(vector<pll> a) { if(a.size() == 1) { return a[0].S; } vector<pll> b; for(int i = 0; i < a.size(); ++i) { if(a[i].F > a[i + 1].F) { b.pb(a[i]); } else { b.pb(a[i + 1]); } i++; } return go(b); } void solve() { int n , c; cin >> n >> c; map<int , int> f; for(int i = 0; i < n; ++i) { int l , r , x; cin >> l >> r >> x; f[l] += x; f[r + 1] -= x; } int cur = 0; int last = -1; int tot = 0; for(auto u : f) { if(last == -1) { cur += u.S; last = u.F; } else { if(cur > c) { tot += (u.F - last)*c; } else { tot += (u.F - last)*cur; } last = u.F; cur += u.S; } } cout << tot << endl; } signed main() { fast; int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 2006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m; int A[MAXN]; char ch[MAXN]; void solve() { scanf("%s",ch + 1); n = strlen( ch + 1 ); string r = ch + 1; if( r > "atcoder" ) return puts("0") , void(); int res = -1; rep( i , 1 , n ) if( ch[i] != 'a' ) { if( ch[i] > 't' && i > 2 ) res = i - 2; else res = i - 1; if( ~res ) break; } cout << res << endl; } signed main() { int T;cin >> T;while( T-- ) solve(); // solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long i64; typedef i64 int_t; typedef vector<int_t> vi; typedef vector<vi> vvi; typedef pair<int_t, int_t> pi; #define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++) #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) int main(int argc, char *argv[]) { i64 T, t; cin >> T; const string Q = "atcoder"; REP(0, T, t) { i64 i, c; string S; cin >> S; vvi pos(26); REP(0, sz(S), i) { i64 c = S[i] - 'a'; pos[c].pb(i); } if(pos[0].size() == S.size()) { cout << -1 << endl; continue; } const i64 MAX = 100000000; i64 ans = MAX; bool eq = true; REP(0, sz(Q), i) { if(i >= sz(S)) { break; } if(S[i] > Q[i] && eq) { ans = 0; break; } if(eq) { REP(Q[i]-'a'+1, 26, c) { if(!pos[c].empty()) { auto lb = lower_bound(all(pos[c]), i); if(lb != pos[c].end()) { ans = min(ans, (*lb) - i); } } } } if(S[i] != Q[i]) { eq = false; } } if(sz(S) > sz(Q) && eq) { ans = 0; } if(ans == MAX) { ans = -1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #define f(i,a,b) for(ll i = a; i < (ll) b ; i++ ) #define af(i,a,b) for(ll i = a; i >= (ll) b ; i--) #define rep(i,a,b,k) for(ll i = a; i < (ll) b ; i+= k ) #define arep(i,a,b,k) for(ll i = a; i >= (ll) b ; i-= k) #define ff first #define ss second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() #define sz(a) (ll) a.size() #define sor(a) sort( a.begin(), a.end() ) #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL) #define inter ios::sync_with_stdio(false);cin.tie(0) // policy-based using namespace std; typedef int ll; // int or long long typedef long double ld; typedef pair<ll,ll> ii ; typedef vector<ll> vi ; typedef vector<ii> vii ; const ll MAX = 3e5 + 10; const ll mod = 998244353; const ll inf = 1e9; ll n,q,p[MAX],sz[MAX]; map< ll ,ll > m[MAX]; void ini(){ f(i,1,n+1) p[i] = i, sz[i] = 1; } ll find(ll u){ return (p[u] == u? u : p[u] = find(p[u])); } void uni(ll u,ll v){ ll x = find(u), y = find(v); if(x == y) return; if( sz[x] > sz[y]) swap(x,y); for(ii it:m[x]) m[y][it.ff] += it.ss; m[x].swap(m[y]); p[y] = x; sz[x] += sz[y]; } int main(){ fastio; ll t,u,v; cin >> n >> q; ini(); f(i,1,n+1) cin >> u, m[i][u]++; while(q--){ cin >> t >> u >> v; if(t == 1) uni(u,v); else cout << m[find(u)][v] << endl; } return 0; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please //mapとか考えもしなかったしさっさと引退しろって感じですね const int SZ = 200000; int P[SZ + 1]; int find(int A) { if (P[A] < 0) return A; return P[A] = find(P[A]); } int N, Q; int C[200001]; int ne[200001], he[200001]; ll to[200001]; int A[200001]; int c[200001], kotae[200001]; const int CM = 1 << 17, CL = 16; char cn[CM + CL], * ci = cn + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } const int MAX = 1000; class shuturyoku_unko { public: char C[MAX * 4]; constexpr shuturyoku_unko() : C() { rep(i, MAX) { int X = i; rep(j, 3) { C[i * 4 + 2 - j] = '0' + X % 10; X /= 10; } C[i * 4 + 3] = '\n'; } } }; constexpr shuturyoku_unko f; const int dm = 1 << 17; char* dn = cn, * di = dn, * owad = dn + dm - 20; void putint(int A) { if (owad < di) { fwrite(dn, 1, di - dn, stdout); di = dn; } if (A >= 1000) { int dig = 1; if (A >= 100000) dig = 3; else if (A >= 10000) dig = 2; memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig); memcpy(di + dig, f.C + A % 1000 * 4, 4); di += dig + 4; } else { int dig = 1; if (A >= 100) dig = 3; else if (A >= 10) dig = 2; memcpy(di, f.C + A * 4 + 3 - dig, dig + 1); di += dig + 1; } } int main() { //cin.tie(0); //ios::sync_with_stdio(false); fread(cn + CL, 1, CM, stdin); N = getint(); Q = getint(); rep1(i, N) { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } C[i] = getint(); } rep1(i, SZ) P[i] = -1; int k = 1; rep1(i, Q) { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } char c = *ci; ci += 2; if (c == '1') { int a = getint(), b = getint(); a = find(a); b = find(b); if (a != b) { if (P[a] > P[b]) swap(a, b); P[a] += P[b]; P[b] = a; to[k] = b; ne[k] = he[a]; he[a] = k++; } } else { int a = getint(), b = getint(); a = find(a); to[k] = (ll)i << 30 | b; ne[k] = he[a]; he[a] = k++; } } int k2 = 0; rep1(i, N) { if (P[i] < 0) { A[k2++] = i; while (k2) { int a = A[--k2]; if (a > 0) { for (int j = he[a]; j; j = ne[j]) { ll t = to[j]; if (t >> 30) { kotae[t >> 30] = 1000000 - c[t & ((1 << 20) - 1)]; A[k2++] = -j; } else { A[k2++] = t; } } c[C[a]]++; } else { ll t = to[-a]; kotae[t >> 30] += c[t & ((1 << 20) - 1)]; } } } } rep1(i, Q) { if (kotae[i] >= 1000000) putint(kotae[i] - 1000000); } fwrite(dn, 1, di - dn, stdout); Would you please return 0; }
#ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG //checks for container overflows #pragma GCC optimize "trapv" //to chcek for int overflow #pragma -Wduplicated-cond //dupilcate conditions #endif #include "bits/stdc++.h" using namespace std; using namespace chrono; typedef unsigned long long int ull; typedef long long ll; #define F first // #define S second #define nl '\n' #define de1(x) cerr << #x << ":" << x <<nl; #define de2(x,y) cerr << #x << ":" << x << " " << #y << ":" << y << nl #define de3(x,y,z) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<nl #define de4(x,y,z,w) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<" | "<<#w<<": "<<w<<nl #define all(x) x.begin(), x.end() #define rt return 0; #define vvi vector<vector<int>> #define vi vector<int> #define vpi vector<pair<int,int>> #define fast ios_base::sync_with_stdio(0); cin.tie(0); #define pb push_back #define um unordered_map<int,int> const float pi = 3.14; const ull mod = 1ll << 32; void solve() { stack<char> s; int n; string str; cin >> n >> str; if (n <= 3 && str != "fox") { cout << n; return; } register int cnt = 0, i; for (i = 0; i < n; i++) { if (str[i] == 'x' && s.size() && s.top() == 'o') { s.pop(); if (s.size() && s.top() == 'f') { cnt += 3; s.pop(); continue; } s.push('o'); s.push('x'); } else s.push(str[i]); } cout << n - cnt; } int main() { #ifndef ONLINE_JUDGE auto start = high_resolution_clock::now(); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast solve(); #ifndef ONLINE_JUDGE auto end = high_resolution_clock::now(); double time_taken = duration_cast<nanoseconds>(end - start).count(); time_taken *= 1e-9; cerr << "Time : " << fixed << time_taken << setprecision(9) << nl; #endif return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define REP(i,n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define debug(var) do{cout << #var << " : "; view(var);}while(0) 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;} using namespace std; template<class T> void view(T e) {cout << e << endl;} template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;} template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}} using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int,int>; const int inf = 1<<30; const ll inf_l = 1LL<<61; const int MAX = 1e5; int main() { int n; cin >> n; string s; cin >> s; string t; rep(i,n) { t += s[i]; int sz = t.size(); if (sz - 3 >= 0 && t.substr(sz - 3, 3) == "fox") { rep(i,3) t.pop_back(); } } int ans = t.size(); cout << ans << endl; }
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <string.h> #include <vector> #include <queue> #include <cmath> #include <complex> #include <functional> #include <numeric> #include <iomanip> #include <cassert> #include <random> #include <chrono> /* #include <atcoder/all> */ /* using namespace atcoder; */ using namespace std; void debug_out(){ cout << "\n"; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1000000007; /* const ll MOD = 998244353; */ const ll INF = 1ll<<60; const double PI = acos(-1); struct INIT { INIT(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }}INIT; void f(ld x, ld y, ld t, ld &ax, ld &ay){ ax = cos(t)*x - sin(t)*y; ay = sin(t)*x + cos(t)*y; } int N; void solve(ld x0, ld y0, ld xn, ld yn, ld &ax, ld &ay){ ld mx = double(x0+xn)/2; ld my = double(y0+yn)/2; ld h = atan2(yn-y0, xn-x0); ld r = sqrt((x0-xn)*(x0-xn) + (y0-yn)*(y0-yn))/2; /* cout << h*180/PI << "\n"; */ ld x00, y00, xn0, yn0; ld mx0, my0; f(x0, y0, -h, x00, y00); f(xn, yn, -h, xn0, yn0); f(mx, my, -h, mx0, my0); ld t = 2*PI/N; /* cout << x00 << " " << y00 << "\n"; */ /* cout << xn0 << " " << yn0 << "\n"; */ if(x00 < xn0){ t += PI; } /* cout << t*180/PI << "\n"; */ /* cout << "\n"; */ /* cout << mx0 << " " << my0 << "\n"; */ mx0 += r*cos(t); my0 += r*sin(t); /* cout << mx0 << " " << my0 << "\n"; */ f(mx0, my0, h, ax, ay); } signed main() { cin >> N; ld x0, y0, xn, yn; cin >> x0 >> y0 >> xn >> yn; ld ax, ay; solve(x0, y0, xn, yn, ax, ay); cout << ax << " " << ay << "\n"; return 0; }
// #include <atcoder/all> #include <bits/stdc++.h> using namespace std; // using namespace atcoder; #define rep(i, s, n) for (int i = s; i < (int)(n); i++) typedef long long ll; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int main() { ll N; cin >> N; vector<ll> x(N); vector<ll> y(N); ll x_; ll y_; ll max_x; ll max_x_i = 0; ll min_x; ll min_x_i = 0; ll max_y; ll max_y_i = 0; ll min_y; ll min_y_i= 0; rep(i,0,N){ cin >> x_; x[i] = x_; cin >> y_; y[i] = y_; if (i==0){ max_x = x_; min_x = x_; max_y = y_; min_y = y_; } else { if (max_x < x_) { max_x = x_; max_x_i = i; }if (max_y < y_) { max_y = y_; max_y_i = i; } if (min_x > x_) { min_x = x_; min_x_i = i; }if (min_y > y_) { min_y = y_; min_y_i = i; } } } sort(x.begin(), x.end()); sort(y.begin(), y.end()); ll ans = 0; vector<ll> temp(6); if (max_x_i == max_y_i && min_x_i == min_y_i) { chmax(ans, x[N-1]-x[1]); chmax(ans, x[N-2]-x[0]); chmax(ans, y[N-1]-y[1]); chmax(ans, y[N-2]-y[0]); } else if (max_x_i == min_y_i && min_x_i == max_y_i){ chmax(ans, x[N-1]-x[1]); chmax(ans, x[N-2]-x[0]); chmax(ans, y[N-1]-y[1]); chmax(ans, y[N-2]-y[0]); } else { temp[0] = x[N-1]-x[0]; temp[1] = y[N-1]-y[0]; temp[2] = x[N-2]-x[0]; temp[3] = y[N-2]-y[0]; temp[4] = x[N-1]-x[1]; temp[5] = y[N-1]-y[1]; sort(temp.begin(), temp.end()); ans = temp[4]; } cout << ans << endl; }