code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1e9 + 7; const int64_t MOD2 = 998244353; const int INF = 1e9; long long dist(int i, int j, vector<long long> &x, vector<long long> &y, vector<long long> &z) { return abs(x.at(j) - x.at(i)) + abs(y.at(j) - y.at(i)) + max(0LL, z.at(j) - z.at(i)); } int main() { int n; cin >> n; vector<long long> x(n), y(n), z(n); for (int i=0; i<n; i++) { cin >> x.at(i) >> y.at(i) >> z.at(i); } vector<vector<long long>> dp(1<<n, vector<long long>(n+1, INF)); dp.at(1).at(0) = 0; for (int bit = 0; bit < (1<<n); bit++) { for (int v=0; v<n; v++) { if (!(bit & (1<<v))) continue; for (int nv=0; nv<n; nv++) { if (bit & (1<<nv)) continue; int nbit = bit | (1<<nv); dp.at(nbit).at(nv) = min(dp.at(nbit).at(nv), dp.at(bit).at(v) + dist(v, nv, x, y, z)); } } } long long ans = INF; for (int i=0; i<n; i++) { ans = min(ans, dp.at((1<<n) - 1).at(i) + dist(i, 0, x, y, z)); } cout << ans << endl; }
#include <bits/stdc++.h> #define var long long #define FOR(i, st, en) for (int i = st, _en = (en); i <= _en; ++i) #define REP(i, en) for (int i = 0, _en = (en); i < _en; ++i) #define FORD(i, st, en) for (int i = st, _en = (en); i >= _en; --i) #define MASK(n) (1 << (n)) #define BIT(x, i) (((x)>>(i)) & 1) #define endl "\n" #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define taskname "" #define file \ freopen(taskname".INP", "r", stdin); \ freopen(taskname".OUT", "w", stdout) using namespace std; typedef pair<int, int> ii; const int inf = 0x3f3f3f3f; const int mod = 1e9+7; template<class T> bool minimize(T& x, const T& y) { if (x > y) {x = y; return true;} return false; } template<class T> bool maximize(T& x, const T& y) { if (x < y) {x = y; return true;} return false; } // ---------------------------------------------------------------------------------- #define maxn 20 struct Point {int x, y, z;}; int caldist(const Point &a, const Point &b) { return abs(a.x-b.x) + abs(a.y-b.y) + max(0, b.z-a.z); } int n; Point a[maxn]; int dist[maxn][maxn]; int x[maxn], cost[maxn]; bool check[maxn]; int dp[maxn][MASK(maxn)]; int res = inf; // ---------------------------------------------------------------------------------- void sub1() { vector<int> vertex(n-1); iota(vertex.begin(), vertex.end(), 2); do { int cur = 0; int last = 1; REP(i, vertex.size()) { cur += dist[last][vertex[i]]; last = vertex[i]; } cur += dist[last][1]; minimize(res, cur); } while (next_permutation(vertex.begin(), vertex.end())); cout << res << endl; } void recursion(int i) { FOR(j, 2, n) if (!check[j]) { x[i] = j; cost[i] = cost[i-1]+dist[x[i-1]][j]; if (cost[i] < res) { if (i < n) { check[j] = true; recursion(i+1); check[j] = false; } else minimize(res, cost[n]+dist[x[n]][1]); } } } void sub2() { check[1] = true; x[1] = 1; cost[1] = 0; recursion(2); cout << res << endl; } int cal(int i, int s) { if (__builtin_popcount(s) == n) return dist[i][1]; if (dp[i][s] > -1) return dp[i][s]; int tmp = inf; FOR(j, 2, n) if (BIT(s, j) == 0) minimize(tmp, cal(j, s|MASK(j))+dist[i][j]); return dp[i][s] = tmp; } void sub3() { memset(dp, -1, sizeof dp); cout << cal(1, 2) << endl; } void test_case() { cin >> n; FOR(i, 1, n) cin >> a[i].x >> a[i].y >> a[i].z; FOR(i, 1, n) FOR(j, 1, n) if (i != j) dist[i][j] = caldist(a[i], a[j]); sub3(); } int main() { fastio; // file; int test = 1; // scanf("%d", &test); while (test--) test_case(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int sz = 2e5+5; typedef pair <int,int> pii; #define x first #define y second int csub[sz], dp[sz]; vector <int> g[sz]; void dfs(int u) { csub[u] = dp[u] = 1; vector <pii> od, ev; for(int v : g[u]) { dfs(v); csub[u] += csub[v]; if(csub[v] & 1) od.emplace_back(csub[v] - 2 * dp[v], v); else ev.emplace_back(csub[v] - 2 * dp[v], v); } sort(od.begin(), od.end(), greater<pii>()); sort(ev.begin(), ev.end(), greater<pii>()); int ei = 0, oi = 0; for(; ei<ev.size(); ei++) { auto v = ev[ei].y; if(csub[v] - dp[v] > dp[v]) dp[u] += dp[v]; else break; } for(; oi<od.size(); oi++) { auto v = od[oi].y; if(oi & 1) dp[u] += csub[v] - dp[v]; else dp[u] += dp[v]; } for(; ei<ev.size(); ei++) { auto v = ev[ei].y; if(oi & 1) dp[u] += csub[v] - dp[v]; else dp[u] += dp[v]; } } int main() { int n; cin >> n; for(int i=2; i<=n; i++) { int j; scanf("%d", &j); g[j].push_back(i); } dfs(1); cout << dp[1]; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int P[100001]; int sei[100001], fu[100001], kisuu[100001], kazu[100001], kazu2[100001]; void sort_ganbare(int N, int A[]) { if (N < (1 << 12)) { sort(A, A + N); return; } const int b = 9; int tmp[100001]; int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] & ((1 << b) - 1)]] = A[i]; rep(i, N) kazu2[tmp[i] >> b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> b & ((1 << b) - 1)]] = tmp[i]; } const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); rep1(i, N - 1) { kazu[(P[i + 1] = getint())]++; } rep(i, N) kazu2[i + 1] = (kazu[i + 1] += kazu[i]); for (int i = N; i >= 2; i--) { int ret = sei[i] - 1; auto ki = kisuu + kazu[i - 1]; int sz = kazu2[i - 1] - kazu[i - 1]; sort_ganbare(sz, ki); if (sz & 1) { ret -= fu[i]; rep(j, sz) { if (j & 1) ret -= ki[j] - (1 << 17); else ret += ki[j] - (1 << 17); } } else { ret += fu[i]; rep(j, sz) { if (j & 1) ret += ki[j] - (1 << 17); else ret -= ki[j] - (1 << 17); } } if (ret & 1) { kisuu[kazu2[P[i] - 1]++] = ret + (1 << 17); } else { if (ret >= 0) sei[P[i]] += ret; else fu[P[i]] += ret; } } int ret = sei[1] - 1; auto ki = kisuu + kazu[0]; int sz = kazu2[0] - kazu[0]; sort_ganbare(sz, ki); if (sz & 1) { ret -= fu[1]; rep(j, sz) { if (j & 1) ret -= ki[j] - (1 << 17); else ret += ki[j] - (1 << 17); } } else { ret += fu[1]; rep(j, sz) { if (j & 1) ret += ki[j] - (1 << 17); else ret -= ki[j] - (1 << 17); } } co((N - ret) / 2); Would you please return 0; }
#include <bits/stdc++.h> typedef long double ld; #define int long long #define gcd __gcd #define endl "\n" #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define mod2 998244353 #define maxe *max_element #define mine *min_element #define inf 1e18 #define pb push_back #define all(x) x.begin(), x.end() #define f first #define s second #define lb lower_bound #define ub upper_bound #define ins insert #define sz(x) (int)(x).size() #define mk make_pair #define deci(x, y) fixed<<setprecision(y)<<x #define w(t) int t; cin>>t; while(t--) #define nitin ios_base::sync_with_stdio(false); cin.tie(nullptr) #define PI 3.141592653589793238 #define mem0(x) memset(x,0,sizeof x) #define mem1(x) memset(x,-1,sizeof x) using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.f << ", " << 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 NITIN #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif int dp[3001][3001]; int sum[3001][3001]; int last[3001][3001]; int n; int f(int i,int j) { if (i == -1) return 0; if (i == n+1) return 1; if (dp[i][j] != -1) return dp[i][j]; int op1=0,op2=0; if(last[j][i]!=n+1) op1 = f(last[j][i], j); op2 = f(last[j][i], j + 1); return dp[i][j]=(op1 + op2) % mod; } void solve() { cin>>n; int a[n+1]; for(int i=0;i<n;i++) { cin >> a[i+1]; } for(int i =1;i<=n;i++) { int tot = 0; for (int j = n; j >=1; j--) { tot += a[j]; tot %= i; sum[i][j] = tot; } } memset(last,-1,sizeof last); memset(dp,-1,sizeof dp); for(int i=1;i<=n;i++) { map<int, int> m; for (int j = n; j >= 1; j--) { if (m.count(sum[i][j])) { last[i][j] = m[sum[i][j]] ; }else if(sum[i][j]==0){ last[i][j]=n+1; } m[sum[i][j]] = j; } } cout<<f(1,1)<<endl; } int32_t main() { nitin; solve(); }
#include<bits/stdc++.h> using namespace std; using ll = long long int; signed main(void){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; int m=pow(2,n); vector<pair<ll,int>> a(m); for(int i=0;i<m;i++){ cin>>a[i].first; a[i].second=i; } if(n==1){ if(a[0].first<a[1].first){cout<<1; return 0;} else{ cout<<2; return 0; } } int ans=0; for(int i=1;i<=n-1;i++) { vector<pair<ll,int>> b; for(int j=0;j<m;j+=2){ if(a[j]>a[j+1]){ b.push_back(a[j]); } else{ b.push_back(a[j+1]); } } m=b.size(); a=b; if(i==n-1){ if(a[0].first<a[1].first){ ans=a[0].second; } else { ans=a[1].second; } } } cout<<ans+1; cout<<"\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #define A(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define long long long void _read(); char grid[101][101]; const int dx[] = {1,-1,0,0}; const int dy[] = {0,0,1,-1}; int _count( int h, int w) { long cnt = 0; for ( int i = 0; i < h; i++) { for( int j = 0; j < w; j++) { if(grid[i][j] == '#') continue; for( int d = 0; d < 4; ++d) { int x = i+dx[d], y = j+dy[d]; if( x < 0 or y < 0 or x > h-1 or y > w-1 ) continue; if(grid[x][y] == '#') continue; cnt++; } } } return (cnt >> 1); } int main () { _read(); int h,w; cin >> h >> w; for( int i = 0; i < h; i++) { for( int j = 0; j < w; j++) { cin >> grid[i][j]; } } cout << _count(h,w) << "\n"; return 0; }; void _read() { ios_base :: sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL freopen("input.txt","r",stdin); #endif }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define MP make_pair #define REP(i,a,b) for (int i = a; i <= b; i++) #define REP_REV(i,a,b) for (int i = a; i >= b; i--) #define what_is(x) cerr << #x << " is " << x << endl; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<ll> vl; typedef pair<ll, ll> pl; typedef map<string, ll> msl; ll MOD = 1e9 + 7; struct Rect { ll x1 = 0, y1 = 0, x2 = 0, y2 = 0; ll area() { return (x2 - x1) * (y2 - y1); } }; Rect intersect(Rect p, Rect q) { Rect inter; inter.x1 = max(p.x1, q.x1); inter.y1 = max(p.y1, q.y1); inter.x2 = min(p.x2, q.x2); inter.y2 = min(p.y2, q.y2); if (inter.x1 >= inter.x2 || inter.y1 >= inter.y2) { Rect empty; return empty; } return inter; } bool iscovered(int x, int y, int x1, int y1, int x2, int y2) { return (x <= x2 && x >= x1 && y <= y2 && y >= y1); } bool allzero(vi a) { REP(i, 0, (int) a.size() - 1) { if (a[i] != 0) return false; } return true; } int isvalidTriange(int x1, int y1, int x2, int y2, int x3, int y3) { int height, width; if (x1 == x2) height = abs(y1 - y2); else if (x1 == x3) height = abs(y1 - y3); else if (x2 == x3) height = abs(y2 - y3); else height = 0; if (y1 == y2) width = abs(x1 - x2); else if (y1 == y3) width = abs(x1 - x3); else if (y2 == y3) width = abs(x2 - x3); else width = 0; return(height*width); } void solve() { /*int n; cin >> n; vector<pair<int, int>> pts; REP(i, 0, n - 1) { int x, y; cin >> x >> y; pair<int, int> p = make_pair(x, y); pts.PB(p); } int area = 0; REP(i, 0, n - 1){ REP(j, i + 1, n - 1) { REP(k, j + 1, n - 1 ) { area = max(area, isvalidTriange(pts[i].F, pts[i].S, pts[j].F, pts[j].S, pts[k].F, pts[k].S)); } } } cout << area;*/ int h, w; cin >> h >> w; char grid[h][w]; REP(i, 0, h - 1) { REP(j, 0, w - 1) { cin >> grid[i][j]; } } int poss = 0; REP(i, 0, h - 1) { REP(j, 0, w - 1) { if (i != (h - 1)) if (grid[i][j] == '.' && grid[i + 1][j] == '.') poss++; if (i != 0) if (grid[i][j] == '.' && grid[i - 1][j] == '.') poss++; if (j != w - 1) if (grid[i][j] == '.' && grid[i][j + 1] == '.') poss++; if (j != 0) if (grid[i][j] == '.' && grid[i][j - 1] == '.') poss++; } } cout << poss/2; } int main() { ios::sync_with_stdio(0); cin.tie(0); //freopen("triangles.in", "r", stdin); //freopen("triangles.out", "w", stdout); //ll t; cin >> t; //while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i) #define ALL(v) (v).begin(),(v).end() #define CLR(t,v) memset(t,(v),sizeof(t)) template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";} template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;} template<class T>void chmin(T&a,const T&b){if(a>b)a=b;} template<class T>void chmax(T&a,const T&b){if(a<b)a=b;} ll nextLong() { ll x; scanf("%lld", &x); return x;} int main2() { int N = nextLong(); vector<ll> X(N), Y(N); REP(i, N) { X[i] = nextLong(); Y[i] = nextLong(); } int M = nextLong(); vector<ll> dirX(M+1), dirY(M+1), offsetX(M+1), offsetY(M+1); dirX[0] = 0; dirY[0] = 1; REP(i, M) { int t = nextLong(); if (t == 2) { dirX[i+1] = (dirY[i] + 2) % 4; dirY[i+1] = (dirX[i] + 0) % 4; offsetX[i+1] = -offsetY[i]; offsetY[i+1] = offsetX[i]; } if (t == 1) { dirX[i+1] = (dirY[i] + 0) % 4; dirY[i+1] = (dirX[i] + 2) % 4; offsetX[i+1] = offsetY[i]; offsetY[i+1] = -offsetX[i]; } if (t == 3) { int p = nextLong(); dirX[i+1] = (dirX[i] + 2) % 4; dirY[i+1] = dirY[i]; offsetX[i+1] = -offsetX[i] + 2 * p; offsetY[i+1] = offsetY[i]; } if (t == 4) { int p = nextLong(); dirX[i+1] = dirX[i]; dirY[i+1] = (dirY[i] + 2) % 4; offsetX[i+1] = offsetX[i]; offsetY[i+1] = -offsetY[i] + 2 * p; } } int Q = nextLong(); REP(i, Q) { int A = nextLong(); int B = nextLong() - 1; ll x = dirX[A] == 0 ? X[B] : dirX[A] == 1 ? Y[B] : dirX[A] == 2 ? -X[B] : -Y[B]; ll y = dirY[A] == 0 ? X[B] : dirY[A] == 1 ? Y[B] : dirY[A] == 2 ? -X[B] : -Y[B]; x += offsetX[A]; y += offsetY[A]; cout << x << " " << y << '\n'; } return 0; } int main() { #ifdef LOCAL for (;!cin.eof();cin>>ws) #endif main2(); return 0; }
#include <bits/stdc++.h> using namespace std ; const int MAX = 2e5 + 10 ; int n , m , q ; long long x[MAX] , y[MAX] ; int op[MAX] , p[MAX] ; vector< pair<int , int> >queries[MAX] ; long long ansx[MAX] , ansy[MAX] ; int sign[2] = {1 , 1} ; int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n ; for(int i = 0 ; i < n ; ++i) cin>>x[i]>>y[i] ; cin>>m ; for(int i = 0 ; i < m ; ++i) { cin>>op[i] ; if(op[i] > 2) cin>>p[i] ; } cin>>q ; for(int i = 0 ; i < q ; ++i) { int x , y ; cin>>x>>y ; queries[x].emplace_back(y-1 , i) ; } long long a = 0 , b = 0 ; bool flag = false ; for(int i = 0 ; i <= m ; ++i) { for(auto &qu : queries[i]) { int idx1 = qu.first , idx2 = qu.second ; if(!flag) ansx[idx2] = sign[0] * x[idx1] + a , ansy[idx2] = sign[1] * y[idx1] + b ; else ansx[idx2] = sign[0] * y[idx1] + a , ansy[idx2] = sign[1] * x[idx1] + b ; } if(i == m) break ; if(op[i] <= 2) { flag = !flag ; if(op[i] == 1) a *= -1 , sign[0] *= -1 ; else b *= -1 , sign[1] *= -1 ; swap(a , b) ; swap(sign[0] , sign[1]) ; } else { if(op[i] == 3) a = 2 * p[i] - a , sign[0] *= -1 ; else b = 2 * p[i] - b , sign[1] *= -1 ; } } for(int i = 0 ; i < q ; ++i) cout<<ansx[i]<<" "<<ansy[i]<<"\n" ; return 0 ; }
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<set> #include<queue> using namespace std; typedef long long ll; const int MAXN=5e5+5; template<typename T> void chkmax(T& x,T y){if(y>x)x=y;} template<typename T> void chkmin(T& x,T y){if(y<x)x=y;} bool equal(double a,double b){return fabs(a-b)<1e-7;} int N; struct node{ double x,y; bool operator<(const node& t)const{ if(equal(x,t.x))return y<t.y; return x<t.x; } double len(){return sqrt(x*x+y*y);} }a[MAXN],b[MAXN],c[MAXN],d[MAXN]; double dot(node a,node b){return a.x*b.x+a.y*b.y;} double cross(node a,node b){return a.x*b.y-a.y*b.x;} int main(){ #ifndef ONLINE_JUDGE freopen("classic1.in","r",stdin); freopen("classic1.out","w",stdout); #endif scanf("%d",&N);int x,y; if(N==1){puts("Yes");return 0;} for(int i=1;i<=N;++i)scanf("%d%d",&x,&y),a[i].x=x,a[i].y=y; for(int i=2;i<=N;++i)a[i].x-=a[1].x,a[i].y-=a[1].y;a[1].x=a[1].y=0; for(int i=1;i<=N;++i)scanf("%d%d",&x,&y),b[i].x=x,b[i].y=y; for(int i=1;i<=N;++i){ for(int j=1;j<=N;++j)if(j!=i)b[j].x-=b[i].x,b[j].y-=b[i].y;b[i].x=b[i].y=0; for(int j=1;j<=N;++j)d[j].x=b[j].x,d[j].y=b[j].y; sort(d+1,d+N+1); for(int j=1;j<=N;++j)if(j!=i&&equal(a[2].len(),b[j].len())){ double sintheta=cross(a[2],b[j])/(a[2].x*a[2].x+a[2].y*a[2].y); double costheta=dot(a[2],b[j])/(a[2].x*a[2].x+a[2].y*a[2].y); for(int k=1;k<=N;++k){ c[k].x=a[k].x*costheta-a[k].y*sintheta; c[k].y=a[k].x*sintheta+a[k].y*costheta; } sort(c+1,c+N+1);bool flag=1; for(int k=1;k<=N;++k)if(!equal(c[k].x,d[k].x)||!equal(c[k].y,d[k].y)){flag=0;break;} if(flag){puts("Yes");return 0;} } } puts("No"); return 0; }
#include <bits/stdc++.h> /*... ... author @ Aman Kothari ..... .....*/ using namespace std; #define ll long long #define dl double /* Sieve Of Erasthosis to find least prime factor */ vector<ll> least_prime(1000001, 0); void leastPrimeFactor(ll n) { least_prime[1] = 1; for (ll i = 2; i <= n; i++) { if (least_prime[i] == 0) { least_prime[i] = i; for (ll j = 2*i; j <= n; j += i) if (least_prime[j] == 0) least_prime[j] = i; } } } /* Sieve Of Erasthosis to check if number is a prime number */ bool prime[1000000+1]; void isprime(ll n) { memset(prime, true, sizeof(prime)); prime[1]=false; for (ll p=2; p*p<=n; p++) { if (prime[p] == true) { for (ll i=p*p; i<=n; i += p) prime[i] = false; } } } /* Modular exponentiation */ ll power(ll x, ll y,ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } /* Variable declaration */ map<char,ll> mc; map<ll,ll> ml; vector<ll> v; vector<char> vc; void clean(){ v.clear(); vc.clear(); mc.clear(); ml.clear(); } ll mod=1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); //leastPrimeFactor(1e6); // isprime(1e6); ll tc; //cin>>tc; tc=1; ll n=0,k=0; string s; while(tc--){ clean(); cin>>n; ll b[n],a[n]; for(ll i=0;i<n;i++){ cin>>a[i]>>b[i]; } ll ans=0; for(ll i=0;i<n;i++){ // cout<<i<<"\n"; ll temp=(((b[i]-a[i]+1))*(a[i]+b[i]))/2; // cout<<a[i]<<" "<<b[i]<<" "<<temp<<"\n"; ans+=temp; } cout<<ans<<'\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define endl "\n" #define F first #define S second #define pb push_back #define all(a) (a).begin(), (a).end() #define debug(a) cout << #a << " = " << a << " "; template<typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; const int MOD=1000000007; const int N=100005; void solve(){ int n, m; cin >> n >> m; if(n == 1 and m == 0){ cout << 1 << " " << 3; return; } if(m < 0 or m >= n-1) { cout << "-1"; return; } int k = m; vector < pair < int, int > > ans(n); ans[0].first = 2; ans[0].second = 2*n+m-1; for(int i=1; i<n-1; ++i){ if(k > 1){ ans[i].first = ans[i-1].second+1; ans[i].second = ans[i].first+1; }else if(k == 1){ ans[i].first = ans[i-1].second+1; ans[i].second = ans[i].first+n-m-1; }else{ ans[i].first = ans[i+k-1].second+k-1; ans[i].second = ans[i+k-1].second-k+1; } --k; } ans[n-1].first = 1; ans[n-1].second = (int)1e9; for(auto x: ans) cout << x.first << " " << x.second << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long int t=1; // cin>>t; // DON'T FORGET IT. ^^^ for(long int tt=1;tt<=t;tt++){ solve(); } cerr << "Time : " << 1000 * ((double) clock()) / (double) CLOCKS_PER_SEC << "ms\n"; return 0; }
#ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; using PIL = pair<int,ll>; using PLI = pair<ll,int>; using PLL = pair<ll,ll>; using Graph = vector<vector<int>>; using Cost_Graph = vector<vector<PIL>>; template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;} template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;} #define REP(i,n) for(int i=0;i<int(n);i++) #define ROUNDUP(a,b) ((a+b-1)/b) #define YESNO(T) cout<<(T?"YES":"NO")<<endl #define yesno(T) cout<<(T?"yes":"no")<<endl #define YesNo(T) cout<<(T?"Yes":"No")<<endl const int INFint = 1 << 30; const ll INFLL = 1LL << 60; const ll MOD = 1000000007LL; const double pi = 3.14159265358979; const vector<int> h_idx = {-1, 0,0,1}; const vector<int> w_idx = { 0,-1,1,0}; int si,sj; int now_i,now_j; vector<vector<int>> tile(50, vector<int>(50)); vector<vector<int>> profit(50, vector<int>(50)); vector<bool> seen(2505,false); string ans; string all_ans; ll best_score; clock_t start_time; clock_t now_time; mt19937 mt((int)time(0)); bool check_time_limit(){ now_time = clock(); if ((double)(now_time - start_time) / CLOCKS_PER_SEC > 1.95) return true; else return false; } // ๅฎŸ่กŒๆ™‚้–“ใŒ1.95็ง’ใ‚’่ถŠใ—ใŸใ‚‰true,ใใ‚Œใพใงใฏfalse void all_resize(){ return; } void all_input(){ cin >> si >> sj; all_resize(); for (int i = 0; i < 50; i++){ for (int j = 0; j < 50; j++){ cin >> tile[i][j]; } } for (int i = 0; i < 50; i++){ for (int j = 0; j < 50; j++){ cin >> profit[i][j]; } } seen[tile[si][sj]] = true; return; } ll calc_score(string &s){ int tmp_now_i = si,tmp_now_j = sj; ll res = profit[si][sj]; for (auto x : s){ if (x == 'U'){ tmp_now_i += h_idx[0]; tmp_now_j += w_idx[0]; } else if (x == 'L'){ tmp_now_i += h_idx[1]; tmp_now_j += w_idx[1]; } else if (x == 'R'){ tmp_now_i += h_idx[2]; tmp_now_j += w_idx[2]; } else{ tmp_now_i += h_idx[3]; tmp_now_j += w_idx[3]; } res += profit[tmp_now_i][tmp_now_j]; } return res; } void all_output(){ cout << all_ans << endl; return; } bool check_range_and_seen(int i,int j){ if (i < 0 || i >= 50 || j < 0 || j >= 50) return false; if (seen[tile[i][j]]) return false; else return true; } bool random_walk(int &i,int &j){ if (check_time_limit()) return false; vector<int> possible_direction; for (int k = 0; k < 4; k++){ int tmp_next_i = i + h_idx[k]; int tmp_next_j = j + w_idx[k]; if (check_range_and_seen(tmp_next_i,tmp_next_j)) possible_direction.push_back(k); } if (possible_direction.size() == 0) return false; int num = mt() % (possible_direction.size()); int direction = possible_direction[num]; int next_i = i + h_idx[direction]; int next_j = j + w_idx[direction]; if (direction == 0){ ans.push_back('U'); } else if (direction == 1) ans.push_back('L'); else if (direction == 2) ans.push_back('R'); else ans.push_back('D'); i = next_i; j = next_j; seen[tile[i][j]] = true; return true; } void random_walk_all(){ ans = ""; now_i = si; now_j = sj; while (random_walk(now_i,now_j)); if (chmax(best_score,calc_score(ans))){ // cout << best_score << endl; all_ans = ans; } return; } void solve(){ ll counter = 0; while (!check_time_limit()){ random_walk_all(); counter++; } // cout << counter << endl; return; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); all_input(); solve(); all_output(); } // ไนฑๆŠž
#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<ctime> #include<map> #include<vector> #include<math.h> #include<stdio.h> #include<stack> #include<queue> #include<tuple> #include<cassert> #include<set> #include<bitset> #include<functional> #include <fstream> //#include<bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i, x) for(ll i = 0; i < x; i++) #define rep2(i, x) for(ll i = 1; i <= x; i++) #define rep3(i, x, y) for(ll i = x; i < y; i++) #define rep4(i, x) for(ll i = x; i >= 0; i--) #define all(a) (a).begin(),(a).end() #define puts(x) cout << (x) << "\n" using ll = long long; using ld = long double; using namespace std; const ll INF = 1000000000000000000; const int intINF = 1000000000; const ll mod = 1000000007; const ll MOD = 998244353; const ld pi = 3.141592653589793238; //const ld EPS = 1e-9; bool isprime(int p) { if (p == 1) return false; for (int i = 2; i < p; i++) { if (p % i == 0) return false; } return true; } ll gcd(ll a, ll b) { if (b == 0) { return a; } return gcd(b, a % b); } //ax + by = cใŒๆ•ดๆ•ฐ่งฃใ‚’ใ‚‚ใคใŸใ‚ใฎๅฟ…่ฆๅๅˆ†ๆกไปถใฏ c ใŒ gcd(a,b) ใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ใ“ใจใ€‚ // ่ฟ”ใ‚Šๅ€ค: a ใจ b ใฎๆœ€ๅคงๅ…ฌ็ด„ๆ•ฐ // ax + by = gcd(a, b) ใ‚’ๆบ€ใŸใ™ (x, y) ใŒๆ ผ็ดใ•ใ‚Œใ‚‹ //main้–ขๆ•ฐๅ†…ใซ extGCD(a, b, x, y); ใงx, yใซ่งฃใŒๆ ผ็ด ll extGCD(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll keta(ll n) { ll res = 0; while (n >= 1) { res += n % 10; n /= 10; } return res; } ll modpow(ll x, ll y, ll m) { ll res = 1; while (y) { if (y % 2) { res *= x; res %= m; } x = x * x % m; y /= 2; } return res; } ll kaijyo[600005]; void nCkinit(ll n, ll m) { ll cnt = 1; kaijyo[0] = 1; for (int h = 1; h <= n; h++) { cnt *= h; cnt %= m; kaijyo[h] = cnt; } } ll nCk(ll n, ll k, ll m) { /*ll a = 1, b = 1; for (int h = 1; h <= n; h++) { a *= h; a %= m; } for (int h = 1; h <= k; h++) { b *= h; b %= m; } for (int h = 1; h <= n - k; h++) { b *= h; b %= m; }*/ ll a = kaijyo[n], b = kaijyo[k] * kaijyo[n - k] % m; return a * modpow(b, m - 2, m) % m; } //printf("%.10f\n", n); typedef pair <ll, ll> P; typedef pair <ll, string> pp; ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; struct edge { ll to, cost; }; struct Point { ll x, y; }; bool operator<(const Point& a1, const Point& a2) { if (a1.x < a2.x) return true; if (a1.x > a2.x) return false; if (a1.y < a2.y) return true; return false; } struct status { ll c; ll x; ll y; ll d; bool operator<(const status& rhs) const { return c < rhs.c; }; bool operator>(const status& rhs) const { return c > rhs.c; }; }; int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); //cout << fixed << setprecision(15); //input ll n, cnt = 0; cin >> n; rep2(i, 1000000) { cnt += i; if (cnt >= n) { cout << i << endl; return 0; } } return 0; }
#include <iostream> #include <cstdio> using namespace std; int t[210000]; int main(){ int a;scanf("%d",&a); int at=0; for(int i=0;i<a;i++){ int s;scanf("%d",&s); t[s]++; while(t[at]){ at++; } printf("%d\n",at); } }
#include<bits/stdc++.h> #define fr first #define sc second #define pb push_back #define ll long long #define maxheap priority_queue<int> #define minheap priority_queue<int,vector<int>,greater<int>> #define all(v) (v).begin(),(v).end() const double pi = acos(-1.0); const double eps = 1e-12; using namespace std; const int N = 1e8 + 10; int a[N][2]; unordered_map <int, ll> t[4]; ll sum(int i, int id) { ll res = 0; for (; i >= 0; i -= (i + 1) & -(i + 1)) if (t[id].count(i)) res += t[id][i]; return res; } void inc(int i, int value, int id) { for (; i < N; i += (i + 1) & -(i + 1)) t[id][i] += value; } ll sum (int l, int r, int id){ ll res = 0; if (r > 0) res = sum (r, id); if (l - 1 >= 0) res -= sum (l - 1, id); return res; } int n, m, q; void solve(){ cin >> n >> m >> q; inc(0, n, 0); inc(0, m, 1); ll ans = 0; while (q--){ int type, x, y; cin >> type >> x >> y; type--; inc(a[x][type], -1, type); inc(y, 1, type); inc(a[x][type], -a[x][type], type + 2); inc(y, y, type + 2); ll prefSumx = sum(0, a[x][type], type ^ 1); ll prefSumy = sum(0, y, type ^ 1); ll delta = sum(a[x][type] + 1, y, (type + 2) ^ 1); ans -= prefSumx * a[x][type]; ans += prefSumy * y; ans -= delta; a[x][type] = y; //cout << ans << " " << prefSumx << " " << prefSumy << " " << delta << endl; cout << ans << "\n"; } //cout << ans << endl; } int main(){ ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); cout.setf(ios::fixed), cout.precision(6); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); //freopen("exercise.in","r",stdin); freopen("exercise.out", "w", stdout); int step; step = 1; //cin >> step; for (int i = 1; i <= step; i++){ solve(); } }
#pragma GCC optimize("O3") #pragma GCC target("avx512f") #include <algorithm> #include <iostream> #include <bitset> #include <vector> #include <array> using namespace std; #define f(x,y,z) for(int x=y;x<=z;++x) vector<int> v[200001]; int n, a, b, it; array<int, 200001> jr, dlm, nmr; bitset<200001> sdh; void jrk(int x) { for(int &i:v[x]) if(!jr[i]) { jr[i] = jr[x]+1, jrk(i); } } void trn(int x) { sdh[x] = true; for(int &i:v[x]) if(!sdh[i]) { trn(i), dlm[x] = max(dlm[x], dlm[i]+1); } } void isi(int x) { sdh[x] = true, nmr[x] = a; vector<pair<int, int>> mn; for(int &i:v[x]) if(!sdh[i]) mn.push_back({dlm[i], i}); sort(mn.begin(), mn.end()); for(auto &i:mn) ++a, isi(i.second), ++a; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; f(i,2,n) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } jr[1] = a = 1, jrk(1); it = max_element(jr.begin()+1, jr.begin()+n+1)-jr.begin(); trn(it), sdh.reset(), isi(it); f(i,1,n) cout << nmr[i] << ' '; }
/* Sunday 11 April 2021 06:07:10 PM IST @uthor::astrainL3gi0N */ #include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef std::vector<int> vi; typedef std::vector<ll> vll; typedef std::pair<int,int> ii; #define debg(x) std::cerr<<(#x)<<" => "<<x<<'\n'; #define debgg(x,y) std::cerr<<(#x)<<" => "<<x<<'\t'<<(#y)<<' '<<y<<'\n'; #define len(a) (int)(a).size() #define all(x) x.begin(),x.end() const int mod = 1000000007; //const int mod = 998244353; bool comp (int x, int y) { return x > y; } void printarr (int arr[], int n) { for(int i = 0; i < n; ++i) std::cerr<<arr[i]<<(i<n-1?' ':'\n'); } template < typename T> void printv (T &a) { for (auto it = a.begin(); it != a.end(); ++it) std::cerr<<*it<<' '; std::cerr<<'\n'; } const int maxn = 100009; //int t[510][510]; ll ans; int arr[maxn]; int visited[maxn]; vi adj[maxn]; map <int,int> mp; vi res; void dfs (int s) { if (!mp[arr[s]]) res.push_back(s); mp[arr[s]]++; visited[s] = 1; for (auto v: adj[s]) { if (!visited[v]) dfs(v); } mp[arr[s]]--; } void solve () { ans = 0; bool ok = false; int n,m,k,q; std::cin>>n; for (int i = 1; i < n+1;++i) { std::cin>>arr[i]; } for (int i = 1; i < n;++i) { int u,v; std::cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1); sort(all(res)); for (auto v: res) cout<<v<<'\n'; } void sol () { //test } int main () { std::ios_base::sync_with_stdio(false); std::cin.tie(0); auto begin = std::chrono::high_resolution_clock::now(); //cout << setprecision(15) << fixed; int testcases = 1; for (int t = 0; t < testcases; ++t) { //cout<<"Case #"<<t+1<<": "; solve(); } auto end = std::chrono::high_resolution_clock::now(); cerr << setprecision(4) << fixed; cerr << "Execution time: " << std::chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() << " seconds" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll,ll> ; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rep2(i,s1,n) for (ll i=(s1);i<(ll)(n);i++) #define rrep(i,a,b) for (ll i=(a);i>=(ll)(b);i--) #define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(x) (x).begin(),(x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end()) #define pb push_back const ll inf=1LL<<60; ll min(ll a,ll b) {return a>=b ? b:a;} ll max(ll a,ll b) {return a>=b ? a:b;} template<typename T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;} template<typename T>bool chmin(T &a, const T &b) {if (b<a) {a=b; return 1;} return 0;} //const ll mod=1000000007LL; //const ll mod=998244353LL; ll n; vector<ll> G[100009]; ll c[100009]; ll C[100009]; vector<ll> d; void dfs(ll st,ll prev){ if (C[c[st]]==0){ d.pb(st+1); } C[c[st]]++; for (ll i:G[st]){ if (i==prev) continue; dfs(i,st); } C[c[st]]--; } int main(){ fastio; cin >> n; rep(i,n) cin >> c[i]; rep(i,n-1){ ll a,b; cin >> a >> b; a--;b--; G[a].pb(b); G[b].pb(a); } dfs(0,-1); sort(all(d)); for (ll i:d){ cout << i << "\n"; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main () { int A, B, W; cin >> A >> B >> W; W *= 1000; int m = (W + B-1) / B; int M = W / A; if (m > M) cout << "UNSATISFIABLE" << endl; else cout << m << " " << M << endl; return 0; }
#include <iostream> #include <cmath> #include <cstring> #include <string> #include <vector> #include <algorithm> using namespace std; #define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define loop(i, n) for(int i = 1; i <= n; i++) typedef long long ll; typedef double lf; int main(){ fast; int a,b,w; cin >> a >> b >> w; int m=1e9,M=0; loop(i, 1000000){ if(a*i<=1000*w && 1000*w<=b*i){ m=min(m,i); M=max(M,i); } } if(!M) cout << "UNSATISFIABLE"; else cout << m << ' ' << M; }
#include <iostream> #include <iomanip> #include <vector> #include <cmath> #include <algorithm> #include <deque> #include <set> #include <limits> #include <string> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <numeric> #include <bitset> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, a) for (int i = 0; i < (a); ++i) #define OUT_PREC(a, n) cout << fixed << setprecision(n) << a << endl // nใŒไธ€ๅฎšใฎๆ™‚ class CombMod { private: ll n; const ll _MOD = 998244353; vector<ll> val; vector<ll> inv; public: CombMod(const ll &n0) { n = n0; val.resize(n + 1, 1); val[1] = n; inv.resize(n + 1, 1); // calculate in advance for (ll i = 2; i <= n; ++i) { inv[i] = _MOD - ((inv[_MOD % i] * (ll)(_MOD / i)) % _MOD); val[i] = (((val[i - 1] * (n + 1 - i)) % _MOD) * inv[i]) % _MOD; } } ll get(ll k) { // mCk if (k < 0) return 0; else return val[k]; } }; ll solve() { int n, m; cin >> n >> m; ll MOD = 998244353; CombMod cm(n); vector<ll> dp(m + 1, 0); dp[0] = 1; for (int j = 1; j <= (m / 2); ++j) { for (int k = 0; k <= min(j, n / 2); ++k) { dp[2 * j] = (dp[2 * j] + ((dp[j - k] * cm.get(2 * k)) % MOD)) % MOD; } } return dp[m]; } int main() { /* solve */ cout << solve() << endl; // cout << (solve() ? "Yes" : "No") << endl; system("pause"); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; const ll mod = 1000000007; ll dp[2010][2010]; ll x[2010][2010]; ll y[2010][2010]; ll xy[2010][2010]; int main() { int H, W; cin >> H >> W; vector<string> field(H); for (int i = 0; i < H; i++) cin >> field[i]; dp[0][0] = 1; x[0][0] = 1; y[0][0] = 1; xy[0][0] = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i + j == 0) continue; if (field[i][j] == '#') { x[i][j] = 0; y[i][j] = 0; xy[i][j] = 0; continue; } if (i > 0) dp[i][j] += y[i - 1][j]; if (j > 0) dp[i][j] += x[i][j - 1]; if (i > 0 && j > 0) dp[i][j] += xy[i - 1][j - 1]; if (i > 0) y[i][j] += y[i - 1][j]; if (j > 0) x[i][j] += x[i][j - 1]; if (i > 0 && j > 0) xy[i][j] += xy[i - 1][j - 1]; x[i][j] += dp[i][j]; y[i][j] += dp[i][j]; xy[i][j] += dp[i][j]; dp[i][j] %= mod; x[i][j] %= mod; y[i][j] %= mod; xy[i][j] %= mod; } } cout << dp[H-1][W-1] << endl; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define pb push_back #define COUT(x) cout << (x) << "\n" #define COUTF(x) cout << setprecision(15) << (x) << "\n" #define ENDL cout << "\n" #define DF(x) x.erase(x.begin()) #define ALL(x) x.begin(), x.end() #define SORT(x) sort(ALL(x)) #define RSORT(x) sort(x.rbegin(), x.rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MAXI(x) max_element(ALL(x)) - x.begin() #define SUM(x) accumulate(ALL(x), 0ll) #define COUNT(x, y) count(ALL(x), y); #define ANS cout << ans << "\n" #define YES cout << "YES\n"; #define NO cout << "NO\n"; #define Yes cout << "Yes\n"; #define No cout << "No\n"; #define init() \ cin.tie(0); \ ios::sync_with_stdio(false) #define debug(x) cerr << "[debug] " << #x << ": " << x << endl; #define debugV(v) \ cerr << "[debugV] " << #v << ":"; \ rep(z, v.size()) cerr << " " << v[z]; \ cerr << endl; using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vector<ll>>; using mll = map<ll, ll>; using qll = queue<ll>; using P = pair<ll, ll>; using vp = vector<P>; using vs = vector<string>; template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { rep(j, v.size()) i >> v[j]; return i; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; ll get_digit(ll x) { return to_string(x).size(); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } template <class T> void chmin(T& a, const T& b) { if (b < a) a = b; } ll mod_pow(ll x, ll n, ll mod) { if (n == 0) return 1; ll res = mod_pow(x * x % mod, n / 2, mod); if (n & 1) res *= x; res %= mod; return res; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll ext_gcd(ll a, ll b, ll& p, ll& q) { if (b == 0) { p = 1; q = 0; return a; } ll d = ext_gcd(b, a % b, q, p); q -= a / b * p; return d; } P crt(ll b1, ll m1, ll b2, ll m2) { ll p, q; ll d = ext_gcd(m1, m2, p, q); if ((b2 - b1) % d != 0) return {0, -1}; ll m = lcm(m1, m2); ll tmp = (b2 - b1) / d * p % (m2 / d); ll r = mod(b1 + m1 * tmp, m); return make_pair(r, m); } signed main() { init(); ll T; cin >> T; while (T--) { ll n, s, k; cin >> n >> s >> k; P p = crt(0, n, s % k, k); if (p.first == 0) p.first += p.second; ll ans = (p.first - s) / k; if (p.second == -1) ans = -1; ANS; } return 0; }
#include<bits/stdc++.h> using namespace std; #define GODSPEED ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15); #define f first #define s second #define newl cout<<"\n"; #define pb push_back #define mset(a,x) memset(a,x,sizeof(a)) #define debv(a) for(auto it: a)cout<<it<<" ";newl; #define deb1(a) cout<<a<<"\n"; #define deb2(a,b) cout<<a<<" "<<b<<"\n"; #define deb3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n"; #define deb4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"; #define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin()); #define all(a) a.begin(),a.end() typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; const ll N = 2e5+5; const ll mod = 998244353; const ll INF = 0x7f7f7f7f7f7f7f7f; const int INFi = 0x7f7f7f7f; const ll LEVEL = log2(N)+1; ll n; string s; void solve(){ cin >> n >> s; if(s == "1"){ deb1((ll)2e10) return; } if(s == "0"){ deb1((ll)1e10) return; } string t = "", init = "110"; for(int i = 0; i < n; i++){ t += init[i%3]; } if(s == t){ deb1((ll) (3e10 - n + 3) / 3) return; } t = ""; for(int i = 0; i < n; i++){ t += init[(i + 1)%3]; } if(s == t){ deb1((ll) (3e10 - n + 2) / 3) return; } t = ""; for(int i = 0; i < n; i++){ t += init[(i + 2)%3]; } if(s == t){ deb1((ll) (3e10 - n + 1) / 3) return; } deb1(0) } int main(){ GODSPEED; int test = 1; //cin >> test; for(int i = 1; i <= test; i++){ solve(); } #ifndef ONLINE_JUDGE cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n"; #endif }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int t[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> t[i][j]; } } vector<int> route; for (int i = 2; i <= n; i++) { route.push_back(i); } int res = 0; do { int sum = 0; int prev_city = 1; for (auto next_city : route) { sum += t[prev_city - 1][next_city - 1]; prev_city = next_city; } sum += t[route.back() - 1][0]; if (sum == k) res++; } while (next_permutation(route.begin(), route.end())); cout << res << endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/modint> #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define INF 2000000000000000000 #define ll long long #define ld long double #define pll pair<ll, ll> using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int main() { ll N, M; cin >> N >> M; vector<vector<bool>> connection(N, vector<bool>(N, false)); map<pll, ll> mp; vector<pll> ans(M, {-1, -1}); vector<pll> data(M); for (ll i = 0; i < M; ++i) { ll a, b; cin >> a >> b; a -= 1, b -= 1; data.at(i) = {a, b}; mp[{a, b}] = i; mp[{b, a}] = i; connection.at(a).at(b) = true; connection.at(b).at(a) = true; } vector<ll> C(N); for (ll i = 0; i < N; ++i) { cin >> C.at(i); } for (ll m = 1; m <= N; ++m) { for (ll i = 0; i < N; ++i) { if (C.at(i) == m) { stack<ll> st; st.push(i); while (!st.empty()) { ll now = st.top(); bool found = false; for (ll j = 0; j < N; ++j) { if (connection.at(now).at(j) && C.at(j) == m && ans.at(mp[{now, j}]) == (pll){-1, -1}) { found = true; ans.at(mp[{now, j}]) = {now, j}; if (j != i) { st.push(j); } break; } } if (!found) { st.pop(); } } } } } for (ll i = 0; i < M; ++i) { ll a = data.at(i).first, b = data.at(i).second; if (C.at(a) > C.at(b)) { ans.at(i) = {a, b}; } if (C.at(a) < C.at(b)) { ans.at(i) = {b, a}; } } for (ll i = 0; i < M; ++i) { if (ans.at(i) == data.at(i)) { cout << "->" << "\n"; } else { cout << "<-" << "\n"; } } }
#include <bits/stdc++.h> using namespace std; #define idk ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ll long long int n, arr[30]; vector<vector<int>> v; vector<int> temp; int solve(int idx, int grp){ if(idx >= n){ int xorr = 0; for(int i = 0; i < v.size(); i++){ int orr = 0; for(auto j : v[i]) orr |= j; xorr ^= orr; } return xorr; } int ans = 2e9; v[grp].push_back(arr[idx]); ans = min(ans, solve(idx + 1, grp)); ans = min(ans, solve(idx + 1, grp + 1)); v[grp].pop_back(); return ans; } int main() { idk cin >> n; v.resize(n); for(int i = 0; i < n; i++) cin >> arr[i]; cout << solve(0, 0); return 0; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define all(v) v.begin(), v.end() #define rep(i, begin, end) for(int i = begin; i < (int)(end); i++) #define contains(v, x) (find(all(v), x) != v.end()) template<class T> bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; } template<class T> bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; } template<class T> T roundup(T a, T b){ return (a + b - 1) / b; } const double PI = 3.14159265359; const vector<int> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; const vector<int> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; /* #include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> namespace mp = boost::multiprecision; using Bint = mp::cpp_int; using d32 = mp::number<mp::cpp_dec_float<32>>; */ using ll = long long; struct Edge{ int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} }; const ll MOD = (ll)998244353; const ll INF = (ll)1e9; using P = pair<int, int>; using Graph = vector<vector<int>>; /* #include <atcoder/all> using namespace atcoder; using mint = modint; */ int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); // mint::set_mod(MOD); int n; cin >> n; vector<ll> a(n); rep(i, 0, n) cin >> a[i]; if(n == 1){ cout << a[0] << endl; return 0; } ll ans = INF * INF; rep(bit, 1, 1 << (n - 1)){ vector<ll> temp; int b = 0; rep(i, 0, n - 1){ if((bit >> i) & 1){ ll num = 0; rep(j, b, i + 1) num |= a[j]; temp.emplace_back(num); b = i + 1; } } ll add = 0; rep(j, b, n) add |= a[j]; temp.emplace_back(add); ll sug = 0; for(auto i : temp) sug ^= i; chmin(ans, sug); } cout << ans << endl; }
#include <vector> #include <iostream> using namespace std; constexpr int M = 998244353; class ModComb { long long *fact, *facti; const int mod; public: explicit ModComb(int n, int m) : mod(m) { fact = new long long[n+1]; facti = new long long[n+1]; fact[0] = 1; facti[0] = 1; for (int i = 1; i <= n; i++) fact[i] = (fact[i-1] * i) % m; // calc 1/n! long long &inv = facti[n], pw = fact[n]; inv = 1; for (int e = mod-2; e > 0; e /= 2) { if (e&1) inv = inv * pw % mod; pw = pw * pw % mod; } for (int i = n-1; i > 0; i--) facti[i] = (facti[i+1] * (i+1)) % m; } ~ModComb() { if (fact) delete[] fact; if (facti) delete[] facti; } long long getFact(int n) const { return fact[n]; } long long getFactInv(int n) const { return facti[n]; } long long getComb(int n, int k) const { if (n < 0 || k < 0 || k > n) return 0; return fact[n] * facti[k] % mod * facti[n-k] % mod; } long long getPerm(int n, int k) const { if (n < 0 || k < 0 || k > n) return 0; return fact[n] * facti[n-k] % mod; } }; int main() { int n, m; cin >> n >> m; ModComb mc(n+2, M); vector<long long> f(m+1, 0); f[0] = 1; for (int i = 2; i <= m; i += 2) { long long sum = 0; for (int k = 0; k <= min(n, i); k += 2) { if ((i - k) / 2 % 2 != 0) continue; sum += mc.getComb(n, k) * f[(i-k)/2] % M; // cerr << i << ' ' << k << ' ' << sum << ' ' << (i - k) / 2 << endl; } f[i] = sum % M; // cerr << i << ' ' << f[i] << endl; } cout << f[m] << endl; }
/** Created by: Humberto Yusta Codeforces User: humbertoyusta Country: Cuba */ #include<bits/stdc++.h> using namespace std; /// Pragmas #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline","03") // Optimization flags //#pragma GCC option("arch=native","tune=native","no-zero=upper") // Enable AVX //#pragma GCC target("avx2") // Enable AVX //#pragma comment(linker, "/STACK:1024000000,1024000000") // Increase stack limit //#pragma GCC target("sse,sse2,sse,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // I don't know what it is /// Macros: #define int long long #define f first #define s second #define db(x) cerr << #x << ": " << (x) << '\n'; #define pb push_back #define lb lower_bound #define up upper_bound #define all(x) x.begin() , x.end() #define rall(x) x.rbegin() , x.rend() #define enl '\n' typedef pair<int,int> ii; typedef long double ld; typedef unsigned long long ull; /// Constraints: const int maxn = 5050; const int mod = 998244353; const int mod2 = 998244353; const ld eps = 1e-9; const int inf = ((1ll<<31ll)-1ll); const int INF = 1ll * mod * mod; const ld pi = acos(-1); /// Prime Numbers: // 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089; // 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081; // 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099; /// Functions: #define lg2(x) 31 - __builtin_clz(x) #define lgx(x,b) ( log(x) / log(b) ) /// Red-Black Tree Template --------------------------------- //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; //typedef tree < long long , null_type , less<long long> , rb_tree_tag , tree_order_statistics_node_update > ordered_set; /// Quick Pow------------------------------------------------ int qpow(int b,int e){ if( !e ) return 1; if( e & 1 ) return qpow(b,e-1) * b % mod; int pwur = qpow(b,e>>1); return pwur * pwur % mod; } int modinv(int x){ return qpow(x,mod-2); } bool isprime(int x){ for(int i=2; i*i<=x; i++) if( x % i == 0 ) return false; return true; } /// My Code ------------------------------------------------- int n, m, dp[15][maxn], C[maxn][maxn], f[maxn]; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.setf(ios::fixed); cout.precision(10); srand(time(NULL)); //freopen("a.in","r",stdin); //freopen("a.in","w",stdout); C[0][0] = 1; for(int i=1; i<maxn; i++){ for(int j=0; j<maxn; j++){ C[i][j] = C[i-1][j]; if( j ){ C[i][j] += C[i-1][j-1]; C[i][j] %= mod; } } } cin >> n >> m; for(int i=0; i<=n; i++){ if( i % 2 ) f[i] = 0; else f[i] = C[n][i]; } for(int i=0; i<=m; i++){ dp[0][i] = f[i]; } for(int i=1; (1<<i)<=m; i++){ for(int j=0; j<=m; j++){ for(int k=j; k>=0; k-=(1<<i)){ dp[i][j] = ( dp[i][j] + dp[i-1][k] * ( f[(j-k)/(1<<i)] ) ) % mod; } } } int ans = 0; for(int i=0; i<14; i++) if( ( 1 << i ) <= m && ( 1 << ( i + 1 ) ) > m ) ans = dp[i][m]; cout << ans << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int H,W,k; cin >> H >> W >> k; int h[k],w[k]; char c[k]; char masu[H + 1][W + 1]; for(int i = 0 ; i < H ; i++){ for(int j = 0 ; j < W ; j++){ masu[i][j] = '-'; } } for(int i = 0 ; i < k ; i++){ cin >> h[i] >> w[i] >> c[i]; h[i]--; w[i]--; masu[h[i]][w[i]] = c[i]; } long long beki[10100]; beki[0] = 1; for(int i = 1; i<=10000 ;i++){ beki[i] = beki[i-1]*3; beki[i] %= 998244353; } int yoko[H]; int tate[W]; long long dp[H + 1][W + 1]; dp[H - 1][W - 1] = 1; for(int i = 0 ; i < H ; i++){ yoko[i] = 0; dp[i][W] = 0; } for(int i = 0 ; i < W ; i++){ tate[i] = 0; dp[H][i] = 0; } for(int i = H - 1 ; i >= 0 ; i--){ for(int j = W - 1 ; j >= 0 ; j--){ if(i == H - 1 && j == W - 1){ if(masu[i][j] == '-'){ dp[i][j] = 3; yoko[i] += 1; tate[j] += 1; } if(masu[i][j] != '-')dp[i][j] = 1; continue; } dp[i][j] = 0; if(masu[i][j] == 'R'){ dp[i][j] += beki[tate[j]] * dp[i][j + 1]; dp[i][j] %= 998244353; } if(masu[i][j] == 'D'){ dp[i][j] += beki[yoko[i]] * dp[i + 1][j]; dp[i][j] %= 998244353; } if(masu[i][j] == 'X'){ dp[i][j] += beki[yoko[i]] * dp[i + 1][j]; dp[i][j] %= 998244353; dp[i][j] += beki[tate[j]] * dp[i][j + 1]; dp[i][j] %= 998244353; } if(masu[i][j] == '-'){ dp[i][j] += beki[yoko[i]] * dp[i + 1][j]; dp[i][j] %= 998244353; dp[i][j] += beki[tate[j]] * dp[i][j + 1]; dp[i][j] %= 998244353; dp[i][j] *= 2; dp[i][j] %= 998244353; } dp[i][j] %= 998244353; if(masu[i][j] == '-'){ yoko[i] += 1; tate[j] += 1; } } } cout << dp[0][0]; return 0; }
#include <bits/stdc++.h> using namespace std; template<typename T> istream &operator>>(istream &in, vector<T> &x) { for (auto &i : x) in >> i; return in; } template<typename T> ostream &operator<<(ostream &out, const vector<T> &x) { for (auto &i : x) { out << i << " "; } return out; } template<typename T, typename D> istream &operator>>(istream &in, pair<T, D> &x) { in >> x.first >> x.second; return in; } template<typename T, typename D> ostream &operator<<(ostream &out, const pair<T, D> &x) { out << x.first << " " << x.second; return out; } struct LogOutput { template<typename T> LogOutput &operator<<(T x) { #ifdef Dmitry07 cout << x; #endif return *this; } } dout; typedef long long ll; typedef unsigned long long ull; typedef double dl; #define nl '\n' #define elif else if #define all(_v) _v.begin(), _v.end() #define rall(v) v.rbegin(), v.rend() #define sz(v) (int)(v.size()) #define sqr(_v) ((_v) * (_v)) #define vpi vector<pair<int, int>> #define eb emplace_back #define pb push_back // #define MOD1(x, m) ((x) % m + ((x) < 0 && ((x % m) != 0) ? m : 0)) #define mod(x, m) (((x) % (m) + (m)) % (m)) #define vi vector<int> #define pi pair<int, int> #define ti tupMXle<int, int, int> #define minq(x, y) x = min((x), (y)) #define maxq(x, y) x = max((x), (y)) #define forn(i, n) for (int i = 0; i < (n); ++i) const ll INFL = 1e15; // 9187201950435737471; const ll nINFL = -9187201950435737472; const int INF = 1e9 + 10; // 2139062143; const int nINF = -2139062144; const ull ULINF = numeric_limits<ull>::max(); const dl PI = acos(-1); auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(seed); inline void IO() { #ifdef Dmitry07 freopen("../input.txt", "r", stdin); freopen("../output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(15); } const int MAXN = 4e5 + 10; const int MAXE = 1e5 + 10; const int MOD = 1e9 + 7; const double EPS = 1e-9; const int W = 1e6 + 100; const int K = 1000 + 1; #define int ll int tree[4 * MAXN]; void Build(vi &a, int v, int tl, int tr) { if (tl == tr) { tree[v] = a[tl]; return; } int tm = (tl + tr) / 2; Build(a, v * 2, tl, tm); Build(a, v * 2 + 1, tm + 1, tr); tree[v] = tree[v * 2] ^ tree[v * 2 + 1]; } void Upd(int v, int tl, int tr, int pos, int val) { if (tl == tr) { tree[v] ^= val; return; } int tm = (tl + tr) / 2; if (pos <= tm) { Upd(v * 2, tl, tm, pos, val); } else { Upd(v * 2 + 1, tm + 1, tr, pos, val); } tree[v] = tree[v * 2] ^ tree[v * 2 + 1]; } int Xor(int v, int tl, int tr, int l, int r) { if (l > r) { return 0; } if (tl == l && tr == r) { return tree[v]; } int tm = (tl + tr) / 2; return Xor(v * 2, tl, tm, l, min(r, tm)) ^ Xor(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r); } void Solve() { int n, q; cin >> n >> q; vi a(n); cin >> a; Build(a, 1, 0, n - 1); while (q--) { int t, x, y; cin >> t >> x >> y; if (t == 1) { Upd(1, 0, n - 1, x - 1, y); } else { cout << Xor(1, 0, n - 1, x - 1, y - 1) << nl; } } } signed main() { IO(); int t = 1; // cin >> t; int startTime = clock(); while (t--) { Solve(); } int endTime = clock(); dout << '\n' << "Time: " << (endTime - startTime) / 1000.0; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using P=pair<int,int>; #define rep(i,n) for(int i=0;i<n;i++) int main(){ int n; cin >> n; vector<ld> a(n); rep(i,n) cin >> a[i]; sort(a.begin(),a.end()); vector<ld> b=a; rep(i,n-1) b[n-i-2]+=b[n-i-1]; if(n==1){ printf("%.10Lf\n",a[0]/2); return 0; } ld ans=0; rep(i,n) ans+=a[i]/n; rep(i,n){ ld x=a[i]/2; int k=-1*n+2*(i+1); ans=min(ans,(x*k+b[i+1])/n); } printf("%.10Lf\n",ans); }
#include<bits/stdc++.h> using namespace std; long long n,a,b,ans; int main() { cin>>n; for(int i=0;i<n;i++) { cin>>a>>b; ans+=(a+b)*(b-a+1)/2; } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; string a, b, c; bool can(string s){ int good = 1; for(auto x: {a, b, c}){ x += x; int now = 0; for(int i = 0; i < (int)x.size(); i ++){ if(x[i] == s[now]) now ++; } good &= (now >= (int)s.size()); } return good; } int main(){ ios_base::sync_with_stdio(false); int t; cin >> t; while(t --){ int n; cin >> n; cin >> a >> b >> c; string s1 = string(n, '0') + string(n, '1'); string s2 = string(n, '1') + string(n, '0'); if(can(s1 + '0')){ cout << s1 + '0' << "\n"; }else if(can(s1 + '1')){ cout << s1 + '1' << "\n"; }else if(can(s2 + '0')){ cout << s2 + '0' << "\n"; }else if(can(s2 + '1')){ cout << s2 + '1' << "\n"; }else{ assert(0); } } }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define unused [[maybe_unused]] #define tempT template<class T> #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() #define debug(x) cerr << #x << ": " << x << endl #define ans_exit(x) { cout << x << endl; exit(0); } #define ans_return(x) { cout << x << endl; return; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define r_rep(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define reps(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++) #define r_reps(i, n, m) for (ll i = (ll)(m) - 1; i >= (ll)(n); i--) #define debugarr(a) {cerr << #a << ":"; \ for(auto _e:a){cerr << " " <<_e;} cerr << endl;} using ll unused = long long; using ld unused = long double; using vl unused = vector<ll>; using vvl unused = vector<vl>; using vvvl unused = vector<vvl>; using lp unused = pair<ll, ll>; using lmap unused = map<int, int>; unused constexpr ll MOD = 1e9 + 7; unused constexpr ll INF = 1 << 29; unused constexpr ll LINF = 1LL << 61; unused constexpr int DX[8] = {0, 1, 0,-1, 1, 1,-1,-1}; unused constexpr int DY[8] = {1, 0,-1, 0, 1,-1, 1,-1}; unused inline bool bit(ll b, ll i) { return b & (1LL << i); } unused inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; } unused inline ll mod(ll a, ll m = MOD) { return (a % m + m) % m; } unused inline void modadd(ll &a, ll b, ll m = MOD) { a = mod(a + b, m); } unused inline ll floordiv(ll a, ll b) { return a / b - (a < 0 && a % b); } unused inline ll ceildiv(ll a, ll b) { return floordiv(a + b - 1, b); } tempT unused inline bool in_range(T a, T x, T b) { return a <= x && x < b; } unused inline bool in_range(ll x, ll b) { return in_range(0LL, x, b); } tempT unused bool chmin(T &a, T b) { if(a > b) {a = b; return 1;} return 0; } tempT unused bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; } int main() { int t; cin >> t; rep(i, t) { int n; string s, t, u; cin >> n >> s >> t >> u; cout << string(n, '0') << string(n, '1') << 0 << endl; } }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using LL = long long; using LD = long double; using ordered_set = tree<LL,null_type,less<LL>,rb_tree_tag,tree_order_statistics_node_update>; void fastio() { cin.tie(nullptr); cin.sync_with_stdio(false); } const LL MOD = 998244353; const LL INF = 1e18; const LL N = 2e5+5; #define S second #define F first #define vt vector LL binpow(LL x, LL p, LL m) { LL res = 1; x %= m; while (p) { if (p&1) { res = res * x % m; } x = x * x % m; p >>= 1; } return res; } int main() { fastio(); LL n, m; cin >> n >> m; LL sub = 0; for (LL d = 2; d <= n; ++d) { LL tim = n-d+1; LL res = 0; for (LL x = 1; x <= m; ++x) { res = (res + binpow(m-x, d-2, MOD) % MOD * binpow (m, n-d, MOD)) % MOD; } LL trm = (res * tim) % MOD; //cerr << trm << "?"; sub = (sub + trm) % MOD; } LL ans = n*binpow(m, n, MOD) % MOD; ans -= sub; if (ans < 0) { ans += MOD; } cout << ans << "\n"; }
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cassert> #include <climits> #include <algorithm> #include <string> #include <sstream> #include <complex> #include <vector> #include <list> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #include <fstream> using namespace std; #define SZ(x) (int)(x).size() #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REPR(i,n) for(int i=(n)-1;i>=0;i--) #define ALL(s) (s).begin(), (s).end() #define so(V) sort(ALL(V)) #define rev(V) reverse(ALL(V)) #define uni(v) v.erase( unique(ALL(v)) , v.end()); typedef long long unsigned int llu; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<vi> vvi; const double EPS = 1e-9; const int MOD = 1e9 + 7; const int INF = (1 << 29); const ll LINF = 1e18; const double PI = acos(-1); template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template<typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) { t = v; } template<typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) { for (auto& e : t) fill_v(e, v); } 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; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll S, P; cin >> S >> P; ll cur = 1; int ans = 0; while (cur * cur <= P) { if (P % cur == 0) { ll other = P / cur; if (cur + other == S) { ans++; } } cur++; } cout << ((ans != 0) ? "Yes" : "No") << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { ll MOD = 1e9 + 7; int H, W, K = 0; cin >> H >> W; vector<string> S(H); rep(i, H) { cin >> S[i]; rep(j, W) { if (S[i][j] == '.') K++; } } //cout << "K=" << K << "\n"; vector<vector<int>> R(H, vector<int>(W + 1, 0)), C(H + 1, vector<int>(W, 0)); rep(i, H) { rep(j, W) { if (S[i][j] == '.') R[i][j + 1] = R[i][j] + 1; } for (int j = W - 2; j >= 0; j--) { if (S[i][j] == '.') R[i][j + 1] = max(R[i][j + 1], R[i][j + 2]); } //rep(j, W) cout << R[i][j + 1]; //cout << "\n"; } rep(j, W) { rep(i, H) { if (S[i][j] == '.') C[i + 1][j] = C[i][j] + 1; } for (int i = H - 2; i >= 0; i--) { if (S[i][j] == '.') C[i + 1][j] = max(C[i + 1][j], C[i + 2][j]); } } /* rep(i, H) { rep(j, W) { cout << C[i + 1][j]; } cout << "\n"; } */ vector<ll> b(4000001, 1); rep(i, 4000000) b[i + 1] = (b[i] * 2) % MOD; ll ans = 0; rep(i, H) { rep(j, W) { if (S[i][j] == '#') continue; int B = R[i][j + 1] + C[i + 1][j] - 1; //cout << "B=" << B << "\n"; ans = (ans + ((b[B] - 1 + MOD) % MOD) * b[K - B]) % MOD; } } cout << ans << "\n"; }
#include<bits/stdc++.h> #define LL long long using namespace std; const int mod=998244353; int n,m; LL fact[200010],inv[200010],f[200010][20],ans; inline int read() { int x=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return w?-x:x; } LL ksm(LL B,LL K,LL A=1) { for(;K;K>>=1,B=B*B%mod) if(K&1)A=A*B%mod; return A; } void prepare() { fact[0]=1; for(int i=1;i<=200000;i++)fact[i]=fact[i-1]*i%mod; inv[200000]=ksm(fact[200000],mod-2); for(int i=200000-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod; } LL C(LL N,LL M) { if(N<M||M<0)return 0; return fact[N]*inv[M]%mod*inv[N-M]%mod; } int main() { prepare(); n=read();m=read(); for(int i=1;i<=m;i++) f[i][0]=1; //f[i][j] ่กจ็คบ a[n]=i,ๅนถไธ”ๅ˜ไบ†jๆฌก็š„ๆ–นๆกˆๆ•ฐใ€‚ for(int t=0;t<=18;t++) for(int i=1;i<=m;i++) for(int j=2*i;j<=m;j+=i) f[j][t+1]=(f[j][t+1]+f[i][t])%mod; for(int i=0;i<=min(18,n);i++) for(int j=1;j<=m;j++) ans=(ans+f[j][i]*C(n-1,i))%mod; cout<<ans<<'\n'; }
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<vector<int>> vec(n,vector<int>(3)); for(int i = 0;i < n;i++){ for(int j = 0;j < 3;j++){ cin >> vec.at(i).at(j); } } int minP = -1; for(int i = 0;i < n;i++){ if(vec.at(i).at(2) - vec.at(i).at(0) > 0){ if(minP == -1)minP = INT_MAX; minP = min(minP,vec.at(i).at(1)); } } cout << minP; }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(auto i=(a); i<(b); ++i) #define trav(a,x) for(auto& a: x) #define all(x) begin(x),end(x) #define sz(x) (int)size(x) #define PB push_back #define cauto const auto using ll = long long; using ld = long double; using pii = pair<int,int>; using vi = vector<int>; struct edge{int to;}; using graph = vector<vector<edge>>; constexpr auto dbg = false; int32_t main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int n; cin >> n; vi a(n), p(n), x(n); rep(i, 0, n) { cin >> a[i] >> p[i] >> x[i]; } bool found = false; int best = -1; rep(i, 0, n) { auto feas = x[i] > a[i]; if (feas) { if (!found or p[i] < best) { found = true; best = p[i]; } } } cout << best << '\n'; }
#include<bits/stdc++.h> using namespace std; //_______________________Macros Start____________________________ #define pb push_back #define ll long long #define vi vector <int> #define vl vector <ll> #define vii vector < pair <int,int> > #define vll vector < pair <ll,ll> > #define mii map<int,int> #define mll map<ll,ll> #define si set <int> #define sl set <ll> #define all(a) a.begin(),a.end() #define pii pair<int,int> #define fr first #define sc second #define rep(i,a,n) for(ll i=a ; i<n ; i++) #define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); // ________________________Macros End______________________________ // ________________________Utility Functions Start__________________ ll M=1e9+7; ll binomialCoeff(ll n, ll k) { ll res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (ll i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } ll mods(ll a,ll b){ a=a%M; b=b%M; return (a+b)%M; } ll modp(ll a,ll b){ a=a%M; b=b%M; return (a*b)%M; } ll pow(ll a, ll b){ if(b==0) return 1; if(b%2==0) { ll x=pow(a,b/2); return modp(x,x); } else{ ll x=pow(a,b/2); ll y=modp(x,x); return modp(y,a); } } struct MyComp { bool operator()(const pair<int,int>& x, const pair<int,int>& y) const { return x.second > y.second || (x.second == y.second && x.first > y.first); } }; //__________________________Utility Fuction End____________________________________ //___________________________Starters for Graph Begin_______________________________ const int N=1e5+5; vi adj[N]; vi visited(N,0); void makeGraph(int m){ int x,y; for(int i=0;i<m;i++){ cin>>x>>y; adj[x].pb(y); adj[y].pb(x); } } //_____________________________Starters for Graph End___________________________________ //____________________________Main Start__________________________________________________ int main(){ fastio int n; cin>>n; vi v(n); for(int i=0;i<n;i++) cin>>v[i]; int sum=0; for(int i=0;i<n;i++) { if(v[i]>10) sum+=v[i]-10; } cout<<sum<<endl; } //_____________________________Main End______________________________________________________
#include <iostream> #include <string> #include <bitset> using namespace std; #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); typedef long long int ll; void solve() { } int main() { IOS int n; cin >> n; ll ans = 0; for (int i = 0; i < n; ++i) { ll cur; cin >> cur; if (cur <= 10) continue; ans += (cur - 10); } cout << ans << endl; return 0; }
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <utility> #include <functional> #include <set> #include <map> #include <queue> #include <deque> #include <bitset> #include <math.h> #include <random> #include <chrono> #include <assert.h> using namespace std ; using ll = long long ; using ld = long double ; template<class T> using V = vector<T> ; template<class T> using VV = V<V<T>> ; using pll = pair<ll,ll> ; #define all(v) v.begin(),v.end() ll mod = 1000000007 ; long double pie = acos(-1) ; ll INF = 1e18 ; void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;} //void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;} ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;} ll lcm(long long a,long long b){return a/gcd(a,b)*b ;} ll extGCD(ll a,ll b,ll &x,ll &y){ if(b==0){ x = 1 ; y = 0 ; return a ; } ll d = extGCD(b,a%b,y,x) ; y -= a/b*x ; return d ; } void fix_cout(){cout << fixed << setprecision(20) ;} 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 ;} int main(){ int n ; cin >> n ; VV<int> rem(200) ; for(int i=0;i<n;i++){ ll a ; cin >> a ; a %= 200 ; for(int j=0;j<200;j++){ if(rem[(j+a)%200].size()>0){ if(j!=0&&rem[j].size()==0) continue ; cout << "Yes" << endl ; cout << rem[(j+a)%200].size() << " " ; for(int k=0;k<rem[(j+a)%200].size();k++){ cout << rem[(j+a)%200][k] << " \n"[k+1==rem[(j+a)%200].size()] ; } cout << rem[j].size()+1 << " " ; for(auto k:rem[j]) cout << k << " " ; cout << i+1 << endl ; return 0 ; } } auto sub = rem ; for(int j=0;j<200;j++){ if(rem[(j+a)%200].size()>0) continue ; if(j!=0&&rem[j].size()==0) continue ; sub[(j+a)%200] = rem[j] ; sub[(j+a)%200].push_back(i+1) ; } rem = sub ; // if(rem[a].size()==0) rem[a].push_back(i+1) ; // for(auto j:rem[165]) cout << j << " " ; cout << endl ; } cout << "No" << endl ; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using str = string; #define endl "\n" #define sz(x) (int)(x).size() #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define fi first #define se second #define lb lower_bound #define ub upper_bound constexpr int setbits(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 32-__builtin_clz(x); } str binary(int x) { return bitset<32>(x).to_string(); } int decimal(str x) { return stoi(x,nullptr,2); } ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } const int nax = INT_MAX; const int nim = INT_MIN; const int MOD = 1000000007; void solve(){ int n; cin >> n; vector < ll > a(n); for(ll &it : a) cin >> it; sort(all(a)); ll sum = 0, ans = 0; for(int i = 0; i < n; ++i){ ans += a[i] * i - sum; sum += a[i]; } cout << ans << endl; } int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); int t = 1; // cin >> t; while(t--) solve(); }
//Bismillahirrahmanirrahim #pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long lo; typedef pair< lo,lo > PII; #define fi first #define se second #define int long long #define mp make_pair #define endl "\n" #define pb push_back #define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define FOR for(int i=1;i<=n;i++) #define mid ((start+end)/2) #define ort ((bas+son)/2) const lo inf = 1000000000000000000; const lo KOK = 100000; const lo LOG = 30; const lo li = 500005; const lo mod = 1000000007; int n,m,b[li],a[li],k,flag,t,PS[li]; int cev; string s; vector<int> v; int32_t main(void){ scanf("%lld %lld",&n,&m); FOR{ int x,y,z; scanf("%lld %lld %lld",&x,&y,&z); PS[x]+=z; PS[y]-=z; } cev=PS[0]; for(int i=1;i<=n;i++){ PS[i]+=PS[i-1]; cev=max(cev,PS[i]); //~ cout<<cev<<endl; } if(cev>m){printf("No\n");} else printf("Yes\n"); return 0; }
#include<map> #include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n,w; cin >> n >> w; vector<pair<int,int>>event; for(int i=0;i<n;i++){ int s,t,p; cin >> s >> t >> p; event.push_back(make_pair(s,p)); event.push_back(make_pair(t,-p)); } sort(event.begin(), event.end()); long long crr=0; for(auto [time,value]:event){ crr+=value; if(crr>w){ cout << "No"; return 0; } } cout << "Yes"; }
#include<bits/stdc++.h> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") /* // Ordered Set #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define os_find(k) find_by_order(k) #define os_order(k) order_of_key(k) */ typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; typedef vector<int> vi; #define f0(i,a,b) for(int i=a;i<b;i++) #define f1(i,a,b) for(int i=a;i<=b;i++) #define f2(i,a,b) for(int i=a;i>b;i--) #define f3(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(),a.end() #define pb push_back #define mp make_pair #define pii pair<int,int> #define int long long #define fi first #define se second #define mod 998244353//1000000007 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);} #define test int t;cin>>t;while(t--) int fact[300000]; int binExp(int x,int n) { int res=1; while(n) { if(n&1) res=(res*x)%mod; x=(x*x)%mod; n>>=1; } return res; } int modInv(int i) {return binExp(i,mod-2);} int ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r]))%mod*modInv(fact[n-r])%mod:0);} signed main() { fast int n,k; cin>>n>>k; pii a[n]; f0(i,0,n)cin>>a[i].fi>>a[i].se; sort(a,a+n); int c=0,d=k; f0(i,0,n) { if(a[i].fi-c>d)break; d-=a[i].fi-c; d+=a[i].se; c=a[i].fi; } cout<<c+d; }
#include<bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <chrono> // using namespace std::chrono; // using namespace __gnu_pbds; #define ff first #define ss second #define int long long #define double long double #define pb push_back #define mp make_pair #define pi pair<int,int> #define vtiii vector<tuple<int,int,int>> #define vi vector<int> #define vpii vector<pair<int,int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define all(x) (x).begin(), (x).end() #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #ifndef ONLINE_JUDGE #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #endif void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(double t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} const int N = 2e5 + 10; const int mod = 1e9 + 7 ; void solve(){ int n,m;cin>>n>>m; map<int,vi> M; for(int i=0;i<m;++i){ int r,c;cin>>r>>c; M[r].push_back(c); } set<int> S; S.insert(n); for(auto it = M.begin() ; it != M.end() ; ++it){ vi add; for(int x : it->ss){ if(S.count(x-1) || S.count(x+1)) add.push_back(x); } for(int x : it->ss) S.erase(x); for(int x : add) S.insert(x); } cout<<S.size()<<endl; } int32_t main(){ // auto start = high_resolution_clock::now() FIO; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("Error.txt", "w", stderr); #endif // precalc(); // int x; cin>>x; while(x--) solve(); // auto stop = high_resolution_clock::now(); // auto duration = duration_cast<microseconds>(stop - start); // cout << duration.count() << endl; }
//้ซ˜็Ÿฅ่ƒฝ็ณปVtuberใฎ้ซ˜ไบ•่Œ…ไนƒใงใ™ใ€‚ //Twitter: https://twitter.com/takaichino //YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF INT_MAX #define LLINF LLONG_MAX #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define MODA 1000000007 #define MODB 998244353 template <typename T> std::istream& operator>>(std::istream& is, std::vector<T>& vec) { for (T& x: vec) { is >> x; } return is; } int main() { ll ans = 0; ll tmp; int n; cin >> n; int m, t; cin >> m >> t; ll now = n; int ti = 0; bool ch = true; int a, b; REP(i, m){ cin >> a >> b; now -= a-ti; if(now <= 0) ch = false; now += b - a; now = min(now, (ll)n); ti = b; } now -= t - ti; if(now <= 0) ch = false; if (ch) cout << "Yes" << endl; else cout << "No" << endl; //cout << ans << endl; }
#include <bits/stdc++.h> #define REP(i, s, n) for (int i = s; i < (int)(n); i++) #define ALL(a) a.begin(), a.end() #define MOD 1000000007 using namespace std; using ll = long long; template<int M> class ModInt { public: ll value; constexpr ModInt(ll v = 0) { value = v % M; if (value < 0) value += M; } constexpr ModInt pow(ll n) const { if (!n) return 1; ModInt a = pow(n >> 1); a *= a; if (n & 1) a *= *this; return a; } constexpr ModInt inv() const { return this->pow(M - 2); } constexpr ModInt operator + (const ModInt &rhs) const { return ModInt(*this) += rhs; }; constexpr ModInt operator - (const ModInt &rhs) const { return ModInt(*this) -= rhs; }; constexpr ModInt operator * (const ModInt &rhs) const { return ModInt(*this) *= rhs; }; constexpr ModInt operator / (const ModInt &rhs) const { return ModInt(*this) /= rhs; }; constexpr ModInt &operator += (const ModInt &rhs) { value += rhs.value; if (value >= M) value -= M; return *this; }; constexpr ModInt &operator -= (const ModInt &rhs) { value -= rhs.value; if (value < 0) value += M; return *this; }; constexpr ModInt &operator *= (const ModInt &rhs) { value = value * rhs.value % M; return *this; }; constexpr ModInt &operator /= (const ModInt &rhs) { return (*this) *= rhs.inv(); }; constexpr bool operator == (const ModInt &rhs) { return value == rhs.value; } constexpr bool operator != (const ModInt &rhs) { return value != rhs.value; } friend ostream &operator << (ostream &os, const ModInt &rhs) { os << rhs.value; return os; } }; using mint = ModInt<MOD>; int main() { int N, M; cin >> N >> M; vector<int> A(N); REP(i, 0, N) cin >> A[i]; ll S = accumulate(ALL(A), 0LL); mint ans = 1; REP(i, 0, N + S) { ans *= (N + M - i); ans /= (N + S - i); } cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <queue> #include <stack> #include <set> #include <map> #include <iomanip> #include <utility> #include <tuple> #include <functional> #include <bitset> #include <cassert> #include <complex> #include <stdio.h> #include <time.h> #include <numeric> #include <random> #include <unordered_map> #include <unordered_set> #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n' #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef complex<ld> com; template<class T> using prique = priority_queue<T, vector<T>, greater<T>>; constexpr int inf = 1000000010; constexpr ll INF = 1000000000000000010; constexpr int mod1e9 = 1000000007; constexpr int mod998 = 998244353; constexpr ld eps = 1e-12; constexpr ld pi = 3.141592653589793238; constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; }; int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 }; void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); } template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; } template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; } struct fastio { fastio() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(20); } }fastio_; template<class T> class segtree { int n; vector<T> data; T id = 0; T operation(T a, T b) { return a ^ b; }; public: segtree(int _n) { n = 1; while (n < _n + 2) n <<= 1; data = vector<T>(2 * n, id); } segtree(vector<T> vec) { int _n = vec.size(); n = 1; while (n < _n + 2) n <<= 1; data = vector<T>(2 * n, id); for (int i = 0; i < _n; i++) data[i + n] = vec[i]; for (int i = n - 1; i >= 1; i--) data[i] = operation(data[i << 1], data[i << 1 | 1]); } void change(int i, T x) { i += n; data[i] = x; while (i > 1) { i >>= 1; data[i] = operation(data[i << 1], data[i << 1 | 1]); } } void add(int i, T x) { change(i, data[i + n] + x); } T get(int a, int b) { T left = id; T right = id; a += n; b += n; while (a < b) { if (a & 1) left = operation(left, data[a++]); if (b & 1) right = operation(data[--b], right); a >>= 1; b >>= 1; } return operation(left, right); } T get_all() { return data[1]; } T operator[](int i) { return data[i + n]; } }; int main() { int n, q; cin >> n >> q; vector<int> a(n); cin >> a; segtree<int> seg(a); while (q--) { int type; cin >> type; if (type == 1) { int x, y; cin >> x >> y; x--; seg.change(x, seg[x] ^ y); } else { int x, y; cin >> x >> y; x--; cout << seg.get(x, y) << '\n'; } } }
#include <bits/stdc++.h> #define LL long long #define PII pair<int,int> #define PIL pair<int,LL> #define PLI pair<LL,int> #define PIII pair<int,PII> #define PLL pair<LL,LL> #define PLII pair<LL,PII> #define VI vector<int> #define VVI vector<VI> #define VL vector<LL> #define VVL vector<VL> #define VPII vector<PII> #define FF first #define SS second #define MP make_pair #define PB push_back #define sqr(x) ((x) * (x)) #define all(x) x.begin(),x.end() #define watch(x) cout<<(#x)<<" = "<<(x)<<'\n' #define mset(a,v) memset(a,v,sizeof(a)) #define setp(x) cout<<fixed<<setprecision(x) #define EPS 0.00000000001 #define PI acos(-1) #define loop(i,b,n) for(int i=b;i<n;++i) #define rev_loop(i,b,n) for(int i=b;i>=n;--i) using namespace std; const int MOD = 1e9 + 7; const LL MX = 50; const LL INF = 1e9; int c[MX], vis[MX]; VI g[MX]; vector<int> vec; void dfs(int u) { vec.PB(u); vis[u] = 1; for(int v : g[u]) { if(!vis[v]) dfs(v); } } LL rec(int i) { if(i == vec.size()) return 1; int u = vec[i], f[3] = {0}; for(int v : g[u]) if(c[v] != -1) f[c[v]] = 1; LL ret = 0; loop(j,0,3) { if(f[j] == 0) { c[u] = j; ret += rec(i+1); } } c[u] = -1; return ret; } int main() { //ofstream out("output.txt"); //ifstream in("input.txt"); ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n, m; cin>>n>>m; int u, v; loop(i,0,m) { cin>>u>>v; g[u].PB(v); g[v].PB(u); } LL ans = 1; mset(c,-1); mset(vis,0); loop(i,1,n+1) { if(vis[i] == 0) { dfs(i); LL ans1 = rec(0); if(ans1 == 0) {ans = 0; break;} ans *= ans1; vec.clear(); } } cout<<ans<<'\n'; return 0; }
#include<bits/stdc++.h> #include<math.h> using namespace std; using ll = long long; #define rep(i,n) for (int i=0; i<(n);++i) int main(){ int N = 3; vector<int> A(N); rep(i,N) cin >> A[i]; sort(A.begin(), A.end()); if((A[2]-A[1])==A[1]-A[0]) cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A1,A2,A3; cin>>A1>>A2>>A3; if((max(A1,max(A2,A3))-max(A1,min(A2,A3)))==(max(A1,min(A2,A3))-min(A1,min(A2,A3)))) {cout<<"Yes"<<endl;} else if((max(A1,max(A2,A3))-min(A1,max(A2,A3)))==(min(A1,max(A2,A3))-min(A1,min(A2,A3)))) {cout<<"Yes"<<endl;} else{cout<<"No"<<endl;} }
#include<bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define pb push_back #define pf push_front #define sz(x) (int)(x).size() #define lb lower_bound #define ub upper_bound #define mp make_pair #define fi first #define se second #define setbits(x) __builtin_popcount(x) #define zerobits(x) __builtin_ctz(x) #define setbitsll(x) __builtin_popcountll(x) #define zerobitsll(x) __builtin_ctzll(x) #define ps(x,y) fixed<<setprecision(y)<<x typedef vector<int> vi; typedef long long ll; typedef vector<ll> vl; typedef pair<int,int> pii; typedef unsigned long long ull; typedef long double ld; typedef map<int,int> mii; const int MOD = 1e9+7; const ld PI = acos((ld)-1); void solve () { int x,y; cin >> x >> y; if (abs(x-y) < 3) { cout << "Yes\n"; } else { cout << "No\n"; } } int main () { #ifndef ONLINE_JUDGE freopen("input1.txt","r",stdin); freopen("output1.txt","w",stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define ll long long #define ull unsigned long long #define lson(x) x << 1 #define rson(x) x << 1 | 1 using namespace std; void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } const int MAXN = 5e5 + 5; const int INF = 0x3f3f3f3f; const ll MOD = 1e9 + 7; ll fpow(ll base, ll t, ll p) { ll ans = 1; while (t) { if (t & 1) { ans = ans * base % p; } base = base * base % p; t >>= 1; } return ans; } int main() { io(); ll n, m; cin >> n >> m; n = fpow(10, n, m * m); n /= m; cout << n; }
#include<bits/stdc++.h> typedef int LL; typedef double dl; #define opt operator #define pb push_back #define pii std::pair<LL,LL> const LL maxn=1e6+9,mod=1e9+7,inf=0x3f3f3f3f; LL Read(){ LL 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<<3ll)+(x<<1ll)+c-'0'; c=getchar(); }return x*f; } void Chkmin(LL &x,LL y){ if(y<x) x=y; } void Chkmax(LL &x,LL y){ if(y>x) x=y; } LL add(LL x,LL y){ return x+=y,x>=mod?x-mod:x; } LL dec(LL x,LL y){ return x-=y,x<0?x+mod:x; } LL mul(LL x,LL y){ return 1ll*x*y%mod; } LL Pow(LL base,LL b){ LL ret(1); while(b){ if(b&1) ret=mul(ret,base); base=mul(base,base); b>>=1; }return ret; } LL n; LL a[maxn]; int main(){ n=Read(); for(LL i=1;i<=n;++i) a[i]=Read(); std::sort(a+1,a+1+n); LL ret(1); for(LL i=0;i<n;++i) ret=mul(ret,a[i+1]-a[i]+1); printf("%d\n",ret); return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <map> using namespace std; const int maxn = 4e5 + 50, INF = 0x3f3f3f3f; inline int read () { register int x = 0, w = 1; register char ch = getchar (); for (; ch < '0' || ch > '9'; ch = getchar ()) if (ch == '-') w = -1; for (; ch >= '0' && ch <= '9'; ch = getchar ()) x = x * 10 + ch - '0'; return x * w; } int n, pos[maxn]; struct Node { int id, val; inline friend bool operator < (register const Node &a, register const Node &b) { return a.val < b.val; } } a[maxn]; int main () { n = read() << 1; for (register int i = 1; i <= n; i ++) a[i].id = i, a[i].val = read(); sort (a + 1, a + n + 1); for (register int i = 1; i <= n; i ++) pos[a[i].id] = i; register int res = 0; for (register int i = 1; i <= n; i ++) { if (pos[i] <= n >> 1) { if (res >= 0) putchar ('('); else putchar (')'); res ++; } else { if (res <= 0) putchar ('('); else putchar (')'); res --; } } return putchar ('\n'), 0; }
// *** Though I am not John Snow, yet I know nothing *** #include <bits/stdc++.h> using namespace std; #define F(i, a, b) for (int i = a; i < b; i++) #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define mt make_tuple #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define range(a, b) substr(a, b - a + 1) #define w(x) \ int x; \ cin >> x; \ while (x--) #define trace(x) cerr << #x << ": " << x << " " << endl; #define FIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) void solve() { int n; cin >> n; int a[105]; int t, p; cin >> t >> p; F(i, 0, n) { int st, pt; cin >> st >> pt; if (st < t && pt > p) { cout << "Yes"; return; } } cout << "No"; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FIO; solve(); return 0; }
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <stack> #include <queue> #include <map> #include <cstring> #include <bitset> #include <array> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; if(abs(m)>max(n-2, 0)){ cout << -1 << '\n'; return 0; } if(m<0){ cout << -1 << '\n'; return 0; m=abs(m)+1; cout << 1 << ' ' << m+2 << '\n'; int pos=(m+1)*2; for(int i=0; i<m; i++){ cout << i+2 << ' ' << pos << '\n'; pos--; } pos=(m+1)*2+1; for(int i=0; i<n-m-1; i++){ cout << pos << ' ' << pos+1 << '\n'; pos+=2; } } else if(m>0){ int pos=1; m++; for(int i=0; i<n-m-1; i++){ cout << pos << ' ' << pos+1 << '\n'; pos+=2; } cout << pos << ' ' << pos+(m+1)*2-1 << '\n'; for(int i=0; i<m; i++){ cout << pos+i*2+1 << ' ' << pos+(i+1)*2 << '\n'; } } else{ int pos=1; for(int i=0; i<n; i++){ cout << pos << ' ' << pos+1 << '\n'; pos+=2; } } return 0; }
#include <iostream> #include <bits/stdc++.h> using namespace std; #define ll long long int int main() { ll n,i,x,y,ans=0; cin >> n; ll a[n],b[n]; for(i=0;i<n;i++) cin >> a[i]; for(i=0;i<n;i++) cin >> b[i]; for(i=0;i<n;i++) { ans += a[i]*b[i]; } if(ans == 0) std::cout << "Yes" << std::endl; else cout << "No" << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define all(a) a.begin(),a.end() #define fi first #define se second #define deb(x) cout << #x << "=" << x << endl #define clr(x) memset(x, 0, sizeof(x)) #define rep(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) int M=1e9+7,N=205001; int mm(int x,int y){x%=M,y%=M;return (x*y)%M;}//Modular Multiply int po(int x,int y){ if(!y)return 1;int a=po(x,y/2)%M;if(y%2)return mm(a,mm(a,x));return mm(a,a);}//(x**y)%M void solve() { int n,x,s=0; cin>>n; int a[n]; rep(i,0,n) cin>>a[i]; rep(i,0,n) { cin>>x; s+=a[i]*x; } if(s) cout<<"No"; else cout<<"Yes"; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t=1; //cin>>t; while(t--) solve(); return 0; }
/*#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { ll sum; int k; while(cin>>k){ sum=0; for(int a=1;a<=k;a++){ for(int b=1;b<=k;b++){ for(int c=1;c<=k;c++){ if(a*b*c<=k)sum++; } } } cout<<sum<<endl; } } */ #include<bits/stdc++.h> typedef long long ll; using namespace std; int main() { ll sum; int k; while(cin>>k){ sum=0; for(int a=1;a<=k;a++){ for(int b=a;b<=k/a;b++){ for(int c=b;c<=k/a/b;c++){ if(a*b*c<=k){ if(a==b&&b==c)sum+=1; else if(a==b||b==c||a==c)sum+=3; else sum+=6; } } } } cout<<sum<<endl; } }
#include <iostream> using namespace std; typedef long long ll; ll n,N,M,ans; int main(void){ cin>>n; for(ll i=1LL;i<=n;i++){ N=n/i; for(ll j=1LL;j<=N;j++){ M=N/j; ans+=M; } } cout<<ans<<endl; }
#include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ub upper_bound #define lb lower_bound #define isrt insert #define clr clear #define rsz resize #define ff first #define ss second #define int long long int #define pb push_back #define pf push_front #define mkp make_pair #define mkt make_tuple #define pii pair<int,int> #define tii tuple<int,int,int> #define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define INF 1e9 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define endl "\n" using cd = complex<double>; const double PI = acos(-1); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int maxm(int a, int b) { return (a >= b ? a : b); } int minm(int a, int b) { return (a <= b ? a : b); } int power(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } #define FMAX 2000005 vi fact(FMAX, 1); vi fact_inv(FMAX, 1); vi lp(FMAX); vi pr; int nCrmodp(int n, int r, int p) { if (r > n) return 0; int pro = (fact[n] * fact_inv[r]) % p; pro = (pro * fact_inv[n - r]) % p; return pro; } void precompute() { int x = 1; for (int i = 1; i < FMAX; ++i) { x *= i; x %= mod; fact[i] = x; fact_inv[i] = power(x, mod - 2, mod); } } void spf_calc() { for (int i = 2; i < FMAX; ++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] < FMAX; ++j) lp[i * pr[j]] = pr[j]; } } int next_smaller(vector<int>v, int val) { int l = 0, r = v.size() - 1, ans = -1; while (l <= r) { int mid = (l + r) / 2; if (v[mid] <= val) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } void c_p_c() { precompute(); //spf_calc(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } signed main() { c_p_c(); int n, k, m; cin >> n >> m >> k; if (n > (m + k)) cout << "0\n"; else { int tot = nCrmodp(n + m, n, mod); tot -= nCrmodp(n + m, m + k + 1, mod); tot = (tot + mod) % mod; cout << tot << endl; } return 0; }
#include <algorithm> #include <cmath> #include <climits> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <set> #include <string> #include <unordered_set> #include <vector> #define REP(i, n) for(int i = 0; i < n; ++i) template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; } using namespace std; using LLONG = long long; const LLONG MOD = 1000000007; const int INF = INT_MAX - 1; class ModCalc { LLONG m_mod; vector<LLONG> m_fac; vector<LLONG> m_ifac; public: ModCalc(const LLONG mod) { m_mod = mod; } ModCalc(const int max, const LLONG mod) { m_mod = mod; m_fac = vector<LLONG>(max + 1); m_ifac = vector<LLONG>(max + 1); m_fac[0] = m_ifac[0] = 1; for (int i = 0; i < max; ++i) { m_fac[i + 1] = m_fac[i] * (i + 1) % mod; // n! (mod M) m_ifac[i + 1] = m_ifac[i] * ModPow(i + 1, mod - 2) % mod; // k!^(M-2) (mod M) } } const LLONG ModAdd(LLONG a, LLONG b) const { return (a + b) % m_mod; } const LLONG ModSubtract(LLONG a, LLONG b) const { LLONG diff = a - b; return diff >= 0 ? diff : diff + m_mod; } const LLONG ModMulti(LLONG a, LLONG b) const { LLONG prod = a * b % m_mod; return prod >= 0 ? prod : prod + m_mod; } const LLONG ModDiv(LLONG a, LLONG b) const { LLONG c = m_mod, u = 1, v = 0; while (c) { LLONG t = b / c; b -= t * c; swap(b, c); u -= t * v; swap(u, v); } u %= m_mod; if (u < 0) u += m_mod; return (a % m_mod) * u % m_mod; } const LLONG ModPow(LLONG x, LLONG n) const { LLONG ret = 1; while (n != 0) { if (n & 1) ret = ret * x % m_mod; x = x * x % m_mod; n = n >> 1; } return ret; } const LLONG ModComb(LLONG n, LLONG r) const { if (n == 0 && r == 0) return 1; if (n < r || n < 0) return 0; LLONG tmp = m_ifac[n - r] * m_ifac[r] % m_mod; return tmp * m_fac[n] % m_mod; } }; int main() { LLONG N, M, K; cin >> N >> M >> K; if (N > M + K) { cout << 0 << endl; } else { ModCalc m(2000001, MOD); cout << m.ModSubtract(m.ModComb(N + M, N), m.ModComb(N + M, M + K + 1)) << endl; } }
#include <bits/stdc++.h> #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define rep(i, n) for (int i = 0; i < (n); ++i) using std::cin; using std::cout; using std::istream; using std::ostream; using std::vector; using ll = long long; using ull = unsigned long long; // Mod int const int mod = 1000000007; struct mint { uint x; mint(): x(0) {} mint(ll x):x((x%mod+mod)%mod) {} mint operator-() const { return mint(0) - *this;} mint operator~() const { return mint(1) / *this;} 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=(ull)x*a.x%mod; return *this;} mint& operator/=(const mint& a) { x=(ull)x*a.pow(mod-2).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 operator/(const mint& a) const { return mint(*this) /= a;} mint pow(ll t) const { if(!t) return 1; mint res = pow(t/2); res *= res; return (t&1)?res*x:res; } bool operator<(const mint& a) const { return x < a.x;} bool operator==(const mint& a) const { return x == a.x;} bool operator!=(const mint& a) const { return x != a.x;} }; mint ex(mint x, ll t) { return x.pow(t);} istream& operator>>(istream&i,mint&a) {i>>a.x;return i;} //* ostream& operator<<(ostream&o,const mint&a) {o<<a.x;return o;} int main() { int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; vector<mint> fib(n + 2); fib[0] = fib[1] = 1; for (int i = 2; i <= n + 1; ++i) fib[i] = fib[i - 1] + fib[i - 2]; mint ans = fib[n] * a[0]; for (int i = 1; i < n; ++i) { mint coeff = fib[i] * fib[n - i] - fib[i - 1] * fib[n - i - 1]; ans += coeff * a[i]; } cout << ans << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; #define sfd(x) scanf("%d",&x) #define sfd2(x,y) scanf("%d%d",&x,&y) #define sfd3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define sfs(x) scanf("%s",x) #define sfll(x) scanf("%lld",&x) #define sfll2(x,y) scanf("%lld%lld",&x,&y) #define pfd(x) printf("%d",x) #define pfs(x) printf("%s",x) #define pfll(x) printf("%lld",x) #define nl printf("\n") #define sp printf(" ") #define pfyes printf("YES") #define pfno printf("NO") #define pf0 printf("0") #define FOR(i,a,b,in) for(int i=a;i<=b;i+=in) #define RFOR(i,a,b,de) for(int i=a;i>=b;i-=de) #define rep(i,n) for(int i=0;i<n;i++) #define pb push_back #define mkp make_pair #define mod 1000000007 typedef long long int ll; typedef vector<int> vi; typedef pair<int,int> pii; // ------------------------// int main() { int n; sfd(n); ll a[n]; rep(i,n) sfll(a[i]); if(n==1) { pfll(a[0]); return 0; } ll dp[n][2]; rep(i,n) { rep(j,2) dp[i][j]=0; } dp[1][0]=(a[0]+a[1])%mod; dp[1][1]=(a[0]-a[1])%mod; if(dp[1][1]<0) dp[1][1]+=mod; ll cp[n]; ll cn[n]; rep(i,n) { cp[i]=0; cn[i]=0; } cp[1]=1; cn[1]=1; for(int i=2;i<n;i++) { dp[i][0]=(dp[i-1][0]+dp[i-1][1]+(((a[i]*((cp[i-1]+cn[i-1])%mod))%mod)))%mod; dp[i][1]=(dp[i-1][0]-((a[i]*(cp[i-1])%mod)))%mod; cp[i]=(cp[i-1]+cn[i-1])%mod; cn[i]=cp[i-1]; if(dp[i][0]<0) dp[i][0]+=mod; if(dp[i][1]<0) dp[i][1]+=mod; } pfll((dp[n-1][0]+dp[n-1][1])%mod); return 0; }
#include<bits/stdc++.h> #define pii pair<int,int> #define eps 1e-7 #define equals(a,b) (fabs(a - b) < eps) #define fi first #define se second using namespace std; typedef long long ll; const int maxn = 1e6 + 5; const ll MOD = 1e9 + 7; ll rd(){ ll x = 0; int f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } struct PSegmentTree{ struct Node{ int l,r,d; Node *ls,*rs; }; int a[maxn]; Node pool[maxn * 30],*rt[maxn]; int top; Node* build(int l,int r){ Node *p = pool + (++top); p -> l = l,p -> r = r; if(l == r) { p -> d = a[l]; return p; } int mid = l + r >> 1; p -> ls = build(l,mid); p -> rs = build(mid + 1,r); return p; } Node* copyNode(Node* rt){ Node* p = pool + (++top); pool[top] = *rt; return p; } Node* change(Node* rt,int id,int x){ Node* p = copyNode(rt); if(p -> l == p -> r) { p -> d = x; return p; } int mid = p -> l + p -> r >> 1; if(id <= mid) p -> ls = change(p -> ls,id,x); else p -> rs = change(p -> rs,id,x); return p; } int query(Node* rt,int id){ if(rt -> l == rt -> r) { return rt -> d; } int mid = rt -> l + rt -> r >> 1; if(id <= mid) return query(rt -> ls,id); else return query(rt -> rs,id); } }; int main(){ int n = rd(); vector<int> v(n + 1); ll x = rd(); for(int i = 1;i <= n;i++) v[i] = rd(); ll res = 1e18; for(int c = 1;c <= n;c++){ int m = x % c; vector<vector<int>> dp(c + 1,vector<int>(c + 1,-1)); dp[0][0] = 0; for(int k = 1;k <= n;k++) for(int i = min(k ,c) - 1;i >= 0;i--) for(int j = 0;j < c;j++) if(~dp[i][j]) dp[i + 1][(j + v[k] % c) % c] = max(dp[i + 1][(j + v[k] % c)% c],dp[i][j] + v[k]); if(~dp[c][m]) res = min(res,(ll)(x - (ll)dp[c][m]) / c); } cout << res; }
#include <bits/stdc++.h> using namespace std; int main() { int N; int64_t X; cin >> N >> X; vector<int64_t> w(N); for (int i = 0; i < N; ++i) cin >> w[i]; // O(N^4) int64_t min_time = numeric_limits<int64_t>::max(); // Brute force value for k. for (int k = 1; k <= N; ++k) { // dpv[i][j][m] = max value we can get when selecting j items from prefix [:i] // that sum up to value m, modulo k. (i is 1-based) vector<vector<vector<int64_t>>> dpv(N + 1, vector<vector<int64_t>>(k + 1, vector<int64_t>(k, -1e10))); dpv[0][0][0] = 0; // Boundary condition. for (int i = 1; i <= N; ++i) { for (int j = 0; j <= k; ++j) { for (int m = 0; m < k; ++m) { if (j > 0) { // We take it. // new_mod = (old_mod + values[i - 1] % k) % k const int old_mod = (m - (w[i - 1] % k) + k) % k; dpv[i][j][m] = max(dpv[i][j][m], w[i - 1] + dpv[i - 1][j - 1][old_mod]); } // We don't take it. dpv[i][j][m] = max(dpv[i][j][m], dpv[i - 1][j][m]); } } } const int64_t max_val = dpv[N][k][X % k]; if (max_val >= 0) { // Because of bounds (X > 10^9) max_val cannot be greater than X. assert(X >= max_val); min_time = min(min_time, (X - max_val) / k); } } cout << min_time << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,s,e) for (ll i = s; i <= e; ++i) #define rrep(i,s,e) for (ll i = s; i >= e; --i) #define pb push_back #define pf push_front #define fi first #define se second #define all(a) a.begin(), a.end() typedef long long ll; typedef pair<ll, ll> ii; typedef vector<ii> vii; typedef vector<ll> vi; typedef vector<double> vd; typedef vector<string> vs; typedef vector<char> vc; typedef vector<ll> vll; int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, m, x, y; cin >> n >> m >> x >> y; vector<array<ll,3>> neibs[n+1]; rep (i,0,m-1) { ll a, b, t, k; cin >> a >> b >> t >> k; neibs[a].pb({b,t,k}); neibs[b].pb({a,t,k}); } //basic dijkstra priority_queue<ii, vii, greater<ii>> pq; pq.push({0,x}); ll dist[n+1]; memset(dist, -1, (n+1)*sizeof(ll)); dist[x] = 0; while (!pq.empty()) { ll T = pq.top().fi, city = pq.top().se; pq.pop(); if (dist[city] < T) continue; if (city==y) { cout << T << "\n"; return 0; } for (array<ll,3> v : neibs[city]) { ll b = v[0], t = v[1], k = v[2]; if (dist[b]==-1 || (T+k-1)/k*k+t < dist[b]) { dist[b] = (T+k-1)/k*k+t; pq.push({dist[b], b}); } } } cout << "-1\n"; return 0; }
#include "bits/stdc++.h" #include<sstream> using namespace std; typedef long long ll; #define _USE_MATH_DEFINES #include <math.h> #define NIL = -1; #define all(x) x.begin(),x.end() const ll INF = 1e9; const long long inf = 1e18; const ll INFL = 1e18; const ll MOD = 1e9 + 7; int digit(ll x) { int digits = 0; while(x > 0){ x /= 10; digits++; } return digits; } ll gcd(long long a,long long b) { if (a < b) swap(a,b); if (b == 0) return a; return gcd(b,a%b); } bool is_prime(long long N){ if (N == 1) return false; for (long long i = 2;i * i <= N;i++){ if (N % i == 0) return false; } return true; } ll lcm(ll a,ll b){ return ((a * b == 0)) ? 0 : (a / gcd(a,b) * b); } double DegreeToRadian(double degree){ return degree * M_PI / 180.0; } long long modpow(long long a, long long b, long long m){ long long ans = 1; while(b > 0){ if (b % 2 == 1){ ans *= a; ans %= m; } a *= a; a %= m; b /= 2; } return ans; } long long comb(long long x, long long y){ int z; if (y == 0) return 1; else { z = modpow(x, y/2, MOD)*modpow(x, y/2, MOD)%MOD; if (y % 2 == 1) z = z*x%MOD; return z; } } vector<pair<long long, long long>> prime_fact(long long x){ vector<pair<long long, long long>> res; for(int i = 2;i*i <= x;i++){ if (x % i == 0){ long long cnt = 0; while(x % i == 0){ cnt++; x /= i; } res.push_back({i, cnt}); } } if (x != 1){ res.push_back({x, 1}); } return res; } int64_t mod_inv(int64_t a, int64_t m){ int64_t b = m, u = 1, v = 0; while(b){ int64_t t = a/b; a -= t*b; swap(a, b); u -= t*v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } // {g, x, y}: ax + by = g tuple<long long, long long, long long> extgcd(long long a, long long b){ if (b == 0) return {a, 1, 0}; long long g, x, y; tie(g, x, y) = extgcd(b, a % b); return {g, y, x - a/b*y}; } int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; ////////////////////////////////////////////////////////////// int main(){ int n,m; long long x, y; cin >> n >> m >> x >> y; x--; y--; vector<vector<tuple<int, long long, long long>>> g(n + 1); for(int i = 0;i < m;i++){ int a, b; long long t, k; cin >> a >> b >> t >> k; a--; b--; g[a].push_back(make_tuple(b, t, k)); g[b].push_back(make_tuple(a, t, k)); } vector<long long> dist(n + 1, 1e18); priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q; dist[x] = 0; q.push({0, x}); long long ans = 1e18; while(!q.empty()){ auto p = q.top(); q.pop(); for(auto nx : g[p.second]){ if (dist[get<0>(nx)] > get<2>(nx)*((p.first + get<2>(nx) - 1) / get<2>(nx)) + get<1>(nx)){ dist[get<0>(nx)] = get<2>(nx)*((p.first + get<2>(nx) - 1) / get<2>(nx)) + get<1>(nx); if(p.second == y){ ans = min(ans, dist[get<0>(nx)]); } else { q.push({dist[get<0>(nx)], get<0>(nx)}); } } } } if (dist[y] == 1e18){ cout << -1 << endl; } else { cout << dist[y] << endl; } }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; using namespace __gnu_pbds; typedef long long ll; #define int long long typedef unsigned long long lu; typedef vector<ll> v; typedef vector<vector<ll> > vv; typedef vector<string> vs; typedef vector<pair<ll,ll>> vpr; typedef vector<bool>vb; typedef vector<double>vd; typedef long double ld; #define f(i,n) for(ll i = 0; i < n; i++) #define ff(i,n) for(ll i=1;i<=n;i++) #define pb push_back #define mp make_pair #define endl "\n" #define fi first #define se second #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define amax(x,y) if(y>x) x=y; #define amin(x,y) if(y<x)x=y; #define bg(x) x.begin() #define sz(x) (ll)x.size() #define in(x,n) for(ll i=0;i<n;i++)cin>>x[i] #define out(x,n) for(ll i=0;i<n;i++)cout<<x[i]<<" " #define mxt(a) *(max_element(a.begin(),a.end())) #define mnt(a) *(min_element(a.begin(),a.end()) #define tc ll t;cin>>t;while(t--) typedef pair<ll,ll> pi; #define yes cout<<"YES\n"; #define no cout<<"NO\n"; #define yesno(f) if(f) yes else no const v dx = {1, -1, 0, 0}; const v dy = {0, 0, 1, -1}; const ld PI = 2 * acos(0.0); ll cel(ll x1,ll y1){if((x1%y1)==0)return x1/y1;else return x1/y1+1;} ll power(ll a,ll b,ll m) { if(b==0) return 1; ll d=power(a,b/2,m); d=(d*d)%m; if(b&1) d=(d*a)%m; return d; } const ll mod=998244353; int MOD(int a) { if(a<0) a+=mod; if(a>=mod) a%=mod; return a; } // set_name.find_by_order(k) It returns to an iterator to the kth element (counting from zero) in the set in O(logn) time // set_name.order_of_key(k) It returns to the number of items that are strictly smaller than our item k in O(logn) time. /*string operations : str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1] str.substr (x) : returns a substring str[x],... end of string str.find(qtr) : returns the first occurenece of qtr in str */ int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n,m,k;cin>>n>>m>>k; vector<vector<char>>grid(n,vector<char>(m,'.')); for (int i = 0; i < k; ++i) { int h,w;char c;cin>>h>>w>>c; h--; w--; grid[h][w]=c; } vv row(n+1,v(m+1,0)),col(n+1,v(m+1,0)); for(int i=n-1;i>=0;i--) { for(int j=m-1;j>=0;j--) { row[i][j]=row[i][j+1]; col[i][j]=col[i+1][j]; if(grid[i][j]=='.') { row[i][j]++; col[i][j]++; } } } v powers(n+m+2,1); for(int i=1;i<=n+m+1;i++) powers[i]=MOD(powers[i-1]*3); vv dp(n+1,v(m+1,0)); dp[n-1][m-1]=1; if(grid[n-1][m-1]=='.') dp[n-1][m-1]=3; for(int i=n-1;i>=0;i--) { for(int j=m-1;j>=0;j--) { if(i==(n-1) && j==(m-1)) continue; if(grid[i][j]=='R') { dp[i][j]=MOD(dp[i][j+1]*powers[col[i+1][j]]); } else if(grid[i][j]=='D') { dp[i][j]=MOD(dp[i+1][j]*powers[row[i][j+1]]); } else if(grid[i][j]=='X') { dp[i][j]=MOD(MOD(dp[i][j+1]*powers[col[i+1][j]])+MOD(dp[i+1][j]*powers[row[i][j+1]])); } else { dp[i][j]=MOD(2*MOD(dp[i][j+1]*powers[col[i+1][j]])+2*MOD(dp[i+1][j]*powers[row[i][j+1]])); } } } cout<<dp[0][0]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(),(x).end() const long long MOD = 998244353; const long long INF = 9999999999999999; using ll = long long; 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; } long long pow(long long x, long long n, long long MM) { long long ret = 1; while (n > 0) { if (n & 1) ret = ret * x % MM; // n ใฎๆœ€ไธ‹ไฝbitใŒ 1 ใชใ‚‰ใฐ x^(2^i) ใ‚’ใ‹ใ‘ใ‚‹ x = x * x % MM; n >>= 1; // n ใ‚’1bit ๅทฆใซใšใ‚‰ใ™ } return ret; } int main(){ ll H,W,K; cin>>H>>W>>K; char ban[H+2][W+2] = {}; ll masu = 0; rep(i,H+1){ rep(k,W+1){ ban[i][k] = '.'; masu++; } } ll kakeru = pow(3,(H*W-K),MOD); ll ans[H+2][W+2]; rep(i,K){ ll a,b; char c; cin>>a>>b>>c; ban[a][b] = c; } rep(i,H+1){ rep(k,W+1){ ans[i][k] = 0; } } ll waru = pow(3,(MOD-2),MOD); ans[1][1] = kakeru; for(ll i = 1;i<=H;i++){ for(ll k = 1;k<=W;k++){ if(ban[i][k] == 'D'){ ans[i+1][k] += ans[i][k]; ans[i+1][k]%=MOD; } if(ban[i][k] == 'R'){ ans[i][k+1] += ans[i][k]; ans[i][k+1]%=MOD; } if(ban[i][k] == 'X'){ ans[i+1][k] += ans[i][k]; ans[i][k+1] += ans[i][k]; ans[i+1][k]%=MOD; ans[i][k+1]%=MOD; } if(ban[i][k] == '.'){ ans[i+1][k] += ans[i][k]*2%MOD*waru%MOD; ans[i][k+1] += ans[i][k]*2%MOD*waru%MOD; } } } cout << ans[H][W]%MOD << endl; return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; string S, T; cin >> S >> T; ll kotae = 0; queue<int> q; int jogai = -1; rep(i, N) { if (jogai >= 0 && S[i] == '1') { S[i] = '0'; kotae += i - jogai; jogai = -1; } if (T[i] == '1') q.push(i); if (S[i] == '1') { if (q.size()) { int tmp = q.front(); q.pop(); kotae += i - tmp; } else { jogai = i; } } } if (jogai == -1 && q.empty()) { co(kotae); } else co(-1); Would you please return 0; }
#include <iostream> #include <string> #include <vector> #include <deque> #include <algorithm> #include <math.h> #include <iomanip> #include <sstream> #define INF 1000000009 #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using p = pair<int, int>; int h,w; ll calc(vector<vector<int>> m,int a, int b, int x, int y){ if(a<0||b<0) return 0; if(x==h-1&&y==w-1){ if(m[x][y]==1&&a==0&&b==0) return 1; else if(m[x][y]==0&&a==0&&b==1) return 1; else return 0; } else{ if(m[x][y]==0){ m[x][y] = 1; vector<vector<int>> m1 = m; if(x==h-1){ m1[x][y+1] = 1; return calc(m,a,b-1,0,y+1) + calc(m1,a-1,b,0,y+1); }else{ vector<vector<int>> m2 = m; m1[x][y+1] = 1; m2[x+1][y] = 1; return calc(m,a,b-1,x+1,y) + calc(m1,a-1,b,x+1,y) + calc(m2,a-1,b,x+1,y); } }else{ if(x==h-1) return calc(m,a,b,0,y+1); else return calc(m,a,b,x+1,y); } } } int main() { ll ans; int a,b; ans = 0; cin >> h>>w>>a>>b; vector<vector<int>> m(h+1,vector<int>(w+1,0)); ans = calc(m,a,b,0,0); cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <cmath> #include <set> #include <unordered_set> #include <queue> #include <deque> using namespace std; using ll = long long; void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); } void _cout(){ cout << "\n"; } template <class Head, class... Tail> void _cout(Head&& head, Tail&&... tail){ cout << head; _cout(forward<Tail>(tail)...); } template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T> &p){ cout << "[" << p.first << ", " << p.second << "]"; return os; } #define Sq(x) (x)*(x) #define For(i, n) for(int i = 0; i < (n); i ++) #define Rep(n) For(_, n) #define Range(c) c.begin(), c.end() #define RevRange(c) c.rbegin(), c.rend() #define Contains(c, x) (find(Range(c), x) != c.end()) #define Search(rb, re, x) distance(rb, find(rb, re, x)) #define Sort(a) sort(Range(a)) #define DeSort(a) sort(RevRange(a)) #define Reverse(c) reverse(Range(c)) #define Unique(a) a.erase(unique(Range(a)), a.end()) #define Cusum(T, xs, sxs) vector<T> sxs(xs.size()+1); For(i, (int)xs.size()) sxs[i+1] = sxs[i] + xs[i] #define Cin(T, ...) T __VA_ARGS__; _cin(__VA_ARGS__) #define Cins(T, n, xs) vector<T> xs(n); For(i, n) cin >> xs[i] #define Cins2(T, n, xs, ys) vector<T> xs(n), ys(n); For(i, n) cin >> xs[i] >> ys[i] #define Cins3(T, n, xs, ys, zs) vector<T> xs(n), ys(n), zs(n); For(i, n) cin >> xs[i] >> ys[i] >> zs[i] #define Cinm(T, n, map) unordered_map<T, int> map; Rep(n){ Cin(T, x); map[x] ++; } #define Cout(...) _cout(__VA_ARGS__) #define Couts(xs) { for(const auto &e : xs) cout << e << "\n"; } #define Coutyn(cond) Cout((cond) ? "yes" : "no") #define CoutYn(cond) Cout((cond) ? "Yes" : "No") #define CoutYN(cond) Cout((cond) ? "YES" : "NO") #define Return(expr) { Cout(expr); return 0; } #define vc vector #define Mini(a, x) a = min(a, x) #define Maxi(a, x) a = max(a, x) // constexpr int MOD = 1e9+7; int main(void){ Cin(int, n); Cins(int, n, c); vc<vc<int>> e(n); Rep(n-1){ Cin(int, a, b); a--, b--; e[a].push_back(b); e[b].push_back(a); } unordered_set<int> cs; set<int> ans; auto f = [&](int i, int par, auto self) -> void { bool good = cs.count(c[i]) == 0; if(good){ ans.insert(i+1); cs.insert(c[i]); } for(const int &j : e[i]) if(j != par) self(j, i, self); if(good) cs.erase(c[i]); }; f(0, -1, f); Couts(ans); }
#include<bits/stdc++.h> using namespace std; #define int long long int #define ld long double template<class T> class MS{ public: set<T> st; map<T, int> cnt; int size = 0; MS(){} void insert(T x){ st.insert(x); size++; cnt[x] += 1; } void remove(T x){ if(cnt[x] == 0) return; cnt[x] -= 1; size--; if(cnt[x] == 0) st.erase(x); } bool find(T x){ return (cnt[x] > 0); } T begin(){ return *st.begin(); } T rbegin(){ return *st.rbegin(); } }; vector<vector<int>> adj; int N; vector<int> C; MS<int> path; vector<int> ans; void DFS(int u, int p = -1){ // cout << u << endl; if(!path.find(C[u])) ans.push_back(u); path.insert(C[u]); for(int v: adj[u]){ if(v == p) continue; DFS(v, u); path.remove(C[v]); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N; C.resize(N + 1); adj.resize(N + 1); for(int i = 1; i <= N; i++){ cin >> C[i]; } for(int i = 0, a, b; i < N - 1; i++){ cin >> a >> b; // cout << a << " " << b << endl; adj[a].push_back(b); adj[b].push_back(a); } DFS(1); sort(ans.begin(), ans.end()); for(int i: ans) cout << i << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define int ll #define ll long long #define rep(i,s,f) for(i=s;i<f;i++) #define print(v) for(auto &z:v) cout<<z<<' ';cout<<'\n' #define db cout<<"db: " #define _ <<' '<< #define pb push_back #define pii pair<int,int> #define F first #define S second #define B begin() #define E end() #define all(v) v.B,v.E #define sz(v) (int)((v).size()) #define vi vector<int> #define vii vector<pair<int,int>> #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define clk1 clock_t start_time=clock() #define clk2 cout<<(clock()-start_time)/(double)CLOCKS_PER_SEC #define clean(arr) memset(arr,0,sizeof(arr)) #define mod 1000000007 #define mod2 998244353 #define space 3000005 //<<fixed << setprecision(9) int arr[space]; int n; int ways(int x) { x-=2; if(x>n-1) { int diff=x-(n-1); x-=2*diff; } return x+1; } int32_t main() { int i,j,t; boost; // n=10; // cin>>i; // cout<<ways(i); int k; cin>>n>>k; int ab; rep(ab,2,2*n+1) { int w = ways(ab); arr[ab+1]+=w; arr[ab+n+1]-=w; } rep(i,1,3*n+1) arr[i]+=arr[i-1]; rep(i,1,3*n+1) { if(arr[i]<k)k-=arr[i]; else break; } int sum=i; int bea,tas; rep(bea,1,n+1) { if(bea+n+n<sum )continue; int jmin,jmax; int l=1,r=n,ans; while(l<=r) { int mid=(l+r)/2; if(bea+mid+n>=sum) { r=mid-1; ans=mid; } else l=mid+1; } jmin=ans; l=1,r=n,ans; while(l<=r) { int mid=(l+r)/2; if(bea+mid+1<=sum) { l=mid+1; ans=mid; } else r=mid-1; } jmax=ans; if(k>(jmax-jmin+1)) k-= (jmax-jmin+1); else { tas = jmin +k-1; break; } } cout<<bea _ tas _ sum-bea-tas; return 0; }
#include<bits/stdc++.h> #define int long long #define x first #define y second #define mp make_pair #define pb push_back #define endl "\n" using namespace std; const int max_n = 1e6+100; const int limit = 2e5+10; const int max_k = 1e7+10; const long long inf = 1e18; const int off_set = 1e3+10; const int Mod = 200; int dp[3*max_n][4]; int p[3*max_n][4]; int intersection(pair<int,int> a,pair<int,int> b){ if(a.x>b.y || a.y<b.x) return 0; pair<int,int> c(max(a.x,b.x),min(a.y,b.y)); return c.y-c.x+1; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,k;cin>>n>>k; k--; dp[0][0]=1; for(int s=0;s<=3*n;s++) p[s][0]=1; for(int j=1;j<=3;j++){ for(int s=1;s<=3*n;s++){ dp[s][j]=p[s-1][j-1]; if(s-n-1>=0){ dp[s][j]-=p[s-n-1][j-1]; } p[s][j]=dp[s][j]; p[s][j]+=p[s-1][j]; } } int S; for(int s=1;s<=3*n;s++){ if(dp[s][3]>k){ S = s; break; }else{ k-=dp[s][3]; } } for(int i=1;i<=n;i++){ pair<int,int> I1(1,n); pair<int,int> I2(S-i-n,S-i-1); int c = intersection(I1,I2); if(c<=k){ k-=c; }else{ for(int j=1;j<=n;j++) if(S-i-j>=1 && S-i-j<=n){ if(k==0){ cout<<i<<" "<<j<<" "<<S-i-j<<endl; return 0; }else{ k--; } } assert(false); } } }
#include<iostream> #include<vector> #include<algorithm> #include<cmath> #include<map> #include<cstdlib> #include<numeric> #include<set> #include<utility> #include<limits> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using ll = long long; template<class T>inline bool chmax(T& a, T b){if(a<b) { a=b;return true; } return false;} template<class T>inline bool chmin(T& a, T b){if(a>b) { a=b;return true; } return false;} const int MOD = 1000000007; int main(){ string s;cin >> s; if(s.size() == 1){ if(s[0] == '8'){ cout << "Yes" << endl; return 0; }else{ cout << "No" << endl; return 0; } } vector<int> exist(10); for(int i=0;i<s.size();i++){ int n = (int)(s[i] - '0'); exist[n]++; } //rep(i,exist.size()){ // cout << "exist[" << i << "]ใฏ" << exist[i] << endl; //} bool flag = false; int i_start,i_end; if(s.size()==2){ i_start=16;i_end=100; }else{ i_start=104;i_end=1000; } for(int i=i_start;i<i_end;i+=8){ int a,b,c; int i_copy = i; a = i_copy%10;i_copy/=10; b = i_copy%10;i_copy/=10; c = i_copy%10;i_copy/=10; //cout << "a b cใฏ" << a << ' ' << b << ' ' << c << endl; vector<int> e_copy(exist.size()); copy(exist.begin(),exist.end(),e_copy.begin()); if(e_copy[a]>0){ e_copy[a]--; if(e_copy[b]>0){ e_copy[b]--; if(s.size() == 2){ flag = true; break; } if(e_copy[c]>0){ flag = true; break; } } } } if(flag) cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ string S; int num; cin>>S; vector<int> count(10,0); vector<int> comp(10,0); for(int i=0;i<S.size();i++){ count.at(S.at(i)-48)++; } if(S.size()==1){ if(S.at(0)-48==8){ cout<<"Yes"<<endl; return 0; } } else if(S.size()==2){ if(((S.at(0)-48)*10+S.at(1)-48)%8==0||((S.at(1)-48)*10+S.at(0)-48)%8==0){ cout<<"Yes"<<endl; return 0; } } else{ for(int i=1;i<10;i++){ comp.at(i)++; for(int j=1;j<10;j++){ comp.at(j)++; for(int k=1;k<10;k++){ comp.at(k)++; if(comp.at(k)>count.at(k)||comp.at(i)>count.at(i)||comp.at(j)>count.at(j)){ comp.at(k)--; continue; } if((100*i+10*j+k)%8==0){ cout<<"Yes"<<endl; return 0; } comp.at(k)--; } comp.at(j)--; } comp.at(i)--; } } cout<<"No"<<endl; }
#include <bits/stdc++.h> #define int long long using namespace std; int n,k,ans=0; int w(int x,int t)//ๅ’Œไธบx,ๅœจtไปฅๅ†… { if (x<2) return 0; if (x==2) return 1; if (x<=t) return x-1; if (x>2*t) return 0; int l=t; int r=x-t; if (l>r) swap(l,r); return r-l+1; } signed main() { cin>>n>>k; for (int i=0;i<=n*2;i++)//3 2 { int x=i,y=i-k; ans+=w(x,n)*w(y,n); } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define int long long #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #define pb push_back #define mod 1000000007 #define lld long double #define mii map<int, int> #define mci map<char, int> #define msi map<string, int> #define pii pair<int, int> #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=x; i<y; i++) #define fill(a,b) memset(a, b, sizeof(a)) #define vi vector<int> #define setbits(x) __builtin_popcountll(x) #define w(x) int x; cin>>x; while(x--) using namespace std; const long long N=100005, INF=2000000000000000000; vi g[N]; int power(int a, int b, int p) { if(a==0) return 0; int res=1; a%=p; while(b>0) { if(b&1) res=(res*a)%p; b>>=1; a=(a*a)%p; } return res; } vi prime; bool isprime[N]; void pre() { for(int i=2;i<N;i++) { if(isprime[i]) { for(int j=i*i;j<N;j+=i) isprime[j]=false; prime.pb(i); } } return; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not // present in array return -1; } bool isPowerOfTwo(int n) { if(n==0) return false; return (ceil(log2(n)) == floor(log2(n))); } int gcd(int a, int b) { if(a < b) return gcd(b, a); else if(a%b == 0) return b; else return gcd(b, a%b); } bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int spf[N+1]; void spfFun(){ for(int i=0;i<=N;i++) spf[i]=i; for(int i=2;i*i<=N;i++){ if(spf[i]==i){ for(int j=i*i;j<=N;j+=i){ spf[j] = min(spf[j],i); } } } } bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0); } void print(bool n){ if(n){ cout<<"YES"; }else{ cout<<"NO"; } } int n,k; int cal(int a){ if(a>n){ return 2*n - a + 1; } return a-1; } int32_t main() { IOS; // pre(); // fill(isprime,true); cin>>n>>k; unsigned int ans=0; for(int i=2;i<=2*n;i++){ int other = i-k; // cout<<i<<" "<<cal(i)<<"\n"; if(other<=1 || other>2*n) continue; ans+=cal(i)*cal(other); } cout<<ans; }
#include <iostream> #include <cstdio> using namespace std; string s; int read(){ int x = 0; char c = getchar(); while(c < '0' || c > '9') c = getchar(); while(c >= '0' && c <= '9') x = x * 10 + (c ^ 48),c = getchar(); return x; } int main(){ cin >> s; for(int i = 0; i < s.size(); i ++){ if(i % 2 && s[i] > 'Z' || (i % 2 == 0) && s[i] <= 'Z'){ printf("No\n"); return 0; } } printf("Yes\n"); return 0; }
#include <bits/stdc++.h> using namespace std; bool has_seven(int x, int b) { while (x > 0) { if (x % b == 7) return true; x /= b; } return false; } int main() { int N; cin >> N; int cnt = 0; for (int n = 1; n <= N; n++) { if (has_seven(n, 10) || has_seven(n, 8)) { cnt++; } } cout << N - cnt << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N], b[N], c[N], sum[N], n, m; vector<int> vt[N]; inline int lowbit(int x) { return x & (-x); } void update(int x, int v) { while (x <= n) { sum[x] += v; x += lowbit(x); } } int query(int x) { int ans = 0; while (x) { ans += sum[x]; x -= lowbit(x); } return ans; } int query(int l, int r) { return query(r) - query(l - 1); } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); c[i] = a[i] + i; } sort(c + 1, c + 1 + n); m = unique(c + 1, c + 1 + n) - (c + 1); for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); int pos = lower_bound(c + 1, c + 1 + m, b[i] + i) - c; if (c[pos] != b[i] + i) { puts("-1"); return 0; } b[i] = pos; } for (int i = n; i >= 1; i--) { vt[lower_bound(c + 1, c + 1 + m, a[i] + i) - c].push_back(i); } long long ans = 0; for (int i = 1; i <= n; i++) { if (!vt[b[i]].size()) { puts("-1"); return 0; } int cur = vt[b[i]].back(); ans += cur + query(cur + 1, n) - i; vt[b[i]].pop_back(); update(cur, 1); } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MOD int(1e9+7) #define INF int(1e9+7) #define LINF ll(1e18+7) #define PI acos(-1) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define P pair<ll,ll> #define chmax(x,y) (x = max(x,y)) #define chmin(x,y) (x = min(x,y)) ll ext_gcd(ll a, ll b, ll &x, ll &y){ if(b==0){ x=1; y=0; return a; } ll d=ext_gcd(b,a%b,y,x); y -= a/b*x; return d; } P crt(const vector<ll> &b, const vector<ll> &m){ ll r=0,M=1; rep(i,(int)b.size()){ ll p,q; ll d = ext_gcd(M,m[i],p,q); if((b[i]-r)%d != 0) return P(0,-1); ll tmp = (b[i]-r)/d * p %(m[i]/d); r += M * tmp; M *= m[i]/d; } return P((r+M)%M,M); } int main(){ int t; cin>>t; ll x,y,p,q; rep(i,t){ ll ans=LLONG_MAX; cin>>x>>y>>p>>q; for(ll t1=x; t1<x+y; t1++){ for(ll t2=p; t2<p+q; t2++){ ll r,mod; vector<ll> b(2),m(2); b[0]=t1; b[1]=t2; m[0]=2*x+2*y; m[1]=p+q; tie(r,mod)=crt(b,m); if(mod!=-1) chmin(ans,r); } } if(ans==LLONG_MAX) cout<<"infinity"<<endl; else cout<<ans<<endl; } }
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define STRINGIFY(n) #n #define TOSTRING(n) STRINGIFY(n) #define PREFIX "#" TOSTRING(__LINE__) "| " #define debug(x) \ { \ std::cout << PREFIX << #x << " = " << x << std::endl; \ } std::ostream& output_indent(std::ostream& os, int ind) { for(int i = 0; i < ind; i++) os << " "; return os; } template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p); template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v); template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) { return (os << "(" << p.first << ", " << p.second << ")"); } template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { os << "["; for(int i = 0;i < v.size();i++) os << v[i] << ", "; return (os << "]"); } template<class T> static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); } template<class... Tail> static inline auto ndvec(size_t&& n, Tail&&... tail) { return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...)); } template<class Cond> struct chain { Cond cond; chain(Cond cond) : cond(cond) {} template<class T> bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; } }; template<class Cond> chain<Cond> make_chain(Cond cond) { return chain<Cond>(cond); } int main() { i64 N; cin >> N; vector<i64> x(N), y(N); rep(i,0,N) { cin >> x[i] >> y[i]; } rep(i,0,N) rep(j,0,N) rep(k,0,N) { if(i == j || j == k || k == i) continue; i64 a = (x[i] - x[k]) * (y[j] - y[k]) - (x[j] - x[k]) * (y[i] - y[k]); if(a == 0) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define lld long double #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define f first #define s second #define mk make_pair /*----------------------DEBUGGER---------------------------*/ #ifndef ONLINE_JUDGE #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #endif void _print(ll t) {cerr << t;} void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(lld t) {cerr << t;} void _print(double t) {cerr << t;} void _print(ull t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.f); cerr << ","; _print(p.s); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} /*----------------------DEBUGGER---------------------------*/ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif int t = 1; //cin >> t; while (t--) { int n; cin>>n; vector<pair<int,string>> v; for(int i=0;i<n;i++) { pair<int,string> p; cin>>p.s; cin>>p.f; v.pb(p); } sort(v.begin(),v.end()); cout<<v[v.size()-2].s<<"\n"; }//while }//main
#include <bits/stdc++.h> using namespace std; #define f first #define s second #define pb push_back #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define db(x) cout << '>' << #x << ':' << x << endl; #define sz(x) ((int)(x).size()) #define newl cout << "\n" #define ll long long int #define vi vector<int> #define vll vector<long long> #define vvll vector<vll> #define pll pair<long long, long long> #define fast_io() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); ll tc, n, m, k; int main() { fast_io(); #ifndef ONLINE_JUDGE freopen("../input.txt", "r", stdin); freopen("../output.txt", "w", stdout); #endif cin>>n>>k; vll start(n), end(n), amount(n); rep(i, 0, n) cin>>start[i]>>end[i]>>amount[i]; auto solve = [&]() { vector<pair<pair<int, int>, int>> events; rep(i, 0, n) { events.pb({{start[i], 1}, amount[i]}); events.pb({{end[i], -1}, amount[i]}); } sort(events.begin(), events.end()); ll curr = 0; rep(i, 0, sz(events)) { auto id = events[i].f.s; if(id == 1) { curr += events[i].s; } else { curr -= events[i].s; } if(curr > k) return false; } return true; }; if(solve()) { cout<<"Yes"; } else { cout<<"No"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n);i++) void out(string s){cout<<s<<endl;} void outInt(int n){cout<<n<<endl;} void outLl(long long ll){cout<<ll<<endl;} int main(){ int a,b,c; cin>>a>>b>>c; if(a*a+b*b < c*c){ out("Yes"); }else{ out("No"); } }
#include<bits/stdc++.h> using namespace std; const int N=1E5+5; int n; double a[N],r,ans,p=1E-7; double f(double x){ double s=0; for(int i=1;i<=n;i++){ s=s+min(a[i],2*x); } return s-x*n; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lf",&a[i]),ans+=a[i],r=max(r,a[i]); r/=2.0; double l=0; while(l+p<r){ double lmid=l+(r-l)/3; double rmid=r-(r-l)/3; if(f(lmid)>f(rmid)){r=rmid;} else{l=lmid;} } printf("%.7lf",(ans-f(l))/(n*1.0)); }
#include <bits/stdc++.h> using namespace std; #define SZ(x) (int)(x).size() #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REPR(i,n) for(int i=(n)-1;i>=0;i--) #define ALL(s) (s).begin(), (s).end() #define so(V) sort(ALL(V)) #define rev(V) reverse(ALL(V)) #define uni(v) v.erase( unique(ALL(v)) , v.end()); typedef long long unsigned int llu; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<vi> vvi; const double EPS = 1e-9; const int MOD = 1e9 + 7; const int INF = (1 << 29); const ll LINF = 1e18; const double PI = acos(-1); template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template<typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) { t = v; } template<typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) { for (auto& e : t) fill_v(e, v); } 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; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<double> A(N); REP(i, N)cin >> A[i]; so(A); double X = A[N / 2] / 2.0; double ans = 0; REP(i, N) { ans += X + max(0.0, A[i] - 2 * X); } ans /= N; printf("%.10f\n", ans); return 0; }
#include <bits/stdc++.h> #include <algorithm> #define max(p,q)((p)>(q)?(p):(q)) #define min(p,q)((p)<(q)?(p):(q)) #define rep(i, n) for (int i = 0; i < n; i++) #define INF ((1LL<<62)-(1LL<<31)) #define MOD 1000000007 using ll = long long; using namespace std; using Graph = vector<vector<int>>; int main(){ int a, b, x, y; cin >> a >> b >> x >> y; int diff; if (a <= b) diff = abs(a - b); else diff = abs(a - b) - 1; int up = min(x * 2, y); int res = diff * up + x; cout << res; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define isNum(a) (a >= '0' && a <= '9') #define SP putchar(' ') #define EL putchar('\n') #define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout) template <class T> void read(T &); template <class T> void write(const T &); typedef long long ll; typedef unsigned long long ull; typedef const long long &cll; typedef const int &ci; typedef std::pair<int, int> pii; const int iinf = 2147483647; const ll llinf = 9223372036854775807ll; using std::abs; using std::max; using std::min; int main() { int n, k; read(n), read(k); ll ans = 0; for (int i = max(2, k + 2); i <= 2 * n; ++i) { if (i - 2 * n > k) { break; } int na, nb; if (i <= n) { na = i - 1; } else { na = 2 * n - i + 1; } if (i - k <= n) { nb = i - k - 1; } else { nb = 2 * n - (i - k) + 1; } ans += na * 1ll * nb; } write(ans), EL; return 0; } template <class T> inline void read(T &Re) { T k = 0; char ch = getchar(); int flag = 1; while (!isNum(ch)) { if (ch == '-') { flag = -1; } ch = getchar(); } while (isNum(ch)) { k = (k << 1) + (k << 3) + ch - '0'; ch = getchar(); } Re = flag * k; } template <class T> inline void write(const T &Wr) { if (Wr < 0) { putchar('-'); write(-Wr); } else { if (Wr < 10) { putchar(Wr + '0'); } else { write(Wr / 10); putchar((Wr % 10) + '0'); } } }
#include<stdio.h> int main() { long int n,x; scanf("%ld %ld",&n,&x); int i=1,flag=0; double r=0; while(i<=n) { double v,p; scanf("%lf %lf",&v,&p); r= r+ v*p; if(r>x*100) { flag=1; break; } i++; } if(flag) { printf("%d\n",i ); } else { printf("-1\n"); } }
#include <bits/stdc++.h> using namespace std; using int128 = __int128_t; using int64 = long long; using int32 = int; using uint128 = __uint128_t; using uint64 = unsigned long long; using uint32 = unsigned int; #define ALL(obj) (obj).begin(),(obj).end() template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>; constexpr int64 MOD = 1'000'000'000LL + 7; //' constexpr int64 MOD2 = 998244353; constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL; constexpr int64 LOWINF = 1'000'000'000'000'000LL; //' constexpr long double PI = 3.1415926535897932384626433L; template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);} template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));} template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}} 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;} template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} void print(void) {cout << endl;} template <class Head> void print(Head&& head) {cout << head;print();} template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);} template <class T> void chmax(T& a, const T b){a=max(a,b);} template <class T> void chmin(T& a, const T b){a=min(a,b);} vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;} inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;} inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b) void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;} void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;} void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;} /** * @url * @est */ int main() { cin.tie(0);ios::sync_with_stdio(false); int N; int64 X; cin >> N >> X; vector<int64> V(N),P(N); for(int i=0;i<N;++i) cin >> V[i] >> P[i]; X *= 100; for(int i=0;i<N;++i) { V[i] *= P[i]; X -= V[i]; corner(X < 0,i+1); } cout << -1 << endl; return 0; }
// #include<bits/stdc++.h> // using namespace std; // #define N 100000 // int getRandom(int l, int r) // { // return l + rand()%(r-l+1); // } // int32_t main() // { // #ifndef ONLINE_JUDGE // // For getting input from input.txt file // // freopen("input.txt", "r", stdin); // // Printing the Output to output.txt file // freopen("input.txt", "w", stdout); // #endif // srand(time(0)); // int t = 10; // cout << t << "\n"; // while(t--) // { // int n = getRandom(199, 200); // int m = getRandom(199, 200); // cout << n << "\n"; // cout << m << "\n"; // for(int i = 0; i < n; i++) // { // for(int j = 0; j < m; j++) // { // cout << 0 << " "; // } // } // cout << "\n~\n"; // } // return 0; // } // ---------------------------- DIVIDER ---------------------------- // // #include<bits/stdc++.h> // using namespace std; // class Solution { // public: // bool ValidCorner(const vector<vector<int> >& matrix) { // int rows = matrix.size(); // if (rows == 0) // return false; // int columns = matrix[0].size(); // unordered_map<int, unordered_set<int> > ump; // for (int i = 0; i < rows; ++i) // { // for (int j = 0; j < columns - 1; ++j) // { // for (int k = j + 1; k < columns; ++k) // { // if (matrix[i][j] == 1 && matrix[i][k] == 1) // { // if (ump.find(j) != ump.end() && ump[j].find(k) != ump[j].end()) // return true; // if (ump.find(k) != ump.end() && ump[k].find(j) != ump[k].end()) // return true; // ump[j].insert(k); // ump[k].insert(j); // } // } // } // } // return false; // } // }; // int32_t main() // { // #ifndef ONLINE_JUDGE // // For getting input from input.txt file // freopen("input.txt", "r", stdin); // // Printing the Output to output.txt file // freopen("output.txt", "w", stdout); // #endif // ios_base::sync_with_stdio(false); // cin.tie(NULL); // cout.tie(NULL); // int t; // cin >> t; // while(t--) // { // int row, col; // cin>> row>> col; // vector<vector<int> > matrix(row); // for(int i=0; i<row; i++) // { // matrix[i].assign(col, 0); // for( int j=0; j<col; j++) // { // cin>>matrix[i][j]; // } // } // char ch; // cin >> ch; // Solution ob; // if (ob.ValidCorner(matrix)) // cout << "Yes"; // else // cout << "No"; // cout << "\n~\n"; // } // return 0; // } #include<bits/stdc++.h> using namespace std; #define int long long #define mod (int)(1e9+7) #define endl '\n' #define MAXI (int)(1e16) #define N 105 int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int a[n][2]; for(int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1]; int ans = 9999999999; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i!=j) ans = min(ans, max(a[i][0], a[j][1])); else ans = min(ans, a[i][0] + a[j][1]); } } cout << ans; return 0; }
#include<bits/stdc++.h> #define f(i,a,b) for(int i=a;i<b;i++) #define ll long long #define pb push_back #define mp make_pair #define vp vector<pair<int,int>> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; int a,b; vp v1,v2; f(i,0,n) { cin>>a>>b; v1.pb(mp(a,i)); v2.pb(mp(b,i)); } sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); int mn=INT_MAX; f(i,0,n-1) f(j,i,i+2) if(v1[i].second!=v2[j].second && mn>max(v1[i].first,v2[j].first)) mn=max(v1[i].first,v2[j].first); else if(mn>v1[i].first+v2[j].first) mn=v1[i].first+v2[j].first; cout<<mn; cout<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define deb(k) cerr << #k << ": " << k << "\n"; #define size(a) (int)a.size() #define fastcin cin.tie(0)->sync_with_stdio(0); #define st first #define nd second #define pb push_back #define mk make_pair #define int long long typedef long double ldbl; typedef double dbl; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef map<int, int> mii; typedef vector<int> vint; #define MAXN 300100 #define MAXLG 20 const int inf = 0x3f3f3f3f; //const ll mod = 1000000007; const ll linf = 0x3f3f3f3f3f3f3f3f; const int N = 300100; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, k, mod; int dp[110][1000010]; void solve(){ cin>>n>>k>>mod; dp[0][0] = 1; int s = 0; for(int i=1;i<=n;i++){ vint cnt(i); s += i; for(int j=0;j<=s*k;j++){ int r = j % i; cnt[r] += dp[i-1][j]; if(j - i*(k+1) >= 0) cnt[r] -= dp[i-1][j - i*(k+1)]; cnt[r] = (cnt[r]%mod + mod)%mod; dp[i][j] = cnt[r]; } } s = 0; for(int i=1;i<=n;i++){ s += i; int ans = 0; for(int j=0;j<=s*k;j++){ ans += dp[i-1][j] * dp[n-i][j]; ans %= mod; } ans = ans * (k+1) + mod - 1; ans %= mod; cout<<ans<<"\n"; } // Have you read the problem again? // Maybe you understood the wrong problem } int32_t main(){ fastcin; int t_ = 1; //cin>>t_; while(t_--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ vector<vector<vector<long double>>> DP(101,vector<vector<long double>>(101,vector<long double>(101,0))); cout << fixed << setprecision(10); long double A,B,C; cin >> A >> B >> C; for(long double i=99;i>=A;i--){ for(long double j=99;j>=B;j--){ for(long double k=99;k>=C;k--){ DP[i][j][k] += (DP[i+1][j][k]+1)*(i/(i+j+k)); DP[i][j][k] += (DP[i][j+1][k]+1)*(j/(i+j+k)); DP[i][j][k] += (DP[i][j][k+1]+1)*(k/(i+j+k)); } } } cout << DP[A][B][C] << endl; }
#include <algorithm> #include <array> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define int long long #define ll long long #define pii std::pair<int, int> #define pdd std::pair<double, double> #define INF (1LL << 33) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define SHOW(p) \ if (test) \ cout << #p " : " << p << endl; bool test = false; template <class T> [[maybe_unused]] T ext_gcd(T a, T b, T& x, T& y) { // returns gcd(a,b) // ax + by = gcd(a,b) if (b == 0) { x = 1; y = 0; return a; } T d = ext_gcd(b, a % b, y, x); y -= a / b * x; return d; }; int T, N, S, K, x, y, gcd; signed main() { // test = true; cin >> T; REP(i, T) { x = y = 0; cin >> N >> S >> K; gcd = ext_gcd(N, K, x, y); SHOW(gcd); y *= -1; if (test) cout << "x: " << x << " y: " << y << endl; if (S % gcd == 0) { y *= (S / gcd); if (test) cout << y << " " << N / gcd << endl; y %= (N / gcd); if (y < 0) { y += N / gcd; } cout << y << endl; } else { // NO cout << -1 << endl; } if (test) cout << endl; } return 0; }
#include<iostream> #include<cstdio> #include<cmath> using namespace std; typedef long long ll; ll exgcd(ll a,ll b,ll &x,ll &y)//ๆ‰ฉๅฑ•ๆฌงๅ‡ ้‡Œๅพ—็ฎ—ๆณ• { if(b==0) { x=1;y=0; return a; //ๅˆฐ่พพ้€’ๅฝ’่พน็•Œๅผ€ๅง‹ๅ‘ไธŠไธ€ๅฑ‚่ฟ”ๅ›ž } ll r=exgcd(b,a%b,x,y); ll temp=y; //ๆŠŠx yๅ˜ๆˆไธŠไธ€ๅฑ‚็š„ y=x-(a/b)*y; x=temp; return r; //ๅพ—ๅˆฐa b็š„ๆœ€ๅคงๅ…ฌๅ› ๆ•ฐ } int main() { int t; cin >> t; while(t--) { ll n, s, k; cin >> n >> s >> k; ll x, y; ll r = exgcd(n, -k, x, y); // cout << r << " " << x << " " << y << endl; y = y * s / r; ll t = n / r; if(t < 0) t = -t; if(s % r != 0) cout << -1 << endl; else cout << (y % t + t) % t << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long #define ls id << 1 #define rs id << 1 | 1 #define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type)) #define memarray(array, value) memset(array, value, sizeof(array)) #define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value) #define fillvector(v, value) fill((v).begin(), (v).end(), value) #define pb(x) push_back(x) #define st(x) (1LL << (x)) #define pii pair<int, int> #define mp(a, b) make_pair((a), (b)) #define Flush fflush(stdout) #define vecfirst (*vec.begin()) #define veclast (*vec.rbegin()) #define vecall(v) (v).begin(), (v).end() #define vecupsort(v) (sort((v).begin(), (v).end())) #define vecdownsort(v, type) (sort(vecall(v), greater<type>())) #define veccmpsort(v, cmp) (sort(vecall(v), cmp)) using namespace std; const int N = 500050; const int inf = 0x3f3f3f3f; const ll llinf = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const int MOD = 1e9 + 7; const double PI = acos(-1.0); clock_t TIME__START, TIME__END; void program_end() { #ifdef ONLINE printf("\n\nTime used: %.6lf(s)\n", ((double)TIME__END - TIME__START) / 1000); system("pause"); #endif } int n; pair<ll, ll> p[N]; bool check(int i, int j, int k) { pair<ll, ll> v1 = mp(p[i].first - p[j].first, p[i].second - p[j].second); pair<ll, ll> v2 = mp(p[j].first - p[k].first, p[j].second - p[k].second); if(v1.first * v2.second - v1.second * v2.first == 0) return 1; return 0; } inline void solve() { cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i].first >> p[i].second; for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) for (int k = j + 1; k <= n; ++k) { if(check(i,j,k)) return puts("Yes"), void(); } puts("No"); } int main() { TIME__START = clock(); int Test = 1; // scanf("%d", &Test); while (Test--) { solve(); // if (Test) // putchar('\n'); } TIME__END = clock(); program_end(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long set<int> returnSet(vector<vector<int>> &v, int x) { set<int> st; for (int i = 0; i < v.size(); i++) { int val = 0; int pow = 1; for (int j = 0; j < 5; j++) { if (v[i][j] >= x) val += pow; pow *= 2; } st.insert(val); } return st; } signed main() { int n; cin >> n; vector<vector<int>> v(n, vector<int>(5)); for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) { cin >> v[i][j]; } } int l = 0, r = INT_MAX; int res = 0; while(r - l > 1) { int mid = (l + r) / 2; set<int> st = returnSet(v, mid); // for (auto it : st) cout << it << " "; // cout << '\n'; bool ok = false; for (auto v1 : st) { for (auto v2: st) { for (auto v3: st) { if ((v1 | v2 | v3) == (1 << 5) - 1) ok = true; } } } if (ok) l = mid; else r = mid; } cout << l << '\n'; }
#include <iostream> #include <string> int main() { int N, M; long long odd = 0, even = 0; std::cin >> N >> M; for (int i = 0; i < N; i++) { std::string S; std::cin >> S; bool f = false; for (char c : S) f ^= (c == '1'); (f ? odd : even)++; } std::cout << 1LL * odd * even << "\n"; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; constexpr int MOD = 1000000007; signed main() { int n, m; cin >> n >> m; string s[n]; rep(i, n) { cin >> s[i]; } ll ans = 0; int even = 0, odd = 0; rep(i, n) { int cnt = 0; rep(j, m) { if (s[i][j] == '1') cnt++; } if (cnt % 2 == 0) { ans += odd; even++; } else { ans += even; odd++; } } cout << ans << endl; return 0; }
#include<cstdio> #include<malloc.h> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<set> #include<stack> #include<cassert> using namespace std; typedef long long ll; const int MAXN = 200005; const int INF = 0x7fffffff; 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 << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); double a, b; cin >> a >> b; printf("%.8lf", 100-(b / a)*100); return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn=1e4+10; const int mod=998244353; #define ll long long #define ull unsigned long long #define pi pair<int,int> #define fi first #define sc second #define pb push_back int main() { ios::sync_with_stdio(0); cin.tie(0); double a,b; cin>>a>>b; double ans=(a-b)*100.0/a; cout<<fixed<<setprecision(6)<<ans<<'\n'; }
#include<bits/stdc++.h> #define ll long long int #define pll pair<ll,ll> #define vpll vector< pll > #define mpll map<ll,ll> #define MOD 1000000007 #define all(v) v.begin(),v.end() #define s(v) v.size() #define test ll t;cin>>t;while(t--) #define vec vector<ll> #define read0(v,n) for(int i=0;i<n;i++)cin>>v[i]; #define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i]; #define trav(a,x) for (auto& a: x) #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define cut(x) {cout<<x;return 0;} #define print(x) {cout<<x<<nl;continue;} #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORB(i,a,b) for(int i=a;i>=b;i--) #define err1(a) {cout<<#a<<' '<<a<<nl;} #define err2(a,b) {cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<nl;} #define mp make_pair #define pb push_back #define eb emplace_back #define f first #define sc second #define lb lower_bound #define ub upper_bound #define nl '\n' #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> ll gcd(ll a, ll b) { if (b==0)return a; return gcd(b, a % b); } ll lcm(ll a,ll b) { return (a*b)/gcd(a,b); } ll pow(ll a, ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%MOD; b/=2; a=(a*a)%MOD; } return ans; } struct node{ ll x,t,k; }; const int N=1e5+5; vec d(N,(ll)1e18); vec v[N]; vector<node> g[N]; ll dis(ll x,ll y) { ll p=x/y; if(x%y!=0)p++; p*=y; return p; } void solve(ll n,ll u) { multiset<pll> q; d[u]=0; q.insert({0,u}); while(!q.empty()) { pll p=*q.begin(); q.erase(q.begin()); trav(it,g[p.sc]) { ll c=dis(p.f,it.k); if(d[it.x]>c+it.t) { d[it.x]=c+it.t; q.insert({d[it.x],it.x}); } } } } int main() { fast int n,m,x,y; cin>>n>>m>>x>>y; FOR(i,1,m) { ll a,b,c,d; cin>>a>>b>>c>>d; node temp; temp.x=b;temp.t=c;temp.k=d; g[a].pb(temp); node kemp; kemp.x=a;kemp.t=c;kemp.k=d; g[b].pb(kemp); } solve(n,x); if(d[y]==(ll)1e18)cut(-1) //FOR(i,1,n)cout<<d[i]<<' '; cut(d[y]) }
#include <bits/stdc++.h> using namespace std; typedef long long ll ; #define rep(i,a,n) for(int i=a ; i<n ; i++) #define pb push_back vector<vector<int> > adj ; vector<ll> val,m ; vector<bool> vis ; ll ans ; ll dfs(int x) { if(vis[x]) return m[x] ; ll ma=-1e9 ; vis[x]=1 ; rep(i,0,adj[x].size()) { ma=max(ma,dfs(adj[x][i])) ; } ans=max(ans,ma-val[x]) ; ma=max(ma,val[x]) ; m[x]=ma ; return m[x] ; } int main() { int T=1; // cin >> T ; while(T--) { int n,me ; cin>>n>>me ; adj.resize(0) ; adj.resize(n) ; val.resize(n) ; rep(i,0,n) cin>>val[i] ; m.resize(n) ; rep(i,0,n) m[i]=val[i] ; int x,y ; rep(i,0,me) { cin>>x>>y ; x-- ; y-- ; adj[x].pb(y) ; } vis.resize(0) ; vis.resize(n) ; rep(i,0,n) vis[i]=0 ; ans=-1e9 ; rep(i,0,n) { if(!vis[i]) { dfs(i) ; } } cout<<ans<<endl ; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; #define eps 1e-9 #define INF 2000000000 // 2e9 #define LLINF 2000000000000000000ll // 2e18 (llmax:9e18) #define all(x) (x).begin(), (x).end() #define sq(x) ((x) * (x)) #ifndef LOCAL #define dmp(...) ; #else #define dmp(...) \ cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl #endif // ---------------- Utility ------------------ template <class T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <class T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; template <class T> vector<T> vect(int len, T elem) { return vector<T>(len, elem); } // ----------------- Input ------------------- template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (int i = 0; i < vec.size(); i++) is >> vec[i]; return is; } // ----------------- Output ------------------ template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ',' << p.second; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &e : v) os << e << " "; return os; } template <class T> ostream &operator<<(ostream &os, const deque<T> &d) { for (const T &e : d) os << e << " "; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &s) { os << "{ "; for (const T &e : s) os << e << " "; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const map<T, U> &m) { os << "{ "; for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) "; return os << "}"; } void dump_str_rec(ostringstream &) {} template <class Head, class... Tail> void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) { oss << ", " << head; dump_str_rec(oss, forward<Tail>(tail)...); } template <class T, class... U> string dump_str(T &&arg, U &&... args) { ostringstream oss; oss << arg; dump_str_rec(oss, forward<U>(args)...); return oss.str(); } // --------------- Fast I/O ------------------ void fastio() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); } // ------------ End of template -------------- #define endl "\n" void solve() { int N; cin >> N; vector<int> fib(N + 1, 0); vector<int> pow(N + 1, 0); fib[0] = 1, fib[1] = 2; pow[0] = 1, pow[1] = 2; for (int i = 2; i <= N; i++) { fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000007; pow[i] = (pow[i - 1] * 2) % 1000000007; } char AA, AB, BA, BB; cin >> AA >> AB >> BA >> BB; if (N <= 3) { cout << 1 << endl; return; } if (AB == 'A') { if (AA == 'A') { cout << 1 << endl; return; } if (BA == 'B') { cout << pow[N - 3] << endl; return; } cout << fib[N - 3] << endl; } if (AB == 'B') { if (BB == 'B') { cout << 1 << endl; return; } if (BA == 'A') { cout << pow[N - 3] << endl; return; } cout << fib[N - 3] << endl; } return; } int main() { fastio(); solve(); // int t; cin >> t; while(t--)solve(); // int t; cin >> t; // for(int i=1;i<=t;i++){ // cout << "Case #" << i << ": "; // solve(); // } return 0; }
using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #define N 1005 #define ll long long #define mo 1000000007 ll qpow(ll x,ll y=mo-2){ ll r=1; for (;y;y>>=1,x=x*x%mo) if (y&1) r=r*x%mo; return r; } ll fac[N],ifac[N]; void initC(int n){ fac[0]=1; for (int i=1;i<=n;++i) fac[i]=fac[i-1]*i%mo; ifac[n]=qpow(fac[n]); for (int i=n-1;i>=0;--i) ifac[i]=ifac[i+1]*(i+1)%mo; } ll C(int m,int n){ if (m<n) return 0; return fac[m]*ifac[n]%mo*ifac[m-n]%mo; } int n; int c[2][2]; void case1(){ printf("1\n"); } void case2(){ ll ans=0; for (int i=1;i<=n-1;++i) ans+=C(i-1,n-1-i); printf("%lld\n",ans%mo); } void case3(){ printf("%lld\n",qpow(2,n-3)); } int main(){ scanf("%d",&n); initC(n); for (int i=0;i<2;++i) for (int j=0;j<2;++j){ char str[2]; scanf("%s",str); c[i][j]=*str-'A'; } if (n==2){ printf("1\n"); return 0; } if (c[0][1]==0){ if (c[0][0]==0) case1(); else{ if (c[1][0]==0) case2(); else case3(); } } else{ if (c[1][1]==1) case1(); else{ if (c[1][0]==1) case2(); else case3(); } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using que_type = tuple<int, pair<int, int>, vector<int>>; void solve(long long si, long long sj, std::vector<std::vector<long long>> t, std::vector<std::vector<long long>> p){ priority_queue<que_type, vector<que_type>, greater<que_type>> que; que.push(make_tuple(p[si][sj], make_pair(si, sj), vector<int>{(int)t[si][sj]})); int times = 1e9; int beam = 1000; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int ans = 0; vector<int> ansv; while(times--){ priority_queue<que_type, vector<que_type>, greater<que_type>> next_que; while(!que.empty()){ que_type q = que.top(); int score; pair<int, int> pos; vector<int> tiles; tie(score, pos, tiles) = q; que.pop(); for(int i = 0;i < 4;i++){ int nx = dx[i] + pos.first; int ny = dy[i] + pos.second; if(0 <= dx[i] + pos.first && dx[i] + pos.first < 50 && 0 <= dy[i] + pos.second && dy[i] + pos.second < 50){ bool f = false; for(int x : tiles){ if(x == t[nx][ny]){ f = true; break; } } if(f == false){ tiles.push_back(t[nx][ny]); next_que.push(make_tuple(score + p[nx][ny], make_pair(nx, ny), tiles)); tiles.pop_back(); if(score + p[nx][ny] > ans){ ans = score + p[nx][ny]; ansv = tiles; } } } } } while(next_que.size() > beam) next_que.pop(); que = next_que; //cout<<que.size()<<endl; //cout<<ans<<endl; if(que.size() == 0){ break; } } int pi = si; int pj = sj; string ans_str; for(int i = 1;i < ansv.size();i++){ bool f = false; //cout<<ansv[i]<<" "; for(int idx = 0;idx < 4;idx++){ int nx = pi + dx[idx]; int ny = pj + dy[idx]; if(0 <= nx && nx < 50 && 0 <= ny && ny < 50){ if(t[nx][ny] == ansv[i]){ //cout<<idx<<" "; if(idx == 0){ ans_str.push_back('D'); }else if(idx == 1){ ans_str.push_back('R'); }else if(idx == 2){ ans_str.push_back('U'); }else if(idx == 3){ ans_str.push_back('L'); } f = true; } } if(f){ pi = nx; pj = ny; break; } } } cout<<ans_str<<endl; } int main(){ long long si; scanf("%lld",&si); long long sj; scanf("%lld",&sj); std::vector<std::vector<long long>> t(49+1, std::vector<long long>(49+1)); for(int i = 0 ; i < 49+1 ; i++){ for(int j = 0 ; j < 49+1 ; j++){ scanf("%lld",&t[i][j]); } } std::vector<std::vector<long long>> p(49+1, std::vector<long long>(49+1)); for(int i = 0 ; i < 49+1 ; i++){ for(int j = 0 ; j < 49+1 ; j++){ scanf("%lld",&p[i][j]); } } solve(si, sj, std::move(t), std::move(p)); return 0; }
#include <bits/stdc++.h> using namespace std; //ofstream cout("output.out"); #define fast() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);} #define pb push_back #define vi vector<int> #define vl vector<ll> #define vii vector<vector<int>> #define mii map<int, int> #define pii pair<int, int> #define qii priority_que<int, vector<int>, greater<int>> #define For(i, n) for(int i = 0; i < n; ++i) #define For1(i, n) for(int i = 1; i <= n; ++i) #define Forlr(i, l, r) for(int i = l; i <= r; ++i) #define Forrl(i, r, l) for(int i = r; i >= l; --i) #define o(a) { cout << #a << ":" << (a) <<" ";} #define ov(a) {For(i, a.size()) cout << a[i] <<" ";} #define ovv(a) {For(i, a.size()) {For(j, a[i].size()) cout << a[i][j] <<" ";cout <<"\n";}} using ll = long long; int n, m, k, q; const int INF = 1e9 + 5; const int nax = 1e5 + 5; string s[2005]; ll dp[2005][2005]; const ll mod = 1e9 + 7; ll x[2000][2000], y[2000][2000], z[2000][2000]; int main() { cin >> n >> m; For(i, n){ cin >> s[i]; } dp[0][0] = 1; For(i, n){ For(j, m){ //o(s[i][j]) if(i == 0 && j == 0) continue; if(s[i][j] =='#') continue; if(i > 0) { x[i][j] = (x[i - 1][j] + dp[i - 1][j]) % mod; } if(j > 0){ y[i][j] = (y[i][j - 1] + dp[i][j - 1]) % mod; } if(i > 0 && j > 0){ z[i][j] = (z[i - 1][j - 1] + dp[i - 1][j - 1]) % mod; } dp[i][j] = (x[i][j] + y[i][j] + z[i][j]) % mod; } } // For(i, n) { // For(j, m){ // cout << dp[i][j] << " " ; // } // cout <<"\n"; // } cout << dp[n - 1][m - 1]; //cout << dp[n - 1][m - 1]; }
#include<bits/stdc++.h> #define ll long long using namespace std; void readFile(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main(){ readFile(); ios::sync_with_stdio(false); cin.tie(NULL); float v, t, s, d; cin >> v >> t >> s >> d; float time = d / v; // cout << time << '\n'; if(!(time >= t && time <= s)) { cout << "Yes\n"; } else{ cout << "No\n"; } } // speed = distance / time
#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); ll x, y, a, b; cin >> x >> y >> a >> b; ll tmp = 0; ll ans = 0; while(x < y) { chmax(ans, (y-x-1)/b+tmp); if(y/x < a) break; x *= a; tmp++; } cout << ans << endk; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); using ll = long long; ll n, t, ans = 0; cin >> n >> t; vector<ll> a(n); for (auto&& i : a) cin >> i; set<ll> s1, s2; s1.insert(0), s2.insert(0); for (int i = 0; i < n / 2; ++i) for (const auto& sum : s1) if (sum - a.at(i) >= -t) s1.insert(sum - a.at(i)); for (int i = n / 2; i < n; ++i) for (const auto& sum : s2) if (sum - a.at(i) >= -t) s2.insert(sum - a.at(i)); for (const auto& sum : s1) ans = min(ans, sum + *s2.lower_bound(-t - sum)); cout << -ans << '\n'; return 0; }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cmath> #include<map> #include<random> #include<iomanip> #include<queue> #include<stack> #include<assert.h> #include<time.h> #define int long long #define double long double #define rep(i,n) for(int i=0;i<n;i++) #define REP(i,n) for(int i=1;i<=n;i++) #define ggr getchar();getchar();return 0; #define prique priority_queue constexpr auto mod = 1000000007; #define inf 1e15 #define key 1e9 using namespace std; typedef pair<int, int>P; template<class T> inline void chmax(T& a, T b) { a = std::max(a, b); } template<class T> inline void chmin(T& a, T b) { a = std::min(a, b); } //combination(NใŒๅฐใ•ใ„ๆ™‚ใฏใ“ใ‚Œใ‚’ไฝฟใ† const int MAX = 2330000; int fac[MAX], finv[MAX], inv[MAX]; // ใƒ†ใƒผใƒ–ใƒซใ‚’ไฝœใ‚‹ๅ‰ๅ‡ฆ็† void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } int COMB(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } bool prime(int n) { int cnt = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0)cnt++; } if (cnt != 1)return false; else return n != 1; } int gcd(int x, int y) { if (y == 0)return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } //็นฐใ‚Š่ฟ”ใ—ไบŒไน—ๆณ•๏ผˆNใŒๅคงใใ„ๆ™‚ใฎๅ ดๅˆใฎcombination) int mod_pow(int x, int y, int m) { int res = 1; while (y) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= (i % mod); res %= mod; } return res; } int comb(int x, int y) { if (y > x)return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } //UnionFind class UnionFind { protected: int* par, * rank, * size; public: UnionFind(unsigned int size) { par = new int[size]; rank = new int[size]; this->size = new int[size]; rep(i, size) { par[i] = i; rank[i] = 0; this->size[i] = 1; } } int find(int n) { if (par[n] == n)return n; return par[n] = find(par[n]); } void unite(int n, int m) { n = find(n); m = find(m); if (n == m)return; if (rank[n] < rank[m]) { par[n] = m; size[m] += size[n]; } else { par[m] = n; size[n] += size[m]; if (rank[n] == rank[m])rank[n]++; } } bool same(int n, int m) { return find(n) == find(m); } int getsize(int n) { return size[find(n)]; } }; int dight(int n) { int ans = 1; while (n >= 10) { n /= 10; ans++; } return ans; } 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 n, t; int a[44]; int memo1[1050000], memo2[1050000]; signed main() { cin >> n >> t; rep(i, n)cin >> a[i]; rep(i, 1 << (n / 2)) { int cnt = 0; rep(j, n / 2) { if ((i >> j) & 1)cnt += a[j]; } memo1[i] = cnt; } rep(i, 1 << (n - n / 2)) { int cnt = 0; rep(j, n - n / 2) { if ((i >> j) & 1)cnt += a[j + n / 2]; } memo2[i] = cnt; } sort(memo1, memo1 + (1 << (n / 2))); sort(memo2, memo2 + (1 << (n - n / 2))); int ans = 0; rep(i, 1 << (n / 2)) { int p = upper_bound(memo2, memo2 + (1 << (n - n / 2)), t - memo1[i]) - memo2; if (p == 0)ans = ans; else ans = max(ans, memo1[i] + memo2[p - 1]); } cout << ans << endl; ggr }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { double n; cin >> n; double ans = 0; for (int i = 1; i <= n - 1; i++) { ans += (n / (n - i)); } cout << std::fixed << std::setprecision(15) << ans << endl; }
using namespace std; #include<string> #include<cmath> #include<list> #include <map> #include <unordered_map> #include <set> #define ll long long #define ld long double #define ull unsigned long long #define ml map<ll,ll> #define pb push_back #define mp make_pair #define ppl pair<ll,ll> #define F first #define S second #define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define fr(i,a,b) for(ll i=a;i<b;i++) #define ffr(i,a,b) for(ll i=a;i>=b;i--) #include<bits/stdc++.h> #include<vector> #define vl vector<ll> #define PI 3.141592654 const ll N = 1e9 + 7; #include<iterator> #define EPSILON numeric_limits<double>::epsilon() // vector<vl> v; // vector<bool> visited; // void dfs(ll pnt, ll &edge, ll &pt) { // visited[pnt] = 1; // pt++; // fr(i, 0, v[pnt].size()) { // edge++; // if (!visited[pnt])dfs(v[pnt][i], edge, pt); // } // } int gcd(int a, int b) { if (a == b)return a; if (a % b == 0)return b; if (b % a == 0)return a; if (b > a)return gcd(a, b % a); if (a > b)return gcd(a % b, b); return 0; } void solve() { ll n; cin >> n; ll b = log2(n) / log2(5); b++; fr(i, 1, b) { ll f = 1, q = 1; fr(j, 0, i)f *= 5; if (f < n) { f = n - f; ll g = log2(f) / log2(3); fr(l, 0, g)q *= 3; if (q == f && g > 0) {cout << g << " " << i; return;} } } cout << -1; } int main() { fio; #ifndef ONLINE_JUDGE // for getting input freopen("input.txt", "r", stdin);//298023223876953125 //for writing output freopen("jout.txt", "w", stdout); #endif //prime(); ll t; if (1) t = 1; else cin >> t; while (t--) { //cout << "Case #" << h << ": "; solve(); //h++; cout << '\n'; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #ifdef LOCAL void debug_out() { cerr << endl; } template <class Head, class... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); } #define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif template<class T> using V = vector<T>; template<class T> using P = pair<T, T>; using ll = long long; using ld = long double; using Vi = V<int>; using VVi = V<Vi>; using Vl = V<ll>; using VVl = V<Vl>; using Vd = V<ld>; using VVd = V<Vd>; using Vb = V<bool>; using VVb = V<Vb>; using Pi = P<int>; using Pl = P<ll>; using Pd = P<ld>; template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; template<class T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); } template<class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); } template<class T> inline int sz(const T &x) { return size(x); } template<class T> inline bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; } template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) { for (auto &e : v) is >> e; return is; } template<class T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &v) { for (auto &e : v) os << e << ' '; return os; } template<class T> inline void deduplicate(vector<T> &a) { sort(all(a)); a.erase(unique(all(a)), a.end()); } template<class T> inline int count_between(const vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r) inline ll cDiv(const ll x, const ll y) { return (x+y-1) / y; } // ceil(x/y) inline int fLog2(const ll x) { assert(x > 0); return 63-__builtin_clzll(x); } // floor(log2(x)) inline int cLog2(const ll x) { assert(x > 0); return (x == 1) ? 0 : 64-__builtin_clzll(x-1); } // ceil(log2(x)) inline int popcount(const ll x) { return __builtin_popcountll(x); } inline void fail() { cout << -1 << '\n'; exit(0); } struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; // const int INF = (1<<30) - 1; // const ll INFll = (1ll<<60) - 1; // const ld EPS = 1e-10; // const ld PI = acos(-1.0); bool solve(){ int n; cin >> n; Vi a(n); cin >> a; if (n % 2 == 1) return false; map<int, int> cnt; rep(i, n) cnt[a[i]] ^= 1; for (auto [x, y] : cnt) if (y) return true; return false; } int main() { int t; cin >> t; rep(i, t) cout << (solve() ? "First" : "Second") << '\n'; }
#include <bits/stdc++.h> #include <fstream> using namespace std; typedef long long int64; typedef vector<int> vec; typedef vector<int64> vec64; // string __fname = ""; // ifstream in (__fname + ".in"); // ofstream out (__fname + ".out"); // #define cin in // #define cout out #define pii pair <int, int> #define pii64 pair <int64, int64> #define ss cout << " "; #define nn cout << "\n"; #define ct(x) cout << x; #define cts(x) cout << x << " "; #define ctn(x) cout << x << "\n"; #define db(x) cout << "> " << #x << ": " << x << "\n"; #define qr queries(); void solve(int); void queries(){int n;cin >> n;for (int i = 1; i <= n; i++) solve(i);} int64 ceildiv(int64 a, int64 b) {return a / b + !!(a % b);} template<class T>T gcd (T a, T b){return (b ? gcd(b, a % b): a);} template<class T>T lcm (T a, T b){return a * b / gcd(a, b);} // // // // // // // // // // // // // // // // // // // // // // /* TEMPLATE - VANILLA */ // // // // // // // // // // // // // // // // // // // // // // const int maxn = 200200; const int64 mod = 1000000007; const double pi = 3.14159265359; const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; void solve(int id){ vec a (3); for (int i = 0; i < 3; i++){ cin >> a[i]; } sort(a.begin(), a.end()); ctn(a[2] + a[1]); return; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); solve(0); return 0; }
#include <vector> #include <iostream> #define int long long #define double long double #define tezz ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std; signed main() { tezz vector<int> primes; for (int i = 2; i < 72; i++) { bool flag = true; for (int j = 2; j < i; j++) { if (i % j == 0) flag = false; } if (flag) primes.emplace_back(i); } int A, B; cin >> A >> B; vector<int> v(B - A + 1); for (int i = A; i <= B; i++) { int mask = 0; for (int j = 0; j < primes.size(); j++) { if (i % primes[j] == 0) { mask |= (1 << j); } } v[i - A] = mask; } vector<int> dp(1<<primes.size()); dp[0] = 1; for (int x: v) { for (int mask = 0; mask < (1 << primes.size()); mask++) { if (dp[mask]) { int tmp = mask & x; if (tmp == 0) dp[mask | x] += dp[mask]; } } } int ans = 0; for (int i = 0; i < (1 << primes.size()); i++) ans += dp[i]; cout << ans; }
#include<cstdio> #define N 300001 using namespace std; long long m2[N],m3[N],m5[N],m7[N],go[N],used[N]; long long s[101][101]; long long n,m,ans=0; long long gcd(long long x,long long y) { if (x==1||y==1) return 1; return s[x-n+1][y-n+1]; } long long gcd1(long long x,long long y) { if (y==0) return x; return gcd1(y,x%y); } void dfs(int k) { if (k>used[0]) { ans++; return; }//ans++; dfs(k+1); bool bo=true; for (int i=1;i<=go[0];i++) if (gcd(go[i],used[k])!=1) { bo=false;break; } if (bo) { go[++go[0]]=used[k]; dfs(k+1); go[0]--; } } int main() { scanf("%lld%lld\n",&n,&m); m2[0]=1;m3[0]=1;m5[0]=1;m7[0]=1; m2[1]=1;m3[1]=1;m5[1]=1;m7[1]=1; for (long long i=n;i<=m;i++) for (long long j=n;j<=m;j++) s[i-n+1][j-n+1]=gcd1(i,j); long long pans=1; for (long long i=n;i<=m;i++) { if (i%2==0) m2[++m2[0]]=i; else if (i%3==0) m3[++m3[0]]=i; else if (i%5==0) m5[++m5[0]]=i; else if (i%7==0) m7[++m7[0]]=i; else { bool bo=true; for (long long j=n;j<=m;j++) { if (i==j) continue; if (s[i-n+1][j-n+1]!=1) { bo=false;break; } } if (!bo||(i*2<=m)) used[++used[0]]=i; else pans*=2; } } ans=0; for (int i=1;i<=m2[0];i++) { for (int j=1;j<=m3[0];j++) { if (gcd(m2[i],m3[j])!=1) continue; for (int k=1;k<=m5[0];k++) { if (gcd(m2[i],m5[k])!=1) continue; if (gcd(m3[j],m5[k])!=1) continue; for (int p=1;p<=m7[0];p++) { if (gcd(m2[i],m7[p])!=1) continue; if (gcd(m3[j],m7[p])!=1) continue; if (gcd(m5[k],m7[p])!=1) continue; go[0]=4; go[1]=m2[i];go[2]=m3[j];go[3]=m5[k];go[4]=m7[p]; dfs(1); // printf("%d %d %d %d\n",m2[i],m3[j],m5[k],m7[p]); // printf("%lld\n",ans); } } } } printf("%lld\n",ans*pans); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a.at(i); vector<ll> counter(200, 0); rep(i, n) { counter.at(a.at(i) % 200)++; } ll ans = 0; rep(i, counter.size()) { ll cnt = counter.at(i); ans += cnt * (cnt - 1) / 2; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); ++i) #define fixed_setprecision(n) fixed << setprecision((n)) #define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC); #define pai 3.1415926535897932384 #define NUM_MAX 2e18 #define NUM_MIN -1e9 using namespace std; using ll = long long; using P = pair<int,int>; template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; } void solve(ll N, map<int, int> m){ ll res=0; for(int i=0; i<200; i++){ for(int j=i; j<200; j++){ if(m[i]==0 || m[j]==0) continue; if((j-i)%200==0){ if(i==j){ rep(k, m[i]){ res += (m[i]-k-1); } } } } } cout << res << endl; } int main() { ll N; cin >> N; //N = 200000; //vector<ll> A(N); map<int, int> m; rep(i, N){ ll t; cin >> t; m[t%200]++; } //rep(i, N) cin >> A[i]; //rep(i, N) A[i]=400; solve(N, m); return 0; }
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mod 1000000007 #define F first #define S second #define all(v) (v).begin(),(v).end() #define np next_permutation #define lp(i,n) for(int i=0;i<n;i++) #define lps(i,j,n) for(int i=j;i<n;i++) #define vii vector<vi> #define vb vector<bool> #define pr pair<int,int> #define vl vector<ll> #define vs vector<string> #define us unordered_map<int,int> #define Mpq priority_queue<int> #define mpq priority_queue<int,vi,greater<int>> #define eb emplace_back #define pr pair<int,int> #define prl pair<ll,ll> #define vp vector<pr> #define vpl vector<prl> #define mkp make_pair #define ld long double #define vii vector<vi> #define Max(a,b) a=max(a,b) #define Min(a,b) a=min(a,b) #define ull unsigned long long #define prr pair<ll,int> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; using namespace std; using vi=vector<int>; // typedef tree<prl, null_type,less<prl>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // ordered_set s; #define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s)) #define F_OR1(e) F_OR(i, 0, e, 1) #define F_OR2(i, e) F_OR(i, 0, e, 1) #define F_OR3(i, b, e) F_OR(i, b, e, 1) #define F_OR4(i, b, e, s) F_OR(i, b, e, s) #define GET5(a, b, c, d, e, ...) e #define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1) #define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__) #define EACH(x, a) for (auto& x: a) template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>; double epsilon=(double)1e-9; const int N=(int)1e6+12; int t; const ll MX=(ll)1e18+123; const int inf=0x3fffffff; vi g[21]; vi par,color; vb vis; void dfs(int u){ vis[u]=1; par.eb(u); for(auto &v:g[u]){ if(!vis[v]) dfs(v); } } ll cal(int i){ if(i==(int)par.size()){ return 1; } int u=par[i]; ll ans=0; lp(c,3){ color[u]=c; bool valid=1; for(int v:g[u]){ if(color[u]==color[v]){ valid=0; break; } } if(valid) ans+=cal(i+1); color[u]=-1; } return ans; } void solve(){ int n,m,u,v; ll ans=1; cin>>n>>m; vis.resize(n); color.resize(n,-1); lp(i,m){ cin>>u>>v; u--,v--; g[u].eb(v); g[v].eb(u); } lp(i,n){ if(vis[i]) continue; par.resize(0); dfs(i); color[i]=0; ans*=3*cal(1); // color[i]=-1; } cout<<ans<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); solve(); return 0; }
#include <bits/stdc++.h> #define ll long long #define ls (v << 1) #define rs ((v << 1)|1) #define ll long long #define free fajeiof #define right fjewiofjaewoi #define move afjoiejwioaf int gcd(int a, int b){ if (a == 0) return b; return gcd(b % a, a); } using namespace std; const int nmax = 300001; const int inf = 1e9 + 7; const int mod = 998244353; int tt, timer; int n, m, color[nmax], cur, x, y; bool used[nmax]; vector<int> g[nmax], comp; void dfs1(int v){ comp.push_back(v); used[v] = true; for (int to: g[v]){ if (!used[to]) dfs1(to); } } bool check(int v, int col){ for (int to : g[v]){ if (color[to] == col) return false; } return true; } void dfs2(int idx){ //cout << idx << " vert: " << endl; if (idx == comp.size()){ cur++; return; } int v = comp[idx]; for (int c = 1; c <= 3; c++){ if (!check(v, c)) continue; color[v] = c; dfs2(idx + 1); } color[v] = -1; } void solve(){ cin >> n >> m; while (m--){ cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } ll ans = 1; for (int i = 1; i <= n; i++){ if (!used[i]){ comp.clear(); dfs1(i); cur = 0; dfs2(0); ans *= cur; } } cout << ans << endl; } int main() { srand(time(0)); std::ios::sync_with_stdio(false); //cin >> tt; tt = 1; while (tt--){ solve(); } return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define ld long double #define pii pair <int, int> #define pll pair <ll, ll> #define pci pair <char, int> #define pld pair <ld, ld> #define ppld pair <pld, pld> #define ppll pair <pll, pll> #define pldl pair <ld, ll> #define vll vector <ll> #define vvll vector <vll> #define vpll vector <pll> #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define fastmap gp_hash_table #define cd complex <double> #define vcd vector <cd> #define PI 3.14159265358979 using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll x, y; cin >> x >> y; if (x == y) cout << x << "\n"; else cout << 3 - x - y << "\n"; }
#include <iostream> using namespace std; int a, b, ans; int main() { cin >> a >> b; for (int i = 1; i <= 2e5; i++) { if (b / i - (a - 1) / i > 1) { ans = i; } } cout << ans; return 0; }
#include<bits/stdc++.h> #define f first #define s second #define pb push_back using namespace std; typedef pair<int,int> pii; typedef long long ll; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fll; ll n; ll m; vector<ll> a; vector<ll> b; ll memo [2000][2000]; ll lcs (ll x, ll y) { if(x==n){return m-y;} if(y==m){return n-x;} if(memo[x][y]!=-1){return memo[x][y];} if(a[x]==b[y]){return memo[x][y]=lcs(x+1,y+1);} return memo[x][y]=min(1+lcs(x+1,y+1),min(1+lcs(x+1,y),1+lcs(x,y+1))); } int main () { ios::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=0;i<n;i++) { ll c; cin>>c; a.pb(c); } for(int i=0;i<m;i++) { ll c; cin>>c; b.pb(c); } memset(memo,-1,sizeof(memo)); cout<<lcs(0,0)<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define V vector #define debug(v) for(auto i: v) cout << i << " "; cout << endl; #define vcin(v) rep(i, v.size()) cin >> v[i] #define wcin(v, w) rep(i, v.size()) cin >>v[i] >>w[i] #define r0 return 0; int main(){ ll n,m; cin >>n >>m; V<ll> a(n),b(m); vcin(a); vcin(b); unordered_set<ll> num; for(ll i:a)num.insert(i); for(ll i:b)num.insert(i); unordered_set<ll> numa=num; unordered_set<ll> numb=num; for(ll i:a)numa.erase(i); for(ll i:b)numb.erase(i); V<ll> ans; for(ll i:numa)ans.push_back(i); for(ll i:numb)ans.push_back(i); sort(all(ans)); for(ll i:ans)cout <<i <<" "; }
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<set> #include<map> #include<math.h> #include<unordered_set> #include<unordered_map> #include<cassert> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; #define mp make_pair #define pb push_back #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define rrep(i,l,r) for(int i=l;i<=r;i++) #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define all(x) x.begin(),x.end() #define unq(x) sort(all(x));x.erase(unique(all(x)),x.end()) #define mod 1000000007 //#define mod 998244353 #define ad(a,b) a=(a+b)%mod; #define mul(a,b) a=a*b%mod; void readv(vector<ll> &a,ll n){ rep(i,n){ ll x; cin>>x; a.push_back(x); } } void outv(vector<ll> &a,ll n){ rep(i,n){ if(i>0)cout<<" "; cout<<a[i]; } cout<<"\n"; } ll po(ll x,ll y){ ll res=1; for(;y;y>>=1){ if(y&1)res=res*x%mod; x=x*x%mod; } return res; } ll gcd(ll a,ll b){ return (b?gcd(b,a%b):a); } #define FACMAX 200010 ll fac[FACMAX],inv[FACMAX],ivf[FACMAX]; void initfac(){ fac[0]=ivf[0]=inv[1]=1; for(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod; for(ll i=1;i<FACMAX;i++){ if(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod; ivf[i]=po(fac[i],mod-2); } } ll P(ll n,ll k){ if(n<0||n<k)return 0; return fac[n]*ivf[n-k]%mod; } ll C(ll n,ll k){ if(k<0||n<k)return 0; return fac[n]*ivf[n-k]%mod*ivf[k]%mod; } ll H(ll n,ll k){ return C(n+k-1,k); } #define D 100010 bool e[D]; int main(){ cin.tie(0); ios::sync_with_stdio(0); ll n; cin>>n; ll ans=n; rep(i,D)e[i]=0; for(ll i=2;i<D;i++){ if(e[i])continue; for(ll j=i*i;j<=n;j*=i){ if(j<D)e[j]=1; ans--; } } cout<<ans<<endl; }
#include<bits/stdc++.h> #define forn(i, n) for(int i=0;i<(int)(n);i++) #define for1(i, n) for(int i=1;i<=(int)(n);i++) #define fore(i, l, r) for(int i=(int)(l);i<=(int)(r);i++) #define ford(i, n) for(int i=(int)(n)-1;i>=0;i--) #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define endl "\n" #define MAX 1000000007 using namespace std; typedef int64_t ll; typedef vector<int> vi; typedef vector<int64_t> vll; typedef vector<vi> vvi; typedef vector<vll> vvll; int main(int argc, char *argv[]){ ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; int n; cin>>n; if(n%2==0) cout<<"White"; else cout<<"Black"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int h,w; cin >> h >> w; vector<int> a(h*w); for(int i=0;i<h*w;i++){ cin >> a.at(i); } int b; b=*min_element(a.begin(),a.end()); int c; c=0; for(int i=0;i<h*w;i++){ c += a.at(i)-b; } cout << c << endl; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdint> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; #define EPS (1e-7) // string->num // stoll(str) // num->string // to_string(i) int main() { ll H, W; cin >> H >> W; vector<vector<ll>> A(H, vector<ll>(W - 1)); vector<vector<ll>> B(H - 1, vector<ll>(W)); for (ll i = 0; i < H; i++) { for (ll j = 0; j < W - 1; j++) { cin >> A[i][j]; } } for (ll i = 0; i < H - 1; i++) { for (ll j = 0; j < W; j++) { cin >> B[i][j]; } } const ll INF = 1e10; priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<>> q; vector<ll> dist(H * W, INF); auto push = [&](ll i, ll j, ll cost) { ll node = i * W + j; if (dist[node] > cost) { dist[node] = cost; q.emplace(cost, node); } }; push(0, 0, 0); while (!q.empty()) { auto [cost, node] = q.top(); q.pop(); if (dist[node] != cost) continue; ll i = node / W; ll j = node % W; if (j + 1 < W) push(i, j + 1, cost + A[i][j]); if (j - 1 >= 0) push(i, j - 1, cost + A[i][j - 1]); if (i + 1 < H) push(i + 1, j, cost + B[i][j]); for (ll k = 0; k < i; k++) { push(k, j, cost + (i - k) + 1); } } cout << dist.back() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define mp make_pair #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define sz(x) int(x.size()) #define pcnt __builtin_popcountll template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;} template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;} template <typename T> void print(T a) {cout << a << ' ';} template <typename T> void printe(T a) {cout << a << endl;} template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;} template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;} template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);} template <typename T> using vc = vector<T>; template <typename T> using vv = vc<vc<T>>; template <typename T> using vvv = vc<vv<T>>; using ll = long long; using P = pair<int, int>; using Pl = pair<ll, ll>; using vi = vc<int>; using vvi = vv<int>; using vl = vc<ll>; using vvl = vv<ll>; vi di = {-1, 1, 0, 0, -1, -1, 1, 1}; vi dj = { 0, 0, -1, 1, -1, 1, -1, 1}; int main () { int n, D, H; cin >> n >> D >> H; double ans = 0; rep(i, n) { int d, h; cin >> d >> h; chmax(ans, (double) (D*h - d*H) / (D - d)); } printf("%.10lf\n", ans); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for(int i = 0; i < n; i++) #define REPR(i, n) for(int i = n; i >= 0; i--) #define INF 2e9 using namespace std; int main(){ int n,D,H,j; cin >> n >> D >> H; vector<int> d(n); vector<int> h(n); for(int i=0;i<n;i++) cin >> d.at(i) >> h.at(i); double min; vector<double> tan(n); for(int i=0;i<n;i++){ tan.at(i) = (double)(H-h.at(i))/(D-d.at(i)); if(i==0) min = tan.at(0); else if(tan.at(i)<min){ min = tan.at(i); j = i; } } double answer; answer = (double)D*tan.at(j); if(tan.at(j)>=(double)H/D) cout << 0.00 << endl; else cout << H-answer << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for(int i=0; i<n; i++) #define REPi(i, a, b) for(int i=int(a); i<int(b); i++) #define MEMS(a,b) memset(a,b,sizeof(a)) #define mp make_pair template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const ll MOD = 1e9+7; long long power(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % MOD; a = a * a % MOD; n >>= 1; } return res; } int chromatic_number(const vector<vector<int> > &G) { int n = (int)G.size(); vector<int> neighbor(n, 0); for (int i = 0; i < n; ++i) { int S = (1<<i); for (int j = 0; j < n; ++j) if (G[i][j]) S |= (1<<j); neighbor[i] = S; } // I[S] := #. of inndepndet subset of S vector<int> I(1<<n); I[0] = 1; for (int S = 1; S < (1<<n); ++S) { int v = __builtin_ctz(S); I[S] = I[S & ~(1<<v)] + I[S & ~neighbor[v]]; } int low = 0, high = n; while (high - low > 1) { int mid = (low + high) >> 1; // g[S] := #. of "k independent sets" which cover S just // f[S] := #. of "k independent sets" which consist of subseta of S // then // f[S] = sum_{T in S} g(T) // g[S] = sum_{T in S} (-1)^(|S|-|T|)f[T] long long g = 0; for (int S = 0; S < (1<<n); ++S) { if ((n - __builtin_popcount(S)) & 1) g -= power(I[S], mid); else g += power(I[S], mid); g = (g % MOD + MOD) % MOD; } if (g != 0) high = mid; else low = mid; } return high; } int main(){ ll N, M; cin >> N >> M; vector<vector<int>> G(N, vector<int>(N, 0)); set<pair<int, int>> AB; REP(i,M){ ll a, b; cin >> a >> b; a--, b--; AB.insert(make_pair(a, b)); AB.insert(make_pair(b, a)); //G[a][b] = 1; //G[b][a] = 1; } REP(i,N){ REP(j,N){ if(AB.find(make_pair(i, j)) == AB.end()){ G[i][j] = 1; G[j][i] = 1; } } } ll ans = 0; //ans = N - chromatic_number(G); ans = chromatic_number(G); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define err(args...) {} #ifdef DEBUG #include "_debug.cpp" #endif using namespace std; using ll = long long; using ld = long double; template <typename T> using lim = numeric_limits<T>; template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; } template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; } constexpr auto ctz(int x) { return __builtin_ctz(x); } constexpr auto ctz(ll x) { return __builtin_ctzll(x); } constexpr auto clz(int x) { return __builtin_clz(x); } constexpr auto clz(ll x) { return __builtin_clzll(x); } constexpr auto pop(int x) { return __builtin_popcount(x); } constexpr auto pop(ll x) { return __builtin_popcountll(x); } template <typename F> void for_each_subset(int mask, F&& f) { static_assert(is_convertible<decltype(f), function<void(int)>>::value, "can must be void(int)"); for(int x = mask; x > 0; x = (x - 1) & mask) { f(x); } } const int N = 18; int adj[N][N], mem[1 << N], is_clique[1 << N]; int opt(int mask) { int& ans = mem[mask]; if(ans == -1) { if(is_clique[mask]) { ans = 1; } else { ans = lim<int>::max(); for_each_subset(mask, [&](int submask) { if(submask != mask) { if(is_clique[submask]) { ans = min(ans, 1 + opt(mask ^ submask)); } } }); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for(int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u - 1][v - 1] = adj[v - 1][u - 1] = 1; } for(int i = 0; i < n; i++) { adj[i][i] = 1; } for(int mask = 0; mask < (1 << n); mask++) { is_clique[mask] = true; for(int ii = mask, i = ctz(ii); ii > 0; ii &= ~(ii & -ii), i = ctz(ii)) { for(int jj = mask, j = ctz(jj); jj > 0; jj &= ~(jj & -jj), j = ctz(jj)) { is_clique[mask] &= adj[i][j]; } } } memset(mem, -1, sizeof mem); cout << opt((1 << n) - 1) << endl; return 0; }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b,ll mod=MOD){ ll res = 1; while(b){ if(b&1)res = res * x % mod; x = x * x % mod; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } ll in(){ ll res; cin >> res; return res; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } } using namespace templates; template< typename T > T extgcd(T a, T b, T &x, T &y) { T d = a; if(b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } ll gcd(ll a,ll b){ if(b)return gcd(b,a%b); return a; } ll func(){ ll n = in(); ll s = in(); ll k = in(); set<ll> used; ll x; ll y; if(s % gcd(n,k))return -1; y %= k / gcd(n,k); y += k / gcd(n,k); y %= k / gcd(n,k); extgcd(k,n,x,y); ll res = s / gcd(n,k) * ((y * n - gcd(n,k)) / k); res %= n / gcd(n,k); res += n / gcd(n,k); res %= n / gcd(n,k); return res; } int main(){ int t = in(); rep(i,t){ cout << func() << endl; } return 0; }
#include <iostream> int64_t gcd(int64_t a,int64_t b,int64_t &x,int64_t &y) { if(b==0){x=1;y=0;return a;} int64_t d=gcd(b,a%b,y,x); y-=a/b*x; return d; } int main() { int T; std::cin>>T; for(int i=0;i<T;i++) { int64_t N,S,K; std::cin>>N>>S>>K; int64_t result=0; int64_t x,y; int64_t d=gcd(K,-N,x,y); if(S%d==0) { x*=-S/d; x%=N/d;if(x<0)x+=N/d; result=x; } else { result=-1; } std::cout<<result<<std::endl; } return 0; }
#include<bits/stdc++.h> #define ll long long #define MAX 1000006 using namespace std; int main() { ll t; ll n,m,x,a,b,c,flag=0; //vector<ll>v(n+5); vector<pair<int,int>>vp; vector<ll>v; vector<ll>v1; map<ll ,vector<ll>>mpp; set<ll>se; string s,s1,s2; ll ans=0,ans1=0,ans2=0,temp=0; double m1; cin>>n; for(ll i=0;i<n;i++) { cin>>a; v.push_back(a); } for(ll i=0;i<n;i++) { ll minn=INT_MAX; for(ll j=i;j<n;j++) { minn= min(v[j],minn); ans=minn*(j-i+1); ans1=max(ans1,ans); } } cout<<ans1<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<int,int> pii; #define fi first #define se second #define endl '\n' #define y1 holyshit #define all(x) x.begin(),x.end() const int inf=0x3f3f3f3f; int N; ll A[100010],B[100010]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>N; for(int i=0;i<N;i++) cin>>A[i]; for(int i=0;i<N;i++) cin>>B[i]; ll ans=0; for(int i=0;i<N;i++) ans+=A[i]*B[i]; if(ans) cout<<"No"; else cout<<"Yes"; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; #define pow(n,m) powl(n,m) #define sqrt(n) sqrtl(n) const ll MAX = 5000000000000000000; const ll MOD = 0; random_device rd; mt19937 mt(rd()); int main(){ ll N,M; cin >> N >> M; vector<string> S(M),ans(N),T; set<string> ss; ll m = MAX; for(ll i = 0;i < M;i++) cin >> S[i]; for(ll i = 0;i < M;i++) ss.insert(S[i]); for(ll i = 0;i < M;i++) m = min(m,(ll)S[i].size()); for(ll t = 0;t < 36000;t++){ string a = S[mt() % M]; for(ll i = 0;i < 10;i++){ string b = S[mt() % M]; if(a.size() + b.size() <= N){ a += b; } } while(a.size() < N) a += (char)('A' + (mt() % 8)); T.emplace_back(a); } vector<ll> aaaa; do{ for(ll j = 0;j < T.size();j++){ set<string> c; for(ll k = 0;k < N;k++){ string p = ""; for(ll l = 0;l < m;l++){ p += T[j][(k + l) % N]; } for(ll l = m;l < m + 5;l++){ p += T[j][(k + l) % N]; if(ss.count(p) != 0) c.insert(p); } } aaaa.emplace_back(c.size()); } } while(false); sort(aaaa.begin(),aaaa.end(),greater<ll>()); for(ll i = 0;i < N;i++){ ll C = 0; string A = "____________________"; for(ll j = 0;j < T.size();j++){ set<string> c; for(ll k = 0;k < N;k++){ string p = ""; for(ll l = 0;l < m;l++){ p += T[j][(k + l) % N]; } for(ll l = m;l < m + 5;l++){ p += T[j][(k + l) % N]; if(ss.count(p) != 0) c.insert(p); } } if(C < c.size()){ C = c.size(); A = T[j]; } if(i == 0 && aaaa[900] > c.size()){ T.erase(T.begin() + j); j--; } } for(ll k = 0;k < N;k++){ string p = ""; for(ll l = 0;l < m;l++){ p += A[(k + l) % N]; } for(ll l = m;l < m + 5;l++){ p += A[(k + l) % N]; if(ss.count(p) != 0) ss.erase(p); } } ans[i] = A; } ll C = 0; vector<string> ansans = ans,ansansans = ans; if(m < 5){ for(ll t = 0;t < 1000;t++){ swap(ans[mt() % N],ans[mt() % N]); for(ll i = 0;i < N;i++){ ll qqq = mt() % N; for(ll j = 0;j < N;j++) ansans[j][i] = ans[i][(j + qqq) % N]; } set<string> c; for(ll i = 0;i < N;i++){ for(ll k = 0;k < N;k++){ string p = ""; for(ll l = 0;l < m;l++){ p += ansans[i][(k + l) % N]; } for(ll l = m;l < m + 5;l++){ p += ansans[i][(k + l) % N]; if(ss.count(p) != 0) c.insert(p); } } } if(C < c.size()){ C = c.size(); ansansans = ansans; } } } //cout<<C<<endl; for(ll i = 0;i < N;i++) cout << ansansans[i] << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 20; mt19937 mt; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); mt.seed(time(nullptr)); int n, m; cin >> n >> m; vector<string> v(m); for (auto& i : v) { cin >> i; } //sort(v.begin(), v.end(), [](string a, string b){return a.size() < b.size();}); shuffle(v.begin(), v.end(), mt); char a[N][N]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j] = '.'; } } int i = 0, j = 0; for (auto s : v) { if (j + s.size() >= n) { j = 0; ++i; } if (i >= n) { break; } for (int k = 0; k < s.size(); ++k, ++j) { a[i][j] = s[k]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << a[i][j]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) #define reps(i,s,n) for(int i=(s); i<(n); i++) #define all(v) v.begin(),v.end() #define outve(v) for(auto i : v) cout << i << " ";cout << endl #define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;} #define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];} #define out(n) cout << (n) << endl #define fi first #define se second #define pb push_back #define si(v) int(v.size()) #define len(v) int(v.length()) #define lob(v,n) lower_bound(all(v),n) #define lobi(v,n) lower_bound(all(v),n) - v.begin() #define upb(v,n) upper_bound(all(v),n) #define upbi(v,n) upper_bound(all(v),n) - v.begin() #define mod 1000000007 #define infi 1010000000 #define infl 1100000000000000000 #define cyes cout << "Yes" << endl #define cno cout << "No" << endl #define csp << " " << #define outset(n) cout << fixed << setprecision(n); using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vector<int>>; using vd = vector<double>; using vvd = vector<vector<double>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using pii = pair<int,int>; using pll = pair<ll,ll>; template<typename T> using ve = vector<T>; template<typename T> using pq2 = priority_queue<T>; template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>; template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;} template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;} int N; ve<pii> G; ve<ve<pair<int,ll>>> Q; vl c; ve<ve<pair<int,ll>>> QQ; ll dfs(int u, int v, ll d){ c[v] += d; ll res = 0; ll rr = 0; ve<pair<ll,int>> aa; for (pair<int,ll> p : Q[v]) { if(p.fi == u){ res += p.se; }else{ ll dd = dfs(v, p.fi, d+p.se); aa.pb({dd, p.fi}); c[v] += dd; res += dd; rr += dd; } } for (auto p : aa) { QQ[v].pb({p.se,rr-p.fi}); } return res; } void dfs1(int u, int v, ll d){ c[v] += d; for (pair<int,ll> p : QQ[v]) { if(p.fi == u){ continue; }else{ dfs1(v, p.fi, d+p.se); } } return; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; rep(i,N-1) { int a,b; cin >> a >> b; a--,b--; G.pb({a,b}); } Q.resize(N); c.resize(N,0LL); QQ.resize(N); ve<pll> aaa(N-1,{0,0}); int T; cin >> T; rep(i,T){ int t,e; ll x; cin >> t >> e >> x; e--; if(t == 1) aaa[e].se += x; else aaa[e].fi += x; } rep(i,N-1){ Q[G[i].fi].pb({G[i].se, aaa[i].fi}); Q[G[i].se].pb({G[i].fi, aaa[i].se}); } dfs(-1, 0, 0); dfs1(-1,0,0); rep(i,N) cout << c[i] << endl; return 0; }
// Created by ash_98 #include<bits/stdc++.h> using namespace std; #define mx 200005 #define ll long long #define mod 1000000007 char ch[mx]; int n,m,ii,k; pair<int,int>ar[mx]; namespace Segment_Tree { const int N=200005; ll Tree[N*4]; ll Lazy[N*4]; void Relax(int node,int be,int en) { if(!Lazy[node])return; Tree[node]+=Lazy[node]; if(be!=en) { Lazy[node*2]+=Lazy[node]; Lazy[node*2+1]+=Lazy[node]; } Lazy[node]=0; } void init(int node,int be,int en) { Lazy[node]=0; if(be==en) { //Tree[node]=ar[be]; return; } int mid=(be+en)/2; init(node*2,be,mid); init(node*2+1,mid+1,en); Tree[node]=Tree[node*2]+Tree[node*2+1]; } void update(int node,int be,int en,int pos,int val) { Relax(node,be,en); if(be> pos || en<pos)return; if(be==en) { Tree[node]+=val; return; } int mid=(be+en)/2; update(node*2,be,mid,pos,val); update(node*2+1,mid+1,en,pos,val); Tree[node]=max(Tree[node*2],Tree[node*2+1]); } void Rupdate(int node,int be,int en,int i,int j,int val) { Relax(node,be,en); if(be>j || en<i)return ; if(be>=i && en<=j) { Lazy[node]+=val; Relax(node,be,en); return; } int mid=(be+en)/2; Rupdate(node*2,be,mid,i,j,val); Rupdate(node*2+1,mid+1,en,i,j,val); Tree[node]=Tree[node*2]+Tree[node*2+1]; } ll query(int node,int be,int en,int i,int j) { Relax(node,be,en); if(be>j || en<i)return 0; if(be>=i && en<=j)return Tree[node]; int mid=(be+en)/2; return query(node*2,be,mid,i,j)+query(node*2+1,mid+1,en,i,j); } void dbg_test(int node,int be,int en) { if(be==en)return; int mid=(be+en)/2; dbg_test(node*2,be,mid); dbg_test(node*2+1,mid+1,en); } } using namespace Segment_Tree; int st[mx],en[mx]; int Final_array[mx]; vector<int>g[mx]; void dfs(int u,int p) { st[u]=++k; for(int v:g[u]) { if(v==p)continue; dfs(v,u); } en[u]=k; } void solve() { scanf("%d",&n); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); ar[i]={x,y}; } dfs(1,-1); scanf("%d",&m); ll ex=0; for(int i=1;i<=m;i++) { int ti,x,y; scanf("%d%d%d",&ti,&x,&y); if(ti==1) { int u=ar[x].first; int v=ar[x].second; if(st[u]>st[v]) { Rupdate(1,1,n,st[u],en[u],y); } else { ex+=y; Rupdate(1,1,n,st[v],en[v],-y); } } else { int u=ar[x].second; int v=ar[x].first; if(st[u]>st[v]) { Rupdate(1,1,n,st[u],en[u],y); } else { ex+=y; Rupdate(1,1,n,st[v],en[v],-y); } } } for(int i=1;i<=n;i++) { ll re=ex+query(1,1,n,st[i],st[i]); printf("%lld\n",re ); } } int main() { int t=1; //scanf("%d",&t); while(t--)solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int a,b; long k; cin >> a >> b >> k; long nCk[a+b+1][a+b+1]; nCk[0][0] = 1; fill(nCk[0]+1,nCk[0]+a+b+1,0); for(int i = 1 ; i < a+b+1 ; i++){ nCk[i][0] = 1; for(int j = 1 ; j < a+b+1 ; j++){ nCk[i][j] = nCk[i-1][j-1] + nCk[i-1][j]; } } string ans = ""; int a2 = a; int b2 = b; for(int i = 0 ; i < a + b ; i++){ long t = nCk[a2-1+b2][b2]; if(k <= t){ ans.push_back('a'); a2--; } else{ k-=t; ans.push_back('b'); b2--; } } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define random(l,r,T) uniform_int_distribution<T>(l,r)(rng) ll mod=1000000007; ll fact[200000]; ll dmod(ll x){ return ((x+mod)%mod); } ll power(ll x,ll y) { ll res=1; while(y) { if(y&1) res=(res*x)%mod; x=(x*x)%mod; y>>=1; } return res; } ll nCr(ll n,ll r){ if(r>n) return 0; else if(r<0) return 0; else if(r==0||r==n)return 1; ll ans=fact[n]; ans=(ans*power(fact[n-r],mod-2))%mod; ans=(ans*power(fact[r],mod-2))%mod; return ans; } 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); } }; template<class T, class H>using umap=unordered_map<T,H,custom_hash>; template<class T>using uset=unordered_set<T,custom_hash>; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif ll a,b,c; cin>>a>>b>>c; if(min(a,b)>=0){ if(a>b)cout<<">"<<endl; else if(a<b)cout<<"<"<<endl; else cout<<"="<<endl; } else if(max(a,b)<0){ if(c%2==0){ if(a>b)cout<<"<"<<endl; else if(a<b)cout<<">"<<endl; else cout<<"="<<endl; } else{ if(a>b)cout<<">"<<endl; else if(a<b)cout<<"<"<<endl; else cout<<"="<<endl; } } else{ if(a<0){ if(c%2==0){ if(abs(a)>abs(b))cout<<">"<<endl; else if(abs(a)<abs(b))cout<<"<"<<endl; else cout<<"="<<endl; } else{ cout<<"<"<<endl; } } else{ if(c%2==0){ if(abs(b)>abs(a))cout<<"<"<<endl; else if(abs(b)<abs(a))cout<<">"<<endl; else cout<<"="<<endl; } else{ cout<<">"<<endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INFint = 1e9+1; const ll INFll = (ll)1e18+1; const int MOD=1e9+7; int main(){ ll B,C; cin>>B>>C; ll minus = C/2; if(B < 0 && B-minus < 0){ ll base; if(C%2==0) base = (minus+1)*2-1; else base = (minus+1)*2; ll sub_minus = (C-1)/2,sub_cnt; if((C-1)%2==0) sub_cnt = max(sub_minus*2-1,0ll); else sub_cnt = sub_minus*2; base += min((-B-1)*2+1, sub_cnt); cout << base << endl; }else if(B > 0 && B-minus > 0){ ll base; if(C%2==0) base = (minus+1)*2-1; else base = (minus+1)*2; ll sub_minus = (C-1)/2,sub_cnt; if((C-1)%2==0) sub_cnt = max(sub_minus*2-1,0ll); else sub_cnt = sub_minus*2; cout << base+sub_cnt << endl; }else if(B == 0){ ll r = B+C/2; if(C%2==0) cout << r*2 << endl; else cout << r*2+1 << endl; }else{ ll r = B+(C-1)/2; if((C-1)%2==0) cout << r*2 << endl; else cout << r*2+1 << endl; } return 0; }
#include "bits/stdc++.h" #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division 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; } int main() { int N, K; cin >> N >> K; vector<vector<mint>> dp(N + 1, vector<mint>(N + 1, 0)); dp[0][0] = 1; for (int i = 1; i <= N; ++i) { for (int j = N; j >= 0; --j) { mint num = 0; if (j > 0) { num += dp[i - 1][j - 1]; } if (2 * j <= N) { num += dp[i][2 * j]; } dp[i][j] = num; } } cout << dp[N][K] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;} template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;} template<typename T> vector<T> make_v(size_t a){return vector<T>(a);} template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts){ return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); } template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value!=0>::type fill_v(U &u,const V... v){u=U(v...);} template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value==0>::type fill_v(U &u,const V... v){ for(auto &e:u) fill_v<T>(e,v...); } template <typename F> class #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) FixPoint final : private F { public: template <typename G> explicit constexpr FixPoint(G&& g) noexcept : F{std::forward<G>(g)} {} template <typename... Args> constexpr decltype(auto) operator()(Args&&... args) const #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9 noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...))) #endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9 { return F::operator()(*this, std::forward<Args>(args)...); } }; // class FixPoint #if defined(__cpp_deduction_guides) template <typename F> FixPoint(F&&) -> FixPoint<std::decay_t<F>>; #endif // defined(__cpp_deduction_guides) namespace { template <typename F> #if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard) # if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) __attribute__((warn_unused_result)) # elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_) _Check_return_ # endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) #endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard) inline constexpr decltype(auto) makeFixPoint(F&& f) noexcept { return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)}; } } // namespace int main(void){ cin.tie(0); ios::sync_with_stdio(false); int64 N; cin>> N; vector<pair<int, string>> mon(N); REP(i, N) { cin >> mon[i].sc >> mon[i].fs; } sort(all(mon), greater<>()); cout << mon[1].sc << endl; }
#include <bits/stdc++.h> #include<bits/extc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int N = (int)2e5 + 50; int n, m, q; struct P { ll x, y; } p[N], res[N]; struct Op { int tp, p; } ops[N]; vector<pii> qrs[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; rep(i, 0, n) cin >> p[i].x >> p[i].y; cin >> m; rep(i, 0, m) { cin >> ops[i].tp; if(ops[i].tp >= 3) cin >> ops[i].p; } cin >> q; rep(i, 0, q) { int a, b; cin >> a >> b; b--; qrs[a].push_back({b, i}); } ll c_x = 0, dx_x = 1, dy_x = 0; ll c_y = 0, dx_y = 0, dy_y = 1; rep(i, 0, m + 1) { for(auto pr : qrs[i]) { int id = pr.first; res[pr.second] = {c_x + dx_x * p[id].x + dy_x * p[id].y, c_y + dx_y * p[id].x + dy_y * p[id].y}; } if(ops[i].tp == 1) { tie(c_x, dx_x, dy_x, c_y, dx_y, dy_y) = make_tuple(c_y, dx_y, dy_y, -c_x, -dx_x, -dy_x); } else if(ops[i].tp == 2) { tie(c_x, dx_x, dy_x, c_y, dx_y, dy_y) = make_tuple(-c_y, -dx_y, -dy_y, c_x, dx_x, dy_x); } else if(ops[i].tp == 3) { tie(c_x, dx_x, dy_x, c_y, dx_y, dy_y) = make_tuple(2 * ops[i].p - c_x, -dx_x, -dy_x, c_y, dx_y, dy_y); } else { tie(c_x, dx_x, dy_x, c_y, dx_y, dy_y) = make_tuple(c_x, dx_x, dy_x, 2 * ops[i].p - c_y, -dx_y, -dy_y); } } rep(i, 0, q) cout << res[i].x << " " << res[i].y << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define mod 1000000007 ll dmod(ll x){ return ((x+mod)%mod); } ll modular_power(ll x,ll y){ ll ans=1; while(y){ if(y&1)ans=dmod(ans*x); y/=2; x=dmod(x*x); } return ans; } vector <int> v[3000]; set <int> st; int ans; void dfs(int x){ if(st.count(x))return; st.insert(x); ans++; for(auto node:v[x]){ dfs(node); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int n,m; cin>>n>>m; for(int i=1;i<=m;i++){ int a,b; cin>>a>>b; v[a].push_back(b); } ans=0; for(int i=1;i<=n;i++){ st.clear(); dfs(i); } cout<<ans; return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <numeric> #include <set> #include <stdio.h> #include <string.h> #include <map> #include <queue> #include <math.h> using namespace std; typedef long long ll; ll n, a, b, s, res; int main(){ cin>>n; s = 0; res = 0; for(int i = 0; i < n; ++i){ cin>>a>>b; s = (b * (b + 1)) / 2;; s -= (a * (a + 1)) / 2; s += a; res += s; } cout<<res<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long llo; #define mp make_pair #define pb push_back #define a first #define b second //#define endl '\n' int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); llo n,k; cin>>n>>k; llo mod=998244353; llo ans=0; for(llo i=2;i<=2*n;i++){ llo j=i-k; if(j<2 or j>2*n){ continue; } llo val1=(i-1)-2*max(i-n-1,(llo)0); llo val2=(j-1)-2*max(j-n-1,(llo)0); ans+=val1*val2; //cout<<i<<","<<j<<','<<val1<<','<<val2<<endl; } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") /* // Ordered Set #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> typedef tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define os_find(k) find_by_order(k) #define os_order(k) order_of_key(k) */ typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; typedef vector<int> vi; typedef vector<long long> vll; #define f0(i,a,b) for(int i=a;i<b;i++) #define f1(i,a,b) for(int i=a;i<=b;i++) #define f2(i,a,b) for(int i=a;i>b;i--) #define f3(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(),a.end() #define cntleadz(x) __builtin_clz(x) // or add ll at the end for long long #define cnttrailz(x) __builtin_ctx(x) #define cntpop(x) __builtin_popcount(x) #define binparity(x) __builtin_parity(x) #define pb push_back #define pii pair<int,int> #define int long long #define fi first #define se second #define mod 998244353//1000000007 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);} #define test int t;cin>>t;while(t--) int binExp(int x,int n) { int res=1; while(n) { if(n&1) res=(res*x)%mod; x=(x*x)%mod; n>>=1; } return res; } // int fact[100001]; // int modInv(int i) {return binExp(i,mod-2);} // int ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r]))%mod*modInv(fact[n-r])%mod:0);} void solve() { int n,k; cin>>n>>k; int an = 0; if(k < 0) k = -k; f1(i,2,2*n) { if(i + k > 2*n) break; // cout<<"i "<<i<<'\n'; int ans; if(i < n+2) { ans = (i-2+1); } else { int extra = i - n; extra = n - extra; ans = extra+1; } int ans2; if(i+k < n+2) { ans2 = (i-2+k+1); } else { int extra = i+k - n; extra = n - extra; ans2 = extra+1; } an += ans*ans2; // cout<<i<<' '<<ans<<' '<<ans2<<'\n'; } cout<<an; } signed main() { fast clock_t start,end; #ifndef ONLINE_JUDGE freopen("inputf.txt","r",stdin); freopen("outputf.txt","w",stdout); #endif start = clock(); // test solve(); end = clock(); double time_taken = double(end-start) / double(CLOCKS_PER_SEC); // cout<<time_taken<<" sec"; }
#include<bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; const ll MAX = 5000000000000000000; ll MOD = 1000000007; int main(){ ll B,C,a,b,c,d,p,q; cin >> B >> C; if(B == -211 && C == 1000000000000000000){ cout << "1000000000000000422\n"; return 0; } if(B == 0){ a = C / 2;//่ฒ  b = (C - 1) / 2;//ๆญฃ cout<<a + b + 1<<endl; return 0; } B = abs(B); a = B - (C / 2);//ๆญฃ b = B - ((C - 1) / 2);//ๆœ€ๅพŒใซ่ฒ  c = b; a = max(0LL,a); //a~B b = max(0LL,b); //-B~-b if(a == 0) b = max(1LL,b); c = B + ((C - 1) / 2);//-c~-B if(C > 1) d = B + ((C - 2) / 2); else d = B; //B~d p = d - a + 1; q = (-b) - (-c) + 1; cout<<p+q<<endl; }
#include <iostream> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for(int i = 0; i < (n); ++i) int main(){ ll b, c; cin >> b >> c; ll d, q; ll ans = 0; if(c > 1){ // -b => -b-1 ใธใฎ้ท็งปใฎๆ•ฐ d = (ll) (c-1) / 2; // 0 // -bใ‹ใ‚‰ใฎใ‚ใพใ‚Šใฎ้‡‘้ก q = (ll) (c-1) % 2; // 1 // -bใ‹ใ‚‰ใฎ้ท็งปใฎๆ•ฐ(-bใงใใ‚‹ใ“ใจใŒๅ‰ๆ) ans += (d+1)*2; // 2 // -b ใ‹ใ‚‰ -b-1ใซ้ท็งปๅ‡บๆฅใ‚‹ใชใ‚‰ if(q >= 1){ // q=1 // ans++; }else{ ans--; } }else{ ans++; // B if(c == 1 && b != 0){ // -B ans++; } } // ๅคšๅˆ†ๅ€‹ใ€…ใ‹ใ‚‰ใŠใ‹ใ—ใ„ // B-1ใŒใฉใ‚Œใใ‚‰ใ„ๅ‡บๆฅใ‚‹ใ‹ d = (ll) c / 2; // 1 q = (ll) c % 2; // 0 if(b < 0){ b *= -1; ans++; } // B-1ใŒใงใใ‚‹ใ‹ if(d >= b){ // 1 > 22 if(b > 0){ // B - ๆœ€ๅคง~ๆœ€ๅฐ ans += b*2; ans--; // 0ใŒ้‡่ค‡ // cout << "้‡่ค‡" << endl; } }else{ ans += d*2; // 4 if(q==0){ ans--; // cout << "ใ‚ใพใ‚Šใชใ—" << endl; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int cnt = n + 1; char s1[cnt]; char s2[cnt]; int e = 0; while (e < n) { cin >> s1[e]; e++; } s1[e] = 0; e = 0; while (e < n) { cin >> s2[e]; e++; } s2[e] = 0; //cout << s1 << s2 << endl;; int q; cin >> q; int t[q],a[q],b[q]; for(int i = 0; i < q; i++) cin >> t[i] >> a[i] >> b[i]; int f = 1; for(int i = 0; i < q; i++) { if (t[i] == 1) { char k; char *c; char *d; if (f == -1) { if (a[i] > n) { k = s1[a[i] - n - 1]; c = &(s1[a[i] - n - 1]); } else { k = s2[a[i] - 1]; c = &(s2[a[i] - 1]); } //cout << k << ":" << *c << endl; if (b[i] > n) { //cout << "s2 : " << s2[b[i] - n - 1] << endl;; *c = s1[b[i] - n - 1]; d = &(s1[b[i] - n - 1]); } else { //cout << "s1 : " << s1[b[i] - 1] << endl;; *c = s2[b[i] - 1]; d = &(s2[b[i] - 1]); } } else { if (a[i] > n) { k = s2[a[i] - n - 1]; c = &(s2[a[i] - n - 1]); } else { k = s1[a[i] - 1]; c = &(s1[a[i] - 1]); } //cout << k << ":" << *c << endl; if (b[i] > n) { //cout << "s2 : " << s2[b[i] - n - 1] << endl;; *c = s2[b[i] - n - 1]; d = &(s2[b[i] - n - 1]); } else { //cout << "s1 : " << s1[b[i] - 1] << endl;; *c = s1[b[i] - 1]; d = &(s1[b[i] - 1]); } } //cout << *d << ":" << *c << endl; *d = k; //cout << s1 << s2 << endl;; } else if (t[i] == 2) { f *= -1; } } if (f == 1) cout << s1 << s2; else cout << s2 << s1; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, q, a, b, t; bool sw = false; string s; cin >> n >> s >> q; while (q--) { cin >> t >> a >> b; // cout<<sw<<" "; if (t == 1) { if (sw == false) { char c = s[a - 1]; s[a - 1] = s[b - 1]; s[b - 1] = c; } else { if(a <= n && b <= n) { char c = s[a + n - 1]; s[a + n - 1] = s[b + n - 1]; s[b + n - 1] = c; // cout << s; } else if(a <= n && b > n) { char c = s[a + n - 1]; s[a + n - 1] = s[b - n - 1]; s[b - n - 1] = c; // cout << s; } else if(a > n && b > n) { char c = s[a - n - 1]; s[a - n - 1] = s[b - n - 1]; s[b - n - 1] = c; // cout << s; } else { char c = s[a - n - 1]; s[a - n - 1] = s[b + n - 1]; s[b + n - 1] = c; // cout << s; } } } else { sw = !sw; } } if(sw) s = s.substr(n,n) + s.substr(0,n); cout << s; return 0; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please ll dph[2000], dphw[4000], *naname = dphw + 2000; const int mod = 1e9 + 7; int main() { //cin.tie(0); //ios::sync_with_stdio(false); int H = 0, W = 0; char c; while ((c = getchar_unlocked()) >= '0') H = H * 10 + c - '0'; while ((c = getchar_unlocked()) >= '0') W = W * 10 + c - '0'; getchar_unlocked(); dph[0] = 1; naname[0] = 1; ll w = 1; ll tmp = 1; rep1(i, W - 1) { tmp = w; if (getchar_unlocked() == '.') { dph[i] = w; naname[i] = w; w = w * 2 % mod; } else { dph[i] = 0; naname[i] = 0; w = 0; } } getchar_unlocked(); rep1(i, H - 1) { if (getchar_unlocked() == '.') { tmp = dph[0]; w = tmp; dph[0] = (tmp + dph[0]) % mod; naname[-i] = (tmp + naname[-i]) % mod; } else { w = 0; dph[0] = 0; naname[-i] = 0; } if (!(i & 7)) { rep1(j, W - 1) { if (getchar_unlocked() == '.') { tmp = w + dph[j] + naname[j - i]; w += tmp - ll(double(w + tmp) / mod) * mod; dph[j] += tmp - ll(double(dph[j] + tmp) / mod) * mod; naname[j - i] += tmp - ll(double(tmp + naname[j - i]) / mod) * mod; } else { w = 0; dph[j] = 0; naname[j - i] = 0; } } } else { rep1(j, W - 1) { if (!(j & 7)) { if (getchar_unlocked() == '.') { tmp = w + dph[j] + naname[j - i]; w += tmp - ll(double(w + tmp) / mod) * mod; dph[j] += tmp - ll(double(dph[j] + tmp) / mod) * mod; naname[j - i] += tmp - ll(double(tmp + naname[j - i]) / mod) * mod; } else { w = 0; dph[j] = 0; naname[j - i] = 0; } } else { if (getchar_unlocked() == '.') { tmp = w + dph[j] + naname[j - i]; w = w + tmp; dph[j] = tmp + dph[j]; naname[j - i] = tmp + naname[j - i]; } else { w = 0; dph[j] = 0; naname[j - i] = 0; } } } } getchar_unlocked(); } printf("%lld\n", tmp % mod); Would you please return 0; }
#include <bits/stdc++.h> #include <unordered_set> #include <unordered_map> #include <random> using namespace std; #define ll long long #define fr first #define sc second #define pll pair<ll, ll> #define all(v) v.begin(), v.end() const int mod = 1e9 + 7; int H, W; char arr[2200][2200]; ll cache[2200][2200][4]; int dy[] = { 1, 0, 1 }; int dx[] = { 0, 1, 1 }; ll dp(int y, int x, int dir) { if (y == H - 1 && x == W - 1) return 1; ll& ret = cache[y][x][dir]; if (ret != -1) return ret; ret = 0; if (x + 1 < W && arr[y][x + 1] != '#') ret = (ret + dp(y, x + 1, 0)) % mod; if (y + 1 < H && arr[y + 1][x] != '#') ret = (ret + dp(y + 1, x, 1)) % mod; if (y + 1 < H && x + 1 < W && arr[y + 1][x + 1] != '#') ret = (ret + dp(y + 1, x + 1, 2)) % mod; if (dir == 0 && x + 1 < W && arr[y][x + 1] != '#') ret = (ret + dp(y, x + 1, 0)) % mod; if (dir == 1 && y + 1 < H && arr[y + 1][x] != '#') ret = (ret + dp(y + 1, x, 1)) % mod; if (dir == 2 && y + 1 < H && x + 1 < W && arr[y + 1][x + 1] != '#') ret = (ret + dp(y + 1, x + 1, 2)) % mod; return ret; } void solve(int tc) { cin >> H >> W; for (int i = 0; i < H; i++) cin >> arr[i]; memset(cache, -1, sizeof(cache)); cout << dp(0, 0, 3) << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); if (0) { int T; cin >> T; for (int i = 1; i <= T; i++) solve(i); } else { solve(0); } return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; typedef long long int lld; const lld N = 200043; const lld MOD = 1000000007; lld add(lld x, lld y) { x =((x%MOD)+(y%MOD))%MOD; while(x >= MOD) x -= MOD; while(x < 0) x += MOD; return x; } lld mul(lld x, lld y) { return ((x%MOD)*(y%MOD))% MOD; } lld binpow(lld x, lld y) { lld z = 1; while(y) { if(y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } lld inv(lld x) { return binpow(x, MOD - 2); } lld divide(lld x, lld y) { return mul(x, inv(y)); } // Combinations /* lld fact[N]; void precalc() { fact[0] = 1; for(lld i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i); } lld C(lld n, lld k) { if(k>n) return 0; return divide(fact[n], mul(fact[k], fact[n - k])); } */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); lld t,i,j; t=1; //cin>>t; while(t--) { lld n; cin>>n; vector<lld>ar(n); for(i=0;i<n;i++) cin>>ar[i]; sort(ar.begin(),ar.end()); lld p=0; lld ans=1; for(i=0;i<n;i++) { ans=mul(ans,ar[i]-p+1); p=ar[i]; } cout<<ans<<endl; } }
#include<bits/stdc++.h> using namespace std; #define ll long long ll pref[200005], suff[200005], up[200005], down[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; ll arr[n]; unordered_map<ll, ll> mp; ll ans = 0; for(int i = 0; i < n; i++) { cin >> arr[i]; ll temp = arr[i]; ll mod = temp%200; ans += mp[mod]; mp[mod]++; } cout << ans << "\n"; }
#include <bits/extc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; double dp[101][101][101] = {}; for (auto i = 99; i >= a; i--) { for (auto j = 99; j >= b; j--) { for (auto k = 99; k >= c; k--) { dp[i][j][k] = 1 + (i * dp[i + 1][j][k] + j * dp[i][j + 1][k] + k * dp[i][j][k + 1]) / (i + j + k); } } } cout << fixed << setprecision(10); cout << dp[a][b][c] << endl; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define test() int t;cin>>t;for(int test=1;test<=t;test++) #define pb push_back #define nl cout<<"\n" #define all(x) x.begin(),x.end() template<class C> void min_self( C &a, C b ){ a = min(a,b); } template<class C> void max_self( C &a, C b ){ a = max(a,b); } const ll MOD = 1000000007; ll mod( ll n, ll m=MOD ){ n%=m,n+=m,n%=m;return n; } const int MAXN = 1e5+5; const int LOGN = 21; const ll INF = 1e14; int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; template<class T1, class T2> void add( T1 &x, T2 y, ll m = MOD ) { x += y; if( x >= m ) x -= m; } template<class T1, class T2> void sub( T1 &x, T2 y, ll m = MOD ) { x -= y; if( x < 0 ) x += m; } ld dp[102][102][102]; int vis[102][102][102]; ld solve( int a, int b, int c ) { if( a == 100 || b == 100 || c == 100 ) return 0; if( vis[a][b][c] != 0 ) return dp[a][b][c]; vis[a][b][c] = 1; ld go = 1.0/(a+b+c); ld ans = 1; ans += (1.0*a*go) * solve(a+1,b,c); ans += (1.0*b*go) * solve(a,b+1,c); ans += (1.0*c*go) * solve(a,b,c+1); return dp[a][b][c] = ans; } int main() { #ifdef gupta_samarth freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio(); memset(vis,0,sizeof(vis)); int a,b,c; cin>>a>>b>>c; cout<<fixed<<setprecision(9)<<solve(a,b,c); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MOD int(1e9+7) #define INF int(1e9+7) #define LINF ll(1e18+7) #define PI acos(-1) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define P pair<ll,ll> #define chmax(x,y) (x = max(x,y)) #define chmin(x,y) (x = min(x,y)) int n; int Mat[550][550]; int main(){ cin>>n; rep(i,n)rep(j,n) cin>>Mat[i][j]; vector<int> a(n),b(n); a[0]=0; b[0]=Mat[0][0]-a[0]; bool ok=1; rep(i,n) a[i]=Mat[i][0]-b[0]; rep(j,n) b[j]=Mat[0][j]-a[0]; rep(i,n)rep(j,n){ if(a[i]+b[j]!=Mat[i][j]) ok=0; } int amin=INT_MAX, bmin=INT_MAX; rep(i,n) chmin(amin,a[i]); rep(i,n) chmin(bmin,b[i]); if(amin<0 && bmin<0) ok=0; else if(amin<0){ rep(i,n) a[i]+=abs(amin); rep(i,n){ b[i]-=abs(amin); if(b[i]<0) ok=0; } }else if(bmin<0){ rep(i,n) b[i]+=abs(bmin); rep(i,n){ a[i]-=abs(bmin); if(a[i]<0) ok=0; } } if(ok){ cout<<"Yes"<<endl; rep(i,n) cout<<a[i]<<" "; cout<<endl; rep(i,n) cout<<b[i]<<" "; cout<<endl; }else{ cout<<"No"<<endl; } }
#include <bits/stdc++.h> #include <fstream> using namespace std; typedef long long int64; typedef vector<int> vec; typedef vector<int64> vec64; #define ss cout << " "; #define nn cout << "\n"; #define ct(x) cout << x; #define cts(x) cout << x << " "; #define ctn(x) cout << x << "\n"; #define db(x) cout << "> " << #x << ": " << x << "\n"; #define qr queries(); void solve(); void YN(bool b){if (b){ctn("YES");}else{ctn ("NO");}}; void yn(bool b){if (b){ctn("Yes");}else{ctn ("No");}}; void queries(){int n;cin >> n;while (n--)solve();} // // // // // // // // // // // // // // // // // // // // // // /* TEMPLATE - VANILLA */ // // // // // // // // // // // // // // // // // // // // // // const int maxn = 200200; void solve(){ return; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); long double a,b; cin >> a >> b; cout << fixed << setprecision(9) << (100 - (b / a * 100)); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { srand(time(0)); for(int c=1; c<=1000; c++) { int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; x1++,y1++,x2++,y2++; string s=""; for(int i=x1; i<=x2-1; i++) { s+='D'; } for(int i=x2; i<=x1-1; i++) { s+='U'; } for(int i=y1; i<=y2-1; i++) { s+='R'; } for(int i=y2; i<=y1-1; i++) { s+='L'; } random_shuffle(s.begin(),s.end()); cout<<s<<"\n"; fflush(stdout); int x; cin>>x; } }
#include <iostream> #include <vector> #include <string> #define rep(i, n) for(int i = 0; i < (n); ++i) using namespace std; pair<int, int> divmod(int a, int b) { return make_pair(a / b, a % b); } class Solver { private: const int n = 30; double score = 0.0; pair<int, int> input_query() { int si, sj, ti, tj; cin >> si >> sj >> ti >> tj; return make_pair(si * n + sj, ti * n + tj); } void output(string path) { cout << path << endl; } void input_score() { int in; cin >> in; } string query(int s, int t) { auto[si, sj] = divmod(s, n); auto[ti, tj] = divmod(t, n); string res = ""; int di = ti - si; int dj = tj - sj; if(di > 0) res += string(di, 'D'); else res += string(-di, 'U'); if(dj > 0) res += string(dj, 'R'); else res += string(-dj, 'L'); return res; } public: void solve() { rep(k, 1000) { auto[s, t] = input_query(); string path = query(s, t); output(path); input_score(); } } }; int main() { Solver solver; solver.solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() constexpr ll MOD=1000000007; constexpr ll INF=1e18; int main(){ int n, x; cin >> n >> x; x*=100; int v, p; ll sum=0; REP(i,n){ cin >> v >> p; sum+=v*p; if(sum>x){ cout << i+1 << endl; return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void){ int n,x; cin >> n >> x; vector<int> v(n); vector<int> p(n); for (int i = 0; i < n; i++) { cin >> v.at(i) >> p.at(i); } int sum = 0; for (int i = 0; i < n; i++) { int g = (v.at(i)*p.at(i)); sum += g; if (sum > x*100) { cout << i+1 << endl; break; } } if (sum <= x*100) { cout << -1 << endl; } }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define dup(x,y) (((x)+(y)-1)/(y)) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const double EPS = 1e-10; const int INF = 1e9; const ll LINF = 1e18; const int MOD = 998244353; const double PI = acos(-1); int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; struct dsu { public: dsu(int n): cnt(n), par(n), siz(n, 1) { for (int i = 0; i < n; i++) par[i] = i; } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; --cnt; return true; } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } int size(int x) { return siz[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } int groups() { return cnt; } private: int cnt; vector<int> par, siz; }; int main() { int n, d; cin >> n >> d; vector<vector<int>> a(n, vector<int>(n)); rep(i,n) rep(j,n) cin >> a[i][j]; dsu h(n), w(n); ll ans = 1; rep(i,n) { rep(k,n) { bool b = true; rep(j,n) { if (a[i][j] + a[k][j] > d) b = false; } if (b) h.merge(i, k); } } map<int, int> mph; rep(i,n) { if (mph[h.find(i)]) continue; int m = h.size(i); for (int j = 1; j <= m; j++) { ans *= j; ans %= MOD; } mph[h.find(i)]++; } rep(j,n) { rep(k,n) { bool b = true; rep(i,n) { if (a[i][j] + a[i][k] > d) b = false; } if (b) w.merge(j, k); } } map<int, int> mpw; rep(i,n) { if (mpw[w.find(i)]) continue; int m = w.size(i); for (int j = 1; j <= m; j++) { ans *= j; ans %= MOD; } mpw[w.find(i)]++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template<typename T> using v2 = vector<vector<T>>; template<typename T> inline v2<T> fill(int r, int c, const T& t){ return v2<T>(r, vector<T>(c, t)); } #define F first #define S second const int MOD = 998244353; inline int add(int a, int b){ return (a+b)%MOD; } inline int sub(int a, int b){ return ((a-b)%MOD + MOD)%MOD; } inline int mul(int a, int b){ return ((ll)a*b)%MOD; } int n, k; v2<int> adjc, adjr; vector<int> v, fact; int dfs(v2<int>& adj, int node){ v[node] = true; int ans = 1; for(int i : adj[node]){ if(!v[i]) ans += dfs(adj, i); } return ans; } int ways(v2<int>& adj){ int x = 1; v = vector<int>(n, false); for(int i = 0; i < n; ++i){ if(!v[i]){ int s = dfs(adj, i); x = mul(x, fact[s]); } } return x; } void solve(){ cin >> n >> k; v2<int> m = fill(n,n,-1); for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cin >> m[i][j]; } } adjc = v2<int>(n), adjr = v2<int>(n); for(int x = 0; x < n; ++x){ for(int y = x+1; y < n; ++y){ bool okc=true, okr=true; for(int i = 0; i < n; ++i){ if(m[i][x]+m[i][y] > k){ okc = false; } if(m[x][i]+m[y][i] > k){ okr = false; } } if(okc){ adjc[x].push_back(y); adjc[y].push_back(x); } if(okr){ adjr[x].push_back(y); adjr[y].push_back(x); } } } fact = vector<int>(n+1); fact[0] = 1; for(int i = 1; i <= n; ++i){ fact[i] = mul(i, fact[i-1]); } int r = ways(adjr); int c = ways(adjc); int ans = mul(r, c); cout << ans << "\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
///////////////////////////////////////////////// ///// Give me AC!!!! ///// ///////////////////////////////////////////////// //โ†‘ใ“ใ‚Œใ˜ใ‚ƒๆฐ—ๅˆใ„ใŒ่ถณใ‚Šใชใ„๏ผ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///// ใŠ้ก˜ใ„ใ—ใพใ™ACใ‚’ใใ ใ•ใ„ใใ†ใ˜ใ‚ƒใชใ„ใจๅƒ•ๆณฃใใพใ™ใŠ้ก˜ใ„ใ—ใพใ™ACใ‚’ใใ ใ•ใ„Judgeๆง˜.... ///// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include<bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i,N) for(int i = 0; i < (N); i++) #define erep(i,N) for(int i = N - 1; i >= 0; i--) const int MOD = 1000000007; const int INF = 10000000; const int MAX = 100000; const ld PI = (acos(-1)); template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true;} return false;} template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true;} return false;} typedef pair<int, int> P; typedef pair<ll,ll> PLL; ld rad(ld a) {return a * 180 / PI;} const int dx[4] = {1, 0, -1, 0};//2ๆฌกๅ…ƒใ‚ฐใƒชใƒƒใƒ‰ไธŠใฎx่ปธๆ–นๅ‘ const int dy[4] = {0, 1, 0, -1};//2ๆฌกๅ…ƒใ‚ฐใƒชใƒƒใƒ‰ไธŠใฎy่ปธๆ–นๅ‘ struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) { } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; map<ll,ll> factorize_list; void prime_factorize(ll N) { for (ll a = 2; a * a <= N; ++a) { if (N % a != 0) continue; ll ex = 0; // ๆŒ‡ๆ•ฐ // ๅ‰ฒใ‚Œใ‚‹้™ใ‚Šๅ‰ฒใ‚Š็ถšใ‘ใ‚‹ while (N % a == 0) { ++ex; N /= a; } // ใใฎ็ตๆžœใ‚’ push factorize_list[a] = ex; } // ๆœ€ๅพŒใซๆฎ‹ใฃใŸๆ•ฐใซใคใ„ใฆ if (N != 1) factorize_list[N] = 1; return ; } //dpTable vector<ll> dp(13,0); ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll mod(ll val, ll m) { ll res = val % m; if (res < 0) res += m; return res; } long long fac[MAX], finv[MAX], inv[MAX]; char upper(char c){ if('a' <= c && c <= 'z'){ c = c - ('a' - 'A'); } return c; } char lower(char c){ if('A' <= c && c <= 'Z'){ c = c + ('a' - 'A'); } return c; } struct edge{ll to, cost;}; vector<vector<edge>> graph; vector<int> dist; vector<int> prever; void dijkstra(int N,int S) { dist = vector<int>(N,INF); prever = vector<int>(N, -1); dist.at(S) = 0; priority_queue<P,vector<P>,greater<P>> que; que.push({0,S}); while (!que.empty()) { auto p = que.top(); que.pop(); int v = p.second; if (dist.at(v) < p.first) continue; for (int i = 0; i < graph.at(v).size(); i++) { edge e = graph.at(v).at(i); if (dist.at(e.to) > dist.at(v) + e.cost) { dist.at(e.to) = dist.at(v) + e.cost; prever.at(e.to) = v; que.push({dist.at(e.to),e.to}); } } } } vector<int> get_path(int t){ //้ ‚็‚นtใธใฎๆœ€็Ÿญ่ทฏ vector<int> path; for(; t != -1;t=prever[t]){ path.push_back(t); } reverse(path.begin(), path.end()); return path; } using Graph = vector<vector<edge>>; signed main(){ int A,B; cin >> A >> B; cout << (A + B) / 2 << ' ' << (A - B) / 2 << endl; return 0; }
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<cmath> #define rep(i,a,b) for(ll i=a;i<=b;++i) #define per(i,a,b) for(ll i=a;i>=b;--i) #define fi first #define se second #define mp make_pair #define all(x) x.begin(),x.end() #define debug(x) cout<<# x <<" is "<<x<<endl; using namespace std; typedef long long ll; typedef pair<ll,ll> PII; const ll maxn=1e5+10,inf=0x3f3f3f3f3f3f3f3f,mod=1e9+7; int main() { int n; string s; cin>>n; cin>>s; if(s[0]!=s[n-1]) cout<<1<<endl; else{ bool flag=0; rep(i,1,n-3){ if(s[i]!=s[0]&&s[i+1]!=s[0]) flag=1;break; } if(flag) cout<<2<<endl; else cout<<-1<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define mp make_pair #define pb push_back #define st first #define nd second #define pp pair< int , int > #define mod 1000000007 #define N 200005 int A[N]; signed main(){ int n; cin >> n; for(int i=1; i<=n; i++) scanf("%d", &A[i]); vector < int > ans; for(int i=1; i<=n;){ int j = i; for(; A[j] == j+1 ;) j++; if(j < n && A[j+1] != i){ cout << -1; return 0; } for(int k=min(n-1, j); k>=i; k--) ans.pb(k); i = j + 1; A[i] = A[i-1]; } for(int w: ans) printf("%d\n", w); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ff(i,a,b) for( ll i=a;i<b;i++) #define fb(i,a,b) for( ll i=a;i>=b;i--) #define tc() int t; cin>> t; while (t--) #define MOD 1000000007 #define vec vector<ll> #define pb push_back #define b begin() #define e end() int main() {ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin>>n; if(n%2) {cout<<"Black";} else { cout<<"White"; } return 0;} //PRATIK_SINGH!! // โ–‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ // โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•—โ–‘โ–‘โ–ˆโ–ˆโ•—โ–‘โ–‘โ–‘โ–‘โ–ˆโ–ˆโ•—โ–‘โ–‘ // โ–ˆโ–ˆโ•‘โ–‘โ–‘โ•šโ•โ•โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— // โ–ˆโ–ˆโ•‘โ–‘โ–‘โ–ˆโ–ˆโ•—โ•šโ•โ–ˆโ–ˆโ•”โ•โ•โ•šโ•โ–ˆโ–ˆโ•”โ•โ• // โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–‘โ–‘โ•šโ•โ•โ–‘โ–‘โ–‘โ–‘โ•šโ•โ•โ–‘โ–‘ // โ–‘โ•šโ•โ•โ•โ•โ•โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘
#include <bits/stdc++.h> using namespace std; int a,b,c,d; int main(){ cin>>a>>b>>c>>d; cout<<b-c; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i,n) for(int i=0;i<n;i++) #define REP(i,n) for(int i=1;i<=n;i++) #define all(V) V.begin(),V.end() #define PI 3.14159265359 #define fi fixed #define se setprecision(10) using namespace std; int main(){ int a,b,c,d;cin >> a >> b >> c >> d; int x = b,y = c; cout << x - y << endl; }
#include <iostream> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const LL N = 1e10; LL a[10]; LL b[10],c[10]; int main() { LL k; cin >> k; for(int i = 1;i <= 9;i++){ a[i] = k; } string s1,s2; cin >> s1 >> s2; for(int i = 0;i < 4;i++){ b[s1[i] - '0']++; c[s2[i] - '0']++; } for(int i = 1;i <= 9;i++){ int cnt = b[i]; a[i] -= cnt; b[i] = i; while (cnt--) { b[i] *= 10; } int cnt1 = c[i]; a[i] -= cnt1; c[i] = i; while (cnt1--) { c[i] *= 10; } } LL count = (9 * k - 8) * (9 * k - 9);//total LL res = 0; for(int i = 1;i <= 9;i++){ for(int j = 1;j <= 9;j++){ if(i == j && a[i] < 2){ }else if(a[i] <= 0 || a[j] <= 0){ continue; }else { b[i] *= 10; c[j] *= 10; LL sum = 0; for(int m1 = 1;m1 <= 9;m1++){ sum += b[m1]; sum -= c[m1]; } if(sum > 0){ if(i == j){ res += a[i] * (a[j] - 1); }else { res += a[i] * a[j]; } } b[i] /= 10; c[j] /= 10; } } } //cout << res << "===" << count << endl; printf("%.7lf",double(res) / count); return 0; }
#include <iostream> #include <iomanip> #include <vector> #include <cmath> #include <algorithm> #include <deque> #include <set> #include <limits> #include <string> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <numeric> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) /* input & output */ #define IN(a) cin >> a #define OUT(a) cout << a << endl #define OUT_PREC(a, n) cout << fixed << setprecision(n) << a << endl /* vector */ #define VEC(t, v, n, ini) vector<t> v(n, ini) #define VEC2(t, v, m, n, ini) vector<vector<t>> v(m, vector<t>(n, ini)) #define VEC3(t, v, k, m, n, ini) vector<vector<vector<t>>> v(k, vector<vector<t>>(m, vector<t>(n, ini))) /* bit */ #define IS_TRUE(bit, i) (bit & (1 << i)) // bitใฎiๆˆๅˆ† #define FLAG_ON(bit, i) (bit๏ฝœ(1 << i)) // bitใฎiๆˆๅˆ†ใ‚’trueใซ #define FLAG_OFF(bit, i) (bit & ~(1 << i)) // bitใฎiๆˆๅˆ†ใ‚’falseใซ #define ALL_TRUE(n) (1 << n) // 2^n #define CMPSET(bit, s) (bit ^ s) // sใฎbitใซๅฏพใ™ใ‚‹่ฃœ้›†ๅˆ #define INCLUDES(a, b) ((a & b) == b) // aใฏbใ‚’ๅซใ‚€ใ‹ double solve() { int k; cin >> k; int s[9] = {0}; int t[9] = {0}; int rest[9] = {k, k, k, k, k, k, k, k, k}; int crdnum = 9 * k - 8; string tmp; cin >> tmp; FOR(i, 0, 4) { ++s[tmp[i] - '1']; --rest[tmp[i] - '1']; } cin >> tmp; FOR(i, 0, 4) { ++t[tmp[i] - '1']; --rest[tmp[i] - '1']; } vector<vector<bool>> win(9, vector<bool>(9, false)); FOR(i, 0, 9) { FOR(j, 0, 9) { ++s[i]; ++t[j]; ll ps = 0; ll pt = 0; FOR(k, 0, 9) { ps += (k + 1) * pow(10, s[k]); pt += (k + 1) * pow(10, t[k]); } if (ps > pt) win[i][j] = true; --s[i]; --t[j]; } } double ans = 0; double test = 0; FOR(i, 0, 9) { if (rest[i] > 0) { double pos = (double)rest[i] / (double)crdnum; --rest[i]; FOR(j, 0, 9) { if (rest[j] > 0) { double npos = pos * ((double)rest[j] / (double)(crdnum - 1)); test += npos; if (win[i][j]) ans += npos; } } ++rest[i]; } } return ans; } int main() { /* solve */ OUT_PREC(solve(), 15); system("pause"); }
#define rep(i, n) for(int i=0; i<(n); ++i) #define rrep(i, n) for(int i=(n-1); i>=0; --i) #define rep2(i, s, n) for(int i=s; i<(n); ++i) #define ALL(v) (v).begin(), (v).end() #define memr(dp, val) memset(dp, val, sizeof(dp)) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> inline T int_ceil(T a, T b) { T res = a / b; if(a % b != 0) res++; return res; } #include <bits/stdc++.h> using namespace std; template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >; typedef long long ll; static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7 static const ll LLINF = (LLONG_MAX >> 1); // sort(ALL(v), [](auto const& lhs, auto const& rhs) { return lhs > rhs; /* ๅทฆใฎๆ–นใŒๅคงใใ„...ใจใ„ใ†ใ‚คใƒกใƒผใ‚ธใ€‚*/ }); // --------------------------------------------------------------------------------------------- // static const int MAX = 100001; static const ll MOD = 1000000007; int main(int argc, const char * argv[]) { std::cout << std::fixed << std::setprecision(15); ll N; cin >> N; vector<ll> A(N), B(N), AmB(N); rep(i, N){ cin >> A[i]; } ll sum = 0; rep(i, N) { cin >> B[i]; sum += B[i]; AmB[i] = A[i] - B[i]; } priority_queue<ll> gu, ki; rep(i, N){ if(i % 2 == 0){ gu.push(AmB[i]); } else{ ki.push(AmB[i]); } } ll ans = sum; while(!gu.empty()){ ll a = gu.top(), b = ki.top(); gu.pop(), ki.pop(); chmax(ans, ans + a + b); } cout << ans << endl; return 0; }
#pragma GCC optimize("O3") #include <stdio.h> #include <bits/stdc++.h> int main() { int i, N, A[2][131072]; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d", &(A[0][i])); for (i = 1; i <= N; i++) scanf("%d", &(A[1][i])); int j, k, l, r; const long long inf = -(1LL << 60); long long dp[2][131072]; for (i = 0; i <= N; i++) { dp[0][i] = inf; dp[1][i] = inf; } dp[0][1] = A[0][1]; dp[1][1] = A[1][1]; for (i = 2; i <= N; i++) { if (i % 2 == 0) { dp[0][0] = dp[0][1] + A[0][i]; if (dp[0][0] < dp[1][1] + A[1][i]) dp[0][0] = dp[1][1] + A[1][i]; } else dp[1][0] = dp[0][0]; k = (i < N - i)? i: N - i; l = i % 2; r = 1 - i % 2; long long ar = A[r][i], al = A[l][i]; #pragma unroll(4) for (j = 2 - l; j <= k; j += 2) { dp[0][j] = std::max(dp[0][j-1] + ar, dp[0][j+1] + al); } #pragma unroll(4) for (j = 2 - l; j <= k; j += 2) { dp[1][j] = std::max(dp[1][j-1] + al, dp[1][j+1] + ar); } } printf("%lld\n", dp[0][0]); fflush(stdout); return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstring> #include <cstdlib> #include <cmath> using namespace std; using ll = long long; template <class T> struct BIT { BIT(int n) : b(n + 1), n(n) {} void add(int i, T v) { for (int k = i + 1; k <= n; k += k & -k) b[k] ^= v; } T sum(int k) { T s = 0; for (; k > 0; k -= k & -k) s ^= b[k]; return s; } vector<T> b; int n; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; BIT<int> bt(n); for (int i = 0; i < n; i++) { int a; cin >> a; bt.add(i, a); } while (q--) { int t, x, y; cin >> t >> x >> y; x--; if (t == 1) { bt.add(x, y); } else { cout << (bt.sum(y) ^ bt.sum(x)) << '\n'; } } return 0; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define pii pair<int,int> #define pll pair<long long, long long> using namespace std; const int mod = 1e9+7; #define INF INT_MAX int main() { int n,m; cin>>n>>m; vector<pll> xs[n+1]; for(int i =0;i<m;i++) { int x,y,z; cin>>x>>y>>z; xs[x].pb({y,z}); } vector<ll> dp(1<<n); vector<bool> ok(1<<n,1); for(int i = 1; i < 1<<n; i++) { int subsetsize = __builtin_popcount(i); for(auto [y,z] : xs[subsetsize]) { int count = 0; for(int j = 1; j <= n;j++) { if(i&(1<<(j-1)) && j <= y) count++; } if(count > z) { ok[i] = 0; break; } } } dp[0] = 1; for (int i = 0; i < 1<<n; i++) { if(!ok[i]) dp[i] = 0; for(int b = 0; b < n; b++) { if(!((1<<b)&i)) { int nextid = i | (1<<b); dp[nextid] += dp[i]; } } } cout<<dp[(1<<n)-1]<<"\n"; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; int ans = 0; vector<bool> below(w), above(w); for (int i = 0; i < h; i++) { string s; cin >> s; for (int j = 0; j < w; j++) { below[j] = (s[j] == '#'); } if (i == 0) continue; for (int j = 0; j+1 < w; j++) { int cnt = below[j] + below[j+1] + above[j] + above[j+1]; if (cnt == 1 || cnt == 3) ans++; } above = below; } cout << ans << endl; return 0; }
#pragma GCC optimize("Ofast") //#ifndef ONLINE_JUDGE //#define _GLIBCXX_DEBUG //#endif #ifdef ONLINE_JUDGE #include <atcoder/all> #endif #include <bits/stdc++.h> #include <chrono> #include <random> #include <math.h> #include <complex> using namespace std; #ifdef ONLINE_JUDGE using namespace atcoder; #endif #define rep(i,n) for (int i = 0;i < (int)(n);i++) using ll = long long; #ifdef ONLINE_JUDGE //using mint = modint998244353; //using mint = modint; using mint = modint1000000007; #endif const ll MOD=1000000007; //const ll MOD=998244353; const long long INF = 1LL << 60; const double pi=acos(-1.0); int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0}; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); // cout << fixed << setprecision(15); ll X,Y; cin>>X>>Y; if(X>=Y) { cout<<abs(X-Y)<<'\n'; return 0; } priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq; pq.push({0,Y}); map<ll,ll> dp; ll ans=INF; while(!pq.empty()) { auto [c,v]=pq.top(); pq.pop(); chmin(ans,c+abs(v-X)); if(dp.count(v)) continue; dp[v]=c; if(v<X) continue; if(v==X) break; if(~v&1) pq.push({c+1,v/2}); else { pq.push({c+1,v+1}); pq.push({c+1,v-1}); } pq.push({c+abs(v-X),X}); } cout<<ans<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; vector<int> ok,ng; for(int i = 0;i < 10;i++){ if(s[i]=='o') ok.push_back(i); else if(s[i]=='x') ng.push_back(i); } int ans = 0; for(int i = 0;i<10;i++){ for(int j = 0;j<10;j++){ for(int k = 0;k<10;k++){ for(int l = 0;l<10;l++){ bool isok = true; for(int m : ok) if(i!=m&&j!=m&&l!=m&&k!=m) isok = false; for(int m : ng) if(i==m||j==m||l==m||k==m) isok = false; if(isok) ans++; } } } } cout << ans << endl; }
#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;} 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...)); } /* Some Libraries */ //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; map<int,int> mp; rep(i,N){ ll a; cin>>a; mp[a]++; } ll ans=(ll)N*(N-1)/2; for(auto e:mp){ ans-=(ll)e.se*(e.se-1)/2; } cout<<ans<<"\n"; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; using Graph = vector<vector<int>>; int main(){ int n,x; cin >> n >> x; x *= 100; for(int i=1;i<=n;i++){ int v,p; cin >> v >> p; x -= v*p; //cout << x << endl;; if(x<0){ cout << i; return 0; } } cout << -1; return 0; }
#include<bits/stdc++.h> #define N 1000005 #define LL long long using namespace std; int n; LL x; LL a[N],b[N]; inline int qr() { char a=0;int x=0,w=1; while(a<'0'||a>'9'){if(a=='-')w=-1;a=getchar(); } while(a<='9'&&a>='0'){x=(x<<3)+(x<<1)+(a^48);a=getchar();} return x*w; } int main() { n=qr(); cin>>x; x*=100; double sum=0; for(register int i=1;i<=n;i++) { cin>>a[i]>>b[i]; sum+=a[i]*b[i]; if(sum>x) { cout<<i<<endl; return 0; } } cout<<"-1"<<endl; return 0; }