code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,tune=native") #define ll long long #define int ll #define ull unsigned ll #define ld long double #define rep(a) rep1(i,a) #define rep1(i,a) rep2(i,0,a) #define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++) #define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define pb push_back //#define inf 1010000000 #define inf 4000000000000000000 #define eps 1e-9 #define sz(a) ((int)a.size()) #define pow2(x) (1ll<<(x)) #define ceiling(a,b) (((a)+(b)-1)/(b)) #define print0(a) cout << (a) << ' ' #define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #ifdef i_am_noob #define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__) template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(T && x) {cerr << x << endl;} template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);} #else #define bug(...) 826 #endif template<typename T> void print(T && x) {cout << x << "\n";} template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);} const int Mod=1000000007,Mod2=998244353; const int MOD=Mod2; const int maxn=505; //i_am_noob #define wiwihorz int n,a[maxn]; vector<int> res; void op(int x){ if((x+sz(res))&1){ bug(x,sz(res)); } if(sz(res)==60604) bug(x); assert((x+1+sz(res))&1); res.pb(x+1); swap(a[x],a[x+1]); } void report(){ print(sz(res)); for(auto i: res) print0(i); cout << "\n"; } void orzck(){ res.clear(); cin >> n; rep(n) cin >> a[i]; rep(n) a[i]--; if(n<=3){ while(!is_sorted(a,a+n)){ if(sz(res)&1) op(1); else op(0); } report(); return; } rep3(i,n-1,4){ int pos; rep1(j,n) if(a[j]==i) pos=j; if((pos+sz(res))&1){ if(sz(res)&1) op(pos==2?3:1); else op(pos==1?2:0); } rep2(j,pos,i) op(j); } bug(sz(res)); rep(n) bug(a[i]); int pos; if(sz(res)&1) op(1); rep(4) if(a[i]==3) pos=i; if((pos+1+sz(res))&1){ rep2(i,pos,3) op(i); } else if(pos<3){ op(2); rep2(i,pos,3) op(i); } rep(n) bug(a[i]); while(!is_sorted(a,a+n)){ if(sz(res)&1) op(1); else op(0); } assert(is_sorted(a,a+n)); report(); } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); #ifdef i_am_noob freopen("input1.txt","r",stdin); freopen("output1.txt","w",stdout); freopen("output2.txt","w",stderr); #endif cout << fixed << setprecision(15); int t; #ifdef wiwihorz cin >> t; #else t=1; #endif while(t--) orzck(); return 0; }
#pragma GCC optimize(2) //#include <bits/stdc++.h> #include <iostream> #include <set> #include <cmath> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include <utility> #include <map> #define rush() int T; while(cin>>T) while(T--) #define ms(a,b) memset(a,b,sizeof a) #define lowbit(x) ((x)&(-x)) #define inf 0x7f7f7f7fll #define eps 1e-11 #define sd(a) scanf("%d",&a) #define sll(a) scanf("%lld",&a) #define sll2(a,b) scanf("%lld%lld",&a,&b) #define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c) #define sdd(a,b) scanf("%d%d",&a,&b) #define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define sf(a) scanf("%lf",&a) //#define sc(a) scanf("%c\n",&a) #define ss(a) scanf("%s",a) #define pd(a) printf("%d\n",a) #define pdd(a,b) printf("%d %d\n",a,b) #define pddd(a,b,c) printf("%d %d %d\n",a,b,c) #define pll(a) printf("%lld\n",a) #define pf(a) printf("%.1lf\n",a) #define pc(a) printf("%c",a) #define ps(a) printf("%s\n",a) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define forn(i,a,b,x) for(int i=(a);i<=(b);i+=(x)) #define per(i,a,b) for(int i=(b);i>=(a);i--) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define debug(a) cout<<#a<<" : "<<a<<" "<<" ; " #define dbg(a) cout<<#a<<" : "<<a<<endl #define show(a,b) cout<<a<<" :\t"<<b<<"\t*"<<endl #define IOS ios::sync_with_stdio(0); cin.tie(0) #define PAUSE system("pause") #define all(x) x.begin(),x.end() //#define print(a) cout<<a<<endl;continue; #define fr first #define sc second #define ceils(a,b) ((a-1ll+b)/(b)) #define floors(a,b) floor(double(a)/double(b)) using namespace std; //inline void swap(int &x,int &y){x^=y^=x^=y;} typedef long long ll; //using ui=unsigned int; typedef unsigned long long ull; typedef pair<int,int> pii; const double pi=acos(-1.0); const int dx[]={0,1,0,-1}; const int dy[]={1,0,-1,0}; const int limit=5e5; int read() { int s=0; char c=getchar(),lc='+'; while (c<'0'||'9'<c) lc=c,c=getchar(); while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar(); return lc=='-'?-s:s; } void write(int x) { if (x<0) putchar('-'),x=-x; if (x<10) putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } void print(int x,char c='\n') { write(x); putchar(c); } ll mod=2147483648; const int N=3e5+5; int n, m, _; int i, j, k; int a[N]; int pos[N]; vector<int> ans; signed main() { //IOS; rush(){ sd(n); rep(i, 1, n) sd(a[i]), pos[a[i]] = i; int opt = 1; rep(i, 1, n){ if(i == pos[i]) continue; if((pos[i] - 1) % 2 != opt){ if(pos[i] - 2 >= i){ ans.pb(pos[i] - 2); int x = pos[i] - 2, y = pos[i] - 1; swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); opt ^= 1; } else if(pos[i] == i + 1){ ans.pb(i - 1); ans.pb(i); ans.pb(i - 1); ans.pb(i); ans.pb(i - 1); //ans.pb(i); opt ^= 1; int x = pos[i], y = pos[i] - 1; swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); continue; } } for(int j = pos[i] - 1; j >= i; j--){ ans.pb(j); int x = j, y = j + 1; swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); opt ^= 1; } } pd(ans.size()); for(auto it : ans) printf("%d ", it); puts(""); ans.clear(); } //PAUSE; return 0; }
#pragma GCC optimize("Ofast", "unroll-loops") #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int N; string S; string X; void input(void){ cin >> N >> S >> X; } map<pair<int, int>, int> memo; bool canTWin(int i, int m){ if (i == N) return (m == 0); if (memo[{i, m}] > 0) return memo[{i, m}] == 1; char p = X[i]; int d = (int)(S[i] - '0'); if (p == 'T'){ bool ret = canTWin(i + 1, m * 10 % 7) || canTWin(i + 1, (m * 10 + d) % 7); memo[{i, m}] = ret ? 1 : 2; return ret; } bool ret = canTWin(i + 1, m * 10 % 7) && canTWin(i + 1, (m * 10 + d) % 7); memo[{i, m}] = ret ? 1 : 2; return ret; } int main(void){ input(); cout << (canTWin(0, 0) ? "Takahashi" : "Aoki") << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define mk make_pair #define fi first #define se second #define mset(a,b) memset(a, b, sizeof(a)) #define dbg(x) cout << "[" << #x << "]: " << x << endl; #define forn(i,n) for(int i=0; i < n;i++) #define forab(i,a,b) for(int i = a; i <= b; i++) #define forba(i,b,a) for(int i = b; i >= a; i--) #define each(val, v) for(auto val : v) #define abs(u) (u >= 0 ? u : -u) using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; const int MAXN = 2e5 + 5; const int INF = 0x3f3f3f3f; const int MOD = 998244353; int add(int a, int b){ a += b; if( a >= MOD) a -= MOD; if(a < 0) a += MOD; return a; } int mul(int a, int b){ int ret = (1LL * a * b)%MOD; return ret; } int pw(int a, int n){ int ret = 1; while(n > 0){ if(n&1 == 1){ ret = mul(ret, a); } a = mul(a,a); n = n>> 1; } return ret; } int inv(int a){ return pw(a,MOD-2); } int divide(int a, int b){ return mul(a, inv(b)); } void solve(){ int a,b,c; cin >> a >> b >> c; int soma_a = mul( a, divide(a+1,2) ); int soma_b = mul( b, divide(b+1,2) ); int soma_c = mul( c, divide(c+1,2) ); int ans = mul( soma_a, mul(soma_b, soma_c) ); cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define ff first #define ss second #define pb push_back #define pii pair <ll , ll> #define vi vector <ll> #define vii vector <pair <ll , ll>> #define mii map <ll , ll> #define mci map <char , ll> #define mcc map <char , char> #define pqb priority_queue <ll> #define pqs priority_queue <ll , vi,greater<ll> > #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define inf (ll) (9e18) #define sp(x , y) fixed << setprecision(y) << x #define mk(arr , n , type) type *arr = new type[n]; #define mar(arr , n , type) type arr[n]; #define w(x) ll x; cin >> x; while(x--) #define rep(i , j , n) for(ll i = j ; i < n ; i++) #define f(n) for(ll i = 0 ; i < n ; i++) #define all(p) p.begin() , p.end() #define ub(a , b) upper_bound(all(a) , b) #define lb(a , b) lower_bound(all(a) , b) #define in(arr , n) for(ll i = 0 ; i < n ; i++) cin >> arr[i]; #define out(arr , n) for(ll i = 0 ; i < n ; i++) cout << arr[i] << " "; cout << endl; #define d(x) cerr << #x << " = " << x << endl; #define len(x) x.length(); #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); int main() { ll n; cin >> n; mar(arr , n , ll); in(arr , n); ll ans = 0 , cnt = 0 , index = -1; for(ll i = 2 ; i <= 1000 ; i++) { cnt = 0; for(ll j = 0 ; j < n ; j++) { if(arr[j] % i == 0) { cnt ++; } } if(cnt >= ans) { ans = max(ans , cnt); index = i; } } cout << index << endl; return 0; }
#include <iostream> #include <iomanip> #include <algorithm> #include <bitset> #include <string> #include <cmath> #include <complex> #include <numeric> #include <cassert> #include <vector> #include <array> #include <map> #include <set> #include <stack> #include <queue> #include <deque> #include <utility> #define PI 3.14159265358979323846 #define int64 long long #define uint64 unsigned long long #define coutfix(i) cout << fixed << setprecision(i) //coutの浮動小数出力の桁数設定 using namespace std; int main() { int64 nn; cin >> nn; vector<vector<int64>> aa(nn + 1, vector<int64>(1ll << nn, 0ll)); for(int64 ii = 0;ii < (1ll << nn);ii++) { cin >> aa[0][ii]; } for(int64 ii = 0;ii < nn;ii++) { for(int64 jj = 0;jj < 1ll << nn;jj += (2ll << ii)) { int64 tmp = max(aa[ii][jj], aa[ii][jj + (1ll << ii)]); aa[ii + 1][jj] = tmp; } //cout << aa[ii + 1][0] << ' '; for(int64 jj = 1;jj < 1ll << nn;jj++) { if(aa[ii + 1][jj] == 0) { aa[ii + 1][jj] = aa[ii + 1][jj - 1]; } //cout << aa[ii + 1][jj] << ' '; } //cout << endl; } int64 memo = aa[nn][0]; if(aa[nn - 1][0] != memo) { memo = aa[nn - 1][0]; } else { memo = aa[nn - 1][(1ll << nn) - 1]; } for(int64 ii = 0;ii < 1ll << nn;ii++) { if(aa[0][ii] == memo) { cout << ii + 1 << endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;} template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;} template<typename T> vector<T> make_v(size_t a){return vector<T>(a);} template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts){ return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); } template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value!=0>::type fill_v(U &u,const V... v){u=U(v...);} template<typename T,typename U,typename... V> typename enable_if<is_same<T, U>::value==0>::type fill_v(U &u,const V... v){ for(auto &e:u) fill_v<T>(e,v...); } int main(void){ cin.tie(0); ios::sync_with_stdio(false); int64 N; cin >> N; auto S = make_v<int64>(2, N); REP(k, 2) REP(i, N) cin >> S[k][i]; priority_queue<int64> pq[2]; int64 cnt[2] = {}; int64 res = 0; REP(i, N) { res += max(S[0][i], S[1][i]); if (S[0][i] > S[1][i]) { cnt[i%2]++; pq[i%2].push(S[1][i] - S[0][i]); } else { pq[1 ^ (i % 2)].push(S[0][i] - S[1][i]); } } while (cnt[0] != cnt[1]) { if (cnt[1] < cnt[0]) { res += pq[0].top(); pq[0].pop(); cnt[1]++; } else { res += pq[1].top(); pq[1].pop(); cnt[0]++; } } cout << res << endl; }
#include<bits/stdc++.h> #define ll long long using namespace std; int main(){ ll N,sum=0; cin>>N; vector<ll> A(N),B(N); for(ll &i:A)cin>>i,sum+=i; for(ll &j:B)cin>>j; ll M=N/2; vector<ll> C(M),D(M); for(ll i=0;i<M;i++)C[i]=B[i*2]-A[i*2]; for(ll i=0;i<M;i++)D[i]=B[i*2+1]-A[i*2+1]; sort(C.rbegin(),C.rend()); sort(D.rbegin(),D.rend()); ll ans=sum; for(ll i=0;i<M;i++){ sum+=C[i]+D[i]; ans=max(ans,sum); } cout<<ans<<endl; }
// __________________ // | ________________ | // || ____ || // || /\ | || // || /__\ | || // || / \ |____ || // ||________________|| // |__________________| // \###################\ // \###################\ // \ ____ \ // \_______\___\_______\ // An AC a day keeps the doctor away. #ifdef local #include <bits/extc++.h> #define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n" #define debug(args...) qqbx(#args, args) using ost = std::ostream; #define DESTL(STL, BEG, END, OUT) \ template <typename ...T> ost& operator<<(ost &O, std::STL<T...> v) { int f=0; for(auto x: v) O << (f++ ? ", " : BEG) << OUT; return O << END; } DESTL(deque, "[", "]", x); DESTL(vector, "[", "]", x); DESTL(set, "{", "}", x); DESTL(multiset, "{", "}", x); DESTL(unordered_set, "{", "}", x); DESTL(map , "{", "}", x.first << ":" << x.second); DESTL(unordered_map , "{", "}", x.first << ":" << x.second); template <typename U, typename V> ost& operator<<(ost &O, std::pair<U,V> p) { return O << '(' << p.first << ',' << p.second << ')'; } template <typename T, size_t N> ost& operator<<(ost &O, std::array<T,N> a) { int f=0; for(T x: a) O << (f++ ? ", " : "[") << x; return O << "]"; } template <typename T, size_t ...I> ost& prtuple(ost &O, T t, std::index_sequence<I...>) { return (..., (O << (I ? ", " : "(") << std::get<I>(t))), O << ")"; } template <typename ...T> ost& operator<<(ost &O, std::tuple<T...> t) { return prtuple(O, t, std::make_index_sequence<sizeof...(T)>()); } template <typename ...T> void qqbx(const char *s, T ...args) { int cnt = sizeof...(T); (std::cerr << "\033[1;32m(" << s << ") = (" , ... , (std::cerr << args << (--cnt ? ", " : ")\033[0m\n"))); } #else #pragma GCC optimize("Ofast") #pragma loop_opt(on) #include <bits/extc++.h> #include <bits/stdc++.h> #define debug(...) ((void)0) #define safe ((void)0) #endif // local #define all(v) begin(v),end(v) #define get_pos(v,x) int(lower_bound(begin(v),end(v),x)-begin(v)) #define sort_uni(v) sort(begin(v),end(v)),v.erase(unique(begin(v),end(v)),end(v)) #define pb emplace_back #define ff first #define ss second #define mem(v,x) memset(v,x,sizeof v) using namespace std; using namespace __gnu_pbds; typedef int64_t ll; typedef long double ld; typedef pair<ll,ll> pll; typedef pair<ld,ld> pld; template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >; template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >; template <typename T> using rbt = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; constexpr ld PI = acos(-1), eps = 1e-7; constexpr ll N = 100025, INF = 1e18, MOD = 998244353, K = 19, inf = 1e7; constexpr inline ll cdiv(ll x, ll m) { return x/m + (x%m ? (x<0) ^ (m>0) : 0); } // ceiling divide constexpr inline ll modpow(ll e,ll p,ll m=MOD) { ll r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; } int dp[3001][3001][2]; int vis[3001][3001][2]; int DP(int n, int k, bool first) { if(k == 0 || n == 0) return n == 0 && k == 0; if(k > n) return 0; if(vis[n][k][first]) return dp[n][k][first]; vis[n][k][first] = true; int &ans = dp[n][k][first]; return ans = (DP(n-1, k-1, false) + DP(n, k*2, true)) % MOD; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, k; cin >> n >> k; cout << DP(n, k, true) << '\n'; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl using lint = long long; typedef pair<lint,lint> P; const bool debugFlag=true; const lint linf=1e18+7; const lint inf=1e9+7; const int MOD=1000000007; signed main(){ int n,m;cin>>n>>m; map<P,int> st; rep(i,m){ lint a,b;cin>>a>>b; st[{b,a}]=3; if(st.find({b-1,a-1})==st.end())st[{b-1,a-1}]=0; if(st.find({b,a-1})==st.end())st[{b,a-1}]=0; if(st.find({b+1,a-1})==st.end())st[{b+1,a-1}]=0; st[{b-1,a-1}]=max(st[{b-1,a-1}],1); st[{b,a-1}]=max(st[{b,a-1}],2); st[{b+1,a-1}]=max(st[{b+1,a-1}],1); } vector<int> status; int cur=0; st[{n,0}]=3; vector<vector<int>> edge(st.size()); int start=-1; for(auto& val:st){ status.push_back(val.second); val.second=cur++; } for(auto& [p,no]:st){ auto& [y,x]=p; if(y==n&&x==0)start=no; auto it=st.find({y+1,x+1}); if(it!=st.end()){ if(status[it->second]==3)edge[no].push_back(it->second); } it=st.find({y-1,x+1}); if(it!=st.end()){ if(status[it->second]==3)edge[no].push_back(it->second); } it=st.upper_bound({y,x}); if(it==st.end())continue; if(it->first.first==y&&it->first.second>x){ if(status[it->second]<=2)edge[no].push_back(it->second); } } queue<int> que; que.push(start); vector<int> ok(edge.size(),0); ok[start]=1; while(!que.empty()){ int buf=que.front();que.pop(); for(auto& e:edge[buf]){ if(ok[e]==0){ ok[e]=1; que.push(e); } } } set<int> res; for(auto& [p,no]:st){ if(ok[no]&&status[no]%2==1){ auto it=st.upper_bound(p); if(it==st.end()){ res.insert(p.first); } else if(it->first.first!=p.first){ res.insert(p.first); } } } cout<<res.size()<<"\n"; return 0; }
#ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG //checks for container overflows #pragma GCC optimize "trapv" //to chcek for int overflow #pragma -Wduplicated-cond //dupilcate conditions #endif #include "bits/stdc++.h" using namespace std; using namespace chrono; typedef unsigned long long int ull; typedef long long ll; #define F first // #define S second #define nl '\n' #define de1(x) cerr << #x << ":" << x <<nl; #define de2(x,y) cerr << #x << ":" << x << " " << #y << ":" << y << nl #define de3(x,y,z) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<nl #define de4(x,y,z,w) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<" | "<<#w<<": "<<w<<nl #define all(x) x.begin(), x.end() #define rt return 0; #define vvi vector<vector<int>> #define vi vector<int> #define vpi vector<pair<int,int>> #define fast ios_base::sync_with_stdio(0); cin.tie(0); #define pb push_back #define um unordered_map<int,int> const float pi = 3.14; const ull mod = 1ll << 32; void solve() { stack<char> s; int n; string str; cin >> n >> str; if (n <= 3 && str != "fox") { cout << n; return; } register int cnt = 0, i; for (i = 0; i < n; i++) { if (str[i] == 'x' && s.size() && s.top() == 'o') { s.pop(); if (s.size() && s.top() == 'f') { cnt += 3; s.pop(); continue; } s.push('o'); s.push('x'); } else s.push(str[i]); } cout << n - cnt; } int main() { #ifndef ONLINE_JUDGE auto start = high_resolution_clock::now(); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast solve(); #ifndef ONLINE_JUDGE auto end = high_resolution_clock::now(); double time_taken = duration_cast<nanoseconds>(end - start).count(); time_taken *= 1e-9; cerr << "Time : " << fixed << time_taken << setprecision(9) << nl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T> inline void chkmax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void chkmin(T &x, T y) { if (x > y) x = y; } inline int read() { #define nc getchar() int x = 0; char c = nc; while (c < 48) c = nc; while (c > 47) x = (x << 3) + (x << 1) + (c ^ 48), c = nc; return x; #undef nc } const int maxn = 1e6 + 10; char A[maxn], B[maxn]; int n, m, nxt[maxn], st[maxn], pos[maxn]; int main() { scanf("%d%s",&n, A + 1); B[1]='f',B[2]='o',B[m=3]='x'; for (int i = 2, j = 0; i <= m; i++) { while (j && B[i] != B[j + 1]) j = nxt[j]; nxt[i] = B[i] == B[j + 1] ? ++j : j; } int top = 0; for (int i = 1, j = 0; i <= n; i++) { while (j && A[i] != B[j + 1]) j = nxt[j]; j += A[i] == B[j + 1]; pos[i] = j, st[++top] = i; if (j == m) { top -= m, j = pos[st[top]]; } } printf("%d\n",top); // rep(i, 1, top) { // putchar(A[st[i]]); // } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <utility> #include <deque> #include <stack> #include <bitset> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<int,ll> pil; typedef pair<ll,int> pli; #define rep(i,n) for (int i=0;i<n;++i) #define REP(i,n) for (int i=1;i<=n;++i) #define all(x) x.begin(),x.end() #define mp make_pair #define pb push_back #define pf push_front #define F first #define S second #define RI(x) scanf("%d",&x) #define RII(x,y) scanf("%d%d",&x,&y) int cnt[10]; char s[200005]; int main(){ ios::sync_with_stdio(false); cin>>s+1; int len=strlen(s+1); for (int i=1;i<=len;++i) cnt[s[i]-'0']++; if (len>=3){ for (int x=0;x<1000;++x){ if (x%8) continue; int a=x%10,b=(x/10)%10,c=x/100; --cnt[a];--cnt[b];--cnt[c]; if (cnt[a]>=0&&cnt[b]>=0&&cnt[c]>=0){ cout<<"Yes"<<endl;return 0; } ++cnt[a];++cnt[b];++cnt[c]; } cout<<"No"; } else{ if (len==1){ if ((s[1]-'0')%8==0) cout<<"Yes"; else cout<<"No"; } else{ if (((s[1]-'0')*10+s[2]-'0')%8==0) cout<<"Yes"; else if (((s[2]-'0')*10+s[1]-'0')%8==0) cout<<"Yes"; else cout<<"No"; } } return 0; }
#include"iostream" #include"cstdio" #include"cmath" #include"cstring" #include"algorithm" #include"stack" #include"queue" using namespace std; #define read(x) scanf("%d",&x) #define readl(x) scanf("%lld",&x) #define ll long long #define ull unsigned long long #define MOD 998244353 #define MAXN 200005 char c[200005]; int cnt[15]={0}; int len=0; int main() { scanf("%s",c); int len=strlen(c); for(int i=0;i<len;i++) cnt[c[i]-'0']++; if(len==1&&c[0]=='8') return puts("Yes"),0; for(int i=2;i<=12;i++) { if(i==11) { if(cnt[8]>=2) { cnt[8]-=2; if(len==2) return puts("Yes"),0; for(int j=2;j<=8;j+=2) { if(cnt[j]) { puts("Yes"); return 0; } } cnt[8]+=2; } continue; } if(i==5||i==10) continue; int op=i*8; int q=op/10,w=op%10; if(cnt[q]&&cnt[w]) { if(len==2) return puts("Yes"),0; cnt[q]--,cnt[w]--; for(int j=2;j<=8;j+=2) { if(cnt[j]) { puts("Yes"); return 0; } } cnt[q]++,cnt[w]++; } } if(len<3) return puts("No"),0; for(int i=13;i<125;i++) { int op=i*8; int a=op/100,b=(op/10)%10,s=op%10; if(!a||!b||!c) continue; if(a==b&&b==s) { if(cnt[a]>=3) return puts("Yes"),0; } else if(a==b) { if(cnt[a]>=2&&cnt[s]) return puts("Yes"),0; } else if(b==s) { if(cnt[b]>=2&&cnt[a]) return puts("Yes"),0; } else if(a==s) { if(cnt[a]>=2&&cnt[b]) return puts("Yes"),0; } else if(cnt[a]&&cnt[b]&&cnt[s]) return puts("Yes"),0; } return puts("No"),0; }
#include<bits/stdc++.h> #define MAX 1000000007 #define MOD 998244353 #define ll long long int #define fo(i,a,n) for(int i=a;i<n;i++) #define ull unsigned long long int #define pb push_back #define eb emplace_back #define in insert #define cl clear #define ld long double #define mp make_pair #define ii pair<int,int> const ll inf = 10e16+7; using namespace std; typedef vector<vector<int>> vvi; void print(int arr[], int n){ fo(i,0,n){ cout<<arr[i]<<" "; } cout<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); // int t; // cin>>t; // while(t--){ // } int n; cin>>n; ll ans = 0; for(int i=1;i<=n;i++){ if(n-i>=1)ans++; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; cout << N-1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i)) #define REP(i, n) FOR(i,n,0) #define OF64 std::setprecision(40) const ll MOD = 1000000007; const ll INF = (ll) 1e15; ll modpow(ll n, ll r, ll mod) { ll ret = 1; ll p = n % mod; while (r > 0) { if (r & 1) ret = (ret * p) % mod; p = (p * p) % mod; r >>= 1LL; } return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, M; cin >> N >> M; ll ans = modpow(10, N, M * M) / M; cout << ans << endl; return 0; }
#define LOCAL #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2") #define IOS ios::sync_with_stdio(false);cin.tie(0) #define all(x) x.begin(), x.end() #define ff first #define ss second #define LLINF 0x3f3f3f3f3f3f3f3f #define INF 0x3f3f3f3f #define uniq(x) sort(all(x)); x.resize(unique(all(x))-x.begin()); #define sz(x) (int)x.size() #define pw(x) (1LL<<x) using pii = pair<int, int>; using ll = long long; const long double PI = acos(-1.0); ll MOD; ll add(ll x, ll y) { x+=y; x%=MOD; return x; } ll sub(ll x, ll y) { x-=y; x%=MOD; if(x<0) x+=MOD; return x; } ll mult(ll x, ll y) { return (x*y)%MOD; } ll bin_pow(ll x, ll p) { if(p==0) return 1; if(p&1) return mult(x,bin_pow(x,p-1)); return bin_pow(mult(x,x),p/2); } ll rev(ll x) { return bin_pow(x,MOD-2); } void solve() { ll n, m; cin >> n >> m; MOD = m*m; ll lft=bin_pow(10,n); lft /= m; cout << lft << "\n"; } int main() { IOS; int t; t=1; while(t--) solve(); }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define REP(i,n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define debug(var) do{cout << #var << " : "; view(var);}while(0) template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;} template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;} using namespace std; template<class T> void view(T e) {cout << e << endl;} template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;} template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}} using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int,int>; const int inf = 1<<30; const ll inf_l = 1LL<<61; const int MAX = 1e5; // union-find struct union_find { vector<int> dat; union_find(int size) {dat.resize(size, -1);} int find(int x) { if (dat[x] < 0) {return x;} return dat[x] = find(dat[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) {return false;} if (dat[x] > dat[y]) swap(x, y); dat[x] += dat[y]; dat[y] = x; return true; } int size(int x) {return -dat[find(x)];} vvint groups() { int n = dat.size(); vector<vector<int>> res(n); for (int i = 0; i < n; i++) { res[find(i)].push_back(i); } res.erase( remove_if(res.begin(), res.end(), [](const vint &v) {return v.empty();}), res.end()); return res; } }; int main() { int n; cin >> n; vint x(n), y(n); rep(i,n) cin >> x[i] >> y[i]; double ok = 0, ng = 200; while (ng - ok > 1e-10) { double mid = (ok + ng) / 2; union_find uf(n + 2); rep(i,n)rep(j,i) { double dis = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)); if (dis < 2 * mid) { uf.unite(i, j); } } rep(i,n) { if (y[i] - (-100) < 2 * mid) uf.unite(i, n); if (100 - y[i] < 2 * mid) uf.unite(i, n + 1); } if (uf.find(n) == uf.find(n + 1)) ng = mid; else ok = mid; } printf("%.10f\n", ok); }
#include<iostream> #include<algorithm> #include<cmath> #include<iomanip> using namespace std; int N_MAX=11000; typedef pair<int,int> P; typedef pair<int,P> PP; int main() { int n,i,j,k=0; PP b[N_MAX]; // 枝データ int c[N_MAX]; // 頂点iの属する集合番号 int nc[N_MAX]; // 集合jの要素数 int pc[N_MAX]; // 集合jの先頭の要素番号 int pi[N_MAX]; // 頂点iと同じ集合の要素へのポインタ int i1,i2,s1,s2,p,pt; int x[N_MAX],y[N_MAX]; cin >> n; for(i=1;i<=n;i++) cin >> x[i] >> y[i]; for(i=1;i<=n+2;i++){ c[i]=i; nc[i]=1; pc[i]=i; pi[i]=0; } for(i=1;i<=n;i++){ for(j=i+1;j<=n;j++,k++){ b[k].first=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]); b[k].second.first=i; b[k].second.second=j; } b[k].first=(y[i]-100)*(y[i]-100); b[k].second.first=i; b[k].second.second=n+1; k++; b[k].first=(y[i]+100)*(y[i]+100); b[k].second.first=i; b[k].second.second=n+2; k++; } b[k].first=200*200; b[k].second.first=n+1; b[k].second.second=n+2; k++; sort(b,b+k); for(i=0;i<k;i++){ i1=b[i].second.first; i2=b[i].second.second; if(c[i1]!=c[i2]){ // 集合のマージここから s1=c[i1]; s2=c[i2]; if(nc[s1]<nc[s2]){// 小さい方を潰す【重要】 s1=c[i2]; s2=c[i1]; } // res[i-1]=res[i]-nc[s1]*nc[s2]; pt=pc[s1]; p=pc[s2]; pc[s1]=p; nc[s1]+=nc[s2]; pc[s2]=0; nc[s2]=0; while(true){ c[p]=s1; if(pi[p]==0) break; p=pi[p]; } pi[p]=pt; // 集合のマージここまで } if(c[n+1]==c[n+2]){ cout << setprecision(8) << sqrt(b[i].first)/2 << endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, rs = 0; map<int, int> M; void add(int key, int value) { M[key] = __gcd(M[key], value); } int main() { #ifdef ACM freopen("input", "r", stdin); #endif cin >> n; int x, minV = 2e9; for (int i = 1; i <= n; i++) { cin >> x; minV = min(minV, x); for (int j = 1; j * j <= x; j++) if (x % j == 0) { add(j, x); if (x / j != j) add(x / j, x); } } for (auto tmp:M) { if (tmp.first > minV) break; rs += (tmp.first == tmp.second); } cout << rs; }
#include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef WA_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif using ll = long long; using ull = unsigned long long; #define pb push_back #define fi first #define se second #define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++) #define per(i,a,b) for(int i=int(a);i>=(int)(b);i--) const int mod = 1e9+7; const int inf = 0x3f3f3f3f; const int maxn = 1e5+10; int main() { #ifndef WA_DEBUG ios::sync_with_stdio(false);cin.tie(nullptr); #endif int n; cin>>n; vector<int> a(n+1); int Min=inf; rep(i,1,n) { cin>>a[i]; Min=min(a[i],Min); } set<int> s; rep(i,1,n) { rep(j,1,maxn) { if(j*j>a[i]) break; if(a[i]%j==0) { s.insert(j); if(j*j!=a[i]) s.insert(a[i]/j); } } } int cnt=0; for(int x:s) { if(x>Min) continue; int ans=-1; rep(i,1,n) { if(ans==1) break; if(a[i]%x==0) { if(ans==-1) ans=a[i]/x; else ans=__gcd(ans,a[i]/x); } } if(ans==1&&x<=Min) cnt++; } cout<<cnt<<'\n'; return 0; }
#include <iostream> using namespace std; int main() { int n,w; cin>>n>>w; cout<<n/w; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int a, b; cin >> a >> b; int mn = min(a, b), mx = max(a, b); vector<int> ans; ll s1 = 0, s2 = 0; for(int i = 0; i < mx; i++) { ans.push_back(i + 1); s1 += i + 1; } for(int i = 0; i < mn; i++) { ans.push_back(-(i + 1)); s2 += i + 1; } ll d = s1 - s2; ans[a+b-1] -= d; if(mn==a) { for(int i = 0; i < a + b; i++) ans[i] = ans[i] * -1; } for(int x : ans) cout << x << " "; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long typedef long long ll; long long t,n,m,k,ccnt,pos=0,sum=0,minn2=INT_MAX,sum2,cnt=0,cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0,cnt7=0,a[2000005],x1,y7,x2,y2,x3,y3,aaa,bbb; long long vis1[1005][1005],vis2[1005][1005],b[1000005],c[1000005],c3[2005][2005],c4[1000005],kk[1000005],x,y,z,d=0,l2,r2; long long pos1=0,pos2=0,tem,tem2,l,r,ans=0,ans2,mod=998244353,tw[100005],th[100005],dd,ee,out[1000005],te[100005]; long long minn=LONG_LONG_MAX,maxx=-LONG_LONG_MAX,maxx2; string s1,s2,s; char cc[2005][2005],ch; pair<int,int> p[200005]; vector<int> v[200005]; long long fpow(long long aa,long long bb) { if(bb==0) { return 1; } else if(bb<0) { return 0; } long long kk1=aa; long long kk2=1; while(bb>1) { if(bb%2) { kk2*=kk1; kk2%=mod; } kk1*=kk1; kk1%=mod; bb=bb/2; } return (kk1*kk2)%mod; } long long co(long long x,long long y) { long long ans=1; long long ans2=1; if(x==y||y==0) { return 1; } else { for(int i=x-y+1;i<=x;i++) { ans=ans*i; ans=ans%mod; } for(int i=1;i<=y;i++) { ans2=ans2*i; ans2=ans2%mod; } ans2=fpow(ans2,mod-2); ans2=(ans2*ans)%mod; return ans2; } } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin>>s; d=1; for(int i=0;i<s.size();i++) { if(i%2==0) { if(s[i]-'a'<0) { d=0; break; } } else { if(s[i]-'A'>=26) { d=0; break; } } } if(d) { cout<<"Yes"; } else { cout<<"No"; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p_ll; template<class T> void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; } #define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++) #define all(vec) vec.begin(), vec.end() #define rep(i,N) repr(i,0,N) #define per(i,N) for (ll i=(ll)N-1; i>=0; i--) const ll MOD = pow(10,9)+7; const ll LLINF = pow(2,61)-1; const ll INF = pow(2,30)-1; vector<ll> fac; void c_fac(ll x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; } ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; } ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; } ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; } ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); } ll lcm(ll a, ll b) { return a/gcd(a,b)*b; } int main() { ll T; cin >> T; rep(_,T) { ll N, A, B; cin >> N >> A >> B; if (A+B>N) { cout << 0 << endl; continue; } ll t1 = (N-A-B+1) * (N-A-B+2) / 2 % MOD; ll x1 = t1 * t1 % MOD, x2 = t1 * ((N-A+1)*(N-B+1)%MOD) % MOD; ll result = (x2-x1+MOD) * 4 % MOD; cout << result << endl; } return 0; }
#include <iostream> #include<cmath> #include<iomanip> using namespace std; typedef long long int LL; LL x[100000]; int main(void){ cout<<setprecision(16); LL n,i,j; LL ans; long double dans; cin>>n; for(i=0;i<n;i++)cin>>x[i]; ans=0; for(i=0;i<n;i++)ans+=abs(x[i]); cout<<ans<<endl; dans=0.0; for(i=0;i<n;i++)dans+=(double)(x[i]*x[i]); cout<<sqrt(dans)<<endl; ans=0; for(i=0;i<n;i++)if(ans<abs(x[i]))ans=abs(x[i]); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) //debug用出力関数 dout("n",n); void dout(string s, int n) { cout << s << ":" << n << endl; return; } int main() { int n,ans = 100000; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> c(n); rep(i,n){ cin >> a.at(i) >> b.at(i); } for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ if(i == j){ ans = min(ans,a.at(i) + b.at(j)); } else { ans = min(ans,max(a.at(i),b.at(j))); } } } cout << ans << endl; }
#include "bits/stdc++.h" #define FI first.first #define SE first.second #define TH second #define fi first #define se second #define th second using namespace std; template<typename T> string to_string(const vector<T>& vc, int w) { if(vc.empty()) return ""; if(w + 1 == vc.size()) return to_string(vc[w]); return to_string(vc[w]) + "," + to_string(vc, w + 1); } template<typename T> string to_string(const vector<T>& vc) { return "{" + to_string(vc, 0) + "}"; } 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 DEBUG #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif class DebugStream {}LOG; template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; } #ifdef DEBUG #define LOG clog #endif typedef long long ll; typedef pair<ll, ll> ii; typedef long double ld; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29; ll gcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return a; } ll x1, y1; ll d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int t; cin >> t; while(t--) { ll n, s, k, x, y; cin >> n >> s >> k; k %= n; ll gg = gcd(n, k, x, y); ll nm = n - s; if(y < 0) y = (n - abs(y) % n) % n; else y %= n; if(nm % gg) { cout << "-1\n"; continue; } nm /= gg; n /= gg; ll ans = nm * y % n; cout << ans << '\n'; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; pair <ll,ll> extgcd(ll a,ll b) { if (b==0) return {1,0}; pair<ll,ll> u=extgcd(b,a%b); return {u.second,u.first-a/b*u.second}; } ll gcd(ll x,ll y) { if(y==0) return x; return gcd(y,x%y); } void solve () { ll n,s,k; cin>>n>>s>>k; if (s%gcd(n,k)!=0) cout<<-1<<endl; else { ll e=gcd(n,k); n=n/e; s=s/e; k=k/e; pair<ll,ll> v=extgcd(k,n); cout<<((-v.first*s)%n+n)%n<<endl; } } int main() { int t; cin>>t ; while (t--) { solve (); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long n, q; cin >> n >> q; long a[n], k[q]; long b[n]; vector <pair <long, long>> v; vector <long> ans(q); for(int i = 0; i < n; i++){ cin >> a[i]; if(i == 0) b[i] = a[i] - 1; else b[i] = b[i - 1] + a[i] - a[i - 1] - 1; } for(int i = 0; i < q; i++){ cin >> k[i]; v.push_back(make_pair(k[i], i)); } sort(v.begin(), v.end()); long x, y, z, P, W; W = 0; for(int i = 0; i < q; i++){ P = 0; for(int j = W; j < n; j++){ if(v[i].first <= b[j]){ x = a[j]; y = b[j] - v[i].first; z = x - y - 1; P = 1; W = j; break; } } if(P == 1) ans[v[i].second] = z; else{ ans[v[i].second] = n + v[i].first; W = n; } } for(int i = 0; i < q; i++){ cout << ans[i] << endl; } }
#include<ctime> #include<cstdio> #include<cctype> #include<algorithm> using namespace std; const int N=50+5; int read() { char c; int x=0,f=1; while(!isdigit(c=getchar())) f-=2*(c=='-'); while (isdigit(c)){ x=x*10+(c-48)*f; c=getchar(); } return x; } struct bag{ int w,v; bag(int a=0,int b=0){ w=a; v=b; } bool operator <(bag b){ return v>b.v; } }; bag a[N]; int n,m,q,l,r,x[N],vis[N]; int main() { #ifndef ONLINE_JUDGE freopen("D.in","r",stdin); freopen("D.out","w",stdout); #endif clock_t t1=clock(); //-------- n=read(); m=read(); q=read(); for(int i=1;i<=n;++i){ int w=read(); int v=read(); a[i]=bag(w,v); } sort(a+1,a+n+1); for(int i=1;i<=m;++i) x[i]=read(); for(int i=1;i<=q;++i){ l=read(); r=read(); int cnt=0; int res=0; int sz=m-(r-l+1); for(int j=1;j<=m;++j) vis[j]=0; for(int j=1;j<=n;++j){ if(cnt==sz) break; int pos=0; for(int k=1;k<l;++k){ if(x[k]<a[j].w) continue; if(vis[k]) continue; if(!pos||x[k]<x[pos]) pos=k; } for(int k=m;k>r;--k){ if(x[k]<a[j].w) continue; if(vis[k]) continue; if(!pos||x[k]<x[pos]) pos=k; } if(pos){ ++cnt; vis[pos]=1; res+=a[j].v; } } printf("%d\n",res); } //-------- clock_t t2=clock(); fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n,s,d,x,y; cin>>n>>s>>d; int f=0; while(n--){ scanf("%lld%lld",&x,&y); if(x<s&&y>d) f++; } if(!f) printf("No\n"); else printf("Yes\n"); }
#include<bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define rep(i,n) for(int i=0; i<(n); i++) #define all(v) v.begin(),v.end() int main() { int x, y, z; cin >> x >> y >> z; int ans = 1000*1000; while (ans * x >= z * y) ans--; cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1e9 + 7; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; vector<int> a(n); vector<int> b(m); for(int &x:a) cin >> x; for(int &x:b) cin >> x; int h1, h2; h1 = h2 = 0; vector<int> res; while(h1 < n && h2 < m){ if (a[h1] == b[h2]) { h1++; h2++; } else if (a[h1] < b[h2]){ res.push_back(a[h1]); h1++; } else { res.push_back(b[h2]); h2++; } } while (h1 < n) res.push_back(a[h1++]); while (h2 < m) res.push_back(b[h2++]); for(int x:res) cout << x << ' '; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define _GLIBCXX_DEBUG typedef long long int ll; typedef long double ld; const int inf = 1e8; const ll llinf = 1e18; const double PI = acos(-1); #define yes "Yes" #define no "No" #define all(n) n.begin(), n.end() template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } struct UnionFind { vector<int> par, siz; UnionFind(int n) : par(n, -1), siz(n, 1) {} int root(int x) { if (par[x] == -1) return x; return par[x] = root(par[x]); } bool isSame(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x), y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); par[y] = x; siz[x] += siz[y]; return true; } int size(int x) { return siz[root(x)]; } }; using Graph = vector<vector<int>>; vector<int> visited(false); const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; // const int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; // const int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; void dfs(const Graph &G, int v) { visited[v] = true; for (auto next : G[v]) { if (visited[next]) continue; dfs(G, next); } } bool isPrime(ll n) { if (n < 2) return false; for (ll i = 2; i * i <= n; ++i) if (n % i == 0) return false; return true; } // 最大公約数 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最小公倍数 ll lcm(ll a, ll b) { return abs(a) / gcd(a, b) * abs(b); } ll gcd2(const vector<ll> &v) { ll val = v[0]; for (ll i = 1; i < v.size(); ++i) val = gcd(val, v[i]); return val; } ll lcm2(const vector<ll> &v) { ll val = v[0]; for (ll i = 1; i < v.size(); ++i) val = lcm(val, v[i]); return val; } vector<ll> divisors(ll n) { vector<ll> a; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); if (n / i != i) a.push_back(n / i); } } sort(a.begin(), a.end()); return a; } // pairを要素に持つvectorをsecondを基準にソートする比較関数 bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; } vector<int> eratosthenes(int n) { vector<bool> is_prime(n + 1, true); vector<int> p; for (int i = 2; i <= n; ++i) { if (is_prime[i]) { for (int j = i * 2; j <= n; j += i) { is_prime[j] = false; } p.push_back(i); } } return p; } int main() { int n, m; cin >> n >> m; vector<bool> a(1010, false), b(1010, false); rep(i, n) { int v; cin >> v; a[v] = true; } rep(i, m) { int v; cin >> v; b[v] = true; } for (int i = 1; i < 1010; ++i) { if ((a[i] && !b[i]) || (!a[i] && b[i])) cout << i << " "; } // cout << fixed << setprecision(9) << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define BE(x) x.begin(), x.end() int main() { string s; cin >> s; ll ans = 0; vector<int> v(26, 0); int len = s.length(); v[s[len-1]-'a']++; v[s[len-2]-'a']++; char prev = '0'; int x = len; for (int i = len-3; i >= 0; i--) { if (s[i] == s[i+1] && s[i+1] != s[i+2]) { if (s[i] == prev) ans += x - (i+2) - (v[s[i]-'a']-1); else ans += len - (i+2) - (v[s[i]-'a']-1); for (auto &x : v) x = 0; prev = s[i]; x = i; } else v[s[i]-'a']++; } cout << ans << endl; }
#include <iostream> #include <vector> using namespace std; int main(){ int N; cin >> N; vector<int> prime_number; vector<int> used(N+1); for (int i=2; i<=N; i++){ if (used.at(i)==0){ prime_number.push_back(i); int num = 2*i; while (num<=N){ used.at(num)=1; num += i; } } } vector<int> ans(N+1); ans.at(1)=1; for (int i=1; i<=N; i++){ int num = i; int index = 0; while(index<=(int)prime_number.size()-1&&num<=N){ num = i*prime_number.at(index); if (num<=N) ans.at(num) = ans.at(i)+1; index++; } } for (int i=1; i<=N; i++){ cout << ans.at(i) << " "; } cout << endl; }
#include<bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { T f = 1; x = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(char(x % 10 + 48)); } template <typename T> void writeln(T x, char ch = '\n') { write(x); putchar(ch); } int n, m, k; int main() { read(n); read(m); read(k); cout << (n - m + k) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N,K,A; cin >> N >> K; string S; for(int i=0;i<K;i++){ S=to_string(N); sort(S.begin(), S.end()); A=stoi(S); reverse(S.begin(), S.end()); N=stoi(S)-A; } cout << N << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); ++i) #define vec std::vector using ll = long long; template<class T> std::istream &operator >> (std::istream &is, std::vector<T> & v) { for(T &x : v) is >> x; return is; } int main() { vec<string> s(3); cin >> s; set<char> st; rep(i, 3) { for(auto &c : s[i]) st.insert(c); } if(st.size() > 10) { cout << "UNSOLVABLE" << '\n'; return 0; } vector<char> ch; for(auto &c : st) ch.push_back(c); while(ch.size() < 10) ch.push_back('0'); sort(ch.begin(), ch.end()); vec<int> num(26, -1); auto tol = [&](string str) -> ll { ll res = 0, x = 1; for(int i = str.size() - 1; i >= 0; --i) { res += x * num[str[i] - '0']; x *= 10; } return res; }; do { rep(i, 10) { if(ch[i] == '0') continue; num[ch[i] - '0'] = i; } ll a = tol(s[0]); ll b = tol(s[1]); ll c = tol(s[2]); if(!a || !b || !c) continue;; if(to_string(a).size() != s[0].size()) continue; if(to_string(b).size() != s[1].size()) continue; if(to_string(c).size() != s[2].size()) continue; if(a + b == c) { cout << a << '\n'; cout << b << '\n'; cout << c << '\n'; return 0; } } while(next_permutation(ch.begin(), ch.end())); cout << "UNSOLVABLE" << '\n'; return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> //#include <atcoder/all> //using namespace atcoder; using namespace std; #define all(v) v.begin(), v.end() using in = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define Yes cout<<"Yes"<<endl #define No cout<<"No"<<endl #define yes cout<<"yes"<<endl #define no cout<<"no"<<endl #define YES cout<<"YES"<<endl #define NO cout<<"NO"<<endl const in MOD = 1000000007; const in INF=(in)1e18+7; const int inf=max(int(1e18)+7,int(1e9)+7); using P=pair<int,int>; using T=tuple<int,int,int>; vector<int> dx={0,1,-1,0}; vector<int> dy={1,0,0,-1}; template <typename Typ> bool chmin(Typ &a, const Typ& b) {if (a > b) {a = b; return true;} return false;} template <typename Typ> bool chmax(Typ &a, const Typ& b) {if (a < b) {a = b; return true;} return false;} template <typename Typ> Typ ceil(Typ x, Typ y){return (x+y-1)/y;} #ifdef _DEBUG #define debug(x) cout<<"debug:"<<x<<endl #define vdebug(x) cout<<"debug:";\ for(auto asdf:x) cout<<asdf<<" ";\ cout<<endl #else #define debug(x) #define vdebug(x) #endif int main(){ ios::sync_with_stdio(false);cin.tie(nullptr); //cout << fixed << setprecision(12); int n,m; cin>>n>>m; vector<int> a(m),b(m); rep(i,m) cin>>a[i]>>b[i]; int k; cin>>k; vector<int> c(k),d(k); rep(i,k) cin>>c[i]>>d[i]; int ans=0; for(int tmp = 0;tmp< (1<<k); tmp++){ bitset<16> bt(tmp); vector<bool> num(n,false); rep(i,k){ if(bt.test(i)) num[d[i]-1]=true; else num[c[i]-1]=true; } int cnt=0; rep(i,m){ if(num[a[i]-1] and num[b[i]-1]) cnt ++; } chmax(ans,cnt); } cout<<ans<<"\n"; }/* ./problem.exe */
#include <bits/stdc++.h> using namespace std; // clang-format off using ll = int64_t; template <class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v) cin >> a; return is; } template <class T> istream& operator>>(istream& is, vector<pair<T, T>>& v) { for (auto& a : v) cin >> a.first >> a.second; return is; } template <class T> istream& operator>>(istream& is, vector<tuple<T, T, T>>& v) { for (auto& a : v) { T a1, a2, a3; cin >> a1 >> a2 >> a3; a = {a1, a2, a3}; } return is; } template <class T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto& a : v) os << a << " "; os << endl; return os; } 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; } #define all(v) begin(v), end(v) #define TRC1(a) cout << #a ":" << a #define TRC2(a, b) TRC1(a); cout << " "; TRC1(b) #define TRC3(a, b, c) TRC2(a, b); cout << " "; TRC1(c) #define TRC4(a, b, c, d) TRC3(a, b, c); cout << " "; TRC1(d) using veci = vector<int>; using vecll = vector<ll>; using Pi = pair<int, int>; using Ti = tuple<int, int, int>; #define rep(i, n) for (int i = 0; i < n; i++) #define fore(x, v) for (auto x : v) const int IINF = INT32_MAX; const ll LINF = INT64_MAX; // cout << fixed << setprecision(15); void solve(); int main() { cout << fixed << setprecision(15); solve(); return 0; } // clang-format on void solve() { double R, X, Y; cin >> R >> X >> Y; double d = sqrt(X * X + Y * Y); if (d == R) { cout << 1 << endl; return; } if (d <= 2 * R) { cout << 2 << endl; return; } ll ans = ceil(d / R); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; int main() { ll R,X,Y; cin>>R>>X>>Y; ll count=0; ll r=0; while(r<X*X+Y*Y){ if(R*R>X*X+Y*Y){ count=2; break; } count++; r=(count*R)*(count*R); } cout<<count<<endl; }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; #define idfc ios_base::sync_with_stdio(false),cin.tie(nullptr) //:/ #define pb push_back #define mp make_pair #define nt _ll128 #define ld long double long double PI =3.14159265; using ll = long long; const ll modo=998244353; const ll ms=2e5+5; const ll inf=1e17; ll ar[ms][2]; ll op[ms][2]; ll ans[ms][2]; vector<pair<ll,ll>> vv[ms]; int main() { idfc; ll n; cin>>n; ll i; ll x,y; ll a,b; for(i=1;i<=n;i++) { cin>>ar[i][0]>>ar[i][1]; } ll m; cin>>m; for(i=1;i<=m;i++) { cin>>op[i][0]; if(op[i][0]>2)cin>>op[i][1]; } ll q; cin>>q; for(i=1;i<=q;i++) { cin>>x>>y; vv[x].pb({y,i}); } for(auto ii:vv[0]) { ans[ii.second][0]=ar[ii.first][0]; ans[ii.second][1]=ar[ii.first][1]; } ll t; ll p; ll tot=0; x=0; y=0; a=0; b=0; ll tmp; ll t2=0; ll t3=0; for(i=1;i<=m;i++) { t=op[i][0]; p=op[i][1]; if(t==1) { tot=(tot+1)%2; if(tot) { x=-x; t2=(t2+1)%2; } else{ y=-y; t3=(t3+1)%2; } } else if(t==2) { tot=(tot+1)%2; if(tot) { y=-y; t3=(t3+1)%2; } else{ x=-x; t2=(t2+1)%2; } } else if(t==3) { if(!tot) { x=p-x; t2=(t2+1)%2; } else{ y=p-y; t3=(t3+1)%2; } } else if(t==4) { if(!tot) { y=p-y; t3=(t3+1)%2; } else{ x=p-x; t2=(t2+1)%2; } } for(auto it:vv[i]) { ll nx,ny; if(t2) { nx=x+x-ar[it.first][0]; } else{ nx=x+x+ar[it.first][0]; } if(t3) { ny=y+y-ar[it.first][1]; } else{ ny=y+y+ar[it.first][1]; } if(tot) { ans[it.second][0]=ny; ans[it.second][1]=nx; } else{ ans[it.second][0]=nx; ans[it.second][1]=ny; } } } for(i=1;i<=q;i++)cout<<ans[i][0]<<" "<<ans[i][1]<<"\n"; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = int64_t; using P = pair<ll, ll>; template <class T> using V = vector<T>; template <class T> class Matrix { public: int h, w; V<V<T>> data; Matrix(int h_, int w_ = 0, T v = 0) { h = h_; w = (w != 0) ? w_ : h; data.resize(h); rep(i, h) data[i].resize(w, v); } Matrix(const V<V<T>>& data_) : h(data_.size()), w(data_[0].size), data(data_) {} Matrix(initializer_list<initializer_list<T>> list) { data.resize(list.size()); int cnt = 0; for (auto row : list) data[cnt++] = V<T>(row); h = data.size(), w = data[0].size(); } Matrix(const Matrix& a) : h(a.h), w(a.w), data(a.data) {} Matrix& operator=(const Matrix& a) { h = a.h, w = a.w, data = a.data; return *this; } V<T>& operator[](int n) { return data[n]; } Matrix& operator*=(Matrix& a) { V<V<T>> res(w, V<T>(a.w)); rep(i, w) rep(j, a.w) { rep(k, w) res[i][j] += data[i][k] * a[k][j]; } swap(data, res); h = w, w = a.w; return *this; } Matrix operator*(Matrix& a) { return Matrix(*this) *= a; } Matrix& operator+=(Matrix& a) { rep(i, h) rep(j, w) data[i][j] += a[i][j]; return *this; } string str() { ostringstream oss; rep(i, h) { rep(j, w) oss << data[i][j] << " "; if (i != h - 1) oss << "\n"; } return oss.str(); } }; int main() { int n, m; cin >> n; V<P> xy(n); rep(i, n) cin >> xy[i].first >> xy[i].second; V<Matrix<ll>> ops; Matrix<ll> now = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; ops.push_back(now); Matrix<ll> clk = {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}; Matrix<ll> clw = {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}}; Matrix<ll> f3 = {{-1, 0, 2}, {0, 1, 0}, {0, 0, 1}}; Matrix<ll> f4 = {{1, 0, 0}, {0, -1, 2}, {0, 0, 1}}; cin >> m; rep(i, m) { int op; ll p; cin >> op; if (op > 2) cin >> p; if (op == 1) { now = clk * now; } else if (op == 2) { now = clw * now; } else if (op == 3) { f3[0][2] = 2 * p; now = f3 * now; } else if (op == 4) { f4[1][2] = 2 * p; now = f4 * now; } ops.emplace_back(now); } int Q; cin >> Q; rep(i, Q) { int a, b; cin >> a >> b; b--; Matrix<ll> pos = {{xy[b].first}, {xy[b].second}, {1}}; auto ans = ops[a] * pos; ll x = ans[0][0], y = ans[1][0]; printf("%ld %ld\n", x, y); } }
#include <iostream> using namespace std; int main(){ long long a, b, c; cin >> a >> b >> c; int c_mod = c % 4; if(c_mod == 0) c_mod = 4; int b_mod = b % 4; if(b_mod == 0) b_mod = 4; int a_down = a % 10; int b_res = 1; int a_res = 1; for(int i = 0; i < c_mod; i++){ b_res *= b_mod; } b_mod = b_res % 4; if(b_mod == 0) b_mod = 4; for(int i = 0; i < b_mod; i++){ a_res *= a_down; } int a_mod = a_res % 10; cout << a_mod << endl; }
#include <iostream> #include <sstream> #include <string> #include <list> #include <vector> #include <queue> #include <algorithm> #include <climits> #include <cstring> #include <cmath> #include <stack> #include <iomanip> #include <tuple> #include <functional> #include <cfloat> #include <map> #include <set> #include <array> #include <stdio.h> #include <string.h> #include <random> #include <cassert> using ll = long long; using ull = unsigned long long; using uint = unsigned int; using namespace std; #define int long long #define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end()) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ARY_SORT(a, size) sort((a), (a)+(size)) #define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end())) #define REVERSE(v) (reverse((v).begin(), (v).end())) #define ARY_REVERSE(v,a) (reverse((v), (v)+(a))) #define REP(i, n) for (int (i)=0; (i) < (n); (i)++) #define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++) #define CONTAINS_MAP(m, a) ((m).find((a)) != m.end()) #define CONTAINS_SET(m, a) ((m).find((a)) != m.end()) void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); } void Yes() { cout << "Yes" << endl; exit(0); } void No() { cout << "No" << endl; exit(0); } //---------- (a^n)%m ----------- ll pom(ll a, ll n, int m) { ll x = 1; for (a %= m; n; n /= 2) n & 1 ? x = x * a % m : 0, a = a * a % m; return x; } signed main() { int A, B, C; cin >> A >> B >> C; int bc = pom(B, C, 4); if (bc == 0) bc = 4; int a = pom(A, bc, 10); cout << a << endl; }
#include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; typedef long long ll; ll pow (ll a, ll n) { if (n==0) return 1; if (n==1) return a; ll tmp = pow (a, n/2); ll tmp2 = tmp*tmp; if (n%2) return tmp2*a; return tmp2; } int main () { ll N; cin >> N; ll tmp = 1; ll keta = 0; for (int i=0; i<12; i++) { if (N / tmp > 0) { keta ++; } tmp *= 10; } ll ans = 0; tmp = 1; for (int i=3; i<=keta; i+=2) { ans += 9*tmp; tmp *= 10; } if (keta%2 == 0) { ll hketa = keta / 2; ll d = pow(10, hketa); ll Nh = N / d; ll Nl = N % d; ans += Nh - d/10; if (Nl >= Nh) ans ++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ string n;cin>>n; ll X = (ll)n.size(); string S; ll Q = stoll(n); if(Q<10){ cout<<0<<endl; return 0; } if(X%2==1){ n=""; for(ll i=0;i<X-1;i++){ n+='9'; } } ll Y = (ll)n.size(); string B; string C; for(ll i=0;i<Y/2;i++){ B+=n[i]; } for(ll i=Y/2;i<Y;i++){ C+=n[i]; } //cout<<B<<endl; ll b = stoll(B); ll c = stoll(C); if(b>c){ cout<<b-1<<endl; }else{ cout<<b<<endl; } }
#include "bits/stdc++.h" using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(a) (a).begin(),(a).end() #define LL long long #define MAX_H 10000 #define MAX_W 10000 int n; int x[200],y[200],r[200]; int a[200],b[200],c[200],d[200]; std::mt19937 mt; std::random_device rnd; struct Del { int i; char dir;// U D L R }; void init(){ REP(i,n){ a[i]=x[i]; c[i]=x[i]+1; b[i]=y[i]; d[i]=y[i]+1; } } void flush(){ REP(i,n)cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<" "<<d[i]<<endl; } LL score(){ double ret = 0.0; REP(i,n){ LL s = (c[i]-a[i])*(d[i]-b[i]); double rat = (double)min(s,(LL)r[i])/(double)max(s,(LL)r[i]); ret+=1-(1-rat)*(1-rat); } return (LL)(ret*1e9/n); } /* control functions finish */ /* util functions start */ bool is_collide_two_interval(int a_,int b_,int c_, int d_){ return (a_<=c_ && c_<b_) || (c_<=a_ && a_<d_); } bool is_collide_two_ad(int i,int j){ if(i==j)return true; return is_collide_two_interval(a[i],c[i],a[j],c[j]) && is_collide_two_interval(b[i],d[i],b[j],d[j]); } bool is_collide_any_ad(int i){ bool ret = false; REP(j,n){ if(i==j)continue; ret = ret || is_collide_two_ad(i,j); } return ret; } int area(int a_,int b_,int c_, int d_){ return (c_-a_)*(d_-b_); } bool is_over_area(int i){ return area(a[i],b[i],c[i],d[i]) > r[i]; } Del generate_del(){ int i = mt()%n; int dir_i=mt()%4; switch(dir_i){ case 0: return Del{i,'U'}; case 1: return Del{i,'D'}; case 2: return Del{i,'L'}; case 3: return Del{i,'R'}; } } bool redo_del(Del del){ int i = del.i; switch(del.dir){ case 'U': if(b[i]==0)return false; b[i]--; return true; case 'D': if(d[i]==MAX_H)return false; d[i]++; return true; case 'L': if(a[i]==0)return false; a[i]--; return true; case 'R': if(c[i]==MAX_W)return false; c[i]++; return true; } } void undo_del(Del del){ int i = del.i; switch(del.dir){ case 'U': b[i]++; return; case 'D': d[i]--; return; case 'L': a[i]++; return; case 'R': c[i]--; return; } } /* util functions finish */ int main(){ mt.seed(42); cin>>n; REP(i,n)cin>>x[i]>>y[i]>>r[i]; init(); REP(i,3000000){ auto del = generate_del(); auto suc = redo_del(del); if(!suc){continue;} if(is_over_area(del.i) || is_collide_any_ad(del.i))undo_del(del); } flush(); //cerr<<score()<<endl; return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; //using namespace atcoder; const int mod = 1000000007; const int INF =1e9+1; int main(){ int n;cin>>n; vector<int> p(n); rep(i,n)cin>>p[i]; int maxP=0; vector<bool> boo(200005,0); rep(i,n){ boo[p[i]]=1; if(maxP==p[i]){ for(int i=maxP+1;i<200005;i++){ if(boo[i]==0){ maxP=i; break; } } } cout<<maxP<<endl; } }
#include <iostream> #include <random> #include <cmath> #include <queue> #include <utility> #include <vector> #include <algorithm> #include <stack> #include <cassert> using namespace std; const int SIZE = 30; const int INF = 99999999; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const char dc[4] = {'D', 'R', 'U', 'L'}; struct Point { int i, j; Point() {} Point(int i, int j) : i(i), j(j) {} bool is_valid() { return (i >= 0) && (i < SIZE) && (j >= 0) && (j < SIZE); } }; struct Edge { Point to; int n_passed = 0; double length = -1.0; }; Edge graph[SIZE][SIZE][4]; deque<char> calculate_path(Point start, Point terminal) { bool visited[SIZE][SIZE]; fill((bool*)visited, (bool*)visited+(SIZE*SIZE), false); //for (int i = 0; i < SIZE; ++i) { // for (int j = 0; j < SIZE; ++j) { // visited[i][j] = false; // } //} int dist[SIZE][SIZE]; fill((int*)dist, (int*)dist+(SIZE*SIZE), INF); int direction[SIZE][SIZE]; fill((int*)direction, (int*)direction+(SIZE*SIZE), -1); stack<Point> s; visited[start.i][start.j] = true; dist[start.i][start.j] = 0; s.push(start); while (!s.empty()) { Point p = s.top(); s.pop(); for (int dir = 0; dir < 4; ++dir) { Point np = p; np.i += dx[dir]; np.j += dy[dir]; if (np.is_valid() && !visited[np.i][np.j]) { visited[np.i][np.j] = true; dist[np.i][np.j] = dist[np.i][np.j] + graph[p.i][p.j][dir].length; direction[np.i][np.j] = dir; s.push(np); } } } deque<char> path; Point p = terminal; while (p.i != start.i || p.j != start.j) { int dir = direction[p.i][p.j]; assert(dir >= 0 && dir < 4); path.push_front(dc[dir]); p.i -= dx[dir]; p.j -= dy[dir]; } return path; } void print_path(deque<char> path) { for (char c : path) { cout << c; } cout << endl << flush; } int main() { for (int i = 0; i < 1000; ++i) { // input start and terminal; Point s, t; cin >> s.i >> s.j >> t.i >> t.j; // calculate path deque<char> path = calculate_path(s, t); // output path print_path(path); // input path length int b; cin >> b; // update edge length } }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++) #define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define popcount(v) __builtin_popcount(v) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__))) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=1LL<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;} ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} /*string dijkstra(int sy,int sx,int gy,int gx){ }*/ int main(){ int test=1000,sy,sx,gy,gx; int score; double d2; while(test--){ cin>>sy>>sx>>gy>>gx; string s,t; if(gy>sy)s.resize(gy-sy,'D'); else s.resize(sy-gy,'U'); if(gx>sx)t.resize(gx-sx,'R'); else t.resize(sx-gx,'L'); cout<<s<<t<<endl; cin>>score; } }
// https://atcoder.jp/contests/abc182/tasks/abc182_f #include <bits/stdc++.h> using namespace std; using ll = int64_t; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++) #define DEBUG 1 #if DEBUG #define _GLIBCXX_DEBUG #define DUMP(a) REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ") #define DUMP2D(b) REP(_j, b.size()) DUMP(b[_j]); cout << endl #else #define DUMP(a) #define DUMP2D(b) #endif const int N_MAX = 50; const ll A_N_MAX = 1e15; const ll X_MAX = 1e15; const ll INF = 2e15; int N; ll X; vector<ll> A; vector<ll> C, Y; vector<vector<ll>> dp; int main() { cin >> N >> X; A = vector<ll>(N); REP(i, N) cin >> A[i]; C = vector<ll>(N); Y = vector<ll>(N); ll tmp = X; REP(i, N - 1) { C[i] = A[i + 1] / A[i]; Y[i] = tmp % C[i]; tmp /= C[i]; } C[N - 1] = INF; Y[N - 1] = tmp; // DUMP(C); // DUMP(Y); dp = vector<vector<ll>>(N + 1, vector<ll>(2)); dp[0][0] = 1; dp[0][1] = 0; REP(i, N) { dp[i + 1][0] += dp[i][0]; if (Y[i] < C[i] - 1) dp[i + 1][0] += dp[i][1]; if (i + 1 < N) { dp[i + 1][1] += dp[i][1]; if (Y[i] > 0) dp[i + 1][1] += dp[i][0]; } } // DUMP2D(dp); ll ans = dp[N][0]; cout << ans << endl; }
#include<bits/stdc++.h> #include<iostream> #include<algorithm> #include<unordered_map> #include<vector> #include<unordered_set> #include<set> #include<map> #include<queue> #include<stack> #include<string> //Required Headers using namespace std; #define PI 3.1415926535897932384626 #define sp(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; // dynamic allocation of memory #define del(arr) delete []arr; // deletion of allocated memory const int mod = 1'000'000'007; const int inf = 1e9 + 5; //for loops #define fo(i, n) for(i=0; i<n; i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define fo2(i, n,t) for(i=0; i<n; i+=t) //essentials #define tc(t) ll t;ll i; cin>>t; fo(i,t) #define ll long long //STL #define pb push_back #define mp make_pair #define F first #define S second #define vbe(x) x.begin(), x.end() #define sortvbe(x) sort(vbe(x)) #define setit(it, a) for(auto it = a.begin(); it != a.end(); it++) typedef map<int,int> mii; typedef map<ll,ll> mll; typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef set<int> si; typedef set<ll> sl; //inputs #define ci(x) cin>>x; #define ci2(x,y) cin>>x>>y; //template<typename... T> //void read(T&... args) { // ((cin >> args), ...); //} //outputs #define pi(x) cout<<x; #define pinl(x) cout<<x<<endl; //template<typename... T> //void write(T&&... args) { //rvalue reference is new to C++ // ((cout << args << " "), ...); //} ll m=1000000007; //some imp func inline int gcd(int a,int b) {if (a==0) return b; else return gcd(b%a,a);} inline void take_input(int A[],int n){for(int i=0;i<n;i++){ cin>>A[i];}} inline void show(int *A,int n){for(int i=0;i<n;i++){cout<<A[i]<<" ";}cout<<endl;} inline bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) { return (a.second < b.second); } inline bool sortbysecdesc(const pair<int,int> &a, const pair<int,int> &b) { return (a.second > b.second); } inline void swap(int &a,int &b){int temp=a;a=b;b=temp;} inline void initialise(int A[],int n,int k){for(int i=0;i<n;i++){A[i]=k;}} inline void vect_input(vector<int>&v,int n){for(int i=0;i<n;i++){int input;cin>>input;v.pb(input);}} inline int modulo(int a,int b){return (b+(a%b))%b;} inline int getascii(char ch){return (int)ch;} inline ll expo(ll e,ll p){if(p==0)return (1);ll ans=expo(e,p/2);ans=((ans%m)*(ans%m))%m;if(p%2!=0)ans=((e%m)*(ans%m))%m;return ans%m;} inline ll com_wd_m(ll n,ll r,ll m){ll p=1,d=1;if(r==0 or r==n )return(1);if(r==1 )return (n%m);r=min(r,n-r);for(ll i=0;i<r;i++){p=((p%m)*((n%m)-(i%m))%m)%m;p=((p%m)*(expo((i+1),m-2)%m))%m;}return (p%m);} template<typename T> void show_vect(vector<T> v){for(int i=0;i<v.size();i++){cout<<v[i]<<" ";}cout<<endl;} template<typename T> void vect_input(vector<T>&v,T n){for(int i=0;i<n;i++){cin>>v[i];}} int fact(int n) { return (n==1 || n==0) ? 1: n * fact(n - 1); } //debugging #define deb(x) cout << #x << " = " << x << endl; //to check the values #define deb2(x, y) cout << #x << " = " << x << "," << #y << " = " << y << endl; void A_A() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); freopen("output.in", "w", stdout); freopen("error.in","w",stderr); #endif } ll num; void solve(){ ll i,n,j; cin>>n; vl a(n); ll s=0,mx=0; long double s2=0; fo(i,n){ cin>>a[i]; s+=abs(a[i]); s2+=a[i]*a[i]; mx=max(abs(a[i]),mx); } ll x=15; long double ans=sqrt(s2); cout<<s<<endl; cout << fixed; cout<<setprecision(15)<<ans<<endl; cout<<mx<<endl; } int main(){ A_A(); // tc(t){ solve(); // } return 0; } /* */
#include <iostream> #include <queue> #include <utility> using namespace std; int main() { int N; cin >> N; int P[N]; for (int i = 0; i < N; i++) cin >> P[i]; bool flag = true; bool flag_[N - 1]; for (int i = 0; i < N - 1; i++) flag_[i] = false; int now = 0; queue<int> Que; for (int i = 0; i < N; i++){ if (i == P[i] - 1) flag = false; if (i > P[i] - 1){ for (int j = i - 1; j >= now; j--){ if (P[j + 1] < P[j] && flag_[j] == false){ flag_[j] = true; Que.push(j); swap(P[j], P[j + 1]); } } now = i; } } for (int i = 0; i < N - 1; i++){ if (flag_[i] == false) flag = false; } for (int i = 0; i < N; i++){ if (i != P[i] - 1) flag = false; } if (flag == true) { for (int i = 0; i < N - 1; i++){ cout << Que.front() + 1 << endl; Que.pop(); } } else cout << -1 << endl; }
#include<vector> #include<cstdio> using namespace std; int n,per[200010],ori[200010],mn=1e9; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&per[i]),ori[per[i]]=i; vector<int> ans; for(int i=n;i;--i) { if((ori[i]^i)&&mn>ori[i]) { mn=ori[i]; for(int j=ori[i];j<i;++j) ans.push_back(j),(per[j]^=per[j+1]^=per[j]^=per[j+1]); } } if(ans.size()^int(n-1)) printf("-1\n"); else { for(int i=1;i<=n;++i) if(per[i]^i) return printf("-1\n"),0; for(int now : ans) printf("%d\n",now); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define chmax(x, y) (x = max(x, y)) #define chmin(x, y) (x = min(x, y)) using ll = long long; using PAIR = pair<int, int>; using PAIRLL = pair<ll, ll>; using vi = vector<int>; using vvi = vector<vi>; using vc = vector<char>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; using vll = vector<ll>; using vvll = vector<vll>; using Graph = vector<vector<int>>; int main() { ll B, C, ans = 0; cin >> B >> C; if (B > 0) { if(C==1) { cout << 2 << endl; return 0; } if (B - C / 2 > 0) { if (C % 2) ans += 2 * (C / 2); else { ans += 2 * (C / 2) - 1; } } else { ans += 2 * B - 1; } if (C % 2) { ans += 2 * ((C-1) / 2) + 1; } else { ans += 2 * ((C-1) / 2+1); } } else if (B == 0) { if(C==1) { cout << 1 << endl; return 0; } if (C % 2) { cout << 2 * (C / 2) + 1 << endl; return 0; } else { cout << 2 * (C / 2) << endl; return 0; } } else { if(C==1) { cout << 2 << endl; return 0; } if (C % 2) { ans += 2 * (C / 2+1); } else { ans += 2 * (C / 2+1) - 1; } C--; B *= -1; if (B - C / 2 > 0) { if(C==1) { cout << 4 << endl; return 0; } if (C % 2) ans += 2 * (C / 2); else { ans += 2 * (C / 2+1) + 1; } } else { ans += 2 * B - 1; } } cout << ans << endl; }
#include<bits/stdc++.h> #define LL long long #define DB double #define pb push_back #define mkp make_pair #define LB lower_bound #define UB upper_bound #define rint register int #define MS(x,y) memset(x,y,sizeof(x)) #define rep(i,a,b) for(rint i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(rint i=a,i##end=b;i>=i##end;--i) #define cms printf("%.2lf\n",(&o2-&o1)/1024.0/1024) using namespace std;bool o1; char IO; inline int Rd(){ int _s_=0; bool _f_=0; while(!isdigit(IO=getchar())) IO=='-'&&(_f_=1); do _s_=(_s_<<1)+(_s_<<3)+(IO^48); while(isdigit(IO=getchar())); return _f_?-_s_:_s_; } const int M=400005; int n,m,p,t,A[M],B[M],b[M],c[M],f[M],g[M],T,ans; vector<int>v[M]; bool o2;int main(){ n=Rd(),m=Rd(); rep(i,1,m) c[++p]=A[i]=Rd(),b[i]=B[++t]=Rd(); b[++t]=n; sort(b+1,b+1+t); sort(c+1,c+1+p); t=unique(b+1,b+1+t)-b-1; p=unique(c+1,c+1+p)-c-1; rep(i,1,m){ A[i]=LB(c+1,c+1+p,A[i])-c; B[i]=LB(b+1,b+1+t,B[i])-b; v[A[i]].push_back(B[i]); } f[LB(b+1,b+1+t,n)-b]=1; rep(i,1,p){ rep(j,0,(int)v[i].size()-1){ int x=v[i][j]; g[x]=0; if(b[x-1]==b[x]-1) g[x]|=f[x-1]; if(b[x+1]==b[x]+1) g[x]|=f[x+1]; } rep(j,0,(int)v[i].size()-1){ int x=v[i][j]; f[x]=g[x]; } } rep(i,1,t) ans+=f[i]; cout<<ans; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> using namespace std; int main() { int N; cin >> N; N*=2; vector<pair<long long, int>> dogs; vector<int> color = vector<int>(3); color[0] = color[1] = color[2] = 0; int odd1=-1, odd2=-1; for(int i=0; i < N; i++) { long long x; cin >> x; char c; cin >> c; while(c==' ') cin >> c; switch(c) { case 'R': dogs.push_back(make_pair(x, 0)); color[0]++; break; case 'G': dogs.push_back(make_pair(x, 1)); color[1]++; break; case 'B': dogs.push_back(make_pair(x, 2)); color[2]++; break; } } for(int i=0; i < 3; i++) { if(color[i]%2==1) { if(odd1==-1) odd1=i; else odd2=i; } } if(odd1==-1) cout << 0 << endl; else { const long long INF = 1e16; long long diff[3][3] = {{INF, INF, INF}, {INF, INF, INF}, {INF, INF, INF}}; sort(dogs.begin(), dogs.end()); vector<long long> last = vector<long long>(3, -1); last[dogs[0].second] = dogs[0].first; for(int i=1; i < N; i++) { int tmp = dogs[i].second; last[tmp] = dogs[i].first; for(int j=0; j < 3; j++) if(last[j]!=-1) diff[j][tmp] = diff[tmp][j] = min(diff[j][tmp], dogs[i].first-last[j]); } int odd3 = 3-odd1-odd2; cout << min(diff[odd1][odd2], diff[odd1][odd3]+diff[odd3][odd2]) << endl; } return 0; }
#include <bits/stdc++.h> #include <string> typedef long long ll; typedef long double ldb; typedef double db; using namespace std; #define ff first #define ss second #define pb push_back #define pf push_front #define ins insert #define mp make_pair #define len(s) s.length() #define forp(i, a, b) for (ll i = a; i <= b; i++) #define rep(i, n) for (ll i = 0; i < n; i++) #define ren(i, n) for (ll i = n - 1; i >= 0; i--) #define forn(i, a, b) for (ll i = a; i >= b; i--) #define on cout << endl #define o2(a, b) cout << a << " " << b #define os cout << " " #define all(v) v.begin(), v.end() #define mem(n, m) memset(n, m, sizeof(n)) #define vi vector<int> #define vl vector<ll> #define ld long double #define vld vector<ld> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define vvld vector<vector<ld>> #define pii pair<int, int> #define pll pair<ll, ll> #define vpii vector<pii> #define vpll vector<pll> #define mll map<ll, ll> #define lb lower_bound #define ub upper_bound #define ret return 0 #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define ford(container, it) for (__typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define fors(container, it, a, b) for (__typeof(container.begin()) it = a; it != b; it++) #define d1(x) cout << (x) << endl #define d2(x, y) cout << (x) << " " << (y) << endl #define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl #define d4(a, b, c, d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define max3(a, b, c) max(max(a, b), c) #define min3(a, b, c) min(min(a, b), c) #define itoa(x) to_string(x); #define atoi(x) stoll(x); #define boost ios_base::sync_with_stdio(0) #define MOD 1000000007 #define EPSILON 1e-9 #define PI 3.14159265358979323846 #define inf 999999999999999999 #define siz 100001 #define SIZ 1000001 #define SIZE 200001 #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define w(x) \ int x; \ cin >> x; \ while (x--) ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll pwr(ll x, ll y) { ll res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (b * a) / gcd(a, b); } ll fact(ll n) { if (n <= 1) return 1; return n * fact(n - 1); } ll nPr(ll n, ll r) { return fact(n) / fact(n - r); } ll cd(ll n) { ll c=0; while(n>0) { n/=10; c++; } return c; } ll getIndex(vl v, ll K) { auto it = find(v.begin(), v.end(), K); // If element was found if (it != v.end()) { // calculating the index // of K ll index = it - v.begin(); return index; } else { // If the element is not // present in the vector return -1; } } void c_p_c() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); c_p_c(); ll t; t = 1; // cin >> t; while (t-- > 0) { ll a, b; cin>>a>>b; ll d = b-a, j=d; for(j; j>(d+1)/2; j--) { if((j-a%j)%j<d-j+1) goto out; } out: d1(j); } return 0; }
//#define _GLIBCXX_DEBUG //#include<bits/stdc++.h> //* #include<vector> #include<iostream> #include<queue> #include<algorithm> #include<math.h> #include<iomanip> #include <numeric> //*/ using namespace std; #define ll long long #define yes {cout<<"Yes"<<endl;return 0;} #define no {cout<<"No"<<endl;return 0;} #define YES {cout<<"YES"<<endl;return 0;} #define NO {cout<<"NO"<<endl;return 0;} #define cyes {cout<<"Yes"<<endl;continue;} #define cno {cout<<"No"<<endl;continue;} #define MOD 1000000007 #define MOD2 1000000009 #define MOD3 998244353 #define INF 2147483647 constexpr double PI = 3.14159265358979; #define LINF 9223372036854775807 #define rep(i,n) for (int i = 0; i < (n); ++i) #define rep1(i,n) for (int i = 1; i < (n); ++i) #define PF push_front #define PB push_back #define POF pop_front #define POB pop_back #define MP make_pair #define MT make_tuple #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define SZ(a) (int)((a).size()) #define NEXP(a) next_permutation((a).begin(),(a).end()) #define FI first #define SE second #define SORT(c) sort((c).begin(),(c).end()) #define SORTR(c) sort((c).rbegin(), (c).rend()) #define REVERSE(c) reverse((c).begin(),(c).end()) typedef long long LL; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<string> VS; typedef vector<VS> VVS; typedef vector<char> VC; typedef vector<VC> VVC; typedef vector<PII> VPII; typedef vector<PLL> VPLL; void solve(vector<pair<PII, PII>> &s,vector<pair<PII,PII>> &a) { int n=s.size(); SORT(s); //map<int,int>ymax; //ymax[0]=10000; rep(i,n) { int now,pre; now=s[i].SE.SE; int xmax,ymax; if(i==0) { a[now].FI.FI=0; a[now].FI.SE=0; a[now].SE.FI=s[i].FI.FI+1; a[now].SE.SE=s[i].FI.SE+1; //ymax[-s[i].FI.FI]=s[i].FI.SE; xmax=s[i].FI.FI; ymax=s[i].FI.SE; } else { if(s[i].FI.FI==xmax) { a[now].FI.FI=a[pre].FI.FI; a[now].FI.SE=a[pre].SE.SE; a[now].SE.FI=s[i].FI.FI+1; a[now].SE.SE=s[i].FI.SE+1; xmax=s[i].FI.FI;ymax=s[i].FI.SE; if(i==n-1) { a[now].SE.SE=10000; } if(s[n-1].FI.FI==s[i].FI.FI) { a[now].SE.FI=10000; } } else { a[now].FI.FI=a[pre].SE.FI; a[now].FI.SE=0; a[now].SE.FI=s[i].FI.FI+1; a[now].SE.SE=s[i].FI.SE+1; if(i==n-1) { a[now].SE.SE=10000; } if(s[n-1].FI.FI==s[i].FI.FI) { a[now].SE.FI=10000; } xmax=s[i].FI.FI; ymax=s[i].FI.SE; a[pre].SE.SE=10000; } } pre=now; } } int main(void) { int n; cin >> n; vector<pair<PII, PII>> s(n); vector<pair<PII,PII>> ans(n); rep(i, n) { cin >> s[i].first.first>> s[i].first.second >> s[i].second.FI; s[i].SE.SE=i; } solve(s,ans); rep(i,n)cout<<ans[i].FI.FI<<" "<<ans[i].FI.SE<<" "<<ans[i].SE.FI<<" "<<ans[i].SE.SE<<endl; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int n; struct xyr { int x; int y; int r; }; struct sq { int x1; int y1; int x2; int y2; int s; }; vector<xyr> ad; vector<sq> ans; vector<bool> okArea; clock_t start; vector<vector<int>> area(10001, vector<int>(10001, -1)); void input () { cin >> n; ad.resize(n); rep(i, n) cin >> ad[i].x >> ad[i].y >> ad[i].r; } void init() { ans.resize(n); okArea.resize(n); rep(i, n) { ans[i].x1 = ad[i].x; ans[i].y1 = ad[i].y; ans[i].x2 = ad[i].x+1; ans[i].y2 = ad[i].y+1; ans[i].s = 1; area[ad[i].y][ad[i].x] = i; okArea[i] = false; } } void moveLeft(int i) { bool ok = true; for (int j=ans[i].y1; j<=ans[i].y2; j++) { if (!(ans[i].x1-1 >= 0 && area[j][ans[i].x1-1] == -1)) { ok = false; break; } } if (!ok) return; ans[i].x1--; for (int j=ans[i].y1; j<=ans[i].y2; j++) { area[j][ans[i].x1] = i; ans[i].s++; } } void moveRight(int i) { bool ok = true; for (int j=ans[i].y1; j<=ans[i].y2; j++) { if (!(ans[i].x2+1 <= 10000 && area[j][ans[i].x2+1] == -1)) { ok = false; break; } } if (!ok) return; ans[i].x2++; for (int j=ans[i].y1; j<=ans[i].y2; j++) { area[j][ans[i].x2] = i; ans[i].s++; } } void moveUp(int i) { bool ok = true; for (int j=ans[i].x1; j<=ans[i].x2; j++) { if (!(ans[i].y1-1 >= 0 && area[ans[i].y1-1][j] == -1)) { ok = false; break; } } if (!ok) return; ans[i].y1--; for (int j=ans[i].x1; j<=ans[i].x2; j++) { area[ans[i].y1][j] = i; ans[i].s++; } } void moveDown(int i) { bool ok = true; for (int j=ans[i].x1; j<=ans[i].x2; j++) { if (!(ans[i].y2+1 <= 10000 && area[ans[i].y2+1][j] == -1)) { ok = false; break; } } if (!ok) return; ans[i].y2++; for (int j=ans[i].x1; j<=ans[i].x2; j++) { area[ans[i].y2][j] = i; ans[i].s++; } } void calcArea(int i) { if (ans[i].s >= ad[i].r*0.9) okArea[i] = true; } bool isTimeLimit(clock_t start) { clock_t end = clock(); const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0; if (time >= 4500) return true; else return false; } void output() { rep(i, n) { cout << ans[i].x1 << " " << ans[i].y1 << " " << ans[i].x2 << " " << ans[i].y2 << endl; } } int main() { // 入力 input(); init(); int cnt = 0; while (1) { rep(i, n) { if (okArea[i]) continue; moveLeft(i); moveRight(i); moveUp(i); moveDown(i); calcArea(i); } if (cnt%1000 == 0) { if (isTimeLimit(start)) break; } cnt++; } output(); return 0; }
#include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <unordered_map> #include <unordered_set> using namespace std; #define x first #define y second #define all(v) v.begin(), v.end() #define compress(v) (sort(all(v)), v.erase(unique(all(v)), v.end())) #define setio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) //cout << fixed << setprecision(15); typedef long long ll; typedef pair<ll, int> p; int N, M; vector<int> adj[2004]; int adj2[2004][2004]; ll dist[2004]; void dijk(int s) { int i; for (i = 1; i <= N; i++) dist[i] = -1; dist[s] = 0; priority_queue<p> pq; pq.push({ 0,s }); while (!pq.empty()) { int u = pq.top().y; ll cost = -pq.top().x; pq.pop(); if (dist[u] < cost) continue; for (auto& v : adj[u]) { int c = adj2[u][v]; if (dist[v] == -1 || dist[v] > dist[u] + c) { dist[v] = dist[u] + c; pq.push({ -dist[v],v }); } } } } int main() { setio; int i, j; cin >> N >> M; for (i = 1; i <= N; i++) for (j = 1; j <= N; j++) adj2[i][j] = -1; for (i = 1; i <= M; i++) { int a, b, c; cin >> a >> b >> c; if (adj2[a][b] == -1 || adj2[a][b] > c) adj2[a][b] = c; } for (i = 1; i <= N; i++) for (j = 1; j <= N; j++) { if (adj2[i][j] != -1) adj[i].push_back(j); } for (i = 1; i <= N; i++) { dijk(i); ll mn; if (adj2[i][i] != -1) mn = adj2[i][i]; else mn = (ll)1e10; for (j = 1; j <= N; j++) { if (i == j) continue; if (dist[j] == -1 || adj2[j][i] == -1) continue; mn = min(mn, dist[j] + adj2[j][i]); } if (mn == (ll)1e10) cout << -1 << '\n'; else cout << mn << '\n'; } }
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <utility> #include <functional> #include <set> #include <map> #include <queue> #include <deque> #include <bitset> #include <math.h> #include <random> #include <chrono> #include <assert.h> using namespace std ; using ll = long long ; using ld = long double ; template<class T> using V = vector<T> ; template<class T> using VV = V<V<T>> ; using pll = pair<ll,ll> ; #define all(v) v.begin(),v.end() ll mod = 1000000007 ; long double pie = acos(-1) ; ll INF = 1e18 ; void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;} //void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;} ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;} ll lcm(long long a,long long b){return a/gcd(a,b)*b ;} ll extGCD(ll a,ll b,ll &x,ll &y){ if(b==0){ x = 1 ; y = 0 ; return a ; } ll d = extGCD(b,a%b,y,x) ; y -= a/b*x ; return d ; } void fix_cout(){cout << fixed << setprecision(20) ;} template<class T> void chmax(T &a,T b){if(a<b) a = b ;} template<class T> void chmin(T &a,T b){if(a>b) a = b ;} int main(){ int n,m ; cin >> n >> m ; VV<pair<int,pair<ll,ll>>> g(n) ; for(int i=0;i<m;i++){ ll a,b,c,d ; cin >> a >> b >> c >> d ; a-- ; b-- ; g[a].emplace_back(b,make_pair(c,d)) ; g[b].emplace_back(a,make_pair(c,d)) ; } using T = pair<ll,int> ; priority_queue<T,V<T>,greater<T>> pq ; V<ll> dis(n,INF) ; pq.emplace(0,0) ; while(!pq.empty()){ int cur = pq.top().second ; ll cost = pq.top().first ; pq.pop() ; if(dis[cur]<cost) continue ; dis[cur] = cost ; for(auto p:g[cur]){ int nx = p.first ; ll c = p.second.first ; ll d = p.second.second ; if(dis[nx]!=INF) continue ; ll f = sqrt(d)-1 ; ll mn = INF ; if(cost<=f){ chmin(mn,(f+c+d/(f+1))); } if(cost<=f+1){ chmin(mn,f+1+c+d/(f+2)) ; } if(cost<=f+1){ chmin(mn,(f+1+c+d/(f+2))) ; } chmin(mn,cost+c+d/(cost+1)) ; pq.emplace(mn,nx) ; } } if(dis[n-1]==INF) dis[n-1] = -1 ; cout << dis[n-1] << endl ; }
#include<bits/stdc++.h> using namespace std; const int maxn = 100005; #define inf 1e9 int N, M, w[maxn]; int dis[10][10], ans = inf; struct Data{ int l, v; Data(){} Data(int l, int v){ this -> l = l, this -> v = v; } }d[maxn]; struct segment_tree{ static const int maxn = 100005; int rt, np, lc[maxn << 1], rc[maxn << 1], mx[maxn << 1]; int newnode(){ np++; lc[np] = rc[np] = mx[np] = 0; return np; } void pushup(int now){ mx[now] = max(mx[lc[now]], mx[rc[now]]); } void build(int &now, int L, int R, Data *a){ now = newnode(); if(L == R){ mx[now] = a[L].l; return; } int m = L + R >> 1; build(lc[now], L, m, a); build(rc[now], m + 1, R, a); pushup(now); } int query(int now, int L, int R, int A, int B){ if(A <= L && R <= B) return mx[now]; int m = L + R >> 1, re = 0; if(A <= m) re = max(re, query(lc[now], L, m, A, B)); if(B > m) re = max(re, query(rc[now], m + 1, R, A, B)); return re; } }st; bool cmp(Data a, Data b){ return a.v < b.v; } bool Fail(){ int mn = inf, mx = 0; for(int i = 1; i <= N; i++) mx = max(mx, w[i]); for(int i = 1; i <= M; i++) mn = min(mn, d[i].v); return mx > mn; } void floyd(){ for(int k = 1; k <= N; k++) for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) dis[i][j] = max(dis[i][j], dis[i][k] + dis[k][j]); } bool vis[10]; int seq[10], sum[10]; void run(int i){ if(i > N){ for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) dis[i][j] = -inf; for(int i = 1; i < N; i++) dis[i][i + 1] = 0; sum[0] = 0; for(int i = 1; i <= N; i++) sum[i] = sum[i - 1] + w[seq[i]]; Data tmp; for(int lef = 1; lef < N; lef++) for(int rig = lef + 1; rig <= N; rig++){ tmp.v = sum[rig] - sum[lef - 1]; int r = lower_bound(d + 1, d + M + 1, tmp, cmp) - d - 1; tmp.v = max(sum[rig - 1] - sum[lef - 1], sum[rig] - sum[lef]); int l = lower_bound(d + 1, d + M + 1, tmp, cmp) - d; if(l <= r) dis[lef][rig] = max(dis[lef][rig], st.query(st.rt, 1, M, l, r)); } floyd(); ans = min(ans, dis[1][N]); return; } for(int j = 1; j <= N; j++){ if(vis[j]) continue; vis[j] = 1, seq[i] = j; run(i + 1); vis[j] = 0; } } int main() { scanf("%d%d", &N, &M); for(int i = 1; i <= N; i++) scanf("%d", &w[i]); for(int i = 1; i <= M; i++) scanf("%d%d", &d[i].l, &d[i].v); if(Fail()){ printf("-1\n"); } else{ sort(d + 1, d + M + 1, cmp); st.build(st.rt, 1, M, d); run(1); printf("%d\n", ans); } return 0; }
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <array> #include <numeric> //#include <atcoder/all> using namespace std; //using namespace atcoder; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, int> pdi; typedef tuple<ll, ll, ll> tl3; //typedef modint998244353 mint; const int BIG_NUM = 1e9; const ll INF = 1000000000000000000; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const double EPS = 1e-7; const int MAX = 1e5 + 5; int main() { int n, m; cin >> n >> m; vector<int> w(n); for (int i = 0; i < n; i++) { cin >> w[i]; } sort(w.begin(), w.end()); vector<int> l(m); vector<int> v(m); for (int i = 0; i < m; i++) { cin >> l[i] >> v[i]; } vector<pii> b(m); for (int i = 0; i < m; i++) { b[i].first = v[i]; b[i].second = l[i]; } sort(b.begin(), b.end()); sort(v.begin(), v.end()); if (w[n - 1] > v[0]) { cout << -1 << endl; return 0; } vector<int> lim(m + 1); for (int i = 0; i < m; i++) { lim[i + 1] = max(lim[i], b[i].second); } vector<int> p(n); iota(p.begin(), p.end(), 0); ll ans = INF; do { vector<vector<ll>> g(n, vector<ll>(n)); for (int i = 0; i < n; i++) { ll sum = w[p[i]]; for (int j = i + 1; j < n; j++) { sum += w[p[j]]; auto it = lower_bound(v.begin(), v.end(), sum); int k = (it - v.begin()); //cout << k << endl; g[i][j] = lim[k]; //cout << lim[k] << endl; } } /* for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << g[i][j] << " "; } cout << endl; } */ vector<ll> d(n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { d[j] = max(d[j], d[i] + g[i][j]); } } ans = min(ans, d[n - 1]); } while (std::next_permutation(p.begin(), p.end())); cout << ans << endl; }
#include <iostream> #include <vector> #include <math.h> #include <algorithm> #include <queue> #define REP(i, n) for(int i = 0; i < n; i++) using namespace std; int H, W, A, B; //--------------------------------------------------------------------------------------------------- bool used[16][16]; int dfs(int x, int y, int a) { // 最後まで探索して、長方形のタイルを使い切っているなら、組合せを+1する if (H == y) return a == 0; // 横の端まで行ったら次の行へ if (W == x) return dfs(0, y + 1, a); // 既に置かれているなら何もできないので、次のマスへ if (used[y][x]) return dfs(x + 1, y, a); int res = 0; // 縦置き if (y + 1 < H && !used[y + 1][x] && 0 < a) { used[y][x] = used[y + 1][x] = true; res += dfs(x + 1, y, a - 1); used[y][x] = used[y + 1][x] = false; } // 横置き if (x + 1 < W && !used[y][x + 1] && 0 < a) { used[y][x] = used[y][x + 1] = true; res += dfs(x + 1, y, a - 1); used[y][x] = used[y][x + 1] = false; } // 何も置かない res += dfs(x + 1, y, a); return res; } //--------------------------------------------------------------------------------------------------- int main() { cin >> H >> W >> A >> B; cout << dfs(0, 0, A) << endl; }
#include <iostream> #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double db; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define pll pair<ll,ll> #define pb push_back #define eb emplace_back #define mp make_pair #define ub(v,val) upper_bound(v.begin(),v.end(),val) #define np(str) next_permutation(str.begin(),str.end()) #define lb(v,val) lower_bound(v.begin(),v.end(),val) #define sortv(vec) sort(vec.begin(),vec.end()) #define rev(p) reverse(p.begin(),p.end()); #define v vector #define pi 3.14159265358979323846264338327950288419716939937510 #define len length() #define repc(i,s,e) for(ll i=s;i<e;i++) #define fi first #define se second #define mset(a,val) memset(a,val,sizeof(a)); #define mt make_tuple #define repr(i,n) for(i=n-1;i>=0;i--) #define rep(i,n) for(i=0;i<n;i++) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define at(s,pos) *(s.find_by_order(pos)) #define set_ind(s,val) s.order_of_key(val) long long int M = 1e9 + 7 ; long long int inf = 9 * 1e18; //CLOCK ll begtime = clock(); #define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //CLOCK ENDED ll n, m; // modular exponentiation ll binpow(ll val, ll deg) { if (deg < 0) return 0; if (!deg) return 1 % M; if (deg & 1) return binpow(val, deg - 1) * val % M; ll res = binpow(val, deg >> 1); return (res * res) % M; } //binomial ll modinv(ll n) { return binpow(n, M - 2); } ll a, b; int main() { // your code goes here IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll i, j, t, k, x, y, z, N; cin >> n >> m; cin >> a >> b; ll nways = 0; x = n * m; ll p1[1ll << x] = {0}, p2[1ll << x] = {0}; rep(i, 1ll << x) { ll flg = 0; ll row = 0; ll mat[n][m]; mset(mat, 0); rep(j, x) { if ((i >> j) & 1) { mat[row][j % m] = 1ll; } if ((j + 1ll) % m == 0)row++; } ll cnt = 0; rep(j, n) { ll par = 0; rep(k, m) { // cout << mat[j][k] << ' '; if (mat[j][k]) { if (par == 0) { par = 1ll; } else if (par == 1ll) { if (mat[j][k - 1ll]) { par = 0; cnt++; } else { flg = 1ll; } } } } // cout << '\n'; if (par) flg = 1ll; } if (flg == 0) { p1[i] = cnt; } // cout << '\n'; flg = 0; cnt = 0; rep(k, m) { ll par = 0; rep(j, n) { if (mat[j][k]) { if (par == 0) { par = 1ll; } else if (par == 1ll) { if (mat[j - 1ll][k]) { par = 0; cnt++; } else { flg = 1ll; } } } } if (par) flg = 1ll; } if (flg == 0) { p2[i] = cnt; } } for (i = 0; i < (1ll << x); i++) { if (p1[i]) { if (p1[i] < a) { y = ((1ll << x) - 1ll)^i; for (j = y; j > 0; j = y & (j - 1ll)) { if (p2[j]) { if (p2[j] == a - p1[i]) nways++; } } } } } rep(i, 1ll << x) { if (p1[i] == a) nways++; if (p2[i] == a) nways++; } if (a == 0) { cout << 1; } else cout << nways; return 0; }
#include <bits/stdc++.h> #include <fstream> using namespace std; #define all(a)a.begin(),a.end() using ll=long long; const int INF = 1<<30; const ll INFll =1LL<<62; //const int mod= int(1e9)+7; const int mod=998244353; using P = pair<int,int>; using Pl= pair<ll,ll>; using ld=long double; using V=vector<int>; using Vl=vector<ll>; using VV=vector<vector<int>>; using VVl=vector<vector<ll>>; // modint: mod 計算を int を扱うように扱える構造体 template<int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; using mint = Fp<mod>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n,k;cin >>n>>k; vector<vector<mint>>dp(n+1,vector<mint>(n+1,0)); dp[0][0]=1; for (int i = 1; i <=n; ++i) { for(int j=n;j>=1;j--){ if(i<j)continue; mint cost=(2*j<=n)?dp[i][2*j]:0; dp[i][j]=dp[i-1][j-1]+cost; } } cout <<dp[n][k]<<"\n"; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; ll n, m, dp[2010][2010]; char s[2011][2011]; void solve() { scanf("%lld%lld", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); memset(dp, 63, sizeof(dp)); dp[n][m] = 0; for (int i = n; i > 0; i--) for (int j = m; j > 0; j--) { if (i == n && j == m) continue; dp[i][j] = max((s[i + 1][j] == '+' ? 1 : -1) - dp[i + 1][j], (s[i][j + 1] == '+' ? 1 : -1) - dp[i][j + 1]); } if (dp[1][1] > 0)cout << "Takahashi\n"; else if (dp[1][1] == 0)cout << "Draw\n"; else cout << "Aoki\n"; } int main() { // ios::sync_with_stdio(false), cin.tie(nullptr); solve(); return 0; }
#include<bits/stdc++.h> #define MAXN 5000+10 #define MOD 998244353 using namespace std; int n,m; long long DP[MAXN][2]; long long a[MAXN][2]; int main() { scanf("%d%d",&n,&m); long long Sum = 0; for(int i=1;i<=m;i++) { memset(a,0,sizeof(a)); memset(DP,0,sizeof(DP)); a[0][0] = 1; long long l = i-1; long long r = m - i; for(int j=1;j<=n;j++) { DP[j][0] = (DP[j][0] + DP[j-1][0] * l + DP[j-1][1] * l) % MOD; a[j][0] = (a[j][0] + a[j-1][0] * l + a[j-1][1] * l) % MOD; DP[j][1] = (DP[j][1] + DP[j-1][1] + DP[j-1][0] + a[j-1][0]) % MOD; a[j][1] = (a[j][1] + a[j-1][0] + a[j-1][1]) % MOD; DP[j][1] = (DP[j][1] + DP[j-1][1] * r) % MOD; a[j][1] = (a[j][1] + a[j-1][1] * r) % MOD; DP[j][0] = (DP[j][0] + DP[j-1][0] * r) % MOD; a[j][0] = (a[j][0] + a[j-1][0] * r) % MOD; } Sum = (Sum + DP[n][0] + DP[n][1]) % MOD; } cout << Sum; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int N=105; ll n,x,a[N],dp[N][N][N]; ll k,ned; ll cal(int i,int c,int mod){ if(i==n){ if(c==k && mod==ned)return 0; return -1e9; } if(dp[i][c][mod]!=-1)return dp[i][c][mod]; ll sol=cal(i+1,c,mod); if(c+1<=k) sol=max(sol,cal(i+1,c+1,(mod+a[i])%k)+a[i]); return dp[i][c][mod]=sol; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>x; for(int i=0;i<n;i++){ cin>>a[i]; } ll ans=1e18; for(int i=1;i<=n;i++){ memset(dp,-1,sizeof dp); k=i; ned=x%i; ll res=cal(0,0,0); if(res<=0)continue; ll rem=x-res; ans=min(ans,rem/i); } cout<<ans<<'\n'; }
#include <bits/stdc++.h> #define fi first #define se second using namespace std; const int N=2*1e5+5; typedef pair<long long, long long> ii; long long n, m, a=1, b; ii pw(long long n) { if (!n) return {0, 1}; ii p=pw(n/2); p.fi=p.fi*p.fi*m+2*p.fi*p.se+p.se*p.se/m;p.fi%=m; p.se=p.se*p.se%m; if (n&1) { p.fi=p.fi*10+p.se*10/m;p.fi%=m; p.se=p.se*10%m; } return p; } void solve() { cin>>n>>m; if (m==1) cout<<0; else cout<<pw(n).fi; } int main() { // freopen(".inp","r",stdin); // freopen(".out","w",stdout); ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); solve(); return 0; }
/* © 2021-05-01 07:01:01 Franco1010 All Rights Reserved */ // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") // #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> #define f first #define s second #define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' using namespace std; typedef long double ld; typedef long long lli; typedef pair<lli,lli> ii; typedef vector<lli> vi; #define deb(x) cout << #x": " << (x) << endl; const lli N = 5e2 + 5; int a[N][N], b[N][N]; int dp[N][N]; bool vis[N][N]; lli n, m; using Node = array<int,3>; void dijkstra(){ priority_queue<Node, vector<Node>, greater<Node>> pq; pq.push({0, 0, 0}); dp[0][0] = 0; vis[0][0] = true; while(!pq.empty()){ auto u = pq.top(); pq.pop(); if(u[0] != dp[u[1]][u[2]]) continue; // (x, y + 1) int cost = u[0] + a[u[1]][u[2]]; if(u[2] + 1 < m and (!vis[u[1]][u[2] + 1] or cost < dp[u[1]][u[2] + 1])){ dp[u[1]][u[2] + 1] = cost; vis[u[1]][u[2] + 1] = true; pq.push({cost, u[1], u[2] + 1}); } // (x, y - 1) if(u[2] > 0){ cost = u[0] + a[u[1]][u[2] - 1]; if(!vis[u[1]][u[2] - 1] or cost < dp[u[1]][u[2] - 1]){ dp[u[1]][u[2] - 1] = cost; vis[u[1]][u[2] - 1] = true; pq.push({cost, u[1], u[2] - 1}); } } // (x + 1, y) cost = u[0] + b[u[1]][u[2]]; if(u[1] + 1 < n and (!vis[u[1] + 1][u[2]] or cost < dp[u[1] + 1][u[2]])){ dp[u[1] + 1][u[2]] = cost; vis[u[1] + 1][u[2]] = true; pq.push({cost, u[1] + 1, u[2]}); } // else fore(i, 1, u[1] + 1){ cost = u[0] + lli(i) + 1ll; if(!vis[u[1] - i][u[2]] or cost < dp[u[1] - i][u[2]]){ dp[u[1] - i][u[2]] = cost; vis[u[1] - i][u[2]] = true; pq.push({cost, u[1] - i, u[2]}); } } } cout << dp[n - 1][m - 1] << ENDL; } int main(){ _ // freopen("file.in","r",stdin); // freopen("file.out","w",stdout); cin >> n >> m; fore(i, 0, n) fore(j, 0, m - 1) cin >> a[i][j]; fore(i, 0, n - 1) fore(j, 0, m) cin >> b[i][j]; dijkstra(); return 0; }
#include <cstdio> #define ll long long using namespace std; int n,m;ll p[5010],g[5010][5010],ans; const ll mod=998244353; int main() { scanf("%d %d",&n,&m); p[0]=1; for(int i=1;i<=n;i++) p[i]=p[i-1]*m%mod; for(int i=0;i<=m;i++) { g[i][0]=1; for(int j=1;j<=n;j++) g[i][j]=g[i][j-1]*i%mod; } ans=p[n]*n%mod; for(int x=1;x<=m;x++) for(int j=1;j<n;j++) ans=(ans-(n-j)*g[m-x][j-1]%mod*p[n-j-1]%mod+mod)%mod; printf("%lld",ans); }
#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 const int MOD = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // 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; } friend ostream& operator<<(ostream& os, const mint& value) { os << value.x; return os; } friend istream& operator>>(istream& is, mint& value) { ll t; is >> t; value = mint(t); return is; } }; #pragma endregion library int main() { int n, m; cin >> n >> m; mint mm = m; mint ans = mm.pow(n) * n; vector<mint> mpow(n + 1); mpow[0] = 1; rep(i, n) { mpow[i + 1] = mpow[i] * m; } rep(v, 1, m + 1) { vll vs(n); vs[0] = 1; mint now = 1; mint mv = 1; rep(i, 1, n) { ans -= now * mpow[n - i - 1]; now *= m; mv *= m - v; now += mv; } } print(ans); }
#include <bits/stdc++.h> using namespace std; #define int long long #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define Test int T;cin>>T;while(T--) #define ls lower_bound #define up upper_bound #define bs binary_search #define S second #define F first #define vi vector<int> #define pii pair<int,int> #define vpii vector<pii> #define vvi vector<vi> #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b))/gcd((a),(b)) #define m_n(v) *min_element(all(v)) #define m_x(v) *max_element(all(v)) #define _(a) for(auto &i:a) #define nl cout<<"\n"; #define momo(k) memset(k,0,sizeof(k)) #define momi(k) memset(k,-1,sizeof(k)) // #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; // typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set; // const int mod = 1e9+7; // int power(int x, int y,int mod){ // int res = 1; // while (y){ // if (y & 1) // res = (res*x)%mod; // y = y>>1; // x = (x*x)%mod; // } // return res; // } // modInv = power(a,m-2,m) // int fac[10009]; // void factorial(int n){ // fac[0]=1; // for(int i=1;i<=n;i++)fac[i]=(i*fac[i-1])%mod; // } // // // int ncr(int n,int r){ // return (fac[n]*((power(fac[r],mod-2,mod)*power(fac[n-r],mod-2,mod))%mod))%mod; // } // vector<int> prime; // void sieve(){ // bool arr[200000]={0}; // prime.pb(2); // for(int i=3;i<200000;i+=2){ // if(arr[i])continue; // for(int j=i*i;j<200000;j+=i) arr[j]=1; // prime.pb(i); // } // } // const int N=1e18+2; const int N = 2e5+9; const int mod = 1e9+7; void doper(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); freopen("error.txt","w",stderr); #endif } int32_t main(){ // doper(); IOS; int a,b; cin>>a>>b; if(a > b){ int s = 0; for(int i=1;i<=a;i++) { cout<<i<<" "; s += i; } for(int i=1;i<b;i++){ cout<<-1*i<<" "; s -= i; } cout<<-1*s; } else if(a == b){ for(int i=1;i<=a;i++) cout<<i<<" "; for(int j=1;j<=b;j++) cout<<-1*j<<" "; } else{ int s = 0; for(int i=1;i<=b;i++) { cout<<-1*i<<" "; s -= i; } for(int i=1;i<a;i++){ cout<<i<<" "; s += i; } cout<<-1*s; } return 0; }
#include <iostream> #include <stdio.h> #include <vector> //martin02 using namespace std; int main() { int a, b; scanf("%d %d", &a, &b); vector<int> v(a + b, 0); for (int i = 0; i < a; i++) { v.at(i) = i + 1; } for (int i = a; i < a + b; i++) { v.at(i) = -1 * (i - a + 1); } if (a * (a + 1) / 2 - b * (b + 1) / 2 > 0) { v.at(a + b - 1) -= a * (a + 1) / 2 - b * (b + 1) / 2; } else { v.at(a - 1) -= a * (a + 1) / 2 - b * (b + 1) / 2;; } for (int i = 0; i < a + b; i++) { printf("%d ", v.at(i)); } printf("\n"); return 0; }
// Template begins #include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <stdio.h> #include <math.h> #include <iomanip> #include <queue> #include <string.h> #include <string> using namespace std; #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ll long long #define endl "\n" #define lb lower_bound #define ub upper_bound #define fo(i,a,b) for(i=a;i<=b;i++) #define all(v) (v).begin(),(v).end() #define sort0(v) sort(all(v)) #define lbb(a,b,arr,x) lower_bound(arr+a,arr+b+1,x)-arr #define ubb(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-arr #define freq(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-lower_bound(arr+a,arr+b+1,x) typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define d0(x) cout<<(x)<<" " #define d1(x) cout<<(x)<<endl #define d2(x,y) cout<<(x)<<" "<<(y)<<endl #define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl #define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl #define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl #define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl #define max3(a,b,c) max(max((a),(b)),(c)) #define max4(a,b,c,d) max(max((a),(b)),max((c),(d))) #define ss second #define ff first #define pb push_back #define mp make_pair #define printp(x) cout << x.ff << " " << x.ss << endl const ll mod=998244353; #define inf 9999999999999 #define MAXN 100001 // stores smallest prime factor for every number ll inv(ll i){if(i==1) return 1;return (mod-((mod/i)*inv(mod%i))%mod)%mod;} ll gcd(ll a,ll b){if(a==0) return b;return gcd(b,b%a);} inline void fastio(){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);} // Template ends const int MN = 2e5+7; ll val(ll a){ return ((a*(a+1))/2)%mod; } void solve(){ ll a,b,c; cin>>a>>b>>c; a%=mod; b%=mod; c%=mod; ll ans=val(a); ans%=mod; ans= (ans*val(b))%mod; ans=(ans*val(c))%mod; cout<<ans<<endl; } int main(){ fastio(); //write code int t; t=1; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define P pair<int,char> #define Tp tuple<int,int,int> using Graph = vector<vector<P>>; //Cの計算 C[i][j]でiCjを得る void combination(vector<vector <long long> > &v){ for(int i = 0;i <v.size(); i++){ v[i][0]=1; v[i][i]=1; } for(int k = 1;k <v.size();k++){ for(int j = 1;j<k;j++){ //modの法は問題により変更 v[k][j]=(v[k-1][j-1]+v[k-1][j])%998244353; } } } ll ptn(ll k,ll N,vector<vector<ll>> &C,vector<ll> &f){ ll a = 0; if(f[k]!=-1) return f[k]; if(k==0) return 1LL; if(k==2) return C[N][2]; for(ll i=0;i<=k;i += 2){ if((k-i)%4!=0) continue; a += (C[N][i]*ptn((k-i)/2,N,C,f))%998244353; a%= 998244353; } f[k] = a; return a; } int main(){ //main関数内に以下をペースト Kは計算が必要な最大の大きさ vector<vector<long long>> C(5001,vector<long long int>(5001,0)); combination(C); ll N,M; cin >> N >> M; vector<ll> f; rep(i,M+1){ f.push_back(-1); } ll ans = ptn(M,N,C,f); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; namespace ftr { vector<int> prime; void run_sieve(int n) { assert((int) prime.size() == 0); prime = vector<int>(n, 1); prime[0] = prime[1] = 0; for (int i = 2; i < n; i++) { if (prime[i]) { for (int j = i + i; j < n; j += i) { prime[j] = 0; } } } } template <typename T> vector<pair<T, int>> factorize(T x) { if (x <= 1) { return {}; } vector<pair<T, int>> ret; for (T i = 2; i * i <= x; i++) { T t = x / i; if (i > t) break; if (x == t * i) { int cnt = 0; while (x % i == 0) { x /= i; cnt++; } ret.emplace_back(i, cnt); } } if (x > 1) { ret.emplace_back(x, 1); } return ret; } } // namespace ftr int main() { ios::sync_with_stdio(false); cin.tie(0); ftr::run_sieve(100); vector<int> primes; int val = 2; while ((int) primes.size() < 20) { if (ftr::prime[val]) { primes.push_back(val); } ++val; } long long A, B; cin >> A >> B; const int N = (int) primes.size(); vector<long long> dp(1 << N); dp[0] = 1; for (long long val = A; val <= B; val++) { int bit = 0; for (int i = 0; i < N; i++) { if (val % primes[i] == 0) { bit |= (1 << i); } } for (int t = 0; t < (1 << N); t++) { if ((bit & t) == 0) { dp[bit ^ t] += dp[t]; } } } long long ans = 0; for (int i = 0; i < (1 << N); i++) { ans += dp[i]; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; //begin of def #define fastio ios_base::sync_with_stdio(false);cin.tie(0) #define endl '\n' using lli = long long int; using ulli = unsigned long long int; using Ld = long double; using pii = pair<int, int>; using pll = pair<lli, lli>; using pld = pair<Ld, Ld>; #define X first #define Y second #define rep(I, S, E) for(int I = (S); I < (E); I++) #define repq(I, S, E) for(int I = (S); I <= (E); I++) #define pb push_back #define epb emplace_back #define ALL(X) X.begin(), X.end() //end of def int main(){ fastio; int n; cin >> n; string a, b; cin >> a >> b; int az = 0, bz = 0; int ct = 0; rep(i, 0, n){ if(b[i] == '0'){ bz++; } if(a[i] == '0'){ az++; ct += (az != bz || a[i] != b[i]); } } if(az != bz) cout << -1; else cout << ct; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define eFOR(i,a,n) for(int i=(a);i<=(n);++i) #define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i) #define erFOR(i,a,n) for(int i=(n);i>=(a);--i) #define SORT(a) sort(a.begin(),a.end()) #define rSORT(a) sort(a.rbegin(),a.rend()) #define fSORT(a,f) sort(a.begin(),a.end(),f) #define all(a) a.begin(),a.end() #define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x)) #define tp(a,i) get<i>(a) #ifdef _DEBUG #define line cout << "-----------------------------\n" #define stop system("pause") #define debug(x) print(x) #endif constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; } template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); } template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } template<class T>inline void print(const T& a) { cout << a << "\n"; } template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); } template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); } inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; } template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); } template<class T>inline void END(T s) { print(s); exit(0); } void END() { exit(0); } int main() { init(); int n; cin >> n; VI a(n), b(n), p(n); cin >> a >> b >> p; set<pair<int, int>> s; // {持てる重さ、index} FOR(i, 0, n) { --p[i]; if (a[i] <= b[p[i]] && p[i] != i)END(-1); if (i != p[i]) { s.emplace(a[i], i); } } vector<pair<int, int>> ans; while (!s.empty()) { int cur = s.rbegin()->second; int to = p[cur]; ans.emplace_back(cur, to); if (p[to] == cur) { s.erase(pair<int, int>(a[cur], cur)); s.erase(pair<int, int>(a[to], to)); } else { s.erase(pair<int, int>(a[to], to)); p[cur] = p[to]; } } print(ans.size()); for (auto [x, y] : ans)print(x + 1, y + 1); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int dp[60][2]; int two_power(int x) { int mul=1; for(int i=0;i<x;i++) mul*=2; return mul; } int32_t main() { ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); dp[0][1]=1,dp[0][0]=0; int n;cin>>n; vector <string> s(n+1); for(int i=1;i<=n;i++) cin>>s[i]; for(int i=1;i<=n;i++) { for(int j=0;j<2;j++) { if(s[i]=="AND" and j==1) dp[i][j]=dp[i-1][0]+dp[i-1][1]; else if(s[i]=="AND" and j==0) dp[i][j]=0; else if(s[i]=="OR" and j==1) dp[i][j]=two_power(i); else if(s[i]=="OR" and j==0) dp[i][j]=dp[i-1][0]+dp[i-1][1]; } } cout<<dp[n][1]+dp[n][0]<<endl; return 0; } /* */
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define _overload3(_1,_2,_3,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(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define all(x) x.begin(),x.end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define V vector #define P pair<int,int> #define F first #define S second 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 ll = long long; using ld = long double; using namespace std; const int INF =1001001001; const int MAX = 2e6+1; ll in(){ double x; cin>>x; x*=10000; return round(x); } int main() { ll x=in(); ll y=in(); ll r=in(); x%=10000; y%=10000; ll ans=0; ll rr=r*r; ll y1=(y/10000)*10000; ll y2=y1+10000; ll right=0,left=0; while((x-right)*(x-right)+(y-y1)*(y-y1)<=rr){ right+=10000; } while((x-left)*(x-left)+(y-y1)*(y-y1)<=rr){ left-=10000; } while(y1>=y-r){ while((x-right)*(x-right)+(y-y1)*(y-y1)>rr){ right-=10000; } while((x-left)*(x-left)+(y-y1)*(y-y1)>rr){ left+=10000; } if(right>=left) ans+=(right-left+10000)/10000; y1-=10000; } right=0;left=0; while((x-right)*(x-right)+(y-y2)*(y-y2)<=rr){ right+=10000; } while((x-left)*(x-left)+(y-y2)*(y-y2)<=rr){ left-=10000; } while(y2<=y+r){ while((x-right)*(x-right)+(y-y2)*(y-y2)>rr){ right-=10000; } while((x-left)*(x-left)+(y-y2)*(y-y2)>rr){ left+=10000; } if(right>=left) ans+=(right-left+10000)/10000; y2+=10000; } cout<<ans<<endl; }
#include <bits/stdc++.h> template <typename T> inline void rd(T& x) { int si = 1; char c = getchar(); x = 0; while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar(); while(isdigit(c)) x = x * 10 + c - 48, c = getchar(); x *= si; } template <typename T, typename... Args> inline void rd(T& x, Args&... args) { rd(x); rd(args...); } #define fi first #define se second #define mkp std::make_pair typedef long long ll; typedef double ff; typedef std::pair <int, int> pii; const int N = 1e5 + 5, B = 14, Inf = 0x3f3f3f3f, Mod = 998244353; const ll InfLL = 0x3f3f3f3f3f3f3f3fLL; int QPow(int a, int b) { int ret = 1, bas = a; for(; b; b >>= 1, bas = 1LL * bas * bas % Mod) if(b & 1) ret = 1LL * ret * bas % Mod; return ret; } int n, m, f[B][N], fac[N], fac_inv[N]; void Init() { fac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = 1LL * fac[i - 1] * i % Mod; fac_inv[n] = QPow(fac[n], Mod - 2); for(int i = n - 1; ~i; --i) fac_inv[i] = 1LL * fac_inv[i + 1] * (i + 1) % Mod; } int C(int x, int y) { return 1LL * fac[x] * fac_inv[x - y] % Mod * fac_inv[y] % Mod; } void Calc() { f[0][0] = 1; for(int i = 1; i < B; ++i) { for(int j = 0; j <= m; ++j) { for(int k = 0; k * (1 << (i - 1)) <= j && k <= n; k += 2) f[i][j] = (1LL * f[i - 1][j - k * (1 << (i - 1))] * C(n, k) + f[i][j]) % Mod; } } } int main() { #ifndef ONLINE_JUDGE freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); #endif int test_case_cnt = 1; //rd(test_case_cnt); while(test_case_cnt--) { rd(n, m); Init(); Calc(); printf("%d\n", f[B - 1][m]); } return 0; }
#pragma GCC optimize(3) #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <queue> #include <algorithm> #define maxn 200010 using namespace std; int read() { int x=0,w=1; char ch=getchar(); while((ch>'9'||ch<'0')&&(ch!='-')) ch=getchar(); if(ch=='-') { w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); } return x*w; } int n; struct Code { int l,r,num; }code[maxn]; struct Heap { int l,r,num; }heap[maxn]; priority_queue <int> q; int ans,res; bool cmpl(Code x,Code y) { return x.l<y.l; } bool cmpr(Heap x,Heap y) { return x.r<y.r; } int main() { n=read(); for(register int i=1;i<=n;i++) { code[i].l=read(); code[i].r=read(); code[i].num=i; heap[i].l=code[i].l; heap[i].r=code[i].r; heap[i].num=i; } sort(code+1,code+n+1,cmpl); sort(heap+1,heap+n+1,cmpr); if(code[n].l-code[1].l>code[n].r-code[1].r) q.push(code[n].l-code[1].l); if(code[n-1].l-code[1].l>code[n-1].r-code[1].r) q.push(code[n-1].l-code[1].l); if(code[n].l-code[2].l>code[n].r-code[2].r) q.push(code[n].l-code[2].l); if(heap[n].r-heap[1].r>=heap[n].l-heap[1].l) q.push(heap[n].r-heap[1].r); if(heap[n-1].r-heap[1].r>=heap[n-1].l-heap[1].l) q.push(heap[n-1].r-heap[1].r); if(heap[n].r-heap[2].r>=heap[n].l-heap[2].l) q.push(heap[n].r-heap[2].r); q.pop(); cout<<q.top(); return 0; }
#include <algorithm> #include <array> #include <cassert> #include <chrono> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef NEAL_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #include <iostream> #include <vector> using namespace std; struct union_find { // When data[x] < 0, x is a root and -data[x] is its tree size. When data[x] >= 0, data[x] is x's parent. vector<int> data; vector<int> edges; int components = 0; union_find(int n = -1) { if (n >= 0) init(n); } void init(int n) { data.assign(n + 1, -1); edges.assign(n + 1, 0); components = n; } int find(int x) { return data[x] < 0 ? x : data[x] = find(data[x]); } int get_size(int x) { return -data[find(x)]; } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) { edges[x]++; return false; } if (-data[x] < -data[y]) swap(x, y); data[x] += data[y]; data[y] = x; edges[x] += edges[y]; edges[x]++; components--; return true; } }; const int V_MAX = 400005; int main() { ios::sync_with_stdio(false); #ifndef NEAL_DEBUG cin.tie(nullptr); #endif int N; cin >> N; union_find UF(V_MAX); for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; UF.unite(a, b); } int answer = 0; for (int i = 0; i < V_MAX; i++) if (UF.find(i) == i) answer += min(UF.get_size(i), UF.edges[i]); cout << answer << '\n'; }
#include <bits/stdc++.h> #define rep(i,n) for (ll i = 0; i < (n); ++i) #define All(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() //reverse #define strall(v) (v).cbegin(),(v).cend() //const_itterator #define IN(a, b, x) (a<=x&&x<b) using namespace std; using ll = long long; using Pair = pair<int,int>; using Graph = vector<vector<ll>>; template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; template<typename t, typename u, typename Comp=less<>> bool chmax(t& xmax, const u& x, Comp comp={}) { if(comp(xmax, x)) { xmax = x; return true; } return false;} template<typename t, typename u, typename Comp=less<>> bool chmin(t& xmin, const u& x, Comp comp={}) { if(comp(x, xmin)) { xmin = x; return true;} return false;} const int INF = 1e9; const ll infl = ll(1e18)+5; int main(){ int M,H; cin >> M >> H; if(H%M == 0) cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define endl '\n' typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const ll linf = 0x3f3f3f3f3f3f3f3f; int g(string a) { int t=0; for(int i=0;i<a.size();i++) { t=t*10+a[i]-'0'; } return t; } int f(int x) { string s; while(x) { s+=x%10+'0'; x/=10; } sort(s.begin(),s.end()); int ans1=g(s); reverse(s.begin(),s.end()); int ans2=g(s); return ans2-ans1; } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n,k; cin>>n>>k; int res=n; for(int i=1;i<=k;i++) { res=f(res); } cout<<res<<endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)n;++i) #define irep(i,a,b) for(int i=int(a);i<(int)b;++i) #define rrep(i,a,b) for(int i=int(a);i>=(int)b;--i) #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vector<ll>> #define vvp vector<vector<pair<ll,ll>>> #define vpl vector<pair<ll,ll>> #define vpi vector<pair<int,int>> #define pb push_back #define se second #define fi first #define all(v) v.begin(),v.end() #define v(T) vector<T> #define vv(T) vector<vector<T>> using namespace std; template<typename T> istream& operator>>(istream&i,v(T)&v){rep(j,v.size())i>>v[j];return i;} template<typename T> string join(const v(T)&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);} template<typename T> ostream& operator<<(ostream&o,const v(T)&v){if(v.size())o<<join(v);return o;} using ll = long long; const ll INF = LLONG_MAX-1e9; const double PI = acos(-1); const ll mod = 1e9 + 7; //998244353; 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; } ll modpow(ll a,ll b){ if(b == 0){ return 1; } if(b%2 == 0){ ll tmp = modpow(a,b/2); return tmp*tmp%mod; }else{ return modpow(a,b-1)*a%mod; } } int main(void) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; cin >> n; vector<pair<double,double>>ps; rep(i,n){ int t,l,r; cin >> t >> l >> r; if(t == 1){ ps.pb({l,r+0.1}); }else if(t == 2){ ps.pb({l,r}); }else if(t == 3){ ps.pb({l+0.1,r+0.1}); }else{ ps.pb({l+0.1,r}); } } int ans = 0; rep(i,n)for(int j = i+1;j < n; ++j){ auto p1 = ps[i],p2 = ps[j]; if(p1.first < p2.first){ if(p1.second > p2.first)ans++; }else if( p1.first > p2.first){ if(p2.second > p1.first)ans++; }else ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long int #define w(t) int t; cin>>t; while(t--) #define F first #define S second #define pb push_back #define mp make_pair #define pii pair<int,int> #define mii map<int,int> #define sp(x,y) fixed<<setprecision(y)<<x using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); ll a,b,c; cin>>a>>b>>c; if(a==b)cout<<c; else if(a==c)cout<<b; else if(b==c)cout<<a; else cout<<0; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> ii; typedef tuple<ll, ll, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define REP(i,n) for (ll i = 0; i < n; ++i) #define REPR(i,n) for (ll i = n-1; i >= 0; --i) #define FOR(i,m,n) for (ll i = m; i < n; ++i) #define FORR(i,m,n) for (ll i = n-1; i >= m; --i) #define FORE(x,xs) for (const auto& x : xs) #define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++) #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REV(v) reverse(ALL(v)) #define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v)) #define CHMIN(x,y) x = min(x, y) #define CHMAX(x,y) x = max(x, y) #define YES(b) cout << ((b) ? "YES" : "NO") << endl #define Yes(b) cout << ((b) ? "Yes" : "No") << endl struct UnionFind { private: int N; vi parent; vi rank_; void init(int n) { N = n; parent.resize(0); parent.clear(); parent.resize(n); REP (i, N) parent[i] = i; rank_.resize(0); rank_.clear(); rank_.resize(n); } public: UnionFind() { init(0); } UnionFind(int n) { init(n); } int root(int x) { return (parent[x] == x) ? x : parent[x] = root(parent[x]); } bool isSame(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank_[x] < rank_[y]) parent[x] = y; else { parent[y] = x; if (rank_[x] == rank_[y]) rank_[x]++; } } }; const int MAX = 111; int N; int X[MAX], Y[MAX]; bool check(ld r) { UnionFind uf(N+2); REP (i, N) REP (j, i) { ld dist = pow(pow(X[i]-X[j],2) + pow(Y[i]-Y[j],2), .5); if (dist < 2*r) uf.merge(i, j); } REP (i, N) { if (100 - Y[i] < 2*r) uf.merge(i, N); if (Y[i] + 100 < 2*r) uf.merge(i, N+1); } return !uf.isSame(N, N+1); } ld solve() { ld ok = 0, ng = 200; REP (i, 100) { ld m = (ok + ng) / 2; if (check(m)) ok = m; else ng = m; } return ok; } int main() { cout << fixed << setprecision(15); cin >> N; REP (i, N) cin >> X[i] >> Y[i]; cout << solve() << endl; }
#include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int N = 200; int a[N], b[N], c[N], d[N]; int main() { int n; cin >> n; for (int _ = 0; _ < 2; _ ++) { for (int i = 0; i < n; i ++) cin >> a[i] >> b[i]; int x = 0, y = 0; for (int i = 0; i < n; i ++) { x += a[i]; y += b[i]; a[i] *= n; b[i] *= n; } for (int i = 0; i < n; i ++) { a[i] -= x; b[i] -= y; } swap(a, c); swap(b, d); } for (int i = 0; i < n; i ++) if (a[i] || b[i]) { swap(a[i], a[0]); swap(b[i], b[0]); } string ans = "No"; double eps = 1e-6; for (int i = 0; i < n; i ++) { double angle = atan2(d[i], c[i]) - atan2(b[0], a[0]); bool flag = true; for (int j = 0; j < n; j ++) { double u = a[j] * cos(angle) - b[j] * sin(angle); double v = a[j] * sin(angle) + b[j] * cos(angle); bool flag2 = false; for (int k = 0; k < n; k ++) if (abs(u - c[k]) <= eps && abs(v - d[k]) < eps) flag2 = true; flag &= flag2; } if (flag) ans = "Yes"; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() long long n,ans; string s,t; set<int> s1; template<typename T> void fin(T a){ cout<<a<<endl; exit(0); } const int INF=1e9; void f(int idx){ if(idx==n-1)fin(-1); int p=*s1.upper_bound(idx); if(p==INF)fin(-1); ans+=p-idx; s[p]='0'; s[idx]^=1; s1.erase(p); } /* f(idx)はidxを変えたいからidxより大きくて最初の1に対して操作を行いたい訳で 0が続く場合はそこがガッと移動するから直近の1を移動させるイメージ */ int main(){ cin>>n>>s>>t; REP(i,n)if(s[i]=='1')s1.insert(i); s1.insert(INF); REP(i,n-1)if(s[i]!=t[i])f(i); if(s!=t)fin(-1); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define pb push_back #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define rep(i, n) for (int i = 0; i < (int)(n); ++ i) int main() { int n; string s, t; cin >> n >> s >> t; vector <int> vs, vt; rep(i, n) if (s[i] == '1') vs.pb(i); rep(i, n) if (t[i] == '1') vt.pb(i); if (vs.size() < vt.size() || (vs.size() - vt.size()) % 2 == 1) { printf("-1\n"); return 0; } ll ans = 0; int j = 0; rep(i, vs.size()) { if (j < vt.size() && vs[i] >= vt[j]) { ans += vs[i] - vt[j]; ++ j; } else { if (i + 1 == vs.size()) { printf("-1\n"); return 0; } ans += vs[i + 1] - vs[i]; ++ i; } } if (j < vt.size()) { printf("-1\n"); return 0; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define endl "\n" using namespace std; #define int long long int #define watch(x) cout << (#x) << " is " << (x) << "\n" #define watch2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << "\n" #define INF LLONG_MAX template<typename ... T> void print(const T& ... t) { initializer_list<int>{ (std::cout << t << " ", 0)... }; cout << "\n"; } template <typename T, size_t SIZE> inline void zero(T(&arr)[SIZE]) { memset(arr, 0, SIZE * sizeof(T)); } void readFile() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE time_t ttt ; struct tm *tmptmptmp ; char MY_TIME[50]; time( &ttt ); tmptmptmp = localtime( &ttt ); strftime(MY_TIME, sizeof(MY_TIME), "%I:%M:%S %p", tmptmptmp); print("-----------"); print(MY_TIME); print("-----------"); #endif } void smallestLeftandRight(vector<int> &input, vector<int> &left, vector<int> &right) { int sz = input.size(); left.resize(sz); right.resize(sz); input.insert(input.begin(), 1, -INF); input.push_back(-INF); stack<int> s; stack<int> pos; int idx = -1; for(auto val : input) { if(pos.empty()) { s.push(val); pos.push(idx); ++idx; continue; } while(!s.empty() and s.top() > val) { s.pop(); if(pos.top() != -1) { right[pos.top()] = idx; } pos.pop(); } if(s.top() == val) { if(pos.top() != -1) { left[idx] = left[pos.top()]; } } else { left[idx] = pos.top(); } s.push(val); pos.push(idx); ++idx; } } int maxAreaHistogram(vector<int> &input, vector<int> &left, vector<int> &right) { //Note: We have manipulated the original input array; int maxArea = 0; for(int idx = 0; idx < left.size(); ++idx) { int area = input[idx+1] * (right[idx] - left[idx] - 1); maxArea = max(maxArea, area); } return maxArea; } int32_t main() { readFile(); int n; cin >> n; vector<int> test, left, right; for(int i = 0; i < n; ++i) { int temp; cin >> temp; test.push_back(temp); } smallestLeftandRight(test, left, right); cout << maxAreaHistogram(test, left, right) << endl; }
#include <iostream> #include <iomanip> #include <vector> #include <set> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <numeric> #include <list> #include <stack> #include <cstdio> #include <cstdlib> #include <cstring> #include <tuple> #include <deque> #include <complex> #include <bitset> #include <functional> #include <cassert> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using ld = long double; using vld = vector<ld>; using vb = vector<bool>; #define rep(i, n) for (ll i = 0; i < (n); i++) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif template <class T> bool chmin(T& a, T b) { if(a > b) { a = b; return true; } else return false; } template <class T> bool chmax(T& a, T b) { if(a < b) { a = b; return true; } else return false; } template <class T> ostream& operator<<(ostream& s, const vector<T>& a) { for(auto i : a) s << i << ' '; return s; } constexpr ll INFL = 1LL << 60; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); void solve() { ll n; cin >> n; stack<pll> st; ll ans = 0; vll A(n+1); rep(i, n) cin >> A[i]; rep(i, n+1) { ll a = A[i]; if(st.empty()) st.emplace(a, i); else if(a > st.top().first) st.emplace(a, i); else if(a < st.top().first) { ll l = i; while(!st.empty() && a < st.top().first) { pll p = st.top(); st.pop(); chmax(ans, p.first * (i - p.second)); l = p.second; } st.emplace(a, l); } } cout << ans << endl; return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define e(v) sort(v.begin(), v.end()) #define rev(v) reverse(v.begin(), v.end()) #define out(s) cout << s << endl #define decimal10(s) cout << fixed << setprecision(10) << s << endl; typedef long long ll; using Graph = vector<vector<int>>; const int MOD = 1000000007; int main(){ float a, b; cin >> a >> b; float c = 100.0 - (b / a) * 100; decimal10(c); }
#include<bits/stdc++.h> #include<iostream> #include<string> #include<cmath> #include<cstdio> #include<cctype> #include<cstring> #include<iomanip> #include<cstdlib> #include<ctime> #include<set> #include<map> #include<utility> #include<queue> #include<vector> #include<stack> #include<sstream> #include<algorithm> /************************************************/ #define rep(i,n) for(int i=0;i<n;i++) #define m_p make_pair #define pb push_back #define fr first #define se second #define ford(i,n) for(int i=n-1;i>=0;i--) #define forn(i,a,n) for(int i=a;i<n;i++) #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++) #define pib pair<int,bool> #define vi vector<int> #define ll long long #define vll vector<ll> #define sz(s) (int)(s.size()) #define all(s) s.begin(),s.end() #define zero(x) memset(x,0,sizeof(x)) #define vib vector<pib> #define mpis map<int,string> #define mpii map<int,int> #define mpsi map<string,int> #define re return #define mod 1000000007 /************************************************/ using namespace std; long long get(){ char c=getchar(); long long x=0LL; while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9'){ x*=10LL; x+=(c-'0'); c=getchar(); } return x; } string i_t_s(int x){ string t=""; while(x){ t+=x%10+'0'; x/=10; } reverse(all(t)); re t; } int s_t_i(string t){ int x=0; rep(i,sz(t)){ x=x*10+(t[i]-'0'); } re x; } ll q_p(ll x,ll y){ ll res=1; x%=mod; while(y){ if(y%2){ res=res*x; res%=mod; } y/=2; x=x*x; x%=mod; } re res; } vi v[100001]; int n; int p; pib ans(int x){ ll now=0,s1=0,s2=0; vi e; bool f=1; rep(i,sz(v[x])){ pib nn=ans(v[x][i]); if(nn.se){ e.pb(nn.fr); f^=1; }else{ if(nn.fr>0) s1+=nn.fr; else s2+=nn.fr; } } sort(all(e)); now++; now+=s2; rep(i,sz(e)){ if(i%2){ now-=e[i]; }else now+=e[i]; } if(sz(e)%2) now-=s1; else now+=s1; re m_p(now,f); } int main(){ ios::sync_with_stdio(0); cin>>n; forn(i,1,n){ cin>>p; p--; v[p].pb(i); } cout<<(ans(0).fr+n)/2; re 0; } /* 检查循环是rep(i,n)还是rep(i,m)!! long long所要的时间比int长!! 没有long long必要不写long long!! */
/* I love the sound you make when you shut up. */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; /*----typedefs--------*/ typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; using ll = long long; using pi = pair<int, int>; /*-----in and out--------*/ #define pf(a) cout << a << endl #define forIn(arr, num) for(int i = 0; i < num; i++) cin >> arr[i]; #define vpnt(ans) for(int i = 0; i < int(ans.size()); i++) cout << ans[i] << (i + 1 < int(ans.size()) ? ' ' : '\n'); /*---useful defines------*/ #define sz(x) (int)(x).size() #define pb push_back #define mem(a, b) memset(a,(b), sizeof(a)) #define ff first #define ss second #define all(x) x.begin(), x.end() /*----- the binary answer of life-----*/ #define no cout << "NO" << endl #define yes cout << "YES" << endl /*---checking and pushing-----*/ template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } /*---- FAST I/O and file read ---*/ void go() { 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 } /*-------- movement in a 2D array ------*/ const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; /*--------test-case stuff---------------*/ #define ssolve solve(); #define msolve int t;cin >> t;while(t--) {solve();} #define mcsolve int t;cin >> t;for(int tt = 1;tt <= t;tt++) {cout << "Case #" << tt << ": ";solve();} /*----------------------------------------------------------------*/ const int MOD = 1e9 + 7; const int N = 2e5 + 5; /*-------------- Push your limits here ---------------------------*/ int n, m, k; int cnt[N]; void add(int x) { for(int i=x;i<=m;i+=(i&(-i))){ ++cnt[i]; } } int ask(int x) { int ret=0; for(int i=x;i>0;i-=(i&(-i))){ ret+=cnt[i]; } return ret; } void solve() { cin>>n>>m>>k; vector<int> a(n+1),b(m+1); for(int i = 1;i <= n;i++) a[i]=m; for(int i = 1;i <= m;i++) b[i]=n; if(!k){ cout<<1ll*n*m<<endl; return; } for(int i = 0;i < k;i++) { int x,y; cin>>x>>y; a[x]=min(a[x],y-1); b[y]=min(b[y],x-1); } ll ans=0; for(int i = 1;i <= b[1];i++) ans+=a[i]; vector<int> pos(a[1]+1); for(int i = 1;i <= a[1];i++) pos[i]=i; sort(pos.begin()+1,pos.end(),[&](int x,int y){ return b[x]<b[y]; }); for(int i = 1;i <= a[1];i++) { int r=pos[i],l=pos[i-1]; for(int j=b[l]+1;j<=b[r]&&j<=b[1];j++){ add(a[j]); } ans+=b[r]+ask(r-1)-ask(m); } pf(ans); } int main() { //go(); ssolve return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> struct binary_indexed_tree{ int N; vector<T> BIT; binary_indexed_tree(int n){ N = 1; while (N < n){ N *= 2; } BIT = vector<T>(N + 1, 0); } void add(int i, T x){ i++; while (i <= N){ BIT[i] += x; i += i & -i; } } T sum(int i){ T ans = 0; while (i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } T query(int L, int R){ return sum(R) - sum(L); } T all(){ return BIT[0]; } }; int main(){ int H, W, M; cin >> H >> W >> M; vector<int> X(M), Y(M); for (int i = 0; i < M; i++){ cin >> Y[i] >> X[i]; X[i]--; Y[i]--; } vector<int> right(H, W); for (int i = 0; i < M; i++){ right[Y[i]] = min(right[Y[i]], X[i]); } vector<int> down(W, H); for (int i = 0; i < M; i++){ down[X[i]] = min(down[X[i]], Y[i]); } binary_indexed_tree<long long> BIT(H); for (int i = 0; i < down[0]; i++){ BIT.add(i, 1); } vector<vector<int>> s(W); for (int i = 0; i < down[0]; i++){ if (right[i] != W){ s[right[i]].push_back(i); } } long long ans = 0; for (int i = 0; i < down[0]; i++){ ans += right[i]; } for (int i = 0; i < right[0]; i++){ for (int j : s[i]){ BIT.add(j, -1); } ans += down[i] - BIT.sum(down[i]); } cout << ans << endl; }
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <stack> #include <queue> #include <map> #include <cstring> #include <bitset> #include <array> using namespace std; typedef long long ll; const int maxn=2e5+5, mod=998244353, maxp=305, Log=30; inline int sum(int a, int b){ if(a+b>=mod){ return a+b-mod; } if(a+b<0){ return a+b+mod; } return a+b; } inline int mul(int a, int b){ return (ll)a*b%mod; } int pote(int a, int b){ int sol=1; for(int i=0; i<Log; i++){ if((1<<i)&b){ sol=mul(sol, a); } a=mul(a, a); } return sol; } int inv; int a[maxn]; int pot[maxn][maxp]; int sumb[maxn][maxp]; int sump[maxp]; int n; void precompute(){ int suma; for(int i=0; i<n; i++){ pot[i][1]=a[i]; sumb[i][0]=n-1; for(int j=2; j<maxp; j++){ pot[i][j]=mul(pot[i][j-1], a[i]); } } sump[0]=n; for(int i=1; i<maxp; i++){ suma=0; for(int j=0; j<n; j++){ suma=sum(suma, pot[j][i]); } sump[i]=suma; for(int j=0; j<n; j++){ suma=sum(suma, -pot[j][i]); sumb[j][i]=suma; } } inv=pote(2, mod-2); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k; cin >> n >> k; for(int i=0; i<n; i++){ cin >> a[i]; } precompute(); vector < int > pasc={1, 1}; vector < int > vi; int sol; int pota, potb; int jed; for(int i=0; i<k; i++){ sol=0; pota=i+1; potb=0; for(int j=0; j<(int)pasc.size()-1; j++){ sol=sum(sol, mul(pasc[j], sum(mul(sump[pota], sump[potb]), -sump[i+1]))); if(!j){ jed=sol; } pota--; potb++; } sol=sum(sol, -jed); sol=mul(sol, inv); sol=sum(sol, jed); cout << sol << '\n'; for(int j=0; j<(int)pasc.size(); j++){ if(j){ vi.push_back(sum(pasc[j], pasc[j-1])); } else{ vi.push_back(pasc[j]); } } vi.push_back(pasc.back()); pasc=vi; vi.clear(); } return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cmath> #include <iomanip> #include <queue> #include <map> using namespace std; using ll = long long int; using P = pair<int, int>; int main(){ int n, m; cin >> n >> m; vector<int> a(n); for(auto &p: a) cin >> p; a.push_back(n+10); map<int, int> memo; vector<int> exist(n, 0); for(int i = 0; i <= n; i++) memo[i] = 1; int ans = 1001001001; for(int i = 0; i < m; i++){ exist[a[i]] += 1; } for(int i = 0; i <= n; i++){ if(exist[i] > 0){ memo.erase(i); } } ans = memo.begin()->first; //cerr << ans << endl; for(int i = m; i < n; i++){ exist[a[i]] += 1; memo.erase(a[i]); exist[a[i-m]] -= 1; if(exist[a[i-m]] == 0) memo[a[i-m]] = 1; ans = min(memo.begin()->first, ans); } cout << ans << endl; return 0; }
#include <iostream> #include <sstream> #include <string> #include <list> #include <vector> #include <queue> #include <algorithm> #include <climits> #include <cstring> #include <cmath> #include <stack> #include <iomanip> #include <tuple> #include <functional> #include <cfloat> #include <map> #include <set> #include <array> #include <stdio.h> #include <string.h> #include <random> #include <cassert> using ll = long long; using ull = unsigned long long; using uint = unsigned int; using namespace std; #define int long long #define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end()) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ARY_SORT(a, size) sort((a), (a)+(size)) #define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end())) #define REVERSE(v) (reverse((v).begin(), (v).end())) #define ARY_REVERSE(v,a) (reverse((v), (v)+(a))) #define REP(i, n) for (int (i)=0; (i) < (n); (i)++) #define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++) #define CONTAINS_MAP(m, a) ((m).find((a)) != m.end()) #define CONTAINS_SET(m, a) ((m).find((a)) != m.end()) void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); } void Yes() { cout << "Yes" << endl; exit(0); } void No() { cout << "No" << endl; exit(0); } int N; int S[200001]; char X[200001]; int memo[200010][10]; bool func(int depth, int a) { if (depth == N) { return a % 7 == 0; } if (memo[depth][a] >= 1) { return memo[depth][a] == 1; } int a1 = a * 10; int a2 = a * 10 + S[depth]; a1 %= 7; a2 %= 7; bool f1 = func(depth + 1, a1); bool f2 = func(depth + 1, a2); bool ans; if (X[depth] == 'T') { ans = f1 || f2; } else { ans = f1 && f2; } memo[depth][a] = ans ? 1 : 2; return ans; } signed main() { cin >> N; string s; cin >> s; REP(i, N) { S[i] = s[i] - '0'; } cin >> X; bool f = func(0, 0); cout << (f ? "Takahashi" : "Aoki") << endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair<int,int>; using vv = vector<vector<int>>; const int INF = (int)1e9; const LL LINF = (LL)1e18; int main(){ int N; string S, X; cin >> N >> S >> X; reverse(S.begin(), S.end()); reverse(X.begin(), X.end()); vv dp(N + 1); dp[0].emplace_back(0); rep(i,N){ rep(j,7){ int n = S[i] - '0'; int k1 = (j * 10 + n) % 7, k2 = j * 10 % 7; auto itr1 = find(dp[i].begin(), dp[i].end(), k1); auto itr2 = find(dp[i].begin(), dp[i].end(), k2); if(X[i] == 'A'){ if(itr1 != dp[i].end() and itr2 != dp[i].end()){ dp[i+1].emplace_back(j); } } else{ if(itr1 != dp[i].end() or itr2 != dp[i].end()){ dp[i+1].emplace_back(j); } } } } rep(i,dp[N].size()){ if(dp[N][i] == 0){ cout << "Takahashi" << endl; return 0; } } cout << "Aoki" << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const long long INF = 1LL<<60; 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; } #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define rep1(ff, aa) for(auto ff : aa) #define rep2(ff, aa) for(auto &ff : aa) #define ALL(a) (a).begin(),(a).end() using pii = pair<int, int>; using Graph = vector<vector<ll>>; void daiku(const vector<ll> &kyori,const vector<vector<pii>> &gg,const ll ss,vector<ll> &dis){ ll nn=gg.size(); vector<ll> rr(nn,INF); dis=rr; priority_queue<pii,vector<pii>,greater<pii>> pq; dis[ss]=0; pq.emplace(dis[ss],ss); while(!pq.empty()){ auto ww=pq.top(); pq.pop(); ll genzai=ww.second; if(ww.first>dis[genzai]) continue; for(auto &ee:gg[genzai]){ if(chmin(dis[ee.first],dis[genzai]+kyori[ee.second])){ pq.emplace(dis[ee.first],ee.first); } } } } int main(){ ll nn,mm,start; cin>>nn>>mm; vector<vector<pii>> gg(nn); vector<ll> kyori(mm); vector<ll> ans(nn,INF); vector<vector<pii>> wwee(nn); ll ss,tt,dd; rep(i,mm){ cin>>ss>>tt>>dd; ss--; tt--; gg[ss].push_back({tt,i}); wwee[tt].push_back({ss,i}); kyori[i]=dd; } ll aaee=INF; rep(i,nn){ aaee=INF; daiku(kyori,gg,i,ans); for(auto ee : wwee[i]){ chmin(aaee,(ans[ee.first]+kyori[ee.second])); } if(aaee>=INF){ cout<<-1<<endl; }else{ cout<<aaee<<endl; } } }
/* Stay motivated and keep working hard */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define int long long #define pb push_back #define cntbit __builtin_popcount #define fr first #define mem(arr, val) memset(arr, val, sizeof(arr)) #define sc second #define sz(v) ((int)(v).size()) #define mp make_pair #define all(a) a.begin(),a.end() #define rep(i,a,n) for(int i=a;i<n;i++) #define repd(i,a,p) for(int i=a-1;i>=p;i--) #define md 998244353 #define vi vector<int> #define vp vector<pair<int,int> > #define ml map<int,int> #define MPI 3.1415926536 #define in insert #define endl "\n" #define ub upper_bound #define lb lower_bound #define gcd __gcd #define setval(a,b) memset(a,b,sizeof(a)) #define lwr lower_bound #define pii pair<int,int> #define upr upper_bound #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> //CANDIDATE MASTER TOH BNKE RHUNGA //dsu is pel concept //KMP is pel concept int k,n,t,i,j=0,m; const int N=2e3+5; int md2; inline void add(int &a, int b) { a += b; if (a >= md) a -= md; } inline void sub(int &a, int b) { a -= b; if (a < 0) a += md; } inline int mul(int a, int b) { return (int) ((long long) a * b % md); } inline int power(int a, long long b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } inline int inv(int a) { a %= md; if (a < 0) a += md; int b = md, u = 0, v = 1; while (a) { int t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } if (u < 0) u += md; return u; } vp graph[N]; const int inf=1e9; signed main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=m;i++) { int a,b,c; cin>>a>>b>>c; graph[a].pb({b,c}); } for(int i=1;i<=n;i++) { priority_queue <pii,vp,greater<pii>> pq; pq.push({0,i}); vector <int> dist(n+1,inf); while(pq.size()) { pii p=pq.top(); pq.pop(); for(auto it:graph[p.sc]) { if(dist[it.fr]>p.fr+it.sc) { dist[it.fr]=p.fr+it.sc; pq.push({dist[it.fr],it.fr}); } } } if(dist[i]==inf) cout<<-1; else cout<<dist[i]; cout<<endl; } }
#include<iostream> #include<string> using namespace std; int main() { string s; cin>>s; if(s[0]==s[1]&&s[1]==s[2]) { cout<<"Won"; return 0; } else { cout<<"Lost"; return 0; } }
#include <bits/stdc++.h> #include <stdio.h> using namespace std; #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define sz(x) (int)x.size() #define f first #define s second #define bug cout << "ok\n"; #define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout); #define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long const long double pi = 3.14159265358979323846264; const long long maxn = 2e5 + 1, mod = 1e9 + 7, inf = 3e18; void solve(){ char c1, c2, c3; cin >> c1 >> c2 >> c3; if(c1 == c2 && c2 == c3)cout << "Won"; else cout << "Lost"; } main(){ //file(""); speed; int qq = 1; //cin >> qq; while(qq--)solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define make_it_fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define mp make_pair #define pb push_back #define all(x) (x).begin(),(x).end() #define ll long long #define ld long double #define endl "\n" #define ff first #define ss second #define imn INT_MIN #define imx INT_MAX ld pi=3.14159265358979323846; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) 20 #endif ll mod=1e9+7; vector<ll> seg,fac; ll power(ll a,ll b,ll m) { a%=m; if(b==1) return a; if(b==0) return 1LL; ll ret=power(a,b/2,m); ret=(ret*ret)%m; if(b&1) ret=(ret*a)%m; return ret; } ll power(ll a,ll b) { if(b==1) return a; if(b==0) return 1LL; ll ret=power(a,b/2); ret=(ret*ret); if(b&1) ret=(ret*a); return ret; } ll lcm(ll a,ll b) { return (a*b)/(__gcd(a,b)); } ll phi(ll n) // Euler-Totient Function { ll result = n; for(ll i=2;i*i<=n;i++) { if (n%i==0) { while (n%i==0) n/=i; result/=i; result*=(i-1); } } if (n > 1) { result/=n; result*=(n-1); } return result; } ll findpar(ll x,ll par[]) { while(x!=par[x]) { x=par[x]; } return x; } ll construct(ll a[],ll s,ll e,ll i) { if(s==e) { seg[i]=a[s]; return seg[i]; } ll mid=s+(e-s)/2; seg[i]=construct(a,s,mid,2*i+1)^construct(a,mid+1,e,2*i+2); return seg[i]; } void update(ll node,ll val,ll s,ll e,ll i) { if(s==e) { seg[i]=val; return; } ll mid=s+(e-s)/2; if(node<=mid) { update(node,val,s,mid,2*i+1); } else { update(node,val,mid+1,e,2*i+2); } seg[i]=seg[2*i+1]^seg[2*i+2]; } ll query(ll l,ll r,ll s,ll e,ll i) { if(e<l || s>r) return 0; if(s>=l && e<=r) return seg[i]; ll mid=s+(e-s)/2; return (query(l,r,s,mid,2*i+1)^query(l,r,mid+1,e,2*i+2)); } void computeFac(ll mx) { fac.resize(mx+10); fac[0]=1; for(ll i=1;i<mx+10;i++) { fac[i]=(fac[i-1]*i)%mod; } } ll ncr(ll a,ll b) { if(a<b || b<0) return 0; ll x=(fac[a-b]*fac[b])%mod; return (fac[a]*power(x,mod-2,mod))%mod; } void answer_nikaal() { string s; cin>>s; ll i=0; while(i<s.size() && s[i]!='.') cout<<s[i],i++; } int main() { make_it_fast; int TEST_CASES=1; // cin>>TEST_CASES; while(TEST_CASES--) { answer_nikaal(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int main() { fast; string s; cin>>s; for(int i=0;i<s.size();i++) { if(s[i]!='.') { cout<<s[i]; } else { break; } } }
#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-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// int main() { ll K, N, M; cin >> K >> N >> M; vl A(K); rep(i, K) cin >> A[i]; ll ng = -1, ok = MOD * MOD; vl ansB; while(ok - ng > 1){ ll mid = (ok + ng) / 2; ll dcnt = 0, ucnt = 0; rep(i, K){ dcnt += max(0ll, (M * A[i] - mid + N - 1) / N); ucnt += (M * A[i] + mid) / N; } if(!(dcnt <= M and M <= ucnt)) { ng = mid; continue; } ok = mid; ll rest = M; vl B(K); rep(i, K) { B[i] = max(0ll, (M * A[i] - mid + N - 1) / N); rest -= B[i]; } rep(i, K){ ll d = (M * A[i] + mid) / N - B[i]; chmin(d, rest); B[i] += d, rest -= d; } ansB = B; } repa(b, ansB) cout << b << " "; cout << endl; // err(ok); }
#include <bits/stdc++.h> using namespace std; #define fr(i,n) for(int i = 0; i<n; i++) #define sz(v) (int)(v.size()) #define prin(a) cout << #a << " = " << a << endl #define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl #define all(v) (v).begin(),(v).end() typedef long long ll; #define rmin(a,b) a = min<ll>(a,b) #define rmax(a,b) a = max<ll>(a,b) #define fi first #define se second const int N =1010; vector<int> g[26][N]; int vis[N][N], nb[N][N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; fr(i,m){ int a, b; char c; cin >> a >> b >> c; a--,b--; int ic= c-'a'; g[ic][a].push_back(b); g[ic][b].push_back(a); nb[a][b] = 1; nb[b][a] = 1; } fr(i,n) nb[i][i] = 1; int INF = INT_MAX/2; int ans = INF; queue<tuple<int,int,int>> q; q.emplace(0,0,n-1); vis[0][n-1] = 1; while(!q.empty()){ int dac, a, b; tie(dac,a,b) = q.front(); q.pop(); if(nb[a][b]){ if(a==b) rmin(ans,dac); else rmin(ans,dac+1); } if(ans+2<dac) break; fr(ic,26){ for(auto ann : g[ic][a]) for(auto bnn : g[ic][b]){ int an = ann, bn = bnn; if(an>bn) swap(an,bn); if(vis[an][bn]) continue; q.emplace(dac+2,an,bn); vis[an][bn] = 1; } } } if(ans==INF) ans = -1; cout << ans << "\n"; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template<class T, class S> inline T pow_L(T a, S b){ T res = 1; res = 1; for(;;){ if(b&1){ res *= a; } b >>= 1; if(b==0){ break; } a *= a; } return res; } inline double pow_L(double a, double b){ return pow(a,b); } unsigned long long powmod(unsigned long long a, unsigned long long b, unsigned long long m){ unsigned long long r = 1; while(b){ if(b&1){ r = r * a % m; } b>>=1; a = a * a % m; } return r; } int main(){ long long A; rd(A); long long B; rd(B); long long C; rd(C); long long k; long long res; if(pow(B,C) > 10){ k = powmod(B, C, 4) + 4; } else{ k =(pow_L(B,C)); } res = powmod(A, k, 10); wt_L(res); wt_L('\n'); return 0; } // cLay version 20210103-1 [bug fixed 4] // --- original code --- // { // ll @A, @B, @C, k, res; // if(pow(B,C) > 10){ // k = powmod(B, C, 4) + 4; // } else { // k = B ** C; // } // res = powmod(A, k, 10); // wt(res); // }
#include<bits/stdc++.h> //#include<ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/tree_policy.hpp> #define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned ll #define ld long double #define pb push_back #define pf push_front #define dll deque<ll> #define vll vector<ll> #define vvll vector<vll> #define pll pair<ll,ll> #define vpll vector<pll> #define dpll deque<pll> #define mapll map<ll,ll> #define umapll umap<ll,ll> #define endl "\n" #define all(v) v.begin(),v.end() #define ms(a,x) memset(a,x,sizeof(a)) //#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; //using namespace __gnu_pbds; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return move(v); } template<typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; s << "["; for(int i = 0; i < n - 1; i++) s << arr[i] << ","; s << arr[n - 1] << "]"; return s.str(); } #define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);} inline void __evars_begin(int line) { cerr << "#" << line << ": "; } template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); } template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); } template<typename T> inline void __evars_out_var(T val) { cerr << val; } inline void __evars(vector<string>::iterator it) { cerr << endl; } template<typename T, typename... Args> inline void __evars(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << "="; __evars_out_var(a); cerr << "; "; __evars(++it, args...); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template<class T, class H>using umap=unordered_map<T,H,custom_hash>; template<class T>using uset=unordered_set<T,custom_hash>; int32_t main() { clock_t clk = clock(); fast_az_fuk ll testcase; cin>>testcase; for(ll test=1;test<=testcase;test++) {//cout<<"Case #"<<test<<": "; ll n; cin>>n; if(n&1) cout<<"Odd\n"; else if((n/2)&1) cout<<"Same\n"; else cout<<"Even\n"; } // cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(1 << n); for (int i = 0; i < (1 << n); i++) cin >> v[i]; array<int, 2> ar1 {-1, 0}; for (int i = 0; i < (1 << (n - 1)); i++) { if (v[i] > ar1[0]) { ar1[0] = v[i]; ar1[1] = i + 1; } } array<int, 2> ar2 {-1, 0}; for (int i = (1 << (n - 1)); i < (1 << n); i++) { if (v[i] > ar2[0]) { ar2[0] = v[i]; ar2[1] = i + 1; } } auto best = min(ar1, ar2); cout << best[1] << "\n"; }
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <string> #include <math.h> #include <iomanip> #include <limits> #include <list> #include <queue> #include <tuple> #include <map> #include <stack> #include <set> #include <bitset> #include <functional> #include <cassert> using namespace std; #define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ; #define ll long long int #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 REP(i,n) for(int i=n-1; i>=0; i--) #define REPS(i,n) for(int i=n; i>0; i--) #define MOD (long long int)(1e9+7) #define INF (int)(1e9) #define LINF (long long int)(1e18) #define chmax(a, b) a = (((a)<(b)) ? (b) : (a)) #define chmin(a, b) a = (((a)>(b)) ? (b) : (a)) #define all(v) v.begin(), v.end() typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; ll mpow(ll a, ll b){ if(b==0) return 1; else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;} else return mpow(a,b-1) * a % MOD; } ll lpow(ll a, ll b){ if(b==0) return 1; else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;} else return lpow(a,b-1) * a; } ll gcd(ll a, ll b){ if(b==0) return a; else return gcd(b, a%b); } vector<ll> kaijo_memo; ll kaijo(ll n){ if(kaijo_memo.size() > n) return kaijo_memo[n]; if(kaijo_memo.size() == 0) kaijo_memo.push_back(1); while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD); return kaijo_memo[n]; } vector<ll> gyaku_kaijo_memo; ll gyaku_kaijo(ll n){ if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n]; if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1); while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD); return gyaku_kaijo_memo[n]; } ll nCr(ll n, ll r){ if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く?不安. if(n < r || r < 0) return 0; ll ret = 1; if(n <= 1e7){ ret *= kaijo(n); ret %= MOD; ret *= gyaku_kaijo(r); ret %= MOD; ret *= gyaku_kaijo(n-r); ret %= MOD; }else{ rep(i,r){ ret *= n-i; ret %= MOD; ret *= mpow(r-i, MOD-2); ret %= MOD; } } return ret; } /* std::mt19937 mt{ std::random_device{}() }; std::uniform_int_distribution<int> dist(1, 6); dist(mt); while(1){ val; cout << val << endl; sol1 = solver1(); sol2 = solver2(); if(sol1==sol2){ cout << "OK" << endl; }else{ cout << "NG" << endl; cout << sol1 << endl; cout << sol2 << endl; break; } } */ int main(void){ fast_io cout<<fixed<<setprecision(15); ll n; cin >> n; cout << 100-n%100 << endl; return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int IM = 2e9; int dat[400004]; 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; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); dat[0] = -IM; ll kotae = -IM * ll(N); for (int i = N + 1; i > 1; i--) { dat[i] = getint(); kotae += dat[i]; } for (int i = 2; i < N + 2; i++) { int p = 2 + (dat[2] > dat[3]); int v = dat[i] - IM; while (v > dat[p]) { dat[p >> 1] = dat[p]; p <<= 1; p += dat[p] > dat[p + 1]; } dat[p >> 1] = v; int a = getint(); kotae += a; p = i; v = a - IM; while (v < dat[p >> 1]) dat[p] = dat[p >> 1], p >>= 1; dat[p] = v; kotae -= dat[1]; } co(kotae); Would you please return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll =long long; using vi = vector<ll>; using vc = vector<char>; using vs = vector<string>; using P = pair<ll , ll>; using vp = vector<P>; #define rep(i, n) for (long long int i = 0; i < n; i++) #define rep2(i, s, n) for (int i = (s); i < n; i++) #define sort(A) sort(A.begin(),A.end()); #define reverse(A) reverse(A.begin(),A.end()); #define vecint2(A, n, m) vector<vector<long long int>> A(n, vector<long long int>(m)); #define vecchar2(A, n, m) vector<vector<char>> A(n, vector<char>(m)); #define k(s) cout << fixed << setprecision(s); #define t cout<<"OK"<<endl; #define Yes cout<<"Yes"<<endl #define No cout<<"No"<<endl #define YES cout<<"YES"<<endl #define NO cout<<"NO"<<endl const long double pi=3.14159265358979323846; int main() { int N; cin>>N; vector<string> S(N); rep(i,N) cin>>S[i]; ll dpf[65],dpt[65]; dpf[0]=1;dpt[0]=1; rep(i,N){ if(S[i]=="AND"){ dpt[i+1]=dpt[i]; dpf[i+1]=2*dpf[i]+dpt[i]; }else{ dpt[i+1]=2*dpt[i]+dpf[i]; dpf[i+1]=dpf[i]; } } cout<<dpt[N]<<endl; }
#include <iostream> #define rep(i,n) for(int i=0; i < (n);++i) using namespace std; using ll = long long; using P = pair<int,int>; template <class _T> inline bool chmax (_T& a,_T b){if(a<b){a=b;return true;}return false;} template <class _T> inline bool chmin (_T& a,_T b){if(a>b){a=b;return true;}return false;} const int inf = 2147483647; const long long INF = 1LL << 60; const int MAX_N = 11000; const int MAX_W = 10100; ll mod = 1000000007; ll MOD = 998244353; int main(void){ ll b,c; cin >> b >> c; ll ans = 0; if(c==1 && b != 0){ cout << 2 << endl; return 0; } if(b>0){ //0 <= |b|の部分 if( b-c/2 <= 0){ ans += 2*b+1; }else if( b-c/2 > 0 ){ if(c%2==0){ ans += (c/2)*2+1; }else{ ans += (c/2)*2+2; } } // |b|より大きい部分 c--; if(c%2==0){ ans += (c/2)*2-1; }else{ ans += (c/2)*2; } }else if(b==0){ ans++; if(c%2==0){ ans += (c/2)*2-1; }else{ ans += (c/2)*2; } }else{ //0 < |b|の部分 c--; if(-b-c/2 <= 0){ ans += 2*(-b)+1; }else if( -b-c/2 > 0){ if(c%2==0){ ans += (c/2)*2+1; }else{ ans += (c/2)*2+2; } } c++; // |b|より大きい部分 if(c%2==0){ ans += (c/2)*2-1; }else{ ans += (c/2)*2; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> /// 500 485 462 A4 using namespace std; typedef long long int ll; typedef complex<double> point; typedef long double ld; #define pb push_back #define pii pair < ll , ll > #define F first #define S second #define endl '\n' #define int long long #define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #define kill(x) return cout<<x<<'\n', 0; const int N=2e5+100; ll mod=998244353; ll power(ll n,ll k){ if (k==0) return 1; if (k%2==1){ ll x=power(n,k/2); return x*x%mod*n%mod; } ll x=power(n,k/2); return x*x%mod; } ll fac[N]; ll fm[N]; ll ent(ll k,ll n){ if (k==0 || k==n) return 1; if (k<0 || k>n) return 0; return fac[n]*fm[k]%mod*fm[n-k]%mod; } vector <int> p[N]; int32_t main(){ sync; fac[0]=1; fm[0]=1; for (int i=1;i<N;i++){ fac[i]=fac[i-1]*i%mod; fm[i]=power(fac[i],mod-2); } for (int i=2;i<N;i++){ if (p[i].size()) continue; for (int j=i;j<N;j+=i){ p[j].pb(i); } } ll ans=0; ll n,m; cin >> n >> m; for (int i=1;i<=m;i++){ ll jav=1; for (auto u : p[i]){ ll z=i,t=0; while(z%u==0){ t++; z/=u; } jav*=ent(n-1,t-1+n); jav%=mod; // cout << i << " " << u <<" " << t << " " << jav<< endl; } ans+=jav; } cout << ans%mod << endl; }
#include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define test int t;cin>>t;while(t--) #define REP(i,n) for (int i = 1; i <= n; i++) #define MOD 1000000007 #define MOD2 998244353 #define ll long long #define ld long double #define int ll #define pb push_back #define ii pair<int,int> #define llll pair<ll,ll> #define vi vector<int> #define vl vector<ll> #define vii vector<ii> #define vllll vector<llll> #define ff first #define ss second #define INF 1000000000 #define HINF 1000000000000000 #define mem(a,b) memset(a,b,sizeof(a)) #define arrin(x,n) int x[n]; for(int o=0;o<n;o++) cin>>x[o] #define arrout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl #define vecin(x,n); vi x;int hool; for(int o=0;o<n;o++) {cin>>hool; x.pb(hool);} #define vecout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl #define all(x) x.begin(),x.end() #define deb(x) cout<<#x<<'='<<x<<endl #define deb2(x,y) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<endl #define deb3(x,y,z) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<" "<<#z<<'='<<z<<endl #define debarr(arr,n) for(int o=0;o<n;o++) deb2(o,arr[o]) #define debarrall(arr) debarr(arr,arr.size()) #define IO freopen("input.txt", "r", stdin); freopen("output.txt", "w+", stdout) const double pi = 3.14159265358979323846; int powersimple(int a, int b){//a^b int res=1; while(b>0){ if(b&1) {res=(res*a); b--;} a=(a*a); b>>=1; } return res; } int ncr(int n,int k) { int ans=1; if(k>n-k) k=n-k; for(int i=1;i<=k;i++) ans*=(n-i+1),ans/=i; return ans; } int power(int x,int y,int p) { int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int modInverse(int n, int p) { return power(n, p - 2, p); } int ncrModPFermat(int n,int r, int p) { if (r == 0) return 1; int fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } //int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} ///////////////END OF TEMPLATE////////////////// void solve() { int i=0,j=0,k=0,m,n,p,sum=0,cnt=0; cin>>n; vi arr; for(i=0;i<n;i++) { cin>>j; k=__gcd(k,j); } cout<<k; } int32_t main() { fast //IO; //cout<<fixed<<setprecision(6); //test { solve(); } return 0; }
#include <bits/stdc++.h> #include <iostream> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; int main() { int N; cin >> N; map<int, int> a; int x = 1e9; int X = 0; for (int i = 0; i < N; i++) { int tmp; cin >> tmp; a[tmp]++; if (tmp > X) X = tmp; if (tmp < x) x = tmp; } while (1) { if (x == X) break; a[X - x] = a[X]; a.erase(X); X = a.rbegin()->first; x = a.begin()->first; } cout << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; int main(){ int t; cin >> t; ll N; cin >> N; ll count=0; ll ans=0; vector<int> A(100); for(int i=0;i<100;i++){ A[i]=0; } for(ll i=0;i<100;i++){ if(i*t/100!=(i+1)*t/100){ A[count]=i; count++; } } int last=0; for(int i=99;i>-1;i--){ if(A[i]!=0){ last=A[i]; break; } } ll a=N/count; ll b=N%count; if(b==0){ cout << ((a-1)*100+last)*(100+t)/100+1 << endl; } else{ cout << (a*100+A[b-1])*(100+t)/100+1 << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (ll i = 0; i < N; i++) int main() { int m=1000000007; ll n; cin >> n; vector<ll> a(n); rep(i,n) cin >> a[i]; vector<vector<ll>> f(n+2,vector<ll>(2)); f[0][0]=0; f[0][1]=1; rep(i,n){ f[i+1][0]=f[i][1]%m; f[i+1][1]=(f[i][0]+f[i][1])%m; } ll ans=0; rep(i,n){ ans=(ans%m)+(a[i]%m)*(((f[i][1]*f[n-i][1])%m)-((f[i][0]*f[n-i][0])%m))%m; } ans%=m; if (ans < 0) ans += m; cout << ans%m << endl; }
#include<bits/stdc++.h> #include<vector> #include<math.h> #include<algorithm> #include<stack> #include<list> #include<queue> #include<set> #include<string> #include<string.h> #include<map> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") //#pragma GCC optimization("unroll-loops") #define ll long long #define yes cout<<"YES"<<"\n" #define no cout<<"NO"<<"\n" using namespace std; const int MOD = 1000000007; const int Nmax=100005; ll gcd(ll a,ll b) { if(a==0){ return b; } return gcd(b%a,a); } int lcm(int a,int b) { return (a*b)/gcd(a,b); } bool isPrime(ll n){ for(int i = 2; i <=sqrt(n); i++) if(n % i == 0) return 0; return 1; } void binaryRepresentation(int x) { for (int i = 31; i >= 0; i--) { if (x&(1<<i)) cout << "1"; else cout << "0"; } } int combination(int n,int r){ if(r==n || r==0){ return 1; } return combination(n-1,r-1) + combination(n-1,r); } bool isPowerOfTwo(ll n) { if(n==0) return false; return (ceil(log2(n)) == floor(log2(n))); } ll power(ll a,ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a); b/=2; a=(a*a); } return ans; } bool cmp(const pair<ll,ll> &a,const pair<ll,ll> &b) { if(a.first==b.first) return a.second<b.second; return a.first<b.first; } int main() { //vector<vector<int> >ar(n,vector<int> (m,0)); //vector<int>::iterator ptr; ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin>>n; vector<ll>ar(n); for(ll i=0;i<n;i++) { cin>>ar[i]; } sort(ar.begin(),ar.end()); ll ans=0; for(ll i=0;i<n;i++) ans+=ar[i]*ar[i]; ans*=(n-1); ll sum=0; for(ll i=n-1;i>=0;i--) { ans-=2*(ar[i]*sum); sum+=ar[i]; } cout<<ans; return 0; }
#include <iostream> #include <algorithm> #include<cmath> #include<cstring> #include<cstdio> #include<cstdlib> #include<vector> #include<iomanip> #include<ctime> #include<set> #include<map> #include<queue> #include<stack> #include<bitset> #define sqr(x) ((x)*(x)) #define fz1(i,n) for ((i)=1;(i)<=(n);(i)++) #define fd1(i,n) for ((i)=(n);(i)>=1;(i)--) #define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++) #define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--) #define fz0k(i,n) for ((i)=0;(i)<(n);(i)++) #define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--) #define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++) #define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--) #define fzin fz1(i,n) #define fzim fz1(i,m) #define fzjn fz1(j,n) #define fzjm fz1(j,m) #define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr) #define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;} #define inc(x,y) {x+=(y);if(x>=mod)x-=mod;} #define dec(x,y) {x-=(y);if(x<0)x+=mod;} #define incm(x,y) {x=((x)+(y))%mod;} #define spln(i,n) (i==n?'\n':' ') #define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}} using namespace std; inline void read(int &x) { char c;int f=1; while(!isdigit(c=getchar()))if(c=='-')f=-1; x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15); x*=f; } int n,m,mod,i,j,k,l,f[2100005],g[2100005],ans[105]; int main() { read(n);read(m);read(mod); int cc=2*n*n*m; f[cc/2]=m+1; fz1(i,n-1){ fz0k(j,i){ int s=0; for(k=cc/2/i*i+j;k<=cc;k+=i){ inc(s,f[k]) g[k]=s; if(k>=i*m) dec(s,f[k-i*m]) } } fz(k,cc/2,cc) f[k]=g[k]; } fz1(i,(n+1)/2){ printf("%d\n",ans[i]=ans[n-i+1]=(f[cc/2]-1+mod)%mod); if(i==n){ i++;break; } int x=n-i; fz0k(j,x){ int s=0; for(k=j;k<=cc;k+=x){ g[k]=(f[k]-s+mod)%mod; inc(s,g[k]) if(k>=x*m) dec(s,g[k-x*m]) } } fz0g(k,cc) f[k]=g[k]; fz0k(j,i){ int s=0; for(k=cc/i*i+j;k>=0;k-=i)if(k<=cc){ inc(s,f[k]) g[k]=s; if(k+i*m<=cc) dec(s,f[k+i*m]) } } fz0g(k,cc) f[k]=g[k]; } while(i<=n){ printf("%d\n",ans[i++]); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define ld long double #define ff first #define ss second #define pb push_back #define mp make_pair #define all(a) a.begin(),a.end() #define sz(a) (ll)(a.size()) const ll mod = 998244353; const int M = 2e5+5; ll a[M],cs[M],val[M],ncr[500][500],tot[500]; void pre() { ncr[0][0]=1; for(int i=1;i<500;i++) { ncr[i][0] = 1; for(int j=1;j<=i;j++) ncr[i][j] = (ncr[i-1][j-1]+ncr[i-1][j])%mod; } } ll power(ll a,ll b) { ll val = 1; while(b) { if(b%2) val = (val*a)%mod; a = (a*a)%mod; b/=2; } return val; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); pre(); int n,k; cin>>n>>k; ll sum = 0; for(int i=1;i<=n;i++) { cin>>a[i]; sum = (sum+a[i])%mod; val[i] = 1; } for(int j=1;j<=k;j++) { ll res = 0; for(int i=1;i<=n;i++) { val[i]=(val[i]*a[i])%mod; res = (res+val[i])%mod; } tot[j] = res; } tot[0] = n; for(int i=1;i<=k;i++) { ll ans = 0; ll c = 1; for(int j=0;j<=i;j++) { ll z = ncr[i][j]; z = (z*tot[i-j])%mod; z = (z*tot[j])%mod; ans = (ans + z)%mod; if(j) c=(c*2)%mod; } ans = (ans-(c*tot[i])%mod+mod)%mod; ans = (ans*power(2,mod-2))%mod; cout<<ans<<"\n"; } return 0; }
// Author: wlzhouzhuan #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pb push_back #define fir first #define sec second #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, t) memset(s, t, sizeof(s)) #define mcpy(s, t) memcpy(s, t, sizeof(t)) template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); return f ? -x : x; } template<typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } template<typename T> void print(T x, char let) { print(x), putchar(let); } const int N = 1005; const int mod = 998244353; char a[N][N]; int pw[N * N]; int A[N], B[N], C[N]; int n, m; int main() { scanf("%d%d", &n, &m); pw[0] = 1; for (int i = 1; i <= n * m; i++) { pw[i] = 2 * pw[i - 1] % mod; } for (int i = 1; i <= n; i++) { scanf("%s", a[i] + 1); for (int j = 1; j <= m; j++) { int pos = i + j; if (a[i][j] == 'B') A[pos]++; if (a[i][j] == 'R') B[pos]++; if (a[i][j] == '.') C[pos]++; } } int ans = 1; for (int i = 2; i <= n + m; i++) { if (A[i] && B[i]) return puts("0") & 0; if (!A[i] && !B[i]) ans = 2ll * ans % mod; } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 2e5 + 5, INF = 0x3f3f3f3f; int main(){ ll n; cin >> n; int res = 0; for(int i = 1; i <= 1000000; i ++ ){ int ans = 0, x = i; while(x){ ans ++; x /= 10; } if(i * pow(10, ans) + i <= n) res ++; } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } string s; int n; int is_ok(int i, int c, int d){ if(i == n){ return (c == 0) ? d : INT_MAX; } return min(is_ok(i+1, (c+s[i]-'0')%3, d), is_ok(i+1, c, d+1)); } signed main(){ cin >> s; n = s.size(); int ans = is_ok(0, 0, 0); if(ans >= n){ ans = -1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double using namespace std; const int N = 1000 + 5, mod = 1e9 + 7; int n, m, a[N], b[N], dp[N][N]; int solve(int idx_a, int idx_b){ if(idx_a == n) return m - idx_b; if(idx_b == m) return n - idx_a; if(dp[idx_a][idx_b] != -1) return dp[idx_a][idx_b]; int op1 = 1 + solve(idx_a + 1, idx_b); int op2 = 1 + solve(idx_a, idx_b + 1); int op3 = solve(idx_a + 1, idx_b + 1); if(a[idx_a] != b[idx_b]) op3++; return dp[idx_a][idx_b] = min({op1, op2, op3}); } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < m; i++) cin >> b[i]; memset(dp, -1, sizeof dp); cout << solve(0, 0); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define fon(i,n) for(int i=0;i<n;i++) #define fo(i,n) for(int i=1;i<=n;i++) #define pb push_back #define mp make_pair #define ff first #define ss second #define pii pair<int,int> #define pll pair<ll ,ll> #define vii vector<int> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define vpll vector<pll> #define bs binary_search #define lb lower_bound #define ub upper_bound #define M (1000*1000*1000+7) #define test ll T; cin>>T; while(T--) #define all(z) z.begin() , z.end() #define allr(z) z.rbegin() , z.rend() // ============================================= //1.integer overflow (1e5 * 1e5) (2e9 + 2e9) // 2.runtime error //3.boundary condition int a[1002] , b[1002]; int dp[1002][1002]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(dp , 0 , sizeof(dp)); int n , m; cin >> n >> m; // int a[n] , b[n]; fon(i ,n) cin >> a[i]; fon(i , m)cin >> b[i]; for(int i = 0 ; i <= n ; i++) for(int j = 0 ; j <= m ; j++) if(i==0 || j== 0) dp[i][j] = (i+j); for(int i = 1 ; i <= n; i++) { for(int j = 1 ; j <= m ; j++) { if(a[i-1]==b[j-1]) dp[i][j] = dp[i-1][j-1]; else dp[i][j] = min(1+dp[i-1][j-1] , 1+min(dp[i-1][j] , dp[i][j-1])); } } cout << dp[n][m] << endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back using namespace std; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; using ll = long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using pdd = pair<ld,ld>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; n *= 2; V<ll> a(n); rep(i,n) cin >> a[i]; V<int> c(n); V<int> id(n); rep(i,n) id[i] = i; sort(all(id), [&](int i, int j){ return a[i] < a[j]; }); rep(i,n/2){ c[id[i]] = 0; c[id[i+n/2]] = 1; } string s = ""; rep(i,n) s += ')'; stack<int> st; rep(i,n){ if(st.size()==0) st.push(i); else{ if(c[st.top()] != c[i]){ s[st.top()] = '('; s[i] = ')'; st.pop(); } else st.push(i); } } cout << s << endl; }
#include <bits/stdc++.h> using namespace std; #define PRINT_YesNo(condition) cout<<((condition)?"Yes":"No")<<endl #define PRINT_YESNO(condition) cout<<((condition)?"YES":"NO")<<endl #define __MAGIC__ ios::sync_with_stdio(false);cin.tie(nullptr) #define SET_PRECISION(x) cout<< fixed << setprecision((x)) #define REP(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef vector<int> vi_t; typedef vector<vector<int> > vvi_t; typedef vector<ll> vl_t; typedef vector<vector<ll> > vvl_t; typedef pair<int, int> pii_t; vi_t str2numvec(const std::string& str); vector<string> split(const string &str, char delim); //=====================// // メ イ ン 関 数 //  //=====================// int main() { __MAGIC__; int N; cin >> N; std::vector<pair<long, int> > X(N); std::vector<pair<long, int> > Y(N); vl_t x(N); vl_t y(N); for(int i=0; i<N; ++i) { cin >> x[i] >> y[i]; X[i].first = x[i]; Y[i].first = y[i]; X[i].second = i; Y[i].second = i; } std::sort(X.begin(), X.end()); std::sort(Y.begin(), Y.end()); // ll dx1 = std::abs(X[0].first - X[N-1].first); // ll dy1 = std::abs(Y[0].first - Y[N-1].first); // ll dx2 = std::max( std::abs(X[1].first - X[N-1].first), std::abs(X[0].first - X[N-2].first) ); // ll dy2 = std::max( std::abs(Y[1].first - Y[N-1].first), std::abs(Y[0].first - Y[N-2].first) ); set<pii_t> S; S.insert( std::make_pair(min(X[0].second,X[N-1].second), max(X[0].second,X[N-1].second)) ); S.insert( std::make_pair(min(Y[0].second,Y[N-1].second), max(Y[0].second,Y[N-1].second)) ); if( std::abs(X[1].first - X[N-1].first) > std::abs(X[0].first - X[N-2].first) ) { S.insert( std::make_pair(min(X[1].second,X[N-1].second), max(X[1].second,X[N-1].second)) ); } else { S.insert( std::make_pair(min(X[0].second,X[N-2].second), max(X[0].second,X[N-2].second)) ); } if( std::abs(Y[1].first - Y[N-1].first) > std::abs(Y[0].first - Y[N-2].first) ) { S.insert( std::make_pair(min(Y[1].second,Y[N-1].second), max(Y[1].second,Y[N-1].second)) ); } else { S.insert( std::make_pair(min(Y[0].second,Y[N-2].second), max(Y[0].second,Y[N-2].second)) ); } vl_t d; for(auto [m, M] : S) { d.push_back( std::max(std::abs(x[M]-x[m]), std::abs(y[M]-y[m])) ); } std::sort(d.rbegin(), d.rend()); cout << d[1] << endl; return 0; } //----------------------------- // ヘルパー関数群 //----------------------------- vi_t str2numvec(const std::string& str) { vi_t v(str.size()); for(int i=0, n=str.size(); i<n; ++i) { v[i] = str[i] - '0'; } return v; } vector<string> split(const string &str, char delim){ vector<string> res; size_t current = 0, found; while((found = str.find_first_of(delim, current)) != string::npos){ res.push_back(string(str, current, found - current)); current = found + 1; } res.push_back(string(str, current, str.size() - current)); return res; }
#include<iostream> #include<vector> #include<algorithm> #include<set> #include<map> #include<queue> #include<cmath> #include<iomanip> #include<cstring> #include<complex> #include<cstdio> #define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1; #define fi first #define se second #define pb push_back #define pii pair<int,int> #define ppi pair<pii,int> #define pip pair<int,pii> #define ll long long #define pll pair<ll,ll> #define rep(i,n) for(int i=0;i<n;i++) #define repd(i,n) for(int i=n-1;i>=0;i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 1000005 #define mid(l,r) l+(r-l)/2 #define removeduplicates(vec) vec.erase( unique( vec.begin(), vec.end() ), vec.end() ) using namespace std; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1}; void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;} ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);} vector<int>res; int a[N]; int n; void rec(int l,int r,int ch){ if(ch) for(int i=r-1;i>=l;i--){ if(a[i]>a[i+1]){ swap(a[i],a[i+1]); res.pb(i+1); rec(l,i,1); rec(i+1,r,0); return; } } else for(int i=l;i<r;i++){ if(a[i]>a[i+1]){ swap(a[i],a[i+1]); res.pb(i+1); rec(l,i,1); rec(i+1,r,0); return; } } } void solve(){ cin>>n; rep(i,n)cin>>a[i]; //int mk[n-1]={0}; //int c=0; /*for(int i=0;i<n;i++){ for(int j=0;j+1<n;j++){ if(a[j]>a[j+1]){ if(mk[j]){cout<<-1;return;} res.pb(j+1); mk[j]=1; swap(a[j],a[j+1]); c++; } } if(c==n-1)break; }*/ rec(0,n-1,0); for(int i=0;i+1<n;i++)if(a[i]>a[i+1]){cout<<-1;return;} if(res.size()<n-1){cout<<-1;return;} for(int i:res)cout<<i<<"\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while(t--){ solve(); } }
#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define PPC(x) __builtin_popcount(x) #define MSB(x) (63 - __builtin_clzll(x)) #define SZ(x) ((int)(x).size()) #define HASK(S, x) (S.find(x) != S.end()) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m) >> (i) & 1) #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; const int maxN = 1 << 19; int P[maxN], R[maxN]; bool juz[maxN]; vector <int> res; void click(int i) { swap(P[i], P[i+1]); swap(R[P[i]], R[P[i+1]]); juz[i] = true; res.pb(i); } void fail() { printf("-1\n"); exit(0); } void solve() { int n; scanf ("%d", &n); FOR(i, 1, n+1) { scanf ("%d", P+i); R[P[i]] = i; } FORD(i, n, 1) { if (R[i] == i and (!juz[i-1] or !juz[i])) fail(); while (R[i] != i) { if (juz[R[i]]) fail(); click(R[i]); } } for (int i : res) printf("%d\n", i); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; }
// International Master sm0016 #include <bits/stdc++.h> #define ll long long #define pb push_back #define all(x) x.begin(),x.end() #define raftaar ios_base::sync_with_stdio(false);cin.tie(NULL); using namespace std; const ll MOD1=1000000007; const ll MOD2= 998244353; const int N=1e6+6; int main(){ raftaar int a,b;cin>>a>>b;int ans=1;int check[N]={0}; for(int i=a;i<=b;i++){ for(int j=1;j*j<=i;j++){ if(i%j==0){ if(j*j==i) check[j]++; else{check[j]++;check[i/j]++;} } } } for(int i=1;i<=max(a,b);i++){ if(check[i]>1) ans=max(ans,i); } cout<<ans; }
#include <iostream> using namespace std; int main(){ int a,b;cin>>a>>b; for(int c = b; ; c--) if((a + c - 1) / c < b / c){ cout << c << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; int main(){ long long l,ans=1;cin>>l;l--; int d[5][2]{{2,8},{3,4},{5,2},{7,1},{11,1}}; for(long long i=0;i<11;i++){ ans*=l-i; for(int j=0;j<5;j++){ while(ans%d[j][0]==0&&d[j][1]>0){ ans/=d[j][0]; d[j][1]--; } } } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; //#define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' #define vec vector<ll> #define mat vector<vector<ll> > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; //typedef long double ld; typedef complex<double> Complex; const ll INF=1e9+7; const ll MOD=INF; const ll inf=INF*INF; const ll mod=MOD; const ll MAX=100010; signed main(){ string s;cin>>s; string a="",b=""; rep(i,s.size()){ if(s[i]=='R')swap(a,b); else{ if(a.size()>0&&a[a.size()-1]==s[i])a.pop_back(); else a+=s[i]; } } ll n=a.size(),m=b.size(); reverse(all(a)); reverse(all(b)); rep(i,min(n,m)){ if(a[n-i-1]==b[m-i-1]){ a.pop_back(); b.pop_back(); }else{ break; } } reverse(all(a)); cout<<b<<a<<endl; }
#include<bits/stdc++.h> using namespace std; #define endl "\n" //#define int long long #define rep(i,j,k) for(int i=j;i<k;i+=1) #define rrep(i,j,k) for(int i=n-1;i>=0;i-=1) #define sorti(v) sort(v.begin(),v.end()) #define sortd(v) sort(v.begin(),v.end(),greater<int>()) #define mm map<int,int> #define vv vector<int> #define tmm map<int,map<int,int>> #define trav(a, x) for(auto& a : x) #define ff first #define ss second #define lb lower_bound #define ub upper_bound #define all(v) v.begin(),v.end() #define pb push_back #define alot(x,y) memset(x,y,sizeof(x)) #define pii pair<int,int> #define code ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) const int MOD=1e9+7; const double pi=acos(-1.0); //memset(array_name,value_to_be_intialized,sizeof(array_name)); // __builtin_popcount(n) count no of set bit in number n //next_permutation(s.begin(),s.end()); /*int change(string g){ int p=10; int ans=g[g.length()-1]-'0'; for(int i=g.length()-2;i>=0;i--){ int j=g[i]-'0'; ans=p*ans+j; } return ans; } int binpow(int a,int b){ int res=1; while(b>0){ if(b&1)res*=a; a*=a; b>>=1; } return res; } // lower_bound==> if the element is present it return its first occurence else 1st element which is greater than // upper_bound==> it will always return iterator to element which is greater than searched element */ vector<pii>v[1000]; char c[2002][2002]; int dis[2002][2002]; bool vis[2002][2002]; bool g[2002]; int n,m; bool check(int i,int j){ if(i<n && i>=0 && j<m && j>=0)return true; return false; } int32_t main () { code; int tt; //cin>>tt; tt=1; while(tt--){ int x,y; cin>>n>>m; int rx,ry; rep(i,0,n){ rep(j,0,m){ cin>>c[i][j]; if(c[i][j]=='S'){ x=i; y=j; } else if(c[i][j]=='G'){ rx=i; ry=j; } else if(c[i][j]!='#' && c[i][j]!='.'){ v[c[i][j]-'a'].pb(make_pair(i,j)); } } } queue<pii>q; q.push(make_pair(x,y)); alot(vis,false); alot(g,false); while(!q.empty()){ pii x=q.front(); q.pop(); int i=x.ff; int j=x.ss; vis[i][j]=true; if(check(i+1,j) && c[i+1][j]!='#' && !vis[i+1][j]){ dis[i+1][j]=dis[i][j]+1; vis[i+1][j]=true; q.push(make_pair(i+1,j)); } if(check(i-1,j) && c[i-1][j]!='#' && !vis[i-1][j]){ dis[i-1][j]=dis[i][j]+1; vis[i-1][j]=true; q.push(make_pair(i-1,j)); } if(check(i,j+1) && c[i][j+1]!='#' && !vis[i][j+1]){ dis[i][j+1]=dis[i][j]+1; vis[i][j+1]=true; q.push(make_pair(i,j+1)); } if(check(i,j-1) && c[i][j-1]!='#' && !vis[i][j-1]){ dis[i][j-1]=dis[i][j]+1; vis[i][j-1]=true; q.push(make_pair(i,j-1)); } if(c[i][j]>='a' && c[i][j]<='z' && !g[c[i][j]-'0']){ g[c[i][j]-'0']=true; trav(k,v[c[i][j]-'a']){ if(!vis[k.ff][k.ss]){ dis[k.ff][k.ss]=dis[i][j]+1; vis[k.ff][k.ss]=true; q.push(make_pair(k.ff,k.ss)); } } } } if(dis[rx][ry]){ cout<<dis[rx][ry]<<'\n'; } else cout<<"-1\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; const int INF = 5e6; int di[4] = {1, 0, -1, 0}; int dj[4] = {0, 1, 0, -1}; int main() { int h, w; cin >> h >> w; vector<string> s(h); rep(i,h) cin >> s[i]; // vector dist(h, vector(w, INF)); vector<vector<int>> dist(h, vector<int>(w, INF)); queue<P> q; rep(i,h)rep(j,w) { if (s[i][j] == 'S') { q.emplace(i,j); dist[i][j] = 0; } } vector<vector<P>> warps(256); rep(i,h)rep(j,w) warps[s[i][j]].emplace_back(i,j); while (!q.empty()) { auto [i,j] = q.front(); q.pop(); if (s[i][j] == 'G') { cout << dist[i][j] << endl; return 0; } rep(v,4) { int ni = i + di[v], nj = j + dj[v]; if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if (s[ni][nj] == '#') continue; if (dist[ni][nj] != INF) continue; dist[ni][nj] = dist[i][j] + 1; q.emplace(ni,nj); } if (islower(s[i][j])) { for (P p : warps[s[i][j]]) { int ni = p.first, nj = p.second; if (dist[ni][nj] != INF) continue; dist[ni][nj] = dist[i][j] + 1; q.emplace(ni,nj); } warps[s[i][j]] = vector<P>(); } } cout << -1 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; cin >> N; long long A[400010] = {}; long long sum = 0; long long mn = 0; for(int i = 0; i < N * 2; i++){ cin >> A[i]; sum += A[i]; } priority_queue<long long, vector<long long>, greater<long long>> que; for(int i = 0; i < N; i++){ que.push(A[N-1-i]); que.push(A[N+i]); long long p = que.top(); mn += p; que.pop(); } cout << sum - mn << "\n"; }
#include <bits/stdc++.h> using namespace std; using lint = long long int; using P = pair<int, int>; using PL = pair<lint, lint>; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int 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 ALL(a) (a).begin(),(a).end() constexpr int MOD = 1000000007; constexpr lint B1 = 1532834020; constexpr lint M1 = 2147482409; constexpr lint B2 = 1388622299; constexpr lint M2 = 2147478017; constexpr int INF = 2147483647; void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";} template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; } template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int N; cin >> N; vector<lint> V1(N), V2(N); IREP(i, N) cin >> V1[i]; REP(i, N) cin >> V2[i]; multiset<lint> st; REP(i, N) { if(st.size() > 0 && *st.begin() < min(V1[i], V2[i])) { st.erase(st.begin()); st.insert(V1[i]); st.insert(V2[i]); } else { st.insert(max(V1[i], V2[i])); } } lint ans = 0; for(lint x : st) ans += x; cout << ans << endl; }
/* { ###################### # Author # # Gary # # 2021 # ###################### */ #include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen("check.out","w",stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; //inline int read(){ // int x=0; // char ch=getchar(); // while(ch<'0'||ch>'9'){ // ch=getchar(); // } // while(ch>='0'&&ch<='9'){ // x=(x<<1)+(x<<3)+(ch^48); // ch=getchar(); // } // return x; //} const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ const int MAXN=200000+233; vector<int> g[MAXN]; int n; mp fr; int depth[MAXN],rest[MAXN],cnt; void dfs(int now,int dis=0,int pre=0){ check_max(fr,II(dis,now)); for(auto it:g[now]) if(it!=pre){ dfs(it,dis+1,now); } } void dfs2(int now,int pre=0){ for(auto it:g[now]) if(it!=pre){ depth[it]=depth[now]+1; dfs2(it,now); } for(auto it:g[now]) if(it!=pre){ check_max(depth[now],depth[it]); } } bool cmp(int A,int B){ return depth[A]<depth[B]; } void dfs3(int now,int pre=0){ rest[now]=++cnt; vector<int> ord; for(auto it:g[now]) if(it!=pre) ord.PB(it); sort(ALL(ord),cmp); for(auto it:ord) dfs3(it,now),++cnt; } int main(){ scanf("%d",&n); rb(i,1,n-1){ int u,v; scanf("%d%d",&u,&v); g[u].PB(v); g[v].PB(u); } fr=II(-1,-1); dfs(1); int tmp=fr.SEC; fr=II(-1,-1); dfs(tmp); int root=fr.SEC; dfs2(root); dfs3(root); rb(i,1,n) printf("%d ",rest[i]); return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn=200005; int n,ql,qr,lst,s,t; vector<int>edge[maxn]; int dist[maxn],par[maxn],dep[maxn],que[maxn],ans[maxn]; bool bad[maxn]; void bfs(int s){ for(int i=1;i<=n;i++)dist[i]=-1; dist[s]=0; ql=1;qr=0; que[++qr]=s; while(ql<=qr){ int x=que[ql++]; for(int i=0;i<int(edge[x].size());i++){ int y=edge[x][i]; if(dist[y]<0)dist[y]=dist[x]+1,par[y]=x,que[++qr]=y; } } } void dfs(int x,int p){ lst=x; int yy=0; for(int i=0;i<int(edge[x].size());i++){ int y=edge[x][i]; if(y==p)continue; if(!bad[y]) ans[y]=ans[lst]+dist[lst]+dist[y]-2*dist[x],dfs(y,x); else yy=y; } if(yy) ans[yy]=ans[lst]+dist[lst]+dist[yy]-2*dist[x],dfs(yy,x); } int main(){ scanf("%d",&n); for(int i=1;i<=n-1;i++){ int u,v; scanf("%d%d",&u,&v); edge[u].push_back(v); edge[v].push_back(u); } bfs(1); for(int i=1;i<=n;i++)if(!s||dist[i]>dist[s])s=i; bfs(s); for(int i=1;i<=n;i++)if(!t||dist[i]>dist[t])t=i; while(t!=s)bad[t]=true,t=par[t]; ans[s]=1; dfs(s,0); for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n?'\n':' '); return 0; }
#include <bits/stdc++.h> using namespace std; #define ALL(c) (c).begin(),(c).end() #define PB push_back #define IN(x,c) (find(c.begin(),c.end(),x) != (c).end()) #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 INIT(a,v) memset(a,v,sizeof(a)) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; } #define SPC << " " << #define DEBUG(x) cerr << #x << " = "; cerr << x << endl; #define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl; typedef pair<int,int> PII; typedef long long int64; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<double> VD; int n; int c[500][500]; int main() { //freopen("test.in","r",stdin); cin >> n; REP (i,n) REP (j,n) cin >> c[i][j]; if (n==1) { cout << "Yes" << endl; cout << 0 << endl; cout << c[0][0] << endl; return 0; } int ok=1; vector<int> dx={0},dy={0}; REP (i,n-1) { set<int> dif; REP (j,n) dif.insert(c[i+1][j]-c[i][j]); if (dif.size()!=1) { ok=0; break; } dy.PB(dy.back()+*dif.begin()); } int m=*min_element(ALL(dy)); for (int &x : dy) x-=m; REP (j,n-1) { set<int> dif; REP (i,n) dif.insert(c[i][j+1]-c[i][j]); if (dif.size()!=1) { ok=0; break; } dx.PB(dx.back()+*dif.begin()); } m=*min_element(ALL(dx)); for (int &x : dx) x-=m; int d=c[0][0]-(dy[0]+dx[0]); if (d<0) ok=0; else { for (int &x : dx) x+=d; } if (!ok) { cout << "No" << endl; return 0; } cout << "Yes" << endl; REP (i,n) { if (i!=0) cout << " "; cout << dy[i]; } cout << endl; REP (i,n) { if (i!=0) cout << " "; cout << dx[i]; } cout << endl; return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <functional> #include <cmath> #include <iomanip> #include <stack> #include <queue> #include <numeric> #include <map> #include <unordered_map> #include <set> #include <fstream> #include <chrono> #include <random> #include <bitset> //#include <atcoder/all> #define rep(i,n) for(int i=0;i<(n);i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) ((int)(x).size()) #define pb push_back using ll = long long; using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) {return a/gcd(a,b)*b;} int score(vector<int> v){ int res = 0; vector<int> cnt(10,0); rep(i,5){ cnt[v[i]]++; } rep(i,10){ if(i==0) continue; res += i*pow(10,cnt[i]); } //cerr << res << endl; return res; } int main(){ int K; cin >> K; string S, T; cin >> S >> T; vector<int> cnt(10,K); cnt[0]=0; rep(i,4){ cnt[S[i]-'0']--; cnt[T[i]-'0']--; } double ans = 0; for(int s=1;s<=9;s++){ for(int t=1;t<=9;t++){ vector<int> c=cnt; c[s]--; c[t]--; rep(i,10) if(c[i]<0)continue; double p; if(s==t){ p = (double)cnt[s]*(cnt[s]-1)/(9*K-8)/(9*K-9); } else{ p = (double)cnt[s]*cnt[t]/(9*K-8)/(9*K-9); } vector<int> ss, tt; rep(i,4){ ss.pb(S[i]-'0'); tt.pb(T[i]-'0'); } ss.pb(s); tt.pb(t); if(score(ss)>score(tt)) ans+=p; } } cout << fixed << setprecision(20) << ans << endl; return 0; }
#include <iostream> #include <algorithm> #include <stack> #include <queue> #include <vector> #include <bitset> #include <iomanip> #include <cmath> #include <set> #include <unordered_set> #include <functional> #include <map> #include <unordered_map> #include <string> #include <tuple> using namespace std; using ll = long long; #define pi 3.14159265358979323846 #define ee 2.71828182845904523536 #define INF 1e18+1 long long gcd(long long x, long long y) { // 最大公約数 if (y == 0)return x; else return gcd(y, x % y); } bool prime(int x) { for (int i = 2; i <= sqrt(x); i++) { if (x % i == 0)return false; } return true; } long long modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0)u += m; return u; } struct UnionFind { // UnionFind vector<int> par; vector<int> siz; UnionFind(int N) : par(N), siz(N, 1) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; if (par[rx] > par[ry])swap(rx, ry); siz[rx] += siz[ry]; par[ry] = rx; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } ll size(int x) { return siz[root(x)]; } }; int main() { ll n; cin >> n; ll cnt = 3; ll sum3 = 1; while (n > cnt) { if ((n - cnt) % 5 == 0) { ll cn5 = 5; ll sum5 = 1; while (n - cnt >= cn5) { if (n - cnt == cn5) { cout << sum3 << " " << sum5 << endl; return 0; } cn5 *= 5; sum5++; } } cnt *= 3; sum3++; } cout << -1 << endl; }
#include<bits/stdc++.h> using namespace std; #define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define mod 1000000007 #define endl "\n" #define test ll t; cin>>t; while(t--) typedef long long int ll; vector<ll>as; ll check(ll n){ if(n<1){ return -1; } for(int i=0;i<as.size();i++){ if(as[i]==n){ return i+1; } } return -1; } int main() { FIO; ll n,l=-1,r=-1,p,count=1; cin>>n; p=5ll; while(p<n){ as.push_back(p); p*=5; } p=3ll; while(true){ if(p<n && check(n-p)!=-1){ l=count; r=check(n-p); break; } if(p*3<n){ p*=3; count++; } else{ break; } } if(l==-1) cout<<"-1"<<endl; else cout<<l<<" "<<r<<endl; //cout<<(l==-1?"-1\n":l<<" "<<r<<endl;) return 0; }
#include<bits/stdc++.h> #define inf 1e18 #define endl "\n" #define mp make_pair #define pb push_back #define loop(i,x,y) for(int i = x; i < y ; i++ ) #define all(x) (x).begin(),(x).end() #define in(n) int n; cin>>n; #define inarr(arr,n) vector<int>arr(n); for(int i = 0; i < n ; i++){cin>>arr[i];} #define maploop(x) for(auto itr = x.begin(); itr != x.end();itr++) #define int long long using namespace std; #ifndef ONLINE_JUDGE #define debug(x) cerr << #x<<" "; _print(x); cerr << endl; #else #define debug(x); #endif void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(double t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ const int N = 10001; int mod = 1000000007; void Main() { string s; cin >> s; loop(i, 1, s.size()) { cout << s[i]; } cout << s[0] << endl; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while (t--) { Main(); } }
#include<bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x){ x=0;bool f=false;char ch=getchar(); while(!isdigit(ch)){f|=ch=='-',ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } template <typename T> inline void write(T x){ if(x<0) x=-x,putchar('-'); if(x>9) write(x/10); putchar(x%10^48); return ; } const int N=2e5+25,MOD=998244353; #define ll long long int n,a[N],t[N],sta[N],top; bool vis[N]; signed main(){ read(n); for(int i=1;i<=n;i++) read(a[i]),t[a[i]]=i; for(int i=1;i<=n;i++){ while(t[i]>i){ if(vis[t[i]-1]){write(-1);return 0;} int tmp=t[i]; t[i]=tmp-1,t[a[tmp-1]]=tmp; swap(a[tmp],a[tmp-1]);vis[t[i]]=true; sta[++top]=t[i]; } while(t[i]<i){ if(vis[t[i]]){write(-1);return 0;} int tmp=t[i]; t[i]=tmp+1,t[a[tmp+1]]=tmp; swap(a[tmp],a[tmp+1]);vis[tmp]=true; sta[++top]=tmp; } } if(top!=n-1) write(-1); else{for(int i=1;i<=top;i++) write(sta[i]),putchar('\n');} return 0; }
#include <bits/stdc++.h> #include <cmath> #include <limits> using namespace std; long long INF = (1LL<<62); typedef long long LL; LL f(LL X, LL x){ LL u = max(X, x); LL v = min(X, x); LL r = u % v; if(r == 0) return v; return f(v, r); } int main(int argc, char* argv[]){ cin.tie(0); ios::sync_with_stdio(false); LL N; cin >> N; vector<LL> A; for(int i=0; i<N; i++){ LL a; cin >> a; A.push_back(a); } sort(A.begin(), A.end()); LL X = A[A.size()-1]; LL x = A[0]; if(N == 1){ printf("%lld\n", A[0]); return 0; } LL ans = A[0]; for(int i=1; i<N; i++) ans = f(ans, A[i]); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ffor(i, a, b) for (int i = a ; i <= (int) b ; i++) #define rfor(i, a, b) for (int i = (int) b ; i >= a ; i--) #define vec vector #define PB push_back #define MP make_pair #define MT make_tuple #define F first #define S second using ll = long long int; // watch out for overflows in your code using pii = pair<int,int>; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void debug() { cout << "\n"; }; template <typename T, typename... Targs> void debug(T t, Targs... args) { cout << t << " "; debug(args...); } template <typename T1, typename T2> inline ostream& operator << (ostream& os, const pair<T1,T2>& p) { return os << "(" << p.F << ", " << p.S << ")"; } template <typename T> inline ostream& operator << (ostream& os, const vec<T>& v) { for(auto& t : v) { os << t << " "; } return os; } template <typename T> inline ostream& operator << (ostream& os, const vec<vec<T>>& m) { for(auto& v : m) { for(auto& t : v) { os << t << " "; } os << "\n"; } return os; } template <typename T> inline ostream& operator << (ostream& os, const set<T>& v) { for(auto& t : v) { os << t << " "; } return os; } template <typename K, typename V> inline ostream& operator << (ostream& os, const map<K,V>& m) { for(auto& p : m) { os << p.F << " -> " << p.S << "\n"; } return os; } //////////////////////////////////////////////////////////////////////// template <ll mod> class modular_int { private: ll x; public: modular_int(ll y) : x( (y % mod + mod) % mod) {} modular_int() : x(0LL) {} ll rpr() const { return x; } modular_int<mod>& operator += (const modular_int<mod>& rhs) { x = (x + rhs.x) % mod; return *this; } friend modular_int<mod> operator + (modular_int<mod> lhs, const modular_int<mod>& rhs) { lhs += rhs; return lhs; } modular_int<mod>& operator *= (const modular_int<mod>& rhs) { x = (x * rhs.x) % mod; return *this; } friend modular_int<mod> operator * (modular_int<mod> lhs, const modular_int<mod>& rhs) { lhs *= rhs; return lhs; } modular_int<mod>& operator -= (const modular_int<mod>& rhs) { x = ((x - rhs.x) % mod + mod) % mod; return *this; } friend modular_int<mod> operator - (modular_int<mod> lhs, const modular_int<mod>& rhs) { lhs -= rhs; return lhs; } }; const ll MOD = 1e9 + 7; using mint = modular_int<MOD>; //////////////////////////////////////////////////////////////////////// int N; vec<int> A; map<int, int> m; void compute_m() { sort(A.begin()+1, A.end()); int cur_h = A[N]; rfor(i, 1, N) { int h = A[i]; int n = N-i+1; if(h < cur_h) { m[cur_h] = n-1; cur_h = h; } } m[cur_h] = N; } vec<ll> g; void compute_g() { auto it = m.rbegin(); int cur_h = it->F; int cur_n = it->S; advance(it, 1); for(; it != m.rend(); ++it) { auto [h, n] = *it; g.PB((ll) (cur_h-h)); cur_h = h; } g.PB((ll) cur_h); } //////////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N; A.assign(N+1, 0); ffor(i, 1, N) { cin >> A[i]; } compute_m(); compute_g(); //debug(m); //debug(A); //debug(g); mint ans(1LL); for(ll x : g) { ans *= mint(x+1LL); } cout << ans.rpr() << "\n"; }
#include <bits/stdc++.h> #define rep(i,n) for(ll i=0;i<n;++i) #define rrep(i, n) for(ll i=n-1;i>=0;--i) #define rep1(i, n) for(ll i=1; i<=n; ++i) #define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr) #define ALL(a) (a).begin(),(a).end() template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}} template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}} using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using pll = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll LINF = 1LL << 60; const int INF = 1e9 + 7; const double PI = 3.1415926535897932384626; ll mod(ll a, ll m){return (a + m) % m;} int main(){ ll a, b, c; cin >> a >> b >> c; if(a* a + b * b < c*c)cout << "Yes"; else cout << "No"; }
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long #define Test(tt) cout << "Case #" << tt << ": " using namespace std; int main() { #ifdef HOME ifstream cin("A.in"); ofstream cout("A.out"); #endif int i; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); vector<int> arr(4); int tot = 0; for(i = 0; i < 4; i++) { cin >> arr[i]; tot += arr[i]; } for(int conf = 0; conf < 16; conf++) { int sum = 0; for(int i = 0; i < 4; i++) { if(conf & (1 << i)) { sum += arr[i]; } } if(sum * 2 == tot) { cout << "Yes"; return 0; } } cout << "No"; return 0; }
/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll n; cin>>n; ll a[n]; ll b[n]; ll c[n]; for(ll i=0;i<n;i++){ cin>>a[i]; } for(ll i=0;i<n;i++){ cin>>b[i]; } map<ll,ll>freq; for(ll i=0;i<n;i++){ cin>>c[i]; freq[c[i]]++; } sort(a,a+n); ll ans=0; for(auto i:freq){ ll temp=upper_bound(a,a+n,b[i.first-1])-lower_bound(a,a+n,b[i.first-1]); ans+=(temp*i.second); } cout<<ans<<'\n'; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; #define ll long long #define INF INT_MAX #define FIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); #define mod 1000000007ll #define forn(i,n) for(int i=0;i<n;i++) #define forab(i,a,b) for(int i=a;i<=b;i++) #define forba(i,b,a) for(int i=b;i>=a;i--) #define debx(x) cout << #x << ": " << x << endl #define debxy(x,y) cout << #x << ": " << x << ' ' << #y << ": " << y << endl #define YESp cout << "YES" << endl #define NOp cout << "NO" << endl #define Yesp cout << "Yes" << endl #define Nop cout << "No" << endl #define write(a) cout << a << endl #define ff first #define ss second #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define vi vector<int> #define vll vector<ll> #define vvi vector<vector<int>> #define vpi vector<pair<int, int>> #define pq priority_queue ll fast_exp(ll base, ll power){ll ans=1;while(power){if(power&1)ans=(ans*base)%mod;base=(base*base)%mod;power=power>>1;}return ans%mod;} ll modInv(ll a,ll p){return fast_exp(a,p-2)%p;} bool is_prime(int n){if(n==1){return false;}int i=2;while(i*i<=n){if(n%i==0){return false;}i+=1;}return true;} ll gcd(ll a,ll b){if(a==0)return b;return gcd(b%a,a);} ll modFact(ll n){if(n>=mod)return 0;ll result=1;for(ll i = 1; i <= n; i++)result=(result*i)%mod;return result;} pii moves[] = {{-1, 0}, {1, 0}, {0 ,-1}, {0, 1}}; /****** S ****** X ****** T ****** Y ****** A ****** M ******/ int main() { FIO // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", sto"); int n; cin >> n; int a[n], b[n], c[n]; forn(i, n) cin >> a[i]; forn(i, n) cin >> b[i]; forn(i, n) cin >> c[i]; map<int, ll> mp1, mp2, mp3; forn(i, n) mp1[c[i]]++; forn(i, n) { if(mp1.count(i+1)) mp2[b[i]] += mp1[i+1]; else mp2[b[i]] += 0; } forn(i, n) mp3[a[i]]++; ll ans = 0; for(auto it : mp3) { if(mp2.count(it.ff)) ans += (mp2[it.ff] * it.ss); } cout << ans << endl; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <x86intrin.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define V vector #define sz(x) int(x.size()) template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;} template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;} template<typename T> void print(T a) {cout << a << ' ';} template<typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;} template<typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);} V<int> dx = {-1, 1, 0, 0, -1, -1, 1, 1}; V<int> dy = { 0, 0, -1, 1, -1, 1, -1, 1}; int main () { int n, m; cin >> n >> m; V<V<int>> p(n); rep(i, n) p[i].emplace_back(0); rep(i, n) { int a; cin >> a; p[a].emplace_back(i+1); } rep(i, n) p[i].emplace_back(n+1); V<int> d(n, 0); rep(i, n) { rep(j, sz(p[i])-1) chmax(d[i], p[i][j+1] - p[i][j]); } int ans = n; rep(i, n) { if (d[i] >= m+1) chmin(ans, i); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define mk make_pair #define fs first #define sc second using namespace std; typedef long long ll; typedef long double ld; // please, read the question correctly (do you need set or multiset)??? const int N=2000010; //check the limits, dummy int a[N]; int n, m; vector<int> pos[N]; int main(){ scanf("%d%d",&n,&m); for(int i=0; i<=n; ++i){ pos[i].push_back(-1); } for(int i=0; i<n; ++i){ scanf("%d",a+i); pos[a[i]].push_back(i); } int ans = -1; for(int i=0;i<=n; ++i){ pos[i].push_back(n); for(int j=0; j+1<pos[i].size(); ++j){ if(pos[i][j+1]-pos[i][j]-1>=m){ ans = i; break; } } if(ans!=-1)break; } cout<<ans<<endl; }
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <iomanip> #include <functional> #include <deque> #include <stack> #include <queue> #include <bitset> #include <map> using namespace std; typedef long long ll; #define rep(i,n) for (ll i = 0; i < (ll)(n); i++) #define REP(i,n) for (ll i = 1; i <= (ll)(n); i++) #define all(v) v.begin(), v.end() #define fcout cout<<fixed<<setprecision(15) #define PI 3.1415926535897932384626433832795028841971693993751058209749445923078160628620899 const ll MOD = 1000000007; const ll INF = 1000000000000000000; const ll NIL = -1; ll N; vector<ll> prime; bool isPrime(ll N) { ll T = sqrt(N); for (int i = 2; i <= T; i++) { if (N % i == 0) return false; } return true; } int main() { cin >> N; ll p = 0; for (int i = 2; i <= N; i++) { if (isPrime(i)) { prime.push_back(i); } } REP(i, N) { if (i > 1) cout << " "; ll Z = 1; ll k = 0; ll U = i; while (U > 1) { if (U % prime[k] == 0) { U /= prime[k]; Z++; } else k++; } cout << Z; } cout << endl; return 0; }
#ifndef BZ #pragma GCC optimize "-O3" #endif #include <bits/stdc++.h> #define ALL(v) (v).begin(), (v).end() #define rep(i, l, r) for (int i = (l); i < (r); ++i) using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; /* ll pw(ll a, ll b) { ll ans = 1; while (b) { while (!(b & 1)) b >>= 1, a = (a * a) % MOD; ans = (ans * a) % MOD, --b; } return ans; } */ int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cout.setf(ios::fixed), cout.precision(20); int n; cin >> n; if (n == 1) { cout << 1 << " " << 1 << "\n"; return 0; } int k = 0; while ((1 << k) < n) { ++k; } int mod = (1 << k); for (int i = 0; i < n; ++i) { int l = (i * 2) % mod; int r = (i * 2 + 1) % mod; if (l >= n) { l -= (1 << (k - 1)); } if (r >= n) { r -= (1 << (k - 1)); } cout << l + 1 << " " << r + 1 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define FOR(i,m) for(int i = 0 ;i < m ;i++) #define mem(a ,b) memset(a, (b) , sizeof(a)) #define all(cont) cont.begin(),cont.end() #define mp make_pair #define pb push_back #define F first #define S second #define cn cout<<'\n' #define re return typedef pair<int,int> pi; typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef unsigned long long int ull; const int mod = 1e9 +7; const int maxN = 1e5 + 1; int n , m , w; void solve(){ cin >> n ; int a[n]; ll sum = 0; for(int i= 0 ;i < n;i++){ cin >> a[i]; sum += a[i]; } bool dp[n + 1][sum + 1]; memset(dp , 0 , sizeof(dp)); dp[0][0] = true; for(int i = 1 ;i <= n ;i++){ for(int j = 0 ;j <= sum ;j++){ dp[i][j] = dp[i-1][j]; if(j - a[i-1] >= 0)dp[i][j] |= dp[i-1][j-a[i-1]]; } } for(int j= sum/2 ; j>= 0 ;j--){ if(dp[n][j]){ cout<<sum-j<<"\n"; return; } } } int32_t main(){ ios; int _t = 1; // cin >> _t; for(int i = 1 ; i <= _t ;i++){ // cout<< "Case #"<<i<<": "; solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #ifndef aa #define trace(...) #define endl '\n' #endif #define pb push_back #define ub upper_bound #define lb lower_bound #define fi first #define se second #define int long long typedef long long ll; typedef long double ld; #define double long double #define pii pair<int,int> #define pdd pair<double,double> #define pll pair<ll,ll> #define sz(x) ((long long)x.size()) #define fr(a,b,c) for(int a=b; a<=c; a++) #define frev(a,b,c) for(int a=c; a>=b; a--) #define rep(a,b,c) for(int a=b; a<c; a++) #define trav(a,x) for(auto &a:x) #define all(con) con.begin(),con.end() #define deb(x) cout << x << endl #define done(x) {cout << x << endl;return;} #define mini(x,y) x=min(x,y) #define maxi(x,y) x=max(x,y) const ll infl = 0x3f3f3f3f3f3f3f3fLL; const int infi = 0x3f3f3f3f; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); //const int mod = 998244353; const int mod = 1e9 + 7; typedef vector<int> vi; typedef vector<string> vs; typedef vector<vector<int>> vvi; typedef vector<pair<int, int>> vpii; typedef map<int, int> mii; typedef set<int> si; typedef set<pair<int,int>> spii; typedef queue<int> qi; const int N = 20; int x[N], y[N], z[N], dp[1<<N]; vi g[1<<N]; int d(int i, int j){ return abs(x[j]-x[i])+abs(y[j]-y[i])+max(0ll,z[j]-z[i]); } void solve() { int n; cin>>n; rep(i,0,n) cin>>x[i]>>y[i]>>z[i]; dp[1] = 0; g[1] = {0,0}; rep(msk, 3, 1<<n){ if(msk%2==0) continue; vi v; int val = infl; rep(i,1,n){ if(msk>>i&1 == 0) continue; int msk1 = msk ^ (1<<i); rep(j,0,sz(g[msk1])-1){ int dis = dp[msk1] - d(g[msk1][j],g[msk1][j+1]) + d(g[msk1][j], i) + d(i, g[msk1][j+1]); //cout<<dis<<endl; if(dis>=val)continue; val = dis; v.clear(); rep(k,0,j+1) v.pb(g[msk1][k]); v.pb(i); rep(k,j+1,sz(g[msk1])) v.pb(g[msk1][k]); } } dp[msk] = val; g[msk] = v; //cout<<dp[msk]<<endl; } cout<<dp[(1<<n)-1]; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); //srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout << fixed << setprecision(15); int t = 1; //cin >> t; while (t--) solve(); #ifdef aa cout << endl << endl << endl << endl << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl; #endif return 0; } int powm(int a, int b) { int res = 1; while (b) { if (b & 1) res = (res*a) % mod; a = (a * a) % mod; b >>= 1; } return res; } int divide(int a, int b) { return (a % mod) * powm(b, mod - 2) % mod; } int norm(int a) { while (a >= mod) a -= mod; while (a < 0) a += mod; return a; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
#include <bits/stdc++.h> using namespace std; // using mint = long double; // using mint = modint998244353; // using mint = modint1000000007; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> T; typedef pair<ll, vector<ll>> Pd; const ll INF = 1e18; const ll fact_table = 820000; priority_queue<ll> pql; priority_queue<P> pqp; // big priority queue priority_queue<ll, vector<ll>, greater<ll>> pqls; priority_queue<P, vector<P>, greater<P>> pqps; // small priority queue // top pop ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1}; //↓,→,↑,← #define p(x) cout << x << "\n"; #define el cout << "\n"; #define pe(x) cout << x << " "; #define ps(x) cout << fixed << setprecision(25) << x << endl; #define pu(x) cout << (x); #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define pc(x) cout << x << ","; #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = a; i <= (b); i++) #define rep3(i, a, b) for (ll i = a; i >= (b); i--) #define all(c) begin(c), end(c) #define sorti(v) sort(all(v)) #define sortd(v) \ sort(all(v)); \ reverse(all(v)); #define SUM(v) accumulate(all(v), 0LL) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) typedef vector<ll> vec; typedef vector<vector<ll>> mat; // vec v(n) -> 長さnのベクトルを宣言 // mat dp(h, vec(w)) -> h * w の行列を宣言 const ll mod = 1000000007ll; // const ll mod = 998244353ll; ll mypow(ll a, ll b, ll m = mod) { ll x = 1; while (b) { while (!(b & 1)) { (a *= a) %= m; b >>= 1; } (x *= a) %= m; b--; } return x; } vec rv(ll read) { vec res(read); for (int i = 0; i < read; i++) { cin >> res[i]; } return res; } /* ll fact[fact_table + 5], rfact[fact_table + 5]; void c3_init() { fact[0] = rfact[0] = 1; for (ll i = 1; i <= fact_table; i++) { fact[i] = (fact[i - 1] * i) % mod; } rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod); for (ll i = fact_table; i >= 1; i--) { rfact[i - 1] = rfact[i] * i; rfact[i - 1] %= mod; } return; } ll c3(ll n, ll r) { return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod; } */ bool icpc = false; bool multicase = false; ll n; ll x[100005]; ll dp[100005]; bool solve() { cin >> n; vec v = rv(n); sorti(v); ll mid = v[(n + 1) / 2 - 1]; ll sum2 = mid * n; sum2 += SUM(v) * 2ll; for (auto e : v) { sum2 -= min(e, mid) * 2ll; } long double ans = sum2 / 2.0; ans /= n; ps(ans); return true; } /* */ int main() { // ios::sync_with_stdio(false); // cin.tie(nullptr); if (icpc) { while (solve()) ; return 0; } ll q, testcase = 1; if (multicase) { cin >> q; } else { q = 1; } while (q--) { // cout << "Case #" << testcase << ": "; solve(); testcase++; } // solve(); return 0; }
#include <iostream> #include <vector> //#include <string> #include <algorithm> //#include <math.h> //#include <queue> //#include <stack> //#include <iomanip> // sometimes used //#include <set> //#include <map> //#include <numeric> //#include <list> //#include <deque> //#include <unordered_map> typedef long long LL; //typedef long double LD; using namespace std; //#define MOD 1000000007 //#define MOD 998244353 //#define MAX 200100 //#define NIL -1 //#define INFTY 1000000000000000000 int main(){ LL n; cin >> n; vector<LL> a(n); for(LL i=0; i<n; i++){ cin >> a[i]; } sort(a.begin(),a.end()); while(a[0]!=a[n-1]){ for(LL i=1; i<n; i++){ if(a[i]%a[0]==0){ a[i]=a[0]; }else{ a[i]=a[i]-(a[i]/a[0])*a[0]; } } sort(a.begin(),a.end()); } cout << a[0] << endl; return 0; }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- template<int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) { } ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; }; template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } typedef ModInt<1000000007> mint; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N; ll A[3010]; mint dp[3010][3010]; // dp[i][k] := i番目まででk組作った mint tot[3010][3010]; // tot[k][mo] := k組作っていてこれまでの総和を(k+1)で割った余りがmoである //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 0, N) cin >> A[i]; dp[0][0] = 1; tot[0][0] = 1; ll sum = 0; rep(i, 0, N) { sum += A[i]; rep(k, 1, N + 1) dp[i + 1][k] = tot[k - 1][sum % k]; rep(k, 1, N + 1) tot[k][sum % (k + 1)] += dp[i + 1][k]; } mint ans = 0; rep(k, 1, N + 1) ans += dp[N][k]; cout << ans << endl; }
#pragma GCC optimize("O3") #pragma GCC target("sse4") #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using pi = pair<int, int>; using vpi = vector<pair<int, int>>; using pl = pair<ll, ll>; using vl = vector<ll>; #define all(v) (v).begin(), (v).end() #define ar array #define PB push_back #define sz(x) (int)(x).size() template <typename T> using pqg = priority_queue<T, vector<T>, greater<T>>; template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream &operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif 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); } }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int INF = 1e9; const ll LINF = 1e18; const int MOD = 1e9 + 7; //998244353 const int N = 3005; int dp[N][N], mem[N][N]; ll sum[N]; void solve() { int n; cin >> n; vl a(n+1); for (int i=1;i<=n;++i) cin >> a[i], sum[i] = sum[i-1]+a[i]; mem[1][0] = dp[0][0] = 1; for (int i=0;i<n;++i){ for (int j=1;j<=n;++j){ dp[i+1][j] = mem[j][sum[i+1]%j]; } for (int j=2;j<=n;++j){ mem[j][sum[i+1]%j] += dp[i+1][j-1]; mem[j][sum[i+1]%j]%=MOD; } } ll ans = 0LL; for (int i=0;i<=n;++i) ans = (ans+dp[n][i])%MOD; cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); }
#include <bits/stdc++.h> #define rep(i,n) for (ll i = 0; i < (ll)(n); i++) #define rrep(i,a,b) for (ll i = (a); i < (ll)(b); i++) #define rng(x) (x).begin(), (x).end() #define pb push_back #define eb emplace_back #define fr first #define sc second using namespace std; template <class T, class U> inline bool chmax(T &a, const U &b) { return (a < b ? a = b, 1 : 0); } template <class T, class U> inline bool chmin(T &a, const U &b) { return (a > b ? a = b, 1 : 0); } template <class T> using vc = vector<T>; template <class T> using vv = vc<vc<T>>; template <class T> using vvv = vc<vv<T>>; template <class T> using PQ = priority_queue<T, vc<T>, greater<T>>; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>; using vi = vc<int>; using vl = vc<ll>; using vvi = vv<int>; using vvl = vv<ll>; const ld pi = 3.141592653589793; const ll linf = 1001001001001001001LL; const int inf = 1001001001; int main() { string s; cin >> s; int n = s.size(); int ans = inf; rrep(i,1,1<<n) { ll sum = 0; rep(j,n) if (i>>j&1) sum += s[j]-'0'; if (sum%3 == 0) chmin(ans, n-__builtin_popcountll(i)); } cout << (ans == inf ? -1 : ans) << endl; }
#include <bits/stdc++.h> #define ln '\n' #define all(dat) dat.begin(), dat.end() #define loop(i, to) for (int i = 0; i < to; ++i) #define cont(i, to) for (int i = 1; i <= to; ++i) #define circ(i, fm, to) for (int i = fm; i <= to; ++i) #define foreach(i, dat) for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i) typedef long long num; using namespace std; template<int mod> struct modulo { typedef modulo T; int w; operator int() const { return w; } modulo() {} modulo(int w) { this->w = w; fix(); } template<class A> modulo(A w) { this->w = w % mod; fix(); } inline void fix() { if (w >= mod || w < -mod) w %= mod; if (w < 0) w += mod; } inline T operator - () const { return mod - w; } inline T& operator += (const T &b) { if ((w += b.w) >= mod) w -= mod; return *this; } inline T& operator -= (const T &b) { if ((w -= b.w) < 0) w += mod; return *this; } inline T& operator *= (const T &b) { w = (unsigned long long) w * b.w % mod; return *this; } inline T& operator /= (const T &b) { return *this *= b.inv(); } #define make_operation(P)\ friend inline T operator P (const T &a, const T &b) { return T(a) P##= b; }\ template<class U> friend inline T operator P (const T &a, const U &b) { return T(a) P##= b; }\ template<class U> friend inline T operator P (const U &a, const T &b) { return T(a) P##= b; } make_operation(+) make_operation(-) make_operation(*) make_operation(/) #undef make_operation inline T inv() const { int a = w, m = mod, u = 0, v = 1, t; for (; a != 0; t = m / a, m -= t * a, swap(a, m), u -= t * v, swap(u, v)); return u; } friend inline T pow(const T &w, num p) { T a = w, r = 1; for (; p > 0; a *= a, p >>= 1) if (p & 1) r *= a; return r; } }; const int mod = 998244353; typedef modulo<mod> rem; const int nsz = 1e2, asz = 1e2, msz = nsz * asz; int n, m, a[nsz + 5], pre[nsz + 5]; rem ans, dp[2][nsz + 5][msz + 5]; int main() { scanf("%d", &n); cont (i, n) scanf("%d", &a[i]), pre[i] = pre[i - 1] + a[i]; m = pre[n] / 2; if (pre[n] & 1) { printf("0\n"); return 0; } int o = 0, x = 1; dp[o][0][0] = 1; loop (i, n) { int w = a[i + 1], mx = min(pre[i], m); circ (k, 0, i) circ (a, 0, mx) { rem val = dp[o][k][a]; if (!val) continue; dp[x][k + 1][a + w] += val * (k + 1); dp[x][k][a] += val * (i - k + 1); } swap(o, x); memset(dp[x], 0, sizeof(dp[x])); } circ (k, 0, n) ans += dp[o][k][m]; printf("%d\n", ans.w); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define pb emplace_back #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 pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; typedef multiset<ll> S; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; 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> 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 outvvp(T v){rep(i,v.size())outvp(v[i]);} 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]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void noyes(T b){if(b)out("no");else out("yes");} template<class T> void NoYes(T b){if(b)out("No");else out("Yes");} template<class T> void NOYES(T b){if(b)out("NO");else out("YES");} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} int main(){ ll n;cin>>n; vi v(n); rep(i,n){ cin>>v[i]; v[i]--; } vi id(n); rep(i,n)id[v[i]]=i; vi ans; rep(i,n-1){ ll k=id[i]; for(int j=k;j>=i+1;j--){ ans.pb(j); id[v[j-1]]++; id[i]--; swap(v[j-1],v[j]); } if(ans.size()>=n)dame(-1); } rep(i,n)if(v[i]!=i)dame(-1); if(ans.size()!=n-1)dame(-1); vi used(n-1,false); rep(i,n-1){ if(used[ans[i]-1])dame(-1); used[ans[i]-1]=true; } for(ll x:ans)out(x); }
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define DEBUG #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef pair<int,int> pii; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = (1<<30)-1; const ll linf = 1LL<<61; const int MAX = 10100000; int dy[8] = {0,1,0,-1,1,-1,-1,1}; int dx[8] = {-1,0,1,0,1,-1,1,-1}; const double pi = acos(-1); const double eps = 1e-7; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ int sz = a.size(); for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr; sz--; if(sz) cout << " "; } cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){ cout << a << " " << b << "\n"; } template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} //const int mod = 1e9 + 7; const int mod = 998244353; int main(){ ll n,k; cin >> n >> k; while(k--){ string s; while(n){ s.push_back(n % 10); n /= 10; } sort(rall(s)); ll t = 0; for(auto c : s){ t = t * 10 + c - '0'; } reverse(all(s)); ll u = 0; for(auto c : s){ u = u * 10 + c - '0'; } n = t - u; } cout << n << "\n"; }
#include <iostream> #include <cmath> using namespace std; int main(){ int minimum; cin >> minimum; for (int i = 0; i < 3; i++){ int temp; cin >> temp; minimum = min(minimum, temp); } cout << minimum << endl; }
/*** ..*.*... \O/..*..* *.....*...|....*... *......../.\....*..* */ #include<bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; cout << min(min(min(a, b), c), d); }
// Author: wlzhouzhuan #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pb push_back #define fir first #define sec second #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, t) memset(s, t, sizeof(s)) #define mcpy(s, t) memcpy(s, t, sizeof(t)) template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); return f ? -x : x; } template<typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } template<typename T> void print(T x, char let) { print(x), putchar(let); } int n; int main(){ n=read(); printf("%d\n",(1<<n)-1); for(int i=1;i<(1<<n);++i){ for(int j=0;j<(1<<n);++j){ putchar((__builtin_popcount(i&j)&1)+'A'); } putchar('\n'); } return 0; }
/** * created: 16/01/2021, 14:27:24 **/ #include <bits/stdc++.h> using namespace std; const int max_n = 1111, inf = 1000111222; int n, cnt[max_n][max_n]; vector<string> ans; int c2(int n) { return n * (n - 1) / 2; } bool get_bit(int mask, int pos) { return (mask >> pos) & 1; } void check() { for (string s : ans) { if (!(s.length() == (1 << n))) { cout << s.length() << endl; cout << (1 << n) << endl; exit(2); } for (int i = 0; i < s.length(); ++i) { for (int j = i + 1; j < s.length(); ++j) { cnt[i][j] += s[i] == s[j]; } } } for (int i = 0; i < (1 << n); ++i) { for (int j = i + 1; j < (1 << n); ++j) { cout << cnt[i][j] << " "; } cout << endl; } } vector<string> solve(int n) { if (n == 1) { return {"AB"}; } vector<string> v = solve(n - 1); vector<string> res; for (string s : v) { string t = s; for (int i = 0; i < t.size(); ++i) { t[i] ^= 'A' ^ 'B'; } res.push_back(s + s); res.push_back(s + t); } string s; for (int i = 0; i < (1 << (n - 1)); ++i) { s += 'A'; } for (int i = 0; i < (1 << (n - 1)); ++i) { s += 'B'; } res.push_back(s); return res; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin >> n; /*int a = c2(1 << (n - 1)) * 2; int b = c2(1 << n); int lc = a / __gcd(a, b) * b; cout << a << " " << b << " " << lc << endl << lc / a << endl;*/ /* ans.push_back("AABB"); ans.push_back("ABAB"); ans.push_back("ABBA"); */ ans = solve(n); /*ans.push_back("AAAABBBB"); ans.push_back("AABBAABB"); ans.push_back("ABABABAB"); ans.push_back("ABBAABBA"); ans.push_back("AABBBBAA"); ans.push_back("ABABBABA"); ans.push_back("ABBABAAB");*/ cout << ans.size() << "\n"; for (auto s : ans) { cout << s << "\n"; } //check(); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author NikuKyabe */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i) #define REV(i, a, b) for (int i = (b) - 1; i >= (a); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001l #define MOD 1000000007 #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; /** * [l >= 0, r]にある1e4の倍数 * @param l * @param r * @return */ int g(int l, int r){ return r / (int)1e4 - l / (int)1e4; } /** * [l, r]にある,1e4の倍数 * @param l * @param r * @return */ int f(int l, int r){ if(r < 0) return g(-r-1, -l); if(l > 0) return g(l-1, r); return g(0,r) + g(0, -l) + 1; } int sqr(int x){ int r = sqrt(x) - 1; while((r+1) * (r+1) <= x)r++; return r; } class DCircleLatticePoints { public: void solve(std::istream& cin, std::ostream& cout) { double x,y,r; cin >> x >> y >> r; x *= 1e4; y *= 1e4; r *= 1e4; int X = round(x); int Y = round(y); int R = round(r); int now_x = X - R; while(now_x % (int)1e4 != 0) now_x++; int ans = 0; while(now_x <= X + R){ int t = sqr(R * R - (X - now_x) * (X - now_x)); ans += f(Y - t, Y + t); now_x += 1e4; } cout << ans << endl; } }; signed main() { DCircleLatticePoints solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include<bits/stdc++.h> #define rep(i,N) for(int i=0;i<(N);i++) #define rrep(i, n) for (int i = (int)n-1; i >= 0; --i) #define FOR(i,a,b) for(int i=(a);i<(b);i++) using namespace std; const long long INF = 1e12; const int inf = 1e9; const int mod = 1e9+7; typedef long long ll; typedef pair<ll,int> P; typedef set<int> S; 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; } long long pow(long long x, long long n, long long MOD) { long long ret = 1; while (n > 0) { if (n & 1) ret = ret * x % MOD; // n の最下位bitが 1 ならば x^(2^i) をかける x = x * x % MOD; n >>= 1; // n を1bit 左にずらす } return ret; } int main(){ ll a, b, c; cin >> a >> b >> c; ll cnt = 0; // a は何回かけたら一周するか set<int> si; ll now = a; while(1){ if(si.count(now*a%10ll)){ break; }else{ si.insert(now * a % 10ll); now *= a % 10; now %= 10; } cnt++; } ll b_c = pow(b, c, cnt); if (!b_c){ b_c = cnt; } ll ans = pow(a, b_c, 10); cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define N 200005 #define re register #define ll long long #define P 998244353 using namespace std; int n,m,K,q,u; inline void Rd(int &res){ re char c;res=0;bool f=0; while(c=getchar(),c<48)(c=='-')&&(f=1); do res=(res<<3)+(res<<1)+(c^48); while(c=getchar(),c>47); f&&(res=-res); } int f[25][N],len,fac[N],fac1[N]; inline void Mod(int &x){x>=P&&(x-=P);} inline int C(int n,int m){ if(n<m)return 0; return 1ll*fac[n]*fac1[m]%P*fac1[n-m]%P; } int Pow(int x,int k){ int res=1; while(k){ if(k&1)res=1ll*res*x%P; x=1ll*x*x%P; k>>=1; } return res; } int main(){ Rd(n),Rd(m); for(re int i=1;i<=m;i++)f[1][i]=1; for(re int i=2;;i++){ bool bo=0; for(re int j=1;j<=m;j++)if(f[i-1][j]){ for(re int k=2;;k++) if(j*k<=m){ bo=1; Mod(f[i][j*k]+=f[i-1][j]); } else break; } if(!bo){ len=i-1; break; } } fac[0]=1;for(re int i=1;i<=n;i++)fac[i]=1ll*i*fac[i-1]%P; fac1[n]=Pow(fac[n],P-2);for(re int i=n-1;i>=0;i--)fac1[i]=1ll*(i+1)*fac1[i+1]%P; int ans=0; for(re int i=1;i<=len;i++){ for(re int j=1;j<=m;j++)if(f[i][j]){ ans=(ans+1ll*f[i][j]*C(n-1,i-1))%P; } } printf("%d\n",ans); return 0; }
#include<iostream> #include<cstdio> #include<map> using namespace std; #define N 100012 inline int sgn(int x){return (x==0)?0:((x>0)?1:(-1));} inline int Abs(int x){return (x>0)?x:(-x);} int n,L,a[N],b[N],c[N],la[N];bool fl=true;map<int,int>mp;long long ans=0; inline void Fin(int l,int r) { if(l+1>=r)return;int i,ql=l,qr=r,ls; for(i=l+1;i<r-1;i++)if((c[i]==1)&&(c[i+1]==(-1))){fl=false;return;} for(i=l;i<=r;i++)if(c[i]==(-1))ql=i;for(i=r;i>=l;i--)if(c[i]==1)qr=i; mp.clear();for(i=l;i<=ql;i++){if(c[i]){if(!mp.count(b[i]-i)){fl=false;return;}la[i]=mp[b[i]-i];}mp[a[i]-i]=i;} mp.clear();for(i=r;i>=qr;i--){if(c[i]){if(!mp.count(b[i]-i)){fl=false;return;}la[i]=mp[b[i]-i];}mp[a[i]-i]=i;} ls=l;for(i=l;i<=r;i++){if(c[i]&&(ls>la[i])){fl=false;return;}if(c[i])ls=max(ls,la[i]);} ls=r;for(i=r;i>=l;i--){if(c[i]&&(ls<la[i])){fl=false;return;}if(c[i])ls=min(ls,la[i]);} ls=r;for(i=ql;i>=l;i--)if(c[i]&&(la[i]!=ls)){ans+=i-la[i];ls=la[i];} ls=l;for(i=qr;i<=r;i++)if(c[i]&&(la[i]!=ls)){ans+=la[i]-i;ls=la[i];} } int main(){ scanf("%d%d",&n,&L);int i,l,r;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)scanf("%d",&b[i]);a[0]=b[0]=0;a[n+1]=b[n+1]=L+1; for(i=1;i<=n;i++)c[i]=sgn(b[i]-a[i]); for(i=0;(i<=(n+1))&&fl;i=r){l=i;r=i+1;while((r<=n)&&c[r])++r;Fin(l,r);}if(!fl){printf("-1");return 0;}printf("%lld",ans);return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define endl "\n"; #define rep(i, a, b) for (int i=a; i<b; i++) #define pre(i, a, b) for (int i=a; i>=b; i--) const int MAX_N = 1e5+5; const int MOD = 1e9 + 7; const int INF = 1e9; #define pb push_back #define fi first #define se second #define pii pair<int, int> void solve() { int n,m,q; cin>>n>>m>>q; vector<pii> a(n); int x,y; rep(i,0,n){ cin>>x>>y; a[i] = {y,x}; } sort(a.rbegin(),a.rend()); vector<int> box(m); rep(i,0,m) cin>>box[i]; int l,r; rep(i,0,q){ cin>>l>>r; --l,--r; multiset<int> ms; rep(i,0,m){ if(i<l || i>r){ ms.insert(box[i]); } } int ans = 0; rep(i,0,n){ auto itr = ms.lower_bound(a[i].se); if(itr!=ms.end()){ ans+=a[i].fi; ms.erase(itr); } } cout<<ans<<endl; } } signed main() { fast #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif cout.precision(10); cout<<fixed; int t = 1; // cin >> t; for (int i = 1; i <= t; i++) { // cout << "Case #" << i << ": "; solve(); } }
// ###### Fri Mar 5 20:57:40 IST 2021 #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #define forn(ii, a, b) for (int ii = a; ii < b; ii++) #define scan(any) for (auto &i : any) cin >> i; #define debug(x) cerr << #x << " " << x << " -- \n" #define int64 long long #define sized(x) (int)x.size() #define all(x) (x).begin(), (x).end() #define precise(x) cout << fixed << setprecision(9) << x << '\n'; using namespace std; using namespace __gnu_pbds; template <class T> using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 3e5+7; int test(){ int n,m,q; cin >> n >> m >> q; vector <pair<int,int>> baggage(n); for(auto& p : baggage) cin >> p.first >> p.second; sort(all(baggage)); vector <int> boxes(m); scan(boxes); int l,r; while(q--){ cin >> l >> r; l--,r--; int real_ans = 0; vector <pair<int,int>> b = baggage; vector <int> BOX; forn(j,0,l) BOX.push_back(boxes[j]); forn(j,r+1,m) BOX.push_back(boxes[j]); sort(all(BOX)); for(auto p : BOX){ int ans = -1; int last = n; int box_size = p; forn(i,0,n) if( b[i].first <= box_size){ last = i; if(b[i].second > 0) ans = max(ans, b[i].second); }else break; if(ans == -1) continue; forn(i,0,last+1) if(b[i].second == ans){ b[i].second = 0; break; } real_ans += ans; } cout << real_ans << '\n'; } return 0 ; } int main(){ ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int cases = 1; // cin >> cases; while(cases--) test(); return 0LL; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll n,cap,m,t; cin>>n>>m>>t; cap = n; vector<ll> A(m); vector<ll> B(m); for(int i=0; i<m; i++) cin>>A[i]>>B[i]; ll Time = 0, i=0; while(i<m){ cap -= A[i] - Time; //cout<<cap<<endl; if(cap <= 0){ cout<<"No"<<endl; return 0; } cap = min(n,cap + B[i] - A[i]); //cout<<cap<<endl; Time = B[i]; i++; } cap -= t - Time; if(cap <= 0){ cout<<"No"<<endl; } else{ cout<<"Yes"<<endl; } return 0; }
// Problem: B - Smartphone Addiction // Contest: AtCoder - AtCoder Beginner Contest 185 // URL: https://atcoder.jp/contests/abc185/tasks/abc185_b // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) // Problem: A - ABC Preparation // Contest: AtCoder - AtCoder Beginner Contest 185 // URL: https://atcoder.jp/contests/abc185/tasks/abc185_a // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> #define ll long long #define fast ios_base::sync_with_stdio(false);cin.tie(NULL) #define fr(i,a,b) for(int i=a;i<b;i++) #define frr(i,a,b) for(int i=a;i>=b;i--) //vector #define pb push_back #define vi vector<int> #define vl vector<ll> #define pp pop_back //pair #define pi pair<int ,int > #define mk make_pair #define ff first #define ss second //set #define si set<int> #define sit set<int>::iterator //bits #define bp(x) __builtin_clzll(x) #define case Case # using namespace std; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif fast; ll n,m,t;cin>>n>>m>>t; ll f=1,pre=0,mx=n; fr(i,0,m) { ll x,y;cin>>x>>y; n-=abs(x-pre); if(n<=0) f=0; n+=abs(x-y); n=min(n,mx); pre=y; } n-=abs(pre-t); if(n<=0) f=0; if(f) cout<<"Yes"; else cout<<"No"; return 0; }
#include <iostream> using namespace std; using ll = long long; template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} //atcoder 0,19,3,14,4,5,17 -96 ll ceil(double x); ll gcd(ll a, ll b); int main(){ cin.tie(0); ll n; cin>>n; for(ll i=1;i<=n;i++){ cout<<ceil(double(i-0)/2)<<" "<<n-int((n-i)/2)<<endl; } return 0; } ll ceil(double x){ //cout<<"double:"<<x<<"ll:"<<ll(x)<<endl; if(x==ll(x))return ll(x); else return ll(x)+1; }
#include <bits/stdc++.h> int n; int main(){ scanf("%d",&n); for (int i=0;i<n;i++) printf("%d %d\n",(i*2)%n+1,(i*2+1)%n+1); }
//BISMILLAHIR RAHMANIR RAHEEM //ALLAH IS WATCHING ME // Shoeb Akibul Islam // Dept of ICE, NSTU #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> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #define dua ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define i_love_u_huu dua long long t;cin >> t;while(t--) #define ses "\n" #define whp " " #define mxi 200003 #define mp make_pair #define pii pair<long long, long long> #define pf printf #define sf scanf #define ff first #define sob(z) (z).begin(), (z).end() #define ss second #define pb push_back #define rep0(i,a,b) for(int i=a; i<b; i++) #define rep1(i,a,b) for(int i=a; i<=b; i++) #define rep0in(i,a,b) for(int i=a-1; i>=b; i--) #define rep1in(i,a,b) for(int i=a; i>b; i--) #define repv(i,a) for(auto i=a.begin(); i!=a.end();++i) #define INF 0x3f3f3f3f #define CLR(a,b) memset(a,b,sizeof(a)); #define PI acos(-1) #define what_is(x) cerr<<x<<ses; //set <int, greater <int> > ::iterator it; //vector<pair<int,int> >a; //memset(arr,0,sizeof(hg)); //priority_queue <int, vector<int>, greater<int> > pq; /// string single character erase- /// s.erase(s.begin()+x); where s is st ring name /// ans x is index; /// transform(sl.begin(), sl.end(), sl.begin(), ::tolower); /// transform(su.begin(), su.end(), su.begin(), ::toupper); typedef long long v99; typedef unsigned long long ull; using namespace std; using namespace __gnu_pbds; v99 fx[4]= {1,-1,0,0}; v99 fy[4]= {0,0,1,-1}; v99 ox8[] = {0, 0, 1, 1, 1, -1, -1, -1}; v99 oy8[] = {1,-1, 1, -1, 0, 0, -1, 1}; bool sort2val(const pii &a,const pii &b) { if(a.second==b.second)return a.first<b.first; return a.second<b.second; } template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); } template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } typedef tree< v99, null_type, less<v99>, rb_tree_tag,tree_order_statistics_node_update> ott; typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset; /// find_by_order(k) – kth index a ki ache, pointer return korbe. /// order_of_key(x) – x kon position a ache , oita bole dibe /**bool prime[10000020];vector<v99>prm; void SieveOfEratosthenes(v99 n) { memset(prime, true, sizeof(prime)); for(v99 i=4;i<=n;i+=2)prime[i]=false; for (v99 p=3; p*p<=n; p+=2) { if (prime[p] == true) { /// Update all multiples of p for (v99 i=p*p; i<=n; i += 2*p) prime[i] = false; } } rep1(i,2,n)if(prime[i])prm.push_back(i); }*/ bool isPowerOfTwo (v99 x) { /* First x in the below expression is for the case when x is 0 */ return x && (!(x&(x-1))); } v99 pw(v99 a, v99 b) { v99 ans = 1; for(v99 i = 1; i <= b; ++i) ans = (ans * a); return ans; } void vout(auto a) { for(auto i:a)cout<<i<<whp;cout<<ses; } void mout(auto a) { for(auto i:a)cout<<i.ff<<whp<<i.ss<<ses; } void aout0(v99 a[],v99 n) { rep0(i,0,n)cout<<a[i]<<whp;cout<<ses; } void aout1(v99 a[],v99 n) { rep1(i,1,n)cout<<a[i]<<whp;cout<<ses; } template <class T> inline T bigmod(T n,T p,T m) { if(p==0)return 1; else if(p%2==0) { v99 val=bigmod(n,p/2,m); return (T)((val*val)%m); } else return (T)(((v99)n*(v99)bigmod(n,p-1,m))%m); } ///------------------------------------------------------------------------------------------------------- void solve() { /// code is here-> v99 n;cin>>n; set<v99>s; for(v99 i=1;i*i<=n;i++) { if(n%i==0)s.insert(i),s.insert(n/i); } for(v99 i:s)cout<<i<<ses; } signed main() { //dua //freopen("data.out","w",stdout); //while(1) //i_love_u_huu solve(); return 0; } /// Alhamdulillah...
#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; bool dist(ll x1,ll x2,ll y1,ll y2){ return y1 * x2 == y2 * x1; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; bool ans = false; cin>>n; vector<ll>x(n),y(n); rep(i,n)cin>>x[i]>>y[i]; rep(i,n)rep(j,i+1,n)rep(k,j+1,n){ if(ans)break; ans = dist(x[i] - x[j],x[i] - x[k],y[i] - y[j],y[i] - y[k]); } if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl; }
#include <bits/stdc++.h> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> using namespace std; const int nax = 105; vector<int> adj[nax]; set<pair<pii, int> > s; set<pair<pii, int> > zostaly; int n,m; int c[nax]; int ans[nax * nax * nax]; bool vis[nax]; set<pii> zero; int dep[nax]; void dfs(int v,int lvl) { dep[v] = lvl; vis[v] = true; for(int x : adj[v]) { if(vis[x] == false && c[x] == c[v]) { zero.insert(mp(v,x)); dfs(x,lvl+1); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=1;i<=m;i++) { int x,y; cin>>x>>y; s.insert(mp(mp(x,y),i)); } for(int i=1;i<=n;i++) cin>>c[i]; for(auto x : s) { int fr = x.st.st; int to = x.st.nd; int nbr = x.nd; if(c[fr] != c[to]) { if(c[fr] > c[to]) ans[nbr] = 0; else ans[nbr] = 1; } else { zostaly.insert(x); adj[fr].pb(to); adj[to].pb(fr); } } for(int i=1;i<=n;i++) { if(vis[i] == false) dfs(i,0); } for(auto x : zostaly) { pii cur = x.st; pii xd = mp(cur.nd,cur.st); if(zero.count(cur) || zero.count(xd)) { // w dol if(dep[cur.st] < dep[cur.nd]) ans[x.nd] = 0; else ans[x.nd] = 1; } else { int one = x.st.st; int two = x.st.nd; if(dep[one] > dep[two]) ans[x.nd] = 0; else ans[x.nd] = 1; } } for(int i=1;i<=m;i++) { if(ans[i] == 0) cout<<"->"<<"\n"; else cout<<"<-"<<"\n"; } return 0; }
#include "bits/stdc++.h" //#include "atcoder/all" using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" vector<int>to[100]; bool Check[100]; map<pair<int, int>, int>mp; vector<string>Ans(10000); vector<int>A(10000); vector<int>B(10000); void dfs(int v, int p = -1) { if (Check[v]) { return; } Check[v] = true; for (int e : to[v]) { if (p == e) { continue; } if (mp.count({ v,e })) { if (v == A[mp[{v, e}]]) { Ans[mp[{v, e}]] = "->"; } else { Ans[mp[{v, e}]] = "<-"; } mp.erase({ v,e }); mp.erase({ e,v }); } dfs(e, v); } } int main() { int N, M; cin >> N >> M; rep (i,M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<int>C(N); rep (i,N) { cin >> C[i]; } rep(i, M) { if (C[A[i]] == C[B[i]]) { to[A[i]].push_back(B[i]); to[B[i]].push_back(A[i]); mp[{ A[i], B[i] }] = i; mp[{ B[i], A[i] }] = i; continue; } if (C[A[i]] > C[B[i]]) { Ans[i] = "->"; } else { Ans[i] = "<-"; } } rep(i, N) { if (Check[i]) { continue; } dfs(i); } rep(i, M) { cout << Ans[i] << endl; } return 0; }
#define _USE_MATH_DEFIMES #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> const int MOD = 1'000'000'007; const int MOD2 = 998'244'353; const int INF = 1'000'000'000; //1e9 const int NIL = -1; const long long LINF = 1'000'000'000'000'000'000; // 1e18 const long double EPS = 1E-10; template<class T, class S> inline bool chmax(T &a, const S &b){ if(a < b){ a = b; return true; } return false; } template<class T, class S> inline bool chmin(T &a, const S &b){ if(b < a){ a = b; return true; } return false; } int main(){ int N; std::cin >> N; std::string s, t; std::cin >> s; t = s; int cnt(0); std::vector<int> f(N, -1); for(int i(0); i < s.length(); ++i){ if(s[i] == 'x' && i > 1){ if(s[i-2] == 'f' && s[i-1] == 'o'){ s = s.substr(0, i-2) + s.substr(i+1); i -= 3; } } } std::cout << s.length() << std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define en '\n' #define endl '\n' #define ll long long signed #define pb push_back #define append push_back #define all(v) (v).begin(),(v).end() #define FOR(i,a,b,k) for(int i=a; k>0?i<b:i>b; i+=k) #define fori(a) for(int i=0; i<a; i++) #define forj(a) for(int j=0; j<a; j++) #define fork(a) for(int k=0; k<a; k++) #define MOD 1000000007 #define MAX 1000010 #define mset(a,s,k) memset(a,k,s*sizeof(a[0])) #define ff first #define ss second #define __ <<" "<< #define _ <<en #define int ll ll pow_(ll a,ll e,ll m=MOD){ll r=1;while(e){if(e&1)r=r*a%m;a=a*a%m;e>>=1;}return r;} int inv(ll a) {return (int)(pow_(a,MOD-2,MOD));}int add(ll a, ll b){return (a+b+MOD)%MOD;} int mul(ll a, ll b) {return (MOD+a*b%MOD)%MOD;}int di(ll a, ll b){return mul(a,inv(b));}ll ceil(ll a,ll b){return a/b+(a%b!=0);} template<typename T1,typename T2>ostream&operator<<(ostream&O,pair<T1,T2>a){O<<"( ";O<<a.ff<<", "<<a.ss;O<<" )";return O;} template<typename T>ostream&operator<<(ostream&O,vector<T>V){O<<"[ ";fori(V.size())O<<V[i]<<(i==V.size()-1?"":", ");O<<" ]";return O;} template<typename T>ostream&operator<<(ostream&O,set<T>S){O<<"{ ";for(T i:S)O<<i<<", ";O<<" }";return O;} template<typename T1,typename T2>ostream&operator<<(ostream&O,map<T1,T2>S){O<<"{ ";for(auto i:S)O<<i<<", ";O<<" }";return O;} template<typename T=int>class print{public:vector<T>V;print(vector<T>v){V=v;cout<<" <"<<V<<"> ";} print(T*x,int N){fori(N)V.push_back(x[i]);cout<<" <"<<V<<"> ";}friend ostream&operator<<(ostream&O,print p){return O;}}; #define vi vector<int> #define tp pair<int,int> int arr[MAX]; int N,M; void reset(); vector<int> G[MAX]; int mi[MAX]; int dfs(int x){ int &r=mi[x]; if(r==INT_MIN){ for(auto i: G[x]){ r=max(r,dfs(i)); } } return max(r,arr[x]); } int solve(){ cin>>N>>M; reset(); fori(N){ cin>>arr[i];} fori(M){ int a,b; cin>>a>>b; G[a-1].push_back(b-1); } int res=INT_MIN; fori(N){ dfs(i);// res=max(res, dfs(i,INT_MAX)); } // cout<<print(mi,N) _; fori(N){ if(mi[i]!=INT_MIN) res=max(res,mi[i]-arr[i]); } cout<<res; return 0; } void reset(){ fori(N) { G[i].clear(); mi[i]=INT_MIN; } // Clear Vectors, maps, sets // Set default values of arr } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; while(t--){ solve(); // cout<<solve()<<en; // cout<<(solve()?"Yes":"No")<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 210; PII path[N]; int a[N]; int s[N]; bool st[N]; void print(int x, bool flag) { stack<int> sta; while(x != 0) { sta.push(path[x].second); x = path[x].first; } if (!flag) cout << sta.size() << ' '; else cout << sta.size() + 1 << ' '; while (sta.size()) { cout << sta.top() << ' '; sta.pop(); } } int main() { int n; cin >> n; for (int i = 1; i <= n; i ++) cin >> a[i]; st[0] = 1; for (int i = 1; i <= n; i ++) { vector<int> v; for (int j = 0; j < 200; j ++) { if (st[j]) { if (s[(a[i] + j) % 200] > 0) { cout << "Yes" << endl; if ((a[i] + j) % 200 == 0) { cout << 1 <<' '; cout << path[(a[i] + j) % 200].second; } else print((a[i] + j) % 200, false); cout << endl; print(j, true); cout << i; return 0; } s[(a[i] + j) % 200] ++; v.push_back((a[i] + j) % 200); path[(a[i] + j) % 200] = {j, i}; } } for (int i = 0; i < v.size(); i ++) st[v[i]] = 1; } cout << "No"; return 0; }
#include <bits/stdc++.h> using namespace std; namespace tensor { template<typename T, int N, int...args> struct static_tensor : std::array<static_tensor<T, args...>, N> { static_tensor() : std::array<static_tensor<T, args...>, N>() {} void fill(const T &val) { for (int i = 0; i < N; i++) { (*this)[i].fill(val); } } }; template<typename T, int N> struct static_tensor<T, N> : std::array<T, N> { static_tensor() : std::array<T, N>() {} }; template<typename T, int D> struct dynamic_tensor : std::vector<dynamic_tensor<T, D - 1>> { dynamic_tensor() : std::vector<dynamic_tensor<T, D - 1>>() {} template<typename...args> dynamic_tensor(size_t n, args...A) : std::vector<dynamic_tensor<T, D - 1>>(n, dynamic_tensor<T, D - 1>(A...)) {} dynamic_tensor(const std::initializer_list<dynamic_tensor<T, D - 1>> &list) : std::vector<dynamic_tensor<T, D - 1>>(list) {} void fill(const T &val) { for (auto it = this->begin(); it != this->end(); it++) { it->fill(val); } } }; template<typename T> struct dynamic_tensor<T, 1> : std::vector<T> { dynamic_tensor() : std::vector<T>() {} dynamic_tensor(size_t n, const T &val = T()) : std::vector<T>(n, val) {} dynamic_tensor(const std::initializer_list<T> &list) : std::vector<T>(list) {} void fill(const T &val = T()) { std::fill(this->begin(), this->end(), val); } }; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); using tensor::dynamic_tensor; int n; cin >> n; vector<int> a(n); for (auto &x : a) cin >> x, x %= 200; dynamic_tensor<bool, 4> dp(n + 1, 200, 4, 2); //dp[pos][diff][mask][f] dynamic_tensor<int, 4> move(n + 1, 200, 4, 2); dynamic_tensor<tuple<int, int, int>, 4> back_track(n + 1, 200, 4, 2); dp[0][0][0][0] = true; for (int i = 0; i < n; i++) { for (int j = 0; j < 200; j++) { for (int mask = 0; mask < 4; mask++) { for (int f = 0; f < 2; f++) { if (!dp[i][j][mask][f]) { continue; } //pick nothing dp[i + 1][j][mask][f] = true; move[i + 1][j][mask][f] = 0; back_track[i + 1][j][mask][f] = make_tuple(j, mask, f); //pick for b int new_j = (j + a[i]) % 200; dp[i + 1][new_j][mask | 1][1] = true; move[i + 1][new_j][mask | 1][1] = 1; back_track[i + 1][new_j][mask | 1][1] = make_tuple(j, mask, f); //pick for c new_j = (j + 200 - a[i]) % 200; dp[i + 1][new_j][mask | 2][1] = true; move[i + 1][new_j][mask | 2][1] = 2; back_track[i + 1][new_j][mask | 2][1] = make_tuple(j, mask, f); //pick for both dp[i + 1][j][mask | 3][f] = true; move[i + 1][j][mask | 3][f] = 3; back_track[i + 1][j][mask | 3][f] = make_tuple(j, mask, f); } } } } if (!dp[n][0][3][1]) { cout << "No\n"; return 0; } vector<int> b, c; for (int i = n, j = 0, mask = 3, f = 1; i > 0; i--) { if (move[i][j][mask][f] == 1) { b.push_back(i); } else if (move[i][j][mask][f] == 2) { c.push_back(i); } else if (move[i][j][mask][f] == 3) { b.push_back(i); c.push_back(i); } tie(j, mask, f) = back_track[i][j][mask][f]; } sort(b.begin(), b.end()); sort(c.begin(), c.end()); cout << "Yes\n"; cout << b.size() << ' '; for (auto &x : b) cout << x << ' '; cout << '\n'; cout << c.size() << ' '; for (auto &x : c) cout << x << ' '; cout << '\n'; return 0; }
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <tuple> #include <cstdio> #include <cmath> #define rep(i, n) for(i = 0; i < n; i++) #define int long long using namespace std; int INF = 1e+15; int n; int x[17], y[17], z[17]; int dp[1 << 17][17]; int dist(int i, int j) { return abs(x[j] - x[i]) + abs(y[j] - y[i]) + max(0LL, z[j] - z[i]); } signed main() { int i, j, k; cin >> n; rep(i, n) cin >> x[i] >> y[i] >> z[i]; rep(i, (1 << n)) rep(j, n) dp[i][j] = INF; dp[1][0] = 0; rep(i, (1 << n)) { rep(j, n) { rep(k, n) { dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dist(j, k)); } } } int ans = INF; rep(i, n) { ans = min(ans, dp[(1 << n) - 1][i] + dist(i, 0)); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define print(x) cout << "[" << #x << ": " << x << "]"; #define println(x) cout << "[" << #x << ": " << x << "]\n"; #define pb push_back #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define INF 1e18 + 5 #define MOD 1e9 + 7 void solve(){ int n; cin >> n; string s; cin >> s; vector<char> rem; for(int i = 0; i < n; i++){ rem.pb(s[i]); if(rem.size() > 2){ int x = rem.size()-1; if(rem[x] == 'x' && rem[x-1] == 'o' && rem[x-2] == 'f'){ // println(x) rem.pop_back(); rem.pop_back(); rem.pop_back(); } } } cout << rem.size() << '\n'; return; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); int tt = 1; while(tt--){ solve(); cout << '\n'; } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " s" << '\n'; }