code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define speedX ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) using namespace std; // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE template<typename T> void __p(T a) { cout << a << " "; } template<typename T, typename F> void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; } template<typename T> void __f(const char* s, T t) { cout << s << " : "; __p(t); } template<typename T> void __t(const char* s, const T &x) { cout << s << " : { "; for (auto it : x) __p(it); cout << "} ";} template<typename T, typename ... F> void __f(const char* s, T t, F ... f) { int i = 0; for (;; ++i) if (s[i] == ',') break; cout.write(s, i) << " : "; __p(t); cout << " | "; __f(s + i + 1, f...); } #define trace(...) { cout<<"LINE: "<<__LINE__<<" || "; __f(#__VA_ARGS__,__VA_ARGS__); cout<<"\n\n"; } #define debug(...) { cout<<"LINE: "<<__LINE__<<" || "; __t(#__VA_ARGS__,__VA_ARGS__); cout<<"\n\n"; } int begtime = clock(); #define end_time() cout << "\n\nTime of execution: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; #else #define trace(...) #define debug(...) #define end_time() #endif #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; typedef vector<pii> vii; inline bool equals(ld a, ld b) {return fabs(a - b) < 1e-9;} inline int gcd(int a, int b) { a=abs(a); b=abs(b); while(a>0 && b>0) (a>b?a%=b:b%=a); return (a==0?b:a); } inline int power(int x, int n, int m = LLONG_MAX) { int res=1; x = (x%m + m)%m; while(n) { if(n&1) res=(res*x)%m; x=(x*x)%m; n >>= 1; } return res; } #define pb push_back #define mp make_pair #define mt make_tuple #define ff first #define ss second #define uset unordered_set #define umap unordered_map #define all(x) x.begin(), x.end() #define mod 1000000007 void solve(){ int n,a,b; cin>>n>>a>>b; cout<<(n-a+b)<<"\n"; } signed main(){ speedX; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t=1; //cin>>t; for(int i=1;i<=t;++i) { //cout<<"Case #"<<i<<": "; solve(); } end_time(); return 0; }
#include <iostream> #include <cstdio> using namespace std; int n, a, b; int main() { cin >> n >> a >> b; cout << n - a + b << endl; return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define sz(a) (int)(a).size() #define all(a) (a).begin(), (a).end() #define reset(a,v) memset((a), v, sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> ii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ii> vii; int n; bool checkDecimal(int x) { while (x) { int mods = x%10; if (mods == 7) return true; x /= 10; } return false; } bool checkOctal(int x) { while (x) { int mods = x%8; if (mods == 7) return true; x /= 8; } return false; } int main() { scanf("%d", &n); int ans = 0; for (int i = 1; i <= n; ++i) { if (!checkDecimal(i) && !checkOctal(i)) { ++ans; } } printf("%d\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define mp make_pair typedef long long int ll; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ll n; cin >> n; ll k = 0; set<ll> s; for (int i = 1; i <= n; i++) { ll t = i; while (t) { if (t % 10 == 7) { s.insert(i); break; } t = t / 10; } t=i; while (t) { if (t % 8 == 7) { s.insert(i); break; } // t = t - ((t / 8) * 8); t = t / 8; } } // for (auto i : s) { // cout << i << " "; // } cout << n - s.size(); }
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define rep2(i, s, n) for(long long i=(s);i<(long long)(n);i++) #define repi(i, n) for(int i=0;i<(int)(n);i++) #define rep2i(i, s, n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define chmax(s, t) s=max(s,t); #define chmin(s, t) s=min(s,t); #define deg2rad(deg) (((deg)/360)*2*M_PI) #define rad2deg(rad) (((rad)/2/M_PI)*360) using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>; using vi = vector<int>; using vvi = vector<vi>; const ll INF = (1LL<<60); const int INFi = (1<<29); /*素数判定*/ bool is_prime(ll n){ if(n==1) return false; for(ll i=2;i*i<=n;i++){ if(n%i==0) return false; } return true; } /*約数列挙*/ vll enum_divisors(ll n){ vll l; for(ll i=1;i*i<=n;i++){ if(n%i==0){ l.push_back(i); if(n/i != i) l.push_back(n/i); } } sort(all(l)); return l; } /*素因数分解*/ vector<P> prime_factorize(ll n){ vector<P> l; for(ll i=2;i*i<=n;i++){ if(n%i!=0) continue; ll e = 0; while(n%i==0){ e++; n /= i; } l.push_back({i, e}); } if(n!=1) l.push_back({n, 1}); return l; } /*最小公倍数*/ ll lcm(ll a, ll b){ return a*b/__gcd(a,b); } /*最大公約数*/ ll gcd(ll a, ll b){ return __gcd(a,b); } /*組み合わせ(Combination)*/ const ll CMAX = 1010000; const ll CMOD = 1e9+7; ll fac[CMAX], finv[CMAX], inv[CMAX]; // テーブルを作る前処理 void combinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < CMAX; i++) { fac[i] = fac[i - 1] * i % CMOD; inv[i] = CMOD - inv[CMOD%i] * (CMOD / i) % CMOD; finv[i] = finv[i - 1] * inv[i] % CMOD; } } // 二項係数計算 ll comb(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % CMOD) % CMOD; } /*階乗*/ ll factorial(ll n){ const ll FMOD = 1e9+7; ll ret = n; for(ll i=n-1;i>0;i--){ ret*=i; ret%=FMOD; } return (ret?ret:1); } int main(){ ll n, s, d; cin >> n >> s >> d; string ans = "No"; rep(i, n){ ll x, y; cin >> x >> y; if(x<s && y>d){ ans = "Yes"; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool check(const int64_t x, vector<vector<int64_t>>& a) { set<int> s; for (auto i = 0; i < (int)a[0].size(); i++) { auto t = 0; for (auto j = 0; j < 5; j++) t |= ((a[j][i] >= x) << j); s.insert(t); } for (auto& i : s) for (auto& j : s) for (auto& k : s) if ((i | j | k) == (1 << 5) - 1) return true; return false; } int64_t bin_search(const int64_t ng, const int64_t ok, vector<vector<int64_t>>& a) { int64_t left = ng, right = ok; while (abs(right - left) > 1) { int64_t mid = (left + right) / 2; if (check(mid, a)) right = mid; else left = mid; } return right; }; int main() { int64_t n; cin >> n; vector<vector<int64_t>> a(5, vector<int64_t>(n)); for (auto i = 0; i < n; i++) for (auto j = 0; j < 5; j++) cin >> a[j][i]; cout << bin_search(1e9 + 1, 0, a) << endl; return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define DB double #define U unsigned #define P std::pair #define LL long long #define LD long double #define pb emplace_back #define MP std::make_pair #define SZ(x) ((int)x.size()) #define all(x) x.begin(),x.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define FOR(i,a,b) for(int i = a;i <= b;++i) #define ROF(i,a,b) for(int i = a;i >= b;--i) #define DEBUG(x) std::cerr << #x << '=' << x << std::endl const int MAXN = 5000+5; const int ha = 998244353; int n,m; inline void add(int &x,int y){ x += y-ha;x += x>>31&ha; } int pw[MAXN],pw1[MAXN]; int main(){ scanf("%d%d",&n,&m); if(m == 1){ puts("1"); return 0; } pw[0] = 1;FOR(i,1,MAXN-1) pw[i] = 1ll*pw[i-1]*m%ha; pw1[0] = 1;FOR(i,1,MAXN-1) pw1[i] = 1ll*pw1[i-1]*(m-1)%ha; int ans = 0; FOR(x,2,m){ int c = 1; FOR(k,2,n-1){ c = 1ll*c*(m-x)%ha; int d = (pw1[k-1]+ha-c)%ha; add(ans,1ll*d*(n-k)%ha*pw[n-(k+1)]%ha); } } FOR(i,1,n) add(ans,1ll*pw1[i-1]*pw[n-i+1]%ha); printf("%d\n",ans); return 0; }
// 解き直し. // https://atcoder.jp/contests/arc105/editorial/170 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using LL = long long; using P = pair<LL, LL>; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() LL cW[11], bL[101010], bV[101010]; struct bridge{ LL l, v; // 長さ, 耐荷重. bool operator < (const bridge& rhs) const{ return (v != rhs.v) ? (v > rhs.v) : (l < rhs.l); } }; int main(){ // 1. 入力情報. int N, M; scanf("%d %d", &N, &M); LL maxW = 0, minV = 202020202020202020; rep(i, N){ scanf("%lld", &cW[i]); maxW = max(maxW, cW[i]); } priority_queue<bridge> pq; rep(i, M){ LL l, v; scanf("%lld %lld", &l, &v); minV = min(minV, v); pq.push({l, v}); } // 2. 橋が崩落するパターンを除外. if(maxW > minV){ puts("-1"); return 0; } // 3. 橋の耐荷重に対する必要な最小間隔を保存. int idx = 0; while(!pq.empty()){ bridge b = pq.top(); pq.pop(); // printf("b.l=%lld b.v=%lld\n", b.l, b.v); bL[idx + 1] = max(bL[idx], b.l); bV[idx + 1] = b.v; idx++; } // rep(i, M + 1) printf("l=%lld v=%lld\n", bL[i], bV[i]); // 4. ラクダの隊列を全探索. vector<int> z(N); iota(all(z), 0); LL ans = -202020202020202020; do{ // 4-1. ラクダの隊列の重量(累積和). LL cWCum[N + 1]; memset(cWCum, 0LL, sizeof(cWCum)); rep(i, N) cWCum[i + 1] = cWCum[i] + cW[z[i]]; // rep(i, N) printf("%d ", z[i]); // puts(""); // rep(i, N + 1) printf("%lld ", cWCum[i]); // puts(""); // 4-2. 制約. LL x[N][N]; memset(x, 0LL, sizeof(x)); rep(i, N){ repx(j, i + 1, N){ int at = lower_bound(bV, bV + M + 1, cWCum[j + 1] - cWCum[i]) - bV - 1; x[i][j] = max(x[i][j], bL[at]); x[j][i] = x[i][j]; } } // rep(i, N){ // repx(j, i + 1, N) printf("%lld ", x[i][j]); // puts(""); // } // 4-3. グラフの最長路の長さは? // ex. // AtCoder Beginner Contest 022 (C - Blue Bird) を ベース に 実装. // https://atcoder.jp/contests/abc022/tasks/abc022_c // -> 具体的には, Warshall–Floyd法で, // repx(k, 1, N) repx(i, 1, N) repx(j, 1, N) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); // とあるものを, i = 0 に 固定するイメージで, 更新する形に変更. LL g[N][N], way = 0; memset(g, 0LL, sizeof(g)); repx(i, 1, N){ rep(j, i + 1){ g[0][i] = min(g[0][i], g[0][j] - x[i][j]); way = min(way, g[0][i]); // printf("i=%d j=%d %lld \n", i, j, x[i][j]); } } // 4-4. 最長路の長さについて, 最小値を更新. ans = max(ans, way); }while(next_permutation(all(z))); // 5. 出力. printf("%lld\n", -ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 500010, mod = 998244353; int a[3]; int main() { string s1, s2; int d1 = 0, d2 = 0; int n; cin >> n; while (n --) { string s; int h; cin >> s >> h; if(h >= d1) d2 = d1, s2 = s1, d1 = h, s1 = s; else if (h > d2) d2 = h, s2 = s; } cout << s2 << endl; return 0; }
#define LOCAL0 #include<bits/stdc++.h> #define INF 0x3f3f3f3f #define debug(msg) cout << (msg) << endl #define FOR(i, a, b) for (ll i=a; i<=(b); i++) #define ROF(i, a, b) for (ll i=a; i>=(b); i--) #define pb(x) push_back(x) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define sz(x) (ll)x.size() #define JUDGE(x) if(x) cout << "Yes\n"; else cout << "No\n"; using namespace std; typedef long long ll; const ll mod = 1e9+7; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int T; cin >> T; while(T--){ ll l, r; cin >> l >> r; if(r-l<l) cout << 0 << endl; else cout << (r-l+1)*(r-2*l+2)-(r-2*l+2)*(r+1)/2 << endl;; } return 0; }
//vec[i]の範囲外エラー表示 #define _GLIBCXX_DEBUG //includeとusing #include <bits/stdc++.h> using namespace std; //型名省略 template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using P = pair<T,T>; template <class T> using M = map<T,T>; template <class T> using S = set<T>; template <class T> using PQ = priority_queue<T>; template <class T> using PQG = priority_queue<T,V<T>,greater<T>>; using ll = long long; using st = string; using vl = V<ll>; using vvl = V<V<ll>>; using vc = V<char>; using vvc = V<V<char>>; using vs = V<st>; using vvs = V<V<st>>; using vb = V<bool>; using vvb = V<V<bool>>; //定数 const ll INF = 1e9; const ll MOD = 1e9+7; const vl dx = {1,0,-1,0,1,0,-1,0}; const vl dy = {0,1,0,-1,0,1,0,-1}; //マクロ #define re(n) for(ll _ = 0; _ < (ll) n; _++) #define rep(i,n) for(ll i = 0; i < (ll) n; i++) #define rrep(i,n) for(ll i = (ll) n - 1; i >= 0;i--) #define repp(i,x,n) for(ll i = (ll) x; i < (ll) n; i++) #define rrepp(i,x,n) for(ll i = (ll) n - 1; i >= x; i--) #define bitrep(i,n) for(ll i = 0; i < (1<<(ll)n); i++) #define each(x,A) for(auto &(x): (A)) #define ALL(A) A.begin(), A.end() #define SIZE(A) ll(A.size()) #define pb(a) push_back(a) #define mp make_pair #define PRECISE_COUT cout << setprecision(15) << fixed //入力 void CINT(){} template <class Head,class... Tail> void CINT(Head&& head,Tail&&... tail){ cin>>head; CINT(move(tail)...);} #define CIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__) #define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__) #define DCIN(...) double __VA_ARGS__;CINT(__VA_ARGS__) #define VCIN(A) for(auto &a : (A)) cin >> a //出力 #define COUT(a) cout << a << endl #define TEST(i,a) cout << (ll) i << " : " << (ll) a << endl #define vout(A) for(auto &a : (A)) cout << a << " ";cout << endl //正誤判定 void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;} void YES(bool ans){cout << (ans? "YES" : "NO") << endl;} //余り切り上げ ll ceil(ll a,ll b) {return (a + b - 1) / b;} //最大値・最小値 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;} //最大公約数 ll gcd(ll a,ll b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);} //最小公倍数 ll lcm(ll a,ll b){return a/gcd(a,b)*b;} //n乗の余り ll modpow(ll x,ll n,ll mod){ll res=1;for(ll i=0;i<n;i++){res=res*x%mod;}return res;} ll MODpow(ll x,ll n){ll res=1;for(ll i=0;i<n;i++){res=res*x%MOD;}return res;} //グリッドグラフの範囲外エラー bool range(ll ny, ll nx, ll H, ll W){if(ny < 0 || ny >= H || nx < 0 || nx >= W) return false; return true;} /*----------------------------------------------------------------------*/ //main関数 int main() { CIN(s,p); rep(i,sqrt(p)+1){ if(i * (s-i) == p){ COUT("Yes"); return 0; } } COUT("No"); }
#include <iostream> #include <string> #include <vector> #include <deque> #include <algorithm> #include <math.h> #include <iomanip> #define INF 100000000 #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using p = pair<int, int>; int main() { ll s,p; cin >> s>>p; for(ll i=1;i<1000007;i++){ if(p%i==0){ if((p/i)==(s-i)){ cout << "Yes" << endl; return 0; } } } cout << "No" << endl; return 0; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define N 300100 #define MOD 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 mp[5010]; int main() { ll dp[10000]; ll dp2[10000]; vector<pair<pii, int> >pp; int x, y; int n, m, k; ll z, s; ll lq[N]; ll rq; rep(i, N)lq[i] = 1; char c[3]; cin >> n >> m >> k; rep(i, k) { rep(j, 3)c[j] = 0; cin >> x >> y; cin >> c; if (c[0] == 'R')pp.pb({ {x - 1,y - 1},1 }); if (c[0] == 'D')pp.pb({ {x - 1,y - 1},2 }); if (c[0] == 'X')pp.pb({ {x - 1,y - 1},3 }); } int idx = 0; sort(all(pp), greater<pair<pii, int> >()); z = 1; rep(j, m)mp[j] = 0; while (idx < k) { if (pp[idx].first.first < n - 1)break; else mp[pp[idx].first.second] = pp[idx].second; idx++; } rep3(j, m - 1, 0) { if (j == m - 1) { if (mp[j] == 0) { z = (z * 3) % MOD; lq[j] = (lq[j] * 3) % MOD; } } else { if (mp[j] == 2)z = 0; if (mp[j] == 0) { z = (z * 2) % MOD; lq[j] = (lq[j] * 3) % MOD; } } dp[j] = z; } rep(j, m)dp2[j] = dp[j]; rep3(i, n - 2, 0) { rq = 1; rep(j, m)mp[j] = 0; while (idx < k) { if (pp[idx].first.first < i)break; else mp[pp[idx].first.second] = pp[idx].second; idx++; } rep3(j, m - 1, 0) { if (j == m - 1) { if (mp[j] == 0) { dp[j] = (dp2[j] * 2) % MOD; rq = (rq * 3) % MOD; lq[j] = (lq[j] * 3) % MOD; } if (mp[j] == 1)dp[j] = 0; if (mp[j] == 2)dp[j] = dp2[j]; if (mp[j] == 3)dp[j] = dp2[j]; } else { if (mp[j] == 0) { dp[j] = (lq[j] * dp[j + 1]) % MOD;; z = (rq*dp2[j]) % MOD; dp[j] = ((dp[j] + z) * 2) % MOD; rq = (rq * 3) % MOD; lq[j] = (lq[j] * 3) % MOD; } if (mp[j] == 1)dp[j] = (lq[j] * dp[j + 1]) % MOD; if (mp[j] == 2)dp[j] = (rq*dp2[j]) % MOD; if (mp[j] == 3) { dp[j] = (lq[j] * dp[j + 1]) % MOD;; z = (rq*dp2[j]) % MOD; dp[j] = (dp[j] + z) % MOD; } } } rep(j, m) dp2[j] = dp[j]; } cout << dp2[0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} char a[5005][5005]; mint dp[5005][5005]; int main(){ int h, w, k; cin >> h >> w >> k; for(int i = 0; i < k; i++){ int A, B; char ch; cin >> A >> B >> ch; A--; B--; a[A][B] = ch; } dp[0][0] = 1; mint t = mint(3).inv(); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(a[i][j]=='R') dp[i][j+1] += dp[i][j]; if(a[i][j]=='D') dp[i+1][j] += dp[i][j]; if(a[i][j]=='X'){ dp[i][j+1] += dp[i][j]; dp[i+1][j] += dp[i][j]; } if(a[i][j]=='\0'){ dp[i][j+1] += dp[i][j]*mint(2)*t; dp[i+1][j] += dp[i][j]*mint(2)*t; } } } mint ans = dp[h-1][w-1]; ans *= mint(3).pow(h*w-k); cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int a1,a2,a3; cin>>a1>>a2>>a3; int t; if(a1>a2) { t=a1; a1=a2; a2=t; } if(a2>a3) { t=a2; a2=a3; a3=t; } if(a1>a2) { t=a1; a1=a2; a2=t; } if(a3-a2==a2-a1) cout<<"Yes"; else cout<<"No"; }
#include<iostream> #include<string> using namespace std; main() { int n,x,a=0,b=0; string s; cin>>n>>x; cin>>s; for(int i=0; i<n; i++) { if(s[i]=='o') { x++; } else if(x>0) { x--; } } cout<<x<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1009; struct Edges { Edges(){} Edges(int x, char c):x(x), c(c){} int x; char c; }; struct Vertices { Vertices(){} Vertices(int x, int y, int d):x(x), y(y), d(d){} int x, y, d; bool operator < (const Vertices&a) const { return d > a.d; } }; int n, m, d[N][N]; vector<Edges> g[N]; priority_queue<Vertices> q; template<typename T> T read() { T num = 0; char c; bool flag = 0; while((c=getchar())==' ' || c=='\n' || c=='\r'); if(c=='-') flag = 1; else num = c - '0'; while(isdigit(c=getchar())) num = num * 10 + c - '0'; return flag ? -num : num; } int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); freopen("output.out", "w", stdout); #endif n = read<int>(); m = read<int>(); for(int i = 1; i <= m; i++) { int x = read<int>(), y = read<int>(); char c; scanf("%c", &c); g[x].push_back(Edges(y, c)); g[y].push_back(Edges(x, c)); } memset(d, 0x3f, sizeof(d)); q.push(Vertices(1, n, 0)); d[1][n] = d[n][1] = 0; int ans = 2147483647; while(!q.empty()) { Vertices now = q.top(); q.pop(); for(int i = 0; i < g[now.x].size(); i++) for(int j = 0; j < g[now.y].size(); j++) { int x = g[now.x][i].x; int y = g[now.y][j].x; if(g[now.x][i].c!=g[now.y][j].c) continue; if((x==now.y) || (y==now.x)) ans = min(ans, now.d+1); if(x==y) ans = min(ans, now.d+2); if(now.d+2>=d[x][y]) continue; d[x][y] = now.d + 2; d[y][x] = now.d + 2; q.push(Vertices(x, y, now.d+2)); } } if(ans<2147483647) printf("%d\n", ans); else puts("-1"); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using pii = pair<int, int>; int n, m; vector<ll> a, b; vector<vector<int>> g; vector<bool> check; ll dfs(int v) { check[v] = true; ll ret = b[v] - a[v]; for (auto nv : g[v]) { if (check[nv]) continue; ret += dfs(nv); } return ret; } int main() { cin >> n >> m; a.resize(n); b.resize(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; g.resize(n); rep(i, m) { int c, d; cin >> c >> d; c--, d--; g[c].push_back(d); g[d].push_back(c); } check.resize(n); bool ok = true; for (int i = 0; i < n; i++) { if (check[i]) continue; if (dfs(i) != 0) ok = false; } if (ok) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pi acos(-1.0) const int inf = 1e9; signed main() { int h, w, x, y; cin >> h >> w >> x >> y; string a[h + 1]; for (int i = 1; i <= h; i++) { cin >> a[i]; a[i] = '*' + a[i]; } if (a[x][y] == '#') { cout << 0; return 0; } int ans = 0; { int i = x, j = y; int l = 0; while (j - l - 1 >= 1 && a[i][j - l - 1] == '.') l++; ans += l; } { int i = x, j = y; int l = 0; while (i - l - 1 >= 1 && a[i - l - 1][j] == '.') l++; ans += l; } { int i = x, j = y; int l = 0; while (j + l + 1 <= w && a[i][j + l + 1] == '.') l++; ans += l; } { int i = x, j = y; int l = 0; while (i + l + 1 <= h && a[i + l + 1][j] == '.') l++; ans += l; } cout << ans + 1; }
#include <bits/stdc++.h> #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(),(x).end() #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second using namespace std; typedef double DB; typedef long double LD; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VPII; //head const int N=1e5+5; int n,m,k; bool st[N]; pair<DB,DB> f[N],dp[N<<1]; int main() { //freopen("E:/OneDrive/IO/in.txt","r",stdin); ios::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>m>>k; for(int i=1;i<=k;i++) { int x; cin>>x; st[x]=true; } for(int i=n;~i;i--) { f[i].FI=f[i+1].FI-dp[i+1+m].FI+dp[i+1].FI; f[i].SE=f[i+1].SE-dp[i+1+m].SE+dp[i+1].SE; if(st[i]) dp[i]=MP(1,0); else { if(i<n) dp[i].FI=f[i].FI/m,dp[i].SE=f[i].SE/m+1; } } if(fabs(1-dp[0].FI)<1e-6) cout<<-1<<'\n'; else cout<<fixed<<setprecision(4)<<dp[0].SE/(1-dp[0].FI)<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> pii; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vpii; #define endl "\n" #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #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 fi first #define se second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLR(v) v.rbegin(), v.rend() #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 TC(t) while (t--) void OPEN(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } bool isfirst(string s) { return (s[0] == '!'); } string clear(string s) { if (s[0] == '!') s.erase(s.begin()); return s; } string ans(vector <string> a) { set <string> s; for (string t : a) s.insert(t); for (auto it=s.begin();it!=s.end();it++) { string k = *it; if (isfirst(k)) { string e = clear(k); if (s.count(e) == 1) return e; } } return "satisfiable"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector <string> a; FOR(i,0,n) { string temp; cin >> temp; a.pb(temp); } cout << ans(a); }
/*** In the name of Allah(swt), the most gracious, most merciful.***/ /*** Alhamdulillah for Everything ***/ #include <bits/stdc++.h> #include<vector> using namespace std; typedef bool boo; typedef int li; typedef long il; typedef unsigned long ul; typedef long long int ll; typedef unsigned long long ull; typedef double dd; typedef string str; #define Test ll t; std :: cin >> t; while(t--) #define input(a , n) for( int i = 0 ; i < n ; i++ ) std :: cin >> a[i]; #define lp(a , i , b) for( ll i = a ; i < b ; i++ ) #define rlp(a , i , b) for( ll i = a; i >= b ; i-- ) #define sz(x) x.size() #define len(z) z.begin() , z.end() #define ci(x) std :: cin >> x; #define co(x) std :: cout << x nl; #define fix(x) fixed << setprecision(x) #define mem(z , l) memset( z , l , sizeof(z) ) #define MP make_pair #define pb push_back #define F first #define S second #define nl << endl; #define nll std :: cout << endl; #define cy std :: cout << "Yes" << endl; #define cn std :: cout << "No" << endl; #define rn return; #define Good_Bye return 0; #define gcd(a , b) __gcd( a , b ) #define lcm(a , b) ( a * ( b / gcd( a , b ) ) ) #define Faster ios_base :: sync_with_stdio( 0 ); cin.tie( 0 ); cout.tie( 0 ); const double eps = 1e-9; const int inf = 2000000000; const ll infLL = 9000000000000000000; const ll MOD = 1e+7; const double PI = 3.141592653589793238462643383279; //int dx[]={-1,1,0},dy[]={-1,1,0} ///_____________________________________________ L E T ' S B E G I N ____________________________________________________________ int r,c,ans(0); string s[11]; int main() { /*#ifndef ONLINE_JUDGE freopen( "input.txt" , "r" , stdin ); freopen( "output.txt" , "w" , stdout ); #endif */ Faster cin>>r>>c; lp(0,i,r) cin>>s[i]; lp(0,i,r-1) { lp(0,j,c-1) { int f(0); lp(0,a,2) { lp(0,b,2) { if(s[i+a][j+b]=='#') ++f; } } if(f%2) ++ans; } } cout<<ans<<endl; Good_Bye } // author : Md. Nahid Chowdhury(IIUC_CSE48th Batch) ///||||||||||||||||||||||||||||||||||||||||||||||| E N D ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int N; int A[100]; int main(){ int k; int res; int opt = -1; int tmp; rd(N); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } for(k=(2);k<(1001);k++){ int i; tmp = 0; for(i=(0);i<(N);i++){ if(A[i]%k==0){ tmp++; } } if(opt < tmp){ opt = tmp; res = k; } } wt_L(res); wt_L('\n'); return 0; } // cLay varsion 20201102-1 // --- original code --- // int N, A[100]; // { // int res, opt = -1, tmp; // rd(N,A(N)); // rep(k,2,1001){ // tmp = 0; // rep(i,N) if(A[i]%k==0) tmp++; // if(opt < tmp) opt = tmp, res = k; // } // wt(res); // }
#include<iostream> #include <bits/stdc++.h> #include<algorithm> #include <cstring> #include <string.h> #define endl "\n" #define SRK ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define EPS 1e-6 #define MAX 100000 typedef long long ll; const ll MOD = 1e9; using namespace std; int freq[1005]; bool comparison(pair<ll, ll> a,pair<ll, ll> b) { if (a.first == b.first) { return a.second > b.second; } else { return a.first < b.first; } } void divisors(int a) { for (int i = 1; i * i <= a; i++) { if (a % i == 0) { freq[i]++; int j = a / i; if (i != j) { freq[j]++; } } } } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; divisors(x); } freq[1] = 0; int high = *max_element(freq,freq+1005); for (int i = 0; i < 1005; i++) { if (freq[i] == high) { cout << i << endl; break; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define Pint pair<int,int> #define Pll pair<ll,ll> #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 #define eb emplace_back template<class T>void chmax(T &a,T b){if(a<b)a=b;} template<class T>void chmin(T &a,T b){if(a>b)a=b;} constexpr int INF=1000000000; constexpr ll llINF=1000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-8; 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); } 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; } 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; } ll modpow(ll a,ll b){ if(b==0)return 1; if(b%2)return modpow(a,b-1)*a%mod; ll memo=modpow(a,b/2); return memo*memo%mod; } vector<ll>kaijo,invkaijo; void init_fact(int n){ kaijo.resize(n+1); invkaijo.resize(n+1); kaijo[0]=1; for(ll i=1;i<=n;i++){ kaijo[i]=kaijo[i-1]*i; kaijo[i]%=mod; } rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2); } 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; } } } ll comb(int a,int b){ if(a<b)return 0; if(a<0||b<0)return 0; return kaijo[a]*invkaijo[a-b]%mod*invkaijo[b]%mod; } ll inv(ll x){ x=modpow(x,mod-2); return x; } int dp[100010]; void solve(){ int n; cin>>n; vector<ll>a(n),b(n),c,d; rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; ll ans=0; for(int i=0;i<n;i+=2){ ans+=max(a[i]+a[i+1],b[i+1]+b[i]); if(a[i]+a[i+1]>=b[i]+b[i+1]){ c.pb(b[i]-a[i]); d.pb(b[i+1]-a[i+1]); } else{ c.pb(a[i+1]-b[i+1]); d.pb(a[i]-b[i]); } } sort(all(d)); sort(all(c)); rep(i,c.size()){ if(c[i]+d[i]>=0)ans+=c[i]+d[i]; } cout<<ans<<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; #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = a; i >= b; i--) const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; typedef long long LL; typedef pair <int, int> P; const int N = 2e5 + 5; int n, a[N], b[N], c[N]; LL ans, res; int main() { cin>>n; rep (i, 1, n) scanf("%d", &a[i]), ans += a[i]; rep (i, 1, n) scanf("%d", &b[i]), b[i] -= a[i]; rep (i, 1, n) { if (i&1) b[(i + 1)>>1] = b[i]; else c[i>>1] = b[i]; } sort(b + 1, b + n/2 + 1); sort(c + 1, c + n/2 + 1); res = ans; per (i, n/2, 1) res += b[i] + c[i], ans = max(ans, res); printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define N 200005 #define INF 1000000005 #define INFLL 2e18 #define PI 3.1415926535897 typedef long long ll; #define ALL(v) (v).begin(),(v).end() #define SZ(x) int(x.size()) #define IN(a) cin>>(a) #define OUT(a) cout<<(a)<<endl typedef pair<int,int> P; const int MAX = (1<<20); const int MOD = 1000000007; int par[MAX]; int depth[MAX]; int siz[MAX]; bool check[MAX]; //初期化 void init(){ for(int i=0;i<MAX;i++){ par[i]=i; siz[i]=1; check[i]=true; } } //根を探索  int root(int x){ if(par[x]==x){ return x; }else{ return par[x]=root(par[x]); } } //統合 void unit(int x,int y){ x=root(x); y=root(y); if(x==y){ check[x]=false; return; } if(depth[x]>depth[y])swap(x,y); par[x]=y; siz[y]+=siz[x]; check[y]&=check[x]; if(depth[x]==depth[y])depth[x]++; } // 同じ集合か判定 bool issame(int x,int y){ return root(x)==root(y); } //集合の大きさ int size(int x){ return siz[root(x)]; } void solve(){ int n; cin>>n; map<int,int>mp,cnt; init(); vector<int>a(n),b(n); REP(i,n){ cin>>a[i]>>b[i]; a[i]--;b[i]--; unit(a[i],b[i]); } REP(i,n){ if(0==cnt[a[i]]++)mp[root(a[i])]++; if(0==cnt[b[i]]++)mp[root(b[i])]++; } int ans=0; for(auto p:mp){ ans+=p.second; if(check[p.first])ans--; } OUT(ans); } int main(){ solve(); return 0; }
#include "bits/stdc++.h" using namespace std; typedef int ll; typedef pair<ll ,ll> pll; #define F first #define S second #define pb push_back #define mp make_pair #define all(X) (X).begin(), (X).end() #define REP(i,x,y) for(ll i = x;i <= y;++i) #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) vector<string> vec_splitter(string s) { for(char& c: s) c = c == ','? ' ': c; stringstream ss; ss << s; vector<string> res; for(string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> args, int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if(idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) void localTest() { #ifndef ONLINE_JUDGE freopen("inp", "r", stdin); freopen("out", "w", stdout); #endif } const ll N = 2e5 + 5; ll par[N], sz[N]; unordered_map<ll, ll> has[N]; ll find(ll x) { if(x == par[x]) { return x; } return par[x] = find(par[x]); } void merge(ll x, ll y) { ll rx = find(x), ry = find(y); if(rx == ry) { return; } if(sz[rx] < sz[ry]) { swap(rx, ry); } par[ry] = rx; sz[rx] += sz[ry]; for(auto u : has[ry]) { has[rx][u.F] += u.S; } return; } int main() { localTest(); fastio; ll n, q; cin >> n >> q; for(ll i = 1; i <= n; ++i) { ll x; par[i] = i; cin >> x; sz[x] = 1; has[i][x]++; } while(q--) { ll ty, x, y; cin >> ty >> x >> y; if(ty == 1) { merge(x, y); } else { ll rx = find(x); cout << has[rx][y] << "\n"; } } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;} template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;} int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0}; long double eps = 1e-9; long double pi = acos(-1); template<typename T> struct BIT{ vector<T> bit; BIT(int n) : bit(n+1) {} void add(int a,T w){ for(int x=++a; x < bit.size(); x+=(x&(-x)) ){ bit[x] += w; } } // return v[0]+v[1]+...+v[a-1]; T sum(int a){ T ret=0; for(int x=a;x>0;x-=(x&(-x)) ){ ret+=bit[x]; } return ret; } }; signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int n,m,q; cin>>n>>m>>q; vector<array<ll,3>> v(q); map<int,int> mp; for(int i=0;i<q;i++){ int a,b,c; cin>>a>>b>>c; b--; mp[c]=0; v[i]={a,b,c}; } mp[0]=0; int now = 0; for(auto &i:mp){ i.second = now; now++; } ll ans = 0; int k = now; BIT<ll> c1(k),c2(k),s1(k),s2(k); int a[n]={},b[m]={}; c1.add(0,n); c2.add(0,m); for(int i=0;i<q;i++){ auto [t,x,y] = v[i]; int pos = mp[y]; if(t == 1){ int pre = mp[a[x]]; ans -= a[x] * c2.sum(pre); ans -= s2.sum(k) - s2.sum(pre); c1.add(pre,-1); c1.add(pos,1); s1.add(pre,-a[x]); s1.add(pos,y); a[x]=y; ans += y * c2.sum(pos); ans += s2.sum(k) - s2.sum(pos); } else{ int pre = mp[b[x]]; ans -= b[x] * c1.sum(pre); ans -= s1.sum(k) - s1.sum(pre); c2.add(pre,-1); c2.add(pos,1); s2.add(pre,-b[x]); s2.add(pos,y); b[x]=y; ans += y * c1.sum(pos); ans += s1.sum(k) - s1.sum(pos); } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; //using mint = modint1000000007; #define int long long #define rep(i, n) for(int i = 0, i##_len=(n); i < i##_len; ++i) #define per(i, n) for(int i = (n)-1; i >= 0 ; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define len(x) ((int)(x).size()) void _cin() {} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail) { cin >> head; _cin(forward<Tail>(tail)...); } #define cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__) #define cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i] #define cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i] #define cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i] #define cinv4(Type, xs, ys, zs, zzs, n) vector<Type> xs(n), ys(n), zs(n), zzs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i] >> zzs[i] #define cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<Type>(w)); rep(i, h) rep(j, w) cin >> xs[i][j] void print() { cout << endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); } template <class Type> void print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; } template <class Type> void print(vector<vector<Type>> &df) { for (auto& vec : df) { print(vec); } } void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } void yesno(bool b) { cout << (b ? "yes" : "no") << endl; } template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a < b) { a = b; return 1; } return 0; } template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b < a) { a = b; return 1; } return 0; } using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; const int inf = 1ll << 62; const int mod = 1000000007; void Main() { cin(string, s); reverse(all(s)); rep(i, 10) { string t = s; reverse(all(t)); if (t == s) { print("Yes"); return; } s += "0"; } print("No"); } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(16); //int t; cin >> t; while (t--) Main(); }
#include <bits/stdc++.h> #define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0); //#define ExecuteTime cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n" #define pi acos(-1) #define pb push_back #define F first #define S second #define forr(i,a,b) for(ll i = (ll)a, asdf = (ll)b ; i < asdf; ++i) #define fore(i,a,b) for(ll i = (ll)a, asdf = (ll)b ; i >= asdf; --i) #define fori(i,a,b,c) for(ll i = (ll)a, asdf = (ll)b, INC = (ll) c; i < asdf; i+=INC) #define sz(x) (ll)x.size() #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define b_p(n) __builtin_popcountll(n) using namespace std; typedef long long ll; typedef pair<ll,ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; typedef vector<vll> vvll; int main(){ optimizar_io //freopen("prueba_input.txt","r",stdin); //freopen("prueba_output.txt","w",stdout); ll n; cin>>n; vpll idx(n), idy(n); vll dist; forr(i,0,n){ cin>>idx[i].F>>idy[i].F; idx[i].S = i; idy[i].S = i; } sort(all(idx)); sort(all(idy)); ll index_x = idx[n-1].S; ll index_y = idy[n-1].S; ll index_x2 = idx[n-2].S; ll index_y2 = idy[n-2].S; vpll dx, dy, dx2, dy2; forr(i,0,n-1){ dx.pb({ idx[i].S, abs( idx[i].F-idx[n-1].F ) }); dy.pb({ idy[i].S, abs( idy[i].F-idy[n-1].F ) }); } forr(i,0,n-2){ dx2.pb({ idx[i].S, abs( idx[i].F-idx[n-2].F ) }); dy2.pb({ idy[i].S, abs( idy[i].F-idy[n-2].F ) }); } sort(all(dx)); sort(all(dy)); sort(all(dx2)); sort(all(dy2)); forr(i,0,sz(dx)){ if(dx[i].F==dy[i].F && index_x==index_y) dist.pb(max(dx[i].S, dy[i].S)); else{ dist.pb(dx[i].S); dist.pb(dy[i].S); } } forr(i,0,sz(dx2)){ if(dx2[i].F==dy2[i].F && index_x2==index_y2) dist.pb(max(dx2[i].S, dy2[i].S)); else{ dist.pb(dx2[i].S); dist.pb(dy2[i].S); } } sort(rall(dist)); cout<<dist[1]<<"\n"; //ExecuteTime; return 0; } // 2^n - 1LL = 1,2,4,...,2^(n-1). //See the real limit per case and the total limit // (n-1)% m + 1LL; - formula n%m==x and special case n%m==0; // set<char>st(s.begin(),s.end()) // or vector<int> v(all(w.begin(),w.end())) // put suffix LL or ULL for constants integers, 1.0f float, 1.0 double, 1.0L long double // read the question correctly. What are the exact constraints?. 'y' is considered a vowel in special cases. // n&(n-1)==0 if this is true n is a power of 2. // look out for SPECIAL CASES (n=1 or n=0 or n=limit ?) and overflow (ll vs int) ARRAY OUT OF BOUNDS
#include<bits/stdc++.h> using namespace std; typedef long long ll;typedef double db; typedef pair<int, int> pii;typedef pair<ll, ll> pll; typedef pair<int,ll> pil;typedef pair<ll,int> pli; #define Fi first #define Se second #define _Out(a) cerr<<#a<<" = "<<(a)<<endl const int INF = 0x3f3f3f3f, MAXN = 1e6 + 50; const ll LINF = 0x3f3f3f3f3f3f3f3f, MOD = 998244353; const db Pi = acos(-1), EPS = 1e-6; void test(){cerr << "\n";}template<typename T,typename...Args>void test(T x,Args...args){cerr<<x<<" ";test(args...);} inline ll qpow(ll a, ll b){return b?((b&1)?a*qpow(a*a%MOD,b>>1)%MOD:qpow(a*a%MOD,b>>1))%MOD:1;} inline ll qpow(ll a, ll b,ll c){return b?((b&1)?a*qpow(a*a%c,b>>1,c)%c:qpow(a*a%c,b>>1,c)) %c:1;} inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} inline ll cede(ll a,ll b){if(b<0)return cede(-a,-b);if(a<0)return a/b;return (a+b-1)/b;} inline ll flde(ll a,ll b){if(b<0)return flde(-a,-b);if(a<0)return (a-b+1)/b;return a/b;} inline int sign(db x){return x<-EPS ? -1:x>EPS;} inline int dbcmp(db l,db r){return sign(l - r);} namespace Fast_IO{ //orz laofu const int MAXL((1 << 18) + 1);int iof, iotp; char ioif[MAXL], *ioiS, *ioiT, ioof[MAXL],*iooS=ioof,*iooT=ioof+MAXL-1,ioc,iost[55]; char Getchar(){ if (ioiS == ioiT){ ioiS=ioif;ioiT=ioiS+fread(ioif,1,MAXL,stdin);return (ioiS == ioiT ? EOF : *ioiS++); }else return (*ioiS++); } void Write(){fwrite(ioof,1,iooS-ioof,stdout);iooS=ioof;} void Putchar(char x){*iooS++ = x;if (iooS == iooT)Write();} inline int read(){ int x=0;for(iof=1,ioc=Getchar();(ioc<'0'||ioc>'9')&&ioc!=EOF;)iof=ioc=='-'?-1:1,ioc=Getchar(); if(ioc==EOF)Write(),exit(0); for(x=0;ioc<='9'&&ioc>='0';ioc=Getchar())x=(x<<3)+(x<<1)+(ioc^48);return x*iof; } inline long long read_ll(){ long long x=0;for(iof=1,ioc=Getchar();(ioc<'0'||ioc>'9')&&ioc!=EOF;)iof=ioc=='-'?-1:1,ioc=Getchar(); if(ioc==EOF)Write(),exit(0); for(x=0;ioc<='9'&&ioc>='0';ioc=Getchar())x=(x<<3)+(x<<1)+(ioc^48);return x*iof; } void Getstr(char *s, int &l){ for(ioc=Getchar();ioc==' '||ioc=='\n'||ioc=='\t';)ioc=Getchar(); if(ioc==EOF)Write(),exit(0); for(l=0;!(ioc==' '||ioc=='\n'||ioc=='\t'||ioc==EOF);ioc=Getchar())s[l++]=ioc;s[l] = 0; } template <class Int>void Print(Int x, char ch = '\0'){ if(!x)Putchar('0');if(x<0)Putchar('-'),x=-x;while(x)iost[++iotp]=x%10+'0',x/=10; while(iotp)Putchar(iost[iotp--]);if (ch)Putchar(ch); } void Putstr(const char *s){for(int i=0,n=strlen(s);i<n;++i)Putchar(s[i]);} } // namespace Fast_IO using namespace Fast_IO; ll fac[MAXN],invfac[MAXN]; void init() { fac[0]=1;for(int i=1;i<MAXN;i++)fac[i]=fac[i-1]*i%MOD; invfac[MAXN-1]=qpow(fac[MAXN-1],MOD-2); for(int i=MAXN-2;i>=0;i--)invfac[i]=invfac[i+1]*(i+1)%MOD; } inline ll C(int n,int m) { if(m<0||m>n)return 0; return fac[n]*invfac[m]%MOD*invfac[n-m]%MOD; } ll tot[333]; void work() { int n=read(),k=read(); for(int i=1;i<=n;i++) { ll x=read(); ll now=1; for(int j=0;j<=k;j++) { tot[j]=(tot[j]+now)%MOD; now=now*x%MOD; } } ll mi2=1; for(int i=1;i<=k;i++) { mi2=mi2*2%MOD; ll ans=0; for(int j=0;j<=i;j++) { ans=(ans+tot[j]*tot[i-j]%MOD*C(i,j)%MOD)%MOD; } ans=(ans-tot[i]*mi2%MOD+MOD)*qpow(2,MOD-2)%MOD; printf("%lld\n",ans); } } int main() { init(); //std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //int T=read();for(int cas=1;cas<=T;cas++) //int T;scanf("%d",&T);for(int i=1;i<=T;i++) work(); // Write(); } /** 2 5 3 6 3 1 2 4 5 */
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; cout<<7-a+7-b+7-c; return 0; }
#include <bits/stdc++.h> #define ll long long #define vll vector<ll> #define vpp vector<pair<pair<ll, ll>, ll>> #define vp vector<pair<ll, ll>> #define mk make_pair #define pb push_back #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define N 1000000007 using namespace std; void fun() { // code begins ll a,b,c;cin>>a>>b>>c; ll k = 0; if( 2*a == b+c) k = 1; if( 2*b == a+c) k = 1; if( 2*c == a+b) k = 1; if(k) cout<<"Yes"; else cout<<"No"; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS; ll t; t = 1; //cin >> t; for (ll i = 1; i <= t; i++) fun(); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pi; char s[20]; ll g1(ll x){ vector<int> frec(10); int i = 0; while(x){ frec[x%10]++; //s[i++]=x%10 + '0'; x/=10; } for(int i = 9; i >= 0; i--) for(int j = 0; j < frec[i]; j++) x=x*10+i; return x; } ll g2(ll x){ vector<int> frec(10); int i = 0; while(x){ frec[x%10]++; x/=10; } for(int i = 1; i < 10; i++) for(int j = 0; j < frec[i]; j++) x=x*10+i; return x; } ll fun(ll x){ return g1(x)-g2(x); } int main(){ int n,k; scanf("%d %d",&n,&k); ll ans = n; for(int i = 1; i <= k; i++){ ans = fun(ans); } printf("%lld\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; int mod = 1e9+7; int add(int a, int b){ int ans = a + b; while(ans >= mod){ ans -= mod; } return ans; } void add_self(int & a, int b){ a = add(a,b); } int sub(int a, int b){ int ans = a - b; while(ans < 0){ ans += mod; } return ans; } void sub_self(int & a, int b){ a = sub(a,b); } int mul(int a, int b){ int ans = ((int64_t)a * b) % mod; return ans; } int binexpo(int b, int e){ int ans = 1; while(e){ if(e & 1){ ans = mul(ans,b); } b = mul(b,b); e >>= 1; } return ans; } int64_t dp[74][1<<21]; int main(){ int64_t a,b; cin >> a >> b; int64_t zero = a; int64_t len = b - a + 1; int64_t ans = 0; vector<int>masks(len); vector<int>primes; vector<bool>is_prime(73,true); for(int i = 2; i < 73; ++i){ if(is_prime[i]){ primes.push_back(i); for(int j = i * i; j < 73; j+= i){ is_prime[j] = false; } } } for(int j = 0; j < len; ++j){ for(int k = 0; k < primes.size(); ++k){ if((j + zero)%primes[k] == 0){ masks[j] |= (1 << k); } } } dp[0][0] = 1; for(int i = 1; i <= len; ++i){ for(int k = 0; k < (1<<20); ++k){ dp[i][k] = dp[i-1][k]; } for(int k = 0; k < (1<<20); ++k){ if((masks[i-1] & k) == masks[i-1]){ dp[i][k] += dp[i][k ^ masks[i-1]]; } } } for(int i = 0; i < (1<<20); ++i){ ans += dp[len][i]; } cout << ans << "\n"; return 0; } //12341234
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define rsort(a) {sort(all(a));reverse(all(a));} #define pb emplace_back #define eb emplace_back #define lb(v,k) (lower_bound(all(v),(k))-v.begin()) #define ub(v,k) (upper_bound(all(v),(k))-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void noyes(T b){if(b)out("no");else out("yes");} template<class T> void NoYes(T b){if(b)out("No");else out("Yes");} template<class T> void NOYES(T b){if(b)out("NO");else out("YES");} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} int main(){ ll t;cin>>t; rep(tt,t){ ll n;cin>>n; string a,b,c;cin>>a>>b>>c; string ans; rep(i,n)ans+='0'; rep(i,n)ans+='1'; ans+='0'; out(ans); } }
#include <bits/stdc++.h> using namespace std; int N; int ans[1010][1010]; int main() { cin >> N; int m = 2; ans[0][0] = 0; ans[0][1] = 1; for(int q = 0; q < N - 1; q++) { for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { for(int k = 0; k < m / 2; k++) { for(int l = 0; l < m; l++) { ans[k + i * m / 2 + m - 1][l + j * m] = ans[k + m / 2 - 1][l] ^ (i == 1 && j == 1); } } } } for(int i = 0; i < m - 1; i++) { for(int j = m - 1; j >= 0; j--) { ans[i][j * 2 + 1] = ans[i][j]; ans[i][j * 2] = ans[i][j]; } } m *= 2; } cout << m - 1 << endl; for(int i = 0; i < m - 1; i++) { for(int j = 0; j < m; j++) { cout << (!ans[i][j] ? 'A' : 'B'); } cout << endl; } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define f first #define s second #define pb push_back #define mp make_pair #define ull unsigned long long #define ln cout<<"\n" #define all(x) x.begin(),x.end() typedef vector<int> vi; typedef vector<long long> vl; typedef pair<int,int> pi; typedef pair<long long,long long> pl; #define fs(i,a,n) for (int i = a; i <=b; i++) #define fd(i,a,n) for (int i = a; i <=b; i--) #define mod 1000000007 void solve(){ int a,b,c; int n; cin>>n; vector<pair<pi,int>> v(n); for (int i = 0; i < n; i++) { cin>>a>>b>>c; v[i].s=a; v[i].f.f=b; v[i].f.s=c; } ll ans=0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { if(v[i].f.f<=v[j].f.f && v[i].f.s>v[j].f.s){ ans++;continue; } if(v[j].f.f<=v[i].f.f && v[j].f.s>v[i].f.s){ ans++;continue; } if(v[j].f.f<=v[i].f.f && v[i].f.f<v[j].f.s){ ans++;continue; } if(v[i].f.f<=v[j].f.f && v[j].f.f<v[i].f.s){ ans++;continue; } if(v[i].f.s==v[j].f.f){ if((v[i].s==3 || v[i].s==1)&&(v[j].s==2 || v[j].s==1))ans++; continue; } if(v[j].f.s==v[i].f.f){ if((v[j].s==3 || v[j].s==1)&&(v[i].s==2 || v[i].s==1))ans++; continue; } } } cout<<ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("inp.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int n=1; // cin>>n; while(n--){ solve(); } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define maxn 100005 #define maxm 200005 #define INF 1234567890 #define eps 1e-15 #define p 1000000007 const double pi=acos(-1); template<class T>inline bool read(T &x) { x=0;register char c=getchar();register bool f=0; while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();} while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); if(f)x=-x; return true; } template<class T>inline void print(T x) { if(x<0)putchar('-'),x=-x; if(x>9)print(x/10); putchar(x%10^48); } template<class T>inline void print(T x,char c){print(x),putchar(c);} template<class T,class ...S>inline bool read(T &x,S &...y){return read(x)&&read(y...);} ll n,x,a[maxn],b[maxn],c[maxn],ans; vector<ll>v[maxn]; int main() { read(n); for(ll i=1;i<=n;i++) { read(x); a[x]++; } for(ll i=1;i<=n;i++) { read(b[i]); v[b[i]].push_back(i); } for(ll i=1;i<=n;i++) { read(x); c[x]++; } for(ll i=1;i<=n;i++) { for(ll j=0;j<v[i].size();j++) { ans+=a[i]*c[v[i][j]]; } } print(ans,'\n'); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int int main() { ll n;cin>>n; vector<ll>a(n),b(n); for(ll i=0;i<n;++i) { cin>>a[i]; } for(ll i=0;i<n;++i) { cin>>b[i]; } ll l=a[0],r=b[0]; for(ll i=1;i<n;++i) { if(a[i]>l) { l=a[i]; } if(b[i]<r) { r=b[i]; } } if(l<=r) cout<<r-l+1<<"\n"; else cout<<"0\n"; }
// Date-- 24/04/2021 //practice #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define all(x) (x).begin(),(x).end() #define rep(i,a,b) for(ll i = a; i<b ; i++) #define sz(x) (int)((x).size()) #define PI 3.141592653589 #define mod 1000000007 #define MAX 1000001 //1e6+1 long long const INF = 1e18; void cf() { ll n; cin>>n; ll a[n],b[n]; rep(i,0,n) cin>>a[i]; rep(i,0,n) cin>>b[i]; ll x = *max_element(a,a+n); ll y = *min_element(b,b+n); ll ans = y-x+1; if(ans<=0) cout<<0; else cout<<ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int T=1; //cin>>T; while(T--) cf(); return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define mo 1000000007 #define ll long long #define maxn 1005 using namespace std; int n,i,j,k,f[2][2]; ll ans; ll ksm(ll x,ll y){ ll s=1; for(;y;y/=2,x=x*x%mo) if (y&1) s=s*x%mo; return s; } void reverse(){ static int g[2][2]; for(i=0;i<2;i++) for(j=0;j<2;j++) g[i^1][j^1]=f[j][i]^1; memcpy(f,g,sizeof(f)); } void doit1(){ static int f[maxn][2]; f[1][0]=1; for(i=2;i<=n-1;i++){ f[i][0]=(f[i-1][0]+f[i-1][1])%mo; f[i][1]=f[i-1][0]; } printf("%d\n",f[n-1][0]); } int main(){ scanf("%d",&n); char ch=getchar(); for(i=0;i<2;i++) for(j=0;j<2;j++){ while (ch!='A'&&ch!='B') ch=getchar(); f[i][j]=ch-'A',ch=getchar(); } if (f[0][1]==1) reverse(); if (n==2||f[0][0]==0) printf("1"); else if (f[1][0]==0) doit1(); else printf("%lld\n",ksm(2,n-3)); }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back using namespace std; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; using ll = long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using pdd = pair<ld,ld>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int n2 = 2*n; V<ll> a(n2); V<char> c(n2); rep(i,n2) cin >> a[i] >> c[i]; V<int> sum(3); rep(i,n2){ if(c[i]=='R') sum[0]++; if(c[i]=='G') sum[1]++; if(c[i]=='B') sum[2]++; } if(sum[0]%2==0 && sum[1]%2==0 && sum[2]%2==0){ cout << 0 << endl; return 0; } VV<ll> x(3); rep(i,n2){ if(c[i]=='R') x[0].pb(a[i]); if(c[i]=='G') x[1].pb(a[i]); if(c[i]=='B') x[2].pb(a[i]); } rep(i,3) sort(all(x[i])); if(sum[0]%2==0) swap(x[0], x[2]); if(sum[1]%2==0) swap(x[1], x[2]); ll ans = 1e18; rep(i,x[0].size()){ auto it = lower_bound(all(x[1]), x[0][i]); ans = min(ans, abs(*it-x[0][i])); if(it != x[1].begin()){ it--; ans = min(ans, abs(*it-x[0][i])); } } rep(i,x[1].size()){ auto it = lower_bound(all(x[0]), x[1][i]); ans = min(ans, abs(*it-x[1][i])); if(it != x[0].begin()){ it--; ans = min(ans, abs(*it-x[1][i])); } } //cout << 1 << endl; if(x[2].size()==0){ cout << ans << endl; return 0; } V<pll> t0, t1; rep(i,x[2].size()){ auto it = lower_bound(all(x[0]), x[2][i]); if(it != x[0].end()) t0.eb(abs(*it-x[2][i]),i); if(it != x[0].begin()){ it--; t0.eb(abs(*it-x[2][i]),i); } } rep(i,x[2].size()){ auto it = lower_bound(all(x[1]), x[2][i]); if(it != x[1].end()) t1.eb(abs(*it-x[2][i]),i); if(it != x[1].begin()){ it--; t1.eb(abs(*it-x[2][i]),i); } } sort(all(t0)); sort(all(t1)); //cout << 1 << endl; ll tmp = t0[0].first; rep(j,(int)t0.size()){ rep(i,t1.size()){ if(t1[i].second != t0[j].second){ ans = min(ans, t0[j].first+t1[i].first); break; } } } //cout << 1 << endl; tmp = t1[0].first; rep(j,(int)t1.size()){ rep(i,t0.size()){ if(t0[i].second != t1[j].second){ ans = min(ans, t1[j].first+t0[i].first); break; } } } cout << ans << endl; }
#include<bits/stdc++.h> // #pragma optimize("-O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,avx,mmx,tune=native") using namespace std; #define pb push_back #define mp make_pair #define ff first #define ss second #define INF 1e9 //#define BIG_INF 1e18 #define vi vector<ll> #define sz(a) ll((a).size()) #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1) // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update // typedef tree< // ll, // null_type, // less<ll>, // rb_tree_tag, // tree_order_statistics_node_update> // ordered_set; typedef long long ll; const ll BIG_INF = 3e18; ll mod = 1e9+7; ll fast_exp(ll a, ll b) { if(b <= 0) return 1; else { ll res = 1; res = fast_exp(a,b/2); res = (res*res)%mod; if(b%2 == 1) res = (res*a)%mod; return res; } } typedef long double ld; const long long N = 1e5+1000; const long double pi = acos(-1.0); using cd = complex<double>; const double PI = acos(-1); int par[2][N] = {}; int comp[2] = {}; int find_par(int v, int mode) { if(par[mode][v] == v) return v; else return par[mode][v] = find_par(par[mode][v],mode); } void union_find(int a, int b, int mode) { a = find_par(a,mode); b = find_par(b,mode); if(a != b) { par[mode][a] = b; comp[mode]--; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); //freopen("input.txt","r",stdin); int h, w; cin >> h >> w; char a[h][w] = {}; for(int i = 0; i<h; i++) for(int j = 0; j<w; j++) { cin >> a[i][j]; } a[0][0] = '#'; a[h-1][0] = '#'; a[0][w-1] = '#'; a[h-1][w-1] = '#'; for(int i = 0; i<h; i++) par[0][i] = i; comp[0] = h; for(int i = 0; i<w; i++) par[1][i] = i; comp[1] = w; for(int category = 0; category < w; category++) { int prev = -1; for(int i = 0; i<h; i++) { if(a[i][category] == '#') { if(prev == -1) prev = i; else { union_find(prev,i,0); prev = i; } } } } for(int category = 0; category < h; category++) { int prev = -1; for(int i = 0; i<w; i++) { if(a[category][i] == '#') { if(prev == -1) prev = i; else { union_find(prev,i,1); prev = i; } } } } int ans = min(comp[0],comp[1]); ans--; cout << ans << '\n'; // cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ; return 0; }
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native") #include <bits/stdc++.h> using namespace std ; #define int int64_t //be careful about this #define endl "\n" #define f(i,a,b) for(int i=int(a);i<int(b);++i) #define pr pair #define ar array #define fr first #define sc second #define vt vector #define pb push_back #define LB lower_bound #define UB upper_bound #define PQ priority_queue #define sz(x) ((int)(x).size()) #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define mem0(a) memset(a, 0, sizeof(a)) #define mem1(a) memset(a, -1, sizeof(a)) template<class A> void rd(vt<A>& v); template<class T> void rd(T& x){ cin >> x; } template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;} template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;} template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<typename T> void __p(T a) { cout<<a; } template<typename T, typename F> void __p(pair<T, F> a) { cout<<"{"; __p(a.first); cout<<","; __p(a.second); cout<<"}\n"; } template<typename T> void __p(std::vector<T> a) { cout<<"{"; for(auto it=a.begin(); it<a.end(); it++) __p(*it),cout<<",}\n"[it+1==a.end()]; } template<typename T, typename ...Arg> void __p(T a1, Arg ...a) { __p(a1); __p(a...); } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "; __p(arg1); cout<<endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(;; i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; const char *comma=names+i; cout.write(names,comma-names)<<" : "; __p(arg1); cout<<" | "; __f(comma+1,args...); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); cout.precision(15); cout << fixed; #ifdef ONLINE_JUDGE if(sz(s)){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } #endif } const int inf = 1e9, A = 26; signed main(){ setIO() ; int n,m; rd(n,m); int dp[n][n]; f(i,0,n) f(j,0,n) dp[i][j] = inf ; f(i,0,n) dp[i][i] = 0; vt<int> g[n][A]; f(_,0,m){ int u,v; char c; rd(u,v,c); --u,--v; ckmin(dp[u][v],int(1)); ckmin(dp[v][u],int(1)); g[u][c-'a'].pb(v); g[v][c-'a'].pb(u); } PQ<ar<int,3>> q; f(i,0,n) f(j,0,n) q.push({-dp[i][j],i,j}); while(sz(q)){ auto a = q.top(); q.pop(); int v = -a[0], i = a[1], j = a[2]; //if(dp[i][j] != v) continue; f(k,0,A){ for(auto x : g[i][k]){ for(auto y : g[j][k]){ if(ckmin(dp[x][y],v + 2)){ q.push({-dp[x][y],x,y}); } } } } } cout << (dp[0][n-1] == inf ? int(-1) : dp[0][n-1]); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i=0; i < (n); ++i) using ll = long long; using P = pair<int, int>; int n; vector<vector<int>> es; vector<int> a, b; vector<int> depth; void DepthDFS(int a, int d){ depth[a] = d; for (int next : es[a]){ if (depth[next] == -1){ DepthDFS(next, d+1); } } } vector<ll> dp; void ImosDFS(int a, ll now){ now += dp[a]; dp[a] = now; for (int next : es[a]) { if (depth[next] > depth[a]) { ImosDFS(next, now); } } } int main(){ cin >> n; es.resize(n); a.resize(n - 1); b.resize(n - 1); depth = vector<int>(n, -1); dp.resize(n); rep(i, n-1){ cin >> a[i] >> b[i]; a[i]--; b[i]--; es[a[i]].push_back(b[i]); es[b[i]].push_back(a[i]); } DepthDFS(0, 0); int q; cin >> q; rep(i, q){ int t, e, x; cin >> t >> e >> x; e--; int va, vb; if (t == 1){ va = a[e]; vb = b[e]; } else { vb = a[e]; va = b[e]; } if (depth[va] < depth[vb]) { dp[0] += x; dp[vb] -= x; } else { dp[va] += x; } } ImosDFS(0, 0); rep(i, n){ cout << dp[i] << endl; } return 0; }
#pragma region RegionDefs #include <bits/stdc++.h> #define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i) #define reps(i,l,r) for(int i=(l),i##_len=(r);i<i##_len;++i) #define repr(i,l,r) for(int i=(r)-1,i##_left=(l);i>=i##_left;--i) #define all(x) begin(x),end(x) using namespace std; typedef long long ll; const ll INF = 1e9; template<class T=ll> using V = vector<T>; template<class T=ll> using PQ = priority_queue<T>; template<class T=ll> using PQG = priority_queue<T, V<T>, greater<T>>; const ll MOD = 1000000007LL; void in() {} template<class Head, class... Tail> void in(Head&& head, Tail&&... tail) { cin >> head; in(move(tail)...); } #define IN(...) ll __VA_ARGS__; in(__VA_ARGS__) #define TIN(T, ...) T __VA_ARGS__; in(__VA_ARGS__) #define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem #define VIND(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem,--_elem #define OUT(x) cout << x; #define OUTL(x) cout << x << endl; template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;} template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;} template<class T> string join(vector<T>& v, string delim="") { ostringstream os; rep(i,v.size())i?os<<delim<<v[i]:os<<v[0]; return os.str(); } #pragma endregion RegionDefs // 区間更新できる1-indexedなBIT struct BIT { ll n; // 要素数 vector<ll> bit[2]; // データ BIT(ll n) : n(n) { bit[0].assign(n+1, 0); bit[1].assign(n+1, 0); } // i番目にvを足す void add_sub(int p, int i, ll v) { if (!i) return; for(int x = i; x <= n; x += x & -x) bit[p][x] += v; } // 区間[l,r)にvを足す void add(int l, int r, ll v) { add_sub(0, l, -v * (l - 1)); add_sub(0, r, v * (r - 1)); add_sub(1, l, v); add_sub(1, r, -v); } ll sum_sub(int p, int i) { ll s = 0; for (int x = i; x > 0; x -= x & -x) s += bit[p][x]; return s; } // [1,i]の総和 ll sum(int i) { return sum_sub(0, i) + sum_sub(1, i) * i; } // [l,r]の総和 ll sum(int l, int r) { return sum(r) - sum(l-1); } }; void euler(V<V<ll>>& g, ll from, ll v, V<ll>& res, V<ll>& b, V<ll>& e) { e[v] = b[v] = res.size(); res.push_back(v); for (auto to : g[v]) { if (to == from) continue; euler(g, v, to, res, b, e); e[v] = res.size(); res.push_back(v); } } void solve() { IN(n); V<V<ll>> g(n); V<ll> a(n-1), b(n-1); rep(i, n-1) { cin >> a[i] >> b[i]; --a[i]; --b[i]; g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } V<ll> res; V<ll> bg(n), en(n); euler(g, -1, 0, res, bg, en); BIT bit(res.size()); IN(q); rep(i, q) { IN(t, e, x); --e; ll from = t == 1 ? a[e] : b[e]; ll noth = t == 1 ? b[e] : a[e]; if (bg[from] < bg[noth]) { // nothはfromの子 bit.add(1, res.size() + 1, x); bit.add(bg[noth] + 1, en[noth] + 2, -x); } else { bit.add(bg[from] + 1, en[from] + 2, x); } } rep(i, n) OUTL(bit.sum(bg[i] + 1, bg[i] + 1)); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << setprecision(numeric_limits<double>::max_digits10); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef ARTHUR_LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif using ll = long long; #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define len(x) int((x).size()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define randint(n) uniform_int_distribution<int>(1, (n))(rng) int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ARTHUR_LOCAL ifstream cin("input.txt"); #endif ll n; cin>>n; set<ll> S; for(int p=2; p<=50; p++){ for(ll a=2; a<=100000; a++){ int p2=p; ll cur=1LL; while(p2--) cur*=a; if(cur>n) break; S.insert(cur); // cout<< } } cout<<n-ll(len(S))<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N,en,i; i=0; en=0; cin >> N; while (en<N) { en=en+i; i++; } cout << i-1 << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,(n)-1,0) #define all(v) v.begin(), v.end() #define endk '\n' const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; const ll mod2 = 998244353; const ld eps = 1e-10; template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;} template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;} int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<ll> W(n); rep(i, n) cin >> W[i]; vector<pair<ll, ll>> LV(m); ll minV = longinf; rep(i, m) { cin >> LV[i].first >> LV[i].second; chmin(minV, LV[i].second); } sort(all(LV), [&](pair<ll, ll> p1, pair<ll, ll> p2) { return p1.second < p2.second; }); vector<ll> L(m), V(m); rep(i, m) { L[i] = LV[i].first; V[i] = LV[i].second; } vector<ll> mx(m+1); rep(i, m) mx[i+1] = max(mx[i], L[i]); rep(i, n) { if(W[i] > minV) { cout << -1 << endk; return 0; } } vector<int> id(n); rep(i, n) id[i] = i; ll ans = longinf; auto solve = [&](vector<int> id) { vector<ll> sum(n+1); rep(i, n) sum[i+1] = sum[i]+W[id[i]]; vector<vector<ll>> d(n, vector<ll>(n)); rep(i, n) { REP(j, i+1, n) { int k = upper_bound(all(V), sum[j+1]-sum[i]-1)-begin(V); d[i][j] = mx[k]; } } vector<ll> D(n); rep(i, n) { REP(j, i+1, n) { chmax(D[j], D[i]+d[i][j]); } } chmin(ans, D[n-1]); }; do { solve(id); } while(next_permutation(all(id))); cout << ans << endk; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int BS=524288; int CNT,rc; char inb[BS]; inline char gchar(void){ if(CNT%BS==0){ CNT=0; rc=fread(inb,1,BS,stdin); } return CNT<rc?inb[CNT++]:0; } inline int _readint(int *ptr){ int n=0,c=0; *ptr=0; while(1){ char read=gchar(); if(read>='0'&&read<='9')read-='0',n*=10,n+=read; else{*ptr=n;return n==0?0:!c;} c++; } *ptr=n; return 0; } inline int readint(void){int ptr;while(_readint(&ptr));return ptr;} int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; // cin >> n >> m; n=readint(); m=readint(); vector<ll> w(n); for(int i=0;i<n;i++){ // cin >> w[i]; w[i]=readint(); } sort(w.rbegin(), w.rend()); vector<ll> l(m),v(m); for(int i=0;i<m;i++){ // cin >> l[i] >> v[i]; l[i]=readint(); v[i]=readint(); } for(int i=0;i<m;i++){ if(v[i]<w[0]){ printf("-1\n"); return 0; } } ll res=1e18; vector<int> p(n-2); for(int i=0;i<n-2;i++){ p[i]=i+2; } do{ vector<ll> vec(n,0); vector<int> pp(n); pp[0]=0; for(int i=0;i<n-2;i++){ pp[i+1]=p[i]; } pp[n-1]=1; for(int i=1;i<n;i++){ ll mi=vec[i-1]; for(int j=0;j<m;j++){ ll sum=w[pp[i]]; for(int k=i-1;k>=0;k--){ if(abs(mi-vec[k])<l[j]){ // cout << i << " " << k << " " << mi << endl; sum+=w[pp[k]]; // cout << sum << endl; if(sum>v[j]){ mi=vec[k]+l[j]; break; } } else break; } } vec[i]=mi; } res=(res>vec[n-1]?vec[n-1]:res); // res=min(res,vec[n-1]); }while(next_permutation(p.begin(), p.end())); cout << res << endl; }
#include <bits/stdc++.h> using namespace std; using i8 = int8_t; using i16 = int16_t; using i32 = int32_t; using i64 = int64_t; using isize = ptrdiff_t; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using usize = size_t; using f32 = float_t; using f64 = double_t; auto main() -> i32 { ios::sync_with_stdio(false), cin.tie(nullptr); usize n, m; cin >> n >> m; vector<usize> f(1000 + 1, 0); for (usize i = 0; i < n; i++) { usize a; cin >> a; f[a] = 1; } for (usize i = 0; i < m; i++) { usize b; cin >> b; f[b] = (f[b] + 1) % 2; } for (usize i = 0; i < 1000 + 1; i++) { if (f[i] == 1) { cout << i << " "; } } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; //#define endl '\n' #define pb push_back #define ll long long #define int ll #define FF first #define SS second #define f(a,b,c) for(int a=b;a<=c;a++) #define fd(a,b,c) for(int a=b;a>=c;a--) #define fspr(X) fixed<<setprecision(X) int mod = 1000000007; ll mod_pow(ll x, ll n, ll m = mod) { if (n < 0) { ll res = mod_pow(x, -n, m); return mod_pow(res, m - 2, m); } if (abs(x) >= m)x %= m; if (x < 0)x += m; ll res = 1; while (n) { if (n & 1)res = res * x % m; x = x * x % m; n >>= 1; } return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int T=1;//cin>>T; while(T--) { int n,m;cin>>n>>m; int A[n],B[m]; f(i,0,n-1)cin>>A[i];f(i,0,m-1)cin>>B[i]; int i=0,j=0; while(i<n || j<m){ if(i>=n){ while(j<m){ cout<<B[j]<<" ";j++; } } if(j>=m){ while(i<n){ cout<<A[i]<<" ";i++; } } while(i<n && j<m && A[i]<B[j]){ cout<<A[i]<<" ";i++; } while(j<m && i<n && A[i]>B[j]){ cout<<B[j]<<" ";j++; } while(i<n && j<m && A[i]==B[j]){ i++;j++; } } cout<<endl; } }
#include <bits/stdc++.h> #define int long long using namespace std; const int maxn=3e5+5; int a[maxn]; int b[maxn]; int flag[maxn]; void solve() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } for(int i=1;i<=n;i++) { cin>>b[i]; } int sum=0; for(int i=1;i<=n;i++) { sum+=a[i]*b[i]; } if(sum==0) cout<<"Yes"; else cout<<"No"; } signed main() { cin.tie(),cout.tie(); int _=1; //cin>>_; while(_--) { solve(); } }
#include <bits/stdc++.h> using ll = long long; using namespace std; #define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define repi(i, x) \ for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++) #define all(x) (x).begin(), (x).end() #define F first #define S second #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back string solve(bool a) { return ((a) ? "Yes" : "No"); } typedef vector<int> Vi; typedef vector<Vi> VVi; typedef pair<int, int> Pi; typedef vector<Pi> VPi; typedef vector<long long> V; typedef vector<V> VV; typedef pair<long long, long long> P; typedef vector<P> VP; 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, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; rep(i, v.size()) { if (i) os << ","; os << v[i]; } os << "}"; return os; } template <class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> istream& operator>>(istream& is, vector<T>& v) { rep(i, v.size()) { is >> v[i]; } return is; } const long long INFLL = 1LL << 60; const int INF = 1 << 30; const double PI = acos(-1); int main() { int n; ll ans = 0LL; cin >> n; V a(n), b(n); cin >> a >> b; rep(i, n) { ans += a[i] * b[i]; } cout << solve(ans == 0LL) << endl; }
#include<bits/stdc++.h> using namespace std; inline int read(){ int res=0; bool zf=0; char c; while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-')zf=1; else res=c-'0'; while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0'; if(zf)return -res; return res; } const int maxn=4e5+5,P=1e9+7; int a[maxn]; int jc[maxn],inv[maxn]; inline int c(int x,int y){ if(x<y){ return 0; } y=min(y,x-y); if(!y){ return 1; } return x; } int C(int x,int y){ if(!y){ return 1; } return C(x/3,y/3)*c(x%3,y%3)%3; } signed main(){ int n=read(); string s;cin>>s; for(register int i=0;i<n;++i){ if(s[i]=='R'){ a[i+1]=0; } else if(s[i]=='W'){ a[i+1]=1; } else{ a[i+1]=2; } } if(!(n&1)){ for(register int i=1;i<n;++i){ if(a[i]!=a[i+1]){ a[i]=3-a[i]-a[i+1]; } } --n; } int A=0; for(register int i=1;i<=n;++i){ A=(A+a[i]*C(n-1,i-1))%3; } if(A==0){ puts("R"); } else if(A==1){ puts("W"); } else{ puts("B"); } 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 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) ll g1(vl val) { ll Max = 0; ll MaxNum=-1; ll N =10; MFOR(i, 1, N) { if(val[i]>0) { MaxNum = i; break; } } if(MaxNum==-1) { // val = 0 return 0; } Max = MaxNum; val[MaxNum] --; MREP(i, N) { while(val[i]>0) { Max = Max*10+i; val[i] --; } } // cout << "Max:" << Max << endl; return Max; } ll g2(vl val) { ll Max = 0; ll Min = 0; ll MinNum=-1; ll N =10; REP(i, N) { while(val[i]>0) { Min = Min*10+i; val[i] --; } } // cout << "Min:" << Min << endl; return Min; } vl make_vals(ll N) { vl vals(10, 0); ll Min = N%10; vals[Min] ++; N/=10; while(N >0) { Min = N%10; vals[Min] ++; N /= 10; } return vals; } int main(void) { ll N, K; cin >> N >> K; vl NowVal = make_vals(N); vl anss; if(K==0) { cout << N << endl; } else { map<ll, ll> Val_Times; ll NowN = N; Val_Times[NowN] = 1; anss.push_back(N); ll Tim = 1; ll NextN = g1(NowVal)-g2(NowVal); vl NextVal = make_vals(NextN); anss.push_back(NextN); while(Val_Times[NowN] < K && NextN >0) { NowVal = NextVal; NowN = NextN; Val_Times[NextN] = ++Tim; // cout << NextN << " " << Tim << endl; NextN =g1(NowVal)-g2(NowVal); NextVal = make_vals(NextN); anss.push_back(NextN); } if(NextN==0) { cout << 0 << endl; } else { ll Ans = g1(NowVal)-g2(NowVal); cout << Ans << endl; } } return 0; }
#include<bits/stdc++.h> #define ll long long #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define w(t) ll t;cin>>t;while(t--) #define pb push_back #define mp make_pair #define fi first #define se second #define all(x) x.begin(), x.end() #define fo(i,a,n) for(ll i=a;i<n;i++) #define precision cout<<fixed<<setprecision(10) const ll mod=1e9+7; using namespace std; int main() { IOS ll n,s=0,r; cin>>n; fo(i,0,n) { ll a,b; cin>>a>>b; r=((b*(b+1))/2)-((a*(a-1))/2); s=s+r; } cout<<s; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; const int INF=1e9; const int MOD=1e9+7; int main(){ int N; cin >> N; ll sum=0; for(int i=0;i<N;i++){ ll a,b; cin >> a >> b; sum+=(a+b)*(b-a+1)/2; } cout << sum << endl; }
#include<iostream> #include<stdlib.h> #include<string> #include<iomanip> using namespace std; int main() { int si, sj; cin >> si >> sj; int t[51][51],p[51][51],i,j,k; for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) cin >> t[i][j]; for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) cin >> p[i][j]; string idou; int idoumax=0,ugoku=0; int hikaku[5]; //動けるところで一番大きいところを探して動く while (1) { //探す for (i = 0; i < 4; i++) { //上 if (i == 0) { if (sj - 1 >= 0) { if (t[si][sj] != t[si][sj - 1]) hikaku[0] = p[si][sj - 1]; else { hikaku[0] = -1; p[si][sj - 1] = -1; } } else { hikaku[0] = -1; p[si][sj - 1] = -1; } } //左 else if (i == 1) { if(si-1>=0){ if (t[si][sj] != t[si - 1][sj]) hikaku[1] = p[si - 1][sj]; else { hikaku[1] = -1; p[si - 1][sj] = -1; } } else { hikaku[1] = -1; p[si - 1][sj] = -1; } } //下 else if (i == 2) { if(sj+1<=49){ if (t[si][sj] != t[si][sj + 1]) hikaku[2] = p[si][sj + 1]; else { hikaku[2] = -1; p[si][sj + 1] = -1; } } else { hikaku[2] = -1; p[si][sj + 1] = -1; } } //右 else { if(si+1<=49){ if (t[si][sj] != t[si + 1][sj]) hikaku[3] = p[si + 1][sj]; else { hikaku[3] = -1; p[si + 1][sj] = -1; } } else { hikaku[3] = -1; p[si + 1][sj] = -1; } } } idoumax = -1; for (int i = 0; i < 4; i++) if (idoumax < hikaku[i]) idoumax = hikaku[i]; if (idoumax == -1) break; for (i = 0; i < 4; i++){ if (idoumax == hikaku[i]) { ugoku = i; break; } } //動く //上 if (ugoku == 0) { idou += 'L'; p[si][sj] = -1; sj--; } //左 else if (ugoku == 1) { idou += 'U'; p[si][sj] = -1; si--; } //下 else if (ugoku == 2) { idou += 'R'; p[si][sj] = -1; sj++; } //右 else if (ugoku == 3) { idou += 'D'; p[si][sj] = -1; si++; } else break; } cout << endl; cout << idou << endl; 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; double start_temp = 200, end_temp = 10; 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]; } double temp = start_temp + (end_temp - start_temp) * chrono::duration_cast<std::chrono::milliseconds>(dur).count() / 2000; double prob = exp((pnt - lastpnt) / temp); if (pnt >= lastpnt) { lastpnt = pnt; lastans = ans; } else if(pnt != -1 && r <= prob * 1000){ lastpnt = pnt; lastans = ans; } dur = chrono::system_clock::now() - start; } cout << lastans << "\n"; }
#include<bits/stdc++.h> using namespace std; #define int long long const int N=1e5+100; int n,a[N]; int sum[N]; signed main(){ ios::sync_with_stdio(false),cin.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n); for(int i=1;i<=n;i++){ sum[i]=sum[i-1]+a[i]; } double ans=1e18; for(int i=1;i<=n;i++){ double x=1.0*a[i]/2; ans=min(ans,x*i+(sum[n]-sum[i])-(n-i)*x); } cout<<fixed<<setprecision(20)<<ans/n<<endl; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll=long long; using ld=long double; using pll=pair<ll, ll>; //using mint = modint1000000007; #define rep(i,n) for (ll i=0; i<n; ++i) #define all(c) begin(c),end(c) #define PI acos(-1) #define oo 2e12 template<typename T1, typename T2> bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} template<typename T1, typename T2> bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} //priority_queue<ll, vector<ll>, greater<ll>> Q; // LMAX = 18446744073709551615 (1.8*10^19) // IMAX = 2147483647 (2.1*10^9) /* */ int main(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); ll N; cin >> N; ll A[100010]; rep(i, N) cin >> A[i]; ld high = oo; ld low = 0.0; ld mh = (high*2.0 + low)/3.0; ld ml = (high + low * 2.0)/3.0; ld ans = oo; while (high > low + 0.00001){ ld lost1 = 0.0; ld lost2 = 0.0; rep(i, N){ lost1 += mh + A[i] - min(2.0*mh, ld(A[i])); lost2 += ml + A[i] - min(2.0*ml, ld(A[i])); } if (lost1 > lost2) high = mh; else low = ml; mh = (high*2.0 + low)/3.0; ml = (high + low * 2.0)/3.0; chmin(ans, lost1/N); chmin(ans, lost2/N); } ans += 0.000000001; cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("input.in","r",stdin); int n,x; cin>>n>>x; while(n--){ char ch; cin>>ch; if(ch=='o') x++; else x=max(0,x-1); } cout<<x<<endl; }
#include<bits/stdc++.h> #define pb push_back #define x first #define y second #define pdd pair<double,double> #define pii pair<int,int> #define pll pair<LL,LL> #define mp make_pair #define LL long long #define ULL unsigned long long #define sqr(x) ((x)*(x)) #define pi acosl(-1) #define MEM(x) memset(x,0,sizeof(x)) #define MEMS(x) memset(x,-1,sizeof(x)) using namespace std; #define Point pdd #define Polygon vector<Point> #define Line pair<Point,Point> #define rank Rank bool solve(){ int n,k; scanf("%d %d",&n,&k); int cnt[300005]; fill(cnt,cnt+n+1,0); for(int i = 0;i<n;i++){ int x; scanf("%d",&x); cnt[x]++; } int last=k; LL ans=0; for(int i = 0;i<n;i++){ ans+=max(last-cnt[i],0)*i; last=min(last,cnt[i]); } printf("%lld\n",ans); return true; } int main(){ int t=1;//00000; // scanf("%d",&t); while(t--)solve(); } /* 1 4 10100 1 6 1010100 1 8 101010100 1 y 10100 (y+1) y/2 2 5 1100100 2 6 11001100 2 7 110011000 2 8 1100110000 2 9 11001100100 */
#include<cstdio> #include<algorithm> #include<cstring> #include<cctype> #include<cstdlib> #include<ctime> using std::max; using std::min; using std::sort; using std::swap; typedef long long LL; int T; LL N; int main() { #ifndef ONLINE_JUDGE freopen("1.in","r",stdin); #endif scanf("%d",&T); while(T--) { scanf("%lld",&N); if(N&1) puts("Odd"); else { if(N%4==0) puts("Even"); else puts("Same"); } } 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<ll, ll>; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; const int mod = 1000000007; //const int mod = 998244353; //MINT struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} // POWER_MODver. N^k % MOD ll mod_pow(ll n, ll k){ ll res = 1; for(; k > 0; k >>= 1){ if(k&1) res = (res*n)%mod; n = (n*n)%mod; } return res; } int main() { int n; cin >> n; vector<mint> a(n); rep(i, 0, n) cin >> a[i]; vector<vector<mint>> dp(n, vector<mint> (2)); vector<vector<mint>> dpp(n+1, vector<mint> (2)); dpp[0][0] = 1; rep(i, 1, n){ dpp[i][0] += dpp[i-1][0] + dpp[i-1][1]; dpp[i][1] += dpp[i-1][0]; } dp[0][0] = a[0]; rep(i, 1, n){ dp[i][0] += dp[i-1][0] + a[i]*mint(dpp[i-1][0]) + dp[i-1][1] + a[i]*mint(dpp[i-1][1]); dp[i][1] += dp[i-1][0] - a[i]*mint(dpp[i-1][0]); } cout << dp[n-1][0] + dp[n-1][1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(i64 i=0; i<n; i++) #define all(v) v.begin(),v.end() #define pp pair<int,int> using Graph=vector<vector<int>>; using i64=int64_t; int main() { int n,m;cin>>n>>m; rep(i,m) { string s; cin>>s; } rep(i,20) { rep(j,20) { cout << "H"; } cout << endl; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n'; const ll nmax = 1000000007; int main(){ ll a, b; cin >> a >> b; ll sa = 0, sb = 0; while(a > 0){ sa += (a%10); a /= 10; }while(b > 0){ sb += (b%10); b /= 10; } cout << (max(sa, sb)) << endl; return 0; }
#include<iostream> #include<array> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> #include<utility> #include<tuple> #include<cassert> using namespace std; typedef long long ll; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define Per(i,sta,n) for(int i=n-1;i>=sta;i--) typedef long double ld; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;} template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;} template <typename T> struct Dijkstra{ struct Edge{ int to; T cost; Edge(int to,T cost):to(to),cost(cost){} bool operator<(const Edge &o)const{return cost>o.cost;} }; const T inf = (numeric_limits<T>::max())/2;//infを設定する vector<vector<Edge>> G; vector<T> ds; vector<int> bs; Dijkstra(int n):G(n){} void add_edge(int u,int v,T c){ G[u].push_back(Edge(v,c)); } void build(int s){ int n=G.size(); ds.assign(n,inf); bs.assign(n,-1); priority_queue<Edge> pq; ds[s]=0; pq.emplace(s,ds[s]); while(!pq.empty()){ auto p=pq.top();pq.pop(); int v=p.to; if(ds[v]<p.cost) continue; for(auto e:G[v]){ if(ds[e.to]>ds[v]+e.cost){ ds[e.to]=ds[v]+e.cost; bs[e.to]=v; pq.emplace(e.to,ds[e.to]); } } } } T operator[](int k){return ds[k];} vector<int> restore(int to){ vector<int> res; if(bs[to]<0) return res; while(~to) res.emplace_back(to),to=bs[to]; reverse(res.begin(),res.end()); return res; } }; int n,m,k; int c[20]; int d[20][20]; int dp[200010][20]; void solve(){ cin >> n >> m; Dijkstra<int> dij(n); rep(i,m){ int a,b;cin >> a >> b;a--;b--; dij.add_edge(a,b,1); dij.add_edge(b,a,1); } cin >> k; rep(i,k) { cin >> c[i];c[i]--; } rep(i,k){ dij.build(c[i]); rep(j,k){ d[i][j]=dij[c[j]]; } } int U=((1<<k)-1); rep(S,U+1){ rep(i,k) dp[S][i]=mod; } rep(i,k) dp[(1<<i)][i]=1; rep(S,U+1){ rep(i,k){ rep(j,k){ if(S&(1<<i)) if(S&(1<<j)){ chmin(dp[S][i],dp[S^(1<<i)][j]+d[j][i]); } } } } int ans=mod; rep(i,k) chmin(ans,dp[U][i]); if(ans<mod)cout << ans << endl; else cout << -1 << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }
//#pragma GCC optimize("Ofast") //#pragma GCC target("avx2") #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define uniq(x) x.resize(unique(all(x)) - x.begin()) #define ff first #define ss second #define pb push_back #define emb emplace_back using namespace std; using ull = unsigned long long; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; const int N = 1e5 + 5, K = 17; int n, m, k, c[K], dist[K][N], dp[1 << K][K]; queue<int> q; vector<int> adj[N]; void bfs(int s, int d[]) { for (int i = 1; i <= n; ++i) d[i] = -1; d[s] = 0; q.emplace(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int &v: adj[u]) { if (d[v] == -1) { d[v] = d[u] + 1; q.emplace(v); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1, a, b; i <= m; ++i) { cin >> a >> b; adj[a].emb(b); adj[b].emb(a); } cin >> k; for (int i = 0; i < k; ++i) { cin >> c[i]; bfs(c[i], dist[i]); } for (int mk = 1; mk < (1 << k); ++mk) { if (__builtin_popcount(mk) == 1) { for (int i = 0; i < k; ++i) { if ((mk >> i) & 1) { dp[mk][i] = 1; } else { dp[mk][i] = -1; } } } else { for (int i = 0; i < k; ++i) { if ((mk >> i) & 1) { int omk = mk ^ (1 << i); dp[mk][i] = 1e9; for (int j = 0; j < k; ++j) { if (((omk >> j) & 1) && (dp[omk][j] != -1) && (dist[i][c[j]] != -1)) { dp[mk][i] = min(dp[mk][i], dp[omk][j] + dist[i][c[j]]); } } if (dp[mk][i] == 1e9) { dp[mk][i] = -1; } } else { dp[mk][i] = -1; } } } } int ans = 1e9; for (int i = 0; i < k; ++i) { if (dp[(1 << k) - 1][i] != -1) { ans = min(ans, dp[(1 << k) - 1][i]); } } if (ans == 1e9) { ans = -1; } cout << ans << "\n"; return 0; }
#define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <vector> #include <array> #include <map> #include <unordered_map> #include <list> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <limits> #include <numeric> #include <iomanip> #include <string> #include <sstream> using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define chmin(x, y) x = std::min(x, y) #define chmax(x, y) x = std::max(x, y) #define all(v) v.begin(), v.end() #define rad_to_deg(rad) (((rad)/2/M_PI)*360) #define deg_to_rad(deg) (((deg)/360)*2*M_PI) typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; template <typename T> void dump(const vector<T>& v) { for (const T& n : v) { cout << n << " "; } cout << endl; } template <typename T> void dump(const vector<vector<T>>& v) { for (const vector<T>& ns : v) { for (const T& n : ns) { cout << n << " "; } cout << endl; } } const int INF = (numeric_limits<int>::max)() / 2; const ll INF_LL = (numeric_limits<ll>::max)() / 2; const ll MOD = 1000000007; void solve() { int n, m; cin >> n >> m; vector<P> a(n); rep(i, n) { cin >> a[i].first; a[i].second = i; } vector<vector<ll>> ls(n); rep(i, m) { ll x, y; cin >> x >> y; x--; y--; ls[y].push_back(x); } vector<ll> dp(n, INF_LL); for (int i = 1; i < n; i++) { for(ll j : ls[i]) { dp[i] = min({ dp[i], dp[j], a[j].first }); } } ll ans = -INF_LL; rep(i, n) { chmax(ans, a[i].first - dp[i]); } cout << ans << endl; } int main() { int t = 1; while (t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; using ll=long long; vector<vector<int>> t; int ans=0; int k; int n; void cal(int s,int cnt,vector<bool>ok,int sum){ for(int i=1;i<n;++i){ if(ok[i]==true || s==i)continue; //int tmp=sum; ok[i]=true; sum+=t[s][i]; cal(i,cnt+1,ok,sum); ok[i]=false; sum-=t[s][i]; } //cout << s << endl; if(cnt==n){ //cout << s << endl; sum+=t[s][0]; //cout << sum << endl; if(sum==k)ans++; } return; } int main(){ //int n,k; cin >> n >> k; t=vector<vector<int>>(n,vector<int>(n)); for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ cin >> t[i][j]; } } vector<bool> ok(n,false); ok[0]=true; int sum=0; cal(0,1,ok,sum); cout << ans << endl; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) typedef long long ll; int main(){ //input, initialize int a, b, c; cin >> a >> b >> c; if(a == b){ cout << c << endl; } else if(b == c){ cout << a << endl; } else if(c == a){ cout << b << endl; } else{ cout << 0 << endl; } //solve //output }
#include <bits/stdc++.h> using namespace std; #define int long long int #define pb emplace_back #define mp make_pair #define fi first #define se second #define all(v) v.begin(),v.end() #define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0); #define LL_MAX LLONG_MAX #define ub(v,x) upper_bound(v.begin(),v.end(),x) #define lb(v,x) lower_bound(v.begin(),v.end(),x) #define mod 1000000007 int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;} int modinv(int x){return exp(x,mod-2);} int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;} int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;} int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;} int fac[1000009];int ncr_mod(int n,int k){int ans=fac[n];ans*=modinv(fac[k]);ans%=mod;ans*=modinv(fac[n-k]);ans%=mod;return ans;} vector<int>v_prime;void Sieve(int n){bool prime[n + 1];memset(prime,true,sizeof(prime));for (int p = 2; p*p <=n;p++){if(prime[p] ==true) {for(int i = p*p; i<= n; i += p)prime[i]=false;}}for(int p = 2;p<= n;p++)if (prime[p])v_prime.pb(p);} vector<int>v_factor;void factor(int n){ for (int i=1; i<=sqrt(n); i++) {if (n%i == 0) {if (n/i == i) v_factor.pb(i);else { v_factor.pb(i),v_factor.pb(n/i);};} } sort(all(v_factor)); } int power(int x, int y){int temp;if( y == 0)return 1; temp = power(x, y / 2);if (y % 2 == 0) return temp * temp;else return x * temp * temp;} int gcd(int a, int b){if (b == 0)return a; return gcd(b, a % b);} int log2n( int n){ return (n > 1) ? 1 + log2n(n / 2) : 0;} void out(vector<int>&a){for(int i=0;i<a.size();i++) cout<<a[i]<<" "; cout<<endl;} void sout(set<int>s){for(auto it=s.begin();it!=s.end();++it)cout<<*it<<" "; cout<<endl;} void mout(map<int,int>mm){for(auto it=mm.begin();it!=mm.end();++it) cout<<it->fi<<" "<<it->se<<endl;} #define ms(a,x) memset(a, x, sizeof(a)); #define decimal(n,k) cout<<fixed<<setprecision(k)<<n<<endl #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl // In binary search always report l-1 signed main() { run; /* open for mod factorial fac[0]=1; for(int i=1;i<1000009;i++) { fac[i]=mul(fac[i-1],i); } */ /* for sieve open this and use v_prime int pp=pow(10,6)+100000; Sieve(pp); */ // factor(n) && USE v_factor For calculating factors && don't forget v_factor.clear(); int t=1; while(t--) { // map<int,int>mm; // it->second-->frequency int n,i,x,y,ok=0,sum=0,ans=0,j,k,cnt=0,m,c=0,q,z; int h[100009]={0}; n=3; string s; //cin>>s; vector<int>a(n,0),v; for(i=0;i<n;i++) cin>>a[i]; sort(all(a)); if(a[0]==a[1]) { cout<<a[2]<<endl; } else { if(a[1]==a[2]) cout<<a[0]<<endl; else cout<<0<<endl; } } }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define rng(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define per(i, b) gnr(i, 0, b) #define all(obj) begin(obj), end(obj) #define allr(obj) rbegin(obj), rend(obj) #define cinv(a) rep(i, (int)a.size()) cin >> a[i] #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> Pi; typedef vector<Pi> vp; typedef vector<vp> vvp; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<vb> vvb; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e15; const long double eps = 1e-10; const char nl = '\n'; ll power(ll a, ll b) { return b ? power(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1; } ll nCk(int n, int k) { ll x = 1, y = 1; for (int i = 1; i <= k; ++i) { x = x * (n - i + 1) % MOD; y = y * i % MOD; } return x * power(y, MOD - 2) % MOD; } struct UF { vi d; UF(int n) : d(n, -1) {} int root(int x) { if (d[x] < 0) return x; return d[x] = root(d[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -d[root(x)]; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vi A(n); cinv(A); vvi mp(200); auto show = [](vi &vec) -> void { cout << sz(vec); for (int &i : vec) cout << " " << i; cout << nl; }; n = min(n, 8); rng(bit, 1, 1<<n) { int a = 0; vi s; rep(i, n) { if (bit >> i & 1) { a += A[i]; a %= 200; s.pb(i+1); } } if (sz(mp[a]) != 0) { cout << "Yes" << nl; show(mp[a]); show(s); return 0; } mp[a] = s; } cout << "No" << nl; }
#include <bits/stdc++.h> using namespace std; #define MAXN (int)(2 * 1e5 + 5) #define MAXL 20 #define F first #define S second #define endl "\n" #define MOD (lli)(1e9 + 7) #define lli long long int #define sz(a) int(a.size()) #define DEBUG if (0) cout << "aqui" << endl; #define PI 2 * acos(0.0) typedef pair<int, int> ii; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int dddx[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dddy[] = {0, 0, 1, -1, 1, -1, 1, -1}; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; cout << max(0, n) << endl; return 0; }
#include<iostream> #include<map> #include<vector> using namespace std; typedef long long li; #define maximize(a,b) (a<(b)?a=(b),1:0) #define rep(i,n) for(li i=0;i<(n);i++) #define df 0 template<class T> void print(const T& t){ cout << t << "\n"; } template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); } // Container コンテナ型, map以外 template< template<class ...> class Ctn,class T> std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){ auto itr=v.begin(); while(itr!=v.end()){ if(itr!=v.begin())cout << " "; cout << *(itr++); } return os; }; // pair 型 template<class S,class T> std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){ cout << "(" << p.first << "," << p.second << ")"; return os; } int main(){ int n,m; cin >>n >>m; vector<pair<int,int>> cond(m); rep(i,m){ int a,b; cin >>a >>b; a--; b--; cond[i]={a,b}; } auto ts=[&](vector<int>& sara){ int cnt=0; rep(i,m){ int f=cond[i].first,s=cond[i].second; cnt+=sara[f]&sara[s]; } return cnt; }; int k ; cin >>k; vector<int> c(k),d(k); rep(i,k){ int cc,dd; cin >>cc >>dd; cc--; dd--; c[i]=cc; d[i]=dd; } li lim=1LL<<k; int ans=0; rep(bs,lim){ vector<int> sara(n,0); rep(i,k){ if(bs&(1LL<<i)){ sara[c[i]]|=1; }else{ sara[d[i]]|=1; } } if(maximize(ans,ts(sara))){ if(df)(print(sara,ans)); } } print(ans); }
#include<iostream> #include<string.h> #include<string> #include<stdio.h> #include<algorithm> #include<vector> #include<bitset> #include<math.h> #include<stack> #include<queue> #include<set> #include<map> using namespace std; typedef long long ll; typedef long double db; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int,int> pii; const int N=100000+100; const db pi=acos(-1.0); #define lowbit(x) ((x)&(-x)) #define sqr(x) (x)*(x) #define rep(i,a,b) for (register int i=a;i<=b;i++) #define per(i,a,b) for (register int i=a;i>=b;i--) #define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to) #define fir first #define sec second #define mkp make_pair #define pb push_back #define maxd 998244353 #define eps 1e-8 inline int read() { int x=0,f=1;char ch=getchar(); while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();} while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();} return x*f; } inline ll readll() { 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; } ll f[110],n; int m; vi ans,a; int main() { f[0]=1;f[1]=1;m=88; rep(i,2,m) f[i]=f[i-2]+f[i-1]; //rep(i,1,10) cout << f[i] << " ";cout << endl; n=readll(); per(i,m,1) { if (n>=f[i]) {n-=f[i];a.pb(i);} } int siz=a.size(); int len=(a[0]+1)/2,pos=siz-1; //rep(i,0,siz-1) printf("%d ",a[i]);puts(""); rep(i,1,len) { ans.pb(3);ans.pb(4); while ((pos>=0) && ((a[pos]+1)/2==i)) { if (a[pos]&1) ans.pb(2);else ans.pb(1);pos--; } } len=ans.size(); printf("%d\n",len); per(i,len-1,0) printf("%d\n",ans[i]); return 0; }
#include<bits/stdc++.h> #define For(i,a,b) for(register int i=(a);i<=(b);++i) #define Rep(i,a,b) for(register int i=(a);i>=(b);--i) #define int long long using namespace std; inline int read() { char c=getchar();int x=0;bool f=0; for(;!isdigit(c);c=getchar())f^=!(c^45); for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); if(f)x=-x;return x; } #define fi first #define se second #define mkp make_pair #define pb push_back typedef pair<int,int>pii; #define maxn 200005 int b,c; inline int cross(int l1,int r1,int l2,int r2){ if(l1>l2)swap(l1,l2),swap(r1,r2); //cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<endl; if(l2<=r1)return max(r1,r2)-l1+1; return r2-l2+1+r1-l1+1; } signed main() { b=read(),c=read(); if(b>=0){ int l1=-b-(c-1)/2,r1=-b+(c-1)/2; int l2=b-c/2,r2=b; if(c>2)r2=max(r2,b+(c-2)/2); cout<<cross(l1,r1,l2,r2); }else{ int l1=b-c/2,r1=b+(c-2)/2; int l2=-b-(c-1)/2,r2=-b+(c-1)/2; cout<<cross(l1,r2,l2,r2); } return 0; }
#include <iostream> #include <bits/stdc++.h> #include <vector> #include <algorithm> #include <utility> #include <cmath> #include <unordered_map> #include <math.h> #define mod 1000000007 #define PI 3.141592653589 typedef long long ll; const long long INF=1e18; const long long inf=1e5 + 5; using namespace std; ll power(ll a, ll b) { ll result =1; while(b) { if(b%2) result=(result*a)%mod; a=(a*a)%mod; b/=2; } return result; } void solve() { ll n,i,j; ll sum=0; cin>>n; vector <ll> a(n); for(auto &x : a) { cin>>x; sum+=x; } //Bottom up approach check whether given sum can be made or not bool dp[n+1][sum+1]; for(i=0;i<=n;i++) //if sum is 0 answer is true { dp[i][0]=true; } for(i=1;i<=sum;i++) //if sum>0 and set is empty { dp[0][i]=false; } //bottom up approach for(i=1;i<=n;i++) { for(j=1;j<=sum;j++) { if(dp[i-1][j]) { dp[i][j]=true; } else { if(j>=a[i-1] && dp[i-1][j-a[i-1]]) { dp[i][j]=true; } else { dp[i][j]=false; } } } } ll init=(sum+1)/2; for(i=init;i<=sum;i++) { if(dp[n][i]) { cout<<i; return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t=1; //cin>>t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int K = 18; const int N = 100005; const int INF = 1e9; int n, m; vector<int> edges[N]; int k; int c[K]; int dist[K][N]; void bfs(int u, int dist[]) { queue<int> q; for (int i = 0; i < n; ++i) dist[i] = -1; q.push(u); dist[u] = 0; while (q.size()) { int u = q.front(); q.pop(); for (int v : edges[u]) { if (dist[v] != -1) continue; dist[v] = dist[u] + 1; q.push(v); } } } int dp[K][1 << K]; int f(int x, int mask) { if ((mask + 1) == (1 << k)) return 0; if (~dp[x][mask]) return dp[x][mask]; int ret = INF; for (int i = 0; i < k; ++i) { if (mask >> i & 1) continue; if (dist[x][c[i]] == -1) continue; ret = min(ret, dist[x][c[i]] + f(i, mask | 1 << i)); } return dp[x][mask] = ret; } int solve() { scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { int u, v; scanf("%d %d", &u, &v); --u, --v; edges[u].push_back(v); edges[v].push_back(u); } scanf("%d", &k); for (int i = 0; i < k; ++i) { scanf("%d", &c[i]); --c[i]; bfs(c[i], dist[i]); } memset(dp, -1, sizeof dp); int ans = INF; for (int i = 0; i < k; ++i) { ans = min(ans, 1 + f(i, 1 << i)); } if (ans >= INF) ans = -1; printf("%d\n", ans); return 0; } int main() { int t = 1; // scanf("%d", &t); for (int tc = 0; tc < t; ++tc) { solve(); } return 0; }
#pragma GCC optimize(3,"Ofast","inline") #include<bits/stdc++.h> #define ll long long #define maxn 1000005 #define inf 1e9 #define pb push_back #define rep(i,a,b) for(int i=a;i<=b;i++) #define per(i,a,b) for(int i=a;i>=b;i--) using namespace std; inline ll read() { ll x=0,w=1; char c=getchar(); while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();} while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();} return w==1?x:-x; } ll n,m,k,c[maxn],d[25][25],dis[25][maxn],dp[25][maxn],vis[maxn],pos[maxn]; queue <int> q; vector <int> mp[maxn]; inline void wk(int nw,int s) { rep(i,1,n) vis[i]=0; vis[s]=1; dis[nw][s]=0; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(vis[v]) continue; dis[nw][v]=dis[nw][u]+1; if(pos[v]) d[nw][pos[v]]=dis[nw][v]; vis[v]=1; q.push(v); } } } int main() { n=read(); m=read(); rep(i,1,m) { int u=read(),v=read(); mp[u].pb(v); mp[v].pb(u); } rep(i,1,20) rep(j,1,n) dis[i][j]=inf; k=read(); rep(i,1,k) c[i]=read(),pos[c[i]]=i; rep(i,1,k) rep(j,1,k) if(i!=j) d[i][j]=inf; rep(i,1,k) wk(i,c[i]); int ed=(1<<k)-1; rep(i,0,k) rep(j,0,ed) dp[i][j]=inf; dp[0][0]=0; rep(i,1,k) d[0][k]=1,d[k][0]=1; rep(i,1,k) dp[i][1<<(i-1)]=1; rep(i,1,ed) { rep(j,1,k) { if(i&(1<<(j-1))) { rep(K,1,k) { if(!(i&(1<<(K-1)))) dp[K][i|(1<<(K-1))]=min(dp[K][i|(1<<(K-1))],dp[j][i]+d[j][K]); } } } } ll ans=inf; rep(i,1,k) ans=min(ans,dp[i][ed]); if(ans==inf) puts("-1"); else cout<<ans<<endl; return 0; }
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> #define ll long long using namespace std; int main(){ ll N; scanf("%lld",&N); vector<ll> a(N); for(ll i = 0; i < N; i++) scanf("%lld",&a.at(i)); sort(a.begin(),a.end()); for(ll i = 0; i < N - 1; i++){ if(a.at(i) == a.at(i + 1)){ a.erase(a.begin() + i); N--; i--; } } ll b = a.at(0); if(N == 1) printf("%lld\n",a.at(0)); else { ll c = a.at(1); vector<pair<ll,bool>> lcd; pair<ll,bool>p; for(ll i = 1; i < sqrt(a.at(0)); i++){ if(b % i == 0){ if(c % i == 0){ p.first = i; p.second = true; lcd.push_back(p); } if(c % (b / i) == 0){ p.first = b / i; p.second = true; lcd.push_back(p); } } } sort(lcd.begin(),lcd.end()); for(ll i = 0; i < lcd.size(); i++){ for(ll j = 0; j < N; j++){ if(a.at(j) % lcd.at(i).first){ lcd.at(i).second = false; break; } } } for(ll i = lcd.size() - 1; i > -1; i--){ if(lcd.at(i).second) { printf("%lld\n",lcd.at(i).first); break; } } } 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 N, std::vector<long long> a){ ll ans=a[0]; REP(N){ ans=gcd(ans,a[i]); } cout<<ans<<endl; } int main(){ cout<<setprecision(1000); long long N; scanf("%lld",&N); std::vector<long long> a(N); for(int i = 0 ; i < N ; i++){ scanf("%lld",&a[i]); } solve(N, std::move(a)); return 0; }
#include <cstdio> using namespace std; const int mod = 998244353; int a[50][50]; int par[100]; int cnt[100]; long long fact[51]; int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } int main() { int n, k, i, j, l; long long ans = 1; scanf("%d %d", &n, &k); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } for (i = 0; i < n * 2; i++) par[i] = i; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { for (l = 0; l < n; l++) { if (a[i][l] + a[j][l] > k) break; } if (l == n) unite(i, j); for (l = 0; l < n; l++) { if (a[l][i] + a[l][j] > k) break; } if (l == n) unite(n + i, n + j); } } for (i = 0; i < n * 2; i++) cnt[find(i)]++; fact[0] = 1; for (i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod; for (i = 0; i < n * 2; i++) ans = ans * fact[cnt[i]] % mod; printf("%lld\n", ans); return 0; }
#include <iostream> #include <vector> #include <string> #include <list> #include <queue> #include <algorithm> #define rep(i, n) for(i = 0; i < (n); i++) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define MOD 998244353 #define PI 3.14159265358979323846 #define INF 1 << 30 using namespace std; typedef long long ll; typedef pair<int, int> pp; class UnionFind { private: vector<int> p, n; public: UnionFind(int num) { p.resize(num + 1); n.resize(num + 1); fill(n.begin(), n.end(), 1); for (int i = 0; i < p.size(); i++) p[i] = i; } int Parent(int n) { if (p[n] == n) return n; return p[n] = Parent(p[n]); } void Union(int a, int b) { a = Parent(a); b = Parent(b); if (a == b) return; p[a] = b; n[b] += n[a]; return; } int Same(int a, int b) { return Parent(a) == Parent(b); } int Size(int a) { return n[Parent(a)]; } }; // d[n] = n! の配列初期化 void Ini_fact(vector<ll>& d) { d[1] = 1; for (ll i = 2; i < d.size(); i++) d[i] = d[i - 1] * i % MOD; return; } int main(void) { int num, k, i, j, s, t; ll ans = 1, a = 0, b = 0; cin >> num >> k; vector<ll> kai(num + 10); vector<vector<int>> d(num, vector<int>(num)); UnionFind x(num), y(num); Ini_fact(kai); rep(i, num) { rep(j, num) cin >> d[i][j]; } rep(s, num) { for (t = s + 1; t < num; t++) { int ok1 = 1, ok2 = 1; rep(i, num) { if (d[s][i] + d[t][i] > k) { ok1 = 0; } if (d[i][s] + d[i][t] > k) { ok2 = 0; } } if (ok1) x.Union(s, t); if (ok2) y.Union(s, t); } } rep(i, num) { if (i == x.Parent(i)) { ans *= kai[x.Size(i)]; } if (i == y.Parent(i)) { ans *= kai[y.Size(i)]; } ans %= MOD; } cout << ans << "\n"; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i]; #define ft first #define sc second #define pb push_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(),(v).end() #define LB(a,x) lb(all(a),x)-a.begin() #define UB(a,x) ub(all(a),x)-a.begin() #define mod 1000000007 //#define mod 998244353 #define FS fixed<<setprecision(15) using namespace std; typedef long long ll; const double pi=3.141592653589793; template<class T> using V=vector<T>; using P=pair<ll,ll>; typedef unsigned long long ull; typedef long double ldouble; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline void out(T a){ cout << a << '\n'; } void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;} //void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;} const ll INF=1e18; const int mx=200005; //abc192 int main(){ //オーバーフローは大丈夫ですか?? cin.tie(0);ios::sync_with_stdio(false); ll n,k; cin>>n>>k; ll ans=n; rep(i,k){ ll tmp=ans; string s=to_string(tmp); string t=s; string u=s; sort(all(t)); sort(all(u)); reverse(all(u)); ans=stoll(u)-stoll(t); } out(ans); } //ペナルティ出しても焦らない ACできると信じろ!!! //どうしてもわからないときはサンプルで実験 何か見えてくるかも //頭で考えてダメなら紙におこせ!!
// /Applications/Geany.app/Contents/MacOS/geany #include<bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define ppb pop_back #define mp make_pair #define pairii pair<int,int> #define ff first #define ss second #define all(x) x.begin(),x.end() const int NUM=1000030; const int N = 10000000; vector<int> lp, sieve; vector<int> pr; void primefactor(); int binpow(int a, int b); int binpow(int a, int b, int mod); int gcd(int a, int b); int lcm (int a, int b); bool comp(int a, int b); int inversemod(int a, int mod); void calc_sieve(); string getstring(int n) { string s; while(n>0) { s.pb('0'+n%10); n/=10; } sort(all(s)); return s; } int getnum(string s) { //reverse(all(s)); int ans=0; for(int i=0;i<s.size();i++) { ans=ans*10+s[i]-'0'; } return ans; } void test() { int n,k,i,j; cin>>n>>k; for(i=1;i<=k;i++) { string s=getstring(n); string s1=s; reverse(all(s1)); n=getnum(s1)-getnum(s); } cout<<n<<endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int t=1; //cin>>t; while(t--) test(); return 0; } void calc_sieve() //credits: Anish_Sofat { sieve.resize(NUM+1,0); for (int x = 2; x <= NUM; x++) { if (sieve[x]) continue; for (int u = x; u <= NUM; u += x) { sieve[u] = x ; } } } void primefactor() { lp.resize(N+1,0); for (int i=2; i<=N; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back (i); } for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; } } int binpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } int binpow(int a, int b, int mod) { int res = 1; while (b > 0) { if (b & 1) res = (res * a)%mod; a = (a * a)%mod; b >>= 1; } return res%mod; } int gcd(int a, int b) { if(b==0) return a; else return gcd(b,a%b); } int lcm (int a, int b) { return ((a / gcd(a, b)) * b); } bool comp(int a, int b) { return a>b; } int inversemod(int a, int mod) { return binpow(a,mod-2, mod); }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <cmath> #include <tuple> #include <cstdio> #include <bitset> #include <sstream> #include <iterator> #include <numeric> #include <map> #include <cstring> #include <set> #include <functional> #include <iomanip> using namespace std; #define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!! #ifdef DEBUG_ #define dump(x) cerr << #x << " = " << (x) << endl; #else #define dump(x) ; #endif #define equals(a,b) (fabs((a)-(b)) < EPS) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back //#define int long long typedef long long LL; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; 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 T> std::string printVector(const std::vector<T> &data) { std::stringstream ss; std::ostream_iterator<T> out_it(ss, ", "); ss << "["; std::copy(data.begin(), data.end() - 1, out_it); ss << data.back() << "]"; return ss.str(); } template <typename T> void print_array(const T &ary, int size){ REP(i,size){ cout << ary[i] << " "; } cout << endl; } const int mod = 1e9+7; const LL LINF = 1001002003004005006ll; const int INF = 1001001001; const double EPS = (1e-10); const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899; int dx[] = {0,0,-1,1}; int dy[] = {-1,1,0,0}; signed main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12); string enc = "BWR"; int N; cin >> N; string S; cin >> S; VL A(N); REP(i,N){ int a = -1; if(S[i] == 'B') a = 0; if(S[i] == 'W') a = 1; if(S[i] == 'R') a = 2; A[i] = a; } VL f(N+1); // f[x]: x!を3で何回割れるか VL g(N+1); // g[x]: g!を3で割り切れなくなるまで割ったとき、残った整数を3で割った余り f[0] = 0; g[0] = 1; FOR(i,1,N+1){ int x = i; int cnt = 0; while(x % 3 == 0){ x /= 3; cnt++; } f[i] = f[i-1] + cnt; } FOR(i,1,N+1){ int x = i; while(x % 3 == 0){ x /= 3; } g[i] = (g[i-1] * (x % 3) ) % 3; } LL ans = 0; REP(i,N){ LL nCi = -1; if(f[N-1] - f[i] - f[(N-1)-i] > 0){ nCi = 0; }else{ if(g[N-1] == 1 && g[i] * g[N-1-i] == 1) nCi = 1; if(g[N-1] == 1 && g[i] * g[N-1-i] == 2) nCi = 2; if(g[N-1] == 1 && g[i] * g[N-1-i] == 4) nCi = 1; if(g[N-1] == 2 && g[i] * g[N-1-i] == 1) nCi = 2; if(g[N-1] == 2 && g[i] * g[N-1-i] == 2) nCi = 1; if(g[N-1] == 2 && g[i] * g[N-1-i] == 4) nCi = 2; } ans += nCi * A[i]; ans %= 3; } if(N%2 == 0){ ans = (-ans+3) % 3; } cout << enc[ans] << endl; }
#include <bits/stdc++.h> using namespace std; const int f[3][3] = { {1, 0, 0}, {1, 1, 0}, {1, 2, 1} }; const int mod = 3; int lucas(int x, int y) { int res = 1; while (x) { res = res * f[x % mod][y % mod] % mod; x /= mod, y /= mod; } return res; } int mp[200]; void solve() { mp['B'] = 0, mp['W'] = 1, mp['R'] = 2; int n, res = 0; cin >> n; --n; for (int i = 0; i <= n; ++i) { char c; cin >> c; int x = mp[c]; res = (res + lucas(n, i) * x) % mod; } if (n & 1) res = -res; res = (res + mod) % mod; if (res == 0) putchar('B'); if (res == 1) putchar('W'); if (res == 2) putchar('R'); } int main() { solve(); return 0; }
/* _ _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||_ \ | | \\\ - /'| | | | \_| `\`---'// |_/ | \ .-\__ `-. -'__/-. / ___`. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' _> \"". | | : `- \`. ;`. _/; .'/ / .' ; | \ \ `-. \_\_`. _.'_/_/ -' _.' / ===========`-.`___`-.__\ \___ /__.-'_.'_.-'================ Please give me AC. */ #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <algorithm> #include <numeric> #include <string> #include <sstream> #include <complex> #include <bitset> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <deque> #include <stack> #include <unordered_map> #include <unordered_set> #include <utility> #include <chrono> #include <random> using namespace std; using int64 = long long; using uint64 = unsigned long long; using vi = vector<int>; using vl = vector<int64>; using pii = pair<int, int>; using pll = pair<int64, int64>; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) (v).begin(), (v).end() #define print(x) cout << (x) << '\n' #define print2(x, y) cout << (x) << ' ' << (y) << '\n' #define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n' #define printn(v) rep(i, (v).size() - 1) cout << (v)[i] << ' '; cout << (v)[n - 1] << '\n'; #ifdef ONLINE_JUDGE #define debug(x) #define debug2(x, y) #define debug3(x, y, z) #define dbg(v) #else #define debug(x) cerr << #x << ": " << (x) << '\n' #define debug2(x, y) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n' #define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n' #define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';} #endif // constant const int INF = (1<<30) - 1; const int64 INF64 = (1LL<<62) - 1; template<typename T> T gcd(T a, T b) { if (a < b) return gcd(b, a); T r; while ((r = a % b)) { a = b; b = r; } return b; } template<typename T> T lcm(const T a, const T b) { return a / gcd(a, b) * b; } template<typename T> bool chmin(T& a, const T& b) { if (a > b) return a = b, true; else return false; } template<typename T> bool chmax(T& a, const T& b) { if (a < b) return a = b, true; else return false; } // End of template. int main() { cout << fixed << setprecision(15); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n, k; cin >> n >> k; vector<int64> a(n, 0), m(n, 0); rep(i, n) { int a0; cin >> a0; a[a0] += 1; } m[0] = a[0]; rep(i, n - 1) m[i + 1] = min(m[i], a[i + 1]); int64 ans = 0; int c = 0; for (int i = n - 1; i >= 0 && k > 0;) { if (m[i] > c) { ans += i + 1; k--; c++; } else { i--; } } print(ans); return 0; } /* _ | |__ ___ _ __ _ __ | '_ \ / __| '_ \| '_ \ | |_) | (__| |_) | |_) | |_.__(_)___| .__/| .__/ |_| |_| */
#include<bits/stdc++.h> using namespace std; #define int long long int cnt[300005],k,n; signed main(){ cin>>n>>k; for(int i=1;i<=n;i++){ int x; cin>>x; cnt[x]++; } int ans=0; for(int i=0;i<n;i++){ if(cnt[i]<k){ ans+=i*(k-cnt[i]); k=cnt[i]; } } cout<<ans<<endl; }
//int a = s - '0'; 文字から数字 //小文字から大文字 //transform(a.begin(), a.end(), a.begin(), ::toupper); //map 全探索 //auto begin = p.begin(), end = p.end(); //for (auto it = begin; it != end; it++) {} //mapのキー:it->first mapのバリュー:it->second //大文字判定 isupper(文字) 小文字判定 islower(文字) //do{}while(next_permutation(ALL(配列))) //小文字に対応する文字コード:S[i] - 'a' //文字コード→小文字:(char)(数字+'a') //グラフの距離:隣接行列で扱う //bool型 初期値はTrue //島渡りの問題:中間ノードに着目 //数が大きい時の比較はstring型で行う //全て0になったか調べたい->0になるたびにcntする //例外処理は最初にする //x = p^m + q^n...の約数の個数:(n+1)*(m+1).... //N!のどの素因数で何回割れるか //⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和 //パズルの問題->一般化して全探索 //stack<ll> s; //s.push(要素);s.top();s.pop(); //queue<ll> q; //q.push(要素);q.front();q.pop(); //同じ作業繰り返す系の問題:収束先を見つける //過半数:N/2.0で判定 //優先度付きキュー //priority_queue< //ll, //vector<ll> //> q; #include <bits/stdc++.h> #define rep(i,N) for(int i = 0; i < N;i++) #define ALL(a) (a).begin(),(a).end() #define ll long long int #define PI 3.14159265358979323846264338327950L using namespace std; //割るやつ const ll MOD = (pow(10, 9) + 7); // K進数でのNの桁数 ll dig(ll N, ll K) { ll dig = 0; while (N) { dig++; N /= K; } return dig; } // a,bの最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); } //a,bの最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } //階乗計算 ll f(ll n) { if (n == 0 || n == 1) return 1; else return (n * f(n - 1)); } //Nのd桁目の数 ll dignum(ll N, ll d) { ll x = pow(10, d); N %= x; ll y = pow(10, d - 1); N /= y; return N; } //Nをdで何回割れるか ll divcnt(ll N, ll d) { ll ans = 0; while (1) { if (N%d == 0) { ans++; N /= d; } else break; } return ans; } //素数判定 bool prime(ll num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } //フィボナッチ数列 vector<ll> memo(pow(10, 6) + 1); ll fibo(ll n) { if (n == 1) return 1; else if (n == 2) return 1; else if (memo[n] != 0) return memo[n]; else return memo[n] = fibo(n - 1) + f(n - 2); } ll RS(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } return N * RS(N, P - 1, M); } vector<int> IntegerToVector(int bit, int N) { vector<int> S; for (int i = 0; i < N; ++i) { if (bit & (1 << i)) { S.push_back(i); } } return S; } int main() { ll N; cin >> N; vector<ll> A(N), P(N), X(N); rep(i,N) cin >> A[i] >> P[i] >> X[i]; ll ans = 1e10; int flag = 0; rep(i,N){ if(X[i] - A[i] >= 1){ ans = min(P[i],ans); flag = 1; } } if(flag == 1) cout << ans << endl; else cout << -1 << endl; }
#include <iostream> using namespace std; const int INF = 0x3fffffff; void chmin(int& a, int b){ if(a > b) a = b; } int main(){ int N; cin >> N; int ans = INF; for(int i = 0; i < N; i++){ int A, P, X; cin >> A >> P >> X; if(X > A) chmin(ans, P); } if(ans == INF) ans = -1; cout << ans << endl; }
#ifdef LOCAL #pragma warning(disable:4996) #pragma warning(disable:4244) #pragma warning(disable:4554) #define gets gets_s #endif #include<map> #include<set> #include<list> #include<cmath> #include<stack> #include<queue> #include<cmath> #include<vector> #include<string> #include<cctype> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<bitset> #include<assert.h> #include<unordered_map> #include<ctime> #define ll long long #define cl(a,x) memset((a),(x),sizeof(a)) #define lson 2*i #define rson 2*i+1 #define sf scanf #define ull unsigned long long #define lowbit(x) (x)&(-x) #define pii pair<int,int> #define pll pair<long long,long long> #define pdd pair<double,double> #define ti tree[i] #define mp make_pair #define debug cerr <<"flag" << endl; ll q_pow(ll x, ll n, ll mod) { ll ret = 1; while (n) { if (n & 1) ret = x * ret%mod; x = x * x%mod; n >>= 1; }return ret; } ll __gcd(ll x, ll y) { if (!y) return x; return __gcd(y, x%y); } ll getbit(ll now) { int cnt = 0; while (now) { cnt++; now -= lowbit(now); }return cnt; } int dx[] = { -1,1,0,0 };//上下左右 int dy[] = { 0,0,-1,1 };// using namespace std; const ll maxn = 2e5 + 10; const ll INF = 0x3f3f3f3f; const ll IINF = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-9; const int mod = 1e9 + 7; int dp[maxn]; char s[maxn]; void solve() { sf("%s", s + 1); int n = strlen(s + 1); int cnt = n; ll ans = 0; int last = -INF; for (int i = 1; i <= n; i++) { if (s[i] == s[i + 1] and s[i] != s[i + 2] and last != s[i]) { if (last != s[i]) ans += n - i, last = s[i]; } else if (last == s[i]) ans--; } printf("%lld\n", ans); } signed main(int argc, char *argv[]) { if (argc == 2 && strcmp("-debug", argv[1]) == 0) { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } int t = 1; ///cin >> t; while (t--) { solve(); } return 0; }
#include<bits/stdc++.h> #define int long long #define pii pair<int,int> #define pb push_back #define fastio ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); const int MOD = 1000000007; #define PI = acos(-1) using namespace std ; signed main(){ fastio ; int n ; cin >> n ; vector<int> v ; for(int i = 0 ; i < 61 ; i++){ v.pb(1LL << i); } //for(auto x : v) cout << x << endl; int ans = 1e18; for(auto x : v){ int b = x ; int c = n%b ; int a = (n - c)/b ; ans = min(ans , a + (int)log2(b) + c); } cout << ans ; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define ui unsigned int #define ull unsigned ll #define foi(n) for(ll i=0;i<n;i++) #define foj(n) for(ll j=0;j<n;j++) #define fok(n) for(ll k=0;k<n;k++) #pragma GCC optimize("O3", "unroll-loops") #define fixed(); cout << fixed << setprecision(20); #define fast(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); /*****************************************************************************/ #define graph vector<vector<int>> #define V vector typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef V<int> vi; typedef V<ll> vll; typedef V<string> vs; typedef V<double> vd; typedef V<pii> vpii; typedef V<pll> vpll; typedef pair<int,pii> piii; typedef priority_queue<pii, vector<pii>, greater<pii> > pqq; /*****************************************************************************/ #define mp make_pair #define eb emplace_back #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define hashmap unordered_map #define hashset unordered_set #define lb lower_bound #define ub upper_bound #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define ff first #define ss second #define lbpos(v,x) (int)(lower_bound(all(v),x)-v.begin())//=v.size()==>No LB #define ubpos(v,x) (int)(upper_bound(all(v),x)-v.begin())//=v.size()==>No UB /*****************************************************************************/ //for declaring most used global variables and constants ll mod=1e9+7; //for string with space use std::getline(cin,string_name); //*****************UVAISH ZAFRI(Rush_For_AC)********************** int32_t main() { fast(); fixed(); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif ll a[4]; bool is=false; foi(4) cin>>a[i]; for(ll i=0;i<=15;i++){ ll od=0,ev=0; foj(4) if(i&1<<j) od+=a[j]; else ev+=a[j]; if(ev==od) is=true; } if(is) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
#include <bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fod(i,a,b) for(int i=a;i>=b;i--) #define me0(a) memset(a,0,sizeof(a)) #define me1(a) memset(a,-1,sizeof(a)) #define op freopen("in.txt", "r", stdin) #define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin); #define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout) #define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout) #define pii pair<int,int> #define Please return #define AC 0 using namespace std; const int INF = 0x3f3f3f3f; typedef long long LL; template <class T> void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int mod=998244353; const int maxn = 1e6+10; int n,m,a[maxn],q,t; char s[maxn]; int main(){ read(t); while(t--){ read(n); fo(i,1,n) read(a[i]); if(n==1) { puts("Second"); } else if(n&1){ puts("Second"); } else{ unordered_map<int,int>mpp; fo(i,1,n) mpp[a[i]]++; int same = 1; for(auto to:mpp) if(to.second&1) same = 0; if(same) puts("Second"); else puts("First"); } } Please AC; }
#include<bits/stdc++.h> #define for0(i, n) for(int i = 0; i < (n); i++) #define for1(i, n) for(int i = 1; i <= (n);i++) #define puts(x) cout << (x) << "\n" using namespace std; int si, sj, t[60][60], p[60][60], M; bool b1[50 * 51]; int score(string s) { int xs = si, ys = sj, r = p[xs][ys]; for (char c : s) { if (c == 'U')xs--; if (c == 'D')xs++; if (c == 'L')ys--; if (c == 'R')ys++; r += p[xs][ys]; } return r; } signed main() { cin >> si >> sj; si++; sj++; b1[2525] = 1; for0(i, 52)for0(j, 52) { if (i * j == 0 || i == 51 || j == 51)t[i][j] = 2525; else { cin >> t[i][j]; M = max(M, t[i][j]); } } for1(i, 50)for1(j, 50)cin >> p[i][j]; string s1 = ""; int v1 = score(s1), ti = clock(); while(clock() - ti <= 1800000) { string sp = ""; int xi = si, yi = sj; while (1) { b1[t[xi][yi]] = 1; int tm = b1[t[xi - 1][yi]] + b1[t[xi + 1][yi]] + b1[t[xi][yi - 1]] + b1[t[xi][yi + 1]]; if (tm == 4)break; int ri = rand() % (4 - tm); if (b1[t[xi - 1][yi]] == 0) { if (ri == 0) { sp += 'U'; xi--; } ri--; } if (b1[t[xi + 1][yi]] == 0) { if (ri == 0) { sp += 'D'; xi++; } ri--; } if (b1[t[xi][yi - 1]] == 0) { if (ri == 0) { sp += 'L'; yi--; } ri--; } if (b1[t[xi][yi + 1]] == 0) { if (ri == 0) { sp += 'R'; yi++; } ri--; } } int vi = score(sp); if (vi > v1) { s1 = sp; v1 = vi; } for0(i, M)b1[i] = 0; } puts(s1); }
#include <bits/stdc++.h> using namespace std; template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } const int n = 50; const int vi[] = {0, 1, 0, -1}; const int vj[] = {1, 0, -1, 0}; chrono::steady_clock::time_point sTime; char vtoc(int v) { string s = "RDLU"; return s[v]; } void mainSolve(string &ans, int si, int sj, const vector<vector<int>> &t, const vector<vector<int>> &p) { mt19937 mt(04252021); uniform_int_distribution<int> rdV(0, 3); const int TL = 2 * 1000 - 25; auto passTime = [&]() { return chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - sTime).count(); }; auto passTimeNow = passTime(); int bestScore = 0; while (passTimeNow < TL) { passTimeNow = passTime(); string tans; int ni = si, nj = sj; set<int> visit = {t[ni][nj]}; int score = p[ni][nj]; for (int tt = 0; tt < 1000; tt++) { int v = rdV(mt); int ei = ni + vi[v], ej = nj + vj[v]; if (0 > ej || ej >= n || 0 > ei || ei >= n) continue; if (!visit.count(t[ei][ej])) { ni = ei, nj = ej; score += p[ei][ej]; tans += vtoc(v); visit.insert(t[ei][ej]); } } if (chmax(bestScore, score)) { ans = tans; } } } int main() { sTime = chrono::steady_clock::now(); int si, sj; cin >> si >> sj; vector<vector<int>> t(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> t[i][j]; } } vector<vector<int>> p(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; } } string ans; mainSolve(ans, si, sj, t, p); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> x(N); vector<int> y(N); for(int i=0;i<N;i++){ cin >> x.at(i); cin >> y.at(i); } int num = 0; float delx = 0; float dely = 0; for(int j=0;j<N;j++){ for(int k=j+1;k<N;k++){ dely = y.at(j) - y.at(k); delx = x.at(j) - x.at(k); if((dely / delx )>=-1){ if((dely / delx )<=1){ num ++; } } } } cout << num; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i)) #define ALL(x) ::std::begin(x), ::std::end(x) using namespace std; int64_t solve(int n, const std::vector<int64_t> &a) { int64_t ans = 0; int64_t b = 0; map<int64_t, int> c; c[0] += 1; REP (i, n) { b += (i % 2 == 0 ? a[i] : - a[i]); ans += (c.count(b) ? c[b] : 0); c[b] += 1; } return ans; } // generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int N; std::cin >> N; std::vector<int64_t> A(N); REP (i, N) { std::cin >> A[i]; } auto ans = solve(N, A); std::cout << ans << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; /**************************************************/ #define int long long #define fr(i,st,c) for(int i=st;i<c;i++) #define rfr(i,e,c) for(int i=e;i>=c;i--) #define i_output(f1) cout<<(f1?"YES":"NO")<<endl; #define vi vector<int> #define pb push_back #define print(n) cout<<n<<endl #define thor ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define N 1e9+7 int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } /***************************************************/ signed main(){ thor double a,b; cin>>a>>b; double ans=((a - b)/ a )*100; cout<<setprecision(20)<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 998244353; //10^9+7:合同式の法 int main(){ ll H, W, p, ans, i, j,last; cin >> H >> W; vector<string> S(H); for(string &s : S) { cin >> s; } ans=1;i=0;j=0;last=0; while(true){ p=-1; ll now=i;j=0; while(now>=0){ //cout<<now<<" "<<j<<endl; if(now>=H||j>=W){j++;now--;continue;} if(now==H-1&&j==W-1){last=-1;} if(p==-1){ if(S[now][j]=='R'){p=1;} else if(S[now][j]=='B'){p=2;} }else{ if(S[now][j]=='R'&&p==2){p=0;} else if(S[now][j]=='B'&&p==1){p=0;} } if(last==-1){break;} j++;now--; } if(p==-1){ ans*=2; ans=ans%MOD; }else if(p==0){ ans=0;break; } if(last==-1){break;} //cout<<i<<" "<<ans<<endl; i++; } cout<<ans<<endl; }
#include<iostream> using namespace std; int main(void){ int N; cin>>N; int A[N]; for(int i=0;i<N;i++){ cin>>A[i]; } int k,gcd=0; for(int i=2;i<=1000;i++){ int gcd2=0; for(int j=0;j<N;j++){ if(A[j]%i==0){ gcd2++; } } if(gcd2>gcd){ gcd=gcd2; k=i; } } cout<<k<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (n); ++(i)) typedef long long ll; const int INF = 1001001001; int main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) { cin >> a[i]; } vector<ll> gcdd(1000); rep(i, n) { rep(j, 1000) { if (a[i] % (j + 1) == 0 && (j + 1) <= a[i]) gcdd[j]++; } } gcdd[0] = 0; ll maxid = 0; ll maxval = 0; rep(i, 1000) { maxval = max(maxval, gcdd[i]); if (maxval == gcdd[i]) maxid = i + 1; } cout << maxid; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; #define FOR(i, n) for(int (i)=0; (i)<(n); (i)++) #define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--) template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);} template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);} template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); } template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); } int n; vector<ll> a, b; map<pair<ll, int>, ll> H; ll cc(ll x, ll q){ if (x>=0) return (x+q-1)/q; else return x/q; } ll ff(ll x, ll q){ if (x>=0) return x/q; else return (x-q+1)/q; } ll solve(ll x, int i){ auto key = make_pair(x, i); if (H.count(key)) return H[key]; if (abs(x) > b[i]) return 0; if (i==0) return abs(x) <= b[0]; ll ret = 0; ret += solve(x, i-1); //-b[i-1] <= x + q * a[i] <= b[i-1] ll q1 = cc(-b[i - 1] - x, a[i]); ll q2 = ff(b[i - 1] - x, a[i]); ll lim = 1e18; if (i+1 < n) lim = a[i+1] / a[i]; if (q1 == q2 && abs(q1) < lim && q1) ret += solve(x + q1 * a[i], i - 1); else if (q1 < q2) { if (q1 && abs(q1) < lim) ret += solve(x + q1 * a[i], i - 1); if (q2 && abs(q2) < lim) ret += solve(x + q2 * a[i], i - 1); } // cout << x << " " << i << " " << ret << endl; return H[key] = ret; } int main(int argc, char** argv) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15); if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin); ll x; cin >> n >> x; a.resize(n); b.resize(n); FOR(i, n) cin >> a[i]; FOR(i, n-1) b[i] = a[i+1]-1; b[n-1] = 1e18; cout << solve(x, n-1) << endl; if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n"; return 0; }
#include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef WA_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif using ll = long long; using ull = unsigned long long; #define pb push_back #define fi first #define se second #define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++) #define per(i,a,b) for(int i=int(a);i>=(int)(b);i--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int mod = 1e9+7; const int inf = 0x3f3f3f3f; const int maxn = 1e5+10; struct edge { int to; ll c,d; }; int main() { #ifndef WA_DEBUG ios::sync_with_stdio(false);cin.tie(nullptr); #endif int n,m; cin>>n>>m; vector<vector<edge>> g(n+1); rep(i,1,m) { int a,b; ll c,d; cin>>a>>b>>c>>d; g[a].pb({b,c,d}); g[b].pb({a,c,d}); } auto gett=[&](ll x) { if(x==0) return 1ll; ll tmp=sqrt(x); while(tmp*(tmp+1)<=x) tmp++; return tmp; }; vector<ll> dis(n+1,ll(1e18)); dis[1]=0; priority_queue<pair<ll,int>> q; q.push({0,1}); while(!q.empty()) { auto now=q.top();q.pop(); now.fi=-now.fi; if(now.fi!=dis[now.se]) continue; for(auto x:g[now.se]) { ll t=gett(x.d),val; if(now.fi+1<t) val=t-1+x.c+x.d/t; else val=now.fi+x.c+x.d/(now.fi+1); if(dis[x.to]>val) { dis[x.to]=val; q.push({-val,x.to}); } } } if(dis[n]>9e17) cout<<-1<<'\n'; else cout<<dis[n]<<'\n'; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; int D = abs(r1-r2) + abs(c1-c2); int64_t Ap = r1 + c1; int64_t Am = r1 - c1; int64_t Bp = r2 + c2; int64_t Bm = r2 - c2; //0回 if(r1 == r2 && c1 == c2){ cout << 0 << endl; return 0; } //1回? if(Ap == Bp || Am == Bm){ cout << 1 << endl; return 0; } if(D <= 3){ cout << 1 << endl; return 0; } //2回? for(int i=-3; i<=3; i++){ for(int j=-3; j<=3; j++){ if(abs(i) + abs(j) > 3){ continue; } if(Ap == Bp + i + j || Am == Bm + i + j){ cout << 2 << endl; return 0; } } } if(Ap %2 == Bp %2){ cout << 2 << endl; return 0; } if(D <= 6){ cout << 2 << endl; return 0; } //3回 cout << 3 << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) using namespace std; using ll = long long; const int INF = 1e9; const ll LINF = 1e18; template <class T> void get_unique(vector<T>& x) { x.erase(unique(x.begin(), x.end()), x.end()); } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < sz(v) - 1) os << ' '; } return os; } int main() { int n; ll w; cin >> n >> w; vector<ll> v(200200); rep(i, n) { int s, t; ll p; cin >> s >> t >> p; v[s] += p; v[t] -= p; } rep(i, 200200 - 1) v[i + 1] += v[i]; if (*max_element(all(v)) <= w) cout << "Yes\n"; else cout << "No\n"; }
#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(){ vl a(4); rep0(i, 4)cin >> a[i]; sort(all(a)); if(a[0] + a[1] + a[2] == a[3] || a[0] + a[3] == a[1] + a[2])yes; else no; return 0; }
#include<bits/stdc++.h> using namespace std; #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ll long long int #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define vii vector<pii> #define pb push_back #define B begin() #define E end() #define F first #define S second #define loop(z, v) for(auto &z: v) #define nl "\n" #define fat 1000000007 int main() { boost int n, k, m; cin >> n >> k >> m; vi a(n - 1); loop(z, a) cin >> z; ll sum = accumulate(a.B, a.E, 0); int ans = m*n - sum; if(ans < 0) cout << 0 << nl; else if(ans <= k) cout << ans << nl; else cout << -1 << nl; return 0; }
#include<iostream> #include<iomanip> #include<cstdio> #include<cstring> #include<cmath> #include<cinttypes> #include<vector> #include<algorithm> #include<map> #include<set> #include<queue> #include<string> #include<stack> #define FSCNd64 "%" SCNd64 #define FPRId64 "%" PRId64 using namespace std; using ll=long long; using vi=vector<int>; using vvi=vector<vi>; using pii=pair<int,int>; using vll=vector<ll>; using vvll=vector<vll>; using vpii=vector<pii>; #define PI 3.1415926535897932384626433832795 template<typename X> bool max_u(X&m, X v) { if(m<v) { m=v; return true; } return false; } template<typename X> bool min_u(X&m, X v) { if(m>v) { m=v; return true; } return false; } struct solve { solve(){} ll operator()() { return 0; } }; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); for(;;) { int n; cin >> n; if(cin.fail()) break; vi a(n); for(auto&m:a) cin >> m; for(;;) { n=a.size(); sort(a.begin(), a.end()); int a_min=a[0]; int a_max=a.back(); if(a_min==a_max) break; vi t; t.push_back(a[0]); for(int i=1;i<n;i++) { if(a[i]!=a[i-1]) { if(a[i]%a_min) t.push_back(a[i]%a_min); } } /* for(auto&m:t) cout << m << " "; cout << "\n"; */ a.swap(t); } cout << a[0] << "\n"; } return 0; }
#include<bits/stdc++.h> #define For(i,x,y) for (register int i=(x);i<=(y);i++) #define FOR(i,x,y) for (register int i=(x);i<(y);i++) #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) #define Debug(v) for (auto i:v) printf("%lld ",i);puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define fil(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pa; typedef pair<ll,ll> PA; typedef vector<int> poly; inline ll read(){ ll x=0,f=1;char c=getchar(); while ((c<'0'||c>'9')&&(c!='-')) c=getchar(); if (c=='-') f=-1,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } int n,a[100010]; int main(){ n=read(); For(i,1,n) a[i]=read(); int ans=a[1]; For(i,2,n) ans=__gcd(a[i],ans); printf("%d\n",ans); }
/* � Ashiq Uddin Pranto Department of Computer Science and Engineering BATCH 27 University Of Rajshahi,Bangladesh */ #include<bits/stdc++.h> #define ll long long #define ld long double #define FOR(x,y) for(ll i=x;i<=y;i++) #define pb(x) push_back(x) #define mp make_pair #define pii pair<int,int> #define pll pair<ll,ll> #define vii vector<int> #define vll vector<ll> #define matrix(x) vector<vector<x>> #define vss vector<string> #define PI (2*acos(0.0)) #define sqr(x) ((x)*(x)) #define sf(a) scanf("%d",&a) #define sfl(a) scanf("%lld",&a) #define sff(a,b) scanf("%d %d",&a,&b) #define sffl(a,b) scanf("%lld %lld",&a,&b) #define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c) #define sfffl(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define ms(a,b) memset(a, b, sizeof(a)) #define ff first #define ss second #define lcm(a, b) ((a)*((b)/__gcd(a,b))) #define all(a) a.begin(),a.end() #define readfile freopen("input.txt","r",stdin); #define writefile freopen("output.txt","w",stdout); #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); #define gap " " #define mx 104 #define inf (ll)1e9 #define WHITE 1 #define GRAY 2 #define BLACK 3 #define EMPTY_VALUE -1 #define mod 1000000007 #define MOD(a,b) (a%b + b)%b using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif void eff() { ll a,b,x,y; sffl(a,b); sffl(x,y); ll ans = 0; if(a==b) { ans = x; printf("%lld\n",ans); return ; } ans = inf; ll temp; temp = abs(a-b)*y; temp+=x; ans = min(ans,temp); if(a>b) { temp = abs(a-b)*2*x; temp -= x; ans = min(ans,temp); temp = abs(a-b)*2*x; temp-=x*2; temp+=y; ans = min(ans,temp); temp = abs(a-b)*y; temp+=x; ans = min(ans,temp); temp = (abs(a-b)-1)*y; temp+=x; ans = min(ans,temp); } else { temp = abs(a-b)*2*x; temp+=x; ans = min(ans,temp); temp = abs(a-b)*y; temp+=x; ans = min(ans,temp); } // if((x*2)<y){ // if(a>b) // { // ans = abs(a-b)*2*x; // ans-=x; // } // else // { // ans = abs(a-b)*2*x; // ans+=x; // } // } // else // { // ans = abs(a-b)*y; // ans+=x; // } printf("%lld\n",ans); return ; } int main() { //fastio // ll test ; // sfl(test); // for(ll i=0;i<test;i++) // { // printf("Case %lld: ",i+1); eff(); // } // cout<<check(81); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } #define DEBUG #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif struct Setup { Setup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __Setup; using ll = long long; #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define repl(i, a, b) for(int i = a; i < int(b); i++) #define rep(i, n) repl(i, 0, n) const int INF = 1 << 30; const ll LLINF = 1LL << 60; constexpr int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; //------------------------------------- int main() { int a, b, c; cin >> a >> b >> c; int turn = c; while(1) { if(turn == 0) { if(a > 0) { a--; } else { cout << "Aoki\n"; return 0; } } else { if(b > 0) { b--; } else { cout << "Takahashi\n"; return 0; } } turn = 1 - turn; } }
#include <stdio.h> #include <limits.h> #include <math.h> #include <iostream> #include <algorithm> using namespace std; //check if points are collinear or not bool check_collinear(int x1, int y1, int x2, int y2, int x3, int y3){ int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if (a == 0) return true; else return false; } bool solve(string n, int l) { // Less than three digit number // can be checked directly. if (l < 3) { if (stoi(n) % 8 == 0) return true; // check for the reverse of a number reverse(n.begin(), n.end()); if (stoi(n) % 8 == 0) return true; return false; } // Stores the Frequency of characters in the n. int hash[10] = { 0 }; for (int i = 0; i < l; i++) hash[n[i] - '0']++; for (int i = 104; i < 1000; i += 8) { int dup = i; int freq[10] = { 0 }; freq[dup % 10]++; dup = dup / 10; freq[dup % 10]++; dup = dup / 10; freq[dup % 10]++; dup = i; if (freq[dup % 10] > hash[dup % 10]) continue; dup = dup / 10; if (freq[dup % 10] > hash[dup % 10]) continue; dup = dup / 10; if (freq[dup % 10] > hash[dup % 10]) continue; return true; } // when all are checked its not possible return false; } int main(){ string number; getline(cin, number); int l = number.length(); if (solve(number, l)) cout << "Yes"; else cout << "No"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); string S; cin >> S; string ans = "No"; vector<ll> d(10, 0); for (ll i = 0; i < S.size(); i++) { d[S[i] - '0']++; } if (S.size() == 1) { if (S[0] == '8') ans = "Yes"; } else if (S.size() == 2) { if (S == "16") ans = "Yes"; if (S == "24") ans = "Yes"; if (S == "32") ans = "Yes"; if (S == "48") ans = "Yes"; if (S == "56") ans = "Yes"; if (S == "64") ans = "Yes"; if (S == "72") ans = "Yes"; if (S == "88") ans = "Yes"; if (S == "96") ans = "Yes"; if (S == "61") ans = "Yes"; if (S == "42") ans = "Yes"; if (S == "23") ans = "Yes"; if (S == "84") ans = "Yes"; if (S == "65") ans = "Yes"; if (S == "46") ans = "Yes"; if (S == "27") ans = "Yes"; if (S == "69") ans = "Yes"; } else { for (ll x = 1; x < 125; x++) { ll eight = 8 * x; vector<ll> d2(10, 0); for (ll i = 0; i < 3; i++) { d2[eight % 10]++; eight /= 10; } bool ok = true; for (ll i = 0; i < 10; i++) { if (d[i] < d2[i]) ok = false; } if (ok) { ans = "Yes"; } } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 long long pows(long long a, long long b) { if (a == 0) return 0; long long left = b; long long ks = a; long long ans = 1; while (left > 0) { if (left%2 == 1) { ans = ans*ks%MOD; } ks = ks*ks%MOD; left /= 2; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n,p; cin >> n >> p; long long ans = p-1; ans *= pows(p-2,n-1); ans %= MOD; if (n == 1 && p == 2) ans = 1; cout << ans << endl; }
#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 ⊃ ---------------------------------------------------------------------------------------------------*/ string X; ll M; //--------------------------------------------------------------------------------------------------- bool check(ll n) { vector<ll> Y; ll m = M; while (0 < m) { Y.push_back(m % n); m /= n; } reverse(all(Y)); if (X.size() < Y.size()) return true; else if (X.size() > Y.size()) return false; // the same size. int len = X.size(); rep(i, 0, len) { ll x = X[i] - '0'; ll y = Y[i]; if (x == y) continue; else if (x < y) return true; else return false; } // X == Y return true; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> X >> M; if (X.length() == 1) { ll x = stoll(X); cout << (x <= M ? "1" : "0") << endl; return; } int ma = -1; fore(c, X) chmax(ma, c - '0'); ll ok = ma, ng = infl; while (ok + 1 < ng) { ll md = (ok + ng) / 2; if (check(md)) ok = md; else ng = md; } ll ans = ok - ma; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < n; i++) int main() { ll n; cin >> n; ll cnt = 0; unordered_set<ll> d={}; n += n; for (ll i = 1; i <= sqrt(n); i++) { if (n%i==0){ if (i%2!=(n/i)%2){ cnt++; } } } cout << 2*cnt << endl; }
/*      />  フ      |  _  _|      /`ミ _x 彡      /      |     /  ヽ   ?  / ̄|   | | |  | ( ̄ヽ__ヽ_)_)  \二つ */ #pragma GCC optimize("Ofast","inline","-ffast-math") #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<bits/stdc++.h> #define int long long #define pb push_back #define pf push_front #define F first #define S second #define SS stringstream #define sqr(x) ((x)*(x)) #define m0(x) memset(x,0,sizeof(x)) #define m1(x) memset(x,63,sizeof(x)) #define CC(x) cout << (x) << endl #define AL(x) x.begin(),x.end() #define pw(x) (1ull<<(x)) #define NMSL cout << "NMSL" << endl; #define debug(x) cout << #x << ": " << x << endl; #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; #define fio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define in128 __int128_t using namespace std; const int N = 2e5+10; const int INF = numeric_limits<int>::max() / 4; const double EPS = 1e-3; const long double PI = 3.14159265358979323846; signed main() { fio int n; cin>>n; int ans=0; int base=0,a=0; while(1) { int nx=base*1000+999; if(n>=nx) { ans+=(nx-base)*a; } else { ans+=a*(n-base); break; }a++;base=nx; }cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; // -------------------------------------------------------- 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; } #define FOR(i,l,r) for (ll i = (l); i < (r); ++i) #define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define SUMLL(c) accumulate(ALL(c), 0LL) #define COUNT(c,v) count(ALL(c),(v)) #define SZ(c) ((ll)(c).size()) #define BIT(b,i) (((b)>>(i)) & 1) #define PCNT(b) __builtin_popcountll(b) #define CLZ(b) __builtin_clzll(b) #define BITLEN(b) (64LL - CLZ(b)) #define CIN(c) cin >> (c) #define COUT(c) cout << (c) << '\n' #define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n' ll llceil(ll a, ll b) { return (a + b - 1) / b; } string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; } string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; } using P = pair<ll,ll>; using VP = vector<P>; using VVP = vector<VP>; using VS = vector<string>; using VVS = vector<VS>; using VLL = vector<ll>; using VVLL = vector<VLL>; using VVVLL = vector<VVLL>; using VB = vector<bool>; using VVB = vector<VB>; using VVVB = vector<VVB>; using VD = vector<double>; using VVD = vector<VD>; using VVVD = vector<VVD>; using VLD = vector<ld>; using VVLD = vector<VLD>; using VVVLD = vector<VVLD>; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const ll MOD = 1000000007; // static const ll MOD = 998244353; static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1 // -------------------------------------------------------- // #include <atcoder/all> // using namespace atcoder; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); ll N, M; cin >> N >> M; VLL X(M),Y(M),Z(M); REP(i,M) cin >> X[i] >> Y[i] >> Z[i]; VVLL E(N+1); REP(i,M) { E[X[i]].push_back(i); } VLL dp(1<<N, 0); dp[0] = 1; REP(S,1<<N) { ll p = PCNT(S); bool ok = true; for (ll id : E[p]) { ll cnt = 0; REP(i,N) if (BIT(S,i)) { if (i+1 <= Y[id]) cnt++; } if (cnt > Z[id]) ok = false; } if (!ok) continue; REP(u,N) if (BIT(S,u)) { dp[S] += dp[S ^ (1<<u)]; } } ll ans = dp[(1<<N)-1]; COUT(ans); return 0; }
/** * @FileName a.cpp * @Author kanpurin * @Created 2021.04.29 06:43:59 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; int main() { int n,m;cin >> n >> m; vector<vector<pair<int,int>>> a(n+1); for (int i = 0; i < m; i++) { int x,y,z;cin >> x >> y >> z; a[y].push_back({x,z}); } vector<ll> dp(1<<n); dp[0]=1; for (int i = 1; i < 1<<n; i++) { bool ok = true; for(auto [x,z] : a[__builtin_popcount(i)]) { if (__builtin_popcount((1<<x)-1&i) > z) { ok=false; break; } } if (!ok) { dp[i]=0; continue; } for (int j = 0; j < n; j++) if (i&1<<j) dp[i] += dp[i^(1<<j)]; } cout << dp[(1<<n)-1] << endl; return 0; }
#include <iostream> using namespace std; int main() { string x,z = ""; cin>>x; for(int i = 0; i < x.length(); i++) { if(x[i] == '.') break; z += x[i]; } cout<<z; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define re register int #define inf 0x7fffffffffffffff inline int read() { int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} return f==1?x:-x; } signed main() { while(1) { char x=getchar(); if(x=='.' || x=='\n')break; printf("%c",x); } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<string> #include<stdio.h> #include<stdlib.h> #include<float.h> #include<tuple> #include<string.h> #include<iomanip> #include<stack> #include<queue> #include<map> #include<deque> #include<math.h> using namespace std; #define ll long long #define rep(i,n) for(ll i=0;i<n;i++) #define REP(i,n) for(ll i=1;i<=n;i++) #define ALLOF(c) (c).begin(), (c).end() #define Pa pair<ll,ll> const ll mod=1000000007; const ll modq=998244353; const ll INF=1e12; const ll inf=-1; ll ABS(ll a){return max(a,-a);} ll modpow(ll a,ll n) { ll res = 1; while (n>0) { if(n&1) res=res*a%mod; a=a*a%mod; n>>=1; } return res; } // a^{-1} mod を計算する ll modinv(ll a) { return modpow(a, mod - 2); } ll nCk(ll n,ll k){ ll nck=1; rep(i,k){ nck=(nck*(n-i))%mod; nck=(nck*modinv(k-i))%mod; } return nck; } int main(void){ ll N,M,Asum=0; cin>>N>>M; rep(i,N){ ll A; cin>>A; Asum+=A; } if(M<Asum){ cout<<0<<endl; return 0; } cout<<nCk(M+N,N+Asum)<<endl; return 0; }
#include <iostream> #include <cmath> #include <stdio.h> #include <string.h> #include <bits/stdc++.h> #include <vector> #include <array> #include <tuple> #include <algorithm> using namespace std; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { long long N, M, S, x, P, C; cin >> N >> M; S=0; for (int i=0; i<N; i++){ cin >> x; S=S+x; } P=1; C=1000000007; for (int i=0; i<S+N; i++){ P=(P*(M+N-i))%C; P=(P*modinv(S+N-i, C))%C; } cout << P << endl; }
#include <bits/stdc++.h> using namespace std; #define F(i,l,r) for(int i=l;i<=r;i++) #define ll long long char p[10000]; int res=0; int main() { int m,t,n; cin >> n >> m >> t; int vt=0; int cnt=n; F(i,1,m) { int x,y; cin >> x >> y; cnt-=(x-vt); if(cnt<=0) break; cnt=min(n,cnt+(y-x)); vt=y; } cnt-=(t-vt); // cout << n << endl; if(cnt>0) cout << "Yes"; else cout << "No"; return 0; }
#include <bits/stdc++.h> #define N 200005 #define M 400005 #define inf 1e18 #define mp make_pair #define int long long using namespace std; int n,m; int head[N],v[M],C[M],D[M],nxt[M],tot; priority_queue<pair<int,int> > q; int dis[N]; bool vis[N]; int read() { int x=0,f=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9') { x=(x<<1)+(x<<3)+(c^48); c=getchar(); } return x*f; } inline void add(int x,int y,int c,int d) { tot++; v[tot]=y; C[tot]=c; D[tot]=d; nxt[tot]=head[x]; head[x]=tot; } inline int Dij(int s) { for(int i=2;i<=n;i++) dis[i]=inf; dis[s]=0; q.push(mp(0,s)); while(!q.empty()) { int x=q.top().second,s=q.top().first;q.pop(); if(x==n) return -s; if(vis[x]) continue; vis[x]=1; for(int i=head[x];i;i=nxt[i]) { if(dis[v[i]]>((dis[x]>floor(sqrt(D[i]+1))-1)?dis[x]+C[i]+(D[i]/(dis[x]+1)):floor(sqrt(D[i]+1))-1+C[i]+(D[i]/(floor(sqrt(D[i]+1)))))) { dis[v[i]]=((dis[x]>floor(sqrt(D[i]+1))-1)?dis[x]+C[i]+(D[i]/(dis[x]+1)):floor(sqrt(D[i]+1))-1+C[i]+(D[i]/(floor(sqrt(D[i]+1))))); q.push(mp(-dis[v[i]],v[i])); } } } return -1; } signed main() { n=read(); m=read(); int a,b,c,d; for(int i=1;i<=m;i++) { a=read(); b=read(); c=read(); d=read(); //if(a==b) continue; add(a,b,c,d); add(b,a,c,d); } printf("%lld\n",Dij(1)); return 0; }
#include<bits/stdc++.h> using namespace std; char c2,c1,c3; int main() { c1=getchar(),c2=getchar(),c3=getchar(); if (c1==c2&&c2==c3) printf("Won\n"); else printf("Lost\n"); return 0; }
#include<bits/stdc++.h> using namespace std; #define MAX 101 int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string slots; cin>>slots; if(slots[0]==slots[1]&&slots[1]==slots[2]) cout<<"Won"; else cout<<"Lost"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for (int i = 0; i < (n); ++i) int main(){ string s; std::cin >> s; std::map<int,ll> ma; rep(i,s.size()){ int num=(int)(s[i]-'0'); ma[num]++; } if(s.size()==1){ if(s[0]=='8')std::cout << "Yes" << '\n'; else std::cout << "No" << '\n'; return 0; } if(s.size()==2){ int si = std::stoi(s); int s2i = ((int)(s[1]-'0'))*10+(int)(s[0]-'0'); // std::cout << s2i << '\n'; if(si%8==0 || s2i%8==0)std::cout << "Yes" << '\n'; else std::cout << "No" << '\n'; return 0; } for (int i = 1; i < 10; i++) { for (int k = 1; k < 10; k++) { for (int l = 1; l < 10; l++) { if((i*100+k*10+l)%8!=0)continue; std::map<int,ll> ma2=ma; if(ma2[i]>0)ma2[i]--; else continue; if(ma2[k]>0)ma2[k]--; else continue; if(ma2[l]>0){ std::cout << "Yes" << '\n'; return 0; } } } } std::cout << "No" << '\n'; return 0; }
#include <iostream> using namespace std; typedef long long ll; ll num,b[200],sum=0; int main() { ll n; cin>>n; for(int i=1;i<=n;i++) { cin>>num; b[num%200]++; } for(int i=0;i<200;i++) { if(b[i]>=2) { sum+=b[i]*(b[i]-1)/2; } } cout<<sum<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; int main() { int A,B,C; cin >> A >> B >> C; if(A + C <= B){ cout << "Aoki" << endl; }else{ cout << "Takahashi" << endl; } return(0); }
#include <bits/stdc++.h> using namespace std; int main(){ int A, B, C; cin >> A >> B >> C; if ( A > B ) cout << "Takahashi" << endl; if ( A < B ) cout << "Aoki" << endl; if ( A == B && C == 0 ) cout << "Aoki" << endl; if ( A == B && C == 1 ) cout << "Takahashi" << endl; }
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <queue> #include <math.h> #include <tuple> #include <set> #include <unordered_map> #include <stack> #include <map> #include <cstdlib> using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define all(x) (x).begin(),(x).end() using ll = long long; template<class T>bool chmaxj(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void YesNo(bool y) { cout << (y ? "Yes" : "No") << endl; } //true==yes #define INF 21474836 ////////////////////////////////////////////////////////////// int H, W; int main() { int Sx, Sy; int Gx, Gy; cin >> H >> W; vector<vector<char>> a(H, vector<char>(W, 0)); vector<vector<int>> max(H, vector<int>(W, INF)); vector<vector<pair<int, int>>> alpha(30,vector<pair<int,int>>(1,pair<int,int>(-1,-1))); bool checked[30] = { false }; for (int y = 0; y < H; y++) { string s; cin >> s; for (int x = 0; x < W; x++) { a[y][x] = s[x]; if (a[y][x] == 'S') { Sx = x; Sy = y; } if (a[y][x] == 'G') { Gx = x; Gy = y; } if ('a' <= a[y][x] && a[y][x] <= 'z') { if (alpha[a[y][x] - 'a'][0].first == -1) { alpha[a[y][x] - 'a'][0].first = x; alpha[a[y][x] - 'a'][0].second = y; } else { alpha[a[y][x] - 'a'].push_back(pair<int,int>(x,y)); } } } } max[Sy][Sx] = 0; queue<pair<int,int>> que; que.push(pair<int, int>(Sy,Sx)); while (!que.empty()) { pair<int, int> p = que.front(); que.pop(); int x = p.second; int y = p.first; if (0 < x) { if (a[y][x - 1] != '#') { if (max[y][x - 1] > max[y][x] + 1) { max[y][x - 1] = max[y][x] + 1; que.push(pair<int, int>(y, x - 1)); } } } if (x < W - 1) { if (a[y][x + 1] != '#') { if (max[y][x + 1] > max[y][x] + 1) { max[y][x + 1] = max[y][x] + 1; que.push(pair<int, int>(y, x + 1)); } } } if (0 < y) { if (a[y - 1][x] != '#') { if (max[y - 1][x] > max[y][x] + 1) { max[y - 1][x] = max[y][x] + 1; que.push(pair<int, int>(y - 1, x)); } } } if (y < H - 1) { if (a[y + 1][x] != '#') { if (max[y + 1][x] > max[y][x] + 1) { max[y + 1][x] = max[y][x] + 1; que.push(pair<int, int>(y + 1, x)); } } } if ('a' <= a[y][x] && a[y][x] <= 'z') { int ss = a[y][x] - 'a'; if (checked[ss] == false) { for (int i = 0; i < alpha[ss].size(); i++) { int alx = alpha[ss][i].first; int aly = alpha[ss][i].second; if (max[aly][alx] > max[y][x] + 1) { max[aly][alx] = max[y][x] + 1; que.push(pair<int, int>(aly, alx)); } } } checked[ss] = true; } } if (max[Gy][Gx] == INF) { cout << -1; return 0 ; } cout << max[Gy][Gx]; return 0; }
#include <cstdio> #include <iostream> #include <vector> #include <cstring> using namespace std; # define rep(i,a,b) for(int i=(a); i<=(b); ++i) inline int readint(){ int a = 0; char c = getchar(), f = 1; for(; c<'0'||c>'9'; c=getchar()) if(c == '-') f = -f; for(; '0'<=c&&c<='9'; c=getchar()) a = (a<<3)+(a<<1)+(c^48); return a*f; } const int MaxN = 1000005; char s[MaxN<<1], cmd[MaxN]; char res[MaxN<<1]; int main(){ int L = MaxN+1, R = L-1; scanf("%s",cmd); int n = strlen(cmd), op = 1; for(int i=0; i<n; ++i) if(cmd[i] == 'R') op ^= 1; else if(op) s[++ R] = cmd[i]; else s[-- L] = cmd[i]; if(!op) rep(i,L,L+R-i) swap(s[i],s[L+R-i]); int lst = L-1; // last one res[L-1] = '#'; for(int i=L; i<=R; ++i) if(lst >= L && s[i] == res[lst]) -- lst; else res[++ lst] = s[i]; res[lst+1] = '?'; rep(i,L,lst) if(i < lst && res[i] == res[i+1]) ++ i; // skip the two else putchar(res[i]); return 0; }
#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cstdio> #include<stdio.h> #include<cmath> #include<math.h> #include<vector> #include<set> #include<queue> #include<map> #include<sstream> #include<iomanip> #define forn(i,n) for(int (i)=0;i<(n);i++) #define pb push_back #define mp make_pair using namespace std; typedef pair<int,int>pii; typedef long long ll; typedef pair<ll,ll> pll; const int MAXN=100005; const int INF=2147483647; const ll LINF=9223372036854775807; const int dx[]={1,-1,0,0},dy[]={0,0,1,-1}; int n,ans; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n; while(n--) { int x; cin>>x; ans+=max(0,x-10); } cout<<ans; return 0; }
#include <iostream> using namespace std; void solve() { int n; string s1,s2,s3; cin >> n >> s1 >> s2 >> s3; cout << string(n,'0') << string(n,'1') << '0' << endl; } int main() { int t; cin >> t; for (int _=0; _<t; _++) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); i++) #define all(v) (v).begin(), (v).end() using ll = long long; using P = pair<ll,ll>; int n; vector<int> c(100005); vector<bool> ok(100005,false); vector<bool> visited(100005,false); vector<int> g[100005]; vector<int> cnt(100005,0); void dfs(int x){ visited[x] = true; if(cnt[c[x]]==0){ ok[x] = true; } cnt[c[x]]++; for(auto next:g[x]){ if(visited[next]) continue; dfs(next); } cnt[c[x]]--; } int main(){ cin >> n; rep(i,n){ cin >> c[i]; } rep(i,n-1){ int a,b; cin >> a >> b; a--;b--; g[a].push_back(b); g[b].push_back(a); } dfs(0); rep(i,n){ if(ok[i]){ cout << i+1 << endl; } } }
#include<bits/stdc++.h> #define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);} using namespace std; const int mxn=2e5+5; int c[mxn]; vector<int> g[mxn]; map<int, int> mp; set<int> ans; void dfs(int x, int p){ if(mp[c[x]]==0) ans.insert(x); mp[c[x]]++; for(auto y:g[x]){ if(y==p) continue; dfs(y, x); } mp[c[x]]--; } int main() { int n,i; cin>>n; for(i=1; i<=n; i++) cin>>c[i]; for(i=0; i<n-1; i++){ int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } dfs(1, -1); for(auto x:ans) cout<<x<<'\n'; }
#include <bits/stdc++.h> using namespace std; int main() { double sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; double x = (sy*gx + gy*sx) / (sy + gy); cout.precision(12); cout << x; return 0; }
#include <iostream> #include <string> #include <cmath> using namespace std; int main() { float R, X, Y; cin >> R >> X >> Y; double dist = sqrt(X * X + Y * Y); int ans = 0; while (true) { ans++; if (R == dist) break; if (R < dist) dist -= R; if (R > dist) { ans += 1; break; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void input(int& rnMaxNum) { cin >> rnMaxNum; } bool is7NotIncluded(int nNum, int nBase) { while (nNum != 0) { if (nNum % nBase == 7) return false; nNum /= nBase; } return true; } int calc7NotIncluded(int nMaxNum) { int n7NotIncluded = 0; for (int nx = 1; nx <= nMaxNum; ++nx) if ( is7NotIncluded(nx, 10) && is7NotIncluded(nx, 8) ) ++n7NotIncluded; return n7NotIncluded; } int main() { int nMaxNum; input(nMaxNum); cout << calc7NotIncluded(nMaxNum) << endl; return 0; }
#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; ll MOD=998244353; int main(){FIN; ll n; cin>>n; vector<ll> a(n); fore(i,0,n)cin>>a[i]; sort(ALL(a)); ll res=0; ll sum=0; fore(i,0,n){ res=(res+sum*a[i])%MOD; sum=(2*sum+a[i])%MOD; } fore(i,0,n)res=(res+a[i]*a[i])%MOD; cout<<res<<"\n"; return 0; }
// Problem : C - Friends and Travel costs // Contest : AtCoder - AtCoder Beginner Contest 203(Sponsored by Panasonic) // URL : https://atcoder.jp/contests/abc203/tasks/abc203_c // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef pair<int,int> pii; typedef vector<pii> vii; #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define ff first #define ss second #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define INF (long long) 1e13 #define MOD 1000000007 #define MAX (int) 2e5+5 void solve() { } int32_t main(){ FAST int n,k; cin>>n>>k; int best = 0; vector<pii> a(n); for(int i=0;i<n;i++){ cin >> a[i].ff >> a[i].ss; } sort(all(a)); for(int i=0;i<n;i++){ if(abs(a[i].ff-best)<=k){ k -= a[i].ff-best; best = a[i].ff; k += a[i].ss; } } best += k; cout << best << endl; return 0; }
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { int n,k; cin>>n>>k; string p=to_string(n); for(int i=1;i<=k;i++){ string tmp1=p,tmp2=p; sort(tmp1.begin(),tmp1.end()); sort(tmp2.begin(),tmp2.end(),greater<int>()); p=to_string(stoi(tmp2)-stoi(tmp1)); } cout<<p<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; #define rep(i,n) for (int i = 0; i < (n); ++i) #define endl '\n' ll n,m; vector <P> v,w; set<ll> s; int main(){ cin >> n >> m; rep(i,m){ ll x,y; cin >> x >> y; v.emplace_back(x,y); } sort(v.begin(),v.end()); s.insert(n); int l=0,r; while(l<m){ vector <ll> a,b; r=l; while(r<m){ if(v[r].first==v[l].first)r++; else break; } for(int i=l;i<r;i++){ int x=v[i].second; if((s.find(x-1)!=s.end())||(s.find(x+1)!=s.end()))a.push_back(x); if(((s.find(x-1)==s.end())&&(s.find(x+1)==s.end())) && (s.find(x)!=s.end()))b.push_back(x); } rep(i,a.size())s.insert(a[i]); rep(i,b.size())s.erase(b[i]); l=r; } cout << s.size() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; long long x; cin >> n >> x; vector<long long> a(n); for (int i=0;i<n;i++) cin >> a[i]; vector<vector<long long>> dp(n,vector<long long>(2,0)); vector<long long> mins(n); long long nx = x; for (int i=n-1;i>=0;i--) { mins[i] = nx/a[i]; nx -= mins[i]*a[i]; } dp[0][0] = 1; if (n > 1 && mins[0] != 0) { dp[0][1] = 1; } for (int i=1;i<n-1;i++) { dp[i][0] = dp[i-1][0]; if (mins[i]+1 != a[i+1]/a[i]) { dp[i][0] += dp[i-1][1]; } dp[i][1] = dp[i-1][1]; if (mins[i] != 0) { dp[i][1] += dp[i-1][0]; } } if (n > 1) { dp[n-1][0] = dp[n-2][0]+dp[n-2][1]; } /*for (int i=0;i<n;i++) { cout << dp[i][0] << ' '; } cout << endl; for (int i=0;i<n;i++) { cout << dp[i][1] << ' '; } cout << endl;*/ cout << dp[n-1][0] << endl; }
#include<bits/stdc++.h> #include<bits/extc++.h> #pragma GCC optimize("Ofast") using namespace std; using namespace __gnu_pbds; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t){ while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef DEBUG #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr<<#x<<": "; for(auto i:x) cerr<<i<<" "; cerr<<endl;}} #define debugr(l, r, x) {{cerr<<#x<<": "; for(int i=l;i<=r;i++) cerr<<x<<" "; cerr<<endl;}} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define debugr(l, r, x) #define cerr while(0) cerr #endif mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define priority_queue std::priority_queue #define F first #define S second typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; char a[1005][1005]; bool vis[3005]; vi adj[3005]; void dfs(int x){ vis[x]=true; for(int y:adj[x]){ if(!vis[y]) dfs(y); } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int h, w; cin>>h>>w; set<int> row, col; auto add=[&](int x, int y){ adj[x].push_back(h+y); adj[h+y].push_back(x); row.insert(x); col.insert(h+y); }; add(1, 1); add(1, w); add(h, 1); add(h, w); for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ cin>>a[i][j]; if(a[i][j]=='#'){ add(i, j); } } } int ccs=0; for(int i:row){ if(!vis[i]){ dfs(i); ccs++; } } for(int i:col){ if(!vis[i]){ dfs(i); ccs++; } } debug(row.size(), col.size()); cout<<ccs+min(h-row.size(), w-col.size())-1<<endl; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; int n; vector<int> adj[200005]; ordered_set ins[200005], outs[200005]; int in[200005], out[200005], dis[200005]; int t=0; void dfs(int u,int p) { in[u] = ++t; for(auto x:adj[u]) { if(x!=p) dis[x]=dis[u]+1, dfs(x,u); } out[u]=++t; } int main() { cin>>n; for(int i=2;i<=n;i++) { int x;cin>>x; adj[i].push_back(x); adj[x].push_back(i); } dfs(1,0); for(int i=1;i<=n;i++)ins[dis[i]].insert(in[i]), outs[dis[i]].insert(out[i]); int q; cin>>q; while(q--) { int u,d;cin>>u>>d; int ans=ins[d].order_of_key(out[u])-ins[d].order_of_key(in[u]); cout<<ans<<endl; } }
#include <bits/stdc++.h> using namespace std; int main(){ //入力 int A,B,C,X,Y,Z; cin >> X>>Y>>Z; if(X>=Y && X>=Z){ A=X; if(Y>=Z){ B=Y, C=Z; } else { B=Z, C=Y; } } else if(Y>=X && Y>=Z){ A=Y; if(X>=Z){ B=X, C=Z; } else { B=Z, C=X; } } else { A=Z; if(X>=Y){ B=X, C=Y; } else { B=Y, C=X; } } //cout << A << B << C<< endl; if(C-B==B-A){ cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
#include<bits/stdc++.h> //#include <atcoder/all> #define rep(i,n)for(int i=0;(i)<(int)(n);i++) #define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++) #define ALL(a) (a).begin(),(a).end() #define pb push_back #define fi first #define se second #define sz(x) ((int)x.size()) using namespace std; //using namespace atcoder; using ld = long double; using ll = long long; using P = pair<ll, ll>; template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; } template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; } const ll MOD = 1e9 + 7; const ll INF = 1e9; int main(){ vector<int> a(3); rep(i, 3)cin >> a[i]; sort(ALL(a)); bool f = false; do{ if(a[1] - a[0] == a[2] - a[1])f = true; }while(next_permutation(ALL(a))); cout << (f ? "Yes" : "No") << endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; int main(){ ld S,P; cin >> S >> P; for(ld i=1;i<1e6+5;i++){ if((ll)(P/i)==(P/i)){ if((P/i)+i==S){ cout << "Yes" << endl; return 0; } if(i+(P/i)==S){ cout << "Yes" << endl; return 0; } } } cout << "No" << endl; }
/**************************** ///// Shubham Singhal ///// LNMIIT *****************************/ #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <functional> // using namespace __gnu_pbds; #define ff first #define ss second #define mp make_pair #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define lwr lower_bound #define upr upper_bound #define pq priority_queue #define umap unordered_map #define rep(i,n) for(i=0; i<n;i++) #define repp(i,a,n) for(i=a; i<n;i++) #define precise(x) cout<<fixed<<setprecision(x) #define all(x) x.begin(),x.end() #define lcm(a,b) (a*b)/__gcd(a,b) #define prn(x) cout<<x<<"\n" #define prn2(x, y) cout<<x<<" "<<y<<"\n" #define prn3(x, y, z) cout<<x<<" "<<y<<" "<<z<<"\n" #define prn4(x, y, z, a) cout<<x<<" "<<y<<" "<<z<<" "<<a<<"\n" #define prnv(x) for(auto i:x) cout<<i<<" "; cout<<"\n" #define MOD 1000000007 #define MaX 300005 using namespace std; typedef unsigned long long ull; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<long> vl; typedef vector<char> vc; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<pil> vpil; typedef vector<pli> vpli; typedef vector<pll> vpll; const long long INF64 = 1e18; clock_t time_p=clock(); void time() { time_p=clock()-time_p; cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n"; } void doc() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); } // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> // new_data_set; /************************************************/ bool isprime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void solve() { ll s,p; cin>>s>>p; map<ll,ll> m; for(ll i=1;i<=sqrt(p);i++) { if(p%i==0) { if(i*i==p) m[i]++; else { m[i]++; m[p/i]++; } } } // sort(all(a)); // for(auto it : m) // cout<<it.ff<<' '; // cout<<'\n'; for(auto it=m.begin();it!=m.end();it++) { // cout<<it.ff<<' '; ll x=s-it->ff; // cout<<x<<' '; if(m.find(x)!=m.end()) { // cout<<x<<'\n'; cout<<"Yes"; exit(0); } } cout<<"No\n"; } int main() { fast(); int t=1; // cin>>t; while(t--) solve(); }
#include<bits/stdc++.h> using namespace std; int main () { int n; cin>>n; if(n%2==0) cout<<"White"<<endl; else cout<<"Black"<<endl; }
#include<bits/stdc++.h> #define ll long long using namespace std; int main() { ll t; string s; cin>>s; cout<<s[1]<<s[2]<<s[0]; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long using namespace std; #define inf 1e15+18 #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define mod 998244353 #define f1(i,n) for(i=1;i<=n;i++) #define f0(i,n) for(i=0;i<n;i++) #define w(x) while(x--) const int maxn=100001; #define pq priority_queue #define ff first #define ss second #define pb push_back #define lb lower_bound vector<int>r[52]; vector<int>c[52]; ll a[52][52]; int n; ll c_c; int vis_r[52],vis_c[52]; bool check_r(int i,int j,int k) { for(int x=1;x<=n;x++) { if(a[i][x]+a[j][x]>k) return 0; } return 1; } bool check_c(int i,int j,int k) { for(int x=1;x<=n;x++) { if(a[x][i]+a[x][j]>k) return 0; } return 1; } void dfs_r(int node) { vis_r[node]=1; c_c++; for(int child:r[node]) { if(!vis_r[child]) dfs_r(child); } } void dfs_c(int node) { vis_c[node]=1; c_c++; for(int child:c[node]) { if(!vis_c[child]) dfs_c(child); } } int main() { ll i,j,l,k,ans=1; cin >> n >>k; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cin >>a[i][j]; } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(check_r(i,j,k)){ r[i].pb(j); r[j].pb(i); } } } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(check_c(i,j,k)){ c[i].pb(j); c[j].pb(i); } } } for(int i=1;i<=n;i++) { if(vis_r[i]==0) { c_c=0; dfs_r(i); l=1; for(j=1;j<=c_c;j++) l=(l*j)%mod; ans=(ans*l)%mod; } } for(int i=1;i<=n;i++) { if(vis_c[i]==0) { c_c=0; dfs_c(i); l=1; for(j=1;j<=c_c;j++) l=(l*j)%mod; ans=(ans*l)%mod; } } cout<<ans; }
#include <bits/stdc++.h> #define int long long #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") using namespace std; const int MOD = 998244353; const int N = 2e5 + 5; int resh[N], f[N + 500], n, m; vector <int> primes; int pow(int a, int b) { if (b == 0) return 1ll; else if (b & 1) return (a * pow(a, b - 1)) % MOD; else { int ce = pow(a, b / 2); return (ce * ce) % MOD; } } int C(int a, int b) { int ans = 1; ans = (ans * f[b]) % MOD; ans = (ans * pow(f[a], MOD - 2)) % MOD; ans = (ans * pow(f[b - a], MOD - 2)) % MOD; return ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); f[0] = 1; for (int i = 1; i < N + 500; ++i) { f[i] = (f[i - 1] * i) % MOD; } cin >> n >> m; for (int i = 2; i <= N - 5; ++i) { if (resh[i] == 0) { resh[i] = i; primes.push_back(i); } for (int j = 0; j < primes.size(); ++j) { if (primes[j] > resh[i] || primes[j] * i > N - 5) { break; } resh[primes[j] * i] = primes[j]; } } int ans = 1; for (int i = 2; i <= m; ++i) { int cur_ans = 1; // 1 1 1 1 1 1 m int cure = i; while (cure > 1) { int min_del = resh[cure], cnt = 0; while (cure % min_del == 0) { cure /= min_del; ++cnt; } //cout << cnt << " " << cnt + n - 1 << endl; //cout << C(cnt, cnt + n - 1) << endl; cur_ans = (cur_ans * C(cnt, cnt + n - 1)) % MOD; } ans = (ans + cur_ans) % MOD; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<int(n);i++) #define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++) typedef long long ll; typedef pair<int,int> P; const ll MOD=998244353; ll modpow(ll x,ll n){ ll res=1; while(n!=0){ if(n%2!=0) res=(res*x)%MOD; x=(x*x)%MOD; n/=2; } return res; } int main(void){ ll i; cin.tie(0); ios_base::sync_with_stdio(false); ll N; cin >> N; vector<ll> a(N+1); a[0]=-1; for(i=1;i<=N;i++) cin >> a[i]; sort(a.begin(),a.end()); ll S=(a[N]*a[N])%MOD; ll ans=S; for(i=N-1;i>=1;i--){ if(a[i]!=0){ ll x=(2*a[i]*S)%MOD; ll y=modpow(a[i+1],MOD-2); S=(x*y)%MOD; S+=(a[i]*a[i])%MOD; S-=(a[i]*a[i+1])%MOD; S=(S+MOD)%MOD; ans+=S; ans=ans%MOD; }else{ break; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define vec vector<int> #define vecp vector<pair<int,int>> #define ll long long #define ull unsigned long long #define pb push_back #define fr first #define sc second #define fr1(i,a,b) for(int i=a;i<b;i++) #define fr2(i,a,b) for(int i=a;i>=b;i--) #define fr3(i,a,b) for(int i=a;i<=b;i++) #define umap unordered_map<int,int,custom_hash> #define omap map<int,int> #define uset unordered_set<int,custom_hash> #define oset set<int> #define pr pair<int,int> #define mod 1000000007 #define mp make_pair #define all(v) v.begin(),v.end() #define ppb pop_back struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void read(vec &v) { for(auto &i:v)cin>>i; } bool power(int val) { if(val%6==0||val%10==0||val%15==0) return true; return false; } void solve() { int n; cin>>n; if(n==3) { cout<<"6 10 15"<<"\n"; return ; } int i=6; fr1(j,0,n) { if(power(i)) cout<<i<<" "; else j--; i++; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; //cin>>t; fr3(i,1,t){ // cout<<"Case #"<<i<<": "; solve(); } return 0; }
#include<bits/stdc++.h> #define pb push_back #define Int long long using namespace std; Int N,M,K; Int h,w; Int X,Y,Z; const int MAXN=500005; const int NAX=1005; const int HIGH=1e8+500; int main() { #ifndef ONLINE_JUDGE freopen("inputf.txt","r",stdin); freopen("outputf.txt","w",stdout); #endif cin>>N; vector<Int> A(N); Int GCD=0; for(auto &x:A) { cin>>x; GCD=__gcd(GCD,x); } cout<<GCD<<'\n'; }
#include <stdio.h> int gcd(int a, int b) { if (!a) return b; return gcd(b % a, a); } int main() { int n, a, b; scanf("%i%i", &n, &a); for (int i = 1; i < n; i++) { scanf("%i", &b); a = gcd(a, b); } printf("%i", a); }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define lowbit(x) (x & (-x)) char __c, __neg, __digit[128] = {0}, __skip[128] = {0}; void init_io() { LL i; for (i = 0; i <= 9; i++) __digit[i + '0'] = 1; for (i = 0; i < 128; i++) if ((i < '0' || i > '9') && i != '-') __skip[i] = 1; } #define getLL(v) do { __neg = 0, v = 0; \ do { __c = getchar(); } while (__skip[__c]); \ if (__c == '-') { __neg = 1; __c = getchar(); } \ while (__digit[__c]) { v = v * 10 + __c - '0'; __c = getchar(); } \ if (__neg) v = -v; \ } while (0) const LL inf = 1e17+9; const LL mod = 1e9+7; const LL maxn = 2e5+8; map<LL, LL> mp[maxn]; LL n, q, fa[maxn], c[maxn]; LL find(LL u) { if (fa[u] == u) return u; return fa[u] = find(fa[u]); } void solve() { LL i, j, opt, a, b, x, y; getLL(n); getLL(q); for (i = 1; i <= n; i++) { getLL(c[i]); fa[i] = i; mp[i][c[i]]++; } for (i = 1; i <= q; i++) { getLL(opt); if (opt == 1) { getLL(a); getLL(b); x = find(a), y = find(b); if (x != y) { fa[x] = y; if (mp[x].size() > mp[y].size()) swap(mp[x], mp[y]); for (auto u : mp[x]) mp[y][u.first] += u.second; mp[x].clear(); } } else { getLL(x); getLL(y); x = find(x); printf("%lld\n", mp[x][y]); } } } signed main() { init_io(); solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define deb(x) cout << #x << " = " << x << endl; typedef long long ll; typedef long double ld; const int MOD = 1e9 + 7; void solve() { int n; cin >> n; vector<ll> a(n); for(ll &i : a) cin >> i; ll ans = 0; sort(a.begin(), a.end()); for(int i = n - 1; i >= 0; i--) ans += (i * a[i]) - ((n - i - 1) * a[i]); cout << ans << '\n'; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1, tt = 0; //cin >> t; while(t--){ //cout << "Case #" << ++tt << ": "; solve(); } return 0; } //Hajimemashite
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=998244353; int n,a[200005],ans=0; int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; sort(a+1,a+n+1); for(int i=1,sum=0;i<=n;i++){ ans=(ans+1ll*a[i]*sum+1ll*a[i]*a[i])%mod; sum=(sum*2ll+a[i])%mod; } cout<<ans; }
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define int long long #define cin(a,n) for(int i=0;i<n;i++) cin>>a[i] #define cin2(a,b,n) for(int i=0;i<n;i++) cin>>a[i]>>b[i] #define mod 998244353 #define pb push_back #define pie 3.141592653589793238462643383279 vector<bool> prime; vector<int> fact,inv,primes,factors; void factorize(int a) { fact.clear(); for(int i=1;i*i<=a;i++) { if (i*i==a) factors.pb(i); else if (a%i==0) { factors.pb(i); factors.pb(a/i); } } sort(factors.begin(),factors.end()); } int power(int a,int b) { if(a==1||b==0) return 1; int c=power(a,b/2); int res=1; res=c*c; if(res>=mod) res%=mod; if(b%2) res*=a; if(res>=mod) res%=mod; return res; } int modInv(int a) { return power(a,mod-2); } void factorial(int n) { fact.resize(n+1); fact[0]=1; for(int i=1;i<=n;i++) { fact[i]=fact[i-1]*i; if(fact[i]>=mod) fact[i]%=mod; } } void InvFactorial(int n) { inv.resize(n+1); inv[0]=1; for(int i=1;i<=n;i++) inv[i]=modInv(fact[i]); } int ncr(int n,int r) { if(n<r||n<0||r<0) return 0; int b=inv[n-r]; int c=inv[r]; int a=fact[n]*b; if(a>=mod) a%=mod; a*=c; if(a>=mod) a%=mod; return a; } void remove_duplicates(vector<pair<int,int>> &v) { sort(v.begin(),v.end()); int _size=unique(v.begin(),v.end())-v.begin(); v.resize(_size); } unsigned int gcd(unsigned int u, unsigned int v) { if(u==0||v==0) return max(u,v); unsigned int shift=__builtin_ctz(u|v); u>>=__builtin_ctz(u); do{ v>>=__builtin_ctz(v); if(u>v) swap(u,v); v-=u; }while(v!=0); return u<<shift; } void sieve(int n) { prime.assign(n+1,1); prime[1]=false; for(int p=2;p*p<=n;p++) if(prime[p]) for(int i=p*p;i<=n;i+=p) prime[i]=false; for(int i=2;i<n+1;i++) if(prime[i]) primes.push_back(i); } //---------------------------------------------------------------------------------------------------------------------------------------------------- void solve() { int n; cin>>n; int a[n]; cin(a,n); sort(a,a+n); int sum=0,exp=a[0]; for(int i=1;i<n;i++) { sum=(sum+a[i]*exp)%mod; exp=(2*exp+a[i])%mod; } for(int i=0;i<n;i++) sum=(sum+a[i]*a[i])%mod; cout<<sum; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T=1; //cin>>T; for(int i=1;i<=T;i++) solve(); }
/** try <3 **/ #include<bits/stdc++.h> using namespace std; #define task "sol" #define lb lower_bound #define ub upper_bound #define fi first #define se second #define pb push_back #define mp make_pair #define zs(v) ((int)(v).size()) #define BIT(x, i) (((x) >> (i)) & 1) #define CNTBIT __builtin_popcountll #define ALL(v) (v).begin(),(v).end() #define endl '\n' typedef long double ld; typedef long long ll; typedef pair<int,int> pii; const int dx[8]={0,-1,0,1,1,-1,-1,1}; const int dy[8]={-1,0,1,0,-1,-1,1,1}; void gogo() { string s; cin>>s; s=s+s[0]; s.erase(0,1); cout<<s; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if(fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } gogo(); }
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T> using vc = vector<T>; template<class T> using vvc = vc<vc<T>>; template<class T> using vvvc = vc<vvc<T>>; template<class T> using vvvvc = vvc<vvc<T>>; template<class T> using PQ = priority_queue<T>; template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>; using IP = pair<int, int>; using LP = pair<ll, ll>; #define mp make_pair #define pb push_back #define all(x) begin(x), end(x) #define rep(i, n) for (int i = 0; (i) < (int)(n); i++) #define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++) #define repr(i, n) for (int i = n; (i) >= 0; i--) #define rep3r(i, m, n) for (int i = (n); (i) >= (int)(m); i--) template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; } //constexpr int INF = 1070000000; //constexpr long long LINF = 4611686015206162431; //constexpr int MOD = 1000000007; signed main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vc<int> V(N*2); rep (i, N*2) cin >> V[i]; ll s = 0; rep (i, N*2) s += V[i]; priority_queue<int, vc<int>, greater<int>> que; int l = N-1, r = N; rep (i, N) { que.push(V[l]); que.push(V[r]); s -= que.top(); que.pop(); l--; r++; } cout << s << endl; }
#include<iostream> #include<vector> #include<bitset> using namespace std; unsigned xorshift() { static unsigned y=2463534242; y^=y<<13; y^=y>>17; y^=y<<5; return y; } const int N=20; int M; string S[800]; string outH[N],outW[N]; int G[800][800]; using bit=bitset<800>; bit rest; bit can[N+1][800]; int cnt[800]; int main() { cin>>M>>M; for(int i=0;i<M;i++) { cin>>S[i]; rest[i]=1; cnt[i]=1; } for(int i=0;i<M;i++)for(int j=0;j<M;j++)if(i!=j&&rest[j]) { for(int k=1;k<=S[i].size();k++) { int len=S[i].size()-k; if(S[j].size()<len)len=S[j].size(); if(S[i].substr(k,len)==S[j].substr(0,len)) { G[i][j]=S[j].size()-len; if(G[i][j]==0) { rest[j]=0; cnt[i]+=cnt[j]; cnt[j]=0; } break; } } } for(int tm=0;tm<N;tm++) { string now=""; if(rest.any()) { vector<vector<int> >dist(N+1,vector<int>(M,0)); vector<vector<int> >pr(N+1,vector<int>(M,-1)); for(int v=0;v<M;v++)if(rest[v]) { for(int u=S[v].size();u<=N;u++) { dist[u][v]=cnt[v]; can[u][v]=rest; can[u][v][v]=0; } } for(int u=0;u<N;u++)for(int v=0;v<M;v++)if(dist[u][v]>0) { for(int w=0;w<M;w++)if(can[u][v][w]) { int nu=u+G[v][w]; if(nu<=N&&dist[nu][w]<dist[u][v]+cnt[w]) { dist[nu][w]=dist[u][v]+cnt[w]; pr[nu][w]=v; can[nu][w]=can[u][v]; can[nu][w][w]=0; } } } int go=-1; for(int v=0;v<M;v++)if(go==-1||dist[N][v]>dist[N][go])go=v; vector<int>vs; { int nu=N; while(true) { vs.push_back(go); int ngo=pr[nu][go]; if(ngo<0)break; nu-=G[ngo][go]; go=ngo; } } now=S[go]; for(int i=vs.size()-1;i--;) { int d=G[go][vs[i]]; go=vs[i]; now+=S[go].substr(S[go].size()-d); } } while(now.size()<N)now+='.'; cout<<now<<endl; outW[tm]=now+now; for(int j=0;j<N;j++)outH[j]+=now[j]; for(int i=0;i<M;i++)if(rest[i]) { for(int k=0;k<N;k++)if(outW[tm].substr(k,S[i].size())==S[i]) { rest[i]=0; break; } if(rest[i]&&tm+1>=S[i].size()) { for(int j=0;j<N;j++)if(outH[j].substr(tm+1-S[i].size())==S[i]) { rest[i]=0; break; } } } } }
#include<bits/stdc++.h> using namespace std; #define ll long long // memo fixed setprecision(20); using vvll = vector<vector<ll>>; ll mod =1e9+7; /*"itob" int to "N"base */ template<typename TypeInt> string itob(const TypeInt v, int base){static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";string ret;static numeric_limits<TypeInt> t;TypeInt n = v;if (t.is_signed) {if (v < 0) n *= -1;}while (n >= base) {ret += table[n%base];n /= base;}ret += table[n];if (t.is_signed) {if (v < 0 && base == 10) ret += '-';}reverse(ret.begin(), ret.end());return ret;} /*"chmin" a = MAX*/ template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } /*"chmax" a = MIN*/ template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } /*"ctoi" char to int*/ int ctoi(char c) {return c-'0';} /*"gcd" MAX Euclidean */ ll gcd(ll a,ll b){if(b==0)return a; return gcd(b,a%b);} /*"lcm" MIN*/ ll lcm(ll a,ll b){ll g = gcd(a,b);return a/g*b;} /*"primecheck"If prime,return true.*/bool primecheck(ll n){if(n < 2) return false;else{for(int i = 2; i * i <= n; i++){if(n % i == 0) return false;}return true;}} string reverserange(string s,ll a,ll b){reverse(s.begin()+a-1,s.begin()+b); return s;} ll modpow(ll a,ll n, ll mod){ll res = 1;while(n>0){if (n%2==1){res = res * a % mod;}a = a * a % mod;n/=2;}return res;} int main() { srand(time(NULL)); ll n,m; cin >> n >> m; vector<string>che(m); string ra ="ABCDEFGH"; for(int i=0;i<m;i++){ cin>>che.at(i); } sort(che.begin(),che.end()); ll check =0; for(int i=0;i<n;i++){ string d; while(d.size()<n){ if(d.size()+che.at(check).size()>n){ while(d.size()<n){ d+=ra.at(rand()%8); } break; } d+=che.at(check); check++; } if(d.size()>n){ string g; for(int j=0;j<n;j++){ g+=d.at(j); } d=g; } cout<<d<<endl; } }
#include <array> #include <cstddef> #include <cstdint> constexpr std::size_t bsr64(std::uint64_t c) noexcept { #ifdef __GNUC__ return 63 - __builtin_clzll(c); #else constexpr std::array<std::size_t, 64> table = { 0, 1, 2, 7, 3, 13, 8, 27, 4, 33, 14, 36, 9, 49, 28, 19, 5, 25, 34, 17, 15, 53, 37, 55, 10, 46, 50, 39, 29, 42, 20, 57, 63, 6, 12, 26, 32, 35, 48, 18, 24, 16, 52, 54, 45, 38, 41, 56, 62, 11, 31, 47, 23, 51, 44, 40, 61, 30, 22, 43, 60, 21, 59, 58}; c |= c >> 1; c |= c >> 2; c |= c >> 4; c |= c >> 8; c |= c >> 16; c |= c >> 32; return table[((c >> 1) + 1) * 0x218A7A392DD9ABFULL >> 58 & 0x3F]; #endif } #include <array> #include <cstddef> #include <cstdint> constexpr std::size_t bsf64(const std::uint64_t c) noexcept { #ifdef __GNUC__ return __builtin_ctzll(c); #else constexpr std::array<std::size_t, 64> table = { 0, 1, 2, 7, 3, 13, 8, 27, 4, 33, 14, 36, 9, 49, 28, 19, 5, 25, 34, 17, 15, 53, 37, 55, 10, 46, 50, 39, 29, 42, 20, 57, 63, 6, 12, 26, 32, 35, 48, 18, 24, 16, 52, 54, 45, 38, 41, 56, 62, 11, 31, 47, 23, 51, 44, 40, 61, 30, 22, 43, 60, 21, 59, 58}; return table[(c & ~c + 1) * 0x218A7A392DD9ABFULL >> 58 & 0x3F]; #endif } #include <algorithm> #include <array> #include <cstdint> #include <iostream> #include <limits> int main() { using i64 = std::int64_t; using u64 = std::uint64_t; const auto distance = [](u64 a, u64 b) { return a < b ? b - a : a - b; }; u64 X, Y; std::cin >> X >> Y; if (X >= Y) { std::cout << X - Y << std::endl; return 0; } u64 ans = std::numeric_limits<u64>::max(); const auto update = [&](u64 t) { ans = std::min(ans, t); }; const auto check = [&](u64 step) { const u64 ctz = bsf64(Y); if (step <= ctz) { update(distance(X, Y >> step) + step); return; } step -= ctz + 1; const u64 Y_ = Y >> (ctz + 1); const u64 stm = ~(~u64(0) << step); struct elem_t { u64 cost; u64 mask; }; const auto base = [&](const u64 mat_base, const u64 add) { std::array<std::array<elem_t, 3>, 3> mat = {}; for (int i = 0; i < 3; ++i) { mat[i][std::min(i + 1, 2)].mask |= mat_base & stm; mat[i][std::max(i - 1, 0)].mask |= ~mat_base & stm; mat[i][i].mask |= ~stm; } mat[0][1].cost = mat_base & stm; mat[1][2].cost = mat_base & stm; const auto iterate = [&](const u64 w, const u64 mask) { std::array<std::array<elem_t, 3>, 3> sq = {}; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 3; ++k) { u64 temp = (mat[i][j].mask & mask) & (mat[j][k].mask >> w & mask); temp |= temp << w; sq[i][k].mask |= temp; sq[i][k].cost |= ((mat[i][j].cost & mask) + (mat[j][k].cost >> w & mask)) & temp; } } } mat = sq; }; u64 w = 1; for (; w < step; w *= 2) { iterate(w, ~u64(0) / ((u64(1) << w) + 1)); } u64 res = 0; for (int i = 0; i < 3; ++i) { res |= mat[1][i].cost & (w * 2 - 1); } update(res + ctz + 2 + step + distance(X, (Y_ >> step) + add)); }; base(Y_, 0); base(~Y_, 1); }; const i64 bsrY = bsr64(Y); for (i64 i = bsrY - 2; i <= bsrY + 1; ++i) { if (0 <= i && i < 64) { check(i); } } const i64 diff = bsrY - bsr64(X); for (i64 i = diff - 1; i <= diff + 1; ++i) { if (0 <= i && i < 64) { check(i); } } std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int,null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define ordered_set_pair tree<pair<int,int>,null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> #define ordered_set_mutiset tree<int,null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> typedef long long int ll; typedef long double ld; typedef unsigned long long int ull; typedef pair<int,int> pi; #define PI 3.1415926535897932384 #define FOR(i,vv,n) for(ll i=vv;i<n;i++) #define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--) #define ve vector #define maxind(v) (max_element(v.begin(),v.end())-v.begin()) #define minind(v) (min_element(v.begin(),v.end())-v.begin()) #define maxe(v) *max_element(v.begin(),v.end()) #define mine(v) *min_element(v.begin(),v.end()) #define all(v) v.begin(),v.end() #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define eb emplace_back #define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mp make_pair #define M 1000000007ll #define INF 1000000000000000000ll #define PRECISE cout.precision(18); #define BS(v,n) binary_search(v.begin(),v.end(),n) #define srt(v) sort(v.begin(),v.end()) #define rsrt(v) sort(v.begin(),v.end(),greater <ll>()) #define uni(v) v.resize(unique(v.begin(),v.end())-v.begin()) #define F first #define S second #define GET(i,p) get<p>(i) int main(){ #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif FAST // PRECISE char s,t; cin>>s>>t; if(s=='Y')cout<<(char)toupper(t); else cout<<(char)tolower(t); return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> P; typedef long long ll; #define int ll ll score(char c[], int a){ ll ans = 0; int num[10]; for(int i=0;i<10;i++) num[i]=0ll; for(int i=0;i<4;i++) num[c[i]-'0']++; num[a]++; for(int i=0;i<10;i++) ans += i*pow(10,num[i]); return ans; } main(){ int k; char s[10],t[10]; cin >> k >> s >> t; int card[10]; card[0] = 0; for(int i=1;i<=9;i++) card[i] = k; for(int i=0;i<4;i++) card[s[i]-'0']--; for(int i=0;i<4;i++) card[t[i]-'0']--; ll sum = (9ll*k-8ll)*(9ll*k-9ll); ll win = 0ll; for(int i=1;i<=9;i++){ for(int j=1;j<=9;j++){ if(score(s, i)>score(t,j)){ if(i==j){ if(card[i]>=2) win += card[i]*(card[i]-1); }else{ win += card[i]*card[j]; } } } } double ans = (double) win / sum; printf("%.10lf\n",ans); return 0; }
#include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <math.h> #include <string> using namespace std; int calc_score(long score, int cards, int p){ score -= p* pow(10, cards - 1); score += p * pow(10, cards); return score; } int main(){ long k; cin >> k; int s_in, t_in; vector<int> s(4); vector<int> t(4); scanf("%d# %d#",&s_in, &t_in); vector<int> card_s(9); vector<int> card_t(9); long cs = 0; long ct = 0; int a = 1000; for(int i = 0; i < 4; i++){ s[i] = s_in / a; t[i] = t_in / a; s_in -= s[i]*a; t_in -= t[i]*a; a = a/10; card_s[s[i] - 1]++; card_t[t[i] - 1]++; } for(int i = 0; i < 4; i++){ int si = s[i]; int ti = t[i]; } for(int i = 1; i < 10; i++){ cs += i * pow(10, card_s[i - 1]); ct += i * pow(10, card_t[i - 1]); } long kati = 0; for(int i = 1; i < 10; i++){ if(card_s[i-1] + card_t[i-1]== k) continue; for(int j = 1; j < 10; j++){ if(card_s[j-1] + card_t[j-1]== k) continue; if(i == j && card_s[i-1] + card_t[i-1] + 2 > k){ continue; } if(calc_score(cs, card_s[i-1]+1, i) > calc_score(ct, card_t[j-1]+1, j)){ if(i == j){ kati += (k - card_s[i-1] - card_t[i-1]) * (k - card_s[i-1] - card_t[i-1] - 1); } else{ kati += (k - card_s[i-1] - card_t[i-1]) * (k - card_s[j-1] - card_t[j-1]); } } } } double ans = (double)kati / ((9.0 * k - 8.0) * (9.0 * k - 9.0)); cout << ans << endl; return 0; }
// UpS0lver #include <bits/stdc++.h> using namespace std; #define sz(a) int((a).size()) #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define uns(v) sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end()) #define inf 1000000000 #define MAXN 2007 #define MOD 1000000007 typedef long long ll; bool vis[MAXN]; vector<int> adj[MAXN]; int cnt; void dfs(int u) { if (!vis[u]) { vis[u] = true; cnt++; for (int i = 0; i < sz(adj[u]); i++) { int v = adj[u][i]; if (vis[v]) continue; dfs(v); } } } void _run_test() { int n, m, a, b; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; a--, b--; adj[a].pb(b); } ll res = 0; for (int i = 0; i < n; i++) { memset(vis, false, sizeof(vis)); cnt = 0; dfs(i); res += cnt; } cout << res << "\n"; } int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while (t--) { _run_test(); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define irep(i, n) for (int i = (n-1); i >= 0; i--) using namespace std; using ll = long long; using P = pair<int,int>; constexpr int N = 2000; vector<vector<int>> g(N); int bfs(int start, int n) { vector<int> dist(N, -1); queue<int> q; dist[start] = 0; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); for (int nv : g[v]) { if (dist[nv] != -1) continue; dist[nv] = dist[v] + 1; q.push(nv); } } int r = 0; rep(i,n) { if (dist[i] >= 0) r++; } return r; } int main() { int n, m; cin >> n >> m; rep(i,m) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); } int ans = 0; rep(i,n) { ans += bfs(i, n); } cout << ans << endl; }
#include <bits/stdc++.h> typedef long long ll; #define Boost \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define endl '\n' #define fo(i, x, y) for (__typeof(x) i = x; i < y; ++i) #define fr(i, x, y) for (__typeof(x) i = x; i > y; i--) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define vdp(n, x) vector<vector<ll>> dp(n + 1, vector<ll>(x + 1)); ll mod = 1000000007; const ll AM = 2e5 + 5; using namespace std; #include <bits/stdc++.h> using namespace std; int main() { Boost; int t; //cin >> t; t = 1; while (t--) { ll m, h; cin >> m >> h; if (h % m == 0) cout << "Yes" << endl; else cout << "No" << endl; } }
#include<bits/stdc++.h> using namespace std; // #pragma GCC target ("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimization ("unroll-loops") // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("fpmath=387") //extra precision #define debug_input cout<<"input"<<endl;freopen("input.txt","r",stdin); #define debug_output cout<<"output"<<endl;freopen("output.txt","w",stdout); #define debug debug_input;debug_output; typedef double db; typedef string str; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef pair<double,double> pd; #define F first #define S second #define endl '\n' #define ALL(a) a.begin(),a.end() #define mp make_pair #define pb push_back #define FOR(i,a,b) for(int i=a;i<(int)b;i++) #define FORN(i,a,b) for(int i=a;i<=(int)b;i++) #define FORB(i,a,b) for(int i=a;i>=(int)b;i--) #define REP(i,x) FOR(i,0,x) #define MEM(arr, x) memset(arr, (x), sizeof(arr)) typedef vector<int> vi; typedef vector<ll> vl; typedef vector<str> vs; typedef vector<pi> vii; typedef vector<pl> vll; const int MOD = 1e9+7; const ll INF = 1e18; const ld PI = acos((ld)-1); const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; void solve(){ str x; cin>>x; int a = 1, b = 1; for(int i=0;i<(int)x.size();i+=2) if(x[i] >= 'A' && x[i] <= 'Z') a = 0; for(int i=1;i<(int)x.size();i+=2) if(x[i] >= 'a' && x[i] <= 'z') b = 0; if(a && b) cout<<"Yes"; else cout<<"No"; cout<<endl; } int main(){ ios_base::sync_with_stdio(false);cin.tie(nullptr); // cout.tie(nullptr); // debug_output; // debug; int t = 1; // cin>>t; for(int tc=1;tc<=t;tc++){ // cout<<"Case #"<<tc<<": "; solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; for(int i = 1; i <= 10; i++) { string t = s; reverse(t.begin(), t.end()); if(t == s) { cout << "Yes\n"; return 0; } s = '0' + s; } cout << "No\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define eFOR(i,a,n) for(int i=(a);i<=(n);++i) #define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i) #define erFOR(i,a,n) for(int i=(n);i>=(a);--i) #define SORT(a) sort(a.begin(),a.end()) #define rSORT(a) sort(a.rbegin(),a.rend()) #define fSORT(a,f) sort(a.begin(),a.end(),f) #define all(a) a.begin(),a.end() #define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x)) #define tp(a,i) get<i>(a) #ifdef _DEBUG #define line cout << "-----------------------------\n" #define stop system("pause") #define debug(x) print(x) #endif constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; } template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); } template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } template<class T>inline void print(const T& a) { cout << a << "\n"; } template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); } template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); } inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; } template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); } template<class T>inline void END(T s) { print(s); exit(0); } void END() { exit(0); } VS solve(int n) { if (n == 1)return { "AB" }; VS res{ string(1 << n - 1,'A') + string(1 << n - 1,'B') }; auto sub = solve(n - 1); for (auto& s : sub)res.push_back(s + s); for (auto& s : sub) { res.push_back(s); for (auto& c : s)c ^= ('A' ^ 'B'); res.back() += s; } return res; } int main() { init(); int n; cin >> n; auto ans = solve(n); print((1 << n) - 1); for (auto& x : ans)print(x); return 0; }
//by ciwomuli //enter ICPC WF 2020 #include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <stack> #include <vector> #include <string> #include <sstream> #define LL long long using namespace std; template <typename T> inline void read(T &t) { int f = 0, c = getchar(); t = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) t = t * 10 + c - 48, c = getchar(); if (f) t = -t; } template <typename T, typename... Args> inline void read(T &t, Args &... args) { read(t); read(args...); } int h, w; int main(){ int sum = 0; int mi = 1000; read(h, w); for (int i = 1; i <= h;i++){ for (int j = 1; j <= w;j++){ int a; read(a); sum += a; if(a<mi) mi = a; } } cout << sum - mi * h * w; }
// #define _GLIBCXX_DEBUG #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i)) #define ALL(x) std::begin(x), std::end(x) using namespace std; void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head &&h, Tail &&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); } #define dump(...) \ cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), \ cerr << "*/\n\n"; typedef long long ll; ll solve(ll h, ll w, vector<vector<ll>> &field) { ll m = 1000; REP(i, h) REP(j, w) { m = min(m, field[i][j]); } ll res = 0; REP(i, h) REP(j, w) { res += field[i][j] - m; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll h, w; cin >> h >> w; vector<vector<ll>> field(h, vector<ll>(w, 0)); REP(i, h) REP(j, w) { cin >> field[i][j]; } ll res = solve(h, w, field); cout << res << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;} template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>b)return 0; a=b; return 1;} #define bitUP(x,a) ((x>>a)&1) int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0}; long double EPS = 1e-6; long double PI = acos(-1); const ll INF=(1LL<<62); const int MAX=(1<<30); constexpr ll MOD=1000000000+7; inline void bin101(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); } using pii=pair<int,int>; using pil=pair<int,ll>; using pli=pair<ll,int>; using pll=pair<ll,ll>; using psi=pair<string,int>; using pis=pair<int,string>; using psl=pair<string,ll>; using pls=pair<ll,string>; using pss=pair<string,string>; //1-indexed vector cin template<typename T> inline void vin1(vector<T> &v){ for(size_t i=1;i<v.size();i++) cin>>v[i]; } //0-indexed vector cin template<typename T> inline void vin0(vector<T> &v){ for(size_t i=0;i<v.size();i++) cin>>v[i]; } //1-indexed vector<vector> cin template<typename T> inline void vin1(vector<vector<T>> &v){ for(size_t i=1;i<v.size();i++){ for(size_t j=1;j<v[i].size();j++) cin>>v[i][j]; } } //0-indexed vector<vector> cin template<typename T> inline void vin0(vector<vector<T>> &v){ for(size_t i=0;i<v.size();i++){ for(size_t j=0;j<v[i].size();j++) cin>>v[i][j]; } } //要素数n 初期値x template<typename T> inline vector<T> vmake(size_t n,T x){ return vector<T>(n,x); } //a,b,c,x data[a][b][c] 初期値x template<typename... Args> auto vmake(size_t n,Args... args){ auto v=vmake(args...); return vector<decltype(v)>(n,move(v)); } //pair cout template<typename T, typename U> inline ostream &operator<<(ostream &os,const pair<T,U> &p) { os<<p.first<<" "<<p.second; return os; } //pair cin template<typename T, typename U> inline istream &operator>>(istream &is,pair<T,U> &p) { is>>p.first>>p.second; return is; } //ソート template<typename T> inline void vsort(vector<T> &v){ sort(v.begin(),v.end()); } //逆順ソート template<typename T> inline void rvsort(vector<T> &v){ sort(v.rbegin(),v.rend()); } //1ビットの数を返す inline int popcount(int x){ return __builtin_popcount(x); } //1ビットの数を返す inline int popcount(ll x){ return __builtin_popcountll(x); } bool flag(ll sx,ll sy,ll gx,ll gy){ if(sx+sy==gx+gy or sx-sy==gx-gy or abs(sx-gx)+abs(sy-gy)<=3){ return true; } return false; } void solve(){ ll sx,sy; ll gx,gy; cin>>sx>>sy>>gx>>gy; if(sx==gx and sy==gy){ cout<<0<<endl; return; } //1会で行ける場合 if(flag(sx,sy,gx,gy)){ cout<<1<<endl; return; } //2会 if((sx+sy)%2==(gx-gy)%2){ cout<<2<<endl; return; } for(int i=-3;i<=3;i++){ for(int j=-3;j<=3;j++){ if(abs(i)+abs(j)>3) continue; if(flag(sx+i,sy+j,gx,gy)){ cout<<2<<endl; return; } } } cout<<3<<endl; } int main(){ bin101(); int T=1; //cin>>T; while(T--) solve(); }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<n; i++) using namespace std; int main() { long long r1,c1,r2,c2; cin >> r1 >> c1; cin >> r2 >> c2; int ans; long long dist = abs(r2-r1)+abs(c2-c1); if(dist==0) ans=0; else{ if(dist<=3) ans=1; else{ if(abs(abs(r2-r1)==abs(c2-c1))) ans=1; else{ if(dist%2==0) ans=2; else { if(abs(abs(r2-r1)-abs(c2-c1))<=3) ans=2; else ans=3; } } } } cout << ans << endl; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; constexpr ll MOD = 998244353; #ifndef ONLINE_JUDGE template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;} ostream &operator<<(ostream &o, const string &obj) {o << "\""; o << obj.c_str(); o << "\""; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;} template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} void print_sim_py(void) {cout << endl;} template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();} template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);} #define print(...) print_sim_py(__VA_ARGS__); #else #define print(...); #endif template <typename... Ts> std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); } #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define Rrepr(i, a, b) for (int i = b; i >= a; i--) #define Rrep(i, n) for (int i = n-1; i >= 0; i--) bool dfs(ll p, vector<vector<ll>>& v,vector<ll>& fin_list, vector<ll>& cv,ll c, ll depth){ bool closed = false; if(fin_list[p]){ if(depth >= 3) return true; else return false; } cv[p] = c; fin_list[p] = 1; rep(i, v[p].size()){ if(v[p][i]){ closed |= dfs(i, v, fin_list, cv, c, depth+1); } } return closed; } class Vsum2{ public: vector<vector<ll>> vsum; Vsum2(vector<vector<ll>>& a, function<ll (ll&)> func=[](auto& a){ return a; }){ ll h = a.size(); ll w = a[0].size(); vsum.resize(h+1, vector<ll>(w+1, 0)); repr(i,0,h){ ll jsum = 0; rep(j,w){ jsum += func(a[i][j]); vsum[i+1][j+1] = jsum + vsum[i][j+1]; } } //print("#", vsum) } ll range(ll y1,ll x1, ll y2,ll x2){ return (vsum[y2][x2] - vsum[y2][x1] - vsum[y1][x2] + vsum[y1][x1]); } }; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); ll n,k; IN(n,k); vector<vector<ll>> a(n, vector<ll>(n, 0)); ll amax = 0; rep(i,n){ rep(j,n){ IN(a[i][j]); amax = max(amax, a[i][j]); } } ll l = -1 ,r = 1e9; while(l+1<r){ ll m = (l+r)/2; bool ok = false; Vsum2 asum(a, [&](auto& a){ return a > m; }); rep(i,n-k+1){ rep(j, n-k+1){ ll sumr = asum.range(i,j, i+k,j+k); if(sumr < k*k/2+1){ ok = true; } } } if(ok){ r=m; }else{ l=m; } } ll ans = r; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define FOR(i,m) for(int i = 0 ;i < m ;i++) #define mem(a ,b) memset(a, (b) , sizeof(a)) #define all(cont) cont.begin(),cont.end() #define mp make_pair #define pb push_back #define F first #define S second #define cn cout<<'\n' #define re return typedef pair<int,int> pi; typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef unsigned long long int ull; const int mod = 1e9 +7; int a[801][801]; int p[801][801]; int n , k; bool check(int x){ memset(p , 0 , sizeof(p)); for(int i = 1 ;i <= n ;i++){ for(int j = 1 ;j <= n ;j++){ p[i][j] = (a[i][j] <= x ? 1 : 0); } } for(int i =1 ;i <= n;i++){ for(int j = 1; j <= n ;j++){ p[i][j] += p[i-1][j] + p[i][j-1] - p[i-1][j-1]; } } int max_val = 0; for(int i = k; i <= n ;i++){ for(int j = k ;j <= n ;j++){ max_val = max(max_val , p[i][j] - p[i-k][j] - p[i][j-k] + p[i-k][j-k]); } } return max_val >= (k*k + 1)/2; } void solve(){ cin >> n >> k; for(int i = 1 ;i <= n ;i++){ for(int j = 1 ; j <= n ;j++)cin>>a[i][j]; } int l = 0 , r = mod-7; while(l < r){ int mid = l + (r-l)/2; if(check(mid)){ r = mid; } else l = mid + 1; } cout<<l<<"\n"; } int32_t main(){ ios; int _t = 1; // cin >> _t; for(int i = 1 ; i <= _t ;i++){ // cout<< "Case #"<<i<<": "; solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++) #define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) #define setp(n) fixed << setprecision(n) template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define ins insert #define debug(a) cerr<<(a)<<endl #define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl #define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;} using namespace std; template<class A, class B> ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";} template<class A, class B> istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;} //------------------------------------------------- //--ModInt //------------------------------------------------- const uint_fast64_t MOD = 1e9+7; class mint { private: using Value = uint_fast64_t; Value n; public: mint():n(0){} mint(int_fast64_t _n):n(_n<0 ? MOD-(-_n)%MOD : _n%MOD){} mint(const mint &m):n(m.n){} friend ostream& operator<<(ostream &os, const mint &a){ return os << a.n; } friend istream& operator>>(istream &is, mint &a){ Value temp; is>>temp; a = mint(temp); return is; } mint& operator+=(const mint &m){n+=m.n; n=(n<MOD)?n:n-MOD; return *this;} mint& operator-=(const mint &m){n+=MOD-m.n; n=(n<MOD)?n:n-MOD; return *this;} mint& operator*=(const mint &m){n=n*m.n%MOD; return *this;} mint& operator/=(const mint &m){return *this*=m.inv();} mint& operator++(){return *this+=1;} mint& operator--(){return *this-=1;} mint operator+(const mint &m) const {return mint(*this)+=m;} mint operator-(const mint &m) const {return mint(*this)-=m;} mint operator*(const mint &m) const {return mint(*this)*=m;} mint operator/(const mint &m) const {return mint(*this)/=m;} mint operator++(int){mint t(*this); *this+=1; return t;} mint operator--(int){mint t(*this); *this-=1; return t;} bool operator==(const mint &m) const {return n==m.n;} bool operator!=(const mint &m) const {return n!=m.n;} mint operator-() const {return mint(MOD-n);} mint pow(Value b) const { mint ret(1), m(*this); while(b){ if (b & 1) ret*=m; m*=m; b>>=1; } return ret; } mint inv() const {return pow(MOD-2);} }; char C[4]; mint dp[1010]; //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; rep(i,4) cin>>C[i]; // C[1]=A のときは C[0]を見る if (C[1]=='A' && C[0]=='A'){ cout<<"1\n"; return 0; } // C[1]=B のときは C[3]を見る if (C[1]=='B' && C[3]=='B'){ cout<<"1\n"; return 0; } bool flg=(C[1]!=C[2]); dp[0] = 1; rep(i,N){ FOR(j,i+2,N){ if (flg && i!=0) dp[j]+=dp[i]*(j-i-1); else dp[j]+=dp[i]; } } cout<<dp[N]<<endl; return 0; }
#include<iostream> using namespace std; int n,k,mod,maxn; int f[101][550001]; int main() { cin>>n>>k>>mod; f[0][0]=1; for(int i=1;i<=n;i++) { maxn=(i+1)*i/2*k; for(int j=0;j<=maxn;j++) { long long sum=0; for(int kk=0;kk<=k;kk++) { if(j-i*kk<0)break; sum+=f[i-1][j-kk*i]; } f[i][j]=sum%mod; } } for(int i=1;i<=n;i++) { int ans=0; for(int j=0;j<=n*(n+1)/2*k;j++) { ans+=(long long)f[i-1][j]*f[n-i][j]%mod; ans%=mod; } cout<<((long long)ans*(k+1)-1)%mod<<endl; } return 0; }
// // // File Creation Date: // Author: Gourav(https://github.com/GouravKhunger) #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef vector<ll> vll; typedef set<int> si; typedef priority_queue<int> pq; typedef priority_queue<int,vector<int>,greater<int>> pqs; #define F first #define S second #define PB push_back #define MP make_pair #define FOR(i, a, b) for (int i=a; i<=b; i++) #define FORn(i, a, b) for (int i=a; i>=b; i--) #define all(v) v.begin(), v.end() #define allR(v) v.rbegin(), v.rend() #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 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__) const int MOD = 1e9+7; const ll INF = 1e15; int main() { FIO; //start here int a, b, c; cin>>a>>b>>c; cout<<(7-a)+(7-b)+(7-c); //end here return 0; }
//----AUTHOR:kkdrummer----/ #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //#include <boost/multiprecision/cpp_int.hpp> //using namespace boost::multiprecision; using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef long double ld; typedef unordered_set<ll> usll; typedef unordered_multiset<ll> umsll; typedef multiset<ll> msll; typedef set<ll> sll; typedef vector<ll> vll; typedef pair<ll,ll> pll; typedef vector<pll> vpll; typedef priority_queue<ll> pqll; typedef vector<int> vi; typedef set<int> si; typedef multiset<int> msi; typedef unordered_multiset<int> umsi; typedef unordered_set<int> usi; typedef pair<int,int> pi; typedef vector<pi> vpi; typedef priority_queue<int> pqi; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_sll; typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi; typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_msll; #define in insert #define fi first #define se second #define pb push_back #define mp make_pair #define be begin #define en end #define itr iterator #define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define mo 1000000007 #define inf 8222372026854775807 #define ninf -inf #define ima 2047483647 #define imi -ima #define oncnt __builtin_popcount #define zerobegin __builtin_clz #define zeroend __builtin_ctz #define parity __builtin_parity #define eps 1e-9 #define coutd cout<<setprecision(10)<<fixed #define mems(dp,x) memset(dp,x,sizeof(dp)) #define fbo find_by_order #define ook order_of_key #define all(x) x.be(),x.en() #define upb upper_bound #define lowb lower_bound #define lte(v,x) (upb(all(v),x)-v.be()) #define gte(v,x) (v.end()-lowb(all(v),x)) #define gt(v,x) (v.en()-upb(all(v),x)) #define lt(v,x) (lowb(all(v),x)-v.be()) const ld PI= 3.1415926535897932384626433832792884197169399375105820974944; inline ll mpow(ll x,ll n){if(n==0)return 1;if(x==0)return 0;if(n==1)return(x%mo);ll u=(mpow(x,n/2));u=(u*u)%mo;if(n%2!=0)u=(u*x%mo)%mo;return u;} inline ll minv(ll x){return mpow(x,mo-2);} inline ll mmul(ll a,ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);} inline ll madd(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a+b>=mo)return(a+b)%mo;return(a+b);} inline ll msub(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;return(((a-b)%mo+mo)%mo);} inline ll mdiv(ll a,ll bb){if(a>=mo)a=a%mo;ll b=minv(bb);if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);} inline ll gcd(ll a,ll b){return __gcd(a,b);} inline ll lcm(ll a,ll b){return (a*b)/gcd(a,b);} // printf("%.0Lf",ans); // for printing 10^19 order ld numbers int main() { io int testcases=1; // cin>>testcases; while(testcases--) { int a,b,c; cin>>a>>b>>c; cout<<21-a-b-c; }return 0;}
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (int)(n); i++) typedef long long ll; const int INF = 1000100; const ll mod=1e9+7; typedef pair<int,int> P; const double pi=acos(-1); int main() { int n; cin>>n; vector<ll> a(n); rep(i,n)cin>>a[i]; ll ans=1e17; rep(i,1<<(n-1)){ ll o=a[0]; ll x=0; rep(j,n-1){ if(i>>j&1){ o|=a[j+1]; } else { x^=o; o=a[j+1]; } } x^=o; ans=min(ans,x); } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<int> A(N); for(int i=0;i<N;i++) cin >> A[i]; vector<int> pow2(31,1); for(int i=1;i<=30;i++) pow2[i] = pow2[i-1] * 2; int ans = pow2[30]; for(int i=0;i<(1<<(N-1));i++){ bitset<20> B(i); vector<int> XOR(30,0), nowOR(30,0); for(int j=0;j<N;j++){ for(int k=0;k<30;k++){ if((A[j]/pow2[k])%2 == 1) nowOR[k] = 1; } if(B.test(j)){ for(int k=0;k<30;k++){ if((nowOR[k]+XOR[k])%2==1) XOR[k] = 1; else XOR[k] = 0; nowOR[k] = 0; } } } for(int k=0;k<30;k++){ if((nowOR[k]+XOR[k])%2==1) XOR[k] = 1; else XOR[k] = 0; nowOR[k] = 0; } int score = 0; for(int k=0;k<30;k++){ if(XOR[k]==1) score += pow2[k]; } ans = min(ans,score); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; #define rep(i,n) for(int i = 0; i < (n); i++) using ll = long long; using P = pair<int,int>; #define ALL(a) (a).begin(),(a).end() int main(){ string n; cin >> n; while(true){ if(n.back() == '0'){ int len = n.size(); n.erase(len-1,1); } else break; } int len = n.size(); bool ok = true; rep(i,len/2){ if(n[i] != n[len - i - 1]){ ok = false; } } if(ok) cout << "Yes" << endl; else cout << "No" << endl; }
#include<bits/stdc++.h> #include<algorithm> #include<cmath> #include<climits> using namespace std; typedef long long int lli; typedef vector<int> vi; typedef vector<long long int> vlli; typedef pair<int,int> pii; typedef pair<long long int,long long int> plli; typedef vector< vi > vvi ; typedef vector< vlli > vvlli ; #define fi(i,a,b) for(int i=a;i<=b;i++) #define flli(i,a,b) for(long long int i=a;i<=b;i++) #define bi(i,a,b) for(int i=a;i>=b;i--) #define blli(i,a,b) for(long long int i=a;i>=b;i--) #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define all(x) x.begin(),x.end() #define sz(x) x.size() #define pi 2*acos(0.0) #define pb push_back #define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++) #define present(v,num) (v.find(num)!=v.end()) #define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end()) #define pq priority_queue #define mp make_pair const int inf=INT_MAX; const lli INF =LLONG_MAX; const lli mod = 1e9+7; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast; lli n;cin>>n; vlli digs; bool flag=false; while(n>0) { int temp=n%10; n=n/10; if(temp==0 && !flag)continue; flag=true; digs.pb(temp); } int l=0,r=sz(digs)-1; bool ans=true; while(l<r) { if(digs[l]!=digs[r])ans=false; l++; r--; } if(ans)cout<<"Yes\n"; else cout<<"No\n"; cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <algorithm> #include <iomanip> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <string> #include <sstream> #include <unordered_map> #include <unordered_set> #include <utility> #include <numeric> #include <vector> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif #define GET_MACRO(_1, _2, _3, NAME, ...) NAME #define _rep(i, n) _rep2(i, 0, n) #define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++) #define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using i64 = long long; template<class T, class U> bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; } template<class T, class U> bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; } template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;} template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);} template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;} template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;} template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;} template<class T> using pq = priority_queue<T, vector<T>, greater<T>>; struct C { int x, y; C(int x = 0, int y = 0) : x(x), y(y) {} C operator*(const C& a) const { return C(x * a.x - y * a.y, x * a.y + y * a.x); } C operator-(const C& a) const { return C(x - a.x, y - a.y); } bool operator<(const C& a) const { if (x == a.x) return y < a.y; return x < a.x; }; bool operator==(const C& a) const { return x == a.x && y == a.y; }; int norm() const { return x*x + y*y;} }; int main() { int n; cin >> n; vector<C> a(n), b(n); rep(i, n) { int x,y; cin >> x >> y; a[i] = C(x, y); } rep(i, n) { int x, y; cin >> x >> y; b[i] = C(x, y); } if(n == 1) { cout << "Yes" << endl; return 0; } rep(i, n) { rep(j, n) { // a[0] -> b[i] // a[1] -> b[j] if(i == j) continue; if((a[1] - a[0]).norm() != (b[j] - b[i]).norm()) continue; // 長さが違えばアウト auto f = [&](vector<C> p, C o, C r) { rep(i, n) p[i] = p[i] - o; // 平衡移動 rep(i, n) p[i] = p[i] * r; // 回転 sort(p.begin(), p.end()); return p; }; vector<C> na = f(a, a[0], b[j] - b[i]); // a[0] を原点にして,回転 vector<C> nb = f(b, b[i], a[1] - a[0]); // a[0] を原点にして,回転 if(na == nb) { cout << "Yes" << endl; return 0; } } } cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define mp make_pair #define pb push_back #define fi first #define se second #define sz(x) int(x.size()) const int mod = 1e9 + 7; const int inf = 2e9 + 5; const ll linf = 9e18 + 5; const ll ll0 = 0 * 1ll; int n; const int N = 1e2 + 5; ll arr[N][2]; ll equi[2]; ll arr2[N][2]; ll equi2[2]; pair<ll, ll> iden[N]; pair<ll, ll> examp[N]; ll cross (int i) { pair<ll, ll> v1 = mp(arr[0][0] - equi[0], arr[0][1] - equi[1]); pair<ll, ll> v2 = mp(arr[i][0] - equi[0], arr[i][1] - equi[1]); return v1.fi * v2.se - v1.se * v2.fi; } ll cross2 (int i, int c) { pair<ll, ll> v1 = mp(arr2[c][0] - equi2[0], arr2[c][1] - equi2[1]); pair<ll, ll> v2 = mp(arr2[i][0] - equi2[0], arr2[i][1] - equi2[1]); return v1.fi * v2.se - v1.se * v2.fi; } ll dist (int i) { ll ans = (equi[0] - arr[i][0]) * (equi[0] - arr[i][0]); ans += (equi[1] - arr[i][1]) * (equi[1] - arr[i][1]); return ans; } ll dist2 (int i) { ll ans = (equi2[0] - arr2[i][0]) * (equi2[0] - arr2[i][0]); ans += (equi2[1] - arr2[i][1]) * (equi2[1] - arr2[i][1]); return ans; } void init () { } void input () { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i][0] >> arr[i][1]; equi[0] += arr[i][0]; equi[1] += arr[i][1]; arr[i][0] *= n; arr[i][1] *= n; } for (int i = 0; i < n; i++) { cin >> arr2[i][0] >> arr2[i][1]; equi2[0] += arr2[i][0]; equi2[1] += arr2[i][1]; arr2[i][0] *= n; arr2[i][1] *= n; } for (int i = 0; i < n; i++) { ll d = dist(i); iden[i] = mp(cross(i), d); } sort(iden, iden + n); } void create_with(int ind) { for (int i = ind; i < ind + n; i++) { int j = i % n; ll d = dist2(j); examp[j] = mp(cross2(j, ind), d); } sort(examp, examp + n); } void solve() { for (int i = 0; i < n; i++) { create_with(i); bool good = true; for (int i = 0; i < n; i++) { if (examp[i] != iden[i]) { good = false; break; } } if (good) { cout << "Yes"; return; } } cout << "No"; } void output() { } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int number_of_testcases = 1; //cin >> number_of_testcases; while (number_of_testcases--) { init(); input(); solve(); output(); } return 0; }