code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <cmath> #include <tuple> #include <cstdio> #include <bitset> #include <sstream> #include <iterator> #include <numeric> #include <map> #include <cstring> #include <set> #include <functional> #include <iomanip> using namespace std; #define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!! #ifdef DEBUG_ #define dump(x) cerr << #x << " = " << (x) << endl; #else #define dump(x) ; #endif #define equals(a,b) (fabs((a)-(b)) < EPS) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back //#define int long long typedef long long LL; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template <typename T> std::string printVector(const std::vector<T> &data) { std::stringstream ss; std::ostream_iterator<T> out_it(ss, ", "); ss << "["; std::copy(data.begin(), data.end() - 1, out_it); ss << data.back() << "]"; return ss.str(); } template <typename T> void print_array(const T &ary, int size){ REP(i,size){ cout << ary[i] << " "; } cout << endl; } const int mod = 1e9+7; const LL LINF = 1001002003004005006ll; const int INF = 1001001001; const double EPS = (1e-10); const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899; int dx[] = {0,0,-1,1}; int dy[] = {-1,1,0,0}; signed main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12); LL A,B; cin >> A >> B; if(A == B){ REP(i,A){ cout << 1+i << " "; } REP(i,B){ cout << -1-i << " "; } cout << endl; }else if(A > B){ REP(i,A){ cout << 1+i << " "; } REP(i,B-1){ cout << -1-i << " "; } cout << -((A+B) * (A-B+1) / 2) << " "; cout << endl; }else{ REP(i,A-1){ cout << 1+i << " "; } cout << ((A+B) * (B-A+1) / 2) << " "; REP(i,B){ cout << -1-i << " "; } cout << endl; } }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<n; i++) #define rng(i,l,r) for (int i=l; i<r; i++) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) using namespace std; using ll = long long; using P = pair<int,int>; template <class T> using V = vector<T>; 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; } struct in { template <class T> operator T() { T t; std::cin >> t; return t; } }; const int INF = 1 << 30; const ll LINF = 1LL << 60; int dx[8] = {0,1,0,-1,1,1,-1,-1}; int dy[8] = {1,0,-1,0,1,-1,1,-1}; using Graph = V<V<int>>; auto solve() { int n = in(); int m = in(); Graph G(n); vector<int> a(n); rep(i,n) cin >> a[i]; rep(i,m) { int x=in(), y=in(); G[x-1].push_back(y-1); } auto cmp = [&a](int i, int j) { return a[i] < a[j]; }; V<int> idx(n); rep(i,n) idx[i] = i; sort(all(idx), cmp); vector<int> seen(n, 0); int ans = -INF; for (auto i : idx) { if (seen[i]) continue; queue<int> q; q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); if (v != i) chmax(ans, a[v] - a[i]); for (auto u : G[v]) { if (seen[u]) continue; seen[u] = 1; q.push(u); } } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << solve() << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define vi vector<int> #define vvi vector<vector<int>> #define pb push_back #define pii pair<ll,ll> #define tii tuple<ll,ll,ll> #define all(v) (v).begin(),(v).end() #define ll long long #define ull unsigned long long #define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr); #define ld long double #define mod 1000000007 #define PI 3.141592653589793238 #define eps 1e-9 #define endl "\n" using namespace std; using namespace __gnu_pbds; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int getRand(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } void run_case() { ll n, q; cin>>n>>q; vector<ll> vec(n+1, 0); for(int i = 1; i <= n; ++i) cin>>vec[i]; vector<ll> dif; for(int i = 0; i < n; ++i) dif.push_back(vec[i+1] - vec[i] - 1); vector<ll> pref(n); pref[0] = dif[0]; for(int i = 1; i < dif.size(); ++i) pref[i] = pref[i-1] + dif[i]; // for(auto& it : pref) cout<<it<<" "; // cout<<endl; while(q--) { ll k; cin>>k; auto it = lower_bound(all(pref), k); ll diff = 0; if(it != pref.begin()) { auto it2 = prev(it); diff = *it2; } ll ind = it - pref.begin(); ll now = k - diff; if(it == pref.end()) { cout<<now + vec[ind]<<endl; } else { cout<<now + vec[ind]<<endl; } } } int32_t main() { fastio int t = 1; //cin>>t; for(int i = 0; i < t; ++i) { run_case(); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define re(i,n) for(int i = 0 ; i < n ; i++) #define rep(i,a,b) for(int i = a ; i <= b ; i++) #define rer(i,b,a) for(int i = b ; i >= a ; i--) #define debug(x) cout << #x << " : " << x << '\n'; const int N = (int)2e3+3; int main(){ ios_base::sync_with_stdio(false),cin.tie(nullptr); int n ,q; cin >> n >> q ; vector <ll> a(n); re(i,n) cin >> a[i]; vector<ll>gaps(n); re(i,n) { gaps[i] = a[i] - (i + 1); } while(q--) { ll k ; cin >> k; if( k < a[0] ) { cout << k << "\n"; }else if( a[0] <= k && k <= a[n-1]) { int idx = lower_bound(gaps.begin(),gaps.end(),k)-gaps.begin(); if(idx == n) cout << a[n - 1] + (k - gaps[n - 1]) << "\n"; else cout << a[idx] - gaps[idx] + k - 1 << "\n"; }else { cout << n + k << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define all(x) (x).begin(),(x).end() using ll = long long; using P = pair<ll, ll>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, -1, 0, 1}; string char_to_string(char val) { return string(1, val); } int char_to_int(char val) { return val - '0'; } char inverse_char(char c) { if(isupper(c)) return tolower(c); else return toupper(c); } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct edge { ll to, cost; }; bool IsPrime(ll num){ if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; // 偶数はあらかじめ除く double sqrtNum = sqrt(num); for (ll i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { // 素数ではない return false; } } // 素数である return true; } vector<ll> divisor(ll n) { vector<ll> ret; for(ll i = 1; i * i <= n; i++) { if(n % i == 0) { ret.push_back(i); if(i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } int main() { ll N; cin >> N; vector<ll> ans(N + 1, 1); for(int i = 2; i <= N; ++i) { auto x = divisor(i); ll tmp = 0; REP(k, x.size()) { if(x[k] == i) continue; tmp = max(tmp, ans[x[k]]); } ans[i] = tmp + 1; } for(int i = 1; i <= N; ++i) { cout << ans[i] << " "; } cout << endl; }
/* CREATED BY STREAM_CIPHER june-2021 */ #include<bits/stdc++.h> using namespace std; void __print(long long x) {cerr << x;}void __print(unsigned long long 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 int long long int #define double long double #define fix_precision(n) cout<<fixed<<setprecision(n) #define all(a) a.begin(),a.end() const double pi=acos(-1.0); int inf=0x3f3f3f3f3f3f3f3f; const int mod=1e9+7; // const int mod=998244353; const int mx=5*1000000;//5*64M bit ->5*8M byte ->40MB size for long long int (64 bit) int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif int n; cin>>n; set<int>a; for(int i=0;i<n;i++){ int in; cin>>in; a.insert(in); } if(a.size()==n){ cout<<"Yes\n"; } else{ cout<<"No\n"; } }
/* Author: QAQAutoMaton Lang: C++ Code: B.cpp Mail: lk@qaq-am.com Blog: https://www.qaq-am.com/ */ #include<bits/stdc++.h> #define debug(...) fprintf(stderr,__VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define all(x) x.begin(),x.end() #define x first #define y second using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef complex<double> cp; typedef pair<int,int> pii; int inf; const double eps=1e-8; const double pi=acos(-1.0); template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;} template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;} template<class T>T sqr(T a){return a*a;} template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;} template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;} template<class T>T aabs(T a){return a<0?-a:a;} template<class T>int dcmp(T a,T b){return a>b;} template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} #define min mmin #define max mmax #define abs aabs struct __INIT__{ __INIT__(){ fill((unsigned char *)(&inf),(unsigned char *)(&inf)+sizeof(inf),0x3f); } }__INIT___; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); } template<typename A> inline bool read (A &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read (char &x) { while((x=gc())==' '||x=='\n' || x=='\r'); return x!=EOF; } inline bool read(char *x){ while((*x=gc())=='\n' || *x==' '||*x=='\r'); if(*x==EOF)return 0; while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc(); *x=0; return 1; } template<typename A,typename ...B> inline bool read(A &x,B &...y){ return read(x)&&read(y...); } template<typename A> inline bool write (A x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); return 0; } inline bool write (char x) { putc(x); return 0; } inline bool write(const char *x){ while(*x){putc(*x);++x;} return 0; } inline bool write(char *x){ while(*x){putc(*x);++x;} return 0; } template<typename A,typename ...B> inline bool write(A x,B ...y){ return write(x)||write(y...); } //no need to call flush at the end manually! struct Flusher_ {~Flusher_(){flush();}}io_flusher_; } using io :: read; using io :: putc; using io :: write; int a[300005]; signed main(){ #ifdef QAQAutoMaton freopen("B.in","r",stdin); freopen("B.out","w",stdout); #endif int n,k,x; read(n,k); for(int i=1;i<=n;++i){read(x);++a[x];} int mn=k; int ans=0; for(int i=0;i<n;++i){chkmin(mn,a[i]);ans+=mn;} write(ans,'\n'); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int llu; typedef long double ld; #define vi vector<int> #define vl vector<ll> #define vp vector<pair<int,int>> #define pb push_back #define pf push_front #define mp map<ll,ll> #define INF 1000000000000000000 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); ll gcd(ll a,ll b) { if(a==0) { return b; } else { return gcd(b%a,a); } } ll lcm(ll a,ll b) { return a*b/gcd(a,b); } int f(char c) { return (int)c-96; } struct cmp { bool operator()(const int &a,const int &b) { return a>b; } }; /********************************************************************** *********************************************************************** *********************ACTUAL CODE BEGINS HERE***************************/ void solve() { ll n,m,k; cin>>n>>k>>m; ll a[105]; ll sum=0; for(int i=1;i<=n-1;i++) { cin>>a[i]; sum+=a[i]; } ll t=n*m-sum; if(t>=0&&t<=k) { cout<<t; } else if(t<0) { cout<<0; } else { cout<<-1; } } int main() { fast; int t; t=1; //cin>>t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define clr(a) memset(a, 0, sizeof(a)) #define full(a) memset(a, 0x3f, sizeof(a)) #define fornext(x, i) for(int i = head[x], y = ver[i]; i; i = nxt[i], y = ver[i]) #define mset(a, b) memset(a, b, sizeof(a)) #define Rep(i, a, b) for(int i = (a); i <= (b); ++i) #define dRep(i, a, b) for(int i = (a); i >= (b); --i) #define IOset(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout); #define endl '\n' #define int ll #define un unsigned #define ll long long #define db double #define rept cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl; #define dbg cout<<"c " #define dbug(x) cerr << #x " = " << (x) << endl template<typename _T> inline void _prt(_T _d) { cerr << _d << ' '; } template <typename _T, typename ...Args> inline void _prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...); } template <typename _T, typename ...Args> inline void prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...), cerr<<endl; } template <typename _T, typename ...Args> inline void prt(_T _d) { cerr << _d <<endl; } template <typename _T, typename ...Args> inline void cbg(_T _d, Args ..._a) { cerr << "--LINE " << __LINE__ << "--: "; prt(_d, _a...); } template<typename _T> inline void rd(_T &_d) { signed _f; char _c; _f = _d = 0; while (_c = getchar(), !isdigit(_c)) _f = _f || (_c == '-'); while (isdigit(_c)) _d = _d * 10 + _c - '0', _c = getchar(); _d = (_f ? -_d : _d); } template <typename _T, typename ...Args> inline void rd(_T &_d, Args &..._a) { rd(_d); rd(_a...); } const int N=5e5+5; int n; char s[N],t[N]; stack<int> st; void no() { puts("-1"); exit(0); } signed main() { rd(n); scanf("%s",s+1); scanf("%s",t+1); Rep(i,1,n) s[i]-='0'; Rep(i,1,n) t[i]-='0'; int ans=0; dRep(i,n,1) { if(s[i]==t[i]) continue; if(s[i]==1) st.push(i); else { if(st.empty()) no(); else ans+=st.top()-i,st.pop(); } } int x; if(st.size()&1) no(); while(st.size()) x=st.top(),st.pop(),ans+=st.top()-x,st.pop(); cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N=1e5+1e3+7; int n,f[N],sz[N]; vector<int>g[N]; int main() { scanf("%d",&n); for(int i=2;i<=n;i++) { int p; scanf("%d",&p); g[p].push_back(i); } for(int i=n;i>=1;i--) { int x=i; f[x]=1; sz[x]=1; vector<int>cho; int sum=0; for(auto v:g[x]) { sz[x]+=sz[v]; if(sz[v]&1) cho.push_back(f[v]); else if(f[v]<0) f[x]+=f[v]; else sum+=f[v]; } sort(cho.begin(),cho.end()); cho.push_back(sum); for(int i=0;i<cho.size();i++) if(i&1) f[x]-=cho[i]; else f[x]+=cho[i]; } printf("%d\n",(f[1]+n)/2); }
#pragma GCC optimize("O3") #pragma GCC target("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define mod 1000000007 #define pi 3.1415926535898 #define eps 1e-9 #define fast ios::sync_with_stdio(0); cin.tie(0);cout.tie(0) #define vt vector #define ar array #define fs first #define sc second #define pb push_back #define sp printf(" ") #define nl '\n' #define all(a) a.begin(),a.end() #define unique(c) (c).resize(unique(all(c)) - (c).begin()) #define sz(x) (int)(x).size() #define revsort(x) sort(all(x));reverse(all(x)) #define set0(a) memset(a,0,sizeof(a)) #define setneg(a) memset(a,-1,sizeof(a)) #define setinf(a) memset(a,126,sizeof(a)) #define REP(i,start,end) for (int i = start; i <=end; i++) #define RREP(i,end,start) for (int i=end; i>=start ;i--) #define EACH(x, a) for (auto& x: a) typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<LL> vll; typedef vector<double> vd; typedef vector<vector<LL> > matrix; typedef vector<vector<int> > graph; template<class A> void read(vt<A>& v); template<class A, size_t S> void read(ar<A, S>& a); template<class T> void read(T& x) { cin >> x; } void read(double& d) { string t; read(t); d=stod(t); } void read(long double& d) { string t; read(t); d=stold(t); } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template<class A> void read(vt<A>& x) { EACH(a, x) read(a); } template<class A, size_t S> void read(array<A, S>& x) { EACH(a, x) read(a); } int add (LL var1, LL var2) { return ( (var1 % mod) + (var2 % mod) + mod) % mod; } int mul (LL var1, LL var2) { return (var1*var2) % mod; } int powm (LL x, LL y) { int ans = 1; while (y) { if (y & 1) { ans = mul(ans, x); } x = mul(x, x); y /= 2; } return ans; } int inv(LL x) { return powm(x, mod-2); } LL INF=1e9; void solve() { int n; read(n); vll a(n); read(a); sort(all(a)); LL sum = 0; for (int i=n-1; i>=0; i--) sum += i*a[i] - (n-1-i)*a[i]; cout << sum << nl; } int main() { fast; solve(); }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int n, ans=0; cin >> n; for(int i = 1; i < n; i++) { if ((n-i) > 0) ++ans; } cout << ans << 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; template<typename has_less> using ordered_set = tree<has_less, null_type, less_equal<has_less>, rb_tree_tag, tree_order_statistics_node_update>; // typedef tree< // int, // null_type, // less_equal<int>, // rb_tree_tag, // tree_order_statistics_node_update> ordered_set; // find_by_order(k) returns iterator to kth element starting from 0; // order_of_key(k) returns count of elements strictly smaller than k; # define ll long long int #define M 1000000007 #define pb push_back #define ss second #define ff first #define inf 1000000000000000 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 //nestedcode long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll m[20][20]; ll dpp[1<<20]; map<ll,ll> gm; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,m1,i,j,mask,a,b,fl,h,ans = 0,msk; cin>>n>>m1; for(i=0;i<m1;i++){ cin>>a>>b; a--; b--; m[a][b] = 1; m[b][a] = 1; } for(mask=1;mask<(1<<n);mask++){ dpp[mask] = inf; ll v = 0,d,sub,sub2; vector<ll> vec; for(j=0;j<n;j++){ if((mask>>j) & 1){ v += (1ll<<j); vec.pb(j); } } fl = 0; for(j=0;j<vec.size();j++){ for(h=0;h<vec.size();h++){ if(j == h) continue; if(m[vec[h]][vec[j]] == 0){ fl = 1; break; } }if(fl) break; } if(fl == 0){ dpp[v] = 1; continue; } for(int i = mask; i > 0; i = (i-1) & mask){ dpp[mask] = min(dpp[mask], dpp[i] + dpp[v ^ i]); } } cout<<dpp[(1<<n) - 1]; }
#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,m,f[21][5002],fac[5002],inv[5002],s; inline int C(re int x,re int y){return 1ll*fac[x]*inv[y]%M*inv[x-y]%M;} signed main(){ n=read(),m=read(); for(re int i=fac[0]=inv[0]=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%M,inv[i]=ksm(fac[i],M-2); f[0][m]=1; for(re int i=0;(1<<i)<=m;++i){ s=i; for(re int j=0;j<=m;++j){ if(j%(1<<i))continue; if(!f[i][j])continue; for(re int k=0;k*(1<<i)<=j&&k<=n;k+=2)add(f[i+1][j-k*(1<<i)],1ll*f[i][j]*C(n,k)%M); } } printf("%d",f[s+1][0]); }
#include<bits/stdc++.h> using namespace std; vector<int> G[400400]; bool vis[400400]; int ce,cv; void dfs(int x) { vis[x]=true; cv++; ce+=(int)(G[x].size()); for(int i=0;i<(int)(G[x].size());i++) if(!vis[G[x][i]]) dfs(G[x][i]); return ; } int main() { int n; cin>>n; for(int i=0;i<n;i++) { int a,b; cin>>a>>b; a--; b--; G[a].push_back(b); G[b].push_back(a); } int ans=0; for(int i=0;i<400000;i++) if(!vis[i]) { ce=cv=0; dfs(i); ce/=2; ans+=min(ce,cv); } cout<<ans<<endl; return 0; }
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<bitset> #include<set> #include<map> #include<stack> #include<queue> #include<deque> #include<list> #include<iomanip> #include<cmath> #include<cstring> #include<functional> #include<cstdio> #include<cstdlib> #include<numeric> //#include<atcoder/all> using namespace std; //using namespace atcoder; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 #define MOD 1000000007 //#define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 #define PI acos(-1.0) const double EPS = 1e-10; using ll = long long; using Pi = pair<int,int>; using Pl = pair<ll,ll>; //using mint = modint998244353; int dh[] = {-1,0,1,0}; int dw[] = {0,1,0,-1}; class DisjointSet{ public: vector<ll> rank,p,siz,es; DisjointSet(){} DisjointSet(ll size){ rank.resize(size,0); p.resize(size,0); siz.resize(size,1); es.resize(size,0); rep(i,size) makeSet(i); } void makeSet(ll x){ p[x] = x; rank[x] = 0; } bool same(ll x, ll y){ return root(x) == root(y); } void unite(ll x, ll y){ x = root(x); y = root(y); es[x]++; if(x == y) return; if(rank[x] > rank[y]){ siz[x] += siz[y]; es[x] += es[y]; p[y] = x; } else{ siz[y] += siz[x]; es[y] += es[x]; p[x] = y; if(rank[x] == rank[y]) rank[y]++; } } ll root(ll x){ if(x != p[x]){ p[x] = root(p[x]); } return p[x]; } ll size(ll x){ return siz[root(x)]; } ll esize(ll x){ return es[root(x)]; } }; int main(){ int n; cin >> n; DisjointSet uf(400000); rep(i,n){ int a,b; cin >> a >> b; a--; b--; uf.unite(a,b); } int ans = 0; rep(i,400001){ if(uf.root(i) == i) ans += min(uf.size(i),uf.esize(i)); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int X,Y; bool a = false; cin >> X >> Y; if(max(X,Y)-min(X,Y) < 3){ a = true; } if(a == false){ cout << "No" << endl; } if(a == true){ cout << "Yes" << endl; } }
#include<cstdio> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<iomanip> #include<map> #include<unordered_map> #include<set> #include<unordered_set> #include<vector> #include<algorithm> #include<string> #include<queue> using namespace std; #define DEBUG(x) cout<<#x<<"="<<x<<endl #define DEBUG2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl typedef long long ll; const int MAXN=4e5+10; const ll MOD=998244353; ll _gcd(ll a,ll b){if(b==0)return a;return _gcd(b,a%b);} ll gcd(ll a,ll b){a=abs(a),b=abs(b);if(a<b)swap(a,b);return _gcd(a,b);} ll qpow(ll a,ll n){ll rt=1;while(n){if(n&1)rt=(rt*a)%MOD;a=a*a%MOD;n>>=1;}return rt;} template <int _MOD> struct Mint { //一个好的软件包应该是封闭的,例如,mint的所有操作都可以在软件包内部实现的 long long v = 0; Mint() {} Mint(int _v) : v((_v%_MOD+_MOD)%_MOD) {} Mint(long long _v) : v(static_cast<int>((_v%_MOD+_MOD)%_MOD)) {} Mint operator = (const int &_v) { return *this = Mint(_v); } Mint operator = (const long long &_v) { return *this = Mint(_v); } bool operator ! () const { return !this->v; } bool operator < (const Mint &b) const { return v < b.v; } bool operator > (const Mint &b) const { return v > b.v; } bool operator == (const Mint &b) const { return v == b.v; } bool operator != (const Mint &b) const { return v != b.v; } bool operator <= (const Mint &b) const { return v < b.v || v == b.v; } bool operator >= (const Mint &b) const { return v > b.v || v == b.v; } Mint operator + (const Mint &b) const { return Mint(v+b.v); } Mint operator - (const Mint &b) const { return Mint(v-b.v); } Mint operator * (const Mint &b) const { return Mint(1ll*v*b.v); } Mint operator / (const Mint &b) const { return Mint(b.inv()*v); } Mint& operator += (const Mint &b) { return *this = *this+b; } Mint& operator -= (const Mint &b) { return *this = *this-b; } Mint& operator *= (const Mint &b) { return *this = *this*b; } Mint& operator /= (const Mint &b) { return *this = *this/b; } Mint operator - () const { return Mint(-v); } Mint& operator ++ () { return *this += 1; } Mint& operator -- () { return *this -= 1; } Mint operator ++ (int) { Mint tmp = *this; *this += 1; return tmp; } Mint operator -- (int) { Mint tmp = *this; *this -= 1; return tmp; } Mint pow(int p) const { Mint res(1), x(*this); while (p) { if (p&1) res = res*x; x *= x; p >>= 1; } return res; } Mint inv() const { if(v==0)return Mint(1); return pow(_MOD-2); } friend istream& operator >> (istream &is, Mint &mt) { return is >> mt.v;mt.v=mt.v%_MOD; } friend ostream& operator << (ostream &os, const Mint &mt) { return os << mt.v; } }; using mint = Mint<MOD>; mint factor[MAXN]; mint inv[MAXN]; void cal_factor(){factor[0]=mint(1);for(int u=1;u<MAXN;u++){factor[u]=factor[u-1]*mint(u);}} void cal_factor_inv(){inv[0]=mint(1);inv[MAXN-1]=factor[MAXN-1].inv();for(int u=MAXN-2;u>=1;u--){inv[u]=inv[u+1]*mint(u+1);}} void init_factor(){cal_factor();cal_factor_inv();} mint C(ll n,ll k){if(n<k)return mint(0);return factor[n]*inv[n-k]*inv[k];} void solve(){ int n,m; cin>>n>>m; init_factor(); mint ans=mint(0); for(ll t=1;t<=m;t++){ ll x=t; mint tmp=mint(1); for(ll q=2;q*q<=x;q++){ int cnt=0; while(x%q==0){ x/=q; cnt++; } tmp*=C(cnt+n-1,cnt); } if(x>1)tmp*=mint(n); ans+=tmp; } cout<<ans<<"\n"; } // #define LOCAL int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define mii map <int, int> #define mll map <ll, ll> #define pii pair <int, int> #define pll pair <ll, ll> #define vi vector <int> #define vd vector <double> #define vll vector <ll> #define fi first #define se second #define si set <int> #define sll set <ll> #define spii set <pii> #define vs vector <string> #define vpii vector <pair <int, int> > #define vpll vector <pair <long long, long long> > #define vvi vector <vector <int> > #define vvpii vector <vector <pii > > #define vb vector <bool> #define vvb vector <vb> #define mp make_pair #define vvll vector <vll> #define vsi vector <si> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define MANX MAXN #define itn int #define dbg(x); {cout << #x << "=" << x << ", ";} #define in(x); { for (auto &to : x) cin >> to;} #define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; } const ll INFLL = 1e18; const int MAXN = 1e6 + 100; const ll INF = 1e9; const int mod1 = 1e9 + 7; const int mod2 = 1e9 + 21; void solve() { int n, k; cin >> n >> k; vector<int> cnt(n); for (int i = 0; i < n; i++) { int x; cin >> x; cnt[x]++; } int was = cnt[0]; ll ans = 0; vector <pll> a; for (int i = 1; i < n; i++) { int now = min(was, cnt[i]); int stay = was - now; a.pb({i,stay}); swap(was, now); } reverse(all(a)); for (auto to:a) { int x=min(k,(int)(to.se)); ans+=1ll*x*to.fi; k-=x; } cout<<ans<<'\n'; } int main() { #ifdef Mip182 freopen("a.in", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif int _t; _t = 1; // cin>>_t; while (_t--) solve(); #ifdef Mip182 cout << '\n' << "Time : " << (double) (clock()) / CLOCKS_PER_SEC << '\n'; #endif }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; ll gcd(ll a, ll b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } ll f(ll h) { if (h == 1) { return 1; } else { return 2 * f(h / 2) + 1; } } ll mod = 1000000007; ll powmod(ll a, ll b) { ll ret = 1; for (int i = 0; i < b; i++) { ret = (ret * a) % mod; } return ret; } ll comb(ll n, ll k) { map<string, ll> memo; if (n == k || k == 0) { return 1; } else { string key = to_string(n) + to_string(k); if (memo.count(key) != 0) { return memo[key]; } else { ll v = comb(n - 1, k - 1) + comb(n - 1, k); memo[key] = v; return v; } } } int f(int n, int k) { int ret = 0; while (n < k) { n *= 2; ret++; } return ret; } int main() { int N, K; cin >> N >> K; vector<int> v(N), box(K, -1); for (int i = 0; i < N; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int i = 0; int prev = -1; for (auto a : v) { if (prev != a) { i = 0; } if (box[i] + 1 == a) { box[i] = a; i++; } prev = a; } ll ans = 0; for (auto a : box) { if (a != -1) { ans += (a + 1); } } cout << ans << endl; }
#pragma GCC optimize("O3") #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;} template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;} constexpr int MOD = 998244353; constexpr int inf = 1e9; constexpr long long INF = 1e18; #define all(a) (a).begin(), (a).end() int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; class mint{ public: ll x; constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} constexpr mint operator-() const{ return mint(-x); } constexpr mint& operator+=(const mint& a){ if ((x += a.x) >= MOD) x -= MOD; return *this; } constexpr mint& operator-=(const mint& a){ if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } constexpr mint& operator*=(const mint& a){ (x *= a.x) %= MOD; return *this; } constexpr mint operator+(const mint& a) const{ mint res(*this); return res += a; } constexpr mint operator-(const mint& a) const{ mint res(*this); return res -= a; } constexpr mint operator*(const mint& a) const{ mint res(*this); return res *= a; } constexpr 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 constexpr mint inv() const{ return pow(MOD - 2); } constexpr mint& operator/=(const mint& a){ return (*this) *= a.inv(); } constexpr mint operator/(const mint& a) const{ mint res(*this); return res /= a; } }; istream& operator>>(istream& is, mint& a) {return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) {return os << a.x;} mint modpow(mint a, ll b){ if(b == 0) return 1; else if(b % 2 == 0){ mint d = modpow(a, b / 2); return d * d; } else{ return a * modpow(a, b - 1); } } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin>>n; vector<int> a(n); for(int i=0; i<n; i++) cin>>a[i]; sort(all(a)); mint ans = 0, sum = 0; for(int i=n-1; i>=0; i--){ ans += (sum + a[i]) * a[i]; sum = sum * 2 + a[i]; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; char s[n]; for (int i=0;i<n;i++){ cin >> s[i]; } string res; res=""; for(int i=0;i<n;i++){ res.push_back(s[i]); if(res.size()>=3){ if(res.at(res.size()-3)=='f' && res.at(res.size()-2)=='o' && res.at(res.size()-1)=='x'){ res.erase(res.size()-3); } } //cout << res << endl; } cout << res.size() <<endl; return 0; }
// Bismillahir Rahmanir Rahim //============================ #include<bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define infL LLONG_MAX #define infI INT_MAX #define pb push_back #define fo(i,a,b) for(int i=a;i<b;i++) #define fo2(i,a,b) for(int i=a;i<=b;i++) #define rfo(i,a,b) for(int i=a; i>=b;i--) #define precise(x,y)cout<<fixed<<setprecision(y)<<x<<endl; #define MAX 100010 #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define bye return 0 #define debug cout<<"\nDebug\n"; #define debug2 cout<<"\nDebug2\n"; #define PEREGRINE_FALCON {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);} typedef pair<ll,ll>PII; double epsilon = 0.0000001f; ll mod=1e9+7; double Pi =acos(-1); void print(vector<ll>V) { cout<<"\n---------------Vector checking start-------------------\n"; ll sz=V.size(); fo(i,0,sz){cout<<V[i]<<" ";} cout<<"\n----------------Vector checking end--------------------\n"; } void print(ll arr[], ll n) { cout<<"\n---------------Array checking start-------------------\n"; fo(i,0,n){cout<<arr[i]<<" ";} cout<<"\n----------------Array checking end--------------------\n"; } struct strct{ ll a,b; }; bool cmp(strct A, strct B) { return A.a<B.a; } int main() { PEREGRINE_FALCON //freopen("inputNew.txt", "r", stdin); ll testCase=1; //cin>>testCase; while(testCase--){ ll n,m,k,x,y,z,a,b,c,d,mn,mx,temp,cnt,ans,sum,sz,len; cin>>n>>k; struct strct s[n]; fo(i,0,n){cin>>s[i].a>>s[i].b;} sort(s,s+n,cmp); cnt=k; for(int i=0; i<n; i++){ if(s[i].a > cnt) break; cnt+=s[i].b; } cout<<cnt<<endl; } bye; }
#include<bits/stdc++.h> using namespace std; int main() { long long n,k,i,j,num,sum=0,x,y; cin>>n>>k; vector< pair <long long,long long> > vect; for (int i=0; i<n; i++){ cin>>x>>y; vect.push_back( make_pair(x,y) ); } sort(vect.begin(), vect.end()); for(i=0;i<n;i++) { if(vect[i].first>k) break; k=k+vect[i].second; } cout<<k<<endl; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second typedef long long ll; typedef pair<int,int> ii; double a, b, c, d; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b >> c >> d; cout << fixed << setprecision(12); cout << a + b*(c-a)/(b+d) << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; template <typename T> void read(T &x) { x = 0; char c = getchar(); int f = 0; for (; !isdigit(c); c = getchar()) f |= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ '0'); if (f) x = -x; } template <typename T> void write(T x, char ed = '\n') { if (x < 0) putchar('-'), x = -x; static short st[30], tp; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar(st[tp--] | '0'); putchar(ed); } int ans[23453], cnt, lim = 90; ll f[43242], n; int main() { //srand((ll)new char ^ time(0)); //n = (1e18) - rand(); read(n); f[0] = f[1] = 1; for (int i = 2;i <= lim; ++i) f[i] = f[i - 1] + f[i - 2]; for (int i = 1;i <= lim; ++i) { if (n >= f[lim - i + 1]) ans[++cnt] = 2 - (i & 1), n -= f[lim - i + 1]; if (cnt) ans[++cnt] = 3 + (i & 1); } write(cnt); for (int i = 1;i <= cnt; ++i) write(ans[i]); //ll x = 0, y = 0; //for (int i = 1;i <= cnt; ++i) { //if (ans[i] == 1) ++x; //else if (ans[i] == 2) ++y; //else if (ans[i] == 3) x += y; //else y += x; //} //write(x, ' '), write(y); return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please ll A[200001], B[200001], tmp[200001]; void pakuri_sort(int N, ll A[]) { const int b = 8; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } const int CM = 400001, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); rep(i, N) { A[i] = ll(i) << 32 | getint(); B[i] = ll(i) << 32 | getint(); } pakuri_sort(N, A); pakuri_sort(N, B); const ll ma = (1ll << 32) - 1; rep(i, N) { auto tmp = (A[i] & ma) < (B[N - 1 - i] & ma); cn[A[i] >> 31] = tmp; cn[B[N - 1 - i] >> 31 | 1] = tmp; } int c = -1, pn = 0; rep(i, N << 1) { if (c != -1 && pn == cn[i]) { cn[c] = '('; cn[i] = ')'; c = tmp[c]; pn ^= 1; } else { pn = cn[i]; tmp[i] = c; c = i; } } fwrite(cn, 1, N << 1, stdout); Would you please return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; //mp::int128_t; //mp::_int512_tなど //256, 1024 #define sz(x) int(x.size()) //size_tはunsigned using P = pair<int,char>; int main() { string s; cin >> s; char last; bool first = true; int ssize = sz(s); ll ans=0; vector<P> v; for(int i=0;i<ssize-2;i++) { if(s[i] == s[i+1] && s[i] != s[i+2]) { v.push_back({i+2,s[i]}); } } int vsize = sz(v); if(vsize == 0) { cout << 0 << endl; return 0; } last = v.back().second; v.pop_back(); int c = 0; for(int i=ssize-1;i>=2;i--) { if(last == s[i]) c++; if(s[i-2] == s[i-1] && s[i-1] != s[i]) { // last = s[i-1]; string tmp = s.substr(i,ssize-i); // int t = count(tmp.cbegin(),tmp.cend(), last); ans += ssize - i -c; // string rp(ssize-t,last); // s.replace(i,ssize-t,rp); if(sz(v) == 0) break; char h = v.back().second; v.pop_back(); if(h==last) c=ssize-i; else c=0; last = h; } } // for(int i=ssize-1;i>=2;i--) { // if(s[i-2] == s[i-1] && s[i-1] != s[i]) { // last = s[i-1]; // string tmp = s.substr(i,ssize-i); // int t = count(tmp.cbegin(),tmp.cend(), last); // ans += ssize - i -t; // string rp(ssize-t,last); // s.replace(i,ssize-t,rp); // } // } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ldb = long double; //sort(myVec.begin(),myVec.end(),[](const vector<int> &alpha,const vector<int> &beta){return alpha[0] < beta[0];}); using ll = long long; using ldb = long double; int main() { int N; cin >> N; int s = N*1.08; if( s == 206) { cout << "so-so" << endl; } else if( s > 206) { cout << ":(" << endl; } else if( s < 206) { cout << "Yay!" << endl; } }
#include <bits/stdc++.h> #define endl '\n' #define ff first #define ss second #define ll long long #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #define deb(x) cout << #x << ':' << x << '\n'; #define int long long using namespace std; template <class T, class K>ostream& operator<<(ostream &cout, pair<T, K> a) {cout << a.first << ' ' << a.second; return cout;} template<class T>void show(vector<T> &a) {cerr << "[ "; for (int ij = 0; ij < (int)a.size(); ij++) {cerr << a[ij] << " ";} cerr << "]\n";} template<class T>void show(T a) {cerr << a << endl;} template<typename... T>void show(T... args) {((cerr << args << ' '), ...); cerr << endl;} template<class T>void read(vector<T> &a) {for (auto &x : a) {cin >> x;}} template<class T> void read(T &a) {cin >> a;} void solve() { int n; cin >> n; int res = (n * 1.08) * 1; if (res < 206) cout << "Yay!" << endl; else if (res == 206) cout << "so-so" << endl; else cout << ":("; } int32_t main(int32_t argc, char *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); int T = 1; // cin >> T; while (T--) { solve(); } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; }
#include <string> #include <vector> #include <map> #include <set> #include <deque> #include <iostream> #include <functional> #include <algorithm> #include <sstream> #include <iterator> #include <cmath> #include <limits> #include <tuple> #include <queue> #include <stack> #include <bitset> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() int main() { int N; cin >> N; set<string> memo; rep(i, N) { string S; cin >> S; auto it = memo.find(S); if (it != memo.end()) { auto ans = (*it)[0] == '!' ? it->substr(1) : *it; cout << ans << endl; return 0; } memo.insert(S[0] == '!' ? S.substr(1) : "!" + S); } cout << "satisfiable" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define SPEED \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); // Aakash int32_t main() { SPEED; int n; cin>>n; vector<int> a(3,0); int temp=n; int digit=0; int sum=0; while(temp>0) { a[(temp%10)%3]++; sum+=temp%10; temp/=10; digit++; } if(sum%3==0) cout<<0; else if(sum%3==1) { if(a[1]>0 && digit!=1) { cout<<1; } else if(a[1]>0 && digit==1) { cout<<-1; } else { if(a[2]>1 && digit>2 ) { cout<<2; } else { cout<<-1; } } } else if(sum%3==2) { if(a[2]>0 && digit!=1) { cout<<1; } else if(a[2]>0 && digit==1) { cout<<-1; } else { if(a[1]>1 && digit>2) { cout<<2; } else { cout<<-1; } } } return 0; }
#include <iostream> #include <algorithm> #include <vector> using namespace std; int N; string S, X; vector<int> v; int main() { cin >> N >> S >> X; v.resize(N); for (int i = N - 1, a = 1; i >= 0; i--, a = a * 10 % 7) { v[i] = a * (S[i] - '0') % 7; } vector<vector<bool>> dp(N + 1, vector<bool>(7)); dp[N][0] = true; for (int i = N - 1; i >= 0; i--) { if (X[i] == 'T') { for (int j = 0; j < 7; j++) { dp[i][j] = dp[i+1][j] || dp[i+1][(j-v[i]+7) % 7]; } } else { for (int j = 0; j < 7; j++) { dp[i][j] = dp[i+1][j] && dp[i+1][(j-v[i]+7) % 7]; } } } cout << (dp[0][0] ? "Takahashi" : "Aoki") << endl; }
#include<iostream> #include<string> #include<cmath> using namespace std; int main() { int r1, r2, c1, c2; int result = 0; cin >> r1 >> c1; cin >> r2 >> c2; if (r1 == r2 && c1 == c2) { result = 0; } else if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2 || abs(r1 - r2) + abs(c1 - c2) <= 3) { result = 1; } else if((r1+c1)%2==(r2+c2)%2||abs(r1-r2+c1-c2)<=3||abs(r1-r2-c1+c2)<=3){ result = 2; } else { result = 3; } cout << result; return 0; }
#include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define PI acos(-1) #define pb push_back #define int long long #define ld long double #define sp fixed<<setprecision #define bp __builtin_popcountll #define all(x) x.begin(),x.end() #define pii pair<long long,long long> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const int M = (1e9)+7; const int N = 400005; const int INF = 1e12; signed main() { FAST int tc=1; // cin>>tc; for(int ti=1;ti<=tc;ti++) { ld r,x,y; cin>>r>>x>>y; x *= x; y *= y; ld d = sqrt(x+y); int ans = d/r; ans += (1 + (!ans))*(ans*r != d); cout<<ans<<endl; } return 0; }
// #include <bits/stdc++.h> const int INF=0x3f3f3f3f; const int maxn=50000005; using namespace std; int main(){ //freopen(".in", "r",stdin ); //freopen(".out","w",stdout); ios::sync_with_stdio(0); int a,b,x,y; cin>>a>>b>>x>>y; if(b>a){ if(2*x<y) cout<<(b-a)*2*x+x<<endl; else cout<<(b-a)*y+x<<endl; } else if(a==b) cout<<x<<endl; else{ if(2*x<y) cout<<(a-b-1)*2*x+x<<endl; else cout<<(a-b-1)*y+x<<endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<(n);++i) #include <algorithm> #include <iostream> using ll=long long; using namespace std; int main() { int n; cin>>n; vector<pair<ll,int>>x(n),y(n); rep(i,n){ cin>>x[i].first>>y[i].first; x[i].second=i+1; y[i].second=i+1; } sort(x.begin(),x.end()); sort(y.begin(),y.end()); ll ans; ll ans1=max((abs(x[1].first-x[n-1].first)),(abs(x[0].first-x[n-2].first))); ll ans2=max((abs(y[1].first-y[n-1].first)),(abs(y[0].first-y[n-2].first))); ans=max(ans1,ans2); if(x[0].second==y[0].second&&x[n-1].second==y[n-1].second){ cout<<ans<<endl; return 0; } if(abs(x[0].first-x[n-1].first)<ans||abs(y[0].first-y[n-1].first)<ans){ cout<<ans<<endl; return 0; } cout<<min((abs(x[0].first-x[n-1].first)),(abs(y[0].first-y[n-1].first)))<<endl; }
#include <bits/stdc++.h> using namespace std; typedef pair <int,int> P; typedef long long int ll; P get_max(vector <P> vec){ int n=vec.size(); vector <P> vx(n),vy(n); for(int i=0;i<n;i++) vx[i]=P(vec[i].first,i); for(int i=0;i<n;i++) vy[i]=P(vec[i].second,i); sort(vx.begin(),vx.end()); sort(vy.begin(),vy.end()); if(vx[n-1].first-vx[0].first>=vy[n-1].first-vy[0].first){ return P(vx[0].second,vx[n-1].second); } return P(vy[0].second,vy[n-1].second); } int dist(vector <P> vec,P p){ int x=p.first,y=p.second; return max(abs(vec[x].first-vec[y].first),abs(vec[x].second-vec[y].second)); } int main(){ int n; scanf("%d",&n); vector <P> vec; for(int i=0;i<n;i++){ int x,y; scanf("%d%d",&x,&y); vec.push_back(P(x,y)); } P p=get_max(vec); vector <P> v1,v2; for(int i=0;i<vec.size();i++){ if(i!=p.first) v1.push_back(vec[i]); if(i!=p.second) v2.push_back(vec[i]); } P q=get_max(v1),r=get_max(v2); printf("%d\n",max(dist(v1,q),dist(v2,r))); }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") typedef long long ll; typedef long double ld; #define REP(i,a,b) for (int i = a; i <= b; i++) #define REPR(i,a,b) for(int i = b; i >= a; i--) #define all(a) a.begin(), a.end() #define alln(a, n) (a, a+n) #define sz(x) (int)(x).size() #define MAX 10000007 #define MIN -10000007 #define pi pair<int,int> #define pl pair<ll,ll> #define mii map<int,int> #define ff first #define ss second #define pb push_back #define pf push_front #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define inf 1e18 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<string> vs; typedef set<int> si; const int MOD=1e9+7; ll lcm(ll a,ll b){return (a * b) / __gcd(a,b);} ll gcd(ll a,ll b){return __gcd(a,b);} ll power(ll a,ll b,ll MOD){if(b == 0)return 1;if(b == 1)return a;ll ans = power(a,b/2,MOD) % MOD;ans *= ans;ans %= MOD;if(b % 2 == 1)ans *= a;return ans%MOD;} ll inverse(ll x){x%=MOD;return power(x,MOD-2,MOD);} ll fact[1],inv[1]; 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; } } ll ncr(ll n,ll r){ if(n<r||n<0||r<0) return 0; ll b=inv[n-r]; ll c=inv[r]; ll a=fact[n]*b; if(a>=MOD) a%=MOD; a*=c; if(a>=MOD) a%=MOD; return a; } vector<char> is_prime(100001, true); void sieve(ll n){ is_prime[0] = is_prime[1] = false; for (int i = 2; i <= n; i++) { if (is_prime[i] && (long long)i * i <= n) { for (int j = i * i; j <= n; j += i) is_prime[j] = false; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t; t=1; cout<<fixed<<setprecision(0); // cin>>t; while(t--){ int n,m,ans=0; cin>>n>>m; char a[n][m]; REP(i,0,n-1) REP(j,0,m-1) cin>>a[i][j]; REP(i,0,n-1){ REP(j,0,m-1){ if(i+1<=n-1&&a[i][j]=='.'&&a[i+1][j]=='.') ans++; if(j+1<=m-1&&a[i][j]=='.'&&a[i][j+1]=='.') ans++; } } cout<<ans<<"\n"; } cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl; return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <map> #include <set> #include <cmath> #include <iomanip> using namespace std; using ll = long long; using vi = vector<int>; #define in(v) v; cin >> v; void ins() {} template<class T,class... Rest>void ins(T& v,Rest&... rest){cin>>v;ins(rest...);} #define rep(i,n) for(int i=0,_i=(n);i<_i;++i) #define rrep(i,n) for(long long i=(n);i>=0;--i) #define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c)) // === debug === template<class T>ostream& operator<<(ostream& os,const vector<T>& vec){os<<"{";for(size_t i=0;i<vec.size();++i)os<<(i?", ":"")<<vec[i];os<<"}";return os;} ostream& operator<<(ostream& os,const vector<char>&v){for(size_t i=0;i<v.size();++i)os<<v[i];return os;} template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>& rhs){os<<"("<<rhs.first<<", "<<rhs.second<<")";return os;} #ifdef LOCAL void debug() {cerr << "\n";} template<class First> void debug(const First& first) {cerr<<first<<"\n";} template<class First, class... Rest> void debug(const First& first, const Rest&... rest) {cerr<<first<<",";debug(rest...);} void debug2() {cerr << "\n";} template<class First> void debug2(const First& first) {cerr<<first<<" ";} template<class First, class... Rest> void debug2(const First& first, const Rest&... rest) {cerr<<first<<" ";debug2(rest...);} #else #define debug(...) 42 #define debug2(...) 42 #endif // === end === int main() { int H, W; ins(H, W); vector<string> S(H); rep(i, H) { cin >> S[i]; } int ans = 0; rep(i, H) rep(j, W-1) { if (S[i][j] == '.' && S[i][j+1] == '.') ++ans; } rep(i, H-1) rep(j, W) { if (S[i][j] == '.' && S[i+1][j] == '.') ++ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int bound = 10000; bool visited[10010]; int main(){ int N; cin >> N; cout << 6 << " " << 10 << " " << 15; N -= 3; for(int j = 12; j <= bound; j += 6){ if(!visited[j] && N > 0){ cout << " " << j; visited[j] = true; N--; } } for(int j = 20; j <= bound; j += 10){ if(!visited[j] && N > 0){ cout << " " << j; visited[j] = true; N--; } } for(int j = 30; j <= bound; j += 15){ if(!visited[j] && N > 0){ cout << " " << j; visited[j] = true; N--; } } cout << '\n'; }
#include <cstdio> using namespace std; int n,cnt,ans[10010]; int main() { scanf("%d",&n); if(n==3) { printf("6 10 15"); return 0; } for(int i=1;i<=10000;i++) if(i%10==0||i%15==0||i%6==0) ans[++cnt]=i; printf("%d %d %d ",6,10,15); for(int i=1;i<=n;i++) { if(ans[i]==6||ans[i]==10||ans[i]==15) continue; printf("%d ",ans[i]); } // printf("%d",cnt); }
#define _DEBUG #include "bits/stdc++.h" //#include <atcoder/all> #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0,a1,a2,a3,a4,a5,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_6(x1,x2,x3,x4,x5,x6) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<", "#x6<<": "<<x6<<endl #ifdef _DEBUG #define debug(...) CHOOSE((__VA_ARGS__,debug_6,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__) #else #define debug(...) #endif #define rep(index,num) for(int index=0;index<(int)num;index++) #define rep1(index,num) for(int index=1;index<=(int)num;index++) #define brep(index,num) for(int index=(int)num-1;index>=0;index--) #define brep1(index,num) for(int index=(int)num;index>0;index--) #define scan(argument) cin>>argument #define prin(argument) cout<<argument<<endl #define prind(argument) cout<<std::fixed<<setprecision(13)<<argument<<endl #define kaigyo cout<<endl #define eps 1e-7 #define PI acosl(-1) #define mp(a1,a2) make_pair(a1,a2) #define ALL(a) (a).begin(),(a).end() #define rALL(a) (a).rbegin(),(a).rend() #define puba emplace_back #define pubamp(a,b) emplace_back(mp(a,b)) typedef long long ll; typedef long double ld; using namespace std; //using namespace atcoder; typedef pair<ll,ll> pll; typedef pair<int,int> pint; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<vint> vvint; typedef vector<vll> vvll; typedef vector<pint> vpint; typedef vector<pll> vpll; 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; } ll INFl=(ll)1e+18+1; int INF=1e+9+1; int main(){ int N; int L[2001],R[2001]; scan(N); rep(i,N){ int t,l,r; scan(t>>l>>r); l*=2,r*=2; if(t<=2) L[i]=l; else L[i]=l+1; if(t%2) R[i]=r; else R[i]=r-1; } int ans=0; rep(i,N){ for(int j=i+1;j<N;j++){ //debug(L[i],R[i],L[j],R[j]); if(R[i]<L[j]||R[j]<L[i]){ } else ans++; } } prin(ans); return 0; }
/*君は今なにをして生きる 君は今なにをして生きてる? いつまでたっても君は相も変わらず暮らしているようで 本音 深い暗い沼の底で 溜まりに溜まって溢れそうです 光景 あの時の思い出 美化に美化 重ねてしまった いいないいなあの子は楽しそう 嫌いなあの子は わたしはわたしでがんばった あなたはあなたで何していた? 気づいた時には粉々の鏡 あなたにきづいてほしかったな まだかな まだかな 待っていたのに 待っていたのに 愛していたのに 愛していたのに 永遠の愛はメーデーの中 永遠の愛はたしかにどこにももうない*/ #include <bits/stdc++.h> #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cout << (#x) << " : " << x << endl; #define ll long long #define ull unsigned long long #define ld long double #define pow power #define mp make_pair #define ff first #define ss second #define pii pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define siz(a) int((a).size()) #define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i)) #define endl "\n" #define pi 3.14159265 const ll mod = 1000 * 1000 * 1000 + 7; const ll mod1 = 998244353; const ll INF = 1ll*1000*1000*1000*1000*1000*1000 + 7; //typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; using namespace std; ll power(ll x, ll y) { ll res = 1; while (y > 0) { if (y & 1) res = (long long)(res*x); y = y>>1; x = (long long)(x*x); //cout<<x<<'\n'; } return res; } // Stolen Templates template <typename T> T MIN(T first) { return first; } template <typename T, typename... Args> T MIN(T first, Args... args) { return min(first, MIN(args...)); } template <typename T> T MAX(T first) { return first; } template <typename T, typename... Args> T MAX(T first, Args... args) { return max(first, MAX(args...)); } // Stolen Templates end here int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif fastio(); ll n; cin>>n; ll a[n][n]; ll sum = 0; For(i,0,n) { For(j,0,n) { cin>>a[i][j]; sum+=a[i][j]; } } // if (sum%n!=0) { // cout<<"No"; // return 0; // } vector <pii> val; ll cu = a[0][0]; ll pos = 0; for(int j=1;j<n;j++){ ll cur = a[0][j-1] - a[0][j]; for(int i=1;i<n;i++) { if (cur!=a[i][j-1]-a[i][j]) { cout<<"No"; return 0; } } if (a[j][0] < cu) { cu = a[j][0]; pos = j; } } cout<<"Yes"<<endl; for(int i = 0; i < n; i++) cout<<a[i][i] - a[pos][i]<<' '; cout<<endl; for(int i = 0; i < n; i++) cout<<a[pos][i]<<' '; return 0; } // check all product based operations for integer overflow // careful of renamed variables especially in loops
#include <bits/stdc++.h> using namespace std; struct BIT{ vector<long long> num; int N; BIT(int n){ N = n; num.resize(N + 1, 0); } long long sum(long long t){ long long res = 0; while(t > 0){ res += num[t]; t -= t & -t; } return res; } void add(int ind, long long t){ ind++; while(ind <= N){ num[ind] += t; ind += ind & -ind; } } }; int main(){ int N; cin >> N; vector<int> P(N); for(int i = 0; i < N; i++) { cin >> P[i]; P[i]--; } BIT bit(N); vector<int> ind(N); for(int i = 0; i < N; i++) ind[i] = i; sort(ind.begin(), ind.end(), [&](int x, int y){ return P[x] < P[y]; }); long long sum1 = 0; for(int i = 0; i < N; i++){ bit.add(ind[i], 1); sum1 += bit.sum(N) - bit.sum(ind[i] + 1); } if(sum1 != N - 1){ cout << -1 << endl; return 0; } vector<int> ans; set<int> s; int ind2 = N - 1; while(1){ if(ind2 < 0) break; int tem = ind[ind2]; for(int i = 0; i < ind2 - tem; i++){ ans.push_back(ind[ind2]); if(s.count(ind[ind2])){ cout << -1 << endl; return 0; } s.insert(ind[ind2]); swap(ind[ind2], ind[P[ind[ind2] + 1]]); } ind2--; } for(int i = 0; i < (int)ans.size(); i++){ cout << ans[i] + 1 << endl; } }
#include<bits/stdc++.h> #define For(i,x,y) for (register int i=(x);i<=(y);i++) #define FOR(i,x,y) for (register int i=(x);i<(y);i++) #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) #define Debug(v) for (auto i:v) cout<<i<<" ";puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define fil(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pa; typedef pair<ll,ll> PA; typedef vector<int> poly; inline ll read(){ ll x=0,f=1;char c=getchar(); while ((c<'0'||c>'9')&&(c!='-')) c=getchar(); if (c=='-') f=-1,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } const int N = 2e5+10; int n,a[N],pos[N]; bool vis[N]; poly ans; inline void Swap(int x,int y){ if (vis[x]){ puts("-1"); exit(0); } vis[x]=1,swap(pos[a[x]],pos[a[y]]),swap(a[x],a[y]); ans.pb(x); } int main(){ n=read(); For(i,1,n) a[i]=read(),pos[a[i]]=i; FOR(i,1,n){ while (a[i]!=i) Swap(pos[i]-1,pos[i]); } if (siz(ans)!=n-1) return puts("-1"),0; For(i,1,n-1) printf("%d\n",ans[i-1]); }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define fr(i, a, b) for (ll i = a; i < b; i++) #define rf(i, a, b) for (ll i = a; i >= b; i--) typedef std::vector<long long> vi; #define F first #define S second #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mod 1000000007 #define PB push_back #define MP make_pair #define PI 3.14159265358979323846 #define all(a) a.begin(), a.end() #define mx(a) *max_element(all(a)) #define mn(a) *min_element(all(a)) const ll INF = LLONG_MAX / 2; const ll N = 2e5 + 1; using namespace std; ll n, maxid = 0, m; ll pow1(ll n, ll p) { if (p == 0) return 1; ll x = pow1(n, p / 2); if (x > m || x >= 1000000001) return INF; x = (x * x); if (x > m) return INF; if (p % 2 == 0) return x; else { if (x * n > m) return INF; return (x * n); } } string x; bool check(ll k) { ll j = 0, ans = 0; rf(i, n - 1, 0) { if (pow1(k, j) == INF) return false; if (pow1(k, j) + ans > m || ans > m) return false; ans += (pow1(k, j) * ((ll)(x[i] - '0'))); j++; } if (ans > m) return false; return true; } int main() { fast; ll t = 1; // std::cin >> t; while (t--) { cin >> x; n = x.length(); cin >> m; if (x.length() == 1) { ll c = x[0] - '0'; if (m < c) cout << 0 << "\n"; else cout << 1 << "\n"; continue; } fr(i, 0, n) maxid = max(maxid, (ll)(x[i] - '0')); ll l = maxid + 1, r = m + 1; // cout << l << "\n"; // cout << maxid << "\n"; while (l <= r) { ll mid = (l + r) / 2; if (check(mid)) { l = mid + 1; } else { r = mid - 1; } } cout << l - 1 - maxid << "\n"; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define pii pair<int,int> #define fi first #define se second #define pb push_back #define rep(i,n) for (int i=1;i <=int(n);i++) #define ffor(i,n) for (int i=0;i <int(n);i++) #define all(x) x.begin(),x.end() //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //use rng() for random pii a[200005]; pii b[200005]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; rep(i,n){ cin >> a[i].fi >> b[i].fi; a[i].se = i ; b[i].se = i;} map<pii,int> cnt; sort(a+1,a+1+n); sort(b+1,b+1+n); int res = a[n].fi- a[1].fi; vector<pair<ll,pii>> ans ; int res1 = min(a[n].se,a[1].se); int res2 = max(a[n].se,a[1].se); ans.pb({res,{res1,res2}}); res = a[n-1].fi - a[1].fi; res1 = min(a[n-1].se,a[1].se); res2 = max(a[n-1].se,a[1].se); ans.pb({res,{res1,res2}}); res = a[n].fi - a[2].fi; res1 = min(a[n].se,a[2].se); res2 = max(a[n].se,a[2].se); ans.pb({res,{res1,res2}}); swap(a,b); res = a[n].fi- a[1].fi; res1 = min(a[n].se,a[1].se); res2 = max(a[n].se,a[1].se); ans.pb({res,{res1,res2}}); res = a[n-1].fi - a[1].fi; res1 = min(a[n-1].se,a[1].se); res2 = max(a[n-1].se,a[1].se); ans.pb({res,{res1,res2}}); res = a[n].fi - a[2].fi; res1 = min(a[n].se,a[2].se); res2 = max(a[n].se,a[2].se); ans.pb({res,{res1,res2}}); sort(all(ans)); reverse(all(ans)); int num=0; for (auto p: ans){ int a1 = min(p.se.fi,p.se.se); int a2 = max(p.se.fi,p.se.se); if (cnt[{a1,a2}]) continue; cnt[{a1,a2}]++; num++; if (num==2){ cout << p.fi << '\n'; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long void solve(){ int n; cin>>n; vector<int> v; for(int i = 1; i*i <= n; i++){ if(n%i == 0){ v.push_back(i); if(i*i != n) v.push_back(n/i); } } sort(v.begin(), v.end()); for(int i : v) cout << i << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; //cin>>t; while(t--) solve(); return 0; }
// Problem: C - Secret Number // Contest: AtCoder - Mynavi Programming Contest 2021(AtCoder Beginner Contest 201) // URL: https://atcoder.jp/contests/abc201/tasks/abc201_c // Memory Limit: 1024 MB // Time Limit: 2000 ms #include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int ans = 0; for (int i = 0; i < 10000; ++i) { bool flag = true; for (int k = 1; k <= 1000; k *= 10) { if (str[i / k % 10] == 'x') flag = false; } for (int j = 0; j < 10; ++j) { bool vis = false; if (str[j] == 'o') { for (int k = 1; k <= 1000; k *= 10) { if (i / k % 10 == j) vis = true; } if (!vis) flag = false; } } if (flag) ++ans; } cout << ans; return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } #define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0) int A, B; void MAIN() { scanf("%d%d", &A, &B); if (A+B >= 15 && B >= 8) { puts("1"); } else if (A+B >= 10 && B >= 3) { puts("2"); } else if (A+B >= 3) { puts("3"); } else { puts("4"); } } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define inf INT_MAX #define llinf LLONG_MAX #define mt make_tuple #define mp make_pair #define parll pair<ll,ll> #define parii pair<int,int> #define min3( brp1, brp2, brp3 ) min( min( brp1, brp2 ), brp3 ) #define min4( brp1, brp2, brp3, brp4 ) min( min( brp1, brp2 ), min( brp3, brp4 ) ) ll a,b; int main() { ios; cin >> a >> b; if ( a+b >= 15 && b >= 8 ) { cout << 1; return 0; } if ( a+b >= 10 && b >= 3 ) { cout << 2; return 0; } if ( a+b >= 3 ) { cout << 3; return 0; } cout << 4; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define int long long int #define pb push_back #define mp make_pair #define pll pair<ll, ll> #define vll vector<ll> #define mll map<ll, ll> #define setbits(x) __builtin_popcountll(x) #define md (ll)1e9 + 7 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define f(i, j, n) for (ll i = j; i < n; i++) #define r(i, j, n) for (ll i = n - 1; i >= j; i--) void solve() { ll b,c; cin>>b>>c; ll p=c,n=c-1; if(b<0) swap(p,n); else if(b==0) p=c,n=c; b=abs(b); ll l1=b+(n-1)/2; ll r1=b-p/2; ll l2=-(b-(p-1)/2); ll r2=-b-n/2; ll ans=l1-r1+1 + l2-r2+1; ans-=max(0ll,l2-r1+1); cout<<ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("inpput.txt", "r", stdin); //freopen("op.txt", "w", stdout); #endif ll test_cases = 1; //cin >> test_cases; for (ll t = 1; t <= test_cases; t++) solve(),cout<<"\n";; //cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n"; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <set> #include <sstream> #include <string> #include <tuple> #include <type_traits> #include <unordered_set> #include <utility> #include <vector> using namespace std; using i64 = int64_t; using vi64 = vector<i64>; using vvi64 = vector<vi64>; using vvvi64 = vector<vvi64>; using u64 = uint64_t; #define rep(i, s, n) for (i64 i = (s); i < (n); ++i) void run() { i64 n; cin >> n; vi64 as(n); rep(i, 0, n) { cin >> as[i]; } sort(begin(as), end(as)); i64 ret{0}, pre{0}; rep(i, 1, n) { pre += (as[i] - as[i - 1]) * i; ret += pre; } cout << ret << endl; } int main() { cin.tie(nullptr); cout.precision(std::numeric_limits<double>::max_digits10); run(); return 0; }
// #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long int; using int64 = long long int; template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int INF = 1LL << 29; const ll LONGINF = 1LL << 60; const ll MOD = 1000000007LL; bool dp[200010][10]; bool visited[200010][10]; int main() { int N; cin >> N; string S, X; cin >> S >> X; auto calc = [&](auto &&f, int idx, int mod) { if(visited[idx][mod]) return dp[idx][mod]; visited[idx][mod] = true; if(idx + 1 == N) { int m1 = (mod * 10 + 0) % 7; int m2 = (mod * 10 + (S[idx] - '0')) % 7; if(X[idx] == 'T' and (m1 == 0 or m2 == 0)) return dp[idx][mod] = true; if(X[idx] == 'A' and (m1 != 0 or m2 != 0)) return dp[idx][mod] = true; return dp[idx][mod] = false; } else { int m1 = (mod * 10 + 0) % 7; int m2 = (mod * 10 + (S[idx] - '0')) % 7; bool b1 = f(f, idx+1, m1); bool b2 = f(f, idx+1, m2); if(X[idx] == X[idx+1] and (b1 or b2)) { return dp[idx][mod] = true; } else if(X[idx] != X[idx+1] and (!b1 or !b2)) { return dp[idx][mod] = true; } return dp[idx][mod] = false; } }; bool a = calc(calc, 0, 0); if(X.front() == 'T') { cout << (a ? "Takahashi" : "Aoki") << endl; } else { cout << (a ? "Aoki" : "Takahashi") << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "/debug.h" #else #define db(...) #endif #define all(v) v.begin(), v.end() #define pb push_back using ll = long long; const int NAX = 2e5 + 5, MOD = 1000000007; string solveCase(int64_t N, string S, string X) { // TODO: edit here vector<int> win_t(7); win_t[0] = 1; reverse(all(S)); reverse(all(X)); int curr = 1; for (size_t i = 0; i < N; i++) { int val = ((S[i] - '0') * curr) % 7; vector<int> win_next_t(7); for (size_t j = 0; j < 7; j++) { if (X[i] == 'T') { int now = (j + val) % 7; if (win_t[now]) win_next_t[j] = 1; else win_next_t[j] = win_t[j]; } else { int now = (j + val) % 7; if (!win_t[now]) win_next_t[j] = 0; else win_next_t[j] = win_t[j]; } } curr = (curr * 10) % 7; win_t = win_next_t; db(i, win_t, curr, val); } if (win_t[0]) return "Takahashi"; return "Aoki"; } // generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator) int main() { #ifndef LOCAL std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #endif constexpr char endl = '\n'; int N; string X, S; cin >> N >> S >> X; auto ans = solveCase(N, S, X); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define db double #define ld long double #define forr(i, a, n) for (int i = a; i < n; i++) #define rep(i, n) forr(i, 0, n) #define repp(i, n) forr(i, 1, n + 1) #define foreach(it, t) for (__typeof(t.begin()) it = t.begin(); it != t.end(); it++) #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() #define init(a, i) memset(a, i, sizeof(a)) #define pii pair<int, int> #define vvi vector<vector<int>> #define MAXN 0x3f3f3f3f int min(int a, int b) { if (a < b) return a; else return b; } int max(int a, int b) { if (a > b) return a; else return b; } int n, p, q, u, d[200005], cur = -1, B; int l[200005], r[200005]; int deep[200005], num[200005], cnt[200005], ans[200005]; vvi v; vector<pair<pii, int>> s; void dfs(int k, int dp) { cur++; deep[cur] = dp; num[k] = cur; l[num[k]] = cur; rep(i, v[k].size()) dfs(v[k][i], dp + 1); r[num[k]] = cur; } bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { int a1 = a.first.first, b1 = a.first.second, a2 = b.first.first, b2 = b.first.second; if (a1 / B != a2 / B) return a1 / B < a2 / B; if (b1 != b2) return b1 < b2; return a1 < a2; } void solve() { cin >> n; B = sqrt(n); v.resize(n); rep(i, n - 1) { cin >> p; v[p - 1].pb(i + 1); } dfs(0, 0); cin >> q; rep(i, q) { cin >> u >> d[i]; u--; u = num[u]; s.pb(mp(mp(l[u], r[u]), i)); } sort(s.begin(), s.end(), cmp); int x = 0, y = -1; rep(i, q) { int L = s[i].first.first, R = s[i].first.second; while (y < R) cnt[deep[++y]]++; while (y > R) cnt[deep[y--]]--; while (x < L) cnt[deep[x++]]--; while (x > L) cnt[deep[--x]]++; ans[s[i].second] = cnt[d[s[i].second]]; } rep(i, q) cout << ans[i] << endl; } int main() { int T; T = 1; //cin>>T; while (T--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long double dp[100][100][100]; double dfs(ll a,ll b,ll c){ if(a==99 && b==99 && c==99)return 1.0; if(a==100 || b==100 || c==100)return 0.0; if(dp[a][b][c]!=0)return dp[a][b][c]; double ans=0.0; if(a>0)ans+=((double)a/(a+b+c))*(1+dfs(a+1,b,c)); if(b>0)ans+=((double)b/(a+b+c))*(1+dfs(a,b+1,c)); if(c>0)ans+=((double)c/(a+b+c))*(1+dfs(a,b,c+1)); //cout<<a<<" "<<b<<" "<<c<<" "<<ans<<endl; return dp[a][b][c]=ans; } int main(){ ll a,b,c; cin>>a>>b>>c; memset(dp,0,sizeof(dp)); double ans=dfs(a,b,c); cout<<fixed<<setprecision(10)<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i = (m); i <= (n); i++) #define zep(i,m,n) for(ll i = (m); i < (n); i++) #define rrep(i,m,n) for(ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; char c[2][2]; cin >> c[0][0] >> c[0][1] >> c[1][0] >> c[1][1]; if(n <= 3){print(1) return 0;} ll x = 1; zep(i, 0, n - 3){x *= 2; x %= MOD;} ll dp[n][2]; memset(dp, 0, sizeof(dp)); dp[0][0] = 1; zep(i, 1, n - 1){ dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % MOD; dp[i][1] = dp[i - 1][0]; } if(c[0][1] == 'A'){ if(c[0][0] == 'A'){ print(1) }else{ if(c[1][0] == 'B'){ print(x) }else{ print(dp[n - 2][0]) } } }else{ if(c[1][1] == 'B'){ print(1) }else{ if(c[1][0] == 'A'){ print(x) }else{ print(dp[n - 2][0]) } } } return 0; }
#include<bits/stdc++.h> #define pb emplace_back #define AI(i) begin(i), end(i) using namespace std; using ll = long long; template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); } template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); } #ifdef KEV #define DE(args...) kout("[ " + string(#args) + " ] = ", args) void kout() {cerr << endl;} template<class T, class ...U> void kout (T v, U ...e) { cerr << v << ' ', kout(e...); } template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; } #else #define DE(...) 0 #define debug(...) 0 #endif // What I should check // 1. overflow // 2. corner cases // Enjoy the problem instead of hurrying to AC // Good luck ! const int MAX_N = 5005, p = 998244353; char w[MAX_N][MAX_N]; int n, m, k; ll dp[MAX_N][MAX_N]; constexpr ll bin_pow(ll v, ll t) { ll res = 1; for (;t;t>>=1, v = v * v % p) if (t&1) res = res * v % p; return res; } constexpr ll inv(ll v) { return bin_pow(v, p - 2); } const int i3 = inv(3); int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> m >> k; for (int x, y, c, i = 0;i < k;++i) { cin >> x >> y; cin >> w[x][y]; } if (w[n][m] == 0) { w[n][m] = 'X'; } dp[1][1] = bin_pow(3, n * m - k); DE(i3 * 3 % p); for (int i = 1;i <= n;++i) for (int j = 1;j <= m;++j) { dp[i][j] %= p; if (w[i][j] == 'R') dp[i][j + 1] += dp[i][j]; if (w[i][j] == 'D') dp[i + 1][j] += dp[i][j]; if (w[i][j] == 'X') { dp[i][j + 1] += dp[i][j]; dp[i + 1][j] += dp[i][j]; } if (w[i][j] == 0) { dp[i][j] = dp[i][j] * i3 % p; dp[i + 1][j] += 2 * dp[i][j]; dp[i][j + 1] += 2 * dp[i][j]; } //DE(i, j, dp[i][j]); } cout << dp[n][m] << '\n'; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; ll calcNumOfCombination(ll n, ll r){ ll num = 1; for(ll i = 1; i <= r; i++){ num = num * (n - i + 1) / i; } return num; } int main() { int a, b; ll k; cin >> a >> b >> k; k--; string ans; ll now = 0; int ua = 0, ub = 0; rep(i, a+b) { if(ub == b) { ans.push_back('a'); continue; } else if(ua == a) { ans.push_back('b'); continue; } int z = a+b - (i+1); ll p = calcNumOfCombination(z, a-ua-1); // cerr << i << " " << p << " " << now << endl; if(now+p <= k) { now += p; ans.push_back('b'); ub++; } else { ans.push_back('a'); ua++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int mem[40][40]; void comb(vector<vector<long long int> > &v) { for (int i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (int k = 1; k < v.size(); k++) { for (int j = 1; j < k; j++) { v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]); } } } int main() { memset(mem, -1, sizeof(mem)); vector<vector<long long int> > v(63, vector<long long int>(63, 0)); comb(v); long long int a, b, k; cin >> a >> b >> k; int l = a + b; for (int i = 0; i < l; i++) { long long int c = v[a + b - 1][b]; if (c >= k) { cout << 'a'; a--; } else { k -= c; cout << 'b'; b--; } } cout << endl; }
#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 main(){ int n; double D,H; cin>>n>>D>>H; double ans=0; rep(i,n){ double d,h; cin>>d>>h; double tmp=(D*h-d*H)/(D-d); ans=max(ans,tmp); } cout<<setprecision(10)<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(void) { vector<int> A(3); rep(i, 3) cin >> A.at(i); sort(A.begin(), A.end()); do { if (A.at(0) - A.at(1) == A.at(1) - A.at(2)) { cout << "Yes" << endl; return 0; } } while (next_permutation(A.begin(), A.end())); cout << "No" << endl; }
#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, A[1010], B[1010]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M; rep(i, 0, N) cin >> A[i]; rep(i, 0, M) cin >> B[i]; map<int, int> cnt; rep(i, 0, N) cnt[A[i]]++; rep(i, 0, M) cnt[B[i]]++; bool top = true; fore(p, cnt) if (p.second == 1) { if (!top) printf(" "); printf("%d", p.first); top = false; } printf("\n"); }
#include <bits/stdc++.h> #define rep(i, n) for (lli i = 0; i < (n); i++) #define rrep(i, n) for (lli i = (n)-1; i >= 0; i--) #define vall(x) (x).begin(), (x).end() using namespace std; namespace atcoder {} using namespace atcoder; using lli = long long int; void YESNO(bool), YesNo(bool); template <class T1, class T2> bool chmin(T1 &l, const T2 &r); template <class T1, class T2> bool chmax(T1 &l, const T2 &r); #define int long long int void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B) { vector<int> count(10000); for (auto c : A) count[c]++; for (auto c : B) count[c]++; rep(i, 10000) { if (count[i] == 1) { cout << i << " "; } } } signed main() { long long N; scanf("%lld", &N); long long M; scanf("%lld", &M); std::vector<long long> A(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } std::vector<long long> B(M); for (int i = 0; i < M; i++) { scanf("%lld", &B[i]); } solve(N, M, std::move(A), std::move(B)); return 0; } // -- lib void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } template <class T1, class T2> bool chmin(T1 &l, const T2 &r) { return (l > r) ? (l = r, true) : false; } template <class T1, class T2> bool chmax(T1 &l, const T2 &r) { return (l < r) ? (l = r, true) : false; } template <class T1, class T2> void vadd(vector<T1> &v, T2 x) { for (auto &s : v) s += T2(x); }
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define IT iterator #define V vector #define TP template <class o> #define TPP template <typename t1, typename t2> #define SZ(a) ((int)a.size()) #define all(a) a.begin(), a.end() #define rep(i, a, b) for (int i = a; i <= b; i++) #define REP(i, a, b) for (int i = b; i >= a; i--) #define FOR(i, n) rep(i, 1, n) #define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl using namespace std; typedef double db; typedef unsigned ui; typedef long long ll; typedef long double ld; // char buf[1 << 20],*p1=buf,*p2=buf; TP void qr(o& x) { char c = gc; x = 0; int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = gc; } while (isdigit(c)) x = x * 10 + c - '0', c = gc; x *= f; } template <class o, class... O> void qr(o& x, O&... y) { qr(x), qr(y...); } TP void qw(o x) { if (x < 0) putchar('-'), x = -x; if (x / 10) qw(x / 10); putchar(x % 10 + '0'); } TP void pr1(o x) { qw(x), putchar(' '); } template <class o, class... O> void pr1(o x, O... y) { pr1(x), pr1(y...); } TP void pr2(o x) { qw(x), putchar(10); } template <class o, class... O> void pr2(o x, O... y) { pr2(x), pr2(y...); } TP bool cmax(o& x, o y) { return (x < y ? x = y, 1 : 0); } TP bool cmin(o& x, o y) { return (x > y ? x = y, 1 : 0); } const int mod = 998244353; TPP void ad(t1& x, t2 y) { (x += y) >= mod && (x -= mod); } TPP void dl(t1& x, t2 y) { (x -= y) < 0 && (x += mod); } const int N = 2e5 + 10, inf = 2e9; const ll INF = 1e15; int n, a[N], b[N]; void solve() { qr(n); int x = 0, y = 1000; FOR(i, n) qr(a[i]), cmax(x, a[i]); FOR(i, n) qr(b[i]), cmin(y, b[i]); pr2(max(y - x + 1, 0)); } int main() { #ifndef ONLINE_JUDGE clock_t start_time = clock(); #endif int T = 1; // qr(T); while (T--) solve(); #ifndef ONLINE_JUDGE cerr << 1.0 * (clock() - start_time) / CLOCKS_PER_SEC << ' ' << 's' << endl; #endif return 0; }
#include <bits/stdc++.h> #define SLAY ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define PI 3.141592653589793238 #define ll long long #define pii pair<int , int> #define pll pair<long long , long long> using namespace std; int _=1 , tc=1; int dx[] = { 0 , 0 , 1 , -1}; int dy[] = { 1 , -1 , 0 , 0}; const int MAXN = 1e7 + 1; int spf[MAXN]; int d,x,y; void sieve(){ for(int i = 1 ; i<MAXN ; i++)spf[i]=i; for(int i = 4 ; i<MAXN ; i+=2)spf[i]=2; for(int i = 3; i*i<MAXN; i+=2){ if(spf[i]==i){ for(int j = i*i ; j<MAXN ; j+=i){ if(spf[j]==j)spf[j]=i; } } } } void extended_gcd(int a , int b){ if(b==0){ d=a; x=1; y=0; }else{ extended_gcd(b,a%b); int temp = y; y = x - y * (a/b); x = temp; } } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return a * (b / gcd(a, b)); } int getmod(int x, int y , int mod ){return (((x - y)%mod)+mod)%mod;} vector<string> split(string line, char dil){ vector <string> tokens; stringstream check1(line); string intermediate; while(getline(check1, intermediate,dil)){ tokens.push_back(intermediate); } return tokens; } void read(vector<ll>&v){ for(int i =0;i<v.size();i++)cin>>v[i]; } void solve(){ ll y,x,a,b; cin>>x>>y>>a>>b; ll ans = 0; while( (double)a*x<=2e18 && x*a<=x+b && x*a<y){ ans++; x *= a; } ans += (y-x-1)/b; cout<<ans<<endl; } int main(){ SLAY while(_--){ solve(); } return 0; } // 2.2360679775 // 2.236067977499790
#include<bits/stdc++.h> using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' //#define vec vector<ll> //#define mat vector<vector<ll> > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; //typedef long double ld; typedef complex<double> Complex; const ll INF=1e9+7; const ll MOD=998244353; const ll inf=INF*INF; const ll mod=INF; const ll MAX=200010; const double PI=acos(-1.0); //dijkstra(O(ElogV)) //0-indexed void dijkstra(ll n,ll s,vector<vector<pll> >&G,vector<ll> &d){ priority_queue<pll,vector<pll>,greater<pll> >pq; rep(i,n){ d[i]=inf; } d[s]=0; pq.push(mp(0,s)); while(!pq.empty()){ pll k=pq.top();pq.pop(); ll v=k.second; if(d[v]<k.first)continue; for(auto e:G[v]){ if(e.fi<0)continue; if(d[e.first]>d[v]+e.second){ d[e.first]=d[v]+e.second; pq.push(mp(d[e.first],e.first)); } } } } ll dx[]={0,1,0,-1}; ll dy[]={1,0,-1,0}; signed main(){ int n=30; int N=n*n; int T=1000; vector<vector<pll> >G(N); rep(i,n){ rep(j,n){ ll v=i*n+j; rep(k,4){ ll nx=i+dx[k],ny=j+dy[k]; ll nv=nx*n+ny; ll x=1; if(nx<0||nx>=n||ny<0||ny>=n){ G[v].pb(mp(-1,x)); }else{ G[v].pb(mp(nv,x)); } } } } vector<ll>dist(N); while(T--){ ll a,b,c,d;cin>>a>>b>>c>>d; string res=""; ll s=a*n+b,t=c*n+d; ll nxt=t; dijkstra(N,s,G,dist); ll x=dist[t]; vector<ll>used(N); while(nxt!=s){ used[nxt]=1; ll idx=-1; rep(i,N){ if(used[i])continue; ll c=inf; for(auto e:G[i]){ if(e.fi==nxt)c=e.se; } if(dist[i]+c==dist[nxt])idx=i; } if(nxt-idx==1){ res+='R'; } if(nxt-idx==-1){ res+='L'; } if(nxt-idx==n){ res+='D'; } if(nxt-idx==-n){ res+='U'; } nxt=idx; } reverse(all(res)); cout<<res<<endl; ll score;cin>>score; ll mean=score/(ll)res.size(); rep(i,res.size()){ if(res[i]=='R'){ G[s][0].se=mean; G[s+1][2].se=mean; s++; } if(res[i]=='L'){ G[s][2].se=mean; G[s-1][0].se=mean; s--; } if(res[i]=='U'){ G[s][3].se=mean; G[s-n][1].se=mean; s-=n; } if(res[i]=='D'){ G[s][1].se=mean; G[s+n][3].se=mean; s+=n; } } } }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, from, to) for (ll i = from; i < (to); ++i) #define mp(x,y) make_pair(x,y) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define pb push_back using ll = long long; using ld=long double; using vin=vector<int>; using vvin=vector<vin>; using vll=vector<ll>; using vvll=vector<vll>; using vst=vector<string>; using P = pair<ll,ll>; using vp=vector<P>; using vvp=vector<vp>; const int inf=1e9+7; const ll INF=9e18/2; const long double PI = acosl(-1.0); template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;} template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;} template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; template<typename T> struct BIT{ int N; vector<T> dat; BIT(int siz):N(siz+1),dat(siz+1,0){} void add(int i,T x){ for(int idx=i;idx<N;idx+=(idx & -idx)){ dat[idx]+=x; } } T sum(int i){ T res=0; for(int idx=i;idx>0;idx-=(idx & -idx)){ res+=dat[idx]; } return res; } }; int main(){//cout<<fixed<<setprecision(20); long long n; cin>>n; BIT<long long> bit(n); vll a(n); rep(i,0,n)cin>>a[i]; ll ans=0; rep(i,0,n){ a[i]++; ans+=bit.sum(n)-bit.sum(a[i]); bit.add(a[i],1); } cout<<ans<<endl; rep(i,0,n-1){ ans+=n+1-2*a[i]; cout<<ans<<endl; } }
#include <iostream> #include <iostream> #include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define ll long long #define li longll #define pp pair<ll, ll> #define lb lower_bound #define ub upper_bound #define mk make_pair #define pb push_back #define llp(i, x, n) for (ll i = x; i < n; i++) #define fr first #define ss second #define yes cout << "YES" << endl #define no cout << "NO" << endl #define mod 1000000007 #define all(x) x.begin(), x.end() using namespace std; int main() { fast; int n; cin>>n; string s; cin>>s; int t; cin>>t; int c=0; string s1=s.substr(0,n); string s2=s.substr(n,n); while(t--) { int x,a,b; cin>>x>>a>>b; if(x==1) { a--,b--; if(a<n && b<n) swap(s1[a],s1[b]); else if(a>=n && b>=n) swap(s2[a-n],s2[b-n]); else swap(s1[a],s2[b-n]); } else { swap(s1,s2); } } cout<<s1+s2<<endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; //required using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define debug(x) cout << #x << " is " << x << endl #define yes cout << "YES" << endl #define no cout << "NO" << endl typedef long long ll; const int MOD = 1e9 + 7; int dp[200005][7]; int main(){ fast; int n; cin >> n; string s, x; cin >> s >> x; // 1: Takahashi -1: Aoki for (int i = 0; i < 7; ++i){ if (i){ dp[n][i] = -1; } else{ dp[n][i] = 1; } } int num, turn, mod1, mod2; for (int i = n - 1; i >= 0; --i){ num = s[i] - '0'; turn = x[i] == 'T' ? 1 : 0; // 1: Takahashi 0: Aoki for (int k = 0; k < 7; ++k){ mod1 = (k * 10) % 7; mod2 = (mod1 + num) % 7; if (turn){ // T if (dp[i+1][mod1] == 1 or dp[i+1][mod2] == 1){ dp[i][k] = 1; } else{ dp[i][k] = -1; } } else{ // A if (dp[i+1][mod1] == -1 or dp[i+1][mod2] == -1){ dp[i][k] = -1; } else{ dp[i][k] = 1; } } } } if (dp[0][0] == 1){ cout << "Takahashi" << endl; } else{ cout << "Aoki" << endl; } return 0; }
#include <iostream> using namespace std; int main() { int N; cin >> N; int x[N]; int y[N]; for(int i = 0; i < N; i++) { cin >> x[i] >> y[i]; } for(int i = 0; i < N-2; i++) { for(int j = i+1; j < N-1; j++) { for(int k = j+1; k < N; k++) { int x_diff, y_diff; int x_diff2, y_diff2; if(x[i] < x[j] && x[i] < x[k]) { x_diff = x[j] - x[i]; y_diff = y[j] - y[i]; x_diff2 = x[k] - x[i]; y_diff2 = y[k] - y[i]; } else if(x[j] < x[k]) { x_diff = x[i] - x[j]; y_diff = y[i] - y[j]; x_diff2 = x[k] - x[j]; y_diff2 = y[k] - y[j]; } else { x_diff = x[i] - x[k]; y_diff = y[i] - y[k]; x_diff2 = x[j] - x[k]; y_diff2 = y[j] - y[k]; } if(x_diff == 0 && x_diff2 == 0) { // cout << "x:" << x_diff << ", " << x_diff2 << endl; // cout << i << "," << j << "," << k << endl; cout << "Yes" << endl; return 0; } else if(y_diff == 0 && y_diff2 == 0) { // cout << "y:" << y_diff << ", " << y_diff2 << endl; // cout << i << "," << j << "," << k << endl; cout << "Yes" << endl; return 0; } else if(((float)x_diff2/x_diff) == ((float)y_diff2/y_diff)) { // cout << "x:" << x_diff << ", " << x_diff2 << endl; // cout << "y:" << y_diff << ", " << y_diff2 << endl; // cout << "i:" << ((float)x_diff2/x_diff) << ", " << ((float)y_diff2/y_diff) << endl; // cout << i << "," << j << "," << k << endl; cout << "Yes" << endl; return 0; } } } } cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() void fin(string s){ cout<<s<<endl; exit(0); } signed main(){ int n;cin>>n; vector<int> x(n),y(n); REP(i,n)cin>>x[i]>>y[i]; auto check=[&](int i,int j,int k){ if(i==j||j==k||k==i)return false; return (x[i]-x[j])*(y[i]-y[k])==(x[i]-x[k])*(y[i]-y[j]); }; REP(i,n)REP(j,n)REP(k,n)if(check(i,j,k))fin("Yes"); fin("No"); }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { int n; cin >> n; string t; cin >> t; string s = "110"; long long res = 0; REP(i, 3) { bool ok = true; REP(j, n) ok = ok && s[(i+j)%3] == t[j]; if (ok) res += 1e10 - (n+i+2)/3 + 1; } cout << res << '\n'; return 0; }
// #include <atcoder/all> #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = (n - 1); i >= 0; --i) #define BIT(n, k) ((n >> k) & 1) #define LEN(s) (int)s.size() #define RANGE(a) a.begin(), a.end() #define RRANGE(a) a.rbegin(), a.rend() using namespace std; using ll = int64_t; using P = pair<ll, ll>; template <class T> using V = vector<T>; int main() { int n; cin >> n; string s, t; cin >> s >> t; deque<int> idx1; rep(i, n) if (s[i] == '1') idx1.push_back(i); ll ans = 0; rep(i, n) { if (s[i] == '1') idx1.pop_front(); if (s[i] == t[i]) continue; if (s[i] == '1') { if (idx1.size() == 0) { ans = -1; break; } int j = idx1.front(); ans += j - i; s[j] = '0'; idx1.pop_front(); } else if (s[i] == '0') { if (idx1.size() == 0) { ans = -1; break; } int j = idx1.front(); ans += j - i; s[j] = '0'; idx1.pop_front(); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for (int i = 0; i < (n); ++i) template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;} template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;} using ll = long long; using P = pair<int,int>; using Pl = pair<long long,long long>; using veci = vector<int>; using vecl = vector<long long>; using vecveci = vector<vector<int>>; using vecvecl = vector<vector<long long>>; const int MOD = 1000000007; const double pi = acos(-1); ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);} ll lcm(ll a, ll b) {return a*b/gcd(a,b);} template<int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) {if (val < 0) val += MOD;} constexpr int getmod() { return MOD; } constexpr Fp operator - () const noexcept {return val ? MOD - val : 0;} constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept {val += r.val;if (val >= MOD) val -= MOD;return *this;} constexpr Fp& operator -= (const Fp& r) noexcept {val -= r.val;if (val < 0) val += MOD;return *this;} constexpr Fp& operator *= (const Fp& r) noexcept {val = val * r.val % MOD;return *this;} constexpr Fp& operator /= (const Fp& r) noexcept {long long a = r.val, b = MOD, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}val = val * u % MOD;if (val < 0) val += MOD;return *this;} constexpr bool operator == (const Fp& r) const noexcept {return this->val == r.val;} constexpr bool operator != (const Fp& r) const noexcept {return this->val != r.val;} friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {return os << x.val;} friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {if (n == 0) return 1;auto t = modpow(a, n / 2);t = t * t;if (n & 1) t = t * a;return t;} }; using mint = Fp<MOD>; int main() { int N; cin >> N; int K; cin >> K; veci A(N); REP(i,N) cin >> A[i]; sort(A.begin(),A.end()); vecveci Box(K); int now; int i = 0; while(i < N) { int j = i; now = A[i]; while(now == A[j]) { Box[(j-i)%K].push_back(now); j++; } i = j; } // REP(i,K) { // for(int x : Box[i]) cout << x << " "; // cout << endl; // } set<int> st; REP(i,N+1) st.insert(i); ll ans = 0; REP(i,K) { for(int x : Box[i]) if(st.count(x)) st.erase(x); auto itr = st.begin(); ans += *itr; for(int x : Box[i]) st.insert(x); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) #define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i) #define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define popcount __builtin_popcount using ll = long long; constexpr ll mod = 1e9 + 7; constexpr ll INF = 1LL << 60; // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> T mypow(T x, T n, const T &p = -1) { //x^nをmodで割った余り T ret = 1; while (n > 0) { if (n & 1) { if (p != -1) ret = (ret * x) % p; else ret *= x; } if (p != -1) x = (x * x) % p; else x *= x; n >>= 1; } return ret; } using namespace std; // using namespace atcoder; void solve() { ll n,k; cin>>n>>k; vector<ll>a(n); ll ans=0; map<ll,ll>mp; rep(i,n){ cin>>a[i]; mp[a[i]]++; } ll pre=min(k,mp[0]); REP(i,1,n){ if(pre>mp[i]){ ans+=1LL*(pre-mp[i])*i; pre=mp[i]; } } cout<<ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using tiii = tuple<int, int, int>; using str = string; using ld = long double; #define FOR(i,a,b) for(int i = (a);i < (b);i ++) #define ROF(i,a,b) for(int i = (a);i >= (b);i --) #define trav(a, b) for(auto& a : b) #define sz(a) int((a).size()) #define all(a) (a).begin(), (a).end() #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define mk make_pair #define mt make_tuple #define f first #define s second #define LOCAL #ifdef LOCAL #define nme(x) #x << " : " << x << ' ' #else #define cerr if(false)cerr #define nme(x) #x << " : " << x << ' ' #endif template<class T> void maxself(T& x, const T& y){x = (y > x ? y : x);} template<class T> void minself(T& x, const T& y){x = (y < x ? y : x);} const ld PI = 3.141592653589793238462643; const ll mod = 1000000007; struct mi{ ll val;explicit operator int() const {return val;} mi():val(0){} mi(ll v):val(v % mod){} }; mi operator+(const mi& a, const mi& b){return (a.val + b.val) % mod;} mi operator*(const mi& a, const mi& b){return (a.val * b.val) % mod;} mi operator-(const mi& a, const mi& b){return (a.val - b.val + mod) % mod;} const int N = 10000; int n, a[N], lef[N], rigt[N]; void solve(){ cin >> n; FOR(i,0,n){ cin >> a[i]; } vector<pii> st; FOR(i,0,n){ while(sz(st) && st.back().f >= a[i]){ st.pop_back(); } if(sz(st)) lef[i] = st.back().s; else lef[i] = -1; st.eb(a[i], i); } st.clear(); ROF(i,n - 1,0){ while(sz(st) && st.back().f >= a[i]) st.pop_back(); if(sz(st)) rigt[i] = st.back().s; else rigt[i] = n; st.eb(a[i], i); } int ans = 0; FOR(i,0,n){ maxself(ans, a[i] * (rigt[i] - lef[i] - 1)); } cout << ans << '\n'; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int T = 1; // cin >> T; while(T --) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pi; typedef vector<ll> vi; typedef vector<pi> vpi; typedef long double ld; const int inf = 1e9 + 7; const int mod = 998244353; const int maxn = 1e6 + 3; #define pb push_back #define mp make_pair #define ff first #define ss second #define rep(i, a, b) for(int i = a; i < b; i++) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define lb lower_bound #define ub upper_bound #define fact(n) rep(i, 1, n+1)ft.pb((ft[i-1]*i)%mod); #define mod_in(a) exp(a, mod-2) #define ncr(n, r) ((ft[n]*mod_in((ft[r]*ft[(n)-(r)])%mod))%mod) #define deb(x) cout << #x << " " << x << endl; #define flash ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl '\n' /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.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>; template<typename T> using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;*/ //cout << fixed << setprecision(0) << ans <<endl; ll prime[4000005]; ll exp(ll a,ll b); void sieve(ll n); ll gcd(ll a ,ll b); ll sod(ll x); ll countdigit(ll n); ll mySqrt(ll x); vector<ll>ft; int ans = 0; int findMin(int arr[], int n) { int sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; bool dp[n+1][sum+1]; for (int i = 0; i <= n; i++) dp[i][0] = true; for (int i = 1; i <= sum; i++) dp[0][i] = false; for (int i=1; i<=n; i++) { for (int j=1; j<=sum; j++) { dp[i][j] = dp[i-1][j]; // If i'th element is included if (arr[i-1] <= j) dp[i][j] |= dp[i-1][j-arr[i-1]]; } } int diff = INT_MAX; for (int j=sum/2; j>=0; j--) { // Find the if (dp[n][j] == true) { //cout << 2*j << " " << sum-j << endl; ans = max(j,sum-j); diff = sum-2*j; break; } } return diff; } int main() { flash; ll t=1; // sieve(4000003); //ft.pb(1); //fact(10000001); //cin >> t; while(t--) { int n; cin >> n; int v[n]; rep(i,0,n)cin >> v[i]; ll i = 0, j = n-1; int res = findMin(v,n); cout << ans << endl; } } // // // // // // IMPORTANT FUNCTIONS ll exp(ll a, ll b) { int res=1; while(b) { if(b&1) res=(res*a)%mod; a=(a*a)%mod; b>>=1; } return res%mod; } ll gcd(ll a,ll b){ if(a==0)return b; return gcd(b%a,a); } void sieve(ll n) { for(ll i=0;i<=n;i++) { prime[i]=1; } prime[0]=0;prime[1]=0; for(ll i=2;i<=n;i++) { if(prime[i]==1) { for(ll j=2;i*j<=n;j++) { prime[i*j]=0; } } } } ll countdigit(ll n) { return floor(log10(n) + 1); } ll sod(ll x){ ll s = 0; while (x != 0) { s = s + x % 10; x = x / 10; } return s; } ll mySqrt(ll x) { if (x == 0 || x == 1) { return x; } ll start = 1, end = x / 2; // standard binary search template while (start + 1 < end) { ll mid = start + (end - start) / 2; // why not mid * mid <= x? because it may cause overflow when mid is very large if (mid <= x / mid) { start = mid; } else { end = mid - 1; } } return end <= x / end ? end : start; }
#include <bits/stdc++.h> #define REP(i,n) for(ll i=0;i<(ll)(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++) #define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--) #define ALL(x) (x).begin(),(x).end() #define RALL(x) (x).rbegin(),(x).rend() #define SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) using namespace std; typedef long long ll; const int INF = 1000000000; vector<ll> res; vector<ll> enum_divisors(ll N) { for (ll i = 1; i * i <= N; ++i) { if (N % i == 0) { res.push_back(i); if (N/i != i) res.push_back(N/i); } } sort(res.begin(), res.end()); return res; } int main() { ll N; cin >> N; enum_divisors(2*N); int ans = 0; REP(i, SIZE(res)){ if(res[i]%2 == 1){ if(N%res[i] == 0){ ans++; } } else { if((N%res[i])*2 == res[i]) ans++; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,ans; int main(){ cin>>n; for(ll i=1;i*i<=n+n;i++){ ll S=n+n-i*(i-1); if(S%(i+i)!=0)continue; ll l=S/(i+i); if(l<=0)continue; ans++; } ans<<=1; cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int n, m, a, b, ans = 0; int vis[21][21]; void dfs(int x,int y) { if(x==n+1) { if(a==0 && b==0) ans++; return; } if(vis[x][y]==1) { if(y==m) dfs(x+1,1); else dfs(x,y+1); return; } if(b) { --b; vis[x][y]=1; if(y==m) dfs(x+1,1); else dfs(x,y+1); vis[x][y]=0; ++b; } if(a) { --a; vis[x][y]=1; if(vis[x+1][y]==0) { vis[x+1][y]=1; if(y==m) dfs(x+1,1); else dfs(x,y+1); vis[x+1][y]=0; } if(vis[x][y+1]==0) { vis[x][y+1]=1; if(y==m) dfs(x+1,1); else dfs(x,y+1); vis[x][y+1]=0; } ++a; vis[x][y]=0; } } int main() { cin >> n >> m >> a >> b; dfs(1,1); cout << ans << endl; return 0; }
/** * author: naniwazu **/ #include<bits/stdc++.h> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf=1e9+7; const ll INF=1LL<<60 ; const ll mod=1e9+7 ; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(),c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; 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< typename T > int64_t largest_rectangle(vector< T > height) { stack< int > st; height.push_back(0); vector< int > left(height.size()); int64_t ret = 0; for(int i = 0; i < height.size(); i++) { while(!st.empty() && height[st.top()] >= height[i]) { ret = max(ret, (int64_t) (i - left[st.top()] - 1) * height[st.top()]); st.pop(); } left[i] = st.empty() ? -1 : st.top(); st.emplace(i); } return (ret); } int popcount(int x) { int cnt = 0; while(x) cnt+=x&1, x>>=1; return cnt; } void solve(){ int H, W, A, B; cin >> H >> W >> A >> B; int ans = 0; rep(bit, 1 << (2*H*W-H-W)){ bool flag = true; if(!(popcount(bit) == A)){ continue; } vector<int> S(H*W,0); rep(i,H){ rep(j,W-1){ if (bit >> (i*(W-1)+j) & 1){ if (S[i*W+j] or S[i*W+j+1]){ flag = false; } S[i*W+j] = 1; S[i*W+j+1] = 1; } } } rep(j,W){ rep(i,H-1){ if (bit >> (j*(H-1)+i+H*(W-1)) & 1){ if (S[i*W+j] or S[i*W+j+W]){ flag = false; } S[i*W+j] = 1; S[i*W+j+W] = 1; } } } if (flag){ ans++; } } cout << ans << "\n"; } signed main(){ ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(10); //int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,b) for(int i=a;i<b;i+=1) #define repE(i,a,b) for(int i=a;i<=b;i+=1) #define pb push_back const ll MOD = 1e9 + 7; const ll N = 1e5 + 5, M = 10; const ll WE = 1e5; const ll maxV = 1e3; const ll INF = 1e18; ll ceil(ll a, ll b) { return (a + b - 1) / b; } int s(int n) { int sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } void solve() { int a, b; cin >> a >> b; int suma = s(a); int sumb = s(b); cout << max(sumb, suma) << "\n"; } int main() { ios::sync_with_stdio (0); cin.tie (NULL); int t = 1; // cin >> t; // compute(); // sieve(); while (t--) { solve(); } return 0; }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cstdlib> #include<queue> #include<set> #include<cstdio> #include<map> #include<cassert> using namespace std; #define ll long long #define reps(i, a, b) for(int i = a; i < b; i++) #define rreps(i, a, b) for(int i = a-1; i >= b; i--) #define rep(i, n) reps(i, 0, n) #define rrep(i, n) rreps(i, n, 0) #define P pair<int, int> const ll mod = 1000000007; const ll INF = 1001001001001001001; int main(){ int n, m; cin >> n >> m; vector<int> h(n); vector<int> w(m); rep(i, n) cin >> h[i]; rep(i, m) cin >> w[i]; sort(h.begin(), h.end()); vector<ll> l(n/2+1), r(n/2+1); l[0] = 0; r[0] = 0; rep(i, n/2){ l[i+1] = l[i] + (h[2*i+1] - h[2*i]); } rep(i, n/2){ r[i+1] = r[i] + (h[2*i+2] - h[2*i+1]); } // rep(i, n/2+1){ // cout << l[i] << " "; // }cout << endl; // rep(i, n/2+1){ // cout << r[i] << " "; // }cout << endl; ll ans = INF; rep(i, m){ int idx = upper_bound(h.begin(), h.end(), w[i]) - h.begin(); ll ans_ = 0; if(idx%2 == 1){ ans_ = l[idx/2] + (w[i] - h[idx-1]) + (r[n/2] - r[idx/2]); }else { ans_ = l[idx/2] + (h[idx] - w[i]) + (r[n/2] - r[idx/2]); } ans = min(ans, ans_); // cout << ans_ << " " << idx << endl; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={-1,0,1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main(){ ll n;cin >> n; vector<set<ll>> dp(n+1); vl ans(n+1); repl(i,1,n+1){ ll p=1;while(dp[i].count(p))p++; ans[i]=p; for(int j=i;j<=n;j+=i)dp[j].insert(ans[i]); } repl(i,1,n+1)cout << ans[i] <<" ";cout << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; if(N==1){cout<<1;} if(N==2){cout<<1<<' '<<2;} if(N==3){cout<<1<<' '<<2<<' '<<2;} if(N>3){ vector<int> a(N); a[0]=1; a[1]=2; a[2]=2; cout<<a[0]<<' '<<a[1]<<' '<<a[2]<<' '; for (double i = 4; i <= N; i++) { a[i-1]=2; for(double j=2;j<=pow(i,0.5);j++){ int l; l=i/j; double l2; l2=i/j; if(l==l2){ a[i-1]=a[l-1]+1; break;} } cout<<a[i-1]<<' '; } } return 0; }
/*#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")*/ #include <bits/stdc++.h> #define f first #define s second #define sym(s) s = "#" + s + "#"; #define all(x) (x).begin(), (x).end() #define alll(x,n) x+1, x+n+1 #define newl cout<<"\n"; #define foo(i,a,n) for(ll i = (a); i <= n; i++) #define deb1(a) cout<<a<<"\n"; #define deb2(a,b) cout<<a<<" "<<b<<"\n"; #define deb3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n"; #define deb4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"; #define debp(a) cout<<a.f<<" "<<a.s<<"\n"; #define debv(a) for(auto it: a)cout<<it<<"\n";newl; #define debm(a) for(auto it: a)cout<<"{"<<it.f<<","<<it.s<<"}, ";newl; #define deb1d(a,n) foo(i,1,n)cout<<a[i]<<" ";newl; #define deb2d(a,n,m) foo(i,1,n){foo(j,1,m){cout<<a[i][j]<<" ";}newl;} using namespace std; using ll = long long; using ld = long double; const ll MOD = 1e+9 + 7; const ll INF = LLONG_MAX; const int N = (int)2e+5 + 8; ld n, x, y, a, b; ld str, res, cnt; void MAIN(int tc){ cin >> x >> y >> a >> b; ld mul = 1; res = floor((y - x)/b)- (fmod(y - x, b) == 0); for(ld k = 1; k <= 100; k++){ mul *= a; ld nx = x * mul; if(nx >= y) { res = max(res, k - 1); break; } ld rem = floor((y - nx) / b) - (fmod(y - nx, b) == 0); res = max(res, k + rem); } deb1((ll)res); } int main(){ ios:: sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout<<fixed; cout<<setprecision(10); int test_cases = 1; //cin>>test_cases; for(int i = 1; i <= test_cases; i++){ MAIN(i); } }
/** * Author: Sanchit Sinha * Created: 17.10.2020 16:38:47 **/ #include "bits/stdc++.h" using namespace std; #define ll unsigned long long ll powermod(ll x,ll ex,ll md){ll ans=1ll;while(ex>0){if(ex&1ll) ans=(ans*x)%md; ex>>=1ll;x=(x*x)%md;}return ans;} inline ll gcd(ll a,ll b) {if(b==0)return a;a%=b;return gcd(b,a);} inline ll max(ll a,ll b) {return((a>b)?a:b);} inline ll min(ll a,ll b) {return((a>b)?b:a);} int main(){ ios::sync_with_stdio(false); cin.tie(0); ll x,y,a,b; cin>>x>>y>>a>>b; ll ans=0; while(x<y){ if(x>=y/a && x+a>=y) break; if(x>=y/a){ ll div=(y-1-x)/b; ans+=max(div,0); break; } ll x1=x*a; ll x2=x+b; if(x1<=x2){ x*=a; if(x<y)ans++; else break; } else{ ll div=(y-1-x)/b; ans+=max(div,0); break; } } cout<<ans<<"\n"; return 0; }
#include <bits/stdc++.h> #define mid(l,r) ((l + r) >> 1) #define lsb(x) (x & (-x)) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define FOR(X,L,R) for(int X=L;X<R;++X) #define endl '\n' #define int long long #define MOD ((int)1e9 + 7) #define INF (0x3f3f3f3f) #define LLINF (0x3f3f3f3f3f3f3f3fLL) using namespace std; template<typename T> inline void input(vector<T>& v) { for(T& x: v) cin >> x; } void solve() { int a, b, w; cin >> a >> b >> w; if((1000 * w + b - 1) / b > (1000 * w) / a) cout << "UNSATISFIABLE" << endl; else cout << (1000 * w + b - 1) / b << " " << (1000 * w) / a << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); #if defined(CODEFORCES) || defined(CF) int t; cin >> t; while(t--) solve(); #else solve(); #endif return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1e9+7; int solve_naive(const int n, const int s, const int k){ int cnt = 0; ll now = s; while (now % n != 0 && cnt < n){ now += k; cnt += 1; } if (cnt >= n){ return -1; } return cnt; } ll _gcd(ll a, ll b){ return (a%b) ? _gcd(b, a%b) : b; } pair<ll, ll> extgcd(ll a, ll b){ // ax+by=gcd(a, b)なるx, yを求める if(a%b==0)return {0, 1}; // a*0+b*1=b const auto [x, y] = extgcd(b, a%b); // b*x + (a-a/b*b)*y = gcd(a, b) return {y, x-a/b*y}; // a*y + b*(x+a/b*y) = gcd(a, b) } ll powmod(ll x, ll n, ll p){ // return (x ** n) mod p if (n==0)return 1; if(n==1)return x%p; const auto y = powmod(x, n/2, p); return (y*y%p)*powmod(x, n%2, p)%p; } int solve(int n, int s, int k){ // s + a * k == 0 mod n なる最小の a を求める const auto g = _gcd(k, n); if(s%g!=0){ // k, n が g の倍数なので s は g の倍数である必要がある return -1; } if(g>1){ return solve(n/g, s/g, k/g); } // k*a == 1 mod n なる a を求める const auto [a, _] = extgcd(k, n); return ((n-s)*a%n+n)%n; } int main() { int t; cin >> t; for(int ti=0; ti<t; ti++){ int n,s,k; cin >> n >> s >> k; cout << solve(n, s, k) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repp(i, l, n) for (int i = (l); i < (int)(n); i++) using ll = long long; int main(){ int n; cin>>n; cout << n-1 << endl; }
#define _CRT_SECURE_NO_WARNINGS #include<bits/stdc++.h> #define endl '\n'; #define sz(s) int(s.size()) #define all(v) ((v).begin()),((v).end()) #define allr(v) ((v).rbegin()), ((v).rend()) #define mem(x,y) memset(x,y,sizeof(x)) #define Ceil(x,y) ((x+y-1)/y) typedef long long ll; typedef double dl; using namespace std; void Selim() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } /***************************************************/ void solve(){ int n; cin >> n; cout << n - 1 << endl; } int main() { Selim(); int t = 1; //cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> #define iter_all(x) x.begin(), c.end() #define umap unordered_map #define uset unordered_set using namespace std; typedef long long int ll; auto io_speed = []() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); return 0; }(); ll pow_mod(ll a, ll e, ll mod) { ll res = 1; while(e) { if(e&1) res = res * a%mod; e>>=1; a=a*a%mod; } return res; } ll inv_mod(ll b, ll m) { ll a=m, x=0, y=1, r, q; while(b) { r = a%b; q = a/b; a = b; b = r; r = x; x = y; y = r-q*y; } x+=m; return x%m; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll rec(ll x, ll y, map<ll, ll> &dp) { auto itr = dp.find(y); if(itr == dp.end()) { if(y <= x) dp[y] = x-y; else { ll res = y-x; if(y&1) { res = min(res, rec(x, y+1, dp) + 1); res = min(res, rec(x, y-1, dp) + 1); } else { res = min(res, rec(x, y>>1, dp) + 1); } dp[y] = res; } } return dp[y]; } void solve() { ll x, y; cin >> x >> y; map<ll, ll> dp; cout << rec(x, y, dp) << endl; } int main() { int t = 1; // cin >> t; for(int i = 1; i <=t; i++) { // cout << "Case #"<<i<<": "; solve(); } }
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define pll pair<ll,ll> #define rep(i,n) for(ll i=0;i<n;i++) #define mod 1000000007 #define INF 10000000000000000 #define ff first #define ss second #define pb push_back #define lb lower_bound #define ub upper_bound #define pie 3.141592653589793238462643383279 #define PYES cout<<"YES"<<endl #define PNO cout<<"NO"<<endl #define SB(a) sort(a.begin(),a.end(),greater<ll>()); #define SS(a) sort(a.begin(),a.end()); #define vll vector<ll> #define vpll vector<pll> vector<bool> prime; vector<ll> fact,inv,primes,factors,pf; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void factorize(ll a) { factors.clear(); for(ll i=1;i*i<=a;i++) { if (i*i==a) factors.pb(i); else if (a%i==0) { factors.pb(i); factors.pb(a/i); } } sort(factors.begin(),factors.end()); } ll power(ll a,ll b) { if(a==1||b==0) return 1; ll c=power(a,b/2); ll res=1; res=c*c; if(res>=mod) res%=mod; if(b%2) res*=a; if(res>=mod) res%=mod; return res; } ll modInv(int a) { return power(a,mod-2); } void factorial(ll n) { fact.resize(n+1); fact[0]=1; for(ll i=1;i<=n;i++) { fact[i]=fact[i-1]*i; if(fact[i]>=mod) fact[i]%=mod; } } void InvFactorial(ll n) { inv.resize(n+1); inv[0]=1; for(ll i=1;i<=n;i++) inv[i]=modInv(fact[i]); } ll ncr(ll n,ll r) { if(n<r||n<0||r<0) return 0; ll b=inv[n-r]; ll c=inv[r]; ll a=fact[n]*b; if(a>=mod) a%=mod; a*=c; if(a>=mod) a%=mod; return a; } void remove_duplicates(vpll &v) { sort(v.begin(),v.end()); ll _size=unique(v.begin(),v.end())-v.begin(); v.resize(_size); } unsigned ll gcd(unsigned ll u, unsigned ll v) { if(u==0||v==0) return max(u,v); unsigned ll shift=__builtin_ctz(u|v); u>>=__builtin_ctz(u); do{ v>>=__builtin_ctz(v); if(u>v) swap(u,v); v-=u; }while(v!=0); return u<<shift; } void sieve(ll n) { prime.assign(n+1,1); prime[1]=false; for(ll p=2;p*p<=n;p++) { if(prime[p]) { for(ll i=p*p;i<=n;i+=p) prime[i]=false; } } for(ll i=2;i<n+1;i++) { if(prime[i]) primes.push_back(i); } } void prime_sieve(ll n) { pf.assign(n+1,0); rep(i,n+1) pf[i]=i; for(ll i=2;i<=n;i++) { if (pf[i]==i) { for(ll j=i;j<=n;j+=i) pf[j]=i; } } } //------------------------------------------------------------------------------------------------------------------------------------------------- // Always remember: if (condition) if (condition) statement; This doesn't work !! // Always increase boundaries for global or frequency questions // pow((ll)10,i) doesn't work sometimes. It is better to use multiple of 10 in arrays. Always remember this. // delete values when defining global if there are multiple testcases // don't try to be smart and make stupid mistakes void solve() { ll n; cin>>n; vll a(n),psum(n,0); rep(i,n) cin>>a[i]; SS(a); rep(i,n) { if (!i) psum[i]=a[i]; else psum[i]=psum[i-1]+a[i]; } double ans=INF; ans=psum[n-1]*n; rep(i,n) { double a11=a[i]; double p=a11/2; double q=p*n; double r=psum[n-1]-psum[i]; ans=min(ans,q+r-2*p*(n-1-i)); } cout.precision(20); cout<<ans/n<<endl; } int main() { fast(); ll T=1; //cin>>T; for(ll z=1;z<=T;z++) solve(); }
#include<iostream> #include<cstdio> #include<algorithm> #include<map> namespace do_while_true { #define ld double #define ll long long #define re register #define pb push_back #define fir first #define sec second #define pp std::pair<int, int> #define mp std::make_pair const ll mod = 1000000007; template <typename T> inline T Max(re T x, re T y) { return x > y ? x : y; } template <typename T> inline T Min(re T x, re T y) { return x < y ? x : y; } template <typename T> inline T Abs(re T x) { return x < 0 ? -x : x; } template <typename T> inline void read(re T& r) { r = 0; bool w = 0; char ch = getchar(); while(ch < '0' || ch > '9') w = ch == '-' ? 1 : 0, ch = getchar(); while(ch >= '0' && ch <= '9') r = r * 10 + (ch ^ 48), ch = getchar(); r = w ? -r : r; } template <typename T> inline T qpow(re T x, re T y) { re T sumq = 1; x %= mod; while(y) { if(y&1) sumq = sumq * x % mod; x = x * x % mod; y >>= 1; } return sumq; } } using namespace do_while_true; const int N = 10010; int n, m, k; int a[N], b[N], c[N][2]; bool vis[N]; void solve() { read(n); read(m); for(int i = 1; i <= m; ++i) read(a[i]), read(b[i]); read(k); int ans = 0; for(int i = 1; i <= k; ++i) read(c[i][0]), read(c[i][1]); for(int i = 0; i < (1 << k); ++i) { for(int j = 1; j <= n; ++j) vis[j] = 0; for(int j = 1; j <= k; ++j) { int p = (1 << (j-1)) & i; p = p>0; vis[c[j][p]] = 1; } int sumq = 0; for(int j = 1; j <= m; ++j) if(vis[a[j]] && vis[b[j]]) ++sumq; ans = Max(ans, sumq); } printf("%d\n", ans); } signed main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); #endif // freopen("out1.txt", "w", stdout); int T = 1; // read(T); while(T--) solve(); fclose(stdin); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 4e2+5; char s[N],t[N]; inline char get(char a,char b){ if(a == b) return a; if(a == 'P' && b == 'R') return 'P'; if(a == 'R' && b == 'P') return 'P'; if(a == 'S' && b == 'P') return 'S'; if(a == 'P' && b == 'S') return 'S'; if(a == 'R' && b == 'S') return 'R'; if(a == 'S' && b == 'R') return 'R'; } inline void change(int n){ for(int i = 0;i < n;++i) t[i] = s[i]; for(int i = n;i < n*2;++i) t[i] = s[i-n]; for(int i = 0;i < n;++i) s[i] = get(t[i*2],t[i*2+1]); return ; } int main() { int n,k; scanf("%d%d",&n,&k); scanf("%s",s); while(k){ change(n); k--; } printf("%c",s[0]); return 0; }
#include <bits/stdc++.h> #define ALL(A) (A).begin(), (A).end() #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 }; // i<4:4way i<8:8way int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 }; const ll mod = 1e9 + 7; const ll INF = -1 * ((1LL << 63) + 1); const int inf = -1 * ((1 << 31) + 1); int s[1505][1505]; int t[1505][1505]; int main(void){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int h,w,n,m,ans = 0; cin >> h >> w >> n >> m; vector<int> a(n),b(n); rep(i,n){ cin >> a[i] >> b[i]; a[i]--;b[i]--; } rep(i,m){ int c,d; cin >> c >> d; c--;d--; s[c][d] = 2; t[c][d] = 2; } rep(i,n){ // 現時点から横 int x = a[i]; int y = b[i]; s[x][y] = 1; while(y - 1 >= 0 && s[x][y-1] == 0){ s[x][y-1] = 1; y--; } y = b[i]; while( y + 1 < w && s[x][y+1] == 0){ s[x][y+1] = 1; y++; } } rep(i,n){ // 現時点から縦 int x = a[i]; int y = b[i]; t[x][y] = 1; while(x-1 >=0 && t[x-1][y] == 0){ t[x-1][y] = 1; x--; } x = a[i]; while(x + 1 < h && t[x+1][y] == 0){ t[x+1][y] = 1; x++; } } rep(i,h)rep(j,w){ if(s[i][j] == 1){ ans++; } else if(t[i][j] == 1){ ans++; } } cout << ans << endl; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int N; vector<int> E[200001]; pair<int, int> dfs(int u, int prev) { int saidai = 0; int ind = -1, p = u; rep(i, E[u].size()) { int to = E[u][i]; if (to != prev) { auto tmp = dfs(to, u); if (tmp.first > saidai) { saidai = tmp.first; ind = i; p = tmp.second; } } } int saigo = E[u].size(); if(ind != -1) swap(E[u][saigo - 1], E[u][ind]); return { saidai + 1, p }; } int kotae[200001], k; void dfs2(int u, int prev) { k++; kotae[u] = k; for (int to : E[u]) if (to != prev) { dfs2(to, u); } k++; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; rep(i, N - 1) { int a, b; cin >> a >> b; E[a].pb(b); E[b].pb(a); } auto tmp = dfs(1, -1); dfs(tmp.second, -1); dfs2(tmp.second, -1); rep1(i, N) cosp(kotae[i]); Would you please return 0; }
#include<bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(nullptr) #define all(x) x.begin(),x.end() #define pb push_back #define eb emplace_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double ld; const ll LINF=1e18; const ll MOD=998244353; const int INF=0x3f3f3f3f; const int MAXN=200050; ll gcd(ll a,ll b){ if(b==0)return a; else return gcd(b,a%b); } ll exgcd(ll a,ll b,ll &x,ll &y){ //注意x和y必须是引用 if(!b){x=1,y=0;return a;} ll d=exgcd(b,a%b,x,y); ll t=x;x=y,y=t-(a/b)*y; return d;//d是a和b的gcd,顺便求出来 } ll inv(ll a,ll b){ ll x,y; exgcd(a,b,x,y); return (x%b+b)%b; } void solve(int T){ ll n,s,k,ans; scanf("%lld%lld%lld",&n,&s,&k); ll gcdd=gcd(n,gcd(n-s,k)); s=(n-s); //n/=gcdd,k/=gcdd,s=(n-s)/gcdd; if(gcd(n,k)!=gcdd){ printf("-1\n"); }else{ ans=s*inv(k,n)%n; printf("%lld\n",ans/gcdd); } } signed main(){ //IOS; int t=1; scanf("%d",&t); for(int i=1;i<=t;i++){ solve(i); } }
#include <bits/stdc++.h> #include <vector> #include <iostream> #include<algorithm> #include<string> #include <map> #include <queue> #include <stack> #include<set> #include<math.h> #define DIV 1000000007 #define TE 2e5 using namespace std; using ll = long long; using ldb = long double; ll gcd(ll a, ll b) { if (b == 0)return a; else return gcd(b, a % b); } ll exgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll d = exgcd(b, a % b, x, y); ll oldx = x, oldy = y; x = oldy; y = oldx - ((a - a % b) / b) * oldy; return d; } int main() { int T; cin >> T; for (int i = 0; i < T; i++) { ll N, S, K; cin >> N >> S >> K; ll x = 0, y = 0; ll d = exgcd(K, N, x, y); //printf("i=%d\n", i); //cout << x << " " << y << " " << d << endl; if (S % d)cout << -1 << endl; else { ll p = -S / d; x *= p, y *= -p; K /= d, N /= d; x %= N; x = (x + N) % N; //cout << x << " " << y << " "<<d<<endl; cout << x << endl; } } /* ll x = 0, y = 0; ll d = exgcd(2, -3, x, y); cout << x << " " << y << " " << d << endl; */ }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define mk make_pair #define fi first #define se second #define mset(a,b) memset(a, b, sizeof(a)) #define dbg(x) cout << "[" << #x << "]: " << x << endl; #define forn(i,n) for(int i=0; i < n;i++) #define forab(i,a,b) for(int i = a; i <= b; i++) #define forba(i,b,a) for(int i = b; i >= a; i--) #define each(val, v) for(auto val : v) #define abs(u) (u >= 0 ? u : -u) using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; const int MAXN = 2e5 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e9+7; ll cnt[MAXN][26]; void solve(){ string s; cin >> s; ll n = s.size(); forn(i,26){ cnt[n][i] = 0; } ll ans= 0; for(int i = n-2; i >= 0; i--){ ll d1 = s[i] - 'a'; ll d2 = s[i+1] - 'a'; if(d1 == d2){ ans += n - (i+2) - cnt[i+2][d1]; forn(j,26){ cnt[i+2][j] = 0; } cnt[i+2][d1] = n - (i+2); } forn(j,26){ cnt[i+1][j] = cnt[i+2][j]; } cnt[i+1][ s[i+1] - 'a' ]++; } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i<n;++i) #define ocut cout #define ouct cout #define itn int struct Union{ vector<ll> par; Union(ll a){ par=vector<ll>(a,-1); } ll find(ll a){ if(par[a]<0){ return a; } else{ return par[a]=find(par[a]); } } bool same(ll a,ll b){ return (find(a)==find(b)); } ll size(ll a){ return -par[find(a)]; } void unite(ll a,ll b){ ll c=find(a),d=find(b); if(c==d) return; if(size(c)<size(d)){ swap(c,d); } par[c]+=par[d]; par[d]=c; } }; ll euclidean_gcd(ll a, ll b) { if(a < b) return euclidean_gcd(b, a); ll r; while ((r=a%b)) { a = b; b = r; } return b; } ll euclidean_lcm(ll a, ll b) { return a/euclidean_gcd(a,b)*b; } int main(void){ ll n,k=0; cin >> n; rep(i,200000000){ if((n-(i+1)*(i+2)/2)%(i+1)==0&&n/(i+1)-(i/2)>0)k++; } cout << k*2 << endl; }
#include<bits/stdc++.h> using namespace std; const double eps=1e-7; struct point { double x,y; friend bool operator<(point a,point b) { if(a.x==b.x)return a.y<b.y; return a.x<b.x; } } p[205]; int n; int main() { int x,y; scanf("%d",&n); for(register int i=1; i<=n; i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } sort(p+1,p+n+1); for(register int i=1; i<n-1; i++) { for(register int j=i+1; j<n; j++) { for(register int k=j+1; k<=n; k++) { double k1=(p[j].y-p[i].y)/(p[j].x-p[i].x),k2=(p[k].y-p[j].y)/(p[k].x-p[j].x); if(fabs(k1-k2)<eps||(p[j].x==p[i].x&&p[k].x==p[j].x)) { puts("Yes"); return 0; } } } } puts("No"); return 0; }
/* by Natsu Kinmoe */ #include <bits/stdc++.h> using namespace std; #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define loop(i, n) for(int i = 0; i < (n); ++i) #define cont(i, n) for(int i = 1; i <= (n); ++i) #define circ(i, a, b) for(int i = (a); i <= (b); ++i) #define range(i, a, b, c) for(int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c)) #define foreach(it, v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) #define y0 y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0 #define y1 y1II11II11III11I1III11II111IIII1II1I1 #define pub push_back #define pob pop_back #define mak make_pair typedef long long ll; typedef long double lf; const int Inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fll; /* Source code starts here */ int n; int x[105], y[105]; int main() { cin >> n; cont(i, n) cin >> x[i] >> y[i]; bool can = 0; cont(i, n) circ(j, i + 1, n) circ(k, j + 1, n) { int nx = x[j] - x[i], ny = y[j] - y[i]; int mx = x[k] - x[i], my = y[k] - y[i]; if(nx * my - ny * mx == 0) can = 1; } puts(can ? "Yes" : "No"); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define p (long long)(1e9+7) #define _(i,a,n) for(int i=a;i<n;i++) ll solve(ll a,ll b) { a%=1000000; ll r=1; while (b) { if (b&1) r=(r*a)%3000000000; a=(a*a)%3000000000; b>>= 1; } return r; } int main() { ll a,b,c; cin>>a>>b>>c; a%=10; b=solve(b,c); a=solve(a,b); cout<<a%10<<endl; return 0; }
#include <iostream> using namespace std; long long mod=998244353; int llpow(long long a,long long b){ long long r; if(b==0)r=1; else if(b==1)r=a%mod; else if(b%2==1){ r=llpow(a,b-1)*a; r%=mod; }else{ r=llpow(a,b/2); r*=r; r%=mod; } if(r<0)r+=mod; return r; } int main(){ long long N,M,K; cin>>N>>M>>K; long long ans=0; long long t; if(N>=2&&M>=2){ for(long long i=1;i<=K;++i){ t=(llpow(i,N)-llpow(i-1,N))%mod; ans+=(t*llpow(K-i+1,M))%mod; } }else if(N==1||M==1){ ans=llpow(K,N+M-1); } ans%=mod; if(ans<0)ans+=mod; cout<<ans<<endl; return 0; }
#include<iostream> using namespace std; int main(void) { char s,t; cin>>s>>t; if(s=='Y'){cout<<(char)(t-32)<<endl;} else{cout<<t<<endl;} return 0; }
#include<bits/stdc++.h> #define FULL(x,y) memset(x,y,sizeof(x)) #define ll long long #define pb push_back using namespace std; int x,y; int main() { cin>>x>>y; if (abs(x-y)<3) printf("Yes"); else printf("No"); return 0; }
#include<bits/stdc++.h> using namespace std; #define fo(i,a,b,c) for(int (i)=a;(i)<=b;(i)+=c) string S; deque<char> ans; bool op; int main() { cin>>S; int L=S.size(); fo(i,0,L-1,1) { if(S[i]=='R') op^=1; else { if(op) { if(ans.size()&&ans.front()==S[i]) ans.pop_front(); else ans.push_front(S[i]); } else { if(ans.size()&&ans.back()==S[i]) ans.pop_back(); else ans.push_back(S[i]); } } } if(op) { while(ans.size()) { cout<<ans.back(); ans.pop_back(); } } else { while(ans.size()) { cout<<ans.front(); ans.pop_front(); } } }
#pragma region RegionDefs #include <bits/stdc++.h> #define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i) #define reps(i,l,r) for(int i=(l),i##_len=(r);i<i##_len;++i) #define repr(i,l,r) for(int i=(r)-1,i##_left=(l);i>=i##_left;--i) #define all(x) begin(x),end(x) using namespace std; typedef long long ll; const ll INF = 1e9; template<class T=ll> using V = vector<T>; template<class T=ll> using PQ = priority_queue<T>; template<class T=ll> using PQG = priority_queue<T, V<T>, greater<T>>; const ll MOD = 1000000007LL; void in() {} template<class Head, class... Tail> void in(Head&& head, Tail&&... tail) { cin >> head; in(move(tail)...); } #define IN(...) ll __VA_ARGS__; in(__VA_ARGS__) #define TIN(T, ...) T __VA_ARGS__; in(__VA_ARGS__) #define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem #define VIND(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem,--_elem #define OUT(x) cout << (x); #define OUTL(x) cout << (x) << endl; template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;} template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;} template<class T> string join(vector<T>& v, string delim="") { ostringstream os; rep(i,v.size())i?os<<delim<<v[i]:os<<v[0]; return os.str(); } #pragma endregion RegionDefs void solve() { TIN(string, s); deque<char> d; bool rev = false; rep(i, s.size()) { if (s[i] == 'R') rev = !rev; else { if (rev) { if (!d.empty() && d.front() == s[i]) d.pop_front(); else d.push_front(s[i]); } else { if (!d.empty() && d.back() == s[i]) d.pop_back(); else d.push_back(s[i]); } } } string t(d.begin(), d.end()); if (rev) reverse(t.begin(), t.end()); OUTL(t); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << setprecision(numeric_limits<double>::max_digits10); solve(); return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; const int maxn = 2e5+8, inf = 1e18+9, mod = 1e9+7; char ans[maxn * 2]; struct node { int v, id, b; } a[maxn * 2]; int n, m; void solve() { int i, j, cnt = 0; cin >> n; for (i = 1; i <= (n * 2); i++) cin >> a[i].v, a[i].id = i; sort(a + 1, a + n * 2 + 1, [](node &o1, node &o2) { return o1.v < o2.v; }); for (i = 1; i <= (n * 2); i++) a[i].b = (i <= n) ? -1 : 1; sort(a + 1, a + n * 2 + 1, [](node &o1, node &o2) { return o1.id < o2.id; }); for (i = 1; i <= (n * 2); i++) { if (cnt * a[i].b < 0) ans[i] = ')'; else ans[i] = '('; cnt += a[i].b; } cout << ans + 1 << endl; } signed main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15); int t = 1; //cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using vi=vector<int>; int main() { int N; cin>>N; vi A(2*N); for (int i = 0; i < 2*N; i++)cin >> A[i]; vector<pair<int,int>> B(2*N); for (int i = 0; i < 2*N; i++)B[i] = make_pair(A[i], i); sort(B.begin(),B.end()); string S(2*N, '#'); for (int i = 0; i < N; i++)S[B[i].second] = '-'; for (int i = N; i < 2*N; i++)S[B[i].second] = '+'; stack<int> sp, sm; vector<pair<int,int>> vp; for (int i = 0; i < 2*N; i++){ if(S[i]=='-'){ if(!sp.empty()){ vp.push_back(make_pair(sp.top(), i)); sp.pop(); }else{ sm.push(i); } }else{ if(!sm.empty()){ vp.push_back(make_pair(sm.top(), i)); sm.pop(); }else{ sp.push(i); } } } string ans = string(2*N, '#'); for(auto e:vp){ auto [l, r] = e; ans[l] = '('; ans[r] = ')'; } cout << ans << endl; }
#include <bits/stdc++.h> #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef unsigned long long llu; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<string, int> psi; typedef pair<char, int> pci; typedef pair<int, char> pic; const int MOD = 1e9 + 7; const long double PI = 3.141592653589793238462643383279502884197; ll fac[1] = {1}, inv[1] = {1}; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;} ll cmb(ll r, ll c) {return (c>r||c<0) ? 0:fac[r] * inv[c] % MOD * inv[r - c] % MOD;} int main() { ll n, p; scanf("%lld %lld", &n, &p); printf("%lld", (p-1) * mp(p - 2, n-1) % MOD); } // author: rdd6584
#include <bits/stdc++.h> #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 drep(i, n) for(int i = (n)-1; i >= 0; i--) #define ALL(x) (x).begin(), (x).end() #define dup(x,y) (((x)+(y)-1)/(y)) #define srep(i,s,t) for (int i = s; i < t; ++i) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } // const int 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() { mint a, b, c; cin >> a >> b >> c; mint ans = a*(a+1)/2 *b*(b+1)/2 * c*(c+1)/2; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--) #define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" template<class T> void chmax(T &a, const T b){ a = max(a, b); } template<class T> void chmin(T &a, const T b){ a = min(a, b); } long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll x, y, z; cin >> x >> y >> z; cout << (y * z - 1) / x << endl; return 0; }
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define X first #define Y second #define nl '\n' #define AC return 0 #define pb(a) push_back(a) #define mst(a,b) memset(a, b, sizeof a) #define rep(i,n) for(int i = 0; (i)<(n); i++) #define rep1(i,n) for(int i = 1; (i)<=(n); i++) #define scd(a) scanf("%lld", &a) #define scdd(a,b) scanf("%lld%lld", &a, &b) #define scs(s) scanf("%s", s) //#pragma GCC optimize(2) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18; const int N = 1e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f; ll a, b; int main() { IOS; ll n, m, t, pre = 0; cin>>n>>m>>t; ll now = n; bool ok = 1; rep1(i, m) { cin>>a>>b; now -= a-pre; if(now <= 0) { ok = 0; break; } now = min(n, now+b-a); pre = b; } now -= t-pre; if(now <= 0) ok = 0; if(ok) puts("Yes"); else puts("No"); AC; }
#include<bits/stdc++.h> #define INFTY 10000000000 #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long int ll; const int MOD=998244353; const int di[4] = {-1,0,1,0}; const int dj[4] = {0,-1,0,1}; const int INF = 1e9; const int A = 1000005; int main(){ int n;cin>>n; vector<int> a(n,0); rep(i,n)cin>>a[i]; vector<ll> sum(n,0),max_v(n,0); sum[0] = a[0]; max_v[0] = max(0,a[0]); for(int i = 1;i < n;i++){ sum[i] = sum[i-1] + a[i]; max_v[i] = max(sum[i],max_v[i-1]); } ll ans = 0,cal = 0; rep(i,n){ ans = max(cal + max_v[i],ans); cal += sum[i]; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl '\n' #define sz(v) (int)v.size() #define all(v) v.begin(), v.end() void dbg_out() { cerr << "\b\b]\n"; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);} #define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__) int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector <int> A(n); for (int &i: A) cin >> i; ll mx = 0, curr = 0, ans = -1e18, p = 0; for (int i = 0; i < n; ++i) { ans = max(ans, curr + mx); curr += p + A[i]; p += A[i]; mx = max(mx, p); } cout << max(curr, ans); return 0; }
#include <iostream> int main() { int X, Y, Z; std::cin >> X >> Y >> Z; auto ans = (Z * Y - 1) / X; std::cout << ans << std::endl; return 0; }
#include<iostream> #include<vector> #include<stdio.h> #include<stdint.h> #include<string> #include<algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a; int b; int c; cin >> a >> b >> c; int ans = b * c / a; if((b * c) % a == 0) cout << ans-1 << endl; else cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; #define rep(i, j, n) for (int i = j; i < (n); ++i) #define rrep(i, j, n) for (int i = (n)-1; j <= i; --i) #define vi vector<ll> #define vii vector<vector<ll>> template <typename T> std::ostream& operator<<(std::ostream& os, std::vector<T>& a) { for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i]; return os << '\n'; } template <typename T> std::ostream& operator<<(std::ostream& os, pair<T, T>& p) { return os << "{ " << p.first << ", " << p.second << " }" << std::endl; } template <typename T> std::istream& operator>>(std::istream& is, pair<T, T>& p) { return is >> p.first >> p.second; } template <typename T> std::istream& operator>>(std::istream& is, std::vector<T>& a) { for (T& x : a) is >> x; return is; } [[maybe_unused]] constexpr ll MOD = 1e9 + 7; [[maybe_unused]] constexpr int INF = 0x3f3f3f3f; [[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; string s; cin >> s; vector<int> a(n + 1); cin >> a; int k = 100000; rep(i, 0, n) { k = min(k, abs(a[i] - a[i + 1])); } cout << k << '\n'; rep(i, 0, k) { rep(j, 0, n + 1) { cout << (a[j] + i) / k << ' '; } cout << '\n'; } return 0; }
// clang-format off #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define fst first #define snd second #define forn(i,n) for (int i = 0; i < int(n); i++) #define forn1(i,n) for (int i = 1; i <= int(n); i++) #define popcnt __builtin_popcount #define ffs __builtin_ffs #define ctz __builtin_ctz #define clz __builtin_clz #define all(a) (a).begin(), (a).end() using namespace std; using namespace __gnu_pbds; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pli = pair<ll,int>; using pil = pair<int,ll>; using pll = pair<ll,ll>; template <typename T> using vec = vector<T>; using vi = vec<int>; using vl = vec<ll>; template <typename T> using que = queue<T>; template <typename T> using deq = deque<T>; template <typename T> T id(T b) {return b;}; template <typename T> void chmax(T &x, T y) {if (x < y) x = y;} template <typename T> void chmin(T &x, T y) {if (x > y) x = y;} template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); } void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); } template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>; // clang-format on string s[100]; int main() { fastio(); int h, w; cin >> h >> w; forn(i, h) cin >> s[i]; int ans = 0; forn(i, h) forn(j, w) { if (s[i][j] == '#') continue; if (j < w - 1) { ans += s[i][j + 1] == '.'; } if (i < h - 1) { ans += s[i + 1][j] == '.'; } } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <iomanip> #include <numeric> #include <cassert> #include <vector> #include <random> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> //#include <atcoder/all> #define syosu(x) fixed<<setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef pair<double,double> pdd; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int,P> pip; typedef vector<pip> vip; const int inf=1<<30; const ll INF=1ll<<60; const double pi=acos(-1); const double eps=1e-8; const ll mod=998244353; const int dx[8]={-1,0,1,0,1,1,-1,-1},dy[8]={0,1,0,-1,1,-1,1,-1}; //using namespace atcoder; int n,m; vi a; vi f(){ vi b(n/2+1); for(int i=1;i<=n/2;i++){ b[i]=abs(a[2*i-1]-a[2*i-2])+b[i-1]; } reverse(a.begin(),a.end()); return b; } int main(){ cin>>n>>m; a=vi(n); for(auto &i:a) cin>>i; sort(a.begin(),a.end()); vi b=f(),c=f(); int res=inf; for(int i=0;i<m;i++){ int x; cin>>x; int j=upper_bound(a.begin(),a.end(),x)-a.begin(); res=min(res,b[j/2]+c[(n-j)/2]+abs(x-a[(j%2==1?j-1:j)])); } cout<<res<<endl; }
//#include <bits/stdc++.h> #include<iostream> #include<cstdio> #include <string.h> #include<cmath> #include<string> #include<algorithm> #include<vector> #include<stack> #include<queue> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define pb push_back #define INF 2e9 using namespace std; typedef long long ll;//int64 typedef unsigned long long ull; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; template<class T> void chmax(T& a, T b){if(a<b)a=b;} template<class T> void chmin(T& a, T b){if(a>b)a=b;} ll n,x[20],y[20],z[20]; ll dp[1<<17][20]; ll dif(int i1,int i2){ return abs(x[i1]-x[i2])+abs(y[i1]-y[i2])+max(0LL,z[i2]-z[i1]); } int main(){ cin >> n; REP(i,n)cin >> x[i] >> y[i] >> z[i]; REP(i,n)REP(j,1<<n)dp[j][i] = 1LL<<60; dp[1][0] = 0; REP(S,1<<n){ REP(x,n){ REP(y,n){ if(S & (1<<x)){ dp[S|(1<<y)][y] = min(dp[S|(1<<y)][y], dp[S][x]+dif(x,y)); } } } } cout << dp[(1<<n)-1][0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5e3 + 5; const int INF = 0x3f3f3f3f; #define DEBUG(x) cout << (x) << '\n' #define fi first #define se second char s[MAXN]; int n; void run(){ cin >> n >> s + 1; int ans = 0; for(int i = 1; i < n; i++){ int f1 = 0, f2 = 0; for(int j = i; j <= n; j++){ f1 += s[j] == 'A'; f2 += s[j] == 'C'; f1 -= s[j] == 'T'; f2 -= s[j] == 'G'; ans += f1 == 0 && f2 == 0; } } DEBUG(ans); } int main(){ #ifdef Irene freopen("in.txt", "r", stdin); #endif // Irene ios_base::sync_with_stdio(false); run(); return 0; }
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <map> #include <cmath> #include <queue> #include <deque> #include <set> #include <iomanip> #include <utility> typedef long long ll; typedef long double ld; using namespace std; int sum[5010][4]; int main() { int N; string S; cin >> N >> S; for(int i=0; i<4; ++i) sum[0][0]=0; for(int i=0; i<N; ++i){ for(int j=0; j<4; ++j) sum[i+1][j]=sum[i][j]; if(S[i]=='A') ++sum[i+1][0]; else if(S[i]=='T') ++sum[i+1][1]; else if(S[i]=='C') ++sum[i+1][2]; else ++sum[i+1][3]; } int ans=0; for(int i=0; i<N; ++i){ for(int j=i+1; j<=N; ++j){ if(sum[j][0]-sum[i][0]==sum[j][1]-sum[i][1] && sum[j][2]-sum[i][2]==sum[j][3]-sum[i][3]) ++ans; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using ll = long long; using namespace std; #define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define repi(i, x) \ for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++) #define all(x) (x).begin(), (x).end() #define F first #define S second #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back typedef vector<int> Vi; typedef vector<Vi> VVi; typedef pair<int, int> Pi; typedef vector<Pi> VPi; typedef vector<long long> V; typedef vector<V> VV; typedef pair<long long, long long> P; typedef vector<P> VP; 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, 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) { rep(i, v.size()) { if (i) os << " "; os << v[i]; } return os; } template <class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> istream& operator>>(istream& is, vector<T>& v) { rep(i, v.size()) { is >> v[i]; } return is; } const long long INFLL = 1LL << 60; const int INF = 1 << 30; const double PI = acos(-1); #ifdef LOCAL #define dbg(x) cerr << #x << ": " << (x) << '\n' #define say(x) cerr << (x) << '\n' #else #define dbg(x) #define say(x) #endif string solve(bool a) { return ((a) ? "Yes" : "No"); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; pair<double, double> ans, a, b, center, dis; cin >> n >> a >> b; double turn = 2.0 * PI / n; dbg(turn); center = mp(a.F + b.F, a.S + b.S); center.F /= 2.0; center.S /= 2.0; dbg(center); dis = mp(a.F - center.F, a.S - center.S); dbg(dis); ans.F = dis.F * cos(turn) - dis.S * sin(turn); ans.S = dis.F * sin(turn) + dis.S * cos(turn); dbg(ans); ans.F += center.F; ans.S += center.S; cout << fixed << setprecision(15) << ans.F << " " << ans.S << '\n'; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define fcout cout << fixed << setprecision(18) int main() { ios::sync_with_stdio(false); cin.tie(0); double N; cin >> N; double X0, Y0; cin >> X0 >> Y0; double Xn, Yn; cin >> Xn >> Yn; double Xc = (X0 + Xn) / 2.0; double Yc = (Y0 + Yn) / 2.0; double X00 = (X0 - Xc); double Y00 = (Y0 - Yc); double theta = (2 * 3.1415926535) / N; double X10 = (X00 * cos(theta) - Y00 * sin(theta)); double Y10 = (X00 * sin(theta) + Y00 * cos(theta)); double ansX = X10 + Xc; double ansY = Y10 + Yc; fcout << ansX << ' ' << ansY << '\n'; 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(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<double, double> PL; const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9, DIG = 50; int n, m, k; set<int> a; PL bit[MAX_N + 1][DIG + 1]; double divi[DIG + 1]; PL sum(int i, int d) { ll fi = 0, se = 0; while (i > 0) { fi += bit[i][d].fi; se += bit[i][d].se; i -= i & -i; } return PL(fi, se); } void add(int i, int d, ll x, ll y) { while (i <= n) { bit[i][d].fi += x; bit[i][d].se += y; i += i & -i; } } int main() { ci3(n, m, k); rep(i, k) { int tmp; ci(tmp); a.ep(tmp); } rep(i, n) { if (a.find(i) != a.end()) continue; rep (j, DIG) { PL p = (i == 0 && j == 0) ? PL(0, 1) : sum(i, j); ll x = p.fi + p.se, y = p.se; add(i + 1, j, x / m, y / m); add(i + 1, j + 1, x % m, y % m); if (i + m + 1 <= n) { add(i + m + 1, j, -x / m , -y / m); add(i + m + 1, j + 1, -x % m , -y % m); } else { add(n, j, x * (i + m - n) / m, y * (i + m - n) / m); add(n, j + 1, x * (i + m - n) % m, y * (i + m - n) % m); } } } divi[0] = 1.0; rep(i, DIG) divi[i + 1] = divi[i] / m; ll coe[DIG + 1], ans[DIG + 1]; coe[0] = 1.0; fill(coe + 1, coe + DIG + 2, 0.0); rep(i, DIG + 1) ans[i] = sum(n, i).fi; double jdg = 0.0; rep(i, DIG + 1) jdg += ans[DIG - i] * divi[DIG - i]; if (abs(jdg) < 1e-10) { co(-1); return 0; } for (auto &i : a) { rep(j, DIG + 1) { PL tmp = sum(i, j); ans[j] += tmp.fi; coe[j] -= tmp.se; } } double res1 = 0.0, res2 = 0.0; rep(i, DIG + 1) { res1 += ans[DIG - i] * divi[DIG - i]; res2 += coe[DIG - i] * divi[DIG - i]; } co(res1 / res2); return 0; }
#include<bits/stdc++.h> #define double long double #define maxn 100010 using namespace std; int read() { int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*w; } int n,m,K,v[maxn]; double k[maxn],b[maxn],sk[maxn],sb[maxn]; #define ABS(x) ((x)<0?-(x):(x)) signed main() { n=read(),m=read(),K=read(); for(int i=1,x;i<=K;i++) v[x=read()]=1,k[x]=1; for(int i=n-1;~i;i--) { if(!v[i]) k[i]=(sk[i+1]-sk[i+m+1])/m,b[i]=(sb[i+1]-sb[i+m+1])/m+1; sk[i]=sk[i+1]+k[i],sb[i]=sb[i+1]+b[i]; } if(ABS(k[0]-1)<1e-7) cout<<-1<<'\n'; else cout<<fixed<<setprecision(10)<<b[0]/(1-k[0])<<'\n'; return 0; }
#include<bits/stdc++.h> using namespace std; # define ll long long # define read read1<ll>() # define Type template<typename T> Type T read1(){ T t=0; char k; bool vis=0; do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9'); while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar(); return vis?-t:t; } # define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout) void exgcd(ll u,ll v,ll &x,ll &y){ if(!v){x=1,y=0;return;} if(u<v){ exgcd(v,u,y,x);x=-x;y=-y; return; }ll i=u/v; exgcd(u%v,v,x,y); y+=x*i; } int main(){ for(int T=read;T--;){ int x=read,y=read,p=read,q=read; ll a=x+y<<1,b=p+q,d=__gcd(a,b);ll ans=-1; for(int i=x;i<x+y;++i) for(int j=p;j<p+q;++j){ ll c=j-i;//ax-by=c if(c/d*d!=c)continue; ll l=a/d,r=b/d,u=0,v=0;c/=d;//lu-rv=c exgcd(l,r,u,v);//lu-rv=1 u*=c;v*=c;//lu-rv=c ll m=max(max((r-u-1)/r,(l-v-1)/l),0ll); u+=r*m,v+=l*m; m=max(min(u/r,v/l),0ll); u-=r*m;v-=l*m; //cout<<u*a+i<<' '<<v*b+j<<endl; if(!~ans||ans>v*b+j)ans=v*b+j; }if(~ans)cout<<ans<<endl; else puts("infinity"); } return 0; }
//abc193_e.cpp //Mon Mar 15 23:09:25 2021 #include <bits/stdc++.h> #define INTINF 2147483647 #define LLINF 9223372036854775807 #define MOD 1000000007 #define rep(i,n) for (int i=0;i<(n);++i) using namespace std; using ll=long long; typedef pair<int,int> P; inline ll mod(ll a, ll m){ return (a%m+m)%m; } ll extGCD(ll a, ll b, ll &p, ll &q){ if (b == 0){ p = 1; q = 0; return a; } ll d = extGCD(b, a%b, q, p); q -= a/b*p; return d; } //中国剰余定理。 //リターンは(r,m)。x ≡ r (mod m) //解なしの時は(0,-1)が帰ってくる。 pair<ll,ll> ChineseRem(ll b1, ll m1, ll b2, ll m2){ ll p,q; ll d = extGCD(m1,m2,p,q); if ((b2-b1)%d!=0) return make_pair(0,-1); ll m = m1*(m2/d); ll tmp = (b2-b1)/d*p%(m2/d); ll r = mod(b1+m1*tmp,m); return make_pair(r,m); } int main(){ int t; cin >> t; rep(i,t){ ll x,y,p,q; cin >> x >> y >> p >> q; ll ans = LLINF; for(ll j=x;j<x+y;j++){ for(ll k=p;k<p+q;k++){ pair<ll,ll> tmp = ChineseRem(j,2*x+2*y,k,p+q); if (tmp.second!=-1){ ans = min(ans,tmp.first); } } } if (ans == LLINF)cout << "infinity" << endl; else cout << ans << endl; } }
#include<bits/stdc++.h> using namespace std; # define ll long long # define read read1<ll>() # define Type template<typename T> Type T read1(){ T t=0; char k; bool vis=0; do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9'); while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar(); return vis?-t:t; } # define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout) void exgcd(ll u,ll v,ll &x,ll &y){ if(!v){x=1,y=0;return;} if(u<v){ exgcd(v,u,y,x);x=-x;y=-y; return; }ll i=u/v; exgcd(u%v,v,x,y); y+=x*i; } int main(){ for(int T=read;T--;){ int x=read,y=read,p=read,q=read; ll a=x+y<<1,b=p+q,d=__gcd(a,b);ll ans=-1; for(int i=x;i<x+y;++i) for(int j=p;j<p+q;++j){ ll c=j-i;//ax-by=c if(c/d*d!=c)continue; ll l=a/d,r=b/d,u=0,v=0;c/=d;//lu-rv=c exgcd(l,r,u,v);//lu-rv=1 u*=c;v*=c;//lu-rv=c ll m=max(max((r-u-1)/r,(l-v-1)/l),0ll); u+=r*m,v+=l*m; m=max(min(u/r,v/l),0ll); u-=r*m;v-=l*m; //cout<<u*a+i<<' '<<v*b+j<<endl; if(!~ans||ans>v*b+j)ans=v*b+j; }if(~ans)cout<<ans<<endl; else puts("infinity"); } return 0; }
#include <bits/stdc++.h> using namespace std; int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){ int64_t d = a; if(b != 0){ d = extgcd(b, a%b, y, x); y -= (a/b) * x; }else{ x = 1; y = 0; } return d; } pair<int64_t, int64_t> crt(int64_t b1, int64_t m1, int64_t b2, int64_t m2){ int64_t p, q; int64_t d = extgcd(m1, m2, p, q); if((b2-b1) % d != 0) return {-1, -1}; int64_t m = m1 * (m2/d); int64_t tmp = (b2 - b1) / d * p % (m2/d); int64_t r = ((b1 + m1 * tmp) % m + m) % m; return make_pair(r, m); } void solve(){ int64_t X, Y, P, Q; cin >> X >> Y >> P >> Q; int64_t INF = 8e18; int64_t ans = INF; for(int a=X; a<X+Y; a++) for(int b=P; b<P+Q; b++){ auto res = crt(a, 2*X+2*Y, b, P+Q).first; if(res != -1) ans = min(ans, res); } if(ans == INF){ cout << "infinity" << endl; }else{ cout << ans << endl; } } int main(){ int T; cin >> T; while(T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair<ll, ll>; using tp = tuple<ll, ll, ll>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; #define all(hoge) (hoge).begin(), (hoge).end() #define en '\n' #define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i) #define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i) #define REP(i, n) rep(i, 0, n) #define REP2(i, n) rep2(i, 0, n) constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; constexpr long long MOD = (ll)1e9 + 7; //constexpr long long MOD = 998244353LL; static const ld pi = 3.141592653589793L; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } //グラフ関連 struct Edge { int to, rev; ll cap; Edge(int _to, ll _cap, int _rev) : to(_to), cap(_cap), rev(_rev) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (int)G[to].size())); if(revFlag) G[to].push_back(Edge(from, revCap, (int)G[from].size() - 1)); } void solve() { ll n; string s; cin >> n >> s; vec<ll> a(n + 1); REP(i, n + 1) { cin >> a[i]; } ll d = INF; REP(i, n) { chmin(d, abs(a[i + 1] - a[i])); } vvec<ll> b(d, vec<ll>(n + 1, 1)); REP(i, d) { REP(j, n + 1) { b[i][j] = a[j] / d; if(i < a[j] % d) b[i][j]++; } } cout << d << en; for(auto i : b) { for(auto j : i) cout << j << " "; cout << en; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // ll t; // cin >> t; // REP(i, t - 1) { // solve(); // } solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #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++) int a[110]; int b[110]; int main() { int n,k,m,mi; string s; cin >> n >> s; rep(i,n+1)cin >> a[i]; rep(i,n) b[i]=a[i+1]-a[i]; k=10010; rep(i,n){ if(b[i]>0) k=min(k,b[i]); else k=min(k,-b[i]); } cout << k << endl; rep(j,k) { rep(i,n+1) cout << (int)((a[i]+j)/k)<<" "; cout << endl; } return 0; }
#line 1 "main_b.cpp" #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <cmath> #include <complex> #include <cstdint> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <utility> #include <vector> /* template start */ using i64 = std::int_fast64_t; using u64 = std::uint_fast64_t; #define rep(i, a, b) for (i64 i = (a); (i) < (b); (i)++) #define all(i) i.begin(), i.end() #ifdef LOCAL #define debug(...) \ std::cerr << "LINE: " << __LINE__ << " [" << #__VA_ARGS__ << "]:", \ debug_out(__VA_ARGS__) #else #define debug(...) #endif void debug_out() { std::cerr << std::endl; } template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) { std::cerr << " " << h; if (sizeof...(t) > 0) std::cout << " :"; debug_out(t...); } template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) { return os << pa.first << " " << pa.second; } template <typename T> std::ostream& operator<<(std::ostream& os, std::vector<T> vec) { for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1& a, T2 b) { return a > b && (a = b, true); } template <typename Num> constexpr Num mypow(Num a, u64 b, Num id = 1) { if (b == 0) return id; Num x = id; while (b > 0) { if (b & 1) x *= a; a *= a; b >>= 1; } return x; } template <typename T> std::vector<std::pair<std::size_t, T>> enumerate(const std::vector<T>& data) { std::vector<std::pair<std::size_t, T>> ret; for (std::size_t index = 0; index < data.size(); index++) ret.emplace_back(index, data[index]); return ret; } /* template end */ int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); i64 n,m; std::cin>>n>>m; std::vector<i64> a(n),b(n); for(auto&& e:a)std::cin>>e; for(auto&& e:b)std::cin>>e; std::vector<std::vector<i64>> graph(n); rep(i,0,m){ i64 c,d; std::cin>>c>>d; c--;d--; graph[c].emplace_back(d); graph[d].emplace_back(c); } std::vector<bool> visit(n,false); i64 aval=0,bval=0; auto dfs=[&](auto f,i64 now)->void{ visit[now]=true; aval += a[now]; bval += b[now]; for(const auto& e:graph[now]){ if(!visit[e])f(f,e); } }; rep(i,0,n){ if(!visit[i]){ aval = 0; bval = 0; dfs(dfs,i); if(aval!=bval){ std::cout<<"No\n"; return 0; } } } std::cout<<"Yes\n"; return 0; }
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags //#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> #ifdef LOCAL #include "debug-template.hpp" #endif using namespace std; #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } vector<int> f(n); iota(f.begin(), f.end(), 0); vector<long long> g(n); for (int i = 0; i < n; i++) { g[i] = a[i] - b[i]; } function<int(int)> find_set = [&](int u) { return f[u] == u ? f[u] = u : f[u] = find_set(f[u]); }; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; u = find_set(u); v = find_set(v); if (u != v) { g[v] += g[u]; g[u] = 0; f[u] = v; } } for (int i = 0; i < n; i++) { if (g[i]) { cout << "No\n"; return 0; } } cout << "Yes\n"; return 0; }
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char S[200200]; int K, bc[1 << 16], tr[200200], dig[200200]; const long long mod = 1000000007; long long prv[17][17], nxt[17][17]; int main() { scanf ("%s %d", S, &K); int l = 0; while (S[l]) l++; for (int i = 0; i < l; i++){ if ('0' <= S[i] && S[i] <= '9') tr[i] = S[i] - '0'; else tr[i] = S[i] - 'A' + 10; dig[i + 1] = dig[i] | (1 << tr[i]); } for (int i = 1; i < (1 << 16); i++) bc[i] = bc[i - (i & (-i))] + 1; for (int i = 0; i <= 16; i++) prv[i][i] = 1; long long ans = (bc[dig[l]] == K); for (int i = l - 1; i >= 0; i--){ if (i) ans = (ans + prv[1][K] * 15) % mod; for (int s = 0; s < tr[i]; s++){ if (s == 0 && i == 0) continue; int b = bc[dig[i] | (1 << s)]; ans = (ans + prv[b][K]) % mod; } memset(nxt, 0, sizeof(nxt)); for (int j = 0; j <= 16; j++){ for (int k = j; k <= 16; k++){ nxt[j][k] = (nxt[j][k] + prv[j][k] * k) % mod; if (k < 16) nxt[j][k + 1] = (nxt[j][k + 1] + prv[j][k] * (16 - k)) % mod; } } memcpy(prv, nxt, sizeof(nxt)); } printf ("%lld\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; const int N=5005,mod=998244353; int n,m,ans,v[N][N]; int qp(int a,int b) { if(v[a][b]!=-1) return v[a][b]; int ma=a,mb=b; int ans=1;while(b){if(b&1)ans=1ll*ans*a%mod;a=1ll*a*a%mod;b>>=1;} v[ma][mb]=ans; return ans; } int main() { memset(v,-1,sizeof(v)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) ans=(ans+1ll*qp(m-1,i-1)*qp(m,n-i))%mod; ans=1ll*ans*m%mod; for(int i=0;i<=n-2;i++) { int t=n-1-i; for(int j=1;j<=m;j++) ans=(ans+1ll*t*(qp(m-1,i)+mod-qp(j-1,i))%mod*qp(m,n-2-i))%mod; } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<char> vc; typedef queue<ll> ql; typedef deque<ll> dql; typedef priority_queue<ll> pql; typedef set<ll> sl; typedef pair<ll, ll> pl; typedef map<ll, ll> ml; typedef vector<vl> vvl; typedef vector<pl> vpl; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++) #define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--) #define all(v) (v).begin(), (v).end() ll mod(ll a, ll b) {return (a % b + b) % b;} ll quo(ll a, ll b) {return (a - mod(a, b)) / b;} template <typename T, typename U> bool chmin(T &a, const U b) {if(a > b) {a = b; return 1;} return 0;} template <typename T, typename U> bool chmax(T &a, const U b) {if(a < b) {a = b; return 1;} return 0;} const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const ll MAX = 2e5; const ld eps = 1e-9; const char newl = '\n'; ll itr1, itr2, M1 = 0, M2 = 0; bool found = 0; vl e, d1, d2; vb diam; vvl G; void dfs1(ll v) { for(ll u: G[v]) { if(d1[u] == INF) { d1[u] = d1[v] + 1; dfs1(u); } } return; } void dfs2(ll v) { for(ll u: G[v]) { if(d2[u] == INF) { d2[u] = d2[v] + 1; dfs2(u); } } return; } void dfs3(ll v) { if(v == itr2) { found = 1; return; } for(ll u: G[v]) { if(found) break; if(d2[v] < d2[u]) dfs3(u); if(found) diam[u] = 1; } return; } ll dfs4(ll v, ll num) { e[v] = num; ll last = -1; for(ll u: G[v]) { if(e[u] == INF) { if(diam[u]) { last = u; continue; } num = dfs4(u, num+1); } } if(last != -1) num = dfs4(last, num+1); return num+1; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, a, b; cin >> n; e = vl(n, INF); d1 = vl(n, INF); d2 = vl(n, INF); diam = vb(n, 0); G = vvl(n); rep(_, n-1) { cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } d1[0] = 0; dfs1(0); rep(i, n) if(chmax(M1, d1[i])) itr1 = i; d2[itr1] = 0; dfs2(itr1); rep(i, n) if(chmax(M2, d2[i])) itr2 = i; dfs3(itr1); dfs4(itr1, 1); //cout << itr1 << " " << itr2 << newl; //rep(i, n) if(diam[i]) cout <<i << newl; rep(i, n) cout << e[i] << ' '; cout << newl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; template<typename T> ostream& operator<<(ostream &os, vector<T> &v){ string sep = " "; if(v.size()) os << v[0]; for(int i=1; i<v.size(); i++) os << sep << v[i]; return os; } template<typename T> istream& operator>>(istream &is, vector<T> &v){ for(int i=0; i<v.size(); i++) is >> v[i]; return is; } #ifdef DBG void debug_(){ cout << endl; } template<typename T, typename... Args> void debug_(T&& x, Args&&... xs){ cout << x << " "; debug_(forward<Args>(xs)...); } #define dbg(...) debug_(__VA_ARGS__) #else #define dbg(...) #endif int main() { ios_base::sync_with_stdio(false); cout << setprecision(20) << fixed; int n; cin >> n; vector<int> s(2*n), d(2*n); for(int i=0; i<n; i++){ int a, b; cin >> a >> b; a--; b--; if(a>=0&&b>=0){ if(b<=a||s[a]!=0||s[b]!=0) { cout << "No" << endl; return 0; } s[a] = (i+1); s[b] = -(i+1); d[a] = b-a; d[b] = a-b; } else if(a>=0){ if(s[a]!=0){ cout << "No" << endl; return 0; } s[a] = i+1; } else if(b>=0){ if(s[b]!=0){ cout << "No" << endl; return 0; } s[b] = -(i+1); } } vector<bool> dp(n+1); dp[0] = true; for(int i=0; i<n; i++){ if(!dp[i]) continue; for(int j=1; i+j<=n; j++){ bool ok = true; for(int k=0; k<j; k++){ int p = 2*i+k, q = 2*i+j+k; if(d[p]!=0&&d[p]!=j) ok = false; if(d[q]!=0&&d[q]!=-j) ok = false; if(s[p]!=0&&s[q]!=0&&s[p]+s[q]!=0) ok = false; if(s[p]<0 || 0<s[q]) ok = false; } dp[i+j] = dp[i+j] || ok; } } cout << (dp[n]?"Yes":"No") << endl; return 0; }
#include <bits/stdc++.h> using ll = long long; const int N(1010); ll n, f[N]; int pos, fl[N]; std::vector<int> ans; int main() { std::scanf("%lld", &n), f[1] = 1; for (int i = 2; i <= 90; i++) f[i] = f[i - 1] + f[i - 2]; for (int i = 90; i; i--) if (n >= f[i]) n -= f[i], fl[i] = 1, pos = std::max(pos, i); for (int i = pos, j = 1; i; i--, j++) { if (fl[i]) { if (i & 1) ans.push_back(1); else ans.push_back(2); } if (j & 1) ans.push_back(4); else ans.push_back(3); } printf("%lu\n", ans.size()); for (auto i : ans) printf("%d\n", i); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for ((i)=1;(i)<=(n);(i)++) #define rep0(i,n) for ((i)=0;(i)<(n);(i)++) using namespace std; long long n,m,s,i; vector<int> ans; long long gcd(long long x,long long y){ if(!y) return x; long long t=gcd(y,x%y); s+=x/y;return t; } void print(long long x,long long y){ if(!x){ while(y--)ans.push_back(2); return; } if(!y){ while(x--)ans.push_back(1); return; } if(x>y){ while(x>=y){ ans.push_back(3); x-=y; } } else{ while(x<=y){ ans.push_back(4); y-=x; } } print(x,y); } int main(){ srand(time(NULL)); cin>>n; for(;;){ m=(rand()+rand()*65536ll+rand()*65536ll*65536ll/*+rand()*65536ll*65536ll*65536ll*/)%n; s=0; long long g=gcd(n,m); if(s+g>130) continue; print(n,m); printf("%d\n",ans.size());reverse(ans.begin(),ans.end()); rep0(i,ans.size())printf("%d\n",ans[i]); return 0; } return 0; }
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define pb push_back #define N 100012 #define ll long long #define int ll #define pi pair<int , int> #define pip pair<pair ,int> #define mp make_pair #define f first #define s second #define mod 1000000007 ll Mod(ll x, ll y, int p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int* getlps(string pattern) { int len = pattern.length(); int * lps = new int[len]; lps[0] = 0; int i = 1 , j = 0; while (i < len) { if (pattern[i] == pattern[j]) { lps[i] = j + 1; i++; j++; } else { if (j != 0) { j = lps[j - 1]; } else { lps[i] = 0; i++; } } } return lps; } class Triplet { public: int x ; int y ; int gcd; }; Triplet extendedEuclid(int a , int b) { if (b == 0) {Triplet ans; ans.gcd = a; ans.x = 1; ans.y = 0; return ans;} Triplet smallAns = extendedEuclid(b , a % b); Triplet ans; ans.gcd = smallAns.gcd; ans.x = smallAns.y; ans.y = smallAns.x - (a / b) * smallAns.y; return ans; } int mmInverse(int a , int m) { Triplet ans = extendedEuclid(a , m); return (ans.x + m) % m; } int fact[N]; void calfac(int n ) { fact[0] = 1; for (int i = 1 ; i <= n + 2; i++) { fact[i] = (((fact[i - 1] % mod) * (i % mod)) % mod + mod) % mod; } } int calc(int n , int r) { if (r > n)return 0; if (r == n)return 1; int ans = 1; ans = ((ans % mod) * (fact[n]) % mod + mod) % mod; ans = ((ans % mod) * (mmInverse(fact[n - r] , mod) % mod) + mod) % mod; ans = ((ans % mod) * (mmInverse(fact[r] , mod) % mod) + mod) % mod; return (ans + mod) % mod; } void solve(int cntt) { int n; cin >> n; double x; cin >> x; x = x * 100.0; double ans = 0; int val = 1010; for (int i = 0 ; i < n ; i++) { double am , per; cin >> am >> per; double z = (1.0 * am) * (1.0 * per); double jaadu = z; // cout << "jaadu:" << jaadu << endl; x = x - jaadu; if (x < 0) { //cout << ans << endl; //cout << x << endl; //cout << "hello" << endl; val = min(i + 1 , val); // cout << i + 1 << endl; // return; } } if (val == 1010) cout << "-1" << endl; else cout << val << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // preprocess(); int t; t = 1; int l = 0; while (t--) { l++; solve(l); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll bin(ll a[],ll l,ll r,ll x){ ll m=(l+r)/2; if(l>=r)return -1; if(a[m]==x)return m; if(a[m]>x)return bin(a,l,m-1,x); return bin(a,m+1,r,x); } ll exp(ll a,ll b){ if(b==0)return 1; ll res=exp(a,b/2); if(b%2==0)return res*res; return res*res*a; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int n; cin>>n; int x; cin>>x; x*=100; int a[n],b[n]; double f[n]; for(int i=0;i<n;i++){ cin>>a[i]>>b[i]; f[i]=(a[i]*b[i]); } int s=0; int flag=0,cnt=0; for(int i=0;i<n;i++){ s+=f[i]; cnt++; if(s>x){ flag=1; break; } } cout<<(flag?cnt:-1); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = (int)5e5 + 77; const ll mod = (ll)1e9 + 7; const ll inf = (ll)1e18; ll n, m, ans; map<ll, ll> mp; int main() { int t = 1; // cin >> t; while (t--) { cin >> n; ans = 0; for (int i = 0; i < n; i++) { cin >> m; ans += i - mp[m]; mp[m] += 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main() { int n; cin >> n; vector<ll> dp(2, 1); rep(i, n) { vector<ll> temp(2); swap(dp, temp); string s; cin >> s; rep(y, 2) rep(x, 2){ int l; if (s == "AND") l = y & x; else l = y | x; dp[l] += temp[y]; } } cout << dp[1] << endl; return 0; }
#pragma region RegionDefs #include <bits/stdc++.h> #define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i) #define reps(i,l,r) for(int i=(l),i##_len=(r);i<i##_len;++i) #define repr(i,l,r) for(int i=(r)-1,i##_left=(l);i>=i##_left;--i) #define all(x) begin(x),end(x) using namespace std; typedef long long ll; const ll INF = 1e9; template<class T=ll> using V = vector<T>; template<class T=ll> using PQ = priority_queue<T>; template<class T=ll> using PQG = priority_queue<T, V<T>, greater<T>>; const ll MOD = 1000000007LL; void in() {} template<class Head, class... Tail> void in(Head&& head, Tail&&... tail) { cin >> head; in(move(tail)...); } #define IN(...) ll __VA_ARGS__; in(__VA_ARGS__) #define TIN(T, ...) T __VA_ARGS__; in(__VA_ARGS__) #define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem #define VIND(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem,--_elem #define OUT(x) cout << x; #define OUTL(x) cout << x << endl; template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;} template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;} template<class T> string join(vector<T>& v, string delim="") { ostringstream os; rep(i,v.size())i?os<<delim<<v[i]:os<<v[0]; return os.str(); } #pragma endregion RegionDefs int mins(ll a, ll b) { if (a == 0 && b == 0) return 0; if (a == b || a == -b) return 1; if (abs(a) + abs(b) <= 3) return 1; if (abs(a - b) <= 3 || abs(a + b) <= 3) return 2; if (abs(a) + abs(b) <= 6) return 2; if ((a + b) % 2 == 0) return 2; return 3; } void solve() { IN(a, b, c, d); OUTL(mins(c-a, d-b)); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << setprecision(numeric_limits<double>::max_digits10); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio; cin.tie(0); cout.tie(0) #define ll long long #define ull unsigned long long #define pb push_back #define pii pair<int,int> #define pll pair<long,long> #define vi vector<int> #define vll vector<ll> #define vii vector<pii> #define Mi map<int,int> #define mii map<pii,int> #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define ff first #define ss second #define sz(x) (int)x.size() //#define endl '\n' #define mod 1000000007 //#define mod 998244353 #define rep(i,a,b) for(int i=a;i<b;i++) #define rem(i,a,b) for(int i=a;i>b;i--) #define mp(a,b) make_pair(a,b) #define INF numeric_limits<ll>::max(); #define NINF numeric_limits<ll>::min(); #define vvi(a,b,name) vector<vector<int>> name(a,vector<int>(b,-1)) const long double pi=3.14159265359; inline ll add(ll a, ll b, ll m) { if ((a + b) >= m) return (a + b) % m; return a + b; } inline ll mul(ll a, ll b, ll m) { if ((a * b) < m) return a * b; return (a * b) % m; } ll power(ll x, ll y, ll m) { ll res = 1; x = x % m; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % m; y = y >> 1; x = (x * x) % m; } return res; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int main() {fast; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif int t=1; //cin>>t; while(t--) {int n,m; cin>>n>>m; vector<string> v(n); rep(i,0,n){ cin>>v[i]; } int ans=0; rep(i,1,n){ rep(j,0,m){ int f=0; while(v[i][j]=='#'&&v[i-1][j]=='.'){ f=1; j++; } if(f){ ans++; } } } rep(i,0,n-1){ rep(j,0,m){ int f=0; while(v[i][j]=='#'&&v[i+1][j]=='.'){ f=1; j++; } if(f){ ans++; } } } rep(j,1,m){ rep(i,0,n){ int f=0; while(v[i][j]=='#'&&v[i][j-1]=='.'){ f=1; i++; } if(f){ ans++; } } } rep(j,0,m-1){ rep(i,0,n){ int f=0; while(v[i][j]=='#'&&v[i][j+1]=='.'){ f=1; i++; } if(f){ ans++; } } } cout<<ans<<endl; } return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //required #include <ext/pb_ds/tree_policy.hpp> //required using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // find_by_order(k) returns iterator to kth element starting from 0; // order_of_key(k) returns count of elements strictly smaller than k; typedef long long ll; typedef vector<ll> VL; typedef vector<int> VI; typedef pair<ll,ll> PLL; typedef pair<int,int> PII; #define pb push_back #define F first #define S second #define SZ(a) int((a).size()) #define ALL(a) a.begin(),a.end() #define fr(i,x,y) for(int i=x;i<y;i++) #define frr(i,x,y) for(int i=x-1;i>=y;i--) #define inf 1e18+1 const long double PI = acos(-1.0L); #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); const int mod=1000000007; //const int mod=998244353; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;} ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;} VL dx={1,0,-1,0}; VL dy={0,1,0,-1}; // string to integer stoi() // string to long long stoll() // string.substr(position,length); // integer to string to_string(); const int N=500000; vector<vector<PLL>> adj; VL visit2; map<ll,ll> gg; bool dfs(ll v,ll p) { gg[v]=1; visit2[v]=1; bool cyc=false; for(auto to:adj[v]) { if(visit2[to.F]==1) { if(to.S!=p) { cyc=true; } } else { cyc|=dfs(to.F,to.S); } } return cyc; } void solve() { ll n; cin>>n; vector<PLL> arr; map<ll,ll> mp,visited; map<PLL,VL> store; VL pres(N,0); visit2.assign(N,0); adj.assign(N,vector<PLL>()); fr(i,0,n) { ll a,b; cin>>a>>b; pres[a]=1,pres[b]=1; if(a>b) { swap(a,b); } if(a==b) { mp[a]=1; visited[i]=1; } arr.pb({a,i}); arr.pb({b,i}); store[{a,b}].pb(i); } sort(ALL(arr)); for(auto i:store) { if(SZ(i.S)>1) { PLL tmp=i.F; mp[tmp.F]=1; mp[tmp.S]=1; VL fk=i.S; visited[fk[0]]=1; visited[fk[1]]=1; } PLL tmp=i.F; VL fk=i.S; for(auto pp:fk) { adj[tmp.F].pb({tmp.S,pp}); adj[tmp.S].pb({tmp.F,pp}); } } //cout<<SZ(mp)<<endl; mp.clear(); ll ans=0; fr(i,0,N) { if(pres[i]==1&&visit2[i]==0) { gg.clear(); if(dfs(i,-1)) { ans+=SZ(gg); } else { ans+=SZ(gg)-1; } } } cout<<ans<<endl; return; } int main() { IOS; ll t=1,pp; //cin>>t; pp=t; while(t--) { //cout<<"Case #"<<pp-t<<": "; solve(); } 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 * BE CAREFUL REGARDING THE DEFAULT VALUES IN segement trees etc * BE very careful in int vs long long vs unsigned long long */ /* recursion - matrix exponential */ // BITMASK: // 1)When some constrall is of the order of 15-20, think of bitmask DP. // 2)When some constrall is around 40, try out meet in the middle // 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithmQ
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; constexpr int inf=1e9+7; constexpr ll longinf=1LL<<60 ; constexpr ll mod=1e9+7 ; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; cin>>n>>m; vector<string> s(n); ll odd = 0, even = 0; rep(i,n){ string s; cin>>s; int sum=0; rep(j,m)sum+=s[j]-'0'; if(sum%2)++odd; else ++even; } cout<<odd*even<<endl; return 0; }
#include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<deque> #include<queue> #include<map> #include<set> #include<list> #define ll long long #define str string #define ld long double #define vec vector #define vll vec<ll> #define vvll vec<vll> #define rep(i,a,b) for(ll i=a;i<b;i++) #define rrep(i,b,a) for(ll i=b;i>=a;i--) #define repset(itr,a) for(auto itr=a.begin();itr!=a.end();itr++) #define repnck(bit,n,k) for(ll bit=(1<<(k))-1;bit<(1<<(n));bit=nec(bit)) #define bi1(bit,i) ((bit)&1<<(i)?true:false) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define kai "\n" #define prque priority_queue #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define mie min_element #define mae max_element #define tos to_string #define sep setprecision #define lob lower_bound #define upb upper_bound #define bis binary_search #define nep next_permutation #define MOD 1000000007ll #define MAX 2147483647 #define MIN (1e-10) #define equal(a,b) (abs((a)-(b))<MIN) using namespace std; void solve(); struct ll2{ll x,y;bool operator<(const ll2 &l)const{return x!=l.x?x<l.x:y<l.y;};}; struct ll3{ll x,y,z;bool operator<(const ll3 &l)const{if(x!=l.x)return x<l.x;if(y!=l.y)return y<l.y;return z<l.z;};}; class ufset{ public: ufset(ll n) {rank.resize(n,0);rep(i,0,n)p.pub(i);} bool same(ll i,ll j){return find(i)==find(j);} void unite(ll i,ll j){link(find(i),find(j));} private: vll rank,p; void link(ll i,ll j){if(rank[i]>rank[j])p[j]=i;else{p[i]=j;if(rank[i]==rank[j])rank[j]++;}} ll find(ll i){if(i!=p[i])p[i]=find(p[i]);return p[i];} }; class nck{ public: nck(ll maxn,ll mod=MOD){ NUM=maxn+1;p=mod; fac.resize(NUM);finv.resize(NUM);inv.resize(NUM); fac[0]=fac[1]=finv[0]=finv[1]=inv[1]=1; rep(i,2,NUM){fac[i]=fac[i-1]*i%p;inv[i]=p-inv[p%i]*(p/i)%p;finv[i]=finv[i-1]*inv[i]%p;} } ll c(ll n,ll k){return fac[n]*(finv[k]*finv[n-k]%p)%p;} private: ll NUM,p; vll fac,finv,inv; }; ll gcd(vector<ll> a){rep(i,1,a.size()){ll k=a[i-1]%a[i];while(k!=0){a[i-1]=a[i];a[i]=k;k=a[i-1]%a[i];}}return a.back();} ll lcm(vector<ll> a){ll n;rep(i,1,a.size()){n=a[i-1]*a[i];ll k=a[i-1]%a[i];while(k!=0){a[i-1]=a[i];a[i]=k;k=a[i-1]%a[i];}n/=a[i];a[i]=n;}return n;} ll pow2(ll a,ll b,ll p=MOD){if(b==-1)return pow2(a,p-2);ll res=1;if(b>0){res=pow2(a,b/2);if(b%2==0)res=res*res%p;else res=res*res%p*a%p;}return res;} ll nec(ll bit){ll x=bit&-bit,y=bit+x;return (((bit&~y)/x)>>1)|y;} ll bisz(ll bit){ll size=0;while(bit>0){bit/=2;size++;}return size;} ll ransu(){static unsigned int TX=123456789, TY=362436069, TZ=521288629, TW=88675123;unsigned int tt=(TX^(TX<<11));TX=TY;TY=TZ;TZ=TW;return (TW=(TW^(TW>>19))^(tt^(tt>>8)));} int main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed; solve(); } void solve(){ ll x,y,a,b;cin>>x>>y>>a>>b; ll ans=0; if(x<=b){ while(x*(a-1)<=b){ x*=a; if(x>=y){ cout<<ans<<kai; return; } ans++; }} ans+=(y-x+b-1)/b-1; cout<<ans<<kai; }
#include<iostream> using namespace std; int main() { int v,t,s,d,s1,s2; cin>>v>>t>>s>>d; s1=t*v; s2=v*s; if(d>=s1&&d<=s2) cout<<"No"; else cout<<"Yes"; return 0; }
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <string.h> #include <vector> #include <queue> #include <cmath> #include <complex> #include <functional> #include <numeric> #include <iomanip> #include <cassert> #include <random> #include <chrono> /* #include <atcoder/all> */ /* using namespace atcoder; */ using namespace std; void debug_out(){ cout << "\n"; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1000000007; /* const ll MOD = 998244353; */ const ll INF = 1ll<<60; const double PI = acos(-1); struct INIT { INIT(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }}INIT; signed main() { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; int ans = 0, s = 0; REP(i, N){ ans += A[i]*A[i]; s += A[i]; } cout << N*ans-s*s << "\n"; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <complex> #include <queue> #include <set> #include <unordered_set> #include <list> #include <chrono> #include <random> #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <map> #include <unordered_map> #include <stack> #include <iomanip> #include <fstream> using namespace std; #define all(x) (x).begin(),(x).end() #define int long long int #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define F first #define S second #define inf 1e18 #define vi vector<int> #define Point complex<int> #define X real() #define Y imag() #define pii pair<int,int> #define num0Beg(x) __builtin_clz(x) // no. of zero in beginning of bit representation #define num0end(x) __builtin_ctz(x) // no. of zero in the end #define numOf1(x) __builtin_popcount(x) // no. of ones in bit rep. #define parity(x) __builtin_parity(x) // parity of 1 #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int32_t main() { FAST int tt=1; //cin >> tt; while(tt--) { int n; cin>>n; int a[n]; map<int,int> mp; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) mp[a[i]]++; int ans=0; for(int i=-200;i<=200;i++) { for(int j=i+1;j<=200;j++) { ans+=((mp[i]*mp[j])*(i-j)*(i-j)); } } cout<<ans; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 20; int h,w,a,b,ans; bool vis[N][N]; void dfs(int now,int x,int y,int a,int b){ if (now==h*w){ ans++; return ; } int nx=x+1,ny=y; if (x==w) nx=1,ny++; if (vis[x][y]) return dfs(now+1,nx,ny,a,b); if (a){ if (x!=w&&!vis[nx][ny]){ vis[x][y]=vis[nx][ny]=true; dfs(now+1,nx,ny,a-1,b); vis[x][y]=vis[nx][ny]=false; } if (y<h){ vis[x][y]=vis[x][y+1]=true; dfs(now+1,nx,ny,a-1,b); vis[x][y]=vis[x][y+1]=false; } } if (b){ vis[x][y]=true; dfs(now+1,nx,ny,a,b-1); vis[x][y]=false; } } int main(){ cin>>h>>w>>a>>b; dfs(0,1,1,a,b); cout<<ans; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long #define endl '\n' #define F first #define S second #define test ll cases; cin>>cases; for(ll testCase = 1; testCase <= cases; testCase++) #define fill(name, val) memset(name, val, sizeof(name)); #define mop(a, op, b) (((a%mod) op (b%mod))%mod + mod)%mod #define precise fixed<<setprecision(9) #define vll vector<ll> #define vvll vector<vll> #define pll pair<ll, ll> #define get(a, n) for(ll it = 0; it < n; it++) cin>>a[it]; #define pbds tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& p){ return(out<<'('<< p.first<< ", "<< p.second<<')'); } template<typename T> ostream& operator<<(ostream& out, const vector<T>& v){ out<<'['; for(auto i = v.begin(); i != v.end(); i++) out<<(i!=v.begin()?", ":"")<<(*i); return(out<<']'); } template<typename T> ostream& operator<<(ostream& out, const set<T>& s){ out<<'['; for(auto i = s.begin(); i != s.end(); i++) out<<(i!=s.begin()?", ":"")<<(*i); return(out<<']'); } template<typename T1, typename T2> ostream& operator<<(ostream& out, const map<T1, T2>& m){ out<<'{'<<endl; for(auto x : m) out<<" "<<x.first<<" -> "<<x.second<<endl; return(out<<'}'); } const ll MX = 1000001, mod = 1e9+7, inf = 1e18; const double eps = 1e-9; template<typename T> T mpow(T x, ll n){ T ans = 1; while(n){ if(n&1) ans = mop(ans, *, x); n >>= 1, x = mop(x, *, x); } return ans; } ll modinv(ll x){ return mpow(x, mod-2); } vll sieve(ll n = MX){ vll primes; vector<bool> nums(n, 1); primes.push_back(2); for(ll i = 4; i < n; i += 2) nums[i] = 0; for(ll i = 3; i < n; i += 2){ if(nums[i]){ primes.push_back(i); for(ll j = i*i; j < n; j += i) nums[j] = false; } } return primes; } vll FACT(1, 1); ll ncr(ll n, ll r){ if(n < r) return 0; while(n >= FACT.size()) FACT.push_back(mop(FACT[FACT.size()-1], *, FACT.size())); return mop(FACT[n],*,mop(modinv(FACT[r]),*,modinv(FACT[n-r]))); } ll fact(ll n){ while(n >= FACT.size()) FACT.push_back(mop(FACT[FACT.size()-1], *, FACT.size())); return FACT[n]; } void init(){ 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 } /* Author: 🆉🅴🅽🅾🅻🆄🆂 */ ll h, w, a, b; map<ll, map<ll, map<ll, map<ll, set<vvll>>>>> dp; vvll g; set<vvll> ans; ll ctr = 0; void calc(ll a, ll b, ll i, ll j){ if(i == h){ vvll v = g; ans.insert(v); return; } if(dp[a][b][i][j].count(g)) return; dp[a][b][i][j].insert(g); if(j == w) return calc(a, b, i+1, 0); if(g[i][j]) return calc(a, b, i, j+1); if(b > 0) calc(a, b-1, i, j+1); if(i+1 < h && !g[i+1][j] && a > 0){ g[i][j] = g[i+1][j] = 1; calc(a-1, b, i, j+1); g[i][j] = g[i+1][j] = 0; } if(j+1 < w && !g[i][j+1] && a > 0){ g[i][j] = g[i][j+1] = 2; calc(a-1, b, i, j+1); g[i][j] = g[i][j+1] = 0; } } int main(){ init(); cin>>h>>w>>a>>b; g.resize(h, vll(w)); calc(a, b, 0, 0); // for(auto x : ans){ // for(auto y : x){ // for(auto z : y) cout<<z<<' '; // cout<<endl; // } // cout<<endl; // } cout<<ans.size(); }
#include "bits/stdc++.h" #include "random" #include <unistd.h> using namespace std; #define ll long long #define vi vector<int> #define vl vector<long long> #define vvi vector<vi> #define pi pair<int,int> #define mp make_pair #define pb push_back #define MOD int(1e9) + 7 #define PAI 3.1415926535 #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define chmax(x, y) x = max(x,y) #define chmin(x, y) x = min(x,y) #define pr(x) cout << x << endl #define Endl endl #define rep(i, n) for(int i = 0 ; i < n; ++i) const int dx2[4] = {0,1,0,1}; const int dy2[4] = {0,0,1,1}; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; const int ddx[8] = {-1,0,1,-1,1,-1,0,1}; const int ddy[8] = {-1,-1,-1,0,0,1,1,1}; const int inf = 99999999; const ll linf = 1LL << 62; ll gcd(ll a,ll b){ if(a < b)swap(a , b); if(a % b != 0) return(gcd(b, a%b)); return b; } ll lcm(ll a,ll b){ if(a < b)swap(a , b); return (a / gcd(a , b)) * b; } int Uniform_Random(int a, int b){ // (a <= x <= b) random_device rd; mt19937 mt(rd()); uniform_int_distribution<int> rv1(a, b); return rv1(mt); } int main(){ ll N; cin >> N; ll ans = N; set<ll> st; for(ll i = 2; i <= sqrt(N); ++i){ ll cnt = 2; if(st.find(i) == st.end()){ while(pow(i, cnt) <= N){ st.insert(pow(i, cnt)); ans --; cnt ++; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define f first #define dbg(x) cout<< #x <<" "<<x<<"\n"; #define pii pair<ll,ll> using namespace std; void solve(){ ll n; cin>>n; unordered_set<ll> s; for(ll i=2; i*i<=n; ++i){ ll x = i*i; while(x<=n){ s.insert(x); x*=i; } } cout<<n-s.size()<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t,tt=0; // cin>>t; t=1; while(t--){ //cout<<"Case #"<<++tt<<": "; solve(); } }
#include <bits/stdc++.h> using namespace std; int main(){ int A, B; cin >> A >> B; int ans = 1; for (int i = 1; i <= B; i++){ int X = (A + i - 1) / i * i + i; if (X <= B){ ans = i; } } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n;cin >> n; string s;cin >> s; ll res = -1; if(s[0] != s[s.size() - 1])res = 1; else{ for(int i = 1;i < (int)(s.size() - 2);i++){ if(s[0] != s[i] && s[i + 1] != s[s.size() - 1])res = 2; } } cout << res << endl; }
#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 n; string s,t; cin>>n>>s>>t; vector<int>l,r; rep(i,n){ if(s[i]=='0'){l.push_back(i);} if(t[i]=='0'){r.push_back(i);} } if(l.size()!=r.size()){ cout<<-1<<endl; return; } int cnt=0; rep(i,l.size()){ if(l[i]!=r[i]){cnt++;} } cout<<cnt<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#line 2 "/home/snow/competitive-programming/competitive-programming-library/snow/template.hpp" #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> constexpr long long INF = 1LL << 60; constexpr long double PI = 3.141592653589; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep1(i, n) for (ll i = 1; i <= (n); ++i) #define rrep(i, n) for (ll i = (n - 1); i >= 0; --i) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define pb push_back #define to_s to_string #define len(v) (ll) v.size() #define debug(x) cout << #x << ": " << (x) << '\n' using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> tpl; template <typename T = ll> using vec = vector<T>; template <typename T = ll> using vec2 = vector<vector<T>>; template <typename T = ll> using vec3 = vector<vector<vector<T>>>; template < typename T > inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template < typename T > inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #line 4 "/home/snow/competitive-programming/competitive-programming-library/snow/io/setup.hpp" namespace snow{ struct IoSetup { IoSetup() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(10); } } iosetup; } #line 5 "/home/snow/competitive-programming/competitive-programming-library/snow/io/helper.hpp" template< typename T1, typename T2 > std::ostream &operator << (std::ostream &os, const std::pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > std::istream &operator >> (std::istream &is, std::pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T1, typename T2, typename T3 > std::ostream &operator << (std::ostream &os, const std::tuple< T1, T2, T3 > &t) { auto &[a, b, c] = t; os << a << " " << b << " " << c; return os; } template< typename T1, typename T2, typename T3 > std::istream &operator >> (std::istream &is, std::tuple< T1, T2, T3 > &t) { auto &[a, b, c] = t; is >> a >> b >> c; return is; } template< typename T > std::ostream &operator << (std::ostream &os, const std::vector< T > &v){ for (int i = 0; i < (int)v.size(); ++i) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > std::istream &operator >> (std::istream &is, std::vector< T > &v){ for(T &in : v) is >> in; return is; } template< typename T > std::ostream &operator << (std::ostream &os, const std::set< T > &st){ int ct = 0; for(auto& s : st) os << s << (++ct != st.size() ? " " : ""); return os; } template<class... T> void input(T&... a){ (std::cin >> ... >> a); } void print() { std::cout << '\n'; } template<class T, class... Ts> void print(const T& a, const Ts&... b){ std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; } int drop() { std::cout << '\n'; exit(0); } template<class T, class... Ts> int drop(const T& a, const Ts&... b){ std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; exit(0); } #line 4 "main.cpp" using namespace snow; // #include "snow/algorithm/binary-search.hpp" // #include "atcoder/modint" // using namespace atcoder; // using mint = modint1000000007; int main() { ll N, M; cin >> N >> M; vec<> A(N), B(M); cin >> A >> B; set<ll> a(ALL(A)); rep(i, M){ if(a.count(B[i])) a.erase(B[i]); else a.insert(B[i]); } print(a); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<char> vc; typedef queue<ll> ql; typedef deque<ll> dql; typedef priority_queue<ll> pql; typedef set<ll> sl; typedef pair<ll, ll> pl; typedef map<ll, ll> ml; typedef vector<vl> vvl; typedef vector<pl> vpl; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++) #define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--) #define all(v) (v).begin(), (v).end() ll mod(ll a, ll b) {return (a % b + b) % b;} ll quo(ll a, ll b) {return (a - mod(a, b)) / b;} template <typename T, typename U> bool chmin(T &a, const U b) {if(a > b) {a = b; return 1;} return 0;} template <typename T, typename U> bool chmax(T &a, const U b) {if(a < b) {a = b; return 1;} return 0;} const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const ll MAX = 2e5; const ld eps = 1e-9; const char newl = '\n'; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, u, v, w, ans = 0; cin >> n; vl d(n, INF); vector<vpl> G(n); rep(_, n-1) { cin >> u >> v >> w; u--, v--; G[u].push_back(pl(v, w)); G[v].push_back(pl(u, w)); } ql q; d[0] = 0; q.push(0); while(!q.empty()) { ll x = q.front(); q.pop(); for(pl p: G[x]) { if(d[p.first] == INF) { d[p.first] = d[x] ^ p.second; q.push(p.first); } } } vl exp2(60), cnt(60); exp2[0] = 1; rep(j, 59) exp2[j+1] = exp2[j] * 2; rep(i, n) rep(j, 60) if(d[i] & exp2[j]) cnt[j]++; rep(j, 60) (ans += exp2[j] % MOD * cnt[j] % MOD * (n - cnt[j]) % MOD) %= MOD; cout << ans << newl; return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn=2e5+10; const int mod=1e9+7; char s[maxn];int n,vis[300],ans=0,cnt=0; int dp[maxn][17],f[maxn][17][17]; int mp[300]; void dfs(int pos) { if(pos>n) return; for(int i=1;i<=16;i++){ dp[pos][i]=(dp[pos][i]+1LL*i*dp[pos-1][i]%mod)%mod; dp[pos][i]=(dp[pos][i]+1LL*(17-i)*dp[pos-1][i-1]%mod)%mod; } if(pos!=1){ dp[pos][1]=(dp[pos][1]+15)%mod; }else{ dp[pos][1]=(dp[pos][1]+(mp[s[pos]]-1))%mod; } if(pos!=1){ for(int i=0;i<mp[s[pos]];i++){ if(vis[i]==0) dp[pos][cnt+1]=(dp[pos][cnt+1]+1)%mod; else dp[pos][cnt]=(dp[pos][cnt]+1)%mod; } } if(vis[mp[s[pos]]]==0) cnt++,vis[mp[s[pos]]]=1; //cout<<pos<<" "<<dp[pos][1]<<endl; dfs(pos+1); } int main() { for(int i=0;i<10;i++) mp[i+'0']=i; for(int i=0;i<6;i++) mp[i+'A']=i+10; scanf("%s",s+1);n=strlen(s+1);int k;scanf("%d",&k); dfs(1);if(cnt==k) dp[n][k]++; cout<<dp[n][k]<<endl; }
#include<bits/stdc++.h> #define ll long long #define INF LLONG_MAX #define MAXN 1000000000 #define mod 1000000007 #define PI 3.14159265 #define pb push_back #define all(x) x.begin(),x.end() #define d1(x) cerr<<#x<<":"<<x<<endl; #define d2(x,y) cerr<<#x<<":"<<x<<' '<<#y<<":"<<y<<endl; #define fast std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) using namespace std; void solve(){ int m , h ; cin>>m>>h ; if(h%m == 0 ) cout<<"Yes" ; else cout<<"No" ; } int main(){ int t = 1 ; //scanf("%d",&t) ; while(t--){ solve(); } }
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools #include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const long long MOD = 1000000007; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template <typename T> bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } // doc: https://shibh308.github.io/library/library/lib/classes/modint.cpp.html template <i64 mod = MOD> struct ModInt{ i64 p; ModInt() : p(0){} ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;} ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;} ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;} ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;} ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;} ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;} ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;} ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;} ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;} friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){ stream << *x; return stream; } friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){ stream >> *x; return stream; } ModInt& operator++(){p = (p + 1) % mod; return *this;} ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;} bool operator==(const ModInt& y) const{return p == *y;} bool operator!=(const ModInt& y) const{return p != *y;} const i64& operator*() const{return p;} i64& operator*(){return p;} }; using mint = ModInt<>; void solve(long long n, long long p){ // doc: https://shibh308.github.io/library/library/lib/functions/modpow.cpp.html auto mpow = [](auto x, i64 y){ auto z = x; decltype(x) val = y & 1 ? x : decltype(x)(1); while(z *= z, y >>= 1) if(y & 1) val *= z; return val; }; cout << (mint(p - 1) * mpow(mint(p - 2), n - 1)) << endl; } signed main(){ long long N; scanf("%lld",&N); long long P; scanf("%lld",&P); solve(N, P); return 0; }
#include <iostream> #include <vector> #include <set> #include <algorithm> #include <ctime> using namespace std; template<class T> T gcd(T a, T b) { while (a > 0) { T t = a; a = b%a; b = t; } return b; } int main() { int N; cin>>N; vector<int> A(N); for (int &a: A) cin>>a; int m = A[0]; for (int a: A) m = min(a, m); set<int> F; for (int a: A) for (int f=1; f*f<=a; f++) if (a%f==0) F.insert(f), F.insert(a/f); int ans = 0; for (int f: F) if (f<=m) { int g = 0; for (int a: A) if (a%f==0) if (g==0) g = a; else g = gcd(g, a); if (g==f) ans++; } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #endif #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define endl "\n" typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef std::vector<vector<int> > vvi; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef vector<vll> vvll; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vector<double>> vvd; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<bool> vb; typedef vector<vb> vvb; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define repi(i, a, b) for (int i = (int)(a); i >= (int)(b); i--) #define pb push_back #define fi first #define se second #define all(a) a.begin(), a.end() #define sz(a) (int)(a.size()) template<typename T> void umin(T &a, T b) { a = min(a, b); } template<typename T> void umax(T &a, T b) { a = max(a, b); } ll nxt() { ll x; cin >> x; return x; } void setIO(string s) { #ifndef LOCAL ios_base::sync_with_stdio(0); cin.tie(0); freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); #endif } void yes() { cout << "YES\n"; } void no() { cout << "NO\n"; } const ll mod = 1e9 + 7; const ll INF = 2e9; void solve() { ll n = nxt(); vll a(n); rep(i, 0, n) cin >> a[i]; ll mn = *min_element(all(a)); map<ll, set<ll>> multiples; rep(i, 0, n) { ll x = a[i]; ll mxfac = sqrt(x) + 2; for (ll fac = 1; fac <= mxfac; fac++) { if (x % fac == 0) { multiples[fac].insert(i); multiples[x / fac].insert(i); } } } ll ans = 1; debug(multiples); for (auto &it : multiples) { if (sz(it.se) == 1) continue; ll fac = it.fi; ll g = 0; for (auto x : it.se) { g = __gcd(g, a[x]); } debug(g, fac, it.se); if (g == fac && g < mn) { ans++; } } cout << ans << endl; } int32_t main(){ // setIO("trapped"); ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(10); int T = 1; // cin >> T; for (int tc = 1; tc <= T; tc++) { // cout << "Case #" << tc << ": "; solve(); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define SP << " " #define LLi long long int using namespace std; int imax=2147483647; long long int llimax=9223372036854775807; //int型vectorを出力 void PV(vector<int> pvv) { rep(i, pvv.size()) cout << pvv[i] SP; cout << endl; } //LLi型vectorを出力 void PVL(vector<LLi> pvv) { rep(i, pvv.size()) cout << pvv[i] SP; cout << endl; } int main(){ LLi m; cin>> m; int fsz; vector<LLi> f; for(LLi i=1;i*i<m;i++){ if(m%i==0) f.push_back(i); } fsz=f.size(); if((LLi)sqrt(m)*(LLi)sqrt(m)==m) f.push_back((int)sqrt(m));//平方根が約数の場合 rep(i, fsz) f.push_back(m/f[fsz-1-i]); rep(i, f.size()) cout<< f[i] <<endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { string x; cin >> x; int n = x.find("."); if (n == std::string::npos) cout << x << endl; else { cout << x.substr(0, n) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; long long modpow(long long a, long long p){ if(p == 0)return 1; if(p % 2 == 1)return a * modpow(a, p - 1) % MOD; long long r = modpow(a, p / 2); return r * r % MOD; } long long modinv(long long a){ return modpow(a, MOD - 2); } long long C(int n, int r){ long long res = 1; for(int i = 1; i <= r; i++){ res = res * (n - i + 1) % MOD; res = res * modinv(i) % MOD; } return res; } int main(){ int n, m; scanf("%d %d", &n, &m); int sum = 0; for(int i = 0; i < n; i++){ int a; scanf("%d", &a); sum += a; } printf("%lld\n", C(m + n, sum + n)); return 0; }
#include <iostream> #include <math.h> #include <iomanip> #include <bits/stdc++.h> #include <string.h> #include <string> #include <algorithm> #define ll long long int #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; ll binomialCoeff(ll n,ll k) { ll res = 1; if(k >n - k) k = n - k; for(int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } ll power(ll x,ll y) { ll temp; if(y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return (temp*temp); else return (((x*temp))*temp); } //nCr % mod const int N0 = 2e6 + 5; ll fact[6*N0]; ll inv[6*N0],invfac[6*N0]; ll mod = 1e9 + 7; void factorial() { fact[0] = invfac[0] = fact[1] = invfac[1] = 1; inv[1] = 1; for(int i=2;i<=5*N0 + 10;i++) { fact[i] = (fact[i-1]*i)%mod; inv[i] = (inv[mod%i]*(mod - mod/i))%mod; invfac[i] = (invfac[i-1]*inv[i])%mod; } } vector<ll> primes; void Sieve(int n) { bool prime[n+1]; memset(prime, true, sizeof(prime)); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*p; i<=n; i += p) prime[i] = false; } } for (int p=2; p<=n; p++) if (prime[p]) primes.push_back(p); } //****************************************************** CHECK CONSTRAINTS ***************************************************************// const int N = 2e5 + 5; int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); ll n,m; ll sum = 0; cin >> n >> m; for(int i=0;i<n;i++) { ll x; cin >> x; sum += x; } m += n; sum += n; if(m < sum) return cout << 0,0; ll ans = 1; factorial(); //cout << m << " " << sum << '\n'; for(int i=0;i<sum;i++) { ll val = (m - i)*(inv[sum - i])%mod; //cout << val << "\n"; ans = (ans * val)%mod; } cout << ans; }
#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 ll INF=1e18; const int mod=998244353; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { 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; } mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } } c(2000005); vector<pair<ll,ll>>prime_factorize(ll N) { vector<pair<ll,ll>>res; for (ll i= 2; i*i<=N;i++) { if (N%i!=0){continue;} ll cnt=0; while(N%i==0){ cnt++; N/=i; } res.push_back({i, cnt}); } if(N!=1){res.push_back({N, 1});} return res; } int main(){ int n,m;cin>>n>>m; mint ans=0; for(int i=1;i<=m;i++){ auto f=prime_factorize(i); mint now=1; for(auto p:f){ now*=c(n-1+p.second,n-1); } ans+=now; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stack> #include <vector> #include <string> #include <set> #include <map> #include <random> #define rep(i,n) for (int i = 0; i < (n); ++i) #define repp(i,n,m) for (int i = m; i < (n); ++i) #define repl(i,n) for (long long i = 0; i < (n); ++i) #define reppl(i,n,m) for (long long i = m; i < (n); ++i) //#define int long long using namespace std; //#include <atcoder/all> //using namespace atcoder; using ll = long long; using ld = long double; using P = pair<int, int>; using PI = pair<pair<int,int>,int>; using PL = pair<long long, long long>; using PLL = pair<pair<long long, long long>, long long>; using Pxy = pair<long double, long double>; using Tiib = tuple<int, int, bool>; using vvl = vector<vector<ll>>; const int INF = 2001001007; const int modd = 1000000007; const long long modl = 1000000007LL; const long long mod = 998244353LL; const ll inf = 1e18; template <typename AT> void priv(vector<AT> &ar){ rep(i,ar.size()-1) cout << ar[i] << " "; cout << ar[ar.size()-1] << endl; } template <typename Q> void privv(vector<vector<Q>> &ar){ rep(i,ar.size()){ rep(j,ar[i].size()-1) cout << ar[i][j] << " "; cout << ar[i][ar[i].size()-1] << endl; } } template <typename S> bool range(S a, S b, S x){return (a <= x && x < b);} template <typename SS> void sor(vector<SS> &ar){sort(ar.begin(),ar.end());} template <typename SSS> void rev(vector<SSS> &ar){reverse(ar.begin(),ar.end());} template <typename SSSS> void chmin(SSSS &a, const SSSS &b){a = min(a,b);} template <typename SSSSS> void chmax(SSSSS &a, const SSSSS &b){a = max(a,b);} void yes(){cout << "Yes" << endl;} void no (){cout << "No" << endl;} void yn (bool t){if(t)yes();else no();} ll cel (ll a, ll b){ if (a % b == 0) return a / b; else return a / b + 1; } ll gcds(ll a, ll b){ ll c = a % b; while (c != 0){ a = b; b = c; c = a % b; } return b; } ll saiki(vector<vector<ll>> &dp, int n, int k){ if (dp[n][k] != -1) return dp[n][k]; if (n == k) return dp[n][k] = 1; if (n < k) return dp[n][k] = 0; if (k == 0) return dp[n][k] = 0; dp[n][k] = saiki(dp,n-1,k-1) + saiki(dp,n,2*k); dp[n][k] %= mod; return dp[n][k]; } int main(){ int n, k; cin >> n >> k; vector<vector<ll>> dp(3001,vector<ll>(6000,-1)); cout << saiki(dp,n,k) << endl; }
#include <iostream> #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double db; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define pll pair<ll,ll> #define pb push_back #define eb emplace_back #define mp make_pair #define ub(v,val) upper_bound(v.begin(),v.end(),val) #define np(str) next_permutation(str.begin(),str.end()) #define lb(v,val) lower_bound(v.begin(),v.end(),val) #define sortv(vec) sort(vec.begin(),vec.end()) #define rev(p) reverse(p.begin(),p.end()); #define v vector #define pi 3.14159265358979323846264338327950288419716939937510 #define len length() #define repc(i,s,e) for(ll i=s;i<e;i++) #define fi first #define se second #define mset(a,val) memset(a,val,sizeof(a)); #define mt make_tuple #define repr(i,n) for(i=n-1;i>=0;i--) #define rep(i,n) for(i=0;i<n;i++) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define at(s,pos) *(s.find_by_order(pos)) #define set_ind(s,val) s.order_of_key(val) long long int M = 1e9 + 7 ; long long int inf = 9 * 1e18; //CLOCK ll begtime = clock(); #define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //CLOCK ENDED ll n, m; // modular exponentiation ll binpow(ll val, ll deg) { if (deg < 0) return 0; if (!deg) return 1 % M; if (deg & 1) return binpow(val, deg - 1) * val % M; ll res = binpow(val, deg >> 1); return (res * res) % M; } //binomial ll modinv(ll n) { return binpow(n, M - 2); } //GCD ll gcd (ll a, ll b) { if (b == 0) return a; else return gcd (b, a % b); } int main() { // your code goes here IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll i, j, t, k, x, y, z, N; cin >> n >> m; cout << n / m; return 0; }
#include "bits/stdc++.h" using namespace std; #define endl "\n" using ll = long long; const int MOD = 1e9 + 7; void solve() { int n, w; cin >> n >> w; cout << n / w << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using vi = vector<int>; using si = set<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define P pair<int,int> #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define rev(i,s,n) for(int i=(s);i>=(int)(n);i--) #define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i] #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) ll x;cin >> x #define cci(x,y) ll x,y;cin >> x >> y #define co(x) cout << x << endl #define pb push_back #define eb emplace_back #define rz resize #define sz(x) int(x.size()) #define yn cout<<"Yes"<<endl;else cout<<"No"<<endl #define YN cout<<"YES"<<endl;else cout<<"NO"<<endl template<class T>void chmax(T &x,T y){x=max(x,y);} template<class T>void chmin(T &x,T y){x=min(x,y);} int main(){ cii(t); V<ss> ans; while(t--){ cii(n); vvi v(2,vi(2,0)); V<ss> s(3); rep(i,0,3)ci(s[i]); rep(i,0,3){ int fr=s[i][0]-'0'; int ba=s[i][n*2-1]-'0'; v[ba][fr]=1; } ss now; ss s0,s1; rep(i,0,n){ s0+="0"; s1+="1"; } if(v[0][0]==0){ now=s0+"1"+s0; } else { if(v[1][1]==0){ now=s1+"0"+s1; } else { if(v[0][1]==0)now=s0+s1+"0"; else now=s1+s0+"1"; } } ans.pb(now); } for(ss s:ans)co(s); }
#include<bits/stdc++.h> #define int long long #define zmrl(x) (int)((x).size()) #define ahen(x) (x).begin(), (x).end() #define pb push_back #define fi first #define se second using namespace std; using pii = pair<int, int>; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; priority_queue<int, vector<int>, greater<int> > pq; for (int i=0; i<=200000; i++) pq.push(i); priority_queue<int, vector<int>, greater<int> > ers; for (int i=1; i<=n; i++) { int a; cin>>a; ers.push(a); while (zmrl(ers)) { if (ers.top() > pq.top()) break; while (zmrl(ers) && ers.top() < pq.top()) ers.pop(); if (zmrl(ers) && ers.top() == pq.top()) { ers.pop(); pq.pop(); } } cout << pq.top() << '\n'; } 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; const int mod = 998244353; struct mint { ll x; mint (ll x = 0) : x((x % mod + mod) % mod) {} mint operator -() const {return mint(-x);} mint& operator +=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator -=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator *=(const mint a) {(x *= a.x) %= mod; return *this;} mint operator +(const mint a) const {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; } mint inv() const {return pow(mod - 2);} mint operator /=(const mint a) {return *this *= a.inv();} mint operator /(const mint a) {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;} mint dp[5005][5005]; bool exist[5005][5005]; int main() { int h, w, k; cin >> h >> w >> k; map<P,char> mp; rep(i,k) { int y, x; cin >> y >> x; y--; x--; exist[y][x] = true; char c; cin >> c; mp[P(y, x)] = c; } mint inv_3 = mint(1) / 3; dp[0][0] = !exist[0][0] ? inv_3 : 1; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i == 0 && j == 0) continue; if (i) { if (exist[i - 1][j]) { char c = mp[P(i - 1, j)]; if (c == 'X' || c == 'D') dp[i][j] += dp[i - 1][j]; } else { dp[i][j] += dp[i - 1][j] * 2; } } if (j) { if (exist[i][j - 1]) { char c = mp[P(i, j - 1)]; if (c == 'X' || c == 'R') dp[i][j] += dp[i][j - 1]; } else { dp[i][j] += dp[i][j - 1] * 2; } } if (!exist[i][j]) dp[i][j] *= inv_3; } } mint ans = dp[h - 1][w - 1] * mint(3).pow(h * w - k); if (!exist[h - 1][w - 1]) ans *= 3; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; long double dp[101][101][101]; long double f(int x, int y, int z) { long double ans = 0; if (dp[x][y][z] > 0) { return dp[x][y][z]; } if (x == 100 || y == 100 || z == 100) { return 0; } else { ans += x * f(x + 1, y, z); ans += y * f(x, y + 1, z); ans += z * f(x, y, z + 1); ans /= (x + y + z); ans += 1; } dp[x][y][z] = ans; return ans; } int x, y, z; int main() { cin >> x >> y >> z; cout << std::fixed << std::setprecision(15) << f(x, y, z) << endl; return 0; }
#include <bits/stdc++.h> //#include "debugger.h" #define int long long using namespace std; int gcd (int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int lcm (int a, int b) { return (a / gcd(a, b)) * b; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int Lcm = 1; for (int i = 2; i <= n; i++) { Lcm = lcm (Lcm, i); } cout << Lcm + 1; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef map<ll,ll>::iterator itll; typedef long double ld; typedef map<ll,ll> mapll; #define con continue #define pb push_back #define fi first #define se second #define fr front() #define INF 1000000000000000000 #define all(vl) vl.begin(),vl.end() #define m_p make_pair #define sz(a) sizeof(a) #define forn(mp,it) for(it = mp.begin();it!=mp.end();it++) #define FOR(i,a,n) for(ll i=a;i<n;i++) #define FORE(i,a,n) FOR(i,a,n+1) #define Endl endl #define eNdl endl #define enDl endl #define endL endl #define MIN(x,y) x=min(x,y) #define MAX(x,y) x=max(x,y) #define left 0 #define right 1 const double one = 1.0; const double PI = 3.14159265/*358979323846*/; const ll maxn = 1e4+10; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin>>n; ll an = n; n = 1; FOR(i,0,an)n*=2; vector<ll> vl(n); map<ll,ll> pos; FOR(i,0,n) { cin>>vl[i]; pos[vl[i]] = i; } ll ans = 0; while(1) { vector<ll> winner; if(vl.size() == 2) { if(vl[0]>vl[1])ans = vl[1]; else ans = vl[0]; break; } for(int i = 0;i<vl.size();i+=2) { ll a = vl[i],b = vl[i+1]; if(a<b)winner.pb(b); else winner.pb(a); } vl = winner; } cout<<1+pos[ans]<<Endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define reps(i, s, n) for (int i = (s); i < (int)(n); ++i) #define all(obj) begin(obj), end(obj) #define cinv(a) rep(i,(int)a.size()) cin >> a[i] #define debug(a) rep(i,(int)a.size()) cout << a[i] << " " #define pb push_back #define eb emplace_back typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<vb> vvb; 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;} int main() { ll n, C; cin >> n >> C; vector<pair<ll, ll>> p; rep(i, n) { ll a, b, c; cin >> a >> b >> c; p.eb(a, c); p.eb(b+1, -c); } sort(all(p)); ll ans = 0; ll x = 0; ll y = 0; for (auto [i,j] : p) { ans += (i-x) * min(y, C); x = i; y += j; } cout << ans << endl; }
#include <bits/stdc++.h> #if MYDEBUG #include "lib/cp_debug.hpp" #else #define DBG(...) ; #endif #if __cplusplus <= 201402L template <typename T> T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #endif using LL = long long; constexpr LL LINF = 334ll << 53; constexpr int INF = 15 << 26; constexpr LL MOD = 1E9 + 7; namespace Problem { using namespace std; class Solver { public: void solve() { int a, b, c; vector<vector<vector<double>>> memo(101, vector<vector<double>>(101, vector<double>(101, -1))); cin >> a >> b >> c; cout << calc(a, b, c, memo) << endl; } double calc(int a, int b, int c, vector<vector<vector<double>>> &memo) { if (a >= 100 || b >= 100 || c >= 100) { return 0; } else if (memo[a][b][c] >= 0) { return memo[a][b][c]; } else { double ret = 1.0; ret += (double)(a) / (double)(a + b + c) * calc(a + 1, b, c, memo); ret += (double)(b) / (double)(a + b + c) * calc(a, b + 1, c, memo); ret += (double)(c) / (double)(a + b + c) * calc(a, b, c + 1, memo); return memo[a][b][c] = ret; } } }; } // namespace Problem int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(12); Problem::Solver sol; sol.solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int h,w,a[110][110],maxx=0,minn=1010; long long sum=0; cin>>h>>w; for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ cin>>a[i][j]; minn=min(minn,a[i][j]); maxx=max(maxx,a[i][j]); } } if(maxx==minn) { cout<<0; return 0; } else { for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++) { sum+=a[i][j]-minn; } } } cout<<sum; return 0; }
//インクルードなど #include<bits/stdc++.h> using namespace std; typedef long long ll; //イテレーション #define REP(i,n) for(ll i=0;i<ll(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define FORD(i,a,b) for(ll i=a;i>=ll(b);i--) #define FORA(i,I) for(const auto& i:I) //x:コンテナ #define ALL(x) x.begin(),x.end() #define SIZE(x) ll(x.size()) //定数 #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //問題による //略記 #define F first #define S second //出力(空白区切りで昇順に) #define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()<<endl; //aをbで割る時の繰上げ,繰り下げ ll myceil(ll a, ll b) {return (a + (b - 1)) / b;} ll myfloor(ll a, ll b) {return a / b;} int main() { //input ll N, Q; cin >> N >> Q; vector<ll> A(N); vector<ll> B(N); REP(i, N) { cin >> A[i]; B[i] = A[i] - i; } vector<ll> K(Q); REP(i, Q) cin >> K[i]; //calculation REP(i, Q) { ll k = K[i]; if (k >= B[N - 1]) { cout << A[N - 1] + 1 + k - B[N - 1] << endl; continue; } REP(j, N) { if (B[j] > k) { if (j == 0) cout << k << endl; else cout << A[j - 1] + 1 + k - B[j - 1] << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; constexpr ll MOD = 1e9 + 7; //constexpr ll MOD = 998244353; //constexpr ll MOD = ; ll mod(ll A, ll M) {return (A % M + M) % M;} constexpr ll INF = 1LL << 60; 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;} ll divceil(ll A, ll B) {return (A + (B - 1)) / B;} #define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false) class graph { public: struct edge { ll to, cost; edge(ll to, ll cost) : to(to), cost(cost) {} bool operator<(const edge &right) const { return cost < right.cost; } bool operator>(const edge &right) const { return cost > right.cost; } }; ll N; vector<vector<edge>> G; graph(ll n) { N = n; G.resize(N); } void connect(ll sv, ll gv, ll c) { G.at(sv).push_back(edge(gv, c)); } void connect2(ll v0, ll v1, ll c) { connect(v0, v1, c), connect(v1, v0, c); } void dijkstra(ll sv, vector<ll> &costs) { costs = vector<ll>(N, INF); costs.at(sv) = 0; priority_queue<pll, vector<pll>, greater<pll>> pque; for (auto e : G.at(sv)) { pque.push(make_pair(e.cost, e.to)); } while (!pque.empty()) { pll cv = pque.top(); ll c = cv.first, v = cv.second; pque.pop(); if (costs.at(v) != INF) continue; costs.at(v) = c; for (auto e : G.at(v)) { if (costs.at(e.to) != INF) continue; pque.push(make_pair(c + e.cost, e.to)); } } } }; ll R, C; ll tptol(ll r, ll c, ll b) { return 2 * (R * c + r) + b; } int main() { cin >> R >> C; vector<vector<ll>> A(R, vector<ll>(C - 1)); for (ll i = 0; i < R; i++) { for (ll j = 0; j < C - 1; j++) { cin >> A.at(i).at(j); } } vector<vector<ll>> B(R - 1, vector<ll>(C)); for (ll i = 0; i < R - 1; i++) { for (ll j = 0; j < C; j++) { cin >> B.at(i).at(j); } } graph gr(2 * R * C); for (ll r = 0; r < R; r++) { for (ll c = 0; c < C - 1; c++) { gr.connect(tptol(r, c, 0), tptol(r, c + 1, 0), A.at(r).at(c)); //cerr << r << " " << c << " " << r << " " << c + 1 << " " << A.at(r).at(c) << endl; } } for (ll r = 0; r < R; r++) { for (ll c = 1; c < C; c++) { gr.connect(tptol(r, c, 0), tptol(r, c - 1, 0), A.at(r).at(c - 1)); //cerr << r << " " << c << " " << r << " " << c - 1 << " " << A.at(r).at(c - 1) << endl; } } for (ll r = 0; r < R - 1; r++) { for (ll c = 0; c < C; c++) { gr.connect(tptol(r, c, 0), tptol(r + 1, c, 0), B.at(r).at(c)); //cerr << r << " " << c << " " << r + 1 << " " << c << " " << B.at(r).at(c) << endl; } } for (ll r = 0; r < R; r++) { for (ll c = 0; c < C; c++) { gr.connect(tptol(r, c, 0), tptol(r, c, 1), 0); gr.connect(tptol(r, c, 1), tptol(r, c, 0), 1); } } for (ll r = 1; r < R; r++) { for (ll c = 0; c < C; c++) { gr.connect(tptol(r, c, 1), tptol(r - 1, c, 1), 1); } } vector<ll> costs; gr.dijkstra(0, costs); ll ans = costs.at(tptol(R - 1, C - 1, 0)); cout << ans << endl; }
#include<cstdio> #include<queue> #define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i) using namespace std; const int N=505,INF=0x3fffffff; template<typename T>void read(T &x) { bool neg=false; unsigned char c=getchar(); for(;(c^48)>9;c=getchar())if(c=='-')neg=true; for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48); if(neg)x=-x; } struct node { int x,y,d; bool operator<(node b)const{return b.d<d;} }n1,n2; int n,m,a[N][N],b[N][N],dis[N][N]; priority_queue<node> q; int main() { read(n);read(m); F(i,0,n)F(j,0,m-1)read(a[i][j]); F(i,0,n-1)F(j,0,m)read(b[i][j]); F(i,0,n)F(j,0,m)dis[i][j]=INF; dis[0][0]=0; n1.x=n1.y=n1.d=0; q.push(n1); while(!q.empty()) { int x,y,d; n1=q.top();q.pop(); if(dis[x=n1.x][y=n1.y]!=(d=n1.d))continue; if(x<n)if(dis[x+1][y]>dis[x][y]+b[x][y]) { dis[x+1][y]=dis[x][y]+b[x][y]; n2.x=x+1;n2.y=y;n2.d=dis[x+1][y]; q.push(n2); } F(i,0,x)if(dis[i][y]>dis[x][y]+x-i+1) { dis[i][y]=dis[x][y]+x-i+1; n2.x=i;n2.y=y;n2.d=dis[i][y]; q.push(n2); } if(y&&dis[x][y-1]>dis[x][y]+a[x][y-1]) { dis[x][y-1]=dis[x][y]+a[x][y-1]; n2.x=x;n2.y=y-1;n2.d=dis[x][y-1]; q.push(n2); } if(y<m&&dis[x][y+1]>dis[x][y]+a[x][y]) { dis[x][y+1]=dis[x][y]+a[x][y]; n2.x=x;n2.y=y+1;n2.d=dis[x][y+1]; q.push(n2); } } printf("%d\n",dis[n-1][m-1]); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { int H, W; cin >> H >> W; vector<vector<int>> grid(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; if (c == '+') grid[i][j] = 1; else grid[i][j] = -1; } } vector<vector<int>> dp(H, vector<int>(W, 0)); for (int i = H-1; i >= 0; i--) { for (int j = W-1; j >= 0; j--) { if (i == H-1 && j == W-1) continue; if ((i+j)%2 == 0) { dp[i][j] = -1e9; if (i < H-1) dp[i][j] = max(dp[i][j], dp[i+1][j]+grid[i+1][j]); if (j < W-1) dp[i][j] = max(dp[i][j], dp[i][j+1]+grid[i][j+1]); } if ((i+j)%2 == 1) { dp[i][j] = 1e9; if (i < H-1) dp[i][j] = min(dp[i][j], dp[i+1][j]-grid[i+1][j]); if (j < W-1) dp[i][j] = min(dp[i][j], dp[i][j+1]-grid[i][j+1]); } } } if (dp[0][0] > 0) cout << "Takahashi\n"; else if (dp[0][0] == 0) cout << "Draw\n"; else cout << "Aoki\n"; return 0; }
#include <bits/stdc++.h> using namespace std; // using mint = long double; // using mint = modint998244353; // using mint = modint1000000007; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> T; typedef pair<ll, vector<ll>> Pd; const ll INF = 1e18; const ll fact_table = 3200008; priority_queue<ll> pql; priority_queue<P> pqp; // big priority queue priority_queue<ll, vector<ll>, greater<ll>> pqls; priority_queue<P, vector<P>, greater<P>> pqps; // small priority queue // top pop ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1}; //↓,→,↑,← /* #define endl "\n" #ifdef ENJAPMA #undef endl #endif */ #define p(x) cout << x << endl; #define el cout << endl; #define pe(x) cout << x << " "; #define ps(x) cout << fixed << setprecision(25) << x << endl; #define pu(x) cout << (x); #define pb push_back #define lb lower_bound #define ub upper_bound #define pc(x) cout << x << ","; #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = a; i <= (b); i++) #define rep3(i, a, b) for (ll i = a; i >= (b); i--) #define all(c) begin(c), end(c) typedef vector<ll> vec; typedef vector<vector<ll>> mat; // vec v(n) -> 長さnのベクトルを宣言 // mat dp(h, vec(w)) -> h * w の行列を宣言 // const ll mod = 998244353ll; const ll mod = 1000000007ll; ll mypow(ll a, ll b, ll m = mod) { ll x = 1; while (b) { while (!(b & 1)) { (a *= a) %= m; b >>= 1; } (x *= a) %= m; b--; } return x; } vec rv(ll read) { vec res(read); for (int i = 0; i < read; i++) { cin >> res[i]; } return res; } /* ll fact[fact_table + 5], rfact[fact_table + 5]; void c3_init() { fact[0] = rfact[0] = 1; for (ll i = 1; i <= fact_table; i++) { fact[i] = (fact[i - 1] * i) % mod; } rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod); for (ll i = fact_table; i >= 1; i--) { rfact[i - 1] = rfact[i] * i; rfact[i - 1] %= mod; } return; } ll c3(ll n, ll r) { return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod; } */ bool icpc = false; bool multicase = false; ll n, m, k, q; string s; ll dp[1050000]; vector<P> ban[20]; bool solve() { cin >> n >> m; rep(i, m) { int a, b, c; cin >> a >> b >> c; ban[a].pb(P(b, c)); } for (int i = 1; i <= n; i++) { sort(all(ban[i])); } dp[0] = 1; ll bit = (1ll << n); for (ll set = 0; set < bit; set++) { // set -> newset (= set + (1ll << j)) for (int j = 0; j < n; j++) { if ((set >> j) & 1) continue; ll newset = set + (1ll << j); ll newcnt = __builtin_popcount(newset); bool dame = false; for (auto [maxi, cnt] : ban[newcnt]) { ll r = 0; for (int k = 0; k < n; k++) { if ((k < maxi) && (newset >> k) & 1ll) r++; } if (r > cnt) dame = true; } if (!dame) dp[newset] += dp[set]; } } p(dp[(1ll << n) - 1]); return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); if (icpc) { while (solve()) ; return 0; } ll q, testcase = 1; if (multicase) { cin >> q; } else { q = 1; } while (q--) { // cout << "Case #" << testcase << ": "; solve(); testcase++; } // solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #ifdef ENABLE_DEBUG #define dump(a) cerr<<#a<<"="<<a<<endl #define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl #else #define dump(a) #define dumparr(a,n) #endif #define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++) #define For(i, a) FOR(i, 0, a) #define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--) #define Rev(i, a) REV(i, 0, a) #define REP(a) For(i, a) #define SIGN(a) (a==0?0:(a>0?1:-1)) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair<ll, ll> pll; typedef pair<ll,pll> ppll; typedef vector<ll> vll; typedef long double ld; typedef pair<ld,ld> pdd; pll operator+(pll a,pll b){ return pll(a.first+b.first,a.second+b.second); } pll operator-(pll a,pll b){ return pll(a.first-b.first,a.second-b.second); } pll operator*(ll a,pll b){ return pll(b.first*a,b.second*a); } const ll INF=(1LL<<60); #if __cplusplus<201700L ll gcd(ll a, ll b) { a=abs(a); b=abs(b); if(a==0)return b; if(b==0)return a; if(a < b) return gcd(b, a); ll r; while ((r=a%b)) { a = b; b = r; } return b; } #endif template<class T> bool chmax(T& a,const T& b){ if(a<b){ a=b; return true; } return false; } template<class T> bool chmin(T& a,const T& b){ if(a>b){ a=b; return true; } return false; } template<class S,class T> std::ostream& operator<<(std::ostream& os,pair<S,T> a){ os << "(" << a.first << "," << a.second << ")"; return os; } template<class T> std::ostream& operator<<(std::ostream& os,vector<T> a){ os << "[ "; REP(a.size()){ os<< a[i] << " "; } os<< " ]"; return os; } const long long MOD = 1000000007; long long modpow(long long a,long long b,unsigned long long MOD){ long long base=a,ans=(1LL); while(b){ if(b&1){ ans=ans*base%MOD; } base=base*base%MOD; b>>=1; } return ans; } void solve(long long N, long long P){ cout<<(P-1)*modpow(P-2,N-1,MOD)%MOD<<endl; } int main(){ cout<<setprecision(1000); long long N; scanf("%lld",&N); long long P; scanf("%lld",&P); solve(N, P); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch-'0'); ch=getchar(); } return x*f; } const int maxn=2e5+5; ll n,k,a[maxn],cnt[15]; ll f(ll x) { for(int i=0; i<=9; i++) cnt[i]=0; while(x) { cnt[x%10]++; x/=10; } ll g1=0,g2=0; for(int i=9; i>=0; i--) { for(int j=0; j<cnt[i]; j++) { g1=g1*10+i; } } for(int i=0; i<=9; i++) { for(int j=0; j<cnt[i]; j++) { g2=g2*10+i; } } return g1-g2; } int main() { n=read(),k=read(); a[0]=n; for(int i=1; i<=k; i++) { a[i]=f(a[i-1]); } printf("%lld\n",a[k]); }
#include <bits/stdc++.h> using namespace std;using ll=long long;using vi=vector<int>;using vvi=vector<vi>;using vl=vector<ll>;using vvl=vector<vl>;using P=pair<int,int>;using PL=pair<ll,ll>;using vp=vector<P>;using vpl=vector<PL>; #define all(v)(v).begin(),(v).end() #define rall(v)(v).rbegin(),(v).rend() // cin and cout of a vector template<class T>class iterable{static false_type c(string v);template<class U>static auto c(U v)->decltype(all(v),true_type());static false_type c(...);public:const static bool value=decltype(c(declval<T>()))::value;}; template<class T,enable_if_t<iterable<T>::value,int> =0>ostream&operator<<(ostream&o,const T&v){for(auto&&i:v)o<<i<<' ';return o;} template<class T>istream&operator>>(istream&i,vector<T>&v){for(T&j:v)i>>j;return i;}template<class T>vector<T>&operator<<(vector<T>&v,const T&t){v.push_back(t);return v;} // for loop #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define revv(i, a, b) for (int i = (int)(b-1); i >= (int)(a); i--) #define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--) #define rev1(i, n) for(int i = (int)(n); i > 0; i--) // #define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++) // #define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--) // #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) // #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) // Yes/no #define cho(n,a,b) cout<< ((n)?a:b)<<endl; void YES(int n){cho(n,"YES","NO");}void Yes(int n){cho(n,"Yes","No");}void Poss(int n){cho(n,"Possible","Impossible");} #define pb push_back #define revsort(v) sort((v).rbegin(), (v).rend()) #define sortv(v) sort((v).begin(), (v).end()) // max/min template<class T, class U>bool chmax(T&a,const U&b){bool x=a<b;x?a=b:b;return x;}template<class T, class U>bool chmin(T&a,const U&b){bool x=a>b;x?a=b:b;return x;} template<class T, class U>auto max(const T&a,const U&b){return a<b?b:a;}template<class T, class U>auto min(const T&a,const U&b){return a<b?a:b;} //======================================== //======================================== //======================================== template<class T>auto minv(const T&a){return *min_element(all(a));} template<class T>auto maxv(const T&a){return *max_element(all(a));} template<class T>auto sumv(const T&a){ll sum = 0; for(auto x: a) sum+= x; return sum;} template<class T>void pr(const T&a){cout << a << endl;} template<class T>void pr_precise(const T&a){cout.precision(10); cout << fixed << a << endl;} int check(ll a, ll b){ if (a == 0 && b == 0) return 0; if (a == b || a + b <= 3) return 1; if ((a-b)%2 == 0) return 2; // long + long if (a + b <= 6) return 2; // short + short. miss this case; if ( abs(a - b) <= 3) return 2; // long + short return 3; } int main() { int cases = 1; // cin >> cases; rep(CASE, cases){ ll a, b, c; cin >> a >> b >> c; double dp[101][101][101] = {{{0.}}}; rev(i,100){ rev(j,100){ rev(k, 100){ dp[i][j][k] = (i*dp[i+1][j][k] + j*dp[i][j+1][k] + k*dp[i][j][k+1])/(i+j+k) + 1.; } } } pr_precise(dp[a][b][c]); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; int main() { double a, b, c; cin >> a >> b >> c; vector<vector<vector<double>>> dp( 110, vector<vector<double>>(110, vector<double>(110, 0.0))); dp[a][b][c] = 1.0; for(int i = 0; i <= 100; i++) { for(int j = 0; j <= 100; j++) { for(int k = 0; k <= 100; k++) { if(i == 0 && j == 0 && k == 0) { continue; } dp[i + 1][j][k] += dp[i][j][k] * (double)i / (i + j + k); dp[i][j + 1][k] += dp[i][j][k] * (double)j / (i + j + k); dp[i][j][k + 1] += dp[i][j][k] * (double)k / (i + j + k); } } } double ans = 0; for(int i = 0; i < 100; i++) { for(int j = 0; j < 100; j++) { ans += dp[i][j][100] * (100 - c); ans += dp[i][100][j] * (100 - b); ans += dp[100][i][j] * (100 - a); } } printf("%.10f\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define endl "\n" #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // const ll mod = 1e9 + 7; const ll mod = 998244353; const ll sz = 1e6 + 5; vector<ll> fac(sz, 1); vector<bool> prime(sz, true); vector<ll> fib(sz, 0); ll powmod(ll a, ll b) { ll res=1; a%=mod; assert(b>=0); for(;b;b>>=1) { if(b&1) res=res*a%mod; a=a*a%mod; } return res; } ll nCr(ll n, ll r) { return ( ( ( (fac[n] * powmod(fac[n-r], mod-2)) % mod ) * powmod(fac[r], mod-2) ) % mod ); } 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; } // * Always PASS arrays, strings etc to functions BY REFERENCE void solve() { ll n, x; cin >> n >> x; string s; cin >> s; for (auto c: s) { if (c == 'o') x += 1; else x = max(0ll, x-1); } cout << x << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // fib[1] = 1; // for (int i = 2; i < sz; i++) // fib[i] = (fib[i-1] + fib[i-2]) % mod; // for (ll i = 2; i < sz; i++) // fac[i] = (fac[i-1] * i) % mod; // prime[0] = prime[1] = false; // for (ll i = 2; i*i < sz; i++) { // if (!prime[i]) // continue; // for (ll j = i*i; j < sz; j += i) // prime[j] = false; // } int t = 1; // cin >> t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, x; string s; cin >> n >> x >> s; for (auto &c : s) { if (c == 'o') { x++; } else { x--; } if (x < 0) { x = 0; } } cout << x << "\n"; }