code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
// Its just opening , middle game and end game #include<bits/stdc++.h> using namespace std ; #define F first #define S second int MOD = 998244353 ; int mod = 1e9 + 7 ; int inf = 1e9 ; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<int> a(2005) ; vector<int> all_divisors ; signed main() { // OPENING ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ; int n ; cin >> n ; int mn = inf ; for( int i = 0 ; i < n ; i++ ) cin >> a[i] , mn = min( mn , a[i] ) ; ; // sort( a , a + n ) ; // MIDDLE GAME for( int i = 0 ; i < n ; i++ ){ for( int j = 2 ; j*j <= a[i] ; j++ ){ if( a[i] % j == 0 ){ all_divisors.push_back(j) ; all_divisors.push_back(a[i]/j) ; } } } sort( all_divisors.begin() , all_divisors.end() ) ; auto it = unique( all_divisors.begin() , all_divisors.end() ) ; all_divisors.resize( distance( all_divisors.begin() , it ) ) ; all_divisors.emplace_back(1) ; // END GAME int ans = 0 , one = 1 ; for( auto divisor : all_divisors ){ int g = 0 ; if( divisor >= mn ) continue ; for( int i = 0 ; i < n ; i++ ){ if( a[i]%divisor ) continue ; g = __gcd( g , a[i] ) ; if( g == divisor ){ ans++ ; break ; } } } cout << ans + 1 ; cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n"; }
#include <bits/stdc++.h> //#include <atcoder/lazysegtree> //#include <atcoder/segtree> #define overload4(_1, _2, _3, _4, name, ...) name #define rep1(n) for(ll i = 0; i < (n); ++i) #define rep2(i, n) for(ll i = 0; i < (n); ++i) #define rep3(i, a, b) for(ll i = (a); i < (b); ++i) #define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep(i,a,n) for (int i=n-1;i>=a;i--) #define ALL(x) x.begin(),x.end() #define pb push_back #define mp make_pair #define fi first #define se second #define pause "read -p 'Press Enter to continue...' var" using namespace std; //using namespace atcoder; 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; } /* テスト通りますように   ●   /⌒ヽ   |  |/⌒ヽ(ヽ   (` ∥ー⌒) |  | ̄|| ̄ ̄ ̄ ̄ ̄|  |―||―――――|  | U     |  | ̄ ̄ ̄ ̄ ̄ ̄ ̄|  |_______|   |―――――|   |―――――| wwWwwWwWWw */ typedef long long ll; typedef vector<ll> vll; typedef pair<ll,ll> pll; typedef vector<pll> vpll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} const ll INF = numeric_limits<ll>::max()/4; const ll MAX = 100005; const int MOD = 1000000007; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; int main(){ ll n; cin >> n; double L[2005],R[2005]; rep(n){ ll t; double l,r; cin >> t >> l >> r; if(t == 1){ L[i] = l; R[i] = r; } if(t == 2){ L[i] = l; R[i] = r-0.5; } if(t == 3){ L[i] = l+0.5; R[i] = r; } if(t == 4){ L[i] = l+0.5; R[i] = r-0.5; } } ll ans = 0; rep(n){ rep(j,i+1,n){ if(L[i] <= R[j] && R[i] >= L[j]) ans++; } } cout << ans << endl; return 0; }
#include<iostream> using namespace std; void solve(){ int A, B; cin >> A >> B; int X = int((A+B) / 2); int Y = A - X; cout << X << " " << Y << endl; } int main(){ solve(); cin.get(); }
#include<bits/stdc++.h> using namespace std; #define endl "\n" #define LL long long #define ULL unsigned long long const LL INF = 1LL<<59; const double PI = 2.0 * acos(0.0); typedef pair<int,int> pii; typedef pair<LL,LL> pll; typedef vector<int> vi; typedef vector<LL> vl; typedef vector<pii> vii; typedef vector<pll> vll; //// Including Policy Based DS //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; // /////cout<<*X.find_by_order(1)<<endl; /////cout<<X.order_of_key(-5)<<endl; // //typedef tree< //int, //null_type, //less< int >, //rb_tree_tag, //tree_order_statistics_node_update> //ordered_set; #define READ freopen("in.txt","r",stdin) #define WRITE freopen("out.txt","w",stdout) #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define sf scanf #define pf printf #define F first #define S second #define pb push_back #define NL pf("\n") #define ALL(v) v.begin(),v.end() #define si(x) sf("%d",&x); #define sii(x,y) sf("%d%d",&x,&y); #define siii(x,y,z) sf("%d%d%d",&x,&y,&z); #define sl(x) sf("%lld",&x); #define sll(x,y) sf("%lld%lld",&x,&y); #define slll(x,y,z) sf("%lld%lld%lld",&x,&y,&z); #define sqr(x) ( (x) * (x) ) #define mem(x,y) memset(x,y,sizeof x ) #define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n") #define deb(x) cerr<<#x<<" :: "<<x<<"\n" #define SV(v) sort(v.begin(),v.end()) #define SVR(v,cmp) sort(v.begin(),v.end(),cmp) #define FOR(i,n) for(int i=0;i<n;i++) #define cnd tree[idx] #define lc tree[idx*2] #define rc tree[idx*2+1] #define lnd (2*idx),(b),( (b+e) /2 ) #define rnd ((2*idx)+1),(((b+e)/2)+1),(e) // graph direction array [4] int X[4]={0,0,-1,1}; int Y[4]={1,-1,0,0}; // graph direction array [8] //int X[8]={0,0,1,-1,-1,1,1,-1}; //int Y[8]={1,-1,0,0,-1,-1,1,1}; // Bishop direction array [8] //int BX[8]={0,0,1,-1,-1,1,1,-1}; //int BY[8]={1,-1,0,0,-1,-1,1,1}; // Knight Direction Array //int KX[8] = {1,1,2,2,-1,-1,-2,-2}; //int KY[8] = {2,-2,1,-1,2,-2,1,-1}; // Bit Operations #define popcountL __builtin_popcountll #define popcount __builtin_popcount inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));} // check bit inline int setBit(int N,int pos){ return N=N | (1<<pos);}// set bit inline int unsetBit(int N,int pos){ return N=( N & ( ~( 1<<pos ) ) );}// unset bit inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }// toggle bit inline LL modMul(LL a, LL b,LL mod){ LL ans = 0; a = a % mod; while (b > 0){ if ( b % 2 )ans = (ans%mod+ a%mod) % mod; a = (a%mod * 2%mod) % mod; b /= 2; } return ans % mod; } inline LL powerMod(LL a,LL b,LL mod){ if(b==0)return 1LL%mod; LL x=powerMod( a,b/2,mod ); x = ( x%mod * x%mod ) % mod; if( b%2==1 )x = ( x%mod * a%mod ) % mod; return x%mod; } inline LL lcm( LL a,LL b ){ LL g = __gcd(a,b); return ((a/g) * b); } //------------------------------------------------------------------------------- //------------------------------------------------------------------------------- const int mx = 1e5+5; int main(){ // BOOST; #ifdef MujahidPC double start = clock(); // READ; // WRITE; #endif int t,n,m,q; int a,b; cin>>a>>b; int x = (a+b)/2; int y = a-x; cout<<x<<" "<<y<<endl; #ifdef MujahidPC double tim = (clock() - start)/CLOCKS_PER_SEC; cerr<<"Running Time : "<<tim<<" \n"; #endif return 0; }
//競技プログラミング用のテンプレート #include <iostream> #include <vector> #include <algorithm> #include <map> #include <queue> #include <string> #include <math.h> #include <stack> #include <limits> #define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define repr(i, n) for(int i = n - 1; i >= 0; i--) using namespace std; //エイリアス using ll = long long int; using p = pair<int, int>; using pl = pair<ll, ll>; using v = vector<int>; using vd = vector<double>; using vs = vector<string>; using vl = vector<ll>; //定数 const int intmax = numeric_limits<int>::max(); const ll llmax = numeric_limits<ll>::max(); const ll mod = 1e9 + 7; const double pi = M_PI; int main() { string n; cin >> n; int k = n.size(); int mincnt = k + 1; rep(i, pow(2, k)) { string result; for(int tmp = 1, index = 0; index < k; tmp = tmp << 1, index++) { if (i & tmp) { result.push_back(n[index]); } } if (result.size() == 0) continue; ll tmp = stoll(result); if (tmp % 3 == 0) { int cnt = k - result.size(); mincnt = min(mincnt, cnt); } } mincnt = mincnt == k + 1 ? -1 : mincnt; cout << mincnt << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL n, rem[20], len, num, ans; bool fla[20], fla1; vector<int> v; inline LL read() { LL x = 0; bool f = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c <= '9' && c >= '0') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } if (f) return ~(x - 1); return x; } inline void work(int x, int m) { if (fla1) return; if (v.size() > m || v.size() + (len - x + 1) < m) return; if (x == len + 1) { int sum = 0; for (register int i = 1; i <= len; i++) if (!fla[i]) sum += rem[i]; if (sum % 3 == 0 && sum != 0) { printf("%d\n", m); fla1 = 1; return; } } v.push_back(x); fla[x] = 1; work(x + 1, m); v.pop_back(); fla[x] = 0; work(x + 1, m); } int main() { // freopen ("test.in", "r", stdin); // freopen ("test.out", "r", stdin); n = read(); if (n % 3 == 0) { puts("0"); return 0; } LL n1 = n; while (n1) { rem[++len] = n1 % 10; n1 /= 10; } for (register int i = 1; i <= len; i++) { work(1, i); if (fla1) return 0; } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { double r, x, y, l2; std::cin >> r >> x >> y; l2 = (x * x) + (y * y); double l; l = pow(l2, 0.5); double a = l / r; int inta = (int)a; if (a < 1) { std::cout << 2 << '\n'; } else if (a == (double)inta) { std::cout << inta << '\n'; } else { std::cout << inta + 1 << '\n'; } }
#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 a, b; void solve() { cin >> a >> b; ld ans = (a - b) * 100.0 / a; deb1(ans) } int main() { GODSPEED; int test = 1; //cin >> test; for (int i = 1; i <= test; i++) { solve(); } }
#include<bits/stdc++.h> using namespace std; #define ll long long #define int long long //# define int int64_t #define pb push_back //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //used to take input a line after taking input a variable //#define first fi //#define second se #define gok ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); const ll mod = 1e9 + 7,mxn=5e5 + 5; signed main(){ gok int n; cin>>n; string s; cin>>s; stack<char> st; int an=0; for(int i=0;i<=n;i++) { if(st.size()<3&&i<n) { st.push(s[i]); } else if(st.size()>=3) { char c = st.top(); st.pop(); char b = st.top(); st.pop(); char a = st.top(); st.pop(); //cout<<a<<" "<<b<<" "<<c<<endl; if(a=='f'&&b=='o'&&c=='x') an+=3; else { st.push(a); st.push(b); st.push(c); } if(i<n) st.push(s[i]); } } cout<<n-an; return 0; }
/** Fucc 😕 - doanphuc : / * author: doanphuc. * **/ #include <bits/stdc++.h> #define TASK "" #define inpf freopen(TASK".inp", "r", stdin) #define outf freopen(TASK".out", "w", stdout) template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i,x,y) for(int i = x; i <= y ; i ++) #define FORT(i,x,y) for(int i = x; i < y ; i ++) #define FORD(i,x,y) for(int i = x; i >= y ; i --) #define clean(x) memset(x, 0, sizeof(x)) #define F first #define S second #define maxn 100005 #define MOD 1000000007 #define pii pair<int,int> #define ll long long #define db double #define mp make_pair #define pb push_back #define yes cout << "YES" << "\n" #define no cout << "NO" << "\n" #define ct(x) cout << x << endl #define bit(p,x) ((x>>p) & 1) #define all(x) (x).begin(), (x).end() #define resized(x) (x).resize(unique(all(x)) - (x).begin()) #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; ll m, n, k, t, dem = 0, ans = 0 ; string s ; int main() { cin >> n >> s ; for(int i = 0; i < n; i ++) { if(s[i] == 'f' && s[i + 1] == 'o' && s[i + 2] == 'x' && i + 1 < n && i + 2 < n) { s.erase(i, 3) ; i = max(-1, i - 3) ; // i = i - 2 ; } } // cout << s << endl; cout << s.length() ; }
#include <bits/stdc++.h> #define rep(i,n) for (long long i = 0; i < (n); ++i) #define all(v) v.begin(), v.end() using namespace std; using ll = long long; struct Input{ int n; struct Data { int x, y, r; }; Input(){} vector<Data> data; void in(){ cin >> n; data.resize(n); for (int i = 0; i < n; i++) cin >> data[i].x >> data[i].y >> data[i].r; } }; //ミョ(-ω- ?) int main() { Input input; input.in(); rep(i,input.n){ cout << input.data[i].x << " " << input.data[i].y << " " << input.data[i].x + 1<< " " << input.data[i].y + 1 << "\n"; } return 0; }
//雪花飄飄北風嘯嘯 //天地一片蒼茫 #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; #define ll long long #define ii pair<ll,ll> #define iii pair<ii,ll> #define fi first #define se second #define endl '\n' #define debug(x) cout << #x << " is " << x << endl #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define up upper_bound #define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> //change less to less_equal for non distinct pbds, but erase will bug mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); int n; struct dim{ int u,d,l,r; }; dim rect[205]; ii ori[205]; int r[205]; bool grid[10005][10005]; double sq(double i){return i*i;} int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); //freopen("in/0003.txt","r",stdin); cin>>n; int a,b,c; rep(x,0,n){ cin>>a>>b>>c; ori[x]=ii(a,b); r[x]=c; grid[a][b]=true; rect[x]={a,a,b,b}; } ll stime=std::chrono::system_clock::now().time_since_epoch().count(); while (std::chrono::system_clock::now().time_since_epoch().count()-stime<4.5e9){ int idx=rng()%n; int dir=rng()%4; bool can=true; if (dir==0){ if (rect[idx].u==0){can=false;continue;} rep(x,rect[idx].l,rect[idx].r+1) if (grid[rect[idx].u-1][x]) can=false; } if (dir==1){ if (rect[idx].d==9999){can=false;continue;} rep(x,rect[idx].l,rect[idx].r+1) if (grid[rect[idx].d+1][x]) can=false; } if (dir==2){ if (rect[idx].l==0){can=false;continue;} rep(x,rect[idx].u,rect[idx].d+1) if (grid[x][rect[idx].l-1]) can=false; } if (dir==3){ if (rect[idx].r==9999){can=false;continue;} rep(x,rect[idx].u,rect[idx].d+1) if (grid[x][rect[idx].r+1]) can=false; } if (!can) continue; int ai=(rect[idx].d-rect[idx].u+1)*(rect[idx].r-rect[idx].l+1); int af=ai+(dir<2?rect[idx].r-rect[idx].l+1:rect[idx].d-rect[idx].u+1); double ci=1-sq(1-(double)min(ai,r[idx])/max(ai,r[idx])); double cf=1-sq(1-(double)min(af,r[idx])/max(af,r[idx])); //cout<<ci<<" "<<cf<<endl; if (cf>ci){ if (dir==0){ rect[idx].u--; rep(x,rect[idx].l,rect[idx].r+1) grid[rect[idx].u][x]=true; } if (dir==1){ rect[idx].d++; rep(x,rect[idx].l,rect[idx].r+1) grid[rect[idx].d][x]=true; } if (dir==2){ rect[idx].l--; rep(x,rect[idx].u,rect[idx].d+1) grid[x][rect[idx].l]=true; } if (dir==3){ rect[idx].r++; rep(x,rect[idx].u,rect[idx].d+1) grid[x][rect[idx].r]=true; } } } rep(x,0,n){ cout<<rect[x].u<<" "<<rect[x].l<<" "<<rect[x].d<<" "<<rect[x].r<<endl; } }
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <string.h> #include <vector> #include <queue> #include <cmath> #include <complex> #include <functional> #include <numeric> #include <iomanip> #include <cassert> #include <random> #include <chrono> /* #include <atcoder/all> */ /* using namespace atcoder; */ using namespace std; void debug_out(){ cout << "\n"; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1000000007; /* const ll MOD = 998244353; */ const ll INF = 1ll<<60; const double PI = acos(-1); struct INIT { INIT(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }}INIT; struct Edge { int to, t, k; Edge(int to, int t, int k): to(to), t(t), k(k) {} }; signed main() { int N, M, s, g; cin >> N >> M >> s >> g; s--; g--; vector<vector<Edge>> graph(N); REP(i, M){ int u, v, t, k; cin >> u >> v >> t >> k; u--; v--; graph[u].emplace_back(Edge(v, t, k)); graph[v].emplace_back(Edge(u, t, k)); } vector<int> dist(N, INF); dist[s] = 0; using P = pair<int, int>; priority_queue<P, vector<P>, greater<P>> q; q.emplace(0, s); while(!q.empty()){ auto [d, v] = q.top(); q.pop(); if(dist[v] < d) continue; for(auto u : graph[v]){ int nd = (d+u.k-1)/u.k*u.k + u.t; if(chmin(dist[u.to], nd)){ q.emplace(nd, u.to); } } } if(dist[g] == INF) dist[g] = -1; cout << dist[g] << "\n"; return 0; }
#include <bits/stdc++.h> #define INF 10000000000000 using namespace std; typedef long long ll; const int maxn=3e5+5; ll n,m,s,t; struct op{ ll v; ll w,k; ll next; }e[maxn*2]; ll head[maxn*2],cnt; void add(int u,int v,ll w,ll k){ e[++cnt]={v,w,k,head[u]}; head[u]=cnt; } ll dis[maxn]; ll vis[maxn]; struct node{ ll w; ll now; bool operator <(const node &x)const{ return w>x.w; } }; priority_queue<node>q; void dijkstra(){ memset(dis,127, sizeof(dis)); dis[s]=0; q.push((node){0,s}); while(!q.empty()){ node x=q.top(); q.pop(); ll u=x.now; if(vis[u])continue; vis[u]=1; for (int i = head[u]; i ; i=e[i].next) { ll v=e[i].v; if((dis[v]>dis[u]+e[i].w+e[i].k-dis[u]%e[i].k&&dis[u]%e[i].k!=0)||(dis[v]>dis[u]+e[i].w&&dis[u]%e[i].k==0)) { if(dis[u]%e[i].k!=0) dis[v]=dis[u]+e[i].w+e[i].k-dis[u]%e[i].k; else dis[v]=dis[u]+e[i].w; q.push({dis[v],v}); } } } } int main() { scanf("%lld %lld %lld %lld",&n,&m,&s,&t); ll a,b; ll c,d; for (int i = 1; i <= m; ++i) { scanf("%lld %lld %lld %lld",&a,&b,&c,&d); add(a,b,c,d); add(b,a,c,d); } dijkstra(); printf("%lld\n",!vis[t]?-1:dis[t]); return 0; }
/* { ###################### # Author # # Gary # # 2020 # ###################### */ //#pragma GCC target ("avx2") //#pragma GCC optimization ("O3") //#pragma GCC optimization ("unroll-loops") #pragma GCC optimize(2) #include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen("check.out","w",stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ priority_queue<int,vector<int>,greater<int> > heap; int h[200000+20],w[200000+20]; int dp1[200000+20],dp2[200000+20]; int main(){ fastio; int n,m; cin>>n>>m; rb(i,1,n) cin>>h[i]; rb(i,1,m) cin>>w[i],heap.push(w[i]); sort(h+1,h+1+n); rb(i,2,n){ dp1[i]=dp1[i-2]+h[i]-h[i-1]; } rl(i,n-1,1){ dp2[i]=dp2[i+2]+h[i+1]-h[i]; } LL best=1e18; rb(i,0,n){ while(!heap.empty()&&(i==n||heap.top()<=h[i+1])){ LL rest=0; if(i&1){ rest+=dp1[i-1]; rest+=heap.top()-h[i]; rest+=dp2[i+1]; } else{ rest+=dp1[i]; rest+=h[i+1]-heap.top(); rest+=dp2[i+2]; } check_min(best,rest); heap.pop(); } } cout<<best<<endl; return 0; } /** 程序框架: * * * * **/
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define all(v) v.begin(), v.end() #define sz(v) v.size() #define INF 100000000000000 //10^14 template <typename T> bool chmax(T &a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T& b) { if (a > b) { a = b; return true; } return false; } // 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 = 1000000007; 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;} mint mpow(mint x, ll y){ if(y == 0){ return 1; } if(y % 2 == 0){ mint a = mpow(x, y / 2); return a * a; } else{ mint a = mpow(x, y / 2); return a * a * x; } } int main() { ll H, W; cin >> H >> W; vector<string> S(H); rep(i, H){ cin >> S.at(i); } mint ans = 0; vector<vector<ll>> clut_r(H, vector<ll>(1, -1)); rep(i, H){ rep(j, W){ if(S.at(i).at(j) == '#'){ clut_r.at(i).push_back(j); } } clut_r.at(i).push_back(W); } vector<vector<ll>> clut_c(W, vector<ll>(1, -1)); rep(i, W){ rep(j, H){ if(S.at(j).at(i) == '#'){ clut_c.at(i).push_back(j); } } clut_c.at(i).push_back(H); } vector<vector<ll>> space(H, vector<ll>(W, 0)); //各マスを照らす照明を置けるマスの数 rep(i, H){ ll p = 0; rep(j, W){ ll left = clut_r.at(i).at(p); ll right = clut_r.at(i).at(p+1); if(S.at(i).at(j) == '#'){ p++; continue; } space.at(i).at(j) += right-left-1; } } rep(i, W){ ll p = 0; rep(j, H){ ll left = clut_c.at(i).at(p); ll right = clut_c.at(i).at(p+1); if(S.at(j).at(i) == '#'){ p++; continue; } space.at(j).at(i) += right-left-2; } } ll K = 0; rep(i, H){ rep(j, W){ if(S.at(i).at(j) == '.'){ K++; } } } rep(i, H){ rep(j, W){ if(S.at(i).at(j) == '.'){ ans += mpow(2, K) - mpow(2, K-space.at(i).at(j)); //全体 - (i,j)を照らさない置き方 } } } cout << ans << endl; }
#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("fast-math") #pragma GCC optimize("trapv") #pragma GCC target("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define deb(x) cout << #x << " is " << x << "\n" #define int long long #define MOD 1000000007LL #define mod(x) (x % MOD + MOD) % MOD #define PI acos(-1) template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using max_heap = priority_queue<T>; template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename... T> void read(T &... args) { ((cin >> args), ...); } template <typename... T> void write(T &&... args) { ((cout << args), ...); } template <typename T> void readContainer(T &t) { for (auto &e : t) read(e); } template <typename T> void writeContainer(T &t) { for (const auto &e : t) write(e, " "); write("\n"); } void solve(int tc) { int H, W, M; read(H, W, M); vector<int> X(M), Y(M); vector<int> hLimit(H + 1, W), wLimit(W + 1, H); for (int i = 0; i < M; i++) { read(X[i], Y[i]); hLimit[X[i]] = min(hLimit[X[i]], Y[i] - 1); wLimit[Y[i]] = min(wLimit[Y[i]], X[i] - 1); } int ans = 0; for (int i = 1; i <= hLimit[1]; i++) { ans += wLimit[i]; } vector<pair<int, int>> arr; for (int i = 2; i <= wLimit[1]; i++) { arr.push_back({hLimit[i], i}); } sort(arr.begin(), arr.end()); ordered_set<int> st; int r = 0; for (const auto &ele : arr) { while (r < ele.first && r < hLimit[1]) { st.insert(wLimit[++r]); } ans += (hLimit[ele.second] - st.size() + st.order_of_key(ele.second - 1)); } write(ans); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int tc = 1; // read(tc); for (int curr = 1; curr <= tc; curr++) solve(curr); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 123; const int mod = 998244353; #define int long long int t[N * 4]; void upd(int v, int tl, int tr, int pos, int val) { if(tl == tr) { t[v] += val; t[v] = max(t[v], 0LL); return; } int tm = (tl + tr) / 2; if(pos <= tm) upd(v + v, tl, tm, pos, val); else upd(v + v + 1, tm + 1, tr, pos ,val); t[v] = t[v + v]+ t[v + v + 1]; } int get(int v, int tl, int tr, int l, int r) { if(l > r) return 0; if (l == tl && r == tr) return t[v]; int tm = (tl + tr) / 2; return (get (v*2, tl, tm, l, min(r,tm)) + get (v*2+1, tm+1, tr, max(l,tm+1), r)); } vector <int> v[N]; int n, m; main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; int q; cin >> q; for(int i = 1; i <= q; i++) { int x, y; cin >> x >> y; v[y].push_back(x); } for(int i = 1; i <= n; i++) { upd(1, 1, n, i, 1); } long long ans = n * 1LL*m; v[1].push_back(n + 1); sort(v[1].begin(), v[1].end()); for(int j = v[1][0]; j <= n; j++){ upd(1, 1, n, j, -1); ans--; } for(int i = 2; i <= m; i++) { v[i].push_back(n + 1); v[i].push_back(0); sort(v[i].begin(), v[i].end()); ans -= v[i].size() - 2; int l = 1, r = v[i][1] - 1; if(get(1, 1, n, l, r) == r - l + 1 or get(1, 1, n, 1, 1) == 1){ }else { ans -= -get(1, 1, n, l, r) + r - l + 1; } for(int j = 1; j < v[i].size() - 1; j++) { int st = v[i][j] + 1; int ed = v[i][j + 1] - 1; ans -= max(ed - st + 1 - get(1, 1, n, st, ed), 0LL); } for(int j = 1; j < v[i].size() - 1; j++) { if(v[i][j] >= 1 && v[i][j] <= n){ upd(1, 1, n, v[i][j], -1); } } } cout << ans; } //N?
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define MV 200001 #define LMV 21 #define ff first #define ss second #define pb push_back #define eb emplace_back #define emp emplace #define mp make_pair #define ins insert #define sz(x) (int)x.size() #define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); } void whi(istream_iterator<string> it) { cerr<<"\n"; } template<typename T, typename... Args> void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); } void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);} void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);} void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);} typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<PII, int> PPII; typedef pair<PLL, ll> PPLL; typedef map<int, int> MII; const int MOD = 1000000007; const ll INF = 4e18; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; struct h_llint { 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); } }; struct h_pair{ size_t operator()(const PLL&x)const{ return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32)); } }; typedef map<ll, ll> MLL; typedef map<PII, int> MPII; typedef map<PLL, ll> MPLL; typedef set<int> SI; typedef set<ll> SL; //ordered_set = order_of_key(.) //ordered_set = find_by_order(.) typedef ordered_set<int> OSI; typedef ordered_set<ll> OSL; typedef ordered_multiset<int> MOSI; typedef ordered_multiset<ll> MOSL; typedef unordered_map<ll, int, h_llint> UMLI; typedef unordered_map<ll, ll, h_llint> UMLL; typedef unordered_map<PLL, int, h_pair> UMPI; typedef unordered_map<PLL, ll, h_pair> UMPL; int ar[MV]; ll arr[MV]; void solve(int T) { int n; cin>>n; ll zx = 1; for(ll i=2;i<=n;i++) zx = (zx * (i/__gcd(zx, i))); cout<<zx+1<<"\n"; return; } int main(void) { FLASH(); //freopen("cowjog.in", "r", stdin); //freopen("cowjog.out", "w", stdout); int T; T = 1; #ifndef ONLINE_JUDGE time_t time_t1, time_t2; time_t1 = clock(); #endif //cin>>T; while(T--) solve(T); #ifndef ONLINE_JUDGE time_t2 = clock(); SETF(); cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n"; UNSETF(); #endif 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<ll, ll>; const ll INF = ll(1e18) + 5; ll dp[int(1e5) * 2 + 5][2]; // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template) int main() { ll N; cin >> N; vector<ll> maxV(N + 2, -INF); vector<ll> minV(N + 2, INF); rep(i, N) { ll X, C; cin >> X >> C; if (maxV[C] < X) { maxV[C] = X; } if (minV[C] > X) { minV[C] = X; } } maxV[N + 1] = 0; minV[N + 1] = 0; rep(i, N + 5) rep(j, 2) { dp[i][j] = INF; } dp[0][0] = 0; ll preMin = 0; ll preMax = 0; rep(i, N + 1) { if (maxV[i + 1] == -INF) { dp[i + 1][0] = dp[i][0]; dp[i + 1][1] = dp[i][1]; // cerr << "i= " << i << " dp[i+1][0]=" << dp[i + 1][0] << " dp[i+1][1]=" << dp[i + 1][1] << endl; continue; } ll cost = maxV[i + 1] - minV[i + 1]; dp[i + 1][0] = min(dp[i][0] + abs(maxV[i + 1] - preMin) + cost, dp[i][1] + abs(maxV[i + 1] - preMax) + cost); dp[i + 1][1] = min(dp[i][0] + abs(minV[i + 1] - preMin) + cost, dp[i][1] + abs(minV[i + 1] - preMax) + cost); preMin = minV[i + 1]; preMax = maxV[i + 1]; // cerr << "i+1 =" << i + 1 << " dp[i+1][0]=" << dp[i + 1][0] << " dp[i+1][1]=" << dp[i + 1][1] << " cost=" << cost << endl; } cout << dp[N + 1][0] << endl; // Failed to predict input format return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; static const int mod1 = 998244353; static const int mod2 = 1000000007; int main() { string s; cin >> s; int size=s.size(); rep(i,size) { if(i%2==0) { if('a'<=s[i] && s[i]<='z') continue; else { cout << "No" << endl; return 0; } } if(i%2==1) { if('A'<=s[i] && s[i]<='Z') continue; else { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; }
#include <bits/stdc++.h> #include <cassert> #define rep(i, N) for (int i = 0; i < (N); ++i) #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep3(i, a, b) for (ll i = a; i >= b; --i) #define pb push_back #define eb emplace_back #define fi first #define se second #define all(c) begin(c), end(c) #define ok() puts(ok ? "Yes" : "No"); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vi>; using vii = vector<ii>; using gt = greater<int>; using minq = priority_queue<int, vector<int>, gt>; using P = pair<ll, ll>; template <class T> void takeUnique(vector<T> &v) { auto last = std::unique(v.begin(), v.end()); v.erase(last, v.end()); } const ll LINF = 1e18L + 1; const int INF = 1e9 + 1; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1}; // clang++ -std=c++11 -stdlib=libc++ string s; int main() { cin >> s; bool hard = true; rep(i, s.size()) { if (i & 1) { if (s[i] <= 'Z' and s[i] >= 'A') hard &= true; else hard &= false; } else { if (s[i] <= 'z' and s[i] >= 'a') hard &= true; else hard &= false; } } if (hard) puts("Yes"); else puts("No"); return 0; }
//#include <atcoder/maxflow.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #include <iostream> #include <map> #include <list> #include <set> #include <algorithm> #include <vector> #include <string> #include <functional> #include <queue> #include <deque> #include <stack> #include <unordered_map> #include <unordered_set> #include <cmath> #include <iterator> #include <random> #include <chrono> #include <complex> #include <bitset> #include <fstream> #define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) int i; cin >> i #define readll(i) ll i; cin >> i #define readdouble(i) double i; cin >> i #define readstring(s) string s; cin >> s typedef long long ll; //using namespace __gnu_pbds; //using namespace atcoder; using namespace std; const ll modd = (1000LL * 1000LL * 1000LL + 7LL); //const ll modd = 998244353; int main(int argc, char *argv[]) { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); // rand_gen(rng) gets the rand no ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(12); // readint(test_cases); int test_cases = 1; forr(t, 1, test_cases) { readstring(s); bool reverse = false; deque<char> out; for(char c : s) { if (c=='R') { reverse = !reverse; } else { if (reverse) { out.push_front(c); if ((out.size()>=2) && (out[0]==out[1])) { out.pop_front(); out.pop_front(); } } else { out.push_back(c); if ((out.size()>=2) && (out.back()==*prev(out.end(), 2))) { out.pop_back(); out.pop_back(); } } } } forr(i,0,out.size()) { if (reverse) { cout << out[out.size()-1-i]; } else { cout << out[i]; } } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, l, n) for (int i = (int)(l); i < (int)(n); i++) #define drep(i, n, l) for (int i = (int)(n); i > (int)(l); i--) #define INF INT_MAX #define INFF (pow(2,64)) #define def (200007) #define MOD (1000000007) typedef int64_t intt; typedef vector<int> veci; typedef vector<vector<int>> Veci; typedef vector<int64_t> vecit; typedef vector<vector<int64_t>> Vecit; typedef vector<vector<double>> Vecd; typedef vector<double> vecd; typedef pair<intt, intt> P; typedef unordered_set<int> uset; typedef unordered_set<intt> usett; int main(){ int N;cin>>N; Vecit g(N,vecit(3)); int ans=0; rep(i,0,N){ intt t,l,r;cin>>t>>l>>r; g[i]={l,r,t}; } sort(g.begin(),g.end()); rep(i,0,N){ rep(j,i+1,N){ if(g[i][2]==1 || g[i][2]==3){ if(g[j][2]==1 || g[j][2]==2){ if(g[i][1]<g[j][0]){ continue; } }else{ if(g[i][1]<=g[j][0]){ continue; } } }else{ if(g[i][1]<=g[j][0])continue; } ans++; } } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; typedef unsigned long long int ll; #define fast ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL) #define endl "\n" #define ii pair<ll,ll> #define ic pair<char,ll> #define pb push_back #define vi vector<ll> #define popb pop_back #define pf push_front #define popf pop_front #define all(v) (v).begin(), (v).end() #define F first #define S second //#define size(x) (int)(x.size()) ll M = 1000000007; ll i,j; ll fac(ll x); ll inv_fac(ll x); ll binexp(ll a,ll b); ll fac(ll x) { ll fcto=1; for(ll i=2;i<=x;i++) { fcto*=i; fcto%=M; } return fcto; } ll inv_fac(ll x) { return binexp(fac(x),M-2); } ll binexp(ll a,ll b) { if(a==0||a==1) return a; if(b==0) return 1; ll ret= binexp(a,b/2); if(b%2) return (((ret*ret)%M)*a)%M; else return (ret*ret)%M; } bool sortbysecdesc(const pair<int,int> &a, const pair<int,int> &b) { return a.second>b.second; } ll isPrime(ll n) { if (n <= 3) return (1); if (n%2 == 0 || n%3 == 0) return (0); for ( i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return (0); return (1); } ll smallestDivisor(ll n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for ( i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } long long maxPrimeFactors(long long n) { long long maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n >>= 1; } for ( i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } if (n > 2) maxPrime = n; return maxPrime; } ll countDivisors(ll n) { ll cnt = 0; for ( i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) cnt++; else cnt = cnt + 2; } } return cnt; } //Merging two sets in cpp: merge(a.begin(), a.end(), b.begin(), b.end(), inserter(m, m.end())); int main() { fast; ll tt=1; //cin>>tt; while(tt--) { ll n; cin>>n; ll a[n]; map<ll,ll>mp; for(i=0;i<n;i++) { cin>>a[i]; a[i]=a[i]%200; mp[a[i]]++; } map<ll,ll>:: iterator itr; ll sum=0; for(itr=mp.begin();itr!=mp.end();itr++) { if(itr->S>1) { sum+=(itr->S)*(itr->S-1)/2; } } cout<<sum<<"\n"; } }
// // main.cpp // 1014 . 打地鼠 // // Created by Paul Liao on 2021/5/6. // #include <iostream> #include<map> #define ll long long #define AC ios::sync_with_stdio(0);cin.tie(0) #define endl "\n"; #define FOR(i,a,b) for(int i=a;i<b;i++) using namespace std; ll x[20],MOD=1e9+7,haha[2]={(ll)1e9+7,(ll)1e9+9}; ll fpow(ll x,ll y){ ll now,r; r=1; now=x; while(y){ if(y%2){ r*=now; r%=MOD; } now*=now; now%=MOD; y/=2; } return r; } int main() { AC; ll n,a[200005],ans=0; map<int,int> num; cin>>n; FOR(i,0,n){ cin>>a[i]; ans+=num[a[i]%200]; num[a[i]%200]++; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define fr(i, a, b) for (ll i = a; i < b; i++) #define rf(i, a, b) for (ll i = a; i >= b; i--) typedef std::vector<long long> vi; #define F first #define S second #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mod 1000000007 #define PB push_back #define MP make_pair #define PI 3.14159265358979323846 #define all(a) a.begin(), a.end() #define mx(a) *max_element(all(a)) #define mn(a) *min_element(all(a)) const ll INF = LLONG_MAX / 2; const ll N = 2e5 + 1; using namespace std; void solve() { ll n, i, j = 0, b, c, a, d; std::cin >> a >> b >> c >> d; ll e = d * c - b; if (e <= 0) { cout << "-1\n"; return; } ll ans = (a + e - 1) / e; cout << ans << "\n"; } int main() { fast; ll _ = 1, counti = 0; // std::cin >> _; while (_--) { // counti++; // cout << "Case #" << counti << ": "; solve(); } }
#include <chrono> #include <thread> #include<bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; #define T top() #define E end() #define PS push #define Q queue #define PP pop() #define L0 (LL)0 #define V vector #define ST stack #define FI first #define MAX 4e18 #define MIN -MAX #define DQ deque #define SZ size() #define C clear() #define B begin() #define F front() #define SE second #define DL double #define G getline #define IN insert #define endl "\n" #define EM empty() #define cnn continue #define MD 1000000007 #define PSB push_back #define PI acos(-1.00) #define PPB pop_back() #define PSF push_front #define ub upper_bound #define lb lower_bound #define PPF pop_front() #define CF cout.flush() #define line cout<<endl #define LL long long int #define one cout<<1<<endl #define zer cout<<0<<endl #define PQ priority_queue #define kth find_by_order #define nil cout<<-1<<endl #define N cout<<"NO"<<endl #define NN cout<<"No"<<endl #define Y cout<<"YES"<<endl #define fndidx order_of_key #define YY cout<<"Yes"<<endl #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #define MAU cout<<"Case "<<mau<<": " #define SP(pre) fixed<<setprecision(pre) #pragma GCC optimization ("unroll-loops") #define MMAU cout<<"Case "<<mau<<":"<<endl #define __lcm(A1,A2) (A1/(__gcd(A1,A2)))*A2 #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout) #define UOSMOY v.C;w.C;ww.C;uu.C;uo.C;vo.C;vu.C;ab.C;bc.C;cc.C;uuu.C;a.C;b.C;c.C;u.C typedef tree<LL, null_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define remembermeforcenturies using namespace std::chrono_literals;std::this_thread::sleep_for(-4611686018427387904ms); V<LL>v,w,ab,bc; set<LL>uo; map<LL,LL>ww,cc,u; V<pair<LL,LL> >uu,su; map<LL,V<LL> >uuu; map<pair<LL,LL>,LL> vo,vu; map<LL,pair<LL,pair<LL,LL > > > vs; priority_queue<LL,V<LL>,greater<LL> > moy; LL dx[]= {-1,0,1,0,1,1,-1,-1}; LL dy[]= {0,1,0,-1,-1,1,1,-1}; LL dxx[]= {2,1,-1,-2,-2,-1,1,2}; LL dyy[]= {1,2,2,1,-1,-2,-2,-1}; LL dp[101][10001]; LL ar[10][10]; int main() { // BRR; // file; FAST; char ch; string a,b,c; DL x,y,d,e,f,g,h; LL n,i,j,k,p,q,o,l,s,tt=1,m,t,z,aa,r=0; // cin>>tt; for(LL mau=1; mau<=tt; mau++) { UOSMOY; ordered_set os; remembermeforcenturies; cin>>n>>p>>q>>m; r=0; k=0; t=1000000; while(t--) { if(n<=r*m)break; n+=p; r+=q; k++; } if(n>r*m)k=-1; cout<<k<<endl; } return 0; //IN //AL }
//#include<cstdio> //#include<cassert> //#include<iostream> //#include<cstring> //#include<algorithm> #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define MAX ((int)2e9 + 5) #define MAXP ((int)1e5 + 5) #define MAXL ((ll)1e18 + 5) #define MAX_X ((int)2001) #define MAX_Y ((int)2001) #define pi acos(-1) //#define MOD ((int)1e9 + 7) #define MOD ((int)998244353 + 0) #define BAS ((int)1e6 + 3) //#define BAS ((int)2e5 + 3) #define N ((int)4e5 + 9) #define eps (1e-8) #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL) #define logn 17 #define endl "\n" #define mpp make_pair #define BUCK 105 #define LEF (idx<<1) #define RIG ((idx<<1)|1) //#define int ll using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; /*fast io ios_base::sync_with_stdio(false); cin.tie(NULL); */ typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > o_set; typedef tree < pair < int, int >, null_type, less < pair < int, int > >, rb_tree_tag, tree_order_statistics_node_update > o_setp; /// o_set s; /// s.order_of_key(k) : Number of items strictly smaller than k . /// *(s.find_by_order(k)) : K-th element in a set (counting from zero). typedef long double ldd; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int my_rand(int l, int r) { return uniform_int_distribution<int>(l,r) (rng); } int tar[N] , vis[N] , L[N] , R[N]; pair < int , int > arr[N]; queue < int > que; void remv(int a , int b) { int pa = L[a] , pb = R[b]; R[pa] = pb; L[pb] = pa; a = pa , b = pb; if(tar[a] && !vis[a] && !tar[b]) que.push(a); if(tar[b] && !vis[b] && !tar[a]) que.push(b); } int main() { fastio; int n; cin>>n; n += n; string str; for(int i = 1 ; i<=n ; i++) cin>>arr[i].first , arr[i].second = i, str += "@"; str += "@"; sort(arr + 1 , arr + n + 1); for(int i = n ; i > n/2 ; i--){ tar[arr[i].second] = 1; } tar[0] = tar[n+1] = 1; L[0] = 0; R[0] = 1; L[n+1] = n; R[n+1] = n+1; vis[0] = vis[n+1] = 1; for(int i = 1 ; i<=n ; i++){ L[i] = i-1 , R[i] = i+1; if(tar[i] && (!tar[i-1] || !tar[i+1])) que.push(i); } while(!que.empty()){ int a = que.front(); // cout<<a<<" "; que.pop(); if(vis[a]) continue; // cout<<a<<" "; if(L[a] > 0 && !tar[L[a]]){ str[L[a]] = '('; str[a] = ')'; remv(L[a] , a); vis[a] = 1; } else if(R[a] <= n && !tar[R[a]]){ str[a] = '('; str[R[a]] = ')'; remv(a,R[a]); vis[a] = 1; } } for(int i = 0 ; i<n ; i++) str[i] = str[i+1]; str.pop_back(); cout<<str<<endl; return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; signed main(){ int N; cin>>N; vector<pair<int,int>> A(2*N); for(int i=0;i<2*N;i++){ cin>>A[i].first; A[i].second=i; } sort(A.begin(),A.end()); vector<int> B(2*N); for(int i=0;i<N;i++) B[A[i].second]=1; string C(2*N,'('); stack<pair<int,int>> st; for(int i=0;i<2*N;i++){ if(st.size()==0 || st.top().first==B[i]) st.push({B[i],i}); else st.pop(),C[i]=')'; } cout<<C<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<vector<pair<int, long long int>>> G(N); for (int i = 0; i < N - 1; i++) { int u, v; long long int w; cin >> u >> v >> w; u--; v--; pair<int, long long int> p = make_pair(v, w), q = make_pair(u, w); G.at(u).push_back(p); G.at(v).push_back(q); } vector<long long int> dist(N, -1); queue<int> que; que.push(0); dist.at(0) = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0; i < G.at(u).size(); i++) { int v = G.at(u).at(i).first; if (dist.at(v) >= 0) continue; long long int w = G.at(u).at(i).second; dist.at(v) = dist.at(u) ^ w; que.push(v); } } long long int ans = 0, MOD = 1000000007; for (int k = 0; k < 60; k++) { vector<long long int> cnt(2, 0); for (int i = 0; i < N; i++) cnt.at((dist.at(i) >> k) & 1)++; ans += ((long long int)1 << k) % MOD * cnt.at(0) % MOD * cnt.at(1) % MOD; ans %= MOD; } cout << ans << endl; }
// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder #include<bits/stdc++.h> //#include<atcoder/all> typedef long long ll; typedef long double ld; using namespace std; //using namespace atcoder; using Pii = pair<int, int>; using Pll = pair<ll, ll>; //ordered_set 重複不可 #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // use set_function + find_by_order(select itr-num) #define REP(i, l, n) for(ll i=(l), i##_len=(n); i<i##_len; ++i) #define ALL(x) (x).begin(),(x).end() #define pb push_back ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} 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;} char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, 1, -1}; //cout << std::fixed << std::setprecision(15) << y << endl; //小数表示 const ll mod = 1e9 + 7; int main(){ int n; cin >> n; vector<vector<Pll> > tree(n); REP(i,0,n-1){ ll a, b, c; cin >> a >> b >> c; a--; b--; tree[a].pb(Pll(b,c)); tree[b].pb(Pll(a,c)); } vector<ll> dist(n, -1); dist[0] = 0; queue<ll> que; que.push(0); while(!que.empty()){ ll now = que.front(); que.pop(); REP(i,0,tree[now].size()){ int next = tree[now][i].first; if(dist[next] == -1){ que.push(next); dist[next] = dist[now] ^ tree[now][i].second; } } } ll ans = 0; vector<vector<ll>> bit(60, vector<ll>(2, 0)); for(ll i = n-1; i >= 0; i--){ REP(l,0,60){ if((dist[i] & (1ll << l)) == 0){ ans += (((1ll<<l) % mod) * (bit[l][1] % mod))%mod; ans %= mod; bit[l][0]++; }else{ ans += (((1ll<<l) % mod) * (bit[l][0] % mod))%mod; ans %= mod; bit[l][1]++; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N,K; int T[10][10]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K; FOR(y,N) FOR(x,N) cin>>T[y][x]; vector<int> V; FOR(x,N-1) V.push_back(x+1); int ret=0; do { int sum=T[0][V[0]]+T[V.back()][0]; FOR(i,V.size()-1) sum+=T[V[i]][V[i+1]]; if(sum==K) ret++; } while(next_permutation(ALL(V))); cout<<ret<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long int ; using P = pair<ll,ll>; using Graph= vector<vector<ll>>; struct edge{ll to ; ll cost ;} ; using graph =vector<vector<edge>> ; #define rep(i,n) for (ll i=0; i < (n); ++i) #define rep2(i,n,m) for(ll i=n;i<=m;i++) #define rep3(i,n,m) for(ll i=n;i>=m;i--) #define pb push_back #define eb emplace_back #define ppb pop_back #define mpa make_pair #define fi first #define se second #define set20 cout<<fixed<<setprecision(20) ; const ll INF=1e18 ; inline void chmax(ll& a,ll b){a=max(a,b);} inline void chmin(ll& a,ll b){a=min(a,b);} long double pi=acos(-1) ; ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} ll dx[4] {1,0,-1,0} ; ll dy[4] {0,1,0,-1} ; // sum(x) x以下の和 // sum(a,b) a以上b以下の和 template<typename T> struct BIT { int n; vector<T> d; BIT(int n=0):n(n),d(n+1) {} void add(int i, T x) { //x=1ならsumは個数のカウント for (i++; i <= n; i += i&-i) { d[i] += x; } } T sum(int i) { T x = 0; for (i++; i; i -= i&-i) { x += d[i]; } return x; } T sum(int i,int j) { if(i>0) return sum(j)-sum(i-1); else return sum(j) ; } }; int main(){ ios::sync_with_stdio(false) ; cin.tie(nullptr) ; ll n,m,q ; cin>>n>>m>>q ; vector<ll> A(n) ,B(m) ; ll sum=0 ; vector<pair<ll,pair<ll,ll>>> f(q) ; rep(i,q){ cin>>f[i].fi>>f[i].se.fi>>f[i].se.se ; } vector<ll> ff(q+1) ; ff[0]=0ll ; rep(i,q) ff[i+1]=f[i].se.se ; vector<ll> ans(q+1) ; //圧縮後の配列 vector<ll> za ; rep(i,q+1) za.pb(ff[i]) ; sort(za.begin(),za.end()) ; za.erase(unique(za.begin(),za.end()),za.end()) ; rep(i,q+1) ans[i] = lower_bound(za.begin(),za.end(),ff[i])-za.begin() ; //ans[i] //i+1 map<ll,ll> mp ; rep(i,q+1){ mp[ff[i]]=ans[i] ; } BIT<ll> bit(500000) ,cit(500000) ; BIT<ll> x(500000),y(500000) ; x.add(0,n) ; y.add(0,m) ; rep(i,q){ if(f[i].fi==1){ ll now= A[f[i].se.fi-1] ; ll next = f[i].se.se ; ll aa= mp[now] ; ll bb = mp[next] ; if(aa<bb){ sum-= cit.sum(aa,bb) ; sum+= y.sum(aa,bb)*next ; if(aa>0) sum-= y.sum(0,aa-1)*now ; if(aa>0) sum+= y.sum(0,aa-1)*next ; } else{ sum-= y.sum(bb,aa)*now ; sum+=cit.sum(bb,aa) ; if(bb>0) sum-= y.sum(0,bb-1)*now ; if(bb>0) sum+= y.sum(0,bb-1)*next ; } A[f[i].se.fi-1]=next ; bit.add(aa,-now) ; bit.add(bb,next) ; x.add(bb,1) ; x.add(aa,-1) ; } else{ ll now= B[f[i].se.fi-1] ; ll next = f[i].se.se ; ll aa= mp[now] ; ll bb = mp[next] ; if(aa<bb){ sum-= bit.sum(aa,bb) ; sum+= x.sum(aa,bb)*next ; if(aa>0) sum-= x.sum(0,aa-1)*now ; if(aa>0) sum+= x.sum(0,aa-1)*next ; } else{ sum-= x.sum(bb,aa)*now ; sum+=bit.sum(bb,aa) ; if(bb>0) sum-= x.sum(0,bb-1)*now ; if(bb>0) sum+= x.sum(0,bb-1)*next ; } B[f[i].se.fi-1]=next ; cit.add(bb,next) ; cit.add(aa,-now) ; y.add(bb,1) ; y.add(aa,-1) ; } cout<<sum<<endl ; } return 0 ; }
#include<bits/stdc++.h> #define ll long long using namespace std; #define quick ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); #define s(x) sort(x.begin(),x.end()) #define sr(x) sort(x.begin(),x.end(),greater<ll>()) #define v1 vector<ll> #define p1 pair<ll,ll> #define m1 map<ll,ll> #define ff first #define ss second #define ee '\n'; #define pb push_back #define all(v) v.begin(),v.end() #define f(i,x,y) for(i=x;i<y;i++) #define mp make_pair #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl void solve() { string s; cin>>s; cout<<s[1]<<s[2]<<s[0]<<ee; } int32_t main() { quick; ll t=1; //cin>>t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define sf scanf #define pf printf const int high=1e3+5; int main() { string s, ans=""; while(cin >> s) { int len = s.length(); for(int i=1; i<len; i++) { ans += s[i]; } ans+=s[0]; cout << ans << "\n"; ans.clear(); } return 0; }
#include<iostream> #include<string> #include<stdio.h> using namespace std; int main() { int h, w, x, y, c = 0; cin >> h >> w >> x >> y; x--; y--; string *a = new string[h]; for (int i = 0; i < h; i++) cin >> a[i]; if (a[x][y] == '.') c++; for (int j = x - 1; j >= 0; j--) if (a[j][y] == '.') c++; else break; for (int j = x + 1; j <= h - 1; j++) if (a[j][y] == '.') c++; else break; for (int j = y - 1; j >= 0; j--) if (a[x][j] == '.') c++; else break; for (int j = y + 1; j <= w-1; j++) if (a[x][j] == '.') c++; else break; cout << c; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int H, W, X, Y; cin >> H >> W >> X >> Y; vector<vector<char>> mas(H, vector<char>(W)); for (int i=0;i<H;i++) { for (int j=0;j<W;j++) { cin >> mas.at(i).at(j); } } int ans = 0; for (int i=0;;i++) { if (X-1<0 || Y-1+i>=W) break; if (mas[X-1][Y-1+i]=='.') { ans++; } else { break; } } for (int i=0;;i++) { if (X-1<0 || Y-1-i<0) break; if (mas[X-1][Y-1-i]=='.') { if (i==0){ continue; } else { ans++; } } else { break; } } for (int i=0;;i++) { if (X-1+i>=H || Y-1<0) break; if (mas[X-1+i][Y-1]=='.') { if (i==0){ continue; } else { ans++; } } else { break; } } for (int i=0;;i++) { if (X-1-i<0 || Y-1<0) break; if (mas[X-1-i][Y-1]=='.') { if (i==0){ continue; } else { ans++; } } else { break; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using P=pair<ll,ll>; #define rep(i,n) for(int i=0;i<n;i++) int main(){ ll n,k; cin >> n >> k; vector<P> a(n); rep(i,n) cin >> a[i].first >> a[i].second; sort(a.begin(),a.end()); ll x=0,ans; rep(i,n){ if(k<a[i].first-x){ ans=x+k; break; } k-=a[i].first-x; k+=a[i].second; x=a[i].first; ans=a[i].first+k; } cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define pb(a) push_back(a) #define vll vector<ll> #define loop(i, n) for(ll i=1;i<=n;i++) #define loop0(i, n) for(ll i=0;i<n;i++) #define in(i) scanf("%lld", &i); #define out(i) printf("%lld", i) #define pll pair<ll, ll> #define vpll vector<pair<ll, ll>> #define mp(i, j) make_pair(i, j) #define google cout<<"Case #"<<tc-t<<": "; const ll mod = 1000000007; const ll big = 2999999999999999999; const ll small = -2999999999999999999; void in2(ll& a, ll& b) { in(a); in(b); } void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); } void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; } void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); } ll n, dp[101][100001]; vll t; ll solve(ll i, ll rem) { if(rem < 0) return small; if(i == n) return 0; if(dp[i][rem] != -1) return dp[i][rem]; return dp[i][rem] = max(solve(i+1, rem-t[i])+t[i], solve(i+1, rem)); } void solve() { memset(dp, -1, sizeof(dp)); in(n); t.resize(n); ll tot = 0; loop0(i, n) { in(t[i]); tot += t[i]; } sort(t.begin(), t.end(), greater<ll>()); ll rem = (tot+1)/2; ll ans = solve(0, rem); cout<<max(ans, tot-ans)<<"\n"; } int main() { solve(); }
#include<bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define pb push_back #define lb lower_bound #define ub upper_bound #define fi first #define se second #define pii pair<int,int> #define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); #define mp make_pair #define endl '\n' #define int long long typedef double db; typedef long long ll; const int N = 1e5 + 5; const int MOD = 1e9 + 7; const int INF = 1e12; int arr[801][801]; int dp[801][801]; int n, k; bool isPossible(int median) { //build prefix sum to calc size of each k*k submatrix in O(1) memset(dp, 0, sizeof(dp)); //1 if element is smaller than or equal to median for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[i][j] = (arr[i][j] <= median) ? 1 : 0; } } //build pref sum for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 && j == 0) continue; else if (i == 0) { dp[i][j] += dp[i][j - 1]; } else if (j == 0) { dp[i][j] += dp[i - 1][j]; } else { dp[i][j] += (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]); } } } //check if any subarray has cnt of 1s >= (k*k - 1)/2 int limit = ((k * k) - 1) / 2; for (int i = k - 1; i < n; i++) { for (int j = k - 1; j < n; j++) { int cnt = dp[i][j]; if ((i - k) >= 0) cnt -= dp[i - k][j]; if ((j - k) >= 0) cnt -= dp[i][j - k]; if ((i - k) >= 0 && (j - k) >= 0) cnt += dp[i - k][j - k]; if (cnt > limit) return true; } } return false; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } int s = 0, e = 1e9 + 5; int ans = e; while (s <= e) { int mid = (s + e) / 2; if (isPossible(mid)) { ans = mid; e = mid - 1; } else { s = mid + 1; } } cout << ans; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastIO int t = 1; // cin >> t; for (int tt = 1; tt <= t; tt++) { solve(); } }
#include<bits/stdc++.h> using namespace std; #define f(i,a,b) for(register int i=a;i<=b;++i) #define ll long long #define N 200005 #define se second #define fi first #define pb push_back #define mp make_pair inline ll read(){ll x;scanf("%lld",&x);return x;} vector<ll>eg[N]; ll d[N],edge[N][3],deep[N]; void dfs(int s,int dad) { deep[s]=deep[dad]+1; for(auto v:eg[s])if(v!=dad) { dfs(v,s); } } void dfs2(int s,int dad) { for(auto v:eg[s])if(v!=dad) { d[v]+=d[s]; dfs2(v,s); } } void solve() { ll n=read(); f(i,1,n-1) { edge[i][1]=read(); edge[i][2]=read(); eg[edge[i][1]].pb(edge[i][2]); eg[edge[i][2]].pb(edge[i][1]); } dfs(1,0); ll q=read(); f(i,1,q) { ll op=read(),e=read(),x=read(); if(op==1) { if(deep[edge[e][1]]>deep[edge[e][2]]) d[edge[e][1]]+=x; else d[1]+=x,d[edge[e][2]]-=x; } else { if(deep[edge[e][1]]>deep[edge[e][2]]) d[1]+=x,d[edge[e][1]]-=x; else d[edge[e][2]]+=x; } } dfs2(1,0); f(i,1,n) printf("%lld\n",d[i]); } int main() { ll T=1; while(T--) solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include <chrono> #define int long long #define ld long double #define db double #define endl "\n" #define pb push_back #define mp make_pair #define all(x) x.begin(), x.end() using namespace __gnu_pbds; using namespace std; using namespace chrono; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; void deb_out() { cout << endl; } template <typename Head, typename... Tail> void deb_out(Head H, Tail... T) { cout << " " << H; deb_out(T...); } #ifdef LOCAL #define deb(...) cout << "[" << #__VA_ARGS__ << "]:", deb_out(__VA_ARGS__); #else #define deb(...) ; #endif const long double PI = 3.14159265358979323846264338; const long long INF = 1000000000000000000; const long long INF1 = 1000000000; const long long MOD = 1000000007; // const long long MOD = 998244353; inline int cl(int a, int b){ if(a % b) return 1 + a / b; return a / b; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int Tests = 1; // cin >> Tests; while (Tests--) { int n, x; cin >> n >> x; while (n--) { int X; cin >> X; if (X != x) { cout << X << " "; } } } }
#include <bits/stdc++.h> using namespace std; int main() { int a,b; cin >> a >> b; int array[a]; for(int i = 0;i < a;i++){ cin >> array[i]; } for(int i = 0;i < a;i++){ if(array[i] == b) continue; else cout << array[i] << " " ; } }
// Problem : D - Takahashi Unevolved // Contest : AtCoder - AtCoder Beginner Contest 180 // URL : https://atcoder.jp/contests/abc180/tasks/abc180_d // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include<bits/stdc++.h> using namespace std; typedef long long int ll; ll power(ll x,ll y,ll m){if(y==0)return 1;ll p=power(x,y/2,m)%m;p=(p*p)%m;return (y%2==0)?p:(x*p)%m;} ll nCr(ll n,ll r,ll m){if(r>n)return 0;ll a=1,b=1,i;for(i=0;i<r;i++){a=(a*n)%m;--n;}while(r){b=(b*r)%m;--r;}return (a*power(b,m-2,m))%m;} vector<ll> fac; ll nCr2(ll n,ll r,ll m){if(r>n) return 0;return (((fac[n]*power(fac[r],m-2,m))%m)*power(fac[n-r],m-2,m))%m;} ll gcd(ll a, ll b) {return (b ? gcd(b, a % b) : a);} #define Test int t;cin>>t;while(t--) #define endl '\n' void solve() { ll x,y,a,b; cin >> x >> y >> a >> b; ll t1=x,t2=0; int size3 = trunc(log10(y)) + 1; while(t1<y) { int size = trunc(log10(t1)) + 1; int size2 = trunc(log10(a)) + 1; if(size+size2>18 || size+size2>size3) { ll t3=(y-t1); t2+=(t3-1+b)/b; break; } if(t1+b<t1*a) { ll t3=(y-t1); t2+=(t3-1+b)/b; break; } else { t1*=a; t2++; } } cout << t2-1 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //Test solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define ld long double // g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -o int main(){ ll x,y,a,b; cin>>x>>y>>a>>b; // ll xd=x+b; ll exp=0; while(x*a<=x+b && (x*a>0)){ x*=a; if(x>=y || x<=0){ cout<<exp<<"\n"; return 0; } exp++; } // cout<<exp<<"\n"; // cout<<y-x<<"\n"; exp+=((y-x-1)/b); cout<<exp<<"\n"; return 0; }
#include<bits/stdc++.h> using namespace std; int n,k; long long s; void solve(){ cin>>n>>k; if(k<0)k=-k; for(int i=1;i<=2*n-k;++i){ if(n<k+i){ if(n>i){ s+=1ll*(2*n-k-i)*i; } else{ s+=1ll*(2*n-k-i)*(2*n-i); } } else{ s+=1ll*(k+i)*(i); } } cout<<s<<"\n"; } int main(){ solve(); return 0; }
#include <algorithm> #include <iostream> #include <iomanip> #include <numeric> #include <cassert> #include <vector> #include <random> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> //#include <atcoder/all> #define syosu(x) fixed<<setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef pair<double,double> pdd; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int,P> pip; typedef vector<pip> vip; const int inf=1<<30; const ll INF=1ll<<60; const double pi=acos(-1); const double eps=1e-8; const ll mod=998244353; const int dx[8]={-1,0,1,0,1,1,-1,-1},dy[8]={0,1,0,-1,1,-1,1,-1}; //using namespace atcoder; ll Pow(ll n,ll p){ ll r=1; for(;p>0;p>>=1){ if(p&1) r=(r*n)%mod; n=(n*n)%mod; } return r; } const int M=1000005; ll F[M]; void Init(){ F[0]=1; for(int i=1;i<M;i++) F[i]=F[i-1]*i%mod; } ll Div(ll n,ll m){ return n*Pow(m,mod-2)%mod; } ll nCk(ll n,ll k){ return Div(F[n],F[n-k]*F[k]%mod); } ll dp[21][5005],N; ll Rec(int i,int n){ if(n==0) return 1; if(i<0) return 0; if(dp[i][n]>=0) return dp[i][n]; ll res=0,x=1<<i; for(int j=0;j*x<=n&&j<=N;j+=2){ (res+=nCk(N,j)*Rec(i-1,n-j*x))%=mod; } return dp[i][n]=res; } int m; int main(){ for(int i=0;i<21;i++) for(int j=0;j<5005;j++) dp[i][j]=-1; Init(); cin>>N>>m; cout<<Rec(20,m)<<endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll ; typedef pair<int,int> P ; #define rep(i,n) for(int i = 0 ; i < n ; i++) int main(){ string x ; cin >> x ; rep(i, x.size()){ if(x[i] != '.') cout << x[i] ; else break ; } }
#include <bits/stdc++.h> using namespace std; int main() { vector<char> c; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s.at(i) == '.') break; c.push_back(s.at(i)); } for (int i = 0; i < c.size(); i++) { cout << c.at(i); } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N,M,Q; cin >> N >> M >> Q; vector<pair<int, int>> vec; for (int i = 0; i < N; i++) { int w,v; cin >> w >> v; vec.push_back(make_pair(v , w)); } vector<int> X; for (int i = 0; i < M; i++) { int a; cin >> a; X.push_back(a); } for (int i = 0; i < Q; i++) { int64_t sum = 0; vector<int> Y = X; vector<pair<int, int>> VW = vec; int qa,qb; cin >> qa >> qb; Y.erase(Y.begin() + qa-1, Y.begin() + qb); // Yの個数分グリーディーに価値が高い順に選ぶ // 箱の大きさが小さい順に選んでいく sort(Y.begin(), Y.end()); // 価値が高い順に並べる sort(VW.rbegin(), VW.rend()); // 箱の個数分繰り返す for (int j = 0; j < Y.size(); j++) { int num=0; // 箱の大きさ以下でもっとも価値が高いものを選び、潰す for (int k = 0; k < VW.size(); k++) { if(VW.at(k).second <= Y.at(j)){ num = VW.at(k).first; VW.at(k).second = 1e9; break; } } sum += num; } cout << sum << endl; } }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ll long long #define P pair<ll,int> #define PI 3.141592653589793 #define pb push_back const int INF = 1001001001; const ll MX = 1e18; const int mod = 998244353; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int n, m, q; cin >> n >> m >> q; vector<P> a(n); vector<int> x(m); rep(i, n) { cin >> a[i].second >> a[i].first; } sort(a.rbegin(), a.rend()); rep(i, m) cin >> x[i]; rep(qi, q) { ll ans = 0; int l, r; cin >> l >> r; l--; r--; vector<int> xi; vector<int> c(n); rep(j, l) xi.pb(x[j]); for (int j = r + 1; j < m; j++) xi.pb(x[j]); if (xi.size() == 0) { cout << 0 << endl; continue; } sort(xi.begin(), xi.end()); rep(i, xi.size()) { int now = xi[i]; rep(j, a.size()) { if (now >= a[j].second) { if (c[j] != 0) continue; ans += a[j].first; c[j] = 1; break; } } } cout << ans << endl; } }
#include <bits/stdc++.h> #define ll long long #define rep(i,n) for(int i=0;i<n;i++) using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin>>n; vector<ll> v(n), w(n+1); rep(i,n) cin>>v[i]; w[0] = 0; rep(i,n) if (i % 2) w[i+1] = w[i] + v[i]; else w[i+1] = w[i] - v[i]; ll cnt = 0; map<ll, int> mp; mp[0]++; rep(i,n + 1) { if (!i) continue; cnt += mp[w[i]]; mp[w[i]]++; } cout << cnt << "\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; constexpr bool typetest = 0; constexpr int N = 3e5 + 5; int n; ll a[N]; void Read() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i], a[i] = a[i - 1] + ((i & 1) ? 1 : -1) * a[i]; } void Solve() { sort(a, a + n + 1); ll ans(0); for (int i = 0, j = 0; i <= n; ++i) { while (j <= n && a[i] == a[j]) ++j; ans += 1ll * (j - i) * (j - i - 1) / 2; i = j - 1; } cout << ans; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t(1); if (typetest) cin >> t; for (int _ = 1; _ <= t; ++_) { Read(); Solve(); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define repk(i, k, n) for (int i = k; i < n; i++) using namespace std; using ll = long long; int main (){ int n; cin >> n; ll ans = -1; rep(i, n){ ll a, p, x; cin >> a >> p >> x; if(x - a > 0){ if(ans==-1) ans = p; else if(ans>p) ans=p; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10 #define ll long long #define double long double #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #define aint(a) (a).begin(), (a).end() #define f first #define s second #define pb push_back #define mp make_pair #define pi pair #define rc(s) return cout<<s,0 #define endl '\n' #define mod 1000000007ll #define PI 3.14159265359 #define INF 1000000005 #define LINF 1000000000000000005ll #define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; ll a,b,c,d; int32_t main(){ CODE_START; cin>>a>>b>>c>>d; cout<<b-c<<endl; }
#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; const int maxN = 205; bool can[maxN]; int n; int a[maxN], b[maxN]; void bad() { cout << "No"; exit(0); } bool was[maxN]; int par[maxN]; bool le[maxN], ri[maxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // freopen("input.txt", "r", stdin); cin >> n; memset(was, 0, sizeof was); for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; if (a[i] != -1 && b[i] != -1 && a[i] >= b[i]) { bad(); } if (a[i] != -1 && was[a[i]]) bad(); if (b[i] != -1 && was[b[i]]) bad(); if (a[i] != -1) was[a[i]] = true; if (b[i] != -1) was[b[i]] = true; if (a[i] != -1) le[a[i]] = true; if (b[i] != -1) ri[b[i]] = true; if (a[i] != -1 && b[i] != -1) { par[a[i]] = b[i]; par[b[i]] = a[i]; } } memset(can, 0, sizeof can); can[0] = true; for (int r = 1; r <= 2 * n; r++) { for (int l = r - 1; l >= 1; l--) { if (!can[l - 1]) continue; if ((r - l + 1) % 2 != 0) continue; bool ok = true; int len = (r - l + 1) / 2; for (int j = l; j <= l + len - 1; j++) { if (par[j] != 0 && par[j] != j + len) ok = false; if (par[j + len] != 0 && par[j + len] != j) ok = false; if (ri[j]) ok = false; if (le[j + len]) ok = false; if (par[j] == 0 && le[j] && ri[j + len]) ok = false; } if (ok) can[r] = true; } } cout << (can[2 * n] ? "Yes" : "No"); return 0; }
#include <stdio.h> #include <algorithm> #include <set> #include <tuple> #include <queue> #include <vector> using namespace std; using ll = unsigned long long; using pii = pair<int, int>; int n, m, a[10000], b[10000], c[200], d[10000], v[200]; vector<pii> e[200]; void dfs(int x, int p) { if (v[x]) return; v[x] = 1; for (pii ed : e[x]) { int y, id; tie(y, id) = ed; if (y == p) continue; if (d[id]) continue; d[id] = (a[id] == x ? 2 : 3); dfs(y, x); } } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) scanf("%d %d", a + i, b + i); for (int i = 1; i <= n; i++) scanf("%d", c + i); for (int i = 0; i < m; i++) { if (c[a[i]] != c[b[i]]) d[i] = ((c[a[i]] > c[b[i]]) ? 2 : 3); else { e[a[i]].emplace_back(b[i], i); e[b[i]].emplace_back(a[i], i); } } for (int i = 1; i <= n; i++) dfs(i, 0); for (int i = 0; i < m; i++) printf((d[i] ^ 2) ? "<-\n" : "->\n"); }
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #include <bits/stdc++.h> using namespace std; const double TL = 1.5 * CLOCKS_PER_SEC; mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); #define clr(x, y) memset(x, y, sizeof(x)) #define forn(i, n) for (int i = 0; i < n; i++) #define LL long long const double pi = acos(-1.0); int n; int a[200005]; int main() { #ifdef LOCAL auto start_clock = clock(); freopen("in", "r", stdin); #endif scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } bool flag = true; vector<int> ans; for (int i = 1; i <= n;) { if (a[i] == i) { i += 1; continue; } int j = i + 1; for (; j <= n && a[j] != i; j++); int k = j; while (j > i) { j -= 1; swap(a[j], a[j + 1]); ans.push_back(j); } for (int l = i + 1; l < k; l++) { if (a[l] != l) { flag = false; break; } } if (!flag) { break; } } if (flag && (int)ans.size() == (n - 1)) { for (auto w : ans) { printf("%d\n", w); } } else { puts("-1"); } #ifdef LOCAL auto end_clock = clock(); cout << "Time:" << (end_clock - start_clock) * 1.0 / CLOCKS_PER_SEC << endl; #endif return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) typedef long long ll; const ll INF = (1LL << 62) - (1LL << 31); /*オーバーフローしない程度に大きい数*/ const ll MOD = 1000000007; using namespace std; int main() { #ifdef LOCAL std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif int n; cin >> n; vector<int> p(n + 1); vector<int> order(n + 1); vector<int> used(n + 1, false); vector<int> c_used(n + 1, false); // 1〰nまで空いているのか?. typedef pair<int, int> pattern; vector<int> ans1; repi(i, 1, n + 1) { cin >> p[i]; order[p[i]] = i; } bool is_ok = true; repi(i, 1, n + 1) { if (!is_ok) break; for (int pos = order[i]; pos != i; pos--) { if (pos == i) break; if (used[pos - 1]) { is_ok = false; break; } else { used[pos - 1] = true; int tmp = p[pos - 1]; // 1つ前のp[] p[pos - 1] = i; p[pos] = tmp; int tmp_order = order[tmp]; order[tmp] = pos; order[i] = pos - 1; ans1.push_back(pos - 1); } } } if (is_ok && (ans1.size()==(n-1))) { rep(i, ans1.size()) cout << ans1[i] << endl; } else { cout << -1; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll ,ll> pll; #define F first #define S second #define pb push_back #define mp make_pair #define all(X) (X).begin(), (X).end() #define REP(i,x,y) for(ll i = x;i <= y;++i) #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) vector<string> vec_splitter(string s) { for(char& c: s) c = c == ','? ' ': c; stringstream ss; ss << s; vector<string> res; for(string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> args, int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if(idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) void localTest() { #ifndef ONLINE_JUDGE freopen("inp", "r", stdin); freopen("out", "w", stdout); #endif } void solver() { string s; cin >> s; reverse(all(s)); for(auto &c : s) { if(c == '6') { c = '9'; } else if(c == '9') { c = '6'; } } cout << s << "\n"; return; } int main() { localTest(); fastio; int t = 1; // cin >> t; while(t--) { solver(); } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; char fc(char x, char y){ if(x == y) return x; if(x == 'R'){ if(y == 'P') return y; else return x; }else if(x == 'P'){ if(y == 'S') return y; else return x; }else{ if(y == 'R') return y; else return x; } } char rc(int pos, int lv, string &s, int &nxpos){ static vector<vector<char>> dp(lv+1, vector<char>(s.size(), 0)); static vector<vector<int>> posdp(lv+1, vector<int>(s.size(), -1)); int n = s.size(); if(dp.at(lv).at(pos) != 0){ nxpos = posdp.at(lv).at(pos); return dp.at(lv).at(pos); } if(lv == 0){ nxpos = (pos+1) % n; return s.at(pos); } int rpos; char l = rc(pos, lv-1, s, rpos); char r = rc(rpos, lv-1, s, nxpos); posdp.at(lv).at(pos) = nxpos; return dp.at(lv).at(pos) = fc(l, r); } int main(){ int n, k; cin >> n >> k; string s; cin >> s; int tmp; cout << rc(0, k, s, tmp) << endl; return 0; }
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <utility> using ll = long long; //#define rep(i, n) for(int i = 0; i < (int)(n); i++) #define rep(i, n) for(ll i = 0; i < (ll)(n); i++) using namespace std; ll n; ll sum = 0; ll m; ll k; ll minV; int main() { vector<ll> v; cin >> n >> m; rep(i,m) { cin >> k; v.push_back(k); } if (m == 0) { cout << 1; return 0; } sort(v.begin(), v.end()); if(v[v.size()-1] != n) v.push_back(n+1); minV = 1000001000; if (v[0] - 1 < minV && v[0] != 1) minV = v[0] - 1; for (int i = 1; i < v.size(); i++) { if (v[i] - v[i-1] -1 < minV && (v[i] - v[i-1] -1) != 0) minV = v[i] - v[i-1] -1; } if (v[0] != 0){ if ((v[0] - 1) % minV == 0){ sum += (v[0] - 1) / minV; } else{ sum += (v[0] - 1) / minV + 1; } } if (minV == 1000001000) { cout << 0; return 0; } for (int i = 1; i < v.size(); i++) { if((v[i] - v[i-1]-1) % minV == 0){ sum += (v[i] - v[i-1] -1) / minV; } else{ sum += (v[i] - v[i-1]-1) / minV + 1; } } cout << sum; return 0; }
// Problem: C - Many Segments // Contest: AtCoder - AtCoder Beginner Contest 207 // URL: https://atcoder.jp/contests/abc207/tasks/abc207_c // Memory Limit: 1024 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define int long long int #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define vi vector <int> #define ff first #define ss second #define vp vector <pair<int,int>> #define vpp vector <pair<int,pair<int,int>>> #define seti set<int> #define setbit(x) __builtin_popcountll(x) #define sov(v) accumulate(all(v),0) #define fs(it,a) for(auto it=a.begin();it!=a.end();it++) #define pb push_back #define pob pop_back #define mp make_pair #define pqmax priority_queue <int,vector <int>> #define pqmin priority_queue <int,vector <int>,greater<int>> #define dq deque <int> #define umi unordered_map<int,int> #define ums unordered_map<string,int> #define sp(x,y) fixed << setprecision(y) << x #define out(r,x) cout<<"Case #"<<r<<": "<<x<<ee; #define all(x) x.begin(),x.end() #define f(x,y,z) for(x=y;x<z;x++) #define si size() #define countdigit(x) floor(log10(x) +1) #define M 1000000007 #define Z 998244353 #define fill(arr,x) memset(arr,x,sizeof(arr)) //Use (k%M+m)%m always where k is any no #define PI 3.1415926535 #define lcm(a,b) a*b/(__gcd(a,b)) typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pdbs; // // null for set,mapped_type for map; // //less for assending greater for descending //ceil(a/b)=(a+b-1)/b // v.si-1 can never be negative #define ghadi() cerr<<"\nTime Taken : "<<(float)(clock()-time_p)/CLOCKS_PER_SEC<<"\n"; clock_t time_p=clock(); #define ee "\n" #define re return //Author Rahul Sannigrahi vector<int> take(int n) { int i,j; vi v; f(i,0,n) { cin>>j; v.pb(j); } return v; } int power(int x,int y) { int res=1; while(y>0) { if(y&1) res=((res%M)*(x%M))%M; y=y>>1; x=((x%M)*(x%M))%M; } re res; } bool sortinrev(const pair<int,int> &a, //desc of vp const pair<int,int> &b) { return (a.first > b.first); } void show(vector<int>v) { int i; for(i=0;i<v.si;i++) { cout<<v[i]<<" "; } cout<<ee; } int gk=1; int ks=0; int decode() { int i,j,n,k; cin>>n; double l,r; vector<pair<double,double>> v; f(i,0,n) { cin>>k>>l>>r; if(k==1) v.pb({l,r}); else if(k==2) v.pb({l,r-.01}); else if(k==3) v.pb({l+.01,r}); else v.pb({l+.01,r-.01}); } int ans=0; sort(all(v)); // f(i,0,n) // cout<<v[i].ff<<" "<<v[i].ss<<endl; f(i,0,n) { f(j,i+1,n) { if((v[j].ff>=v[i].ff and v[j].ff<=v[i].ss)||(v[j].ss>=v[i].ff and v[j].ss<=v[i].ss)) { ans++; // cout<<v[j].ff<<ee; } } } cout<<ans<<endl; re 0; } int32_t main() { IOS // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif int t=1; // cin>>t; while(t--) { if (ks) { cout << "Case #" << gk << ": "; gk++; } decode(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i, n) for (i = 0; i < n; i++) #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define ll long long #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define PI 3.1415926535897932384626 typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<vi> vvi; //======================= const int MOD = 1000000007; const int N = 2e6 + 13, M = N; //======================= vi g[N]; int arr[N]; vi a; int n, m, k; //======================= void solve() { int i, j; cin >> n; string s; int q; cin >> s >> q; int beg = 0; fo(i, q) { int t, a, b; cin >> t >> a >> b; if (t == 1) { a--; b--; if (beg == 0) { swap(s[a], s[b]); } else { if (a >= n) { a -= n; } else { a += n; } if (b >= n) { b -= n; } else { b += n; } swap(s[a], s[b]); } } else { if (beg == 0) { beg = n; } else { beg = 0; } } } if(beg == 0) cout << s << endl; else { for(i = n; i < 2*n; i++) cout << s[i]; for(i = 0; i < n; i++) cout << s[i]; } } int main() { 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 int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int INF = 2000000000; const int MOD = 1e9 + 7; const int MAX = 30; vector<vector<ll>> dp(MAX+1,vector<ll>(MAX+1)); string solve(int a,int b, ll k){ if(a == 0) return string(b,'b'); if(b == 0) return string(a,'a'); if(k <= dp[a-1][b]) return string ("a") + solve(a-1,b,k); else return string("b") + solve(a,b - 1,k - dp[a-1][b]); } int main(){ ll a,b,k; cin >> a >> b >> k; dp[0][0] = 1; rep(i,a+1){ rep(j,b+1){ if(i) dp[i][j] += dp[i-1][j]; if(j) dp[i][j] += dp[i][j-1]; } } cout << solve(a,b,k) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll mod = 1e9 + 7; ll ans = 0; ll get(map<ll, ll> &parent, ll a) { return parent[a] = (parent[a] == a) ? a : get(parent, parent[a]); } void merge(map<ll, ll> &parent, map<ll, ll> &rank, ll a, ll b) { a = get(parent, a); b = get(parent, b); if (a != b) ans++; if (rank[a] == rank[b]) rank[a]++; if (rank[a] > rank[b]) parent[b] = a; else parent[a] = b; } void solve() { ll n; cin >> n; map<ll, ll> parent; map<ll, ll> rank; vector<ll> a(n); for (ll &i : a) { cin >> i; parent[i] = i; rank[i] = 1; } for (ll i = 0; i < n / 2; ++i) { merge(parent, rank, a[i], a[n - i - 1]); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // ll t; // cin >> t; // while (t--) // { cout << setprecision(15) << fixed; solve(); // } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define mt make_tuple #define ff first #define ss second #define pii pair <int,int> #define pll pair <ll,ll> #define testcase() int t; cin>>t; while(t--) #define forn(i,n) for(int i=0;i<n;i++) #define forn1(i,n) for(int i=1;i<=n;i++) #define vll vector <ll> #define vi vector <int> #define all(v) v.begin(),v.end() ll M = 1e9 + 7; double pi = acos(-1.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll powerm(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x)%M; y=y>>1; x=(x*x)%M;} return res%M; } ll power(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x); y=y>>1; x=(x*x);} return res; } ll gcd(ll a,ll b){if(b>a)return gcd(b,a); if(b==0)return a; return gcd(b,a%b);} const int N = 2e5 + 5; vi adj[N]; vector <bool> vis(N,false); int cc = 0; void dfs(int u,int par=-1) { vis[u] = true; cc++; for(auto it:adj[u]) { if( it == par || vis[it] ) continue; dfs(it,u); } } int main() { //ifstream cin("input.txt"); //ofstream cout("output.txt"); ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vi a(n); forn(i,n) cin >> a[i]; set <pii> s; set <int> kk; for(int i=0;i<n/2;i++) { if( a[i] == a[n-i-1] ) continue; pii p = {a[i],a[n-i-1]}; if( p.ff > p.ss ) swap(p.ff,p.ss); if( !s.count(p) ) { adj[a[i]].pb(a[n-i-1]); adj[a[n-i-1]].pb(a[i]); kk.insert(a[i]); kk.insert(a[n-i-1]); } } int ans = 0; for(auto it:kk) { if( vis[it] ) continue; cc = 0; dfs(it); ans += (cc - 1); } cout << ans << endl; return 0; }
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define LL long long #define DB double #define LD long double #define ST string #define BS bitset #define PA pair<LL,LL> #define VE vector #define VL VE<LL> #define VP VE<PA> #define VVL VE<VL> #define VVVL VE<VVL> #define PQ priority_queue #define PQS priority_queue<LL,vector<LL>,greater<LL>> #define FI first #define SE second #define PB push_back #define POB pop_back #define PF push_front #define POF pop_front #define MP make_pair #define TS to_string #define TU to_ullong #define BPL __builtin_popcountll #define FOR(i,a,n) for(i=a;i<n;++i) #define FORR(i,a,n) for(i=n-1;i>=a;--i) #define rep(i,n) FOR(i,0,n) #define repr(i,n) FORR(i,0,n) #define ALL(a) a.begin(),a.end() #define RALL(a) a.rbegin(),a.rend() #define SORT(a) sort(ALL(a)) #define REV(a) reverse(ALL(a)) #define UB(a,n) *upper_bound(ALL(a),n) #define UBn(a,n) upper_bound(ALL(a),n)-a.begin() #define LB(a,n) *lower_bound(ALL(a),n) #define LBn(a,n) lower_bound(ALL(a),n)-a.begin() #define INF 1000000000000000003 #define PI 3.14159265358979323846264338327950288 //#define MOD 1000000007 #define MOD 998244353 #define ERR 1e-10 #define coutl cout<<fixed<<setprecision(15) #define FAST cin.tie(0);ios::sync_with_stdio(false) void Yn(LL a){if(a)cout<<"Yes"<<endl;else cout<<"No"<<endl;} void YN(LL a){if(a)cout<<"YES"<<endl;else cout<<"NO"<<endl;} LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;} LL dig(LL n){LL ret=0;while(n)n/=10,++ret;return ret;} LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;} LL LCM(LL a,LL b){return a*b/GCD(a,b);} LL cmod(LL a,LL m){if(a%m<0)return a%m+abs(m);else return a%m;} LL DIV(LL a,LL d,LL m){LL l=m,x=1,y=0,k;while(l){k=d/l;d-=k*l;swap(l,d);x-=k*y;swap(x,y);}return cmod(a*cmod(x,m),m);} LL POW(LL a,LL n,LL m){LL ans=1;while(n>0){if(n&1)ans=ans*a%m;a=a*a%m;n>>=1;}return ans;} VL fact,finv,inv; void comi(LL n){LL i;fact.resize(max(2LL,n+1));finv.resize(max(2LL,n+1));inv.resize(max(2LL,n+1));fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[0]=inv[1]=1;FOR(i,2,n+1){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}} LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;} bool cmps(PA a,PA b){if(a.SE!=b.SE)return a.SE<b.SE;return a.FI<b.FI;} template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;} template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;} template<typename T>void vout(VE<T> &v){LL i;rep(i,v.size()){cout<<v[i];if(i<v.size()-1)cout<<" ";}cout<<endl;} template<typename T>void v2out(VE<VE<T>> &v){for(auto a:v)vout(a);} int main(){ FAST; LL i,ans=0,N,S,D,X,Y; cin>>N>>S>>D; rep(i,N){ cin>>X>>Y; if(X<S&&Y>D){ cout<<"Yes"<<endl; return 0; } } cout<<"No"<<endl; //cout<<ans<<endl; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <iostream> #include <fstream> #include <iostream> #include <limits.h> #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <list> #include <map> #include <numeric> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #include <queue> #include <unordered_map> #include <unordered_set> using namespace std; typedef long long ll; class BAchieveTheGoal { public: void solve(std::istream& cin, std::ostream& cout) { int n,k,m;cin >> n >> k >> m; vector<ll> a(n-1);for(int i = 0;i < n-1;i++)cin >> a[i]; int sum = 0; for (int i = 0; i < n-1; ++i) { sum += a[i]; } if(sum >= n*m){ cout << 0 << endl; }else{ if(n*m - sum <= k){ cout << n*m - sum<< endl; }else{ cout << -1 << endl; } } } }; int main() { BAchieveTheGoal solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=a;i<=b;i++) using LL = long long; const int Nmax=109; const LL INF=1e18+9; int N,r; LL X,A[Nmax],dp[Nmax][Nmax][Nmax],ans=INF; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>N>>X; REP(i,1,N) cin>>A[i]; REP(MOD,1,N) { REP(i,0,N) { REP(j,0,N) { REP(k,0,N-1) dp[i][j][k]=-INF; } } dp[0][0][0]=0; REP(i,0,N-1) { REP(j,0,MOD) { REP(k,0,MOD-1) { if (dp[i][j][k]<0) continue; dp[i+1][j][k]=max(dp[i+1][j][k],dp[i][j][k]); // cout<<i+1<<" "<<j<<" "<<k<<" -- "<<dp[i+1][j][k]<<endl; r=(k+A[i+1])%MOD; dp[i+1][j+1][r]=max(dp[i+1][j+1][r],dp[i][j][k]+A[i+1]); // cout<<i+1<<" "<<j+1<<" "<<r<<" : "<<dp[i+1][j+1][r]<<endl; } } } r=X%MOD; if (dp[N][MOD][r]>0) ans=min(ans,(X-dp[N][MOD][r])/MOD); // cout<<MOD<<" "<<r<<" -> "<<(X-dp[N][MOD][r])/MOD<<endl; } cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; typedef long long LL; int n, m, k; int st[N]; long double a[N], b[N], suma[N], sumb[N]; int main() { cin >> n >> m >> k; for (int i = 0; i < k; i++) { int x; cin >> x; st[x] = 1; } for (int i = n - 1; i >= 0; i--) { if (st[i]) { a[i] = 1; b[i] = 0; } else { a[i] = (suma[i + 1] - suma[i + 1 + m]) / (m * 1.0); b[i] = (sumb[i + 1] - sumb[i + 1 + m]) / (m * 1.0) + 1.0; } suma[i] = suma[i + 1] + a[i]; sumb[i] = sumb[i + 1] + b[i]; } if (fabs(a[0] - 1.0) < 1e-6) puts("-1"); else printf("%.4Lf\n", b[0] / (long double)(1.0 - a[0])); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #ifdef ENABLE_DEBUG #define dump(a) cerr<<#a<<"="<<(a)<<endl #define dumparr(a,n) cerr<<#a<<"["<<(n)<<"]="<<(a[n])<<endl #else #define dump(a) #define dumparr(a,n) #endif #define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++) #define For(i, a) FOR(i, 0, a) #define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--) #define Rev(i, a) REV(i, 0, a) #define REP(a) For(i, a) #define SIGN(a) (a==0?0:(a>0?1:-1)) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair<ll, ll> pll; typedef pair<ll,pll> ppll; typedef vector<ll> vll; typedef long double ld; typedef pair<ld,ld> pdd; pll operator+(pll a,pll b){ return pll(a.first+b.first,a.second+b.second); } pll operator-(pll a,pll b){ return pll(a.first-b.first,a.second-b.second); } pll operator*(ll a,pll b){ return pll(b.first*a,b.second*a); } const ll INF=(1LL<<60); #if __cplusplus<201700L ll gcd(ll a, ll b) { a=abs(a); b=abs(b); if(a==0)return b; if(b==0)return a; if(a < b) return gcd(b, a); ll r; while ((r=a%b)) { a = b; b = r; } return b; } #endif template<class T> bool chmax(T& a,const T& b){ if(a<b){ a=b; return true; } return false; } template<class T> bool chmin(T& a,const T& b){ if(a>b){ a=b; return true; } return false; } template<class S,class T> std::ostream& operator<<(std::ostream& os,pair<S,T> a){ os << "(" << a.first << "," << a.second << ")"; return os; } template<class T> std::ostream& operator<<(std::ostream& os,vector<T> a){ os << "[ "; REP(a.size()){ os<< a[i] << " "; } os<< " ]"; return os; } void solve(long long R, long long C, std::vector<std::vector<long long>> A, std::vector<std::vector<long long>> B){ vector<vector<pll>> graph(R*C); auto id2pair = [C](ll x){ return pll(x/C,x%C); }; auto pair2id = [C](pll x){ return x.first*C+x.second; }; For(ii,R*C){ auto [x,y] = id2pair(ii); if(y>0){ auto jj = pair2id(pll(x,y-1)); graph[ii].push_back(pll(jj,A[x][y-1])); graph[jj].push_back(pll(ii,A[x][y-1])); } if(x<R-1){ auto jj = pair2id(pll(x+1,y)); graph[ii].push_back(pll(jj,B[x][y])); graph[jj].push_back(pll(ii,2)); } } priority_queue<tuple<ll,ll,ll>,vector<tuple<ll,ll,ll>>,greater<tuple<ll,ll,ll>>> q; q.push(make_tuple(0LL,0LL,-INF)); vector<ll> distances(R*C,INF); vector<bool> fromxp1(R*C,false); distances[0]=0; while(!q.empty()){ auto [curdist,cur,pre] = q.top();q.pop(); auto [curx,cury]=id2pair(cur); auto [prex,prey]=id2pair(pre); for (auto [next,dist] : graph[cur]) { auto [nextx,nexty]=id2pair(next); if(curx+1==prex&&nextx+1==curx){ --dist; } if(distances[next]>curdist+dist){ distances[next]=curdist+dist; q.emplace(curdist+dist,next,cur); } if(distances[next]==curdist+dist&&nextx+1==curx&&fromxp1[next]==false){ fromxp1[next]=true; q.emplace(curdist+dist,next,cur); } } } cout<<distances[R*C-1]<<endl; } int main(){ cout<<setprecision(1000); long long R; scanf("%lld",&R); long long C; scanf("%lld",&C); std::vector<std::vector<long long>> A(R, std::vector<long long>(C-1)); for(int i = 0 ; i < R ; i++){ for(int j = 0 ; j < C-1 ; j++){ scanf("%lld",&A[i][j]); } } std::vector<std::vector<long long>> B(R-1, std::vector<long long>(C)); for(int i = 0 ; i < R-1 ; i++){ for(int j = 0 ; j < C ; j++){ scanf("%lld",&B[i][j]); } } solve(R, C, std::move(A), std::move(B)); return 0; }
#include <iostream> #include <array> #include <algorithm> #include <vector> #include <bitset> #include <set> #include <unordered_set> #include <cmath> #include <complex> #include <deque> #include <iterator> #include <numeric> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <string> #include <tuple> #include <utility> #include <limits> #include <iomanip> #include <functional> #include <cassert> // #include <atcoder/all> using namespace std; using ll=long long; template<class T> using V = vector<T>; template<class T, class U> using P = pair<T, U>; using vll = V<ll>; using vii = V<int>; using vvll = V<vll>; using vvii = V< V<int> >; using PII = P<int, int>; using PLL = P<ll, ll>; #define RevREP(i,n,a) for(ll i=n;i>a;i--) // (a,n] #define REP(i,a,n) for(ll i=a;i<n;i++) // [a,n) #define rep(i, n) REP(i,0,n) #define ALL(v) v.begin(),v.end() #define eb emplace_back #define pb push_back #define sz(v) int(v.size()) template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; } template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; } template< class A, class B > ostream& operator <<(ostream& out, const P<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template< class A > ostream& operator <<(ostream& out, const V<A> &v) { out << '['; for (int i=0;i<int(v.size());i++) { if (i) out << ", "; out << v[i]; } return out << ']'; } template<class A, class B> istream& operator >>(istream& in, P<A, B> &p) { return in >> p.first >> p.second; } template<class A> istream& operator >>(istream& in, V<A> &v) { for (int i = 0; i < int(v.size()); i++) in >> v[i]; return in; } const long long MOD = 1000000007; const long long HIGHINF = (long long)1e18; const int INF = (int)1e9; int main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t-->0) { int n; cin >> n; vii p(n); cin >> p; if (n == 2) { if (p[0] > p[1]) { cout << 1 << '\n'; cout << 1 << '\n'; } else { cout << 0 << '\n'; cout << '\n'; } continue; } vii ans; for (int tor = n; tor > 3; tor--) { int j = 1; for (; j <= n; j++) if (p[j - 1] == tor) break; if (j == tor) continue; if (sz(ans) & 1) { // guusuu kaime if (j & 1) { if (j == 1) { ans.eb(j + 1), swap(p[j], p[j + 1]); } else { ans.eb(j - 1), swap(p[j - 2], p[j - 1]); ans.eb(j), swap(p[j - 1], p[j]); j--; } } for (int k = j; k < tor; k++) { ans.eb(k); swap(p[k - 1], p[k]); } } else { // kisuu kaime if (!(j & 1)) { ans.eb(j - 1), swap(p[j - 2], p[j - 1]); ans.eb(j), swap(p[j - 1], p[j]); j--; } for (int k = j; k < tor; k++) { ans.eb(k); swap(p[k - 1], p[k]); } } } cerr << p << '\n'; while (!(p[0] == 1 and p[1] == 2 and p[2] == 3)) { if (sz(ans) & 1) { ans.eb(2); swap(p[1], p[2]); } else { ans.eb(1); swap(p[0], p[1]); } } cout << sz(ans) << '\n'; rep(i, sz(ans)) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
//#include <atcoder/all> //using namespace atcoder; #include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for(int i=0; i<n; i++) #define REPR(i, n) for(int i=n-1; i>=0; i--) #define FOR(i, m, n) for(int i=m; i<n; i++) #define ALL(v) v.begin(), v.end() #define bit(n) (1LL<<(n)) #define FIX(d) fixed << setprecision(d) using P = pair<int, int>; using Graph = vector<vector<int>>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1e9; const ll LINF = (1LL<<60); int MOD; ll modpow(ll a, ll n ) { ll res = 1; while(n > 0) { if(n & 1) res = res * a % MOD; a = a * a % MOD; n >>= 1; } return res; } ll modinv(ll a) { ll b = MOD, u = 1, v = 0; while(b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= MOD; if(u < 0) u += MOD; return u; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll n,m; cin >> n >> m; MOD = m*m; ll ans = modpow(10, n); cout << ans/m << endl; return 0; }
//c++ テンプレ #include<bits/stdc++.h> using namespace std; typedef long long llint; typedef long double ld; #define inf 1e18 #define mod 1000000007 #define sort(v) sort(v.begin(),v.end()) #define reverse(v) reverse(v.begin(),v.end()) #define rep(i,a,n) for(int i=a;i<n;i++) priority_queue<llint,vector<llint>,greater<llint> > que; priority_queue<llint> Que; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } int a,b; void solve(){ cin >> a >> b; int ans; if(a+b>=15 && b>=8)ans=1; else if(a+b>=10 && b>=3)ans=2; else if(a+b>=3)ans=3; else ans=4; cout << ans << endl; } int main(){ solve(); return 0; }
#include <iostream> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <queue> #include <unordered_map> using namespace std; #define int long long #define pii pair<long long, long long> const double pi = acos(-1); const int maxn = 3e5 + 5; const int mod = 1000000007; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int a,b,x,y; cin >> a >> b >> x >> y; y = min(y,x*2); if(a > b) a--; cout << abs(a-b) * y + x << endl; return 0; }
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> inline int read() { int x = 0, f = 1; char ch = getchar(); while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); } do x = x * 10 + ch - 48, ch = getchar(); while(ch >= '0' && ch <= '9'); return x * f; } int f[101][2]; int main() { int a = read(), b = read(), x = read(), y = read(); if(b >= a) { f[a][1] = x; for(int i = a + 1;i <= b;i++) { f[i][0] = std::min(f[i - 1][0] + y,f[i - 1][1] + x); f[i][1] = std::min(f[i - 1][1] + y,f[i][0] + x); f[i][0] = std::min(f[i][0],f[i - 1][1] + x + y); } std::printf("%d\n",f[b][1]); } else { f[a][1] = x; for(int i = a - 1;i >= b;i--) { f[i][1] = std::min(f[i + 1][0] + x,f[i + 1][1] + y); f[i][0] = std::min(f[i][1] + x,f[i + 1][0] + y); f[i][1] = std::min(f[i][1],f[i + 1][0] + y + x); } std::printf("%d\n",f[b][1]); } return 0; }
#include <cstdio> #include <algorithm> #define N 200010 using namespace std; long long n,k,cur=0; struct node{ long long a,b; bool operator<(const node x)const{ return a<x.a; } }c[N]; int main(){ scanf("%lld%lld",&n,&k); for(int i=1;i<=n;i++) scanf("%lld%lld",&c[i].a,&c[i].b); sort(c+1,c+1+n); for(int i=1;i<=n;i++){ if(cur+k<c[i].a){ printf("%lld",cur+k); return 0; } k=c[i].b-c[i].a+cur+k; cur=c[i].a; } printf("%lld",cur+k); return 0; }
//Radhe Radhe #include<bits/stdc++.h> #define FASTIO_ ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mod 1000000007 #define ll long long #define ld long double #define fi first #define se second #define mp make_pair #define pb push_back #define en '\n' #define itr(i,n) for(ll i=0; i<n; i++) #define itr_ab(i,a,b) for(ll i=a; i<=b; i++) #define itrV(i,n) for(ll i=0; i<v.size(); i++) #define fixed cout.setf(ios::fixed); #define precise cout.precision(2); #define Dont_Quit int main(void) #define Avada_Kedavra return 0; #define yes cout<<"Yes"<<'\n' #define no cout<<"No"<<'\n' using namespace std; bool cmp(const pair<ll,ll>&a,const pair<ll,ll>&b) { if(a.fi<b.fi) return 1; return 0; } Dont_Quit { FASTIO_ ll t,n,i,j,k,ans; t=1; while(t--) { ll n,k,ans; cin>>n>>k; ans=k; ll a[n],b[n],cur=0; bool ok=1; vector<pair<ll,ll>>v; itr(i,n) { cin>>a[i]>>b[i]; v.pb(mp(a[i],b[i])); } sort(v.begin(),v.end(),cmp); for(auto it:v) { if(ans>=it.fi) ans+=it.se; else break; } cout<<ans<<en; } Avada_Kedavra }
//doit4von //LLJW //999 #include<bits/stdc++.h> using namespace std; const int N=1e6+5; const long long mod=pow(10,9)+7; typedef long long ll; #define pb push_back #define vi vector<int> #define sz(x) (int)(x).size() void google(int tc) { cout<<"Case #"<<tc<<": "; } ll convert(string z) { ll ans=0; int count=0; for(int i=z.length()-1;i>=0;i--) { ans=ans+(z[i]-'0')*pow(10,count); count++; } return ans; } bool palindrome(const string &s) { int n = s.length(); for(int i = 0; i < n; i++) { if(s[i] != s[n - i - 1]) return false; } return true; } bool primecheck(long long int z) { for(long long int i=2;i*i<=z;i++) { if(z%i==0) return false; } return true; } string boolean (int y) { string ans=""; while(y>0) { ans=(char)('0'+y%2)+ans; y=y/2; } return ans; } ll rem(ll x) { ll ans=0; while(x>0) { if(x%10) ans=ans*10+x%10; x=x/10; } return ans; } /*ll total=0; ll no=0; bool visited[N]; vector <int> v[N]; int n; void dfs(int curr ,int level)// curr=current node level=number of nodes travelled to reach there { if(visited[curr]==true) return; visited[curr]=true; for(auto y : v[curr]) dfs(y,level+1); if(v[curr].size()==1 && curr!=1) { total=total+level; no++; } }*/ int SieveOfEratosthenes(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } int ans=0; for (int p = 2; p <= n; p++) ans+=prime[p]; return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin>>n; map <int,int> a; for(int i=0;i<n;i++) { int ex; cin>>ex; a[ex]++; } int b[n+2]; for(int i=1;i<=n;i++) cin>>b[i]; ll ans=0; for(int j=0;j<n;j++) { int ex; cin>>ex; ans=ans+a[b[ex]]; } cout<<ans; }
/* Vineeth Kada */ #include<bits/stdc++.h> using namespace std; #define int long long typedef unsigned long long ull; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; #define F first #define S second #define PB push_back #define rep(i,a,b) for(int i=a; i<=b; i++) #define rrep(i,a,b) for(int i=a; i>=b; i--) #define inf(dtype) numeric_limits<dtype>::max() int Solve(){ int n; cin >> n; int a[n+1], b[n+1], c[n+1]; rep(i, 1, n) cin>>a[i]; rep(i, 1, n) cin>>b[i]; rep(i, 1, n) cin>>c[i]; map<int, int> hm; rep(i, 1, n) hm[c[i]]++; int ans[n+1]; rep(i, 1, n){ ans[i] = 0; } rep(i, 1, n){ ans[b[i]] += hm[i]; } int output = 0; rep(i, 1, n){ output += ans[a[i]]; } cout << output << endl; return 1; } int32_t main(){ ios_base::sync_with_stdio(false), cin.tie(NULL); int t = 1; // cin >> t; while(t--) Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; const int maxn = 1e5 + 233; int c[maxn], d[maxn]; int main(){ vector<int> a, b; scanf("%d", &n); ll res = 0; for(int i = 1; i <= n; i++){ scanf("%d", &c[i]); res += c[i]; } for(int i = 1; i <= n; i++){ scanf("%d", &d[i]); if(i & 1) a.push_back(d[i] - c[i]); else b.push_back(d[i] - c[i]); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); while(!a.empty() && a.back() + b.back() > 0){ res += a.back() + b.back(); a.pop_back(); b.pop_back(); } printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair < int , int > pii; #define mpr make_pair #define FS first #define SC second #define PB push_back template < typename T > void UMAX(T &a,T b){a=(a>b?a:b);} template < typename T > void UMIN(T &a,T b){a=(a<b?a:b);} LL readint(){ char c=getchar(); LL ret=0ll; bool neg=0; while(!(c>='0' && c<='9')){ if(c=='-') neg=1; c=getchar(); } while(c>='0' && c<='9'){ ret=ret*10ll+(LL)(c-'0'); c=getchar(); } return neg?-ret:ret; } void putint(LL v){ if(v<0){ putchar('-'); v=-v; } if(!v){ putchar('0'); return; } if(v>=10ll) putint(v/10ll); putchar('0'+(v%10ll)); } int n,f[100005][2]; int main(){ int i,j,k; n=readint(); for(i=0;i<n;++i) f[i][i&1]=readint(); for(i=0;i<n;++i) f[i][!(i&1)]=readint(); LL sum=0; priority_queue < int > pq; for(i=0;i<n;++i){ sum+=f[i][0]; pq.push(f[i][1]-f[i][0]); } for(i=0;i<n/2;++i){ sum+=pq.top(); pq.pop(); } printf("%lld\n",sum); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int n,m,t; while(scanf("%d%d",&n,&m)!=EOF) { n+=m; if(n>=15&&m>=8)t=1; else if(n>=10&&m>=3)t=2; else if(n>=3)t=3; else t=4; printf("%d\n",t); } }
#include <iostream> using namespace std; int a,b,c,d,e; int main(void){ cin>>a>>b>>c>>d; if((a+b+c+d)%2==1){cout<<"No"<<endl;return 0;} e=(a+b+c+d)/2; if(a+b==e)cout<<"Yes"<<endl; else if(a+c==e)cout<<"Yes"<<endl; else if(a+d==e)cout<<"Yes"<<endl; else if(b+c==e)cout<<"Yes"<<endl; else if(b+d==e)cout<<"Yes"<<endl; else if(d+c==e)cout<<"Yes"<<endl; else if(a==e)cout<<"Yes"<<endl; else if(b==e)cout<<"Yes"<<endl; else if(c==e)cout<<"Yes"<<endl; else if(d==e)cout<<"Yes"<<endl; else cout<<"No"<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int N, M; cin >> N >> M; vector<array<int, 3>> A(M); for (int i = 0; i < M; i++) { cin >> A[i][0] >> A[i][1] >> A[i][2]; } vector<int64_t> dp(1<<N); dp[0] = 1; for (int i = 0; i < (1 << N); i++) { int n = 0; vector<int> p(N + 1); for (int j = 0; j < N; j++) { if (i & (1 << j)) { p[j + 1] += 1; n += 1; } p[j + 1] += p[j]; } for (int j = 0; j < N; j++) { if (i & (1 << j)) continue; int c = 1; for (int k = 0; k < M; k++) { if (A[k][0] <= n) continue; if (j + 1 <= A[k][1] && p[A[k][1]] >= A[k][2]) { c = 0; break; } } if (c) { dp[i | (1 << j)] += dp[i]; } } } cout << dp[(1<<N) - 1]; }
#include <bits/stdc++.h> #define rep(i, n) for (auto i = 0; i < n; ++i) #define ALL(a) a.begin(), a.end() using namespace std; using ll = long long int; const int MOD_NUM = 1e9 + 7; int main() { // Input int N, Q; string S; cin >> N >> S >> Q; vector<int> T(Q), A(Q), B(Q); rep(i, Q) cin >> T[i] >> A[i] >> B[i]; // Process string ans(S); int reverse = 0; rep(i, Q) { if (T[i] == 1) { int a = A[i] - 1; int b = B[i] - 1; if (reverse % 2 == 1) { a = (a + N) % (N * 2); b = (b + N) % (N * 2); } char tmp = ans[a]; ans[a] = ans[b]; ans[b] = tmp; } else reverse++; } if (reverse % 2 == 1) { string tmp1 = ans.substr(N); string tmp2 = ans.substr(0, N); ans = tmp1 + tmp2; } // Output cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll inf = (1ll << 63) - 1; array<ll, 3> egcd(ll a, ll b) { ll s0 = 1, t0 = 0, s1 = 0, t1 = 1; while (b) { ll d = a / b; tie(a, b) = make_pair(b, a % b); tie(s0, s1) = make_pair(s1, s0 - d * s1); tie(t0, t1) = make_pair(t1, t0 - d * t1); } return {a, s0, t0}; } pair<ll, ll> crt(const vector<ll> &r, const vector<ll> &m) { ll r0 = 0, m0 = 1; for (int i = 0; i < int(r.size()); i++) { ll r1 = r[i] % m[i], m1 = m[i]; auto [g, x, y] = egcd(m0, m1); if ((r1 - r0) % g) return {0, 0}; r0 = (r0 + m0 * ((x * (r1 - r0) / g) % (m1 / g))) % (m0 * m1 / g); m0 *= m1 / g; if (r0 < 0) r0 += m0; } return {r0, m0}; } int main() { int t; cin >> t; while (t--) { ll x, y, p, q; cin >> x >> y >> p >> q; ll ans = inf; for (ll i = x; i < x + y; i++) { for (ll j = p; j < p + q; j++) { auto [r, n] = crt({i, j}, {2 * (x + y), p + q}); if (n) ans = min(r, ans); } } if (ans == inf) cout << "infinity"; else cout << ans; cout << '\n'; } }
#include <iostream> #include <sstream> #include <algorithm> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <memory> #include <complex> #include <numeric> #include <cstdio> #include <iomanip> #include <random> #define REP(i,m,n) for(int i=int(m);i<int(n);i++) #define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i) #define EACH(i,c) for (auto &(i): c) #define all(c) begin(c),end(c) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort(begin(c),end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) //#define int long long #ifdef LOCAL #define DEBUG(s) cout << (s) << endl #define dump(x) cerr << #x << " = " << (x) << endl #define BR cout << endl; #else #define DEBUG(s) do{}while(0) #define dump(x) do{}while(0) #define BR #endif using namespace std; using UI = unsigned int; using UL = unsigned long; using LL = long long; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VS = vector<string>; using PII = pair<int,int>; using VP = vector<PII>; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } constexpr int MOD = 1'000'000'007; template<class T> inline T sqr(T x) {return x*x;} void solve() { LL n,a,b; cin >> n >> a >> b; if (a + b > n) { cout << 0 << endl; return; } LL ap = n - a + 1, bp = n - b + 1, cp = n - a - b + 1; LL x4 = (cp * (cp + 1) / 2) % MOD; LL x3 = (x4 * 2) % MOD; LL x2 = (ap * bp - x3) % MOD; LL x1 = sqr(x2) % MOD; LL ans = ((sqr(ap)%MOD) * (sqr(bp)%MOD) - x1) % MOD; if (ans < 0) ans += MOD; cout << ans << endl; } signed main() { int t; cin >> t; REP(i,0,t) solve(); return 0; }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define st first #define nd second #define pii pair<int,int> #define piii pair<int,pii> #define sp " " #define nl "\n" #define all(x) x.begin(),x.end() #define fastio() ios_base::sync_with_stdio(0);cin.tie(0); #define ll long long #define int ll using namespace std; const int N = 2e5+5; const int INF = 1e9+5; const int mod = 1e9+7; int fac[11],cnto,cnts,ans; string s; void factorial(){ fac[0]=1; for(int i=1;i<=10;i++) fac[i]=fac[i-1]*i; } int comb(int n,int r){ return fac[n]/(fac[r]*fac[n-r]); } signed main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif fastio() factorial(); cin >> s; for(int i=0;i<10;i++){ cnto+=(s[i]=='o'); cnts+=(s[i]=='?'); } if(cnto>4){ cout << 0 << nl; return 0; } for(int i=0;i<=cnts;i++){ if(cnto+i==0) continue; else if(cnto+i==1){ ans+=comb(cnts,i); } else if(cnto+i==2){ ans+=comb(cnts,i)*(2*4+6); } else if(cnto+i==3){ ans+=comb(cnts,i)*3*12; } else if(cnto+i==4){ ans+=comb(cnts,i)*24; } else break; } cout << ans << nl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #ifndef _LOCAL int main() { string s; cin >> s; ll ans = 0ll; vector<bool> must(10, false); vector<bool> must_not(10, false); for(int i = 0; i < 10; i++) { if(s[i] == 'o') { must[i] = true; } else if(s[i] == 'x') { must_not[i] = true; } } for(int a = 0; a < 10; a++) { for(int b = 0; b < 10; b++) { for(int c = 0; c < 10; c++) { for(int d = 0; d < 10; d++) { vector<bool> p_must(10, false); p_must[a] = p_must[b] = p_must[c] = p_must[d] = true; bool ok = true; for(int i = 0; i < 10; i++) { if(must[i] && !p_must[i]) { ok = false; } if(must_not[i] && (a == i || b == i || c == i || d == i)) { ok = false; } } if(ok) { ans++; } } } } } cout << ans << endl; return 0; } #endif
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(long long i=(long long)(a);i<(long long)(b);i++) #define ALL(a) (a).begin(),(a).end() typedef long long ll; typedef pair<ll,ll> P; ll a[5151],t[5155],c[5151],g[5151]; int main(){ ll n; string s; cin>>n>>s; rep(i,0,n){ if(s[i]=='A')a[i+1]++; a[i+1]+=a[i]; if(s[i]=='T')t[i+1]++; t[i+1]+=t[i]; if(s[i]=='C')c[i+1]++; c[i+1]+=c[i]; if(s[i]=='G')g[i+1]++; g[i+1]+=g[i]; } ll an=0; rep(i,0,n){ rep(j,i+1,n+1){ if(a[j]-a[i]==t[j]-t[i]&&c[j]-c[i]==g[j]-g[i])an++; } } cout<<an<<endl; }
#pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define REP(n) FOR(O, 1, (n)) #define pb push_back #define f first #define s second typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<piii> viii; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MAXN = 5000100, MAXK = 23; const ll MOD = 1e9+7; //const ll MOD = 998244353; const ll INF = 1e16; const ld PI = asin(1) * 2; void setIO () { FAST_IO; } void setIO (string s) { setIO(); freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } int n, k; int x; vi adj[MAXN]; int dp[MAXN]; int cnt[MAXN]; void dfs (int v, int p = -1) { cnt[v] = 0; dp[v] = 0; int mn = 0, mx = 0; for (int u : adj[v]) { if (u == p) continue; dfs (u, v); cnt[v] += cnt[u]; mn = min(mn, dp[u]+1); mx = max(mx, dp[u]+1); } //if (mx) if (mn+1 + mx <= 0) { dp[v] = mn; } else if (mx == x || v==1){ dp[v] = -x-1; cnt[v]++; } else { dp[v] = mx; } //cout << " v = " << v << " cnt = " << cnt[v] << " dp = " << dp[v] << endl; } int main() { setIO(); cin >> n >> k; REP(n-1) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } /* if (k == n) { cout << "0\n"; }*/ int le = 1, ri = n; while (le < ri) { FOR(i, 1, n) cnt[i] = dp[i] = 0; x = (le+ri)/2; // cout << " x = " << x << endl; dfs (1); if (cnt[1] <= k) ri = x; else le = x+1; } cout << le << "\n"; return 0; } /* 10 2 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 ans: 2 */
#include <iostream> #include <vector> #include <set> #include <map> #include <tuple> template <class E> struct FenwickTree { int _n; std::vector<E> data; FenwickTree(int n) : _n(n), data(n) { } void add(int p, E x) { ++p; while (p <= _n) { data[p - 1] += x; p += p & -p; } } E sum(int r) const { E s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } E sum(int l, int r) const { return sum(r) - sum(l); } }; long long proc( std::vector<int> &cur, int x, int y, FenwickTree<int> &ftcnt0, FenwickTree<long long> &ftsum0, const FenwickTree<int> &ftcnt1, const FenwickTree<long long> &ftsum1, const std::vector<int> &arr) { int v = cur[x]; long long dem = (long long)ftcnt1.sum(0, v) * arr[v]; long long cre = (long long)ftcnt1.sum(0, y) * arr[y]; long long adj = (long long)ftsum1.sum(y, v); ftsum0.add(v, -arr[v]); ftcnt0.add(v, -1); cur[x] = y; ftsum0.add(y, arr[y]); ftcnt0.add(y, 1); return cre + adj - dem; } int main(int argc, char **argv) { int n, m, q; std::cin >> n >> m >> q; std::vector<std::tuple<int, int, int>> txy; std::set<int> s = { 0 }; for (int i = 0; i < q; i++) { int t, x, y; std::cin >> t >> x >> y; txy.push_back(std::make_tuple(t, x - 1, y)); s.insert(y); } std::vector<int> arr(s.begin(), s.end()); std::map<int, int> map; int ll = s.size(); for (int i = 0; i < ll; i++) map.emplace(arr[i], i); FenwickTree<long long> ftsuma(ll); FenwickTree<long long> ftsumb(ll); FenwickTree<int> ftcnta(ll); FenwickTree<int> ftcntb(ll); std::vector<int> cura(n); std::vector<int> curb(m); ftcnta.add(0, n); ftcntb.add(0, m); long long sum = 0; for (const auto&e : txy) { int cy = map[std::get<2>(e)]; sum += (std::get<0>(e) == 1) ? proc(cura, std::get<1>(e), cy, ftcnta, ftsuma, ftcntb, ftsumb, arr) : proc(curb, std::get<1>(e), cy, ftcntb, ftsumb, ftcnta, ftsuma, arr); std::cout << sum << std::endl; } return 0; }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++) #define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define vector1d(type,name,...) vector<type>name(__VA_ARGS__) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__))) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=(ll)1<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} //各桁の数の和を求める template <typename T> T digitsum(T n){ string s=to_string(n); T sum=0; T d=1; for(T e=0;e<s.length();e++){ sum+=(n/d)%10;d*=10; } return sum; } int main(){ INT(a,b); a=digitsum(a); b=digitsum(b); out(max(a,b)); }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) #define sz(x) int(x.size()) // #define show(x) {for(auto i:x){cout << i << " ";} cout << endl;} #define isin(x,l,r) ((l)<=(x) && (x)<(r)) using namespace std; using ll=long long; using pii=pair<int,int>; using pll=pair<ll,ll>; template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return true;} else return false;} template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return true;} else return false;} void show(vector<int> A) { for (int a : A) { if (a==0) cout << 'A'; else cout << 'B'; } cout << '\n'; } int main() { int N; cin >> N; int M = 1 << N; cout << M-1 << '\n'; for (int i = 1; i < M; i++) { vector<int> A(M); for (int j = 0; j < M; j++) { int p = __builtin_popcountll(i&j); if (p&1) A[j] = 1; } show(A); } return 0; }
///In the name of GOD //#pragma GCC optimize("O2") #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll MXN = 5000 + 10; ll k, n; ll ans; vector<string> ANS; string t, one; void Print(){ cout << ans << '\n'; for(auto X : ANS){ cout << X << '\n'; } exit(0); } string Rev(const string& s){ t = s; reverse(t.begin(), t.end()); return t; } string Not(const string& s){ t = ""; for(int i = 0; i < int(s.size()); i ++){ if(s[i] == 'A') t += "B"; else t += "A"; } return t; } int main(){ ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); cin >> k, n = (1LL << k); ans = 1; ANS.push_back("AB"); one = "A"; for(int i = 1; i < k; i ++){ for(int j = 0; j < ans; j ++){ ANS.push_back(ANS[j] + Not(ANS[j])); ANS[j] = ANS[j] + ANS[j]; } one = one + one; ANS.push_back(one + Not(one)); ans = int(ANS.size()); } Print(); return 0; } /*! HE'S AN INSTIGATOR, ENEMY ELIMINATOR, AND WHEN HE KNOCKS YOU BETTER YOU BETTER LET HIM IN. */ //! N.N
#define _USE_MATH_DEFINES #include <iomanip> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <deque> #include <queue> #include <set> #include <functional> #include <cmath> #include <set> #include <map> #include <limits> //マクロ #define REP(i,n) for(int i=0; i<(int)(n); ++i) #define REPS(i,n) for(int i=1; i<=(int)(n); ++i) #define RREP(i,n) for(int i=(int)(n)-1; i>=0; --i) #define RREPS(i,n) for(int i=(int)(n); i>0; --i) #define FOR(i,s,e) for(int i=(int)(s); i<(int)(e); ++i) #define RFOR(i,s,e) for(int i=(int)(e)-1; i>=(int)(s); --i) #define All(a) (a).begin(),(a).end() #define RAll(a) (a).rbegin(),(a).rend() #define dump(x) cerr << #x << " = " << (x) << endl; //定数 #define INF 1e9 #define INFL 1e18 #define MOD 998244353 typedef long long ll; typedef unsigned long long ull; typedef long double ld; using namespace std; using vi = vector<int>; using vii = vector<vi>; using vs = vector<string>; using pii = pair<int, int>; using Graph = vector<vector<int>>; //とりあえず書いておく変数 int dx[4] = { 1,0,-1,0 }; int dy[4] = { 0,1,0,-1 }; //今回の変数 const ld eps = 1.0e-10; // 許容される誤差 //使用する関数 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; } bool comp(pair<int,pair<int,int>> a, pair<int, pair<int, int>> b){ return a.first <= b.first; } ld mysqrtl(ld x) { ld a = sqrt((double)x); // 近似値 do { a = (a + x / a) / 2.0L; } while (fabsl(x - a*a) > eps); return a; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); int n; //* cin >> n; vector<int> x = vector<int>(n); REP(i, n) { cin >> x[i]; } /*/ n = 100000; vector<int> x = vector<int>(n, 100000); //*/ ll mh = 0; REP(i, n) { mh += abs(x[i]); } cout << mh << endl; ld tmpEu = 0; REP(i, n) { tmpEu += x[i] * (ll)x[i]; } cout << sqrt(tmpEu) << endl; int ch = 0; REP(i, n) { chmax(ch, abs(x[i])); } cout << ch << endl; }
#include <iostream> #include <cstdlib> #include <cmath> #include <iomanip> #include <limits> using namespace std; int main() { long long N; cin >> N; long long x[N]; for (int i = 0; i < N; i++) cin >> x[i]; long long ans1 = 0; for (int i = 0; i < N; i++) ans1 += abs(x[i]); double ans2 = 0; for (int i = 0; i < N; i++) ans2 += x[i] * x[i]; ans2 = sqrt(ans2); long long ans3 = 0; for (int i = 0; i < N; i++) ans3 = max(ans3, abs(x[i])); cout << ans1 << endl; cout << fixed << setprecision(13) << ans2 << endl; cout << ans3 << endl; }
#include <functional> #include <ios> #include <numeric> #include <ostream> #include <string> #include <valarray> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") // #pragma GCC target("popcnt") #include <cmath> #include <cstdio> #include <cstdlib> #include <algorithm> #include<bits/stdc++.h> #include <cstdarg> #include <cstdint> #include <cstring> #include <iomanip> #include <math.h> #include <vector> // @author : sam000013 #define int long long #define ll long long #define PI 3.14159265 #define pb(x) push_back(x) #define all(v) v.begin(), v.end() #define dbg(x) cout<<#x<<" "<<x<<endl; #define bye cout<<endl;return #define uni 1.000000 #define ld long double using namespace std; const int mod = 1000000007; const int secondMod = 998244353; const int hashMod = 29996224275833; template <typename T> void show(vector<T> x) { for (auto e : x){ cout<<e<<" "; } cout<<endl; } template <typename T1> void showPair(vector<pair<T1, T1>> x) { for(auto e:x){ cout<<e.first<<" "<<e.second<<endl; } } template <typename T2> void showSetPair(set<T2> x) { for(auto e:x){ cout<<e.first<<" "<<e.second<<"\n"; } // cout<<endl; } template <typename T3> void showMultiSet(multiset<T3> x) { for(auto e:x){ cout<<e<<" "; } cout<<endl; } /* */ void solve(){ int n; cin>>n; set<int> s; for(int i=0; i<n; i++){ int x;cin>>x; s.insert(x); } int ans = 1, prev = 0; for(auto x:s){ ans = (ans * (x-prev+1))%mod; prev = x; } cout<<ans;bye; } signed main(){ ios::sync_with_stdio(false);cin.tie(0); int t = 1; // cin >> t; for(int i=1; i<=t; i++){ solve(); } return 0; }
#include<bits/stdc++.h> #include<algorithm> #include<cmath> #include<climits> using namespace std; typedef long long int lli; typedef vector<int> vi; typedef vector<long long int> vlli; typedef pair<int,int> pii; typedef pair<long long int,long long int> plli; typedef vector< vi > vvi ; typedef vector< vlli > vvlli ; #define fi(i,a,b) for(int i=a;i<=b;i++) #define flli(i,a,b) for(long long int i=a;i<=b;i++) #define bi(i,a,b) for(int i=a;i>=b;i--) #define blli(i,a,b) for(long long int i=a;i>=b;i--) #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define all(x) x.begin(),x.end() #define sz(x) x.size() #define pi 2*acos(0.0) #define pb push_back #define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++) #define present(v,num) (v.find(num)!=v.end()) #define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end()) #define pq priority_queue #define mp make_pair const int inf=INT_MAX; const lli INF =LLONG_MAX; const lli mod = 1e9+7; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast; lli n;cin>>n; vlli a(n);flli(i,0,n-1)cin>>a[i]; sort(all(a)); lli val=(a[0]+1)%mod; flli(i,1,n-1) { val=(val*(a[i]-a[i-1]+1))%mod; } cout<<val; cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = a; i >= b; i--) const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; typedef long long LL; typedef pair <int, int> P; const int N = 2e5 + 5; int n, a[N], b[N], c[N]; LL ans, res; int main() { cin>>n; rep (i, 1, n) scanf("%d", &a[i]), ans += a[i]; rep (i, 1, n) scanf("%d", &b[i]), b[i] -= a[i]; rep (i, 1, n) { if (i&1) b[(i + 1)>>1] = b[i]; else c[i>>1] = b[i]; } sort(b + 1, b + n/2 + 1); sort(c + 1, c + n/2 + 1); res = ans; per (i, n/2, 1) res += b[i] + c[i], ans = max(ans, res); printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define lb lower_bound #define ub upper_bound #define bs binary_search #define F first #define S second #define pll pair<ll, ll> #define all(v) (v).begin(), (v).end() #define decimal(n) cout << fixed << setprecision(n) #define mod 1000000007 // #define mod 998244353 #define pi 3.14159265358979323846 ll ncr(ll n, ll r) { ll p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; ll m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t = 1, tc; //cin >> t; for (tc = 1; tc <= t; tc++) { ll l; cin >> l; cout << ncr(l - 1, 11); } return 0; }
// Problem: B - ARC Wrecker // Contest: AtCoder - AtCoder Regular Contest 117 // URL: https://atcoder.jp/contests/arc117/tasks/arc117_b // Memory Limit: 1024 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef long long ll; #define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define mp make_pair #define pb push_back #define ff first #define ss second #define vi vector<ll> #define vll vector<ll> #define all(x) (x).begin() , (x).end() mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define rnd(x, y) uniform_ll_distribution<ll>(x, y)(rng) void dbg(){ cerr << endl; } template<typename Head , typename... Tail> void dbg(Head h , Tail... t){ cerr << h << " "; dbg(t...); } #ifdef EMBI_DEBUG #define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", dbg(__VA_ARGS__) #else #define debug(...) #endif const ll max_n = 1e5 + 9; const ll mod = 1e9 + 7; const ll inf = 1e9; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; ll power(ll a , ll b) { ll prod = 1; while(b) { if(b&1) prod = (prod*a)%mod; a = (a*a)%mod; b >>= 1; } return prod; } void solve(){ ll n; cin >> n; vector<ll> a(n); for(ll i = 0 ; i < n ; i++) { cin >> a[i]; } sort(all(a)); ll prod = (a[0] + 1); for(ll i = 1 ; i < n ; i++) { prod *= (a[i] - a[i-1] + 1); prod %= mod; } cout << prod << "\n"; } signed main(){ ll t = 1; // cin >> t; for(ll i = 1 ; i <= t ; i++){ solve(); } }
#include<bits/stdc++.h> using namespace std; #define INF 1234567890 #define ll long long #define MOD 1000000007 int T; int N; int main() { ios::sync_with_stdio(0); cin.tie(0); cin.exceptions(ios::badbit | ios::failbit); cin >> N; vector<int> A(N); for(int i=0; i<N; i++) cin >> A[i]; A.push_back(0); sort(A.begin(), A.end()); A.erase(unique(A.begin(), A.end()), A.end()); N = A.size(); ll res = 1; for(int i=1; i<N; i++) { res *= A[i]-A[i-1]+1; res %= MOD; } cout << res << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n, m; cin >> n >> m; vector<pair<ll,ll>> As(n); vector<ll> A(n); vector<vector<ll>> g(n, vector<ll>(0)); for(ll i=0; i<n; i++){ ll a; cin >> a; As.at(i).first = a; As.at(i).second = i; A.at(i) = a; } for(ll i=0; i<m; i++){ ll x, y; cin >> x >> y; g.at(x-1).push_back(y-1); } sort(As.begin(), As.end()); vector<ll> P(n, 0); ll ans = -10000000000; queue<ll> que; for(int i=0;i<n;i++){ ll now = As.at(i).second; que.push(now); while (!que.empty()) { ll v = que.front(); que.pop(); if(now!=v) { ans = max(ans, A.at(v) - A.at(now)); P.at(v) = 1; } for (ll nv : g.at(v)) { if (P.at(nv) == 1) continue; que.push(nv); } } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<LL,LL> pii; typedef pair<LL,pii> ppi; typedef pair<pii,pii> ppp; #define FOR(i, n) for(int i = 1; i<=n; i++) #define F0R(i, n) for(int i = 0; i<n; i++) #define mp make_pair #define pb push_back #define f first #define s second template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; } template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; F0R(i, v.size()) {if (i) cout << ", "; cout << v[i];} return cout << "]";} //var const LL INF = 1e17; LL t, n, m, a[200005], ans = -INF; vector<LL> e[200005], e2[200005]; LL v[200005], minbuy[200005], maxsell[200005], v2[200005]; void dfs(LL g){ if(v[g]) return; v[g] = 1; for(auto u : e[g]){ if(!v[u]) dfs(u); maxsell[g] = max(maxsell[g], maxsell[u]); ans = max(ans, maxsell[u] - minbuy[g]); } //cout << g << '\n'; } void dfs2(LL g){ if(v2[g]) return; v2[g] = 1; for(auto u : e2[g]){ if(!v2[u]) dfs2(u); minbuy[g] = min(minbuy[g], minbuy[u]); } } void solve(){ cin >> n >> m; FOR(i, n) cin >> a[i]; LL t1, t2; FOR(i, m){ cin >> t1 >> t2; e[t1].pb(t2); e2[t2].pb(t1); } FOR(i, n){ maxsell[i] = a[i]; minbuy[i] = a[i]; } for(int i = n; i>=1; i--){ if(v2[i]) continue; dfs2(i); } FOR(i, n){ if(v[i]) continue; dfs(i); } cout << ans << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); t = 1; //cin >> t; FOR(i, t) solve(); cout.flush(); return 0; }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; #ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep2(i, s, n) for (int i = s; i < (int)(n); ++i) #define Clear(a) a = decltype(a)() #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define vec vector typedef long long ll; typedef pair<ll,ll> P; typedef string::const_iterator state; //構文解析 const double PI=acos(-1); const ll big=998244353; //const ll big=1000000007LL; const ll INF=1e18; const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; ll max(ll x,ll y){ if(x>y)return x; else return y; } ll min(ll x,ll y){ if(x<y)return x; else return y; } ll expm(ll x,ll y,ll mod=big){ if(y==0)return 1;//0^0=1 if(x==1||x==0)return x; if(y%2==1)return (expm(x,y-1,mod)*x)%mod; ll t=expm(x,y/2,mod); return (t*t)%mod; } ll exp(ll x,ll y){ if(y==0)return 1;//0^0=1 if(x==1||y==0)return x; if(y%2==1)return exp(x,y-1)*x; ll t=exp(x,y/2); return t*t; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(10); /*--------------------------------*/ ll a,b,c;cin >> a >> b >> c; cout << a*(a+1)%big * b %big * (b+1) %big * c%big * (c+1) %big * expm(8,big-2,big)%big << endl; }
//clear adj and visited vector declared globally after each test case //check for long long overflow //while adding and subs check if mod becomes -ve //while using an integer directly in a builtin function add ll //Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod; //Dont keep array name as size or any other key word //Incase of close mle change language to c++17 or c++14 #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define int long long #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10); #define pb push_back #define mod 1000000007ll //998244353ll #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 print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";} typedef std::numeric_limits< double > dbl; using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; const long long N=200005, INF=2000000000000000000; lld pi=3.1415926535897932; int lcm(int a, int b) { int g=__gcd(a, b); return a/g*b; } 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; } int32_t main() { IOS; int n, m; cin>>n>>m; //cout<<n%m; //cout<<power(10, n, m*m)%m<<" "<<power(10, n, m)<<"\n"; cout<<(((power(10, n, m*m)-power(10, n, m))/m)+m)%m; }
#include <bits/stdc++.h> #include <cassert> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; /* struct Edge { int to; int weight; Edge(int t, int w) : to(t), weight(w) {} }; using Graph = vector<vector<Edge>>; */ using Graph = vector<vector<int>>; const long long INF = 1LL << 60; const int INT_INF = 1000000000; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; // int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1}; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<ll> a(N); for (auto &it : a) cin >> it; ll ans = a[0]; for (int i = 1; i < N; i++) { ans = gcd(ans, a[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <string> #include <cstring> #include <algorithm> #include <math.h> #include <map> #include <bitset> #include <cmath> #include <set> #include <utility> #include <sstream> #define INF 1000000000000 #define MOD 1000000007 //10^9+7:合同式の法 using namespace std; typedef long long ll; /* 0のアスキー "48" */ //マクロ #define REP(i,n) for(ll i=0;i<(ll)(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++) #define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--) #define ALL(x) x.begin(),x.end() #define chmax(x,y) (x = max(x,y)) #define chmin(x,y) (x = min(x,y)) int main(){ ll N; cin >> N; vector<ll> A(N); for(ll i = 0;i < N;i++) cin >> A.at(i); ll min = MOD; for(ll i = 0;i < N;i++){ if(min > A.at(i)) min = A.at(i); } if(min == 1){ cout << min << endl; }else{ while(1){ bool zero_fla = true; ll hako = MOD; for(ll i = 0;i < N;i++){ A.at(i) %= min; if(A.at(i) != 0 and hako > A.at(i)){ zero_fla = false; hako = A.at(i); } } if(min > hako) min = hako; if(zero_fla or min == 1) break; } cout << min << endl; } return 0; }
#include <iostream> #include <cstring> using namespace std; typedef long long ll; const int N=20,M=210; int l; ll dp[N][M]; ll dfs(int p,int s){ if(s>l) return 0; if(p==13){ if(s==l) return 1; return 0; } if(dp[p][s]!=-1) return dp[p][s]; ll ans=0; ans+=dfs(p,s+1); ans+=dfs(p+1,s+1); return dp[p][s]=ans; } int main(){ cin>>l; memset(dp,-1,sizeof(dp)); cout<<dfs(1,0)<<endl; }
#include "bits/stdc++.h" using namespace std; #define endl "\n" using ll = long long; const int MOD = 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; } void solve() { ll num; cin >> num; cout << binomialCoeff(num - 1, 11) << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } }
#include<bits/stdc++.h> using namespace std; #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define int long long int #define fi first #define se second #define pub push_back #define pi pair<int,int> #define all(v) (v).begin(), (v).end() #define rep(i, l, r) for(int i=(int)(l);i<(int)(r);i++) #define repd(i, l, r) for (int i=(int)(l);i>=(int)(r);i--) #define clrg(i, l, r) for(int i=(int)(l);i<(int)(r);i++)vis[i]=0,v[i].clear(); int power(int x, unsigned int y){int res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;} int powermod(int x, unsigned int y, int p){int res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;} #define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<< endl;}} #define clr(a,x) memset(a,x,sizeof(a)) #define rr(v) for(auto &val:v) #define print(v) for (const auto itr : v){cout<<itr<<' ';}cout<<"\n"; #define ln length() #define sz size() #define mod 1000000007 #define elif else if int32_t main(){ fastIO int m,h; cin>>m>>h; if(h%m==0)cout<<"Yes\n"; else cout<<"No\n"; return 0; }
#include<iostream> #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<pll, null_type,less<>, rb_tree_tag,tree_order_statistics_node_update> #define ll long long #define FOR(i,a,b) for(ll i=a;i<=b;i++) #define FORR(i,a,b) for(ll i=a;i>=b;i--) #define vl vector<ll> #define ld long double #define vld vector<ld> #define vvl vector<vector<ll>> #define vvld vector<vector<ld>> #define pll pair<ll,ll> #define vpll vector<pll> #define ff first #define ss second #define pb push_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define d1(x) cout<<(x)<<endl #define d2(x,y) cout<<(x)<<" "<<(y)<<endl #define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl #define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl #define PI 3.14159265358979323846264338327950288419716939937510 #define fix(f,n) fixed<<setprecision(n)<<f #define all(x) x.begin(),x.end() #define rev(p) reverse(p.begin(),p.end()); #define mset(a,val) memset(a,val,sizeof(a)); #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define popcount(x) __builtin_popcountll(x) #define sz(x) ((ll)x.size()) const ll M = 1000000007; const ll MM = 998244353; #define endl "\n" ll begtime = clock(); #define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " template<typename T, typename F> void chmax( T &a, F b) { if (b > a)a = b; } template<typename T, typename F> void chmin( T &a, F b) { if (b < a)a = b; } const ll N = 2e5+5; void OJ() { #ifndef ONLINE_JUDGE freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif } int main() { IOS; OJ(); ll a,b,c; cin>>a>>b>>c; if(a*a+b*b<c*c)cout<<"Yes"; else cout<<"No"; return 0; }
#include <unordered_map> #include <unordered_set> #include <algorithm> #include <iostream> #include <iostream> #include <fstream> #include <numeric> #include <cstring> #include <cassert> #include <string> #include <vector> #include <queue> #include <deque> #include <cmath> #include <set> #include <map> #include <functional> #include <bitset> #include <iomanip> #include <stack> #include <list> #include <cstdint> #include <chrono> #include <hash_map> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; #define lp(i,a,n) for(ll i=a;i<n;i++) #define lp2(i,n,a) for(ll i=a-1;i>=n;i--) #define all(arr,n) arr,arr+n #define allv(v) (v).begin(),(v).end() #define rallv(v) (v).rbegin(),(v).rend() #define m_p make_pair #define ll long long #define pii pair<ll,ll> #define vi vector<int> #define vll vector<ll> #define vii vector<pii> #define sz(x) (int)x.size() #define pb push_back #define endl '\n' #define Endl '\n' #define f first #define s second #define mem(dp,n) memset(dp,n,sizeof dp) #define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> template<typename T>void max_self(T &a,T b){a=max(a,b);} template<typename T>void min_self(T &a,T b){a=min(a,b);} int dx[] = { 1 , 0 ,-1 , 0 ,-1 ,-1 , 1 , 1 }; int dy[] = { 0 , 1 , 0 ,-1 ,-1 , 1 ,-1 , 1 }; int KnightX[] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int KnightY[] = { 1, 2, 2, 1, -1, -2, -2, -1 }; void fast(){ std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } vector<string> vec_splitter(string s) { s += ','; vector<string> res; while(!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out( vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx, __attribute__ ((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) const ll mxN=5e5,oo=0x3f3f3f3f,MOD=998244353; const long double PI = acos(-1),eps=1e-9; void solve(){ int a,b; cin>>a>>b; int sa=0,sb=0; while(a) sa+=(a%10),a/=10; while(b) sb+=(b%10),b/=10; cout<<max(sa,sb); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast(); int t=1; //cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; //indexed set is using "int" here--------- use ll ... // typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef long long int ll; typedef long double ld; #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define rep(g, i, n) for (ll g = i; g < n; g++) #define rev(g, n, i) for (ll g = n - 1; g >= i; g--) #define all(v) v.begin(), v.end() #define pb push_back #define mxe(v) *max_element(v.begin(), v.end()) #define mne(v) *min_element(v.begin(), v.end()) #define ve vector #define rem 1000000007 #define PI 3.141592653589793238462643383279502 ll power(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } void solve_dp(ve<ve<ll>> &dp, ll r, ll c, ve<ve<ll>> &A, ve<ve<ll>> &B) { dp[1][1] = 0; rep(i, 1, r + 1) { rep(j, 1, c + 1) { if (i == 1) { dp[i][j] = min(dp[i][j], dp[i][j - 1] + A[i][j - 1]); continue; } if (j == 1) { dp[i][j] = min(dp[i][j], dp[i - 1][j] + B[i - 1][j]); continue; } dp[i][j] = min({dp[i][j], dp[i][j - 1] + A[i][j - 1], dp[i - 1][j] + B[i - 1][j]}); } } } int main() { FAST; // freopen("input1.txt", "r", stdin); // freopen("output.txt", "w", stdout); /*ll tests; cin>>tests; rep (gg,0,tests) {}*/ ll r, c; cin >> r >> c; ve<ve<ll>> A(r + 1, ve<ll>(c + 1)), B(r + 1, ve<ll>(c + 1)); rep(i, 1, r + 1) { rep(j, 1, c) { cin >> A[i][j]; } } rep(i, 1, r) { rep(j, 1, c + 1) { cin >> B[i][j]; } } ve<ve<ll>> dp(r + 1, ve<ll>(c + 1, 1e18)); solve_dp(dp, r, c, A, B); rep(z, 0, 1001) { rep(i, 1, r + 1) { rev (j, c+1, 2) { dp[i][j - 1] = min(dp[i][j - 1], dp[i][j] + A[i][j - 1]); } } ve<ll> mn(c + 1, 1e18); rev(i, r + 1, 1) { rep(j, 1, c + 1) { mn[j] = min(mn[j], dp[i][j] + i + 1); } rep(j, 1, c + 1) { dp[i][j] = min(dp[i][j], mn[j] - i); } } solve_dp(dp, r, c, A, B); } cout << dp[r][c] << "\n"; }
//#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using db = double; using ld = long double; template<typename T> using V = vector<T>; template<typename T> using VV = vector<vector<T>>; template<typename T> using PQ = priority_queue<T>; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(),(v).end() #define siz(v) (ll)(v).size() #define rep(i,a,n) for(ll i=a;i<(ll)(n);++i) #define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i) #define ENDL '\n' typedef pair<int,int> Pi; typedef pair<ll,ll> PL; constexpr ll mod = 1000000007; // 998244353; constexpr ll INF = 1000000099; constexpr ll LINF = (ll)(1e18 +99); const ld PI = acos((ld)-1); constexpr ll dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;} template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;} template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;} inline void Yes() { cout << "Yes" << ENDL; } inline void No() { cout << "No" << ENDL; } inline void YES() { cout << "YES" << ENDL; } inline void NO() { cout << "NO" << ENDL; } template<typename T,typename Y> inline T mpow(T a, Y n) { T res = 1; for(;n;n>>=1) { if (n & 1) res = res * a; a = a * a; } return res; } template <typename T> vector<T> finddivisor(T x) { //整数xの約数(xを含む) vector<T> divisor; for(T i = 1; (i * i) <= x; i++) { if(x % i == 0) { divisor.push_back(i); if(i * i != x) { divisor.push_back(x / i);} } } sort(divisor.begin(), divisor.end()); return divisor; } template <typename T> V<T> prefix_sum(const V<T>& v) { int n = v.size(); V<T> ret(n + 1); rep(i, 0, n) ret[i + 1] = ret[i] + v[i]; return ret; } template<typename T> istream& operator >> (istream& is, vector<T>& vec){ for(auto&& x: vec) is >> x; return is; } template<typename T,typename Y> ostream& operator<<(ostream& os,const pair<T,Y>& p){ return os<<"{"<<p.fs<<","<<p.sc<<"}"; } template<typename T> ostream& operator<<(ostream& os,const V<T>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<typename ...Args> void debug(Args&... args){ for(auto const& x:{args...}){ cerr<<x<<' '; } cerr<<ENDL; } signed main(){ cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false); cout<<fixed<<setprecision(20); ll n;cin>>n; V<ll> a(n),b(n);cin>>a>>b; V<ll> c(n,0); ll amx=0; rep(i,0,n){ chmax(amx,a[i]); if(i)chmax(c[i],c[i-1]); chmax(c[i],amx*b[i]); } rep(i,0,n)cout<<c[i]<<ENDL; } //! ( . _ . ) ! //CHECK overflow,vector_size,what to output? //any other simpler approach? //list all conditions, try mathematical and graphic observation
#include <iostream> #include <string> #include <vector> #include <stdio.h> #include <math.h> using namespace std; int main(void){ int N; int maxA = 1; scanf("%d\n", &N); vector<long long> a(N), b(N), c(N); for(int i=0; i<N; i++){ scanf("%lld", &a[i]); } for(int i=0; i<N; i++){ scanf("%lld", &b[i]); } maxA = a[0]; c[0] = maxA*b[0]; for(int i=1; i<N; i++){ if(maxA<a[i]){ maxA = a[i]; } c[i] = max(c[i-1],maxA*b[i]); } for(int i=0; i<N; i++){ printf("%lld\n",c[i]); } }
// $%D%$.$%M%$.$%Y%$ // $%h%$:$%m%$:$%s%$ #pragma GCC optimize("O3") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define IOS ios::sync_with_stdio(false); cin.tie(0) #define ll long long #define ld long double #define rep(i,a,n) for (int i=(int)(a);i<(int)(n);i++) #define per(i,a,n) for (int i=(int)(n-1);i>=(int)(a);i--) #define mp(a, b) make_pair(a, b) #define pii pair<int, int> #define pll pair<ll, ll> #define pil pair<int, ll> #define pli pair<ll, int> #define fi first #define se second const int MOD = 1e9+7; // 998244353; // = (119<<23)+1 const int MX = 2e5+5; const ll INF = 1e18; const ld PI = 4*atan((ld)1); const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int cmp(pii a, pii b) { swap(a.first, a.second); swap(b.first, b.second); return a < b; } int main() { int n, m; cin >> n >> m; vector<ll> a(n), dp(n, 1e18); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<pll> e(m); for (int i = 0; i < m; ++i) { cin >> e[i].first >> e[i].second; --e[i].first, --e[i].second; } ll res = -1e18; sort(e.begin(), e.end(), cmp); for (auto x : e) { dp[x.second] = min(dp[x.second], dp[x.first]); dp[x.second] = min(dp[x.second], a[x.first]); res = max(res, a[x.second] - dp[x.second]); } cout << res << '\n'; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; std::vector<int> topological_sort(const std::vector<std::vector<int>> &graph) { int n = graph.size(); std::vector<int> deg(n, 0); for (int i = 0; i < n; ++i) { for (int e : graph[i]) ++deg[e]; } std::queue<int> que; for (int i = 0; i < n; ++i) { if (deg[i] == 0) que.emplace(i); } std::vector<int> res; while (!que.empty()) { int ver = que.front(); que.pop(); res.emplace_back(ver); for (int e : graph[ver]) { if (--deg[e] == 0) que.emplace(e); } } return res.size() == n ? res : std::vector<int>(); } int main() { int n, m; cin >> n >> m; vector<int> a(n); REP(i, n) cin >> a[i]; vector<vector<int>> graph(n); while (m--) { int x, y; cin >> x >> y; --x; --y; graph[x].emplace_back(y); } vector<int> ts = topological_sort(graph); int ans = -INF; vector<int> dp(n, -INF); for (int i = n - 1; i >= 0; --i) { int town = ts[i]; for (int e : graph[town]) chmax(dp[town], max(dp[e], a[e])); chmax(ans, dp[town] - a[town]); } cout << ans << '\n'; return 0; }
/* #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofast","inline") */ #include<bits/stdc++.h> #define ALL(x) (x).begin(),(x).end() #define ll long long #define db long double #define ull unsigned long long #define pii_ pair<int,int> #define mp_ make_pair #define pb push_back #define fi first #define se second #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) #define show1(a) cout<<#a<<" = "<<a<<endl #define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl using namespace std; const ll INF = 1LL<<60; const int inf = 1<<30; const int maxn = 2e5+5; inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} int flag[maxn]; db f1[maxn],f2[maxn],suf1[maxn],suf2[maxn]; int main() { //fastio(); int n,m,k; scanf("%d%d%d",&n,&m,&k); rep(i,1,k){ int x; scanf("%d",&x); flag[x] = 1; } per(i,n-1,0){ if(flag[i]){ f1[i] = 0;f2[i] = 1; }else{ f1[i] = 1.0 + (suf1[i+1] - suf1[i+m+1]) / m; f2[i] = (suf2[i+1] - suf2[i+m+1]) / m; }//show2(f1[i],f2[i]); suf1[i] = f1[i] + suf1[i+1]; suf2[i] = f2[i] + suf2[i+1]; } db ans = f1[0] / (1 - f2[0]); //printf("%.4Lf\n",ans); if(ans > 1e13) cout << -1 << endl; else cout << fixed << setprecision(4) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < b; ++i) int debug = 0; const int N = 2e5, K = 18, inf = 1e9; int n, m, k, ans, c[K], dp[K][1 << K], dist[K][K], d[N]; vector <int> g[N]; bool belong(int x, int mask) { return (mask >> x) & 1; } void bfs(int id) { int start = c[id]; fill(d + 1, d + n + 1, -1); queue <int> q; q.push(start); d[start] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int u : g[v]) { if (d[u] == -1) { d[u] = d[v] + 1; q.push(u); } } } rep(j, 0, k) { dist[id][j] = d[c[j]]; } } void calcDist() { rep(i, 0, k) { bfs(i); } } main() { cin >> n >> m; rep(i, 0, m) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } cin >> k; rep(i, 0, k) fill(dp[i], dp[i] + (1 << k), inf); rep(i, 0, k) { cin >> c[i]; dp[i][1 << i] = 1; } calcDist(); rep(mask, 0, (1 << k)) { if ((int)__builtin_popcount(mask) > 1) { rep(v, 0, k) if (belong(v, mask)) { rep(u, 0, k) if (belong(u, mask) && u != v && dist[u][v] != -1) { dp[v][mask] = min(dp[v][mask], dp[u][mask ^ (1 << v)] + dist[u][v]); } } } } ans = inf; rep(i, 0, k) { ans = min(ans, dp[i][(1 << k) - 1]); } if (ans == inf) ans = -1; cout << ans << '\n'; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair<int,int>; using vv = vector<vector<int>>; const int INF = (int)1e9; const LL LINF = (LL)1e18; const int query = 1000; const int grid_size = 30; const int grid_total = grid_size * grid_size; const int initial_length = 4000; string path_ex(int si, int sj, int ti, int tj){ int di = ti - si, dj = tj - sj; string s; if(di >= 0) rep(k,di) s.push_back('D'); else rep(k,-di) s.push_back('U'); if(dj >= 0) rep(k,dj) s.push_back('R'); else rep(k,-dj) s.push_back('L'); return s; } vector<vector<int>> G; int cost[grid_total][grid_total]; int update_number[grid_total][grid_total]; void length_update(int from, int to, int ave_length){ int now_length = cost[from][to]; int num = update_number[from][to] + 1; int new_length = (now_length * num + ave_length) / (num + 1); cost[from][to] = new_length, cost[to][from] = new_length; update_number[from][to]++, update_number[to][from]++; } string dijkstra(int st, int ed){ int N = G.size(); vector<int> dist(N, INF), par(N, -1); dist[st] = 0; priority_queue<P,vector<P>,greater<P>> q; q.push({dist[st], st}); while(!q.empty()){ P p = q.top(); q.pop(); int d = p.first, n = p.second; if(dist[n] < d) continue; for(auto v : G[n]){ int c = cost[n][v]; if(dist[v] <= d + c) continue; dist[v] = d + c; q.push({dist[v], v}); par[v] = n; } } string path; int now = ed; while(now != st){ char c; if(par[now] + 1 == now) c = 'R'; else if(par[now] - 1 == now) c = 'L'; else if(par[now] + grid_size == now) c = 'D'; else c = 'U'; path.push_back(c); now = par[now]; } reverse(path.begin(), path.end()); return path; } int main(){ //FILE *outputfile; //outputfile = freopen("test.txt", "w", stdout); G.resize(grid_total); rep(i,grid_size){ rep(j,grid_size) cost[i][j] = INF; } rep(i,grid_size){ rep(j,grid_size-1){ int l = i * grid_size + j, r = i * grid_size + j + 1; assert(l < grid_total and r < grid_total); G[l].emplace_back(r); G[r].emplace_back(l); cost[l][r] = initial_length, cost[r][l] = initial_length; } } rep(j,grid_size){ rep(i,grid_size-1){ int u = i * grid_size + j, d = (i + 1) * grid_size + j; assert(u < grid_total and d < grid_total); G[u].emplace_back(d); G[d].emplace_back(u); cost[u][d] = initial_length, cost[d][u] = initial_length; } } rep(q,query){ int si, sj, ti, tj; cin >> si >> sj >> ti >> tj; int st = si * grid_size + sj, ed = ti * grid_size + tj; assert(st < grid_total and ed < grid_total); string path = dijkstra(st, ed); cout << path << endl; int dist_result; cin >> dist_result; int path_size = path.size(); random_device rnd; mt19937 mt(rnd()); uniform_real_distribution<> rand_e(0.9, 1.1); //uniform_int_distribution<> rand_length(1, 1000); int sum_result = 0; vector<int> path_vertices(path_size + 1); path_vertices[0] = st; rep(k,path_size){ int now = path_vertices[k]; if(path[k] == 'R') now++; else if(path[k] == 'L') now--; else if(path[k] == 'D') now += grid_size; else now -= grid_size; path_vertices[k+1] = now; } rep(k,path_size){ int from = path_vertices[k], to = path_vertices[k+1]; int ave_length = dist_result / path_size; length_update(from, to, ave_length); sum_result += cost[from][to]; } /* int dif = dist_result - sum_result; rep(k,path_size){ int pre = path_vertices[k], now = path_vertices[k+1]; cost[pre][now] += dif / path_size; cost[now][pre] += dif / path_size; } */ } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { double a, b, c, d; cin >> a >> c >> b >> d; cout << fixed << setprecision(10); cout << c * (b-a) / (c+d) + a << endl; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less using namespace __gnu_pbds; using namespace std; #define int long long #define endl "\n" #define deb(x) cout<<#x<<" "<<x<<endl; #define sc(ar,n) for(int pen=0;pen<n;pen++){ cin>>ar[pen];} #define pr(ar,n) for(int pen=0;pen<n;pen++){ cout<<ar[pen]<<" ";} cout<<endl; #define fr(i,x,n) for(int i=x;i<n;i++) #define frt(it,vb) for(auto it=vb.begin();it!=vb.end();it++) #define mem(ar,x) memset(ar,x,sizeof(ar)); #define pb push_back #define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mod 1000000007 #define rt return 0; #define ct continue; #define MAX 1000000000000000000 #define MAX1 1000000000 #define CLK clock_t clk = clock();//Start of main #define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> inline int solve() { int n,m; cin>>n>>m; map <int , vector <int> > mp; fr(i,0,m){ int x,y; cin>>x>>y; mp[x].push_back(y); } set <int> st; st.insert(n); for(auto it : mp){ set <int> a,b; for(auto ut : it.second){ int x = ut; if(st.find(x) != st.end() && st.find(x-1) == st.end() && st.find(x+1) == st.end()){ b.insert(x); } if(st.find(x) == st.end() && (st.find(x-1) != st.end() || st.find(x+1) != st.end()) ){ a.insert(x); } } for(auto ut : a){ st.insert(ut); } for(auto ut : b){ st.erase(ut); } } cout << st.size() << endl; rt; } signed main() { // fastio; int t; int test_cases=1; if(test_cases==0) cin>>t; else t=1; while(t--){ solve(); } }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i) #define irreps(i, m, n) for (long long i = ((long long)(n)-1); i > (long long)(m); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v+n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cinline(n) getline(cin,n); #define replace_all(s, b, a) replace(s.begin(),s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) (long long)(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll,ll,ll>>; using vb = vector<bool>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const ll INF = 1e9+10; const ll MOD = 1e9+7; const ll LINF = 1e18; signed main() { cin.tie( 0 ); ios::sync_with_stdio( false ); ll a,b,c; cin>>a>>b>>c; if(c==0){ if(a>b) cout<<"Takahashi"<<endl; else cout<<"Aoki"<<endl; }else{ if(a>=b) cout<<"Takahashi"<<endl; else cout<<"Aoki"<<endl; } }
#include<ctime> #include<cstdio> #include<cctype> using namespace std; const int N=1e3+7; int read() { char c; int x=0,f=1; while(!isdigit(c=getchar())) f-=2*(c=='-'); while (isdigit(c)){ x=x*10+(c-48)*f; c=getchar(); } return x; } int n,m,a[N],b[N]; int main() { #ifndef ONLINE_JUDGE freopen("B.in","r",stdin); freopen("B.out","w",stdout); #endif clock_t t1=clock(); //-------- n=read(); m=read(); for(int i=1;i<=n;++i) a[read()]=1; for(int i=1;i<=m;++i) b[read()]=1; for(int i=1;i<=1000;++i){ if(a[i]^b[i]) printf("%d ",i); } //-------- clock_t t2=clock(); fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC); return 0; }
#include<bits/stdc++.h> using namespace std; using u64 = uint_least64_t; using s64 = int_least64_t; using ll = uint_least64_t; int main(){ int n,m; cin >> n >> m; vector<int> a(n+m),b(m); for (int i = 0; i < n; i++){ cin >> a[i]; } for (int i = 0; i < m; i++){ cin >> a[n+i]; } sort(a.begin(), a.end()); int nn = n + m; for (int i = 1; i < nn; i++){ if (a[i-1] == a[i]){ a.erase(a.begin()+i-1); a.erase(a.begin()+i-1); i--; nn-=2; } } for (auto i = a.begin(); i != a.end(); i++){ cout << *i << " "; } cout << endl; return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cctype> #define N 201000 template<typename T> inline void read(T &x) { x = 0; char c = getchar(); bool flag = false; while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); } while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); if (flag) x = -x; } using namespace std; int n; int a[N], b[N], h[N], htot; long long ans; inline void sol(int L, int R) { if (L + 1 == R) return ; // printf("sol(%d, %d) : \n", L, R); int ptr = R; for (int i = L + 1; i < R; ++i) if (b[i] > a[i]) { ptr = i; break; } // printf("ptr = %d\n", ptr); int tol = a[ptr - 1]; for (int i = ptr - 1; i > L; --i) { if (b[i] < tol) { int p = upper_bound(a + L, a + R + 1, b[i]) - a - 1;//bug // printf("i = %d, p = %d\n", i, p); ans += i - p; tol = b[i]; } } int tor = a[ptr]; for (int i = ptr; i < R; ++i) { if (b[i] > tor) { int p = lower_bound(a + L, a + R + 1, b[i]) - a;//bug bug // printf("i = %d, p = %d\n", i, p); ans += p - i; tor = b[i]; } } } int main() { read(n); int L; read(L); ++n; ++n; a[0] = b[0] = 0; a[n - 1] = b[n - 1] = L + 2 - n;//0 ... L + 1 for (int i = 1; i < n - 1; ++i) read(a[i]), a[i] -= i; for (int i = 1; i < n - 1; ++i) read(b[i]), b[i] -= i; // for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts(""); // for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts(""); for (int i = 0; i < n; ++i) h[++htot] = a[i]; htot = unique(h + 1, h + 1 + htot) - h - 1; h[htot + 1] = -12341; for (int i = 0; i < n; ++i) a[i] = lower_bound(h + 1, h + 1 + htot, a[i]) - h; for (int i = 0; i < n; ++i) { int v = lower_bound(h + 1, h + 1 + htot, b[i]) - h; if (h[v] != b[i]) return puts("-1"), 0; b[i] = v; } // for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts(""); // for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts(""); int lst = 0; for (int i = 1; i < n; ++i) if (a[i] == b[i]) { // printf("\ni = %d : \n", i); sol(lst, i); lst = i; // printf("ans = %lld\n", ans); } printf("%lld\n", ans); return 0; }
#include<bits/stdc++.h> #define int long long #define ld long double #define fi first #define se second #define vll vector<int> #define pii pair<int,int> #define pb push_back #define sz(v) (int)(v).size() #define inf (int)(1e18) #define md (int)(998244353) #define all(v) (v).begin(),(v).end() #define rep(i,a,b) for(int i=a;i<b;++i) using namespace std; const int M = 2e5 + 10; int ex(int a,int b=md-2){ int ans=1; while(b) { if(b&1) ans=(ans*a)%md; a=(a*a)%md,b/=2; } return ans; } inline int add(int a,int b){ a+=b; if(a>=md) a-=md; return a; } inline int sub(int a,int b){ a-=b; if(a<0) a+=md; return a; } inline int mul(int a,int b){ return (a*b)%md; } int f[M],iv[M]; int C(int n,int r){ if(n<r) return 0; return mul(f[n],mul(iv[r],iv[n-r])); } void init(){ f[0]=1; for(int i=1;i<M;++i) { f[i]=mul(i,f[i-1]); } iv[M-1]=ex(f[M-1]); for(int i=M-2;i>=0;--i) { iv[i]=mul(i+1,iv[i+1]); } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); init(); int n,m,k; cin>>n>>m>>k; if(n==1 or m==1) { cout<<ex(k,n+m-1); return 0; } // Bi>=max(A) int ans=0; rep(i,1,k+1) { int now=sub(ex(i,n),ex(i-1,n)); now=mul(now,ex(k-i+1,m)); ans=add(ans,now); } cout<<ans; return 0; }
#include <iostream> #include <string> using namespace std; int main() { int n,x; cin>>n>>x; string s; for(int i=0;i<n;i++){ cin>>s; if(s[i]=='o'){ x=x+1; } else{ if(x>0){ x=x-1; } } } cout<<x<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false);cin.tie(NULL); int n,x; string s; cin>>n>>x>>s; for(auto i=0;i<n;i++){ if(x<=0){ if(s[i]=='o'){ x++; }else continue; }else{ if(s[i]=='o')x++; else x--; } } cout<<x <<"\n"; }
#include<iostream> #include<cmath> using namespace std; int main(){ long long a[4]; for(int i=0;i<4;i++) cin>>a[i]; if(a[1]==a[3]&&a[2]==a[0]) cout<<"0"; else if(a[0]+a[1]==a[2]+a[3]) cout<<"1"; else if(a[0]-a[1]==a[2]-a[3]) cout<<"1"; else if(abs(a[0]-a[2])+abs(a[1]-a[3])<=3) cout<<"1"; else if((a[0]+a[1])%2==(a[3]+a[2])%2) cout<<"2"; else if(abs(a[0]-a[2])+abs(a[1]-a[3])<=6) cout<<"2"; else if( abs(a[0]-a[2]+1)==abs(a[1]-a[3])|| abs(a[0]-a[2]-1)==abs(a[1]-a[3])|| abs(a[0]-a[2])==abs(a[1]-a[3]+1)|| abs(a[0]-a[2])==abs(a[1]-a[3]-1)|| abs(a[0]-a[2]+3)==abs(a[1]-a[3])|| abs(a[0]-a[2]-3)==abs(a[1]-a[3])|| abs(a[0]-a[2])==abs(a[1]-a[3]+3)|| abs(a[0]-a[2])==abs(a[1]-a[3]-3)) cout<<"2"; else cout<<"3"; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; #define NOT_FOUND -1 #define rep(i, n) for(int i = 0; i < (int)(n); i++) int main() { int N; cin >> N; if(N % 2) { cout << "Black" << endl; } else { cout << "White" << endl; } return 0; } void swap(int *a, int *b) { *b = *a - *b; *a -= *b; *b += *a; } int sosu_judge(int N) { /*素数ならば1 素数でなければ0を返す */ int i, j; if(N == 2) return 1; if(N % 2 == 0) return 0; for(i = 3; i < N && N % i != 0; i += 2) ; if(i >= N) return 1; return 0; } int factorial(int N) { // Nの階乗を求める関数 int ans = 1; while(N--) ans *= (N + 1); return ans; } //クイックソートをする関数 void q_sort(int a[], int first, int last) { int i, j; int x, t; x = a[(first + last) / 2]; i = first; j = last; while(1) { while(a[i] < x) i++; while(x < a[j]) j--; if(i >= j) break; swap(&a[i], &a[j]); i++; j--; } if(first < i - 1) q_sort(a, first, i - 1); if(last > j + 1) q_sort(a, j + 1, last); } int b_search(int target, int array[], int right, int left) { /*二分探索を行う関数である。 戻り値は探索したい値の格納されている位置である。 もし値が存在しない場合、NOT_FOUND(-1)を返す */ int mid; while(left < right) { mid = (right + left) / 2; if(array[mid] < target) left = mid + 1; else right = mid; } if(array[mid] == target) return mid; return NOT_FOUND; } unsigned long combination(int n, int r) { /*nCrの値を求める関数である。 値が巨大になる可能性があるため、戻り地はunsigned longである */ int i, j; unsigned long a[17]; if(n - r < r) r = n - r; if(r == 0) return 1; if(r == 1) return n; if(r > 17) return 0; for(i = 1; i < r; i++) a[i] = i + 2; for(i = 3; i <= n - r + 1; i++) { a[0] = i; for(j = 1; j < r; j++) a[j] += a[j - 1]; } return a[r - 1]; }
#include <bits/stdc++.h> #include <fstream> #define ll long long #define pi pair<int,int> #define pll pair<long,long> #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define vc vector<char> #define vll vector<long long> #define vpi vector<pi> #define vpll vector<pll> #define pb push_back #define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define all(v) v.begin(), v.end() #define pq(type) priority_queue<type> #define inp(n, mas) for(int i=0;i<n;i++)cin>>mas[i]; #define vb vector<bool> #define mp make_pair /////////////////////////////////////////// #define hello ivanmordvintsevgay /////////////////////////////////////////// using namespace std; const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; bool comp(string a, string b) { for (int i = 0; i < min(a.length(), b.length()); i++) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return (a.length() < b.length()); } int bin_poisk(const vector <string> &d, string z) { int l = 0; int r = d.size() - 1; while (l < r) { int m = (l + r ) / 2; if (comp(z, d[m])) r = m; else l = m + 1; } if (d[r] == z) return r; else return r; } int plus(int a, int b) { return (a + b) % 4; } void solve() { int m, h; cin >> m >> h; cout << ((h % m == 0) ? "Yes":"No"); } int main() { /*ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);*/ int t = 1; //cin >> t; while (t--) { solve(); cout << endl; } }
#include<bits/stdc++.h> #define io ios_base::sync_with_stdio(false); cin.tie(NULL) #define nl "\n" #define fr first #define sc second #define MP make_pair #define pb push_back #define VI vector<int> #define VII vector<ll> #define MAP map <ll,ll> #define PI pair < int , int > #define sz(v) (int)(v).size() #define all(v) v.begin(),v.end() #define mem(a,val) memset(a,val,sizeof(a)) #define precise(n) fixed(cout);cout<<setprecision(n) #define in(v) for(auto &it : v) cin >> it #define out(v) for(auto &it : v) cout << it << " "; cout << nl #define casee(v) cout << "Case " << CASE << ": " << v << nl #define des (res ? "Yes" : "No") typedef long long ll; typedef double db; const int N = (int)4e7; const int MOD = (int)1e9 + 7; const double pi = 2 * acos(0.0); const double eps = 1e-9; // 10^-9 = 0.0000000001 using namespace std; bool isEqual(db a, db b) { return abs(a - b) <= eps; } bool isGreater(db a, db b) { return a > b + eps; } bool isSmaller(db a, db b) { return a + eps < b; } void execution() { int m, h; cin >> m >> h; bool res = false; if (((h / m)*m) == h) res = true; cout << des << nl; } int main() { io; #ifdef CODE_MAFIA freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ///Let the showdown begin //ll t; //cin >> t; //while (t--) { execution(); //} ///Happily DONE return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; template<class T> struct graph{ struct edge{ int to; T cost; edge(int to, T cost) : to(to), cost(cost){} bool operator<(const edge& r)const { return cost < r.cost; } }; int V; vector< vector< edge > > adj; vector< T > dist; vector< int > prev; graph(int n = 0){ init(n); } void init(int size){ V = size; adj.assign(size, vector<edge>()); } void add_edge(int from, int to, T cost){ adj[from].push_back(edge(to, cost)); } void Dijkstra(int from, T inf){ using P = pair<T, int>; priority_queue< P, vector<P>, greater<P> > q; dist.assign(V, inf); prev.assign(V, -2); dist[from] = 0; prev[from] = -1; q.push(P(0, from)); while(!q.empty()){ P p = q.top(); q.pop(); int v = p.second; if(dist[v] < p.first) continue; for(edge e: adj[v]){ if(dist[v] + e.cost < dist[e.to]){ dist[e.to] = dist[v] + e.cost; prev[e.to] = v; q.push(P(dist[e.to], e.to)); } } } } void get_path(int to, vector<int>& path){ path.clear(); if(prev[to] == -2) return; while(prev[to] != -1){ path.push_back(to); to = prev[to]; } path.push_back(to); reverse(path.begin(), path.end()); } void print_path(int to){ vector< int > path; get_path(to, path); cout << "path: " << endl; for(int i: path) cout << i << " ( " << dist[i] << " )" << endl; } }; int main(void){ int n, m; cin >> n >> m; vector< vector< int > > a(n, vector< int >(m)); vector< vector< int > > b(n, vector< int >(m)); for(int i = 0; i < n; i++){ for(int j = 0; j < m-1; j++){ cin >> a[i][j]; } } for(int i = 0; i < n-1; i++){ for(int j = 0; j < m; j++){ cin >> b[i][j]; } } graph< int > g(n*m); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ int id = i * m + j; if(j+1 < m) g.add_edge(id, i*m + j+1, a[i][j]); if(j > 0) g.add_edge(id, i*m + j-1, a[i][j-1]); if(i+1 < n) g.add_edge(id, (i+1)*m + j, b[i][j]); for(int k = 0; k < i; k++) g.add_edge(id, k*m + j, i-k+1); } } g.Dijkstra(0, 1e9); cout << g.dist[n*m-1] << endl; }
#include <iostream> #include <vector> #include <queue> using namespace std; typedef long long ll; ll d[20][200010],dp[1<<20][20]; vector<int> G[100010]; queue<pair<int,int>> que; void bfs(int s,int t){ que.push({0,s}); while(que.size()){ pair<int,int> p = que.front(); que.pop(); if(d[t][p.second]!=-1) continue; d[t][p.second] = p.first; for(int v:G[p.second]) que.push({p.first + 1,v}); } } int a[100010],b[100010],c[200]; int main(){ int i,j,l,n,m; cin >> n >> m; for(i=0;i<m;i++){ cin >> a[i] >> b[i]; a[i]--; b[i]--; G[a[i]].push_back(b[i]); G[b[i]].push_back(a[i]); } int k; cin >> k; for(i=0;i<k;i++){ cin >> c[i]; c[i]--; for(j=0;j<n;j++) d[i][j] = -1; bfs(c[i],i); } /*for(i=0;i<k;i++){ for(j=0;j<n;j++) cout << d[i][j] << " "; cout << endl; }*/ ll inf = 100000000000; for(i=0;i<(1<<k);i++){ for(j=0;j<k;j++) dp[i][j] = inf; } for(i=0;i<k;i++) dp[1<<i][i] = 1; for(i=0;i<(1<<k);i++){ for(j=0;j<k;j++){ if(i>>j&1){ for(l=0;l<k;l++){ if(l==j) continue; if(i>>l&1 && d[l][c[j]]!=-1){ // cout << i << " " << j << " " << l << " " << dp[i][j] << " " << dp[i^(1<<j)][l] << " " << d[l][c[j]] << endl; dp[i][j] = min(dp[i][j],dp[i^(1<<j)][l] + d[l][c[j]]); } } } } } /*for(i=0;i<(1<<k);i++){ for(j=0;j<k;j++) cout << dp[i][j] << " "; cout << endl; }*/ ll ans = inf; for(i=0;i<k;i++){ ans = min(ans,dp[(1<<k) - 1][i]); } if(ans==inf){ cout << -1 << endl; }else{ cout << ans << endl; } }
#include<bits/stdc++.h> #define ll long long using namespace std; const long long N=2e6+6; ll a[N],b[N]; long long n,m,q,ans; struct segment { long long siz[N<<2],sum[N],ls[N<<2],rs[N<<2],cnt=0,root=0; void change(ll l,ll r,ll &p,ll x,ll k) { if(!p) p=++cnt; if(l==r) { sum[p]+=k*x; siz[p]+=k; return; } ll mid=(l+r)>>1; if(x<=mid) change(l,mid,ls[p],x,k); else change(mid+1,r,rs[p],x,k); sum[p]=sum[ls[p]]+sum[rs[p]]; siz[p]=siz[ls[p]]+siz[rs[p]]; } ll query1(ll l,ll r,ll p,ll x) { if(!p) return 0; if(r<=x) return siz[p]; ll mid=(l+r)>>1; if(x<=mid) return query1(l,mid,ls[p],x); else return siz[ls[p]]+query1(mid+1,r,rs[p],x); } ll query2(ll l,ll r,ll p,ll x) { if(!p) return 0; if(l>x) return sum[p]; ll mid=(l+r)>>1; if(x<=mid) return sum[rs[p]]+query2(l,mid,ls[p],x); else return query2(mid+1,r,rs[p],x); } } A,B; signed main() { scanf("%lld %lld %lld",&n,&m,&q); A.change(0,1e8,A.root,0,n),B.change(0,1e8,B.root,0,m); for(long long i=1;i<=q;i++) { long long opt,t,x,y; cin>>opt>>x>>y; if(opt==1) { ans=ans-B.query1(0,1e8,B.root,a[x])*a[x]-B.query2(0,1e8,B.root,a[x]); A.change(0,1e8,A.root,a[x],-1); A.change(0,1e8,A.root,y,1); a[x]=y; ans+=B.query1(0,1e8,B.root,y)*y+B.query2(0,1e8,B.root,y); } else { ans=ans-A.query1(0,1e8,A.root,b[x])*b[x]-A.query2(0,1e8,A.root,b[x]); B.change(0,1e8,B.root,b[x],-1); B.change(0,1e8,B.root,y,1); b[x]=y; ans+=A.query1(0,1e8,A.root,y)*y+A.query2(0,1e8,A.root,y); } cout<<ans<<endl; } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; const int P=1e9+7,N=4e5+5; int A[N],B[N],b[N],T[N],X[N],Y[N],ans,len; struct BIT{ int t[N],sum; void add(int i,int k) { sum+=k; for(;i<=len;i+=(i&-i))t[i]+=k; } int qry(int i) { int res=0; for(;i>0;i-=(i&-i))res+=t[i]; return res; } }T1,T2,t1,t2; int read() { int x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } main() { int n=read(),m=read(),Q=read(); for(int i=1;i<=n;++i)A[i]=0; for(int i=1;i<=m;++i)B[i]=0; b[len=1]=0; for(int i=1;i<=Q;++i) { T[i]=read(); X[i]=read(); b[++len]=Y[i]=read(); } sort(b+1,b+len+1); len=unique(b+1,b+len+1)-b-1; for(int i=1;i<=n;++i) { A[i]=1; T1.add(A[i],b[A[i]]); t1.add(A[i],1); } for(int i=1;i<=m;++i) { B[i]=1; T2.add(B[i],b[B[i]]); t2.add(B[i],1); } for(int i=1;i<=Q;++i) { Y[i]=lower_bound(b+1,b+len+1,Y[i])-b; if(T[i]==1) { int p=X[i]; ans-=b[A[p]]*t2.qry(A[p]); ans-=(T2.sum-T2.qry(A[p])); t1.add(A[p],-1);T1.add(A[p],-b[A[p]]); A[p]=Y[i]; ans+=b[A[p]]*t2.qry(A[p]); ans+=(T2.sum-T2.qry(A[p])); t1.add(A[p],1);T1.add(A[p],b[A[p]]); } else { int p=X[i]; ans-=b[B[p]]*t1.qry(B[p]); ans-=(T1.sum-T1.qry(B[p])); t2.add(B[p],-1);T2.add(B[p],-b[B[p]]); B[p]=Y[i]; ans+=b[B[p]]*t1.qry(B[p]); ans+=(T1.sum-T1.qry(B[p])); t2.add(B[p],1);T2.add(B[p],b[B[p]]); } printf("%lld\n",ans); } return 0; }
#pragma GCC optimize ("Ofast,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") #include <bits/stdc++.h> using namespace std; constexpr int N = 2e6+7; constexpr int M = 998244353; #define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define int long long #define pii pair<int, int> #define fi first #define se second #define SZ(x) ((int)(x.size())) #ifdef int #define INF 0x3f3f3f3f3f3f3f3f #define INF2 (int)(0xcfcfcfcfcfcfcfcf) #else #define INF 0x3f3f3f3f #define INF2 0xcfcfcfcf #endif signed main() { fastio int a, b, c; cin >> a>> b >> c; cout << (a*(a+1)/2 % M) * (b*(b+1)/2 % M) % M * (c*(c+1)/2 % M) % M << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { ll N, K; ll mod; cin >> N >> K >> mod; ll MX = 500000; vector<vector<ll>> dp(N, vector<ll>(MX, 0)); dp[0][0] = 1; for (int i = 1; i < N; i++) { for (int j = 0; j < i; j++) { ll summand = 0; for (int k = j; k < MX; k += i) { summand += dp[i-1][k]; if (k >= (K+1)*i) summand += mod-dp[i-1][k-(K+1)*i]; while (summand >= mod) summand -= mod; dp[i][k] = summand; } } } for (int i = 1; i <= N; i++) { ll ans = 0; for (int j = 0; j < MX; j++) { ans += dp[i-1][j]*dp[N-i][j]%mod*(K+1)%mod; } ans -= 1; ans %= mod; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vector<int>>; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) void chmin(int &ret, const int &tg) { if (tg < ret) ret = tg; } void chmax(int &ret, const int &tg) { if (tg > ret) ret = tg; } const int INF = 1e9 + 10; const int M = 5; const int K = 3; int ans = 0; void dfs(int m, vvi &R, const vvi &P) { if (m < M) { rep(k, R.size()) { R[k].push_back(m); dfs(m + 1, R, P); R[k].pop_back(); } if (R.size() < K) { R.push_back({m}); dfs(m + 1, R, P); R.pop_back(); } } else { vi ret(R.size(), 0); rep(i, P.size()) { rep(j, ret.size()) { int r = INF; for (int k : R[j]) { chmin(r, P[i][k]); } chmax(ret[j], r); } } chmax(ans, *min_element(ret.begin(), ret.end())); } return; } int main() { int N; cin >> N; vvi P(N, vi(M)); rep(i, N) { rep(j, M) cin >> P[i][j]; } vvi R; dfs(0, R, P); cout << ans << endl; return 0; }
//IQ134高知能系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 = 0; int n; cin >> n; int a[n][5]; REP(i, n) REP(j, 5) cin >> a[i][j]; int he = 0, bo = 1000000001; int now; set<int> mp; while(he + 1 < bo){ now = (he + bo) / 2; //cout << he << "," << bo << ":" << now << endl; mp.clear(); REP(i, n){ int bit = 0; REP(j, 5){ bit = bit << 1; if(a[i][j] >= now) bit++; } mp.insert(bit); } bool ch = false; for(auto i = mp.begin(); i != mp.end(); i++){ for(auto j = mp.begin(); j != mp.end(); j++){ for(auto k = mp.begin(); k != mp.end(); k++){ if(((*i | *j)| *k) == (1 << 5) - 1){ //cout << bitset<5>(*i) << endl << bitset<5>(*j) << endl << bitset<5>(*k) << endl << endl; ch = true; } } } } if(ch) he = now; else bo = now; } cout << he << endl; }
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; long long a[200010],b[200010]; int n,m; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) scanf("%lld",&a[i]); for(int i=1;i<=m;++i) scanf("%lld",&b[i]); sort(a+1,a+1+n); sort(b+1,b+1+m); long long l=0,r=0; for(int i=n;i>=2;i-=2){ r+=a[i]-a[i-1]; } long long ans=1e18; for(int i=1;i<=n;i+=2){ int p=lower_bound(b+1,b+1+m,a[i])-b; if(p!=m+1) ans=min(ans,r+l+b[p]-a[i]); if(p!=1) ans=min(ans,r+l+a[i]-b[p-1]); l+=a[i+1]-a[i]; r-=a[i+2]-a[i+1]; } cout<<ans<<endl; return 0; }
#include <iostream> #include <string> #include <vector> #include <map> #include <set> #include <utility> #include <algorithm> #include <cmath> #include <climits> #include <iomanip> #include <queue> #include <stack> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; i++) int main() { int n, m; cin >> n >> m; vector<ll> h(n), w(m); rep(i, n) cin >> h[i]; rep(i, m) cin >> w[i]; sort(h.begin(), h.end()); sort(w.begin(), w.end()); vector<ll> s1(n/2+1, 0), s2(n/2+1, 0); rep(i, n/2) { s1[i+1] = s1[i] + abs(h[2*i]-h[2*i+1]); } rep(i, n/2) { s2[i+1] = s2[i] + abs(h[2*i+1]-h[2*i+2]); } /* rep(i, n/2+1) cout << s1[i] << " "; cout << endl; rep(i, n/2+1) cout << s2[i] << " "; cout << endl; */ ll ans = 1e18; rep(i, m) { auto ite = lower_bound(h.begin(), h.end(), w[i]); int j = ite - h.begin(); ll tmp; if (ite == h.end()) { tmp = w[i] - h[n-1] + s1[n/2]; } if (j % 2) { tmp = abs(w[i]-h[j-1]) + s1[(j-1)/2] - s1[0] + s2[n/2] - s2[(j-1)/2]; ans = min(tmp, ans); } else { if (j > 0) { tmp = s1[j/2] - s1[0] + abs(h[j]-w[i]) + s2[n/2] - s2[j/2]; ans = min(ans, tmp); } else { tmp = abs(h[0]-w[i]) + s2[n/2] - s2[0]; ans = min(tmp, ans); } } //cout << tmp << endl; } cout << ans << endl; return 0; } //小数点精度 //cout << fixed << std::setprecision(15) << y << endl;
#include <bitset> #include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <limits> #include <iomanip> #include <stack> #include <algorithm> #include <iostream> #include <string> #include <map> #include <queue> #include <set> #include <stdio.h> #include <math.h> using namespace std; typedef long long ll; typedef vector<vector<int>> vvi; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; #define mp(x, y) make_pair(x, y) #define rep(i, n) for(int i=0; i<(n); ++i) #define all(v) v.begin(), v.end() //sort( all(v) ) などと使える //nが素数かをO(√n)で判断 bool is_prime(int n) { bool res = true; if (n == 1) return false; else if (n == 2) return true; else { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = false; break; } } } return res; } int perm(int n, int r) { if (r == 1) return n; return n * perm(n - 1, r - 1); } int fact(int n) { if (n == 1) return 1; return fact(n - 1) * n; } int comb(int n, int r) { int a = min(n, r), b = max(n, r); return perm(a, b) / fact(a); } //aの逆元 a^(-1) ll inv_mod(ll a, ll mod) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } //n! ll fact_mod(ll n, ll mod) { ll f = 1; for (int i = 2; i <= n; i++) f = f * (i % mod) % mod; return f; } ll pow_mod(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; //右シフト(n = n >> 1) } return res; } //nCr(mod P) ll comb_mod(ll n, ll r, ll mod) { if (r > n - r) r = n - r; if (r == 0) return 1; ll a = 1; for (int i = 0; i < r; i++) a = a * ((n - i) % mod) % mod; ll b = pow_mod(fact_mod(r, mod), mod - 2, mod); return (a % mod) * (b % mod) % mod; } bool isContain(int v, int s) { return s & (1 << v); } //unionFind ランクとりバージョン /* 注意点 ・tree.isSame(a, b)とtreeに対してisSameを使う ・スペルはisSame */ struct unionFind { vector<int> par, rnk; unionFind(int N) : par(N), rnk(N, 0) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } void unite(int x, int y) { x = root(x), y = root(y); if (x == y) return; else if (rnk[x] < rnk[y]) par[x] = y; else { par[y] = x; if (rnk[x] == rnk[y]) rnk[y]++; } } bool isSame(int x, int y) { return root(x) == root(y); } }; /*クラスカル法 注意点! ・long long 型 ・関数kraskulの引数は(頂点数、辺集合)の順 ・unionFindはランクバージョンを使用すること */ struct edge { int s, t; long long cost; }; bool comp(const edge& left, const edge& right) { return left.cost < right.cost; } vector<edge> es; long long kraskul(int N, vector<edge> es) { long long res = 0; unionFind tree(N); //sortを忘れないように! //compを忘れないように! sort(es.begin(), es.end(), comp); for (int i = 0; i < es.size(); i++) { edge e = es[i]; if (!tree.isSame(e.s, e.t)) { res += e.cost; tree.unite(e.s, e.t); } } return res; } //max intはだいたい2e9 //max llはだいたい9e18 typedef pair<int, int> P; const int INF = 1e8; const long long LINF = 1e18; int main() { int N; cin >> N; vll A(N), B(N); rep(i, N) cin >> A[i] >> B[i]; ll sum = 0; rep(i, N) sum += (A[i] + B[i]) * (B[i] - A[i] + 1) / 2; cout << sum << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define endl "\n" #define pb push_back #define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define ll long long #define all(v) v.begin(),v.end() ll sum[1000000+10]={}; int main() { ll n, a , b, i, j; ll total=0,l,r; sum[0]=0; for(i=1;i<1000005;i++) { sum[i]=sum[i-1]+i; } cin>>n; while(n--) { cin>>l>>r; total=total+sum[r]-sum[l-1]; //cout<<total<<endl; } cout<<total<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvs = vector<vector<string>>; template<typename T> using vv = vector<vector<T>>; template<typename T> using vvv = vector<vector<vector<T>>>; template<typename T> using pq = priority_queue<T>; template<typename T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; const int DX[4] = {1, 0, -1, 0}; const int DY[4] = {0, 1, 0, -1}; #define _sel(_1, _2, x, ...) x #define repn(i, n) for (ll i = 0; i < (n); i++) #define reps(i, a, b) for (ll i = (a); i < (b); i++) #define rep(i, ...) _sel(__VA_ARGS__, reps, repn)(i, __VA_ARGS__) #define reppn(i, n) for (ll i = 1; i <= (n); i++) #define repps(i, a, b) for (ll i = (a); i <= (b); i++) #define repp(i, ...) _sel(__VA_ARGS__, repps, reppn)(i, __VA_ARGS__) #define rrepn(i, n) for (ll i = (n) - 1; i >= 0; i--) #define rreps(i, a, b) for (ll i = (b) - 1; i >= (a); i--) #define rrep(i, ...) _sel(__VA_ARGS__, rreps, rrepn)(i, __VA_ARGS__) #define rreppn(i, n) for (ll i = (n); i >= 1; i--) #define rrepps(i, a, b) for (ll i = (b); i >= (a); i--) #define rrepp(i, ...) _sel(__VA_ARGS__, rrepps, rreppn)(i, __VA_ARGS__) #define pb push_back #define fi first #define se second #define rng(v) (v).begin(), (v).end() #define rrng(v) (v).rbegin(), (v).rend() #define siz(x) int((x).size()) #define pow2(x) (1ll << (x)) #define bit(x, n) ((x) >> (n) & 1) #define prt(x) cout << (x) << endl #define dbg(x) dbgn(#x, x) template<typename T, typename U> inline bool umax(T& m, U x) { if (m < x) { m = x; return true; } return false; } template<typename T, typename U> inline bool umin(T& m, U x) { if (m > x) { m = x; return true; } return false; } template<typename T> inline void errv(T& v) { for (auto x: v) cerr << x << " "; cerr << endl; } inline void errv(vb& v) { for (auto x: v) cerr << (x ? 1 : 0) << " "; cerr << endl; } template<typename T> inline void dbgn(string n, T x) { cerr << n << ": " << x << endl; } template<typename T> inline void dbgn(string n, vector<T>& v) { cerr << n << ": "; errv(v); } template<typename T> inline void dbgn(string n, vv<T>& m) { cerr << n << ":" << endl; for (auto& v: m) errv(v); } template<ll mod> struct mint { ll x; mint(ll a = 0): x((a + mod) % mod) { } mint operator+(mint a) { return mint(*this) += a; } mint operator-(mint a) { return mint(*this) -= a; } mint operator*(mint a) { return mint(*this) *= a; } mint operator/(mint a) { return mint(*this) /= a; } mint& operator+=(mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(mint a) { (x *= a.x) %= mod; return *this; } mint& operator/=(mint a) { return *this *= a.inv(); } mint pow(ll t) { mint a = x, ret = 1; while (t) { if (t & 1) ret *= a; a *= a; t >>= 1; } return ret; } mint inv() { return pow(mod - 2); } friend istream& operator>>(istream& is, mint& a) { return is >> a.x; } friend ostream& operator<<(ostream& os, mint& a) { return os << a.x; } }; struct UnionFind { vi p; UnionFind(int n): p(n, -1) { } int root(int x) { if (p[x] < 0) return x; else return p[x] = root(p[x]); } int size(int x) { return -p[root(x)]; } void unite(int x, int y) { int rx = root(x), ry = root(y); if (rx == ry) return; if (p[rx] > p[ry]) swap(rx, ry); p[rx] += p[ry]; p[ry] = rx; } }; int main() { int N; cin >> N; if (N % 2 == 0) { prt("White"); } else { prt("Black"); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; const int mod =1e9+7; int n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; if(n%2) cout<<"Black\n"; else cout<<"White\n"; return 0; }
///Bismillahir Rahmanir Rahim #include "bits/stdc++.h" #define ll long long #define int ll #define fi first #define si second #define mp make_pair #define pb push_back #define pi pair<int,int> #define nd(a,b,c) mp(mp(a,b),c) #define clr(x) memset(x,0,sizeof(x)); #define f(i,l,r) for(int i=l;i<=r;i++) #define rf(i,r,l) for(int i=r;i>=l;i--) #define done(i) cout<<"done = "<<i<<endl; #define show(x,y) cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl; #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; const ll inf=2e18; const int mod=1e9+7; const int M=100005; void solve() { int a,b,c,d,z; cin>>a>>b>>c>>d; int hor=c*d-b; if(hor==0) { cout<<"-1\n";return ; } z=(a+hor-1)/hor; int cyan=z*b+a; int red=z*c*d; if(cyan>red) { cout<<"-1\n";return ; } cout<<z<<"\n"; } main() { fast int t=1; //cin>>t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; //素数判定 O√N bool is_prime(long long x) { assert(0<=x); if(x<2) { return false; } for(long long i=2;i*i<=x;i++) { if(x%i==0) { return false; } } return true; } template<class T> void chmin(T& a,T b){ if(a>b){ a=b; } } template<class T> void chmax(T& a,T b){ if(a<b){ a=b; } } //約数列挙 vector<ll> enumeration_divisor(ll n){ vector<ll> res_divisor; for(ll i=1;i<=sqrt(n);i++){ if(n%i==0){ res_divisor.push_back(i); if(i*i!=n){ res_divisor.push_back(n/i); } } } sort(res_divisor.begin(),res_divisor.end()); return res_divisor; } using Graph = vector<vector<ll>>; vector<bool> seen; void dfs(const Graph &G, ll v){ seen[v] = true; //vを訪問済みにする //vからいける各頂点next_vについて for(auto next_v : G[v]){ if(seen[next_v]==true) continue; //next_vが探索済みだったらスルー dfs(G, next_v); //再帰的に探索 } } template<typename T,typename U> T power(T a,U n){ T res=1; while(n){ res*=(n&1)?a:1; a*=a; n>>=1; } return res; } const int mod = 1000000007; class mint { long long x; public: mint(long long 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 { mint res(*this); return res+=a; } mint operator-(const mint& a) const { mint res(*this); return res-=a; } mint operator*(const mint& a) const { mint res(*this); return res*=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 { mint res(*this); return res/=a; } friend ostream& operator<<(ostream& os, const mint& m){ os << m.x; return os; } }; std::vector<int> Eratosthenes( const int N ) { std::vector<bool> is_prime( N + 1 ); for( int i = 0; i <= N; i++ ) { is_prime[ i ] = true; } std::vector<int> P; for( int i = 2; i <= N; i++ ) { if( is_prime[ i ] ) { for( int j = 2 * i; j <= N; j += i ) { is_prime[ j ] = false; } P.emplace_back( i ); } } return P; } bool flag[100010]; mint dp[100010]; int main(){ ll a,b,c,d; cin >> a >> b >> c >> d; if(a==0) cout << 0 << endl; else if(b>=c*d) cout << -1 << endl; else{ bool p=true; ll red=0,cnt=0; while(p==true){ a+=b; red+=c; cnt++; if(a<=red*d){ cout << cnt << endl; p=false; } } } }
#include"bits/stdc++.h" using namespace std; #define ri register int #define ll long long #define N 2010 int n,a[N],mn=0x3f3f3f3f; inline int get_gcd(int x,int y){ return !y?x:get_gcd(y,x%y); } unordered_map<int,int>vis; int main(){ ios::sync_with_stdio(false); cin>>n; for(ri i=1;i<=n;++i){ cin>>a[i];mn=min(mn,a[i]); } for(ri i=1;i<=n;++i){ for(ri j=1;j*j<=a[i]&&j<=mn;++j){ if(a[i]%j==0){ //cout<<a[i]<<" "<<j<<endl; vis[j]=get_gcd(a[i],vis[j]); if(a[i]/j<=mn) vis[a[i]/j]=get_gcd(vis[a[i]/j],a[i]); } } } int ans=0; for (auto x : vis) if (x.first == x.second) ans++; cout<<ans; return 0; }
#include <cstdio> #include <cmath> #include <iostream> #include <set> #include <algorithm> #include <vector> #include <map> #include <cassert> #include <string> #include <cstring> #include <queue> using namespace std; #define rep(i,a,b) for(int i = a; i < b; i++) #define S(x) scanf("%d",&x) #define S2(x,y) scanf("%d%d",&x,&y) #define P(x) printf("%d\n",x) #define all(v) v.begin(),v.end() #define FF first #define SS second #define pb push_back #define mp make_pair typedef long long int LL; typedef pair<int, int > pii; typedef vector<int > vi; map<int, vi > M; map<int, vi >::iterator it; int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a % b); } int main() { int n; S(n); int mn = 1000000000; rep(i,1,n+1) { int x; S(x); mn = min(mn, x); for(int j = 1; j * j <= x; j++) if(x % j == 0) { M[j].pb(x); if(j * j != x) M[x / j].pb(x); } } int ans = 0; for(it = M.begin(); it != M.end(); it++) { if(it->FF > mn) break; int g = 0; rep(i,0,it->SS.size()) { g = gcd(it->SS[i], g); } ans += g == it->FF; } P(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define END '\n' #define int long long #define pb push_back #define pii pair<int, int> #define ff first #define ss second #define bug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define loop(i, a, b) for(int i = (a); i < (b); i++) #define loopb(i, b, a) for(int i = (b); i > (a); --i) const int mod = 1e9+7; const int mod1 = 998244353; const int inf = 5e18; map<int,int> mp; void solve() { int num = 3,p = 1; while(num <= 1e18) { mp[num] = p; num *= 3; p++; } int n; cin>>n; num = 5; p = 1; while(num < n) { if(mp[n - num]) { cout<<mp[n - num]<<" "<<p; return; } num *= 5; p++; } cout<<-1; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); /*int t; cin>>t; while(t--)*/ solve(); //cerr<<END<<1.0*clock(); return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } #ifdef __LOCAL #define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n' #define debugArray(x, n) \ cerr << __LINE__ << ": " << #x << " = {"; \ for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \ cerr << ((hoge) ? "," : "") << x[hoge]; \ cerr << "}" << '\n' #define debugMatrix(x, h, w) \ cerr << __LINE__ << ": " << #x << " =\n"; \ for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \ cerr << ((hoge ? " {" : "{{")); \ for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \ cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \ cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \ } #else #define debug(x) (void(0)) #define debugArray(x, n) (void(0)) #define debugMatrix(x, h, w) (void(0)) #endif signed main() { cin.tie(0); ios::sync_with_stdio(0); long long N; cin >> N; long long X = 5; long long A = -1; long long B; for (B = 1; B <= 10000 && X < N; B++) { long long M = N - X; if (M % 3 == 0 && (M & 1)) { A = 0; while (M % 3 == 0) M /= 3, A++; if (M == 1) { break; } A = -1; } X *= 5; } if (A == -1) cout << -1 << '\n'; else cout << A << " " << B << '\n'; return 0; }
#include <bits/stdc++.h> #define ms(a,b) memset(a, b, sizeof(a)) #define rep(a,b,c) for(int a = (int)(b); a < (int)(c); a++) #define fi first #define se second #define pb push_back #define pf push_front #define m_p(a,b) make_pair(a, b) #define lson l,mid,o << 1 #define rson mid + 1,r,o << 1 | 1 #define ls o << 1 #define rs o << 1 | 1 #define inf 0x3f3f3f3f using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int,int> pii; inline bool isdigit(char& ch) { return ch >= '0' && ch <= '9'; } template<class T> void read(T &x) { x = 0; ll f = 1;char ch = getchar(); for (;!isdigit(ch);ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; x *= f; } template<class T> inline void write(T x) { if (x == 0) {putchar('0');return ;} if (x < 0) {putchar('-');x = -x;} int _stk[65],_top = 0; for (;x;x /= 10) _stk[++_top] = x % 10 + 48; for (;_top;_top--)putchar(_stk[_top]); } namespace Union { vector<int>fa; void Assign(int n) { fa.assign(n, -1); } int root(int x) { if (fa[x] < 0) return x; return fa[x] == x ? fa[x] : fa[x] = root(fa[x]); } bool same(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { fa[root(x)] = root(y); } } using namespace Union; int main () { int n; using P = pair<db, db>; using Edge = pair<db, pair<int, int>>; vector<P>pint; cin >> n; pint.resize(n); for (auto &[x, y] : pint) cin >> x >> y; // cout << pint.size(); Assign(n + 2); int S = n, T = n + 1; auto calc = [&](P a, P b) {return sqrt((a.fi-b.fi) * (a.fi-b.fi) + (a.se-b.se)*(a.se-b.se));}; vector<Edge>edge; for (int i = 0; i < pint.size(); ++i) { edge.push_back({100+pint[i].se, {S, i}}); edge.push_back({100-pint[i].se, {T, i}}); for (int j = i + 1; j < pint.size(); ++j) { edge.pb({calc(pint[i], pint[j]), {i, j}}); } } sort(edge.begin(), edge.end()); db res = 0; for (auto E : edge) { merge(E.se.fi, E.se.se); // cout << E.se.fi << " " << E.se.se << " " << E.fi<<endl; if (same(S, T)) { res = E.fi / 2.0; break; } } cout << fixed << setprecision(10) << res << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,s,t) for(int i = (int)(s); i < (int)(t); i++) int main(){ int n; cin >> n; vector<ll> a(n); rep(i,0,n) cin >> a[i]; vector<ll> aa(n); rep(i,0,n) aa[i] = a[i]*a[i]; vector<ll> aarec(n); aarec[0] = aa[0]; rep(i,1,n) aarec[i] = aarec[i-1] + aa[i]; vector<ll> arec(n); arec[0] = a[0]; rep(i,1,n) arec[i] = arec[i-1] + a[i]; ll ans = 0; rep(i,2,n+1) ans += (i - 1)*aa[i - 1]; rep(i,0,n-1) ans += aarec[i]; rep(i,2,n+1) ans -= 2*a[i - 1]*arec[i - 2]; cout << ans << endl; }
#include <iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; cout<<a-b+c; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define MAX 100 #define THOU 1000 #define FIVEMAX 100000 #define NINEMAX 1000000000 #define TWEMAX 1000000000000 #define rep(i,n) for (int i=0;i<n;i++) #define drep(i,n) for(int i=n-1;i>=0;i--) #define arep(i,a,n) for(int i=a;i<n;i++) #define darep(i,a,n) for(int i=n;i>=a;i--) template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} const ll INF=1LL<<60; #define yn {puts("Yes");}else{puts("No");} int main(){ int n,a,b; int ans; cin>>n>>a>>b; ans=n-a+b; cout<<ans<<endl; return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #define reg register typedef long long ll; const int N=1400; ll f[N]={0,1},n,x,y; ll g[N][2]; int st[N],opt[N],tot,top; ll R() { reg ll x=0; reg bool f=0; reg char ch=getchar(); for(;ch<48||ch>57;ch=getchar()) f|=(ch=='-'); for(;ch>=48&&ch<=57;ch=getchar()) x=x*10+ch-48; return f?-x:x; } int main() { n=R(); if(n==1) return puts("1\n1"),0; reg int p=1,i; reg bool now=0; while(1) { f[p+1]=f[p]+f[p-1]; if(f[p+1]>n) break; ++p; } for(i=p;i;--i) if(n>=f[i]) n-=f[i],st[++top]=i; /* now=0,x=0,y=1,plan to do x=x+y now=1,x=1,y=0,plan to do y=x+y */ x=0,y=1; for(i=1;i<=p+2;++i) { (i&1)?x=x+y:y=x+y; g[i][0]=x; } g[0][1]=x=1,y=0; for(i=1;i<=p+2;++i) { (i&1)?y=x+y:x=x+y; g[i][1]=x; } for(i=0;top;++i) { now^=1; if(g[i][now]==f[st[top]]) opt[++tot]=2-now,--top; opt[++tot]=4-now; } while(opt[tot]>=3) --tot; printf("%d\n",tot); x=y=0; for(i=tot;i;--i) { printf("%d\n",opt[i]); /* if(opt[i]==1) ++x; if(opt[i]==2) ++y; if(opt[i]==3) x=x+y; if(opt[i]==4) y=x+y; */ } // printf("end:%lld %lld\n",x,y); }
/** * Coded by : lucky_21 * --------Lokesh Singh **/ #include<bits/stdc++.h> using namespace std; #include<ext/pb_ds/tree_policy.hpp> #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; #define F first #define S second #define pb push_back #define lb lower_bound #define ub upper_bound #define vi vector<int> #define all(x) x.begin(),x.end() #define fix fixed<<setprecision(10) #define rep(i,a,b) for(int i=int(a);i<=int(b);i++) #define repb(i,b,a) for(int i=int(b);i>=int(a);i--) #define FastIO ios_base::sync_with_stdio(0),cin.tie(0) typedef double db; typedef long long ll; const int N=2e5+5; const int mod=1e9+7; void solve(){ ll n; cin>>n; int cnt=0; while(n%2==0) n/=2,cnt++; if(!cnt) cout<<"Odd\n"; else if(cnt==1) cout<<"Same\n"; else cout<<"Even\n"; } signed main(){ FastIO; int t; cin>>t; while(t--) solve(); return 0; }
#include "bits/stdc++.h" #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long int ll; typedef pair<ll, ll> P; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } void solve(){ int n; cin >> n; set<ll> st; vector<vector<ll>> a(n, vector<ll>(5)); rep(i,n){ rep(j,5){ cin >> a[i][j]; st.insert(a[i][j]); } } ll ans = 0; vector<ll> S; for(auto p : st) S.push_back(p); int ok = 0, ng = S.size(); while(ng - ok > 1){ int mid = (ok + ng) / 2; vector<vector<bool>> v(4, vector<bool>(1<<5, false)); v[0][0] = true; rep(i,n){ for(int j = 2; j >= 0; --j){ rep(k,(1<<5)){ if(!v[j][k]) continue; int nj = j+1; int nk = k; rep(l,5){ if(a[i][l] >= S[mid]){ nk |= (1<<l); } } v[nj][nk] = true; } } } if(v[3][(1<<5)-1]){ ok = mid; } else{ ng = mid; } } cout << S[ok] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define vi vector<int> #define ff first #define ss second #define pb push_back #define mp make_pair #define pi 3.141592653589793238 #define rep(i,a,b) for (int i = a; i <= b; i++) #define zip(i,a,b) for(int i=a;i<b;i++) #define rzip(i,a,b) for(int i=a;i>=b;i--) #define ll unsigned int #define test int t;cin>>t; while(t--) #define en '\n' typedef pair<int, int> pii; typedef pair<char, int> pci; typedef pair<char, char> pcc; typedef vector<pii> vii; typedef long double ld; #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define sz(x) (int)x.size() #define INF (1e18+5) #define inf (1e9+5) #define mod 1000000007 void __print(int x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} // You should only debug a pair of simple data types. For example, // the debug won't work if one of pair's elements is collection type // (std::vector, std::map, std::set...). template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // bool check_prime(int n) return whether a number is prime or not O(sqrt(N)); // int BE(int x,int n,int m) return x^n%m; O(logN); // void sieve() from number 1-1000001 saare prime store kar lega in bool is_prime[1000001] array // vector<int> z_array(string s) return vector which is Z-array of string s; // vector<int> lps(string s) return vector which is lps array of string s; // int power(int x,int n) return x^n; O(logN); // using_ordered_set (template for using ordered set , replace first 2 lines of this page with this code..); const int gf = 1e6 + 9; int a[gf][5]; int power(int x, int n) //function to calculate x raise to power n { int result = 1; while (n > 0) { if (n % 2 == 1) result = result * x; x = x * x; n = n / 2; } return result; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE if (fopen("input.txt", "r")) { freopen ("input.txt" , "r" , stdin); freopen ("output.txt" , "w" , stdout); } #endif //_______________________________-code starts-_______________________________________________ int n; cin >> n; //int a[n], b[n], c[n], d[n], e[n]; zip(i, 0, n) { cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4]; } int l = 1, r = 1e9; int ans = 0; while (l <= r) { int m = (l + r) / 2; //debug(l, r); set<int>s; zip(i, 0, n) { int var = 0; zip(k, 0, 5) { if (a[i][k] >= m) var += power(2, 4 - k); } s.insert(var); } // debug(s); int flag = 0; for (int p : s)for (int q : s)for (int r : s) { int lol = p | q | r; if (lol == 31)flag = 1; } debug(s, flag); if (flag) { ans = m; l = m + 1; } else { r = m - 1; } } cout << ans << en; return 0; }
#include <bits/stdc++.h> #include <variant> #define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++) #define rep(i,n) rep2(i,0,n) #define all(x) begin(x),end(x) #ifdef ENV_LOCAL #define dump if (1) cerr #else #define dump if (0) cerr #endif using namespace std; using namespace std::string_literals; using i32 = int32_t; using i64 = int64_t; using f64 = double; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; //using namespace harudake; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i64 n,k; cin>>n>>k; vector<tuple<i64,i64>> vt; rep(i,n) { i64 a,b; cin>>a>>b; vt.emplace_back(a,b); } vt.emplace_back(0,k); vt.emplace_back(1'000'000'000'000'000,0); sort(all(vt)); i64 sum = 0; i64 pos = 0; rep(i,n+1) { auto[a0,b0] = vt[i]; auto[a1,b1] = vt[i+1]; sum += b0; i64 d = a1 - a0; if (sum < d) break; pos = a1; sum -= d; } pos += sum; cout<<pos<<endl; return 0; }
#include<bits/stdc++.h> #define ulli unsigned long long int using namespace std; int main() { ulli n,k; cin>>n>>k; vector<pair<ulli,ulli>>v; for(int i = 0;i<n;i++) { ulli a,b; cin>>a>>b; v.push_back({a,b}); } sort(v.begin(),v.end()); for(int i = 0;i<n;i++) { if(v[i].first > k) break; k += v[i].second; } cout<<k; }
#include <bits/stdc++.h> #define int long long using namespace std; const int MAXN = 510; int A[MAXN]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while(T--) { int N; cin >> N; for(int i = 1 ; i <= N ; i++) { cin >> A[i]; } bool flag = true; vector<int> res; auto op = [&](int x) { res.push_back(x); swap(A[x],A[x+1]); flag = !flag; }; for(int x = N ; x >= 5 ; x--) { int pos = -1; for(int i = 1 ; i <= N ; i++) { if(A[i] == x) { pos = i; } } if(pos == 1) { if(!flag)op(2); } if(pos == 2) { if(flag)op(3); } if(pos == 3) { if(!flag)op(4); } if(pos >= 4) { if(pos%2 == 1) { if(!flag)op(2); } else { if(flag)op(1); } } for(int i = pos ; i < x ; i++) { op(i); } } if(N >= 4) { int pos4 = -1; for(int i = 1 ; i <= 4 ; i++) { if(A[i] == 4) { pos4 = i; } } if(pos4 == 1) { if(!flag)op(2); } else if(pos4 == 2) { if(flag)op(3); } else if(pos4 == 3) { if(!flag) { op(2); op(3); pos4 = 2; } } else { } for(int i = pos4 ; i < 4 ; i++) { op(i); } } if(N >= 3) { while(A[1] != 1 || A[2] != 2 || A[3] != 3) { if(flag)op(1); else op(2); } } else { while(A[1] != 1 || A[2] != 2) { if(flag)op(1); } } // for(int i = 1 ; i <= N ; i++) // { // cout << A[i] << " "; // } // cout << '\n'; cout << res.size() << '\n'; for(auto x : res) { cout << x << " "; } cout << '\n'; } return 0; }
#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <vector> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <iostream> #include <numeric> /* debug macros */ #ifdef WAFDAYO #define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";} #define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);} #define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7 #define dbg1(x1) DBG_PRINT(#x1,x1,std::endl) #define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2) #define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3) #define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4) #define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5) #define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6) #define dbg(...) DBG_LINE()\ SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__) #else #define dbg(...) {} #endif /* utility functions */ struct read_item{read_item(){}; template<class T>operator T(){T t;std::cin>>t;return t;}}; char splf(int i,int n){return(i+1<n)?' ':'\n';}; template<bool up> struct _RI{int i;_RI(int a):i(a){} int operator*(){return i;}void operator++(){i+=(up?1:-1);} bool operator!=(const _RI& r){return up?i<r.i:i>r.i;}}; template<bool up> struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;} _RX(int a,int b):x(up?a:(b-1)),y(up?b:(a-1)){}_RX(int a):_RX(0,a){}}; typedef _RX<true> range; typedef _RX<false> revrange; /* types and constants */ typedef int64_t i64; typedef uint64_t u64; // const i64 inf = (i64)1.05e18; // const int inf = (int)1.05e9; using namespace std; bool iseven(int x) { return (x & 1) == 0; } void oesort3(vector<int>& xs, int pos, bool even, vector<int>& ops) { int retry = 0; auto check = [&]() { for(auto i : range(3)) { if(xs[pos + i] != pos + i) { return false; } } return true; }; while(!check()) { int p = (iseven(pos) == even) ? pos : pos + 1; ops.push_back(p); swap(xs[p], xs[p + 1]); even = !even; retry += 1; assert(retry < 100); } } void oesort(vector<int>& xs, int pos, bool even, vector<int>& ops) { const int N = xs.size(); if(N - pos == 3) { return oesort3(xs, pos, even, ops); } assert(N - pos >= 4); int target = pos; while(xs[target] != pos) { target += 1; } if(target == pos) { oesort(xs, pos + 1, even, ops); return; } int retry = 0; while(iseven(target - 1) != even) { if(target - 2 >= pos) { ops.push_back(target - 2); swap(xs[target - 2], xs[target - 1]); even = !even; } else { ops.push_back(target); swap(xs[target], xs[target + 1]); target += 1; even = !even; } retry += 1; assert(retry < 100); } for(auto i : revrange(pos, target)) { assert(iseven(i) == even); ops.push_back(i); swap(xs[i], xs[i + 1]); even = !even; } oesort(xs, pos + 1, even, ops); } void solve() { const int N = read_item(); vector<int> As(N); for(auto i : range(N)) { As[i] = (int)read_item() - 1; } if(N == 2) { if(As[0] == 0) { printf("0\n"); return; } else { printf("1\n1\n"); return; } } static vector<int> ops; ops.resize(0); oesort(As, 0, true, ops); int c = ops.size(); assert(c <= N * N); printf("%d\n", c); for(auto i : range(c)) { printf("%d%c", ops[i] + 1, splf(i, c)); } } int main() { const int T = read_item(); for(auto _ : range(T)) { solve(); } return 0; } /* wafdayo~~~ */
#include <bits/stdc++.h> #define REP(i, e) for(int (i) = 0; (i) < (e); ++(i)) #define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i)) #define ALL(c) (c).begin(), (c).end() #define PRINT(x) cout << (x) << "\n" using namespace std; using ll = long long; using pll = pair<ll, ll>; const long long MOD = 1000000007; ll N, X, A[200]; ll dp[200][200][200]; signed main(){ cin >> N >> X; REP(i, N) cin >> A[i]; ll ans = LONG_LONG_MAX; FOR(k, 1, N + 1){ REP(i, N + 1) REP(j, N + 1) REP(m, k) dp[i][j][m] = LONG_LONG_MAX; dp[0][0][X % k] = X; REP(i, N){ REP(j, k + 1){ REP(m, k){ if(dp[i][j][m] == LONG_LONG_MAX) continue; dp[i + 1][j][m] = min(dp[i + 1][j][m], dp[i][j][m]); ll d = (k + (m - A[i]) % k) % k; if(j < k && dp[i][j][m] - A[i] >= 0) dp[i + 1][j + 1][d] = min(dp[i + 1][j + 1][d], dp[i][j][m] - A[i]); } } } if(dp[N][k][0] != LONG_LONG_MAX) ans = min(ans, dp[N][k][0] / k); } PRINT(ans); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define forn(i,x,n) for(int i = x; i < n; ++i) int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m,s,g; cin>>n>>m; vector<int> dist(n*m); vector<bool> done(n*m,false); vector<vector<int>> adj(n*m); unordered_map<char,vector<int>> teleport; vector<string> a(n); forn(i,0,n) cin>>a[i]; forn(i,0,n) forn(j,0,m){ dist[i*m+j]=INT_MAX; if(a[i][j]!='#' && a[i][j]!='.') teleport[a[i][j]].push_back(i*m+j); if(a[i][j]=='S') s=i*m+j; if(a[i][j]=='G') g=i*m+j; if(i && a[i-1][j]!='#') adj[i*m+j].push_back((i-1)*m+j); if(j && a[i][j-1]!='#') adj[i*m+j].push_back(i*m+j-1); if(i<n-1 && a[i+1][j]!='#') adj[i*m+j].push_back((i+1)*m+j); if(j<m-1 && a[i][j+1]!='#') adj[i*m+j].push_back(i*m+j+1); } priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q; dist[s]=0; q.push({dist[s],s}); while(q.size()){ int f = q.top().second; q.pop(); // if(done[f]) // continue; done[f]=true; for(auto v: adj[f]) if(dist[v]>dist[f]+1) dist[v]=dist[f]+1, q.push({dist[v],v}); if(teleport.find(a[f/m][f%m])!=teleport.end()){ for(auto v:teleport[a[f/m][f%m]]) if(dist[v]>dist[f]+1) dist[v]=dist[f]+1, q.push({dist[v],v}); teleport.erase(a[f/m][f%m]); } } cout<<(dist[g]==INT_MAX?-1:dist[g]); }
#include <bits/stdc++.h> using namespace std; long long i,j,p,l,t; long long c[234567],a[213167],b[211167],d,e[234567],tt=0,k=0,y,z,n=0,x; char m; string q,w,s; int main(){ cin >>t>>l>>p; cout<<t+l+p-min(t,min(l,p)); }
#include <bits/stdc++.h> using namespace std; int main(void) { long long x,y; cin >> x >> y; long long a[3]={0}; if(x==y) cout<<x<<endl; else { a[x]++; a[y]++; for(long long i=0;i<3;i++) { if(a[i]==0) cout<<i<<endl; } } return 0; }
/** Fucc :/ - doanphuc : / ** author: doanphuc. ** **/ #include <bits/stdc++.h> #define TASK "" #define inpf freopen(TASK".inp", "r", stdin) #define outf freopen(TASK".out", "w", stdout) #define FOR(i,x,y) for(int i = x; i <= y ; i ++) #define FORT(i,x,y) for(int i = x; i < y ; i ++) #define FORD(i,x,y) for(int i = x; i >= y ; i --) #define clean(x) memset(x, 0, sizeof(x)) #define F first #define S second #define maxn 3000005 #define pii pair<ll,ll> #define ll long long #define pb push_back #define yes cout << "YES" << endl #define no cout << "NO" << endl #define all(x) (x).begin(), (x).end() #define pia pair<pii, int> #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; const ll MOD = 1e9 + 7 ; ll n, a[maxn], b[maxn], gt[maxn], d[maxn] ; int main() { cin >> n; FOR(i, 1, n) cin >> a[i] ; FOR(i, 1, n) cin >> b[i] ; ll res = 0 ; FOR(i, 1, n) { res = max(res, a[i]) ; gt[i] = res ; } res = 0 ; FOR(i, 1, n) { ll x = b[i] * gt[i] ; if(x > res) res = x ; d[i] = res ; } FOR(i, 1, n) cout << d[i] << endl ; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; const int INF = 1e9; const int MOD = 1000000007; const int dx[] = {-1,0,1,0}; const int dy[] = {0,-1,0,1}; // const int dx[] = {-1,-1,-1, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1,-1, 1,-1, 0, 1}; #define PI 3.14159265358979323846264338327950L // static const double e = 2.718281828459045; //cout << fixed << setprecision(10) << ans << endl; vector<ll> A, B; struct UnionFind { vector<int> par, rnk, mem; vector<ll> sum_a, sum_b; UnionFind(int n) { par.resize(n); fill(par.begin(), par.end(), -1); rnk.resize(n); fill(rnk.begin(), rnk.end(), 0); mem.resize(n); fill(mem.begin(), mem.end(), 1); sum_a.resize(n); rep(i, n) sum_a[i] = A[i]; sum_b.resize(n); rep(i, n) sum_b[i] = B[i]; } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; mem[y] += mem[x]; sum_a[y] += sum_a[x]; sum_b[y] += sum_b[x]; } else { par[y] = x; mem[x] += mem[y]; sum_a[x] += sum_a[y]; sum_b[x] += sum_b[y]; if (rnk[x] == rnk[y]) rnk[x]++; } } int size(int x) { return mem[root(x)]; } ll size_a(int x) { return sum_a[root(x)]; } ll size_b(int x) { return sum_b[root(x)]; } }; int main() { int N, M; cin >> N >> M; A.resize(N); B.resize(N); rep(i, N) cin >> A[i]; rep(i, N) cin >> B[i]; UnionFind uf(N); rep(i, M) { int c, d; cin >> c >> d; c--; d--; uf.merge(c, d); } int flg = 1; rep(i, N) { if (uf.size_a(i) != uf.size_b(i)) flg = 0; } if (flg == 1) cout << "Yes" << endl; else cout << "No" << endl; }
#include<bits/stdc++.h> using namespace std; int h,w,a,b; int ans; void dfs(int i,int bit,int a,int b){ if(i==h*w){ ans++; return; } if(bit&(1<<i)){ dfs(i+1,bit,a,b); return; } if(b){ dfs(i+1,bit|1<<i,a,b-1); } if(a){ if(i%w!=w-1&&(~bit&1<<(i+1))){ dfs(i+1,bit|1<<i|1<<(i+1),a-1,b); } if(i+w<h*w){ dfs(i+1,bit|1<<i|1<<(i+w),a-1,b); } } } int main(){ cin>>h>>w>>a>>b; dfs(0,0,a,b); cout<<ans<<'\n'; }
// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder #include<bits/stdc++.h> //#include<atcoder/all> typedef long long ll; typedef long double ld; using namespace std; //using namespace atcoder; using Pii = pair<int, int>; using Pll = pair<ll, ll>; //ordered_set 重複不可 #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // use set_function + find_by_order(select itr-num) #define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i) #define ALL(x) (x).begin(),(x).end() #define pb push_back ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} 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;} char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; int dx[2] = {1, 0}; int dy[2] = {0, 1}; //cout << std::fixed << std::setprecision(15) << y << endl; //小数表示 int h, w, a, b; vector<vector<vector<Pii>>> gridmemo; vector<vector<vector<Pii>>> gridmemo2; void solve(vector<vector<Pii>> grid, int x, int y, int count){ if(x+1 == h && y+1 == w){ if(count == a){ gridmemo.pb(grid); }else{ return; } }else if(x+1 == h){ solve(grid, 0, y+1, count); }else{ solve(grid, x+1, y, count); } REP(i,0,2){ int nx = x + dx[i], ny = y+dy[i]; if(nx >= 0 && nx < h && ny >= 0 && ny < w && grid[nx][ny] == Pii(-1, -1) && grid[x][y] == Pii(-1, -1)){ grid[x][y] = Pii(nx, ny); grid[nx][ny] = Pii(x, y); if(x+1 == h && y+1 == w){ if(count+1 == a){ gridmemo.pb(grid); }else{ return; } }else if(x+1 == h){ solve(grid, 0, y+1, count+1); }else{ solve(grid, x+1, y, count+1); } grid[x][y] = Pii(-1, -1); grid[nx][ny] = Pii(-1, -1); } } } int main(){ cin >> h >> w >> a >> b; vector<vector<Pii> > grid(h, vector<Pii>(w, Pii(-1, -1))); int ans = 0; solve(grid, 0, 0, 0); /* REP(i, 0, gridmemo.size()){ REP(x, 0,h){ REP(y, 0, w){ cout << gridmemo[i][x][y] << " "; } cout << endl; } cout << endl; } */ REP(i,0,gridmemo.size()){ int flag = 1; REP(l, 0, gridmemo2.size()){ int f = 1; REP(x, 0, h){ REP(y, 0, w){ if(gridmemo[i][x][y] != gridmemo2[l][x][y]){ f = 0; //OK } } } if(f){ flag = 0; } } if(flag){ gridmemo2.pb(gridmemo[i]); } ans += flag; } cout << ans << endl; }
//---Vaidik Patel---// //-------------------Proud to be Indian--------------------------------// #include<bits/stdc++.h> using namespace std; #include<ext/pb_ds/assoc_container.hpp> // include the associative containers or group of templates such as set,multi multimap, map etc. #include<ext/pb_ds/tree_policy.hpp> // include the tree_order_statistics_node update using namespace __gnu_pbds; #define int long long int #define ld long double #define pii pair<int,int> typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; // find_by_order() #define deb1(x) cout<<#x<<" : "<<x<<"\n"; #define deb2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<"\n"; #define deb3(x,y,z) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<" "<<#z<<": "<<z<<"\n"; #define deb4(x,y,z,w) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<"\t"<<#w<<" : "<<w<<endl; #define deb5(a, b, c, d, e) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl; #define deb6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl; #define FOR(i,a,b) for(int i=a;i<b;i++) #define rFOR(i,a,b) for(int i=a;i>=b;i--) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define pb push_back #define ff first #define ss second #define vi vector<int> #define vvi vector<vector<int>> #define M 998244353 #define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int powermodm(int x,int n,int m) {int ans=1; while(n){if(n&1){ans=(ans*x)%m;}x=(x*x)%m;n=n>>1;}return ans;} void solve() { int n; cin>>n; string s; cin>>s; stack<char>st; FOR(i,0,n) { if(i<2) { st.push(s[i]); } else { if(s[i]=='x' && st.size()>=2) { char y=st.top(); st.pop(); char x=st.top(); st.pop(); if(x=='f' && y=='o') { } else { st.push(x); st.push(y); st.push(s[i]); } } else { st.push(s[i]); } } } cout<<st.size()<<"\n"; } signed main() { FIO int t; t=1; //cin>>t; FOR(i,0,t) { //cout<<"Case #"<<i+1<<": "; solve(); } }
#include<bits/stdc++.h> using namespace std; #define ll long long #define u unsigned #define ull unsigned long long #define ld long double #define pb push_back #define fasterInOut ios::sync_with_stdio(false); cin.tie(0); #define pi acos(-1) #define f(i,a,b) for(int i=(a);i<=(b);i++) #define fr(i,a,b) for(int i=(a);i>=(b);i--) #define MAX 1000000 void function(int x); int main() { #ifndef ONLINE_JUDGE freopen("C:/Users/ASUS/Desktop/inout/input.txt", "r", stdin); freopen("C:/Users/ASUS/Desktop/inout/output.txt", "w", stdout); #endif int a,b,c; cin>>a>>b>>c; if((a+b)>=(b+c) && (a+b)>=(a+c)) cout<<a+b<<endl; else if((b+c)>=(a+b) && (b+c)>=(a+c)) cout<<b+c<<endl; else if((a+c)>(a+b) && (a+c)>(b+c)) cout<<a+c<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, j, n) for(int i = (j); i < (int)(n); i++) #define print(s) cout << s << endl #define SIZE(s) long long(s.size()) using Graph = vector<vector<int>>; using ll = long long; int main(){ int a, b, c; cin >> a >> b >> c; if(a == b) print(c); else if(b == c) print(a); else if(c == a) print(b); else print(0); }
/*      />  フ      |  _  _|      /`ミ _x 彡      /      |     /  ヽ   ?  / ̄|   | | |  | ( ̄ヽ__ヽ_)_)  \二つ */ #pragma GCC optimize("Ofast","inline","-ffast-math") #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<bits/stdc++.h> #define int long long #define pb push_back #define pf push_front #define F first #define S second #define SS stringstream #define sqr(x) ((x)*(x)) #define m0(x) memset(x,0,sizeof(x)) #define m1(x) memset(x,63,sizeof(x)) #define CC(x) cout << (x) << endl #define AL(x) x.begin(),x.end() #define pw(x) (1ull<<(x)) #define NMSL cout << "NMSL" << endl; #define debug(x) cout << #x << ": " << x << endl; #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; #define fio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define in128 __int128_t using namespace std; const int N = 2e5+10; const int INF = 0x7f7f7f7f; const double EPS = 1e-3; const long double PI = 3.14159265358979323846; int ksm(int a,int b,int mod) { int ans=1; while(b) { if(b&1) ans=ans*a%mod; a=a*a%mod; b>>=1; } return ans; } signed main() { fio int a,b,c; cin>>a>>b>>c; b=ksm(b,c,4)+4; a%=10; cout<<ksm(a,b,10); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define forn(i,x,n) for(int i = x; i < n; ++i) int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m,s,g; cin>>n>>m; vector<int> dist(n*m); vector<bool> done(n*m,false); vector<vector<int>> adj(n*m); unordered_map<char,vector<int>> teleport; vector<string> a(n); forn(i,0,n) cin>>a[i]; forn(i,0,n) forn(j,0,m){ dist[i*m+j]=INT_MAX; if(a[i][j]!='#' && a[i][j]!='.') teleport[a[i][j]].push_back(i*m+j); if(a[i][j]=='S') s=i*m+j; if(a[i][j]=='G') g=i*m+j; if(i && a[i-1][j]!='#') adj[i*m+j].push_back((i-1)*m+j); if(j && a[i][j-1]!='#') adj[i*m+j].push_back(i*m+j-1); if(i<n-1 && a[i+1][j]!='#') adj[i*m+j].push_back((i+1)*m+j); if(j<m-1 && a[i][j+1]!='#') adj[i*m+j].push_back(i*m+j+1); } priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q; dist[s]=0; q.push({dist[s],s}); while(q.size()){ int f = q.top().second; q.pop(); if(done[f]) continue; done[f]=true; for(auto v: adj[f]) if(dist[v]>dist[f]+1) dist[v]=dist[f]+1, q.push({dist[v],v}); if(teleport.find(a[f/m][f%m])!=teleport.end()){ for(auto v:teleport[a[f/m][f%m]]) if(dist[v]>dist[f]+1) dist[v]=dist[f]+1, q.push({dist[v],v}); teleport.erase(a[f/m][f%m]); } } cout<<(dist[g]==INT_MAX?-1:dist[g]); }
#include "bits/stdc++.h" #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") // #pragma GCC optimization ("O3") // #pragma GCC optimization ("unroll-loops") // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; //#include <boost/multiprecision/cpp_int.hpp> //using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(),(c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(),(c).rend() #define ifr(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m,n,r) m.reserve(n);m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> //order_of_key (k) : Number of items strictly smaller than k . //find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd(){return 0ll;} template<typename T,typename... Args> T gcd(T a,Args... args) { return __gcd(a,(__typeof(a))gcd(args...)); } typedef map<ll,ll> mll; typedef map<string,ll> msll; typedef unordered_map<ll,ll> umap; typedef vector<ll> vll; typedef pair<ll,ll> pll; typedef long double ld; #define mod 1000000007 const int N = 1e5 + 2; void Solve() { long long t, n; cin>>t>>n; long long ans = ceil((100.0*n)/t); cout<<ans+n-1<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // #endif // int T;cin>>T;while(T--) { Solve(); } return 0; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; // using cint = cpp_int; #define fastio \ std::cin.tie(nullptr); \ std::ios::sync_with_stdio(false); using ll = long long; using ld = long double; #define For(i, m, n) for(int i = (m); (i) < (n); ++(i)) #define Rep(i, n) For(i, 0, n) #define Each(e, con) for(auto e: con) #define rEach(e, con) for(auto &e: con) #define crEach(e, con) for(const auto &e: con) #define All(con) (con).begin(), (con).end() #define Unique(con) std::sort(All(con)); (con).erase(std::unique(All(con)), (con).end()) #define Reverse(con) std::reverse(All(con)) #define Middle(con) (con).begin() + ((con).end() - (con).begin()) / 2 template <class Ty1, class Ty2> std::istream &operator>>(std::istream &is, std::pair<Ty1, Ty2> &arg) { is >> arg.first >> arg.second; return is; } template <class Ty> std::istream &operator>>(std::istream &is, std::vector<Ty> &arg) { rEach(e, arg) is >> e; return is; } template <class Ty> std::istream &operator>>(std::istream &is, std::vector<std::vector<Ty>> &arg) { rEach(sub, arg) { rEach(e, sub) { is >> e; }} return is; } template <class Ty1, class Ty2> std::istream &vecinput(std::vector<Ty1> &arg1, std::vector<Ty2> &arg2) { assert(arg1.size() == arg2.size()); auto sz = arg1.size(); Rep(i, (int)sz) std::cin >> arg1[i] >> arg2[i]; return std::cin; } template <class Ty1, class Ty2, class Ty3> std::istream &vecinput(std::vector<Ty1> &arg1, std::vector<Ty2> &arg2, std::vector<Ty3> &arg3) { assert(arg1.size() == arg2.size()); assert(arg2.size() == arg3.size()); auto sz = arg1.size(); Rep(i, (int)sz) std::cin >> arg1[i] >> arg2[i] >> arg3[i]; return std::cin; } template <class Ty1, class Ty2, class Ty3, class Ty4> std::istream &vecinput(std::vector<Ty1> &arg1, std::vector<Ty2> &arg2, std::vector<Ty3> &arg3, std::vector<Ty4> &arg4) { assert(arg1.size() == arg2.size()); assert(arg2.size() == arg3.size()); assert(arg3.size() == arg4.size()); auto sz = arg1.size(); Rep(i, (int)sz) std::cin >> arg1[i] >> arg2[i] >> arg3[i] >> arg4[i]; return std::cin; } int main() { fastio int r, x, y; cin >> r >> x >> y; double dist = hypot(x, y); int ans = 0; while(dist > 2 * r) { dist -= r; ++ans; } if(abs(dist - r) < 1e-9) cout << ans + 1 << endl; else cout << ans + 2 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define vi vector<int> #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define ff first #define ss second #define max(a,b) (a>b ? a:b) #define min(a,b) (a<b ? a:b) void solve(){ int r,x,y; cin>>r>>x>>y; int ans=1; int k=x*x+y*y; if(r*r>k){ cout<<2; return; } while(k>(ans*r*ans*r)){ ans++; } cout<<ceil(ans); } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // int t;cin>>t;while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl '\n' #define ll long long #define ding cerr<<endl<<"Time: "<<(clock()*1000./CLOCKS_PER_SEC)<<" ms"<<endl; const int maxn=2e5+10; int siz[maxn],dfn[maxn],rdfn[maxn],tot,totd,n; vector<int> yuan[maxn],cun[maxn]; void dfs(int now,int fa,int dep){ dfn[now]=++tot;rdfn[tot]=now; siz[now]=1; totd=max(totd,dep); cun[dep].push_back(dfn[now]); for(int i=0;i<yuan[now].size();i++){ int v=yuan[now][i]; if(v==fa) continue; dfs(v,now,dep+1); siz[now]+=siz[v]; } } ll req(int now1,int now2,int dep){ return upper_bound(cun[dep].begin(),cun[dep].end(),now2)-lower_bound(cun[dep].begin(),cun[dep].end(),now1); } signed main(){ ios;cin>>n; for(int i=2;i<=n;i++){ int a1;cin>>a1; yuan[i].push_back(a1);yuan[a1].push_back(i); } dfs(1,0,1); int q;cin>>q; while(q--){ int a1,a2;cin>>a1>>a2;a2++; cout<<req(dfn[a1],dfn[a1]+siz[a1]-1,a2)<<endl; } ding;return 0; }
//بسم الله الرحمن الرحيم // -----> Author : N A Y E M // -----> [ CSE-19 , CU ] #include<bits/stdc++.h> using namespace std ; typedef long long ll; typedef unsigned long long ull ; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int,int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; typedef double dl; //using u128 = __uint128_t; #define el << '\n' #define PI 3.141592653589793238 #define _test_ int _ ; cin >> _ ; while( _-- ) #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define file() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout) ll gcd ( ll a, ll b ) { return __gcd ( a, b ) ; } ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ) ; } ll big_mod(ll base , ll power , ll mod){ ll ret=1;base%=mod;while(power){if(power&1)ret=ret*base%mod;base=base*base%mod;power>>=1;}return ret;} ll mod_inverse(ll a , ll mod ){ return big_mod(a,mod-2,mod);} ull POW(ll a , ll b){if(b==0) return 1;ull ret=1;while(b--)ret*=a;return ret;} int dx[]={0,0,+1,-1,+1,-1,-1,+1} ; int dy[]={+1,-1,0,0,+1,-1,+1,-1} ; // ------------------> const ll infLL = 1e15; const int inf = 1e8+123; const ll mod = 1e9+7; const ll mx = 3e5+123; // ------------------> pdd p[mx]; void _case_() { ll n , ans = 0 ; cin >> n ; for(int i=0;i<n;i++) { cin >> p[i].first >> p[i].second ; } for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) { double m = (abs(p[i].second-p[j].second)/abs(p[i].first-p[j].first)); if(m>=-1 and m<=1) ans ++ ; } cout << ans el; } int main( ) { optimize() ; //_test_ _case_() ; return 0 ; }
#include <bits/stdc++.h> using namespace std; #define eb emplace_back using ll = long long; const int inf = 1e9+5; const ll INF = 1e18+5; const int nax = 2e5+500; void out(vector<int> &a) { cout << a.size(); for(int z: a) { cout <<" "<< z; } cout <<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n]; for(int i=0;i<n;i++) cin >> a[i]; int base = (1 << min(n,8)) - 1; vector<vector<int>> f(200, vector<int> (0)); for(int i=1;i<=base;i++) { int ga = 0; vector<int> ans; for(int j=0;j<min(n,8);j++) { if(i & (1 << j)) { ga += a[j]; ga %= 200; ans.eb(j+1); } } if(f[ga].size() != 0) { cout << "Yes\n"; out(f[ga]); out(ans); return 0; } else { f[ga] = ans; } } cout <<"No\n"; }
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<cmath> #include<queue> #include<stack> #include<deque> #include<list> #include<bitset> #include<sstream> using namespace std; using ll = long long; const ll MOD = 1e9+7; const ll INF = 1e18; #define REP(i, n) for(int i = 0; i < n; i++) #define endl "\n" using Graph = vector<vector<int>>; int main(){ string s; cin>>s; string ans; for(int i=0;i<3;i++){ ans += s[(i+1)%3]; }; cout<<ans<<endl; }
#include <bits/stdc++.h> #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define ll long long using namespace std; const int MAX = 5e5+50; char s[MAX], t[MAX]; int n; vector<int> a, b; int main() { scanf("%d%s%s", &n, s + 1, t + 1); for (int i = 1; i <= n; i++) { if (s[i] == '0') a.emplace_back(i); if (t[i] == '0') b.emplace_back(i); } if (SZ(a) != SZ(b)) { puts("-1"); return 0; } int sum = 0; for (int i = 0; i < SZ(a); i++) sum += a[i] != b[i]; printf("%d\n", sum); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define deb(x) cerr << #x << ":" << x << "\n" #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define int ll int get(string s,string t){ int n=s.size(); set<int> zero; set<int> one; for(int i=0;i<n;i++){ if(s[i]=='0'){ zero.insert(i); }else{ one.insert(i); } } int ans=0; set<int> st; for(int i=0;i<n;i++){ if(s[i]!=t[i])st.insert(i); } for(int i=0;i<n;i++){ deb(i); if(s[i]!=t[i]){ if(t[i]=='0'){ ++ans; assert(zero.size()>0); int pos=*zero.begin(); zero.erase(zero.begin()); s[pos]='1'; one.insert(pos); }else{ deb("else"); int pos=*one.begin(); one.erase(one.begin()); int x=*zero.begin(); int y=n-1; auto zit=zero.lower_bound(pos); if(zit!=zero.end()){ y=*zit;--y; } auto eit=st.lower_bound(pos); int rpos=pos; if(eit!=st.end()){ rpos=*eit; } int z=n; auto sit=zero.lower_bound(x+1); if(sit!=zero.end()){ z=*sit; // zero.erase(zero.begin()); } if(z<pos){ ans+=pos-i; s[rpos]='0'; zero.insert(rpos); }else{ ++ans; s[rpos]='0'; zero.insert(rpos); } } st.erase(i); } } return ans; } int get_ans(string &s,string &t){ int ans=min(get(s,t),get(t,s)); reverse(all(s));reverse(all(t)); return min(ans,min(get(s,t),get(t,s))); } int optiaml(string s,string t){ int n=s.size(); if(count(all(s),'0')!=count(all(t),'0'))return -1; vector<int> v; for(int i=0;i<n;i++){ if(t[i]=='0')v.emplace_back(i); } int ans=0,j=0; for(int i=0;i<n;i++){ if(s[i]=='0'){ ans+=(i!=v[j++]); } } return ans; } void solve() { int n; cin>>n; string s,t; cin>>s>>t; cout<<optiaml(s,t)<<"\n"; } int32_t main() { #ifndef ONLINE_JUDGE freopen("../text_files/input.txt", "r", stdin); freopen("../text_files/output.txt", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int N, vis[10005]; int main () { #ifndef ONLINE_JUDGE freopen("cpp.in", "r", stdin); #endif cin >> N; if (N == 3) return puts("6 10 15") & 0; for (int i = 6; i <= 10000; i += 6) vis[i] = 1; for (int i = 10; i <= 10000; i += 10) vis[i] = 1; for (int i = 15; i <= 10000; i += 15) vis[i] = 1; for (int i = 1; i <= 10000 && N; i++) if (vis[i]) printf("%d ", i), N--; putchar('\n'); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 10009; int n, a[N], tot; int main() { cin >> n; for(int i = 6; i < 10000; i ++) { if(i == 12) continue; if(i % 6 == 0 || i % 10 == 0 || i % 15 == 0) a[tot ++] = i; } for(int i = 0; i < n; i ++) cout << a[i] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; int main() { int n,m; cin >> n >> m; set<int> sa, sb; rep(i,n){ int ai ; cin >> ai; sa.insert(ai); } rep(i,m){ int bi ; cin >> bi; sb.insert(bi); } vector <int> v(1001,0); for(int a : sa) if(sb.count(a)==0) v[a] = 1; for(int b : sb) if(sa.count(b)==0) v[b] = 1; rep(i,1001) if(v[i] == 1) cout << i << ' '; }
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << (a+b) / 2 << ' ' << (a-b) / 2 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define ll long long #define ld long double #define test ll t=0;cin>>t; while(t--) #define loop(i,a,b) for(i=a;i<=b;i++) #define loopr(i,a,b) for(i=a;i>=b;i--) #define lb lower_bound #define ub upper_bound #define mid(l,r) mid=l+(r-l)/2 #define endl "\n" #define yess cout<<"YES\n" #define noo cout<<"NO\n" int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif fastio; ll n,k;cin>>n>>k; vector<ll>a(n); ll i,y=0; ll s[1000000]={0}; ll ans=0; for(i=0;i<n;i++) { cin>>a[i]; s[a[i]]++; } for(i=0;i<1e6;) { if(s[i]==0){ ans=ans+i;y++; if(y==k)break; if(i==0)break; i=0;continue; } s[i]--;i++; } cout<<ans; }
#include<bits/stdc++.h> using namespace std; #define stop exit(0) #define pb push_back #define skip continue #define ll long long ll a,b,c,d=0,e=0,i,f=0,g=0,j,k=0,l=0,r,z,m,h,p[200001],mod=1000000007,mod1=998244353; string t,x,q,s; long long df[101]; //double ; vector<ll>v,u,w; vector<ll>dv[101]; //vector<string>; set<ll>y; map<ll,ll>n; void dfs(ll x){ df[x]=1; for(ll i=0;i<dv[x].size();i++){ if(df[dv[x][i]]==0){ df[dv[x][i]]=1; dfs(dv[x][i]); } } } int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); cin>>a>>b; for(i=1;i<=b;i++){ cin>>p[i]; } d=0; e=a+1; sort(p+1,p+b+1); if(b==0){ cout<<1; } else if(b==a){ cout<<0; } else{ if(p[1]>1){ v.pb(p[1]-1); } for(i=2;i<=b;i++){ if(p[i]-1!=p[i-1]){ v.pb(p[i]-p[i-1]-1); } } if(p[b]<a){ v.pb(e-p[b]-1); } sort(v.begin(),v.end()); d=v[0]; e=mod; for(i=1;i<=d;i++){ f=0; for(j=0;j<v.size();j++){ f=f+((v[j]+v[0]-1)/v[0]); } e=min(e,f); } cout<<e; } }
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<int, LL> PIL; const int N = 1e5 + 10, M = 3e5 + 10; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; #define x first #define y second const int mod = 1e9 + 7; #define gcd __gcd void solve() { LL n; scanf("%lld", &n); LL res = 2e18; for(LL i = 0; (1LL << i) <= n; i++) { LL a = n / (1LL << i); LL c = n % (1LL << i); res = min(res, a + i + c); } printf("%lld\n", res); } int main() { // freopen("in.txt", "r", stdin); // int t; cin >> t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> #include <string> typedef long long ll; typedef long double ldb; typedef double db; using namespace std; #define ff first #define ss second #define pb push_back #define pf push_front #define ins insert #define mp make_pair #define len(s) s.length() #define forp(i, a, b) for (ll i = a; i <= b; i++) #define rep(i, n) for (ll i = 0; i < n; i++) #define ren(i, n) for (ll i = n - 1; i >= 0; i--) #define forn(i, a, b) for (ll i = a; i >= b; i--) #define on cout << endl #define o2(a, b) cout << a << " " << b #define os cout << " " #define all(v) v.begin(), v.end() #define mem(n, m) memset(n, m, sizeof(n)) #define vi vector<int> #define vl vector<ll> #define ld long double #define vld vector<ld> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define vvld vector<vector<ld>> #define pii pair<int, int> #define pll pair<ll, ll> #define vpii vector<pii> #define vpll vector<pll> #define mll map<ll, ll> #define lb lower_bound #define ub upper_bound #define ret return 0 #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define ford(container, it) for (__typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define fors(container, it, a, b) for (__typeof(container.begin()) it = a; it != b; it++) #define d1(x) cout << (x) << endl #define d2(x, y) cout << (x) << " " << (y) << endl #define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl #define d4(a, b, c, d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define max3(a, b, c) max(max(a, b), c) #define min3(a, b, c) min(min(a, b), c) #define itoa(x) to_string(x); #define atoi(x) stoll(x); #define boost ios_base::sync_with_stdio(0) #define MOD 1000000007 #define EPSILON 1e-9 #define PI 3.14159265358979323846 #define inf 999999999999999999 #define siz 100001 #define SIZ 1000001 #define SIZE 200001 #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 w(x) \ int x; \ cin >> x; \ while (x--) ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll pwr(ll x, ll y) { ll res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (b * a) / gcd(a, b); } ll fact(ll n) { if (n <= 1) return 1; return n * fact(n - 1); } ll nPr(ll n, ll r) { return fact(n) / fact(n - r); } void c_p_c() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } ll f(ll n) { ll m = 0; while(n>0) { m = max(m, n%10); n/=10; } return m; } // int dp[1005][100005]; //#books vs pricelimit int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); c_p_c(); ll tt; tt = 1; // cin >> tt; while (tt-- > 0) { ll n; cin>>n; ll b = 0, a = n, c = 0, q = a*pwr(2, b); ll m = a+b+c; while(pwr(2, b)<=n) { b++; a = n/pwr(2, b); c = n%pwr(2, b); m = min(a+b+c, m); } d1(m); } return 0; }