code_file1
stringlengths
80
4k
code_file2
stringlengths
91
4k
similar_or_different
int64
0
1
#include <bits/stdc++.h> #define err(args...) {} #ifdef DEBUG #include "_debug.cpp" #endif using namespace std; using ll = long long; using ld = long double; template <typename T> using lim = numeric_limits<T>; template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; } const int M = 1'000'000'007; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<ll> x(n), y(m); cin >> x >> y; adjacent_difference(x.begin(), x.end(), x.begin()); adjacent_difference(y.begin(), y.end(), y.begin()); ll ysum = 0; for(int j = 1; j < m; j++) { ysum += y[j] * (m - j) % M * j % M; ysum %= M; } ll xsum = 0; for(int i = 1; i < n; i++) { xsum += x[i] * (n - i) % M * i % M; xsum %= M; } cout << xsum * ysum % M << endl; return 0; }
#include <bits/stdc++.h> using namespace std; # define REP(i,n) for (int i=0;i<(n);++i) # define rep(i,a,b) for(int i=a;i<(b);++i) # define p(s) std::cout << s ; # define pl(s) std::cout << s << endl; # define printIf(j,s1,s2) cout << (j ? s1 : s2) << endl; # define YES(j) cout << (j ? "YES" : "NO") << endl; # define Yes(j) std::cout << (j ? "Yes" : "No") << endl; # define yes(j) std::cout << (j ? "yes" : "no") << endl; # define all(v) v.begin(),v.end() # define showVector(v) REP(i,v.size()){p(v[i]);p(" ")} pl("") template<class T> inline bool chmin(T &a, T b){ if(a > b) { a = b; return true;} return false;} template<class T> inline bool chmax(T &a, T b){ if(a < b) { a = b; return true;} return false;} typedef long long int ll; typedef pair<ll,ll> P_ii; typedef pair<double,double> P_dd; template<class T> vector<T> make_vec(size_t a){ return vector<T>(a); } template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<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); } const int MOD = 1000000007; const int inf=1e9+7; const ll longinf=1LL<<60 ; void addM(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; } void mulM(long long &a, long long b) { a = ((a%MOD)*(b%MOD))%MOD ; } int main() { ll n, m; cin >> n >> m; vector<ll> x(n),y(m); ll xsum = 0, ysum = 0; REP(i, n) cin >> x[i]; sort(all(x)); REP(i, n - 1) addM(xsum, (x[i+1] - x[i])*(i + 1)*(n - i - 1)); REP(i, m) cin >> y[i]; sort(all(y)); REP(i, m - 1) addM(ysum, (y[i+1] - y[i])*(i + 1)*(m - i - 1)); mulM(xsum,ysum); cout << xsum << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; #define ll long long //~ #define endl '\n' void solve(){ int a[3]; cin>>a[0]>>a[1]>>a[2]; for(int i = 0; i<3; i++){ if(a[i] == a[(i + 1)%3] && a[i] != a[(i + 2)%3]){ cout<<"Yes"<<endl; return; } } cout<<"No"<<endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1;// cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) using namespace std; int main() { int n, k; cin >> n >> k; if (n % k == 0) { std::cout << 0 << endl; return 0; } std::cout << 1 << endl; return 0; }
0
#include"bits/stdc++.h" using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) const long long INF10=1e10+1,ID_MAX=20; const long mod=(1e9)+7; const long long INF18=1e18+1; struct edge{ int to; long cost; }; void sort2array(double *a,long *b,long n){ for(int i=0;i<n;i++){ b[i]=i; } sort(b,b+n,[a](long i,long j)->bool {return a[i]<a[j];} ); } //bを何回足せばaを超えるか(O(a/b)) int wtover(int a,int b){ if(a%b>0)return (a/b)+1; else return a/b; } int bi_e[ID_MAX]={0}; //2進数表示したときの最高桁(O(log n)) int bi_max(long n){ int m=0; for(m;(1<<m)<=n;m++);m=m-1; return m; } //bi_eに二進数表示したやつを代入(O(log^2 n)) void bi_exs(long n){ memset(bi_e,0,sizeof(bi_e)); if(n<(1<<ID_MAX)){ for(int i=0;n>0;n=(n>>1),i++)bi_e[i]=n&1; } } //x^n mod m (nが負の時は0)(O(log n)) long myPow(long x, long n, long m){ if(n<0)return 0; 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; } //m以下のn以下の分割数のmodの表生成(O(nm))d[i][j]=jのi分割 long long d[10001][10001]; void divNum(int n,int m,long long md=mod){ rep(j,n+1){ d[j][0]=1; } rep(j,m){ d[0][j+1]=0; } for(int j=1;j<=n;j++){ for(int k=1;k<=m;k++){ if(j>k)d[j][k]=d[j-1][k]; else d[j][k]=(d[j-1][k]+d[j][k-j])%md; } } } const int N_MAX=1e5+1,M_MAX=1e9+1; int n,m; vector<int> a; long long div_res[N_MAX][100]; void divide(int s,int t){ div_res[0][0]=1; rep(i,s){ rep(j,t+1){ rep(k,t-j+1){ div_res[i+1][j+k]=(div_res[i+1][j+k]+div_res[i][j])%mod; } } } } int main(){ int i=0,maxa=0; //入力 cin>>n>>m; int mm=m,x=2,r; for(int j=2;j<=ceil(sqrt(m));j++){ r=0; while(mm%j==0){ mm=mm/j; r++; } if(r!=0){ if(maxa<r)maxa=r; a.push_back(r); } } if(mm!=1)a.push_back(1); //処理 long long res=1; divide(n,maxa); rep(i,a.size()){ res=(res*div_res[n][a.at(i)])%mod; } //出力 cout<<res<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long int li; typedef long double lb; #define rep(i,j,n) for (ll i = j; i < (n); i++) #define repr(i,j,n) for(ll i = j; i >= (n); i--) #define all(x) (x).begin(),(x).end() #define CLR(mat,f) memset(mat, f, sizeof(mat)) #define IN(a, b, x) (a<=x&&x<b) #define out(ans) cout << ans << endl template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } typedef pair<ll,ll>P; const ll mod=1e9+7; const ll INF = 1LL<<60; const ll m=1LL<<32; const int MAX = 510000; const int MOD = 1000000007; long long 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; } } // 二項係数計算 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(){ ll n,m;cin>>n>>m; COMinit(); map<ll,ll>mp; for(ll i=2;i*i<=m;i++){ while(m%i==0){ mp[i]++; m/=i; } } if(m!=1)mp[m]++; ll ans=1; for(auto p:mp){ // out(p.first<<":"<<p.second<<" "<<COM(p.second+n-1,p.second)); ans*=COM(p.second+n-1,p.second); ans%=mod; } //因数別にそれぞれ配分していく組み合わせを掛けていく out(ans); return 0; }
1
#include <cstdio> #include <iostream> #include <algorithm> #define MAX_STATIONS 100+1 #define INF 1<<15 #define reep(i,n,m) for(int i=n;i<m;i++) #define rep(i,n) reep(i,0,n) using namespace std; class Stations{ private: int time[MAX_STATIONS][MAX_STATIONS]; int cost[MAX_STATIONS][MAX_STATIONS]; public: void solve(int a[MAX_STATIONS][MAX_STATIONS], int r){ //wayは文系をあらわす rep(way,MAX_STATIONS){ rep(to,MAX_STATIONS){ rep(from,MAX_STATIONS){ a[from][to] = min(a[from][to], a[from][way] + a[way][to]); a[to][from] = a[from][to]; } } } if(r==1){ rep(i,MAX_STATIONS){ rep(k,MAX_STATIONS){ time[k][i] = a[k][i]; } } } else{ rep(i,MAX_STATIONS){ rep(k,MAX_STATIONS){ cost[k][i] = a[k][i]; } } } } int getTime(int from, int to){ return time[from][to]; } int getCost(int from, int to){ return cost[from][to]; } }; int main(void){ int n,m; int t[MAX_STATIONS][MAX_STATIONS], c[MAX_STATIONS][MAX_STATIONS]; while(cin >> n >> m,n+m){ rep(i,MAX_STATIONS){ rep(k,MAX_STATIONS){ t[i][k] = INF; c[i][k] = INF; } } rep(i,n){ int from,to,cost,time; cin >> from >> to >> cost >> time; t[from][to] = t[to][from] = time; c[from][to] = c[to][from] = cost; } Stations station; station.solve(c,0); station.solve(t,1); int q; cin >> q; rep(i,q){ int from,to,r; cin >> from >> to >> r; if(r==1){ cout << station.getTime(from,to) << endl; }else{ cout << station.getCost(from,to) << endl; } } } return 0; }
#include <bits/stdc++.h> #include "atcoder/all" using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i)) #define REP(i, n) FOR(i,n,0) #define OF64 std::setprecision(10) const ll MOD = 1000000007; const ll INF = (ll) 1e15; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, Q; cin >> N >> Q; atcoder::fenwick_tree<ll> bit(N); REP(i, N) { ll a; cin >> a; bit.add(i, a); } REP(_, Q) { ll t, a, b; cin >> t >> a >> b; if (t == 0) { bit.add(a, b); } else { cout << bit.sum(a, b) << endl; } } return 0; }
0
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <cassert> #include <iostream> #include <cctype> #include <sstream> #include <string> #include <list> #include <vector> #include <queue> #include <set> #include <stack> #include <map> #include <utility> #include <numeric> #include <algorithm> #include <iterator> #include <bitset> #include <complex> #include <fstream> #include <iomanip> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for(int i = x; i < n; i++) template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); } template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; } template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; } template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); } int main() { int n, m; while(cin >> n >> m && n) { vint p(n); rep(i, n) cin >> p[i]; sort(ALL(p)); int res = accumulate(ALL(p), 0); rep(i, n) res -= ((i + 1) % m== 0) * p[n - i - 1]; cout << res << endl; } }
#include <iostream> #include <queue> int main(){ std::priority_queue<int> p; int n, m; int price; while(std::cin >> n >> m){ int sum_price = 0; if(n == 0 && m == 0) break; //input for(int i = 0; i < n; ++i){ std::cin >> price; p.push(price); } //袋詰め while(!p.empty()){ for(int i = 1; i <= m && !p.empty(); ++i){ if(i != m){ sum_price += p.top(); p.pop(); } else{ p.pop(); } } } std::cout << sum_price << std::endl; } }
1
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <tuple> #include <vector> using namespace std; typedef long long ll; ll const INF = 1LL << 60; int main() { ll A, B, C, D; cin >> A >> B >> C >> D; if (A + B < C + D) { cout << "Right" << endl; } else if (A + B > C + D) { cout << "Left" << endl; } else { cout << "Balanced" << endl; } return 0; }
#include <iostream> #include <string> int main () { int a, b, c, d; std::string ans = ""; std::cin >> a >> b >> c >> d; int x = a + b - c - d; if(x > 0) ans = "Left"; else if(x == 0) ans = "Balanced"; else ans = "Right"; std::cout << ans << std::endl; }
1
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int mod=1e9+7; const int INF=1001001001; int dp[105][10]; int main() { int H,W,K; cin>>H>>W>>K; dp[0][0]=1; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(dp[i][j]==0){continue;} for(int s=0;s<(1<<W-1);s++){ vector<int>t(W-1); for(int k=0;k<W-1;k++){ if((s>>k)&1){t[k]=1;} } bool ok=true; for(int k=0;k<W-2;k++){ if(t[k]==1&&t[k+1]==1){ok=false;} } if(ok){ if(j<W-1&&t[j]){(dp[i+1][j+1]+=dp[i][j])%=mod;} else if(j>0&&t[j-1]){(dp[i+1][j-1]+=dp[i][j])%=mod;} else{(dp[i+1][j]+=dp[i][j])%=mod;} } } } } cout<<dp[H][K-1]<<endl; return 0; }
#include "bits/stdc++.h" #define MOD 1000000007 #define rep(i, n) for(ll i=0; i < (n); i++) #define rrep(i, n) for(ll i=(n)-1; i >=0; i--) #define ALL(v) v.begin(),v.end() #define rALL(v) v.rbegin(),v.rend() #define FOR(i, j, k) for(ll i=j;i<k;i++) #define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" " using namespace std; typedef long long int ll; typedef vector<ll> llvec; typedef vector<double> dvec; typedef pair<ll, ll> P; typedef long double ld; struct edge{ll x, c;}; ll mod(ll a, ll mod){ ll res = a%mod; while(res<0)res=res + mod; return res; } ll modpow(ll a, ll n, ll mod){ ll res=1; while(n>0){ if(n&1) res=res*a%mod; a=a*a%mod; n>>=1; } return res; } ll modinv(ll a, ll mod){ return modpow(a, mod-2, mod); } ll gcd(ll a, ll b){ ll r = a%b; if(r==0) return b; else return gcd(b, a%b); } bool is_prime(ll n){ ll i = 2; if(n==1)return false; if(n==2)return true; bool res = true; while(i*i <n){ if(n%i==0){ res = false; } i = i+1; } //if(i==1)res = false; if(n%i==0)res=false; return res; } struct UnionFind{ ll N; llvec p; llvec cnt; UnionFind(ll n){ N = n; p=llvec(N); cnt=llvec(N, 0); rep(i, N){ p[i] = i; cnt[i] = 1; } } void con(ll a, ll b){ P at = root(a); P bt = root(b); if(at.second!=bt.second){ if(at.first>bt.first){ swap(at, bt); swap(a, b); } p[at.second] = bt.second; cnt[bt.second]+=cnt[at.second]; p[a]=bt.second; } } P root(ll a){ ll atmp = a; ll c=0; while(atmp!=p[atmp]){ atmp = p[atmp]; c++; } p[a] = atmp; return {c, atmp}; } bool is_con(ll a, ll b){ P at=root(a); P bt=root(b); return at.second == bt.second; } }; struct dijkstra{ ll N; llvec d; vector<vector<edge>> e; dijkstra(ll n){ N = n; //d = llvec(N, 1e18); e = vector<vector<edge>>(N); } void add_edge(ll from, ll to, ll cost){ e[from].push_back({to, cost}); } void run(ll start){ priority_queue<P, vector<P>, greater<P>> que; que.push({0, start}); d = llvec(N, 1e18); d[start]=0; while(!que.empty()){ P q = que.top();que.pop(); ll dc = q.first; ll x = q.second; if(dc>d[x]){ continue; }else{ for(auto ip: e[x]){ if(d[ip.x]<=d[x]+ip.c){ continue; }else{ d[ip.x]= d[x]+ip.c; que.push({d[ip.x], ip.x}); } } } } } }; ll fact[500000]; ll ifact[500000]; void gen(){ fact[0]=ifact[0]=1; rep(i, 200100){ fact[i+1]=mod(fact[i]*(i+1), MOD); ifact[i+1]=modinv(fact[i+1], MOD); } } ll comb(ll n, ll k){ return mod(mod(fact[n]*ifact[n-k], MOD)*ifact[k], MOD); } /************************************** ** A main function starts from here ** ***************************************/ int main(){ ll N, M, K; cin >> N >> M >> K; ll ans = 0; gen(); rep(i, N*M){ ll x = i/N+1; ll y = i/M+1; ans =mod(ans+ (M-x)*(M-x+1)/2*N, MOD); ans =mod(ans+ (N-y)*(N-y+1)/2*M, MOD); } cout << mod(ans*comb(N*M-2, K-2),MOD)<<endl; return 0; }
0
#include <stdio.h> int main(){ int menit; int angkaangka[5]; for(int i=0;i<5; i++){ scanf("%d", &angkaangka[i]); } //for(int i=0;i<5; i++){ // printf("%d ", angkaangka[i]); //} int jam=angkaangka[2]-angkaangka[0]; //printf("total jam: %d\n", jam); if(angkaangka[3]>angkaangka[1]){ menit=angkaangka[3]-angkaangka[1]; } else if(angkaangka[1]>angkaangka[3]){ menit=60-(angkaangka[1]-angkaangka[3]); jam--; } else{ menit=0; } //printf("total menit=%d\n", menit); //printf("total k= %d\n", angkaangka[4]); printf("%d\n", jam*60+menit-angkaangka[4]); return 0; }
#include <cstdio> #include <vector> #include <algorithm> using namespace std; #define REP(i,n) for (int i=0; i<(n); i++) #define SORT(c) sort((c).begin(),(c).end()) typedef vector<int> vi; int main() { vi W(10),K(10); REP(i,10) scanf("%d",&W[i]); REP(i,10) scanf("%d",&K[i]); SORT(W); SORT(K); int w=0,k=0; REP(i,3) w+=W[9-i]; REP(i,3) k+=K[9-i]; printf("%d %d\n",w,k); }
0
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (int)(n); i++) using namespace std; using ll = long long; const double PI = 3.14159265358979323846; int main() { string a, b, c; cin >> a >> b >> c; bool flag = true; if(a[a.size()-1] != b[0]) flag = false; if(b[b.size()-1] != c[0]) flag = false; if(flag) cout << "YES" << endl; else cout << "NO" << endl; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define pi 3.14159265358979323846 #define ll long long #define mp make_pair #define pf push_front #define MOD 1000000007 #define MAX 100000000 #define MIN -1000000000 #define LLINF 0x3f3f3f3f3f3f #define INF 0x3f3f3f3f #define ld long double #define MAXN 1010 #define MAXM 1010 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int d, t, s; cin >> d >> t >> s; if(s*t >= d){ cout << "Yes" << endl; }else{ cout << "No" << endl; } return 0; }
0
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <chrono> using namespace std; #define INF 1e18 long long DP[5001][5001]; struct Constraint { long long H, P; Constraint(long long H = 0, long long P = 0):H(H), P(P){} bool operator<(const Constraint& r)const { return P+H < r.P+r.H; } }; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<Constraint>C(N); for (int i = 0; i < N; i++) { int P, H; cin >> H >> P; C[i] = Constraint(H, P); } sort(C.begin(), C.end()); for (int i = 0; i <= N; i++)for (int j = 0; j <= N; j++)DP[i][j] = INF; DP[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < i + 1; j++) { if (DP[i][j] == INF)continue; if (DP[i][j] <= C[i].H) { DP[i + 1][j + 1] = min(DP[i + 1][j + 1], DP[i][j] + C[i].P); } DP[i + 1][j] = min(DP[i + 1][j], DP[i][j]); } } int ans = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (DP[i][j]==0 || DP[i][j] == INF)continue; ans = max(ans, j); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int M = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<ll, ll>> A(n); for (int i = 0; i < n; ++i) { cin >> A[i].first >> A[i].second; } auto comp = [&](pair<ll, ll>& a, pair<ll, ll>& b) { return a.first + a.second < b.first + b.second; }; sort(A.begin(), A.end(), comp); vector<vector<ll>> dp(2, vector<ll>(n + 1, 1e14)); dp[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { dp[i%2][j] = dp[(i - 1)%2][j]; if (A[i - 1].first >= dp[(i - 1)%2][j - 1]) { dp[i%2][j] = min(dp[i%2][j], A[i - 1].second + dp[(i - 1)%2][j - 1]); } } } // cout <<"dp array :: "<< "\n"; // for(int i = 1 ; i <= n ;++i){ // for(int j = 1 ; j <= n ;++j){ // cout << dp[i][j] << " "; // } // cout << "\n"; // } for (int j = n; j >= 0; --j) { if (dp[n%2][j] != 1e14) { cout << j << "\n"; break; } } return 0; }
1
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG using namespace std; using ll = long long; #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++) int main() { string S; cin >> S; int N = S.size(); int sum = 0; vector<int> vec(N); rep(i, N) { if(S.at(i) == '0') { vec.at(i) = 0; } if(S.at(i) == '1') { vec.at(i) = 1; } if(S.at(i) == '2') { vec.at(i) = 2; } if(S.at(i) == '3') { vec.at(i) = 3; } if(S.at(i) == '4') { vec.at(i) = 4; } if(S.at(i) == '5') { vec.at(i) = 5; } if(S.at(i) == '6') { vec.at(i) = 6; } if(S.at(i) == '7') { vec.at(i) = 7; } if(S.at(i) == '8') { vec.at(i) = 8; } if(S.at(i) == '9') { vec.at(i) = 9; } } reverse(vec.begin(), vec.end()); vector<int> vec10(N); rep(i, N) { if(i == 0) { vec10.at(i) = 1; } else { vec10.at(i) = vec10.at(i-1)*10; } } int sum2 = 0; rep(i, N) { sum += vec.at(i) * vec10.at(i); sum2 += vec.at(i); } if(sum%sum2 == 0) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for(int i = n-1; i >= 0; i--) #define all(x) (x).begin(),(x).end() // 昇順ソート #define rall(v) (v).rbegin(), (v).rend() // 降順ソート #define FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) typedef long long ll; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<vector<int>>; using VL = vector<ll>; using VVL = vector<vector<ll>>; using VP = vector<P>; template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; } int main(){ int n; cin >> n; int sum = 0; int x = n; while(n){ sum += n % 10; n /= 10; } if (x % sum == 0) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1
#include "bits/stdc++.h" using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<VVI> VVVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef long long LL; typedef priority_queue<int> PQ_DESC; typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC; typedef priority_queue<PII> PQ_DESC_PII; typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<VVLL> VVVLL; #define SORT_ASC(c) sort((c).begin(), (c).end()) #define SORT_DESC(c) sort((c).begin(), (c).end(), greater<typeof((c).begin())>()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORL(i,a,b) for(LL i=(a);i<(b);++i) #define REPL(i,n) FORL(i,0,n) #define SIZE(a) int((a).size()) #define ALL(a) (a).begin(),(a).end() const double EPS = 1e-10; const double PI = acos(-1.0); const int INT_LARGE = 1000000100; //debug func template<typename T> void vecprint(vector<T> v) { for(auto x : v) { cerr << x << " "; } cerr << endl; } template<typename T> void vecvecprint(vector<vector<T>> vv) { REP(i, SIZE(vv)) { REP(j, SIZE(vv[i])) { cerr << vv[i][j] << " "; } cerr << endl; } } template<typename T> void pqprint(priority_queue<T> q) { while(!q.empty()) { cerr << q.top() << " "; q.pop(); } cerr << endl; } template<typename T> void qprint(queue<T> q) { while(!q.empty()) { cerr << q.front() << " "; q.pop(); } cerr << endl; } template<typename T> void vecqprint(vector<queue<T>> v) { for(int i = 0; i < v.size(); i++) { qprint(v[i]); } } template <typename Iterator> inline bool next_combination(const Iterator first, Iterator k, const Iterator last) { /* Credits: Thomas Draper */ if ((first == last) || (first == k) || (last == k)) return false; Iterator itr1 = first; Iterator itr2 = last; ++itr1; if (last == itr1) return false; itr1 = last; --itr1; itr1 = k; --itr2; while (first != itr1) { if (*--itr1 < *itr2) { Iterator j = k; while (!(*itr1 < *j)) ++j; iter_swap(itr1,j); ++itr1; ++j; itr2 = k; rotate(itr1,j,last); while (last != j) { ++j; ++itr2; } rotate(k,itr2,last); return true; } } rotate(first,k,last); return false; } inline double get_time_sec(void){ return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count())/1000000000; } template<typename T> T gcd(T a, T b) { if(a > b) swap(a, b); if(a == 0) return b; else return gcd(b%a, a); } template<typename T> map<T, T> prime_list(T n) { map<T, T> ret; for(T i = 2; i*i <= n; i++) { if(n % i == 0) { ret[i] = 0; while(n % i == 0) { n /= i; ret[i]++; } } } if(n != 1) ret[n]++; return ret; } int main(void) { int n; cin >> n; VI a(n+2, 0); REP(i, n) cin >> a[i+1]; VI b(n+1); REP(i, n+1) b[i] = abs(a[i+1] - a[i]); int sum = 0; REP(i, n+1) sum += b[i]; REP(i, n) { cout << sum - b[i] - b[i+1] + abs(a[i] - a[i+2]) << endl; } }
#include <bits/stdc++.h> using namespace std; int main(void) { int N,sum; cin >> N; vector<int> A(N+2,0); vector<bool> bs(N+1,false); // 経由地点が間にあるかどうかを判定する sum = 0; for(int i=0;i<N;i++) { cin >> A[i+1]; if(i) { // 2個前と1個前と現在を見て1個前を確定 if(((A[i+1] >= A[i]) && (A[i] >= A[i-1])) || ((A[i+1] <= A[i]) && (A[i] <= A[i-1]))) { bs[i] = true; } } sum += abs(A[i+1] - A[i]); } // 最後を確定 if(((A[N+1] >= A[N]) && (A[N] >= A[N-1])) || ((A[N+1] <= A[N]) && (A[N] <= A[N-1]))) { bs[N] = true; } sum += abs(A[N+1] - A[N]); for(int i=1;i<=N;i++){ if(bs[i]) { cout << sum << endl; } else { cout << sum - abs(A[i]-A[i-1]) - abs(A[i+1]-A[i]) + abs(A[i+1]-A[i-1]) << endl; } } return 0; }
1
#include<bits/stdc++.h> #define pb(a) push_back(a) #define ms(x,y) memset(x,y,sizeof(x)) #define sci(a) scanf("%d",&a) #define scl(a) scanf("%lld",&a) #define scd(a) scanf("%lf",&a) #define ll long long using namespace std; /* --------------- fast io --------------- */ // begin namespace Fread{ const int SIZE=1<<20; char buf[SIZE],*S,*T; inline char getchar(){ if(S==T){ T=(S=buf)+fread(buf,1,SIZE,stdin); if(S==T)return EOF; } return *S++; } }//namespace Fread namespace Fwrite{ const int SIZE=1<<20; char buf[SIZE],*S=buf,*T=buf+SIZE; inline void flush(){ fwrite(buf,1,S-buf,stdout); S=buf; } inline void putchar(char c){ *S++=c; if(S==T)flush(); } struct _{ ~_(){flush();} }__; }//namespace Fwrite #ifdef ONLINE_JUDGE #define getchar Fread::getchar #define putchar Fwrite::putchar #endif template<typename T>inline void read(T& x){ x=0;int f=1; char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c))x=x*10+(c-'0'),c=getchar(); x*=f; } template<typename T>inline void write(T x,bool _enter=0,bool _space=0){ if (!x)putchar('0');else{ if(x<0)putchar('-'),x=-x; static char dig[41]; int top=0; while(x)dig[++top]=(x%10)+'0',x/=10; while(top)putchar(dig[top--]); } if(_enter)putchar('\n'); if(_space)putchar(' '); } namespace Fastio{ struct reader{ template<typename T>reader& operator>>(T& x){::read(x);return *this;} reader& operator>>(char& c){ c=getchar(); while(c=='\n'||c==' ')c=getchar(); return *this; } reader& operator>>(char* str){ int len=0; char c=getchar(); while(c=='\n'||c==' ')c=getchar(); while(c!='\n'&&c!=' ')str[len++]=c,c=getchar(); str[len]='\0'; return *this; } }cin; const char endl='\n'; struct writer{ template<typename T>writer& operator<<(T x){::write(x,0,0);return *this;} writer& operator<<(char c){putchar(c);return *this;} writer& operator<<(const char* str){ int cur=0; while(str[cur])putchar(str[cur++]); return *this; } }cout; }//namespace Fastio #define cin Fastio::cin #define cout Fastio::cout #define endl Fastio::endl /* --------------- fast io --------------- */ // end const int maxn=100005; int main(){ //#define int ll int d,t,s; while(scanf("%d %d %d",&d,&t,&s)!=EOF){ if(t*s>=d) puts("Yes"); else puts("No"); } return 0; }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for (int i=0;i<(int)(n);i++) #define codefor int test;scanf("%d",&test);while(test--) #define yn(ans) if(ans)printf("Yes\n");else printf("No\n") #define YN(ans) if(ans)printf("YES\n");else printf("NO\n") #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} int main(){ string s; cin>>s; if(s=="zyxwvutsrqponmlkjihgfedcba"){ out(-1); return 0; } if(s.length()<26){ cout<<s; char c='a'; rep(i,26){ if(count(all(s),c)==0){ cout<<c<<endl; return 0; } c++; } }else if(s.length()==26){ int n=25,d=0; while(1){ s[n]++; if(s[n]>'z'){ s[n-1]++; s.erase(s.begin()+n); n--; } if(s[n]>='a'&&s[n]<='z'&&count(all(s),s[n])==1)break; } cout<<s<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int arr[4]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int d; cin>>d; if(d==1) cout<<"Hello World"; else { int a,b; cin>>a>>b; cout<<a+b; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a) for(int i=0;i<(a);i++) const ll MOD=1000000007; //const ll MOD=998244353; int main(){ int M; cin>>M; ll sum=0,sumC=0; rep(i,M){ ll d,c; cin>>d>>c; sum+=d*c; sumC+=c; } cout<<sumC-1+(sum-1)/9<<endl; return 0; }
0
#include <iostream> using namespace std; class Dice { private: int number[6]; int state[6]; public: Dice(); ~Dice(); void initialize(); void setNumber(int index, int num); void roll(char d); void move(string d); int getNumber(int idx); int getTop(); int getSide(int t, int f); bool cmp(Dice dice1); }; Dice::Dice() { initialize(); } Dice::~Dice() { } void Dice::initialize() { for (int i = 0; i < 6; i++) state[i] = i+1; } void Dice::setNumber(int index, int num) { number[index] = num; } void Dice::roll(char d) { int nextIndex[4][6] = { {1, 5, 2, 3, 0, 4}, {3, 1, 0, 5, 4, 2}, {2, 1, 5, 0, 4, 3}, {4, 0, 2, 3, 5, 1}, }; int *idx, nextState[6]; switch (d) { case 'N': idx = nextIndex[0]; break; case 'E': idx = nextIndex[1]; break; case 'W': idx = nextIndex[2]; break; case 'S': idx = nextIndex[3]; break; default: return; } for (int i = 0; i < 6; i++) nextState[i] = state[idx[i]]; for (int i = 0; i < 6; i++) state[i] = nextState[i]; } void Dice::move(string d) { int oldIndex[4][6] = { {1, 5, 2, 3, 0, 4}, {3, 1, 0, 5, 4, 2}, {2, 1, 5, 0, 4, 3}, {4, 0, 2, 3, 5, 1}, }; for (int i = 0; i < d.size(); i++) { int newNumber[6] = {0}; int *idx; char c = d[i]; switch (c) { case 'N': idx = oldIndex[0]; break; case 'E': idx = oldIndex[1]; break; case 'W': idx = oldIndex[2]; break; case 'S': idx = oldIndex[3]; break; default: return; } for (int i = 0; i < 6; i++) newNumber[i] = number[idx[i]]; for (int i = 0; i < 6; i++) number[i] = newNumber[i]; } } int Dice::getNumber(int idx) { return number[state[idx]-1]; } int Dice::getTop() { return getNumber(0); } int Dice::getSide(int t, int f) { string s = "NNNNENNNN"; for (int i = 0; i < s.size(); i++) { if (number[state[1]-1] == f) { break; } char c = s[i]; roll(c); } s = "EEEEE"; for (int i = 0; i < s.size(); i++) { if (number[state[0]-1] == t) break; char c = s[i]; roll(c); } return getNumber(2); } bool Dice::cmp(Dice op) { string s = "SWWWWNEEEEENEEEENNEEEESEEEEESEEEE"; for (int i = 0; i < s.size(); i++) { char c = s[i]; op.roll(c); int flag = 0; for (int i = 0; i < 6; i++) if (op.getNumber(i) != number[i]) { flag = 1; break; } if (flag == 0) return true; } return false; } int main() { Dice *dice; int n, m; cin >> n; dice = new Dice[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < 6; j++) { cin >> m; dice[i].setNumber(j, m); } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (dice[i].cmp(dice[j]) == true) { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; template <typename T> bool chmax(T &u, const T z) { if (u < z) {u = z; return true;} else return false; } template <typename T> bool chmin(T &u, const T z) { if (u > z) {u = z; return true;} else return false; } #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define ll long long typedef pair<ll, ll> P; const ll INF = 1LL << 60; //const int MOD = 1000000007; const ll MOD=998244353; ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } int main(){ string s; cin>>s; int n=s.size(); rep(i,n-1){ string t=s.substr(i,2); if(t=="AC"){ cout<<"Yes"<<endl; return 0; } } cout<<"No"<<endl; return 0; }
0
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> using namespace std; static const double EPS = 1e-5; typedef long long ll; int main(){ int N,Q,M,Date[101],i,l,k; while(0<=scanf("%d%d",&N,&Q)){ for(i=0;i<101;i++){ Date[i]=0; } if((N==0) && (Q==0)){ break; } for(k=0;k<N;k++){ scanf("%d",&M); if(M==0){ continue; }else{ for(i=0;i<M;i++){ scanf("%d",&l); ++Date[l]; } } } l=0; i=0; for(k=0;k<100;k++){ if(i<Date[k] && Date[k]>=Q){ i=Date[k]; l=k; } } printf("%d\n",l); } return 0; }
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int main() { for(;;){ int n, q; cin >> n >> q; if(n == 0) return 0; vector<int> num(101); for(int i=0; i<n; ++i){ int m; cin >> m; for(int j=0; j<m; ++j){ int a; cin >> a; ++ num[a]; } } int ret = 0; for(int i=1; i<=100; ++i){ if(num[i] >= q && num[i] > num[ret]) ret = i; } cout << ret << endl; } }
1
#include<iostream> #include<algorithm> #include<vector> #include<numeric> #include<string> #include<cmath> #include<set> #include<queue> #include<deque> #include<bitset> #include<iomanip> #include<cctype> #include<map> #include<cstring> #include<bitset> #include<cassert> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++) #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long int; const int inf = 1001001000; const long long int Inf = 1001001001001001000; void print(vector<vector<int>> a){ for (int i = 0; i < a.size(); i++) { for (int j=0;j<a[i].size();j++){ cout << a[i][j] << " "; } cout << endl; } } void print(vector<vector<long long int>> a){ for (int i=0;i<a.size();i++){ for (int j=0;j<a[i].size();j++){ cout << a[i][j] << " "; } cout << endl; } } void print(vector<int> a){ int n = a.size(); for (int j=0;j<n;j++) { if (j != n-1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(vector<long long int> a){ int n = a.size(); for (int j=0;j<n;j++) { if (j != n-1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(set<int> a){ for (auto x:a) cout << x << " "; cout << endl; } //nの約数を返す関数.sortされてない. vector<long long int> divisors(long long int n){ vector<long long int> res; for (long long int i = 1; i * i <= n;i++){ if (n % i == 0){ res.push_back(i); if(i * i != n){ res.push_back(n / i); } } } return res; } int main() { ll n; cin >> n; vector<ll> d = divisors(n); int ans = inf; for (auto x : d) { ans = min(max(int(to_string(x).size()), int(to_string(n/x).size())),ans); } cout << ans << endl; return 0; }
#include<iostream> #include<cmath> using namespace std; int main() { int r; cin>>r; double pi=3.147; double ans=pi*2*r; cout<<ans<<endl; return 0; }
0
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <map> #include <cmath> #include <iomanip> #include <set> #include <sstream> #include <queue> using namespace std; typedef long long ll; int main() { int n; string s; cin >> n >> s; vector<int> west_sum(n); vector<int> east_sum(n); for (int i = 1; i < n; i++) { if (s[i-1] == 'W') { west_sum[i] = west_sum[i-1] + 1; } else { west_sum[i] = west_sum[i-1]; } } for (int i = n-2; i >= 0; i--) { if (s[i+1] == 'E') { east_sum[i] = east_sum[i+1] + 1; } else { east_sum[i] = east_sum[i+1]; } } int ans = 1e9; for (int i = 0; i < n; i++) { if (i == 0) { ans = min(ans, east_sum[i]); } else if (i == n-1) { ans = min(ans, west_sum[i]); } else { ans = min(ans, west_sum[i] + east_sum[i]); } } cout << ans << endl; return 0; }
#include<iostream> #include<string.h> #include<string> #include<stdio.h> #include<algorithm> #include<vector> #include<bitset> #include<math.h> #include<stack> #include<queue> #include<set> #include<map> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; const int N=100000; const db pi=acos(-1.0); #define lowbit(x) (x)&(-x) #define sqr(x) (x)*(x) #define rep(i,a,b) for (register int i=a;i<=b;i++) #define per(i,a,b) for (register int i=a;i>=b;i--) #define go(u,i) for (register int i=head[u];i;i=sq[i].nxt) #define fir first #define sec second #define mp make_pair #define pb push_back #define maxd 1000000007 #define eps 1e-8 inline int read() { int x=0,f=1;char ch=getchar(); while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();} while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();} return x*f; } namespace My_Math{ #define N 100000 int fac[N+100],invfac[N+100]; int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;} int dec(int x,int y) {return x<y?x-y+maxd:x-y;} int mul(int x,int y) {return 1ll*x*y%maxd;} ll qpow(ll x,int y) { ll ans=1; while (y) { if (y&1) ans=mul(ans,x); x=mul(x,x);y>>=1; } return ans; } int inv(int x) {return qpow(x,maxd-2);} int c(int n,int m) { if ((n<m) || (n<0) || (m<0)) return 0; return mul(mul(fac[n],invfac[m]),invfac[n-m]); } int math_init() { fac[0]=invfac[0]=1; rep(i,1,N) fac[i]=mul(fac[i-1],i); invfac[N]=inv(fac[N]); per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1); } #undef N } using namespace My_Math; int n,A,B,C,D,f[1010][1010]; int main() { n=read();A=read();B=read();C=read();D=read(); math_init(); f[0][0]=1; rep(i,1,B-A+1) { int t=i+A-1; rep(j,0,n) { f[i][j]=f[i-1][j]; rep(k,C,D) { if (k*t>j) continue; int tmp=mul(f[i-1][j-k*t],c(n-j+k*t,k*t)); tmp=mul(mul(tmp,invfac[k]),mul(qpow(invfac[t],k),fac[k*t])); f[i][j]=add(f[i][j],tmp); } } } printf("%d",f[B-A+1][n]); return 0; }
0
#include <bits/stdc++.h> #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " : " << x << '\n' using namespace std; typedef long long ll; typedef long double ld; typedef string str; typedef pair<ll, ll> pll; const ll Mod = 998244353; const int N = 4e3 + 10; const ll Inf = 2242545357980376863LL; const ll Log = 30; ll mul(ll a, ll b){ return (a*b) % Mod; } ll p2[N], C[N][N]; ll Solve(ll n, ll s){ if(n == 0 && s == 0) return 1; if(s < 0) return 0; if(n <= 0) return 0; return C[s + n - 1][s]; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); p2[0] = 1; for(int i = 1; i < N; i++) p2[i] = (p2[i - 1] << 1) % Mod; for(int i = 0; i < N; i++){ for(int j = 0; j <= i; j++) C[i][j] = (j ? C[i - 1][j] + C[i - 1][j - 1] : 1) % Mod; } ll n, k; cin >> k >> n; for(int i = 2; i <= k+k; i++){ ll res = 0; if(i & 1){ ll t = (i <= k + 1 ? i / 2 : (k + k + 2 - i) / 2); for(int y = 0; y <= t; y++){ res += mul(mul(p2[y], C[t][y]), Solve(k - t - t + y, n - y)); } } else { ll t = (i <= k + 1 ? i / 2 : (k + k + 2 - i) / 2) - 1; for(int y = 0; y <= t; y++){ res += mul(mul(p2[y], C[t][y]), Solve(k - t - t - 1 + y, n - y - 1)); res += mul(mul(p2[y], C[t][y]), Solve(k - t - t - 1 + y, n - y)); } } cout << res%Mod << '\n'; } return 0; }
/* [arc102] E - Stop. Otherwise... */ #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, int> pli; const int MAX_Z = 2000 * 2 + 2; const ll MOD = 998244353; int K, N; int fact[MAX_Z + 1]; int ifac[MAX_Z + 1]; int pow2[MAX_Z + 1]; void prepare() { int inv[MAX_Z + 1]; fact[1] = fact[0] = 1; ifac[1] = ifac[0] = 1; inv[1] = 1; for (int i = 2; i <= MAX_Z; i++) { fact[i] = 1LL * i * fact[i - 1] % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; ifac[i] = 1LL * ifac[i - 1] * inv[i] % MOD; } pow2[0] = 1; for (int i = 1; i <= MAX_Z; i++) { pow2[i] = (2LL * pow2[i - 1]) % MOD; } } int comb(int n, int r) { if (n < 0 || r < 0 || n < r) { return 0; } return (1LL * fact[n] * ifac[n - r]) % MOD * ifac[r] % MOD; } int hcomb(int n, int r) { if (n == 0 && r == 0) { return 1; } return comb(n + r - 1, r); } ll solve(int t) { ll ans = 0; int p = 0; for (int j = 1; j <= K; j++) { if (j < t - j && t - j <= K) p++; } for (int q = 0; q <= min(N, p); q++) { ll c = (1LL * pow2[q] * comb(p, q)) % MOD; if (t % 2 == 0) { (c *= ((hcomb(K - 2 * p + q - 1, N - q) + hcomb(K - 2 * p + q - 1, N - q - 1)) % MOD)) %= MOD; } else { (c *= hcomb(K - 2 * p + q, N - q)) %= MOD; } (ans += c) %= MOD; } return ans; } int main() { cin >> K >> N; prepare(); for (int t = 2; t <= 2 * K; t++) { cout << solve(t) << endl; } return 0; }
1
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstring> #include <map> #include <queue> #include <stack> #include <set> #include <functional> #include <sstream> #include <complex> #include <climits> using namespace std; #define REP(i,a,n) for(int i=(a);i<(int)(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define EPS 1e-8 int main(){ int m,d; const int day[] = {31,29,31,30,31,30,31,31,30,31,30,31}; const char *youbi[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; while(scanf("%d",&m),m){ scanf("%d",&d); int sum = 0; rep(i,m-1)sum+=day[i]; sum += d; puts(youbi[(sum+2)%7]); } return 0; }
#include <iostream> #include <iomanip> #include <vector> #include <string> #include <stack> #include <set> #include <cmath> #include <algorithm> using namespace std; int zeller(int y , int m , int d){ if(m==1 || m==2) {m+=12; y-=1;} return(y + y/4 - y/100 + y/400 + ((13*m+8)/5) + d )%7; } int main(){ int m , d , w=4 , fw; while (cin >> m >> d, m){ switch(zeller(2004,m,d)){ case 0: cout << "Sunday" << endl; break; case 1: cout << "Monday" << endl; break; case 2: cout << "Tuesday" << endl; break; case 3: cout << "Wednesday" << endl; break; case 4: cout << "Thursday" << endl; break; case 5: cout << "Friday" << endl; break; case 6: cout << "Saturday" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; class Dice{ public: unique_ptr<vector<int>> nums; Dice(int n){ nums.reset(new vector<int>(6)); } void rotate(char dir){ int a, b, c, d; switch(dir){ case 'N': a = nums->at(0), b = nums->at(1), c = nums->at(5), d = nums->at(4); nums->at(0) = b; nums->at(1) = c; nums->at(5) = d; nums->at(4) = a; break; case 'E': a = nums->at(0), b = nums->at(2), c = nums->at(5), d = nums->at(3); nums->at(0) = d; nums->at(2) = a; nums->at(5) = b; nums->at(3) = c; break; case 'W': a = nums->at(0), b = nums->at(2), c = nums->at(5), d = nums->at(3); nums->at(0) = b; nums->at(2) = c; nums->at(5) = d; nums->at(3) = a; break; case 'S': a = nums->at(0), b = nums->at(1), c = nums->at(5), d = nums->at(4); nums->at(0) = d; nums->at(1) = a; nums->at(5) = b; nums->at(4) = c; break; } } }; int main() { Dice d(6); for(int i = 0; i < 6; i++){ cin >> d.nums->at(i); } string s; cin >> s; for(char c: s){ d.rotate(c); } cout << d.nums->at(0) << endl; }
#include <iostream> #include <sstream> using namespace std; class Dice { public: static int rollingface[][4]; int face[6]; Dice(string &face_data); // ~Dice(); void roll(const char &op); int top(); void print_faces(); }; int Dice::rollingface[][4] = { {0, 2, 5, 3}, {0, 4, 5, 1}, {0, 1, 5, 4}, {0, 3, 5, 2}, }; Dice::Dice(string &face_data) { istringstream bstrm; bstrm.str(face_data); for (int i = 0; i < 6; i++) { bstrm >> face[i]; } } void Dice::roll(const char &op) { int *fseq; switch (op) { case 'E': fseq = Dice::rollingface[0]; break; case 'N': fseq = Dice::rollingface[1]; break; case 'S': fseq = Dice::rollingface[2]; break; case 'W': fseq = Dice::rollingface[3]; break; default: break; } int t, p = face[fseq[3]]; for (int i = 0; i < 4; i++) { t = face[fseq[i]]; face[fseq[i]] = p; p = t; } } void Dice::print_faces() { cout << "face:" << endl; cout << " " << face[4] << endl; cout << face[3] << " " << face[0] << " " << face[2] << endl; cout << " " << face[1] << endl; cout << " " << face[5] << endl; cout << endl; } int Dice::top() { return face[0]; } int main() { string buf, op_seq; int i; Dice *dice; getline(cin, buf); dice = new Dice(buf); getline(cin, op_seq); for (i = 0; i < op_seq.size(); i++) { dice->roll(op_seq[i]); // dice->print_faces(); } cout << dice->top() << endl; return 0; }
1
// based on http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2784229#1 #include <iostream> #include <vector> #include <string> #include <cmath> #include <set> #include <algorithm> #include <array> #include <complex> #include <utility> #include <map> int needleX[4] = { -1,0,1,0 }; int needleY[4] = { 0,1,0,-1 }; bool dfs(int x, int y, int goalx, int goaly, std::vector<std::vector<bool>>&canGo) { if (x == goalx && y == goaly) { return true; } canGo[y][x] = false; for (int i = 0; i < 4; i++) { int next_x = x + needleX[i]; int next_y = y + needleY[i]; if (canGo[next_y][next_x]) { bool flag = dfs(next_x, next_y, goalx, goaly, canGo); if (flag) return true; } } return false; } int main(void) { int h, w, n; std::cin >> w >> h; std::vector<std::string> ans; while(!(h == 0 && w == 0)){ int color=-1, orient, leftX, leftY; int sx, sy, gx, gy; std::cin >> sx >> sy >> gx >> gy; std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0)); std::vector<std::vector<bool>> canGo(h + 2, std::vector<bool>(w + 2, false)); std::cin >> n; int start = -1, goal = -1; for (int i = 0; i < n; i++) { std::cin >> color >> orient >> leftX >> leftY; if (orient == 0) { for (int j = 0; j <= 3; j++) { for (int k = 0; k <= 1; k++) { board[leftY + k][leftX + j] = color; if (leftX + j == sx && leftY + k == sy) { start = color; } else if (leftX + j == gx && leftY + k == gy) { goal = color; } } } } else { for (int j = 0; j <= 1; j++) { for (int k = 0; k <= 3; k++) { board[leftY + k][leftX + j] = color; if (leftX + j == sx && leftY + k == sy) { start = color; } else if (leftX + j == gx && leftY + k == gy) { goal = color; } } } } } // for (int i = 0; i <= h; i++) { // for (int j = 0; j <= w; j++) { // std::cerr << board[i][j] << " "; // } // std::cerr << std::endl; // } if (start != -1) { for (int i = 0; i <= h; i++) { for (int j = 0; j <= w; j++) { if (board[i][j] == start) { canGo[i][j] = true; } } } } if (start != goal || start == -1) { ans.push_back("NG"); std::cin >> w >> h; continue; } bool result = dfs(sx, sy, gx, gy, canGo); if (result) { ans.push_back("OK"); } else { ans.push_back("NG"); } std::cin >> w >> h; } int anssize = ans.size(); for (int i = 0; i < anssize; i++) { std::cout << ans[i] << std::endl; } return 0; }
#include <cstdio> #include <cstdlib> #include <cmath> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <utility> #include <set> #include <map> #include <stack> #include <queue> #include <functional> using namespace std; #define fr first #define sc second #define pb push_back #define mp make_pair #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define rep(i,x) for(ll i=0;i<x;++i) #define rep1(i,x) for(ll i=1;i<=x;++i) #define rrep(i,x) for(ll i=x-1;i>=0;--i) #define rrep1(i,x) for(ll i=x;i>=1;--i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define rev(a) reverse(all(a)) template<typename T> T get(){T a;cin >> a;return a;} typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int inf = 1e9; const ll linf = 1e18; int main() { int N; cin >> N; vi team(N), rnk(N); fill(all(team), 0); fill(all(rnk), 1); rep(i, (N*N-N)/2) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; if (c > d) team[a]+=3; else if (d > c) team[b]+=3; else {team[a]++;team[b]++;} } rep(i, N) rep(j, N) { if (team[i] < team[j]) rnk[i]++; } rep(i, N) cout << rnk[i] << endl; }
0
#include <stdio.h> #include <algorithm> using namespace std; int a[200010]; long long f[200010]; long long g[200010]; long long get(int x) { if (x <= 0) return 0; return f[x]; } int main() { int n, i; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (i) g[i] = g[i-1]; if (i%2==0) g[i]+= a[i]; } for (i = 0; i < n; i++) { if ((i+1)%2==0) { f[i]=max(a[i]+get(i-2),g[i-1]); } else { f[i]=max(a[i]+get(i-2),f[i-1]); } } printf("%lld\n", f[n-1]); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i) #define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i) #define all(n) begin(n), end(n) using ll = long long; using P = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vc = vector<char>; using vb = vector<bool>; using vd = vector<double>; vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1}; int main() { ll n; cin >> n; vl a(n); rep(i, n) cin >> a[i]; ll m = n % 2 + 1; ll dp[n + 1][m + 1][2] = {}; rep(i, n + 1) rep(j, m + 1) rep(k, 2) dp[i][j][k] = -1e18; dp[0][0][0] = 0; rep(i, n) rep(j, m + 1) rep(k, 2) { if(k == 1) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]); else { if(j != 0 || i != n - 1) dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + a[i]); if(j < m) dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][k]); } } ll ans = -1e18; rep(j, m + 1) rep(k, 2) ans = max(ans, dp[n][j][k]); cout << ans << endl; }
1
#include <iostream> #include <string> #include <sstream> int partition(int* arr, int n) { const int x = arr[n - 1]; //pivot value int i = 0; //increment if swaped int j = 0; //increment every loop for (; j < n; ++j) { //scan start to end if (arr[j] <= x) { std::swap(arr[i], arr[j]); ++i; } } return i - 1; } void print(int const* arr, const int n, int q, int i) { if (i == n - 1 ) { std::cout << arr[i] << std::endl; return; } if (i == q) { std::cout << "[" << arr[i] << "] "; } else { std::cout << arr[i] << " "; } return print(arr, n, q, i + 1); } void print(int const* arr, const int n, int q) { print(arr, n, q, 0); return; } int main() { int n; { std::string buf; std::getline(std::cin, buf); n = std::stoi(buf); } int arr[100000]; { std::string buf; std::getline(std::cin, buf); std::stringstream stream; stream << buf; for (int i = 0; std::getline(stream, buf, ' '); ++i) { arr[i] = std::stoi(buf); } } int q = partition(arr, n); print(arr, n, q); return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> using namespace std; int A[100001]; int n; int partition(){ int i = -1; for (int j = 0; j < n - 1; ++j) if (A[j] <= A[n-1]) swap(A[++i], A[j]); swap(A[i + 1], A[n-1]); return i + 1; } int main(){ scanf("%d",&n); for (int i = 0; i < n; ++i) scanf("%d",&A[i]); int pivot = partition(); for (int i = 0; i < n; ++i){ if (i != 0) cout << ' '; if (i == pivot){ cout << '[' << A[i] << ']'; } else cout << A[i]; } cout << '\n'; }
1
#include <bits/stdc++.h> using namespace::std; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() typedef long long ll; typedef array<int, 3> tri; typedef long double ld; template <class T> istream& operator>>(istream& I, vector<T>& v) {for (T &e: v) I >> e; return I;} template <class T> ostream& operator<<(ostream &O, const vector<T>& v) {for (const T &e: v) O << e << ' '; return O;} const int MOD = 1E9 + 7; void _main() { int n, m; cin >> n >> m; vector<int> x(n), y(m); cin >> x >> y; auto solve = [](vector<int> a) { ll ans = 0, pre = 0; int n = sz(a); for (int i = 0; i < n; i++) { ans += (ll) i * a[i] - (ll)(n - i - 1) * a[i]; ans += MOD; ans %= MOD; } return ans; }; cout << solve(x) * solve(y) % MOD; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); int _t = 1; // cin >> _t; while (_t--) _main(); return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define f first #define s second #define mp make_pair #define pb push_back #define vll vector<ll> using namespace std; const ll N = 4e5+100; const ll MOD = 1e9+7; string s,t; ll tree[N]; void build(ll node,ll l,ll r) { if(l==r) { tree[node] = (s[l-1] == t[r-1]); } else { ll mid = (l + r) >> 1LL; build(node*2,l,mid); build(2*node+1,mid+1,r); tree[node] = tree[node*2] + tree[node*2+1]; } } ll query(ll node,ll st,ll en,ll l,ll r) { if(r < st || l> en) return 0; if(st >= l && en <= r) return tree[node]; ll mid = (st + en)/2; ll p1 = query(node*2,st,mid,l,r); ll p2 = query(node*2+1,mid+1,en,l,r); return p1 + p2; } ll pre[N]; ll pr2[N]; int main() { ll n,m; cin >> n >>m; ll a[n+1],b[m+1]; for(int i =1;i<=n;i++) cin >>a[i-1]; for(int i =1;i<=m;i++) cin >> b[i-1]; ll co = 0,co2 = 0; for(int i =0;i<(n-1);i++) { ll x = i+1; co = co + ((a[i+1]- a[i]) * (x) * (n-x)); if(co < MOD) co+=MOD; co%=MOD; } for(int i =0;i<(m-1);i++) { ll x = i+1; co2 = co2 + ((b[i+1]- b[i]) * (x) * (m-x)); if(co2 < MOD) co2+=MOD; co2%=MOD; } cout << (co*co2) %MOD << endl; return 0; }
1
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<cmath> #include<bitset> #include<deque> #include<functional> #include<iterator> #include<map> #include<set> #include<stack> #include<queue> #include<utility> using namespace std; typedef long long ll; #define a first #define b second #define sz(x) (ll)((x).size()) #define pb push_back #define mp make_pair #define bg begin() #define ed end() #define all(x) (x).bg,(x).ed #define rep(i,n) for(ll i=0;i<(n);i++) #define rep1(i,n) for(ll i=1;i<=(n);i++) #define rrep(i,n) for(ll i=(n)-1;i>=0;i--) #define rrep1(i,n) for(ll i=(n);i>=1;i--) #define FOR(i,a,b) for(ll i=(a);i<(b);i++) const ll MOD=1000000007; const ll INF=1000000000000000; template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;} template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;} ll maxx(ll x,ll y,ll z){return max(max(x,y),z);} ll minn(ll x,ll y,ll z){return min(min(x,y),z);} ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);} ll lcm(ll x,ll y){return x*(y/gcd(x,y));} ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}} ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;} vector<ll> pw2(62,1);vector<ll> pw10(19,1); int main(){ {rep1(i,61) pw2[i]=2*pw2[i-1];} {rep1(i,18) pw10[i]=10*pw10[i-1];} ll N; cin>>N; ll ans=0; string x; cin>>x; string s="i"; s+=x; ll Bcnt=0; rep1(i,N){ if(s[i]=='B') Bcnt++; } rep1(R,N)rep1(G,N){ if(s[R]=='R' && s[G]=='G'){ //B探す ll ans1,ans2,ans3,tmp=Bcnt; if(abs(R-G)%2==0){ ans1=(R+G)/2; if(s[ans1]=='B') tmp--; } ans2=2*R-G; if(1<=ans2 && ans2<=N && s[ans2]=='B') tmp--; ans3=2*G-R; if(1<=ans3 && ans3<=N && s[ans3]=='B') tmp--; ans+=tmp; } } 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; #define REP(i,n) for(ll i=0;i<(ll)(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++) #define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--) //定数 #define INF 1000000000000 //10^12:極めて大きい値,∞ //略記 #define PB push_back //vectorヘの挿入 #define MP make_pair //pairのコンストラクタ #define F first //pairの一つ目の要素 #define S second //pairの二つ目の要素 int main(){ int N; string S; cin>>N>>S; vector<int> v(3); rep(i,N) v[S[i]%3]++; ll ans=(ll)v[0]*v[1]*v[2]; ll sub=0; rep(i,N){ for(int j=i+1;j<N;j++){ if(S[i]==S[j]) continue; int k=2*j-i; if(k>=N) continue; if(S[i]==S[k] || S[j]==S[k]) continue; sub++; } } ans-=sub; cout<<ans<<endl; return 0; }
1
#include<bits/stdc++.h> #define re register #define inc(i,j,k) for(re int i=j;i<=k;i++) using namespace std; const int N=105; inline int read(){ int x=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x; } int n,m,a[N],pos[N],cnt; int main(){ n=read(),m=read(); inc(i,1,m) a[i]=read(); inc(i,1,m) if(a[i]&1) pos[++cnt]=i; if(cnt>2) return puts("Impossible"),0; if(pos[1]) swap(a[1],a[pos[1]]); if(pos[2]) swap(a[m],a[pos[2]]); if(m==1&&a[1]==1) return printf("1\n1\n1"),0; inc(i,1,m) printf("%d ",a[i]); puts(""); if(m==1) return printf("2\n1 %d",n-1),0; a[1]++,a[m]--; if(!a[m]) m--; printf("%d\n",m); inc(i,1,m) printf("%d ",a[i]); }
#include <iostream> using std::cin; using std::cout; using std::endl; using ll = long long; template <typename T> T min(T a, T b){ return (a < b) ? a : b; } template <typename T> class SegTree{ static const int MAXN = 100010; T INF; int n; T A[MAXN*4]; T time[MAXN*4]; public: SegTree(int size, T INF): INF(INF){ n = 1; while(n < size){ n *= 2; } n *= 2; for(int i = 0; i < MAXN*4; i++) A[i] = INF; for(int i = 0; i < MAXN*4; i++) time[i] = 0; } void update(int a, int b, T v, T t){ return _update(a, b, 0, 0, n, v, t); } void _update(int a, int b, int k, int l, int r, T v, T t){ if(a >= r || b <= l){ return; } if(l >= a && r <= b){ A[k] = v; time[k] = t; return; } _update(a, b, 2*k + 1, l, (l+r)/2, v, t); _update(a, b, 2*k + 2, (l+r)/2, r, v, t); } T query(int _idx){ int offset = n - 1; int idx = _idx + offset; T t = 0; T ans = INF; while(idx > 0){ if(time[idx] >= t){ ans = A[idx]; t = time[idx]; } idx = (idx-1)/2; } return ans; } }; int main(void){ int n, q; cin >> n >> q; SegTree<int> rmq(n, 0x7FFFFFFF); for(int loop = 0; loop < q; loop++){ int com; cin >> com; if(com == 0){ int x, y, v; cin >> x >> y >> v; rmq.update(x, y+1, v, loop+1); }else{ int i; cin >> i; cout << rmq.query(i) << endl; } } }
0
// I SELL YOU...! #include<iostream> #include<vector> #include<algorithm> #include<functional> #include<queue> #include<chrono> #include<iomanip> #include<map> #include<set> using namespace std; using ll = long long; using P = pair<ll,ll>; void init_io(){ cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(10); } signed main(){ init_io(); ll n; string s; cin >> n >> s; vector<ll> a(n,0); for(int x=0;x<2;x++){ for(int y=0;y<2;y++){ a[0] = x; a[1] = y; for(int i=1;i<n-1;i++){ //sheep if(a[i]==0){ if(s[i]=='o'){ a[i+1] = a[i-1]; }else{ a[i+1] = !a[i-1]; } //wolf }else{ if(s[i]=='o'){ a[i+1] = !a[i-1]; }else{ a[i+1] = a[i-1]; } } } bool is_ans=true; if(a[0]==0){ if(s[0]=='o'&&a[1]!=a[n-1]){ is_ans = false; }else if(s[0]=='x'&&a[1]==a[n-1]){ is_ans = false; } }else{ if(s[0]=='o'&&a[1]==a[n-1]){ is_ans = false; }else if(s[0]=='x'&&a[1]!=a[n-1]){ is_ans = false; } } if(a[n-1]==0){ if(s[n-1]=='o'&&a[n-2]!=a[0]){ is_ans = false; }else if(s[n-1]=='x'&&a[n-2]==a[0]){ is_ans = false; } }else{ if(s[n-1]=='o'&&a[n-2]==a[0]){ is_ans = false; }else if(s[n-1]=='x'&&a[n-2]!=a[0]){ is_ans = false; } } if(is_ans){ for(int i=0;i<n;i++){ if(a[i]==0) cout <<"S"; else cout <<"W"; } cout << endl; return 0; } } } cout << "-1\n"; }
#include <cstdio> #include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int main() { string s; cin >> s; int n = s.size(); int ret = 0; for(int i=0; i<n; ++i){ int j = i; while(j < n && s[j] == 'O') ++ j; int m = j - i; if(i - m >= 0 && s.substr(i - m, m) == string(m, 'J')){ if(s.substr(j, m) == string(m, 'I')){ ret = max(ret, m); } } i = j; } cout << ret << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); int n; cin >> n; set <int> s; for(int i=0; i<n; i++) { int temp; cin >> temp; s.insert(temp); } if(s.size() == n) { cout << "YES"; } else { cout << "NO"; } }
#include<iostream> using namespace std; int main(){ int n, num[10000]; int max = -1000000, min = 1000000; long long sum = 0; cin >> n; for(int i = 0; i < n; i++){ cin >> num[i]; if(min > num[i]) min = num[i]; if(max < num[i]) max = num[i]; sum = sum + num[i]; } cout << min << " " << max << " " << sum << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; #define rep(i,n) for (int i=0; i<(n); i++) #define reps(i,s,n) for (int i=(s); i<(n); i++) #define rep1(i,n) for (int i=1; i<=(n); i++) #define repr(i,s,n) for (int i=(s)-1; i>=(n); i--) #define print(a) cout << (a) << endl #define all(v) v.begin(), v.end() #define rsort(v) sort(v.rbegin(), v.rend()) struct UnionFind { vector<int> par; UnionFind(int n) : par(n) { par.assign(n, -1); } int root(int a) { if (par[a] < 0) return a; return par[a] = root(par[a]); } int size(int a) { return -par[root(a)]; } void unite(int a, int b) { int ra = root(a), rb = root(b); if (ra == rb) return; if (par[a]>par[b]) swap(a, b); par[ra] += par[rb]; par[rb] = ra; return; } bool same(int a, int b) { int ra = root(a), rb = root(b); return ra == rb; } }; UnionFind uft(100001); struct edge { ll cost; int n1; int n2; bool operator<(const edge& right) const { return cost<=right.cost; } }; int main() { int N; cin >> N; vector<pair<ll,int>> X(N),Y(N); rep(i,N) { ll x,y; cin >> x >> y; X[i] = {x,i}; Y[i] = {y,i}; } set<edge> S; sort(all(X)); sort(all(Y)); rep1(i,N-1) { S.insert({X[i].first-X[i-1].first, X[i].second, X[i-1].second}); S.insert({Y[i].first-Y[i-1].first, Y[i].second, Y[i-1].second}); } ll ans = 0; while (!S.empty()) { auto it = S.begin(); S.erase(it); if (uft.same(it->n1,it->n2)) { continue; } uft.unite(it->n1,it->n2); ans += it->cost; } print(ans); return 0; }
#include <bits/stdc++.h> #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 ll long long #define lld long double #define ALL(x) x.begin(), x.end() using namespace std; const double EPS = 1e-3; const double PI = 3.1415926535897932384626433832795028841971; const int IINF = 1 << 30; const ll INF = 1ll << 60; const ll MOD = 1e9 + 7; int main() { int n; cin >> n; vector<pair<ll, int>> x(n), y(n); rep(i, n) { x[i].second = i; cin >> x[i].first; y[i].second = i; cin >> y[i].first; } sort(ALL(x)); sort(ALL(y)); vector<int> xpos(n), ypos(n); rep(i, n) { xpos[x[i].second] = i; ypos[y[i].second] = i; } priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pque; pque.push({0, 0}); ll ans = 0; vector<bool> used(n, false); while (!pque.empty()) { // cerr << pque.size() << endl; ll cost = pque.top().first; int idx = pque.top().second; pque.pop(); if (used[idx]) { continue; } //cerr << idx << " " << cost << endl; used[idx] = true; //cerr << xpos[idx] << endl; ans += cost; if (xpos[idx] > 0 && !used[x[xpos[idx] - 1].second]) { pque.push({llabs(x[xpos[idx]].first - x[xpos[idx] - 1].first), x[xpos[idx] - 1].second}); } if (ypos[idx] > 0 && !used[y[ypos[idx] - 1].second]) { pque.push({llabs(y[ypos[idx]].first - y[ypos[idx] - 1].first), y[ypos[idx] - 1].second}); } if (xpos[idx] + 1 < n && !used[x[xpos[idx] + 1].second]) { pque.push({llabs(x[xpos[idx]].first - x[xpos[idx] + 1].first), x[xpos[idx] + 1].second}); } if (ypos[idx] + 1 < n && !used[y[ypos[idx] + 1].second]) { pque.push({llabs(y[ypos[idx]].first - y[ypos[idx] + 1].first), y[ypos[idx] + 1].second}); } } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; bool is_squere(long N) { long r = (long) floor(sqrt(N)); return (r * r) == N; } int main() { int N; cin >> N; for(int i = N; i > 0; i--) { if(is_squere(i)) { cout << i << endl; return 0; } } }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; int i = 1; while(1) { if(i*i>n) break; i++; } cout << (i-1)*(i-1) << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; int is_prime(int n){ if(n<2) return 0; else if(n==2) return 1; else if(n%2==0) return 0; double sqrtNum = sqrt(n); for (int i = 3; i <= sqrtNum; i += 2) { if (n % i == 0) { return 0; } } return 1; } int main(){ int n; cin >> n; int cnt=0; int cal=11; vector<int> ans; while(cnt!=n){ if(is_prime(cal)){ if(cal%5==1){ ans.push_back(cal); cnt++; //cout << cal << endl; } } cal++; } for(int i=0;i<n;++i){ if(i!=n-1) cout << ans[i] << " "; else cout << ans[i] << endl; } return 0; }
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <queue> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <fstream> #include <stdio.h> #include <complex> #include <cstdint> #include <tuple> #include <regex> #include <numeric> #define M_PI 3.14159265358979323846 using namespace std; //conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } //typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; //container util //------------------------------------------ #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) //repetition //------------------------------------------ #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define MOD 1000000007 #define SQ(x) ((x)*(x)) int main() { LL k; cin >> k; int n = 50; VLL ret(n); LL x = (k + 49) / 50 - 1; LL t = ((k + 49) / 50) * 50; REP(i, n) ret[i] = 50 + x - i; int loop = ((k + 49) / 50) * 50 - k; REP(i, loop) { REP(j, n) { ret[j]++; } ret[i] -= 51; } printf("%d\n", n); REP(i, n)printf("%lld ", ret[i]); printf("\n"); return 0; }
0
#include<stdio.h> int max(int a,int b){return a>b?a:b;} int main(){ int i,j; int n; char c; int jc=0; int oc=0; int ic=0; int lev=0; int ans=0; scanf("%d",&n); while(scanf("%c",&c)!=EOF){ switch(c){ case 'J': if(jc==0){ oc=0; ic=0; lev=0; } jc++; break; case 'O': if(oc==0){ if(jc>0)lev=jc; if(ic>0)lev=0; jc=0; ic=0; } oc++; if(lev<oc){ oc=0; lev=0; } break; case 'I': if(ic==0){ if(lev>0)lev=oc; jc=0; oc=0; } ic++; if(ic>=lev)ans=max(ans,lev); break; } } printf("%d\n",ans); }
#include<bits/stdc++.h> using namespace std; #define int long long typedef pair<char,int> P; signed main(){ string str; cin>>str; vector<P> A; int idx=0; while(idx<str.size()){ char ch = str[idx]; int cnt=0; while(str[idx] == ch) idx++,cnt++; A.push_back(P(ch,cnt)); } int ans = 0; for(int i=1;i<(int)A.size() - 1;i++){ if(A[i-1].first!='J' || A[i].first !='O' || A[i+1].first !='I')continue; int J = A[i-1].second; int O = A[i].second; int I = A[i+1].second; if(O<=I && O<=J) ans = max(ans,min(O,min(J,I))); } cout<<ans<<endl; return 0; }
1
#pragma region header #define _GLIBCXX_DEBUG #include <bitset> #include <tuple> #include <cstdint> #include <cstdio> #include <cctype> #include <assert.h> #include <stdlib.h> #include <stdio.h> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <limits> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #include <math.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--) #define rev1(i, n) for(int i = (int)(n); i > 0; i--) #define pb push_back #define ts to_string #define all(v) (v).begin(), (v).end() #define resort(v) sort((v).rbegin(), (v).rend()) #define vi vector<int> #define vvi vector<vector<int>> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> #define vs vector <string> #define vvs vector <vector<string>> #define pq priority_queue<int> #define smallpq priority_queue<int, vector<int>, greater<int>> using ll = long long; using P = pair<int, int>; /* ----------------よく使う数字や配列----------------- */ int dx[] = { 1,0,-1,0 }; int dy[] = { 0,1,0,-1 }; constexpr ll mod = 1e9 + 7; constexpr ll inf = INT32_MAX / 2; constexpr ll INF = LLONG_MAX / 2; constexpr long double eps = DBL_EPSILON; constexpr long double pi = 3.141592653589793238462643383279; /* ----------------------end----------------------- */ /* --------------------テンプレート------------------ */ template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } /* ----------------------end----------------------- */ /* --------------------ライブラリ-------------------- */ ll fact(int i) { //階乗 if (i == 0) return 1; return (fact(i - 1)) * i % mod; } ll gcd(ll a, ll b) { //最大公約数 if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { //最小公倍数 return a * b / gcd(a, b); } bool isprime(ll a)//素数判定、primeならtrue,違うならfalse { if (a < 2) return false; else if (a == 2) return true; else if (a % 2 == 0) return false; double m = sqrt(a); for (int i = 3; i <= m; i += 2) { if (a % i == 0) { return false; } } // 素数である return true; } ll keta(ll n) { //桁数を求める if (n == 0) return 1; int count = 0; while (n != 0) { n /= 10; count++; } return count; } ll ketasum(ll n) { //各桁の和 ll sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } /* ----------------------end----------------------- */ #pragma endregion signed main() { int N, K; cin >>N >> K; vi A(N); rep(i, N)cin >> A[i]; sort(all(A), greater<int>()); int sum = 0; rep(i, K) { sum += A[i]; } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; using PLL = pair<ll, ll>; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define repn(i,n) for(int i = 0; i <= (int)(n); i++) #define srep(i,l,n) for(int i = l; i < (int)(n); i++) #define srepn(i,l,n) for(int i = l; i <= (int)(n); i++) #define drep(i,n) for(int i = (int)(n-1); i >= 0; i--) #define drepn(i,n) for(int i = (int)(n); i >= 0; i--) #define size(s) (int)s.size() #define debug(var) do{std::cout << #var << " : ";view(var);}while(0) template<typename T> void view(T e){std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } } template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;} template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);} template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;} template<class T = int> using V = vector<T>; template<class T = int> using VV = vector<V<T>>; bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;} void Yes(){cout << "Yes" << endl;} void No(){cout << "No" << endl;} void YES(){cout << "YES" << endl;} void NO(){cout << "NO" << endl;} void err() {cout << -1 << endl;} #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb push_back #define ep emplace_back const int MOD = 1000000007; const int INF = 1e9; #define PI acos(-1); int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; int ddx[8] = {1,1,1,-1,-1,-1,0,0}; int ddy[8] = {0,1,-1,0,1,-1,1,-1}; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; V<> a(n); cin >> a; sort(rall(a)); int ans = accumulate(a.begin(), a.begin() + k, 0); cout << ans << endl; }
1
#include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<iostream> #define N 1100000 using namespace std; char s[N],t[N]; int p[N],head,tail,n,f[N],ans; int main() { scanf("%d",&n); scanf("%s",s+1); scanf("%s",t+1); bool bo=1; for(int i=1;i<=n;i++) if(s[i]!=t[i]) {bo=0;break;} if(bo) {printf("0\n");return 0;} int j=n; for(int i=n;i>=1;i--) { j=min(j,i); while(j && s[j]!=t[i]) j--; if(j==0) {printf("-1\n");return 0;} f[i]=j; } head=1;tail=0; for(int i=n;i>=1;i--) { while(head<=tail && p[head]-(tail-head)>i) head++; if(f[i]!=f[i+1]) p[++tail]=f[i]; ans=max(ans,tail-head+1); } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int dh[4] = {1, 0, -1, 0}; int dw[4] = {0, 1, 0 , -1}; const int INF = 100000; int main() { int wcount = 0; char grid[100][100]; int H, W; cin >> H >> W; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { cin >> grid[i][j]; if (grid[i][j] == '.') wcount++; } } int dp[100][100]; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { dp[i][j] = INF; } } dp[1][1] = 0; queue<pair<int,int>> que; pair<int, int> p = make_pair(1,1); que.push(p); while(que.size()) { pair<int, int> p = que.front(); que.pop(); int h = p.first, w = p.second; for (int i = 0; i < 4; i++) { int nh = h + dh[i], nw = w + dw[i]; if (grid[nh][nw] == '.' && dp[nh][nw] == INF && nh >=1 && nh <= H && nw >= 1 && nw <= W) { dp[nh][nw] = dp[h][w] + 1; pair<int, int> np = make_pair(nh,nw); que.push(np); } } } if (dp[H][W] == INF) { cout << -1 << endl; } else { cout << wcount - dp[H][W] - 1 << endl; } }
0
#include <iostream> #include <cstdio> using namespace std; #define N 2 void InsertionSort(int* a, int n, int g); void ShellSort(int* a, int n); int a[1000005]; int cnt = 0; int main(void){ int n; cin >> n; for(int i=0; i<n; i++){ cin >> a[i]; } ShellSort(a, n); cout << cnt << endl; for(int i=0; i<n; i++){ printf("%d\n", a[i]); } return 0; } void InsertionSort(int* a, int n, int g){ for(int i=g; i<n; i++){ int v = a[i]; int j = i - g; while(j >= 0 && a[j] > v){ a[j+g] = a[j]; j = j - g; cnt++; } a[j+g] = v; } } void ShellSort(int* a, int n){ int g[100001]; g[0] = 1; int i=0; while(g[i] <= n && i<100){ g[i+1] = g[i] * 3 + 1; i++; } int m = i; for(int i=m-1; i>=0; i--){ InsertionSort(a, n, g[i]); } cout << m << endl; for(int i=m-1; i>0; i--){ printf("%d ", g[i]); } printf("%d\n", g[0]); }
#include <iostream> #include <sstream> #include <string> using namespace std; constexpr int n = 10; int L[n+2],P[n]; int main(){ string str; while(getline(cin,str)){ string tmp; istringstream stream(str); for(int i=0;getline(stream,tmp,',');++i){ L[i] = stod(tmp); } P[0] = L[0]; for(int i=1;i<n;++i) P[i] = P[i-1] + L[i]; int v1 = L[n],v2 = L[n+1]; double p = (double)P[n-1]/(v1+v2)*v1; for(int i=0;i<n;++i){ if(p<=P[i]){ cout << i+1 << endl; break; } } } }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n; cin >> n; int a[n]; rep(i, n) cin >> a[i]; deque<int> r; rep(i, n) { int idx = lower_bound(r.begin(), r.end(), a[i]) - r.begin(); --idx; if (idx < 0) { r.push_front(a[i]); } else { r[idx] = a[i]; } } cout << r.size() << endl; return 0; }
#include <bits/stdc++.h> #define pb push_back #define Int long long using namespace std; const int MAXN=500005; const int NAX=1005; const long long MOD=(long long)(998244353); Int N,M,K; Int X,Y,Z; Int H,W; Int perform(Int x){ return (x*(x+1))/2LL; } vector<Int> A; vector<int> adj[MAXN]; using D=long double; bool inRange(Int a,Int b){ return a*b<=0; } long long C[NAX][NAX]; const D pi=3.14159265359; long long ModExpo(long long x,unsigned long long y,long long M){ Int ans=1; ans=(long long)ans; while(y>0){ if(y&1) ans=((ans%M)*(x%M))%M; y>>=1LL; x=((x%M)*(x%M))%M; } return ans%M; } long long fac[MAXN]; void fillFac(){ fac[0]=1; for(int i=1;i<MAXN;++i){ fac[i]=((fac[i-1]%MOD)*(i%MOD))%MOD; } } long long ModInv(long long x){ return ModExpo(x,MOD-2,MOD); } long long FermatLast(long long n,long long r){ if(r>n) return 0; if(!r) return 1; return (fac[n] * ModInv(fac[r]) % MOD * ModInv(fac[n - r]) % MOD) % MOD; } Int performMod(Int x){ Int y=((x%MOD)*((x+1)%MOD))%MOD; return (y*ModInv(2))%MOD; } Int madd(Int x,Int y){ return (x+y)%MOD; } Int mmul(Int x,Int y){ return ((x%MOD)*(y%MOD))%MOD; } bool isMSB(int bit,Int x){ return (int)(floor(log2l(x)))==bit; } bool VIS[MAXN]; void dfs(int u){ VIS[u]=true; for(auto &v:adj[u]){ if(!VIS[v]){ dfs(v); } } } int main() { string S;cin>>S; vector<char> counts(26); for(auto x:S) ++counts[x-'a']; for(int i=0;i<26;++i){ if(!counts[i]){ cout<<char(i+'a'); exit(0); } } cout<<"None\n"; }
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; int bubble(vector<int>::iterator, vector<int>::iterator); int main() { int n; cin >> n; vector<int> nums; for(int i = 0; i < n; ++i) { int num; cin >> num; nums.push_back(num); } int count = 0; for(int i = 0; i < n - 1; ++i) { count += bubble(nums.begin(), nums.end() - i); } for(auto j = nums.begin(); j != nums.end() - 1; ++j) cout << *j << " "; cout << *(nums.end() - 1) << endl << count << endl; return 0; } int bubble(vector<int>::iterator begin, vector<int>::iterator end) { int count = 0; for(auto i = begin; i != (end - 1); ++i) { if(*i > *(i + 1)) { iter_swap(i, i + 1); ++count; } } return count; }
#include <iostream> using namespace std; int bubblesort(int A[], int N) { int count = 0; int flag = 1; while (flag) { flag = 0; for (int i = N-1; i > 0; i--) { if (A[i] < A[i-1]) { swap(A[i], A[i-1]); flag = 1; count++; } } } for (int j = 0; j < N; j++) { if (j != N -1) cout << A[j] << " "; else cout <<A[j] << "\n"; } cout << count << "\n"; } int main() { int A[100]; int N; cin >> N; for (int i=0; i < N; i++) { cin >> A[i]; } bubblesort(A, N); return 0; }
1
#include<bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; vector<P> G[1001][1001]; int dp[1001][1001]; vector<P> tp; int dfs(int x,int y,int px=1000,int py=1000) { if(dp[x][y]!=0)return dp[x][y]; if(G[x][y].empty())return dp[x][y] = 1; for(auto i:G[x][y]) { dp[x][y] = max(dp[x][y],dfs(i.first,i.second,x,y)+1); } return dp[x][y]; } int main() { int n; cin >> n; vector<vector<P>> input(n); map<P,int> h;//入次数 for(int i = 0;i<n;i++) { for(int j = 0;j<n-1;j++) { int x=i; int y; cin >> y; y--; if(x>y)swap(x,y); input[i].emplace_back(x,y); } } for(int i = 0;i<n;i++) { for(int j =0;j<n-2;j++) { h[input[i][j+1]]++; G[input[i][j].first][input[i][j].second].emplace_back(input[i][j+1]); } } stack<P> S; //入次数がゼロの頂点 for(int i = 0;i<n;i++) { for(int j = i+1;j<n;j++) { if(h[P(i,j)]==0) { S.emplace(i,j); } } } while(S.size()) { auto now = S.top();S.pop(); tp.push_back(now); for(auto i:G[now.first][now.second]) { h[i]--; if(h[i]==0)S.emplace(i); } } if(tp.size()!=n*(n-1)/2) { cout << -1 << endl; return 0; } int ans = 0; for(int i = 0;i<n;i++) { for(int j = i+1;j<n;j++) { ans = max(ans,dfs(i,j)); } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;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; } typedef long long ll; using namespace std; int main(void){ int n;cin>>n; vector<vector<int>> a(n,vector<int>(n)); rep(i,n)rep(j,n-1){ cin>>a[i][j];a[i][j]--; } vector<int> t(n,0); vector<bool> used(n,false); ll cou=0; queue<int> que1; rep(i,n) que1.push(i); while(true){ cou++; //cout<<que1.size()<<endl; rep(i,n)used[i]=false; bool changed=false; queue<int> que2; while(que1.size()){ int i=que1.front();que1.pop(); if(used[i]||t[i]==n-1)continue; int b=a[i][t[i]]; if(!used[b]&&a[b][t[b]]==i&&t[b]<n-1){ used[i]=true;used[b]=true;changed=true; t[i]++;t[b]++; que2.push(i);que2.push(b); } } if(!changed){ cout<<-1<<endl; return 0; } rep(i,n){ if(t[i]!=n-1)changed=false; } if(changed){ cout<<cou<<endl; return 0; } swap(que1,que2); } }
1
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #include <list> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; const ll MOD=998244353; ll powmod(ll a, ll k){ ll ap=a, ans=1; while(k){ if(k&1){ ans*=ap; ans%=MOD; } ap=ap*ap; ap%=MOD; k>>=1; } return ans; } ll inv(ll a){ return powmod(a, MOD-2); } ll dp[303][303][303]; int main() { string s; int k; cin>>s>>k; s+='0'; int cnt=0; vector<int> v; int l=0; for(int i=0; i<s.size(); i++){ if(s[i]=='0'){ v.push_back(cnt); cnt=0; }else{ cnt++; l++; } } int m=v.size(); dp[0][0][0]=1; for(int i=0; i<m; i++){ for(int j=0; j<=l; j++){ for(int t=0; t<=l; t++){ if(dp[i][j][t]==0) continue; for(int u=max(0, v[i]-t); u<=min({l, l+v[i]-t, l+v[i]-j}); u++){ if(u<v[i]){ dp[i+1][j][t-(v[i]-u)]+=dp[i][j][t]; if(dp[i+1][j][t-(v[i]-u)]>=MOD) dp[i+1][j][t-(v[i]-u)]-=MOD; }else{ dp[i+1][j+u-v[i]][t+u-v[i]]+=dp[i][j][t]; if(dp[i+1][j+u-v[i]][t+u-v[i]]>=MOD) dp[i+1][j+u-v[i]][t+u-v[i]]-=MOD; } } } } } ll ans=0; for(int i=0; i<=min(l, k); i++){ (ans+=dp[m][i][0])%=MOD; } cout<<ans<<endl; return 0; }
#include<cstdio> #include<utility> #include<algorithm> #include<vector> #include<queue> #define MAX_N 262144 #define INF 1000000000000000 typedef long long ll; using namespace std; int used[MAX_N]; ll n,d,a[MAX_N],ans=0,two[20]; pair<ll,ll> p[MAX_N],t[2*MAX_N][2]; vector<pair<ll,ll> > g[MAX_N]; void init(){ for(ll i=0;i<2*MAX_N;i++){ t[i][0]=t[i][1]=make_pair(INF,i); } two[0]=1; for(int i=1;i<20;i++){ two[i]=two[i-1]*2; } } void change(ll i){ ll y=p[i].second+MAX_N; t[y][0].first=d*(y-MAX_N)+p[i].first; t[y][1].first=d*(n-1-y+MAX_N)+p[i].first; y/=2; while(y>0){ t[y][0]=min(t[2*y][0],t[2*y+1][0]); t[y][1]=min(t[2*y][1],t[2*y+1][1]); y/=2; } } pair<ll,ll> smq(ll l,ll r,int y){ ll cnt=1,x=l; if(l==r){ return t[l][y]; } while(x>0){ if(x/2*two[cnt]==l&&(x/2+1LL)*two[cnt]-(1LL)==r){ return t[x/2][y]; } else if(x/2*two[cnt]<l||(x/2+1LL)*two[cnt]-(1LL)>r){ return min(t[x][y],smq((x+1LL)*two[cnt-1],r,y)); } x/=2; cnt++; } return t[x][y]; } int main(){ pair<ll,ll> x; priority_queue<pair<ll,ll> ,vector<pair<ll,ll>>, greater<pair<ll,ll>>> q; scanf("%lld %lld",&n,&d); for(ll i=0;i<n;i++){ scanf("%lld",&a[i]); p[i]=make_pair(a[i],i); } init(); sort(p,p+n); change(0); for(ll i=1;i<n;i++){ x=smq(p[i].second+MAX_N,n-1+MAX_N,0); if(x.first<INF){ g[p[i].second].push_back(make_pair(x.first-d*p[i].second+p[i].first,x.second-MAX_N)); g[x.second-MAX_N].push_back(make_pair(x.first-d*p[i].second+p[i].first,p[i].second)); } x=smq(MAX_N,p[i].second+MAX_N,1); if(x.first<INF){ g[p[i].second].push_back(make_pair(x.first-d*(n-1-p[i].second)+p[i].first,x.second-MAX_N)); g[x.second-MAX_N].push_back(make_pair(x.first-d*(n-1-p[i].second)+p[i].first,p[i].second)); } change(i); } used[p[n-1].second]=1; for(int i=0;i<g[p[n-1].second].size();i++){ q.push(g[p[n-1].second][i]); } while(!q.empty()){ x=q.top(); q.pop(); if(used[x.second]==0){ ans+=x.first; for(int i=0;i<g[x.second].size();i++){ q.push(g[x.second][i]); } used[x.second]=1; } } printf("%lld\n",ans); }
0
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <map> #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long 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; } #define MOD 998244353 ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } class mint{ typedef long long ll; public: mint(ll a){ _val = a%_mod; } mint(){ _val=0; } explicit operator ll() {return _val;} ll _val; static const ll _mod = MOD; }; mint operator*(mint mi, mint i){ return mint(mi._val * i._val); } mint operator+(mint mi, mint i){ return mint(mi._val + i._val); } mint operator/(mint mi, mint i){ ll div = modpow(i._val,mi._mod-2,mi._mod); return mint(mi._val*div); } int main(){ /* mint a(8960); mint b = a+3; cout << b._val << endl; b=a+6; cout << b._val << endl; b=a*5; cout << b._val <<endl; b=7*a; cout << b._val <<endl; b=a/2/1; cout << (ll)b<<endl; */ ll n,m,k; cin >> n >> m >> k; vector<mint> kj(n), MM(n); kj[0] = mint(1); MM[0] = mint(1); rep(i,n-1){ kj[i+1] = kj[i] * (i+1); MM[i+1] = MM[i] * (m-1); //cout << i+1 << " kj[i+1]: "<< kj[i+1]._val <<" MM[i+1]: "<< MM[i+1]._val<<endl; } mint sum(0); rep(i,k+1){ mint tmp; tmp= tmp+kj[n-1]*m*MM[n-1-i]; tmp= tmp/kj[n-1-i]/kj[i]; sum = sum+tmp; } cout<<sum._val<<endl; return 0; }
#include <iostream> #include <limits> #include <algorithm> using namespace std; int c[100][100]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, t; int M[101]; cin>>n; cin>>M[0]>>M[1]; for(int i=2; i<=n; i++) { cin>>t>>M[i]; } for(int l=1; l<n; l++) { for(int i=0; i+l<n; i++) { int cost=numeric_limits<int>::max(); for(int j=i+1; j<=i+l; j++) { cost=min(cost, c[i][j-1]+M[i]*M[j]*M[i+l+1]+c[j][i+l]); } c[i][i+l]=cost; } } cout<<c[0][n-1]<<'\n'; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int m; cin >> m; vector<int> nodes; vector<pair<int, int>> child; int n = 0; for (int i = 0; i < m; i++){ string q; cin >> q; if (q == "insert"){ int k; cin >> k; if (n != 0){ int v = 0; while (1){ if (k < nodes[v]){ if (child[v].first != -1){ v = child[v].first; } else { child[v].first = nodes.size(); break; } } else { if (child[v].second != -1){ v = child[v].second; } else { child[v].second = nodes.size(); break; } } } } nodes.push_back(k); child.push_back(make_pair(-1, -1)); n++; } if (q == "find"){ int k; cin >> k; if (n == 0){ cout << "no" << endl; } else { int v = 0; while (1){ if (k < nodes[v]){ if (child[v].first == -1){ cout << "no" << endl; break; } else { v = child[v].first; } } else if (k > nodes[v]){ if (child[v].second == -1){ cout << "no" << endl; break; } else { v = child[v].second; } } else { cout << "yes" << endl; break; } } } } if (q == "delete"){ int k; cin >> k; int v = 0; int u; while (1){ if (k < nodes[v]){ u = v; v = child[v].first; } else if (k > nodes[v]){ u = v; v = child[v].second; } else { break; } } if (child[v].first != -1 && child[v].second != -1){ u = v; int w = child[v].second; while (child[w].first != -1 || child[w].second != -1){ u = w; if (child[w].first != -1){ w = child[w].first; } else { w = child[w].second; } } swap(nodes[v], nodes[w]); v = w; } if (child[v].first != -1 && child[v].second == -1){ int w = child[v].first; nodes[v] = nodes[w]; child[v].first = child[w].first; child[v].second = child[w].second; } else if (child[v].second != -1 && child[v].first == -1){ int w = child[v].second; nodes[v] = nodes[w]; child[v].first = child[w].first; child[v].second = child[w].second; } else { if (child[u].first == v){ child[u].first = -1; } else { child[u].second = -1; } } n--; } if (q == "print"){ //Inorder if (n != 0){ stack<int> S; vector<int> t(nodes.size(), 0); S.push(0); while (!S.empty()){ int v = S.top(); S.pop(); if (t[v] == 1){ cout << ' ' << nodes[v]; } if (t[v] == 0){ S.push(v); if (child[v].second != -1){ S.push(child[v].second); } S.push(v); if (child[v].first != -1){ S.push(child[v].first); } } t[v]++; } } cout << endl; //Preorder if (n != 0){ stack<int> S; vector<int> t(nodes.size(), 0); S.push(0); while (!S.empty()){ int v = S.top(); S.pop(); if (t[v] == 0){ cout << ' ' << nodes[v]; } if (t[v] == 0){ S.push(v); if (child[v].second != -1){ S.push(child[v].second); } S.push(v); if (child[v].first != -1){ S.push(child[v].first); } } t[v]++; } } cout << endl; } } }
#include <iostream> #include <cstdlib> #include <cstdio> using namespace std; typedef struct node{ int key; struct node *parent; struct node *left; struct node *right; } Node, Tree; void insert(Tree **T, int key) { Node *pre = NULL; Node *curr = *T; Node *newNode = NULL; newNode = (Node*)malloc(sizeof(Node)); newNode->key = key; newNode->left = NULL; newNode->right = NULL; while (curr != NULL) { pre = curr; if (key > curr->key) { curr = curr->right; } else { curr = curr->left; } } newNode->parent = pre; //如果根结点存在,则pre一定不为空,反之pre为空 if (pre == NULL) { *T = newNode; } else { if (key > pre->key) { pre->right = newNode; } else { pre->left = newNode; } } } void Inorder(Tree *T) { if (T == NULL) return; Inorder(T->left); cout << " " << T->key; Inorder(T->right); } void Preorder(Tree *T) { if (T == NULL) return; cout << " " << T->key; Preorder(T->left); Preorder(T->right); } bool find(Tree *T, int key) { Node *curr = T; if (T == NULL) return false; while (curr != NULL) { if (key == curr->key) return true; else if (key > curr->key) { curr = curr->right; } else { curr = curr->left; } } return false; } Node *minNode(Node *N) { Node *pre = NULL; Node *curr = N; while (curr != NULL) { pre = curr; curr = curr->left; } return pre; } bool deleteNode(Tree **T, int key) { Node *pre = NULL; Node *curr = *T; Node *nextNode = NULL; Node *p = NULL; if (key == (*T)->key) { p = *T; if ((*T)->left == NULL && (*T)->right == NULL) { (*T) = NULL; free(p); } else if ((*T)->left == NULL) { (*T) = (*T)->right; free(p); } else if ((*T)->right == NULL) { (*T) = (*T)->left; free(p); } else { p = minNode((*T)->right); (*T)->key = p->key; if (p->right == NULL) { if (p->parent->left == p) { p->parent->left = NULL; } else { p->parent->right = NULL; } } else { (*T)->right = p->right; p->right->parent = (*T); } free(p); } return true; } else { while (curr != NULL) { if (key == curr->key) { if (curr->left == NULL && curr->right == NULL) { if (curr->parent->left == curr) { curr->parent->left = NULL; } else { curr->parent->right = NULL; } free(curr); } else if (curr->left == NULL) { if (curr->parent->left == curr) { curr->parent->left = curr->right; } else { curr->parent->right = curr->right; } curr->right->parent = curr->parent; free(curr); } else if (curr->right == NULL) { if (curr->parent->left == curr) { curr->parent->left = curr->left; } else { curr->parent->right = curr->left; } curr->left->parent = curr->parent; free(curr); } else { p = minNode(curr->right); curr->key = p->key; if (p->right == NULL) { if (p->parent->left == p) { p->parent->left = NULL; } else { p->parent->right = NULL; } } else { if (curr->left == p) { curr->left = p->right; } else { curr->right = p->right; } p->right->parent = curr; } free(p); } return true; } else if (key > curr->key) { curr = curr->right; } else { curr = curr->left; } } } return false; } void print(Tree *T) { Inorder(T); cout << '\n'; Preorder(T); cout << '\n'; } int main(void) { Tree *T = NULL; int n; int key; string cmd; //freopen("out.txt", "w", stdout); cin >> n; for (int i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> key; insert(&T, key); //print(T); } else if (cmd == "print"){ print(T); } else if (cmd == "find") { cin >> key; if (find(T, key) == true) { cout << "yes\n"; } else { cout << "no\n"; } } else if (cmd == "delete") { cin >> key; deleteNode(&T, key); } } return 0; }
1
#include<iostream> using namespace std; string rep(string s,int a,int b,string p); string rev(string s,int a,int b); void pri(string s,int a,int b); int main(){ string code,k,p; int n,a,b; string haki; getline(cin,code); cin >> n; getline(cin, haki); for(int i = 0; i < n; i++){ cin >> k; if(k == "replace"){ cin >> a >> b >> p; code = rep(code ,a,b,p); }else if(k == "reverse"){ cin >> a >> b; code =rev(code , a,b); }else if(k == "print"){ cin >> a >> b; pri(code, a,b); } getline(cin,haki); } } string rep(string s,int a, int b, string p){ for(int i = a; i <=b; i++){ s[i] = p[i-a]; } return s; }string rev(string s, int a, int b){ string p = s; for(int i = a; i <= b; i++){ s[i] = p[b-i+a]; } return s; } void pri(string s, int a, int b){ for(int i = a; i <= b;i++){ cout <<s[i]; } cout << endl; }
#include <iostream> #include <string> using namespace std; int main(){ string str, trans, s; int q, a, b; cin>>str; cin>>q; for(int i=0;i<q;i++){ cin>>trans>>a>>b; string left = str.substr(0, a); string right = str.substr(b+1, str.length()-b-1); if(trans == "print"){ s = str.substr(a, b-a+1); cout<<s<<endl; }else if(trans == "reverse"){ s = str.substr(a, b-a+1); str = left; for(int j=s.length()-1;j>=0;j--){ str += s[j]; } str += right; }else if(trans == "replace"){ cin>>s; str = left + s + right; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair using ll = long long; using ld = long double; const int MOD = 1e9+7; const ll INF = 1LL << 60; int main() { string s; cin >> s; if(s == "hi" || s == "hihi" || s == "hihihi" || s == "hihihihi" || s == "hihihihihi") cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; bool ok = true; if (S.size() % 2 == 1) ok = false; for (int i = 0; i < S.size() / 2; i += 2) { if (S.substr(i, 2) != "hi") { ok = false; } } if(ok) cout << "Yes" << endl; else cout << "No" << endl; }
1
#include <bits/stdc++.h> using namespace std; int val=1,n; double k; int main (){ cin>>n>>k; int z=k; while(double(k)/2>1){ val++; k/=2; } if(n>=val){ cout<<pow(2,val)+z*(n-val); } else{ cout<<pow(2,n); } }
#include <bits/stdc++.h> using namespace std; int main() {int N, K, n{1}; cin >> N >> K; while (N--) n += min(n, K); cout << n;}
1
#include<stdio.h> #include<iostream> using namespace std; int main(void){ int n,a[4],t[100],i,c,j; cin>>n; for(i=0;i!=n;i++){ t[i]=0; } for(i=0;i!=n*(n-1)/2;i++){ for(j=0;j!=4;j++) cin>>a[j]; if(a[2]<a[3]) t[a[1]-1]+=3; else if(a[2]>a[3]) t[a[0]-1]+=3; else{ t[a[0]-1]++; t[a[1]-1]++; } } for(i=0;i!=n;i++){ c=1; for(j=0;j!=n;j++){ if(t[i]<t[j]) c++; } cout<<c<<endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<pair<int,int> > team(N); for (int i = 0; i < N; i++) { team[i].second = i + 1; } for (int i = 0; i < N * (N - 1) / 2; i++) { int a, b, c, d; cin >> a >> b >> c >> d; if (c < d) team[b-1].first+=3; if (c > d) team[a-1].first+=3; if (c == d) { team[a-1].first++; team[b-1].first++; } } sort(team.begin(), team.end()); reverse(team.begin(), team.end()); vector<int> rank(N); rank[team[0].second-1] = 1; for (int i = 1, count = 1, r = 1; i < N; i++) { if (team[i].first == team[i-1].first) { rank[team[i].second-1] = r; count++; } else { r += count; rank[team[i].second-1] = r; count = 1; } } for (auto i : rank) cout << i << endl; return 0; }
1
#include <algorithm> #include <iostream> #include <vector> #include <math.h> #include <set> #include <map> #include <string> #include <stack> #include <queue> #include <iomanip> #include <numeric> #include <tuple> #include <bitset> #include <complex> #include <unistd.h> #include <cassert> #include <cctype> #include <random> #define _USE_MATH_DEFINES #define _GLIBCXX_DEBUG using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> plglg; typedef pair<double, ll> pdlg; typedef tuple<int, int, int> tiii; typedef tuple<ll, ll, ll> tlglglg; typedef tuple<double, double, double> tddd; typedef complex<double> xy_t; #define REP(i, x, y) for(ll i = x; i < y; i++) #define PER(i, x, y) for(ll i = x; i > y; i--) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; double pi = 3.141592653589793; ll mod = 1000000007; int intmax = 2147483647; int intmin = -2147483648; ll llmax = 9223372036854775807; ll llmin = -9223372036854775807; int iinf = intmax / 8; ll inf = llmax / 8; double eps = 1e-11; ll multiply(ll x, ll y) { return (x % mod) * (y % mod) % mod; } ll modpower(ll x, ll y) { if (y == 0) { return 1; } else if (y == 1) { return x % mod; } else if (y % 2 == 0) { ll p = modpower(x, y / 2); return p * p % mod; } else { ll p = modpower (x, y / 2); return (p * p) % mod * (x % mod) % mod; } } ll divide(ll x, ll y) { return multiply(x, modpower(y, mod - 2)); } ll frac[1000000]; ll invfrac[1000000]; void fracinvfrac(ll n) { frac[0] = 1; for (int i = 1; i <= n; i++) { frac[i] = multiply(frac[i - 1], i); } invfrac[n] = divide(1, frac[n]); for (int i = n - 1; i >= 0; i--) { invfrac[i] = multiply(invfrac[i + 1], i + 1); } } ll modcombi(ll x, ll y) { if (x < y || y < 0) { return 0; } else if (y == 0) { return 1; } else { return multiply(multiply(frac[x], invfrac[x - y]), invfrac[y]); } } ll dp[1010][1010]; ll mcb[1010][1010]; int main() { ll N, A, B, C, D; cin >> N >> A >> B >> C >> D; fracinvfrac(10000); REP(i, 0, 1010) { fill(dp[i], dp[i] + 1010, 0); REP(j, 0, 1010) { mcb[i][j] = modcombi(i, j); } } dp[0][0] = 1; REP(i, 1, N + 1) { REP(j, 0, N + 1) { dp[i][j] = dp[i - 1][j]; } if (A <= i && i <= B) { REP(j, 0, N + 1) { ll combi = 1; if (j + i * C <= N) { REP(k, 1, C) { combi = multiply(combi, mcb[N - j - i * (k - 1)][i]); } REP(k, C, D + 1) { if (j + i * k <= N) { combi = multiply(combi, mcb[N - j - i * (k - 1)][i]); dp[i][j + i * k] = (dp[i][j + i * k] + multiply(dp[i - 1][j], multiply(combi, invfrac[k]))) % mod; } } } } } } // REP(i, 0, N + 1) { // REP(j, 0, N + 1) { // cout << dp[i][j] << " "; // } // cout << endl; // } ll ans = dp[N][N]; cout << ans << endl; }
#pragma GCC optimize ("O3") #pragma GCC target ("avx") //#include<bits/stdc++.h> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const ll mod = 1e9 + 7; ll modpow(ll A, ll B) { ll kotae = 1; while (B > 0) { if (B & 1) kotae = kotae * A % mod; A = A * A % mod; B >>= 1; } return kotae; } const int IMAX = 1001; ll fac[IMAX + 1], ifac[IMAX + 1]; int dp[1001]; int main() { //cin.tie(0); //ios::sync_with_stdio(false); fac[0] = 1; rep1(i, IMAX) fac[i] = fac[i - 1] * i % mod; ifac[IMAX] = modpow(fac[IMAX], mod - 2); for (int i = IMAX; i > 0; i--) ifac[i - 1] = ifac[i] * i % mod; int N, A, B, C, D; scanf("%d %d %d %d %d", &N, &A, &B, &C, &D); dp[0] = fac[N]; for (int i = A; i <= B; i++) { ll ic = modpow(ifac[i], C); for (int j = N - i * C; j >= 0; j--) { int tmp = dp[j] * ic % mod; int p = C; for (int k = j + i * C; k <= min(j + i * D, N); k += i) { dp[k] = (dp[k] + tmp * ifac[p++]) % mod; tmp = tmp * ifac[i] % mod; } } } printf("%lld", dp[N]); Would you please return 0; }
1
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N=101; char s[N]; short g[N][N],f[N][N],v[N][N][N][N]; inline void upd(short &x,short y) {x=max(x,y);} int main() { int n,m,x,y,ans=0; cin>>n>>m; for(int i=1;i<=n;i++) { scanf("%s",s+1); for(int j=1;j<=m;j++) { if(s[j]=='E') x=i,y=j; int t=(s[j]=='o'); f[i][j]=f[i][j-1]+t,g[i][j]=g[i-1][j]+t; } } memset(v,-1,sizeof(v)); v[0][0][0][0]=0; for(int i=0;i<x;i++) for(int j=0;x+j<=n;j++) for(int p=0;p<y;p++) for(int q=0;y+q<=m;q++) { int d=v[i][j][p][q]; if(d==-1) continue; ans=max(ans,d); if(j+1<x-i) upd(v[i+1][j][p][q],d+f[x-i-1][min(y+q,m-p)]-f[x-i-1][max(y-p,q+1)-1]); if(x+j<n-i) upd(v[i][j+1][p][q],d+f[x+j+1][min(y+q,m-p)]-f[x+j+1][max(y-p,q+1)-1]); if(q+1<y-p) upd(v[i][j][p+1][q],d+g[min(x+j,n-i)][y-p-1]-g[max(x-i,j+1)-1][y-p-1]); if(y+q<m-p) upd(v[i][j][p][q+1],d+g[min(x+j,n-i)][y+q+1]-g[max(x-i,j+1)-1][y+q+1]); } cout<<ans; return 0; }
/// {{{ Author: Wang, Yen-Jen // include #include <bits/stdc++.h> // using using namespace std; // types typedef long long ll; typedef pair<int,int> pii; // macro #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin() , (x).end() #define REP(i , n) for(int i = 0; i < int(n); i++) #define REP1(i , a , b) for(int i = a; i <= int(b); i++) #define F first #define S second #define MP make_pair #define PB push_back #define LC o<<1 , l , m #define RC o<<1|1 , m + 1 , r #define MS(x , v) memset(x , (v) , sizeof(x)) // input inline bool SR(int &x) { return scanf("%d",&x) == 1; } inline bool SR(ll &x) { return scanf("%lld",&x) == 1; } inline bool SR(double &x) { return scanf("%lf",&x) == 1; } inline bool SR(char *s) { return scanf("%s",s) == 1; } inline bool RI() { return true; } template<typename I , typename... T> inline bool RI(I &x , T&... tail) { return SR(x) && RI(tail...); } // output inline void SP(const int x) { printf("%d",x); } inline void SP(const ll x) { printf("%lld",x); } inline void SP(const double x) { printf("%.16lf",x); } inline void SP(const char *s) { printf("%s",s); } inline void PL() { puts(""); } template<typename I , typename... T> inline void PL(const I x , const T... tail) { SP(x); if(sizeof...(tail)) putchar(' '); PL(tail...); } // debug #define WangYenJen #ifdef WangYenJen template<typename I> void _DOING(const char *s , I&& x) { cerr << s << " = " << x << endl; } template<typename I , typename... T> void _DOING(const char *s , I&& x , T&&... tail) { int c = 0; while(*s != ',' || c != 0) { if(*s == '(' || *s == '[' || *s == '{') c++; if(*s == ')' || *s == ']' || *s == '}') c--; cerr << *s++; } cerr << " = " << x << " , "; _DOING(s + 1 , tail...); } #define DEBUG(...) \ do {\ fprintf(stderr , "%s: Line %d - ",__PRETTY_FUNCTION__,__LINE__);\ _DOING(#__VA_ARGS__ , __VA_ARGS__);\ } while(0); #else #define DEBUG(...) #endif // constant number const int INF = 0x3f3f3f3f; const ll INF64 = 0x3f3f3f3f3f3f3f3fll; // random function inline int RAND() { static int x = 880301; return (x = x * 0xdefaced + 1) % 0x7fffffff; } /// }}} const int MAX_N = 100 + 3; int n, m; int sx, sy; char str[MAX_N]; int sum[MAX_N][MAX_N]; short dp[MAX_N][MAX_N][MAX_N][MAX_N]; int get_sum(int x1, int y1, int x2, int y2) { if (x1 > x2 || y1 > y2) return 0; else return sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1]; } int main() { RI(n, m); REP1(i, 1, n) { SR(str + 1); REP1(j, 1, m) { if (str[j] == 'E') sx = i, sy = j; else if (str[j] == 'o') sum[i][j] = 1; sum[i][j] = sum[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; } } int ans = 0; REP1(i, 0, sx - 1) { REP1(j, 0, sy - 1) { REP1(x, 0, n - sx) { REP1(y, 0, m - sy) { ans = max(ans, (int)dp[i][j][x][y]); int v = 0; if (i + x + 1 <= sx - 1) v = get_sum(sx - i - 2, max(sy - j - 1, y), sx - i - 1, min(sy + y, m - j)); else v = 0; dp[i + 1][j][x][y] = max((int)dp[i + 1][j][x][y], dp[i][j][x][y] + v); if (i + x + 1 <= n - sx) v = get_sum(sx + x, max(sy - j - 1, y), sx + x + 1, min(sy + y, m - j)); else v = 0; dp[i][j][x + 1][y] = max((int)dp[i][j][x + 1][y], dp[i][j][x][y] + v); if (j + y + 1 <= sy - 1) v = get_sum(max(sx - i - 1, x), sy - j - 2, min(sx + x, n - i), sy - j - 1); else v = 0; dp[i][j + 1][x][y] = max((int)dp[i][j + 1][x][y], dp[i][j][x][y] + v); if (j + y + 1 <= m - sy) v = get_sum(max(sx - i - 1, x), sy + y, min(sx + x, n - i), sy + y + 1); else v = 0; dp[i][j][x][y + 1] = max((int)dp[i][j][x][y + 1], dp[i][j][x][y] + v); } } } } PL(ans); return 0; }
1
#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 fi first #define se second #define mp make_pair #define pb push_back typedef long long ll; typedef pair<int,int> ii; typedef vector<int> vi; typedef unsigned long long ull; typedef long double ld; typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds; int dp[311][311][311]; string s; int solve(int l, int r, int k) { if(k<0) return -int(1e9); if(l>r) return 0; if(dp[l][r][k]>=0) return dp[l][r][k]; if(l==r) return 1; int ans = max(solve(l,r-1,k),solve(l+1,r,k)); ans=max(ans,solve(l+1,r-1,k-(s[l]!=s[r]))+2); return (dp[l][r][k]=ans); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>s; int k; cin>>k; memset(dp,-1,sizeof(dp)); cout<<solve(0,int(s.length())-1,k)<<'\n'; }
#include <algorithm> #include <cctype> #include <cstring> #include <cstdio> using namespace std; inline int read(int f = 1, int x = 0, char ch = ' ') { while(!isdigit(ch = getchar())) if(ch == '-') f = -1; while(isdigit(ch)) x = x*10+ch-'0', ch = getchar(); return f*x; } const int N = 3e2+5; int n, m, f[N][N][N], ans; char s[N]; int main() { scanf("%s", s+1), n = strlen(s+1), m = read(); for(int i = 1; i <= n; ++i) f[0][i][i] = 1; for(int k = 0; k <= m; ++k) for(int l = 2; l <= n; ++l) for(int i = 1, j = l; j <= n; ++i, ++j) { f[k][i][j] = max(f[k][i+1][j], f[k][i][j-1]); if(s[i] == s[j]) f[k][i][j] = max(f[k][i][j], f[k][i+1][j-1]+2); if(k) f[k][i][j] = max(f[k][i][j], f[k-1][i+1][j-1]+2); } for(int i = 0; i <= m; ++i) ans = max(ans, f[i][1][n]); printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d,e,f,g,h,i,j,k,l,m,n,t,prothom,boro; cin>>n>>k; cin>>a; boro=a; for(int i=1;i<n;i++) { cin>>b; a=__gcd(a,b); boro=max(boro,b); } if(k<=boro && k%a==0) { cout<<"POSSIBLE"<<endl; } else { cout<<"IMPOSSIBLE"<<endl; } }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s, c, p; int q, a, b; cin >> s >> q; while (q--) { cin >> c >> a >> b; int l = b - a + 1; if (c == "print") cout << s.substr(a, l) << endl; else if (c == "reverse") reverse(s.begin() + a, s.begin() + b + 1); else { cin >> p; s = s.replace(a, l, p); } } return 0; }
0
#include<bits/stdc++.h> using namespace std ; #define ll long long #define ld long double #define ff first #define ss second const ll mod = 1e9 +7 ; ll multiply(ll a, ll b ){ return ((a % mod) * (b % mod)) % mod; } ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; } ll sub(ll a, ll b) { return ((a%mod) - (b % mod)+ mod) % mod ; } //////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// void solve(){ ll n , m , v , p ; cin >> n >> m >> v >> p ; vector < ll > a(n) ; for( int i = 0 ; i < n ; i++) cin >> a[i] ; sort(a.begin(),a.end()); vector < ll > pre(n) ; pre[0] = a[0] ; for( int i = 1 ; i < n ; i++) pre[i] = pre[i-1] + a[i] ; // for( int i = 0 ; i < n ; i++) cout << a[i] << " " ; // cerr << "\n" ; int ans = 0 ; for( int i = n-1 ; i >= 0 ; i--){ ll r = n-p ; if(i>=r){ ans++; continue ; } if(a[i]+m>=a[r]){ int vote = v-p-i ; if(vote>0){ ll sum = pre[r]-pre[i] ; // cout << i << " " << vote << " " << sum << " " << (a[i]+m)*(r-i)-sum<< "\n"; if(m*vote<=(a[i]+m)*(r-i)-sum){ ans++; } } else{ ans++; } } else{ break ; } } cout << ans ; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1 ; //cin >> t ; while(t--){ solve( ); } }
#include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #ifndef AT_HOME #define getchar() IO::myGetchar() #define putchar(x) IO::myPutchar(x) #endif namespace IO { static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; inline char myGetchar() { static char buf[IN_BUF], *ps = buf, *pt = buf; if (ps == pt) { ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin); } return ps == pt ? EOF : *ps++; } template<typename T> inline bool read(T &x) { bool op = 0; char ch = getchar(); x = 0; for (; !isdigit(ch) && ch != EOF; ch = getchar()) { op ^= (ch == '-'); } if (ch == EOF) { return false; } for (; isdigit(ch); ch = getchar()) { x = x * 10 + (ch ^ '0'); } if (op) { x = -x; } return true; } inline int readStr(char *s) { int n = 0; char ch = getchar(); for (; isspace(ch) && ch != EOF; ch = getchar()) ; for (; !isspace(ch) && ch != EOF; ch = getchar()) { s[n++] = ch; } s[n] = '\0'; return n; } inline void myPutchar(char x) { static char pbuf[OUT_BUF], *pp = pbuf; struct _flusher { ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } }; static _flusher outputFlusher; if (pp == pbuf + OUT_BUF) { fwrite(pbuf, 1, OUT_BUF, stdout); pp = pbuf; } *pp++ = x; } template<typename T> inline void print_(T x) { if (x == 0) { putchar('0'); return; } static int num[40]; if (x < 0) { putchar('-'); x = -x; } for (*num = 0; x; x /= 10) { num[++*num] = x % 10; } while (*num){ putchar(num[*num] ^ '0'); --*num; } } template<typename T> inline void print(T x, char ch = '\n') { print_(x); putchar(ch); } inline void printStr_(const char *s, int n = -1) { if (n == -1) { n = strlen(s); } for (int i = 0; i < n; ++i) { putchar(s[i]); } } inline void printStr(const char *s, int n = -1, char ch = '\n') { printStr_(s, n); putchar(ch); } } using namespace IO; int H, W, N; int main() { read(H), read(W), read(N); print((N - 1) / std::max(H, W) + 1); }
0
#include <iostream> #include <iomanip> #include <map> #include <unordered_map> #include <list> #include <set> #include <unordered_set> #include <vector> #include <utility> #include <algorithm> #include <queue> #include <cstdint> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long ll; typedef unsigned long long ull; constexpr ll inf = INT64_MAX / 4; constexpr double pi = asin(1) * 2; constexpr ll mod = 1000000007; pair<ll, ll> extgcd(ll a, ll b) { if (b == 0)return make_pair(a, 0); auto t = extgcd(b, a % b); return make_pair(t.second, t.first - t.second * (a / b)); } ll invmod(ll n) { return extgcd(n, mod).first; } class modint { public: ll n; modint() = default; explicit modint(ll number) { n = ((number % mod) + mod) % mod; } bool operator==(modint e) { return n == e.n; } modint operator+(modint e) { return modint(n + e.n); } modint operator-(modint e) { return modint(n - e.n); } modint operator-(ll e) { return modint(n - e); } modint operator*(modint e) { return modint(n * e.n); } modint operator/(modint e) { return modint(n * invmod(e.n)); } }; template<class T> T f1(T n, T m) { return m * m * (n * n * (n + T(1)) - (n * (n + T(1)) * (T(2) * n + T(1))) / T(3)); } template<class T> T f2(T n, T m) { return (f1(n, m) + f1(m, n)) / T(2); } template<class T> T comb(T n, T k) { return k == T(0) ? T(1) : comb(n - T(1), k - T(1)) * n / k; } int main() { modint n, m, k; cin >> n.n >> m.n >> k.n; cout << (f2(n, m) * comb(n * m - 2, k - 2)).n << endl; }
#include <iostream> #include <string> #include <vector> #include <list> #include <map> #include <queue> #include <stack> #include <algorithm> #include <fstream> #include <sstream> #include <iomanip> #define ll long long #define MAX_N 200001 using namespace std; long long MOD = 1000000007; long long fact[MAX_N]; void factorial(int N){ fact[0]=1; for(int i=0; i<N; i++){ fact[i+1]=(i+1)*fact[i]%MOD; } } map<long long, long long> inv; long long inverse(long long x){ if (inv.count(x)){ return inv[x]; } long long p=MOD-2; long long ret=1; long long tmp=x; while(p>0){ if(p%2==1)ret=ret*tmp%MOD; tmp=tmp*tmp%MOD; p/=2; } inv[x]=ret; return ret; } long long comb(int a, int b){ long long ret=fact[a]*inverse(fact[a-b])%MOD; return ret*inverse(fact[b])%MOD; } int main(){ ll N, M, K; cin >> N >> M >> K; factorial(200000); ll ans=0; for(ll y=0; y<N-1; y++){ if(N*M-2<K-2)continue; ans+=((N+y)*(N-y-1)/2-y*(N-y-1))%MOD*M%MOD*M%MOD*comb(N*M-2,K-2)%MOD; ans%=MOD; } for(ll x=0; x<M-1; x++){ if(M*N-2<K-2)continue; ans+=((M+x)*(M-x-1)/2-x*(M-x-1))%MOD*N%MOD*N%MOD*comb(M*N-2,K-2)%MOD; ans%=MOD; } cout << ans << endl; }
1
#include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <unordered_map> using namespace std; int main() { int N; string A, B, C; cin >> N >> A >> B >> C; string str = ""; vector<int> count(10e3, 0); for (int i = 0; i < N; ++i) { int x, y, z; x = y = z = 0; if (A[i] == B[i]) x++; if (B[i] == C[i]) y++; if (A[i] == C[i]) z++; if (x >= y && x >= z) str += A[i]; else if (y >= x && y >= z) str += B[i]; else if (z >= y && z >= x) str += C[i]; } int cnt = 0; for (int i = 0; i < N; ++i) { if (A[i] != str[i]) cnt++; if (B[i] != str[i]) cnt++; if (C[i] != str[i]) cnt++; } printf("%d\n", cnt); }
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<cmath> #include<cstdio> #include<queue> #include<deque> using namespace std; typedef pair<int,int> ii; typedef long long ll; typedef pair<ll,ll> pll; const int INF=1e9; const ll MOD=1e9+7; ll gcd(ll x,ll y){ if(y==0) return 0; return gcd(y,x%y); } int dis2(int x1,int y1,int x2,int y2){ return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); } int main(){ int n;cin>>n; string a,b,c;cin>>a>>b>>c; int ans=0; for(int i=0;i<n;i++){ if(a[i]!=b[i]&&b[i]!=c[i]&&c[i]!=a[i]) ans+=2; else if(a[i]==b[i]&&b[i]==c[i]) continue; else ans++; } cout<<ans<<endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define FOR(i, j, k) for(int i=(j); i<=(k); i++) #define FFOR(i, j, k) for(int i=(j); i<(k); i++) #define DFOR(i, j, k) for(int i=(j); i>=(k); i--) #define bug(x) cerr<<#x<<" = "<<(x)<<'\n' #define pb push_back #define mp make_pair #define bit(s, i) (((s)>>(i))&1LL) #define mask(i) ((1LL<<(i))) #define builtin_popcount __builtin_popcountll #define __builtin_popcount __builtin_popcountll using ll=long long; using ld=long double; mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi=acos(0)*2; template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;} template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);} template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);} template <typename T> inline void writeln(T x){write(x); putchar('\n');} template <typename CT, typename T> inline void reset_container(CT &c, int sz, T v){c.resize(sz); for(auto &x: c) x=v;} #define taskname "C" int n; ll x; ll start; class subject{ public: ll b, l, u, w; void input(){ read(b); read(l); read(u); start+=b*l; w=(x-b)*u+b*l; } ll cost(ll x){ if(x<=b*l){ return (x-1)/l+1; } else{ x-=b*l; return b+(x-1)/u+1; } } } s[100001]; ll f[100001]; int get(int i){ int res=n; int low=0, high=n-1, mid; while(low<=high){ mid=(low+high)/2; ll sum=f[mid]; if(mid<i) sum+=s[i].w; if(sum>=start){ res=mid; high=mid-1; } else low=mid+1; } return res; } int main(){ #ifdef Aria if(fopen(taskname".in", "r")) freopen(taskname".in", "r", stdin); #endif // Aria read(n); read(x); FOR(i, 1, n) s[i].input(); sort(s+1, s+n+1, [](subject A, subject B){ return A.w>B.w; }); ll ans=x*n; FOR(i, 1, n) f[i]=f[i-1]+s[i].w; FOR(i, 1, n){ int j=get(i); ll res=x*j; if(j>i) res-=x; ll now=start-f[j]; if(j>i) now+=s[i].w; if(now>0) ans=min(ans, res+s[i].cost(now)); else ans=min(ans, res); } writeln(ans); }
#include<iostream> #include<algorithm> #include<string> using namespace std; int main(){ string X,Y; int l=0; int point; cin>>X>>Y; int N[X.size()+1][Y.size()+1]; for(int i=0;i<=Y.size();i++){ N[0][i]=i; } for(int i=1;i<=X.size();i++){ N[i][0]=i; } for(int i=1;i<=X.size();i++){ for(int j=1;j<=Y.size();j++){ if(X[i-1]==Y[j-1]){ point=0; }else{ point=1; } N[i][j]=min(min(N[i-1][j]+1,N[i][j-1]+1),N[i-1][j-1]+point); } } cout<<N[X.size()][Y.size()]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string A; cin >> A; if(A.at(0) == '7') { cout << "Yes" << endl; } else if(A.at(1) == '7') { cout << "Yes" << endl; } else if(A.at(2) == '7') { cout << "Yes" << endl; } else{ cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { string s ; cin>>s; bool a[27]={}; for(int i=0;i<s.size();i++) { a[s[i]-'a']=true; } for(int i=0;i<27;i++) { if (i==26) cout<<"None"; else if(!a[i]) { char c='a'; c=c+i; cout<<c; break; } } return 0; }
0
#include <iostream> #include <string> #include <algorithm> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <numeric> #include <cmath> using namespace std; typedef long long int ll; typedef pair<int,int> P; #define all(x) x.begin(),x.end() const ll mod = 1e9+7; const ll INF = 1e9; const ll MAXN = 1e9; int main() { ll n,m; cin>>n>>m; ll ans=1; vector<int> a(n),b(m),flag(n*m,0); for(int i = 0; i < n; i++){ cin>>a[i]; a[i]--; if(flag[a[i]]>0){ ans=0; } flag[a[i]]=1; } for(int i = 0; i < m; i++){ cin>>b[i]; b[i]--; if(flag[b[i]]>=2){ ans=0; } if(flag[b[i]]>0) flag[b[i]]=3; else{ flag[b[i]]=2; } } int j=-1,k=-1; for(int i = n*m-1; i >= 0 ; i--){ if(flag[i]==3){ j++; k++; }else if(flag[i]==2){ ans*=j+1; ans%=mod; k++; }else if(flag[i]==1){ ans*=k+1; ans%=mod; j++; }else{ ans*=((j+1)*(k+1)-(n*m-(i+1))); ans%=mod; } } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define endl "\n" #define loop(i,s,e) for(int i=s;i<e;i++) #define int long long #define vi vector<int> #define S(v) sort(v.begin(),v.end()) #define RS(v) sort(v.rbegin(),v.rend()) #define R(v) reverse(v.begin(),v.end()) #define mxpq(T) priority_queue <T> #define mnpq(T) priority_queue<T,vector<T>,greater<T>> #define prv(v) for(auto &x : v)cout << x << " "; #define piirv(v) for(auto &x : v)cout << x.ff << " " << x.ss << endl; #define prvv(v) for(auto &x : v)for(auto &y : x.ss)cout << y << " "; #define mp make_pair #define pii pair<int,int> #define mset(a,f) memset(a , f , sizeof(a)) #define pb push_back #define inf INT_MAX #define INF LLONG_MAX typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset; const int N = 1e5 + 6; const int M = 1e9+7; int A[N]; string s; int n , d; int dp[N][102][2]; int alpha(int id , int sum , int ok) { if(id == n)return (sum == 0); int &abs = dp[id][sum][ok]; if(abs != -1)return abs; int ans = 0; int k = s[id]-'0'; if(ok)k = 9; loop(i,0,k+1) { if(i < k) { int nsum = (sum + i)%d; ans += alpha(id+1 , nsum , 1); ans %= M; } else { int nsum = (sum + i)%d; ans += alpha(id+1 , nsum , ok); ans %= M; } } return abs = ans; } void solve() { cin >> s >> d; n = s.length(); mset(dp,-1); int ans = alpha(0,0,0)-1; if(ans == -1)ans = M-1; cout << ans; } int32_t main() { cout<<fixed<<setprecision(16); ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while(t--)solve(); }
0
#include <bits/stdc++.h> #define REP(i, e) for(int (i) = 0; (i) < (e); ++(i)) #define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i)) #define ALL(c) (c).begin(), (c).end() #define PRINT(x) cout << (x) << "\n" using namespace std; using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>; template<typename T> auto compare = [](T x, T y) -> bool{return (x < y);}; const int MOD = 1000000007; ll N, M; ll A[100010]; signed main(){ cin >> N >> M; REP(i, N) cin >> A[i]; ll sum = 0; map<ll, ll> mp; mp[0]++; REP(i, N){ sum = (sum + A[i]) % M; mp[sum]++; } ll ans = 0; for(auto p : mp){ ans += p.second * (p.second - 1) / 2; } PRINT(ans); return 0; }
#define _USE_MATH_DEFINES #include <iostream> #include <string> #include <algorithm> #include <vector> #include <cmath> #include <map> #include <iomanip> #include <deque> #include <set> #include <climits> #include <memory> #include <numeric> #include <utility> #define rep(i,a,b) for(int i=a;i<b;++i) #define rrep(i,a,b) for(int i=a;i>=b;--i) #define fore(i,a) for(auto &i:a) #define INF INT_MAX/2; #define PI 3.14159265358979323846264338327950L; const int INF2 = 1001001001; typedef long long ll; using namespace std; using vi = vector<int>; using vll = vector<ll>; ll mod = 1e9 + 7; //ll llmax = 10000000000000000000ll; const ll LINF = 1e18; using namespace std; using Graph = vector<vector<int>>; int cnt_digit(ll N) { int digit = 0; while (N > 0) { N /= 10; digit++; } return digit; } // 最大公約数計算 ll gcd(ll a, ll b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } // 最小公倍数の計算 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } struct union_find { vector<int> par, r; union_find(int n) { par.resize(n); r.resize(n); init(n); } void init(int n) { for (int i = 0; i < n; i++) par[i] = i; for (int i = 0; i < n; i++) r[i] = 0; } int find(int x) { if (par[x] == x)return x; else return find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (r[x] < r[y]) { par[x] = y; } else { par[y] = x; if (r[x] == r[y]) { r[x]++; } } } }; const int MAX_N = 110000; int memo[MAX_N]; int rec(int n) { if (n == 0)return 0; if (memo[n] != -1)return memo[n]; int res = n; for (int pow6 = 1; pow6 <= n; pow6 *= 6)res = min(res, rec(n - pow6) + 1); for (int pow9 = 1; pow9 <= n; pow9 *= 9)res = min(res, rec(n - pow9) + 1); return memo[n] = res; } int calc(int x) { int ret = 0; while (x % 2 == 0) { x /= 2; ret++; } return ret; } int a[12][12]; int D; ll G; vector<ll> P, C; //aのp乗を求めるアルゴリズム long modpow(long a, int p) { if (p == 0)return 1; if (p % 2 == 0) { //pが偶数のとき int halfP = p / 2; long half = modpow(a, halfP); return half * half % mod; } else { //pが奇数の時は、 //pを偶数にするために1減らす return a * modpow(a, p - 1) % mod; } } //(10*9*8)/(3*2*1); //10*9*8 -> ansMul //3*2*1 -> ansDiv; long calcComb(int a, int b) { if (b > a - b) return calcComb(a, a - b); long ansMul = 1; long ansDiv = 1; rep(i, 0, b) { ansMul *= (a - i); ansDiv *= (i + 1); ansMul %= mod; ansDiv %= mod; } //ansMul /ansDivをやりたい //ansDivの逆元を使って求めよう。 long ans = ansMul * modpow(ansDiv, mod - 2) % mod; return ans; } bool is_war(const vector<int>& x, const vector<int>& y) { for (int Z = -100; Z <= 100; Z++) { bool is_ok = true; rep(i, 0, x.size()) { if (x[i] >= Z) { is_ok = false; } rep(i, 0, y.size()) { if (y[i] < Z) { is_ok = false; } } } if (is_ok)return false; } return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> x(n + 1),y(m+1); cin >> x[0] >> y[0]; rep(i, 0, n) { cin >> x[i + 1]; } rep(i, 0, m) { cin >> y[i + 1]; } if (is_war(x, y)) { cout << "War" << endl; } else { cout << "No War" << endl; } }
0
#include <bits/stdc++.h> #define SIZE 300005 #define MOD 1000000007LL #define INF 1 << 30 #define LLINF 1LL << 60 #define REP(i,n) for(int i=0;i<n;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define DOWN(i,b,a) for(int i=b;i>=a;i--) #define SET(a,c) memset(a,c,sizeof a) #define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define FOREACH(i,c) for(auto (i) : (c)) #define BIT(i,j) ((i)>>(j))&1 #define ALL(o) (o).begin(), (o).end() #define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end()) #define SQ(x) ((x)*(x)) using namespace std; typedef long long ll; typedef valarray<int> Array; typedef pair<ll,ll> Pll; typedef pair<int, int> Pii; typedef pair<double, double> Pdd; template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i]<<((i==a.size()-1)?"\n":" ");}} ll gcd(ll a,ll b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);} ll lcm(ll a,ll b){return a==0||b==0?0:a*b/gcd(a,b);} int a[144]; void flip(int x, int y) { if(x>11||y>11||x<0||y<0) return; if(a[x+y*12]) { a[x+y*12] = 0; flip(x+1,y); flip(x-1,y); flip(x,y+1); flip(x,y-1); } } void solve() { int cnt = 0; REP(i,144) if(a[i]) { cnt++; flip(i%12,i/12); } cout << cnt << endl; } int main() { string s; int i = 0; while(cin >> s) { REP(j,12) a[i*12+j] = s[j]-'0'; i=(++i)%12; if(i==0) solve(); } return 0; }
#include<iostream> #include<string> #define COL 12 #define ROW 12 using namespace std; int field[COL][ROW]; void mapping(int y, int x){ //cout<<"mapping("<<y<<", "<<x<<")\n"; if(field[y][x]==1){ field[y][x]=0; if(y+1<COL) mapping(y+1,x); if(x+1<COL) mapping(y,x+1); if(y-1>=0) mapping(y-1,x); if(x-1>=0) mapping(y,x-1); } return; } int main(){ string str; int i, j,cnt; bool loop = true; while(true){ for(i=0;i<COL;i++){ cin>>str; if(cin.eof()){ loop = false; break; } for(j=0;j<ROW;j++){ field[i][j] = str[j] - '0'; } } if(loop==false) break; cnt=0; for(i=0;i<COL;i++){ for(j=0;j<ROW;j++){ if(field[i][j]==1){ mapping(i,j); cnt++; }; } } cout<<cnt<<endl; } return 0; }
1
#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; string s, t; cin >> N; cin >> s >> t; int len=s.size() + t.size(); rep(i,s.size()) { if (t.find(s.substr(i)) != string::npos) { len -= (s.size() - i); break; } } cout << len << endl; return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=2e6+500; int n;char st[N]; int nxt[N]; int main() { scanf("%d",&n); scanf("%s",st+1);scanf("%s",st+n+1); for(int i=1;i<=n;i++)swap(st[i],st[i+n]); int j=0; for(int i=2;i<=2*n;i++) { while(j&&st[j+1]!=st[i])j=nxt[j]; if(st[j+1]==st[i])++j; nxt[i]=j; } int t=nxt[2*n];while(t>n)t=nxt[t]; cout<<2*n-t<<endl; }
1
#include <bits/stdc++.h> #define FOR(i,bg,ed) for(ll i=(bg);i<(ed);i++) #define REP(i,n) FOR(i,0,n) #define MOD 1000000007 //#define int long long using namespace std; typedef long long ll; const int INF = 1e9; int m, nMin, nMax; int P[210]; pair<int, int> ans[210]; signed main() { while (true) { cin >> m >> nMin >> nMax; if (m == 0 && nMin == 0 && nMax == 0) break; REP(i,m) cin >> P[i]; sort(P, P + m, greater<int>()); int idx = 0; for (int i=nMin; i<=nMax; i++) { ans[idx++] = make_pair(P[i-1] - P[i], i); } sort(ans, ans + idx); cout << ans[idx-1].second << endl; } }
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define watch(a) { cout << #a << " = " << a << endl; } template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } template<class T> void operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) cin >> e; } typedef long long ll; int const inf = 1<<29; int main() { int M, N[2]; for(; cin >> M >> N[0] >> N[1] && M;) { vector<int> vs(M); cin >> vs; int maxgap = 0; int maxnum = -1; REP(num, N[0], N[1] + 1) { if(maxgap <= vs[num - 1] - vs[num]) { maxgap = vs[num - 1] - vs[num]; maxnum = num; } } cout << maxnum << endl; } return 0; }
1
#include <bits/stdc++.h> #define repd(i, a, b) for (ll i = (a); i < (b); i++) #define repb(i, n) for (ll i = (n)-1; i >= 0; i--) #define rep(i, n) repd(i, 0, n) using namespace std; using ll = long long; using ul = unsigned long long; using ld = long double; ll mod = 1000000007; int main() { ll n, m; cin >> n >> m; set<ll> div; for (int i = 1; i * i < m; i++) { if (m % i == 0) { div.insert(i); div.insert(m / i); } } ll ans = 1; for (auto iter = div.begin(); iter != div.end(); iter++) { if (m / (*iter) >= n) { ans = (*iter); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; const ll LINF = 1e18; const int INF = 1e9; const ll MOD = 1000000007; int main(){ int r, g; cin >> r >> g; cout << 2*g - r << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; long long r = 0, g = 0, b = 0; for(int i = 0; i < n; i++) { if(s.at(i) == 'R') r++; else if(s.at(i) == 'G') g++; else b++; } long long sum = r * g * b; for(int i = 0; i < (n - 2); i++) { for(int j = i + 1; j < (n - 1); j++) { if(s.at(i) != s.at(j)) { if(j + (j - i) >= n) { continue; } char x = s.at(j + (j - i)); if(s.at(i) != x && s.at(j) != x) sum--; } } } cout << sum << endl; }
#include <algorithm> #include <climits> #include <iostream> #include <string> #include <utility> #include <vector> #include <set> #include <map> #include <queue> #include <iomanip> #include <cmath> #include <stack> using namespace std; using ll = long long int; template <class T> ostream &operator<<(ostream &os, vector<T> &v) { for (auto i = v.begin(); i != v.end(); i++) { os << *i << " "; } return os; } void solve(std::string A){ ll x = 0; ll len = A.size(); for(ll i=0; i<26; i++) { ll cnt = 0; for(auto c : A) { if(c == 'a'+i) cnt++; } x += cnt*(cnt-1) / 2; } std::cout << (len * (len-1))/2 - x + 1<< std::endl; } int main(){ std::string A; std::cin >> A; solve(A); return 0; }
0
#include<iostream> using namespace std; long N,M,mod=998244353; long F[1<<25],I[1<<25]; long power(long a,long b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;} long C(long a,long b) { return a<b||a<0||b<0?0:F[a]*I[b]%mod*I[a-b]%mod; } long H(long a,long b){return a<0?0:C(a+b-1,b-1);} long calc(long n,long m,long s) { long ret=0; for(int i=0;i<=n&&i<=m;i++) { if(s-i&1)continue; (ret+=C(n,i)*H((s-i)/2,n)%mod)%=mod; } return ret; } main() { cin>>N>>M; long T=2*M+N; F[0]=1; for(long i=1;i<T;i++)F[i]=F[i-1]*i%mod; I[T-1]=power(F[T-1],mod-2); for(long i=T-1;i--;)I[i]=I[i+1]*(i+1)%mod; long ans=0; for(int i=0;i<=N&&i<=M;i++) { if((3*M-i)%2==1)continue; (ans+=C(N,i)*H((3*M-i)/2,N)%mod)%=mod; } long ret=0; for(int i=0;i<=N&&i<=M;i++) { if((M-i)%2==1)continue; (ret+=C(N,i)*H((M-i)/2,N)%mod)%=mod; } for(int i=0;i<=N-1&&i<=M;i++) { if((M-i)%2==1)continue; (ret+=mod-C(N-1,i)*H((M-i)/2,N-1)%mod)%=mod; } cout<<(calc(N,M,3*M)-calc(N,M,M)*N%mod+calc(N-1,M,M)*N%mod+mod)%mod<<endl; }
#define _USE_MATH_DEFINES #include "bits/stdc++.h" #define rep(i,a,b) for (int i = (a); i < (b); i++) using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef complex<double> com; const int mod = 1e9 + 7; const int MOD = 998244353; const int inf = 2e9; ll mpow(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; } vector<int> fac, finv; void factor(int N) { fac.resize(N); finv.resize(N); fac[0] = finv[0] = 1; rep(i, 1, N)fac[i] = 1LL * fac[i - 1] * i % mod; finv[N - 1] = mpow(fac[N - 1], mod - 2); for (int i = N - 2; i; --i) finv[i] = 1LL * finv[i + 1] * (i + 1) % mod; } int nCr(int n, int m) { if (m < 0 || n < m) return 0; return 1LL * (1LL * fac[n] * finv[m] % mod) * finv[n - m] % mod; } int main() { int n, a, b, c, d; cin >> n >> a >> b >> c >> d; factor(200010); vector<vector<ll>> dp(n+10); rep(i, 0, n + 10) dp[i].resize(n + 10, 0); dp[0][0] = 1; rep(i, 0, n + 1)rep(j, 0, b - a + 2) { int idx = a + j; dp[i][j + 1] = (dp[i][j + 1] + dp[i][j]) % mod; rep(k, c, d + 1) { int sum = idx * k + i; if (sum > n) break; ll base = nCr(n - i, idx * k); base = (1LL * base * fac[idx * k]) % mod; base = (1LL * base * mpow(finv[idx], k)) % mod; base = (1LL * base * finv[k]) % mod; dp[sum][j + 1] = (dp[sum][j + 1] + (dp[i][j] * base) % mod) % mod; } } printf("%lld", dp[n][b - a + 1]); return 0; }
0
//Radhe Radhe #include<bits/stdc++.h> #define ll long long #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define mod 1000000007 using namespace std; int main() { fast_io ll i,a,b,c,d,j,k; cin>>a>>b>>c; cout<<max(a+b,max(a*b,a-b)); return 0; }
#include<bits/stdc++.h> #define Vsort(a) sort(a.begin(), a.end()) #define rep(i,n) for(int i = 0; i < (n); i++) #define Srep(i,a,n) for(int i = (a); i < (n); i++) #define ll long long #define P pair<int, int> using namespace std; int main(){ int x,y,z; cin >> x >> y >> z; swap(x,y); swap(x,z); cout << x << " " << y << " " << z << endl; }
0
#include <bits/stdc++.h> using namespace std; vector<vector<int>> graph; vector<int> parent; vector<int> visited; vector<pair<int,int>> depth; void DFS(int v, int parent_v, int d){ parent[v] = parent_v; depth[v] = {d, v}; for(int child_v : graph[v]){ DFS(child_v, v, d+1); } } int main(){ int s = 0;// starting point int n, k; cin >> n >> k; graph.resize(n); parent.resize(n); visited.resize(n); depth.resize(n); int ans = 0; int a0; cin >> a0; if(a0 != 1) ans = 1; for(int i=1; i<n; i++){ int a; cin >> a; a -= 1; graph[a].push_back(i); } DFS(0,-1,0); sort(depth.begin(),depth.end()); reverse(depth.begin(),depth.end()); for(int i=0; i<n; i++){ if(depth[i].first <= k) break; int current = depth[i].second; for(int j=0; j<k; j++){ if(visited[current] == true) break; visited[current] = true; current = parent[current]; if(j == k-1) ans += 1; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve(int n,int mn,int mx){ int t,st=0,maxx=0,ans=0; vector<int> v; cin >> st; for(int i=0;i<n-1;i++){ cin >> t; v.push_back(st-t); st=t; } for(int i=mn-1;i<mx;i++){ if(maxx<v[i]){ maxx=v[i]; } if(maxx<=v[i]){ ans=i+1; } } cout<<ans<<endl; } int main(void){ int a,b,c; while(cin>>a>>b>>c,a){ solve(a,b,c); } }
0
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <iostream> #include <algorithm> #include <vector> #include <string> #include <queue> #include <map> #include <set> #include <functional> #include <cmath> #include <numeric> #include <iterator> #include <fstream> #define SIZE 100005 #define MOD 1000000007 using namespace std; class BK { public: static void solve(std::istream& cin, std::ostream& cout) { int64_t k, t; cin >> k >> t; vector<int64_t> vec(t); for (int i = 0; i < t; ++i) { cin >> vec[i]; } sort(vec.begin(), vec.end()); int64_t max_vec = vec.back(); vec.pop_back(); int64_t sum = accumulate(vec.begin(), vec.end(), 0l); cout << max(max_vec - sum - 1, 0l) << endl; } }; int main() { BK solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include<iostream> int main(){ long long a,b; std::cin >> a >> b; long long ans = 1e18; if(std::abs(a) == std::abs(b)){ std::cout << 1 << std::endl; return 0; } if(-a < b){ ans = std::min(ans, a + b + 1); } if(a < -b){ ans = std::min(-a - b + 1, ans); } if(-a < -b){ ans = std::min(ans,a - b + 2); } if(a < b){ ans = std::min(ans, b - a); } std::cout << ans << std::endl; }
0
#include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> using namespace std; #define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++) #define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++) #define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--) #define all(v) begin(v), end(v) #define pb(a) push_back(a) #define fr first #define sc second #define INF 2000000000 #define int long long int #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a,b) (abs((a) - (b)) < EPS) #define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) ) #define LE(n, m) ((n) < (m) + EPS) #define LEQ(n, m) ((n) <= (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) #define GEQ(n, m) ((n) + EPS >= (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; int dx[]={1, -1, 0, 0}; int dy[]={0, 0, 1, -1}; int const MOD = 1000000007; ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;} int madd(int a, int b) {return (a + b) % MOD;} int msub(int a, int b) {return (a - b + MOD) % MOD;} int mmul(int a, int b) {return (a * b) % MOD;} int minv(int a) {return mod_pow(a, MOD-2);} int mdiv(int a, int b) {return mmul(a, minv(b));} namespace std { bool operator<(const P& a, const P& b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } int board[15][15]; signed main() { int n; while(cin >> n, n) { memset(board, 0, sizeof(board)); rep(i,0,n) rep(j,0,n) cin >> board[i][j]; rep(i,0,n) rep(j,0,n) { board[i][n] += board[i][j]; board[n][n] += board[i][j]; } rep(j,0,n) rep(i,0,n) { board[n][j] += board[i][j]; } repq(i,0,n) { repq(j,0,n) printf("%5lld", board[i][j]); cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; while (getline(cin, str)) { int cnt = 0; for (int i = 0; i < 26; ++i) { for (int j = 0; j < (int)str.size(); ++j) { if (str[j] >= 'a' && str[j] <= 'z') { str[j] = (str[j] - 'a' + 1) % 26 + 'a'; } } cnt++; if (str.find("the") != string::npos || str.find("this") != string::npos || str.find("that") != string::npos) break; } for (int i = 0; i < 26; ++i) { for (int j = 0; j < (int)str.size(); ++j) { if (str[j] >= 'a' && str[j] <= 'z') { str[j] = (str[j] - 'a' + cnt) % 26 + 'a'; } } } cout << str << endl; } }
0
#include<bits/stdc++.h> #include<iomanip> using namespace std; int main(){ int N, K; cin >> N >> K; vector<float> p(N); for(int i=0; i<N; i++) cin >> p.at(i); for(int i=0; i<N; i++) p.at(i) = (p.at(i) + 1) / 2; vector<double> q(N); q.at(0) = p.at(0); for(int i=1; i<N; i++) q.at(i) = q.at(i-1) + p.at(i); vector<double> r(N - K + 1); r.at(0) = q.at(K-1); for(int i=1; i<N-K+1; i++){ r.at(i) = q.at(i+K-1) - q.at(i-1); } double max = 0; for(int i=0; i<N-K+1; i++){ if(max < r.at(i)) max = r.at(i); } cout << fixed << setprecision(12); cout << max << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long lol; //マクロ //forループ関係 //引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか //Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる #define REP(i,n) for(lol i=0;i<lol(n);i++) #define REPD(i,n) for(lol i=n-1;i>=0;i--) #define FOR(i,a,b) for(lol i=a;i<=lol(b);i++) #define FORD(i,a,b) for(lol i=a;i>=lol(b);i--) #define VL vector<lol> //xにはvectorなどのコンテナ #define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい #define SIZE(x) lol(x.size()) //sizeをsize_tからllに直しておく //定数 #define PI 3.1415926535897932385 //pi #define INF 1000000000000 //10^12:極めて大きい値,∞ #define MOD 1000000007 //10^9+7:合同式の法 #define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用) //最大値最小値 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 gcd(int a,int b){return b?gcd(b,a%b):a;} int main() { cout << fixed << setprecision(7) ; double result{0}; lol n; cin >> n; lol k; cin >> k; VL p(n); REP(i, n) { cin >> p[i]; } FOR(i, 0, k-1) { double nume = (p[i]*(p[i]+1))/2; result += nume/p[i]; } double MA = result; FOR(i, 1, n-k) { double nume1 = (p[i-1]*(p[i-1]+1))/2; MA -= nume1/p[i-1]; double nume2 = (p[k+i-1]*(p[k+i-1]+1))/2; MA += nume2/p[k+i-1]; //cout << MA << " "; result = max(result, MA); } //cout << endl; cout << result << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int N, M; while (cin >> N >> M, N) { vector<int> p(N); int sum = 0; for (int i = 0; i < N; i++) { cin >> p[i]; sum += p[i]; } sort(p.begin(), p.end()); for (int i = N - M; i >= 0; i -= M) { sum -= p[i]; } cout << sum << endl; } return 0; }
#include <iostream> #include <string> #include <algorithm> #include <numeric> #include <boost/tokenizer.hpp> #include <boost/lexical_cast.hpp> #include <vector> #include <list> using namespace std; using namespace boost; int main(int argc, char* argv[]) { using sprtr = char_separator<char>; using tknzr = tokenizer<sprtr>; sprtr sep(" ", "", keep_empty_tokens); int f, mf, mo; string line; while(1) { int n, m; { getline(cin, line); tknzr tkns(line, sep); auto it = tkns.begin(); n = lexical_cast<int>(*it++); m = lexical_cast<int>(*it); } if( n == 0 && m == 0 ) { break; } list<int> ps; { getline(cin, line); tknzr tkns(line, sep); for( auto p : tkns ) { //cout << p << endl; ps.push_back(lexical_cast<int>(p)); } } ps.sort(greater<int>()); int div = n / m; int mod = n % m; int sum = 0; auto it = ps.begin(); for( int i = 0; i < div; i++ ) { for( int j = 0; j < m; j++ ) { auto p = *it++; if( j == m-1 ) continue; sum += p; } } if( mod ) { for( int i = 0; i < mod; i++ ) { auto p = *it++; sum += p; } } cout << sum << endl; ps.clear(); } return 0; }
1
#ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS # include <intrin.h> # define __builtin_popcount __popcnt #endif #define _USE_MATH_DEFINES #include <stdio.h> #include <iostream> #include <vector> #include <unordered_map> #include <stack> #include <list> #include <math.h> #include <map> #include <iomanip> #include <functional> #include <string> #include <algorithm> #include <queue> #include <set> using namespace std; template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template<class T> istream& operator>>(istream& is, vector<T>& v) { for (auto i = begin(v); i != end(v); i++) is >> *i; return is; } #define MOD 1000000007 struct Q { int b_id; int steps; int q_id; Q(int b_id, int q_id) : b_id(b_id), q_id(q_id), steps(0) {} bool operator<(const Q& other) const { return std::tie(b_id, q_id) < std::tie(other.b_id, other.q_id); } }; int ofs[1000000]; int ans[1000000]; vector<set<Q>> qs; void merge_into(int from, int to) { if (qs[from].size() > qs[to].size()) { swap(qs[from], qs[to]); swap(ofs[from], ofs[to]); } for (auto x : qs[from]) { x.steps += ofs[from] - ofs[to]; qs[to].insert(x); } qs[from].clear(); } int main() { #if defined(_DEBUG) freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> x(n); for (int i = 0; i < n; i++) cin >> x[i]; qs.resize(n); int L; cin >> L; int q; cin >> q; for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; a--, b--; if (a > b) swap(a, b); qs[a].insert(Q(b, i)); } for (int i = 0; i < n; i++) { while (qs[i].size() > 0 && qs[i].begin()->b_id <= i) { auto f = *qs[i].begin(); qs[i].erase(qs[i].begin()); ans[f.q_id] = f.steps + ofs[i]; } ofs[i]++; int to = lower_bound(x.begin(), x.end(), x[i] + L + 1) - x.begin() - 1; merge_into(i, to); } for (int i = 0; i < q; i++) cout << ans[i] << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; /***********debug tools***********/ template<class T> inline ostream& operator<<(ostream& os,vector<T> arr) {os << "["; for(int i = 0; i < (int)arr.size(); i++)os << arr[i] << (i == (int)arr.size() - 1 ? "]" : ",");os << endl;return os;} template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A,B>& p){os << "{" << p.first << "," << p.second << "}";return os; } #define prvec(v) cerr<<#v<<": [";for(int __i = 0;__i < (int)(v).size(); __i++)cerr<<((v)[__i])<<(__i+1==(int)(v).size()?"]\n":","); #define print(x) cerr<<#x<<": "<<(x)<<endl /*********************************/ const int D = 30; const int MAX_N = 100000; int n, l; vector<int> x; vector<vector<int>> tor, tol; void ftor() { for(int i = 0; i < n; i++) { int L = i + 1, R = n; //ooooxxx while(R - L > 1) { int mid = (L + R) / 2; (x[mid] - x[i] <= l ? L : R) = mid; } tor[0][i] = L; } tor[0][n - 1] = n - 1; for(int i = 0; i < D - 1; i++) { for(int j = 0; j < n; j++) { tor[i + 1][j] = tor[i][tor[i][j]]; } } } void ftol() { for(int i = 1; i < n; i++) { int L = -1, R = i - 1;//xxxxxoooo while(R - L > 1) { int mid = (L + R) / 2; (x[i] - x[mid] <= l ? R : L) = mid; } tol[0][i] = R; } tol[0][0] = 0; for(int i = 0; i < D - 1; i++) { for(int j = 0; j < n; j++) { tol[i + 1][j] = tol[i][tol[i][j]]; } } } int main() { cin >> n; x.resize(n), tor.resize(D, vector<int>(n)); tol = tor; for(int i = 0; i < n; i++)cin >> x[i]; cin >> l; ftor(); ftol(); int Q; cin >> Q; while(Q--) { int a, b; cin >> a >> b; a--,b--; if(a > b) { int l = 0, r = 2 * n;//xxxxxxoooo while(r - l > 1) { int mid = (l + r) / 2; int pos = a; for(int i = 0; i < D; i++) { if(mid & (1<<i))pos = tol[i][pos]; } (pos <= b ? r : l) = mid; } cout << r << endl; } else { int l = 0, r = 2 * n;//xxxxxxoooo while(r - l > 1) { int mid = (l + r) / 2; int pos = a; for(int i = 0; i < D; i++) { if(mid & (1<<i))pos = tor[i][pos]; } (pos >= b ? r : l) = mid; } cout << r << endl; } } }
1
#include <bits/stdc++.h> #define be(v) (v).begin(),(v).end() #define pb(q) push_back(q) typedef long long ll; using namespace std; const ll mod=1000000007; #define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); string s; cin>>s; cout << (s.substr(0,(s.length())-8))<<endl; return 0; }
#include<iostream> using namespace std; int main() { int d; while(cin >> d) { long long ans = 0; int y = d; while (y <= 600 - d) { ans += (long long)y * y * d; y += d; } cout << ans << endl; } }
0
#include<bits/stdc++.h> #define REP(i,n) for(int i=0;i<n;i++) #define RREP(i,n) for(int i=n;i>=0;i--) #define FOR(i,m,n) for(int i=m;i<n;i++) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() using namespace std; typedef long long ll; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } //const ll INF = 1LL << 60; //const int INF = 1001001001; int main(void){ int k,x; cin >> k >> x; for(int i=x-(k-1);i<=x+(k-1);i++) printf("%d ",i); return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; int *ary = new int[n]; for (int i = 0; i < n; ++i) { cin >> ary[i]; } int _n = n - 1; for (int j = _n; 0 <= j; --j) { if (j == 0) cout << ary[j] << endl; else cout << ary[j] << " "; } delete[] ary; return 0; }
0
#include <iostream> #include <string> using namespace std; int main() { int n; char c; string s; while(cin >> n) { int cd[13]={0},pr[5]={0}; cd[n-1]++; for(int i=0; i<4; i++) { cin >> c >> n; cd[n-1]++; } for(int i=0; i<13; i++) { pr[cd[i]]++; } bool st=false; for(int i=0; i<10; i++) { int sm=0; for(int j=0; j<5; j++) sm+=(cd[(i+j)%13]?1:0); if(sm==5) st=true; } s="null"; if(pr[4]) s="four card"; else if(pr[3]&&pr[2]) s="full house"; else if(st) s="straight"; else if(pr[3]) s="three card"; else if(pr[2]==2) s="two pair"; else if(pr[2]) s="one pair"; cout << s << endl; } }
#include <iostream> #include <cstdio> using namespace std; int main(){ int n[5], num[14], pair[3],i; while (scanf("%d,%d,%d,%d,%d",&n[0],&n[1],&n[2],&n[3],&n[4]) != EOF){ i = 0; for (int i = 0; i < 14; i++) num[i] = 0; for (int i = 0; i < 3; i++) pair[i] = 0; for (int i = 0; i < 5; i++) num[n[i] - 1]++; for (int i = 0; i < 13; i++){ if (num[i] > 1) pair[num[i] - 2]++; } if (pair[2]) cout << "four card" << endl; else if (pair[0] && pair[1]) cout << "full house" << endl; else if (pair[1]) cout << "three card" << endl; else if (pair[0] == 2) cout << "two pair" << endl; else if (pair[0]) cout << "one pair" << endl; else{ num[13] = num[0]; for (i = 0; i < 10; i++){ if (num[i] == 1 && num[i] == num[i + 1] && num[i + 1] == num[i + 2] && num[i + 2] == num[i + 3] && num[i + 3] == num[i + 4]) { cout << "straight" << endl; break; } } } if (i == 10) cout << "null" << endl; } return 0; }
1
#include<bits/stdc++.h> #define LL long long using namespace std; const int N=1e6+10; int rd() { int x=0,w=1;char ch=0; while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();} return x*w; } int n,q[N<<1],hd,tl,tg; char cc[N],ss[N]; int main() { /// n=rd(); scanf("%s%s",cc+1,ss+1); hd=n+1,tl=n; int ans=0; for(int i=n,j=n;i;--i) { while(j&&(i<j||cc[j]!=ss[i])) --j; if(!j){puts("-1");return 0;} if(i==j){hd=n+1,tl=n,tg=0;continue;} if(hd>tl) q[++tl]=j-tg,q[++tl]=i-tg,ans=max(ans,1); else { --tg; q[--hd]=j-tg; while(hd<=tl&&q[tl]+tg>=i) --tl; q[++tl]=i-tg; ans=max(ans,tl-hd); } while(i>j&&ss[i-1]==ss[i]) --i; } printf("%d\n",ans); return 0; }
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; vector<int>A(N +1 ); for (int n = 0;n<N;++n) { cin >> A[n + 1]; } vector<int>B(N + 1); for (int n = N;n>=1;--n) { int sum = 0; for (int m = n + n;m <= N;m += n) { sum += B[m]; } if (A[n] == sum%2) { B[n] = 0; } else{ B[n] = 1; } } vector<int>Ans; for (int n = 0; n < N; ++n) { if (0 != B[n + 1]) { Ans.push_back(n + 1); } } cout << Ans.size()<<endl; for (int n = 0; n < Ans.size(); ++n) { if (0 !=n) { cout << " "; } cout << Ans[n]; } return 0; }
0
#include <iostream> #include <iomanip> #include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <functional> #include <sstream> #include <string> #include <utility> #include <map> #include <memory> #include <set> #include <vector> #include <deque> #include <list> #include <stack> #include <queue> using namespace std; class APSPath { public: int solve(); }; int APSPath::solve() { const int infty = 2147483647; int vn, en; cin >> vn >> en; vector<vector<int>> adj (vn, vector<int>(vn, infty)); for ( auto i = 0; i < en; ++i ) { int u, v; cin >> u >> v; cin >> adj[u][v]; } for ( auto i = 0; i < vn; ++i ) adj[i][i] = 0; for ( auto k = 0; k < vn; ++k ) { for ( auto i = 0; i < vn; ++i ) { for ( auto j = 0; j < vn; ++j ) { if ( adj[i][j] > adj[i][k] + adj[k][j] && adj[i][k] != infty && adj[k][j] != infty ) adj[i][j] = adj[i][k] + adj[k][j]; } } } for ( auto i = 0; i < vn; ++i ) if ( adj[i][i] < 0 ) { cout << "NEGATIVE CYCLE" << endl; return 0; } for ( auto i = 0; i < vn; ++i ) { for ( auto j = 0; j < vn; ++j ) { if ( j ) cout << " "; if ( adj[i][j] == infty ) cout << "INF"; else cout << adj[i][j]; } cout << endl; } return 0; } int main() { APSPath apsp; apsp.solve(); return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; void dfs1(vector<vector<pair<int,int>>> &graph, vector<int> &num, vector<int> &used, int node, int &con){ if(used[node]) return ; used[node] = true; for(pair<int,int> next : graph[node]){ dfs1(graph, num, used, next.first, con); } num[con++] = node; } void dfs2(vector<vector<pair<int,int>>> &graph, vector<int> &scc, vector<int> &used, int node, int same){ if(used[node]) return ; used[node] = true; for(pair<int,int> next : graph[node]){ dfs2(graph, scc, used, next.first, same); } scc[node] = same; } void Strongly_Connected_Components(vector<vector<pair<int,int>>> &graph, vector<int> &scc){ vector<int> used(graph.size()), num(graph.size()); vector<vector<pair<int,int>>> graph2(graph.size()); int con = 0; scc.resize(graph.size()); for(int i = 0; i < graph.size(); i++){ dfs1(graph, num, used, i, con); } for(int i = 0; i < graph.size(); i++){ for(int j = 0; j < graph[i].size(); j++){ graph2[graph[i][j].first].push_back(make_pair(i, graph[i][j].second)); } } used.clear(); used.resize(graph.size()); for(int i = (int)graph.size()-1, same = 0; i >= 0; i--){ if(!used[num[i]]){ dfs2(graph2, scc, used, num[i], same); same++; } } } signed main(){ int V, E; int Q; vector<int> scc; vector<vector<pair<int,int>>> graph; cin>>V>>E; graph.resize(V); for(int i = 0; i < E; i++){ int s, t; cin>>s>>t; graph[s].push_back(make_pair(t,0)); } Strongly_Connected_Components(graph, scc); cin>>Q; for(int i = 0; i < Q; i++){ int u, v; cin>>u>>v; cout<<(scc[u] == scc[v])<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #define rep(i, n) for(ll i = 0; i < n; i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define fi first #define se second #define pb push_back #define debug(x) cerr << #x << ": " << x << endl #define debug_vec(v) cerr << #v << ":"; rep(i, v.size()) cerr << " " << v[i]; cerr<<endl template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = (1<<30) - 1; const ll LINF = (1LL<<60) - 1; template<typename T> struct BinaryIndexedTree { /* 0-indexed */ vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; vector<pii> Q[505050]; void solve() { int n, q; cin >> n >> q; vector<int> C(n); rep(i, n) { cin >> C[i]; C[i]--; } vector<int> ans(q); rep(i, q) { int l, r; cin >> l >> r; l--; r--; Q[r].pb({l, i}); } BinaryIndexedTree<int> BIT(n); vector<int> R(n + 1, -1); rep(i, n) { if (R[C[i]] != -1) BIT.add(R[C[i]], -1); BIT.add(i, 1); R[C[i]] = i; for(auto query : Q[i]) { int l = query.fi, r = i; int idx = query.se; ans[idx] = BIT.sum(r) - BIT.sum(l - 1); } } rep(i, q) { cout << ans[i] << endl; } } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include<bits/stdc++.h> //Ithea Myse Valgulious namespace chtholly{ typedef long long ll; #define re0 register int #define rel register ll #define rec register char #define gc getchar //#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++) #define pc putchar #define p32 pc(' ') #define pl puts("") /*By Citrus*/ char buf[1<<23],*p1=buf,*p2=buf; inline int read(){ int x=0,f=1;char c=gc(); for (;!isdigit(c);c=gc()) f^=c=='-'; for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0'); return f?x:-x; } template <typename mitsuha> inline bool read(mitsuha &x){ x=0;int f=1;char c=gc(); for (;!isdigit(c)&&~c;c=gc()) f^=c=='-'; if (!~c) return 0; for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0'); return x=f?x:-x,1; } template <typename mitsuha> inline int write(mitsuha x){ if (!x) return 0&pc(48); if (x<0) pc('-'),x=-x; int bit[20],i,p=0; for (;x;x/=10) bit[++p]=x%10; for (i=p;i;--i) pc(bit[i]+48); return 0; } inline char fuhao(){ char c=gc(); for (;isspace(c);c=gc()); return c; } }using namespace chtholly; using namespace std; const int yuzu=5e5; typedef int fuko[yuzu|10]; struct node { int l,r,id; void rd(int x) { read(l),read(r),id=x; } }qr[yuzu|10]; fuko c,cnt,lxy; int main() { int i,n,q,bk; read(n),read(q),bk=sqrt(n); for (i=1;i<=n;++i) c[i]=read(); for (i=1;i<=q;++i) qr[i].rd(i); sort(qr+1,qr+q+1,[&](node a,node b){return a.l/bk^b.l/bk?a.l<b.l:a.r<b.r;}); int nl=1,nr=0,ans=0; for (i=1;i<=q;++i) { for (;nr<qr[i].r;) !cnt[c[++nr]]++?++ans:0; for (;nr>qr[i].r;) !--cnt[c[nr--]]?--ans:0; for (;nl<qr[i].l;) !--cnt[c[nl++]]?--ans:0; for (;nl>qr[i].l;) !cnt[c[--nl]]++?++ans:0; lxy[qr[i].id]=ans; } for (i=1;i<=q;++i) printf("%d\n",lxy[i]); }
1
#include<iostream> using namespace std; int main(){ int n; while(cin>>n && n){ int a=0,b=0; for(int i=0;i<n;i++){ int x,y; cin>>x>>y; if(x>y)a+=x+y; else if(x==y){ a+=x;b+=y; } else b+=x+y; } cout<<a<<" "<<b<<endl; } }
#include <bits/stdc++.h> #define fo(i,a) for(i = 0;i < a;i++) using namespace std; int main(void) { int n,m,fr=0,i,a[10001],b[10001],flag[10001]={0}; flag[0] = 1; cin >> n >> m ; while(n != 0 || m != 0){ fo(i,m){ cin >> a[i] >> b[i] ; if(a[i] == 1){ fr++ ; flag[b[i]] = 2 ; } } fo(i,m){ if(flag[a[i]] == 2 && flag[b[i]] == 0){ fr++ ; flag[b[i]] = 3 ; } } fo(i,m){ if(flag[a[i]] == 0 && flag[b[i]] == 2){ fr++ ; flag[a[i]] = 3 ; } } cout << fr-1 << endl; fr = 0; flag[0] = 1 ; fill (flag,flag + 10001,0); cin >> n >> m ; } return 0; }
0
#include<bits/stdc++.h> using namespace std; long long solve(){ int n; cin>>n; long long A[n][n]; long long dp[(1<<n)]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>A[i][j]; dp[0]=0; for(int i=1;i<1<<n;i++){ dp[i]=0; for(int j=0;j<n;j++){ if(i&(1<<j)){ for(int k=j+1;k<n;k++){ if(i&(1<<k)) dp[i]+=A[j][k]; } } } } for(int mask=1;mask<1<<n;mask++){ for(int ss=mask;ss>0;ss=(ss-1)&mask){ dp[mask]=max(dp[mask],dp[ss]+dp[mask^ss]); } } return dp[(1<<n)-1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout<<solve()<<"\n"; }
#include <stdio.h> #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <deque> #include <set> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #include <chrono> #include <random> #include <time.h> #include <fstream> #define ll long long #define rep2(i,a,b) for(ll i=a;i<=b;++i) #define rep(i,n) for(ll i=0;i<n;i++) #define rep3(i,a,b) for(ll i=a;i>=b;i--) #define pii pair<int,int> #define pll pair<ll,ll> #define pq priority_queue<int> #define pqg priority_queue<int,vector<int>,greater<int>> #define pb emplace_back #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define endl "\n" #define all(c) begin(c),end(c) using namespace std; int in() {int x;scanf("%d",&x);return x;} ll lin() {ll x;scanf("%lld",&x);return x;} template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} void print(vec v){for(auto e:v)cout<<e<<" ";cout<<endl;} void print(vecll v){for(auto e:v)cout<<e<<" ";cout<<endl;} void print(vector<vec> v){for(auto e:v){for(auto ee:e)cout<<ee<<" ";cout<<endl;}} void print(map<int,int> mp){for(auto e:mp)cout<<e.first<<" "<<e.second<<endl;cout<<endl;} const int INF=1e6; const int MAX_ROW = 510; // to be set appropriately const int MAX_COL = 510; // to be set appropriately struct UnionFind{ vector<int> par,sizes; UnionFind(int n):par(n+1),sizes(n+1,1){ rep2(i,1,n)par[i]=i; } int find(int x){ return (x==par[x]?x:par[x]=find(par[x])); } void unite(int x,int y){ x=find(x);y=find(y); if(x==y)return ; if(sizes[x]<sizes[y]){ swap(x,y); } par[y]=x; sizes[x]+=sizes[y]; } bool same(int x,int y) {return (find(x)==find(y));} int size(int x) {return sizes[find(x)];} }; ll ans,d; void solve(int n,vector<ll> v,vector<ll> w){ vector<pair<ll,int>> x,y; rep(i,n){ x.pb(v[i],i); y.pb(w[i],i); } sort(all(x));sort(all(y)); vec b(n),c(n); vecll B(n),C(n); int now=0; rep(i,n){ while(now<x[i].second){ b[now]=x[i].second; B[now]=x[i].first+w[now]; now++; } } B[n-1]=LONG_MAX; C[0]=LONG_MAX; now=n-1; rep(i,n){ while(now>y[i].second){ c[now]=y[i].second; C[now]=y[i].first+v[now]; now--; } } UnionFind uf(n); // print(b);print(B); // print(c);print(C); rep(i,n){ if(B[i]<C[i]){ if(!uf.same(i,b[i])){ ans+=B[i]; uf.unite(i,b[i]); } } else{ if(!uf.same(i,c[i])){ ans+=C[i]; uf.unite(i,c[i]); } } } map<int,vec> mp; rep(i,n){ mp[uf.find(i)].pb(i); } if(mp.size()==1)return ; else{ int nn=mp.size(); vecll vvc,ww; for(auto vv:mp){ ll t=LONG_MAX,s=LONG_MAX; for(auto e:vv.second){ chmin(t,v[e]); chmin(s,w[e]); } vvc.push_back(t); ww.pb(s); } solve(nn,vvc,ww); } } main(){ int n=in();d=in(); vector<ll> v,w; vecll a; rep(i,n)a.pb(in()); rep(i,n){ v.pb(a[i]+d*i); w.pb(a[i]-d*i); } solve(n,v,w); cout<<ans; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define MOD 1000000007 #define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++) #define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++) #define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++) #define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++) #define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--) #define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--) #define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--) #define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--) #define iter(i, it) for(auto &(i): (it)) template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) { cout << "(" << m.first << ", " << m.second << ")"; return s; } template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) { ll c = 0; cout << "{ "; iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", "); cout << "}"; return s; } template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) { cout << "{ "; rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", "); cout << "}"; return s; } template<typename T> ostream& operator<<(ostream &s, const list<T> &v) { ll c = 0; cout << "{ "; iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", "); cout << "}"; return s; } int main(void) { ll N; vector<ll> L; ll count = 0; cin >> N; L.resize(2 * N); rep(i, 2 * N) cin >> L[i]; sort(L.begin(), L.end()); rep(i, N) count += L[2 * i]; cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int size; cin>>size; int arr[2*size]={}; int ans=0; for(int i=0;i<2*size;i++){ cin>>arr[i]; } sort(arr,arr+2*size); for(int i=0;i<2*size;i++){ if(i%2==0){ ans=ans+arr[i]; } } cout<<ans; return 0; }
1
#include <stdio.h> char str[16][16]; /* void erase(int x, int y) { if (x < 0 || x > 11 || y < 0 || y > 11) return; if (str[y][x] == '0') return; str[y][x] = '0'; erase(x + 1, y); erase(x, y + 1); erase(x - 1, y); erase(x, y - 1); } */ void is(int i, int j) { int n; int k; str[i][j] = '0'; k = 1; for (n = 0; n < 2; n++){ if (str[i + k][j] == '1' && i + k >= 0 && i + k < 12){ is(i + k, j); } if (str[i][j + k] == '1' && j + k >= 0 && j + k < 12){ is(i, j + k); } k = -1; } } int main(void) { int i, j; int ans; int a; // int case_ = 0; while (1){ // if (case_) fgets(str[0], 16, stdin); // case_++; ans = 0; for (i = 0; i < 12; i++){ if (fgets(str[i], 16, stdin) == NULL){ return (0); } } for (i = 0; i < 12; i++){ for (j = 0; j < 12; j++){ if (str[i][j] == '1'){ is(i, j); str[i][j] = '1'; ans++; } } } printf("%d\n", ans); if (fgets(str[0], 16, stdin) == NULL) return(0); } }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int MAXN=400; LL n, m, ans, cntW, cntB; bool visited[MAXN][MAXN]; string s[MAXN]; bool avail(int i, int j, char c){ if(i < 0 || i >= n || j < 0 || j >= m) return false; if(s[i][j] == c) return false; return true; } void dfs(int i, int j){ if(i < 0 || i >= n || j < 0 || j >= m) return; if(visited[i][j]) return; visited[i][j] = true; if(s[i][j] == '.') cntW++; else cntB++; if(avail(i, j - 1, s[i][j])) dfs(i, j - 1); if(avail(i, j + 1, s[i][j])) dfs(i, j + 1); if(avail(i + 1, j, s[i][j])) dfs(i + 1, j); if(avail(i - 1, j, s[i][j])) dfs(i - 1, j); } int main(){ cin >> n >> m; for(int i = 0; i < n; i++) cin >> s[i]; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) visited[i][j] = false; ans = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(!visited[i][j]){ cntB = 0; cntW = 0; dfs(i, j); ans += cntB * cntW; } cout << ans << "\n"; }
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int n; cin >> n; vector<string> s(0); vector<int> p(0); rep(i, n) { string si; cin >> si; bool hit = false; rep(j, s.size()) { if (s.at(j) == si) { p.at(j)++; hit = true; break; } } if (!hit) { s.push_back(si); p.push_back(1); } } cin >> n; rep(i, n) { string si; cin >> si; rep(j, s.size()) { if (s.at(j) == si) { p.at(j)--; break; } } } int max = 0; rep(j, s.size()) if (p.at(j) > max) max = p.at(j); cout << max << endl; }
#include <bits/stdc++.h> using namespace std; template <class t> using vc = vector<t>; template <class t> using vvc = vector<vector<t>>; typedef long long ll; typedef vc<int> vi; typedef vvc<int> vvi; typedef pair<int, int> pi; #define uset unordered_set #define umap unordered_map #define endl "\n" #define fi first #define se second #define pb push_back #define mp make_pair #define bg begin() #define ed end() #define all(a) a.bg, a.ed template <class t, class u> ostream &operator<<(ostream &os, const pair<t, u> &p) { return os << "( " << p.first << ", " << p.second << " )"; } template <class t> ostream &operator<<(ostream &os, const vc<t> &v) { os << "[ "; for (int i = 0; i < v.size(); i++) { os << v[i]; if (i != v.size() - 1) { os << ", "; } } return os << " ]"; } int main() { ios::sync_with_stdio(0); cin.tie(0); // cout << fixed << setprecision(18); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // Solution long long n, m; cin >> n; umap<string, long long> points; string temp; for (int i = 1; i <= n; i++) { cin >> temp; points[temp]++; } cin >> m; for (int i = 1; i <= m; i++) { cin >> temp; points[temp]--; } long long best_point = 0; for (auto &p : points) { best_point = max(best_point, p.second); } cout << best_point; }
1
#include <cstdio> #include <algorithm> using i8 = std::int8_t; using u8 = std::uint8_t; using i16 = std::int16_t; using u16 = std::uint16_t; using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; using usize = std::size_t; i32 a[200000]; auto main() -> i32 { i32 n, k; scanf("%d%d", &n, &k); for (i32 i = 0; i < n; i += 1) { scanf("%d", a + i); } i32 b[200001]; for (i32 step = 0; step < std::min(64, k); step += 1) { std::fill(b, b + n + 1, 0); for (i32 i = 0; i < n; i += 1) { b[std::max(0, i - a[i])] += 1; b[std::min(n, i + a[i] + 1)] -= 1; } a[0] = b[0]; for (i32 i = 1; i < n; i += 1) { b[i] += b[i - 1]; a[i] = b[i]; } } for (i32 i = 0; i < n; i += 1) { printf("%d%c", a[i], i == n - 1 ? '\n' : ' '); } return 0; }
#include <iostream> #include <vector> using namespace std; int n, k; bool alln(vector<int>& v){ for(int i=0;i<n;++i) if(v[i]!=n) return false; return true; } void simulate(vector<int>& v){ int cnt = 0; vector<int> tmp(n+1, 0); for(int i=0;i<n;++i) { tmp[max(0, i-v[i])]++; tmp[min(n, i+v[i]+1)]--; } for(int i=0;i<n;++i){ cnt += tmp[i]; v[i] = cnt; } } int main() { scanf("%d %d", &n, &k); vector<int> v(n); for(int i=0;i<n;++i) scanf("%d", &v[i]); while(k--){ simulate(v); if(alln(v)){ for(int i=0;i<n;++i) cout << n << (i==n-1?'\n':' '); return 0; } } for(int i=0;i<n;++i) cout << v[i] << (i==n-1?'\n':' '); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long mod = 1e9+7; int main(){ int N; cin >> N; vector<long long> A(N); for(int i = 0; i < N; i++) cin >> A[i]; long long ans = 0; long long sum = 0; long long sum2 = 0; for(int i = 0; i < N; i++){ sum2 = (sum2 + A[i]*A[i]) % (2*mod); sum = (sum + A[i]) % (2*mod); } ans = (sum*sum - sum2) % (2*mod) / 2; cout << ans << endl; }
#include <iostream> #include <algorithm> #include <cmath> const int lens = 2e5 + 1; const long long mod = 1e9 + 7; long long a[lens],sum[lens]; int main() { int n; std::ios::sync_with_stdio(false); std::cin >> n; for(int i = 1;i <= n;i++) { std::cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } long long ans = 0; for(int i = 1;i <= n;i++) { ans = (ans + (((sum[n] - sum[i]) % mod) * (a[i] % mod)) % mod) % mod; } std::cout << ans; return 0; }
1
#include <iostream> #include <stdlib.h> using namespace std; int main() { int n = 0; int q = 0; cin >> n; int s[n]; for (int i = 0; i < n; i++) { scanf("%d",&s[i]); } cin >> q; int t[q]; for (int i = 0; i < q; i++) { scanf("%d",&t[i]); } int sum = 0; for (int i = 0; i < q; i++) { for (int j = 0; j < n; j++) { if (s[j] == t[i]) { sum++; break; } } } printf("%d\n",sum); return 0; }
#include <bits/stdc++.h> using namespace std; using ll =long long; typedef pair<int,int> P; #define SORT(a) sort((a).begin(),(a).end()) #define REV(a) reverse((a).begin(),(a).end()) #define For(i, a, b) for(int i = (a) ; i < (b) ; ++i) #define rep(i, n) For(i, 0, n) #define debug(x) cerr << #x << " = " << (x) << endl; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } void coY() {cout <<"Yes"<<endl;} void coN(){cout <<"No"<<endl;} //Write From this Line //const ll mod = 1e9+7; //const ll mod = 998244353; int main() { int n; cin >> n; multiset<int> s; rep(i,n){ int x; cin >> x; auto itr = s.lower_bound(x); if(itr == s.begin()){ s.insert(x); continue; } itr--; s.erase(itr); s.insert(x); } cout << s.size() << endl; }
0
//g++ 7.4.0 #include <iostream> #include <string> using namespace std; int main() { int k; cin >> k; std::string s; cin >> s; if(s.length() <= k){ cout << s << endl; }else{ cout << s.substr(0, k) << "..." << endl; } }
#pragma GCC optimize("O3") #include<bits/stdc++.h> #define sp " " #define endl "\n" #define MAX 10e9 #define MAXN 100001 #define mod 1000000007 #define inf_int 2e9 #define inf_ll 2e18 #define e 1e-9 #define vi vector<int> #define vl vector<ll> #define vc vector<char> #define vs vector<string> #define vpl vector<pair<ll,ll>> #define vpc vector<pair<char,char>> #define adj_list vector<vl> #define umap unordered_map<ll, ll> #define pll pair<ll,pair<ll, ll>> #define clr(x) memset(x,0,sizeof(x)) #define cy cout << "YES" << endl #define cn cout << "NO" << endl #define google cout << "Case #" << ++it << ": " << ans << endl; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; typedef long long int ll; ll it = 0, ans = 0; ll spf[MAXN]; void sieve() { spf[1] = 1; for (auto i=2; i<MAXN; i++) spf[i] = i; for (auto i=4; i<MAXN; i+=2) spf[i] = 2; for (auto i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (auto j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } return; } vl getFactorization(ll x) { vl ret; while (x != 1) { ret.push_back(spf[x]); x = x / spf[x]; } return ret; } void check() { ll k; string s; cin >> k >> s; if(s.size()<=k) cout << s; else { // cout << k << endl; for(int i=0;i<k;i++) cout << s[i]; cout << "..."; } return ; } int32_t main() { fastio ll t = 1; // cin >> t; while(t--) check(); return 0; }
1
#define _CRT_SECURE_NO_WARNINGS #include<fstream> #include<iostream> #include<string> #include<iomanip> #include<list> #include<math.h> #include<stack> #include<queue> #include<vector> #include<algorithm> #include<utility> using namespace std; short cost[110][110]; short time_data[110][110]; int main(){ short n, m; while (cin >> n >> m && (n != 0 && m != 0)) { short min_ = 20000; // 答えの最小値 short min_num = 1; // 答えの町 short from, to, time_, cost_; short k; short k_from, k_to, info; for (short i = 1; i < 110; i++) { for (short j = 1; j < 110; j++) { if (i == j) { cost[i][j] = 0; time_data[i][j] = 0; } else { cost[i][j] = 20000; time_data[i][j] = 20000; } } } for (short i = 0; i < n; i++) { cin >> from >> to >> cost_ >> time_; cost[from][to] = cost_; cost[to][from] = cost_; time_data[from][to] = time_; time_data[to][from] = time_; } for (short k = 1; k <= m; k++) { for (short i = 1; i <= m; i++) { for (short j = 1; j <= m; j++) { cost[i][j] = min((int)cost[i][j], cost[i][k] + cost[k][j]); time_data[i][j] = min((int)time_data[i][j], time_data[i][k] + time_data[k][j]); } } } // 答えを求める cin >> k; for (short i = 0; i < k; i++) { cin >> k_from >> k_to >> info; if (info == 0) { cout << cost[k_from][k_to] << endl; } else { cout << time_data[k_from][k_to] << endl; } } } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; long long member[4000]; bool ached[4000]; vector<int> ach; int main(void){ int n; while(true){ cin >> n; if(n == 0) break; fill(member, member + 4000, 0); fill(ached, ached + 4000, false); ach.clear(); for(int i = 0; i < n; i++){ long long id, tanka, suryo; cin >> id >> tanka >> suryo; member[id] += tanka * suryo; if(member[id] >= 1000000 && ached[id] == false){ ach.push_back(id); ached[id] = true; } } if(ach.empty()){ cout << "NA" << endl; }else{ for(int i = 0; i < ach.size(); i++){ cout << ach[i] << endl; } } } return 0; }
0
#include <iostream> #include <set> using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); i++) int main(){ int m,n; while(cin >> n >> m, m + n){ int p[10001][2]; set<int> fri1; set<int> fri2; rep(i,0,m){ int a,b; cin >> a >> b; p[i][0] = a; p[i][1] = b; if(a == 1 || b == 1){ fri1.insert(a); fri1.insert(b); } } if(fri1.size() == 0){ cout << 0 << endl; continue; } rep(i,0,m){ if(fri1.find(p[i][0]) != fri1.end() || fri1.find(p[i][1]) != fri1.end()){ fri2.insert(p[i][0]); fri2.insert(p[i][1]); } } cout << fri2.size()-1 << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n,m,a,b,x=0,count=0; while(1){ cin >> n; if(n == 0) break; cin >> m; int data[n][n]; int str[501]={0}; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ data[i][j]=0; } } for(int i=0;i<m;i++){ cin >> a >> b; data[a-1][b-1]++; data[b-1][a-1]++; if( a == 1 ){ str[x]=b-1; x++; } else if( b == 1){ str[x]=a-1; x++; } } for(int i=0;i<x;i++){ for(int j=1;j<n;j++){ if(data[str[i]][j] != 0 && data[0][j] == 0){ data[0][j]++; } } } for(int i=1;i<n;i++){ if( data[0][i] != 0){ count++; } } cout << count << endl; count=0; x=0; } return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n,m,q;cin>>n>>m>>q; int a[n+1][n+1]{}; for (int i=0;i<m;i++) { int b,c;cin>>b>>c; a[b][c]++; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) a[i][j]+=a[i][j-1]; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) a[i][j]+=a[i-1][j]; for (int i=0;i<q;i++) { int b,c;cin>>b>>c; cout<<a[c][c]-a[c][b-1]-a[b-1][c]+a[b-1][b-1]<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<double> vd; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int,int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; typedef vector<pdd> vdd; #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define mem(a,b) memset(a, b, sizeof(a) ) #define all(x) (x).begin(),(x).end() #define INF 1000000000000 #define MOD 1000000007 #define PB push_back #define MP make_pair #define F first #define S second inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; } inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; } inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInverse(ll a) { return modPow(a, MOD-2); } inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } 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 main(){ int n,m,q;cin>>n>>m>>q; vvi g(n); rep(i,m){ int l,r; cin>>l>>r; l--;r--; g[l].PB(r); } rep(i,n){ sort(all(g[i])); } rep(i,q){ int P,Q; cin>>P>>Q; P--;Q--; int cnt=0; for(int j=P;j<=Q;++j){ cnt+=upper_bound(all(g[j]),Q)-g[j].begin(); } cout<<cnt<<endl; } }
1
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> #include <functional> #include <ctime> #include <cmath> #include <limits> #include <numeric> #include <type_traits> using namespace std; using ll = long long; unsigned euclidean_gcd(unsigned a, unsigned b) { if (a < b) return euclidean_gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } class UnionFind { public: vector <ll> par; vector <ll> siz; UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (ll i = 0; i < sz_; ++i) par[i] = i; } ll root(ll x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int main() { int a,b,c; cin >> a >> b >> c; cout << max((a - b + 1),0) * max((a - c + 1),0) << endl; }
#include <bits/stdc++.h> const int MOD = 1e9 + 7, N = 40, L = 17; int n, x, y, z, f[ N + 1 ][ 1 << L ]; int main( ) { std::ios::sync_with_stdio( false ); std::cin >> n >> x >> y >> z; int mx = ( 1 << ( x + y + z ) ) - 1, ans = 1, haiku = ( ( 1 << ( x + y + z ) ) >> 1 ) | ( ( 1 << ( y + z ) ) >> 1 ) | ( ( 1 << z ) >> 1 ); f[ 0 ][ 0 ] = 1; for( int i = 0; i < n; ++i, ans = ans * 10LL % MOD ) for( int s = 0; s <= mx; ++s ) { if( !f[ i ][ s ] ) continue; for( int j = 1; j <= 10; ++j ) { int trs = ( s << j | ( 1 << j >> 1 ) ) & mx; if( ( trs & haiku ) ^ haiku ) f[ i + 1 ][ trs ] = ( f[ i + 1 ][ trs ] + f[ i ][ s ] ) % MOD; } } int sub = 0; for( int i = 0; i <= mx; ++i ) sub = ( sub + f[ n ][ i ] ) % MOD; std::cout << ( ans - sub + MOD ) % MOD; return 0; }
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); i++) #define all(v) begin(v),end(v) using ll = long long; int main() { int n,m;cin>>n>>m; vector<pair<int,int>> pr(m); rep(i,m){ int s,c;cin>>s>>c; s--; pr[i]=make_pair(s,c); } rep(i,1000){ bool ans=true; string a=to_string(i); int keta=a.size(); if(n!=keta)continue; rep(j,m){ if(a[pr[j].first]!=(char)('0'+pr[j].second))ans=false; } if(ans){ cout<<i<<endl; return 0; } } cout<<-1<<endl; }
#include <cstdio> #include <iostream> #include <array> #include <algorithm> using namespace std; int main() { array<array<int,10>,2> ar; for(int i = 0; 2 > i; i++){ for(int j = 0; 10 > j; j++){ scanf("%d\n", &ar[i][j]); } sort(ar[i].begin(), ar[i].end(), [](int a, int b){return a > b;}); } printf("%d %d\n", accumulate(ar[0].begin(), ar[0].begin()+3, 0), accumulate(ar[1].begin(), ar[1].begin()+3, 0)); return 0; }
0
#include<bits/stdc++.h> using namespace std; using p=pair<int,int>; #define int long long #define rep(i,N) for(int i=0;i<N;i++) signed main(){ int N; cin>>N; int ans=1; while(ans*ans<=N)ans+=1; ans-=1; cout<<ans*ans<<endl; return 0; }
#include <iostream> int main(){ int n; std::cin >> n; std::cout << n * n * n << std::endl; return 0; }
0
#include <stdio.h> #include <string.h> int main() { int a, b, c = 0; char pscode[50]; scanf("%d %d", &a, &b); scanf("%s", &pscode); for (int i = 0; i < strlen(pscode); i++) { if (pscode[a] != '-') { printf("No"); return 0; } if (pscode[i] >= '0' && pscode[i] <= '9') { c++; } } if (c != (a+b)) { printf("No"); return 0; } printf("Yes"); }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <stdio.h> #include <queue> #include <climits> #include <map> #include <set> const int mod = 1e9 + 7; const int inf = 1 << 20; const long long INF = 1LL << 60; using namespace std; typedef long long ll; typedef pair<int, int> P; using namespace std; int main() { int n,h,w; cin >> n >> h >> w; cout << (n-h+1)*(n-w+1); }
0
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> #define gc getchar() #define N 103 #define sint short #define upd(x,y) (x=max(x,(short)(y))) #define debug(x) cerr<<#x<<"="<<x #define sp <<" " #define ln <<endl using namespace std; inline int inn() { int x,ch;while((ch=gc)<'0'||ch>'9'); x=ch^'0';while((ch=gc)>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^'0');return x; } sint s[N][N],dp[N][N][N][N];char str[N][N]; inline sint S(int a,int b,int c,int d) { if(a>c) swap(a,c);if(b>d) swap(b,d); return (short)(s[c][d]+s[a-1][b-1]-s[a-1][d]-s[c][b-1]); } int main() { int n=inn(),m=inn(),x=0,y=0;sint ans=0; for(int i=1;i<=n;i++) scanf("%s",str[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(str[i][j]=='E') x=i,y=j; else s[i][j]=(str[i][j]=='o'); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) s[i][j]=(short)(s[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1]); for(int a=x;a;a--) for(int b=y;b;b--) for(int c=x;c<=n;c++) for(int d=y;d<=m;d++) { ans=max(ans,dp[a][b][c][d]);int L=d-y+1,R=m-y+b,U=c-x+1,D=n-x+a; if(a>U) upd(dp[a-1][b][c][d],dp[a][b][c][d]+S(a-1,max(b,L),a-1,min(d,R))); if(b>L) upd(dp[a][b-1][c][d],dp[a][b][c][d]+S(max(a,U),b-1,min(c,D),b-1)); if(c<D) upd(dp[a][b][c+1][d],dp[a][b][c][d]+S(c+1,max(b,L),c+1,min(d,R))); if(d<R) upd(dp[a][b][c][d+1],dp[a][b][c][d]+S(max(a,U),d+1,min(c,D),d+1)); } return !printf("%d\n",(int)ans); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (int i = a; i < b; i++) int n, k; ll ans = 10000000000, len; int main() { cin >> n >> k; vector<ll> x(n); rep(i, 0, n) { cin >> x[i]; } ll l, r, lr, rl; for (int i = 0; i <= (n - k); i++) { l = i; r = l + k - 1; rl = abs(x[r]) + abs(x[r] - x[l]); lr = abs(x[l]) + abs(x[r] - x[l]); len = min(lr, rl); // cout << ans << "," << len << endl; ans = min(ans, len); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; #define LL long long #define DD long double #define M 1000000007 #define INF 1e18 void setIO(string s = "") { cin.sync_with_stdio(0); cin.tie(0); if (s.size()) s += ".in", freopen(s.c_str(), "r", stdin); } int tt = 1; void solve() { set<int> nums; int a; while (cin >> a) nums.insert(a); cout << ((nums.size() == 2) ? "Yes" : "No") << endl; } int main() { setIO(); while (tt--) { solve(); } return 0; }
#include"bits/stdc++.h" #include<iostream> using namespace std; typedef long long ll; typedef vector<int> vi; #define rep(i, c) for(int i = 0; i < (int)c; i++) const ll inf = 1000000007; int main() { int a, b,c; cin >> a >> b >> c; if (a != b) { if (a==c || b==c) { cout << "Yes" << endl; return 0; } } else { if (a != c) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
1
#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,Q; cin >> N >> Q; vector<int> S(N),T(N),X(N); vector<int> D(Q); rep(i,N) cin >> S.at(i) >> T.at(i) >> X.at(i); rep(i,Q) cin >> D.at(i); set<P> s; vector<tuple<int,int,int>> a(N); vector<int> ans(Q,-1); rep(i,Q) s.insert(P(D.at(i),i)); rep(i,N) a.at(i) = tie(X.at(i),S.at(i),T.at(i)); sort(a.begin(),a.end());//位置でソート rep(i,N){//工事止めに対して int x,l,r;tie(x,l,r)=a.at(i); auto it=s.lower_bound(P(l-x,-1)); while(it!=s.end()){ if(it->first + x >= r) break; else{ ans.at(it->second)=x; s.erase(it++); } } /*auto it=s.lower_bound(P(l,-1));//l以降のものを探す while(it!=s.end()){ if(it->first>=r) break;//工事の影響がない ans.at(it->second)=x;//工事の影響でストップ s.erase(it++);//要素を消し,itをインクリメント }*/ } rep(i,Q) cout << ans.at(i) << endl; return 0; }
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<deque> using namespace std; const int maxn = 1000000 + 10; int n, ans = 0; char a[maxn], b[maxn]; deque <int> q; int main() { scanf("%d", &n); scanf("%s%s", a + 1, b + 1); q.push_back(n + 1); for(int i = n, j = n + 1, del = 0;i >= 1;i --) { int tmp = j; while(j && (j > i || a[j] != b[i])) j --; if(!j) { puts("-1"); return 0; } if(tmp == j) { while(!q.empty() && q.back() - del >= i) q.pop_back(); q.push_back(i + del); } else { del ++; if(i != j) { ans = max(ans,(int)q.size()); q.push_front(j + del); } } } printf("%d\n", ans); return 0; }
0
#include <iostream> #include <cstdio> using namespace std; int main(){ int n, ans, out; string str; bool base[3]; cin >> n; for(int loop = 0; loop < n; loop++){ base[0] = false; base[1] = false; base[2] = false; ans = 0; out = 0; while(true){ cin >> str; if(str == "HIT"){ if(base[2]){ ans++; } if(base[1]){ base[2] = true; }else{ base[2] = false; } if(base[0]){ base[1] = true; }else{ base[1] = false; } base[0] = true; }else if(str == "HOMERUN"){ ans++; if(base[2]){ ans++; } if(base[1]){ ans++; } if(base[0]){ ans++; } base[0] = false; base[1] = false; base[2] = false; }else if(str == "OUT"){ out++; } if(out >= 3){ break; } } printf("%d\n", ans); } return 0; }
#include <iostream> using namespace std; int type(string s){ if(s == "HIT")return 0; if(s == "OUT")return 1; if(s == "HOMERUN")return 2; } int main(){ int n; string s; cin >> n; while(n--){ int outCount = 0; int base = 0; int result = 0; while(outCount < 3){ cin >> s; switch(type(s)){ case 0: base <<= 1; base++; break; case 1: outCount++; break; case 2: result += __builtin_popcount(base)+1; base = 0; break; } if(base & 8)result++; base &= 7; // 0b0111 } cout << result << endl; } }
1
#include <bits/stdc++.h> using namespace std; #define TEMP_T template<typename T> TEMP_T void sort(T& v){ sort(v.begin(), v.end()); } TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); } TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); } TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; } TEMP_T T cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; } TEMP_T void maxe(T& v, T m){ v = max(v, m); } TEMP_T void mine(T& v, T m){ v = min(v, m); } static inline int in(){ int x; scanf("%d", &x); return x; } int main() { int n, m; while (n = in(), m = in()){ vector<int> p; for (int i = 0; i < n; i++){ p.push_back(in()); } p.push_back(0); n++; vector<int> pp; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ pp.push_back(p[i] + p[j]); } } int maxi = 0; sort(pp); for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ int s = p[i] + p[j]; int v = *(upper_bound(pp.begin(), pp.end(), m - s) - 1); if (s + v <= m){ maxe(maxi, s + v); } } } printf("%d\n", maxi); } return 0; }
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <vector> #include <map> #include <utility> #include <stack> #include <queue> #include <set> #include <list> #include <bitset> #include <array> using namespace std; #define fi first #define se second #define long long long typedef pair<int,int> ii; vector<long> vec; bool prime(int x) { for(int i = 2; i <= sqrt(x); i++) { if(x%i == 0) return 0; } return 1; } long MAXN = 500; long A[503][503]; long gcd(long a, long b) {return (b)? gcd(b,a%b) : a;} long lcm(long a, long b) { if(a == b && b == 0) return 0; else if(a == 0) return b; else if(b == 0) return a; else return a/gcd(a,b)*b; } int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.in", "r", stdin); for(int i = 2; i <= 10000; i++) if(prime(i)) vec.push_back(i); int n; scanf("%d", &n); int idx = 0; for(int i = 1; i <= MAXN; i+=2) { int x = 1, y = i; while(y <= MAXN) A[x++][y++] = vec[idx]; idx++; } for(int i = 3; i <= MAXN; i+=2) { int x = i, y = 1; while(x <= MAXN) A[x++][y++] = vec[idx]; idx++; } for(int i = 1; i <= MAXN; i+=2) { int x = 1, y = i; while(y >= 1 && x <= MAXN) A[x++][y--] *= vec[idx]; idx++; } for(int i = MAXN; i >= 2; i-=2) { int x = i, y = MAXN; while(y >= 1 && x <= MAXN) A[x++][y--] *= vec[idx]; idx++; } long mx = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(A[i][j] == 0) A[i][j] = lcm(lcm(A[i-1][j],A[i][j-1]),lcm(A[i+1][j],A[i][j+1]))+1; // mxnax(A[i][j],mx); printf("%lld", A[i][j]); if(j < n) printf(" "); } printf("\n"); } // printf("%lld\n", mx); }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; g[a].emplace_back(b); } for (int t = 0; t < n; t++) { queue<int> que; vector<bool> was(n); vector<int> depth(n); vector<set<int>> p(n); vector<int> q(n); que.push(t); was[t] = true; while (!que.empty()) { for (int i = que.size(); i > 0; i--) { int v = que.front(); que.pop(); set<int> st; for (int to : g[v]) { if (was[to]) { st.emplace(to); continue; } was[to] = true; que.push(to); depth[to] = depth[v] + 1; p[to] = p[v]; q[to] = v; p[to].emplace(v); } if (!st.empty()) { int mx = -1, id = -1; for (int to : st) { if (p[v].count(to)) { if (mx < depth[to]) { id = to; mx = depth[to]; } } } if (id != -1) { cout << depth[v] - mx + 1 << '\n'; cout << v + 1 << '\n'; while (id != v) { v = q[v]; cout << v + 1 << '\n'; } return 0; } } } } } cout << -1 << '\n'; return 0; }
#include<bits/stdc++.h> #include<string.h> typedef long long int ll; #define all(x) (x).begin(), (x).end() using namespace std; int nxt() { int x; cin >> x; return x; } ll nxtl(){ ll x; cin>>x; return x; } void SieveOfEratosthenes(int n,vector <int> &primes) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. bool prime[n+1]; memset(prime, true, sizeof(prime)); for (int p=2; p*p<=n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i=p*p; i<=n; i += p) prime[i] = false; } } // Print all prime numbers for (int p=2; p<=n; p++) if (prime[p]) primes.push_back(p); } ll max(ll a,ll b) { if(a>b) return a; return b; } ll power(ll x, ll y,ll mod) { ll temp; if( y == 0) return 1; temp = power(x, y/2,mod); if (y%2 == 0) return (temp*temp)%mod; else return (((x*temp)%mod)*temp)%mod; } vector <vector <int> > g; void dfs(int s,vector <int> &p,int &parent,int &child,set <int> &s2,vector <bool> &visited,vector <int> &depth,int &min,vector <int> &path); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n=nxt(),m=nxt(); vector <int> row; g.resize(n+1,row); set <pair<int,int> > edges; for(int i=0;i<m;i++) {//cout<<"came3\n"; int u=nxt(),v=nxt(); g[u].push_back(v); edges.insert({u,v}); } vector <int> p(n+1); int parent=0,child=0,min=INT_MAX; vector <int> path(n+1); for(int i=1;i<=n;i++) {//cout<<"came"<<endl; vector <bool> visited; visited.resize(n+1,0); set <int> s2; vector <int> depth(n+1); depth[i]=0; dfs(i,p,parent,child,s2,visited,depth,min,path); } if(parent!=0) { vector <int> test; int cur=child; test.push_back(cur); //cout<<cur<<" "<<parent<<endl; //return 0; while(cur!=parent) {//cout<<cur<<endl; cur=path[cur]; test.push_back(cur); } cout<<test.size()<<endl; for(auto val:test) cout<<val<<endl; return 0; } cout<<"-1\n"; return 0; } void dfs(int s,vector <int> &p,int &parent,int &child,set <int> &s2,vector <bool> &visited,vector <int> &depth,int &min,vector <int> &path) { visited[s]=1; int x=s; s2.insert(x); for(int i=0;i<g[x].size();i++) { if(!visited[g[x][i]]) { p[g[x][i]]=x; depth[g[x][i]]=depth[x]+1; dfs(g[x][i],p,parent,child,s2,visited,depth,min,path); } else{ if(s2.count(g[x][i])&&(depth[x]-depth[g[x][i]])<min) { child=x; parent=g[x][i]; min=depth[x]-depth[g[x][i]]; path=p; } } } s2.erase(x); }
1
#include<bits/stdc++.h> using namespace std; main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long i,j,k,l,m,n,o,p,q; while(cin>>m>>n>>o>>p) { if(m+n>o+p) cout<<"Left"<<endl; else if(m+n<o+p) cout<<"Right"<<endl; else cout<<"Balanced"<<endl; } return 0; }
#include <bits/stdc++.h> #include <iostream> #include<math.h> using namespace std; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int A, B, C, D; cin >> A >> B >> C >> D; int left = A + B; int right = C + D; if (left > right) { cout << "Left" << endl; } else if (left < right) { cout << "Right" << endl; } else { cout << "Balanced" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define LL long long #define LD long double #define SC(t,x) static_cast<t>(x) #define AR(t) vector < t > #define PII pair < int, int > #define PLL pair < LL, LL > #define PIL pair < int, LL > #define PLI pair < LL, int > #define MP make_pair #define PB push_back #define PF push_front #define POB pop_back #define POF pop_front #define PRF first #define PRS second #define INIT(ar,val) memset ( ar, val, sizeof ( ar ) ) #define lp(loop,start,end) for ( int loop = start; loop < end; ++loop ) #define lpd(loop,start,end) for ( int loop = start; loop > end; --loop ) #define lpi(loop,start,end) for ( int loop = start; loop <= end; ++loop ) #define lpdi(loop,start,end) for ( int loop = start; loop >= end; --loop ) #define qmax(a,b) (((a)>(b))?(a):(b)) #define qmin(a,b) (((a)<(b))?(a):(b)) #define qabs(a) (((a)>=0)?(a):(-(a))) const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 100007; int n; int a[MAXN]; void init (); void input (); void work (); int getcur () { int cc = 0; lpi ( i, 1, n ) cc ^= ( a[i] - 1 ) & 1; return cc; } int gcd ( int x, int y ) { return y ? gcd ( y, x%y ) : x; } int main() { init(); input(); work(); } void init () { // Init Everything Here ios::sync_with_stdio ( false ); } void input () { // input method scanf ( "%d", &n ); lpi ( i, 1, n ) scanf ( "%d", &a[i] ); } void work () { // main work if ( n == 1 ){ cout << "Second" << endl; return; }else if ( n == 2 ){ cout << ( ( ( a[1] ^ a[2] ) & 1 ) ? "First" : "Second" ) << endl; return; } if ( getcur () ) cout << "First" << endl; else{ int t = 0, cc, g; while ( 1 ){ cc = 0; if ( getcur () ){ cout << ( t ? "Second" : "First" ) << endl; break; } lpi ( i, 1, n ) cc += a[i] & 1; if ( cc > 1 || ( *min_element ( a+1, a+1+n ) == 1 ) ){ cout << ( t ? "First" : "Second" ) << endl; break; } lpi ( i, 1, n ) if ( a[i] & 1 ) --a[i]; g = a[1]; lpi ( i, 2, n ) g = gcd ( g, a[i] ); lpi ( i, 1, n ) a[i] /= g; t ^= 1; } } }
#include <bits/stdc++.h> using namespace std; const int N = 100010; int a[N], n, cnt[2]; bool gao() { for (int i = 1; i <= n; i++) if (a[i] & 1) a[i]--; cnt[0] = cnt[1] = 0; int gcd = a[1]; for (int i = 2; i <= n; i++) gcd = __gcd(a[i], gcd); for (int i = 1; i <= n; i++) cnt[(a[i] /= gcd) & 1]++; if (cnt[0] & 1) return 1; else if (cnt[1] > 1) return 0; else { for (int i = 1; i <= n; i++) if (a[i] == 1) return cnt[0] & 1 ? 1 : 0; return gao() ^ 1; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i), cnt[a[i] & 1]++; if (cnt[0] & 1) puts("First"); else if (cnt[1] > 1) puts("Second"); else { for (int i = 1; i <= n; i++) if (a[i] == 1) { puts(cnt[0] & 1 ? "First" : "Second"); return 0; } puts(gao() ^ 1 ? "First" : "Second"); } return 0; }
1