code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define MV 2000001 #define LMV 21 #define ff first #define ss second #define pb push_back #define eb emplace_back #define emp emplace #define mp make_pair #define ins insert #define sz(x) (int)x.size() #define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); } void whi(istream_iterator<string> it) { cerr<<"\n"; } template<typename T, typename... Args> void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); } void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);} void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);} void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);} typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<PII, int> PPII; typedef pair<PLL, ll> PPLL; typedef map<int, int> MII; const int MOD = 1000000007; const ll INF = 4e18; 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>; struct h_llint { 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); } }; struct h_pair{ size_t operator()(const PLL&x)const{ return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32)); } }; typedef map<ll, ll> MLL; typedef map<PII, int> MPII; typedef map<PLL, ll> MPLL; typedef set<int> SI; typedef set<ll> SL; //ordered_set = order_of_key(.) //ordered_set = find_by_order(.) typedef ordered_set<int> OSI; typedef ordered_set<ll> OSL; typedef ordered_multiset<int> MOSI; typedef ordered_multiset<ll> MOSL; typedef unordered_map<ll, int, h_llint> UMLI; typedef unordered_map<ll, ll, h_llint> UMLL; typedef unordered_map<PLL, int, h_pair> UMPI; typedef unordered_map<PLL, ll, h_pair> UMPL; int ar[MV]; ll arr[MV], lo[MV], mu[MV]; VL px; void siever(void) { lo[1] = 1; for(int i=2;i<MV;i++) { if(!lo[i]) { lo[i] = i; px.pb(i); } for(int j=0;((j<sz(px)) && (px[j]<=lo[i]) && ((i*px[j])<MV));j++) lo[i*px[j]] = px[j]; } mu[1] = 1; for(ll i=2;i<MV;i++) { if(lo[i/lo[i]] == lo[i]) mu[i] = 0; else mu[i] = -1 * mu[i/lo[i]]; } return; } void solve(int T) { ll l,r; cin>>l>>r; ll tt = 0; for(ll i=2;i<=r;i++) { ll cf = (r/i) - ((l-1)/i); tt += (-mu[i]) * ((cf * (cf-1))>>1LL); } for(ll i=max(2LL, l);i<=r;i++) { ll cf = (r/i) - ((l-1)/i) - 1; tt -= cf; } tt <<= 1LL; cout<<tt<<"\n"; return; } int main(void) { FLASH(); //freopen("cowjog.in", "r", stdin); //freopen("cowjog.out", "w", stdout); int T; T = 1; #ifndef ONLINE_JUDGE time_t time_t1, time_t2; time_t1 = clock(); #endif siever(); //cin>>T; while(T--) solve(T); #ifndef ONLINE_JUDGE time_t2 = clock(); SETF(); cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n"; UNSETF(); #endif return 0; }
#include <bits/stdc++.h> // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18; void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);} void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);} void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);} void ioinit() { cout << fixed << setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); } // clang-format on vector<ll> mkprimes(ll n) { vector<ll> result; vector<bool> isprime(n+1, true); for (ll i = 2; i <= n; i++) { if (isprime[i]) { result.push_back(i); for (ll j = 2 * i; j <= n; j += i) { isprime[j]=false; } } } return result; } vector<ll> primefactors(ll n, vector<ll>&primes) { vector<ll> result; for (auto p: primes) { if(p > n) break; if(n % p == 0) { result.push_back(p); n = n / p; while(n % p == 0) { n = n / p; } } } if(n>1) result.push_back(n); return result; } int main() { ioinit(); ll l, r; cin >> l >> r; auto prmap = mkprimes(sqrt(r)+2); ll ans = 0; for (ll x = l; x < r; x++) { if (x == 1) continue; vector<ll> primes = primefactors(x, prmap); ll m = primes.size(); for (ll i = 1; i < (1LL << m); i++) { ll v = 1; for (ll j = 0; j < m; j++) { if ((i >> j) & 1) { v *= primes[j]; } } if (__builtin_popcount(i) % 2 == 0) { ans -= (r-x)/v; }else{ ans += (r-x)/v; } } ans -= (r-x)/x; } print(ans * 2); }
#include <bits/stdc++.h> #define F(i,j,k) for(int i=(j);i<=(k);++i) #define D(i,j,k) for(int i=(j);i>=(k);--i) #define rep(it,s) for(__typeof(s.begin()) it=s.begin();it!=s.end();++it) #define Rep(a,s) for(auto a:s) #define PB push_back #define MP make_pair #define FF first #define SS second typedef long long ll; typedef unsigned long long ull; using namespace std; ll quick_pow(ll x,ll y,ll Mod){ ll res=1; while(y){ if(y&1)res=res*x%Mod; x=x*x%Mod,y>>=1; } return res; } ll inv(ll x,ll Mod){return quick_pow(x,Mod-2,Mod);} namespace IO{ template <class T> inline void read(T &x){ x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar(); x*=f; } }; using namespace IO; const int N=500005; int n; ll a[N],ans; priority_queue<ll,vector<ll>,greater<ll>>pq; int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif read(n); F(i,1,n*2)read(a[i]); D(i,n,1){ ll x=a[i],y=a[2*n-i+1]; if(x<y)swap(x,y); pq.push(x); if(pq.top()<y)pq.pop(),pq.push(y); } ll ans=0; while(!pq.empty())ans+=pq.top(),pq.pop(); cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define ALL(x) ((x).begin()), ((x).end()) #define READ(x) (cin >> (x)) #define WRITE_N(x) (cout << (x) << endl) #define WRITE(x) (cout << (x)) #define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No")) #define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); } ll solve(ll L, ll R) { ll x = R - 2 * L + 1; if (x <= 0) { return 0; } else { return x * (x + 1) / 2; } } int main() { // get values from input cin.tie(0); ios::sync_with_stdio(false); int T; cin >> T; vector<ll> L(T); vector<ll> R(T); for (size_t i = 0; i < T; i++) { cin >> L[i] >> R[i]; } // main procedure // output for (size_t i = 0; i < T; i++) { ll ans = solve(L[i], R[i]); cout << ans << endl; } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; const int N=100010,M=500010; int n,m,s,t,x,y,u,v,a[M],b[M],c[N],d[M],e[M],cnt,f[N];bool vis[N]; inline void add(int u,int v,int D,int x){a[++cnt]=v,b[cnt]=c[u],c[u]=cnt,d[cnt]=D,e[cnt]=x;} struct node{ int dis,pos; bool operator <( const node &x)const{ return x.dis < dis; } }; priority_queue<node>q; inline int F(int x,int y){if(!(x%y))return x;return (x/y+1)*y;} inline void dijkstra(){ f[s]=0,q.push((node){0,s}); while(!q.empty()){ node tmp=q.top();q.pop(); int x=tmp.pos; if(vis[x])continue;vis[x]=1; for(int i=c[x];i;i=b[i]){ int y=a[i]; if(F(f[x],e[i])+d[i]<f[y]){ f[y]=F(f[x],e[i])+d[i]; if(!vis[y])q.push((node){f[y],y}); } } } } signed main(){ scanf("%lld%lld%lld%lld",&n,&m,&s,&t); for(int i=1;i<=n;++i)f[i]=1e18; for(int i=1;i<=m;++i)scanf("%lld%lld%lld%lld",&x,&y,&u,&v),add(x,y,u,v),add(y,x,u,v); dijkstra(); f[t]==1e18?puts("-1"):printf("%lld\n",f[t]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<long long, long long>; constexpr char ln = '\n'; constexpr long long MOD = 1000000007; constexpr long long INF = 1000000000 + 100; constexpr long long LINF = 1000000000000000000 + 100; #define all(v) v.begin(), v.end() #define rep(i, n) for(int i=0;i<(n);i++) #define rept(i, j, n) for(int i=(j); i<(n); i++) #define rrep(i, n) for(int i=(n); i>=0; i--) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } struct edge{ll to, time, cost;}; struct graph{ ll V; vector<vector<edge> > G; vector<ll> d; vector<ll> prev; graph(ll n){ init(n); } void init(ll n){ V = n; G.resize(V); d.resize(V); rep(i,V){ d[i] = LINF; } } void add_edge(ll s, ll t, ll cost, ll time){ edge e; e.to = t, e.time = time, e.cost = cost; G[s].push_back(e); } void dijkstra(ll s){ rep(i,V){ d[i] = LINF; } d[s] = 0; priority_queue<pll,vector<pll>, greater<pll> > que; que.push(pll(0,s)); //[その頂点への最短コスト候補、頂点番号] prev.assign(V, -1); while(!que.empty()){ pll p = que.top(); que.pop(); ll v = p.second; if(d[v]<p.first) continue; for(auto e : G[v]){ ll ncost = d[v]; if(ncost % e.time){ ncost += e.time - (ncost % e.time); } ncost += e.cost; if(d[e.to]>ncost){ d[e.to] = ncost; prev[e.to] = v; que.push(pll(d[e.to],e.to)); } } } } vector<int> get_path(int t){ //頂点tへの最短路 vector<int> path; for(; t != -1;t=prev[t]){ path.push_back(t); } reverse(path.begin(), path.end()); return path; } }; int main(){ int N, M, X, Y; cin >> N >> M >> X >> Y; X--, Y--; graph g(N); rep(i, M){ int a, b; ll t, k; cin >> a >> b >> t >> k; a--, b--; g.add_edge(a,b,t,k); g.add_edge(b,a,t,k); } g.dijkstra(X); cout << (g.d[Y] == LINF ? -1 : g.d[Y]) << ln; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repp(i, l, n) for (int i = (l); i < (int)(n); i++) using ll = long long; int main(){ int n; cin>>n; vector<int> a(n); rep(i,n) cin >> a[i]; rep(i,n){ a[i] %= 200; } vector<int> cnt(200); rep(i,n) cnt[a[i]]++; ll ans=0; rep(i,200){ ans += (ll)(cnt[i]*(ll)(cnt[i]-1))/2; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define len(x) int((x).size()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define randint(n) uniform_int_distribution<int>(1, (n))(rng) vector<int> adj[101]; bool vis[101][101]; int cnt; void dfs(int v, int stem){ vis[stem][v]=1; cnt++; for(auto u:adj[v]){ if(!vis[stem][u]) dfs(u,stem); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ARTHUR_LOCAL ifstream cin("input.txt"); #endif int n; cin>>n; for(int i=1; i<=n; i++){ string s; cin>>s; for(int j=0; j<n; j++){ if(s[j]=='1') adj[j+1].push_back(i); } } cout << setprecision(11); double ans = 0; for(int v=1; v<=n; v++){ cnt = 0; dfs(v,v); ans += (double)1 / (double)cnt; } cout << ans << "\n"; }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cstdlib> #include<queue> #include<set> #include<cstdio> #include<map> #include<cassert> using namespace std; #define ll long long #define reps(i, a, b) for(int i = a; i < b; i++) #define rreps(i, a, b) for(int i = a-1; i >= b; i--) #define rep(i, n) reps(i, 0, n) #define rrep(i, n) rreps(i, n, 0) #define P pair<int, int> #define vec vector<int> #define mat vector<vec> const ll mod = 1000000007; const int INF = 1001001001; int main(){ int n; cin >> n; vector<double> x(n), y(n); rep(i, n) cin >> x[i] >> y[i]; vector<vector<double> > dist(n+2, vector<double>(n+2)); rep(i, n){ rep(j, n){ double dx = x[i] - x[j]; double dy = y[i] - y[j]; dist[i][j] = dx * dx + dy * dy; } } rep(i, n) { int dy = 100 - y[i]; dist[n][i] = dy * dy; dist[i][n] = dy * dy; } rep(i, n) { int dy = -100 - y[i]; dist[n+1][i] = dy * dy; dist[i][n+1] = dy * dy; } dist[n][n+1] = 40000; dist[n+1][n] = 40000; // rep(i, n+2){ // rep(j, n+2){ // cout << dist[i][j] << " "; // }cout << endl; // } double l = 0, r = 200; rep(s, 100){ double mid = (l+r) / 2; mat g(n+2); rep(i, n+2){ rep(j, n+2){ if((double)dist[i][j] <= mid*mid){ g[i].push_back(j); } } } // rep(i, n+2){ // rep(j, g[i].size()){ // cout << g[i][j] << " "; // }cout << endl; // } vec pos(n+2); queue<int> que; que.push(n); pos[n] = 1; while(que.size() > 0){ int u = que.front(); que.pop(); rep(i, g[u].size()){ int v = g[u][i]; if(pos[v] == 1) continue; pos[v] = 1; que.push(v); } } if(pos[n+1]) r = mid; else l = mid; // cout << l << " " << r << endl; } double ans = r / 2; printf("%.16lf\n", ans); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define re register #define ls (o<<1) #define rs (o<<1|1) //#define m (l+r)/2 #define pb push_back typedef pair<int,int> pii; const double PI= acos(-1.0); const int M = 1e5+7; /* int head[M],cnt=1; void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;} struct EDGE{int to,nxt,w;}ee[M*2]; void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;} */ double eps = 1e-7; int n; double x[110],y[110]; double dis(double x1,double y1,double x2,double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int fa[210]; int gt(int x){ if(x==fa[x])return x; return fa[x]=gt(fa[x]); } bool ck(double r){ for(int i=1;i<=n+2;i++)fa[i]=i; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(dis(x[i],y[i],x[j],y[j])+eps<=2*r) fa[gt(i)]=gt(j); } if(100-y[i] + eps<=2*r)fa[gt(i)]=gt(n+1); if(y[i]+100 + eps<=2*r)fa[gt(i)]=gt(n+2); } if(gt(n+1)==gt(n+2))return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>x[i]>>y[i]; } double l=0,r=100,ans=0; while(l+eps<=r){ double mid=(l+r)/2; if(ck(mid))l=mid,ans=mid; else r=mid; } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define f first #define s second #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() const int inf = 1e9; const ll infll = 1e18; const int M = 1e9 + 7; const int N = 3e6; int fact[N+1], inv[N+1]; int mult(ll a, ll b){ return (a * b) %M; } void ad(int &a, int b){ a += b; if(a >= M) a -= M; if(a < 0) a += M; } int fp(int a, int b){ int ret = 1; while(b){ if(b&1) ret = mult(ret , a); b >>= 1; a = mult(a, a); } return ret; } int nck(int a, int b){ if(b > a) return 0; return mult(fact[a], mult(inv[b], inv[a-b])); } void precompute(){ fact[0] = inv[0] = 1; for (int i = 1; i <= N; ++i){ fact[i] = mult(fact[i-1], i); inv[i] = fp(fact[i], M - 2); } } int W , B , k; int main(){ precompute(); scanf("%d%d%d", &W, &B ,&k); if(W >= B + k + 1) return puts("0"), 0; //w = b + k + 1 = bad int ans = nck(W+B, W); ad(ans , -nck(W+B, B+k+1)); printf("%d\n",ans); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MOD=1000000007; #define INF 1LL<<60 #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int V; ll G[20][20]; // グラフ ll dp[200000][20]; int main() { cin >> V; // グラフの初期化 rep(i, 20) { rep(j, 20) { G[i][j] = INF; } } vector<ll> x(V),y(V),z(V); rep(i, V) { cin>>x[i]>>y[i]>>z[i]; } rep(i,V){ rep(j,V){ G[i][j]=abs(x[j]-x[i])+abs(y[j]-y[i])+max(0LL,z[j]-z[i]); } } // dp の初期化 rep(i, 200000) { rep(j, 20) { dp[i][j] = INF; } } dp[0][0] = 0; // スタートを頂点 0 とする rep(S, 1 << V) { rep(v, V) { rep(u, V) { if ((S & (1 << v)) == 0) { if (v != u) chmin(dp[S | (1 << v)][v], dp[S][u] + G[u][v]); } } } } if (dp[(1 << V) - 1][0] != INF) { cout << dp[(1 << V) - 1][0] << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second typedef long long ll; typedef pair<int,int> ii; const int N = 1e2; int n; bool vis[N+2]; vector<int> adj[N+2]; int dfs(int cur) { vis[cur] = 1; int ret = 1; for (int nx : adj[cur]) { if (!vis[nx]) ret += dfs(nx); } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { char x; cin >> x; if (x == '1') adj[j].push_back(i); } } double ans = 0; for (int i = 1; i <= n; i++) { memset(vis,0,sizeof(vis)); ans += 1.0 * 1/dfs(i); } cout << fixed << setprecision(20); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template<class T> using vc = vector<T>; template<class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i,n) for (auto& i: n) template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;} template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;} struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_; #ifdef DEBUG template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } 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; //--------------------------------------------------------------------------------// struct UnionFind { private: vector<int> par; vector<int> count; vector<int> rank; public: UnionFind(int N) { count.assign(N, 1); rank.assign(N, 0); par.assign(N, 0); rep(i, N) par[i] = i; } int root(int x) { if (par[x] == x) return x; else { par[x] = root(par[x]); return par[x]; } } void unite(int x, int y) { x = root(x), y = root(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); else if (rank[x] == rank[y]) rank[y]++; par[y] = x; count[x] += count[y]; return; } int size(int x) { return count[root(x)]; } bool issame(int x, int y) { return root(x) == root(y); } }; using UF = struct UnionFind; int main() { ll N; cin >> N; vc<string> S(N); rep(i, N) cin >> S[i]; vvc<int> child(N), parent(N); rep(i, N){ vl srch(N); srch[i] = true, child[i].eb(i); queue<ll> q; q.emplace(i); while(!q.empty()){ ll now = q.front(); q.pop(); rep(j, N){ if (srch[j] or S[now][j] == '0') continue; srch[j] = true; child[i].eb(j); q.emplace(j); } } srch = vl(N), q.emplace(i), parent[i].eb(i), srch[i] = true; while(!q.empty()){ ll now = q.front(); q.pop(); rep(j, N){ if (srch[j] or S[j][now] == '0') continue; srch[j] = true; parent[i].eb(j); q.emplace(j); } } } ld ans = 0; rep(i, N) ans += 1.0L / parent[i].size(); cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define e endl #define iff(u,v) if(abs(u-v)<1e-9) #define f(i,p,q) for(int i=p;i<q;i++) #define vi vector<long long>v; #define tc int t; cin>>t; while(t--) //#define an long long n; cin>>n; long long a[n]; for(int i=0;i<n;i++) cin>>a[i]; int main() { //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,i,j,k=1,x,y,r,c,ans=0,size,sum=0; string s; bool flag; // tc{ // } cin>>n>>i>>j; cout<<n-i+j<<e; }
#include <bits/stdc++.h> using namespace std; int main() { int N, A, B, sum; cin >> N >> A >> B; sum = N-A+B; cout << sum << endl; }
#include <bits/stdc++.h> #define watch(x) std::cout << (#x) << " is " << (x) << std::endl using LL = long long; using pii = std::pair<int, int>; using pll = std::pair<LL, LL>; const int N = 100; double dp[N + 1][N + 1][N + 1]; double f(int i, int j, int k) { if (k > j) std::swap(j ,k); if (j > i) std::swap(i, j); return dp[i][j][k]; } void solve() { for (int i = N - 1; i >= 0; --i) { for (int j = i; j >= 0; --j) { for (int k = j; k >= 0; --k) if (i + j + k) { dp[i][j][k] = 1 + (f(i + 1, j, k) * i + f(i, j + 1, k) * j + f(i, j, k + 1) * k) / (i + j + k); } } } } int main() { //freopen("in","r",stdin); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); solve(); std::vector<int> a(3); for (auto &x : a) std::cin >> x; std::sort(a.begin(), a.end()); std::cout.precision(12); std::cout << std::fixed << f(a[2], a[1], a[0]) << std::endl;; return 0; }
#include <bits/stdc++.h> #include <unistd.h> #define FAST_IO ios::sync_with_stdio(0);cin.tie(NULL) #define all(v) (v).begin(),(v).end() using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif typedef long long Long; typedef long double Double; typedef unsigned long long ULong; typedef pair<Double, Double> Pair; const int N = 1e6; const Double EPS = 1e-9; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); Long random(Long a, Long b) { return uniform_int_distribution<Long> (a, b) (rng); } Pair operator + (const Pair& a, const Pair& b) { return {a.first + b.first, a.second + b.second}; } Pair operator - (const Pair& a, const Pair& b) { return {a.first - b.first, a.second - b.second}; } Pair operator / (const Pair& a, Double k) { return {a.first / k, a.second / k}; } void Solve(void) { Long n, m, k; cin >> n >> m >> k; if (k == 0) { vector<Double> dp(n + m + 2, 0.0); vector<Double> suffix(n + m + 2, 0.0); for (int i = n - 1; i >= 0; i--) { Double sum = suffix[i + 1] - suffix[i + m + 1]; dp[i] = sum / m + 1; suffix[i] = dp[i] + suffix[i + 1]; } cout << dp[0] << '\n'; } else { vector<bool> is_hole(n + m + 2, false); for (int i = 0; i < k; i++) { int a; cin >> a; is_hole[a] = true; } vector<int> prefix(n + m + 2, 0); for (int i = 1; i <= n; i++) { prefix[i] = prefix[i - 1]; if (is_hole[i]) prefix[i]++; } for (int i = 1; i < n - m + 1; i++) { if (prefix[i + m - 1] - prefix[i - 1] == m) { cout << -1 << '\n'; return; } } vector<Pair> dp(n + m + 2, {0.0, 0.0}); vector<Pair> suffix(n + m + 2, {0.0, 0.0}); Pair one = {0.0, 1.0}; for (int i = n - 1; i >= 0; i--) { if (is_hole[i]) { dp[i] = {1.0, 0}; } else { Pair sum = suffix[i + 1] - suffix[i + m + 1]; dp[i] = sum / m + one; } suffix[i] = dp[i] + suffix[i + 1]; } Double ans = dp[0].second / (1.0 - dp[0].first); cout << fixed << setprecision(10) << ans << '\n'; } } int main(void) { FAST_IO; int t = 1; //cin >> t; while (t--) Solve(); return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i <= int(b); i++) #define per(i, a, b) for (int i = (a); i >= int(b); i--) using namespace std; typedef long long ll; const int maxn = 1e5; int n, L, a[maxn + 5], b[maxn + 5]; void read(int a[], int n) { rep(i, 1, n - 1) { scanf("%d", &a[i]); } a[n] = L + 1; per(i, n, 1) { a[i] = a[i] - a[i - 1] - 1; } } int main() { scanf("%d %d", &n, &L), ++n; read(a, n), read(b, n); ll res = 0; int p = 1; rep(i, 1, n) if (b[i] > 0) { for (; p < i && a[p] == 0; p++); int l = p, c = 0; for (; p <= n && c < b[i]; p++) { c += a[p]; } if (c > b[i]) { puts("-1"), exit(0); } int r = p - 1; res += max(r - i, 0) + max(i - l, 0); } rep(i, p, n) if (a[i] > 0) { puts("-1"), exit(0); } printf("%lld\n", res); return 0; }
#include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i=a; i<=b; i++) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) #define sz(x) (int) x.size() #define f first #define s second #define nl "\n" typedef long long ll; typedef pair<int, int> pii; const int MOD = 1e9+7; template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>; int n; int s[200001]; string t; vector<int> vec; int main(){ ios::sync_with_stdio(false); cin.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; rep(i, 0, n-1){ char c; cin >> c; s[i]=(c-'0')%7; } cin >> t; int mask=1; int pten=1; for(int i=n-1; i>=0; i--){ int oldmask=mask; rep(k, 0, 6){ if(oldmask&(1<<k)){ mask|=(1<<((k+pten*s[i])%7)); } } if(i==0 || t[i-1]!=t[i]){ vec.push_back(mask); mask=1; } pten*=10; pten%=7; } int need=1; rep(i, 0, sz(vec)-1) { int mask=vec[i]; int oldneed=need; need=0; if((t[n-1]=='T' && i%2==0) || (t[n-1]=='A' && i%2==1)){ rep(j, 0, 6){ if(!(oldneed&(1<<j))) continue; rep(k, 0, 6){ if(mask&(1<<k)){ need|=(1<<((j-k+7)%7)); } } } } else{ rep(j, 0, 6){ int get=0; rep(k, 0, 6){ if(mask&(1<<k)) get|=(1<<((j+k)%7)); } if(oldneed==(get|(oldneed^get))) need|=(1<<j); } } } cout << (need&1?"Takahashi":"Aoki"); }
#include<bits/stdc++.h> using namespace std; #define debug(x) cerr<<#x<<" = "<<x #define sp <<" " #define el <<endl #define fgx cerr<<" ------------------------------------------------ "<<endl #define LL long long #define DB double #define mpt make_pair #define pb push_back inline LL read(){ LL nm=0; bool fh=true; char cw=getchar(); for(;!isdigit(cw);cw=getchar()) fh^=(cw=='-'); for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0'); return fh?nm:-nm; } #define M 200200 #define INF 2000000000 int n,m,mx[M],a[M],fs[M],to[M],nt[M],tot,ind[M],oud[M],H[M],ans=-INF; bool vis[M]; inline void link(int x,int y){to[++tot]=y,nt[tot]=fs[x],fs[x]=tot;} inline void dfs(int x){ if(vis[x]) return; vis[x]=true,mx[x]=a[x]; for(int i=fs[x];i;i=nt[i]) dfs(to[i]),mx[x]=max(mx[x],mx[to[i]]),H[x]=max(H[x],mx[to[i]]); } int main(){ n=read(),m=read(); for(int i=1;i<=n;i++) a[i]=read(); for(int i=1,x,y;i<=m;i++) x=read(),y=read(),link(x,y),++oud[x],++ind[y]; for(int i=1;i<=n;i++) if(!ind[i]) dfs(i); for(int i=1;i<=n;i++) if(oud[i]) ans=max(ans,H[i]-a[i]); printf("%d\n",ans);return 0; }
#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 forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++) #define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--) #define rep(i,n)for(int i=0,_n=n;i<n;i++) #define ll long long #define pii pair<int,int> #define vi vector<int> #define vpii vector<pii> #define m_p make_pair #define re return #define pb push_back #define si set<int> #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 #define x1 XZVJDFADSPFOE #define y1 GASDIJSLDAEJF #define x2 DFDAJKVOHKWIW #define y2 PSFSAODSXVNMQ #define LLINF 0x3f3f3f3f3f3f3f3fLL #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++) using namespace std; inline void read(int &x) { short negative=1; x=0; char c=getchar(); while(c<'0' || c>'9') { if(c=='-') negative=-1; c=getchar(); } while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar(); x*=negative; } ll quickpower(ll n,ll k){ ll ans=1; while(k){ if(k%2){ ans*=n; ans%=mod; } n*=n; n%=mod; k/=2; } return ans; } string int_to_string(int n) { string s=""; while(n) { int now=n%10; s+=now+'0'; n/=10; } reverse(s.begin(),s.end()); return s; } int string_to_int(string s) { int n=0; rep(i,s.size()) { n*=10; n+=s[i]-'0'; } return n; } int n,m; vector<int>v[200100]; ll a[200100]; bool used[200100]; ll can[200100]; void dfs(int u) { used[u]=1; rep(i,v[u].size()) { if(!used[v[u][i]])dfs(v[u][i]); can[u]=max(can[u],can[v[u][i]]); can[u]=max(can[u],a[v[u][i]]); } } int main() { ios::sync_with_stdio(0); cin>>n>>m; rep(i,n)cin>>a[i]; rep(i,m) { int x,y; cin>>x>>y; x--; y--; v[x].pb(y); } rep(i,n)can[i]=-1e18; rep(i,n) { if(!used[i])dfs(i); } ll ans=-1e18; rep(i,n) { ans=max(ans,can[i]-a[i]); } cout<<ans<<endl; return 0; }
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #include <bits/stdc++.h> #include<set> #include <array> using namespace std; #define ll long long #define endl '\n' #define mod 998244353 #define pb push_back #define ff first #define ss second #define con continue #define ub upper_bound #define lb lower_bound #define si(x) int(x.size()) #define sum_all(a) ( accumulate ((a).begin(), (a).end(), 0ll)) #define mine(a) (*min_element((a).begin(), (a).end())) #define maxe(a) (*max_element((a).begin(), (a).end())) #define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin()) #define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin()) #define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) const double pi = 2 * acos(0.0); const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, -1, 0, 1 }; const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 }; const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 }; ll min(ll a,ll b) { if(a<b)return a; return b; } ll max(ll a,ll b) { if(a>b)return a; return b; } ll ceil1(ll a,ll b) { return(a+b-1)/b; } void read(vector<ll> & arr) { for(ll i=0;i<si(arr);i++) cin >> arr[i]; } void read_graph(vector<vector<ll>>& g, ll m) { while(m--) { ll x,y; cin >> x>> y ; x--,y--; g[x].pb(y); g[y].pb(x); } } ll modmul(ll x, ll y, ll m=mod) { if (y==0) return 0; ll a = modmul(x, y / 2, m) % m; a = (a + a) % m; if (y % 2 != 0) { a = (a + x) % m; } return a; } ll power(ll a, ll b, ll m) { if (b == 1) return a; ll x = power(a, b / 2, m); x *= x; x %= m; if (b % 2 == 1) { x *= a; x %= m; } return x; } void solve() { int n; cin >> n ; vector<ll> arr(n); for(int i=0;i<n;i++) cin >> arr[i]; sort(arr.begin(),arr.end()); // for(ll i=0;i<n;i++) cout << arr[i] << " " ; // cout << endl ; vector<ll> suff(n); vector<ll> pw(n+1); pw[0]=1; for(ll i=1;i<=n;i++) { pw[i]=pw[i-1]*2; pw[i]%=mod; } for(ll i=0;i<n;i++) { suff[i]= modmul(arr[i],pw[i],mod); suff[i]%=mod; } for(ll i=n-2;i>=0;i--) { suff[i] +=suff[i+1]; suff[i]%=mod; } ll inv_2= power(2,mod-2,mod); ll base=inv_2; // ll k =853983 *853983 ; ll ans=0 ; for(ll i=0;i<n;i++) { ans+=(modmul(arr[i],arr[i]))%mod; ans%=mod; } for(ll i=0;i<n-1;i++) { ll x ; x= modmul(suff[i+1],inv_2); x=modmul(arr[i],x); ans+=x; ans%=mod; inv_2= modmul(inv_2,base); } cout << ans <<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // int t; cin >> t; while (t--) solve(); }
#include<bits/stdc++.h> #define re register #define int long long using namespace std; inline int read(){ re int t=0;re char v=getchar(); while(v<'0')v=getchar(); while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar(); return t; } const int M=998244353; inline int ksm(re int x,re int y){ re int s=1; while(y){ if(y&1)s=1ll*s*x%M; x=1ll*x*x%M,y>>=1; } return s; } int n,m,a[1000002],ans,b[1000002],sum; char s[1000002]; signed main(){ n=read(); for(re int i=1;i<=n;++i)a[i]=read(); sort(a+1,a+n+1); for(re int i=1;i<=n;++i){ ans=(ans+a[i]*sum)%M; ans=(ans+a[i]*a[i])%M; sum=(sum+sum)%M; sum=(sum+a[i])%M; } printf("%lld",ans); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() void chmax(ll &x, ll y) {x = max(x,y);} void chmin(ll &x, ll y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} const ll inf = 1e18; const ll mod = 1e9 + 7; int main() { ll N, M; cin >> N >> M; vector<ll> A(N), B(M); rep(i,N) cin >> A[i]; rep(i,M) cin >> B[i]; vector<vector<ll>> dp(N+1,vector<ll>(M+1,inf)); rep(i,N+1) dp[i][0] = i; rep(i,M+1) dp[0][i] = i; repp(i,1,N+1) repp(j,1,M+1) { ll a = A[i-1], b = B[j-1]; chmin(dp[i][j],dp[i-1][j]+1); chmin(dp[i][j],dp[i][j-1]+1); if (a==b) { chmin(dp[i][j],dp[i-1][j-1]); } else { chmin(dp[i][j],dp[i-1][j-1]+1); } } ll ans = dp[N][M]; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll solve(int L, int R){ vector<ll> memo(1<<21, -1); auto f = [&](auto self, int L, int R) -> ll { // L < x <= R であって、gcd(x,y) == 1 となるものを数える auto key = L + R; if(memo[key] != -1) return memo[key]; ll res = ll(R - L) * (R - L); int d = max(L, R); while(d > 1){ int nxt_d = max(L / (L / d + 1), R / (R / d + 1)); res -= (d - nxt_d) * self(self, L / d, R / d); d = nxt_d; } memo[key] = res; return res; }; auto g = [&](int L, int R) -> ll { ll res = 0; int d = max(L, R); while(d){ int nxt_d = max(L / (L / d + 1), R / (R / d + 1)); ll cnt = max(0, min(d, R) - max(nxt_d, L)); res += cnt * (R / d - L / d); d = nxt_d; } return res; }; --L; ll ans = ll(R - L) * (R - L); ans -= f(f, L, R) + 2 * g(L, R); ans += 2 * (L < 1 ? R - L : 0) + (R - L); ans -= L < 1; return ans; } int main(){ int L, R; cin >> L >> R; ll ans = solve(L, R); cout << ans << endl; return 0; }
#include <iostream> using namespace std; int main() { int v, t, s, d; cin >> v >> t >> s >> d; cout << (d < v*t || v*s < d ? "Yes" : "No") << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define Test int T;cin>>T;while(T--) #define PI acos(-1) #define endl "\n" #define fx(x) fixed<<setprecision(x) #define sz(s) (int)s.size() #define all(v) (v).begin(),(v).end() #define allr(v) (v).rbegin(),(v).rend() #define mem(a,n) memset((a),n,sizeof (a)) #define INF 1e9 #define ii pair<int,int> ll gcd(ll x,ll y){return(!y?x:gcd(y,x%y));} ll lcm(ll x,ll y){return x/gcd(x,y)*y;} void file(){ #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #else //freopen("east.in", "r", stdin); //freopen("output.txt", "w", stdout); #endif } void fast(){ ios_base::sync_with_stdio(false); cin.tie(NULL); file(); } int dx[]= {-1,0,1,0}; int dy[]= {0,1,0,-1}; const double eps=1e-9; const int mod=1e9+7; const int N=1e5+7; int main(){ fast(); int v,t,s,d; cin>>v>>t>>s>>d; if(d>=t*v&&d<=s*v){ cout<<"No"; } else cout<<"Yes"; }
#pragma gcc target("avx2") #pragma gcc optimize("o3") #pragma gcc optimize("unroll-loops") //#pragma warning(disable : 4996) //#define ATCODER #ifdef ATCODER #include <atcoder/all> using namespace atcoder; #endif #include <limits.h> #include <math.h> #include <time.h> #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #if defined(LOCAL) || defined(_MSC_VER) void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define DEBUG(...) 42 #endif #ifdef _MSC_VER #include <intrin.h> #include "stdafx.h" #define __builtin_popcount __popcnt #define __builtin_popcountll __popcnt64 #endif #define ll long long #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 VSORT(v) sort(v.begin(), v.end()); #define VREVERSE(v) reverse(v.begin(), v.end()) #define print(x) cout << (x) << '\n' #define spa << " " << #define lb(v, n) lower_bound(v.begin(), v.end(), (n)) #define ub(v, n) upper_bound(v.begin(), v.end(), (n)) //#define int long long //#define double long double #define all(x) (x).begin(), (x).end() #define print_space(v) \ REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1] template <typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; } typedef pair<int, int> pii; typedef pair<long long, long long> pll; std::random_device rd; std::mt19937 mt(rd()); constexpr ll MOD = 1e9 + 7; constexpr int MAX = 400040; const double pi = acos(-1); constexpr long double EPS = 1e-9; const int dx[4] = { 0, 0, -1, 1 }, dy[4] = { -1, 1, 0, 0 }; using ld = long double; const int INF = 1e9; struct Dijkstra { using pii = pair<int, int>; struct edge { int to, cost; edge(int to_,int cost_):to(to_),cost(cost_){} }; int N; vector<vector<edge>> G; vector<int> d; Dijkstra(int N_) : N(N_), G(N_), d(N_, INF) {} void add_edge(int u, int v, int c) { G[u].emplace_back( v, c ); } void build(int s) { d[s] = 0; priority_queue<pii, vector<pii>, greater<pii>> que; que.push(pii(0, s)); while (!que.empty()) { pii p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(pii(d[e.to], e.to)); } } } } }; int H, W; int toid(int x, int y) { return x * W + y; } int A[505][505], B[505][505]; void solve() { cin >> H >> W; REP(i, H) REP(j, W - 1) cin >> A[i][j]; REP(i, H - 1) REP(j, W) cin >> B[i][j]; int V = H * W; Dijkstra G(H * W); REP(i, H) REP(j, W) { if (j + 1 < W) { G.add_edge(toid(i, j), toid(i, j + 1), A[i][j]); } if (j - 1 >= 0) G.add_edge(toid(i, j), toid(i, j - 1), A[i][j - 1]); if (i + 1 < H) G.add_edge(toid(i, j), toid(i + 1, j), B[i][j]); REP(k, i) { G.add_edge(toid(i, j), toid(k, j), i - k + 1); } } G.build(toid(0, 0)); print(G.d[H * W - 1]); } signed main() { cin.tie(0); ios::sync_with_stdio(false); // int q; cin >> q; // while (q--) solve(); }
#include <bits/stdc++.h> using namespace std; const int N = 500, D = 2 * 1000 * N; vector<array<short, 2>> dijkstra[D]; int a[N][N], b[N][N], dist[N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m - 1; ++j) { cin >> a[i][j]; } } for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < m; ++j) { cin >> b[i][j]; } } for (int i = 0; i < n; ++i) { fill(dist[i], dist[i] + m, INT_MAX); } dijkstra[0].push_back({0, 0}); dist[0][0] = 0; for (int d = 0; d < D; ++d) { while (!dijkstra[d].empty()) { auto [r, c] = dijkstra[d].back(); dijkstra[d].pop_back(); if (d > dist[r][c]) { continue; } if (c < m - 1 && d + a[r][c] < dist[r][c + 1]) { dist[r][c + 1] = d + a[r][c]; dijkstra[dist[r][c + 1]].push_back({r, c + 1}); } if (c > 0 && d + a[r][c - 1] < dist[r][c - 1]) { dist[r][c - 1] = d + a[r][c - 1]; dijkstra[dist[r][c - 1]].push_back({r, c - 1}); } if (r < n - 1 && d + b[r][c] < dist[r + 1][c]) { dist[r + 1][c] = d + b[r][c]; dijkstra[dist[r + 1][c]].push_back({r + 1, c}); } for (int i = 1; i <= r; ++i) { if (d + 1 + i < dist[r - i][c]) { dist[r - i][c] = d + 1 + i; dijkstra[dist[r - i][c]].push_back({r - i, c}); } } } } cout << dist[n - 1][m - 1] << "\n"; }
#include<bits/stdc++.h> using namespace std; int a[200010],b[200010]; queue<int>pos[200010]; map<int,int>id; int tot,n; int T[200010<<2]; int q(int rt,int l,int r,int pos){ if(l==r)return T[rt]; int mid=l+r>>1; if(pos<=mid)return T[rt]+q(rt<<1,l,mid,pos); else return T[rt]+q(rt<<1|1,mid+1,r,pos); } void ad(int rt,int l,int r,int L,int R){ if(l==L&&r==R){ T[rt]++;return; } int mid=l+r>>1; if(R<=mid)ad(rt<<1,l,mid,L,R); else if(L>mid)ad(rt<<1|1,mid+1,r,L,R); else ad(rt<<1,l,mid,L,mid),ad(rt<<1|1,mid+1,r,mid+1,R); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); a[i]+=i; int t=id[a[i]]; if(!t)id[a[i]]=t=++tot; pos[t].push(i); } int flag=1; for(int i=1;i<=n;i++){ scanf("%d",&b[i]); b[i]+=i; int t=id[b[i]]; if(t&&!pos[t].empty()){ b[i]=pos[t].front(); pos[t].pop(); } else flag=0; } if(!flag)printf("-1"); else{ long long ans=0; for(int i=1;i<=n;i++)ans+=q(1,1,n,b[i]),ad(1,1,n,1,b[i]); printf("%lld",ans); } }
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include <bits/stdc++.h> using namespace std; template<typename T> struct BIT{ private: vector<T> A; const int n; public: BIT(int _n) : A(_n+1,0), n(_n){} //i番目までの累積和 T sum(int i){ T s=0; while(i>0){ s+=A[i]; i-=i&-i; } return s; } //[i,j]の要素の総和 T sum(int i,int j){ return sum(j)-sum(i-1); } //i番目にxを代入 void add(int i,T x){ while(i<=n){ A[i]+=x; i+=i&-i; } } }; int main(){ int n; cin>>n; vector<ll> A(n),B(n); rep(i,n) cin>>A[i]; rep(i,n) cin>>B[i]; vector<pair<ll,int>> C(n),D(n); rep(i,n){ C[i]={A[i]+i,i}; D[i]={B[i]+i,i}; } sort(ALL(C)); sort(ALL(D)); vector<int> E(n); rep(i,n){ if(C[i].first!=D[i].first){ cout<<-1<<endl; return 0; } E[C[i].second]=D[i].second+1; } BIT<int> bit(n); ll ans=0; for(int j=0;j<n;j++){ ans+=j-bit.sum(E[j]); bit.add(E[j],1); } cout<<ans<<endl; return 0; }
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <utility> using namespace std; int c[500009], p[500009];; vector<int> v; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; string s, t; cin >> s >> t; for (int i = 0; i < n - 1; i++) { if (s[i] != t[i]) { p[i + 1] = 1; if (t[i + 1] == '0') t[i + 1] = '1'; else t[i + 1] = '0'; } if (s[i + 1] == '1') v.push_back(i + 1); } if (s[n - 1] != t[n - 1]) { cout << -1 << '\n'; return 0; } long long ans = 0; int pi = 0, r = 0; for (int i = 1; i < n; i++) { while (pi < v.size() && v[pi] < i) pi++; r = (r + c[i]) % 2; if (r == p[i]) continue; if (pi >= v.size()) { cout << -1 << '\n'; return 0; } c[v[pi] + 1]++; r = (r + 1) % 2; ans += v[pi] - i + 1; pi++; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N,M,T; cin >> N >> M >> T; vector<int> a(M); vector<int> b(M); for(int i = 0; i < M*2; i++) { if(i%2 == 0) { cin >> a.at(i/2); }else{ cin >> b.at((i-1)/2); } } bool Ans = true; int B = N; int Time = 0; for(int i = 0; i < M; i++) { B -= (a.at(i)-Time); Time = a.at(i); if(B <= 0) Ans = false; B += (b.at(i)-Time); Time = b.at(i); if(N < B) B = N; } B -= (T-b.at(M-1)); if(B <= 0) Ans = false; if(Ans) { cout << "Yes" << endl; }else{ cout << "No" << endl; } }
#define LOCAL #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++) #define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--) #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define All(x) x.begin(), x.end() #define Range(x, i, j) x.begin() + i, x.begin() + j // #define M_PI 3.14159265358979323846 // CF #define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI)) #define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360) #define Find(set, element) set.find(element) != set.end() #define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank #define endl "\n" #define Case(x) printf("Case #%d: ", x); // gcj typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; typedef vector<PI> vpi; typedef vector<vector<PI>> vvpi; typedef vector<PLL> vpl; typedef vector<vector<PLL>> vvpl; typedef vector<char> vch; typedef vector<vector<char>> vvch; constexpr ll LINF = 8001002003004005006ll; constexpr int INF = 1002003004; constexpr int n_max = 2e5+10; template<class T> inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; }; template<class T> inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; }; template<class T, class U> T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;}; // debug template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) {return '"' + s + '"';}; string to_string(const char c) {return to_string((string) &c);}; string to_string(bool b) {return (b ? "true" : "false");}; template <size_t N> string to_string(bitset<N> v){ string res = ""; for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; }; template <typename A> string to_string(A v) { bool first = true; string res = "{"; for(const auto &x : v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; }; template <typename A, typename B> string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";} void debug_out() {cerr << endl;}; template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }; void LINE_OUT() { cout << "--------------" << endl; }; #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define LINE LINE_OUT(); #else #define debug(...) 71 #define LINE 71; #endif void print() { cout << endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }; template <class T> void print(vector<T> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }; template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll X, Y, A, B; cin >> X >> Y >> A >> B; ll x = X; // 強さ ll ans = 0; while (1) { if ((ld)x * (ld)A > INF) { ll diff = Y - x -1; if (diff <= 0) break; ans += (diff/B); break; } else if (x * A > Y-1) { ll diff = Y - x -1; if (diff <= 0) break; ans += (diff/B); break; } else { if (x * A <= x + B) { x = x * A; ans++; } else { ll diff = x * A - x; ans += (diff/B); x = x + (diff/B) * B; } } } cout << ans << endl; return 0; };
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <complex> #include <iomanip> #include <numeric> #include<unordered_set> #include <climits>//INT_100010n #include <regex> #include<deque> //#include<bits/stdc++.h> #define PP pair<ll,int> #define inf 0x3f3f3f3f #define INF 0x7fffffff #define llinf 0x3f3f3f3f3f3f3f3fll #define dinf 1000000000000.0 #define PI 3.1415926 #define LL unsigned int #define wc 1e-18 typedef long long ll; using namespace std; const ll mod=1e9+7; ll x,y,a,b,ct; int main() { cin>>x>>y>>a>>b; while(x<y/a&&x<(x+b)/a){ ct++; x=x*a; } ct=ct+(y-1-x)/b; cout<<ct<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define start() int t;cin>>t;while(t-->0) #define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define endl "\n" #define mod 1000000007 bool isPrime(int n) { // Corner cases if(n<=3){ return true; } if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int main() { speed; ll n; cin>>n; ll a[n]; map<ll,ll> m; for(int i=0;i<n;i++){ cin>>a[i]; m[a[i]%200]++; } ll sum=0; for(auto x:m){ if(x.second>1){ sum+=(x.second*(x.second-1))/2; } } cout<<sum; return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define pb(a) push_back(a) #define vll vector<ll> #define loop(i, n) for(ll i=1;i<=n;i++) #define loop0(i, n) for(ll i=0;i<n;i++) #define in(i) scanf("%lld", &i); #define out(i) printf("%lld", i) #define pll pair<ll, ll> #define vpll vector<pair<ll, ll>> #define mp(i, j) make_pair(i, j); const ll mod = 1000000007; const ll big = 2999999999999999999; const ll small = -2999999999999999999; void in2(ll& a, ll& b) { in(a); in(b); } void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); } void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; } void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); } ll n; int main() { in(n); vll counter(200, 0); ll ip; loop0(i, n) { in(ip); counter[ip%200] += 1; } ll ans = 0; loop0(j, 200) { ll i = counter[j]; ans += (i*(i-1)/2); } cout<<ans<<"\n"; }
#include <bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fod(i,a,b) for(int i=a;i>=b;i--) #define me0(a) memset(a,0,sizeof(a)) #define me1(a) memset(a,-1,sizeof(a)) #define op freopen("in.txt", "r", stdin) #define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin); #define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout) #define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout) #define pii pair<int,int> #define Please return #define AC 0 using namespace std; const int INF = 0x3f3f3f3f; typedef long long LL; template <class T> void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int mod=998244353; const int maxn = 1e6+10; int n,m,a[maxn],q,t; int b[maxn],c[maxn],fa[maxn],d[maxn]; int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } LL sum[maxn]; int main(){ // op; read(n);read(m); fo(i,1,n) read(a[i]); fo(i,1,n) read(b[i]); fo(i,1,n) fa[i]=i; fo(i,1,m) read(c[i]),read(d[i]),fa[find(c[i])]=find(d[i]); fo(i,1,n) { sum[find(i)]+=a[i]; sum[find(i)]-=b[i]; } int flag = 0; fo(i,1,n) if(sum[i]) flag =1; if(flag)puts("No"); else puts("Yes"); Please AC; }
#include <bits/stdc++.h> #define pb push_back #define int long long #define ld long double using namespace std; const int M = 1000000007; const int N = 1008; vector<int> g[N]; char p[N][N]; bool used[N]; int parent[N]; char ans0[N][N]; void dfs(int v) { used[v] = true; for (auto u : g[v]) { if (!used[u]) { parent[u] = v; if (p[v][u] != '-') { ans0[v][u] = p[v][u]; ans0[u][v] = p[v][u]; p[v][u] = '-'; p[u][v] = '-'; } dfs(u); } else if (p[v][u] != '-') { ans0[v][u] = p[v][u]; ans0[u][v] = p[v][u]; p[v][u] = '-'; p[u][v] = '-'; } } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; vector<int> A(m), B(m); for (int i = 0; i < m; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<int> C(n); for (int i = 0; i < n; i++) cin >> C[i]; vector<char> ans(m, '>'); for (int i = 0; i < m; i++) { if (C[A[i]] > C[B[i]]) { ans[i] = '>'; } else if (C[A[i]] < C[B[i]]) { ans[i] = '<'; } else { g[A[i]].pb(B[i]); g[B[i]].pb(A[i]); p[A[i]][B[i]] = '>'; p[B[i]][A[i]] = '<'; } } for (int v = 0; v < n; v++) { if (!g[v].empty() && !used[v]) { dfs(v); } } for (int i = 0; i < m; i++) { if (C[A[i]] == C[B[i]]) { ans[i] = ans0[A[i]][B[i]]; } if (ans[i] == '>') { cout << "->\n"; } else { cout << "<-\n"; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define sz(a) int(a.size()) template <typename A, typename B> ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif void solve() { int h,w,x,y; cin >> h >> w >> x >> y; vector<string> v(h); for(int i=0; i<h; i++) { cin >> v[i]; } --x; --y; int ret = 0; int dir[] = {0, 1, 0, -1, 0}; for(int d=0; d<4; d++) { int i = x, j = y; while(i >= 0 and i < h and j >= 0 and j < w and v[i][j] == '.') { ++ret; i += dir[d]; j += dir[d+1]; } } ret -= 3; cout << ret << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> using namespace std; //using namespace __gnu_pbds; #define fastio ios_base::sync_with_stdio(0);cin.tie(0) #define fp(i,a,b) for(int i=a ; i<b ; i++) #define fn(i,a,b) for(int i=a ; i>=b ; i--) #define ones(x) __builtin_popcount(x) #define pb push_back #define mk make_pair #define ff first #define ss second #define all(x) x.begin(),x.end() #define dbg(x) cerr << #x << " = " << x << " " #define fin cout << "\n"; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; /* typedef tree< pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;*/ const int M=5e3+7; const int inf=2e9; const int mod=998244353; int h,w,k; ll dp[M][M]; char mat[M][M]; ll row[M][M],col[M][M]; ll pot3[M]; void go(){ cin >> h >> w >> k; pot3[0] = 1; fp(i,1,M) pot3[i] = (pot3[i-1] * 3LL) % mod; memset(mat, '0', sizeof mat); while (k--){ int i,j; cin >> i >> j; char c; cin >> c; mat[i][j] = c; } fp(i,1,h+1) fp(j,1,w+1) row[i][j] = row[i][j-1] + ((mat[i][j] == '0') ? 1 : 0); fp(j,1,w+1) fp(i,1,h+1) col[j][i] = col[j][i-1] + ((mat[i][j] == '0') ? 1 : 0); dp[1][1] = 1; fp(i,1,h+1) fp(j,1,w+1){ char c = mat[i][j]; if (c == 'D' || c == 'X') dp[i+1][j] = (dp[i+1][j] + (pot3[row[i+1][j-1]] * dp[i][j]) % mod) % mod; if (c == 'R' || c == 'X') dp[i][j+1] = (dp[i][j+1] + (pot3[col[j+1][i-1]] * dp[i][j]) % mod) % mod; if (c == '0'){ dp[i+1][j] = (dp[i+1][j] + (2LL * pot3[row[i+1][j-1]] * dp[i][j]) % mod) % mod; dp[i][j+1] = (dp[i][j+1] + (2LL * pot3[col[j+1][i-1]] * dp[i][j]) % mod) % mod; } } /*fp(i,1,h+1){ cout << i << " : "; fp(j,1,w+1) cout << dp[i][j] << " "; fin; }*/ if (mat[h][w] == '0') dp[h][w] *= 3; cout << dp[h][w] % mod << "\n"; } int main(){ fastio; int tst=1; //cin >> tst; fp(i,1,tst+1) go(); return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main(){ int n, m; cin >> n >> m; vector<int> A(m); for(int i = 0; i < m; ++i) cin >> A[i]; m += 2; A.emplace_back(0); A.emplace_back(n+1); sort(A.begin(), A.end()); int d = n; for(int i = 1; i < m; ++i){ if(A[i] == A[i-1]+1) continue; d = min(d,A[i]-A[i-1]-1); } int ans = 0; for(int i = 1; i < m; ++i){ int dd = A[i] - A[i-1] - 1; if(!dd) continue; ans += max(0,(dd+d-1)/d); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std ; #define rep(i,n) for( int i = 0 ; i < n ; i++ ) typedef long long ll ; const ll mod = 1e9 + 7 ; ll upper_cal(ll n,ll m){ if( n%m == 0)return n/m ; else return n/m + 1 ; } int main(){ ll n , m ; cin >> n >> m ; if( m == 0 ){ cout << 1 << endl ; return 0 ; } if( m == n ){ cout << 0 << endl ; return 0 ; } vector<ll> A(m+2) ; A[0] = -1 ; A[m+1] = n ; rep(i,m){ cin >> A.at(i+1) ; A.at(i+1)-- ; } sort(A.begin(),A.end()) ; ll range = n ; for(ll i = 0 ; i < m + 1 ; i++ ){ ll tmp = max((ll)0, A.at(i+1) - A.at(i) - 1 ) ; if( tmp != 0 ) range = min( range , tmp ) ; } ll ans = 0 ; for(ll i = 0 ; i < m + 1 ; i++ ){ ll tmp = max((ll)0, A.at(i+1) - A.at(i) - 1 ) ; if( tmp != 0 ) ans +=upper_cal(tmp,range) ; } cout << ans << endl ; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; vector<vector<int>> dist(N, vector<int>(N, -1)); queue<pair<int, int>> que; for (int i = 0; i < N; i++) { dist[i][i] = 0; que.push(make_pair(i, i)); } vector<vector<pair<int, int>>> G(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; char c; cin >> c; u--, v--; G[u].emplace_back(make_pair(v, c - 'a')); G[v].emplace_back(make_pair(u, c - 'a')); if (dist[u][v] < 0) { dist[u][v] = 1; que.push(make_pair(u, v)); } if (dist[v][u] < 0) { dist[v][u] = 1; que.push(make_pair(v, u)); } } while (!que.empty()) { auto [x, y] = que.front(); que.pop(); for (const auto &a : G[x]) { for (const auto &b : G[y]) { if (a.second == b.second) { if (dist[a.first][b.first] < 0) { dist[a.first][b.first] = dist[x][y] + 2; que.push(make_pair(a.first, b.first)); } } } } } cout << dist[0][N - 1] << '\n'; return 0; }
#include <bits/stdc++.h> #define REP(i,n) for(int i=0;i<(int)(n);++i) #define ALL(v) (v).begin(),(v).end() #define debug(x) cerr<<#x<<": "<<(x)<<endl using namespace std; using llong = long long; using vi = vector<int>; using vvi = vector<vi >; using vvvi = vector<vvi >; using pii = pair<int,int>; constexpr int INF = 1e9; constexpr llong LINF = 1e18; constexpr double EPS = 1e-12; constexpr int MOD = 1e9+7; template<class Type> void line(const Type &a){int cnt=0;for(const auto &elem:a)cerr<<(cnt++?' ':'>')<<elem;cerr<<endl;} /*ダイクストラ法*/ template<typename T> class Dijkstra{ using P=std::pair<T,int>; struct Edge{ //Edge:辺の情報を表す構造体. int to; T cost; }; int vn; //vn:=(ノード数). std::vector<std::vector<Edge> > g; //g[v]:=(ノードvがもつ辺のリスト). std::vector<T> d; //d[v]:=(ノードsからvへの最短距離). std::vector<int> pre; //pre:=(逆方向経路(経路復元用)). T inf; public: //constructor. Dijkstra():Dijkstra(0){} Dijkstra(size_t vn_,T inf_=1e9):vn(vn_),g(vn_),d(vn_,inf_),pre(vn_,-1),inf(inf_){} int get_vn() const{return vn;} T get_inf() const{return inf;} void add_edge(int from,int to,T cost){//重み付き有向辺を張る. assert(0<=from and from<vn); assert(0<=to and to<vn); g[from].push_back((Edge){to,cost}); } void dijkstra(int s){//sから各ノードへの最短距離を求める. O(|E|log|V|). assert(0<=s and s<vn); std::fill(d.begin(),d.end(),inf); d[s]=0; std::fill(pre.begin(),pre.end(),-1); std::priority_queue<P,std::vector<P>,std::greater<P> > pque; pque.emplace(0,s); while(!pque.empty()){ auto [cost,v]=pque.top(); pque.pop(); if(d[v]<cost) continue; for(const Edge &e:g[v]){ if(d[e.to]>d[v]+e.cost){ d[e.to]=d[v]+e.cost; pre[e.to]=v; pque.emplace(d[e.to],e.to); } } } } T dist(int t) const{//ノードsからtへの最短距離を返す. assert(0<=t and t<vn); return d[t]; } std::vector<int> get_path(int t) const{//ノードtへの最短経路を返す. assert(0<=t and t<vn); std::vector<int> path; for(;t!=-1;t=pre[t]) path.push_back(t); std::reverse(path.begin(),path.end()); return path; } }; int main(){ int n,m; cin>>n>>m; vvvi g(n,vvi(26));//g[v][c]:=(vがもつ文字cが書かれる辺のリスト). REP(i,m){ int a,b; char c; cin>>a>>b>>c; a--, b--; g[a][c-'a'].push_back(b); g[b][c-'a'].push_back(a); } map<pii,int> mp;//グラフのノード番号参照用. int vn=0; REP(i,n)for(int j=i;j<n;++j) mp[pii(i,j)]=vn++; Dijkstra<llong> ob(vn,LINF); int s=mp[pii(0,n-1)], t=vn-1; REP(i,n) ob.add_edge(mp[pii(i,i)],t,0); REP(i,n)REP(c,26)for(auto j:g[i][c]){ if(i>=j) continue; ob.add_edge(mp[pii(i,j)],t,1); } REP(i,n)for(int j=i;j<n;++j){ int u=mp[pii(i,j)]; REP(c,26)for(auto ni:g[i][c])for(auto nj:g[j][c]){ int cni=ni, cnj=nj; if(cni>cnj) swap(cni,cnj); int v=mp[pii(cni,cnj)]; ob.add_edge(u,v,2); } } ob.dijkstra(s); llong ans=ob.dist(t); cout<<(ans==ob.get_inf()?-1:ans)<<endl; }
#line 2 "header.hpp" //%snippet.set('header')% //%snippet.fold()% #ifndef HEADER_H #define HEADER_H // template version 2.0 using namespace std; #include <bits/stdc++.h> // varibable settings template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1; #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i) #define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(...) make_tuple(__VA_ARGS__) #define ub upper_bound #define lb lower_bound #define lpos(A, x) (lower_bound(all(A), x) - A.begin()) #define upos(A, x) (upper_bound(all(A), x) - A.begin()) template <class T, class U> inline void chmax(T &a, const U &b) { if ((a) < (b)) (a) = (b); } template <class T, class U> inline void chmin(T &a, const U &b) { if ((a) > (b)) (a) = (b); } template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); } template <class T> T cdiv(T a, T b){ assert(a >= 0 && b > 0); return (a+b-1)/b; } #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) sort(all(x)); x.erase(unique(all(x)), x.end()) #define slice(l, r) substr(l, r - l) typedef long long ll; typedef long double ld; using vl = vector<ll>; using vvl = vector<vl>; using pll = pair<ll, ll>; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); } #if defined(PCM) || defined(LOCAL) #else #define dump(...) ; #define dump_1d(...) ; #define dump_2d(...) ; #define cerrendl ; #endif #endif /* HEADER_H */ //%snippet.end()% #line 2 "solve.cpp" template<class T=ll> using vec = vector<T>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; int solve() { ll n;cin>>n; ll ans = 0; rep(i, n){ ll a,b;cin>>a>>b; ans += (a+b) * (b-a+1) / 2; } cout << ans << endl; return 0; } int main(){/*{{{*/ solve(); #if defined(PCM) || defined(LOCAL) check_input(); #endif return 0; }/*}}}*/
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define ll int64_t #define rep(i,n) for(int i=0;i<n;i++) #define REP(i,n) for(int i=1;i<=n;i++) #define all(V) V.begin(),V.end() #define PI 3.14159265359 #define fi fixed #define se setprecision(10) using namespace std; int main(){ ll n;cin >> n; ll a[n],b[n]; rep(i,n) cin >> a[i] >> b[i]; ll ans = 0; rep(i,n){ ll aaa = a[i]; ll bbb = b[i]; ans += (aaa+bbb)*(bbb-aaa+1); } cout << ans/2 << endl; }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;} template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int N; int A[402020]; ll mo=3; ll fact[403030]; ll fact3[403030]; ll modpow(ll a, ll n=mo-2) { ll r=1; while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1; return r; } ll comb(int A,int B) { int C=A-B; if(fact3[A]>fact3[B]+fact3[C]) return 0; return fact[A]*modpow(fact[B]*fact[C])%mo; } void solve() { int i,j,k,l,r,x,y; string s; cin>>N; fact[0]=1; fact3[0]=0; for(i=1;i<=N-1;i++) { fact[i]=fact[i-1]; fact3[i]=fact3[i-1]; x=i; while(x%3==0) { fact3[i]++; x/=3; } fact[i]=fact[i]*x%3; } cin>>s; ll ret=0; FOR(i,N) { if(s[i]=='B') A[i]=0; if(s[i]=='W') A[i]=1; if(s[i]=='R') A[i]=2; if(N%2==1) { ret+=comb(N-1,i)*A[i]; } else { ret-=comb(N-1,i)*A[i]; } } ret=(ret%3+3)%3; cout<<"BWR"[ret]<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef map<ll,ll>::iterator itll; typedef long double ld; typedef map<ll,ll> mapll; #define con continue #define pb push_back #define fi first #define se second #define fr front() #define INF 1000000000000000000 #define all(vl) vl.begin(),vl.end() #define m_p make_pair #define sz(a) sizeof(a) #define forn(mp,it) for(it = mp.begin();it!=mp.end();it++) #define FOR(i,a,n) for(ll i=a;i<n;i++) #define FORE(i,a,n) FOR(i,a,n+1) #define Endl endl #define eNdl endl #define enDl endl #define endL endl #define MIN(x,y) x=min(x,y) #define MAX(x,y) x=max(x,y) #define left 0 #define right 1 #define pq priority_queue const double one = 1.0; const double PI = 3.14159265/*358979323846*/; const ll maxn = 1e4+10; const ld eps = 1e-14; const ll mod = 1000000007; map<ll,ll> cnt; void solve() { ll n,a,b; cin>>n>>a>>b; ll N = n,A = a,B = b; if(n-a+1 <= 0 || n-b+1 <= 0 || n-a-b+1 <= 0) { cout<<0<<endl; return; } ll x = ((N-A+1)*(N-B+1)-(N-A-B+2)*(N-A-B+1))%mod; ll aa = (N-A+1)*(N-A+1)%mod; ll bb = ((N-B+1)*(N-B+1)%mod); ll cc = (x*x%mod); ll res = (aa*bb+mod-cc)%mod; cout<<res<<Endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; cin>>t; while(t--)solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define rep(i, n) ;for (int i = 0;i < (int)(n);i++) #define rep2(i, s, n) ;for (int i = s; i < (int)(n);i++) #define ALL(vec) (vec).begin(),(vec).end() #define pi 3.1415926535897932384626433832795 #define MAX_INF 9223372036854775807 #define MIN_INF (9223372036854775807+1) #define sosuu 1000000007 lli combi[65][35]; lli nCr(lli n, lli r) { if(n<r) return 0; if(n==r) return combi[n][r] = 1; if(r==0) return combi[n][r] = 1; if(r==1) return combi[n][r] = n; if(combi[n][r]) return combi[n][r]; return combi[n][r] = nCr(n-1,r) + nCr(n-1,r-1); } int main() { int A, B; cin >> A >> B; lli K; cin >> K; int now=A+B; lli ground=0; int all=now; rep(i, all){ if(now==0){ if(K==0) cout << 'a'; else cout << 'b'; break; } if(A!=0){ if(B!=0){ if(nCr(now-1, A-1)<K){ cout << 'b'; K-=nCr(now-1, A-1); now --; B --; continue; }//(nCr(now-1, A-1)<K) else{ cout << 'a'; now --; A --; continue; }//(nCr(now-1, A-1)>=K) }//B!=0 else{ cout << 'a'; A--; continue; }//B==0 }//A!=0 else{ cout << 'b'; B--; continue; }//A==0 } cout << 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>; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; const int INF = (int)1e9; const ll INFL = (ll)1e18; const int MOD = 1e9 + 7; const double EPS = 1e-10; int dx[]={0, 0, -1, 1}; int dy[]={1, -1, 0, 0}; 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; } int main() { int h, w, x, y; cin >> h >> w >> x >> y; char s[110][110] = {'#'}; x--; y--; rep(i,h) { rep(j,w) { cin >> s[i][j]; } } int ans = 1; int p = 1; if (s[x][y + p] == '.') { while (s[x][y + p] == '.' && y + p != w) { p++; ans++; } } int i = 1; if (y != 0) { if (s[x][y - i] == '.') { while (s[x][y - i] == '.') { i++; ans++; if (y - i == -1) break; } } } int j = 1; if (s[x + j][y] == '.') { while (s[x + j][y] == '.' && x + j != h) { j++; ans++; } } int k = 1; if (x != 0) { if (s[x - k][y] == '.') { while (s[x - k][y] == '.') { k++; ans++; if (x - k == -1) break; } } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int a,b,c,mod=4; long long mypow(long long x,long long times){ if(times==0)return 1; long long ret=mypow(x,times/2);ret=ret*ret%mod; if(times%2)ret=ret*x%mod;return ret; } bool vis[10]; int x[4]; int main(){ cin>>a>>b>>c;a%=10; int qaq; if(a==1){cout<<1;return 0;} if(a==0){cout<<0;return 0;} if(a==5){cout<<5;return 0;} x[1]=a;for(int i=2;i<=4;i++)x[i%4]=x[i-1]*a%10; cout<<x[mypow(b,c)]; }
//OM TAT SAT #include<bits/stdc++.h> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; if(c==0 && a>b) { cout<<"Takahashi"<<endl; } else if(c==0 &&a<=b) { cout<<"Aoki"<<endl; } else if(c==1 && a<b) { cout<<"Aoki"<<endl; } else if(c==1 &&a>=b) { cout<<"Takahashi"<<endl; } }
// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder #include<bits/stdc++.h> //#include<atcoder/all> typedef long long ll; typedef long double ld; using namespace std; //using namespace atcoder; using Pii = pair<int, int>; using Pll = pair<ll, ll>; //ordered_set 重複不可 #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // use set_function + find_by_order(select itr-num) #define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i) #define ALL(x) (x).begin(),(x).end() #define pb push_back 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;} template<class T>inline bool chmax(T &a, T b) {if(a < b) {a = b;return true;}return false;} template<class T>inline bool chmin(T &a, T b) {if(a > b) {a = b;return true;}return false;} char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, 1, -1}; //cout << std::fixed << std::setprecision(15) << y << endl; //小数表示 int main(){ int n; cin >> n; vector<ll> lis(n); REP(i,0,n){cin >> lis[i];} vector<vector<vector<ll>>> dp(200), pdp(200); REP(i,0,n){ int key = lis[i] % 200; dp = pdp; REP(l,0,200){ int count = 0; REP(j,0, pdp[l].size() ){ count++; if(count >= 3) break; vector<ll> mid = pdp[l][j]; mid.pb(i); if(dp[(l+key)%200].size() >= 2) continue; dp[(l+key)%200].pb(mid); } } if(!(dp[(key)%200].size() >= 2)){ vector<ll> mid; mid.pb(i); dp[key].pb(mid); } pdp = dp; } /* REP(i,0,10){ cout << "#" << i << endl; cout << dp[i].size() << endl;; REP(l,0,dp[i].size()){ REP(j,0,dp[i][l].size()){ cout << dp[i][l][j] << " "; } cout << endl; } } */ REP(i,0,200){ if(dp[i].size() >= 2){ cout << "Yes" << endl; REP(k,0,2){ cout << dp[i][k].size()<< " "; REP(l,0,dp[i][k].size()){ cout << dp[i][k][l] + 1 << " "; } cout << endl; } return 0; } } cout << "No" << endl; }
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main(void){ int n; cin >> n; vector< int > a(n+1); for(int i = 1; i <= n; i++){ cin >> a[i]; a[i] %= 200; } for(int i = 1; i <= n; i++){ if(a[i] % 200 == 0){ puts("Yes"); if(i == 1){ cout << "1 " << i+1 << endl; cout << "2 " << i << " " << i+1 << endl; }else{ cout << "1 " << i-1 << endl; cout << "2 " << i-1 << " " << i << endl; } return 0; } } vector< vector< int > > dp(n+1, vector< int >(200, 0)); auto f = [&](auto f, int i, int j, vector< int >& v){ if(i == 0) return; if(dp[i-1][j] > 0){ f(f, i-1, j, v); }else{ v.push_back(i); f(f, i-1, (j-a[i]+200)%200, v); } }; dp[0][0] = 1; for(int i = 1; i <= n; i++){ for(int j = 0; j < 200; j++){ dp[i][j] = dp[i-1][j] + dp[i-1][(j-a[i]+200)%200]; if(dp[i-1][j] > 0 && dp[i-1][(j-a[i]+200)%200] > 0){ vector< int > b, c({i}); f(f, i-1, j, b); if(b.empty()) continue; f(f, i-1, (j-a[i]+200)%200, c); puts("Yes"); sort(b.begin(), b.end()); cout << b.size(); for(int i: b) cout << " " << i; cout << endl; sort(c.begin(), c.end()); cout << c.size(); for(int i: c) cout << " " << i; cout << endl; return 0; } } } puts("No"); }
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<string> #include<stdio.h> #include<stdlib.h> #include<float.h> #include<tuple> #include<string.h> #include<iomanip> #include<stack> #include<queue> #include<map> #include<deque> #include<math.h> using namespace std; #define ll long long #define rep(i,n) for(ll i=0;i<n;i++) #define REP(i,n) for(ll i=1;i<=n;i++) #define ALLOF(c) (c).begin(), (c).end() #define Pa pair<ll,ll> const ll mod=1000000007; const ll INF=1e12; const ll inf=-1; ll ABS(ll a){return max(a,-a);} int main(void){ ll N; string T; cin>>N>>T; if(T=="1"){ cout<<20000000000<<endl; return 0; } if(T=="11" || T=="0"){ cout<<10000000000<<endl; return 0; } if(T.at(0)=='0') T="11"+T; if(T.at(0)=='1' && T.at(1)=='0') T="1"+T; if(T.at(T.size()-3)=='0' && T.at(T.size()-2)=='1' && T.at(T.size()-1)=='1') T+="0"; if(T.at(T.size()-2)=='0' && T.at(T.size()-1)=='1') T+="10"; if(T.size()%3){ cout<<0<<endl; return 0; } rep(i,T.size()/3){ if(T.at(3*i)=='1' && T.at(3*i+1)=='1' && T.at(3*i+2)=='0') continue;; cout<<0<<endl; return 0; } cout<<10000000000-T.size()/3+1<<endl; return 0; }
//#include<atcoder/all> //#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; //using namespace atcoder; using ll = long long; //using mint = modint1000000007; using vi = vector<ll>; using vi2 = vector<vector<ll>>; using vl = vector<ll>; using vl2 = vector<vector<ll>>; using vb = vector<bool>; using vb2 = vector<vector<bool>>; using vc = vector<char>; using vc2 = vector<vector<char>>; using vs = vector<string>; using si = set<ll>; using vs2 = vector<vector<string>>; using pii = pair<ll, ll>; using mii = map<ll, ll>; const int INF = 1e9+1; const ll LINF = 1e18+1; const long double LPI = acos(-1.0); const double PI = acos(-1.0); const ll mod = 998244353; #define MP(a,b) make_pair((a),(b)) #define MT(...) make_tuple(__VA_ARGS__) #define sz(x) (int)(x).size() #define FOR(i,a,b) for(ll i=(a);i<(b);i++) #define REP(i,x) for(ll i=0;i<(int)(x);i++) #define REPS(i,x) for(ll i=1;i<=(int)(x);i++) #define RREP(i,x) for(ll i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(ll i=((int)(x));i>0;i--) #define ALL(x) (x).begin(),(x).end() #define RALL(x) (x).rbegin(),(x).rend() #define SORT(v) sort(ALL(v)); #define RSORT(v) sort(RALL(v)); #define IN(type,a) type a;cin >> a; #define VIN(type,a,n) vector<type> a(n);cin >> a; #define YES(n) cout << ((n) ? "YES" : "NO" ) << endl; #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl; #define COUT(x) cout << (x) << endl; #define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl; #define ENDL cout << endl; #define DB(s,x) cout << (s) << " : " << x << endl; #define HOGE COUT("HOGE") template<typename T>inline istream& operator>>(istream&i,vector<T>&v) {REP(j,sz(v))i>>v[j];return i;} template<typename T> bool chmax(T& a, const T& b){if(a<b){a=b;return true;}return false;} template<typename T> bool chmin(T& a, const T& b){if(a>b){a=b;return true;}return false;} template<typename T> T gcd(T a,T b){if(b==0){return a;}else{return gcd(b, a%b);}} template<typename T> T lcm(T a,T b){return a*b/gcd(a, b);} template<typename T> bool isPrime(T a){if(a<=1){return false;} for(T i=2;i*i<=a;i++){if (a%i==0) {return false;}}return true;} template<typename T> T ord(T p,T n){T ans=0;while(true){if(n%p==0){n/=p;ans++;}else{break;}}return ans;} char janken(char a,char b){ if (a==b) { return a; } if (a=='R') { if (b=='P') { return b; } if (b=='S') { return a; } } if (a=='P') { if (b=='R') { return a; } if (b=='S') { return b; } } if (a=='S') { if (b=='P') { return a; } if (b=='R') { return b; } } return 'k'; } int main() { IN(ll, n); IN(string, s); reverse(ALL(s)); ll start = -1; REP(i, n){ if (s[i]=='0') { start = i; break; } } if (start==-1) { if (s=="1") { COUT(20000000000); return 0; }else if (s=="11"){ COUT(10000000000); return 0; }else{ COUT(0); return 0; } } if (start>=3) { COUT(0); return 0; } ll count = 0; for (ll i = start; i < n; i++) { if ((i-start)%3==0) { if (s[i]=='0') { count++; }else{ COUT(0); return 0; } }else{ if (s[i]=='1') { }else{ COUT(0); return 0; } } } if (start==0) { COUT(10000000000-count+1); }else{ COUT(10000000000-count); } }
#pragma region #define _USE_MATH_DEFINES #include <iostream> #include <string> #include <algorithm> #include <cmath> #include <cstdlib> #include <vector> #include <map> #include <queue> #include <stack> #include <set> #include <list> #include <iomanip> #include <cstdint> #include <bitset> #include <fstream> #include <cassert> #include <numeric> //#include <atcoder/all> using namespace std; //using namespace atcoder; //using mint = modint; typedef long long ll; //#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i)) #define all(x) x.begin(),x.end() #pragma region UnionFind struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; #pragma endregion #pragma region GCD int gcd(int a, int b) { if (a%b == 0)return b; return gcd(b, a%b); } #pragma endregion #pragma region chmin template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } #pragma endregion #pragma region chmax template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } #pragma endregion #pragma region 一次元座圧 template <typename T> vector<T> compress(vector<T> &X) { vector<T> vals = X; sort(all(vals)); vals.erase(unique(all(vals)), vals.end()); rep(i, (int)X.size())X[i] = lower_bound(all(vals), X[i]) - vals.begin(); return vals; } #pragma endregion #pragma endregion int main() { int n; cin >> n; map<int, bool> mp; int now = 0; rep(i, n) { int q; cin >> q; mp[q] = true; while (mp[now])++now; cout << now << endl; } }
// For the purpose of C++ competitive programming only. #include "bits/stdc++.h" using namespace std; #define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++) #define FORL(a, b, c) for (int(a) = (b); (a) < (c); (a)++) #define FORR(a, b, c) for (int(a) = (b); (a) > (c); (a)--) #define INF 1000000000000000003 #define MOD 1000000007 #define F first #define S second #define pb push_back #define pob pop_back #define mp make_pair #define endl '\n' typedef long long int ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N, f = 0, s = 0, t; string fn, sn, tn; cin >> N; FOR(i, N) { cin >> tn >> t; if (t > f) { s = f; f = t; sn = fn; fn = tn; } else { if (t > s) { s = t; sn = tn; } } } cout << sn << endl; return 0; }
#include <bits/stdc++.h> using ll = long long int; using P = std::pair<int, int>; #define rep(i, n) for (int i = 1; i <= (n); ++i) #define rag(con) std::begin(con), std::end(con) int main() { int n; ll x; std::cin >> n >> x; std::vector<ll> a(n + 1), b(n + 1), c(n); std::vector<std::vector<ll>> d(n + 1, std::vector<ll>(2)); rep(i, n) std::cin >> a[i]; for (int i = n; i >= 1; --i) { b[i] = x / a[i]; x = x % a[i]; //std::cerr << b[i]; } //std::cerr << std::endl; for (int i = 1; i < n; ++i) c[i] = a[i + 1] / a[i]; d[1][0] = 1; if (b[1] > 0) d[1][1] = 1; for (int i = 2; i <= n; ++i) { d[i][0] = d[i - 1][0]; if (b[i] != c[i] - 1) d[i][0] += d[i - 1][1]; d[i][1] = d[i - 1][1]; if (b[i] > 0) d[i][1] += d[i - 1][0]; //std::cerr << d[i][0] << ' ' << d[i][1] << std::endl; } std::cout << d[n][0] << std::endl; return 0; }
#include<bits/stdc++.h> #define int long long #define HgS 998244353 using namespace std; char ch[510][510]; char dep[1510]; long long ksm(int x) { long long a=2; long long ans=1; while(x) { if(x&1)ans=ans*a%HgS; a=a*a%HgS;x>>=1; } return ans; } signed main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int n,m; bool Noans=0; cin>>n>>m; for(int i=2;i<=n+m;++i)dep[i]='.'; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { cin>>ch[i][j]; if(ch[i][j]=='.')continue; // cout<<i<<' '<<j<<'\n'; if(dep[j+i]=='.') dep[j+i]=ch[i][j]; else if(dep[j+i]!=ch[i][j])Noans=1; } } if(Noans) { cout<<0; return 0; } int ans=0; for(int i=2;i<=n+m;++i) { if(dep[i]=='.')ans++; } // cout<<ans<<'\n'; cout<<ksm(ans)<<'\n'; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int main(){ string s; cin>>s; if(s.length()==1){ puts(stoi(s)%8==0?"Yes":"No"); return 0; } if(s.length()==2){ string t=s; swap(t[0],t[1]); puts(stoi(s)%8==0||stoi(t)%8==0?"Yes":"No"); return 0; } int freq[10]={}; for(char c:s) freq[c-'0']++; rep(d1,10) if(freq[d1]>0) { freq[d1]--; rep(d2,10) if(freq[d2]>0) { freq[d2]--; rep(d3,10) if(freq[d3]>0) { freq[d3]--; if((100*d1+10*d2+d3)%8==0){ puts("Yes"); return 0; } freq[d3]++; } freq[d2]++; } freq[d1]++; } puts("No"); return 0; }
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;} template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int ok[1<<20]; int N,M; int E[20]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>M; FOR(i,M) { cin>>x>>y; x--,y--; E[x]|=1<<y; E[y]|=1<<x; } int mask; FOR(mask,1<<N) { ok[mask]=1; FOR(y,N) if(mask&(1<<y)) if(E[y]&mask) ok[mask]=0; } ll ret=0; for(mask=1;mask<(1<<N);mask+=2) if(ok[mask]) { int sm=mask^((1<<N)-1); for(int x=sm; x>=0; x--) { x&=sm; if(ok[x]&&ok[x^sm]) ret++; } } cout<<ret*3<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
//in the name of god// // SOBHET BEKHEIR JOOOOOOON E DEL #include <bits/stdc++.h> using namespace std; typedef long long ll; #define mem(x,s) memset(x,s,sizeof(x)) #define all(x) x.begin(),x.end() #define _ ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); #define F first #define S second #define endi return cout<<'\n',0 #define MP make_pair #define pb push_back #define ppb pop_back #define pii pair<int,int> #define pll pair<ll,ll> const int maxn = 3e5 + 10; int n; int a[maxn], b[maxn], p[maxn], arr[maxn]; vector <pii> ans, v; void sol(){ cin >> n; for(int i = 1;i <= n; i++){ cin >> a[i]; v.pb(MP(a[i],i)); } for(int i = 1; i <= n; i++) cin >> b[i]; for(int i = 1;i <= n; i++){ cin >> p[i]; arr[p[i]] = i; } sort(v.begin(),v.end()); for(int i = 0; i < v.size(); i++){ int p0 = v[i].S; int p1 = arr[p0]; if(p0 == p1) continue; if(a[p0] > b[p[p0]] && a[p1] > b[p[p1]]){ ans.pb(MP(p0,p1)); swap(p[p0],p[p1]); arr[p[p0]] = p0; arr[p[p1]] = p1; } else{ cout << -1 << '\n'; return; } } cout << ans.size() << '\n'; for(auto i: ans) cout << i.F <<' '<< i.S << '\n'; } int main(){_ int t = 1; //cin >>t; while(t--){ sol(); } return 0; }
#include <bits/stdc++.h> #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define rep(i, l, r) for (ll i = l; i < (ll)(r); i++) #define rrep(i, l, r) for (ll i = ((ll)(r) - 1); i >= l; i--) using namespace std; using ll = long long; using ld = long double; using pii = pair<ll, ll>; 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) { 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 * b / gcd(a, b); } vector<ll> calc_depth(vector<vector<ll>>& g) { ll n = g.size(); vector<ll> depth(n, INFL); deque<pii> que; que.push_back({0, 0}); while (!que.empty()) { pii p = que.front(); que.pop_front(); ll pn = p.first; ll pd = p.second; depth[pn] = pd; for (auto cn : g[pn]) { if (depth[cn] == INFL) { que.push_back({cn, pd + 1}); } } } return depth; } void update(vector<ll>& a, vector<ll>& b, vector<ll>& depth, vector<ll>& cnt, ll t, ll e, ll x) { if (t == 1) { if (depth[a[e]] < depth[b[e]]) { cnt[0] += x; cnt[b[e]] -= x; } else { cnt[a[e]] += x; } } else { if (depth[a[e]] < depth[b[e]]) { cnt[b[e]] += x; } else { cnt[0] += x; cnt[a[e]] -= x; } } } vector<ll> calc_score(vector<vector<ll>>& g, vector<ll>& cnt) { ll n = g.size(); vector<ll> score(n, INFL); deque<pii> que; que.push_back({0, cnt[0]}); while (!que.empty()) { pii p = que.front(); que.pop_front(); ll pn = p.first; ll pd = p.second; score[pn] = pd; for (auto cn : g[pn]) { if (score[cn] == INFL) { que.push_back({cn, pd + cnt[cn]}); } } } return score; } template <typename Seq> void print_vector(Seq seq) { for (auto const& value: seq) { cout << value << " "; } cout << endl; } signed main() { ll n; cin >> n; vector<ll> a; vector<ll> b; rep(_, 0, n - 1) { ll ai, bi; cin >> ai >> bi; a.push_back(ai - 1); b.push_back(bi - 1); } vector<vector<ll>> g(n); rep(i, 0, n - 1) { g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } vector<ll> depth = calc_depth(g); vector<ll> cnt(n, 0); ll q; cin >> q; rep(_, 0, q) { ll t, e, x; cin >> t >> e >> x; e -= 1; update(a, b, depth, cnt, t, e, x); } vector<ll> score = calc_score(g, cnt); for (auto c : score) { cout << c << endl; } }
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = (int)1e5 + 7; int n, m; int x[N]; int ss[N]; int main() { scanf("%d %d", &n, &m); string s; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < m; j++) { x[i] += (s[j] - '0'); } ss[x[i]]++; } ll ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= 20; j++) { if (abs(j - x[i]) % 2 == 1) { ans += ss[j]; } } } printf("%lld\n", ans / 2); }
#include<iostream> #include<algorithm> #include<vector> #include<iomanip> #include<cmath> #include<map> #include<string> #include<bitset> using namespace std; #define rep(i,N) for(ll i=0;i<N;i++) //0から typedef long long ll; ll binary_search(vector<ll> a, ll n, ll key){ ll right = n, left = -1; ll md = (right + left) / 2; while(right - left > 1){ if(a[md] <= key){ right = md; }else{ left = md; } md = (right + left) / 2; } if(left == -1) return -1; //無い場合 return right; } vector<ll> prime; void Prime(ll n){ //線形篩,素数列挙 vector<ll> p(n,0); p[0]=1; p[1]=1; for(ll i=2;i<n;i++){ if(p[i]==0){ prime.push_back(i); p[i]=i; } ll k=prime.size(); for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){ p[i*prime[j]]=prime[j]; } } } ll gcd(ll a,ll b){ if(a<b){ swap(a,b); } //a>=b ll r=a%b; while(r!=0){ a=b; b=r; r=a%b; } return b; } ll modexp(ll x, ll a, ll m){ //x^a mod m ll ret = 1; while (a > 0) { if (a & 1) ret = ret * x % m; // n の最下位bitが 1 ならば x^(2^i) をかける x = x * x % m; a >>= 1; // n を1bit 左にずらす } return ret; } ll modinv(ll a, ll m){ //a^{-1} mod 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=(u+m)%m; return u; } ll nCr(ll n, ll r) { ll ans = 1; for (ll i = n; i > n - r; --i) { ans = ans*i; } for (ll i = 1 ; i < r + 1; ++i) { ans = ans / i; } return ans; } double pi=3.14159265358979323846; //next_purmitation!!!!!!!!!!!!! //https://atcoder.jp/contests/abc198/tasks/abc198_d //cout<<fixed<<setprecision(18); int main(){ ll n,m; cin>>n>>m; vector<string> s(n); ll odd=0,even=0; rep(i,n){ cin>>s[i]; ll cnt=0; rep(j,m){ if(s[i][j]=='1'){ cnt++; } } if(cnt%2==0){ even++; } else{ odd++; } } cout<<odd*even<<endl; return 0; }
#include <iostream> #include <vector> #include <set> using namespace std; using ll = long long; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; set<int> s{}; for (int i = 0; i <= 200000; i++) { s.insert(s.end(), i); } for (int i = 0; i < N; i++) { int p; cin >> p; if (s.find(p) != s.end()) { s.erase(p); } cout << *s.begin() << "\n"; } return 0; }
#include<fstream> #include<iostream> #include<sstream> #include<vector> #include<stack> #include<string> #include<algorithm> #include<cmath> #include<map> #include<queue> #include<tuple> #include<iomanip> #include<stdio.h> using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) using graph = vector<vector<int>>; const long long MOD = 1e9 + 7; const long long INF = 1e18; using ll = long long; using P = pair<int, int>; int main(){ int n; cin >> n; vector<pair<int,int>> a; vector<pair<int,int>> b; vector<int> z; rep(i,n){ int c,d; cin >> c >> d; int f = c+d; a.push_back(make_pair(c,i)); b.push_back(make_pair(d,i)); z.push_back(f); } sort(a.begin(),a.end()); sort(b.begin(),b.end()); sort(z.begin(),z.end()); int ans = 0; if(a[0].second!=b[0].second) cout << max(a[0].first,b[0].first) << endl; else{ int p = max(a[1].first,b[0].first); int q = max(a[0].first,b[1].first); int r = min(p,q); ans = min(z[0],r); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, a, b) for (auto i = (a); i < (b); ++i) #define per(i, a, b) for (auto i = (b); i-- > (a); ) #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define sz(x) int((x).size()) #define lb(x...) lower_bound(x) #define ub(x...) upper_bound(x) template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 21; vector<int> g[N]; bool vis[N]; vector<int> cc; void dfs(int u) { cc.push_back(u); vis[u] = 1; for (int v : g[u]) if (!vis[v]) dfs(v); } int nxt(int x, int b) { x += b + 1; if (x > 3) x -= 3; return x; } short col[N]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; rep(e, 0, m) { int u, v; cin >> u >> v; --u; --v; g[u].push_back(v); g[v].push_back(u); } ll res = 1; rep(u, 0, n) { if (vis[u]) continue; cc.clear(); dfs(u); int mx = sz(cc) - 1; mx = 1 << mx; ll tmp = 0; rep(S, 0, mx) { for (auto u : cc) col[u] = 0; col[u] = 1; bool valid = 1; int T = S; int cur = 0; for (auto u : cc) for (auto v : g[u]) if (col[v]) valid &= col[u] != col[v]; else col[v] = nxt(col[u], T >> cur++ & 1); tmp += valid; } res *= 3 * tmp; } cout << res << '\n'; }
#include <stdio.h> #include <iostream> #include <vector> #include <queue> #include <stack> #include <algorithm> using ll = long long int; using P = std::pair<ll, ll>; const int INF = (1<<30); const ll INFLL = (1ll<<60); const ll MOD = (ll)(1e9+7); #define l_ength size void mul_mod(ll& a, ll b){ a *= b; a %= MOD; } void add_mod(ll& a, ll b){ a = (a<MOD)?a:(a-MOD); b = (b<MOD)?b:(b-MOD); a += b; a = (a<MOD)?a:(a-MOD); } std::vector<P> v; int main(void){ int n,i; ll a,b,c,m,ans=0ll,p=0ll; std::cin >> n >> m; for(i=0; i<n; ++i){ std::cin >> a >> b >> c; --a; v.push_back(P(a,c)); v.push_back(P(b,-c)); } std::sort(v.begin(),v.end()); for(i=1; i<v.l_ength(); ++i){ p += v[i-1].second; ans += std::min(p,m)*(v[i].first-v[i-1].first); } std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define loop(i,a,b) for (int i=a; i<b; ++i) int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll K; cin >> K; string S,T; cin >> S >> T; long double ans=0; vector <ll> pow10 = {1,10,100,1000, 10000, 100000}; for (int i=1; i<=9; ++i) { for (int j=1; j<=9; ++j) { vector <int> freqS(10, 0), freqT(10, 0), freqcombine(10, 0); for (int k=0; k<4; ++k) { freqS[(S[k]-'0')]++; freqT[(T[k]-'0')]++; freqcombine[(S[k]-'0')]++; freqcombine[(T[k]-'0')]++; } bool possible=true; for (int k=1; k<=9; ++k) { if (freqcombine[k]>K) { possible=false; } } if (!possible) continue; freqS[i]++; freqT[j]++; ll scoreA =0, scoreB=0; for (int k=1; k<=9; ++k) { scoreA += k*pow10[freqS[k]]; scoreB += k*pow10[freqT[k]]; } long double prob = ((long double)(K-freqcombine[i]))/(9*K-8); freqcombine[i]++; prob *= ((long double)(K-freqcombine[j]))/(9*K-9); if (scoreA>scoreB) ans += prob; } } cout << fixed << setprecision(20) << ans << "\n"; return 0; }
#include <iostream> using namespace std; using ll= long long; #define REP(i,n) for(ll i=0;i<ll(n);i++) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define ALL(x) x.begin(),x.end() #define MOD (int)1e9+7 signed main() { ll n, k; ll ans=0; cin>>n>>k; ll a[n]; ll numc[n]; REP(i,n) cin>>a[i]; REP(i,n) numc[i]=0; REP(i,n) { numc[a[i]]++; } if(numc[0]>k) numc[0]=k; REP(i,n) { if(i==0){ ans+=numc[i]; }else if(numc[i-1]>numc[i]){ ans+=numc[i]; }else{ numc[i]=numc[i-1]; ans+=numc[i]; } } cout<<ans<<endl; return 0; }
#include<stdio.h> #include<algorithm> using namespace std; int main(){ long N, K, S = -1, A = -1; scanf("%ld %ld", &N, &K); while(true){ S++; long cnt; if(S < N) cnt = (S + 1) * (S + 2) / 2; else if(S < 2 * N - 2) cnt = (N * 9 - N * N * 3 + N * S * 6 - S * 6 - S * S * 2 - 4) / 2; else cnt = (N * 3 - S - 1) * (N * 3 - S - 2) / 2; if(K <= cnt) break; K -= cnt; } while(true){ A++; long cnt = min(S - A + 1, N * 2 - S + A - 1); if(cnt <= 0) continue; if(K <= cnt) break; K -= cnt; } long B = S - A - min(S - A, N - 1), C = S - A - B; while(--K) B++, C--; printf("%ld %ld %ld\n", A + 1, B + 1, C + 1); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int N; ll K; cin >> N >> K; ll dp[4][3000005] = {0}; dp[0][0] = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j <= i * N; j++) { dp[i + 1][j + 1] += dp[i][j]; dp[i + 1][j + N + 1] -= dp[i][j]; } for (int j = 1; j <= (i + 1) * N; j++) { dp[i + 1][j] += dp[i + 1][j - 1]; } } int x; for (int i = 3; i <= 3 * N; i++) { if (K <= dp[3][i]) { x = i; break; } else { K -= dp[3][i]; } } for (int i = 1; i <= N; i++) { int lo = max(1, x - i - N); int hi = min(N, x - i - 1); if (lo > hi) continue; if (K > (hi - lo + 1)) { K -= (hi - lo + 1); continue; } int y = lo + K - 1; int z = x - i - y; cout << i << " " << y << " " << z << endl; return 0; } }
#include<iostream> #include<vector> #include<utility> #include<map> #include<deque> #include<numeric> #include<algorithm> #include<unordered_map> #include<cmath> #include<string> #include<iomanip> using namespace std; using ll = long long int; constexpr int INF = 1 << 30; constexpr ll MOD = ll(998244353); vector<ll> ret; int t, l, r; int main(void) { cin >> t; ll n = 0; for (int i = 0; i < t; i++) { cin >> l >> r; n = r - l; if (l <= n) { n -= l - 1; ret.push_back(n * (n + 1) / 2); } else ret.push_back(0); } for (int i = 0; i < t; i++) cout << ret[i] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); #define ll long long #define int long long #define vi std::vector<int> #define setbits(x) __builtin_popcountll(x) #define pb push_back #define fn 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 ret(x) {cout << #x << endl; return;} #define PI 3.14159265358979323846 #define pii pair<ll,ll> #define fr first #define sc second const int mod = 1e9+7; void getio(); ll ceill(ll a, ll b) { return (a + b - 1) / b; } int ans[1000001]; void solve() { int l, r; cin >> l >> r; int use = r-2*l; int ans = 0; // cout << "use " << use << endl; if(use<0) ret(0) // if(use==0) ret(1) if(use%2) { int n = (use+1)/2; int sum = (n * (2*1 + (n-1) * 2)) / 2; ans = sum*2 + n; } else { int n = use/2 + 1; int sum = n * (n-1); ans = sum * 2 + n; } cout << ans << endl; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); getio(); int t; cin >> t; while (t--) solve(); } void getio() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif }
#include <bits/stdc++.h> using namespace std; using std::cout; using std::cin; using std::endl; #define rep(i,a) for (int i=0;i<a;i++) template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>; template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;} const int HIGH=30,WIDTH=30; int start_x,start_y,end_x,end_y; pair<int,int> DIRECTION[HIGH][WIDTH]; int DISTANCE[HIGH][WIDTH][2]={0}; int SUB_1[HIGH][WIDTH][2]; string WAY; int ANSWER; void solve(); void bulid(); void adjust(); void dijkstr(bool typ); int diff_x[4]={0,1,0,-1}; int diff_y[4]={-1,0,1,0}; int TEISUU=20; int D=5; int f(int a,int b){ if(a>b) swap(a,b); if(b-a==1) return DISTANCE[a/30][a%30][0]; else return DISTANCE[a/30][a%30][1]; } int g(int a,int b){ if(a>b) swap(a,b); if(b-a==1) return SUB_1[a/30][a%30][0]; else return SUB_1[a/30][a%30][1]; } tuple<int,int,int> h(int a,int b){ if(a>b) swap(a,b); if(b-a==1) return {a/30,a%30,0}; else return{a/30,a%30,1}; } char kigou(pair<int,int> a){ if(a==(pair<int,int>){0,1}) return 'R'; if(a==(pair<int,int>){1,0}) return 'D'; if(a==(pair<int,int>){0,-1}) return 'L'; return 'U'; } pair<int,int> dif(char a){ if(a=='R') return {0,1}; if(a=='L') return {0,-1}; if(a=='U') return {-1,0}; return {1,0}; } //main関数だよーーー int main() { solve(); } void solve(){ rep(i,HIGH) rep(j,WIDTH){ SUB_1[i][j][0]=TEISUU; SUB_1[i][j][1]=TEISUU; DISTANCE[i][j][0]=5000; DISTANCE[i][j][1]=5000; } int first_roop=167; rep(i,1000){ //cout<<"I"<<endl; cin>>start_x>>start_y>>end_x>>end_y; dijkstr(i<first_roop); bulid(); cout<<WAY<<endl; cin>>ANSWER; adjust(); } } void bulid(){ WAY=""; int look_x=end_x,look_y=end_y; while(look_x!=start_x||look_y!=start_y){ WAY+=kigou(DIRECTION[look_x][look_y]); int a,b; tie(a,b)=DIRECTION[look_x][look_y]; look_x-=a; look_y-=b; } reverse(WAY.begin(),WAY.end()); } void adjust(){ int ave=ANSWER/(int) WAY.size(); int look=start_x*30+start_y; rep(i,(int)WAY.size()){ int a,b,c; tie(a,b)=dif(WAY[i]); int now=look; now+=a*30+b; tie(a,b,c)=h(look,now); if(SUB_1[a][b][c]==TEISUU) DISTANCE[a][b][c]=ave; else{ int E=(SUB_1[a][b][c]-TEISUU); DISTANCE[a][b][c]=(DISTANCE[a][b][c]*E+ave+(E/2))/(E+1); } SUB_1[a][b][c]++; //cout<<a<<" "<<b<<" "<<c<<'\n'; look=now; } } void dijkstr(bool type){ _pq<tuple<int,int>> pq;//<lenth,to> vector<int> seen(900,(1<<30)); int start=start_x*30+start_y; seen[start]=0; pq.push({0,start}); while(!pq.empty()){ int lenth,to; tie(lenth,to)=pq.top(); pq.pop(); if(lenth<seen[to]){ continue; } int x=to/30,y=to%30; rep(i,4){ int now_x=x+diff_x[i]; int now_y=y+diff_y[i]; int now=now_x*30+now_y; if(now_x==30||now_y==30||now_x<0||now_y<0) continue; int new_dis=lenth; if(type==true) new_dis+=g(to,now); else new_dis+=f(to,now); if(chmin(seen[now],new_dis)){ pq.push({seen[now],now}); DIRECTION[now_x][now_y]={diff_x[i],diff_y[i]}; } } } }
#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 pb 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 all(a) a.begin(),a.end() #define rsort(a) {sort(all(a));reverse(all(a));} #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; 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 outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';} 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;} bool is_prime(ll n){ if(n<=1)return false; REP(i,2,sqrt(n)+1)if(n%i==0)return false; return true; } int main(){ ll a,b;cin>>a>>b; vi s; REP(i,2,72)if(is_prime(i))s.pb(i); vvi t(20); ll r=1; REP(i,a,b+1){ bool c=false; rep(j,20)if(i%s[j]==0){ c=true;t[j].pb(i);break; } if(!c)r*=2; } vb used(20,false); ll ans=0; function<void(ll)> dfs=[&](ll c){ if(c==20){ ans++;return; } dfs(c+1); if(used[c])return; for(ll x:t[c]){ bool ok=true; rep(i,20)if(used[i]&&x%s[i]==0)ok=false; if(ok){ rep(i,20)if(x%s[i]==0)used[i]=true; dfs(c+1); rep(i,20)if(x%s[i]==0)used[i]=false; } } }; dfs(0); ans*=r; out(ans); }
#include<iostream> #include<vector> #include<cmath> #include<unordered_set> #include<algorithm> using namespace std; int main(){ int n;cin>>n; vector<int> a(n),b(n),c(n),d(n); for(int i=0;i<n;i++)cin>>a[i]>>b[i]; for(int i=0;i<n;i++)cin>>c[i]>>d[i]; if(n==1){ puts("Yes"); return 0; }/* if(n==2){ if((a[1]-a[0])*(a[1]-a[0])+(b[1]-b[0])*(b[1]-b[0]) ==(c[1]-c[0])*(c[1]-c[0])+(d[1]-d[0])*(d[1]-d[0])) puts("Yes"); else puts("No"); return 0; }*/ vector<pair<long double,long double>> ab; int A=b[1]-b[0],B=-a[1]+a[0],C=a[1]*b[0]-a[0]*b[1]; for(int i=0;i<n;i++){ long double y=(A*a[i]+B*b[i]+C)/(long double)(sqrtl(A*A+B*B)); long double x=(a[i]-a[0])*(a[i]-a[0])+(b[i]-b[0])*(b[i]-b[0])-y*y; x=abs(x); x=sqrt(x); ab.push_back(make_pair(x,y)); }stable_sort(ab.begin(),ab.end()); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i==j)continue; A=d[i]-d[j],B=-c[i]+c[j],C=c[i]*d[j]-c[j]*d[i]; vector<pair<long double,long double>> cd; for(int k=0;k<n;k++){ long double y=(A*c[k]+B*d[k]+C)/(long double)(sqrtl(A*A+B*B)); long double x=(c[k]-c[j])*(c[k]-c[j])+(d[k]-d[j])*(d[k]-d[j])-y*y; x=abs(x); x=sqrt(x); cd.push_back(make_pair(x,y)); }int f=0; stable_sort(cd.begin(),cd.end()); for(int k=0;k<n;k++)if(ab[k]!=cd[k])f=1; if(f==0){ puts("Yes"); return 0; } } }puts("No"); return 0; }
#include<iostream> #include<string> #include<vector> #include<utility> #include<algorithm> #include<map> #include<set> #include<cstdlib> #include<cmath> #include<numeric>//fill #include<iomanip> #include<functional>//https://cpprefjp.github.io/reference/functional/function.html #include<cstdlib>//https://cpprefjp.github.io/reference/cstdlib.html #include<queue> #include<deque> #include<cassert> #include<stack> #include <iterator> // std::back_inserter //#include<atcoder/all> using namespace std; //using namespace atcoder; const double PI = acos(-1); using ll = long long; using P = pair<ll,ll>; #define rep(i,n)for(ll i=0;i<(n);i++) #define repp(x,arr) for(auto& x:arr) #define repp2(x,y,arr) for(auto& [x,y]:arr) #define all(a) (a.begin()),(a.end()) const ll MOD = 1e9 + 7; const ll inf = (ll)1e18+6; // デフォルトは整数部も含めた桁数指定 //cout <<setprecision(); ll myPow(ll x, ll n, ll m) { if (n == 0) return 1; if (n % 2 == 0) return myPow(x * x % m, n / 2, m); else return x * myPow(x, n - 1, m) % m; } double dl[4] = { 0,0,0.1,0.1 }; double dr[4] = { 0,-0.1,0,-0.1 }; int main() { ios::sync_with_stdio(false); int N; cin >> N; vector<pair<double, double>>s(N), t(N); map<pair<double, double>,bool>m; rep(i, N) { cin >> t[i].first >> t[i].second; } rep(i, N) { cin >>s[i].first >> s[i].second; m[s[i]] = true; } if (N == 1) { cout << "Yes"; return 0; } double a, b,a2,b2; a = s[0].first, b = s[0].second; a2 = s[1].first, b2 = s[1].second; double p1; p1 = atan((b2 - b) / (a2 - a)); bool ans = false; //cout << "指標は" << a << " " << b << endl; rep(i, N) { double q, r,c, d; c = t[i].first, d = t[i].second; for (int j = 0; j < N; j++) { if(j==i)continue; double c2, d2,p,p2; c2 = t[j].first; d2 = t[j].second; c2 -= c; d2 -= d; p = p1-atan(d2 / c2); p2 = p + PI; { double A1, B1, x, y, x2, y2; A1 = t[j].first - c; B1 = t[j].second - d; x = A1 * cos(p) - B1 * sin(p); y = A1 * sin(p) + B1 * cos(p); x2 = A1 * cos(p2) - B1 * sin(p2); y2 = A1 * sin(p2) + B1 * cos(p2); x += a; y += b; x = round(x); y = round(y); x2 += a; y2 += b; x2 = round(x2); y2 = round(y2); if (x==a2&&y==b2) { p = p; } else if (x2 == a2 && y2 == b2) { p = p2; } else { continue; } } rep(k, N) { double A1, B1,x,y,x2,y2; A1 = t[k].first - c; B1 = t[k].second - d; x = A1 * cos(p) - B1 * sin(p); y = A1 * sin(p) + B1 * cos(p); //x2 = A1 * cos(p2) - B1 * sin(p2); y2 = A1 * sin(p2) + B1 * cos(p2); //cout << x << " " << y << endl; //cout << x2 << " " << y2 << endl; x += a; y+= b; x = round(x); y = round(y); if (m[{x,y}] == false) { //cout << "break"<<endl; break; } if (k == N - 1)ans = true; } } } if (ans)cout << "Yes"; else cout << "No"; }
#include<bits/stdc++.h> using namespace std; const int maxn=2e5; const int maxq=3e5; char ss[maxn + 1 << 1]; int main() { int n,q,t,a,b; scanf("%d\n%s\n%d\n",&n,ss,&q); int base=0; int l=n<<1; while(q-->0) { scanf("%d %d %d",&t,&a,&b); if(t==2) { base^=n; } else {// T==1 a=(a-1+base)%l; b=(b-1+base)%l; swap(ss[a],ss[b]); } } for(int i=0;i<l;i++) { putchar(ss[(base+i)%l]); } }
#include <bits/stdc++.h> #define rep(i, n) for (auto i = 0; i < n; ++i) #define ALL(a) a.begin(), a.end() using namespace std; using ll = long long int; const int MOD_NUM = 1e9 + 7; int main() { // Input int N, Q; string S; cin >> N >> S >> Q; vector<int> T(Q), A(Q), B(Q); rep(i, Q) cin >> T[i] >> A[i] >> B[i]; // Process string ans(S); int reverse = 0; rep(i, Q) { if (T[i] == 1) { int a = A[i] - 1; int b = B[i] - 1; if (reverse % 2 == 1) { a = (a + N) % (N * 2); b = (b + N) % (N * 2); } char tmp = ans[a]; ans[a] = ans[b]; ans[b] = tmp; } else reverse++; } if (reverse % 2 == 1) { string tmp1 = ans.substr(N); string tmp2 = ans.substr(0, N); ans = tmp1 + tmp2; } // Output cout << ans << endl; return 0; }
///Astalavista Code........~sachinait #include <bits/stdc++.h> using namespace std; #define endl "\n" #define Br cout<<endl #define int long long #define pb push_back #define mp make_pair #define double long double #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define PT(x) cout << (x) << endl #define PTT(x) cout << (#x) << " is " << (x) << endl; #define debug(x) cout<<"~~"<<#x<<" => "<<x<<endl; #define lets_go ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; const int M = 1e9 + 7; const int N = 2e6 + 10; const int IM = 1e15 + 37; const double PI = 3.1415926535897932384; inline void PP(int n) { cout << n << " ";} void IN(vector<int>&arr,int n,int x=0){for(int i=x;i<n+x;i++)cin>>arr[i];} void PV(vi v) { for (int i = 0 ; i < v.size() ; i++) cout << v[i] << " " ; cout << endl;} void PVR(vi v) { for (int i = v.size()-1 ; i >=0 ; i--) cout << v[i] << " " ; cout << endl;} void PVV(vii v) {for (int i = 0 ; i < v.size() ; i++) cout << v[i].first << " " << v[i].second << endl;} void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] << " " ; cout << endl;} void IN(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];} // <================================================================================================================================= // <================================================================================================================================= void fun(){ int h,w;cin>>h>>w; vector<string>a(h); for(int i=0;i<h;i++) { cin>>a[i]; } int ans=0; for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { if(j+1<w && a[i][j]=='.' && a[i][j+1]=='.') { ans++; } if(i+1<h && a[i][j]=='.' && a[i+1][j]=='.') { ans++; } } } cout<<ans; } signed main() { lets_go int t; t=1; // cin >> t; while (t--) { fun(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int maxm = 20 + 10; int n, m, ans; char s[maxm]; int a[2]; int main() { cin >> n >> m; for(int i = 1; i <= n; ++i) { scanf("%s", s); int ret = 0; for(int j = 0; j < m; ++j) ret = (ret << 1) + s[j] - '0'; ++a[__builtin_popcount(ret) & 1]; } printf("%lld\n", 1ll * a[0] * a[1]); return 0; }
#include "bits/stdc++.h" using namespace std; const int N=2e3+20,MOD=1e9+7; int n,m,r[N][N],c[N][N],d[N][N],dp[N][N]; char a[N][N]; void add(int &x,int y) {x=(x+y%MOD)%MOD;} int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf(" %s",a[i]+1); dp[1][1]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(a[i][j]=='#') continue; add(dp[i][j],r[i][j]); add(dp[i][j],c[i][j]); add(dp[i][j],d[i][j]); add(r[i][j+1],dp[i][j]+r[i][j]); add(c[i+1][j],dp[i][j]+c[i][j]); add(d[i+1][j+1],dp[i][j]+d[i][j]); } } printf("%d",dp[n][m]); }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") // Use g++ compiler for this include to work. #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; // Constants #define INF 2e18 #define EPS 1e-9 #define MOD 1000000007 // // for using in pairs #define F first #define S second // Macros #define all(x) begin(x), end(x) #define allr(x) rbegin(x), rend(x) // aliases using ll = long long; using ull = unsigned long long; using ui = unsigned int; using ld = long double; template<typename T> using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; // Temporary macro. #define int long long // Operator overloads. /** * Both types in the pair should have overload for << operator with ostream. */ template<typename T1, typename T2> ostream& operator<<(ostream &ostream, pair<T1, T2>& p) { ostream << p.first << " " << p.second; return ostream; } /** * The type of element in the vector should have overload for << operator with ostream. */ template<typename T> ostream& operator<<(ostream &stream, vector<T>& v) { for (const auto &it : v) stream << it << " "; return stream; } template<typename T1, typename T2> istream& operator>>(istream &istream, pair<T1, T2>& p) { istream >> p.first >> p.second; return istream; } template<typename T> istream& operator>>(istream &istream, vector<T>& a) { for (auto &it : a) istream >> it; return istream; } // Utility functions. template<typename T> T MIN(T first) { return first; } template<typename T, typename... Args> T MIN(T first, Args... args) { return min(first, MIN(args...)); } template<typename T> T MAX(T first) { return first; } template<typename T, typename... Args> T MAX(T first, Args... args) { return max(first, MAX(args...)); } template <typename T> void DEBUG(T&& t) {cout << t << "\n";} template <typename T, typename... Args> void DEBUG(T&& t, Args&&... args) { cout << t << " "; DEBUG(forward<Args>(args)...); } template<typename T> int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); } // Mathematical functions. int GCD(int a, int b) { if (!b) return a; return GCD(b, a % b); } int LCM(int a, int b) { return (a * b) / GCD(a, b); } int modpow(int x, int n, int m = INF) { if (n == 0) return 1 % m; long long u = modpow(x, n / 2, m); u = (u * u) % m; if (n % 2 == 1) u = (u * x) % m; return u; } /** * m should be prime */ int modinv(int x, int m = MOD) { return modpow(x, m - 2, m); } //////////////////// START CODE HERE /////////////////////////// void preSolve() { } struct comp { int l, t, lt, val; comp() { l = 0; t = 0; lt = 0; val = 0; } comp(int l, int t, int lt, int val) : l(l), t(t), lt(lt), val(val) {} int sum() { return l + lt + t; } }; void solve(int tc) { int n, m; cin >> n >> m; char a[n + 1][m + 1]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; comp dp[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (!i || !j) continue; if (a[i][j] == '#') { dp[i][j] = comp(); continue; } if (i == 1 && j == 1) { dp[1][1] = comp(0, 0, 0, 1); continue; } dp[i][j] = comp(); dp[i][j].l += (dp[i][j - 1].val + dp[i][j - 1].l) % MOD; dp[i][j].t += (dp[i - 1][j].val + dp[i - 1][j].t) % MOD; dp[i][j].lt += (dp[i - 1][j - 1].val + dp[i - 1][j - 1].lt) % MOD; dp[i][j].val = (dp[i][j].sum()) % MOD; } } cout << dp[n][m].val; } int32_t main() { #ifdef LOCAL_PROJECT freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); preSolve(); int t = 1; // cin >> t; for (int i = 1; i <= t; i++) { solve(i); } return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int (i) = 0; (i) < (n); (i)++) #define repp(i, n, m) for(int (i) = (n); (i) < (m); (i)++) #define repn(i, n) for(int (i) = 1; (i) <= (n); (i)++) #define repr(i, n) for(int (i) = (n-1); (i) >= 0; (i)--) #define all(x) (x).begin(), (x).end() #define lint long long #define ulint unsigned long long #define ldou long double #define fi first #define se second #define setpre(x) std::cout << fixed << setprecision(x) #define ii(x) int x; cin >> (x) #define ii2(x, y) int x, y; cin >> (x) >> (y) #define ii3(x, y, z) int x, y, z; cin >> (x) >> (y) >> (z) #define out(x) cout << (x) << endl #define outs(x) cout << (x) << " " #define yn(x) cout << ((x)?("Yes"):("No")) << endl #define YN(x) cout << ((x)?("YES"):("NO")) << endl #define bit_c(x) __builtin_popcountll(x) inline void logger(){ std::cout << " [LOGGER] " << endl; } template<typename A, typename... B> void logger(const A& a, const B&... b){ cout << a << " , "; logger(b...); } typedef pair<lint, lint> P; const lint MOD = 1000000007; const lint MOD9 = 998244353; const lint INF = MOD * MOD; const int MAX = 200005; /* ...o(^-^)o... */ const lint mod = MOD9; class mint{ public: long long x; constexpr mint(long long _x = 0) : x((_x%mod+mod)%mod) {} constexpr mint operator-() const{ return mint(-x); } constexpr mint& operator+=(const mint& a){ if((x += a.x) >= mod) x -= mod; return *this; } constexpr mint& operator-=(const mint& a){ if((x += mod-a.x) >= mod) x -= mod; return *this; } constexpr mint& operator*=(const mint& a){ (x *= a.x) %= mod; return *this; } constexpr mint operator+(const mint& a) const{ mint res(*this); return res+=a; } constexpr mint operator-(const mint& a) const{ mint res(*this); return res-=a; } constexpr mint operator*(const mint& a) const{ mint res(*this); return res*=a; } constexpr mint pow(long long x) const{ if(!x) return 1; mint a = pow(x>>1); a *= a; if(x&1) a *= *this; return a; } constexpr mint inv() const{ return pow(mod-2); } constexpr mint& operator/=(const mint& a){ return (*this) *= a.inv(); } constexpr mint operator/(const mint& a) const{ mint res(*this); return res/=a; } friend istream& operator>>(istream& is, mint& m){ long long x; is >> x; m = x; return is; } friend ostream& operator<<(ostream& os, const mint& m){ os << m.x; return os; } }; mint fac[MAX], facinv[MAX], inv[MAX]; void minit(){ fac[0] = fac[1] = 1; facinv[0] = facinv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i; inv[i] = -mint(mod / i)*inv[mod % i]; facinv[i] = facinv[i - 1] * inv[i]; } } mint mcomb(lint a, lint b){ if (a < b) return 0; if (a < 0 || b < 0) return 0; return fac[a] * facinv[b] * facinv[a - b]; } mint mperm(lint a, lint b){ if (a < b) return 0; if (a < 0 || b < 0) return 0; return fac[a] * facinv[a - b]; } int main(){ lint N,K; cin>>N>>K; vector<vector<mint> > a(N, vector<mint>(K+1)); rep(i, N) cin >> a[i][1]; rep(i, N) rep(k, K+1){ if(k==0) a[i][k] = 1; else if(k==1) continue; else a[i][k] = a[i][k-1] * a[i][1]; } minit(); vector<mint> b(K+1, 0); rep(k, K+1){ rep(i, N) b[k] += a[i][k]; b[k] /= fac[k]; } mint d = 2; repn(x, K){ mint c(0), ans(0); rep(i, N) c += d * a[i][x]; d *= 2; rep(k, x+1) ans += b[k] * b[x-k]; ans = (ans * fac[x] - c) / mint(2); out(ans); } }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={1,0,-1,0,1,1,-1,-1}; const ll dx[8]={0,1,0,-1,1,-1,1,-1}; template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } int main(){ ll n,l;cin >> n >> l;l++; ll left=0; vl a(n+1),b(n+1); rep(i,n){ ll t;cin >> t; a[i]=t-left-1;left=t; } a[n]=l-left-1; left=0; rep(i,n){ ll t;cin >> t; b[i]=t-left-1;left=t; } b[n]=l-left-1; left=0; //rep(i,n+1)cout << a[i] <<" ";cout << endl; //rep(i,n+1)cout << b[i] <<" ";cout << endl; ll ans=0; ll start=0; rep(i,n+1){ if(b[i]==0)continue; ll sum=0; ll left=-1,right=-1; for(int j=start;j<=n;j++){ if(a[j]){ if(left==-1)left=j; sum+=a[j]; if(sum>b[i])break; if(sum==b[i]){ right=j;break; } } } //cout << left <<" " << right <<endl; if(right==-1)cout << -1 <<endl,exit(0); if(left<=i&&i<=right)ans+=right-left; else if(i<left){ ans+=right-i; } else{ ans+=i-left; } start=right+1; } cout << ans <<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<long long> Vll; ll mm=1000000000;ll MM=mm+7; #define rep(i, n) for(int i=0;i<n;i++) #define PI 3.141592653589793 int main(){ ll n,m; cin >> n >>m; Vll a(m); rep(i,m)cin >> a[i]; sort(a.begin(),a.end()); if(m==0){ cout << 1 << endl; } else if(n==m)cout << 0 << endl; else{ ll stmp=MM; Vll d; if((a[0]-1)!=0){ stmp=min(stmp,a[0]-1); d.push_back(a[0]-1); } for(int i=1;i<m;i++){ if((a[i]-a[i-1]-1)!=0){ stmp=min(stmp,(a[i]-a[i-1]-1)); d.push_back(a[i]-a[i-1]-1); } } if(n-a[m-1]!=0){ stmp=min(stmp,n-a[m-1]); d.push_back(n-a[m-1]); } ll num=d.size(); ll ans=0; rep(i,num){ if(d[i]%stmp==0){ ans+=d[i]/stmp; } else{ ans+=(d[i]/stmp)+1; } } cout << ans << endl; } }
#include <bits/stdc++.h> //#pragma GCC optimize("O2") using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define F first #define S second #define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ll long long #define ld long double #define pii pair <long long ,long long > #define bug(x) cerr<<"passed "<<x<<"\n"; #define debug(x) cerr<<"---> "<<x<<"\n" ; #define kill(x) cout<<x<<"\n" ; return 0 ; #define ppi pair <pair <int, long long > , long long > #define ashar(x , y , z) cout<<setprecision(x)<<fixed<<y ; if (z==1) cout<<"\n" ; #define ordered_set tree<long long , null_type,less<long long , rb_tree_tag,tree_order_statistics_node_update> long long const inf = 3e18, linf = 2e9, mod = 1e9+7 , inf2 = 1e12; long long const mxn =4e5+10 ; ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } int main(){ long long n , m ; cin>>n>>m ; if (m==0){ cout<<1 ; return 0 ; } vector <int > vec ; for (int i = 1 ; i <= m ; i++){ int a ; cin>>a ; vec.push_back(a) ; } vec.push_back(n+1) ; sort(vec.begin(), vec.end()) ; int p = 0 ; int kk = linf; vector <int > vec2 ; for (auto u : vec){ if(u-p-1!=0) kk=min(kk , u-p-1) ; vec2.push_back(u-p-1) ; p = u ; } long long ans = 0 ; // cout<<vec2.size(); for (auto u : vec2){ /// cout<<u<<" " ; ans+= u/kk + ((u%kk)!=0) ; } cout<<ans ; }
//{{{ #include <bits/stdc++.h> using namespace std; using LL = long long; using VLL = vector<LL>; using vi = vector<int>; using pii = pair<int, int>; #define sz(x) (int)((x).size()) #define all(x) (x).begin(), (x).end() #define clr(a, b) memset(a, b, sizeof(a)) #ifdef LOCAL #include "prettyprint.hpp" #endif // clang-format off void _print() { cerr << "]\033[0m\n"; } template <typename T> void __print(T x) { cerr << x; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef LOCAL #define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x) #define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n" #else #define debug(x...) #define debug_arr(x...) #endif // clang-format on //}}} const int N = 2e2 + 10; LL n; int main() { #ifdef LOCAL freopen("in", "r", stdin); // freopen("out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); while (cin >> n) { LL ans = 0; for (LL i = 1; i * i <= n; i++) { if (n % i == 0) { if (i % 2 == 1) ans++; if (i * i != n && (n / i) % 2 == 1) { ans++; } } } ans *= 2; cout << ans << endl; } return 0; }
#include <iostream> #include <string> using namespace std; #include <algorithm> #include <vector> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <iomanip> #include <set> #include <map> #include <cassert> #define MOD1 1000000007 #define MOD2 998244353 #define LIMIT1 200010 #define LIMIT2 500010 #define LIMIT3 1000010 #define INF ((1<<30)-1) #define LLINF (1LL<<60) #define EPS (1e-10) typedef long long ll; typedef long double ld; typedef const void cv; typedef pair<ll,ll> P; #define rep(i,n) for((i)=0;(i)<(n);(i)++) #define per(i,n) for((i)=(n)-1;(i)>=0;(i)--) #define ALL(a) (a).begin(),(a).end() #define ALL2(a, n) (a),(a)+(n) template<class T,class C> T max(T a,C b){ return std::max(a, (T)b); } template<class T,class C> T min(T a,C b){ return std::min(a, (T)b); } #define zt(a,b) (max((a),(b))-min((a),(b))) #define setpre(n) fixed << setprecision(n) ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return (a/gcd(a,b))*b;} int dx[8]={1,0,-1,0,1,-1,-1,1},dy[8]={0,1,0,-1,1,1,-1,-1}; ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1){ res *= a; if(mod>=1) res %= mod; } a *= a; if(mod>=1) a %= mod; n >>= 1; } return res; } void initialize(){ } int main(void){ initialize(); ll n,m,i,j,k,result=0; int a[LIMIT2]={0}; string s; cin >> n; for(i=1;i*(i+1)/2<=n;i++){ ll tmp = i*(i+1)/2; if(abs(tmp-n)%i==0) result+=2; } cout << result << endl; return 0; }
#include<iostream> #include<array> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> #include<utility> #include<tuple> #include<cassert> using namespace std; typedef long long ll; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define Per(i,sta,n) for(int i=n-1;i>=sta;i--) typedef long double ld; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;} template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;} int n,m,k; int a[210],b[210],c[20],d[20]; void solve(){ cin >> n >> m; rep(i,m){ cin >> a[i] >> b[i];a[i]--;b[i]--; } cin >> k; rep(i,k){ cin >> c[i] >> d[i];c[i]--;d[i]--; } int U=((1<<k)-1); int ans=0; rep(S,U+1){ vector<bool> v(n,false); rep(i,k){ if(S&(1<<i))v[c[i]]=true; else v[d[i]]=true; } int res=0; rep(i,m){ if(v[a[i]] && v[b[i]]) res++; } chmax(ans,res); } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 1e9 #define linf 1e18 #define BASE 1000000 #define EPS 1e-10 #define PI acos(-1) #define pii pair<int,int> #define pdi pair<double,int> #define vi vector<int> #define fi first #define se second #define ALL(x) (x).begin(), (x).end() #define ms(x,val_add) memset(x, val_add, sizeof(x)) #define pb(x) push_back(x) #define make_unique(x) sort(ALL(x)) ; x.erase( unique(ALL(x)), x.end()) ; #define dbg(x) do { cout << #x << " = " << x << endl; } while(0) #define mp(x, y) make_pair(x, y) #define minimize(x, y) x = min(x, y) #define maximize(x, y) x = max(x, y) /*** IMPLEMENTATION ***/ bool exitInput = false; int ntest = 1, itest = 1 ; const int dx[4] = { -1, 0, 1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; const ll Mod = 1000000007LL; const int maxn = 10000 + 5; const int maxv = 500 + 5; const int maxe = 10000 + 5; int n, a[maxn]; int main() { #ifdef HOME freopen( "input.txt", "r", stdin ); freopen( "output.txt", "w", stdout ); #endif // HOME ios_base::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(10); cout << fixed; cin >> n; for(int i = 1; i <= n; ++i) cin >> a[i]; int res = 0; for(int i = 1; i <= n; ++i) { int m = a[i]; res = max(res, a[i]); for(int j = i+1; j <= n; ++j) { m = min(m, a[j]); res = max(res, (j - i + 1) * m); } } cout << res; return 0; }
//مبتسمون رغم الخرا #include <bits/stdc++.h> #define int long long int #define ZaZo ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define ceil1(a,b) (a+(b-1))/b #define all(v) v.begin(),v.end() using namespace std; int32_t main() { ZaZo double a,b; cin>>a>>b; cout<<(double)(((a-b)/a)*(double)100); }
#include <bits/stdc++.h> #define ll long long int using namespace std; ll f=0; ll pre[500]; ll nCr(int n, int r) { ll p = 1, k = 1; // C(n, r) == C(n, n-r), // choosing the smaller value if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; // gcd of p, k long long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } int main() { ll dp[200]; dp[0] =1; ll n; cin>>n; for(ll i=1;i<200;i++) { dp[i] = dp[i-1]*11; // cout<<dp[i]<<endl; } // n-=12; ll ans =0; n--; cout<<nCr(n,11)<<endl; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; void*wmem; char memarr[96000000]; template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){ sort(a, a+N); } 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(int &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; } } 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 S, class T> inline S chmax(S &a, T b){ if(a<b){ a=b; } return a; } int N; long long T; long long A[40]; int n1; int n2; long long a1[2000000]; long long a2[2000000]; int main(){ int mask; wmem = memarr; int i; int j; long long res = 0; rd(N); rd(T); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } n1 = N / 2; n2 = N - n1; for(mask=(0);mask<(1<<n1);mask++){ for(i=(0);i<(n1);i++){ if(((mask) &(1<<(i)))){ a1[mask] += A[i]; } } } for(mask=(0);mask<(1<<n2);mask++){ for(i=(0);i<(n2);i++){ if(((mask) &(1<<(i)))){ a2[mask] += A[i+n1]; } } } n1 = (1<<n1); n2 = (1<<n2); sortA_L(n1, a1); sortA_L(n2, a2); j = n2 - 1; for(i=(0);i<(n1);i++){ while(j >= 0 && a1[i] + a2[j] > T){ j--; } if(j == -1){ break; } chmax(res, a1[i] + a2[j]); } wt_L(res); wt_L('\n'); return 0; } // cLay version 20201121-1 // --- original code --- // int N; ll T, A[40]; // int n1, n2; // ll a1[2d6], a2[2d6]; // { // int i, j; // ll res = 0; // rd(N,T,A(N)); // n1 = N / 2; // n2 = N - n1; // rep(mask,1<<n1) rep(i,n1) if(BIT_ith(mask,i)) a1[mask] += A[i]; // rep(mask,1<<n2) rep(i,n2) if(BIT_ith(mask,i)) a2[mask] += A[i+n1]; // n1 = (1<<n1); // n2 = (1<<n2); // sortA(n1, a1); // sortA(n2, a2); // j = n2 - 1; // rep(i,n1){ // while(j >= 0 && a1[i] + a2[j] > T) j--; // if(j == -1) break; // res >?= a1[i] + a2[j]; // } // wt(res); // }
#pragma GCC optimize("Ofast","inline","-ffast-math") #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define MOD 998244353 using namespace __gnu_pbds; using namespace std; 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); } }; int h,w,k,dp[5010][5010],val[5010][5010],sum[5010][5010],pw[5010]; inline void add(int &x,int y) { x+=y; if(x>=MOD) x-=MOD; } signed main() { ios::sync_with_stdio(false); cin>>h>>w>>k; pw[0]=1; for(int i=1;i<=5000;i++){ pw[i]=(1ll*pw[i-1]*3%MOD); } for(int i=1;i<=k;i++){ int x,y;char c;cin>>x>>y>>c; if(c=='X') val[x][y]=1; if(c=='R') val[x][y]=2; if(c=='D') val[x][y]=3; } for(int i=1;i<=h;i++){ sum[i][0]=0; for(int j=1;j<=w;j++) sum[i][j]=sum[i][j-1]+(val[i][j]==0); } for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ if(i==1&&j==1){ dp[i][j]=1;continue; } dp[i][j]=0; if(i>1){ int num=sum[i-1][w]-sum[i-1][j]+sum[i][j-1]; if(val[i-1][j]==0) add(dp[i][j],1ll*dp[i-1][j]*2%MOD*pw[num]%MOD); if(val[i-1][j]==1) add(dp[i][j],1ll*dp[i-1][j]*pw[num]%MOD); if(val[i-1][j]==3) add(dp[i][j],1ll*dp[i-1][j]*pw[num]%MOD); } if(j>1){ if(val[i][j-1]==0) add(dp[i][j],1ll*dp[i][j-1]*2%MOD); if(val[i][j-1]==1) add(dp[i][j],dp[i][j-1]); if(val[i][j-1]==2) add(dp[i][j],dp[i][j-1]); } } } if(val[h][w]==0) add(dp[h][w],1ll*dp[h][w]*2%MOD); cout<<dp[h][w]<<endl; return 0; }
/* Jai Shree Krishna !! Radhe Radhe !! उद्यमेन हि सिद्ध्यन्ति कार्याणि न मनोरथैः। नहि सुप्तस्य सिंहस्य मुखे प्रविशन्ति मृगाः।। */ #include <bits/stdc++.h> using namespace std; #define int long long typedef long long ll; #define loop(i,n) for(int i=0;i<n;i++) #define read(a) int a; cin >>a; #define readarr(a,n) int a[n] ; for(int i=0;i<n;i++) cin >> a[i]; #define readmat(a,n,m) int a[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[i][j]; #define pb push_back #define ff first #define ss second #define vi vector<int> #define pi pair<int,int> #define vvi vector<vector<int>> #define sti set<int> #define vpi vector<pair<int,int>> #define print(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; #define printmat(a,n,m) for(int i=0;i<n;i++) {for(int j=0;j<m;j++) cout<<a[i][j]<<" "; cout<<endl;} #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); #define endl "\n" #define mp make_pair #define ins insert #define rep(i,s,e) for(int i=s;i<=e;i++) #define repd(i,s,e) for(int i=e;i>=s;i--) #define vb vector<bool> #define read2(n,m) int n,m; cin >> n >> m; #define range(v) v.begin(),v.end() #define sortd(v) sort(v.begin(),v.end(),greater<int>()) #define sorti(v) sort(v.begin(),v.end()) const ll inf = 1e18; const ll mod = 1e9 + 7; signed main() { fast; read(n); int x = sqrt(n); int ans=0; rep(i,1,x){ if(n%i==0 && i%2==1) ans+=2; int k = n/i; if(k!=i && n%k==0 && k%2==1) ans+=2; } cout<<ans<<endl; return 0; } /* Pooja Yadav */
#include <algorithm> #include <iostream> #include <iterator> #include <list> #include <memory> #include <vector> using namespace std; struct ConnectedComponent { long long sum; list<int> vertices; }; int main() { int vertex_cnt; int edge_cnt; cin >> vertex_cnt >> edge_cnt; vector<int> diffs; diffs.reserve(vertex_cnt); for (int i = 0; i < vertex_cnt; ++i) { int a; cin >> a; diffs.push_back(a); } for (auto iter = diffs.begin(); iter != diffs.end(); ++iter) { int b; cin >> b; *iter -= b; } vector<shared_ptr<ConnectedComponent>> conn_comps; conn_comps.reserve(vertex_cnt); for (int i = 0; i < vertex_cnt; ++i) { shared_ptr<ConnectedComponent> comp(new ConnectedComponent); comp->sum = diffs[i]; comp->vertices.push_back(i); conn_comps.push_back(comp); } for (int i = 0; i < edge_cnt; ++i) { int c, d; cin >> c >> d; --c; --d; auto comp_p = conn_comps[c]; auto comp_q = conn_comps[d]; if (comp_p->vertices.size() < comp_q->vertices.size()) { comp_p = conn_comps[d]; comp_q = conn_comps[c]; } if (comp_p.get() != comp_q.get()) { for (auto vertex : comp_q->vertices) { conn_comps[vertex] = comp_p; } comp_p->sum += comp_q->sum; comp_p->vertices.splice(comp_p->vertices.end(), comp_q->vertices); } } for (auto comp : conn_comps) { if (comp->sum != 0) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; } /* [-905371741, -999219903, 969314057, -989982132, -87720225, -175700172, -993990465, 929461728, 895449935, -999016241, 782467448, -906404298, 578539175, 9684413, -619191091, -952046546, 125053320] [-440503430, -997661446, -912471383, -995879434, 932992245, -928388880, -616761933, 929461728, 210953513, -994677396, 648190629, -530944122, 578539175, 9684413, 595786809, -952046546, 125053320] 2 10 6 12 9 11 11 5 7 6 3 15 3 1 1 9 10 4 diffs: [-464868311, -1558457, 1881785440, 5897302, -1020712470, 752688708, -377228532, 0, 684496422, -4338845, 134276819, -375460176, 0, 0, -1214977900, 0, 0] Connected Components: 1 3 5 9 11 15 2 4 10 6 7 12 8 */
#include <iostream> using namespace std; int main() { int x,y; cin >> x >> y; cout <<x/y<<endl; }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define fi first #define se second #define pb push_back #define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k)) #define forn(i,a,b) for(int i = a; i < b; i++) #define forr(i,a,b) for(int i = a; i >= b; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int,int> ii; typedef vector<int> vi; typedef vector<ii> vii; const ll mod = 1e9+7; ll mul(ll a,ll b){return ((a%mod)*(b%mod))%mod;} ll sum(ll a,ll b){return ((a%mod)+(b%mod))%mod;} ll sub(ll a,ll b){return ((a%mod)-(b%mod))%mod;} ll power(ll a,ll b){ ll res = 1; while(b){ if(b&1)res = mul(res,a); b >>= 1; a = mul(a,a); } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N,W; cin >> N >> W; cout << N/W << '\n'; return 0; } /* __builtin_mul_overflow(x,y,&x) -fsplit-stack */
#include <bits/stdc++.h> using namespace std; using ll = long long; template<typename A, typename B = A> using pr = pair<A, B>; #define vec vector #define ar array #define len(a) ((int)((a).size())) #define all(a) (a).begin(), (a).end() int main() { ios::sync_with_stdio(false), cin.tie(nullptr); vec<int> primes; for (int i = 2; i <= 50; i++) { bool prime = true; for (int j = 2; j < i; j++) if (i % j == 0) prime = false; if (prime) primes.push_back(i); } int n; cin >> n; vec<int> a(n); for (auto &x : a) cin >> x; ll ans = 1e18; for (int mask = 0; mask < (1 << len(primes)); mask++) { vec<char> good(51); ll prod = 1; for (int i = 0; i < len(primes); i++) if ((mask >> i) % 2) { prod *= primes[i]; for (int j = primes[i]; j <= 50; j += primes[i]) good[j] = 1; } bool ok = true; for (auto x : a) ok &= good[x]; if (ok) ans = min(ans, prod); } cout << ans << '\n'; }
//#pragma optimization_level 3 //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset; */ #define fr first #define sc second #define vec vector #define pb push_back #define pii pair<int, int> #define forn(x,y) for(int x = 1 ; x <= (int)y ; ++x) #define all(x) (x).begin(),(x).end() #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0); using namespace std; typedef long long ll; typedef unsigned int uint; typedef pair<ll,ll> pll; typedef complex<int> point; const int nmax = 300005; const ll linf = 1e18; const ll mod = 998244353; const int inf = 1e9; const int sq = 5000; int n; char s[nmax]; int a[nmax]; int b[nmax]; int main(){ cin >> n; for(int i = 1; i <= n; i++){ cin >> s[i]; } for(int i = 0; i <= n; i++){ cin >> a[i]; } int k = inf; for(int i = 0; i < n; i++){ k = min(k,abs(a[i+1]-a[i])); } cout << k << '\n'; for(int i = 0 ; i < k; i++){ for(int j = 0; j <= n; j++){ cout << (a[j]/k) + (a[j]%k > i) << ' '; } cout << '\n'; } // a[i] - a[j] >= k*(dist(i,j)) }
#include <bits/stdc++.h> #define ull unsigned long long int #define ll long long int #define vll vector<ll> #define pll pair<long long, long long> #define vpll vector < pair<long long,long long> > #define itr vll::iterator #define all(c) c.begin(),c.end() #define pb push_back #define f first #define s second #define mod 1000000007 #define inf 1000000000000000001 #define rep(i, n) for(ll i = 0; i < n; ++i) #define repA(i, a, n) for(ll i = a; i <= n; ++i) #define repD(i, a, n) for(ll i = a; i >= n; --i) using namespace std; bool func(const pll &a, const pll &b){ return a.s < b.s; } ll power(ll x, ll y) { if (y == 0) return 1; else if (y % 2 == 0) return ((power(x, y / 2)%mod) * (power(x, y / 2)%mod)%mod); else return (x * ((power(x, y / 2)%mod) * (power(x, y / 2)%mod)%mod))%mod; } ll modInverse(ll a, ll m){ ll m0 = m; ll y = 0, x = 1; if (m == 1) return 0; while (a > 1){ ll q = a / m; ll t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void solve(int t){ ll n,k, m; cin >> n >> k >> m; ll sm = 0; rep(i, n-1){ ll tmp; cin >> tmp; sm += tmp; } ll er = n*m-sm; if(er >= 0 && er <= k) cout << er; else if(er < 0) cout << 0; else cout << -1; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, tmp = 0; // cin >> t; t = 1; while(tmp++ < t){ // cout << "Case #" << tmp << ": "; solve(tmp-1); if(t-tmp) cout << "\n"; } return 0; }
#include<bits/stdc++.h> #include<cmath> #define pb push_back #define ld long double #define mp make_pair #define vl vector<ll> #define vd vector<double> #define vld vector<long double> #define ll long long int #define pl pair<ll, ll> #define all(a) a.begin(), a.end() #define forr(i, n) for(ll i=0; i<n; i++) #define forr1(i, n) for(ll i=1; i<=n; i++) using namespace std; const ld PI =3.1415926535897923846; const ll MOD = 1000000007; const ll N=998244353; ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;} ll modinverse(ll a){return power(a, MOD-2);} void solve() { vl a(4); cin>>a[0]>>a[1]>>a[2]>>a[3]; sort(all(a)); if(a[3]==a[0]+a[1]+a[2]||a[3]+a[0]==a[1]+a[2]) cout<<"Yes"; else cout<<"No"; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll test=1; //cin>>test; while(test--) { solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include<bits/stdc++.h> typedef uint64_t u64; typedef int64_t i64; typedef long double f128; using namespace std; template<u64 mod> struct modint{ u64 val; constexpr modint(const i64 x=0) noexcept:val((x%i64(mod)+i64(mod))%i64(mod)){} constexpr u64 &value() noexcept{ return val; } constexpr modint operator+(const modint rhs) const noexcept{ return modint(*this)+=rhs; } constexpr modint operator-(const modint rhs) const noexcept{ return modint(*this)-=rhs; } constexpr modint operator*(const modint rhs) const noexcept{ return modint(*this)*=rhs; } constexpr modint operator/(const modint rhs) const noexcept{ return modint(*this)/=rhs; } constexpr modint &operator+=(const modint rhs) noexcept{ val+=rhs.val; val-=((val>=mod)?mod:0); return *this; } constexpr modint &operator-=(const modint rhs) noexcept{ val+=((val<rhs.val)?mod:0); val-=rhs.val; return *this; } constexpr modint &operator*=(const modint rhs) noexcept{ val=val*rhs.val%mod; return *this; } constexpr modint &operator/=(modint rhs) noexcept{ u64 exp=mod-2; while(exp){ if(exp&1){ (*this)*=rhs; } rhs*=rhs; exp>>=1; } return *this; } friend constexpr ostream &operator<<(ostream &os,const modint &x) noexcept{ return os<<(x.val); } friend constexpr istream &operator>>(istream &is,modint &x) noexcept{ u64 t; is>>t; x=t; return is; } }; template<typename T> void scan(T& n){ cin>>n; } void scan(){} template<typename T,class... Args> void scan(T& n,Args&... args){ scan(n); scan(args...); } template<typename T> void scanall(T start,T end){ for(;start!=end;++start){ scan(*start); } } template<typename T> void print(T n){ cout<<n; } void print(){} template<typename T,class... Args> void print(T n,Args... args){ print(n); print(args...); } template<typename T> void println(T n){ print(n); cout<<endl; } template<typename T,class... Args> void println(T n,Args... args){ print(n,' '); println(args...); } template<typename T> void printall(T start,T end){ if(start!=end){ print(*start); for(++start;start!=end;++start){ print(' ',*start); } } cout<<endl; } template<typename T> void chmax(T& n,T m){ n=max(n,m); } template<typename T> void chmin(T& n,T m){ n=min(n,m); } template<typename T,typename U> T power(T a,U n){ T res=1; while(n){ res*=(n&1)?a:1; a*=a; n>>=1; } return res; } template<typename T> struct combination{ vector<T> fact; combination(const int Max=3000000):fact(Max+1,1){ for(int i=2;i<=Max;++i){ fact[i]=fact[i-1]*i; } } template<typename U> T nCk(U n,U k){ if(n<k||n<0||k<0){ return 0; } return fact[n]/fact[k]/fact[n-k]; } }; void solve(); int main(){ cout<<fixed<<setprecision(15); bool is_multicase=true; int T=1; if(is_multicase){ scan(T); } for(int i=0;i<T;++i){ solve(); } return 0; } template<typename T> inline T f(T n){ return n*(n+1)/2; } void solve(){ typedef modint<1000000007> mint; mint N,A,B; scan(N,A,B); if(A.val+B.val>N.val){ println(0); return; } if(A.val<B.val){ swap(A,B); } mint ans=0; ans+=(N-A+1)*(A-B+1)*f(N-A-B+1)*4; ans+=f(N-A-B+1)*f(N-A)*2*4; ans-=f(N-A-B+1)*f(N-A-B+1)*4; println(ans); }
#include<iostream> #include<iomanip> #include<cmath> #include<string> #include<cstring> #include<vector> #include<list> #include<algorithm> #include<map> #include<set> #include<queue> #include<stack> using namespace std; typedef long long ll; #define fi first #define se second #define mp make_pair #define mt make_tuple #define pqueue priority_queue const int inf=1e9+7; const ll mod=1e9+7; const ll mod1=998244353; const ll big=1e18; const double PI=2*asin(1); int main() { int T; cin>>T; ll N, A, B; ll ans[T]; ll tmp; ll zero = 0; for(int i=0;i<T;++i) { cin>>N>>A>>B; ans[i] = 0; if(N<A+B) continue; if(A<B) swap(A, B); tmp = (N-A-B+1)*(N-A-B+2)/2%mod; ans[i] += 4*tmp%mod*((N-A+1)*(N-B+1)%mod-tmp+mod)%mod; } for(int i=0;i<T;++i) cout<<ans[i]<<endl; }
#include <iostream> #include <algorithm> using namespace std; long long md = 1000000007; long long fp(long long n, long long k) { long long s = 1; while (k) { if (k & 1) s = (s * n) % md; k /= 2; n = (n * n) % md; } return s; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; int s = n; for (int i = 0; i < n; i++) { int a; cin >> a; s += a; } int p = n + m; long long ns = 1, rs = 1; for (int i = 0; i < s; i++) { ns = (ns * (p - i)) % md; rs = (rs * (i + 1)) % md; } rs = fp(rs, md - 2); cout << (ns * rs) % md << '\n'; return 0; }
// Problem: B - 200th ABC-200 // Contest: AtCoder - KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) // URL: https://atcoder.jp/contests/abc200/tasks/abc200_b // Memory Limit: 1024 MB // Time Limit: 2000 ms // Author: abhidot #include <bits/stdc++.h> #define int long long #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #define pb push_back #define mod 1000000007 //998244353 #define lld long double #define pii pair<int, int> #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=x; i<y; i++) #define fill(a,b) memset(a, b, sizeof(a)) #define vi vector<int> #define setbits(x) __builtin_popcountll(x) #define w(x) int x; cin>>x; while(x--) using namespace std; const long long N=200005, INF=2000000000000000000; /*------------- Modular exponentiation -------------*/ int power(int a, int b, int p) { if(a==0) return 0; int res=1; a%=p; while(b>0) { if(b&1) res=(res*a)%p; b>>=1; a=(a*a)%p; } return res; } /*------------- Sieve --------------*/ vector <int> prime; bool is_composite[N]; void sieve (int n) { fill (is_composite, false); for (int i = 2; i < n; ++i) { if (!is_composite[i]) prime.push_back (i); for (int j = 0; j < prime.size () && i * prime[j] < n; ++j) { is_composite[i * prime[j]] = true; if (i % prime[j] == 0) break; } } } /*------------ Utitlity function ---------------*/ void print(bool n){ if(n){ cout<<"YES"; }else{ cout<<"NO"; } } int32_t main() { IOS; int n,k; cin>>n>>k; int cnt=0; while(k&&n%200==0){ n/=200; k--; } for(int i=0;i<k-1;i+=2){ n*=10; n+=2; n/=2; } string s = to_string(n); if(k%2) s+="200"; cout<<s; } // Written by abhidot
#include <iostream> #include <string.h> #include <string> #include <cmath> #include<vector> #include <algorithm> #include <iterator> #include <map> #include <utility> #include<numeric> #include <iomanip> #include <set> #define ul unsigned long long int #define sll signed long long int #define ll long long int #define pb push_back #define db double using namespace std; ll meh=1000000000+7; ll gcd(ll a , ll b) { if(b==0) return a; a%=b; return gcd(b,a); } ll levi(ll n , ll k) { if (n%k==0) { return n/k; } else { return (((n-(n%k))/k)+1); } } ll mex(ll a,ll b){ if (a>=b) { return a; } else{ return b; } } ll men(ll a,ll b){ if (a<=b) { return a; } else{ return b; } } ll diff(ll a,ll b){ if (a>=b) { return a-b; } else{ return b-a; } } ll expoi(ll a,ll n){ if (n==0) { return 1; } else{ return a*expoi(a,n-1); } } ll asb(ll n){ if (n>=0) { return n; } else{ return -n; } } ll sumdi(ll n){ if (n<10) { return n; } else{ return n%10+sumdi((n-n%10)/10); } } int main() { ll n; cin>>n; vector<ll> arr(n+1); for (ll i = 0; i < n; i++) { cin>>arr[i+1]; } if (n==1) { cout<<arr[1]%meh<<endl; } else if (n==2) { cout<<(2*arr[1])%meh<<endl; } else { vector<ll> ans(n+1),calc(n+1); ans[1]=arr[1]%meh,calc[1]=1,calc[2]=2,ans[2]=(2*arr[1])%meh; for (ll i = 2; i <= n; i++) { calc[i]=(calc[i-1]+calc[i-2])%meh; } for (ll i = 2; i <= n; i++) { ans[i]=(ans[i-1]+calc[i-2]*arr[i]+ans[i-2]+arr[i-1]*(calc[i-1]))%(meh); } ll r=ans[n]%(meh); cout<<r<<endl; } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cassert> #include <climits> #include <algorithm> #include <string> #include <sstream> #include <complex> #include <vector> #include <list> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #include <fstream> using namespace std; #define SZ(x) (int)(x).size() #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REPR(i,n) for(int i=(n)-1;i>=0;i--) #define ALL(s) (s).begin(), (s).end() #define so(V) sort(ALL(V)) #define rev(V) reverse(ALL(V)) #define uni(v) v.erase( unique(ALL(v)) , v.end()); typedef long long unsigned int llu; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<vi> vvi; const double EPS = 1e-9; const int MOD = 1e9 + 7; const int INF = (1 << 29); const ll LINF = 1e18; const double PI = acos(-1); 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 V> typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) { t = v; } template<typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) { for (auto& e : t) fill_v(e, v); } template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; REP(i, N) { int x, y, r; cin >> x >> y >> r; cout << x << " " << y << " " << x + 1 << " " << y + 1 << endl; } return 0; }
#include<cstdio> const int mod=1e9+7; long long ans; int n,m,mp[2010][2010],row[2010],col[2010],dia[5010]; char str[2010]; int add(long long a,long long b) { if(a+b>=mod) return (a+b)%mod; else return a+b; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) { scanf("%s",str+1); for(int j=1;j<=m;++j) { if(str[j]=='.') mp[i][j]=0; else mp[i][j]=1; } } int lay=2e3; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { if(mp[i][j]) { row[i]=0; col[j]=0; dia[i-j+lay]=0; } else { int tmp=add(add(row[i],col[j]),dia[i-j+lay]); if(i==1&&j==1) ++tmp; row[i]=add(row[i],tmp); col[j]=add(col[j],tmp); dia[i-j+lay]=add(dia[i-j+lay],tmp); ans=tmp; } } } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for(int(i) = 0; (i) < (n); ++(i)) #define REPR(i, n) for(int(i) = (n); (i) >= 0; --(i)) #define FOR(i, n, m) for(int(i) = (n); (i) < (m); ++(i)) // clang-format off 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; } // clang-format on constexpr int INF = 1e9; class Timer { chrono::time_point<chrono::system_clock> start; public: Timer() { start = chrono::system_clock::now(); } double duration() { return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - start).count(); // return chrono::duration<chrono::milliseconds>(chrono::system_clock::now()- start).count(); } }; class xorShift { uint32_t x, y, z, w; static const double TO_DOUBLE; public: xorShift() { x = 123456789; y = 362436069; z = 521288629; w = 88675123; } xorShift(uint32_t seed) { x = seed = 1812433253u * (seed ^ (seed >> 30)); y = seed = 1812433253u * (seed ^ (seed >> 30)) + 1; z = seed = 1812433253u * (seed ^ (seed >> 30)) + 2; w = seed = 1812433253u * (seed ^ (seed >> 30)) + 3; } uint32_t nextInt(uint32_t n) { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w % n; } uint32_t nextInt() { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } double nextDouble() { return nextInt() * TO_DOUBLE; } }; const double xorShift::TO_DOUBLE = 1.0 / (1LL << 32); Timer T; xorShift rnd; constexpr double TIME_LIMIT = 1800; constexpr int N = 20; int M; vector<string> s; struct Solver { void first_method() { string all_string; REP(i, M) { all_string += s[i]; } REP(i, N) { string line; REP(j, N) { line += all_string[i * N + j]; } cout << line << endl; } } }; int main() { int n; cin >> n >> M; s.resize(M); REP(i, M) { cin >> s[i]; } Solver solver; solver.first_method(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 105, mod = 1000000; int n, m, a[N]; void read (int &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } struct ll { int p[550], len; ll() { memset (p, 0, sizeof (p)); len = 0; } void print() { printf ("%d", p[len]); for (int i = len - 1; i > 0; --i) { if (!p[i]) { printf ("0000"); continue; } for (int k = 10; k * p[i] < mod; k *= 10) putchar (48); printf ("%d", p[i]); } putchar ('\n'); } // ll operator + (const ll &a) const { // ll t; t.len = max (len, a.len); int q = 0; // for (int i = 1; i <= t.len; ++i) { // t.p[i] = p[i] + a.p[i] + q; // q = t.p[i] / mod; t.p[i] %= mod; // } // if (q > 0) t.p[++t.len] = q; return t; // } // ll operator + (const int &a) const { // ll t; t.len = max (len, 10); int q = 0; // for (int i = 1; i <= t.len; ++i) { // t.p[i] = p[i] + a % mod + q; // q = t.p[i] / mod; t.p[i] %= mod, a /= mod; // } // if (q > 0) t.p[++t.len] = q; return t; // } ll operator * (const int &a) const { ll t; t.len = len; int q = 0; for (int i = 1; i <= len; ++i) { t.p[i] = p[i] * a + q; q = t.p[i] / mod, t.p[i] %= mod; } while (q) t.p[++t.len] = q % mod, q /= mod; return t; } } res; int lim = 10000, tot, cnt[100000], tp, t[100000]; int is (int x) { if (x == 1) return 0; for (int i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; } signed main() { read (n); res.len = 1, res.p[1] = 1; for (int i = 1; i <= n; ++i) read (a[i]); for (int i = 1; i <= n; ++i) if (is (a[i])) t[++tp] = a[i], res = res * a[i]; for (int i = 1; i <= n; ++i) { if (is (a[i])) continue; int tag = 0; for (int j = 1; j <= tp; ++j) if (a[i] % t[j] == 0) { tag = 1; break; } if (tag) continue; ++tot; for (int j = 1; j <= lim; ++j) if (__gcd (j, a[i]) != 1) ++cnt[j]; } for (int i = 1; i <= lim; ++i) if (cnt[i] == tot) { res = res * i; break; } return res.print(), 0; }
#include<bits/stdc++.h> using namespace std; // typedef long long long long int ll; int main() { long long int n; cin>>n; long long int arr[n]; for (long long int i = 0; i < n; ++i) { cin>>arr[i]; } if (n==1) { cout<<arr[0]<<"\n"; return 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (__gcd(arr[i],arr[j])!=1) { arr[i]=arr[j]=__gcd(arr[i],arr[j]); } } } set <int>s(arr,arr+n); std::vector<int> v2(s.begin(), s.end()); std::vector<int> v3; for (int i = 0; i < v2.size(); ++i) { for (int j = 2; j <= 50; ++j) { if (v2[i]%j==0) { v3.push_back(j); break; } } } long long int h=1; for (int i = 0; i < v3.size(); ++i) { h*=v3[i]; } cout<<h<<"\n"; return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; int n,mi,ans; map<int,int> md; int gcd(int x,int y){return y==0?x:gcd(y,x%y);} int main() { int a; scanf("%d",&n); mi=1<<30; for(int i=1;i<=n;i++) { scanf("%d",&a); mi=min(mi,a); for(int j=1;j*j<=a;j++) if(a%j==0)md[j]=gcd(md[j],a),md[a/j]=gcd(md[a/j],a); } for(map<int,int>::iterator i=md.begin();i!=md.end();i++) if(i->first==i->second&&i->first<=mi)ans++; printf("%d",ans); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main(){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; int ans = 0; int kazu = 0; for(int j = 2; j <=1000; j++){ int counts = 0; rep(i,n){ if(a[i]%j == 0) counts++; } if(ans < counts) { kazu = j; ans = counts; } } cout << kazu << endl; return 0; } // cout << fixed << setprecision(15) << << endl;
#include<bits/stdc++.h> using namespace std; #define hi ios_base::sync_with_stdio(false);cin.tie(NULL); typedef int64_t ll; typedef long double ld; typedef long long unsigned llu; #define test int test; cin>>test; while(test--) #define loop(i,n,k) for(int i=n;i<k;i++) #define rloop(i,n,k) for(int i=n;i>=k;i--) #define INT(a) int a;scanf("%d",&a); #define intin(temp) scanf("%d",&temp); #define longin(temp) scanf("%lld",&temp); #define LLI(a) ll a;scanf("%lld",&a); #define DOUBLE(a) double a;scanf("%f",&a); #define LD(a) long double a;scanf("%lf",&a); #define pb(i) push_back(i) #define sz(i) i.size() #define mkp(i,j) make_pair(i,j) #define ip pair<int,int> #define lp pair<ll,ll> #define FOR(i,n) for(i=0;i<n;i++) #define range(v) v.begin(),v.end() #define bye return 0; #define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); template<class c> c gcd (c a,c b){ if(b==0) return a; gcd(b,a%b); } template<class c> c lcm(c a,c b){ return a/gcd(a,b)*b; } template<class a> bool prime(a n){ for(a i=2;i<=sqrt(n);i++){ if(n%i==0) return 0; } return 1; } bool bigger(pair<int,int>picachu,pair<int,int> qicachu){ return (picachu.first<qicachu.first); }void printarr(int arr[],int n){ for(int i=0;i<n;i++) cout<<arr[i]<<' '; cout<<endl; } int chartoint(char c) { if (c >= 'a' && c <= 'z') return c - 'a'; if (c >= 'A' && c <= 'Z') return c - 'A' + 26; return 52; } int kadane(int arr[],int n){ int maximum= arr[0]; int max_so_far = 0; for(int i=0;i<n;i++){ max_so_far+=arr[i]; if(max_so_far>maximum) maximum = max_so_far; if(max_so_far<0) max_so_far = 0; } return maximum; } void run(){ int n,x; cin >> n >> x; int temp; for(int i = 0;i<n;i++){ cin >> temp; if(temp!=x ){ cout << temp << ' '; } } cout << endl; } int main(){ hi; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif { run(); } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl; bye; }
#include <iostream> #include <iomanip> #include <algorithm> #include <assert.h> #include <complex> #include <utility> #include <vector> #include <string> #include <stack> #include <queue> #include <tuple> #include <cmath> #include <bitset> #include <cctype> #include <set> #include <map> #include <unordered_map> #include <numeric> #include <functional> #include <chrono> #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=a;i<b;++i) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define _rrep(i,a) rrepi(i,a,0) #define rrepi(i,a,b) for(int i=a-1;i>=b;--i) #define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__) #define all(x) (x).begin(),(x).end() #define PRINT(V) cout << V << "\n" #define SORT(V) sort((V).begin(),(V).end()) #define RSORT(V) sort((V).rbegin(), (V).rend()) using namespace std; using ll = long long; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); } template<class itr> void cins(itr first,itr last){ for (auto i = first;i != last;i++){ cin >> (*i); } } template<class itr> void array_output(itr start,itr goal){ string ans = "",k = " "; for (auto i = start;i != goal;i++) ans += to_string(*i)+k; if (!ans.empty()) ans.pop_back(); PRINT(ans); } ll gcd(ll a, ll b) { return a ? gcd(b%a,a) : b; } const ll INF = 1e18; const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll MOD3 = 1e6; const ll EPS = 1e-10; int sgn(const double a){ return (a < -EPS ? -1 : (a > EPS ? +1 : 0)); } typedef pair<int,int> pi; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> tri; typedef pair<double,double> point; typedef complex<double> Point; typedef string::const_iterator State; const ll MAX = 105; constexpr ll nx[4] = {-1,0,1,0}; constexpr ll ny[4] = {0,1,0,-1}; int main(){ ll n,x; cin >> n >> x; vector<ll> a(n),ans; cins(all(a)); rep(i,n){ if (a[i] == x) continue; ans.push_back(a[i]); } array_output(all(ans)); }
#include<bits/stdc++.h> using namespace std; #define _rep(i, x, y) for(int i = (int)x; i < (int)y; ++i) #define _dep(i,x,y) for(int i = (int)x; i > (int)y; i--) #define PII pair<int,int> #define eb emplace_back #define pb push_back #define fi first #define se second #define PQ priority_queue #define lb lower_bound #define ub upper_bound #define all(x) (x).begin(), (x).end() typedef long long ll; typedef vector<int> VI; typedef vector<VI> VII; typedef vector<ll> VL; typedef vector<vector<ll>> VLL; constexpr int mod = 1e9 + 7; constexpr ll KINF = 1e18; constexpr double eps = 1e-7; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; ll x; cin >> n >> x; VI a(n); _rep(i, 0, n) cin >> a[i]; auto check = [&](int k){ int rem = x % k; int b[n]; _rep(i, 0, n) b[i] = a[i] % k; int dp[n + 1][k + 1][k + 1]; memset(dp, -0x3f, sizeof dp); _rep(i, 0, n + 1) dp[i][0][0] = 0; _rep(i, 1, n + 1) _rep(j, 1, k + 1) _rep(z, 0, k){ dp[i][j][z] = dp[i - 1][j][z]; if(z - b[i - 1] >= 0) dp[i][j][z] = max(dp[i][j][z], dp[i - 1][j - 1][z - b[i - 1]] + a[i - 1]); else dp[i][j][z] = max(dp[i][j][z], dp[i - 1][j - 1][z + k - b[i - 1]] + a[i - 1]); if(dp[n][k][rem] == x) return 0; } return dp[n][k][rem]; }; int res = -1; vector<PII> ans; _dep(k, n, 1){ res = check(k); if(res > 0) { ans.eb(res, k); } if(res == 0){ cout << 0 << endl; return 0; } } if(ans.empty()) { sort(all(a)); // auto it = lb(all(a), x); // if(it != a.end()) cout << x - *it << endl; // else cout << x - a[n - 1] << endl; cout << x - a[n - 1] << endl; return 0; } else{ ll res = KINF; for(auto& [ini, div] : ans){ res = min(res, (x - ini) / div); } cout << res << endl; } return 0; } // 小心左移运算符导致的int溢出 // dp记得处理非法状态 // resize并不会初始化数组 // 全局函数一定要记得inline // 带空格字符串不能直接cin,要用getline
#include<bits/stdc++.h> using namespace std; #define pb emplace_back #define st first #define nd second typedef long long ll; typedef vector<int> vi; typedef pair<int,int> ii; typedef vector<ii> vii; typedef vector<vi> vvi; #define sz(a) int((a).size()) #define all(c) (c).begin(),(c).end() #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #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 endl "\n" #define fre freopen("in.txt","r",stdin);freopen("out.txt","w",stdout) #define start ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define N 200005 //2 4 4 +1 // ll yen[20][1<<20]; queue<pair<int,int>> q; vector<int> v[N]; ll vis[N]; ll dp[20][20]; ll k; int idx[N]; vector<int> c; ll sum=0; bool check=true; void solve() { int n,m; cin>>n>>m; rep(i,0,m) { int a,b; cin>>a>>b; v[a].pb(b);v[b].pb(a); } cin>>k; memset(idx,-1,sizeof(idx)); rep(i,0,k) { int a; cin>>a; c.pb(a); idx[c[i]]=i; } rep(i,0,k) { memset(vis,0,sizeof(vis)); q.push(make_pair(c[i],0)); rep(j,0,k) { dp[i][j]=N; } while(!q.empty()) { ll temp=q.front().st; ll cur=q.front().nd; q.pop(); if(vis[temp]) continue; vis[temp]=1; if(idx[temp] != -1) dp[i][idx[temp]] = cur; rep(j,0,v[temp].size()) { if(vis[v[temp][j]]) continue; q.push({v[temp][j],cur+1}); } } } memset(yen, N, sizeof(yen)); for (int i=0; i<k; i++) yen[i][1<<i] = 0; for (int j=0; j<(1<<k); j++) { for (int i=0; i<k; i++) if (j & (1<<i)) { int cur = j & ~(1<<i); for (int l=0; l<k; l++) if (cur & (1<<l)) { yen[i][j] = min(yen[i][j], yen[l][cur] + dp[l][i]); } } } ll ans = N; for (int i=0; i<k; i++) ans = min(ans, yen[i][(1<<k)-1]); if (ans == N) cout<<-1<<endl; else cout<<ans+1<<endl; } int main(){ start int t=1; //cin>>t; rep(i,0,t) solve(); }
/* I love the sound you make when you shut up. */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace __gnu_pbds; /*-------typedefs------*/ template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ; 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 } /*-------- 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();} /*-------- 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}; /*----------------------------------------------------------------*/ const int MOD = 1e9 + 7; const int N = 2e5 + 5; /*-------------- Push your limits here ---------------------------*/ int h, w, ans; void dfs(int i, int bit, int a, int b) { if(i == h * w) { ans++; return; } if(bit & 1 << i) dfs(i + 1, bit, a, b); if(b) dfs(i + 1, bit | 1 << i, a, b - 1); if(a) { if((i % w) != (w - 1) && ~bit & 1 << (i + 1)) dfs(i + 2, bit | 1 << i | 1 << (i + 1), a - 1, b); if((i + w) <= (h * w) && ~bit & 1 << (i + w)) dfs(i + 1, bit | 1 << i | 1 << (i + w), a - 1, b); } } void solve() { int a, b; cin >> h >> w >> a >> b; ans = 0; dfs(0, 0, a, b); pf(ans); } int main() { go(); ssolve return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll solve() { ll H, W, A, B; cin >> H >> W >> A >> B; ll ans = 0; ll a = A, b = B; vector<vector<int>> u(H, vector<int>(W)); auto dfs = [&](auto self, int h, int w) -> void { while ( u[h][w] > 0 ) { if ( w < W-1 ) { w++; } else if ( h < H-1 ) { h++; w = 0; } else { ans++; return; } } if ( u[h][w] == 0 && b > 0 ) { u[h][w] = 1; b--; self(self, h, w); b++; u[h][w] = 0; } if ( w < W-1 && u[h][w] == 0 && u[h][w+1] == 0 && a > 0 ) { u[h][w] = 2; u[h][w+1] = 2; a--; self(self, h, w); a++; u[h][w] = 0; u[h][w+1] = 0; } if ( h < H-1 && u[h][w] == 0 && u[h+1][w] == 0 && a > 0 ) { u[h][w] = 3; u[h+1][w] = 3; a--; self(self, h, w); a++; u[h][w] = 0; u[h+1][w] = 0; } }; dfs(dfs,0,0); return ans; } int main() { auto ans = solve(); cout << ans << "\n"; return 0; }
#include <cstdio> using namespace std; #define ll long long ll r, x, y; ll read() { ll X = 0, F = 1; char C = getchar(); while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();} while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();} return X * F; } int main() { r = read(); x = read(); y = read(); ll dis = x * x + y * y; r = r * r; for(ll i = 0;; ++i) if(i * i * r >= dis) { printf("%lld\n", i == 1 ? (i * i * r == dis ? 1 : 2) : i); break; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long R, X, Y, ans; double k; cin >> R >> X >> Y; k = sqrt(X * X + Y * Y); ans = ceil(k / R); if(k < R) ans++; cout << ans << endl; }
#include<bits/stdc++.h> #define fill(arr,c) memset(arr,c,sizeof(arr)) #define perm(r) next_permutation(all(r)) //it gives bool value ans permute the string lexoggraphically #define sortdesc greater<int>() #define ll long long int //std::setfill('0') << std::setw(5) << 25; it adds leaing zeroes; #define f(i,a) for(ll i=0;i<a;i++) #define fo(i,a) for(ll i=1;i<=a;i++) #define foo(i,x,n,c) for(ll i=x;i<n;i+=c) #define foi(i,x,n,c) for(ll i=x;i>=n;i+=c) #define fauto(i,m) for(auto i:m) #define forall(a) for( auto itr=a.begin();itr!=a.end();itr++) #define ld long double #define in push_back #define mll map<ll,ll> #define mcl map<char,ll> #define msl map<string,ll> #define vi vector<int > #define vl vector<ll > #define vp vector< pair<int,int> > #define vs vector <string> #define vc vector <char> #define o(a) cout<<a #define os(a) cout<<a<<" " #define en cout<<"\n"; #define testcase ll t ;cin>>t; while(t--) #define ff first #define ss second #define pl pair<ll,ll> #define pi pair<int,int> #define all(a) (a).begin(), (a).end() #define sz(a) (a).size() #define prec(n) fixed<<setprecision(n) #define mp make_pair #define bitcount __builtin_popcountll #define imin o("imin");en; using namespace std; bool comp(const pair<int,int> &a , const pair<int,int> &b){ if(a.first == b.first) return a.second < b.second; return a.first < b.first; } bool comps(const pair<int,int> &a , const pair<int,int> &b){ if(a.second == b.second) return a.first< b.first; return a.second < b.second; } bool compmx(int a, int b) { return (a < b); } //ll pi=acos(0.0)*2; void fastio() { ios::sync_with_stdio(0); cin.tie(0); } void inout() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { fastio(); inout(); ll n; cin>>n; ll a[n]; f(i,n) cin>>a[i]; ll ans=0; f(i,n) { // os(1); ll cur=a[i]; foo(j,i+1,n,1) { if(a[j]>=a[i]) { cur+=a[i]; } else break; } foi(j,i-1,0,-1) { if(a[j]>=a[i]) { cur+=a[i]; } else break; } ans=max(ans,cur); } o(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define forn(i,a,n) for (int i=a; i<n; i++) int n; int a[10001]; int mn[10001][10001]; signed main(){ ios::sync_with_stdio(false); cin.tie(0); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); cin>>n; forn(i,0,n) cin>>a[i]; forn(i,0,n){ mn[i][i]=a[i]; forn(j,i+1,n) mn[i][j]=min(mn[i][j-1],a[j]); } int res=-1; forn(i,0,n){ int mx=-1; forn(j,i,n) mx=max(mx,mn[i][j]*(j-i+1)); res=max(res,mx); } cout<<res<<endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define endl "\n" using ll = long long; const int MOD = 1e9 + 7; void solve() { int n, m; cin >> n >> m; if (m == 0) { cout << 1 << endl; return; } vector<int> a(m); for (int &x : a) cin >> x; sort(a.begin(), a.end()); a.push_back(n + 1); int ans = 0; int cur = 1; vector<int> gaps; for (int i = 0; i < m + 1; i++) { int w = a[i] - cur; if (w) gaps.push_back(w); cur = a[i] + 1; } int k = n; for (int w : gaps) k = min(k, w); for (int w : gaps) ans += (w + k - 1) / k; cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; template<typename T> using V = vector<T>; template<typename T> using P = pair<T, T>; template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); } template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); } template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;} template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());} template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++) {cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}}; template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0]; for(ll i=1;i<n;i++)cerr spa v[i]; cerr<<"\n";}; const ll INF = (1ll<<62); // const ld EPS = 1e-10; // const ld PI = acos(-1.0); const ll mod = (int)1e9 + 7; //const ll mod = 998244353; int main(){ ll N, M; cin >> N >> M; V<ll> A(M); REP(i, M) cin >> A[i]; A.push_back(0); A.push_back(N+1); sort(ALL(A)); M += 2; ll mi = INF; REP(i, M-1){ if(A[i+1]-A[i]-1 > 0) chmin(mi, A[i+1]-A[i]-1); } if(mi == INF){ cout << 0 << endl; return 0; } ll res = 0; REP(i, M-1){ res += (A[i+1]-A[i]-1+mi-1)/mi; } cout << res << endl; return 0; }
//include //------------------------------------------ #include <string> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <complex> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <cstring> #include <ctime> // namespace using namespace std; // type alias using ll = long long; using ull = unsigned long long; using comp = complex<double>; // constant static const ll MOD = 1000000007LL; // static const ll MOD = (1LL << 61LL) - 1LL; static const double PI = 3.14159265358979323846; // conversion inline ll toint(string s) { ll v; istringstream sin(s); sin >> v; return v; } template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); } // print #define RET(x) return cout << x << endl, 0; // for loop #define REP(i, a, b) for ((i) = (ll)(a);(i) < (ll)(b);(i)++) #define REPD(i, a, b) for (ll i = (ll)(a);(i) < (ll)(b);(i)++) #define REPI(v, vs) for (auto& v : vs) //debug #ifdef LOCAL_ENV #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __LINE__ << ")" << " " << __FILE__ << endl #else #define DUMP(x) #define DEBUG(x) #endif #define MAX_VALUE 9223372036854775807LL template <ull N, class T, class... Args, std::enable_if_t<N == 0, int> = 0> auto make_multiple_vector(Args... args) { return T(args...); } template <ull N, class T, class... Args, std::enable_if_t<N != 0, int> = 0> auto make_multiple_vector(ull size, Args... args) { using value_type = std::decay_t<decltype(make_multiple_vector<N - 1, T>(args...))>; return vector<value_type>(size, make_multiple_vector<N - 1, T>(args...)); } map<ll, ll> get_prime_factors(ll a, const vector<bool>& is_primes){ ll sqrt_a = sqrt(double(a)); while ((sqrt_a + 1) * (sqrt_a + 1) <= a) ++sqrt_a; while (sqrt_a * sqrt_a > a) --sqrt_a; map<ll, ll> prime_factors; REPD(p, 2, sqrt_a + 1) { if (!is_primes[p]) continue; while (a % p == 0LL) { if (prime_factors.count(p) == 0) prime_factors[p] = 0; ++prime_factors[p]; a /= p; } } if (a != 1LL) prime_factors[a] = 1; return prime_factors; } // computational complexity: o(log(max(a, b))) inline ull get_gcd(ull a, ull b) { if (b == 0) { return a; } return get_gcd(b, a % b); } int solve() { ll n; cin >> n; vector<ll> as(n); REPD(i, 0, n) cin >> as[i]; ll max_a = *max_element(as.begin(), as.end()); ll min_a = *min_element(as.begin(), as.end()); ll sqrt_a = sqrt(double(max_a)); while ((sqrt_a + 1) * (sqrt_a + 1) <= max_a) ++sqrt_a; while (sqrt_a * sqrt_a > max_a) --sqrt_a; vector<bool> is_primes(sqrt_a + 1, true); is_primes[1] = is_primes[0] = false; REPD(p, 2, sqrt_a + 1) { if (!is_primes[p]) continue; ll i = 2; while(p * i <= sqrt_a) is_primes[p * i++] = false; } map<ll, ll> num_to_gcd; ll prime, exponent; REPD(i, 0, n) { auto prime_factors = get_prime_factors(as[i], is_primes); ll multiple_count = 1; REPI(prime_info, prime_factors) { tie(prime, exponent) = prime_info; multiple_count *= exponent + 1LL; } REPD(j, 0, multiple_count) { ll j0 = j; ll multiple = 1; REPI(prime_info, prime_factors) { tie(prime, exponent) = prime_info; REPD(k, 0, j0 % (exponent + 1LL)) multiple *= prime; j0 /= exponent + 1LL; } if (multiple > min_a) continue; if (num_to_gcd.count(multiple) == 0) num_to_gcd[multiple] = as[i]; num_to_gcd[multiple] = get_gcd(num_to_gcd[multiple], as[i]); } } ll res = 0; ll num, gcd; REPI(num_gcd, num_to_gcd) { tie(num, gcd) = num_gcd; if (num == gcd) ++res; } cout << res << endl; return 0; } //main function int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); // ll t; // cin >> t; // REPD(i, 0, t) solve(); return 0; }
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <string> #include <queue> #include <map> #define mod 1000000007 #define N 200005 #define int long long using namespace std; int n, b[N], a[N], z[N], mp[N], ans = 0, anss[N], us[N]; void Msort(int l, int r) { int mid = (l + r) / 2; if(l == r) return ; Msort(l, mid); Msort(mid + 1, r); int l1 = l, r1 = mid + 1; for(int i=l; i<=r; i++) { if(l1 <= mid && r1 <= r) { if(a[l1] <= a[r1]) { z[i] = a[l1]; l1++; } else { z[i] = a[r1]; r1++; ans += (mid - l1 + 1); } } else { if(l1 <= mid) z[i] = a[l1], l1++; if(r1 <= r) z[i] = a[r1], r1++; } } for(int i=l; i<=r; i++) a[i] = z[i]; } signed main(void) { cin >> n; for(int i=1; i<=n; i++) { cin >> b[i]; mp[b[i]] = i; a[i] = b[i]; } Msort(1, n); if(ans != n-1) { cout << -1; return 0; } int now = 1, cnt = 0, flag = 0; while(1) { while(now <= n && b[now] == now) now++; if(now > n) break; anss[++cnt] = mp[now] - 1; if(us[mp[now]-1]) flag = 1; us[mp[now]-1] = 1; if(anss[cnt] <= 0 || anss[cnt] > n-1) flag = 1; int p = mp[now]; mp[b[p-1]] = p; mp[b[p]] = p-1; swap(b[p], b[p-1]); } if(flag || cnt != ans) { cout << -1; return 0; } for(int i=1; i<=cnt; i++) cout << anss[i] << '\n'; for(int i=cnt+1; i<=n-1; i++) cout << 1 << '\n'; }
#include <bits/stdc++.h> using namespace std; int main(){ vector<int> h; vector<string> name; string temp; int n, f_max = 0, s_max, temp_no; cin >> n; for(int i = 0; i < n; i++){ cin >> temp >> temp_no; name.push_back(temp); h.push_back(temp_no); } for(int i = 1; i < n; i++){ if(h[i] > h[f_max]) f_max = i; } if(f_max == 0) s_max = 1; else s_max = 0; for(int i = 0; i < n; i++){ if(i != f_max){ if(h[i] > h[s_max]) s_max = i; } } cout << name[s_max]; return 0; }
#include <bits/stdc++.h> using namespace std; void solveC() { int n; cin >> n; vector<vector<int>> arr(n, vector<int>(2, 0)); for (int i = 0; i < n; i++) cin >> arr[i][0] >> arr[i][1]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { int x1 = arr[j][0] - arr[i][0], y1 = arr[j][1] - arr[i][1]; int x2 = arr[k][0] - arr[j][0], y2 = arr[k][1] - arr[j][1]; if (y2 * x1 == x2 * y1) { cout << "Yes" << "\n"; return ; } } } } cout << "No" << "\n"; } void code() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { // code(); solveC(); }
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main(){ int N; cin>>N; vector<vector<ll>> V(N+1); for(int i=0;i<N+1;i++){ V[i] = {}; } for(int i=0;i<N;i++){ ll X; int C; cin>>X>>C; V[C].push_back(X); } vector<pair<ll,ll>> pv={}; int lpv=0; for(int i=0;i<N+1;i++){ int l = V[i].size(); if(l == 0)continue; pair<ll,ll> p; sort(V[i].begin(),V[i].end()); p.first=V[i][0]; p.second=V[i][l-1]; pv.push_back(p); lpv++; } ll dp[lpv][2]; dp[0][0] = abs(pv[0].second) + abs(pv[0].first-pv[0].second); dp[0][1] = abs(pv[0].first) + abs(pv[0].first-pv[0].second); for(int i=1;i<lpv;i++){ dp[i][0] = min(dp[i-1][0] + abs(pv[i].second-pv[i-1].first) + abs(pv[i].first-pv[i].second), dp[i-1][1] + abs(pv[i].second-pv[i-1].second) + abs(pv[i].first-pv[i].second) ); dp[i][1] = min(dp[i-1][0] + abs(pv[i].first-pv[i-1].first) + abs(pv[i].first-pv[i].second), dp[i-1][1] + abs(pv[i].first-pv[i-1].second) + abs(pv[i].first-pv[i].second) ); } ll ans = min(dp[lpv-1][0] + abs(pv[lpv-1].first), dp[lpv-1][1] + abs(pv[lpv-1].second) ); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; // -------------------------------------------------------- template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; } #define FOR(i,l,r) for (ll i = (l); i < (r); ++i) #define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define SUMLL(c) accumulate(ALL(c), 0LL) #define COUNT(c,v) count(ALL(c),(v)) #define SZ(c) ((ll)(c).size()) #define BIT(b,i) (((b)>>(i)) & 1) #define PCNT(b) __builtin_popcountll(b) #define CIN(c) cin >> (c) #define COUT(c) cout << (c) << '\n' #define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n' ll llceil(ll a, ll b) { return (a + b - 1) / b; } string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; } string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; } using P = pair<ll,ll>; using VP = vector<P>; using VVP = vector<VP>; using VS = vector<string>; using VVS = vector<VS>; using VLL = vector<ll>; using VVLL = vector<VLL>; using VVVLL = vector<VVLL>; using VB = vector<bool>; using VVB = vector<VB>; using VVVB = vector<VVB>; using VD = vector<double>; using VVD = vector<VD>; using VVVD = vector<VVD>; using VLD = vector<ld>; using VVLD = vector<VLD>; using VVVLD = vector<VVLD>; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const ll MOD = 1000000007; // static const ll MOD = 998244353; static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1 // -------------------------------------------------------- // #include <atcoder/all> // using namespace atcoder; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); ll N; cin >> N; VLL X(N),C(N); REP(i,N) cin >> X[i] >> C[i]; map<ll,VLL> mp; // mp[0].push_back(0); mp[N+1].push_back(0); REP(i,N) mp[C[i]].push_back(X[i]); ll M = SZ(mp); VVLL dp(M+1,VLL(2,INF)); dp[0][0] = 0; ll i = 1; VLL prev = {0,0}; for (auto& [_, X] : mp) { ll L = INF, R = -INF; for (ll x : X) { chmin(L, x); chmax(R, x); } chmin(dp[i][0], dp[i-1][0] + llabs(prev[0] - R) + llabs(L - R)); chmin(dp[i][0], dp[i-1][1] + llabs(prev[1] - R) + llabs(L - R)); chmin(dp[i][1], dp[i-1][0] + llabs(prev[0] - L) + llabs(L - R)); chmin(dp[i][1], dp[i-1][1] + llabs(prev[1] - L) + llabs(L - R)); i++; prev[0] = L; prev[1] = R; } ll ans = MIN(dp[M]); COUT(ans); return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; const ll MOD = 1000000007; const double PI = 3.141592653589793238; int main() { ll N, M; cin >> N >> M; set<ll> s; for (ll i = 0; i < N; ++i) { ll A; cin >> A; s.insert(A); } for (ll i = 0; i < M; ++i) { ll B; cin >> B; if (s.find(B) != s.end()) s.erase(B); else s.insert(B); } ll cnt = 0; for (auto i = s.begin(); i != s.end(); ++i) { ++cnt; cout << *i << (cnt == s.size() ? "\n" : " "); } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define yes() cout << "Yes" << endl #define no() cout << "No" << endl using ll = long long; using ld = long double; using P = pair<int,int>; int main() { int n,m; cin>>n>>m; vector <int> memo(1001,0); rep(i,n+m){ int x; cin>>x; memo[x]++; } rep(i,1001){ if(memo[i]==1)cout<<i<<" "; } cout<<endl; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (n); i++) using namespace std; using P = pair<int, int>; using ll = long long; const int INF = 1001001001; int h, w; int a[2005][2005]; bool visited[2005][2005]; int memo[2005][2005]; int f(int i, int j){ if(i == h-1 && j == w-1) return 0; if(visited[i][j]) return memo[i][j]; visited[i][j] = true; int res = -INF; if(j+1 < w) res = max(res, a[i][j+1]-f(i, j+1)); if(i+1 < h) res = max(res, a[i+1][j]-f(i+1, j)); return memo[i][j] = res; } int main(){ cin >> h >> w; rep(i, h)rep(j, w){ char x; cin >> x; if(x == '+') a[i][j] = 1; else a[i][j] = -1; } int score = f(0, 0); if(score == 0) cout << "Draw" << endl; else if(score > 0) cout << "Takahashi" << endl; else cout << "Aoki" << endl; }
// author: sharad12arse #include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define gcd __gcd #define INF LONG_LONG_MAX void solve(){ int n,m; cin>>n>>m; vector<string> arr(n); for(int i=0;i<n;i++){ cin>>arr[i]; } vector<vector<int> > dp(n,vector<int> (m,0)); vector<vector<int> > vst(n,vector<int> (m,0)); queue<tuple<int,int,int> > qu; tuple<int,int,int> temp; int a=0; if((n+m)%2){ a=1; }else{ a=0; } int cnt=0; char ch=arr[0][0]; qu.push(make_tuple(n-1,m-1,a)); while(qu.size()){ cnt++; temp=qu.front(); qu.pop(); int u,v,w; u=get<0>(temp); v=get<1>(temp); w=get<2>(temp); if(arr[u][v]=='!'){ continue; } if(u==n-1&&v==m-1){ if(w){ if(arr[u][v]=='+'){ dp[u][v]++; }else{ dp[u][v]--; } }else{ if(arr[u][v]=='+'){ dp[u][v]--; }else{ dp[u][v]++; } } }else if(u==n-1){ if(w){ if(arr[u][v]=='+'){ dp[u][v]++; }else{ dp[u][v]--; } }else{ if(arr[u][v]=='+'){ dp[u][v]--; }else{ dp[u][v]++; } } dp[u][v]+=dp[u][v+1]; }else if(v==m-1){ if(w){ if(arr[u][v]=='+'){ dp[u][v]++; }else{ dp[u][v]--; } }else{ if(arr[u][v]=='+'){ dp[u][v]--; }else{ dp[u][v]++; } } dp[u][v]+=dp[u+1][v]; }else{ if(w){ if(arr[u][v]=='+'){ dp[u][v]++; }else{ dp[u][v]--; } dp[u][v]+=min(dp[u+1][v],dp[u][v+1]); }else{ if(arr[u][v]=='+'){ dp[u][v]--; }else{ dp[u][v]++; } dp[u][v]+=max(dp[u+1][v],dp[u][v+1]); } } arr[u][v]='!'; if(u-1>=0&&vst[u-1][v]!=1){ qu.push(make_tuple(u-1,v,w^1)); vst[u-1][v]=1; } if(v-1>=0&&vst[u][v-1]!=1){ qu.push(make_tuple(u,v-1,w^1)); vst[u][v-1]=1; } } if(ch=='+'){ dp[0][0]++; }else{ dp[0][0]--; } // for(int i=0;i<n;i++){ // for(int j=0;j<m;j++){ // cout<<dp[i][j]<<" "; // } // cout<<"\n"; // } if(dp[0][0]==0){ cout<<"Draw\n"; }else{ if(dp[0][0]>0){ cout<<"Takahashi\n"; }else{ cout<<"Aoki\n"; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t=1; // cin>>t; while(t--){ solve(); } }
//arc111_c.cpp //Sat Jan 9 22:32:39 2021 #include <bits/stdc++.h> #define INTINF 2147483647 #define LLINF 9223372036854775807 #define MOD 1000000007 #define rep(i,n) for (int i=0;i<(n);++i) using namespace std; using ll=long long; typedef pair<int,int> P; int main(){ int n; cin >> n; vector<int> konohitonotaijyu(n); vector<P> taijyu; rep(i,n){ int tmp; cin >> tmp; taijyu.push_back(P(tmp,i)); konohitonotaijyu[i] = tmp; } vector<int> cargo(n); rep(i,n){ cin >> cargo[i]; } //何番さんが何番の荷物持ってるか vector<int> cn(n); rep(i,n){ cin >> cn[i]; cn[i]--; } //何番の荷物を何番さんが持ってるか vector<int> mn(n); rep(i,n){ mn[cn[i]]=i; } sort(taijyu.begin(),taijyu.end()); vector<int> order; rep(i,n){ order.push_back(taijyu[i].second); } vector<P> ans; rep(i,n){ int from = order[i]; if (cn[from]==from)continue; if (cargo[cn[from]]>=konohitonotaijyu[from]){ if (i==n-1)break; cout << -1 << endl; return 0; } int to = mn[order[i]]; ans.push_back(P(from,to)); int fromnimotu = cn[from]; int tonimotu = cn[to]; int x = fromnimotu; cn[from] = tonimotu; cn[to] = fromnimotu; mn[fromnimotu] = to; mn[tonimotu] = from; } cout << ans.size() << endl; rep(i,ans.size()){ cout << ans[i].first+1 << " " << ans[i].second+1 << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<int> A(n + 1), B(n + 1), P(n + 1), par(n + 1); vector<vector<int>> cycles(n + 1); int cid = 0; for(int i = 1; i <= n; ++i) { cin>>A[i]; } for(int i = 1; i <= n; ++i) { cin>>B[i]; } for(int i = 1; i <= n; ++i) { cin>>P[i]; par[i] = P[i]; } vector<int> vs(n + 1, 0); for(int i = 1; i <= n; ++i) { if(!vs[i]) { int x = i; while(!vs[x]) { cycles[cid].push_back(x); vs[x] = 1; x = par[x]; } cid += 1; } } vector<pair<int, int>> ans; bool imps = 0; for(auto cycle: cycles) { set<pair<int, int>> wts; for(auto x: cycle) { // cout<<"x = "<<x<<endl; if(B[par[x]] >= A[x] && par[x] != x) { imps = 1; } wts.insert({B[par[x]], x}); } while(!imps && !wts.empty()) { auto tp = --wts.end(); int cur = tp->second; int nxt = par[cur]; wts.erase(tp); if(nxt == cur) { continue; } // cout<<" next "<<nxt<<" "<<cur<<endl; par[cur] = par[nxt]; par[nxt] = nxt; ans.push_back({cur, nxt}); wts.insert({B[par[cur]], cur}); if(B[par[cur]] >= A[cur] && par[cur] != cur) { imps = 1; break; } } } if(imps) { cout<<"-1\n"<<endl; return 0; } cout<<ans.size()<<endl; for(auto x: ans) { cout<<x.first<<" "<<x.second<<endl;; } return 0; }
#include <bits/stdc++.h> #include <climits> using namespace std; template<class T> using V = vector<T>; template<class T> using VV = V<V<T>>; using ld = long double; #define ll long long using ull = unsigned ll; using PLL = pair<ll, ll>; using VLL = V<ll>; using VB = V<bool>; using VVB = VV<bool>; using VVLL = VV<ll>; using Gr = VVLL; using MLL = map<ll, ll>; #define UMLL unordered_map<ll, ll, custom_hash> #define fast ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); #define R & #define CR const R #define FORI(i, a, b) for(ll i = a, max##i = b; i < max##i; ++i) #define FOR(i, n) FORI(i, 0, n) #define RFORI(i, a, b) for(ll i = a, min##i = b; i >= min##i; --i) #define RFOR(i, n) RFORI(i, n, 0) #define FORA(i, a) for(auto i : a) #define FORAR(i, a) for(auto R i : a) #define FORACR(i, a) for(auto CR i : a) #define ALL(obj) begin(obj), end(obj) #define Count(q) while(q--) #define OK cerr << "OK\n"; #define mp make_pair #define pb push_back //#define DEBUG template<class T> T sqr(T x) { return x * x; } void YES(bool g, ostream R os, bool upper = true) { if(g) if(upper) os << "YES"; else os << "Yes"; else if(upper) os << "NO"; else os << "No"; os << "\n"; } template<class T> void show(T CR t, ostream R os = cerr) { FORACR(i, t) os << i << " "; os << "\n"; } template<class T> void show2d(T CR t, ostream R os = cerr) { FORACR(i, t) show(i, os); os << "\n"; } constexpr ll MOD = 1e9 + 7; //998244353; constexpr ll len = 'z' - 'a' + 1; constexpr ll INF = 1e15, MINF = -INF; constexpr ld PI = atanl(1.0L) * 4; constexpr ld eps = 1e-9, EPS = 1e-9; 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); } }; //arr void init() {} void solve(istream R is, ostream R os) { ll n; is >> n; string s; is >> s; char t = s[0]; bool g = 1; FORA(i, s) g &= i == t; if(g) { os << "-1\n"; return; } if(s.front() != s.back()) { os << "1\n"; return; } g = 0; FOR(i, n - 1) if(t != s[i] && t != s[i + 1]) { g = 1; } if(g) os << "2\n"; else os << "-1\n"; } void tester(istream R is, ostream R os) { auto seed = time(nullptr); //cerr << "seed: " << seed << "\n"; srand(seed); fast init(); ll q = 1; //is >> q; os << setprecision(999); Count(q) solve(is, os); } int main() { //ifstream in("input.txt"); //ofstream out("output.txt"); tester(cin, cout); }
#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 mp make_pair #define for0(i,n) for(int i=0;i<n;i++) #define for1(i,n) for(int i=1;i<=n;i++) #define w(x) int x;cin>>x;while(x--) #define pii pair<int,int> #define vi vector<int> #define vpii vector<pair<int,int>> #define vvi vector<vector<int>> #define precision(x,k) fixed<<setprecision(k)<<x #define inf 1e16 typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int a,b,c; vector<int>v[10]; for(int i=0;i<=9;i++) { int x=i; v[i].push_back(x); if(i!=0) x=(x*i)%10; while(x!=i) { v[i].push_back(x); x=(x*i)%10; } } /* for0(i,10) { for0(j,v[i].size()) cout<<v[i][j]<<" "; cout<<"\n"; }*/ cin>>a>>b>>c; int r=a%10; // cout<<r<<"\n"; int ans1= b%(v[r].size()); if(ans1==0) ans1=v[r].size(); ans1=v[r][ans1-1]; // cout<<ans1<<"\n"; int p[10]; for(int i=0;i<10;i++) p[i]=0; int j=ans1; int i=1; while(p[j]==0) {//cout<<j<<" "<<i<<"\n"; p[j]=i; i++; int k=j; j=b%(v[k].size()); if(j==0) j=v[k].size(); j=v[k][j-1]; } int st=p[j]-1; int sz=i-p[j]; // cout<<st<<" "<<sz<<"\n"; int ans; if(c<=st) ans=c; else{ ans=(c-st)%sz; if(ans==0) ans=sz; ans=ans+st; } // cout<<ans<<"\n"; int final_ans; for(int i=0;i<10;i++) { //cout<<p[i]<<" "; if(p[i]==ans) final_ans=i; } cout<<final_ans<<"\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define reps(i, n) for (ll i = 1; i <= (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define uniq(a) (a).erase(unique(all(a)), (a).end()) #define bit(n) (1LL << (n)) #define dump(a) cerr << #a " = " << (a) << endl using vll = vector<ll>; using pll = pair<ll, ll>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double PI = 3.1415926535897932384626433832795028; constexpr ll DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; constexpr ll DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; ll sign(ll a) { return (a > 0) - (a < 0); } ll cdiv(ll a, ll b) { return (a - 1 + b) / b; } template <typename T> T sq(T a) { return a * a; } template <typename T, typename U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <typename T, typename U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) { os << "(" << a.first << ", " << a.second << ")"; return os; } template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) { auto [t, u, v] = a; os << "(" << t << ", " << u << ", " << v << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) { os << "{"; for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr ll PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; int main() { ll n; cin >> n; string s; cin >> s; if (s[0] != s[s.length() - 1]) { cout << 1 << endl; return 0; } rep(i, s.length() - 1) { if (s[i] != s[0] && s[i + 1] != s[0]) { cout << 2 << endl; return 0; } } cout << -1 << endl; }
#include<bits/stdc++.h> using namespace std; #define int long long #define fo(i,a,b) for(int i = a; i<b ; i++) #define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define pb push_back #define M 1000000007 int mod(int x){ return ((x%M + M)%M); } int add(int a,int b){ return mod(mod(a)+mod(b)); } int mul(int a,int b){ return mod(mod(a)*mod(b)); } // **************************************************************************** void solve(){ int n; cin>>n; string s; cin>>s; if(n==1){ cout<<"-1\n"; return; } if(s[0]!=s[n-1]){ cout<<"1\n"; return; } bool f = false; fo(i,1,n-2){ if(s[i]!=s[0] && s[i+1]!=s[0]) f=true; } if(f) cout<<"2\n"; else cout<<"-1\n"; } signed main() { FIO int t; t=1; // cin>>t; while(t--) { solve(); } }
#include<bits/stdc++.h> #define R register #define ll long long using namespace std; const int MAXN = 1e5+5; ll n,k,m,a[MAXN],b[MAXN],ans; struct node { int id; bool operator < (const node &x)const { return b[id]*n-m*a[id]>b[x.id]*n-m*a[x.id]; } }; bool check(ll x) { ll tot=0; for(R int i=1;i<=k;++i) { ll num=m*a[i]-x; if(num<0) continue; { if(n*num/n>=num&&n*num/n<=num+2*x) tot+=num/n; else if(n*(num/n+1)>=num&&n*(num/n+1)<=num+2*x) tot+=num/n+1; else return false; } } return tot<=m; } void solve(ll x) { ll tot=0; for(R int i=1;i<=k;++i) { ll num=m*a[i]-x; if(num<0) b[i]=0; else { if(n*num/n>=num&&n*num/n<=num+2*x) b[i]=num/n; else if(n*(num/n+1)>=num&&n*(num/n+1)<=num+2*x) b[i]=num/n+1; } tot+=b[i]; } ll d=m-tot; for(R int i=1;i<=k;++i) b[i]+=d/k; d%=k; for(R int i=1;i<=k;++i) { if(!d) break; while(abs((b[i]+1)*n-m*a[i])<=ans) { --d; b[i]++; } } priority_queue <node> q; for(R int i=1;i<=k;++i) q.push(node{i}); while(d--) { b[q.top().id]++; q.pop(); } for(R int i=1;i<=k;++i) printf("%lld ",b[i]); } int main() { scanf("%lld %lld %lld",&k,&n,&m); for(R int i=1;i<=k;++i) scanf("%d",&a[i]); ll l=0,r=n*m; ans=n*m; while(l<=r) { ll mid=l+r>>1; if(check(mid)) { ans=mid; r=mid-1; } else l=mid+1; } solve(ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; std::mt19937 rnd(time(0)); inline int sj(int n) { unsigned int x=rnd(); return x%n+1; } #define rand fst template<typename typC> void read(register typC &x) { register int c=getchar(),fh=1; while ((c<48)||(c>57)) { if (c=='-') {c=getchar();fh=-1;break;} c=getchar(); } x=c^48;c=getchar(); while ((c>=48)&&(c<=57)) { x=x*10+(c^48); c=getchar(); } x*=fh; } template<typename typC, typename... Args> void read(typC &first, Args& ... args) { read(first); read(args...); } template<typename typC> void read(register typC *a,int num) { for (register int i=1;i<=num;i++) read(a[i]); } template<typename typC> void write(register typC x) { if (x<0) putchar('-'),x=-x; static int st[100]; register int tp=1; register typC y;st[1]=x%10;x/=10; while (x) y=x/10,st[++tp]=x-y*10,x=y;++tp; while (--tp) putchar(st[tp]|48); } template<typename typC> void write(register typC *a,register int num) { for (register int i=1;i<=num;i++) write(a[i]),putchar(i==num?10:32); } template<typename typC> typC ab(register typC x) { if (x<0) return -x; return x; } #define space(x) write(x),putchar(32) #define enter(x) write(x),putchar(10) const int p=998244353; const db eps=1e-9; inline void inc(register int &x,const int y) { if ((x+=y)>=p) x-=p; } inline void dec(register int &x,const int y) { if ((x-=y)<0) x+=p; } inline int ksm(register int x,register int y) { register int r=1; while (y) { if (y&1) r=(ll)r*x%p; x=(ll)x*x%p; y>>=1; } return r; } priority_queue<int> ONLYYFORRCOPYY; priority_queue<int,vector<int>,greater<int> > ONLYYFORRCOPYY__; struct Q { int u,v; Q(int a=0,int b=0):u(a),v(b){} bool operator<(const Q &o) const {return v<o.v;} }; typedef pair<ll,int> pa; const int N=1e6+2,M=1e6+2,inf=1e9; char s[N]; int a[N],b[N]; pa c[N]; int T,n,m,i,j,k,x,y,z,ans,la,ksiz,ks; int main() { //ios::sync_with_stdio(false); cout<<setiosflags(ios::fixed)<<setprecision(15); T=1; read(k,n,m);read(a,k); for (i=1;i<=k;i++) ans+=(b[i]=(ll)a[i]*m/n),c[i]=pa((ll)a[i]*m-(ll)b[i]*n,i); ans=m-ans; sort(c+1,c+k+1);reverse(c+1,c+k+1); for (i=1;i<=ans;i++) ++b[c[i].second]; write(b,k); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1000000000; void chmax(int &a, int b) { if(b>a) a = b; } void chmin(int &a, int b) { if(b<a) a = b; } int ctoi(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } bool cisi(char c) { switch (c) { case '0': return 1; case '1': return 1; case '2': return 1; case '3': return 1; case '4': return 1; case '5': return 1; case '6': return 1; case '7': return 1; case '8': return 1; case '9': return 1; default: return 0; } } string hantei(ll N) { if(N%2 == 1) return "Odd"; else if(N%4 == 0) return "Even"; else return "Same"; } int main() { ll T; cin >> T; for(int i=0; i<T; i++) { ll N; cin >> N; string ans = hantei(N); cout << ans << endl; } }
#include<bits/stdc++.h> //#define int long long #define pii pair<int,int> #define F first #define S second using namespace std; int n,x,ans; main(){ ios::sync_with_stdio(0),cin.tie(0); cin>>n; for(int i=0;i<n;i++){ cin>>x; if(x<=10)continue; ans+=(x-10); } cout<<ans; }
#include <bits/stdc++.h> using namespace std; #define ar array #define ll long long const int MAX_N = 1e5 + 1; const ll MOD = 1e9 + 7; const ll INF = 1e9; 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); } }; void output_vector(vector<ll>& v) { for(ll i=0;i<v.size()-1;i++) { cout<<v[i]<<" "; } cout<<v.back()<<"\n"; } ll getSol(vector<vector<ll>>& v, ll i, ll j, ll turn, ll n, ll m, vector<vector<ll>>& dp) { if((i == n-1) && (j == m-1)) { return 0; } if(dp[i][j] != -1) { return dp[i][j]; } if(turn == 0) { ll p1 = INT_MIN; if((i + 1) < n) { p1 = v[i+1][j] + getSol(v, i+1, j, turn^1, n, m, dp); } ll p2 = INT_MIN; if((j+1) < m) { p2 = v[i][j+1] + getSol(v, i, j+1, turn^1, n, m, dp); } return dp[i][j] = max(p1, p2); } else { ll p1 = INT_MAX; if((i + 1) < n) { p1 = -v[i+1][j] + getSol(v, i+1, j, turn^1, n, m, dp); } ll p2 = INT_MAX; if((j+1) < m) { p2 = -v[i][j+1] + getSol(v, i, j+1, turn^1, n, m, dp); } return dp[i][j] = min(p1, p2); } } void solve() { ll n, m; cin>>n>>m; char pp; vector<vector<ll>> v(n, vector<ll>(m, 0)); for(ll i=0;i<n;i++) { for(ll j=0;j<m;j++) { cin>>pp; if(pp == '+') { v[i][j] = 1; } else { v[i][j] = -1; } } } vector<vector<ll>> dp(n, vector<ll>(m, -1)); ll ppp = getSol(v, 0, 0, 0, n, m, dp); if(ppp == 0) { cout<<"Draw\n"; } else if(ppp > 0) { cout<<"Takahashi\n"; } else { cout<<"Aoki\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { // cout << "Case #" << t << ": "; solve(); } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define ff first #define ss second #define rsz resize #define ins insert #define mp make_pair #define pf push_front #define pb push_back #define eb emplace_back #define ft front() #define bk back() #define sz(x) (int)(x).size() #define all(x) begin(x), end(x) #define rep(i,a,b) for (int i = (a); i < (b); ++i) #ifndef ONLINE_JUDGE #define deb(x) cerr<< #x <<" "; __print(x); cerr<<endl; #else #define deb(x) #endif const ll mod = 1e9+7; // 998244353; const ll inf = 1e18; const ld pie = acos((ld)-1); ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; void __print(int x) {cerr<<x;} void __print(char x) {cerr<<x;} void __print(string x) {cerr<<x;} void __print(float x) {cerr<<x;} void __print(double x) {cerr<<x;} void __print(bool x) {cerr<<x;} void __print(ll x) {cerr<<x;} template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";} template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; } template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";} template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";} template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";} template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";} template<typename... T> void read(T&... args) { ((cin>>args),...);} template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';} int N,M; int main(){ scanf("%d%d", &N, &M); vector<vector<int>> arr(N, vector<int>(M, 0)); rep(i, 0, N) { string s; cin>>s; for(int j = 0; j < M; ++j) { if(s[j] == '+') { arr[i][j] = 1; } else { arr[i][j] = -1; } } } vector<vector<int>> dp(N, vector<int>(M, 0)); for(int i = N - 1; i >= 0; --i) { for(int j = M - 1; j >= 0; --j) { if(i == N - 1 && j == M - 1) { dp[i][j] = 0; } else if(i == N - 1) { dp[i][j] = arr[i][j + 1] - dp[i][j + 1]; } else if(j == M - 1) { dp[i][j] = arr[i + 1][j] - dp[i + 1][j]; } else { dp[i][j] = max(arr[i + 1][j] - dp[i + 1][j], arr[i][j + 1] - dp[i][j + 1]); } } } if(dp[0][0] > 0) { puts("Takahashi"); } else if(dp[0][0] < 0) { puts("Aoki"); } else { puts("Draw"); } return 0; /*STUFF TO LOOK: 1. Check the constraints 2. Corner cases(n==0||n==1) 3. Variables in loops 4. Make sure two ints aren’t multiplied to get a long long 5. Don't use scanf or printf while using ios_base 6. using the local variable of the same name when global variable was required to be used */ }
#include <bits/stdc++.h> using namespace std; int main(){ int L; cin >> L; long long ans = 1; for(int i = 0; i < 11; i++){ ans *= L-1-i; ans /= i+1; } cout << ans << endl; }
#include <bits/stdc++.h> #define INF INT_MAX-1 // #define INF LLONG_MAX-1 using namespace std; typedef long long ll; typedef pair<ll,ll> pi; // vector<vector<ll>> vv(n, vector<ll>(m)); int main(){ string S; cin >> S; for(ll i=0; i<S.size(); i++){ if(i%2 == 0){ if(S[i] >= 'A' && S[i] <= 'Z'){ cout << "No" << endl; return 0; } }else{ if(S[i] >= 'a' && S[i] <= 'z'){ cout << "No" << endl; return 0; } } } cout << "Yes" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; const int inf = 1 << 29; const ll infll = 1LL << 60; const int mod = 1000000007; const int mod9 = 998244353; 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; } int main(){ int h, w, a, b; cin >> h >> w >> a >> b; vector<pair<int, int>> s; if(w > 0){ for(int i=1; i<w; i++){ for(int j=0; j<h; j++){ s.push_back(make_pair(i + w * j, i + w * j + 1)); } } } if(h > 0){ for(int j=0; j<h-1; j++){ for(int i=1; i<=w; i++){ s.push_back(make_pair(w * j + i, w * (j+1) + i)); } } } if(h == 1 && w == 1){ cout << 1 << endl; return 0; } int n = s.size(); vector<bool> used(h * w, false); int ans = 0; for(int i=0; i<(1 << n); i++){ int m = 0; for(int j=0; j<n; j++){ if(i & (1 << j)) m++; } if(m == a){ used.assign(h * w, false); for(int j=0; j<n; j++){ if(i & (1 << j)){ if(used[s[j].first-1]) break; else used[s[j].first-1] = true; if(used[s[j].second-1]) break; else used[s[j].second-1] = true; } if(j == n-1) ans++; } } } cout << ans << endl; }
#include "bits/stdc++.h" #define REP(i,num) for(ll i=0;i<(num);++i) #define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i)) #define LOOP(i) while(i--) #define ALL(c) c.begin(),c.end() #define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl; #define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;} using namespace std; using ll = long long; using ull = unsigned long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; constexpr ll atcoder_mod = 1e9+7; template<typename T=ll> T in(){ T x; cin >> x; return (x); } template<typename T=ll,typename C=vector<T>> C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; } template<typename T=ll,size_t N> array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;} void vout(){ cout << endl; } template<typename Head,typename... Tail> void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); } void out(){ cout << endl; } template<typename Head,typename... Tail> void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); } template<typename T> bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; } template<typename T> bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; } ll dfs(ll H,ll W,ll B,ll u,ll v,ll b,vector<ll> M){ ll sum=0; if(b==B){ return 1ll; } if(u+1<W){ if(!M[u+v*W] && !M[u+1+v*W]){ M[u+v*W]=M[u+1+v*W]=1; if(u+1<W){ sum += dfs(H,W,B,u+1,v,b+1,M); } else{ if(v+1<H){ sum += dfs(H,W,B,0,v+1,b+1,M); } } M[u+v*W]=M[u+1+v*W]=0; } } if(v+1<H){ if(!M[u+v*W] && !M[u+(v+1)*W]){ M[u+v*W]=M[u+(v+1)*W]=1; if(u+1<W){ sum += dfs(H,W,B,u+1,v,b+1,M); } else{ if(v+1<H){ sum += dfs(H,W,B,0,v+1,b+1,M); } } M[u+v*W]=M[u+(v+1)*W]=0; } } if(u+1<W){ sum += dfs(H,W,B,u+1,v,b,M); } else if(v+1<H){ sum += dfs(H,W,B,0,v+1,b,M); } return sum; } int main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); auto [H,W,B,A] = arrin<ll,4>(); vector<ll> M(H*W,0); auto sum = dfs(H,W,B,0,0,0,M); out(sum); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define all(a) (a).begin(),(a).end() #define F first #define S second #define sz(x) (int)x.size() #define endl '\n' #define inf INT_MAX #define rep(i,a,b) for(i=a;i<b;i++) template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; } template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; } const int N = 2e5 + 5; const ll mod = 1e9 + 7; void solve() { ll n, i, ans = 2; cin >> n; vector<ll> v; n = n * 2; for(i = 2 ; i*i <= n ; i++) { if(n % i == 0) v.pb(i); } for(auto i : v) { ll l, r, a, b; a = i; b = n / i; l = (b - 1 - a)/2; r = (a + b - 1)/2; if((b - 1 - a) & 1 || (a + b - 1) & 1) continue; l = (b - 1 - a)/2; r = (a + b - 1)/2; if(l >= 0) ans += 2; } cout << ans; } int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ll long long #define pb push_back #define vec vector<ll> #define pii pair<ll,ll> #define pr_que priority_queue<pii,vector<pii>,greater<pii> > #define itr(i,x,y) for(ll i=x;i<y;i++) #define mod 1000000007 const long long N=200001,INF=2000000000000000000; const double err=1e-9; ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } int main() { IOS; ll n,ans=INF; cin >> n; int a[n]; itr(i,0,n) { cin >> a[i]; } int arr[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; for(int mask=0;mask<(1<<15);mask++) { ll tmp=1,c=0; for(int i=0;i<15;i++) { if(mask&(1<<i)) { tmp*=arr[i]; } } for(int i=0;i<n;i++) { if(gcd(a[i],tmp)==1) { c=1; break; } } if(c==0) ans=min(ans,tmp); } cout << ans; return 0; }
#include <bits/stdc++.h> #define F first #define S second #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define per(i, a, b) for(int i = (b)-1; i >= (a); --i) #define bck(i, a, b) if ((i) >= (a) && (i) < (b)) #define trav(x, a) for (auto &x : (a)) #define sz(a) (int)(a).size() #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long ll; typedef string str; template<typename T> using vec = vector<T>; template<typename T> using pq = priority_queue<T, vector<T>, std::greater<T>>; template<typename T> using mxpq = priority_queue<T>; typedef pair<int,int> pii; typedef vec<int> vi; typedef vec<pii> vii; typedef vec<vi> vvi; typedef pair<ll,ll> pll; typedef vec<ll> vl; typedef vec<pll> vll; typedef vec<vl> vvl; template<typename A, typename B> istream& operator>>(istream& s, pair<A,B>& p) { return s>>p.first>>p.second; } template<typename T> istream& operator>>(istream& s, vec<T>& p) { for (T& t : p) s >> t; return s; } vec<pair<pii, pii>> squares; vec<pair<pii, int>> points; vec<bool> grid(10000*10000); bool expand_up(int i) { int y = squares[i].S.S; if (y == 10000) return false; rep(x, squares[i].F.F, squares[i].S.F) { if (grid[y*10000 + x]) return false; } squares[i].S.S += 1; rep(x, squares[i].F.F, squares[i].S.F) { grid[y*10000 + x] = true; } return true; } bool expand_down(int i) { int y = squares[i].F.S; if (y == 0) return false; y -= 1; rep(x, squares[i].F.F, squares[i].S.F) { if (grid[y*10000 + x]) return false; } squares[i].F.S -= 1; rep(x, squares[i].F.F, squares[i].S.F) { grid[y*10000 + x] = true; } return true; } bool expand_left(int i) { int x = squares[i].F.F; if (x == 0) return false; x -= 1; rep(y, squares[i].F.S, squares[i].S.S) { if (grid[y*10000 + x]) return false; } squares[i].F.F -= 1; rep(y, squares[i].F.S, squares[i].S.S) { grid[y*10000 + x] = true; } return true; } bool expand_right(int i) { int x = squares[i].S.F; if (x == 10000) return false; rep(y, squares[i].F.S, squares[i].S.S) { if (grid[y*10000 + x]) return false; } squares[i].S.F += 1; rep(y, squares[i].F.S, squares[i].S.S) { grid[y*10000 + x] = true; } return true; } int area(int i) { auto &s = squares[i]; return abs(s.F.F - s.S.F) * abs(s.F.S - s.S.S); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; rep(i, 0, n) { int x, y, r; cin >> x >> y >> r; points.pb({{x, y}, r}); squares.pb({{x, y}, {x+1, y+1}}); grid[y*10000 + x] = true; } vec<bool> up(n, true), left(n, true), down(n, true), right(n, true); while (true) { bool done = true; rep(i, 0, n) { if (area(i) < points[i].S) { up[i] = up[i] && expand_up(i); left[i] = left[i] && expand_left(i); down[i] = down[i] && expand_down(i); right[i] = right[i] && expand_right(i); } else { up[i] = left[i] = down[i] = right[i] = false; } done = done && !up[i] && !left[i] && !left[i] && !right[i]; } if (done) break; } trav(x, squares) { printf("%d %d %d %d\n", x.F.F, x.F.S, x.S.F, x.S.S); } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll ,ll> pll; #define F first #define S second #define pb push_back #define mp make_pair #define all(X) (X).begin(), (X).end() #define REP(i,x,y) for(ll i = x;i <= y;++i) #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) vector<string> vec_splitter(string s) { for(char& c: s) c = c == ','? ' ': c; stringstream ss; ss << s; vector<string> res; for(string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> args, int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if(idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) void localTest() { #ifndef ONLINE_JUDGE freopen("inp", "r", stdin); freopen("out", "w", stdout); #endif } const int N = 2e3 + 5; int A[N][N], dp[N][N][2], n, m; bool check(int x, int y) { return (x >= 1 and x <= n and y >= 1 and y <= m); } int doit(int x, int y, int z) { if(x == n and y == m) { return 0; } int &ret = dp[x][y][z]; if(ret != -1) { return ret; } int ans = 0, v1 = -1e9, v2 = -1e9, inf = -1e9; if(check(x + 1, y)) { v1 = A[x + 1][y] - doit(x + 1, y, z ^ 1); } if(check(x, y + 1)) { v2 = A[x][y + 1] - doit(x, y + 1, z ^ 1); } ans = max(v1, v2); // debug(x, y, v1, v2); if(ans == inf) { ans = 0; } return ret = ans; } void solver() { memset(dp, -1, sizeof dp); cin >> n >> m; string s[n]; for(int i = 0; i < n; ++i) { cin >> s[i]; for(int j = 0; j < m; ++j) { A[i + 1][j + 1] = 1; if(s[i][j] == '-') { A[i + 1][j + 1] = -1; } } } int ans = doit(1, 1, 0); if(n == 1 and m == 1) { ans = 0; } if(ans > 0) { cout << "Takahashi\n"; } else if(ans < 0) { cout << "Aoki\n"; } else { cout << "Draw\n"; } return; } int main() { localTest(); fastio; int t = 1; // cin >> t; while(t--) { solver(); } return 0; }
#include<iostream> #include<vector> #include<queue> #include<cstring> #include<cmath> #include<map> #include<set> #include<cstdio> #include<algorithm> #define debug(a) cout<<#a<<"="<<a<<endl; using namespace std; const int maxn=880; typedef int LL; inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();} return x*f;} LL a[maxn][maxn],sum[maxn][maxn]; LL arr[maxn*maxn],tot=0; LL n,k; bool check(LL val){ for(LL i=1;i<=n;i++){ for(LL j=1;j<=n;j++){ sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(a[i][j]>=val); } } for(LL i=k;i<=n;i++){ for(LL j=k;j<=n;j++){ LL x2=i;LL y2=j; LL x1=i-k+1;LL y1=j-k+1; LL res=sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]; if(res<(k*k/2)+1) return false; } } return true; } int main(void){ cin.tie(0);std::ios::sync_with_stdio(false); cin>>n>>k; for(LL i=1;i<=n;i++){ for(LL j=1;j<=n;j++){ cin>>a[i][j]; arr[++tot]=a[i][j]; } } sort(arr+1,arr+1+tot); LL l=1;LL r=tot; while(l<r){ LL mid=(l+r+1)>>1; if(check(arr[mid])) l=mid; else r=mid-1; } cout<<arr[l]<<"\n"; return 0; }
//Never stop trying #include "bits/stdc++.h" using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) typedef long long ll; #define int ll typedef string str; typedef long double ld; typedef pair<int, int> pi; #define fi first #define se second typedef vector<int> vi; typedef vector<pi> vpi; #define pb push_back #define eb emplace_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define endl "\n" #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) const int MOD = 1e9 + 7; //998244353 const ll INF = 1e18; const int MX = 800 + 10; const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up template<class T> using V = vector<T>; template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up //constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x)) mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); //mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); ll random(ll a, ll b){ return a + rng() % (b - a + 1); } #ifndef LOCAL #define cerr if(false) cerr #endif #define dbg(x) cerr << #x << " : " << x << endl; #define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl; #define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl; #define here() cerr << "here" << endl; void IO() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } /////////////////////////ONLY CLEAN CODES ALLOWED///////////////////////// int N,K,g[MX][MX]; bool check(int k){ int a[N][N]; FOR(i,0,N) FOR(j,0,N) a[i][j]=(g[i][j]<=k); FOR(i,0,N) FOR(j,0,N){ if(i) a[i][j]+=a[i-1][j]; if(j) a[i][j]+=a[i][j-1]; if(i&&j) a[i][j]-=a[i-1][j-1]; } FOR(i,K-1,N) FOR(j,K-1,N){ int x=a[i][j]; if(i-K>=0) x-=a[i-K][j]; if(j-K>=0) x-=a[i][j-K]; if(i-K>=0 && j-K>=0) x+=a[i-K][j-K]; if(x>=(K*K+1)/2) return 1; } return 0; } int32_t main() { boost; IO(); cin>>N>>K; FOR(i,0,N) FOR(j,0,N) cin>>g[i][j]; int l=0,r=INF,ans; while(l<=r){ int m=(l+r)/2; if(check(m)){ ans=m; r=m-1; } else l=m+1; } cout << ans << endl; return 0; } //Change your approach
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; std::vector<int> prime_sieve(int n, bool get_only_prime) { std::vector<int> prime, smallest_prime_factor(n + 1); std::iota(smallest_prime_factor.begin(), smallest_prime_factor.end(), 0); for (int i = 2; i <= n; ++i) { if (smallest_prime_factor[i] == i) prime.emplace_back(i); for (int p : prime) { if (i * p > n || p > smallest_prime_factor[i]) break; smallest_prime_factor[i * p] = p; } } return get_only_prime ? prime : smallest_prime_factor; } struct osa_k { std::vector<int> smallest_prime_factor; osa_k(int n = 10000000) : smallest_prime_factor(prime_sieve(n, false)) {} std::vector<std::pair<int, int>> query(int n) const { std::vector<std::pair<int, int>> res; while (n > 1) { int prime = smallest_prime_factor[n], exponent = 0; while (smallest_prime_factor[n] == prime) { ++exponent; n /= prime; } res.emplace_back(prime, exponent); } return res; } }; constexpr int M = 1000000; vector<int> factor[M + 1]{}; int main() { osa_k osa(M); for (int i = 1; i <= M; ++i) { for (auto [f, _] : osa.query(i)) factor[i].emplace_back(f); } map<pair<int, int>, ll> m; auto f = [&](int l, int r) -> ll { if (m.count({l, r}) == 1) return m[{l, r}]; m[{l, r}] = 0; for (int i = l; i <= r; ++i) { REP(b, 1 << factor[i].size()) { int p = 1; REP(j, factor[i].size()) { if (b >> j & 1) p *= factor[i][j]; } m[{l, r}] += (i / p - (l - 1) / p) * (__builtin_popcount(b) & 1 ? -1 : 1); } } return m[{l, r}]; }; int l, r; cin >> l >> r; ll ans = 0; for (int g = 2; r / g >= 2; ++g) { int x = max((l + g - 1) / g, 2), y = r / g; if (x <= y) ans += f(x, y); } cout << ans * 2 << '\n'; return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n,m; cin>>n>>m; string s; vector<pair<int,string>> vec; int i,j; for(i=0;i<m;i++){ cin>>s; bool add=true; for(j=0;j<vec.size();j++){ if(s.size()<=vec[j].second.size()){ if(vec[j].second.find(s)==std::string::npos) continue; (vec[j].first)++; add=false; break; }else{ if(s.find(vec[j].second)==std::string::npos) continue; (vec[j].first)++; vec[j].second=s; add=false; break; } } if(add){ vec.push_back({0,s}); } } sort(vec.begin(),vec.end()); string ans[20]; for(i=vec.size()-1;i>=0; i--){ int len=vec[i].second.size(); for(j=0;j<20;j++){ if(ans[j].size()+len<=20){ ans[j]+=vec[i].second; break; } } } for(i=0;i<20;i++){ for(j=ans[i].size();j<20;j++){ ans[i]+='.'; } cout<<ans[i]<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; long long tmp_or,tmp_xor,ans; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) cin >> A.at(i); ans = pow(2,32); for (int bit = 0; bit < (1<<(N-1)); bit++) { tmp_or = 0, tmp_xor = 0; for (int i = 0; i < N; i++) { if (i == 0) tmp_or = A.at(i); else { if (bit & (1<<(i-1))) { tmp_xor ^= tmp_or; tmp_or = A.at(i); } else tmp_or |= A.at(i); } } tmp_xor ^= tmp_or; if (ans > tmp_xor) ans = tmp_xor; } cout << ans << endl; }
#include <bits/stdc++.h> #include <chrono> using namespace std; using namespace chrono; typedef long long int ll; typedef vector<int> vii; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define pb push_back #define odd(x) ((x)&1) #define even(x) (!odd(x)) #define all(v) (v).begin(),(v).end() #define rep(i,n) for(auto i=0;i<n;++i) #define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define TEST_CASE int tc;cin>>tc;while(tc--) #define Clock high_resolution_clock::now() 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;} #ifdef LOCAL #define cerr cout #else #endif #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // #define TIME /******************************************************************************************************************************/ const ll inf = 2e18; const ll mod = 998244353; const ll N = 1e7+3; int main() { auto start_time = Clock; FASTIO int n; cin >> n; if(n%100 == 0) cout << n/100; else { cout << (n/100 + 1); } auto end_time = Clock; #ifndef TIME return 0; #endif cout << "\nTime elapsed: " << (double)duration_cast<milliseconds>(end_time-start_time).count() << "ms"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const LL mod = 1e9 + 7; const int N = 3005; LL f[N][N], a[N]; LL pre[N]; LL g[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); pre[i] = pre[i - 1] + a[i]; } LL ans = 0; f[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) g[j] = 0; for (int j = 0; j <= n; j++) { f[i][j] = g[pre[j] % i] % mod; g[pre[j] % i] += f[i - 1][j]; } ans += f[i][n]; } cout << ans % mod << endl; return 0; }
// UTF−8 #include<bits/stdc++.h> using namespace std; using ll = long long int; using lc = complex<double>; template <class T> bool complex_comparator(const complex<T> &a, const complex<T> &b) { return real(a) == real(b) ? imag(a) < imag(b) : real(a) < real(b); } /* #include<atcoder/all> */ /* using namespace atcoder; */ template<class T>bool chmax(T &a, const T &b) { return (a<b ? (a=b,1) : 0); } template<class T>bool chmin(T &a, const T &b) { return (a>b ? (a=b,1) : 0); } int main(void) { constexpr ll MOD = 1 ? 1e9+7 : 998244353; const double PI = acos(-1); constexpr double eps = 1e-5; cout << fixed << setprecision(32); cin.tie(0); ios::sync_with_stdio(false); if(1) { ll n; cin >> n; vector<ll> a(n); for(auto &e: a) cin >> e; vector<ll> d(n, 1); ll res = 0; res++; // 全部一緒 for(ll i=2; i<=n; i++) { map<ll,vector<ll>> cu; vector<ll> e(n); { ll x = 0; for(ll j=0; j<n; j++) cu[((x+=a[j])%=i)].push_back(j); } for(auto &[k, v]: cu) { ll x = 0; for(ll j=0; j<v.size(); j++) { ll t2 = v[j]; (e[t2] += x) %= MOD; if(t2 == n-1) (res += x) %= MOD; x += d[v[j]]; } } // for(auto &x: e) cerr << x << ' '; cerr << endl; swap(d, e); } cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long int N, cnt=0; cin >> N; vector<long long int> a(N+1), b(N+1), c(N+1), d(N+1); for(long long int i=1; i<N+1; i++){ cin >> a.at(i); } for(long long int j=1; j<N+1; j++){ cin >> b.at(j); } for(long long int k=1; k<N+1; k++){ cin >> c.at(k); d.at(b.at(c.at(k)))++; } for(long long int l=1; l<N+1; l++){ cnt = cnt + d.at(a.at(l)); } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int lld; typedef pair<int,int> pi; typedef pair<lld,lld> pl; typedef pair<int,lld> pil; typedef pair<lld,int> pli; typedef vector<int> vit; typedef vector<vit> vitt; typedef vector<lld> vlt; typedef vector<vlt> vltt; typedef vector<pi> vpit; typedef vector<vpit> vpitt; typedef long double ld; #define x first #define y second #define pb push_back #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define mk(a,b) make_pair(a,b) bool isrange(int y,int x,int n,int m){ if(0<=y&&y<n&&0<=x&&x<m) return true; return false; } int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1}; bool can(string& x,string& y){ int st = 0; for(int p=0;p<sz(x);p++){ if(st==sz(y)) return true; if(x[p]==y[st]) st++; } return (st==sz(y)); } void solve(int tc){ int n; string a[3],b[3]; cin >> n >> a[0] >> a[1] >> a[2]; b[0] = a[0] + a[0]; b[1] = a[1] + a[1]; b[2] = a[2] + a[2]; string f1 = ""; string f2 = ""; for(int e=0;e<n;e++) f1 += "0"; for(int e=0;e<n;e++) f1 += "1"; for(int e=0;e<n;e++) f2 += "1"; for(int e=0;e<n;e++) f2 += "0"; { string f3 = "0" + f1; bool suc = true; for(int p=0;p<3;p++) if(!can(b[p],f3)) suc = false; if(suc){ cout << f3 << "\n"; return; } } { string f3 = f1 + "0"; bool suc = true; for(int p=0;p<3;p++) if(!can(b[p],f3)) suc = false; if(suc){ cout << f3 << "\n"; return; } } { string f3 = "1" + f2; bool suc = true; for(int p=0;p<3;p++) if(!can(b[p],f3)) suc = false; if(suc){ cout << f3 << "\n"; return; } } { string f3 = f2 + "1"; bool suc = true; for(int p=0;p<3;p++) if(!can(b[p],f3)) suc = false; if(suc){ cout << f3 << "\n"; return; } } } int main(void){ ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tc = 1; cin >> tc; for(int test_number=1;test_number<=tc;test_number++){ solve(test_number); } return 0; }
#include<iostream> #include<bits/stdc++.h> using namespace std; int prim[10001]; void init(){ prim[0]=1;prim[1]=1; for(int i=2;i*i<=1e4;i++){ if(prim[i]==0){ for(int j=i*i;j<=1e4;j+=i){ prim[j]=1; } } } return; } int main(){ int n;cin>>n; set<int>s; s.insert(6);s.insert(10);s.insert(15); int val=12;int cnt=3; while(val<=10000 && cnt<n){ if(s.find(val)==s.end()){ s.insert(val);cnt++; } val+=6; } val=20; while(val<=10000 && cnt<n){ if(s.find(val)==s.end()){ s.insert(val);cnt++; } val+=10; } val=30; while(val<=10000 && cnt<n){ if(s.find(val)==s.end()){ s.insert(val);cnt++; } val+=15; } for(auto it:s) cout<<it<<" "; cout<<endl; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstring> #include <iostream> #include <sstream> #include <numeric> #include <map> #include <set> #include <queue> #include <vector> using namespace std; typedef long long LL; string solve(long long N) { switch (N % 4) { case 0: return "Even"; case 2: return "Same"; default: return "Odd"; } } string solve2(long long N) { LL even = 0, odd = 0; for (LL i = 1; i <= N; ++i) { if ((N % i) == 0) { (((i % 2) == 0) ? even : odd) += 1; } } if (even > odd) { return "Even"; } if (odd > even) { return "Odd"; } return "Same"; } int main() { long long T, N; std::cin >> T; for (int i = 0; i < T; i++) { std::cin >> N; cout << solve(N) << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define dd double #define endl "\n" #define pb push_back #define all(v) v.begin(),v.end() #define mp make_pair #define fi first #define se second #define vll vector<ll> #define pll pair<ll,ll> #define fo(i,n) for(int i=0;i<n;i++) #define fo1(i,n) for(int i=1;i<=n;i++) ll mod=1000000007; ll n,k,t,m,q,flag=0; ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a // s.find_by_order(i) -- itertor to ith element (0 indexed) ll min(ll a,ll b){if(a>b)return b;else return a;} ll max(ll a,ll b){if(a>b)return a;else return b;} ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;} int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef NOOBxCODER freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else #define NOOBxCODER 0 #endif //cin>>t; t=1; while(t--){ cin>>k; cin>>n>>m; ll a[k]; fo(i,k)cin>>a[i]; ll sum=0; pll p[k]; fo(i,k){ p[i]= mp((m*a[i])%n , i); sum+= (m*a[i])%n; } //cout<<sum<<endl; sort(p,p+k); ll val = sum/n; //cout<<val; ll ans[k]; fo(i, k){ ll j = p[i].se; //cout<<j<<endl; //cout<<a[j]<<endl; if(i<k-val)ans[j]= m*a[j]/n; else ans[j] =m*a[j]/n + 1; } fo(i,k)cout<<ans[i]<<" "; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include<bits/stdc++.h> #define int long long int const int M = 998244353; const int mod = 1e9+7; const int inf = 1e18+5; using namespace std; #define debug() for(int l=0;l<50;l++){} #define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0); int bs(int arr[], int l, int r, int x){ int mid=0; if (r >= l){ if (arr[mid] == x) return mid; if (arr[mid] > x) return bs(arr, l, mid - 1, x); return bs(arr, mid + 1, r, x); } return -1; } int power(int x,int y,int p){ int res = 1; x = x % p; if(x == 0) return 0; while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } const int max1=500001; pair<int,int>st[4*max1]; int arr[max1],arr1[max1]; void buildtree(int si,int ss,int se){ if(ss==se){ st[si].first=arr1[ss]; st[si].second=arr1[ss]; return; } int mid=ss+(se-ss)/2; buildtree(2*si,ss,mid); buildtree(2*si+1,mid+1,se); if(arr1[mid]>arr1[mid+1]) st[si].first=st[2*si].first+st[2*si+1].first; else st[si].second=st[2*si].second+st[2*si+1].second; } int query(int si,int ss,int se,int qs,int qe){ if(qs>se || qe<ss) //completely outside return inf; if(ss>=qs && se<=qe) return st[si].first; int mid=ss+(se-ss)/2; int l=query(2*si,ss,mid,qs,qe); int r=query(2*si+1,mid+1,se,qs,qe); return min(l,r); } int geteven(int si,int ss,int se,int qs,int qe){ if(ss>qe || se<qs) //completely outside return 0; if(ss>=qs && se<=qe) return st[si].second; int mid=ss+(se-ss)/2; int l=geteven(2*si,ss,mid,qs,qe); int r=geteven(2*si+1,mid+1,se,qs,qe); return l+r; } int getodd(int si,int ss,int se,int qs,int qe){ if(qs>se || qe<ss) //completely outside return 0; if(ss>=qs && se<=qe) return st[si].first; int mid=ss+(se-ss)/2; int l=getodd(2*si,ss,mid,qs,qe); int r=getodd(2*si+1,mid+1,se,qs,qe); return l+r; } void update(int si,int ss,int se,int qi,int newval){ if(ss==se){ if(arr[ss]%2==1) st[si]={0,1}; else st[si]={1,0}; arr[ss]=newval; return; } int mid=ss+(se-ss)/2; if(qi<=mid) update(2*si,ss,mid,qi,newval); else update(2*si+1,mid+1,se,qi,newval); st[si].first=st[2*si].first+st[2*si+1].first; st[si].second=st[2*si].second+st[2*si+1].second; } int mm(int a, int b, int m){ int res = 0; a %= m; while (b){ if (b & 1) res = (res + a) % m; a = (2 * a) % m; b >>= 1; } return res; } string solve(){ string s; cin>>s; int l=s.length(); if(l==1){ if(s[0]=='8') return "Yes\n"; } else if(l==2){ int val=(s[0]-'0')*10 + s[1]; int val2=(s[1]-'0')*10 + s[0]; if(!(val%8) || !(val2%8)) return "Yes\n"; } else{ int ch[10]={0}; for(auto i:s) ch[i-'0']++; for(int i=104;i<1000;i+=8){ int f=0,ch1[10]={0}; string str=to_string(i); for(auto j:str) ch1[j-'0']++; for(int j=0;j<10;j++) if(ch[j]<ch1[j]) f=1; if(!f) return "Yes\n"; } } return "No\n"; } signed main(){ fast; int t=1; //cin>>t; while(t--) cout<<solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ll long long #define db double #define el "\n" #define ld long double #define rep(i,n) for(int i=0;i<n;i++) #define rev(i,n) for(int i=n;i>=0;i--) #define rep_a(i,a,n) for(int i=a;i<n;i++) #define all(ds) ds.begin(), ds.end() #define ff first #define ss second #define pb push_back #define mp make_pair typedef vector< long long > vi; typedef pair<long long, long long> ii; typedef priority_queue <ll> pq; #define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> const ll mod = 1000000007; const ll INF = (ll)1e18; const ll MAXN = 1000006; ll po(ll x, ll n){ ll ans=1; while(n>0){ if(n&1) ans=(ans*x)%mod; x=(x*x)%mod; n/=2;} return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T=1; //cin >> T; while(T--){ int n; cin>>n; n*=2; vector<ii> v; ll x; rep(i,n){ cin>>x; v.pb(mp(x,i)); } sort(all(v)); int b[n]; rep(i,n/2) b[v[i].ss]=0; rep_a(i,n/2,n) b[v[i].ss]=1; int cnt = 0, f; string s = ""; rep(i,n){ if(cnt==0){ f=b[i]; } if(b[i]==f){ cnt++; s+='('; } else{ cnt--; s+=')'; } } cout<<s<<el; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back typedef long long ll; typedef pair<int, int> pi; const int maxn = 4e5 + 5; int n; int a[maxn]; pi b[maxn]; int col[maxn]; int S[maxn]; int main(){ ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for(int i = 1;i <= n * 2;i++){ cin >> a[i]; b[i] = mp(a[i], i); } sort(b + 1, b + 1 + 2 * n); for(int i = 1;i <= 2 * n;i++){ int j = b[i].se; col[j] = (i > n); } stack<int> st; for(int i = 1;i <= 2 * n;i++){ if(st.size() > 0 and col[st.top()] + col[i] == 1){ S[i] = 1; S[st.top()] = 0; st.pop(); }else{ st.push(i); } } string ans = ""; for(int i = 1;i <= 2 * n;i++){ ans.push_back(S[i] ? ')' : '('); } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i=a; i<=b; i++) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) #define sz(x) (int) x.size() #define pb push_back #define f first #define s second #define nl "\n" typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int MOD = 1e9+7; template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>; int n; vi adj[100001]; int ss[100001]; int dfs(int x){ vi swap; vi same; int dif=0; int num=1; trav(k, adj[x]){ int p=dfs(k); num+=ss[k]; if(ss[k]%2==1) swap.pb(p); else same.pb(p); } if(sz(swap)%2==0){ trav(k, same){ dif+=k; } } else{ trav(k, same){ if(k<0) dif+=k; else dif-=k; } } sort(all(swap)); rep(i, 0, sz(swap)-1){ if(i%2==0) dif+=swap[i]; else dif-=swap[i]; } ss[x]=num; return dif+1; } int main(){ ios::sync_with_stdio(false); cin.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; rep(i, 2, n){ int u; cin >> u; adj[u].pb(i); } int d=dfs(1); cout << (n+d)/2; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count()); #define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n" template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}; template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";}; void _print() {cerr << "]\n";} template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);} #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define fi first #define se second #define SZ(x) (int)((x).size()) #define pii pair<int,int> const int mx = 1e5+5; vector<int> adj[mx]; int sz[mx], dp[mx]; void dfs(int v) { vector<vector<vector<pii>>> amt(2, vector<vector<pii>>(2)); vector<pii> alternate; sz[v] = 1; for (int a : adj[v]) { dfs(a); sz[v] += sz[a]; if (dp[a] <= sz[a]/2) { int dif = sz[a]-2*dp[a]; //how much + they get if (sz[a] % 2 == 0) amt[0][0].push_back({dif,a}); else { amt[0][1].push_back({dif,a}); alternate.emplace_back(dif,a); } } else { int dif = sz[a]-2*dp[a]; //how much + they get if (sz[a] % 2 == 0) amt[1][0].push_back({dif,a}); else { amt[1][1].push_back({dif,a}); alternate.emplace_back(dif,a); } } } for (int q = 0; q < 2; q++) { for (int w = 0; w < 2; w++) { sort(amt[q][w].begin(), amt[q][w].end()); } } int tot = 0; for (pii x : amt[0][0]) { int a,b; tie(a,b) = x; int ge = (sz[b]+a)/2; tot += ge; } for (pii x : amt[1][0]) { int a,b; tie(a,b) = x; int ge = (sz[b]+a)/2; if (SZ(alternate) % 2 == 0) tot += ge; else tot += (sz[b]-ge); } sort(alternate.rbegin(), alternate.rend()); for (int q = 0; q < SZ(alternate); q++) { int a,b; tie(a,b) = alternate[q]; int ge = (sz[b]+a)/2; if (q % 2 == 0) tot += ge; else tot += (sz[b]-ge); } dp[v] = sz[v] - tot; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int q = 2; q <= n; q++) { int a; cin >> a; adj[a].push_back(q); } dfs(1); cout << dp[1] << "\n"; return 0; }
#include <iostream> #include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <functional> #include <numeric> #include <utility> #include <limits> #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <map> #define for1(x,y) for(long long x = 1; x<ll(y); x++) #define forn(x,y,z) for(long long x = y; x< ll(z); x++) #define for0(x,y,z) for(long long x = y; x< ll(z); x++) #define PII pair<int,int> #define FF first #define SS second #define PB push_back #define all(x) x.begin(), x.end() #define forn(x,y,z) for(long long x = y; x< ll(z); x++) #define for0(x,y,z) for(long long x = y; x< ll(z); x++) using namespace std; //Errichto Orz debug template #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // ends debug // aliases using ll = long long; using ull = unsigned long long; using ui = unsigned int; using ld = long double; #define vi vector<int> #define vl vector<ll> #define pb(x) push_back(x) const double EPS = (1e-7), EPSm= (1e7); const ll linf = (ll)1e17 + 5; long double PI = 3.14159265359; int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{1,1},{-1,1},{1,-1}}; const int MAXN = 1e5+10; const int NINF = -1e5; const int INF = 20000000; const ll MOD = 1e9+7; // Mathematical functions. int GCD(int a, int b) { if (!b) return a; return GCD(b, a % b); } int LCM(int a, int b) { return (a * b) / GCD(a, b); } int modpow(int x, int n, int m = MOD) { if (n == 0) return 1 % m; long long u = modpow(x, n / 2, m); u = (u * u) % m; if (n % 2 == 1) u = (u * x) % m; return u; } /** * m should be prime */ int modinv(int x, int m = MOD) { return modpow(x, m - 2, m); } //////////////////// START CODE HERE /////////////////////////// bool cmp(array<ll,2> a,array<ll,2> b){ return 2*a[0]+a[1]>2*b[0]+b[1]; } void solve() { int n; cin>>n; vector<array<ll,2>> v(n); ll cnt1=0,cnt2=0; forn(i,0,n){ cin>>v[i][0]>>v[i][1]; cnt2+=v[i][0]; } sort(all(v),cmp); int i=0,ans=0; while(cnt1<=cnt2 && i<n){ cnt1+=v[i][0]; cnt1+=v[i][1]; cnt2-=v[i][0]; i++; ans++; } //cout<<ans<<endl; cout<<ans<<endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(0); int test; //cin >> test; //while(test--) solve(); return 0; }
// D - Choose Me // 模範解答 #include <bits/stdc++.h> using namespace std; using ll = long long; using st = string; using Pii = pair<int, int>; using Cd = complex<double>; #define rep2(i, m, n) for(int i = m; i < n; i++) #define rep(i, n) rep2(i, 0, n) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define INF 1 << 30 #define RE0 return 0 const int mod = 1e9 + 7; const int di[4] = {0, 1, 0, -1}; const int dj[4] = {1, 0, -1, 0}; const int di2[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dj2[8] = {1, 0, -1, 0, 1, -1, 1, -1}; int main() { int n; cin >> n; vector<pair<ll, pair<ll, ll>>> v(n); // .first: 高橋氏がその町に演説に行くことで得られる票数 // .second.first: その町の青木派 // .second.second: その町の高橋派 for(int i = 0; i < n; i++) { cin >> v.at(i).second.first >> v.at(i).second.second; v.at(i).first = v.at(i).second.first + v.at(i).second.first + v.at(i).second.second; } ll aokisum = 0; ll takahashisum = 0; for(int i = 0; i < n; i++) { aokisum += v.at(i).second.first; } // cout << endl; // cout << aokisum << endl; sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int cnt = 0; for(int i = 0; i < n; i++) { takahashisum += v.at(i).second.first + v.at(i).second.second; aokisum -= v.at(i).second.first; // cout << takahashisum << " " << aokisum << endl; if(takahashisum <= aokisum) cnt++; else { cnt++; break; } } cout << cnt << endl; RE0; }
#include <bits/stdc++.h> #include <chrono> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; /*............................................................................*/ #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> /* find_by_order(x) give iterator of the index x order_by_key(x) give the position where x will be placed*/ #define int long long #define lop(i,a,b,c) for (int i=a;i<b;i+=c) #define rlop(i,a,b,c) for (int i=a-1;i>=b;i-=c) #define prii pair <int,int> #define PB push_back #define S second #define F first #define all(x) x.begin(),x.end() #define vvii vector < vector < int > > #define vii vector <int> #define count_1(x) __builtin_popcount(x) #define cn(a) scanf("%lld",&a); #define cn_vl(arr,n) lop(i,0,n,1){scanf("%lld",arr+i);} #define shw_vl(arr,n) lop(i,0,n,1){printf("%lld ",arr[i]);}printf("\n"); #define shw(CON) cout << &("No\0Yes"[3 * CON]) << '\n'; const int MAX=2e5+10; /*......................................................................*/ void fastscan(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); } for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative) number *= -1; } struct dsu { int parent[MAX];int size[MAX]; void init(){ lop (i,1,MAX,1){parent[i]=i;size[i]=1;} } int trace(int a){ return (a==parent[a])?a:parent[a]=trace(parent[a]); } bool connect(int a,int b){ if ((a=trace(a))==(b=trace(b)))return false; if (size[b]>size[a])swap(a,b); parent[b]=a;size[a]+=size[b]; return true; } }ori; vii adj[MAX]; typedef long double ld; void solve(){ int a,b,c;cn(a)cn(b)cn(c) if ((a>=0 and b>=0) or c%2==1){ if (a>b){ cout<<">"; } else if(a==b){ cout<<"="; } else cout<<"<"; return ; }a=abs(a);b=abs(b); if (a>b){ cout<<">"; } else if(a==b){ cout<<"="; } else cout<<"<"; return ; } int32_t main(){ int t;t=1; // cin>>t; while (t--){ auto t_1=chrono::high_resolution_clock::now(); solve(); auto t_2=chrono::high_resolution_clock::now(); // cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,(n)-1,0) #define all(v) v.begin(), v.end() #define endk '\n' const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; const ll mod2 = 998244353; const ld eps = 1e-10; template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;} template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;} int main() { cin.tie(0); ios::sync_with_stdio(false); ll a, b, c; cin >> a >> b >> c; if(c & 1) { if(a == b) cout << "=" << endk; else if(a > b) cout << ">" << endk; else cout << "<" << endk; } else { a = abs(a), b = abs(b); if(a == b) cout << "=" << endk; else if(a > b) cout << ">" << endk; else cout << "<" << endk; } return 0; }
#include <iostream> #include <string> #include <vector> #include <deque> #include <queue> #include <algorithm> #include <iomanip> #include <set> #include <map> #include <bitset> #include <cmath> #include <functional> using namespace std; #define REP(i,n) for(ll (i) = (0);(i) < (n);++i) #define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl;} #define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}} #define ALL(v) v.begin(),v.end() #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 998244353LL typedef long long ll; typedef pair<ll,ll> P; long long mod_div(long long a,long long b){ long long tmp = MOD - 2,c = b,ret = 1; while(tmp > 0){ if(tmp & 1){ ret *= c;ret %= MOD; } c *= c;c %= MOD;tmp >>= 1; } return a*ret%MOD; } #define MAX_K 333333 vector<long long> fact(MAX_K); long long combination(long long n, long long r){ if(n < r || n < 0 || r < 0) return 0; if(fact[0] != 1){ fact[0] = 1; for(long long i = 1;i < MAX_K;i++)fact[i] = (fact[i-1] * i) % MOD; } long long ret = fact[n]; long long tmp = (fact[r] * fact[n-r]) % MOD; return mod_div(ret,tmp); } void mod_add(ll &a, ll b) { a += b; a %= MOD; } ll mp[202222][333]; ll memo[333][333]; ll seica[333]; int main(){ cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); ll n, k;cin >> n >> k; REP(i, n) { ll a;cin >> a; ll tmp = 1; REP(j, k + 1) { mp[i][j] = tmp; tmp = tmp * a % MOD; } } REP(j, k + 1) { REP(i, n) { mod_add(mp[i + 1][j], mp[i][j]); } } for(ll i = 1;i <= k;i++) mod_add(seica[i], mp[n][i] * (n - 1) % MOD); for(ll i = 1;i <= k;i++) { for(ll j = 1;j <= i;j++) { if(i + j > k) continue; ll kake = combination(i + j, i); if(i == j) kake = mod_div(kake, 2); ll tmp = (MOD + mp[n][i] * mp[n][j] % MOD - mp[n][i+j]) % MOD; mod_add(seica[i + j], kake * tmp % MOD); } } for(ll i = 1;i <= k;i++) cout << seica[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int MOD=998244353; int d[305][223456]; int pw(int n,int k){ assert(k>=0); int res=1; while(k){ if(k&1)(res*=n)%=MOD; (n*=n)%=MOD; k>>=1; } return res; } std::vector<int> Factorial(5e6),Finverse(5e6); inline void Cinit(){ Factorial[0]=1; for(int i=1;i<5e6;i++)Factorial[i]=(Factorial[i-1]*i)%MOD; Finverse[4999999]=pw(Factorial[4999999],MOD-2); for(int i=4999998;i>=0;i--)Finverse[i]=(i+1)*Finverse[i+1]%MOD; } int nCk(int n,int k){ if(n<k)return 0;if(k<0)return 0; if(!Factorial[0])Cinit(); int res=Factorial[n]; (res*=Finverse[k])%=MOD; (res*=Finverse[n-k])%=MOD; return res; } signed main(){ int n,k;cin>>n>>k; vector<int> v(n); for(int i=0;i<n;i++)cin>>v[i]; for(int i=0;i<n;i++)d[0][i]=1; vector<int> sum(k+1,0); for(int i=0;i<k;i++){ for(int j=0;j<n;j++){ d[i+1][j]=d[i][j]*v[j]%MOD; } } int B=pw(2,MOD-2); for(int i=0;i<=k;i++){ for(int j=0;j<n;j++)(sum[i]+=d[i][j])%=MOD; } for(int x=1;x<=k;x++){ int ans=0; for(int i=0;i<=x;i++){ int tmp=nCk(x,i); int A=(sum[i]*sum[x-i]-sum[x])%MOD; (ans+=tmp*A)%=MOD; } if(ans<0)ans+=MOD; (ans*=B)%=MOD; cout<<ans<<endl; } }
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <unordered_map> #include <queue> #include <vector> #include <algorithm> #include <cmath> using namespace std; typedef unsigned long long ull; long long MX = 1e18; int n; int a[110]; ull x; ull calc2(long long sum, int k) { sum = x - sum; if (sum % k) return MX; return sum / k; } long long f[110][110][110]; ull calc(int k) { for (int i = 0; i <= n; ++i) for (int h = 0; h <= k; ++h) for (int j = 0; j < k; ++j) f[i][h][j] = -MX; f[0][1][a[0] % k] = a[0]; for (int i = 1; i < n; ++i) { for (int h = 2; h <= i+1 && h <= k; ++h) for (int j = 0; j < k; ++j) { f[i][h][j] = max(f[i-1][h][j], f[i-1][h-1][((j-a[i])%k+k)%k] + a[i]); } f[i][1][a[i]%k] = a[i]; for (int j = 0; j < k; ++j) { f[i][1][j] = max(f[i][1][j], f[i-1][1][j]); } } ull ans = MX; for (int j = 0; j < k; ++j) { if (f[n-1][k][j] <= 0) continue; ans = min(ans, calc2(f[n-1][k][j], k)); } return ans; } int main() { cin >> n >> x; for (int i = 0; i < n; ++i) cin >> a[i]; ull ans = MX; for (int k = 1; k <= n; ++k) { ull c = calc(k); if (c < ans) ans = c; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define all(v) begin(v), end(v) #define sz(v) v.size() #define INF 1e18 #define EPSILON 1e-14 template <typename T> bool chmax(T &a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T& b) { if (a > b) { a = b; return true; } return false; } //浮動小数点はなるべく使うな、powも気をつけろ int main() { ll N, X; cin >> N >> X; vector<ll> A(N); rep(i, N) cin >> A.at(i); ll ans = 2e18; rep(i, N){ vector<vector<vector<ll>>> dp(N+1, vector<vector<ll>>(N+1, vector<ll>(i+1, -INF))); //dp.at(j).at(k).at(l)はj番目までのうちk個使ってi+1で割った余りがlになる素材の魔力合計の最大値 rep(j, N+1){ rep(k, N+1){ rep(l, i+1){ dp.at(j).at(k).at(l) = -INF; } } } rep(j, N+1){ dp.at(j).at(0).at(0) = 0; } rep(j, N){ rep(k, i+2){ rep(l, i+1){ chmax(dp.at(j+1).at(k).at(l), dp.at(j).at(k).at(l)); if(k < i+1){ chmax(dp.at(j+1).at(k+1).at((l+A.at(j))%(i+1)), dp.at(j).at(k).at(l)+A.at(j)); } } } } if(dp.at(N).at(i+1).at(X%(i+1)) >= 0){ chmin(ans, (X - dp.at(N).at(i+1).at(X%(i+1))) / (i+1)); } } cout << ans << '\n'; }
#include<bits/stdc++.h> using namespace std; typedef int ll; unordered_map<ll,ll> ma,pre; int main(){ ios::sync_with_stdio(0); cin.tie(0); ll n,m,i,j,k,a,b,c,d,p,q,h,t,sum =0 ,maxn = 0,minx = 2e9,x,y,z,ans = 0; string str; cin>>n>>m; for(i=1;i<=n;i++){ cin>>a; b = i - pre[a]; ma[a] = max(ma[a],b); pre[a] = i; } for(i=0;i<=n;i++){ if(ma[i]==0||ma[i]>=m+1||pre[i]<=n-m){ ans = i; break; } } cout<<ans; }
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 233; int a[maxn], b[maxn]; typedef long long ll; int n, l; map<int, int> pre, suf; int main(){ scanf("%d %d", &n, &l); pre[0] = 0; suf[l - n] = n + 1; for(int i = 1; i <= n; i++)scanf("%d", &a[i]); for(int i = 1; i <= n; i++)scanf("%d", &b[i]); ll res = 0; for(int i = 1; i <= n; i++){ pre[a[i] - i] = i; if(a[i] <= b[i]) { continue; } if(pre.find(b[i] - i) == pre.end()){ puts("-1"); return 0; } res += i - pre[b[i] - i]; pre[b[i] - i] = i; } for(int i = n; i; i--){ suf[a[i] - i] = i; if(a[i] >= b[i]){ continue; } if(suf.find(b[i] - i) == suf.end()){ puts("-1"); return 0; } res += suf[b[i] - i] - i; suf[b[i] - i] = i; } printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; #define max(p,q) ((p)>(q)?(p):(q)) #define min(p,q) ((p)<(q)?(p):(q)) #define rep(i,n) for (int i = 0, i##_len = (n); i < i##_len; i++) #define rep1(i,n) for (int i = 1, i##_len = (n); i <= i##_len; i++) #define repr(i,n) for (int i = ((int)(n)-1); i >= 0; i--) #define rep1r(i,n) for (int i = ((int)(n)); i >= 1; i--) #define repl(i,l,n) for (int i = (l), i##_len = (n); i < i##_len; i++) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define RSORT(x) sort(rall(x)); #define mp make_pair #define has_key(m, k) (m.find(k) != m.end()) #define INF (1e9) #define EPS (1e-7) // 総数を1000000007(素数)で割った余り const ll mod = (1000000007); ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; } ull lcm(ull a, ull b) { return a / gcd(a, b) * b; } int main(){ string ans = ""; cin >> ans; for(auto c : ans) { if(c == '.') break; cout << c; } cout << endl; return 0; }
#include<iostream> #include<math.h> #include<stdlib.h> #include<string> #include<limits.h> #include<utility> #include<vector> //#include<bits/stdc++.h> #include<tuple> #include<queue> #include<map> #include<algorithm> //#include <bits/stdc++.h> #define rep(i,n) for(ll i=0;i<n;i++) #define ALL(A) A.begin(),A.end() using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // �e�[�u�������O���� void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // �񍀌W���v�Z long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { int t; cin >> t; rep(I, t) { int n; cin >> n; vector<ll> a(n); rep(i, n) { cin >> a[i]; } sort(ALL(a)); ll x = 0; rep(i, n) { if (i % 2 == 0) { if (a[i] != a[i + 1]) { x = 1; } } } if (x != 0) { if (n % 2 == 0) { cout << "First" << endl; } else { cout << "Second" << endl; } }else { if (n % 2 == 1) { cout << "First" << endl; } else { cout << "Second" << endl; } } } }
#include <bits/stdc++.h> //#include <bits/extc++.h> //#include <ext/pb_ds/assoc_container.hpp> // Common file //#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; //using namespace __gnu_pbds; //template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int ll using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ull = unsigned ll; #define pb push_back #define mp make_pair #define eb emplace_back #define all(x) (x).begin(),(x).end() #define x first #define y second #define PI 3.14159265 const int MOD = 1e9 + 7; //const int MOD = 998244353; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; const char dir[] = {'R', 'L', 'D', 'U'}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int add(int a, int b){ //(a + b) % 1e9 + 7 a += b; if(a < 0){ a += MOD; } if(a >= MOD){ a -= MOD; } return a; } int sub(int a, int b){ a -= b; if(a < 0) a += MOD; return a; } int mult(int a, int b){ return ((ll) a * b) % MOD; } void setIO() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen((s+".in").c_str(),"r",stdin); // freopen((s+".text").c_str(),"w",stdout); } 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> struct Seg { // comb(ID,b) = b const T ID = 0; T comb(T a, T b) { return a^b; } int n; vector<T> seg; void init(int _n) { n = _n; seg.assign(2*n,ID); } void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); } void upd(int p, T val) { // set val at position p seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); } T query(int l, int r) { // sum on interval [l, r] T ra = ID, rb = ID; for (l += n, r += n+1; l < r; l /= 2, r /= 2) { if (l&1) ra = comb(ra,seg[l++]); if (r&1) rb = comb(seg[--r],rb); } return comb(ra,rb); } }; signed main(){ setIO(); //CHECK FOR LONG LONG!!! //LONG LONG OVERFLOW?? int n, q; cin >> n >> q; Seg<int> st; st.init(n); int a[n]; for(int i = 0; i < n; ++i) { cin >> a[i]; st.upd(i, a[i]); } while(q--) { int tp; cin >> tp; if(tp == 1){ int x, y; cin >> x >> y; --x; st.upd(x, a[x]^y); a[x] ^= y; } else{ int l, r; cin >> l >> r; --l; --r; cout << st.query(l, r) << "\n"; } } //CHECK FOR LONG LONG!!! }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<n;i++) typedef long long ll; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} mint all(ll n, ll a, ll b) { mint ax = n-a+1; mint bx = n-b+1; return ax*ax*bx*bx; } mint exclude(ll n, ll a, ll b) { mint ax = n-a+1; // N内にaの区間を置くパターン mint bx = n-b+1; // N内にbの区間を置くパターン mint x4 = a+b <= n ? (n-a-b+2)*(n-a-b+1)/2 : 0; mint x3 = x4*2; mint x2 = ax*bx-x3; return x2*x2; } int main() { int t; cin >> t; REP(i,t) { ll n,a,b; cin >> n >> a >> b; cout << all(n,a,b) - exclude(n,a,b) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(int i=0;i<n;i++) #define ALL(v) v.begin(),v.end() constexpr ll MOD=998244353; constexpr ll INF=1e18; ll power(ll x, ll y){ x%=MOD; ll ret=1; while(y){ if(y&1) ret=ret*x%MOD; x=x*x%MOD; y>>=1; } return ret; } ll divid(ll x, ll y){ x%=MOD; return x*power(y,MOD-2)%MOD; } const int MAX=510000; ll fac[MAX], finv[MAX], inv[MAX]; void COMinit(){ fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for (int i=2;i<MAX;i++){ fac[i]=fac[i-1]*i%MOD; inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD; finv[i]=finv[i - 1]*inv[i]%MOD; } } ll COM(int n, int k){ if(n<k) return 0; if(n<0||k<0) return 0; return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD; } int main(){ COMinit(); int n, k; cin >> n >> k; VI a(n); REP(i,n) cin >> a[i]; VI p(n,1), sump(k+1,0); REP(i,k+1){ REP(j,n){ sump[i]=(sump[i]+p[j])%MOD; p[j]=p[j]*a[j]%MOD; } } for(int i=1;i<=k;i++){ ll ans=0; for(int j=0;j<=i;j++){ ans+=(sump[j]*sump[i-j]%MOD+MOD-sump[i])*COM(i,j)%MOD; ans%=MOD; } cout << divid(ans,2) << endl; } return 0; }
#include <bits/stdc++.h> #define endl "\n" using namespace std; typedef long long ll; typedef vector<ll> vl; typedef pair<ll, ll> PP; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rrep(i, n) for(ll i = n - 1; i > -1; i--) #define all(v) v.begin(), v.end() #define pb push_back #define fi first #define se second template <class X> void print(X x) { cout << x << endl; } #define in(A, n) \ rep(i, n) { \ cin >> f; \ A.push_back(f); \ } void print(vl x) { for(ll i : x) { cout << i << " "; } cout << endl; } const ll INF = (1LL << 61) - 1; const ll MOD = 998244353; const ll MAX_N = 500010; ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m; ll codeforces = 1; string S, T; vector<vl> g(MAX_N); vl A, B; #include <cstdint> template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if(a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if(a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while(exp) { if(exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint<MOD>; void input() { cin >> n>>c; rep(i, n) { cin >> x; A.pb(x); } } void solve() { vector<mint> K(n, 1), T, kaijo; kaijo.pb(1); rep(i, 602) { kaijo.pb(kaijo.back() * (i + 1)); } rep(i, 602) { mint k = 0; rep(j, n) { k += K[j]; K[j] *= A[j]; } T.pb(k); } rep(i, c) { t = i + 1; mint ans = 0; for(ll j = 0; j <= t - j; j++) { if(j == 0) { ans += T[t]*(n-1); } else { mint p = T[j] * T[t - j] - T[t]; if(j != t - j) { ans += p * kaijo[t]/(kaijo[j] * kaijo[t - j] ); } else ans += p * kaijo[t]/(kaijo[j] * kaijo[t - j] ) / 2; } } print(ans.a); } } int main() { // cout<<fixed<<setprecision(15); cin.tie(0); ios::sync_with_stdio(false); input(); while(codeforces--) { solve(); } }
#include<cstdio> #include<cstring> using namespace std; char s[210000]; int f[210000]; int main() { int n;scanf("%d",&n); scanf("%s",s+1); f[0]=0;int ss=0; for(int i=1,j=1;i<=n;i++,j++) { if(s[i]=='f')f[j]=1; else if(s[i]=='o'&&f[j-1]==1)f[j]=2; else if(s[i]=='x'&&f[j-1]==2) { ss++; j-=3; } else f[j]=0; } printf("%d\n",n-ss*3); return 0; }
#include <bits/stdc++.h> using namespace std; stack<char> st; int n; string s; int main() { ios::sync_with_stdio(0); cin>>n>>s; for(int i=0;i<n;i++){ if(st.size()<2){ st.push(s[i]); continue; } char c=st.top(); st.pop(); if(s[i]=='x' && c=='o' && st.top()=='f'){ st.pop(); continue; } st.push(c),st.push(s[i]); } cout<<st.size(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; vector<long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; if (n & 1) cout << "Second"; else { sort(a.begin(),a.end()); long count = 0ll; for (int i = 0; i < n; i += 2) count += a[i] - a[i + 1]; if (count == 0) cout << "Second"; else cout << "First"; } cout << '\n'; } 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 Pr pair<ll,ll> #define Tp tuple<ll,ll,ll,ll> using Graph = vector<vector<int>>; ll mod = 1000000007; int main() { ll T; cin >> T; rep(i,T){ ll N; cin >> N; vector<ll> a; ll sum = 0; a.clear(); rep(i,N){ ll b; cin >> b; a.push_back(b); sum += b; } if(N%2==1) cout << "Second" << endl; else{ sort(a.begin(),a.end()); ll subsum = 0; rep(i,N/2){ subsum += a[i*2]; } if(subsum*2==sum) cout << "Second" << endl; else cout << "First" << endl; } } }