code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; // 約数列挙 vector<int> get(int n) { vector<int> ret; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i * i != n) { ret.push_back(n / i); } } } return ret; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } class FGCDOrMIN { public: void solve(std::istream& cin, std::ostream& cout) { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; int mina = INF; map<int, int> m; REP(i, N) { for (auto n : get(A[i])) { if (m.count(n) == 0) { m[n] = A[i]; } else { m[n] = gcd(m[n], A[i]); } } mina = min(mina, A[i]); } int res = 0; for (auto kv : m) { if (kv.second <= mina && kv.first == kv.second) { res++; } } cout << res << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream& in(std::cin); std::ostream& out(std::cout); FGCDOrMIN solver; solver.solve(in, out); return 0; }
#include <bits/stdc++.h> #include <map> #include <algorithm> #include <vector> #include <string.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <chrono> #include <thread> #include <iomanip> #include <typeinfo> //define #define trav(i,v) for(auto i: v) #define rep(i,n) for(int i=0;i<n;i++) #define repu(i,k,n) for(int i=k;i<=n;i++) #define repd(i,k,n) for(int i=k;i>=n;i--) #define se second #define fi first #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define itn int //typedef typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; typedef std::pair<int, int> pii; typedef std::vector<int> vi; typedef std::vector< pii > vii; typedef std::vector< ll > vll; typedef std::vector< std::pair < pii, int > > vpp; const long long MOD = 1000000007; // const long long MOD = 998244353; const long long inf = 1000000000000000000; using namespace std; ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n>>=1; } return res; } ll power(ll x, ll n) { ll res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res; } void init() { } void solve() { int n, m; cin>>n>>m; vll a(n+1); repu(i,1,n) cin>>a[i]; vi v[n+1], vv[n+1]; vi in(n+1,0), out(n+1,0); rep(i,m) { int x, y; cin>>x>>y; in[y]++; out[x]++; v[x].pb(y); vv[y].pb(x); } vll v1(n+1,inf), v2(n+1,0); repu(i,1,n) { v1[i] = inf; v2[i] = a[i]; } vi vis(n+1,0); set<int> s; repu(i,1,n) { if(in[i]==0) { s.insert(i); v1[i] = inf; } } while(!s.empty()) { int x = *s.begin(); if(vis[x]) continue; vis[x] = 1; s.erase(s.begin()); trav(y,v[x]) { s.insert(y); v1[y] = min(v1[y],min(v1[x],a[x])); } } // cout<<v1[3]<<" "<<v2[3]<<endl; ll ans = -inf; repu(i,1,n) { ans = max(v2[i]-v1[i],ans); } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t = 1, tt=1; // cin>>t; init(); while(tt<=t) { // cout<<"Case #"<<tt<<": "; solve(); tt++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; #define endl ("\n") #define int long long #define pb push_back #define mp make_pair #define ff first #define ss second #define all(c) c.begin(), c.end() #define rep(i,n) for(int i=0;i<n;i++) #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int32_t main(){ fast int t=1; // cin>>t; while(t--){ int n;cin>>n;int a[n], b[n], c[n];rep(i, n)cin>>a[i];rep(i, n)cin>>b[i];rep(i, n)cin>>c[i]; map<int, int>m1, m2; set<int>s; rep(i, n)m1[a[i]]++, s.insert(a[i]); rep(i, n)m2[b[c[i]-1]]++; int ans=0; for(auto i:s){ ans+=(m1[i]* m2[i]); } cout<<ans<<endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N; cin >> N; vector<int> A(N), B(N), C(N), BC(N), count(N+1); 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 >> C[i]; for (int j=0; j<N; j++) { BC[j] = B[C[j]-1]; count[BC[j]]++; } long long answer = 0; for (int a : A) { answer += count[a]; } cout << answer << endl; return 0; }
#include <iostream> namespace wxy{ const int mod = 998244353; int f[15][5050],fac[5050],invfac[5050]; inline void init(){ fac[0]=invfac[0]=invfac[1]=1; for(int i=1;i<=5000;i++)fac[i]=(long long)fac[i-1]*i%mod; for(int i=2;i<=5000;i++)invfac[i]=(long long)(mod-mod/i)*invfac[mod%i]%mod; for(int i=2;i<=5000;i++)invfac[i]=(long long)invfac[i-1]*invfac[i]%mod; } inline int C(int n,int m){return n<m?0:(long long)fac[n]*invfac[m]%mod*invfac[n-m]%mod;} inline void main(){ int n,m; std::cin >> n >> m; init(); if (m % 2 == 1) {std::cout << 0; return;} int limit = 1,t = 0; while (limit < m) {limit <<= 1; t++;} for (int k = 0; 2 * k <= n; k++) f[0][2 * k] = C(n,2 * k); int ans = 0; for (int i = 1; i <= t; i++){ f[i][0] = 1; for(int j = 1; j <= m; j++){ if (j % 2 == 1) continue; for (int k = 0; 2 * k <= n && j - (2 * k) * (1 << i) >= 0; k++) f[i][j] = (f[i][j] + 1ll * C(n,2 * k) * f[i - 1][j - (2 * k) * (1 << i)] % mod) % mod; } } std::cout << f[t][m]; } }signed main(){wxy::main(); return 0;}
#include <bits/stdc++.h> #define lc (o<<1) #define rc ((o<<1)|1) #define PB push_back #define MK make_pair using namespace std; #define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl const int maxn = 5000 + 5; const int modu = 998244353; // 1e9 + 7 const int inf = 0x3f3f3f3f; const double eps = 1e-5; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; typedef long long LL; void read(LL &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return; } void read(int &x) { LL y; read(y); x = (int)y; } void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; } void read(char *s) { scanf("%s", s); } template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); } int n, m; int a[maxn]; LL inv[maxn], c[maxn], f[20][maxn]; int b[20]; int main() { // freopen("my.txt", "w", stdout); read(n, m); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (modu - (modu/i))*inv[modu%i]%modu; c[0] = 1; for (int i = 1; i <= n; ++i) c[i] = (n-i+1)*inv[i]%modu*c[i-1]%modu; int nb = 0; while (m) { b[nb++] = m%2; m /= 2; } memset(f, 0, sizeof(f)); f[nb-1][0] = 1; int tot = b[nb-1]; for (int i = nb-2; i >= 0; --i) { for (int j = 0; j <= tot*2 + b[i]; ++j) if (j%2 == 0) { for (int k = 0; k <= j && k <= n; k += 2) f[i][j] = (f[i][j] + f[i+1][(j-k)/2]*c[k]) % modu; } tot = tot*2 + b[i]; } printf("%lld\n", f[0][tot]); return 0; }
#include <bits/stdc++.h> #include <map> #include <algorithm> #include <vector> #include <string.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <chrono> #include <thread> #include <iomanip> #include <typeinfo> //define #define trav(i,v) for(auto i: v) #define rep(i,n) for(int i=0;i<n;i++) #define repu(i,k,n) for(int i=k;i<=n;i++) #define repd(i,k,n) for(int i=k;i>=n;i--) #define se second #define fi first #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define itn int //typedef typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; typedef std::pair<int, int> pii; typedef std::vector<int> vi; typedef std::vector< pii > vii; typedef std::vector< ll > vll; typedef std::vector< std::pair < pii, int > > vpp; // const long long MOD = 1000000007; const long long MOD = 998244353; const long long inf = 1000000000000000000; using namespace std; ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n>>=1; } return res; } ll power(ll x, ll n) { ll res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res; } void solve() { ll a, b, c; cin>>a>>b>>c; ll x=1; x*=a; x%=MOD; x*=(a+1); x%=MOD; x*=modpow(2,MOD-2); x%=MOD; a=b; x*=a; x%=MOD; x*=(a+1); x%=MOD; x*=modpow(2,MOD-2); x%=MOD; a=c; x*=a; x%=MOD; x*=(a+1); x%=MOD; x*=modpow(2,MOD-2); x%=MOD; cout<<x<<endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t = 1, tt=1; // cin>>t; while(tt<=t) { // cout<<"Case #"<<tt<<": "; solve(); tt++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define endl '\n' #define all(x) (x).begin(),(x).end() const int INF=1000000000+5; const int N=300+5; const ll oo=1e18+5; const ll mod=998244353; ll inv2; ll mul(ll a, ll b){ return a*b%mod; } ll poww(ll a,ll b){ ll r=1; while(b){ if(b&1) r=mul(r,a); a=mul(a,a); b>>=1; } return r; } ll sum(ll x){ return mul(x,mul(x+1,inv2)); } int main(){ ios::sync_with_stdio(0); cin.tie(0); ll a,b,c; cin>>a>>b>>c; inv2=poww(2ll,mod-2); cout<<mul(sum(a),mul(sum(b),sum(c))); return 0; }
#include<bits/stdc++.h> using namespace std; void __print(int x) {cout << x;}void __print(long x) {cout << x;}void __print(long long x) {cout << x;}void __print(unsigned x) {cout << x;}void __print(unsigned long x) {cout << x;}void __print(unsigned long long x) {cout << x;}void __print(float x) {cout << x;}void __print(double x) {cout << x;}void __print(long double x) {cout << x;}void __print(char x) {cout << '\'' << x << '\'';}void __print(const char *x) {cout << '\"' << x << '\"';}void __print(const string &x) {cout << '\"' << x << '\"';}void __print(bool x) {cout << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';} template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";} void _print() {cout << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cout << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define loop(i,a,b) for (ll i = a; i <= (long long)b; i++) #define rloop(i,a,b) for (ll i = a; i >= (long long)b; i--) #define ms(a,x) memset(a,x,sizeof a) #define ll long long int #define ff first #define ss second #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define pb push_back #define nl cout<<"\n" #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b))/(gcd(a,b)) #define sq(a) ((a)*(a)) #define sz(a) a.size() #define mod 1000000007 #define PI (2*acos(0.0)) int main() { IOS int T=1; //cin>>T; while(T--) { ll n; cin>>n; std::vector<ll> a(n),b(n),c(n),d(n),e(n); loop(i,0,n-1) cin>>a[i]; b[0]=a[0]; loop(i,1,n-1) b[i]=a[i]+b[i-1]; e[0]=b[0]; loop(i,1,n-1) e[i]=e[i-1]+b[i]; ll mx=0; loop(i,0,n-1) { mx=max(mx,b[i]); c[i]=mx; } // debug(b); // debug(c); d[0]=max(0LL,a[0]); ll ans=max(0LL,d[0]); loop(i,1,n-1) { d[i]=e[i-1]+c[i]; ans=max(ans,d[i]); } //debug(d); cout<<ans; } return 0; }
#include <iostream> #include <algorithm> // for next_permutation #include <vector> #include <bits/stl_numeric.h> #include <queue> #include <regex> #define DEBUG 0 #define REP(i, n) for (long long i = 0; i < (n); i++) typedef long long ll; static const ll MOD = 1000000007; static const ll INF = 1000000000000000000LL; using namespace std; int main() { int N; cin >> N; vector <ll> a(N); vector <ll> ido(N,0); vector <ll> syunkan(N,0); ll wawa = 0; REP(i,N)cin >> a[i]; ido[0] = a[0]; syunkan[0] = max(0LL,a[0]); for(int i = 1; i < N; ++i) { ido[i] = ido[i-1] + a[i]; syunkan[i] = max(syunkan[i-1],ido[i-1] + a[i]); } ll res = 0; ll x = 0; x += a[0]; res = max(res,x); for(int i = 1; i < N; ++i) { res = max(res, x + syunkan[i]); x = x + ido[i]; } cout << res << endl; return 0; }
/* @author: saran * Git: https://github.com/abhisheksaran * LinkedIn: https://www.linkedin.com/in/abhishek-saran-2900b/ */ #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; typedef vector<LL> vl; typedef vector<vl> vvl; double EPS = 1e-9; int INF = 1000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1); int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 }; int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 }; #ifdef TESTING #define DEBUG fprintf(stderr, "====TESTING====\n") #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define DEBUG #define VALUE(x) #define debug(...) #endif #define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a)) #define FOREACH(a, b) for (auto&(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define SQR(x) ((LL)(x) * (x)) #define RESET(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define TC(t) while (t--) inline string IntToString(LL a) { char x[100]; sprintf(x, "%lld", a); string s = x; return s; } inline LL StringToInt(string a) { char x[100]; LL res; strcpy(x, a.c_str()); sscanf(x, "%lld", &res); return res; } inline string GetString(void) { char x[1000005]; scanf("%s", x); string s = x; return s; } inline string uppercase(string s) { int n = SIZE(s); REP(i, n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A'; return s; } inline string lowercase(string s) { int n = SIZE(s); REP(i, n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a'; return s; } inline void OPEN(string s) { #ifndef TESTING freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); #endif } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t,i; int ans = 0; cin>>t; REP(i,t){ int x; cin>>x; ans += max(0, x-10); } cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" const int N = 1e5 + 5; typedef long long LL; int main() { #ifdef LOCAL //freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; map <int,int> cnt; for (int i = 1; i <= n; i++) { int x; cin >> x; for (int j = 1; j * j <= x; j++) { if (x % j == 0) { cnt[j]++; if (x / j != j) cnt[x / j]++; } } } int ans = 0, mx = 0; for (auto x: cnt) { if (x.first == 1) continue; if (x.second > mx) mx = x.second, ans = x.first; } cout << ans << endl; return 0; }
#pragma GCC optimize("Ofast") #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; using ll = int64_t; using db = double; using ld = long double; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vector<int>>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpll = vector<pll>; constexpr char newl = '\n'; constexpr double eps = 1e-10; #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define F0R(i,b) FOR(i,0,b) #define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--) #define RF0(i,b) RFO(i,0,b) #define show(x) cout << #x << " = " << x << '\n'; #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define sz(x) (int)(x).size() #define YesNo {cout<<"Yes";}else{cout<<"No";} #define YESNO {cout<<"YES";}else{cout<<"NO";} #define v(T) vector<T> #define vv(T) vector<vector<T>> template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) { return l.first < r.first; } template<class T> istream& operator>>(istream& i, v(T)& v) { F0R(j, sz(v)) i >> v[j]; return i; } template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) { return i >> p.first >> p.second; } template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) { return i >> get<0>(t) >> get<1>(t) >> get<2>(t); } template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) { o << "(" << v.first << "," << v.second << ")"; return o; } template<class T> ostream& operator<<(ostream& o, const vector<T>& v) { F0R(i, v.size()) { o << v[i] << ' '; } o << newl; return o; } template<class T> ostream& operator<<(ostream& o, const set<T>& v) { for (auto e : v) { o << e << ' '; } o << newl; return o; } template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) { for (auto& p : m) { o << p.first << ": " << p.second << newl; } o << newl; return o; } #if 1 // INSERT ABOVE HERE signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; ll a = 0, b = 0; F0R(i, N) { string s; cin >> s; int c = 0; F0R(i, M) { c += s[i] == '1'; } (c & 1) ? a++ : b++; } cout << (a * b) << newl; } #endif
#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 using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef long double ld; typedef tree<pii,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define F first #define S second const int MOD = 1e9 + 7;//XXX const int C = 26;//XXX void add(int &x, int y){ x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int fix(int x){ while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x; } int pw(int a, int b){ int ret = 1; while (b){ if (b & 1) ret = 1ll*ret*a%MOD; b >>= 1; a = 1ll*a*a%MOD; } return ret; } const int MAXN = 2e5 + 10; ll col[MAXN], row[MAXN], sec[MAXN]; int H, W, n; bool cmp(int i, int j) { return row[i] < row[j]; } void solve() { cin >> H >> W >> n; for(int i = 1; i <= W; i++) col[i] = H; for(int i = 1; i <= H; i++) row[i] = W; for(int i = 0; i < n; i++) { ll x, y; cin >> x >> y; col[y] = min(col[y], x-1); row[x] = min(row[x], y-1); } iota(sec, sec+H, 1); sort(sec, sec+H, cmp); int bad = 1; while(row[bad]) bad++; ll ans = 0; ordered_set s; for(int i = 0, c = 1; i < H; i++) { while(c <= row[sec[i]]) { ans += col[c]; s.insert({col[c], c}); if(!col[c]) c = W+1; c++; } if(sec[i] < bad) ans += row[sec[i]] - (s.size() - s.order_of_key({sec[i], -1})); } cout << ans << "\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //cout << fixed << setprecision(6); int te = 1; //cin >> te; for (int w = 1; w <= te; w++){ //cout << "Case #" << w << ": "; solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define pb push_back struct query{int time,type;ll p;int idx;}; bool operator<(const query &a,const query &b){ return a.time<b.time; } int main(){ cin.tie(0);ios::sync_with_stdio(false); int n,m,q; ll x[200010],y[200010],nx=0,ny=0,ax[200010],ay[200010]; bool fx=false,fy=false,sw=false; cin>>n; vector<query>qs; rep(i,n)cin>>x[i]>>y[i]; cin>>m; rep(i,m){ int type; cin>>type; if(type==1||type==2)qs.pb({2*i,type,-1,-1}); if(type==3||type==4){ ll p; cin>>p; qs.pb({2*i,type,p,-1}); } } cin>>q; rep(i,q){ int a,b; cin>>a>>b; a--,b--; qs.pb({2*a+1,0,b,i}); } sort(all(qs)); for(auto[time,type,p,idx]:qs){ if(type==1){ if(sw==0){ fx=!fx; nx=-nx; } if(sw==1){ fy=!fy; ny=-ny; } sw=!sw; } if(type==2){ if(sw==0){ fy=!fy; ny=-ny; } if(sw==1){ nx=-nx; fx=!fx; } sw=!sw; } if(type==3){ if(sw==0){ fx=!fx; nx=-nx+p*2; } if(sw==1){ fy=!fy; ny=-ny+p*2; } } if(type==4){ if(sw==0){ fy=!fy; ny=-ny+p*2; } if(sw==1){ fx=!fx; nx=-nx+p*2; } } if(type==0){ if(sw==0){ if(fx==0)ax[idx]=x[p]+nx; if(fx==1)ax[idx]=-x[p]+nx; if(fy==0)ay[idx]=y[p]+ny; if(fy==1)ay[idx]=-y[p]+ny; } if(sw==1){ if(fy==0)ax[idx]=y[p]+ny; if(fy==1)ax[idx]=-y[p]+ny; if(fx==0)ay[idx]=x[p]+nx; if(fx==1)ay[idx]=-x[p]+nx; } } } rep(i,q)cout<<ax[i]<<" "<<ay[i]<<endl; return 0; }
#include <iostream> #include <algorithm> #include <queue> #include <bitset> #include <string> #include <cstring> #include <limits> #include <cmath> #include <cstdlib> #define int long long int const INF = std::numeric_limits<int>::max(); int const NINF = std::numeric_limits<int>::min(); int const N = 200000; int const M = N; int const Q = N; struct Point { int x, y; }; struct Oper { int op; int p; }; struct Query { int oper; int point; int index; bool operator<(const Query &other) const { return oper < other.oper; } }; struct Tag { int add; int times = 1; }; Point points[N + 1]; Oper opers[M + 1]; Query queries[Q + 1]; Tag xtag; Tag ytag; int swaps; int x_ans[Q + 1]; int y_ans[Q + 1]; void plus(int, Tag&); void times(int, Tag&); void plus(int v, Tag &tag) { tag.add += v; } void times(int v, Tag &tag) { tag.add *= v; tag.times *= v; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n, m; std::cin >> n; for (int i = 1; i <= n; i++) { std::cin >> points[i].x >> points[i].y; } std::cin >> m; for (int i = 1; i <= m; i++) { std::cin >> opers[i].op; if (opers[i].op >= 3) { std::cin >> opers[i].p; } } int q; std::cin >> q; for (int i = 1; i <= q; i++) { std::cin >> queries[i].oper >> queries[i].point; queries[i].index = i; } std::sort(queries + 1, queries + 1 + q); int pointer = 1; for (int i = 1; i <= m; i++) { while (pointer <= q && i > queries[pointer].oper) { int x = points[queries[pointer].point].x; int y = points[queries[pointer].point].y; if (swaps % 2) { std::swap(x, y); } x_ans[queries[pointer].index] = x * xtag.times + xtag.add; y_ans[queries[pointer].index] = y * ytag.times + ytag.add; pointer++; } int op = opers[i].op; int p = opers[i].p; if (op == 1) { swaps++; std::swap(xtag, ytag); times(-1, ytag); } else if (op == 2) { swaps++; std::swap(xtag, ytag); times(-1, xtag); } else if (op == 3) { times(-1, xtag); plus(2 * p, xtag); } else { times(-1, ytag); plus(2 * p, ytag); } } while (pointer <= q) { int x = points[queries[pointer].point].x; int y = points[queries[pointer].point].y; if (swaps % 2) { std::swap(x, y); } x_ans[queries[pointer].index] = x * xtag.times + xtag.add; y_ans[queries[pointer].index] = y * ytag.times + ytag.add; pointer++; } for (int i = 1; i <= q; i++) { std::cout << x_ans[i] << ' ' << y_ans[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int BIG = 9999999; vector<int> interests; vector<int> vinterests; vector<int> kapairs; int nums[400001]; int main() { ios::sync_with_stdio(false); cin.tie(0); int t, a, b, c, d, n, ans = 0; long long int w, x, y, z; cin >> n; d = 0; kapairs.resize(400001); for(int i = 0; i < 400001; i++) { kapairs[i] = 0; } for(int i = 0; i < n; i++) { cin >> a >> b; nums[a]++; nums[b]++; kapairs[a] += b; kapairs[b] += a; } for(int i = 0; i < 400001; i++) { if(nums[i]) d++; if(nums[i] == 1) interests.push_back(i); } // cout << "yay\n"; while(interests.size()) { for(int i = 0; i < interests.size(); i++) { d--; if(kapairs[interests[i]] == 0) continue; int c = kapairs[interests[i]], k = interests[i]; kapairs[k] -= c; kapairs[c] -= k; nums[k]--; nums[c]--; if(nums[c] == 1) vinterests.push_back(c); ans++; } interests.swap(vinterests); vector<int>().swap(vinterests); } // cout << "Left with " << numnums[0].first << ' ' << numnums[0].second << '\n'; cout << ans+d << '\n'; }
//Shinchan Loves Coding :)) #include<bits/stdc++.h> using namespace std; #define night_rider ios::sync_with_stdio(false);cin.tie(NULL); #define double long double #define int long long int #define en "\n" #define fi first #define se second #define PB push_back #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define rep(i,x,y) for(int i=x ; i<y ; i++) #define erep(i,x,y) for(int i=x ; i<=y ; i++) #define erepr(i,x,y) for(int i=x ; i>=y ; i--) #define repr(i,x,y) for(int i=x ; i>y ; i--) typedef vector<int> vi; typedef vector<vector<int>> vii; typedef pair<int,int> pi; typedef vector<pair<int,int>> pii; //-------------------------------------------------------------------------------------------------------------------------------------------------------------- const int no=3e6+5,modulo=1e9+7,inf=1e18,N=2e3+1; const double PI = 3.1415926535897932384; int ar[no] ,br[no],cr[no];//,dr[no],er[no]; int used[no]; //int color[no]; //pii adj[no]; vi adj[no] ; //LOWER_BOUND(n) - FIRST ELEMENT GREATER THAN EQUAL TO n //UPPER_BOUND(n) - FIRST ELEMENT GREATER THAN n //-------------------------------------------------------------------------------------------------------------------------------------------------------------- int mark[no]; vi ans; void dfs(int node) { used[node]=1; if(!mark[ar[node]]) ans.PB(node); mark[ar[node]]++; for(int child : adj[node]) { if(!used[child]) { dfs(child); } } mark[ar[node]]--; } void solve () { int n=0,m=0,a=0,b=0,c=0,d=0,x=0,y=0,z=0,w=0,k=0; cin>>n; rep(i,1,n+1) cin>>ar[i]; m=n-1; rep(i,0,m) { cin>>x>>y; adj[x].PB(y); adj[y].PB(x); } dfs(1); sort(all(ans)); for(auto ii : ans) cout<<ii<<en; } inline void runn() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } signed main() { night_rider runn(); int t=1; cout<<fixed<<setprecision(15); // cin>>t; rep(i,1,t+1) { // cout << "Case #" << i << ": "; solve(); } }
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; typedef unsigned long long int ull; typedef long int li; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define test ll t; cin >> t; while(t--) #define print_tc(t, c) cout << "Case #" << t << ": " << c << "\n"; const long long int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0}; const long long int cons = 100005, ncr_cons = 100005; const long long int MOD = 1000000007; // const long long int MOD = 998244353; const long long int const_INT_MAX = 1000000000000000000, const_INT_MIN = -1 * 1000000000000000000; using namespace std; bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.first > b.first);} bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second < b.second);} bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second > b.second);} ll gcd(ll x, ll y){return (ll)(__gcd(x, y));} ll lcm(ll x, ll y){return (ll)((x * y) / gcd(x, y));} ll mod_expo(ll x, ll y, ll p){ if((x + p) % p == 0) return 0; ll res = 1; x = (x + p) % p; while (y > 0){ if(y & 1) res = (res*x + p) % p; y = y>>1; x = (x*x + p) % p; } return res; } void usaco(string str = ""){ fastio; if(str.size()){ freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } } int main(){ usaco(); ll x, y, z; cin >> x >> y >> z; cout << (y * z - 1) / x << "\n"; cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> #include <string> #include <sstream> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; using P=pair<ll,ll>; const int INF=1001001001; const int mod=1e9+7; void solve(){ int x,y,z; cin>>x>>y>>z; cout<<(z*y-1)/x<<endl; } int main(){ solve(); return 0; }
#include<bits/stdc++.h> #define rep(i, n) for (int i = 0, length = n; i < length; i++) #define fi first #define se second #define lb lower_bound #define ub upper_bound #define ep emplace #define epb emplace_back #define scll static_cast<long long> #define sz(x) static_cast<int>((x).size()) #define pfll(x) printf("%lld\n", x) #define ci(x) cin >> x #define ci2(x, y) cin >> x >> y #define ci3(x, y, z) cin >> x >> y >> z #define ci4(w, x, y, z) cin >> w >> x >> y >> z #define co(x) cout << x << endl #define co2(x, y) cout << x << " " << y << endl #define co3(x, y, z) cout << x << " " << y << " " << z << endl using namespace std; typedef long long ll; typedef pair<int, int> P; typedef priority_queue<int> PQ; typedef priority_queue<int, vector<int>, greater<int>> PQG; typedef priority_queue<P> PQP; typedef priority_queue<P, vector<P>, greater<P>> PQPG; const int MAX_N = 100, MOD = 1e9 + 7, INF = 1e9; int n, m, a[4950], b[4950], c[100]; bool ans[4950], used[4950]; vector<P> to[100]; void dfs(int x) { for (auto &i: to[x]) { if (used[i.se]) continue; if (i.fi == a[i.se]) ans[i.se] = 1; used[i.se] = true; dfs(i.fi); } } int main() { ci2(n, m); rep(i, m) ci2(a[i], b[i]); rep(i, n) ci(c[i]); rep(i, m) { a[i]--; b[i]--; if (c[a[i]] < c[b[i]]) ans[i] = 1; if (c[a[i]] == c[b[i]]) { to[a[i]].epb(b[i], i); to[b[i]].epb(a[i], i); } } rep(i, n) dfs(i); rep(i, m) { if (ans[i]) co("<-"); else co("->"); } return 0; }
#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}; vector<ll> to[100005]; //先手取り、後てとり、turn? tuple<ll, ll, bool> dfs(ll pos) { //ここに来て撮った ll mypoint = 0; ll tekipoint = 1; bool myturn = true; //P0 - P1, P0, P1 vector<tuple<ll, ll, ll> >V; ll sump0 = 0; ll sump1 = 0; for(ll next: to[pos]) { ll p0, p1; bool turn; tie(p0, p1, turn) = dfs(next); if(!turn) { //順番は変わらず的よりたくさん取れる if(p0 > p1) { mypoint += p0; tekipoint += p1; } else { //turnは変更せず、敵が徳、なるべく行きたくない sump0 += p0; sump1 += p1; } } else { V.push_back({p0 - p1, p0, p1}); } } sort(V.rbegin(), V.rend()); rep(i, V.size()) { ll _, p0, p1; tie(_, p0, p1) = V[i]; if(myturn) { mypoint += p0; tekipoint += p1; } else { mypoint += p1; tekipoint += p0; } myturn = !myturn; } if(myturn) { mypoint += sump0; tekipoint += sump1; return {mypoint, tekipoint, true}; } else { mypoint += sump1; tekipoint += sump0; return {mypoint, tekipoint, false}; } } int main(){ ll N; cin >> N; rep(i, N-1) { ll n; cin >> n; n--; to[n].push_back(i+1); } ll a, b, c; tie(a, b, c) = dfs(0); cout << b << endl; }
#include <bits/stdc++.h> #define ll long long #define V vector<long long> #define VV vector<vector<long long>> #define VVV vector<vector<vector<long long>>> #define P pair<ll,ll> #define rep(i,n) for(ll (i)=0;(i)<(n);++(i)) using namespace std; template<typename T> struct segment_tree{ function<T(T,T)> f = [](T a, T b){return a+b;}; T inf=0; vector<T> value; int N; segment_tree(int n){ N=1; while(N<n)N*=2; value.assign(2*N-1,inf); } segment_tree(int n, function<T(T,T)> func, T t1){ N=1; while(N<n)N*=2; value.assign(2*N-1,t1); f=func; inf=t1; } void update(int i,T x){ i+=N-1; value[i]=x; while(i>0){ i=(i-1)/2; value[i]=f(value[i*2+1],value[i*2+2]); } } T query(T a,T b){ return query(a,b,0,0,N); } T query(T a,T b, int k, int l, int r){ if(r<=a || b<=l)return inf; if(a<=l && r<=b){ return value[k]; }else{ ll c1=query(a,b,2*k+1,l,(l+r)/2); ll c2=query(a,b,2*k+2,(l+r)/2,r); return f(c1,c2); } } void print(){ for(int i=0;i<ceil(log2(N))+1;i++){ for(int j=0;j<(1<<i);j++){ cout<<value[j+(1<<i)-1]<<((j==(1<<i)-1)?';':','); } cout<<endl; } } }; int main() { ll h,w,m; cin>>h>>w>>m; V tatem(w,h); V yokom(h,w); VV mm(h,V()); rep(i,m){ ll x,y; cin>>y>>x; x--;y--; tatem[x]=min(tatem[x],y); yokom[y]=min(yokom[y],x); mm[y].push_back(x); } ll ans=0; rep(i,yokom[0]){ ans+=tatem[i]; } segment_tree<ll> sg(w); rep(i,tatem[0]){ if(i==0){ rep(j,w){ if(j<yokom[0]){ sg.update(j,0); }else{ sg.update(j,1); } } }else{ ans+=sg.query(0,yokom[i]); for(ll x:mm[i]){ sg.update(x,1); } } } cout<<ans<<endl; }
#include<iostream> #include<algorithm> #include<vector> using namespace std; const int MAX = 200000; template<class T> class segtree{ public: T *tree; T u; int height,treeSize,dataSize; segtree(long long int length,T unit){ long long int pre = 1; height=1; u=unit; while(pre<length){ pre*=2; height++; } treeSize = pre*2-1; dataSize = pre; tree = new T[treeSize]; for(int i=0;i<dataSize;i++)set(i,unit); construct(); } void replace(long long int index, T value){ tree[treeSize-dataSize+index]=value; index=treeSize-dataSize+index+1; while(index>0){ index/=2; tree[index-1]=f(tree[index*2-1],tree[index*2]); } } T result(long long int l,long long int r){return result(l,r,0,1<<(height-1),0);} T result(long long int l,long long int r,int s,int len,long long int ind){ if(l==r)return get(l); else if(l==s && r==s+len-1)return tree[ind]; len/=2; T res=u; long long int L=s,R=s+len-1; if(!(r<L || R<l))res=f(result(max(L,l),min(R,r),L,len,ind*2+1),res); L+=len;R+=len; if(!(r<L || R<l))res=f(result(max(L,l),min(R,r),L,len,ind*2+2),res); return res; } void construct(){for(int i=treeSize-2;i>0;i-=2)tree[i/2]=f(tree[i],tree[i+1]);} void set(long long int index, T value){tree[treeSize-dataSize+index]=value;} T get(long long int index){return tree[treeSize-dataSize+index];} T f(T a,T b){ return a+b; } }; int main(){ bool flag=true; long long int H,W,M,X,Y,ans=0; vector<long long int> row[MAX]; cin>>H>>W>>M; segtree<long long int> seg(W,0); for(int i=0;i<W;i++)seg.set(i,0); seg.construct(); for(int i=0;i<M;i++){ cin>>X>>Y;X--;Y--; row[X].push_back(Y); } for(int i=0;i<H;i++){ row[i].push_back(W); sort(row[i].begin(),row[i].end()); if(row[i][0]==0)flag=false; ans+=row[i][0]; if(!flag)ans-=seg.result(0,row[i][0]-1); if(i==0){ for(int j=row[i][0];j<W;j++)seg.set(j,1); seg.construct(); }else{ for(int j=1;j<row[i].size();j++){ if(row[i][j-1]+1==row[i][j])continue; ans+=(row[i][j]-1)-(row[i][j-1]+1)+1-seg.result(row[i][j-1]+1,row[i][j]-1); } for(int j=0;j<row[i].size()-1;j++)seg.replace(row[i][j],1); } } cout<<ans; return 0; }
#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<string> using namespace std; int shu1,shu2,shu3,shu4; int shu; int main() { cin>>shu1>>shu2>>shu3>>shu4; shu=min(shu1,min(shu2,min(shu3,shu4))) ; cout<<shu; }
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int h, w, a, b; cin >> h >> w >> a >> b; int t = h * w; int m = 1 << (h * w); int dp[m][a + 1]; memset(dp, 0, sizeof dp); dp[0][0] = 1; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { for (int k = 0; k < (1 << (i * w + j)); k++) { for (int l = 0; l < a; l++) { if (i > 0) { int temp = (1 << (i * w - w + j)) | (1 << (i * w + j)); if ((temp & k) == 0) dp[temp | k][l + 1] += dp[k][l]; } if (j > 0) { int temp = (1 << (i * w - 1 + j)) | (1 << (i * w + j)); if ((temp & k) == 0) dp[temp | k][l + 1] += dp[k][l]; } } } } } int ans = 0; for (int i = 0; i < m; i++) { ans += dp[i][a]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define eni(x) template < class c > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { template < class c > struct rge { c b, e; }; template < class c > rge<c> range(c i, c j) { return rge<c>{i, j}; } template < class c > auto dud(c* x) -> decltype(cerr << *x, 0); template < class c > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; return * this; } eni(==) return * this << range(begin(i), end(i)); } template < class c, class b > debug & operator <<(pair < b, c > d) { return * this << "(" << d.first << ", " << d.second << ")"; } template < class c > debug & operator <<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; return * this << "]"; } #else template < class c > debug & operator <<(const c&) { return * this; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define ll long long #define pb push_back #define ff first #define ss second #define pll pair<ll,ll> #define pii pair<int,int> #define all(v) v.begin(),v.end() #define mod 1000000007 #define mx 1000050 mt19937 myrand(time(0)); int main() { ios::sync_with_stdio(0); cin.tie(),cout.tie(); int n; cin>>n; set<int> s; for(int i=0;i<=200001;i++) s.insert(i); for(int i=0;i<n;i++){ int x; cin>>x; s.erase(x); cout<<*s.begin()<<endl; } return 0; }
//go to line 53 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 MX ((int)(2e5+5)) //* write code from here int fen[MX]; void add(int idx){ for(;idx<MX;idx += (idx&(-idx))) fen[idx]++; } int sum(int idx){ int val = 0; for(;idx>0;idx -= (idx &(-idx))) val +=fen[idx]; return val; } void solve(){ int n; cin>>n; vii a(n), b(n); for(auto &aa:a)cin>>aa; for(auto &bb:b)cin>>bb; for(int i=0;i<n;++i){ a[i] +=i; b[i] +=i; } map<int,queue<int>> map; f(i, 0, n) map[b[i]].push(i+1); f(i, 0, n){ int t = a[i]; if(map.find(t) == map.end() || map[t].empty()){ cout<<-1<<endl; return; } a[i] = map[t].front(); map[t].pop(); } ll ans = 0; f(i, 0, n){ ans += sum(MX-3)-sum(a[i]); add(a[i]); } cout<<ans<<endl; } int main() { fast_io(); cerr << "...............Console is yours! :)................." << endl; //freopen("input.in","r",stdin); solve(); cerr<<"......^_^....."<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int p = 31; const int m = 1e9 + 7; typedef long long ll; typedef int in; #define ff first #define ss second void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define pb push_back typedef long long ll; typedef long double ld; typedef pair <int, int> pii; typedef pair <ll, ll> pll; const int N = 1e2 + 10, M = N * N; int seen[N]; vector<pii> g[N]; vector<int>type(M, -1); void dfs(int u, int par, vector<pii>& edges) { seen[u] = 1; for (auto& [v, e] : g[u]) { if (v == par or seen[v] == 2) { continue; } if (seen[v]) { if (edges[e].first == u) { type[e] = 2; } else { type[e] = 1; } } else { if (edges[e].first == u) { type[e] = 2; } else { type[e] = 1; } dfs(v, u, edges); } } seen[u] = 2; } int main() { int n, m; cin >> n >> m; vector<pii> edges(m); for (auto& [a, b] : edges) { cin >> a >> b; --a; --b; } vector<int> c(n); for (auto& i : c) { cin >> i; } for (int i = 0; i < m; ++i) { int a = edges[i].first, b = edges[i].second; if (c[a] == c[b]) { g[a].pb({b, i}); g[b].pb({a, i}); continue; } if (c[a] < c[b])type[i] = 1; else type[i] = 2; } for (int i = 0; i < n; ++i) { if (!seen[i]) { dfs(i, -1, edges); } } for (int i = 0; i < m; ++i) { if (type[i] == 1) { cout << "<-\n"; } else { cout << "->\n"; } } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i=0;i<(int)(n);i++) int main() { int n,x; cin >> n >> x; vector<int> a(n), b(n); int i = 0; rep(i,n){ cin >> a[i]; if(a[i] != x) { b[i] = a[i]; cout << b[i] << " "; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1000000007; typedef int64_t ll; typedef uint64_t ull; #define FOR(i, start, end) for(uint64_t i=start; i<end; i++) #define REP(i, n) FOR(i, 0, n) // 最大公約数gcd // 最小公倍数lcm=m*n/gcd uint64_t gcd(uint64_t m, uint64_t n) { uint64_t temp; while (m % n != 0){ temp = n; n = m % n; m = temp; } return n; } uint64_t lcm(uint64_t m, uint64_t n) { uint64_t g = gcd(m,n); if(m%g==0) return (m/g)*n; if(n%g==0) return (n/g)*m; return (m*n)/gcd(m,n); } // vector<vector<uint64_t> > v(n+1, vector<uint64_t>(n+1, 0)) // v[n][k]に組み合わせ数が入る。 void comb(vector<vector <uint64_t> > &v){ for(uint64_t i = 0;i <v.size(); i++){ v[i][0]=1; v[i][i]=1; } for(uint64_t k = 1;k <v.size();k++){ for(uint64_t j = 1;j<k;j++){ v[k][j]=(v[k-1][j-1]+v[k-1][j]); } } } // 掛け算オーバーフロー判定 bool is_product_overflow(uint64_t a, uint64_t b) { uint64_t prod = a * b; return (prod / b != a); } //素因数分解 void primeFactorization(uint64_t a, list<uint64_t> &factors){ //素因数分解を出力するプログラム long i,sq; if(a%2==0){ //偶数の場合 factors.push_back(2); primeFactorization(a/2,factors); //2で割った値で再帰 return; } sq = sqrt(a); for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合 if(a%i==0){ factors.push_back(i); primeFactorization(a/i,factors); //割れた値で再帰 return; } } //偶数でも3以上√a以下の奇数の場合でも割り切れない場合 if(a!=1){ //aが1でないなら、a自身は素数 factors.push_back(a); } } // フェルマーの小定理 // mod. m での a の逆元 a^{-1} を計算する // (a/b)%m = a*movinv(b,m)%m int64_t modinv(int64_t a, int64_t m) { int64_t b = m, u = 1, v = 0; while (b) { int64_t t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } /* //エラトステネスのふるい int maxNum = 100000; char isNotPrime[maxNum+1] = {}; isNotPrime[0]=isNotPrime[1]=1; for(int i=2;i<maxNum;i++){ if(isNotPrime[i]==1) continue; int num = i+i; while(num<=maxNum){ isNotPrime[num]=1; num += i; } } */ // 円周率 // M_PI // #include <iomanip> // setprecisionを使用するのに必要 // cout << std::fixed << std::setprecision(15) << y << endl; // 昇順 // priority_queue<int, vector<int>, greater<int> > queue; // 降順ソート vector<int> v; // sort(v.begin(),v.end(),greater<int>()); signed main() { ll n = 0; ll m = 1000; for(int i=0;i<4;i++){ cin >> n; m = min(m,n); } cout << m << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; // -------------------------------------------------------- template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; } #define FOR(i,l,r) for (ll i = (l); i < (r); ++i) #define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define SUMLL(c) accumulate(ALL(c), 0LL) #define COUNT(c,v) count(ALL(c),(v)) #define SZ(c) ((ll)(c).size()) #define BIT(b,i) (((b)>>(i)) & 1) #define PCNT(b) __builtin_popcountll(b) #define CIN(c) cin >> (c) #define COUT(c) cout << (c) << '\n' #define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n' ll llceil(ll a, ll b) { return (a + b - 1) / b; } ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); } string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; } string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; } using P = pair<ll,ll>; using VP = vector<P>; using VVP = vector<VP>; using VS = vector<string>; using VVS = vector<VS>; using VLL = vector<ll>; using VVLL = vector<VLL>; using VVVLL = vector<VVLL>; using VB = vector<bool>; using VVB = vector<VB>; using VVVB = vector<VVB>; using VD = vector<double>; using VVD = vector<VD>; using VVVD = vector<VVD>; using VLD = vector<ld>; using VVLD = vector<VLD>; using VVVLD = vector<VVLD>; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const ll MOD = 1000000007; // static const ll MOD = 998244353; static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1 // -------------------------------------------------------- // #include <atcoder/all> // using namespace atcoder; // Editorial AC int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); ll N; cin >> N; VLL A(N); REP(i,N) cin >> A[i]; VLL B(N); REP(i,N) cin >> B[i]; VLL E,O; // Even,Odd REP(i,N) { if (i % 2 == 0) { E.push_back(B[i] - A[i]); } else { O.push_back(B[i] - A[i]); } } RSORT(E); RSORT(O); ll score = SUMLL(A); ll ans = score; REP(i,N/2) { score += E[i] + O[i]; chmax(ans, score); } COUT(ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define vll vector<ll> #define vvll vector<vll> #define mll map<ll,ll> #define f(i,m,n) for(i=m;i<n;i++) #define ee endl #define pb push_back #define mp make_pair #define ff first #define ss second #define take(v,n) for(i=0;i<n;i++) cin>>v[i]; #define print(v,n) for(i=0;i<n;i++) cout<<v[i]<<" "; #define No cout<<"NO"<<endl;return #define Yes cout<<"YES"<<endl;return #define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sot(v) sort(v.begin(),v.end()); #define PI 3.14159265 ; ll mod =1e9+7; bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i*i <= n; i = i + 6) if (n%i == 0 || n % (i + 2) == 0) return false; return true; } ll powe(ll x, ll y) { ll res = 1; while (y > 0) { if (y & 1) res =( res * x)%mod; y = y >> 1; x = (x * x)%mod; } return res; } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; unsigned long long power(unsigned long long x, int y, int p) { unsigned long long res = 1; // Initialize result x = x % p; // Update x if it is more than or while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } 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) return 0; // Base case if (r == 0) return 1; unsigned long long fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } void solve() { ll i, x,m, y, j, k, h, n, w, k1, k2,k3,q; cin>>n>>x; x*=100; ll sum=0; for(i=0;i<n;i++) { cin>>k1>>k2; ll temp=k1*k2; sum+=temp; //cout<<sum<<ee; if(sum>x) { cout<<i+1<<ee; return; } } cout<<-1; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast; ll i, j, l, k, t, n; t=1; for (i = 1; i <= t; i++) { //cout<<"Case #"<<i<<": "; solve(); } } //
#include <iostream> using namespace std; int main(){ int n, t=0, x; double s; cin >> n >> s; s = (int)s*100; int a[n+1]={0}, b[n+1]={0}; for (int i=1; i<=n; i++) cin >> a[i] >> b[i]; for (int i=1; i<=n; i++){ x = a[i]*b[i]; t += x; if (t > s){ cout << i << endl; return 0; } } cout << -1 << endl; 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; typedef vector<vector<ll>> Graph; bool bipartite_coloring(int now, int col, Graph &G, vector<int> &mark){ //vector<int> mark(N, -1) を渡す! if(mark[now] != -1) return (mark[now] == col); mark[now] = col; bool ok = true; for(auto next: G[now]){ ok &= bipartite_coloring(next, 1-col, G, mark); } return ok; } int main(){ ll N, M; cin >> N >> M; V<ll> A(M), B(M); REP(i, M){ cin >> A[i] >> B[i]; A[i]--, B[i]--; } ll res = 0; REP(mask, 1ll<<N){ Graph G(N); ll cnt = 1; V<int> mark(N, -1); REP(i, M){ if((mask >> A[i] & 1) and (mask >> B[i] & 1)){ goto HELL; } if((!(mask >> A[i] & 1)) and (!(mask >> B[i] & 1))){ G[A[i]].push_back(B[i]); G[B[i]].push_back(A[i]); } } REP(i, N){ if(!(mask >> i & 1)){ if(mark[i] == -1){ if(bipartite_coloring(i, 0, G, mark)){ cnt *= 2; }else{ cnt *= 0; } } } } res += cnt; HELL:; } cout << res << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define REP(i,n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define debug(var) do{cout << #var << " : "; view(var);}while(0) template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;} template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;} using namespace std; template<class T> void view(T e) {cout << e << endl;} template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;} template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}} using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int,int>; const int inf = 1<<30; const ll inf_l = 1LL<<61; const int MAX = 1e5; ll f(ll x, ll n) { ll sum = 0; for (int i = 1; i <= n; i++) { if (i + 2 > x) break; ll j = x - i; ll k = max<ll>(0, j - n); if (k > n) k = n; sum += n * k; ll l = (j - 1) - (k + 1) + 1; if (l >= 1) sum += l * (l + 1) / 2; if (n < j - 1) { l = j - 1 - n; sum -= l * (l + 1) / 2; } // debug(i); // debug(sum); } return sum; } map<ll, vvll> g(ll n) { map<ll, vvll> mp; REP(i,n+1)REP(j,n+1)REP(k,n+1) { mp[i + j + k].push_back(vll{i, j, k}); } return mp; } int main() { ll n, k; cin >> n >> k; // debug(f(n * 3 - 1, n)); // debug(f(14, n) - f(13, n)); // debug(f(12, n)); // auto res = g(n); // debug((int)res[14].size()); // debug(f(n * 3, n)); ll ok = 0, ng = 3 * n + 1; while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid, n) < k) ok = mid; else ng = mid; } k -= f(ok, n); int first = -1; ll sum_1 = ng; // debug(sum_1); // debug(k); for (int i = 1; i <= n; i++) { // debug(i); ll tmp = sum_1 - i; ll l = max<ll>(1, tmp - n); ll r = min<ll>(n, tmp - 1); ll len = r - l + 1; if (len <= 0) continue; // debug(len); if (k <= len) { first = i; break; } else k -= len; } // debug(k); // debug(first); ll sum_2 = sum_1 - first; // debug(sum_2); int second = -1; for (int i = 1; i <= n; i++) { if (sum_2 - i <= 0 || sum_2 - i > n) continue; k--; if (k == 0) { second = i; break; } } cout << first << " " << second << " " << sum_2 - second << endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #define fi first #define se second #define rep(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define pb push_back 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(a>b){ a=b; return true; } return false; } constexpr int INF=1000000000; constexpr ll llINF=3000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-8; const double pi=acos(-1); int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int Random(int mi,int ma){ random_device rnd; mt19937 mt(rnd());//32bit //[mi,ma] uniform_int_distribution<int>engine(mi,ma); return engine(mt); } /* vector<vector<ll>>C,sC; void init_comb(int n,int m){ C.resize(n+1,vector<ll>(m+1,0)); sC.resize(n+1,vector<ll>(m+1,0)); C[0][0]=1; for(int i=1;i<=n;i++){ C[i][0]=1; for(int j=1;j<=m;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } rep(i,n+1){ rep(j,m){ sC[i][j+1]=(sC[i][j]+C[i][j])%mod; } } }*/ ll gcd(ll a,ll b){ while(a%b){ a%=b; swap(a,b); } return b; } ll lcm(ll a,ll b){ return a/gcd(a,b)*b; } bool isprime(int a){ if(a==1)return false; for(int i=2;i*i<=a;i++){ if(a%i==0)return false; } return true; } vector<int>primes; void init_prime(int n){ primes.push_back(2); for(int i=3;i<=n;i+=2){ bool f=true; for(int j:primes){ if(j*j>i)break; if(i%j==0){ f=false; break; } } if(f)primes.push_back(i); } } ll modpow(ll a,ll b){ ll res=1; while(b){ if(b&1){ res*=a; res%=mod; } a*=a; a%=mod; b>>=1; } return res; } vector<ll>inv,fact,factinv; void init_fact(int n){ inv.resize(n+1); fact.resize(n+1); factinv.resize(n+1); inv[0]=0; inv[1]=1; fact[0]=1; factinv[0]=1; for(ll i=1;i<=n;i++){ if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod); fact[i]=(fact[i-1]*i)%mod; factinv[i]=factinv[i-1]*inv[i]%mod; } } ll _inv(ll a,ll m=mod){ //gcd(a,m) must be 1 ll b=m,u=1,v=0; while(b){ ll t=a/b; a-=t*b;swap(a,b); u-=t*v;swap(u,v); } u%=m; if(u<0)u+=m; return u; } ll comb(int a,int b){ if(a<b)return 0; if(a<0)return 0; return fact[a]*factinv[a-b]%mod*factinv[b]%mod; } ll multicomb(int a,int b){ return comb(a+b-1,b); } int n,m; vector<int>g[1010][26]; int dp[1010][1010]; #define P pair<int,int> #define PP pair<int,P> void solve(){ cin>>n>>m; priority_queue<PP,vector<PP>,greater<PP>>q; rep(i,n)rep(j,n)dp[i][j]=INF; rep(i,n){ dp[i][i]=0; q.push({0,{i,i}}); } rep(i,m){ int a,b; char c; cin>>a>>b>>c; a--,b--; g[a][c-'a'].push_back(b); g[b][c-'a'].push_back(a); if(a!=b){ dp[a][b]=dp[b][a]=1; q.push({1,{a,b}}); q.push({1,{b,a}}); } } while(!q.empty()){ PP p=q.top(); q.pop(); int l=p.second.first,r=p.second.second; if(dp[l][r]!=p.first)continue; rep(k,26){ for(int a:g[l][k]){ for(int b:g[r][k]){ if(chmin(dp[a][b],p.first+2)){ q.push({dp[a][b],{a,b}}); } } } } } cout<<(dp[0][n-1]==INF?-1:dp[0][n-1])<<endl; } int main(){ cin.tie(0);ios::sync_with_stdio(false); //int t;cin>>t;while(t--) solve(); return 0; }
/*** Bismillahir Rahmanir Rahim Rabbi Zidni Ilma Author : Saidur Rahman Department of CSE, City University, Bangladesh. ***/ #include<bits/stdc++.h> using namespace std; #define endl "\n" #define EPS 1e-6 #define F first #define sf scanf #define pf printf #define S second #define done return 0 #define MP make_pair #define pb push_back #define start int main() #define PI acos(-1.0) #define NO cout<<"NO\n" #define No cout<<"No\n" #define no cout<<"no\n" #define YES cout<<"YES\n" #define Yes cout<<"Yes\n" #define yes cout<<"yes\n" #define sz(x) int(x.size()) #define all(a) a.begin(),a.end() #define srt(x) sort(x.begin(),x.end()) #define clock_start clock_t tStart = clock() #define MEM(a, b) memset(a, (b), sizeof(a)) #define watch(x) cout << (#x) << " is " << (x) << endl #define Fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define show_time cout<<"\n\nTime Taken: "<<fixed<<setprecision(3)<<(double)(clock() - tStart) / CLOCKS_PER_SEC<<"s" /****** Template of some bit operations *****/ long long ToggleBit (long long n, long long X) { return n ^ (1LL << X); }//k-1 tomo bit 1 hole 0 korbe r 0 hole 1 long long SetBit (long long n, long long X) { return n | (1LL << X); }//k-1 tomo bit 0 hole 1 korbe, 1 hole unchange long long ClearBit (long long n, long long X) { return n & ~(1LL << X); }//k-1 tomo bit 1 hole 0 korbe, 0 hole unchange bool CheckBit (long long n, long long X) { return (bool)(n & (1LL << X)); }//k-1 tomo bit 1 naki 0 bolbe /////////////////////////////////////////////////////////////////////////// ///////////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////// ////////////////////////////////////////////////////////////////////////// typedef long long ll; const int MAX = 1e5 + 5; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e18; start { Fast; #ifndef ONLINE_JUDGE clock_start; freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s1, s2; cin >> s1 >> s2; int cnt1=0, cnt2=0; for (int i = 0; i < sz(s1);i++) { cnt1 += (s1[i] - '0'); } for (int i = 0; i < sz(s2);i++) { cnt2 += (s2[i] - '0'); } cout << max(cnt1, cnt2) << endl; #ifndef ONLINE_JUDGE show_time; #endif done; }
//ABHISHEK AGRAWAL,BIT mesra// //Newbie......You have to be odd to be no. ONE :)// //Authhor: Abhishekagrawal //Date: 20/02/2021 //Time: 17:30:13 #include<bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define fl float #define vll vector<ll> #define pii pair<int,int> #define vpll vector<pair<ll,ll>> #define pb push_back #define f first #define s second #define mll map<ll,ll> #define mp make_pair #define sp(n) fixed<<setprecision(n) #define mod (ll)1000000007 #define sortv(v) sort(v.begin(),v.end()) #define INF (ll)(1e15) #define loop(i,n) for(int i=0;i<n;i++) #define loop1(i,n) for(int i=1;i<=n;i++) #define rloop(n,i) for(int i=n-1;i>=0;i--) #define sorta(a) sort(a,a+n,greater<ll>()) #define countone(n) __builtin_popcount(n) #define numoftrailzero(n) __builtin_ctz(n) #define maxpowoftwo(n) __builtin_clz(n) #define leastindexwithone(n) __builtin_ffs(n) #define what_is(x) cerr << #x << " is " << x << endl; #define pfv(v) cout<<v.size()<<"\n";loop(i,v.size()) cout<<v[i]<<" ";cout<<"\n"; #define pv(v) loop(i,v.size()) cout<<v[i]<<" "; #define all(v) v.begin(),v.end() #define mset(dp,val) memset(dp,val,sizeof(dp)) ll test,n;ll sum,ans; // #ifdef TESTING // #define DEBUG fprintf(stderr, "====TESTING====\n") // #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl // #define debug(...) fprintf(stderr, __VA_ARGS__) // #else // #define DEBUG // #define VALUE(x) // #define debug(...) // #endif void the_happiest_place_on_earth() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #ifdef ENABLE_FILE_IO freopen("out.txt", "r", stdin); freopen("out1.txt", "w", stdout); #endif } // bool comp(const pair<ll,ll> &a, const pair<ll,ll> &b){ // if(a.s+2*a.f==b.s+2*b.f) return a.f>b.f; // return (a.s+2*a.f>b.s+2*b.f); // } const int N=200040;ll a[N],b[N],c[N],par[N],Size[N],present[N],color[N]; ll power(ll a,ll b,ll M){a%=M;ll res=1;while(b>0){if(b%2 ==1){res=(res*a)%M;}a=(a*a)%M;b/=2;} return res;} void testcase(){ ll m; cin>>n>>m;ll k;cin>>k; ll mo=998244353; if(n==1){ cout<<power(k,m,mo); return; } if(m==1){ cout<<power(k,n,998244353); return; } ans=0; loop1(i,k){ ll n1=power(i,n,mo); ll n2=power(i-1,n,mo); n1=((n1-n2)%mo+mo)%mo; n2=power(k-i+1,m,mo); n1=(n1*n2)%mo; (ans+=n1)%=mo; } cout<<ans<<"\n"; return; } int main(){ the_happiest_place_on_earth(); // Today's thought: Push yourself,because no one else is going to do it for you. // Things are not always #000000 and #FFFFFF // START OF PROGRAM LOGIC // g++ -o a BB.cpp -DENABLE_FILE_IO;./a // g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -o a file_name.cpp;./a test=1;int t=0; // cin>>test; while(test--){ //cout<<"Case #"<<++t<<": "; testcase(); } cerr << "Time : " << 1000 *((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; // //END OF PROGRAM LOGIC return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T,class U> using P = pair<T,U>; template<class T> using vec = vector<T>; template<class T> using vvec = vector<vec<T>>; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) ll M = 998244353; ll mpow(ll x, ll n){ //x^n(mod M) ←普通にpow(x,n)では溢れてしまうため,随時mod計算 ll ans = 1; while(n != 0){ if(n&1) ans = ans*x % M; x = x*x % M; n = n >> 1; } return ans; } // vector<ll> fac(300001); //n!(mod M) // vector<ll> ifac(300001); //k!^{M-2} (mod M) // ll comb(ll a, ll b){ //aCbをmod計算 // if(a == 0 && b == 0)return 1; // if(a < b || a < 0)return 0; // ll tmp = ifac[a-b]* ifac[b] % M; // return tmp * fac[a] % M; // } int main(){ // fac[0] = 1; // ifac[0] = 1; // for(ll i = 0; i<300000; i++){ // fac[i+1] = fac[i]*(i+1) % M; // n!(mod M) // ifac[i+1] = ifac[i]*mpow(i+1, M-2) % M; // k!^{M-2} (mod M) ←累乗にmpowを採用 // } int n, m, k; cin >> n >> m >> k; if (n == 1 & m == 1) { cout << k << endl; return 0; } if (n == 1 | m == 1) { ll ans = 0; for(int d = 1; d <= k; d++) { ans += (mpow(d, n) - mpow(d - 1, n)) * (mpow(k-d + 1, m) - mpow(k - d, m)); ans += M; ans %= M; } cout << ans << endl; return 0; } // if (m == 1) { // ll ans = 0; // for(int a0 = 1; a0 <= k; a0++) { // ans += (mpow(d, n) - mpow(d - 1, n)) * (mpow(k-d + 1, m) - mpow(k - d, m)); // ans += M; // ans %= M; // } // cout << ans << endl; // return 0; // } ll ans = 0; ll tmp = 0; for(int d = 1; d <= k; d++) { ans += (mpow(d, n) - mpow(d - 1, n)) * mpow(k-d + 1, m); ans += M; ans %= M; } cout << ans << endl; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> #define ll long long #define ull unsigned long long #define all(x) (x).begin(),(x).end() #define print(x) if(x)cout<<"YES\n";else cout<<"NO\n"; #define pb push_back #define F first #define S second //#define mod 1000000007 #define mod 998244353 using namespace __gnu_pbds; using namespace std; long long int binaryexpo(long long x, long long n) { if (n == 0) return 1; else if (n & 1) return (x * binaryexpo(x * x, (n - 1) / 2)); else return binaryexpo(x * x, (n / 2)); } ll score(string x) { ll freq[10] = {}; for (auto c : x) { freq[c - '0']++; } ll sc = 0; for (ll i = 1; i <= 9; i++) { ll temp = i * binaryexpo(10, freq[i]); sc += temp; } return sc; } void solve() { cout << fixed << setprecision(20); ll k; cin >> k; vector<ll> v(9, k); for (int i = 0; i < 9; i++) { v[i] = k; } string s, t; cin >> s >> t; for (auto x : s) { v[x - '1']--; } for (auto x : t) { v[x - '1']--; } ll total = (9 * k - 8) * (9 * k - 9), cnt = 0; long double ans = 0.00; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i == j && v[i - 1] > 1) { string a = s.substr(0, 4); a.pb((i + '0')); string b = t.substr(0, 4); b.pb((i + '0')); ll x = score(a); ll y = score(b); if (x > y) { ll aa = v[i - 1] * (v[i - 1] - 1) ; ans += (long double)aa / total ; cnt += aa; } } else if (i != j && v[i - 1] > 0 && v[j - 1] > 0) { string a = s.substr(0, 4); a.pb((i + '0')); string b = t.substr(0, 4); b.pb((j + '0')); ll x = score(a); ll y = score(b); if (x > y) { ll aa = v[i - 1] * v[j - 1]; ans += (long double)aa / total; cnt += aa; } } } } // cout << cnt << " " << total << "\n"; // ans = (long double)cnt / total; cout << ans << "\n"; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif fastio; int T = 1; // cin >> T; while (T--) { solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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; } #define all(x) (x).begin(),(x).end() #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=1000000007,MAX=200005,INF=1<<30; ll rui[20]; int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); rui[0]=1; for(int i=1;i<10;i++) rui[i]=rui[i-1]*10; ll K;cin>>K; string S,T;cin>>S>>T; vector<ll> rem(10,K); double ans=0; for(int i=0;i<4;i++){ rem[int(S[i]-'0')]--; rem[int(T[i]-'0')]--; } for(int a=1;a<=9;a++){ for(int b=1;b<=9;b++){ if(a==b){ if(rem[a]<=1) continue; ll cnt=rem[a]*(rem[a]-1); ll X=0,Y=0; for(int i=1;i<=9;i++){ int ca=0; for(int j=0;j<4;j++){ if(int(S[j]-'0')==i) ca++; } if(i==a) ca++; X+=rui[ca]*i; ca=0; for(int j=0;j<4;j++){ if(int(T[j]-'0')==i) ca++; } if(i==b) ca++; Y+=rui[ca]*i; } if(X>Y) ans+=cnt; }else{ if(rem[a]<=0||rem[b]<=0) continue; ll cnt=rem[a]*(rem[b]); ll X=0,Y=0; for(int i=1;i<=9;i++){ int ca=0; for(int j=0;j<4;j++){ if(int(S[j]-'0')==i) ca++; } if(i==a) ca++; X+=rui[ca]*i; ca=0; for(int j=0;j<4;j++){ if(int(T[j]-'0')==i) ca++; } if(i==b) ca++; Y+=rui[ca]*i; } if(X>Y) ans+=cnt; } } } ll pat=(9*K-8)*(9*K-9); ans/=pat; cout<<fixed<<setprecision(25)<<ans<<endl; }
#include <iostream> #include <algorithm> #include <vector> #define int long long using namespace std; vector<pair<int,int>>x, y; signed main() { int N; ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N; int i; for (i = 0; i < N; i++) { int a, b; cin >> a >> b; x.push_back({ a,i }); y.push_back({ b,i }); } sort(x.begin(), x.end()); sort(y.begin(), y.end()); vector<int>r; r.push_back(x[N - 1].first - x[0].first); r.push_back(x[N - 2].first - x[0].first); r.push_back(x[N - 1].first - x[1].first); if(!((((x[0].second==y[0].second)&&(x[N-1].second==y[N-1].second))||((((x[0].second == y[N-1].second) && (x[N - 1].second == y[0].second))))))) r.push_back(y[N - 1].first - y[0].first); r.push_back(y[N - 2].first - y[0].first); r.push_back(y[N - 1].first - y[1].first); sort(r.begin(), r.end()); cout << r[r.size()-2]; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) #define sz(x) int(x.size()) using namespace std; typedef long long ll; typedef pair<int,int> P; typedef tuple<int,int,int> tp; int main(){ int n; cin >> n; set<int> house; vector<P> h(n); vector<int> X(4); X[0] = X[1] = -1e9; X[2] = X[3] = 1e9; vector<int> idx(4); vector<int> Y(4); Y[0] = Y[1] = -1e9; Y[2] = Y[3] = 1e9; vector<int> idy(4); rep(i,n){ int x,y; cin >> x >> y; if(x > X[0]){ swap(X[0],X[1]); swap(idx[0],idx[1]); X[0] = x; idx[0] = i; } else if(x > X[1]){ X[1] = x; idx[1] = i; } if(x < X[2]){ swap(X[2],X[3]); swap(idx[2],idx[3]); X[2] = x; idx[2] = i; } else if(x < X[3]){ X[3] = x; idx[3] = i; } if(y > Y[0]){ swap(Y[0],Y[1]); swap(idy[0],idy[1]); Y[0] = y; idy[0] = i; } else if(y > Y[1]){ Y[1] = y; idy[1] = i; } if(y < Y[2]){ swap(Y[2],Y[3]); swap(idy[2],idy[3]); Y[2] = y; idy[2] = i; } else if(y < Y[3]){ Y[3] = y; idy[3] = i; } h[i] = make_pair(x,y); } rep(i,4) house.insert(idx[i]); rep(i,4) house.insert(idy[i]); vector<int> dist; for(auto u : house){ for(auto v : house){ if(u == v) continue; dist.push_back(max(abs(h[u].first-h[v].first), abs(h[u].second-h[v].second))); } } sort(dist.begin(),dist.end()); cout << dist[sz(dist)-3] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int a,b,c,d; bool check(int a,int b,int c,int d) { if(a + b == c + d || a - b == c - d || abs(a-c) + abs(b-d) <= 3) return true; return false; } signed main() { cin >> a >> b >> c >> d; if(a == c && b == d) {puts("0"); return 0;} else if(a + b == c + d || a - b == c - d || abs(a-c) + abs(b-d) <= 3) {puts("1"); return 0;} else { for(int i = a - 3; i <= a + 3; ++ i) { for(int j = b - 3; j <= b + 3; ++ j) { if(abs(i - a) + abs(j - b) <= 3) { if(check(i,j,c,d)) {puts("2"); return 0;} } } } } double x1 = (d + c + a - b) / 2.0 , x2 = (a + b + c -d) / 2.0; // cout << x1 << ' ' << x2 << endl; if(x1 == (int)x1 || x2 == (int)x2) puts("2"); else puts("3"); }
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int main(){ long long r1,r2,c1,c2, a, b; cin >> r1 >> c1; cin >> r2 >> c2; int x; a=abs(r2-r1); b=abs(c2-c1); if(a==0&&b==0){x=0;cout<< x <<endl;return 0;} if((a+b)<4||a-b==0){x=1;cout<< x <<endl;return 0;} if((a+b)%2==0||abs(a-b)<4){x=2;cout<< x <<endl;return 0;} x=3; cout<< x <<endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int64_t INF = INT64_C(1) << 60; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; int64_t X; cin >> X; vector<int64_t> A(N); for (auto &&e : A) { cin >> e; } int64_t res = INF; for (int k = 1; k < N + 1; k++) { vector<vector<int64_t>> dp(k + 1, vector<int64_t>(k, -INF)); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = k; j > 0; j--) { for (int r = 0; r < k; r++) { if (dp[j - 1][r] < 0) continue; dp[j][(r + A[i]) % k] = max(dp[j][(r + A[i]) % k], dp[j - 1][r] + A[i]); } } } if (dp[k][X % k] > 0) res = min(res, (X - dp[k][X % k]) / k); } cout << res << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define rep(i, n) for(int i=0;i<(int)(n);i++) #define rep2(i, j, n) for(int i=j;i<(int)(n);i++) #define all(v) v.begin(), v.end() using ll = long long; using P = pair<int, int>; map<int, ll> table; int main(){ int N; cin >> N; ll C; cin >> C; rep(i, N){ int a, b, e; cin >> a >> b >> e; table[a-1] += e; table[b] -= e; } vector<pair<int, ll>> table2; for(pair<int, ll> p : table){ table2.pb(p); } sort(all(table2)); ll rui[table2.size()+1]; ll sum = 0; rui[0] = 0; rep(i, table2.size()){ rui[i+1] = rui[i] + table2[i].second; if(i>0 && rui[i]>C){ sum += (table2[i].first - table2[i-1].first)*C; } else if(i>0) { sum += (table2[i].first - table2[i-1].first)*rui[i]; } } cout << sum << endl; }
#include <bits/stdc++.h> inline int read( ) { int x = 0; char c = getchar( ); while ( !isdigit( c ) ) c = getchar( ); while ( isdigit( c ) ) { x = x * 10 + ( c ^ 48 ); c = getchar( ); } return x; } const int N = 107, mo = 1e9 + 7; inline int qp( int b, int p ) { int ret = 1; while ( p ) { if ( p & 1 ) ret = 1ll * ret * b % mo; b = 1ll * b * b % mo; p >>= 1; } return ret; } int n, m, k, a[ N ], deg[ N ]; struct Matrix { int matrix[101][101]; void clear() { memset(matrix, 0, sizeof(matrix)); } void I() { for (int i = 0; i <= 100; ++ i) matrix[i][i] = 1; } Matrix operator *(const Matrix &a) const { Matrix res; res.clear(); for (int i = 1; i <= n; ++ i) { for (int j = 1; j <= n; ++ j) { for (int k = 1; k <= n; ++ k) { (res.matrix[i][j] += 1ll * matrix[i][k] % mo * a.matrix[k][j] % mo) %= mo; } } } return res; } }mat, ret; int main( ) { n = read( ), m = read( ), k = read( ); for ( int i = 1; i <= n; ++ i ) a[ i ] = read( ); int INV = qp( 2 * m, mo - 2 ); for ( int i = 1, x, y; i <= m; ++ i ) { x = read( ), y = read( ); ++ deg[ x ]; ++ deg[ y ]; mat.matrix[ x ][ y ] = mat.matrix[ y ][ x ] = INV; } for ( int i = 1; i <= n; ++ i ) mat.matrix[ i ][ i ] = 1ll * ( 2 * m - deg[ i ] + mo ) % mo * INV % mo; for ( int i = 1; i <= n; ++ i ) ret.matrix[ 1 ][ i ] = a[ i ]; Matrix ans; ans.clear(); ans.I(); while (k) { if (k & 1) ans = ans * mat; k >>= 1; mat = mat * mat; } ret = ret * ans; for ( int i = 1; i <= n; ++ i ) printf( "%d\n", ret.matrix[ 1 ][ i ] ); return 0; }
#include<bits/stdc++.h> #define rep(i, n) for (int i = 0, length = n; i < length; i++) #define fi first #define se second #define lb lower_bound #define ub upper_bound #define ep emplace #define epb emplace_back #define scll static_cast<long long> #define sz(x) static_cast<int>((x).size()) #define pfll(x) printf("%lld\n", x) #define ci(x) cin >> x #define ci2(x, y) cin >> x >> y #define ci3(x, y, z) cin >> x >> y >> z #define ci4(w, x, y, z) cin >> w >> x >> y >> z #define co(x) cout << x << endl #define co2(x, y) cout << x << " " << y << endl #define co3(x, y, z) cout << x << " " << y << " " << z << endl using namespace std; typedef long long ll; typedef pair<int, int> P; typedef priority_queue<int> PQ; typedef priority_queue<int, vector<int>, greater<int>> PQG; typedef priority_queue<P> PQP; typedef priority_queue<P, vector<P>, greater<P>> PQPG; const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9; int n, m, k; vector<int> to[100]; typedef vector<int> vec; typedef vector<vec> mat; int po(int x, int y) { int ans = 1; while (y > 0) { if (y & 1) ans = static_cast<long>(ans) * x % MOD; x = static_cast<long>(x) * x % MOD; y >>= 1; } return ans; } mat mul(mat &A, mat &B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); i++) { for (int k = 0; k < B.size(); k++) { for (int j = 0; j < B[0].size(); j++) { C[i][j] = (C[i][j] + scll(A[i][k]) * B[k][j] % MOD) % MOD; } } } return C; } mat pow(mat A, long n) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); i++) { B[i][i] = 1; } while (n > 0) { if (n & 1) B = mul(B, A); A = mul(A, A); n >>= 1; } return B; } int main() { ci3(n, m, k); mat a(n, vec(1)); rep(i, n) ci(a[i][0]); rep(i, m) { int x, y; ci2(x, y); to[x - 1].epb(y - 1); to[y - 1].epb(x - 1); } mat PRO(n, vec(n)); rep(i, n) { rep(j, n) PRO[i][j] = 0; for (auto &j: to[i]) PRO[i][j] = 1; PRO[i][i] = (2 * m - to[i].size()) % MOD; } mat POPRO = pow(PRO, k); mat ans = mul(POPRO, a); int pp = po(po(2 * m, MOD - 2), k); rep(i, n) co(scll(ans[i][0]) * pp % MOD); return 0; }
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ll long long #define rep(i,j,n) for(int i=j;i<n;i++) #define scn(a) scanf("%d",&a) #define scns(a,b) scanf("%d %d",&a,&b) #define print(a) printf("%d\n",a) #define vec vector<int> #define pb emplace_back #define pairs pair<int,int> #define f first #define s second #define all(v) v.begin(),v.end() #define mem(a,b) memset(a,b,sizeof(a)) #define inf 1e18 const int mod = 1e9+7; const int N = 5e5+5; int n; int main() { int test; test = 1; while(test--) { string s1; cin>>s1; s1 = "0" + s1; int n = (int)s1.size()-1, st = -1; vector<pair<int,int>> v1; int mp[n+5][26]; memset(mp, 0, sizeof mp); for(int i = 1; i <= n; ++i) ++mp[i][s1[i]-'a']; for(int i = 1; i <= n; ++i) for(int j = 0; j < 26; ++j) mp[i][j] += mp[i-1][j]; for(int i = 1; i <= n-2;) { if(s1[i] == s1[i+1] && s1[i] != s1[i+2]) v1.pb(i, s1[i]-'a'), i += 2; else ++i; } if((int)v1.size() == 0) { print(0); continue; } // cout<<v1.size()<<endl; ll ans = 0; reverse(all(v1)); int ind = -1, prev = -1; // cout<<n<<endl; for(auto [st, ch]:v1) { // cout<<st<<" ch "<<ch<<endl; if(prev == -1) { // cout<<ch<<endl; int val = mp[n][ch] - mp[st+1][ch]; // cout<<val<<endl; ans += n - (st + 1) - val; } else { if(prev == ch) { // cout<<prev<<" "<<ind<<endl; int val = mp[ind-1][ch] - mp[st+1][ch]; // cout<<"VAL "<<val<<endl; ans += n - (st + 1) - val - (n - ind + 1); } else { int val = mp[ind-1][ch] - mp[st+1][ch]; ans += n - (st + 1) - val; } } prev = ch, ind = st; // cout<<ans<<endl; } printf("%lld\n",ans); } return 0; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define mp make_pair #define fi first #define se second #define REP(i,j) for(int i=0;i<j;i++) #define REPA(i,j) for(int i=1;i<=j;i++) #define FORN(i,j,k) for(int i=j;i<k;i++) #define vi vector<int> #define vvi vector<vi > #define pii pair<int,int> #define vpii vector<pii > #define all(a) a.begin(),a.end() using namespace std; const int INF=1<<30; inline void read(int &x){ // short neg=1; x=0; char c=getchar(); /*while(c<'0'||c>'9'){ if(c=='-')neg=-1; c=getchar(); }*/ while(c>='0'&&c<='9'){ x=(x<<3)+(x<<1)+(c^48),c=getchar(); } // x*=neg; } string s; int n; int sum[256]={0}; int main(void){ cin>>s; n=s.size(); reverse(all(s)); ll ans=0; sum[(int)s[0]]++; for(int i=1;i+1<n;i++){ if(s[i]==s[i+1]){ ans+=i-sum[(int)s[i]]; for(char c='a';c<='z';c++)sum[(int)c]=0; sum[(int)s[i]]=i+2; i++; } else{ sum[(int)s[i]]++; } } cout<<ans<<endl; return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<set> #include<queue> #include<vector> #define IL inline #define re register #define LL long long #define ULL unsigned long long #ifdef TH #define debug printf("Now is %d\n",__LINE__); #else #define debug #endif using namespace std; template<class T>inline void read(T&x) { char ch=getchar(); int fu; while(!isdigit(ch)&&ch!='-') ch=getchar(); if(ch=='-') fu=-1,ch=getchar(); x=ch-'0';ch=getchar(); while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();} x*=fu; } inline int read() { int x=0,fu=1; char ch=getchar(); while(!isdigit(ch)&&ch!='-') ch=getchar(); if(ch=='-') fu=-1,ch=getchar(); x=ch-'0';ch=getchar(); while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();} return x*fu; } int G[55]; template<class T>inline void write(T x) { int g=0; if(x<0) x=-x,putchar('-'); do{G[++g]=x%10;x/=10;}while(x); for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n'); } int main() { char ch=getchar(); if(getchar()==ch&&getchar()==ch) cout<<"Won"; else cout<<"Lost"; return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int i = 0 ; i < (n); ++i) using namespace std ; using ll=long long ; using P=pair<int,int> ; const int mod=1000000007; const int N=1e5 ; const int INF=1001001001; void solve(){ string s ; cin>>s; int n=s.length() ; //cout<<n<<"\n" ; ll ans=0 ; map<char,int> mp ; mp[s.back()]=1 ; for(int i=n-2;i>=0;i--){ mp[s[i]]++ ; if(s[i]==s[i+1]){ ans+=n-i-mp[s[i]] ; mp=map<char,int>() ; mp[s[i]]=n-i ; } // cout<<i<<" "<<ans<<"\n" ; } cout<<ans ; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin) ; freopen("output.txt","w",stdout) ; #endif cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int y ; y=1 ; //cin>>y ; while(y--){ solve() ; } return 0 ; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64_t> a(n), b(n); for (auto &ai : a) { cin >> ai; } for (auto &bi : b) { cin >> bi; } int64_t mxa = a[0]; int64_t c = a[0] * b[0]; cout << c << '\n'; for (int i = 1; i < n; ++i) { mxa = max(mxa, a[i]); c = max(c, mxa * b[i]); cout << c << '\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; #define boost \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long int // #define int ll #define vi vector<ll> #define vvi vector<vi> #define pii pair<int, int> #define vii vector<pii> #define pb push_back #define B begin() #define E end() #define sz(A) (ll)(A).size() #define F first #define S second #define loop(z, v) for (auto &z : v) #define sp << ' ' << #define sps << ' ' #define nl '\n' // #define nl endl #define fat 1000000007 // #define fat 998244353 #define PI_2 1.57079632679489661923 int main() { ll tt = 1, n, q, m, l, r, c, i, j, k, w; boost; // cin >> tt; ll ans = 1; for(int _ = 1; _ <= tt; _++){ cin >> l >> r; cout << (l + r) / 2 sp (l - r) / 2 << nl; } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int n = str.length() - 1; char ch = ' '; long long ans = 0; for (int i = 0; i <=n; i++) { if (i !=n &&str[i]!=ch&& str[i] == str[i + 1] && str[i] != str[i + 2]) { ans += n - i -1; ch = str[i]; i++; } else if (ch == str[i]) { ans--; } } cout << ans << "\n"; 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) cerr << #x << " = " << (x) << endl; #define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")"; #define rep(i, n) for(int i = 0;i < n;i++) #define pb push_back #define F first #define S second // template<typename _Ty1, typename _Ty2> // ostream& operator<<(ostream& _os, const pair<_Ty1, _Ty2>& _p) { // _os << '(' <<_p.first << ',' << _p.second << ')'; // return _os; // } // // template<typename _Ty1, typename _Ty2> // istream& operator>>(istream& _is, pair<_Ty1, _Ty2>& _p) { // _is >> _p.first >> _p.second; // return _is; // } void solve() { string s; cin>>s; int n = s.size(); vector<int> F(26); int i = n-3, j = n-2, k = n-1; ll ans = 0; while (i >= 0) { if(s[i] == s[j] && s[j] != s[k]) { ans += n-k-1-F[s[i]-'a']+1; F.assign(26,0); F[s[i]-'a']= n-1-j; } else { F[s[k]-'a']++; } i--;j--;k--; } cout<<ans<<endl; } // 0123....i=j!=k......n-1 // aababbbbb // aaaabbbbb // aaaaabbbb // d+1, d+2, d+3, ..., d' ,... , M // 0 [1] // 1 [1] // 2 [2,4,8,6,2] // 3 [3,9,7,1,3] // 4 [4,6,4] // 5 [1] // 6 [1] // 7 [7,9,3,1,7] // 8 [8,4,2,6,8] // 9 [9,1,9] int main() { // freopen("input.in","r",stdin); // freopen("output.out","w",stdout); // cout << fixed << setprecision(15);2000000000 ios_base::sync_with_stdio(false); cin.tie(nullptr); // prime_factors(); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #ifdef __GXX_EXPERIMENTAL_CXX0X__ #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #ifdef __GXX_EXPERIMENTAL_CXX0X__ #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif using namespace std; #define mod 1000000007 #define mod2 998244353 #define pb push_back #define ll long long #define fi first #define lb lower_bound #define test ll int t;cin>>t;while(t--) #define se second #define pll pair<long long,long long> #define icc ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); ll gcd(ll a, ll b){ if(!a)return b; return gcd(b%a,a); } long long power(long long a,long long b){ long long ans=1; while(b>0){ if(b&1){ans=(ans*a);} a=(a*a); b>>=1; } return ans; } vector<ll> spf,prime; void primes(ll n){ spf.assign(n+1,0); spf[1] = 1; for(ll i=2; i<=n; i++){ if (!spf[i]){ spf[i]=i;prime.pb(i); for (ll j=i*i; j<=n; j+=i) if (!spf[j]) spf[j] = i; } } } ll fact[1000001]; void factorial(ll n){ fact[0]=1; for(ll i=1;i<=n;i++){ fact[i]=fact[i-1]*i; if(fact[i]>=mod) fact[i]%=mod; } } priority_queue<ll,vector<ll>,greater<ll> > qqqqq; void solve(){ ll a,b,c; cin>>a>>b>>c; if(c&1){ if(a>b)cout<<">"; else if(a==b)cout<<"="; else cout<<"<"; } else{ if(abs(a)>abs(b))cout<<">"; else if(abs(a)==abs(b))cout<<"="; else cout<<"<"; } } int main(){ icc; //test solve(); return 0; } // ll op; // cin>>op; // for(ll fff=1;fff<=op;fff++){ // cout<<"Case #"<<fff<<": "<<ans<<endl; // }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(0); int a, b, c; cin >> a >> b >> c; if(c % 2 == 0) { a = abs(a); b = abs(b); } if(a == b) cout << "="; else cout << (a > b ? ">" : "<"); }
#include <algorithm> #include <iostream> #include <map> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { vector<int> vec(3); for (int i = 0; i < 3; i++) { cin >> vec[i]; } sort(vec.begin(), vec.end(), greater<int>()); cout << vec[0]+vec[1] << endl; }
///////////////////////Loading Payloads.......//////////////////////////// #include<bits/stdc++.h> #define f(i,a,b) for(int i = a; i < b; i++) #define fd(i,a,b) for(int i = a; i > b;i--) #define fld(i,a,b) for(ll i = a ; i > b;i--) #define ll long long #define fl(i,a,b) for(ll i = a; i < b; i++) #define VI vector<int> #define VP vector<pair<int,int>> #define VPL vector<pair<ll,ll>> #define VS vector<string> #define VL vector<long long> #define VC vector<char> #define VF vector<float> #define VD vector<double> #define VB vector<bool> #define PB push_back #define MP make_pair #define F first #define S second #define fa(i,skill) for(auto i : skill) #define fr(i,skill) for(VI :: reverse_iterator i = skill.rbegin(); i != skill.rend(); i++) #define PI pair<int,int> #define mi map<int,int> #define ml map<ll,ll> #define mci map<char,int> #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define MAX 1000000007 #define INF 10000000000000007 #define debug(x) cout<<"This side ----> "<<#x<<" -> "<<x<<endl #define printvec(x) fa(a,x)cout << a << " ";cout << "\n"; using namespace std; ///////////////////////Engine Check.......//////////////////////////// void edit(VI &skill,int n) { f(i,0,n) { int x; cin >> x; skill.PB(x); } } bool cmp(pair<ll,ll>p1,pair<ll,ll>p2) { if(p1.S > p2.S) return true; if(p1.S < p2.S) return false; if(p1.F > p2.F) return true; else return false; } void editl(VL &skill,ll n) { fl(i,0,n) { ll x; cin >> x; skill.PB(x); } } void editpl(VPL &skill,ll n) { fl(i,0,n) { ll x,y; cin >> x >> y; skill.PB({x,y}); } } ll nextPowerOf2(ll n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } const ll N = 0; VL graph[N]; VB vis(N,0); set<ll> st; void dfs(ll node) { if(vis[node]) return; vis[node] = true; st.insert(node); // debug(c); fa(a,graph[node]) { dfs(a); } } bool isGoogles = 0; bool testcase = 0; void achilles() { VL arr;editl(arr,3); sort(rall(arr)); cout << arr[0] + arr[1] << "\n"; } ///////////////////////3....2....1...Ignition//////////////////////////// int main() { ios :: sync_with_stdio(0); cin.tie(0); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // seive(N); ll t=1,w=1; if(testcase) cin >>t; // t = 2e4; while(t--) { achilles(); w++; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) void solve(long long X, long long Y, long long A, long long B) { ll exp_a = 0; while (true) { if (X > Y / A) break; if (X * A >= X + B) break; if (X * A >= Y) break; X *= A; exp_a++; } ll exp_b = (Y - X - 1) / B; cout << exp_a + exp_b << endl; } int main() { long long X; scanf("%lld", &X); long long Y; scanf("%lld", &Y); long long A; scanf("%lld", &A); long long B; scanf("%lld", &B); solve(X, Y, A, B); return 0; }
#include <bits/stdc++.h> #define int long long int #define ll long long int #define ld long double #define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr) #define rep(i, init, n) for (int i = init; i < (int)n; i++) #define rev(i, n, init) for (int i = (int)n; i >= init; i--) #define MOD1 1e9 + 7 #define MOD2 998244353 #define f first #define s second #define endl '\n' #define pii pair<int, int> #define tii tuple<int, int> #define all(v) v.begin(), v.end() #define mt make_tuple #define precise(i) cout << fixed << setprecision(i) #define codejam cout << "Case #" << ii + 1 << ": "; #define impossible cout << "IMPOSSIBLE" << endl; #define error(s) throw runtime_error(s) #define prev prev1 std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); int myRand(int R) { int val = rng() % R; assert(val >= 0); return val; } const long double PI = atan(1.0) * 4; const int32_t INF32 = 2e9 + 7; const int64_t INF64 = 3e18; const int32_t LOG = 21; const int32_t MOD = MOD1; using namespace std; //-------------------DEBUGGING------------------------- void my_debugger(string s, int LINE_NUM) { cerr << endl; } template <typename start, typename... end> void my_debugger(string s, int LINE_NUM, start x, end... y) { if (s.back() != ',') { s += ','; cerr << "LINE(" << LINE_NUM << "): "; } int i = s.find(','); cerr << s.substr(0, i) << " = " << x; s = s.substr(i + 1); if (!s.empty()) cerr << ", "; my_debugger(s, LINE_NUM, y...); } #ifdef AJIT #define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__); #else #define debug(...) ; #endif //----------------------------------------------------- int32_t main() { getFaster; //files_init(); int tests = 1; //cin >> tests; rep(ii, 0, tests) { int n,k; cin>>n>>k; string s; cin>>s; vector<int> pow2(k+1,1); rep(i,1,k+1)pow2[i]=(pow2[i-1]*2)%n; vector<vector<int>> dp(n+1,vector<int>(k+1,0)); map<char,int> m; m['R']=0,m['P']=1,m['S']=2; vector<char> convert={'R','P','S'}; rep(round,0,k+1) { rep(i,0,n) { if(round==0) { dp[i][round]=m[s[i]]; continue; } int val1=dp[i][round-1]; int val2=dp[(i+pow2[round-1])%n][round-1]; if(val1>val2)swap(val1,val2); if(val1==val2 || val1+1==val2) dp[i][round]=val2; } } cout<<convert[dp[0][k]]<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define vec vector #define rep(i,n) for (int i=0; i<(n); i++) #define print(a) cout << a << endl #define fix(n) fixed << setprecision(n) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define Yes "Yes" #define YES "YES" #define No "No" #define NO "NO" int main() { int N; ll X; cin >> N >> X; X *= 100; rep(i,N) { ll V,P; cin >> V >> P; X -= V*P; if (X<0) { print(i+1); return 0; } } print(-1); return 0; }
/*author - Ashutosh Chitranshi*/ #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; #pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define watch(x) cout << (#x) << " is " << (x) << "\n" #define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n" #define watch3(x,y,z) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<"\n" #define pow2(x) ((x)*(x)) #define ll long long #define ld long double #define eb emplace_back #define pb push_back #define pf push_front #define mod 1000000007 //#define ordered_set tree<pair<ll,ll>,null_type,less<pair<ll,ll>>,rb_tree_tag,tree_order_statistics_node_update> #define clock (clock() * 1000.0 / CLOCKS_PER_SEC) #define mp make_pair #define ff first #define ss second #define null NULL #define all(c) (c).begin(),(c).end() #define nl "\n" typedef vector<int> vi; typedef vector<ll> vl; typedef vector< vi > vvi; typedef vector< vl > vvl; typedef pair< int,int > ii; typedef pair< ll,ll> pll; typedef map< ll,ll> mll; typedef map< int,int> mii; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll a,b,c,d; cin >> a >> b>>c>>d; cout << min({a,b,c,d}) << nl; return 0; }
/*AUTHOR - LAKSHYA JAIN*/ #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define vi vector<int> #define vl vector<long long> #define vll vector<pair<long long,long long>> #define vii vector<pair<int,int>> #define vvi vector<vector<int>> #define maxq(x) priority_queue<x> #define minq(x) priority_queue<x,vector<x>,greater<x>> #define graph map<int,vi> #define vvl vector<vector<long long>> #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define all(x) (x).begin(),(x).end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define sz(x) (int)((x).size()) #define f first #define s second #define pii pair<int,int> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i = a;i>=b;i--) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define ppc __builtin_popcount #define ppcll __builtin_popcountll # define quick ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); vvl dp; string solve(int a,int b,ll k) { if(a == 0) { string temp(b,'b'); return temp; } if(b == 0) { string temp(a,'a'); return temp; } if(dp[a - 1][b]>=k) return 'a' + solve(a - 1,b,k); return 'b' + solve(a,b - 1,k - dp[a - 1][b]); } int main() { quick int T = 1; //cin>>T; #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif while(T--) { int a,b; ll k; cin>>a>>b>>k; dp = vvl(a + 1,vl(b + 1,0)); dp[0][0] = 1; rep(i,0,a + 1) { rep(j,0,b + 1) { if(i>0) dp[i][j] += dp[i - 1][j]; if(j>0) dp[i][j] += dp[i][j - 1]; } } cout<<solve(a,b,k); } return 0; }
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <string> #include <cmath> #include <cstdio> #include <iomanip> #include <fstream> #include <cassert> #include <cstring> #include <unordered_set> #include <unordered_map> #include <numeric> #include <ctime> #include <bitset> #include <complex> #include <chrono> #include <random> #include <functional> using namespace std; #define int long long const int N = 70; const int INF = 1e18 + 239; int C[N][N]; void init() { for (int i = 0; i < N; i++) { C[i][0] = 1; C[i][i] = 1; for (int j = 1; j < i; j++) { C[i][j] = min(INF, C[i - 1][j] + C[i - 1][j - 1]); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); init(); int a, b, k; cin >> a >> b >> k; k--; while (a > 0 || b > 0) { if (a == 0) { cout << 'b'; b--; continue; } if (b == 0) { cout << 'a'; a--; continue; } if (C[a + b - 1][a - 1] <= k) { k -= C[a + b - 1][a - 1]; cout << 'b'; b--; } else { cout << 'a'; a--; } } cout << '\n'; }
/*{{{*/ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<iostream> #include<sstream> #include<set> #include<map> #include<queue> #include<bitset> #include<vector> #include<limits.h> #include<assert.h> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++) #define MP make_pair #define PB emplace_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL,LL> PLL; typedef vector<PLL> VPLL; template<class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);} template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } #ifdef HOME #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);} #else #define DEBUG(...) #endif int MOD = 1e9+7; void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;} /*}}}*/ const int SIZE = 1<<20; #define UnionFindSIZE 1234567 map<int,int>AA[UnionFindSIZE]; int map_id[UnionFindSIZE]; struct Union_Find{ int d[UnionFindSIZE],num[UnionFindSIZE]; void init(int n){ REP(i,n)d[i]=i,num[i]=1,map_id[i]=i; } int find(int x){ int y = x, z = x; while(y != d[y]) y = d[y]; while(x != y){ x = d[x]; d[z] = y; z = x; } return y; } bool is_root(int x){return d[x]==x;} bool uu(int x,int y){ x=find(x); y=find(y); if(x==y)return 0; if(num[x]>num[y])swap(x,y); if(SZ(AA[map_id[x]])>SZ(AA[map_id[y]])){ swap(map_id[x],map_id[y]); } for(auto v:AA[map_id[x]]){ AA[map_id[y]][v.F]+=v.S; } num[y]+=num[x]; d[x]=y; return 1; } }U; int N,Q; int C[SIZE]; void solve() { REP(i,Q){ int ty; R(ty); if(ty==1){ int a,b; R(a,b); a--;b--; U.uu(a,b); }else{ int x,y; R(x,y); x--; W(AA[map_id[U.find(x)]][y]); } } } void input() { R(N,Q); U.init(N); REP(i,N){ R(C[i]); AA[i][C[i]]=1; } } int main(){ input(); solve(); return 0; }
#include <iostream> #include <cstring> #include <cstdio> #include <map> #define N 200008 using namespace std; int a[N],f[N]; map<int,int>mp[N]; int find(int); int main() { map<int,int>::iterator k; int i,n,q,typ,x,y; scanf("%d %d",&n,&q); for(i=1;i<=n;++i) { scanf("%d",&a[i]); f[i]=i;mp[i][a[i]]=1; } while(q--) { scanf("%d %d %d",&typ,&x,&y); if(typ==1) { x=find(x);y=find(y); if(x==y)continue; if(mp[x].size()<mp[y].size())swap(x,y); for(k=mp[y].begin();k!=mp[y].end();++k) mp[x][k->first]+=k->second; f[y]=x; } else {x=find(x);printf("%d\n",mp[x][y]);} } return 0; } int find(int u) { int i=u,j; while(i!=f[i])i=f[i]; while(u!=f[u]) { j=u; u=f[u]; f[j]=i; } return i; }
#define LOCAL #define _USE_MATH_DEFINES #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <vector> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <complex> #include <cmath> #include <numeric> #include <bitset> #include <functional> #include <random> #include <ctime> using namespace std; template <typename A, typename B> ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } typedef long long int64; typedef pair<int, int> ii; #define SZ(x) (int)((x).size()) template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2; const int MOD = 1e9 + 7; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; int main() { string s; cin >> s; int n = SZ(s); vector<vector<int>> cnt(n + 1, vector<int>(26)); for (int i = 0; i < n; ++i) { cnt[i + 1] = cnt[i]; int k = s[i] - 'a'; cnt[i + 1][k]++; } int64 ret = 0; int pre = -1, pos = n; for (int i = n - 2; i >= 0; --i) { if (s[i] == s[i + 1] && s[i] != s[i + 2]) { int j = i + 2; int k = s[i] - 'a'; ret += (pos - j) - (cnt[pos][k] - cnt[j][k]); if (k != pre) { ret += n - pos; } pre = k; pos = i; } } cout << ret << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long n; unordered_set<long long> st; int main() { long long ans = 0; cin >> n; for (int i = 2; ; ++i) { if ((long long)round(pow(i, 2)) > n) break; for (int j = 2; ; ++j) { if ((long long)round(pow(i, j)) > n) break; st.insert((long long)round(pow(i, j))); } } cout << n - st.size() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ff first #define ss second #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define d(a,b,c) cout<<a<<" "<<b<<" "<<c<<'\n' #define IO ios_base::sync_with_stdio(0);cin.tie(0); int main() { IO; string s; cin >> s; char lstc = ' '; int lst = 0; ll ans = 0; int cnt[26]; memset(cnt, 0, sizeof(cnt)); for(int i = s.size()-1, cntin = 0; i >= 0; i--) { cnt[s[i]-'a']++; cntin++; if(i < s.size()-1 and s[i] == s[i+1]) { for(int j = 0; j < 26; j++) { if(int(s[i]-'a') != j) { ans += cnt[j]; cnt[j] = 0; } else cnt[j] = cntin; } } } cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) const int INF = 1001001001; int N; vector<int> A; vector<pair<int,int>> X; vector<int> Side; vector<int> Stacks[2]; string ans; int main(){ cin >> N; N*=2; vector<int> A(N); rep(i,N) cin >> A[i]; rep(i,N) X.push_back({A[i],i}); sort(X.begin(),X.end()); Side.resize(N); rep(i,N) Side[X[i].second] = i / (N/2); ans = string(N,'.'); rep(i,N){ if(Stacks[1-Side[i]].empty()){ Stacks[Side[i]].push_back(i); } else{ int opposite = Stacks[1-Side[i]].back(); Stacks[1-Side[i]].pop_back(); ans[opposite] = '('; ans[i] = ')'; } } cout << ans << "\n"; return 0; }
#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 = n-1; i >= 0; i--) #define all(x) (x).begin(),(x).end() // 昇順ソート #define rall(v) (v).rbegin(), (v).rend() // 降順ソート #define FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define sz(x) ((int)(x).size()) typedef long long ll; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<vector<int>>; using VL = vector<ll>; using VVL = vector<vector<ll>>; using VP = vector<P>; template<typename T> void view(T e){std::cout << e << std::endl;} template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; } const int inf = 1 << 30; const ll INF = 1LL << 60; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main(){ int h, w, n, m; cin >> h >> w >> n >> m; VVI mp(h, VI(w)); VP L(n); rep(i,n){ int a, b; cin >> a >> b; a--; b--; L[i].first = a, L[i].second = b; } rep(i, m){ int c, d; cin >> c >> d; c--; d--; mp[c][d] = -1; } vector<vector<vector<bool>>> visited(h, vector<vector<bool>>(w, vector<bool>(4))); auto check = [&](int x, int y){ if (x < 0 || x >= w || y < 0 || y >= h) return false; else return true; }; rep(li, n){ int row = L[li].first, col = L[li].second; rep(i, 4){ if (visited[row][col][i]) continue; visited[row][col][i] = true; int sy = row + dy[i], sx = col + dx[i]; while(check(sx, sy)){ if (mp[sy][sx] == -1 || visited[sy][sx][i]) break; visited[sy][sx][i] = true; sy += dy[i], sx += dx[i]; } } } int ans = 0; rep(row, h) rep(col, w){ bool ok = false; rep(i, 4) if (visited[row][col][i]) ok = true; if (ok) ans++; } view(ans); return 0; }
//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; //#define int long long 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 eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #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() #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} template<class t> using vc=vector<t>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.fi<<","<<p.sc<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } 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 mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); template<class T> void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } return res; } template<class T> using mat = vc<vc<T>>; mat<ll> make_e(int n){ mat<ll>ret(n, vc<ll>(n)); rep(i, n) ret[i][i] = 1; return ret; } template<class T> mat<T> operator+(mat<T> a, mat<T> b){ assert(a.size() == b.size()); if(a.size()) assert(a[0].size() == b[0].size()); rep(i, a.size()) rep(j, a[0].size()){ a[i][j] += b[i][j]; if(a[i][j] >= mod) a[i][j] -= mod; } return a; } template<class T> mat<T> operator-(mat<T> a, mat<T> b){ assert(a.size() == b.size()); if(a.size()) assert(a[0].size() == b[0].size()); rep(i, a.size()) rep(j, a[0].size()){ a[i][j] += mod - b[i][j]; if(a[i][j] >= mod) a[i][j] -= mod; } return a; } template<class T> mat<T> operator*(mat<T> a, mat<T> b){ if(a.empty() || b.empty()) return mat<T>(); int n = a.size(); int m = b[0].size(); int len = a[0].size(); assert(len == b.size()); mat<T>ret(n); rep(i, n) ret[i] = vc<T>(m); rep(i, n) rep(j, len) rep(k, m){ ll add = (ll)(a[i][j]) * b[j][k]; add %= mod; ret[i][k] += add; if(ret[i][k] >= mod) ret[i][k] -= mod; } return ret; } void solve(){ int n, m, k; cin >> n >> m >> k; mat<ll>M = make_e(n); vc<ll>num(n); rep(i, n) cin>>num[i]; ll rv = modpow(2*m, mod-2); rep(i, m){ int a, b; cin >> a >> b; a--; b--; M[a][a] -= rv; M[b][b] -= rv; M[a][b] += rv; M[b][a] += rv; } rep(i, n) rep(j, n) M[i][j] = (M[i][j]%mod+mod)%mod; mat<ll>ret(n, vc<ll>(n)); rep(i, n) ret[i][i] = 1; while(k){ if(k&1) ret = ret*M; k >>= 1; M = M*M; } mat<ll>ans(n, vc<ll>(1)); rep(i, n) ans[i][0] = num[i]; auto result = ret*ans; rep(i, n) o(result[i][0]); } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); int t; t = 1; //cin >> t; while(t--) solve(); }
#include <cstdio> #include <cstdlib> using namespace std; #define ll long long const ll MAXN = 400011; struct fhqTreap { ll tot, rt; ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN]; void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];} void split(ll u, ll k, ll &x, ll &y) { if(!u) {x = y = 0; return;} if(val[u] <= k) x = u, split(rs[u], k, rs[u], y); else y = u, split(ls[u], k, x, ls[u]); pushup(u); } ll merge(ll u, ll v) { if(!u || !v) return u | v; if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;} else {ls[v] = merge(u, ls[v]); pushup(v); return v;} } ll add(ll k) { rd[++tot] = rand(); sz[tot] = 1; val[tot] = sum[tot] = k; return tot; } void insert(ll k) { ll x, y; split(rt, k, x, y); rt = merge(merge(x, add(k)), y); } ll cb(ll k) { ll x, y; split(rt, k, x, y); ll ans = sz[x] * k + sum[y]; rt = merge(x, y); return ans; } void delet(ll k) { ll x, y, z; split(rt, k, x, z); split(x, k - 1, x, y); y = merge(ls[y], rs[y]); rt = merge(merge(x, y), z); } } t[2]; ll q; ll len[2]; ll w[2][MAXN]; ll read() { ll X = 0, F = 1; char C = getchar(); while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();} while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();} return X * F; } int main() { ll ans = 0; len[1] = read(); len[0] = read(); q = read(); for(ll i = 0; i < 2; ++i) for(ll j = 1; j <= len[i]; ++j) t[i].rt = t[i].merge(t[i].rt, t[i].add(0)); for(ll i = 1, opt, x, y; i <= q; ++i) { opt = (read() & 1); x = read(); y = read(); t[opt].delet(w[opt][x]); t[opt].insert(y); ans -= t[opt^1].cb(w[opt][x]); w[opt][x] = y; ans += t[opt^1].cb(w[opt][x]); printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; struct BIT { vector<int64_t> t; BIT (int n) : t(n, 0) {} void update(int i, int64_t x) { for (; i < int(t.size()); i |= i + 1) { t[i] += x; } } int64_t query(int i) { int64_t res = 0; for (; i > 0; i &= i - 1) { res += t[i - 1]; } return res; } }; int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int n[2], q; cin >> n[0] >> n[1] >> q; vector<array<int, 3>> queries(q); vector<int> nums(1, 0); nums.reserve(q + 1); for (auto& it : queries) { cin >> it[0] >> it[1] >> it[2]; it[0]--; it[1]--; nums.push_back(it[2]); } sort(nums.begin(), nums.end()); nums.erase(unique(nums.begin(), nums.end()), nums.end()); int sz = nums.size(); int64_t ans = 0; vector<int> v[2]; v[0].resize(n[0]); v[1].resize(n[1]); vector<BIT> cnt(2, BIT(sz)), sum(2, BIT(sz)); cnt[0].update(0, n[0]); cnt[1].update(0, n[1]); for (auto qq : queries) { int a = qq[0], b = qq[0] ^ 1; int before = v[a][qq[1]]; int id = lower_bound(nums.begin(), nums.end(), before) - nums.begin(); ans -= int64_t(before) * cnt[b].query(id); ans += sum[b].query(id); int after = qq[2]; int nid = lower_bound(nums.begin(), nums.end(), after) - nums.begin(); ans -= sum[b].query(nid); ans += int64_t(after) * cnt[b].query(nid); cnt[a].update(id, -1); sum[a].update(id, -before); cnt[a].update(nid, 1); sum[a].update(nid, after); v[a][qq[1]] = after; cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() constexpr ll MOD=998244353; constexpr ll INF=2e18; int main(){ int n; cin >> n; vector<P> p(n); REP(i,n) cin >> p[i].second >> p[i].first; sort(ALL(p)); int now=-1; vector<P> q; REP(i,n){ if(i==0||p[i-1].first!=p[i].first){ now++; q.push_back({p[i].second,p[i].second}); } else{ q[now].first=min(q[now].first,p[i].second); q[now].second=max(q[now].second,p[i].second); } } int m=q.size(); VVI dp(m+1,VI(2,INF)); REP(i,m){ if(i==0){ dp[i+1][0]=abs(0-q[i].second)+abs(q[i].first-q[i].second); dp[i+1][1]=abs(0-q[i].first)+abs(q[i].first-q[i].second); } else{ dp[i+1][0]=min(dp[i][0]+abs(q[i-1].first-q[i].second)+abs(q[i].first-q[i].second),dp[i][1]+abs(q[i-1].second-q[i].second)+abs(q[i].first-q[i].second)); dp[i+1][1]=min(dp[i][0]+abs(q[i-1].first-q[i].first)+abs(q[i].first-q[i].second),dp[i][1]+abs(q[i-1].second-q[i].first)+abs(q[i].first-q[i].second)); } } cout << min(dp[m][0]+abs(q[m-1].first),dp[m][1]+abs(q[m-1].second)) << endl; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #pragma comment(linker, "/stack:200000000") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //turn on extra precision //#pragma GCC target("fpmath=387") using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef string str; typedef pair <int,int> pii; typedef pair <ll,ll> pll; typedef vector <int> vi; typedef vector <ll> vll; typedef vector <pii> vpii; typedef vector <pll> vpll; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> #define mp make_pair #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define fi first #define se second #define fs first.second #define ss second.second #define ff first.first #define sf second.first #define newl '\n' #define fbo find_by_order #define ook order_of_key #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(),x.rend() #define watch(x) cout << (#x) << " is : " << (x) << newl mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); vi dirx = {0,0,1,-1}; vi diry = {1,-1,0,0}; char to_upper (char x){ if( 97 <= int(x) && int(x) <= 122) return char(x-32); if( 65 <= int(x) && int(x) <= 90) return x; return -1; } char to_lower (char x){ if( 97 <= int(x) && int(x) <= 122) return x; if( 65 <= int(x) && int(x) <= 90) return char(x+32); return -1; } int numerize (char x){ if(48 <= int(x) && int(x) <= 57) return int(x-'0'); if(97 <= int(x) && int(x) <= 122) return int(x-96); if(65 <= int(x) && int(x) <= 90) return int(x-64); return -1; } bool isect (int l1, int r1, int l2, int r2){ return max(l1,l2) <= min(r1,r2); } ll quickpow (ll num1, ll num2, ll MOD){ if(num2==0)return 1%MOD; else if(num2==1)return num1%MOD; else{ ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD; if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res; } } ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);} ll gcd (ll num1, ll num2){ if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ; while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;} return num2; } ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;} // end of Template int n, c[200010], x[200010]; ll dp[200010][2]; pii p[200010]; void compress(){ set <int> s; for(int i = 1; i <= n; ++i) s.insert(c[i]); map <int,int> trans; int idx = 0; for(auto i : s) trans[i] = ++idx; for(int i = 1; i <= n; ++i) c[i] = trans[c[i]]; for(int i = 1; i <= n; ++i){ if(p[c[i]] == mp(0,0)){ p[c[i]] = mp(x[i],x[i]); } else {p[c[i]].se = max(p[c[i]].se, x[i]); p[c[i]].fi = min(p[c[i]].fi, x[i]); } } n = idx; return; } ll cost(ll a, ll b, ll c){ return abs(a-b) + abs(b-c); } int main(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i = 1; i <= n; ++i) cin >> x[i] >> c[i]; compress(); dp[0][0] = 0, dp[0][1] = 0; for(int i = 1; i <= n; ++i){ dp[i][0] = min(dp[i-1][0] + cost(p[i-1].fi,p[i].se,p[i].fi) , dp[i-1][1] + cost(p[i-1].se,p[i].se,p[i].fi)); dp[i][1] = min(dp[i-1][0] + cost(p[i-1].fi,p[i].fi,p[i].se) , dp[i-1][1] + cost(p[i-1].se,p[i].fi,p[i].se)); } ll ans = min(dp[n][0] + abs(p[n].fi) , dp[n][1] + abs(p[n].se)); cout << ans << newl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long i64; typedef unsigned long long ui64; typedef vector<i64> vi; typedef vector<vi> vvi; typedef pair<i64, i64> pi; #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) inline void RI(i64 &i) {scanf("%lld", &(i));} inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } } inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } } inline void WI(const i64 &i) {printf("%lld\n", i);} inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); } inline void WS(const string &s) { printf("%s\n", s.c_str()); } inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} } inline void YESNO(bool b) { WB(b, "YES", "NO"); } inline void YesNo(bool b) { WB(b, "Yes", "No"); } #define BUF_LENGTH 1000000 inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;} template<typename T> inline bool IN(T &S, const typename T::key_type &key) { return S.find(key) != S.end(); } template<typename T> inline bool ON(const T &b, i64 idx) { return ((T(1) << idx) & b) != 0; } i64 dfs(const vvi &edges, i64 cur, i64 prev, i64 n, const vi &to, vi &ans, bool f) { // start from v //cerr << "dfs " << cur << " " << prev << " " << n << endl; ans[cur] = n; for(auto &e: edges[cur]) { if(e != prev && e != to[cur]) { n += 1; n = dfs(edges, e, cur, n, to, ans, false); n += 1; } } if(f && to[cur] != -1) { n += 1; n = dfs(edges, to[cur], cur, n, to, ans, true); } return n; } int main(int argc, char *argv[]) { i64 i, j, k; i64 t, T; i64 N; cin >> N; vvi edges(N); REP(0, N-1, i) { i64 a, b; RI(a); RI(b); --a; --b; edges[a].pb(b); edges[b].pb(a); } const i64 INF = N * 2; auto bfs = [&](i64 s) { vi dists(N, INF); vi from(N, -1); dists[s] = 0; queue<i64> q; q.push(s); while(!q.empty()) { i64 cur = q.front(); q.pop(); for(auto &e: edges[cur]) { if(dists[e] == INF) { dists[e] = dists[cur] + 1; from[e] = cur; q.push(e); } } } /* for(auto &d: dists) { cerr << d << " "; } cerr << endl; for(auto &d: from) { cerr << d << " "; } cerr << endl; */ return pair<vi, vi> {dists, from}; }; auto res_0 = bfs(0); vi &d0 = res_0.first; i64 u = -1; REP(0, N, i) { if(u == -1 || d0[u] < d0[i]) { u = i; } } assert(u != -1); auto res_u = bfs(u); vi &du = res_u.first; vi &fu = res_u.second; i64 v = -1; REP(0, N, i) { if(v == -1 || du[v] < du[i]) { v = i; } } assert(v != -1); //cerr << "diameter " << du[v] << " " << u << " -> " << v << endl; vi ans(N, -1); dfs(edges, v, -1, 1, fu, ans, true); WVI(ans); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; class ETraveler { public: void solve(std::istream& cin, std::ostream& cout) { int N; cin >> N; vector<pair<int, int>> v(N); REP(i, N) { int x, c; cin >> x >> c; v[i] = {c, x}; } sort(v.begin(), v.end()); vector<pair<int, int>> LR; int prev = v[0].first; int l = v[0].second; int r = v[0].second; REP(i, v.size()) { if (v[i].first != prev) { LR.emplace_back(l, r); l = r = v[i].second; } else { l = min(l, v[i].second); r = max(r, v[i].second); } prev = v[i].first; } LR.emplace_back(l, r); LR.emplace_back(0, 0); vector<vector<int>> dp(LR.size() + 1, vector<int>(2, INF)); dp[0][0] = dp[0][1] = 0; REP(i, LR.size()) { int L = LR[i].first; int R = LR[i].second; int PL = i > 0 ? LR[i - 1].first : 0; int PR = i > 0 ? LR[i - 1].second : 0; dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + abs(PL - R) + R - L); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + abs(PR - R) + R - L); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + abs(PL - L) + R - L); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + abs(PR - L) + R - L); } cout << min(dp[LR.size()][0], dp[LR.size()][1]) << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream& in(std::cin); std::ostream& out(std::cout); ETraveler solver; solver.solve(in, out); return 0; }
#include <bits/stdc++.h> #define union Union #define rank rank228 #define tm tm228 #define hash hash228 #define matr vector < vector < int > > #define int long long #define pii pair < int, int > #define papa pair < node*, node* > #define all(v) v.begin(), v.end() #define endl "\n" #define pb push_back #define PI 3.14159265358979323846 #define ff first #define ss second #define mp make_pair using namespace std; const int inf = 1e18, maxn = 1e6 + 100, mod1 = 1e9 + 7, mod2 = 998244353; int mood1(int x) { x %= mod1; if (x < 0) x += mod1; return x; } int mood2(int x) { x %= mod2; if (x < 0) x += mod2; return x; } int a[maxn]; void solve(){ int ans = 0, now = 0, pref = 0, mpref = 0; int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; now += mpref; ans = max(ans, now); pref += a[i]; now += pref - mpref; mpref = max(pref, mpref); ans = max(ans, now); } cout << ans; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); srand(100); cout << fixed << setprecision(10); int t = 1; #ifdef LOCAL freopen("task.in", "r", stdin); #endif //cin >> t; while (t--) { solve(); cout << endl; } }
#include<bits/stdc++.h> using namespace std; #define asll unsigned long long #define ll long long #define ld long double #define imt int #define pii pair<int,int> #define st first #define nd second #define psbk(x) push_back(x); #define For(x,a,b) for(int x=(a);x!=(b+((b-a)/abs(b-a)));x+=(b-a)/abs(b-a)) #define FOR(x,n) for(int x=0;x<(n);x++) #define m_p make_pair #define kj <<" "<< #define kjb <<" "; #define dg <<","<< #define ml <<endl; #define co cout<< #define ter cout<<endl; #define sibi cout<<"TES"<<endl; #define tes(x) cout<<"TES"<<x<<endl; #define kpr(x) cout<<"("<<x.st dg x.nd <<")" ml #define kkor(p) cout<<"("<<p.x dg p.y <<")" ml #define kera(x,m) for( int i = 0;i < m ; i++ ){ cout<<"x["<< i <<"] =" kj x[i] ml} asll M=1e9+7; int n,m,k,tc; ll p,q,r; ll pms[200005]; ll ps[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(0); //freopen("stdin.in", "r", stdin) //freopen("stdout.out", "w", stdout) srand(time(0)); cin>>n; pms[0]=0; ps[0]=0; FOR(i,200005){ ps[i]=0; pms[i]=0; } For(i,1,n){ cin>>p; ps[i]=ps[i-1]+p; pms[i]=max(pms[i-1],ps[i]); //co ps[i] kj pms[i] ml } ll js1=0,js2=0,js3=0; //js1=psum //js2=psum sebelum + max ini //js3 maks akhir For(i,1,n){ js2=js1+pms[i]; js3=max(js3,js2); js1+=ps[i]; } co js3 ml; }
// Code for A - twiblr #include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(ll i=0;i<(ll)(n);i++) #define FOR(i,x,y) for(ll i=x;i<(ll)(y);i++) #define MOD 1000000007 #define mod(x) ( (x) % MOD + MOD ) % MOD #define debug(var) do{std::cout << #var << " : ";view(var);}while(0) template<typename T> void view(T e){std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } } template<typename T, typename U> void view(const std::map<T, U>& m){ for(const auto& e : m){ std::cout << e.first << "=>" << e.second << " "; } std::cout << std::endl; } int main() { int A, B; cin >> A >> B; cout << 2 * A + 100 - B << endl; return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; #define DEBUG #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef pair<int,int> pii; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = (1<<30) - 1; const ll linf = 1LL<<61; const int MAX = 510000; int dy[8] = {0,1,0,-1,1,-1,-1,1}; int dx[8] = {-1,0,1,0,1,-1,1,-1}; const double pi = acos(-1); const double eps = 1e-8; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<=b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ int sz = a.size(); for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr; sz--; if(sz) cout << " "; } cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){ cout << a << " " << b << "\n"; } template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} const int mod = 1e9 + 7; //const int mod = 998244353; int main(){ int n; cin >> n; string s; cin >> s; vl a(n+1); rep(i,n+1) cin >> a[i]; int ans = inf; rep(i,n) chmin(ans, abs(a[i]-a[i+1])); cout << ans << endl; vvl b(ans,vl(n+1)); rep(i,n+1){ ll q = a[i] / ans; ll r = a[i] % ans; rep(j,r) b[j][i] = q + 1; for(int j=r; j<ans; j++) b[j][i] = q; } rep(i,ans){ rep(j,n+1) cout << b[i][j] << " "; cout << endl; } }
#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<LL,int>; using vv = vector<vector<int>>; const int INF = (int)1e9; const LL LINF = (LL)1e18; struct edge{ int to; LL c, d; edge(int to, LL c, LL d): to(to), c(c), d(d) {} }; vector<vector<edge>> G; vector<long long> dijkstra(int N, int st){ vector<long long> dist(N); for(int i = 0; i < N; i++){ dist[i] = LINF; } dist[st] = 0; priority_queue<P,vector<P>,greater<P>> q; q.push({dist[st], st}); while(!q.empty()){ P p = q.top(); q.pop(); long long t = p.first; int n = p.second; if(dist[n] < t) continue; for(auto e : G[n]){ int v = e.to; LL c = e.c, d = e.d; LL ave = sqrt(d); ave = max(ave, t + 1); LL cost = min(c + ave - 1 + d / ave, c + ave + d / (ave + 1)); if(dist[v] <= cost) continue; dist[v] = cost; q.push({dist[v], v}); } } return dist; } int main(){ int N, M; cin >> N >> M; G.resize(N); rep(i,M){ int a, b; LL c, d; cin >> a >> b >> c >> d; a--; b--; G[a].emplace_back(b, c, d); G[b].emplace_back(a, c, d); } vector<LL> dist = dijkstra(N, 0); LL ans = dist[N-1]; if(ans == LINF) ans = -1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long i64; typedef unsigned long long ui64; typedef vector<i64> vi; typedef vector<vi> vvi; typedef pair<i64, i64> pi; #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) inline void RI(i64 &i) {scanf("%lld", &(i));} inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } } inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } } inline void WI(const i64 &i) {printf("%lld\n", i);} inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); } inline void WS(const string &s) { printf("%s\n", s.c_str()); } inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} } inline void YESNO(bool b) { WB(b, "YES", "NO"); } inline void YesNo(bool b) { WB(b, "Yes", "No"); } #define BUF_LENGTH 1000000 inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;} template<typename T> inline bool IN(T &S, const typename T::key_type &key) { return S.find(key) != S.end(); } template<typename T> inline bool ON(const T &b, i64 idx) { return ((T(1) << idx) & b) != 0; } i64 get_dist(i64 t_cur, i64 c, i64 d) { auto compute = [&](i64 t) { return t + c + d / (t + 1); }; i64 Tmin = max(0LL, i64(floor(sqrt((long double)(d))) - 2)); i64 Tmax = Tmin + 5; i64 d_min = compute(Tmin); i64 Topt = Tmin; i64 T; REP(Tmin, Tmax+1, T) { i64 d = compute(T); if(d < d_min) { d = d_min; Topt = T; } } if(t_cur <= Topt) { return compute(Topt) - t_cur; } return compute(t_cur) - t_cur; } int main(int argc, char *argv[]) { i64 i, j, k; i64 N, M; cin >> N >> M; using ppi = pair<i64, pi>; vector<vector<ppi>> edges(N); REP(0, M, i) { i64 a, b, c, d; RI(a); RI(b); RI(c); RI(d); --a; --b; edges[a].pb({b, {c, d}}); edges[b].pb({a, {c, d}}); } vi dist(N, -1); priority_queue<pi, vector<pi>, greater<pi>> pq; dist[0] = 0; pq.push({0, 0}); while(!pq.empty()) { pi c = pq.top(); pq.pop(); i64 cur = c.second, cur_dist = c.first; if(dist[cur] != cur_dist) { continue; } for(auto &e: edges[cur]) { i64 n_dist = cur_dist + get_dist(cur_dist, e.second.first, e.second.second); i64 n = e.first; if(dist[n] == -1 || n_dist < dist[n]) { dist[n] = n_dist; pq.push({n_dist, n}); } } } WI(dist.back()); 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; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int long long #define reset(x) memset(x,0,sizeof(x)) #define db1(x) cout<<#x<<"="<<x<<'\n' #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n' #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n' #define rep(i,n) for(int i=0;i<(n);++i) #define repA(i,a,n) for(int i=a;i<=(n);++i) #define repD(i,a,n) for(int i=a;i>=(n);--i) #define sz(a) (int)a.size() #define pii pair<int,int> #define all(a) a.begin(),a.end() #define PI 3.1415926535897932384626433832795 #define INF 1000000000 #define MOD 1000000007 #define MOD2 1000000009 #define EPS 1e-6 #define pb push_back #define fi first #define se second #define mp make_pair int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen(".inp", "r", stdin); //freopen(".out", "w", stdout); int n; cin >> n; vector<int> arr; map<int, int> cnt; for (int i = 0; i < n; i++) { int a; cin >> a; arr.pb(a); if (cnt.count(a % 200) == 0) { cnt[a % 200] = 0; } cnt[a % 200]++; } int sum = 0; for (auto x : cnt) { int num = x.se; sum += ((num * (num - 1)) / 2); } cout << sum; }
#include <bits/stdc++.h> #define endl "\n" using namespace std; #define ll long long const long long llINF = 1LL << 60; const int iINF = 0x20000000; template<class T> inline bool chmin(T& a, T b) { if (b < a) { 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 execute(); int main() { cin.tie(nullptr); ios::sync_with_stdio(false);// cin と scanfの併用禁止 cout << fixed << setprecision(15); execute(); return 0; } void execute() { ll N; ll A[200000]; cin >> N; vector<int> idxs[200]; for(int i = 0; i < N; i++){ cin >> A[i]; idxs[A[i] % 200].push_back(i); } ll cnt = 0; for(int m = 0; m < 200; m++){ vector<int> &a = idxs[m]; ll n = a.size(); cnt += n * (n-1) / 2; } cout << cnt << endl; }
#include <cmath> #include <iostream> #include <string> using namespace std; long long r1, c1, r2, c2, x, y; int main() { cin >> r1 >> c1 >> r2 >> c2; x = abs(r1 - r2); y = abs(c1 - c2); if (r1 == r2 && c1 == c2) { cout << 0; } else if (y == x || x + y <= 3) { cout << 1; } else if ((x + y) % 2 == 0 || x + y <= 6 || abs(x - y) <= 3) { cout << 2; } else { cout << 3; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i)) #define ALL(x) ::std::begin(x), ::std::end(x) using namespace std; template <typename T> void print_vector(T& vec) { stringstream ss; ss << "{"; for (auto v : vec) ss << v << " "; ss << "}" << endl; cout << ss.str() << endl; } template <typename T> void print_pairs(T& vec) { stringstream ss; ss << "{"; for (auto v : vec) ss << v.first << "," << v.second << " "; ss << "}" << endl; cout << ss.str() << endl; } template <typename T> string string_vector(vector<T>& vec) { stringstream ss; ss << "{"; for (auto v : vec) ss << v << " "; ss << "}\n"; return ss.str(); } template <typename T> string string_vector(vector<vector<T>>& vec) { stringstream ss; ss << "{"; for (auto v : vec) { ss << "{"; for (auto vv : v) ss << vv << " "; ss << "}"; } ss << "}\n"; return ss.str(); } using ll = long long; class Solution { public: ll solve(int r1, int c1, int r2, int c2) { // cout << "r1=" << r1 << " c1="<< c1 << " r2=" << r2 << " c2=" << c2 << endl; if(r1==r2 and c1==c2) return 0; if(abs(r1-r2) == abs(c1-c2)) return 1; // cout << "r1-r2=" << abs(r1-r2) % 2 << " c1-c2="<< abs(c1-c2) % 2 << endl; if( abs(r1-r2) + abs(c1-c2) <=3) return 1; if( (abs(r1-r2) % 2) == (abs(c1-c2) % 2) ) return 2; if( abs( abs(r1-r2) - (abs(c1-c2) ))<=3) return 2; return 3; } }; // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); Solution sol; // failed to analyze input format // TODO: edit here int r1, c1, r2, c2; cin >> r1>> c1>> r2>>c2 ; auto ans = sol.solve(r1, c1, r2, c2); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxN = 6e5+10; long long a[maxN],temp[maxN]; long long N; long long tree1[maxN],tree2[maxN]; long long lowbit(int x){return x&(-x);} void add(long long tree[],long long x,long long k){for(;x<=N*2;x+=lowbit(x))tree[x]+=k;} long long pre(long long tree[],long long x) { long long ans=0; for(;x;x-=lowbit(x))ans+=tree[x]; return ans; } int main() { cin>>N; for(int i=0;i<=N-1;i++)cin>>a[i]; for(int i=N;i<=N+N-1;i++)a[i]=a[i-N]; for(int i=0;i<=N+N-1;i++)temp[i]=a[i]; sort(temp,temp+N+N-1); int siz=unique(temp,temp+N+N-1)-temp; for(int i=0;i<=N+N-1;i++)a[i]=lower_bound(temp,temp+siz,a[i])-temp+1; long long ans=0; for(int i=N-1;i>=0;i--) { ans+=pre(tree1,a[i]-1); add(tree1,a[i],1); } for(int i=0;i<=N-1;i++)add(tree2,a[i],1); int L=0,R=N-1; for(int i=0;i<=N-1;i++) { cout<<ans<<endl; add(tree2,a[L],-1); ans-=pre(tree2,a[L]); L++; R++; add(tree2,a[R],1); ans+=N-pre(tree2,a[R]); } }
//I'll always miss you like a darling. #include <bits/stdc++.h> using namespace std; #define LL long long #define LD long double #define ull unsigned long long #define x first #define y second #define pb push_back #define pf push_front #define mp make_pair #define Pair pair<int,int> #define pLL pair<LL,LL> #define pii pair<double,double> #define LOWBIT(x) x & (-x) #define rep(i,a,b) for (int i=a;i<=b;i++) #define REP(i,a,b) for (int i=a;i>=b;i--) const int INF=2e9; const LL LINF=2e16; const int magic=348; const int MOD=1e9+7; const double eps=1e-10; const double pi=acos(-1); struct fastio { static const int S=1e7; char rbuf[S+48],wbuf[S+48];int rpos,wpos,len; fastio() {rpos=len=wpos=0;} inline char Getchar() { if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin); if (!len) return EOF; return rbuf[rpos++]; } template <class T> inline void Get(T &x) { char ch;bool f;T res; while (!isdigit(ch=Getchar()) && ch!='-') {} if (ch=='-') f=false,res=0; else f=true,res=ch-'0'; while (isdigit(ch=Getchar())) res=res*10+ch-'0'; x=(f?res:-res); } inline void getstring(char *s) { char ch; while ((ch=Getchar())<=32) {} for (;ch>32;ch=Getchar()) *s++=ch; *s='\0'; } inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;} inline void Writechar(char ch) { if (wpos==S) flush(); wbuf[wpos++]=ch; } template <class T> inline void Print(T x,char ch) { char s[20];int pt=0; if (x==0) s[++pt]='0'; else { if (x<0) Writechar('-'),x=-x; while (x) s[++pt]='0'+x%10,x/=10; } while (pt) Writechar(s[pt--]); Writechar(ch); } inline void printstring(char *s) { int pt=1; while (s[pt]!='\0') Writechar(s[pt++]); } }io; template<typename T> inline void check_max(T &x,T cmp) {x=max(x,cmp);} template<typename T> inline void check_min(T &x,T cmp) {x=min(x,cmp);} template<typename T> inline T myabs(T x) {return x>=0?x:-x;} template<typename T> inline T mygcd(T x,T y) {return y==0?x:mygcd(y,x%y);} inline int add(int x) {if (x>=MOD) x-=MOD;return x;} inline int add(int x,int MO) {if (x>=MO) x-=MO;return x;} inline int sub(int x) {if (x<0) x+=MOD;return x;} inline int sub(int x,int MO) {if (x<0) x+=MO;return x;} inline void Add(int &x,int y) {x=add(x+y);} inline void Add(int &x,int y,int MO) {x=add(x+y,MO);} inline void Sub(int &x,int y) {x=sub(x-y);} inline void Sub(int &x,int y,int MO) {x=sub(x-y,MO);} template<typename T> inline int quick_pow(int x,T y) {int res=1;while (y) {if (y&1) res=1ll*res*x%MOD;x=1ll*x*x%MOD;y>>=1;}return res;} template<typename T> inline int quick_pow(int x,T y,int MO) {int res=1;while (y) {if (y&1) res=1ll*res*x%MO;x=1ll*x*x%MO;y>>=1;}return res;} const int MAXN=3e5; int n; int a[MAXN+48]; namespace BIT { int c[MAXN+48]; void clear() {memset(c,0,sizeof(c));} void modify(int x) {while (x<=n) c[x]++,x+=LOWBIT(x);} int query(int x) {int res=0;while (x) res+=c[x],x^=LOWBIT(x);return res;} } int main () { #ifndef ONLINE_JUDGE double TIME=clock(); freopen ("a.in","r",stdin); freopen ("a.out","w",stdout); cerr<<"Running..."<<endl; #endif scanf("%d",&n);rep(i,1,n) scanf("%d",a+i),a[i]++;LL ans=0; BIT::clear();rep(i,1,n) ans+=(i-1)-BIT::query(a[i]),BIT::modify(a[i]); printf("%lld\n",ans); rep(i,1,n-1) { ans-=(a[i]-1); ans+=(n-a[i]); printf("%lld\n",ans); } #ifndef ONLINE_JUDGE cerr<<"Exec Time: "<<(clock()-TIME)/CLOCKS_PER_SEC<<endl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define int32 int32_t #define int long long #define pb push_back #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define pq priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define mod 1000000007 #define w(x) int x; cin>>x; while(x--) #define loop(x, n) for(int x = 0; x < n; x++) bool sorted(vi &p) { int n = p.size(); for(int i = 0; i < n - 1; i++) if(p[i] > p[i + 1]) return false; return true; } int32 main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vi p(n); loop(i, n) cin >> p[i]; vi ans; set<int> s; int swaps = 0; for(int i = n - 2; i >= 0; i--) { int x = i; while(x < (n - 1) && p[x] > p[x + 1] && swaps < n) { swap(p[x], p[x + 1]); if(s.find(x) != s.end()) { swaps = n; break; } else s.insert(x); ans.pb(x + 1); swaps++; x++; } } if(swaps >= n || (n - 1 - swaps)) cout << -1 << endl; else { for(int i: ans) cout << i << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MOD=1000000007; #define INF 1LL<<30 #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() int did[202020]; vector<int> ret; int main() { int n; cin>>n; vector<int> p(n),r(n); rep(i,n){ cin>>p[i]; p[i]--; r[p[i]]=i; } rep(i,n){ int x=r[i]; while(x>i){ if(did[x-1]==1){ cout<<-1<<endl; return 0; } did[x-1]=1; ret.push_back(x); swap(p[x-1],p[x]); r[p[x]]=x; r[p[x-1]]=x-1; x--; } } rep(i,n-1){ if(did[i]==0){ cout<<-1<<endl; return 0; } } for(auto e : ret) cout<<e<<endl; }
#include<iostream> #include<cstdio> #include<queue> #define ll long long using namespace std; struct node{ int num,id; }; int n,x,Q,u,d; int edgenum,NIL=-1,head[200005],nex[400005],vet[400005]; int h[200005],fa[200005],Size[200005],son[200005]; int Num[200005],t[200005]; bool vis[200005]; queue<node> q[200005]; void addedge(int u,int v){ edgenum++; nex[edgenum]=head[u]; vet[edgenum]=v; head[u]=edgenum; } void DFS(int u,int H){ int Max=0; h[u]=H; for(int i=head[u];i!=NIL;i=nex[i]){ int v=vet[i]; if(v==fa[u]) continue; fa[v]=u; Size[v]=1; DFS(v,H+1); Size[u]+=Size[v]; if(Size[v]>Max){ Max=Size[v]; son[u]=v; } } } void clac(int u,int num){ if(num>0) { Num[h[u]]++; } else { Num[h[u]]--; } for(int i=head[u];i!=NIL;i=nex[i]){ int v=vet[i]; if(v==fa[u]||vis[v]) continue; clac(v,num); } } void DSU(int u,int keep){ for(int i=head[u];i!=NIL;i=nex[i]){ int v=vet[i]; if(v==fa[u]||v==son[u]) continue; DSU(v,0); } if(son[u]){ DSU(son[u],1);vis[son[u]]=true; } clac(u,1);vis[son[u]]=false; while(!q[u].empty()){ node X=q[u].front(); q[u].pop(); if(h[u]<=X.num) t[X.id]=Num[X.num]; else t[X.id]=0; } if(keep==0) clac(u,-1); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) head[i]=NIL; for(int i=2;i<=n;i++){ scanf("%d",&x); addedge(x,i); addedge(i,x); } scanf("%d",&Q); for(int i=1;i<=Q;i++){ scanf("%d%d",&u,&d); node X; X.num=d+1; X.id=i; q[u].push(X); } DFS(1,1); DSU(1,0); for(int i=1;i<=Q;i++){ printf("%d\n",t[i]); } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define pb push_back #define COUT(x) cout << (x) << "\n" #define COUTF(x) cout << setprecision(15) << (x) << "\n" #define ENDL cout << "\n" #define DF(x) x.erase(x.begin()) #define ALL(x) x.begin(), x.end() #define SORT(x) sort(ALL(x)) #define RSORT(x) sort(x.rbegin(), x.rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MAXI(x) max_element(ALL(x)) - x.begin() #define SUM(x) accumulate(ALL(x), 0ll) #define COUNT(x, y) count(ALL(x), y); #define ANS cout << ans << "\n" #define YES cout << "YES\n"; #define NO cout << "NO\n"; #define Yes cout << "Yes\n"; #define No cout << "No\n"; #define init() \ cin.tie(0); \ ios::sync_with_stdio(false) #define debug(x) cerr << "[debug] " << #x << ": " << x << endl; #define debugV(v) \ cerr << "[debugV] " << #v << ":"; \ rep(z, v.size()) cerr << " " << v[z]; \ cerr << endl; using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vector<ll>>; using mll = map<ll, ll>; using qll = queue<ll>; using P = pair<ll, ll>; using vp = vector<P>; using vs = vector<string>; template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { rep(j, v.size()) i >> v[j]; return i; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; ll get_digit(ll x) { return to_string(x).size(); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } template <class T> void chmin(T& a, const T& b) { if (b < a) a = b; } vvll to; vp range; vll dep; vll points; ll dfs(ll i, ll s, ll d) { ll count = 1; points.pb(i); for (ll t : to[i]) count += dfs(t, s + count, d + 1); dep[i] = d; range[i] = {s, s + count - 1}; return count; } signed main() { init(); ll N; cin >> N; vll A(N - 1); cin >> A; rep(i, N - 1) A[i]--; to.resize(N); range.resize(N); dep.resize(N); rep(i, N - 1) to[A[i]].pb(i + 1); dfs(0, 0, 0); vector<unordered_map<ll, ll>> x(N); unordered_map<ll, ll> m; ll M = 1130; rep(i, N) { ll p = points[i]; m[dep[p]]++; if (i % M == 0) x[i] = m; } ll Q; cin >> Q; rep(i, Q) { ll u, d; cin >> u >> d; u--; ll l = range[u].first; ll r = range[u].second; ll ans = x[r - r % M][d]; rep(k, r % M) if (dep[points[r - k]] == d) ans++; if (l != 0) { l--; rep(k, l % M) if (dep[points[l - k]] == d) ans--; ans -= x[l - l % M][d]; } COUT(ans); } return 0; }
#include<iostream> #include<set> #include<vector> using namespace std; typedef long long li; #define rep(i,n) for(int i=0;i<(n);i++) #define df 0 template<class T> void print(const T& t){ cout << t << "\n"; } template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); } int find(int a,vector<int>& uf){ if(uf[a]<0) return a; return uf[a]=find(uf[a],uf); } void unite(int a,int b,vector<int>& uf){ a=find(a,uf),b=find(b,uf); if(a==b)return ; if(uf[a]>uf[b]) swap(a,b); uf[a]+=uf[b]; uf[b]=a; } int main(){ int h,w; cin >>h >>w; vector<string> mp(h); rep(i,h) cin >>mp[i]; vector<vector<int>> adj(h+w); int n=h+w; vector<int> uf(n,-1); unite(0,h,uf); unite(h-1,h,uf); unite(h-1,h+w-1,uf); unite(0,h+w-1,uf); rep(i,h) rep(j,w){ if(mp[i][j]=='#'){ unite(i,h+j,uf); } } set<int> tate,yoko; rep(i,h){ tate.insert(find(i,uf)); } rep(j,w){ yoko.insert(find(h+j,uf)); } print(min(tate.size(),yoko.size())-1); }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, M; ll dp[1 << 18]; vector<pair<int, int>> restrictions[18]; //--------------------------------------------------------------------------------------------------- int tot[19]; bool check(int msk) { int cnt = 0; rep(i, 1, N + 1) tot[i] = 0; rep(i, 0, N) if (msk & (1 << i)) { tot[i + 1]++; cnt++; } rep(i, 2, N + 1) tot[i] += tot[i - 1]; fore(p, restrictions[cnt]) if (p.second < tot[p.first]) return false; return true; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M; rep(i, 0, M) { int X, Y, Z; cin >> X >> Y >> Z; restrictions[X].push_back({ Y, Z }); } dp[0] = 1; rep(msk, 0, 1 << N) rep(nxt, 0, N) if (!(msk & (1 << nxt))) { if (check(msk + (1 << nxt))) dp[msk + (1 << nxt)] += dp[msk]; } cout << dp[(1 << N) - 1] << endl; }
/* * Author: Moon-light */ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define lowbit(x) ((x)&(-x)) #define sz(x) ((int)x.size()) #define fr(x) freopen(x,'r',stdin) #define fw(x) freopen(x,'w',stdout) #define mst(x,a) memset(x,a,sizeof(x)) #define all(a) begin(a),end(a) #define bitcnt(x) (__builtin_popcountll(x)) #define rep(i,a,b) for(int i = (a);i<=(b); ++i) #define per(i,a,b) for(int i = (a);i>=(b); --i) typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef pair<double,double> PDD; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<double> VB; typedef double db; template <class T> void clear(T& a) { T().swap(a);} template <class T,class S> inline bool upmin(T& a,const S&b){ return a>b ? a=b,1:0;} template <class T,class S> inline bool upmax(T&a, const S&b){ return a<b? a=b,1:0;} //fast read(improved by fread) char buf[1<<21],*p1=buf,*p2=buf; inline int getc(){ return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++; } inline int redi() { int ret = 0,f = 0;char ch = getc(); while (!isdigit (ch)) { if (ch == '-') f = 1; ch = getc(); } while (isdigit (ch)) { ret = ret * 10 + ch - 48; ch = getc(); } return f?-ret:ret; } //global variable const int N = 2e5+100; const int mod = 1e9+7; LL a[N]; int n; LL s[N]; //functions LL qmi(LL a,LL k,LL mod){ LL res = 1; while(k){ if(k&1) res = res*a%mod; k >>= 1; a = a*a%mod; } return mod; } int gcd(int a,int b) { return b==0? a:gcd(b,a%b); } // //code from here! Come on! Have a pleasant experience~ int main() { IO; cin>>n; rep(i,1,n) cin>>a[i]; sort(a+1,a+1+n,greater<LL>()); s[n] = a[n]; for(int i = n-1; i>=1; i--){ s[i] = s[i+1] + a[i]; } LL ans = 0; rep(i,1,n-1){ ans += (n-i)*a[i] - s[i+1]; } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define rep(i,n) for(int i = 0; i < n ; ++i) #define REP(i,a,b) for(int i = a ; i <= b ; ++i) #define filei freopen("input.txt", "r", stdin); #define fileo freopen("output.txt", "w", stdout); #define gooi cout<<"Case #"<<i+1<<" :"; #define s(n) scanf("%d",&n) #define rev(i,n) for(int i = n ; i >= 0 ; --i) #define REV(i,a,b) for(int i = a ; i >= b ; --i) #define miN(a,b) (((a)<(b))?(a):(b)) #define sc(n) scanf("%c",&n) #define tr(c,i) for(typeof((c).begin()) i=(c).begin();i!=(c).end();i++) #define INF 1000000000 #define pii pair<long long int,long long int> #define pb(a) push_back(a) #define F first #define S second typedef long long ll; using namespace std; const long long int mod = 998244353; long long int power(long long int x,long long int y){ long long int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0){ if (y & 1) res = (res*x) % mod; y = y>>1; x = (x*x) % mod; } return res; } long long int inv(long long int a){return power(a,mod-2);} long long int add(long long int a,long long int b){return (a+b)%mod;} long long int sub(long long int a,long long int b){return (a-b+mod)%mod;} long long int mul(long long int a,long long int b){return (a*b)%mod;} long long int divi(long long int a,long long int b){return (a*inv(b))%mod;} ll fact[5001]; // Please write the recurances once :( void pre(){ fact[0]=1; for(int i=1;i<=5000;i++) fact[i] = mul(fact[i-1],i); } ll ncr(int n,int r){ return divi(fact[n],mul(fact[n-r],fact[r])); } ll dp[15][5001]{0}; void solve(){ ll n,m;cin>>n>>m; dp[0][0]=1; for(int i=1;i<=14;i++){ for(int j=0;j<=n;j+=2){ for(int k=0;k<=m;k++){ if( j*(1<<(i-1)) + k > m ) break; dp[i][j*(1<<(i-1)) + k] = add(dp[i][j*(1<<(i-1)) + k],mul(ncr(n,j),dp[i-1][k])); } } } cout<<dp[14][m]<<"\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); pre(); long long int num = 1; //cin>>num; for(long long int i=0;i<num;i++){ solve(); } }
#include<bits/stdc++.h> #define fast ios_base::sync_with_stdio(false);cin.tie(NULL) #define ll long long int #define vi vector<int> #define pb push_back #define pop pop_back using namespace std; int main(){ ll n,k; cin>>n>>k; vector<pair<ll,ll>>v; while(n--) { ll x,y; cin>>x>>y; v.pb({x,y}); } sort(v.begin(),v.end()); for(auto it:v) { ll a=it.first; ll b=it.second; if(k<a) { cout<<k<<endl; return 0; } else k+=b; } cout<<k<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define FASTIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define ll long long #define mset(A,val) memset(A,val,sizeof(A)) #define fi(a,b) for(int i=a;i<=b;++i) #define fj(a,b) for(int j=a;j<=b;++j) #define all(x) x.begin(),x.end() #define vi vector<int> #define pii pair<int,int> #define pb push_back #define eb emplace_back #define DEBUG(a...) cout<<#a<<": ";for(auto &it:a)cout<<it<<" ";cout<<endl; #define debug(a)cout<<#a<<": "<<a<<"\n"; #define hola cout<<"hola"<<endl; #define int long long // --------------------------------------------------------------------------- const int mod = 1e9+7; const int maxn = 2e5 + 9; // --------------------------------------------------------------------------- void init(){} // --------------------------------------------------------------------------- void test_case(int tc) { // cout<<"Case #"<<tc<<": "; int n,k;cin>>n>>k; vector<pii>arr(n); fi(0,n-1)cin>>arr[i].first>>arr[i].second; sort(all(arr)); int currPos = 0; fi(0,n-1){ ll nextPos = arr[i].first; ll cost = nextPos-currPos; if(cost>k){ cout<<currPos+k; return; } k-=cost; currPos = nextPos; k+=arr[i].second; } cout<<currPos+k; init(); } int32_t main() { FASTIO; #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif int t=1; //cin>>t; for(int tc=1;tc<=t;++tc)test_case(tc); }
#include<bits/stdc++.h> #define re register using namespace std; inline int read(){ re int t=0;re char v=getchar(); while(v<'0')v=getchar(); while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar(); return t; } const int M=998244353; inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;} inline int ksm(re int x,re int y){ re int s=1; while(y){ if(y&1)s=1ll*s*x%M; x=1ll*x*x%M,y>>=1; } return s; } int n,f[502][502],a[502],b[502],px; signed main(){ n=read(); re int mn=2e9; for(re int i=1;i<=n;++i)for(re int j=1;j<=n;++j)f[i][j]=read(),mn=min(mn,f[i][j]); for(re int i=1;i<=n;++i){ for(re int j=1;j<=n;++j){ if(f[i][j]==mn){ px=i; } } } for(re int i=1;i<=n;++i)b[i]=f[px][i]-mn; for(re int i=1;i<=n;++i){ for(re int j=1;j<=n;++j){ a[i]=f[i][j]-b[j]; } } for(re int i=1;i<=n;++i)if(a[i]<0)return puts("No"),0; for(re int i=1;i<=n;++i)for(re int j=1;j<=n;++j)if(a[i]+b[j]!=f[i][j])return puts("No"),0; puts("Yes"); for(re int i=1;i<=n;++i)printf("%d ",a[i]);puts(""); for(re int i=1;i<=n;++i)printf("%d ",b[i]);puts(""); }
#include <sstream> #include <iostream> #include <fstream> #include <string> #include <vector> #include <array> #include <deque> #include <numeric> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <list> #include <cassert> #include <cmath> #include <climits> #include <map> #include <queue> #include <functional> #include <bitset> #include <cassert> using namespace std; using ll = int64_t; #define rep(i, a) for (int i = 0; i < (int)(a); ++i) #define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define repr(i, a) for (int i = (int)((a) - 1); i >= 0; --i) template <class T> class V1 : public vector<T> { public: V1(size_t n1, T init = T()) : vector<T>(n1, init) {} }; template <class T> class V2 : public vector<V1<T>> { public: V2(size_t n1, size_t n2, T init = T()) : vector<V1<T>>(n1, V1<T>(n2, init)) {} }; template <class T> class V3 : public vector<V2<T>> { public: V3(size_t n1, size_t n2, size_t n3, T init = T()) : vector<V2<T>>(n1, V2<T>(n2, n3, init)) {} }; template <class T> class V4 : public vector<V3<T>> { public: V4(size_t n1, size_t n2, size_t n3, size_t n4, T init = T()) : vector<V3<T>>(n1, V3<T>(n2, n3, n4, init)) {} }; template <class T> class V5 : public vector<V4<T>> { public: V5(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, T init = T()) : vector<V4<T>>(n1, V4<T>(n2, n3, n4, n5, init)) {} }; template <class T> vector<T> GetV1(istream& in, size_t N) { vector<T> ret; ret.reserve(N); rep(i, N) { T a; in >> a; ret.emplace_back(a); } return ret; } struct dstream { ofstream fout; dstream(const char* fname) : fout(fname) {} }; template <typename T> dstream& operator << (dstream& lhs, const T& rhs) { lhs.fout << rhs; cout << rhs; return lhs; } int main() { #ifdef ENV_LOCAL ifstream fin("input.txt"); dstream dout("output.txt"); auto& in = fin; auto& out = dout; #else auto& in = cin; auto& out = cout; out << ""; #endif int N; in >> N; V2<ll> c(N, N); rep(i, N) { rep(j, N) { in >> c[i][j]; } } ll min1 = LLONG_MAX; int imin = -1; rep(i, N) { if (c[0][i] < min1) { min1 = c[0][i]; imin = i; } } vector<ll> As(N), Bs(N); rep(i, N) { As[i] = c[i][imin]; } rep(j, N) { Bs[j] = c[0][j] - As[0]; rep2(i, 1, N) { ll b = c[i][j] - As[i]; if (b != Bs[j]) { cout << "No"; return 0; } } } cout << "Yes" << endl; cout << As[0]; rep2(i, 1, N) cout << " " << As[i]; cout << endl; cout << Bs[0]; rep2(i, 1, N) cout << " " << Bs[i]; return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define inf 1e17 #define precise(x,y) fixed<<setprecision(y)<<x #define int long long #define float double #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define f(n) for(int i=0;i<n;i++) #define fa(n) int a[n];for(int i=0;i<n;i++){cin>>a[i];} #define fv(n) vector<int> v ;for(int i=0;i<n;i++){int y;cin>>y;v.push_back(y);} #define test int t; cin>>t; while(t--) #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); void solve() { int n,m; cin>>n>>m; if(n<m) {if(n+3>m) { // if(n<=m) cout<<"Yes"; // else // { // cout<<"No"; // } } else { cout<<"No"; }} else { if(m+3>n) { // if(n<=m) cout<<"Yes"; // else // { // cout<<"No"; // } } else { cout<<"No"; } } } signed main() { fast // test { solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cerr<<#x<<" = "<<x<<endl; #define PQ(T) priority_queue<T,v(T),greater<T> > #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef tuple<int,int,int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return 0;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; int main() { int x,y; cin >> x >> y; if(x<y) swap(x,y); y += 3; if(x<y) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
// C headers #include <cassert> #include <cctype> // #include <cerrno> #include <cfloat> // #include <ciso646> #include <climits> // #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L // #include <ccomplex> #include <cfenv> #include <cinttypes> // #include <cstdalign> // #include <cstdbool> #include <cstdint> // #include <ctgmath> // #include <cuchar> // #include <cwchar> // #include <cwctype> #endif // C++ headers #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> // #include <atomic> #include <chrono> // #include <codecvt> // #include <condition_variable> #include <forward_list> // #include <future> #include <initializer_list> // #include <mutex> #include <random> #include <ratio> #include <regex> // #include <scoped_allocator> #include <system_error> // #include <thread> #include <tuple> // #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif #if __cplusplus >= 201402L #include <shared_mutex> #endif #define rep(i,n) for(int i=0; i<(n); i++) #define rep2(i, x, n) for (int i = x; i < (n); i++) #define all(x) (x).begin(), (x).end() #define endl "\n"; #define pb push_back #define mp make_pair using namespace std; using ll = long long; int main() { //cout << fixed << setprecision(10); cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; int money = 0; int i = 1; while (true) { money += i; if (money >= n) break; i++; } cout << i << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; long long s = 0; for (int i = 1; i <= n; i++){ if (s >= n){ cout << i - 1; return 0; } s += i; } if (n == 1){ cout << 1; } else if (n == 2){ cout << 3; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll N; cin >> N; vector<pair<ll, ll>> edge(N - 1); vector<vector<ll>> g(N); for (ll i = 0; i < N - 1; i++) { ll a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); edge[i] = {a, b}; } vector<ll> depth(N, -1); depth[0] = 0; vector<ll> q = {0}; while (q.size()) { ll at = q.back(); q.pop_back(); for (ll i : g[at]) if (depth[i] == -1) { depth[i] = depth[at] + 1; q.push_back(i); } } vector<ll> s(N); ll Q; cin >> Q; while (Q--) { ll t, e, x; cin >> t >> e >> x; ll a, b; tie(a, b) = edge[e - 1]; if (depth[a] > depth[b]) { swap(a, b); t ^= 3; } if (t == 1) { s[0] += x; s[b] -= x; } else s[b] += x; } q = {0}; while (q.size()) { ll at = q.back(); q.pop_back(); for (ll i : g[at]) if (depth[at] < depth[i]) { s[i] += s[at]; q.push_back(i); } } for (ll i : s) cout << i << endl; }
#include <iostream> #include <climits> #include <utility> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <algorithm> #include <chrono> #include <array> #include <cmath> #include <iomanip> #include <cassert> #include <numeric> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; constexpr int INF = INT_MAX / 4; constexpr ll LL_INF = LLONG_MAX / 4; const double EPS = 1e-9; const double PI = 2.0*acos(0.0); struct transformation { array<array<ll, 3>, 2> m; }; transformation unit = { array<ll, 3>({1,0,0}), array<ll, 3>({0,1,0}) }; transformation r90_clockwise = { array<ll, 3>({0,1,0}), array<ll, 3>({-1,0,0}) }; transformation r90_counterclockwise = { array<ll, 3>({0,-1,0}), array<ll, 3>({1,0,0}) }; transformation reflection_x(ll p) { return { array<ll, 3>({-1,0,2*p}), array<ll, 3>({0,1,0}) }; } transformation reflection_y(ll p) { return { array<ll, 3>({1,0,0}), array<ll, 3>({0,-1,2*p}) }; } transformation mul(const transformation& a, const transformation& b) { return { array<ll, 3>({b.m[0][0] * a.m[0][0] + b.m[0][1] * a.m[1][0], b.m[0][0] * a.m[0][1] + b.m[0][1] * a.m[1][1], b.m[0][0] * a.m[0][2] + b.m[0][1] * a.m[1][2] + b.m[0][2]}), array<ll, 3>({b.m[1][0] * a.m[0][0] + b.m[1][1] * a.m[1][0], b.m[1][0] * a.m[0][1] + b.m[1][1] * a.m[1][1], b.m[1][0] * a.m[0][2] + b.m[1][1] * a.m[1][2] + b.m[1][2]}) }; } pair<ll,ll> calc(const transformation& a, const pair<ll,ll>& c) { return {a.m[0][0] * c.first + a.m[0][1] * c.second + a.m[0][2], a.m[1][0] * c.first + a.m[1][1] * c.second + a.m[1][2]}; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, q; cin >> n; vector<pair<ll,ll>> c(n); for (int i = 0; i < n; i++) { ll a,b; cin >> a >> b; c[i] = {a,b}; } cin >> m; vector<pair<int, int>> ops(m); for (int i = 0; i < m; i++) { int op; int p = 0; cin >> op; if (op == 3 || op == 4) { cin >> p; } ops[i] = {op, p}; } cin >> q; vector<pair<ll, pair<ll, ll>>> queries(q); for (int i = 0; i < q; i++) { ll a,b; cin >> a >> b; queries[i] = {a, {i, b-1}}; } vector<pair<ll,ll>> ans(q); sort(queries.begin(), queries.end()); transformation cur_transformation = unit; int cur_q = 0; for (; cur_q < q && queries[cur_q].first == 0; cur_q++) { ans[queries[cur_q].second.first] = c[queries[cur_q].second.second]; // no transformation } for (ll i = 1; i <= m; i++) { transformation i_transformation = unit; switch (ops[i-1].first) { case 1: i_transformation = r90_clockwise; break; case 2: i_transformation = r90_counterclockwise; break; case 3: i_transformation = reflection_x(ops[i-1].second); break; default: i_transformation = reflection_y(ops[i-1].second); break; } cur_transformation = mul(cur_transformation, i_transformation); for (; cur_q < q && queries[cur_q].first == i; cur_q++) { ans[queries[cur_q].second.first] = calc(cur_transformation, c[queries[cur_q].second.second]); // no transformation } } for (int i = 0; i < q; i++) { cout << ans[i].first << " " << ans[i].second << "\n"; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int inf = INT_MAX; int main() { int n, x; cin >> n >> x; vector<int> ans; rep(i, n) { int a; cin >> a; if (a == x)continue; ans.push_back(a); }for (int i : ans)cout << i << ' '; cout << endl; }
#include <iostream> #include <vector> #include <algorithm> #include <unordered_set> #include <cstdlib> #include <cmath> #define ll long long using namespace std; int main(){ ll i,j,n,x,a,flag=0; cin >> n >> x; for (i = 0; i < n; i++) { cin >> a; if (a != x) { if (flag == 1) { cout << " "; } cout << a; flag = 1; } } cout << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const long long mod=998244353,INF=1e18; #define ll long long #define pll pair<ll,ll> #define x first #define y second #define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n"; vector<ll> par,sze; ll root(ll a) { if(a==par[a]) return a; return par[a]=root(par[a]); } void union1(ll a,ll b) { a=root(a),b=root(b); if(a==b) return ; if(sze[a]<sze[b]) swap(a,b); sze[a]+=sze[b]; par[b]=a; } ll power(ll a,ll b) { ll res=1; while(b) { if(b&1) { res=(res*a)%mod; } a=(a*a)%mod; b>>=1; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); ll t=1; // cin>>t; while(t--) { ll n; cin>>n; par.resize(n); sze.resize(n,1); for(int i=0;i<n;i++) par[i]=i; vector<vector<ll> > a(n); for(int i=0;i<n;i++) { ll u; cin>>u; u--; union1(u,i); } ll ans=0; for(int i=0;i<n;i++) if(root(i)==i) { ans++; } ans=power(2,ans); ans+=(mod-1); ans%=mod; cout<<ans<<"\n"; } return 0; }
using namespace std; #include "bits/stdc++.h" // add your library with double quotation #include"" here. // #include "atcoder/all" // using namespace atcoder; // define your macros here. // #define REP(a,b) for(long long a = 0;a < b;++a) using namespace std; typedef string::const_iterator State; #define eps 1e-8L #define MAX_MOD 1000000007LL #define GYAKU 500000004LL #define MOD 998244353LL #define pb push_back #define mp make_pair typedef long long ll; typedef long double ld; #define REP(a,b) for(long long (a) = 0;(a) < (b);++(a)) #define ALL(x) (x).begin(),(x).end() #define int long long vector<int> vertexs[300000]; int dists[300000]; int backs[300000]; int ans[300000]; int doned[300000]; int dfs(int now,int back){ for(auto x:vertexs[now]){ if(x == back) continue; backs[x] = now; dists[x] = dists[now] + 1; dfs(x,now); } return 0; } int value = 0; int dfs2(int now,int back){ value++; ans[now] = value; for(auto x:vertexs[now]){ if(x == back) continue; dfs2(x,now); } value++; return 0; } void solve(){ // write your solution here. int n; cin >> n; REP(i,n-1){ int a,b; cin >> a >> b; a--;b--; vertexs[a].push_back(b); vertexs[b].push_back(a); } dfs(0,-1); pair<int,int> best = mp(0,0); REP(i,n){ if(dists[i] > dists[best.first]){ best.first = i; } } dists[best.first] = 0; backs[best.first] = -1; dfs(best.first,-1); REP(i,n){ if(dists[i] > dists[best.second]){ best.second = i; } } while(true){ doned[best.second] = 1; value++; ans[best.second] = value; for(auto x:vertexs[best.second]){ if(doned[x] == true or x == backs[best.second]) continue; dfs2(x,best.second); } if(best.first == best.second) break; best.second = backs[best.second]; } REP(i,n){ if(i != 0) cout << " "; cout << ans[i]; } cout << endl; } #undef int // generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator) int main() { // Fasterize input/output script ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(100); // scanf/printf user should delete this fasterize input/output script int t = 1; //cin >> t; // comment out if solving multi testcase for(int testCase = 1;testCase <= t;++testCase){ solve(); } return 0; }
#line 1 "/workspaces/compro/lib/template.hpp" #line 1 "/workspaces/compro/lib/io/vector.hpp" #include <iostream> #include <vector> #ifndef IO_VECTOR #define IO_VECTOR template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { int size = v.size(); for (int i = 0; i < size; i++) { std::cout << v[i]; if (i != size - 1) std::cout << " "; } return out; } template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) { for (auto &el : v) { std::cin >> el; } return in; } #endif #line 4 "/workspaces/compro/lib/template.hpp" #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) (v).begin(), (v).end() #define coutd(n) cout << fixed << setprecision(n) #define ll long long int #define vl vector<ll> #define vi vector<int> #define MM << " " << using namespace std; template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } // 重複を消す。計算量はO(NlogN) template <class T> void unique(std::vector<T> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } #line 2 "main.cpp" long long solve(long long A, long long B, long long C) { vi loop; int now = A % 10; loop.push_back(now); while (true) { int next = (now * (A % 10)) % 10; int len = loop.size(); if (next == loop[0]) break; loop.push_back(next); now = next; } ll mod = loop.size(); ll res = 1; while (C) { if (C & 1) { res = res * B % mod; } B = B * B % mod; C /= 2; } return loop[(res - 1 + mod) % mod]; } // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { long long A, B, C; std::cin >> A >> B >> C; auto ans = solve(A, B, C); std::cout << ans << std::endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { long long A, B, C; cin >> A >> B >> C; if (A % 10 == 0) { cout << 0 << endl; return 0; } vector<int> number; { long long x = A % 10; number.push_back(x); for (int i=0; i<10; i++) { x *= A; x %= 10; if (x == A % 10) break; number.push_back(x); } } vector<int> numberB; { int R = number.size(); long long x = B % R; if (x == 0) { cout << number.at(number.size()-1) << endl; return 0; } numberB.push_back(x); for (int i=0; i<10; i++) { x *= B; x %= R; if (x == B % R) break; numberB.push_back(x); } } cout << number.at( ((int)( numberB.at((C-1) % numberB.size()) )-1) % number.size()) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef pair<int,int> P; typedef tuple<int,int,int> tpl; #define ALL(a) (a).begin(),(a).end() #define SORT(c) sort((c).begin(),(c).end()) #define REVERSE(c) reverse((c).begin(),(c).end()) #define EXIST(m,v) (m).find((v)) != (m).end() #define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i) #define RREP(i,n) RFOR(i,n,0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL<<60; constexpr long long MOD = 998244353; 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; } void Main(){ ll A,B,C; cin >> A >> B >> C; ll a = A*(A+1)/2 % MOD; ll b = B*(B+1)/2 % MOD; ll c = C*(C+1)/2 % MOD; ll ans = a*b%MOD; ans = ans*c%MOD; cout << ans << en; return; } int main(void){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15); int t=1; //cin>>t; REP(_,t) Main(); 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; } ll n, K, P, f[110][110*110*110], up; signed main() { scanf("%lld %lld %lld", &n, &K, &P), up = K * n * (n - 1) / 2, f[0][0] = 1; for(int i = 1; i < n; i++) { for(int j = 0; j <= up; j++) { f[i][j] = f[i - 1][j]; if(j >= i) (f[i][j] += f[i][j - i]) %= P; } for(int j = up; j >= (K + 1) * i; j--) (f[i][j] += P - f[i][j - (K + 1) * i]) %= P; } for(int i = 1; i <= n; i++) { ll nw = 0; for(int j = 0; j <= up; j++) (nw += f[i - 1][j] * f[n - i][j]) %= P; printf("%lld\n", (nw * (K + 1) % P - 1 + P) % P); } fprintf(stderr, "time=%.4f\n", (db)clock()/CLOCKS_PER_SEC); return 0; }
// Template #include "bits/stdc++.h" #define rep_override(x, y, z, name, ...) name #define rep2(i, n) for (int i = 0; i < (int)(n); ++i) #define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i) #define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; constexpr int inf = 1001001001; constexpr ll INF = 3003003003003003003LL; template <typename T> inline bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template <typename T> inline bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } struct IOSET { IOSET() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } ioset; template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &element : vec) is >> element; return is; } template <typename T> vector<T> operator--(vector<T> &vec) { for (T &element : vec) --element; return vec; } // Mod-Int template <int MOD> // MOD > 1 struct ModInt { static_assert(MOD >= 1); long long val; ModInt() = default; ModInt(long long x) : val(x % MOD + (x < 0 ? MOD : 0)) {} ModInt<MOD> &operator+= (const ModInt<MOD> &x) { if ((val += x.val) >= MOD) val -= MOD; return *this; } ModInt<MOD> &operator-= (const ModInt<MOD> &x) { if ((val -= x.val) < 0) val += MOD; return *this; } ModInt<MOD> &operator*= (const ModInt<MOD> &x) { (val *= x.val) %= MOD; return *this; } ModInt<MOD> operator+ (const ModInt<MOD> &x) const { return ModInt(*this) += x; } ModInt<MOD> operator- (const ModInt<MOD> &x) const { return ModInt(*this) -= x; } ModInt<MOD> operator* (const ModInt<MOD> &x) const { return ModInt(*this) *= x; } ModInt<MOD> pow(long long t) const { assert(t >= 0); ModInt<MOD> res(1), now(*this); while (t) { if (t & 1) res *= now; now *= now; t >>= 1; } return res; } ModInt<MOD> inv() const { return pow(MOD - 2); } ModInt<MOD> &operator/= (const ModInt<MOD> &x) { return *this *= x.pow(MOD - 2); } ModInt<MOD> operator/ (const ModInt<MOD> &x) const { return ModInt(*this) /= x; } friend std::istream &operator>> (std::istream &is, ModInt<MOD> &x) { long long a; is >> a; x = ModInt<MOD>(a); return is; } friend std::ostream &operator<< (std::ostream &os, const ModInt<MOD> &x) { os << x.val; return os; } }; constexpr int mod = 1000000007; using mint = ModInt<mod>; // Main int main() { int h, w; cin >> h >> w; vector<string> s(h); rep(i, h) cin >> s[i]; int k = 0; rep(i, h) rep(j, w) if (s[i][j] == '.') ++k; vector<vector<int>> a(h, vector<int>(w, 0)); rep(i, h) rep(j, w) { if (s[i][j] == '.') ++a[i][j]; } vector<vector<int>> b = a, c = a, d = a; rep(i, h) rep(j, w - 1) { if (s[i][j] == '.') a[i][j + 1] += a[i][j]; } rep(i, h) for (int j = w - 1; j > 0; --j) { if (s[i][j] == '.') b[i][j - 1] += b[i][j]; } rep(i, h - 1) rep(j, w) { if (s[i][j] == '.') c[i + 1][j] += c[i][j]; } for (int i = h - 1; i > 0; --i) rep(j, w) { if (s[i][j] == '.') d[i - 1][j] += d[i][j]; } mint ans(0); rep(i, h) rep(j, w) { if (s[i][j] == '#') continue; int x = a[i][j] + b[i][j] + c[i][j] + d[i][j] - 3; ans += (mint(2).pow(x) - mint(1)) * mint(2).pow(k - x); } cout << ans << "\n"; }
#include <bits/stdc++.h> //#include <boost/multiprecision/cpp_int.hpp> using namespace std; //using namespace boost::multiprecision; typedef long long int ll; typedef long double ld; typedef pair<ll,ll> P; #define PI 3.141592653589793 #define MOD 1000000007 //#define MOD 998244353 #define ALL(obj) (obj).begin(),(obj).end() const ll INF = 1LL << 60; //四方向への移動ベクトル const int dx[4] = {1, 0, -1, 0}; struct edge{//グラフに使うヤツ ll from,to,cost; }; typedef vector<vector<edge> > G; ll gcd(ll a,ll b){ if (a%b==0)return(b); else return(gcd(b,a%b)); } ll myPow(ll x,ll n,ll m){ if(n == 0) return 1; if(n % 2 == 0) return myPow(x * x % m, n / 2, m); else return x * myPow(x, n - 1, m) % m; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main(){ ll h,w; cin >> h >> w; string s[h]; for (ll i=0;i<h;i++)cin >> s[i]; ll tate[h][w],yoko[h][w]; memset(tate,-1,sizeof(tate)); memset(yoko,-1,sizeof(yoko)); ll lc=0; for (ll i=0;i<h;i++){ for (ll j=0;j<w;j++){ ll c=0,f=j; for (ll k=j;s[i][k]=='.' and k<w;k++){ c++; f=k; lc++; } for (ll k=j;k<=f;k++){ yoko[i][k]=c; } j=f; } } for (ll i=0;i<w;i++){ for (ll j=0;j<h;j++){ ll c=0,f=j; for (ll k=j;s[k][i]=='.' and k<h;k++){ c++; f=k; } for (ll k=j;k<=f;k++){ tate[k][i]=c; } j=f; } } ll ans=0; ll lcm=myPow(2,lc,MOD),inv=modinv(2,MOD); int ni[h+w+10]; ll a=1; for (ll i=0;i<h+w+5;i++){ ni[i]=a; a*=2; a%=MOD; } for (ll i=0;i<h;i++){ for (ll j=0;j<w;j++){ if (s[i][j]=='.'){ ll c=tate[i][j]+yoko[i][j]-1; c=ni[c]; ll x=(c-1)*lcm%MOD; x*=modinv(c,MOD); x%=MOD; ans+=x; ans%=MOD; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #define ALL(x) (x).begin(),(x).end() #define REP(i,n) for(int i=0;i<(n);i++) #define REP1(i,n) for(int i=1;i<(n);i++) #define REP2(i,d,n) for(int i=(d);i<(n);i++) #define RREP(i,n) for(int i=(n);i>=0;i--) #define CLR(a) memset((a),0,sizeof(a)) #define MCLR(a) memset((a),-1,sizeof(a)) #define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY)) typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI > VVI; typedef vector<string> VS; typedef vector<LL> VLL; typedef pair<int,int> PII; const int INF = 0x3f3f3f3f; const LL INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const int DX[]={1,0,-1,0},DY[]={0,-1,0,1}; int main(){ LL N; cin >> N; VI P(N); VI P_index(N); REP(i, N) { cin >> P[i]; P[i]--; P_index[P[i]] = i; } set<int> swap_idx; VI ans; REP(i, N-1) { while(i != P_index[i]) { int prev_num = P[P_index[i]-1]; if (swap_idx.count(P_index[i])) { cout << -1 << endl; return 0; } swap_idx.insert(P_index[i]); ans.PB(P_index[i]); swap(P[P_index[i]], P[P_index[i]-1]); swap(P_index[i], P_index[prev_num]); } } if (swap_idx.size() == N-1) { REP(i, ans.size()) { cout << ans[i] << endl; } } else { cout << -1 << endl; } }
#include<bits/stdc++.h> using namespace std; #define INF 9999999999999999 template<class T> bool chmin(T& a,T b) { if(a > b) { a = b; return true; } else { return false; } } struct Edge { long long to; long long weight; long long time; }; long long calc(long long nowminute,long long time) { if(nowminute % time == 0) return nowminute; else return(nowminute + time - nowminute % time); } using Graph = vector<vector<Edge>>; using P = pair<long long,long long>; int main() { long long N,M,X,Y; cin >> N >> M >> X >> Y; X--; Y--; Graph G(N); vector<long long> dp(N); vector<bool> kakutei(N); for(long long i = 0;i < M;i++) { long long tmpa,tmpb,tmpt,tmpk; cin >> tmpa >> tmpb >> tmpt >> tmpk; tmpa--; tmpb--; Edge tmpedge1,tmpedge2; tmpedge1.to = tmpb; tmpedge2.to = tmpa; tmpedge1.weight = tmpedge2.weight = tmpt; tmpedge1.time = tmpedge2.time = tmpk; G[tmpa].push_back(tmpedge1); G[tmpb].push_back(tmpedge2); } priority_queue<P,vector<P>,greater<P>> PQ; kakutei.assign(N,false); dp.assign(N,INF); dp[X] = 0; PQ.push(make_pair(0,X)); while(!PQ.empty()) { auto tmppair = PQ.top(); PQ.pop(); if(kakutei[tmppair.second]) continue; kakutei[tmppair.second] = true; for(auto nextedge : G[tmppair.second]) { if(kakutei[nextedge.to]) continue; if(chmin(dp[nextedge.to],calc(dp[tmppair.second],nextedge.time)+nextedge.weight)) { PQ.push(make_pair(dp[nextedge.to],nextedge.to)); } } } if(dp[Y] == INF) cout << -1 << endl; else cout << dp[Y] << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; #define fst first #define snd second /* clang-format off */ template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; }; template <class T> struct _vec<T, 0> { using type = T; }; template <class T, size_t D> using vec = typename _vec<T, D>::type; template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); } template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); } template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; } template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; } /* clang-format on */ const int MAX = 505000 + 10; int main() { #ifdef DEBUG ifstream ifs("in.txt"); cin.rdbuf(ifs.rdbuf()); #endif int N, K, M; while (cin >> N >> K >> M) { auto dp = make_v(N + 1, MAX, 0); auto sum = make_v(MAX, 0); dp[0][0] = 1; for (int n = 1; n <= N; n++) { for (int w = 0; w < MAX; w++) { sum[w] = (dp[n - 1][w] + (w - n >= 0 ? sum[w - n] : 0)) % M; } for (int w = 0; w < MAX; w++) { dp[n][w] = (sum[w] - (w - n * (K + 1) >= 0 ? sum[w - n * (K + 1)] : 0) + M) % M; } } for (int x = 1; x <= N; x++) { int res = 0; for (int w = 0; w < MAX; w++) { res = (res + 1ll * dp[x - 1][w] * dp[N - x][w] % M) % M; } res = (1ll * res * (K + 1)) % M; res = (res - 1 + M) % M; cout << res << endl; } } return 0; }
//#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using db = double; using ld = long double; template <typename T> using V = vector<T>; template <typename T> using VV = vector<vector<T>>; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(), (v).end() #define siz(v) (ll)(v).size() #define rep(i, a, n) for(ll i = a; i < (ll)(n); ++i) #define repr(i, a, n) for(ll i = n - 1; (ll)a <= i; --i) #define ENDL '\n' typedef pair<int, int> Pi; typedef pair<ll, ll> PL; constexpr ll INF = 1000000099; constexpr ll LINF = (ll)(1e18 + 99); const ld PI = acos((ld)-1); const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; template <typename T, typename U> inline bool chmin(T& t, const U& u) { if(t > u) { t = u; return 1; } return 0; } template <typename T, typename U> inline bool chmax(T& t, const U& u) { if(t < u) { t = u; return 1; } return 0; } template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T, typename Y> inline T mpow(T a, Y n) { T res = 1; for(; n; n >>= 1) { if(n & 1) res = res * a; a = a * a; } return res; } template <typename T> V<T> prefix_sum(const V<T>& v) { int n = v.size(); V<T> ret(n + 1); rep(i, 0, n) ret[i + 1] = ret[i] + v[i]; return ret; } template <typename T> istream& operator>>(istream& is, vector<T>& vec) { for(auto&& x : vec) is >> x; return is; } template <typename T, typename Y> ostream& operator<<(ostream& os, const pair<T, Y>& p) { return os << "{" << p.fs << "," << p.sc << "}"; } template <typename T> ostream& operator<<(ostream& os, const V<T>& v) { os << "{"; for(auto e : v) os << e << ","; return os << "}"; } template <typename... Args> void debug(Args&... args) { for(auto const& x : {args...}) { cerr << x << ' '; } cerr << ENDL; } ll dp[110][1000010] = {}; signed main() { cin.tie(0); cerr.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ll n, k,mod; cin >> n >> k >> mod; V<ll> ans(n + 1, 0); dp[1][0] = 1; rep(i, 1, n) { rep(j,0,n*n*k+1){ if(dp[i][j]==0)continue; (dp[i + 1][j] += dp[i][j]) %= mod; if(j+(k+1)*i<n*n*k+1)(dp[i + 1][j+(k+1)*i] += mod-dp[i][j]) %= mod; } rep(j,0,n*n*k+1){ if(j+i<n*n*k+1)(dp[i+1][j+i]+=dp[i+1][j])%=mod; } } rep(i, 1, (n + 3) / 2) { ll x=max(i-1,n-i); rep(j,0,x*x*k +1){ ll tmp=(dp[i][j]*dp[n-i+1][j])%mod; tmp%=mod; tmp*=k+1; tmp%=mod; (ans[i]+=tmp)%=mod; } ans[i]+=mod-1;//empty set ans[i]%=mod; ans[n - i + 1] = ans[i]; } rep(i, 1, n + 1) cout << ans[i] << ENDL; } //! ( . _ . ) ! // CHECK overflow,vector_size,what to output?
// 問題の URL を書いておく // #include <bits/stdc++.h> using namespace std; //#define ENABLE_PRINT #if defined(ENABLE_PRINT) #define Print(v) \ do {\ cout << #v << ": " << v << endl; \ }while(0) #define PrintVec(v) \ do {\ for(int __i = 0; __i < v.size(); ++__i) \ { \ cout << #v << "[" << __i << "]: " << v[__i] << endl; \ }\ }while(0) #define P(fmt, ...) printf(fmt, __VA_ARGS__) #define LP printf("L: %d\n", __LINE__) #else #define Print(v) ((void)0) #define PrintVec(v) ((void)0) #define P(fmt, ...) ((void)0) #define LP ((void)0) #endif #define rep(i, n) for(int i = 0; i < (int)(n); ++i) using ll = long long; const double PI = acos(-1); using P = pair<int, int>; int calcDist(const P& a, const P& b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } int calcMul(const P& a, const P& b) { return a.first * b.second - b.first * a.second; } pair<P, int> calcDistAndMul(const P& a, const P& b, const P& c) { P dm; dm.first = calcDist(a, c); dm.second = calcDist(b, c); P v0 = {c.first - a.first, c.second - a.second}; P v1 = {c.first - b.first, c.second - b.second}; auto mul = calcMul(v0, v1); return {dm, mul}; } int main(int, const char**) { int N; cin >> N; vector<P> S(N), T(N); rep(i, N) cin >> S[i].first >> S[i].second; rep(i, N) cin >> T[i].first >> T[i].second; if(N == 1) { cout << "Yes" << endl; return 0; } if(N == 2) { if(calcDist(S[0], S[1]) == calcDist(T[0], T[1])) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } vector<pair<P, int>> md(N - 2); rep(i, N - 2) { auto ti = i + 2; md[i] = calcDistAndMul(T[0], T[1], T[ti]); } sort(md.begin(), md.end()); #if 1 rep(i, N - 2) { P("[%d]: (%d, %d, %d)\n", i, md[i].first.first, md[i].first.second, md[i].second); } #endif rep(i, N) rep(j, N) { if(i == j) continue; P("i, j = (%d, %d)-----------------\n", i, j); vector<pair<P, int>> d(N - 2); int dcount = 0; rep(k, N) { if(k == i || k == j) continue; d[dcount++] = calcDistAndMul(S[i], S[j], S[k]); } sort(d.begin(), d.end()); #if 1 rep(k, dcount) { P("[%d]: (%d, %d, %d)\n", k, d[k].first.first, d[k].first.second, d[k].second); } #endif bool valid = true; rep(k, dcount) { if(md[k] != d[k]) { valid = false; break; } } if(valid) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
#include <iostream> #include <array> #include <algorithm> #include <vector> using namespace std; #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++) #define prl(a) cout << (a) << endl #define allrange(a) a.begin(),a.end() bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){ int N = S.size(); int dx = S[0].first-T[0].first; int dy = S[0].second-T[0].second; bool flg = true; reps(i,1,min(N,20)){ if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;} } return flg; } vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){ int N = S.size(); vector<pair<int,int>> PS(N); auto pt0 = S[0]; PS[0] = pt0; bool flg = true; int dx,dy,x,y; reps(i,1,N){ dx = S[i].first - pt0.first; dy = S[i].second - pt0.second; x=dx*a-dy*b; y=dx*b+dy*a; if(((x%c)!=0) || ((y%c)!=0)) {flg = false; break;} PS[i] = make_pair(x/c+pt0.first, y/c+pt0.second); } if(flg) return PS; else return vector<pair<int,int>>(); } int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); int N;cin >> N; vector<pair<int,int>> S(N),T(N); rep(i,N) { int x,y; cin >> x >> y; S[i].first = x;S[i].second = y; } rep(i,N) { int x,y; cin >> x >> y; T[i].first = x; T[i].second = y; } sort(allrange(T)); /*ピタゴラ三角形 5 12 13 8 15 17 3 4 5 */ constexpr int tri[12][3] ={ {3,4,5}, {4,3,5}, {-3,4,5}, {-4,3,5}, {0,1,1}, {1,0,1}, {-1,0,1}, {0,-1,1}, {-3,-4,5}, {-4,-3,5}, {3,-4,5}, {4,-3,5} }; /* vector<vector<int>> tri(0); tri.push_back(vector<int>({3,4,5})); tri.push_back(vector<int>({4,3,5})); tri.push_back(vector<int>({-3,4,5})); tri.push_back(vector<int>({-4,3,5})); tri.push_back(vector<int>({0,1,1})); tri.push_back(vector<int>({1,0,1})); tri.push_back(vector<int>({-1,0,1})); tri.push_back(vector<int>({0,-1,1})); tri.push_back(vector<int>({-3,-4,5})); tri.push_back(vector<int>({-4,-3,5})); tri.push_back(vector<int>({3,-4,5})); tri.push_back(vector<int>({4,-3,5})); // tri.push_back(vector<int>({-5, -12, 13})); // tri.push_back(vector<int>({-12, -5, 13})); // tri.push_back(vector<int>({-8,-15,17})); // tri.push_back(vector<int>({-15,-8,17})); // tri.push_back(vector<int>({5, 12, 13})); // tri.push_back(vector<int>({12, 5, 13})); // tri.push_back(vector<int>({8,15,17})); // tri.push_back(vector<int>({15,8,17})); // tri.push_back(vector<int>({-5, 12, 13})); // tri.push_back(vector<int>({-12, 5, 13})); // tri.push_back(vector<int>({-8,15,17})); // tri.push_back(vector<int>({-15,8,17})); // tri.push_back(vector<int>({5, -12, 13})); // tri.push_back(vector<int>({12, -5, 13})); // tri.push_back(vector<int>({8,-15,17})); // tri.push_back(vector<int>({15,-8,17})); */ bool flg; if(N==2){ auto sx = S[0].first-S[1].first; auto sy = S[0].second-S[1].second; auto tx = T[0].first-T[1].first; auto ty = T[0].second-T[1].second; flg= (sx*sx+sy*sy==tx*tx+ty*ty); } else { rep(j,12){ auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]); if(S2.empty()) continue; sort(allrange(S2)); flg = solve_translated(S2,T); if(flg) break; } } if(flg) prl("Yes"); else prl("No"); }
#include<bits/stdc++.h> #define X first #define Y second #define eb emplace_back #define pb push_back #define print1d(x) for(auto i : x){ cout << i << " ";} cout << '\n' #define print2d(x) for(auto u : x){ print1d(u);} cout << "------" << '\n' #define CLR(x, y) memset(x, y, sizeof(x)) #define ALL(X) X.begin(), X.end() #define io_o() ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0) #define mp(x, y) make_pair(x, y) #define mpp(w, x, y, z) make_pair(mp(w, x), mp(y, z)) #define For(i, n) for(int i = 0; i < n; i++) #define For1(i, n, m) for(int i = n; i < m; i++) #define For2(X) for(auto x : X) #define maxn 200005 #define pi = acos(-1) using namespace std; typedef long long ll; const ll mod = (1e9)+7; typedef pair<int,int> pii; typedef pair<ll,ll> pll; //priority_queue<ll> pq; priority_queue<pii,vector<pii>, greater<pii>> pq; //sort(a, a+n, greater<int>()); pll b[maxn]; int main(){ io_o(); //cute! ll n, m; cin >> n >> m; set<ll> pos; pos.insert(n); For(i, m) { cin >> b[i].X >> b[i].Y; } sort(b, b+m); vector<ll> add, era; for(int i = 0; i < m; i++) { // cout << i << '\n'; ll x = b[i].X, y = b[i].Y; if(x == 0) continue; if(y-1 >= 0 && pos.count(y-1)) { add.push_back(y); // cout << "push " << y << '\n'; } else if(y+1 <= 2*n && pos.count(y+1)) { add.push_back(y); // cout << "push " << y << '\n'; } if(pos.count(y)) { era.push_back(y); // cout << "erase " << y << '\n'; } if(i == m-1 || x != b[i+1].X) { for(auto ii: era) { if(pos.count(ii)) pos.erase(ii); } // cout << "pos\n"; // for(auto a: pos) cout << a << ' '; cout << '\n'; for(auto ii: add) pos.insert(ii); add.clear(); era.clear(); // for(auto a: pos) cout << a << ' '; cout << '\n'; } } cout << pos.size() << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long c[64][64]; int main() { cin.tie(nullptr)->sync_with_stdio(false); c[0][0] = 1; for (int i = 1; i < 64; i++) { c[i][0] = c[i][i] = 1; for (int j = 1; j < i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } int a, b; long long k; cin >> a >> b >> k; k--; string ans = ""; int cnta = a; int cntb = b; for (int i = 0; i < a + b; i++) { if (!cnta) { ans += 'b'; cntb--; } else if (!cntb) { ans += 'a'; cnta--; } else { if (k < c[cnta + cntb - 1][cnta - 1]) { ans += 'a'; cnta--; } else { k -= c[cnta + cntb - 1][cnta - 1]; ans += 'b'; cntb--; } } } cout << ans << "\n"; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using pll = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using pdd = pair<double, double>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using pbb = pair<bool, bool>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using pss = pair<string, string>; using vs = vector<string>; using vvs = vector<vs>; using vvvs = vector<vvs>; #define repeat(counter, start, goal, step) for (int counter = (int)(start); counter < (int)(goal); counter += step) #define rep(counter, times) repeat(counter, 0, times, 1) #define rep2(counter, start, goal) repeat(counter, start, goal, 1) #define rep0(times) repeat(counter, 0, times, 1) #define rrepeat(counter, start, goal, step) for (int counter = (int)(start); counter > (int)(goal); counter -= step) #define rep3(counter, start, goal) rrepeat(counter, start, goal, 1) #define rrep(counter, start, goal) rrepeat(counter, start - 1, goal - 1, 1) #define all(container) begin(container), end(container) template <typename T> bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template <typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } void yn(bool flag) { cout << (flag ? "yes" : "no") << "\n"; } void Yn(bool flag) { cout << (flag ? "Yes" : "No") << "\n"; } void YN(bool flag) { cout << (flag ? "YES" : "NO") << "\n"; } void set_prec(const int &digits) { cout << fixed << setprecision(digits); cerr << fixed << setprecision(digits); } template <typename T> void pr(const T &obj) { cerr << obj; } template <typename T, typename ...Ts> void pr(const T &first, const Ts &...rest) { pr(first); pr(", "); pr(rest...); } template <typename S, typename T> void pr(const pair<S, T> &pair) { pr("("); pr(pair.first); pr(", "); pr(pair.second); pr(")"); } template <typename T> void pr(const vector<T> &vec) { pr("{"); for (T obj : vec) { pr(obj); pr(", "); } pr("}"); } template <typename T> void pr(const vector<vector<T>> &vv) { pr("\n"); rep(index, vv.size()) { pr("["); pr(index); pr("]: "); pr(vv[index]); pr("\n"); } } template <typename T> void pr(const set<T> &vec) { pr("{"); for (T obj : vec) { pr(obj); pr(", "); } pr("}"); } template <typename S, typename T> void pr(const map<S, T> &map) { pr("{"); for (pair<S, T> pair : map) { pr("("); pr(pair.first); pr(": "); pr(pair.second); pr("), "); } pr("}"); } #define db(obj) cerr << #obj << ": "; pr(obj); cerr << " " #define dl(obj) db(obj); cerr << "\n"; #define dm(...) cerr << "(" << #__VA_ARGS__ << "): ("; pr(__VA_ARGS__); cerr << ") " #define dml(...) dm(__VA_ARGS__); cerr << "\n" // #include <atcoder/all> // using namespace atcoder; // const ll MOD = 1000000007; // const ll MOD = 998244353; int main() { int N, S, D; cin >> N >> S >> D; vi X(N), Y(N); rep(i, N) cin >> X[i] >> Y[i]; bool f = false; rep(i, N) if (X[i] < S && Y[i] > D) f = true; Yn(f); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; double dp[105][105][105]; double dfs(int a,int b,int c,int cnt){ if(a==100||b==100||c==100){ return cnt; } if(dp[a][b][c]) { return dp[a][b][c]; } dp[a][b][c]=(a*dfs(a+1,b,c,cnt+1)+b*dfs(a,b+1,c,cnt+1)+c*dfs(a,b,c+1,cnt+1))*1.0/(a+b+c); return dp[a][b][c]; } void solve(){ int a,b,c; cin>>a>>b>>c; printf("%.10f",dfs(a,b,c,0)); } int main() { int _=1; while(_--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll a; cin >> a; ll b; cin >> b; ll c; cin >> c; if((a*a+b*b)<c*c) { cout << "Yes" <<'\n'; } else { cout << "No" <<'\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; //int:2*10**9 typedef long double ld; typedef pair<ll,ll> P; #define REP(i,n) for(ll i = 0; i<(ll)(n); i++) #define FOR(i,a,b) for(ll i=(a);i<=(b);i++) #define FORD(i,a,b) for(ll i=(a);i>=(b);i--) #define vec2(name,i,j,k) vector<vector<ll>> name(i,vector<ll>(j,k)) #define vec3(name,i,j,k,l) vector<vector<vector<ll>>> name(i,vector<vector<ll>>(j,vector<ll>(k,l))) #define pb push_back #define MOD 1000000007 //998244353 #define PI 3.141592653 #define INF 100000000000000 //14 //cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); int main(){ ll a, b, c; cin >> a >> b >> c; if (a>b) cout << "Takahashi" << endl; else if (a<b) cout << "Aoki" << endl; else { if (c==0) cout << "Aoki" << endl; else cout << "Takahashi" << endl; } }
// #pragma GCC optimize("Ofast") // #pragma GCC optimize ("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; #define int long long unordered_map <int, int> dp, seen; int x; int go(int y) { if(x >= y) return abs(x-y); if(seen[y]) return dp[y]; seen[y] = 1; int a = abs(x-y); if(y%2) { a = min(a, 2+go(y/2)); a = min(a, 2+go((y+1)/2)); } else { a = min(a, 1+go(y/2)); } dp[y] = a; return dp[y]; } void solve() { int y; cin >> x >> y; cout << go(y) << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int t = 1; //cin >> t; for(int i = 1; i <= t; i++) { solve(); } } /* */
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; 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<int, int> > vpii; 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; #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()) #define MATHPI acos(-1) #define itn int; 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 modpow(ll x, ll y) { if(y == 1) { return x; } ll ans; if(y % 2 == 1) { ll r = modpow(x,(y-1)/2); ans = r * r % MOD; ans = ans * x % MOD; } else { ll r = modpow(x,y/2); ans = r * r % MOD; } return ans; } ll modncr(ll N, ll K) { ll res = 1; ll p=1; for (ll n = 0; n < K; ++n) { res = (res*(N - n))%MOD; p = (p*(n + 1))%MOD; } return (res*modpow(p,MOD-2))%MOD; } ll x, y; map <ll, ll> memo; ll rec(ll y) { if (y == 1) return abs(x-y); if (memo.find(y) != memo.end()) return memo[y]; if (y%2 == 0) { memo[y] = min(rec(y/2)+1, abs(y-x)); } else { memo[y] = min({rec(y+1)+1, rec(y-1)+1, abs(y-x)}); } return memo[y]; } signed main() { ll y; cin >> x >> y; mes(rec(y)); }
#include<bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(),v.end() #define puts(i) cout << i << endl #define INF INT_MAX #define INFL LLONG_MAX typedef long long ll; using namespace std; int main(){ ll n; cin >> n; string s; cin >> s; char dna; ll a=0,t=0,c=0,g=0,count=0; for(ll i=0;i<n-1;i++){ a=0; t=0; c=0; g=0; for(ll k=i;k<n;k++){ dna=s.at(k); if(dna=='A') a++; else if(dna=='T') t++; else if(dna=='C') c++; else if(dna=='G') g++; if(a==t && a!=0 && t!=0 && c==g) count++; else if(c==g && c!=0 && g!=0 && a==t) count++; } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; string a, b, c, p; set<char> st; char mp[26]; ll str_to_num(string s) { ll res = 0; for (char x : s) res = 10*res + mp[x-'a']; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin>>a>>b>>c; for (char x : a) st.insert(x); for (char x : b) st.insert(x); for (char x : c) st.insert(x); if (st.size() > 10) { cout<<"UNSOLVABLE"<<endl; return 0; } int n = 0; for (char x : st) mp[x-'a'] = 'a' + n++; for (char &x : a) x = mp[x-'a']; for (char &x : b) x = mp[x-'a']; for (char &x : c) x = mp[x-'a']; p = "abcdefghij"; do { if (p[0] == a[0] || p[0] == b[0] || p[0] == c[0]) { continue; // no leading zeros } n = 0; for (char x : p) mp[x-'a'] = n++; ll aa = str_to_num(a); ll bb = str_to_num(b); ll cc = str_to_num(c); if (aa + bb == cc) { cout<<aa<<endl<<bb<<endl<<cc<<endl; return 0; } } while (next_permutation(p.begin(), p.end())); cout<<"UNSOLVABLE"<<endl; }
#include<iostream> #include<vector> #include<string> #include<sstream> #include<functional> #include<algorithm> #include<map> #include<queue> #include<stack> using namespace std; int main() { int n; cin >> n; vector<vector<long long>> a(n, vector<long long>(n)),coma(n,vector<long long>(n)), comb(n, vector<long long>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> a[i][j]; } for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { coma[i][j] = a[i][j] - a[i][j - 1] - (a[i - 1][j] - a[i - 1][j - 1]); } } for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { comb[i][j] = a[i][j] - a[i - 1][j] - (a[i][j - 1] - a[i - 1][j - 1]); } } bool ans = true; for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { if (comb[i][j] != 0 || coma[i][j] != 0) ans = false; } } if (!ans) cout << "No" << endl; else { vector<int> ansa(n), ansb(n), an(n), bn(n);; for (int i = 0; i < n; i++) ansa[i] = a[0][i]; for (int i = 0; i < n; i++) ansb[i] = a[i][0]; for (int i = 0; i < n; i++) an[i] = ansa[i]; for (int i = 0; i < n; i++) bn[i] = ansb[i]; sort(an.begin(), an.end()); sort(bn.begin(), bn.end()); int af = ansa[0] - an[0], bf = ansb[0] - bn[0]; if (af + bf > ansa[0]) cout << "No" << endl; else { cout << "Yes" << endl; for (int i = 0; i < n; i++) { ansb[i] -= af; cout << ansb[i]; if (i < n - 1) cout << " "; else cout << endl; } int k = ansa[0]; for (int i = 0; i < n; i++) { ansa[i] -= k - af; cout << ansa[i]; if (i < n - 1) cout << " "; else cout << endl; } } } }
// template #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Input { int id; int x; int y; int r; int sx,sy,gx,gy; }; bool compare_pos(Input a, Input b) { if (a.x == b.x) { return a.y < b.y; } else { return a.x < b.x; } } bool compare_id(Input a, Input b) { return a.id < b.id; } int main(void) { int N; cin >> N; vector<Input> input(N); for (size_t i = 0; i < N; i++) { input[i].id = i; cin >> input[i].x >> input[i].y >> input[i].r; } // input[0]が一番左上 sort(input.begin(), input.end(), compare_pos); int idx = 0; int left = 0; int top = 0; for (int i = 0; i < N; i++) { if (i < N-1 && input[i + 1].x == input[i].x) { input[i].sx = left; input[i].sy = top; input[i].gx = input[i].x + 1; input[i].gy = input[i].y + 1; top = input[i].y + 1; } else { input[i].sx = left; input[i].sy = top; input[i].gx = input[i].x + 1; input[i].gy = 10000; left = input[i].x + 1; top = 0; } } sort(input.begin(), input.end(), compare_id); for (int i = 0; i < N; i++) { printf("%d %d %d %d\n", input[i].sx, input[i].sy, input[i].gx, input[i].gy); } }
#include <bits/stdc++.h> #define f(a, n) for (int a = 0; a < n; a++) #define F(a, n) for (int a = 1; a <= n; a++) using namespace std; struct Student{ map<int, int> mp; int sz = 1; Student* par = this; }; Student* find(Student* a){ if (a->par == a) return a; return a->par = find(a->par); } void join(Student* a, Student* b){ // b joins a a->sz += b->sz; b->par = a; for (auto p: b->mp){ int x = p.first, y = p.second; a->mp[x] += y; } } int main(){ int n, q; cin >> n >> q; Student A[200005]; F(i, n) { int x; cin>>x; A[i].mp[x]++; } F(asdf, q){ int a, b, c; cin >> a >> b >> c; if (a == 1){ auto d = find(&A[b]); auto e = find(&A[c]); if (d != e){ if (d->sz >= e->sz){ join(d, e); } else{ join(e, d); } } } else{ cout<<find(&A[b])->mp[c]<<endl; } } }
//#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> // Общий файл. //#include <ext/pb_ds/tree_policy.hpp> // Содержит класс tree_order_statistics_node_updat //#include <ext/pb_ds/detail/standard_policies.hpp> //#define int long long #define ll long long #define ull unsigned long long #define ld long double #define rep(i, l, r) for (int i = l; i <= r; i++) #define repb(i, r, l) for (int i = r; i >= l; i--) #define sz(a) (int)a.size() #define fi first #define se second #define all(x) x.begin(),x.end() #define pb push_back #define mp(a, b) make_pair(a, b) #define ret(a) {cout << a; exit(0);} using namespace std; mt19937 mrand(random_device{}()); const int mod = 998244353; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod;if(a==1)return 1; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} typedef pair < int , int > pii ; typedef pair < long long , long long > pll ; const int N = 5000; using namespace std; int n, m; int u[N],v[N]; int c[N]; char ans[N]; int id[200][200]; vector <int> gr[N]; bool was[N]; void dfs(int u, int col){ was[u] = 1; for(int to : gr[u]){ if(c[to] != col)continue; if(ans[id[u][to]] != 0)continue; int i = id[u][to]; if(v[i] == u) ans[i] = '<'; else ans[i] = '>'; if(!was[to]) dfs(to,col); } } main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; rep(i,1,m){ cin >> u[i] >> v[i]; id[u[i]][v[i]] = id[v[i]][u[i]] = i; gr[u[i]].pb(v[i]); gr[v[i]].pb(u[i]); } rep(i,1,n) cin >> c[i]; for(int i = 1; i <= n; i++){ if(!was[i]) dfs(i,c[i]); } for(int i = 1; i <= m;i++){ if(c[u[i]] > c[v[i]]) ans[i] = '>'; else if(c[u[i]] < c[v[i]]) ans[i] = '<'; } for(int i = 1; i <= m; i++){ if(ans[i] == '<') cout << "<-\n"; else cout << "->\n"; } }
#include <bits/stdc++.h> #define endl '\n' #define modulo 1000000007 #define int long long #define PI acos(-1) #pragma GCC optimize("-Ofast") #pragma GCC optimize("trapv") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops") #define sinDegrees(x) sin((x) * PI / 180.0) #define cosDegrees(x) cos((x) * PI / 180.0) #define tanDegrees(x) tan((x) * PI / 180.0) #define atanDegrees(x) atan(x)* 180.0 / PI #define asinDegrees(x) asin(x)* 180.0 / PI #define EPS 0.000000001 using namespace std; int power(int x,int y,int m) { int temp; if(y == 0) return 1; temp = (power(x, y/2,m))%m; if (y%2 == 0) return ((temp%m)*temp); else return ((x*temp%m)*temp%m)%m; } int inv(int x,int m=modulo) { return (power(x,m-2,m))%m; } ///IOI 2021 isA int32_t main() { //freopen("output.txt","w",stdout); //freopen("colors.in","r",stdin); cin.tie(0),iostream::sync_with_stdio(0); string a,b; cin>>a>>b; int S=0,S2=0; for(int i=0;i<a.size();i++) S+=a[i]-'0',S2+=b[i]-'0'; cout<<max(S,S2); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<int> vi; typedef pair<int, int> pi; typedef pair<long long, long long> pl; #define rep(i, a, b) for (int i = a; i < b; i++) void solve() { ll a, b; cin >>a >> b; ll sa = 0; while(a > 0){ sa += a%10; a/=10; } ll sb = 0; while(b > 0){ sb += b%10; b/=10; } cout << max(sa, sb) << endl; } int main(int argc, char const *argv[]) { ios::sync_with_stdio(0); cin.tie(0); // int t; // cin >> t; // while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long x, y, a, b; long long s; cin >> x >> y >> a >> b; s = 0; while ( x*a <= x+b && x*a < y && x < x*a) { x *= a; s++; } s += ( (y-x-1) / b < 0 ? 0 : (y-x-1) / b ); cout << s << endl; return (0); }
#include<bits/stdc++.h> using namespace std; #define arep(i,x,n) for(int i=int(x);i<(int)(n);i++) #define rep(i,n) for(long long i = 0;i < n;++i) #define rrep(i,n) for(int i=int(n-1);i>=0;i--) #define fs first #define sc second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define coy cout<<"Yes"<<endl #define con cout<<"No"<<endl #define pi 3.141592653589793 #define eps 0.00000001 #define INF 1e9+7 #define LINF 1e18+10 using ll = long long; using P = pair<int, int>; using lP = pair<ll, ll>; using fP = pair<double, double>; using PPI = pair<P, int>; using PIP = pair<int, P>; using Ps = pair<int, string>; using vi = vector<int>; using vl = vector<ll>; using vc = vector<char>; using vd = vector<double>; using vs = vector<string>; using vp = vector<P>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; using vvd = vector<vector<double>>; using vvc = vector<vector<char>>; using vvp = vector<vector<P>>; using vvb = vector<vector<bool>>; template <typename T> bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } //const ll mod=998244353; const ll mod = 1e9 + 7; const ll MAX = 300000; template <typename T> T abs(T a) { if (a < 0)return -a; else return a; }//2020/09/30 stdlib has abs(long) abs(long long) error ////////////////////////////////////// ll x,y,a,b; int main(){ cin>>x>>y>>a>>b; ll ans=0; y--; while(x<b&&x<y){ x*=a; ans++; } if(x>y){ cout<<ans-1<<endl; return 0; } ans+=(y-x)/b; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n,m,q; cin >> n >> m >> q; vector<int> w(n); vector<int> v(n); for (int i=0;i<n;i++) cin >> w[i] >> v[i]; vector<int> x(m); for (int i=0;i<m;i++) cin >> x[i]; for (int i=0;i<q;i++) { int l,r; cin >> l >> r; l--; r--; vector<int> ok; for (int j=0;j<m;j++) { if (j >= l && j <= r) continue; ok.emplace_back(x[j]); } sort(ok.begin(),ok.end()); int sz = ok.size(); vector<int> left = v; int ans = 0; for (int j=0;j<sz;j++) { int maxv = -1; int mind = -1; for (int k=0;k<n;k++) { if (w[k] <= ok[j]) { if (left[k] > maxv) { maxv = left[k]; mind = k; } } } if (mind == -1) continue; ans += maxv; left[mind] = 0; } cout << ans << endl; } }
//@formatter:off #include<bits/stdc++.h> #define overload4(_1,_2,_3,_4,name,...) name #define rep1(i,n) for (ll i = 0; i < ll(n); ++i) #define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i) #define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d) #define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__) #define rrep1(i,n) for (ll i = ll(n)-1; i >= 0; i--) #define rrep2(i,n,t) for (ll i = ll(n)-1; i >= (ll)t; i--) #define rrep3(i,n,t,d) for (ll i = ll(n)-1; i >= (ll)t; i-=d) #define rrep(...) overload4(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define popcount(x) __builtin_popcountll(x) #define pb push_back #define eb emplace_back #ifdef __LOCAL #define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); cout << flush; } #else #define debug(...) void(0) #endif #define INT(...) int __VA_ARGS__;scan(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__) #define STR(...) string __VA_ARGS__;scan(__VA_ARGS__) #define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__) #define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__) #define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__) using namespace std; using ll = long long; using ld = long double; using P = pair<int,int>; using LP = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vector<int>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vd = vector<double>; using vvd = vector<vector<double>>; using vs = vector<string>; using vc = vector<char>; using vvc = vector<vector<char>>; using vb = vector<bool>; using vvb = vector<vector<bool>>; using vp = vector<P>; using vvp = vector<vector<P>>; template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; } template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; } template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; } template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; } template<class T> void vecout(const vector<T> &v,char div='\n') { rep(i,v.size()) cout<<v[i]<<(i==int(v.size()-1)?'\n':div);} template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;} template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;} void scan(){} template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); } template<class T> void print(const T& t){ cout << t << '\n'; } template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); } template<class... T> void fin(const T&... a) { print(a...); exit(0); } struct Init_io { Init_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << boolalpha << fixed << setprecision(15); } } init_io; const string yes[] = {"no","yes"}; const string Yes[] = {"No","Yes"}; const string YES[] = {"NO","YES"}; const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on int main() { INT(x, y, z); rrep(i, 1000000) { if (y * z > i * x) fin(i); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll;const int mod = 1000000007; //const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} int main(){ int N; cin>>N; vector<ll> A(N+1); ll a; for(int i=1;i<=N;i++){ cin>>a; A[i]=a+A[i-1]; } vector<vector<ll> >dp(N+1,vector<ll>(N+1,0)); for(int i=1;i<=N;i++){ for(int j=0;j<=N;j++){ dp[i][j]=A[j]%i; } } vector<vector<mint> > g(N+1,vector<mint>(N+1,0)); for(int i=1;i<=N;i++){ g[1][i]=1; } for(int i=2;i<=N;i++){ map<ll,ll> mp1; map<ll,mint> mp2; for(int j=i-1;j<=N;j++){ mp1[dp[i][j]]++; if(mp1[dp[i][j]]>1){ g[i][j]=mp2[dp[i][j]]; }; mp2[dp[i][j]]+=g[i-1][j]; } /* cout<<"i: "<<i<<" "; for(auto a:mp1){ cout<<a.first<<" "<<a.second<<endl; } */ } mint ans=0; for(int i=0;i<=N;i++){ ans+=g[i][N]; } cout<<ans<<endl; /* for(int i=0;i<=N;i++){ for(int j=0;j<=N;j++){ cout<<g[i][j]<<" "; } cout<<endl; } cout<<" A "<<endl; for(int i=0;i<=N;i++){ cout<<A[i]<<" "; } cout<<endl; cout<<" dp "<<endl; for(int i=0;i<=N;i++){ for(int j=0;j<=N;j++){ cout<<dp[i][j]<<" "; } cout<<endl; } */ }
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<tuple> #include<cmath> #include<iomanip> using namespace std; typedef long long ll; typedef vector<ll> v; typedef vector<vector<ll>> vv; #define MOD 1000000007 #define INF 1001001001 #define MIN -1001001001 #define rep(i,k,N) for(int i=k;i<N;i++) #define MP make_pair #define MT make_tuple //tie,make_tuple は別物 #define PB push_back #define PF push_front #define all(x) (x).begin(),(x).end() int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; int main(){ ll N; cin>>N; v A(N),sA(N,0); rep(i,0,N)cin>>A[i]; sA[0]=A[0]; rep(i,1,N){ sA[i]=sA[i-1]+A[i]; } vv dp(N,v(N,0)); rep(i,0,N)dp[0][i]=1; rep(i,1,N){ ll NM = i+1; v mods(NM,0); mods[sA[i-1]%NM]+=dp[i-1][i-1]; mods[sA[i-1]%NM]%=MOD; rep(j,i,N){ dp[i][j] += mods[sA[j]%NM]; dp[i][j] %=MOD; mods[sA[j]%NM]+=dp[i-1][j]; mods[sA[j]%NM] %=MOD; } } ll ans = 0; rep(i,0,N){ ans+=dp[i][N-1]; ans%=MOD; } cout<<ans; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; constexpr ll MOD = 998244353; #ifndef ONLINE_JUDGE template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;} template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} void print_sim_py(void) {cout << endl;} template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();} template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);} #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define print(...) print_sim_py(__VA_ARGS__); #else #define print(...); #endif int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; vector<ll> nc(10,0); for(int i=0; i<s.length(); i++){ ll x=s[i]-'0'; nc[x]++; } auto tmp = nc; ll ie=10,je=10; switch(s.length()){ case 1: je = 1; nc[0]++; case 2: ie = 1; nc[0]++; } auto f = [&](){ string ans = "No"; for(ll i=0; i<ie; i++){ for(ll j=0; j<je; j++){ for(ll k=0; k<10; k++){ tmp[i]--; tmp[j]--; tmp[k]--; if(tmp[i]>=0 && tmp[j]>=0 && tmp[k]>=0){ ll v = i*100+j*10+k; if(v%8==0){ ans = "Yes"; return ans; } } tmp = nc; } } } return ans; }; auto ans = f(); cout << ans << endl; return 0; }
#include<iostream> #include<vector> #include<queue> #include<stack> #include<algorithm> #include<set> #include<iterator> #include <map> #include<cmath> #include <list> #include<string> #include <stdlib.h> #include<ctime> #include<cstring> #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL); #define line cout<<'\n' typedef long long ll; using namespace std; vector<long double>roots1; void printVec(vector<long double>&v) { int sz = v.size(); for (long double x : v) { cout << x << " "; } line; } void printVec(vector<double>&v) { int sz = v.size(); for (double x : v) { cout << x << " "; } line; } void printVec(vector<vector<long double>>&vec) { for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec[i].size(); j++) { cout << vec[i][j] << " "; } line; } line; } void print(vector<pair<double, double>>&vec) { int sz = vec.size(); for (int i = 1; i < sz; i++) { cout << vec[i].first << " " << vec[i].second << endl; } line; } int main() { fastIO #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s; ll n; cin >> s; int sz=s.size(); if(sz<=2) { string s1=s; reverse(s1.begin(), s1.end()); if(stoi(s)%8==0 or stoi(s1)%8==0) { cout<<"Yes\n"; } else { cout<<"No\n"; } } else { bool flag=false; vector<int>freq(10,0); for(int i=0;i<sz;i++) { freq[s[i]-'0']++; } for(int i=104;i<1000;i+=8) { int temp=i; flag=false; vector<int> freq1(10,0); while(temp!=0) { freq1[temp%10]++; temp/=10; } for(int j=0;j<10;j++) { if(freq[j]<freq1[j]) { flag=true; break; } } if(!flag) { cout<<"Yes\n"; break; } } if(flag) { cout<<"No\n"; } } return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn=5e5+100; int read() { int ans=0;bool f=0;char ch=getchar(); while(ch<'0' or ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0' and ch<='9'){ans=(ans<<1)+(ans<<3)+(ch^48);ch=getchar();} return f?~ans+1:ans; } int n,ans,nx[maxn],per[maxn]; char s1[maxn],s2[maxn]; vector<int>g1,g2; int abx(int x) { return x>0?x:-x; } int main() { n=read(); scanf("%s%s",s1+1,s2+1); int now=1; for(int i=1;i<=n;++i) { if(s1[i]=='0') g1.push_back(i); if(s2[i]=='0') g2.push_back(i); } nx[now]=n+1; if(g1.size()!=g2.size()) { puts("-1"); return 0; } for(int i=0;i<g1.size();++i) { ans+=abx(g1[i]-g2[i])!=0; } cout<<ans; return 0; }
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") #pragma GCC optimize(3 , "Ofast" , "inline") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <iostream> #include <cstdio> #include <algorithm> #include <unordered_map> #include <vector> #include <map> #include <list> #include <queue> #include <cstring> #include <cstdlib> #include <ctime> #include <cmath> #include <stack> #include <set> #include <bitset> #include <deque> using namespace std ; #define ios ios::sync_with_stdio(false) , cin.tie(0) #define x first #define y second #define pb push_back #define ls rt << 1 #define rs rt << 1 | 1 typedef long long ll ; const double esp = 1e-6 , pi = acos(-1) ; typedef pair<int , int> PII ; const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7; int n ; string a , b ; vector<int> va , vb ; int work() { cin >> n >> a >> b ; for(int i = 0 ;i < a.size() ;i ++ ) if(a[i] == '0') va.pb(i) ; for(int i = 0 ;i < b.size() ;i ++ ) if(b[i] == '0') vb.pb(i) ; if(va.size() != vb.size()) return puts("-1") , 0 ; int ans = 0 ; for(int i = 0 ;i < va.size() ;i ++ ) if(va[i] != vb[i]) ans ++ ; cout << ans << "\n" ; return 0 ; } int main() { // freopen("C://Users//spnooyseed//Desktop//in.txt" , "r" , stdin) ; // freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ; work() ; return 0 ; } /* */
#pragma GCC optimize "trapv" #include<iostream> #include <bits/stdc++.h> using namespace std; #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ll long long #define ull unsigned long long #define ui unsigned int #define vi vector<int> #define vll vector<ll> #define pb push_back #define ld long double //#define mp make_pair #define pii pair<int,int> #define mod 1000000007 #define rep(i,n) for(int i=0;i<n;i++) #define repp(i,a,n) for(int i=a;i<n;i++) #define all(v) v.begin(),v.end() #define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1] vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return move(v); } template<typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; s << "["; for(int i = 0; i < n - 1; i++) s << arr[i] << ","; s << arr[n - 1] << "]"; return s.str(); } #define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);} inline void __evars_begin(int line) { cerr << "#" << line << ": "; } template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); } template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); } template<typename T> inline void __evars_out_var(T val) { cerr << val; } inline void __evars(vector<string>::iterator it) { cerr << endl; } template<typename T, typename... Args> inline void __evars(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << "="; __evars_out_var(a); cerr << "; "; __evars(++it, args...); } template<typename T_vector> void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n'); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fio; clock_t clk = clock(); int t = 1; // cin >> t; while(t--) { string s; cin >> s; ll n = (ll)s.size(); bool ok = true; for(int i = 0; i < n; i++) { if(i % 2 == 0) ok &= (s[i] >= 'a' && s[i] <= 'z'); else ok &= (s[i] >= 'A' && s[i] <= 'Z'); } cout << (ok ? "Yes" : "No") << endl; } cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n'; }
#include <bits/stdc++.h> using namespace std; #define int long long const int N=2e5+5; int mod=1e9+7; int gcd(int x,int y) { if(y==0) { return x; } return gcd(y,x%y); } int power(int a,int n) { int res=1; while(n) { if(n%2) { res=(res*a)%mod; } n=n/2; a=(a*a)%mod; } return res; } void solve() { int i,j,n,total=0,ans=1e8; cin>>n; vector<int> arr; arr.assign(n,0); for(i=0;i<n;i++) { cin>>arr[i]; total=total+arr[i]; } int dp[n+1][total+1]; for(i=0;i<=n;i++) { for(j=0;j<=total;j++) { dp[i][j]=0; } } dp[0][0]=1; for(i=1;i<=n;i++) { for(j=1;j<=total;j++) { if(j<arr[i-1]) { dp[i][j]=dp[i-1][j]; } else { dp[i][j]=dp[i-1][j]+dp[i-1][j-arr[i-1]]; } } } for(j=1;j<total;j++) { if(dp[n][j]) { ans=min(ans,max(j,total-j)); } } if(n==1) { cout<<arr[0]; return; } cout<<ans; } int32_t main() { int t=1; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double d; typedef long double ld; inline int read() { register int s=0,f=0; register char ch=getchar(); while(!isdigit(ch)) f|=(ch=='-'),ch=getchar(); while(isdigit(ch)) s=(s<<1)+(s<<3)+(ch^48),ch=getchar(); return f?-s:s; } inline void write(int x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int a[5005][305]; char ch[5] = {' ', 'A', 'T', 'G', 'C'}; signed main() { int n; string s; cin >> n >> s; int ans = 0; s = ' ' + s; for (int i = 1; i <= n; i ++) { for (int j = 1; j <= 4; j ++) { a[i][ch[j]] = a[i - 1][ch[j]] + (s[i] == ch[j]); } } for (int i = 1; i <= n; i ++) { for (int j = 2; j <= (n - i) + 1; j += 2) { string t = s.substr (i, j); int sa; int st; int sg; int sc; sa = a[i + j - 1]['A'] - a[i - 1]['A']; st = a[i + j - 1]['T'] - a[i - 1]['T']; sg = a[i + j - 1]['G'] - a[i - 1]['G']; sc = a[i + j - 1]['C'] - a[i - 1]['C']; if (sa == st && sg == sc) ans ++; } } cout << ans << endl; return 0; }
#include <iostream> #include <iomanip> #include <vector> #include <array> #include <stack> #include <queue> #include <tuple> #include <algorithm> #include <unordered_set> #include <cmath> using ull = unsigned long long; using ll = long long; #define REP(i, n) for(ll i=0;i<(ll)n;i++) #define REPab(i,a,b)for(ll i=(ll)a;i<(ll)b;i++) constexpr bool bitp(ull bits, ull pos) { return bits & (1ULL << pos); } int main() { ll N; std::cin >> N; std::vector<ull> A(N); REP(i, N) std::cin >> A[i]; ull max = 1ULL << (N - 1); ull score = std::numeric_limits<ull>::max(); for (ull b = 0; b < max; b++) { ull bOR = A[0]; ull bXOR = 0; for (ull t = 0; t < N - 1; t++) { if (bitp(b, t)) { bXOR ^= bOR; bOR = 0; } bOR |= A[t + 1]; } bXOR ^= bOR; score = std::min(score, bXOR); } std::cout << score << "\n"; }
#include <bits/stdc++.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <list> #define eps 10e-14 //cout<<fixed<<setprecision(15)<< //s=to_string(100); //vector<vector<char>>c(h,vector<char>(w)); //next_permutation(orig.begin(),orig.end()); using namespace std; int gcd(int x,int y){ while(y>0){ int r=x%y; x=y; y=r; } return x; } int lcm(int a,int b){ int r=gcd(a,b); return a*b/r; } template<typename T, typename U> void primeFactorization(map<T, U> &primes, T N) { T n=N; U num=0; while(n%2==0){ num++; n/=2; } if(num!=0){ primes.emplace(2,num); } for(T i=3;i*i<=N;i+=2) { num=0; while(n%i==0){ num++; n/=i; } if(num!=0){ primes.emplace(i, num); } } if(n!=1){ primes.emplace(n,1); } } template<typename TypeInt> string Itoa(const TypeInt v, int base){ static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string ret; static numeric_limits<TypeInt> t; TypeInt n = v; if (t.is_signed) { if (v < 0) n *= -1; } while (n >= base) { ret += table[n%base]; n /= base; } ret += table[n]; if (t.is_signed) { if (v < 0 && base == 10) ret += '-'; } // 文字列の順番を逆にする std::reverse(ret.begin(), ret.end()); return ret; } int hanoi(int n){ return pow(2,n)-1; } int main() { long long n,k,count=0; cin>>n; vector<long long>a(n),b(n); map<long long,long long>m; for(int i=0;i<n;i++){ cin>>a.at(i); m[a.at(i)%200]++; } for(auto t:m){ if(t.second>=2){ for(int i=1;i<t.second;i++)count+=i; } } cout<<count<<endl; }
#include <iostream> #include <vector> #include <set> using namespace std; typedef long long ll; const ll INF = 1e18 + 2000; ll mdist (const set<pair<ll, int>> &A, const set<pair<ll, int>> &B) { ll ans = INF; for (auto it = A.begin(); it != A.end(); it++) { auto oth = B.lower_bound(*it); if (oth != B.end()) { ans = min(ans, oth->first - it->first); } if (oth != B.begin()) { ans = min(ans, it->first - prev(oth)->first); } } return ans; } int main () { int n; cin >> n; n *= 2; set<pair<ll, int>> rs, gs, bs; for (int i = 0; i < n; i++) { ll a; char c; cin >> a >> c; auto pr = make_pair(a, i); switch(c) { case 'R': rs.insert(pr); break; case 'G': gs.insert(pr); break; case 'B': bs.insert(pr); break; } } if ((int) rs.size() % 2 == 0 && (int) gs.size() % 2 == 0 && (int) bs.size() % 2 == 0) { cout << 0 << endl; return 0; } if (bs.size() % 2 == 0) { swap(bs, gs); } if (rs.size() % 2 == 0) { swap(rs, gs); } cout << min(mdist(rs, bs), mdist(rs, gs) + mdist(bs, gs)) << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) void check(long long &a, vector<int> &v, vector<int> &c, int i, int &lc){ if(c.at(i) == lc){ a++; return; } if(c.at(i) != -1){ return; } c.at(i) = lc; check(a, v, c, v.at(i), lc); } int main(){ int N; long long a = 0; cin >> N; vector<int> v(N); rep(i, N){ cin >> v.at(i); v.at(i)--; } vector<int> c(N, -1); for(int i = 0; i < N; i++){ if(c.at(i) == -1){ check(a, v, c, i, i); } } long long ans = 1; rep(i, a){ ans *= 2; ans %= 998244353; } cout << ans - 1 << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i] #define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep_up(i, a, n) for (ll i = a; i < n; ++i) #define rep_down(i, a, n) for (ll i = a; i >= n; --i) #define P pair<ll, ll> #define all(v) v.begin(), v.end() #define fi first #define se second #define vvvll vector<vector<vector<ll>>> #define vvll vector<vector<ll>> #define vll vector<ll> #define pqll priority_queue<ll> #define pqllg priority_queue<ll, vector<ll>, greater<ll>> constexpr ll INF = (1ll << 60); // constexpr ll mod = 1000000007; constexpr ll mod = 998244353; constexpr double pi = 3.14159265358979323846; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> void pt(T val) { cout << val << "\n"; } template <typename T> void pt_vll(vector<T> &v) { ll vs = v.size(); rep(i, vs) { cout << v[i]; if (i == vs - 1) cout << "\n"; else cout << " "; } } ll mypow(ll a, ll n) { ll ret = 1; if(n==0) return 1; if(a==0) return 0; rep(i, n) { if (ret > (ll)(1e18 + 10) / a) return -1; ret *= a; } return ret; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll dig(ll n) {//桁数 ll res = 0; while(n > 0) { res ++; n /= 10; } return res; } vector<bool> seen; void dfs(vvll &G, ll v){ seen[v]=true; for(auto next:G[v]){ if(seen[next]) continue; dfs(G,next); } } int main() { ll n,x,ans=1; cin>>n; vvll G(n); vll a(n); rep(i,n){ cin>>x; x--; G[i].push_back(x); G[x].push_back(i); } seen.assign(n,false); rep(i,n){ if(seen[i]) continue; dfs(G,i); ans*=2; ans%=mod; } cout<<(mod+ans-1)%mod<<endl; }
#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <math.h> #include <stdio.h> #include <string.h> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <functional> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <bitset> #include <complex> using namespace std; //#pragma GCC optimize("Ofast") //#pragma GCC optimization("unroll-loops, no-stack-protector") //#pragma GCC target("avx,avx2,fma") int main() { ios::sync_with_stdio(false); cin.tie(0); long long k, i, j, ans, x, s; cin >> k; vector<long long> v(k + 10, 0); // # of ways to get AB <= x for (i = 1; i <= k; i++) { for (j = 1; (i * j) <= k; j++) v[i * j]++; } for (i = 1; i <= k; i++) v[i] += v[i - 1]; ans = 0; for (i = 1; i <= k; i++) ans += v[k / i]; cout << ans << "\n"; return 0; }
#include<bits/stdc++.h> #define int long long #define ll long long #define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define fs first #define sd second #define rept(x,n) for(int i=x;i<n;i++) #define rrept(n,x) for(int i=n-1;i>=x;i--) #define all(v) v.begin(),v.end() #define mpit map<int,int>::iterator #define stit set<int,int>::iterator #define quit queue<int,int>::iterator #define maxt(a,b,c) max(a,max(b,c)) #define mint(a,b,c) min(a,min(b,c)) #define rsort(v) sort(v.rbegin(),v.rend()) #define pb push_back #define prnt(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "; using namespace std; void solve() { ll m; cin>>m; ll cnt = 0; for(ll i = 1;;i++) { if(i>m) break; for(ll j = 1;;j++) { if(i*j>m) break; for(ll k = 1;;k++) { if(i*j*k>m) { break ; } else cnt++; } } } cout<<cnt<<"\n"; } signed main() { speed; int t=1; //cin>>t; while(t--) { solve(); } return 0; }
#include <iostream> #include<vector> #include<algorithm> #include<string> #include<map> #include<set> #include<stack> #include<queue> #include<math.h> using namespace std; typedef long long ll; #define int long long #define double long double typedef vector<int> VI; typedef pair<int, int> pii; typedef vector<pii> VP; typedef vector<string> VS; typedef priority_queue<int> PQ; template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i<n;i++) #define eREP(i,n) for(int i=0;i<=n;i++) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define eFOR(i,a,b) for(int i=(a);i<=(b);++i) #define SORT(c) sort((c).begin(),(c).end()) #define rSORT(c) sort((c).rbegin(),(c).rend()) #define LB(x,a) lower_bound((x).begin(),(x).end(),(a)) #define UB(x,a) upper_bound((x).begin(),(x).end(),(a)) #define PRINT(a) printf("%.15Lf\n",a); #define YESNO(a) cout << (a ? "YES" : "NO") << endl #define YesNo(a) cout << (a ? "Yes" : "No") << endl #define yesno(a) cout << (a ? "yes" : "no") << endl #define INF 1000000000 #define LLINF 9223372036854775807 #define mod 1000000007 #define eps 1e-12 //priority_queue<int,vector<int>, greater<int> > q2; int dp[200010][7]; int ten[200010]; int N; string S, X; int solve(int now, int m) { //cout << now << " " << m << endl; if (dp[now][m] != 0)return dp[now][m]; if (now == N) { if (m == 0)return 1; else return 2; } else { int ans1 = solve(now + 1, m); int ans2 = solve(now + 1, (m + (S[now] - '0')*ten[N - now - 1]) % 7); //cout << now << " " << m << " " << ans1 << " " << ans2 <<" "<< S[now]<< endl; if (ans1 == 1 && ans2 == 1) { dp[now][m] = 1; return 1; } else if (ans1 == 2 && ans2 == 2) { dp[now][m] = 2; return 2; } else if (X[now] == 'T'){ dp[now][m] = 1; return 1; } else{ dp[now][m] = 2; return 2; } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; REP(i, N) { if (i == 0)ten[i] = 1; else { ten[i] = (ten[i - 1] * 10) % 7; } } cin >> S >> X; int ans = solve(0, 0); if (ans == 1) { cout << "Takahashi" << endl; } else { cout << "Aoki" << endl; } return 0; }
#include <iostream> #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 <cmath> using namespace std; const int INF = 2000000000; int N,M,Q; int main() { // Step #1. Input cin >> N; vector<int> xa(N+1); vector<int> ya(N+1); for (int i = 1; i <= N; i++){ cin >> xa[i] >> ya[i]; } cin >> M; vector<vector<int64_t> > X(M+1,vector<int64_t>(4)); vector<vector<int64_t> > Y(M+1,vector<int64_t>(4)); /* for (int i = 1; i <= N; i++){ X[0][i]=xa[i]; Y[0][i]=ya[i]; } */ X[0][1]=0; Y[0][1]=0; X[0][2]=1; Y[0][2]=0; X[0][3]=0; Y[0][3]=1; for(int i=1;i<=M;i++){ int a; int64_t b; cin >> a; if(a>2){ cin >> b; } for(int j=1;j<=3;j++){ int64_t x=X[i-1][j]; int64_t y=Y[i-1][j]; if(a==1){ X[i][j]=y; Y[i][j]=0-x; }else if(a==2){ X[i][j]=0-y; Y[i][j]=x; }else if(a==3){ X[i][j]=2*b-x; Y[i][j]=y; }else{ X[i][j]=x; Y[i][j]=2*b-y; } } } cin >> Q; for(int i=1;i<=Q;i++){ int a,b; cin >> a >> b; int64_t x_x=X[a][2]-X[a][1]; int64_t x_y=Y[a][2]-Y[a][1]; int64_t y_x=X[a][3]-X[a][1]; int64_t y_y=Y[a][3]-Y[a][1]; cout << X[a][1]+x_x*xa[b]+y_x*ya[b] << " " << Y[a][1]+x_y*xa[b]+y_y*ya[b] << endl; //cout << X[a][b] << " " << Y[a][b] << endl; } /* for(int i=1;i<=3;i++){ cout << X[M][i] << " " << Y[M][i] << endl; } */ //cout << sum << endl; }
#include<iostream> #include<iomanip> #include<string> #include<vector> #include<algorithm> #include<utility> #include<tuple> #include<map> #include<queue> #include<deque> #include<set> #include<stack> #include<numeric> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> using namespace std; struct Edge { int to; long long weight; Edge() : to(0), weight(0) {} Edge(int to, long long weight) : to(to), weight(weight) {} Edge(const Edge& e) { to = e.to; weight = e.weight; } }; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using Graph = vector<vector<int>>; using weightedGraph = vector<vector<Edge>>; using heap = priority_queue<int, vector<int>, greater<int>>; #define BIL ((ll)1e9) #define MOD ((ll)1e9+7) #define INF (1LL<<60) #define inf (1<<29) int main(int argc,char* argv[]){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int n; cin >> n; vector<int> ans(n+1, 2); ans[1] = 1; for(int i=2;i<n+1;i++) { for(int j=2;i*j<n+1;j++) { if(ans[i*j] != ans[i]) continue; ans[i*j]++; } } for(int i=1;i<n+1;i++) { cout << ans[i]; if(i != n) cout << " "; } cout << 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 //#pragma GCC optimize("Ofast") //#pragma comment(linker, "/stack:200000000") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4") #define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout); #define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define all(x) x.begin(), x.end() #define sz(s) (int)s.size() #define pb push_back #define ppb pop_back #define mp make_pair #define s second #define f first typedef pair < long long, long long > pll; typedef pair < int, int > pii; typedef unsigned long long ull; typedef vector < pii > vpii; typedef vector < int > vi; typedef long double ldb; typedef long long ll; typedef double db; typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set; const int inf = 1e9, maxn = 2e5 + 48, mod = 1e9 + 7, N = 1e5 + 512; const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 300; const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9); const db eps = 1e-12, pi = acos(-1); const ll INF = 1e18; int n; main () { cin >> n; cout << setprecision(8) << fixed; db ans = 0; for (int i = 1; i < n; ++i) ans += (n + 0.0) / i; cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; int a[N],b[N]; bool visited[N]; vector<int>adj[N]; long long add,sub; void DFS(int cur) { if(visited[cur])return; visited[cur]=1; if(a[cur] > b[cur])sub+=a[cur]-b[cur]; else add+=b[cur]-a[cur]; for(int it : adj[cur])DFS(it); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int i,m,n,x,y; cin >> n >> m; for(i=1;i<=n;i++)cin >> a[i]; for(i=1;i<=n;i++)cin >> b[i]; while(m--) { cin >> x >> y; adj[x].emplace_back(y); adj[y].emplace_back(x); } for(i=1;i<=n;i++) { if(!visited[i]) { add=sub=0; DFS(i); if(add != sub) { cout << "No"; return 0; } } } cout << "Yes"; return 0; }
//#undef DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; const ll llinf = (1ll<<61)-1; const char lf = '\n', splf[] = " \n"; #define sz(a) int(a.size()) #define all(x) begin(x), end(x) #define TCC template<class C #define OSO ostream &operator<<(ostream &os, const #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 #define Pair make_pair class DSU//{ { vector<int> dsupar; public: int cntDSU; vector<int> siz; void reset(int nd) { for (int i = 0; i <= nd; i++) dsupar[i] = i, siz[i] = 1; cntDSU = nd; } DSU(int N, int noOfSets = -1) { dsupar.resize(N+5), siz.resize(N+5); reset(N+4); cntDSU = noOfSets==-1 ? N : noOfSets; } int find(int x) { return dsupar[x]==x ? x : dsupar[x] = find(dsupar[x]); } void union_(int u, int v) { if ((u = find(u))==(v = find(v))) return; if (siz[u]<siz[v]) swap(u, v); dsupar[v] = u, siz[u] += siz[v], siz[v] = 0; cntDSU--; } bool same(int u, int v) { return find(u)==find(v); } int size(int u) { return siz[find(u)]; } vector<vector<int>> groups(int mnNode, int mxNode) { vector<vector<int>> ret(mxNode+1); for (int i = mnNode; i <= mxNode; ++i) { ret[find(i)].push_back(i); } ret.erase( remove_if(ret.begin(), ret.end(), [](const vector<int> &v) { return v.empty(); }), ret.end()); return ret; } };//} #define yes cout << "Yes\n" #define no cout << "No\n" const int inf = 1000000007,N=200005; int TC=1, CN,n,m,a[N],b[N]; ll sa[N],sb[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>>m; DSU dsu(n); for(int i=1;i<=n;++i)cin>>a[i]; for(int i=1;i<=n;++i)cin>>b[i]; for(int i=0;i<m;++i){ int u,v; cin>>u>>v; dsu.union_(u,v); } for(int i=1;i<=n;++i){ int p=dsu.find(i); sa[p]+=a[i],sb[p]+=b[i]; } for(int i=1;i<=n;++i){ if(sa[i]!=sb[i]){no;return;} } yes; }; while (CN++!=TC) kase(); }
#include "bits/stdc++.h" #pragma GCC optimize "03" using namespace std; #define int long long int #define double long double #define pb push_back #define pii pair<int,int> #define fi first #define se second #define rep(i,a,b) for (int i = a; i < b; ++i) #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef LOCAL #define dbg(...) ; #define endl '\n' #endif const int inf = 1e15; const int MOD = 1e9 + 7; const int N = 2e5 + 5; signed main(){ IOS; int a, b; cin >> a >> b; if (b >= a) { int s = 0; vector<int> v; for (int i = 1; i <= b; i++) { v.push_back(-i); s -= i; } for (int i = 1; i < a; i++) { v.push_back(i); s += i; } s = -s; for (auto i : v) cout << i << " "; cout << s << endl; } else { int s = 0; vector<int> v; for (int i = 1; i <= a; i++) { v.push_back(i); s += i; } for (int i = 1; i < b; i++) { v.push_back(-i); s -= i; } s = -s; for (auto i : v) cout << i << " "; cout << s << 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; struct Lucas { vector<vector<long long>> table; long long p; Lucas(long long _p) : p(_p) { init(p); } void init(long long p) { table.assign(p, vector<long long> (p)); table[0][0] = 1; for(int i = 1; i < p; i++) { table[i][0] = 1; for(int j = i; j > 0; j--) { table[i][j] = (table[i - 1][j - 1] + table[i - 1][j]) % p; } } } long long comb(long long n, long long k) { long long ret = 1; while(n > 0) { ret *= table[n % p][k % p]; ret %= p; n /= p; k /= p; } return ret; } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); Lucas lu(3); int n,B=0,W=1,R=2; string s; cin >> n >> s; vector<int> a(n); rep(i,n){ if(s[i] == 'B') a[i] = B; if(s[i] == 'W') a[i] = W; if(s[i] == 'R') a[i] = R; } int ans = 0; rep(i,n) ans = (ans + lu.comb(n - 1, i) * a[i]) % 3; if(n % 2 == 0) ans = (- ans + 3) % 3; if(ans == B) cout << 'B'; if(ans == W) cout << 'W'; if(ans == R) cout << 'R'; cout << endl; }
#include<bits/stdc++.h> using namespace std; //#include <atcoder/scc> //using namespace atcoder; //using mint = modint998244353; //using mint = modint1000000007; #pragma region template using ll = long long; using PII = pair<int, int>; using PLL = pair<ll, ll>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define overload4(_1,_2,_3,_4,name,...) name #define overload3(_1,_2,_3,name,...) name #define rep1(n) for(ll i=0;i<n;++i) #define rep2(i,n) for(ll i=0;i<n;++i) #define rep3(i,a,b) for(ll i=a;i<b;++i) #define rep4(i,a,b,c) for(ll i=a;i<b;i+=c) #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__) #define rrep1(n) for(ll i=n;i--;) #define rrep2(i,n) for(ll i=n;i--;) #define rrep3(i,a,b) for(ll i=b;i-->(a);) #define rrep(...) overload3(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define fi first #define se second #define debug(var) do{std::cerr << #var << " : ";view(var);}while(0) template<typename T> void view(T e){std::cerr << e << std::endl;} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){cerr << endl;int cnt = 0;for(const auto& v : vv){cerr << cnt << "th : "; view(v); cnt++;} cerr << endl;} std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } template<class T, class K>bool chmax(T &a, const K b) { if (a<b) { a=b; return 1; } return 0; } template<class T, class K>bool chmin(T &a, const K b) { if (b<a) { a=b; return 1; } return 0; } const int inf = 1001001001; const ll INF = 1001001001001001001ll; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; ll rddiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // -20 / 3 == -7 ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;} ll modpow(ll a, ll p, ll mod){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;} ll rudiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // 20 / 3 == 7 #pragma endregion // const double pi = acos(-1); //const ll mod = 998244353; const ll mod = 1000000007; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); //cout << fixed << setprecision(20); int n, m; cin >> n >> m; int ans = 0; VV<int> G(n); rep(i,m){ int a, b; cin >> a >> b; --a, --b; G[a].pb(b); } rep(i,n){ queue<int> que; V<bool> seen(n); que.push(i); while(que.size()){ int v = que.front(); que.pop(); if(seen[v]) continue; seen[v] = true; ans++; for(int nv : G[v]){ if(!seen[nv]) que.push(nv); } } } cout << ans << endl; }
#include<iostream> using namespace std; #include<bits/stdc++.h> typedef long long int ll; void dfs(int i,vector<vector<int>>&adj,int vis[]){ vis[i]=1; for(int child:adj[i]){ if(vis[child]==0) dfs(child,adj,vis); } } int main(){ int n,m; cin>>n>>m; vector<vector<int>>adj(n+1); int u,v; for(int i=1;i<=m;i++){ cin>>u>>v; adj[u].push_back(v); } ll cnt=0; for(int i=1;i<=n;i++){ int vis[n+1]; for(int j=1;j<=n;j++) vis[j]=0; dfs(i,adj,vis); for(int j=1;j<=n;j++){ if(vis[j]==1) cnt++; } } cout<<cnt<<endl; }
//BY: YASH JAIN, CF: BitSane #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; #define May_the_fork_be_with_you ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define rall(v) v.rbegin(),v.rend() #define forn(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define deb(x) cout<<#x<<": "<<x<<endl; #define debn(x) cout<<#x<<": "<<x<<" "; #define MOD 1000000007 #define mod 998244353 #define INF 1e18 #define ll long long #define f first #define s second #define pb push_back #define eb emplace_back #define endl "\n" #define int long long #define N 100005 #define sq(x) ((x)*(x)) typedef vector<int> vi; ll gcd(ll a, ll b) {if (!b)return a; return gcd(b, a % b);} ll power(ll x, ll y, ll p = INF) {ll res = 1; x %= p; while (y > 0) {if (y & 1)res = (res * x) % p; y = y >> 1; x = (x * x) % p;} return res;} // Do Not use power when calculating powers of 2 (its inefficient) void solve() { int n, k; cin >> n >> k; int res = 0; res += n * (k * (k + 1)) / 2; res += 100 * k * (n * (n + 1)) / 2; cout << res; } int32_t main() { #ifndef ONLINE_JUDGE // for geting input form input.txt freopen("input.txt", "r", stdin); // for wrting output to output.txt freopen("output.txt", "w", stdout); #endif May_the_fork_be_with_you int t = 1; int x = 1; // cin >> t; cout << fixed << setprecision(12); while (t--) { // cout << "Case #" << x << ": "; solve(); x++; } #ifndef ONLINE_JUDGE cerr << "Time Taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; #endif }
#include <iostream> using namespace std; int main() { int n,k; long long res=0; cin>>n>>k; for (int i=1;i<=n;i++) for (int j=1;j<=k;j++) { res+=100*i+j; } cout<<res; return 0; }
#include<iostream> using namespace std; int main(){ long long x,y,a,b; cin >> x >> y >> a >> b; long long ans=0; while((double)a*x<=2e18 && a*x<=x+b && a*x<y){ x*=a; ans++; } cout << ans+(y-1-x)/b << endl; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = (0); i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define limit(x,l,r) max(l,min(x,r)) #define lims(x,l,r) (x = max(l,min(x,r))) #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define show(x) cerr << #x << " = " << (x) << endl #define show2(x,y) cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl #define show3(x,y,z) cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << endl #define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ') #define showv2(v) rep(j,v.size()) showv(v[j]) #define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ') #define showt2(t,r,c) rep(j,r) showt(t[j],c) #define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second) #define printv(v) rep(i,v.size()) printf("%d\n", v[i]) #define printt(t,n) rep(i,n) printf("%d\n", t[i]) #define incl(v,x) (find(all(v),x)!=v.end()) #define incls(s,c) (s.find(c)!=string::npos) #define lb(a,x) distance((a).begin(),lower_bound(all(a),(x))) #define ub(a,x) distance((a).begin(),upper_bound(all(a),(x))) #define fi first #define se second #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define bit(n,k) ((n>>k)&1) // nのk bit目 #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") #define uni(x) x.erase(unique(all(x)),x.end()) #define SP << " " << using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vc = vector<char>; using vvc = vector<vc>; using vs = vector<string>; using vb = vector<bool>; using vvb = vector<vb>; using P = pair<int, int>; using T = tuple<int, int, int>; using vp = vector<P>; using vt = vector<T>; //const int mod = 1000000007; const double EPS = 1e-9; //const long double EPS = 1e-14; const int INF = (1<<30)-1; const ll LINF = (1LL<<62)-1; #define dame { puts("No"); return 0;} #define yn {puts("Yes");}else{puts("No");} #define YN {puts("YES");}else{puts("NO");} inline int in() { int x; cin >> x; return x;} inline ll lin() { ll x; cin >> x; return x;} inline char chin() { char x; cin >> x; return x;} inline string stin() { string x; cin >> x; return x;} inline double din() { double x; cin >> x; return x;} //template<class T = int> inline T in() { T x; cin >> x; return (x);} template<typename T>inline ll suma(const vector<T>& a) { ll res(0); for (auto&& x : a) res += x; return res;} template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } char itoa(int n) { return n + '0';} ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} const int dy[4] = {1, 0, -1, 0}; const int dx[4] = {0, 1, 0, -1}; int main () { ll x, y, a, b; cin >> x >> y >> a >> b; ll t = b/(a-1); ll now = x; ll ans = 0; while (now < y) { if (now <= t) { now *= a; ans++; } else { ans += (y-1-now)/b; break; } } if (now >= y) ans--; cout << ans << endl; return 0; }
// 解き直し. // https://atcoder.jp/contests/abc191/editorial/610 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using LL = long long; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define pb push_back const LL INF = 202020202020202020; struct edge{ int n; // 次の頂点. LL c; // 移動時間. bool operator < (const edge &E) const{ return c > E.c; } }; LL d[2020][2020]; // i -> j の 移動時間. LL ds[2020]; // i -> i の 移動時間. LL ans[2020]; // 出力用. vector<edge> G[2020]; int main(){ // 1. 入力情報. int N, M; scanf("%d %d", &N, &M); rep(i, N) rep(j, N) d[i][j] = INF; rep(i, N) ds[i] = INF; rep(i, M){ int a, b; LL c; scanf("%d %d %lld", &a, &b, &c); a--, b--; edge e; e.n = b; e.c = c; G[a].pb(e); // a -> b. if(a == b) ds[a] = min(ds[a], c); // a -> a. (random_10.txt の WA回避?) } // 2. ダイクストラ法. // https://ja.wikipedia.org/wiki/ダイクストラ法 auto dijkstra = [&](int s){ // 1. 初期化. priority_queue<edge> pq; // 2. 始点設定. d[s][s] = 0; pq.push({s, 0LL}); // 3. キュー pq が 空になるまで, 探索. while(!pq.empty()){ // 4. 探索を継続するために, 頂点を取得. edge e = pq.top(); pq.pop(); int cn = e.n; LL cc = e.c; // 4. 各隣接する頂点について, 距離を確認し, 移動時間を更新. for(auto &g : G[cn]){ int nn = g.n; LL nc = cc + g.c; // 5. 移動時間更新. if(d[s][nn] > nc) d[s][nn] = nc, pq.push({nn, nc}); } } return; }; // 3. 各頂点に対して, 最短経路を求める. rep(i, N) dijkstra(i); // 4. 出力用の情報を作成. rep(i, N){ ans[i] = ds[i]; rep(j, N) if(i != j) ans[i] = min(ans[i], d[i][j] + d[j][i]); } rep(i, N) if(ans[i] >= INF) ans[i] = -1; // 5. 出力. rep(i, N) printf("%lld\n", ans[i]); return 0; }
#include<bits/stdc++.h> using namespace std; #define endline '\n' #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define pb push_back #define fr(i,a,b) for(int i=a;i<(int)b;i++) #define rfr(i,a,b) for(int i=a;i>=(int)b;i--) #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define INF 0x3f3f3f3f #define debug(x) cerr<<x<<endline #define PI 3.14159265358979323846 typedef long long int ll; const int N = 4e5+10; const ll mod=1e9+7; clock_t startTime; double getCurrenttime() { return (double) (clock() - startTime) / CLOCKS_PER_SEC; } bool comp(pi a,pi b) { if(a.first==b.first)return a.second<b.second; return a.first<b.first; } void solve() { vector<pair<ll,pll>> v; int n;cin>>n; fr(i,0,n) { ll x,y,z; cin>>x>>y>>z; v.pb({x,{y,z}}); } int c=0; sort(all(v)); fr(i,0,n) { fr(j,i+1,n) { double a[]={0,0,0.00001,0.00001}; double b[]={0,-0.00001,0,-0.00001}; int f1=v[i].first;int f2=v[j].first; long double x1=0,y1=0,x2=0,y2=0; fr(k,0,4) { if(f1==k+1) { x1=v[i].second.first+a[k],y1=v[i].second.second+b[k]; } } fr(k,0,4) { if(f2==k+1) { x2=v[j].second.first+a[k],y2=v[j].second.second+b[k]; } } if(x1<=x2 and x2<=y1)c++; else if(x2<=x1 and x1<=y2)c++; else if(x1<=y2 and y2<=y1)c++; else if(x2<=y1 and y1<=y2)c++; } } cout<<c<<endline; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); #ifndef ONLINE_JUDGE startTime=clock(); freopen("input.txt","r",stdin); freopen("output1.txt","w",stdout); // freopen("debug.txt", "w", stderr); #endif int t=1; // cin>>t; while(t--) { solve(); } #ifndef ONLINE_JUDGE cout << "Time: " << getCurrenttime() * 1000 << " ms" << endline; #endif }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);++i) #define repA(i,k,n) for(int i=k;i<=(n);++i) #define repD(i,k,n) for(int i=k;i>=(n);--i) #define endl '\n' #define print(b) for(auto a:b) cout<<a<<' '; #define printN(b) for(auto a:b) cout<<a<<endl; #define fin freopen("C:\\Users\\Nazmul Rahul\\Desktop\\in.txt", "r++", stdin) #define fout freopen("C:\\Users\\Nazmul Rahul\\Desktop\\out.txt", "w++", stdout) #define fastio ios::sync_with_stdio(false) #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define pb push_back #define pob pop_back #define ll long long int #define MAX 2*100001 std::vector<ll> total(MAX); using namespace std; int main() { #ifndef ONLINE_JUDGE fin; fout; #endif ll n, w; ll s, t, p; cin >> n >> w; for (ll i = 0; i < n; i++) { cin >> s >> t >> p; total[s] += p; total[t] -= p; } for (ll i = 1; i < MAX; i++) { total[i] += total[i - 1]; } // cout << total[2] << endl; for (int i = 0; i < MAX; i++) { if (total[i] > w) { cout << "No" << endl; return 0; } } cout << "Yes"; return 0; }
//FEARLESS //Fear is the path to the dark side. //Fear leads to anger. Anger leads to hate. //Hate leads to suffering. - Master Yoda // Templates // 1. tempgeo.cpp // 2. tempunion.cpp #include <bits/stdc++.h> using namespace std; #define ll int64_t #define pb push_back #define pii pair <int, int> #define pll pair <long long, long long> #define endl '\n' #define PI (2.0 * acos(0.0)) #define testcase ll T; cin >> T; for (ll tc = 1; tc <= T; tc++) #define info pair<int, pii> #define mkp make_pair #define MAXN 1000002 const long long M = 1e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7; const long long MC = (1<<20) + 5; void precomp () { // precomputation here } void solve () { int n, s, d; cin >> n >> s >> d; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (x < s && y > d) { cout << "Yes" << endl; return; } } cout << "No" << endl; } int main () { //freopen("E:/newts/in.txt", "r", stdin); //freopen("E:/newts/out.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); precomp(); //testcase { solve (); //} return 0; }
#include<bits/stdc++.h> using namespace std; #define fast {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);} typedef long long int ll; typedef string S; #define M 1e18 #define AS 250005 #define sp cout<<' ' #define nw cout<<endl #define rt return #define __ template<typename T, typename... Types> void in() {rt;} __ void in(T &a, Types&... b){cin>>(a); in(b...);} void o() {rt;} __ void o(T a, Types... b){cout<<(a);sp; o(b...);} #define fr(Hi,a,n) for(ll Hi=a;Hi<=n;Hi++) #define frm(Hi,a,n) for(ll Hi=n;Hi>=a;Hi--) #define P pair<ll,ll> #define vc vector<ll> #define pb push_back #define MP map<ll,ll> bool sortin(const pair<ll,ll> &e,const pair<ll,ll> &f){return (e.first<f.first);} bool POT(ll x){return x && (!(x&(x-1)));} ll i,j,k,l,m,n,p,q,r,a,b,c,x,y,z,ts,mn=1e18,mod=1e9+7; ll ar[AS],br[AS],xr[AS],tem[AS]; int main() { fast; in(n,a,b); fr(i,0,n-1) { in(x,y); if(x<a and y>b)c=1; } if(c)o("Yes"); else o("No"); }
#include <bits/stdc++.h> using namespace std; int main() { int n,k,m,sum,z; sum=0; cin >> n >> k >> m; vector<int> A(n-1); for (int i=0;i<n-1;i++)cin >> A.at(i); for (int j=0;j<n-1;j++)sum+=A.at(j); if(sum+k<n*m)cout << "-1" <<endl; else{ z=n*m-sum; if(z<=0)cout << 0 <<endl; else cout << z <<endl; } }
#include <iostream> using namespace std; int main() { // your code goes here int a,b,c; cin>>a>>b>>c; if(a==b) cout<<c; else if(a==c) cout<<b; else if(b==c) cout<<a; else cout<<"0"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A,B,C; cin >> A >> B >>C; if(A*A+B*B<C*C) printf("Yes"); else printf("No"); }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.rbegin(), a.rend()); ll ans = 0; ll b = n - 1; rep(i, n) { ans += a[i] * b; b -= 2; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; long long a[n]; long long b[n] = {}; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + sizeof(a)/sizeof(a[0])); for(int i = 0; i < n-1; ++i) { if(i == 0){ b[n-1] = a[n-1]-a[n-2]; } else { b[n-1-i] = b[n-i] + (i + 1)*(a[n-1-i] - a[n-2-i]); } } long long ans = 0; for(int i = 1; i < n; ++i) { ans += b[i]; } cout << ans << "\n"; return 0; }
#include<bits/stdc++.h> #define pl(a) " [" << #a << ": " << (a) << "] " #define pts(a) " [" << #a << ": { first: " << (a.first) << ", second: " << (a.second) << "}] " #define all(vi) vi.begin(), vi.end() #define endl "\n" #define ll long long using namespace std; pair<int, int> n4[4] { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }; pair<int, int> n8[8] { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1} }; ll int h(ll int x1, ll int y1, ll int x2, ll int y2) { return abs(x2 - x1) + abs(y2 - y1); } ll int pow(ll int x, ll int y) { if (y == 0) return 1; if (y % 2 == 0) { ll int a = pow(x, y / 2); return a * a; } else { int a = pow(x, y / 2); return a * a * x; } return 0;} template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v); template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "[" << p.first << ", " << p.second << "]"; } template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";} template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } void set_in(string s) { if (s.length() > 0) { freopen ((s+".in").c_str(), "r", stdin); freopen ((s+".out").c_str(), "w", stdout); } } void solve() { ll int s; cin >> s; // s *= 2; set<ll int> fact; // find factors of 2 * n s *= 2; for (ll int i = 1; i * i <= s; i++) { if (s % i == 0) { fact.insert(i); fact.insert(s/i); // cerr << i << endl; } } s /= 2; ll int ans = 0; for (ll int n : fact) { ll int t = 2 * s / n; t -= (n - 1); if (t % 2 == 0) { ans++; // cout << pl(t / 2) << endl; } } cout << ans << endl; } int main() { #ifndef DEBUG set_in(""); #endif #ifdef DEBUG auto clock_start = chrono::high_resolution_clock::now(); cout << endl; #endif ios::sync_with_stdio(false); cin.tie(nullptr); solve(); // int T; cin >> T; // for (int t = 1; t <= T; t++) { // cout << "Case #" << t << ": "; // solve(); // } // Clock #ifdef DEBUG auto clock_end = chrono::high_resolution_clock::now(); cout << endl; chrono::duration<double> elapsed = clock_end - clock_start; cout << "Execution time: " << elapsed.count() << "s" << endl; #endif return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; vector<pair<long long, long long> > prime_factorize(long long N) { vector<pair<long long, long long> > res; for (long long a = 2; a * a <= N; ++a) { if (N % a != 0) continue; long long ex = 0; while (N % a == 0) { ++ex; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } int main(){ ll n; cin >> n; vector<pair<ll, ll> > res = prime_factorize(n); ll kis = 1; ll gus = 0; for(int i=0;i<res.size();i++){ if(res[i].first%2==0) gus+=res[i].second; else kis*=(res[i].second+1); } cout << kis*2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int n, q; cin >> n >>q; vector<long long> a(n), c(n); long long x; for (int i=0; i <n; i++){ cin >> x; if (i == 0){ a.at(i) = x; c.at(i) = x-1; } else{ a.at(i) = x; c.at(i) = c.at(i-1) + x - a.at(i-1) -1; } } vector<long long> k(q); for (auto& num : k){ cin >> num; } for (auto num : k){ if (c.at(n-1) <num){ cout << a.at(n-1) + (num-c.at(n-1)) << endl; } else{ auto index = lower_bound(c.begin(), c.end(),num) - c.begin(); cout << a.at(index)-1-c.at(index) + num <<endl; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,(n)-1,0) #define all(v) v.begin(), v.end() #define endk '\n' const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; const ll mod2 = 998244353; const ld eps = 1e-10; template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;} template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;} int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vector<ll> A(n); rep(i, n) cin >> A[i]; sort(all(A)); vector<ll> K(q); rep(i, q) cin >> K[i]; auto bsearch = [&](ll k) { auto test = [&](ll m) { return upper_bound(all(A), m) - begin(A) + k <= m; }; ll ok = longinf, ng = 0; while(abs(ok-ng) > 1) { ll mid = (ok+ng)/2; (test(mid) ? ok : ng) = mid; } return ok; }; rep(i, q) { cout << bsearch(K[i]) << endk; } return 0; }
#include<bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/detail/standard_policies.hpp> // using namespace __gnu_pbds; #pragma GCC optimize("O3") #ifdef LOCAL #include "/Users/lbjlc/Desktop/coding/debug_utils.h" #else #define print(...) ; #define printn(...) ; #define printg(...) ; #define fprint(...) ; #define fprintn(...) ; #endif #define rep(i, a, b) for(auto i = (a); i < (b); i++) #define rrep(i, a, b) for(auto i = (a); i > (b); i--) #define all(v) (v).begin(), (v).end() #define pb push_back // #define mp make_pair #define fi first #define se second #define maxi(x, y) x = max(x, y) #define mini(x, y) x = min(x, y) // long long fact(long long n) { if(!n) return 1; return n*fact(n-1); } // #define endl '\n' mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int get_random() { static uniform_int_distribution<int> dist(0, 1e9 + 6); return dist(rng); } #define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);} typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<pdd> vpdd; typedef vector<long long> vll; #define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val) template<class T> void make_unique(T & v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int geti() { int x; cin >> x; return x; } long long getll() { long long x; cin >> x; return x; } double getd() { double x; cin >> x; return x; } // pair<int, int> a(geti(), geti()) does not work // pair<int, int> a({geti(), geti()}) works, since it uses initializer. const int MAXN = 3e5 + 100; void solve(int tt) { // cout<<"Case #"<<tt<<": "; } int main(int argc, char * argv[]) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // solve_testcase; int a,b,x,y; cin>>a>>b>>x>>y; if(a==b) { cout<<x<<endl; } else if(a>b) { cout<<min(x+(a-b-1)*y,x*((a-b)*2-1))<<endl; } else if(a<b) { cout<<min(x+abs(a-b)*y,x*(abs(a-b)*2+1))<<endl; } return 0; }
/* after dusk passed, there is a starry sky. */ #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define m_k make_pair using namespace std; const int N=2*1e5+100; int n,a[N],b[N],p[N],vi[N],wh[N]; vector <pair<int,int> > ans; priority_queue <pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q; inline int read() { int f=1,x=0;char s=getchar(); while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();} while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();} return x*f; } signed main() { n=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<=n;i++) b[i]=read(); for (int i=1;i<=n;i++) p[i]=read(); for (int i=1;i<=n;i++) { if (p[i]==i){vi[i]=1;continue;} if (a[i]<=b[p[i]]) { printf("-1\n"); return 0; } q.push(m_k(a[i],i));wh[p[i]]=i; } while (!q.empty()) { int x=q.top().second,y;q.pop(); if (vi[x]) continue; vi[x]=1;y=wh[x]; ans.push_back(m_k(x,y)); wh[p[x]]=y; swap(p[x],p[y]); if (y==p[y]) vi[y]=1; } printf("%d\n",(int)ans.size()); for (int i=0;i<(int)ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second); }
#include <bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fod(i,a,b) for(int i=a;i>=b;i--) #define me0(a) memset(a,0,sizeof(a)) #define me1(a) memset(a,-1,sizeof(a)) #define op freopen("in.txt", "r", stdin) #define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin); #define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout) #define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout) #define pii pair<int,int> #define Please return #define AC 0 using namespace std; const int INF = 0x3f3f3f3f; typedef long long LL; template <class T> void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int mod=1e9+7; const int maxn = 1e6+10; int n,m,a[maxn],q,t; char s[maxn]; char AB[2],AA[2],BA[2],BB[2]; LL ksm(LL a, LL b) { LL res = 1; while(b){ if(b&1){ res = res*a%mod; } a=a*a%mod; b>>=1; } return res; } LL dp[maxn]; void solve(){ if(BB[0]=='B'){ puts("1");return ; } if(BA[0]=='A') { cout<<ksm(2,n-3)<<endl; } else { dp[1]=dp[0]=1; fo(i,2,n-2){ dp[i]=(dp[i-1]+dp[i-2])%mod; } printf("%lld\n",dp[n-2]); } } void solve2(){ if(AA[0]=='A'){ puts("1");return ; } if(BA[0]=='B'){ cout<<ksm(2,n-3)<<endl; } else { dp[1]=dp[0]=1; fo(i,2,n-2){ dp[i]=(dp[i-1]+dp[i-2])%mod; } printf("%lld\n",dp[n-2]); } } int main(){ // op; read(n); cin>>AA>>AB>>BA>>BB; if(n<=3){ puts("1");return 0; } if(AB[0]=='B'){ solve(); } else solve2(); Please AC; }
#include <bits/stdc++.h> #define fi first #define se second #define ll long long #define dl double long using namespace std; const int N = 1e4 + 7; const long long mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; string s = "AB"; map < char , map < char , char > > c; int main() { ios_base::sync_with_stdio(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); cin >> n; cin >> c['A']['A']; cin >> c['A']['B']; cin >> c['B']['A']; cin >> c['B']['B']; vector < int > d(N , 1) , f( N , 0 ) , ff( N , 0 ); ff[2] = ff[3] = 1; f[2] = f[3] = 1; for( int i = 4; i <= n; i++ ){ ff[i] = ff[i - 1] + ff[i - 2]; f[i] = f[i - 1] * 2; ff[i] %= mod; f[i] %= mod; } for( int nn = 2; nn <= 10; nn++ ){ set < string > t; t.insert(s); for( int it = 3; it <= nn; it++ ){ set < string > f; for( auto x : t ){ for( int i = 0; i + 1 < (int)x.size(); i++ ){ string res = ""; for( int j = 0; j <= i; j++ ){ res += x[j]; } res += c[x[i]][x[i + 1]]; for( int j = i + 1; j < (int)x.size(); j++ ){ res += x[j]; } f.insert(res); } } t = f; } d[nn] = (int)t.size(); } if( n <= 10 || d[10] == 1 )cout << d[n]; else if( d[10] == d[9] + d[8] ){ cout << ff[n]; }else cout << f[n]; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; int main() { int h,w,n,m; cin >> h >> w >> n >> m; vector<int> a(n), b(n); vector<vector<int>> done(h, vector<int>(w,0)); rep(i,n){ cin >> a[i] >> b[i]; a[i]--; b[i]--; } rep(i,m){ int c, d; cin >> c >> d; c--; d--; done[c][d]--; } rep(i,n){ if(!done[a[i]][b[i]]){ int x = a[i]; while(x >= 0 && done[x][b[i]] != -1) { done[x][b[i]] = 1; x--; } x = a[i]; while(x < h && done[x][b[i]] != -1) { done[x][b[i]] = 1; x++; } } } rep(i,n){ if(done[a[i]][b[i]] != 2){ int y = b[i]; while(y >= 0 && done[a[i]][y] != -1) { done[a[i]][y] = 2; y--; } y = b[i]; while(y < w && done[a[i]][y] != -1) { done[a[i]][y] = 2; y++; } } } int ans = 0; rep(i,h){ rep(j,w){ if(done[i][j] > 0) ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i<n; i++) #define repx(i, l, n) for(int i=l; i<n; i++) #define all(v) v.begin(), v.end() #define show(x) cout << #x << ": " << x << endl; #define list(x) cout << #x << ": " << x << " "; #define pb push_back using vi = vector<lint>; using vvi = vector<vector<lint>>; template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> inline void drop(T x) { cout << x << endl; exit(0); } template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; int N, M, TIME=clock(); vector<pair<int, string>> V; vi C(8); int sum = 0; vector<string> S(20); int total() { string s; int p = 0; set<string> t, u; rep(i, M) t.insert(V[i].second); vector<string> v(20); rep(i, N) v[i] = S[i]; rep(q, 2) { rep(i, N) { s = v[i] + v[i]; for (auto a : t) { bool f = false; rep(j, N) { if (a == s.substr(j, a.size())) { f = true; break; } } if (f) p += a.size(); else u.insert(a); } t.clear(); swap(t, u); } rep(i, N) { v[i].clear(); rep(j, N) v[i] += S[j][i]; } } return p; } int delta(int a, int b, int x, int y) { string s; int p = 0; set<string> t, u; rep(i, M) t.insert(V[i].second); rep(q, 2) { s = S[a] + S[a]; for (auto r : t) { bool f = false; int c = (b+N-r.size()+1)%N; rep(j, r.size()) { if (r == s.substr(c+j, r.size())) { f = true; break; } } if (f) p += r.size(); else u.insert(r); } t.clear(); swap(t, u); swap(a, x); } rep(q, 2) { s = ""; rep(i, N) s += S[i][b]; rep(i, N) s += S[i][b]; for (auto r : t) { bool f = false; int c = (a+N-r.size()+1)%N; rep(j, r.size()) { if (r == s.substr(c+j, r.size())) { f = true; break; } } if (f) p += r.size(); else u.insert(r); } t.clear(); swap(t, u); swap(b, y); } return p; } int delta_1(int a, int b) { string s; int p = 0; set<string> t, u; rep(i, M) t.insert(V[i].second); s = S[a] + S[a]; for (auto r : t) { bool f = false; int c = (b+N-r.size()+1)%N; rep(j, r.size()) { if (r == s.substr(c+j, r.size())) { f = true; break; } } if (f) p += r.size(); else u.insert(r); } t.clear(); swap(t, u); s = ""; rep(i, N) s += S[i][b]; rep(i, N) s += S[i][b]; for (auto r : t) { bool f = false; int c = (a+N-r.size()+1)%N; rep(j, r.size()) { if (r == s.substr(c+j, r.size())) { f = true; break; } } if (f) p += r.size(); else u.insert(r); } t.clear(); swap(t, u); return p; } int main() { cin >> N >> M; string s; rep(i, M) { cin >> s; V.pb({s.size(), s}); rep(j, s.size()) C[s[j]-'A']++; } rep(i, 8) sum += C[i]; rep(i, 7) C[i+1] += C[i]; sort(all(V)); int a=0, b=0, c=0, x, y, z; rep(i, N) { rep(j, N) { x = rand()%sum; rep(k, 8) { if (x < C[k]) { S[i] += 'A'+k; break; } } } } // show(total()) int count = 0; while (true) { count++; a = rand()%N; b = rand()%N; if (count%5 == 0) { y = S[a][b]-'A'; x = y; while (x == y) x = rand()%8; c = delta_1(a, b); S[a][b] = 'A'+x; z = delta_1(a, b); if (c > z) S[a][b] = 'A'+y; } else { x = rand()%N; y = rand()%N; c = delta(a, b, x, y); swap(S[a][b], S[x][y]); z = delta(a, b, x, y); if (c > z) swap(S[a][b], S[x][y]); } // list(c)list(z) show(total()) if (clock()-TIME > 2.95 * CLOCKS_PER_SEC) break; } // show(count) rep(i, N) std::cout << S[i] << '\n'; }
#include <bits/stdc++.h> using namespace std; int n,m; int dist[2010][2010]; int dp[2010][2010]; vector<int> v[2010]; int main() { cin>>n>>m; for(int i=0;i<m;i++) { int x,y,z; cin>>x>>y>>z; if(!dist[x][y]||dist[x][y]>z) dist[x][y]=z; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(dist[i][j]) v[i].push_back(j); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp[i][j]=1e9; for(int i=1;i<=n;i++) { dp[i][i]=0; queue<int> q; q.push(i); while(!q.empty()) { int x=q.front(); q.pop(); for(int j=0;j<v[x].size();j++) if(dp[i][v[x][j]]>dp[i][x]+dist[x][v[x][j]]) { dp[i][v[x][j]]=dp[i][x]+dist[x][v[x][j]]; q.push(v[x][j]); } } } for(int i=1;i<=n;i++) { int ans=1e9; for(int j=1;j<=n;j++)if(dist[j][i])ans=min(ans,dist[j][i]+dp[i][j]); if(ans==1e9)cout<<-1<<endl; else cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(false); cin.tie(0); #define FOR(i,s,n) for(int i = (s); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for(int i = (n); i >= 0; i--) #define ALL(n) (n).begin(), (n).end() #define RALL(n) (n).rbegin(), (n).rend() #define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n'; #define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n'; #define OUT(n) cout << (n) << '\n'; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; int main(void) { IOS const int INF = 1e9; int n, m; cin >> n >> m; vector<vector<pii>> v(n); vector<int> self(n,INF); REP(i,m) { int a, b, c; cin >> a >> b >> c; a--; b--; if (a == b) self[a] = min(self[a],c); else v[a].emplace_back(c,b); } auto dijkstraToAll = [&](const int from) { priority_queue<pii,vector<pii>,greater<pii>> pq; vector<bool> visit(n); vector<int> ret(n,INF); for (auto p : v[from]) pq.push(p); visit[from] = true; while(!pq.empty()) { pii p = pq.top(); pq.pop(); if (visit[p.second]) continue; visit[p.second] = true; ret[p.second] = p.first; for(auto [c,b]:v[p.second]) { if (!visit[b]) pq.push({p.first+c,b}); } } return ret; }; vector<vector<int>> len; REP(i,n) len.push_back(dijkstraToAll(i)); REP(i,n) { int ans = self[i]; REP(j,n) ans = min(ans,len[i][j]+len[j][i]); if (ans <INF) OUT(ans) else OUT(-1) } return 0; }