code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <algorithm> #define N 100007 #define mod 19260817 #define mid ((l + r) >> 1) using namespace std; typedef long long ll; inline ll rd() { ll x = 0; bool f = 0; char c = getchar(); for(; !isdigit(c); c = getchar()) if (c == '-') f = 1; for(; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; } inline ll fpow(ll x, ll t, ll p) { ll res = 1; x = x % p; while (t) { if (t & 1) res = 1ll * res * x % p; x = 1ll * x * x % p; t >>= 1; } return res; } int main() { int a = rd(); int b = rd(); int c = rd(); int t; if (b > 1 && c >= 2) t = fpow(b, c, 4) + 4; else t = fpow(b, c, 10000000); printf("%lld\n", fpow(a, t, 10)); return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<math.h> #include<iostream> #include<cmath> #include<queue> #include<vector> #define INF 0x7fffffff #define IOS ios::sync_with_stdio(false);cin.tie();cout.tie(0) using namespace std; typedef long long ll; const int N=1e5+100; //const int mod=1e9+7; string str; bool st[N]; int main() { IOS; int n; cin>>n; cin>>str; if(n==1) {cout<<"-1"<<endl;return 0;} if(str[0]!=str[n-1]) { cout<<"1"<<endl; } else { int w1=0; for(int i=0;i<n;i++) { if(str[i]!=str[0]) st[i]=true; } int flag=0; for(int i=1;i<n;i++) if(st[i-1]&&st[i]) flag=1; if(!flag) cout<<"-1"<<endl; else cout<<"2"<<endl; } return 0; }
#pragma region Macros #include <bits/stdc++.h> using namespace std; using ll = long long; #define REP2(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i) #define REP3(i, l, r) for (int i = (l), i##_len = (int)(r); i < i##_len; ++i) #define GET_MACRO_REP(_1, _2, _3, NAME, ...) NAME #define REP(...) GET_MACRO_REP(__VA_ARGS__, REP3, REP2) (__VA_ARGS__) #define RREP2(i, n) for (int i = (n - 1); i >= 0; --i) #define RREP3(i, l, r) for (int i = (r - 1), i##_len = (l); i >= i##_len; --i) #define GET_MACRO_RREP(_1, _2, _3, NAME, ...) NAME #define RREP(...) GET_MACRO_REP(__VA_ARGS__, RREP3, RREP2) (__VA_ARGS__) #define IN(type, n) type n; cin >> n #define INALL(v) for (auto &e : v) { cin >> e; } #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #ifdef _DEBUG #define DEBUG(x) cout << #x << ": " << x << endl #else #define DEBUG(x) #endif 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; } void yes() { cout << "Yes" << endl; } void no() { cout << "No" << endl; } #pragma endregion int main() { IN(ll, N); vector<ll> As(N); vector<ll> Bs(N); INALL(As); INALL(Bs); ll ans = 0; REP(i, N) { ans += As.at(i) * Bs.at(i); } if (ans == 0) yes(); else no(); 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 REPE(i, l, n) for (ll i = l; i <= ll(n); i++) #define FORA(i, I) for (const auto& i : I) #define ALL(v) v.begin(), v.end() #define UQ(v) v.erase(unique(ALL(v)), v.end()) #define ACM(v) accumulate(ALL(v), 0LL) #define C cin >> #define P(str) cout << str << endl #define VLL vector<ll> #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) const long long INF = 1100000000000000000; int main() { ll x, y, a, b; C x >> y >> a >> b; ll ans = 0; while (true) { if (x > (x + b) / a || x >= y / a) { break; } x *= a; ans++; } ans += (y - 1 - x) / b; cout << ans << endl; }
#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("fast-math") #pragma GCC optimize("trapv") #pragma GCC target("sse4") #include<bits/stdc++.h> using namespace std; typedef pair<int, int> pi; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef long long int ll; typedef unsigned long long int ull; typedef priority_queue<ll> mxhpl; typedef priority_queue<int> mxhpi; typedef priority_queue<ll, vector<ll>, greater<ll>> mnhpl; typedef priority_queue<int, vector<int>, greater<int>> mnhpi; #define rep(i,start,end) for(ll i = start; i <= end; ++i) #define rrep(i,end,start) for(ll i = end; i >= start; --i) #define parr(a,n) rep(i,0,n-1){cout << a[i] << " ";}cout<<"\n" #define sarr(a,n) rep(i,0,n-1)cin >> a[i] #define pb push_back #define F first #define S second #define all(a) a.begin(),a.end() #define mset(a,x) memset(a, x, sizeof(a)) #define ps(x,y) fixed<<setprecision(y)<<x //cout << ps(ans, decimal places); #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) // DEBUGGING 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 // Fast IO void IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif } #define MOD 1000000007 //CODE /*******************************************************/ #define MX 100000 ll n; vvl gr(400001,vl()); bool vis[400001]; bool cycle = false; int dfs(int i, int p = -1) { if (vis[i]) { cycle = true; return 0; } vis[i] = 1; int ans = 1; ll cntP = 0; for (auto j: gr[i]) { if (j == p && !cntP) { cntP++; continue; } ans += dfs(j,i); } return ans; } void solve() { cin >> n; // vector<pll> vp(n); ll u,v; mset(vis,0); rep(i, 0, n - 1) { cin >> u >> v; gr[u].pb(v); gr[v].pb(u); } ll ans = 0; rep(i,0,400000) { if (gr[i].size() == 0 || vis[i]) { continue; } cycle = false; ans += dfs(i); // debug(ans, cycle); if (!cycle) ans--; } cout << ans << "\n"; } int main() { IO(); int t = 1; // cin >> t; while (t--) { solve(); } return 0; } /*******************************************************/
#include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long #define endl "\n" using namespace std; vector<pair<int,int>>adj[205]; //wt vertex; int32_t main() { fast int a, b, x, y; cin >> a >> b >> x >> y; // 100 200 // 1 101 for(int i=1;i<=100;i++){ adj[i].push_back({x,i+100}); adj[i+100].push_back({x,i}); if(i!=100){ adj[i+1].push_back({x,i+100}); adj[i+100].push_back({x,i+1}); } } for(int i=1;i<=99;i++){ adj[i].push_back({y,i+1}); adj[i+1].push_back({y,i}); adj[100+i].push_back({y,i+101}); adj[101+i].push_back({y,100+i}); } vector<int>d; d.assign(205,1e18); d[a] = 0; multiset<pair<int,int>>q; //dist vertex q.insert({0,a}); while(!q.empty()){ auto curr = *q.begin(); q.erase(q.begin()); int v = curr.second; for(auto neigh:adj[v]){ int to =neigh.second; int len = neigh.first; if(d[to]>d[v]+len){ q.erase({d[to],to}); d[to] = d[v]+len; q.insert({d[to],to}); } } } cout << d[b+100] << endl; // y = min(y,2*x); // if(a>b){ // // int ans = (a-b)*x+(a-b-1)*(x); // // ans = min(ans,(a-b)*x+y); // } // else if(a!=b){ // int ans = 2; // } // ans = (a-b-) // // if(b>a){ // // swap(a,b);swap(x,y); // // } // // if() // // if(2*x<y){ // int ans = abs(b - a) * (2 * x + x); // // if(a > b) // ans = min(ans, max(0LL, (abs(b - a) - 1)) * y + x); // cout << ans << endl; // // } // // else{ // // int ans = max(0LL,(abs(b-a)-1))*y+x; // // cout << ans << endl; // // } return 0; }
#include<bits/stdc++.h> using namespace std; #define f first #define s second #define MOD (int)1e9+7 #define INF (int)1e9 #define endll "\n" #define input(n, a) for (int i=0;i<n;++i) {cin>>a[i];} #define Laylo cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0); typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<double> vd; typedef vector<pii> vpii; typedef map<string, int> mpsi; typedef map<int, int> mpii; int main() { Laylo char x,y,z; cin >> x >>y >>z; if(x == y && x == z){cout <<"Won"<<endll;} else{cout <<"Lost"<<endll;} }
#include <bits/stdc++.h> using namespace std; int main(){ string C; cin >> C; if(C[0]==C[1] && C[1]==C[2]) cout << "Won" << endl; else cout << "Lost" << endl; return 0; }
#include <iostream> #include <vector> using namespace std; #define repeat(i, n) for (int i = 0; i < (int)(n); i++) using int64 = long long; namespace { void solve() { int n; int64 X; cin >> n >> X; vector<int64> as(n), ubs(n), xs(n); repeat (i, n) cin >> as[i]; repeat (i, n - 1) ubs[i] = as[i + 1] / as[i]; ubs[n - 1] = 1e18; for (int i = n - 1; i >= 0; i--) { xs[i] = X / as[i]; X %= as[i]; } vector<int64> dp(2, 0); dp[0] = 1; repeat (i, n) { vector<int64> next(2, 0); repeat (j, 2) repeat (nj, 2) { // x + z + j = y + as[i] * nj // z = as[i] * nj - x - j int64 z = ubs[i] * nj - xs[i] - j; if (0 <= z && z < ubs[i]) { next[nj] += dp[j]; } // y = x + j - as[i] * nj int64 y = xs[i] + j - ubs[i] * nj; if (0 < y && y < ubs[i]) { next[nj] += dp[j]; } } swap(dp, next); } cout << dp[0] << endl; } } int main() { solve(); return 0; }
#include <iostream> #include <string> using namespace std; #include <algorithm> #include <vector> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <iomanip> #include <set> #include <map> #include <cassert> #define MOD1 1000000007 #define MOD2 998244353 #define LIMIT1 200010 #define LIMIT2 500010 #define LIMIT3 1000010 #define INF ((1<<30)-1) #define LLINF (1LL<<60) #define EPS (1e-10) typedef long long ll; typedef long double ld; typedef const void cv; typedef pair<ll,ll> P; #define rep(i,n) for((i)=0;(i)<(n);(i)++) #define per(i,n) for((i)=(n)-1;(i)>=0;(i)--) #define ALL(a) (a).begin(),(a).end() #define ALL2(a, n) (a),(a)+(n) template<class T,class C> T max(T a,C b){ return std::max(a, (T)b); } template<class T,class C> T min(T a,C b){ return std::min(a, (T)b); } #define zt(a,b) (max((a),(b))-min((a),(b))) #define setpre(n) fixed << setprecision(n) 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;} int dx[8]={1,0,-1,0,1,-1,-1,1},dy[8]={0,1,0,-1,1,1,-1,-1}; ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1){ res *= a; if(mod>=1) res %= mod; } a *= a; if(mod>=1) a %= mod; n >>= 1; } return res; } void initialize(){ } ll dp[200010][16]={}; //i桁目まで決め,種類数がj int main(void){ initialize(); ll n,m,i,j,k,result=0; int a[LIMIT2]={0}; string s; cin >> s >> k; n = s.size(); set<char> sc; vector<ll> dp2(n+1,0); for(i=0;i<n;i++){ sc.insert(s[i]); dp2[i+1] = (ll)sc.size(); } sc.clear(); string ss="0123456789ABCDEF"; //dp[1][1] = (ll)(s[0]>='A'?s[0]-'A'+10:s[0]-'0')-1; for(i=0;i<n;i++){ ll tmp = (ll)(s[i]>='A'?s[i]-'A'+10:s[i]-'0'); //cout << tmp << ' '; for(j=(i==0?1:0);j<tmp;j++){ dp[i+1][dp2[i]+(sc.count(ss[j])?0:1)] += 1; } sc.insert(s[i]); } for(i=2;i<=n;i++){ dp[i][1] += 15; } for(i=0;i<n;i++){ for(j=1;j<=k;j++){ dp[i+1][j] += dp[i][j] * (j); dp[i+1][j] %= MOD1; dp[i+1][j+1] += dp[i][j] * (16-j); dp[i+1][j+1] %= MOD1; } } result = dp[n][k]+(dp2[n]==k?1:0); result %= MOD1; cout << result << endl; return 0; }
#define _USE_MATH_DEFIMES #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> const int MOD = 1'000'000'007; const int MOD2 = 998'244'353; const int INF = 1'000'000'000; //1e9 const int NIL = -1; const long long LINF = 1'000'000'000'000'000'000; // 1e18 const long double EPS = 1E-10; template<class T, class S> inline bool chmax(T &a, const S &b){ if(a < b){ a = b; return true; } return false; } template<class T, class S> inline bool chmin(T &a, const S &b){ if(b < a){ a = b; return true; } return false; } int main(){ int N; std::cin >> N; std::vector<int> A(N); for(auto &e: A) std::cin >> e; std::vector<std::vector<int>> dp(N, std::vector<int>(N)); for(int i{0}; i < N; ++i){ for(int j{i}; j < N; ++j){ int r{0}; for(int k{i}; k <= j; ++k){ r |= A[k]; } dp[i][j] = r; } } int ans{2*INF}; for(int i{0}, i_len{1 << (N -1)}; i < i_len; ++i){ int res{}, prv{}; for(int j{0}, j_len{N-1}; j < j_len; ++j){ if((i >> j) & 1){ res ^= dp[prv][j]; prv = j + 1; } } res ^= dp[prv][N-1]; chmin(ans, res); } std::cout << ans << std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> p2; #define sz(a) ll(a.size()) ll p[4000005],cnt[2005][2005],hell=pow(10,9)+7; ll mul(ll x,ll y){ x=(x*y)%hell; return x; } ll add(ll x,ll y){ x+=y; if(x>=hell) x-=hell; return x; } ll sub(ll x,ll y){ x-=y; if(x<0) x+=hell; return x; } void solve(){ ll n,m,c=0; cin>>n>>m; vector<string> v(n); for(ll i=0;i<n;i++){ cin>>v[i]; for(ll j=0;j<m;j++) if(v[i][j]=='.') c++; } for(ll i=0;i<n;i++) for(ll j=0;j<m;j++){ if(v[i][j]=='#') continue; ll k=j; while(k<m&&v[i][k]=='.') k++; ll x=k-j; while(j<k){ cnt[i][j]=x; j++; } j--; } for(ll j=0;j<m;j++) for(ll i=0;i<n;i++){ if(v[i][j]=='#') continue; ll k=i; while(k<n&&v[k][j]=='.') k++; ll x=k-i; while(i<k){ cnt[i][j]+=x-1; i++; } i--; } ll res=0; for(ll i=0;i<n;i++) for(ll j=0;j<m;j++) if(v[i][j]=='.'){ ll val=sub(p[cnt[i][j]],1); val=mul(val,p[c-cnt[i][j]]); res=add(res,val); } cout<<res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif clock_t z=clock(); ll qc=1; //cin>>qc; p[0]=1; for(ll i=1;i<4000005;i++) p[i]=mul(p[i-1],2); for(ll i=1;i<=qc;i++){ solve(); } debug("Total Time:%.4Lf\n",(ld)(clock()-z)/CLOCKS_PER_SEC); }
#include<bits/stdc++.h> using namespace std; struct fastio{fastio(){cin.tie(nullptr);ios_base::sync_with_stdio(false);std::cout<<std::fixed<<setprecision(10);}}oitsaf; #define rep(i,n) for(int i=0;i<int(n);++i) constexpr char newl = '\n'; template<class T>std::istream&operator>>(std::istream&is,std::vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;} template<class T>std::ostream&operator<<(std::ostream&os,std::vector<T>const&v){for(auto const& vi:v)os<<vi<<" ";return os;} #if _local struct debug_structure{template<class T>debug_structure&operator<<(const T&elm){std::cerr<<elm<<std::flush;return*this;}}dout; #else struct debug_structure{template<class T>debug_structure&operator<<(const T&elm){return*this;}}dout; #endif template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;} int main() { int n, m; cin >> n >> m; vector<int> a(n); cin >> a; vector<int> S(2e6); set<int> st; rep(i, m) { S[a[i]]++; } rep(i, 2e6) if (S[i] == 0) st.emplace(i); int mex = 0; while (S[mex]) mex++; int ans = mex; for (int i = m; i < n; ++i) { dout << mex << newl; S[a[i]]++; S[a[i - m]]--; if (S[a[i]] == 1) { st.erase(a[i]); } if (S[a[i - m]] == 0) { st.emplace(a[i - m]); if (chmin(mex, a[i - m])) { chmin(ans, mex); } } dout << mex << newl; if (S[a[i]] == 1 and a[i] == mex) { auto itr = st.upper_bound(mex); mex = *itr; } } cout << (ans) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n, s, k; ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extgcd(b, a % b, x, y); ll t = x; x = y; y = t - (a / b) * y; return d; } void solve() { cin >> n >> s >> k; ll x, y, d; if ((d = extgcd(k, n, x, y)) != 1ll) { if (s % d) { puts("-1"); return ; } else { k /= d, n /= d, s /= d; } } x *= -s; y *= -s; cout << (x % n + n) % n << endl; } int main() { int T; cin >> T; while (T--) solve(); return 0; }
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define MOD 1000000007 #define pb push_back #define mp make_pair #define sortv(v) sort(v.begin(),v.end()) #define sorta(A,N) sort(A,A+N) #define rep(i,a,N) for(ll i=a;i<N;i++) #define test(T) ll T; cin>>T; while(T--) #define f first #define s second #define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; ll gcd(ll a,ll b){ if(!a) return b; else return gcd(b%a,a); } ll power(ll a,ll b,ll mod=MOD){ a%=mod; ll res=1; while(b){ if(b&1) res=res*a%mod; a=a*a%mod; b>>=1; } return res; }/* ll N=1e6+1; vector<ll>prime(N); void sieve(){ rep(i,0,N) prime[i]=i; for(ll i=4;i<N;i+=2) prime[i]=2; for(ll i=3;i*i<N;i+=2){ if(prime[i]==i) for(ll j=i*i;j<N;j+=i) if(prime[j]==j) prime[j]=i; } } vector<ll> factor(ll N){ vector<ll> fac; while(N!=1){ fac.pb(prime[N]); N/=prime[N]; } return fac; }*/ void solve(){ //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ll N; cin>>N; string s1,s2,s3; cin>>s1>>s2>>s3; cout<<string(N,'0')<<string(N,'1')<<"0\n"; } int main(){ speed; test(T){ solve(); } }
#include<bits/stdc++.h> using namespace std; using ll=long long; constexpr ll mod=1e9+7; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int t; cin>>t; for (int i = 0; i < t; ++i) { int n; cin>>n; string s; bool one = false, zero = false; for (int i = 0; i < 3; ++i) { cin>>s; if (s.front() == '0' && s.back() == '0') zero = true; else if (s.front() == '1' && s.back() == '1') one = true; } if (zero && one) { cout << 1; for (int i = 0; i < n; ++i) { cout << 0; } for (int i = 0; i < n; ++i) { cout << 1; } } else if (zero) { for (int j = 0; j < n; ++j) { cout << 1; } cout << 0; for (int j = 0; j < n; ++j) { cout << 1; } } else { for (int j = 0; j < n; ++j) { cout << 0; } cout << 1; for (int j = 0; j < n; ++j) { cout << 0; } } cout << "\n"; } }
#include <bits/stdc++.h> #define rep(i,a,b) for(int i=int(a);i<int(b);++i) #define SIZE 200005 #define INF 1000000005LL #define MOD 1000000007 using namespace std; typedef long long int ll; typedef pair <int,int> P; int main(){ int n; cin >> n; vector<ll> data(n); rep(i,0,n){ cin >> data[i]; } ll m=0; ll y=0; ll t=0; rep(i,0,n){ m += abs(data[i]); y += pow(data[i],2); if(t<abs(data[i])){ t = abs(data[i]); } } double ans = sqrt(y); cout << std::setprecision(15) << m <<endl; cout << std::setprecision(15) << ans <<endl; cout << std::setprecision(15) << t << endl; // cout << m << endl; // cout<< y <<endl; // cout<<t << endl; return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #define rep(a,b,c) for(int a=b;a<=c;a++) const int p=3; const int N=4e5+5; int fac[4],inv[4]; inline int C(int n,int m) { return (n>=m)?(1ll*fac[n]*inv[m]*inv[n-m]%p):0; } inline int lucas(int n,int m) { if(!m) return 1; return 1ll*C(n%p,m%p)*lucas(n/p,m/p)%p; } int n,ans; char s[N]; inline int get(char S) { if(S=='B') return 0; else if(S=='W') return 1; else return 2; } int main() { fac[0]=1; inv[0]=1; fac[1]=1; inv[1]=1; fac[2]=2; inv[2]=2; scanf("%d",&n); scanf("%s",s+1); rep(i,1,n) (ans+=lucas(n-1,i-1)*get(s[i]))%=p; if(!(n&1)) ans=(3-ans)%p; if(ans==0) putchar('B'); if(ans==1) putchar('W'); if(ans==2) putchar('R'); }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, k; cin >> n >> k; int a = n; int g1 = 0; int g2 = 0; for (int i = 0; i < k; i++) { // 各桁の数値を取り出す int num = a; vector<int> v1, v2; while (true) { v1.push_back(num % 10); if (num < 10) { break; } num /= 10; } v2 = v1; // g1をつくる g1 = 0; sort(v1.begin(), v1.end(), greater<int>()); for (size_t j = 0; j < v1.size(); j++) { g1 *= 10; g1 += v1[j]; } // g2をつくる g2 = 0; sort(v2.begin(), v2.end()); for (size_t j = 0; j < v2.size(); j++) { g2 *= 10; g2 += v2[j]; } a = g1 - g2; } // cout << g1 << endl; // cout << g2 << endl; cout << a << 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 ull = unsigned long long; using P = pair<int, int>; const int INF = 1e9; const int MOD = 998244353; // const int dx[] = {-1,0,1,0}; const int dy[] = {0,-1,0,1}; // const int dx[] = {-1,-1,-1, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1,-1, 1,-1, 0, 1}; #define PI 3.14159265358979323846264338327950L //cout << fixed << setprecision(10) << Min << endl << Max << endl; int main() { int N; cin >> N; ll ans = 0; rep(i, N) { ll A, B; cin >> A >> B; ans += (B - A + 1)*(A+B)/2; } cout << ans << endl; }
#include <bits/stdc++.h> #define F first #define S second #define prev azaza #define MP make_pair #define PB push_back using namespace std; typedef long long ll; typedef long double ld; const int max_n = 200111, inf = 1000111222; vector<int> a, b; int main() { //freopen("input.txt", "r", stdin); int n; cin >> n; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); a.PB(x); } for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); b.PB(x); } reverse(b.begin(), b.end()); multiset<int> ms; ll sum = 0; for (int i = 0; i < n; ++i) { ms.insert(a[i]); ms.insert(b[i]); auto it = (--ms.end()); sum += *it; ms.erase(it); } cout << sum << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll=long long; const ll inf=998244353; int main(){ ll n;cin>>n; vector<ll>a(n); for(ll i=0;i<n;i++)cin>>a.at(i); sort(a.begin(),a.end()); ll ans=0; for(ll i=0;i<n;i++){ans+=a.at(i)%inf*a.at(i)%inf;ans%=inf;} ll tmp=a.at(n-1); for(ll i=n-2;i>=0;i--){ ans+=a.at(i)*tmp%inf; ans%=inf; tmp*=2; tmp%=inf; tmp+=a.at(i); tmp%=inf; } cout<<ans<<endl; }
#include <stdio.h> #include <iostream> #include <stdlib.h> #include <vector> #include <cassert> #include <algorithm> #include <functional> using namespace std; unsigned long long L; int main(){ cin >> L; long long result = 1LL; for(int i=1; i<12; i++){ result = result*(L-i)/i; } cout << result; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; // s.find_by_order(index); // s.order_of_key(value); #define int long long #define all(a) a.begin(), a.end() #define dl '\n' #define why (int)1000000007 //#define why (int)998244353 typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; int inf = 1e18; double pi = 3.141592653589; int mod = why; int fast_power(int base, int power) { int result = 1; while (power) { if (power % 2) result = (result * base) % mod; base = (base * base) % mod; power /= 2; } return result; } int inverse(int base) { return fast_power(base, mod - 2); } int test = 1; int printNcR(int n, int r) { long long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; long long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } const pair<int, int> dir[] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}; const int N = 2e5 + 5; vector<int> adj[N]; vector<int> vis(N); void solve() { int n; cin >> n; cout << printNcR(n - 1, 11); } signed main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { // cout << "Case #" << test++ << ": "; solve(); } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll t = 1,n,m,k,q,l,r,a[809][809],b[809][809],c[809][809],u,v,x,y,z,mod = 1e9 + 7; string s; struct st{ll x,y; /*bool operator < (const st &a) const{return x < a.x;};*/}; bool cmp(const st &a,const st &b){return a.x < b.x;} inline ll read(){ ll s = 0,w = 1; char ch = getchar(); while (ch > '9' || ch < '0'){ if (ch == '-') w = -1; ch = getchar();} while (ch <= '9' && ch >= '0') s = (s << 1) + (s << 3) + (ch ^ 48),ch = getchar(); return s * w; } ll qp(ll x,ll y){ ll a = 1,b = x; while (y){ if (y & 1) a = a * b % mod; b = b * b % mod,y >>= 1; } return a; } bool judge(ll x){ for (ll i = 1;i <= n;i += 1) for (ll j = 1;j <= n;j += 1) c[i][j] = a[i][j] <= x; for (ll i = 1;i <= n;i += 1) for (ll j = 1;j <= n;j += 1) b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + c[i][j]; for (ll i = 1;i <= n - k + 1;i += 1) for (ll j = 1;j <= n - k + 1;j += 1) if (b[i + k - 1][j + k - 1] - b[i - 1][j + k - 1] - b[i + k - 1][j - 1] + b[i - 1][j - 1] >= (k * k + 1) / 2){ return 1; } return 0; } int main(){//t = read(); while (t --){ n = read(),k = read(); for (ll i = 1;i <= n;i += 1) for (ll j = 1;j <= n;j += 1) a[i][j] = read(); ll l = 0,r = 1e9,ans = 1e9; while (l <= r){ ll mid = (l + r) >> 1; if (judge(mid)) r = mid - 1,ans = mid; else l = mid + 1; } cout<<ans; } return 0; }
// Author: old_school // Created: 20.02.2021 17:30:29 #include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") using namespace std; #define lld long long int #define ld long double #define pb push_back #define ppb pop_back #define mp make_pair #define F first #define S second #define nl '\n' #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define sz(c) (c).size() #define tr(x,a) for(auto &a : x) #define psnt(x,a) (x).find(a)!=(x).end() #define vpsnt(x,a) find(all(x),a)!=(x).end() #define MOD 1000000007 #define tod 100005 #define pi 3.1415926536 #define itr(i,a,b) for(lld i=a;i<=b;i++) #define itrn(i,a,b) for(lld i=a;i>=b;i--) #define iot(n) for(lld i=0;i<n;i++) #define pls(n,arr) lld arr[n]; iot(n) cin>>arr[i]; #define bye fflush(stdout) typedef pair<lld,lld> pii; typedef pair<string,lld> psi; template <typename T> bool mycomp(T x,T y){ return (x==y); //give your condition here } bool paircomp(const pair<lld,lld> &x,const pair<lld,lld> &y){ return x.second<y.second; } vector<pair<lld,pair<lld,lld> > > adj[tod]; void dijkstra(lld V,lld source,lld dist[]){ priority_queue<pair<lld,lld> ,vector<pair<lld,lld> >,greater<pair<lld,lld> > > pq; //minheap for(lld i=0;i<=V;i++) dist[i]=-1; dist[source]=0; pq.push(make_pair(dist[source],source)); while(!pq.empty()){ lld u=pq.top().second; pq.pop(); for(auto &p:adj[u]){ lld v=p.first,weight=p.second.first,lx=p.second.second; lld z=dist[u]/lx; lld ex=0; if(dist[u]%lx){ z++; } z*=lx; ex=z-dist[u]; if (dist[v]==-1) { dist[v] = dist[u] + ex+weight; pq.push(make_pair(dist[v], v)); } else if(dist[v]>dist[u]+ex+weight){ dist[v]=dist[u]+ex+weight; pq.push(make_pair(dist[v], v)); } } } } void solve(){ lld n,m,x,y; cin>>n>>m>>x>>y; iot(m){ lld a,b,t,k; cin>>a>>b>>t>>k; adj[a].pb({b,{t,k}}); adj[b].pb({a,{t,k}}); } lld dist[n+1]; dijkstra(n,x,dist); cout<<dist[y]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); lld t=1;// cin>>t; while(t--){ solve(); cout<<endl; } }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author NikuKyabe */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i) #define REV(i, a, b) for (int i = (b) - 1; i >= (a); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001l #define MOD 1000000007 #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; template<class T> using M =vector<vector<T>>; class CPOW { public: void solve(std::istream& cin, std::ostream& cout) { int a,b,c; cin >> a >> b >> c; if(c % 2 == 0){ a = abs(a); b = abs(b); } if(a > b){ cout << ">" << endl; } else if(a < b){ cout << "<" << endl; } else{ cout << "=" << endl; } } }; signed main() { CPOW solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int A, B, C; while(scanf("%d%d%d", &A, &B, &C) > 0){ if(C%2 == 0){ A = abs(A); B = abs(B); } if(A > B) printf(">"); if(A < B) printf("<"); if(A == B) printf("="); printf("\n"); } }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; cout << N * (N + 1) * 50 * K + K * (K + 1) / 2 * N << endl; }
#include<bits/stdc++.h> using namespace std; int main() { int n,k; cin>>n>>k; int sum=0; int i,j; for(i=1;i<=n;i++) { for(j=1;j<=k;j++) { sum=sum+(100*i)+j; } } cout<<sum<<endl; return 0; }
#include<bits/stdc++.h> #define forn(i, n) for (int i = 0; i < n; i++) #define form(i, n) for (int i = n-1; i >=0; i--) #define forx(i,x,n) for (int i = x; i < n; i++) #define all(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define pb push_back #define ll long long #define srt(a) sort(a.begin(), a.end()); #define pl pair<long long,long long> #define pi pair<int, int> #define fi first #define se second #define ve vector using namespace std; const int inf = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, k, l; cin >> n >> m; ve<int> c(n); forn(i, n) cin >> c[i]; multiset<int> a; set<int> b; forn(i, m+1) { b.insert(i); } forn(i, m) { a.insert(c[i]); b.erase(c[i]); } int R = *b.begin(); forx(i, m, n) { a.insert(c[i]); a.erase(a.find(c[i - m])); if (a.count(c[i-m])==0) b.insert(c[i - m]); b.erase(c[i]); R = min(R, *b.begin()); } cout << R; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i) #define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i) #define revrep(i,n) for(ll i=(ll)(n)-1; i>=0; --i) inline constexpr ll Inf = (1ULL << 60) -123456789; #define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10); #define newl '\n' #define YN(e) ((e)?"Yes":"No") #define all(a) begin(a),end(a) #define rall(a) rbegin(a),rend(a) #define delif(c,pred) (c).erase(remove_if(all(c),(pred)), end(c)) template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;} template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;} inline constexpr int Mod = 1000000007; //inline constexpr int Mod = 998244353; #define dbg(a) cerr << #a << ": " << a << endl; #define dbgs(s) cerr << #s << endl; #define dbg1(a,n) cerr<<#a<<": "; rep(i,n) cerr<<a[i]<<" "; cerr<<endl; #define dbg2(m,h,w) cerr<<#m<<":"<<endl; rep(i,h){ rep(j,w)cerr<<m[i][j]<<" "; cerr<<endl; } template <class T, class U> ostream& operator << (ostream& os, pair<T,U> v) {os<<v.first<<","<<v.second;return os;} template <class T, size_t N> ostream& operator << (ostream& os, array<T,N> v) {rep(i,N)os<<v[i]<<(i+1<(ll)N?" ":"");return os;} int rng(int n) { return rand()/(RAND_MAX+1.0)*n; } int main() { fastio; ll ans{}; ll a,b,c; cin >> a >> b >> c; ans = 21-a-b-c; cout << ans << endl; }
#include<bits/stdc++.h> #define rep(i , a , b) for(int i = a ; i <= b ; i ++) #define fi first #define se second using namespace std; const int N = 1e3 + 10; int mp[N][N] , vis[N][N]; vector<int>G[N][30]; signed main() { int n , m ; cin >> n >> m ; rep(i , 1 , m) { int u , v; char ch; cin >> u >> v >> ch; G[u][ch - 'a'].push_back(v); G[v][ch - 'a'].push_back(u); mp[u][v] = mp[v][u] = 1; } queue<pair<pair<int , int> , int>>que; int res = 1e9; que.push(make_pair(make_pair(1 , n) , 0)); vis[1][n] = 1; while(!que.empty()) { pair<pair<int , int> , int>q = que.front(); que.pop(); int x = q.fi.fi , y = q.fi.se , z = q.se; if(z > res) break ; if(x == y) res = min(res , z); if(mp[x][y]) res = min(res , z + 1); rep(k , 0 , 25) { for(auto i : G[x][k]) { for(auto j : G[y][k]) { if(vis[i][j]) continue ; vis[i][j] = 1; que.push(make_pair(make_pair(i , j) , z + 2)); } } } } if(res == 1e9) res = -1; cout << res << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; const int N=1e6+1e5,M=1e3+10; int n,m; vector<int>e[N]; vector<int>f[M][26]; int a,b; char c[5]; int d[N]; int cal(int x,int y){ return x*1001+y; } queue<int>q; int main(){ scanf("%d%d",&n,&m); memset(d,-1,sizeof d); for(int i=1;i<=n;++i){ d[cal(i,i)]=0; q.push(cal(i,i)); } for(int i=1;i<=m;++i){ scanf("%d%d%s",&a,&b,c); if(a!=b){ d[cal(a,b)]=1; q.push(cal(a,b)); d[cal(b,a)]=1; q.push(cal(b,a)); } int v=c[0]-'a'; f[a][v].push_back(b); f[b][v].push_back(a); } for(int i=1;i<=n;++i){ for(int j=0;j<26;++j){ for(auto &x:f[i][j]){ for(int k=1;k<=n;++k){ for(auto &y:f[k][j]){//i-x k-y e[cal(i,k)].push_back(cal(x,y)); e[cal(i,k)].push_back(cal(y,x)); e[cal(i,y)].push_back(cal(k,x)); e[cal(i,y)].push_back(cal(x,k)); } } } } } while(!q.empty()){ int x=q.front();q.pop(); //printf("(%d,%d):%d\n",x/1001,x%1001,d[x]); for(auto &y:e[x]){ if(d[y]==-1){ d[y]=d[x]+2; q.push(y); } } } printf("%d\n",d[cal(1,n)]); return 0; }
#include <bits/stdc++.h> using namespace std; long long INF = 1LL<<60; using ll = long long; using vll = vector<ll>; using mll = map<ll, ll>; #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() 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; } //* calculate x^n mod something in O(log n) -- recursive version ll my_pow(ll x, ll n){ if(n == 0) return 1; ll res = my_pow(x*x , n/2); if(n & 1) res = res * x ; return res; } void solve(long long N){ rep(A, 1, 38){ rep(B, 1, 26){ ll tmp = my_pow(3, A) + my_pow(5, B); if(tmp == N){ cout << A << " " << B << endl; return; } } } cout << -1 << endl; } int main(){ long long N; scanf("%lld",&N); solve(N); return 0; }
#include <iostream> #include <cmath> #include <stdio.h> #include <string.h> #include <bits/stdc++.h> #include <vector> #include <array> #include <tuple> using namespace std; int main(){ long long N; cin >> N; long long p, S; S=N; p=1; for (int i=0; i<=60; i++){ S=min(N/p+i+N%p, S); p=2*p; } cout << S << endl; }
#include<bits/stdc++.h> using namespace::std; long mat[10000][10000]; class Advertisement{ public: void set_val(int x,int y,int inr); long get_height(); long get_width(); long obtain_patch(long mode); long a; long b; long c; long d; long s; long r; long mode1; long mode2; long mode3; long mode4; }; void Advertisement::set_val(int x,int y,int inr){ a = x; b = y; c = x+1; d = y+1; r = inr; mode1 = 1; mode2 = 1; mode3 = 1; mode4 = 1; mat[a][b] = 1; s = 1; } long Advertisement::obtain_patch(long mode){ //mode:1->left 2->right 3->upper 4->lower if(mode == 1){ if(a-1<0 || mode1 != 1){ return -1; }else{ for(int i=b;i<d;i++){ if(mat[a-1][i] != 0){ mode1 = -1; return -1; } } for(int i=b;i<d;i++){ mat[a-1][i] = 1; } s += get_height(); a-=1; } }else if(mode == 2){ if(c>9999 || mode2 != 1){ return -1; }else{ for(int i=b;i<d;i++){ if(mat[c][i] != 0){ mode2 = -1; return -1; } } for(int i=b;i<d;i++){ mat[c][i] = 1; } s += get_height(); c+=1; } }else if(mode == 3){ if(b-1<0 || mode3 != 1){ return -1; }else{ for(int i=a;i<c;i++){ if(mat[i][b-1] != 0){ mode3 = -1; return -1; } } for(int i=a;i<c;i++){ mat[i][b-1] = 1; } s += get_width(); b-=1; } }else{ if(d>9999 || mode4 != 1){ return -1; }else{ for(int i=a;i<c;i++){ if(mat[i][d] != 0){ mode4 = -1; return -1; } } for(int i=a;i<c;i++){ mat[i][d] = 1; } s += get_width(); d+=1; } } return 0; } long Advertisement::get_width(){ return d-b; } long Advertisement::get_height(){ return c-a; } int main(){ long n; cin >> n; long x,y,r; Advertisement *ad = new Advertisement[n]; for(int i=0;i<n;i++){ cin >> x >> y >> r; ad[i].set_val(x,y,r); } clock_t start,time; start = clock(); bool notstop = true; while(notstop){ for(int i=0;i<n && notstop;i++){ for(int j=1;j<5 && notstop;j++){ time = clock(); if((double)(time-start)/CLOCKS_PER_SEC > 4.8){ notstop = false; } ad[i].obtain_patch(j); } } } for(int i=0;i<n;i++){ cout << ad[i].a << ' ' << ad[i].b << ' ' << ad[i].c << ' ' << ad[i].d << ' ' << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define mp make_pair #define fr first #define sc second int n; int x[202],y[202],r[202]; struct A{ int a[202],b[202],c[202],d[202]; void out(){ for(int i=0;i<n;i++){ printf("%d %d %d %d\n",a[i],b[i],c[i],d[i]); } } }; int main(){ scanf("%d",&n); for(int i=0;i<n;i++)scanf("%d%d%d",&x[i],&y[i],&r[i]); A ans; for(int i=0;i<n;i++){ ans.a[i]=x[i]; ans.c[i]=x[i]+1; ans.b[i]=y[i]; ans.d[i]=y[i]+1; } ans.out(); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef unsigned long ul; int main() { int n; //木の頂点の数 scanf("%d", &n); vector<int> p(n, -1), c(n, 1), d(n, 0); // p[i]: iの親 // c[i]: iを根とする部分木の濃度 // d[i]: iに進んだ時に獲得できる個数 vector<vector<int>> ch(n); // ch[i]: iの子 rep(i, n - 1) { scanf("%d", &p[i + 1]); p[i + 1]--; ch[p[i + 1]].push_back(i + 1); } rep(i, n - 1) { c[p[n - 1 - i]] += c[n - 1 - i]; } for (int i = n - 1; i >= 0; i--) { int epp = 0; //偶数個の木に進んで得するとき取れる数 int epm = 0; //偶数個の木に進んで得するとき取られる数 int emp = 0; //偶数個の木に進んで損するとき取れる数 int emm = 0; //偶数個の木に進んで損するとき取られる数 vector<pair<int, int>> od(0); //奇数個の木に進んだとき(取られる-取れる, 取れる); rep(j, ch[i].size()) { int u = ch[i][j]; if (c[u] % 2) { od.push_back(make_pair(c[u] - d[u] * 2, d[u])); } else { if (d[u] > c[u] - d[u]) { epp += d[u]; epm += c[u] - d[u]; } else { emp += d[u]; emm += c[u] - d[u]; } } } if (od.size() == 0) { d[i] += epp + emp; } else { d[i] += epp; if (od.size() % 2) { d[i] += emm; } else { d[i] += emp; } sort(od.begin(), od.end()); rep(j, od.size()) { if (j % 2) { d[i] += od[j].first + od[j].second; } else { d[i] += od[j].second; } } } } printf("%d\n", n - d[0]); //rep(i, n) cout << d[i] << " "; cout << "\n"; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; void Main() { ll n; R n; ll a[n],b[n]; rep(i,n) R a[i]; rep(i,n) R b[i]; ll Ma=0,ans=0; rep(i,n) { Ma=max(Ma,a[i]); ans=max(ans,Ma*b[i]); pr(ans); } } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
#include<iostream> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<algorithm> #include<utility> using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; signed main(){ cout<<fixed<<setprecision(10); int N; int a = 0, b = 0; vector<int> T; vector<int> dp; cin>>N; T.resize(N); dp.resize(110000, INF); dp[0] = 0; for(int i = 0; i < N; i++){ cin>>T[i]; } for(int i = 0; i < N; i++){ vector<int> ndp(110000, INF); for(int j = (int)dp.size()-1; j >= 0; j--){ if(j+T[i] < dp.size()) { ndp[j+T[i]] = min(ndp[j+T[i]], dp[j]); } ndp[j] = min(dp[j] + T[i], ndp[j]); } swap(ndp, dp); // cout<<endl; // cout<<"====== "<<i<<" T = "<<T[i]<<endl; // for(int i = 0; i <= 20; i++){ // cout<<i<<" <><> "<<dp[i]<<" "<<endl; // } // cout<<endl; } int ans = INF; for(int i = 0; i < dp.size(); i++){ ans = min(ans, max(dp[i], i)); } cout<<ans<<endl; // sort(T.begin(), T.end(), greater<int>()); // for(int i = 0; i < N; i++){ // if(a < b) { // a += T[i]; // } else { // b += T[i]; // } // } // cout<<max(a, b)<<endl;; return 0; }
#include<bits/stdc++.h> #include<cmath> #define pb push_back #define ld long double #define mp make_pair #define vl vector<ll> #define vd vector<double> #define vld vector<long double> #define ll long long int #define pl pair<ll, ll> #define all(a) a.begin(), a.end() #define forr(i, n) for(ll i=0; i<n; i++) #define forr1(i, n) for(ll i=1; i<=n; i++) using namespace std; const ld PI =3.1415926535897923846; const ll MOD = 1000000007; const ll N=998244353; ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;} ll modinverse(ll a){return power(a, MOD-2);} ll n, m; vl a, b; int dp[1001][1001]; ll ans(ll ind1, ll ind2) { if(ind1==n||ind2==m) return max(n-ind1, m-ind2); if(dp[ind1][ind2]!=-1) return dp[ind1][ind2]; if(a[ind1]==b[ind2]) { // either take these return dp[ind1][ind2]=min({ ans(ind1+1, ind2+1), 1+ans(ind1+1, ind2), 1+ans(ind1, ind2+1), }); } else { return dp[ind1][ind2]=min({ 1+ans(ind1+1, ind2+1), 1+ans(ind1+1, ind2), 1+ans(ind1, ind2+1), }); } } void solve() { cin>>n>>m; a.resize(n); b.resize(m); forr(i, n) { cin>>a[i]; } forr(i, m) { cin>>b[i]; } forr(i, n+1) forr(j, m+1) dp[i][j]=-1; cout<<ans(0, 0)<<endl; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll test=1; //cin>>test; while(test--) { solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include<bits/stdc++.h> using namespace std; int main() { long long int a,b,res,ans; cin >>a>>b; ans=(2*a)+100; if(b<=ans) { res=ans-b; } cout<<res<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve(){ int a,b; cin>>a>>b; cout<<2*a+100-b; } int main(){ int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define double long double #define endl '\n' #define pb push_back #define pob pop_back #define pii pair<int,int> // int mod = 3; #define mod 1000000007 // #define mod 1000000009 // #define mod 163577857 // #define mod 998244353 #define rep(i,n) for (int i = 0; i < n; i++) #define repp(i,a,b) for(int i = a ; i<b ; i++) #define reppr(i,a,b) for(int i = a-1 ; i>=b ; i--) #define repr(i,n) for (int i = n - 1; i >= 0; i--) #define ff first #define ss second #define inf 9223372036854775807 #define infn -9223372036854775807 #define pi 3.14159265358979323846 #define eps 0.0000000001 #define setprec(x) cout << fixed << setprecision(x); #define REVERSE(a) reverse(all(a)); #define SORT(a) sort(all(a)); #define all(n) n.begin(),n.end() #define setbits(x) __builtin_popcountll(x) //GCD and LCM int gcd (int a, int b) { return b ? gcd (b, a % b) : a; } int lcm (int a, int b) { return a / gcd(a, b) * b; } //Modular Exponentiation int powmod(int x,int y) { if (y == 0) return 1; int p = powmod(x, y/2) % mod; p = (p * p) % mod; return (y%2 == 0)? p : (x * p) % mod; } //Modular Inverse int inverse(int a) { return powmod(a,mod-2); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////NO TEXT ABOVE IT////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool codejam = 0, testcases = 0; int parent[400001]; int rank[400001]; int find_set(int v) { if (v == parent[v]) return v; return find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) parent[b] = a; } void solve() { int n , a , b; cin >> n; map<int,vector<int>> m; repp(i,1,400001) parent[i] = i; rep(i,n) { cin >> a >> b; if(a>b) swap(a,b); m[a].pb(b); } set<int> ans; for(auto i : m) { for(auto j : i.ss) { if(ans.find(find_set(j))==ans.end()) ans.insert(find_set(j)) , union_sets(i.ff,j); else ans.insert(find_set(i.ff)); } } cout << ans.size() << endl; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////NO TEXT BELOW IT////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// signed main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int t = 1; if(testcases) cin >> t; rep(i,t) { if(codejam) cout << "Case #" << i+ 1<< ": "; solve(); } }
#include <bits/stdc++.h> using namespace std; vector<long long> base_n(long long n, long long M){ vector<long long> basen; while(M>0){ basen.push_back(M%n); M/=n; } return basen; } int check(long long n, long long M, string X){ vector<long long> basen = base_n(n,M); int ketasuu = basen.size(); if(ketasuu>X.size()){ return 1; }else if(ketasuu<X.size()){ return 0; }else if(ketasuu==X.size()){ for(int j=0;j<X.size();j++){ if(basen[ketasuu-j-1]>X[j]-'0'){ return 1; }else if(basen[ketasuu-j-1]<X[j]-'0'){ return 0; } } return 2; } return -1; } int main(){ string X; long long M; cin >> X >> M; int digit_max = 0; for(int i=0;i<X.size();i++){ digit_max = max(digit_max, X[i] - '0'); } long long ans = 0; long long start = digit_max; long long right = M+1; long long left = start; long long mid = (left+right)/2; if(X.size()==1){ if((X[0] - '0')<=M){ ans=1; }else{ ans=0; } } else{ while(left+1<right){ mid = (left+right)/2; //cout << mid << endl; if(check(mid,M,X)==1){ left = mid; }else if(check(mid,M,X)==0){ right = mid; }else if(check(mid,M,X)==2){ left = mid; } } ans = left-start; } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define MOD 1000000007 ll getXOR(ll BITree[], ll index) { ll ans = 0; index += 1; while (index > 0) { ans ^= BITree[index]; index -= index & (-index); } return ans; } void updateBIT(ll BITree[], ll n, ll index, ll val) { index = index + 1; while (index <= n) { BITree[index] ^= val; index += index & (-index); } } ll* constructBITree(ll arr[], ll n) { ll* BITree = new ll[n + 1]; for (ll i = 1; i <= n; i++) BITree[i] = 0; for (ll i = 0; i < n; i++) updateBIT(BITree, n, i, arr[i]); return BITree; } ll rangeXor(ll BITree[], ll l, ll r) { return getXOR(BITree, r) ^ getXOR(BITree, l - 1); } int main() { int n, q; cin >> n >> q; ll a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } ll* BITree = constructBITree(a, n); while (q--) { ll t, x, y; cin >> t >> x >> y; if (t == 1) { x--; a[x] ^= y; updateBIT(BITree, n, x, y); } else { x--, y--; cout << rangeXor(BITree, x, y) << "\n"; } } return 0; }
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; typedef unsigned long long int ull; typedef long int li; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define test ll t; cin >> t; while(t--) const long long int cons = 300005; const long long int N = 100005; const long long int MOD = 1000000007; // const long long int MOD = 998244353; const long long int const_INT_MAX = 1000000000000000000; const long long int const_INT_MIN = -1 * 1000000000000000000; using namespace std; // to sort pair vector in reverse order bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){ return (a.first > b.first); } // to sort pair vector according to the second elements bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){ return (a.second < b.second); } // to sort pair vector according to the second elements in reverse order bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){ return (a.second > b.second); } ll lcm(ll x, ll y){ return (ll)((x * y) / __gcd(x, y)); } map<ll, ll> primeFact(ll n){ map<ll, ll> mp; while(n % 2 == 0){ mp[2]++; n = n / 2; } for(ll i=3;i<=sqrt(n);i+=2){ while(n % i == 0){ mp[i]++; n = n / i; } } if(n > 2){ mp[n]++; } return mp; } ll mod_expo(ll x, ll y, ll p){ // reference from GFG ll res = 1; if((x + p) % p == 0){ res = 0; } else { x = (x + p) % p; while (y > 0){ if (y & 1){ res = (res*x + p) % p; } y = y>>1; x = (x*x + p) % p; } } return res; } struct Segtree { ll siz; vector<ll> ar; ll oper(ll a, ll b){ return (ll)(a ^ b); } void init(ll n){ siz = 1; while(siz < n){ siz = siz * 2; } ar.assign(2 * siz, (ll)(0)); } ll query(ll v, ll l, ll r, ll l_bound, ll r_bound){ if(l >= r_bound || r <= l_bound){return 0;} if(l >= l_bound && r <= r_bound){return ar[v];} ll mid = l + (r - l) / 2; return oper(query(v*2+1, l, mid, l_bound, r_bound), query(v*2+2, mid, r, l_bound, r_bound)); } void update(ll v, ll l, ll r, ll pos, ll val){ if(!(r - (l + 1))){ ar[v] = val; } else { ll mid = l + (r - l) / 2; if(pos < mid){ update(v*2+1, l, mid, pos, val); } else { update(v*2+2, mid, r, pos, val); } ar[v] = oper(ar[v*2+1], ar[v*2+2]); } } }; int main(){ #ifdef n00bie_1004_local freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio /* ll p = MOD; InverseofNumber(p); InverseofFactorial(p); factorial(p); */ ll n, q; cin >> n >> q; ll a[n]; Segtree seg; seg.init(n); for(ll i=0;i<n;i++){ cin >> a[i]; seg.update(0, 0, n, i, a[i]); } for(ll i=0;i<q;i++){ ll typ; cin >> typ; if(!(typ - 1)){ ll x, y; cin >> x >> y; x--; a[x] ^= y; seg.update(0, 0, n, x, a[x]); } else if(!(typ - 2)){ ll l, r; cin >> l >> r; l--; ll ans = seg.query(0, 0, n, l, r); cout << ans << "\n"; } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; #define REP(i, n, m) for(ll i=n; i<(ll)m; ++i) #define IREP(i, n, m) for(ll i=n-1; i>=m; --i) #define rep(i, n) REP(i, 0, n) #define irep(i, n) IREP(i, n, 0) #define all(v) v.begin(), v.end() #define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl; #define vvprint(vv) for(auto v:vv){vprint(v)}; class FenwickTree{ public: ll size; vll v; FenwickTree(ll n){ this->size = n; ll m = 1; while(n>m) m *= 2; this->v = vll(m, 0); } ll _get(ll x){ ll lsb = x & -x; ll r = x - lsb; if(r>0) return this->v[x-1] + _get(r); return this->v[x-1]; } ll get(ll left, ll right){ if(left<=0) return _get(right+1); return _get(right+1) - _get(left); } void update(ll x, ll e){ this->v[x] += e; if(x==this->v.size()-1) return; ll lsb = (x+1) & -(x+1); update(x+lsb, e); } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20); ll N, M, Q; cin >> N >> M >> Q; vll T(Q), X(Q), Y(Q); rep(i, Q) cin >> T[i] >> X[i] >> Y[i]; vll a(N, 0), b(M, 0); vvll ua(Q, vll(2, -1)), ub(Q, vll(2, -1)); rep(i, Q){ if(T[i]==1){ ua[i] = {a[X[i]-1], Y[i]}; a[X[i]-1] = Y[i]; } else{ ub[i] = {b[X[i]-1], Y[i]}; b[X[i]-1] = Y[i]; } } FenwickTree fa(Q+1), fb(Q+1), ca(Q+1), cb(Q+1); ca.update(0, N); cb.update(0, M); set<ll> ys = set<ll>(all(Y)); ys.insert(0); vll sys; for(ll y : ys) sys.push_back(y); unordered_map<ll, ll> y_idx; rep(i, sys.size()) y_idx[sys[i]] = i; ll t = 0; rep(i, Q){ if(T[i]==1){ ll da = fb.get(0, y_idx[ua[i][1]]) - fb.get(0, y_idx[ua[i][0]]); t += cb.get(0, y_idx[ua[i][1]]) * ua[i][1] - da - cb.get(0, y_idx[ua[i][0]]) * ua[i][0]; fa.update(y_idx[ua[i][0]], -ua[i][0]); fa.update(y_idx[ua[i][1]], ua[i][1]); ca.update(y_idx[ua[i][0]], -1); ca.update(y_idx[ua[i][1]], 1); } else{ ll db = fa.get(0, y_idx[ub[i][1]]) - fa.get(0, y_idx[ub[i][0]]); t += ca.get(0, y_idx[ub[i][1]]) * ub[i][1] - db - ca.get(0, y_idx[ub[i][0]]) * ub[i][0]; fb.update(y_idx[ub[i][0]], -ub[i][0]); fb.update(y_idx[ub[i][1]], ub[i][1]); cb.update(y_idx[ub[i][0]], -1); cb.update(y_idx[ub[i][1]], 1); } cout << t << endl; } }
//#include <atcoder/all> #include <bits/stdc++.h> #include <cassert> #include <numeric> using namespace std; //using namespace atcoder; typedef long long LONG; const int MOD = 1000000007; int gcd(int x, int y) { if (x % y == 0) { return y; } else { return gcd(y, x % y); //x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。 } } LONG pow2(long long x, long long n) { long long ret = 1; while (n > 0) { if (n & 1) ret = ret * x % MOD; // n の最下位bitが 1 ならば x^(2^i) をかける x = x * x % MOD; n >>= 1; // n を1bit 左にずらす } return (int)ret; } void printVector(const vector<int>& vec) { for (int value : vec) { cout << value << " "; } cout << endl; } LONG t(LONG N, LONG i) { return N - abs(i); } int count(vector<int>&a, int s) { int result = 0; for (int i = 0; i < a.size(); i++) { if (a[i] == s) { result++; } } return result; } LONG kaizyo(int N) { LONG result = 1; LONG mod = 998244353; for (LONG i = 2; i <= N; i++) { result *= i; result = result % mod; } return result; } class Point { public: int x; int y; }; LONG stock[3500][3500]; LONG test(int N, int task) { LONG mod = 998244353; if (N < task)return 0; if (N < 0)return 0; if (task == 0) { if (N == 0) { return 1; } else { return 0; } } if (stock[N][task] > 0)return stock[N][task]; stock[N][task] = (test(N - 1, task - 1) + test(N, task * 2)) % mod; return stock[N][task] % mod; } class DP { public: DP() { } DP(int a, int b) { other = a; path = b; } int other; int path; }; void dp(vector<vector<DP>>& list, vector<int> &able, int pos, int limit) { for (int i = 0; i < list[pos].size(); i++) { if (list[pos][i].path <= limit) { if (able[list[pos][i].other] == 0) { able[list[pos][i].other] = 1; dp(list, able, list[pos][i].other, limit); } } } } int main() { LONG a, b, x, y; cin >> a >> b >> x >> y; if (y > 2*x) { if (b >= a) { cout << (b - a)*2*x + x; return 0; } else { cout << (a - b - 1)*2*x + x; return 0; } } if (b >= a) { cout << (b - a)*y + x; return 0; } else { cout << (a - b - 1)*y + x; return 0; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i< int (n); i++) using namespace std; typedef long long ll; int main(){ vector<int> a(200100); int n; cin >> n; int k=0; for(int i=0; i<n; i++){ int p; cin >> p; a[p]++; while(k<200010){ if(a[k]==0) { cout << k << endl; break; } else k++; } } /* for(int i=0; i<n; i++){ for(int j=0; j<200100; j++){ if(a[j]==0) { cout << a[j] << endl; break; } } } */ }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int sz = 2e5+5; typedef pair <int,int> pii; #define x first #define y second int w[sz], b[sz], p[sz], g[sz]; bitset <sz> vis; int main() { int n; cin >> n; for(int i=1; i<=n; i++) scanf("%d", &w[i]); for(int i=1; i<=n; i++) scanf("%d", &b[i]); for(int i=1; i<=n; i++) scanf("%d", &p[i]); for(int i=1; i<=n; i++) { if(p[i] == i) { vis[i] = 1; continue; } if(b[p[i]] >= w[i]) { puts("-1"); exit(0); } g[p[i]] = i; } vector <pii> ans; for(int i=1; i<=n; i++) { if(vis[i]) continue; vector <int> now; int u = i; while(!vis[u]) { vis[u] = 1; now.push_back(u); u = g[u]; } int smol = 0; for(int j=0; j<now.size(); j++) { int p1 = now[j], p2 = now[smol]; if(b[p[p1]] < b[p[p2]]) smol = j; } rotate(now.begin(), now.begin()+smol, now.end()); for(int j=1; j<now.size(); j++) ans.emplace_back(now[j-1], now[j]); } printf("%d\n", ans.size()); for(auto &p : ans) printf("%d %d\n", p.x, p.y); }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1000000007; #define REP(i, n) for (ll i = 0; i < (n); i++) #define REP2(i, x, n) for (ll i = x; i < (n); i++) #define PR(x) cout << (x) << "\n" #define PS(x) cout << (x) << " " #define PRYES(x) PR((x) ? "Yes" : "No") const long long INF = numeric_limits<long long>::max(); template <class T> using arr = vector<vector<T> >; using vint = vector<int>; using vll = vector<ll>; // SourceTest用出力マクロ #ifdef SOURCE_TEST #define SPR(x) PR(x) #define SPS(x) PS(x) #define STRACE(var) cout << #var << "=" << var << endl; #else #define SPR(x) \ {} #define SPS(x) \ {} #define STRACE(var) \ {} #endif int main() { ll n; cin >> n; PR(n >= 0 ? n : 0); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a,b,c; cin>>a; if(a>=0) { printf("%d\n",a); } else { printf("0\n"); } }
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <climits> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> using namespace std; #define lson 2*i #define rson 2*i+1 #define LS l,mid,lson #define RS mid+1,r,rson #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define ll long long #define MAX 1000005 #define MOD 1000000007 #define EXP 1e-8 #define lowbit(x) (x&-x) const ll INF = 0x1fffffffffffffff; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} void chmin(ll& a, ll b){ if(a > b) a = b; } void chmax(ll& a, ll b){ if(a < b) a = b; } ll dp[105][105]; int main () { int n; ll x, a[105] = {0}, ans = INF; cin >> n >> x; for (int i = 0; i < n; i++) cin >>a[i]; for (int num = 1; num <= n; ++num) { // select num obj for (int j = 0; j < 105; ++j) for (int k = 0; k < 105; ++k) dp[j][k] = -INF; dp[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = num-1; j >= 0; --j) { for (int k = 0; k < num; ++k) { if (dp[j][k] != -INF) { chmax(dp[j+1][(k + a[i]) % num], a[i] + dp[j][k]); } } } } if (dp[num][x % num] != -INF) chmin(ans, (x - dp[num][x % num]) / num); } cout << ans <<endl; return 0; }
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <cassert> #include <cfloat> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; //#include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i) #define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i) #define Sort(v) sort(v.begin(), v.end()) #define rSort(v) sort(v.rbegin(), v.rend()) #define Reverse(v) reverse(v.begin(), v.end()) #define Lower_bound(v, y) \ distance(v.begin(), lower_bound(v.begin(), v.end(), y)) #define Upper_bound(v, y) \ distance(v.begin(), upper_bound(v.begin(), v.end(), y)) using ll = long long; using ull = unsigned long long; using P = pair<ll, ll>; using T = tuple<ll, ll, ll>; using vll = vector<ll>; using vP = vector<P>; using vT = vector<T>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vvP = vector<vector<P>>; using dqll = deque<ll>; ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0}; ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; 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; } /* Macros reg. ends here */ const ll INF = 1LL << 50; int main() { // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); ll n, x; cin >> n >> x; vvvll dp(n + 1, vvll(n + 1, vll(n + 1, -1))); repLRE(i, 1, n) dp[0][i][0] = 0; rep(_, n) { ll a; cin >> a; vvvll ndp = dp; rep(i, n) repLRE(j, 1, n) rep(k, j) { if (dp[i][j][k] != -1) { ll ni = i + 1; ll nk = (dp[i][j][k] + a) % j; chmax(ndp[ni][j][nk], dp[i][j][k] + a); } } swap(ndp, dp); } ll ans = x; repLRE(i, 1, n) rep(k, i) { ll v = x - dp[i][i][k]; if (dp[i][i][k] != -1 && v % i == 0) { ll tmp = v/i; chmin(ans, tmp); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define u unsigned #define endl '\n' #define mod 1000000007 #define quick ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); void solve(){ ll n; cin>>n; unordered_set<ll> s; for(ll a=2;a*a<=n;a++){ ll val = a*a; while(val<=n){ s.insert(val); val*=a; } } cout<<n - s.size()<<endl; } int main() { quick; int t=1; // cin>>t; while(t--){ solve(); } }
//雪花飄飄北風嘯嘯 //天地一片蒼茫 #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; #define ll long long #define ii pair<ll,ll> #define iii pair<ii,ll> #define fi first #define se second #define endl '\n' #define debug(x) cout << #x << " is " << x << endl #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define up upper_bound #define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> //change less to less_equal for non distinct pbds, but erase will bug mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); indexed_set s; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); ll p=2; while (p<40){ ll b=2; while (true){ ll curr=1; rep(x,0,p){ curr*=b; if (curr>1e10) goto die; } s.insert(curr); b++; } die:; p++; } ll n; cin>>n; cout<<n-s.order_of_key(n+1)<<endl; }
#include <bits/stdc++.h> #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> #define all_of(v) (v).begin(), (v).end() #define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define fi first #define se second const int MAXN = 101; //const LL INF = (LL) 1e9 + 8763; //const LL MOD = (LL) 1e9 + 7; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, mod, k, ans[MAXN], dp[MAXN][MAXN*MAXN*MAXN]; inline int add(int x, int y) { int sum = x + y; if (sum >= mod) sum -= mod; return sum; } void solve() { cin >> n >> k >> mod; int bound = n * (n + 1) / 2 * k; dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int r = 0; r < i; r++) { int sum = 0; for (int j = r; j <= bound; j += i) { sum = add(sum, dp[i - 1][j]); if (j - (k + 1) * i >= 0) { sum = add(sum, -dp[i - 1][j - (k + 1) * i] + mod); } dp[i][j] = sum; } } } for (int i = 1; i <= n; i++) { int a = i - 1, b = n - i; for (int j = 0; j <= bound; j++) { // cout << "At " << i << ' ' << j << " -> " << dp[a][j] << ' ' << dp[b][j] << '\n'; if (dp[a][j] == 0 || dp[b][j] == 0) continue; ans[i] = (ans[i] + (LL) dp[a][j] * dp[b][j] % mod * (k + 1) % mod) % mod; } ans[i] = (ans[i] - 1 + mod) % mod; } for (int i = 1; i <= n; i++) { cout << ans[i] << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc = 1; // cin >> tc; for (int i = 1; i <= tc; i++) { solve(); } return 0; }
#include <bits/stdc++.h> #define INF32 2147483647 #define INF64 9223372036854775807 #define rep(i, l, n) for(int i = l, i##_end = n; i < i##_end; ++i) #define rrep(i, n, l) for(int i = ((int)(n)-1), i##_end = l; i >= i##_end; --i) #define erep(i, n) for(auto &i : n) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using vi = vector<int>; using vii = vector<vi>; template<class S, class T> bool chmax(S &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } template<class S, class T> bool chmin(S &a, const T &b) { if(b < a) { a = b; return 1; } return 0; } int main() { // 1. input ll A, B, C; cin >> A >> B >> C; bool isEven = !(C % 2); // 2. char ans = '>'; if(A == B || (A == -B && isEven)) ans = '='; else if((isEven && abs(A) < abs(B)) || (!isEven && ((A <= 0 && 0 <= B) || (A < 0 && B < 0 && A > B)))) ans = '<'; // 3. output cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll=long long; static const ll mod=998244353; ll N,M; ll sum[5005]; ll POW[5005]; ll POWM[5005]; void init(){ for(ll i=0;i<5005;i++)POW[i]=1; for(ll i=0;i<5005;i++){ ll x=0; for(ll j=0;j<M;j++){ x=(x+POW[j])%mod;POW[j]=(j*POW[j])%mod; }sum[i]=x; } POWM[0]=1;for(ll i=0;i<5005;i++)POWM[i+1]=(M*POWM[i])%mod; } int main(){ cin>>N>>M;init(); ll ans=N*POWM[N]; for(ll n1=1;n1<N;n1++) for(ll n2=n1+1;n2<=N;n2++) ans=(ans+mod-(POWM[N-n2+n1-1]*sum[n2-n1-1])%mod)%mod; cout<<ans<<endl; return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<climits> #include<algorithm> #include<queue> #include<vector> #define pii pair<int,int> #define mp make_pair #define pb push_back #define fi first #define se second #define int long long #define mod 998244353 using namespace std; inline int read(){ int f=1,ans=0; char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int MAXN=111; int f[2][MAXN][MAXN*MAXN],N,fac[MAXN],ifac[MAXN],inv[MAXN],A[MAXN],sum; int binom(int a,int b){return fac[a]*ifac[b]%mod*ifac[a-b]%mod;} signed main(){ //freopen("9.in","r",stdin); N=read(); for(int i=1;i<=N;i++) A[i]=read(); fac[0]=fac[1]=inv[1]=ifac[0]=ifac[1]=1; for(int i=2;i<MAXN;i++) fac[i]=fac[i-1]*i%mod,inv[i]=(mod-mod/i)*inv[mod%i]%mod,ifac[i]=ifac[i-1]*inv[i]%mod; int cur=0; f[0][0][0]=1; for(int i=1;i<=N;i++){ cur^=1; memset(f[cur],0,sizeof(f[cur])); for(int j=0;j<i;j++) for(int k=0;k<=sum;k++) f[cur][j][k]=f[cur^1][j][k]; for(int j=0;j<i;j++) for(int k=0;k<=sum;k++) if(f[cur^1][j][k]) f[cur][j+1][k+A[i]]+=f[cur^1][j][k],f[cur][j+1][k+A[i]]%=mod; sum+=A[i]; } if(sum&1){printf("0\n");return 0;} int Ans=0; for(int i=1;i<=N;i++) Ans+=f[cur][i][sum/2]*fac[i]%mod*fac[N-i]%mod,Ans%=mod; printf("%lld\n",Ans); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long int ull; #define pii pair<ll,ll> #define vi vector<ll> #define vii vector<pii> #define endl "\n" #define pb push_back #define sq(a) ((a)*(a)) #define sz(x) (ll)(x).size() #define all(x) (x).begin(), (x).end() #define rall(a) (a).rbegin(),(a).rend() #define x first #define y second #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugv(v) cerr<<#v<<" : "; for(auto x:v) cerr<<x<<' '; cerr<<endl; #define output(x) cout<<(x ? "YES" : "NO")<<endl; #define Output(x) cout<<(x ? "Yes" : "No")<<endl; #define MOD (1000000007) #define MOD2 (998244353) #define PI acos(-1) #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL); ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll exp(ll x,ll n,ll mod) { ll result=1; while(n) { if(n & 1) result=(result * x)%mod; x=(x*x)%mod; n>>=1; } return result; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS int mn=101; for(int i=0;i<4;i++) { int x;cin>>x; mn=min(mn,x); } cout<<mn<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define P pair<ll,ll> 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 main(){ int a,b,c,d; cin>>a>>b>>c>>d; cout<<min(min(a,b),min(c,d))<<endl; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; //#include<boost/multiprecision/cpp_int.hpp> //#include<boost/multiprecision/cpp_dec_float.hpp> //namespace mp=boost::multiprecision; //#define mulint mp::cpp_int //#define mulfloat mp::cpp_dec_float_100 struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init; #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) constexpr int MOD=1000000007; //constexpr int MOD=998244353; #define INF (1<<30) #define LINF (lint)(1LL<<56) #define endl "\n" #define rep(i,n) for(lint (i)=0;(i)<(n);(i)++) #define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--) #define Flag(x) (1<<(x)) #define Flagcount(x) __builtin_popcountll(x) #define pint pair<int,int> #define pdouble pair<double,double> #define plint pair<lint,lint> #define fi first #define se second typedef long long lint; int dx[8]={1,1,0,-1,-1,-1,0,1}; int dy[8]={0,1,1,1,0,-1,-1,-1}; const int MAX_N=2e5+5; //struct edge{lint to,num;}; //vector<int> bucket[MAX_N/1000]; int main(void){ int N; cin >> N; lint A[N],B[N]; rep(i,N) cin >> A[i]; rep(i,N) cin >> B[i]; lint ans=accumulate(A,A+N,0LL); int n=N/2; lint odd[n],even[N]; for(int i=0;i<N;i+=2) even[i/2]=B[i]-A[i]; for(int i=1;i<N;i+=2) odd[i/2]=B[i]-A[i]; sort(odd,odd+n,greater<lint>()); sort(even,even+n,greater<lint>()); rep(i,n){ if(odd[i]+even[i]>0) ans+=odd[i]+even[i]; } cout << ans << endl; }
#include<iostream> #include<vector> using namespace std; typedef long long li; #define minimize(a,b) (a>(b)?a=(b),1:0) #define rep(i,n) for(int i=0;i<(n);i++) #define df 0 template<class T> void print(const T& t){ cout << t << "\n"; } template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); } // Container コンテナ型, map以外 template< template<class ...> class Ctn,class T> std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){ auto itr=v.begin(); while(itr!=v.end()){ if(itr!=v.begin())cout << " "; cout << *(itr++); } return os; }; void makea(vector<vector<li>>& c,vector<li>& a){ int n=c.size(); rep(i,n-1){ a[i]=c[i+1][0]-c[0][0]; } } void makeb(vector<vector<li>>& c,vector<li>& b){ int n=c.size(); rep(j,n-1){ b[j]=c[0][j+1]-c[0][0]; } } int judgea(vector<vector<li>>& c,vector<li>& a){ int n=c.size(); rep(i,n-1){ rep(j,n){ if(a[i]!=c[i+1][j]-c[0][j]) return 0; } } return 1; } int judgeb(vector<vector<li>>& c,vector<li>& b){ int n=c.size(); rep(j,n-1){ rep(i,n){ if(b[j]!=c[i][j+1]-c[i][0]) return 0; } } return 1; } pair<li,int> mini(vector<li>& a){ int n=a.size(); int it=0; li m=0; rep(i,n){ if(minimize(m,a[i])) it=i+1; } return {m,it}; } int main(){ int n; cin >>n; vector<vector<li>> c(n,vector<li>(n)); rep(i,n) rep(j,n) cin >>c[i][j]; if(n==1){ print("Yes"); print(0); print(c[0]); return 0; } vector<li> a(n-1,0),b(n-1,0); makea(c,a); if(df)print(a); makeb(c,b); if(df)print(b); if(!judgea(c,a)){ print("No"); return 0; } if(!judgeb(c,b)){ print("No"); return 0; } if(df)print(1); pair<li,int> ma=mini(a),mb=mini(b); li aa=c[ma.second][mb.second]-ma.first; li bb=-mb.first; vector<li> ansa(n,aa),ansb(n,bb); rep(i,n-1){ ansa[i+1]=aa+a[i]; ansb[i+1]=bb+b[i]; } print("Yes"); print(ansa); print(ansb); }
#include <bits/stdc++.h> using namespace std; // #define LOCAL // 提出時はコメントアウト #define DEBUG_ typedef long long ll; const double EPS = 1e-9; const ll INF = ((1LL<<62)-(1LL<<31)); typedef vector<ll> vecl; typedef pair<ll, ll> pairl; template<typename T> using uset = unordered_set<T>; template<typename T, typename U> using mapv = map<T,vector<U>>; template<typename T, typename U> using umap = unordered_map<T,U>; #define ALL(v) v.begin(), v.end() #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define sz(x) (ll)x.size() ll llceil(ll a,ll b) { return (a+b-1)/b; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> vector<vector<T>> genarr(ll n, ll m, T init) { return vector<vector<T>>(n,vector<T>(m,init)); } ///// DEBUG #define DUMPOUT cerr #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) template<typename T>istream&operator>>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;} template<typename T,typename U>ostream&operator<<(ostream&os,pair<T,U>&pair_var){os<<"("<<pair_var.first<<", "<<pair_var.second<<")";return os;} template<typename T>ostream&operator<<(ostream&os,const vector<T>&vec){os<<"{";for(int i=0;i<vec.size();i++){os<<vec[i]<<(i+1==vec.size()?"":", ");} os<<"}";return os;} template<typename T,typename U>ostream&operator<<(ostream&os,map<T,U>&map_var){os<<"{";repi(itr,map_var){os<<*itr;itr++;if(itr!=map_var.end())os<<", ";itr--;} os<<"}";return os;} template<typename T>ostream&operator<<(ostream&os,set<T>&set_var){os<<"{";repi(itr,set_var){os<<*itr;itr++;if(itr!=set_var.end())os<<", ";itr--;} os<<"}";return os;} void dump_func(){DUMPOUT<<endl;} template<class Head,class...Tail>void dump_func(Head&&head,Tail&&...tail){DUMPOUT<<head;if(sizeof...(Tail)>0){DUMPOUT<<", ";} dump_func(std::move(tail)...);} #ifndef LOCAL #undef DEBUG_ #endif #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \ << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif ////////// #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") int solve(ostringstream &cout) { #ifdef LOCAL ifstream in("../../Atcoder/input.txt"); cin.rdbuf(in.rdbuf()); #endif ll A,B; cin>>A>>B; ll N = B - A + 1; vecl P = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; while (sz(P) > 0 && N < P.back()) P.pop_back(); ll U = 1LL<<sz(P); auto dp = genarr(N+1,U+1,0LL); dp[0][0] = 1; ll ans = 0; rep(i,N) { ll x = i + A; ll mask = 0; rep(j,sz(P)) { if (x % P[j]) continue; mask += 1LL<<j; // if (S & (1LL<<j)) ok = false; } rep(S,U) { bool ok = true; rep(j,sz(P)) if ((mask & (1LL<<j)) && (S & (1LL<<j))) ok = false; if (ok) dp[i+1][S | mask] += dp[i][S]; dp[i+1][S] += dp[i][S]; } if (i == N-1) { rep(S,U) ans += dp[i+1][S]; } } cout << ans << endl; return 0; } int main() { ostringstream oss; int res = solve(oss); cout << oss.str(); return res; }
#include <iostream> main(){int H,W,A,P,K=1e5;for(std::cin>>H>>W;std::cin>>A;P+=A)K=std::min(A,K);std::cout<<P-K*H*W;}
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define nl '\n' #define X first #define Y second #define nimble ios::sync_with_stdio(0);cin.tie(0); #define all(v) (v).begin(),(v).end() #define _sz(x) (int)x.size() #define rep(i,a,b) for(int i=a;i<b;i++) #define repr(i,b,a) for(int i=b;i>=a;i--) #define preci(x) fixed<<setprecision(x) #define tc int tt=1;cin>>tt;while(tt--) #define TRACE #ifdef TRACE #define see(...) __f(#__VA_ARGS__,__VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr<<name<<" : "<<arg1<<nl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma=strchr(names+1,','); cerr.write(names,comma-names)<<" : "<<arg1<<" | "; __f(comma+1, args...); } #else #endif int inf=0x3f3f3f3f; ll infl=0x3f3f3f3f3f3f3f3fLL; ld infd=1.0/0.0; const ll MOD=1e9+7;// 998244353 // = (119<<23)+1 const ld pi=2*acos(0.0); int main(){ nimble; int n,k; cin>>n>>k; vector<vector<ll>> ct(n,vector<ll>(n)); rep(i,0,n) rep(j,0,n) cin>>ct[i][j]; vector<int> p; rep(i,1,n) p.pb(i); int cnt=0; do{ ll cst=0; int in=0; rep(i,0,_sz(p)){ cst+=ct[in][p[i]]; in=p[i]; if(i==_sz(p)-1) cst+=ct[0][p[i]]; } if(cst==k) cnt++; }while(next_permutation(all(p))); cout<<cnt; return 0; }
#define _USE_MATH_DEFINES #include <iostream> #include <sstream> #include <string> #include <list> #include <vector> #include <queue> #include <algorithm> #include <climits> #include <cstring> #include <cmath> #include <stack> #include <iomanip> #include <tuple> #include <functional> #include <cfloat> #include <map> #include <set> #include <array> #include <stdio.h> #include <string.h> #include <random> #include <cassert> using ll = long long; using ull = unsigned long long; using uint = unsigned int; using namespace std; #define int long long #define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end()) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ARY_SORT(a, size) sort((a), (a)+(size)) #define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end())) #define REVERSE(v) (reverse((v).begin(), (v).end())) #define ARY_REVERSE(v,a) (reverse((v), (v)+(a))) #define REP(i, n) for (int (i)=0; (i) < (n); (i)++) #define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++) #define CONTAINS_MAP(m, a) ((m).find((a)) != m.end()) #define CONTAINS_SET(m, a) ((m).find((a)) != m.end()) void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); } void Yes() { cout << "Yes" << endl; exit(0); } void No() { cout << "No" << endl; exit(0); } signed main() { char s, t; cin >> s; cin >> t; if (s == 'Y') { cout << (char)(t + ('A' - 'a')) << endl; } else { cout << t << endl; } }
#include <bits/stdc++.h> using namespace std; #define fo(i, x, y) for (int i = (x); i <= (y); ++i) #define fd(i, x, y) for (int i = (x); i >= (y); --i) typedef long long ll; const int maxn = 2e5 + 5; int n; ll a[maxn], s[maxn]; int getint() { char ch; int res = 0, p; while (!isdigit(ch = getchar()) && (ch ^ '-')); p = ch == '-'? ch = getchar(), -1 : 1; while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar(); return res * p; } int main() { //freopen("t.in", "r", stdin); n = getint(); fo(i, 1, n) a[i] = getint(), s[i] = s[i - 1] + a[i]; ll sum = 0, mx = 0; fo(i, 1, n) { mx = max(mx, a[i]); sum += s[i]; printf("%lld\n", sum + mx * i); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // -------------------------------------------------------- template <class T> bool chmax(T& a, const T b) { if(a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T b) { if(b < a) { a = b; return 1; } return 0; } #define FOR(i, l, r) for(int i = (l); i < (r); ++i) #define RFOR(i, l, r) for(int i = (r)-1; (l) <= i; --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define SUM(c) accumulate(ALL(c), 0) #define SUMLL(c) accumulate(ALL(c), 0LL) #define SZ(c) ((int)(c).size()) #define CIN(c) cin >> (c) #define COUT(c) cout << (c) << '\n' #define debug(x) cerr << #x << " = " << (x) << '\n'; using P = pair<ll, ll>; using VP = vector<P>; using VVP = vector<VP>; using VS = vector<string>; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<ll>; using VVLL = vector<VLL>; using VB = vector<bool>; using VVB = vector<VB>; using VD = vector<double>; using VVD = vector<VD>; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const ll MOD = 1000000007; // static const ll MOD = 998244353; static const int INF = (1 << 30) - 1; // 1073741824 - 1 // static const ll INF = (1LL << 60) - 1; // 4611686018427387904 - 1 const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // メモ化意味あんのかなぁ??わかんないからコンテスト終わったらメモ化実験する double dp[101][101][101]; int n; bool found[101][101][101]; double rec(int a, int b, int c) { if(a < 0 || b < 0 || c < 0) { return 0; } if(a >= 100 || b >= 100 || c >= 100) return 1 / 3; if(found[a][b][c]) return dp[a][b][c]; double val = 1; double total = a + b + c; val += rec(a + 1, b, c) * (a / total); val += rec(a, b + 1, c) * (b / total); val += rec(a, b, c + 1) * (c / total); found[a][b][c] = true; dp[a][b][c] = val; return val; } int main() { cin.tie(0); ios::sync_with_stdio(false); int a, b, c; cin >> a >> b >> c; double ans = rec(a, b, c); cout << fixed << setprecision(15) << ans << endl; return 0; }
#include <bits/stdc++.h> #define clr(x) memset(x,0,sizeof(x)) #define For(i,a,b) for (int i=(a);i<=(b);i++) #define Fod(i,b,a) for (int i=(b);i>=(a);i--) #define fi first #define se second #define kill _z_kill #define y0 _z_y0 #define y1 _z_y1 #define x0 _z_x0 #define x1 _z_x1 #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) using namespace std; typedef long long LL; typedef unsigned long long ull; typedef unsigned uint; typedef long double LD; typedef vector <int> vi; typedef pair <int,int> pii; void enable_comma(){} string tostring(char c){ string s=""; s+=c; return s; } string tostring(string s){ return "\""+s+"\""; } string tostring(const char *c){ return tostring((string)c); } string tostring(long long x){ if (x<0) return "-"+tostring(-x); if (x>9) return tostring(x/10)+tostring(char('0'+x%10)); else return tostring(char('0'+x)); } string tostring(int x){ return tostring((long long)x); } string tostring(unsigned long long x){ if (x>9) return tostring((long long)(x/10))+tostring(char('0'+x%10)); else return tostring(char('0'+x)); } string tostring(unsigned x){ return tostring((long long)x); } string tostring(double x){ static char res[114]; sprintf(res,"%lf",x); string s=tostring(res); return s.substr(1,(int)s.size()-2); } string tostring(long double x){ return tostring((double)x); } template <class A,class B> string tostring(pair <A,B> p){ return "("+tostring(p.fi)+","+tostring(p.se)+")"; } template <class T> string tostring(T v){ string res=""; for (auto p : v) res+=(res.size()?",":"{")+tostring(p); return res.size()?res+"}":"{}"; } template <class A> string tostring(A* a,int L,int R){ return tostring(vector <A>(a+L,a+R+1)); }; template <class A> string tostring(A a,int L,int R){ return tostring(a.data(),L,R); } string tostrings(){ return ""; } template <typename Head,typename... Tail> string tostrings(Head H,Tail... T){ return tostring(H)+" "+tostrings(T...); } #define User_Time ((double)clock()/CLOCKS_PER_SEC) #ifdef zzd #define outval(x) cerr<<#x" = "<<tostring(x)<<endl #define outvals(...) cerr<<"["<<#__VA_ARGS__<<"]: "<<\ tostrings(__VA_ARGS__)<<endl #define outtag(x) cerr<<"--------------"#x"---------------"<<endl #define outsign(x) cerr<<"<"#x">"<<endl #define outarr(a,L,R) cerr<<#a"["<<(L)<<".."<<(R)<<"] = "<<\ tostring(a,L,R)<<endl #else #define outval(x) enable_comma() #define outvals(...) enable_comma() #define outtag(x) enable_comma() #define outsign(x) enable_comma() #define outarr(a,L,R) enable_comma() #endif #ifdef ONLINE_JUDGE #ifdef assert #undef assert #endif #define assert(x) (!(x)?\ cout<<"Assertion failed!"<<endl<<\ "function: "<<__FUNCTION__<<endl<<\ "line: "<<__LINE__<<endl<<\ "expression: "<<#x<<endl,exit(3),0:1) #endif LL read(){ LL x=0,f=0; char ch=getchar(); while (!isdigit(ch)) f=ch=='-',ch=getchar(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?-x:x; } template <class T> void ckmax(T &x,const T y){ if (x<y) x=y; } template <class T> void ckmin(T &x,const T y){ if (x>y) x=y; } //int Pow(int x,int y){ // int ans=1; // for (;y;y>>=1,x=(LL)x*x%mod) // if (y&1) // ans=(LL)ans*x%mod; // return ans; //} //void Add(int &x,int y){ // if ((x+=y)>=mod) // x-=mod; //} //void Del(int &x,int y){ // if ((x-=y)<0) // x+=mod; //} //int Add(int x){ // return x>=mod?x-mod:x; //} //int Del(int x){ // return x<0?x+mod:x; //} //int md(LL x){ // return (x%mod+mod)%mod; //} const int N=100005; int n; int a[N]; void solve(){ n=read(); For(i,1,n) a[i]=read(); sort(a+1,a+n+1); if (n&1){ puts("Second"); } else { LL s0=0,s1=0; For(i,1,n){ if (i&1) s0+=a[i]; else s1+=a[i]; } if (s0==s1) puts("Second"); else puts("First"); } } int main(){ int T=read(); while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0); #define print(a) for(int i=0;i<(a.size());i++){cout<<a[i]<<"\n";} #define sortt(a) sort(a.begin(),a.end()) #define rep(i,a,b) for(int i=a;i<b;i++) #define int long long #define pb push_back #define PQ priority_queue #define F first #define S second #define mod 1000000007 #define endl "\n" const int N=1000005; int calc(int n) { vector<int>v1,v2; while(n>0) { int x=n%10; v1.pb(x); v2.pb(x); n=n/10; } sortt(v1); sort(v2.begin(),v2.end(),greater<int>()); int maxx=0; int minn=0; int c=v2.size(); for(int i=0;i<v2.size();i++) { if(v2[i]==0) { c--; continue; } maxx+=v2[i]*powl(10,c-1); c--; } c=v1.size(); for(int i=0;i<v1.size();i++) { minn+=v1[i]*powl(10,c-1); c--; } return maxx-minn; } void solve() { int n,k; cin>>n>>k; int x=n; while(k--) { x=calc(x); } cout<<x<<endl; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS; int t=1; // cin>>t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> #define pi 3.141592653897932384626 #define abs(x) ((x)>0?(x):(-(x))) #define eps (double)(0.0000001) #define fs first #define se second //freopen("in.txt","r",stdin);freopen("out.txt","w",stdout); using namespace std; int main() { int n,a[400005],mod=3; int fex[400005],g[400005],m[3]={1,1,2}; cin>>n; char c; char x[10]; fgets(x,100,stdin); fex[0]=0; g[0]=0; for(int i=1;i<n;++i){ if(i%3==0) if((i/3)%3==0){ fex[i]=fex[i/3]+1; g[i]=g[i/3]; } else{ fex[i]=fex[i/3]+1; g[i]=i/3%3; } else{ fex[i]=0; g[i]=i%3; } } for(int i=2;i<n;++i){ fex[i]+=fex[i-1]; g[i]=(g[i]*g[i-1])%3; } for(int i=0;i<n;++i){ cin>>c; switch(c){ case 'B':a[i]=0; break; case 'R':a[i]=1; break; case 'W':a[i]=2; break; }; } int s=0; for(int i=0;i<n;++i){ if(fex[n-1]==fex[i]+fex[n-1-i]) s+=a[i]*m[g[n-1]]*m[g[i]]*m[g[n-1-i]]%mod; } s%=mod; if(!(n%2))s=(mod-s)%mod; cout<<(s==0?'B':(s==1?'R':'W'))<<endl; return 0; }
#include <iostream> #include <algorithm> #include <unordered_map> #include <vector> #include <string> #include <cmath> #include <queue> #include <deque> using namespace std; typedef long long ll; typedef vector<ll> vll; typedef tuple<ll, ll, ll> tplll; typedef pair<ll, ll> pll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) const ll MOD = 1e9 + 7; ll modpow(ll a, ll b) { if (b == 0) return 1; return modpow(a * a % MOD, b / 2) * (b & 1 ? a : 1) % MOD; } ll modulo(ll a) { a %= MOD; a += MOD; a %= MOD; return a; } void show_v( const vll l) { for (auto x: l) { cout << x << " "; } cout << endl; } void show_vpll( const vector<pair<ll, ll>> l) { for (auto x: l) { cout << get<0>(x) << ":" << get<1>(x) << " "; } cout << endl; } bool comp_tp( const tplll &lhs, const tplll &rhs ) { if (std::get<0>(lhs) != std::get<0>(rhs)) return std::get<0>(lhs) < std::get<0>(rhs); if (std::get<1>(lhs) != std::get<1>(rhs)) return std::get<1>(lhs) < std::get<1>(rhs); return std::get<2>(lhs) < std::get<2>(rhs); } bool comp_pll( const pll &lhs, const pll &rhs ) { if (std::get<0>(lhs) != std::get<0>(rhs)) return std::get<0>(lhs) < std::get<0>(rhs); if (std::get<1>(lhs) != std::get<1>(rhs)) return std::get<1>(lhs) < std::get<1>(rhs); else return true; } vll all_divisors(ll x, bool with_self=true, bool do_sort=false){ // ex: 100 -> [2,50,4,25,5,20,10,(100 if self=true)] vll ret; for (ll i = 2; i < (ll)sqrt(x) + 1; i++) { if (x % i == 0) { ret.push_back(i); if (x != i*i) { ret.push_back(x/i); } } } if (with_self) {ret.push_back(x);} if (do_sort) {sort(ret.begin(), ret.end());} return ret; } unordered_map<ll, ll> prime_factors(ll x) { unordered_map<ll, ll> ret; for (ll i=2; i < sqrt(x) + 2; i++) { if (x % i != 0) { continue; } ll tmp = 0; while(x % i == 0) { tmp++; x = x/i; } ret[i] = tmp; } if (x != 1) {ret[x] = 1;} return ret; } long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } ll comb(ll m, ll n) { if (m < n) {return 0;} if (n == 0) { return 1;} if (n == 1) { return m;} ll ret = 1; rep(i, n) {ret *= m-i;} rep(i, n) {ret /= i+1;} return ret % 3; } ll lucas(ll m, ll n) { if (m < n) {return 0;} if (n == 0) { return 1;} if (n == 1) { return m;} ll ret = 1; // cout << m << n << endl; while (m > 0) { auto c = m % 3; auto d = n % 3; ret *= comb(c, d); ret %= 3; m /= 3; n /= 3; // cout << ret << endl; } return ret % 3; } int main() { ll n; cin >> n; vector<ll> cn(n); string s; cin >> s; rep(i, n) { char c = s[i]; if (c == 'B') cn[i] = 0; if (c == 'W') cn[i] = 1; if (c == 'R') cn[i] = 2; } // cout << "ll" << endl; ll ans = 0; // show_v(cn); rep(i, n) { ans += lucas(n-1, i) * cn[i] + 3; // cout << lucas(n-1, i) * cn[i] << endl; ans %= 3; } if (n % 2 == 0) { ans = 3 - (ans% 3);} ans %= 3; // cout << ans << endl; if (ans == 0) cout << 'B' << endl; if (ans == 1) cout << 'W' << endl; if (ans == 2) cout << 'R' << endl; return 0; }
#include <string> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <queue> #include <deque> #include <bitset> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <functional> #include <numeric> #include <utility> #include <limits> #include <time.h> #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include<unordered_set> #include<climits> //#include<bits/stdc++.h> using namespace std; using namespace std; #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) /******* All Required define Pre-Processors and typedef Constants *******/ #define SCD(t) scanf("%d",&t) #define SCLD(t) scanf("%ld",&t) #define SCLLD(t) scanf("%lld",&t) #define SCC(t) scanf("%c",&t) #define SCS(t) scanf("%s",t) #define SCF(t) scanf("%f",&t) #define SCLF(t) scanf("%lf",&t) #define MEM(a, b) memset(a, (b), sizeof(a)) #define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in) #define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in) #define REP(i, j) FOR(i, 0, j, 1) #define RREP(i, j) RFOR(i, j, 0, 1) #define all(cont) cont.begin(), cont.end() #define rall(cont) cont.end(), cont.begin() #define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++) #define IN(A, B, C) assert( B <= A && A <= C) #define REP1(m,n) for(int i=m;i<=(n);i++) #define REP2(m,n) for(int j=m;j<=(n);j++) #define REP3(m,n) for(int i=0;i<(n);i++) #define REP4(m,n) for(int i=n;i>=0;i--) #define MP make_pair #define PB push_back #define INF (int)1e9 #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 #define read(type) readInt<type>() const double pi = acos(-1.0); #define pnl printf("\n") #define pns printf("@\t") #define pns1(s) printf("%s",s) #define pn(l) printf("%d\t",l) typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<string> VS; typedef vector<PII> VII; typedef vector<VI> VVI; typedef map<int, int> MPII; typedef set<int> SETI; typedef multiset<int> MSETI; typedef long int int32; typedef unsigned long int uint32; typedef long long int int64; typedef unsigned long long int uint64; //int min_coin = INT_MAX; int main() { fast; #ifndef ONLINE_JUDGE freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout); #endif int a, b, c, mid_ele; //preprocess(); cin >> a >> b >> c; int last_ele = max(a, max(b, c)); int first_ele = min(a, min(b, c)); if (last_ele == a && first_ele == b) mid_ele = c; else if (last_ele == b && first_ele == c) mid_ele = a; else mid_ele = b; string s; if (mid_ele - first_ele != last_ele - mid_ele) cout << "No" << endl; else cout << "Yes"; /*cout << "hello World" << endl; fprintf(stdout, "\nTIME: %.3lf sec\n", (double)clock() / (CLOCKS_PER_SEC));*/ return 0; }//end main*=1
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; vector<int>v; v.push_back(a); v.push_back(b); v.push_back(c); sort(v.begin(),v.end()); if(v[2]-v[1]==v[1]-v[0]) cout<<"Yes"; else cout<<"No"; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> 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 dph[2000], dphw[4000], *naname = dphw + 2000; const int mod = 1e9 + 7; int main() { //cin.tie(0); //ios::sync_with_stdio(false); int H = 0, W = 0; char c; while ((c = getchar_unlocked()) >= '0') H = H * 10 + c - '0'; while ((c = getchar_unlocked()) >= '0') W = W * 10 + c - '0'; getchar_unlocked(); dph[0] = 1; naname[0] = 1; ll w = 1; ll tmp = 1; rep1(i, W - 1) { tmp = w; if (getchar_unlocked() == '.') { dph[i] = w; naname[i] = w; w = w * 2 % mod; } else { dph[i] = 0; naname[i] = 0; w = 0; } } getchar_unlocked(); rep1(i, H - 1) { if (getchar_unlocked() == '.') { tmp = dph[0]; w = tmp; dph[0] = (tmp + dph[0]) % mod; naname[-i] = (tmp + naname[-i]) % mod; } else { w = 0; dph[0] = 0; naname[-i] = 0; } if (!(i & 7)) { rep1(j, W - 1) { if (getchar_unlocked() == '.') { tmp = w + dph[j] + naname[j - i]; w += tmp - ll(double(w + tmp) / mod) * mod; dph[j] += tmp - ll(double(dph[j] + tmp) / mod) * mod; naname[j - i] += tmp - ll(double(tmp + naname[j - i]) / mod) * mod; } else { w = 0; dph[j] = 0; naname[j - i] = 0; } } } else { rep1(j, W - 1) { if (!(j & 7)) { if (getchar_unlocked() == '.') { tmp = w + dph[j] + naname[j - i]; w += tmp - ll(double(w + tmp) / mod) * mod; dph[j] += tmp - ll(double(dph[j] + tmp) / mod) * mod; naname[j - i] += tmp - ll(double(tmp + naname[j - i]) / mod) * mod; } else { w = 0; dph[j] = 0; naname[j - i] = 0; } } else { if (getchar_unlocked() == '.') { tmp = w + dph[j] + naname[j - i]; w = w + tmp; dph[j] = tmp + dph[j]; naname[j - i] = tmp + naname[j - i]; } else { w = 0; dph[j] = 0; naname[j - i] = 0; } } } } getchar_unlocked(); } printf("%lld\n", tmp % mod); Would you please return 0; }
#include<bits/stdc++.h> #include<iostream> #include<map> #include<math.h> #include<string> #include<string.h> #include<vector> #include<queue> #include<algorithm> #include<set> #include<stack> #define _GLIBCXX_DEBUG #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() using namespace std; using ll=long long; const ll INF=5000000000000; const ll dx[8]={1,0,-1,0,1,1,-1,-1}; const ll dy[8]={0,1,0,-1,1,-1,1,-1}; ll mod(ll x,ll m){return x & m;} ll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0){u+=m;}return u;} ll modpow(ll a,ll n,ll m){ll res=1;while(n>0){if(n&1){res=res*a%m;}a=a*a%m;n>>=1;}return res;} ll npow(ll a,ll n){ll res=1;while(n>0){if(n&1){res=res*a;}a=a*a;n>>=1;}return res;} ll yaku(ll x){ll cnt=0;for(int i=1;i*i<=x;i++){if(x%i==0){if(i==x/i){cnt++;}else{cnt+=2;}}}return cnt;} ll modwaru(ll a, ll b, ll m){a%=m;return a*modinv(b,m)%m;} ll gcd(ll x, ll y){if(x%y==0){return y;}else{return gcd(y, x % y);}} ll lcm(ll a, ll b){return a/gcd(a, b)*b;} struct UnionFind{vector<ll>par;UnionFind(ll N):par(N){for(ll i=0;i<N;i++)par[i]=i;}ll root(ll x){if(par[x]==x){return x;}return par[x]=root(par[x]);}void unite(ll x,ll y){ ll rx=root(x);ll ry=root(y);if(rx==ry){return;}par[rx]=ry;}bool same(ll x,ll y){ll rx=root(x);ll ry=root(y);return rx==ry;}}; int main(){ ll H, W, M = 1000000007; cin >> H >> W; vector<vector<char>> S(H, vector<char>(W)); for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++) cin >> S[i][j]; } vector<vector<ll>> dp(H + 1, vector<ll>(W + 1, 0)), tate(H + 1, vector<ll>(W + 1, 0)), yoko(H + 1, vector<ll>(W + 1, 0)), naname(H + 1, vector<ll>(W + 1, 0)); dp[1][1] = 1; for(int i = 1; i <= H; i++){ for(int j = 1; j <= W; j++){ if(S[i - 1][j - 1] == '#') continue; tate[i][j] += tate[i - 1][j] + dp[i - 1][j]; yoko[i][j] += yoko[i][j - 1] + dp[i][j - 1]; naname[i][j] += naname[i - 1][j - 1] + dp[i - 1][j - 1]; dp[i][j] += tate[i][j] + yoko[i][j] + naname[i][j]; tate[i][j] %= M; yoko[i][j] %= M; naname[i][j] %= M; dp[i][j] %= M; } } cout << dp[H][W] << endl; }
#include<bits/stdc++.h> #define N 5000005 using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int n,a[N],b[N],ans; bool tag[2005]; int main() { n = read(); for(int i=1; i<=n; ++i)a[i] = read(); for(int i=1; i<=n; ++i) { for(int j=1; j<=sqrt(a[i]); ++j) { if(a[i]%j == 0)b[++b[0]] = j,b[++b[0]] = a[i]/j; } } sort(b+1,b+b[0]+1); b[0] = unique(b+1,b+b[0]+1)-b-1; for(int i=1; i<=b[0]; ++i) { int x = b[i],now = 0,id,suc; memset(tag,0,sizeof(tag)); for(int j=1; j<=n; ++j)if(a[j]%x == 0){suc = now = a[j];id = j+1;tag[j] = 1;break;} for(int j=id; j<=n; ++j) { if(a[j]%x == 0)now = __gcd(now,a[j]),tag[j] = 1; } if(now == x)suc = 1; else suc = 0; for(int j=1; j<=n; ++j)if(now > a[j])suc = 0; ans += suc; } cout << ans; }
#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; int lo = *min_element(a.begin(), a.end()); unordered_map<int, int> memo; for (int ai : a) { for (int j = 1; j * j <= ai && j < lo; ++j) { if (ai % j == 0) { memo[j] = __gcd(ai, memo[j]); if (ai / j < lo) memo[ai / j] = __gcd(ai, memo[ai / j]); } } } int ans = 1; for (auto [factor, terminal] : memo) if (factor == terminal) ans++; cout << ans; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long #define ull unsigned long long #define ll long long #define M 998244353 #define pb push_back #define p_q priority_queue #define pii pair<ll,ll> #define vi vector<ll> #define vii vector<pii> #define mi map<ll,ll> #define mii map<pii,ll> #define all(a) (a).begin(),(a).end() #define sz(x) (ll)x.size() #define endl '\n' #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b)) / gcd((a),(b)) #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define mp make_pair #define lb lower_bound #define ub upper_bound #define F first #define S second #define rep(i, begin, end) for (int i=begin;i<end;i++) #define repd(i, begin, end) for (int i=end-1;i>=begin;i--) #define ini(a,n,b) for(ll int i=0;i<n;i++) a[i]=0; #define cset(a) __builtin_popcountll(a) #define hell 1000000007 #define re resize #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update > const int INF=1e18; int powm(int a,int b,int mod) { int res=1; while(b) { if(b&1) res=(res*a)%mod; a=(a*a)%mod; b>>=1; } return res; } int pow(int a,int b) { int res=1; while(b) { if(b&1) res=(res*a); a=(a*a); b>>=1; } return res; } const int N = 100005; ll dis[N]; vector<pair<int,pair<int,int> > > v[N]; signed main(void) {ios #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout); #endif int tests=1; // cin>>tests; while(tests--) { int n,m,x,y; cin>>n>>m>>x>>y; for(int i=1;i<=n;++i) dis[i] = INF; dis[x] = 0; for(int i=0;i<m;++i) { int x,y,t,k; cin>>x>>y>>t>>k; v[x].pb({y,{t,k}}); v[y].pb({x,{t,k}}); } set<pii> s; s.insert({0ll,x}); while(!s.empty()) { auto j = *s.begin(); int x=j.F,y=j.S; s.erase(s.begin()); for(auto p:v[y]) { int j=p.F,k=p.S.F,l=p.S.S; ll u = ceil(x*1.0/l)*l; if(dis[j] > u + k) { s.erase({dis[j],j*1ll}); dis[j] = u+k; s.insert({dis[j],j*1ll}); } } } if(dis[y] == INF) dis[y] = -1; cout<<dis[y]<<endl; } }
#define LOCAL #define _USE_MATH_DEFINES #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <vector> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <complex> #include <cmath> #include <numeric> #include <bitset> #include <functional> #include <random> #include <ctime> using namespace std; template <typename A, typename B> ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } typedef long long int64; typedef pair<int, int> ii; #define SZ(x) (int)((x).size()) template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2; const int MOD = 1e9 + 7; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; typedef complex<double> point; int main() { int n; cin >> n; point u, v; double x, y; cin >> x >> y; u = {x, y}; cin >> x >> y; v = {x, y}; point c = (u + v) / 2.0; u -= c; v -= c; double theta = 2 * M_PI / n; point ret = point{cos(theta), sin(theta)} * u + c; cout << ret.real() << " " << ret.imag() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ull=unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) ll GCD(ll a,ll b){ return b?GCD(b,a%b):a; } ll A,B; int E[73][73]; int pN; int I[38]; ll G[38]; int dp1[1<<19], dp2[1<<19]; int dp3[1<<19]; ll solve(){ rep(i,pN) G[i]=0; rep(i,pN) rep(j,pN) if(!E[I[i]][I[j]]) G[i]|=(1ll<<j); int N1=(pN+1)/2, N2=pN/2; ll N1mask = (1ll<<N1)-1; rep(i,1<<N1) dp1[i]=0; rep(i,1<<N2) dp2[i]=0; dp1[0]=dp2[0]=1; rep(d,N1) rep(p,1<<N1) if(!(p&(1<<d))) if(!(p&G[d])) dp1[p|(1<<d)]+=dp1[p]; rep(d,N2) rep(p,1<<N2) if(!(p&(1<<d))) if(!(p&(G[d+N1]>>N1))) dp2[p|(1<<d)]+=dp2[p]; rep(d,N2) rep(p,1<<N2) if(!(p&(1<<d))) dp2[p|(1<<d)]+=dp2[p]; ll ans=0; rep(p,1<<N1){ int mask = (1ll<<N2)-1; rep(d,N1) if(p&(1ll<<d)) mask &= ~(G[d]>>N1); dp3[p] = mask; ans += dp1[p]*dp2[mask]; } return ans; } ll solve2(int x){ int N1=(pN+1)/2, N2=pN/2; ll N1mask = (1ll<<N1)-1; ll allMask = 0; rep(d,pN) if(!E[I[d]][x]) allMask |= (1ll<<d); ll ans=0; rep(p,1<<N1) if(!(p&allMask)){ ll mask = dp3[p] & ~(allMask>>N1); ans += dp1[p]*dp2[mask]; } return ans; } int main(){ cin>>A>>B; int N = B-A+1; rep(i,N) rep(j,N) E[i][j]=((GCD(A+i,A+j)==1)?1:0); pN=0; rep(i,N) if((A+i)%2==1) I[pN++]=i; ll ans = solve(); rep(i,N) if((A+i)%2==0) ans += solve2(i); cout<<ans<<endl; return 0; }
#include <cstdio> #include <cassert> long long A,B,ans,tmp; long long a[105]; long long g[105][105]; bool fp[105]; long long gcd(long long a,long long b) { if(b==0) return a; return gcd(b,a%b); } void dfs(long long now,int p) { if(now==B+1) { ans++; return; } dfs(now+1,p); if(!fp[now-A]) { for(int i=0;i<p;i++) { if(g[now-A][a[i]-A]!=1) return; } a[p]=now; dfs(now+1,p+1); } } int main() { scanf("%lld%lld",&A,&B); for(long long i=A;i<=B;i++) for(long long j=A;j<=B;j++) g[i-A][j-A]=gcd(i,j); for(long long i=A;i<=B;i++) { fp[i-A]=true; for(long long j=A;j<=B;j++) if(i!=j&&g[i-A][j-A]!=1) { fp[i-A]=false; break; } } tmp=1; for(long long i=A;i<=B;i++) if(fp[i-A]) tmp*=2; dfs(A,0); printf("%lld\n",ans*tmp); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f; const double pi=3.1415926535897932384626; 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^48); ch=getchar(); } return x*f; } const int maxn=2e5+5; ll k,ans,a[maxn],sum[maxn]; int main(){ k=read(); for(int i=1;i<=k;++i){ int x=sqrt(i); for(int j=1;j<=x;++j){ if(i%j==0){ ++a[i]; if(i/j!=j) ++a[i]; } } } for(int i=1;i<=k;++i) sum[i]=sum[i-1]+a[i]; for(int i=1;i<=k;++i) ans+=sum[k/i]; printf("%lld\n",ans); return 0; }
#include <iostream> using namespace std; int main(){ int i,j,k,x,cnt = 0; cin >> x; for(i=1;i<=x;i++){ for(j=1;j<=x;j++){ if(i*j>x) break; for(k=1;k<=x;k++){ if(i*j*k>x) break; cnt++; } } } cout << cnt << endl; }
#include<bits/stdc++.h> using namespace std; #define int long long #define TEST int t; cin>>t; while(t--) #define pb push_back #define pii pair <int, int> #define vi vector <int> #define ff first #define ss second #define vpii vector <pair <int, int>> #define vvi vector <vector <int> > #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define endl "\n" #define MAX 1e18 #define MIN INT_MIN #define mod 1000000007 int lcm(int a, int b) {return (a * b) / (__gcd(a, b));} //----------------------------------SEIVE---------------------------------------------// vector<int>prime; bitset<1000030>vis; void seive() { vis.set(); vis[0] = false; vis[1] = false; vis[2] = true; prime.pb(2); for (int i = 3; i <= 1000030; i += 2) if (vis[i] == 1) { prime.pb(i); for (int j = i * i; j <= 1000030; j += i) { vis[j] = 0; } } } //----------------------------------SEIVE---------------------------------------------// // bool isPrime(int n) // { // if (n < 2) return false; // if (n < 4) return true; // if (!(n & 1)) return false; // if (n % 3 == 0) return false; // int lim = sqrt(n); // for (int i = 5; i <= lim; i += 6) // if (n % i == 0 || n % (i + 2) == 0) // return false; // return true; // } // map<int, int> primeFact(int n) { // map<int, int> mp; // while (n % 2 == 0) { // mp[2]++; // n = n / 2; // } // for (int i = 3; i <= sqrt(n); i += 2) { // while (n % i == 0) { // mp[i]++; // n = n / i; // } // } // if (n > 2) { // mp[n]++; // } // return mp; // } // int binomialCoeff(int n, int k) // { // if (k > n)return 0; // int C[n + 1][k + 1]; // int i, j; // for (i = 0; i <= n; i++) { // for (j = 0; j <= min(i, k); j++) { // if (j == 0 || j == i) // C[i][j] = 1; // else // C[i][j] = (C[i - 1][j - 1] % (mod) + C[i - 1][j] % (mod)) % (mod); // } // } // return C[n][k]; // } // int countDivisors(int n) { // int cnt = 0; // for (int i = 1; i <= sqrt(n); i++) { // if (n % i == 0) { // if (n / i == i)cnt++; // else { // cnt += 2; // } // } // } // return cnt; // } // int reverse_number(int n) { // int rev = 0, r; // while (n != 0) { // r = n % 10; // rev = rev * 10 + r; // n /= 10; // } // return rev; // } // int perfectSquare(int n) { // int h = n & 0xF; // if (h > 9)return 0; // if (h != 2 && h != 3 && h != 5 && h != 6 && h != 7 && h != 8) { // int x = (int)floor(sqrt((double)n) + 0.5); // return x * x == n; // } // return 0; // } void solve() { int n; cin >> n; int m; cin >> m; int c1 = 0; int o = 0; for (int i = 0; i < n; i++) { string s; cin >> s; c1 = 0; for (auto p : s) { if (p == '1')c1++; } if (c1 & 1)o++; } cout << o*(n - o) << endl; } signed main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count()); ios_base :: sync_with_stdio(0); cin.tie(0); //seive(); //TEST solve(); }
#define nl "\n" #define ll long long #define ull unsigned long long #define pb push_back #define SIZE(a) (int)a.size() #define SORT(v) sort(v.begin(),v.end()) #define RSORT(v) sort(v.rbegin(),v.rend()) #define REV(v) reverse(v.begin(),v.end()) #define ff first #define ss second #define sq(a) ((a)*(a)) #define For(i,a,b) for(i=a;i<=b;i++) #define Rof(i,a,b) for(i=a;i>=b;i--) #define Rep(i,b) for(i=0;i<b;i++) #define MOD 1000000007 #define PI acos(-1.0) #define eps 1e-9 #define Linf 2e18 #define inf 1<<30 #define MX5 100005 #define MX6 1000006 #define MX3 1005 #define GCD(a,b) __gcd(a,b) #define Abs(a) abs(a) #define input(a,b) scanf("%lld%lld",&a,&b) #define in1(a) scanf("%lld",&a) #define output(a) printf("%lld\n",a); #define mem(a) memset(a,-1,sizeof(a)) // complexity O(n) #define clr(a) memset(a,0,sizeof (a)) #define mk make_pair #define pLL pair<ll,ll> #define invcos(a) (acos(a)*(180/3.14159265)) #define rtan(a) (tan(a*3.14159265/180.0)) #define pip printf("pip") #define Case(x) printf("Case %lld: ",x) #define timelimit 1.0*clock()/CLOCKS_PER_SEC #define pc_one __builtin_popcount #define pcl_one __builtin_popcountl #define pcll_one __builtin_popcountll #define parity __builtin_parity #define parityl __builtin_parityl #define parityll __builtin_parityll #define infile freopen("input.txt","r",stdin) #define outfile freopen("output.txt","w",stdout) #define debug(x) cerr << #x << " is " << x << endl #define fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0); //const ll Linf=2e18; ///4direction -> int del_x[]={-1,0,1,0},del_y[]={0,1,0,-1}; ///8direction -> int del_x[]={-1,0,1,0,1,-1,-1,1},del_y[]={0,1,0,-1,-1,-1,1,1}; #include <bits/stdc++.h> /*#include <iomanip> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp>*/ using namespace std; /*using namespace __gnu_pbds; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; */ ll bigmod(ll n, ll p) {if (p == 0) return 1; if (p == 1)return (n + MOD) % MOD; if (p % 2)return (bigmod(n, p - 1) * n + MOD) % MOD; else {ll x = bigmod(n, p / 2); return (x * x + MOD) % MOD;}} ll modinverse(ll n) {return bigmod(n, MOD - 2) % MOD;} bool check_2s_power(ll a) {return (pcll_one(a) == 1 ? true : false);} ll check_Bit(ll n,ll k){ if(n & (1LL << k)) return true; return false; } ll setBit(ll n, ll k){ return (n | (1LL << k)); } ll clearBit(ll n,ll k){ return (n & (~(1LL << k))); } ll toggleBit(ll n,ll k){ return (n ^ (1LL << k)); } /*----------------------Graph Moves----------------*/ //const int fx[]={+1,-1,+0,+0}; //const int fy[]={+0,+0,+1,-1}; //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move /*------------------------------------------------*/ int main() { ll i,j,n,m,milse=0,milenai=0; string s,p; input(n,m); cin >> s; n--; vector<string>v(n); Rep(i,n) { cin >> p; ll counter=0; Rep(j,m) { if(s[j]!=p[j]) counter++; } //cout << counter << endl; if(counter%2) milenai++; } milse=n+1-milenai; output(milse*milenai); }
#include<iostream> #include<string> #include<vector> #include<set> #include<functional> #include<map> #include<cmath> #include <iterator> #include<algorithm> #include<queue> #include<cstdlib> #include<stdio.h> #include<assert.h> #include<sstream> #define ff first #define ss second #define rep(i,a,b) for(i=a;i<b;i++) #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define pb push_back #define pf pop_front #define sz(x) (int)((x).size()) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define endl "\n" using namespace std; #define MOD 1000000007 #define vl vector<ll> #define vpl vector<pair<ll,ll>> typedef long long int ll; template<typename T, typename T1>T amax(T& a, T1 b) { if (b > a)a = b; return a; } template<typename T, typename T1>T amin(T& a, T1 b) { if (b < a)a = b; return a; } /*void binpow(long long a,long long n) { int res = 1; while (n) { if (n & 1) { res = (res * a )%MOD; n--; } else { a = (a * a) % MOD; n = n / 2; } } cout << res<< endl; }*/ int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ll x; ll y; cin >> x >> y; cout << (abs(y - x )< 3 ? "Yes" : "No"); }
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; //cin>>t; while(t--){ long long k; cin>>k; long long n; cin>>n; vector<long long> all; long long cur =0; for(long long i=1;i<=100;i++){ cur+=k; if(cur>=100){ all.push_back(i); cur-=100; } } long long es = n/all.size(); //cout<<es<<endl; cur=(100*es); long long rem=n-es*all.size(); if(rem!=0) cur+=all[rem-1]; //cout<<cur<<endl; //cout<<(100+k)*cur<<endl; long long num = ((100+k)*cur)/100 -1; cout<<num<<endl; } return 0; }
#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #define rep(i,n) for(ll i=0;i<(n);++i) #define rep2(i,n) for(ll i=1;i<=(n);++i) #define rep3(i,i0,n) for(ll i=i0;i<(n);++i) #define rrep(i,n) for(ll i=((n)-1); i>=0; --i) #define rrep2(i,n) for(ll i=(n); i>0; --i) #define pb push_back #define mod 998244353 #define fi first #define se second #define len(x) ((ll)(x).size()) using namespace std; using ll = long long; using ld = long double; using Pi = pair< ll, ll >; using vl = vector<ll>; using vc = vector<char>; using vb = vector<bool>; using vs = vector<string>; using vp = vector<Pi>; using vvc = vector<vector<char>>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; const ll INF = 1LL << 60; const ld PI = 3.1415926535897932385; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) {return a/gcd(a,b)*b;} #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define mp make_pair void printb(ll N,ll d=16){ rep(i,d){ cout<<(N/(1<<(d-i-1)))%2; } cout<<endl; } void printv(vector<ll>a){ rep(i,a.size()){ if(i==a.size()-1){ cout<<a[i]<<endl; }else{ cout<<a[i]<<" "; } } } bool In_map(ll y,ll x,ll h,ll w){ if(y<0 || x<0 || y>=h || x>=w){ return 0; }else{ return 1; } } bool compare(Pi a, Pi b) { if(a.first != b.first){ return a.first < b.first; }else{ return a.second < b.second; } } //const vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1}; //const vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1}; const vector<ll> dx{1,0,-1,0}; const vector<ll> dy{0,1,0,-1}; template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint<mod>; using vm = vector<mint>; using vvm = vector<vector<mint>>; int main() { ll H,W; cin>>H>>W; vs S(H); rep(i,H){ cin>>S[i]; } mint ans=1; rep(i,H+W-1){ ll rf=0,bf=0; rep(j,H){ if(i-j<0 || i-j>=W)continue; if(S[j][i-j]=='R'){ rf=1; }else if(S[j][i-j]=='B'){ bf=1; } } ans*=(2-rf-bf); } cout<<ans.a<<endl; return 0; }
#include <bits/stdc++.h> #ifdef LOCAL #include "prettyprint.hpp" #else #define deb(...) #endif using namespace std; using ll = long long; const int mod = 998244353; int main() { cin.tie(0)->sync_with_stdio(0); // cout << fixed << setprecision(15); int h, w; cin >> h >> w; vector<vector<char>> g(h, vector<char>(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> g[i][j]; } } vector<vector<char>> diagnol(2 * max(h, w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { diagnol[i+j].push_back(g[i][j]); } } ll sum = 1; for (auto i : diagnol) { bool onlydots = true; bool red = false; bool blue = false; int dotcount = 0; for (auto j: i) { if (j == '.') dotcount++; if (j == 'R') { onlydots = false; red = true; } if (j == 'B') { onlydots = false; blue = true; } } if (red && blue) { sum *= 0; break; } if (dotcount == 0) { continue; } if (red || blue) { sum *= 1; continue; } sum = sum * 2; sum %= mod; } cout << sum << "\n"; return 0; }
#include <cstdio> #include <algorithm> int A[2001][2001]; int dp[2001][2001]; int main(){ int h, w; scanf("%d%d", &h, &w); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ char c; do { scanf("%c", &c); } while(c != '+' && c != '-'); A[i][j] = c == '+' ? 1 : -1; } } for(int i = h-1; i >= 0; i--){ for(int j = w-1; j >= 0; j--){ if(i == h-1 && j == w-1) continue; dp[i][j] = -1000000000; if(i < h-1) dp[i][j] = -dp[i+1][j]+A[i+1][j]; if(j < w-1) dp[i][j] = std::max(dp[i][j], -dp[i][j+1]+A[i][j+1]); } } if(dp[0][0] == 0) puts("Draw"); else if(dp[0][0] > 0) puts("Takahashi"); else puts("Aoki"); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long llo; #define mp make_pair #define pb push_back #define a first #define b second #define endl '\n' llo aa[100001]; llo bb[100001]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); llo n,l; cin>>n>>l; llo ans=0; for(llo i=0;i<n;i++){ cin>>aa[i]; } for(llo i=0;i<n;i++){ cin>>bb[i]; } vector<llo> cc; cc.pb(aa[0]-1); for(llo i=1;i<n;i++){ cc.pb(aa[i]-aa[i-1]-1); } cc.pb(l-aa[n-1]); vector<llo> dd; dd.pb(bb[0]-1); for(llo i=1;i<n;i++){ dd.pb(bb[i]-bb[i-1]-1); } dd.pb(l-bb[n-1]); llo ind=0; /*for(auto j:cc){ cout<<j<<","; } cout<<endl; for(auto j:dd){ cout<<j<<","; } cout<<endl;*/ for(llo i=0;i<=n;i++){ if(dd[i]==0){ continue; } llo su=0; llo l=ind; while(su<dd[i] and ind<=n){ su+=cc[ind]; ind++; } llo r=ind-1; if(su!=dd[i]){ cout<<-1<<endl; return 0; } while(l<r){ if(cc[l]==0){ l+=1; } else{ break; } } while(r>l){ if(cc[r]==0){ r-=1; } else{ break; } } ind=r+1; // cout<<i<<":"<<l<<":"<<r<<endl; ans+=(max(r-i,(llo)0)+max(i-l,(llo)0)); } cout<<ans<<endl; return 0; }
#include<iostream> #include<string> #include<algorithm> using namespace std; int g1(string s) { sort(s.begin(), s.end(), greater<char>()); return stoi(s); } int g2(string s) { sort(s.begin(), s.end()); return stoi(s); } int main() { string n; int ak; cin>>n>>ak; int size = ak+1; string dp[size]{}; dp[0] = n; for(int k = 1; k < size; k++) { dp[k] = to_string(g1(dp[k-1]) - g2(dp[k-1])); if(dp[k] == dp[k-1]) { k++; for(;k < size; k++) dp[k] = dp[k-1]; } } if(ak == 0) cout<<dp[0]; else { cout<<dp[ak]; } }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define PI (acos(-1.0)) #define EPS 1e-9 #define INF 0x3f3f3f //#define int ll //int:4e10 ll:9e18 ull:1e20 __int128:3e38 typedef long long ll; typedef unsigned long long ull; typedef double db; typedef __int128 lll; const int maxn = 100010; const ll mod = 1e9 + 7; int d[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}}; template <typename T> inline T read() { // 声明 template 类,要求提供输入的类型T,并以此类型定义内联函数 read() T sum = 0, fl = 1; // 将 sum,fl 和 ch 以输入的类型定义 int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0'; return sum * fl; } void solve(){ } int main(){ IOS; double oldp, newp; cin >> oldp >> newp; cout << ((oldp - newp) / oldp) * 100; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; const int mod = 1000000007; struct mint { ll x; mint(ll x=0):x(x%mod){} mint operator-() const {return mint(-x);} mint& operator+=(const mint a) { if((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const{ mint res(*this); return res+=a; } mint operator-(const mint a) const{ mint res(*this); return res-=a; } mint operator*(const mint a) const{ mint res(*this); return res*=a; } mint pow(ll t) const{ if(!t) return 1; mint a = pow(t>>1); a *= a; if(t&1) a*= *this; return a; } // prime mod mint inv() const {return pow(mod-2);} mint& operator/=(const mint a){return (*this) *= a.inv();} mint operator/(const mint a) const{ mint res(*this); return res/=a; } }; istream& operator>>(istream& is, mint& a){return is >> a.x;} ostream& operator<<(ostream& os, const mint& a){return os << a.x;} mint f(int n){ if(n < 0) return 0; return mint(n)*(n+1)/2; } mint c(int n){ if(n < 0) return 0; return f(n) * f(n); } int main(){ ll t; cin >> t; rep(i,t){ int n, a, b; cin >> n >> a >> b; mint x = f(n-a-b+1)*(n-b+1)*(n-a+1); mint y = c(n-a-b+1); cout << x * 4 - y * 4 << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1e9+7; ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;} int main(){ int t; cin>>t; while(t--){ ll a,b,c,ans=0; cin>>a>>b>>c; if(a>=b+c){ ll res=(a-b-c+1)*(a-b-c+2)%mod; ll num=2*(a-b+1)*(a-c+1)%mod-res; if(num<0) num=(mod-(-num)%mod)%mod; ans=res*num%mod; } cout<<ans<<endl; } }
#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() { ll n; cin >> n; vector<vector<int>> seen(n + 1, vector<int>(500, 0)); vector<ll> ans; for (int i = 1; i <= n; i++) { for (int j = 1; j < 500; j++) { if (seen[i][j] == 0) { ans.push_back(j); break; } } for (int j = i; j <= n; j += i) { seen[j][*ans.rbegin()] = 1; } } rep(i, n) cout << ans[i] << " "; cout << endl; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; int main() { ios_base :: sync_with_stdio(false); cin.tie(NULL); int t = 1; //cin >> t; while(t--) { int n; cin >> n; ll ans = 16ll * 25ll * 27ll * 7ll * 11ll * 13ll * 17ll * 19ll * 23ll * 29ll + 1; cout << ans << "\n"; } return 0; }
#include<algorithm> #include<bitset> #include<cmath> #include<complex> #include<deque> #include<functional> #include<iomanip> #include<iostream> #include<map> #include<numeric> #include<queue> #include<set> #include<stack> #include<string> #include<unordered_map> #include<unordered_set> #include<utility> #include<vector> using namespace std; typedef long long ll; #define REP(i, n) for(ll i=0;i<(ll)(n);i++) #define RREP(i, n) for(ll i=(ll)(n-1);i>=0;i--) #define FOR(i, a, b) for(ll i=(ll)(a); i<=(ll)(b); i++) #define RFOR(i, a, b) for(ll i=(ll)(a);i>=(ll)(b); i--) #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((ll)(x).size()) #define INF 1000000000000 #define MOD 1000000007 // 10^9+7 #define PB push_back #define MP make_pair #define F first #define S second ll binsearch(ll d, ll M, vector<int> X){ ll mind = d+1; ll maxd = 1000000000000000001; { ll num=0; for(auto c : X){ //if(M/(double)r - c< num/(double)r ){ if((M-c)/(double)mind<num){ return 0; } num *= mind; num += c; } } while(true){ loop: ll newd = (mind+maxd)/2; ll num = 0; for(auto c : X){ //if(M/(double)c - r < num/(double)c){ if((M-c)/(double)newd<num){ maxd = newd; goto loop; } num *= newd; num += c; } if(mind == newd)break; mind = newd; } return mind - d; } signed main() { string X; ll M; cin >> X; cin >> M; if(X.size() == 1){ if(X[0]-'0'>M){ cout << 0; }else{ cout << 1; } cout << endl; return 0; } int d = 0; vector<int> vecX; for(char c : X){ vecX.PB(c-'0'); if(d < c-'0'){ d = c-'0'; } } cout << binsearch(d, M, vecX) << endl; }
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <map> #include <set> #include <queue> #include <stack> #include <list> using namespace std; #define PB push_back #define MP make_pair #define REP(i,n) for(i=0;i<(n);++i) #define PER(i,n) for(i=(n - 1);i>=0;--i) #define FOR(i,l,h) for(i=(l);i<=(h);++i) #define FORD(i,h,l) for(i=(h);i>=(l);--i) #define mod 1000000007 #define maxInt 2147483647 #define minInt -2147483648 #define maxLL 9223372036854775807 #define minLL -9223372036854775807 typedef vector<int> VI; typedef vector<string> VS; typedef vector<double> VD; typedef long long LL; typedef unsigned long long ULL; typedef __int128 l128; typedef pair<int,int> PII; typedef map<int, int> MII; int check(ULL n, string x, LL m) { l128 res = 0; for(int i = 0; i < x.size(); i++) { res = res * n + l128(x[i] - '0'); if(res > m) return 0; } return 1; } int main() { string x; ULL m, i; while(cin>>x) { cin>>m; if(x.size() == 1) { if(x[0] - '0' - 1 <= m) cout<<1<<endl; else cout<<0<<endl; } else{ int maxdig = 0; REP(i, x.size()) maxdig = max(maxdig, x[i] - '0'); ULL l = 0, r = 1e18, mid; while(l < r) { mid = (l + r +1)/2; if(check(mid,x, m) == 1) l = mid; else r = mid -1; } if(l <= maxdig ) cout<<0<<endl; else cout<<l - maxdig<<endl; } } return 0; }
#include <iostream> #include <bits/stdc++.h> #define pb push_back #define ll long long int using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin>>s; ll n=s.length(),c=0,li=n; char pri='0'; for(ll i=n-2;i>=0;i--) { if(s[i]==s[i+1] && s[i]!=s[i+2]) { char o=s[i]; for(ll j=i+2;j<li;j++) if(s[j]!=o) c++; if(s[i]!=pri) c+=n-li; pri=s[i]; li=i; } } cout<<c<<"\n"; }
#include <bits/stdc++.h> #define rep(i,n) for(ll i=0;i<n;++i) #define rrep(i, n) for(ll i=n-1;i>=0;--i) #define rep1(i, n) for(ll i=1; i<=n; ++i) #define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr) #define ALL(a) (a).begin(),(a).end() template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}} template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}} using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using pll = pair<ll, ll>; const ll MOD = 998244353; const ll LINF = 1LL << 60; const int INF = 1e9 + 7; const double PI = 3.1415926535897932384626; ll mod(ll a, ll m){return (a + m) % m;} int main(){ string s; cin >> s; reverse(ALL(s)); vector<ll> cnt(26, 0); ll ans = 0; rep(i, (int)s.size()){ if(i && s[i-1] == s[i]){ rep(j, 26)if(j != s[i] - 'a'){ ans += cnt[j]; cnt[j] = 0; } cnt[s[i] - 'a'] = i+1; } else cnt[s[i]-'a']++; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long const int N = 2e5+4; ll seg[4*N], lazy[4*N]; vector<int> g[N]; int in[N], out[N], timer = 0; void propagate(int node, int l, int r) { if(lazy[node]==0) return; seg[node] += lazy[node] * (r-l+1); if(l != r) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } void update(int node, int l, int r, int s, int e, ll val) { propagate(node, l, r); if(e < l || r < s) return; if(s <= l && r <= e) { lazy[node] += val; propagate(node, l, r); return; } int mid = (l+r) >> 1; update(node << 1, l, mid, s, e, val); update(node << 1|1, mid+1, r, s, e, val); seg[node] = seg[node << 1] + seg[node << 1 | 1]; } ll query(int node, int l, int r, int idx) { propagate(node, l, r); if(idx < l || idx > r) return 0; if(l == r) return seg[node]; int mid = (l+r) >>1; return query(node << 1, l, mid, idx) + query(node<<1|1, mid+1, r, idx); } void dfs(int node, int parent) { in[node] = ++timer; for(auto v : g[node]) { if(v == parent) continue; dfs(v, node); } out[node] = timer; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<int, int>> edge; for (int i = 0; i < n-1; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); edge.emplace_back(x,y); } dfs(1, -1); int q; cin >> q; while(q--) { int op, e, x; cin >> op >> e >> x, e--; int a = edge[e].first, b = edge[e].second; if(op == 2) swap(a, b); if(in[a] < in[b]) { update(1, 1, n, 1, n, x); update(1, 1, n, in[b], out[b], -x); } else update(1, 1, n, in[a], out[a], x); } for(int i = 1; i <= n; i++) cout << query(1, 1, n, in[i]) << endl; return 0; }
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" using namespace __gnu_pbds; using namespace std; #define F first #define S second #define pb push_back #define eb emplace_back #define vi vector<int> #define vpi vector<pi> #define fr(i, a, b) \ for (auto i = (a); i != (b); i++) typedef long long ll; typedef pair<int, int> pi; template <class T> class fenwick { int size; vector<T> arr, _arr, act; public: fenwick(int N) { size = N; arr.resize(N + 1, 0); act.resize(N + 1, 0); _arr.resize(N + 1, 0); } void add(int k, T x) { while (k <= size) { act[k] += x; k += k & -k; } } void _add(int k, T x) { while (k <= size) { arr[k] += x; k += k & -k; } } void __add(int k, T x) { while (k <= size) { _arr[k] += x; k += k & -k; } } T sum(int k) { int temp = k; T s = 0, t = 0; while (k >= 1) { s += act[k]; k -= k & -k; } k = temp; while (k >= 1) { t += arr[k]; k -= k & -k; } t *= temp; k = temp; while (k >= 1) { t -= _arr[k]; k -= k & -k; } return s + t; } void range_add(int l, int r, T val) { _add(l, val); _add(r + 1, -val); __add(l, val * (l - 1)); __add(r + 1, -val * r); } }; int dfs(int node, vector<bool> &vis, vi &s, vector<vi> &a, vi &tra, vi &p) { tra.eb(node); int size = 1; vis[node] = true; for (int u : a[node]) { if (!vis[u]) { size += dfs(u, vis, s, a, tra, p); p[u] = node; } } s[node] = size; return size; } const ll mod = 1e9 + 7; int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vpi a(n - 1); vector<vi> adj(n); fr(i, 0, n - 1) { cin >> a[i].F >> a[i].S; adj[a[i].S - 1].pb(a[i].F - 1); adj[a[i].F - 1].pb(a[i].S - 1); a[i].F--; a[i].S--; } vi s(n), p(n, -1); vi tra; vector<bool> vis(n, false); dfs(0, vis, s, adj, tra, p); fenwick<ll> f(n); vi ac(n); for (int i = 0; i < n; i++) { ac[tra[i]] = i + 1; } int q; cin >> q; for (int i = 0; i < q; i++) { ll t, e, x; cin >> t >> e >> x; e--; if (t == 1) { int node = a[e].F; if (p[node] == a[e].S) { f.range_add(ac[node], ac[node] + s[node] - 1, x); } else { f.range_add(1, n, x); f.range_add(ac[a[e].S], ac[a[e].S] + s[a[e].S] - 1, -x); } } else { int node = a[e].S; if (p[node] == a[e].F) { f.range_add(ac[node], ac[node] + s[node] - 1, x); } else { f.range_add(1, n, x); f.range_add(ac[a[e].F], ac[a[e].F] + s[a[e].F] - 1, -x); } } } for (int i = 0; i < n; i++) { ll answer = f.sum(ac[i]) - f.sum(ac[i] - 1); cout << answer << "\n"; } 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 rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; // typedef tree< // int, // null_type, // less<int>, // rb_tree_tag, // tree_order_statistics_node_update> // ordered_set; int n, m, x, y; map<int,vector<int>> black; int main() { cin >> n >> m; rep(i,m) { cin >> x >> y; black[x].push_back(y); } set<int> possible{n}; for (auto [x, ys] : black) { vector<int> add; for (auto y : ys) { if (possible.count(y+1)) add.push_back(y); if (possible.count(y-1)) add.push_back(y); } for (auto y : ys) possible.erase(y); for (auto y : add) possible.insert(y); } cout << possible.size() << "\n"; }
#include <bits/stdc++.h> #include <bits/extc++.h> #define double long double #define rbtree __gnu_pbds::tree<int,__gnu_pbds::null_type,less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update> #define int ll #define IOS ios_base::sync_with_stdio(false);cin.tie(0); #define pb push_back #define ALL(X) X.begin(),X.end() #define F(i,n) FF(i,0,n) #define F1(i,n) FF(i,1,n+1) #define FF(i,n,m) for(int i=(int)n;i<(int)m;++i) #ifndef LOCAL //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #else #endif // LOCOL //#define mp make_pair using namespace std; //using namespace __gnu_pbds; template<typename T> bool remax(T& a, const T& b) {return b>a?a=b,1:0;} template<typename T> bool remin(T& a, const T& b) {return b<a?a=b,1:0;} inline ostream& operator << (ostream& os,__int128_t a){if(a==0) {return (os<<'0');}bool flag=0;if(a<0) {a=-a,flag=1;}string s;while(a){s+=a%10+'0';a/=10;}s+=flag?"-":"";reverse(ALL(s));return os<<s;} inline istream& operator >>(istream& is,__int128_t& a){string s;cin>>s;a=0;for(auto c:s) a=a*__int128_t(10)+__int128_t(c-'0');return is;} template<typename T,typename P> inline ostream& operator << (ostream& os,pair<T,P> a){os<<a.first<<" "<<a.second; return os;} template<typename T,typename P> inline istream& operator >> (istream& is,pair<T,P> &a){is>>a.first>>a.second; return is;} using ll=long long; using ull=unsigned long long; using int128= __int128_t; using uint128= __uint128_t; using pii =pair<int,int>; const double pi=acos(-1); const int N=800+5; const ll M=1000000000; const ll INF_64=0x3f3f3f3f3f3f3f3f; const int INF_32=0x3f3f3f3f; const int16_t INF_16=0x3f3f; const int klog=20; const int mod=998244353;//1E9+7 const double eps=1E-8; void gen(){ } void sol(){ int n,m; cin>>n>>m; vector<pii> a(m); F(i,m){ cin>>a[i].first>>a[i].second; } sort(ALL(a),[](auto&l,auto&r){return l.first<r.first;}); map<int,int> dp; dp[n]=1; vector<pii> upd; F(t,m){ if(t!=0&&a[t].first!=a[t-1].first){ while(upd.size()){ auto p=upd.back();upd.pop_back(); dp[p.first]=p.second; } } int x=a[t].first,y=a[t].second; int tmp=0; if(y-1>=0) tmp+=dp[y-1]; if(y+1<=2*n) tmp+=dp[y+1]; upd.pb({y,tmp>0?1:0}); } for(auto &p:upd){ dp[p.first]=p.second; } upd.clear(); int an=0; for(auto &[x,y]:dp) if(y!=0) an++; cout<<an<<"\n"; } int32_t main(){ #ifdef LOCAL //freopen(".in","r",stdin); //freopen(".out","w",stdout); #endif // LOCAL IOS; int t=1; gen(); //cin>>t; FF(i,1,t+1){ //cout<<"Case #"<<i<<": "; sol(); } }
#include<bits/stdc++.h> using namespace std; #define ll long long #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define REP(i,n) for(ll i=0;i<(n);i++) #define FOR(i,a,b) for(ll i=(a);i<(b);i++) #define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c)) #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) #define ff first #define ss second #define dd long double #define all(x) x.begin(),x.end() template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "["; for (ll i=0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << "]"; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v){ os << "{"; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "}"; return os; } template <typename T, typename S> ostream& operator<<(ostream& os, const map<T, S>& v){ os<<"{"; for (auto it : v){ os << it.first << " : " << it.second ; if (it != *v.rbegin()) os << ", "; } os << "}"; return os; } template <typename T, typename S> ostream& operator<<(ostream& os, const pair<T, S>& v){ os << "("; os << v.first << ", " << v.second << ")"; return os; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--){ ll n; cin>>n; vector<ll> arr(n); REP(i,n){ cin>>arr[i]; arr[i]--; } // cout<<arr<<endl; if(n==2){ if(arr[0]==0){ cout<<0<<endl; } else{ cout<<1<<endl; cout<<1<<endl; } continue; } ll turn=0; vector<ll> ans; for(ll elem=n-1;elem>2;elem--){ ll pos; for(ll i=0;i<=elem;i++){ if(arr[i]==elem){ pos=i; break; } } if(pos==elem) continue; if((pos & 1)==turn){ for(ll i=pos;i<elem;i++){ ans.pb(i); swap(arr[i],arr[i+1]); turn^=1; } } else{ ll ideal=-1; for(ll j=0;j<elem;j++){ if((j & 1)==turn){ if(j+1!=pos){ ideal=j; break; } } } if(ideal!=-1){ ans.pb(ideal); swap(arr[ideal],arr[ideal+1]); turn^=1; for(ll i=pos;i<elem;i++){ ans.pb(i); swap(arr[i],arr[i+1]); turn^=1; } } else{ ans.pb(pos-1); swap(arr[pos-1],arr[pos]); turn^=1; ans.pb(pos); swap(arr[pos],arr[pos+1]); turn^=1; for(ll i=pos-1;i<elem;i++){ ans.pb(i); swap(arr[i],arr[i+1]); turn^=1; } } } } // cout<<arr<<endl; // cout<<turn<<endl; vector<ll> aux(arr.begin(),arr.begin()+3); vector<ll> tar{0,1,2}; while(aux!=tar){ if(turn & 1){ ans.pb(1); swap(aux[1],aux[2]); turn^=1; } else{ ans.pb(0); swap(aux[0],aux[1]); turn^=1; } } cout<<ans.size()<<endl; for(auto & x:ans){ cout<<x+1<<" "; } cout<<endl; } }
#include <bits/stdc++.h> #define ALL(v) v.begin(), v.end() #define MAX 510000 #define rrep(i, n) for(ll i = 0; i < (ll)(n); i++) #define rep(i, n) for(ll i = 1; i <= (ll)(n); i++) #define dcout cout<<fixed<<setprecision(15); #define mp make_pair #define pb push_back using namespace std; typedef long long int ll; typedef pair<ll, ll> P; constexpr int MOD = 1e9 + 7; constexpr ll inf = 1LL << 60; template< typename S, typename T > inline void chmax(S &a, const T &b) { if(a < b) a = b; } template< typename S, typename T > inline void chmin(S &a, const T &b) { if(a > b) a = b; } ll gcd(ll x, ll y) { if (x == 0) return y; return gcd(y%x, x);} ll lcm(ll x, ll y) { return x * y / gcd(x, y); } ll modpow(ll a, ll n, ll p) { if(n==0) return (ll)1; if (n == 1) return a % p; if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p; ll t = modpow(a, n / 2, p); return (t * t) % p;} ll modinv(ll a, ll m) { if(m==0)return (ll)1; ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u;} ll fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; }} ll COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;} ll com(ll n,ll m){ if(n<m || n<=0 ||m<0){ return 0; } if( m==0 || n==m){ return 1; } ll k=1; for(ll i=1;i<=m;i++){ k*=(n-i+1); k%=MOD; k*=modinv(i,MOD); k%=MOD; } return k;} ll rad(ll u, ll p){ ll cnt=0; while(u%p==0){ u/=p; cnt++; } return cnt;} //////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); ll tt; cin>>tt; ll num[550]; ll dis[550]; for(int i=0;i<=501;i++){ num[i]=i; } for(int i=0;i<=500;i++){ dis[i]=i; } rep(qq,tt){ ll cnt=0; ll n; cin>>n; vector<ll> v; rep(i,n){ ll t; cin>>t; v.pb(t); } if(n==1){cout<<0<<endl; continue;} if(n==2){ if(v[0]==1){cout<<0<<endl;} else{ cout<<1<<endl; cout<<1<<endl; } continue; } for(ll i=0;i<n;i++){ num[i+1]=v[i]; dis[v[i]]=i+1; } vector<ll> ans; ll fin=0; for(ll i=1;i<=n;i++){ if(num[i]==i && i!=n-2)continue; if(i==n-2){ while(fin==0){ cnt++; if(cnt%2==n%2){ ll u=num[n-2]; dis[num[n-1]]=n-2; dis[num[n-2]]=n-1; num[n-2]=num[n-1]; num[n-1]=u; ans.pb(n-2); } else{ ll u=num[n-1]; dis[num[n-1]]=n; dis[num[n]]=n-1; num[n-1]=num[n]; num[n]=u; ans.pb(n-1); } if(dis[n-2]==n-2 && dis[n-1]==n-1 && dis[n]==n){fin=1;} } } if(fin==1)break; while(num[i]!=i){ cnt++; if(cnt%2==dis[i]%2){ if(cnt%2==n%2){ ll u=num[n-2]; dis[num[n-1]]=n-2; dis[num[n-2]]=n-1; num[n-2]=num[n-1]; num[n-1]=u; ans.pb(n-2); } else{ ll u=num[n-1]; dis[num[n-1]]=n; dis[num[n]]=n-1; num[n-1]=num[n]; num[n]=u; ans.pb(n-1); } } else{ ll u=num[dis[i]]; num[dis[i]]=num[dis[i]-1]; num[dis[i]-1]=u; dis[i]--; dis[num[dis[i]+1]]++; ans.pb(dis[i]); } } } cout<<cnt<<endl; for(int i=1;i<ans.size();i++){ cout<<ans[i-1]<<" "; } cout<<ans[cnt-1]<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> vec(3); for (int i = 0; i < 3; ++i) { cin >> vec.at(i); } sort(vec.begin(), vec.end()); if (vec.at(0) - vec.at(1) == vec.at(1) - vec.at(2)) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
#include "bits/stdc++.h" using namespace std; // Hi const double eps = 0.0000001; int main(){ ios::sync_with_stdio(0);cin.tie(0); long double r,x,y; cin>>r>>x>>y; long double d = sqrt(x*x+y*y); if(fabs(d-r) < eps){ cout<< 1 << '\n'; }else if(d <= r+r){ cout << 2 << '\n'; }else{ cout << ceil(d/r) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using str = string; using ll = long long; using vb = vector<bool>; using vvb = vector<vector<bool>>; using vi = vector<int>; using vvi = vector<vector<int>>; using vl = vector<long long>; using vvl = vector<vector<long long>>; #define ld long double; #define REP(i,n) for(int i=0; i<int(n); i++) #define RREP(i,n) for(int i=int(n)-1; i>=0; i--) #define FOR(i,f,t) for(int i=int(f); i<=int(t); i++) #define RFOR(i,f,t) for(int i=int(f); i>=int(t); i--) #define ALL(vec) (vec).begin(),(vec).end() #define SORT(vec) sort(ALL(vec)) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define PB(vec) push_back(vec); #define YES(ans) if(ans) cout<<"YES"<<endl; else cout<<"NO"<<endl; #define Yes(ans) if(ans) cout<<"Yes"<<endl; else cout<<"No"<<endl; #define yes(ans) if(ans) cout<<"yes"<<endl; else cout<<"no"<<endl; const long long mod = 1e9 + 7; const long long INF = 1e18; const double PI = 3.1415926535897932; int main() { int a, b, c; cin >> a >> b >> c; if( c==1 ) a++; while(true){ if( a==0 ){ cout << "Aoki" << endl; break; } a--; if( b==0 ){ cout << "Takahashi" << endl; break; } b--; } }
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; if(c==1){ if(b>a){ cout<<"Aoki"; } else{ cout<<"Takahashi"; } } else{ if(a>b){ cout<<"Takahashi"; } else{ cout<<"Aoki"; } } cout<<endl; return 0; }
#include<bits/stdc++.h> #define f first #define s second #define pb push_back #define M (L+R)/2 #define N 500009 #define inf 99999999999 #define ll long long #define mod 1000000007 #define sz size(); using namespace std; ll k,ans; int main(){ ios::sync_with_stdio(0); cin>>k; for(ll a=1;a<=sqrt(k);a++){ for(ll b=a;b<=k;b++){ int c = k/a/b; if(c<b)break; if(a==b && b==c){ ans++; continue; } if(a==b){ ans+=3*(c-b)+1; continue; } if(b==c){ ans+=3; continue; } ans+=6*(c-b)+3; } } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> // #include<atcoder/all> using namespace std; // using namespace atcoder; using lint = long long; using graph = vector<vector<int>>; #define endl '\n' lint const mod = 1e9+7; //long const mod = 998244353; int main(){ int n; cin >> n; vector<pair<int,int>>ans(n); for(int i=1;i<=n;i++){ cout << (i*2-1+n)%n + 1 << " " << (i*2)%n + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(int i=0;i<n;i++) #define ALL(v) v.begin(),v.end() constexpr ll MOD=998244353; constexpr ll INF=1e18; int main(){ ll n, m; cin >> n >> m; VI a(m); REP(i,m) cin >> a[i]; sort(ALL(a)); ll k=n, l=1; REP(i,m){ if(a[i]-l!=0) k=min(k,a[i]-l); l=a[i]+1; } if(n+1-l!=0) k=min(k,n+1-l); ll ans=0; l=1; REP(i,m){ if(a[i]-l!=0) ans+=(a[i]-l+k-1)/k; l=a[i]+1; } if(n+1-l!=0) ans+=(n+1-l+k-1)/k; cout << ans << endl; return 0; }
#define _GLIBCXX_DEBUG #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 vb = vector<bool>; using vvb = vector<vb>; using pii = pair<int, int>; using pll = pair<ll, ll>; #define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i) ll INF = 1ll << 60; int main(){ int n; string s; cin >> n >> s; stack<int> que; int cnt = 0; rep(i, 0, n){ if(que.empty()){ if(s[i] == 'f') que.push(0); continue; } if(s[i] == 'f'){ que.push(0); continue; } if(s[i] == 'o'){ if(que.top() == 0){ que.pop(); que.push(1); } else{ stack<int> tmp; que = tmp; } continue; } if(s[i] == 'x'){ if(que.top() == 1){ que.pop(); cnt++; } else{ stack<int> tmp; que = tmp; } continue; } else{ stack<int> tmp; que = tmp; } } cout << n-cnt*3 << endl; return 0; }
// #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; #define int long long int #define vi vector<int> #define ff first #define ss second #define pb push_back #define MOD 1000000007 //const int nax = 1e6+5; #define inf 4e18 #define Graph vector<vi> #define Graphw vector<vector<pair<int,int>>> #define sz(x) (int)x.size() #define pr pair<int,int> #define display(v) for(int i =0;i<(int)v.size();++i)cout << v[i] << " " #define all(v) v.begin(),v.end() #define read(a) for(int i = 0;i<(int)a.size();++i)cin>>a[i] #define show(x) cout << #x << " is " << x << "\n"; // typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si; // typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi; #define show_arr(a) cout << #a << "\n"; display(a) ; cout << "\n"; inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; } inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; } inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; } inline int sum_till(int n) { return mul(mul(n, n + 1), (MOD + 1) / 2); } const int nax = 300*1000 + 10; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#define ook order_of_key //#define fbo find_by_order // order_of_key(x) -> number of elements less than x. // * find_by_order(x) -> gives the element at index x (0 - indexing) void test_case(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin>>t; while(t--){ test_case(); } return 0; } void test_case(){ int n, q; cin>>n>>q; vi a(n); read(a); vi b(n); b[0] = a[0] - 1; for(int i = 1 ; i < n ; ++i){ b[i] = a[i] - a[i - 1] - 1; b[i] += b[i - 1]; } // show_arr(b); while(q--){ int k; cin>>k; int id = lower_bound(all(b), k) - b.begin(); int offset = (id - 1 >= 0)?a[id - 1]:0; int lst = (id - 1 >= 0)?b[id - 1]:0; cout << offset + (k - lst) << '\n'; } } // Химаншу // check overflow // special case n == 1 // 🍪🍪🍪
#include<iostream> #include<map> #include<vector> using namespace std; typedef long long li; #define MOD 1000000007 #define rep(i,n) for(int i=0;i<(n);i++) #define df 0 template<class T> void print(const T& t){ cout << t << "\n"; } template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); } struct modint{ li num; modint(li a=0){ num=(a%MOD+MOD)%MOD; } modint operator+(modint a) {return{(num+a.num)%MOD};} modint operator-(modint a) {return{((num-a.num)%MOD+MOD)%MOD};} modint operator*(modint a) {return{num*a.num%MOD};} modint& operator+=(modint a) { (num += a.num)%=MOD; return *this; } modint& operator-=(modint a) { (num += MOD-a.num)%=MOD; return *this; } modint& operator*=(modint a) { (num*=a.num)%=MOD; return *this; } }; std::ostream& operator<<(std::ostream& os, const modint& m){ // ここでストリームに obj を書き込みます。 li a=m.num; a%=MOD; a+=MOD; a%=MOD; cout << a; return os; } std::istream& operator>>(std::istream& os,modint& m){ // ここでストリームに obj を書き込みます。 li a; cin >>a; m=a; return os; } modint modpow(modint a,long int r){ modint b=a,ans=1; while(r){ if(r%2) ans*=b; b*=b; r/=2; } return ans; } // Container コンテナ型, map以外 template< template<class ...> class Ctn,class T> std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){ auto itr=v.begin(); while(itr!=v.end()){ if(itr!=v.begin())cout << " "; cout << *(itr++); } return os; }; struct node{ int v; li w; node(int a, li b=1){v=a, w=b;;} }; struct graph{ int n,rho; int m; int weighted,origin,directed; li ma; vector<vector<node>> adj; vector<li> col,w; vector<int> uf; int find(int v){ return (uf[v]<0)?v:uf[v]=find(uf[v]); } int unite(int a,int b){ a=find(a); b=find(b); if(a==b) return 0; rho--; if(a>b) swap(a,b); uf[a]+=uf[b]; uf[b]=a; return 1; } int size(int a){ return -uf[find(a)];} graph(int nn,int mm,int d_flag=0,int w_flag=0,int ori=1){ ma=0; n=nn; m=mm; weighted=w_flag; directed=d_flag; origin=ori; if(df){ print(origin,"-origin"); if(weighted) print("weighted"); if(directed) print("directed"); } adj.resize(n); uf.resize(n,-1); rho=n; rep(i,m){ int a,b; li w=1; cin >>a >>b; a-=origin; b-=origin; if(weighted) cin >>w; // if(df)print(w); adj[a].push_back({b,w}); if(not directed) adj[b].push_back({a,w}); unite(a,b); ma=max(ma,w); } } void add(int a,int b,li w=1){ adj[a].push_back({b,w}); if(not directed) adj[b].push_back({a,w}); ma=max(ma,w); unite(a,b); } li dijkstra(int a,int b); li dijkstra(int b=-1){ if(b==-1) b=n-1; return dijkstra(0,b); }; }; std::ostream& operator<<(std::ostream& os,const node& n){ printf("%d[%ld]",n.v,n.w); return os; }; std::ostream& operator<<(std::ostream& os,const graph& g){ if(g.weighted){ rep(i,g.n){ printf("%d :",i+g.origin); for(auto p:g.adj[i]){ printf(" %d[%ld]",p.v+g.origin,p.w); } print(""); } }else{ rep(i,g.n){ printf("%d :",i+g.origin); for(auto p:g.adj[i]){ printf(" %d",p.v+g.origin); } print(""); } } return os; }; void dfs(graph& g,vector<li>& cs,int a=0,int p=-1){ for(auto x:g.adj[a]){ if(x.v==p) continue; cs[x.v]=cs[a]^x.w; dfs(g,cs,x.v,a); } } int main(){ int n; cin >>n; graph g(n,n-1,0,1); if(df)print(g); vector<li> cs(n,0); dfs(g,cs); if(df)print(cs); li b=1; modint ans=0; rep(i,60){ modint one,zero; rep(t,n){ ((b&cs[t])?one:zero)+=1; } if(df)print(one,zero,b); ans+=one*zero*b; b*=2; } print(ans); }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } #define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0) int N; int P[555]; void MAIN() { scanf("%d", &N); REP (i, N) scanf("%d", P+i), P[i]--; #ifdef LOCALx N = 500; REP (i, N) P[i] = i; //reverse(P, P+N); random_shuffle(P, P+N); #endif VI ans; int t = 0; while (1) { int k = -1; bool sorted = true; for (int i=N-2; i>=0; i--) { if (P[i] != i) { sorted = false; } if (P[i] > i && i % 2 == t % 2) { k = i; break; } } if (sorted) { break; } if (k == -1) { if (t % 2 == 0) { ans.push_back(0); swap(P[0], P[1]); } else { ans.push_back(1); swap(P[1], P[2]); } t++; } else { while (k < N-1 && P[k] > k) { ans.push_back(k); swap(P[k], P[k+1]); k++; t++; } } } printf("%d\n", (int)ans.size()); REP (i, ans.size()) printf("%d%c", ans[i]+1, " \n"[i==(int)ans.size()-1]); assert((int)ans.size() <= N*N); REP (i, N) assert(P[i] == i); } int main() { int TC = 1; scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#pragma GCC optimize(2) //#include <bits/stdc++.h> #include <iostream> #include <set> #include <cmath> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include <utility> #include <map> #define rush() int T; while(cin>>T) while(T--) #define ms(a,b) memset(a,b,sizeof a) #define lowbit(x) ((x)&(-x)) #define inf 0x7f7f7f7fll #define eps 1e-11 #define sd(a) scanf("%d",&a) #define sll(a) scanf("%lld",&a) #define sll2(a,b) scanf("%lld%lld",&a,&b) #define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c) #define sdd(a,b) scanf("%d%d",&a,&b) #define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define sf(a) scanf("%lf",&a) //#define sc(a) scanf("%c\n",&a) #define ss(a) scanf("%s",a) #define pd(a) printf("%d\n",a) #define pdd(a,b) printf("%d %d\n",a,b) #define pddd(a,b,c) printf("%d %d %d\n",a,b,c) #define pll(a) printf("%lld\n",a) #define pf(a) printf("%.1lf\n",a) #define pc(a) printf("%c",a) #define ps(a) printf("%s\n",a) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define forn(i,a,b,x) for(int i=(a);i<=(b);i+=(x)) #define per(i,a,b) for(int i=(b);i>=(a);i--) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define debug(a) cout<<#a<<" : "<<a<<" "<<" ; " #define dbg(a) cout<<#a<<" : "<<a<<endl #define show(a,b) cout<<a<<" :\t"<<b<<"\t*"<<endl #define IOS ios::sync_with_stdio(0); cin.tie(0) #define PAUSE system("pause") #define all(x) x.begin(),x.end() //#define print(a) cout<<a<<endl;continue; #define fr first #define sc second #define ceils(a,b) ((a-1ll+b)/(b)) #define floors(a,b) floor(double(a)/double(b)) using namespace std; //inline void swap(int &x,int &y){x^=y^=x^=y;} typedef long long ll; //using ui=unsigned int; typedef unsigned long long ull; typedef pair<int,int> pii; const double pi=acos(-1.0); const int dx[]={0,1,0,-1}; const int dy[]={1,0,-1,0}; const int limit=5e5; int read() { int s=0; char c=getchar(),lc='+'; while (c<'0'||'9'<c) lc=c,c=getchar(); while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar(); return lc=='-'?-s:s; } void write(int x) { if (x<0) putchar('-'),x=-x; if (x<10) putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } void print(int x,char c='\n') { write(x); putchar(c); } ll mod=2147483648; const int N=3e5+5; int n, m, _; int i, j, k; int a[N]; int pos[N]; vector<int> ans; signed main() { //IOS; rush(){ sd(n); rep(i, 1, n) sd(a[i]), pos[a[i]] = i; int opt = 1; rep(i, 1, n){ if(i == pos[i]) continue; if((pos[i] - 1) % 2 != opt){ if(pos[i] - 2 >= i){ ans.pb(pos[i] - 2); int x = pos[i] - 2, y = pos[i] - 1; swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); opt ^= 1; } else { //if(pos[i] == i + 1){ ans.pb(i - 1); ans.pb(i); ans.pb(i - 1); ans.pb(i); ans.pb(i - 1); //ans.pb(i); opt ^= 1; int x = pos[i], y = pos[i] - 1; swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); continue; } } for(int j = pos[i] - 1; j >= i; j--){ ans.pb(j); int x = j, y = j + 1; swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); opt ^= 1; } } pd(ans.size()); for(auto it : ans) printf("%d ", it); puts(""); ans.clear(); } //PAUSE; return 0; }
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<queue> #include<map> using namespace std; #define LL long long #define DB double #define MAXN 200 #define Pr pair<int,int> #define X first #define Y second #define INF 1000000001 #define mem(x,v) memset(x,v,sizeof(x)) LL read(){ LL x=0,F=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*F; } int n,a[MAXN+5],b[MAXN+5],dp[MAXN+5],F; int ta[MAXN+5],tb[MAXN+5]; int vis[MAXN+5]; bool check(int l,int r){ int L=(r-l+1)/2; for(int i=1;i<=n;i++)ta[i]=a[i],tb[i]=b[i]; for(int i=1;i<=2*n;i++)vis[i]=0; for(int i=1;i<=n;i++) if(ta[i]!=-1||tb[i]!=-1){ if(ta[i]==-1){ ta[i]=tb[i]-L; if(!(tb[i]>=l&&tb[i]<=r))continue; } else if(tb[i]==-1){ tb[i]=ta[i]+L; if(!(ta[i]>=l&&ta[i]<=r))continue; } if((ta[i]>=l&&ta[i]<=r)^(tb[i]>=l&&tb[i]<=r)) return 0; if((ta[i]>=l&&ta[i]<=r)&&(tb[i]>=l&&tb[i]<=r)){ if(tb[i]-L!=ta[i])return 0; if(vis[ta[i]]||vis[tb[i]])return 0; vis[ta[i]]=vis[tb[i]]=1; } } return 1; } int main(){ n=read(); for(int i=1;i<=n;i++)a[i]=read(),b[i]=read(); dp[0]=1; for(int r=2;r<=2*n;r+=2) for(int l=r-1;l>=1;l-=2) if(dp[l-1]&&check(l,r)){dp[r]=1;break;} printf("%s",dp[2*n]?"Yes":"No"); }
#include <bits/stdc++.h> #define ln '\n' #define all(dat) dat.begin(), dat.end() #define loop(i, to) for (int i = 0; i < to; ++i) #define cont(i, to) for (int i = 1; i <= to; ++i) #define circ(i, fm, to) for (int i = fm; i <= to; ++i) #define foreach(i, dat) for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i) typedef long long num; using namespace std; const int nsz = 2e2; bool f[nsz + 5]; char s[nsz + 5]; int n, fm[nsz + 5], l[nsz + 5], r[nsz + 5]; void inline exit_with(bool can) { printf(can ? "Yes\n" : "No\n"); exit(0); } bool inline chk(int l, int r) { int md = (l + r) >> 1; circ (i, l, md) if (s[i] == ')') return 0; circ (i, md + 1, r) if (s[i] == '(') return 0; circ (i, l, md) if (fm[i] && ::r[fm[i]] != -1 && ::r[fm[i]] != i + md - l + 1) return 0; circ (i, md + 1, r) if (fm[i] && ::l[fm[i]] != -1 && ::l[fm[i]] != i - md + l - 1) return 0; circ (i, l, md) { int j = i + md - l + 1; if (fm[i] && fm[j] && fm[i] != fm[j]) return 0; } return 1; } int main() { scanf("%d", &n); cont (i, 2 * n) s[i] = '?'; cont (i, n) { int l, r; scanf("%d%d", &l, &r); if ((l != -1 && s[l] != '?') || (r != -1 && s[r] != '?')) exit_with(0); if (l != -1) s[l] = '('; if (r != -1) s[r] = ')'; ::l[i] = l, ::r[i] = r; fm[l] = fm[r] = i; } f[0] = 1; cont (i, 2 * n) { for (int j = i - 1; j >= 0; --j) { if ((i - j) % 2 == 1) continue; f[i] |= f[j] & chk(j + 1, i); } } exit_with(f[2 * n]); }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int long long #define reset(x) memset(x,0,sizeof(x)) #define db1(x) cout<<#x<<"="<<x<<'\n' #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n' #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n' #define rep(i,n) for(int i=0;i<(n);++i) #define repA(i,a,n) for(int i=a;i<=(n);++i) #define repD(i,a,n) for(int i=a;i>=(n);--i) #define sz(a) (int)a.size() #define pii pair<int,int> #define all(a) a.begin(),a.end() #define PI 3.1415926535897932384626433832795 #define INF 1000000000 #define MOD 1000000007 #define MOD2 1000000009 #define EPS 1e-6 #define pb push_back #define fi first #define se second #define mp make_pair int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen(".inp", "r", stdin); //freopen(".out", "w", stdout); vector<pii> total; // tong, so luong int n, k; cin >> n >> k; int dp[4][3000005] = {0}; dp[0][0] = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j <= i * n; j++) { dp[i + 1][j + 1] += dp[i][j]; dp[i + 1][j + n + 1] -= dp[i][j]; } for (int j = 1; j <= (i + 1) * n; j++) { dp[i + 1][j] += dp[i + 1][j - 1]; } } int x; for(int i=3;i<=3*n;i++){ if(k<=dp[3][i]){x=i;break;} else{k-=dp[3][i];} } for(int i=1;i<=n;i++){ int jmi=max(1LL,x-i-n); int jma=min(n,x-i-1); if(jmi>jma){continue;} if(k>(jma-jmi+1)){k-=(jma-jmi+1);continue;} int y=jmi+k-1; int z=x-i-y; cout << i << ' ' << y << ' ' << z << '\n'; return 0; } }
#include<bits/stdc++.h> using namespace std; using ll=long long; #define all(v) v.begin(),v.end() int main() { ll N,K;cin>>N>>K; vector<ll> sum(3*N+1,0); for(ll i=3;i<=3*N;i++) { sum[i]=(i-1)*(i-2)/2; if(i-3>=N) { ll k=i-3-N; sum[i]-=((k+2)*(k+1)/2)*3; } if(i-3>=2*N) { ll k=i-3-2*N; sum[i]+=((k+2)*(k+1)/2)*3; } } vector<ll> rsum(3*N+1,0); for(ll i=3;i<=3*N;i++) { rsum[i]=rsum[i-1]+sum[i]; } ll s=3; ll g=3*N; while(s<g) { ll k=(s+g+1)/2; if(rsum[k-1]<K) { s=k; } else { g=k-1; } } K-=rsum[s-1]; vector<ll> kirei(s-1,0); ll s2=max(1LL,s-2*(N)); for(ll i=s2;i<=min(N,s-2);i++) { ll k=s-i-2; kirei[i]=k+1; if(k>=N) { ll t=k-N; kirei[i]-=(t+1)*2; } if(k>=2*N) { ll t=k-2*N; kirei[i]+=t+1; } } vector<ll> rkirei(s-1,0); for(ll i=s2;i<=min(N,s-2);i++) { rkirei[i]=rkirei[i-1]+kirei[i]; } ll g2=min(N,s-2); while(s2<g2) { ll k=(s2+g2+1)/2; if(rkirei[k-1]<K) { s2=k; } else { g2=k-1; } } K-=rkirei[s2-1]; vector<ll> oi(s-s2,0); ll s3=max(1LL,s-s2-N); for(ll i=s3;i<=min(N,s-s2-1);i++) { oi[i]=1; } vector<ll> roi(s-s2,0); for(ll i=s3;i<=min(N,s-s2-1);i++) { roi[i]=roi[i-1]+oi[i]; } ll g3=min(N,s-s2-1); while(s3<g3) { ll k=(s3+g3+1)/2; if(roi[k-1]<K) { s3=k; } else { g3=k-1; } } cout<<s2<<" "<<s3<<" "<<s-s2-s3<<endl; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define pi 3.141592653589793238 #define int long long #define ll long long #define ld double 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>; mt19937 rnd(time(0)); long long powm(long long a, long long b,long long mod) { long long res = 1; while (b > 0) { if (b & 1) res = res * a %mod; a = a * a %mod; b >>= 1; } return res; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } vector<vector<pair<int,int> > >adj[26]; int dis[1005][1005]; void dik(int so,int des) { dis[so][des]=0; priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > >qq; qq.push({0,{so,des}}); while(!qq.empty()) { auto pp=qq.top(); qq.pop(); if(dis[pp.second.first][pp.second.second]<pp.first) continue; for(int i=0;i<26;i++) { for(auto j:adj[i][pp.second.first]) { for(auto k:adj[i][pp.second.second]) { if(dis[j.first][k.first]>pp.first+j.second+k.second) { dis[j.first][k.first]=pp.first+j.second+k.second; qq.push({dis[j.first][k.first],{j.first,k.first}}); } } } } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int t = 1; // cin>>t; while(t--) { int n,m,so,des; cin>>n>>m; so = 1; des = n; for(int i=0;i<26;i++) { adj[i].clear(); adj[i].resize(n+1); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=1e17; char w; int x,y,z; vector<pair<int,pair<int,int> > >edge; for(int i=0;i<m;i++) { cin>>x>>y>>w; z=1; edge.push_back({z,{x,y}}); adj[w-'a'][x].push_back({y,z}); adj[w-'a'][y].push_back({x,z}); } dik(so,des); int mn=1e17; for(auto i:edge) { mn=min(mn,i.first+dis[i.second.first][i.second.second]); mn=min(mn,i.first+dis[i.second.second][i.second.first]); } for(int i=1;i<=n;i++) mn=min(mn,dis[i][i]); if(mn==1e17) cout<<-1<<'\n'; else cout<<mn<<'\n'; } }
#include <bits/stdc++.h> #define rep(i,a,b) for(ll i=a;i<b;i++) #define rrep(i,b,a) for(int i=b;i>=a;i--) #define fori(a) for(auto i : a ) #define all(a) begin(a), end(a) #define set(a,b) memset(a,b,sizeof(a)) #define sz(a) a.size() double pi=acos(-1); #define ll long long #define ull unsigned long long #define pb push_back #define PF push_front //deque // #define mp make_pair #define pq priority_queue const ll mod=1000000007; #define f first #define s second #define pii pair< ll, ll > #define vi vector<int> #define vpii vector<pii> #define debug(v) for(auto i:v) cout<<i<<" "; #define tc int t; cin >> t; while(t--) using namespace std; void optimizeIO(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int MAX=200005; vector<int> adj[MAX]; int n,l; void add_edge(int u, int v){ adj[u].push_back(v); adj[v].push_back(u); } int timer; vector<int> tin, tout; vector<vector<int>> up; ll dp[MAX],ans[MAX]; void dfs(int v, int p){ tin[v] = ++timer; up[v][0] = p; for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i-1]][i-1]; for (int u : adj[v]) if (u != p) dfs(u, v); tout[v] = ++timer; } bool is_ancestor(int u, int v){ return tin[u] <= tin[v] && tout[u] >= tout[v]; } pair<int,int> lca(int u, int v){ if (is_ancestor(u, v)) return {1,u}; if (is_ancestor(v, u)) return {2,v}; for (int i = l; i >= 0; --i) if (!is_ancestor(up[u][i], v)) u = up[u][i]; return {3,up[u][0]}; } void preprocess(int root) { tin.resize(n+1); tout.resize(n+1); timer = 0; l = ceil(log2(n)); up.assign(n+1, vector<int>(l + 1)); dfs(root, root); } void dfs2(int v,int p,ll rem){ rem+= dp[v]; ans[v]=rem; for(auto i:adj[v]) if(i!=p) dfs2(i,v,rem); } void solve(){ cin>>n; pii x[n]; rep(i,1,n){ cin>>x[i].f>>x[i].s; add_edge(x[i].f,x[i].s); } preprocess(1); int q; cin>>q; while(q--){ int tq,eq,xq; cin>>tq>>eq>>xq; int from,notto; if(tq==1){ from=x[eq].f; notto=x[eq].s; } else{ from=x[eq].s; notto=x[eq].f; } pii z=lca(from,notto); if(z.f==3){ dp[1]+= xq; dp[notto]-=xq; } else if(z.f==1){ dp[1]+=xq; dp[notto]-=xq; } else{ int anc=notto,u=from; while(1){ int flag=0; for(int i=l;i>=0;i--){ if(up[u][i]!=anc && is_ancestor(up[u][i],anc)==anc){ flag=1; u=up[u][i]; break; } } if(flag==0) break; } dp[u]+=xq; } } dfs2(1,-1,0); rep(i,1,n+1) cout<<ans[i]<<'\n'; } int main(){ optimizeIO(); solve(); }
#include <iostream> #include <cmath> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <tuple> #include <utility> #include <functional> #include <set> #include <map> #include <bitset> #include <list> #include<iomanip> using namespace std; using ll = long long; using ULL = unsigned long long; using pll = std::pair<ll, ll>; using vi = std::vector<int>; using vl = std::vector<ll>; using vpl = std::vector<pll>; using vb = std::vector<bool>; using db = double; using vdb = std::vector<db>; using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending template<class T> using vv = std::vector<std::vector<T> >; using std::cout; using std::string; using std::endl; using std::cin; using std::transform; using std::toupper; #define REPL(i, n) for (ll i = 0; i < (ll)(n); i++) #define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define REP(i, n) FORL(i, 0, n) #define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- ) #define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define bit(n) (1LL << (n)) #define INF pow(10, 10) void chmin(ll& a, ll b){ if(a > b) a = b; } void chmax(ll& a, ll b){ if(a < b) a = b; } int main(void) { // たてx, 横y ll N, M; cin >> N >> M; vl As(N), Bs(M); REP(i, N) { cin >> As[i]; } REP(i, M) { cin >> Bs[i]; } ll i = 0, j = 0; while(i < N && j < M) { if(As[i] == Bs[j]) { i ++; j ++; } else if(As[i] < Bs[j]) { cout << As[i] << " "; i++; } else { cout << Bs[j] << " "; j ++; } } if(i < N) { while(i<N) { cout << As[i] << " "; i++; } } else { while(j < M) { cout << Bs[j] << " "; j ++; } } cout << endl; return 0; }
// UTF−8 #include<bits/stdc++.h> /* #include<atcoder/all> */ /* using namespace atcoder; */ using namespace std; using ll = long long int; using lc = complex<double>; template<class T>bool chmax(T &a, const T &b) { return (a<b ? (a=b,1) : 0); } template<class T>bool chmin(T &a, const T &b) { return (a>b ? (a=b,1) : 0); } int main(void) { constexpr ll MOD = 1 ? 1e9+7 : 998244353; const double PI = acos(-1); constexpr double eps = 1e-10; cout << fixed << setprecision(32); cin.tie(0); ios::sync_with_stdio(false); if(1) { ll n; cin >> n; vector<ll> a(n), b(n), p(n), rp(n); for(auto &e: a) cin >> e; for(auto &e: b) cin >> e; for(auto &e: p) cin >> e, e--; for(ll i=0; i<n; i++) rp[p[i]] = i; vector<tuple<ll,ll>> aa(n); for(ll i=0; i<n; i++) aa[i] = {a[i], i}; sort(begin(aa), end(aa)); vector<tuple<ll,ll>> res; for(ll i=0; i<n; i++) { auto [_, j] = aa[i]; if(p[j] == j) continue; ll k = rp[j]; if(a[j] <= b[p[j]]) { cout << -1 << endl; return 0; } if(a[k] <= b[p[k]]) { cout << -1 << endl; return 0; } swap(rp[p[j]], rp[p[k]]); swap(p[j], p[k]); res.push_back({j, k}); } cout << res.size() << endl; for(auto [x, y]: res) cout << x+1 << ' ' << y+1 << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define lli long long int #define nl "\n"; #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define db long double #define mod 1e9 + 7 #define lb lower_bound #define ub upper_bound #define vlli vector<lli> #define mslli multiset<lli> #define inf INT_MAX #define sp " " #define pb push_back #define pie 3.14159265358979323846 #define test lli t; cin>>t; while(t--) int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } int main() { /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL; */ lli n; cin>>n; cout<<n-1<<nl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; N--; cout << N << 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<int,int>; const ll INF=1e18; const int mod=998244353; int main(){ int n; cin>>n; vector<int>cnt(200005); int j=0; rep(i,n){ int p; cin>>p; cnt[p]++; while(j<=200000&&cnt[j]){j++;} cout<<j<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int ans = 0; map<int, bool> m; for (int i=0; i<N; i++) { int p; cin >> p; m[p] = true; for (int j=ans; j<=200000; j++) { if (!m.count(j)) { cout << j << endl; ans = j; break; } } } }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; #define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ff first #define ss second #define int long long #define ll long long #define pb push_back #define pii pair<int,int> #define vi vector<int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define mod 998244353 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define vpii vector<pair<int,int> > #define all(x) x.begin(),x.end() #define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";} #define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n" #define sz(x) (int)x.size() #define db(x) cout<<x<<"\n"; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //insert,find_by_order,order_of_key,lower_bound,upper_bound; #define TRACE #ifdef TRACE #define deb(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define deb(...) #endif //////////////////////////////code////////////////////////////// const int N = 200; void solve() { int n; cin >> n; int temp = n; int cnt = 0; vi s; while(temp) cnt++,s.pb(temp%10),temp/=10; int ans = 0; for(int i = 2;i < cnt; i += 2) { int curr = 9; for(int j = 1;j < i/2; j++) curr*=10; ans += curr; } if(cnt&1) { db(ans) return; } reverse(all(s)); for(int i = 0;i < (cnt/2); i++) { int curr = 0; if(i == 0) curr = max(s[i] - 1,0ll); else curr = s[i]; for(int j = i + 1;j < (cnt/2) ;j++) curr*=10; ans += curr; } int f = 1; for(int i = 0;i < cnt/2 ;i++) { if(s[i] > s[cnt/2 + i]) f = 0; else if(s[i] < s[cnt/2 + i]) break; } db(ans + f) } int32_t main() { LETS_GET_SCHWIFTY; #ifndef ONLINE_JUDGE freopen("INP.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t = 1; // cin >> t; while (t--) solve(); } // check out for following mistakes- // if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only // is using prime sieve make sure it fits // when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
// KEEP CALM and Enjoy the Problem B-) #include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define mod 1000000007 // 1e9+7 #define inf 1e18 #define watch(x) cout<<(#x)<<" is "<<(x)<<"\n" #define watch2(x,y) cout<<(#x)<<" is "<<(x)<<" and "<<(#y)<< " is " <<(y)<<"\n" struct custom_hash { static uint64_t splitmix64(uint64_t x){ // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; ll integer_power(ll x, ll y){ // integer_func() funcs in O(log y) time. Just WOW! /* Take Care: MUST Remove modulo, if in Problem, dealing with integers >= 10^9*/ ll res = 1; // res -> result if (x == 0) return 0; while (y > 0){ if (y & 1) res = (res*x); y = y>>1; // y = y/2 x = (x*x); } return res; } ll count_no_of_digit(ll N){ ll count; count = 0; while(N > 0){ count += 1; N /= 10; } return count; } void run_case(){ ll N,n,count_digit,i,ans,left,right; ll current_num; cin>>N; count_digit = count_no_of_digit(N); //watch(count_digit); ans = 0; n = 2; while(n < count_digit){ ans += 9 * integer_power(10,(n/2 - 1)); n += 2; } if(n == count_digit){ // n is even left = N / (integer_power(10,n/2)); right = N % (integer_power(10,n/2)); i = integer_power(10,(n/2-1)); while(1){ current_num = i*integer_power(10,n/2) + i; //watch(current_num); if(current_num > N) break; ans += 1; i += 1; } } cout<<ans<<"\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll T = 1; //cin>>T; while(T--) run_case(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define mp make_pair #define deb(x) cout<< #x << " " << x << "\n"; #define MAX 9223372036854775807 #define MIN -9223372036854775807 #define PI 3.141592653589 #define setbits(n) __builtin_popcountll(n) #define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin()); const ll mod=1e9+7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll T=clock(); vector<ll> a(4); for(ll i=0;i<4;i++) cin>>a[i]; sort(a.begin(),a.end(),greater<ll>()); if(a[0]+a[2]==a[1]+a[3] || a[0]+a[1]==a[2]+a[3] || a[0]+a[3]==a[1]+a[2] || a[0]==a[1]+a[2]+a[3]) cout<<"Yes"; else cout<<"No"; cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n"; T = clock(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define ff first #define ss second #define pii pair<int,int> #define pll pair<ll,ll> #define mod 1000000007 #define rep(i,n) for(int i=0; i<n; i++) #define repa(i,start,end) for(int i=start; i<=end; i++) #define repd(i,start,end) for(int i=start; i>=end; i--) #define all(x) x.begin(),x.end() #define debug(x) cout << "(" << #x << ": " << x << ")" << endl; void solve() { ll a,b,c,d; cin>>a>>b>>c>>d; if(a==b+c+d || a+b==c+d || a+c==b+d || c==a+b+d || a+b+c==d || b+c==a+d || b==a+c+d) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = s; i < (int)(n); i++) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define even(x) (x % 2 == 0 ? true : false) #define odd(x) (x % 2 ? true : false) using namespace std; using ll = long long; using ull = unsigned long long; using uint = unsigned; using pii = pair<int, int>; //const ll INF = 1e18L + 5; //const int INF = 1e9 + 5; //const double pi = 3.14159265358979323846; //const int mod = 1000000007; //vector<vector<int>> dp(m, vector<int>(n)); void dfs(int p, vector<vector<int>>& paths, vector<bool>& visited) { if (visited[p]) return ; visited[p] = true; for (int path : paths[p]) dfs(path, paths, visited); } void solve() { int n, m; cin >> n >> m; vector<int> a(m), b(m); vector<vector<int>> paths(n); int ans = 0; while (m--) { int a, b; cin >> a >> b; paths[a - 1].push_back(b - 1); } for (int i = 0; i < n; i++) { vector<bool> visited(n); dfs(i, paths, visited); ans += accumulate(all(visited), 0); } cout << ans << endl; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define M 1000000007 #define FOR(i, s, n) for (ll i = (s); i < (n); ++i) #define FORALL(i, n) for (ll i = 0; i < (n); ++i) #define MOD 1000000007 #define ff first #define ss second #define pll pair<long long, long long> #define pii pair<int,int> #define fast ios_base::sync_with_stdio(false) #define countbits(x) __builtin_popcount(x) #define pb(x) push_back((x)) #define vll vector<ll> #define all(x) x.begin(),x.end() #define print(x) cout << x << '\n'; #define debug print("reaching\n") #define mll map<ll,ll> #define lsb(i) i&(-i) #define printv(x) \ FORALL(i, x.size()) \ { \ cout << x[i] << ' '; \ } \ cout << endl \ ; #define printm(m) \ for(auto i: m){\ cout<<i.ff<<' '<<i.ss<<endl; \ }\ ; template <typename T> T extGcd(T a, T b, T &x, T &y) { if (b == 0) { x = 1; y = 0; return a; } else { int g = extGcd(b, a % b, y, x); y -= a / b * x; return g; } } template <typename T> T modInv(T a, T m) { T x, y; extGcd(a, m, x, y); return (x % m + m) % m; } long long binpow(long long a, long long b, ll mod) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } res%=mod; return res; } int main() { // fast; // cin.tie(0); // cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t=1; // cin>>t; ll tot=t; while(t--){ ll n,k; cin>>n>>k; ll x; ll ans = 0; FOR(y, 2, 2*n+1){ ll num = k + y; if(num < 2) continue; if(num > 2*n) continue; ll mid = n + 1; // has n ways ll dist = abs(num - mid); ll dist2 = abs(y - mid); ans += (n - dist) * (n - dist2); } print(ans); } return 0; } /* */
/* A */ #include <bits/stdc++.h> using namespace std; using ll = int64_t; using pii = pair<int, int>; const int N = 3; int n, m, a[N][N]; signed main(int argc, char** argv) { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif for (int k = 1; k <= 2; ++k) { for (int i = 1; i <= 2; ++i) cin >> a[k][i]; } cout << a[1][1] * a[2][2] - a[1][2] * a[2][1] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int A1,A2,A3; cin >> A1 >> A2 >> A3; string ans = "No"; //(A1,A2,A3) if(A3-A2 == A2-A1) ans = "Yes"; //(A1,A3,A2) if(A2-A3 == A3-A1) ans = "Yes"; //(A2,A1,A3) if(A3-A1 == A1-A2) ans = "Yes"; //(A2,A3,A1) if(A1-A3 == A3-A2) ans = "Yes"; //(A3,A1,A2) if(A2-A1 == A1-A3) ans = "Yes"; //(A3,A2,A1) if(A1-A2 == A2-A3) ans = "Yes"; cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long const ll INF=1e17; const int N=1e5+3,M=1e5+3; template <typename T> inline void read(T &x){ x=0;char ch=getchar();bool f=false; while(!isdigit(ch)) f|=ch=='-',ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); x=f?-x:x; } template <typename T> inline void print(T x){ if(x<0) putchar('-'),x=-x; if(x>9) print(x/10); putchar(x%10^48); } int n,m,st,ed; ll k[M<<1],t[M<<1]; int head[N],to[M<<1],Next[M<<1],tot; ll dist[N]; priority_queue<pair<ll,int> > pq; bool vis[N]; inline void Addedge(int u,int v,ll w1,ll w2){to[++tot]=v,Next[tot]=head[u],head[u]=tot,k[tot]=w2,t[tot]=w1;return;} inline ll DJ(){ for(register int i=1;i<=n;++i) dist[i]=INF; dist[st]=0; pq.push(make_pair(0,st)); while(!pq.empty()){ int x=pq.top().second;pq.pop(); if(vis[x]) continue; vis[x]=true; if(x==ed) return dist[ed]; for(register int i=head[x];i;i=Next[i]){ int y=to[i]; if(dist[x]%k[i]==0){ if(dist[y]>dist[x]+t[i]){ dist[y]=dist[x]+t[i]; pq.push(make_pair(-dist[y],y)); } } else{ ll tep=(dist[x]/k[i]+1)*k[i]; if(dist[y]>tep+t[i]){ dist[y]=tep+t[i]; pq.push(make_pair(-dist[y],y)); } } } } return -1; } int main(){ read(n),read(m),read(st),read(ed); for(register int i=1;i<=m;++i){ int u,v;ll w1,w2; read(u),read(v),read(w1),read(w2); Addedge(u,v,w1,w2);Addedge(v,u,w1,w2); } print(DJ()); return 0; }
#pragma warning(disable:4996) //#include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <string.h> #include <queue> #define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define ln '\n' #define rp ' ' typedef long long ll; typedef long double lf; using namespace std; typedef struct Edge { ll e, c, k; }; typedef struct Node { ll e, c; bool operator<(const Node tmp) const { return c > tmp.c; } }; ll n, m, x, y; ll dist[100100]; ll INF = 1e18; priority_queue<Node> pq; vector<Edge> map[100100]; void dijkstra() { while (!pq.empty()) { Node now = pq.top(); pq.pop(); if (now.c > dist[now.e]) continue; for (auto i : map[now.e]) { if (dist[now.e] % i.k) { if (dist[i.e] > dist[now.e] + i.c + i.k - dist[now.e] % i.k) { dist[i.e] = dist[now.e] + i.c + i.k - dist[now.e] % i.k; pq.push({ i.e, dist[i.e] }); } } else { if (dist[i.e] > dist[now.e] + i.c) { dist[i.e] = dist[now.e] + i.c; pq.push({ i.e, dist[i.e] }); } } } } } int main() { fastio; cin >> n >> m >> x >> y; for (ll i = 1; i <= m; i++) { ll a, b, t, k; cin >> a >> b >> t >> k; map[a].push_back({ b, t, k }); map[b].push_back({ a, t, k }); } fill(dist, dist + n + 1, INF); dist[x] = 0; pq.push({ x, 0 }); dijkstra(); if (dist[y] == INF) cout << -1; else cout << dist[y]; }
#include <iostream> using namespace std; int main() { int a,b,x,y,i,j,sum=1; cin>>a>>b>>x>>y; char str[a+1][b+1]; for(i=0;i<a;i++) { cin>>str[i]; } i=x-1,j=y-1; for(i=x;i<a;i++) { if(str[i][j]=='.') { sum++; } if(str[i][j]=='#') break; } for(i=x-1,j=y;j<b;j++) { if(str[i][j]=='.') { sum++; } if(str[i][j]=='#') break; } for(i=x-1,j=y-2;j>=0;j--) { if(str[i][j]=='.') { sum++; } if(str[i][j]=='#') break; } for(i=x-2,j=y-1;i>=0;i--) { if(str[i][j]=='.') { sum++; } if(str[i][j]=='#') break; } cout<<sum; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<vector<vector<long double> > > vvvld; typedef vector<vector<long double> > vvld; typedef vector<vector<vector<int> > > vvvi; typedef vector<vector<int> > vvi; #define rep(i,n) for (int i = 0; i< (int)(n); i++) int kazu(int x, string s){ int ans =0; int now = 0; bool y = false; rep(i,s.size()){ if(s.at(i)=='.') now++; if(s.at(i)=='#'){ if(y){ y = false; ans = now; break; } else{ now = 0; } } if(i == x && s.at(i) != '#') y = true; } if(y) ans = now; return ans; } int main(){ int h,w,x,y; cin>>h>>w>>x>>y; vector<string> s(h); rep(i,h) cin>>s.at(i); string tate; rep(i,h){ tate += s.at(i).at(y-1); } int ans=kazu(y-1,s.at(x-1)) + kazu(x-1, tate); if(ans>0) ans--; cout<<ans<<endl; //cout << fixed; //cout << setprecision(10)<< << endl; }
//1階から決めると色々決まるため、DPができる. #include <iostream> #include <vector> #include <algorithm> #define rep(i, n) for(i = 0; i < n; i++) using namespace std; int n; int a[100], b[100]; int idA[200]; int idB[200]; bool dp[201]; bool check(int i, int j) { int k; //[i, i + j) -- [i + j, i + 2j) for (k = 0; k < 2 * j; k++) { if (idA[i + k] != -1) { if (k >= j) { return false; } if (b[idA[i + k]] != -1) { if (idB[i + k + j] != idA[i + k]) { return false; } } else { if (idB[i + k + j] != -1) { return false; } } } if (idB[i + k] != -1) { if (k < j) { return false; } if (a[idB[i + k]] != -1) { if (idA[i + k - j] != idB[i + k]) { return false; } } else { if (idA[i + k - j] != -1) { return false; } } } } return true; } int main() { int i, j; cin >> n; vector<int> abSet; rep(i, n) { cin >> a[i] >> b[i]; if (a[i] != -1) a[i]--; if (b[i] != -1) b[i]--; if (a[i] != -1 && b[i] != -1 && a[i] >= b[i]) { //a[i] < b[i] cout << "No" << endl; return 0; } if (a[i] != -1) abSet.push_back(a[i]); if (b[i] != -1) abSet.push_back(b[i]); } sort(abSet.begin(), abSet.end()); rep(i, (int)abSet.size() - 1) { if (abSet[i] == abSet[i + 1]) { //distinct cout << "No" << endl; return 0; } } rep(i, 2 * n) idA[i] = idB[i] = -1; rep(i, n) if (a[i] != -1) idA[a[i]] = i; rep(i, n) if (b[i] != -1) idB[b[i]] = i; dp[0] = true; rep(i, 2 * n) { if (dp[i] == false) continue; for (j = 1; i + 2 * j <= 2 * n; j++) { if (check(i, j)) { dp[i + 2 * j] = true; } } } if (dp[2 * n]) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
// AUTHOR: Sumit Prajapati #include <bits/stdc++.h> using namespace std; #define ull unsigned long long #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define mk make_pair #define ff first #define ss second #define all(a) a.begin(),a.end() #define trav(x,v) for(auto &x:v) #define debug(x) cerr<<#x<<" = "<<x<<'\n' #define llrand() distribution(generator) #define rep(i,n) for(int i=0;i<n;i++) #define repe(i,n) for(int i=1;i<=n;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define printar(a,s,e) FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n' #define endl '\n' #define curtime chrono::high_resolution_clock::now() #define timedif(start,end) chrono::duration_cast<chrono::nanoseconds>(end - start).count() #define MD 1'000'000'007 #define MDL 998244353 #define MX 100'005 const ll INF=1e16; auto time0 = curtime; random_device rd; default_random_engine generator(rd()); uniform_int_distribution<ull> distribution(0,0xFFFFFFFFFFFFFFFF); //Is testcase present? ll minimum_dif(vector<ll>& a,vector<ll>& b){ ll res=INF; if(a.size()==0) return INF; if(b.size()==0) return INF; for(auto &el:b){ ll l=0,r=a.size()-1; ll mid,ind=r; while(l<=r){ mid=(l+r)>>1; if(a[mid]>=el){ r=mid-1; ind=mid; } else l=mid+1; } res=min(res,abs(el-a[ind])); if(ind>0) res=min(res,abs(el-a[ind-1])); if(ind<(a.size()-1)) res=min(res,abs(el-a[ind+1])); } return res; } void solve(){ ll n;cin>>n; vector<ll>R,G,B; ll x; char ch; repe(i,2*n){ cin>>x>>ch; if(ch=='R') R.pb(x); else if(ch=='G') G.pb(x); else B.pb(x); } sort(all(R)); sort(all(G)); sort(all(B)); ll sz1=R.size(),sz2=G.size(),sz3=B.size(); if(sz1%2==0 && sz2%2==0 && sz3%2==0){ cout<<"0"; return; } // cout<<sz1<<" "<<sz2<<" "<<sz3<<'\n'; // return; assert(((sz1%2)+(sz2%2)+(sz3%2))==2); ll res=INF; ll GB=minimum_dif(B,G),RB=minimum_dif(R,B),RG=minimum_dif(R,G); if(sz1%2==0){ res=min(GB,RB+RG); } else if(sz2%2==0){ res=min(RB,RG+GB); } else{ res=min(RG,RB+GB); } cout<<res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); time0 = curtime; int t=1; // cin>>t; repe(tt,t){ //cout<<"Case #"<<tt<<": "; solve(); } //cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(false); cin.tie(0); #define st first #define nd second #define endl '\n' //toggle off for interactive problems #define what_is(x) cerr << #x << " is " << x << endl; #define what_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << e << ' '; cerr << '\n'; //vector, set #define what_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << x[i] << ' '; cerr << '\n'; //n first element of array #define cerr_pair(x) '{' << x.st << ", " << x.nd << '}' #define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl; #define pwhat_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << cerr_pair(e) << ' '; cerr << '\n'; //vector, set #define pwhat_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << cerr_pair(x[i]) << ' '; cerr << '\n'; //n first element of array // #define int long long // #define pow mypow typedef pair<int,int> pii; const int N = 2e3+5; const int INF = 1e9+5; const int MOD = 1e9+7; int n, m; char a[N][N]; int d[N][N]; bool vis[N][N], visc[26]; vector<pii> g[26]; queue<pii> q; int di[4] = {-1, 0, 1, 0}; int dj[4] = {0, 1, 0, -1}; int gx, gy; int32_t main() { IOS // freopen("input.txt", "r", stdin); cin >> n >> m; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ cin >> a[i][j]; if (a[i][j] == 'S') { vis[i][j] = true; q.push({i, j}); } else if (a[i][j] == 'G') { gx = i; gy = j; } else if (a[i][j] != '.' && a[i][j] != '#') { g[a[i][j]-'a'].push_back({i, j}); } } } while(!q.empty()){ auto [x, y] = q.front(); q.pop(); for (int i=0; i<4; i++) { int nx = x + di[i]; int ny = y + dj[i]; if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue; if (vis[nx][ny] || a[nx][ny] == '#') continue; d[nx][ny] = d[x][y] + 1; vis[nx][ny] = true; q.push({nx, ny}); } if (a[x][y] == 'G') break; if (a[x][y] != '.' && a[x][y] != '#' && !visc[a[x][y]-'a']) { visc[a[x][y]-'a'] = true; for(auto [nx, ny]:g[a[x][y]-'a']) { if (vis[nx][ny]) continue; d[nx][ny] = d[x][y] + 1; vis[nx][ny] = true; q.push({nx, ny}); } } } int ans = d[gx][gy]; cout << ((ans)?ans:-1) << endl; return 0; }
#include<bits/stdc++.h> #include<tuple> typedef long long ll; #define INF 9999999999 #define mod 1000000007 #define eps 1e-12 using namespace std; typedef pair<ll,ll>du; ll n,m,a,b,c,d,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},ans=-1; vector<du>edge[200]; queue<du>q; ll dis[2005][2005],gone[2005][2005],done[205]; char s[2005][2005]; int main(){ ll i,j,k; ios::sync_with_stdio(0); cin>>n>>m; for(ll j=1;j<=n;j++) cin>>&s[j][1]; for(ll i=0;i<=200;i++) done[i]=0; for(ll i=1;i<=n;i++) for(ll j=1;j<=m;j++){ gone[i][j]=0; if(s[i][j]=='S'){ q.push({i,j}); dis[i][j]=0; } else if(s[i][j]>='a'&&s[i][j]<='z') edge[s[i][j]].push_back({i,j}); if(s[i][j]!='S') dis[i][j]=INF; } while(!q.empty()){ tie(a,b)=q.front(); q.pop(); //cout<<a<<" "<<b<<endl; if(s[a][b]=='G'){ ans=dis[a][b]; break; } if(!done[s[a][b]]&&s[a][b]>='a'&&s[a][b]<='z'){ for(auto k:edge[s[a][b]]){ tie(c,d)=k; if(!gone[c][d]) q.push({c,d}),gone[c][d]=1,dis[c][d]=dis[a][b]+1; } done[s[a][b]]=1; } for(ll i=0;i<4;i++){ c=a+dx[i],d=b+dy[i]; if(c<=0||c>n||d<=0||d>m||s[c][d]=='#') continue; if(!gone[c][d]) q.push({c,d}),gone[c][d]=1,dis[c][d]=dis[a][b]+1; } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define all(c) c.begin(), c.end() #define rep(i, n) for (int i = 0; i < n; ++i) #define rep2(i, a, b) for (int i = a; i <= b; ++i) #define rep3(i, a, b) for (int i = a; i >= b; --i) #define endl '\n' #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; void YES(bool t = 1) { cout << YESNO[t] << endl; } void Yes(bool t = 1) { cout << YesNo[t] << endl; } void yes(bool t = 1) { cout << yesno[t] << endl; } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ IN(name) #define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ IN(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); } template <class T> void scan(vector<T> &); template <class T> void scan(vector<T> &a) { for (auto &i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template <class T> vector<T> divisor(T x) { vector<T> ans; for (T i = 1; i * i <= x; i++) if (x % i == 0) { ans.pb(i); if (i * i != x) ans.pb(x / i); } return ans; } using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; typedef long long int ll; typedef long double ld; typedef pair<int, int> pi; typedef vector<int> vi; #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // constexpr int inf = 1e9; // constexpr i64 inf = 1e18; // const int N = 500 * 1000 + 5; // use for N <= 5 * 10^5 // const int MX = 1e9 + 7; // For convenience, find the answer modulo 10^9+7 // cout << rng(); int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); INT(h, w); vector<string> s(h); rep(i, h) { cin >> s[i]; } int ans = 0; rep2(i, 1, h - 2) { rep2(j, 1, w - 2) { if (s[i][j] == '#') { // for R and L if (s[i][j - 1] == '.') { ans += 1; if (s[i - 1][j] == '#' and s[i - 1][j - 1] == '.') { ans -= 1; } } if (s[i][j + 1] == '.') { ans += 1; if (s[i - 1][j] == '#' and s[i - 1][j + 1] == '.') { ans -= 1; } } if (s[i - 1][j] == '.') { ans += 1; if (s[i][j - 1] == '#' and s[i - 1][j - 1] == '.') { ans -= 1; } } if (s[i + 1][j] == '.') { ans += 1; if (s[i][j - 1] == '#' and s[i + 1][j - 1] == '.') { ans -= 1; } } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 1e9 + 7; template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; for(int i = 0; i < v.size(); ++i) { if (i) os << ","; os << v[i]; } os << "}"; return os; } void solve() { ll H, W; cin >> H >> W; vector<string> grid(H); for(string &row: grid) { cin >> row; } vector<vector<char>> c = vector<vector<char>>(H, vector<char>(W, '.')); ll ans = 0; for(ll i = 0; i < H - 1; ++i) { for(ll j = 0; j < W - 1; ++j) { ll count = 0; if(grid[i][j] == grid[i+1][j]) count++; if(grid[i][j] == grid[i][j+1]) count++; if(grid[i][j] == grid[i+1][j+1]) count++; if(count == 0 || count == 2) { ans++; c[i][j] = 'C'; } } } // for(auto r: c) { // for(char x: r) cout << x << ""; // cout << "\n"; // } cout << ans; } signed main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin >> t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define s(x, n) memset(x, n, sizeof(x)); #define pb(x) push_back(x) #define NIL -1 #define MAXN 11 void solve() { ll n, a, b; cin >> n >> a >> b; if (a + b > n) cout << "0\n"; else { ll temp = (n - a - b) % mod; ll ans = (temp + 1) % mod * (temp + 2) % mod * (2 * (n - a + 1) % mod * (n - b + 1) % mod - (temp + 1) % mod * (temp + 2) % mod + mod) % mod; cout << ans << "\n"; } } int main() { #ifdef WIN32 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif #ifndef WIN32 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif int t; t = 1; cin >> t; for (int I = 1; I <= t; I++) { solve(); } return 0; }
typedef long long ll; typedef long double ld; #include <bits/stdc++.h> using namespace std; int main() { ll t; const ll MOD = 1e9+7; std::cin >> t; for (int i = 0; i < t; i++) { ll n,a,b; std::cin >> n>>a>>b; if(a>b)swap(a,b); ll ma = min(a-1,n-b); ll mm = 0; mm += 2*ma*(1 - 2*b - ma + 2*n)%MOD*(1 - b + n)%MOD*(b-a+1); // std::cout << mm << std::endl; mm += ma*ma%MOD*(-2*b-ma+2*n+1)%MOD*(-2*b-ma+2*n+1)%MOD; mm += (b-a+1)%MOD*(b-a+1)%MOD*(n-b+1)%MOD*(n-b+1)%MOD; mm %= MOD; // std::cout << (n-a+1)*(n-a+1)*(n-b+1)*(n-b+1) << std::endl; // std::cout << mm << std::endl; std::cout << ((n-a+1)%MOD*(n-a+1)%MOD*(n-b+1)%MOD*(n-b+1)%MOD-mm+MOD)%MOD << std::endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long LL; void debug_out(){ cerr << endl; } template<typename Head, typename... Tail> void debug_out(Head H, Tail... T){ cerr << " " << to_string(H); debug_out(T...); } #ifdef local #define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__) #else #define debug(...) 55 #endif #define all(x) x.begin(), x.end() const int N = 2e5 + 5; vector<int> E[N]; int dep[N], ans[N], mx[N], t; void dfs(int u, int pre){ mx[u] = dep[u]; for(int& v: E[u]){ if(v == pre) continue; dep[v] = dep[u] + 1; dfs(v, u); mx[u] = max(mx[u], mx[v]); } } void dfs1(int u, int pre){ sort(all(E[u]), [&](int x, int y){ return mx[x] < mx[y]; }); ans[u] = ++t; for(int& v: E[u]){ if(v != pre) dfs1(v, u); } ++t; } int main(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; cin >> n; for(int i = 1, u, v; i < n; i++){ cin >> u >> v; E[u].push_back(v); E[v].push_back(u); } dfs(1, 0); int x = 1; for(int i = 1; i <= n; i++) if(dep[x] < dep[i]) x = i; dep[x] = 0; dfs(x, 0); dfs1(x, 0); for(int i = 1; i <= n; i++) cout << ans[i] << ' '; return 0; }
#include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; int qmax(int a, int b){ return (a > b) ? a : b; } int n; vector<int> v[200200]; vector<int> ord; int ans[200200]; int nowans = 1; int pos[200200]; int dept[200200]; int mx_dept[200200]; bool cmp(int a, int b){ return (mx_dept[a] < mx_dept[b]); } void get_dept(int now, int par){ mx_dept[now] = dept[now]; rep(i, (int)v[now].size()){ if(v[now][i] != par){ dept[v[now][i]] = dept[now] + 1; get_dept(v[now][i], now); mx_dept[now] = qmax(mx_dept[now], mx_dept[v[now][i]]); } } } int rt; void dfs(int now, int par){ ans[(int)ord.size()] = nowans; ord.push_back(now); sort(v[now].begin(), v[now].end(), cmp); rep(i, (int)v[now].size()){ if(v[now][i] != par){ nowans++; dfs(v[now][i], now); } } nowans++; } int main(){ ios::sync_with_stdio(false); cin >> n; rep(i, n - 1){ int a, b; cin >> a >> b; a--, b--; v[a].push_back(b); v[b].push_back(a); } get_dept(0, -1); rt = 0; rep(i, n) if(dept[i] > dept[rt]) rt = i; memset(dept, 0, sizeof(dept)), memset(mx_dept, 0, sizeof(mx_dept)); get_dept(rt, -1); dfs(rt, -1); rep(i, n) pos[ord[i]] = ans[i]; rep(i, n) cout << pos[i] << " "; cout << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;} const char newl = '\n'; int main() { int h,w; cin >> h >> w; vector<string> s(h); for (int i = 0;i < h;++i) cin >> s[i]; int ans = 0; for (int i = 0;i < h;++i) for (int j = 0;j < w-1;++j) if (s[i][j] == '.' && s[i][j+1] == '.') ans++; for (int i = 0;i < h-1;++i) for (int j = 0;j < w;++j) if (s[i][j] == '.' && s[i+1][j] == '.') ans++; cout << ans << newl; }
#include<bits/stdc++.h> #define all(x) (x).begin(),(x).end() #define ll long long #define P pair<int,int> #define rep(i,n) for(int i = 0; i < int(n); i++) #define vi vector<int> using namespace std; const int INF = 1001001001; const int MOD = 1e9+7; template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; } template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; } const int dx[] = {0,1,0,-1}; const int dy[] = {1,0,-1,0}; int main(){ cin.tie(0), ios::sync_with_stdio(false); int h,w; cin >> h >> w; vector<string> s(h); rep(i,h) cin >> s[i]; ll ans = 0; rep(i,h){ rep(j,w){ rep(k,4){ if(j+dx[k]<0 || w<=j+dx[k] || i+dy[k]<0 || h<=i+dy[k]) continue; if(s[i][j] == '.' && s[i+dy[k]][j+dx[k]] == '.') ans++; } } } cout << ans/2; cout << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define pb push_back #define ld long double #define sz size() #define lala for(ll i=0;i<n;i++) #define pll pair<ll,ll> #define ed "\n" #define ct cout #define m_p make_pair #define vi vector<ll> #define vc vector<char> #define ff first #define ss second using namespace std; void bahut_tezz() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void jai_bajrangbali() { ll k,n,m,x,y,cnt=0,res=0,mx=1; ll sum=0; bool ash=false; string s; cin>>n; ll a[n]; lala { cin>>a[i]; } sort(a,a+n); // x=upper_bound(a,a+n,a[n-2])-a+1-1; // ct<<x<<ed; for(ll i=0;i<n-1;i++) { x=upper_bound(a,a+n,a[i])-a; cnt=cnt+abs(n-x); } ct<<cnt<<ed; } int main() { bahut_tezz(); long long t=1; //cin>>t; while(t--)jai_bajrangbali(); return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define dump(...) #endif #define endl '\n' //#define int long long #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define INF 2147483647 #define LLINF 9223372036854775807LL #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define BIT(n) (1LL << (n)) #define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper) #define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower) #define FILLZERO(s, n) \ do { \ ostringstream os; \ os << setw(n) << setfill('0') << s; \ s = os.str(); \ } while (0); #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと using ll = long long; using vi = vector<int>; using vvi = vector<vector<int>>; using pi = pair<int, int>; template <typename T> using Pq = priority_queue<T>; template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>; template <typename T> vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); } template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } 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> bool isProductOverflow(T a, T b) { return (a > numeric_limits<T>::max() / b); } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } __attribute__((constructor)) void ioInit() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << std::fixed << std::setprecision(10); } constexpr ll MOD = 1e9 + 7; // constexpr ll MOD = 998244353; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z; string S, T; ll ans = 0; long long ceilSqrt(long long n) { ll res = sqrt(n) - 1; while ((res + 1) * (res + 1) <= n) res++; return res; } signed main() { cin >> N >> M; vi a(N); cin >> a; vvi adj(N); REP(i, M) { cin >> X >> Y; X--, Y--; adj[X].push_back(Y); } vi b(N, INF); b[0] = INF; REP(i, N) { for (auto j : adj[i]) { chmin(b[j], min(a[i], b[i])); } } ans = -INF; REP(i, N) { chmax(ans, (ll)a[i] - b[i]); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen(".in","r",stdin); //freopen(".out","w",stdout); int n; cin >> n; int ans = n / 100; if ( n % 100 ) ans ++; cout << ans; }
#include<iostream> #include<cstdio> using namespace std; int n; int main() { scanf("%d",&n); if(n >= 0) printf("%d\n",n); else printf("0\n"); return 0; }
#include <bits/stdc++.h> using namespace std; #define FASTIO ios::sync_with_stdio(false);cin.tie(NULL) #define pb push_back #define ub upper_bound #define lb lower_bound #define F(i, a, b) for (int i = a; i < b; ++i) #define R(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef pair<int,int> pii; int main() { FASTIO; string a,b; cin >> a >> b; int s1=0,s2=0; F(i,0,3){ s1+=a[i]-'0'; } F(i,0,3){ s2+=b[i]-'0'; } cout << max(s1,s2); return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; const ll MOD = 1000000007; ll divceil(ll x, ll y) { return(x % y ? x / y + 1 : x / y); } ll invMOD(ll x) { ll b = MOD, u = 1, v = 0; while (b) { ll t = x / b; x -= t * b; swap(x, b); u -= t * v; swap(u, v); } u %= MOD; if (u < 0) u += MOD; return u; } int main() { string A, B; cin >> A >> B; cout << max(A[0] + A[1] + A[2], B[0] + B[1] + B[2]) - '0' * 3 << "\n"; }
#include <iostream> #include <iomanip> #include <algorithm> #include <assert.h> #include <complex> #include <utility> #include <vector> #include <string> #include <stack> #include <queue> #include <tuple> #include <cmath> #include <bitset> #include <cctype> #include <set> #include <map> #include <unordered_map> #include <numeric> #include <functional> #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(ll i=a;i<b;++i) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define _rrep(i,a) rrepi(i,a,0) #define rrepi(i,a,b) for(ll i=a-1;i>=b;--i) #define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__) #define all(x) (x).begin(),(x).end() #define PRINT(V) cout << V << "\n" #define SORT(V) sort((V).begin(),(V).end()) #define RSORT(V) sort((V).rbegin(), (V).rend()) using namespace std; using ll = long long; 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; } inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); } template<class itr> void cins(itr first,itr last){ for (auto i = first;i != last;i++){ cin >> (*i); } } template<class itr> void array_output(itr start,itr goal){ string ans = "",k = " "; for (auto i = start;i != goal;i++) ans += to_string(*i)+k; if (!ans.empty()) ans.pop_back(); PRINT(ans); } ll gcd(ll a, ll b) { return a ? gcd(b%a,a) : b; } const ll INF = 1e18; const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll MOD3 = 1e6; const ll EPS = 1e-10; int sgn(const double a){ return (a < -EPS ? -1 : (a > EPS ? +1 : 0)); } typedef pair<int,int> pi; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> tri; typedef pair<double,double> point; typedef complex<double> Point; const ll MAX = 1000005; constexpr ll nx[4] = {-1,0,1,0}; constexpr ll ny[4] = {0,1,0,-1}; ll m; //m次正方行列 vector<ll> matmul(vector<ll> &dp,vector<vector<ll>> &mt){ vector<ll> ret(m,0); rep(i,m) rep(j,m) (ret[i] += mt[i][j]*dp[j]%MOD)%=MOD; return ret; } vector<vector<ll>> update(vector<vector<ll>> &mt){ vector<vector<ll>> ret(m,vector<ll>(m,0)); rep(i,m) rep(j,m) rep(k,m) (ret[i][j] += mt[i][k]*mt[k][j]%MOD)%=MOD; return ret; } void matpow(vector<ll> &dp,vector<vector<ll>> &mt,ll k){ m = dp.size(); while(k){ if(k&1) dp = matmul(dp,mt); mt = update(mt); k /= 2; } } ll mod_pow(ll n, ll p, ll m){ ll ret = 1; ll mul = n%m; while(p){ if (p&1) ret = ret*mul%m; p >>= 1; mul = mul*mul%m; } return ret; } class Combination{ public: ll MAX,M; vector<ll> fac,finv,inv; Combination(ll MAX_,ll MOD_):MAX(MAX_),M(MOD_){ fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; rep(i,2,MAX){ fac[i] = fac[i-1]*i%M; inv[i] = M-inv[M%i]*(M/i)%M; finv[i] = finv[i-1]*inv[i]%M; } } ll COM(ll n,ll k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n]*(finv[k]*finv[n-k]%M)%M; } }; int main(){ Combination com(100000,MOD); ll n,k; //edge cin >> m >> n >> k; vector<ll> a(m); cins(all(a)); vector<vector<ll>> mat(m,vector<ll>(m,0)),g(m); rep(i,m) mat[i][i] = n; rep(i,n){ ll x,y; cin >> x >> y; x--,y--; g[x].push_back(y); g[y].push_back(x); } rep(i,m){ ll cnt = 0; for (ll v:g[i]){ mat[i][v]++; cnt++; } mat[i][i] += n-cnt; } rep(i,m){ //array_output(all(mat[i])); } ll re = com.inv[2*n],mul = mod_pow(re,k,MOD); matpow(a,mat,k); rep(i,m){ PRINT(a[i]*mul%MOD); } }
#include <bits/stdc++.h> using namespace std; #define reps(i, a, n) for (int i = (a); i < (n); ++i) #define rep(i, n) reps(i, 0, n) #define deps(i, a, n) for (int i = (a); i >= (n); --i) #define dep(i, n) deps(i, n, 0) #define inf 2147483647 #define int long long int n, m; int a[105], b[105]; int k; int c[20], d[20]; int ans = 0; void func(int i, int s[]) { int ans2 = 0; rep (j, m) { if (s[a[j]] >= 1 && s[b[j]] >= 1) ans2++; } ans = max(ans, ans2); if (i >= k) return; s[c[i]]++; func(i + 1, s); s[c[i]]--; s[d[i]]++; func(i + 1, s); s[d[i]]--; } signed main(void) { cin >> n >> m; rep (i, m) cin >> a[i] >> b[i]; cin >> k; rep (i, k) cin >> c[i] >> d[i]; int s[105] = {0}; func(0, s); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; std::pair<int, std::vector<int>> tree_diameter(const std::vector<std::vector<int>>& G) { std::vector<int> to(G.size()); auto dfs = [&](const auto& dfs, int v, int p) -> std::pair<int, int> { std::pair<int, int> ret(0, v); for (int c : G[v]) { if (c == p) continue; auto weight = dfs(dfs, c, v); ++weight.first; if (ret < weight) { ret = weight; to[v] = c; } } return ret; }; auto p = dfs(dfs, 0, -1); auto q = dfs(dfs, p.second, -1); std::vector<int> path; int v = p.second; while (v != q.second) { path.push_back(v); v = to[v]; } path.push_back(v); return {q.first, path}; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> G(N); for (int i = 0; i < N - 1; ++i) { int A, B; cin >> A >> B; --A, --B; G[A].push_back(B); G[B].push_back(A); } vector<int> path = tree_diameter(G).second; vector<int> nxt(N, -1), ans(N); for (int i = 0; i < path.size() - 1; ++i) nxt[path[i]] = path[i+1]; int k = 1; auto dfs = [&](auto& dfs, int v, int p) -> void { ans[v] = k; for (int c : G[v]) { if (c == p || c == nxt[v]) continue; k++; dfs(dfs, c, v); } if (nxt[v] != -1) { k++; dfs(dfs, nxt[v], v); } k++; }; dfs(dfs, path[0], -1); for (int i = 0; i < N; ++i) cout << ans[i] << (i < N - 1 ? " " : "\n"); }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) using namespace std; const int N = 200111; int n, s, t, sub[N], ans[N], e = 1; vector<int> g[N]; pair<int, int> dfs(int v, int p) { pair<int, int> a = {0, v}; for (auto u : g[v]) { if (u != p) { auto b = dfs(u, v); b.first++; a = max(a, b); } } return a; } void go(int v, int p) { sub[v] = (v == t); for (auto u : g[v]) if (u != p) { go(u, v); sub[v] |= sub[u]; } } void solve(int v, int p) { ans[v] = e; sort(g[v].begin(), g[v].end(), [&](int a, int b) { return sub[a] < sub[b]; }); for (auto u : g[v]) if (u != p) { e++; solve(u, v); } e++; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n; rep(i, 1, n - 1) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } s = dfs(1, 0).second; t = dfs(s, 0).second; go(s, 0); solve(s, 0); rep(i, 1, n) cout << ans[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 998244353; ll power(ll a, ll x) { if(x == 0) return 1; if(x == 1) return a; if(x % 2 == 0) return (power(a, x / 2) * power(a, x / 2)) %mod; else return (a * power(a, x - 1)) % mod; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); ll n, m, k, ans = 0; cin >> n >> m >> k; if(n == 1) cout << power(k, m); else if(m == 1) cout << power(k, n); else if(n <= m) { for(int i = 1; i <= k; i++) ans=(ans+((power(i, n)-power(i-1, n))%mod)*power((k-i+1), m))%mod; ans=(ans+mod)%mod; cout << ans; } else { for(int i = 1; i <= k; i++) ans=(ans+((power(i, m)-power(i-1, m))%mod)*power((k-i+1), n))%mod; ans=(ans+mod)%mod; cout << ans; } }
#include <bits/stdc++.h> #include <climits> #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++) #define si(x) int x = scanInt(); #define sll(x) LL x = scanLong(); #define sci(x) int x; scanf("%d", &x); #define scll(x) LL x; scanf("%lld", &x); #define pi(x) printf("%d ", x) #define pll(x) printf("%lld ", x) #define nl printf("\n") #define clr(a) memset(a, 0, sizeof(a)) #define PB push_back #define MP make_pair using namespace std; typedef unsigned int UI; // 32 bit integer typedef long int LI; // 32 bit integer typedef unsigned long int ULI; // 32 bit unsigned integer typedef long long int LL; // 64 bit integer typedef unsigned long long int ULL; // 64 bit unsigned integer typedef long double LD; typedef vector<int> VI; typedef vector<LL> VLL; typedef deque<int> DI; typedef deque<LL> DLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; const LL MOD = 998244353; /* Fast I/O */ inline int scanInt() { int n = 0; char ch = getchar(); int sign = 1; while(ch < '0' || ch > '9') { if(ch == '-') sign = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { n = (n<<1)+(n<<3)+(int)(ch-'0'); ch = getchar(); } return n*sign; } inline LL scanLong() { LL n = 0; char ch = getchar(); LL sign = 1; while(ch < '0' || ch > '9') { if(ch == '-') sign = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { n = (n<<1)+(n<<3)+(LL)(ch-'0'); ch = getchar(); } return n*sign; } LL modpow(LL a, LL n, LL m) { LL r = 1; while(n != 0) { if(n % 2 == 1) r = (r * a) % m; a = (a * a) % m; n >>= 1; } return r; } int main() { sll(n); sll(m); sll(k); if(n == 1 && m == 1) { pll(k); nl; return 0; } if(n == 1) { pll(modpow(k, m, MOD)); nl; return 0; } if(m == 1) { pll(modpow(k, n, MOD)); nl; return 0; } LL ans = 0; rep(x, 1, k+1) { ans += (((modpow(x, n, MOD) - modpow(x-1, n, MOD) + MOD) % MOD) * modpow(k-x+1, m, MOD)) % MOD; ans %= MOD; } pll(ans); nl; }
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <numeric> #include <functional> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for(ll i = 0; i < n; i++) #define exrep(i, a, b) for(ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; vvl G; vl visited; vvl S; // S[v] : 頂点vに辿り着ける頂点集合 void dfs(ll sv, ll v) { // 行きの処理はこのへんに書く S[v].pb(sv); visited[v] = 1; for(ll u : G[v]) { if(!visited[u]) { dfs(sv, u); } // 帰りの処理はこのへんに書く } } int main() { ll n; cin >> n; G.resize(n); rep(i, n) { string s; cin >> s; rep(j, n) { if(s[j] == '1') { G[i].pb(j); } } } S.resize(n); rep(sv, n) { visited.assign(n, 0); dfs(sv, sv); } double ans = 0.0; rep(v, n) { ans += 1.0/(double)S[v].size(); } exout(ans); re0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for(int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; // using namespace atcoder; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int main() { int h, w, x, y; cin >> h >> w >> x >> y; x--; y--; string s[h]; rep(i,h) cin >> s[i]; int ans = 1; srep(j,y+1,w){ if(s[x][j] == '#') break; ans++; } drep(j,y){ if(s[x][j] == '#') break; ans++; } srep(i,x+1,h){ if(s[i][y] == '#') break; ans++; } drep(i,x){ if(s[i][y] == '#') break; ans++; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),i##z=(b);i<=i##z;i++) #define ROF(i,a,b) for(int i=(a),i##z=(b);i>=i##z;i--) #define temT template<typename T> using namespace std; typedef long long ll; const int N=int(5e5)+10; int n; ll res; char s[N],t[N]; bool flag=1; char rd(){ char t=getchar(); while(!isdigit(t)) t=getchar(); return t; } void gn(int &x){//get next for(x++;x<=n && s[x]=='0';x++); } int k2(int &x){ int p1,p2; p1=x,gn(x); p2=x,gn(x); res+=p2-p1; return (p1<=n && p2>n)?-1:1; } int main(){ int tmp=scanf("%d",&n); FOR(i,1,n) s[i]=rd(); FOR(i,1,n) t[i]=rd(); int p=0; gn(p); FOR(i,1,n) if(t[i]=='1'){ while(p<i) if(k2(p)<0) flag=0; if(p>n) flag=0; res+=p-i,gn(p); } while(p<=n) if(k2(p)<0) flag=0; cout<<(flag?res:-1); return 0; }
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 4000000000000000000 //オーバーフローに注意 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; int main(){ //ll num = sqrt(HUGE_NUM/2+100); //printf("num:%lld\n",num); ll N; scanf("%lld",&N); ll left = 0,right = 1414213562,mid = (left+right)/2; ll maxi = -1; while(left <= right){ if((mid*(mid+1)/2) <= N+1){ maxi = mid; left = mid+1; }else{ right = mid-1; } mid = (left+right)/2; } printf("%lld\n",N-maxi+1); return 0; }
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <iomanip> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_map> #include <bitset> #include <chrono> #include <random> #define rep(i,n) for(int i=0;i<n;i++) #define repn(i,n) for(int i=1;i<=n;i++) #define repr(e,x) for(auto& e:x) using namespace std; typedef long long ll; typedef long double ld; // typedef pair<int,int> P; // typedef pair<int,P> IP; // typedef pair<P,P> PP; double const PI=3.141592653589793; int const INF=1001001001; ll const LINF=1001001001001001001; ll const MOD=1000000007; int N; int A[100000]; double f(double x){ double res=0; rep(i,N){ res+=A[i]-min(double(A[i]),2*x); } res/=N; res+=x; return res; } int main(){ cin>>N; rep(i,N) cin>>A[i]; double l=0, r=1e9+1; double diff=1e9+1; while(r-l>1e-7){ // cout<<setprecision(16)<<l<<' '<<r<<endl; double x1=l+(r-l)/3; double x2=r-(r-l)/3; if(f(x1)>f(x2)) l=x1; else r=x2; } cout<<setprecision(20)<<f(l)<<endl; return 0; }
#include <bits/stdc++.h> #define INF 1e9 #define eps 1e-9 typedef long long ll; using namespace std; int n, a[1000010], pos; ll sum[100010], ans = 4e18; double L, R; int main(){ cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); if(n != 1){ for(int i = n; i >= 1; i--) sum[i] = sum[i + 1] + a[i]; for(int i = 1; i < n; i++){ if(i * 2 <= n) ans = min(ans, 2ll * sum[i + 1] + 1ll * a[i + 1] * (i + i - n)); else ans = min(ans, 2ll * sum[i + 1] + 1ll * a[i] * (i + i - n)); } printf("%.12Lf\n", 0.5 * ans / (long double)n); } else{ printf("%.12Lf\n", (long double)a[1] / 2.0); } return 0; }
#include <iostream> #include <vector> using namespace std; void chmin(int& a, int b){ if(a > b) a = b; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> idx(N, -1); int ans = N; for(int i = 0; i < N; i++){ int A; cin >> A; if(i - idx[A] > M) chmin(ans, A); idx[A] = i; } for(int i = 0; i < ans; i++) if(N - idx[i] > M){ cout << i << endl; return 0; } cout << ans << endl; }
#include <cstdio> #include <cmath> #include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #include <deque> #include <stack> #include <tuple> #include <bitset> using namespace std; const int INF = 1000000001; const int MAX_N = 2000000 + 10; int A[MAX_N]; int s[MAX_N]; //set int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; for (int i = 0; i < N; i++) cin >> A[i]; int mex; for (int i = 0; i < M; i++) s[ A[i] ]++; for (int i = 0; i < N; i++) { if(s[i] == 0) { mex = i; break; } } int ans = mex; for (int i = 1; i <= N - M; i++) { int mex_prev = mex; s[A[i-1]]--; if(s[A[i-1]] == 0) mex = min(A[i-1], mex); s[A[i+M-1]]++; if(s[A[i+M-1]] == 1) { if(A[i+M-1] > mex) mex = mex; else if (A[i+M-1] == mex) mex = mex_prev; else mex = mex; } ans = min(ans, mex); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define fur(i,n,m,k) for(int i = (n) ; i < (m) ; i += k) #define fr(i,n,m,k) for(int i = (n - 1) ; i >= (m) ; i -= k) #define pub push_back #define pop pop_back #define Sz size() #define F first #define lob lower_bound #define S second #define B begin() #define E end() typedef pair<int,int> pii; typedef long long ll; typedef pair<ll,ll> pll; typedef long double ld; typedef vector<ll> vll; const int N=4e5+5; ll n, t, ba[N], tol, ans[N], a[N], b, x, k, i; pll e[N]; vll adj[N]; bool mk[N]; deque<ll> q; inline void dfs(ll u=q.front()){ mk[u]=1; q.pop(); for(ll i=0; i<adj[u].size(); i++){ ll v=adj[u][i]; if(mk[v]==0){ a[v]+=a[u]; if(mk[v])q.push_front(v); else q.pub(v); dfs(); } } } inline void find_ba(ll u){ mk[u]=1; for(ll i=0; i<adj[u].size(); i++){ ll v=adj[u][i]; if(mk[v]==0){ find_ba(v); ba[v]=u; } } } int main(){ cin>>n; for(i=1; i<n; i++){ cin>>e[i].F>>e[i].S; adj[e[i].F].pub(e[i].S); adj[e[i].S].pub(e[i].F); } cin>>k; find_ba(1); while(k--){ cin>>t>>i>>x; if(t==1){ if(e[i].F==ba[e[i].S]){ tol+=x; a[e[i].S]-=x; } else{ a[e[i].F]+=x; } } else{ if(e[i].S==ba[e[i].F]){ tol+=x; a[e[i].F]-=x; } else{ a[e[i].S]+=x; } } } fill(mk, mk+n+1, 0); q.pub(1); dfs(); for(ll i=1; i<=n; i++){ cout<<a[i]+tol<<endl; } }
#include <bits/stdc++.h> #include <vector> # include <cstdint> #define FOR(i,l,r) for(int i=(l);i<(r);++i) #define RFOR(i,l,r) for(int i=(l);i>=(int)(r);i--) #define rep(i,n) FOR(i,0,n) #define rrep(i,n) RFOR(i,n-1,0) #define int long long using namespace std; const int MX = 1e6; const int inf = 1e13; const int mod = 1e9+7; #define ll long long struct UnionFind { vector<int> d; UnionFind(int n): d(n, -1) {} int root(int x) { if(d[x] < 0) return x; return d[x] = root(d[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if(x == y) return false; if(d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) {return (root(x) == root(y));} int size(int x) {return -d[root(x)];} }; signed main() { int n,q; cin>>n>>q; vector<int> c(n); rep(i,n)cin>>c[i]; UnionFind uf(n); vector<map<int,int> > mps(n); rep(i,n) { mps[i][c[i]]=1; } rep(i,q) { int x,a,b; cin>>x>>a>>b; if(x==1) { a--; b--; if(uf.same(a,b)) continue; // if(mps[uf.root(a)].size()<=mps[uf.root(b)].size()) { // for(auto key:mps[uf.root(a)]) { // mps[uf.root(b)][key.first]+=key.second; // } // mps[uf.root(a)]=mps[uf.root(b)]; // }else { // for(auto key:mps[uf.root(b)]) { // mps[uf.root(a)][key.first]+=key.second; // } // mps[uf.root(b)]=mps[uf.root(a)]; // } if(uf.size(uf.root(b))>uf.size(uf.root(a))) swap(a,b); for(auto key:mps[uf.root(b)]) { mps[uf.root(a)][key.first]+=key.second; } uf.unite(uf.root(a),uf.root(b)); }else { a--; cout<< mps[uf.root(a)][b] <<endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; #define vi vector<int> #define ff first #define ss second #define pb push_back #define mp make_pair #define pi 3.141592653589793238 #define rep(i,a,b) for (int i = a; i <= b; i++) #define zip(i,a,b) for(int i=a;i<b;i++) #define rzip(i,a,b) for(int i=a;i>=b;i--) #define ll unsigned int #define test int t;cin>>t; while(t--) #define en '\n' typedef pair<int, int> pii; typedef pair<char, int> pci; typedef pair<char, char> pcc; typedef vector<pii> vii; typedef long double ld; #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define sz(x) (int)x.size() #define INF (1e18+5) #define inf (1e9+5) #define mod 1000000007 void __print(int x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} // You should only debug a pair of simple data types. For example, // the debug won't work if one of pair's elements is collection type // (std::vector, std::map, std::set...). template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // bool check_prime(int n) return whether a number is prime or not O(sqrt(N)); // int BE(int x,int n,int m) return x^n%m; O(logN); // void sieve() from number 1-1000001 saare prime store kar lega in bool is_prime[1000001] array // vector<int> z_array(string s) return vector which is Z-array of string s; // vector<int> lps(string s) return vector which is lps array of string s; // int power(int x,int n) return x^n; O(logN); // using_ordered_set (template for using ordered set , replace first 2 lines of this page with this code..); const int gf = 1e6 + 9; vi v[gf]; int sze[gf], parent[gf]; map<int, int>m1[gf]; void make_set(int v) { parent[v] = v; sze[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_set(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (sze[a] < sze[b]) swap(a, b); parent[b] = a; sze[a] += sze[b]; for (auto it : m1[b]) { m1[a][it.ff] += m1[b][it.ff]; } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE if (fopen("input.txt", "r")) { freopen ("input.txt" , "r" , stdin); freopen ("output.txt" , "w" , stdout); } #endif //_______________________________-code starts-_______________________________________________ int n, q; cin >> n >> q; rep(i, 1, n) { make_set(i); } int c[n + 1]; rep(i, 1, n) { cin >> c[i]; m1[parent[i]][c[i]]++; v[c[i]].pb(i); } vi vec; zip(i, 0, q) { int lol; cin >> lol; if (lol == 1) { int a, b; cin >> a >> b; if (find_set(a) != find_set(b)) { union_set(a, b); } } else { int x, y; cin >> x >> y; cout << m1[find_set(x)][y] << en; } } return 0; }
#include<bits/stdc++.h> #define vi vector<int> #define vb vector<bool> #define vs vector<string> #define vvi vector<vector<int>> #define vpi vector<pair<int,int>> #define pb push_back #define mp make_pair #define int long long int #define mod 1000000007 #define from(i,n) for(int i=0;i<(int)n;i++) #define from_j(i,j,n) for(int i=j;i<(int)n;i++) using namespace std; int power(int n , int k) { if (k == 0)return 1; int temp = power(n, k / 2); temp = (temp * temp); if (k % 2) return (temp * n); return temp; } int fact(int x) { int res = 1; for(int i=2;i<=x;i++) res = i*res; return res; } int choose(int n, int r) { return fact(n)/(fact(r)*fact(n-r)); } int gcd(int a, int b) { if (min(a, b) == 0) return max(a, b); return gcd(min(a, b), max(a, b) % min(a, b)); } int lcm(int a, int b) { int x = gcd(a, b); return abs(a*b) / x; } void sync() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); /* #ifndef ONLINEJUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif */ } int32_t main() { sync(); int n, k; cin>>n>>k; vvi T(n, vi (n)); from(i,n) from(j,n) cin>>T[i][j]; vi idx; from(i,n) idx.pb(i); int ans = 0; do { int t = 0; from(i,n) { t += T[idx[i]][idx[(i+1)%n]]; } if(t == k) ans++; } while(next_permutation(idx.begin()+1,idx.end())); cout<<ans; cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
/* * author :Sadik Hassan(_sad_) * */ #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") #include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; #define nl "\n" #define pb push_back #define fi first #define se second #define MP make_pair #define PI (acos(-1.0)) #define rep1(i,n) for(int i=1;i<=n;i++) #define rep(i,n) for(int i=0;i<n;i++) #define SZ(s) (int)s.size() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define w(t) int t;cin>>t;while(t--) #define _SAD() ios::sync_with_stdio(0),cin.tie(0),cout.tie(0),cout<<fixed<<setprecision(12); typedef vector<int> vi; typedef vector<ll> vii; typedef set<int> si; typedef set<ll> sii; /*---------------------------------------------------------------------*/ //freopen("auxiliary.in","r",stdin); //freopen("auxiliary.out","w",stdout); const int N=(int)5e5+5; const ll MOD=(ll)1e9+7; const ll INF=(ll)1e18+5; const ll nn=1e5+5; int main() { _SAD() int n; cin>>n; vii a(n); rep(i,n)cin>>a[i]; rep(i,n) { int x;cin>>x; a[i]*=x; } ll ans=accumulate(all(a),0); if(ans==0)cout<<"Yes"<<nl; else cout<<"No"<<nl; return 0; }
#include "bits/stdc++.h" using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define cool ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long int #define pb push_back #define fe first #define lb lower_bound #define ub upper_bound #define pii pair<pair<int,int>,pair<int,int> > #define se second #define endl "\n" #define pi pair<int, int> #define vi vector<int> #define vvi vector<vi> #define bs binary_search #define rep(i,a,b) for(int i=a;i<b;i++) #define rep1(i,a,b) for(int i=a;i<=b;i++) #define all(c) (c).begin(),(c).end() #define sz(x) (int)x.size() #define PI 3.14159265358979323846 const int N =2e2+10; int mod= 1e9+7; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int po(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = (res%mod * a%mod) % mod; a =(a%mod*a%mod) % mod; b >>= 1; } return res; } vector<pi> adj[N]; int n,m; void solve(){ cin>>n>>m; vector<string > s(n); vector<vi > cost(n,vi (m,INT_MAX)); rep(i,0,n) cin>>s[i]; int sx,sy,gx,gy; rep(i,0,n) { rep(j,0,m) { char c=s[i][j]; if(islower(c)) adj[c].pb({i,j}); else if(s[i][j]=='S') { sx=i; sy=j; } else if(s[i][j]=='G') { gx=i; gy=j; } } } cost[sx][sy]=0; queue<pi> q; q.push({sx,sy}); while(!q.empty()) { pi p=q.front(); q.pop(); int x=p.fe,y=p.se; int nc=cost[x][y]+1; for(int d=0;d<4;d++) { int x2=x+dx[d],y2=y+dy[d]; if(x2<0 || x2>=n || y2<0 || y2>=m || s[x2][y2]=='#') continue; if(cost[x2][y2]>nc) { cost[x2][y2]=nc; q.push({x2,y2}); } } if(islower(s[x][y])) { auto& to=adj[s[x][y]]; for(auto r:to) { if(cost[r.fe][r.se]>nc) { cost[r.fe][r.se]=nc; q.push({r.fe,r.se}); } } to.clear(); } } // rep(i,0,n){ // rep(j,0,m) // cout<<cost[i][j]<<" "; // cout<<endl; // } int ans=cost[gx][gy]; if(ans==INT_MAX) ans=-1; cout<<ans<<endl; } int32_t main() { cool; int t=1; // cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using Vll =vector<ll>; using VVll =vector<vector<ll>>; template <class T> using Vec = vector<T>; template <class T> using VVec = vector<vector<T>>; void Z(){ cout<<"Test"<<endl; } int main(){ ll n,k; cin>>n>>k; cout<<100*k*(n+1)*n/2+n*k*(k+1)/2<<endl; }
#include <bits/stdc++.h> using namespace std; /* Problem B: AtCoder Condominium */ typedef long long ll; void solve(){ int n; int k; scanf("%d%d", &n, &k); ll answer = 0; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= k; ++j){ answer += (100*i+j); } } printf("%lld\n", answer); } int main(){ solve(); }
#include <bits/stdc++.h> using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int N, M, K; vector<int> A, B, C, D; cin >> N >> M; A.resize(M); B.resize(M); rep(i, M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } cin >> K; C.resize(K); D.resize(K); rep(i, K) { cin >> C[i] >> D[i]; C[i]--; D[i]--; } int ans = 0; rep(bit, 1 << K) { int cnt = 0; vector<int> ball(N, 0); rep(i, K) { if (bit & 1 << i) ball[C[i]]++; else ball[D[i]]++; } rep(i, M) if (ball[A[i]] > 0 && ball[B[i]] > 0) cnt++; chmax(ans, cnt); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ostream &operator<<(ostream &os, const vector<lli> &x){ os << "{"; for(int i = 0; i < x.size(); i++){ if(i < x.size()-1) os << x[i] << ", "; else os << x[i]; } os << "}"; return os; } const lli mod = 998244353; template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using mint = ModInt< mod >; int main(void){ int n; cin >> n; vector<lli> x(n); vector<mint> a(n); mint ans = 0; rep(i, n){ cin >> x[i]; } if(n == 1){ ans = x[0]; ans *= x[0]; cout << ans << endl; return 0; } sort(x.begin(), x.end()); rep(i, n) a[i] = x[i]; vector<mint> b(n); mint t = 2; rep(i, n){ b[0] += t.pow(i)*a[i]; } for(int i = 1; i < n; i++){ b[i] = b[i-1]-a[i-1]; b[i]/=2; } rep(i, n-1){ ans += a[i]*b[i+1]; } rep(i, n) ans += a[i]*a[i]; cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int> > #define mk(arr,n,type) type *arr = new type[n] #define fo(i,n) for(i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ld long double #define int long long #define nn '\n' #define w(t) cin>>tc; while(tc--) #define deb(x) cout << #x << "=" << x << endl #define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define ub(v,x) upper_bound(v.begin(),v.end(),x) #define all(x) x.begin(), x.end() #define fill(a, b) memset(a, b, sizeof(a)) #define sortall(x) sort(all(x)) #define tr(it,a) for(auto it = a.begin(); it != a.end(); it++) #define ps(x,y) fixed<<setprecision(y)<<x #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define PI 3.1415926535897932384626 #define inf 1e18 typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<vi> vvi; typedef map<int, int> mii; typedef map<char, int> mci; typedef map<string, int> msi; int mpow(int base, int exp); int lcm(int a, int b); void ipgraph(int m); void dfs(int u, int par); const int mod = 1000000007; const int N = 3e5, M = N; vi g[N]; int getSum(int BITree[], int index) { int sum = 0; while (index > 0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int BITree[], int n, int index, int val) { while (index <= n) { BITree[index] += val; index += index & (-index); } } void convert(int arr[], int n) { int temp[n]; for (int i = 0; i < n; i++) temp[i] = arr[i]; sort(temp, temp + n); for (int i = 0; i < n; i++) { arr[i] = lower_bound(temp, temp + n, arr[i]) - temp + 1; } } int getInvCount(int arr[], int n) { int invcount = 0; convert(arr, n); int BIT[n + 1]; for (int i = 1; i <= n; i++) BIT[i] = 0; for (int i = n - 1; i >= 0; i--) { invcount += getSum(BIT, arr[i] - 1); updateBIT(BIT, n, arr[i], 1); } return invcount; } void sol() { int i, j, k, n, x, y, z, m, tc = 1; string s; while (tc--) { cin >> n; int a[n]; int b[n]; fo(i, n) { cin >> a[i]; b[i] = a[i]; } z = getInvCount(b, n); fo(i, n) { cout << z << nn; k = a[i]; x = n - k - 1; z = z - k + x; } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif sol(); return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; } int lcm(int a, int b) { int g = __gcd(a, b); return a / g * b; } void ipgraph(int m) { int i, u, v; while (m--) { cin >> u >> v; g[u - 1].pb(v - 1); g[v - 1].pb(u - 1); } } void dfs(int u, int par) { for (int v : g[u]) { if (v == par) continue; dfs(v, u); } }
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define INF 0x3F3F3F3F using namespace std; struct point { int x; int y; int z; }Point[17]; int dis(point a, point b) { return abs(b.x - a.x) + abs(b.y - a.y) + max(0, b.z - a.z); } int d[18][18]; int dp[1<<18][18];//x下标为已访问点集,用二进制表示,y下标为当前点,dp表示当前点经过剩余点到原点的距离 int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> Point[i].x >> Point[i].y >> Point[i].z; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) d[i][j] = dis(Point[i], Point[j]); //保存距离的二维数组 for (int i = 0; i < (1 << n); i++) { fill(dp[i], dp[i] + n, INF); } dp[(1 << n) - 1][0] = 0; for (int i = (1 << n) - 2; i >= 0; i--) { //i为访问点集 for (int v = 0; v < n; v++) {//v为当前点 for (int u = 0; u < n; u++) {//u为中间点 dp[i][v] = min(dp[i][v], dp[i | 1 << u][u] + d[v][u]); } } } cout << dp[0][0]; }
#include <iostream> #include <string> #include <bits/stdc++.h> #define endl '\n' #define int long long #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie() using namespace std; signed main(){ //fio; map<int,int>mp; mp[0] = 0; mp[1] = 1; mp[9]= 6; mp[6] = 9; mp[8] = 8; string s; cin >> s; reverse(s.begin(),s.end()); for(int i = 0;i<s.size();i++){ cout << mp[s[i] - '0']; } return 0; }
// Created by ...Rahul Chandra #include <bits/stdc++.h> #define db1(x) cout<<#x<<"="<<x<<'\n' #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n' #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n' #define rep(i,n) for(int i=0;i<(n);++i) #define repA(i,a,n) for(int i=a;i<=(n);++i) #define repD(i,a,n) for(int i=a;i>=(n);--i) using namespace std; using ll = long long; ll n; inline void read() { cin >> n; } inline void solve() { } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long a,b,c,d,e,f,g,rahul=0; string s; cin>>s; for (int i = 0; i < s.length(); ++i) { /* code */ if (s[i]=='6') { s[i]='9'; /* code */ } else if(s[i]=='9'){ s[i]='6'; } else continue; } int n=s.length()-1; for (int i = 0; i <n; ++i) { /* code */ swap(s[i],s[n--]); } cout<<s; return 0; }
#pragma GCC target ("avx2") //#pragma GCC optimize "trapv" #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #define input(a,n) for(ll i1=0;i1<n;i1++)cin>>a[i1] #define ll long long #define pi 2 * acos(0.0) #define usll unordered_set<ll> #define sll set<ll> #define vll vector<ll> #define mll map<ll,ll> #define pll pair<ll,ll> #define umll unordered_map<ll,ll> #define S second #define sz size() #define all(v) v.begin(),v.end() #define Y cout<< "Yes"<< "\n" #define N cout<< "No"<< "\n" #define F first #define mp make_pair #define pb push_back #define pf push_front #define ld long double using namespace std; const ll mod = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif string s; cin>>s; ll n=s.sz,f=1,g=1; for(ll i=0;i<n;i+=2) { if((ll)s[i]<97) { f=0; break; } } for(ll i=1;i<n;i+=2) { if((ll)s[i]>=97) { g=0; break; } } if(f&&g) Y; else N; return 0; }
/* * Created by : imay_10 (Ayush Mishra) * * " It won't happen, until you make it happen !!! " * * Template for CP contests */ #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mxn = 1e6; const int mod1 = 1e9+7; const int mod2 = 998244353; const ll inf = 1e18; const double pi = 3.1415926535897932384626; // Utility for fast exponentiation ll binpow(ll a, ll b){ ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } // Utility for modular exponentiation ll mpow(ll a, ll b, ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } // Utility for modular inverse ll inv(ll a, ll m){ return mpow(a, m-2, m); } // Utility for GCD of two numbers ll gcd(ll a, ll b){ if (b == 0) return a; else return gcd (b, a % b); } // Utility for LCM of two numbers ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } // Utility for binomial coefficient ll ncr(ll n, ll k) { if(n < k) return 0; ll C[n + 1][k + 1]; ll i, j; for(i = 0; i <= n; i++){ for(j = 0; j <= min(i, k); j++){ if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = (C[i-1][j-1] + C[i-1][j]); } } return C[n][k]; } class Solution{ public : void solve(){ // Write your code here string s; cin>>s; ll n=s.length(); int f=0; for(ll i=0;i<n;i++){ if(i%2==0 and s[i]>=97 and s[i]<=122) f=0; else if(i%2==1 and s[i]>=65 and s[i]<=90) f=0; else{ f=1; break; } } if(f==1) cout<<"No"; else cout<<"Yes"; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); clock_t start, end; start = clock(); Solution ob; /* #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif */ // Call the solve() function here ob.solve(); // Call the solve() function with multiple test_cases /* int t; cin>>t; for(int i=1;i<=t;i++){ ob.solve(); cout<<"\n"; } */ end = clock(); double time_taken=double(end-start)/double(CLOCKS_PER_SEC); cerr<<"\nTime : "<<fixed<<time_taken<<setprecision(5)<<" s \n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long color3(const vector<vector<int>> G,vector<int> c){ bool ok; long long ans=0; if(c.size()==G.size()) return 1; for(int i=0;i<3;i++){ ok=1; for(int j:G.at(c.size())){ if(j<c.size()&&c.at(j)==i){ ok=0; break; } } if(ok){ c.push_back(i); ans+=color3(G,c); c.pop_back(); } } return ans; } int main(){ int N,M,A,B; cin >> N >> M; vector<vector<int>> G(N); for(int i=0;i<M;i++){ cin >> A >> B; A--; B--; G.at(A).push_back(B); G.at(B).push_back(A); } long long ans=1; vector<int> done(N,-1); for(int i=0;i<N;i++){ if(done.at(i)>-1) continue; vector<int> VC; queue<int> que; done.at(i)=VC.size(); VC.push_back(i); que.push(i); while(que.size()){ for(int j:G.at(que.front())){ if(done.at(j)>-1) continue; done.at(j)=VC.size(); VC.push_back(j); que.push(j); } que.pop(); } if(VC.size()<3){ ans*=3*VC.size(); continue; } vector<vector<int>> GC(VC.size()); for(int j=0;j<VC.size();j++){ for(int k:G.at(VC.at(j))){ GC.at(j).push_back(done.at(k)); } } ans*=6*color3(GC,{0,1}); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl using lint = long long; typedef pair<int,int> P; const bool debugFlag=true; const lint linf=1e18+7; const lint inf=1e9+7; const int MOD=1000000007; signed main(){ int n,m;cin>>n>>m; vector<set<int>> edge(n); rep(i,m){ int a,b;cin>>a>>b; --a;--b; edge[a].insert(b); edge[b].insert(a); } lint res=0; auto binary_graph=[&](vector<int>const& h,int& sm)->bool{ int hn=h.size(); vector<int> memo(hn,0); vector<vector<int>> edge2(hn); rep(i,hn){ for(int j=i+1;j<hn;++j){ if(edge[h[i]].find(h[j])!=edge[h[i]].end()){ edge2[i].push_back(j); edge2[j].push_back(i); } } } rep(st,hn){ if(memo[st]>0)continue; queue<int> que; que.push(st); ++sm; memo[st]=1; while(!que.empty()){ int no=que.front();que.pop(); for(auto& e:edge2[no]){ if(memo[e]==0){ memo[e]=3^memo[no]; que.push(e); } else if(memo[e]==memo[no])return false; } } } return true; }; rep(bit,(1<<n)){ vector<int> g,h; rep(i,n){ if(bit&(1<<i))g.push_back(i); else h.push_back(i); } bool flag=false; rep(i,g.size()){ for(int j=i+1;j<g.size();++j){ if(edge[g[i]].find(g[j])!=edge[g[i]].end())flag=true; } } if(flag)continue; int cnt=0; if(binary_graph(h,cnt)){ res+=(1<<cnt); //debug(bit); //debug((1<<cnt)); } } cout<<res<<"\n"; return 0; } /* 5 4 1 2 2 3 3 4 4 5 18 */
#include<bits/stdc++.h> using namespace std; struct tree{ int l,r; long long sum,add; }tr[1200000]; long long a[1200000],n,m; void pushup(int u){ tr[u].sum=tr[u*2].sum^tr[u*2+1].sum; } void build(int u,int l,int r){ tr[u].l=l,tr[u].r=r; if(l==r){ tr[u].sum=a[l]; return; } int mid=(l+r)/2; build(u*2,l,mid); build(u*2+1,mid+1,r); pushup(u); } void spread(int u){ if(tr[u].add!=0){ if((tr[u*2].r-tr[u*2].l+1)%2==1)tr[u*2].sum^=tr[u].add; if((tr[u*2+1].r-tr[u*2+1].l+1)%2==1)tr[u*2+1].sum^=tr[u].add; tr[u*2].add^=tr[u].add; tr[u*2+1].add^=tr[u].add; tr[u].add=0; } } void change(int u,int l,int r,int d){ if(l<=tr[u].l&&r>=tr[u].r){ if((tr[u].r-tr[u].l+1)%2==1)tr[u].sum^=d; tr[u].add^=d; return; } spread(u); int mid=(tr[u].l+tr[u].r)/2; if(l<=mid)change(u*2,l,r,d); if(r>mid)change(u*2+1,l,r,d); pushup(u); } long long ask(int u,int l,int r){ if(l<=tr[u].l&&r>=tr[u].r)return tr[u].sum; spread(u); int mid=(tr[u].l+tr[u].r)/2; long long ans=0; if(l<=mid)ans^=ask(u*2,l,r); if(r>mid)ans^=ask(u*2+1,l,r); return ans; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++)cin>>a[i]; build(1,1,n); int as,b,c,d; for(int i=1;i<=m;i++){ cin>>as; if(as==2){ cin>>b>>c; cout<<ask(1,b,c)<<endl; } if(as==1){ cin>>b>>d; change(1,b,b,d); continue; } } return 0; }
#include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define mp make_pair #define ll long long #define pii pair<ll,ll> #define vi vector<ll> #define mii map<ll,ll> #define pqi priority_queue<ll> //max pq #define pqd priority_queue<ll,vi,greater<ll> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define MOD 1000000007 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define pw(b,p) pow(b,p) + 0.1 #define f(i,k,n) for(ll i=k;i<n;i++) #define fd(i,start,end) for(ll i=start;i>=end;i--) ll power(ll a, ll b, ll mod) {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 power(ll a, ll b) {ll res = 1; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = res * a ; a = a * a;} return res;} ll min( ll a, ll b) { return (a < b) ? a : b;} ll max(ll a, ll b) {return (a > b) ? a : b;} ll gcd (ll a, ll b) {if (a == 0) return b; return gcd(b % a, a);} void bwayne() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output2.txt", "w", stdout); #endif } // ll n; // cin >> n; // vi a(n); // f(i, 0, n) { // cin >> a[i]; // } // vector<vi> a(n,vector<vi>(m,0)); // f(i,0,n){ // f(j,0,m){ // cin>>a[i][j]; // } // } int getXOR(int BITree[], int index) { int ans = 0; index += 1; // Traverse ancestors // of BITree[index] while (index > 0) { // XOR current element // of BIT to ans ans ^= BITree[index]; // Update index to that // of the parent node in // getXor() view by // subtracting LSB(Least // Significant Bit) index -= index & (-index); } return ans; } // Updates the Binary Index Tree by // replacing all ancestores of index // by their respective XOR with val void updateBIT(int BITree[], int n, int index, int val) { index = index + 1; // Traverse all ancestors // and XOR with 'val'. while (index <= n) { // XOR 'val' to current // node of BIT BITree[index] ^= val; // Update index to that // of the parent node in // updateBit() view by // adding LSB(Least // Significant Bit) index += index & (-index); } } // Constructs and returns a Binary // Indexed Tree for the given array int* constructBITree(vi &arr, int n) { // Create and initialize // the Binary Indexed Tree int* BITree = new int[n + 1]; for (int i = 1; i <= n; i++) BITree[i] = 0; // Store the actual values in // BITree[] using update() for (int i = 0; i < n; i++) updateBIT(BITree, n, i, arr[i]); return BITree; } int rangeXor(int BITree[], int l, int r) { return getXOR(BITree, r) ^ getXOR(BITree, l - 1); } void solve() { ll q, n; cin >> n >> q; vi a(n); f(i, 0, n) { cin >> a[i]; } int* BITree = constructBITree(a, n); f(i, 0, q) { ll t, x, y; cin >> t >> x >> y; if (t == 1) { int idx = x - 1; int val = y; a[idx] ^= val; // Update the values of all // ancestors of idx updateBIT(BITree, n, idx, val); } if (t == 2) { cout << rangeXor(BITree, x - 1, y - 1) << endl; } } } int main() { bwayne();// remember ll t = 1; // cin >> t; for (ll tt = 1; tt <= t; tt++) { // cout << "Case #" << tt << ": "; solve(); } return 0; }