code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
// arc110_b #include <bits/stdc++.h> #ifdef LOCAL #include "../cxx-prettyprint/prettyprint.hpp" #endif using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; #define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i)) #define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i)) #define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i)) #define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i)) #define ALL(x) x.begin(), x.end() #define INF (ll)(1e9) #define MOD (1000000007) #define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; } #define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;} template<class T> void print(const T& x){cout << x << "\n";} template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); } struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain; int main() { #ifdef LOCAL ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf()); #endif int N; string T; cin >> N >> T; string s = "110"; ll ans = 0; REP(k, 3){ bool same = true; REP(i, N){ if (s[(i + k)%3] != T[i]){ same = false; break; } } if (same){ ans += (ll)1e10 - (k+N+2)/3 + 1; } } print(ans); return 0; }
#include<bits/stdc++.h> #define f first #define int long long #define s second using namespace std; const int N=2e5+5,mod=1e9+7; int t,n,a,b; string s; main(){ // t=1; cin>>a>>b; if(a>=b) { int sum=0; for(int i=1;i<=a;i++){ cout<<i<<" "; if(i>=b) sum -= i; } for(int i=1;i<=b;i++) { if(i<b) cout<<-i<<" "; else cout<<sum; } } else if(a<b) { swap(a,b); int sum=0; for(int i=1;i<=a;i++){ cout<<-i<<" "; if(i>=b) sum += i; } for(int i=1;i<=b;i++) { if(i<b) cout<<i<<" "; else cout<<sum; } } }
#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"); 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; struct abc{ int u,c,d; }; int f(int a,int b){ int re=LLONG_MAX; lop (i,-1,2,1){ int z=b+i; if (z<0)z=0; re=min(z+a/(1+z),re); } return re; } vector <abc> adj[MAX];const int INF=LLONG_MAX; void dijsktra(int u,vii &dst,int n){ dst.assign(n+1,INF); dst[u]=0; priority_queue < prii ,vector <prii> ,greater <prii>> q; q.push({0,u}); while (!q.empty()){ int wt=q.top().F; int u=q.top().S;q.pop(); if (wt!=dst[u]){ continue; } //cout<<u<<" "<<wt<<"\n"; for (auto v:adj[u]){ int re=sqrt(v.d); int zr=dst[u]; // cout<<v.u<<" "<<re<<" "; if (wt>re){ re=wt; } zr=re; re=v.c+v.d/(re+1); // cout<<ren"; if (re+zr<dst[v.u]){ dst[v.u]=re+zr; q.push({dst[v.u],v.u}); } } } } void solve(){ int n,m;cn(n)cn(m) lop (i,0,m,1){ int a,b,c,d; cn(a)cn(b)cn(c)cn(d) if (a!=b){ adj[a].PB({b,c,d}); adj[b].PB({a,c,d}); } } vii dst; dijsktra(1,dst,n); int z=dst[n]; if (z==INF){ z=-1; } cout<<z<<"\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; typedef long long ll; typedef unsigned long long ull; typedef double dd; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int,int> pii; const int Mx=1e6+11; const double PI = acos(-1); #define MOD 1000000007 #define nl '\n' #define pb push_back #define F first #define S second #define I insert #define mp make_pair #define sz(x) x.size() #define tt int t; cin>>t; while(t--) #define be(a) (a).begin(),(a).end() #define rbe(a) (a).rbegin(),(a).rend() #define mem(a,b) memset(a,b,sizeof(a)) #define yes cout<<"Yes\n" #define no cout<<"No\n" #define su ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); #define frac cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); int main() { su; string s,t; cin>>s; int n=sz(s),cnt; if(n==1&&s=="8") yes; else if(n==2) { if(stoi(s)%8==0) yes; else { swap(s[0],s[1]); if(stoi(s)%8==0) yes; else no; } } else if(n>=3) { map<char,int>m,mp; for(int i=0; i<n; i++) { m[s[i]]++; } if(m['0']>=3) yes; else { for(int i=104; i<1000; i+=8) { t=to_string(i); cnt=0; mp=m; for(int j=0; j<sz(t); j++) { if(mp[t[j]]>=1) { cnt++; mp[t[j]]--; } } if(cnt==sz(t)) { yes; return 0; } } no; } } else no; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; vector<int> adj[200005]; int n, k; array<int, 3> dp[200005]; // (#special, min dist(u, special), max dist(u, not satisfy yet)) array<int, 3> combine(array<int, 3> a, array<int, 3> b, int d) { array<int, 3> res; res[0] = a[0] + b[0]; res[1] = min(a[1], b[1] + 1); res[2] = max(a[2], b[2] + 1); if (res[1] + res[2] <= d) { res[2] = -INF; } else if (res[2] == d) { res[0]++; res[1] = 0; res[2] = -INF; } return res; } void dfs(int u, int p, int d) { dp[u] = {0, d + 1, 0}; for (auto i: adj[u]) { if (i == p) continue; dfs(i, u, d); dp[u] = combine(dp[u], dp[i], d); } // cout << u << " " << dp[u][0] << " " << dp[u][1] << " " << dp[u][2] << endl; } int solve(int d) { dfs(1, 0, d); return dp[1][0] + (dp[1][2] >= 0); // cout << dp[1][0] << endl; } int main() { //freopen("input.txt", "r", stdin); ios::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } // cout << solve(2) << endl; // return 0; // for (int i = 1; i <= n; i++) { // cout << solve(i) << " "; // } // return 0; int lower = 1, upper = n; while (lower < upper) { int mid = (lower + upper) / 2; if (solve(mid) <= k) { upper = mid; } else lower = mid + 1; } cout << lower << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long N, M; cin >> N >> M; vector<long long> X(M), Y(M); for (int i = 0; i < M; i++) cin >> X[i] >> Y[i]; set<pair<long long, long long>> Set; for (int i = 0; i < M; i++) { Set.insert(make_pair(X[i], Y[i])); } set<long long> Sec; Sec.insert(N); Sec.insert(-(1LL << 60)); Sec.insert(1LL << 60); Sec.insert(1LL << 59); long long prev = 0; set<long long> help, help2; for (int i = 0; i < M; i++) { auto a = *Set.begin(); Set.erase(Set.begin()); if (prev != a.first) { while (!help2.empty()) { Sec.erase(*help2.begin()); help2.erase(help2.begin()); } while (!help.empty()) { Sec.insert(*help.begin()); help.erase(help.begin()); } prev = a.first; } auto b = Sec.lower_bound(a.second); if (*b == a.second) help2.insert(a.second); b--; if (*b == a.second - 1) help.insert(a.second); b++; if (*b == a.second + 1) help.insert(a.second); b++; if (*b == a.second + 1) help.insert(a.second); } while (!help2.empty()) { Sec.erase(*help2.begin()); help2.erase(help2.begin()); } while (!help.empty()) { Sec.insert(*help.begin()); help.erase(help.begin()); } cout << Sec.size() - 3 << endl; }
#include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() #define int long long bool can[(int) 1e5 + 5] = {false}; int32_t main() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (auto &x: a) cin >> x; int s = accumulate(all(a), 0); can[0] = true; for (int i = 0; i < n; ++i) { for (int j = (int)1e5; j >= 0; --j) { if (j + a[i] <= (int)1e5) can[j + a[i]] |= can[j]; } } int mn = 1e18; for (int j = 0; j <= (int)1e5; ++j) { if (can[j]) mn = min(mn, max(j, s - j)); } cout << mn; }
#include <bits/stdc++.h> #define rep(i,n) for ((i)=1;(i)<=(n);(i)++) using namespace std; const int mod=1e9+7; int n,i,a[100005],s=1; int main(){ cin>>n; rep(i,n)cin>>a[i]; sort(a+1,a+n+1); rep(i,n){ s=1ll*s*(a[i]-a[i-1]+1)%mod; } cout<<s<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define rep(i, n) for(int i = 0;i < n;i++) #define rep1(i, n) for(int i = 1;i < n;i++) #define repr(i, n) for(int i = n;i >= 0;i--) #define FOR(i, m, n) for(int i = m;i < n;i++) vector<vector<ll>> near8={{-1,1},{0,1},{1,1},{-1,0},{1,0},{-1,-1},{0,-1},{1,-1}}; vector<vector<ll>> near4={{0,1},{-1,0},{1,0},{0,-1}}; ll INF = INT64_MAX; bool ll_sqrt(ll x, ll& lower_bound, ll& upper_bound) { constexpr ll ll_max = numeric_limits<ll>::max(); bool first_boost = true; lower_bound = 0; upper_bound = x; while (upper_bound - lower_bound > 1) { if (first_boost) { ll guess = (ll)sqrt((double)x); lower_bound = max(guess - 1, (ll)0); upper_bound = lower_bound + 3; first_boost = false; } ll newval = (lower_bound + upper_bound) / 2; if ((newval != 0) && (newval > ll_max / newval)) // newval*newval becomes overflow upper_bound = newval; // because overflow means newval is too big else { ll newval_sqr = newval * newval; if (newval_sqr < x) lower_bound = newval; else upper_bound = newval; } } if ((upper_bound != 0) && (upper_bound > ll_max / upper_bound)) { if (lower_bound * lower_bound == x) upper_bound = lower_bound; } else { if (lower_bound * lower_bound == x) upper_bound = lower_bound; else if (upper_bound * upper_bound == x) lower_bound = upper_bound; } bool is_exact = (lower_bound == upper_bound); return is_exact; } ll ll_ceil(ll x, int digit); ll ll_floor(ll x, int digit); ll ll_floor(ll x, int digit) { if (x < 0) return -ll_ceil(-x, digit); ll div = 1; while (digit != 0) { div *= 10; digit--; } ll modval = x % div; ll ans = x - modval; return ans; } ll ll_ceil(ll x, int digit) { if (x < 0) return -ll_floor(-x, digit); ll div = 1; while (digit != 0) { div *= 10; digit--; } ll modval = x % div; ll ans = x - modval; if (modval != 0) ans += div; return ans; } ll H,W; vector<vector<char>> map1(501, vector<char>(501,'#')); bool solve(ll h, ll w){ ll nexth=0; ll nextw=0; for(vector<ll> to:near4){ nexth = h+to[0]; nextw = w+to[1]; if(nexth < 0 || nexth >=H || nextw < 0 || nextw >=W || map1[nexth][nextw] == '#'){ continue; } if(map1[nexth][nextw] =='g'){ return true; } map1[nexth][nextw]='#'; if(solve(nexth, nextw)){ return true; }; } return false; } int main() { ll a,b,c; cin >> a>>b>>c; ll ans=0; ans+=(7-a); ans+=(7-b); ans+=(7-c); cout << ans << endl; return 0; }
/* Before submitting, please CHECK: maxn, modu, inf, eps */ #include <bits/stdc++.h> #define lc (o<<1) #define rc ((o<<1)|1) #define PB push_back #define MK make_pair #define ALL(x) x.begin(), x.end() #define FT first #define SC second using namespace std; #define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl const int maxn = 2e6 + 5; const int modu = 1e9 + 7; // 1e9 + 7 const int inf = 0x7fffffff; const double eps = 1e-5; typedef long long LL; int n, m, k; LL inv[maxn]; LL C(int n, int m) { if (m > n) return 0; LL res = 1; for (int i = n; i >= n-m+1; --i) res = res*i % modu; for (int i = 1; i <= m; ++i) res = res * inv[i] % modu; return res; } int main() { // freopen("my.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; if (n > m + k) { cout << 0 << endl; return 0; } inv[1] = 1; for (int i = 2; i <= n+m; ++i) inv[i] = (modu - (modu/i))*inv[modu%i]%modu; cout << ((C(n+m, n) - C(n+m, m+k+1)) % modu + modu)%modu << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve(){ int n; long long c; cin>>n>>c; vector<long long> a(n),b(n); vector<long long> c1(n); set<int> s; for(int i=0;i<n;i++){ cin>>a[i]>>b[i]>>c1[i]; s.insert(a[i]); s.insert(b[i]+1); } map<long long,long long> m; vector<long long> rev(2*n+1); int cord = 0; for(auto el: s){ rev[cord] = el; m[el] = cord++; } vector<long long> p(cord+5); for(int i=0;i<n;i++){ p[m[a[i]]] += c1[i]; p[m[b[i]+1]] -= c1[i]; } long long cost =min(c, p[0]); for(int i=1;i<cord;i++){ cost += min(p[i-1],c)*(rev[i]-rev[i-1]-1); p[i] += p[i-1]; cost += min(p[i], c); } cout<<cost<<"\n"; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int t; // cin>>t; t = 1; while(t--){ solve(); } }
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <regex> #include <cmath> #include <algorithm> #include <vector> #include <list> #include <map> #include <unordered_set> #include <queue> #include <unordered_map> #include <set> #include <cassert> #include <climits> #include <bitset> #pragma GCC optimize("O3") using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i) #define repd(i, n) for(ll i = (n) - 1ll; i >= 0; --i) #define rep1(i, n) for(ll i = 1, i##_len = (n); i <= i##_len; ++i) #define FOR(i, a, b) for(ll i = a; i < ll(b); ++i) #define FORD(i, a, b) for(ll i = a; i > ll(b); --i) #define foreach(i, itr) for(auto& i : (itr)) // 小さい順 #define asort(arr, size) std::sort((arr), (arr) + (size)) // 大きい順 #define dsort(arr, size) std::sort((arr), (arr) + (size), [](auto a, auto b) { return a > b; }) #define vsort(vec) std::sort((vec).begin(), (vec).end()); #define rsort(vec) std::sort((vec).begin(), (vec).end(), [](auto a, auto b) { return a > b; }); #define bit(n) (1LL << (n)) // 行数多く出力するとき用 inline void println(int n) { printf("%d\n", n); } inline void println(ll n) { printf("%lld\n", n); } inline void println(ull n) { printf("%llu\n", n); } inline void println(int* n, size_t size) { FOR(i, 0, size - 1) { printf("%d ", n[i]); } printf("%d\n", n[size - 1]); } inline void println(ll* n, size_t size) { FOR(i, 0, size - 1) { printf("%lld ", n[i]); } printf("%lld\n", n[size - 1]); } inline void println(ull* n, size_t size) { FOR(i, 0, size - 1) { printf("%llu ", n[i]); } printf("%llu\n", n[size - 1]); } //////////////////////////////////// //////////////////////////////////// int main() { ll N, C; cin >> N >> C; vector<pair<ll, ll>> events; rep(i, N) { ll a, b, c; cin >> a >> b >> c; events.emplace_back(a - 1, c); events.emplace_back(b, -c); } vsort(events); ll ans = 0, fee = 0, t = 0; foreach(event, events) { if (event.first != t) { ans += min(C, fee) * (event.first - t); t = event.first; } fee += event.second; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { //freopen("in.txt", "r", stdin); int tt; cin >> tt; while (tt--) { long long n; cin >> n; if (n & 1) { puts("Odd"); } else { n >>= 1; if (n & 1) puts("Same"); else puts("Even"); } } return 0; }
#include<bits/stdc++.h> typedef long long ll; #define int ll #define endl '\n' using namespace std; const int mod = 998244353; const int MAXN = 1000000; //int fa[MAXN]; int b[205]; void solve() { int n; cin >> n; int res = 0; for(int i=0;i<n;i++) { int x; cin >>x; b[x%200]++; } for(int i=0;i<200;i++) res += (b[i]-1)*b[i]/2; cout << res; } signed main(){ ios::sync_with_stdio(0);cin.tie(nullptr); int _=1; //cin>>_; for(_;_--;) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int n,m; long long f[20][1<<18]; vector<pair<int,int>> cs[25]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=0;i<m;i++){ int x,y,z; cin>>x>>y>>z; x--; y--; cs[x].emplace_back(y,z); } f[0][0]=1; for(int i=0;i<n;i++){ for(int s=0;s<1<<n;s++)if(f[i][s]){ static int pre[18]; pre[0]=s&1; for(int j=1;j<n;j++)pre[j]=pre[j-1]+(s>>j&1); for(int j=0;j<n;j++)if(s>>j&1^1){ bool can=true; for(pair<int,int> &p:cs[i])if(p.second<pre[p.first]+(p.first>=j)){ can=false; break; } if(can)f[i+1][s|1<<j]+=f[i][s]; } } } cout<<f[n][(1<<n)-1]; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n, t; cin >> n; double a, b; vector<double> l(n), r(n); for(int i = 0; i < n; ++i){ cin >> t >> a >> b; if(t == 2){b -= 0.1;} else if(t == 3){a += 0.1;} else if(t == 4){a += 0.1; b -= 0.1;} l[i] = a; r[i] = b; } // for(int i = 0; i < n; ++i){cout << l[i] << " ";}cout << endl; // for(int i = 0; i < n; ++i){cout << r[i] << " ";}cout << endl; double c, d, e, f; int ans = 0; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ if(i == j){continue;} c = l[i]; d = r[i]; e = l[j]; f = r[j]; if(d > f){swap(c, e); swap(d, f);} if(d >= e){ans += 1;} } } cout << ans/2 << endl; }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #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 ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;} template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int T; int N; int P[555]; vector<int> R; void go(int x) { swap(P[x],P[x+1]); R.push_back(x); } void solve() { int i,j,k,l,r,x,y; string s; cin>>T; while(T--) { cin>>N; FOR(i,N) { cin>>P[i]; P[i]--; } R.clear(); int step=0; for(i=N-1;i>=3;i--) { FOR(j,N) if(P[j]==i) break; if(j==i) continue; if(i==3&&j==2&&step==1) { go(1); go(2); j=1; } while(j<i) { if(step==j%2) { go(j); j++; } else if(step==0) { if(j==1) go(2); else go(0); } else { if(j==2) go(3); else go(1); } step^=1; } } while(1) { for(j=N-1;j>=0;j--) if(P[j]!=j) break; if(j<0) break; assert(j<3); swap(P[step],P[step+1]); R.push_back(step); step^=1; } cout<<R.size()<<endl; FOR(i,R.size()) { assert(R[i]%2==i%2); cout<<(R[i]+1)<<" "; } cout<<endl; FOR(i,N) assert(P[i]==i); } } 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 <iostream> #include <vector> #include <algorithm> using namespace std; using lli = long long int; int main(){ int t; cin >> t; for(int rep=0; rep<t; rep++){ int n; cin >> n; vector<int> a(n); for(int i=0; i<n; i++){ cin >> a[i]; } vector<vector<int>> wait(2); for(int i=0; i<n-1; i++){ if(a[i] > a[i+1]){ wait[i%2].push_back(i); } } vector<int> ans; for(int i=0; i<n*n; i++){ int p = -1; int d = i%2; while(!wait[d].empty()){ int pos = wait[d].back(); wait[d].pop_back(); if(a[pos] > a[pos+1]){ p = pos; break; } } while(!wait[1-d].empty()){ int pos = wait[1-d].back(); if(a[pos] > a[pos+1]){ break; }else{ wait[1-d].pop_back(); } } if(p == -1 and wait[1-d].empty()){ break; } if(p == -1) p = d; ans.push_back(p); swap(a[p], a[p+1]); if(a[p] > a[p+1]){ wait[d].push_back(p); } if(p != 0 and a[p-1] > a[p]){ wait[1-d].push_back(p-1); } if(p != n-2 and a[p+1] > a[p+2]){ wait[1-d].push_back(p+1); } } cout << ans.size() << endl; for(int i: ans){ cout << i+1 << " "; } cout << endl; } return 0; }
/* ** *** Be Name Khoda *** ** */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3 + 10; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, z = 1; cin >> n; int x = 38, y = 26; vector<ll> v3, v5; while (x--) { z *= (ll)3LL; v3.push_back(z); } z = 1; while (y--) { z *= (ll)5LL; v5.push_back(z); } for (int i = 0; i < v3.size(); i++) { for (int j = 0; j < v5.size(); j++) { if (v3[i]+v5[j]<0) break; if (v3[i] + v5[j] == n) return cout << i+1 << ' ' << j+1, 0; } } cout << -1; return 0; }
#include <bits/stdc++.h> #include<sstream> #include<string> #include<vector> #include <set> #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define pb push_back #define mp make_pair #define ll long long #define ff first #define ss second long long M=1000000007; using namespace std; ll fact[10000]; ll power(ll x, unsigned ll y, unsigned ll m) { if (y == 0) return 1; ll p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } unsigned long long modInverse(unsigned long long n, int p) { return power(n, p - 2, p); } unsigned long long nCrModPFermat(unsigned long long n, int r, int p) { // If n<r, then nCr should return 0 if (n < r) return 0; // Base case if (r == 0) return 1; return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) % p) % p; } int32_t main() { IOS string s; cin>>s; cout<<s[1]<<s[2]<<s[0]; return 0; }
#include <bits/stdc++.h> #define ll long long #define rep(i,n) for(int i=0;i<n;i++) using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(0); ll k,n,m; cin>>k>>n>>m; vector<ll> a(k); rep(i,k) cin>>a[i]; vector<ll> b(k, 0); rep(i,k) { b[i] = a[i] * m; b[i] /= n; } ll lim = m; rep(i,k) lim -= b[i]; vector<pair<ll,int>> c(k); rep(i,k) { pair<ll,int>p = make_pair(n*b[i] - m*a[i], i); c[i] = p; } sort(c.begin(), c.end()); int cnt = 0; while (cnt < lim) { b[c[cnt].second]++; cnt++; } rep(i,k) cout << b[i] << " \n"[i == k-1]; }
/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll a,b,c; cin>>a>>b>>c; string ans=""; if(c%2==0){ int x=abs(a); int y=abs(b); if(x>y){ ans+=">"; }else if(x<y){ ans+="<"; }else{ ans+="="; } }else{ if(a<0 && b<0){ if(a>b){ ans+="<"; }else if(a<b){ ans+=">"; }else{ ans+="="; } }else{ if(a>b){ ans+=">"; }else if(a<b){ ans+="<"; }else{ ans+="="; } } } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll startTime; const int Di[] = {-1,+1, 0, 0}; const int Dj[] = { 0, 0,-1,+1}; long long getTime() { timespec ts; clock_gettime(CLOCK_REALTIME_COARSE, &ts); long long r = ts.tv_sec; r *= 1000000000; r += ts.tv_nsec; return r; } struct Input { int Si, Sj; vector<vector<int>> T, P; Input() : T(50, vector<int>(50)), P(50, vector<int>(50)) { cin >> Si >> Sj; for (auto& a : T) for (auto& x : a) cin >> x; for (auto& a : P) for (auto& x : a) cin >> x; } }; struct State { vector<bool> visited; int i, j, score; State(){} State(int M, int i, int j, int score) : visited(M), i(i), j(j), score(score) {} }; struct Solver { const Input& X; mt19937 mt; int M; pair<int,int> best0; int cnt_best0; pair<int,pair<int,int>> best1; Solver(const Input& x) : X(x) { random_device seed_gen; mt = mt19937(seed_gen()); } int rnd(int n) { return uniform_int_distribution<>(0,n-1)(mt); } bool valid(const State& s, int d) { int i = s.i + Di[d]; int j = s.j + Dj[d]; return 0 <= i && i < 50 && 0 <= j && j < 50 && !s.visited[X.T[i][j]]; } void move(State& s, int d) { s.i += Di[d]; s.j += Dj[d]; s.visited[X.T[s.i][s.j]] = true; s.score += X.P[s.i][s.j]; } void f(int p, int q, int prev, const State& s) { if (p == 15) { if (s.score > best0.first) { best0 = {s.score, q}; cnt_best0 = 1; } else if (s.score == best0.first) { if (rnd(++ cnt_best0) == 0) { best0 = {s.score, q}; } } return; } else { best1 = max(best1, {s.score, {p, q}}); } for (int d = 0; d < 4; ++ d) if ((d^1) != prev && valid(s, d)) { State ss = s; move(ss, d); f(p+1, q | (d << (2*p)), d, ss); } } void solve() { M = 0; for (auto& a : X.T) for (auto& x : a) M = max(M, x+1); int best_score = -1; vector<int> best_res; { State s0(M, X.Si, X.Sj, X.P[X.Si][X.Sj]); s0.visited[X.T[X.Si][X.Sj]] = true; vector<int> res; for (;;) { best0 = {-1, 0}; best1 = {-1, {0, 0}}; f(0, 0, -1, s0); if (best1.first > best_score) { best_score = best1.first; auto rr = res; for (int i = 0; i < best1.second.first; ++ i) { int d = (best1.second.second >> (2*i)) & 3; rr.push_back(d); } best_res = rr; } if (best0.first == -1) { break; } else { int d = best0.second & 3; res.push_back(d); move(s0, d); } } } cerr << "score = " << best_score << endl; for (auto x : best_res) cout << "UDLR"[x]; cout << endl; } }; int main() { startTime = getTime(); Input x; Solver y(x); y.solve(); }
#include <iostream> #include <string> #include <algorithm> #include <iostream> #include <string> #include <algorithm> #include <vector> #include <iomanip> #include <cmath> #include <stdio.h> #include <queue> #include <deque> #include <cstdio> #include <set> #include <map> #include <bitset> #include <stack> #include <cctype> using namespace std; int t[55][55]; int p[55][55]; bool bo[2600] = { false }; int b1[4]; int b2[4]; int b3[4] = { 1,-1,0,0 }; int b4[4] = { 0,0,1,-1 }; int b5[4] = { 0,1,2,3 }; int main() { int x, y; cin >> x >> y; stack<pair<int, int>> s; s.push(make_pair(x, y)); for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { cin >> t[i][j]; } } bo[t[x][y]] = true; for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { cin >> p[i][j]; } } string ans = ""; int score = -1000000007; vector<pair<int, int>> vec; bool bo2[2600]; do { for (int i = 0; i < 4; i++) { b1[i] = b3[b5[i]]; b2[i] = b4[b5[i]]; } string ans1 = ""; int score1 = 0; vector<pair<int, int>> vec1; while (1) { int nx = s.top().first; int ny = s.top().second; bool bo1 = false; for (int i = 0; i < 4; i++) { if (nx + b1[i] < 0 || nx + b1[i] >= 50 || ny + b2[i] < 0 || ny + b2[i] >= 50)continue; if (!bo[t[nx + b1[i]][ny + b2[i]]]) { bo[t[nx + b1[i]][ny + b2[i]]] = true; s.push(make_pair(nx + b1[i], ny + b2[i])); bo1 = true; break; } } if (!bo1) { vec1.emplace_back(make_pair(nx, ny)); score1 += p[nx][ny]; while (s.size() > 1) { s.pop(); if (nx == s.top().first + 1) { ans1 += 'D'; } if (nx == s.top().first - 1) { ans1 += 'U'; } if (ny == s.top().second + 1) { ans1 += 'R'; } if (ny == s.top().second - 1) { ans1 += 'L'; } nx = s.top().first; ny = s.top().second; vec1.emplace_back(make_pair(nx, ny)); score1 += p[nx][ny]; } reverse(ans1.begin(), ans1.end()); reverse(vec1.begin(), vec1.end()); if (score < score1) { ans = ans1; score = score1; vec = vec1; for (int i = 0; i <= 2500; i++) { bo2[i] = bo[i]; } } break; } } while (!s.empty()) { s.pop(); } s.push(make_pair(x, y)); for (int i = 0; i <= 2500; i++) { bo[i] = false; } bo[t[x][y]] = true; } while (next_permutation(b5, b5 + 4)); for (int i = 0; i < 10000; i++) { int ni = rand() % (vec.size() - 1); if (vec[ni].first == vec[ni + 1].first) { if (vec[ni].second == vec[ni + 1].second - 1) { if (!bo2[t[vec[ni].first][vec[ni].second + 1]]) { if (!bo2[t[vec[ni + 1].first][vec[ni + 1].second + 1]]) { bo2[t[vec[ni].first][vec[ni].second + 1]] = true; bo2[t[vec[ni + 1].first][vec[ni + 1].second + 1]] = true; ans = ans.substr(0, ni - 1) + "DRU" + ans.substr(ni, ans.size() - ni); } } if (!bo2[t[vec[ni].first][vec[ni].second - 1]]) { if (!bo2[t[vec[ni + 1].first][vec[ni + 1].second - 1]]) { bo2[t[vec[ni].first][vec[ni].second + 1]] = true; bo2[t[vec[ni + 1].first][vec[ni + 1].second + 1]] = true; ans = ans.substr(0, ni - 1) + "URD" + ans.substr(ni, ans.size() - ni); } } } else { if (!bo2[t[vec[ni].first][vec[ni].second + 1]]) { if (!bo2[t[vec[ni + 1].first][vec[ni + 1].second + 1]]) { bo2[t[vec[ni].first][vec[ni].second + 1]] = true; bo2[t[vec[ni + 1].first][vec[ni + 1].second + 1]] = true; ans = ans.substr(0, ni - 1) + "DLU" + ans.substr(ni, ans.size() - ni); } } if (!bo2[t[vec[ni].first][vec[ni].second - 1]]) { if (!bo2[t[vec[ni + 1].first][vec[ni + 1].second - 1]]) { bo2[t[vec[ni].first][vec[ni].second + 1]] = true; bo2[t[vec[ni + 1].first][vec[ni + 1].second + 1]] = true; ans = ans.substr(0, ni - 1) + "DRU" + ans.substr(ni, ans.size() - ni); } } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double const int mod = 1000000007; int s, p;bool ans=0; // Function to print the divisors void printDivisors() { // Note that this loop runs till square root for (int i=1; i*i<=p; i++) { if (p%i == 0) { // If divisors are equal, print only one if((p/i + i) ==s){ans=1;break;} } } } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>s>>p; printDivisors(); if(ans==1)cout<<"Yes\n"; else cout<<"No\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<LD> VLD; typedef vector<string> VS; #define rep(i, max) for (LL i = 0; i < (LL)(max); i++) #define repm(i, min, max) for (LL i = (LL)(min); i < (LL)(max); i++) #define rev(i, max) for (LL i = (LL)(max) - 1; i >= 0; i--) #define revm(i, min, max) for (LL i = (LL)(max) - 1; i >= (LL)(min); i--) int main() { LL S,P; cin>>S>>P; repm(n,1,min(S, (LL)sqrt(P)+1)){ if(P%n==0){ if(S-P/n==n){ cout<<"Yes"<<endl; return 0; } } } cout<<"No"<<endl; return 0; }
#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define PPC(x) __builtin_popcount(x) #define MSB(x) (31 - __builtin_clz(x)) #define SZ(x) ((int)(x).size()) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m) >> (i) & 1) #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; const int maxN = 1 << 8; char T[maxN][maxN]; void solve() { int n, m; scanf ("%d%d", &n, &m); FOR(i, 0, n) scanf ("%s", &T[i][0]); int res = 0; FOR(i, 0, n) FOR(j, 0, m) { if (T[i][j] == '.' and T[i][j+1] == '.') res++; if (T[i][j] == '.' and T[i+1][j] == '.') res++; } printf("%d\n", res); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define FOR(i,a,b) for(int _n(b),i(a);i<_n;i++) #define REP(i,n) FOR(i,0,n) void setIO() { ios_base::sync_with_stdio(0); cin.tie(0); } const int MX = 3; int a[MX]; int n; int main() { setIO(); REP(i, 3) cin >> a[i]; sort(a, a+3); int r = 0; if (a[0] == a[1]) r = a[2]; else if (a[1] == a[2]) r = a[0]; cout << r << endl; return 0; }
#include<bits/stdc++.h> #define loop(i, n) for(int i = 0; i < n; i++) #define pb push_back #define ll long long int #define vi vector<int> #define pi pair<int, int> #define W(t) int t; cin >> t; while(t--) #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ff first #define ss second #define rep(i, a, b)for(ll i = a; i <= b; i++) #define dep(i, a, b)for(ll i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define PI 3.14159265 using namespace std; const ll mod = (ll)(998244353); ll test = 1; vector<ll> read(int n){ vector<ll> v(n); loop(i, n) cin >> v[i]; return v; } std::vector<ll> a; std::vector<ll> adj[200005]; bitset<200005> used; std::vector<ll> dp(200005, -2e9); void dfs(ll node){ used[node]= 1; ll ans = INT_MIN; dp[node] = max(dp[node], a[node]); for(auto it : adj[node]){ if(!used[it]){ dfs(it); } dp[node] = max(dp[node], dp[it]); } } void solve(){ ll i, j, k, m,n, cnt = 0, ans = 0, sum = 0; cin >> n >> m; a.resize(n); for(auto &i : a)cin >> i; rep(i, 0, m-1){ ll u, v; cin >> u >> v; u--, v--; adj[u].pb(v); } ans = INT_MIN; rep(i, 0, n-1){ if(!used[i]) dfs(i); } ans = INT_MIN; rep(i, 0,n-1){ for(auto j : adj[i]){ ans = max(ans, dp[j]-a[i]); } } cout << ans << "\n"; test++; } int main(){ FIO #ifndef ONLINE_JUDGE freopen("C:/Codeforces/input.txt", "r", stdin); freopen("C:/Codeforces/output.txt", "w", stdout); #endif ll i = 1; //W(t) solve(); return 0; }
#include <bits/stdc++.h> #define pb push_back #define ll long long #define mp make_pair #define si short int #define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define pill pair<ll,ll> #define f first #define s second #define pilc pair<ll,char> #define all(a) (a).begin(),(a).end() #define rep(s,e,step) for(int i = (s); i < (e) ; i += step) #define vrep(s,e,step) for(int j = (s); j < (e) ; j += step) #define ex exit(0) #define sz(a) (a).size() #define triple pair<pill, ll> #define pinode pair<node*, node*> #define quadra pair<pill, pill> #define ld long double using namespace std; const ll N = 1E6 + 100; //const ll M = 1e3 + 10; const ll big = 1e9; const ll pw = 491; const ll hsh1 = 1000003651; const long long mod = 1e9 + 7; const long double EPS = 1e-14; const ll block = 1e7; const ll shift = 2e3; mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count()); ll n, m; vector<ll> g[N]; ll a[N], b[N], ans = -big; ll dp[N]; int main() { speed; cin >> n >> m; for(int i = 1; i <= n; i++) { cin >> a[i]; b[i] = -big; } for(int i = 1, x ,y; i <= m; i++) { cin >> x >> y; g[y].pb(x); } for(int j = n; j >= 1; j--) { ans = max(b[j] - a[j], ans); b[j] = max(b[j], a[j]); for(auto u : g[j]) b[u] = max(b[j], b[u]); } cout << ans; } /* 10 3 0101110001 */
#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define MP std::make_pair #define PR std::pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int przed[200]; bool vis[200]; std::vector<VI> g; void dfs(int v){ vis[v] = true; przed[v]++; TRAV(j, g[v]) if(!vis[j]){ dfs(j); } } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; g.resize(n); FOR(i, n){ FOR(j, n){ char a; std::cin >> a; if(a == '1') g[i].push_back(j); } } FOR(i, n){ FOR(j, 200) vis[j] = false; dfs(i); } long double ans = 0; FOR(i, n) ans += (long double)1 / przed[i]; std::cout << std::fixed << std::setprecision(12) << ans << "\n"; return 0; }
/*Allah Vorosha*/ #include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define in insert #define rev reverse #define all(x) (x).begin(),(x).end() #define all2(x) (x).rbegin(),(x).rend() #define sz(v) (int)v.size() #define yes cout << "YES\n" #define no cout << "NO\n"; #define take for(auto &it : a) cin >> it; #define vi vector<int> #define vii vector<pair<int, int > > #define vl vector<ll> #define fi first #define se second #define l_b lower_bound #define u_b upper_bound #define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie(); #define tail cout << '\n' #define mems(a, b) memset(a, b, sizeof(a)) using namespace std; template<class T> void read(T &x) { cin >> x; } template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); } template<class T> void print1(const T &x) { cout << x << '\n'; } template<class T, class ...U> void print1(const T &x, const U& ... u) { print1(x); print1(u...); } template<class T> void print2(const T &x) { cout << x << ' '; } template<class T, class ...U> void print2(const T &x, const U& ... u) { print2(x); print2(u...); } const int N = 2e5 + 5, M = 5e5 + 5, mod = 1e9 + 7; const ll inf = 9e18; int cs = 1; vector<int> g[N]; int cnt; bool vis[N]; void dfs(int src) { vis[src] = 1; cnt++; for(auto adj : g[src]) { if(vis[adj]) continue; dfs(adj); } } void solve() { int n; read(n); vi a(n), b(N); take; int i = 0, j = n - 1; while(i <= j) { if(a[i] != a[j]) { g[a[i]].pb(a[j]); g[a[j]].pb(a[i]); } i++; j--; } int ans = 0; for(int i = 0; i < n; i++) { if(!vis[a[i]]) { cnt = 0; dfs(a[i]); ans += cnt - 1; } } print1(ans); } int main() { ios; //int tc; cin >> tc; while(tc--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll solve() { ll N, M; cin >> N >> M; ll MM = M*M; auto pow = [](ll x, ll n, ll m) -> ll { ll t = x, r = 1; t %= m; while ( n > 0 ) { if ( n & 1 ) { r *= t; r %= m; } t *= t; t %= m; n >>= 1; } return r; }; ll r1 = pow(10, N, MM); ll ans = r1 / M; return ans; } int main() { auto ans = solve(); cout << ans << "\n"; return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(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'); } } unsigned long long powmod(unsigned long long a, unsigned long long b, unsigned long long m){ unsigned long long r = 1; while(b){ if(b&1){ r = r * a % m; } b>>=1; a = a * a % m; } return r; } int main(){ long long N; rd(N); long long M; rd(M); long long res; res = powmod(10, N, M*M) / M; wt_L(res); wt_L('\n'); return 0; } // cLay version 20210103-1 [bug fixed 1] // --- original code --- // { // ll @N, @M, res; // res = powmod(10, N, M*M) / M; // wt(res); // }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for (long long i = 0; i < (n); ++i) #define INF LONG_MAX/3 #define DIV 1000000007 //#define DIV 998244353 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 dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; long long modpow(long long ori, long long po){ long long res = 1; while(po > 0){ if(po&1){ res *= ori; res %= DIV; } ori *= ori; ori %= DIV; po >>= 1; } return res; } int main(){ ll N, M; cin >> N >> M; vector<ll> A(N); rep(i, N) cin >> A[i]; ll rest = M; rep(i, N) rest -= A[i]; if(rest == 0) { cout << 1 << endl; return 0; } if(rest < 0) { cout << 0 << endl; return 0; } ll ue = 1; ll si = 1; chmin(rest, M + N - rest); for(ll i = M + N; i> M + N - (rest); i--) { ue *= i; ue %= DIV; } for(ll i = rest; i >= 1; i--) { si *= i; si %= DIV; } cout << (ue * modpow(si, DIV-2))%DIV << endl; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stack> #include <queue> #include <set> #include <map> using namespace std; #define MOD 1000000007 #define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; int N, M; int A[2020]; i64 modpow(i64 x, int p = MOD - 2) { if (p == 0) return 1; i64 tmp = modpow(x, p / 2); tmp = tmp * tmp % MOD; if (p % 2 == 1) tmp = tmp * x % MOD; return tmp; } i64 C(i64 x, int y) { i64 ret = 1; for (int i = 0; i < y; ++i) { ret = ret * (x - i) % MOD; ret = ret * modpow(i + 1) % MOD; } return ret; } int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; ++i) scanf("%d", A + i); int sa = 0; for (int i = 0; i < N; ++i) sa += A[i]; // printf("%lld\n", C(M + 1, sa + 1)); printf("%lld\n", C(M + N, sa + N)); return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); int a,b,c,d; cin >> a >> b >> c >> d; cout << a * d - b * c << endl; }
#include <iostream> #include <vector> #include <string> #include <array> #include <functional> #include <algorithm> #include <stack> #include <map> #include <set> #include <climits> #include <queue> #include <bitset> #include <cassert> #include <math.h> #include <complex> #include <iomanip> using namespace std; #define ll long long #define pb(x) push_back(x) #ifdef _OLIMPOLOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 1337 #endif void debug_out() { cout << "\n";} template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cout << " " << A; debug_out(B...); } int test = 1; #define OUT(x) cout << x << "\n";return; //-------------------------- ^ DONT TOUCH ^----------------------------------------------------------------- #define MOD 998244353ll const int lm = 100000; int a, b , c , d; void solve() { cin >> a >> b >> c >> d; cout << a *d - b * c << endl; } int main(int argc, const char * argv[]) { ios_base::sync_with_stdio(0);cin.tie(0); #ifdef _OLIMPOLOCAL cin >> test; #endif for (int testCount = 1; testCount <= test; testCount++) { #ifdef _OLIMPOLOCAL cout << "---------------Test case " << testCount << "---------------"<< endl; #endif solve(); } return 0; }
// JG GAP GG #include <iostream> #include <cstring> #include<vector> #include <algorithm> #include<cstdlib> #include<set> #include<math.h> #include<map> #include<unordered_map> #include<iomanip> #include<queue> #include<bitset> using namespace std; using ll = long long; using ull = unsigned long long; const ll MOD = 1000000007; #define rep(n,x) for(ll i=0;i<n;i++) cin>>x[i]; #define forr(a,b) for(ll i=a;i<b;i++); #define vecsort(myVec,x,y) sort(myVec.begin(),myVec.end(),[](const vector<y> &alpha,const vector<y> &beta){return alpha[x] < beta[x];}); #define vsort(v) sort(v.begin(),v.end()); #define vdownsort(v) sort(v.begin(),v.end(),greater<ll>()); ll modpow(ll a, ll n, ll mod) { ll res = 1; while (0 < n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); double sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; double ans = (sy * gx + sx * gy) / (sy + gy); cout << fixed << setprecision(10) << ans << endl; return 0; }
#include <set> #include <iostream> #include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <queue> #define INFL 100100100100100 #define INF 100100100 using namespace std; int main() { int sx,sy,gx,gy; cin >> sx >> sy >> gx >> gy; printf("%.15lf", sx + (double)(gx-sx) * (sy)/(sy+gy)); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(a, b) (((a) < (b)) ? (b) : (a)) using namespace std; using namespace __gnu_pbds; auto random_address = [] { char *p = new char; delete p; return uint64_t(p); }; const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); mt19937_64 rng(SEED); typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; char s,t; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>s>>t; if(s=='N') cout<<t; else cout<<char(t-32); return 0; }
#include<bits/stdc++.h> #include<iomanip> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);} #define ar array #define all(x) (x).begin(), (x).end() #define Unique(a) a.erase(unique(all(a)), a.end()) #define ld long double #define wh(x) cout<< #x << " is " << x << '\n'; #define pb push_back #define fi first #define se second #define inf 1e9 using namespace __gnu_pbds; using namespace std; typedef long long int ll; template<typename T> using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //member functions : //1. order_of_key(k) : number of elements strictly lesser than k //2. find_by_order(k) : k-th element in the set const int mxn=2e5+5,mod=1e9+7; int main(void){ fast; ll t,n,i,cr=0; cin>>t>>n; ll l=1,h=1e17; while(l<h&&cr<100){ ll m=(l+h)/2; ll ex=(m*t)/(100+t); if(ex>=n) h=m; else l=m; ++cr; } cout<<l; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; int h, w, m; struct BIT{ private: vector<LL> node; int n; public: BIT(int _n){ n=_n; node.resize(n,0); } void add(int a,LL w){ for(int i=a;i<n;i|=i+1){ node[i]+=w; } } //sum[0,a) LL sum(int a){ LL res=0; for(int i=a-1;i>=0;i=(i&(i+1))-1){ res+=node[i]; } return res; } //[a,b) LL sum(int a,int b){ return sum(b)-sum(a); } }; void solve(){ int h,w; cin>>h>>w; int m; cin>>m; vector<int> x(m),y(m); for(int i=0;i<m;i++){ cin>>x[i]>>y[i]; x[i]--,y[i]--; } int lash=h,lasw=w; for(int i=0;i<m;i++){ if(y[i]==0) lash=min(lash,x[i]); if(x[i]==0) lasw=min(lasw,y[i]); } LL ans=0; vector<int> rih(lash,w); for(int i=0;i<m;i++){ if(x[i]<lash) rih[x[i]]=min(rih[x[i]],y[i]); } vector<int> riw(lasw,h); for(int i=0;i<m;i++){ if(y[i]<lasw) riw[y[i]]=min(riw[y[i]],x[i]); } for(int i=0;i<lash;i++) ans+=rih[i]; for(int i=0;i<lasw;i++) ans+=riw[i]; vector<vector<int>> dels(w+1); for(int i=0;i<lash;i++){ dels[rih[i]].push_back(i); } BIT bt(h); for(int i=0;i<lash;i++) bt.add(i,1); for(int i=0;i<lasw;i++){ for(int d:dels[i]) bt.add(d,-1); ans-=bt.sum(riw[i]); } cout<<ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> #define int long long int #define endl "\n" #define MOD 1000000007 #define mod 1000000007 #define M 1000000007 #define pb push_back #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b] #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define mx 1000005 #define fiint(a,b) memset(a,b,sizeof(a)) #define bitcount __builtin_popcount #define fori(i,a,b) for(int i=a;i<b;i++) #define ford(i,a,b) for(int i=a;i>=b;i--) #define debug(x) cout<<x<<endl; #define cases(t) int t; cin>>t; while(t--) #define inf1 INT_MAX #define all(a) a.begin(),a.end() #define vec vector<int> #define pii pair<int,int> #define plii pair<int,int> #define pint pair<int,int> #define ff first #define ss second #define lb lower_bound #define ub upper_bound #define bs binary_search #define mp make_pair #define sz(x) (int)x.size() #define PI 3.14159265359 //const long long INF=1e18; bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; } int factorial[10000001]={0}; int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } int powerFunction(int x,int y){ int res = 1; int p = mod; x = x; while (y > 0){ if (y & 1) res = (res*x)%p ; y = y>>1; x = (x*x)%p ; } return res; } void factorialFunction(int maxLimit){ factorial[0]=1; for(int i=1 ; i <= maxLimit ; i++) factorial[i]=(factorial[i-1]*i)%MOD; return; } int nCr(int n, int r) { if(r < 0 || r > n){ return 0; } int temp = factorial[n]; temp *= (powerFunction(factorial[r], MOD-2)%MOD); temp %= MOD; temp *= (powerFunction(factorial[n-r], MOD-2)%MOD); temp %= MOD; return temp; } vec tree(8 * 100005); void update(int node , int st , int en , int idx , int v) { if( st == en ) {tree[node] = v; return;} int mid = (st + en)/2; if(idx <= mid) update(node*2, st, mid, idx, v); else update(node*2 + 1 , mid+1,en , idx, v); tree[node] = tree[2*node] + tree[2*node + 1]; } int sum(int node , int st ,int en ,int l ,int r) { if(en < l || r < st ) return 0 ; if(l <= st && r >= en ) return tree[node]; int mid = (st + en)/2; return sum(node*2 , st , mid , l ,r) + sum(node*2 + 1, mid+1, en , l ,r); } signed main() { int h,w,m ; cin>>h>>w>>m; vector<vec> row(h); vector<vec> col(w); vector<pii> obs; for(int i = 1 ; i <= m ;i++) { int x,y ; cin>>x>>y; x-- ; y--; obs.pb(mp(x,y)); } sort(all(obs)); for(int i = 0 ; i < m ;i++) { int x = obs[i].ff ; int y = obs[i].ss; row[x].pb(y); col[y].pb(x); } for(int i = 0 ; i < w ; i++) col[i].pb(h); for(int i = 0 ; i < h ; i++) row[i].pb(w); int ma_r , ma_c ; ma_r = col[0].front(); ma_c = row[0].front(); int ans = 0 ; for(int i = 0 ; i < ma_r;i++) { ans += row[i].front(); } for(int i = 0 ; i < ma_c;i++) { int reach = col[i][0]; ans += max( (int)0 , reach - ma_r ); ans += sum(1, 0, h,0, min(reach ,ma_r )-1 ); for(int x : col[i]) update( 1 , 0 , h, x , 1); } cout << ans<<endl; }
#include <bits/stdc++.h> using namespace std; // For Policy Based DS #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; // --------------------------------------- #define endl "\n" #define ff first #define ss second #define int long long #define pb emplace_back #define mp make_pair #define pll pair<int,int> #define vll vector<int> #define vpll vector<pair<int,int>> #define all(c) (c).begin(), (c).end() #define bs binary_search #define pqb priority_queue<int> #define setbits(x) __builtin_popcountll(x) #define mod 1000000007 #define mem(arr,x) memset(arr,x,sizeof(arr)) #define pi 3.14159265358979323846 #define inf 2000000000000000000 #define ps(x,y) fixed<<setprecision(y)<<x #define test(x) int x; cin>>x;while(x--) template<class A> void read(vector<A>& v); template<class A, size_t S> void read(array<A, S>& a); template<class T> void read(T& x) { cin >> x; } void read(double& d) { string t; read(t); d = stod(t); } void read(long double& d) { string t; read(t); d = stold(t); } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template<class A> void read(vector<A>& x) { for (auto &a : x) read(a); } template<class A, size_t S> void read(array<A, S>& x) { for (auto &a : x) read(a); } const int d4i[4] = { -1, 0, 1, 0}; const int d4j[4] = {0, 1, 0, -1}; const int d8i[8] = { -1, -1, 0, 1, 1, 1, 0, -1}; const int d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1}; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } string int_to_string(int n) { ostringstream str1; str1 << n; string sar = str1.str(); return sar; } int pow(int a, int b, int m) { int res = 1; while (b > 0) { if (b % 2) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; } int ncr(int n, int k) { if (k == 0) return 1; if (k > n - k) k = n - k; int a = 1, b = 1, d; while (k) { a *= n; b *= k; d = __gcd(a, b); a /= d; b /= d; n--; k--; } return a; } int modinv(int k, int m) { return pow(k, m - 2, m); } /***************************CODE BEGINS**************************************/ const int N = 102; double dp[102][102][102]; double cal_dp(int x, int y, int z) { if (x == 100 || y == 100 || z == 100) { return dp[x][y][z] = 0.0; } if (dp[x][y][z] != -1.0) { return dp[x][y][z]; } int temp = x + y + z; double t1 = ((double)x / (double)temp) * (cal_dp(x + 1, y, z) + 1.0); double t2 = ((double)y / (double)temp) * (cal_dp(x, y + 1, z) + 1.0); double t3 = ((double)z / (double)temp) * (cal_dp(x, y, z + 1) + 1.0); return dp[x][y][z] = t1 + t2 + t3; } void solve () { int a, b, c; read(a, b, c); for (int i = 0; i <= 101; i++) { for (int j = 0; j <= 101; j++) { for (int k = 0; k <= 101; k++) { dp[i][j][k] = -1.0; } } } cout << ps(cal_dp(a, b, c), 8); } int32_t main() { c_p_c(); // test(x) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define ff first #define ss second #define pb push_back #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define ps(x) fixed << setprecision(10) << x #define mod 1000000007 #define inf 1e18 #define w(tc) \ int tc; \ cin >> tc; \ for (int i = 1; i <= tc; ++i) #define PI 3.14159265358979323846264338327950L #define span(a) begin(a), end(a) #define ll long long const int N = 100 + 10; double dp[N][N][N]; double calc(int a, int b, int c) { if (a >= 100 or b >= 100 or c >= 100) return 0; double ret = dp[a][b][c]; if (ret >= 0) return dp[a][b][c] = ret; double sum = a + b + c; ret = 1 + a / sum * calc(a + 1, b, c) + b / sum * calc(a, b + 1, c) + c / sum * calc(a, b, c + 1); return dp[a][b][c] = ret; } inline void solve() { memset(dp, -1, sizeof(dp)); int a, b, c; cin >> a >> b >> c; double ans = calc(a, b, c); cout << ps(ans); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //w(tc) solve(); return 0; }
#include <bits/stdc++.h> #define int long long #define pb push_back #define ppb pop_back #define all(x) (x).begin(),(x).end() #define w(x) int x; cin>>x; while(x--) #define fr first #define sc second #define sz(x) (int) ( (x).size() ) #define pii pair<int,int> using namespace std; int INF = 1e18+10 ; int neg = - 1e9; const double epsilon = 1e-6; const int64_t M= 1e9 + 7; const int N =(int)(1e6); //~ using u128 = __uint128_t; // segment tree void cpc(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(NULL); } //~ bool seive[N]; //~ vector<int>v; //~ void seeve() { //~ seive[1] = 1; seive[0] = 1; //~ for(int i = 2; i*i <= N; i++) { //~ if ( seive[i] ) continue; //~ for(int j = i; j*i <=N; j++) { //~ seive[i*j] = 1; //~ } //~ } //~ for(int i = 0; i<(int)(1e5); i++) if ( seive[i] == 0 ) v.push_back(i*i); //~ } int power(long long a, long long n, long long m = M) { a = a % m; long long prod = 1; while(n) { if (n & 1) prod = prod * a % m; a = a * a % m; n/=2; } return prod; } int32_t main(){ cpc(); int n; cin >> n; int ans = 1e18; for(int i = 0; i < n; i++){ int a, b, c; cin >> a >> b >> c; if ( c > a) ans = min(ans, b); } if ( ans == 1e18) cout << "-1\n"; else cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, s, n) for (int i = (s); i <= (n); ++i) using ll = long long; using P = pair<int, int>; const ll MX = 1000000000; int main() { int n; cin >> n; vector<ll> a(n), p(n), x(n); rep(i, n) cin >> a[i] >> p[i] >> x[i]; ll ans = MX; rep(i, n) { int m = x[i] - a[i]; if (m > 0) { ans = min(ans, p[i]); } } if (ans == MX) { cout << -1 << endl; return 0; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; vector<int>T[100005]; int p[100005],d[100005],sz[100005]; int c1[100005],c2[100005]; int cmp(int x,int y){ return c1[x]-c2[x]<c1[y]-c2[y]; } void dfs(int rt){ int l=T[rt].size(); sz[rt]=1; c1[rt]++; if(l==0)return; for(int i=0;i<l;i++){ int v=T[rt][i]; dfs(v); sz[rt]+=sz[v]; } sort(T[rt].begin(),T[rt].end(),cmp); int cnt=0,tot=0;; for(int i=0;i<l;i++){ int v=T[rt][i]; if(sz[v]&1){ if(cnt&1){ c1[rt]+=c2[v]; c2[rt]+=c1[v]; } else{ c1[rt]+=c1[v]; c2[rt]+=c2[v]; } cnt++; } else if(c1[v]<c2[v]){ c1[rt]+=c1[v]; c2[rt]+=c2[v]; } } for(int i=0;i<l;i++){ int v=T[rt][i]; if((!(sz[v]&1)) && c1[v]>=c2[v]){ if(cnt&1){ c1[rt]+=c2[v]; c2[rt]+=c1[v]; } else{ c1[rt]+=c1[v]; c2[rt]+=c2[v]; } } } } int main(){ int n; scanf("%d",&n); for(int i=2;i<=n;i++)scanf("%d",&p[i]); for(int i=2;i<=n;i++)T[p[i]].push_back(i); dfs(1); printf("%d\n",c1[1]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define len(x) int((x).size()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define randint(n) uniform_int_distribution<int>(1, (n))(rng) const int mxn=200100; int link[mxn]; int sz[mxn]; map<int,int> M[mxn]; int C[mxn]; int head(int u) { while(u!=link[u]) u=link[u]; return u; } void join(int a, int b) { a=head(a); b=head(b); if(a==b) return; if(sz[a]<sz[b]) swap(a,b); sz[a]+=sz[b]; link[b]=a; // if(len(M[b])>len(M[a])) swap(M[a],M[b]); for(auto thing:M[b]){ M[a][thing.ff] += thing.ss; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ARTHUR_LOCAL ifstream cin("input.txt"); #endif int n,q; cin>>n>>q; // cout << len(M[200]); for(int i=1; i<=n; i++) cin>>C[i]; for(int i=1; i<=n; i++){ link[i]=i; sz[i]=1; M[i][C[i]] = 1; } while(q--){ int t; cin>>t; if(t==1){ int a,b; cin>>a>>b; join(a,b); } else{ int x,y; cin>>x>>y; cout << M[head(x)][y] << "\n"; } } }
#include <bits/stdc++.h> #define ll long long #define pb push_back #define fr first #define sc second using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); long double a , b , ans; cin >> a >> b; long double x = a - b; ans = (x/a)*100; printf("%llf",ans); return 0; }
#include <bits/stdc++.h> using namespace std ; typedef long long ll ; #define rep(i,n) for(int i = 0 ; i < n ; i++) int a , b ; map<int,int> rem ; int main(){ cin >> a >> b ; for(int i = 1 ; i <= b ; i++){ rem[i] = b / i - (a - 1) / i ; } int res = 1 ; for(int i = 1 ; i <= b ; i++){ if(rem[i] >= 2) res = i ; } cout << res << endl ; }
# include <bits/stdc++.h> typedef long long lint; using namespace std; lint gcd(lint x, lint y) { if(x == 0) { return y; } else { return gcd(y%x,x); } } lint lcm(lint x, lint y) { return x/gcd(x,y)*y; } lint C(lint n, lint k) { if(n == k) { return 1; } else if(n < k) { return 0; } else if(k == 0) { return 1; } else if(k == 1) { return n; } else return C(n-1,k-1) + C(n-1,k); } lint P(lint n, lint k) { if(k == 1) { return n; } return (n*(P(n-1,k-1)%1000000007)%1000000007); } int main() { double a,b; cin >> a >> b; printf("%lf\n", (b/100)*a); return 0; }
///131111129.......... #include<bits/stdc++.h> using namespace std; #define ll long long int #define pi acos(-1) #define pb push_back #define mp make_pair #define rip() for(ll i=0;i<n;i++) #define rip1() for(ll i=n-1;i>=0;i--) #define cin(a) cin>>a #define cout(a) cout<<a #define nl '\n' ///Chess moves......... // int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right //int dy[] = {+1, -1, +0, +0}; // int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move // int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; // int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; ///Knight's Move // int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;} ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);} ll bigmod(ll a,ll b, ll mod) { b%=(mod-1); ll result=1ll; while(b>0ll){ if(b&1ll) result=result*a%mod; a=a*a%mod; b>>=1ll; } return result; } ll inverse(ll a, ll M){ if(gcd(a, M)==1) return bigmod(a, M-2, M) % M; return 1; } ll ncr(ll a, ll b, ll mod){ ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans = ans * K % mod; ans = ans * inverse(L, mod) % mod;} return ans;} ll prime(ll n) { ll cnt=0; while(n%2==0){ n/=2; cnt++; } for(ll i=3;i*i<=n;i+=2){ if(n%i==0){ while(n%i==0){ n/=i; cnt++; } } } if(n>1) cnt++; return cnt; } ll fact(ll n,ll mod) { ll res=1; for(ll i=2;i<=n;i++){ res=(res%mod*i%mod)%mod; } return res; } void urmi() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { urmi(); /*#ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif*/ ll t=1; //cin>>t; while(t--){ ll a,b; cin>>a>>b; a*=b; double c=a/(100.0); cout<<fixed<<setprecision(10)<<c<<endl; } }
#include <bits/stdc++.h> #define rep(i, e, n) for (int i = e; i < (n); ++i) using namespace std; typedef long long ll; using P = pair<int,int>; const ll inf=1000000000007; const int mod=1000000007; int main() { char s; cin >> s; char t; cin >> t; if(s=='Y'){ t=toupper(t); } cout << t << endl; return 0; }
#include <bits/stdc++.h> #define N 100005 #define SpeedCode ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); using namespace std; int a; int main(){ SpeedCode; cin>>a; cout<<a-1; }
#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++) #define PI 3.14159265359 const long long INF = 1LL << 60; long long MOD = 1000000007; long long gcd(long long a, long long b) { return b ? gcd(b, a%b) : a; } long long lcm (int a, int b){return (long long) a*b /gcd(a,b);} double radian(double degree){return degree * PI /180.0;} double degree(double radian){return radian * 180.0/PI;} int main(){ string s; cin >> s; cout << s[1] << s[2] << s[0] << endl; }
#include<bits/stdc++.h> using namespace std; int main() { string S; cin >> S; string ans; ans += S.at(1); ans += S.at(2); ans += S.at(0); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < b; ++i) int debug = 0; const int N = 2010, inf = 1e9; int n, m, ans, dp[N][N]; vector <int> g[26][N]; main() { cin >> n >> m; rep(i, 1, n + 1) { rep(j, 1, n + 1) { if (i == j) { dp[i][j] = 0; } else { dp[i][j] = inf; } } } rep(i, 0, m) { int a, b, c; char cc; cin >> a >> b >> cc; c = cc - 'a'; if (debug) { cerr << " edge: " << a << ' ' << b << ' ' << cc << endl; } g[c][a].push_back(b); g[c][b].push_back(a); if (a != b) dp[a][b] = dp[b][a] = 1; } set <array <int, 3>> d; rep(i, 1, n + 1) rep(j, i, n + 1) { d.insert({dp[i][j], i, j}); } while (!d.empty()) { auto cur = *d.begin(); d.erase(cur); int w = cur[0], a = cur[1], b = cur[2]; if (debug) cerr << w << ' ' << a << ' ' << b << '\n'; assert(a <= b); if (w == inf) break; rep(c, 0, 26) { for (int u : g[c][a]) for (int v : g[c][b]) { int uu = u, vv = v; if (uu > vv) swap(uu, vv); if (dp[uu][vv] > w + 2) { d.erase({dp[uu][vv], uu, vv}); dp[uu][vv] = w + 2; d.insert({dp[uu][vv], uu, vv}); } } } } if (dp[1][n] == inf) { ans = -1; } else { ans = dp[1][n]; } cout << ans << '\n'; }
#include <bits/stdc++.h> #define fi first #define se second using namespace std; int st[100][26]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,ex,ey; cin >> n >> m; char mt[n+1][m+1]; queue<pair<int,int> >q; vector<pair<int,int> >p[26]; int dst[n+1][m+1]; memset(dst,-1,sizeof(dst)); for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ cin >> mt[i][j]; if(mt[i][j]=='S'){ q.emplace(i,j); dst[i][j]=0; } if(mt[i][j]=='G'){ ex = i; ey = j; } if(mt[i][j]>='a'&&mt[i][j]<='z'){ p[mt[i][j]-'a'].emplace_back(i,j); } } } int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; while(!q.empty()){ int x=q.front().fi; int y=q.front().se; q.pop(); for(int i=0;i<4;++i){ int nx=x+dx[i]; int ny=y+dy[i]; if(nx<0||nx>=n||ny<0||ny>=m||mt[nx][ny]=='#')continue; if(~dst[nx][ny])continue; dst[nx][ny]=dst[x][y]+1; q.emplace(nx,ny); } if(mt[x][y]>='a'&&mt[x][y]<='z'){ for(auto po : p[mt[x][y]-'a']){ if(~dst[po.fi][po.se])continue; dst[po.fi][po.se]=dst[x][y]+1; q.emplace(po.fi,po.se); } p[mt[x][y]-'a'].clear(); } } // cerr<<dst; cout<<dst[ex][ey]<<"\n"; }
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned long long #define pi 2*acos(0.0) #define readl(v,n) for(ll i=0;i<n;i++) {ll val; cin>>val; v.pb(val);} #define readi(v,n) for(int i=0;i<n;i++) {int val; cin>>val; v.pb(val);} #define srt(v) sort(v.begin(), v.end()); #define rsrt(v) sort(v.rbegin(), v.rend()); #define MIN(v) *min_element(v.begin(), v.end()) #define MAX(v) *max_element(v.begin(), v.end()) #define sz(x) ((ll) (x).size()) #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for(ll i = (a); i < (b); i++) #define rep2(i, a, b) for(ll i = (a); i <= (b); i++) #define vll vector <ll> #define vii vector <int> #define pii pair <int, int> #define pll pair <ll, ll> #define M 1000007 #define MOD 1000000007 #define pb push_back #define mp make_pair #define ff first #define ss second #define endl "\n" #define inf 1000000000000000000 #define ninf -1000000000000000000 #define deb(n) cout<<"--"<<(n)<<"--"<<endl #define debx(x) cout << #x << " = " << (x) << endl #define deb2(a,b) cout<<(a)<<"----"<<(b)<<endl #define debv(v) for(auto x: v) {cout<<x<<", ";} cout<<endl; #define put(n) cout<<(n)<<endl #define toDecimal(s) bitset<64>(s).to_ullong() #define toBinaryStr(n) bitset<64>(n).to_string() #define toBinary(n) stoll(bitset<64>(n).to_string()) /* ---- take notes here ---- */ void solve() { ll n; cin>>n; ll sum=0; for(ll i=0; i<n; i++) { ll val; cin>>val; if(val>10) { sum+=val-10; } } cout<<sum<<endl; } int main() { FAST int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[n+1]; int ans=0; for(int i=1;i<=n;i++){ cin>>a[i]; if(a[i]>10) ans+=a[i]-10; } cout<<ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout<<setprecision(10)<<fixed; int a,b,c,d; cin>>a>>b>>c>>d; cout<<(a*d-b*c)<<'\n'; }
#include<bits/stdc++.h> #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define Freed freopen ("0in.txt","r",stdin); #define Fout freopen ("0out.txt","w",stdout); #define ll long long int #define pb push_back #define mp make_pair #define pi acos(-1.0) #define Mod 1000000007 #define limit 1000008 using namespace std; void solve(ll t) { ll i,j,n,m,k; ll a[5]; for(i=0; i<4; i++) cin >> a[i]; sort(a,a+4); cout <<a[0]<<endl; return ; } int main() { // Fast // Freed // Fout ll t,tt=1; // cin >> tt; for(t=1; t<=tt; t++) solve(t); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; template<typename T> using V = vector<T>; template<typename T> using P = pair<T, T>; template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); } template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); } template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;} template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());} template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++) {cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}}; template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0]; for(ll i=1;i<n;i++)cerr spa v[i]; cerr<<"\n";}; const ll INF = (1ll<<62); // const ld EPS = 1e-10; // const ld PI = acos(-1.0); const ll mod = (int)1e9 + 7; //const ll mod = 998244353; struct edge{ ll to, idx; }; using Graph = V<V<edge>>; int main(){ ll N, M; cin >> N >> M; Graph G(N); V<ll> a(M), b(M); REP(i, M) cin >> a[i] >> b[i], a[i]--, b[i]--; REP(i, M){ G[a[i]].push_back(edge{b[i], i}); G[b[i]].push_back(edge{a[i], i}); } V<ll> c(N); REP(i, N) cin >> c[i]; V<ll> res(M, -1); REP(i, M){ if(res[i] == -1){ if(c[a[i]] > c[b[i]]){ res[i] = 1; }else if(c[a[i]] < c[b[i]]){ res[i] = 0; } } } V<ll> ids(N, -1); ll cnt = 0; auto dfs = [&](auto self, ll now, ll par)->void{ if(ids[now] != -1) return; ids[now] = cnt; cnt++; for(auto &e: G[now]){ if(e.to == par) continue; if(res[e.idx] != -1) continue; if(ids[e.to] == -1){ if(now == a[e.idx]) res[e.idx] = 1; else res[e.idx] = 0; self(self, e.to, now); }else{ dump(now) if(now == a[e.idx]) res[e.idx] = 1; else res[e.idx] = 0; } } return; }; REP(i, N) dfs(dfs, i, -1); // dump(ids) REP(i, M){ assert(res[i] != -1); cout << (res[i] == 0 ? "<-" : "->") << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstdlib> #include <cstring> #include <cmath> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <complex> #include <vector> #include <limits> #include <iomanip> #include <cassert> #include <numeric> #include <chrono> #include <random> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define debug(x) cout << #x << " = " << (x) << endl; #define rep(i, n) for(int i = 0;i < n;i++) #define repr(i, n) for(int i = n-1;i >= 0;i--) #define pb push_back #define mp make_pair #define F first #define S second const long double pi = 3.141592653589793; const int mod = 1e9 + 7; int N, L; int A[1<<20], B[1<<20]; vector<int> dA, dB; void solve() { cin>>N>>L; rep(i,N)cin>>A[i]; rep(i,N)cin>>B[i]; dA.pb(A[0]-1); dB.pb(B[0]-1); for(int i = 1;i < N;i++) { dA.pb(A[i]-A[i-1]-1); dB.pb(B[i]-B[i-1]-1); } dA.pb(L-A[N-1]); dB.pb(L-B[N-1]); // for(auto x:dA)cout<<x<<" ";cout<<endl; // for(auto x:dB)cout<<x<<" ";cout<<endl; int sj = 0;ll ops = 0; for(int i = 0;i < dB.size();i++) { if(dB[i] == 0) continue; while(sj < dA.size() && dA[sj] == 0) sj++; int sum = 0, ej = sj; while (ej < dA.size() && sum < dB[i]) { sum += dA[ej++]; } if(sum != dB[i]) { cout << -1 << endl; return; } int a = sj, b = ej-1, c = i; ops += max(b,c)-min(a,c); sj = ej; } cout << ops << endl; } int main() { // freopen("input.in","r",stdin); // freopen("output.out","w",stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); int t=1;//cin>>t; while (t--) { solve(); } return 0; }
#include <iostream> #include <string> #include <iomanip> #include <algorithm> #include <cassert> #include <numeric> #include <queue> #include <set> #include <vector> #include <cmath> #include <bitset> #include <functional> #include <limits> #include <map> using namespace std; typedef long long ll; #define rep2(i, n) for(ll i = 2; i <=(ll)(n); i++) #define rep1(i, n) for(ll i = 1; i <=(ll)(n); i++) #define rep0(i, n) for(ll i = 0; i <=(ll)(n); i++) 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; } ll pow(ll a, ll n) { //compute a^n with o(logn) ll res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } ll pow(ll a, ll n, ll mod) { //compute a^n (mod mod) with o(logn) ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } const int MAX = 199; const int MOD = 1000000007; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll 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; } //順列計算 ll PER(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[n - k] % MOD) % MOD; } int main(){ ll a,b,c; cin >> a >> b >> c; if(a*a + b*b < c*c) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long int #define double long double #define pb push_back #define fi first #define se second #define vi vector<int> #define pii pair<int,int> #define vpii vector<pair<int,int>> #define pqi priority_queue<int> #define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++) #define inp(a,n,f) vector<int> a(n+f);for(int hh=f;hh<n+f;hh++)cin>>a[hh]; #define printdecimal(k) cout<<fixed<<setprecision(k); #define mem(a,k) memset(a,k,sizeof(a)) #define ub upper_bound #define lb lower_bound #define all(v) v.begin(),v.end() #define mod (int)(1e9+7) #define inf LLONG_MAX #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> int exp(int x,int y){int val=1;x=x%mod;while(y>0){if(y&1)val=(val*x)%mod;y=y>>1;x=(x*x)%mod;}return val;} int modinv(int x){return exp(x,mod-2);} int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;} int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;} int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;} signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int a,b,c; cin>>a>>b>>c; cout<<(a*a+b*b<c*c?"Yes":"No"); return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> using ll = long long; using namespace std; template<typename A, typename B> bool chmin(A &a, const B b) { if (a <= b) return false; a = b; return true; } template<typename A, typename B> bool chmax(A &a, const B b) { if (a >= b) return false; a = b; return true; } #ifndef LOCAL #define debug(...) ; #else #define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl; template<typename T> ostream &operator<<(ostream &out, const vector<T> &v); template<typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template<typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } void _tostr_rec(ostringstream &oss) { oss << "\b\b \b"; } template<typename Head, typename... Tail> void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) { oss << head << ", "; _tostr_rec(oss, forward<Tail>(tail)...); } template<typename... T> string _tostr(T &&...args) { ostringstream oss; int size = sizeof...(args); if (size > 1) oss << "{"; _tostr_rec(oss, forward<T>(args)...); if (size > 1) oss << "}"; return oss.str(); } #endif constexpr int mod = 1'000'000'007; //1e9+7(prime number) constexpr int INF = 1'000'000'000; //1e9 constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18 constexpr int SIZE = 200010; /* Binary Indexed Tree(0-index) */ template<typename Type = int> struct BIT { int N; vector<Type> data; BIT(int n): N(n), data(n + 1, 0) {} //add x to kth value void add(int k, Type x) { debug(k, x); for (k++; k <= N; k += k & -k) data[k] += x; } //sum of [0,k) Type query(int k) const { Type rec = 0; for (k = min(k, N); k; k -= k & -k) rec += data[k]; return rec; } //sum of [a, b) Type query(int a, int b) const { return a < b ? query(b) - query(a) : 0; } }; vector<int> G[SIZE]; vector<pair<int, int>> query[SIZE]; int in[SIZE], out[SIZE]; int id = 0; vector<int> dNode[SIZE]; void dfs(int now, int d = 0) { in[now] = id++; dNode[d].push_back(now); for (int to : G[now]) { dfs(to, d + 1); } out[now] = id++; } int main() { int N, Q; scanf("%d", &N); for (int i = 0; i < N - 1; i++) { int p; scanf("%d", &p); G[p - 1].push_back(i + 1); } dfs(0); scanf("%d", &Q); for (int i = 0; i < Q; i++) { int U, D; scanf("%d%d", &U, &D); query[D].push_back({i, U - 1}); } debug(id); BIT<int> bit(N * 2); vector<int> ans(Q); for (int D = 0; D < N; D++) { for (int n : dNode[D]) { bit.add(in[n], 1); } for (auto p : query[D]) { debug(p, in[p.second], out[p.second]); ans[p.first] = bit.query(in[p.second], out[p.second]); debug(ans[p.first]); } for (int n : dNode[D]) { bit.add(in[n], -1); } } for (int v : ans) { printf("%d\n", v); } return 0; }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 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> ostream &operator<<(ostream &os, const vector<T> &x){ os << "{"; for(size_t i = 0; i < x.size(); i++){ if(i < x.size()-1) os << x[i] << ", "; else os << x[i]; } os << "}"; return os; } int main(void){ int n; cin >> n; vector<vector<int>> v(n); rep(i, n-1){ int p; cin >> p; p--; v[p].push_back(i+1); } vector<int> d(n); vector<vector<int>> in(n); vector<pair<int, int>> a(n); int idx = 0; function<void(int x)> dfs = [&](int x){ a[x].first = idx++; in[d[x]].push_back(a[x].first); for(auto to : v[x]){ d[to] = d[x]+1; dfs(to); } a[x].second = idx++; }; dfs(0); for(auto &in : in) sort(in.begin(), in.end()); int Q; cin >> Q; rep(q, Q){ int U, D; cin >> U >> D; U--; auto [inx, outx] = a[U]; int ans = lower_bound(in[D].begin(), in[D].end(), outx) - lower_bound(in[D].begin(), in[D].end(), inx); cout << ans << endl; } return 0; }
/*ver 7*/ #include <bits/stdc++.h> using namespace std; void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);} using ll = long long; using ld = long double; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vb = vector<bool>; using vvl = vector<vector<ll>>; using vvd = vector<vector<ld>>; using vvs = vector<vector<string>>; using vvb = vector<vector<bool>>; using pll = pair<ll,ll>; using mll = map<ll,ll>; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; #define each(x,v) for(auto& x : v) #define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++) #define rep(i,n) for(ll i=0;i<(ll)n;i++) #define pb push_back #define eb emplace_back #define fi first #define se second #define mp make_pair const ll INF = 1LL << 60; #define CLR(mat,f) memset(mat, f, sizeof(mat)) #define IN(a, b, x) (a<=x&&x<=b) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除 #define debug cout << "line : " << __LINE__ << " debug" << endl; #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ind(...) long double __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}} template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}} template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}} template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}} template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}} void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);} void die(){cout << endl;exit(0);} template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);} template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;} template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}} template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}} template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}} template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}} template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}} #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;} ll ceilDiv(ll a,ll b) {return (a+b-1)/b;} ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離 ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } #define YES(n) ((n) ? "YES" : "NO" ) #define Yes(n) ((n) ? "Yes" : "No" ) #define yes(n) ((n) ? "yes" : "no" ) int main(){ init(); inl(b,c); ll ans=0; bool flag=false; if(b>0){ ans+=min(b,c/2+1); if(b<=c/2)flag=true; ans+=min(b,(c-1)/2+1); c--; ans+=(c/2)+(c-1)/2; out(ans+flag); }else if(b==0){ ans+=c/2+1; ans+=(c-1)/2; out(ans); }else{ ans+=c/2+1; ans+=(c-1)/2+1; ans+=min((c-1)/2,(-b)*2-1); out(ans); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll b,c,ans; ll solve1(ll b,ll c){ ll depth=c/2,flip=c&1; if(depth<b)return (1+depth)*2-(!flip); else return b*2+1; } ll solve2(ll b,ll c){ ll depth=c/2,flip=c&1; return max(ll(0),depth*2-(!flip)); } int main(){ scanf("%lld%lld",&b,&c); if(b>0)ans=solve1(b,c)+solve2(-b,c-1); else if(b==0)ans=1+solve2(b,c); else ans=solve2(b,c)+solve1(-b,c-1); printf("%lld",ans); return 0; }
/* Great things never come from comfort zones, "whatever the mind of a man can conceive and believe,it can achieve." */ #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; // using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>; void my_dbg() { cout << endl; } template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B) { cout << ' ' << A; my_dbg(B...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__) #define ll long long #define dd long double #define scf(n) scanf("%d",&n) #define lscf(n) scanf("%lld",&n) #define lpri(n) printf("%lld ",n) #define pri(n) printf("%d ",(int)n) #define prin(n) printf("%d\n",(int)n) #define lprin(n) printf("%lld\n",n) #define rep(i,ini,n) for(int i=ini;i<(int)n;i++) #define show(a) for(auto xy: a) pri(xy); printf("\n"); #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(),x.end() #define SET(a,b) memset(a,b,sizeof(a)) #define tc int tt; scf(tt); while(tt--) #define inf INT_MAX #define ninf INT_MIN #define gcd __gcd #define bitcount(n) __builtin_popcount(n) const int mod=1e9+7; const int N = 1e6+7; int main() { ll x; lscf(x); while(x%2==0) x/=2; vector<ll>v; int c=0; for(ll i=1;i*i<=x;i++) { if(x%i==0) { if(x!=i*i) c+=2; else c++; } } prin(2*c); }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define sz(a) ((int)a.size()) #define re return #define all(a) a.begin(),a.end() #define int long long #define Type int #define rept(i,a,b) for(int i=(a);i<(b);i++) #define rep(i,a) rept(i,0,a) using namespace std; const int dx[4]={-1,1,0,0}; const int dy[4]={0,0,-1,1}; int a[100005]; int b[100005]; int n,ans; signed main() { cin>>n; rep(i,n){ cin>>a[i]; } rep(i,n){ cin>>b[i]; ans+=a[i]*b[i]; } if (ans==0) cout<<"Yes"; else cout<<"No"; re 0; }
#include<bits/stdc++.h> #define int long long using namespace std; #define rep(i, n) for(int i=0;i<(n);++i) typedef pair<int, int> pii; const int INF = 1l << 60; int N; int P[200200]; int F[200200]; int ans[200200]; signed main() { memset(F, -1, sizeof(F)); cin >> N; rep(i, N) { cin >> P[i]; if (F[P[i]] == -1)F[P[i]] = i; } int i = 0, k = 0; while (i < N) { while (i < F[k] || F[k] == -1) { ans[i] = k; ++i; if (i == N)break; } ++k; } rep(i, N) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> prime_factor(int n) { vector<pair<int, int>> ret; for (int i = 2; i * i <= n; i++) { if (n % i != 0) continue; int tmp = 0; while (n % i == 0) { tmp++; n /= i; } ret.push_back(make_pair(i, tmp)); } if (n != 1) ret.push_back(make_pair(n, 1)); return ret; } int main(void){ int N; cin >> N; long long ans = 1; int data[N+1]; for(int i=2;i<=N;i++){ data[i] = i; } for(int i=2;i<N;i++){ vector<pair<int, int>> v = prime_factor(i); for(int j=0;j<v.size();j++){ for(int k=0;k<v[j].second;k++){ for(int l=i+1;l<=N;l++){ if(data[l] % v[j].first == 0){ data[l] /= v[j].first; break; } } } } } for(int i=2;i<=N;i++){ ans *= data[i]; } ans++; cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define endl "\n" #define pb push_back typedef long long ll; const double pi = acos(-1.0); #define debug puts("*****************") #define memset(a,b) memset(a,b,sizeof(a)) const int mod = (1 ? 1000000007 : 998244353); const int inf = (1 ? 0x3f3f3f3f : 0x7fffffff); const int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 }; const ll INF = (1 ? 0x3f3f3f3f3f3f3f3f : 0x7fffffffffffffff); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll poww(ll a, ll b,ll mod) { ll s = 1; while (b) { if (b & 1)s = (s * a) % mod; a = (a * a) % mod; b >>= 1; }return s % mod; } /*----------------------------------------------------------------------------------------------------------------------*/ const int N = 1e5 + 11; ll n,m; void solve() { cin>>n>>m; ll q=poww(10,n,m*m); cout<<q/m<<endl; } /*----------------------------------------------------------------------------------------------------------------------*/ int main() { ios::sync_with_stdio(false); cin.tie(0); ll T = 1; //cin >> T; while (T--)solve(); return 0; }
#include<bits/stdc++.h> #define int long long typedef long long ll; using namespace std; //template<class T>T ABS(T x) {return x<0?-x:x;} //int ksm(int x,int y) {int r=1;while(y) y&1?r=1ll*r*x%mod:0,x=1ll*x*x%mod,y>>=1;return r;} int read() { int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*w; } const int maxn = 1e5+10; //const int mod = ; //struct Edge{int to,next;}a[maxn];int h[maxn],cnt; //void add(int x,int y) {a[++cnt]=(Edge){y,h[x]},h[x]=cnt,a[++cnt]=(Edge){x,h[y]},h[y]=cnt;} //vector<int>G[maxn]; //void add(int x,int y) {G[x].push_back(y),G[y].push_back(x);} int A,B,K; int C(int x,int y) { int ans=1; for(int i=1;i<=y;i++) ans=ans*(x-i+1)/i; return ans; } signed main() { A=read(),B=read(),K=read(); for(int i=1,now=A+B;i<=now;i++) { int tt=C(A+B-1,A-1); if(!A) cout<<'b',--B; else if(!B) cout<<'a',--A; else if(K<=tt) cout<<'a',--A; else K-=tt,cout<<'b',--B; } return 0; }
#include<bits/stdc++.h> #define rint register int #define ll long long using namespace std; char S[501000],T[500100]; int N; queue<int> q; int main(){ scanf("%d",&N); scanf("%s",S+1); scanf("%s",T+1); int las=-1; ll ans=0; for(rint i=1;i<=N;++i) { if(T[i]=='1') { q.push(i); } if(S[i]=='1') { if(las!=-1) { ans+=i-las; las=-1; } else { if(!q.empty()) {ans+=i-q.front();q.pop();} else las=i; } } } if((!q.empty())||(las!=-1)) { cout<<-1<<"\n"; } else cout<<ans<<"\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; #if DEBUG && !ONLINE_JUDGE ifstream input_from_file("input.txt"); #define cin input_from_file #else #endif int N, L; vector<LL> A, B; LL goingleft(int l, int r) { if (B[l] != A[l]) return -1; if (l == r) return 0; if (A[r] == B[r]) return goingleft(l, r-1); int n = r; while (n > l and A[n] + r-n > B[r]) n--; if (A[n] + r-n != B[r]) return -1; int m = r; while (m > l and A[n] + m-n == B[m]) m--; LL newres = goingleft(l, m); if (newres == -1) return -1; return newres + r-n; } LL goingright(int l, int r) { if (B[r] != A[r]) return -1; if (l == r) return 0; if (A[l] == B[l]) return goingright(l+1, r); int n = l; while (n < r and A[n] + l-n < B[l]) n++; if (A[n] + l-n != B[l]) return -1; int m = l; while (m < r and A[n] + m-n == B[m]) m++; LL newres = goingright(m, r); if (newres == -1) return -1; return newres + n-l; } LL result(int n) { //~ cout << n << endl; if (n == 0) return 0; if (B[n] == A[n]) return result(n-1); if (B[n] > A[n]) { int m = n; while (B[m] > A[m]) m--; LL res1 = result(m); LL res2 = goingright(m+1, n+1); //~ cout << "going right " << m+1 << " " << n << " " << res1 << " " << res2 << endl; if (res1 == -1 or res2 == -1) return -1; return res1 + res2; } int m = n; while (B[m] < A[m]) m--; LL res1 = result(m); LL res2 = goingleft(m, n); if (res1 == -1 or res2 == -1) return -1; //~ cout << "going left " << m+1 << " " << n << " " << res1 << " " << res2 << endl; return res1 + res2; } int main() { ios::sync_with_stdio(false); cin.tie(0); // Togliere nei problemi con query online cin >> N >> L; A.resize(N+2), B.resize(N+2); A[0] = 0, B[0] = 0; A[N+1] = L+1, B[N+1] = L+1; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= N; i++) cin >> B[i]; cout << result(N) << "\n"; }
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i, l, r) for (int i = l; i <= r; ++i) #define dep(i, r, l) for (int i = r; i >= l; --i) void read (int &x) { x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); } const int N = 2e6 + 10, mod = 1e9 + 7; int n, m, k, fac[N], inv[N]; int C (int n, int m) { if (n < m) return 0; if (m == 0) return 1; return fac[n] * inv[m] % mod * inv[n - m] % mod; } int Pow (int a, int k) { if (k == 1) return a; int S = Pow (a, k >> 1); if (k & 1) return S * S % mod * a % mod; else return S * S % mod; } signed main () { fac[0] = 1; rep(i, 1, N - 1) fac[i] = fac[i - 1] * i % mod; inv[N - 1] = Pow(fac[N - 1], mod - 2); dep(i, N - 2, 0) inv[i] = inv[i + 1] * (i + 1) % mod; read(n), read(m), read(k); if (m == 0 && k < n || m + k < n) puts("0"); else printf("%lld", (C(n + m, n) - C(n + m, m + k + 1) + mod) % mod); return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" #define int long long void solve(){ int n,m;cin>>n>>m; vector<int>A(n); vector<int>B(m); for(int i=0;i<n;i++)cin>>A[i]; for(int i=0;i<m;i++)cin>>B[i]; int dp[n+1][m+1]={0}; for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ if (i==0&&j==0){ dp[i][j]=0; continue; } dp[i][j]=1e18; if (i>0)dp[i][j]=min(dp[i][j],dp[i-1][j]+1); if (j>0)dp[i][j]=min(dp[i][j],dp[i][j-1]+1); if (i>0&&j>0)dp[i][j]=min(dp[i][j],dp[i-1][j-1]+(A[i-1]!=B[j-1]?1:0)); } } cout<<dp[n][m]<<endl; } int32_t main(){ IOS; int t=1; //cin>>t; while (t--)solve(); return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; int a,b; void solve(){ cin>>a>>b; int c=b-a; while(true){ if(a%c==0||b%c==0)break; if((b/c)-(a/c)>=2)break; c--; } cout<<c<<'\n'; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); solve(); }
//@Author: KeinYukiyoshi // clang-format off #include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC target("avx") #define int long long using namespace std; #define stoi stoll #define fi first #define se second #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++) #define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++) #define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--) #define FOR(i, a) for (auto &i: a) #define ALL(obj) begin(obj), end(obj) #define _max(x) *max_element(ALL(x)) #define _min(x) *min_element(ALL(x)) #define _sum(x) accumulate(ALL(x), 0LL) const int MOD = 1000000007; // const int MOD = 998244353; // const int INF = (int)1e18; // const int INF = 10000000000007; // 1e13 + 7 // const int INF = LLONG_MAX; // 9.2e18 const double EPS = 1e-8; const double PI = 3.14159265358979; template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T> using VVV = vector<vector<vector<T>>>; template <class T, class S> using P = pair<T, S>; 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;} int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } template<class T> T chmod(T &a, T mod=MOD) {a = a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;}; int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));} int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;} struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // clang-format on vector<int> divisor; void enumerate_divisor(int n) { divisor = {}; for (int i = 1; i <= (int)sqrt(n); i++) { if (n % i == 0) { divisor.emplace_back(i); if (i * i != n) divisor.emplace_back(n / i); } } sort(begin(divisor), end(divisor)); } class CMaxGCD2 { public: static void solve(istream &cin, ostream &cout) { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int A, B; cin >> A >> B; V<int> yaku(300000 + 100); rep2(i, A, B + 1) { enumerate_divisor(i); FOR(j, divisor) yaku[j]++; } rep3(i, 300000 + 10, 1) { if (yaku[i] > 1) { cout << i << endl; return; } } } }; signed main() { CMaxGCD2 solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include<bits/stdc++.h> #define ll long long #define inf 999999999999999 #define ull unsigned long long #define F first #define S second #define eb emplace_back #define mpp make_pair #define P(x) cout<<__LINE__ <<": "<<#x<<' '<<(x)<<'\n' #define dbg cout<<__LINE__ <<": 0manush\n" #define read(x) freopen("input.txt","r",stdin); #define write(x) freopen("aoutput.txt","w",stdout) #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define pi 2*acos(0.0) #define pi2 M_PI #define lcm(a,b) ((a)*((b)/__gcd(a,b))) #define mem(a,b) memset(a,b,sizeof(a)) #define ee exp(1) #define mx 100007 #define mod 10000000000 #define enddl cout<<endl; using namespace std; int main(){//read(x); //fast; ll a,b,c,d,r,e,g,h,i,j,k,l,n,t,z=0,p,q; cin>>a>>b; if(a==b)cout<<b<<endl; else{ if(a==0 && b==1)cout<<2<<endl; if(a==0 && b==2)cout<<1<<endl; if(a==1 && b==2)cout<<0<<endl; if(a==1 && b==0)cout<<2<<endl; if(a==2 && b==0)cout<<1<<endl; if(a==2 && b==1)cout<<0<<endl; } }
#include <iostream> #include <string> #include <algorithm> #include <cmath> #include <vector> #include <iomanip> using ll = long long; using namespace std; int main(){ int x,y; cin >> x >> y; if(x == y) cout << x; else cout << 3-x-y; }
#include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; using ll =long long; typedef pair<int,int> P; #define SORT(a) sort((a).begin(),(a).end()) #define REV(a) reverse((a).begin(),(a).end()) #define For(i, a, b) for(int i = (a) ; i < (b) ; ++i) #define rep(i, n) For(i, 0, n) #define debug(x) cerr << #x << " = " << (x) << endl; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } void coY() {cout <<"Yes"<<endl;} void coN(){cout <<"No"<<endl;} //Write From this Line const int dy[] = {0,0,1,-1}; const int dx[] = {1,-1,0,0}; const ll mod = 1e9+7; //const ll mod = 998244353; const double PI=3.14159265358979323846; const int INF = 1001001001; const int MAX = 4000005; int main() { ll h,w ; cin >> h >>w ; vector<string> s(h); rep(i, h)cin>>s[i]; ll k = 0; rep(i,h){ rep(j,w){ if(s[i][j] == '.')k++; } } vector<ll> k_n(MAX); k_n[0] = 1; For(i,1,MAX){ k_n[i] = (k_n[i-1] * 2)%mod; } // 全てのマスについて,左右に何個,上下に何個ある.っていうのを数え上げしていく vector<vector<ll>> gyo(h,vector<ll>(w,0)); vector<vector<ll>> retu(h,vector<ll>(w,0)); rep(i,h){ // 行ごとに数える int count = 0; rep(j,w){ if(s[i][j] == '.'){ gyo[i][j] = ++count; } else { gyo[i][j] = 0; count = 0; } } } rep(i,h){ for(int j = w - 2; j >= 0; j--){ if(s[i][j] == '#') continue; if(s[i][j+1] == '#')continue; gyo[i][j] = gyo[i][j+1]; } } rep(i,w){ int count = 0; rep(j,h){ if(s[j][i] == '.'){ retu[j][i] = ++count; } else { retu[j][i] = 0; count = 0; } } } rep(i,w){ for(int j =h - 2; j >= 0; j--){ if(s[j][i] == '#') continue; if(s[j+1][i] == '#')continue; retu[j][i] = retu[j+1][i]; } } ll ans = 0; ans = k * k_n[k]; ans %= mod; rep(i,h){ rep(j,w){ if(s[i][j] == '#') continue; ll x = gyo[i][j]; ll y = retu[i][j]; // そのマスが照らされない場合っていうのは,2^(k-(x+y))通り ll ind = k - (x+y-1); ll now = k_n[ind]; ans -= now; while(ans < 0) ans += mod; } } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define int long long const int M=998244353; const int inf=4e18; const int N=300; int fact[N+1]; int power(int x,int y) { if(y<0) return power(x,M-2)*power(x,y+1)%M; y%=(M-1); int res=1; while(y>0) { if(y&1) res=res*x%M; y=y>>1; x=x*x%M; } return res; } int nCr(int n,int r) { if(r>n||r<0) return 0; if(r==0) return 1; return(fact[n]*power(fact[r],M-2)%M*power(fact[n-r],M-2)%M)%M; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,k;cin>>n>>k; int a[n+1]; for(int i=1;i<=n;i++) cin>>a[i]; int po[n+1][k+1]; for(int i=1;i<=n;i++) { po[i][0]=1; for(int j=1;j<=k;j++) po[i][j]=po[i][j-1]*a[i]%M; } int f[k+1];f[0]=n%M; for(int i=1;i<=k;i++) { f[i]=0; for(int j=1;j<=n;j++) f[i]=(f[i]+po[j][i])%M; } fact[0]=1; for(int i=1;i<=N;i++) fact[i]=fact[i-1]*i%M; for(int x=1;x<=k;x++) { int o=0; for(int p=0;p<=x;p++) o=(o+nCr(x,p)*(f[x-p]%M*f[p]%M-f[x]%M+M)%M*power(2,-1)%M)%M; cout<<o<<'\n'; } }
#include<bits/stdc++.h> using namespace std; #define int long long signed main(){ int n; cin >> n; vector<int> a(n), b(n); for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> b[i]; vector<int> c(n); int ma = 0; for(int i = 0; i < n; i++){ c[i] = max(ma, a[i]); ma = max(ma, a[i]); } int max_ = a[0] * b[0]; for(int i = 0; i < n; i++){ cout << (long long)(max(max_, b[i] * c[i])) << endl; max_ = max(max_, b[i] * c[i]); } }
#define LOCAL #include <bits/stdc++.h> #define fi first #define se second #define ins insert #define pb push_back #define eb emplace_back #define sz(x) ((int)x.size()) #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; // template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } 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) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define DB(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define DB(...) 42 #endif int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<ll> yi(n), er(n); for (auto& i : yi) cin >> i; for (auto& i : er) cin >> i; vector<ll> c(n); ll mx = 0; for (int i = 0; i < n; ++i) { if (i == 0) { c[0] = yi[0] * er[0]; mx = max(mx, yi[0]); } else { c[i] = max(max(yi[i] * er[i], er[i] * mx), c[i - 1]); mx = max(mx, yi[i]); } } for (auto& i : c) { cout << i << '\n'; } cout << endl; string sss; while (cin >> sss); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <limits> #include <queue> #include <string> #define int long long int const N = 100000; int const INF = std::numeric_limits<int>::max(); int const NINF = std::numeric_limits<int>::min(); int const MOD = 1e9 + 7; int numbers[N + 1]; int dp[N + 1][2]; int cnt[N + 1][2]; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n; std::cin >> n; for (int i = 1; i <= n; i++) { std::cin >> numbers[i]; } cnt[1][1] = 1; dp[1][1] = numbers[1]; for (int i = 2; i <= n; i++) { cnt[i][0] = cnt[i - 1][1] % MOD; cnt[i][1] = (cnt[i - 1][1] + cnt[i - 1][0]) % MOD; dp[i][0] = (dp[i - 1][1] - cnt[i][0] * numbers[i] + MOD) % MOD; dp[i][1] = (dp[i - 1][1] + dp[i - 1][0] + numbers[i] * cnt[i][1]) % MOD; } std::cout << (dp[n][0] + dp[n][1] + MOD) % MOD; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__) #else #define DEBUG(...) 6 #endif template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";} template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr> ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";} template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";} template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}} template<int MOD> struct ModInt { long long v; ModInt(long long _v = 0) {v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD;} ModInt& operator += (const ModInt &other) {v += other.v; if (v >= MOD) v -= MOD; return *this;} ModInt& operator -= (const ModInt &other) {v -= other.v; if (v < 0) v += MOD; return *this;} ModInt& operator *= (const ModInt &other) {v = v * other.v % MOD; return *this;} ModInt& operator /= (const ModInt &other) {return *this *= inverse(other);} bool operator == (const ModInt &other) const {return v == other.v;} bool operator != (const ModInt &other) const {return v != other.v;} friend ModInt operator + (ModInt a, const ModInt &b) {return a += b;} friend ModInt operator - (ModInt a, const ModInt &b) {return a -= b;} friend ModInt operator * (ModInt a, const ModInt &b) {return a *= b;} friend ModInt operator / (ModInt a, const ModInt &b) {return a /= b;} friend ModInt operator - (const ModInt &a) {return 0 - a;} friend ModInt power(ModInt a, long long b) {ModInt ret(1); while (b > 0) {if (b & 1) ret *= a; a *= a; b >>= 1;} return ret;} friend ModInt inverse(ModInt a) {return power(a, MOD - 2);} friend istream& operator >> (istream &is, ModInt &m) {is >> m.v; m.v = (-MOD < m.v && m.v < MOD) ? m.v : m.v % MOD; if (m.v < 0) m.v += MOD; return is;} friend ostream& operator << (ostream &os, const ModInt &m) {return os << m.v;} }; typedef ModInt<1000000007> M; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a(n); for (int i=0; i<n; i++) cin >> a[i]; // 0/1 for + or - vector<vector<M>> dp(n, vector<M>(2)); dp[0][0] = 1; for (int i=1; i<n; i++) { dp[i][0] = dp[i-1][0] + dp[i-1][1]; dp[i][1] = dp[i-1][0]; } M ret = 0; for (int i=0; i<n; i++) ret += a[i] * (dp[i][0] * (dp[n-i-1][0] + dp[n-i-1][1]) - dp[i][1] * dp[n-i-1][0]); cout << ret << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ll long long #define x1 first #define y1 second #define frb(i,a,b) for(ll (i)=(a);(i) <= (b); (i)+=(i)&-(i)) #define rfrb(i,a) for(ll (i)=(a);(i) > 0;(i)-=(i)&-(i)) #define fr(i,a,b) for(ll (i) = (a); (i) <= (b); (i)++) #define rep(i,c) for(auto (i) : (c)) #define mini(a,b) ((a) < (b)) ? (a) : (b) #define maxi(a,b) ((a) > (b)) ? (a) : (b) #define par pair<ll,ll> #define vp vector<par> #define vi vector<ll> const ll mod = 1e9+7; ll n; vi g; map<ll,ll>ar; int main() { ios_base::sync_with_stdio(0);cin.tie(0); cout.setf(ios::fixed);cout.precision(0); cin>>n; ll a; g.push_back(0); fr(i,1,n){ cin>>a; ar[a]++; if(ar[a]==1)g.push_back(a); } sort(g.begin(),g.end()); ll id = 0,sol=1; rep(v,g){ sol = ((v-id+1)*(sol))%mod; sol%=mod; id = v; } cout<<sol; return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long using namespace std; const ll MOD = 1000000007; int main() { ll N; cin >> N; vector<ll> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } sort(A.begin(), A.end()); ll ans = A[0]+1; for (int i = 0; i < N-1; ++i) { ll dif = A[i+1]-A[i]+1; ans = (ans * dif) % MOD; } cout << ans << "\n"; return 0; }
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define LL long long #define DB double #define LD long double #define ST string #define BS bitset #define PA pair<LL,LL> #define VE vector #define VL VE<LL> #define VP VE<PA> #define VVL VE<VL> #define VVVL VE<VVL> #define PQ priority_queue #define PQS priority_queue<LL,vector<LL>,greater<LL>> #define FI first #define SE second #define PB push_back #define POB pop_back #define PF push_front #define POF pop_front #define MP make_pair #define TS to_string #define TU to_ullong #define BPL __builtin_popcountll #define FOR(i,a,n) for(i=a;i<n;++i) #define FORR(i,a,n) for(i=n-1;i>=a;--i) #define rep(i,n) FOR(i,0,n) #define repr(i,n) FORR(i,0,n) #define ALL(a) a.begin(),a.end() #define RALL(a) a.rbegin(),a.rend() #define SORT(a) sort(ALL(a)) #define REV(a) reverse(ALL(a)) #define UB(a,n) *upper_bound(ALL(a),n) #define UBn(a,n) upper_bound(ALL(a),n)-a.begin() #define LB(a,n) *lower_bound(ALL(a),n) #define LBn(a,n) lower_bound(ALL(a),n)-a.begin() #define INF 1000000000000000003 #define PI 3.14159265358979323846264338327950288 #define MOD 1000000007 //#define MOD 998244353 #define ERR 1e-10 #define coutl cout<<fixed<<setprecision(15) #define FAST cin.tie(0);ios::sync_with_stdio(false) void Yn(LL a){if(a)printf("Yes\n");else printf("No\n");} void YN(LL a){if(a)printf("YES\n");else printf("NO\n");} void vout(VL &v){LL i;rep(i,v.size()){printf("%lld",v[i]);if(i<v.size()-1)printf(" ");}printf("\n");} void v2out(VVL &v){LL i;rep(i,v.size())vout(v[i]);} LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;} LL dig(LL n){LL ret=0;while(n)n/=10,++ret;return ret;} LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;} LL LCM(LL a,LL b){return a*b/GCD(a,b);} LL cmod(LL a,LL m){if(a%m<0)return a%m+abs(m);else return a%m;} LL DIV(LL a,LL d,LL m){LL l=m,x=1,y=0,k;while(l){k=d/l;d-=k*l;swap(l,d);x-=k*y;swap(x,y);}return cmod(a*cmod(x,m),m);} LL POW(LL a,LL n,LL m){LL ans=1;while(n>0){if(n&1)ans=ans*a%m;a=a*a%m;n>>=1;}return ans;} VL fact,finv,inv; void comi(LL n){LL i;fact.resize(max(2LL,n+1));finv.resize(max(2LL,n+1));inv.resize(max(2LL,n+1));fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[1]=1;FOR(i,2,n+1){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}} LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;} bool cmps(PA a,PA b){if(a.SE!=b.SE)return a.SE<b.SE;return a.FI<b.FI;} template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;} template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;} int main(){ FAST; LL i,ans=0,N,k=1,a=0,b=0,c,d; cin>>N; rep(i,N)k*=2; VL A(k); rep(i,k)cin>>A[i]; rep(i,k/2){ if(chmax(a,A[i]))c=i; if(chmax(b,A[k/2+i]))d=k/2+i; } if(a>b)cout<<d+1<<endl; else cout<<c+1<<endl; }
/*--ILRS-- sr4saurabh */ #include <bits/stdc++.h> using namespace std; typedef long long int ll; #define pb push_back #define popb pop_back #define mp make_pair #define ve vector #define vii vector < int > #define vll vector < ll > #define pii pair < int , int > #define pll pair < ll , ll > #define vpl vector < pll > #define fi first #define sz size #define len length #define se second //const ll mod=998244353; const ll mod = 1000000000+7; const ll N=10000000+6; #define M_PI 3.14159265358979323846 //-------------------------------------------- //-------------------------------------------- ll ret_sum(ll sum , ll ma) { if(sum < 2) return 0; if(sum > 2*ma) return 0; ll numways = 0; ma = min(ma , sum-1); if(sum & 1) { ll f1 = sum/2; ll f2 = f1 + 1; ll sub = sum - ma - 1; ll ans = f1 - sub; if(ans < 0) numways = 0; else numways = 2*ans; } else { ll f1 = sum/2; ll f2 = f1; ll sub = sum - ma - 1; ll ans = f1 - sub; if(ans < 0) numways = 0; else numways = 2*ans - 1; } return numways; } //============================================ int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen ("INPUT.txt" , "r" , stdin); freopen ("OUTPUT.txt", "w" , stdout); #endif //Never let somebody tell you - you are weak! You are as strong as anyone be, // u just need consistency and will power to change! ll n,k; cin>>n>>k; ll ans = 0; for(int i = 1; i <= 2*n ; i++) { ll r = ret_sum(i , n); ll f = ret_sum(i-k,n); ans = ans + r*f; } cout<<ans<<'\n'; return 0; }
#include <algorithm> #include <cassert> #include <queue> #include <vector> #include <iostream> using namespace std; int main() { int n; cin >> n; vector<int> adj[n+1]; vector<int> leaves; for (int i = 1; i <= n; i++) { if (2 * i <= n) { adj[i].push_back(2 * i); adj[i].push_back(2 * i + 1 <= n ? 2 * i + 1 : 1); } else { leaves.push_back(i); } } assert(leaves.size() * 2 >= n); int i = 1; for (int k : leaves) { adj[k].push_back((i > n) ? 1 : i); i++; adj[k].push_back((i > n) ? 1 : i); i++; } assert(i >= n); for (int i = 1; i <= n; i++) cout << adj[i][0] << ' ' << adj[i][1] << endl; // check // for (int i = 1; i <= n; i++) { // vector<int> dist(n+1, 1<<30); // queue<int> q; // q.push(i); // dist[i] = 0; // while (!q.empty()) { // int v = q.front(); q.pop(); // for (int u : adj[v]) // if (dist[u] == 1<<30) { // dist[u] = dist[v] + 1; // q.push(u); // } // } // // for (auto x : dist) cerr << x << ' '; cerr << endl; // assert(*max_element(dist.begin()+1, dist.end()) <= 10); // } }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(random_device{}()); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(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;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head int n; int main() { scanf("%d",&n); rep(i,0,n) printf("%d %d\n",(i*2)%n+1,(i*2+1)%n+1); }
#include <bits/stdc++.h> //#include <atcoder/all> #define endl "\n" using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_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; } const long long INF = 1e18; //const ll mod = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); ll t, N; cin >> t >> N; ll A = (100 * N + t - 1) / (t); cerr << A << endl; ll ans = (100 + t) * A / 100 - 1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl using lint = long long; typedef pair<int,int> P; const bool debugFlag=true; const lint linf=1e18+7; const lint inf=1e9+7; const int MOD=1000000007; lint gcd(lint a,lint b){ if(b==0)return a; return gcd(b,a%b); } signed main(){ int t;cin>>t; lint n;cin>>n; lint res=0; lint b=100; res=n/t*b; n%=t; lint ans=res*(100+t)/b; rep1(x,100){ if(n==0)break; if((res+x)*(100+t)/b>=(res+x-1)*(100+t)/b+2){ --n;ans=(res+x)*(100+t)/b; } } cout<<ans-1<<"\n"; return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less using namespace std; #define int long long #define pb push_back #define mp make_pair #define st(arr,a) memset(arr,a,sizeof arr) #define nl cout<<'\n' #define bitcount(x) __builtin_popcountll(x) const int MOD = 1000000007ll; #define forn(i,a,b) for(int i=a;i<=b;i++) #define rfor(i,a,b) for(int i=a;i>=b;i--) #define all(x) x.begin() , x.end() #define pi pair<int,int> #define X first #define Y second #define sz(A) (int)A.size() #define N 1014159 #define vi vector<int> #define v vector #define die exit(0) #define alln(arr) arr+1 , arr+1+n using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). #define in input_int() #define ins input_string() bool sb(int a,int i){return (a&(1LL<<i))>0;} int p2(int a){return (1LL<<a);} int input_int(){int a;cin>>a;return a;} string input_string(){string s;cin>>s;return s;} int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int ceil(int a,int b){return (a+b-1)/b;} int ct,visited[N],ans; vi adj[N]; int n,m,k,a,b,c,d,x,y,arr[N]; void clr() { forn(i,0,n+10)adj[i].clear(),visited[i]=0,arr[i]=0; } string s; void solve() { cin>>n>>m; if(m==0) { forn(i,1,n) { cout<<2*i<<" "<<2*i+1;nl; } return; } if(m<0) { cout<<-1; return; } if(m>n-2) { cout<<-1; return; } int last=1e9; cout<<1<<" "<<last;nl; last--; int first=2; forn(i,1,n-1) { if(i<=m) { cout<<first<<" "<<first+1;nl; first+=2; } else if(i==m+1) { cout<<first<<" "<<last;nl; last--; first++; } else { cout<<first<<" "<<last;nl; first++; last--; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin>>t; forn(i,1,t) { //cout<<":Case #"<<i<<": "; solve(); nl; } return 0; } // clang++ c.cpp ; ./a.out < a.txt ;
//go to line 54 for some useful code. #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.hpp" #else #define dbg(...) 47 #define dbgm(...) 47 #endif // refer https://codeforces.com/blog/entry/66279 #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") //refer-> https://codeforces.com/blog/entry/62393 struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; typedef long long ll; typedef unsigned long int ul; typedef unsigned long long int ull; typedef unsigned int ui; typedef long double ld; typedef pair<ll, ll> pll; typedef vector<vector<ll>> matrix; typedef vector<ll> vll; typedef vector<int> vii; typedef unordered_map<int,int, custom_hash> umap; typedef unordered_set<int, custom_hash>uset; #define f(i, x, n) for (int i = x; i < n; i++) #define rf(i, n, x) for(int i=n;i>=x;--i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define F first #define S second #define pb push_back #define endl "\n" #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); #define mem(a, b) memset(a, b, sizeof(a)) #define fast_io() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define mod ((ll)(1e9+7)) #define oo INT64_MAX #define rng ((int)(2e5+10)) //* write code from here void solve(){ ll n; cin>>n; string s, t; cin>>s>>t; ll ans = 0; vii a, b; f(i, 0, n)if(s[i] == '0')a.pb(i); f(i, 0, n)if(t[i] == '0')b.pb(i); if(a.size()!= b.size()){ cout<<-1<<endl; return; } for(int i=0;i<a.size();++i){ if(a[i]!=b[i]) ans++; } cout<<ans<<endl; } int main() { fast_io(); cerr << "...............Console is yours! :)................." << endl; //freopen("input.in","r",stdin); solve(); cerr<<"......^_^....."<<endl; return 0; }
#include<algorithm> #include<iostream> #include<cstring> #include<bitset> #include<cstdio> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> using namespace std; #define neinf 0xc0c0c0c0c0c0c0c0ll #define inf 0x3f3f3f3f3f3f3f3fll #define uint unsigned int #define ull unsigned ll #define HgS 1000000007 #define ll long long #define reg register #define opr operator #define ret return #define gc getchar #define pc putchar #define cst const #define db double #define il inline il ll rd() { reg ll res=0,lab=1; reg char ch=gc(); while((ch<'0'||ch>'9')&&ch!=EOF) {if(ch=='-')lab=-lab;ch=gc();} while(ch>='0'&&ch<='9') res=(res<<3)+(res<<1)+(ch&15),ch=gc(); return res*lab; } il void prt(ll x,char t='\n') { static char ch[40];reg int tp=0; if(!x){fputs("0",stdout);if(t)pc(t);return;} if(x<0)pc('-'),x=-x; while(x)ch[++tp]=(x%10)^48,x/=10; while(tp)pc(ch[tp--]); if(t)pc(t); } il ll umax(ll a,ll b){return a>b?a:b;} il ll umin(ll a,ll b){return a<b?a:b;} il ll uabs(ll x){return x>0?x:-x;} il ll qpow(ll n,ll e=HgS-2,ll p=HgS) { reg ll res=1; while(e){if(e&1)res=res*n%p;n=n*n%p;e>>=1;} return res; } ll T,L,R; int main() { T=rd(); while(T --> 0) { L=rd();R=rd(); if(2*L>R)prt(0); else prt((R-2*L+1)*(R-2*L+2)/2); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long //#define double long double #define max(x,y) (((x)>(y))?(x):(y)) #define min(x,y) (((x)<(y))?(x):(y)) #define all(v) v.begin(),v.end() #define vi vector<int> #define MOD 1000000007 #define inf 1e18 #define ps(num,precision) fixed<<setprecision(precision)<<num #define md(arr,n,type) type *arr=new type[n] void inout(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } int lcm(int a,int b){ return a/gcd(a,b)*b; } void SieveOfEratosthenes(int n){ bool prime[n+1]; memset(prime,true,sizeof(prime)); for(int p=2;p*p<=n;p++){ if(prime[p]){ for(int i=p*p;i<=n;i+=p){ prime[i]=false; } } } for(int i=2;i<=n;i++){ if(prime[i]){ } } } int printDivisors(int n) { int sum=0; for (int i=1; i<=sqrt(n); i++) { if (n%i == 0) { if (n/i == i) sum+=1; else{ sum+=2; } } } return sum; } void solve(){ int n; cin>>n; int a[n]; int min; int ans=0; int con=0; vector<pair<int,int>> b; for(int i=0;i<n;i++){ cin>>a[i]; b.push_back(make_pair(a[i],i)); } sort(all(b)); for(int i=0;i<n;i++){ min=a[i]; if(ans<a[i]){ ans=a[i]; } for(int j=i+1;j<n;j++){ if(min>a[j]){ min=a[j]; } if((j-i+1)*min>ans){ ans=(j-i+1)*min; } } } cout<<ans; } int32_t main(){ //inout(); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> struct P { T x; T y; P(T x, T y): x(x), y(y) {} }; const int DX[4] = {1, 0, -1, 0}; const int DY[4] = {0, 1, 0, -1}; #define int long long #define rep(i, n) for (int i = 0; i < (n); i++) constexpr long long INF = 1000000000000000000LL + 8; signed main() { int N, M; cin >> N >> M; vector<int> A(M); rep(i, M) cin >> A[i]; sort(A.begin(), A.end()); A.push_back(N + 1); // 最小の連続白を探してスタンプにする // スタンプを押す配列も作っていく int len = INF; vector<int> s; int current = 0; rep(i, M + 1) { int l = A[i] - current; if (l > 1) { chmin(len, l - 1); s.push_back(l - 1); } current = A[i]; } // スタンプを足していく int result = 0; rep(i, s.size()) { result += (s[i] + len - 1) / len; } cout << result << endl; return 0; }
#include <bits/stdc++.h> #define EOL "\n"; typedef long long ll; using namespace std; int main(){ int N, W; cin >> N >> W; vector<pair<int, int>> event; int s, t, p; for (int i = 0; i < N; ++i) { cin >> s >> t >> p; event.push_back(make_pair(s, p)); event.push_back(make_pair(t, -p)); } sort(event.begin(), event.end()); long long crr = 0; for (auto x : event) { crr += x.second; if (crr > W) { cout << "No"; return 0; } } cout << "Yes"; return 0; }
#include <bits/stdc++.h> #include <cassert> typedef long long int ll; using namespace std; // #include <atcoder/all> // using namespace atcoder; // @@ !! LIM() int main(/* int argc, char *argv[] */) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(20); ll N; cin >> N; vector<ll> A(N), B(N); for (ll i = 0; i < N; i++) cin >> A[i]; for (ll i = 0; i < N; i++) cin >> B[i]; ll maxA = 0; ll minB = 1001; for (ll i = 0; i < N; i++) { maxA = max(A[i], maxA); minB = min(B[i], minB); } if (minB >= maxA) { cout << minB - maxA + 1 << endl; }else { cout << 0 << endl; } return 0; }
#include <iostream> #include <deque> #include <unordered_map> #include <unordered_set> #include <vector> //#include "maybe.h" #include <limits> #include <bitset> #include <queue> #include <algorithm> #include <list> #include <cmath> #include <cstring> #include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using ui = unsigned int; using PI = pair<int, int>; void func() { int N; ll md = 0; ll cd = 0; ll ed_s = 0; cin >> N; for (int i = 0; i < N; i++) { ll x; cin >> x; md += abs(x); cd = max(cd, abs(ll(x))); ed_s += x * x; } double ed = sqrt(ed_s); cout << fixed << setprecision(15); cout << md << endl; cout << ed << endl; //printf("%.15f\n", ed); cout << cd << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); func(); return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); //#pragma GCC optimize("Ofast") //#pragma GCC optimize("fast-math") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") #define ios ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0); #define int long long #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() //ll p = 131; int mod = 1e9 + 7; int inf = 3000000000000000000; void solve() { int n; string s; cin >> n >> s; int f = 1; int q; cin >> q; while (q-- > 0) { int t, a, b; cin >> t >> a >> b; if (t == 1) { --a, --b; if (!f) a = (a + n) % (2 * n), b = (b + n) % (2 * n); swap(s[a], s[b]); } else { f = 1 - f; } } if (f) { cout << s; } else { cout << s.substr(n) << s.substr(0, n); } } signed main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios; // int t; // cin >> t; // while (t-- > 0) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define Mp make_pair #define pb push_back using ll = long long; using db = double; using pii = pair<int, int>; using vi = vector<int>; mt19937 mrand(time(0)); ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; } ll get(ll l, ll r) { return get(r - l + 1) + l; } int n, x[20], y[20], z[20], f[1 << 17][17], ans = 1e9; int dis(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]); } signed main() { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d %d %d", &x[i], &y[i], &z[i]); memset(f, 0x3f, sizeof(f)), f[1][0] = 0; for(int S = 2; S < 1 << n; S++) for(int i = 0; i < n; i++) if(S >> i & 1) for(int j = 0; j < n; j++) if((S >> j & 1) && j != i) f[S][i] = min(f[S][i], f[S ^ (1 << i)][j] + dis(j, i)); for(int i = 0; i < n; i++) ans = min(ans, f[(1 << n) - 1][i] + dis(i, 0)); printf("%d\n", ans); fprintf(stderr, "time=%.4f\n", (db)clock()/CLOCKS_PER_SEC); return 0; }
#include<bits/stdc++.h> #define rep(a,b,c) for(int a=b;a<=c;a++) #define per(a,b,c) for(int a=b;a>=c;a--) #define pb push_back #define mk make_pair #define pii pair<int,int> #define mem(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f3f3f3f3f using namespace std; typedef long long ll; typedef double db; const int mod = 1e9+7; const int N = 1e6 + 5; int n, m, k; int main(){ scanf("%d%d%d", &n, &k, &m); int sum=0, x; rep(i,1,n-1) scanf("%d", &x), sum+=x; int ans=-1; rep(i,0,k) if((sum+i)/n>=m){ ans=i; break; } printf("%d\n", ans); return 0; } // // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\____ // .' \| |// `. // / \||| : |||// \ // / _||||| -:- |||||- \ // | | \ - /// | | // | \_| ''\---/'' | | // \ .-\__ `-` ___/-. / // ___`. .' /--.--\ `. . __ // ."" '< `.___\_<|>_/___.' >'"". // | | : `- \`.;`\ _ /`;.`/ - ` : | | // \ \ `-. \_ __\ /__ _/ .-` / / // ======`-.____`-.___\_____/___.-`____.-'====== // `=---=' // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // 佛祖保佑 永无BUG
#include <iostream> using namespace std; int main(){ int N,K,M; cin>>N>>K>>M; int A[110]; for (int i=0; i<N-1; i++) cin>>A[i]; int points=N*M; int sum=0; for ( int i=0; i<N-1; i++) sum+=A[i]; int t=points-sum; if (t<=0) cout<<'0'<<endl; else if (t<=K) cout<<t<<endl; else cout<<"-1"<<endl; }
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10 #define ll long long #define double long double #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #define all(a) (a).begin(), (a).end() #define sz size #define f first #define s second #define pb push_back #define er erase #define in insert #define mp make_pair #define pi pair #define rc(s) return cout<<s,0 #define endl '\n' #define mod 1000000007 #define PI 3.14159265359 #define MAXN 100005 #define INF 1000000005 #define LINF 1000000000000000005ll #define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; ll x,y,a,b,ans=0; int32_t main(){ CODE_START; cin>>x>>y; cin>>a>>b; while(x<b&&x*a<y){ x*=a; ans++; } ans+=(y-1-x)/b; cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // a + bi <=> a + bx using P = complex<double>; int main(){ int n, m, k; cin >> n >> m >> k; vector<int> a(k); for(int i=0; i<k; ++i) cin >> a[i]; // 「振出しに戻る」が M マス連続していないことを確認する int conti_cnt = 1; for(int i=0; i<k; ++i){ if(i > 0 && a[i] - a[i-1] == 1){ conti_cnt += 1; }else{ conti_cnt = 1; } if(conti_cnt >= m){ cout << -1 << endl; return 0; } } // 求める値(マス 0 からマス n に到達するために必要な回数の期待値)を X とする // dp[i] = {マス i からマス n に到達するために必要な回数の期待値} // = 0 ( i>=n のとき) // X (マス i が「振出しに戻る」のとき) // (dp[i+1] + ... + dp[i+m]) / m + 1 (それ以外) vector<P> dp(n+m+1, 0); for(int i=0; i<k; ++i){ dp[a[i]] = P{0, 1}; } P s{0, 0}; // 累積和 for(int i=n-1; i>=0; --i){ s += dp[i+1] - dp[i+m+1]; if(dp[i].imag() == 0){ dp[i] = s / double(m) + 1.0; } } // X = a + bX より X=a/(1-b) cout << setprecision(20) << dp[0].real() / (1 - dp[0].imag()) << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 60; int mod = 1000000007; int dp[3030][10][50]; void init(){ rep(i, 3030) rep(j, 10) rep(k, 50) dp[i][j][k] = 0; dp[0][0][0] = 1; } int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> A(N), B(N), C(N), D(N), E(N); rep(i, N) cin >> A[i] >> B[i] >> C[i] >> D[i] >> E[i]; int ok = 0, ng = INF; int m = (ok+ng)/2; while(abs(ok-ng) > 1){ //cout << "m: " << m << endl; init(); rep(i, N){ int val = 0; if(A[i] >= m) val += (1 << 4); if(B[i] >= m) val += (1 << 3); if(C[i] >= m) val += (1 << 2); if(D[i] >= m) val += (1 << 1); if(E[i] >= m) val += (1 << 0); //cout << i << ": " << val << endl; for(int ninzuu = 0; ninzuu < 5; ninzuu++){ rep(now_state, (1 << 5)){ dp[i+1][ninzuu][now_state] = max(dp[i+1][ninzuu][now_state], dp[i][ninzuu][now_state]); dp[i+1][ninzuu+1][now_state|val] = max(dp[i+1][ninzuu+1][now_state|val], dp[i][ninzuu][now_state]); } } } if(dp[N][3][(1 << 5)-1] == 1) ok = m; else ng = m; m = (ok+ng)/2; } cout << ok << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int,int> pii; typedef long long ll; const long long MOD=1000000007; #define rep(i,n) for(ll i=0;i<(n);i++) #define rep2(i,m,n) for(ll i=(m);i<(n);i++) #define ALL(v) v.begin(), v.end() #define pb push_back const int INF=1e9; int n,m,k; vector<int> c(20); vector<vector<int>> graph(200000,vi(0)); vector<vector<int>> dp(1<<20,vi(20,INF)); vector<vector<int>> dis(20,vi(20)); int bfs(int s, int t){ vi seen(n,-1); seen[s]=0; queue<int> q; q.push(s); while (!q.empty()){ int x=q.front(); q.pop(); for (int next:graph[x]){ if (seen[next]==-1) { seen[next]=seen[x]+1; q.push(next); } } } return seen[t]; } int rec(int S, int v){ if (dp[S][v]!=INF) return dp[S][v]; if (S==(1<<v)) return dp[S][v]=0; rep(i,k){ if ((S>>i & 1) and (i!=v)){ dp[S][v]=min(rec(S & ~(1<<v),i)+dis[v][i], dp[S][v]); } } return dp[S][v]; } int main(){ cin>>n>>m; rep(i,m){ int a,b; cin>>a>>b; a--; b--; graph[a].pb(b); graph[b].pb(a); } cin>>k; rep(i,k){ int tmp; cin>>tmp; tmp--; c[i]=tmp; } rep(i,k){ rep(j,k){ if (i<=j) dis[i][j]=bfs(c[i],c[j]); else dis[i][j]=dis[j][i]; } } int ans=1e9; rep(i,k){ rep(j,k){ if (dis[i][j]==-1) ans=-1; } } if (ans!=-1){ rep(i,k){ ans=min(ans,rec((1<<k)-1,i)+1); } } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ long long int A,B,C; cin >> A >> B >> C; A %= 10; if(A == 0) cout << 0 << endl; if(A == 1) cout << 1 << endl; if(A == 5) cout << 5 << endl; if(A == 6) cout << 6 << endl; if(A == 2){ B %= 4; if(B == 1) cout << 2 << endl; if(B == 2){ C %= 4; if(C == 1) cout << 4 << endl; if(C == 2) cout << 6 << endl; if(C == 3) cout << 6 << endl; if(C == 0) cout << 6 << endl; } if(B == 3){ C %= 4; if(C == 1) cout << 8 << endl; if(C == 2) cout << 2 << endl; if(C == 3) cout << 8 << endl; if(C == 0) cout << 2 << endl; } if(B == 0) cout << 6 << endl; } if(A == 3){ B %= 4; if(B == 1) cout << 3 << endl; if(B == 2){ C %= 4; if(C == 1) cout << 9 << endl; if(C == 2) cout << 1 << endl; if(C == 3) cout << 1 << endl; if(C == 4) cout << 1 << endl; } if(B == 3){ C %= 4; if(C == 1) cout << 7 << endl; if(C == 2) cout << 3 << endl; if(C == 3) cout << 7 << endl; if(C == 0) cout << 3 << endl; } if(B == 0) cout << 1 << endl; } if(A == 4){ B %= 2; if(B == 1) cout << 4 << endl; if(B == 0) cout << 6 << endl; } if(A == 7){ B %= 4; if(B == 1) cout << 7 << endl; if(B == 2){ C %= 4; if(C == 1) cout << 9 << endl; if(C == 2) cout << 1 << endl; if(C == 3) cout << 1 << endl; if(C == 0) cout << 1 << endl; } if(B == 3){ C %= 4; if(C == 1) cout << 3 << endl; if(C == 2) cout << 7 << endl; if(C == 3) cout << 3 << endl; if(C == 0) cout << 7 << endl; } if(B == 0) cout << 1 << endl; } if(A == 8){ B %= 4; if(B == 1) cout << 8 << endl; if(B == 2){ C %= 4; if(C == 1) cout << 4 << endl; if(C == 2) cout << 6 << endl; if(C == 3) cout << 6 << endl; if(C == 0) cout << 6 << endl; } if(B == 3){ C %= 4; if(C == 1) cout << 2 << endl; if(C == 2) cout << 8 << endl; if(C == 3) cout << 2 << endl; if(C == 0) cout << 8 << endl; } if(B == 0) cout << 6 << endl; } if(A == 9){ B %=2; if(B == 1) cout << 9 << endl; if(B == 0) cout << 1 << endl; } }
#include <bits/stdc++.h> using namespace std; int A, B, C; int qp(int x, int y, int P) { int z = 1; for (; y; y >>= 1, x = 1LL * x * x % P) { if (y & 1) z = 1LL * z * x % P; } return z; } int main() { scanf("%d %d %d", &A, &B, &C); if (B <= 4 && C <= 4) { A = qp(A % 10, qp(B, C, 998244353), 10); } else { B = qp(B % 4, C, 4); A = qp(A % 10, B + 4, 10); } printf("%d\n", A); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } constexpr ll mod=998244353; ll mod_pow(ll a,ll b){ a%=mod; if(b==0)return 1; if(b==1)return a; ll res=mod_pow(a,b/2)%mod; res*=res; res%=mod; if(b%2)res*=a; return res%mod; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n); ll sum=0; ll po=1; for(int i=0;i<n;i++){ cin >> a[i]; } sort(a.begin(), a.end()); for(int i=1;i<n;i++){ (sum+=po*a[i]%mod)%=mod; po*=2; po%=mod; } ll res=0; for(int i=0;i+1<n;i++){ (res+=a[i]*sum%mod)%=mod; // cout << res << " " << sum << endl; sum-=a[i+1]; sum*=mod_pow(2,mod-2); sum%=mod; } for(int i=0;i<n;i++){ (res+=a[i]*a[i])%=mod; } if(res<0)res+=mod; cout << res << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ long long n; cin >> n; long long box[210]; for(int i=0;i<=199;i++){ box[i]=0; } for(long long i=0;i<n;i++){ long long a; cin >> a; a %= 200; if(a < 0){ a += 200; } if(a >= 200){ a -= 200; } box[a] += 1; } long long ans = 0; for(int i = 0 ; i <= 199 ; i++){ ans += (box[i] * (box[i] - 1)) / 2; } cout << ans; return 0; }
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int N=100010; int n; ll a[N]; ll ans=1e19; void dfs(ll i,ll pre,ll xo)//x表示当前的位置,我们通过位置去判断是否需要去异或或者或 { //pre表示我们当前的答案,xo表示我们前面统计的所以答案的异或起来的数 if(i>n) { ans=min(ans,xo^pre); return ; } dfs(i+1,pre|a[i],xo);//这个就是a[i]还没有自立门户,我们就直接或上就行了 dfs(i+1,a[i],pre^xo);//表示我们a[i]已经自立门户了,我们就去统后面的x0 //就是去统计之前的异或和来方便后面的计算 } int main() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; dfs(1,0,0); cout<<ans; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; #define MOD 1000000007 int main() { int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } ll ans = 2e18; for (int bit = 0; bit < (1 << n); ++bit) { vector<ll> v; ll sum = 0; for (int i = 0; i < n; ++i) { if (bit >> i & 1) { v.push_back(sum); sum = a[i]; } else { sum |= a[i]; } } v.push_back(sum); ll res = 0; for (int i = 0; i < v.size(); ++i) { res ^= v[i]; } ans = min(ans, res); } cout << ans << endl; return 0; }
/* C - Robot on Grid */ #include <bits/stdc++.h> using namespace std; using ll = int64_t; using pii = pair<int, int>; const int N = 5e3 + 10; const int mod = 998244353; int h, w, n, s[N][N]; char a[N][N]; int ksm(int a, int b, int p) { int res = 1 % p; while (b) { if (b & 1) res = (ll)res * a % p; a = (ll)a * a % p; b >>= 1; } return res; } int solve(void) { int inv = ksm(3, mod - 2, mod); s[1][1] = 1; for (int k = 1; k <= h; ++k) { for (int i = 1; i <= w; ++i) { if ('R' == a[k][i]) s[k][i + 1] = (s[k][i] + s[k][i + 1]) % mod; else if ('D' == a[k][i]) s[k + 1][i] = (s[k][i] + s[k + 1][i]) % mod; else if ('X' == a[k][i]) { s[k][i + 1] = (s[k][i] + s[k][i + 1]) % mod; s[k + 1][i] = (s[k][i] + s[k + 1][i]) % mod; } else { int val = (ll)s[k][i] * 2 * inv % mod; s[k][i + 1] = (val + s[k][i + 1]) % mod; s[k + 1][i] = (val + s[k + 1][i]) % mod; } } } return (ll)s[h][w] * ksm(3, h * w - n, mod) % mod; } signed main(int argc, char** argv) { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif while (cin >> h >> w >> n) { memset(a, 0, sizeof(a)); memset(s, 0, sizeof(s)); for (int k = 1; k <= n; ++k) { int x, y; char c; cin >> x >> y >> c; a[x][y] = c; } cout << solve() << endl; } return 0; }
#include <bits/stdc++.h> #define be(v) (v).begin(),(v).end() #define pb(q) push_back(q) #define rep(i, n) for(int i=0;i<n;i++) #define all(i, v) for(auto& i : v) typedef long long ll; using namespace std; const ll mod=998244353, INF=(1LL<<60); #define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl; ///////modint struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; vector<vector<mint> > dp; int a[5000][5000]; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); ll h, w, k, y, x; cin >> h >> w >> k; char c; memset(a, 0, sizeof(a)); mint inv3 = mint(3).inv(); rep(i, k){ cin >> y >> x >> c; y--; x--; if(c == 'R') a[y][x] = 1; else if(c == 'D') a[y][x] = 2; else a[y][x] = 3; } dp.resize(h, vector<mint> (w, mint(0))); dp[0][0] = mint(3).pow(h * w - k); rep(i, h){ rep(j, w){ if(i < h - 1){ if(a[i][j] == 2 || a[i][j] == 3){ dp[i + 1][j] += dp[i][j]; }else if(a[i][j] == 0){ dp[i + 1][j] += dp[i][j] * inv3 * mint(2); } } if(j < w - 1){ if(a[i][j] == 1 || a[i][j] == 3){ dp[i][j + 1] += dp[i][j]; }else if(a[i][j] == 0){ dp[i][j + 1] += dp[i][j] * inv3 * mint(2); } } } } cout << dp[h - 1][w - 1].x << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long #define ld long double #define F first #define S second #define all(v) v.begin(),v.end() #define sz(v) (int)v.size() #define precision cout << fixed << setprecision(15); const int inf = 1e9; const long long INF = 1e18; int mod = 1e9 + 7; const int bit32 = log2(inf) + 3; const int bit64 = log2(INF) + 3; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); clock_t time_p = clock(); void ktj() { time_p = clock() - time_p; cerr << "Time elapsed : " << (float)(time_p)/CLOCKS_PER_SEC << "\n"; } void pre() {} inline int add(long long a, long long b){a += b; if(a >= mod) a -= mod; return a;} inline int sub(long long a, long long b){a -= b; if(a < 0) a += mod; return a;} inline int mul(long long a, long long b){a %= mod; b %= mod; return (int)((long long) a * b % mod);} inline int modexpo(long long a, long long b){int res = 1; while(b > 0){ if(b & 1) res = mul(res, a); a = mul(a, a); b /= 2;} return res;} inline int divide(long long a, long long b){return mul(a, modexpo(b, mod - 2));} inline int inv(long long a){return modexpo(a, mod - 2);} void solve() { long long a, b, c; cin >> a >> b >> c; mod = 100; long long e = modexpo(b, c); mod = 10; if (e == 0) { cout << modexpo(a, 4) << '\n'; } else { long long f = modexpo(a, e); cout << f << '\n'; } } #define GOOGLE 0 #define MULTIPLE_TC 0 int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); pre(); int t = 1, T; if (MULTIPLE_TC) cin >> t; for (T = 1; T <= t; T++) { if (GOOGLE) cout << "Case #" << T << ": "; solve(); } ktj(); }
#include <iostream> #define int long long using namespace std; int binpow(int a, int b, int c) { int ret = 1; while (b) { if (b & 1) ret = ret * a % c; a = a * a % c; b >>= 1; } return ret % c; } signed main() { int a, b, c; cin >> a >> b >> c; a %= 10; b %= 4; int exp = binpow(b, c, 4); if (exp == 0) exp = 4; cout << binpow(a, exp, 10); return 0; }
#pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; #define pii pair<int,int> #define mp make_pair #define pb push_back #define visit asdf const int N=2e5+5; int n; int a[N],b[N],c[N],p[N]; vector< vector<int> > v; bool visit[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i); for(int i=1;i<=n;i++) scanf("%d",b+i); for(int i=1;i<=n;i++) scanf("%d",p+i); for(int i=1;i<=n;i++) if(a[i]<=b[p[i]] && p[i]!=i) return puts("-1"),0; for(int i=1;i<=n;i++) c[i]=b[p[i]]; for(int i=1;i<=n;i++) b[i]=c[i]; for(int i=1;i<=n;i++) { if(visit[i]) continue; vector<int> vec; vec.pb(i); visit[i]=1; int j=i; while(p[j]!=i) { vec.pb(p[j]); visit[p[j]]=1; j=p[j]; } v.pb(vec); } printf("%d\n",n-v.size()); for(int ii=0;ii<v.size();ii++) { vector<int> vec=v[ii]; int mn=1e9,u; for(int i=0;i<vec.size();i++) mn=min(mn,b[vec[i]]); for(int i=0;i<vec.size();i++) if(b[vec[i]]==mn) u=i; for(int i=vec.size()+u;i>u+1;i--) printf("%d %d\n",vec[i%vec.size()],vec[(i-1)%vec.size()]); } return 0; }
#include <bits/stdc++.h> using namespace std ; const int MAX = 2e5 + 10 ; int n ; int a[MAX] , b[MAX] , p[MAX] , pos[MAX] ; vector< pair<int , int> >vp , ans ; int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n ; for(int i = 0 ; i < n ; ++i) cin>>a[i] ; for(int i = 0 ; i < n ; ++i) cin>>b[i] ; for(int i = 0 ; i < n ; ++i) { cin>>p[i] ; p[i]-- ; pos[p[i]] = i ; if(b[p[i]] >= a[i] && p[i] != i) return cout<<-1<<"\n" , 0 ; } for(int i = 0 ; i < n ; ++i) vp.emplace_back(a[i] , i) ; sort(vp.begin() , vp.end()) ; for(auto &i : vp) { int idx = i.second ; if(p[idx] == idx) continue ; int x = pos[idx] ; ans.emplace_back(idx+1 , x+1) ; swap(p[idx] , p[x]) ; pos[idx] = idx , pos[p[x]] = x ; } cout<<ans.size()<<"\n" ; for(auto &i : ans) cout<<i.first<<" "<<i.second<<"\n" ; return 0 ; }
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define md 998244353 #define N 200010 #define ll long long ll a[N], ans = 0; ll ksm(ll x, ll y) { if(y < 0) return 1; if(!y) return 1; ll l = ksm(x, y / 2); if(y % 2) return l * l % md * x % md; return l * l % md; } int main() { int n, i, j; scanf("%d", &n); for(i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); ll t = 0; for(j = 1; j <= n; j++) t = (t + a[j] * ksm(2, j - 2)) % md; for(i = 1; i <= n; i++) { ans = (ans + a[i] * t) % md; t = (((t - a[i] - a[i + 1]) * ksm(2, md - 2) + a[i + 1]) % md + md) % md; } printf("%lld\n", ans); return 0; }
#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(ll i = 0;i < n;i++) #define ll long long //#define MOD 1000000007LL #define MOD 998244353LL #define doublecout(a) cout<<setprecision(16)<<a<<endl; using vi = vector<ll>; // intの1次元の型に vi という別名をつける using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける const ll llMAX=9223372036854775807LL; const ll llMIN=-9223372036854775808LL; void myprint1D(vi &data) { REP(i,data.size()) cout<<data[i]<<" "; cout<<endl; } //配列を[y][x]で表示 void myprint2D_T(vvi &data) { REP(i,data.size()) myprint1D(data[i]); } //配列を[x][y]で表示 void myprint2D(vvi &data) { ll l1=data.size(); ll l2=data[0].size(); REP(j,l2){ REP(i,l1) cout<<data[i][j]<<" "; cout<<endl; } } //print(a,b...)って使い方 void print1(ll a){cout<<a<<endl;} void print2(ll a,ll b){cout<<a<<" "<<b<<endl;} void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;} void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;} void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;} //moddivとnCkとgarner ll gcd(ll a,ll b){ ll r = a % b; while(r!=0LL){ a = b; b = r; r = a % b; } return b; } //c剰余下でaをbで割った値 ll moddiv(ll a,ll b,ll c){ ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans; if (t==0LL) return a/b; for(int i=0;i<900;i++){ m=x0/x1; x2=x0-x1*m; n2=(n0-m*n1)%c; if (x2==1LL){ ans=(n2+c)%c; break; } x0=x1;x1=x2; n0=n1;n1=n2; } return (a+((t*ans)%c)*b-t)/b; } //剰余の割ざん //aのn乗 mod c ll expmod(ll a,ll n,ll c){ ll ans=1LL,aa=a,beki=n; for(int i=0;i<64;i++){ if (beki%2==1LL) ans=ans*aa%c; aa=aa*aa%c; beki/=2LL; if (beki==0LL)break; } return ans; } //nCk vi mulmod(500001);//0!,1!,2!,3!,4!,,,, void first_nCk(){ mulmod[0]=1LL; for(ll i=1;i<500001;i++){ mulmod[i]=mulmod[i-1]*i%MOD; } } ll nCk(ll nn,ll kk){ return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD); } ll modinv(ll a,ll b){ return moddiv(1,a%b,b); } //条件ar<a,br<b ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){ if (gcd(a,b)==1){ if (a==1){ return br; } if (b==1){ return ar; } __int128_t tmp=(br-ar)%b*modinv(a,b)%b; if (tmp<0)tmp+=b; return ar+tmp*a; }else{//299%200=99,299%300=299とかのとき //ありえない場合-1を出力 __int128_t g=gcd(a,b); if (ar>br){ __int128_t rr=ar-br; if (rr%g!=0){ return -1; } __int128_t rrg=rr/g; __int128_t ret=garner(a/g,b/g,rrg,0); return ret*g+br; }else{ __int128_t rr=br-ar; if (rr%g!=0){ return -1; } __int128_t rrg=rr/g; __int128_t ret=garner(a/g,b/g,0,rrg); return ret*g+ar; } } } ll n,k,x,y; int main(){ ll ans=0; cin >> n; vi a(n),b(n),c(n); REP(i,n) cin>>a[i]; sort(a.begin(), a.end()); ll w=1; for(ll i=1;i<n;i++){ b[i]=a[i]*w%MOD; w=w*2%MOD; } ll cc=0; for(ll i=1;i<n;i++){ cc+=b[i]; } cc%=MOD; for(ll i=0;i<n;i++){ /* for(ll j=i+1;j<n;j++){ //a[i]が最小a[j]が最大が何個あるか ll sc=a[i]*a[j]%MOD; sc=sc*expmod(2,j-i-1,MOD)%MOD; ans+=sc; }*/ if (i==n-1)break; ans+=a[i]*cc%MOD; ans%=MOD; cc=cc-a[i+1]+MOD; cc%=MOD; cc=moddiv(cc,2,MOD); } for(ll i=0;i<n;i++){ ans+=a[i]*a[i]%MOD; ans%=MOD; } /* 最後に1この場合を足す */ cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define pb push_back #define sz size() #define all(c) (c).begin(), (c).end() using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MOD = 1e9 + 7; const int maxn = 5e5 + 7; int main(){ ios::sync_with_stdio(false); cin.tie(0); int a, b, c; cin >> a >> b >> c; cout << 21 - a - b - c << '\n'; return 0; }
//*** Author: ShlokG ***// #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // find_by_order(k) returns iterator to kth element starting from 0 // order_of_key(k) returns count of elements strictly smaller than k #define fix(f,n) std::fixed<<std::setprecision(n)<<f #define epsi (double)(0.00000000001) typedef long long int ll; typedef unsigned long long int ull; #define vi vector<ll> #define pii pair<ll,ll> #define vii vector<pii> #define max(a,b) ((a>b)?a:b) #define min(a,b) ((a>b)?b:a) #define min3(a,b,c) min(min(a,b),c) #define max3(a,b,c) max(max(a,b),c) #define ff(a,b,c) for(int a=b; a<=c; a++) #define frev(a,b,c) for(int a=c; a>=b; a--) #define REP(a,b,c) for(int a=b; a<c; a++) #define pb push_back #define mp make_pair #define endl "\n" #define all(v) v.begin(),v.end() #define sz(a) (ll)a.size() #define F first #define S second #define ld long double //#define INF 2000000000000000000 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define ub upper_bound #define lb lower_bound #define setbits(x) __builtin_popcountll(x) #define trav(a,x) for(auto &a:x) #define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end()) #define rev(arr) reverse(all(arr)) #define gcd(a,b) __gcd(a,b); #define ub upper_bound // '>' #define lb lower_bound // '>=' #define qi queue<ll> #define si stack<ll> const long double PI = acos(-1); mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0,lim-1); return uid(rang); } bool prime[200001]; void SieveOfEratosthenes(){ memset(prime, true, sizeof(prime)); prime[1]=false; for(ll p=2; p*p<=200000; p++){ if(prime[p] == true){ for (int i=p*p; i<=200000; i += p) prime[i] = false; } } return; } ll gcdExtended(ll a, ll b, ll *x, ll *y){ if(a == 0){ *x = 0; *y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b%a, a, &x1, &y1); *x = y1 - (b/a) * x1; *y = x1; return gcd; } ll modInverse(ll a, ll m){ ll x, y; ll g = gcdExtended(a, m, &x, &y); ll res = (x%m + m) % m; return res; } ll binary_Search(vector<ll>&arr,ll key){ ll l=0,r=arr.size()-1; ll ans; while(l<=r){ ll mid=(l+r)/2; ll value=arr[mid]; if(value>key){ r=mid-1; }else if(value==key){ return mid; }else{ l=mid+1; } } return -1; } ll power(ll x,ll y,ll p){ ll res = 1; x = x % p; if (x == 0) return 0; while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } const int N=1e5+5; const ll INF=1e18; void solve(){ ll a,b,c; cin >> a >> b >> c; a*=a; b*=b; c*=c; if((a+b)<(c)){ cout << "Yes\n"; }else{ cout << "No\n"; } } int main(){ fast; //freopen ("auxiliary.in","r",stdin); //freopen ("auxiliary.out","w",stdout); int test=1; //cin >> test; while(test--){ solve(); } return 0; }
#include <bits/stdc++.h> #pragma GCC target("arch=skylake-avx512") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ld = long double; using Graph = vector<vector<int>>; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i = 1; i < (int)(n+1); ++i) const long long INF = 1LL<<60; const long long MOD = 1000000007; const long long mod = 998244353; int ri() { int n; scanf("%d", &n); return n; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N,M,Q; cin>>N>>M>>Q; vector<pair<int,int>>W(N); vector<int>X(M); rep(i,N){ cin>>W[i].second; cin>>W[i].first; } rep(i,M){ cin>>X[i]; } int ans=0; sort(W.rbegin(),W.rend()); rep(i,Q){ int L,R; cin>>L>>R; int score=0; int cn=M-(R-L+1); if(cn!=0){ vector<int>box(cn); vector<bool>H(cn,false); cn=0; rep(j,M){ if(L-1<=j&&j<=R-1){} else{ box[cn]=X[j]; cn++; } } sort(box.begin(),box.end()); rep(j,N){ rep(k,cn){ if(H[k]==false&&W[j].second<=box[k]){ H[k]=true; score+=W[j].first; k=cn; } } } } cout<<score<<endl; } }
/* _|_| _| _| _| _| _| _| _|_| _|_|_|_| _| _| _| _| _| _|_| _| _| _|_| _| _| _| _| _| _| _| _| _|_| _| _|_|_|_| _|_| _| _| */ #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<vector> #include<queue> #include<set> //#define ls (rt<<1) //#define rs (rt<<1|1) #define vi vector<int> #define pb push_back #define mk make_pair #define pii pair<int,int> #define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;i++) #define fi first #define se second typedef long long ll; using namespace std; const int maxn=1<<19; int dp[maxn];//max use side bool ck[maxn]; int g[20][20]; int cnt[maxn]; int main(){ ios::sync_with_stdio(0); int n,m;cin>>n>>m; rep(i,1,m){ int a,b;cin>>a>>b; g[a][b]=g[b][a]=1; } rep(i,1,n)g[i][i]=1; memset(dp,0x3f,sizeof(dp)); rep(mask,0,1<<n){ vi po; rep(j,1,n){ if(mask&(1<<(j-1)))po.pb(j); } ck[mask]=1; for(int i=0;i<po.size();i++) for(int j=i;j<po.size();j++) if(!g[po[i]][po[j]]){ck[mask]=0;break;} if(ck[mask]){ dp[mask]=1; } } rep(i,1,n)dp[1<<(i-1)]=1; rep(mask,0,1<<n){ int left=(1<<(n))-1; left^=mask;int bl=left; for(int i=bl;i;i=(i-1)&bl){ if(ck[i])dp[i|mask]=min(dp[i|mask],dp[mask]+1); } } cout<<dp[(1<<(n))-1]; return 0; }
#include<bits/stdc++.h> using namespace std; int modpow(int p, long long n, int mod) { if (n == 0) return 1; int t = modpow(p, n / 2, mod); return n % 2 == 0 ? 1LL * t * t % mod : 1LL * t * t % mod * p % mod; } int main() { long long N; int M; cin >> N >> M; cout << (modpow(10, N, M * M) / M) % M << endl; }
#include<bits/stdc++.h> #define int long long #define pb emplace_back #define INF 0x3f3f3f3f #define f first #define s second #define FOR(i, n) for(int i = 1; i <= n; i ++) #define FFOR(i, a, b) for(int i = a; i <= b; i ++) #define pii pair<int, int> #define mod 1000000007 #define all(x) x.begin(), x.end() #define mem(x, a) memset(x, a, sizeof(x)) #define loli ios_base::sync_with_stdio(0), cin.tie(0) using namespace std; const int maxn = 1e6 + 50; int n, m; unordered_set<int> st; vector<int> vc; signed main(){ loli; cin >> n >> m; int t = 1LL; st.reserve(10000000); while(1) { if(st.find(t) != st.end()) { break; } else { st.insert(t); vc.pb(t); } t = t * 10LL % (m * m); } //cout << t << "\n"; //cout << st.size() << "\n"; //for(int i : vc) cout << i << " "; cout << "\n"; int i = 0; for(; i < (int)vc.size(); i ++) if(vc[i] == t) break; //cout << "aaa" << i << "\n"; if(n < i) { cout << vc[n] / m << "\n"; return 0; } n -= i; int ssz = (int)vc.size() - i; int tans = vc[n % ssz + i] / m; cout << tans << "\n"; }
//x<<y=x*2^y,x>>y=x/2^y //1e5+3 is prime // in a matrix, all elements of a primary diagonal have constant diff of coordinates // and a secondary diagonal has constant sum of coordinates //use ll() for using an integer in self-built fn //(x&-x) bitwise and of (x and its 2's complement) returns (last set bit) //eg if x=1010 then it will return 0010 // careful dont print empty container(run time error) //v.erase O(n) //use ("\n") instead of endl as endl flushes the output buffer //every time so takes more time than \n (TLE) //stoll() and to_string((less than 1e19)) //INT_MAX is approx 3e10 //For sets use set_name.lower_bound(x)(strictly O(logn)) //NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is O(n)); #include<bits/stdc++.h> typedef long long int ll; #define ull unsigned long long int #define lld long double #define endl "\n" #define fi first #define sec second #define sp setprecision #define lb lower_bound #define ub upper_bound #define For(i, a, b) for(long long int i = (a); i <= (b); i++) #define Forr(i, a, b) for(long long int i = (a); i >= (b); i--) #define pb push_back #define mp(a,b) make_pair(a,b) #define vll vector<ll> #define pll pair<ll,ll> #define plld pair<lld,lld> #define vlld vector<lld> #define vi vector<int> #define vch vector<char> #define sll set<ll> #define sch set<ch> #define vpll vector< pair<ll,ll> > #define vpii vector< pair<int,int> > #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define mll map<ll, ll> #define mcll map<char,ll> #define sz(container) ll((container).size()) #define fill(a,b) memset(a, b, sizeof(a)) #define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) using namespace std; #define PI 3.1415926535897932384626 const ll mod=1000000007; //const ll dx[4]={-1, 0, 1, 0} , dy[4]={0, 1, 0, -1}; const ll dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1} , dyy[8]={0, 1, 1, 1, 0, -1, -1, -1}; string ds="RLDU"; /*************************************** struct cmp { bool operator() (const pll &a, const pll &b) const { ll lena = a.sec - a.fi + 1; ll lenb = b.sec - b.fi + 1; if (lena == lenb) return a.fi < b.fi; return lena > lenb; } }; ********************************/ ll lcm(ll a, ll b) { ll g=__gcd(a, b); return a/g*b; } ll binpow(ll a, ll b,ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll Pow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a ; b >>= 1; } return res; } ll modinv(ll n,ll m) { return binpow(n, m - 2,m); } /*******************************************coding********/ int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast_io; ll T=1;//cin>>T; For(i,1,T) { ll n;cin>>n; vector<tuple<ll,ll,ll>>v; For(j,1,n) { ll x,y,z;cin>>x>>y>>z; v.pb({x,y,z}); } ll dis[n][n]; ll N=1ll<<n; ll cost[n][N]={}; For(j,0,N-1) For(k,0,n-1) cost[k][j]=1e15; cost[0][1]=0; For(j,0,n-1) { For(k,0,n-1) { auto [x1,y1,z1]=v[j]; auto [x2,y2,z2]=v[k]; dis[j][k]=abs(x1-x2)+abs(y1-y2)+max(0ll,z2-z1); } } for(ll j=1;j<N;j+=2) { For(i,1,n-1) { if(j&(1ll<<i)) continue; For(k,0,n-1) { cost[i][j|(1ll<<i)]=min(cost[i][j|(1ll<<i)],cost[k][j]+dis[k][i]); } } } ll ans=1e15; For(j,1,n-1) ans=min(ans,cost[j][N-1]+dis[j][0]); cout<<ans; } }
#include <bits/stdc++.h> using namespace std; int dist(vector<array<int, 3>> &vp, int i, int j) { auto [a, b, c] = vp[i]; auto [p, q, r] = vp[j]; return abs(p - a) + abs(q - b) + max(0, r - c); }; int solve(vector<array<int, 3>> &vp, int s, int cur, vector<vector<int>> &memo) { if (s + 1 == (1 << vp.size())) { return dist(vp, cur, 0); } else if (memo[s][cur] < 0) { int ans = -1; for (int i = 0; i < vp.size(); ++i) { if (!(s & (1 << i))) { int a = solve(vp, s | (1 << i), i, memo) + dist(vp, cur, i); ans = ans < 0 || a < ans ? a : ans; } } memo[s][cur] = ans; } return memo[s][cur]; } int main(int argc, const char *argv[]) { int n; cin >> n; vector<array<int, 3>> vp(n); for (int i = 0; i < n; ++i) { cin >> vp[i][0]; cin >> vp[i][1]; cin >> vp[i][2]; } vector<vector<int>> memo(1 << n, vector<int>(n + 1, -1)); int ans = solve(vp, 0, 0, memo); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define mp make_pair #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define sz(x) int(x.size()) #define pcnt __builtin_popcountll template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;} template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;} template <typename T> void print(T a) {cout << a << ' ';} template <typename T> void printe(T a) {cout << a << endl;} template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;} template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;} template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);} template <typename T> using vc = vector<T>; template <typename T> using vv = vc<vc<T>>; template <typename T> using vvv = vc<vv<T>>; using ll = long long; using P = pair<int, int>; using Pl = pair<ll, ll>; using vi = vc<int>; using vvi = vv<int>; using vl = vc<ll>; using vvl = vv<ll>; vi di = {-1, 1, 0, 0, -1, -1, 1, 1}; vi dj = { 0, 0, -1, 1, -1, 1, -1, 1}; int main () { int n; cin >> n; vvl a(n, vl(5)); rep(i, n) rep(j, 5) cin >> a[i][j]; ll ok = 0, ng = 1e10; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; vv<bool> flag(n, vc<bool>(5)); rep(i, n) rep(j, 5) flag[i][j] = (a[i][j] >= mid); vi dp(1<<5, 10000); dp[0] = 0; rep(i, n) { rep(s, 1 << 5) { int t = s; rep(j, 5) if (flag[i][j]) t |= (1 << j); chmin(dp[t], dp[s] + 1); } } if (dp[(1<<5) - 1] <= 3) ok = mid; else ng = mid; } cout << ok << endl; return 0; }
#include <algorithm> #include <iostream> #include <deque> #include <climits> #include <cmath> #include <map> using namespace std; struct S { int left; int right; }; int main() { int N; std::cin >> N; std::map<int, S> colors; for (int i = 0; i < N; i++) { int x, c; std::cin >> x >> c; if (colors.find(c) == colors.end()) { S s; s.left = x; s.right = x; colors[c] = s; continue; } if (x < colors[c].left) { colors[c].left = x; } if (x > colors[c].right) { colors[c].right = x; } } int left = 0; int64_t cleft = 0; int right = 0; int64_t cright = 0; for (auto pair : colors) { auto it = pair.second; // cout << pair.first << ' ' << it.left << ' ' << it.right << endl; int64_t cleft1 = cleft + abs(left - it.right) + abs(it.right - it.left); int64_t cleft2 = cright + abs(right - it.right) + abs(it.right - it.left); int64_t cright1 = cleft + abs(left - it.left) + abs(it.right - it.left); int64_t cright2 = cright + abs(right - it.left) + abs(it.right - it.left); cleft = cleft1 < cleft2 ? cleft1 : cleft2; cright = cright1 < cright2 ? cright1 : cright2; left = it.left; right = it.right; } cleft += abs(left); cright += abs(right); int64_t result = cleft < cright ? cleft : cright; std::cout << result << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> #define pb push_back #define mp make_pair #define vi vector<int> #define vll vector<ll> const int maxn = 1e6 + 5; const int mod = 1e9 + 7; int a[maxn]; int main() { int n; cin >> n; double ans = 0; for (int i = 1 ; i < n ; i++){ ans += 1.0 * n / i; } printf("%.10f\n" , ans); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define MV 200001 #define LMV 21 #define ff first #define ss second #define pb push_back #define eb emplace_back #define emp emplace #define mp make_pair #define ins insert #define sz(x) (int)x.size() #define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); } void whi(istream_iterator<string> it) { cerr<<"\n"; } template<typename T, typename... Args> void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); } void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);} void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);} void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);} typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<PII, int> PPII; typedef pair<PLL, ll> PPLL; typedef map<int, int> MII; const int MOD = 1000000007; const ll INF = 4e18; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; struct h_llint { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct h_pair{ size_t operator()(const PLL&x)const{ return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32)); } }; typedef map<ll, ll> MLL; typedef map<PII, int> MPII; typedef map<PLL, ll> MPLL; typedef set<int> SI; typedef set<ll> SL; //ordered_set = order_of_key(.) //ordered_set = find_by_order(.) typedef ordered_set<int> OSI; typedef ordered_set<ll> OSL; typedef ordered_multiset<int> MOSI; typedef ordered_multiset<ll> MOSL; typedef unordered_map<ll, int, h_llint> UMLI; typedef unordered_map<ll, ll, h_llint> UMLL; typedef unordered_map<PLL, int, h_pair> UMPI; typedef unordered_map<PLL, ll, h_pair> UMPL; int ar[MV]; ll arr[MV]; void solve(int T) { ll n; cin>>n; ld rz = 0; for(int i=1;i<n;i++) rz += ((ld)n/(ld)i); SETF(); cout<<setprecision(10)<<rz<<"\n"; UNSETF(); return; } int main(void) { FLASH(); //freopen("cowjog.in", "r", stdin); //freopen("cowjog.out", "w", stdout); int T; T = 1; #ifndef ONLINE_JUDGE time_t time_t1, time_t2; time_t1 = clock(); #endif //cin>>T; while(T--) solve(T); #ifndef ONLINE_JUDGE time_t2 = clock(); SETF(); cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n"; UNSETF(); #endif return 0; }
#include <bits/stdc++.h> #define rep(a,n) for (ll a = 0; a < (n); ++a) using namespace std; //using namespace atcoder; using ll = long long; typedef pair<ll,ll> P; typedef pair<ll,P> PP; typedef vector<vector<int> > Graph; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return 1; } return 0; } const ll INF = 1e18; #define SZ(x) ((int)(x).size()) #define debug(v) cout<<#v<<": ",prt(v); template <typename A,typename B> inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";} template <typename A,typename B,typename C> inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";} inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';} template <typename T> inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template<typename T> inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } } template <typename T> inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} /* aはsortしておく 最後に残る可能性がある整数 a1~anのうちいずれかの約数(この時点で1つ以上のaの要素の公約数になることは確定する) a1以下 上の条件を満たす整数を全列挙 kが最後に残るためにはaから取り出したいくつかの要素の最大公約数になることがあれば良い これはaの要素のうちkの倍数のものをみていき、kで割った商の集合の最大公約数が1になれば良い 計算量 約数全列挙:O(N*(maxA)^1/2) 約数の個数の最大:2e6くらい?でも直感的にはもっと少なそう<-ダメだった */ /* divisor(n) 入力:整数 n 出力:nのすべての約数 計算量:O(√n) */ set<ll>st; vector<ll>a; map<ll,ll>mp; void divisor(ll n){ vector<ll>ret; for(ll i=1;i*i<=n;i++){ if(n%i==0){ ret.push_back(i); if(i*i!=n)ret.push_back(n/i); } } for(auto x:ret){ if(x>a[0])continue; mp[x]=__gcd(mp[x],n); //st.insert(x); } return; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll n; cin >> n; a.resize(n); rep(i,n)cin>>a[i]; sort(a.begin(),a.end()); rep(i,n){ divisor(a[i]); //debug(mp); } ll ans = 0; /* for(auto x:st){ ll g = 0;//大丈夫かな? rep(i,n){ if(a[i]%x==0){ g = __gcd(a[i]/x,g); } } if(g==1)ans++; } */ for(auto p:mp){ if(p.second==p.first)ans++; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long void test(){ int n; cin >> n; vector<int> a(n), b(n); map<int,int> mp; for(int &x:a) cin >> x; for(int &x:b) cin >> x; for(int i = 0; i < n; i++){ for(int x = a[i]; x <= b[i]; x++){ mp[x]++; } } int ans = 0; for(int i = 0; i <= 1000; i++){ if(mp[i] == n){ ++ans; } } cout << ans; } signed main(){ ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout); #endif int testcases = 1; // cin >> testcases; while(testcases-->0){ test(); } }
//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() template<class T> void dmp(T a){ rep(i,a.size()) cout << a[i] << " "; cout << endl; } template<class T> bool chmax(T&a, T b){ if(a < b){ a = b; return 1; } return 0; } template<class T> bool chmin(T&a, T b){ if(a > b){ a = b; return 1; } return 0; } template<class T> void g(T &a){ cin >> a; } template<class T> void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 1000000007;//998244353 template<class T> void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } int n; ll a[200005]; int main(){ cin >> n; rep(i, n) cin >> a[i]; sort(a, a+n); ll ans = 0; rep(i, n-1){ ans += 1LL*(i+1)*(n-1-i)*(a[i+1]-a[i]); } cout<<ans<<endl;}
//#undef DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; const ll llinf = (1ll<<61)-1; #define sz(a) int(a.size()) #define all(x) begin(x), end(x) #ifdef DEBUG const int DEBUG_END = 26; #define DOS cout #include <debug.h> #else #define bug(args...) void() #define cbug(a, args...) #endif #define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b)); #define fi first #define se second const int inf = 1000000007, N=200005; int TC = 1, CN, n, a[N]; signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11), cout.setf(ios::fixed); //cin >> TC;// inputAll + resetAll -> solution auto kase = [&]()->void { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a+1, a+1+n); ll ans = 0, ps = 0; for (int i = 1; i <= n; i++) { ans += ll(i-1)*a[i]-ps; ps += a[i]; } cout << ans << '\n'; }; while (CN++!=TC) kase(); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vll = vector<ll>; using vvll = vector<vll>; using vc = vector<char>; using vcc = vector<vc>; using Pii = pair<int,int>; using ld = long double; #define rep(i,n) for(int i = 0; i<(int)n; i++) #define per(i,n) for(int i = n-1; i>=0; i--) #define mysort(V) sort(V.begin(),V.end()) #define myreverse(V) reverse(V.begin(),V.end()) #define myunique(V) V.erase( unique(V.begin(), V.end()), V.end() ); #define print(s) cout << s << endl; const ld PI = 3.14159265358979; long long powll(long long x, long long n){ if(n == 0) return 1; else return powll(x,n-1)*x; } long long gcd(long long a, long long b){ if(a%b==0) return b; else return gcd(b,a%b); } int main(){ int N; cin >> N; ll ans = 0; for(int i = 0; i<N; i++){ ll a,b; cin >> a >> b; ans += b*(b+1)/2; ans -= (a-1)*a/2; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { long N, A, B, Ans=0; cin >> N; rep(i, N){ cin >> A >> B; if((B-A+1)%2==0) Ans+=(A+B)*((B-A+1)/2); else Ans+=(A+B)*((B-A+1)/2)+(A+B)/2; } cout << Ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> Pii; typedef pair<int, ll> Pil; typedef pair<ll, ll> Pll; typedef pair<ll, int> Pli; typedef vector < vector<ll> > Mat; #define fi first #define se second const ll MOD = 1e9 + 7; const ll MOD2 = 998244353; const ll MOD3 = 1812447359; const ll INF = 1ll << 62; const double PI = 2 * asin(1); void yes() {printf("yes\n");} void no() {printf("no\n");} void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} ll Pow(ll A, ll N){ if (N == 0) return 1; if (N % 2 == 0) return Pow(A * A % MOD, N / 2); else return Pow(A * A % MOD, N / 2) * A % MOD; } int H, W; ll K; char S[2005][2005]; ll cnt[2005][2005], ans[2005][2005]; int main(){ cin >> H >> W; for (int i = 1; i <= H; i++){ for (int j = 1; j <= W; j++){ cin >> S[i][j]; if (S[i][j] == '.') K++; } } for (int i = 1; i <= H; i++){ queue <int> que; int now = 0; for (int j = 1; j <= W + 1; j++){ if (S[i][j] == '.') { now++; que.push(j); } else{ while (!que.empty()){ cnt[i][que.front()] += now; que.pop(); } now = 0; } } } for (int j = 1; j <= W; j++){ queue <int> que; int now = 0; for (int i = 1; i <= H + 1; i++){ if (S[i][j] == '.') { now++; que.push(i); } else{ while (!que.empty()){ cnt[que.front()][j] += now - 1; que.pop(); } now = 0; } } } for (int i = 1; i <= H; i++){ for (int j = 1; j <= W; j++){ if (S[i][j] != '.') continue; ans[i][j] = Pow(2, K) - Pow(2, K - cnt[i][j]); ans[i][j] %= MOD; if (ans[i][j] < 0) ans[i][j] += MOD; } } ll res = 0; for (int i = 1; i <= H; i++){ for (int j = 1; j <= W; j++){ res += ans[i][j]; res %= MOD; } } cout << res << endl; return 0; }
#include <iostream> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <set> #include <queue> #include <map> using namespace std; typedef long long int ll; typedef pair <int,int> pii; typedef pair<ll,ll> pll; /*bool compare_function(const pair<int, int> a, const pair<int, int> b) { return a.first < b.first; }*/ // use case //sort(V.begin(), V.end(), compare_function); /*struct compare { bool operator ()(const pair<int, int> &a, const pair<int, int> &b) { if (a.second-a.first==b.second-b.first) { return a.first>b.first; } return a.second-a.first < b.second-b.first;} }; priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/ /*void print1(vector <int> X) { for (int i=0; i<X.size(); i++) { cout<<X[i]<<" "; } cout<<endl; return; } void print2(vector <vector <int>> X) { for (int i=0; i<X.size(); i++) { for (int j=0;j<X[i].size();j++) { cout<<X[i][j]<<" ";} cout<<endl;} return; }*/ void update(vector <vector <int>> &A, vector <vector <pii>> &B, int r, int c, int h, int w) { //cout<<r<<" "<<c<<" "<<h<<" "<<w<<endl; if (r==h-1) { B[r][c].first=B[r][c+1].second+A[r][c+1]; B[r][c].second=B[r][c+1].first; //cout<<B[r][c].first<<endl; return; } if (c==w-1) { B[r][c].first=B[r+1][c].second+A[r+1][c]; B[r][c].second=B[r+1][c].first; return; } int a=B[r+1][c].second+A[r+1][c]-B[r+1][c].first; int b=B[r][c+1].second+A[r][c+1]-B[r][c+1].first; if (a>b) { B[r][c].first=B[r+1][c].second+A[r+1][c]; B[r][c].second=B[r+1][c].first; return; } B[r][c].first=B[r][c+1].second+A[r][c+1]; B[r][c].second=B[r][c+1].first; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int h,w; cin>>h>>w; vector <string> X(h); for (int i=0;i<h;i++) {cin>>X[i];} vector <vector <int>> A(h,vector <int>(w,1)); for (int i=0;i<h;i++) { for (int j=0;j<w;j++) { if (X[i][j]=='-') {A[i][j]=-1;} //cout<<i<<" "<<j<<" "<<A[i][j]<<endl; } } vector <vector <pii>> B(h,vector <pii>(w,{0,0})); for (int r=h-1;r>=0;r--) { for (int c=w-1;c>=0;c--) { if (r<h-1 || c<w-1) {update(A,B,r,c,h,w);} //cout<<r<<" "<<c<<" "<<B[r][c].first<<" "<<B[r][c].second<<endl; } } int a=B[0][0].first-B[0][0].second; if (a>0) {cout<<"Takahashi"<<endl;} if (a==0) {cout<<"Draw"<<endl;} if (a<0) {cout<<"Aoki"<<endl;} return 0; }
#include <iostream> #include <iomanip> #include <cctype> #include <string> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define ALL(v) v.begin(), v.end() using namespace std; int main() { ios::sync_with_stdio(false); int n, count = 0; cin >> n; rep2(i, 1, n + 1){ int n10 = i; while(n10 != 0){ if(n10 % 10 == 7) break; n10 /= 10; } int n8 = i; while(n8 != 0){ if(n8 % 8 == 7) break; n8 /= 8; } if(n10 == 0 && n8 == 0) count++; } cout << count << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--) #define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" template<class T> void chmax(T &a, const T b){ a = max(a, b); } template<class T> void chmin(T &a, const T b){ a = min(a, b); } long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void solve() { ll l, r; cin >> l >> r; ll val = r - 2 * l + 1; ll ans = ((val > 0) ? (1 + val) * val / 2 : 0); cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll t; cin >> t; rep(i, t) solve(); return 0; }
#include <iostream> using namespace std; int main(void){ // Your code here! int n; cin >> n; if(n % 2 == 0){ cout << "White" << endl; } else{ cout << "Black" << endl; } }
#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; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // oset Operations: order_of_key(k) -> Number of elements strictly smaller than k, find_by_order(k) -> kth element in the set #define ull unsigned long long int #define ll long long int #define pb push_back #define eb emplace_back #define all(x) x.begin(),x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vb vector<bool> #define vd vector<double> #define vll vector<ll> #define vpii vector<pii> #define vpll vector<pll> #define vvi vector<vi> #define vvll vector<vll> #define vvb vector<vb> #define vs vector<string> #define pull pair<ull, ull> #define F first #define S second #define m_p make_pair const long double PI = 3.141592653589793238462643383279502884197169399; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res.push_back(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) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 0 #endif // Find Set LSB = (x&(-x)), isPowerOfTwo = (x & (x-1)) const int mod = 1e9+7; void solve(){ int n; cin >> n; cout << (n&1 ? "Black" : "White"); } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(10); int tt=1; // cin >> tt; while(tt--){ solve(); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i,n) for (int i = (int)(n)-1; i>=0; --i) #define For(i,a,b) for(auto i=(a);i<=(b);++i) #define rFor(i,a,b) for(auto i=(b);i>=(a);--i) #define call(var) cerr<<#var<<"="<<var<<"\n"; #define dup(x,y) (((x)+(y)-1)/(y)) #define all(x) (x).begin(),(x).end() using ll=long long; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using Graph = vector<vi>; using P = pair<int,int>; const int MOD=(int)1e9+7; const int INF=(int)1e9; const ll LINF=(ll)1e18; const double pi = acos(-1); template<class t,class u> inline bool chmax(t&a,u b){if(a<b){a=b;return true;}return false;} template<class t,class u> inline bool chmin(t&a,u b){if(b<a){a=b;return true;}return false;} template<class T> istream& operator>>(istream& is, vector<T>& a){rep(i,a.size()) is >> a[i]; return is;} template<class T> ostream& operator<<(ostream& os, vector<T>& a){rep(i,a.size()) os << a[i] << " "; return os;} template<class T, class U> istream& operator>>(istream& is, pair<T,U>& p){is >> p.first >> p.second; return is;} template<class T, class U> ostream& operator<<(ostream& os, const pair<T,U>& p){os << "("<< p.first << "," << p.second << ")"; return os;} template<class T, class U> ostream& operator<<(ostream& os, const map<T,U>& mp){for(auto& t : mp) os << t.first << " " << t.second << "\n"; return os;} int main(){ int n;cin >> n; vi a(n); cin >> a; int ma = 0, ans = a[0]; for(int i=2;i<=1000;i++){ int cnt = 0; rep(j,n){ if(a[j]%i == 0) ++cnt; } if(ma < cnt){ ma = cnt; ans = i; } } cout << ans << endl; }
#include<iostream> #include<vector> #include<functional> #include<algorithm> using namespace std; int main(){ int n; cin>>n; vector<int> a(n); for(int i=0;i<n;i++){ cin>>a[i]; } int ans=2; int max=0; for(int i=2;i<=1000;i++){ int counter=0; for(int j=0;j<n;j++){ if(a[j]%i==0){ counter++; } } if(max<counter){ max=counter; ans=i; } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long int #define mod 1000000007 #define FIO ios_base::sync_with_stdio(false);cin.tie(NULL); int main() { FIO; int t,n,i,j; cin>>n; int x[n]; int y[n]; for(i=0;i<n;i++){ cin>>x[i]>>y[i]; } int cnt=0; for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++){ float slope = (float)(y[j]-y[i])/(float)(x[j]-x[i]); if(slope>=-1 && slope<=1) cnt++; } } cout<<cnt; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) #define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) #define REP(i, n) FOR(i, 0, (n)-1) #define sqr(x) ((x) * (x)) #define all(x) (x).begin(), (x).end() #define reset(x, y) memset(x, y, sizeof(x)) #define uni(x) (x).erase(unique(all(x)), (x).end()) #define BUG(x) cerr << #x << " = " << (x) << endl #define pb push_back #define eb emplace_back #define mp make_pair #define _1 first #define _2 second #define chkmin(a, b) a = min(a, b) #define chkmax(a, b) a = max(a, b) const int maxn = 212345; int h, w, m; int reach[maxn], fen[maxn]; bool vis[maxn]; vector<int> G[maxn]; void add(int x, int v) { while (x <= w) { fen[x] += v; x += x & -x; } } int query(int x) { int ret = 0; while (x > 0) { ret += fen[x]; x -= x & -x; } return ret; } int main() { #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> h >> w; FOR(i, 1, h) reach[i] = w; cin >> m; while (m--) { int x, y; cin >> x >> y; G[x].eb(y); chkmin(reach[x], y - 1); } ll ans = 0; bool f = true; ans += reach[1]; FOR(i, 1, reach[1]) add(i, 1), vis[i] = true; FOR(i, 2, h) { for (auto y : G[i]) if (vis[y]) { add(y, -1); vis[y] = false; } if (!vis[1]) f = false; ans += reach[i] * f + query(w) - query(f ? reach[i] : 0); } cout << ans; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; #define rep(i,n) for(int i=0;i<(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep1(i,n) for(int i=(n);i>0;i--) #define fore(i_in,a) for (auto& i_in: a) #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define cnts(x,c) count(all(x),c) #define fio() cin.tie(nullptr);ios::sync_with_stdio(false); #define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl template<class T> bool chmax(T &a, const T &b) {if (a<b) {a = b; return 1;} return 0;} template<class T> bool chmin(T &a, const T &b) {if (a>b) {a = b; return 1;} return 0;} template<class T> void print(const T &t) {cout << t << "\n";} const ll INF = 1LL << 62; const int iINF = 1 << 30; ll n,m,k; int main() { fio(); cin>>k>>n>>m; vl a(k),c(k); fore(x,a) cin>>x; vector<pair<ll,int>> b; rep(i,k) { c[i]=a[i]*m/n; b.emplace_back(c[i]*n-a[i]*m,i); } sort(all(b)); ll tmp=0; fore(x,c) tmp+=x; ll res=m-tmp; rep(i,res) { auto j=b[i].second; c[j]++; } rep(i,k) cout<<c[i]<<" \n"[i==k-1]; }
#include<iostream> #include<string> #include<vector> #include<set> #include<iomanip> #include<algorithm> #include<cmath> #include<bitset> #include<queue> #include<stack> #include<utility> #include<cstdlib> #include<cstdio> #include<map> #include<unordered_set> #include<unordered_map> #include<list> #include<tuple> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define FOR(i, a, b) for(ll i=(a); i<(b); ++i) #define REP(i, n) FOR(i, 0, n) #define NREP(i, n) FOR(i, 1, n+1) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } struct Edge{ ll to, cost; Edge(ll t, ll c) : to(t), cost(c) { } }; using Graph = vector<vector<Edge>>; ll MODpow(ll x, ll n, ll mod){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n >>= 1; } return res; } const ll MOD=1e9+7; const ll nmax=200000+10; ll N; ll color[nmax][60]; ll cnt1[60]; ll seen[nmax]; Graph G(nmax); ll solve(){ queue<ll> q; q.push(0); REP(i,60) color[0][i]=0; while(!q.empty()){ ll v=q.front(); q.pop(); seen[v]=1; for(auto e:G[v]){ if(seen[e.to]) continue; ll et=e.to, ec=e.cost; q.push(e.to); REP(i,60){ bool eci=ec&1; ec>>=1; color[et][i]=color[v][i]^eci; if(color[et][i]) cnt1[i]++; } } } ll res=0,rec=1; REP(i,60){ ll mid1=rec; rec=rec*2%MOD; ll mid2=(N-cnt1[i])*cnt1[i]%MOD; ll mid=mid1*mid2%MOD; res+=mid; res%=MOD; } return res; } int main(void){ cin>>N; REP(i,N-1){ ll u,v,w; cin>>u>>v>>w; u--,v--; G[u].push_back(Edge{v,w}); G[v].push_back(Edge{u,w}); } ll res=solve(); cout<<res<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double dbl; typedef float flt; #define for0(i, n) for(ll i=0; i<n; i++) #define for1(i, n) for(ll i=1; i<=n; i++) #define forab(i, a, b) for(ll i=a; i<=b; i++) #define clr0(a) memset(a, 0, sizeof(a)) void solve() { ll a, b; cin >> a >> b; if (a+b>=15 && b>=8) { cout << 1 << endl; } else if (a+b>=10 && b>=3) { cout << 2 << endl; } else if (a+b>=3) { cout << 3 << endl; } else { cout << 4 << endl; } } void testcase() { ll t; cin >> t; while (t--) { solve(); } } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //testcase(); solve(); return 0; }
#include<bits/stdc++.h> #define endl "\n" using namespace std; typedef long long ll; int main() { int a , b ; cin >> a >> b ; if((a+b)>=15 && b>=8) { cout << 1 ; /// "ice cream"; return 0; } if((a+b)>=10 && b>=3) { cout << 2 ; /// "1 ice milk"; return 0; } if((a+b)>=3) { cout << 3 ; // "lacto milk"; return 0; } cout << 4 ; /// "flavored ice"; return 0; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=998244353,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; int p[200001],r[200001]; void init(){rep(i,200001)p[i]=i,r[i]=0;} int find(int x){return (p[x]==x)?x:(p[x]=find(p[x]));} void unite(int x,int y) { x=find(x),y=find(y); if(x==y)return; if(r[x]<r[y])p[x]=y; else{p[y]=x;if(r[x]==r[y])r[x]++;} } bool same(int x,int y){return find(x)==find(y);} void Main() { ll n,m; cin >> n >> m; ll a[n][n]; rep(i,n)rep(j,n) R a[i][j]; ll d[n+1]; mem(d); d[0]=1; REP(i,1,n+1) d[i]=d[i-1]*i%MAX; init(); rep(i,n) { REP(j,i+1,n) { ll f1=1,f2=1; rep(k,n) { if(a[k][i]+a[k][j]>m) f1=0; if(a[i][k]+a[j][k]>m) f2=0; } if(f1) unite(i,j); if(f2) unite(i+n,j+n); } } ll x[n],y[n]; mem(x);mem(y); rep(i,n) { x[find(i)]++; y[find(i+n)-n]++; } ll d1=1,d2=1; rep(i,n) { d1*=d[x[i]]; d1%=MAX; d2*=d[y[i]]; d2%=MAX; } pr(d1*d2%MAX); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int,int> #define pll pair<long long,long long> #define pb push_back #define debug(x) cerr<<#x<<"="<<x<<endl #define pq priority_queue #define inf 0x3f #define rep(i,a,b) for (int i=a;i<(b);i++) #define MP make_pair #define SZ(x) (int(x.size())) #define ll long long #define mod 1000000007 #define ALL(x) x.begin(),x.end() void inc(int &a,int b) {a=(a+b)%mod;} void dec(int &a,int b) {a=(a-b+mod)%mod;} int lowbit(int x) {return x&(-x);} ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;} const int maxn=110; int n,m; bool ans[maxn][maxn]; vector <int> edge[maxn]; pii e[maxn*maxn]; int c[maxn]; bool vis[maxn]; void dfs(int u) { vis[u]=true; for(int v:edge[u]) { if (ans[u][v]||ans[v][u]) continue; ans[u][v]=true; if (!vis[v])dfs(v); } } int main() { // freopen("input.txt","r",stdin); std::ios::sync_with_stdio(false);cin.tie(); cin>>n>>m; rep(i,0,m) cin>>e[i].fi>>e[i].se; rep(i,1,n+1) cin>>c[i]; memset(ans,0,sizeof(ans)); rep(i,0,m) { if (c[e[i].fi]==c[e[i].se]) edge[e[i].fi].pb(e[i].se),edge[e[i].se].pb(e[i].fi); else if (c[e[i].fi]>c[e[i].se]) ans[e[i].fi][e[i].se]=true; else ans[e[i].se][e[i].fi]=true; } rep(i,1,n+1) dfs(i); rep(i,0,m) { if (ans[e[i].fi][e[i].se]) cout<<"->\n"; else cout<<"<-\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; int power(long long int x,long long int y,long long int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; //cin>>t; while(t--){ int n; cin>>n; string s; set<string> st; string ans="satisfiable"; for(int i=0;i<n;i++){ cin>>s; reverse(s.begin(),s.end()); string search,temp=s; if(s[s.size()-1]=='!'){ s.pop_back(); if(st.find(s)!=st.end()&&ans=="satisfiable"){ reverse(s.begin(),s.end()); ans=s; } } else{ s+='!'; if(st.find(s)!=st.end()&&ans=="satisfiable"){ reverse(temp.begin(),temp.end()); ans=temp; reverse(temp.begin(),temp.end()); } } st.insert(temp); } cout<<ans<<"\n"; } return 0; }
#include<stdio.h> #include<iostream> #include<vector> #include<math.h> #include<queue> #include<map> #include<algorithm> #include<string.h> #include<functional> #include<limits.h> #include<stdlib.h> #include<string> #include<unordered_map> #include <iomanip> using namespace std; #define intmax INT_MAX #define lmax LONG_MAX #define uintmax UINT_MAX #define ulmax ULONG_MAX #define llmax LLONG_MAX #define ll long long #define rep(i,a,N) for((i)=(a);(i)<(N);(i)++) #define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--) #define llfor ll i,j,k #define sc(a) cin>>a #define pr(a) cout<<a<<endl #define pY puts("YES") #define pN puts("NO") #define py puts("Yes") #define pn puts("No") #define pnn printf("\n") #define sort(a) sort(a.begin(),a.end()) #define reverse(a) reverse(a.begin(),a.end()) #define push(a,b) (a).push_back(b) #define llvec vector<vector<ll>> #define charvec vector<vector<char>> #define sizeoof(a,b) (a,vector<ll>(b)) #define llpvec vector<pair<ll,ll>> /*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;} /*最大公約数*/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;} /*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;} /*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;} /*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);} /*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);} /*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;} ll ans=0;llfor;/////////////////////////////////////////////////////////// int main(){ ll N; cin>>N; ll ans=0; if(N>=1000) ans+=N-999; if(N>=1000000) ans+=N-999999; if(N>=1000000000) ans+=N-999999999; if(N>=1000000000000) ans+=N-999999999999; if(N>=1000000000000000) ans+=N-999999999999999; cout<<ans<<endl; return 0;}
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define mp make_pair #define pb push_back #define pll pair<ll,ll> #define deb1(a) cout<<#a<<" = "<<(a)<<endl; #define deb2(a,b) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<endl; #define deb3(a,b,c) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<", "<<#c<<" = "<<(c)<<endl; ll INF = 1000000007; //-----------------// #define amin main //-----------------// ll dp[1005][1005]; ll arr[1005]; ll brr[1005]; ll solve(ll i,ll j) { if(dp[i][j]!=-1) return dp[i][j]; if(i==0 || j==0) return max(i,j); return dp[i][j] = min(solve(i-1,j)+1,min(solve(i,j-1)+1,solve(i-1,j-1) + ((ll)(arr[i]!=brr[j])))); } int amin() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); ll t,i,j; ll n,m; cin>>n>>m; for(i=1;i<=n;i++) cin>>arr[i]; for(j=1;j<=m;j++) cin>>brr[j]; for(i=0;i<=n;i++) { for(j=0;j<=m;j++) dp[i][j]=-1; } cout<<solve(n,m)<<endl; return 0; }
// ********************************************************************************* // * MURTAZA MUSTAFA KHUMUSI * // * NIT-DGP,CSE - 2019-2023 * // ********************************************************************************* #include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007; #define loop(i, a, n) for (int i = a; i < n; i++) #define loop1(i, b, n) for (int i = b; i <= n; i++) #define loopit(a) for (auto it = a.begin(); it != a.end(); it++) #define ms(a, b) memset(a, b, sizeof(a)) #define pb(a) push_back(a) #define MP make_pair #define pi pair<int, int> #define ff first #define ss second #define bloop(i, a, b) for (int i = a; i > b; i--) #define bloop1(i, a, b) for (int i = a; i >= b; i--) #define PQ priority_queue<int> pq; #define vi vector<int> #define si set<int> #define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq; #define io \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL); const int maxm = 100001; // ll m = 1e9+7; ll m = 998244353; vector<vi> adj; vi h, sz, in; vector<ll> fact; //modular arithmentic ll add(ll a, ll b, ll mod = m) { return ((a % mod) + (b % mod)) % mod; } ll mul(ll a, ll b, ll mod = m) { return ((a % mod) * (b % mod)) % mod; } ll sub(ll a, ll b, ll mod = m) { return ((a % mod) - (b % mod) + mod) % mod; } ll modmul(ll a, ll b, ll mod = m) { ll ans = 0; a = a % mod; while (b) { if (b & 1) ans = add(ans, a, mod); a = mul(a, 2, mod); b >>= 1; } return ans; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll fe(ll base, ll exp, ll mod = m) { ll ans = 1; while (exp) { if (exp & 1) ans = mul(ans, base, mod); base = mul(base, base, mod); exp >>= 1; } return ans; } ll modin(ll a, ll mod = m) { return fe(a, mod - 2, mod); } ll nCr(ll a, ll b, ll mod = m) { if (a < b) return 0; return mul(fact[a], mul(modin(fact[b], mod), modin(fact[a - b], mod))); } //graph theory void dfs_h_sz(int cur, int par) { h[cur] = h[par] + 1; sz[cur]++; for (const auto &v : adj[cur]) { if (v == par) continue; dfs_h_sz(v, cur); sz[cur] += sz[v]; } } void buildGraph(int n, int m) { adj = vector<vi>(n + 1); h = vector<int>(n + 1); sz = vector<int>(n + 1); in = vector<int>(n + 1); loop(i, 0, m) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } dfs_h_sz(1, 0); } struct cmp { bool operator()(const pi &a, const pi &b) { return a.ff < b.ff; } }; void solve() { int n,m; cin>>n>>m; int dp[n+1][m+1]; int in1[n+1],in2[m+1]; loop1(i,1,n)cin>>in1[i]; loop1(i,1,m)cin>>in2[i]; loop1(i,0,n){ loop1(j,0,m){ if(i==0) dp[i][j] = j; else if(j==0) dp[i][j] = i; else dp[i][j] = min(dp[i][j-1]+1,min(dp[i-1][j]+1,dp[i-1][j-1]+(in1[i]!=in2[j]))); } } cout<<dp[n][m]<<"\n"; } int main() { io; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int test; // cin >> test; test=1; while (test--) { solve(); } return 0; }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b){ ll res = 1; while(b){ if(b&1)res = res * x % MOD; x = x * x % MOD; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } ll in(){ ll res; scanf("%lld", &res); return res; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } } using namespace templates; ll func(){ int n = in(); int L = in(); vector<ll> start(n); vector<ll> goal(n); foreach(i,start)i=in(); foreach(i,goal)i=in(); vector<ll> x(n+1); vector<ll> y(n+1); x[0] = start[0]-1; y[0] = goal[0]-1; x.back() = L - start.back(); y.back() = L - goal.back(); rep(i,1,n){ x[i] = start[i]-start[i-1]-1; y[i] = goal[i]-goal[i-1]-1; } int last = 0; ll res = 0; rep(i,n+1){ while(last < n + 1 and x[last]==0)++last; if(y[i]==0)continue; ll sum = 0; int itr = last; while(sum < y[i]){ sum += x[itr++]; } if(sum != y[i])return -1; if(last < i and i < itr){ res += i - last; res += itr - i - 1; }else{ res += max(abs(i-last),abs(i-(itr-1))); } last = itr; } return res; } int main(){ cout << func() << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); ++i) using namespace std; #define MAX 1000000010 using ll = long long; int main(){ ll N, C; cin >> N >> C; vector<pair<ll,ll>>event; for(int i=0;i<N;i++){ ll a, b, c; cin >> a >> b >> c; event.push_back(make_pair(a,c)); event.push_back(make_pair(b+1,-c)); } sort(event.begin(), event.end()); ll cost = 0; ll ans = 0; ll beforeday = 0; ll beforecost = 0; for(auto day : event){ ans += beforecost * (day.first - beforeday); cost += day.second; beforecost = min(cost, C); beforeday = day.first; } cout << ans; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,tune=native") #define ll long long #define int ll #define ull unsigned ll #define ld long double #define rep(a) rep1(i,a) #define rep1(i,a) rep2(i,0,a) #define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++) #define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define pb push_back //#define inf 1010000000 #define inf 4000000000000000000 #define eps 1e-9 #define sz(a) ((int)a.size()) #define pow2(x) (1ll<<(x)) #define ceiling(a,b) (((a)+(b)-1)/(b)) #define print0(a) cout << (a) << ' ' #define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #ifdef i_am_noob #define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__) template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(T && x) {cerr << x << endl;} template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);} #else #define bug(...) 826 #endif template<typename T> void print(T && x) {cout << x << "\n";} template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);} const int Mod=1000000007,Mod2=998244353; const int MOD=Mod2; const int maxn=200005; //i_am_noob //#define wiwihorz int n,a[maxn],b[maxn],c[4]; bool vis[maxn]; void orzck(){ cin >> n; rep(n) cin >> a[i] >> b[i]; c[0]=min_element(a,a+n)-a; c[1]=max_element(a,a+n)-a; c[2]=min_element(b,b+n)-b; c[3]=max_element(b,b+n)-b; vector<int> vec; rep1(j,4){ vis[c[j]]=1; rep(n) if(!vis[i]) vec.pb(max(abs(a[i]-a[c[j]]),abs(b[i]-b[c[j]]))); } sort(all(vec)); print(vec[sz(vec)-2]); } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); #ifdef i_am_noob freopen("input1.txt","r",stdin); freopen("output1.txt","w",stdout); freopen("output2.txt","w",stderr); #endif cout << fixed << setprecision(15); int t; #ifdef wiwihorz cin >> t; #else t=1; #endif while(t--) orzck(); return 0; }
#include<bits/stdc++.h> #include<iostream> #include<set> #include<map> #include<iterator> using namespace std; ///defines..... #define ll long long #define lli long long int #define pb push_back #define mp make_pair #define RIP(i,n) for(ll 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' ///Constants........ const ll mod = 1e9+7; const int Max = 1e5+5; const ll IFN = 1e18+5; ///Chess moves......... int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right int dy[] = {+1, -1, +0, +0}; // int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move // int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; // int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; ///Knight's Move // int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; inline void norm(ll &a) {a%=mod; (a<0) && (a+=mod) ;} inline ll modAdd(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a+b)%mod;} inline ll modSub(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a-b)%mod;} inline ll modMul(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a*b)%mod;} ///Functions.......... ll bigmod(ll a, ll p, ll M) { p %= (M-1); ll result = 1LL; while(p>0LL){ if(p&1LL) result = result * a % M; a = a * a % M; p>>=1LL; } return result; } ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;} ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);} ll inverse(ll a, ll M){ if(gcd(a, M)==1) return bigmod(a, M-2, M) % M; return 1; } ll ncr(ll a, ll b){ ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans = ans * K % mod; ans = ans * inverse(L, mod) % mod;} return ans;} void F_I_O() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } /* void solve() { }*/ /// Code goes from here ::::::> int main() { /*#ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif*/ int t=1; //cin >> t; while(t--) { ll n; cin >> n; vector<pair<ll,ll>>v1,v2; ll a,b; RIP(i,n) { cin >> a >> b; v1.pb({a,i}); v2.pb({b,i}); } sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); vector<pair<ll,pair<ll,ll>>>v; v.pb({abs(v1[n-1].first-v1[0].first),{v1[n-1].second,v1[0].second}}); v.pb({abs(v1[n-1].first-v1[1].first),{v1[n-1].second,v1[1].second}}); v.pb({abs(v1[n-2].first-v1[0].first),{v1[n-2].second,v1[0].second}}); v.pb({abs(v2[n-1].first-v2[0].first),{v2[n-1].second,v2[0].second}}); v.pb({abs(v2[n-1].first-v2[1].first),{v2[n-1].second,v2[1].second}}); v.pb({abs(v2[n-2].first-v2[0].first),{v2[n-2].second,v2[0].second}}); sort(v.begin(),v.end()); reverse(v.begin(),v.end()); ll x1 = v[0].second.first; ll y1 = v[0].second.second; ll x2 = v[1].second.first; ll y2 = v[1].second.second; if(x1!=x2 || y1!=y2) { cout << v[1].first << nl; } else { cout << v[2].first << nl; } } ///<<<<<Khel khotom>>>>> }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e2+5; #define endl '\n' ll num; int cnt[5]; int main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cout.setf(ios::fixed),cout.precision(3); cin>>num; while(num){ int bit=num%10; cnt[bit%3]++; num/=10; } int ans; if(cnt[0]>0||cnt[1]>=3||cnt[2]>=3||(cnt[1]>=1&&cnt[2]>=1)){ int tp1=cnt[1],tp2=cnt[2]; int minn=min(cnt[1],cnt[2]); cnt[1]-=minn; cnt[2]-=minn; cnt[1]%=3; cnt[2]%=3; ans=0; if(cnt[1]) ans=cnt[1]; if(cnt[2]) ans=cnt[2]; cnt[1]=tp1; cnt[2]=tp2; cnt[1]%=3; cnt[2]%=3; minn=min(cnt[1],cnt[2]); cnt[1]-=minn; cnt[2]-=minn; if(cnt[1]==0&&cnt[2]==0) ans=0; else ans=min(ans,max(cnt[1],cnt[2])); } else ans=-1; cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #include<ext/pb_ds/tree_policy.hpp> #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; ///--->>> CONTAINERS <<<---/// using i64 = int64_t; using ll = long long; using ld = long double; using vi = vector<ll>; using vvi = vector<vector< ll > >; using vpi = vector<pair<ll,ll> >; using pi = pair<ll,ll>; using st = set<ll>; using stk = stack<ll>; using kiwi = queue<ll>; using pkiwi = priority_queue<ll>; using mp = map<ll,ll>; using ump = unordered_map<ll,ll>; ///--->>> TEMPLATES <<<---/// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update >; ///--->>> MACROS<<<---/// const ll mx = 3e5+123; #define MOD 998244353 #define mod 1000000007 #define isb(z) __builtin_popcount(z) #define lisb(z) __builtin_popcountll(z) #define iclz(z) __builtin_clz(z) #define lclz(z) __builtin_clzll(z) #define ictz(z) __builtin_ctz(z) #define lctz(z) __builtin_ctzll(z) #define REP(i,n) for (ll i=1;i<=n;++i) #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ///--->>>OPTIMIZE<<<---/// #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") ///--->>> DEBUGGER<<<---/// #define dbg(args...) do {cerr<<#args<<" : "; diraf(args); } while(0) void diraf () { cerr << endl;} template < typename T, typename ... hello>void diraf( T arg, const hello &... rest) {cerr<<arg<< ' ';diraf(rest...);} ///--->>> RANDOMIZER<<<---/// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* usage - just do rng() */ int main(int argc, char* argv[]){ fastio; cin.exceptions(cin.failbit); ll n, tmp, cnt = 0; cin>>n; tmp = n; ll digits = floor(log10(n)) + 1; if(n%3==0){ cout<<0<<endl; return 0; } vi dig(digits); pkiwi subSumDig; for(auto& x:dig) x = tmp % 10, tmp/=10; ll subSet = 1<<digits; for(ll i = 1; i<subSet; ++i){ ll sum = 0, cnt = 0; for(ll j = 0; j<digits; ++j){ if(i & (1<<j)) sum+=dig[j], cnt++; } if(sum%3==0) subSumDig.emplace(cnt); /// dbg(i, sum); } if(subSumDig.empty()){ cout<<-1<<endl; return 0; } cout<<digits - subSumDig.top()<<endl; return 0; }
#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define _overload(_1, _2, _3, _4, name, ...) name #define _rep1(Itr, N) _rep3(Itr, 0, N, 1) #define _rep2(Itr, a, b) _rep3(Itr, a, b, 1) #define _rep3(Itr, a, b, step) for (i64(Itr) = a; (Itr) < b; (Itr) += step) #define repeat(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__) #define rep(...) repeat(__VA_ARGS__) using namespace std; using i64 = long long; using u64 = unsigned long long; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); i64 n; cin >> n; vector<pair<i64, string>> v(n); for (auto &vs : v) cin >> vs.second >> vs.first; sort(v.rbegin(), v.rend()); cout << v[1].second << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mp make_pair #define pb push_back #define lp(i,s,f) for(ll i = s; i < ll(f); i++) #define inF freopen("input.in", "r", stdin); #define outF freopen("output.in", "w", stdout); #define endl '\n' #define MOD 1000000007 #define mm(arr) memset(arr, 0, sizeof(arr)) #define F first #define S second #define int ll int dp[70][2]; string arr[70]; int n; int calc(int ind, bool last){ if(ind == n){ return last; } if(dp[ind][last] != -1){ return dp[ind][last]; } if(arr[ind] == "AND"){ return dp[ind][last] = calc(ind + 1, last & 1) + calc(ind + 1, 0); } else{ return dp[ind][last] = calc(ind + 1, last | 1) + calc(ind + 1, last); } } int32_t main(){ FAST cin >> n; bool OR = 0; for(int i = 0; i < n; i++){ cin >> arr[i]; } memset(dp, -1, sizeof(dp)); cout << calc(0, 0) + calc(0, 1); return 0; }
#include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> #include <climits> #define rep(i, n) for(int i = 0; i < n; i++) #define per(i, n) for(int i = n - 1; i >= 0; i--) using ll = long long; #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mod 1000000007 using namespace std; template<class T, class U> T &chmax(T &a, const U &b){ if(a < b) a = b; return a; } template<class T, class U> T &chmin(T &a, const U &b){ if(a > b) a = b; return a; } int n,m; vvi root; vi visited; //woutの頂点を通らずにposからたどれる場所を見る void fill(int pos, vi &checked){ if(checked[pos] != -1) return; checked[pos] = 1; for(int x : root[pos]){ if(checked[x] != -1) continue; fill(x, checked); } } ll dfs(int pos, vi checked){ vi cnt(3); for(int x : root[pos]){ if(checked[x] == -1) continue; cnt[checked[x]]++; } ll ans = 0; rep(i, 3){ if(cnt[i]) continue; checked[pos] = i; ll res = 1; vi temp = checked; for(int x : root[pos]){ if(checked[x] != -1 || temp[x] != -1) continue; fill(x, temp); ll num = dfs(x, checked); res *= num; } ans += res; } return ans; } int main(){ cin >> n >> m; root.resize(n); rep(i, m){ int a,b; cin >> a >> b; a--; b--; root[a].push_back(b); root[b].push_back(a); } visited.resize(n, -1); ll ans = 1; rep(i, n){ if(visited[i] != -1) continue; fill(i, visited); ll num = dfs(i, vi(n, -1)); ans *= num; if(!ans) break; } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; vector<int> a[27]; int used[27], color[27]; vector< pair<int,int> > t; long long n,m,v1,v2,res,globalres; void dfs(int v,int type) { used[v]=type; t.push_back({v,used[v]}); for(auto to : a[v]) if(used[to] == 0) dfs(to,2); } void put(int pos, int col) { int v = t[pos].first; for(auto to : a[v]) if(color[to]==col) return; if(pos==n-1 || t[pos+1].second==1) res++; else { color[v] = col; put(pos+1, 1); put(pos+1, 2); put(pos+1, 3); color[v] = 0; } } int main() { cin >> n >> m; for(int i=1; i<=m; i++) { cin >> v1 >> v2; a[v1].push_back(v2); a[v2].push_back(v1); } for(int i=1; i<=n; i++) if(used[i]==0) { used[i]=1; dfs(i,1); } globalres = 1; for(int i=0; i<n; i++) if(t[i].second==1) { res = 0; put(i,1); put(i,2); put(i,3); globalres *= res; } cout << globalres << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; int main() { int n, k; cin >> n >> k; string s; cin >> s; rep(i, k) { int len = s.size(); int original = len; if (len % 2) { len *= 2; } string ns = ""; rep(j, len) { int t = j % original; int nt = (j + 1) % original; if (s[t] == 'P') { if (s[nt] == 'S') { ns += 'S'; } else { ns += 'P'; } } if (s[t] == 'R') { if (s[nt] == 'P') { ns += 'P'; } else { ns += 'R'; } } if (s[t] == 'S') { if (s[nt] == 'R') { ns += 'R'; } else { ns += 'S'; } } ++j; } s = ns; } cout << s[0] << endl; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')w=-1;ch=getchar(); } while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline bool read(int& a){ int s=0,w=1; char ch=getchar(); if(ch==EOF){ return false; } while(ch<'0'||ch>'9'){ if(ch=='-')w=-1;ch=getchar(); } while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); a=s*w; return true; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } inline void writeln(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); puts(""); } signed main(){ int a=read(),b=read(); int x,y; x=(a+b)/2; y=a-x; printf("%lld %lld\n",x,y); return 0; }
#include <bits/stdc++.h> #define debug(x) cout << #x << ":\t" << (x) << endl; using namespace std; #define ll long long const int N = 2e6 + 10; const int INF = 0x3f3f3f3f; const ll inf = 0x3f3f3f3f3f3f3f3f; const ll mod = 998244353; int n; char s[N]; vector<int>G[N]; int vis[110]; void dfs(int u) { vis[u] = 1; for (int v : G[u]) { if (!vis[v]) { dfs(v); } } } double ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); for (int j = 1; j <= n; j++) { if (s[j] == '1') { G[j].push_back(i); } } } for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); dfs(i); int t = 0; for (int j = 1; j <= n; j++)if (vis[j])t++; ans += 1.0 / t; } printf("%.10lf\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define p push #define pp pop #define deci(n) fixed << setprecision(n) #define high(n) __builtin_popcount(n) //no of 1 in binary #define highll(n) __builtin_popcountll(n) #define parity(n) __builtin_parity(n) #define clz(n) __builtin_clz(n) //count leading zeros in binary #define clzll(n) __builtin_clzll(n) #define ctz(n) __builtin_ctz(n) //count trailing zeros #define B begin() #define E end() #define rB rbegin() #define rE rend() #define sz size() #define C count() #define yes cout<<"YES\n" #define no cout<<"NO\n" #define mp make_pair typedef long long int ll; #define V(type) vector<type> #define PQ(type) priority_queue<type> #define pll pair<ll, ll> #define F first #define S second #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define rrep(i,n) for(int i=n-1;i>=0;i--) #define mod 1000000007 #define INF 9223372036854775807 const double delta = 0.0000000001; const double pi = 3.1415926535; ll rand_int(ll l, ll r) { //[l, r] #ifdef LOCAL static mt19937_64 gen; #else static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); #endif return uniform_int_distribution<ll>(l, r)(gen); } void FAST() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll fact(ll n) //O(N) { if(n<2)return 1; else return n*fact(n-1); } ll xi(ll x,ll y,ll p){ //gives (x^y)%p //O(n) atmax ,not sure though ll r=1; x=x%p; while(y>0){ if(y%2==0){ x*=x; x=x%p; y/=2; } else { r=((r%p)*(x%p))%p; y--; } } return r; } ll countBits(ll n) { //log n ll count = 0; while (n) { count++; n >>= 1; } return count; } ll isP(ll n) { //returns 1 if prime //O(sqrtN) if (n == 0 || n == 1) return 0; for (ll i = 2; i <= sqrt(n); i++) { if (n % i == 0) return 0; } return 1; } V(int) decTobin(int n) //returns a vector binary number, input a integer { //O(log n) V(int) a,ans; while(n>0) { a.pb(n%2); n/=2; } for(int i=a.size()-1;i>=0;i--) { ans.pb(a[i]); } return ans; } inline ll modadd(ll a, ll b, ll m = mod) { a += b; if (a >= m) a -= m; return a; } inline ll modmul(ll a, ll b, ll m = mod) { return ((a % m) * (b % m)) % m; } void solve() { int n; cin>>n; n=pow(2,n); ll maxa=0,maxb=0,posa=0,posb=0;; V(ll) arr(n/2),b(n/2); rep(i,n/2){cin>>arr[i];maxa=max(maxa,arr[i]);if(maxa==arr[i])posa=i;} rep(i,n/2){cin>>b[i];maxb=max(maxb,b[i]);if(maxb==b[i])posb=i;} if(maxa<maxb)cout<<posa+1; else cout<<n/2+posb+1; return; } int main() { FAST(); ll ttt; //cin>>ttt; ttt=1; while(ttt--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back ll A,B,C,D; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>A>>B>>C>>D; vector<ll>V; V.pb(A); V.pb(B); V.pb(C); V.pb(D); sort(V.begin(),V.end()); if((V[0] + V[3]) == (V[1] + V[2])) cout<<"Yes"<<endl; else if(V[0] + V[1] + V[2] == V[3]) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
#include <iostream> using namespace std; int cookies[4]; bool dfs(int index, int sum) { if(!sum) { return 1; } if(index == 4 || sum < 0) { return 0; } return dfs(index + 1, sum) || dfs(index + 1, sum - cookies[index]); } int main() { ios::sync_with_stdio(0); cin.tie(0); int sum = 0; for(int i = 0; i < 4; i++) { cin >> cookies[i]; sum += cookies[i]; } cout << (!(sum & 1) && dfs(0, sum >> 1) ? "Yes" : "No"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a,a+3); if((a[2] - a[1]) == (a[1] - a[0]) ){ cout << "Yes" << endl; } else cout << "No" << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { set<ll>s; map<ll, ll>mp; ll d = 0; for(ll i = 0; i < 3; i++) { ll x; cin >> x; s.insert(x); mp[x]++; d = x; } if(s.size() == 3) { cout << 0 << '\n'; return 0; } ll ans = 0; if(s.size() == 1) { cout << d << '\n'; return 0; } for(auto i : mp) { if(i.second != 2) d = i.first; } cout << d << '\n'; }
#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() void chmax(ll &x, ll y) {x = max(x,y);} void chmin(ll &x, ll y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} template<class in_vec_cout> void vec_cout(vector<in_vec_cout> vec) { for (in_vec_cout res : vec) {cout << res << " ";} cout << endl; } const ll inf = 1e18; const ll mod = 1e9 + 7; ll N, K; ll nC2(ll n) { if (n<2) return 0; return n * (n-1) / 2; } ll nC1(ll n) { if (n<1) return 0; return n; } ll cnt_ijk(ll S) { ll res = nC2(S-3+2); res -= 3 * nC2(S-N-3+2); res += 3 * nC2(S-2*N-3+2); res -= nC2(S-3*N-3+2); return res; } ll cnt_jk(ll S) { ll res = nC1(S-2+1); res -= 2 * nC1(S-N-2+1); res += nC1(S-2*N-2+1); return res; } int main() { cin >> N >> K; ll cnt = 0, ijk = 0; for (ll i=3; i<=3*N; i++) { cnt += cnt_ijk(i); if (cnt>=K) { cnt -= cnt_ijk(i); ijk = i; break; } } ll imin = max(1LL,ijk-2*N), imax = min(N,ijk-2); ll I = 0, jk = 0; for (ll i=imin; i<=imax; i++) { cnt += cnt_jk(ijk-i); if (cnt>=K) { cnt -= cnt_jk(ijk-i); I = i; jk = ijk - i; break; } } ll jmin = max(1LL,jk-N), jmax = min(N,jk-1); ll J = 0; for (ll j=jmin; j<=jmax; j++) { cnt++; if (cnt==K) { J = j; cout << I << " "; cout << J << " "; cout << jk - J << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 3; int main(void) { int n; ll k; cin >> n >> k; vector<vector<ll>> dp(N + 1, vector<ll>(N * n + 2, 0LL)); dp[0][0] = 1LL; for (int i = 0; i < N; i++) { for (int j = 0; j <= (i * n); j++) { dp[i + 1][j + 1] += dp[i][j]; dp[i + 1][j + 1 + n] -= dp[i][j]; } for (int j = 1; j < (i + 1) * n + 2; j++) { dp[i + 1][j] += dp[i + 1][j - 1]; } } int sum; for (sum = N; sum <= N * n; sum++) { if (k <= dp[N][sum]) { break; } k -= dp[N][sum]; } vector<int> ans(N); for (int i = 0; i < N - 1; i++) { for (int j = 1; j <= n; j++) { if (k <= dp[N - i - 1][sum - j]) { ans[i] = j; sum -= j; break; } k -= dp[N - i - 1][sum - j]; } } ans[N - 1] = sum; for (int i = 0; i < N; i++) { if (i) { cout << " "; } cout << ans[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<long long> Vll; ll mm=1000000000;ll MM=mm+7; #define rep(i, n) for(int i=0;i<n;i++) #define PI 3.141592653589793 int main(){ int h,w; cin >> h >> w; vector<vector<char>> s(h,vector<char>(w)); vector<vector<int>> ss(h+1,vector<int>(w+1)); rep(i,h){ rep(j,w){ cin >> s[i][j]; if(s[i][j]=='#'){ ss[i][j]++; ss[i+1][j]++; ss[i][j+1]++; ss[i+1][j+1]++; } } } ll ans=0; rep(i,h+1){ rep(j,w+1){ if(ss[i][j]%2==1)ans++; } } cout << ans << endl; }
#include<bits/stdc++.h> #define lli long long #define lld double #define test int t;cin>>t;while(t--) #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) #define mod 1000000007 #define ff first #define ss second #define ppl pair<lli , lli> #define ppi pair<int , int> #define large 1000000000000000000 using namespace std; bool comp(const ppl &a,const ppl &b) { return (a.ss < b.ss); } void solve() { lli n , money , pre = 0; cin >> n >> money; vector<ppl>v(n); for(int i = 0 ; i < n ; i++) { cin >> v[i].ff >> v[i].ss; } sort(v.begin() , v.end()); for(int i = 0 ; i < n ; i++) { if(money < (v[i].ff - pre)) { break; } money -= (v[i].ff - pre); pre = v[i].ff; money += v[i].ss; } pre += money; cout << pre; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r" , stdin); freopen("output.txt", "w" , stdout); #endif fast_io; //test { solve(); } return 0; }
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; using ll = long long; using ld = long double; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define mp make_pair const int MOD = 998244353; int mul(int a, int b) { return (1LL * a * b) % MOD; } int add(int a, int b) { int s = (a+b); if (s>=MOD) s-=MOD; return s; } int sub(int a, int b) { int s = (a+MOD-b); if (s>=MOD) s-=MOD; return s; } int po(int a, ll deg) { if (deg==0) return 1; if (deg%2==1) return mul(a, po(a, deg-1)); int t = po(a, deg/2); return mul(t, t); } int inv(int n) { return po(n, MOD-2); } mt19937 rnd(time(0)); const int LIM = 200000; vector<int> facs(LIM), invfacs(LIM); void init() { facs[0] = 1; for (int i = 1; i<LIM; i++) facs[i] = mul(facs[i-1], i); invfacs[LIM-1] = inv(facs[LIM-1]); for (int i = LIM-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1); } int C(int n, int k) { if (n<k) return 0; if (n<0 || k<0) return 0; return mul(facs[n], mul(invfacs[k], invfacs[n-k])); } /* struct DSU { vector<int> sz; vector<int> parent; void make_set(int v) { parent[v] = v; sz[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; } } DSU (int n) { parent.resize(n); sz.resize(n); for (int i = 0; i<n; i++) make_set(i); } };*/ const int N = 10000; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); init(); int n; cin>>n; vector<int> w(n); for (int i = 0; i<n; i++) cin>>w[i]; vector<vector<int>> dp(N+1, vector<int>(n+1)); dp[0][0] = 1; for (int i = 0; i<n; i++) { for (int W = N; W>=w[i]; W--) for (int cnt = i+1; cnt>=1; cnt--) dp[W][cnt] = add(dp[W][cnt], dp[W-w[i]][cnt-1]); } int tot = 0; for (auto it: w) tot+=it; if (tot%2==1) {cout<<0<<endl; return 0;} int need = tot/2; int ans = 0; for (int take = 0; take<=n; take++) { ans = add(ans, mul(dp[need][take], mul(facs[take], facs[n-take]))); } cout<<ans; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; //#define ACL #ifdef ACL #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif #if __has_include("acl-all.h") #include "acl-all.h" //on Wandbox using namespace atcoder; #endif #endif //#ifdef ACL typedef long double ld; typedef long long int ll; typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<ll> vll; typedef vector<pair<ll, ll> > vpll; typedef vector<vector<int> > vvi; typedef vector<vector<char> > vvc; typedef vector<vector<string> > vvs; typedef vector<vector<ll> > vvll; typedef vector<vector<bool> > vvb; typedef pair<ll, ll> pll; #define rep(i, n) for (int i = 0; i < int(n); ++i) #define rrep(i, n) for (int i = 1; i <= int(n); ++i) #define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++) #define drep(i, n) for (int i = int(n)-1; i >= 0; --i) #define MES(a) MES2 a #define MES2(a0,a1,a2,a3,a4,x,...) x #define mes_1(x1) cout<<x1<<endl #define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl #define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl #define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl #define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl #define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__) #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0,a1,a2,a3,a4,x,...) x #define debug_1(x1) cout<<#x1<<": "<<x1<<endl #define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl #define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl #define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl #define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl #define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__) #define Ynmes(a) (a) ? mes("Yes") : mes("No") #define YNmes(a) (a) ? mes("YES") : mes("NO") #define re0 return 0 #define mp(p, q) make_pair(p, q) #define pb(n) push_back(n) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define Sort(a) sort(a.begin(), a.end()) #define rSort(a) sort(a.rbegin(), a.rend()) #define Rev(a) reverse(a.begin(), a.end()) int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 }; int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 }; 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; } struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } }; const int INF = INT_MAX; const ll LLINF = 1LL << 60; const ll MOD = 1000000007; const double EPS = 1e-9; ll fact_mod(ll n, ll mod) { ll f = 1; for (ll i = 2; i <= n; i++) f = f * (i % mod) % mod; return f; } ll modpow(ll x, ll n, ll mod) { if(n == 0) return 1; ll res = modpow((x * x) % mod, n / 2 , mod); if(n & 1) res = (res * x) % mod; return res; } ll modncr(ll n, ll r, ll mod) { if(r > n-r) r = n-r; if(r == 0) return 1; ll a = 1; rep(i, r) a = a * ((n-i) % mod) % mod; ll b = modpow(fact_mod(r, mod), mod-2, mod); return (a % mod) * (b % mod) % mod; } signed main() { ld n; ld x0, y0, xm, ym; cin >> n >> x0 >> y0 >> xm >> ym; ld cx = (x0 + xm) / 2; ld cy = (y0 + ym) / 2; ld x = x0 - cx; ld y = y0 - cy; ld rad = 2 * acos(-1) / n; ld x1 = cos(rad) * x - sin(rad) * y; ld y1 = sin(rad) * x + cos(rad) * y; printf("%.10Lf %.10Lf\n", x1+cx, y1+cy); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vii = vector<vector<int>>; using vl = vector<long long>; using vll = vector<vector<long long>>; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define len(a) (int)a.size() #define all(a) a.begin(), a.end() const int INF32 = 1 << 30; const long long INF64 = 1LL << 60; const double PI = acos(-1); int main() { string S; cin >> S; list<char> T; int inv = 0; rep(i, len(S)) { if (S[i] == 'R') { inv++; } else { if (inv % 2 == 1) { if (!T.empty() && S[i] == T.front()) { T.pop_front(); } else { T.push_front(S[i]); } } else { if (!T.empty() && S[i] == T.back()) { T.pop_back(); } else { T.push_back(S[i]); } } } } if (inv % 2 == 1) T.reverse(); string ans = ""; for(auto itr = T.begin(); itr != T.end(); ++itr) { ans += *itr; } cout << ans << endl; }
#include<cstdio> #define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i) using namespace std; const int N=5e5+5; template<typename T>void read(T &x) { bool neg=false; unsigned char c=getchar(); for(;(c^48)>9;c=getchar())if(c=='-')neg=true; for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48); if(neg)x=-x; } char s[N],a[2*N]; int n,l=N,r=N; bool empty=true; int main() { scanf("%s",s); while(s[n])++n; F(i,0,n) { if(s[i]=='R')l^=r^=l^=r; else { if(empty)a[l=r]=s[i],empty=false; else { if(s[i]==a[r]) { if(l!=r)r-=l<r?1:-1; else empty=true; } else r+=l<r?1:-1,a[r]=s[i],empty=false; } } } if(!empty) { if(l<r)F(i,l,r+1)putchar(a[i]); if(l>r)for(int i=l;i>=r;--i)putchar(a[i]); if(l==r)putchar(a[l]); } putchar('\n'); return 0; }
#include <bits/stdc++.h> using namespace std; /* * * * * * * * * * */ #define mp make_pair typedef long long ll; typedef pair<int, int> pii; /* * * * * * * * * * */ /* * * * Too many mind, no mind. * * */ const ll mod = 998244353; ll add(ll a, ll b) { return a + b >= mod ? a + b - mod : a + b; } ll sub(ll a, ll b) { return a >= b ? a - b : a - b + mod; } ll mul(ll a, ll b) { return a * b % mod; } ll powmod(ll b, ll e) { ll ans = 1; for (ll m = b; e; e >>= 1, m = mul(m, m)) if (e & 1) ans = mul(ans, m); return ans; } int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n; cin >> n; vector <ll> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; sort(a.begin(), a.end()); ll sum = 0, p = 1, ans = 0, inv = powmod(2, mod - 2); for (int i = 0; i < n; ++i) { sum = add(sum, mul(a[i], p)); p = mul(p, 2); } for (int i = 0; i < n; ++i) { sum = sub(sum, a[i]); sum = mul(sum, inv); ans = add(ans, mul(a[i], sum)); ans = add(ans, mul(a[i], a[i])); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair<int,int>; using vv = vector<vector<int>>; const int INF = (int)1e9; const LL LINF = (LL)1e18; long long const mod = 998244353; struct mint{ long long val; mint(long long val = 0): val(val % mod) {} mint& operator += (const mint n){ val += n.val; if(val >= mod) val -= mod; return *this; } mint& operator -= (const mint n){ val -= n.val; if(val < 0) val += mod; return *this; } mint& operator *= (const mint n){ val *= n.val; val %= mod; if(val < 0) val += mod; return *this; } mint operator + (const mint n) const{ mint res(*this); return res += n; } mint operator - (const mint n) const{ mint res(*this); return res -= n; } mint operator * (const mint n) const{ mint res(*this); return res *= n; } mint pow(long long n) const{ if(n == 0) return 1; mint m = pow(n >> 1); m *= m; if(n & 1) m *= *this; return m; } // mint division for prime mod mint inv() const{ return pow(mod - 2); } mint& operator /= (const mint n){ return (*this) *= n.inv(); } mint operator / (const mint n) const{ mint res(*this); return res /= n; } }; int main(){ int N; cin >> N; vector<LL> A(N); rep(i,N) cin >> A[i]; if(N == 1){ cout << A[0] * A[0] % mod << endl; return 0; } sort(A.begin(), A.end()); mint ans = 0; vector<mint> sum(N-1); sum[N-2] = 1; for(int i = N - 3; i >= 0; i--) sum[i] = sum[i+1] * 2; mint tot = 0; rep(i,N-1) tot += sum[i] * A[i]; for(int i = N - 1; i >= 0; i--){ ans += A[i] * A[i]; ans += tot * A[i]; if(i == 0) break; tot -= A[i-1]; tot /= 2; } cout << ans.val << endl; return 0; }
#include <iostream> #include<bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define endl "\n" #define vi vector<int> #define vii vector<pair<int,int>> #define pb push_back using namespace std; using ll = long long; const ll mod = 1e9+7; ll fac[1000001]; void pre(){ fac[0]=fac[1]=1; for(int i=2;i<=1000001;i++) { fac[i]=(fac[i-1]*1LL*i)%mod; } } ll binpower(ll a,ll n){ ll res=1; while(n){ if(n%2) res = (res*1LL*a)%mod; n/=2; a=(a*1LL*a)%mod; } return res; } ll nCrmod(ll n,ll r){ ll res= fac[n]; res = (res*1LL*binpower(fac[r],mod-2))%mod; res= (res*1LL*binpower(fac[n-r],mod-2))%mod; return res; } long long ncr(int n, int r) { if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r) long long ans = 1; int i; for(i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans; } ll modexp(ll a,ll b,ll m){ if(b==0)return 1; if(b%2==0){ ll y = modexp(a,b/2,m); return (y*y)%m; } else{ return ((a%m)*modexp(a,b-1,m))%m; } } ll modinv(ll a,ll m){ return modexp(a,m-2,m); //works only for prime m. //else calculate phi(m)-1. } void SieveOfEratosthenes(ll n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. bool prime[n+1]; memset(prime, true, sizeof(prime)); for (ll p=2; p*p<=n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (ll i=p*p; i<=n; i += p) prime[i] = false; } } // Print all prime numbers for (ll p=2; p<=n; p++) if (prime[p]) cout << p << " "; } vector<int> Centroid(vector<int>g[],int n) { vector<int> centroid; vector<int> sz(n+1); function<void (int, int)> dfs = [&](int u, int prev) { sz[u] = 1; bool is_centroid = true; for (auto v : g[u]) if (v != prev) { dfs(v, u); sz[u] += sz[v]; if (sz[v] > n / 2) is_centroid = false; } if (n - sz[u] > n / 2) is_centroid = false; if (is_centroid) centroid.push_back(u); }; dfs(1, 0); return centroid; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin>>n; ll ans=0; ll k =sqrt(n); set<ll>s; for(int i=2;i<=k;i++){ ll p =i; for(int j=2;j<40;j++){ p*=i; if(p>n)break; else s.insert(p); } } cout<<n-s.size()<<endl; return 0; }
#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() const ll X = 6; struct S { vll v; bool z; vll ret; ll hsh() { ll ret = 0; for(auto i: v) { ret = X*ret + i; } ret = 2*ret + z; return ret; } }; vll solve(ll n, vll v) { ll x=0; vll ret; bool z = 1; while(x<n-X) { while(x<n && v[x]==x) x++; if(x==n) break; ll p; F(n) if(v[i]==x) p=i; if((p&1)==z) { while(p!=x) { swap(v[p-1],v[p]); ret.pb(p-1); z = !z; p--; } } else { F(X-1) { if(((n-i-1)&1)==z && v[n-i-1]!=x && v[n-i-2]!=x) { swap(v[n-i-1],v[n-i-2]); ret.pb(n-i-2); z = !z; break; } } } } if(x!=n) { x = max(0ll, n-X); queue<S> q; vll w; FOR(i,x,n) w.pb(v[i]-x); q.push({w,z}); set<ll> vis; ll m = w.size(); while(q.size()) { auto cur = q.front(); q.pop(); if(vis.count(cur.hsh())) continue; vis.insert(cur.hsh()); bool ok = 1; F(m) ok &= cur.v[i]==i; if(ok) { for(auto i: cur.ret) ret.pb(i+x); break; } FOR(i,1,m) if(((i+x)&1)==cur.z) { S ss = cur; ss.z = !ss.z; ss.ret.pb(i-1); swap(ss.v[i-1], ss.v[i]); q.push(ss); } } } return ret; } void SOLVE() { ll n; cin >> n; vll v(n); F(n) { cin >> v[i]; v[i]--; } auto ret = solve(n,v); cout << ret.size() << endl; for(auto i: ret) cout << i+1 << ' '; cout << 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; const int MOD=3; const int msize=400005; int warusan[msize]; int mod3[msize]; void initComb(){ for(int i=1;i<msize;i++){ int t=i; while(t%3==0){ t/=3; warusan[i]++; } mod3[i]=t%3; } for(int i=2;i<msize;i++){ warusan[i]+=warusan[i-1]; mod3[i]=mod3[i]*mod3[i-1]%MOD; } } int comb(int n,int r){ if(warusan[n]>warusan[r]+warusan[n-r])return 0; if((mod3[n]+mod3[r]*mod3[n-r])%3==0)return 2; return 1; } int main(){ initComb(); int n; cin>>n; vector<short>a(n); char t; char ans[3]={'R','W','B'}; for(int i=0;i<n;i++){ cin>>t; switch(t){ case 'R':a[i]=0;break; case 'W':a[i]=1;break; case 'B':a[i]=2;break; } } int sum=0; for(int i=0;i<n;i++){ sum=(sum+comb(n-1,i)*a[i])%3; } if(n%2==0)sum=(3-sum)%3; cout<<ans[sum]<<'\n'; }
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define maxm 600005 #define maxn 150005 #define ls (tot << 1) #define rs (tot << 1 | 1) #define PII pair<int, int> typedef long long ll; typedef unsigned long long ull; using namespace std; const double pi = acos(-1); const ll mod = 1e9 + 7; const double eps = 1e-10; inline ll read(){ ll x = 0, f = 1;char ch = getchar(); while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();} while(ch >= '0' && ch <= '9'){x = x * 10 + ch -'0';ch = getchar();} return x * f; } int n; char s[400005]; int getc(char c){ if(c == 'B') return 0; if(c == 'R') return 1; return 2; } int c[20][20]; int C(int n, int m){ if(m > n) return 0; if(n <= 3) return c[n][m]; return C(n / 3, m / 3) * C(n % 3, m % 3); } int main(){ n = read(); scanf("%s", s); int sum = 0; int f = 1; c[0][0] = 1; for(int i = 1;i <= 10;i++){ c[i][0] = 1; for(int j = 1;j <= i;j++){ c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 3; } } if(n % 2 == 0) f = -1; for(int i = 0;i < n;i++){ sum = (sum + C(n - 1, i) * f * getc(s[i])) % 3; } sum = sum + 3; sum %= 3; if(sum == 0) puts("B"); else if(sum == 1) puts("R"); else puts("W"); return 0; }