code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
//ABHISHEK AGRAWAL,BIT mesra// //Newbie......You have to be odd to be no. ONE :)// //Authhor: Abhishekagrawal //Date: 20/02/2021 //Time: 17:30:13 #include<bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define fl float #define vll vector<ll> #define pii pair<int,int> #define vpll vector<pair<ll,ll>> #define pb push_back #define f first #define s second #define mll map<ll,ll> #define mp make_pair #define sp(n) fixed<<setprecision(n) #define mod (ll)1000000007 #define sortv(v) sort(v.begin(),v.end()) #define INF (ll)(1e15) #define loop(i,n) for(int i=0;i<n;i++) #define loop1(i,n) for(int i=1;i<=n;i++) #define rloop(n,i) for(int i=n-1;i>=0;i--) #define sorta(a) sort(a,a+n,greater<ll>()) #define countone(n) __builtin_popcount(n) #define numoftrailzero(n) __builtin_ctz(n) #define maxpowoftwo(n) __builtin_clz(n) #define leastindexwithone(n) __builtin_ffs(n) #define what_is(x) cerr << #x << " is " << x << endl; #define pfv(v) cout<<v.size()<<"\n";loop(i,v.size()) cout<<v[i]<<" ";cout<<"\n"; #define pv(v) loop(i,v.size()) cout<<v[i]<<" "; #define all(v) v.begin(),v.end() #define mset(dp,val) memset(dp,val,sizeof(dp)) ll test,n;ll sum,ans; // #ifdef TESTING // #define DEBUG fprintf(stderr, "====TESTING====\n") // #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl // #define debug(...) fprintf(stderr, __VA_ARGS__) // #else // #define DEBUG // #define VALUE(x) // #define debug(...) // #endif void the_happiest_place_on_earth() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #ifdef ENABLE_FILE_IO freopen("out.txt", "r", stdin); freopen("out1.txt", "w", stdout); #endif } // bool comp(const pair<ll,ll> &a, const pair<ll,ll> &b){ // if(a.s+2*a.f==b.s+2*b.f) return a.f>b.f; // return (a.s+2*a.f>b.s+2*b.f); // } const int N=200040;ll a[N],b[N],c[N],par[N],Size[N],present[N],color[N]; ll power(ll a,ll b,ll M){a%=M;ll res=1;while(b>0){if(b%2 ==1){res=(res*a)%M;}a=(a*a)%M;b/=2;} return res;} void testcase(){ string s; cin>>s; ans=0; ll cur='*'; int cnt=1; loop1(i,s.length()-2){ if(s[i]==cur) ans--; if(s[i]==s[i-1]&&s[i]!=s[i+1]&&s[i]!=cur){ ans+=(s.length()-i-1); cur=s[i]; } } if(s[s.length()-1]==cur) ans--; cout<<ans<<"\n"; return; } int main(){ the_happiest_place_on_earth(); // Today's thought: Push yourself,because no one else is going to do it for you. // Things are not always #000000 and #FFFFFF // START OF PROGRAM LOGIC // g++ -o a BB.cpp -DENABLE_FILE_IO;./a // g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -o a file_name.cpp;./a test=1;int t=0; // cin>>test; while(test--){ //cout<<"Case #"<<++t<<": "; testcase(); } cerr << "Time : " << 1000 *((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; // //END OF PROGRAM LOGIC return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; #define in read() #define pii pair<int,int> #define fi first #define se second #define FILE(x) freopen(x".in","r",stdin);\ freopen(x".out","w",stdout); #define pb push_back int read(){ int x = 0,sgn = 1;char ch = getchar(); for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1; for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48); return x*sgn; } const int mod = 998244353; int n,m,k; ll ans; ll qp(ll x,int t){ll res=1;for(;t;t>>=1,x=x*x%mod)if(t&1)res=res*x%mod;return res;} int main (){ #ifndef ONLINE_JUDGE freopen("1.in","r",stdin); #endif n = in,m = in,k = in; if(n == 1 && m == 1) return printf("%d\n",k),0; if(n == 1 || m == 1) return printf("%d\n",qp(k,n+m-1) % mod),0; for(int i = 1;i <= k;i++){ ans = (ans + (qp(i,n) - qp(i-1,n)) % mod * qp(k-i+1,m) % mod) % mod; } printf("%lld\n",(ans+mod)%mod); return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define ld long double #define pii pair <int, int> #define pll pair <ll, ll> #define pci pair <char, int> #define pld pair <ld, ld> #define ppld pair <pld, pld> #define ppll pair <pll, pll> #define pldl pair <ld, ll> #define vll vector <ll> #define vvll vector <vll> #define vpll vector <pll> #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define mll map <ll, ll> #define fastmap gp_hash_table #define cd complex <double> #define vcd vector <cd> #define PI 3.14159265358979 #pragma 03 using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll x[200005], y[200005]; pll tx[200005], ty[200005]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; for (ll i = 1; i <= n; i++){ cin >> x[i] >> y[i]; tx[i] = mp(x[i], i); ty[i] = mp(y[i], i); } sort(tx + 1, tx + n + 1); sort(ty + 1, ty + n + 1); ll ans = 0; if (tx[n].fi - tx[1].fi < ty[n].fi - ty[1].fi){ for (ll i = 1; i <= n; i++){ swap(x[i], y[i]); swap(tx[i], ty[i]); } } ans = max(ans, tx[n - 1].fi - tx[1].fi); ans = max(ans, tx[n].fi - tx[2].fi); ll id[4]; id[0] = tx[1].se, id[1] = tx[n].se; if (id[0] > id[1]) swap(id[0], id[1]); id[2] = ty[1].se, id[3] = ty[n].se; if (id[2] > id[3]) swap(id[2], id[3]); bool c = 1; if ((id[0] == id[2]) && (id[1] == id[3])) c = 0; if (c) ans = max(ans, ty[n].fi - ty[1].fi); ans = max(ans, ty[n - 1].fi - ty[1].fi); ans = max(ans, ty[n].fi - ty[2].fi); cout << ans << "\n"; }
#include<bits/stdc++.h> using namespace std; #define int long long #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; 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; cin >> n; int x[n], y[n]; vii v1, v2; zip(i, 0, n) { cin >> x[i] >> y[i]; v1.pb({x[i], i}); v2.pb({y[i], i}); } sort(all(v1)); sort(all(v2)); set<pair<int, pii>>s; s.insert({v1[n - 1].ff - v1[0].ff, {min(v1[n - 1].ss, v1[0].ss), max(v1[n - 1].ss, v1[0].ss)}}); s.insert({v1[n - 1].ff - v1[1].ff, {min(v1[n - 1].ss, v1[1].ss), max(v1[n - 1].ss, v1[1].ss)}}); s.insert({v1[n - 2].ff - v1[0].ff, {min(v1[n - 2].ss, v1[0].ss), max(v1[n - 2].ss, v1[0].ss)}}); s.insert({v2[n - 1].ff - v2[0].ff, {min(v2[n - 1].ss, v2[0].ss), max(v2[n - 1].ss, v2[0].ss)}}); s.insert({v2[n - 1].ff - v2[1].ff, {min(v2[n - 1].ss, v2[1].ss), max(v2[n - 1].ss, v2[1].ss)}}); s.insert({v2[n - 2].ff - v2[0].ff, {min(v2[n - 2].ss, v2[0].ss), max(v2[n - 2].ss, v2[0].ss)}}); auto var = *s.rbegin(); s.erase(s.find(var)); cout << (*s.rbegin()).ff << en; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, k, n) for (int i = k; i < (int)(n); i++) #define repd(i, n) for (int i = n-1; i >= 0; i--) #define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--) #define all(x) (x).begin(),(x).end() #define chmax(x,y) x=max(x,y) #define chmin(x,y) x=min(x,y) #define F first //pairの一つ目の要素 //遅延セグ木使うときは注意 #define S second //pairの二つ目の要素 //遅延セグ木使うときは注意 #define PB push_back //挿入 #define MP make_pair //pairのコンストラクタ //V,Pは大文字i,l,bは小文字 using ll = long long; using Vi = vector<int>; using VVi = vector<Vi>; using Vl = vector<ll>; using VVl = vector<Vl>; using Vb = vector<bool>; using VVb = vector<Vb>; using P = pair<int,int>; using Pl = pair<ll, ll>; using Vs = vector<string>; const ll mod = 1000000007; const ll inf = 1000000000000000000;//10の18乗 #define yn {puts("Yes");}else{puts("No");} #define dame { puts("-1"); return 0;} int main() { ll n; cin >> n; Vl v(n); rep(i,n) cin >> v[i]; sort(all(v)); ll ok=1; rep(i,n) if(v[i]!=i+1) ok=0; if(ok) yn; }
#include<bits/stdc++.h> using namespace std; int main(){ int a; cin>>a; priority_queue<int> pq; for(int i=0;i<a;i++){ int c; cin>>c; pq.push(c); } while(pq.size()){ if(pq.top()!=a){ cout<<"No"; return 0; } pq.pop(); a--; } cout<<"Yes"; return 0; }
#include "bits/stdc++.h" using namespace std; //#include "atcoder/all" //using namespace atcoder; #define int long long #define REP(i, n) for (int64_t i = 0; i < (int)n; ++i) #define RREP(i, n) for (int64_t i = (int)n - 1; i >= 0; --i) #define FOR(i, s, n) for (int64_t i = s; i < (int)n; ++i) #define RFOR(i, s, n) for (int64_t i = (int)n - 1; i >= s; --i) #define ALL(a) a.begin(), a.end() #define IN(a, x, b) (a <= x && x < b) template<class T>istream&operator >>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;} template<class T>inline void out(T t){cout << t << "\n";} template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);} template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;} template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;} constexpr int64_t INF = 1000000000000000000; // 10^18 int64_t getRoot(int N) { int64_t ret = 1; while(ret * ret <= N) { ret++; } return ret - 1; } signed main() { int K; cin >> K; int ans = 0; FOR(i, 1, K + 1) { auto f = [&](int N) { int ret = 0; int root = getRoot(N); for(int64_t j = 1; j * j <= N; j++) { ret += N / j * 2; } return ret - root * root; }; ans += f(K / i); //out(i, ans); } out(ans); }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; constexpr ll MOD = 1000000007; #ifndef ONLINE_JUDGE template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;} ostream &operator<<(ostream &o, const string &obj) {o << "\""; o << obj.c_str(); o << "\""; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;} template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} void print_sim_py(void) {cout << endl;} template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();} template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);} #define print(...) print_sim_py(__VA_ARGS__); #else #define print(...); #endif template <typename... Ts> std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); } #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) // a^n mod を計算する long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); ll a,b,c; ll ca,cb; IN(a,b,c); ca=cb=1; ll ans = 0; ll a1=a%10; a1=(a1*(a%10))%10; while(a1!=(a%10)){ ca++; a1=(a1*(a%10))%10; } cb = modpow(b,c,ca); cb = (cb+(ca-1))%ca; print(ca,cb) ll ap=a%10; rep(i,cb){ ap=(ap*(a%10))%10; } ans = ap; cout << ans << endl; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define ud unsigned int #define ll long long #define ull unsigned long long #define MAX_INF 0x3f #define MAX_INF_VAL 0x3f3f3f3f #define MAX_INF_VAL_LL 0x3f3f3f3f3f3f3f3f //#define pi 3.141592653589 #define eps 1e-9 #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) //#define p 2173412051LL //#define sz 2 using namespace std; template< typename T > void read( T &x ) { x = 0; char ch = getchar(); ll f = 1; while( !isdigit( ch ) ) { if( ch == '-' ) f *= -1; ch = getchar(); } while( isdigit( ch ) ) { x = x * 10 + ch - 48; ch = getchar(); } x *= f; } struct custom_hash { static uint64_t splitmix64( uint64_t x ) { 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 ); } }; const ll p = 998244353; int a[ 110 ]; ll f[ 110 ][ 10010 ]; ll fac[ 110 ]; int main() { ios::sync_with_stdio( false ); cin.tie( 0 ), cout.tie( 0 ); int n; cin >> n; fac[ 0 ] = 1; ll sum = 0; for( int i = 1; i <= n; ++i ) { cin >> a[ i ]; fac[ i ] = fac[ i - 1 ] * i % p; sum += a[ i ]; } if( sum & 1 ) { cout << 0; return 0; } sum >>= 1; f[ 0 ][ 0 ] = 1; for( int i = 1; i <= n; ++i ) { for( int j = i; j; --j ) { for( int k = j * 100; k >= a[ i ]; --k ) f[ j ][ k ] = ( f[ j ][ k ] + f[ j - 1 ][ k - a[ i ] ] ) % p; } } ll ans = 0; for( int i = 1; i <= n; ++i ) ans = ( ans + fac[ i ] * f[ i ][ sum ] % p * fac[ n - i ] ) % p; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<ll(n);i++) #define REP(i,k,n) for(ll i=k;i<ll(n);i++) #define all(a) a.begin(),a.end() #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define UNIQUE(a) sort(all(a));a.erase(unique(all(a)),a.end()) #define decimal cout<<fixed<<setprecision(10) #define summon_tourist cin.tie(0);ios::sync_with_stdio(false) using ll=long long; using P=pair<ll,ll>; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; constexpr ll inf=1001001001001001; constexpr int INF=1001001001; constexpr int mod=998244353; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} void YesNo(bool b){if(b)out("Yes");else out("No");} void yesno(bool b){if(b)out("yes");else out("no");} void YESNO(bool b){if(b)out("YES");else out("NO");} ll modpow(ll a,ll b){ll c=1;while(b>0){if(b&1){c=a*c%mod;}a=a*a%mod;b>>=1;}return c;} vi calc(ll x){vi v;while(x>0){v.eb(x%10);x/=10;}reverse(all(v));return v;} vector<P> p_fac(ll n){ vector<P> res; for(ll a=2;a*a<=n;a++){ if(n%a!=0) continue; ll ex=0; //exponent number while(n%a==0){ ex++; n/=a; } res.eb(make_pair(a,ex)); } if(n!=1) res.eb(make_pair(n,1)); return res; } const ll max_n=205000; //please define vi fac(max_n),finv(max_n),inv(max_n); // O(max_n) void init(){ fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; REP(i,2,max_n){ fac[i]=fac[i-1]*i%mod; inv[i]=mod-inv[mod%i]*(mod/i)%mod; finv[i]=finv[i-1]*inv[i]%mod; } } // O(1) ll COM(int n,int k){ if(n<k) return 0; if(n<0||k<0) return 0; return fac[n]*(finv[k]*finv[n-k]%mod)%mod; } int main(){ init(); ll n,m; cin>>n>>m; ll ans=1; REP(i,2,m+1){ vector<P> v=p_fac(i); ll c=1; rep(j,v.size()){ ll x=v[j].se; c*=COM(x+n-1,x); c%=mod; } ans+=c; ans%=mod; } out(ans); }
#include <iostream> int main(){ int a, b, c; std::cin >> a >> b >> c; std::cout << (21-(a+b+c)) << '\n'; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d; cin >> a >> b >> c >> d; cout << b - c << endl; }
#include<bits/stdc++.h> using namespace std; const int N=22; const int M=110; #define pii pair<int,int> using ll=long long; #define mp make_pair #define mid (l+r>>1) vector<vector<int> > v(N); vector<int> status(1<<N,0); inline int read() { char c = getchar(); int x = 0, f = 1; 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; } //vector<int> f(N,1); // //void init(){ // for(int i=1;i<N;i++) f[i]=f[i-1]<<2; //} int main(){ int n=read(),m=read(); for(int i=1;i<=m;i++){ int a=read()-1,b=read()-1; v[a].push_back(b); v[b].push_back(a); for(int j=0;j<(1<<n);j++) if((j>>a&1)&&(j>>b&1)) status[j]=-1; } ll ans=0; for(int i=0;i<(1<<n);i++){ if(status[i]==-1) continue; vector<int> color(N,-1); for(int j=0;j<n;j++) if(i>>j&1) color[j]=2; queue<int> q; int num=0; bool ok=false; for(int j=0;j<n;j++){ if(color[j]!=-1) continue; q.push(j); // cout<<i<<" "<<j<<"\n"; color[j]=0; num++; while(q.size()){ int x=q.front(); q.pop(); for(auto y:v[x]){ if(color[x]==color[y]){ ok=true; break; } // cout<<x<<" "<<y<<"\n"; if(color[y]==-1){ color[y]=color[x]^1; q.push(y); } } if(ok) break; } if(ok) break; } // cout<<i<<" "<<num<<"\n"; if(!ok) ans+=1<<num; } printf("%lld\n",ans); }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ff first #define ss second #define pb push_back #define mp make_pair #define deb(x) cout<< #x << " " << x << "\n"; #define MAX 9223372036854775807 #define MIN -9223372036854775807 #define setbits(n) __builtin_popcountll(n) #define mkunique(a) sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end())-a.begin()); #define print(s) for(ll u: s) cout<<u<<" "; cout<<"\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll mod=1e9+7; const int N=1e5+10; vector<pair<ll,pair<ll,ll>>> a[N]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll TT=clock(); ll n,m; cin>>n>>m; for(ll i=0;i<m;i++){ ll u,v,c,d; cin>>u>>v>>c>>d; u--,v--; a[u].pb({v,{c,d}}); a[v].pb({u,{c,d}}); } vector<ll> d(n,mod*mod); d[0] = 0; set<pair<ll,ll>> s; s.insert({0,0}); while(!s.empty()){ ll v = s.begin()->ss; s.erase(s.begin()); for(auto ed: a[v]){ ll u = ed.ff, c = ed.ss.ff, dd = ed.ss.ss; ll t = d[v]; ll wait = max(0ll,(ll)sqrt(dd) - 1ll - t); for(ll i=max(0ll,wait-1);i<=wait+1;i++){ ll cost = t + i + c + dd/(t+i+1); if(cost < d[u]){ s.erase({d[u],u}); d[u] = cost; s.insert({d[u],u}); } } } } if(d[n-1]==mod*mod) cout<<-1; else cout<<d[n-1]; cerr<<"\n\nTIME: "<<(long double)(clock()-TT)/CLOCKS_PER_SEC<<" sec\n"; TT = clock(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long n; cin >> n; long long a = n; long long b = 0; long long c = 0; while(a!=0){ a = a/1000; if(a == 0) break; b ++; } if(b == 1){ c = n-1000; } if(b == 2){ c = n-1000000; } if(b == 3){ c = n-1000000000; } if(b == 4){ c = n-1000000000000; } if(b == 0){ cout << 0 << endl; } if(b == 1){ cout << c+1 << endl; } if(b == 2){ cout << 999999-1000+1 + 2*(c+1) << endl; } if(b == 3){ cout << 2*(999999999-1000000+1)+(999999-1000+1) + 3*(c+1) << endl; } if(b == 4){ cout << 3*(999999999999-1000000000+1)+ 2*(999999999-1000000+1)+(999999-1000+1) + 4*(c+1) << endl; } if(b == 5){ cout << 4*(999999999999999-1000000000000+1)+ 3*(999999999999-1000000000+1)+ 2*(999999999-1000000+1)+(999999-1000+1) + 5 << endl; } return 0; }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; // using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>; void __dbg() { cout << endl; } template<typename Arg, typename... Args> void __dbg(Arg A, Args... B) { cout << " " << A; __dbg(B...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", __dbg(__VA_ARGS__) #define rep(i,a,b) for(int i=a;i<b;i++) #define lprin(n) printf("%lld\n", n) #define lpri(n) printf("%lld ", n) #define lscn(n) scanf("%lld", &n) #define prin(n) printf("%d\n", n) #define pri(n) printf("%d ", n) #define scn(n) scanf("%d", &n) #define vi vector<int> #define ll long long #define pb push_back #define mp make_pair #define F first #define S second const int inf = INT_MAX; const int ninf = INT_MIN; const int mod = 1e9 + 7; const int maxN = 1e6 + 2; map<ll, ll> dp, vis; ll x, y, ans; ll rec(ll val) { if(vis[val]) return dp[val]; vis[val] = 1; dp[val] = 1e18; ll& ret = dp[val]; ret = abs(x - val); if(val == 1) return ret; if(val % 2 == 0) ret = min(ret, 1 + rec(val / 2)); else ret = min(ret, 1 + min(rec(val + 1), rec(val - 1))); return ret; } int main() { lscn(x); lscn(y); lprin(rec(y)); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long vector<ll> arr, fact; #define mod 998244353 // (1/a)%p = (pow(a,p-2))%p void precalc() { fact[0] = 1; fact[1] = 1; for (int i = 1; i <= 300; i++) { fact[i] = (i * fact[i - 1]) % mod; } } ll power(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2) res = (res * a) % mod; a = (a * a) % mod; b = b / 2; } return res; } ll nCk(ll n, ll k) { return (((fact[n] * power(fact[k], mod - 2)) % mod) * power(fact[n - k], mod - 2)) % mod; } int main() { int n, k; cin >> n >> k; arr = vector<ll>(n); fact = vector<ll>(301); precalc(); ll sum3 = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; // sum3 = (sum3 + arr[i])%mod; } // sum3 = (sum3*2)%mod; vector<ll> sumarr(k + 1); for (int i = 0; i <= k; i++) { ll sum = 0; for (int j = 0; j < n; j++) { sum = (sum + power(arr[j], i)) % mod; } sumarr[i] = sum; } for (int x = 1; x <= k; x++) { ll sum = 0, sum4; for (int p = 0; p <= x; p++) { sum4 = 0; sum4 = (sum4 + nCk(x, p)) % mod; // cout<<"x: "<<x<<" p: "<<p<<" nCk: "<<nCk(x,p)<<endl; ll sum1 = 0, sum2 = 0; sum1 = sumarr[p]; sum2 = sumarr[x - p]; // for (int i = 0; i < n; i++) // { // sum1 = (sum1 + power(arr[i], p))%mod; // sum2 = (sum2 + power(arr[i], x - p))%mod; // } // cout<<"x: "<<x<<" p: "<<p<<" sum: "<<sum<<" sum1: "<<sum1<<" sum2: "<<sum2<<endl; sum4 = (sum4 * sum1) % mod; sum4 = (sum4 * sum2) % mod; sum = (sum + sum4) % mod; // cout<<"#"<<sum4<<endl; } // cout<<"*sum: "<<sum<<endl; sum3 = (sumarr[x] * power(2, x)) % mod; // for(int i=0;i<n;i++){ // sum3 = (sum3 + power(2*arr[i],x))%mod; // } // cout<<"sum3: "<<sum3<<endl; ll num1 = (sum - sum3 + mod) % mod; // cout<<"@num1: "<<num1<<endl; sum = (num1 * power(2, mod - 2)) % mod; cout << sum << "\n"; } return 0; }
#include<bits/stdc++.h> #define ll long long #define pb push_back #define mkp make_pair #define vi vector<int> #define pii pair<int,int> #define FI(n) FastIO::read(n) #define FO(n) FastIO::write(n) #define ull unsigned long long #define mst(a,b) memset(a,b,sizeof(a)) #define foR(i,k,j) for(int i=(k);i>=(j);i--) #define For(i,k,j) for(int i=(k);i<=(j);i++) #define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v) #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define Fin(s) freopen(s,"r",stdin) #define Fout(s) freopen(s,"w",stdout) #define file(s) Fin(s".in"),Fout(s".out") #define INF ((1<<30)-1) #define int long long const int P=998244353; // using namespace std; template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);} template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);} inline int mul(int a,int b) {return 1ull*a*b%P;} inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;} inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;} inline void mulmod(int &a,int b) {a=mul(a, b);} inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);} inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);} inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;} inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%lld%c",x,c);} inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%lld %lld%c",x.first,x.second,c);} inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);} inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);} inline int inv(int a) {return ksm(a,P-2);} namespace FastIO { const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt; int read(char *s) { while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;} int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn; } bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;} void write(int x) { if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];} if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;} } void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}} void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;} }; inline int read() {int x; FI(x); return x;} const int MN=2e5+5,MK=305; int n,K,t[MN][MK],S[MK],a[MN]; int fac[MN],ifac[MN]; int C(int n,int m) { if(n<m||n<0||m<0) return 0; return 1ll*fac[n]*ifac[m]%P*ifac[n-m]%P; } void init(int n=MN-5) { fac[0]=1; For(i,1,n) fac[i]=mul(fac[i-1],i); ifac[n]=inv(fac[n]); foR(i,n,1) ifac[i-1]=mul(ifac[i],i); } signed main() { #ifndef ONLINE_JUDGE freopen("pro.in","r",stdin); freopen("pro.out","w",stdout); #endif n=read(),K=read(); init(); For(i,1,n) a[i]=read(),t[i][0]=1; For(i,1,n) { For(j,1,K) { t[i][j]=mul(t[i][j-1],a[i]); } // For(j,1,K) addmod(t[i][j],t[i][j-1]); For(j,0,K) addmod(S[j],t[i][j]); } For(x,1,K) { int ans=0; For(t,0,x) { addmod(ans,mul(C(x,t),mul(S[x-t],S[t]))); } int p2=ksm(2,x); For(i,1,n) submod(ans,mul(p2,t[i][x])); // cerr<<p2<<endl; printf("%lld\n",mul(ans,inv(2))); } return FastIO::Fflush(),0; }
/////home/mohammed/.config/sublime-text-3/Packages/User /*input */ #include <bits/stdc++.h> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <math.h> #include <sstream> #include <iterator> #include <cstdlib> #include <unordered_map> #include <map> #include <list> #include <set> using namespace std; using bin = std::bitset<8>; #define endl ("\n") #define pi (3.141592653589) #define mod 1000000007 #define int int64_t #define float double #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define all(c) c.begin(), c.end() #define min3(a, b, c) min({a,b,c}) #define max3(a, b, c) max({a,b,c}) #define min4(a, b, c, d) min({a,b,c,d}) #define max4(a, b, c, d) max({a,b,c,d}) #define rrep(i, n) for(int i=n-1;i>=0;i--) #define rep(i,n) for(int i=0;i<n;i++) #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr) #define ld long double #define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];} #define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";};cout << endl; #define input(type, n) type n; cin>>n; struct debugger { template<typename T> debugger& operator , (const T& v) { cerr << v << " "; return *this; } } dbg; bool check_key(map<int, int> m, int key) { if (m.find(key) == m.end()) return false; return true; } vector<int> SieveOfEratosthenes(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); vector<int> res; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) if (prime[p]) res.push_back(p); //cout << p << " "; return res; } // function to convert decimal to binary bin decToBinary(int n) { // array to store binary number int binaryNum[32]; // counter for binary array int i = 0; while (n > 0) { // storing remainder in binary array binaryNum[i] = n % 2; n = n / 2; i++; } string x = ""; // printing binary array in reverse order for (int j = i - 1; j >= 0; j--) { x += to_string(binaryNum[j]); } bin result{x}; return result; } // for example: // bin result = decToBinary(2); // bin result2 = decToBinary(10); // bin z = result xor result2; // cout << z; // return 0; int convertBinaryToDecimal(long long n) { int decimalNumber = 0, i = 0, remainder; while (n != 0) { remainder = n % 10; n /= 10; decimalNumber += remainder * pow(2, i); ++i; } return decimalNumber; } int factorial(int n) { long long res = 1; for (int i = 1; i <= n; i++) { res = ((res * i) % mod + mod) % mod ; } return res; } int32_t main() { fast; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif input(int, num1); input(int, num2); if (num2 % num1 == 0) { cout << "Yes"; } else { cout << "No"; } }
#include<bits/stdc++.h> using namespace std; int main() { int t; t=1; while(t--){ int n; cin>>n; string str; bool is = false; unordered_set<string> st; for(int i=0;i<n;i++){ cin>>str; if(str[0] == '!'){ if(st.find(str.substr(1)) != st.end()){ is = true; cout<<str.substr(1); break; } else st.insert(str); } else{ if(st.find("!"+str) != st.end()){ is = true; cout<<str; break; } else st.insert(str); } } if(!is)cout<<"satisfiable"; // cout<<"\n"; } }
#include <iostream> #include <cstdio> #include <algorithm> #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); using namespace std; typedef long long ll; 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;} ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;} inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;} void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}} void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}} const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ #define DEBUG_Switch 0 int n, m; char s[105][105]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { #if DEBUG_Switch freopen("D:\\code\\input.txt", "r", stdin); #endif //freopen("D:\\code\\output.txt","w",stdout); cin >> n >> m; repd(i, 1, n) { cin >> s[i] + 1; } int ans = 0; repd(i, 1, n) { repd(j, 1, m) { repd(k, 0, 3) { int x = i + dx[k]; int y = j + dy[k]; if (s[i][j] == '.' && s[x][y] == '.') { ans++; } } } } ans >>= 1; cout << ans << endl; return 0; }
#include <iostream> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using std::cin; using std::cout; using std::endl; using std::string; using std::vector; int main() { int H, W; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; int count = 0; rep(i, H) { rep(j, W) { if (S[i][j] == '.') { if (i != H - 1 && S[i + 1][j] == '.') { count++; } if (j != W - 1 && S[i][j + 1] == '.') { count++; } } } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; int r = r2 - r1, c = c2 - c1; if (r == 0 && c == 0) { cout << 0 << endl; } else if (r == c || r == -c || abs(r) + abs(c) <= 3) { cout << 1 << endl; } else if ((r ^ c ^ 1) & 1) { cout << 2 << endl; } else if ( abs(r + c) <= 3 || abs(r - c) <= 3 || abs(r) + abs(c) <= 6) { cout << 2 << endl; } else { cout << 3 << endl; } return 0; }
#include<bits/stdc++.h> #define ll long long #define int ll #define mp make_pair #define pii pair<int,int> #define pb push_back #define r1 rt<<1 #define r2 rt<<1|1 #define fi first #define se second #define db double #define ld long db #define ri register int #define rep(i,a,b) for(ri i=(a);i<=(b);++i) #define rep2(i,a,b,c) for(ri i=(a);i<=(b);i+=(c)) #define REP(i,a,b) for(ri i=(a);i>=(b);--i) #define REP2(i,a,b,c) for(ri i=(a);i>=(b);i-=(c)) using namespace std; inline int read(){ ri 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<<1)+(x<<3)+(c^'0'),c=getchar(); return x*f; } bool chk(ri a,ri b,ri c,ri d){ if(a+b==c+d)return 1; if(a-b==c-d)return 1; if(abs(a-c)+abs(b-d)<=3)return 1; return 0; } signed main() { ri a1=read(),b1=read(),a2=read(),b2=read(); if(a1==a2&&b1==b2){ puts("0"); return 0; } if(chk(a1,b1,a2,b2)){ puts("1"); return 0; } rep(x,a1-3,a1+3)rep(y,b1-3,b1+3){ if(abs(a1-x)+abs(b1-y)<=3){ if(chk(x,y,a2,b2)){ puts("2"); return 0; } } } rep(x,a2-3,a2+3)rep(y,b2-3,b2+3){ if(abs(a2-x)+abs(b2-y)<=3){ if(chk(x,y,a1,b1)){ puts("2"); return 0; } } } if((a1+a2+b1+b2)%2==0){ puts("2"); return 0; } puts("3"); return 0; } /* 1353 1025 3426 458 9 2 46 17 10 4 3 17 6 8 9 1 */
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; int main() { int kk; cin >> kk; int64_t count=0; int i,j,k; for (i=1;i<=kk;++i) { for (j=1;j<=kk;++j) { for (k=1;k<=kk;++k) { if (i*j*k<=kk) { ++count; } else { break; } } if (i*j>kk) { break; } } } cout << count << endl; }
#include <bits/stdc++.h> #define FOR(i, a, n) for (int i = a; i < n; i++) #define REP(w, n) FOR(w, 0, n) using namespace std; typedef long long ll; /* UnionFind:素集合系管理の構造体(union by size) isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n)) unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n)) treeSize(x): x を含む集合の要素数。 */ struct UnionFind { vector<int> size, parents; UnionFind() {} UnionFind(int n) { // make n trees. size.resize(n, 0); parents.resize(n, 0); for (int i = 0; i < n; i++) { makeTree(i); } } void makeTree(int x) { parents[x] = x; // the parent of x is x size[x] = 1; } bool isSame(int x, int y) { return findRoot(x) == findRoot(y); } bool unite(int x, int y) { x = findRoot(x); y = findRoot(y); if (x == y) return false; if (size[x] > size[y]) { parents[y] = x; size[x] += size[y]; } else { parents[x] = y; size[y] += size[x]; } return true; } int findRoot(int x) { if (x != parents[x]) { parents[x] = findRoot(parents[x]); } return parents[x]; } int treeSize(int x) { return size[findRoot(x)]; } }; // グループ数える関数 int countTree(UnionFind tree, int n) { set<int> group; REP(i, n) { group.insert(tree.findRoot(i)); } return (group.size()); } int main() { int n; cin >> n; vector<ll> a(2 * 100000); REP(i, n) { cin >> a[i]; } UnionFind tree(2 * 100000); REP(i, n / 2) { if (a[i] != a[n - i - 1]) { tree.unite(a[i] - 1, a[n - i - 1] - 1); } } ll ans = 0; vector<ll> b(2 * 100000); REP(i, 2 * 100000) { if (b[tree.findRoot(i)] != 1) { ans += tree.treeSize(i) - 1; b[tree.findRoot(i)] = 1; } } cout << ans << endl; return (0); }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cstdlib> #include<queue> #include<set> #include<cstdio> #include<map> #include<cassert> using namespace std; #define ll long long #define reps(i, a, b) for(int i = a; i < b; i++) #define rreps(i, a, b) for(int i = a-1; i >= b; i--) #define rep(i, n) reps(i, 0, n) #define rrep(i, n) rreps(i, n, 0) #define P pair<int, int> #define vec vector<int> #define mat vector<vec> const ll mod = 1000000007; const int INF = 1001001001; vec determined; int h, w; int a, b; int ans = 0; int f(int x, int y) {return x * w + y;} P f_i(int pos) {return P(pos/w, pos%w);} void dfs(int pos){ if(pos == h*w){ // cout << a << " " << b << endl; // rep(i, h){ // rep(j, w){ // cout << determined[f(i, j)]; // }cout << endl; // }cout << endl; if(a == 0 && b == 0) ans++; return; } P p = f_i(pos); if(determined[pos] != 0){ dfs(pos+1); }else{ // bを置く if(b > 0){ b--; determined[pos] = 1; dfs(pos+1); determined[pos] = 0; b++; } // aを横に置く if(a > 0 && pos%w < w-1 && determined[pos+1] == 0){ a--; determined[pos] = 2; determined[f(p.first, p.second+1)] = 2; dfs(pos+1); determined[pos] = 0; determined[f(p.first, p.second+1)] = 0; a++; } // if(pos==2){ // cout << a << endl; // cout << b << endl; // cout << pos << endl; // cout << p.first+1 << " " << p.second << endl; // cout << f(p.first+1, p.second) << endl; // // cout << pos/w << endl; // // cout << h-1 << endl; // } // aを縦に置く if(a > 0 && pos/w < h-1){ a--; determined[pos] = 3; determined[f(p.first+1, p.second)] = 3; dfs(pos+1); determined[pos] = 0; determined[f(p.first+1, p.second)] = 0; a++; } } } int main(){ cin >> h >> w; cin >> a >> b; determined = vec(h*w); // determined[f(0,0)] = 2; // determined[f(0,1)] = 2; // a--; dfs(0); cout << ans << endl; }
#include <bits/stdc++.h> #define maxn 100086 using namespace std; typedef long long ll; int n, m, a, b; ll f[1 << 4][6][10]; int main(){ scanf("%d%d%d%d", &n, &m, &a, &b); if(n < m) swap(n, m); f[0][1][0] = 1; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ for(int k = 0;k < (1 << m);k++){ for(int l = 0;l <= a;l++){ if(!(k & (1 << (j - 1))) && i < n) f[k | (1 << (j - 1))][j + 1][l + 1] += f[k][j][l]; f[(k | (1 << (j - 1))) ^ (1 << (j - 1))][j + 1][l] += f[k][j][l]; if(j + 1 <= m && !(k & (1 << (j - 1))) && !(k & (1 << j))) f[k][j + 2][l + 1] += f[k][j][l]; } } } for(int k = 0;k < (1 << m);k++){ for(int l = 0;l <= a;l++){ f[k][1][l] = f[k][m + 1][l]; for(int o = 2;o <= m + 1;o++) f[k][o][l] = 0; } } } printf("%lld", f[0][1][a]); }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int X = (A + B) / 2, Y = (A - B) / 2; printf("%d %d\n", X, Y); return 0; }
/* ============================================================= ------------------Welcome To My Code------------------------- ---------Ankit Kumar || IIITBH || CSE || 2018-2022----------- ============================================================= */ #include <bits/stdc++.h> using namespace std; #define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define ll long long int #define ld long double #define vll vector<ll> #define pll pair<ll,ll> #define vl(p) vector<p> #define w while #define For(i,s,x) for(i=s;i<x;i++) #define Fore(i,s,x) for(i=s;i<=x;i++) #define FOR(i,x) for(i=0;i<x;i++) #define Rof(i,s,x) for(i=s;i>=x;i--) #define all(v) v.begin(),v.end() #define it(r,v) for(auto r=v.begin();r!=v.end();r++) #define pb push_back #define in insert #define sz size() #define F first #define S second #define nl cout<<"\n" #define pr(a) cout<<a<<" " #define pr2(a,b) cout<<a<<" "<<b<<"\n" #define pr3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n" #define mod 1000000007 map<ll, ll>::iterator itr; vector<ll>::iterator it, it1; const double PI = 3.141592653589793238; int main() { FASTIO; ll i,j,k,x,y,z,m,n,a,b,c,c1=0,c2=0,flag=0,sum=0; string s1,str; map<ll,ll>MAP; vector<ll>v; set<ll>s; cin>>a>>b; x=(a+b)/2; y=a-x; cout<<x<<" "<<y; }
#include <bits/stdc++.h> #define int long long using namespace std; const int maxN = 2e5+10; int X[maxN],Y[maxN]; struct node { int k1,b1,k2,b2; bool swap; node(){k1=k2=1;b1=b2=0;swap=false;} node(int a,int b,int c,int d,bool e){k1=a,b1=b,k2=c,b2=d,swap=e;} node clockwise() { if(!swap)return node(-k1,-b1,k2,b2,!swap); return node(k1,b1,-k2,-b2,!swap); } node counterclockwise() { if(!swap)return node(k1,b1,-k2,-b2,!swap); return node(-k1,-b1,k2,b2,!swap); } node X_symmetric(int p) { if(!swap)return node(-k1,2*p-b1,k2,b2,swap); return node(k1,b1,-k2,2*p-b2,swap); } node Y_symmetric(int p) { if(!swap)return node(k1,b1,-k2,2*p-b2,swap); return node(-k1,2*p-b1,k2,b2,swap); } }op[maxN]; signed main() { int N,M,Q; cin>>N; for(int i=1;i<=N;i++)cin>>X[i]>>Y[i]; cin>>M; for(int i=1;i<=M;i++) { int c; cin>>c; if(c==1)op[i]=op[i-1].clockwise(); else if(c==2)op[i]=op[i-1].counterclockwise(); else { int val; cin>>val; if(c==3)op[i]=op[i-1].X_symmetric(val); else if(c==4)op[i]=op[i-1].Y_symmetric(val); } } cin>>Q; for(int i=1;i<=Q;i++) { int A,B; cin>>A>>B; int x=op[A].k1*X[B]+op[A].b1; int y=op[A].k2*Y[B]+op[A].b2; if(op[A].swap)swap(x,y); cout<<x<<" "<<y<<endl; } }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <numeric> #include <utility> #include <tuple> #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << '\n'; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on #include <array> using m = array<ll, 3>; using matrix = array<m, 3>; matrix calc(matrix a, matrix b) { matrix res = {m{0, 0, 0}, m{0, 0, 0}, m{0, 0, 0}}; rep(i, 0, 3) { rep(j, 0, 3) { rep(k, 0, 3) { res[i][j] += a[i][k] * b[k][j]; } } } return res; } matrix q2() { return {m{0, -1, 0}, m{1, 0, 0}, m{0, 0, 1}}; } matrix q1() { return {m{0, 1, 0}, m{-1, 0, 0}, m{0, 0, 1}}; } matrix q3(ll q) { return {m{-1, 0, 2 * q}, m{0, 1, 0}, m{0, 0, 1}}; } matrix q4(ll q) { return {m{1, 0, 0}, m{0, -1, 2 * q}, m{0, 0, 1}}; } matrix init() { return {m{1, 0, 0}, m{0, 1, 0}, m{0, 0, 1}}; } P solve(matrix mat, P p) { return P{mat[0][0] * p.first + mat[0][1] * p.second + mat[0][2], mat[1][0] * p.first + mat[1][1] * p.second + mat[1][2]}; } void print(matrix m) { rep(i, 0, 3) { rep(j, 0, 3) { cout << m[i][j] << ","; } cout << endl; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin, n; vector<ll> x(n), y(n); rep(i, 0, n) { cin, x[i], y[i]; } int m; cin, m; vector<P> op(m); rep(i, 0, m) { ll a, b = -1; cin, a; if (a == 3 || a == 4) { cin, b; } op[i] = P(a, b); } int q; cin, q; vector<vector<P>> query(m + 1); // index, b rep(i, 0, q) { int a, b; cin, a, b; query[a].emplace_back(i, b); } vector<P> ans(q, P(0, 0)); matrix mat = init(); rep(i, 0, m + 1) { for (auto [idx, b] : query[i]) { ans[idx] = solve(mat, P(x[b - 1], y[b - 1])); } if (i != m) { matrix qmat = init(); if (op[i].first == 1) { qmat = q1(); } else if (op[i].first == 2) { qmat = q2(); } else if (op[i].first == 3) { qmat = q3(op[i].second); } else if (op[i].first == 4) { qmat = q4(op[i].second); } mat = calc(qmat, mat); } } for (auto [X, Y] : ans) { print(X, Y); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; //#define ACL #ifdef ACL #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif #if __has_include("acl-all.h") #include "acl-all.h" //on Wandbox using namespace atcoder; #endif #endif //#ifdef ACL typedef long double ld; typedef long long int ll; typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<ll> vll; typedef vector<pair<ll, ll> > vpll; typedef vector<vector<int> > vvi; typedef vector<vector<char> > vvc; typedef vector<vector<string> > vvs; typedef vector<vector<ll> > vvll; typedef vector<vector<bool> > vvb; typedef pair<ll, ll> pll; #define rep(i, n) for (int i = 0; i < int(n); ++i) #define rrep(i, n) for (int i = 1; i <= int(n); ++i) #define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++) #define drep(i, n) for (int i = int(n)-1; i >= 0; --i) #define MES(a) MES2 a #define MES2(a0,a1,a2,a3,a4,x,...) x #define mes_1(x1) cout<<x1<<endl #define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl #define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl #define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl #define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl #define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__) #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0,a1,a2,a3,a4,x,...) x #define debug_1(x1) cout<<#x1<<": "<<x1<<endl #define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl #define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl #define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl #define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl #define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__) #define Ynmes(a) (a) ? mes("Yes") : mes("No") #define YNmes(a) (a) ? mes("YES") : mes("NO") #define re0 return 0 #define mp(p, q) make_pair(p, q) #define pb(n) push_back(n) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define Sort(a) sort(a.begin(), a.end()) #define rSort(a) sort(a.rbegin(), a.rend()) #define Rev(a) reverse(a.begin(), a.end()) int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 }; int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 }; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } }; const int INF = INT_MAX; const ll LLINF = 1LL << 60; const ll MOD = 1000000007; const double EPS = 1e-9; ll fact_mod(ll n, ll mod) { ll f = 1; for (ll i = 2; i <= n; i++) f = f * (i % mod) % mod; return f; } ll modpow(ll x, ll n, ll mod) { if(n == 0) return 1; ll res = modpow((x * x) % mod, n / 2 , mod); if(n & 1) res = (res * x) % mod; return res; } ll modncr(ll n, ll r, ll mod) { if(r > n-r) r = n-r; if(r == 0) return 1; ll a = 1; rep(i, r) a = a * ((n-i) % mod) % mod; ll b = modpow(fact_mod(r, mod), mod-2, mod); return (a % mod) * (b % mod) % mod; } signed main() { ll n; string s, t; cin >> n >> s >> t; ll r = 0; vll a(n+1, 0), b(n+1, 0); rep(i, n) { a[i+1] = a[i] ^ (s[i]-'0'); b[i+1] = b[i] ^ (t[i]-'0'); } ll sc = 0; for(ll tc = 0; tc <= n; tc++) { chmax(sc, tc); if (a[sc] == b[tc]) continue; while (sc+1 <= n && a[sc] == a[sc+1]) { sc++; } if (sc == n) { mes(-1); re0; } sc++; r += sc-tc; } mes(r); }
#include <bits/stdc++.h> using namespace std; using ll = long long; bool solve() { ll N; cin >> N; string S, X; cin >> S >> X; vector<vector<int>> win(N+1, vector<int>(7)); // 1:win win[N][0] = 1; ll b = 1; for ( int i = N-1; i >= 0; i-- ) { ll c = S[i] - '0'; if ( X[i] == 'T' ) { for ( int j = 0; j < 7; j++ ) { if ( win[i+1][j] == 1 ) { win[i][j] = 1; } int jp = (j + 7 - (c * b)%7 ) % 7; if ( win[i+1][jp] == 1 ) { win[i][j] = 1; } } } else { for ( int j = 0; j < 7; j++ ) { int w = 1; if ( win[i+1][j] == 0 ) { w = 0; } int jp = (j + 7 - (c * b)%7 ) % 7; if ( win[i+1][jp] == 0 ) { w = 0; } win[i][j] = w; } } b = b * 10 % 7; } // bool ans; // if ( X[0] == 'T' ) { // ans = false; // for ( int i = 0; i < 7; i++ ) { // if ( win[0][i] ) ans = true; // } // } else { // ans = true; // for ( int i = 0; i < 7; i++ ) { // if ( !win[0][i] ) ans = false; // } // } return win[0][0]==1; } int main() { string ans = solve() ? "Takahashi" : "Aoki"; cout << ans << "\n"; return 0; }
#include <iostream> #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <stack> #include <queue> #include <array> #include <bitset> #include <cstdio> #include <string> #include <vector> #include <random> #include <chrono> #include <utility> #include <numeric> #include <cstdlib> #include <cstring> #include <climits> #include <sstream> #include <assert.h> #include <iomanip> #include <algorithm> #include <functional> #include <unordered_map> using namespace std; struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _; #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 HOME ~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 ff first #define ss second #define REP(i,st,en) for (int i = st; i <= en; i++) #define REPR(i,st,en) for (int i = st; i >= en; i--) const int MOD = 1e9 + 7; class MainClass { public: void solve() { map<int, int> freq; int a, b, c; cin >> a >> b >> c; freq[a]++, freq[b]++, freq[c]++; if ((freq.size() == 3)) cout << 0; else { if (freq[a] == 1) cout << a; else if (freq[b] == 1) cout << b; else cout << c; } return; } }; int main() { int test = 1; //cin >> test; for (int tst = 1; tst <= test; ++tst) { MainClass Ausmosian; Ausmosian.solve(); cout << "\n"; } 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++) int main(){ int a,b,c; cin >> a >> b >> c; if(a == b) cout << c << endl; else if(b == c) cout << a << endl; else if(c == a) cout << b << endl; else cout << 0 << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n, t, m, res = 0; cin >> n >> t; vector<ll> a, b; for (int i = 0; i < n; i++) { ll x; cin >> x; if (i & 1) { b.push_back(x); } else { a.push_back(x); } } n = a.size(), m = b.size(); vector<ll> va, vb; for (int i = 1; i < (1 << n); i++) { ll sum = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum += a[j]; } } va.push_back(sum); } for (int i = 1; i < (1 << m); i++) { ll sum = 0; for (int j = 0; j < m; j++) { if (i & (1 << j)) { sum += b[j]; } } vb.push_back(sum); } sort(vb.begin(), vb.end()); for (auto i : va) { if (i <= t) res = max(res, i); int j = upper_bound(vb.begin(), vb.end(), t - i) - vb.begin(); if (j > 0) { res = max(res, i + vb[j - 1]); } } for (auto i : vb) { if (i <= t) { res = max(res, i); } } cout << res; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long const ll INF = 10e7+7; #define rep(i,n) for(int i=0;i<n;i++) const double pi = acos(-1); const ll MOD = 1000000007; ll cnt =0,ans=0; typedef pair<int , int > P; //cout << setprecision(11) << fixed; const int di[] = {0,0,-1,1}; const int dj[] = {1,-1,0,0}; int main(){ int N,T; cin >> N >> T ; vector<int> a(N); rep(i,N) cin >> a[i]; vector<ll>s,t; s = t = {0}; for(int i=0;i<N;i++){ for(int j = s.size()-1;j>=0;j--){ s.push_back(s[j]+a[i]); } swap(s,t); } sort(s.begin(),s.end()); for(auto x : t){ if(x>T)continue; ll si = upper_bound(s.begin(),s.end(),T-x) - s.begin(); ans=max(ans,x+s[si-1]); } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; // #define LOCAL typedef long long ll; #ifdef LOCAL const int N = 105; #else const int N = 2e5 + 105; #endif const int INF = 0x3f3f3f3f; int n, k, tot = 0, Mid; vector<int> edge[N]; int f[N]; // 离最远的未被覆盖的关键点 int g[N]; // 离最近的已经选择的点 void dfs(int u, int fa){ f[u] = -INF; g[u] = INF; for(auto v : edge[u]){ if(v == fa) continue; dfs(v, u); f[u] = max(f[u], f[v] + 1); g[u] = min(g[u], g[v] + 1); } f[u] = max(f[u], 0); if(f[u] + g[u] <= Mid){ f[u] = -INF; } if(f[u] == Mid){ ++tot; f[u] = -INF; g[u] = 0; } } bool check(int mid){ Mid = mid; tot = 0; dfs(1, 0); tot += f[1] >= 0; return tot <= k; } void solve() { cin >> n >> k; for(int i = 1; i < n; ++i){ int u, v; cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } int l = 0, r = n, mid, ans = n; while(l <= r){ mid = l + r >> 1; if(check(mid)){ ans = min(ans, mid); r = mid - 1; }else{ l = mid + 1; } } cout << ans << endl; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); cout << "start Debug" << endl; #endif int _ = 1; // cin >> _; for(int i = 1; i <= _; ++i) solve(); }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() struct tree{ vector<vector<int>> G,son,ancestor; vector<int> depth,parent; queue<int> que; int n,root=-1; bool made_lca=false; tree(int n_):G(n_),son(n_),parent(n_,-1),depth(n_,-1),ancestor(n_),n(n_){ for(int i=0;i<n_;i++)ancestor[i].resize(30); } void add_edge(int u,int v){ G[u].push_back(v); G[v].push_back(u); } inline void reset_root(){ root=-1; fill(depth.begin(),depth.end(),-1); fill(parent.begin(),parent.end(),-1); for(auto &p:ancestor)for(int &q:p)q=-1; for(auto &p:son)p.resize(0); made_lca=false; } void have_root(int r){ reset_root(); root=r; que.push(r); while(que.size()){ int p=que.front();que.pop(); for(int q:G[p]){ if(parent[p]==q)continue; son[p].push_back(q); parent[q]=p; que.push(q); } } } void set_depth(){ assert(~root); depth[root]=0; que.push(root); while(que.size()){ int p=que.front();que.pop(); for(int q:son[p]){ depth[q]=depth[p]+1; que.push(q); } } } inline void make_lca(){ assert(~root); if(depth[root]<0)set_depth(); for(int i=0;i<G.size();i++)ancestor[i][0]=parent[i]; for(int j=1;j<30;j++) for(int i=0;i<G.size();i++) if(~ancestor[i][j-1]) ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1]; made_lca=1; } int lca(int u,int v){ if(!made_lca)make_lca(); if(depth[u]>depth[v])swap(u,v); int diff=depth[v]-depth[u],cnt=0; while(diff){ if(diff&1)v=ancestor[v][cnt]; cnt++; diff>>=1; } if(u==v)return u; for(int k=28;k>=0;k--) if(ancestor[u][k]!=ancestor[v][k]) u=ancestor[u][k],v=ancestor[v][k]; return ancestor[u][0]; } }; typedef pair<int,int> P; const double TIMELIMIT=2.98*CLOCKS_PER_SEC; int ans=234567; priority_queue<P> que; queue<P> tmp; template<typename T> void fin(T a){ cout<<a<<endl; exit(0); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); clock_t start = clock(); int n,k;cin>>n>>k; tree t(n); REP(i,n-1){ int a,b;cin>>a>>b;a--;b--; t.add_edge(a,b); } int ans=n; t.have_root(0); t.set_depth(); int ng=-1,ok=n; while(ok-ng>1){ int m=(ok+ng)>>1; //cout<<"M:"<<m<<endl; vector<int> used(n,-1),in(n); REP(i,n)in[i]=t.son[i].size(); while(que.size())que.pop(); REP(i,n)if(!in[i])que.push(P(t.depth[i],i)); int cnt=0; while(que.size()){ P quenonaka=que.top();que.pop(); int p=quenonaka.second,D=quenonaka.second; if(~used[p])continue;//ここは今回のやつを助けるやつ探すところ cnt++;if(cnt>k){ //cout<<"WA"<<p<<endl; break; } int now=p; for(int i=0;i<m&&now!=t.root;i++)now=t.parent[now]; //cout<<p<<" "<<now<<endl; while(tmp.size())tmp.pop(); tmp.push(P(now,m));//nowが今回新しく塗るやつ used[now]=m;if(now!=t.root&&!--in[t.parent[now]])que.push(P(t.depth[t.parent[now]],t.parent[now])); while(tmp.size()){ P q=tmp.front();tmp.pop(); int v=q.first,aa=q.second; for(int w:t.son[v]){ if(used[w]<0){ used[w]=aa-1; if(aa>1)tmp.push(P(w,aa-1)); } else if(used[w]<aa-1){ used[w]=aa-1; tmp.push(P(w,aa-1)); } } if(v!=t.root){ int PA=t.parent[v]; if(used[PA]<0){ used[PA]=aa-1; if(PA!=t.root&&!--in[t.parent[PA]])que.push(P(t.depth[t.parent[PA]],t.parent[PA])); if(aa>1)tmp.push(P(PA,aa-1)); } else if(used[PA]<aa-1){ used[PA]=aa-1; tmp.push(P(PA,aa-1)); } } } } if(cnt>k)ng=m; else ok=m; } cout<<ok<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll,ll> #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) #define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I) #define TO(x,t,f) ((x)?(t):(f)) #define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted #define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted #define REV(x) (reverse(x.begin(),x.end())) //reverse ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);} ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);} #define NEXTP(x) next_permutation(x.begin(),x.end()) const ll INF=ll(1e16)+ll(7); const ll MOD=1000000007LL; #define out(a) cout<<fixed<<setprecision((a)) //tie(a,b,c) = make_tuple(10,9,87); #define pop_(a) __builtin_popcount((a)) ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;} #define num_l(a,v) POSL(a,v) //v未満の要素数 #define num_eql(a,v) POSU(a,v) //v以下の要素数 #define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数 #define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数 // static_cast< long long ll > int N = 30*30; int L = 30; // (i,j) i個下 , j個右 int ind(int a,int b){ return a*L + b; } //vector< vector<string> > S(N,vector<string>(N)); vector<vector<long double>> H(L,vector<long double>(L,5000.0)); vector<vector<long double>> V(L,vector<long double>(L,5000.0)); // 上は下より(i,j)のiが小さい 右は左より(i,j)のjが大きい // 順路正の向きで const int From_U = 0;//上から来た const int From_D = 1;//下から来た const int From_L = 2;//左から来た const int From_R = 3;//右から来た // 復元 string make_char = "DURL"; const int dy[] = {-1,1,0,0}; const int dx[] = {0,0,-1,1}; int main(){ int sy,sx,ty,tx; int s,t; FOR(k,0,1000){ cin >> sy >> sx >> ty >> tx; int dir_y = 1; int dir_x = 1; if(sy > ty) dir_y = -1; if(sx > tx) dir_x = -1; // (i,j) には (i - dir_y , j) or (i , j - dir_x) から行く vector< vector<long double> > score(L,vector<long double>(L,INF)); vector< vector<int> > from(L,vector<int>(L,-1)); score[sy][sx] = 0; for(int y=sy;(y-dir_y)!=ty;y+=dir_y){ for(int x=sx;(x-dir_x)!=tx;x+=dir_x){ //cout << y << " " << x << endl; if(y==sy && x==sx) continue; long double a = INF,b = INF; if(y != sy) a = score[y-dir_y][x] + H[min(y-dir_y,y)][x]; if(x != sx) b = score[y][x-dir_x] + V[y][min(x,x-dir_x)]; if(a < b){ from[y][x] = (dir_y == 1 ? From_U : From_D ); score[y][x] = a; }else{ from[y][x] = (dir_x == 1 ? From_L : From_R ); score[y][x] = b; } } } //return 0; //cout << "95" << endl; string T; int y=ty,x=tx; while(y!=sy || x!=sx){ int a = from[y][x]; T += make_char[a]; assert(0 <= a && a <= 3); y += dy[a]; x += dx[a]; //cout << "from[" << y << "][" << x << "] = " << a << endl; } //cout << "106" << endl; //cout << T << endl; REV(T); /*{ // チェック assert(T.size() == (abs(sy-ty)+abs(sx-tx))); int ud=0,lr=0; for(auto c:T){ if(c == 'U' || c == 'D') ud++; if(c == 'L' || c == 'R') lr++; } assert(abs(sy-ty) == ud); assert(abs(sx-tx) == lr); }*/ cout << T << endl; cout << flush; long double score_now,r; cin >> score_now; r = pow( score_now / score[ty][tx] , 1.0 - 1.0*(abs(sy-ty) + abs(sx-tx)) / 80.0 ) * exp(-(1000.0 - 1.0*k)/1000.0); y=ty,x=tx; while(y!=sy || x!=sx){ int a = from[y][x]; int by = y + dy[a],bx = x + dx[a]; if(by != y){ H[min(by,y)][x] *= r; }else{ V[y][min(bx,x)] *= r; } y += dy[a]; x += dx[a]; } } }
#include <algorithm> #include <climits> // FOO_MAX, FOO_MIN #include <cmath> #include <cstdlib> // abs(int) #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <string> #include <unordered_map> #include <vector> using namespace std; #define roundup(n, d) (((n) + ((d)-1)) / (d)) #define assign_max(into, compared) (into = max((into), (compared))) #define assign_min(into, compared) (into = min((into), (compared))) #define rep(i, n) for (long long i = 0; i < (long long)n; ++i) #define v_in(v) \ for (auto&& i : v) \ cin >> i #define vv_in(vv) rep(I, (long long)vv.size()) for (auto&& J : vv[I]) cin >> J #define FAST_IO \ ios::sync_with_stdio(false); \ cin.tie(nullptr); #define fix(n) fixed << setprecision(n) #define ALL(v) v.begin(), v.end() #define printvar(x) cout << #x << ":" << x << endl #define PI 3.14159265358979323846 typedef long long ll; using ull = unsigned long long; using vll = vector<long long>; using vvll = vector<vector<long long>>; map<pair<ll, ll>, ll> m; ll patterns(ll a, ll b) { if (a == 0 && b == 0) return 0; if (a == 0 || b == 0) return 1; if (m.find(make_pair(a, b)) != m.end()) { return m[make_pair(a, b)]; } ll result = patterns(a - 1, b) + patterns(a, b - 1); m[make_pair(a, b)] = result; return result; } string binsearch(ll a, ll b, ll k) { if (a == 0 && b == 0) return ""; if (a == 0 || b == 0) { char appendch = a == 0 ? 'b' : 'a'; string s; rep(i, max(a, b)) { s += appendch; } return s; } ll max_pattern = patterns(a - 1, b); if (k <= max_pattern) { // cout << "\"a\" + binsearch(a - 1, b, k)" << endl; return "a" + binsearch(a - 1, b, k); } else { // cout << "\"b\" + binsearch(a, b - 1, k - max_pattern)" << endl; return "b" + binsearch(a, b - 1, k - max_pattern); } } int main(void) { FAST_IO ll a, b, k; cin >> a >> b >> k; cout << binsearch(a, b, k) << endl; return 0; }
#include<iostream> #include<iomanip> #include<cassert> #include<math.h> #include<complex> #include<algorithm> #include<utility> #include<queue> #include<stack> #include<string.h> #include<string> #include<set> #include<map> #include<unordered_map> #include<functional> #include<vector> #include<bitset> using namespace std; typedef long long ll; typedef pair<ll,ll> P; const ll INF=2e18; const ll MOD=1e9+7; ll H,W; string s[2010]; ll vertical[2010],horizontal[2010],diagonal[4010]; int main(){ cin>>H>>W; for(ll i=0;i<H;i++)cin>>s[i]; vertical[0]=horizontal[0]=diagonal[W]=1; for(ll i=0;i<H;i++){ for(ll j=0;j<W;j++){ if(i==0 && j==0)continue; if(s[i][j]=='#'){ horizontal[i]=0; vertical[j]=0; diagonal[i-j+W]=0; continue; } ll now=horizontal[i]+vertical[j]+diagonal[i-j+W]; now%=MOD; if(i==H-1 && j==W-1){ cout<<now<<endl; return 0; } horizontal[i]+=now; horizontal[i]%=MOD; vertical[j]+=now; vertical[j]%=MOD; diagonal[i-j+W]+=now; diagonal[i-j+W]%=MOD; } } return 0; }
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include<bits/stdc++.h> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define pf printf #define sf(a) scanf("%d",&a) #define sfl(a) scanf("%lld",&a) #define sff(a,b) scanf("%d %d",&a,&b) #define sffl(a,b) scanf("%lld %lld",&a,&b) #define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c) #define sfffl(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define sffff(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d) #define sffffl(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d) #define sfffff(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e) #define sfffffl(a,b,c,d,e) scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e) #define sfc(a) scanf("%c",&a) #define pii pair<int,int> #define ms(a,b) memset(a,b,sizeof(a)) #define pb(a) push_back(a) #define pbp(a,b) push_back({a,b}) #define db double #define ft float #define ll long long #define ull unsigned long long #define pii pair<int,int> #define ff first #define ss second #define sz(x) x.size() #define all(x) x.begin(),x.end() #define CIN ios_base::sync_with_stdio(0); cin.tie(0) #define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c) #define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c) #define for0(i,n) for(int i=0;i<n;i++) #define for1(i,n) for(int i=1;i<=n;i++) #define forrev(i,n) for(int i=n-1; i>=0; i--) #define forab(i,a,b) for(int i=a;i<=b;i++) #define forba(i,b,a) for(int i=b;i>=a;i--) #define stlloop(x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++) #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a)*((b)/gcd(a,b))) #define case1(z) cout<<"Case "<<z<<": " #define case2(z) printf("Case %d: ",z) #define PI acos(-1) //3.14159265358979323846264338328 #define valid(tx,ty) tx>=0 && tx<row && ty>=0 && ty<col #define intlim 2147483648 #define N 2005 #define inf 100000008 #define mod 1000000007 /*------------------------------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 /*---------------------------------------------------------------------*/ char a[N][N]; ll h[N][N],v[N][N],d[N][N],o[N][N],dp[N][N]; int main() { ll n,m,i,j; cin>>n>>m; for1(i,n) { for1(j,m) { cin>>a[i][j]; o[i][j] = a[i][j]=='#'; } } h[1][1] = 1,v[1][1] = 1,d[1][1] = 1,o[1][1] = 1; for1(i,n) { for1(j,m) { if(o[i][j])continue; dp[i][j] += v[i-1][j]; dp[i][j] %= mod; v[i][j] = v[i-1][j]; dp[i][j] += h[i][j-1]; dp[i][j] %= mod; h[i][j] = h[i][j-1]; dp[i][j] += d[i-1][j-1]; dp[i][j] %= mod; d[i][j] = d[i-1][j-1]; v[i][j] += dp[i][j]; v[i][j] %= mod; h[i][j] += dp[i][j]; h[i][j] %= mod; d[i][j] += dp[i][j]; d[i][j] %= mod; } } cout<<dp[n][m]<<endl; return 0; }
#include<bits/stdc++.h> #define L(i, j, k) for(int i = (j); i <= (k); i++) #define R(i, j, k) for(int i = (j); i >= (k); i--) #define ll long long #define sz(a) ((int) a.size()) #define vi vector<int> using namespace std; const int N = 5e5 + 7, inf = 1e9 + 7; int n, a[N]; ll m; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; L(i, 1, 100) { int k = (100 + n) * i / 100; a[k] = true; } ll all = n + 100, o = (m - 1) / n, cnt = m - o * n, ns = o * all; L(i, 1, all) if(!a[i] && cnt) ns = o * all + i, --cnt; cout << ns << "\n"; return 0; }
#pragma GCC optimize("O3") #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;} template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;} constexpr int MOD = 1e9 + 7; constexpr int inf = 1e9; constexpr long long INF = 1e18; #define all(a) (a).begin(), (a).end() int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll t, n; cin>>t>>n; if((100 * n) % t == 0){ cout << (100 * n) / t + (100 * n) / t * t / 100 - 1 << endl; } else{ cout << (100 * n) / t + (100 * n) / t * t / 100 + 1 << endl; } return 0; }
#pragma GCC optimize("Ofast") #pragma region head #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <class T> using vv = vector<vector<T>>; #define _roverload(_a1, _a2, _a3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _roverload(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i) #define rrep(...) _roverload(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define bit(n) (1LL << (n)) 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 (a > b) { a = b; return 1; } return 0; } void print() { cout << '\n'; } template <class T> void print(const vector<T> &v) { bool f = false; for (const auto &e : v) { if (f) cout << ' '; cout << e; f = true; } cout << '\n'; } template <class... T> void print(const T &...args) { bool f = false; for (const auto &e : {args...}) { if (f) cout << ' '; cout << e; f = true; } cout << '\n'; } const int INF = 1002003004; const ll LINF = 1002003004005006007ll; struct preprocess { preprocess() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } ____; #pragma endregion head #pragma region library #pragma endregion library pi dfs(vv<int> &g, int u, int p = -1) { int dist = -1, idx = -1; for (auto v : g[u]) { if (v == p) continue; auto [d, i] = dfs(g, v, u); if (chmax(dist, d)) { idx = i; } } if (dist == -1) { return {0, u}; } else { return {dist + 1, idx}; } } vi res; bool path(vv<int> &g, int u, int u2, int p = -1) { if (u == u2) { res.emplace_back(u2); return true; } bool r = false; for (auto v : g[u]) { if (v == p) continue; bool f = path(g, v, u2, u); r |= f; if (f) { res.emplace_back(u); } } return r; } int now = 1; void dfs2(vv<int> &to, vi &ans, vi &route, int u, bool flag, int p = -1) { ans[u] = now++; for (auto v : to[u]) { if (v == p) continue; if (route.size() && v == route.back()) continue; dfs2(to, ans, route, v, false, u); } if (flag && route.size()) { int v = route.back(); route.pop_back(); dfs2(to, ans, route, v, true, u); } now++; } int main() { int n; cin >> n; vv<int> to(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; to[a].emplace_back(b); to[b].emplace_back(a); } auto [dist, u1] = dfs(to, 0); auto [dist2, u2] = dfs(to, u1); path(to, u1, u2); res.pop_back(); vi ans(n); dfs2(to, ans, res, u1, true); print(ans); }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; inline int read () { int ret = 0, t = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') t = -1, c = getchar(); while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar(); return ret * t; } const int MAXN = 200200; int n, fa[MAXN][22]; vector <int> E[MAXN]; int dep[MAXN], wson[MAXN]; void dfs (int x, int f) { dep[x] = dep[f] + 1; fa[x][0] = f; for (auto i: E[x]) if (i != f) dfs(i, x); } int dis (int u, int v) { int ret = dep[u] + dep[v], i; if (dep[u] < dep[v]) swap(u, v); for (i = 20; i >= 0; i--) if (dep[fa[u][i]] >= dep[v]) u = fa[u][i]; if (u == v) return ret - 2 * dep[u]; for (i = 20; i >= 0; i--) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i]; u = fa[u][0]; return ret - 2 * dep[u]; } int las = 0, ans[MAXN]; void calc (int x, int f) { ans[x] = ans[las] + dis(las, x); las = x; for (auto i: E[x]) if (i != f && i != wson[x]) calc(i, x); if (wson[x]) calc(wson[x], x); } int main () { n = read(); int i, j; for (i = 1; i < n; i++) { int u = read(), v = read(); E[u].push_back(v), E[v].push_back(u); } dfs(1, 0); for (i = 1, j = 0; i <= n; i++) if (dep[j] < dep[i]) j = i; int rt = j; dfs(j, 0); for (i = 1, j = 0; i <= n; i++) if (dep[j] < dep[i]) j = i; for (i = j; i; i = fa[i][0]) wson[fa[i][0]] = i; for (i = 1; i <= 20; i++) for (j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1]; calc(rt, 0); for (i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; /* typedef */ typedef long long ll; typedef pair<int, int> pii; /* constant */ const int MOD = 998244353; /* struct for mod Calculation*/ struct mint { ll x; mint(ll x = 0):x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if(!t) return 1; mint res = pow(t/2); res *= res; return (t&1)?res*x:res; } // for prime mod mint inv() const { return pow(MOD - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } bool operator<(const mint& a)const{ return x < a.x;} bool operator==(const mint& a)const{ return x == a.x;} }; mint ex(mint x, ll t) { return x.pow(t);} istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} /* global variables */ /* function */ /* main */ int main(){ ll a, b, c; cin >> a >> b >> c; mint A = a; mint B = b; mint C = c; mint two = 2; mint inv_two = two.inv(); mint ans = inv_two * A * (A + 1) * inv_two * B * (B + 1) * inv_two * C * (C + 1); cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; /* * @title ModInt * @docs md/util/ModInt.md */ template<long long& mod> class ModInt { public: long long x; constexpr ModInt():x(0) {} constexpr ModInt(long long 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 long long y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator+=(const int y) {ModInt p(y);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 long long y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const int y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator*=(const ModInt &p) {x = (x * p.x % mod);return *this;} ModInt &operator*=(const long long y) {ModInt p(y);x = (x * p.x % mod);return *this;} ModInt &operator*=(const int y) {ModInt p(y);x = (x * p.x % mod);return *this;} ModInt &operator^=(const ModInt &p) {x = (x ^ p.x) % mod;return *this;} ModInt &operator^=(const long long y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;} ModInt &operator^=(const int y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;} ModInt &operator/=(const ModInt &p) {*this *= p.inv();return *this;} ModInt &operator/=(const long long y) {ModInt p(y);*this *= p.inv();return *this;} ModInt &operator/=(const int y) {ModInt p(y);*this *= p.inv();return *this;} ModInt operator=(const int y) {ModInt p(y);*this = p;return *this;} ModInt operator=(const long long y) {ModInt p(y);*this = p;return *this;} ModInt operator-() const {return ModInt(-x); } ModInt operator++() {x++;if(x>=mod) x-=mod;return *this;} ModInt operator--() {x--;if(x<0) x+=mod;return *this;} 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; } 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 inv() 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(long long n) const {ModInt ret(1), mul(x);for(;n > 0;mul *= mul,n >>= 1) if(n & 1) ret *= mul;return ret;} friend ostream &operator<<(ostream &os, const ModInt &p) {return os << p.x;} friend istream &operator>>(istream &is, ModInt &a) {long long t;is >> t;a = ModInt<mod>(t);return (is);} }; long long mod; using mint = ModInt<mod>; int main() { cin.tie(0);ios::sync_with_stdio(false); int N,K; cin >> N >> K >> mod; int L = N*(N-1)/2*K; vector<vector<mint>> dp(N,vector<mint>(L+1,0)); dp[0][0]=1; for(int i=1;i<N;++i) { dp[i]=dp[i-1]; //jの値をj,j+1*i,j+2*i,...,j+K*iに加算したい //累積和で考えると、j+(K+1)*i,j+(K+2)*i,...に足しすぎる。 //imosを考えると先に j+(K+1)*iからjの分を引けば良い。 for(int j=L; 0 <= j; --j) if(j + (K+1)*i <= L) dp[i][j + (K+1)*i] -= dp[i][j]; for(int j=0;j<=L; ++j) if(j + i <= L) dp[i][j+i] += dp[i][j]; } vector<mint> ans(N+1); for(int i=1;i<=N;++i) { int l = i-1; int r = N-i; mint sum = 0; for(int j=0;j<=L;++j) sum += dp[l][j]*dp[r][j]; ans[i]=sum*(K+1) - 1; } for(int i=1;i<=N;++i) { cout << ans[i] << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353 #define int ll #define pii pll #define f first #define s second #define pb emplace_back #define forn(i,n) for(int i=0;i<(n);++i) #define sz(a)((int)(a).size()) #define sqr(x) ((x)*(x)) struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){ #ifdef LOCAL #define dbg(...) _(#__VA_ARGS__,__VA_ARGS__) cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n"; #else #define dbg(...) #endif }}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N=111; int a[N],b[N],u[N+N]; int dp[N][N]; void no(){ cout<<"No\n"; exit(0); } int32_t main(){ int f=0; int n; cin>>n; for(int i=1;i<=n;++i){ cin>>a[i]>>b[i]; if(a[i]!=-1&&b[i]!=-1&&a[i]>=b[i])no(); if(a[i]!=-1&&u[a[i]])no(); if(b[i]!=-1&&u[b[i]])no(); if(a[i]!=-1)u[a[i]]=1; if(b[i]!=-1)u[b[i]]=1; if(a[i]==-1&&b[i]==-1)f++; } dp[0][0]=1; for(int i=1;i<=n;++i){ for(int c=0;c<=min(i,f);c++){ forn(j,i){ dbg(j); memset(u,0,sizeof u); int d=i-j,ok=1; for(int k=1;ok&&k<=n;++k){ int ak=a[k],bk=b[k]; if(ak==bk)continue;//-1 if(ak!=-1&&ak<=i+i&&ak>j+j+d)ok=0; if(bk!=-1&&bk<=j+j+d&&bk>j+j)ok=0; dbg(ak,bk); if(ak>i+i)continue; if(bk>i+i)continue; if(ak!=-1&&ak<=j+j)continue; if(bk!=-1&&bk<=j+j)continue; if(ak==-1)ak=bk-d; if(bk==-1)bk=ak+d; if(bk<=j+j+d)ok=0; dbg(k,ok,ak,d,bk); if(ak+d!=bk)ok=0; if(u[ak]||u[bk])ok=0; u[ak]=u[bk]=1; } int nado=0; for(int k=j+j+1;ok&&k<=j+j+d;++k){ if(!u[k]&&u[k+d])ok=0; if(u[k]&&!u[k+d])ok=0; dbg(k,ok,nado); if(u[k]&&u[k+d])continue; nado++; } dbg(ok,nado); if(ok&&nado<=c)dp[i][c]|=dp[j][c-nado]; } dbg(i,c,dp[i][c]); } dbg("====", i+1); } if(!dp[n][f])no(); cout<<"Yes\n"; return 0; }
#include <bits/stdc++.h> #define REP(i, e) for(int (i) = 0; (i) < (e); ++(i)) #define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i)) #define ALL(c) (c).begin(), (c).end() #define PRINT(x) cout << (x) << "\n" using namespace std; using ll = long long; using pll = pair<ll, ll>; const long long MOD = 1000000007; ll N, A[201], B[201]; ll L[201], R[201]; bool dp[201], used[201]; signed main(){ cin >> N; REP(i, N) cin >> A[i] >> B[i]; REP(i, 2 * N) L[i] = R[i] = -1; REP(i, N){ if(A[i] != -1){ A[i]--; if(L[A[i]] != -1 || R[A[i]] != -1){ PRINT("No"); return 0; } L[A[i]] = i; } if(B[i] != -1){ B[i]--; if(R[B[i]] != -1 || L[B[i]] != -1){ PRINT("No"); return 0; } R[B[i]] = i; } } dp[0] = true; REP(i, 2 * N){ if(!dp[i]) continue; FOR(j, i + 1, 2 * N){ ll w = j - i + 1; if(w & 1) continue; w /= 2; bool ok = true; REP(k, N) used[k] = false; REP(k, w){ ll p = i + k, q = i + w + k; if(L[q] != -1 || R[p] != -1) ok = false; if(L[p] != -1 && R[q] != -1 && L[p] != R[q]) ok = false; if(L[p] != -1){ if(used[L[p]]) ok = false; used[L[p]] = true; } if(R[q] != -1){ if(L[p] != R[q] && used[R[q]]) ok = false; used[R[q]] = true; } } if(ok) dp[j + 1] = true; } } if(dp[2 * N]) PRINT("Yes"); else PRINT("No"); return 0; }
#include <iostream> #include <cstdio> #include <algorithm> #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); using namespace std; typedef long long ll; 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;} ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;} inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;} void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}} void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}} const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ #define DEBUG_Switch 0 char s[maxn]; // R beats S; // P beats R; // S beats P. // int n, base[1010]; char dp[105][105]; char dfs(int pos, int k) { if (k == 0) { return s[pos]; } if (dp[pos][k] != '\0') { return dp[pos][k]; } char l = dfs(pos, k - 1); char r = dfs((pos + base[k - 1]) % n, k - 1); char res; if (l == r) { res = l; } else { if (r + l == 'R' + 'S') { res = 'R'; } if (r + l == 'R' + 'P') { res = 'P'; } if (r + l == 'S' + 'P') { res = 'S'; } } dp[pos][k] = res; return res; } int k; int main() { #if DEBUG_Switch freopen("D:\\code\\input.txt", "r", stdin); #endif //freopen("D:\\code\\output.txt","w",stdout); n = readint(); k = readint(); scanf("%s", s); base[0] = 1; repd(i, 1, k) { base[i] = base[i - 1] * 2 % n; } printf("%c\n", dfs(0, k) ); return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long long const int M=2e5+5,P=1e9+7,base=269; inline int rd(){ int x=0;char c=getchar(); while(!isdigit(c))c=getchar(); while(isdigit(c))x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x; } int n,A[M],B[M],c[M]; map <int,queue <int> > Q; void update(int x,int v){ while(x<=n){ c[x]+=v; x+=(x&-x); } } int query(int x){ int res=0; while(x){ res+=c[x]; x-=x&-x; } return res; } LL ans; int main(){ n=rd(); for(int i=1;i<=n;++i)A[i]=rd(),update(i,1); for(int i=1;i<=n;++i)B[i]=rd(); for(int i=n;i;--i) Q[A[i]+i].push(i); for(int i=n;i;--i){ int x=B[i]+i; if(Q[x].empty()){ puts("-1"); exit(0); } else { int y=Q[x].front(); Q[x].pop(); int tmp=query(n)-query(y); update(y,-1); ans+=tmp; } }printf("%lld\n",ans); return 0; }
// Author : Naresh Bharasagar // Instt. : Indian Institute of Technology Guwahati //-------------------------------------------------------------------------------------------------------------------------------- #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; typedef long long int loo; typedef long double ld; #pragma GCC target ("sse4.2") #define ordered_set(a) tree<a, null_type,less<a>, rb_tree_tag,tree_order_statistics_node_update> #define loop(i,begin,end) for(__typeof(end)i=(begin)-((begin)>(end));i!=(end)-((begin)>(end));i+=1-2*((begin)>(end))) #define loopset(it,val,dtype) for(set<dtype>::iterator it=val.begin(); it!=val.end(); it++) #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define vloo vector<loo> #define vi vector<int> #define pi pair<int,int> #define mp make_pair #define ploo pair<loo,loo> #define fillz(a) memset(a,0,sizeof(a)) #define all(a) a.begin(),a.end() #define outa(a,n) loop(i,0,n) cout << a[i] << " "; #define pr(a) cout << a << "\n"; #define pb(l) push_back(l) #define ff first #define ss second #define input(a) for(auto &i: a) cin >> i; #define deb(i) cout << "[" << #i << "=" << i << "]" << endl; #define nl cout << "\n"; #define INF 1000000000 #define M 1000000007 loo power(loo x, loo y, loo p) { loo res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } bool sortbysec(const pair<loo,loo> &a, const pair<loo,loo> &b) { return (a.second > b.second); } string decToBin(loo n) { string s = "0"; if(n == 0) return s; s = ""; while (n > 0) { s += (n % 2) + '0'; n = n / 2; } reverse(s.begin(), s.end()); return s; } loo binToDec(string s) { loo ans = 0; reverse(s.begin(), s.end()); loop(i, 0, s.size()) ans += pow(2, i)*(s[i] - '0'); return ans; } loo nCr(loo n, loo r) { loo p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; loo m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } // vector<int> dist; vector<bool> vis(100001,false); void dfs(vector<loo> adj[], loo src) { vis[src]=true; loop(i,0,adj[src].size()) { loo x = adj[src][i]; if(vis[x]==false) { vis[x]=true; // dist[x] = dist[src]+1; dfs(adj,x); } } } // vector<loo> sg(100001,-1); //prev void bfs(vector<loo> adj[], loo src) { list<loo> ql; vis[src]=true; ql.pb(src); while(!(ql.empty())) { src = ql.front(); // cout << src << " "; ql.pop_front(); loop(i,0,adj[src].size()) { if(!vis[adj[src][i]]) { vis[adj[src][i]]=true; // sg[adj[src][i]]=src; ql.pb(adj[src][i]); } } } } // ---------------------------------------------------------------- void solve() { loo n; cin >> n; string s; cin >> s; loo q; cin >> q; loo t,a,b; bool f = false; while(q--) { cin >> t >> a >> b; a--,b--; if(t==1) { if(f) a=(a+n)%(2*n), b=(b+n)%(2*n); swap(s[a],s[b]); } else { f^=1; } } if(f) cout << s.substr(n,n)+s.substr(0,n); else cout << s; nl; } int main() { fast_io // freopen("validation_input.txt","r",stdin); // freopen("output.txt","w",stdout); loo q = 1; // cin >> q; while(q--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; string a, b; for(int i = 0; i < n; i++) { a += s[i]; } for(int i = n; i < 2 * n; i++) { b += s[i]; } int q; cin >> q; while(q-- > 0) { int t, i, j; cin >> t >> i >> j; if(t == 1) { if(i >= 1 && j <= n) { swap(a[i - 1], a[j - 1]); } else if(i > n && j <= 2 * n) { i -= n, j -= n; swap(b[i - 1], b[j - 1]); } else { j -= n; swap(a[i -1], b[j - 1]); } } else { swap(a, b); } } cout << a << b << endl; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <cmath> #include <iomanip> #include <cstdio> #include <set> #include <map> #include <list> #include <cstdlib> #include <queue> #include <stack> #include <bitset> using namespace std; #define MOD 1000000007 #define PI 3.1415926535897932 #define INF 1e9 #define rep(i, n) for (int i = 0; i < n; i++) #define repe(i, j, n) for (int i = j; i < n; i++) #define repi(i, n) for (int i = 0; i <= n; i++) #define repie(i, j, n) for (int i = j; i <= n; i++) #define all(x) x.begin(), x.end() #define println() cout << endl #define P pair<int, int> #define fi first #define se second typedef long long ll; using Graph = vector<vector<ll>>; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll dp[2000][2000]; ll nCr(ll n, ll r) { if(n==r) return dp[n][r] = 1; if(r==0) return dp[n][r] = 1; if(r==1) return dp[n][r] = n%MOD; if(dp[n][r]) return dp[n][r]%MOD; return dp[n][r] = nCr(n-1,r)%MOD + nCr(n-1,r-1)%MOD; } ll H(ll n, ll r) { return nCr(n+r-1, r)%MOD; } int prime[10000000]; bool is_prime[100000000 + 1]; int sieve(int n) { int pcnt = 0; for(int i = 0; i <= n; i++) { is_prime[i] = true; } is_prime[0] = is_prime[1] = false; for(int i = 2; i <= n; i++) { if(is_prime[i]) { prime[pcnt++] = i; for(int j = 2*i; j <= n; j += i) { is_prime[j] = false; } } } return pcnt; } struct UnionFind { //自身が親であれば、その集合に属する頂点数に-1を掛けたもの //そうでなければ親のid vector<ll> r; UnionFind(ll N) { r = vector<ll>(N, -1); } ll root(ll x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } ll size(ll x) { return -r[root(x)]; } bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す ll rx = root(x); ll ry = root(y); return rx == ry; } }; void solve1() { int n, w; cin >> n >> w; cout << n / w << endl; } int main() { solve1(); }
#include <iostream> using namespace std; int main() { int n,y; cin>>n>>y; cout<< n/y; }
#include <bits/stdc++.h> using namespace std; int main(){ long long n,c; cin >> n >> c; long long a[n][3]; for(long long i=0;i<n;++i){ for(int j=0;j<3;++j){ cin >> a[i][j]; } } map<long long, long long> m; for(int i=0;i<n;++i){ m[a[i][0]]+=a[i][2]; m[a[i][1]+1]+=(-a[i][2]); } map<long long, long long>::iterator itr; long long sum=0;long long val=0; for (itr = m.begin(); itr != m.end(); ++itr){ itr->second+=val; val=itr->second; } map<long long, long long>::iterator it = m.begin(); for (itr = m.begin(); itr != m.end()--; ++itr){ it++; if(itr->second<c){ int diff = it->first - itr->first; sum+=(itr->second*diff); } else{ int diff = it->first - itr->first; sum+=(c*diff); } } cout << sum << endl; return 0; }
#include<bits/stdc++.h> using namespace std; vector<int> Times; int a[200200],b[200200]; long long c[200200]; long long delta[400400]; int main() { int N; long long C; cin>>N>>C; for(int i=0;i<N;i++) { cin>>a[i]>>b[i]>>c[i]; b[i]++; Times.push_back(a[i]); Times.push_back(b[i]); } sort(Times.begin(),Times.end()); for(int i=0;i<N;i++) { a[i]=lower_bound(Times.begin(),Times.end(),a[i])-Times.begin(); b[i]=lower_bound(Times.begin(),Times.end(),b[i])-Times.begin(); } int sz=Times.size(); for(int i=0;i<N;i++) { delta[a[i]]+=c[i]; delta[b[i]]-=c[i]; } long long tot=0; long long cost=0; for(int i=0;i<sz-1;i++) { tot+=delta[i]; cost+=min(tot,C)*(Times[i+1]-Times[i]); } cout<<cost<<endl; return 0; }
// #pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define dd double #define ld long double #define sl(n) scanf("%lld", &n) #define si(n) scanf("%d", &n) #define sd(n) scanf("%lf", &n) #define pll pair <ll, ll> #define pii pair <int, int> #define mp make_pair #define pb push_back #define all(v) v.begin(), v.end() #define inf (1LL << 61) #define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) #define for1(i, stop) for(ll i = 1; i <= stop; ++i) #define for0(i, stop) for(ll i = 0; i < stop; ++i) #define rep1(i, start) for(ll i = start; i >= 1; --i) #define rep0(i, start) for(ll i = (start-1); i >= 0; --i) #define ms(n, i) memset(n, i, sizeof(n)) #define casep(n) printf("Case %lld:", ++n) #define pn printf("\n") #define pf printf #define EL '\n' #define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const ll sz = 110; ll ans, cnt[sz], c[sz], d[sz], a[sz], b[sz], n, m, k; void solve(ll pos) { if(pos > k) { ll tot = 0; for1(i, m) { if(cnt[ a[i] ] >= 1 && cnt[ b[i] ] >= 1) tot++; } ans = max(ans, tot); return; } cnt[ c[pos] ]++; solve(pos+1); cnt[ c[pos] ]--; cnt[ d[pos] ]++; solve(pos+1); cnt[ d[pos] ]--; } int main() { cin >> n >> m; for1(i, m) { sl(a[i]), sl(b[i]); } cin >> k; for1(i, k) { sl(c[i]), sl(d[i]); } solve(1); cout << ans << EL; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int main() { int n,m; int vdncjdkdkh; cin>>n>>m; vector<pair<int,int>>a(m); for(int i=0;i<m;i++) { cin>>a[i].first>>a[i].second; } int k; cin>>k; vector<pair<int,int>>b(k); for(int j=0;j<k;j++) { cin>>b[j].first>>b[j].second; } ll l=0; ll tot=1<<k; for(ll mask=0;mask<tot;mask++) { vector<int>v(n+1); ll ans=0; for(int i=0;i<k;i++) { if(mask & (1<<i)) v[b[i].second]=1; else v[b[i].first]=1; } for(int i=0;i<m;i++) { if(v[a[i].first] && v[a[i].second]) ans++; } l=max(l,ans); } cout<<l<<endl; return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for(int i = (a); i <= (b); ++ i) #define per(i, a, b) for(int i = (a); i >= (b); -- i) #define pb push_back #define Pii pair<int,int> #define fi first #define se second typedef double db; typedef long long ll; using namespace std; const ll N = 110; const ll P = 998244353; int n, m; int a[N*N],b[N*N],c[N],vis[N],col[N][N],dep[N]; vector<int>G[N]; void dfs(int u, int cc,int pa) { vis[u] = 1; dep[u] = dep[pa] + 1; for(int v: G[u])if(!vis[v]&&c[v]==cc){ // cout << u << " " << v << "\n"; dfs(v,cc,u); col[u][v] = 1; } else if(vis[v]&&c[v]==cc&&dep[v]+1<dep[u]) { col[u][v] = 1; // cout << u << " " << v << "\n"; } } signed main () { ios::sync_with_stdio(false); cin.tie(0); cin >> n>>m; rep(i,1,m) cin >> a[i] >> b[i], G[a[i]].pb(b[i]), G[b[i]].pb(a[i]); rep(i,1,n) cin >> c[i]; rep(i, 1, n) if(!vis[i]) { dfs(i,c[i],0); } rep(i,1,m) { if(c[a[i]]>c[b[i]]) cout << "->\n"; else if(c[a[i]]<c[b[i]]) cout <<"<-\n"; else { cout << (col[a[i]][b[i]] ? "->\n" : "<-\n"); } } }
#include <bits/stdc++.h> using namespace std; using Vi = vector<int>; using VVi = vector<Vi>; using Vb = vector<bool>; VVi tree; int depth[110]; int par[110]; int done[110]; void dfs(int u, const VVi& gr, Vb& ex) { for (int v : gr[u]) { if (!ex[v]) { ex[v] = true; par[v] = u; tree[u][v] = 1; tree[v][u] = -1; depth[v] = depth[u] + 1; dfs(v, gr, ex); } } done[u] = 1; } int main () { int N, M; cin >> N >> M; Vi A(M); Vi B(M); Vi count(N + 1); tree.assign(N + 1, Vi(N + 1, -2)); for (int i = 0; i < M; i ++) { int a, b; cin >> a >> b; tree[a][b] = tree[b][a] = 0; A[i] = a; B[i] = b; } for (int i = 1; i <= N; i ++) { cin >> count[i]; } VVi same(N + 1); for (int i = 1; i <= N; i ++) { done[i] = 1; } for (int i = 1; i <= N; i ++) { for (int j =1; j <= N; j ++) { if (tree[i][j] == 0) { if (count[i] > count[j]) { tree[i][j] = 1; tree[j][i] = -1; } else if (count[j] > count[i]) { tree[j][i] = 1; tree[i][j] = -1; } else { same[i].push_back(j); same[j].push_back(i); done[i] = done[j] = 0; } } } } //cout << tree[4][4] << endl; for (int i = 1; i <= N; i ++) { if (!done[i]) { Vb ex(N + 1, false); ex[i] = true; depth[i] = 0; par[i] = 0; dfs(i, same, ex); } } for (int i = 1; i <= N; i ++) { for (int j = 1; j <= N; j ++) { if (tree[i][j] == 0) { int pk = 1; if (depth[i] < depth[j]) pk = -1; tree[i][j] = pk; tree[j][i] = -pk; } } } for (int i = 0; i < M; i ++) { if (tree[A[i]][B[i]] == 1) { cout << "->" << endl; } else { cout << "<-" << endl; } } }
#include <bits/stdc++.h> using namespace std; using s64 = signed long long; using u64 = unsigned long long; using f128 = long double; int main() { u64 h, w; cin >> h >> w; vector<vector<u64>> a; for(u64 i = 0; i < h; i++) { a.push_back(vector<u64>()); for(u64 j = 0; j < w; j++) { u64 aij; cin >> aij; a[i].push_back(aij); } } u64 minBlock = ULONG_LONG_MAX; for(u64 i = 0; i < h; i++) { for(u64 j = 0; j < w; j++) { if(minBlock > a[i][j]) { minBlock = a[i][j]; } } } u64 removeBlock = 0; for(u64 i = 0; i < h; i++) { for(u64 j = 0; j < w; j++) { removeBlock += a[i][j] - minBlock; } } cout << removeBlock << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define int long long #define forn(i,n) for(int i = 0; i < n; i++) int32_t main() { int n, q, k; cin >> n >> q; vector<int> a(n); forn(i, n) cin >> a[i]; vector<int> ps(n); ps[0] = a[0] - 1; forn(i, n - 1) ps[i + 1] = ps[i] + a[i + 1] - a[i] - 1; while (q--) { cin >> k; int x = --lower_bound(ps.begin(), ps.end(), k) - ps.begin(); if (x == -1) cout << k; else cout << a[x] + k - ps[x]; cout << "\n"; } }
#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ll long long int #define ull unsigned long long #define ld long double #define mod 1000000007 #define EULER 2.7182818284 #define INF 1000000000 #define pb push_back #define ins insert #define IT iterator #define PQ priority_queue #define nl "\n" #define fi first #define se second #define maxe max_element #define mine min_element #define er erase #define lb lower_bound #define ub upper_bound #define vi vector<int> #define vll vector<ll> #define vb vector<bool> #define pi pair<int,int> #define pll pair<ll,ll> #define all(v) v.begin(),v.end() #define mpi map<int,int> #define mpl map<ll,ll> #define umpi unordered_map<int,int> #define umpl unordered_map<ll,ll> #define si set<int> #define sl set<ll> #define msi multi_set<int> #define msl multi_set<ll> #define mem(v,i) memset(v,i,sizeof(v)) // v is array. To set all elements to 0 or -1 only. mem(arr,0); #define repab(i,a,b) for(int i=(a);i<=(b);i++) #define reprab(i,a,b) for(int i=(a);i>=(b);i--) #define repll(i,a,b) for(ll i=(a);i<=(b);i++) #define rep(i,n) for(int i=(0);i<(n);i++) #define repr(i,n) for(int i=(n);i>=0;i--) #define bs bitset #define bpc(a) __builtin_popcount(a) #define tc int t;cin>>t;while(t--) int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; ll max(ll a,ll b) { if(a>b)return a;return b; } ll min(ll a,ll b) { if(a<b)return a;return b; } ll mypow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } int main() { fast ll n,w; cin>>n>>w; cout<<n/w<<nl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define all(a) (a).begin(), (a).end() #define uniq(a) (a).erase(unique(all(a)), (a).end()) #define bit(n) (1LL << (n)) #define dump(a) cerr << #a " = " << (a) << endl #define fin(a) \ cout << (a) << endl; \ return 0 using vint = vector<int>; using pint = pair<int, int>; using vpint = vector<pint>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double PI = 3.1415926535897932384626433832795028; constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int sign(int a) { return (a > 0) - (a < 0); } int cdiv(int a, int b) { return (a - 1 + b) / b; } template <typename T> T sq(T a) { return a * a; } template <typename T, typename U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <typename T, typename U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) { os << "(" << a.first << ", " << a.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; signed main() { int n; cin >> n; vint v(2 * n); rep(i, 2 * n) cin >> v[i]; vint a(n), b(n); rep(i, n) a[i] = v[n - i - 1]; rep(i, n) b[i] = v[n + i]; priority_queue_rev<int> pq; int aoki = 0; rep(i, n) { pq.emplace(a[i]); pq.emplace(b[i]); aoki += pq.top(); pq.pop(); } cout << accumulate(all(v), 0LL) - aoki << endl; }
#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; ll NUM = 10000; ll input(){ double tmp; scanf("%lf",&tmp); tmp *= NUM; return round(tmp); } bool ok(ll dx, ll dy, ll z){ return dx*dx + dy*dy <= z*z; } int main(){ ll x = input(); ll y = input(); ll z = input(); x %= NUM; y %= NUM; ll L = 0,R = 1; ll ans = 0; //上半分 for(ll h = BIG_NUM/2+5*NUM; h >= NUM; h -= NUM){ while(ok(x-L*NUM,h-y,z))L--; while(ok(R*NUM-x,h-y,z))R++; ans += (R-L)-1; } L = 0,R = 1; //下半分 for(ll h = -BIG_NUM/2-NUM; h <= 0; h += NUM){ while(ok(x-L*NUM,y-h,z))L--; while(ok(R*NUM-x,y-h,z))R++; ans += (R-L)-1; } printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) const ll MAX = 1001001; const ll MOD = 1000000007; const double pi = 2.0 * asin(1.0); ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; int main() { long double x, y, r; cin >> x >> y >> r; x = abs(x); y = abs(y); x -= (ll)x / 1; y -= (ll)y / 1; ll d = (ll)(y - r); ll u = (ll)(y + r); ll ans = 0; rep2(i, d - 1, u + 1) { if (abs(i - y) > r) { continue; } long double now2 = r * r - (y - i) * (y - i); long double l = 0; long double l2 = 1001001001; rep(i, 100) { long double mid = (l + l2) / 2; if (mid * mid > now2) { l2 = mid; } else { l = mid; } } ans += (ll)l * 2 + 1; long double x1 = (ll)l + 1; long double x2 = -(ll)l; long double eps = 0.0000000001; if ((i - y) * (i - y) + (x1 - x) * (x1 - x) <= r * r + eps) { ans++; } if ((i - y) * (i - y) + (x2 - x) * (x2 - x) > r * r + eps) { ans--; } } cout << ans << endl; }
#include <iostream> #include <algorithm> #include <stdio.h> #include <cmath> #include <vector> using namespace std; typedef long long ll; int main(){ int n; cin>>n; if(n%2 == 0) cout<<"White"<<endl; else cout<<"Black"<<endl; return 0; }
#include<bits/stdc++.h> #define ntest 0 using namespace std; int n; void solve() { cin >> n; if (n&1) cout << "Black"; else cout << "White"; } int main() { ios::sync_with_stdio(0); cin.tie(0); if(ntest){ int t; cin >> t; while(t--) solve(); } else { solve(); } }
#include<bits/stdc++.h> #include<stdio.h> using namespace std; #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define MAX 100050 #define ll long long #define ld long double #define lli long long int #define pb push_back #define INF 100000000000000 #define mod 998244353 // trignometric function always give value in Radians only #define PI acos(-1) //3.1415926535897932384626433832795028 #define dsin(degree) sin(degree*(PI/180.0)) #define dcos(degree) cos(degree*(PI/180.0)) #define dtan(degree) tan(degree*(PI/180.0)) #define rsin(radian) sin(radian) #define rcos(radian) cos(radian) #define rtan(radian) tan(radian) #define counttrailingzeroes(n) __builtin_ctzll(n) #define countsetbits(n) __builtin_popcountll(n) #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define memf(a) memset(a,false,sizeof(a)) #define loop(i,n) for (lli i = 0; i < n; i++) #define FOR(i,a,b) for (lli i = a; i < b; i++) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define makeuniq(v) v.resize(unique(all(v)) - v.begin()); //only uniq element in vector after this #define sz(x) int(x.size()) #define F first #define S second #define mii map<lli,lli> #define pii pair<lli,lli> #define vi vector<lli> #define vvi vector<vi> #define vpi vector<pii> #define vbool vector<bool> #define seti set<lli> #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) (a/gcd(a,b))*b #define abs(x) ((x < 0)?-(x):x) #define endl '\n' template <typename Head> void print(Head&& head) { cout<<head<<endl; } template <typename Head, typename... Tail> void print(Head&& head, Tail... tail) { cout<<head<<" "; print(tail...); } #define scanarr(a,n) for(lli i=0;i<n;i++) cin>>a[i]; #define scanvec(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);} #define printarr(a,n) for(lli i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; #define printvec(vec) for(auto xt : vec) cout<<xt<<" "; cout<<"\n"; #define FD(N) fixed<<setprecision(N) #define deb(x) cout<<#x<<" "<<x<<endl; /* 1D vector - vi dp(n,value); */ bool isVowel(char a) { if(a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') return true; return false; } int main(){ fastio lli t=1; //cin>>t; while(t--) { lli n; cin>>n; lli a[n]; scanarr(a,n); lli maxi = -1; for(lli i=0;i<n;i++) { lli mini = INF; for(lli j=i;j<n;j++) { lli len = j-i+1; mini = min(mini , a[j]); maxi = max(maxi , mini*len); } } print(maxi); } return 0; }
#include<bits/stdc++.h> using namespace std; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define pbds tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> */ #define MOD 998244353 #define int long long int #define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define PI 3.1415926535897932384626 #define pb push_back #define clr(x) memset(x, 0, sizeof(x)) #define MP make_pair const int N=1e5+100; const int inf=1e15; int d4x[]={1,0,-1,0}; int d4y[]={0,1,0,-1}; int d8x[]={1,1,0,-1,-1,-1,0,1}; int d8y[]={0,1,1,1,0,-1,-1,-1}; int gcd(int a,int b){if(a==0)return b;return gcd(b%a,a);} int lcm(int a,int b){return (max(a,b)/gcd(a,b))*min(a,b);} int powermodm(int x,int n,int M){int result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;} int gcdExtended(int a, int b, int *x, int *y){if (a == 0){*x = 0, *y = 1;return b;}int x1, y1;int gcd = gcdExtended(b % a, a, &x1, &y1);*x = y1 - (b / a) * x1;*y = x1;return gcd;} int modInverse(int a, int m){int x, y;int g = gcdExtended(a, m, &x, &y);if (g != 1)return -1;else{int res = (x % m + m) % m;return res;}} bool isPrime(int a){if(a==1) return false;if(a==2) return true;if(a%2==0) return false;int root=sqrt(a);for(int i=3;i<=(int)root;i+=2){if(a%i==0)return false;}return true;} bool isValidpt(int u,int v,int n,int m){if(u>=0&&v>=0&&u<n&&v<m)return true;return false;} int apne=modInverse(2,MOD); int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int tc=1; //cin >> tc; while(tc--) { int n; cin >> n; int a[n]; for(int i=0;i<n;i++) cin >> a[i]; sort(a,a+n); int cur=1; int sum=0; int ans=0; for(int i=1;i<n;i++) { sum=(sum+(cur*a[i]))%MOD; cur=(cur*2)%MOD; } for(int i=0;i<n;i++) { int yo=(sum+a[i])%MOD; ans=(ans+((yo*a[i])%MOD))%MOD; if(i<n-1) sum=(sum%MOD - a[i+1]+MOD)%MOD; sum=(sum*apne)%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<ll, ll>; int main(){ int n,x; cin >> n >> x; double sum = 0; rep(i,n) { int v,p; cin >> v >> p; sum += v * p; if(sum > x * 100){ cout << i + 1 << endl; return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #include <functional> // for less #include <iostream> using namespace __gnu_pbds; using namespace std; #define int long long #define mod 1000000007 typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; int findintegers(double a,double b) {int c=floor(b); int d=ceil(a); cout<<d-c+1<<" okpaka\n"; return d-c+1; } int32_t main() { #ifndef ONLINE_JUDGE freopen("inp.txt", "r", stdin); freopen("m3.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); long double x,y,r; cin>>x>>y>>r; r+=(1e-14); long double nx1=x+r; long double nx2=x-r; int ub1=ceil(nx2); int ub2=floor(nx1); int sum=0; for(int i=ub1;i<=ub2;i++) { long double c=r*r-(i-x)*(i-x); if(c>=0) {long double val1=sqrt(c)+y; long double val2=-sqrt(c)+y; int lb1=floor(val1); int lb2=ceil(val2); sum+=(lb1-lb2+1); } } cout<<sum<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define input_output freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); template <typename Arg1> void prn(Arg1&& arg1) { cout<<arg1<<"\n";} template <typename Arg1, typename... Args> void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); } template <typename Arg1> void prs(Arg1&& arg1) { cout<<arg1<<" ";} template <typename Arg1, typename... Args> void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); } template <typename Arg1> void read(Arg1&& arg1) { cin>>arg1; } template <typename Arg1, typename... Args> void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); } #define int long long #define all(ds) ds.begin(), ds.end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define gcd(x,y) __gcd(x,y) #define rep(i,a,b) for(int i=a;i<b;i++) #define precise(x) cout<<fixed<<setprecision(x) #define endl "\n" const long long INF = 1e18; const int32_t MOD = 1e9+7; const int N = 1e5+5; int n, m, k, q, r, l, x, y, z; int a[N], b[N], c[N]; string s, t; int ans = 0; void solve(){ read(n,m,q); pair<int, int> a[n]; // val, wt rep(i,0,n){ read(x,y); a[i] = {y,x}; } sort(a, a+n); reverse(a, a+n); int c[m]; rep(i,0,m){ read(c[i]); } rep(i,0,q){ read(l,r); l--, r--; vector<int>v; for(int i = 0; i < m; ++i){ if(i >= l && i <= r) continue; v.push_back(c[i]); } sort(all(v)); int ans = 0; for(int i = 0; i < n; ++i){ int val = a[i].first, wt = a[i].second; for(int i = 0; i < v.size(); ++i){ if(v[i] == -1) continue; if(wt <= v[i]){ ans += val; v[i] = -1; break; } } } prn(ans); } } signed main() { #ifndef ONLINE_JUDGE input_output #else fastio #endif #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t = 1; // cin>>t; while(t--){ solve(); } return 0; }
#include <iostream> using namespace std; typedef long long ll; const int maxn = 1e6+10,mod = 1e9+7; int a,b,x,y,f[maxn][2]; int main() { cin >> a >> b >> x >> y; f[a<b?a:b][a<b?0:1] = 0; f[a<b?a:b][a<b?1:0] = x; for(int i = min(a,b)+1; i <= max(a,b); i++){ f[i][0] = min(f[i-1][0]+y,f[i-1][1]+x); f[i][1] = min(f[i][0]+x,f[i-1][1]+y); } cout << f[a<b?b:a][a<b?1:0] << endl; return 0; }
#include<bits/stdc++.h> #define ull unsigned long long # define ll long long # define mod 1000000007LL using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll arr[63][63]; for(int i=0;i<63;i++) { for(int j=0;j<=i;j++) { if(i==j) { arr[i][j]=1; } else if(j==0) { arr[i][j]=1; } else arr[i][j]=arr[i-1][j]+arr[i-1][j-1]; } } ll a,b,k; cin>>a>>b>>k; ll n=a+b; for(int i=0;i<n;i++) { if(arr[a+b-1][b]>=k) { cout<<'a'; a--; } else { cout<<'b'; k=k-arr[a+b-1][b]; b--; } } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<queue> #include<cmath> #include<string> using namespace std; int main() { int n,q; string s,s1,s2; cin >> n >> s >> q; int t,a,b; for(int i=0;i<n;i++){ s1+=s[i]; s2+=s[i+n]; } for(int i=0;i<q;i++){ cin >> t >> a >> b; if(t==1){ if(b<=n) swap(s1[a-1],s1[b-1]); else if(a>n) swap(s2[a-n-1],s2[b-n-1]); else swap(s1[a-1],s2[b-n-1]); } if(t==2) swap(s1,s2); } s=s1+s2; cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define test() int t;cin>>t;for(int test=1;test<=t;test++) #define pb push_back #define nl cout<<"\n" #define F first #define S second #define all(x) x.begin(),x.end() template<class C> void min_self( C &a, C b ){ a = min(a,b); } template<class C> void max_self( C &a, C b ){ a = max(a,b); } const ll MOD = 1000000007; ll mod( ll n, ll m=MOD ){ n%=m,n+=m,n%=m;return n; } const int MAXN = 1e5+5; const int LOGN = 21; const ll INF = 1e14; int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; template<class T1, class T2> void add( T1 &x, T2 y, ll m = MOD ) { x += y; if( x >= m ) x -= m; } template<class T1, class T2> void sub( T1 &x, T2 y, ll m = MOD ) { x -= y; if( x < 0 ) x += m; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio(); int a,b; cin>>a>>b; a += b; if( a >= 15 && b >= 8 ) { cout<<1; } else if( a >= 10 && b >= 3 ) { cout<<2; } else if( a >= 3 ) { cout<<3; } else { cout<<4; } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; a +=b; if(a>=15&&b>=8){ cout<<1; }else if(a>=10&&b>=3){ cout<<2; }else if(a>=3){ cout<<3; }else{ cout<<4; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define INF 1e9 #define PI 3.14159265359 #define MOD 1000000007 #define ALL(v) v.begin(),v.end() #define ALLR(v) v.rbegin(),v.rend() #define F first #define S second typedef long long ll; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; //isPrime //modpow modinv //getDigit template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { cout << fixed << setprecision(10); string s; cin >> s; string f,b; int m; bool r = false; rep(i,s.size()){ if(s[i]=='R'){ r = !r; continue; } if(!r){ b.push_back(s[i]); m = b.size(); if(b[m-2]==b[m-1]){ b = b.erase(m-2); } }else{ f.push_back(s[i]); m = f.size(); if(f[m-2]==f[m-1]){ f = f.erase(m-2); } } } int ans = 0; rep(i,min(b.size(),f.size())){ if(b[i]==f[i]){ ans = i; }else{ break; } } b = b.substr(ans); f = f.substr(ans); string t; if(!r){ reverse(ALL(f)); t = f+b; }else{ reverse(ALL(b)); t = b + f; } //cout << t << endl; bool a = true; while(a){ a = false; for(int i = 0; i+1 < t.size(); i++){ if(t[i]==t[i+1]){ a = true; t = t.erase(i,2); } } } cout << t << endl; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> #include <functional> #include <queue> using namespace std; typedef long long ll; using pii = pair<int, int>; int main(){ string s; cin >> s; int n = s.length(); // vector<string> res; // int p = 0; // for (int i = 0; i < n; ++i){ // if(s[i] == 'R'){ // string t = s.substr(p, i - p); // reverse(t.begin(), t.end()); // res.push_back(t); // p = i + 1; // } // } // string t = s.substr(p, n); // res.push_back(t); // for(const auto& x : res) // cout << x << endl; deque<char> res; int op = 0; for (int i = 0; i < n; ++i){ if(s[i] == 'R'){ op ^= 1; }else{ if(!op){ if(!res.empty() && res.back() == s[i]){ res.pop_back(); }else res.push_back(s[i]); }else{ if(!res.empty() && res.front() == s[i]){ res.pop_front(); }else res.push_front(s[i]); } } } string ans; for(const auto&c : res) ans += c; if(op) reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include<bits/stdc++.h> #define pb push_back #define int long long using namespace std; int tt,n,inf=1e15; void solve(){ cin>>n; int a[n+1],ans=0; a[0]=0; map<int,int> m; m[0]=1; for(int i=1;i<=n;i++){ int x;cin>>x; if(i%2)a[i]=a[i-1]-x; else a[i]=a[i-1]+x; m[a[i]]++; } // for(int i=0;i<=n;i++)cout<<m[a[i]]<<" "; // cout<<'\n'; for(int i=0;i<=n;i++){ int x=m[a[i]]; if(x){ ans+=((x*(x-1))/2); m[a[i]]=0; } } cout<<ans<<'\n'; } signed main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //cin>>tt; //while(tt--) solve(); }
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define int long long void solve() { int n; double ans = 100000000000000000.0; cin >> n; vector< int > v(n); unordered_map<int,int> pref; double sum = 0; for ( int i=0; i<n; i++ ) { cin >> v[i]; sum += double(v[i]); } sort(v.begin(), v.end()); for ( int i=0; i<n; i++ ) { pref[i] = pref[i-1] + v[i]; } for ( int i=0; i<n; i++ ) { double res = double(n)*double(v[i]/double(2)) + sum; res -= double(pref[i]); res -= double(n-i-1) * double(v[i]); res = res / double(n); ans = min(ans, res); } printf("%.7lf", ans); // cout << setprecision(7) << ans << '\n'; } signed main() { int tc = 1; // cin >> tc; while( tc-- ) { solve(); } } // Tarun IIITA
// Always remember that you are absolutely unique, just like everyone else! #include <iostream> #include <vector> using namespace std; // DEBUGGING SECTION void __print(int x) {cerr << x;} void __print(long long 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 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...);} #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL) #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define MOD 1000000007 #define INF 3e18 #define pb push_back #define endl '\n' // Remove if interactive #define ff first #define ss second #define FOR(i, a, b) for(int i = a; i < b; i++) //#include <algorithm> //#include <cstring> //#include <map> //#include <set> //#include <queue> //#include <stack> //#include <math.h> //#include <bits/stdc++.h> #define int long long #define pii pair<int, int> #define vi vector<int> #define vvi vector<vi> #define vpii vector<pii> #define all(x) x.begin(), x.end() int power(int x, int y) { int ans = 1; x %= MOD; while (y) { if (y & 1) ans = (x * ans) % MOD; x = (x * x) % MOD; y >>= 1; } return ans; } // comparator(A, B) -> should return true if A needs to come before B! // ALWAYS RETURN FALSE IF A == B // USE INTEGERS TO REDUCE RUNTIME! void solve() { int n; cin >> n; int arr[n]; FOR(i, 0, n) { cin >> arr[i]; } vi rem(200, 0); int ans = 0; FOR(i, 0, n) { int r = arr[i] % 200; ans += rem[r]; rem[r]++; } cout << ans << endl; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif fastio; int t = 1; //cin >> t; while (t--) { solve(); } }
#include <cstdio> #define MOD 200 using namespace std; int cnt[MOD]; int main() { int n; scanf("%d", &n); long long ans = 0LL; while(n--) { int x; scanf("%d", &x); ans += cnt[x %= MOD] ++; } printf("%lld\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); double v,t,s,d; cin>>v>>t>>s>>d; double cur = d/v; if(cur<t || cur>s) cout<<"Yes\n"; else cout<<"No\n"; return 0; }
/******************************************************/ /******************************************************/ /** **/ /** BISMILLAHIR RAHMANIR RAHIM **/ /** REAZ AHAMMED CHOWDHURY - reaziii **/ /** Department of Computer Science and Engineering **/ /* INSTITUTE OF SCIENCE AND TECHNOLOGY **/ /** **/ /******************************************************/ /******************************************************/ #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pcase(x) printf("Case %d: ",x++) #define fri(f) for(int i=0;i<f;i++) #define frj(f) for(int j=0;j<f;j++) #define reset(x) memset(x,-1,sizeof(x)) #define all(x) x.begin(),x.end() #define input freopen("input.txt","r",stdin); #define output freopen("output.txt","w",stdout) #define infi INT_MAX #define linfi LLONG_MAX #define pii pair<int,int> #define pll pair<ll,ll> #define mgraph map<int,vector<int> > #define pb push_back #define clr(x) memset(x,0,sizeof(x)) #define fro(i,x,y) for(int i=x;i<y;i++) #define ech(x,a) for(auto &x : a) #define ff first #define ss second #define vi vector<int> #define vl vector<ll> #define pi acos(-1.0) using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> Ordered_set; template<class T> void read(T& x) {cin >> x;} template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);} template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);} template<class H> void print(vector<H> &x) {ech(a, x) cout << a << " "; cout << endl;} template<class P> void debug(P h) { #ifndef ONLINE_JUDGE cerr << h << " "; #endif } template<class W, class... V> void debug(W h, V... t) { #ifndef ONLINE_JUDGE debug(h); debug(t...); cerr << endl; #endif } typedef long long int ll; typedef long double ld; typedef unsigned long long int ull; bool checkbitt(ll num, int pos) {return (num >> pos) & 1;} ll setbitt(ll num, ll pos) {return (1 << pos) | num;} ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;} ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;} ll geti() {ll x; read(x); return x;} ll cdiv(ll a, ll b) {ll ret = a / b; if (a % b) ret++; return ret;} const ll mod = 1e9 + 7; const ll N = 2e5 + 10; int dx[4] = { +0, +0, +1, -1}; int dy[4] = { -1, +1, +0, +0}; //................................___Start from here___...............................// //................................_____________________..............................// int solve() { ll a, b, c, d; read(a, b, c, d); b *= a, c *= a; if (d >= b && d <= c) { cout << "No" << endl; } else cout << "Yes" << endl; return 0; } int main(int argc, char* argv[]) { if (argc <= 1) { #ifndef ONLINE_JUDGE input; output; #endif #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(0); #endif } int cs = 1, cn = 1; // read(cs); while (cs--) { solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef pair<ll, ll> PP; struct edge { ll to; // 辺の行き先 ll weight; // 辺の重み edge(ll t, ll w) : to(t), weight(w) { } }; using Graph = vector<vector<ll>>; using WeightedGraph = vector<vector<edge>>; #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define pb push_back const ll INF = (1LL << 31) - 1; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vl a; ll sum=0; rep(i,n){ ll x; cin >> x; sum += x; a.pb(sum); } vl s; ll y=0; s.pb(0); rep(i,n){ y += a[i]; s.pb(y); } vl m(n+1,0); for(ll i=1;i<n+1;i++){ m[i]=max(m[i-1],a[i-1]); } ll ans = 0; for(ll i=1;i<n+1;i++){ ans = max(ans, s[i-1]+m[i]); } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #include <variant> #define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++) #define rep(i,n) rep2(i,0,n) #define all(x) begin(x),end(x) #ifdef ENV_LOCAL #define dump if (1) cerr #else #define dump if (0) cerr #endif using namespace std; using namespace std::string_literals; using i32 = int32_t; using i64 = int64_t; using f64 = double; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; //using namespace harudake; tuple<i32,i32,i32> tree_dp(const vector<vi32>& g, i32 th, i32 i, i32 p = -1) { i32 ans = 0; i32 deepest = 0; i32 cover = -1e8; for (auto&& to : g[i]) { if (to == p) continue; auto [cst, dep, cov] = tree_dp(g, th, to, i); ans += cst; cover = max(cover, cov-1); deepest = max(deepest, dep+1); } if (deepest <= cover) deepest = -1e8; if (deepest == th) { ++ans; deepest = -1e8; cover = th; } return make_tuple(ans, deepest, cover); } //void dfs(const vector<vi32>& g, vi32& d, i32 i, i32 p = -1) { // for (auto&& to : g[i]) { // if (to == p) continue; // d[to] = d[i] + 1; // dfs(g, d, to, i); // } //} // //i32 get_to_put(const vector<vi32>& g, const vector<bool>& covered, i32 th) { // i32 n = size(g); // rep(i,n) { // if (!covered[i]) { // vi32 d(n, -1); // d[i] = 0; // dfs(g, d, i); // i32 mx = -1; // i32 mxj = 0; // rep(j,n) { // if (!covered[j]) { // if (d[j] > mx) { // mx = d[j]; // mxj = j; // } // } // } // vi32 d2(n, -1); // d2[mxj] = 0; // dfs(g, d2, mxj); // mx = -1; // i32 j2 = 0; // rep(j,n) { // if (!covered[j]) { // if (d[j] > mx) { // mx = d[j]; // j2 = j; // } // } // } // if (mx <= th) { // return j2; // } // vi32 d3(n, -1); // d3[j2] = 0; // dfs(g, d3, j2); // rep(j,n) { // if (d2[j] == th && d2[j] + d3[j] == d2[j2]) { // return j; // } // } // return -1; // } // } // return -1; //} bool check(const vector<vi32>& g, i32 k, i32 th) { auto [ans, deepest, cover] = tree_dp(g, th, 0); if (deepest >= 0) ++ans; return ans <= k; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i32 n,k; cin>>n>>k; vector<vi32> g(n); rep(i,n-1) { i32 u,v; cin>>u>>v; --u;--v; g[u].push_back(v); g[v].push_back(u); } i32 lt = 0; i32 ge = n; while (ge - lt > 1) { i32 mid = (ge + lt) / 2; if (check(g, k, mid)) { ge = mid; } else { lt = mid; } } cout<<ge<<endl; return 0; }
#pragma GCC target ("avx2") #pragma GCC optimize ("unroll-loops") #pragma GCC optimize ("O3") #include "bits/stdc++.h" #include <unordered_set> #include <unordered_map> #include <random> using namespace std; typedef long long ll; const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for(int (i)=0; (i)<(n); (i)++) const int dy[4]={ -1,0,1,0 }; const int dx[4]={ 0,-1,0,1 }; int N; signed main(){ cin >> N; if(N%2 == 0) cout << "White" << endl; else cout << "Black" << endl; }
// @includes_template #ifndef INCLUDES_H #define INCLUDES_H #include<iostream> #include<vector> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #define s64i long long #define u64i unsigned long long #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define all(x) (x).begin(),(x).end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) // #define sz(x) (int)((x).size()) #define ff first #define ss second #define pii pair<int,int> #define ffloop(i,a,b,step) for(int i=a;i<b;i+=step) #define bfloop(i,a,b,step) for(int i=a;i>b;i-=step) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) 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;} using namespace std; #endif // @contest_template #include<algorithm> namespace contest_ns{ void solve(){ /** * new code goes here */ int x,y; cin>>x>>y; if(x == 0 && (y ==0)){ cout<<0<<endl; }else if(x == 0 && y==1){ cout<<2<<endl; }else if(x == 0 && y==2){ cout<<1<<endl; }else if((x == 1) && (y == 0)){ cout<<2<<endl; }else if(x == 1 && y == 1){ cout<<1<<endl; }else if(x == 1 && y == 2){ cout<<0<<endl; }else if(x == 2 && y == 0){ cout<<1<<endl; }else if(x == 2 && y == 1){ cout<<0<<endl; }else if(x == 2 && y == 2){ cout<<2<<endl; } } // template<typename T> void testcase(){ // T t; long long int t; std::cin>>t; while(t>0){ solve(); t--; } } } int main(){ // @problem code contest_ns::solve(); return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 1e9 + 7; #define F first #define S second #define inf 1e16 int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; ll price = floor(1.08*n); if(price < 206){ cout << "Yay!\n"; } else if(price == 206){ cout << "so-so\n"; } else { cout << ":(\n"; } return 0; }
#include<cstdio> #include<algorithm> #include<cmath> #include<queue> using namespace std; long long n; long long maxn; priority_queue<long long,vector<long long>,greater<long long> > q; int main() { scanf("%lld",&n); for(long long i=1;i<=sqrt(n);i++) { if(n%i==0) { q.push(i); maxn++; if(n!=i*i) { q.push(n/i); maxn++; } } } while(!q.empty()) { printf("%lld\n",q.top()); q.pop(); } return 0; }
#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <cstring> #include <chrono> #include <complex> #define endl "\n" #define ll long long int #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #define all(c) c.begin(),c.end() #define mp(x,y) make_pair(x,y) #define mem(a,val) memset(a,val,sizeof(a)) #define eb emplace_back #define f first #define s second using namespace std; vector<ll> solve(ll t, vector<ll> l, vector<ll> r) { vector<ll> res(t); for(ll i = 0ll; i < t; i++) { ll count = max(r[i] - 2ll * l[i] + 1ll, 0ll); res[i] = count * (count + 1ll) / 2ll; } return res; } int main() { std::ios::sync_with_stdio(false); ll t; cin >> t; vector<ll> l(t), r(t); for(int i = 0; i < t; i++) { cin >> l[i] >> r[i]; } vector<ll> res = solve(t, l, r); for(ll i=0ll;i<t; i++) { cout << res[i] << endl; } return 0; }
#include<bits/stdc++.h> #define ll long long using namespace std ; void solve() { ll l , r ; cin>>l>>r ; if(r-l*2>=0){ cout<< (r-2*l+1) * (r-2*l+2) / 2 <<endl; }else { cout<< 0 <<endl; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin.tie(NULL); int t ; cin>>t ; while(t--){ solve() ; } return 0 ; }
//thanks God For Every Thing! //contest link: #include<bits/stdc++.h> #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <cmath> #include <cstdio> #define pi acose(-1) #define flush cin.ignore(numeric_limits<streamsize>::max(),'\n'); #define fasterio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define NL printf("\n") #define fin freopen("int.txt","r",stdin); #define fout freopen("out.txt","w",stdout); using namespace std; typedef long long int ll; typedef double dl; // ---------------------- slove---------------------------// int main() { //fin //fout string s; getline(cin,s); ll ln=0; ln = s.length(); int x1=0, x2=0, x3=0, chekr1=0, chekr2=0; int flg1=0, flg2=0, flg3=0, x=0; if(ln==1) { if(s[0] == '8') { printf("Yes"); NL; } else { printf("No"); NL; } return 0; } else if(ln==2) { x1 = s[ln-1]-48; x2 = s[ln-2]-48; chekr2 = ((x2*10)+x1); chekr1 = ((x1*10)+x2); if(chekr1%8==0 || chekr2%8==0) { printf("Yes"); NL; } else { printf("No"); NL; } return 0; } else if(ln>=3) { int arr[10]={0}; for(int c=0; c<ln; c++) { arr[s[c]-48]++; } for(int i=104; i<=992; i+=8) { int d1, d2, d3, n; n = i; d1 = n%10; n/=10; d2 = n%10; d3 = n/10; if(arr[d1]>0) { arr[d1]--; if(arr[d2]>0) { arr[d2]--; if(arr[d3]>0) { printf("Yes"); NL; return 0; } arr[d2]++; } arr[d1]++; } } printf("No"); NL; return 0; } }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main(){ string s; cin >> s; vector<int> a(10); rep(i, s.length()){ a[s.at(i)-'0']++; } int st = 0; int end = 0; int t = 0; if(s.length() == 1){ st = 1; end = 2; t = 1; }else if(s.length() == 2){ st = 2; end = 13; t = 2; }else{ st = 13; end = 126; t = 3; } for(int i=st; i<end; i++){ int l = 8*i; int f = 0; vector<int> b(10); rep(i, t){ b[l%10]++; l /= 10; } if(b[0] != 0) continue; rep(i, 10){ if(b[i] > a[i]) f = 1; } if(f == 0){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
#pragma GCC optimize("O3") #pragma GCC target("avx2") #include <algorithm> #include <iostream> #include <bitset> #include <vector> #include <array> using namespace std; #define f(x,y,z) for(int x=y;x<=z;++x) vector<int> v[200001]; int n, a, b, it; array<int, 200001> jr, dlm, nmr; bitset<200001> sdh; void jrk(int x) { for(int &i:v[x]) if(!jr[i]) { jr[i] = jr[x]+1, jrk(i); } } void trn(int x) { sdh[x] = true; for(int &i:v[x]) if(!sdh[i]) { trn(i), dlm[x] = max(dlm[x], dlm[i]+1); } } void isi(int x) { sdh[x] = true, nmr[x] = a; vector<pair<int, int>> mn; for(int &i:v[x]) if(!sdh[i]) mn.push_back({dlm[i], i}); sort(mn.begin(), mn.end()); for(auto &i:mn) ++a, isi(i.second), ++a; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; f(i,2,n) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } jr[1] = a = 1, jrk(1); it = max_element(jr.begin()+1, jr.begin()+n+1)-jr.begin(); trn(it), sdh.reset(), isi(it); f(i,1,n) cout << nmr[i] << ' '; }
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=a;i<=b;i++) const int Nmax=2e5+9; int N; string S[Nmax]; map <string,int> M; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>N; REP(i,1,N) cin>>S[i]; REP(i,1,N) { if (S[i][0]!='!') M[S[i]]=1; } REP(i,1,N) { if (S[i][0]!='!') continue; S[i].erase(S[i].begin()); if (M[S[i]]==1) { cout<<S[i]; return 0; } } cout<<"satisfiable"; return 0; }
# define _GLIBCXX_DEBUG # define rep(i, n) for (int i=0; i<(int)(n); i++) # define FOR(i,a,b) for(int i=(a);i<=(b);++i) # define ALL(a) (a).begin(), (a).end() # define COUT(x) cout << (x) << endl; # define KAI() cout << endl; # define INF 10000000000000000LL typedef long long ll; # include <iostream> # include <string> # include <cmath> # include <algorithm> # include <vector> # include <set> # include <bitset> # include <iomanip> using namespace std; int main() { int n; double D,H; cin >> n >> D >> H; vector<double> d(n); vector<double> h(n); rep(i,n) cin >> d[i] >> h[i]; double ans=0.0; rep(i,n) { double temp; temp = (h[i]-H*d[i]/D)/(1.0-d[i]/D); // cout << "i=" << i << ", temp=" << temp << endl; if (temp < 0.0) continue; else ans = max(ans,temp); } cout << fixed << setprecision(15) << ans << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ long double x,y,r; cin>>x>>y>>r; long long ans=0; r+=1e-14; for(long long i=ceil(x-r);i<=floor(x+r);i++) { long double t=sqrt(r*r-(i-x)*(i-x));//r' ans+=(floor(y+t))-(ceil(y-t))+1; } cout<<ans; }
#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 std; using namespace __gnu_pbds; #define endl "\n" //interactive #define fi first #define se second #define pb push_back #define int long long #define ll long long #define ld long double #define pii pair<ll,ll> typedef vector<int> vi; #define read(t) ll t;cin>>t; #define all(value) value.begin(),value.end() #define f(a,b,c) for(ll a=b;a<c;a++) #define F(i,n) for(ll i=0;i<n;i++) #define deb(value) cerr << #value << " is =" << value << endl; #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ; typedef tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update> omultiset; // store ind on sec typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> oset; typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> oset1; // decreasing /* member functions : 1. order_of_key(k) : number of elements strictly lesser than k 2. find_by_order(k) : k-th element in the set (counting from zero)*/ //a.erase(unique(a.begin(),a.end()),a.end()); double pi=3.141592653589793238462641; ll dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; const int mod = 1e9+7 ; const int mod2 = 998244353; const int inf = 1e18; long long binpow(int a,int b,int m) { a %= m;int res = 1; while (b > 0) { if (b & 1)res=(res*a)%m; a=(a*a)%m ; b/=2; } return res; } int modinv(int n,int m) {return binpow(n,m-2,m);} const int man=2e5+5; /* $ don't apply unnecessary operations $ do check boundary cases (i==1/n-1/n) $ change approach / use pen ? paper $ Don't stuck! :- * re-solve * check for another sol * check function , cal */ /*......................................................................................................................*/ void solve() { int n; cin>>n; for(int i=1;i<=n;i++) { int a=(2*i)%n; if(a==0)a=n; int b=(2*i+1)%n; if(b==0)b=n; cout<<a<<" "<<b<<endl; } } signed main() { fast; int test=1; // cin>>test; while(test--)solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int signed main(){ int t = 1; cin >> t; while(t--){ int n; cin >> n; vector<int> a(n); for(auto &x:a) cin >> x; sort(a.begin(),a.end()); if(n%2) { cout << "Second\n"; continue; } int even = 0, odd = 0; for(int i = n-1; i >= 0; i -= 2) odd += a[i]; for(int i = n-2; i >= 0; i -= 2) even += a[i]; if(even == odd) cout << "Second\n"; else cout << "First\n"; } }
#include<bits/stdc++.h> using namespace std; using P=pair<int,int>; constexpr int INF=1e9; struct edge{int to,cost;}; bool chmin(int&a,int b){return a>b?a=b,true:false;} int main(){ int H,W; cin>>H>>W; vector<vector<int>>A(H,vector<int>(W-1)),B(H-1,vector<int>(W)); for(int i=0;i<H;i++)for(int j=0;j<W-1;j++)cin>>A[i][j]; for(int i=0;i<H-1;i++)for(int j=0;j<W;j++)cin>>B[i][j]; vector<vector<edge>>G(H*W); for(int i=0;i<H;i++)for(int j=0;j<W-1;j++)G[i*W+j].push_back({i*W+j+1,A[i][j]}); for(int i=0;i<H;i++)for(int j=1;j<W;j++)G[i*W+j].push_back({i*W+j-1,A[i][j-1]}); for(int i=0;i<H-1;i++)for(int j=0;j<W;j++)G[i*W+j].push_back({i*W+j+W,B[i][j]}); for(int i=1;i<H;i++)for(int j=0;j<W;j++){ for(int k=0;k<i;k++)G[i*W+j].push_back({k*W+j,i-k+1}); } vector<int>dist(H*W,INF); dist[0]=0; priority_queue<P,vector<P>,greater<P>>Q; Q.push({0,0}); while(!Q.empty()){ auto[c,v]=Q.top(); Q.pop(); if(dist[v]<c)continue; for(edge e:G[v])if(chmin(dist[e.to],c+e.cost))Q.push({dist[e.to],e.to}); } cout<<dist[H*W-1]<<endl; }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b){ ll res = 1; while(b){ if(b&1)res = res * x % MOD; x = x * x % MOD; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } ll in(){ ll res; scanf("%lld", &res); return res; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } } using namespace templates; ll func(){ int n = in(); int L = in(); vector<ll> start(n); vector<ll> goal(n); foreach(i,start)i=in(); foreach(i,goal)i=in(); vector<ll> x(n+1); vector<ll> y(n+1); x[0] = start[0]-1; y[0] = goal[0]-1; x.back() = L - start.back(); y.back() = L - goal.back(); rep(i,1,n){ x[i] = start[i]-start[i-1]-1; y[i] = goal[i]-goal[i-1]-1; } int last = 0; ll res = 0; rep(i,n+1){ while(last < n + 1 and x[last]==0)++last; if(y[i]==0)continue; ll sum = 0; int itr = last; while(sum < y[i]){ sum += x[itr++]; } if(sum != y[i])return -1; if(last < i and i < itr){ res += i - last; res += itr - i - 1; }else{ res += max(abs(i-last),abs(i-(itr-1))); } last = itr; } return res; } int main(){ cout << func() << endl; return 0; }
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <iomanip> #include <functional> #include <bitset> #include <limits> #include <cstdio> #include <cmath> #include <cassert> #include <random> #ifdef DEBUG #include "library/Utility/debug.cpp" #else #define debug(...) #endif #define rep(i,n) for(int i=0;i<(n);++i) #define EL '\n' #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() using lnt = long long; struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO; template<typename T> using V = std::vector<T>; template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; } /*-*/ struct C { int size; int c[2]; C(int size, int cc[2]) : size(size) { c[0]=cc[0]; c[1]=cc[1]; } bool operator<(C rhs) const { return c[0]-c[1]<rhs.c[0]-rhs.c[1]; }; }; int main() { int n; std::cin >> n; V<V<int> > g(n); for(int i=1;i<n;i++) { int p; std::cin >> p; p--; g[p].emplace_back(i); } auto dfs = [&](auto f, int i) -> C { int size=1; V<C> v; for(auto e:g[i]) { v.emplace_back(f(f,e)); } int m=v.size(); int o[2]; if(m==0) { o[0]=1; o[1]=0; C c(1,o); return c; } if(m==1) { o[0]=v[0].c[0]+1; o[1]=v[0].c[1]; C c(v[0].size+1,o); return c; } rep(i,m) size+=v[i].size; o[0]=1; o[1]=0; C c(size,o); V<C> vo,ve; rep(i,m) { if(v[i].size&1) vo.emplace_back(v[i]); else ve.emplace_back(v[i]); } std::sort(all(vo)); rep(i,vo.size()) { if(i&1) { c.c[0]+=vo[i].c[1]; c.c[1]+=vo[i].c[0]; } else { c.c[0]+=vo[i].c[0]; c.c[1]+=vo[i].c[1]; } } int odd=vo.size()&1; for(auto e:ve) { if(e.c[0]-e.c[1]<0) { c.c[0]+=e.c[0]; c.c[1]+=e.c[1]; } else { c.c[0]+=e.c[odd]; c.c[1]+=e.c[1^odd]; } } return c; }; print(dfs(dfs,0).c[0]); }
/*Author--Vishwas Agrawal*/ #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; typedef long long int ll; #define vll vector<ll> #define pb push_back #define mp make_pair #define test \ ll t; \ cin >> t; \ while (t--) #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define M 1000000007LL #define input(v, n) \ inc(i, 0, n) \ { \ ll x; \ cin >> x; \ v.pb(x); \ } #define print(v, n) \ inc(i, 0, n) { cout << v[i] << " "; } #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define inc(i, a, n) for (ll i = a; i < n; i++) #define dec(i, a, n) for (ll i = n - 1; i >= a; i--) long long binpow(long long base, long long exp, int mod) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % mod; exp = exp >> 1; base = (base * base) % mod; } return res; } long long mod(long long x) { return ((x % M + M) % M); } long long add(long long a, long long b) { return mod(mod(a) + mod(b)); } long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } bool Is_Prime(long long x) { if (x % 2 == 0) return false; for (int i = 3; i * i <= x; i += 2) if (x % i == 0) return false; return true; } int main() { ll n; cin >> n; string s; cin >> s; ll count = 0; inc(i, 0, s.length()) { ll arr[4] = {0}; if (s[i] == 'A') arr[0]++; if (s[i] == 'T') arr[1]++; if (s[i] == 'C') arr[2]++; if (s[i] == 'G') arr[3]++; inc(j, i + 1, s.length()) { if (s[j] == 'A') arr[0]++; if (s[j] == 'T') arr[1]++; if (s[j] == 'C') arr[2]++; if (s[j] == 'G') arr[3]++; if (arr[0] == arr[1] && arr[2] == arr[3]) { count++; } } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (decltype(n) i = 0, i##_len = (n); i < i##_len; ++i) #define reps(i, n) for (decltype(n) i = 1, i##_len = (n); i <= i##_len; ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pl(s) cout << (s) << "\n"; #define plx(s) {cout << (s) << "\n"; exit(0);} #ifdef __LOCAL #include <dump.hpp> #define dump(...) DUMPOUT << " " << string(#__VA_ARGS__) << ": " << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl << " ", dump_func(__VA_ARGS__) #else #define dump(...) #endif using ll = long long; using ld = long double; template<class T> using V = vector<T>; template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;} template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;} template<class T> istream &operator>>(istream &is, complex<T> &v) {T x, y; is >> x >> y; v.real(x); v.imag(y); return is;} template<class T> istream &operator>>(istream &is, V<T> &v) {for (auto&& e : v) is >> e;return is;} template<class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) {for (auto&& e : v) is >> e;return is;} template<class T> inline string join(const T& v, string sep = " ") {if (v.size() == 0) return "" ;stringstream ss;for (auto&& e : v) ss << sep << e;return ss.str().substr(1);} constexpr ll TEN(int n) {return (n == 0) ? 1 : 10 * TEN(n - 1);} struct IOS {IOS() {cin.tie(nullptr); ios::sync_with_stdio(false); dump("");}} IO; constexpr int INF = (1 << 30) - 1; constexpr ll INFL = 1LL << 60; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; struct Solver { V<string> in, out; ll output_n, input_m; bool isSubString(string &b) { rep(i,output_n) rep(j,output_n) rep(d,2) { bool ok = true; rep(k,sz(b)) { ll ni = (i+dy[d]*k) % output_n; ll nj = (j+dx[d]*k) % output_n; if (out[ni][nj] != b[k]) { ok = false; break; } } if (ok) return true; } return false; } ld computeScore() { ll used_count = 0; ll space_count = 0; rep(i,output_n) rep(j,output_n) if (out[i][j] == '.') ++space_count; rep(k,input_m) if (isSubString(in[k])) ++used_count; ld score = 0; if (used_count < input_m) { score = (ld)(TEN(8) * used_count) / (ld)(input_m); } else { score = (ld)(TEN(8) * 2 * output_n * output_n) / (ld)(2 * output_n * output_n - space_count); } dump(used_count,input_m,score); return round(score); } void solve() { cin >> output_n >> input_m; in.resize(input_m); out.resize(output_n); cin >> in; V<pair<int,string>> in_for_sort(input_m); rep(i,input_m) in_for_sort[i] = {sz(in[i]), in[i]}; sort(rall(in_for_sort)); string tmp = ""; rep(i,input_m) tmp += in_for_sort[i].second; rep(i,output_n) out[i] = tmp.substr(i*output_n, output_n); ll score = computeScore(); dump(score); pl(join(out,"\n")); } } solver; signed main(void) {solver.solve();return 0;}
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template<class T> using vc = vector<T>; template<class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i,n) for (auto& i: n) template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;} template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;} struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_; #ifdef DEBUG template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; } template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; } template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; } template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; } template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); } #endif const ll INF = 4e18; const ld EPS = 1e-5; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// int main() { ll N; cin >> N; vc<complex<ld>> S(N), T(N); rep(i, N) { ld a, b; cin >> a >> b; S[i] = {a, b}; } rep(i, N) { ld a, b; cin >> a >> b; T[i] = {a, b}; } complex<ld> sc(0, 0), tc(0, 0); rep(i, N) sc += S[i], tc += T[i]; sc /= N, tc /= N; rep(i, N){ S[i] -= sc; T[i] -= tc; } rep(i, N) rep(j, N){ vc<complex<ld>> ss(N), tt(N); rep(si, N) ss[si] = S[si] * polar(1.0L, -arg(S[i])); rep(ti, N) tt[ti] = T[ti] * polar(1.0L, -arg(T[j])); bool ok = true; rep(si, N) { bool isok = false; rep(ti, N){ if (abs(ss[si] - tt[ti]) < EPS) isok = true; } if(!isok){ ok = false; break; } } if(ok){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; }
#include<bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif void solve() { string s; cin>>s; int n=s.size(); s="."+s; string t=".atcoder"; int res=-1; for(int i=0; i<=7; i++) { string a=s; int cur=0; for(int j=1; j<=i; j++) { bool found=false; for(int k=j; k<=n; k++) { if(a[k]==t[j]) { found=true; for(int z=k; z>j; z--) { cur++; swap(a[z],a[z-1]); } break; } } if(!found) cur=1e9; } bool have=false; if(i!=7) { for(int k=i+1; k<=n; k++) { if(a[k]>t[i+1]) { have=true; for(int z=k; z>i+1; z--) { cur++; swap(a[z],a[z-1]); } break; } } } if(cur==1e9) continue; if(have||(i==7&&n>7)) { if(res==-1||res>cur) res=cur; } } cout<<res<<'\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin>>t; while(t--) solve(); }
//12252024832524 #include <cstdio> #include <cstring> #include <algorithm> #define TT template<typename T> using namespace std; typedef long long LL; const int MAXN = 205; int n; int dp[MAXN * MAXN],ID[MAXN * MAXN],pre[MAXN * MAXN]; int ans[MAXN],ling1,ling2; LL Read() { LL x = 0,f = 1;char c = getchar(); while(c > '9' || c < '0'){if(c == '-')f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x*10) + (c^48);c = getchar();} return x * f; } TT void Put1(T x) { if(x > 9) Put1(x/10); putchar(x%10^48); } TT void Put(T x,char c = -1) { if(x < 0) putchar('-'),x = -x; Put1(x); if(c >= 0) putchar(c); } TT T Max(T x,T y){return x > y ? x : y;} TT T Min(T x,T y){return x < y ? x : y;} TT T Abs(T x){return x < 0 ? -x : x;} int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); n = Read(); dp[0] = 1; for(int i = 1;i <= n;++ i) { int val = Read() % 200; if(!val) { if(!ling1) ling1 = i; else { printf("Yes\n1 %d\n1 %d",ling1,i); return 0; } continue; } for(int j = 200*200;j >= val;-- j) if(dp[j-val]) { if(dp[j]) { int tot = 0,now = j; while(now > 0) { ans[++tot] = ID[now]; now = pre[now]; } sort(ans+1,ans+tot+1); printf("Yes\n"); Put(tot); for(int k = 1;k <= tot;++ k) putchar(' '),Put(ans[k]); putchar('\n'); now = j-val; tot = 0; while(now > 0) { ans[++tot] = ID[now]; now = pre[now]; } ans[++tot] = i; sort(ans+1,ans+tot+1); Put(tot); for(int k = 1;k <= tot;++ k) putchar(' '),Put(ans[k]); putchar('\n'); return 0; } dp[j] = 1,ID[j] = i,pre[j] = j-val; } int ID1 = -1,ID2 = -1; for(int j = 1;j < 200 && ID2 < 0;++ j) { ID1 = -1; ID2 = -1; for(int k = j;k <= 200*200;k += 200) { if(dp[k]) { if(ID1 < 0) ID1 = k; else if(ID2 < 0) { ID2 = k; break; } } } if(ID1 >= 0 && ID2 >= 0) break; } if(ID1 >= 0 && ID2 >= 0) { printf("Yes\n"); int tot = 0; while(ID1 > 0) { ans[++tot] = ID[ID1]; ID1 = pre[ID1]; } Put(tot); sort(ans+1,ans+tot+1); for(int j = 1;j <= tot;++ j) putchar(' '),Put(ans[j]); putchar('\n'); tot = 0; while(ID2 > 0) { ans[++tot] = ID[ID2]; ID2 = pre[ID2]; } Put(tot); sort(ans+1,ans+tot+1); for(int j = 1;j <= tot;++ j) putchar(' '),Put(ans[j]); putchar('\n'); return 0; } } printf("No"); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t=0; char ch=getchar(); int f=1; while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f; } int n,tot; vector<int> g[1010]; void add(int x,int y) { x++,y++; g[x].push_back(y); } int mx,dis[1010]; queue<int> q; int main() { //freopen("1.txt","r",stdin); read(n); srand(time(0)); for (int i=0;i<n;i++) printf("%d %d\n",(i*2)%n+1,(i*2+1)%n+1); /*for (int i=1;i<=n;i++) { memset(dis,-1,sizeof(dis)); q.push(i); dis[i]=0; while (!q.empty()) { int u=q.front(); q.pop(); for (int j=0,v;j<g[u].size();j++) { v=g[u][j]; if (dis[v]==-1) dis[v]=dis[u]+1,q.push(v); } } for (int j=1;j<=n;j++) { if (dis[j]==-1) { printf("-1\n"); return 0; } mx=max(mx,dis[j]); } } printf("%d\n",mx);*/ return 0; } /* 0. Enough array size? Enough array size? Enough array size? Interger overflow? 1. Think TWICE, Code ONCE! Are there any counterexamples to your algo? 2. Be careful about the BOUNDARIES! N=1? P=1? Something about 0? 3. Do not make STUPID MISTAKES! Time complexity? Memory usage? Precision error? */
#include <bits/stdc++.h> #define int long long #define menhera_chan_is_mine ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pb push_back #define pi pair<int, int> #define BE(i) i.begin(),i.end() #define fi first #define se second #define INF 2147483646 #define mkp make_pair #define ist insert #define mod 100003 //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") //#pragma GCC optimize("O2") using namespace std; int min(int a, int b){return a < b ? a : b;} int max(int a, int b){return a > b ? a : b;} bool isprime(int k){bool is=1 ; for ( int i = 2 ; i*i <= k ; i++ ) if ( k % i == 0 ) is = 0 ; return k>1?is:0;} const double PI=acos(-1); int d,a,b,c; void sol(){ cin >>a>>b>>c>>d; cout <<b-c; } signed main(){ menhera_chan_is_mine int T=1; //cin >>T; while (T--) sol(); return 0; }
#include<bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #include<ext/pb_ds/assoc_container.hpp> #include<chrono> using namespace chrono; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define endl "\n" #define forn(i,x1,n) for(int i=x1; i<n; i++) #define forit(it, m) for(decltype(m)::iterator it = m.begin(); it!=m.end(); ++it) #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x1) __builtin_popcountll(x1) #define zrobits(x1) __builtin_ctzll(x1) #define mod 1000000007 #define inf 1e18 #define ps(x1,y) fixed<<setprecision(y)<<x1 #define mk(arr,n,type) type *arr=new type[n]; #define w(x1) int x1; cin>>x1; while(x1--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void zeus() { 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 } bool prime[100001]; vector<int> prime_no; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { prime_no.pb(p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } } int32_t main() { zeus(); SieveOfEratosthenes(100); int n; cin >> n; vector<int> ans; cout << 6 << " " << 10 << " " << 15 << " "; int cnt = 0; int i = 18; while (cnt < n - 3) { if (i % 6 == 0 || i % 10 == 0 || i % 15 == 0) { cout << i << " "; cnt++; } i++; } }
#include <cstdio> const int N = 2505; int n, ans[N] = {0, 6, 10, 15}, tot, flag[10005]; int main() { scanf("%d", &n); tot = 3; for(int i = 1; i <= 3; i++) { for(int j = ans[i]*2; j+ans[i] <= 10000; j += ans[i]) { if(flag[j]) continue; flag[j] = 1; ans[++tot] = j; if(tot == n) break; } if(tot == n) break; } for(int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
#include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; if(a==b) cout<<a<<endl; else cout<<3-a-b<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin >> a >> b; if (a > b) swap(a, b); if (a == b) cout << a << '\n'; else if (a == 0 and b == 1) cout << 2 << '\n'; else if (a == 0 and b == 2) cout << 1 << '\n'; else cout << 0 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("avx,avx2,fma") typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; #define MP make_pair #define PB push_back #define X first #define Y second #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) #define ALL(a) a.begin(), a.end() #define SZ(a) (int)((a).size()) #define FILL(a, value) memset(a, value, sizeof(a)) #define debug(a) cerr << #a << " = " << a << endl; template<typename T> void setmax(T& x, T y) {x = max(x, y);} template<typename T> void setmin(T& x, T y) {x = min(x, y);} template<typename T> void print(const T& a, ostream& out){ for(auto i: a) out << i << ' '; out << endl; } const double PI = acos(-1.0); const LL INF = 1e9 + 47; const LL LINF = INF * INF; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; vector<int> a(m), b(m), c(n); vector<vector<pair<int, int>>> g(n); FOR(i, 0, m){ cin >> a[i] >> b[i]; --a[i]; --b[i]; g[a[i]].PB({b[i], i}); g[b[i]].PB({a[i], i}); } FOR(i, 0, n) cin >> c[i]; auto finalize = [&](vector<int>& ans){ FOR(i, 0, m){ cout << (ans[i] ? "<-\n" : "->\n"); } exit(0); }; set<PII> S; FOR(i, 0, n) S.insert({c[i], i}); vector<int> ans(m, -1); vector<int> alive(n, 1); while(SZ(S)){ int v = S.begin()->Y; set<int> used; function<void(int)> dfs = [&](int u){ used.insert(u); for(auto to: g[u]){ if (c[to.X] == c[v]){ if (ans[to.Y] == -1){ ans[to.Y] = a[to.Y] == u ? 0 : 1;//vid u } if (!used.count(to.X)){ dfs(to.X); } } } }; dfs(v); //assert(SZ(used) == c[v]); for(auto u: used){ S.erase({c[u], u}); alive[u] = 0; } for(auto u: used){ for(auto to: g[u]){ if (alive[to.X]){ ans[to.Y] = a[to.Y] == u ? 1 : 0; } } } } finalize(ans); cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; }
#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 = 998244353; const ll INF = 1e16; int main() { ll n, k; cin >> n >> k; vvl dp(n+1, vl(2*n + 1)); // dp[i][j] : 要素数iで総和がjとなる場合の数 dp[0][0] = 1; exrep(i, 1, n) { for(ll j = n; j >= 1; j--) { if(i >= j) { dp[i][j] = (dp[i-1][j-1] + dp[i][2*j]) % mod; } } } out(dp[n][k]); re0; }
#include <iostream> #include <algorithm> using namespace std; int main() { int N; int Ai[100]; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; int p_best = -1, p_best_count = 0, p_count; cin >> N; for (int i = 0; i < N; i++) { cin >> Ai[i]; } for (int j = 0; j < sizeof(primes) / sizeof(int); j++) { p_count = 0; for (int i = 0; i < N; i++) { if (Ai[i] == 1) continue; if (Ai[i] % primes[j] == 0) { p_count++; do { Ai[i] /= primes[j]; } while (Ai[i] % primes[j] == 0); } } if (p_count > p_best_count) { p_best = primes[j]; p_best_count = p_count; } } sort(Ai, Ai + N); int curr_p = 1; for (int i = 0; i < N; i++) { if (Ai[i] == 1) continue; if (Ai[i] > curr_p) { if (p_count > p_best_count) { p_best = curr_p; p_best_count = p_count; } curr_p = Ai[i]; p_count = 1; } else { p_count++; } } if (p_count > p_best_count) { p_best = curr_p; p_best_count = p_count; } cout << p_best; return 0; }
#include<iostream> #include<cstdio> #include<ctime> #include<cmath> #include<cstdlib> #include<cstring> #include<string> #include<vector> #include<iomanip> #include<fstream> #include<stack> #include<queue> #include<set> #include<map> #include<algorithm> #include<sstream> using namespace std; #define sync std::ios::sync_with_stdio(false);std::cin.tie(0) //string::npos const double pi = 3.141592653589; const int maxn = 200002; const int maxm = 26; const int mod = 998244353; const int inf = 0x3f3f3f3f; typedef long long ll; ll ksm(int a, int k){ ll res = 1; while(k){ if(k&1) res = (ll)res*a%mod; a = (ll)a*a%mod; k >>= 1; } return res; } int nxt[maxn]; int vis[maxn]; int ans, p, ring, n; void solve(){ for(int i = 1; i <= n; ++i){ if(vis[i]) continue; p = i; ++ring; while(vis[p] == 0){ vis[p] = ring; p = nxt[p]; if(vis[p] == ring) ++ans; } } } int main(){ sync; cin >> n; ring = 1; for(int i = 1; i <= n; ++i){ cin >> nxt[i]; if(nxt[i] == i){ vis[i] = ring++; ans++; } } solve(); cout << ksm(2, ans)-1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve(int64_t b) { int64_t answer = 0; for (int64_t a = 1; a <= 1000000; a++) { stringstream ss; ss << a << a; istringstream iss(ss.str()); int64_t c; iss >> c; if (c <= b) { answer++; } } cout << answer << endl; } int main() { int64_t b; cin >> b; solve(b); return 0; }
#pragma region template #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; using vvl = vector<vl>; using vb = vector<bool>; using vld = vector<ld>; using vs = vector<string>; using pll = pair<ll, ll>; using vpll = vector<pll>; constexpr ll inf = 3001001000100100100LL; #define endl '\n' // clang-format off #define _overload(_1, _2, _3, name, ...) name #define rep(...) _overload(__VA_ARGS__, _rep, _rep2,)(__VA_ARGS__) #define repc(...) _overload(__VA_ARGS__, _repc, _repc2,)(__VA_ARGS__) #define repr(...) _overload(__VA_ARGS__, _repr, _repr2,)(__VA_ARGS__) #define _rep(i,k,n) for(ll i=(k) , i##_xxxx=(n); i < i##_xxxx; ++i) #define _repc(i,k,n) for(ll i=(k) , i##_xxxx=(n); i <=i##_xxxx; ++i) #define _repr(i,k,n) for(ll i=(n-1), i##_xxxx=(k); i >=i##_xxxx; --i) #define _rep2(i,n) _rep(i,0,n) #define _repc2(i,n) _repc(i,1,n) #define _repr2(i,n) _repr(i,0,n) // rep [k,n) repc[k,n] repr[k,n) #define all(o) begin(o), end(o) template <typename C> ll sz(const C &c) { return static_cast<ll>(c.size()); } template <class T> T udiv(T a, T b) {return (a + b - 1) / b;} template <class T> T rdiv(T a, T b) {return (a + b / 2) / b;} template <typename T, typename S> string join(const T &seq, S sep) { stringstream ss; bool f = false; for (auto c : seq) { ss << (f ? sep : "") << c; f = true;} return ss.str(); } template <class T, class S, class... U> string join(const T &v, const S &sep, const U &...args) { stringstream ss; bool f = false; for (auto &&c : v) { if (f) ss << sep; f = true; ss << join(c, args...); } return ss.str(); } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v){ os << '[' << join(v, ",") << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &v){ os << '[' << join(v, ",") << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v){ os << '{' << join(v, ",") << '}'; return os; } template <typename T , typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &v){ os << '{' << join(v, ",") << '}'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << '{'; for (auto v : mp) os << v.first << ":" << v.second << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << '(' << pa.first << ',' << pa.second << ')'; return os; } #define debug(...) _debug(__LINE__, __VA_ARGS__) void print_args(ostream& out) { out << '\n'; } void print() { std::cout << '\n'; } void _debug() { std::cerr << '\n'; } template <class S, class... T> void print_args(ostream& out,const S& a,const T&... args) { out << ' ' << a; print_args(out, args...); } template <class S, class... T> void print(const S& a, const T&... args) { std::cout << a; print_args(std::cout, args...); } template <class S, class... T> void _debug(const S& a, const T&... args) { std::cerr << "(L:" << std::setw(3) << a << ")"; print_args(std::cerr, args...); } // clang-format on struct setup_main { setup_main() { std::cin.tie(nullptr); // std::ios::sync_with_stdio(false); // atcoder-toolsとの共存不可 std::cout << fixed << setprecision(15); } } setup_main_; #pragma endregion void solve(ll N, vector<ll> A) { ll ans = 0; vl v(401, 0); rep(i, N) v[A[i] + 200]++; rep(i1, 401) rep(i2, 401) { ans += v[i1] * v[i2] * (i1 - i2) * (i1 - i2); } print(ans / 2); } signed main() { ll N; cin >> N; vector<ll> A(N); rep(i, N) { cin >> A[i]; } solve(N, move(A)); }
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=a;i<=b;i++) using LL = long long; const int Nmax=2e5+9; int N,A[39],S,ans=INT_MAX,tmp1,tmp2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>N; REP(i,1,N) cin>>A[i]; S=(1<<(N-1))-1; REP(k,0,S) { tmp1=tmp2=0; REP(i,1,N) { if (i>=2 && ((k>>(i-2))&1)) { tmp2^=tmp1; tmp1=0; } tmp1|=A[i]; } tmp2^=tmp1; ans=min(ans,tmp2); // cout<<k<<" "<<tmp2<<" "<<ans<<endl; } cout<<ans; return 0; }
//#pragma GCC optimize ("O3", "unroll-loops") //#pragma GCC target ("avx2") //#pragma comment(linker, "/stack:200000000") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #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 = 300005; //const LL INF = (LL) 1e9 + 8763; //const LL MOD = (LL) 998244353; using namespace std; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int P = 3; int fact[10]; int inv(int x) { return x; } int mod_fact(int n,int &e){ e=0; if(n==0)return 1; int res=mod_fact(n/P,e); e += n/P; if((n/P)%2==0)return res*fact[n%P]%P; return res*(P-fact[n%P])%P; } int Cmod(int n,int m){ int a1,a2,a3,e1,e2,e3; a1=mod_fact(n,e1); a2=mod_fact(m,e2); a3=mod_fact(n-m,e3); if(e1>e2+e3)return 0; return a1*inv(a2*a3%P)%P; } int n; string s; int c2i[256]; char i2c[3] = {'B', 'W', 'R'}; void solve() { fact[0] = 1; for (int i = 1; i < 10; i++) { fact[i] = fact[i - 1] * i % P; } // for (int i = 0; i < 10; i ++) { // for (int j = 0; j <= i; j++) { // cout << Cmod(i, j) << ' '; // } // cout << '\n'; // } cin >> n; c2i['B'] = 0; c2i['W'] = 1; c2i['R'] = 2; int lay = n, ans = 0, sign = (lay % 2 ? 1 : -1); for (int i = 0; i < n; i++) { char ch; cin >> ch; int x = c2i[ch]; ans = (ans + x * Cmod(n - 1, i) * sign) % 3; } ans = (ans % 3 + 3) % 3; cout << i2c[ans] << '\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; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int A[801][801]; int B[801][801]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; rep1(i, N) rep1(j, N) cin >> A[i][j]; int C = (K * K + 1) / 2; int L = -1, R = 1000000001; while (L + 1 < R) { int wj = (L + R) / 2; rep1(i, N) rep1(j, N) B[i][j] = (A[i][j] <= wj); rep1(i, N) rep1(j, N) B[i][j] += B[i][j - 1]; rep1(j, N) rep1(i, N) B[i][j] += B[i - 1][j]; int k = 0; rep(i, N - K + 1) rep(j, N - K + 1) { int tmp = B[i][j] - B[i][j + K] - B[i + K][j] + B[i + K][j + K]; chmax(k, tmp); } if (k < C) L = wj; else R = wj; } co(R); Would you please return 0; }
#include<bits/stdc++.h> using namespace std ; #define vt vector #define all(c) (c).begin(), (c).end() #define sz(x) (int)(x).size() #define ll long long #define ld long double #define ar array #define inf() ifstream cin("d.txt") #define onf() ofstream cout("test.txt") #define mod 1000000007 #define pb push_back #define endl cout<<"\n"; #define prt1(a) cout<<a<<" " #define prt2(a,b) cout<<a<<" "<<b<<" " #define prt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" " #define in(a) cin>>a #define inn(a,b) cin>>a>>b #define innn(a,b,c) cin>>a>>b>>c #define rep(i,st,end) for(ll i=st;i<end;i++) #define repv(i,st,end) for(ll i=st;i>=end;i--) #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define TEST ll tc;cin>>tc;while(tc--) #define EACH(x, a) for (auto& x: a) template<class A> void read(vt<A>& v); template<class A, size_t S> void read(ar<A, S>& a); template<class T> void read(T& x) { cin >> x; } void read(double& d) { string t; read(t); d=stod(t); } void read(long double& d) { string t; read(t); d=stold(t); } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template<class A> void read(vt<A>& x) { EACH(a, x) read(a); } template<class A, size_t S> void read(array<A, S>& x) { EACH(a, x) read(a); } #define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a <<"\n"; err(++it, args...); } const int N=820; int k,n; int g[N][N]; int dp[N][N]; bool check(int mid) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { dp[i][j]=g[i][j]>=mid; dp[i][j]+=dp[i-1][j]; dp[i][j]+=dp[i][j-1]; dp[i][j]-=dp[i-1][j-1]; } } ll mn = 1e9; for(int i=k;i<=n;i++) { for(int j=k;j<=n;j++) { ll cnt=dp[i][j]; cnt-=dp[i][j-k]; cnt-=dp[i-k][j]; cnt+=dp[i-k][j-k]; mn = min(cnt, mn); } } if(mn>=k*k/2+1) return true; return false; } void solve(){ cin>>n>>k; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>g[i][j]; int l=0,r=1e9, ans = 0; while(l <= r) { int mid=(l+r)>>1; if(check(mid)) { l= mid+1; ans = mid; } else r=mid-1; } cout<<ans; } int main() { clock_t start, end; start = clock(); fio; ll k = 1; ll tc = 1; // cin>>tc; while(tc--){ // cout<<"Case #"<<k<<": ";k++; solve(); endl; } end = clock(); double time_taken = double(end - start) / double(CLOCKS_PER_SEC); // debug(time_taken); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxl=2e5+10; const int mod=1e9+7; int n,k; int a[maxl]; ll dp[maxl][17][2][2]; char s[maxl]; inline ll dfs(int pos,int st,bool up,bool lead) { int cnt=__builtin_popcount(st); if(cnt>k) return 0; if(pos>n) return cnt==k && lead; ll &ret=dp[pos][cnt][up][lead]; if(ret!=-1) return ret; ret=0; int r=up?a[pos]:15; for(int i=0;i<=r;i++) ret=(ret+dfs(pos+1,(!i&&!lead)?st:st|(1<<i),up&&(i==r),lead|i))%mod; return ret; } int main() { scanf("%s",s+1);scanf("%d",&k); n=strlen(s+1); for(int i=1;i<=n;i++) if(s[i]>='A' && s[i]<='Z') a[i]=s[i]-'A'+10; else a[i]=s[i]-'0'; memset(dp,-1,sizeof(dp)); printf("%lld\n",dfs(1,0,1,0)); return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> #include<algorithm> 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 dp[18]; char S[200200]; int suuji(char c) { if (c < 'A') return c - '0'; else return c - ('A' - 10); } int main() { //cin.tie(0); //ios::sync_with_stdio(false); fread(S, 1, 200200, stdin); int K = 0; int N = 200200 - 1; while (S[N] != ' ') N--; int tmp = N + 1; while (S[tmp] >= '0') K = K * 10 + S[tmp++] - '0'; const int mod = 1e9 + 7; int k = suuji(S[0]); dp[1] = k - 1; int kazu = 1 << 15 - k; rep1(i, N - 1) { if (!(i & 7)) for (int j = 2; j <= 16; j++) dp[j] %= mod; for (int j = 16; j >= 1; j--) { dp[j + 1] += (16 - j) * dp[j]; dp[j] = j * dp[j]; } dp[1] += 15; int k = suuji(S[i]); int k1 = __builtin_popcount(kazu >> 16 - k); int kazu0 = __builtin_popcount(kazu); dp[kazu0] += k1; dp[kazu0 + 1] += k - k1; kazu |= 1 << 15 - k; } ll kotae = dp[K]; kotae += (__builtin_popcount(kazu) == K); printf("%lld", kotae % mod); Would you please return 0; }
#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)-1;i>=(a);--i) using namespace std; const int N=1e3+9; const int Inf=1e9; char s[N],t[20]={"atcoder"}; int main(){ int T; scanf("%d",&T); rep(Tcase,1,T+1){ scanf("%s",s); int n=strlen(s); int ans=Inf,sum=0; rep(i,0,min(n,8)){ //cout<<i<<' '<<ans<<"check\n"; if(i==7){ ans=min(ans,sum); break; } rep(j,i,n){ if(s[j]>t[i]){ ans=min(ans,sum+j-i); break; } } int ok=0; rep(j,i,n){ if(s[j]==t[i]){ ok=1; while(j>i)swap(s[j],s[j-1]),sum++,--j; break; } } if(sum>ans||!ok)break; } if(ans==Inf)ans=-1; printf("%d\n",ans); } }
#include<bits/stdc++.h> using namespace std; using d = int; using ll = long long; using ud = unsigned int; using ull = unsigned long long; using lf = double; using llf = long double; using ch = char; using st = string; using bl = bool; using vo = void; #define ci cin #define co cout #define fi( x, n ) for( d i = x; i < n; i++ ) #define fj( x, n ) for( d j = x; j < n; j++ ) #define fin( x, n ) for( d i = x; i <= n; i++ ) #define fjn( x, n ) for( d j = x; j <= n; j++ ) #define wh while #define w( n ) while( n-- ) #define vd vector<d> #define vll vector<ll> #define vlf vector<lf> #define vc vector<ch> #define vs vector<st> #define sd set<d> #define sll set<ll> #define slf set<lf> #define sc set<ch> #define ss set<st> #define sz(x) x.size() #define st(x) sort( x.begin(), x.end() ) #define cou(x, y) count( x.begin(), x.end(), y ) #define in insert #define ct count #define fd find #define pb push_back #define er erase #define be begin #define en end #define np next_permutation #define el endl vo InputOutput() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } #define SIZE 100000 bl check[SIZE]; vd prime; void sieve( d number ) // O( n * log(log(n)) ) { d i, j; check[0] = check[1] = 1; for( i = 2; i*i <= number; i++ ) { if( check[i] == 0 ) { for( j = i*i; j <= number; j += i ) check[j] = 1; } } for( i = 2; i <= number; i++ ) { if( check[i] == 0 ) prime.pb( i ); } } d IsPrime( d num ) // O( n / 2 ) { if( num <= 2 ) return 0; if( num % 2 == 0 ) return 0; else { for( d i = 3; i*i <= num; i += 2 ) { if( num % i == 0 ) return 0; } return 1; } } d main() { InputOutput(); d x; ci >> x; d ans = ( (x / 100) + 1 ) * 100; ans -= x; co << ans << el; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; // cin>>t; while(t--){ long long n; cin>>n; long long s=0,ans=0; for(long long i=1;i*i<=2*n;i++){ s+=i-1; long long x=s%i; if(n%i==x) ans++; } cout<<ans*2; cout<<endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using vl = vector<ll>; using vvl = vector<vl>; using pl = pair<ll, ll>; const ll INF = ll(1e18); const ll mod = ll(998244353); const double pi = acos(-1); #define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i)) #define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i)) #define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i)) #define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i)) #define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i)) #define rnfor(i,a,b) for(ll (i) = (b) - 1; (i) >= (a); --(i)) #define pf(x) cout << (x) << endl #define all(x) (x).begin(),(x).end() #define yes pf("Yes") #define no pf("No") 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; } const int dx[8]={1,0,-1,0,1,1,-1,-1}; const int dy[8]={0,1,0,-1,1,-1,1,-1}; int multipf(vector<string>& s){ if(s.size() == 0)return 0; cout << s[0]; rep1(i, s.size() - 1)cout << " " << s[i]; cout << endl; return 0; } int multipf(vector<ll>& n){ if(n.size() == 0)return 0; cout << n[0]; rep1(i, n.size() - 1)cout << " " << n[i]; cout << endl; return 0; } ll gcd(ll a,ll b){ if(a < b)swap(a, b); if(b == 0) return a; return gcd(b,a%b); } ll lcm(ll a,ll b){ ll g = gcd(a,b); return a / g * b; } ll factorial(ll n){ ll ans = 1; rep1(i, n){ ans *= i; ans %= mod; } return ans; } ll power(ll a, ll b, ll m){ ll ans = 1; while(b) { if(b & 1LL) ans = ans * a % m; ans %= m; a = a * a; a %= m; b >>= 1; } return ans % m; } struct UnionFind { vector<ll> par; vector<ll> rank; vector<ll> Size; UnionFind(ll n) { init(n); } void init(ll n) { par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1); rep0(i, n + 1){ par[i] = i; rank[i] = 0; Size[i] = 1; } } ll root(ll x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } bool same(ll x, ll y) { return root(x) == root(y); } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; Size[x] += Size[y]; return true; } ll size(ll x){ return Size[root(x)]; } }; int main() { ll n,f; cin >> n; UnionFind u(n); rep0(i, n){ cin >> f; --f; u.merge(i, f); } vector<bool> ff(n,true); ll cnt = 0; rep0(i, n){ ll k = u.root(i); if(ff[k]){ ff[k] = false; ++cnt; } } pf((power(2, cnt, mod) - 1 + mod) % mod); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; string s[3]; map<char,int>bj; map<char,int>num; int a[12]={0}; char c[12]={0}; int n=0,ok=0; ll x[3]; void solve(int k){ //cout<<k<<endl; if(k==n){ for(int i=0;i<3;i++){ x[i]=0; for(int j=0;j<s[i].size();j++){ x[i]=x[i]*10+num[s[i][j]]; } } //cout<<x[0]<<endl<<x[1]<<endl<<x[2]<<endl; if(x[0]+x[1]==x[2]&&x[0]&&x[1]&&x[2]&&num[s[0][0]]&&num[s[1][0]]&&num[s[2][0]])ok=1; return ; } int i=0; for(;i<10&&!ok;i++){ if(!a[i]){ a[i]=1; num[c[k]]=i; solve(k+1); a[i]=0; } } } int main() { for(int i=0;i<3;i++){ cin>>s[i]; for(int j=0;j<s[i].size();j++){ if(!bj[s[i][j]]){ c[n++]=s[i][j]; bj[s[i][j]]=1; } } } //cout<<n<<endl; solve(0); if(ok)cout<<x[0]<<endl<<x[1]<<endl<<x[2]<<endl; else cout<<"UNSOLVABLE"<<endl; // for(int i=0;i<n;i++){ // cout<<c[i]<<" "<<num[c[i]]<<endl; // } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<vi> vvi; typedef vector<pll> vll; typedef vector<vl> vvl; #define fori(i, n) for (int i = 0; i < n; i++) #define ford(i, n) for (int i = n - 1; i >= 0; i--) #define rep(i, a, b) for (int i = a; i <= b; i++) #define repd(i, a, b) for (int i = a; i >= b; i--) #define trav(x, a) for (auto &x : a) #define all(x) (x).begin(), (x).end() #define pb push_back #define eb emplace_back #define endl '\n' #define sz(a) (int)(a).size() #define fi first #define se second clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } const ll mod = 1e9 + 7; const ll INF = 1e18; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); string s; cin >> s; int n = sz(s); bool ok = 1; fori(i, n){ ok &= ((s[i] >= 'a' and s[i] <= 'z' and i % 2 == 0) or (s[i] >= 'A' and s[i] <= 'Z' and i%2==1)); } cout << (ok ? "Yes" : "No") << endl; time_taken(); return 0; }
//in dp prefix suffix sum helps.. #include<iostream> #include<vector> #include<string.h> #include<algorithm> #include<iomanip> #include<cmath> #include<stack> #include <chrono> #include <random> #include <iterator> #include <map> #include<list> #include <algorithm> #include <fstream> #include<unordered_map> #include<set> #include<queue> #define int long long #define double long double #define pb push_back #define mp make_pair #define pii pair<int,int> #define pip pair<int,pii> #define vip vector<pii> #define vi vector<int> #define vs vector<string> #define vd vector<double> #define mii map<pii, int> #define qu queue<pii> #define set set<int> #define multiset multiset<int> #define rep(i, a, b) for(int i=a;i<b;i++) #define rep1(i, a, b) for(int i=b;i>=a;i--) #define viv vector<pip> #define stack stack<int> #define fi first #define se second #define all(v) (v).begin(), (v).end() #define it iterator #define pq priority_queue<int> #define pq1 priority_queue <pii, vector<pii>, greater<pii> > #define mem(x, y) memset(x, (int)y, sizeof(x)) #define ps(x,y) fixed<<setprecision(y)<<x // author :: Anurag Anand. using namespace std; int z= 1e9+7; //int z= 998244353; int gcd(int a, int b){ if(a==0)return b; if(b==0)return a; return gcd(b, a%b); } int power(int a,int b) { int res=1; while(b) { if(b&1) { res=(res*a)%z; b--; } a=(a*a)%z; b=b>>1; } return res; } vi sv; int fact[200001]; int ncr(int n, int r){ int p=fact[n]; p*=power(fact[r], z-2); p%=z; p*=power(fact[n-r], z-2); p%=z; return p; } // vi seg; // void arrange(int i, int v, int x, int lx, int rx){ // if(rx-lx==1){ // seg[x]=v; // return; // } // int mid=(lx+rx)/2; // if(i<mid) arrange(i, v, 2*x+1, lx, mid); // else arrange(i, v, 2*x+2, mid, rx); // seg[x]= min(seg[2*x+1], seg[2*x+2]); // } // int calc(int l, int r, int x, int lx, int rx){ // if(lx>=r||l>=rx) return 1e9; // if(lx>=l&&rx<=r) return seg[x]; // int mid=(lx+rx)/2; // int k1=calc(l, r, 2*x+1, lx, mid), k2=calc(l, r, 2*x+2, mid, rx); // return min(k1, k2); // } vi pp; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pp.assign(1000001, 0); for(int i=2;i<1000;i++){ if(!pp[i]){ for(int j=i*i;j<=1000000;j+=i){ pp[j]=1; } } } fact[0]=1; rep(i,1,200001) fact[i]=i*fact[i-1], fact[i]%=z; for(int i=2;i<=1000000;i++) if(!pp[i]) sv.pb(i); int t; t=1; //cin>>t; while(t--){ int n; cin>>n; multiset sr, sb, sg; rep(i,0,2*n){ int x; char c; cin>>x>>c; if(c=='R') sr.insert(x); else if(c=='G') sg.insert(x); else sb.insert(x); } if(sr.size()%2==0&&sb.size()%2==0&&sg.size()%2==0) cout<<"0\n"; else{ multiset s1, s2, s3; if(sr.size()&1&&sb.size()&1){ s1=sr; s2=sb; s3=sg; } else if(sr.size()&1&&sg.size()&1){ s1=sr; s2=sg; s3=sb; } else{ s1=sg; s2=sb; s3=sr; } int a=1e15, b1=1e15, b2=1e15; multiset :: it it; for(it=s1.begin();it!=s1.end();it++){ multiset :: it it1= s2.upper_bound(*it); int k; if(it1==s2.end()){ k=*s2.rbegin(); a=min(a, abs(k-*it)); } else{ k=*it1; a=min(a, abs(k-*it)); if(it1!=s2.begin()){ it1--; k=*it1; a=min(a, abs(k-*it)); } } if(s3.size()){ it1=s3.upper_bound(*it); if(it1==s3.end()){ k=*s3.rbegin(); b1=min(b1, abs(k-*it)); } else{ k=*it1; b1= min(b1, abs(k-*it)); if(it1!=s3.begin()){ it1--; k=*it1; b1=min(b1, abs(k-*it)); } } } } if(s3.size()){ for(it=s2.begin();it!=s2.end();it++){ multiset :: it it1= s3.upper_bound(*it); int k; if(it1==s3.end()){ k=*s3.rbegin(); b2=min(b2, abs(k-*it)); } else{ k=*it1; b2= min(b2, abs(k-*it)); if(it1!=s3.begin()){ it1--; k=*it1; b2=min(b2, abs(k-*it)); } } } } cout<<min(a, b1+b2)<<"\n"; } } }
// ********************************************************************************* // * MURTAZA MUSTAFA KHUMUSI * // * CSE - 2019-2023 * // ********************************************************************************* #include <bits/stdc++.h> using namespace std; typedef long long ll; #define loop(i, a, n) for (int i = a; i < n; i++) #define loope(i, b, n) for (int i = b; i <= n; i++) #define loopit(a) for (auto it = a.begin(); it != a.end(); it++) #define ms(a, b) memset(a, b, sizeof(a)) #define pb(a) push_back(a) #define MP make_pair #define pi pair<int, int> #define ff first #define ss second #define bloop(i, a, b) for (int i = a; i > b; i--) #define bloope(i, a, b) for (int i = a; i >= b; i--) #define PQ priority_queue<int> pq; #define vi vector<int> #define vii vector<vector<int>> #define si set<int> #define NO cout<<"NO\n"; #define YES cout<<"YES\n"; #define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq; #define io \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ void solve() { int n; cin >> n; vector<ll>in[3]; auto ret = [](char ch) { if (ch == 'R')return 0; else if (ch == 'G')return 1; else return 2; }; loop(i, 0, n << 1) { ll num; cin >> num; char c; cin >> c; in[ret(c)].pb(num); } ll mx = 1e16; vi odd; loop(i, 0, 3) { if (in[i].size() & 1)odd.pb(i); } // dis(odd); ll ans = 0; if (odd.size() == 0) { cout << ans << "\n"; return; } ans = mx; int od = odd[0]; loop(i, 0, 3)sort(in[i].begin(), in[i].end()); // at least tow odds guranteed int rem = 3 - (odd[0] + odd[1]); auto dif = [&](vector<ll> & vec, ll & v) { ll fin = mx; int val = lower_bound(vec.begin(), vec.end(), v) - vec.begin(); if (val != vec.size())fin = min(fin, llabs(vec[val] - v)); val--; if (val >= 0)fin = min(fin, llabs(vec[val] - v)); return fin; }; // direct difference between odd elems for (auto &v : in[odd[1]]) { ll ret = dif(in[odd[0]], v); // cout << odd[0] << " " << odd[1] << " " << ret << "\n"; ans = min(ans, dif(in[odd[0]], v)); } // use two intermediates int siz = in[rem].size(); // cout << in[0].size() << " " << in[1].size() << " " << in[2].size() << " " << in[rem].size() << " " << ans << "\n"; if (siz == 0) { cout << ans << "\n"; return; } // at least two elements vector<ll>pref(siz), suf(siz), val(siz); ll fn = mx; // Itermediate difference loop(i, 0, siz) { val[i] = dif(in[odd[0]], in[rem][i]); } pref[0] = val[0]; suf[siz - 1] = val[siz - 1]; loop(i, 1, siz) { pref[i] = min(pref[i - 1], val[i]); suf[siz - i - 1] = min(suf[siz - i], val[siz - i - 1]); } loop(i, 0, siz) { ll prev = (i ? pref[i - 1] : mx), nex = (i + 1 < siz ? suf[i + 1] : mx); ll mn = min(prev, nex); ans = min(ans, mn + dif(in[odd[1]], in[rem][i])); } cout << ans << "\n"; } int main() { io; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int test = 1, i = 0; // cin >> test; while (i++ != test) { // cout<<"Case #"<<i<<": "; solve(); } return 0; }
#include<bits/stdc++.h> template <typename _Tp>void read(_Tp &x){ char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar(); x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar(); if(f)x=-x; } template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);} int read_str(char *s){ char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar(); char *tar=s;*tar++=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *tar++=ch,ch=getchar(); return *tar=0,tar-s; } const int N=500005; char s[N],t[N]; int s1[N],s2[N],a[N],b[N],p1,p2; int main(){ int n;read(n); read_str(s+1),read_str(t+1); for(int i=1;i<=n;++i)s1[i]=s1[i-1]+(s[i]=='1'),s2[i]=s2[i-1]+(t[i]=='1'); if(s1[n]!=s2[n])return puts("-1"),0; int ans=0; for(int i=1;i<=n;++i)if(s[i]=='0')a[++p1]=i; for(int i=1;i<=n;++i)if(t[i]=='0')b[++p2]=i; for(int i=1;i<=p1;++i)ans+=s1[a[i]]!=s2[b[i]]; printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; // clang-format off #define range(i, l, r) for ( int i = (int)(l); i < (int)(r); (i) += 1 ) #define rrange(i, l, r) for ( int i = (int)(r)-1; i >= (int)(l); (i) -= 1 ) #define debug(x) cerr << "(" << __LINE__ << ") " << #x << ": " << (x) << endl; template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << ' ' << p.second; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { for ( pair<T1, T2> x : v ) { os << x << (x.first == v.rbegin()->first && x.second == v.rbegin()->second ? "" : "\n"); } return os; } template <typename T> ostream &operator<<(ostream &os, queue<T> v) { if(!v.empty()) { os << v.front(); v.pop(); } while (!v.empty()) { os << " " << v.front(); v.pop(); } return os; } template <typename T> ostream &operator<<(ostream &os, stack<T> v) { if(!v.empty()) { os << v.top(); v.pop(); } while (!v.empty()) { os << " " << v.top(); v.pop(); } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; } using ull = unsigned long long; using ll = long long; using Pll = pair<ll, ll>; using P = pair<int, int>; constexpr ll INF64 = INT64_MAX / 2; constexpr int INF32 = INT32_MAX / 2; constexpr int dy[] = { 0, -1, 1, 0, -1, 1, -1, 1 }; constexpr int dx[] = { -1, 0, 0, 1, -1, -1, 1, 1 }; constexpr int mod998244353 = 998244353; constexpr int mod1000000007 = (int)1e9 + 7; constexpr char newl = '\n'; // clang-format on bool is_valid(string s,string t){ int cnt[2]={0}; for(auto c:s) if(c=='0') cnt[0]++; for(auto c:t) if(c=='0') cnt[1]++; return cnt[0]==cnt[1]; } int main() { int n; cin >> n; string s,t; cin>>s>>t; if(!is_valid(s,t)){ cout<<-1<<endl; return 0; } vector<int> zero1,zero2; range(i,0,n) if(s[i]=='0') zero1.push_back(i); range(i,0,n) if(t[i]=='0') zero2.push_back(i); int ans=0; range(i,0,zero1.size()){ ans+=(zero1[i]!=zero2[i]); } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; const ll mod = 998244353; vector<int> par(10010, 0); vector<int> Rank(10010, 0); void init(int n) { for(int i = 0; i < n; i++) { par.at(i) = i; Rank.at(i) = 0; } } int find(int x) { if(par.at(x) == x) { return x; } else { return par.at(x) = find(par.at(x)); } } void unite(int x, int y) { x = find(x); y = find(y); if(x == y) { return; } if(Rank.at(x) < Rank.at(y)) { par.at(x) = y; } else { par.at(y) = x; if(Rank.at(x) == Rank.at(y)) { Rank.at(x)++; } } } bool same(int x, int y) { return find(x) == find (y); } int main() { ll N, K; cin >> N >> K; vector<vector<ll> > m(N,vector<ll>(N,0)); vector<vector<ll> > M(N,vector<ll>(N,0)); for(ll i = 0; i < N; i++) { for(ll j = 0; j < N; j++) { cin >> m.at(i).at(j); M.at(j).at(i) = m.at(i).at(j); } } init(N); for(ll i = 0; i < N; i++) { for(ll j = i + 1; j < N; j++) { for(ll k = 0; k < N; k++) { if(m.at(i).at(k) + m.at(j).at(k) > K) break; if(k == N - 1) unite(i, j); } } } vector<int> A(N, 0); for(int i = 0; i < N; i++) A.at(find(i))++; init(N); for(ll i = 0; i < N; i++) { for(ll j = i + 1; j < N; j++) { for(ll k = 0; k < N; k++) { if(M.at(i).at(k) + M.at(j).at(k) > K) break; if(k == N - 1) unite(i, j); } } } vector<int> B(N, 0); for(int i = 0; i < N; i++) B.at(find(i))++; ll ans = 1; for(int j = 0; j < N; j++) { for(ll i = 1; i <= A.at(j); i++) ans = (ans*i)%mod; for(ll i = 1; i <= B.at(j); i++) ans = (ans*i)%mod; } cout << ans << endl; return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please //問題に強く祈りをささげることで、気が狂った書き方が確率でふってくる。 int to[400002], ne[400010], he[200001]; int Q[200010]; const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 72 - dig >> 3; return tmp; } int main() { //cin.tie(0); //ios::sync_with_stdio(false); int N = getint(), K = getint(); int k = 2; rep(i, N - 1) { int u = getint(), v = getint(); to[k] = v; ne[k] = he[u]; he[u] = k++; to[k] = u; ne[k] = he[v]; he[v] = k++; } int q = 0, p = 0; Q[q++] = 2; while (q < N) { int u = to[Q[p++]]; for(int ind = he[u]; ind; ind = ne[ind]) { int v = to[ind]; if (he[v] > 0) Q[q++] = ind; } he[u] = 0; } auto pl = ne, dl = ne + 200000; int are = 1000000, pd0 = are; int L = 0, R = (N + K - 1) / K; while (L + 1 < R) { int wj = (L + R) / 2; int num = 0; for (int i = N - 1; i >= 1; i--) { int u = to[Q[i] ^ 1]; int v = to[Q[i]]; int po = pl[v]; int dd = dl[v]; if (dd < pd0) dd = pd0; if (po >= dd) dd = 0; if (dd >= pd0 + wj) { num++; po = pd0 + wj; dd = 0; } if (pl[u] < po - 1) pl[u] = po - 1; if (dl[u] < dd + 1) dl[u] = dd + 1; } int v = to[Q[1] ^ 1]; int po = pl[v]; int dd = dl[v]; if (dd < pd0) dd = pd0; if (po >= dd) dd = 0; if (dd >= pd0) num++; if (num > K) L = wj; else R = wj; pd0 += are; } printf("%d", R); Would you please return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <iomanip> #include <unordered_map> #include <unordered_set> #include <algorithm> #include <map> #include <set> #include <queue> #include <cmath> using namespace std; using VI = vector <int>; using VLL = vector <long long>; using VVLL = vector <VLL>; using MLL = map <long long, long long>; const long long INF = 1000000000000000LL; void solve(int now, MLL& wmap, VI& used, VI& order, VLL& w, long long& ans) { if (now == w.size()) { int N = w.size(); VLL wsum(N); wsum[0] = w[order[0]]; for (int i = 1; i < N; ++i) { wsum[i] = wsum[i-1] + w[order[i]]; } VLL dp(N); for (int i = 0; i < N; ++i) { for (int j = i+1; j < N; ++j) { long long segWeight = i>0 ? wsum[j] - wsum[i-1] : wsum[j]; auto iter = wmap.lower_bound(segWeight); --iter; long long leng = iter->second; // dist[i] - dist[j] >= leng // -leng >= dist[j] - dist[i] dp[j] = max(dp[j], dp[i]+leng); } } ans = min(ans, dp[N-1]); return ; } for (int i = 0; i < used.size(); ++i) { if (used[i] == 0) { used[i] = 1; order[now] = i; solve(now+1, wmap, used, order, w, ans); used[i] = 0; } } } int main() { int N; int M; long long ans = INF; cin >> N >> M; VLL w(N); long long maxw = 0; for (int i = 0; i < N; ++i) { cin >> w[i]; maxw = max(maxw, w[i]); } VVLL lv; long long minv = INF; for (int i = 0; i < M; ++i) { long long l; long long v; cin >> l >> v; minv = min(minv, v); lv.push_back({v, l}); } MLL wmap; sort(lv.begin(), lv.end()); wmap[0] = 0; wmap[INF] = INF; // (v, l), monotone increasing, v+ => l+ for (int i = 0; i < lv.size(); ++i) { if (i+1 == lv.size() || lv[i][0] != lv[i+1][0]) { wmap[lv[i][0]] = lv[i][1]; } } auto iter = wmap.begin(); auto preIter = iter; while (iter != wmap.end()) { iter->second = max(iter->second, preIter->second); preIter = iter; ++iter; } VI used(N); VI order(N); if (minv >= maxw) { solve(0, wmap, used, order, w, ans); } cout << (ans == INF ? -1 : ans) << endl; return 0; }
// Problem: F - Range Xor Query // Contest: AtCoder - AtCoder Beginner Contest 185 // URL: https://atcoder.jp/contests/abc185/tasks/abc185_f // Memory Limit: 1024 MB // Time Limit: 3000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; typedef long long ll; const int mxn = 3e5 + 2; int segt[mxn * 4]; int arr[mxn]; void build(int v, int tl, int tr) { if (tl == tr) segt[v] = arr[tl]; else { int tm = (tl + tr) >> 1; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); segt[v] = segt[v * 2] ^ segt[v * 2 + 1]; } } void update(int v, int tl, int tr, int ind, int val) { if (tl > tr || tr < ind || tl > ind) return; else if (tl == tr && tl == ind) { segt[v] ^= val; } else { int tm = (tl + tr) >> 1; update(v * 2, tl, tm, ind, val); update(v * 2 + 1, tm + 1, tr, ind, val); segt[v] = segt[v * 2] ^ segt[v * 2 + 1]; } } int xos(int v, int tl, int tr, int l, int r) { if (tl > tr || tr < l || tl > r) return 0; else if (l <= tl && tr <= r) { return segt[v]; } else { int tm = (tl + tr) >> 1; return xos(v * 2, tl, tm, l, r) ^ xos(v * 2 + 1, tm + 1, tr, l, r); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> arr[i]; } build(1, 0, n - 1); for (int i = 0; i < q; i++) { int t, x, y; cin >> t >> x >> y; if (t == 1) update(1, 0, n - 1, x - 1, y); else cout << xos(1, 0, n - 1, x - 1, y - 1) << "\n"; } }
#include<bits/stdc++.h> typedef int64_t ll; using namespace std; const int INF = 1000000007; int fact(int n); int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } int fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("out.txt","w",stdout); #endif string s; cin>>s; ll count1=0,count2=0; for(ll i=0;i<s.size();i++){ if(s[i]=='o'){ count1++; } else if(s[i]=='?'){ count2++; } } if(count1>4){ cout<<0<<endl; return 0; } ll n = count1,m=count2,ans=0; for(ll r=0;r<=n;r++){ ans = ans + nCr(n,r) * pow(-1,r) * pow((n+m-r),4); } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define int long long signed main(){ int x=0; int o = 0; int q =0; int ans=0; rep(i, 10){ char s; cin >> s; if (s=='o') o++; else if (s=='x') x++; else q++; } if (o+q>0){ //数字一つ if (o==1) ans+=1; else if (o==0) ans += q; //cout << q << endl; } if (o+q>1) { if (o==2) ans += 14; else if (o==1) ans += q*14; else if (o==0) ans += q*(q-1)/2*14; //cout << q*(q-1)/2*14 << endl; } if (o+q>2){ if (o==3) ans += 36; else if (o==2) ans += q*36; else if (o==1) ans += q*(q-1)/2*36; else if (o==0) ans += q*(q-1)*(q-2)/6*36; //cout << q*(q-1)*(q-2)/6*36 << endl; } if (o+q>3){ if (o==4) ans += 24; else if (o==3) ans += q*24; else if (o==2) ans += q*(q-1)/2*24; else if (o==1) ans += q*(q-1)*(q-2)/6*24; else if (o==0) ans += q*(q-1)*(q-2)*(q-3)/24*24; //cout << q*(q-1)*(q-2)*(q-3)/24*24 << endl; } cout << ans << endl; }
#include<bits/stdc++.h> #define ll long long #define N 200015 #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define inf 0x3f3f3f3f #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define lowbit(i) ((i)&(-i)) #define VI vector<int> #define all(x) x.begin(),x.end() #define SZ(x) ((int)x.size()) using namespace std; int n; char s[N]; int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); scanf("%d",&n); scanf("%s",s+1); if(s[1] != s[n]) return puts("1"),0; else{ rep(i,2,n-2) if(s[i] != s[1] && s[i+1] != s[n]){ puts("2"); return 0; } } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; if (s[0] != s[n - 1]) { cout << 1 << endl; } else { for (int i = 1; i + 1 < n - 1; i++) { if (s[i] != s[0] && s[i + 1] != s[n - 1]) cout << 2 << endl, exit(0); } if (n % 2 == 1) cout << -1 << endl, exit(0); bool flag = 1; for (int i = 0; i < n; i += 2) { flag &= (s[i] != s[i + 1]); } if (flag) cout << n / 2 << endl; else cout << -1 << endl; } return 0; }
#include<iostream> #include<string> using namespace std; int main() { int t=1000; int s1,s2,d1,d2; string p=""; int k; while(t--){ cin>>s1>>s2>>d1>>d2; p=""; if (s1>d1){ while (s1!=d1){ p+="U"; s1--; } } else if (d1>s1){ while (d1!=s1){ p+="D"; d1-- ; } } if (s2>d2){ while (s2!=d2){ p+="L"; s2--; } } else if (d2>s2){ while (d2!=s2){ p+="R"; d2-- ; } } cout<<p<<endl; fflush(stdin); cin>>k; } return 0; }
#include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using P = pair<ll,ll>; #define pb push_back int main(){ vvl tate(29, vl(30,5000)); vvl yoko(30, vl(29,5000)); rep(_,1000){ int a, b, ato, bto; cin >> a >> b >> ato >> bto; int ma = a; int mb = b; string ans; while(a != ato || b != bto){ if (a == ato){ if (b < bto){ b++; ans.pb('R'); continue; // continueの貼り付けを忘れると } else { // バグる ato btoが連続するからです b--; // 全てに張り付けておく必要があるので ans.pb('L'); continue; } } if (b == bto){ if (a < ato){ a++; ans.pb('D'); continue; } else { a--; ans.pb('U'); continue; } } if (a < ato && b < bto){ // 下 右 if (tate[a][b] < yoko[a][b]){ a++; // 縦移動優先 ans.pb('D'); continue; } else { b++; ans.pb('R'); continue; } } if (a < ato && b > bto){ // 下 左 if (tate[a][b] < yoko[a][b-1]){ a++; // 縦移動優先 ans.pb('D'); continue; } else { b--; ans.pb('L'); continue; } } if (a > ato && b < bto){ // 上 右 if (tate[a-1][b] < yoko[a][b]){ a--; // 縦移動優先 ans.pb('U'); continue; } else { b++; ans.pb('R'); continue; } } if (a > ato && b > bto){ // 上 左 ここから続き if (tate[a-1][b] < yoko[a][b-1]){ a--; // 縦移動優先 ans.pb('U'); continue; } else { b--; ans.pb('L'); continue; } } } cout << ans << endl; // flushを炊く ll score; cin >> score; score /= ans.size(); // 通った数で割り一歩当たりのコスト計算 ll change = (5000 - score) / ans.size(); rep(i,ans.size()){ if (ans[i] == 'U'){ tate[ma-1][mb] += change; ma--; } if (ans[i] == 'D'){ tate[ma][mb] += change; ma++; } if (ans[i] == 'L'){ yoko[ma][mb-1] += change; mb--; } if (ans[i] == 'R'){ yoko[ma][mb] += change; mb++; } } // for(auto x : tate){ // コスト計算debug // for(auto y : x){ // cout << y << " "; // } cout << endl; // } // for(auto x : yoko){ // for(auto y : x){ // cout << y << " "; // } cout << endl; // } } }
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> #include <utility> using namespace std; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase #define REP(i, n) for(int i = 0;i < (n);i++) /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const llint mod=998244353; const llint inf=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-10; template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} void dfs(int ter,vector<vector<tuple<int,int,int>>>&go,vector<int>&ans){ for(auto it:go[ter]){ if(ans[get<1>(it)]!=-1){continue;} ans[get<1>(it)]=get<2>(it); dfs(get<0>(it),go,ans); } } int main(void){ int i,n,m;cin>>n>>m; vector<char>L(n); vector<pair<int,int>>road(m); vector<vector<tuple<int,int,int>>>go(n); vector<int>ans(m,-1); vector<int>c(n); for(i=0;i<m;i++){ int a,b;cin>>a>>b;a--;b--; road[i]=mp(a,b); } for(i=0;i<n;i++){cin>>c[i];} for(i=0;i<m;i++){ int a,b;tie(a,b)=road[i]; if(c[a]!=c[b]){continue;} go[a].pub({b,i,0}); go[b].pub({a,i,1}); } vector<int>d(n); for(i=0;i<n;i++){ if(L[i]==0&&go[i].size()>0){//ここを出発点とする dfs(i,go,ans); } } for(i=0;i<m;i++){ int a,b;tie(a,b)=road[i]; if(c[a]>c[b]){cout<<"->"<<endl;} else if(c[a]<c[b]){cout<<"<-"<<endl;} else {cout<<(ans[i]?"<-":"->")<<endl;} } }
#include <bits/stdc++.h> #define sz(v) ((int)(v).size()) #define all(v) ((v).begin()),((v).end()) #define allr(v) ((v).rbegin()),((v).rend()) #define pb push_back #define mp make_pair #define Y imag() #define X real() #define clr(v,d) memset( v, d ,sizeof(v)) #define angle(n) atan2((n.imag()),(n.real())) #define vec(a,b) ((b)-(a)) #define length(a) hypot( (a.imag()),(a.real()) ) #define normalize(a) (a)/(length(a)) #define dp(a,b) (((conj(a))*(b)).real()) #define cp(a,b) (((conj(a))*(b)).imag()) #define lengthsqrt(a) dp(a,a) #define rotate0( a,ang) ((a)*exp( point(0,ang) )) #define rotateA(about,p,ang) (rotate0(vec(about,p),ang)+about) #define lcm(a,b) ((a*b)/(__gcd(a,b))) #define reflection0(m,v) (conj((v)/(m))*(m)) #define reflectionA(m,v,p0) (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0 #define same(p1,p2) ( dp( vec(p1,p2),vec(p1,p2)) < eps ) #define point complex<long double> #define PI acos(-1) typedef long long ll ; typedef unsigned long long ull; const double eps= (1e-15); using namespace std; int dcmp(double a,double b){ return fabs(a-b)<=eps ? 0: (a>b)? 1:-1 ;} int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;} int setBit1(int num, int idx) {return num | (1<<idx);} ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);} ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);} void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);} int countNumBit1(int mask) {int ret=0; while (mask) {mask &= (mask-1);++ret; }return ret;} const ll N=200000+5,mod=998244353,sh=1000000,inf=100000000; int mem[N],a[N]; vector<int> v[N]; int solve(int no){ int &ret=mem[no]; if(ret!=-1) return ret; int ans=0; for(int i=0;i<sz(v[no]);i++){ int nw=v[no][i]; ans=max(ans,max(a[nw],solve(nw))); } return ret=ans; } int main(){ GO(); int T=1; // cin>>T; while(T--){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=0;i<m;i++){ int a,b; cin>>a>>b; v[a].pb(b); } clr(mem,-1); int ans=INT_MIN; for(int i=1;i<=n;i++){ if(solve(i)==0) continue; ans=max(ans,solve(i)-a[i]); } cout<<ans<<"\n"; } }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define deb(...) #endif #define endl "\n" #define pb push_back #define int long long #define ll long long #define pii pair<int,int> #define F first #define S second #define all(c) c.begin(),c.end() #define read(v) for(auto &it:v) cin>>it; const int inf = 1e18; const int N = 5e3 + 5; const int mod = 998244353; int maxi[12]; int n, m; int ans = 0; namespace factorial { int fact[N + 5]; int power(int a, int b) { if (b == 0) return 1; if (b == 1) return a % mod; if (b % 2 == 1) return (power(a, b - 1) * a) % mod; int q = power(a, b / 2); return (q * q) % mod; } int modinverse(int n) { return power(n, mod - 2); } void generate_fact_() { fact[0] = 1; for (int i = 1; i < N; i++) fact[i] = (fact[i - 1] * i) % mod; } int ncr(int n, int r) { if (fact[n] == 0) { cerr << "Call generate_fact_ you dope" << endl; exit(1); } if (r == 0) return 1; return (fact[n] * (modinverse(fact[r]) ) % mod * (modinverse(fact[n - r]) ) % mod) % mod; } } using namespace factorial; namespace modop { ll madd(ll a, ll b) { return (a + b) % mod; } ll msub(ll a, ll b) { return (((a - b) % mod) + mod) % mod; } ll mmul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; } ll mpow(ll base, ll exp) { ll res = 1; while (exp) { if (exp % 2 == 1) { res = (res * base) % mod; } exp >>= 1; base = (base * base) % mod; } return res; } ll minv(ll base) { return mpow(base, mod - 2); } ll mdiv(ll a, ll b) { return mmul(a, minv(b)); } } using namespace modop; int dp[12][5005]; int go(int bit, int sum) { if (bit == 12) { if (sum == 0) { return 1; } else { return 0; } } if (dp[bit][sum] != -1) { return dp[bit][sum]; } int ans = 0; int p = (1LL << bit); for (int i = 0; i <= maxi[bit]; i += 2) { int zx = i * p; if (zx <= sum) { int coeff = ncr(n, i); ans = madd(ans, mmul(coeff, go(bit + 1, sum - zx))); } else { break; } } return dp[bit][sum] = ans; } void testCase() { cin >> n >> m; generate_fact_(); for (int i = 0; i <= 11; i++) { int max_take = min(n, m / (1 << i)); maxi[i] = max_take; } memset(dp, -1, sizeof dp); cout << go(0, m) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--)testCase(); return 0; }
#include<bits/stdc++.h> using namespace std; #define Mod(x) (x>=P)&&(x-=P) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) #define erep(i,a) for(int i=hd[a];i;i=nxt[i]) typedef long long ll; void Max(int &x,int y){(x<y)&&(x=y);} void Min(int &x,int y){(x>y)&&(x=y);} bool vio; char IO; int rd(int res=0){ bool f=0; while(IO=getchar(),IO<48||IO>57) f=IO=='-'; do res=(res<<1)+(res<<3)+(IO^48); while(IO=getchar(),isdigit(IO)); return f?-res:res; } const int M=5005,P=998244353; int pw[M][M],s[M]; bool let; int main(){ cerr<<(&vio-&let)/1024.0/1024<<endl; int n=rd(),m=rd(); rep(i,0,m){ rep(j,pw[i][0]=1,n){ pw[i][j]=1ll*pw[i][j-1]*i%P; if(i!=m)s[j]+=pw[i][j],Mod(s[j]); } } s[0]=m; int ans=0; rep(i,1,n){ if(i!=1)ans=1ll*ans*m%P; rep(j,1,i-1){ ans+=1ll*pw[m][j-1]*(1ll*m*pw[m-1][i-j-1]%P-s[i-j-1]+P)%P; //rep(k,1,m) //ans+=1ll*pw[m][j-1]*(pw[m-1][i-j-1]-pw[m-k][i-j-1]+P)%P,Mod(ans); Mod(ans); } ans+=1ll*pw[m-1][i-1]*m%P,Mod(ans); } printf("%d",ans); }
#include<bits/stdc++.h> #define ll long long using namespace std; int n,m; int main() { #ifdef lovelyG Sfile(1); #endif scanf("%d%d",&n,&m); if(m==0) { for(int i=1;i<=n;++i) { printf("%d %d\n",i*2,i*2+1); } return 0; } if(n<m+2||m<0) { puts("-1"); return 0; } printf("%d %d\n",1,1000000000); for(int i=1;i<=m;++i) { printf("%d %d\n",i*2,i*2+1); } int l=(m+1)*2,r=999999999; printf("%d %d\n",l,r); n-=m+2; for(int i=1;i<=n;++i) { ++l;--r; printf("%d %d\n",l,r); } return 0; }
//DUEL #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define x first #define y second #define pii pair<int,int> #define pb push_back #define eb emplace_back #pragma GCC optimize("unroll-loops") #define shandom_ruffle(a, b) shuffle(a, b, rng) #define vi vector<int> #define vl vector<ll> #define popcnt __builtin_popcount #define popcntll __builtin_popcountll #define all(a) begin(a),end(a) using namespace std; using namespace __gnu_pbds; using ll=long long; using ull=unsigned long long; using ld=long double; int MOD=1000000007; int MOD2=998244353; vector<int> bases; const ll LLINF=1ll<<60; const char en='\n'; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void yes() {cout<<"YES"<<en; exit(0);} void no() {cout<<"NO"<<en; exit(0);} inline int rund() {int x576363482791fuweh=rng();return abs(x576363482791fuweh)%RAND_MAX;} template<class T> void prVec(vector<T> w,bool fl=false) { cout<<w.size()<<en; for (int i=0;i<int(w.size())-1;++i) cout<<w[i]<<' '; if (w.size()) cout<<w[w.size()-1]<<en; if (fl) cout<<flush; } void M998() { swap(MOD,MOD2); } ll raand() { ll a=rund(); a*=RAND_MAX; a+=rund(); return a; } #define rand raand ll raaand() { return raand()*(MOD-7)+raand(); } void compress(vi&v) { set<int> s; for (auto a: v) s.insert(a); vi o(all(s)); for (auto&a: v) a=lower_bound(all(o),a)-o.begin(); } void compress(vl&v) { set<ll> s; for (auto a: v) s.insert(a); vl o(all(s)); for (auto&a: v) a=lower_bound(all(o),a)-o.begin(); } string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; } string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; } ll sti(string a,int base=10) { ll k=0; for (int i=0;i<(int)a.size();++i) { k*=base; k+=a[i]-'0'; } return k; } template<class T> void eras(vector<T>& a,T b) { a.erase(find(a.begin(),a.end(),b)); } string its(ll k,int base=10) { if (k==0) return "0"; string a; while (k) { a.push_back((k%base)+'0'); k/=base; } reverse(a.begin(),a.end()); return a; } ll min(ll a,int b) { if (a<b) return a; return b; } ll min(int a,ll b) { if (a<b) return a; return b; } ll max(ll a,int b) { if (a>b) return a; return b; } ll max(int a,ll b) { if (a>b) return a; return b; } ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b,a%b); } ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } template<class T,class K> pair<T,K> mp(T a,K b) { return make_pair(a,b); } inline int mult(ll a,ll b) { return (a*b)%MOD; } inline int pot(int n,int k) { if (k==0) return 1; ll a=pot(n,k/2); a=mult(a,a); if (k%2) return mult(a,n); else return a; } int divide(int a,int b) { return mult(a,pot(b,MOD-2)); } inline int sub(int a,int b) { if (a-b>=0) return a-b; return a-b+MOD; } inline int add(int a,int b) { if (a+b>=MOD) return a+b-MOD; return a+b; } bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) { if (a%i==0) return 0; } return 1; } int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; const int N=300010; int t,n,m; int main() { ios_base::sync_with_stdio(false); cin.tie(0); for (int i=0;i<10;++i) bases.push_back(rand()%(MOD-13893829*2)+13893829); cin>>n>>m; if (n==1 && m==0) cout<<"1 2"<<en; else if (m<0 || m>=n-1) cout<<-1<<en; else { for (int i=0;i<n-m-2;++i) cout<<i*2+1<<' '<<i*2+2<<en; int st=(n-m-2)*2+1; cout<<st<<' '<<int(1e9)<<en; for (int i=0;i<=m;++i) cout<<st+i*2+1<<' '<<st+i*2+2<<en; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair <int,int> pii; #define rep(i,x,y) for(int i=x;i<y;i++) #define rept(i,x,y) for(int i=x;i<=y;i++) #define per(i,x,y) for(int i=x;i>=y;i--) #define pb push_back #define mp make_pair #define fi first #define se second #define de(x) cout<< #x<<" = "<<x<<"\n" #define dd(x) cout<< #x<<" = "<<x<<" " #define debug() cout<<"I love Miyamizu Mitsuha forever!\n" #define mes(a,b) memset(a,b,sizeof a) const int inf=0x3f3f3f3f; const int maxn=305; const double eps=1e-8; pii p[maxn]; int n; double d(int x1,int y1,int x2,int y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } class ss { public: int id1,id2; double d; }dis[1000005]; bool comp(ss s1,ss s2) { return s1.d<s2.d; } int f[maxn]; bool vis1[maxn],vis2[maxn]; int find_father(int x) { if(f[x]==x) return x; else return f[x]=find_father(f[x]); } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; rep(i,0,n) cin>>p[i].fi>>p[i].se; int cnt=0; rep(i,0,n) { p[i+n].fi=p[i+2*n].fi=p[i].fi; p[i+n].se=100; p[i+2*n].se=-100; vis1[i+n]=1; vis2[i+2*n]=1; } rep(i,0,3*n) { rep(j,i+1,3*n) { dis[cnt].id1=i; dis[cnt].id2=j; dis[cnt].d=d(p[i].fi,p[i].se,p[j].fi,p[j].se); cnt++; } } sort(dis,dis+cnt,comp); rep(i,0,3*n) f[i]=i; rep(i,0,cnt) { int fa=find_father(dis[i].id1),fb=find_father(dis[i].id2); f[fa]=fb; vis2[fb]|=vis2[fa]; vis1[fb]|=vis1[fa]; if(vis2[fb]&&vis1[fb]) { cout<<fixed<<setprecision(15)<<dis[i].d/2<<"\n"; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vb = vector<bool>; using vl = vector<long>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvl = vector<vector<long>>; using pii = pair<int, int>; using pil = pair<int, long>; using pll = pair<long, long>; #define fix20 cout << fixed << setprecision(20) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define rep(i,n) for(long i=0; i<(long)(n);i++) #define REP(i,s,t) for(long i=s; i<t; i++) #define RNG(i,s,t,u) for(long i=s; i<t; i+=u) #define MOD 1000000007 #define all(vec) vec.begin(), vec.end() 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; } vector<long> par; void Initpar(long n){ par = vector<long>(n); for(long i=0; i<n; i++) par.at(i) = i; } long root(long x){ if(x == par.at(x)) return x; else return par.at(x) = root(par.at(x)); } void unite(long x, long y){ long rx = root(x); long ry = root(y); if(rx == ry) return ; if(rx < ry) par.at(ry) = rx; else par.at(rx) = ry; } bool same(long x, long y){ return root(x) == root(y); } void update_par(){ for(int i=0;i<par.size();i++){ root(i); } } struct Edge { double cost; long from; long to; bool operator<(const Edge& o) const { return cost < o.cost; } }; // long root(long x, vector<long> &par){ // if(x == par.at(x)) return x; // else par.at(x) = root(par.at(x), par); // } // bool unite(long x, long y, vector<long> &par){ // long rx = root(x, par); // long ry = root(y, par); // if(rx == ry) return true; // else if(rx < ry) par.at(ry) = rx; // else par.at(rx) = ry; // return false; // } // bool same(long x, long y, vector<long> &par){ // return root(x,par) == root(y,par); // } int main(){ long n; cin >> n; vector<pair<long,long>> d(n); rep(i,n) cin >> d.at(i).first >> d.at(i).second; vector<Edge> list; rep(i,n){ double x = (double)d.at(i).first; double y = (double)d.at(i).second; REP(j,i+1,n){ double nx = (double)d.at(j).first; double ny = (double)d.at(j).second; double r = sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y))/2.0; list.push_back(Edge{r,i,j}); } } rep(i,n){ double y = (double)d.at(i).second; list.push_back(Edge{(100-y)/2.0,i,n}); list.push_back(Edge{(y+100)/2.0,i,n+1}); } list.push_back(Edge{100, n, n+1}); sort(list.begin(), list.end()); Initpar(n+2); rep(i,n+2) par.at(i) = i; for(Edge ed : list){ long s = ed.from; long t = ed.to; double r = ed.cost; unite(s,t); if(same(n,n+1)){ cout << r << endl; return 0; } } }
#include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define int ll #define pb push_back #define setbits(x) __builtin_popcountll(x) #define endl "\n" typedef long long ll; #define MX 200002 int dp[MX]; void solve() { int n,m; cin>>n>>m; int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; int pref[n+10]={}; sort(arr,arr+n); pref[1]=arr[1]-arr[0]; for(int i=3;i<n;i+=2) { pref[i]=arr[i]-arr[i-1]; pref[i]+=pref[i-2]; } int suff[n+10]={}; suff[n-2]=arr[n-1]-arr[n-2]; for(int i=n-4;i>=0;i-=2) { suff[i]=arr[i+1]-arr[i]; suff[i]+=suff[i+2]; } int cnt=INT_MAX; for(int i=0;i<m;i++) { int x; cin>>x; int pos=lower_bound(arr,arr+n,x)-arr; int ans=0; if(pos%2==0) { if(pos!=0) ans+=pref[pos-1]; if(pos<n-1) ans+=suff[pos+1]; ans+=abs(arr[pos]-x); } else { ans+=abs(arr[pos-1]-x); ans+=suff[pos]; if(pos-2>=0) ans+=pref[pos-2]; } cnt=min(cnt,ans); } cout<<cnt<<endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pl; #define all(a) (a).begin(),(a).end() #define overload4(_1, _2, _3, _4, name, ...) name #define rep1(n) for(ll i = 0;i < (n);i++) #define rep2(i,n) for(ll i = 0;i < (n);i++) #define rep3(i,a,b) for(ll i = (a);i < (b);i++) #define rep4(i,a,b,c) for(ll i = (a);i < (b);i += (c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) for(ll i = (n)-1;i >= 0;i--) #define rrep2(i,n) for(ll i = (n)-1;i >= 0;i--) #define rrep3(i,a,b) for(ll i = (a)-1;i >= (b);i--) #define rrep4(i,a,b,c) for(ll i = (a)-1;i >= (b);i -= (c)) #define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define SHOW(x) cout << #x << ": " << x << endl; template<class T> ll sumArr(const T& a){ return accumulate(all(a), 0LL);} template<class T> auto minArr(const T& a){ return *min_element(all(a));} template<class T> auto maxArr(const T& a){ return *max_element(all(a));} template<class T> void DEBUG_PRINT(T tmp){cout<<tmp<<endl;} template<class T1,class... T2> void DEBUG_PRINT(T1 tmp,T2... tmp2){cout<<tmp<<" ";DEBUG_PRINT(tmp2...);} ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b){ if(!a || !b) return 0; return a / gcd(a, b) * b; } const ll INF=1e18,MOD = 1e9+7; const int iINF = 1e9; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll n,m,ans = INF; cin>>n>>m; vector<ll>h(n),w(m); rep(i,n)cin>>h[i]; rep(i,m)cin>>w[i]; sort(all(h)); vector<int>a((n + 1) / 2,0); rep(i,1,n - 1,2){ a[0] += abs(h[i] - h[i + 1]); } rep(i,n / 2){ a[i + 1] = a[i] - abs(h[i * 2 + 1] - h[i * 2 + 2]) + abs(h[i * 2] - h[i * 2 + 1]); } rep(i,m){ auto itr = lower_bound(all(h),w[i]); int dist = itr - h.begin(); int p = dist / 2; ans = min(ans,a[p] + abs(h[p * 2] - w[i])); } cout<<ans<<endl; }
#include<bits/stdc++.h> #define ll long long int #define pragi(a,b) for(ll i = a;i<b;i++) #define pragj(a,b) for(ll j = a;j<b;j++) #define pragk(a,b) for(ll k = a;k>=b;k--) #define all(v) (v.begin(),v.end()) #define eb emplace_back #define lb lower_bound #define ub upper_bound #define MP make_pair #define MT make_tuple #define F first #define S second #define KAKA ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int mod = 1e9 + 7; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<bool> vb; typedef long double ld; typedef pair<int, int> pii; //typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<ll, ll> pll; vector<ll> parent,level; vector<vector<ll> > Adj; vector<ll> flat_tree; vector<ll> pre,post; ll timer = 0; vector<vector<ll> > segtree(5e6); void build(ll node,ll tl,ll tr) { if(tl == tr) { segtree[node] = vector<ll> (1,flat_tree[tl]); return; } else { ll mid = (tl + tr)/2; build(2 * node,tl,mid); build(2 * node + 1,mid + 1,tr); merge(segtree[node*2].begin(), segtree[node*2].end(), segtree[node*2+1].begin(), segtree[node*2+1].end(), back_inserter(segtree[node])); } } ll query(ll node,ll tl,ll tr,ll ql,ll qr,ll k) { if(ql > qr) { return 0ll; } else if((ql == tl) and (qr == tr)) { auto it2 = upper_bound(segtree[node].begin(),segtree[node].end(),k); auto it1 = lower_bound(segtree[node].begin(),segtree[node].end(),k); ll ans = it2 - it1; return ans; } else { ll mid = (tl + tr)/2; ll l = query(node * 2,tl,mid,ql,min(qr,mid),k); ll r = query(node * 2 + 1,mid + 1,tr,max(ql,mid + 1),qr,k); return (l + r); } } void dfs(ll node,ll par,ll dist) { parent[node] = par; level[node] = dist; flat_tree.eb(level[node]); pre[node] = timer; timer++; for(auto child : Adj[node]) { if(child == par) continue; else { dfs(child,node,dist + 1); } } post[node] = timer++; flat_tree.eb(level[node]); } void Compute(ll n) { KAKA parent.resize(n + 1); level.resize(n + 1); Adj.resize(n + 1); pre.resize(n + 1);post.resize(n + 1); pragi(2,n + 1) { cin>>parent[i]; Adj[i].eb(parent[i]); Adj[parent[i]].eb(i); } dfs(1,-1,0); /*for(auto ele : flat_tree) { cout<<ele<<" "; } cout<<"\n";*/ ll m = flat_tree.size(); build(1,0,m - 1); ll q; cin>>q; while(q--) { ll u,d; cin>>u>>d; ll ans = query(1,0,m - 1,pre[u],post[u],d); cout<<ans/2<<"\n"; } } int main() { KAKA ll n; cin>>n; Compute(n); }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <algorithm> #define rep(i,n) for(int i=0;i<(n);++i) #define all(a) (a).begin(),(a).end() using namespace std; using Graph = vector<vector<int>>; typedef long long ll; //using Graph = vector<vector<pair<ll,ll>>>; const int mod =1e+9+7; const int dy[4]={0,1,0,-1}; const int dx[4]={1,0,-1,0}; const ll INF=1e18; int main(){ ll n; cin>>n; vector<vector<ll>>x(n,vector<ll>(4)); vector<ll>a(n); ll suma=0; rep(i,n){ cin>>x[i][1]>>x[i][2]; x[i][3]=i; x[i][0]=2*x[i][1]+x[i][2]; a[i]=x[i][1]; suma+=a[i]; } sort(all(x)); reverse(all(x)); ll Takahashi=0; ll cnt=0; rep(i,n){ Takahashi+=x[i][1]+x[i][2]; suma-=x[i][1]; cnt++; if(Takahashi>suma){ cout<<cnt<<endl; return 0; } } }
#include <iostream> #include <vector> #include <cmath> using namespace std; typedef long long ll; vector<vector<ll>> comb(int n, int r) { vector<vector<ll>> v(n + 1, vector<ll>(n + 1, 0)); for (int i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (int i = 1; i < v.size(); i++) { for (int j = 1; j < i; j++) { v[i][j] = v[i - 1][j - 1] + v[i - 1][j]; } } return v; } auto ncr = comb(60, 30); string recur(int A, int B, ll K) { if (A == 0) return string(B, 'b'); if (B == 0) return string(A, 'a'); if (K <= ncr[A + B - 1][B]) { return "a" + recur(A - 1, B, K); } else { return "b" + recur(A, B - 1, K - ncr[A + B - 1][B]); } } int main() { int A, B; ll K; cin >> A >> B >> K; cout << recur(A, B, K) << endl; }
//int a = s - '0'; 文字から数字 //小文字から大文字 //transform(a.begin(), a.end(), a.begin(), ::toupper); //map 全探索 //auto begin = p.begin(), end = p.end(); //for (auto it = begin; it != end; it++) {} //mapのキー:it->first mapのバリュー:it->second //大文字判定 isupper(文字) 小文字判定 islower(文字) //do{}while(next_permutation(ALL(配列))) //小文字に対応する文字コード:S[i] - 'a' //文字コード→小文字:(char)(数字+'a') //グラフの距離:隣接行列で扱う //bool型 初期値はTrue //島渡りの問題:中間ノードに着目 //数が大きい時の比較はstring型で行う //全て0になったか調べたい->0になるたびにcntする //例外処理は最初にする //x = p^m + q^n...の約数の個数:(n+1)*(m+1).... //N!のどの素因数で何回割れるか //⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和 //パズルの問題->一般化して全探索 //stack<ll> s; //s.push(要素);s.top();s.pop(); //queue<ll> q; //q.push(要素);q.front();q.pop(); //同じ作業繰り返す系の問題:収束先を見つける //過半数:N/2.0で判定 //優先度付きキュー //priority_queue< //ll, //vector<ll> //> q; #include <bits/stdc++.h> #define rep(i,N) for(int i = 0; i < N;i++) #define ALL(a) (a).begin(),(a).end() #define ll long long int #define PI 3.14159265358979323846264338327950L using namespace std; //割るやつ const ll MOD = (pow(10, 9) + 7); // K進数でのNの桁数 ll dig(ll N, ll K) { ll dig = 0; while (N) { dig++; N /= K; } return dig; } // a,bの最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); } //a,bの最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } //階乗計算 ll f(ll n) { if (n == 0 || n == 1) return 1; else return (n * f(n - 1)); } //Nのd桁目の数 ll dignum(ll N, ll d) { ll x = pow(10, d); N %= x; ll y = pow(10, d - 1); N /= y; return N; } //Nをdで何回割れるか ll divcnt(ll N, ll d) { ll ans = 0; while (1) { if (N%d == 0) { ans++; N /= d; } else break; } return ans; } //素数判定 bool prime(ll num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } //フィボナッチ数列 vector<ll> memo(pow(10, 6) + 1); ll fibo(ll n) { if (n == 1) return 1; else if (n == 2) return 1; else if (memo[n] != 0) return memo[n]; else return memo[n] = fibo(n - 1) + f(n - 2); } ll RS(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } return N * RS(N, P - 1, M); } vector<int> IntegerToVector(int bit, int N) { vector<int> S; for (int i = 0; i < N; ++i) { if (bit & (1 << i)) { S.push_back(i); } } return S; } int main() { int a, b, c, d;cin >> a >> b >> c >> d; cout << a * d - b * c << endl; }
#include<iostream> #include<string> using namespace std; int main() { string s; cin>>s; bool flag=false; for(int i=0;i<s.size();i++) { if(i%2!=0) { if(s[i]>='A' && s[i]<='Z') { continue; } else{ flag=true; break; } } else{ if(s[i]>='a' && s[i]<='z') { continue; } else{ flag=true; break; } } } if(flag) { cout<<"No"; } else{ cout<<"Yes"; } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int long long int #define pb push_back #define pi pair<int, int> #define pii pair<pi, int> #define fir first #define sec second #define DEBUG 0 #define MAXN 5001 #define mod 1000000007 signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> ans; for (int i = 1; i <= sqrt(n); i++) { if (!(n % i)) { ans.pb(i); if (n / i != i) ans.pb(n / i); } } sort(ans.begin(), ans.end()); for (auto const &i : ans) cout << i << endl; return 0; }
#include<iostream> #include<string> #include<vector> #include<set> #include<iomanip> #include<algorithm> #include<cmath> #include<bitset> #include<queue> #include<stack> #include<utility> #include<cstdlib> #include<cstdio> #include<map> #include<unordered_set> #include<unordered_map> #include<list> #include<tuple> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define FOR(i, a, b) for(ll i=(a); i<(b); ++i) #define REP(i, n) FOR(i, 0, n) #define NREP(i, n) FOR(i, 1, n+1) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; struct Edge{ ll to, cost; Edge(ll t, ll c) : to(t), cost(c) { } }; using Graph = vector<vector<Edge>>; ll MODpow(ll x, ll n, ll mod){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n >>= 1; } return res; } const ll MOD=1e9+7; const ll nmax=200000+10; ll N; Graph G(nmax); ll solve(){ vvl color(nmax,vl(60,-1)); vl cnt1(60,0); vl seen(nmax,0); queue<ll> q; q.push(0); REP(i,60) color[0][i]=0; while(!q.empty()){ ll v=q.front(); q.pop(); seen[v]=1; for(auto e:G[v]){ if(seen[e.to]) continue; ll et=e.to, ec=e.cost; q.push(e.to); REP(i,60){ bool eci=ec&1; ec>>=1; color[et][i]=color[v][i]^eci; if(color[et][i]) cnt1[i]++; } } } ll res=0,rec=1; REP(i,60){ ll mid1=rec; rec=rec*2%MOD; ll mid2=(N-cnt1[i])*cnt1[i]%MOD; ll mid=mid1*mid2%MOD; res+=mid; res%=MOD; } return res; } int main(void){ scanf(" %ld",&N); REP(i,N-1){ ll u,v,w; scanf(" %ld%ld%ld",&u,&v,&w); u--,v--; G[u].push_back(Edge{v,w}); G[v].push_back(Edge{u,w}); } ll res=solve(); printf("%ld\n",res); return 0; }
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <utility> #include <sstream> #include <algorithm> #include <numeric> int read_int() { std::string s; std::getline(std::cin, s); return std::stoi(s); } std::pair<int,int> read_int_pair() { std::string s; std::getline(std::cin, s); size_t pos = s.find_first_of(" \t"); if (pos == std::string::npos) { throw std::invalid_argument("Not found space delimiter to parse pair of ints"); } int fst = std::stoi(s.substr(0, pos)); while (++pos < s.length() && (s[pos] == ' ' || s[pos] == '\t')) { } if (pos == s.length()) { throw std::invalid_argument("Not found string part 2 to parse pair of ints"); } int snd = std::stoi(s.substr(pos)); return {fst, snd}; } bool have_colinear_3(const std::vector<std::pair<int,int>>& points) { const size_t len = points.size(); for (size_t i = 0; i < len - 2; i++) { for (size_t j = i + 1; j < len - 1; j++) { for (size_t k = j + 1; k < len; k++) { std::vector<std::pair<int,int>> tri { points[i], points[j], points[k] }; std::sort(tri.begin(), tri.end(), [](std::pair<int,int>& a, std::pair<int,int>& b){ return a.first < b.first; }); int x0 = tri[0].first; int x1 = tri[1].first; int x2 = tri[2].first; int y0 = tri[0].second; int y1 = tri[1].second; int y2 = tri[2].second; if (x0 == x1 && x1 == x2) { return true; } else if (y0 == y1 && y1 == y2) { return true; } else if (x0 != x1 && x1 != x2 && y0 != y1 && y1 != y2) { // if (y1 > y0 && y2 > y1) { int a = x1 - x0; int b = y1 - y0; int A = x2 - x0; int B = y2 - y0; if (b * A == B * a) { return true; } // } } } } } return false; } int main() { const int n = read_int(); std::vector<std::pair<int,int>> points; for (int i = 0; i < n; i++) { auto point = read_int_pair(); points.push_back(point); } auto answ = have_colinear_3(points) ? "Yes" : "No"; std::cout << answ << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1LL << 60; 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() { ll n; cin >> n; ll ans; if (n <= 2) { ans = 1; } else { ll c = 0; ll i = 1; ll sum = 1; while (sum <= n + 1) { i += 1; sum += i; c++; } ans = (n + 1) - c; } cout << ans << endl; return 0; }
#include<cstdio> #include<algorithm> #include<vector> #include<set> using namespace std; using ll = long long; constexpr int N = 1e6 + 5; int ed; ll n; ll v[200]; void init() { v[1] = 1; ll b = 0; int t = 1; for(int i = 2; i <= 200; ++i) { v[i] = v[i - 1]; if(t) { b = b + v[i - 1]; } else { v[i] = v[i - 1] + b; } // printf("%lld\n", v[i]); t ^= 1; if(b >= 1e18 && v[i] >= 1e18) { ed = i; break; } } } vector<int> sq; void dfs(ll res) { int tt = upper_bound(v + 1, v + 1 + ed, res) - v - 2; sq.emplace_back(tt); if(v[tt] != res) dfs(res - v[tt]); } int main() { init(); // for(int i = 1; i <= 10; ++i) printf("%lld ", v[i]); scanf("%lld", &n); dfs(n); // printf("kk %d\n", (int) sq.size()); vector<int> ans; ans.emplace_back(1); int cnt = 1; for(int i = 1; i <= sq[0]; ++i) { if(i & 1) { while(cnt < (int) sq.size() && sq[cnt] == sq[0] - i + 1) { ans.emplace_back(1); ++cnt; } if(i != 1) ans.emplace_back(3); } else { ans.emplace_back(4); } } printf("%d\n", (int) ans.size()); for(int num : ans) printf("%d\n", num); }
#include<bits/stdc++.h> using namespace std; #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 const int INF=1e18; const int N=1e2+5; int powm(int a,int b) { int res=1; while(b) { if(b&1) res=(res*a)%M; a=(a*a)%M; 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; } vi a[N],vis(N,0),len(N,0); set<pair<int,int> > s; void dfs(int node,int par) { vis[node]=1; for(auto j:a[node]) { if(!vis[j]) { len[j]=len[node]+1; s.insert({node,j}); dfs(j,node); } else if(j!=par&&len[j]<len[node]) { s.insert({node,j}); } } } signed main(void) {ios #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout); #endif int n,m; cin>>n>>m; vi u(m),v(m); rep(i,0,m) { cin>>u[i]>>v[i]; } vi c(n+1); rep(i,1,n+1) cin>>c[i]; vi dir(m,0); rep(i,0,m) { if(c[u[i]]>c[v[i]]) { dir[i]=1; } else if(c[v[i]]>c[u[i]]) { dir[i]=0; } else { a[u[i]].pb(v[i]); a[v[i]].pb(u[i]); } } rep(i,1,n+1) { if(!vis[i]) { dfs(i,0); } } rep(i,0,m) { if(c[u[i]]==c[v[i]]) { if(s.find({u[i],v[i]})!=s.end()) dir[i]=1; else dir[i]=0; } } rep(i,0,m) { if(dir[i]==1) cout<<"->"<<endl; else cout<<"<-"<<endl; } }
#include<bits/stdc++.h> #define int long long using namespace std; int k,a[1001001],dp[1001001][20],states,mod=1e9+7; string s; signed main() { cin>>s>>k; int n=s.size(); for(int i=1;i<=n;i++) { if(s[i-1]>='0'&&s[i-1]<='9') a[i]=s[i-1]-'0'; else a[i]=s[i-1]-55; } for(int i=0;i<n;i++) { for(int j=1;j<=16; j++) { dp[i+1][j]=(dp[i+1][j]+dp[i][j]*j%mod)%mod; dp[i+1][j+1]=(dp[i+1][j+1]+dp[i][j]*(16-j)%mod)%mod; } dp[i+1][0]=(dp[i+1][0]+dp[i][0])%mod; dp[i+1][1]=(dp[i+1][1]+dp[i][0]*15%mod)%mod; for(int j=0;j<a[i+1];j++) { int ns=states; if(i||j) ns|=(1<<j); int tmp=0; for(int i=30;i>=0;i--) if((ns&(1<<i))>0) tmp++; dp[i+1][tmp]+=1; } states|=(1<<a[i+1]); } int tmp=0; for(int i=30;i>=0;i--) if((states&(1<<i))>0) tmp++; cout<<dp[n][k]+(k==tmp); return 0; }
// #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define all(x) x.begin(), x.end() #define lch (o << 1) #define rch (o << 1 | 1) typedef double db; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pint; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tint; const int N = 2e5 + 5; const int INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; pll p[N], ops[N]; struct Query { int t, pid, id; ll x, y; }; Query qry[N]; int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i].fi >> p[i].se; int m; cin >> m; for (int i = 1; i <= m; i++) { int op, p; cin >> op; if (op == 3 || op == 4) cin >> p; ops[i] = make_pair(op, p); } int q; cin >> q; for (int i = 1; i <= q; i++) { int t, pid; cin >> t >> pid; qry[i] = (Query){t, pid, i}; } sort(qry + 1, qry + q + 1, [](Query &a, Query &b) { return a.t < b.t; }); pll x = make_pair(0, 1); pll y = make_pair(0, 1); bool flag = 0; int id = 1; for (int t = 0; t <= m; t++) { if (id > q) break; if (ops[t].fi == 1) { swap(x, y); y = make_pair(-y.fi, -y.se); flag ^= 1; } else if (ops[t].fi == 2) { swap(x, y); x = make_pair(-x.fi, -x.se); flag ^= 1; } else if (ops[t].fi == 3) { x = make_pair(2LL * ops[t].se - x.fi, -x.se); } else if (ops[t].fi == 4) { y = make_pair(2LL * ops[t].se - y.fi, -y.se); } while (qry[id].t == t) { auto &o = qry[id]; ll nx = p[o.pid].fi; ll ny = p[o.pid].se; if (flag) swap(nx, ny); nx = x.fi + x.se * nx; ny = y.fi + y.se * ny; o.x = nx, o.y = ny; id++; } } sort(qry + 1, qry + q + 1, [](Query a, Query b) { return a.id < b.id; }); for (int i = 1; i <= q; i++) cout << qry[i].x << ' ' << qry[i].y << endl; 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; #define SIZE 200005 struct LOC{ ll x,y; }; typedef vector<ll> V; typedef vector<V> MATRIX; int mat_size = 3; LOC loc[SIZE]; ll table[SIZE][3][3]; MATRIX calc(MATRIX left,MATRIX right){ MATRIX ret(mat_size,V(mat_size)); for(int i = 0; i < mat_size; i++){ for(int k = 0; k < mat_size; k++)ret[i][k] = 0; } for(int row = 0; row < mat_size; row++){ for(int col = 0; col < mat_size; col++){ for(int a = 0; a < mat_size; a++){ ret[row][col] += left[row][a]*right[a][col]; } } } return ret; } int main(){ int num_P; scanf("%d",&num_P); for(int i = 0; i < num_P; i++){ scanf("%lld %lld",&loc[i].x,&loc[i].y); } MATRIX tokei(mat_size,V(mat_size,0)); tokei[0][1] = 1; tokei[1][0] = -1; tokei[2][2] = 1; MATRIX ccw(mat_size,V(mat_size,0)); ccw[0][1] = -1; ccw[1][0] = 1; ccw[2][2] = 1; MATRIX move_X(mat_size,V(mat_size,0)); move_X[0][0] = -1; move_X[1][1] = 1; move_X[2][2] = 1; MATRIX move_Y(mat_size,V(mat_size,0)); move_Y[0][0] = 1; move_Y[1][1] = -1; move_Y[2][2] = 1; int M; scanf("%d",&M); MATRIX MULT(mat_size,V(mat_size,0)); for(int i = 0; i < mat_size; i++){ MULT[i][i] = 1; } for(int row = 0; row < 3; row++){ for(int col = 0; col < 3; col++){ table[0][row][col] = MULT[row][col]; } } int command; ll p; for(int i = 1; i <= M; i++){ scanf("%d",&command); if(command == 1){ MULT = calc(tokei,MULT); }else if(command == 2){ MULT = calc(ccw,MULT); }else{ scanf("%lld",&p); MATRIX work(mat_size,V(mat_size,0)); if(command == 3){ for(int row = 0; row < 3; row++){ for(int col = 0; col < 3; col++){ work[row][col] = move_X[row][col]; } } work[0][2] = 2*p; MULT = calc(work,MULT); }else{ for(int row = 0; row < 3; row++){ for(int col = 0; col < 3; col++){ work[row][col] = move_Y[row][col]; } } work[1][2] = 2*p; MULT = calc(work,MULT); } } for(int row = 0; row < 3; row++){ for(int col = 0; col < 3; col++){ table[i][row][col] = MULT[row][col]; } } } int num_Q; scanf("%d",&num_Q); int a,b; for(int loop = 0; loop < num_Q; loop++){ scanf("%d %d",&a,&b); b--; ll x = table[a][0][0]*loc[b].x + table[a][0][1]*loc[b].y + table[a][0][2]; ll y = table[a][1][0]*loc[b].x + table[a][1][1]*loc[b].y + table[a][1][2]; printf("%lld %lld\n",x,y); } return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define rep(i, n) FOR(i, 0, n - 1) using namespace std; typedef long long ll; template <class T> inline bool dpmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline bool dpmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } void solve(long long N) { ll ans; if (1 <= N && N <= 100) { ans = 1; } else { for (ll c = 1; c <= 30; c++) { if (c * 100 + 1 <= N && N <= (c+1) * 100){ ans = c + 1; break; } } } cout << ans << endl; } // Generated by 2.3.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the // default template now. You can remove this line by using your custom template) int main() { long long N; std::scanf("%lld", &N); solve(N); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; using Graph = vector<vector<ll>>; #define all(x) (x).begin(), (x).end() //string の sort とかはこれ #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) const ll MOD = 1000000007; const ll INF = 1e18; //1*10^18 const double PI = 3.141592653589793238; //18桁 const ll MAX = 1e6; long long fac[MAX], finv[MAX], inv[MAX]; int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } //最大公約数 ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } //最小公倍数 ll powll(ll a, ll n) { //O(log n) ll res = 1; while (n > 0) { if (n & 1) //(n & 1)=(n % 2 !=0) { res = res * a; } a = a * a; n >>= 1; //(n & 1)=(n % 2 !=0) } return res; } ll modpow(ll a, ll n, ll mod) //O(log n) { ll res = 1; while (n > 0) { if (n & 1) //(n & 1)=(n % 2 !=0) { res = res * a % mod; } a = a * a % mod; n >>= 1; //(n>>=1)=(n/2) } return res; } int ReturnDigit(ll num) { string number = to_string(num); return number.length(); } // テーブルを作る前処理 void COMinit() //MODは素数じゃないとだよ!! { 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; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; int main() { // COMinit(); // srand((unsigned int)time(NULL));//--> rand() ll i, j, k, l, ans=0; ll n; cin >> n; if(n%100==0) { ans=n/100; }else { ans=n/100+1; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std ; int main(void) { ios::sync_with_stdio(false) ; cin.tie(0) ; long long N ; cin >> N ; unordered_set<long long> s ; for (long long i = 2 ; i * i <= N ; i++) { long long x = i ; while (x * i <= N) { s.insert(x * i) ; x *= i ; } } cout << N - (int) s.size() ; return 0 ; }
#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("fast-math") #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 deb(x) cout << #x << " is " << x << "\n" #define int long long #define MOD 1000000007LL #define PI acos(-1) template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using max_heap = priority_queue<T>; template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename... T> void read(T &...args) { ((cin >> args), ...); } template <typename... T> void write(T &&...args) { ((cout << args), ...); } template <typename T> void readContainer(T &t) { for (auto &e : t) { read(e); } } template <typename T> void writeContainer(T &t) { for (const auto &e : t) { write(e, " "); } write("\n"); } auto speedup = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }(); void solve(int tc) { int N; read(N); set<int> st; for (int i = 2; i <= (int)1e5; i++) { int x = i * i; while (x <= (int)1e10) { st.insert(x); x *= i; } } int ans = 0; for (const auto &ele : st) { if (ele <= N) { ans++; } } write(N - ans); } signed main() { int tc = 1; // read(tc); for (int curr = 1; curr <= tc; ++curr) { solve(curr); } return 0; }
#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<ctime> #include<map> #include<vector> #include<math.h> #include<stdio.h> #include<stack> #include<queue> #include<tuple> #include<cassert> #include<set> #include<bitset> #include<functional> #include <fstream> //#include<bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i, x) for(ll i = 0; i < x; i++) #define rep2(i, x) for(ll i = 1; i <= x; i++) #define all(a) (a).begin(),(a).end() using ll = long long; using ld = long double; using namespace std; const ll INF = 10000000000000000; const int intINF = 1000000000; const ll mod = 1000000007; const ll MOD = 998244353; const ld pi = 3.141592653589793238; bool isprime(int p) { if (p == 1) return false; for (int i = 2; i < p; i++) { if (p % i == 0) return false; } return true; } ll gcd(ll a, ll b) { if (a < b)swap(a, b); if (a % b == 0)return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll keta(ll n) { ll res = 0; while (n >= 1) { res += n % 10; n /= 10; } return res; } ll modpow(ll x, ll y) { ll res = 1; while (y) { if (y % 2) { res *= x; res %= mod; } x = x * x % mod; y /= 2; } return res; } ll nCk(ll n, ll k) { ll a = 1, b = 1; for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; } for (int h = 1; h <= k; h++) { b *= h; b %= mod; } return a * modpow(b, mod - 2) % mod; } //printf("%.10f\n", n); typedef pair <ll, ll> P; ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; struct edge { ll to, cost; }; struct status { ll cost; ll bango; bool operator<(const status& rhs) const { return cost < rhs.cost; }; bool operator>(const status& rhs) const { return cost > rhs.cost; }; }; ll n, m; vector<string> ansG(5000); vector<ll> c(101); vector<vector<P>> G1(101), G2(101); vector<bool> henvisit(5000, false); vector<bool> visit(101, false); void dfs(ll x) { //cout << x << endl; ll ko = c[x], to = 0; for (int i = 0; i < G1[x].size(); i++) { ll next = G1[x][i].first, ban = G1[x][i].second; if (c[next] == ko && !visit[next] && !henvisit[ban]) { visit[next] = true; henvisit[ban] = true; ansG[ban] = "->"; dfs(next); //cout << G1[q][i].second << endl; } if (c[next] == ko && visit[next] && !henvisit[ban]) { henvisit[ban] = true; ansG[ban] = "->"; } } for (int i = 0; i < G2[x].size(); i++) { ll next = G2[x][i].first, ban = G2[x][i].second; if (c[next] == ko && !visit[next] && !henvisit[ban]) { //cout << G2[q][i].second << endl; visit[next] = true; henvisit[ban] = true; ansG[ban] = "<-"; dfs(next); } if (c[next] == ko && visit[next] && !henvisit[ban]) { henvisit[ban] = true; ansG[ban] = "<-"; } } } signed main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); //cout << fixed << setprecision(15); //input cin >> n >> m; vector<ll> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; G1[a[i]].push_back(P(b[i], i)); G2[b[i]].push_back(P(a[i], i)); } rep2(i, n) { cin >> c[i]; } //dfs rep(i, m) { if (henvisit[i]) { continue; } if (c[a[i]] > c[b[i]]) { ansG[i] = "->"; henvisit[i] = true; } else if (c[a[i]] < c[b[i]]) { ansG[i] = "<-"; henvisit[i] = true; } else if (visit[a[i]] && visit[b[i]]) { ansG[i] = "->"; } else { dfs(a[i]); } } //output rep(i, m) { cout << ansG[i] << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define LL long long #define pa pair<int,int> const int Maxn=110; const int inf=2147483647; const double pi=acos(-1.0); int read() { int 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*10+ch-'0',ch=getchar(); return x*f; } int n,m,c[Maxn],ans[Maxn*Maxn],fa[Maxn],dep[Maxn],ex[Maxn*Maxn],ey[Maxn*Maxn],px,py,o,d[Maxn]; vector<pa>G[Maxn]; void dfs(int x,int f,int u) { dep[x]=dep[f]+1;fa[x]=f;d[x]=u; for(int i=0;i<G[x].size();i++) { int t=G[x][i].second; if(t==u)continue; int y=G[x][i].first; if(!dep[y])ans[t]=(x==ey[t]),dfs(y,x,t); else { if(dep[y]<dep[x])ans[t]=(x==ey[t]); else ans[t]=(y==ey[t]); } } } int main() { n=read(),m=read(); for(int i=1;i<=m;i++)ex[i]=read(),ey[i]=read(); for(int i=1;i<=n;i++)c[i]=read(); for(int i=1;i<=m;i++) { int x=ex[i],y=ey[i]; if(c[x]==c[y])G[x].push_back(make_pair(y,i)),G[y].push_back(make_pair(x,i)); else { if(c[x]>c[y])ans[i]=0; else ans[i]=1; } } dep[0]=0; for(int i=1;i<=n;i++) if(!dep[i]) { py=-1; dfs(i,0,0); } for(int i=1;i<=m;i++) { if(!ans[i])puts("->"); else puts("<-"); } }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define ss second #define ff first #define all(x) x.begin(), x.end() #define INF 3000000000000001 // 3e15 #define ll_max 9000000000000000000 #define PI 3.14159265358979323846L #define fill(a, n, x) for(int i = 0; i < n; i++) a[i] = x; #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) using namespace std; using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long int ll; typedef long double ld; typedef pair<ll,ll> pll; const ll mod = 998244353; const ll N = 100110; const ll M = log2(N) + 1; struct point { ll x, y; }; template<typename T> T fast_exp(T x,T n) { if(n == 0) return 1; T ans = 1; if(n % 2 == 0) { ans = fast_exp(x, n / 2); ans = ans * ans % mod; } else ans = fast_exp(x, n - 1) * x % mod; return ans; } void BITfunc(ll &a, ll &x) { a += x; } void update(ll BIT[], ll i, ll x, ll n) { if(i <= 0) return; for(; i <= n; i += i & (-i)) BITfunc(BIT[i], x); } ll Query(ll BIT[], ll x) { ll res = 0; while(x > 0) { BITfunc(res, BIT[x]); x = x & (x - 1); } return res; } ll query(ll BIT[], ll l, ll r) { l--; ll sum = 0; while(r > 0) { sum += BIT[r]; r = r & (r - 1); } while(l > 0) { sum -= BIT[l]; l = l & (l - 1); } return sum; } ll fact[N], infact[N]; void work() { fact[0] = fact[1] = infact[0] = infact[1] = 1; for(ll i = 2; i < N; i++) { fact[i] = fact[i - 1] * i % mod; infact[i] = fast_exp(fact[i], mod - 2); } } ll ncr(ll n, ll r) { if(r > n || r < 0) return 0; return fact[n] * infact[n - r] % mod * infact[r] % mod; } ll npr(ll n, ll r) { if(r > n || r < 0) return 0; return fact[n] * infact[n - r] % mod; } int main() { fast; int _ = 1, test = 0; //cin >> _; while(_--) { test++; ll n, m, x, q = 0, y = 0, z = 0, ans = 0, k = 0, mx = 0; cin >> x >> y; for(int i = x; i < y; i++) { k = i; for(ll j = 1; j * j <= k; j++) { if(k % j == 0) { if(k + j <= y) ans = max(ans, j); if(k + k / j <= y) ans = max(ans, k / j); } } } cout << ans << "\n"; } return 0; }
#include<iostream> #include <algorithm> using namespace std; int main(){ float A,B; float ans=0; cin>>A>>B; ans=(A-B)*100/A; cout<<ans; }
//================code===================// //#define TLE #ifdef TLE #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #include <random> #include <ctime> #define ci(t) cin>>t #define co(t) cout<<t #define LL long long #define ld long double #define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i) #define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i) #define setp pair<pair<int,int>,int> #define setl pair<LL,LL> #define M_PI 3.14159265358979323846 #define micro 0.000001 using namespace std; #ifdef OHSOLUTION #define ce(t) cerr<<t #define AT cerr << "\n=================ANS=================\n" #define AE cerr << "\n=====================================\n" LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; } LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); } #else #define AT #define AE #define ce(t) #define __popcnt __builtin_popcount #define gcd __gcd #define lcm __lcm #endif pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,1},{1,0}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; } template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; } struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } }; struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } }; const int max_v = 2e5 + 7; const int INF = 1e9 + 7; const LL LNF = (LL)5e18 + 7ll; const LL mod = 998244353; LL cpow(LL x, LL k) { if (!x) return 0; LL ret = 1; while (k) { if (k & 1) ret = ret * x % mod; x = x * x % mod; k >>= 1; } return ret; } int main() { #ifdef OHSOLUTION freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); LL n, m, k; ci(n >> m >> k); LL ans = 0; if (n == 1 || m == 1) { co(cpow(k, n + m - 1)); return 0; } if (n == 1) swap(n, m); fa(i, 1, k + 1) { ans = (ans + cpow(i, n) * ((cpow(k - i + 1, m) - cpow(k - i, m) + mod) % mod) % mod) % mod; } co(ans%mod); return 0; }
#include<bits/stdc++.h> #define maxn 200005 #define mod 998244353 using namespace std; long long n,m,K; long long pw(long long A,long long n) { long long ans=1; while (n) { if (n&1) ans=ans*A%mod; n=n>>1; A=A*A%mod; } return ans; } int main() { long long ans=0; cin>>n>>m>>K; if (n==1) {cout<<pw(K,m)<<endl; return 0;} if (m==1) {cout<<pw(K,n)<<endl; return 0;} for (long long zd=1;zd<=K;zd++) { long long tt=pw(zd,n),gg=pw(zd-1,n); tt-=gg; while (tt<0) tt+=mod; ans+=tt*(pw(K-zd+1,m)); ans%=mod; } cout<<ans<<endl; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; class E { public: void solve(std::istream& cin, std::ostream& cout) { int R, C; cin >> R >> C; vector<vector<int>> A(R, vector<int>(C - 1)); vector<vector<int>> B(R - 1, vector<int>(C)); REP(i, R) REP(j, C - 1) cin >> A[i][j]; REP(i, R - 1) REP(j, C) cin >> B[i][j]; vector<int> dist(R * C * 2, INF); dist[0] = 0; priority_queue<P, vector<P>, greater<>> pq; pq.push({0, 0}); while (!pq.empty()) { int cost, v; tie(cost, v) = pq.top(); pq.pop(); //cout << v << ":" << cost << endl; int x = v % R; int y = v / R; if (v >= R * C) { if (x - 1 >= 0) { int nv = R * y + x - 1; if (cost + 1 < dist[nv]) { dist[nv] = cost; pq.push({cost + 1, nv}); } } int nv = v - R * C; if (cost < dist[nv]) { dist[nv] = cost; pq.push({cost, nv}); } } else { int nv = v + R * C; if (cost + 1 < dist[nv]) { dist[nv] = cost + 1; pq.push({cost + 1, nv}); } if (y + 1 <= C - 1) { nv = R * (y + 1) + x; if (cost + A[x][y] < dist[nv]) { dist[nv] = cost + A[x][y]; pq.push({cost + A[x][y], nv}); } } if (y - 1 >= 0) { nv = R * (y - 1) + x; if (cost + A[x][y - 1] < dist[nv]) { dist[nv] = cost + A[x][y - 1]; pq.push({cost + A[x][y - 1], nv}); } } if (x + 1 <= R - 1) { nv = R * y + x + 1; if (cost + B[x][y] < dist[nv]) { dist[nv] = cost + B[x][y]; pq.push({cost + B[x][y], nv}); } } } } cout << dist[R * C - 1] << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream& in(std::cin); std::ostream& out(std::cout); E solver; solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vec = vector<ll>; using mat = vector<vec>; using pll = pair<ll,ll>; #define INF (1LL << 60) #define MOD 1000000007 #define PI 3.14159265358979323846 #define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i)) #define FORR(i,v) for(auto (i):v) #define ALL(x) (x).begin(), (x).end() #define PR(x) cout << (x) << endl #define PS(x) cout << (x) << " " #define SZ(x) ((ll)(x).size()) #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) #define REV(x) reverse(ALL((x))) #define ASC(x) sort(ALL((x))) #define DESC(x) ASC((x)); REV((x)) #define pb push_back #define eb emplace_back int main() { ll A, B, C; cin >> A >> B >> C; ll s = 0; if(C % 2 == 1) { if(A < B) s = -1; else if(A > B) s = 1; else s = 0; } else { if(A*A < B*B) s = -1; else if(A*A > B*B) s = 1; else s = 0; } if(s > 0) PR(">"); else if(s < 0) PR("<"); else PR("="); return 0; } /* */
#include<iostream> #include<string> using namespace std; int main(){ int n; cin >> n; string s,t; cin >> s; cin >> t; int zero=0,anszero=0; int iti[n],ansiti[n]; int count=0,count2=0; for(int i=0;i<n;i++){ if(s[i]=='0'){ zero++; iti[count]=i; count++; } if(t[i]=='0'){ anszero++; ansiti[count2]=i; count2++; } } if(zero!=anszero){ cout << "-1" << endl; exit(0); } else{ int count3=0; for(int k=0;k<count;k++) if(iti[k]!=ansiti[k]){ count3++; } cout << count3 << endl; return 0; } }
#include "bits/stdc++.h" #include <random> #include <chrono> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;++i) #define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;--i) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef long double ld; typedef pair<lint, lint> plint; typedef pair<ld, ld> pld; struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; template<class T> auto add = [](T a, T b) -> T { return a + b; }; template<class T> auto f_max = [](T a, T b) -> T { return max(a, b); }; template<class T> auto f_min = [](T a, T b) -> T { return min(a, b); }; template<class T> using V = vector<T>; using Vl = V<lint>; using VVl = V<Vl>; template< typename T > ostream& operator<<(ostream& os, const vector< T >& v) { for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; } template< typename T >istream& operator>>(istream& is, vector< T >& v) { for (T& in : v) is >> in; return is; } template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template <class F> struct rec { F f; rec(F&& f_) : f(std::forward<F>(f_)) {} template <class... Args> auto operator()(Args &&... args) const { return f(*this, std::forward<Args>(args)...); } }; lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); } lint digit(lint a) { return (lint)log10(a); } lint e_dist(plint a, plint b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); } lint m_dist(plint a, plint b) { return abs(a.first - b.first) + abs(a.second - b.second); } bool check_overflow(lint a, lint b, lint limit) { if (b == 0) return false; return a > limit / b; } // a * b > c => true void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); } const lint MOD1000000007 = 1000000007, MOD998244353 = 998244353, INF = 1e18; lint dx[8] = { 0, -1, 0, 1, 1, -1, 1, -1 }, dy[8] = { -1, 0, 1, 0, -1, -1, 1, 1 }; bool YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; return flag; } struct Edge { lint from, to; lint cost, color; Edge() { } Edge(lint u, lint v, lint c, lint col) { cost = c; color = col; from = u; to = v; } bool operator<(const Edge& e) const { return cost < e.cost; } }; struct WeightedEdge { lint to; lint cost; WeightedEdge(lint v, lint c) { to = v; cost = c; } bool operator<(const WeightedEdge& e) const { return cost < e.cost; } }; using WeightedGraph = V<V<WeightedEdge>>; typedef pair<plint, lint> tlint; typedef pair<plint, plint> qlint; typedef pair<lint, string> valstr; typedef pair<ld, plint> _pld; typedef pair<ld, lint> pidx; int main() { lint N; cin >> N; string S, T; cin >> S >> T; VVl pos(2); REP(i, N) { if (S[i] == '0') pos[0].push_back(i); if (T[i] == '0') pos[1].push_back(i); } lint ans = SZ(pos[0]); if (SZ(pos[0]) != SZ(pos[1])) { cout << -1 << endk; } else { REP(i, SZ(pos[0])) { if (pos[0][i] == pos[1][i]) ans--; } cout << ans << endk; } }
#include <bits/stdc++.h> using namespace std; int main() { size_t N; cin >> N; vector<double> A(N); for (double &a : A) { cin >> a; } double l = 0.0; double r = 2000000000.0; auto f = [&](double c) -> double { double ret = 0.0; for (double a : A) { ret += c + a - min(a, 2 * c); } return ret; }; for (size_t i = 0; i < 5000; i++) { double c1 = (l * 2.0 + r) / 3.0; double c2 = (l + r * 2.0) / 3.0; if (f(c1) < f(c2)) { r = c2; } else { l = c1; } } cout << fixed << setprecision(12) << f(l) / double(N) << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 2010; int n, a[N]; map <int, vector <int>> cand; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { for (int j = 1; j * j <= a[i]; ++j) { if (a[i] % j) continue; cand[j].emplace_back(i); if (j * j < a[i]) cand[a[i] / j].emplace_back(i); } } int ans = 0; for (auto [key, val] : cand) { if (key > a[1]) break; int cur = 0; for (int i : val) cur = __gcd(cur, a[i]); if (cur != key) continue; ++ans; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++) #define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<ll, pi> ipi; typedef pair<pi, pi> pipi; #define mp make_pair #define f first #define s second typedef vector<ll> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define size(v) (ll) v.size() #define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin()); #define memset(x, v) memset(x, v, sizeof x) #define INF (ll) (1e9 + 100) #define LLINF (ll) 1e18 #define mod (const ll) (1e9 + 7) #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge"))) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng) inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusive ll n, a, b, c, ans = LLINF; int main() { fastio; cin >> n; FOR(i, 1, n) { cin >> a >> b >> c; if (c > a) ans = min(ans, b); } cout << (ans == LLINF ? -1 : ans); }
#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <cstdio> #include <stack> #include <cstring> //#define FILE_INPUT using namespace std; #define inc(i, n) for (int i = 0; i < (n); i++) #define incs(i, st, n) for (int i = (st); i < (n); i++) using ll = long long; constexpr int LIMIT = 0x3f3f3f3f; template <typename T> ll inv(T bg, T nd) { if (nd - bg <= 1) return 0; T md = bg + (nd - bg) / 2; ll ret = inv(bg, md) + inv(md, nd); auto i = bg, j = md; while (i != md) { while (j != nd && *i > *j) { j++; } ret += j - md; i++; } inplace_merge(bg, md, nd); return ret; } int main(int argc, char** argv){ #ifdef FILE_INPUT freopen("input", "r", stdin); #endif int N; scanf("%d", &N); vector<int> a(N, 0); inc(i, N) { scanf("%d", &a[i]); } // calc inv number vector<int> b(a.begin(), a.end()); ll inv_num = inv(b.begin(), b.end()); printf("%lld\n", inv_num); inc(i, N-1) { inv_num = inv_num + N - a[i]*2 -1LL; printf("%lld\n", inv_num); } #ifdef FILE_INPUT fclose(stdin); #endif return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; string S; cin >> N >> S; vector<int> A(N + 1); for (int i = 0; i <= N; i++) cin >> A.at(i); int k = 11000; for (int i = 0; i < N; i++) k = min(k, abs(A.at(i + 1) - A.at(i))); cout << k << endl; vector<vector<int>> B(k, vector<int>(N + 1)); for (int i = 0; i < k; i++) { for (int j = 0; j <= N; j++) { B.at(i).at(j) = A.at(j) / k; if (i < A.at(j) % k) B.at(i).at(j)++; cout << B.at(i).at(j); if (j == N) cout << endl; else cout << " "; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef pair <int, int> pin; const int N = 1005; const ll P = 998244353LL; int n, m, ufs[N << 1]; bool vis[N << 1]; char s[N][N]; inline int id(int x, int y) { return (x - 1) * m + y; } int find(int x) { return ufs[x] == x ? x : ufs[x] = find(ufs[x]); } inline void merge(int x, int y) { int fx = find(x), fy = find(y); if (fx == fy) return; ufs[fx] = fy; } template <typename T> inline void read(T &X) { char ch = 0; T op = 1; for (X = 0; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') op = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) X = (X * 10) + ch - '0'; X *= op; } int main() { #ifndef ONLINE_JUDGE freopen("sample.in", "r", stdin); clock_t st_clock = clock(); #endif read(n), read(m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); for (int i = 1; i <= n + m; i++) ufs[i] = i; s[1][1] = s[1][m] = s[n][1] = s[n][m] = '#'; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (s[i][j] == '#') merge(i, j + n); int ans = 0; for (int i = 1; i <= n; i++) { int now = find(i); if (vis[now]) continue; vis[now] = 1; ++ans; } int tmp = 0; memset(vis, 0, sizeof(vis)); for (int i = n + 1; i <= n + m; i++) { int now = find(i); if (vis[now]) continue; vis[now] = 1; ++tmp; } ans = min(ans, tmp); --ans; printf("%d\n", ans); #ifndef ONLINE_JUDGE clock_t ed_clock = clock(); printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000); #endif return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define ppb pop_back #define endl "\n" #define vll vector<ll> #define pll pair<ll,ll> #define pqb priority_queue<ll> #define pqs priority_queue<ll,vll,greater<ll> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define sz(x) (int)((x).size()) #define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL) #define fo(i,s,n) for(ll i=s;i<n;i++) #define rfo(i,n,s) for(ll i=n;i>=s;i--) using namespace std; typedef long long int ll; //------------------------------MOD-Operations--------------------------------------------------------------------------------------------------------------------------------------------// const ll M = 1e9 + 7; const ll N = 2e5 + 5; const ll inf = 9e18; ll mod(ll x){ return (x%M);} ll ml(ll a,ll b){ return mod(mod(a)*mod(b));} ll power(ll a,ll n){ if(n==0) return 1; else if(n==1) return a; ll R=power(a,n/2)%M; if(n%2==0) { return mod(ml(R,R)); } else { return mod(ml(ml(R,a),mod(R))); } } ll mod_inv(ll n){ return power(n,M-2);} // only if M is prime ll mod_div(ll a,ll b){ return ml(a,mod_inv(b)); } ll C(ll n, ll r) { if(r > n || n < 0 || r < 0)return 0; if (r == 0) return 1; ll nfc=1,rfc=1,nrfc=1; for (int i = 1; i <= n; i++) { nfc=ml(nfc,i); if(i==r){rfc=nfc;} if(i==(n-r)){nrfc=nfc;} } return ml(ml(nfc,mod_inv(rfc)),mod_inv(nrfc)); } ll ncr(ll n, ll 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; } vector<ll> seive(ll n){ vector<ll> isprime(n+1,true); isprime[0]=false; isprime[1]=false;//kyuki 1 prime nhi hota for( ll i=2;i<sqrt(n);i++){ if(isprime[i]){ for(int j=i*i;j<=n;j+=i){ isprime[j]=false; } } } vll allp; fo(i,2,n+1)if(isprime[i])allp.pb(i); return allp; } //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- vll vis(N); vector<vll> adj(N); void solve() { } int main(){ FAST; ll h,w; cin>>h>>w; vector<string> v(h); fo(i,0,h)cin>>v[i]; ll ans=0; fo(i,1,h-1){ fo(j,1,w-1){ if(v[i][j]=='.'){ if(v[i-1][j-1]=='#' && v[i-1][j]=='#' && v[i][j-1]=='#')ans++; if(v[i+1][j+1]=='#' && v[i+1][j]=='#' && v[i][j+1]=='#')ans++; if(v[i-1][j+1]=='#' && v[i][j+1]=='#' && v[i-1][j]=='#')ans++; if(v[i][j-1]=='#' && v[i+1][j]=='#' && v[i+1][j-1]=='#')ans++; } else { if(v[i-1][j-1]=='.' && v[i-1][j]=='.' && v[i][j-1]=='.')ans++; if(v[i+1][j+1]=='.' && v[i+1][j]=='.' && v[i][j+1]=='.')ans++; if(v[i-1][j+1]=='.' && v[i][j+1]=='.' && v[i-1][j]=='.')ans++; if(v[i][j-1]=='.' && v[i+1][j]=='.' && v[i+1][j-1]=='.')ans++; } } } cout<<ans<<endl; }
/*********************************************************************************\ * _________ _ _ ________ _ _ __ ________ * * |___ ___|| | | || ______|| | | | / \ | ____ | * * | | | | | || |______ | |_____| | / /\ \ | |____| | * * | | | | | ||______ || _____ | / /__\ \ | __ __| * * | | | |____| | ______| || | | | / ______ \ | | \ \ * * |_| |________||________||_| |_| /_/ \_\ |_| \_\ * * * * Department of Computer Science & Engineering * * Student ID : 18CSE035 * * Bangabnadhu Sheikh Mujibur Rahman Science & Technology University,Gopalgonj. * * * \*********************************************************************************/ //Now Write to Code ___________________________ #include <bits/stdc++.h> using namespace std ; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vector<int>> vvi; inline int Int(){int x; cin >> x; return x;} inline ll Long(){ll x; cin >> x; return x;} inline float Float(){float x; cin >> x; return x;} inline double Double(){double x; cin >> x; return x;} inline void Yes(){cout << "Yes" << endl;} inline void No(){cout << "No" << endl;} inline void YES(){cout << "YES" << endl;} inline void NO(){cout << "NO" << endl;} const int N =(int)2e5 + 5; const int maxN =(int)1e6 + 6; const ll Mod =(ll)1e9 + 7; const int inf =(int)2e9; const ll Inf =(ll)1e18; #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL) #define T int t,q; cin >> t; for(q=1;q<=t;q++) #define Forn(i,n) for(int i=0;i<n;i++) #define ForN(i,n) int i;for(i=n-1;n>=0;i--) #define forn(i,n) for(int i=1;i<=n;i++) #define forN(i,n) int i;for(i=n;n>=1;i--) #define fors(i,s) for(int i=0;i<s.size();i++) #define Sort(s) sort(s.begin(),s.end()) #define debug(x) cerr << #x << " = " << x << '\n' ; #define rep(i,b,e) for(__typeof(e) i=(b); i!=(e+1)-2*(b>e); i=i+1-2*(b>e)) #define gcd(a,b) __gcd(a , b) #define lcm(a,b) (a*(b/__gcd(a,b))) #define gt greater<int>() #define Int Int() #define Long Long() #define Float Float() #define Double Double() #define all(x) x.begin() , x.end() #define sz(x) (int)x.size() #define ff first #define ss second #define pb push_back #define eb emplace_back #define mp make_pair #define mem(a) memset(a,0,sizeof a) #define memn(a) memset(a,-1,sizeof a) #define biday return 0 #define nl endl #define sp(n) fixed<<setprecision(n) #define pi acos(-1) #define PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342 int main() { ll h,w,i,j,x,res=0; cin >> h >> w; char s[h][w]; for(i=0; i<h; i++) { for(j=0; j<w; j++) { cin >> s[i][j]; } } for(i=0; i<h-1; i++) { for(j=0; j<w-1; j++) { x=0; if(s[i][j]=='#')x++; if(s[i][j+1]=='#')x++; if(s[i+1][j]=='#')x++; if(s[i+1][j+1]=='#')x++; if(x%2)res++; } } cout << res << endl; biday; } //...............BYE BYE................
#include <bits/stdc++.h> #define REP(i,n) for (int i = 0; i <(n); ++i) #define DREP(i,n) for (int i = (n-1); i >=0; --i) #define REP2(i,x,n) for (int i = x; i <(n); ++i) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() using namespace std; using ll = long long; using P = pair<int,int>; static const double PI = acos(-1); static const int INF = 1e9+7; //debug #ifdef _DEBUG #define debug(var) do{cout << #var << " :";view(var);}while(0) #else #define debug(...) #endif template<typename T> void view(T e){cout << e << endl;} template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;} template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } } template<typename T1, typename T2> void view(pair<T1,T2> e) { cout << "("<< e.first << ", " << e.second <<")" << endl;} template<typename T1, typename T2> void view(const vector<pair<T1,T2>>& v) {for(const auto& e: v){view(e);}} // end debug int main(){ //#define int long long int n, s, d; cin >> n >> s >> d; vector<int> x(n); vector<int> y(n); REP(i,n) cin >> x[i] >> y[i]; REP(i,n){ bool ok = true; if(x[i] >= s) ok = false; if(y[i] <= d) ok = false; if(ok){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) v.begin(), v.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;} using pint = pair<int, int>; const int MAX = 10000; int n; vector<vector<int>> xyr; bool flgs[MAX][MAX]; vector<vector<int>> res; void paintOut(int tl_x, int tl_y, int br_x, int br_y) { for (int i = tl_y; i <= br_y; ++i) { for (int j = tl_x; j <= br_x; ++j) { flgs[i][j] = true; } } } int area(int tl_x, int tl_y, int br_x, int br_y) { return (br_x - tl_x + 1) * (br_y - tl_y + 1); } vector<int> topLeft(int sx, int sy, int r) { bool left_f = true, top_f = true; int now_row = sy, now_col = sx; while (left_f || top_f) { if (now_col == 0) left_f = false; if (area(now_col-1, now_row, sx, sy) > r) { left_f = false; } if (left_f) { for (int i = now_row; i <= sy; ++i) { if (flgs[i][now_col-1]) { left_f = false; break; } } if (left_f) now_col--; } if (now_row == 0) top_f = false; if (area(now_col, now_row-1, sx, sy) > r) { top_f = false; } if (top_f) { for (int i = now_col; i <= sx; ++i) { if (flgs[now_row-1][i]) { top_f = false; break; } } if (top_f) now_row--; } } bool right_f = true, bottom_f = true; int ty = now_row, tx = now_col; now_row = sy, now_col = sx; while (right_f || bottom_f) { if (now_col == MAX-1) right_f = false; if (area(tx, ty, now_col+1, now_row) > r) { right_f = false; } if (right_f) { for (int i = ty; i <= now_row; ++i) { if (flgs[i][now_col+1]) { right_f = false; break; } } if (right_f) now_col++; } if (now_row == MAX-1) bottom_f = false; if (area(tx, ty, now_col, now_row+1) > r) { bottom_f = false; } if (bottom_f) { for (int i = tx; i <= now_col; ++i) { if (flgs[now_row+1][i]) { bottom_f = false; break; } } if (bottom_f) now_row++; } } return {tx, ty, now_col, now_row}; } void solve() { rep(i, n) flgs[xyr[i][2]][xyr[i][1]] = true; sort(all(xyr), [](auto& a, auto& b){return a[1] + a[2] < b[1] + b[2];}); rep(i, n) { int x = xyr[i][1], y = xyr[i][2], r = xyr[i][3]; vector<int> top_left = topLeft(x, y, r); paintOut(top_left[0], top_left[1], top_left[2], top_left[3]); res[xyr[i][0]] = {top_left[0], top_left[1], top_left[2] + 1, top_left[3] + 1}; } rep(i, n) { rep(j, 4) { if (j) cout << " "; cout << res[i][j]; } cout << endl; } } int main() { cin >> n; xyr.resize(n, vector<int>(4)); rep(i, n) { xyr[i][0] = i; cin >> xyr[i][1] >> xyr[i][2] >> xyr[i][3]; } res.resize(n, vector<int>(3)); solve(); }