code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- template<class V> struct BIT { BIT() {} // [L, R) int NV;vector<V> bit; BIT(int n){ init(n); } void init(int n) { NV = 1; while (NV < n)NV *= 2; bit.resize(NV); clear(); } V operator[](int e) { V s = 0; e++; while (e) s += bit[e - 1], e -= e&-e; return s; } void add(int e, V v) { e++; while (e <= NV) bit[e - 1] += v, e += e&-e; } int lower_bound(V val) { V tv = 0; int i, ent = 0; for (i = NV - 1; i >= 0; i--) if(tv+bit[ent+(1<<i)-1]<=val)tv+=bit[ent+(1<<i)-1],ent += (1 << i);return ent; } V get(int L, int R) { assert(0 <= L); assert(R <= NV); assert(L <= R); V res = 0; if(R) res += operator[](R - 1); if (L) res -= operator[](L - 1);return res; } void clear() { rep(i, 0, NV) bit[i] = 0; } void update(int e, V v) { add(e, v - get(e, e + 1)); } }; vector<int> compress1(int *v, int n) { vector<int> dic; rep(i, 0, n) dic.push_back(v[i]); sort(all(dic)); dic.erase(unique(all(dic)), dic.end()); rep(i, 0, n) v[i] = lower_bound(all(dic), v[i]) - dic.begin(); return dic; } /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, M, Q, T[201010], X[201010], Y[201010]; int A[201010], B[201010]; BIT<ll> Acnt(201010), Bcnt(201010); BIT<ll> Atot(201010), Btot(201010); //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M >> Q; rep(i, 0, Q) cin >> T[i] >> X[i] >> Y[i]; auto ZY = compress1(Y, Q + 1); Acnt.add(0, N); Bcnt.add(0, M); ll ans = 0; rep(q, 0, Q) { if (T[q] == 1) { int x = X[q] - 1; int y = Y[q]; ans -= Bcnt.get(0, A[x] + 1) * ZY[A[x]]; ans -= Btot.get(A[x] + 1, 201010); Acnt.add(A[x], -1); Atot.add(A[x], -ZY[A[x]]); A[x] = y; ans += Bcnt.get(0, A[x] + 1) * ZY[A[x]]; ans += Btot.get(A[x] + 1, 201010); Acnt.add(A[x], 1); Atot.add(A[x], ZY[A[x]]); } else { int x = X[q] - 1; int y = Y[q]; ans -= Acnt.get(0, B[x] + 1) * ZY[B[x]]; ans -= Atot.get(B[x] + 1, 201010); Bcnt.add(B[x], -1); Btot.add(B[x], -ZY[B[x]]); B[x] = y; ans += Acnt.get(0, B[x] + 1) * ZY[B[x]]; ans += Atot.get(B[x] + 1, 201010); Bcnt.add(B[x], 1); Btot.add(B[x], ZY[B[x]]); } printf("%lld\n", ans); } }
#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; ll A[51]; ll X; ll ret; map<ll,ll> memo[55]; ll dfs(int cur,ll lef) { if(cur==N-1) { return 1; } if(memo[cur].count(lef)) return memo[cur][lef]; ll ret=0; ll step=A[cur+1]/A[cur]; ll a=lef%step; if(a==0) { ret+=dfs(cur+1,lef/step); } else { ret+=dfs(cur+1,lef/step); ret+=dfs(cur+1,lef/step+1); } return memo[cur][lef]=ret; } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>X; FOR(i,N) cin>>A[i]; cout<<dfs(0,X)<<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; }
#include <bits/stdc++.h> using namespace std; typedef long long lint; #define rep(i,n) for(lint (i)=0;(i)<(n);(i)++) #define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++) #define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--) #define INF (1ll<<60) #define all(x) (x).begin(),(x).end() //const lint MOD =1000000007; const lint MOD=998244353; const lint MAX = 1000000; using Graph =vector<vector<lint>>; typedef pair<lint,lint> P; typedef map<lint,lint> M; #define chmax(x,y) x=max(x,y) #define chmin(x,y) x=min(x,y) lint fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (lint i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(lint n, lint k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } lint primary(lint num) { if (num < 2) return 0; else if (num == 2) return 1; else if (num % 2 == 0) return 0; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { return 0; } } return 1; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } lint lcm(lint a,lint b){ return a/__gcd(a,b)*b; } lint gcd(lint a,lint b){ return __gcd(a,b); } int main(){ lint n,q,a,b; string s; cin>>a>>b; lint x=2*a+100; x-=b; chmax(x,0LL); cout<<x<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int A,B,follow; cin>>A>>B; follow=2*A+100; cout<< follow - B<<"\n"; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int h = ri(); int w = ri(); int x = ri() - 1; int y = ri() - 1; std::string s[h]; for (auto &i : s) std::cin >> i; int cnt = -3; for (int i = x; i < h && s[i][y] != '#'; i++) cnt++; for (int i = x; i >= 0 && s[i][y] != '#'; i--) cnt++; for (int j = y; j < w && s[x][j] != '#'; j++) cnt++; for (int j = y; j >= 0 && s[x][j] != '#'; j--) cnt++; printf("%d\n", cnt); return 0; }
#define ALL(x) (x).begin(), (x).end() #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> ii; #define DEBUG freopen("in.txt", "r", stdin); struct fastio { fastio() { ios::sync_with_stdio(false); cout << setprecision(10) << fixed; cin.tie(0); } }; fastio _fast_io; const int N = 105; ll n, x, ans; ll a[N]; void solve(ll num) { ll r = x % num; ll cur = x / num; vector<vector<ll>> dp(num + 1, vector<ll>(num, -1)); dp[0][0] = 0; for (int i = 0; i < n; ++i) { auto dp2 = dp; for (int j = 0; j < num; ++j) { for (int k = 0; k < num; ++k) { if (dp[j][k] != -1) { ll nk = (k + a[i]) % num; ll add = (k + a[i]) / num; if (dp2[j + 1][nk] == -1) { dp2[j + 1][nk] = dp[j][k] + add; } else { dp2[j + 1][nk] = max(dp2[j + 1][nk], dp[j][k] + add); } } } } dp = move(dp2); } if (dp[num][r] != -1) ans = min(ans, cur - dp[num][r]); } int main() { ans = std::numeric_limits<ll>::max(); cin >> n >> x; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { solve(i); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 2e3 + 5; vector<vector<int>> G(N); vector<bool> vis(N); void dfs(int u) { vis[u] = true; for (int v : G[u]) { if (vis[v] == true) continue; dfs(v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; G[x].emplace_back(y); } int ans = 0; for (int i = 1; i <= n; i++) { dfs(i); for (int j = 1; j <= n; j++) { if (vis[j] == true) ans++; vis[j] = false; } } cout << ans << "\n"; }
#include<iostream> #include<string> #include<vector> #include<cmath> #include<queue> #include<stack> #include<set> #include<algorithm> #include<utility> #include<map> #include<tuple> #include<deque> using namespace std; const int mod = 1000000007; const int INF = 1001001001; const long long LINF = 1001002003004005006; const double PI = acos(-1); int dir4row[] = { 1,0,-1,0 }; int dir4col[] = { 0,1,0,-1 }; int dir8row[] = { 1,1,0,-1,-1,-1,0,1 }; int dir8col[] = { 0,1,1,1,0,-1,-1,-1 }; /*-----------------------------------------------*/ int main() { int n, k; cin >> n >> k; vector<vector<int>> t(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> t[i][j]; } } vector<int> ord(n - 1); for (int i = 1; i < n; ++i) { ord[i - 1] = i; } int ans = 0; do { int now = 0; for (int i = 0; i < n - 2; ++i) { now += t[ord[i]][ord[i + 1]]; } now += t[0][ord.front()]; now += t[0][ord.back()]; if (now == k) { ans++; } } while (next_permutation(ord.begin(), ord.end())); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vl; #define f(i,n) for(i=0;i<n;i++) #define f1(i,n) for(i=1;i<n;i++) #define fr(i,n) for(i=n-1;i>=0;i--) #define em emplace_back // #define mp make_pair #define in insert #define fi first #define sc second #define b begin #define e end #define l length #define c clear #define si size #define fastio ios_base::sync_with_stdio(false);cin.tie(0); const double pi=3.141592653; const ll infi=1000000001; // const ll mod=1000000007; // const ll mod=998244353; const string no="NO\n",yes="YES\n",nl="\n"; // void dfs(vl v[],ll node,ll hai[]){ // hai[node]=1; // for(auto i:v[node]){ // if(hai[i]==0){ // dfs(v,i,hai); // } // } // } // ll recur(vl v,ll in,ll m,ll n){ // if(in==-1) return 1000000000001; // ll d,e,c; // d=max(m/v[in],n*v[in]); // c=recur(v,in-1,m/v[in],n*v[in]); // e=recur(v,in-1,m,n); // return min(c,min(e,d)); // } // ll bexpo(ll a,ll p){ // ll x=1; // while(p){ // if(p&1){ // x=(x*a)%mod; // } // a=(a*a)%mod; // p>>=1; // } // return x; // } // int dx[8]{-1,0,0,1,-1,-1,1,1}; // int dy[8]{0,-1,1,0,-1,1,-1,1}; vi v[200009]; int p[200009]; int cc=0; void dfs(int node,int par){ cc++; p[node]++; for(auto j:v[node]){ if(j!=par && p[j]==0){ dfs(j,node); } } } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif fastio int t=1; // cin>>t; ll n,m,i,l,bank,k,j,x,y,ans,mid,q; // string s; while(t--){ans=0; cin>>n; map<pll,int> mp; ll a[n]; f(i,n) cin>>a[i]; f(i,n/2){ if(a[i]!=a[n-1-i]){ x=a[i]; y=a[n-1-i]; v[x].em(y); v[y].em(x); } } f1(i,200001){ if(p[i]==0){ cc=0; dfs(i,-1); ans+=cc-1; } } cout<<ans; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10; ll mn,mx,a,t,v,add; int n,q; int main(){ scanf("%d",&n); mn=1e18,mx=-1e18; for(int i=1;i<=n;++i){ scanf("%lld%lld",&a,&t); if(t==1)add+=a,mn+=a,mx+=a; else if(t==2)mn=max(mn,a),mx=max(mx,a); else mn=min(mn,a),mx=min(mx,a); } scanf("%d",&q); while(q--){ scanf("%lld",&v); printf("%lld\n",min(mn,max(mx,v+add))); } return 0; }
#define nl "\n" #define ll long long #define ull unsigned long long #define pb push_back #define SIZE(a) (int)a.size() #define SORT(v) sort(v.begin(),v.end()) #define RSORT(v) sort(v.rbegin(),v.rend()) #define REV(v) reverse(v.begin(),v.end()) #define ff first #define ss second #define sq(a) ((a)*(a)) #define For(i,a,b) for(i=a;i<=b;i++) #define Rof(i,a,b) for(i=a;i>=b;i--) #define Rep(i,b) for(i=0;i<b;i++) #define MOD 1000000007 #define PI acos(-1.0) #define eps 1e-9 #define Linf 2e18 #define inf 1<<30 #define MX5 100005 #define MX6 1000006 #define MX3 1005 #define GCD(a,b) __gcd(a,b) #define Abs(a) abs(a) #define input(a,b) scanf("%lld%lld",&a,&b) #define in1(a) scanf("%lld",&a) #define output(a) printf("%lld\n",a); #define mem(a) memset(a,-1,sizeof(a)) // complexity O(n) #define clr(a) memset(a,0,sizeof (a)) #define mk make_pair #define pLL pair<ll,ll> #define invcos(a) (acos(a)*(180/3.14159265)) #define rtan(a) (tan(a*3.14159265/180.0)) #define pip printf("pip") #define Case(x) printf("Case %lld: ",x) #define timelimit 1.0*clock()/CLOCKS_PER_SEC #define pc_one __builtin_popcount #define pcl_one __builtin_popcountl #define pcll_one __builtin_popcountll #define parity __builtin_parity #define parityl __builtin_parityl #define parityll __builtin_parityll #define infile freopen("input.txt","r",stdin) #define outfile freopen("output.txt","w",stdout) #define debug(x) cerr << #x << " is " << x << endl #define fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0); #define minheap priority_queue<int, vector<int>, greater<int> > //const ll Linf=2e18; ///4direction -> int del_x[]={-1,0,1,0},del_y[]={0,1,0,-1}; ///8direction -> int del_x[]={-1,0,1,0,1,-1,-1,1},del_y[]={0,1,0,-1,-1,-1,1,1}; #include <bits/stdc++.h> /*#include <iomanip> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp>*/ using namespace std; /*using namespace __gnu_pbds; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; */ ll find_lcm(ll a, ll b) { return ((a * b) / __gcd(a, b));} ll bigmod(ll n, ll p) {if (p == 0) return 1; if (p == 1)return (n + MOD) % MOD; if (p % 2)return (bigmod(n, p - 1) * n + MOD) % MOD; else {ll x = bigmod(n, p / 2); return (x * x + MOD) % MOD;}} ll modinverse(ll n) {return bigmod(n, MOD - 2) % MOD;} bool check_2s_power(ll a) {return (pcll_one(a) == 1 ? true : false);} ll check_Bit(ll n, ll k) { if (n & (1LL << k)) return true; return false; } ll setBit(ll n, ll k) { return (n | (1LL << k)); } ll clearBit(ll n, ll k) { return (n & (~(1LL << k))); } ll toggleBit(ll n, ll k) { return (n ^ (1LL << k)); } /*----------------------Graph Moves----------------*/ //const int fx[]={+1,-1,+0,+0}; //const int fy[]={+0,+0,+1,-1}; //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move /*------------------------------------------------*/ int main() { ll n,sum=0,i; in1(n); for(i=1;;i++) { sum=(i*(i+1))/2; if(sum>=n) { output(i); return 0; } } }
//By: Luogu@rui_er(122461) #include <bits/stdc++.h> #define rep(x,y,z) for(ll x=y;x<=z;x++) #define per(x,y,z) for(ll x=y;x>=z;x--) #define debug printf("Running %s on line %d...\n",__FUNCTION__,__LINE__) using namespace std; typedef long long ll; const ll N = 1e5+5, mod = 1e9+7; ll n, a[N], dp[N][2], ans; template<typename T> void chkmin(T &x, T y) {if(x > y) x = y;} template<typename T> void chkmax(T &x, T y) {if(x < y) x = y;} int main() { scanf("%lld", &n); rep(i, 1, n) scanf("%lld", &a[i]); dp[1][1] = 1; rep(i, 2, n) { dp[i][1] = (dp[i-1][0] + dp[i-1][1]) % mod; dp[i][0] = dp[i-1][1]; } ans = (dp[n][0] + dp[n][1]) * a[1] % mod; rep(i, 2, n) ans = (ans + (dp[i][1] * dp[n-i+2][1] % mod - dp[i][0] * dp[n-i+2][0] % mod + mod) % mod * a[i] % mod) % mod; printf("%lld\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, k, n) for (int i = k; i < (int)(n); i++) #define repd(i, n) for (int i = n-1; i >= 0; i--) #define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--) #define all(x) (x).begin(),(x).end() #define chmax(x,y) x=max(x,y) #define chmin(x,y) x=min(x,y) #define F first //pairの一つ目の要素 #define S second //pairの二つ目の要素 #define PB push_back //挿入 #define MP make_pair //pairのコンストラクタ //V,Pは大文字i,l,bは小文字 using ll = long long; using Vi = vector<int>; using VVi = vector<Vi>; using Vl = vector<ll>; using VVl = vector<Vl>; using Vb = vector<bool>; using VVb = vector<Vb>; using P = pair<int,int>; using Pl = pair<ll, ll>; using Vs = vector<string>; const ll mod = 1000000007; const ll inf = 1000000000000000000;//10の18乗 #define yn {puts("Yes");}else{puts("No");} #define dame { puts("-1"); return 0;} int main() { ll n; cin >> n; Vl v(n); rep(i,n) cin >> v[i]; ll now=0;//操作回数 Vl whe(n); rep(i,n) whe[v[i]-1]=i; Vl ans;//あとでインクリメント Vl used(n-1,0); rep(i,n-1){ ll x=whe[i]; if(x<=i) continue; //ll y=x-i; for(int j=x-1;j>=i;j--){ ans.PB(j); if(used[j])dame; used[j]++; now++; //cout << j << endl; swap(v[j],v[j+1]); whe[v[j]-1]=j; whe[v[j+1]-1]=j+1; //cout <<j<< endl; } } if(now!=n-1) dame; rep(i,n-1) cout << ans[i]+1 << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template <typename T> void update_max(T& a, T b) { if (b > a) a = b; } template <typename T> void update_min(T& a, T b) { if (b < a) a = b; } int main() { #ifdef LOCAL_PROJECT freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); int N, M; ll INF = 100000000000; cin >> N >> M; vector<vector<int>> graph(N); rep(i, 0, M) { int A, B; cin >> A >> B; A--; B--; graph[A].push_back(B); graph[B].push_back(A); } int K; cin >> K; vector<vector<ll>> dist(K, vector<ll>(K, INF)); vector<int> C(K); unordered_map<int, int> C_map; rep(i, 0, K) { cin >> C[i]; C[i]--; C_map[C[i]] = i; } rep(i, 0, K) { dist[i][i] = 0; } rep(i, 0, K) { // BFS queue<pair<int, int>> Q; // {index, depth} vector<bool> visited(N, false); Q.push({C[i], 0}); visited[C[i]] = true; while (!Q.empty()) { pair<int, long long> node = Q.front(); Q.pop(); for (int child : graph[node.first]) { if (!visited[child]) { if (C_map.count(child) > 0) { dist[C_map[C[i]]][C_map[child]] = min( dist[C_map[C[i]]][C_map[child]], node.second + 1); } visited[child] = true; Q.push({child, node.second + 1}); } } } } rep(i, 0, K) { rep(j, 0, K) { if (dist[i][j] == INF) { cout << -1; return 0; } } } vector<vector<ll>> dp(K, vector<ll>(1 << K, INF)); rep(i, 0, K) { dp[i][1 << i] = 0; } rep(mask, 0, 1 << K) { rep(i, 0, K) { if ((mask & (1 << i)) != 0) { rep(j, 0, K) { if (i != j && (mask & (1 << j)) != 0) { // dp[j][mask ^ (1 << i)] != INF && // dp[j][mask ^ (1 << i)] + dist[i][j] < dp[i][mask] dp[i][mask] = min(dp[i][mask], min(dp[j][mask ^ (1 << i)] + dist[i][j], dp[j][mask] + dist[i][j])); } } } } } ll answer = INF; rep(i, 0, K) { answer = min(answer, dp[i][(1 << K) - 1]); } cout << answer + 1; return 0; }
#include<bits/stdc++.h> #define Woody #ifdef Woody #define quick ios::sync_with_stdio(0);cin.tie(0); #else #define quick #endif #define int long long #define rep(n) for(int i=0;i<n;i++) #define max3(a,b,c) max(max(a,b),c) #define min3(a,b,c) min(min(a,b),c) #define mp make_pair #define vi vector<int> #define eb emplace_back #define F first #define S second using namespace std; signed main(){ quick int X; while(cin>>X){ int k=X/100+1; cout<<k*100-X<<"\n"; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; cin>>n; set<ll> st; for(ll a = 2; a * a <= n; a++) { for(ll b = a * a; b <= n; b *= a) { st.insert(b); } } cout<<n - (ll)st.size(); }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define x first #define y second #define FOR(i, m, n) for (ll i(m); i < n; i++) #define DWN(i, m, n) for (ll i(m); i >= n; i--) #define REP(i, n) FOR(i, 0, n) #define DW(i, n) DWN(i, n, 0) #define F(n) REP(i, n) #define FF(n) REP(j, n) #define D(n) DW(i, n) #define DD(n) DW(j, n) using ll = long long; using ld = long double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vtll = vector<tll>; using gr = vector<vll>; using wgr = vector<vpll>; void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); } void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); } template<typename T,typename U> ostream& operator<<(ostream& os, const pair<T,U>& p) { cerr << ' ' << p.x << ',' << p.y; return os; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for(auto x: v) os << ' ' << x; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for(auto x: v) os << ' ' << x; return os; } template<typename T,typename U> ostream& operator<<(ostream& os, const map<T,U>& v) { for(auto x: v) os << ' ' << x; return os; } struct d_ { template<typename T> d_& operator,(const T& x) { cerr << ' ' << x; return *this;} } d_t; #define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; } #define deb(X ...) dbg(#X, "=", X); #define EPS (1e-10) #define INF (1LL<<61) #define YES(x) cout << (x ? "YES" : "NO") << endl; #define CL(A,I) (memset(A,I,sizeof(A))) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() ll mod(ll a, ll b) { return ((a%b)+b)%b; } ll extended_euclid(ll a,ll b,ll &x,ll &y) { if (a == 0) { x = 0; y = 1; return b;} ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } ll mod_inverse(ll a, ll n) { ll x, y; ll d = extended_euclid(a, n, x, y); if (d > 1) return -1; return mod(x,n); } void linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) { ll d = __gcd(a,b); if (c%d) { x = y = -INF; } else { x = c/d * mod_inverse(a/d, b/d); y = (c-a*x)/b; } } void solve() { ll x,y,p,q; cin >> x >> y >> p >> q; ll u = 2*(x+y); ll v = p+q; ll ret = INF; FOR(X,x,x+y) FOR(Y,p,p+q) { ll k,l; linear_diophantine(u,-v,Y-X, k,l); if(k==-INF && l==-INF) continue; ll g = __gcd(u,v); ll s = v/g; if(k<0) k += ((-k+s-1)/s)*s; else k-= s*(k/s); ret = min(ret, k*u+X); } cout << (ret==INF?"infinity":to_string(ret)) << endl; } int main(void) { ios_base::sync_with_stdio(false); ll _T; cin >> _T; REP(_,_T) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; tuple<long long, long long, long long> ext_gcd(long long a, long long b) { if (b == 0) return {a, 1, 0}; auto [gcd, y, x] = ext_gcd(b, a%b); y -= a/b * x; return {gcd, x, y}; // a*x + b*y = gcd } pair<long long,long long> crt(const vector<long long> &r, const vector<long long> &m) { /* return x that satisfies x = a mod b, for a in r, for b in m */ /* O(n log lcm(m[i]))*/ assert(r.size() == m.size()); auto mod=[&](long long a,long long k){return (a%k+k)%k;}; // a mod k for negative a long long y = 0, lcm = 1; int n = r.size(); for(int i=0; i<n; ++i) { assert(m[i] > 0); auto [g, p, q] = ext_gcd(lcm, m[i]); long long ry = mod(r[i],m[i]) - y; if(ry % g) return make_pair(0,0); // no solution exists long long s = ry / g * p % (m[i]/g); y = mod(y+lcm*s, lcm*(m[i]/g)); lcm *= m[i] / g; } return make_pair(y, lcm); // x = y mod lcm } int main() { int T; cin >> T; while(T--) { int x,y,p,q; cin >> x >> y >> p >> q; const long long INF = 2e18; long long ans = INF; for(int a=x; a<x+y; ++a) for(int b=p; b<p+q; ++b) { // t = a mod (2x+2y) // t = b mod (p+q) vector<long long> r(2),m(2); r[0] = a, r[1] = b, m[0] = 2*(x+y), m[1] = p+q; auto [t,lcm] = crt(r,m); if(lcm) ans = min(ans,t); } if (ans == INF) cout << "infinity" << endl; else cout << ans << endl; } }
#include<bits/stdc++.h> using namespace std; int mod = 998244353; long long dp[30][5005]; long long fac[5005],finv[5005],inv[5005]; void COMinit() { fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1; for(int i = 2; i <= 5000; i++) { fac[i] = fac[i-1]*i%mod; inv[i] = mod-inv[mod%i]*(mod/i)%mod; finv[i] = finv[i-1]*inv[i]%mod; } } long long COM(int n,int k) { if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac[n]*(finv[k]*finv[n-k]%mod)%mod; } int main() { int N,M; cin >> N >> M; dp[0][0] = 1; COMinit(); for(int i = 0; i < 13; i++) { for(int j = 0; j <= N; j+=2) { for(int k = 0; k <= M; k++) { long long l = k+(j << i); if(l <= M) { dp[i+1][l] += dp[i][k]*COM(N,j)%mod; dp[i+1][l] %= mod; } } } } cout << dp[13][M] << endl; }
#include<cstdio> #define fo(i,a,b) for(i=a;i<=b;i++) #define fd(i,a,b) for(i=a;i>=b;i--) #define ll long long #define I int #define R read() #define V void using namespace std; const I N=5005,mo=998244353; I i,j,k,n,m,mc,num; I dl[13]; ll f[14][N]; I jc[13]; I jc2[N],ny[N]; ll ch; I R { I x=0,ef=1;char s=getchar(); while ((s<'0')||(s>'9')){if (s=='-')ef=-1;s=getchar();} while ((s>='0')&&(s<='9')) x=(x<<1)+(x<<3)+(s-'0'),s=getchar(); return x*ef; } I ksm(I x,I y) { ll l=x,r=1;I z=y; while (z) { if (z%2==1)r=r*l%mo; l=l*l%mo;z=z/2; } return r; } I C(I x,I y) { ch=jc2[x];ch=ch*ny[y]%mo*ny[x-y]%mo;return ch; } I main() { n=R;m=R;mc=m; while (mc){num++;dl[num]=mc%2;mc/=2;} jc[0]=1;fo(i,1,12){jc[i]=jc[i-1]*2;} ch=1;jc2[0]=1;ny[0]=1;fo(i,1,n){ch=ch*i%mo;jc2[i]=ch;} ch=ksm(ch,mo-2);ny[n]=ch; fd(i,n-1,1){ch=ch*(i+1)%mo;ny[i]=ch;} f[0][m]=1; fo(i,0,num-1) { fd(j,m,0) { if (f[i][j]) { fo(k,0,n/2) { if (j/jc[i]<k*2)break; f[i+1][j-jc[i]*k*2]=(f[i+1][j-jc[i]*k*2]+f[i][j]*C(n,k*2))%mo; } } } } printf("%d\n",f[num][0]); fclose(stdin); fclose(stdout); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long N; cin >> N; set<long long> st; for (long long i = 2; i * i <= N; i++){ long long c = i; while (true){ c *= i; if (c > N){ break; } st.insert(c); } } cout << N - st.size() << endl; }
#include<bits/stdc++.h> #include<iostream> #include<set> #include<map> #include<iterator> #define ll long long #define lli long long int #define pb push_back #define mp make_pair #define RIP(i,n) for(int i=0; i<n; i++) #define F(i,a,b) for(int i=a; i<b; i++) #define RIP1(i,n) for(int i=n-1; i>=0; i--) #define FOR(i,a,b) for(int i=a;i<(b); i++) #define FOR1(i,a,b) for(int i=a; i>=(b); i--) #define sc(a) scanf("%lld",&a) #define SC(a) scanf("%d",&a) #define cin(a) cin >> a #define cout(a) cout << a #define pi acos(-1) #define pr(a) printf("%lld\n",a) #define PR(a) printf("%lld ",a) #define s(a,b) sort(a,b) #define sz(x) (int)(x).size() #define nl '\n' #define Max 10000000000 #define mod 1e9+7 using namespace std; void F_I_O() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { ll n; cin >> n; ll ans=0; ll cnt=0; map<ll,ll>m; //set<ll>s; for(ll i=2; i*i<=n; i++) { ll r = sqrt(i); if(r*r!=i) { for(ll j=i*i; j<=n; j = j*i) { if(m[j]==0) { ans++; m[j]=1; } } } } /* for(ll i=2; i*i<=n; i++) { ll r = sqrt(i); if(r*r!=i) { double e = double(n); double f = double(i); ll p = (double)log(e)/(double)log(f); p++; ll g = pow(i,p); cout << p << ' ' << g << nl; if(g<=n) { ans+=(p-1); } else { ans+=(p-2); } } }*/ n-=ans; cout << n<< nl; }
#include"bits/stdc++.h" using namespace std; vector<string> split(const string & s, char c=' ') { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } template<typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; s << "["; for(int i = 0; i < n - 1; i++) s << arr[i] << ","; s << arr[n - 1] << "]"; return s.str(); } #define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);} inline void __evars_begin(int line) { cerr << "#" << line << ": "; } template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); } template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); } template<typename T> inline void __evars_out_var(T val) { cerr << val; } inline void __evars(vector<string>::iterator it) { cerr << endl; } template<typename T, typename... Args> inline void __evars(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << "="; __evars_out_var(a); cerr << "; "; __evars(++it, args...); } template<typename T> void maxa(T &a,T b){ if(a<b) a=b; } template<typename T> void mina(T &a,T b){ if(a>b) a=b; } #define int long long const int inf=1e18; #define F first #define S second #define Z(c) (int)(c).size() #define pii pair<int,int> const int mod=1e9+7; const int N=3e5+15; int n,k,tt; int a[808][808]; int c[808][808]; bool chk(int m){ for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ if(a[i][j]<=m) c[i][j]=1; else c[i][j]=0; c[i][j]+=c[i-1][j]+c[i][j-1]-c[i-1][j-1]; } } for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ if(i+k-1<=n&&j+k-1<=n){ int sm=c[i+k-1][j+k-1]+c[i-1][j-1]-c[i+k-1][j-1]-c[i-1][j+k-1]; // EVARS(i,j,sm); if(sm>=tt) return true; } } } return false; } void boomer(){ cin>>n>>k; tt=(k*k)/2; tt=k*k-tt; for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ cin>>a[i][j]; } } int lw=0,hg=1e9,mid; // EVARS(chk(3)); while(lw<hg){ mid=(lw+hg)/2; if(chk(mid)) hg=mid; else lw=mid+1; } cout<<hg; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc=1; // cin>>tc; for(int i=1;i<=tc;++i) { // cout<<"Case #"<<i<<": "; boomer(); if(i<tc) cout<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long typedef pair <int, int> ii; typedef pair <ll, int> lli; #define ar array #define pb push_back #define fi first #define se second #define sz size #define mp make_pair #define nl '\n' #define all(x) (x).begin(), (x).end() #define int_max numeric_limits<int>::max() #define int_min numeric_limits<int>::min() #define ll_max numeric_limits<ll>::max() #define ll_min numeric_limits<ll>::min() #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define im(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // custom sort example //sort(all(a), [ ]( const auto& c, const auto& d) { //if (c.fi.se != d.fi.se) { //return c.fi.se < d.fi.se; // lower first //} //if (c.fi.fi != d.fi.fi) { //return c.fi.fi > d.fi.fi; // higher first //} //return c.se > d.se; // higher first //}); struct chash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template <class T> auto lbf(const std::set<std::pair<T, T>>& s, T first) { static constexpr T min = std::numeric_limits<T>::min(); return s.lower_bound({first, min}); } int MOD = 1000000007; ll pw(ll n, ll k) { ll r = 1; for(; k; k >>= 1) { if(k&1) r = r * n % MOD; n = n * n % MOD; } return r; } ll inv(ll n) { return pw(n, MOD-2); } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif //vector<vector<int>> h(100, vector<int>()); vector<vector<vector<double>>> a(101, vector<vector<double>>(101, vector<double>(101))); int b, c, d; cin >> b >> c >> d; for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { for (int k = 0; k < 101; k++) { a[i][j][k] = 0; } } } a[b][c][d] = 1; for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 100; k++) { if (a[i][j][k] == 0) { continue; } double pom = (double) i / (double) (i + j + k); a[i + 1][j][k] = max(a[i + 1][j][k], 0.0) + pom * a[i][j][k]; pom = (double) j / (double) (i + j + k); a[i][j + 1][k] = max(a[i][j + 1][k], 0.0) + pom * a[i][j][k]; pom = (double) k / (double) (i + j + k); a[i][j][k + 1] = max(0.0, a[i][j][k + 1]) + pom * a[i][j][k]; } } } double res = 0; for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { res = res + a[100][i][j] * ((100 - b) + (i - c) + (j - d)); } } for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { res = res + a[i][100][j] * ((100 - c) + (i - b) + (j - d)); } } for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { res = res + a[i][j][100] * ((100 - d) + (i - b) + (j - c)); } } //debug() << a; cout << setprecision(10); cout << res << nl; }
#include <bits/stdc++.h> #include <chrono> #include <ctime> using namespace std; using namespace std::chrono; using i64 = long long; using P = pair<int, int>; using vec = vector<int>; using mat = vector<vector<int>>; #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define endl "\n" constexpr int MOD = 1000000007; constexpr int INF = 1001001001; constexpr bool is_multiple = false; struct Rect { int x1, y1, x2, y2; Rect() {} Rect(int a, int b, int c, int d) { x1 = a; y1 = b; x2 = c; y2 = d; } int get_area() { return (x2 - x1) * (y2 - y1); } bool is_valid() { return x1 < x2 && y1 < y2; } }; Rect merge(Rect a, Rect b) { return (Rect){max(a.x1, b.x1), max(a.y1, b.y1), min(a.x2, b.x2), min(a.y2, b.y2)}; } // 共通部分が存在しない判定 // 計算量: O(n) bool is_ok(vector<Rect>& v, int i) { int n = v.size(); bool flag = true; rep(j, n) { if (i == j) continue; auto mg = merge(v[i], v[j]); if (mg.is_valid()) flag = false; } return flag; } // スコア計算 // 計算量: O(n) i64 calc_score(vector<Rect>& v, vec& r) { int n = r.size(); double res = 0; rep(i, n) { int s = v[i].get_area(); double t = (double)(min(r[i], s)) / max(r[i], s); res += 1 - (1 - t) * (1 - t); } res *= 1000000000; res /= n; i64 ret = round(res); return ret; } static uint32_t randXor() { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } static double next_double() { return (randXor() + 0.5) * (1.0 / UINT_MAX); } constexpr int expand_diff = 1; bool modify(vector<Rect>& rects, vec& r, double prog) { int n = rects.size(); int i = randXor() % n; // if (rects[i].get_area() >= r[i]) return; int side = randXor() % 4; if (side == 0 && rects[i].x1-expand_diff >= 0) rects[i].x1 -= expand_diff; else if (side == 1 && rects[i].y1-expand_diff >= 0) rects[i].y1 -= expand_diff; else if (side == 2 && rects[i].x2+expand_diff <= 10000) rects[i].x2 += expand_diff; else if (side == 3 && rects[i].y2+expand_diff <= 10000) rects[i].y2 += expand_diff; return is_ok(rects, i); } void solve() { int n; cin >> n; vec x(n), y(n), r(n); rep(i, n) cin >> x[i] >> y[i] >> r[i]; vector<Rect> rects(n); // 初期化 rep(i, n) { rects[i] = (Rect){x[i], y[i], x[i]+1, y[i]+1}; } // 焼きなまし auto startClock = system_clock::now(); const double start_temp = 1250; const double end_temp = 50; const double end_time = 4.8; double time = 0.0; do { const double progress_ratio = time / end_time; auto new_rects = rects; bool flag = modify(new_rects, r, progress_ratio); if (!flag) continue; auto new_score = calc_score(new_rects, r); auto pre_score = calc_score(rects, r); auto delta_score = new_score - pre_score; double temp = start_temp + (end_temp - start_temp) * progress_ratio; double prob = exp(delta_score / temp); if (prob > next_double()) { rects = new_rects; } time = (duration_cast<microseconds>(system_clock::now() - startClock).count() * 1e-6); } while (time < end_time); // printf("\n"); rep(i, n) printf("%d %d %d %d\n", rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2); // auto l_sc = calc_score(rects, r); // cout << fixed << setprecision(15) << l_sc * 1e-9 << endl; } int main() { int t = 1; if (is_multiple) cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> #define CIT_MARK ios_base::sync_with_stdio(false);cin.tie(nullptr); #define ll long long #define pll pair<ll,ll> #define pb push_back #define M 1000000007 #define lc '\n' using namespace std; int main() { CIT_MARK ll n; cin>>n; ll a[n][5]; for(ll i=0;i<n;i++) { for(ll j=0;j<5;j++) { cin>>a[i][j]; } } auto check=[&](ll mid){ set<ll> st; for(ll i=0;i<n;i++) { ll val=0; for(ll j=0;j<5;j++) { if(a[i][j]>=mid) { val+=pow(2LL,j); } } st.insert(val); } for(auto i:st) { for(auto j:st) { for(auto k:st) { if((i | j | k)== 31) { return true; } } } } return false; }; ll l=0,r=1e9; ll ans=0; while(l<=r) { ll mid=(l+r)/2; bool is=check(mid); if(is) { ans=mid; l=mid+1; } else { r=mid-1; } } cout<<ans<<lc; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ll long long int main(){ ll n; cin >> n; vector<ll> a(n); rep(i,n){ cin >> a.at(i); } ll ans=0,maxi=0; rep(i,n){ ll mini=1e8; for(ll j=i;j<n;j++){ mini=min(mini,a.at(j)); maxi=mini*(j-i+1); ans=max(ans,maxi); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; long long arr[100005]; int s[100005], top=0; int l[100005], r[100005]; int main(){ int n; cin >> n; arr[0] = arr[n+1] = -1; for (int i=1; i<=n; i++) cin >> arr[i]; for (int i=1; i<=n; i++){ while (arr[s[top]]>=arr[i]) top--; l[i] = i-s[top]; s[++top]=i; } top=0; s[0]=n+1; for (int i=n; i>=1; i--){ while (arr[s[top]]>=arr[i]) top--; r[i] = s[top]-i; s[++top]=i; } long long ans=0; for (int i=1; i<=n; i++){ ans = max(ans, arr[i]*(l[i]+r[i]-1)); } cout << ans; return 0; }
#include<bits/stdc++.h> using namespace std; #define endl '\n' typedef long long int ll; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for(auto it = d.b;it != d.e;++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int a, b;cin >> a >> b; double ans = double(a - b) / (double)a; printf("%.9f\n", ans * 100.0); }
/* Author : MatsuTaku Date : 03/27/21 Certificate: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDF9T447OEo8XSQ6O1AznN5tKC8mzvYc4Zs3+oOKfMqgLXfOwQnpfcoxKs4MAP1eICBD13PkcIezJ9IlV6nKQZKs1BQmvjSXJ+zKK8YCKvAueNPuNO0Bim43IBaNHNFWcMvcmUvHgRb6hUSO0V8I7OsjiFo20KDBj3gAznu9tir0Q== CompetitiveProgrammingCertification:[email protected] */ #include <bits/stdc++.h> using namespace std; #include <x86intrin.h> #define REP(i, f, n) for (auto i = (f), i##_len = (n); i < i##_len; i++) #define rep(i, n) REP(i, (int) 0, n) #define RREP(i, f, n) for (auto i = (n)-1, i##_left = (f); i >= i##_left; i--) #define rrep(i, n) RREP(i, (int) 0, n) using lint = long long int; using ulint = unsigned long long int; template<typename T> using vvec = vector<vector<T>>; template<typename T> vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); } template<typename T> T& chmax(T& dst, T x) { return dst = max(dst, x); } template<typename T> T& chmin(T& dst, T x) { return dst = min(dst, x); } class Solver { public: Solver(); void solve(); }; Solver::Solver() {} void Solver::solve() { int n,m; cin>>n>>m; vector<pair<int,char>> G[n]; vector<pair<int,int>> E(m); auto id = [n](int a, int b) { if (a > b) swap(a,b); return a*n+b; }; rep(i, m) { int a,b; char c; cin>>a>>b>>c; a--; b--; G[a].emplace_back(b,c-'a'); G[b].emplace_back(a,c-'a'); E[i] = {a,b}; } vector<array<vector<int>, 26>> T(n); rep(i, n) { for (auto [t,c] : G[i]) { T[i][c].push_back(t); } } vector<int> W[n*n]; rep(i, n) { REP(j, i, n) { auto s = id(i, j); rep(k, 26) { auto& l = T[i][k]; auto& r = T[j][k]; if (l.empty() or r.empty()) continue; for (auto u : l) { for (auto v : r) { auto t = id(u, v); W[s].push_back(t); } } } } } queue<int> qs; constexpr int INF = 1e9; vector<int> dist(n*n, INF); qs.push(id(0, n-1)); dist[id(0, n-1)] = 0; while (!qs.empty()) { auto s = qs.front(); qs.pop(); auto d = dist[s]; for (auto t:W[s]) { if (dist[t] <= d+1) continue; dist[t] = d+1; qs.push(t); } } int ans = INF; rep(i, n) { chmin(ans, dist[id(i, i)]*2); } for (auto [a,b] : E) { chmin(ans, dist[id(a, b)]*2+1); } cout << (ans != INF ? ans : -1) << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); Solver solver; int t = 1; // cin>>t; while (t--) { solver.solve(); } return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define ins insert template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 200001; //check the limits, dummy const ll identity = 0; const ll SZ = 131072*2; ll sum[2*SZ], lazy[2*SZ]; ll combine(ll A, ll B) { return A+B; } ll combineUpd(ll A, ll B) { return A+B; } void push(int index, ll L, ll R) { sum[index] = combineUpd(sum[index], lazy[index]); if (L != R) lazy[2*index] = combineUpd(lazy[2*index], lazy[index]), lazy[2*index+1] = combineUpd(lazy[2*index+1], lazy[index]); lazy[index] = identity; } void pull(int index) { sum[index] = combine(sum[2*index], sum[2*index+1]); } ll query(int lo, int hi, int index = 1, ll L = 0, ll R = SZ-1) { push(index, L, R); if (lo > R || L > hi) return identity; if (lo <= L && R <= hi) return sum[index]; int M = (L+R) / 2; return combine(query(lo, hi, 2*index, L, M), query(lo, hi, 2*index+1, M+1, R)); } void update(int lo, int hi, ll increase, int index = 1, ll L = 0, ll R = SZ-1) { push(index, L, R); if (hi < L || R < lo) return; if (lo <= L && R <= hi) { lazy[index] = increase; push(index, L, R); return; } int M = (L+R) / 2; update(lo, hi, increase, 2*index, L, M); update(lo, hi, increase, 2*index+1, M+1, R); pull(index); } vector<vi> graph(MX); int tin[MX], tout[MX], ct = 0; void dfs(int v, int p) { tin[v] = ct; ct++; trav(a, graph[v]) { if (a == p) continue; dfs(a, v); } tout[v] = ct-1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector<pi> edges; F0R(i, N-1) { int X, Y; cin >> X >> Y; X--; Y--; graph[X].pb(Y); graph[Y].pb(X); edges.pb({X, Y}); } dfs(0, 0); int Q; cin >> Q; while(Q--) { int T, E, X; cin >> T >> E >> X; E--; int A = edges[E].f, B = edges[E].s; if (T == 2) swap(A, B); if (tin[B] < tin[A]) { update(tin[A], tout[A], X); } else { update(0, SZ-1, X); update(tin[B], tout[B], -X); } } F0R(i, N) { cout << query(tin[i], tin[i]) << nl; } return 0; } // read the question correctly (ll vs int) // template by bqi343
#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>; vector<vector<int>> es; vector<int> depth; void depth_dfs(int v, int pv=-1, int d=0) { depth[v] = d; for (int nv : es[v]) { if (nv == pv) continue; depth_dfs(nv, v, d+1); } } vector<ll> dp; void imos_dfs(int v, int pv=-1) { if (pv>=0) dp[v] += dp[pv]; for (int nv : es[v]) { if (nv == pv) continue; imos_dfs(nv, v); } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; es.resize(n); vector<int> a(n-1), b(n-1); rep(i,n-1) { cin >> a[i] >> b[i]; a[i]--; b[i]--; es[a[i]].push_back(b[i]); es[b[i]].push_back(a[i]); } depth.resize(n); depth_dfs(0); int q; cin >> q; dp.resize(n+5); rep(qi, q) { int t,e,x; cin >> t >> e >> x; e--; if (t == 1) { if (depth[a[e]] < depth[b[e]]) { dp[0] += x; dp[b[e]] -= x; } else { dp[a[e]] += x; } } else { if (depth[a[e]] < depth[b[e]]) { dp[b[e]] += x; } else { dp[0] += x; dp[a[e]] -= x; } } } imos_dfs(0); rep(i,n) cout << dp[i] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++) #define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) #define setp(n) fixed << setprecision(n) template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define ins insert #define debug(a) cerr<<(a)<<endl #define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl #define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;} using namespace std; template<class A, class B> ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";} template<class A, class B> istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;} template<class T> vector<T> make_vec(size_t a){ return vector<T>(a); } template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } /* Some Libraries */ //------------------------------------------------- ll comb[61][32]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll A,B,K; cin>>A>>B>>K; comb[0][0] = 1; rep(i,60)rep(j,31){ comb[i+1][j]+=comb[i][j]; comb[i+1][j+1]+=comb[i][j]; } int len = A+B; rep(i,len){ if (A>0 && K<=comb[A+B-1][A-1]){ cout<<'a'; A--; }else{ if (A>0) K-=comb[A+B-1][A-1]; cout<<'b'; B--; } } cout<<"\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define pb push_back #define F first #define S second #define um map <ll,ll> #define rep(i,z,n) for(int i=z;i<n;i++) #define repi(i,z,n) for(int i=z;i<=n;i++) #define repn(i,z,n) for(int i=n-1;i>=z;i--) #define vec vector<ll> #define vecp vector<pair<ll,ll>> #define pi (double)3.14159265358979323846 #define ld long double #define all(z) z.begin(),z.end() ll power(ll a, ll b, ll m) { ll ans = 1; a = a % m; if (a == 0) return 0; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } ll modInverse(ll a, ll m) {return power(a, m - 2, m);} ll binpow(ll a, ll b) {ll res = 1; while (b > 0) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;} ll binlog2(ll n) {ll logValue = -1; while (n) {logValue++; n >>= 1;} return logValue;} ll ceil(ll t1, ll t2) {ll ans = (t1 + t2 - 1) / t2; return ans;} void print(vec a) {rep(i, 0, a.size())cout << a[i] << " "; cout << endl;} const ll mod = 1e9 + 7; const ll N = 2e3 + 5; const ll inf = 2e18; ll n, m; char a[N][N]; ll dp[N][N]; ll ph[N][N]; ll pv[N][N]; ll pd[N][N]; void recur(ll i, ll j) { if (i == 1 && j == 1) { return; } if (i <= 0 || j <= 0 || a[i][j] == '#') return; if (dp[i][j] != -1) return ; recur(i - 1, j); recur(i, j - 1); recur(i - 1, j - 1); dp[i][j] = (ph[i - 1][j] + pv[i][j - 1] + pd[i - 1][j - 1]) % mod; ph[i][j] = dp[i][j]; pv[i][j] = dp[i][j]; pd[i][j] = dp[i][j]; ph[i][j] += ph[i - 1][j] ; pv[i][j] += pv[i][j - 1] ; pd[i][j] += pd[i - 1][j - 1]; ph[i][j] %= mod; pv[i][j] %= mod; pd[i][j] %= mod; } void solve() { cin >> n >> m; rep(i, 1, n + 1) rep(j, 1, m + 1) { cin >> a[i][j]; } repi(i, 0, n + 3) repi(j, 0, m + 3) dp[i][j] = -1; dp[1][1] = 1; ph[1][1] = 1; pd[1][1] = 1; pv[1][1] = 1; recur(n, m); cout << dp[n][m] << endl; // repi(i, 1, n) { // repi(j, 1, m) { // cout << dp[i][j] << " "; // } // cout << endl; // } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif speed; int test = 1; // init(); // cin >> test; // ll t1 = 1; while (test--) { // cout << "Case #" << t1 << ": "; solve(); // t1++; } return 0; }
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; #define mod 1000000007 ll gcd(ll x,ll y) {return y ? gcd(y,x%y) : x;} ll lcm(ll x,ll y) {return x/gcd(x,y)*y;} struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { 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; } }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} //組み合わせ計算 mint choose(int n, int a) { mint x = 1, y = 1; rep(i,a) { x *= n-i; y *= i+1; } return x / y; } //繰り返し二乗法 mint f(int n) { if (n == 0) return 1; mint x = f(n/2); x *= x; if (n%2 == 1) x *= 2; return x; } int main(){ ll h,w; cin >> h >> w; char grid[2005][2005]; rep(i,h)rep(j,w){ cin >> grid[i][j]; } mint kazu[2005][2005]; rep(i,h){ rep(j,w){ kazu[i][j]=0; } } kazu[0][0]=1; mint houkou[2005][2005][3]; rep(i,h){ rep(j,w){ rep(k,3){ houkou[i][j][k]=0; } } } houkou[0][0][0]=0; houkou[0][0][1]=0; houkou[0][0][2]=0; for(ll i=0;i<h;i++){ for(ll j=0;j<w;j++){ if(i!=0||j!=0){ if(grid[i][j]=='.'){ kazu[i][j]=houkou[i][j][0]+houkou[i][j][1]+houkou[i][j][2]; } } if(grid[i][j]=='#') continue; houkou[i+1][j][0]=houkou[i][j][0]+kazu[i][j]; houkou[i+1][j+1][1]=houkou[i][j][1]+kazu[i][j]; houkou[i][j+1][2]=houkou[i][j][2]+kazu[i][j]; } } cout << kazu[h-1][w-1] << endl; return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define int long long #define ll int #define ull unsigned long long #define all(a) a.begin(),a.end() #define rev(a) a.rbegin(),a.rend() typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset #define ar array #define pb push_back #define fi(a,b) for(int i=a;i<(b);i++) #define fj(a,b) for(int j=a;j<(b);j++) #define fk(a,b) for(int k=a;k<(b);k++) const double pi=acosl(-1); int x,y; map<int,int> mapka; int rec(int val) { if(mapka.find(val)!=mapka.end()) return mapka[val]; if(val<=x) { return mapka[val]=x-val; } mapka[val]=val-x; if(val%2==0) { mapka[val]=min(mapka[val],1+rec(val/2)); } else { mapka[val]=min({mapka[val],2+rec((val+1)/2),2+rec((val-1)/2)}); } return mapka[val]; } void solve() { cin>>x>>y; if(x>=y) { cout<<x-y<<'\n'; return ; } cout<<rec(y); } signed main() { FAST; int tt=1; // cin>>tt; while(tt--) { solve(); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define _GLIBCXX_DEBUG #define rep(i, n) for(int i = 0; i < (int)n; i++) #define v(a, n) vector<int> a(n, 0) #define all(v) v.begin(), v.end() #define F first #define S second #define print(v) rep(i, (int)v.size()) cout << v[i] << (i+1 == (int)v.size() ? "\n" : " "); #define rfor(i, a, b) for (int i = a; i < b; i++) const vector<int> maze = {0, 1, 0, -1, 0}; const int mod = 1e9+7; ll gcd(ll a, ll b) {return b == 0 ? abs(a) : gcd(b, a%b);} ll lcm(ll a, ll b) {return a*b/gcd(a, b);} ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } else { ll d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } } ll modpow(ll x, ll n, ll p) { ll ans = 1; while (n > 0) { if (n&1) ans = ans*x%p; x = x*x%p; n >>= 1; } return ans; } vector<vector<int>> g; vector<int> depth; void dfs(int par, int son){depth[son]=depth[par]+1;for(int i : g[son]){if(i==par)continue;dfs(son, i);}} int main() {int n; cin >>n;vector<pair<int, int>>edge(n-1);g.resize(n);rep(i,n-1){int a,b;cin>>a>>b;a--;b--;edge[i].F=a;edge[i].S=b;g[a].push_back(b);g[b].push_back(a);}int q;cin>>q;vector<vector<int>> query(q,vector<int>(3));rep(i,q)cin>>query[i][0]>>query[i][1]>>query[i][2];depth.resize(n);depth[0]=0;for(int i : g[0])dfs(0,i);vector<ll> cnt(n,0);rep(i,q){int t=query[i][0],c=query[i][2];int a,b;if(t==1){a=edge[query[i][1]-1].F;b=edge[query[i][1]-1].S;}else{a=edge[query[i][1]-1].S;b=edge[query[i][1]-1].F;}if(depth[a]<depth[b]){cnt[0]+=c;cnt[b]-=c;}else{cnt[a]+=c;}}vector<bool> seen(n,false);seen[0]=true;vector<ll> ans(n, 0);ans[0]=cnt[0];queue<int> que;que.push(0);while(!que.empty()){int x=que.front();que.pop();for(int i:g[x]){if(seen[i])continue;seen[i]=true;ans[i]=ans[x]+cnt[i];que.push(i);}}print(ans); }
#include <bits/stdc++.h> using namespace std; #define int long long //#define int unsigned int #define mod 1000000007 #define double long double //int d; int p[200005]; vector <int> adj[200005]; int val[200005]; vector <int> vis(200005); //vector <vector <int>> v(200005); /*int fun(int a,int b){ if(b==0)return 1; else if(a==1)return 1; int temp=fun(a,b/2)%mod; temp=(temp*temp)%mod; if(b%2==1)temp=(temp*a)%mod; return temp%mod; } int inv(int a,int b){ return (a*fun(b,mod-2))%mod; } void dfs(int s){ if(vis[s])return; val[s]+=val[p[s]]; vis[s]=1; for(auto u:adj[s]){ dfs(u); } } */ signed main() { /* #ifndef ONLINE_JUDGE freopen("inputhsr.txt", "r", stdin); freopen("outputhsr.txt", "w", stdout); #endif */ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int i,j,k=0,n,t=1,m,l=0; //cin>>t; while(t--){ cin>>n; for(i=0;i<=n;i++)vis[i]=0; for(i=0;i<=n;i++){ adj[i].clear(); val[i]=0; } vector <pair<int,int>> v; for(i=0;i<n-1;i++){ cin>>l>>m; adj[l].push_back(m); adj[m].push_back(l); v.push_back({l,m}); } //taking 1 ans the root; int d[n+1]; //d=distance for(i=0;i<=n;i++){d[i]=100000000000;p[i]=0;} queue <int> q; d[1]=0; //if 1 is root q.push(1); vis[1]=1; while(!q.empty()){ int s=q.front(); q.pop(); for(auto u:adj[s]){ if(vis[u])continue; vis[u]=1; d[u]=d[s]+1; p[u]=s; q.push(u); } } int q1; cin>>q1; int sum=0; for(i=0;i<=n;i++)vis[i]=0; while(q1--){ int x,y,z; cin>>x>>y>>z; //val[i]+=z; sum+=z; y--; int v1=v[y].first; int v2=v[y].second; if(x==1){ //int v1=v[y].first; //int v2=v[y].second; if(d[v2]>d[v1]){ val[v2]-=z; } else{ val[v1]+=z; val[1]-=z; } } else{ swap(v1,v2); if(d[v2]>d[v1]){ val[v2]-=z; } else{ val[v1]+=z; val[1]-=z; } } } int ans[n+1]; for(i=0;i<=n;i++)ans[i]=0; q.push(1); vis[1]=1; while(!q.empty()){ int s=q.front(); val[s]+=val[p[s]]; ans[s]=sum+val[s]; q.pop(); for(auto u:adj[s]){ if(vis[u])continue; vis[u]=1; //d[u]=d[s]+1; //p[u]=s; q.push(u); } } for(i=1;i<=n;i++){ cout<<ans[i]<<"\n"; } //memset(a,0,sizeof(a)); //cout<<fixed<<setprecision(10)<<ans<<"\n"; } return 0; }
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define ff(i,a,b) for(int i = a; i <= b; i++) #define fb(i,b,a) for(int i = b; i >= a; i--) using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef pair<int,int> pii; const int mxN = 200005; const int inf = 1e9 + 5; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // os.order_of_key(k) the number of elements in the os less than k // *os.find_by_order(k) print the k-th smallest number in os(0-based) int n; ll niz[mxN]; ll pref[mxN]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; ff(i,1,n)cin >> niz[i]; ff(i,1,n)pref[i] = pref[i - 1] + niz[i]; ll tren = 0; ll mx = 0; ff(i,1,n){ tren += pref[i]; mx = max(mx, niz[i]); ll rez = tren + 1ll * i * mx; cout << rez << '\n'; } return 0; } /** // probati bojenje sahovski ili slicno **/
#include <bits/stdc++.h> using namespace std; int main(){ long long i,j,k,l,m,n,t; cin>>n; k=0; m=0; l=0; for(i=0;i<n;i++) { cin>>j; k+=j; l+=k; if (m<j) m=j; cout<<l+m*(i+1)<<' '; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rep2(i,x,n) for(int i=x; i<(n); i++) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define rng(v) v.begin(),v.end() #define rngr(v) v.rbegin(),v.rend() const long long INF = 1LL << 60; using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; using P3 = tuple<int, int, int>; #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vs vector<string> using vp = vector<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; } using CP = complex<double>; double dot( const CP &z, const CP &w ){ return (z.real()*w.real() + z.imag()*w.imag()); } double cross( const CP &z, const CP &w ){ return (z.real()*w.imag() - z.imag()*w.real()); } int n, q; vvi g, ql, qr, pre; vi cnt, res; void dfs(int i, int d){ int m = ql[i].size(); pre[i].resize(m); rep(mi, m){ pre[i][mi] = cnt[ql[i][mi]]; } cnt[d]++; for(auto j : g[i]){ dfs(j, d+1); } rep(mi, m){ int k = ql[i][mi]; res[qr[i][mi]] = cnt[k]-pre[i][mi]; } } int main() { cin >> n; g.resize(n); ql.resize(n); qr.resize(n); cnt.resize(n); pre.resize(n); rep(i, n-1){ int p; cin >> p; --p; g[p].eb(i+1); } cin >> q; res.resize(q); rep(qi, q){ int u, d; cin >> u >> d; --u; ql[u].eb(d); qr[u].eb(qi); } dfs(0, 0); for(auto i : res) cout << i << endl; return 0; }
#include <bits/stdc++.h> #define db(a) cout << a << endl #define db2(a,b) cout << a << " " << b << endl #define dbd(a) cout << #a << ": " << a << endl #define dbd2(a,b) cout << #a << ": " << a << ", " << #b << ": " << b << endl #define dbp(a) cout << a.first << " " << a.second << endl #define adb(a) for(auto _i:a) cout << _i << " "; cout << endl #define adbp(a) for(auto _i:a) cout << _i.first << " " << _i.second << endl; #define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define mod 1000000007 #define N 400010 #define M 100010 using namespace std; vector<vector<int>> graph(N); int depth[N], in[N], out[N], timer = 0; vector<int> tour; void dfs(int v, int p, int d) { tour.push_back(v); in[v] = timer++; depth[v] = d; for(int x:graph[v]) if(x != p) dfs(x, v, d + 1); tour.push_back(v); out[v] = timer++; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif fastIO; int n; cin >> n; int p[n]; for(int i=1; i<n; i++) { cin >> p[i], p[i]--; graph[i].push_back(p[i]); graph[p[i]].push_back(i); } dfs(0, -1, 0); vector<vector<int>> mp(N); for(int i=0; i<tour.size(); i++) mp[depth[tour[i]]].push_back(i); int q; cin >> q; while(q--) { int u, d; cin >> u >> d, u--; auto l = lower_bound(mp[d].begin(), mp[d].end(), in[u]); auto r = upper_bound(mp[d].begin(), mp[d].end(), out[u]); if(r == mp[u].end()) r--; int ans = (r - l + 1) / 2; cout << ans << endl; } }
//#include <atcoder/maxflow.hpp> #include <memory> #include <iostream> #include <map> #include <list> #include <set> #include <algorithm> #include <vector> #include <sstream> #include <string> #include <functional> #include <queue> #include <deque> #include <stack> #include <limits> #include <unordered_map> #include <unordered_set> #include <cmath> #include <fstream> #include <iterator> #include <random> #include <chrono> #include <complex> #include <thread> #include <bitset> #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 std; //using namespace atcoder; ll modd = (1000LL * 1000LL * 1000LL + 7LL); //ll modd = 998244353; ll binary_search(function<bool(ll)> func, ll start, ll end) { /* func:int ->bool returns smallest int x where func(x) evaluates to true, searches in [start,end), it is assumed the values are false, .. , false, true ... */ if (end <= start) { return end; } // has to be here, otherwise func(end-1) in next line could be a problem if (!func(end-1)) { return end; } while (end-start>1) { ll mid = (start+end)/2; if (func(mid)) { end = mid; } else { start = mid; } } if (func(start)) { return start; } else { return end; } }; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); cout.precision(17); 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 // auto start = chrono::steady_clock::now(); // readint(test_cases); int test_cases = 1; forr(t, 1, test_cases) { readint(n); readint(m); vector<vector<int>> adj(n); forr(i,0,m) { readint(aa); readint(bb); --aa; --bb; adj[aa].push_back(bb); adj[bb].push_back(aa); } vector<int> pearls; readint(k); forr(i,0,k) { readint(aa); --aa; pearls.push_back(aa); } vector<vector<ll>> dist_(k, vector<ll>(k, modd)); forr(i,0,k) { queue<pair<int,int>> todo; todo.push(make_pair(pearls[i],0)); vector<int> dist(n, modd); while (!todo.empty()) { auto curr = todo.front(); todo.pop(); dist[curr.first] = min(dist[curr.first], curr.second); if (dist[curr.first] == curr.second) { for(auto nn : adj[curr.first]) { todo.push(make_pair(nn, curr.second+1)); } } } forr(j,0,k) { dist_[i][j] = dist[pearls[j]]; dist_[j][i] = dist_[i][j]; } } vector<vector<ll>> mmin(k, vector<ll>(1<<k, modd)); forr(i,0,mmin[0].size()) { forr(j,0,k) { if (i==0) { continue; } if ((i & (1<<j))==0) { continue; } if (i == 1<<j) { mmin[j][i] = 1; continue; } int ii = i^(1<<j); forr(r,0,k) { if (ii & (1<<r)==0) {continue;} mmin[j][i] = min(mmin[j][i], dist_[j][r] + mmin[r][ii]); } } } ll ret = modd; forr(i,0,k) { ret = min(ret, mmin[i][(1<<k)-1]); } cout << (ret>=modd ? -1 : ret) << endl; } // auto stop = chrono::steady_clock::now(); // auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start); // cout << "Duration: " << duration.count() << endl; return 0; }
#pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> // #include<ext/pb_ds/assoc_container.hpp> // #include<ext/pb_ds/tree_policy.hpp> // #include<ext/pb_ds/tag_and_trait.hpp> // using namespace __gnu_pbds; // #include<boost/multiprecision/cpp_int.hpp> // namespace multiprecisioninteger = boost::multiprecision; // using cint=multiprecisioninteger::cpp_int; using namespace std; using ll=long long; #define double long double using datas=pair<ll,ll>; using ddatas=pair<double,double>; using tdata=pair<ll,datas>; using vec=vector<ll>; using mat=vector<vec>; using pvec=vector<datas>; using pmat=vector<pvec>; // using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>; #define For(i,a,b) for(i=a;i<(ll)b;++i) #define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i) #define rep(i,N) For(i,0,N) #define rep1(i,N) For(i,1,N) #define brep(i,N) bFor(i,N,0) #define brep1(i,N) bFor(i,N,1) #define all(v) (v).begin(),(v).end() #define allr(v) (v).rbegin(),(v).rend() #define vsort(v) sort(all(v)) #define vrsort(v) sort(allr(v)) #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define endl "\n" #define eb emplace_back #define print(x) cout<<x<<endl #define printyes print("Yes") #define printno print("No") #define printYES print("YES") #define printNO print("NO") #define output(v) do{bool f=0;for(auto outi:v){cout<<(f?" ":"")<<outi;f=1;}cout<<endl;}while(0) #define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0) const ll mod=1000000007; // const ll mod=998244353; const ll inf=1LL<<60; const double PI=acos(-1); const double eps=1e-9; template<class T,class E> ostream& operator<<(ostream& os,const pair<T,E>& p){return os<<"("<<p.first<<","<<p.second<<")";} template<class T> ostream& operator<<(ostream& os,const vector<T>& v){ os<<"{";ll i; rep(i,v.size()){ if(i)os<<","; os<<v[i]; } os<<"}"; return os; } template<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;} template<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;} #ifdef DEBUG void debugg(){cout<<endl;} template<class T,class... Args>void debugg(const T& x,const Args&... args){cout<<" "<<x;debugg(args...);} #define debug(...) cout<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debugg(__VA_ARGS__) #else #define debug(...) (void(0)) #endif void startupcpp(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(15); } double distance(ddatas x,ddatas y){ double a=x.first-y.first,b=x.second-y.second; return sqrt(a*a+b*b); } ll modinv(ll a,ll m=mod) { ll b=m,u=1,v=0,t; while(b){ t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); } return (u+m)%m; } ll moddevide(ll a,ll b){return (a*modinv(b))%mod;} vec modncrlistp,modncrlistm; ll modncr(ll n,ll r){ if(n<r)return 0; ll i,size=modncrlistp.size(); if(size<=n){ modncrlistp.resize(n+1); modncrlistm.resize(n+1); if(!size){ modncrlistp[0]=modncrlistm[0]=1; size++; } For(i,size,n+1){ modncrlistp[i]=modncrlistp[i-1]*i%mod; modncrlistm[i]=modinv(modncrlistp[i]); } } return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod; } ll modpow(ll a,ll n,ll m=mod){ ll res=1; while(n>0){ if(n&1)res=res*a%m; a=a*a%m; n>>=1; } return res; } ll gcd(ll a,ll b){if(!b)return abs(a);return (a%b==0)?abs(b):gcd(b,a%b);} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll countdigits(ll n){ ll ans=0; while(n){n/=10;ans++;} return ans; } ll sumdigits(ll n){ ll ans=0; while(n){ans+=n%10;n/=10;} return ans; } ll calc(ll N,ll R){ ll i,res=1; rep(i,R){ (res*=(N-i)%mod)%=mod; res=moddevide(res,i+1); } return res; } ll N,M,K,H,W,A,B,C,D; string s,t; ll ans; int main(){ startupcpp(); // int codeforces;cin>>codeforces;while(codeforces--){ ll i,j; cin>>N>>K; vec v(N); rep(i,N)cin>>v[i]; B=accumulate(all(v),0LL); if(K<B){ print(0); return 0; } A=K-B; print(calc(K+N,N+B)); }
#include <bits/stdc++.h> using namespace std; typedef long long ll ; #define rep(i,a,n) for(int i=a ; i<n ; i++) #define pb push_back int main() { int T=1; // cin >> T ; while(T--) { int x,y ; cin>>x>>y ; if(abs(x-y)<=2) cout<<"Yes\n" ; else cout<<"No\n" ; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); ++i) #define fixed_setprecision(n) fixed << setprecision((n)) #define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC); #define pai 3.1415926535897932384 #define NUM_MAX 2e18 #define NUM_MIN -1e9 using namespace std; using ll = long long; using P = pair<int,int>; template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; } const long long INF = 1LL << 60; int main() { int x, y; cin >> x >> y; if(max(x, y) < min(x, y) + 3) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long int X; cin >> X; int a; a = X%100; int ans; ans = 100 - a; cout << ans << endl; }
//Author:RingweEH #include <cstdio> #include <algorithm> #include <cstring> #define ll long long #define db double using namespace std; int min( int a,int b ) { return (a<b) ? a : b; } int max( int a,int b ) { return (a>b) ? a : b; } void bmin( int &a,int b ) { a=(a<b) ? a : b; } void bmax( int &a,int b ) { a=(a>b) ? a : b; } int read() { int x=0,w=1; char ch=getchar(); while ( ch>'9' || ch<'0' ) { if ( ch=='-' ) w=-1; ch=getchar(); } while ( ch<='9' && ch>='0' ) { x=x*10+ch-'0'; ch=getchar(); } return x*w; } int n; int main() { n=read(); for ( int i=1; i<=n; i++ ) printf( "%d %d\n",(i*2)%n+1,(i*2+1)%n+1 ); return 0; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<endl #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; vector<string> C(vector<string> s) { ll n=s.size(); rep(i,n)REP(j,i+1,n) swap(s[i][j],s[j][i]); return s; } ll n,m; string s[1000]; ll calc(vector<string> t) { ll cnt=0; rep(k,m) { ll f=0; rep(l,2) { rep(i,n) { rep(j,n) { if(t[i].substr(0,s[k].size())==s[k]) f=1; rotate(t[i].begin(),t[i].begin()+1,t[i].end()); } } t=C(t); } cnt+=f; } return cnt; } vector<string> ans; ll MM; void solve(vector<string> t) { ll d=calc(t); if(MM<d) { MM=d; ans=t; } rep(k,5) { random_shuffle(all(t)); rep(i,n) rotate(t[i].begin(),t[i].begin()+rand()%n,t[i].end()); ll d=calc(t); if(MM<d) { MM=d; ans=t; } } } void Main() { srand((unsigned)time(NULL)); cin >> n >> m; rep(i,m) R s[i]; vector<string> v[15]; rep(i,m) v[s[i].size()].pb(s[i]); rep(e,35) { vector<string> t(n); rrep(i,15) { random_shuffle(all(v[i])); rep(j,v[i].size()) { ll M=MAX,x=0; string rr; rep(k,n) { rep(l,t[k].size()+1) { string r1=t[k].substr(l); if(r1.size()<=v[i][j].size()) { string r2=v[i][j].substr(0,r1.size()); if(r1==r2) { ll d=t[k].size()+v[i][j].size()-r1.size(); if(d<=n&&M>d) { M=d; x=k; rr=t[k]+v[i][j].substr(r1.size()); } } } if(l+v[i][j].size()<=t[k].size()) { r1=t[k].substr(l,v[i][j].size()); if(r1==v[i][j]) { M=0; x=k; rr=t[k]; } } } } if(rand()%(i+3)==0) M=MAX; if(M!=MAX) t[x]=rr; } } rep(i,n) { while(t[i].size()<n) { //t[i]+='.'; t[i]+=(char)(rand()%8+'A'); } } solve(t); } rep(i,n) pr(ans[i]); } int main(){Main();return 0;}
#include<iostream> #include<cstring> using namespace std; const int BUF = 73; int nPrime; int prime[BUF]; void makePrime() { bool isPrime[BUF] = {}; for (int i = 0; i < BUF; ++i) isPrime[i] = true; for (int i = 2; i * i < BUF; ++i) { if (isPrime[i]) { for (int j = i * 2; j < BUF; j += i) { isPrime[j] = false; } } } nPrime = 0; for (int i = 2; i < BUF; ++i) { if (isPrime[i]) { prime[nPrime++] = i; } } } long long A, B; void read() { cin >> A >> B; } long long rec(int idx, int mask, long long dp[BUF][1 << 20]) { if (idx == B - A + 1) return 1; long long &ret = dp[idx][mask]; if (ret != -1) return ret; ret = 0; long long val = A + idx; // Pick int nexMask = mask; for (int i = 0; i < nPrime; ++i) { if (val % prime[i] == 0) { if (nexMask & (1 << i)) goto _fail; nexMask |= 1 << i; } } ret += rec(idx + 1, nexMask, dp); _fail: // Don't pick ret += rec(idx + 1, mask, dp); return ret; } void work() { static long long dp[BUF][1 << 20]; memset(dp, -1, sizeof(dp)); cout << rec(0, 0, dp) << endl; } int main() { makePrime(); read(); work(); return 0; }
#include<bits/stdc++.h> #define SORT(v) sort(v.begin(),v.end()) #define si(n) scanf("%d",&n) #define sii(n,m) scanf("%d %d",&n,&m) #define sl(n) scanf("%lld",&n) #define sll(n,m) scanf("%lld %lld",&n,&m) #define ss(cad) scanf("%s",cad) #define all(v) (v).begin(), (v).end() #define PB push_back #define fst first #define scn second #define DBG(x) cerr << #x << " = " << (x) << endl; #define M 1000000007 #define N_MAX 100010 using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<ll> vl; typedef pair<int,int> pi; typedef vector<pi> vp; int main(){ int n, m; sii(n, m); pi vec[m+1]; for(int i = 0; i < m; i++) sii(vec[i].fst, vec[i].scn); int k; si(k); pi ttt[k+1]; for(int i = 0; i < k; i++) sii(ttt[i].fst, ttt[i].scn); bool posi[n+10]; int ans = 0; for(int mask = 0; mask < (1<<k); mask++){ memset(posi, 0, sizeof(posi)); for(int i = 0; i < k; i++) if(mask&(1<<i)) posi[ttt[i].fst] = 1; else posi[ttt[i].scn] = 1; int cnt = 0; for(int i = 0; i < m; i++) if(posi[vec[i].fst] && posi[vec[i].scn]) cnt++; ans = max(ans, cnt); } printf("%d\n", ans); return 0; }
#include<iostream> #include<vector> #include<map> #include<math.h> #include<algorithm> #include<set> #include<queue> #include<cstring> #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++) #define fi first #define se second using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vstring = vector<string>; using vb = vector<bool>; using pii = pair<int, int>; using pll = pair<ll, ll>; const double PI = 3.1415926535897932; const int INF = (int)2e9; const ll LINF = (ll)2e18; //const ll MOD = 998244353; const ll MOD = 1000000007; const int dc[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; const int dr[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; template<typename T> inline istream& operator>>(istream &is, vector<T> &vec) { for(auto &v : vec) { is >> v; } return is; } template<typename T> inline ostream& operator<<(ostream &os, vector<T> &vec) { for(auto &v : vec) { os << v << ",";} return os; } template<typename T> inline ostream& operator<<(ostream &os, vector<vector<T> > &mat) { for(auto &row : mat) { os << row << endl; } return os; } template<typename T, typename U> inline ostream& operator<<(ostream &o, pair<T,U> &p) { return o << "{" << p.first << "," << p.second << "}"; } template<typename T> inline void chmin(T &a, T b) { a = min(a, b); } template<typename T> inline void chmax(T &a, T b) { a = max(a, b); } bool solve(ll a, ll b, ll c, ll d) { if(a+b == c+d) { return true; } if(a-b == c-d) { return true; } if(abs(a-c) + abs(b-d) <= 3) { return true; } return false; } int main(void) { ll a, b, c, d; cin >> a >> b >> c >> d; if(a == c && b == d) { cout << 0 << endl; return 0; } vector<pll> goal; if(solve(a,b,c,d)) { cout << 1 << endl; return 0; } if((a+b)%2 == (c+d)%2) { cout << 2 << endl; return 0 ; } for(int i = -2; i <= 2; i++) { for(int j = -2; j <= 2; j++) { goal.push_back({c+i,d+j}); } } goal.push_back({c-3,d}); goal.push_back({c+3,d}); goal.push_back({c,d-3}); goal.push_back({c,d+3}); int n = goal.size(); rep(i,n) { pll g = goal[i]; ll c2 = g.fi, d2 = g.se; if(solve(a,b,c2,d2)) { cout << 2 << endl; return 0; } } cout << 3 << endl; return 0; } /*atcoder*/
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <set> #include <cstdint> #include <cmath> using namespace std; using ll = long long ; #define LINF (1LL<<60) #define rep(i,n) for (int i=0; i < (n); ++i) //#define LLONG_MAX 9223372036854775807i64 int ctoi(char c){ return int(c)-int('0') ; } const int lim = 1e9 ; int main() { int n ; cin >> n ; int a[n] ; rep(i,n){ cin >> a[i] ; } int ans = lim ; if(n==1){ cout << a[0] << endl ; return 0 ; } for (int bit = 0; bit < (1 << (n-1)); bit++) { int tot = 0 ; int cur = a[0] ; for (int i = 1; i < n ; i++) { if (bit & (1 << i-1)){ tot ^= cur ; cur = a[i] ; }else{ cur |= a[i] ; } } tot ^= cur ; ans = min(ans,tot) ; } cout << ans << endl ; return 0 ; }
#include <cmath> #include <iostream> #include <map> #include <utility> #include <vector> #define LOOP(n) for (int _i = 0; _i < (n); _i++) #define REP(i, n) for (int i = 0; i < (n); ++i) #define RREP(i, n) for (int i = (n); i >= 0; --i) #define FOR(i, r, n) for (int i = (r); i < (n); ++i) using namespace std; using ll = long long; using ld = long double; using std::map; using std::vector; int main() { int h, w; cin >> h >> w; ll ans = 1; string s[h]; REP(i, h) cin >> s[i]; REP(sum, h + w) { int red = 0, blue = 0, empty = 0; REP(i, sum + 1) { if (i >= h) { continue; } int j = sum - i; if (j >= w) { continue; } char c = s[i][j]; switch (c) { case 'B': blue++; break; case 'R': red++; break; case '.': empty++; break; } } if (red == 0 && blue == 0 && empty == 0) { continue; } if (red != 0 && blue != 0) { cout << 0; return 0; } else if (red == 0 && blue == 0) { ans *= 2; ans %= 998244353; } } cout << ans; return 0; }
#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 int long long typedef vector<int> vi; typedef pair<int, int> pii; #define PI 3.14159265 #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() const int MOD = 1000000007; const int MAXN = 1000005, MAXK = 18; int prime[MAXN]; void seive() { for(int i=2;i<=MAXN;i++) { if(prime[i]==0) { for(int j=i;j<=MAXN;j+=i) { prime[j]++; } } } } int32_t main() { int n; cin>>n; int a[n][3]; for(int i=0;i<n;i++) { cin>>a[i][0]>>a[i][1]>>a[i][2]; } int ans=INT_MAX; for(int i=0;i<n;i++) { double time=0.5+(a[i][2]-1)*1.0; if(time>(double)a[i][0]) { ans=min(ans,a[i][1]); } } if(ans==INT_MAX) { cout<<-1; } else { cout<<ans; } return 0; }
#include <iostream> #include <vector> #include <numeric> #include <algorithm> #include <climits> constexpr int SINT_MAX = std::numeric_limits<int>::max(); constexpr int SINT_MIN = std::numeric_limits<int>::min(); using namespace std; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; template<class T> T choose(bool b, T t, T f) { if (b) return t; else return f; } const char *YesNo(bool b) { return choose(b, "Yes", "No"); } const char *YESNO(bool b) { return choose(b, "YES", "NO"); } template<class NumT> NumT diffabs(NumT l, NumT r) { if (l < r) return r-l; else return l-r; } struct myinout_t {} io; template<class T> myinout_t &operator >>(myinout_t &my, T &i) { cin >> i; return my; } myinout_t &operator >>(myinout_t &my, int &i) { int r = scanf("%d", &i); if (r != 1) exit(EXIT_FAILURE); return my; } myinout_t &operator >>(myinout_t &my, ll &i) { int r = scanf("%lld", &i); if (r != 1) exit(EXIT_FAILURE); return my; } template<class T> myinout_t &operator <<(myinout_t &my, const T &i) { cout << i; return my; } myinout_t &operator <<(myinout_t &my, int i) { printf("%d", i); return my; } myinout_t &operator <<(myinout_t &my, ll i) { printf("%lld", i); return my; } myinout_t &operator <<(myinout_t &my, double i) { printf("%.20f", i); return my; } constexpr char BR = '\n'; /////////////////////////////////////////////////// bool f(int N, int M, int T, const vi &A, const vi &B) { int b = N; int t = 0; for (int i = 0; i < M; i++) { if ((A[i] - t) >= b) { return false; } else { b += (B[i] - 2 * A[i] + t); b = min(b, N); t = B[i]; } } return (T - t) < b; } int main() { int N, M, T; io >> N >> M >> T; vi A(M), B(M); for (int i = 0; i < M; i++) io >> A[i] >> B[i]; bool ans = f(N, M, T, A, B); io << YesNo(ans) << BR; }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int n,m; char s[1009]; int mat[1009][1009]; int f[1000009]; int br[1000009],bc[1000009]; int vr[1009],vc[1009]; vector<int> sr[1009],sc[1009],sf[1000009]; int fnd(int x) { return x==f[x]?x:f[x]=fnd(f[x]); } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",s+1); for(int j=1;j<=m;j++) mat[i][j]=s[j]=='.'?0:1; } mat[1][1]=mat[1][m]=mat[n][1]=mat[n][m]=1; // for(int i=1;i<=n;i++) // for(int j=1;j<=m;j++) // printf("%d%c",mat[i][j],j==m?'\n':' '); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int num=(i-1)*m+j; br[num]=i; bc[num]=j; if(mat[i][j]) { sr[i].push_back(num); sc[j].push_back(num); } } for(int i=1;i<=n*m;i++) f[i]=i; for(int i=1;i<=n;i++) { int lst=0; for(int u:sr[i]) { // printf("i:%d u:%d\n",i,u); if(lst) f[fnd(u)]=fnd(lst); else lst=u; } } for(int i=1;i<=m;i++) { int lst=0; for(int u:sc[i]) if(lst) f[fnd(u)]=fnd(lst); else lst=u; } // for(int i=1;i<=n*m;i++) // printf("i:%d fnd:%d\n",i,fnd(i)); for(int i=1;i<=n*m;i++) sf[fnd(i)].push_back(i); int ansr=-1,ansc=-1; for(int i=1;i<=n*m;i++) { if(sf[i].size()>1) { int tr=0,tc=0; for(int u:sf[i]) { // printf("i:%d u:%d\n",i,u); if(!vr[br[u]]) vr[br[u]]=1,tr++; if(!vc[bc[u]]) vc[bc[u]]=1,tc++; } ansr++,ansc++; } } for(int i=1;i<=n;i++) if(!vr[i]) ansr++; for(int i=1;i<=m;i++) if(!vc[i]) ansc++; printf("%d",min(ansr,ansc)); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using Vll =vector<ll>; using VVll =vector<vector<ll>>; template <class T> using Vec = vector<T>; template <class T> using VVec = vector<vector<T>>; void Z(){ cout<<"Test"<<endl; } template <class T> void VVcout(VVec<T> A){ for(auto Vi:A) { for(auto i:Vi)cout<<i<<' '; cout<<endl;}} VVll make_binary_VVll(VVll A,ll p){ VVll Ab=A; ll ni=A.size(); ll nj=A.at(0).size(); for(ll i=0;i<ni;i++) for(ll j=0;j<nj;j++){ ll a=A.at(i).at(j); if(a<=p) a=1; else a=0; Ab.at(i).at(j)=a; } return Ab; } VVll addsum(VVll A){ VVll Ab=A; ll ni=A.size(); ll nj=A.at(0).size(); for(ll i=0;i<ni;i++) for(ll j=0;j<nj;j++){ ll a=A.at(i).at(j); ll upper_sum=0,left_sum=0,upper_left_sum=0; if(i!=0) upper_sum+=Ab.at(i-1).at(j); if(j!=0) left_sum+=Ab.at(i).at(j-1); if(i!=0 && j!=0) upper_left_sum+=Ab.at(i-1).at(j-1); Ab.at(i).at(j)=left_sum+upper_sum-upper_left_sum+a ; } return Ab; } bool check_p(VVll addsum_A,ll k,ll n,ll ci){ ll addsum; bool ans=false; for(ll i=0;i<n-k+1;i++) for(ll j=0;j<n-k+1;j++){ ll sum=0,upper_sum=0,left_sum=0,upper_left_sum=0; sum=addsum_A.at(i+k-1).at(j+k-1); if(i!=0) upper_sum+=addsum_A.at(i-1).at(j+k-1); if(j!=0) left_sum+=addsum_A.at(i+k-1).at(j-1); if(i!=0 && j!=0) upper_left_sum+=addsum_A.at(i-1).at(j-1); addsum=sum+-left_sum-upper_sum+upper_left_sum ; if(addsum>=ci) ans=true; } return ans; } int main(){ ll n,k,a; cin>>n>>k; VVll A(n,Vll(n)); for(ll i=0;i<n;i++) for(ll j=0;j<n;j++){ cin>>a; A.at(i).at(j)=a; } ll min=-1; ll centeri; if(k%2==0) centeri=(k*k)/2; else centeri=(k*k+1)/2; ll l=-1,r=1000000000LL+1; while(r-l>1){ ll m=(l+r)/2; VVll binA,addsumA; binA=make_binary_VVll(A,m); addsumA=addsum(binA); //VVcout(binA); //VVcout(addsumA); //cout<<l<<' '<<m<<' '<<r<<endl; if(check_p(addsumA,k,n,centeri)) r=m; else l=m; //cout<<endl; } cout<<r<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; // #include <atcoder/all> // using namespace atcoder; int main(){ int n; double D, H; cin >> n >> D >> H; double highwari = 10000000; double high = 0; vector<double> d(n), h(n); rep(i,n) cin >> d[i] >> h[i]; rep(i,n){ double wari = (H-h[i])/(D-d[i]); // cout << h[i] - d[i]*wari << " " << wari << endl; if(highwari > wari && h[i] - d[i]*wari > 0){ highwari = min(wari, highwari); high = h[i] - d[i] * wari; } } cout << high << endl; // if(high > 0) cout << high << endl; // else cout << 0 << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ double n, d, h; cin >> n >> d >> h; vector<int> D(n), H(n); for(int i=0;i<n;i++)cin >> D.at(i) >> H.at(i); double minlen = 1000000000; for(int i=0;i<n;i++){ if(d * (h - H.at(i)) / (d - D.at(i)) < minlen){ minlen = d * (h - H.at(i)) / (d - D.at(i)); } } if(0 < h - minlen)cout << fixed << setprecision(15) << h - minlen << endl; else cout << 0 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i) #define ALL(v) (v).begin(),(v).end() #define CLR(t,v) memset(t,(v),sizeof(t)) template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";} template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;} template<class T>void chmin(T&a,const T&b){if(a>b)a=b;} template<class T>void chmax(T&a,const T&b){if(a<b)a=b;} ll nextLong() { ll x; scanf("%lld", &x); return x;} map<pair<int,ll>, ll> memo; int N; ll A[55]; ll f(const int n, const ll X) { if (memo.count({n, X}) > 0) { return memo[{n, X}]; } ll res = 0; if (n + 1 == N) { res = 1; return memo[{n, X}] = res; } ll r = X % A[n+1]; res += f(n+1, X - r); if (r != 0) { res += f(n+1, X + A[n+1] - r); } return memo[{n, X}] = res; } int main2() { N = nextLong(); ll X = nextLong(); REP(i, N) A[i] = nextLong(); ll ans = f(0, X); cout << ans << endl; return 0; } int main() { #ifdef LOCAL for (;!cin.eof();cin>>ws) #endif main2(); return 0; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i,n) for(int i=0;i<(n);++i) #define repi(i,a,b) for(int i=int(a);i<int(b);++i) #define rrep(i,n) for(int i=((n)-1);i>=0;--i) #define all(x) (x).begin(), (x).end() #define PI 3.14159265358979323846264338327950L #define mod 1000000007 using namespace std; typedef long long ll; typedef long double ld; int main() { string s; cin>>s; int cnt=0; rep(i,s.size()){ if(s[i]=='Z'&&s[i+1]=='O'&&s[i+2]=='N'&&s[i+3]=='e') cnt++; } cout<<cnt; }
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; using ll = long long int; #define F first #define S second #define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); const int N=2e5+10,LN=20,SQ=550,M=5e4+10; const ll INF=1e18; const int MOD=1000000007 /*998244353*/; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using pll=pair<ll,ll>; using pii=pair<int,int>; #define ordered_set tree<pair<pll,ll>, null_type,less<pair<pll,ll>>, rb_tree_tag,tree_order_statistics_node_update> ll pow(ll x, ll y, ll mod){ ll ans=1; while (y != 0) { if (y & 1) ans = ans * x % mod; y >>= 1; x = x * x % mod; } return ans; } ll n,a[N],p[N],f[N],inv; vector<ll> ans; void update(ll t, ll x){ while(t<N) f[t]+=x,t+=t&-t; } ll get(ll t, ll x=0){ while(t) x+=f[t],t-=t&-t; return x; } int main(){ fast_io; cin >> n; for(ll i=1; i<=n; i++){ cin >> a[i]; p[a[i]]=i; inv+=get(n)-get(a[i]); update(a[i],1); } //cout << inv << '\n'; if(inv!=n-1) return cout << -1, 0; for(ll i=1; i<=n; i++){ for(ll j=p[i]-1; j>=i; j--){ //cout << "SALAM" << endl; ans.push_back(j); swap(p[a[j]],p[a[j+1]]); swap(a[j],a[j+1]); } } for(ll i : ans) cout << i << '\n'; return 0; }
/* Author: Arjan Singh Bal, IIIT Gwalior "Everything in this world is magic, except to the magician" */ #include <bits/stdc++.h> #ifdef LOCAL #include "pprint.hpp" #else #define trace // #endif using namespace std; template <typename Arg1> void prn(Arg1&& arg1) { cout << arg1 << "\n"; } template <typename Arg1, typename... Args> void prn(Arg1&& arg1, Args&&... args) { cout << arg1 << " "; prn(args...); } template <typename Arg1> void prs(Arg1&& arg1) { cout << arg1 << " ";} template <typename Arg1, typename... Args> void prs(Arg1&& arg1, Args&&... args) { cout << arg1 << " "; prs(args...); } template <typename Arg1> void read(Arg1&& arg1) { cin >> arg1; } template <typename Arg1, typename... Args> void read(Arg1&& arg1, Args&&... args) { cin >> arg1; read(args...); } #define ll long long #define pii pair<int, int> #define pli pair<ll, int> #define pil pair<int, ll> #define pll pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vb vector<bool> #define vd vector<double> #define vs vector<string> #define ff first #define ss second #define pb push_back #define eb emplace_back #define ppb pop_back #define pf push_front #define ppf pop_front #define vpii vector<pii> #define umap unordered_map #define all(x) x.begin(),x.end() #define clr(a,b) memset(a,b,sizeof a) #define fr(i, n) for (int i = 0; i < n; ++i) #define fr1(i, n) for (int i = 1; i <= n; ++i) #define rfr(i, n) for (int i = n - 1; i >= 0; --i) #define precise(x) cout << fixed << setprecision(x) typedef double f80; inline void solve(); signed main() { #ifdef LOCAL freopen("in.txt" , "r" , stdin); //freopen("out.txt" , "w" , stdout); #else ios_base::sync_with_stdio(false); cin.tie(NULL); #endif int t = 1; // read(t); fr1 (tc, t) { // cout << "Case #" << tc << ": "; solve(); } return 0; } inline void solve() { int n; read(n); vi vec(n); fr (i, n) { read(vec[i]); } vb usd(n - 1, 0); set<pii> remain; fr (i, n - 1) { remain.insert({vec[i + 1] - vec[i], i}); } vi ans; while (!remain.empty()) { pii cur = *remain.begin(); remain.erase(cur); if (cur.ff > 0) { prn(-1); return ; } int idx = cur.ss; usd[idx] = 1; ans.pb(idx); if (idx && !usd[idx - 1]) { remain.erase({vec[idx] - vec[idx - 1], idx - 1}); } if (idx + 1 < n - 1 && !usd[idx + 1]) { remain.erase({vec[idx + 2] - vec[idx + 1], idx + 1}); } swap(vec[idx], vec[idx + 1]); if (idx && !usd[idx - 1]) { remain.insert({vec[idx] - vec[idx - 1], idx - 1}); } if (idx + 1 < n - 1 && !usd[idx + 1]) { remain.insert({vec[idx + 2] - vec[idx + 1], idx + 1}); } } trace(vec); fr (i, n) { if (vec[i] != i + 1) { prn(-1); return ; } } for (auto i : ans) { prn(i + 1); } }
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair using ll = long long; int main() { string s; cin >> s; int n = s.size(); int ans = 0; for (int i = 0; i < n - 3; i++) { if(s.substr(i, 4) == "ZONe") ans++; } cout << ans << endl; return 0; }
#define LOCAL #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++) #define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--) #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define All(x) x.begin(), x.end() #define Range(x, i, j) x.begin() + i, x.begin() + j // #define M_PI 3.14159265358979323846 // CF #define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI)) #define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360) #define Find(set, element) set.find(element) != set.end() #define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank #define endl "\n" #define Case(x) printf("Case #%d: ", x); // gcj typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; typedef vector<PI> vpi; typedef vector<vector<PI>> vvpi; typedef vector<PLL> vpl; typedef vector<vector<PLL>> vvpl; typedef vector<char> vch; typedef vector<vector<char>> vvch; constexpr ll INF = 1001002003004005006ll; constexpr int n_max = 2e5+10; template<class T> inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; }; template<class T> inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; }; template<class T, class U> T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;}; // debug template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) {return '"' + s + '"';}; string to_string(const char c) {return to_string((string) &c);}; string to_string(bool b) {return (b ? "true" : "false");}; template <size_t N> string to_string(bitset<N> v){ string res = ""; for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; }; template <typename A> string to_string(A v) { bool first = true; string res = "{"; for(const auto &x : v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; }; template <typename A, typename B> string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";} void debug_out() {cerr << endl;}; template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }; void LINE_OUT() { cout << "--------------" << endl; }; #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define LINE LINE_OUT(); #else #define debug(...) 71 #define LINE 71; #endif void print() { cout << endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }; template <class T> void print(vector<T> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }; template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec); } }; signed main() { ios::sync_with_stdio(false); cin.tie(0); string t = "ZONe"; string s; cin >> s; int ans = 0; rep(i,0,9) { bool ok = true; rep(j,0,4) ok &= (s[i+j] == t[j]); if (ok) ans++; } cout << ans << endl; return 0; };
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define int long long int dp[100001][2], dp1[100001][2], n, arr[100001]; int fun1(int ind, int sign) { if ( ind == n ) return 1; if ( dp1[ind][sign] != - 1 ) return dp1[ind][sign]; int res = 0; if ( sign ) { res = (fun1(ind + 1, sign)%mod)%mod; res = (res%mod + fun1(ind + 1, 0)%mod )%mod; } else { res = ( fun1(ind + 1, 1)%mod)%mod; } return dp1[ind][sign] = res; } int fun(int ind, int sign) { if ( ind == n ) return 0; if ( dp[ind][sign] != - 1 ) return dp[ind][sign]; int res = 0; if ( sign ) { res = ( (arr[ind]%mod*fun1(ind + 1, sign)%mod)%mod + fun(ind + 1, sign)%mod)%mod; res = (res%mod + fun(ind + 1, 0)%mod - (arr[ind]%mod*fun1(ind + 1, 0)%mod)%mod + mod)%mod; } else { res = ((arr[ind]%mod*fun1(ind + 1, 1)%mod )%mod+ fun(ind + 1, 1)%mod)%mod; } return dp[ind][sign] = res; } void solve() { memset(dp, -1, sizeof(dp)); memset(dp1, -1, sizeof(dp)); cin >> n; for ( int i=0; i<n; i++ ) cin >> arr[i]; cout << fun(0,0) << '\n'; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; // cin >> tc; while( tc-- ) { solve(); } } // Tarun IIITA
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll a[200010],sum[200010]; int main(){ ll i,n; cin >> n; for(i=0;i<n;i++) cin >> a[i]; sort(a,a + n); sum[0] = 0; for(i=1;i<=n;i++) sum[i] = sum[i - 1] + a[i - 1]; ll ans = 0; for(i=0;i<n;i++){ ans -= (n - 1 - 2*i)*a[i]; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl "\n" #define quick ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ALL(x) x.begin(),x.end() #define SORT(x) sort(ALL(x)) #define FOR(a,b,c) for(ll (a)= (b);(a)<(c);++(a)) #define REP(a,b) FOR(a,0,b) #define mp make_pair #define pb push_back #define pll pair<ll,ll> #define f first #define s second ll ttt = 1, n; template <class myType> void print_arr(myType &arr, ll L, string sep){ REP(i,L){ cout << arr[i]; if (i < L-1){ cout << sep; } } cout << endl; return; } /* */ void solve(){ ll m; cin >> n >> m; ll x, y; set<ll> pos; map<ll,set<ll>> G; set<ll> rows; REP(i,m) { cin >> x >> y; G[x].insert(y); rows.insert(x); } pos.insert(n); for (ll x: rows) { vector<ll> to_add, to_remove; for (ll y: G[x]) { //cout << "Y:" << y << endl; if (y > 0 && pos.find(y-1) != pos.end()) to_add.pb(y); if (y < 2*n-1 && pos.find(y+1) != pos.end()) to_add.pb(y); to_remove.pb(y); } //print_arr(to_add,to_add.size()," "); //print_arr(to_remove,to_remove.size()," "); for (ll y: to_remove) { pos.erase(y); } for (ll y: to_add) { pos.insert(y); } /*cout << "ROW " << x << endl; for (ll y: pos) { cout << y << endl; }*/ } cout << pos.size() << endl; return; } int main(){ quick; //cin >> ttt; while (ttt--) solve(); return 0; }
/* -*- coding: utf-8 -*- * * e.cc: E - White Pawn */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_M = 200000; /* typedef */ typedef pair<int,int> pii; typedef pair<int,bool> pib; typedef queue<pib> qpib; /* global variables */ pii xys[MAX_M]; bool cs[MAX_M * 2 + 1]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); int m2 = m * 2; for (int i = 0; i < m; i++) scanf("%d%d", &xys[i].first, &xys[i].second); sort(xys, xys + m); cs[m] = true; for (int i = 0; i < m;) { int xi = xys[i].first; qpib q; while (i < m && xys[i].first == xi) { int yi = xys[i].second - n + m; if (yi >= 0 && yi <= m2) { if ((yi > 0 && cs[yi - 1]) || (yi < m2 && cs[yi + 1])) q.push(pib(yi, true)); else if (cs[yi]) q.push(pib(yi, false)); } i++; } while (! q.empty()) { pib yb = q.front(); q.pop(); cs[yb.first] = yb.second; } } int cnt = 0; for (int i = 0; i <= m2; i++) if (cs[i]) cnt++; printf("%d\n", cnt); return 0; }
#include<bits/stdc++.h> using namespace std; int main() {double n,x,p=0; cin>>n>>x; for(int i=0;i<n;i++) { double a,b; cin>>a>>b; p=p+(a*b); if(p>x*100) {cout<<i+1; break; } } if(x*100>=p) cout<<"-1"; return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef struct x { string a; long long b; }x; long long n; bool cmp(x tmp1,x tmp2) { return tmp1.b>tmp2.b; } void solve() { vector < x > a(n); long long i; for (i=0;i<n;i++) cin>>a[i].a>>a[i].b; sort(a.begin(),a.end(),cmp); cout<<a[1].a<<endl; } int main() { while(cin>>n) solve(); }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,x) for(ll i = 0; i < x; 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; } ll f(ll x){ string num = to_string(x); sort(num.begin(),num.end()); ll g1 = stol(num); sort(num.begin(),num.end(), greater<int>{}); ll g2 = stol(num); return -g1 + g2; } int main(){ ll n,k; cin >> n >> k; for(ll i = 0; i < k; i++){ n = f(n); } cout << n << endl; }
#include <bits/stdc++.h> using namespace std; #define MOD (long long int)(998244353) #define ll long long int #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 REP(i,n) for(int i=n-1; i>=0; i--) #define REPS(i,n) for(int i=n; i>0; i--) #define FOR(i,a,b) for(int i=a; i<(int)(b); i++) #define ALL(x) (x).begin(),(x).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset((a), 0 ,sizeof(a)) #define PB push_back #define MP make_pair #define SP << " " << const int INF = 1001001001; const ll LINF = 100100100100100100; const double EPS = 1e-10; const long double PI = acos(-1.0L); typedef pair<int,int> PII; typedef pair<ll,ll> PLL; typedef pair<double,double> PDD; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; #define chmax(a,b) a = (((a)<(b))?(b):(a)) #define chmin(a,b) a = (((a)>(b))?(b):(a)) __attribute__((constructor)) void initial(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } signed main() { ll n; int k; cin>>n>>k; ll m=n; rep(i,k){ VL a; while(m){ a.PB(m%10); m/=10; } ll b=0LL,c=0LL; sort(RALL(a)); rep(j,a.size()){ b += a[j]; if((ll)j<(a.size()-1)) b*=10; } sort(ALL(a)); rep(j,a.size()){ c += a[j]; if((ll)j<(a.size()-1)) c*=10; } m=b-c; } cout<<m<<endl; return 0; }
#include <bits/stdc++.h> #define int int64_t using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; int sum=0; for (int i=1;i<=100005;i++) { sum+=i; if (sum>=n) { cout<<i; return 0; } } return 0; }
#include <bits/stdc++.h> #define reps(i, a, n) for(int i = a; i < (int)n; i++) #define rep(i, n) reps(i, 0, n) #define rrep(i, n) for(int i = n-1; i >= 0; i--) #define all(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define pb push_back #define INF 1 << 29 template<class T>bool chmax(T &a, const T &b){ if (a<b){ a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b){ if (b<a){ a=b; return true; } return false; } using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using P = pair<int, int>; int main(){ int n, i = 1; cin >> n; n *= 2; while(1){ if(n <= i * (i+1)){ cout << i << endl; return 0; } i++; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() const ll inf = 1e18; ll X, Y; map<ll,ll> Mp; ll solve(ll N) { if (Mp.find(N)!=Mp.end()) return Mp[N]; if (N<X) { Mp[N] = abs(N-X); return abs(N-X); } if (N==X) { Mp[N] = 0; return 0; } if (N==X+1) { Mp[N] = 1; return 1; } ll res = abs(N-X); if (N%2) { solve((N+1)/2); solve((N-1)/2); res = min(res,Mp[(N+1)/2]+2); res = min(res,Mp[(N-1)/2]+2); } else { solve(N/2); res = min(res,Mp[N/2]+1); } Mp[N] = res; return res; } int main() { cin >> X >> Y; ll ans = solve(Y); cout << ans << endl; }
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define pb push_back #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, n, s) for (int i = (s); i < (n); i++) #define rrep(i, n) for (int i = (n - 1); i >= 0; i--) #define rreps(i, n, s) for (int i = s; i >= n; i--) using ll = long long; using namespace std; constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int MOD = 1000000007; template <typename T> struct RMQ { // const T INF=numeric_limits<T>::max(); int n; vector<T> dat; RMQ(int n_) : n(), dat(n_ * 4, 0) { int x = 1; while (n_ > x) { x *= 2; } n = x; } void update(int i, T y) { i += n - 1; dat[i] = dat[i] ^ y; while (i > 0) { i = (i - 1) / 2; dat[i] = dat[i * 2 + 1] ^ dat[i * 2 + 2]; } } T query(int a, int b) { return query_sub(a, b, 0, 0, n); } T query_sub(int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return 0; } else if (a <= l && r <= b) { return dat[k]; } else { T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); return vl ^ vr; } } }; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, q; cin >> n >> q; RMQ<ll> rmq(n); vector<ll> a(n); rep(i, n) { cin >> a[i]; rmq.update(i, a[i]); } rep(i, q) { ll t, x, y; cin >> t >> x >> y; x--; if (t == 1) { rmq.update(x, y); } else { cout << rmq.query(x, y) << endl; } } return 0; }
#include<iostream> #include<string> #include<vector> #include<iterator> using namespace std; int main() { int a,b,c,d,m,x,y; cin>> a >> b >> c >> d ; if(a>=b) { x=a; } else { x=b; } if(c<=d) { y=c; } else { y=d; } cout<<x-y<<"\n"; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define FOR(i, n, m) for(int i = (int)(n); i < (int)(m); i++) using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; cout << b-c << endl; }
#include<bits/stdc++.h> using namespace std; const int MAX = 101; int64_t dp[MAX][MAX][MAX]; void clean(int K){ for(int i=0; i<MAX; i++){ for(int j=0; j<MAX; j++){ for(int k=0; k<K; k++){ dp[i][j][k] = -1; } } } } int main(){ int64_t N, X; cin >> N >> X; vector<int64_t> vec(N); for(int i=0; i<N; i++){ cin >> vec[i]; } int64_t ans = 1e18; //使う数決めうち for(int i=1; i<=N; i++){ clean(i); dp[0][0][0] = 0; dp[0][1][vec[0]%i] = vec[0]; for(int j=1; j<N; j++){ //今から使うもの for(int k=0; k<=j; k++){ //何個使ったか? for(int l=0; l<i; l++){ //あまりは何か? if(dp[j-1][k][l] != -1){ //cout << j-1 << " " << k << " " << l << " " << dp[j-1][k][l] << endl; //使う時 dp[j][k+1][(l+vec[j])%i] = max(dp[j][k+1][(l+vec[j])%i], dp[j-1][k][l] + vec[j]); //使わない時 dp[j][k][l] = max(dp[j][k][l],dp[j-1][k][l]); } } } } int64_t A = X % i; if(X %i == 0){ A = 0; } if(dp[N-1][i][A] == -1){ continue; } ans = min(ans, (X - dp[N-1][i][A])/i); } cout << ans << endl; }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> const int N = 2e6; using namespace std; int n,a[N + 5],ans,lim,c[N + 5],cnt,t; void dfs1(int x,int ti) { if (ti > t) return; if (x == lim + 1) { c[++cnt] = ti; return; } dfs1(x + 1,ti + a[x]); dfs1(x + 1,ti); } void dfs2(int x,int ti) { if (ti > t) return; if (x == n + 1) { int pos = upper_bound(c + 1,c + cnt + 1,t - ti) - c; pos--; if (pos <= 0) return; ans = max(ans,c[pos] + ti); return; } dfs2(x + 1,ti + a[x]); dfs2(x + 1,ti); } int main() { scanf("%d%d",&n,&t); for (int i = 1;i <= n;i++) scanf("%d",&a[i]); lim = n / 2; dfs1(1,0); sort(c + 1,c + cnt + 1); dfs2(lim + 1,0); cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> ar(n); for (auto& v: ar) { cin >> v; v--; } if (n == 3 && ar[0] == 0 && ar[1] == 2 && ar[2] == 1) { cout << 5 << endl; cout << "1 2 1 2 1" << endl; continue; } vector<int> where(n); for (int i = 0; i < n; ++i) { where[ar[i]] = i; } int move = 0; vector<int> ans; // for (auto v: ar) { // cout << v << " "; // } // cout << "| " << move << endl; int who = -1; for (int i = 0; i < n; ++i) { if (where[i] == i) continue; while (((where[i] - 1) ^ move) & 1) { if ((i ^ move) & 1) { if (i + 1 >= n - 1) { ans.push_back(move & 1); who = move & 1; swap(ar[move & 1], ar[(move & 1) + 1]); } else { swap(ar[i + 1], ar[i + 2]); where[ar[i + 1]] = i + 1; where[ar[i + 2]] = i + 2; ans.push_back(i + 1); } } else { assert(i < n - 1); swap(ar[i], ar[i + 1]); where[ar[i]] = i; where[ar[i + 1]] = i + 1; ans.push_back(i); } move++; // for (auto v: ar) { // cout << v << " "; // } // cout << "| " << move << endl; } while (where[i] > i) { ans.push_back(where[i] - 1); swap(ar[where[i] - 1], ar[where[i]]); where[ar[where[i]]] = where[i]; where[i]--; move++; // for (auto v: ar) { // cout << v << " "; // } // cout << "| " << move << endl; } } if (who != -1) { assert(((who ^ move) & 1) == 0); ans.push_back(who); swap(ar[who], ar[who + 1]); move++; } if (n == 4 && ar[0] == 0 && ar[1] == 2 && ar[2] == 3 && ar[3] == 1) { ans.push_back(2); swap(ar[2], ar[3]); ans.push_back(1); swap(ar[1], ar[2]); move += 2; } assert(move <= n * n); // cout << endl << endl; // for (auto v: ar) { // cout << v << " "; // } // cout << endl; for (int i = 0; i < n; ++i) { assert(ar[i] == i); } cout << ans.size() << endl; for (auto v: ans) { cout << v + 1 << " "; } cout << endl; } }
#include<bits/stdc++.h> typedef uint32_t uint; typedef uint64_t u64; typedef int64_t i64; typedef long double f128; using namespace std; void scan(){} template<typename T,class... Args> void scan(T& n,Args&... args){ cin>>n; scan(args...); } template<typename T> void scan_vec(T start,T end){ T now=start; for(;now!=end;++now){ cin>>(*now); } } void print(){} template<typename T,class... Args> void print(T n,Args... args){ cout<<n; print(args...); } template<typename T> void println(T n){ cout<<n<<endl; } template<typename T,class... Args> void println(T n,Args... args){ cout<<n<<' '; println(args...); } template<typename T> void print_vec(T start,T end){ if(start!=end){ T now=start; cout<<(*now); ++now; for(;now!=end;++now){ cout<<' '<<(*now); } } cout<<endl; } pair<vector<int>,vector<int>> solve(){ random_device seed_gen; mt19937 engine(seed_gen()); int N; scan(N); vector<int> p(N); scan_vec(p.begin(),p.end()); for(int i=0;i<N;++i){ p[i]-=1; } int invcnt=0; for(int i=0;i<N;++i){ for(int j=i+1;j<N;++j){ if(p[i]>p[j]){ ++invcnt; } } } while(1){ vector<int> vec=p; int cnt=invcnt; vector<int> ans; for(int i=0;i<N*N;++i){ if(cnt==0){ println(ans.size()); print_vec(ans.begin(),ans.end()); return {vec,ans}; } bool is_swap=0; for(int j=i%2;j<N-1;j+=2){ if(vec[j]>vec[j+1]){ swap(vec[j],vec[j+1]); ans.push_back(j+1); --cnt; is_swap=1; break; } } if(is_swap){ continue; } if(i%2==0){ uint n=engine()%(N/2); swap(vec[2*n],vec[2*n+1]); ++cnt; ans.push_back(2*n+1); } else{ uint n=engine()%((N-1)/2); swap(vec[2*n+1],vec[2*n+2]); ++cnt; ans.push_back(2*n+2); } } } } int main(){ int T; scan(T); for(int i=0;i<T;++i){ solve(); } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; inline int read(){ int f=0,x=0; char ch=getchar(); while(!isdigit(ch)){f|=(ch=='-');ch=getchar();} while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();} return f?-x:x; } int a[1000]; int sta[1000],top; int pre[1000][1000]; int dp[1000][1000]; int cnt[1000][1000]; int ans; signed main(){ int n=read(); for(int i=1;i<=n;i++){ a[i]=read();a[i]%=200; } dp[0][0]=1; for(int i=1;i<=n;i++){ for(int j=0;j<200;j++)dp[i][j]=dp[i-1][j],pre[i][j]=0,cnt[i][j]=cnt[i-1][j]; for(int j=199;j>=0;j--){ if(dp[i-1][j]&&dp[i-1][(j-a[i]+200)%200]&&cnt[i-1][j]&&(cnt[i-1][(j-a[i]+200)%200]+1)){ puts("Yes");cout<<cnt[i-1][j]<<" "; int now=i-1,sum=j;top=0; while(now){ if(pre[now][sum])sta[++top]=pre[now][sum]; sum-=a[pre[now][sum]]; sum+=200;sum%=200; now--; } sort(sta+1,sta+1+top); for(int l=1;l<=top;l++)cout<<sta[l]<<" ";puts("");top=0; cout<<cnt[i-1][(j-a[i]+200)%200]+1<<" "; sum=(j-a[i]+200)%200,now=i-1; while(now){ if(pre[now][sum])sta[++top]=pre[now][sum]; sum-=a[pre[now][sum]]; sum+=200;sum%=200; now--; } sort(sta+1,sta+1+top); for(int l=1;l<=top;l++)cout<<sta[l]<<" ";top=0;cout<<i<<" ";puts(""); return 0; }else{ if(dp[i-1][(j-a[i]+200)%200]){ dp[i][j]|=dp[i-1][(j-a[i]+200)%200]; cnt[i][j]=cnt[i-1][(j-a[i]+200)%200]+1; pre[i][j]=i; } } } }puts("No"); return 0; }
#include<bits/stdc++.h> using namespace std; void output(vector<int> &a){ cout << a.size(); for(auto &nx : a){ cout << ' ' << nx; } cout << '\n'; } int main(){ int n; cin >> n; vector<int> a(n); for(auto &nx : a){cin >> nx;} vector<vector<int>> bk(200,vector<int>(0)); int cnt=min(n,8); for(int i=1;i<(1<<cnt);i++){ int sig=0; vector<int> s; for(int j=0;j<cnt;j++){ if(i&(1<<j)){ s.push_back(j+1); sig+=a[j];sig%=200; } } if(bk[sig].size()!=0){ cout << "Yes\n"; output(bk[sig]); output(s); return 0; } else{bk[sig]=s;} } cout << "No\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define int long long #define pb push_back #define mp make_pair #define for0(i,n) for(int i=0;i<n;i++) #define for1(i,n) for(int i=1;i<=n;i++) #define w(x) int x;cin>>x;while(x--) #define pii pair<int,int> #define vi vector<int> #define vpii vector<pair<int,int>> #define vvi vector<vector<int>> #define precision(x,k) fixed<<setprecision(k)<<x #define inf 1e16 typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>ordered_set; int arr[300005]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif // cout << "YES"; int n; cin >> n; int mini = 1e18; //cout << "YES" << endl; int i=1; for0(b, 61) { int k =i; int a = n /k; int c = n % k; int ans=a + b + c; mini = min(ans, mini); i=i*2; } cout << mini << endl; return 0; }
#include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define ll long long int #define ld long double #define pb push_back #define fi first #define se second #define all(x) x.begin(),x.end() #define mem(x,y) memset(x,y,sizeof(x)) #define sz(x) (int)x.size() #define pii pair<int,int> #define pll pair<ll,ll> #define INF 1e9 #define INFL 1e18 #define mod 1000000007 //#define mod 998244353 #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; //using namespace __gnu_pbds; //typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os; //typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> os_pair; ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;} ll powm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} //cout<< fixed << setprecision(10) //__builtin_popcountll() //max no of prime factor 5*1e5=7, 1e6=8, 1e9=10; int main(){ fast; ll n,i,j; cin>>n; ll ans=INFL; for(i=0;i<=60;i++) { ll x=power(2,i); ans=min(ans,n/x+n%x+i); } cout<<ans; }
#include <stdio.h> #include <limits.h> #include <iostream> using namespace std; int main(){ long long a,b,c; cin >> a >> b>>c; long long sum =0; a= a%998244353; b = b%998244353; c = c%998244353; long long mod = 998244353; // for(int i = 1 ; i <= a ; i++){ // for(int j = 1 ; j <= b; j++){ // for(int k =1; k <= c;k++){ // sum+=(i*j*k)%998244353; // } // } // } a = ((a*(a+1))/2)%998244353; b = ((b*(b+1)/2))%998244353; c = (((c*(c+1)/2))%998244353); sum = (a*b)%mod; sum = (sum*c)%mod; cout << sum; return 0; }
//a #include<iostream> #include<algorithm> #include<vector> #include<string> #include<utility> #include<map> #include<set> #include <sstream> #include<queue> #include<stack> #include<functional> #include<math.h> #include <iomanip> #include <regex> #include <initializer_list> #include <numeric> using namespace std; int main(void){ int A,B,C; cin >> A >> B >> C; int maxi ,mid,mini; maxi = max(A,max(B,C)); mini = min(A,min(B,C)); mid = (A+B+C) -maxi - mini; if((maxi - mid) == (mid - mini)){ cout << "Yes" << endl; }else{ cout << "No" << endl; } }
#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 a,b; cin >> a >> b; cout << setprecision(10) << fixed << (double)b*a/100 << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef vector<bool> vb; 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;} int const INF = 1 << 30; const int dx[] = {1, 0,-1, 0}; const int dy[] = {0, 1, 0,-1}; int main() { int h,w; cin >> h >> w; vector<string> g(h); rep(i,h) cin >> g[i]; int res = 0; rep(i,h) { rep(j,w) { if(g[i][j] == '#') continue; rep(k,4) { int ni = i + dx[k]; int nj = j + dy[k]; if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if(g[ni][nj] == '.') res++; } } } cout << res/2 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 0x3f3f3f3f int h,w; const int maxn=2005; char a[maxn][maxn]; vector<pair<int,int> > e[maxn*maxn]; int dis[maxn*maxn]; bool vis[maxn*maxn]; int dr[4]={0,0,1,-1}; int dc[4]={1,-1,0,0}; int common[26]; int trans(int r,int c) { return r*w+c; } bool inboard(int r,int c) { return r>=0&&r<h&&c>=0&&c<w; } int main(void) { cin>>h>>w; int s,g; for(int i=0;i<26;++i) common[i]=h*w+i; for(int i=0;i<h;++i) for(int j=0;j<w;++j) cin>>a[i][j]; for(int i=0;i<h;++i) for(int j=0;j<w;++j) { char ch=a[i][j]; if(ch=='#') continue; for(int k=0;k<4;++k) if(inboard(i+dr[k],j+dc[k])&&a[i+dr[k]][j+dc[k]]!='#') e[trans(i,j)].push_back({trans(i+dr[k],j+dc[k]),1}), e[trans(i+dr[k],j+dc[k])].push_back({trans(i,j),1}); if(ch=='S') s=trans(i,j); else if(ch=='G') g=trans(i,j); else if(ch!='.') e[trans(i,j)].push_back({common[ch-'a'],0}), e[common[ch-'a']].push_back({trans(i,j),1}); } priority_queue<pair<int,int> > pq; memset(dis,inf,sizeof(dis)); memset(vis,0,sizeof(vis)); pq.push({0,s}); dis[s]=0; while(!pq.empty()) { auto now=pq.top(); pq.pop(); if(vis[now.second]) continue; vis[now.second]=true; int id=now.second; int d=-now.first; for(auto x:e[id]) if(dis[x.first]>dis[id]+x.second) dis[x.first]=dis[id]+x.second,pq.push({-dis[x.first],x.first}); } if(dis[g]!=inf) cout<<dis[g]<<endl; else cout<<-1<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define all(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define REP(i,n) for(int _n=n, i=0;i<_n;++i) #define FOR(i,a,b) for(int64_t i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int64_t i=(a),_b=(b);i>=_b;--i) using ull = uint64_t; using ll = int64_t; using PII = pair<int, int>; using VI = vector<int>; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; vector<string> grid(h); vector<VI> dp(h, VI(w, -1)); REP(i, h) cin >> grid[i]; queue<PII> Q; vector<vector<PII>> letters(26); REP(i, h) { REP(j, w) { if (grid[i][j] == 'S') { Q.push({i, j}); dp[i][j] = 0; } if (grid[i][j] >= 'a' && grid[i][j] <= 'z') { letters[grid[i][j] - 'a'].pb({i, j}); } } } vector<bool> F(26, false); int d1[4] = {1, -1, 0, 0}; int d2[4] = {0, 0, -1, 1}; while (!Q.empty()) { auto [r, c] = Q.front(); Q.pop(); if (grid[r][c] == 'G') { cout << dp[r][c] << "\n"; return 0; } if (grid[r][c] >= 'a' && grid[r][c] <= 'z' && !F[grid[r][c] - 'a']) { F[grid[r][c] - 'a'] = true; for (auto p : letters[grid[r][c] - 'a']) { if (p.first == r && p.second == c) continue; if (dp[p.first][p.second] != -1) continue; Q.push({p.first, p.second}); dp[p.first][p.second] = dp[r][c] + 1; } } FOR(i, 0, 3) { int rr = r + d1[i]; int cc = c + d2[i]; if (rr < 0 || rr >= h || cc < 0 || cc >= w) continue; if (grid[rr][cc] == '#') continue; if (dp[rr][cc] != -1) continue; dp[rr][cc] = dp[r][c] + 1; Q.push({rr, cc}); } } cout << "-1\n"; }
#include<bits/stdc++.h> using namespace std; int main(){ long long int n,i,j,x=1,y=1; cin>>n; for(i=1;;i++){ x*=3; if(x>n){ break; } for(j=1;;j++){ y*=5; if(x+y==n){ cout<<i<<" "<<j<<endl; return 0; } else if(x+y>n){ y=1; break; } } } cout<<"-1"<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; if(n%2==0){ cout << "White" <<endl; }else{ cout << "Black" <<endl; } return 0; }
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wfloat-conversion" #include <iostream> // IO #include <cmath> // sqrt, trig, ceil, log #include <utility> // swap, pair // #include <chrono> // non c style time // #include <algorithm> // sort, binary search, reverse, merge // #include <bitset> // #include <cstdio> // #include <cstdlib> //abs, atoi, rand // #include <iterator> // #include <map> // #include <queue> // #include <set> // #include <stack> // #include <string> // std string // #include <vector> // #include <complex> // #include <deque> // #include <iomanip> // setprecision // #include <tuple> #define fo(i, a, b) for(i=a; i<=b; i++) #define ro(i, b, a) for(i=b; i>=a; i--) #define foe(it, x) for(auto it=x.begin(); it!=x.end(); it++) #define ff first #define ss second #define pb push_back #define fil(x, y) memset(x, y, sizeof(x)) #define deb(x) cout << #x << " " << x << "\n" #define sz(a) ((int)(a.size())) using namespace std; using ll = long long; const ll N = 3e3+3; const int inf = 1e9+3; const ll M = 1e9+7; const ll T = 998244353; const double pi = acos(-1); void solve() { ll n,i,j,ans=0;cin>>n; ll dp[n+1][n+1]; ll a[n+1]; ll p[n+1]; p[0]=0; fo(i,1,n){ cin>>a[i]; p[i]=p[i-1]+a[i]; } fo(i,0,n){ fo(j,0,n){ dp[i][j]=0; } } dp[0][0]=1; for(j=1;j<=n;j++){ ll adj[n+1]; fo(i,0,n){ adj[i]=0; } for(i=0;i<=n;i++){ dp[i][j]=(dp[i][j]+(adj[p[i]%j]))%M; adj[p[i]%j]=(adj[p[i]%j]+dp[i][j-1])%M; } } for(j=1;j<=n;j++){ ans=(ans+dp[n][j])%M; } cout << ans << "\n"; } signed main() { // auto s1 = std::chrono::system_clock::now().time_since_epoch(); // auto start_time = std::chrono::duration_cast<std::chrono::microseconds>(s1).count(); ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef Holmes7 freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif int _t=1,i; //cin >> _t; for(i=1;i<=_t;i++){ //cout << "Case #" << i << ": "; solve(); } // auto s2 = std::chrono::system_clock::now().time_since_epoch(); // auto end_time = std::chrono::duration_cast<std::chrono::microseconds>(s2).count(); // cerr << "Time: " << (end_time-start_time) << "\n"; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) #include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<cstring> #include<bitset> #include<stack> #include<time.h> #define X first #define Y second #define PB push_back #define MP make_pair #define scd(a) scanf("%d",&a) #define scdd(a,b) scanf("%d%d",&a,&b) #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define ALL(x) x.begin(),x.end() #define sz(a) ((int)a.size()) #define getmid ((l+r)>>1) #define mst(var,val) memset(var,val,sizeof(var)) #define IOS ios::sync_with_stdio(false);cin.tie(0) #define lowbit(x) x&(-x) #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define ls rt<<1 #define rs rt<<1|1 using namespace std; #ifdef local #define dbg(args...) cout << #args << " -> ", err(args); void err(){ cout << endl; } template<typename T, typename... Args> void err(T a, Args... args){ cout << a << ' '; err(args...); } #else #define dbg(args...) #endif // local typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair <int, ll> pil; typedef pair <double, double> pdd; const int inf=0x3f3f3f3f; const long long INF=0x3f3f3f3f3f3f3f3fLL; const double PI=acos(-1.0); const long double eps=1e-8; const int mod=998244353; const int maxn=2e5+100; const int N=2e5+100; const int M=(1<<20)+10; const ll mm=(1LL<<32); template <class T> inline void read(T &x) { x = 0; char c = getchar(); bool f = 0; for (; !isdigit(c); c = getchar()) f ^= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x = f ? -x : x; } template <class T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } T y = 1; int len = 1; for (; y <= x / 10; y *= 10) ++len; for (; len; --len, x %= y, y /= 10) putchar(x / y + 48); } ll qpow(ll a,ll b,ll mod) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return ans; } int inv[N],fac[N]; void init() { fac[0]=1; for(int i=1;i<N;++i) fac[i]=1ll*fac[i-1]*i%mod; inv[N-1]=qpow(fac[N-1],mod-2,mod); for(int i=N-2;i>=0;--i) inv[i]=1ll*inv[i+1]*(i+1)%mod; } int C(int n,int m) { return 1ll*fac[n]*inv[n-m]%mod*inv[m]%mod; } int a[N],pw[N][305]; int n,k; void solve(int x) { int tmp=0; for(int i=1;i<=n;++i) tmp=(tmp+qpow(2*a[i],x,mod))%mod; ll ans=0; for(int i=0;i<=x;++i) { ans=(ans+1ll*C(x,i)*pw[n][i]%mod*pw[n][x-i])%mod; } ans=(ans-tmp+mod)%mod; ans=ans*inv[2]%mod; cout<<ans<<"\n"; } int main() { #ifdef local freopen("in.txt","r",stdin); #endif // local IOS;cout.tie(0); init(); cin>>n>>k; rep1(i,n) cin>>a[i]; for(int y=0;y<=k;++y) { for(int i=1;i<=n;++i) pw[i][y]=(pw[i-1][y]+qpow(a[i],y,mod))%mod; } rep1(i,k) solve(i); return 0; }
#include<bits/stdc++.h> #include<iostream> #include<string> #include<cmath> #include<cstdio> #include<cctype> #include<cstring> #include<iomanip> #include<cstdlib> #include<ctime> #include<set> #include<map> #include<utility> #include<queue> #include<vector> #include<stack> #include<sstream> #include<algorithm> /************************************************/ #define rep(i,n) for(int i=0;i<n;i++) #define m_p make_pair #define pb push_back #define fr first #define se second #define ford(i,n) for(int i=n-1;i>=0;i--) #define forn(i,a,n) for(int i=a;i<n;i++) #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++) #define pii pair<int,int> #define vi vector<int> #define ll long long #define vll vector<ll> #define sz(s) (int)(s.size()) #define all(s) s.begin(),s.end() #define zero(x) memset(x,0,sizeof(x)) #define vii vector<pair<int,int> > #define mpis map<int,string> #define mpii map<int,int> #define mpsi map<string,int> #define re return #define mod 1000000007 /************************************************/ using namespace std; long long get(){ char c=getchar(); long long x=0LL; while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9'){ x*=10LL; x+=(c-'0'); c=getchar(); } return x; } string i_t_s(int x){ string t=""; while(x){ t+=x%10+'0'; x/=10; } reverse(all(t)); re t; } int s_t_i(string t){ int x=0; rep(i,sz(t)){ x=x*10+(t[i]-'0'); } re x; } ll q_p(ll x,ll y){ ll res=1; x%=mod; while(y){ if(y%2){ res=res*x; res%=mod; } y/=2; x=x*x; x%=mod; } re res; } int n; int a[2001]; set<int>s; int mn=1e9; int main(){ ios::sync_with_stdio(0); scanf("%d",&n); rep(i,n) scanf("%d",&a[i]); rep(i,n) mn=min(mn,a[i]); int ans=0; rep(i,n){ for(int j=1;j*j<=a[i];j++){ if(a[i]%j) continue; if(j>mn) break; s.insert(j); s.insert(a[i]/j); } } set<int> ::iterator it; for(it=s.begin();it!=s.end();it++){ if((*it)>mn) break; int now=0; rep(i,n) if(a[i]%(*it)==0) now=__gcd(now,a[i]); if(now==(*it)&&now<=mn) ans++; } cout<<ans; re 0; } /* 检查循环是rep(i,n)还是rep(i,m)!! */
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int gcd(int x,int y){ if(x<y) swap(x,y); if(y==0) return x; return gcd(x%y,y); } set<int> s; vector<int> a; int main(){ int i,j,n; cin >> n; a.resize(n); for(i=0;i<n;i++) cin >> a[i]; sort(a.begin(),a.end()); a.erase(unique(a.begin(),a.end()),a.end()); int mn = a[0]; for(i=0;i<a.size();i++){ for(j=1;j*j<=a[i];j++){ if(j>mn) break; if(a[i]%j==0){ s.insert(j); if(a[i]/j<=mn) s.insert(a[i]/j); } } } int ans = 0; for(int x:s){ int y = 0; for(i=0;i<a.size();i++){ if(a[i]%x==0) y = gcd(y,a[i]); } if(x==y) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<(int)(n); i++) #define FILL0(x) memset(x,0,sizeof(x)) #define FILL1(x) memset(x,-1,sizeof(x)) using namespace std; typedef long long ll; typedef pair<ll, ll> P; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int h, w; cin >> h >> w; int a[h][w]; int Min = 1000; rep(i, h){ rep(j, w){ cin >> a[i][j]; chmin(Min, a[i][j]); } } ll ans = 0; rep(i, h){ rep(j, w){ ans += a[i][j]-Min; } } cout << ans << endl; return 0; }
#include<iostream> using namespace std; int main() { int H,W; cin >> H >> W; int i,j; int a,MIN = 1000,sum = 0; for(i=0;i<H;i++)for(j=0;j<W;j++) { cin >> a; MIN = min(a,MIN); sum += a; } cout << sum-MIN*H*W << endl; }
/*{{{ #include */ #include <iostream> #include <cstdio> #include <istream> #include <ostream> #include <string> #include <type_traits> #include <vector> #include <set> #include <queue> #include <stack> #include <map> #include <tuple> #include <algorithm> #include <utility> #include <cmath>/*}}}*/ /*{{{ namespace R2357 */ namespace R2357 { inline void IN(void){return;} template<class F,class... R>inline void IN(F& f,R&... r){std::cin>>f;IN(r...);} template <class T>inline void OUT(T x){std::cout<<x<<'\n';} 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;} template<class T=int>T read(){ T x=0; char c; while(((c=getchar())>'9'||c<'0')&&c!='-'); const bool f=(c=='-')&&(c=getchar()); while(x=x*10-48+c,(c=getchar())>='0'&&c<='9'); return f?-x:x; } template<class T>std::istream& operator>>(std::istream& input,std::vector<T>& V){ for(auto& v:V)input>>v; return input; } namespace debug { using namespace std; void Write(void){cout<<endl;} template<class F,class...R>void Write(F& f,R&...r){cout<<' '<<f;Write(r...);} template<class T>void Write(vector<T>& V,int n=-1){ if(n==-1) n = V.size(); for(int i=0;i<n;i++)cout<<' '<<V[i]; cout<<endl; } #include <cxxabi.h> template<class T>string TypeName(T x){ return string(abi::__cxa_demangle(typeid(T).name(), 0, 0, 0)); } } } /*}}}*/ /*{{{ using */ using namespace std; using namespace R2357; using ll=long long; using ld=long double; using pint=pair<int,int>;/*}}}*/ /*{{{ #define */ #define rep(i,a,b) for(ll i=a;i<ll(b);i++) #define repr(i,a,b) for(ll i=a;i>=ll(b);i--) #define each(x,v) for(auto& x:v) #define el '\n' #define ALL(x) x.begin(),x.end() #define ALLR(x) x.rbegin(),x.rend() #define Unique(x) sort(x.begin(),x.end());x.erase(unique(x.begin(),x.end()),x.end()) #define Fill(v,n) fill(v,v+sizeof(v)/sizeof(*v),n) #define ceil_div(a,b) ((a+(b-1))/b) #define max_element(x) *max_element(x.begin(),x.end()) #define INF 1234567890 #define INFL 1122334455667788990ll/*}}}*/ // using namespace debug; int main(){ int n = read(); vector<ll> a(n); cin>>a; vector<ll> sum(n+1);{ rep(i, 0, n) sum[i+1] = sum[i] + a[i]; rep(i, 0, n) sum[i] = sum[i+1]; } // Write("sum"); // Write(sum, n); vector<ll> start(n);{ rep(i, 0, n-1) start[i+1] = start[i] + sum[i]; } // Write("start"); // Write(start); vector<ll> sumMax(n);{ sumMax[0] = sum[0]; rep(i, 1, n) sumMax[i] = max(sumMax[i-1], sum[i]); } // Write("sumMax"); // Write(sumMax); vector<ll> list(n);{ rep(i, 0, n) list[i] = sumMax[i] + start[i]; } // Write("list"); // Write(list); OUT(max(0ll, max_element(list))); return 0; }
#include <bits/stdc++.h> #define rep(i,b) for(int i=0;i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>=(b);i--) #define ALL(a) (a).begin(),(a).end() using namespace std; using ll = long long; using P = pair<int, int>; template<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} else return false;} template<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} else return false;} struct Edge {int to; ll w; Edge(int to, ll w) : to(to), w(w) {}; }; using Graph = vector<vector<Edge>>; const int INF = 1<<30; const ll INFL = 1LL<<60; /* A_012 */ const int h = 30; const int w = 30; const int n = h*w; // 左 上 右 下 vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = { 0, -1, 0, 1}; ll combination(ll n, ll k) { ll r = 1; for (ll d = 1; d <= k; ++d) { r *= n--; r /= d; } return r; } int main () { vector<vector<int>> cost(n, vector<int>(n, INF)); random_device seed_gen; mt19937 engine(seed_gen()); rep(i, n) { if (i % h == h-1) continue; cost[i][i+1] = 4500; cost[i+1][i] = 4500; } rep(i, n-h) { cost[i][i+h] = 4500; cost[i+h][i] = 4500; } rep(ik, 1000) { int b; int sx, sy, gx, gy; cin >> sy >> sx >> gy >> gx; string ans; string str = ""; int x = sx; int y = sy; while (x != gx) { if (x < gx) { str += "R"; x++; } else if (x > gx) { str += "L"; x--; } } while (y != gy) { if (y < gy) { str += "D"; y++; } else if (y > gy) { str += "U"; y--; } } if (ik >= 200) { ll combi = combination((ll)abs(gy-sy)+abs(gx-sx), (ll)abs(gx-sx)); int m; if (combi > 1000) m = 1000; else m = (int)combi; /* シャッフルして文字列生成 */ set<string> cand; cand.insert(str); rep(ij, m) { shuffle(str.begin(), str.end(), engine); cand.insert(str); } /* コスト計算 */ int min_cost = INF; for (auto v : cand) { int nowx = sx, nowy = sy; int now = nowy*h + nowx; int c = 0; rep(j, v.size()) { if (v[j] == 'L') { c += cost[now][now-1]; now--; } else if (v[j] == 'R') { c += cost[now][now+1]; now++; } if (v[j] == 'U') { c += cost[now][now-h]; now -= h; } if (v[j] == 'D') { c += cost[now][now+h]; now += h; } } if (min_cost > c) { min_cost = c; ans = v; } } } else { ans = str; } cout << ans << endl; cin >> b; /* コストを再計算(単純分配) */ int nowx = sx, nowy = sy; int now = nowy*h + nowx; int ave = b/(int)ans.size(); rep(i, (int)ans.size()) { if (ans[i] == 'L') { cost[now][now-1] = ave; now--; } else if (ans[i] == 'R') { cost[now][now+1] = ave; now++; } else if (ans[i] == 'U') { cost[now][now-h] = ave; now -= h; } else if (ans[i] == 'D') { cost[now][now+h] = ave; now += h; } } } return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define int long long int #define double long double #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define endl '\n' #define ff first #define ss second #define mp make_pair #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define rev(a) a.rbegin(),a.rend() #define all(a) a.begin(),a.end() #define setbits(x) (__builtin_popcount(x)) #define pi pair<int,int> #define fill(s,x,n) vector<s> x(n); rep(i,0,n) cin>>x[i]; #define precise(a,k) cout<<fixed<<setprecision(k)<<a; #define minpq(type) priority_queue<type,vector<type>,greater<type>> #define debug(...) cerr<<"{ "<<#__VA_ARGS__<<" = "<<(__VA_ARGS__)<<" }"<<endl; #define debugp(...) cerr<<"{ "<<#__VA_ARGS__<<" = "<<"("<<(__VA_ARGS__).ff<<","<<(__VA_ARGS__).ss<<")"<<" }"<<endl; #define debugv(...) cerr<<"{ "; for(int i=0; i<size(__VA_ARGS__); i++) cerr<<#__VA_ARGS__<<"["<<i<<"]"<<" = "<<(__VA_ARGS__)[i]<<" ; "; cerr<<"}"<<endl; #define debugpv(...) cerr<<"{ "; for(int i=0; i<size(__VA_ARGS__); i++) cerr<<#__VA_ARGS__<<"["<<i<<"]"<<" = "<<"("<<(__VA_ARGS__)[i].ff<<","<<(__VA_ARGS__)[i].ss<<")"<<" ; "; cerr<<"}"<<endl; #define mod 998244353 #define inf 1000000007 #define inf64 1000000000000000005 #define PI 3.141592653589793 void solve(){ int n,m; cin>>n>>m; vector<array<int,2>> adj[n+1]; vector<array<int,2>> rev[n+1]; rep(i,0,m){ int u,v,c; cin>>u>>v>>c; adj[u].pb({v,c}); rev[v].pb({u,c}); } rep(i,1,n+1){ set<pi> s; vector<int> d(n+1,inf); d[i]=0; s.insert({0,i}); while(!s.empty()){ int v = s.begin()->ss; s.erase(s.begin()); for(auto pp: adj[v]){ if(d[pp[0]]>d[v]+pp[1]){ s.erase({d[pp[0]],pp[0]}); d[pp[0]] = d[v]+pp[1]; s.insert({d[pp[0]],pp[0]}); } } } int ans = inf; for(auto pp: rev[i]){ if(d[pp[0]]!=inf){ ans = min(ans,d[pp[0]]+pp[1]); } } if(ans==inf) cout<<-1<<endl; else cout<<ans<<endl; } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); /* #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif */ int test; //cin>>test; test=1; // int cse=1; while(test--){ // cout<<"Case #"<<cse<<": ";//<<endl; solve(); //cout<<endl; //cse++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> #define rep(X, N) for (ll X = 0LL; X < (N); X++) #define ALL(V) (V).begin(), (V).end() #define endl "\n" #define pb push_back #define mp make_pair using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; const double PI = 3.1415926535897932384626; const ll MODN = 1000000007; const ll MODN2 = 998244353; int main(){ int n; cin >> n; vector<ll> p(n); rep(i, n) cin >> p[i]; ll diff = 0; bool able = true; rep(i, n){ ll tmp = p[i] - (i + 1); if(tmp == 0){ able = false; break; } diff += tmp; if(i != n - 1){ if(diff == 0 || abs(diff) > n - 1){ able = false; break; } } } if(!able){ cout << -1 << endl; return 0; } vector<pair<ll, int>> v(n); rep(i, n){ v[i] = mp(p[i], i + 1); } sort(ALL(v)); vector<int> checked(n + 1, 0); vector<int> ans; ll k = 1; while(k <= n - 1){ //auto itr = lower_bound(ALL(v), mp(k, 0)); // pの中でkがあるindex int kidx = v[k - 1].second; for(int i = kidx - 1; i >= k; i--){ if(checked[i] == 1){ cout << -1 << endl; return 0; } checked[i] = 1; ans.pb(i); } k = kidx; } rep(i, n - 1){ printf("%d\n", ans[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); ++i) #define ll long long #define ld long double #define ALL(a) (a).begin(), (a).end() #define Mod 1000000007 int main() { int n; cin >> n; ll ans; cin >> ans; vector<vector<ll>> a(2, vector<ll>(n)); a[0][0] = ans; ll x=1, y=0, z; for(int i=1; i < n; i++){ ll b; cin >> b; b %= Mod; a[0][i] += a[0][i-1] % Mod + (x*b) % Mod + a[1][i-1] % Mod + (y*b) % Mod; a[1][i] += a[0][i-1] % Mod - (x*b) % Mod; z = (x+y) % Mod; y = x % Mod; x = z; } ans = (a[0][n-1] + a[1][n-1]) % Mod; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include<string> using namespace std; #define ll long long #define pb push_back long long dp[100010], a[100010]; ll mod = 1e9+7; int main() { int n, prvi; cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; dp[0]=1; dp[1]=2; for(int i=2;i<=n-1;i++) dp[i] = (dp[i-1]+dp[i-2])%mod; if(n==2){ cout<<2*a[1]<<endl; return 0; } long long res = (a[1]*dp[n-1])%mod; for(int i=2;i<=n;i++){ long long pos = (dp[ i-2 ]*dp[n-i])%mod, neg; if(i==2){ neg = dp[n-3]; } else if(i==n){ neg = dp[n-3]; } else{ neg = (dp[i-3]*dp[n-i-1])%mod; } res+=(a[i]*(pos-neg+mod)%mod)%mod; res%=mod; } cout<<res<<endl; return 0; }
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; #define ll long long void solve(){ int n; cin>>n; string s; cin>>s; int count[26]{0}; for(int i=0;i<n;i++) count[s[i]-'a']=1; for(int i=0;i<26;i++) { if(count[i]==0) { cout<<(char)('a'+i)<<endl; return; } } set<string>st; for(int i=0;i<n-1;i++) { string temp=""; temp+=s[i]; temp+=s[i+1]; st.insert(temp); } for(int i=0;i<26;i++) { for(int j=0;j<26;j++) { string temp=""; temp+=(char)('a'+i); temp+=(char)('a'+j); if(st.find(temp)==st.end()) { cout<<temp<<endl; return; } } } set<string>stt; for(int i=0;i<n-2;i++) { string temp=""; temp+=s[i]; temp+=s[i+1]; temp+=s[i+2]; stt.insert(temp); } for(int i=0;i<26;i++) { for(int j=0;j<26;j++) { { string temp="a"; temp+=(char)('a'+i); temp+=(char)('a'+j); if(stt.find(temp)==stt.end()) { cout<<temp<<endl; return; } } } } } int32_t main() { IOS double a,b; cin>>a>>b; cout<<(a/100*b); //cout<<as; }
#include <bits/stdc++.h> //yaad rkhne layak baatein #include <bits/stdc++.h> // bool found = false use it to find some number after the given number with some cond. // while (!found) #include <iostream> // freq.table-> int freq[26] = {0}; for (ll i = 0; i < s1.length(); i++) freq[s1[i] - 'A']++; #define ll long long //[s[i] - 'a'] ->this convers char to corr.int eg. 'c' to 3. // if(a<=min1){ // min2 = min1; // index2 = index1; // min1 = a; // index1 = i; to find index of 2nd smallest element // } // else if(a<=min2){ // min2 = a; // index2 = i; // } #define INF 2000000000 #define pb push_back using namespace std; //cout<<fixed<<setprecision(12)<<ans<<endl; const ll M = 1e9 + 7; #define loop0(i, n) for (ll i = 0; i < n; i++) // str.insert(0, 5, '1'); => it will insert 1 five times in start of str. #define loop00(i, n) for (ll i = 0; i <= n; i++) #define loop1(i, n) for (ll i = 1; i < n; i++) #define loop11(i, n) for (ll i = 1; i <= n; i++) #define loopab(i, a, b) for (ll i = a; i <= b; i++) void flashSpeed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long mod(long long x) { return ((x % M + M) % M); } long long add(long long a, long long b) //to find max. multiple of x less than n=>(n/x)*x { return mod(mod(a) + mod(b)); } long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } ll modPow(ll a, ll b) { if (b == 0) return 1LL; if (b == 1) return a % M; ll res = 1; while (b) { if (b % 2 == 1) res = mul(res, a); a = mul(a, a); b = b / 2; } return res; } const int N = 2e5 + 2; int fact[N]; void precalc() { fact[0] = 1; for (int i = 1; i < N; i++) { fact[i] = mul(fact[i - 1], i); } } ll inv(ll x) { return modPow(x, M - 2); } ll divide(ll a, ll b) { return mul(a, inv(b)); } ll nCr(ll n, ll r) { return divide(fact[n], mul(fact[r], fact[n - r])); } double Round(double var) { float value = (int)(var * 100 + .5); return (float)value / 100; } ll ceils(ll x, ll y) { return x / y + ((x % y) != 0); } ll Gcd(ll a, ll b) { if (b > a) { return Gcd(b, a); } if (b == 0) return a; else return Gcd(b, a % b); } ll lcm(ll a, ll b) { return a / Gcd(a, b) * b; } bool isPal(string s) { for (int i = 0; i < (int)s.size() / 2; i++) { if (s[i] != s[(int)s.size() - 1 - i]) return false; } return true; } ll Sumdigits(ll a) { //two find no.of power of 2 in a number==>while(n%2==0){cnt++;n/=2} ll total = 0; while (a) { total += a % 10; a /= 10; } return total; } bool isPerfectSquare(int n) { for (int i = 1; i * i <= n; i++) { if ((n % i == 0) && (n / i == i)) { return true; } } return false; } bool isPowerOfTwo(int n){ return (ceil(log2(n)) == floor(log2(n))); } void lexosmallest(string s,string c){ string t1 = s; sort(t1.begin(), t1.end()); int index = -1; for (int i = 0; i < s.length(); i++) { if (s[i] != t1[i]) { index = i; break; } } int j; for (int i = 0; i < s.length(); i++) { //khela yha pe hua hai..loop poora chala hai if (s[i] == t1[index]) j = i; } swap(s[index], s[j]); } void solution() { int n; cin>>n; int sum=0; int arr[n]; loop0(i,n){ cin>>arr[i]; if(arr[i]>=10){ arr[i]=arr[i]-10; sum+=arr[i]; } } cout<<sum<<endl; } int main() { flashSpeed(); int t=1; // cin>>t; while(t--) { solution(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int ans = (n - 1); cout << ans; return 0; }
#include <iostream> using namespace std; int main() { int n,count=0; cin>>n; for(int i=1;i<n;i++) { for(int j=1;j<n;j++) { if(i+j==n) count++; } } cout<<count; return 0; }
// <-- Coded by Pasindu_Piumal --> #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef double db; typedef string str; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef pair<db,db> pd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<db> vd; typedef vector<str> vs; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pd> vpd; #define ft front() #define bk back() #define pf push_front #define pb push_back #define eb emplace_back #define f first #define s second #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORi(i,a,b) for(int i=(a);i>=(b);i--) #define FORZ(i,a) for(int i=0;i<(a);i++) #define FORZi(i,a) for(int i=(a)-1;i>=0;i--) #define trav(a,x) for (auto& a: x) #define what_is(x) cout << #x << " is " << x << "\n" #define printl(a) cout << a << "\n" #define prints(a) cout << a << " " #define printall(x) FORZ(i,sz(x))prints(x[i]) #define nextl cout << "\n" ll mxN=1e5+1; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; if(n%100==0) { cout << "100\n"; } else { cout << 100-(n%100) << "\n"; } return 0; }
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define ll long long int #define pii pair<int,int> #define pll pair<long long int,long long int> #define pci pair<char,int #define mii map<int,int> #define mll map<long long int,long long int> #define mci map<char,int> #define umii unordered_map<int,int> #define umll unordered_map<long long int,long long int> #define umci unordered_map<char,int> #define F first #define S second #define pb push_back #define endl '\n' #define mod 998244353 using namespace std; bool isPrime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } ll power(ll x,ll n) {ll x1=0; ll result=1; while(n>0) { if(n % 2 ==1) result=(result * x); // if(result>=1000000001) return 0; x=(x*x); // if(x>=1000000001) return 0; n=n/2; } return result; } int main(){ IOS; ll t;t=1;//cin>>t; while(t--){ ll n;cin>>n; cout<<100-n%100<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using pll = pair<ll, ll>; #define sz(x) (int)(x).size() template <typename T> bool ckmin (T& a, T b) { return (b < a) ? a = b, 1 : 0; } template <typename T> bool ckmax (T& a, T b) { return (b > a) ? a = b, 1 : 0; } #ifdef XVENOM #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: " << dbg_str(__VA_ARGS__) #else #define dbg(...) ; #endif template <typename U, typename V> string to_string (pair<U, V>); string to_string (const char* e) { return "\"" + string(e) + "\""; } string to_string (string& e) { return "\"" + e + "\""; } string to_string (char e) { return "\'" + string(1, e) + "\'"; } string to_string (bool e) { return e ? "true" : "false"; } template <typename T> string to_string (T e) { string s = "[ "; for (auto& x : e) s += to_string(x) + " "; return s + "]"; } template <typename U, typename V> string to_string (pair<U, V> e) { return "(" + to_string(e.first) + ", " + to_string(e.second) + ")"; } string dbg_str () { return " \n"; } template <typename U, typename... V> string dbg_str (U u, V... v) { return " " + to_string(u) + dbg_str(v...); } /* *** */ const int N = 55; const ll Mod = 998244353; int n, k, a[N][N]; int s[N], p[N]; ll fac[N]; ll mul (ll u, ll v) { return ((u % Mod) * (v % Mod)) % Mod; } ll add (ll u, ll v) { return (u + v) % Mod; } void init() { for (int i = 0; i < n; i++) { s[i] = 1; p[i] = i; } } int leader (int x) { return p[x] == x ? x : (p[x] = leader(p[x])); } void combine (int u, int v) { u = leader(u); v = leader(v); if (u == v) return; if (s[u] > s[v]) swap(u, v); p[u] = v; s[v] += s[u]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); fac[0] = 1; for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i); cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } auto can_swap_cols = [&] (int u, int v) { for (int i = 0; i < n; i++) { if (a[i][u] + a[i][v] > k) return false; } return true; }; auto can_swap_rows = [&] (int u, int v) { for (int i = 0; i < n; i++) { if (a[u][i] + a[v][i] > k) return false; } return true; }; ll ans = 1; init(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (can_swap_rows(i, j)) combine(i, j); } } for (int i = 0; i < n; i++) if (leader(i) == i) { ans = mul(ans, fac[s[i]]); } init(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (can_swap_cols(i, j)) combine(i, j); } } for (int i = 0; i < n; i++) if (leader(i) == i) { ans = mul(ans, fac[s[i]]); } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; #define boostIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define rep(i, a, b) for (int i = a; i < b; i++) #define repn(i, n) for (int i = 1; i <= n; i++) #define rrep(i, b, a) for (int i = b; i >= a; i--) #define int long long #define fi first #define se second #define pb push_back #define pii 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 trav(a, x) for (auto &a : x) #define ppcll(x) __builtin_popcountll(x) void amn(int &x, int y) { x = min(x, y); } void amx(int &x, int y) { x = max(x, y); } #define mod 1000000007 const long long INF = 1e18; // const int N = 5e5 + 5; signed main() { boostIO; int n, m; cin >> n >> m; int a[n], b[m]; set<int> sa, sb; for (int i = 0; i < n; i++) { cin >> a[i]; sa.insert(a[i]); } for (int i = 0; i < m; i++) { cin >> b[i]; sb.insert(b[i]); } set<int> ans; for (int i = 0; i < n; i++) { if (sb.count(a[i]) == 0) { ans.insert(a[i]); } } for (int i = 0; i < m; i++) { if (sa.count(b[i]) == 0) { ans.insert(b[i]); } } for (int i : ans) { cout << i << " "; } cout << endl; return 0; } // Play Hard Kiddo...
#include<bits/stdc++.h> using namespace std; #define A(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define int int64_t void _read(); int mod; inline int inv(int a) { a %= mod; if (a < 0) a += mod; int b = mod, u = 0, v = 1; while (a) { int t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += mod; return u; } void test() { int n,s,k; cin >> n >> s >> k; mod = n; // kx == s; int d = __gcd(k,n); if(s % d) { cout << -1 << '\n'; return; } k /= d; s /= d; n /= d; mod = n; s %= mod; k %= mod; int y = inv(k); int ans = (s * y) % mod; cout << mod - ans << '\n'; } main () { _read(); int tc; cin >> tc; while(tc--) test(); return 0; }; void _read() { ios_base :: sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL freopen("input.txt","r",stdin); #endif }
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> #include <iomanip> #define ll long long int #define inf 1000000000000 #define mod 1000000007 // templates #define all(v) v.begin(),v.end() #define fi first #define se second #define sz(x) (int)x.size() #define ps(x,y) fixed<<setprecision(y)<<x #define fo(i,a,b) for(int i=a;i<b;i++) #define ro(i,a,b) for(int i=a;i>=b;i--) #define vi vector<int> #define vl vector<ll> #define sc(n) scanf("%d",&n) #define sl(n) scanf("%lld",&n) #define pr(n) printf("%d\n",n) #define pl(n) printf("%lld\n",n) #define prs(n) printf("%d ",n) #define pls(n) printf("%lld ",n) using namespace std; ll GCD(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1LL; y = 0LL; return a; } ll x1, y1; ll d = GCD(b, a % b, x1, y1); x = y1; y = x1 - y1 * 1LL * (a / b); return d; } void solve() { ll n, s, k; sl(n), sl(s), sl(k); ll x, y; ll gc = __gcd(n, k); if (s % gc) { pl(-1LL); } else { // y->loops x->moves x*k+s=y*n => (x*k-y*n=-s) n /= gc, s /= gc, k /= gc; gc = GCD(k, n, x, y); ll mod_inv = (x % n + n) % n; ll ans = ((-s * mod_inv) % n + n) % n; pl(ans); } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; scanf("%d", &t); while (t--) { solve(); } }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; typedef long long int lld; const lld N = 200043; const lld MOD = 1000000007; lld add(lld x, lld y) { x =((x%MOD)+(y%MOD))%MOD; while(x >= MOD) x -= MOD; while(x < 0) x += MOD; return x; } lld mul(lld x, lld y) { return ((x%MOD)*(y%MOD))% MOD; } lld binpow(lld x, lld y) { lld z = 1; while(y) { if(y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } lld inv(lld x) { return binpow(x, MOD - 2); } lld divide(lld x, lld y) { return mul(x, inv(y)); } // Combinations /* lld fact[N]; void precalc() { fact[0] = 1; for(lld i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i); } lld C(lld n, lld k) { if(k>n) return 0; return divide(fact[n], mul(fact[k], fact[n - k])); } */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); lld t,i,j; t=1; //cin>>t; while(t--) { lld n; cin>>n; lld ctr=1; lld ptr=0; lld ans=LLONG_MAX; while(ctr<=n) { ans=min(ans,n/ctr+ptr+n-((n/ctr)*ctr)); ptr++; ctr=ctr*2; } cout<<ans<<endl; } }
#include <bits/stdc++.h> using namespace std; // 型 #define ll int64_t // ll -> int64_t using graph = vector<vector<int>>; // グラフ型 // for文 #define lp(i,n) for(int i=0;i<n;i++) // 変数 i をおいて n 回繰り返し #define lp_(i,n) for(int i=0;i<=n;i++) // 条件に=つき #define lpp(i,n,m) for(int i=n;i<m;i++) // 開始と終了を指定して繰り返し #define lpp_(i,n,m) for(int i=n;i<=m;i++) // 開始と終了を指定,条件に=つき // vector定義 #define _GLIBCXX_DEBUG #define vec(v,n) vector<int> v(n) // int #define vec_(v,n,m) vector<int> v(n,m) // int 初期条件あり #define vec64(v,n) vector<ll> v(n) // int64_t #define vec64_(v,n,m) vector<ll> v(n,m) // int64_t 初期条件あり #define vecc(v,n) vector<char> v(n) // char #define vecs(v,n) vector<string> v(n) // string #define vece(v) vector<int> v; // int 空 #define vecec(v) vector<char> v; // char 空 #define vec2i(v,h,w) vector<vector<int>> v(h,vector<int>(w)) // 二次元配列 int #define vec2c(v,h,w) vector<vector<char>> v(h,vector<char>(w)) // 二次元配列 char // vector,string操作 #define all(v) v.begin(),v.end() #define back(v) v.back() // 最後の要素 #define sum(v) accumulate(all(v),0) // 総和 #define sort(v) sort(all(v)) // ソート #define reverse(v) reverse(all(v)) // 反転 #define lower(v,x) lower_bound(all(v),x) // x<=a[i]となる最小のi / a[i]<xとなるiの個数 #define cou(v,n) count(all(v),n) // n の出現回数を数える #define pb(v,n) v.push_back(n) // 最後尾に n を追加 #define ins(v,i,n) v.insert(v.begin()+i,n) // 場所を指定してv[i]に n を追加 #define del(v) v.pop_back() // 最後尾を削除 #define del0(v) v.erase(v.begin()) // 最初の要素v[0]を削除 #define del_(v,i) v.erase(v.begin()+i) // 場所を指定してv[i]を削除 #define del__(v,i,j) v.erase(v.begin()+i,v.begin()+j+1) // 範囲を指定してv[i]~v[j]を削除 #define sub(s,i,j) s.substr(i,j-i+1) // 範囲を指定してs[i]~s[j]を取得【stringのみ】 // others const ll MOD=1000000007; const ll INF=(1LL<<60); #define under(n) cout<<fixed<<setprecision(n) // 小数点以下の桁数を指定 #define cout(n) cout<<n<<endl int main() { ll n,ans=INF; cin>>n; ll t=1; lp(i,61) { ll a=n/t; ll c=n%t; t*=2; ans=min(ans,a+i+c); } cout(ans); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) typedef long long ll; using namespace std; int main() { ll N; cin >> N; N *= 2; int ans = 0; for (ll i = 1; i * i <= N; ++i) { if (N % i == 0) { if ((i + N / i) % 2 == 1) { ans += 2; if (i == N / i) ans--; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <chrono> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; /*............................................................................*/ #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> /* find_by_order(x) give iterator of the index x order_by_key(x) give the position where x will be placed*/ #define int long long #define lop(i,a,b,c) for (int i=a;i<b;i+=c) #define rlop(i,a,b,c) for (int i=a-1;i>=b;i-=c) #define prii pair <int,int> #define PB push_back #define S second #define F first #define all(x) x.begin(),x.end() #define vvii vector < vector < int > > #define vii vector <int> #define count_1(x) __builtin_popcount(x) #define cn(a) scanf("%lld",&a); #define cn_vl(arr,n) lop(i,0,n,1){scanf("%lld",arr+i);} #define shw_vl(arr,n) lop(i,0,n,1){printf("%lld ",arr[i]);}printf("\n"); #define shw(CON) cout << &("NO\0YES"[3 * CON]) << '\n'; const int MAX=2e5+10; /*......................................................................*/ void fastscan(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); } for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative) number *= -1; } struct dsu { int parent[MAX];int size[MAX]; void init(){ lop (i,1,MAX,1){parent[i]=i;size[i]=1;} } int trace(int a){ return (a==parent[a])?a:parent[a]=trace(parent[a]); } bool connect(int a,int b){ if ((a=trace(a))==(b=trace(b)))return false; if (size[b]>size[a])swap(a,b); parent[b]=a;size[a]+=size[b]; return true; } }ori; vii adj[MAX]; typedef long double ld; void solve(){ int n,m;cn(n)cn(m) ld a=(ld)m/(ld)100; printf("%0.6Lf",a*n); } int32_t main(){ int t;t=1; // cin>>t; while (t--){ auto t_1=chrono::high_resolution_clock::now(); solve(); auto t_2=chrono::high_resolution_clock::now(); // cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; priority_queue<long long> max; vector<long long> sum(N); long long ans = 0; for (int i = 0; i < N; i++) { long long a; cin >> a; max.push(a); if (i == 0) { sum.at(0) = a; } else { sum.at(i) = sum.at(i - 1) + a; } ans += sum.at(i); cout << ans + max.top() * (i + 1) << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; #define rep(i,a,b) for(ll i=a;i<b;i++) #define rrep(i,a,b) for(lint i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() ///#pragma GCC optimize ("-O3") ll input(){ ll n; cin >> n; return n; }///input関数 ll combination(ll n,ll r){ if (r == 0 || r == n) return (1); else if (r == 1) return (n); return (combination(n-1,r-1) + combination(n-1,r)); }///nCr ll facctorialMethod(ll k){ ll sum = 1; for (ll i = 1; i <= k; ++i) { sum *= i; } return sum; }///階乗 int main(){ ll n = input(); vector<ll>a; ll a_max = 0; ll ans = 0; ll total = 0; ll pir = 0; rep(i,0,n){ a.push_back(input()); pir += total; total += a.at(i); a_max = max(a_max,a.at(i)); ans = pir + total + a_max*(i+1); cout << ans << endl; } }
/* Auther: ghoshashis545 Ashis Ghosh College: jalpaiguri Govt Enggineering College Date:13/06/2020 */ #include<bits/stdc++.h> #include<string> #include<algorithm> using namespace std; #define ll long long #define int long long #define ld long double #define ff first #define clr(a,x) memset(a,x,sizeof(a)) #define ss string #define se second #define alt(v) v.begin(),v.end() #define pb emplace_back #define mp make_pair #define ipair pair<int,int> #define fab(i,a,b) for(ll i=(a);i<(b);i++) #define fba(i,a,b) for(ll i=(b);i>=(a);i--) #define arr(i,n) fab(i,0,n) ll mod=1000000007; // int mod = 998244353; int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1}; void DBG() { cerr << "]" << endl; } template<class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if(sizeof...(t)) cerr << ", "; DBG(t...); } #ifdef _DEBUG #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #else #define dbg(...) 0 #endif const int inf = 1e18; bool test = 0; const int N = 1e5+5; int n,x; void solve() { cin >>n; set<int>st; for(int i = 1; i <= n; ++i) { cin >>x; st.insert(x); } while(st.size() > 1){ int mnx = *st.begin(); int mx = *st.rbegin(); st.erase(mx); st.insert(mx-mnx); } cout<<*st.begin()<<"\n"; } clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } signed main() { ios_base::sync_with_stdio(false);cin.tie(NULL); int t=1; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // precompute(); if(test) cin>>t; while(t--) { solve(); } DBG(getCurrentTime()); return 0; }
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int n; int arr[100010]; bool judge(int k) { for(int i=1; i<=n; i++) if(arr[i] % k != 0) return false; return true; } int main() { int ans = 1; cin >> n; for(int i=1; i<=n; i++) cin >> arr[i]; sort(arr+1, arr+n+1); for(int i=2; i<=arr[1]; i++) if(judge(i)) ans = i; cout << ans; return 0; }
#include<bits/stdc++.h> #define ll long long #define pll pair<ll, ll> #define fi first #define se second #define pb push_back using namespace std; const int N = 1e5+5; const ll mod = 1e9+7; ll n, m, t, k, ans, tong, a[N], b[N]; void sol() { cin >> n >> m; for(int i = 1; i <= n; i ++)cin >> a[i]; vector<ll> kq; k = n / 2; for(int i = 1; i < (1<<k); i ++) { ll total = 0; for(int j = 0; j < k; j ++) { if((i>>j)&1)total += a[j+1]; } if(total <= m)ans = max(ans, total); kq.pb(total); } sort(kq.begin(), kq.end()); for(int i = 1; i < (1<<(n-k)); i ++) { ll total = 0; for(int j = 0; j < n-k; j ++) { if((i>>j)&1)total += a[j+k+1]; } t = upper_bound(kq.begin(), kq.end(), m-total)-kq.begin(); if(t != 0) { t = kq[t-1]; ans = max(ans, t+total); } if(total <= m)ans = max(ans, total); } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ntest = 1; //cin >> ntest; while(ntest -- > 0)sol(); } /* 2 10 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 3 1 2 3 2 */
#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 pair<int, int> pii; typedef pair<ll, ll> pll; #define REP(i, n) for(int i=0; i<(n); ++i) #define FOR(i, a, b) for(int i=(a); i<(b); ++i) #define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i) #define DEBUG(x) cout<<#x<<": "<<(x)<<'\n' #define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n' #define ALL(a) (a).begin(), (a).end() template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;} template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;} constexpr ll MOD=1000000007ll; // constexpr ll MOD=998244353ll; #define FIX(a) ((a)%MOD+MOD)%MOD const double EPS=1e-9; #define EQ0(x) (abs((x))<EPS) #define EQ(a, b) (abs((a)-(b))<EPS) vl v; int main() { ios::sync_with_stdio(false); cin.tie(0); // cout<<setprecision(10)<<fixed; int n; ll t, a[50]; cin>>n>>t; REP(i, n){ cin>>a[i]; } if(n==1){ cout<<(a[0]<=t ? a[0] : 0)<<'\n'; return 0; } int m=n/2; int vsize=0; REP(i, (1<<(n-m))){ ll tmp=0; REP(j, n-m){ if(i>>j&1){ tmp+=a[m+j]; } } v.push_back(tmp); } sort(ALL(v)); ll ans=0; REP(i, (1<<m)){ ll tmp=0; REP(j, m){ if(i>>j&1){ tmp+=a[j]; } } if(tmp>t){ continue; } auto itr=upper_bound(ALL(v), t-tmp); CHMAX(ans, *(itr-1)+tmp); } cout<<ans<<'\n'; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define mod 1000000007 ll fa[200005]; ll p[200005]; ll gf(ll v){ if(fa[v]==v){ return v; } fa[v]=gf(fa[v]); return fa[v]; } void merge(ll i,ll j){ i=gf(i); j=gf(j); if(i!=j){ fa[j]=i; } } int main() { ll t,n,i,j; cin>>n; for(i=1;i<=n;i++) { scanf("%lld",&p[i]); } for(i=1;i<=200005;i++) { fa[i]=i; } ll ans=0; for(i=1;i<=n;i++) { j=n+1-i; if(gf(p[i])==gf(p[j])) continue; ans++; merge(p[i],p[j]); // for(ll ii=1;ii<=n;ii++) cout<<fa[ii]<<" ";cout<<endl; } cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template<typename T> void view_1d(vector<T> V, string sep) { for (ll i=0; i<V.size(); i++) { cout << V[i]; if (i == V.size() - 1) { cout << endl; } else { cout << sep; } } } template<typename T> void view_2d(vector<vector<T>> V, string sep) { for (ll i=0; i<V.size(); i++) { for (ll j=0; j<V[i].size(); j++) { cout << V[i][j]; if (j == V[i].size() - 1) { cout << endl; } else { cout << sep; } } } } //==============================// vector<vector<int>> graph; vector<int> seen; void dfs(int v, int tmp) { seen[v] = tmp; for (auto next_v : graph[v]) { if (seen[next_v] != -1) continue; dfs(next_v, tmp); } } int main() { int N; cin >> N; vector<int> A(N); for (int i=0; i<N; i++) cin >> A[i]; int maxA = *max_element(A.begin(), A.end()); graph.resize(maxA); for (int i=0; i<N/2; i++) { if (A[i] != A[N-1-i]) { graph[A[i]-1].push_back(A[N-1-i]-1); graph[A[N-1-i]-1].push_back(A[i]-1); } } seen.assign(maxA, -1); int cnt = 0; for (int v=0; v<maxA; v++){ if (seen[v] != -1 || graph[v].empty()) continue; dfs(v, cnt); cnt++; } int ans = 0; if (cnt == 0) { ans = 0; } else { unordered_map<int,int> mp; for (int j=0; j<maxA; j++) { if (seen[j] != -1) mp[seen[j]]++; } for (auto x : mp) { ans += x.second - 1; } } cout << ans << endl; }
#include<bits/stdc++.h> #define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" #define int long long int using namespace std; int mod = 1e9 + 7; int32_t main() { int t, n; t = 1; while(t--) { string a, b; cin >> a >> b; int suma = 0, sumb = 0; for(int i = 0; i < a.length(); i++) suma += (a[i] - '0'); for(int i = 0; i < b.length(); i++) sumb += (b[i] - '0'); cout << max(suma, sumb) << endl; } return 0; }
// MUJHSE BILKUL BHI NAHI HO RHA.....still i am TRYING #include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define pb push_back #define f(i,n) for(i=0;i<n;i++) #define F(i,a,b) for(i=a;a<=b;i++) #define arr(a,n) for( i=0;i<n;i++)cin>>a[i]; #define fi first #define se second #define mp make_pair #define mod 1000000007 #define YES cout<<"YES"<<endl; #define Yes cout<<"Yes"<<endl; #define NO cout<<"NO"<<endl; #define No cout<<"No"<<endl; #define yes cout<<"yes"<<endl; #define no cout<<"no"<<endl; #define vi vector<ll> #define ed end() #define bg begin() #define sz size() #define ln length() #define s() sort(a,a+n); #define sr() sort(a,a+n,greater<ll>()); #define v() sort(v.begin(),v.end()); #define vr() sort(v.begin(),v.end(),greater<ll>()); #define mod 1000000007 #define fast() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);} ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;} int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif fast(); //ll t;cin>>t;while(t--) { ll a,b,p=0,q=0; cin>>a>>b; while(a>0) { int d=a%10; p+=d; a/=10; } while(b>0) { int d=b%10; q+=d; b/=10; } cout<<max(p,q)<<endl; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(3) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") #define int long long #define ri register #define mk make_pair #define fi first #define se second #define pb push_back #define eb emplace_back #define is insert #define es erase #define E (n+m) using namespace std; inline int read() { int s=0, w=1; ri char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w; } signed main() { int n=read(); for(ri int i=0;i<n;i++) { printf("%lld %lld\n",(i*2+1)%n+1,(i*2+2)%n+1); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define debug(val) cerr << "The value of " << #val << " is = " << val << '\n'; typedef long double ld; typedef long long ll; typedef unsigned long long ull; const ld PI = 4*atan((ld)1); const ll mod = 1e9 + 7; const ll inf = 922337203685477; const ll nax = 0; string s; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> s; for(ll i = 0; i < s.size(); i++){ if(s[i] == '.') break; cout << s[i]; } cout << '\n'; }
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 5005, inf = 1e9; #define fi first #define se second int mod = 1e9 + 7; int main(){ ios::sync_with_stdio(0), cin.tie(0); int n; 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()); ll ans = 1; for (int i = n; i > 0; i--){ ans *= a[i] - a[i - 1] + 1; ans %= mod; } cout << ans; }
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define f(i,j,n) for(long long i = j ; i < n ; ++i) #define fr(i,j,n) for(long long i = j ; i >= n ; --i) #define ones(x) __builtin_popcount(x) #define endl '\n' #define sz(v) v.size() using namespace std; //using namespace __gnu_pbds; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vll; typedef deque<int> di; typedef deque<long long> dll; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vpll; /*typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;*/ const ll N = 2e5 + 100; const ll M = 1e5 + 100; const ll inf = 9e18; const ll mod = 1e9 + 7; ll n,a[N]; ll mul(ll x,ll y){ return (x*y)%mod; } void solve(){ cin >> n; a[0] = 0; f(i,1,n+1) cin >> a[i]; sort(a+1,a+n+1); ll ans = 1; f(i,0,n) ans = mul(ans,a[i+1]-a[i]+1); cout << ans << endl; } int main(){ fastio; ll tc = 1; // cin >> tc; while(tc--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1) #define rrange(i, l, r) for (int i = (int)(r)-1; i >= (int)(l); (i) -= 1) template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { a = (a > b ? a : b); } template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { a = (a < b ? a : b); } template <typename T1, typename T2> ostream &operator<<(ostream &os,const pair<T1,T2> &p) { os<<p.first<<' '<<p.second; return os;} template <typename T> ostream &operator<<(ostream &os,const vector<T> &v) { range(i,0,v.size()) {os<<(i?" ":"")<<v[i];} return os;} using ull = unsigned long long; using ll = long long; using Pll = pair<ll, ll>; using P = pair<int, int>; constexpr ll INF64 = INT64_MAX / 2; constexpr int INF32 = INT32_MAX / 2; constexpr int dy[] = {0,-1,1,0,-1,1,-1,1}; constexpr int dx[] = {-1,0,0,1,-1,-1,1,1}; constexpr int mod998244353 = 998244353; constexpr int mod1000000007 = (int)1e9 + 7; constexpr char newl = '\n'; int main() { string s; cin>>s; deque<char> t; bool flg=false; range(i,0,s.size()){ if(s[i]=='R'){ flg=!flg; continue; } if(flg){ if(!t.empty() && t.front()==s[i]){ t.pop_front(); continue; } t.emplace_front(s[i]); }else{ if(!t.empty() && t.back()==s[i]){ t.pop_back(); continue; } t.emplace_back(s[i]); } } if(flg) reverse(t.begin(),t.end()); for(char c:t){ cout<<c; } cout<<endl; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <cmath> #include <iomanip> #include <cstdio> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <cstdlib> #include <queue> #include <stack> #include <bitset> using namespace std; #define MOD 1000000007 #define PI 3.1415926535897932 #define INF 1e9 #define rep(i, n) for (int i = 0; i < n; i++) #define repe(i, j, n) for (int i = j; i < n; i++) #define repi(i, n) for (int i = 0; i <= n; i++) #define repie(i, j, n) for (int i = j; i <= n; i++) #define all(x) x.begin(), x.end() #define println() cout << endl #define P pair<int, int> #define fi first #define se second typedef long long ll; using Graph = vector<vector<ll>>; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll dp[2000][2000]; ll nCr(ll n, ll r) { if(n==r) return dp[n][r] = 1; if(r==0) return dp[n][r] = 1; if(r==1) return dp[n][r] = n%MOD; if(dp[n][r]) return dp[n][r]%MOD; return dp[n][r] = nCr(n-1,r)%MOD + nCr(n-1,r-1)%MOD; } ll H(ll n, ll r) { return nCr(n+r-1, r)%MOD; } int prime[10000000]; bool is_prime[100000000 + 1]; int sieve(int n) { int pcnt = 0; for(int i = 0; i <= n; i++) { is_prime[i] = true; } is_prime[0] = is_prime[1] = false; for(int i = 2; i <= n; i++) { if(is_prime[i]) { prime[pcnt++] = i; for(int j = 2*i; j <= n; j += i) { is_prime[j] = false; } } } return pcnt; } struct UnionFind { //自身が親であれば、その集合に属する頂点数に-1を掛けたもの //そうでなければ親のid vector<ll> r; UnionFind(ll N) { r = vector<ll>(N, -1); } ll root(ll x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } ll size(ll x) { return -r[root(x)]; } bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す ll rx = root(x); ll ry = root(y); return rx == ry; } }; void solve1() { string s; cin >> s; for(int i = 0; i < (int)s.size(); i++) { if(s[i] == '.' ) { cout << endl; return; } cout << s[i]; if(i == (int)s.size()-1) { cout << endl; return; } } } int main() { solve1(); }
//雪花飄飄北風嘯嘯 //天地一片蒼茫 #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; int arr[2005]; unordered_set<int> uni; unordered_map<int,int> m; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); cin>>n; rep(x,0,n) cin>>arr[x]; int mn=1e9+5; rep(x,0,n) mn=min(mn,arr[x]); rep(x,0,n){ if (uni.count(arr[x])) continue; uni.insert(arr[x]); int curr=1; while (curr*curr<arr[x]){ if (arr[x]%curr==0){ m[curr]=__gcd(m[curr],arr[x]); m[arr[x]/curr]=__gcd(m[arr[x]/curr],arr[x]); } curr++; } if (curr*curr==arr[x]){ m[curr]=__gcd(m[curr],arr[x]); } } int ans=0; for (auto &it:m){ if (it.fi==it.se && it.fi<=mn) ans++; } cout<<ans<<endl; }
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define pii pair<int,int> #define pli pair<ll,int> #define pil pair<int,ll> #define pll pair<ll,ll> #define ks std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define fr(i,a,b) for(int i=a;i<b;i++) #define pb(a) push_back(a) #define fi first #define se second using namespace std; const int N=2e5+10; const ll mod=1e9+7; const int inf=INT_MAX; inline ll rd() { ll s=0,f=0; char ch=getchar(); while(ch<'0'&&ch>'9') {if(ch=='-') f=1; ch=getchar();} while(ch>='0'&&ch<='9') {s=s*10+ch-'0'; ch=getchar();} return f?-s:s; } inline void out(int x) { if(x>9) out(x/10); putchar(x%10+'0'); } struct ttt { int n,t; int x[N],y[N]; set<pii> st; int dj(int i,int j) {return x[i]*x[j]+y[i]*y[j]; } int cj(int i,int j) {return x[i]*y[j]-x[j]*y[i]; } void init() //数据初始化 { } void input() //数据输入 { cin>>n; fr(i,0,n) cin>>x[i]>>y[i]; fr(i,1,n) x[i]-=x[0],y[i]-=y[0];t=dj(1,1); fr(i,1,n) st.insert({dj(1,i),cj(1,i)}); fr(i,0,n) cin>>x[i]>>y[i]; } void query() //答案求解 { if(n==1) {cout<<"Yes"<<endl;return ;} fr(i,0,n){ fr(j,0,n){ if(i==j) continue; int nx=x[i]-x[j],ny=y[i]-y[j]; if(nx*nx+ny*ny!=t) continue; int f=1; fr(k,0,n) if(k!=i) x[k]-=x[i],y[k]-=y[i]; x[i]=0,y[i]=0; fr(k,0,n) if(k!=i&&k!=j&&!st.count({dj(j,k),cj(j,k)})) {f=0;break; } if(f){ cout<<"Yes"<<endl; return ; } } } cout<<"No"<<endl; } void debug() //调试 { } } as; int main() { ks; //int T;cin>>T;while(T--) as.input(),as.query(); return 0; }
/// Solution No.: /// Approach: /// Complexity: space: O() /// time: O() /// Comment: /// Verdict: /// Report: #include <iostream> #include <cmath> #include <algorithm> #include <iomanip> #include <string> #include <sstream> #include <cstdio> #include <vector> #include <string> #include <map> #include <queue> #include <deque> #include <bitset> #include <string.h> #include <climits> #include <set> #include <cassert> #include <cfloat> #include <stack> #include <unordered_map> #include <functional> #include <chrono> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> /// Recommended not to use ///using namespace std; ///using namespace __gnu_pbds; #define p(x) std::cout<<#x<<" = "<<x<<"\n" #define mem(a,b) memset(a,b,sizeof(a)) #define mod1 1000000007 #define mod2 998244353 #define absolute(x) (((x)<0)?(-(x)):(x)) /// #define _DEBUG typedef long long int ll; typedef unsigned long long llu; typedef long double lld; template <class T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template<class T> using matrix = std::vector<std::vector<T>>; /// lld pi = 2 * acos(0.0); /// precision error lld pi = 2 * acosl(0.0); int test_case_number, test_case=1; /// Direction Array /// int dx[]={0, 0, 1, -1, -1, 1, -1, 1}; /// int dy[]={-1, 1, 0, 0, 1, 1, -1, -1}; /// typedef struct /// { /// /// } structure; ///place for problem specific functions void function_name() { int a, b, x, y; scanf("%d %d %d %d",&a, &b, &x, &y); int answer =0; if(a==b) { answer = x; } else if(b<a) { b++; answer= x+(a-b)*(std::min(2*x, y)); } else if(a<b) { answer = x+(b-a)*(std::min(2*x, y)); } printf("%d\n", answer); } void function_name(int v1, int v2, int v3) { } void function_name(std::string line) { } class initial_works { public: void with_test_case_number() { scanf("%d", &test_case_number); ///getchar(); while(test_case <= test_case_number) { function_name(); test_case++; } } void single_iteration() { function_name(); } void without_test_case_number() { int v1, v2, v3; while(scanf("%d %d %d", &v1, &v2, &v3)==3 && v1!=0) { function_name(v1, v2, v3); test_case++; } } void eoof() { char dummy_char_array[100]={0}; while(scanf("%[^\n]", &dummy_char_array)!=EOF) { ///it only takes the absence of one getchar to ruin your life. Don't understand remove the getchar below and then input the sample input getchar(); ///if not given getchar. the dummu_char_array will not take new input as there is newline after the first input is taken. It will continue with the previous value. /// So, the function will run with same argument. std::string line = dummy_char_array; function_name(line); test_case++; } } }; int main() { //std::ios_base::sync_with_stdio(false); #ifdef _DEBUG ///freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif initial_works a; a.single_iteration(); return 0; }
#include<bits/stdc++.h> #include <iomanip> using namespace std; #define pb push_back #define ll long long int #define pll pair<ll,ll> int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int a,b,x,y; cin>>a>>b>>x>>y; int ans = x+min(abs(b+1-a)*y,abs(b-a)*y); if(a>b){ ans = min(ans,2*x*(a-b)-x); }else if(b>a){ ans = min(ans,2*x*(b-a)+x); } cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); long long sum = 0; long long sum2 = 0; for (int i = 0; i < N; i++) { cin >> A.at(i); sum += (long long)(N - 1) * A.at(i) * A.at(i); sum2 += A.at(i); } for (int i = 0; i < N - 1; i++) { sum2 -= A.at(i); sum -= 2 * A.at(i) * sum2; } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define hell 1000000007 const int nax=1e5+20; int main(){ // int t; cin >> t; int t = 1; while(t--){ int n; cin >> n; map<ll, ll> cnt; ll ans = 0; for(int i = 0; i < n; i++) { ll x; cin >> x; cnt[x]++; } for(int i = -200; i <= 200; i++) { for(int j = -200; j <= 200; j++) { ans += cnt[i]*cnt[j]*(i-j)*(i-j); } } ans /= 2; cout << ans; cout << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) #define ll long long using namespace std; int main(){ int n; cin >> n; double ans = 0; rep(i, n){ if (i==n-1) break; double i_ans = n / double(n-i-1); ans += i_ans; } printf("%.10f\n", ans); }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } #define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0) int N; using Double = long double; void MAIN() { scanf("%d", &N); Double e = 0; for (int i=N-1; i>=1; i--) { e = e + (Double)(N) / i; } printf("%.20f\n", (double)e); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a=1; while(n--) a*=2; queue<pair<int,int>>q; for(int i=0;i<a;i++) { int x; cin>>x; q.push({x,i+1}); } while(q.size()>2) { int k=q.size(); for(int i=0;i<k;i+=2) { int a,b,in1,in2; a=q.front().first;in1=q.front().second; q.pop(); b=q.front().first;in2=q.front().second; q.pop(); if(a>b) { q.push({a,in1}); } else { q.push({b,in2}); } } } int a1,b,in1,in2; a1=q.front().first;in1=q.front().second; q.pop(); b=q.front().first;in2=q.front().second; q.pop(); if(a1>b) { cout<<in2; } else { cout<<in1; } return 0; }
#include <stdio.h> #include <algorithm> #include <vector> using namespace std; int main(){ int n; scanf("%d",&n); int size = 2<<(n-1); vector<int> a(size); for(int i=0;i<size;i++){ scanf("%d",&a[i]); } int index1, index2; int max1 = 0; int max2 = 0; for(int i=0;i<(size/2);i++){ if(max1<a[i]){ max1 = a[i]; index1 = i; } } for(int i=(size/2);i<size;i++){ if(max2<a[i]){ max2 = a[i]; index2 = i; } } if(max1==max(max1,max2)){ printf("%d",index2+1); } else{ printf("%d",index1+1); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; int main(){ ll N; cin >> N; ll M = N, cnt = 0; vector<ll> A(10,0); while(M > 0){ A[cnt] = M % 10; M /= 10; cnt++; } ll rev = 0, cnt2 = 0; while(true){ if(A[cnt2] == 0) cnt2++; else break; } rep(i,cnt){ rev += A[i]; rev *= 10; } rev /= 10; rev *= pow(10,cnt2); if(N == rev) cout << "Yes" <<endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string S;string s="";cin>>S; for(int i=S.size()-1;i>=0;i--){ if(S.at(i)=='6')s+="9"; else{ if(S.at(i)=='9')s+="6"; else {s+=S.at(i);}}} cout<<s; }
/* ///// ///// ////////////////// ///// ///// ////////////////// ///// ///// ///// ///// ///// ///// ////////// o ///// /////\\\\\ ///// ///// \\\\\ ///// ///// \\\\\ ////////////////// ///// \\\\\ ////////////////// date updated : 20-10-2020 */ #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") #pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define int long long int #define endl '\n' #define PI 3.14159265359 #define boostIO ios_base::sync_with_stdio(false), cin.tie(NULL) #define deb(i) cout << "#i : " << i << endl #define deb2(i, n) cout << "#i : " << i << " #n : " << n << endl #define rep(i,a,b) for(int i = a; i < b; i++) #define vi vector<int> #define iv(vec) for(auto &c : vec) cin >> c; #define sint set<int> #define in insert #define finds(e, s) find(e.begin(), e.end(), s) #define pb push_back #define pii pair<int, int> #define F first #define S second #define setbits(x) __builtin_popcount(x) #define setbitsll(x) __builtin_popcountll(x) #define all(x) x.begin(),x.end() #define sortf(c) sort(c.begin(), c.end()) #define sortr(c) sort(c.begin(), c.end(), greater<>()) #define reset(x,v) memset(x,v,sizeof(x)) #define sz(x) ((int)(x).size()) inline int min(int a, int b) { return (a < b) ? a : b;} inline int max(int a, int b) { return (a > b) ? a : b;} inline int gcd(int a, int b){if(a==0) return b;return gcd(b%a,a);} template<typename T, typename... Args> T sum(T a, Args... args) { return a + sum(args...); } long long power(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 }; const int MOD =998244353; const int hell=1000000007; const int N = 100005; const int INF = 1e18; /*________________________________________________________________________________*/ void ___solve() { int x, y; cin >> x >> y; if(abs(x-y) < 3) cout << "Yes" << endl; else cout << "No" << endl; } int32_t main(int32_t argc, char* argv[]) { boostIO; cin.exceptions(cin.failbit); //FILE *fin = freopen("in","r",stdin); //FILE *fout = freopen("out","w",stdout); int TT = 1; //cin >> TT; while(TT--) { ___solve(); } return 0; }
#include<bits/stdc++.h> #define x first #define y second #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define mem(x,val) memset(x,val,sizeof x) #define pii pair<int,int> #define pb emplace_back #define ar array #define int long long #define FOR(i, a, b) for(int i = a; i < b; i++) #define F0R(i, n) FOR(i, 0, n) #define FOR1(i, n) FOR(i, 1, n+1) #define wopen(x) freopen((x),"w",stdout) #define ropen(x) freopen((x),"r",stdin) using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; const int N = 4e5 + 5; int n, a, b, cnt, ist, ans; vector<int> g[N]; bitset<N> vis; void dfs(int u, int p = 0){ cnt++; for(int v : g[u]){ if(v == p) continue; if(vis[v]) { ist = 0; continue; } vis[v] = 1; dfs(v, u); } } signed main(void){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; if(min(n,m)+3 > max(n,m)){ cout<<"Yes"; } else{ cout<<"No"; } return 0; }
#include<bits/stdc++.h> #define LL long long #define pll pair<LL,LL> #define mp make_pair #define fi first #define se second using namespace std; int read(){ char ch=getchar(); int x=0,fl=1; for(;!isdigit(ch);ch=getchar()) if(ch=='-') fl=-1; for(;isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch-'0'); return x*fl; } const int N=200005; int n,m,q,maxn=1e8; LL a[N],b[N]; void upd(pll &a,pll b){ a.fi+=b.fi; a.se+=b.se; } struct seg{ int ls[N<<5],rs[N<<5],cnt; LL C[N<<5],S[N<<5]; pll qry(int rt,int l,int r,int ql,int qr){ if(!rt||ql>qr) return mp(0LL,0LL); if(ql<=l&&r<=qr) return mp(C[rt],S[rt]); int mid=(l+r)>>1; pll res=mp(0LL,0LL); if(ql<=mid) upd(res,qry(ls[rt],l,mid,ql,qr)); if(qr>mid) upd(res,qry(rs[rt],mid+1,r,ql,qr)); return res; } void mdf(int rt,int l,int r,int pv,LL sgn){ C[rt]+=sgn; S[rt]+=1LL*sgn*pv; if(l==r) return; int mid=(l+r)>>1; if(pv<=mid){ if(!ls[rt]) ls[rt]=++cnt; mdf(ls[rt],l,mid,pv,sgn); } else{ if(!rs[rt]) rs[rt]=++cnt; mdf(rs[rt],mid+1,r,pv,sgn); } } }t[2]; int main(){ n=read(); m=read(); q=read(); t[0].cnt=t[1].cnt=1; for(int i=1;i<=n;i++) t[0].mdf(1,0,maxn,a[i]=0,1); for(int i=1;i<=m;i++) t[1].mdf(1,0,maxn,b[i]=0,1); LL ans=0LL; while(q--){ int opt=read(),x=read(),y=read(); opt--; if(!opt){ t[0].mdf(1,0,maxn,a[x],-1); if(y<a[x]){ pll tmp=t[1].qry(1,0,maxn,y,a[x]); ans-=1LL*a[x]*tmp.fi; ans+=1LL*tmp.se; tmp=t[1].qry(1,0,maxn,0,y-1); ans-=1LL*tmp.fi*a[x]; ans+=1LL*tmp.fi*y; } else if(y>a[x]){ pll tmp=t[1].qry(1,0,maxn,a[x],y); ans-=1LL*tmp.se; ans+=1LL*y*tmp.fi; tmp=t[1].qry(1,0,maxn,0,a[x]-1); ans-=1LL*a[x]*tmp.fi; ans+=1LL*y*tmp.fi; } a[x]=y; t[0].mdf(1,0,maxn,y,1); } else{ t[1].mdf(1,0,maxn,b[x],-1); if(y<b[x]){ pll tmp=t[0].qry(1,0,maxn,y,b[x]); ans-=1LL*b[x]*tmp.fi; ans+=1LL*tmp.se; tmp=t[0].qry(1,0,maxn,0,y-1); ans-=1LL*tmp.fi*b[x]; ans+=1LL*tmp.fi*y; } else if(y>b[x]){ pll tmp=t[0].qry(1,0,maxn,b[x],y); ans-=1LL*tmp.se; ans+=1LL*y*tmp.fi; tmp=t[0].qry(1,0,maxn,0,b[x]-1); ans-=1LL*b[x]*tmp.fi; ans+=1LL*y*tmp.fi; } b[x]=y; t[1].mdf(1,0,maxn,y,1); } cout<<ans<<'\n'; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define chmin(x,y) x = min((x),(y)); #define chmax(x,y) x = max((x),(y)); using namespace std; using ll = long long ; using P = pair<int,int> ; using pll = pair<long long,long long>; const int INF = 1e9; const long long LINF = 1e18; const int MOD = 1000000007; //const int MOD = 998244353; const double PI = 3.14159265358979323846; template<int mod> struct ModInt{ long long x=0; constexpr ModInt(long long x=0):x((x%mod+mod)%mod){} constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;} constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;} constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;} constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;} constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;} constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;} constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;} constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();} constexpr bool operator==(const ModInt& r){ return x == r.x;} constexpr bool operator!=(const ModInt& r){ return x != r.x;} ModInt inv() const { long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1; while(s%t!=0){ long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty; s=t;sx=tx;sy=ty; t=u;tx=ux;ty=uy; } return ModInt(tx); } ModInt pow(long long n) const { ModInt a=1; ModInt b=*this; while(n>0){ if(n&1) a*=b; b*=b; n>>=1; } return a; } friend constexpr ostream& operator<<(ostream& os,const ModInt<mod>& a) {return os << a.x;} friend constexpr istream& operator>>(istream& is,ModInt<mod>& a) {return is >> a.x;} }; using mint = ModInt<MOD>; int main(){ int n; cin >> n; char aa,ab,ba,bb; cin >> aa >> ab >> ba >> bb; if(n == 2 || (ab == 'B' && bb == 'B')) cout << 1 << endl; else if(ab == 'A' && aa == 'A') cout << 1 << endl; else if((ab == 'A' && aa == 'B' && ba == 'B') || (ab == 'B' && bb == 'A' && ba == 'A')){ cout << mint(2).pow(n-3) << endl; }else{ vector<mint> fib(n,0); fib[1] = 1; for(int i=2;i<n;i++) fib[i] = fib[i-1] + fib[i-2]; cout << fib[n-1] << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long #define forn(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define sz(a) (int)a.size() #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define int long long ll pw(ll a,ll b){ll r=1;while(b>0){if(b&1)r=(r*a);a=(a*a);b/=2;}return r;} ll gcd(ll a, ll b) {if(!b)return a;return gcd(b,a % b);} ll lcm(ll a,ll b){return(a*b)/gcd(a,b);} ll fact(ll n, ll mod){ll ret=1;for(ll i=1;i<=n;i++)ret=(ret*i)%mod;return ret;} bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(ll i=5;i*i <= n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} void solve() { int k; cin >> k; vector<int> cnt(10,k); string a,b; cin >> a >> b; for(int i = 0; i <4;i++){ cnt[a[i] - '0']--; cnt[b[i] - '0']--; } int wins = 0; int games= 0 ; for(int A = 1;A<=9;A++) { for(int B = 1;B<=9;B++) { if(A==B)continue; cnt[A]--; cnt[B]--; if(cnt[A]<0||cnt[B]<0){ cnt[A]++; cnt[B]++; continue; } games += (cnt[A]+1) * (cnt[B] + 1); int score_a=0,score_b=0; vector<int> v1(10,0),v2(10,0); for(int i =0;i < 4;i++){ v1[a[i] -'0']++; v2[b[i]-'0']++; } v1[A]++; v2[B]++; for(int i = 1;i<=9;i++){ score_a+=i * pw(10,v1[i]); score_b+=i * pw(10,v2[i]); } if(score_a>score_b){ wins+=(cnt[A]+1)*(cnt[B]+1); } cnt[A]++; cnt[B]++; } } for(int A = 1;A<=9;A++) { if(cnt[A]<2)continue; games+=(cnt[A] - 1) * cnt[A]; int score_a=0,score_b=0; vector<int> v1(10,0),v2(10,0); for(int i =0;i < 4;i++){ v1[a[i] -'0']++; v2[b[i]-'0']++; } v1[A]++; int B = A; v2[B]++; for(int i = 1;i<=9;i++){ score_a+=i * pw(10,v1[i]); score_b+=i * pw(10,v2[i]); } if(score_a>score_b){ wins+=cnt[A] * (cnt[A] -1); } cnt[A]++; cnt[B]++; } cout << fixed << setprecision(16) << double(wins)/double(games); } int32_t main() { //fastio; int t = 1; //cin >> t; while(t--) { solve(); } }
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdio.h> #include<math.h> #include<set> #include<algorithm> #include<string.h> #include<map> #define LL long long #define INF 0x3f3f3f3f #define mod 100000007 using namespace std; int k,cnt1[100],cnt2[100]; string s, t; int main() { cin >> k >> s >> t; for (int i = 0; i < 4; ++i) { cnt1[s[i] - '0']++; cnt2[t[i] - '0']++; } double ans = 0; for (int i = 1; i <= 9; ++i) { if (cnt1[i] + cnt2[i] >= k) continue; int x1=0; cnt1[i]++; for (int k = 1; k <= 9; ++k) x1 += k * (int)pow(10, cnt1[k]); for (int j = 1; j <= 9; ++j) { if (cnt1[j] + cnt2[j] >= k)continue; cnt2[j]++; int x2 = 0; for (int k = 1; k <= 9; ++k) x2 += k * (int)pow(10, cnt2[k]); if (x1 > x2) ans += ((double)k - ((double)cnt1[i] + cnt2[i] - 1-(i==j))) * ((double)k - ((double)cnt1[j] + cnt2[j] - 1-(i==j)) - (i == j)); cnt2[j]--; } cnt1[i]--; } cout << ans / (9.0 * k - 8) / (9.0 * k - 9) << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int Max = 1 << 17; int n; ll dp[20][20][Max + 10]; ll s[20][20]; ll pos[20][3]; ll p2[20]; int pr[Max + 10]; void pre() { for (int i = 0; i < Max; ++i) { int n = i; while (n) { pr[i] += n % 2; n /= 2; } } p2[1] = 1; for (int i = 2; i < 19; ++i) { p2[i] = p2[i - 1] * 2; } } int main() { pre(); cin >> n; const int mx = 1 << (n - 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { cin >> pos[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { s[i][j] = abs(pos[i][0] - pos[j][0]); s[i][j] += abs(pos[i][1] - pos[j][1]); s[i][j] += max(0ll, pos[j][2] - pos[i][2]); } } for (int i = 0; i <= n; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= mx; ++k) dp[i][j][k] = 987654321123456789; for (int i = 1; i < n; ++i) dp[1][i][p2[i]] = s[0][i]; for (int _ = 2; _ < n; ++_) { for (int i = 1; i < n; ++i) { for (int j = 1; j < n; ++j) { if (i == j)continue; for (int k = 0; k < mx; ++k) { if (pr[k] != _ - 1)continue; const int& t = p2[i]; if (t & k)continue; dp[_][i][t | k] = min(dp[_][i][t | k], dp[_-1][j][k] + s[j][i]); } } } } ll mi = 987654321123456789; for (int i = 1; i < n; ++i)mi = min(mi, dp[n-1][i][mx - 1] + s[i][0]); cout << mi << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef struct Point { int x,y,z; }Point; int f(Point a ,Point b){ int v1 = abs(a.x - b.x); int v2 = abs(a.y - b.y); int v3 = max(0,b.z - a.z); return v1+v2+v3; } int main(){ int n; cin>>n; Point a[n]; int x,y,z; for(int i=0;i<n;i++) { cin>>x>>y>>z; a[i]={x,y,z}; } int dp[1<<n][n]; const int INF = 0x3f3f3f3f; for(int i=0;i<(1<<n);i++){ for(int j=0;j<n;j++){ dp[i][j]=INF; } } dp[1][0]=0; for(int i=2;i<(1<<n);i++){ for(int j=0;j<n;j++){ if(i&(1<<j)){ int tgt = i^(1<<j); for(int k=0;k<n;k++){ dp[i][j]=min(dp[i][j],dp[tgt][k]+f(a[k],a[j])); } } } } int ans = INF; for(int i=0;i<n;i++){ ans = min(ans,dp[(1<<n)-1][i]+f(a[i],a[0])); } cout<<ans<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N,M; string a; random_device rnd; cin >> N >> M; for(int i=0; i<M; i++) { cin >> a; } for(int y=0; y<N; y++) { for(int x=0; x<N; x++){ char a = rnd()%8 + 'A'; cout << a; } cout << endl; } };
#include <bits/stdc++.h> using namespace std; int n; mt19937 mt; const int TRIES = 500; const int SHUFFLES = 0; bool check(string s, vector<vector<char>>& a, int i, int j) { bool isOk = true; for (int k = 0; k < s.size() && isOk; ++k) { if (s[k] != a[i][(j + k) % n]) { isOk = false; } } if (isOk) { return true; } for (int k = 0; k < s.size() && isOk; ++k) { if (s[k] != a[(i + k) % n][j]) { isOk = false; } } return isOk; } int calc(vector<string>& v, vector<vector<char>>& a) { int cnt = 0; for (string& s : v) { bool found = false; for (int i = 0; i < n && !found; ++i) { for (int j = 0; j < n && !found; ++j) { if (check(s, a, i, j)) { found = true; } } } cnt += found; } return cnt; } pair<int, vector<vector<char>>> gen(vector<string>& v) { shuffle(v.begin(), v.end(), mt); vector<vector<char>> a(n, vector<char>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j] = char(mt() % 8 + int('A')); } } int i = 0, j = 0; for (auto s : v) { if (j + s.size() >= n) { j = 0; ++i; } if (i >= n) { break; } for (int k = 0; k < s.size(); ++k, ++j) { a[i][j] = s[k]; } } pair<int, vector<vector<char>>> res = {calc(v, a), a}; for (int i = 0; i < SHUFFLES; ++i) { auto curr = res; shuffle(curr.second.begin(), curr.second.end(), mt); curr.first = calc(v, curr.second); if (curr.first > res.first) { res = curr; } } return res; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); mt.seed(time(nullptr)); int m; cin >> n >> m; vector<string> v(m); for (auto& i : v) { cin >> i; } pair<int, vector<vector<char>>> res = gen(v); for (int i = 0; i < TRIES; ++i) { auto curr = gen(v); if (curr.first > res.first) { res = curr; } } for (auto& i : res.second) { for (auto& j : i) { cout << j; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #ifdef ENABLE_DEBUG #define dump(a) cerr<<#a<<"="<<a<<endl #define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl #else #define dump(a) #define dumparr(a,n) #endif #define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++) #define For(i, a) FOR(i, 0, a) #define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--) #define Rev(i, a) REV(i, 0, a) #define REP(a) For(i, a) #define SIGN(a) (a==0?0:(a>0?1:-1)) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair<ll, ll> pll; typedef pair<ll,pll> ppll; typedef vector<ll> vll; typedef long double ld; typedef pair<ld,ld> pdd; pll operator+(pll a,pll b){ return pll(a.first+b.first,a.second+b.second); } pll operator-(pll a,pll b){ return pll(a.first-b.first,a.second-b.second); } pll operator*(ll a,pll b){ return pll(b.first*a,b.second*a); } const ll INF=(1LL<<60); #if __cplusplus<201700L ll gcd(ll a, ll b) { a=abs(a); b=abs(b); if(a==0)return b; if(b==0)return a; if(a < b) return gcd(b, a); ll r; while ((r=a%b)) { a = b; b = r; } return b; } #endif template<class T> bool chmax(T& a,const T& b){ if(a<b){ a=b; return true; } return false; } template<class T> bool chmin(T& a,const T& b){ if(a>b){ a=b; return true; } return false; } template<class S,class T> std::ostream& operator<<(std::ostream& os,pair<S,T> a){ os << "(" << a.first << "," << a.second << ")"; return os; } template<class T> std::ostream& operator<<(std::ostream& os,vector<T> a){ os << "[ "; REP(a.size()){ os<< a[i] << " "; } os<< " ]"; return os; } template <size_t N> class GreaterThan { public: bool operator() (const std::bitset<N> &lhs, const std::bitset<N> &rhs) const { size_t i = N; while ( i > 0 ) { if ( lhs[i-1] == rhs[i-1] ) { i--; } else if ( lhs[i-1] > rhs[i-1] ) { return true; } else { return false; } } return false; } }; void solve(long long A, long long B){ vector<vector<ll>> m(B-A+1,vector<ll>(B-A+1)); For(i,B-A+1){ FOR(j,i+1,B-A+1){ m[i][j]=m[j][i]=gcd(i+A,j+A); } } vector<ll> allnum(B-A+1); bitset<73> start; For(i,B-A+1){ start[i]=true; allnum[i]=i+A; } map<bitset<73>,ll,GreaterThan<73>> q; q[start]=1; auto addmap = [](map<bitset<73>, ll,GreaterThan<73>> &q, const bitset<73> &bs, ll x) { if(q.count(bs)) { q[bs]+=x; } else { q[bs]=x; } }; for (auto [primes, cnt] : q) { if (primes.none()) { continue; } ll i = 0; for (i = 0; i < 73; i++) { if (primes[i]) break; } auto newprimes = primes; newprimes[i] = false; addmap(q, newprimes, cnt); For(j, B - A + 1) { if (m[i][j] != 1) { newprimes[j] = false; } } addmap(q, newprimes, cnt); } bitset<73> clean; cout<<q[clean]<<endl; } int main(){ cout<<setprecision(1000); long long A; scanf("%lld",&A); long long B; scanf("%lld",&B); solve(A, B); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ vector<unsigned char>s(19,'a'); for(int i=0;;i++){ cin>>s.at(i); if(s.at(i)=='a') break; } for(int i=18;;i--){ if(s.at(i)=='a') s.pop_back(); else break; } vector<int>v(s.size()); for(int i=0;i<s.size();i++) v.at(i)=(int)s.at(i)-48; vector<int>v3(3,0); for(int i=0;i<v.size();i++){ if(v.at(i)%3==0) v3.at(0)++; else if(v.at(i)%3==1) v3.at(1)++; else v3.at(2)++; } int a,b; a=v3.at(1)*1; b=v3.at(2)*2; if((a+b)%3==0) cout<<0<<endl; else{ if((a+b)%3==1&&v3.at(1)>0&&v.size()>1) cout<<1<<endl; else{ if((a+b)%3==2&&v3.at(2)>0&&v.size()>1) cout<<1<<endl; else{ if((a+b)%3==2&&v3.at(2)==0&&v3.at(1)>=2&&v.size()>2) cout<<2<<endl; else{ if((a+b)%3==1&&v3.at(1)==0){ for(int i=1;i<=v3.at(2);i++) if((a+b-i*2)%3==0&&v.size()>i){ cout<<i<<endl; return 0; } cout<<-1<<endl; } else cout<<-1<<endl; } } } } }
#pragma GCC optimize("Ofast") #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; using ll = int64_t; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vector<int>>; using pii = pair<int, int>; using vpii = vector<pair<int, int>>; constexpr char newl = '\n'; constexpr double eps = 1e-10; #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define F0R(i,b) FOR(i,0,b) #define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--) #define RF0(i,b) RFO(i,0,b) #define fi first #define se second #define show(x) cout << #x << " = " << x << '\n'; #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define sz(x) (int)(x).size() #define YesNo {cout<<"Yes";}else{cout<<"No";} #define YESNO {cout<<"YES";}else{cout<<"NO";} #define v(T) vector<T> #define vv(T) vector<vector<T>> template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) { return l.first < r.first; } template<class T> istream& operator>>(istream& i, v(T)& v) { F0R(j, sz(v)) i >> v[j]; return i; } template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) { return i >> p.first >> p.second; } template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) { return i >> get<0>(t) >> get<1>(t) >> get<2>(t); } template<class T> ostream& operator<<(ostream& o, const vector<T>& v) { F0R(i, v.size()) { o << v[i] << ' '; } o << newl; return o; } template<class T> ostream& operator<<(ostream& o, const set<T>& v) { for (auto e : v) { o << e << ' '; } o << newl; return o; } template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) { for (auto& p : m) { o << p.first << ": " << p.second << newl; } o << newl; return o; } #if 1 using ld = long double; // INSERT ABOVE HERE signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; v(string) ss(N); cin >> ss; v(bitset<100>) es(N); bitset<100> vs; auto dfs = [&](auto dfs, int r, int v)->void { vs[v] = true; es[v][r] = true; F0R(i, N) { if (!vs[i] && ss[v][i] == '1') { dfs(dfs, r, i); } } }; F0R(i, N) { vs.reset(); dfs(dfs, i, i); } ld r = 0; F0R(i, N) { int pc = 0; F0R(j, N) { if (es[i][j]) pc++; } //show(pc); r += (ld)N / pc; } cout << std::fixed << std::setprecision(15); cout << (r/N); } #endif
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxN = 4e2 + 10; ll n, a[maxN], ans; int read(); int main(){ n = read(); for(int i = 0; i < n; i++){ ll x = read() + 200; a[x]++; for(int j = 0; j <= 400; j++) ans += (x - j) * (x - j) * a[j]; } cout << ans; return 0; } int read(){ int s = 0, f = 1; char ch = getchar(); while(!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * f; }
#include <bits/stdc++.h> #include <iostream> #define ll long long #define rep(i, n) for (int i=0; i < n; i++) using namespace std; int main() { int n, k, num; cin >> n >> k; num = (n*k*(100*n + k + 101))/2; cout << num << endl; }
//{{ /* * Created at: 06/21/21 22:14:44 * * FB: https://facebook.com/tgbaodeeptry * From Viet Nam with Love :D * */ #include <bits/stdc++.h> using namespace std; #define fast_io() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define REP(i, a, b) for (int i = int(a); i < int(b); ++i) #define RED(i, a, b) for (int i = int(a); i > int(b); --i) #define FOR(i, a, b) for (int i = int(a); i <= int(b); ++i) #define FOD(i, a, b) for (int i = int(a); i >= int(b); --i) #define sz(x) ((int)x.size()) #define all(x) (x).begin(), (x).end() #define f first #define s second #define eb emplace_back #define pb push_back #define endl '\n' using ll = long long; using vi = vector<int>; using vvi = vector<vector<int>>; using pii = pair<int, int>; const int MOD = 1e9 + 7; const int MAXN = 1e5; const int INF = INT_MAX; template <class T> void re(T &x) { cin >> x; }; template <class T> void re(vector<T> &v, int n) { FOR(i, 0, n - 1) cin >> v[i]; }; template <class T> void re(vector<T> &v, int f, int t) { FOR(i, f, t) cin >> v[i]; }; template <class T> void re(T v[], int n) { FOR(i, 0, n - 1) cin >> v[i]; }; template <class T> void re(T *v, int f, int t) { FOR(i, f, t) cin >> v[i]; }; template <class H, class... T> void re(H &v, T &... args) { re(v); re(args...); }; void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <typename T> void dbg_out(vector<T> a) { for (auto c : a) cerr << ' ' << c; cerr << endl; }; template <typename T> void dbg_out(vector<T> a, int n) { FOR(i, 0, n - 1) cerr << ' ' << a[i]; cerr << endl; }; template <typename T> void dbg_out(vector<T> a, int u, int v) { FOR(i, u, v) cerr << ' ' << a[i]; cerr << endl; }; template <typename T> void dbg_out(T a[]) { for (auto c : a) cerr << ' ' << c; cerr << endl; }; template <typename T> void dbg_out(T a[], int n) { FOR(i, 0, n - 1) cerr << ' ' << a[i]; cerr << endl; }; template <typename T> void dbg_out(T a[], int u, int v) { FOR(i, u, v) cerr << ' ' << a[i]; cerr << endl; }; #if defined(_CRUN) || defined(_RUN) #define pd(...) \ cerr << "(L:" << __LINE__ << "): " \ << "[" << #__VA_ARGS__ << "] =", \ dbg_out(__VA_ARGS__) #else #define pd(...) #endif #if defined(_CRUN) #define c_input() freopen("input", "r", stdin) #define c_output() freopen("output", "r", stdin) #else #define c_input() #define c_output() #endif void ps(){}; template <typename Head, typename... Tail> void ps(Head H, Tail... T) { cout << H; ps(T...); }; template <typename T> void ps(vector<T> a, int n) { FOR(i, 0, n - 1) cout << a[i] << ' '; }; template <typename T> void ps(T a[], int n) { FOR(i, 0, n - 1) cout << a[i] << ' '; }; ll add_mod(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; } ll sub_mod(ll a, ll b) { return ((a % MOD) - (b % MOD) + MOD) % MOD; } ll mul_mod(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; } ll pow_mod(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = mul_mod(ans, a); a = mul_mod(a, a); b >>= 1; }; return ans; }; ll lcm(ll a, ll b) { return a * b / __gcd(a, b); } ll pow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; a *= a; b >>= 1; }; return ans; }; void solve(); void init(); bool multi_test = false; // ---- main below ----- /// int main() { fast_io(); init(); int T = 1; if (multi_test) re(T); FOR(i, 1, T) solve(); } //}} void init() { // multi_test = true; } void solve() { int n, k; re(n, k); // n floor, k room // each floor: i * k * 100 + k * (k + 1) / 2 ps((ll) (k * 100) * (n * (n + 1) / 2) + (k * (k + 1) / 2) * n); }
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<bitset> #include<set> #include<map> #include<stack> #include<queue> #include<deque> #include<list> #include<iomanip> #include<cmath> #include<cstring> #include<functional> #include<cstdio> #include<cstdlib> #include<numeric> //#include<atcoder/scc> using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 #define MOD 1000000007 //#define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 const double EPS = 1e-10; using ll = long long; using Pi = pair<int,int>; using Pl = pair<ll,ll>; int main(){ int n; cin >> n; vector<ll> a(n),b(n); rep(i,n) cin >> a[i] >> b[i]; vector<ll> c(n); rep(i,n) c[i] = a[i]*2+b[i]; ll sum = 0; rep(i,n) sum += a[i]; sort(c.rbegin(),c.rend()); ll now = 0; rep(i,n){ now += c[i]; if(now > sum){ cout << i+1 << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 2005; int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int n; cin >> n; ll cnt = 0; vector<ll>v(n); for(int i = 0; i < n; i++) { ll x, y; cin >> x >> y; cnt -= x; v[i] = 2 * x + y; } sort(v.begin(), v.end()); int ans = 0; while(cnt <= 0) { cnt += v.back(); v.pop_back(); ans++; } cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <queue> #include <unordered_set> using namespace std; long long ipow(long long x, long long y) { long long ans = 1; for (long long i = 0; i < y; i++) { ans *= x; } return ans; } long long int keta(long long int n) { long long int ans = 0; while (true) { if (n == 0)break; n /= 10; ans++; } return ans; } int main() { vector<int> ans(1001, 0); int n, m; cin >> n >> m; int temp; for (int i = 0; i < n; i++) { cin >> temp; ans[temp]++; } for (int i = 0; i < m; i++) { cin >> temp; ans[temp]++; } for (int i = 0; i < 1001; i++) { if (ans[i] == 1) { cout << i << " "; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (ll i = 0; i < N; i++) int main() { ll n,q; cin >> n >> q; vector<ll> a(n); rep(i,n) cin >> a[i]; pair<ll,ll> k[q]; rep(i,q){ cin >> k[i].first; k[i].second=i; } sort(k,k+q); vector<ll> ans(q); ll num=0; rep(i,q){ while(a[num]<=k[i].first+num && num<n) num++; ans[k[i].second]=k[i].first+num; } rep(i,q) cout << ans[i] << endl; }
///Bismillahir Rahmanir Raheem #include <bits/stdc++.h> #include <algorithm> #define ll long long #define ull unsigned long long #define pb push_back #define vi vector<int> #define vl vector<ll> #define PI 2 * acos(0.0) #define NFS \ ios::sync_with_stdio(false); \ cin.tie(0) #define f first #define s second #define all(x) (x).begin(), (x).end() #define fore(arr) for (auto &x : (arr)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define SORT(v) sort((v).begin(), (v).end()) #define FIND(v, x) ((v).find(x) != (v).end()) #define gcd(x, y) __gcd(x, y) #define lcm(x, y) x *(y / gcd(x, y)) #define MAX 2000010 #define bug cout << "*_*\n" using namespace std; int toint(char c) { return (c - 48); } void solve() { ll n; double x; cin >> n >> x; double sum = 0; int ans = -1, flag = 1; for (int i = 1; i <= n; i++) { double a, b; cin >> a >> b; sum += (a / 100.0) * b; if ((sum - x) > 0.000001 && flag) { flag = 0; ans = i; } } cout << ans << endl; } int main() { NFS; #ifndef ONLINE_JUDGE freopen("D:/code/C++/OJ/in.txt", "r", stdin); freopen("D:/code/C++/OJ/out.txt", "w", stdout); #endif int t = 1; //cin >> t; while (t--) { solve(); } }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } #define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0) int N; int M; int C[10011]; void MAIN() { scanf("%d%d", &N, &M); REP (i, N) { int x; scanf("%d", &x); C[x] |= 1; } REP (i, M) { int x; scanf("%d", &x); C[x] |= 2; } VI ans; REP (i, 10011) if (C[i] == 1 || C[i] == 2) ans.push_back(i); rprintf("%d", ans.begin(), ans.end()); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
//AnkitCode99 here.... //every ups and downs matter! #include<bits/stdc++.h> #define endl "\n" #define IOS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr) typedef long long int ll; #define rep(i,a,b) for(ll i=a;i<b;i++) #define pb push_back using namespace std; const ll sz=1e5+5; const ll szz=1e6+6; const ll mod=1e9+7; int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("op.txt","w",stdout); #endif clock_t startTime=clock(); ll a,b,c,d; cin>>a>>b>>c>>d; cout<<a*d-b*c; cerr << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl; }//Goodbye...
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using pii = pair<int, int>; int main() { int N, C; cin >> N >> C; map<int, ll> mp; rep(i, N) { int a, b, c; cin >> a >> b >> c; mp[a] += c; mp[b + 1] -= c; } ll ans = 0; int p = 0; ll s = 0; for (auto a : mp) { ans += min(s, (ll)C) * (a.first - p); s += a.second; p = a.first; } cout << ans << endl; return 0; }
#include <iostream> #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double db; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define pll pair<ll,ll> #define pb push_back #define eb emplace_back #define mp make_pair #define ub(v,val) upper_bound(v.begin(),v.end(),val) #define np(str) next_permutation(str.begin(),str.end()) #define lb(v,val) lower_bound(v.begin(),v.end(),val) #define sortv(vec) sort(vec.begin(),vec.end()) #define rev(p) reverse(p.begin(),p.end()); #define v vector #define pi 3.14159265358979323846264338327950288419716939937510 #define len length() #define repc(i,s,e) for(ll i=s;i<e;i++) #define fi first #define se second #define mset(a,val) memset(a,val,sizeof(a)); #define mt make_tuple #define repr(i,n) for(i=n-1;i>=0;i--) #define rep(i,n) for(i=0;i<n;i++) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define at(s,pos) *(s.find_by_order(pos)) #define set_ind(s,val) s.order_of_key(val) long long int M = 1e9 + 7 ; long long int inf = 9 * 1e18; //CLOCK ll begtime = clock(); #define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //CLOCK ENDED ll n, m; // modular exponentiation ll binpow(ll val, ll deg) { if (deg < 0) return 0; if (!deg) return 1 % M; if (deg & 1) return binpow(val, deg - 1) * val % M; ll res = binpow(val, deg >> 1); return (res * res) % M; } //binomial ll modinv(ll n) { return binpow(n, M - 2); } //GCD ll gcd (ll a, ll b) { if (b == 0) return a; else return gcd (b, a % b); } v<v<pll>> adj; v<ll> used, col; void dfs(ll s, ll p) { used[s] = 1; col[s] = p; for (auto u : adj[s]) { if (!used[u.fi]) { if (p == u.se) { dfs(u.fi, (p + 1) % n); } else { dfs(u.fi, u.se); } } } used[s] = 2; } int main() { // your code goes here IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll i, j, t, k, x, y, z, N; cin >> n >> m; adj.resize(n); used.assign(n, 0); rep(i, m) { cin >> x >> y >> z; x--; y--; adj[x].eb(y, z - 1); adj[y].eb(x, z - 1); } col.resize(n); dfs(0, 0); rep(i, n) { cout << col[i] + 1 << '\n'; } return 0; }
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef long double ld; typedef pair<lint, lint> plint; typedef pair<ld, ld> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;++i) #define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;--i) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' #define fi first #define se second struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; template<class T> auto add = [](T a, T b) -> T { return a + b; }; template<class T> auto f_max = [](T a, T b) -> T { return max(a, b); }; template<class T> auto f_min = [](T a, T b) -> T { return min(a, b); }; template<class T> using V = vector<T>; using Vl = V<lint>; using VVl = V<Vl>; template< typename T > ostream& operator<<(ostream& os, const vector< T >& v) { for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; } template< typename T >istream& operator>>(istream& is, vector< T >& v) { for (T& in : v) is >> in; return is; } 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; } lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); } lint ceil(lint a, lint b) { return (a + b - 1) / b; } lint digit(lint a) { return (lint)log10(a); } lint e_dist(plint a, plint b) { return abs(a.fi - b.fi) * abs(a.fi - b.fi) + abs(a.se - b.se) * abs(a.se - b.se); } lint m_dist(plint a, plint b) { return abs(a.fi - b.fi) + abs(a.se - b.se); } void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); } const lint MOD1000000007 = 1000000007, MOD998244353 = 998244353, INF = 1e18; lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 }; bool YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; return flag; } struct Edge { lint from, to; lint cost; Edge(lint u, lint v, lint c) { cost = c; from = u; to = v; } bool operator<(const Edge& e) const { return cost < e.cost; } }; struct WeightedEdge { lint to; lint cost; WeightedEdge(lint v, lint c = 1) { to = v; cost = c; } bool operator<(const WeightedEdge& e) const { return cost < e.cost; } }; using WeightedGraph = V<V<WeightedEdge>>; typedef pair<lint, plint> tlint; typedef pair<plint, tlint> qlint; typedef pair<string, lint> valstring; lint A, B, C, D; int main() { cin >> A >> B >> C >> D; yn(!(A * B <= D && D <= A * C)); }
#include <bits/stdc++.h> #define pb push_back #define pf push_front #define ff first #define ss second #define pi acos(-1.0) #define lb lower_bound #define ub upper_bound #define bs binary_search #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) #define endl "\n" //#define inf 1<<30 // isupper() -> upper case check // tolower() -> convert upper to lower case // idx = str.find("bear", pos)) != string::npos =>substring find // Tuple access -> get<0>(v[i]) // cmnt - ctrl [] // stoi("...") -> decimel // istringstream ss(str) while(ss >> n) using namespace std; using ll = long long; using pii = pair<int, int>; using tpl = tuple<int, int, int>; using pcc = pair<char, char>; const ll inf = 1e18+100; const ll mod = 1e9+7; int dr[] = {-1, 0, 1, 0};// up, right, down, left int dc[] = { 0, 1, 0, -1}; // King moves similar to 8 direction //int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; //int dc[] = { 0, 1, 1, 1, 0, -1,-1, -1}; // Knight moves int kr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; int kc[] = {-2, -2, -1, 1, 2, 2, -1, 1}; // diagonal moves int dgr[] = {-1, -1, 1, 1}; int dgc[] = { 1, -1, -1, 1}; int ans, k; vector<pii>v, ara; int cnt[111]; void fun(int idx) { if(idx == k) { int cur = 0; for(pii p: ara) { //printf("%d %d\n", p.ff, cnt[p.ff]); if(cnt[p.ff] >= 1 && cnt[p.ss] >= 1) cur++; } ans = max(ans, cur); return; } int x = v[idx].ff; ++cnt[x]; fun(idx+1); --cnt[x]; int y = v[idx].ss; ++cnt[y]; fun(idx+1); --cnt[y]; return; } void solve() { int n, m; cin >> n >> m; //vector<pii>ara; for(int i=0; i<m; i++) { int x, y; cin >> x >> y; ara.pb({x, y}); } //int k; cin >> k; //int cnt[n*n]; //memset(cnt, 0, sizeof cnt); for(int i=0; i<k; i++) { int x, y; cin >> x >> y; v.pb({x, y}); //if(!cnt[x]) cnt[x] = 1; //else cnt[y]++; } //int ans = 0; fun(0); cout << ans << endl; } int main() { fast_io; int tt = 1; //cin >> tt; while(tt--) { solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main(){ int n,m; cin >> n >> m; vector<int> a(m),b(m); rep(i,m) cin >> a[i] >> b[i]; int k; cin >> k; vector<int> c(k),d(k); rep(i,k) cin >> c[i] >> d[i]; int ans = 0; rep(is,1<<k){ int sum = 0; vector<int> sara(n+1); rep(i,k){ if(is>>i&1)sara[d[i]]++; else sara[c[i]]++; } rep(i,m){ if(sara[a[i]] == 0) continue; if(sara[b[i]] == 0) continue; sum++; } ans = max(ans,sum); } cout << ans << endl; return 0; } // cout << fixed << setprecision(15) << << endl;
#pragma region Macros #include <bits/stdc++.h> #if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG) #include <atcoder/all> #endif using namespace std; #define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define REPR(i, n) for(int i=(n); i>=0; --i) #define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i) #define EACH(i, v) for(const auto& i : v) #define ALL(x) (x).begin(),(x).end() #define ALLR(x) (x).rbegin(),(x).rend() template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T>using vec = vector<T>; template<class T, class U>using umap = unordered_map<T, U>; using ll = long long; using P = pair<ll, ll>; using vl = vec<ll>; #define fi first #define se second #define el endl constexpr ll INF = numeric_limits<ll>::max()/2-1; #pragma endregion #pragma region IOMacros template<class T> istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;} template<class T> ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;} #define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__); void __i() {} template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);} void O() {cout << el;} template<class T, class... Ts> void O(T&& o, Ts&&... args){cout << o << " ";O(forward<Ts>(args)...);} #pragma endregion void Main(); int main(){ std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(15); Main(); return 0; } struct edge{ll to, cost, k;}; typedef pair<ll,ll> P; struct graph{ ll V; vector<vector<edge> > G; vector<ll> d; graph(ll n){ init(n); } void init(ll n){ V = n; G.resize(V); d.resize(V); fill(ALL(d), INF); } void add_edge(ll s, ll t, ll cost, ll k){ edge e; e.to = t, e.cost = cost; e.k=k; G[s].push_back(e); } void dijkstra(ll s){ fill(ALL(d), INF); d[s] = 0; priority_queue<P,vector<P>, greater<P> > que; que.push(P(0,s)); while(!que.empty()){ P p = que.top(); que.pop(); ll v = p.second; if(d[v]<p.first) continue; for(auto e : G[v]){ ll t = ((d[v]+e.k-1)/e.k)*e.k; if(d[e.to]>t+e.cost){ d[e.to] = t+e.cost; que.push(P(d[e.to],e.to)); } } } } }; void Main(){ I(ll, N, M, X, Y); X--;Y--; graph g(N); REP(i, M){ I(ll, A, B, T, K); A--;B--; g.add_edge(A, B, T, K); g.add_edge(B, A, T, K); } g.dijkstra(X); if(g.d[Y] == INF){ cout << -1 << el;return; } cout << g.d[Y] << el; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll z = 0, mod = 1000000007, MAX = 10000000000; int main(){ ll N; cin >> N; vector<ll> data(1, 0); ll now = 0; for (ll i = 0; i < N; i++){ ll a; cin >> a; now = a - now; if (i % 2 == 1){ data.push_back(now); } else{ data.push_back(now * -1); } } sort(data.begin(), data.end()); ll ans = 0, combo = 0, prev = data[0]; for (ll i : data){ if (i == prev){ combo++; } else{ ans += (combo * (combo - 1)) / 2; combo = 1; prev = i; } //cout << i << ' ' << combo << ' ' << ans << endl; } ans += (combo * (combo - 1)) / 2; cout << ans << endl; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ordered_set tree<ll, null_type , less<ll> , rb_tree_tag , tree_order_statistics_node_update> #define ll long long #define ull unsigned long long #define pb push_back #define inf 1e18 #define mk make_pair #define ld long double #define mod 1000000007 #define fi first #define se second #define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define test ll t; cin>>t; while(t--) #define setbits __builtin_popcount #define endl '\n' #define LOCAL #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i));} sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " int main() { fastIO; ll n; cin>>n; pair<ll,ll>p[n]; for(int i=0;i<n;i++) { cin>>p[i].fi>>p[i].se; } ll val,val1,ans=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { val=p[j].se-p[i].se; val1=p[j].fi-p[i].fi; ll val2=__gcd(val,val1); val/=val2; val1/=val2; if(abs(val)<abs(val1)) { ans++; } if(val1==1 && abs(val)==1) { ans++; } } } cout<<ans<<endl; }
#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>; struct UnionFind { // それ自身の場合はsize の-1 掛けたもの // それ以外は自分の親のid vector<int> r; UnionFind(int N) { r = vector<int>(N, -1); } int root(int x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(int x, int y){ x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x,y); r[x] += r[y]; r[y] = x; return true; } int size(int x){ return -r[root(x)]; } }; const int ncol= 400000; int main() { int n; cin >> n; vector<int> a(n),b(n); UnionFind UF(ncol); map<int,int> deg;// 頂点i に接続する 辺の数 set<int> v;// 頂点 rep(i,n){ cin >> a[i] >> b[i]; UF.unite(a[i],b[i]); deg[a[i]]++; deg[b[i]]++; v.insert(a[i]); v.insert(b[i]); } // 頂点数、辺の数 map<int,int> nhen;// 辺の数 for (auto it : v) { int g=UF.root(it); nhen[g] += deg[it]; } // for (auto it : v) { // int g=UF.root(it); // cout << "id: " << g << ", n edge: " << nhen[g] << endl; // } vector<bool> touch(ncol,false); int ans=0; for (auto it : v) { int g=UF.root(it); int nv = UF.size(it); int nedge = nhen[g]/2; // cout << "id: " << g << ", n vert: " << nv << ", n edge: " << nedge << endl; if (touch[g]==false) { if (nedge >= nv ) { ans += nv; } else { ans += nv-1; } } touch[g] = true; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> #include <string> #include <sstream> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; using P = pair<ll,ll>; const int INF=1001001001; const int mod=998244353; int main() { int N,M; cin>>N>>M; vector<int>w(N); int wmx=0; rep(i,N){cin>>w[i];chmax(wmx,w[i]);} vector<int>l(M),v(M); int vmn=INF; vector<P>b(M); rep(i,M){cin>>l[i]>>v[i];b[i]={v[i],l[i]};chmin(vmn,v[i]);} if(wmx>vmn){cout<<-1<<endl;return 0;} sort(b.begin(),b.end()); rep(i,M){ if(i>0){chmax(b[i].second,b[i-1].second);} } vector<int>num(N); iota(num.begin(),num.end(),0); ll ans=1e18; do{ vector<ll>dp(N); rep(i,N){ ll mx=0; for(int j=i;j<N;j++){ mx+=w[num[j]]; int r=lower_bound(b.begin(),b.end(),P(mx,0))-b.begin(); if(r-1>=0){chmax(dp[j],dp[i]+b[r-1].second);} } } chmin(ans,dp[N-1]); }while(next_permutation(num.begin(),num.end())); cout<<ans<<endl; return 0; }
#include "iostream" #include "climits" #include "list" #include "queue" #include "stack" #include "set" #include "functional" #include "algorithm" #include "string" #include "map" #include "unordered_map" #include "unordered_set" #include "iomanip" #include "cmath" #include "random" #include "bitset" #include "cstdio" #include "numeric" #include "cassert" #include "ctime" using namespace std; constexpr long long int MOD = 1000000007; //constexpr int MOD = 1000000007; //constexpr int MOD = 998244353; //constexpr long long int MOD = 998244353; constexpr double EPS = 1e-12; //int N, M, K, T, H, W, L, R; long long int N, M, K, T, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; vector<int>weight(N); for (auto &i : weight)cin >> i; vector<int>l(M); vector<int>d(M); for (int i = 0; i < M; i++) { cin >> l[i] >> d[i]; } if (*max_element(weight.begin(), weight.end()) > *min_element(d.begin(), d.end())) { cout << -1 << endl; return 0; } vector<int>sum(1 << N); for (int i = 0; i < 1 << N; i++) { for (int j = 0; j < N; j++) { if ((i >> j) & 1) { sum[i] += weight[j]; // cout << i << " " << sum[i] << endl; } } } map<int, int>mp; for (int i = 0; i < M; i++) { mp[d[i]] = 0; } for (auto i : sum)mp[i] = 0; int cnt = 0; for (auto &i : mp) { i.second = cnt++; } vector<int>mn(cnt); for (int i = 0; i < M; i++) { mn[mp[d[i]]] = max(l[i], mn[mp[d[i]]]); } for (int i = 1; i < cnt; i++) { mn[i] = max(mn[i], mn[i - 1]); } int ans = MOD; vector<int>p(N); for (int i = 0; i < N; i++) { p[i] = i; } do { vector<int>place(N); for (int i = 1; i < N; i++) { for (int j = 0; j < i; j++) { int bit = 0; for (int k = j; k <= i; k++) { bit |= 1 << p[k]; } // cout << i << " " << j << " " << bit << endl; place[i] = max(place[i], place[j] + mn[mp[sum[bit]] - 1]); } } // cout << place.back() << endl; ans = min(ans, place.back()); } while (next_permutation(p.begin(), p.end())); cout << ans << endl; }
#ifdef _DEBUG #define _GLIBCXX_DEBUG #endif #if __has_include(<atcoder/all>) #include <atcoder/all> #endif #include <bits/stdc++.h> using namespace std; #define int long long using ll = long long; using ld = long double; using P = pair<int, int>; #define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++) #define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i--) const int MOD = 1000000007; const int MOD2 = 998244353; const ll INF = 1e18; const ld PI = acos(-1); signed main() { int N,K; cin >> N >> K; vector<vector<int>> A(N,vector<int>(N)); rep(i,0,N-1) rep(j,0,N-1) cin >> A[i][j]; int ok=INF,ng=-1; while(abs(ok-ng)>1){ int mid=(ok+ng)/2; vector<vector<int>> newA(N+K,vector<int> (N+K,0)); rep(i,0,N-1){ rep(j,0,N-1){ if(A[i][j]<=mid){ newA[i][j]++; newA[i][j+K]--; newA[i+K][j]--; newA[i+K][j+K]++; } } } rep(i,0,N+K-1){ rep(j,0,N+K-1){ if(i&&j) newA[i][j]+=newA[i][j-1]+newA[i-1][j]-newA[i-1][j-1]; else if(i) newA[i][j]+=newA[i-1][j]; else if(j) newA[i][j]+=newA[i][j-1]; } } bool isok=false; int base=(K*K+1)/2; rep(i,0,N-1){ rep(j,0,N-1){ //cout << newA[i][j] << endl; if(newA[i][j]>=base){ isok=true; } } } if(isok){ ok=mid; } else{ ng=mid; } } cout << ok << endl; }
#include <cstdio> #include <iostream> #include <map> #include <queue> #include <vector> #include <stack> #include <algorithm> #include <cmath> typedef long long ll; using namespace std; ll Atoi(string s, ll radix, ll m) { ll ans = 0; for (ll i = 0; i < s.size(); i++) { char t = s[i]; if (ans > m/radix) return 0; ans = ans * radix + t - '0'; } return ans <= m; } int main() { string x; ll m; cin >> x >> m; if (x.size() == 1) { cout << Atoi(x, 10, m) << endl; return 0; } ll d = *max_element(x.begin(), x.end()) - '0' + 1; ll l = d, r = m; ll ans = 0; for (; l<=r; ) { ll mid = (l + r) / 2; if (Atoi(x, mid,m)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } //cout << ans << " " << d << endl; if (d > ans) { cout << 0 << endl; } else { cout << ans - d + 1 << endl; } }
#ifdef LOCAL //#define _GLIBCXX_DEBUG #endif //#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, int> Pi; typedef vector<ll> Vec; typedef vector<int> Vi; typedef vector<string> Vs; typedef vector<char> Vc; typedef vector<P> VP; typedef vector<vector<ll>> VV; typedef vector<vector<int>> VVi; typedef vector<vector<char>> VVc; typedef vector<vector<vector<ll>>> VVV; typedef vector<vector<vector<vector<ll>>>> VVVV; #define endl '\n' #define REP(i, a, b) for(ll i=(a); i<(b); i++) #define PER(i, a, b) for(ll i=(a); i>=(b); i--) #define rep(i, n) REP(i, 0, n) #define per(i, n) PER(i, n, 0) const ll INF=1e18+18; const ll MOD=1000000007; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define ALL(v) v.begin(), v.end() #define rALL(v) v.rbegin(), v.rend() #define pb(x) push_back(x) #define mp(a, b) make_pair(a,b) #define Each(a,b) for(auto &a :b) #define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i) #ifdef LOCAL #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;} #define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl; #define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;} #define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl; #else #define dbg(...) #define dbgmap(...) #define dbgset(...) #define dbgarr(...) #define dbgdp(...) #endif #define out(a) cout<<a<<endl #define out2(a,b) cout<<a<<" "<<b<<endl #define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl #define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define fi first #define se second template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } template<typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; } template<typename T>istream& operator>>(istream&i,vector<T>&v) {rep(j,v.size())i>>v[j];return i;} // vector template<typename T> ostream &operator<<(ostream &s, const vector<T> &v) { int len=v.size(); for(int i=0; i<len; ++i) { s<<v[i]; if(i<len-1) s<<" "; } return s; } // 2 dimentional vector template<typename T> ostream &operator<<(ostream &s, const vector<vector<T> > &vv) { s<<endl; int len=vv.size(); for(int i=0; i<len; ++i) { s<<vv[i]<<endl; } return s; } vector<ll> divisor(ll n) { vector<ll> ret; for(ll i=1; i*i<=n; i++) { if(n%i==0) { ret.pb(i); if(i*i!=n) { ret.pb(n/i); } } } sort(ALL(ret)); return ret; } vector<pair<ll,ll>> factorize(ll n) { vector<pair<ll,ll>> res; for (ll i = 2; i*i <= n; ++i) { if (n%i) continue; res.emplace_back(i,0); while (n%i == 0) { n /= i; res.back().second++; } } if (n != 1) res.emplace_back(n,1); return res; } int solve(){ ll n; cin>>n; Vec div = divisor(n*2); ll ans = 0; dbg(div.size()); Each(x, div){ ll tmp = 2*n/x + 1 -x; if(tmp%2==0){ ans++; } } out(ans); return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout<<std::setprecision(10); // ll T; // cin>>T; // while(T--) solve(); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; scanf("%lld",&n); ll ans=0; for(int i=1;n%2==0;i++) { n=n/2; } if(n==1) { printf("2\n"); return 0; } for(int i=1; i<sqrt(n); i=i+2) { if(n%i==0&&(n/i)%2!=0) { ans=ans+2; } else if(n%i==0&&(n/i)%2==0) { ans=ans+1; } else { ; } } float a=sqrt(n); int b=(int)a; if(b==a&&b%2!=0) ans++; printf("%lld\n",ans*2); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int,int>; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; ll ans = 0; for(int i = 2; i <= 2*n; i++){ if(i + k > 2*n) continue; if(i + k < 2) continue; ll tmp; if(i <= n+1) tmp = i-1; else tmp = 2*n-i+1; if(i + k <= n+1) tmp *= k + i - 1; else tmp *= 2*n - k - i + 1; ans += tmp; } cout << ans << endl; }
#include <iostream> #include <algorithm> #include<cmath> #include<cstring> #include<cstdio> #include<cstdlib> #include<vector> #include<iomanip> #include<ctime> #include<set> #include<map> #include<queue> #include<stack> #include<bitset> #define sqr(x) ((x)*(x)) #define fz1(i,n) for ((i)=1;(i)<=(n);(i)++) #define fd1(i,n) for ((i)=(n);(i)>=1;(i)--) #define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++) #define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--) #define fz0k(i,n) for ((i)=0;(i)<(n);(i)++) #define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--) #define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++) #define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--) #define fzin fz1(i,n) #define fzim fz1(i,m) #define fzjn fz1(j,n) #define fzjm fz1(j,m) #define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr) #define pb push_back #define mk make_pair #define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;} #define inc(x,y) {x+=(y);if(x>=mod)x-=mod;} #define dec(x,y) {x-=(y);if(x<0)x+=mod;} #define spln(i,n) (i==n?'\n':' ') #define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}} using namespace std; inline void read(int &x) { char c;int f=1; while(!isdigit(c=getchar()))if(c=='-')f=-1; x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15); x*=f; } long long n,m,i,ans; long long f(long long x) { if(x<2||x>n+n) return 0; return min(x-2+1,n+n-x+1); } int main() { cin>>n>>m; fz(i,2,n+n){ ans+=f(i+m)*f(i); } cout<<ans<<endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include<bits/stdc++.h> using namespace std; template<class T> struct cLtraits_identity{ using type = T; } ; template<class T> using cLtraits_try_make_signed = typename conditional< is_integral<T>::value, make_signed<T>, cLtraits_identity<T> >::type; template <class S, class T> struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed<S>::type; using tT = typename cLtraits_try_make_signed<T>::type; using type = typename common_type<tS,tT>::type; } ; template<class S, class T> inline auto max_L(S a, T b) -> typename cLtraits_common_type<S,T>::type{ return (typename cLtraits_common_type<S,T>::type) a >= (typename cLtraits_common_type<S,T>::type) b ? a : b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template<class T> int Moebius_L(T n){ T i; int res = 1; if(n%4==0){ return 0; } if(n%2==0){ n /= 2; res = -res; } for(i=3;i*i<=n;i+=2){ if(n%i==0){ n /= i; res = -res; } if(n%i==0){ return 0; } } if(n > 1){ res = -res; } return res; } long long L; long long R; long long res; long long solve(){ long long i; long long res = 0; res += R - L; for(i=(2);i<(R+1);i++){ res -=Moebius_L(i)* (R/i - L/i) * (R/i - L/i); } for(i=(max_L(2, L+1));i<(R+1);i++){ res -= (R / i) * 2; } if(L == 0){ res--; } return res; } int main(){ rd(L);L += (-1); rd(R); res = solve(); wt_L(res); wt_L('\n'); return 0; } // cLay version 20210619-1 [beta] // --- original code --- // ll L, R, res; // // ll solve(){ // ll i, res = 0; // res += R - L; // rep(i,2,R+1) res -= Moebius(i) * (R/i - L/i) * (R/i - L/i); // rep(i,max(2,L+1),R+1) res -= (R / i) * 2; // if(L == 0) res--; // return res; // } // // { // rd(L--,R); // res = solve(); // wt(res); // }
#include <bits/stdc++.h> using namespace std; struct binary_indexed_tree{ int N; vector<int> BIT; binary_indexed_tree(int N): N(N), BIT(N + 1, 0){ } void add(int i, int x){ i++; while (i <= N){ BIT[i] += x; i += i & -i; } } int sum(int i){ int ans = 0; while (i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } int sum(int L, int R){ return sum(R) - sum(L); } }; int main(){ int N; cin >> N; vector<int> l(N), r(N); for (int i = 0; i < N; i++){ int t; cin >> t >> l[i] >> r[i]; l[i] *= 2; r[i] *= 2; if (t == 3 || t == 4){ l[i]++; } if (t == 1 || t == 3){ r[i]++; } } vector<int> x; for (int i = 0; i < N; i++){ x.push_back(l[i]); x.push_back(r[i]); } sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); int M = x.size(); for (int i = 0; i < N; i++){ l[i] = lower_bound(x.begin(), x.end(), l[i]) - x.begin(); r[i] = lower_bound(x.begin(), x.end(), r[i]) - x.begin(); } binary_indexed_tree L(M), R(M); long long ans = 0; for (int i = 0; i < N; i++){ ans += i; ans -= R.sum(l[i] + 1); ans -= L.sum(r[i], M); L.add(l[i], 1); R.add(r[i], 1); } cout << ans << endl; }
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <cassert> #include <cmath> #include <string> #include <queue> #include <set> #include <map> #include <cstdlib> using namespace std; #define INF 1e+9 #define mp make_pair #define pb push_back #define fi first #define fs first #define se second #define i64 long long #define li long long #define lint long long #define pii pair<int, int> #define vi vector<int> #define forn(i, n) for (int i = 0; i < (int)n; i++) #define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++) const int MAXN = 2e5+5; int d[MAXN]; int t_in[MAXN]; int t_out[MAXN]; int timer = 0; vi edges[MAXN]; void dfs(int v, int depth) { d[v] = depth; t_in[v] = timer++; for (int u : edges[v]) { dfs(u, depth + 1); } t_out[v] = timer++; } int main() { int n; scanf("%d", &n); fore(i, 2, n) { int x; scanf("%d", &x); edges[x].pb(i); } dfs(1, 0); vector<vi> lst(n + 1); fore(i, 1, n) { lst[d[i]].pb(t_in[i]); } forn(i, n) { sort(lst[i].begin(), lst[i].end()); } int queries; scanf("%d", &queries); forn(q, queries) { int u, d; scanf("%d%d", &u, &d); if (lst[d].empty()) { printf("0\n"); continue; } auto beg = lower_bound(lst[d].begin(), lst[d].end(), t_in[u]); auto end = upper_bound(lst[d].begin(), lst[d].end(), t_out[u]); printf("%d\n", end - beg); } }
//================code===================// //#define TLE #ifdef TLE #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #include <random> #include <ctime> #define ci(t) cin>>t #define co(t) cout<<t #define LL long long #define ld double #define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i) #define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i) #define setp tuple<LL,LL,LL> #define setl pair<int,int> #define micro 0.000001 using namespace std; LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; } #ifdef OHSOLUTION #define ce(t) cerr<<t #define AT cerr << "\n=================ANS=================\n" #define AE cerr << "\n=====================================\n" #define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl; #define __builtin_popcount __popcnt #define __builtin_popcountll __popcnt64 #else #define AT #define AE #define ce(t) #endif pair <int, int> vu[9] = { {0,1},{1,0},{0,1} ,{-1,0},{1,1},{1,-1}, {-1,1} , {-1,-1},{-1,-1} }; //RDLU EWSN template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; } template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; } struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } }; struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } }; const int max_v = 2e5 + 7; const int max_k = 5e2 + 7; const int bsz = (1ll << 10) + 7; const int INF = 1e9 + 7; const LL LNF = (LL)5e18 + 7ll; LL mod = 1e9 + 7; template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; } vector<int> adj[max_v]; vector<int> edge[max_v]; int t=-1; setl in[max_v]; void dfs(int u,int d=0) { in[u].first = ++t; edge[d].push_back(in[u].first); for (auto& v : adj[u]) dfs(v,d+1); in[u].second = t; } int main() { #ifdef OHSOLUTION freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int v; ci(v); fa(i, 1, v) { int x; ci(x); --x; adj[x].push_back(i); } dfs(0); fa(i, 0, v) sort(edge[i].begin(), edge[i].end()); int q; ci(q); while (q--) { int x, y; ci(x >> y); --x; int ret = upper_bound(edge[y].begin(), edge[y].end(), in[x].second) - lower_bound(edge[y].begin(), edge[y].end(), in[x].first); co(ret << "\n"); } return 0; }
#include "bits/stdc++.h" #pragma region header using i32 = int; using i64 = long long int; using u32 = unsigned int; using u64 = unsigned long long int; using isize = ptrdiff_t; using usize = size_t; constexpr size_t operator"" _uz(unsigned long long v) { return size_t(v); } inline u64 popcount(const u64 v) { #ifdef _MSC_VER return u64(__popcnt64(v)); #else return u64(__builtin_popcount(v)); #endif } class range { struct range_iterator { usize itr; constexpr range_iterator(const usize pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { ++itr; } constexpr bool operator!=(const range_iterator& other) const noexcept { return itr != other.itr; } constexpr usize operator*() const noexcept { return itr; } }; const range_iterator first, last; public: constexpr range(const usize first_, const usize last_) noexcept : first(first_), last(last_) {} constexpr range_iterator begin() const noexcept { return first; } constexpr range_iterator end() const noexcept { return last; } }; template <class F> class rec_lambda { F f; public: constexpr rec_lambda(F&& f_) : f(std::forward<F>(f_)) {} template <class... Args> constexpr auto operator()(Args&&... args) const { return f(*this, std::forward<Args>(args)...); } }; namespace cs_helper { void zip_sort_renumber([[maybe_unused]] std::vector<usize>& order) {} template <class T, class... Args> void zip_sort_renumber(std::vector<usize>& order, std::vector<T>& head, Args&... args) { usize n = order.size(); assert(n == head.size()); std::vector<T> sorted_head(n); for (usize i = 0; i < n; ++i) sorted_head[i] = head[order[i]]; head = std::move(sorted_head); zip_sort_renumber(order, args...); } } // namespace cs_helper template <class T, class... Args> std::vector<usize> zip_sort(std::vector<T>& head, Args&... args) { usize n = head.size(); std::vector<std::pair<T, usize>> tmp(n); for (usize i = 0; i < n; ++i) tmp[i] = std::make_pair(head[i], i); std::sort(tmp.begin(), tmp.end()); std::vector<usize> order(n); for (usize i = 0; i < n; ++i) order[i] = tmp[i].second; cs_helper::zip_sort_renumber(order, head, args...); return order; } #pragma endregion void main_() { /*u64 K, N, M; std::cin >> K >> N >> M; std::vector<u64> A(K); for (auto& el : A) std::cin >> el; if ((N > M and N % M == 0) or (N < M and M % N == 0)) { std::cout << 0 << '\n'; return; } long double ok = 10000000000.0, ng = 0.; while (ok - ng > 0.000001) { const auto mid = (ok + ng) / 2; i64 left = 0, right = 0; for (usize i : range(0, K)) { } }*/ usize N; std::cin >> N; std::vector<u64> T1, T2, T3, T4; for (u64 i : range(1, 10001)) { if (i % 2 == 0 and i % 3 == 0 and i % 5 == 0) T4.emplace_back(i); else if (i % 2 == 0 and i % 3 == 0) T1.emplace_back(i); else if (i % 2 == 0 and i % 5 == 0) T2.emplace_back(i); else if (i % 3 == 0 and i % 5 == 0) T3.emplace_back(i); } std::vector<u64> ans; ans.emplace_back(T1[0]); ans.emplace_back(T2[0]); ans.emplace_back(T3[0]); for (usize i : range(1, T1.size())) ans.emplace_back(T1[i]); for (usize i : range(1, T2.size())) ans.emplace_back(T2[i]); for (usize i : range(1, T3.size())) ans.emplace_back(T3[i]); for (usize i : range(1, T4.size())) ans.emplace_back(T4[i]); for (usize i : range(0, N)) { std::cout << ans[i] << ' '; } // AtCoder Regular Contest 118 } int main() { main_(); }
#include <bits/stdc++.h> using namespace std; int last[120000],a[120000]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ for(int j=2*i;j<=n;j+=i){ last[j]=i; } } for(int i=1;i<=n;i++) { a[i]=a[last[i]]+1; printf("%d ",a[i]); } return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <queue> #include <deque> #include <bitset> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <functional> #include <numeric> #include <utility> #include <iomanip> #include <limits> #include <time.h> #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #define debug(x) cout << #x << " = " << x << endl #define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++) #define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int MAX = 20; bool visited[MAX]; int adj[MAX]; int n, m; vector<int> bfs(int source) { queue<int> q; q.push(source); visited[source] = true; vector<int> touched; while (!q.empty()) { int cur = q.front(); q.pop(); touched.push_back(cur); fori (i, 0, n) { if (adj[cur] & (1 << i)) { if (!visited[i]) { visited[i] = true; q.push(i); } } } } return touched; } int cant[3]; void roll(const vector<int> &v, int idx, ll &total) { if (idx == (int) v.size()) { total++; return; } int k = v[idx]; fori (i, 0, 3) { if (cant[i] & (1 << k)) continue; int prv_cant = cant[i]; cant[i] |= adj[k]; roll(v, idx + 1, total); cant[i] = prv_cant; } } ll calc(const vector<int> &v) { ll total = 0; roll(v, 0, total); return total; } void solve() { cin >> n >> m; fori (i, 0, m) { int x, y; cin >> x >> y; x--, y--; adj[x] |= 1 << y; adj[y] |= 1 << x; } ll ans = 1; fori (i, 0, n) { if (!visited[i]) { vector<int> v = bfs(i); ans *= calc(v); } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> #define speed \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define precision \ cout.precision(30); \ cerr.precision(10); #define ll long long #define ld long double #define pll pair<ll, ll> #define pii pair<int, int> #define forn(n) for (int i = 1; i <= n; i++) #define forlr(l, r) for (int i = l; i != r; (l > r ? i-- : i++)) #define pb(x) push_back(x) #define sz(x) (int)x.size() #define mp(x, y) make_pair(x, y) #define all(x) x.begin(), x.end() #define pc(x) __builtin_popcount(x) #define pcll(x) __builtin_popcountll(x) #define F first #define S second using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); void ioi(string name) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } ll n, m, e, k = 1, a[25]; vector<ll> t, g[25]; bitset<25> w; void dfs(ll v) { t.pb(v); w[v] = 1; for (auto to : g[v]) if (!w[to]) dfs(to); } void kek(ll v = 0) { if (sz(t) <= v) { e++; return; } ll x = t[v]; for (int i = 1; i <= 3; i++) { ll ok = 1; for (auto j : g[x]) if (a[j] == i) goto bruh; a[x] = i; kek(v + 1); a[x] = 0; bruh:; } } int main() { speed; precision; // code cin >> n >> m; for (int i = 1; i <= m; i++) { ll u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } for (int i = 1; i <= n; i++) { if (w[i]) continue; t.clear(); dfs(i); e = 0; kek(); k *= e; } cout << k; // endl #ifndef ONLINE_JUDGE cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <cstdio> #include <algorithm> #include <cmath> const int N = 100 + 5; const double eps = 1e-8; struct Vertice { double x, y; void set_point(double x_, double y_) { x = x_, y = y_; } void set_line(int y_) { x = 205, y = y_; } bool line() const { return x > 204; } }; double distance(const Vertice &lhs, const Vertice &rhs) { if(!lhs.line() && !rhs.line()) #define sq(x) ((x) * (x)) return std::sqrt(sq(lhs.x - rhs.x) + sq(lhs.y - rhs.y)); #undef sq else if(lhs.line() && rhs.line()) return std::abs(lhs.y - rhs.y); else return std::abs(lhs.y - rhs.y); } Vertice a[N]; int n; int fa[N]; void init() { for(int i = 1; i <= n + 2; i++) fa[i] = i; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } bool check(double x) { init(); for(int i = 1; i <= n + 2; i++) for(int j = 1; j <= n + 2; j++) if(i != j && x - distance(a[i], a[j]) > eps) { int x = find(i), y = find(j); if(x != y) fa[x] = y; } // std::printf("check %lf\n", x); if(find(1) == find(2)) return false; else return true; } int main() { std::scanf("%d", &n); a[1].set_line(100); a[2].set_line(-100); for(int i = 3; i <= n + 2; i++) { double x, y; std::scanf("%lf%lf", &x, &y); a[i].set_point(x, y); } // for(int i = 1; i <= n + 2; i++) // for(int j = 1; j <= n + 2; j++) { // if(i < 3) std::printf("Line[%d]\t<-->\t", (int)a[i].y); // else std::printf("(%.0lf, %.0lf) \t<-->\t", a[i].x, a[i].y); // if(j < 3) std::printf("Line[%d]\t= ", (int)a[j].y); // else std::printf("(%.0lf, %.0lf) \t= ", a[j].x, a[j].y); // std::printf("%.3lf\n", distance(a[i], a[j])); // } double l = 0, r = 200; while(r - l > eps) { double mid = (l + r) / 2; if(check(mid * 2)) l = mid; else r = mid; } std::printf("%.5lf", l); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; typedef pair<double, ll> pdl; #define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i)) #define REP(i, n) FOR(i,n,0) #define OF64 std::setprecision(40) const ll MOD = 1000000007; const ll INF = (ll) 1e15; ll N; pll A[105]; struct UnionFind { UnionFind(int n) { rank.assign(n, 0); parent.resize(n); for (int i = 0; i < n; ++i) { parent[i] = i; } } int find(int x) { if (x == parent[x]) return x; return parent[x] = find(parent[x]); } bool same(int x, int y) { return find(x) == find(y); } void unit(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { parent[x] = y; } else { parent[y] = x; if (rank[x] == rank[y]) rank[x]++; } } vector<int> rank; vector<int> parent; }; double len(pll a, pll b) { double x = a.first - b.first; double y = a.second - b.second; return sqrt(x * x + y * y); } bool check(double r) { UnionFind uf(N + 2); REP(i, N) { FOR(j, N, i + 1) { if (len(A[i], A[j]) < 2 * r) uf.unit(i, j); } } REP(i, N) { if (100 - A[i].second < 2 * r) uf.unit(N, i); if (A[i].second + 100 < 2 * r) uf.unit(N + 1, i); } return !uf.same(N, N + 1); } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; REP(i, N) { cin >> A[i].first >> A[i].second; } double ok = 0, ng = 101; REP(_, 100) { double mid = (ok + ng) * 0.5; if (check(mid)) ok = mid; else ng = mid; } cout << OF64 << ok << endl; }