code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <iostream> #include <algorithm> #define flush fflush(stdout) #define endl '\n' using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> Pl; const int mod1 = (int)1e9 + 7, mod2 = (int)998244353; const int INF = (int)1e9; const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1}; #define rep0(i, n) for (i = 0; i < n; i++) #define rep1(i, a, b) for (i = a; i < b; i++) int main(void){ int i, j; int h, w, k, hp, wp; char c, dir[5003][5003] = {}; cin >> h >> w >> k; rep0(i, h){ rep0(j, w){ dir[i][j] = '.'; } } rep0(i, k){ cin >> hp >> wp >> c; dir[hp - 1][wp - 1] = c; } ll dp[5003][5003] = {}; int sc, twth; twth = 332748118 * 2 % mod2; // 2/3 dp[0][0] = 1; rep0(i, h){ rep0(j, w){ if (i > 0){ c = dir[i - 1][j]; if (c == 'R'){ sc = 0; }else if (c == '.'){ sc = twth; }else{ sc = 1; } dp[i][j] = (dp[i][j] + dp[i - 1][j] * sc % mod2) % mod2; } if (j > 0){ c = dir[i][j - 1]; if (c == 'D'){ sc = 0; }else if (c == '.'){ sc = twth; }else{ sc = 1; } dp[i][j] = (dp[i][j] + dp[i][j - 1] * sc % mod2) % mod2; } } } ll wr; wr = 1; rep0(i, h * w - k){ wr = wr * 3 % mod2; } cout << dp[h - 1][w - 1] * wr % mod2 << endl; return (0); }
#include <bits/stdc++.h> const long long SZ = 1e5 + 7; const long long inf = 1e18; const long long MOD = 1e9 + 7; const long long mod = 1e9 + 7; long long opnmbr = 1; #define ll long long #define ld long double #define pb push_back #define mp make_pair #define eb emplace_back #define pll pair<ll, ll> #define vi vector<ll> #define vs vector<string> #define vpl vector<pll> #define qi queue<ll> #define si set<ll> #define mi map<ll, ll> #define umi unordered_map<ll, ll> #define fi first #define se second #define sz(x) (ll)x.size() #define all(c) (c).begin(), (c).end() #define allr(c) (c).rbegin(), (c).rend() #define Max(a,b) ((a > b) ? a : b) #define Min(a,b) ((a < b) ? a : b) #define ci(X) ll X; cin>>X #define cii(X, Y) ll X, Y; cin>>X>>Y #define ciii(X, Y, Z) ll X, Y, Z; cin>>X>>Y>>Z #define ciiii(W, X, Y, Z) ll W, X, Y, Z; cin>>W>>X>>Y>>Z #define dbg(x) cout<<#x<<"="<<(x)<<endl; #define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; #define dbg3(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl; #define dbg4(x,y,z,w) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<" "<<#w<<"="<<(w)<<endl; #define ons() cout<<"Case #"<<opnmbr++<<": "; #define krosuru ll ___T; cin>>___T; while (___T-- > 0) #define yehbhitheekhai ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define inf 1e18 #define Endl endl #define forn(i, n) for(ll i = 0; i < (n); i++) #define fore(i, n) for(ll i = 1; i <= (n); i++) #define forl(i, l, r) for(ll i = l; i < (r); i++) #define forr(i, l, r) for(ll i = l; i >= (r); i--) #define ms0(X) memset((X), 0, sizeof((X))) #define ms1(X, V) memset((X), -1, sizeof((X))) #define flv(X, V) fill(all((X)), V) using namespace std; ll powermod(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = (ans * a) % MOD; b = b / 2; a = (a * a) % MOD; } return ans; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } void krdiyasuru() { string s; cin >> s; ll n = sz(s); string ans = ""; deque<char> str; ll f = 1; forn(i, n) { if(s[i] == 'R') { f ^= 1; } else { if(f == 1) { str.pb(s[i]); } else { str.push_front(s[i]); } } } for(auto x: str) { ans += x; } if(f == 0) { reverse(all(ans)); } string a; for(auto& c : ans) { if(sz(a) && a.back() == c) { a.pop_back(); } else { a.pb(c); } } cout<<a<<endl; return; } int main() { yehbhitheekhai; // solve(); // krosuru { krdiyasuru(); } }
/* _ _ _ __ __ _____ /\ | | /\ | | | | | \/ |/ ____| / \ | |__ ___ / \ | |__ __| | ___ | \ / | | / /\ \ | '_ \ / _ \ / /\ \ | '_ \ / _` |/ _ \| |\/| | | / ____ \| |_) | (_) / ____ \| |_) | (_| | (_) | | | | |____ /_/ \_\_.__/ \___/_/ \_\_.__/ \__,_|\___/|_| |_|\_____| */ #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #define F first #define S second #define pb push_back #define all(x) x.begin(), x.end() #define sz(x) ((int)(x).size()) typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const ld eps = 1e-9; const int mod = 1e9 + 7; const int oo = 1e9 + 7; const ll lloo = 1e18 + 7; const int N = 1e6 + 7; void solve(); int main() { // freopen("in","r",stdin); // freopen("out","w",stdout); int t = 1; // scanf("%d",&t); while (t--) solve(); return 0; } char buff[N]; int nxt[N],bef[N]; void solve() { scanf("%s",buff); string str = buff; int n = (int)str.size(); deque<char> q; bool rev = 0; for(int i = 0 ; i < n ; i++) { if (str[i] == 'R') rev = !rev; else { if (rev) q.push_front(str[i]); else q.push_back(str[i]); } } str = ""; for(auto x:q) str+=x; if (rev) reverse(all(str)); n = (int)str.size(); for(int i = 0 ; i < n ; i++) {nxt[i] = i+1;bef[i+1] = i;} nxt[n+1] = 0; bef[0] = n+1; //cout << str << endl; while(1) { int tmp = nxt[n+1]; bool ok = 0; while (nxt[tmp] != n) { if (str[tmp] == str[nxt[tmp]]) { nxt[bef[tmp]] = nxt[nxt[tmp]]; bef[nxt[nxt[tmp]]] = bef[tmp]; if (bef[tmp] != n+1) tmp = bef[tmp]; else if (nxt[nxt[tmp]] != n) tmp = nxt[nxt[tmp]]; else {ok = 0;break;} ok = 1; } else tmp = nxt[tmp]; } if (!ok) break; } string ans = ""; int tmp = nxt[n+1]; while(tmp != n) {ans += str[tmp];tmp = nxt[tmp];} printf("%s\n",ans.c_str()); }
#include <bits/stdc++.h> #include <chrono> #include <ctime> using namespace std; using chrono::steady_clock; using chrono::milliseconds; #define sp " " #define INF 1e18 #define INT_INF 1e9 typedef long long ll; typedef pair<int,int> P; chrono::steady_clock::time_point now_t; chrono::steady_clock::time_point start_t; random_device x; mt19937 mt(x()); struct coord { int x; int y; }; const int N = 50; vector<vector<int>> t(50,vector<int>(50)); vector<vector<int>> p(50,vector<int>(50)); int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; char direct[] = {'D','R','U','L'}; struct state { //状態を管理 int l; vector<coord> route; vector<char> direct;//D,R,U,L vector<int> score; vector<int> reach;//0 false 1 true }; int get_rnd(int mod){ return mt()%mod; } bool TIME_CHECK( int Limit ){ now_t = steady_clock::now(); if(chrono::duration_cast<milliseconds>(now_t-start_t) < milliseconds(Limit))return true; return false; } void enter(state &S){ //入力 int sx,sy; cin >> sx >> sy; coord s = {sx,sy}; S.route.push_back(s); for(int i = 0;i < 2500;i++)S.reach.push_back(0); for(int i = 0;i < 50;i++){ for(int j = 0;j < 50;j++){ cin >> t[i][j]; } } for(int i = 0;i < 50;i++){ for(int j = 0;j < 50;j++){ cin >> p[i][j]; } } S.reach[t[S.route[0].x][S.route[0].y]] = 1; S.score.push_back(p[S.route[0].x][S.route[0].y]); S.l = 1; return; } void output(state S){ //出力 int l = S.l; for(int i = 0;i < l-1;++i){ //cout << i << sp << S.route[i+1].x-S.route[0].x << sp << S.route[i+1].y-S.route[0].y << endl; // if(S.route[i+1].x - S.route[i].x == 1)cout << "D"; // if(S.route[i+1].x - S.route[i].x == -1)cout << "U"; // if(S.route[i+1].y - S.route[i].y == 1)cout << "R"; // if(S.route[i+1].y - S.route[i].y == -1)cout << "L"; // //cout << endl; cout << S.direct[i]; } } void dfs(state &S){ state best_s = S; stack<state> st; st.push(S); while(!st.empty() && st.size() < 1000 && TIME_CHECK(1800)){ state s = st.top(); st.pop(); int cx,cy,l; l = s.l; cx = s.route[l-1].x; cy = s.route[l-1].y; for(int i = 0;i < 4;i++){ state T = s; if(cx+dx[i] < 0 || cx+dx[i] >= 50 || cy+dy[i] < 0 || cy+dy[i] >= 50)continue; else if(T.reach[t[cx+dx[i]][cy+dy[i]]] == 0){ coord c = {cx+dx[i],cy+dy[i]}; T.route.push_back(c); T.score.push_back(T.score[l-1] + p[c.x][c.y]); if(T.score[l] > best_s.score[best_s.l-1])best_s = T; if(st.size() > 50){ if(T.score[l] < best_s.score[min(l,best_s.l - 1)]*9/10)continue; } T.reach[t[c.x][c.y]] = 1; T.direct.push_back(direct[i]); T.l += 1; st.push(T); } } } S = best_s; } //void mountain( state &S){ //山登り // int l = S.l; // int n = get_rnd(l); //} // void AS ( state &S){ // //焼きなまし // } /* 近傍の取り方でスコア悪化の確立が高すぎるもの、 変化がおおきすぎるものを選ぶとスコア改善しないので、 適切な近傍の取り方を考える 思いつかないなら山登りでよい 極力連続でなだらかな関数にする 近傍の取り方は2種類用意したほうがいい(できれば) 焼きなましの温度は対数的に減らす->確率は線形になる 線形的に減らす->確率は指数的になる rand()は遅いので使わない */ int main(){ start_t = steady_clock::now(); state S; enter(S); // while(true){ // int res = greed(S); // if(res == 0)continue; // else break; // } dfs(S); output(S); }
#include <bits/stdc++.h> #define ll long long using namespace std; #define MOD 1000000007 ll powmod(ll a, ll p) { //cout<<a<<" "<<p<<endl; if(p==0) return 1; if(p%2) return (powmod(a,p-1)*a)%MOD; ll z = powmod(a,p/2); return (z*z)%MOD; } ll inv(ll a) { return powmod(a%MOD,MOD-2); } void solve() { int N, M; cin>>N>>M; vector<int> A(N); for(auto &a: A) cin>>a; ll s = 0; for(auto &a: A) s+=a; if(s>M) { cout<<0<<endl; return; } ll n = M-s; ll k = s+N+1; ll ans = 1; for(int i=1; i<k; ++i) { ans = (ans*(n+i))%MOD; ans = (ans*inv(i))%MOD; } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll N, X; vector<ll> A(60, 0); const ll M = 100000; ll memo[60][M] = {}; ll cnt(ll index, ll zan) { if (zan < M && memo[index][zan] > 0) return memo[index][zan]; if (index == 0) return 1; if (zan == 0) return 1; ll temp = zan / A[index]; ll zan1 = zan - A[index] * temp; ll zan2 = abs(zan - A[index] * (temp + 1)); ll ans; if (temp == 0 && index != N - 1) ans = cnt(index - 1, zan1); else if (zan1 == 0) ans = cnt(index - 1, zan2); else if (zan2 == 0) ans = cnt(index - 1, zan1); else ans = cnt(index - 1, zan1) + cnt(index - 1, zan2); if (zan < M) memo[index][zan] = ans; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; cin >> X; for (ll i = 0; i < N; i++) { cin >> A[i]; } ll ans = cnt(N - 1, X); cout << ans << '\n'; return 0; }
#include <iostream> #include <iomanip> #include <vector> #include <cmath> #include <algorithm> #include <deque> #include <set> #include <limits> #include <string> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <numeric> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) /* input & output */ #define IN(a) cin >> a #define OUT(a) cout << a << endl #define OUT_PREC(a, n) cout << fixed << setprecision(n) << a << endl /* vector */ #define VEC(t, v, n, ini) vector<t> v(n, ini) #define VEC2(t, v, m, n, ini) vector<vector<t>> v(m, vector<t>(n, ini)) #define VEC3(t, v, k, m, n, ini) vector<vector<vector<t>>> v(k, vector<vector<t>>(m, vector<t>(n, ini))) /* bit */ #define IS_TRUE(bit, i) (bit & (1 << i)) // bitのi成分 #define FLAG_ON(bit, i) (bit|(1 << i)) // bitのi成分をtrueに #define FLAG_OFF(bit, i) (bit & ~(1 << i)) // bitのi成分をfalseに #define ALL_TRUE(n) (1 << n) // 2^n #define CMPSET(bit, s) (bit ^ s) // sのbitに対する補集合 #define INCLUDES(a, b) ((a & b) == b) // aはbを含むか #define INF 1e12 ll solve() { int n, m; cin >> n >> m; vector<ll> a(n), dp(n, INF); FOR(i, 0, n) { IN(a[i]); } vector<vector<int>> t(n, vector<int>{}); FOR(i, 0, m) { int x, y; cin >> x >> y; --x; --y; t[y].emplace_back(x); } FOR(i, 0, n) { for (int j : t[i]) { dp[i] = min(min(dp[i], dp[j]), a[j]); } } ll ans = -INF; FOR(i, 0, n) { if (a[i] - dp[i] > ans) ans = a[i] - dp[i]; } return ans; } int main() { /* solve */ OUT(solve()); system("pause"); }
#include <algorithm> #include <array> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <utility> #include <vector> #include <stack> using namespace std; using ll = long long int; // fast IO #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ////////// Template structure for debugging #define tr(...) trace(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void trace(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void trace(const char* names, Arg1&& arg1, Args&&... args){ const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } #define print(...) pr(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void pr(const char* name, Arg1&& arg1){ cout << arg1 << endl; } template <typename Arg1, typename... Args> void pr(const char* names, Arg1&& arg1, Args&&... args){ const char *comma = strchr(names + 1, ','); cout << arg1 << " "; pr(comma + 1, args...); } void printV(vector<int> &arr) { cout << "Vector : "; for(int i=0;i<arr.size();i++) cout << arr[i] << " \n"[ i == arr.size()-1 ]; } void printV(vector<long long> &arr) { cout << "Vector : "; for(int i=0;i<arr.size();i++) cout << arr[i] << " \n"[ i == arr.size()-1 ]; } void printA(int arr[], int n) { cout << "Array : "; for (int i = 0; i < n; i++) cout << arr[i] << " \n"[i == n - 1]; } ///////////////////////////////////////////////////////////////////////////// ////////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////// ///////////////////////////////////////////////////////////////////////////// const ll INF = 1e15; ll N, M, ans = -INF; vector<ll> C, vis; vector<vector<ll>> G; ll dfs(ll curr, ll mn){ vis[curr] = -INF; for(auto child: G[curr]){ if(vis[child] == 0){ vis[curr] = max(vis[curr], dfs(child, min(C[child], mn))); } else vis[curr] = max(vis[curr], vis[child]); } if(!G[curr].empty())ans = max(ans, vis[curr] - mn); //tr(curr, vis[curr], mn); vis[curr] = max(vis[curr], C[curr]); return vis[curr]; } void solve(int tc = 1){ cin >> N >> M; C.resize(N); vis.resize(N, 0); G.resize(N); for(auto &a: C)cin >> a; for(int i=0; i<M; i++){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); } for(int i=0; i<N; i++){ if(vis[i])continue; dfs(i, C[i]); } cout << ans << endl; } int main(){ IOS; int tt = 1; //cin >> tt; while(tt--)solve(); }
#include<bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; string temp="ZONe"; int d=0; for(int i=0;i<s.size();++i){ if(s[i]==temp[0]){ int f=0; for(int j=0;j<temp.size();++j){ if(s[i+j]!=temp[j]){ f=1;break; } } if(f==0)d++; } } cout<<d<<endl; }
#include <bits/stdc++.h> using namespace std; char s[200005]; int cnt[10]; int n; bool dfs(int i, int rem){ if(i == n+1){ if(rem == 0) return 1; return 0; } for(int j=1;j<=9;j++){ if(cnt[j] > 0){ cnt[j]--; if(dfs(i+1, (rem*10+j)%8)) return 1; cnt[j]++; } } return 0; } int main(){ scanf("%s", s+1); n = strlen(s+1); for(int i=1;i<=n;i++) cnt[s[i]-'0']++; n = min(n, 3); bool ok = dfs(1, 0); if(ok) printf("Yes\n"); else printf("No\n"); }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define mt make_tuple #define pii pair<int,int> #define pll pair<ll,ll> #define ldb double template<typename T>void ckmn(T&a,T b){a=min(a,b);} template<typename T>void ckmx(T&a,T b){a=max(a,b);} void rd(int&x){scanf("%i",&x);} void rd(ll&x){scanf("%lld",&x);} void rd(char*x){scanf("%s",x);} void rd(ldb&x){scanf("%lf",&x);} void rd(string&x){scanf("%s",&x);} template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);} template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);} template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);} template<typename T>void rd(){T x;rd(x);return x;} int ri(){int x;rd(x);return x;} template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;} template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);} template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);} template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);} void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}} template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}} const int inf=1e9+7; string atc="atcoder"; int Solve(string str,int pos){ if(str.size()<=pos)return inf; int ans=0; for(int i=0;i<pos;i++){ int o=-1; for(int j=i;j<str.size();j++){ if(str[j]==atc[i]){ o=j; break; } } if(o==-1)return inf; for(int j=o;j>i;j--){ ans++; swap(str[j],str[j-1]); } } int o=-1; for(int j=pos;j<str.size();j++){ if(str[j]>atc[pos]){ o=j; break; } } if(o==-1)return inf; ans+=o-pos; return ans; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t;cin>>t; for(;t--;){ string str;cin>>str; int ans=inf; for(int i=0;i<=atc.size();i++)ckmn(ans,Solve(str,i)); if(ans==inf)cout<<"-1\n"; else cout<<ans<<"\n"; } return 0; }
#include <bits/stdc++.h> #define rep(i, N) for(int i = 0; i < (int)N; i++) #define rep1(i, N) for(int i = 0; i <= (int)N; i++) using namespace std; typedef long long ll; typedef pair<int,int> P; typedef vector<int> vi; const int INF = 101010; int main() { int q, m, ans = 0; const string t = "atcoder"; cin >> q; while(q--){ string s; cin >> s; if (s > t) { puts("0"); continue; } int ans = INF; rep(i,s.size()){ if (s[i] > 'a') { if (s[i] > 't') { ans = i-1; }else{ ans = i; } break; } } if(ans == INF) puts("-1"); else printf("%d\n", ans); } }
#include<bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=(int)(x);i<(int)(y);i++) #define print(A,x,n) {rep(I,0,n){cout<<(I ? " ":"")<<A[I]x;}cout<<endl;} #define pprint(A,y,m,n) {rep(J,0,m){print(A[J],y,n);}} const long mod=998244353; const int siz=3e5; const int inf=1e6; int main(){ int N; cin>>N; int w, sum = 0; long dp[N+1][10001] = {}; dp[0][0] = 1; rep(i,1,N+1){ cin>>w; sum += w; for(int j = i; j > 0; j--){ rep(k,w,10001) (dp[j][k] += dp[j-1][k-w]) %= mod; } } if(sum % 2 == 1){ cout<<0<<endl; return 0; } long ans = 0; long fact[N+1]; fact[0] = 1; rep(i,1,N+1) fact[i] = fact[i-1] * i % mod; rep(i,1,N+1){ long k = fact[i] * fact[N-i] % mod; (ans += dp[i][sum/2] * k % mod) %= mod; } cout<<ans<<endl; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Pine1999 */ #include <bits/stdc++.h> template<int mod> class Modular { private: int x; public: Modular() : x(0) {} Modular(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} Modular &operator+=(const Modular& p) { if ((x += p.x) >= mod) x -= mod; return *this; } Modular &operator-=(const Modular& p) { if ((x -= p.x) < 0) x += mod; return *this; } Modular &operator*=(const Modular& p) { x = (int) (1LL * x * p.x % mod); return *this; } Modular &operator/=(const Modular& p) { *this *= p.Inverse(); return *this; } Modular operator-() const { return Modular(-x); } Modular operator+(const Modular& p) { return Modular(*this) += p; } Modular operator-(const Modular& p) { return Modular(*this) -= p; } Modular operator*(const Modular& p) { return Modular(*this) *= p; } Modular operator/(const Modular& p) { return Modular(*this) /= p; } bool operator==(const Modular& p) { return x == p.x; } bool operator!=(const Modular& p) { return x != p.x; } friend std::ostream& operator<<(std::ostream& out, const Modular& p) { return out << p.x; } friend std::istream& operator>>(std::istream& in, Modular& a) { long long t; in >> t; a = Modular<mod>(t); return in; } Modular Inverse() const { int a = x, m = mod, u = 1, v = 0, t; while (m > 0) { t = a / m; std::swap(a -= t * m, m); std::swap(u -= t * v, v); } return Modular(u); } }; const int mod = 998244353; using mint = Modular<mod>; class Task { std::vector<mint> fact; void init(int n) { fact.resize(n + 1); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; } public: void solve(std::istream& in, std::ostream& out) { using namespace std; int n; in >> n; vector<int> w(n); for (int i = 0; i < n; ++i) in >> w[i]; int sum = accumulate(w.begin(), w.end(), 0); if (sum & 1) { out << 0 << '\n'; return; } const int MAX = (int) 1e5 + 50; vector<vector<mint>> dp(n + 1, vector<mint> (MAX)); dp[0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = n; j >= 1; --j) { for (int k = j * 100; k >= w[i]; --k) { dp[j][k] += dp[j - 1][k - w[i]]; } } } init(n); mint ans = 0; for (int i = 1; i < n; ++i) { ans += dp[i][sum / 2] * fact[i] * fact[n - i]; } out << ans << '\n'; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); Task solver; solver.solve(std::cin, std::cout); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i=0;i<(int)(n);i++) #define ALL(a) (a).begin(),(a).end() using namespace std; using ll=long long; typedef pair<int,int> P; int main(){ int n; string s; cin>>n>>s; while(s.size()>1){ while(n%3!=1){ int k=1; string next; rep(i,s.size()-k){ if((s[i]=='B'&&s[i+k]=='B')||(s[i]=='R'&&s[i+k]=='W')||(s[i]=='W'&&s[i+k]=='R')){ next+='B'; } else if((s[i]=='R'&&s[i+k]=='R')||(s[i]=='B'&&s[i+k]=='W')||(s[i]=='W'&&s[i+k]=='B')){ next+='R'; }else{ next+='W'; } } s.erase(); s=next; n=s.size(); //cout<<s<<endl; } if(s.size()==1) break; while(n%3==1){ int k=3; string next; for(int i=0;i<n;i+=3){ next+=s[i]; } s.erase(); s=next; n=s.size(); //cout<<s<<endl; } } cout<<s<<endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define PI acos(-1) #define vi vector<int> const int kMod = 998244353; const int kInf = (~(1<<31)) / 2 - 500; const ll kInfL = (~(1LL<<63)) / 2 - 2000; int main() { ll B, C; cin >> B >> C; pair<ll, ll> plus = {B, B}, minus = {-B, -B}; // plus plus.second = min(plus.second, B - C / 2); plus.first = max(plus.first, B + (C - 2) / 2); minus.second = min(minus.second, -B - (C - 1) / 2); minus.first = max(minus.first, -B + (C - 1) / 2); ll ans = 0; if (plus.second <= minus.first) { ans = max(plus.first, minus.first) - min(plus.second, minus.second) + 1; } else { ans += plus.first - plus.second + 1; ans += minus.first - minus.second + 1; } cout << ans << endl; }
//#pragma GCC target ("avx2") //#pragma GCC optimization ("O3") //#pragma GCC optimization ("unroll-loops") #include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; #define eps 1e-8 //#define eq(x,y) (fabs((x)-(y)) < eps) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int>pii; const int mod= 1e9+7; long double pi = acosl(-1); //const ll infl = LLONG_MAX; const int inf = 1e9+2; const int nmax = 4e5+5; //const int MAXLG = log2(nmax)+1; //mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); //typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; typedef long long LL; struct Edge { int u, v; LL cap, flow; Edge() {} Edge(int u, int v, LL cap): u(u), v(v), cap(cap), flow(0) {} }; struct Dinic { int N; vector<Edge> edges; vector<vector<int>> adj; vector<int> d, pt; Dinic(int N): N(N), edges(0), adj(N), d(N), pt(N) {} void AddEdge(int u, int v, LL cap) { if (u != v) { edges.emplace_back(u, v, cap); adj[u].emplace_back(edges.size() - 1); edges.emplace_back(v, u, 0); adj[v].emplace_back(edges.size() - 1); } } bool BFS(int S, int T) { queue<int> q({S}); fill(d.begin(), d.end(), N + 1); d[S] = 0; while(!q.empty()) { int u = q.front(); q.pop(); if (u == T) break; for (int k: adj[u]) { Edge &e = edges[k]; if (e.flow < e.cap && d[e.v] > d[e.u] + 1) { d[e.v] = d[e.u] + 1; q.emplace(e.v); } } } return d[T] != N + 1; } LL DFS(int u, int T, LL flow = -1) { if (u == T || flow == 0) return flow; for (int &i = pt[u]; i < adj[u].size(); ++i) { Edge &e = edges[adj[u][i]]; Edge &oe = edges[adj[u][i]^1]; if (d[e.v] == d[e.u] + 1) { LL amt = e.cap - e.flow; if (flow != -1 && amt > flow) amt = flow; if (LL pushed = DFS(e.v, T, amt)) { e.flow += pushed; oe.flow -= pushed; return pushed; } } } return 0; } LL MaxFlow(int S, int T) { LL total = 0; while (BFS(S, T)) { fill(pt.begin(), pt.end(), 0); while (LL flow = DFS(S, T)) total += flow; } return total; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<pii>v(n); for(pii &x : v) cin>>x.first>>x.second; vector<int>shob; for(pii z : v) shob.push_back(z.first), shob.push_back(z.second); sort(shob.begin(), shob.end()); shob.erase(unique(shob.begin(), shob.end()), shob.end()); for(pii &z : v){ z.first = lower_bound(shob.begin(), shob.end(), z.first) - shob.begin(); z.second = lower_bound(shob.begin(), shob.end(), z.second) - shob.begin(); } int sz = shob.size(); int N = sz + n + 2; int sorc = N-2, sink = N-1; Dinic flu(N); for(int i=0; i<n; i++){ flu.AddEdge(sorc, sz+i, 1); flu.AddEdge(sz+i, v[i].first, 1); flu.AddEdge(sz+i, v[i].second, 1); } for(int i=0; i<sz; i++){ flu.AddEdge(i, sink, 1); } cout<<flu.MaxFlow(sorc, sink)<<endl; } /* */
#include <bits/stdc++.h> using namespace std; #ifdef Dhiraj #include "D:/dhiraj/Programming/debug.h" #else #define d(...) 11 #define cerr if(0) cerr #endif #define ll long long int #define endl '\n' template <typename T, typename U> inline istream& operator >> (istream& in, pair<T, U>& p) { in >> p.first >> p.second; return in; } template <typename T> inline istream& operator >> (istream& in, vector<T>& v) { for(T& x : v) in >> x; return in; } const ll MX = 4e5 + 5; ll n, m; vector<vector<ll>> g; vector<ll> vis; void clearAll() { vis.assign(MX, 0); g.assign(MX, vector<ll>()); } ll cc, edges; void dfs(ll u) { if(vis[u] || g[u].empty()) return; cc++; vis[u] = 1; for(ll v : g[u]) { edges++; dfs(v); } } void solve(int &tc) { cin >> n; clearAll(); for(ll i = 0; i < n; i++) { ll x, y; cin >> x >> y; g[x].emplace_back(y); g[y].emplace_back(x); } ll ans = 0; for(ll i = 0; i < MX; i++) { cc = edges = 0; dfs(i); edges /= 2; ans += min(cc, edges); } cout << ans << endl; } int main() { #ifdef Dhiraj freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); // freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); #endif ios::sync_with_stdio(false); cin.tie(0); int tc = 1; for(int i = 1; i <= tc; i++) { cerr << "Case #" << i << "\n"; solve(i); } return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; const ll MOD = 998244353; const double PI = 3.141592653589793238; int main(){ auto start = chrono::system_clock::now(); ll si, sj; cin >> si >> sj; ll nowi = si, nowj = sj; vector<ll> t(2500); for (ll i = 0; i < 50; ++i) { for (ll j = 0; j < 50; ++j) { cin >> t[50 * i + j]; } } vector<ll> p(2500); for (ll i = 0; i < 50; ++i) for (ll j = 0; j < 50; ++j) cin >> p[50 * i + j]; string lastans = ""; ll lastpnt = 0; map<ll, bool> used; string ans = ""; while (true) { ll id = 50 * nowi + nowj; ll maxi = -1; used[t[id]] = true; char togo = ' '; if (nowj != 0 && !used[t[id - 1]] && maxi < p[id - 1]) { togo = 'L'; maxi = p[id - 1]; } if (nowj != 49 && !used[t[id + 1]] && maxi < p[id + 1]) { togo = 'R'; maxi = p[id + 1]; } if (nowi != 0 && !used[t[id - 50]] && maxi < p[id - 50]) { togo = 'U'; maxi = p[id - 50]; } if (nowi != 49 && !used[t[id + 50]] && maxi < p[id + 50]) { togo = 'D'; maxi = p[id + 50]; } if(togo != ' ')ans += togo; switch (togo) { case 'L': --nowj; break; case 'R': ++nowj; break; case 'U': --nowi; break; case 'D': ++nowi; break; default: goto exit; } } exit:; nowi = si, nowj = sj; lastans = ans; ll now = 50 * nowi + nowj; lastpnt += p[now]; for (ll i = 0; i < ans.size(); ++i) { switch (ans[i]) { case 'L': --nowj; break; case 'R': ++nowj; break; case 'U': --nowi; break; case 'D': ++nowi; break; } lastpnt += p[now]; } string sample = "LRUD"; random_device rnd; mt19937 mt(rnd()); uniform_int_distribution<> cha(0, 3), rand(1,1000); auto dur = chrono::system_clock::now() - start; while (chrono::duration_cast<std::chrono::milliseconds>(dur).count() < 1980) { ans = lastans; uniform_int_distribution<> change(0, ans.size()); ll ch = change(mt), at = cha(mt), r = rand(mt); if (ch == ans.size()) ans.push_back(sample[at]); else ans[ch] = sample[at]; nowi = si, nowj = sj; now = 50 * nowi + nowj; map<ll, bool> used; used[t[now]] = true; ll pnt = p[now]; for (ll i = 0; i < ans.size(); ++i) { switch (ans[i]) { case 'L': --nowj; break; case 'R': ++nowj; break; case 'U': --nowi; break; case 'D': ++nowi; break; } if (nowi < 0 || 49 < nowi || nowj < 0 || 49 < nowj) { pnt = -1; break; } now = 50 * nowi + nowj; if (used[t[now]]) { pnt = -1; break; } used[t[now]] = true; pnt += p[now]; } if (pnt >= lastpnt) { lastpnt = pnt; lastans = ans; } else if(pnt != -1 && r <= 125){ lastpnt = pnt; lastans = ans; } dur = chrono::system_clock::now() - start; } cout << lastans << "\n"; }
// C++ (GCC9.2.1) AtCoder /* Ctrl+Option+N で 実行 */ #include <bits/stdc++.h> #define INF 100000000000 #define MOD 1000000007 #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(),(x).end() #define print(x) cout << (x) << endl; typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N,K; cin>>N>>K; ll last = -1; map <ll,ll> mp; REP(i,N){ ll a,b; cin>>a>>b; mp[a] += b; } ll ans = 0; ll money = K; ll now = 0; for(auto itr = mp.begin();itr != mp.end(); itr++ ){ ll next = itr -> first; ll get = itr -> second; //print(next + 10000) //print(money) if(money - (next - now) >= 0){ money -= (next - now); now = next; money = money + get ; continue; } else { print(now + money) return 0; } } //print(money) assert(money >= 0); now += money; print(now); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long #define ff first #define ss second #define ld long double template <class T> void print(vector<T>& v){ for(T x:v) cout<<x<<" "; cout<<"\n"; } void inp(vector<int>& v, int n){ for (int i=0; i<n; ++i) scanf("%d ", &v[i]); } const int nax=1e+5+5; void sol(){ int n; cin>>n; vector<int> nums(n); inp(nums, n); // cout<<n<<endl; // print(nums); ll curr=0, res=0, a=0, b=0; for (ll x:nums){ a+=x; b=max(a, b); res=max(res, curr+b); curr+=a; // cout<<"curr ="<<curr<<" z="<<z<<endl; // res=max(res, curr); } cout<<res<<endl; } int main() { int t; // cin>>t; t=1; t++; // std::cout << std::setprecision(15); for (int i=1; i<t; ++i){ // printf("Case #%d: ", i); sol(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define Go_ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long int #define llu long long unsigned int #define pii pair<int ,int> #define PII pair<ll ,ll> const int Max=1e9+100; const ll MAX=1e18+500; const int sz=2e5+100; string str; int dp[25][4]; int len; int cholo(int id,int last) { if(id>=len) { if(last==0) return 0; else return 100; } int &ret=dp[id][last]; if(~ret) return ret; int mx=cholo(id+1,(last + (str[id]-'0')%3)%3); mx=min(mx, 1+cholo(id+1, last)); return (ret= mx); } int main() { Go_ ll a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,n,mx; char ch; double d; //tring str1,str2,str; cin>>n; mx=0; ll last=0; ll pre=0; ll le=0; for(ll i=1;i<=n;i++) { cin>>x; pre+=x; le=max(le, pre); mx=max(mx, last+le); last+=pre; } cout<<mx<<endl; }
/* ID: gurban1 LANG: C++ TASK: */ #include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn=2e5+5; int n; ll dp[maxn][2]; pair<ll,ll>p[maxn]; ll l[maxn],r[maxn]; int main(){ // freopen("","r",stdin); // freopen("","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 1;i <= n;i++) cin >> p[i].second>>p[i].first; sort(p+1,p+n+1); for(int i = n;i >= 1;i--){ if(p[i].first != p[i+1].first) r[p[i].first] = p[i].second; if(p[i].first != p[i-1].first) l[p[i].first] = p[i].second; } int last = -1; for(int i = n;i >= 1;i--){ if(l[i] == 0) continue; if(last == -1){ dp[i][0] = llabs(l[i]-r[i]) + llabs(r[i]); dp[i][1] = llabs(l[i]-r[i]) + llabs(l[i]); } else { dp[i][0] = llabs(l[i]-r[i]) + min(llabs(r[i]-l[last])+dp[last][0],llabs(r[i]-r[last]) + dp[last][1]); dp[i][1] = llabs(l[i]-r[i]) + min(llabs(l[i]-l[last])+dp[last][0],llabs(l[i]-r[last]) + dp[last][1]); } last = i; } cout<<min(dp[last][0]+llabs(l[last]),dp[last][1]+llabs(r[last])); }
#include <bits/stdc++.h> #define pb push_back #define fst first #define snd second #define fore(i,a,b) for(int i=a,ggdem=b;i<ggdem;++i) #define SZ(x) ((int)x.size()) #define ALL(x) x.begin(),x.end() #define mset(a,v) memset((a),(v),sizeof(a)) #define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; const int MAXN=400005; vector<int> g[MAXN]; // [0,n)->[0,m) int n,m; int mt[MAXN],mt2[MAXN],ds[MAXN]; bool bfs(){ queue<int> q; memset(ds,-1,sizeof(ds)); fore(i,0,n)if(mt2[i]<0)ds[i]=0,q.push(i); bool r=false; while(!q.empty()){ int x=q.front();q.pop(); for(int y:g[x]){ if(mt[y]>=0&&ds[mt[y]]<0)ds[mt[y]]=ds[x]+1,q.push(mt[y]); else if(mt[y]<0)r=true; } } return r; } bool dfs(int x){ for(int y:g[x])if(mt[y]<0||ds[mt[y]]==ds[x]+1&&dfs(mt[y])){ mt[y]=x;mt2[x]=y; return true; } ds[x]=1<<30; return false; } int mm(){ int r=0; memset(mt,-1,sizeof(mt));memset(mt2,-1,sizeof(mt2)); while(bfs()){ fore(i,0,n)if(mt2[i]<0)r+=dfs(i); } return r; } ll a[MAXN],b[MAXN]; int main(){FIN; cin>>n; fore(i,0,n)cin>>a[i]>>b[i]; fore(i,0,n){ g[i].pb(a[i]); g[i].pb(b[i]); } cout<<mm()<<"\n"; return 0; }
#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp" #include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(ll i=0;i<n;i++) #define REP(i,n) for(ll i=1;i<n;i++) #define rev(i,n) for(ll i=n-1;i>=0;i--) #define all(v) v.begin(),v.end() #define P pair<ll,ll> #define len(s) (ll)s.size() template<class T,class U> inline bool chmin(T &a, U b){ if(a>b){a=b;return true;} return false; } template<class T,class U> inline bool chmax(T &a, U b){ if(a<b){a=b;return true;} return false; } constexpr ll inf = 3e18; #line 3 "/home/defineprogram/Desktop/Library/structure/BIT.cpp" template<class T> class BIT{ ll N; vector<T>bit; void add_(ll x,T y){ while(x<=N){ bit[x]+=y;x+=x&-x; } } T sum_(ll x){ T res=0; while(x>0){ res+=bit[x];x-=x&-x; } return res; } public: ll lower_bound(T w){ if(w<=0)return -1; ll x=0; ll k=1;while(k*2<=N)k*=2; for(;k>0;k/=2){ if(x+k<=N&&bit[x+k]<w){ w-=bit[x+k]; x+=k; } } return x; } void add(ll x,T y){add_(x+1,y);} T sum(ll l,ll r){return sum_(r)-sum_(l);} BIT(ll x):N(x),bit(x+1){} }; /* @brief Binary Indexed Tree @docs docs/BIT.md */ #line 3 "main.cpp" int N; int A[200005],B[200005]; int main(){ cin.tie(0);ios::sync_with_stdio(false); cin>>N; rep(i,N){ cin>>A[i]; B[A[i]]=i+1; } set<int>st; rep(i,N-1){ st.insert(A[i]); if(st.upper_bound(i+1)==st.end()||++st.upper_bound(i+1)!=st.end()){ cout<<"-1\n";return 0; } } st.clear(); rev(i,N){ if(!i)break; st.insert(A[i]); if(st.upper_bound(i)==st.begin()||--st.upper_bound(i)!=st.begin()){ cout<<"-1\n";return 0; } } BIT<int> bit(N+5); ll cnt=0; rep(i,N){ cnt+=bit.sum(A[i],N+1); bit.add(A[i],1); } if(cnt!=N-1){ cout<<"-1\n";return 0; } rep(i,N){ for(int j=B[i+1];j>i+1;j--){ cout<<j-1<<"\n"; B[A[j-2]]++; swap(A[j-2],A[j-1]); } } }
#include <bits/stdc++.h> #define f first #define s second #define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' using namespace std; typedef long double ld; typedef long long lli; typedef pair<lli,lli> ii; typedef vector<lli> vi; #define deb(x) cout << #x": " << (x) << endl; lli gauss(lli u) { if(u % 2) { lli v = (u - 1LL) / 2LL; return u * v; } lli v = (u - 1LL); u /= 2LL; return u * v; } void solve() { lli n, k; cin >> n >> k; lli lim = 3LL * n; vi cnt(lim + 1LL); lli next = 1; lli lim2; if(n % 2) lim2 = ((n + 1LL) * 3LL) / 2LL; else lim2 = ((n * 3LL) + 2) / 2LL; for(lli i = 3LL; i <= n + 1LL; ++i) { cnt[i] = cnt[i - 1] + next; ++next; } for(lli i = n + 2LL; i <= lim2; ++i) { cnt[i] = cnt[i - 1] + next; next -= 2; } for(lli i = lim; i > lim2; --i) { cnt[i] = cnt[lim - i + 3LL]; } lli x; for(lli i = 3LL; i <= lim; ++i) { if(k > cnt[i]) k -= cnt[i]; else { x = i; break; } } //deb(k); for(lli i = 1LL; i <= n; ++i) { lli left = (x - i); if(left > (2LL * n)) continue; lli cur = (2LL * n) - left + 1LL; cur = min(cur, left - 1LL); //deb(left); //deb(cur); if(k > cur) { k -= cur; continue; } for(lli j = 1LL; j <= n; ++j) { cur = (left - j); if(cur > n) continue; if(k == 1) { cout << i << " " << j << " " << cur << ENDL; return; } else --k; } } } int main(){ _ solve(); return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <cmath> #include <algorithm> #define ll long long #define rep(i, a, b) for (int i = a; i <= (int)b; i ++) #define per(i, a, b) for (int i = (int)a; i >= b; i --) template < typename T > void read(T & x) { x = 0; int flag = 1; char c = getchar(); for(; !isdigit(c); c = getchar()) if(c == '-') flag = -1; for(; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= flag; } using namespace std; const int maxn = 200010; int n, a[maxn], b[maxn]; ll f[maxn]; int main() { scanf("%d", &n); rep(i, 1, n) scanf("%d", &a[i]), a[i] = max(a[i], a[i - 1]); rep(i, 1, n) scanf("%d", &b[i]), f[i] = max(f[i - 1], 1ll * b[i] * a[i]); rep(i, 1, n) printf("%lld\n", f[i]); return 0; }
#include <bits/stdc++.h> #define MAX 200005 using namespace std; int main() { int n; cin >> n; long long a[MAX], b[MAX]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; long long maxA = 0, ans = 0; for (int i = 0; i < n; i++) { maxA = max(maxA, a[i]); ans = max(ans, maxA*b[i]); cout << ans << "\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9+7; const int N = 2e5+10; ll dp[N][17]; int cnt[N][17]; string n; int K; int get(char c) { if(c >= '0' && c <= '9') return c - '0'; return c - 'A' + 10; } ll dfs(int pos, int k, bool limited, bool pre_zero) { if(pos == n.size()) return (k == K && !pre_zero) ? 1 : 0; if(!limited && !pre_zero && dp[pos][k] != -1) return dp[pos][k]; ll ret = 0; int up = limited ? get(n[pos]) : 15; if(limited) { if(pre_zero) { //继续还是0 ret = dfs(pos + 1, k, false, true); //选up ret = (ret + dfs(pos + 1, k + 1, true, false)) % MOD; //选择小于up但不是0的 if(up > 1) ret = (ret + (up - 1) * dfs(pos + 1, k + 1, false, false)) % MOD; } else { for(int i = 0; i <= up; ++i) ret = (ret + dfs(pos + 1, k + (cnt[pos][i] == 0), i == up, false)) % MOD; } } else { if(pre_zero) { //选择0 ret = dfs(pos + 1, k, false, pre_zero); //选择非0 ret = (ret + dfs(pos + 1, k + 1, false, false) * up) % MOD; } else { ret = dfs(pos + 1, k, false, false) * k + dfs(pos + 1, k + 1, false, false) * (up + 1 - k); ret %= MOD; } } if(!limited && !pre_zero) dp[pos][k] = ret; return ret; } int main() { cin >> n >> K; memset(dp, -1, sizeof dp); for(int i = 0; i < n.size(); ++i) { cnt[i + 1][get(n[i])] = 1; for(int j = 0; j < 16; ++j) cnt[i + 1][j] += cnt[i][j]; } cout << dfs(0, 0, true, true) << endl; return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define all(x) x.begin(),x.end() #define mod 1000000007 #define ad(a,b) a=(a+b)%mod; #define X 5010 ll po(ll x,ll y){ ll res=1; for(;y;y>>=1){ if(y&1)res=res*x%mod; x=x*x%mod; } return res; } ll fac[X],ivf[X]; void init(){ fac[0]=1; for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod; for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2); } ll C(ll n,ll k){ return fac[n]*ivf[n-k]%mod*ivf[k]%mod; } #define N 100010 ll calc(ll n,vector<ll> a,vector<ll> b,ll L){ /*cerr<<"calc:"<<n<<" "<<L<<endl; cout<<"A:";rep(i,n)cout<<a[i]<<" ";cout<<endl; cout<<"B:";rep(i,n)cout<<b[i]<<" ";cout<<endl;*/ rep(i,n){ a[i]-=i,b[i]-=i; } L-=n; a.push_back(L); ll res=0; ll k=-1;//index kまで更新したことがある ll val=-1e17;//更新した値 rep(i,n){ if(val==b[i])continue; ll l=k,r=a.size(),mid; while(l<r-1){ mid=(l+r)>>1; if(b[i]<=a[mid])r=mid; else l=mid; } if(r==a.size())return -1; if(a[r]!=b[i])return -1; res+=r-i;//i~r-1 k=r-1,val=b[i]; } //cout<<"res="<<res<<endl; return res; } ll n,L,a[N],b[N]; ll solve(){ cin>>n>>L; L++; rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; vector<ll> A,B; ll tbef=-1; ll hudobef=0; ll ans=0; rep(i,n+1){ ll t=-1; if(i<n){ if(a[i]<b[i])t=0; if(a[i]==b[i])t=1; if(a[i]>b[i])t=2; } else{ a[i]=b[i]=L; t=1; } if(tbef!=t){ if(tbef==0&&t==2)return -1; if(tbef==0){ ll cres=calc(A.size(),A,B,a[i]); if(cres==-1)return -1; ans+=cres; } if(tbef==2){ rep(i,A.size())A[i]*=-1,B[i]*=-1; reverse(all(A)); reverse(all(B)); ll cres=calc(A.size(),A,B,-hudobef); if(cres==-1)return -1; ans+=cres; } A.clear(); B.clear(); tbef=t; } if(t==1){ hudobef=a[i]; } A.push_back(a[i]); B.push_back(b[i]); } return ans; } int main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<solve()<<endl; }
#include<iostream> #include<string> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<map> #include <queue> #include<sstream> #include <stack> #include <set> #include <bitset> #include<vector> #define FAST ios::sync_with_stdio(false) #define abs(a) ((a)>=0?(a):-(a)) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define mem(a,b) memset(a,b,sizeof(a)) #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define rep(i,a,n) for(int i=a;i<=n;++i) #define per(i,n,a) for(int i=n;i>=a;--i) #define endl '\n' #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef pair<ll,ll> PII; const int maxn = 1e5+200; const int inf=0x3f3f3f3f; const double eps = 1e-7; const double pi=acos(-1.0); const int mod = 1e9+7; inline int lowbit(int x){return x&(-x);} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d); inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;} inline ll inv(ll x,ll p){return qpow(x,p-2,p);} inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;} inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; } int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; int main() { ll n = read(); map<string,int> Map; vector<string> item; rep(i,1,n) { string s; cin>>s; Map[s] = 1; item.pb(s); } int flag = 0; rep(i,1,n) { if(item[i-1][0]=='!') { string t(item[i-1],1,item[i-1].size()); if(Map[t]) { cout<<t<<endl; flag = 1; break; } } } if(!flag) cout<<"satisfiable"<<endl; return 0; }
#include<bits/stdc++.h> #define ll long long using namespace std; void solve(){ long long n,m,tot=0,cnt=0; string s,k=""; bool ok=1; cin>>n; map<string,int>mp; for(int i=0;i<n;i++){ cin>>s; if(s[0]=='!'){ mp[s]++; s=s.substr(1); if(mp.find(s)!=mp.end()) k=s; } else{ mp[s]++; s="!"+s; if(mp.find(s)!=mp.end()) k=s.substr(1); } } if(k=="") cout<<"satisfiable"<<endl; else cout<<k<<endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("bhar.txt", "w", stdout); #endif long long q(1);//cin>>q; while(q--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn=3e5+5; int tree[4*maxn]; void upd(int node,int lo,int hi,int p,int val){ if(lo>hi || lo>p || hi<p){ return; } if(lo==hi){ tree[node]^=val; return; } int mid=(lo+hi)/2; upd(node<<1,lo,mid,p,val); upd(node<<1 | 1,mid+1,hi,p,val); tree[node]=tree[node<<1]^tree[node<<1 | 1]; } int get(int node,int lo,int hi,int l,int r){ if(lo>hi || lo>r || hi<l){ return 0; } if(lo>=l && hi<=r){ return tree[node]; } int mid=(lo+hi)/2; return get(node<<1,lo,mid,l,r)^get(node<<1 | 1,mid+1,hi,l,r); } int main() { int n,q; cin>>n>>q; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; upd(1,0,n-1,i,a[i]); } while(q--){ int ty; cin>>ty; if(ty==1){ int i,val; cin>>i>>val; --i; upd(1,0,n-1,i,val); } else{ int u,v; cin>>u>>v; --u,--v; cout<<get(1,0,n-1,u,v)<<'\n'; } } }
#include <bits/stdc++.h> using namespace std; using ll=long long; const int Nmax=200009; int N, M, Q; ll X[Nmax], Y[Nmax]; struct Data {ll a, b, c;}; Data xp[Nmax], yp[Nmax]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); xp[0]={1, 0, 0}; yp[0]={0, 1, 0}; cin>>N; for(int i=1; i<=N; i++) cin>>X[i]>>Y[i]; cin>>M; for(int i=1; i<=M; i++) { Data &x=xp[i-1], &y=yp[i-1]; int op; cin>>op; if(op==1) { xp[i]={y.a, y.b, y.c}; yp[i]={-x.a, -x.b, -x.c}; } else if(op==2) { xp[i]={-y.a, -y.b, -y.c}; yp[i]={x.a, x.b, x.c}; } else if(op==3) { ll p; cin>>p; xp[i]={-x.a, -x.b, 2*p-x.c}; yp[i]={y.a, y.b, y.c}; } else if(op==4) { ll p; cin>>p; xp[i]={x.a, x.b, x.c}; yp[i]={-y.a, -y.b, 2*p-y.c}; } } cin>>Q; while(Q--) { int A, B; cin>>A>>B; cout<<(xp[A].a*X[B]+xp[A].b*Y[B]+xp[A].c)<<" "; cout<<(yp[A].a*X[B]+yp[A].b*Y[B]+yp[A].c)<<"\n"; } return 0; }
#include<bits/stdc++.h> typedef unsigned long long ull; typedef long long ll; #define rep(i,l,r) for(int i=l;i<=r;i++) #define nep(i,r,l) for(int i=r;i>=l;i--) void sc(int &x){scanf("%d",&x);} void sc(int &x,int &y){scanf("%d%d",&x,&y);} void sc(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);} void sc(ll &x){scanf("%lld",&x);} void sc(ll &x,ll &y){scanf("%lld%lld",&x,&y);} void sc(ll &x,ll &y,ll &z){scanf("%lld%lld%lld",&x,&y,&z);} void sc(char *x){scanf("%s",x);} void sc(char *x,char *y){scanf("%s%s",x,y);} void sc(char *x,char *y,char *z){scanf("%s%s%s",x,y,z);} void out(int x){printf("%d\n",x);} void out(ll x){printf("%lld\n",x);} void out(int x,int y){printf("%d %d\n",x,y);} void out(ll x,ll y){printf("%lld %lld\n",x,y);} void out(int x,int y,int z){printf("%d %d %d\n",x,y,z);} void out(ll x,ll y,ll z){printf("%lld %lld %lld\n",x,y,z);} using namespace std; const int N=3e5+5; int n; unordered_map<ll,int>vis; int main() { //freopen("1.in","r",stdin);freopen("1.out","w",stdout); sc(n); vis[0]++; ll sum=0,ans=0; while(n--) { int x;sc(x); if(n&1) sum+=x; else sum-=x; ans+=vis[sum]; vis[sum]++; } out(ans); }
#include <bits/stdc++.h> using i64 = long long; using u32 = unsigned; using u64 = unsigned long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); i64 n; std::cin >> n; std::map<i64, int> count; count[0]++; i64 ans = 0, sum = 0; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (i % 2 == 0) { sum += x; } else { sum -= x; } ans += count[sum]++; } std::cout << ans << "\n"; return 0; }
#include <iostream> using namespace std; int main(){ int a,b,w,n0,n1; cin >> a >> b; cin >> w; n0=(1000*w+b-1)/b; n1=1000*w/a; if(n0<=n1){ cout << n0 << " " << n1 << endl; } else { cout << "UNSATISFIABLE" << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using std::cin; using std::cout; using std::string; using std::vector; using P = std::pair<int, string>; int main() { int n; cin >> n; vector<P> a(n); rep(i, n) cin >> a[i].second >> a[i].first; sort(a.rbegin(), a.rend()); cout << a[1].second << '\n'; return 0; }
#include <iostream> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <sstream> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #include <climits> #define vi vector<int> #define vvi vector<vi> #define pii pair<int,int> #define vpii vector<pii> #define V vector #define pb push_back #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define F first #define S second #define sqr(x) (x)*(x) #define mp make_pair #define mtp make_tuple #define tpii tuple<int,int,int> #define rep(n) for(int i=0;i<n;i++) #define inf INT_MAX #define debug(x) cerr<<x<<'\n'; #define sz(x) (int)((x).size()) #define int long long #define ull unsigned long long #define quick ios_base::sync_with_stdio(0),cin.tie(0);long double nownow=clock(); #define total_time cerr<<(clock()-nownow)/CLOCKS_PER_SEC*1000<<" (ms)\n"; using namespace std; int gcd(int a,int b){return (a%b?gcd(b,a%b):b);} template<typename T>void in(vector<T> &a){for(auto &i:a)cin>>i;} template<typename T>void out(vector<T> &a){for(auto &i:a)cout<<i<<' ';cout<<'\n';} template<typename T>void out(multiset<T> &a){for(auto i:a)cout<<i<<' ';cout<<'\n';} template<typename T>void out(set<T> &a){for(auto i:a)cout<<i<<' ';cout<<'\n';} #define yn(x) cout<<((x)?"Yes\n":"No\n"); #define YN(x) cout<<((x)?"YES\n":"NO\n"); #define mod (int)(1e9+7) int n; int bit[300001]; void update(int x){ while(x<=n){ bit[x]++; x+=x&(-x); } } int query(int x){ int sum=0; while(x){ sum+=bit[x]; x-=x&(-x); } return sum; } signed main(){ quick cin>>n; vi a(n); int last=0; for(int i=0;i<n;i++){ cin>>a[i]; last+=query(n)-query(a[i]+1); update(a[i]+1); } for(int i=0;i<n;i++){ cout<<last<<'\n'; last+=n-a[i]*2-1; } total_time return 0; }
#include <bits/stdc++.h> #define f first #define s second #define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' #define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) using namespace std; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; typedef long long lli; typedef pair<int,int> ii; typedef vector<int> vi; #define deb(x) cout << #x": " << (x) << endl; lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);} lli lcm(lli a, lli b){ if(!a || !b) return 0; return a * b / gcd(a, b); } int popcount(lli x) { return __builtin_popcountll(x); } // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);} lli poww(lli a, lli b){ lli res =1; while(b){ if(b&1) res = res * a; a = a*a; b/=2; } return res; } vvc<int> graph(int n, int m, bool dir=1){ vv(int,v,n+1,0); fore(i,0,m){ int a,b; cin>>a>>b; v[a].pb(b); if(dir)v[b].pb(a); } return v; } template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2; // ---- コーディングはここから! ('-')7 using T = array<lli,2>; T ini = {0,0}, bad = {0,0}; using L = lli; L nulo = 0; struct LazyST{ #define mid (l + r)/2 #define left(u) (u + 1) #define right(u) (u + ((mid - l + 1) << 1)) vector<T>st; vector<L>lazy; T op(T a, T b){return T{a[0]+b[0],a[1]+b[1]}; } // operacion de merge T cmp(T a, T b){return T{a[0]+b[0],a[1]+b[1]}; } //operacion en query LazyST(int n):st(2*n,ini), lazy(2*n,nulo){} void push(int u, int l, int r){ if(l<r and lazy[u]!=nulo){ st[left(u)][1] += (mid-l+1)*lazy[u]; st[right(u)][1] += (r-mid)*lazy[u]; lazy[left(u)] += lazy[u]; lazy[right(u)]+= lazy[u]; lazy[u]= nulo; } } void update(int u, int l, int r, int ll, int rr, lli x){ push(u, l, r); if(r < ll or l > rr or l>r) return; if(ll <= l and r <= rr){ if(ll==rr)st[u][0]=1; st[u][1]+=(r-l+1)*x; lazy[u]=x; return; } update(left(u), l, mid, ll, rr, x); update(right(u), mid + 1, r, ll, rr, x); st[u] = op(st[left(u)],st[right(u)]); } T query(int u, int l, int r, int ll, int rr){ push(u, l, r); if(r < ll or l > rr or l>r) return bad; if(ll <= l and r <= rr)return st[u]; return cmp(query(left(u),l,mid,ll,rr),query(right(u),mid+1,r,ll,rr)); } }; void solve(){ int n; cin>>n; LazyST st(n); vi v(n); fore(i,0,n)cin>>v[i]; for(int i = n-1;i>=0;i--){ int a = v[i]; auto q1 = st.query(0,0,n-1,0,a); st.update(0,0,n-1,a,a,q1[0]); // menores a la derecha de mi. } cout<<st.query(0,0,n-1,0,n-1)[1]<<ENDL; fore(i,0,n-1){ int a = v[i]; auto q2 = st.query(0,0,n-1,a,a); st.update(0,0,n-1,a,a,-q2[1]); if(a!=n-1)st.update(0,0,n-1,a+1,n-1,1); cout<<st.query(0,0,n-1,0,n-1)[1]<<ENDL; } } int main(){_ //int t; cin>>t; while(t--) solve(); }
//#pragma GCC optimize("Ofast,unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long llo; #define mp make_pair #define pb push_back #define a first #define b second #define endl '\n' llo n,m; llo it[501][501]; llo ans[501]; llo ans2[501]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n; llo cur=1; llo ind=0; for(int i=1;i<=n;i++){ if(i==cur){ ind++; cur*=2; } cout<<ind<<" "; } cout<<endl; return 0; }
#include <bits/stdc++.h> #define be(v) (v).begin(),(v).end() #define pb(q) push_back(q) #define rep(i, n) for(int i=0;i<n;i++) #define all(i, v) for(auto& i : v) using namespace std; vector<int> div(int n){ vector<int> v; for(int i=1;i*i<=n;i++){ if(n % i == 0){ if(n != i * i) v.pb(n / i); v.pb(i); } } return v; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> ans(n + 1, 0); for(int i=1;i<=n;i++){ vector<int> v(60, 1); all(e, div(i)) v[ans[e]] = 0; for(int j=1;j<=60;j++){ if(v[j]){ ans[i] = j; break; } } } rep(i, n) cout << ans[i + 1] << " "; return 0; }
//#define MULTICASES #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a,b) for(i=(a);i<=(b);++i) #define per(i,a,b) for(i=(a);i>=(b);--i) #define REP(i,a,b) for(i=(a);i< (b);++i) #define PER(i,a,b) for(i=(a);i> (b);--i) #define ERR(...) fprintf(stderr,__VA_ARGS__) inline void ac(); int main(){ #ifdef MULTICASES int t;scanf("%d",&t);while(t--)ac(); #else ac(); #endif return 0; } const int MAXN=5005; const ll p=998244353ll; ll C[MAXN][MAXN],Z[MAXN],D[MAXN]; inline void ac(){ int n,m,i,j,a,b;ll t; scanf("%d%d",&n,&m); if(m&1){puts("0");return;} m>>=1; rep(i,0,n){ C[i][0]=1ll; rep(j,1,i)C[i][j]=(C[i-1][j]+C[i-1][j-1])%p; } rep(i,0,n>>1)Z[i]=C[n][i<<1]; D[0]=1ll; for(j=1;j!=4096;j<<=1){ per(i,m,j){ a=0;b=i;t=D[i]; for(++a,b-=j;b>=0;++a,b-=j)t=(t+Z[a]*D[b])%p; D[i]=t; } //rep(i,0,m)ERR("%lld ",D[i]);ERR("\n"); } printf("%lld\n",D[m]); } /* 0. int overflow, array bounds 1. special cases (n=1? n=0?) 2. do smth instead of nothing and stay organized 3. WRITE STUFF DOWN 4. DON'T GET STUCK ON ONE APPROACH 5. STAY CALM AND DON'T GET EXPLODED */
#include<bits/stdc++.h> using namespace std; # define ll long long # define read read1<ll>() # define Type template<typename T> Type T read1(){ T t=0; char k; bool vis=0; do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9'); while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar(); return vis?-t:t; } # define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout) # define mod 998244353 int n,m,fac[5005],inv[5005]; void init(const int N=5000){ fac[0]=inv[0]=inv[1]=fac[1]=1; for(int i=2;i<=N;++i)fac[i]=1ll*i*fac[i-1]%mod,inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod; for(int i=2;i<=N;++i)inv[i]=1ll*inv[i-1]*inv[i]%mod; } ll C(int n,int m){return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;} int f[5005],g[5005]; int main(){init(); n=read,m=read;f[0]=1; for(int i=0;m>>i;++i){ memset(g,0,sizeof(g)); for(int k=0;k<=m;++k) for(int j=0;k+(j<<i)<=m&&j<=n;j+=2) g[k+(j<<i)]=(g[k+(j<<i)]+C(n,j)*f[k])%mod; memcpy(f,g,sizeof(f)); }printf("%d",f[m]); return 0; }
// D - RGB Coloring 2 // sample code // reference: https://atcoder.jp/contests/abc199/submissions/22053625 #include <bits/stdc++.h> using namespace std; #define vec vector using vi = vec<int>; using ll = int64_t; #define rep(i,e) for(int i=0;i<(e);++i) vec<vi> G(20); // 隣接リスト vi used(20, -1); // 彩色順(連結成分ごとに採番) vi colors(20, -1); // 彩色された点 ll dfs(int now){ ll res = 0; for(int to:G[now]) if(colors[to] == colors[now]) return res; // 隣接色かぶり res = 1; for(int to:G[now]){ // 隣接点 if(used[to] == -1) used[to] = used[now] + 1; // 未訪問->彩色順を採番 if(used[to] != used[now] + 1) continue; // 彩色順どおりでない->訪問済み ll tmp = 0; rep(i, 2){ // colors[now] で使っていない 2 色で再帰 colors[to] = (colors[now] + i+1) % 3; // 隣接点を彩色 tmp += dfs(to); // 隣接点へ } colors[to] = -1; // 無色に戻す res *= tmp; if(res == 0) break; // used が採番しきれていない点に注意 } return res; } int main(){ int n, m; cin>>n>>m; rep(i, m){ int a, b; cin>>a>>b; --a, --b; G[a].push_back(b); G[b].push_back(a); } ll ans = 1; rep(i, n){ if(used[i] != -1) continue; // 採番済み->訪問済み used[i] = colors[i] = 0; // 開始点の訪問済み&彩色 ans *= dfs(i) * 3; } cout<< ans <<endl; }
#include <bits/stdc++.h> #include <sys/time.h> #pragma GCC optimize "O3,omit-frame-pointer,inline" #define REP(i, n) for(int i = 0;i < n;i++) #define ll long long using namespace std; //typedef vector<unsigned int>vec; //typedef vector<ll>vec; //typedef vector<vec> mat; typedef pair<int, int> P; typedef pair<ll,ll> LP; //const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; //const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const int INF = 1000000000; const ll LINF = 1000000000000000000;//1e18 const ll MOD = 1000000007; const double PI = acos(-1.0); const double EPS = 1e-10; 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; } //template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;}; constexpr ll CYCLES_PER_SEC = 2800000000; constexpr double TL = 1990; double get_ms() { struct timeval t; gettimeofday(&t, NULL); return (double)t.tv_sec * 1000 + (double)t.tv_usec / 1000; } constexpr int dh[4] = {-1, 0, 1, 0}; constexpr int dw[4] = {0, 1, 0, -1}; constexpr char MOVE[4] = {'U', 'R', 'D', 'L'}; uint32_t XorShift(void) { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } double Prob(void){ double ret = (double)XorShift() / UINT_MAX; return ret; } namespace AHC004{ int N, M; vector<string> S; void input(){ cin >> N >> M; S.resize(M); REP(i,M) cin >> S[i]; } void output(){ REP(i,N) cout << string(N, 'A') << endl; } void main(){ input(); output(); } } int main(){ cin.tie(0); ios::sync_with_stdio(false); AHC004::main(); }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N),P(N),X(N); for (int i=0;i<N;i++) { cin >> A.at(i) >> P.at(i) >> X.at(i); } vector<int> price(N); bool judge=false; for (int i=0;i<N;i++) { if (A.at(i)>=X.at(i)) { } else { judge=true; price.at(i)=P.at(i); } } if (judge) { sort(price.begin(),price.end()); for (int i=0;i<N;i++) { if (price.at(i)==0) { } else { cout << price.at(i) <<endl; break; } } } else { cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int a ; cin>>a; vector<vector<int>> s(a,vector<int>(3)); for(int i =0;i<a;i++){ for(int j =0;j<3;j++){ cin>>s.at(i).at(j); }} int ans=s.at(0).at(1); bool bo =true; for(int i =0;i<a;i++){ if(s.at(i).at(2)-s.at(i).at(0)>0){ ans=min(s.at(i).at(1),ans); bo=false; } } if(!bo)cout<<ans<<endl; if(bo)cout<<"-1"<<endl; }
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define db double #define rep(x,a,b) for(int x=(a);x<=(b);x++) #define per(x,a,b) for(int x=(a);x>=(b);x--) #define reP(x,a,b) for(int x=(a);x<(b);x++) #define Per(x,a,b) for(int x=(a);x>(b);x--) #define scf(a) scanf("%d",&a) #define scfll(a) scanf("%lld",&a) #define scfdb(a) scanf("%lf",&a) #define ptf(a) printf("%d",a) #define ptfll(a) printf("%lld",a) #define ptfsp(a) printf("%d ",a) #define ptfllsp(a) printf("%lld ",a) #define pli(a,b) make_pair(a,b) #define pb push_back #define el puts("") #define FS first #define SE second #define PI acos(-1) #define ls (pos<<1) #define rs (pos<<1|1) /*ios::sync_with_stdio(false); freopen("in.txt","r",stdin); freopen("1.out","w",stdout);*/ using namespace std; const ll mod=1e9+7; const int maxn=1e6+5; ll a[maxn]; int main(){ ll n;scfll(n); int cnt=0; for(ll i=1;i*i<=n;i++){ if(n%i==0){ a[++cnt]=i; if(n/i!=i) a[++cnt]=n/i; } } sort(a+1,a+cnt+1); rep(i,1,cnt) ptfll(a[i]),el; }
#include<bits/stdc++.h> using namespace std; using ll=long long; #define fr(i,n) for(int i=0;i<(n);++i) #define Fr(i,n) for(int i=1;i<=(n);++i) #define ifr(i,n) for(int i=(n)-1;i>=0;--i) #define iFr(i,n) for(int i=(n);i>0;--i) using Graph = vector<vector<int>>; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); // 頂点数と辺数 int N,k; cin >> N>>k; int arr[N][N]; fr(i,N){ fr(l,N){ int a; cin>>a; arr[i][l]=a; } } // グラフ入力受取 (ここでは無向グラフを想定) ll conbu[50]; conbu[0]=1; Fr(i,49){ conbu[i]=((conbu[i-1]*(i+1))%998244353); } bool arrx[N][N]={}; bool arry[N][N]={}; fr(i,N){ fr(l,N){ for(int g=i+1;g<(N);++g){ if(arr[i][l]+arr[g][l]>k) arrx[i][g]=1; } for(int g=l+1;g<(N);++g){ if(arr[i][l]+arr[i][g]>k) arry[l][g]=1; } } } Graph Gx(N); Graph Gy(N); fr(i,N){ for(int l=i+1;l<(N);++l){ if(arrx[i][l]==0){ Gx[l].push_back(i); Gx[i].push_back(l); } if(arry[i][l]==0){ Gy[l].push_back(i); Gy[i].push_back(l); } } } vector<int> distx(N, -1); queue<int> que; ll ans=1; int count=1; for (int v = 0; v < N; ++v) { if (distx[v] != -1) continue; // v が探索済みならスルー distx[v] = 0, que.push(v); while (!que.empty()) { int v = que.front(); que.pop(); for (auto nv : Gx[v]) { if (distx[nv] == -1) { distx[nv] = distx[v] + 1; que.push(nv); ++count; } } } ans=(ans*conbu[count-1])%998244353; count=1; } vector<int> disty(N, -1); for (int v = 0; v < N; ++v) { if (disty[v] != -1) continue; // v が探索済みならスルー disty[v] = 0, que.push(v); while (!que.empty()) { int v = que.front(); que.pop(); for (auto nv : Gy[v]) { if (disty[nv] == -1) { disty[nv] = disty[v] + 1; que.push(nv); ++count; } } } ans=(ans*conbu[count-1])%998244353; count=1; } cout<<ans; }
#include<bits/stdc++.h> #define inf 0x3f3f3f3f #define ll long long #define maxn 300005 using namespace std; int n,maxx; int vis[maxn]; struct nod{ int id[2],val; }x[maxn],y[maxn],ans[10]; int cmp(nod x,nod y){return x.val>y.val;} int main() { cin>>n; for(int i=1;i<=n;i++){ cin>>x[i].val>>y[i].val; x[i].id[0]=y[i].id[0]=i; } int m1,m2; sort(x+1,x+1+n,cmp); sort(y+1,y+1+n,cmp); ans[1].val=x[1].val-x[n].val,ans[1].id[0]=x[1].id[0],ans[1].id[1]=x[n].id[0]; ans[2].val=y[1].val-y[n].val,ans[2].id[0]=y[1].id[0],ans[2].id[1]=y[n].id[0]; ans[3].val=x[2].val-x[n].val,ans[3].id[0]=x[2].id[0],ans[3].id[1]=x[n].id[0]; ans[4].val=x[1].val-x[n-1].val,ans[4].id[0]=x[1].id[0],ans[4].id[1]=x[n-1].id[0]; ans[5].val=y[2].val-y[n].val,ans[5].id[0]=y[2].id[0],ans[5].id[1]=y[n].id[0]; ans[6].val=y[1].val-y[n-1].val,ans[6].id[0]=y[1].id[0],ans[6].id[1]=y[n-1].id[0]; int id1,id2; sort(ans+1,ans+1+10,cmp); for(int i=1;i<=10;i++) { if(i==1) { id1=ans[1].id[0],id2=ans[1].id[1]; } if(ans[i].id[0]==id1&&ans[i].id[1]==id2)continue; if(ans[i].id[1]==id1&&ans[i].id[0]==id2)continue; cout<<ans[i].val; break; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define P pair<int, int> #define fi first #define se second #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() #define pb push_back template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } constexpr int INF = 1000000000; constexpr ll llINF = 1000000000000000000; constexpr int mod = 1000000007; constexpr double eps = 1e-10; const double pi = acos(-1); int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; int Random(int mi, int ma) { random_device rnd; mt19937 mt(rnd()); // 32bit //[mi,ma] uniform_int_distribution<int> engine(mi, ma); return engine(mt); } /* vector<vector<ll>>C,sC; void init_comb(int n,int m){ C.resize(n+1,vector<ll>(m+1,0)); sC.resize(n+1,vector<ll>(m+1,0)); C[0][0]=1; for(int i=1;i<=n;i++){ C[i][0]=1; for(int j=1;j<=m;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } rep(i,n+1){ rep(j,m){ sC[i][j+1]=(sC[i][j]+C[i][j])%mod; } } }*/ bool prime(int a) { if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0) return false; } return true; } vector<int> primes; void init_prime(int n) { primes.push_back(2); for (int i = 3; i <= n; i += 2) { bool f = true; for (int j : primes) { if (j * j > i) break; if (i % j == 0) { f = false; break; } } if (f) primes.push_back(i); } } ll modpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res *= a; res %= mod; } a *= a; a %= mod; b >>= 1; } return res; } vector<ll> inv, fact, factinv; void init_fact(int n) { inv.resize(n + 1); fact.resize(n + 1); factinv.resize(n + 1); inv[0] = 0; inv[1] = 1; fact[0] = 1; factinv[0] = 1; for (ll i = 1; i <= n; i++) { if (i >= 2) inv[i] = mod - ((mod / i) * inv[mod % i] % mod); fact[i] = (fact[i - 1] * i) % mod; factinv[i] = factinv[i - 1] * inv[i] % mod; } } ll _inv(ll a, ll m = mod) { // gcd(a,m) must be 1 ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll comb(int a, int b) { if (a < b) return 0; if (a < 0) return 0; return fact[a] * factinv[a - b] % mod * factinv[b] % mod; } ll multicomb(int a, int b) { return comb(a + b - 1, b); } void solve() { int n; cin >> n; string s, t; cin >> s >> t; if (count(all(s), '1') != count(all(t), '1')) { cout << "-1" << endl; return; } int cnt = count(all(s), '0'); int r = cnt, a = 0; rep(i, s.size()) { if (s[i] == '0') { if (t[i] == '0') { if (a == 0) r--; } else { a++; } } else if (t[i] == '0') { a--; } } cout << r << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); // int t;cin>>t;while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll powk(ll a, ll n = 2){ ll r = 1; for(ll i = 0;i < n;i++){ r *= a; } return r; } int main() { string n; cin >> n; ll mc = (ll)n.size(), ans = 0; bool f = false; for(ll i = 0;i < 1 << ((ll)n.size());i++){ ll cnt = 0, sum = 0; for(ll j = 0;j < (ll)n.size();j++){ if(i & (1<<j)){ sum += n[j] - '0'; } else{ cnt++; } } if(sum % 3 == 0 && mc > cnt){ f = true; mc = cnt; ans = cnt; } } if(f){ cout << ans; } else{ cout << -1; } return 0; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> // #include <atcoder/all> // g++ main.cpp -std=c++17 -I . using namespace std; // using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;} template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;} using ll=long long; const int inf = INT_MAX / 2; const ll INF = 1LL << 60; const int MOD = 1000000007; //cout << fixed << setprecision(15) << x << endl; //////////////////////////////////////////////////////////////////////////////////////////// int main() { int n;cin>>n; vector<int> a(n); for(auto&i:a) { cin>>i; i %= 200; } if(n>8) n=8; map<int, int> m; int first; int second; bool can = true; for(int i = 1; i < (1<<n); i++) { int temp = 0; rep(j, n) { if(i&(1<<j)) { temp += a[j]; temp %= 200; } } if(m[temp]) { first = m[temp]; second = i; break; } m[temp] = i; if(i == (1<<n)-1) can = false; } if(can) { cout << "Yes" << endl; cout << __builtin_popcount(first); rep(i, n) { if((first&(1<<i))) cout << " " << i+1; } cout << endl; cout << __builtin_popcount(second); rep(i, n) { // cout << "a:: " << second << endl; if((second&(1<<i))) cout << " " << i+1; } cout << endl; } else { cout << "No" << endl; } }
#include <bits/stdc++.h> #include <string> #include <cmath> #include <sstream> #include <unordered_map> #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORD(i, a, b) for(ll i = (a-1); i >= (b); i--) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define ff first #define ss second #define spre cout<<fixed<<setprecision(15); typedef long long int ll; using namespace std; //ll mod=67280421310721; // ll mod=998244353; ll mod=1000000007; ll INF=1e18; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test=1; // cin>>test; while(test--) { ll n; cin>>n; ll o=2,g; FOR(i,3,n+1) { g=__gcd(o,i); g=i/g; o*=g; } cout<<o+1; } return 0; }
#include <math.h> #include <algorithm> #include <fstream> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; int main() { long long a,b=1,temp[32]={1,2,3,2,5,1,7,2,3,1,11,1,13,1,1,2,17,1,19,1,1,1,23,1,5,1,3,1,29,1,0,0}; cin >> a; for (int i = 0; i < a;i++){ b *= temp[i]; } cout << b + 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) vector<vector<T>> using ll = long long; using P = pair<int,int>; using Pll = pair<ll,ll>; 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{ ll M; vec(ll) S; bool f(ll n){ bool res=true; ll mm=M; ll ten=1; for(int i=S.size()-1; i>=0 ; i--){ mm -= S[i]*ten; if(mm<0) res = false; if(i>0 && M/n<ten) res=false; else ten*=n; if(!res) break; } return res; } ll bs(ll l, ll r){ //cout << l << " " << r << endl; ll res; if(r-l==1) return l; ll n = (r+l)/2; if(f(n)) res=bs(n,r); else res=bs(l,n); return res; } void solve(){ string X; cin >> X; cin >> M; int l = X.length(); rep(i,l) S.push_back(X[i]-48); ll d=0; rep(i,l) chmax(d,S[i]); if(l==1){ if(S[0]>M) cout << 0 << endl; else cout << 1 << endl; return; } ll ans; ans = bs(d,M+100) - d; cout << ans << endl; /* ll n = d+1; ll ans; while(){ vec(ll) mv; ll mm = M; while(mm){ mv.push_back(mm%n); mm/=n; } if( mv.size()>l ){ n++; }else if( mv.size() < l){ ans = n-d-1; }else{ bool f=true; rep(i,l) if(mv[i]<) } } */ /* if(l==1){ if(S[0]>M) cout << 0 << endl; else cout << 1 << endl; return; } double ex = M / double(S[0]); ll nn = floor( pow(ex,1/double(l-1))); cout << nn-d-1 << endl; */ /* ll nn =d+1; //ll nn=1; //ll ex = log10(M)/(l-1); //rep(i,ex) nn*=10; while(1){ ll mm=M; ll ten=1; cout << "nn=" << nn << endl; for(int i=l-1; (i>=0 && mm>=0); i--){ mm -= S[i]*ten; ten *=nn; } if(mm<0){ cout << max(ll(0),nn-d-1) << endl; break; } nn++; } */ } }; int main(){ int testcasenum=1; //cin >> testcasenum; rep1(ti,testcasenum){ Solver solver; solver.solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; using vll = vector<vl>; using Pll = pair<ll, ll>; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define Rep(i,j,n) for (ll i=(ll)(j);i<=(ll)(n);++i) #define all(v) v.begin(), v.end() #define sz(x) ((int) x.size()) #define eb emplace_back #define pb push_back #define mp make_pair #define mt make_tuple #define F first #define S second const int MOD = 1e9+7; const int mod = 998244353; const ll INF = 2e15; template<class T> void print(const T& t){ cout << t << 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; } ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} /* ll ti=clock(); cout<<("Execution Time: %.4lf sec", 1.0 * (clock() - ti) / CLOCKS_PER_SEC)<<endl; */ int main(){ ll n; cin>>n; vll rest(200,vl(0));//1-indexの番号保管 vl a(n); ll x,y; rep(i,n){ cin>>a[i]; a[i]%=200; if(sz(rest[a[i]])==0){ rest[a[i]].pb(i+1); } else{ print("Yes"); x=1; y=1; cout<<x<<" "<<rest[a[i]][0]<<endl; cout<<y<<" "<<i+1<<endl; return 0; } } Rep(i,1,199){ if(sz(rest[i])!=1){ continue; } Rep(j,1,i-1){ if(sz(rest[j])>0){ if(sz(rest[(i+j)%200])>0){ vl ans_x,ans_y; print("Yes"); for(ll X:rest[(i+j)%200]){ ans_x.pb(X); } for(ll Y:rest[i]){ ans_y.pb(Y); } for(ll Y:rest[j]){ ans_y.pb(Y); } sort(all(ans_x)); sort(all(ans_y)); cout<<sz(rest[(i+j)%200])<<" "; for(ll X:ans_x){ cout<<X<<" "; } cout<<endl; cout<<sz(rest[i])+sz(rest[j])<<" "; for(ll Y:ans_y){ cout<<Y<<" "; } return 0; } else{ for(ll k:rest[i]){ rest[(i+j)%200].pb(k); } for(ll k:rest[j]){ rest[(i+j)%200].pb(k); } } } } } print("No"); }
#include <bits/stdc++.h> using namespace std; int main() { int N, W; cin >> N >> W; int result = N / W; cout << result << endl; }
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int> >; typedef long long ll; #define REP(i, n) for(ll i = 0; i < ll(n); i++) #define REPD(i, n) for(ll i = n-1; i >= 0; i--) #define FOR(i, a, b) for(ll i = a; i < ll(b); i++) #define FORD(i, a, b) for(ll i = a; i > ll(b); i--) #define ALL(v) v.begin(), v.end() #define START ios::sync_with_stdio(false);cin.tie(0); int main() { START int N, W; cin >> N >> W; cout << N / W << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, s, n) for (ll i = s; i < (ll)(n); i++) int main() { ll N; cin >> N; cout << (100 - (N % 100)) << endl; }
#include<iostream> #include<iomanip> #include<algorithm> #include<math.h> #include<vector> using namespace std; int main() { int x; cin >> x; cout << 100-(x % 100); return 0; }
/* author : sgupta_2001 */ #include<bits/stdc++.h> using namespace std; using db = double; using ll = long long; using ld = long double; using ull = unsigned long long; //containers #define sz(x) int((x).size()) #define bg(x) begin(x) #define all(x) bg(x), end(x) #define rall(x) x.rbegin(), x.rend() #define sor(x) sort(all(x)) #define rsz resize #define ins insert #define ft front() #define bk back() #define pb push_back #define eb emplace_back #define pf push_front //pairs #define mp make_pair #define ff first #define ss second //loops #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 each(x, a) for(auto &x : a) //popular constants const int mod = 1e9 + 7; //998244353; const ll inf = 1e18; const ld pie = acos((ld) - 1); //grid problems const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; //modulo operations ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} #ifdef SGUPTA_2001 #define deb(x) cerr<< #x <<" "; __print(x); cerr<<'\n'; #else #define deb(x); #endif //debug containers template<class T> void __print(T x) {cerr<<x;} template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";} template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; } template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";} template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";} template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";} template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";} template<typename... T> void read(T&... args) { ((cin>>args),...);} template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';} void solve() { int a, b, c; read(a, b, c); int ans = 0; ans = max(ans, a + b); ans = max(ans, c + b); ans = max(ans, a + c); printf("%d\n", ans); return; } int main() { #ifdef SGUPTA_2001 freopen("input.txt", "r", stdin); freopen("/home/shivansh/Desktop/error.txt", "w", stderr); #endif auto start = std::chrono::high_resolution_clock::now(); int t; t=1; while(t--) solve(); auto stop = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start); // cerr << "Time taken : " << ((long double)duration.count())/((long double) 1e9) <<"s "<< '\n'; /* STUFF TO LOOK: 1. Check the constraints 2. Corner cases(n == 0 || n == 1) 3. Variables in loops 4. Make sure two ints aren’t multiplied to get a long long */ return 0; }
// NARUTO TRAINING TO BE RED #include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() #define pi pair<ll, ll> #define ll long long #define ar array const ll MOD = 1e9+7; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const int N = 3e5 + 1000; //--------------------------------------------// ll powmod(ll base, ll exp, ll MOD) { ll ans = 1; while (exp) { if (exp & 1) ans = (ans * base) % MOD; base = (base * base) % MOD; exp >>= 1; } return ans; } ll modInverse(ll n, ll p) { return powmod(n, p - 2, p); } //ll f[N]; //ll choose(ll k, ll n) {if(k > n) return 0; return (f[n] * modInverse((f[n - k] * f[k]) % MOD, MOD)) % MOD; } void test_case() { int n; cin>>n; int ans =n; for(int i = 1; i <= n; ++i) { int old = i; while(old > 0) { if(old % 10 == 7) { --ans; break; } old /= 10; } if(old % 10 == 7) continue; old = i; while(old > 0) { if(old % 8 == 7) { --ans; break; } old /= 8; } } cout<<ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt; //cin>>tt; tt=1; while(tt--) { test_case(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll INF = 1e18; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { ll n, x; cin >> n >> x; vector<ll> a(n); rep(i, n) cin >> a[i]; map<ll, ll> m; m[x] = 1; for(int i = n-1; i >= 0; i--) { map<ll, ll> tmp; ll t; if(i == n-1) t = INF; else t = a[i+1] / a[i]; // (-t, t) for(auto p : m) { ll k = p.first; ll v = p.second; ll num = k % a[i]; if(abs(k) / a[i] < t) tmp[num] += v; //cout << a[i] << " " << k << " " << v << " " << num << endl; if(abs(k) / a[i] < t-1 && num != 0) { if(num < 0) tmp[num+a[i]] += v; else tmp[num - a[i]] += v; } } swap(m, tmp); } ll ans = 0; for(auto i : m) ans += i.second; cout << ans << endl; }
#include <bits/stdc++.h> // clang-format off using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18; void print0() {} template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);} void print() { print0("\n"); } template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);} // clang-format on using pll = pair<ll, ll>; ll N,X; vector<ll>A; vector<map<ll, ll>> memo(52); ll recur(ll rest, ll i) { if(rest==0){ return 1; } if(memo[i].count(rest)){ return memo[i][rest]; } if(rest>0 && rest%A[i]==0){ memo[i][rest] = 1; } if(rest>0 && rest%A[i]!=0){ ll ansrec = 0; ll nrest1 = rest/A[i]; if (i==N-1 || nrest1 < A[i+1] / A[i]) { ansrec += recur(rest - A[i]*nrest1, i-1); } ll nrest2 = 1+rest/A[i]; if (i==N-1 || nrest2 < A[i+1] / A[i]) { ansrec += recur(rest - A[i]*nrest2, i-1); } memo[i][rest] = ansrec; } ll rrest = -rest; if(rrest>0 && rrest%A[i]==0){ memo[i][rest] = 1; } if(rest<0 && rrest%A[i]!=0){ ll ansrec = 0; ll nrest1 = rrest / A[i]; if (i==N-1 || nrest1 < A[i+1] / A[i]) { ansrec += recur(rest + A[i]*nrest1, i-1); } ll nrest2 = 1 + rrest / A[i]; if (i==N-1 || nrest2 < A[i+1] / A[i]) { ansrec += recur(rest + A[i]*nrest2, i-1); } memo[i][rest] = ansrec; } return memo[i][rest]; } int main() { cin >> N>>X; A.resize(N); for (ll i = 0; i < N; i++) { cin>>A[i]; } print(recur(X,N-1)); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--) #define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" template<class T> void chmax(T &a, const T b){ a = max(a, b); } template<class T> void chmin(T &a, const T b){ a = min(a, b); } long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll a, b, c; cin >> a >> b >> c; cout << (((a * a + b * b) < (c * c)) ? "Yes" : "No") << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; cin >> a >> b >> c ; if (a*a+b*b < c*c) cout << "Yes"; else cout << "No"; }
#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; } /*----------------------------------------------------------------------------------------------------*/ int main(){ int a, b; cin >> a >> b; int c = a+b; int ans; if(c >= 15 && b >= 8) ans = 1; else if(c >= 10 && b >= 3) ans = 2; else if(c >= 3) ans = 3; else ans = 4; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A,B; cin >> A >> B; cout << 2*A+100 - B << endl; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define x first #define y second #define FOR(i, m, n) for (ll i(m); i < n; i++) #define DWN(i, m, n) for (ll i(m); i >= n; i--) #define REP(i, n) FOR(i, 0, n) #define DW(i, n) DWN(i, n, 0) #define F(n) REP(i, n) #define FF(n) REP(j, n) #define D(n) DW(i, n) #define DD(n) DW(j, n) using ll = long long; using ld = long double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vtll = vector<tll>; using gr = vector<vll>; using wgr = vector<vpll>; void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); } void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); } template<typename T,typename U> ostream& operator<<(ostream& os, const pair<T,U>& p) { cerr << ' ' << p.x << ',' << p.y; return os; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for(auto x: v) os << ' ' << x; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for(auto x: v) os << ' ' << x; return os; } template<typename T,typename U> ostream& operator<<(ostream& os, const map<T,U>& v) { for(auto x: v) os << ' ' << x; return os; } struct d_ { template<typename T> d_& operator,(const T& x) { cerr << ' ' << x; return *this;} } d_t; #define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; } #define deb(X ...) dbg(#X, "=", X); #define EPS (1e-10) #define INF (1LL<<61) #define CL(A,I) (memset(A,I,sizeof(A))) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() void solve() { ll n; cin >> n; map<ll,bool> m; F(n) { ll x; cin>>x; m[x]^=1; } bool has = 0; for(auto &i: m) has |= i.y; if((n%2)==0 && has) cout << "First" << endl; else cout << "Second" << endl; } int main(void) { ios_base::sync_with_stdio(false); ll _T; cin >> _T; REP(_,_T) solve(); return 0; }
#include<bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; ll rd(){ ll x = 0; int f = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void solve(){ int n = rd(); map<int,int> mp; for(int i = 0;i < n;i++) mp[rd()]++; int num = mp.begin() -> se; if(n & 1) { puts("Second"); } else{ int flag = 0; for(auto it:mp){ if(it.se & 1) { flag = 1; } } if(!flag) puts("Second"); else puts("First"); } } int main(){ int T = rd(); while(T--) solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) const long double INF = 1e15; int N = 30*30; int L = 30; // (i,j) i個下 , j個右 int ind(int a,int b){ return a*L + b; } int Vi(int v){return v/L;} int Vj(int v){return v%L;} bool in(int i,int j,int si,int sj,int ti,int tj){ if(i<min(si,ti) || max(si,ti)<i) return false; if(j<min(sj,tj) || max(sj,tj)<j) return false; return true; } int length(int v1,int v2){return abs(Vi(v1)-Vi(v2))+abs(Vj(v1)-Vj(v2));} vector<int> VinRectangle(int si,int sj,int ti,int tj){ // 四角形の中の頂点を始点からマンハッタン距離が小さい順に vector< pair<int,int> > Vs; FOR(i,min(si,ti),max(si,ti)+1){ FOR(j,min(sj,tj),max(sj,tj)+1){ Vs.push_back( { abs(i-si)+abs(j-sj) , ind(i,j) } ); } } sort(Vs.begin(), Vs.end()); vector<int> res; for(auto v:Vs) res.push_back(v.second); return res; } //vector< vector<string> > S(N,vector<string>(N)); vector< vector<long double> > Len(N,vector<long double>(N,INF)); //vector< vector<char> > C(N,vector<char>(N,'.')); vector< vector<string> > route(N,vector<string>(N)); // Len の時のルートを示す文字列 void first_do_in_main(){ FOR(i,0,N){ int ai = Vi(i); int aj = Vj(i); if( in(ai+1,aj,0,0,L-1,L-1) ){ Len[i][ind(ai+1,aj)] = 5000.0; route[i][ind(ai+1,aj)] = "D"; } if( in(ai-1,aj,0,0,L-1,L-1) ){ Len[i][ind(ai-1,aj)] = 5000.0; route[i][ind(ai-1,aj)] = "U"; } if( in(ai,aj+1,0,0,L-1,L-1) ){ Len[i][ind(ai,aj+1)] = 5000.0; route[i][ind(ai,aj+1)] = "R"; } if( in(ai,aj-1,0,0,L-1,L-1) ){ Len[i][ind(ai,aj-1)] = 5000.0; route[i][ind(ai,aj-1)] = "L"; } } } string rev_route(string s){ reverse(s.begin(), s.end()); FOR(i,0,s.size()){ if(s[i] == 'U'){ s[i] = 'D'; }else if(s[i] == 'D'){ s[i] = 'U'; }else if(s[i] == 'R'){ s[i] = 'L'; }else if(s[i] == 'L'){ s[i] = 'R'; } } return s; } string do_calc(int si,int sj,int ti, int tj){ int start,finish; vector<long double> d(N,INF); // startからiまでの距離 d[i] vector<int> pv(N); // pv[i]はiの前の頂点 vector<string> rv(N); // pv[i]からiへどうやって来たか start = ind(si,sj); finish = ind(ti,tj); d[start] = 0; auto Vs = VinRectangle(si,sj,ti,tj); int Nvs = Vs.size(); FOR(i,0,Nvs){ FOR(j,0,i){ // a -> b へのルート int a = Vs[j]; int b = Vs[i]; if(route[a][b].size() > 0 && d[a]+Len[a][b] < d[b]){ d[b] = d[a]+Len[a][b]; pv[b] = a; rv[b] = route[a][b]; } } } vector<string> s; while(finish != start){ s.push_back(rv[finish]); finish = pv[finish]; } string res; reverse(s.begin(), s.end()); for(auto s_ : s) res += s_; return res; } bool check(string s,int si,int sj,int ti,int tj){ for(char x:s){ if(x=='U') si--; if(x=='D') si++; if(x=='R') sj++; if(x=='L') sj--; } return (si==ti)&&(sj==tj); } int main(){ first_do_in_main(); FOR(k,0,1000){ int si,sj,ti,tj; cin >> si >> sj >> ti >> tj; string res = do_calc(si,sj,ti,tj); cout << res << endl; cout << flush; long double be; cin >> be; //assert(check(res,si,sj,ti,tj)); //assert(check(rev_route(res),ti,tj,si,sj)); /*int st = ind(si,sj); int fi = ind(ti,tj); route[st][fi] = res; Len[st][fi] = be; route[fi][st] = rev_route(res); Len[fi][st] = be; */ } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; const int INF = 1 << 30; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int ans = INF; ans = min(ans, a); ans = min(ans, b); ans = min(ans, c); ans = min(ans, d); cout << ans << endl; }
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<vector> #include<functional> #include<algorithm> #include<stdlib.h> #include<string> #include<string.h> #define _USE_MATH_DEFINES #include<math.h> #include<deque> #include<set> #include<map> #include<queue> #include<list> #include<iostream> #include <bitset> using namespace std; typedef long long ll; #define rep(i,a,b) for(auto i=a;i<b;i++) #define rep2(i, a)for(auto i : a) #define all(_x) _x.begin(), _x.end() #define r_sort(_x) sort(_x, std::greater<int>()) #define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n)) #define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end()); #define vvec vector<vector<ll>> #define INF (1ll << 60) ll mod = 1000000007; ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p & 1)a *= x; x *= x; p >>= 1; }return a; } ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p & 1)a = a * x % mod; x = x * x % mod; p >>= 1; }return a; } ll co(ll n, ll k) { ll a = 1; rep(i, 1ll, k + 1) { a *= n - i + 1; a /= i; }return a; } ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; } ll modinv(ll a) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); }u %= mod; if (u < 0) u += mod; return u; } int main(void) { int n, k; cin >> n >> k; vector<vector<int>> a(n, vector<int>(n)); int r = 0, l = 0; rep(i, 0, n)rep(j, 0, n)cin >> a[i][j], r = max(r, a[i][j]); while (r - l > 1) { int m = (r + l) / 2; vector<vector<int>> s(n + 1, vector<int>(n + 1)); rep(i, 0, n) { rep(j, 0, n) { if (a[i][j] > m)s[i + 1][j + 1] = 1; s[i + 1][j + 1] += s[i][j + 1] + s[i + 1][j] - s[i][j]; } } bool ok = false; rep(x, 0, n - k + 1) { rep(y, 0, n - k + 1) { if (s[x + k][y + k] - s[x][y + k] - s[x + k][y] + s[x][y] <= k * k / 2) { ok = true; break; } } if (ok)break; } if (ok)r = m; else l = m; } printf("%d\n", r); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> ii; typedef tuple<ll, ll, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define REP(i,n) for (ll i = 0; i < n; ++i) #define REPR(i,n) for (ll i = n-1; i >= 0; --i) #define FOR(i,m,n) for (ll i = m; i < n; ++i) #define FORR(i,m,n) for (ll i = n-1; i >= m; --i) #define FORE(x,xs) for (const auto& x : xs) #define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++) #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REV(v) reverse(ALL(v)) #define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v)) #define CHMIN(x,y) x = min(x, y) #define CHMAX(x,y) x = max(x, y) #define YES(b) cout << ((b) ? "YES" : "NO") << endl #define Yes(b) cout << ((b) ? "Yes" : "No") << endl const int MAX = 20; int N, M; vii X[MAX]; ll dp[1<<MAX]; int main() { cout << fixed << setprecision(15); cin >> N >> M; REP (i, M) { int x, y, z; cin >> x >> y >> z; X[x].push_back(make_pair(y, z)); } vector<bool> visit(1<<N, false); queue<int> q; q.push(0); dp[0] = 1; while (q.size()) { int state = q.front(); q.pop(); int b = __builtin_popcount(state) + 1; REP (i, N) if ((state & (1<<i)) == 0) { int next = state | (1<<i); bool check = true; FORE (x, X[b]) { if (__builtin_popcount(next&((1<<(x.first))-1)) > x.second) check = false; } if (check) { dp[next] += dp[state]; if (!visit[next]) { q.push(next); visit[next] = true; } } } } cout << dp[(1<<N)-1] << endl; }
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> const int N = 2e5+5; using namespace std; int a[N]; int main() { int n; cin>>n; int m = pow(2,n); for(int i=0;i<m;i++){ cin>>a[i]; } int lmax = 0,rmax = m/2; for(int i=0;i<m/2;i++){ if(a[lmax]<a[i]) lmax = i; } for(int i=m/2;i<m;i++){ if(a[rmax]<a[i]) rmax = i; } if(a[lmax]<a[rmax]) printf("%d",lmax+1); else printf("%d",rmax+1); return 0; }
// Problem: A - Maxi-Buying // Contest: AtCoder - AtCoder Beginner Contest 206(Sponsored by Panasonic) // URL: https://atcoder.jp/contests/abc206/tasks/abc206_a // Memory Limit: 1024 MB // Time Limit: 2000 ms // Author: tooler! // // Powered by CP Editor (https://cpeditor.org) #include<bits/stdc++.h> using namespace std; #define ll long long #define f(i,a,b) for(int i=a;i<=b;i++) inline ll rd() { ll x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c))x=x*10+c-'0',c=getchar(); return x*f; } #define d rd() #define pb push_back double x; int main(){ cin>>x; x*=1.08; if((int)x<206)cout<<"Yay!"; else if((int)x==206)cout<<"so-so"; else cout<<":("; return 0; }
#include <bits/stdc++.h> #define _overload(_1, _2, _3, _4, name, ...) name #define _rep1(Itr, N) _rep3(Itr, 0, N, 1) #define _rep2(Itr, a, b) _rep3(Itr, a, b, 1) #define _rep3(Itr, a, b, step) for (i64(Itr) = a; (Itr) < b; (Itr) += step) #define repeat(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__) #define rep(...) repeat(__VA_ARGS__) using namespace std; using i64 = long long; using u64 = unsigned long long; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); i64 t; cin >> t; auto f = [](string &s, string &t) { int i = 0; int j = 0; while (j < t.size()) { while (i < s.size() && s[i] != t[j]) { i++; } j++; } return i < s.size(); }; repeat(_, t) { i64 n; cin >> n; string s[3]; cin >> s[0] >> s[1] >> s[2]; string cmp[4]; { cmp[0].reserve(2 * n + 1); repeat(i, n) cmp[0].push_back('0'); repeat(i, n) cmp[0].push_back('1'); cmp[0].push_back('0'); } { cmp[1].reserve(2 * n + 1); repeat(i, n) cmp[1].push_back('0'); repeat(i, n) cmp[1].push_back('1'); cmp[1].push_back('1'); } { cmp[2].reserve(2 * n + 1); repeat(i, n) cmp[2].push_back('1'); repeat(i, n) cmp[2].push_back('0'); cmp[2].push_back('0'); } { cmp[3].reserve(2 * n + 1); repeat(i, n) cmp[3].push_back('1'); repeat(i, n) cmp[3].push_back('0'); cmp[3].push_back('1'); } repeat(i, 3) s[i] += s[i]; bool out = false; repeat(i, 4) { if (f(s[0], cmp[i]) && f(s[1], cmp[i]) && f(s[2], cmp[i])) { cout << cmp[i] << '\n'; out = true; break; } } assert(out); } return 0; }
#include<bits/stdc++.h> using namespace std; int read() { int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*w; } string s1,s2,s3; int mp[400010],n; signed main() { int T=read(),dfx=0; while(T--) { n=read(); cin>>s1>>s2>>s3; cout<<0; for(int i=1;i<=n;i++) cout<<1; for(int i=1;i<=n;i++) cout<<0; cout<<'\n'; } return 0; }
#include <iostream> #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #include <vector> #include <algorithm> using ll = long long; #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0) using namespace std; int dx[4] = { 0,0,1,-1 }; int dy[4] = { 1,-1,0,0 }; void solve(ll t) { ll n; cin >> n; vector<vector<ll>> c(n, vector<ll>(n)); ll num = INT_MAX; ll x, y; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> c[i][j]; if (c[i][j] < num) { num = c[i][j]; x = i; y = j; } } } vector<vector<ll>> c1(c); vector<ll> a(n), b(n); vector<ll> tmp = c[x]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] -= tmp[j]; } } //for (int i = 0; i < n; i++) { // for (int j = 0; j < n; j++) { // cout<<c[i][j]<<" "; // } // cout << endl; //} for (ll i = 0; i < n; i++) { int flag = 1; for (ll j = 0; j < n; j++) { if (c[i][j] != c[i][0]) { flag = 0; break; } } if (flag == 1) { a[i] = c[i][0]; } else { cout << "No" << endl; return; } } c = c1; for (ll i = 0; i < n; i++) { for (ll j = 0; j < n; j++) { c[i][j] -= a[i]; } } for (ll j = 0; j < n; j++) { ll flag = 1; for (ll i = 0; i< n; i++) { if (c[i][j] != c[0][j] || c[i][j] < 0) { flag = 0; break; } } if (flag == 1) { b[j] = c[0][j]; } else { cout << "No" << endl; return; } } cout << "Yes" << endl; cout << a[0]; for (ll i = 1; i < n; i++) { cout << " " << a[i] ; } cout << endl; cout << b[0]; for (ll i = 1; i < n; i++) { cout << " " << b[i] ; } cout << endl; } int main(void) { ll t = 0; //cin >> t; for (ll i = 1; i <= t; i++) {solve(i);} solve(t); return 0; }
#include <bits/stdc++.h> #define rep(a,n) for (ll a = 0; a < (n); ++a) using namespace std; //using namespace atcoder; using ll = long long; typedef pair<ll,ll> P; typedef pair<ll,P> PP; typedef vector<vector<int> > Graph; 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 INF = 1e18; #define SZ(x) ((int)(x).size()) #define debug(v) cout<<#v<<": ",prt(v); template <typename A,typename B> inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";} template <typename A,typename B,typename C> inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";} inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';} template <typename T> inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template<typename T> inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } } template <typename T> inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} /* */ ll n,m,x,y; vector<ll>a,b,t,k; struct edge{ int to,cost,mult; }; vector<edge> g[100010]; vector<ll>dist; void dijkstra(int s){ priority_queue<P,vector<P>,greater<P> > pq; dist.resize(n,INF); dist[s]=0; pq.push({0,s}); while(!pq.empty()){ P p = pq.top();pq.pop(); int v = p.second; if(dist[v]<p.first)continue; for(auto e:g[v]){ ll time = (dist[v]+e.mult-1)/e.mult*e.mult+e.cost; if(dist[e.to]>time){ dist[e.to]=time; pq.push({dist[e.to],e.to}); } } } } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cin >> n >> m >> x >> y; x--;y--; a.resize(m); b.resize(m); t.resize(m); k.resize(m); rep(i,m){ cin>>a[i]>>b[i]>>t[i]>>k[i]; a[i]--;b[i]--; } rep(i,m){ g[a[i]].push_back({b[i],t[i],k[i]}); g[b[i]].push_back({a[i],t[i],k[i]}); } dijkstra(x); if(dist[y]==INF)cout<<-1<<endl; else cout << dist[y] << endl; //debug(dist); return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <cmath> #include <bitset> #include <algorithm> #include <climits> using namespace std; #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("avx,avx2,fma") #define DEBUG printf("Passing [%s] in Line %d\n" , __FUNCTION__ , __LINE__) ; #define Fast ios::sync_with_stdio(false); #define REP(i, n) for (int i=0;i<n;++i) #define FOR(i, a, b) for (int i=a;i<b;++i) #define DWN(i, b, a) for (int i=b-1;i>=a;--i) #define REP_1(i, n) for (int i=1;i<=n;++i) #define FOR_1(i, a, b) for (int i=a;i<=b;++i) #define DWN_1(i, b, a) for (int i=b;i>=a;--i) #define CLR(a, b) memset((a),(b),sizeof((a))) #define sz(A) int((A).size()) #define pb push_back #define fi first #define se second typedef long long lli; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; typedef pair<int, int> P; typedef pair<lli, lli> ii; typedef vector<lli> vi; const int MOD = int(1e9) + 7; const int INF = 0x3f3f3f3f; const lli INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const int MAXN = 50000 + 9; inline int read() { char c = getchar(); int x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } bool isPrime(int u) { for (int i = 2; i * i <= u; ++i) { if (u % i == 0) return false; } return true; } vi primes; lli get(lli u) { lli val = 0; FOR(i, 0, sz(primes))if (u % primes[i] == 0) val += (1LL << i); return val; } lli calc(lli u) { lli val = 1; FOR(i, 0, sz(primes))if (u & (1LL << i)) val *= primes[i]; return val; } int main() { Fast; FOR(i, 2, 51) if (isPrime(i)) primes.pb(i); lli ans = 1LL; for (auto it : primes) ans *= it; lli n; cin >> n; vi v(n); FOR(i, 0, n) cin >> v[i], v[i] = get(v[i]); lli sz = sz(primes); FOR(i, 1, 1LL << sz) { lli x = i; for (auto it : v) if (!(x & it)) x = 0; if (x > 0) ans = min(ans, calc(x)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define float double #define sz 100005 #define all(a) a.begin(), a.end() #define mod 1000000007 using namespace std; #define vi vector<int> #define vvi vector<vector<int>> #define debug cout << "here" << endl; #define f(i, n) for (int i = 0; i < n; ++i) #define pb push_back #define ff first #define ss second #define pi pair<int, int> #define YES cout << "YES" << endl; #define NO cout << "NO" << endl; #define p(x) cout << x << " "; #define pe(x) cout << x << endl; using namespace std; // ------------------------------linked list node------------------------ class Node { public: int val; Node *next; Node(int vale) { val = vale; next = NULL; } }; //--------------------------------linked list class --------------------- class LL { public: Node *head; Node *tail; LL() { head = NULL; tail = NULL; } void insert(int data) { // cout << data << endl; Node *newNode = new Node(data); // cout << newNode->val << endl; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } }; //----------------------------TreeNode ----------------------- class TreeNode { public: int val; TreeNode *left; TreeNode *right; TreeNode(int x) { val = x; left = NULL; right = NULL; } }; //---------------------------fast modulo exponetion ------ --- ll pwr(ll a, ll b) { a = a % mod; if (a == 0) return 0; ll res = 1; while (b > 0) { if (b & 1) { res = (res * a) % mod; } b = b >> 1; a = (a * a) % mod; } return res; } int main() { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t; // cin >> t; t = 1; while (t--) { int n; cin >> n; vector<pair<int, string>> help(n); for (int i = 0; i < n; ++i) { cin >> help[i].second; cin >> help[i].first; } sort(help.begin(), help.end()); cout << help[help.size() - 2].second << endl; } #ifndef ONLINE_JUDGE cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; #endif }
#include "bits/stdc++.h" #define int long long #define endl '\n' using namespace std; typedef long long ll; typedef long double ld; #define db(x) cerr << #x << ": " << x << '\n'; #define read(a) int a; cin >> a; #define reads(s) string s; cin >> s; #define readb(a, b) int a, b; cin >> a >> b; #define readc(a, b, c) int a, b, c; cin >> a >> b >> c; #define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];} #define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];} #define print(a) cout << a << endl; #define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl; #define printv(v) for (int i: v) cout << i << " "; cout << endl; #define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;} #define all(v) v.begin(), v.end() #define sz(v) (int)(v.size()) #define rz(v, n) v.resize((n) + 1); #define pb push_back #define fi first #define se second #define vi vector <int> #define pi pair <int, int> #define vpi vector <pi> #define vvi vector <vi> #define setprec cout << fixed << showpoint << setprecision(20); #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) const ll inf = 1e18; const ll mod = 1e9 + 7; const ll N = 2e5 + 1; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int power (int a, int b = mod - 2) { int res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } //cp-algo int gcd(int a, int b, int& x, int& y) { if (b == 0) { x = 1; y = 0; return a; } int x1, y1; int d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) { g = gcd(abs(a), abs(b), x0, y0); if (c % g) { return false; } x0 *= c / g; y0 *= c / g; if (a < 0) x0 = -x0; if (b < 0) y0 = -y0; return true; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); read(t); while (t--) { readc(n, s, k); int x, y, g; bool status = find_any_solution(n, -k, s, x, y, g); if (!status) {cout << -1 << endl; continue;} n /= g; int z = y/n, ans = inf; if (y - z*n > 0) ans = min(ans, y - z*n); if (y - (z - 1)*n > 0) ans = min(ans, y - (z - 1)*n); print(ans); } }
// Problem: C - Squared Error // Contest: AtCoder - AtCoder Beginner Contest 194 // URL: https://atcoder.jp/contests/abc194/tasks/abc194_c // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include<bits/stdc++.h> typedef long long int ll; #define pb push_back #define pi 3.141592653589793238462643383279 #define int long long #define ff first #define ss second #define endl '\n' #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=(int)x; i<y; i++) #define high_functioning_sociopath ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; const unsigned int M = 998244353; /*_____________________________THE GAME IS ON_____________________________*/ void solve() { int n; cin >> n; vector<int>v(n); for(int i=0;i<n;i++) cin >> v[i]; int suf1[n]={0}; int suf2[n]={0}; suf1[n-1] = v[n-1]; suf2[n-1] = (v[n-1]*v[n-1]); for(int i=n-2;i>=0;i--) { suf1[i] = suf1[i+1] + v[i]; suf2[i] = suf2[i+1] + (v[i]*v[i]); } int res = 0; int j = n-1; for(int i=0;i<n-1;i++) { int a = v[i]*v[i]; a*=j; //cout << suf2[i+1] << " " << suf1[i+1] << endl; int b = suf2[i+1] - (2*v[i]*suf1[i+1]) + a; res+=b; j--; } cout << res ; } int32_t main() { high_functioning_sociopath //pre(); ll t=1; // cin>>t; while(t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define fr(i,n) for(int i = 0; i<n; i++) #define sz(v) (int)(v.size()) #define prin(a) cout << #a << " = " << a << endl #define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl #define all(v) (v).begin(),(v).end() typedef long long ll; #define rmin(a,b) a = min<ll>(a,b) #define rmax(a,b) a = max<ll>(a,b) #define fi first #define se second const int N = 2e5+10; namespace diametro_space{ int n; vector<int> *g; void dfs(int no, int from, int dac, ll d[]){ d[no] = dac; for(auto it : g[no]){ if(it!=from) dfs(it,no,dac+1,d); } } int max_dist_glob = 0; // diametro da arvore int get_most_dist(int no, ll d[]){ dfs(no,-1,0,d); int max_dist_cur = -1; int no_max_dist = -1; fr(i,n){ if(d[i]>max_dist_cur){ max_dist_cur = d[i]; no_max_dist = i; } } max_dist_glob = max(max_dist_glob,max_dist_cur); return no_max_dist; } vector<int> nos_diam; int no0, no1; //retorna diametro em numero de arestas! (ou seja, vertices -1) ll find_diametro(vector<int> _g[N], int _n, ll vd[2][N]){ g = _g; n = _n; max_dist_glob = 0; no1 = get_most_dist(0,vd[0]); no0 = get_most_dist(no1,vd[1]); get_most_dist(no0,vd[0]); fr(i,n){ if(vd[0][i]+vd[1][i]==max_dist_glob) nos_diam.push_back(i); } return max_dist_glob; } }; //end space diametro namespace lca_space{ int nlog; int n; vector<int> *g; int pai[N], dist[N]; //pai do nó i (raiz = -1) int st[N][25]; //sparse table - st[i][j] = pai 2^j niveis acima do nó i void dfs(int no, int from, int dac){ dist[no] = dac; for(auto it : g[no]){ if(it==from) continue; pai[it] = no; dfs(it,no,dac+1); } } void make(vector<int> _g[N], int _n, int root){ g = _g; n = _n; pai[root] = -1; dfs(root,-1,0); nlog = 1; while((1<<nlog)<n) nlog++; assert(nlog<25); fr(i,n) fr(j,nlog+1) st[i][j] = -1; fr(i,n) st[i][0] = pai[i]; for(int j = 1; j<=nlog; j++){ fr(i,n){ int ant_pai = st[i][j-1]; if(ant_pai!=-1) st[i][j] = st[ant_pai][j-1]; } } } int lca(int p, int q){ if(dist[p]<dist[q]) swap(p,q); for(int i = nlog; i>=0;i--){ if(dist[p]-dist[q] >= (1<<i) ) p = st[p][i]; } if(p==q) return p; for(int i = nlog; i>=0; i--){ if(st[p][i]!=st[q][i]){ p = st[p][i]; q = st[q][i]; } } return pai[q]; } int get_dist(int u, int v){ return dist[u]+dist[v]-2*dist[lca(u,v)]; } }; int ehdiam[N]; vector<int> g[N]; ll vdist[2][N]; struct cmp{ bool operator()(int a, int b){ return ehdiam[a]<ehdiam[b]; } }; ll cor = 1; int ultno = -1; ll vcor[N]; void dfs(int no, int from){ if(from!=-1){ cor+=lca_space::get_dist(ultno,no); } vcor[no] = cor; ultno = no; for(auto &it : g[no]){ if(it==from) continue; dfs(it,no); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; fr(i,n-1){ int a, b; cin >> a >> b; a--,b--; g[a].push_back(b); g[b].push_back(a); } //prin(n); diametro_space::find_diametro(g,n,vdist); vector<int> nos_diam = diametro_space::nos_diam; int root = diametro_space::no0; for(auto &i : nos_diam) ehdiam[i] = 1; fr(i,n) sort(all(g[i]),cmp()); lca_space::make(g,n,root); //prinv(nos_diam); dfs(root,-1); fr(i,n){ if(i) cout << " "; cout << vcor[i]; } cout << "\n"; }
#include <bits/stdc++.h> using namespace std; #define deb(k) cerr << #k << ": " << k << "\n"; #define size(a) (int)a.size() #define fastcin cin.tie(0)->sync_with_stdio(0); #define st first #define nd second #define pb push_back #define mk make_pair #define int long long typedef long double ldbl; typedef double dbl; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<double, pii> pdii; typedef pair<ll, ll> pll; typedef map<int, int> mii; typedef vector<int> vint; #define MAXN 300100 #define MAXLG 20 const int inf = 0x3f3f3f3f; const ll mod = 1000000007; const ll linf = 0x3f3f3f3f3f3f3f3f; const int N = 300100; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int d[N]; vint g[N]; int cnt = 1; int ans[N]; int pai[N]; set<int>dia; vint cur; void dfs(int x, int p = -1, int u = 1){ d[x] = u; pai[x] = p; ans[x] = cnt; for(auto v : g[x]){ if(v == p) continue; if(dia.count(v)) continue; cnt++; dfs(v, x, u+1); } if(dia.count(x) && !cur.empty()){ int v = cur.back(); cur.pop_back(); cnt++; dfs(v, x, u+1); } cnt++; } void solve(){ int n; cin>>n; for(int i=1;i<n;i++){ int a,b; cin>>a>>b; g[a].pb(b); g[b].pb(a); } dfs(1); int mx = 0, ind = -1; for(int i=1;i<=n;i++){ if(d[i] > mx){ mx = d[i]; ind = i; } } cnt = 1; int root = ind; dfs(root); mx = 0, ind = -1; for(int i=1;i<=n;i++){ if(d[i] > mx){ mx = d[i]; ind = i; } } int aux = ind; while(aux != -1){ cur.pb(aux); dia.insert(aux); aux = pai[aux]; } cur.pop_back(); cnt = 1; dfs(root); for(int i=1;i<=n;i++) cout<<ans[i]<<" "; cout<<"\n"; // Have you read the problem again? // Maybe you understood the wrong problem } int32_t main(){ fastcin; int t_ = 1; //cin>>t_; while(t_--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; void Main() { ll n; R n; if(n%2) pr("Black"); else pr("White"); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();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,m;cin>>n>>m; struct edge{ ll to,id,rev; }; vector<vector<edge>>g(n); rep(i,0,m){ ll u,v;cin>>u>>v;u--;v--; g[u].PB({v,i,0}); g[v].PB({u,i,1}); } vector<ll>c(n); rep(i,0,n)cin>>c[i]; vector<ll>ord(n); iota(ALL(ord),0); sort(ALL(ord),[&](ll i,ll j) { return c[i]<c[j]; } ); vector<bool>used(n); vector<ll>ed(m,-1); for(auto i:ord){ if(used[i])continue; auto dfs=[&](auto &&f,ll k)->void{ used[k]=true; for(auto e:g[k]){ if(ed[e.id]!=-1)continue; if(c[e.to]>c[i])continue; ed[e.id]=e.rev; if(used[e.to])continue; f(f,e.to); } }; dfs(dfs,i); } //debug(ed,m); rep(i,0,m){ ans(!ed[i],"->","<-"); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n,m,x,y; cin>>n>>m>>x>>y; char ch[n][m]; for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ cin>>ch[i][j]; } } x--; y--; int ans=0; if(ch[x][y]=='.')ans++; //left for(int i=x+1;i<n;++i){ if(ch[i][y]=='.')ans++; else break; } for(int i=x-1;i>=0;--i){ if(ch[i][y]=='.')ans++; else break; } for(int i=y+1;i<m;++i){ if(ch[x][i]=='.')ans++; else break; } for(int i=y-1;i>=0;--i){ if(ch[x][i]=='.')ans++; else break; } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define Turnoff std::ios::sync_with_stdio(false) const int Max=3e5+5; const ll Mod=1e9+7; /* 2 6 AGCTAG CTAGCT */ int mp[105][105]; int main(){ Turnoff; int n,m;cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ char temp; cin>>temp; if(temp=='.')mp[i][j]=1; else mp[i][j]=0; } } int cnt=0; for(int i=1;i<=n-1;i++){ for(int j=1;j<=m;j++){ if(mp[i][j]==mp[i+1][j]&&mp[i][j])cnt++; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m-1;j++){ if(mp[i][j]==mp[i][j+1]&&mp[i][j])cnt++; } } cout<<cnt<<endl; }
#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; #ifdef _WIN32 #define getchar_unlocked _getchar_nolock #endif #define int long long #define mp make_pair #define mt make_tuple #define pb push_back #define ppb pop_back #define eb emplace_back #define g0(a) get<0>(a) #define g1(a) get<1>(a) #define g2(a) get<2>(a) #define g3(a) get<3>(a) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef double db; typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef tuple<int, int, int> iii; typedef tuple<int, int, int, int> iiii; typedef tree<iii, null_type, greater<iii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int N, K, ans = LLONG_MAX, A[805][805], P[805][805]; ordered_set S; main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> K; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) cin >> A[i][j]; int lo = 0, hi = 1e9, ans = -1; int calc = K * K - ((K * K) / 2 + 1) + 1; while (lo <= hi) { int mid = (lo + hi) / 2; bool yes = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) { P[i][j] = (A[i][j] <= mid); P[i][j] += P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1]; } for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) { if (i >= K && j >= K) { int tmp = P[i][j] - P[i - K][j] - P[i][j - K] + P[i - K][j - K]; if (tmp >= calc) yes = 1; } } if (yes) ans = mid, hi = mid - 1; else lo = mid + 1; } cout << ans << '\n'; }
#include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i=a; i<=b; i++) #define trav(x, a) for(auto &x:a) #define all(x) x.begin(), x.end() #define sz(x) (int) x.size() #define pb push_bacl #define nl '\n' #define f first #define s second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; template <ll Mod> struct ModInt { ll n; ModInt(const ll x = 0) : n(x) { while (n < 0) n += Mod; n %= Mod; } explicit operator int() const { return n; } inline constexpr ModInt operator+(const ModInt r) const noexcept { return ModInt(*this) += r; } inline constexpr ModInt operator-(const ModInt r) const noexcept { return ModInt(*this) -= r; } inline constexpr ModInt operator*(const ModInt r) const noexcept { return ModInt(*this) *= r; } inline constexpr ModInt operator/(const ModInt r) const noexcept { return ModInt(*this) /= r; } inline constexpr ModInt &operator+=(const ModInt r) noexcept { n += r.n; if (n >= Mod) n -= Mod; return *this; } inline constexpr ModInt &operator-=(const ModInt r) noexcept { if (n < r.n) n += Mod; n -= r.n; return *this; } inline constexpr ModInt &operator*=(const ModInt r) noexcept { n = n * r.n % Mod; return *this; } inline constexpr ModInt &operator/=(const ModInt r) noexcept { return *this *= r.inv(); } inline constexpr ModInt pow(ll x) const noexcept { ModInt<Mod> ret(1), tmp(*this); while (x) { if (x&1) ret *= tmp; tmp *= tmp; x >>= 1; } return ret; } inline constexpr ModInt inv() const noexcept { return pow(Mod-2); } friend ostream& operator<<(ostream& os, const ModInt& obj) { return os << obj.n; } friend istream& operator>>(istream& is, ModInt& obj) { ll t; is >> t; obj = ModInt(t); return is; } }; constexpr ll mod = 998244353; //check! using mint = ModInt<mod>; mint operator"" _mi(unsigned long long n) { return mint(n); } int n; int a[101]; mint dp[101][10001]; mint fac[101]; int main(){ cin.tie(0)->sync_with_stdio(0); // freopen("input.txt", "r", stdin); cin >> n; int tot=0; rep(i, 1, n) cin >> a[i], tot+=a[i]; if(tot&1){ cout << 0; return 0; } fac[0]=1; rep(i, 1, n) fac[i]=fac[i-1]*i; dp[0][0]=1; rep(i, 1, n){ for(int k=i; k>=1; k--) { for(int s=100*n; s>=0; s--) { if(a[i]<=s) dp[k][s]+=dp[k-1][s-a[i]]; } } } mint ans=0; rep(i, 1, n-1){ ans+=dp[i][tot/2]*fac[i]*fac[n-i]; // cout << "added " << dp[i][tot/2] << "*" << fac[i] << "*" << fac[n-i] << nl; } cout << ans; } // w_i > tot/2 -> 0 // step 1: # ways to make sum tot/2 // step 2: each contributes 2*n1!*n2! cuz they force // dp[k][s] = # ways to have k elements sum to s
#include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <array> #include <stack> #include <queue> #include <random> #include <numeric> #include <functional> #include <chrono> #include <utility> #include <iomanip> #include <assert.h> using namespace std; void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define rng_seed(x) mt19937 rng(x) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() // #define int long long const int MXN = 2e5 + 5, INF = 1e9 + 5; vector<int> g[MXN], level[MXN]; int timer; int tin[MXN], tout[MXN]; void dfs(int u, int p, int depth) { tin[u] = timer++; level[depth].push_back(tin[u]); for (const auto &v : g[u]) { if (v == p) continue; dfs(v, u, depth + 1); } tout[u] = timer - 1; } void solve() { int N; cin >> N; for (int i = 1; i < N; i++) { int p; cin >> p; p--; g[i].push_back(p); g[p].push_back(i); } dfs(0, -1, 0); int Q; cin >> Q; while (Q--) { int u, d; cin >> u >> d; u--; auto l = lower_bound(all(level[d]), tin[u]); auto r = upper_bound(all(level[d]), tout[u]); cout << r - l << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int TC = 1; // cin >> TC; while (TC--) solve(); }
#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<ll, P> T; typedef pair<ll, vector<ll>> Pd; const ll INF = 1e18; const ll fact_table = 320000; 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 << "\n"; #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) #define sorti(v) sort(all(v)) #define sortd(v) \ sort(all(v)); \ reverse(all(v)); #define SUM(v) accumulate(all(v), 0LL) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) typedef vector<ll> vec; typedef vector<vector<ll>> mat; // vec v(n) -> 長さnのベクトルを宣言 // mat dp(h, vec(w)) -> h * w の行列を宣言 const ll mod = 998244353ll; 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, m, k, a, b, c; string s; ll x[500005], y[500005], z[500005]; bool solve() { ll ans = 0; cin >> s; 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++) { set<ll> st; st.insert(i); st.insert(j); st.insert(k); st.insert(l); bool ok = true; for (int i = 0; i < 10; i++) { if (s[i] == 'o' && !st.count(i)) ok = false; if (s[i] == 'x' && st.count(i)) ok = false; } if (ok) ans++; } } } } p(ans); 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--) { // cout << "Case #" << testcase << ": "; solve(); testcase++; } // solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef _DEBUG #define dout cout #define debug() if (true) #define dvout(v) vout(v) #else #define dout if (false) cout #define debug() if (false) #define dvout(v) if(false) vout(v) #endif #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define vout(v) \ for (auto &i : v) \ { \ cout << i << " "; \ } \ cout << "\n"; //配列出力 #define vec(v, n) vector<int> v(n, 0); #define vecll(v, n) vector<ll> v(n, 0); #define vecvec(v,n,m) vector<vector<int>> v(n, vector<int>(m)); #define vin(v) \ for (auto &i : v) \ cin >> i; //配列入力 #define mod (int)(1e9+7) typedef long long ll; typedef unsigned long long ull; #define next asdnext #define prev asdprev #define _n "\n" #define _n2 "\n\n" #define _t "\t" // LLONG_MAX 9.2*1e18 9223372036854775807LL = (2^63)-1 // INT_MAX 2.1*1e9 2147483647 = (2^31)-1 // O(1e8) 1.2sec void test(ull,ull,ull); void Main() { char S,T; cin>>S>>T; if(S=='Y'){ T+='A'-'a'; } cout<<T<<endl; } void test(ull N,ull X, ull M){ ull ans=0; int t=X; rep(i,N){ ans+=t; t=(t*t)%M; } dout<<"testans"<<ans<<_n<<_n; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << std::fixed << std::setprecision(15); Main(); return 0; }
#include<bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i,n) for(i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ll long long #define si(x) scanf("%d",&x) #define sl(x) scanf("%lld",&x) #define ss(s) scanf("%s",s) #define pi(x) printf("%d\n",x) #define pl(x) printf("%lld\n",x) #define ps(s) printf("%s\n",s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0,lim-1); return uid(rang); } int mpow(int base, int exp); void ipgraph(int n, int m); void dfs(int u, int par); const int mod = 1'000'000'007; const int N = 3e5, M = N; //======================= vi g[N]; int a[N]; void solve(int num = 0) { ll i, j, n, m, ans = 0; cin >> n; i = 1000; int counter = 1; while(n/i) { ans += n%i + 1; ans +=(n/i - 1)*i; i *= 1000; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int t = 1, cr = 0; // cin >> t; while(t--) { solve(cr++); } return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; } void ipgraph(int n, int m){ int i, u, v; while(m--){ cin>>u>>v; u--, v--; g[u].pb(v); g[v].pb(u); } } void dfs(int u, int par){ for(int v:g[u]){ if (v == par) continue; dfs(v, u); } }
#include <bits/stdc++.h> using namespace std; #define dump(x) cerr << "~ " << #x << " = " << x << endl int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; string s,mat[5]; long long len = 3e10; cin >> n; cin >> s; if(n == 1) { cout << len/3 + (s[0] == '1' ? len/3 : 0) << endl; return 0; } mat[0] = "110"; mat[1] = "101"; mat[2] = "011"; for(int i = 0; i < 3; i++) { int flag = 0; for(int j = 0; j < n; j++) { if(s[j] != mat[i][j%3]) { flag = 1; break; } } if(!flag) { long long id = len-n; while(id%3 != i) id--; cout << id/3+1 << endl; return 0; } } cout << 0 << endl; return 0; }
#include<bits/stdc++.h> typedef long long ll ; #define int ll using namespace std ; signed main( ) { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int testcases = 1 ; // cin >> testcases ; while( testcases -- ) { int a , b ; cin >> a >> b ; int sum = 0 ; for(int i = 1 ; i <=a ; i++) { for(int j = 1 ; j <= b ; j++) { int n = i*100 ; n += j ; sum += n ; } } cout << sum ; cout << endl ; } return 0 ; }
#include <bits/stdc++.h> using namespace std; #define rng(x) x.begin(), x.end() #define maxi(x, y) x = max(x, (y)) #define mini(x, y) x = min(x, (y)) #define pb push_back #define F first #define S second #define el '\n' #define int long long template<typename T> istream&operator>>(istream&is,vector<T>&v){for(auto&it:v)is>>it;return is;} template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.F << "," << P.S << ")"; } template<class T> ostream& operator<<(ostream &os, vector<T> V) { os << "[ "; for(auto v : V) os << v << " "; return os << "]"; } template<class T> ostream& operator<<(ostream &os, set<T> S){ os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; } #define db(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout<<name<<" : "<<arg1<<'\n';} template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names,comma-names)<<" : "<<arg1<<" |";__f(comma+1, args...);} typedef pair<int,int> pi; typedef vector<int> vi; typedef vector<vi> vvi; /*-----------------------------Code begins----------------------------------*/ const int N = 2000; const int MOD = 1e9 + 7; // operation keeping MOD in mind; int add(int x, int y){ x += y; while(x >= MOD) x -= MOD; while(x < 0) x += MOD; return x; } int mul(int x, int y){ // to avoid overflow; return (x * 1ll * y) % MOD; } int fact[N]; // return (x raised to power y) % MOD; int binpow(int x, int y){ int z = 1; while(y){ if(y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } int inv(int x){ return binpow(x, MOD - 2); } // return x/y; int divide(int x, int y){ return mul(x, inv(y)); } // array of factorials; void precalc(){ fact[0] = 1; for(int i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i); } // returns nCk; int C(int n, int k){ if(k > n) return 0; return divide(fact[n], mul(fact[k], fact[n - k])); } void solve(){ int n; cin>>n; char aa, ab, ba, bb; cin>>aa>>ab>>ba>>bb; if(n == 2){ cout<<1<<el; return; } if(ab == aa and ab == 'A'){ cout<<"1\n"; return; } if(ab == bb and ab == 'B'){ cout<<"1\n"; return; } if(ab == ba){ // not consequtive; n--; int ans = 0; for (int bs = 0; bs <= n; ++bs){ ans += C(n - bs - 1, bs + 1 - 1); ans %= MOD; } cout<<ans<<el; } else{ cout<<binpow(2, n - 3)<<el; } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T=1; // cin>>T; precalc(); while(T--){ solve(); } 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; } void solve(long long X, long long Y, long long Z){ ll ans= Y*Z/X; if(Y*Z%X==0){ --ans; } cout<<ans<<endl; } int main(){ cout<<setprecision(1000); long long X; scanf("%lld",&X); long long Y; scanf("%lld",&Y); long long Z; scanf("%lld",&Z); solve(X, Y, Z); return 0; }
#include <iostream> #include <cmath> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <tuple> #include <utility> #include <functional> #include <set> #include <map> #include <bitset> #include <list> #include<iomanip> using namespace std; using ll = long long; using ULL = unsigned long long; using pll = std::pair<ll, ll>; using vi = std::vector<int>; using vl = std::vector<ll>; using vb = std::vector<bool>; using vpll = std::vector<pll>; using db = double; using vdb = std::vector<db>; using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending template<class T> using vv = std::vector<std::vector<T> >; #define REPL(i, n) for (ll i = 0; i < (ll)(n); i++) #define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define REP(i, n) FORL(i, 0, n) #define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- ) #define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define bit(n) (1LL << (n)) #define INF pow(10, 10) void chmin(ll& a, ll b){ if(a > b) a = b; } void chmax(ll& a, ll b){ if(a < b) a = b; } vpll calc_4thval(ll p1, ll p2, vv<ll> &Statuses) { vpll Larges(5); REP(i, 5) { Larges[i] = {max(Statuses[p1][i], Statuses[p2][i]), i}; } sort( all(Larges) ); return Larges; } int main(void) { ll N; cin >> N; vv<ll> Statuses(N, vl(5)); vl Maxvals(5, 0); REP(i, N) { REP(k, 5) { cin >> Statuses[i][k]; Maxvals[k] = max(Maxvals[k], Statuses[i][k]); } } /* 2人で4つ */ ll ans = 0; REP(p1, N) { REP(p2, N) { if(p1 == p2) continue; vpll Stats_2p = calc_4thval(p1, p2, Statuses); bool flag = false; ll Min = Stats_2p[0].first; ll candidate = Stats_2p[1].first; REP(i, 5) { ll subject = Stats_2p[i].second; if(Min == Stats_2p[i].first && Maxvals[subject] >= ans) { ans = max(ans, min(candidate, Maxvals[subject]) ); } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; //const ll INF = numeric_limits<ll>::max() / 4; //const int INF = numeric_limits<int>::max() / 4; int main() { int N; cin >> N; vector<vector<int> > X(5, vector<int>(N,0)); int mx = 0; for(int i = 0; i < N; i++){ for(int k = 0; k < 5; k++){ cin >> X[k][i]; mx = (mx < X[k][i] ? X[k][i] : mx); } } int bl = 0; int bu = mx + 1; vector<bool> P(32); while(bl + 1 < bu){ int b = (bl + bu) / 2; fill(P.begin(),P.end(),false); for(int i = 0; i < N; i++){ int t = 0; for(int k = 0; k < 5; k++){ if(b <= X[k][i]) t |= (1 << k); } P[t] = true; } bool f = false; for(int i1 = 1; i1 < 32; i1++){ if(!P[i1]) continue; for(int i2 = i1; i2 < 32; i2++){ if(!P[i2]) continue; for(int i3 = i2; i3 < 32; i3++){ if(!P[i3]) continue; if(((i1 | i2) | i3) == 31){ f = true; break; } } if(f) break; } if(f) break; } if(f){ bl = b; }else{ bu = b; } } cout << bl << endl; return(0); }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count()); #define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n" template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}; template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";}; void _print() {cerr << "]\n";} template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);} #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define fi first #define se second #define SZ(x) (int)((x).size()) #define pii pair<int,int> int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int a[n]; for (int q = 0; q < n; q++) cin >> a[q]; for (int q = 0; q < n; q++) a[q]++; ordered_set<int> seen; ll tot = 0; for (int q = n-1; q >= 0; q--) { tot += seen.order_of_key(a[q]); seen.insert(a[q]); } cout << tot << "\n"; for (int q = 0; q < n-1; q++) { tot -= (a[q]-1); tot += (n-a[q]); cout << tot << "\n"; } return 0; }
// #pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define dd double #define ld long double #define sl(n) scanf("%lld", &n) #define si(n) scanf("%d", &n) #define sd(n) scanf("%lf", &n) #define pll pair <ll, ll> #define pii pair <int, int> #define mp make_pair #define pb push_back #define all(v) v.begin(), v.end() #define inf (1LL << 61) #define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) #define for1(i, stop) for(ll i = 1; i <= stop; ++i) #define for0(i, stop) for(ll i = 0; i < stop; ++i) #define rep1(i, start) for(ll i = start; i >= 1; --i) #define rep0(i, start) for(ll i = (start-1); i >= 0; --i) #define ms(n, i) memset(n, i, sizeof(n)) #define casep(n) printf("Case %lld:", ++n) #define pn printf("\n") #define pf printf #define EL '\n' #define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const ll sz = 3e5 + 10; ll BIT[sz], ara[sz], big[sz], small[sz], pbig[sz], psmall[sz]; void upd(ll idx, ll n) { while(idx <= n) { BIT[idx]++; idx += idx&(-idx); } } ll query(ll idx) { ll ret = 0; while(idx > 0) { ret += BIT[idx]; idx -= idx&(-idx); } return ret; } inline ll qry(ll l, ll r) { return query(r) - query(l-1); } int main() { ll n; cin >> n; for1(i, n) { sl(ara[i]); ara[i]++; } ll tot = 0; rep1(i, n) { if(ara[i] == n) big[i] = 0; else big[i] = qry(ara[i]+1, n); upd(ara[i], n); small[i] = qry(1, ara[i]-1); tot += small[i]; } ms(BIT, 0); for1(i, n) { if(ara[i] == n) pbig[i] = 0; else pbig[i] = qry(ara[i]+1, n); upd(ara[i], n); psmall[i] = qry(1, ara[i]-1); } pf("%lld\n", tot); for1(i, n-1) { tot -= small[i], tot -= psmall[i]; tot += pbig[i], tot += big[i]; pf("%lld\n", tot); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; // template {{{ 0 // using {{{ 1 using ll = long long int; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; using vii = vector<pii>; using vll = vector<pll>; // }}} 1 // definition {{{ 1 // scaning {{{ 2 #define Scd(x) scanf("%d", &x) #define Scd2(x,y) scanf("%d%d", &x, &y) #define Scd3(x,y,z) scanf("%d%d%d", &x, &y, &z) #define Scll(x) scanf("%lld", &x) #define Scll2(x,y) scanf("%lld%lld", &x, &y) #define Scll3(x,y,z) scanf("%lld%lld%lld", &x, &y, &z) #define Scc(c) scanf("%c", &c); #define Scs(s) scanf("%s", s); #define Scstr(s) scanf("%s", &s); // }}} 2 // constants {{{ 2 #define EPS (1e-7) #define INF (2e9) #define PI (acos(-1)) // }}} 2 // systems {{{ 2 #define Repe(x,y,z) for(ll x = z; x < y; x++) #define Rep(x,y) Repe(x,y,0) #define RRepe(x,y,z) for(ll x = y-z-1; x >= 0; x--) #define RRep(x,y) RRepe(x,y,0) // }}} 2 // output {{{ 2 #define YesNo(a) (a)?printf("Yes\n"):printf("No\n") #define YESNO(a) (a)?printf("YES\n"):printf("NO\n") // }}} 2 // }}} 1 // input {{{ 1 // }}} 1 // }}} 0 int main() { int R,C; Scd2(R,C); int N = R*C; vector<vii> e(N*2); int a; Rep(i,N){ if( i%C == C-1 ) continue; Scd(a); e[i].push_back({i+1, a}); e[i+1].push_back({i, a}); } Rep(i,N-C){ Scd(a); e[i].push_back({i+C, a}); e[i+C+N].push_back({i+N, 1}); } Rep(i,N){ e[i].push_back({i+N, 1}); e[i+N].push_back({i, 0}); } vi d(N*2, INF); priority_queue<pii, vii, greater<pii>> q; q.push({0,0}); d[0] = 0; while(q.size()){ pii top = q.top(); q.pop(); int from = top.second; int dist = top.first; if( d[from] < dist ) continue; for( pii tow : e[from] ) { int to = tow.first; int cost = tow.second; if( dist + cost < d[to] ) { d[to] = dist + cost; q.push({d[to], to}); } } } printf ("%d\n", d[N-1]); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int R,C,inf=1001001001; cin >> R >> C; vector<vector<int>> A(R,vector<int>(C-1)), B(R-1,vector<int>(C)); for(int i=0;i<R;i++){ for(int j=0;j<C-1;j++){ cin >> A[i][j]; } } for(int i=0;i<R-1;i++){ for(int j=0;j<C;j++){ cin >> B[i][j]; } } vector<vector<int>> V(R,vector<int>(C,inf)); V[0][0] = 0; priority_queue<pair<int,int>> Q; Q.push(make_pair(0,0)); while(!Q.empty()){ pair<int,int> p = Q.top(); Q.pop(); int r = p.second % R, c = p.second / R; if(V[r][c]!=-p.first) continue; if(c+1<C){ if(V[r][c+1]>V[r][c]+A[r][c]){ V[r][c+1] = V[r][c] + A[r][c]; Q.push(make_pair(-V[r][c+1],(c+1)*R+r)); } } if(c-1>=0){ if(V[r][c-1]>V[r][c]+A[r][c-1]){ V[r][c-1] = V[r][c] + A[r][c-1]; Q.push(make_pair(-V[r][c-1],(c-1)*R+r)); } } if(r+1<R){ if(V[r+1][c]>V[r][c]+B[r][c]){ V[r+1][c] = V[r][c] + B[r][c]; Q.push(make_pair(-V[r+1][c],c*R+r+1)); } } for(int i=1;r-i>=0;i++){ if(V[r-i][c]>V[r][c]+i+1){ V[r-i][c] = V[r][c] + i+1; Q.push(make_pair(-V[r-i][c],c*R+r-i)); } } } cout << V[R-1][C-1] << endl; return 0; }
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<vector> #include<map> using namespace std; typedef long long ll; const int N=200005; int n,l,a[N],b[N],goal[N],p[N]; ll ans; vector<int> vec; map<int,vector<int>> mp; void mandown(){ puts("-1"); exit(0); } ll solve(int l,int r){ if(l+1==r){ return 0; } ll res=0; int mid=l,pos; while(mid+1<r&&b[mid+1]<a[mid+1]){ mid++; } for(int i=mid,j=i;i>l;i--,j=min(j,i)){ vector<int> &tmp=mp[b[i]-i]; if(tmp.size()==0){ mandown(); } auto it=lower_bound(tmp.begin(),tmp.end(),j); it--; if(*it<l){ mandown(); } goal[i]=*it; j=min(j,*it+1); } for(int i=mid+1,j=i;i<r;i++,j=max(j,i)){ vector<int> &tmp=mp[b[i]-i]; auto it=upper_bound(tmp.begin(),tmp.end(),j); if(it==tmp.end()||*it>r){ mandown(); } goal[i]=*it; j=max(j,*it-1); } for(int i=l;i<=r;i++){ p[i]=1; } pos=mid; for(int i=mid;i>l;i--){ if(pos!=goal[i]){ res+=p[pos]+pos-goal[i]-1; p[goal[i]]+=p[pos]+pos-goal[i]-2; pos=goal[i]; }else{ p[pos]--; } } pos=mid+1; for(int i=mid+1;i<r;i++){ if(pos!=goal[i]){ res+=p[pos]+goal[i]-pos-1; p[goal[i]]+=p[pos]+goal[i]-pos-2; pos=goal[i]; }else{ p[pos]--; } } return res; } int main(){ scanf("%d%d",&n,&l); n+=2; a[1]=b[1]=0; a[n]=b[n]=l+1; for(int i=2;i<n;i++){ scanf("%d",&a[i]); } for(int i=2;i<n;i++){ scanf("%d",&b[i]); } for(int i=1;i<=n;i++){ if(a[i]==b[i]){ vec.push_back(i); } mp[a[i]-i].push_back(i); } for(int i=0;i<(int)vec.size()-1;i++){ ans+=solve(vec[i],vec[i+1]); } printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) {for(auto i: x){cout << i << " ";} cout << endl;} using namespace std; using ll = long long; using P = pair<int, int>; int main() { // 各ペンギンの間のマスに注目 int N, L; cin >> N >> L; vector<int> A(N); rep(i, N) cin >> A[i]; A.push_back(L+1); vector<int> B(N); rep(i, N) cin >> B[i]; B.push_back(L+1); vector<ll> roomA(N+1); vector<ll> roomB(N+1); roomA[0] = A[0] - 1; roomB[0] = B[0] - 1; rep(i, N) { roomA[i+1] = A[i+1] - 1 - A[i]; roomB[i+1] = B[i+1] - 1 - B[i]; } ll ans = 0; int cur = 0; // roomAの今のindex for (int i = 0; i < N+1; i++) { // cout << ans << '\n'; if (roomB[i] == 0) { continue; } ll temp = 0; while (cur < N+1 && roomA[cur] == 0) cur++; ans += max(0, i - cur); while (cur < N+1 && temp < roomB[i]) { temp += roomA[cur++]; } if (temp != roomB[i]) { cout << -1 << '\n'; return 0; } ans += max(0, cur - 1 - i); } cout << ans << '\n'; return 0; // 境界、ll, 0, -, 1i, for s&g, debug }
//////// Squares // https://atcoder.jp/contests/hhkb2020/tasks/hhkb2020_d // date: 2020-10-10T21:00:04.737499 // memo: //<editor-fold desc="my macro"> #include <bits/stdc++.h> #ifdef LOCAL #include <my/dump.h> #else #define dump(...) 42 #define dumpi(x) 42 #define dumpar(...) 42 #endif typedef long long ll; using namespace std; #define repe(x, a) for(auto&& (x) : (a)) #define _overload3(_1, _2, _3, name, ...) name #define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__) #define _rep3(i, a, b) for(ll i=a, i##_len=(b);i<i##_len;i++) #define _rep2(i, n) _rep3(i,0,n) #define repd(...) _overload3(__VA_ARGS__,_repd3,_repd2,)(__VA_ARGS__) #define _repd3(i, a, b) for(ll i=b-1, i##_len=(a);i>=i##_len;i--) #define _repd2(i, n) _repd3(i,0,n) #define loop(...) _overload3(__VA_ARGS__,,_loop2,_loop1)(__VA_ARGS__) #define _loop2(i, a) for(ll i=a;; i++) #define _loop1(i) _loop2(i,0) #define all(x) x.begin(),x.end() #define SIZE(x) ((ll)(x).size()) #define F first #define S second #define vec vector #define umap unordered_map #define uset unordered_set ll dx[4]={1,0,-1,0}; ll dy[4]={0,1,0,-1}; #define bit(n) (1LL<<(n)) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define inrange(x,start,stop) ((start) <= (x) && (x) < (stop)) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template <typename Cont, typename Func> Cont fmap(const Cont &container, Func functor) { Cont result(container.size()); std::transform(container.begin(), container.end(), result.begin(), functor);// [](int x){return x * 2;} return result; } template <typename Cont, typename Pred> Cont filter(const Cont &container, Pred predicate) { Cont result; std::copy_if(container.begin(), container.end(), std::back_inserter(result), predicate);// [](int x){return x == 2;} return result; } template<typename T, typename ...Args> void ci(T& first, Args&... args) { ((cin >> first), ..., (cin >> args)); //C++17 } template<char Sep, typename T, typename ...Args> void print_seq(const T& first, const Args&... args) { ((cout << first), ..., (cout << Sep << args)); //C++17 } void co(){ cout << endl; } template<typename... Words> void co(Words... words) { print_seq<' '>(words...); cout << endl; } #define coi(x) cout<<(x)<<" " #define YES(n) cout << ((n) ? "YES" : "NO" ) << endl #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define sankou(n,a,b) cout << ((n) ? (a) : (b) ) << endl #define asrt(expr...) assert((expr)) //</editor-fold> //digit guide 9876543210987654321 #define LINF 2000000000 //2*10^9 #define INF 9000000000000000000 //9*10^18 #define MOD 1000000007 //1*10^9 + 7 #define MAXR 100000 //digit guide 9876543210987654321 void solve() { ll T; ci(T); rep(t,T) { ll N,A,B; ci(N,A,B); // all = (N-A-B+2) * (N-A-B+1) * (N-A+1) * (N-B+1) * 2; // 全 = (N-A+1) * (N-B+1) ; // かぶらない = (N-A-B+2) * (N-A-B+1); // 答 = all * (全 - かぶらない / 2) / 全; if (N < A+B) { co(0); } else { ll ans = (N - A + 1) * (N - B + 1) - (N - A - B + 2) * (N - A - B + 1) / 2; ans %= MOD; ans *= (N - A - B + 2); ans %= MOD; ans *= (N - A - B + 1) * 2; ans %= MOD; co(ans); } } } int main(){ //入力の高速化用のコード //ios::sync_with_stdio(false); //cin.tie(nullptr); solve(); return 0; }
#include <algorithm> #include <complex> #include <cstdlib> #include <ctime> #include <time.h> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #include <numeric> #include <limits> #include <type_traits> #include <locale> #include <omp.h> using namespace std; #define SAY_YES cout << "YES" << endl; #define SAY_Yes cout << "Yes" << endl; #define SAY_NO cout << "NO" << endl; #define SAY_No cout << "No" << endl; #define IFYES(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) \ { \ cout << "YES" << endl; \ } \ else \ { \ cout << "NO" << endl; \ } #define IFYes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) \ { \ cout << "Yes" << endl; \ } \ else \ { \ cout << "No" << endl; \ } #define IFyes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) \ { \ cout << "yes" << endl; \ } \ else \ { \ cout << "no" << endl; \ } #define DEBUG_OUTPUT_ARRAY(XXX, ONE) \ for (int i = 0; i < (ONE); i++) \ { \ cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \ } #define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO) \ for (int i = 0; i < (ONE); i++) \ { \ cout << "<<< i = " << i << " >>>" << endl; \ for (int j = 0; j < (TWO); j++) \ { \ cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \ } \ } #define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \ for (int i = 0; i < (ONE); i++) \ { \ cout << i << " "; \ for (int j = 0; j < (TWO); j++) \ { \ cout << XXX[i][j] << " "; \ } \ cout << endl; \ } typedef pair<long long int, long long int> pll; typedef pair<long long int, pll> lpll; const long long int mod = 1000000007; const long long int INF = 1e18; const long double PI=3.14159265358979323; //const long long int pl=1000000; long long int N,M,K,H,W,T,A,B; long long int dp[3000][3000]={},res=0; long long int p[2000300]={}; string S[200]; bool used[200200]={}; int main() { cout << fixed << setprecision(18); cin>>T; for(long long int q = 0; q < T; q++){ cin>>N>>A>>B; if(N<A+B){cout<<0<<endl;continue;} long long int tmp=(N-A-B)%mod; long long int mi=(tmp+2)*(tmp+1); mi%=mod; mi*=mi; mi%=mod; res=(2*(N-A+1))%mod; res*=tmp+2; res%=mod; res*=tmp+1; res%=mod; res*=N-B+1; res%=mod; res-=mi; res%=mod; res=(res+mod)%mod; cout<<res<<endl; } }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define N 200100 #define MOD 1000000007 //998244353 #define ll long long #define rep(i, n) for(int i = 0; i < n; ++i) #define rep2(i, a, b) for(int i = a; i <= b; ++i) #define rep3(i, a, b) for(int i = a; i >= b; --i) #define eb emplace_back #define pb push_back #define all(c) (c).begin(), (c).end() #define vi vector<int> #define pii pair<int,int> #define pll pair<ll,ll> struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); } } setup_io; int main() { char a[100]; ll b[100]; ll c[100]; int sz; ll n, x, y, z, l, r, mid; bool v; rep(i, 100) { a[i] = 0; } cin >> a; cin >> n; sz = strlen(a); if (sz == 1) { x = (ll)(a[0] - '0'); if (x <= n)cout << 1 << endl; else cout << 0 << endl; return 0; } l = 0; rep(i, sz) { c[i] = (ll)(a[i] - '0'); l = max(l, c[i]); } z = l; r = n + 1; while (l + 1 < r) { mid = (l + r) / 2; x = n; rep(i, 100)b[i] = 0; rep(i, sz) { b[sz - i - 1] = x % mid; x /= mid; } v = true; if (x == 0) { rep(i, sz) { if (c[i] < b[i]) { v = true; break; } if (c[i] > b[i]) { v = false; break; } } } if (v)l = mid; else r = mid; } cout << (l-z)<<endl; return 0; }
#include<bits/stdc++.h> #define u64 uint64_t #define ll long long #define endl "\n" #define PI acos(-1) #define fi first #define si second #define mkp make_pair #define pb push_back #define set0(arr) memset(arr, 0, sizeof(arr)) #define setinf(arr) memset(arr, 126, sizeof(arr)) #define all(x) (x).begin(),(x).end() #define sz(v) ((int)(v).size()) #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) using namespace std; using pll = pair<ll, ll> ; using vl = vector<ll> ; using vpll = vector<pll> ; using mll = map<ll, ll> ; using mcl = map<char, ll> ; using msl = map<string,ll> ; using sl = set<ll> ; using sc = set<char> ; using dl = deque<ll> ; const int N = 1e6+5 ; ll mod = 1e9+7 ; vl adj[N] ; vpll adjc[N] ; ll vis[N] ; ll arr[N] ; ll muld(ll a,ll b) { return (((a%mod)*(b%mod))%mod); } ll sumd(ll a,ll b) { return (((a%mod)+(b%mod))%mod); } int main() { IOS; ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0, mul = 1, mi = 1e18, ma = -1e18, cs; string str ; char ch ; double db ; ll l, r ; //code cin>>a>>b; x=b; y=2*a+100; if(y>=x) { cout<<y-x<<endl; } else cout<<0<<endl; //code return 0; }
#include<iostream> #include <cassert> #include <numeric> using namespace std; #include<vector> #include <algorithm> #define re(i,n) for(int i = 0; i < n; i++); const int MOD = 998244353; int main(){ int a,b, c; cin >> a >> b >> c; if(a > b - c) cout << "Takahashi"<< endl; else cout << "Aoki"<< endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int v, t, s, d; scanf("%d %d %d %d", &v, &t, &s, &d); if(d < v * t || v * s < d) cout << "Yes"; else cout << "No"; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <string> #include <sstream> #include <iomanip> #include <map> #include <unordered_map> #include <stack> #include <cstdio> #include <climits> #include <tuple> #include <ctime> #include <cstring> #include <numeric> #include <functional> #include <chrono> #include <cassert> #include <bitset> // printf("%.10f\n", ans); using ll = long long; using namespace std; const ll mod = 1e9 + 7; const int N = 1e5 + 1; int n, timer; vector<vector<int>> tree, h; vector<int> tin, tout; void dfs(int u, int p, int he) { tin[u] = timer++; h[he].push_back(u); for (int& v : tree[u]) if (v != p) dfs(v, u, he + 1); tout[u] = timer++; } int bs1(int& u, vector<int>& vec) { int l = 0, r = vec.size() - 1; int id = -1; while (l <= r) { int m = (l + r) >> 1; int v = vec[m]; if (tin[u] <= tin[v] && tout[u] >= tout[v]) r = m - 1, id = m; else if (tin[u] <= tin[v]) r = m - 1; else l = m + 1; } return id; } int bs2(int& u, vector<int>& vec) { int l = 0, r = vec.size() - 1; int id = -2; while (l <= r) { int m = (l + r) >> 1; int v = vec[m]; if (tin[u] <= tin[v] && tout[u] >= tout[v]) l = m + 1, id = m; else if (tin[u] <= tin[v]) r = m - 1; else l = m + 1; } return id; } int main() { scanf("%d", &n); tree = h = vector<vector<int>>(n); tin = tout = vector<int>(n); for (int i = 1; i < n; i++) { int p; scanf("%d", &p); tree[--p].push_back(i); tree[i].push_back(p); } dfs(0, -1, 0); int q; scanf("%d", &q); while (q--) { int u, d; scanf("%d%d", &u, &d); u--; int l = bs1(u, h[d]); int r = bs2(u, h[d]); printf("%d\n", r - l + 1); } return 0; }
#include <bits/stdc++.h> #define rep(i, a, n) for(int i = a; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; const int mod = 1000000007; //const int mod = 998244353; const int MAX_V = 200005; int k = 0; vector<vector<int>> to(MAX_V); vector<bool> used(MAX_V); vector<int> dep(MAX_V); vector<int> in(MAX_V); vector<int> out(MAX_V); vector<vector<int>> ls(MAX_V); void dfs(int v, int d){ in[v] = k++; dep[v] = d; used[v] = true; for(int u : to[v]){ if(used[u]) continue; dfs(u, d+1); } out[v] = k; } int main() { int n; cin >> n; rep(i, 0, n-1){ int p; cin >> p; p--; to[p].push_back(i+1); to[i+1].push_back(p); } dfs(0, 0); rep(i, 0, n) ls[dep[i]].push_back(in[i]); rep(i, 0, n) sort(ls[i].begin(), ls[i].end()); auto f = [&](int d, int r){ return int(lower_bound(ls[d].begin(), ls[d].end(), r) - ls[d].begin()); }; int q; cin >> q; rep(i, 0, q){ int u, d; cin >> u >> d; u--; printf("%d\n", f(d, out[u]) - f(d, in[u])); } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < (n); i++) #define ALL(a) (a).begin(),(a).end() #define SORT(a) sort((a).begin(),(a).end()) #define RSORT(a) sort((a).rbegin(),(a).rend()) #define ll long long #define pb(a) push_back(a) #define PRINTV(v) {for(ll deB=0; deB<(v).size(); deB++){cout << (v)[deB] << " ";}cout << endl;} int main(){ ll H,W; cin >> H >> W; vector< vector<ll> > M(H+1, vector<ll>(W+1, 0)); vector< vector<ll> > T(H+1, vector<ll>(W+1, 0)); //vector< vector<ll> > A(H+1, vector<ll>(W+1, 0)); REP(y,H){ string S; cin >> S; REP(x,W){ if(S[x] == '+') M[y][x] = 1; else M[y][x] = -1; if((x+y)%2==0) M[y][x] = -M[y][x]; } } M[0][0] = 0; for(int y=H-2; y>=0; y--){ T[y][W-1] = T[y+1][W-1] + M[y][W-1]; } for(int x=W-2; x>=0; x--){ T[H-1][x] = T[H-1][x+1] + M[H-1][x]; } for(int y=H-2; y>=0; y--){ for(int x=W-2; x>=0; x--){ if((x+y)%2==0){ T[y][x] = max(T[y+1][x], T[y][x+1]) + M[y][x]; }else{ T[y][x] = min(T[y+1][x], T[y][x+1]) + M[y][x]; } } } ll ret = T[0][0] + M[H-1][W-1]; if(ret > 0){ cout << "Takahashi" << endl; }else if(ret < 0){ cout << "Aoki" << endl; }else{ cout << "Draw" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<vl> vvl; double EPS=1e-9; int INF=1000000005; long long INFF=1000000000000000005ll; double PI=acos(-1); int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 }; int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 }; ll MOD = 1000000007; #define DEBUG fprintf(stderr, "====TESTING====\n") #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #define OUT(x) cout << x << "\n" #define OUTH(x) cout << x << " " #define debug(...) fprintf(stderr, __VA_ARGS__) #define READ(x) for(auto &(z):x) cin >> z; #define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a)) #define EACH(a, b) for (auto&(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define MAX(a, b) a=max(a, b) #define MIN(a, b) a=min(a, b) #define SQR(x) ((ll)(x) * (x)) #define RESET(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define TC(t) while (t--) #define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL) #define what_is(x) cerr << #x << " is " << x << endl template<typename T_vector> void output_vector(const T_vector &v, bool line_break = false, bool add_one = false, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) { cout << v[i] + (add_one ? 1 : 0) << (line_break ? '\n' : i < end - 1 ? ' ' : '\n'); } } void solve() { int h, w; cin >> h >> w; vvi dp(h, vi(w, -INF)), s(h, vi(w)); REP(i, h) REP(j, w) { char x; cin >> x; if(x == '+') s[i][j] = 1; else s[i][j] = -1; } --h, --w; dp[h][w] = 0; FORD(i, h, 0) { FORD(j, w, 0) { if(i < h) MAX(dp[i][j], s[i + 1][j] - dp[i + 1][j]); if(j < w) MAX(dp[i][j], s[i][j + 1] - dp[i][j + 1]); } } if(dp[0][0] > 0) OUT("Takahashi"); else if(dp[0][0] < 0) OUT("Aoki"); else OUT("Draw"); } int main() { FAST_INP; // #ifndef ONLINE_JUDGE // freopen("input.txt","r", stdin); // freopen("output.txt","w", stdout); // #endif // int tc; cin >> tc; // TC(tc) solve(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " = " << x << endl #define boost ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) { cout << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); } template<typename T> inline void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x *= f; } template<typename T, typename ...Args> inline void read(T& x, Args&... args) { read(x), read(args...); } constexpr int MAXN = 1e5 + 5, MOD = 1e9 + 7; int n; array<int, MAXN> a, cnt; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; for (int j = 2; j <= a[i]; j++) { if (a[i] % j == 0) cnt[j]++; } } int ans = 0, id = 0; for (int i = 1; i <= 1000; i++) { if (cnt[i] > ans) ans = cnt[i], id = i; } cout << id << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++ i) #define rep1(i, n) for (int i = 1; i <= (int)(n); ++ i) #define MP make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; int N; pair<double, double> seg[2005]; int main() { cin >> N; rep(i, N) { int t; cin >> t >> seg[i].first >> seg[i].second; if (t == 2) seg[i].second -= 0.1; if (t == 3) seg[i].first += 0.1; if (t == 4) seg[i].first += 0.1, seg[i].second -= 0.1; } sort(seg, seg + N, [&](pair<double, double> x, pair<double, double> y){ if (x.first == y.first) return x.second > y.second; return x.first < y.first; }); vector<double> ver; rep(i, N) ver.push_back(seg[i].first); LL ret = 0; rep(i, N) { int j = upper_bound(ver.begin() + i, ver.end(), seg[i].second) - ver.begin(); ret += max(0, (j - i - 1)); } printf("%lld\n", ret); return 0; }
#include <bits/stdc++.h> #define f(a, n) for (int a = 0; a < n; a++) #define F(a, n) for (int a = 1; a <= n; a++) #define P (1000000007) using namespace std; struct Square{ bool bulb = 0; bool up = 0; bool down = 0; bool left = 0; bool right = 0; bool block = 0; }; Square A[1505][1505]; int main(){ cin.tie(NULL); ios::sync_with_stdio(false); int H, W; cin >> H >> W; int M, N; cin >> N >> M; int x, y; F(i, N){ cin >> y >> x; A[y][x].bulb = 1; } F(i, M){ cin >> y >> x; A[y][x].block = 1; } f(i, H + 2) A[i][0].block = A[i][W + 1].block = 1; f(j, W + 2) A[0][j].block = A[H + 1][j].block = 1; // making the perimeter all blocks to simplify later logic { // just creating a new scope for no good reason queue<pair<int, int>> Q; F(i, H) F(j, W){ if (A[i][j].bulb){ Q.push({i, j}); } } while (!Q.empty()){ auto p = Q.front(); Q.pop(); int i = p.first, j = p.second; if (!A[i][j].block){ if (!A[i][j].up){ // if we haven't visited this node yet for the up direction Q.push({i - 1, j}); A[i][j].up = 1; } } } } { // just creating a new scope for no good reason queue<pair<int, int>> Q; F(i, H) F(j, W){ if (A[i][j].bulb){ Q.push({i, j}); } } while (!Q.empty()){ auto p = Q.front(); Q.pop(); int i = p.first, j = p.second; if (!A[i][j].block){ if (!A[i][j].right){ // if we haven't visited this node yet for the up direction Q.push({i, j + 1}); A[i][j].right = 1; } } } } { // just creating a new scope for no good reason queue<pair<int, int>> Q; F(i, H) F(j, W){ if (A[i][j].bulb){ Q.push({i, j}); } } while (!Q.empty()){ auto p = Q.front(); Q.pop(); int i = p.first, j = p.second; if (!A[i][j].block){ if (!A[i][j].down){ // if we haven't visited this node yet for the up direction Q.push({i + 1, j}); A[i][j].down = 1; } } } } { // just creating a new scope for no good reason queue<pair<int, int>> Q; F(i, H) F(j, W){ if (A[i][j].bulb){ Q.push({i, j}); } } while (!Q.empty()){ auto p = Q.front(); Q.pop(); int i = p.first, j = p.second; if (!A[i][j].block){ if (!A[i][j].left){ // if we haven't visited this node yet for the up direction Q.push({i, j - 1}); A[i][j].left = 1; } } } } int ans = 0; F(i, H) F(j, W){ if (A[i][j].up || A[i][j].down || A[i][j].left || A[i][j].right) ans++; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int arr[n][2]; for(int i=0;i<n;i++) { for(int j=0;j<2;j++) { cin>>arr[i][j]; } } int min1=arr[0][0]; int min2=arr[0][1]; int temp=0; for(int i=1;i<n;i++) { if(arr[i][0]<min1) { min1=arr[i][0]; temp=i; } if(arr[i][1]<min2 && arr[i][1]!=arr[temp][1]) min2=arr[i][1]; } if(min1+arr[temp][1]<max(min1,min2)) cout<<min1+arr[temp][1]; else cout<<max(min1,min2); return 0; }
#include<iostream> #include<algorithm> #include<map> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<set> #define ll long long #define pb push_back #define forn(i,x,n) for(int i=x;i<=n;++i) #define forr(i,x,n) for(int i=n;i>=x;--i) #define lson (rt<< 1) #define rson (rt<< 1 | 1) #define gmid ((l+r)>> 1 ) using namespace std; const int maxn=20000050; struct node { int v;int pos; }aa[maxn],bb[maxn]; int n,s[maxn],a[maxn],b[maxn],c[maxn]; bool cmp(node a,node b) { return a.v<b.v||a.v==b.v&&a.pos<b.pos; } int lowbit(int x) { return (x&-x); } int query(int x) { int ans=0; while(x) { ans+=s[x]; x-=lowbit(x); } return ans; } void insert(int x) { while(x<=n) { s[x]++; x+=lowbit(x); } } int main() { cin>>n; forn(i,1,n) { cin>>a[i]; aa[i].v=a[i]+i; aa[i].pos=i; } forn(i,1,n) { cin>>b[i]; bb[i].v=b[i]+i; bb[i].pos=i; } sort(aa+1,aa+n+1,cmp); sort(bb+1,bb+n+1,cmp); bool flag=true; for(int i=1;i<=n;++i) if(aa[i].v!=bb[i].v)flag=false; if(flag==false) cout<<-1<<endl; else { ll ans=0; forn(i,1,n) c[aa[i].pos]=bb[i].pos; forn(i,1,n) { ans+=query(n)-query(c[i]); insert(c[i]); } cout<<ans<<endl; } }
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 4000000000000000000 //オーバーフローに注意 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; int table[26]; int work[26]; bool FLG = false; vector<char> V; map<int,int> MAP; int main(){ for(int i = 0; i < 26; i++){ table[i] = 0; } char S[3][12]; int len[3]; for(int i = 0; i < 3; i++){ scanf("%s",S[i]); len[i] = 0; for(int k = 0; S[i][k] != '\0'; k++){ table[S[i][k]-'a']++; len[i]++; } } int count = 0; for(int i = 0; i < 26; i++){ if(table[i] > 0){ V.push_back('a'+i); MAP[i] = count++; } } if(count >= 11){ printf("UNSOLVABLE\n"); return 0; } /*for(int i = 0; i < 3; i++){ printf("%d\n",len[i]); }*/ //printf("count:%d\n",count); ll D[10]; for(int i = 0; i < 10; i++){ D[i] = i; } do{ ll A = 0; bool FLG = true; for(int i = 0; i < len[0]; i++){ if(i == 0 && D[MAP[S[0][i]-'a']] == 0){ FLG = false; break; } A = 10*A + D[MAP[S[0][i]-'a']]; } if(!FLG ||A == 0)continue; ll B = 0; for(int i = 0; i < len[1]; i++){ if(i == 0 && D[MAP[S[1][i]-'a']] == 0){ FLG = false; break; } B = 10*B + D[MAP[S[1][i]-'a']]; } if(!FLG||B == 0)continue; ll C = 0; for(int i = 0; i < len[2]; i++){ if(i == 0 && D[MAP[S[2][i]-'a']] == 0){ FLG = false; break; } C = 10*C + D[MAP[S[2][i]-'a']]; } if(!FLG||C == 0)continue; //printf("A:%lld B:%lld C:%lld\n",A,B,C); if(A+B == C){ for(int i = 0; i < 3; i++){ for(int k = 0; k < len[i]; k++){ printf("%lld",D[MAP[S[i][k]-'a']]); } printf("\n"); } return 0; } }while(next_permutation(D,D+10)); if(!FLG){ printf("UNSOLVABLE\n"); } return 0; }
#include<bits/stdc++.h> #define pb push_back #define pl pair<ll,ll> #define pll pair<ll,pair<ll,ll>> #define ll long long #define vl vector<ll> #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); #define mp make_pair #define w(t) int t;cin>>t;while(t--) #define inf 1e18 #define fi(a,n) for(ll i=a;i<n;i++) #define fr(i,a,b) for(ll i=a;i>=b;i--) #define fj(a,n) for(ll j=a;j<n;j++) #define k(a,n) for(ll k=a;k<n;k++) #define endl "\n" #define all(v) (v).begin(),(v).end() #define set1(x) __builtin_popcount(x) #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) using namespace std; const ll mod= 1000000007; const ll N =1000005; void solve() { ll a[3]; fi(0,3)cin>>a[i]; sort(a,a+3); if(a[2]-a[1]==a[1]-a[0]) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif //w(t) solve(); }
#include<bits/stdc++.h> using namespace std; int main() { int a[3]; cin >> a[0] >> a[1] >> a[2]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (i != j and i != k and j != k) { if (a[j] - a[i] == a[k] - a[j]) { cout << "Yes"; return 0; } } } } } cout << "No"; }
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; using ll = long long; int main(){ ios_base::sync_with_stdio(false); ll n, m; cin >> n >> m; const ll mod = m * m; ll x = 10, y = n, z = 1; while(y > 0){ if(y & 1){ z = (z * x) % mod; } x = (x * x) % mod; y >>= 1; } cout << z / m << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define INF 0x3f3f3f3f #define pi M_PI typedef pair<ll, ll> llPair; typedef vector<ll> vll; typedef vector<ld> vld; typedef vector<bool> vb; typedef priority_queue<ll> pqll; ll MOD = 1000000007; /* Really doe, like really doe Really doe, like really doe */ ll power(ll x, ll y, ll m) { if (y == 0) return 1; ll p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; t = 1; for (ll z = 0; z < t; ++z) { ll n, m; cin >> n >> m; n = power(10, n, m * m); cout << (n / m) % m << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define ll long long #define rep(i,n) for(ll i = 0; i < (n); ++i) #define REP(i,x,n) for(ll i = (x); i < (n); ++i) #define P pair<ll,ll> #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() const ll mod = 1e9+7; const ll INF = 1e18; const double pi = acos(-1); //const ll mod = 998244353; 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; } ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; } int main(void) { ll n, x; string s; cin >> n >> x >> s; rep(i,n){ if(s[i] == 'o') x++; else{ x = max(x-1, 0ll); } } cout << x << endl; return 0; }
#include <iostream> #include <string> using namespace std; int i, h, w, x, y, res; string s[101]; int main(void) { //freopen("b.in", "r", stdin); cin >> h >> w >> x >> y;; for (i = 1; i <= h; i++) { cin >> s[i]; s[i] = " " + s[i]; } res = 0; // (x, y) -> (x, y + 1) -> (x, y + 2) -> ...... // right for (i = 0; ; i++) { if (s[x][y + i] == '#' || y + i > w) break; res++; } // ... <- (x, y - 2) <- (x, y - 1) <- (x, y) // left for (i = 0; ; i++) { if (s[x][y - i] == '#' || y - i < 1) break; res++; } // (x - 2, y) // ^ // (x - 1, y) // ^ // (x, y) // up for (i = 0; ; i++) { if (s[x - i][y] == '#' || x - i < 1) break; res++; } // down for (i = 0; ; i++) { if (s[x + i][y] == '#' || x + i > h) break; res++; } res = res - 3; cout << res; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { ll N; cin >> N; vector<ll> A(N), S(N + 1, 0), M(N + 1, 0); rep(i, N) cin >> A[i]; rep(i, N) { S[i + 1] = S[i] + A[i]; M[i + 1] = max(M[i], S[i + 1]); //cout << S[i] << " " << M[i] << "\n"; } ll ans = 0, x = 0; rep(i, N) { ans = max({ans, x + M[i + 1], x + S[i + 1]}); x += S[i + 1]; } cout << ans << "\n"; //rep(i, N + 1) cout << S[i] << " " << M[i] << "\n"; }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) #define sz(x) int(x.size()) #define show(x) {for(auto i:x){cout << i << " ";} cout << endl;} #define isin(x,l,r) ((l)<=(x) && (x)<(r)) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using tii = tuple<int,int,int>; template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return 1;} else return 0;} template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return 1;} else return 0;} int main() { ll K; cin >> K; if (K == 1) { cout << 1 << '\n'; return 0; } ll ans = 0; // 全て違う数 for (int i = 1; i <= K; i++) { for (int j = i+1; j <= K; j++) { if ((ll)i * j * (j+1) > K) break; int left = j+1; int right = K / (i*j); ans += (right - left + 1) * 6; } } // 2つ同じ数 for (int i = 1; i <= K; i++) { if ((ll)i * i > K) break; int right = K / (i * i); int temp = right; if (right >= i) temp--; // iと重複 ans += temp * 3; } // 全て同じ数 for (int i = 1; i <= K; i++) { if ((ll)i * i * i > K) break; ans++; } cout << ans << '\n'; return 0; // get dp together, ll, paraphrase, debug }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i,n) for(int i=0;i<(n);++i) #define repi(i,a,b) for(int i=int(a);i<int(b);++i) #define rrep(i,n) for(int i=((n)-1);i>=0;--i) #define all(x) (x).begin(), (x).end() #define PI 3.14159265358979323846264338327950L #define mod 1000000007 using namespace std; typedef long long ll; typedef long double ld; int main() { int n; cin>>n; vector<pair<int,string>> vp; rep(i,n){ string s; int a; cin>>s>>a; vp.push_back({a,s}); } sort(all(vp)); cout<<vp[n-2].second; }
#include <bits/stdc++.h> #define pb push_back #define fi first #define se second #define int long long using namespace std; inline void yn(bool a){cout<<(a?"YES":"NO");} void solve() { int n;cin>>n; vector<pair<string,int>>v(n); for(int i=0;i<n;i++)cin>>v[i].fi>>v[i].se; sort(v.begin(),v.end(),[&](pair<string,int>a,pair<string,int>b){ return a.se>b.se; }); cout<<v[1].fi; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tc=1;//cin>>tc; for (int t = 1; t <= tc; t++) { //cout<<"Case #"<<t<<" : "; solve(); cout<<'\n'; } }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i) #define irreps(i, m, n) for (long long i = ((long long)(n)-1); i > (long long)(m); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v+n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cinline(n) getline(cin,n); #define replace_all(s, b, a) replace(s.begin(),s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) (long long)(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll,ll,ll>>; using vb = vector<bool>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const ll INF = 1e9+10; const ll MOD = 1e9+7; const ll LINF = 1e18; signed main() { cin.tie( 0 ); ios::sync_with_stdio( false ); ll a,b,x,y; cin>>a>>b>>x>>y; if(a==b){ cout<<x<<endl; }else if(a<b){ ll ans=x; if(2*x<y) ans+=(b-a)*2*x; else ans+=(b-a)*y; cout<<ans<<endl; }else{ ll ans=x; if(2*x<y) ans+=2*x*(a-b-1); else ans+=y*(a-b-1); cout<<ans<<endl; } }
#include<bits/stdc++.h> //#include<iostream> //#include<string> //#include<cmath> //#include<cstdio> //#include<cstring> //#include<algorithm> //#include<cctype> //#include<vector> //#include<map> //#include<deque> //#include<queue> using namespace std; #define re register #define inf 0x3f3f3f3f //#define inf 0x3f3f3f3f3f3f3f3fll #define ll long long //#define int ll //#define x first //#define y second typedef pair<int,int> PII; template<typename T> inline void read(T &x){ re char ch=getchar();re bool f=false;x=0; while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();} while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} x=f?-x:x; return ; } template<typename T> inline void write(T x){ if(x<0){putchar('-');x=-x;} if(x>=10){write(x/10);} putchar(x%10+'0'); } double const eps=1e-18; int const N=3e5+5; int a[N]; struct SegmentTree{ int l,r; int dat; }t[N*4]; inline void build(int p,int l,int r){ t[p].l=l,t[p].r=r; if(l==r){ t[p].dat=a[l]; return ; } int mid=(l+r)>>1; build(p<<1,l,mid); build(p<<1|1,mid+1,r); t[p].dat=t[p<<1].dat^t[p<<1|1].dat; } inline void modify(int p,int x,int y){ if(t[p].l==t[p].r){ t[p].dat^=y; return ; } int mid=(t[p].l+t[p].r)>>1; if(x<=mid) modify(p<<1,x,y); if(x>mid) modify(p<<1|1,x,y); t[p].dat=t[p<<1].dat^t[p<<1|1].dat; } inline int ask(int p,int l,int r){ if(t[p].l>=l&&t[p].r<=r){ return t[p].dat; } int mid=(t[p].l+t[p].r)>>1; int ans=0; if(l<=mid) ans^=ask(p<<1,l,r); if(mid<r) ans^=ask(p<<1|1,l,r); return ans; } signed main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); int n,m; read(n),read(m); for(int i=1;i<=n;i++){ read(a[i]); } build(1,1,n); while(m--){ int op,x,y; read(op),read(x),read(y); if(op==1){ modify(1,x,y); } else{ write(ask(1,x,y)),puts(""); } } return 0; } /* 3 4 1 2 3 2 1 3 2 2 3 1 2 3 2 2 3 10 10 0 5 3 4 7 0 0 0 1 0 1 10 7 2 8 9 2 3 6 2 1 6 2 1 10 1 9 4 1 6 1 1 6 3 1 1 7 2 3 5 */
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; void Main() { ll n; R n; n*=2; vector<ll> a[3]; rep(i,n) { ll x; char c; cin >> x >> c; if(c=='R') a[0].pb(x); else if(c=='B') a[1].pb(x); else a[2].pb(x); } rep(i,3) sort(all(a[i])); P c[3]; rep(i,3) c[i]=P(a[i].size()%2,i); sort(c,c+3,greater<P>()); ll ans=MAXL; if(c[0].F) { ll x=c[0].S,y=c[1].S,z=c[2].S; rep(i,a[x].size()) { ll k=lower_bound(all(a[y]),a[x][i])-a[y].begin(); if(k<a[y].size()) ans=min(ans,abs(a[x][i]-a[y][k])); if(k) ans=min(ans,abs(a[x][i]-a[y][k-1])); } vector<P> v[2]; rep(i,a[z].size()) { ll k=lower_bound(all(a[x]),a[z][i])-a[x].begin(); if(k<a[x].size()) { ll d=abs(a[z][i]-a[x][k]); v[0].pb(P(d,i)); } if(k) { ll d=abs(a[z][i]-a[x][k-1]); v[0].pb(P(d,i)); } } rep(i,a[z].size()) { ll k=lower_bound(all(a[y]),a[z][i])-a[y].begin(); if(k<a[y].size()) { ll d=abs(a[z][i]-a[y][k]); v[1].pb(P(d,i)); } if(k) { ll d=abs(a[z][i]-a[y][k-1]); v[1].pb(P(d,i)); } } rep(i,2) sort(all(v[i])); rep(i,min(3,(int)v[0].size())) { rep(j,min(3,(int)v[1].size())) { if(v[0][i].S!=v[1][j].S) ans=min(ans,v[0][i].F+v[1][i].F); } } } else ans=0; pr(ans); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
#include<bits/stdc++.h> #define rep(i,N) for(int i=0;i<(N);i++) #define rrep(i, n) for (int i = (int)n-1; i >= 0; --i) #define FOR(i,a,b) for(int i=(a);i<(b);i++) using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e12; const int inf = 1e9; const int mod = 1e9+7; typedef long long ll; typedef pair<ll,int> P; typedef set<int> S; 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; } __attribute__ ((constructor)) void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); } int main(){ int n; ll k; cin >> n >> k; vector<vector<ll>> t(n, vector<ll>(n, 0)); rep(i, n) rep(j, n) cin >> t[i][j]; vector<int> v(n -1); rep(i, n-1) v[i] = i + 1; ll cnt = 0; //N!の順列の全探索 do{ ll now = 0; FOR(i, 0, n-1){ if(i == 0){ now += t[0][v[i]]; } if(i == n - 2){ now += t[v[i]][0]; } if(i != 0){ now += t[v[i - 1]][v[i]]; } } if(now == k) cnt++; }while(next_permutation(v.begin(),v.end())); cout << cnt << endl; return 0; }
#pragma GCC optimize("O2") #pragma GCC target("avx") #pragma clang optimize on #include <cstdio> #include <functional> #include <queue> #include <tuple> using namespace std; inline int_fast32_t get_digit() { int_fast32_t x = 0, f = 1; char c = getchar(); while(c > '9' || c < '0') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = (x * 10) + (c ^ 48); c = getchar(); } return x * f; } int main() { int_fast32_t R, C; scanf("%d%d", &R, &C); int_fast32_t A[250000], B[250000]; int_fast32_t dist[500000], seen[500000] = {0}, cost, pt, to_hide = R * C; priority_queue<pair<int_fast32_t, int_fast32_t>, vector<pair<int_fast32_t, int_fast32_t>>, greater<pair<int_fast32_t, int_fast32_t>>> queue; for(int_fast32_t i = 0; i < R * C; ++i) if(i % C != C - 1) A[i] = get_digit(); for(int_fast32_t i = 0; i < (R - 1) * C; ++i) B[i] = get_digit(); for(int_fast32_t i = 1; i < to_hide + to_hide; ++i) dist[i] = 10000000; queue.emplace(pair<int_fast32_t, int_fast32_t>(0, 0)); while(!queue.empty()) { tie(cost, pt) = queue.top(); if(pt == to_hide - 1) break; queue.pop(); seen[pt] = true; if(pt < to_hide) { if(!seen[pt + 1] && pt % C != C - 1) { if(cost + A[pt] < dist[pt + 1]) { dist[pt + 1] = cost + A[pt]; queue.emplace(pair<int_fast32_t, int_fast32_t>(dist[pt + 1], pt + 1)); } } if(!seen[pt - 1] && pt % C != 0) { if(cost + A[pt - 1] < dist[pt - 1]) { dist[pt - 1] = cost + A[pt - 1]; queue.emplace(pair<int_fast32_t, int_fast32_t>(dist[pt - 1], pt - 1)); } } if(!seen[pt + C] && pt < to_hide - C) { if(cost + B[pt] < dist[pt + C]) { dist[pt + C] = cost + B[pt]; queue.emplace(pair<int_fast32_t, int_fast32_t>(dist[pt + C], pt + C)); } } if(!seen[pt + to_hide] && cost + 1 < dist[pt + to_hide]) { dist[pt + to_hide] = cost + 1; queue.emplace(pair<int_fast32_t, int_fast32_t>(dist[pt + to_hide], pt + to_hide)); } } else { if(to_hide < pt - C && !seen[pt - C] && cost + 1 < dist[pt - C]) { dist[pt - C] = cost + 1; queue.emplace(pair<int_fast32_t, int_fast32_t>(cost + 1, pt - C)); } if(!seen[pt - to_hide] && cost < dist[pt - to_hide]) { dist[pt - to_hide] = cost; queue.emplace(pair<int_fast32_t, int_fast32_t>(cost, pt - to_hide)); } } } printf("%d\n", dist[to_hide - 1]); }
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define pb push_back #define int long long #define all(x) (x).begin(), (x).end() using LL = long long; using LD = long double; using pii = pair<int, int>; using tiii = tuple<int, int, int>; using vii = vector<pii>; using viii = vector<tiii>; const int INF = 1e18; const int MOD = 1e9 + 7; const int N = 505; int R, C; int A[N][N]; int B[N][N]; viii g[N + N][N + N]; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> R >> C; for (int r = 1; r <= R; r++) { for (int c = 1; c <= C - 1; c++) { cin >> A[r][c]; } } for (int r = 1; r <= R - 1; r++) { for (int c = 1; c <= C; c++) { cin >> B[r][c]; } } for (int r = 1; r <= R; r++) { for (int c = 1; c <= C; c++) { if (c + 1 <= C) g[r][c].pb({r, c + 1, A[r][c]}); if (c - 1 >= 1) g[r][c].pb({r, c - 1, A[r][c - 1]}); if (r + 1 <= R) g[r][c].pb({r + 1, c, B[r][c]}); g[r][c].pb({r + R, c + C, 1}); g[r + R][c + C].pb({r, c, 0}); if (r - 1 >= 1) { g[r + R][c + C].pb({r - 1 + R, c + C, 1}); } } } auto dijkstra = [&] (int x0, int y0) { vector<vector<int>> dist(2*R + 1, vector<int>(2*C + 1, INF)); dist[x0][y0] = 0; set<tiii> S; S.insert({0, x0, y0}); while (!S.empty()) { auto [d, x1, y1] = *S.begin(); S.erase(S.begin()); for (auto [x2, y2, z]: g[x1][y1]) { if (dist[x2][y2] > d + z) { S.erase({dist[x2][y2], x2, y2}); dist[x2][y2] = d + z; S.insert({dist[x2][y2], x2, y2}); } } } return dist[R][C]; }; cout << dijkstra(1, 1) << "\n"; }
#include <bits/stdc++.h> using namespace std; int a, b, c; double dp[105][105][105]; double solve(int a, int b, int c) { if(dp[a][b][c]) return dp[a][b][c]; if(a==100 or b==100 or c==100) return 0; double ans = 0; ans += (solve(a+1,b,c)+1)*a/(a+b+c); ans += (solve(a,b+1,c)+1)*b/(a+b+c); ans += (solve(a,b,c+1)+1)*c/(a+b+c); dp[a][b][c] = ans; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b >> c; cout << setprecision(10) << fixed << solve(a, b, c); return 0; }
//g++ -std=gnu++14 a.cpp #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <tuple> #include <iomanip> #include <random> #include <math.h> #include <stdio.h> using namespace std; #define ll long long #define rep(i, n) for(ll i = 0; i < (n); i++) #define P pair<ll,ll> ll MOD = 1e9 + 7; int INF = 1 << 30; ll INFL = 1LL << 60; ll MODP = 998244353; const ll M = 1000000007; ll lmax(ll x,ll y){ if(x < y) return y; else return x; } ll lmin(ll x, ll y){ if(x > y)return y; else return x; } ll gen(ll x,ll y){ if(y == 0)return 1; if(y == 1)return x; if(y%2 == 1){ return ((x%M)*gen(x,y-1)%M)%M; }else{ ll res = gen(x,y/2)%M; return ((res%M)*(res%M))%M; } } ll gcd(ll x, ll y){ if(x < y) swap(x,y); if(y == 0) return x; return gcd(y,x%y); } ll lcm(ll x,ll y){ ll s = gcd(x,y); x /= s; y /= s; return x*y*s; } int main(){ int h,w; cin >> h >> w; vector<string> a(h); rep(i,h) cin >> a[i]; pair<int,int> s,g; vector<vector<pair<int,int>>> p(26); vector<int> sumi(26,1); rep(i,h)rep(j,w){ if(a[i][j] == 'S'){ s.first = i; s.second = j; } else if(a[i][j] == 'G'){ g.first = i; g.second = j; }else if(a[i][j] != '.' && a[i][j] != '#'){ char mozi = a[i][j]; p[mozi-'a'].push_back(make_pair(i,j)); }else{ continue; } } queue<pair<int,int>> q; vector<int> di = {0,1,0,-1}, dj = {1,0,-1,0}; vector<vector<int>> ans(h,vector<int>(w,-1)); ans[s.first][s.second] = 0; q.push(s); ll cnt = 0; while(!q.empty()){ int i = q.front().first,j = q.front().second; rep(k,4){ cnt++; int ni = i+di[k],nj = j+dj[k]; if(ni < 0 || ni >= h || nj < 0 || nj >= w)continue; if(a[ni][nj] == '#')continue; if(a[ni][nj] == 'S')continue; if(a[ni][nj] == 'G'){ if(ans[ni][nj] == -1) ans[ni][nj] = ans[i][j] + 1; else ans[ni][nj] = min(ans[i][j] + 1,ans[ni][nj]); } else if(a[ni][nj] == '.'){ if(ans[ni][nj] == -1){ ans[ni][nj] = ans[i][j] + 1; q.push(make_pair(ni,nj)); }else if(ans[ni][nj] <= ans[i][j]+1){ continue; }else{ ans[ni][nj] = ans[i][j] + 1; q.push(make_pair(ni,nj)); } }else{ char m = a[i][j]; if(a[ni][nj] != m){ if(ans[ni][nj] == -1){ ans[ni][nj] = ans[i][j] + 1; q.push(make_pair(ni,nj)); }else if(ans[ni][nj] <= ans[i][j]+1){ continue; }else{ ans[ni][nj] = ans[i][j] + 1; q.push(make_pair(ni,nj)); } } } } if(a[i][j] != 'S' && a[i][j] != 'G' && a[i][j] != '.' && a[i][j] != '#' && sumi[a[i][j]-'a'] == 1){ for(auto x : p[a[i][j]-'a']){ cnt++; if(x.first == i && x.second == j)continue; if(ans[x.first][x.second] == -1){ ans[x.first][x.second] = ans[i][j] + 1; q.push(make_pair(x.first,x.second)); }else if(ans[x.first][x.second] <= ans[i][j]+1){ continue; }else{ ans[x.first][x.second] = ans[i][j] + 1; q.push(make_pair(x.first,x.second)); } } sumi[a[i][j]-'a'] = 0; } q.pop(); cnt++; } cout << ans[g.first][g.second] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<long long >>; int C[2000] = {0}; int main(){ int N,M; cin >> N >> M; for(int i=0;i<N;i++){ int a; cin >> a; C[a]++; } for(int i=0;i<M;i++){ int b; cin >> b; C[b]++; } for(int i=0;i<2000;i++){ if(C[i]==1){ cout << i <<' '; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using v1 = vector<ll>; using v2 = vector<vector<ll>>; #define MP(a,b) make_pair((a),(b)) #define MT(...) make_tuple(__VA_ARGS__) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl; #define sz(x) (int)(x).size() #define HOGE cout << "hoge" << endl; using P = pair<ll,ll>; template<typename T>inline istream& operator>>(istream&i,vector<T>&v) {REP(j,sz(v))i>>v[j];return i;} const ll INF = 1e9; const ll mod = 1e9+7; int main(){ int n, m; cin >> n >> m; v1 x(n+m); REP(i,n+m) cin >> x[i]; x.push_back(0); x.push_back(INF); v1 ans; sort(x.begin(),x.end()); for(int i = 1; i <= n+m; i++){ if(x[i] != x[i+1] && x[i] != x[i-1]) ans.push_back(x[i]); } if(ans.size()!=0){ cout << ans[0]; REP(i,ans.size()-1) cout << " " << ans[i+1]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n; cin>>n; int m; cin>>m; int a[n][n]; for(int i=0;i<n;++i) for(int j=0;j<n;++j) {a[i][j]=0;if(i==j) a[i][j]=1;} for(int i=0;i<m;++i) { int x,y; cin>>x>>y; x--;y--; a[x][y]=1; a[y][x]=1; } bool ok[1<<n]; for(int mask=0;mask<(1<<n);++mask) { int sum=0; vector <int> v; for(int i=0;i<n;++i) { if(mask & (1<<i)) { v.push_back(i); } } for(int i=0;i<v.size();++i) for(int j=0;j<v.size();++j) sum+=a[v[i]][v[j]]; ok[mask]=(sum==(v.size()*v.size())); } int res[1<<n]; res[0]=0; for(int mask=1;mask<(1<<n);++mask) { if(ok[mask]) { res[mask]=1; continue; } res[mask]=1e18; for(int i=mask;;i=((i-1) & mask)) { if(ok[i]) { res[mask]=min(res[mask],res[mask^i]+1); } if(i==0) { break; } } } cout<<res[(1<<n)-1]; return 0; }
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <iomanip> #include <functional> #include <bitset> #include <limits> #include <cstdio> #include <cmath> #include <cassert> #include <random> #ifdef DEBUG #include "library/Utility/debug.cpp" #else #define debug(...) #endif #define rep(i,n) for(int i=0;i<(n);++i) #define EL '\n' #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() using lnt = long long; struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO; template<typename T> using V = std::vector<T>; template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; } /*-*/ int main() { int n,m; std::cin >> n >> m; V<V<int> > g(n,V<int>(n,0)); rep(i,n) g[i][i]=1; rep(i,m) { int a,b; std::cin >> a >> b; a--;b--; g[a][b]=1; g[b][a]=1; } V<lnt> comp(1<<n,0); rep(i,1<<n) { int ok=true; rep(j,n) { rep(k,n) { if((i>>j&1)&&(i>>k&1)&&g[j][k]==0) ok=false; } } comp[i]=ok; } V<lnt> dp(1<<n,n); rep(S,1<<n) { if(comp[S]) { dp[S]=1; continue; } for(int T=(S-1)&S;T>0;T=(T-1)&S) { dp[S]=std::min(dp[S],dp[T]+dp[S^T]); } } print(dp[(1<<n)-1]); }
#include <bits/stdc++.h> using namespace std; int main(){ cin.tie(NULL); ios_base::sync_with_stdio(false); long long n, p; cin >> n >> p; long long sq = sqrt(p) ; string ans = "No"; for(int i = 1; i <= sq; i++){ if(p % i != 0) continue; long long pr = i, an = p/i; if(pr + an == n) ans = "Yes"; } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using pl = pair<ll, ll>; const ll INF = ll(1e18); const ll mod = ll(998244353); const double pi = acos(-1); #define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i)) #define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i)) #define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i)) #define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i)) #define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i)) #define rnfor(i,a,b) for(ll (i) = (b) - 1; (i) >= (a); --(i)) #define pf(x) cout << (x) << endl #define all(x) (x).begin(),(x).end() #define yes pf("Yes") #define no pf("No") 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 int dx[8]={1,0,-1,0,1,1,-1,-1}; const int dy[8]={0,1,0,-1,1,-1,1,-1}; int multipf(vector<string>& s){ cout << s[0]; rep1(i, s.size() - 1)cout << " " << s[i]; cout << endl; return 0; } int multipf(vector<ll>& n){ cout << n[0]; rep1(i, n.size() - 1)cout << " " << n[i]; cout << endl; return 0; } ll gcd(ll a,ll b){ if(a < b)swap(a, b); if(b == 0) return a; return gcd(b,a%b); } ll lcm(ll a,ll b){ ll g = gcd(a,b); return a / g * b; } ll factorial(ll n){ ll ans = 1; rep1(i, n){ ans *= i; ans %= mod; } return ans; } ll power(ll a, ll b){ ll ans = 1; while(b) { if(b & 1LL) ans = ans * a % mod; ans %= mod; a = a * a; a %= mod; b >>= 1; } return ans % mod; } //modの値の確認をすること int main(){ ll s,p; cin >> s >> p; for (ll i = 1; i * i <= p; ++i) { if(i * (s - i) == p){ yes; return 0; } } no; return 0; }
#include <iostream> using ll = long long; int solve() { ll n; std::cin >> n; if(n % 4 == 0) return 2; else if(n % 2 == 0) return 1; else return 0; } int main() { int t; std::cin >> t; for(int testcase = 1; testcase <= t; testcase++) { int val = solve(); if(val == 0) std::cout << "Odd\n"; else if(val == 1) std::cout << "Same\n"; else if(val == 2) std::cout << "Even\n"; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int t; ll n; int main() { cin>>t; while(t--) { cin>>n; if(n%2) {cout<<"Odd"<<endl;continue; } else { n/=2; if(n%2) { cout<<"Same"<<endl;continue; } else { cout<<"Even"<<endl;continue; } } } }
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <utility> #define rep(i, n) for(int i = 0; i < (int)(n); i++) using ll = long long; using namespace std; int N,X; int d; int main() { cin >> N >> X; rep(i,N) { cin >> d; if(d != X) cout << d << " "; } return 0; }
#include<iostream> #include<algorithm> #include<vector> using namespace std; vector<int> remove(int a,int b,vector<int> v){ int target=b; vector<int> vec; for(int i{0};i<a;i++){ if(target != v.at(i)){ vec.push_back(v.at(i)); } } return vec; } int main(){ int a{}; cin>>a; int b{}; cin>>b; vector<int> vec; for(int i{0};i<a;i++){ int num{}; cin>>num; vec.push_back(num); } vector<int> v=remove(a,b,vec); for(auto c:v){ cout<<c<<" "; } cout<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define FOR(i, x, y) for(int i = (int)(x); i < (int)(y); ++i) #define MP make_pair #define fst first #define snd second typedef pair<int, int> pii; const int maxn = 4e5 + 5; int n, top = 0; int a[maxn], ord[maxn], rev[maxn], stk[maxn]; char s[maxn]; inline bool cmp(const int &i, const int &j){ return a[i] < a[j]; } int main(){ scanf("%d", &n); FOR(i, 0, n << 1){ scanf("%d", a + i); ord[i] = i; } sort(ord, ord + (n << 1), cmp); FOR(i, 0, n << 1) rev[ord[i]] = i; FOR(i, 0, n << 1){ if(top && (rev[stk[top]] < n) != (rev[i] < n)){ s[stk[top]] = '(', s[i] = ')'; --top; } else stk[++top] = i; } puts(s); return 0; }
#include <bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f #define PI 3.1415926535898 #define pb push_back #define F first #define S second #define lson i << 1 #define rson i << 1 | 1 #define mem(x, y) memset(x, y, sizeof(x)) #define fin(x) freopen(x, "r", stdin) #define fout(x) freopen(x, "w", stdout) #define fcl fclose(stdin), fclose(stdout) #define ioss ios::sync_with_stdio(false), cout.tie(NULL) #define pii pair<int, int> #define vec vector #define all(x) x.begin(), x.end() #define lowbit(x) x &(-x) const int MAX = 6e5 + 10; const int MOD = 1e9 + 7; using namespace std; int n, c[MAX], a[MAX]; void update(int i, int k) //定点更新 { while (i <= n) { c[i] += k; i += lowbit(i); } } int getsum(int i) //求和 { int res = 0; while (i > 0) { res += c[i]; i -= lowbit(i); } return res; } int main() { ioss; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]++; } ll ans = 0; for (int i = 0; i < n; i++) { ans += i - getsum(a[i]); update(a[i], 1); } memset(c, 0, sizeof(c)); cout << ans << endl; for (int i = 0; i < n - 1; i++) { int k = getsum(a[i]), p = i - k; update(a[i], 1); // cout << k << endl; ans -= a[i] - 1; ans += n - a[i]; cout << ans << endl; } return 0; } // (2 * a + n - 1)n/2 // an + n2/2 = x
#include <bits/stdc++.h> using namespace std; int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0)); using Int = long long; int main() { int N, M; cin >> N >> M; map<int, vector<int>> black; for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; black[y].push_back(x); } for (auto &[y, xs] : black) { sort(begin(xs), end(xs)); } set<pair<int, int>> visited; int ans = 0; stack<pair<int, int>> S; S.emplace(0, N); while (!S.empty()) { auto [x, y] = S.top(); S.pop(); if (visited.count(make_pair(x, y))) { continue; } visited.emplace(x, y); auto &C = black[y]; auto itC = lower_bound(begin(C), end(C), x + 1); int xlim = -1; if (itC == end(C)) { ans++; xlim = 2 * N; } else { xlim = *itC; } auto &L = black[y - 1]; auto itL = lower_bound(begin(L), end(L), x + 1); while (itL != end(L) && *itL <= xlim) { S.emplace(*itL, y - 1); ++itL; } auto &R = black[y + 1]; auto itR = lower_bound(begin(R), end(R), x + 1); while (itR != end(R) && *itR <= xlim) { S.emplace(*itR, y + 1); ++itR; } } cout << ans << '\n'; return 0; }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; 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; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, M; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M; map<int, vector<int>> pawns; rep(i, 0, M) { int X, Y; cin >> X >> Y; pawns[X].push_back(Y); } set<int> ans; ans.insert(N); fore(p, pawns) { set<int> ng; set<int> ok; fore(y, p.second) { if (ans.count(y - 1)) ok.insert(y); else if (ans.count(y + 1)) ok.insert(y); else ng.insert(y); } fore(i, ng) ans.erase(i); fore(i, ok) if (0 <= i && i <= 2 * N) ans.insert(i); } cout << ans.size() << endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N,M; cin >> N >> M; vector<int> g(N); rep(i,M){ int a,b; cin >> a >> b; a--; b--; g[a] |= 1 << b; g[b] |= 1 << a; } vector<int> dp(1 << N, 1e9); dp[0] = 1; rep(i,N)rep(j,1<<N) if(dp[j] == 1 && (g[i] & j) == j) dp[j | 1 << i] = 1; rep(i,1<<N)for(int j = i; --j &= i;) dp[i] = min(dp[i], dp[i ^ j] + dp[j]); cout << dp.back() << endl; }
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> v(n, vector<int>(n, 0)); for(int i = 0; i < n; i++) v[i][i] = 1; int a, b; for(int i = 0; i < m; i++) { cin >> a >> b; v[a - 1][b - 1] = 1; v[b - 1][a - 1] = 1; } vector<bool> dp((1 << n), false); for(int i = 0; i < (1 << n); i++) { bool good = true; for(int j = 0; j < n && good; j++) { if(!((i >> j) & 1)) continue; for(int k = 0; k < n && good; k++) { if(!((i >> k) & 1)) continue; if(v[k][j] == 0) good = false; } } if(good) dp[i] = true; } vector<int> ret(1 << n, 1e9); ret[0] = 0; for(int i = 1; i < (1 << n); i++) { if(dp[i]) { ret[i] = 1; continue; } for(int mask = i; mask; mask = (mask - 1) & i) { ret[i] = min(ret[i], ret[mask] + ret[i ^ mask]); } } cout << ret[(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define M 998244353 #define inf 1e8 #define pb push_back #define ff first #define ss second #define int long long int #define ld long double #define pi pair<li, li> #define ins insert #define vc vector #define umap unordered_map #define uset unordered_set #define all(x) x.begin(), x.end() #define fr(i, a, n) for (int i = a; i < n; i++) #define frr(i, a, n) for (int i = n - 1; i >= a; i--) #define file_in freopen("a.txt", "r", stdin); const int mxn=1e6; void tc() { int n; cin>>n; string s; cin>>s; map<int,int> m; m[0] = 1; int csum = 0 , ans=0; for(int i=0; i<n; i++) { if(s[i] == 'A') csum -= inf; else if(s[i] == 'T') csum += inf; else if(s[i] == 'G') csum += 1; else csum -= 1; if(m.count(csum)) ans += m[csum]; m[csum]+=1; } cout<<ans<<"\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t=1; // cin>>t; while(t--) tc(); }
#include<iostream> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<map> #include<queue> #include<cstdio> #include<cmath> #define inf 0x3f3f3f3f using namespace std; const int N = 5010; int n,f[N][N][4]; string s; int main(){ // freopen("1.txt","r",stdin); cin>>n>>s; int sum=0; for(int i=0;i<n;i++){ int a=0,t=0,c=0,g=0; for(int j=i;j<n;j++){ if(s[j]=='A') a++; else if(s[j]=='T') t++; else if(s[j]=='C') c++; else g++; if(a==t&&c==g) sum++; } } cout<<sum<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < (n); i++) void ng() { puts("No"); exit(0); } int main() { int N; cin >> N; V<int> A(N), B(N); int sz = N * 2; V<int> tp(sz), com(sz, -1); rep(i, N) { cin >> A[i] >> B[i]; if (A[i] != -1) { --A[i]; if (tp[A[i]]) ng(); tp[A[i]] = i + 1; } if (B[i] != -1) { --B[i]; if (tp[B[i]]) ng(); tp[B[i]] = -(i + 1); } if (A[i] != -1 && B[i] != -1) { if (A[i] > B[i]) ng(); com[A[i]] = B[i]; com[B[i]] = A[i]; } } V<bool> dp(sz + 1); dp[0] = true; rep(i, sz) { if (!dp[i]) continue; for (int j = i + 1; j < sz; j += 2) { int w = (j - i + 1) / 2; bool ok = true; rep(k, w) { int p = i + k, q = i + k + w; if (com[p] != -1 && com[p] != q) { ok = false; } if (com[q] != -1 && com[q] != p) { ok = false; } if (tp[p] != 0 && tp[q] != 0 && tp[p] + tp[q] != 0) { ok = false; } if (tp[p] < 0 || tp[q] > 0) { ok = false; } } if (ok) { dp[j + 1] = true; } } } puts(dp[sz] ? "Yes" : "No"); return 0; }
#include<bits/stdc++.h> using namespace std; #define int ll #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} 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;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" "); return os; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } signed main(){ int N,M;cin>>N>>M; map<int,vector<int>> v; rep(i,M){ int h,w;cin>>h>>w; v[h].push_back(w); } set<int> st; st.insert(N); for(auto &p:v){ vector<int> nxt,ers; for(auto &j:p.second){ if(st.count(j)){ if(!st.count(j-1) and !st.count(j+1)) ers.push_back(j); }else{ if(st.count(j-1) or st.count(j+1)) nxt.push_back(j); } } for(auto &j:ers) st.erase(j); for(auto &j:nxt) st.insert(j); } cout<<st.size()<<endl; return 0; }
//#include <atcoder/all> #include <bits/stdc++.h> using namespace std; #define rep2(x,fr,to) for(int x=(fr);x<(to);x++) #define rep(x,to) for(int x=0;x<(to);x++) #define repr(x,fr,to) for(int x=(fr);x>=(to);x--) #define all(c) c.begin(),c.end() #define sz(v) (int)v.size() typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9+7; //998244353; void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);} //using mint = atcoder::modint1000000007; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >>n; VI p(n), b(n), usd(n-1); rep(i, n){ int z; cin >>z; z--; p[i] = z; b[z] = i; } VI ans; rep(i, n){ if(p[i]==i) continue; int zs = b[i]; for(int j=zs; j-1>=i ; j--){ if(usd[j-1]==1){ usd[0] = 1111111; goto lpout;} swap(p[j], p[j-1]); ans.push_back(j-1); b[p[j]] = j; b[p[j-1]] = j-1; usd[j-1]=1; } } lpout: if(count(all(usd),1) != n-1){cout <<"-1\n"; return 0;} rep(i, n) if(p[i] !=i){cout <<"-1\n"; return 0;} for(auto x :ans) cout<<x+1<<"\n"; return 0; }
#define ll loli #include<bits/stdc++.h> #define pb push_back #define eb emplace_back #define push emplace #define lb(x, v) lower_bound(all(x), v) #define ub(x, v) upper_bound(all(x), v) #define sz(x) (int)(x.size()) #define re(x) reverse(all(x)) #define uni(x) x.resize(unique(all(x)) - x.begin()) #define all(x) x.begin(), x.end() #define mem(x, v) memset(x, v, sizeof x); #define int long long #define pii pair<int, int> #define inf 1e9 #define INF 1e18 #define mod 1000000007 #define F first #define S second #define IO ios_base::sync_with_stdio(0); cin.tie(0); #define chmax(a, b) (a = (b > a ? b : a)) #define chmin(a, b) (a = (b < a ? b : a)) using namespace std; void trace_() {cerr << "\n";} template<typename T1, typename... T2> void trace_(T1 t1, T2... t2) {cerr << ' ' << t1; trace_(t2...); } #define trace(...) cerr << "[" << #__VA_ARGS__ << "] :", trace_(__VA_ARGS__); int n; signed main() { IO; cin >> n; int i, sum = 0; for (i = 1; sum < n; i++) { sum += i; } cout << i - 1 << '\n'; }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back template <class T>bool chmax(T &a, T b) {if (a < b) {a = b;return 1;}return 0;} template <class T>bool chmin(T &a, T b) {if (a > b) {a = b;return 1;}return 0;} typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; const int inf = 1e9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int main() { int n, x; cin >> n >> x; string s; cin >> s; for (int i = 0; i < s.size(); i++){ if(s[i]=='o')x++; else if(x!=0)x--; } cout << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } int to_int(string s){ bool f=false; if(s[0]=='-'){ f=true; s=s.substr(1); } int cnt=4; int n=s.size(); for(int i=0;i<n;i++){ if(s[i]=='.'){ s=s.substr(0,i)+s.substr(i+1); cnt-=(n-1-i); break; } } int res=stoi(s); for(int i=0;i<cnt;i++){ res*=10; } if(f)res*=-1; return res; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); string s; ll x,y,r; cin >> s; x=to_int(s); cin >> s; y=to_int(s); cin >> s; r=to_int(s); ll res=0; constexpr int N=10000; auto uo=[](ll x)->ll{ if(x<=0){ x-=(N-1); x+=abs(x)%N; return x; } x-=abs(x)%N; return x; }; auto uoo=[](ll x)->ll{ if(x<=0){ x+=abs(x)%N; return x; } x+=(N-1); x-=abs(x)%N; return x; }; for(ll i=-r;i<=r;){ if(abs(y+i)%N!=0){ i++; continue; } ll L=0,R=2e9; while(R-L>1){ ll mid=(L+R)/2; if(mid*mid+i*i<=r*r){ L=mid; } else{ R=mid; } } // cout << i << endl; ll X=uo(x+L),XX=uoo(x-L); if(X>=XX)res+=(X-XX)/N+1; i+=N; } cout << res << endl; }
// //#ifdef ONLINE_JUDGE //#pragma GCC optimize("Ofast") //#pragma GCC optimize("O2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") ////// //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") //#endif #include <bits/stdc++.h> #include <utility> #define all(x) (x).begin(), (x).end() #define allp(x) (x)->begin(), (x)->end() #define pb push_back using namespace std; void dout() { cerr << endl; } typedef long long ll; typedef long double ld; template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } //using ll = long long; //#ifdef __int128 //using hll = __int128; //#endif //using pii = pair<ll, ll>; //using ld = long double; template <typename T> void do_uniq(vector<T> &vec){ sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin()); } #ifndef ONLINE_JUDGE clock_t timestamp_start = clock(); void time_calc() { cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << endl; } #endif #ifdef _getchar_nolock #else # define _getchar_nolock getchar_unlocked #endif #define integer ll integer mod = 1e9 + 7; integer ml(integer a, integer b) { return (a * 1ll * b) % mod; } integer add(integer a, integer b) { return (a + b) % mod; } integer sub(integer a, integer b) { return add(a, mod - b); } integer sq(integer a) { return ml(a, a); } integer b_p(integer b, integer p) { if (p == 0) return 1; if (p & 1) return ml(b, b_p(b, p - 1)); return sq(b_p(b, p >> 1)); } int gcd(int x, int y){ return y == 0 ? x : gcd(y, x % y); } #define solvsh //#define multi #ifdef solvsh //#define int ll const int MAXN = 5e3 + 32; void solve() { int n; cin >> n; vector<int> v; for (int i = 0; i < n; ++i) { int curr; cin >> curr; v.push_back(curr); } ll mmx = 0; ll pref = 0; ll add = 0; for (int i = 0; i < n; ++i) { mmx = max(mmx, 0ll + v[i]); ll answr = (i + 1) * mmx; pref += v[i]; add += pref; answr += add; cout << answr << "\n"; } } #else #endif void multisolve() { #ifndef ONLINE_JUDGE freopen("../input.in", "r", stdin); #endif int t; cin >> t; int i = 1; while (t--) { // cout << "Case #" << i << ": "; solve(); // i++; } } #ifndef ONLINE_JUDGE void gen() { #ifndef ONLINE_JUDGE freopen("../input.in", "w", stdout); #endif } #endif #define int int int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cout << fixed << setprecision(20); cerr << fixed << setprecision(20); #ifdef multi // gen(); multisolve(); // checkk(); #else solve(); #endif // time_calc(); }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<long long> A(N+1); for(int i=1; i<=N; i++) cin >> A.at(i); int max = 0; long long ans; long long sum; max = A.at(1); sum = A.at(1); ans = A.at(1)*2; cout << ans << endl; for(int i=2; i<=N; i++){ sum += A.at(i); ans += sum; if(max < A.at(i)){ ans += (i-1)*(A.at(i)-max); max = A.at(i); } ans += max; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9+7; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); string s; cin >> s; cout << s[1] << s[2] << s[0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using pll = pair<ll, ll>; #define sz(x) (int)(x).size() template <typename T> bool ckmin (T& a, T b) { return (b < a) ? a = b, 1 : 0; } template <typename T> bool ckmax (T& a, T b) { return (b > a) ? a = b, 1 : 0; } #ifdef XVENOM #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: " << dbg_str(__VA_ARGS__) #else #define dbg(...) ; #endif template <typename U, typename V> string to_string (pair<U, V>); string to_string (const char* e) { return "\"" + string(e) + "\""; } string to_string (string& e) { return "\"" + e + "\""; } string to_string (char e) { return "\'" + string(1, e) + "\'"; } string to_string (bool e) { return e ? "true" : "false"; } template <typename T> string to_string (T e) { string s = "[ "; for (auto& x : e) s += to_string(x) + " "; return s + "]"; } template <typename U, typename V> string to_string (pair<U, V> e) { return "(" + to_string(e.first) + ", " + to_string(e.second) + ")"; } string dbg_str () { return " \n"; } template <typename U, typename... V> string dbg_str (U u, V... v) { return " " + to_string(u) + dbg_str(v...); } /* *** */ int main() { ios_base::sync_with_stdio(0); cin.tie(0); auto f = [&] (ll x) { ll res = 1; while (x--) res *= 10; return res; }; ll n; cin >> n; ll ans = 0; for (ll i = 0; i <= 5; i++) { ll lo = f(3 * i), hi = f(3 * i + 3) - 1; ckmin(hi, n); ans += i * (hi - lo + 1); if (hi == n) break; } cout << ans << '\n'; }
#include <bits/stdc++.h> // #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> // find_by_order returns iterator to element present at that index... // order_of_key returns no. of elements having value strictly less than that given in the order_of_key(); #warning !!!check the size of arrayss!!! // NUMERIC DEFINITIONS : #define MOD 1000000007 #define newMOD 998244353 #define pi 3.1415926535897932384626433832795 #define ld long double #define ll long long #define int long long #define INF 1000000000000000008 #define Need_for_speed(activated) ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) #define satisfy ll t; cin>> t; while(t--) #define mp make_pair #define pb push_back #define vi vector<int> #define x first #define y second #define endl "\n" #define pii pair<ll,ll> #define input freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout) #define time cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; using namespace std; ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} ll power(int x, unsigned int y, unsigned int m){ if (y == 0) return 1;int p = power(x, y/2, m) % m; p = (p * p) % m;return (y%2 == 0)? p : (x * p) % m;} ll modInverse(int a, int m){{ return power(a, m-2, m);}} //ll fact[lim] = {0}; //ll inv[lim]; //void facto(ll modd) { // // fact[0] = 1; fact[1] = 1; // // for(int i = 2; i < lim; i++){fact[i] = (fact[i-1]*i)%modd;} // inv[lim-1] = modInverse(fact[lim-1],modd); // for(ll i=lim-2;i>=0;i--) // inv[i]=(inv[i+1]*(i+1))%modd; // //} void __print(int 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 see(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define see(x...) #endif //ll sub(ll x, ll y) { // return (x - y + MOD) % MOD; //} //ll mul(ll x, ll y) { // return (x * y) % MOD; //} //ll add(ll x, ll y) { // return (x + y) % MOD; //} //ll ncr(ll n, ll r,ll modd) //{ // if(r>n)return 0; // ll res = 1; // res = fact[n]; // res*=inv[r]; // res%=modd; // res*=inv[n-r]; // res%=modd; //// res*=modInverse(fact[r],modd); //// res%=modd //// res*=modInverse(fact[n-r],modd); // return res; //} //*************************** REASON EVERY LINE OF CODE *************************** //*************************** REASON EVERY LINE OF CODE *************************** //*************************** REASON EVERY LINE OF CODE *************************** void solve() { ll n;cin>>n; ll x =0; for(ll i=1;i<=n;i++) { x+=i; if(x>=n) { cout<<i<<endl;return; } } } signed main() { Need_for_speed(activated); //never give up ,irrespective of rating increment/decrement !!!! //Clear the global DATA strucrures!!! //read question statement carefully... // satisfy // { // solve(); // } solve(); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 62; int mod = 1000000007; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int A[4]; rep(i, 4) cin >> A[i]; int sum = A[0]+A[1]+A[2]+A[3]; rep(i, 16){ int val = i; int res = 0; rep(j, 4){ if(val&1) res += A[j]; val >>= 1; } if(res == sum-res){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++) #define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--) #define pb push_back #define mp make_pair #define pll pair<ll,ll> #define endl "\n" using ll=long long; using ld=long double; using namespace std; ll mod= 1e9+7; ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; } vector<pair<ld,ld>> x; int main(){ ll n,ans=0; cin>>n; rep(i,0,n){ ld t,l,r; cin>>t>>l>>r; if((ll)t%2==0)r-=0.5; if(t>2)l+=0.5; x.pb(mp(l,r)); } rep(i,0,n-1){ rep(j,i+1,n){ if(x[i].second>=x[j].first && x[j].second>=x[i].first)ans++; } } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define all(v) v.begin(), v.end() #define rep(i, begin, end) for(auto i = begin; i < (end); i++) #define contains(v, x) (find(all(v), x) != v.end()) template<class T> bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; } template<class T> bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; } template<class T> T roundup(T a, T b){ return (a + b - 1) / b; } const vector<int> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; const vector<int> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; using ll = long long; const ll INF = 1e9; const double EPS = 1e-10; /* #include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> namespace mp = boost::multiprecision; using Bint = mp::cpp_int; using d32 = mp::number<mp::cpp_dec_float<32>>; */ const ll MOD = 1e9 + 7; /* #include <atcoder/all> using namespace atcoder; using mint = modint1000000007; */ using P = pair<int, int>; struct Edge{ int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} }; using Graph = vector<vector<int>>; void solve(){ int sy, sx, ty, tx; cin >> sy >> sx >> ty >> tx; rep(_, 0, ty - sy) cout << "D"; rep(_, 0, sy - ty) cout << "U"; rep(_, 0, tx - sx) cout << "R"; rep(_, 0, sx - tx) cout << "L"; cout << endl; cout.flush(); } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); rep(_, 0, 1000){ solve(); int k; cin >> k; } }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <sstream> #include <stdio.h> #include <cmath> #include <cctype> #include <map> #include <queue> #include <set> #define INT_INF 1e9 #define LONG_INF 1e18 #define MOD 1e9 + 7 #define PI 3.14159265358979323846 typedef long long ll; using namespace std; int main() { int n; cin >> n; if (n == 0) { cout << "Yes" << endl; return 0; } while (n % 10 == 0) { n = n / 10; } string a = to_string(n); int i = 0, j = a.length() - 1; bool f = true; while (i < j) { if (a[i] != a[j]) { f = false; break; } i += 1; j -= 1; } if (f) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for (int i = a; i < (b); i++) #define range(a) a.begin(), a.end() #define endl "\n" #define Yes() cout << "Yes" << endl #define No() cout << "No" << endl #define MP make_pair using P = pair<int, int>; const unsigned long long mod = 1e9 + 7; const long long INF = 1LL<<60; void chmin(long long &a, long long b) { if (a > b) a = b; } void chmax(long long &a, long long b) { if (a < b) a = b; } int main(void){ ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector<ll> A(N, 0), B(N, 0); ll amin = INF, bmin = INF; vector<vector<ll>> C(N, vector<ll>(N, 0)); FOR(i,0,N){ FOR(j,0,N){ ll c; cin >> c; C[i][j] = c; A[i] += c; B[j] += c; } } FOR(i,0,N){ amin = min(amin, A[i]); bmin = min(bmin, B[i]); } FOR(i,0,N){ A[i] -= amin; B[i] -= bmin; } FOR(i,0,N){ if(A[i] % N != 0 || B[i] % N != 0){ No(); return 0; } A[i] /= N; B[i] /= N; } ll add = C[0][0] - A[0] - B[0]; if(add < 0){ No(); return 0; } FOR(i,0,N){ B[i] += add; } FOR(i,0,N){ FOR(j,0,N){ if(A[i] + B[j] != C[i][j]){ No(); return 0; } } } Yes(); FOR(i,0,N){ cout << A[i] << " "; } cout<<endl; FOR(i,0,N){ cout << B[i] << " "; } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c,d; cin >> a >> b >> c >> d; cout << min(min(a,b),min(c,d)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int a, b, c; cin >> a >> b >> c; if (c&1){ int na = (a < 0) ? -1 : 1; int nb = (b < 0) ? -1 : 1; if (na == nb) { if (na == -1){ swap(a, b); if (a > b) cout << '>'; else if (a==b) cout << '='; else cout << '<'; } else { if (a > b) cout << '>'; else if (a==b) cout << '='; else cout << '<'; } } else { if (na < nb) cout << '<'; else cout << '>'; } } else { a = abs(a); b = abs(b); if (a > b) cout << '>'; else if (a==b) cout << '='; else cout << '<'; } }
/* Written By mafailure */ //In the name of God #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less using namespace std; using namespace __gnu_pbds; #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl "\n" #ifdef ill #define int long long #endif template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef AATIF_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif //#define int long long // int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1}; // int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2}; const long long mod = 1e9 + 7; const double eps=1e-9; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef vector<bool> vb; typedef pair<int, int> ii; typedef vector< pair< int, int > > vii; typedef map<int, int> mii; typedef pair<int, ii> pip; typedef pair<ii, int> ppi; #define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i]; #define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m]; #define w(t) int t;cin>>t; while(t--) #define takeInp(n) int n;cin>>n; #define fr(i,init,final) for(int i=init;i<final;i++) #define frr(i,init,final) for(int i=init;i>=final;i--) #define Fr(i,final) for(int i=0;i<final;i++) #define Frr(i,first) for(int i=first;i>=0;i--) #define fi first #define se second #define mp make_pair #define pb push_back #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define debug(x) cerr<<">value ("<<#x<<") : "<<x<<endl; #define setb __builtin_popcount #define lsone(n) (n&(-n)) #define rlsone(n) (n&(n-1)) #define clr(a,b) memset(a,b,sizeof(a)) const int inf= INT_MAX; //struct point_i{int x,y;}; struct point_i{int x,y; point_i(){ x=0; y=0;} point_i(int x_,int y_){x=(x_);y=(y_) ;} }; struct point { float x,y; point (){x=y=0.0;} point (float x_,float y_){x=(x_); y=(y_);} bool operator < (point other) const{ if(fabs(x-other.x)>eps)return x<other.x; return y<other.y; } bool operator == (point other) const { return fabs(other.x-x)<=eps&& fabs(other.y-y)<=eps; } } ; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; vi init(string s) { istringstream sin(s); int n; vi arr; while(sin>>n)arr.push_back(n); return arr; } int power(int x, int y) { if(y==0)return 1; int u=power(x,y/2); u=(u*u)%mod; if(y%2)u=(x*u)%mod; return u; } int gcd(int a,int b) { if(a<b)return gcd(b,a); return (b==0?a:(a%b?gcd(b,a%b):b)); } int gcd_e(int a,int b,int &x,int &y) { if(b==0){x=1; y=0; return a;} int x1,y1; int p=gcd_e(b,a%b,x1,y1); x=y1; y=x1-(a/b)*y1; return p; } int Min (int a,int b){return min(a,b);} int Max(int a,int b){ return max(a,b);} signed main() { IOS int sz=0; int num=1; int n; cin>>n; int a[n]; fr(i,0,n) cin>>a[i]; fr(i,2,1001) { int cnt=0; fr(j,0,n) { if(a[j]%i==0)cnt++; } if(cnt>sz){ sz=cnt; num=i; } } cout<<num<<endl; }
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <iomanip> #include <sstream> #include <fstream> #include <stdint.h> #include <string.h> #define _USE_MATH_DEFINES #include <math.h> #include <vector> #include <list> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> #include <deque> #include <string> #include <algorithm> #include <functional> #include <bitset> #include <functional> #include <chrono> #include <random> #define sqr(x) (x) * (x) typedef unsigned int u32; typedef int i32; typedef unsigned long long int u64; typedef long long int i64; typedef uint16_t u16; typedef int i16; typedef uint8_t u8; typedef int8_t i8; using namespace std; using namespace std::chrono; const i64 mod = 1'000'000'000ll + 7; //const i64 mod = 998'244'353ll; const i64 inf = 1'000'000'000'000'000'000ll; const long double eps = 1e-8; int main(int argc, char* argv[]) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed); if (sizeof(i64) != sizeof(long long int)) { cerr << "i64 != long long int" << endl; } i64 n; cin >> n; vector<i64> a(n); for (i64 i = 0; i < n; i++) { cin >> a[i]; } i64 br = 0; i64 bc = 0; for (i64 i = 2; i <= 1000; i++) { i64 c = 0; for (i64 v : a) { if (v % i == 0) { c += 1; } } if (bc < c) { bc = c; br = i; } } cout << br << endl; return 0; }
//#pragma GCC optimize("Ofast") //#pragma GCC optimize("O2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") // //#pragma GCC target("ssse3,sse4,popcnt,abm,mmx") #include <bits/stdc++.h> #include <utility> #define all(x) (x).begin(), (x).end() #define allp(x) (x)->begin(), (x)->end() #define pb push_back #define GLIBCDEBUG using namespace std; void dout() { cerr << endl; } //typedef long long ll; template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } using ll = long long; //#ifdef __int128 //using hll = __int128; //#endif using pii = pair<ll, ll>; using ld = long double; template <typename T> void do_uniq(vector<T> &vec){ sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin()); } #ifndef ONLINE_JUDGE clock_t timestamp_start = clock(); void time_calc() { cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n"; } #endif #ifdef _getchar_nolock #else # define _getchar_nolock getchar_unlocked #endif #define integer int integer mod = 1e9 + 7; integer ml(integer a, integer b) { return (a * 1ll * b) % mod; } integer add(integer a, integer b) { integer rt = a + b; if (rt < mod) return rt; return rt - mod; } integer sub(integer a, integer b) { return add(a, mod - b); } integer sq(integer a) { return ml(a, a); } integer b_p(integer b, ll p) { if (p == 0) return 1; if (p & 1) return ml(b, b_p(b, p - 1)); return sq(b_p(b, p >> 1)); } integer divv(integer a, integer b) { return ml(a, b_p(b, mod - 2)); } const ll inf = 1e9 + 231; #define solvsh //#define multi //#define GLIBCXX_DEBUG //#define STRESS #ifdef solvsh const int MAXN = 20; int tables[MAXN][MAXN]; int lenn[MAXN]; int wss[26]; void solve() { ll answr = 0; string s; cin >> s; int n = s.size(); int ptr = n - 1; while (ptr > 0) { if (s[ptr - 1] == s[ptr]) { answr += n - 1 - ptr - wss[s[ptr] - 'a']; for (int i = 0; i < 26; ++i) wss[i] = 0; wss[s[ptr] - 'a'] = n + 1 - ptr; --ptr; --ptr; } else { wss[s[ptr] - 'a']++; --ptr; } } cout << answr << "\n"; } #else #endif #ifndef STRESS void multisolve() { // precalc(); int t; cin >> t; int i = 1; while (t--) { solve(); i++; } } void singlesolve (){ // precalc(); solve(); // cerr << cnt << "\n"; } #else #endif #ifndef ONLINE_JUDGE #endif #define int int signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cout << fixed << setprecision(20); #ifdef multi // gen(); multisolve(); #else singlesolve(); // gen(); #endif }
#include <bits/stdc++.h> #define rep(a,n) for (ll a = 0; a < (n); ++a) using namespace std; //using namespace atcoder; using ll = long long; typedef pair<ll,ll> P; typedef pair<ll,P> PP; typedef vector<vector<int> > Graph; 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 INF = 1e18; #define SZ(x) ((int)(x).size()) #define debug(v) cout<<#v<<": ",prt(v); template <typename A,typename B> inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";} template <typename A,typename B,typename C> inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";} inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';} template <typename T> inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template<typename T> inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } } template <typename T> inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} /* l = s.size() 各文字の出現回数について累積和を取っておく lが大きい方から見ていく s[i]==s[i+1]!=s[i+2]ならひっくり返す 前回ひっくり返した文字と違う文字なら全部ひっくり返して、同じ文字なら途中までひっくり返す */ int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); string s; cin >> s; ll l = s.size(); vector<vector<ll> >cnt(26,vector<ll>(l+1,0)); for(int i=1;i<=l;i++){ rep(j,26)cnt[j][i]=cnt[j][i-1]; int id = s[i-1]-'a'; cnt[id][i]++; } ll last_id = l+1; ll last_char = '.'; ll ans = 0; //debug(cnt[2]); for(int i=l-2;i>=1;i--){ if(s[i-1]==s[i]&&s[i+1]!=s[i-1]){ ans += (last_id-i)-(cnt[s[i-1]-'a'][last_id-1]-cnt[s[i-1]-'a'][i-1]); //cout << (last_id-i) << endl; //cout << (cnt[s[i]-'a'][last_id-1]-cnt[s[i]-'a'][i-1]) << endl; if(last_char!=s[i-1]){ ans += l-last_id+1; //cout << ans << endl; } last_char = s[i-1]; last_id = i+2; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) #define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i) #define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define popcount __builtin_popcount using ll = long long; constexpr ll mod = 1e9 + 7; constexpr ll INF = 1LL << 60; // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> T mymod(T x, T n, const T &p = -1) { //x^nをmodで割った余り T ret = 1; while (n > 0) { if (n & 1) { if (p != -1) ret = (ret * x) % p; else ret *= x; } if (p != -1) x = (x * x) % p; else x *= x; n >>= 1; } return ret; } ll gcd(ll n, ll m) { ll tmp; while (m != 0) { tmp = n % m; n = m; m = tmp; } return n; } ll lcm(ll n, ll m) { return abs(n) / gcd(n, m) * abs(m); //gl=xy } using namespace std; //using namespace atcoder; template<int mod> struct Modint{ int x; Modint():x(0){} Modint(int64_t y):x((y%mod+mod)%mod){} Modint &operator+=(const Modint &p){ if((x+=p.x)>=mod) x -= mod; return *this; } Modint &operator-=(const Modint &p){ if((x+=mod-p.x)>=mod) x -= mod; return *this; } Modint &operator*=(const Modint &p){ x = (1LL * x * p.x) % mod; return *this; } Modint &operator/=(const Modint &p){ *this *= p.inverse(); return *this; } Modint operator-() const { return Modint(-x); } Modint operator+(const Modint &p) const{ return Modint(*this) += p; } Modint operator-(const Modint &p) const{ return Modint(*this) -= p; } Modint operator*(const Modint &p) const{ return Modint(*this) *= p; } Modint operator/(const Modint &p) const{ return Modint(*this) /= p; } bool operator==(const Modint &p) const { return x == p.x; } bool operator!=(const Modint &p) const{return x != p.x;} Modint inverse() const{//非再帰拡張ユークリッド int a = x, b = mod, u = 1, v = 0; while(b>0){ int t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return Modint(u); } Modint pow(int64_t n) const{//繰り返し二乗法 Modint ret(1), mul(x); while(n>0){ if(n&1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os,const Modint &p){ return os << p.x; } }; using modint = Modint<mod>; template<typename T> struct Combination{ //Modint用 //構築O(N),クエリO(1) vector<T>fact,rfact; Combination(int n):fact(n+1),rfact(n+1){ fact[0]=1;fact[1]=1; rfact[n]=1; for(int i=2;i<=n;++i){ fact[i]=fact[i-1]*i; } rfact[n]/=fact[n]; for(int i=n-1;i>=0;--i){ rfact[i]=rfact[i+1]*(i+1); } } T C(int n,int r) const{ if(r<0 || n<r)return 0; return fact[n]*rfact[n-r]*rfact[r]; } }; void solve() { ll n,k; cin>>n>>k; vector<ll>a(n); rep(i,n)cin>>a[i]; constexpr ll m=998244353; Combination<Modint<m>>c(k+1); vector<Modint<m>>sum(k+1),sum2(k+1); rep(i,n){ Modint<m>x=1,y=1; rep(j,k+1){ sum[j]+=x; sum2[j]+=y; x*=a[i]; y*=a[i]*2; } } REP(i,1,k+1){ Modint<m>ans=0; rep(j,i+1){ ans+=c.C(i,j)*sum[j]*sum[i-j]; } ans=(ans-sum2[i])/2; cout<<ans<<"\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); solve(); return 0; }
/* ID: gurban1 LANG: C++ TASK: */ #include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 3e2+5; const int mod=998244353; const int maxn=2e5+5; int n,k; int a[maxn]; ll C[N][N],ans[N]; ll dp[maxn],pw[maxn][N]; ll bigmod(ll a,ll b) {if(b==0)return 1;ll x=bigmod(a*a%mod,b>>1);return b&1?x*a%mod:x;} int main(){ // freopen("","r",stdin); // freopen("","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i],pw[i][0] = 1; C[0][0] = 1; for(int i = 0;i <= k;i++){ for(int j = 0;j <= i;j++){ if(j == 0){C[i][j] = 1;continue;} if(i == j){C[i][j] = 1;continue;} C[i][j] = C[i-1][j-1] + C[i-1][j]; if(C[i][j] > mod) C[i][j] -= mod; } } ll iki = bigmod(2,mod-2); dp[0] = n; for(int i = 1;i <= k;i++){ for(int j = 1;j <= n;j++){ pw[j][i] = (pw[j][i-1] * a[j])%mod; dp[i] = (dp[i] + pw[j][i]); if(dp[i] > mod) dp[i] -= mod; } } for(int i = 0;i <= k;i++){ for(int j = i;j+i <= k;j++){ ll nw = (dp[i] * dp[j])%mod; nw -= dp[i + j]; if(nw < 0) nw += mod; if(i == j){nw = (nw * iki) % mod;} nw = (C[i+j][i] * nw) % mod; // if(i == 1 and j == 1) cout<<nw<<'\n'; ans[i + j] += nw; if(ans[i+j] > mod) ans[i+j] -= mod; } } for(int i = 1;i <= k;i++) cout<<ans[i]<<'\n'; }
#include <bits/stdc++.h> #include <cstdlib>//absで絶対値 #define rep(i,n) for(int i=0;i<(n);i++) using ll = long long; using namespace std; int main(){ string S;cin>>S; cout<<S.at(1)<<S.at(2)<<S.at(0)<<endl; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") #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 char cn[1200010]; char *ci = cn; 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; } char tc[200000], * di = tc + 100000; const char d[2] = "0"; int main() { cin.tie(0); ios::sync_with_stdio(false); rep(i, 100000) tc[i] = '0'; rep(i, 100000) di[i] = '1'; fread(cn, 1, 1200010, stdin); int T = getint(); rep(t, T) { int N = getint(); fwrite(di - N, 1, N << 1, stdout); puts(d); ci += 6 * N + 3; } Would you please return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef map<ll,ll>::iterator itll; typedef long double ld; typedef map<ll,ll> mapll; #define con continue #define pb push_back #define fi first #define se second #define fr front() #define INF 1000000000000000000 #define all(vl) vl.begin(),vl.end() #define m_p make_pair #define sz(a) sizeof(a) #define forn(mp,it) for(it = mp.begin();it!=mp.end();it++) #define FOR(i,a,n) for(ll i=a;i<n;i++) #define FORE(i,a,n) FOR(i,a,n+1) #define Endl endl #define eNdl endl #define enDl endl #define endL endl #define MIN(x,y) x=min(x,y) #define MAX(x,y) x=max(x,y) #define left 0 #define right 1 const double one = 1.0; const double PI = 3.14159265/*358979323846*/; const ll maxn = 1e4+10; ll Ceil(ll a,ll b) { return (a+b-1)/b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n,m; cin>>n>>m; vector<ll> blue; FOR(i,0,m) { ll a; cin>>a; blue.pb(a); } if(m == 0) { cout<<1<<endl; return 0; } sort(all(blue)); vector<ll> dis; FOR(i,0,blue.size()) { if(i == 0) { if(blue[i]-1>1)dis.pb(blue[i]-1); } if(blue[i+1]-blue[i]>1)dis.pb(blue[i+1]-blue[i]-1); if(i == blue.size()-1) { if(n-blue[i]>1)dis.pb(n-blue[i]); } } ll mi = 1e18; FOR(i,0,dis.size()) { mi = min(mi,dis[i]); } ll ans = 0; FOR(i,0,dis.size()) { ans+=Ceil(dis[i],mi); } cout<<ans<<Endl; return 0; }
#include <bits/stdc++.h> #define pb push_back #define ll long long #define ull unsigned long long #define mp make_pair #define si short int #define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define pill pair<ll,ll> #define f first #define s second #define pilc pair<ll,char> #define all(a) (a).begin(),(a).end() #define rep(s,e,step) for(int i = (s); i < (e) ; i += step) #define vrep(s,e,step) for(int j = (s); j < (e) ; j += step) #define ex exit(0) #define sz(a) (a).size() #define triple pair<pill, ll> #define pinode pair<node*, node*> #define quadra pair<pill, pill> #define ld long double using namespace std; const ll N = 1e6 + 100; const ll M = 1e6 + 101; const ll big = 1e17; const ll hsh2 = 1964325029 ; const long long mod = 1e9 + 7; const long double EPS = 1e-10; const ll block = 1e7; const ll shift = 2e3; const ld pi = acos(-1.0); ll n, m; ll a[N]; int main() { speed; cin >> n >> m; ll L = 1, sz = n, ans = 0; for(int i = 1; i <= m; i++) cin >> a[i]; sort(a + 1, a + m + 1); for(int i = 1; i <= m; i++) { if(a[i] != L) sz = min(a[i] - L, sz); L = a[i] + 1; } if(a[m] != n) sz = min(sz, n - a[m]); L = 1; for(int i = 1; i <= m; i++) { ans += (a[i] - L + sz - 1) / sz; L = a[i] + 1; } ans += (n - L + sz) / sz; cout << ans; } /* 4 0 3 5 5 4 1 1 1 */
#include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll n,m,val,valu; cin>>n; double ans=0; for(ll i=1; i<n; i++) { ans+=(1.00*n)/(n-i); } cout<<fixed<<setprecision(14)<<ans<<endl; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) x.begin(),x.end() class CTo3 { public: void solve(std::istream& cin, std::ostream& cout) { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; int ans = INT_MIN; for(int i = 1; i < (1 << s.size()); i++){ int cur = 0; int len = 0; for(int j = 0; j < s.size(); j++) if (i & (1 << j)){ cur += s[j] - '0'; len++; } if (cur % 3 == 0 && len > ans){ ans = len; } } if (ans == INT_MIN){ cout << -1 << endl; return; } cout << s.size() - ans << endl; } }; #define loveAfterTheGame main int loveAfterTheGame() { CTo3 solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for (int i = 0; i < (n); ++i) #define DREP(i,s,n) for(int i = (s); i < (n); i++) 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;} using ll = long long; using P = pair<int,int>; using Pl = pair<long long,long long>; using veci = vector<int>; using vecl = vector<long long>; using vecveci = vector<vector<int>>; using vecvecl = vector<vector<long long>>; const int MOD = 1000000007; const double pi = acos(-1); 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*b/gcd(a,b);} int main() { int N; cin >> N; veci A(N); REP(i,N) cin >> A[i]; sort(A.begin(),A.end()); veci B(N); iota(B.begin(),B.end(),1); if(A==B)cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define pb push_back #define fr(i,x,n) for(ll i=x;i<n;i++) #define all(x) x.begin(),x.end() #define fi first #define se second #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) //----------------------------------------------------------------------- const int nax =1; int fac[nax]; int mod_inv[nax]; inline int add(int a, int b){ int c = (a%mod) + (b%mod); if(c >= mod) c -= mod; return c; } inline int mul(int a, int b){ return ((a%mod) * 1ll * (b%mod)) % mod; } inline int sub(int a, int b){ int c = (a%mod) - (b%mod); if(c < 0) c += mod; return c; } inline ll gcd(ll a,ll b){return __gcd(a,b);} inline ll lcm(ll a,ll b){return (a*b)/gcd(a,b);} //----------------------------------------------------------------------- bool comparision(const pair<int,int> &a,const pair<int,int> &b) { if(a.second==b.second) return (a.first>b.first); else return (a.second>b.second); } ll power(ll x,ll y) { ll p=mod; ll res = 1; x=x%p; while(y>0) { if (y&1) res=(res*x)%p; y=y>>1; x=(x*x)%p; } return res; } void pre() { fac[0] = fac[1] = 1; for(int i = 2;i<nax;++i){ fac[i] = mul(i , fac[i-1]); } mod_inv[0] = mod_inv[1] = 1; for(int i =2;i<nax;++i){ mod_inv[i] = power(fac[i] , mod - 2); } } ll ncr(ll n, ll r) { if(r > n)return 0LL; int ans = mul(fac[n] , mod_inv[r]); ans = mul(ans , mod_inv[n-r]); return ans; } ll count_bit_set(ll n) { int c=0; while(n) { n&=(n-1); c++; } return c; } ll mod_inverse(ll n) { //fermate little theorem return power(n,mod-2); } void dfs(int i,vector<int> &v) { int n; vector<bool> visit(21,0); vector<vector<int>> g(21,vector<int>(21,0)); visit[i]=1; v.push_back(i); for(int k=1;k<=n;k++){if(visit[k]==0 && g[i][k]>0)dfs(k,v);} } ll maxSubArraySum(ll p,ll q,vector<ll> &v) { ll msf =-1e16, meh = 0; for (int i = p;i<=q; i++) { meh = meh + v[i]; if (msf < meh)msf = meh; if(meh<0)meh = 0; } return msf; } void seiveoferatosthenes() { int mx=1e7; vector<int> seive(mx+1,1); for(int i=2;i*i<=mx;i++) {if(seive[i]==1)for(int j=i*i;j<=mx;j+=i)seive[j]=0;} } void factorize(ll n,vector<pair<ll,ll>> &v) { ll i,count = 0; while (!(n % 2)) { n >>= 1; count++; } if (count){v.push_back({count,2});} for (i=3; i<=sqrt(n);i+= 2) { count=0; while(n%i == 0){ count++; n=n/i; } if(count)v.push_back({count,i}); } if (n>2)v.push_back({1,n}); } ll ceil(ll n,ll k) { return (n+k-1)/k; } //----------------------------------------------------------------------- void solve() { //read the instructions vector<int> mark(1001,0); int n; cin>>n; int ar[n]; fr(i,0,n){cin>>ar[i];mark[ar[i]]=1;} fr(i,1,n+1){if(mark[i]==0){cout<<"No"<<endl;return;}} cout<<"Yes"<<endl; } /*----------------------------------------------------------------------- 1.Read the question properly(name). 2.Read the question properly(name):| 3.Cheak the constraints. 4.Better to look for edge cases and examples. 5.why the contraints and values are like that?{sum always take ll} 6.think the opposite way -----------------------------------------------------------------------*/ signed main() { fast; int test=1; //cin>>test; //pre(); for(int i=1;i<=test;i++) { //cout<<"Case #"<<i<<": "; solve(); } }
#include "bits/stdc++.h" using namespace std; #define ll long long #define ld long double #define f first #define s second #define pb push_back #define all(x) x.begin(), x.end() #define mp(x, y) make_pair(x, y) #define sz(li) (int) (li).size() #define sum(a) (accumulate ((a).begin(), (a).end(), 0ll)) #define mine(a) (*min_element((a).begin(), (a).end())) #define maxe(a) (*max_element((a).begin(), (a).end())) #define mini(a) (min_element((a).begin(), (a).end()) - (a).begin()) #define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin()) #define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define vi vector<int> #define vl vector<ll> #define vvi vector<vi> #define vvl vector<vl> #define pii pair<int, int> #define pll pair<ll, ll> #define vpii vector<pii> #define vpll vector<pll> #define endl '\n' #define rep(i, a, b) for(int i = (a); i < (b); i++) template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v); template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; } template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "[ "; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << " ]"; } template<typename A> ostream& operator<<(ostream &cout, vector<vector<A>> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) { cout << " "; } cout << v[i]; if (i < v.size() - 1) { cout << endl; } } return cout << "]"; } template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } template<typename T, typename U> void cmin(T& a, U b){ if (a > b) a = b; } template<typename T, typename U> void cmax(T& a, U b){ if (a < b) a = b; } void solve() { int n; cin >> n; vi t(n); for (int i = 0; i < n; i++) { cin >> t[i]; } int mx = accumulate(all(t), 0); vector<bool> possible(mx + 1); possible[0] = true; vi can; for (auto& T : t) { for (int i = sz(possible) - 1; i >= T; i--) { if (possible[i - T] && !possible[i]) { can.pb(i); possible[i] = true; } } } sort(all(can), greater<int>()); int mn = INT_MAX; for (auto& s : can) { mn = min(mn, max(s, mx - s)); } cout << mn << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } cerr << "Time: " << 1000.0*clock()/CLOCKS_PER_SEC << " ms\n"; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void){ int N; cin >> N; vector<int> A(N); vector<int> B(N); vector<int> C(N); vector<int> cnt(N); for(int i=0;i<N;i++){ int t; cin >> t; A[i] = t-1; } for(int i=0;i<N;i++){ int t; cin >> t; B[i] = t-1; } for(int i=0;i<N;i++){ int t; cin >> t; C[i] = t-1; } long long Ans = 0; for(int i=0;i<N;i++){ cnt[B[C[i]]]+=1; } for(int i=0;i<N;i++){ Ans+=cnt[A[i]]; } cout << Ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int tc; cin >> tc; while(tc--){ string str; cin >> str; int n=(int)str.size(); int res=0; string tar="atcoder"; if(str>tar){ cout << 0 << "\n"; continue; } bool allz=true; int ind=-1; for(int i=0;i<n;i++){ if(str[i]!='a'){ allz=false; if(ind==-1) ind=i; } } if(allz){ cout << -1 << "\n"; continue; } while(ind>0){ swap(str[ind],str[ind-1]); ind--; res++; if(str>tar){ cout << res << "\n"; res=-1; break; } } if(res!=-1){ cout << -1 << "\n"; continue; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; // #include <atcoder/all> // using namespace atcoder; const ll INF = 1000000000000000; int main(){ int n; cin >> n; vl x(n), y(n); rep(i,n) cin >> x[i] >> y[i]; ll max_x = -INF; ll max_y = -INF; ll min_x = INF; ll min_y = INF; rep(i,n){ max_x = max(max_x, x[i]); max_y = max(max_y, y[i]); min_x = min(min_x, x[i]); min_y = min(min_y, y[i]); } bool ok_max = true, ok_min = true; // cout << ok_min << ok_min << endl; rep(i,n){ if(x[i] == max_x && y[i] == max_y) { // cout << "max" << endl; ok_max = false; // cout << x[i] << max_x << y[i] << max_y << endl; } if(x[i] == min_x && y[i] == min_y) { // cout << "min" << endl; ok_min = false; // cout << x[i] << min_x << y[i] << min_y << endl; } } sort(x.begin(),x.end()); sort(y.begin(),y.end()); vl ans(6); ans[0] = abs(x[n-1] - x[0]); ans[1] = abs(x[n-2] - x[0]); ans[2] = abs(x[n-1] - x[1]); ans[3] = abs(y[n-1] - y[0]); ans[4] = abs(y[n-2] - y[0]); ans[5] = abs(y[n-1] - y[1]); sort(ans.rbegin(), ans.rend()); if(ok_max || ok_min) cout << ans[1] << endl; else cout << ans[2] << endl; // cout << ok_max << ok_min << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define all(v) begin(v), end(v) #define sz(v) v.size() #define INF 1e18+9 #define EPSILON 1e-14 template <typename T> bool chmax(T &a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T& b) { if (a > b) { a = b; return true; } return false; } int main() { ll N; cin >> N; vector<ll> X(N), C(N); vector<vector<ll>> ball(N+2, vector<ll>(0)); ball.at(0).emplace_back(0); rep(i, N){ cin >> X.at(i) >> C.at(i); ball.at(C.at(i)).emplace_back(X.at(i)); } ball.at(N+1).emplace_back(0); rep(i, N+2){ sort(all(ball.at(i))); } vector<vector<ll>> dp(N+2, vector<ll>(2, 0)); //dp.at(i).at(0)は色iまでを回収して左端にいるときの最小時間 //dp.at(i).at(1)は右端にいる ll p = 0; rep(i, N+1){ if(ball.at(i+1).empty()){ dp.at(i+1).at(0) = dp.at(i).at(0); dp.at(i+1).at(1) = dp.at(i).at(1); } else{ ll preleft = ball.at(p).at(0); ll preright = ball.at(p).at(sz(ball.at(p))-1); ll left = ball.at(i+1).at(0); ll right = ball.at(i+1).at(sz(ball.at(i+1))-1); // preleft -> left dp.at(i+1).at(0) = abs(preleft-right) + right - left + dp.at(i).at(0); // preleft -> right dp.at(i+1).at(1) = abs(preleft-left) + right - left + dp.at(i).at(0); // preright -> left chmin(dp.at(i+1).at(0), abs(preright-right) + right - left + dp.at(i).at(1)); // preright -> right chmin(dp.at(i+1).at(1), abs(preright-left) + right - left + dp.at(i).at(1)); p = i+1; } } cout << min(dp.at(N+1).at(0), dp.at(N+1).at(1)) << '\n'; }
#include <iostream> #include <cmath> #include <string> #include <algorithm> #include <iomanip> using namespace std; long long int max(long long int a,long long int b){ if(a>b){ return a; } return b; } long long int min(long long int a,long long int b){ if(a<b){ return a; } return b; } int main() { int N; cin>>N; int colour[N][2];//stores min and max for each colour for(int i=0;i<N;i++){ colour[i][0]=0; colour[i][1]=0; } for(int i=0;i<N;i++){ int x,c; cin>>x>>c; if(colour[c-1][0]==0){ colour[c-1][0]=x; colour[c-1][1]=x; } if(colour[c-1][0]>x){colour[c-1][0]=x;} if(colour[c-1][1]<x){colour[c-1][1]=x;} } long long int last_left[2]; last_left[0]=0; last_left[1]=0; long long int last_right[2];//moves taken, then place of current person last_right[0]=0; last_right[1]=0; for(int i=0;i<N;i++){ long long int next_left[2],next_right[2];//moves then place if(colour[i][0]==0){continue;} next_right[1]=colour[i][1]; next_left[1]=colour[i][0]; long long int distR=max(last_right[1]-next_right[1],next_right[1]-last_right[1]); long long int distL=max(last_left[1]-next_right[1],next_right[1]-last_left[1]); next_left[0]=min(last_right[0]+distR+next_right[1]-next_left[1],last_left[0]+distL+next_right[1]-next_left[1]); distR=max(last_right[1]-next_left[1],next_left[1]-last_right[1]);//changing to next color distL=max(last_left[1]-next_left[1],next_left[1]-last_left[1]); next_right[0]=min(last_right[0]+distR+next_right[1]-next_left[1],last_left[0]+distL+next_right[1]-next_left[1]); last_left[0]=next_left[0]; last_left[1]=next_left[1]; last_right[0]=next_right[0]; last_right[1]=next_right[1]; } cout<<min(last_right[0]+max(last_right[1],-last_right[1]),last_left[0]+max(last_left[1],-last_left[1]))<<endl; }