code_file1
stringlengths
80
4k
code_file2
stringlengths
91
4k
similar_or_different
int64
0
1
#include <iostream> #include <string> #include <algorithm> using namespace std; int main(){ string str,fc,p; int q,i=0,a,b; cin>>str>>q; for(;i<q;i++){ cin>>fc>>a>>b; switch(fc[2]){ case 'i': cout<<str.substr(a,b-a+1)<<endl; break; case 'v': p=str.substr(a,b-a+1); reverse(p.begin(),p.end()); str.replace(a,b-a+1,p); break; case 'p': cin>>p; str.replace(a,b-a+1,p); break; } } }
#include <stdio.h> int main() { int n,f,e,j,k,i; char m[1001],my[1001],c[10],mc[1001]; scanf("%s",m); scanf("%d",&n); for(i=0;i<n;i++){ scanf("%s %d %d",c,&f,&e); if(c[0]=='p'){ for(k=f;k<=e;k++){ printf("%c",m[k]); } printf("\n"); }else if(c[3]=='e'){ j=e; for(k=f;k<=e;k++){ my[j]=m[k]; j--; } for(k=f;k<=e;k++){ m[k]=my[k]; } }else if(c[3]=='l'){ scanf("%s",mc); j=0; for(k=f;k<=e;k++){ m[k]=mc[j]; j++; } } } return 0; }
1
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <complex> #include <cstdlib> #include <cstring> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <functional> #define mp make_pair #define pb push_back #define all(x) (x).begin(),(x).end() #define rep(i,n) for(int i=0;i<(n);i++) #define repi(i,a,b) for(int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int,int> pii; const int INF=1<<29; const double EPS=1e-9; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; int vote[1010]; int cost[1010]; int main(){ int n,m; cin>>n>>m; memset(vote, 0, sizeof(vote)); memset(cost, 0, sizeof(cost)); rep(i, n){ cin>>cost[i]; } rep(i, m){ int x; cin>>x; rep(j, n){ if(cost[j]<=x){ vote[j]++; break; } } } int cnt = 0; int ans = 0; rep(i, n){ if(vote[i]>=cnt){ cnt =vote[i]; ans=i; } } cout <<ans+1<<endl; return 0; }
#include <bits/stdc++.h> const int MAXN = 1010; typedef long long LL; int pri[MAXN], bak, n; int ispri(int x) { for (int i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } LL lcm(LL a, LL b) { return a / gcd(a, b) * b; } LL get(int x, int y) { if (x < 1 || y < 1 || x > n || y > n) return 1; if ((x & 1) == (y & 1)) return pri[x + y >> 1] * pri[x + n * 3 - y + 1 >> 1]; return lcm(lcm(get(x - 1, y), get(x, y - 1)), lcm(get(x + 1, y), get(x, y + 1))) + 1; } int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n; n += 2; int now = 2; while (bak < n * 2) { if (ispri(now)) pri[++bak] = now; ++now; } for (int i = 2; i < n; ++i, std::cout << '\n') for (int j = 2; j < n; ++j) std::cout << get(i, j) << ' '; return 0; }
0
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <utility> #include <stack> #include <functional> #include <queue> #include <cmath> #include <set> #define rep(i,j,k) for(int i=j;i<k;i++) #define drep(n,m) for(int i=0;i<n;i++)for(int j=0;j<m;j++) #define Sort(x) sort((x).begin(),(x).end()) #define fi first #define se second #define vi vector<int> #define INF 1000000000 #define MOD 1000000009 #define pb push_back #define mp make_pair typedef long long ll; typedef unsigned long long ull; int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; using namespace std; int main() { int n; cin>>n; rep(i,0,n){ int out=0; int score=0; int runner=0; string s; while(out!=3){ cin>>s; if(s=="HIT"){ if(runner==3)score++; else runner++; }else if(s=="OUT"){ out++; }else{ score+=runner+1; runner=0; } } cout<<score<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a) for(int i=0; i<(int)(a); i++) typedef long long ll; template<typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template<typename T> using Edges = vector<edge<T>>; template<typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template<typename T> using Matrix = vector<vector<T>>; ///////////////////////////////////////////////////////////////////// const ll inf = 1LL<<60; int main() { string x; cin>>x; int s=0; int t=0; for (auto c:x) { if (c == 'S') { s++; } else { if (s > 0) s--; else t++; } } cout << t+s << endl; return 0; }
0
#include <iostream> #include <cstdlib> #include <string> #include <algorithm> int main(void) { std::string str; int a; int b; int q; std::string start = ""; std::string sub = ""; std::string end = ""; std::string s = ""; std::cin >> str >> q; for (int i = 0; i < q; i++) { std::string order; std::cin >> order; std::cin >> a >> b; start = str.substr(0, a); sub = str.substr(a, b - a + 1); end = str.substr(b + 1, str.size() - b); if (order == "replace") { s = ""; std::cin >> s; str = start + s + end; } else if (order == "reverse") { std::reverse(sub.begin(), sub.end()); str = start + sub + end; } else if (order == "print") { std::cout << sub << std::endl; } else { } } return EXIT_SUCCESS; }
#include <iostream> int li, ri, n, m, a, b; int main() { a = 0; b = 1000001; std::cin >> n >> m; for (int i = 0; i < m; ++i) { std::cin >> li >> ri; if (li > a) a = li; if (b > ri) b = ri; } int ans = a > b ? 0 : b - a + 1; std::cout << ans; return 0; }
0
#include <iostream> #include <limits.h> using namespace std; int dp[100][100]; int p[101]; int n; void solve() { for (int m = n; m > 0; m--) { for (int t = 0, i = 0, j = n - m + 1; t < n && j < n; t++) { dp[i][j] = INT_MAX; for (int k = i; k < j; k++) { int temp = dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]; if (temp < dp[i][j]) { dp[i][j] = temp; } } i++; j++; } } } int main() { int a, b; cin >> n >> a; p[0] = a; for (int i = 1; i < n; i++) { cin >> a >> b; p[i] = a; } cin >> a; p[n] = a; solve(); cout << dp[0][n - 1] << endl; return 0; }
#include <bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; int dp[111][111],n,L[111],R[111]; int f(int l,int r){ if(dp[l][r]>-1)return dp[l][r]; int res=1e9; for(int i=l;i<r;i++){ res=min(res,f(l,i)+f(i+1,r)+L[l]*R[i]*R[r]); } return dp[l][r]=res; } main(){ memset(dp,-1,sizeof(dp)); r(i,111)dp[i][i]=0; cin>>n; r(i,n)cin>>L[i]>>R[i]; cout<<f(0,n-1)<<endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int ans = n - 1, num = 0; for (int i = 1; i < n; i++) { if (s.at(i) == 'E') num++; } //cout << num << endl; ans = min(ans, num); for (int i = 0; i < n - 1; i++) { if (s.at(i) == 'W') num++; if (s.at(i + 1) == 'E') num--; ans = min(ans, num); //cout << num << endl; } cout << ans << endl; }
#include <iostream> #include <string> #include <unordered_map> #include <unordered_set> #include <cstring> #include <chrono> #include <vector> #include <map> #include <random> #include <set> #include <algorithm> #include <math.h> #include <cstdio> #include <stdio.h> #include <queue> #include <bitset> #include <cstdlib> #include <deque> #include <cassert> #include <stack> using namespace std; #define mp make_pair #define f first #define se second #define pb push_back #define ppb pop_back #define emb emplace_back #define ll long long #define ull unsigned long long #define cntbit(x) __builtin_popcount(x) #define endl '\n' #define uset unordered_set #define umap unordered_map #define pii pair<int, int> #define ld long double #define pll pair<long long, long long> mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> inline T range(T l, T r) { return uniform_int_distribution<T>(l, r)(rng); } inline void setin(string s) { freopen(s.c_str(), "r", stdin); } inline void setout(string s) { freopen(s.c_str(), "w", stdout); } template <typename T> void Min(T &a, T b) { a = min(a, b); } template <typename T> void Max(T &a, T b) { a = max(a, b); } const int inf = 0x3f3f3f3f; const int mod = 998244353; const int N = 3e2 + 15; int n, k; string s; int dp[2][N][N]; // dp[i][operation_applied][free_ones] int a[N], p[N][N]; inline int add(int a, int b) { a += b; if(a >= mod) a -= mod; if(a < 0) a += mod; return a; } inline void add_t(int &a, int b) { a = add(a, b); } inline void construct() { int sz = 1; for(int i = 1; i <= n && s[i] == '1'; ++i) a[1] = i; for(int i = a[1] + 1; i <= n; ++i) { if(s[i] == '0') { if(i + 1 <= n && s[i + 1] == '0') a[++sz] = 0; } else { int j = i; while(j + 1 <= n && s[j + 1] == '1') ++j; a[++sz] = j - i + 1; i = j; } } n = sz; } inline void solve() { // dp[i][operation_applied][free_ones] dp[(n + 1) & 1][0][0] = 1; int sum = 0; for(int i = n; i >= 1; --i) { int bit = i & 1; memset(dp[bit], 0, sizeof(dp[bit])); for(int k = 0; k < N; ++k) for(int t = 0; t < N; ++t) p[k][t] = add(k && t ? p[k-1][t-1] : 0, dp[bit ^ 1][k][t]); for(int k = 0; k < N; ++k) { int val = 0; for(int t = N - 1; t >= 0; --t) { add_t(dp[bit][k][t], val); if(k - a[i] - 1 >= 0 && t - a[i] - 1 >= 0) add_t(dp[bit][k][t], add(p[k][t], -p[k - a[i] - 1][t - a[i] - 1])); else add_t(dp[bit][k][t], p[k][t]); add_t(val, dp[bit ^ 1][k][t]); } } sum += a[i]; } } inline void output() { int ans = 0; for(int i = 0; i < min(N, k + 1); ++i) add_t(ans, dp[1][i][0]); cout << ans << endl; } main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // setin("input.txt"); cin >> s >> k; n = s.size(); s = '#' + s; construct(); solve(); output(); return 0; }
0
#include<bits/stdc++.h> #define MOD 1000000007 #define mp make_pair #define ll long long #define pb push_back #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define debug cout<<"Debugging.."<<endl using namespace std; int main() { faster; ll int n; cin>>n; int a=pow(n,2); int b=pow(n,3); cout<<a+b+n; }
#include"bits/stdc++.h" #include<cstring> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) typedef long long ll; int main(){ int a; int ans; cin>>a; ans=a+a*a+a*a*a; cout<<ans<<endl; return 0; }
1
#pragma GCC optimize(3 , "Ofast" , "inline") #include <bits/stdc++.h> #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--) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int , int> pi; template<class T> inline void read (T &x) { x = 0; int sign = 1; char c = getchar (); while (c < '0' || c > '9') { if ( c == '-' ) sign = - 1; c = getchar (); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar (); } x = x * sign; } const int maxn = 2e5 + 10; const int inf = int (1e9); const ll INF = ll (1e18); const double PI = acos (- 1); const int mod = 1e9+7; const double eps = 1e-8; ll dp[111][10]; int main () { int h,w,m; read (h);read (w);read (m); if(w==1) { puts ("1"); return 0; } dp[0][0]=1; rep (i,1,h) { rep (j,0,(1<<w-1)-1) { bool ok=1; rep (k,0,w-3) { if((j&(1<<k))&&(j&(1<<k+1))) { ok=0; break; } } if(!ok) continue; rep (k,0,w-1) { if(k>=1&&(j&(1<<(k-1)))) { dp[i][k]=(dp[i][k]+dp[i-1][k-1])%mod; } else if(k<w-1&&(j&(1<<k))) { dp[i][k]=(dp[i][k]+dp[i-1][k+1])%mod; } else dp[i][k]=(dp[i][k]+dp[i-1][k])%mod; } } } cout<<dp[h][m-1]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i=0; i < (int)(n); i++) using ull = unsigned long long; using ll = long long; using pii = pair<int, int>; ll MOD = 1E+9 + 7; int main() { ll H, W, K; cin >> H >> W >> K; ll A[H+1][W+1] = {}; A[0][1] = 1; ll c[8] = {1, 1, 2, 3, 5, 8, 13, 21}; for (ll h=0; h<H; h++) { for (ll w=1; w<=W; w++) { if (w>=2) A[h+1][w-1] = (A[h+1][w-1] + A[h][w] * c[w-2] * c[W-w]) % MOD; A[h+1][w] = (A[h+1][w] + A[h][w] * c[w-1] * c[W-w]) % MOD; if (w<W) A[h+1][w+1] = (A[h+1][w+1] + A[h][w] * c[w-1] * c[W-(w+1)]) % MOD; } } cout << A[H][K] << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n)-1;i>=0;i--) #define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--) #define all(a) (a).begin(),(a).end() typedef long long ll; typedef pair<int, int> P; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, p; while (cin >> n >> p, n) { vector<int> v(n, 0); int pot = p; rep(r, 1000000) { int i = r % n; if (pot > 0) { pot--; v[i]++; } else { pot += v[i]; v[i] = 0; } if (v[i] == p) { cout << i << endl; break; } } } }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <cmath> #include <stack> #include <queue> #include <functional> #include <set> #include <map> #include <tuple> #include <bitset> #include <random> int money[300]; int main(){ int x, y; std::cin >> x >> y; int ans = 0; money[1]=300000; money[2]=200000; money[3]=100000; ans += money[x]; ans += money[y]; if (x==y && x==1){ ans += 400000; } std::cout << ans << std::endl; return 0; }
0
// #pragma GCC optimize(3,"Ofast","inline") #include <bits/stdc++.h> #include <iostream> #include <fstream> using namespace std; typedef long long ll; typedef unsigned long long ull; const int mod = 1e9 + 7; const ll infll = 1e18 + 9; const int infint = 1e9 + 7; const int MAXN = 1e5 + 7; const double eps = 1e-9; const long double pi = 3.1415926535897932384626433832795; const long double e = 2.71828182845904523536028747135266; inline ll read() { ll kr = 1, xs = 0; char ls; ls = getchar(); while (!isdigit(ls)) { if (!(ls ^ 45)) kr = -1; ls = getchar(); } while (isdigit(ls)) { xs = (xs << 1) + (xs << 3) + (ls ^ 48); ls = getchar(); } return xs * kr; } // inline __int128 read128() // { // __int128 kr=1,xs=0;char ls;ls=getchar(); // while(!isdigit(ls)){ // if(!(ls^45)) kr=-1;ls=getchar(); // } // while(isdigit(ls)){ // xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar(); // } // return xs*kr; // } inline ull readull() { ull xs = 0; char ls; ls = getchar(); while (!isdigit(ls)) ls = getchar(); while (isdigit(ls)) { xs = (xs << 1) + (xs << 3) + (ls ^ 48); ls = getchar(); } return xs; } inline void writeull(ull x) { if (x < 0) { x *= -1; putchar('-'); } if (x >= 10) writeull(x / 10); putchar('0' + x % 10); } inline void write(ll x) { if (x < 0) { x *= -1; putchar('-'); } if (x >= 10) write(x / 10); putchar('0' + x % 10); } // inline void write(__int128 x){ // if(x<0){x*=-1;putchar('-');} // if(x>=10) write(x/10); // putchar('0'+x%10); // } /////////////////////////////////////////////////////////////////////////////////////// int a[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // t = read(); while (t--) { int n=read(),x=read(),T=read(); n=n/x+(n%x?1:0); n*=T; printf("%d\n",n); } return 0; }
#include <iostream> #include<cmath> using namespace std; int main() { int N,X,T; cin>>N>>X>>T; int k=ceil((double)N/X); int val=k*T; cout<<val<<endl; return 0; }
1
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int main() { string N, ans = "No"; cin >> N; for(char s : N) { if (s == '7') ans = "Yes"; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main() { string s; cin >> s; if(s.at(0)=='7' ||s.at(1)=='7' || s.at(2)=='7') cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a,b; cin >> a >> b; int ans =a*b; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long int #define mod 1000000007 bool isPrime[1000001]; ll power(ll a,ll b) { ll result = 1; while(b>0) { if(b%2) { result = (a*result)%mod; } a = (a*a)%mod; b = b/2; } return result; } int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } void sieve() { memset(isPrime,1,sizeof(isPrime)); isPrime[0] = isPrime[1] = 0; for(int i=2;i*i<=1000000;i++) { if(isPrime[i]) { for(int j=i*i;j<=1000000;j+=i) { isPrime[j] = 0; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL),cout.tie(NULL); int t; t = 1; for(int i=0;i<t;i++) { int a,b; cin>>a>>b; cout<<a*b; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a,b; int n=1; int cnt=0; cin >> a >> b; if(n<b) { n=a, cnt++; } while(n<b) { n+=a-1; cnt++; } cout << cnt << endl; }
#include<iostream> #include<algorithm> using namespace std; int main() { int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; cout << a + b + c + d - min(min(min(a, b), c), d) + max(e, f) << endl; cin >> c; }
0
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 1e5 + 50; ll p[64]; int st[64]; int tot = 0; void ins(ll x) { for (int i = 0; i < tot; ++i) { if ((x >> st[i]) & 1) { if (p[i]) x ^= p[i]; else { p[i] = x; break; } } } } int n; ll a[maxn]; int main() { ios::sync_with_stdio(false); cin >> n; ll sum = 0; for (int i = 0; i < n; ++i) cin >> a[i], sum ^= a[i]; for (int i = 60; i >= 0; --i) if (!((sum >> i) & 1)) st[tot++] = i; for (int i = 0; i < n; ++i) ins(a[i]); ll res = 0; for (int i = 0; i < tot; ++i) if (!((res >> st[i]) & 1)) res ^= p[i]; cout << res + (sum ^ res) << endl; }
#include<iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1<<21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; int prim() { int a, min; int d[MAX], p[MAX], color[MAX]; for ( int i = 0; i < n; i++) { d[i] = INFTY; p[i] = -1; color[i] = WHITE; } d[0] = 0; while ( 1 ) { min = INFTY; a = -1; for ( int i = 0; i < n; i++ ) { if ( min > d[i] && color[i] != BLACK ) { a = i; min = d[i]; } } if ( a == -1 ) break; color[a] = BLACK; for ( int v = 0; v < n; v++ ) { if ( color[v] != BLACK && M[a][v] != INFTY ) { if ( d[v] > M[a][v] ) { d[v] = M[a][v]; p[v] = a; color[v] = GRAY; } } } } int sum = 0; for ( int i = 0; i < n; i++ ) { if ( p[i] != -1 ) sum = sum + M[i][p[i]]; } return sum; } int main() { cin >> n; for ( int i = 0; i < n; i++ ) { for ( int j = 0; j < n; j++ ) { int e; cin >> e; M[i][j] = (e == -1) ? INFTY : e; } } cout << prim() << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> duo; #define TT template<typename T> TT T sq(T x){ return x*x; } TT int ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); } TT int lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); } TT void sort(T& v){ sort(v.begin(), v.end()); } TT void revs(T& v){ reverse(v.begin(), v.end()); } TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); } TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);} TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} } TT T In(){ T x; cin >> x; return x; } static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); } static inline int in(){ int x; scanf("%d", &x); return x; } static inline string instr(){ string x; cin >> x; return x; } static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; int main() { while (int e = in()){ int m = 1e8; for (int z = 0; z <= 100; ++z){ for (int y = 0; y <= 1000; ++y){ int x = e - (sq(z) * z + sq(y)); if (x < 0) continue; m = min(m, z + y + x); } } printf("%d\n", m); } return (0); }
#include <algorithm> #include <iostream> #include <string> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <limits> #include <map> #include <queue> #include <stack> #include <vector> #define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i)) #define rep(i,n) FOR(i,0,n) #define pb push_back #define all(v) begin(v), end(v) #define debug(x) cerr << #x << ": " << x << endl #define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<i_i, int> p_i; typedef vector<int> vi; typedef vector<vector<int>> vvi; template<class T> using vv=vector<vector<T>>; typedef deque<int> di; typedef deque<deque<int>> ddi; int main() { while(1) { int e; cin >> e; if(e == 0) { break; } int min = 1000000; for(int z = 0; z*z*z <= e; z++) { for(int y = 0; y*y <= e-z*z*z; y++) { int tmp = e-y*y-z*z*z + y + z; if(tmp < min){ min = tmp; } } } cout << min << endl; } return 0; }
1
#include<bits/stdc++.h> using namespace std; #define int long long int dp[111][111]; signed main(){ int n; cin>>n; int r[n],c[n]; for(int i=0;i<n;i++) cin>>r[i]>>c[i]; int INF=1LL<<55LL; for(int i=0;i<111;i++) fill_n(dp[i],111,INF); for(int i=0;i<n;i++) dp[i][i+1]=0; for(int i=2;i<=n;i++) for(int j=0;j<=n-i;j++) for(int k=1;k<i;k++) dp[j][j+i]=min(dp[j][j+i],dp[j][j+k]+dp[j+k][j+i]+(r[j]*r[j+k]*c[j+i-1])); cout<<dp[0][n]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #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() { int mod=1000000007; int n; cin >> n; long to=0; vector<int>a(n); vector<int>b(n); vector<int>c(n); rep(i,n){ cin>>a.at(i)>>b.at(i); to-=b.at(i); c.at(i)=a.at(i)+b.at(i); } sort(c.begin(),c.end()); reverse(c.begin(),c.end()); rep(i,(n-1)/2+1){ to+=c.at(i*2); } cout<<to<<endl; }
0
// https://atcoder.jp/contests/abc127/tasks/abc127_e #include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++) #define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\n' : ' ') #define DUMP2D(a, n, m) REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\n' : ' '); puts("") //------------------------------------------------------------------------------ ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) { res = res * x % mod; } x = x * x % mod; n >>= 1; } return res; } ll mod_inverse(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } ll mod_comb(ll n, ll k, ll mod) { if (n < 0 || k < 0 || n < k) return 0; ll a = 1, b = 1; while(k > 0) { a = a * n-- % mod; b = b * k-- % mod; } return a * mod_inverse(b, mod) % mod; } //------------------------------------------------------------------------------ const int M_MAX = 2*1e5; const int MOD = 1e9+7; ll N, M, K; void solve() { ll y = 1; y = y * (N - 1) % MOD; y = y * N % MOD; y = y * (N + 1) % MOD; y = y * M % MOD; y = y * M % MOD; ll x = 1; x = x * (M - 1) % MOD; x = x * M % MOD; x = x * (M + 1) % MOD; x = x * N % MOD; x = x * N % MOD; ll ans = (x + y) % MOD; ans = ans * mod_inverse(6, MOD) % MOD; ans = ans * mod_comb(N * M - 2, K - 2, MOD) % MOD; printf("%lld\n", ans); } void input() { scanf("%lld%lld%lld", &N, &M, &K); } int main() { input(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int64_t MAX = 510000; const int64_t MOD = 1000000007; int64_t 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; } } int64_t 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(){ int64_t N,M,K; cin>>N>>M>>K; COMinit(); int64_t ans=0; for(int i=0;i<N;++i){ for(int j=0;j<M;++j){ if(i!=0 && j!=0) ans += 2*(i+j)*(M-j)*(N-i); else ans += (i+j)*(M-j)*(N-i); ans %= MOD; } } ans *= COM(N*M-2,K-2); cout<<ans%MOD<<endl; }
1
#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, sum = 0; cin >> n; vd a(n); rep(i, n) { cin >> a[i]; sum += a[i]; a[i] *= n; } ll ans, d = 1e18; rep(i, n) { ll nd = abs(sum - a[i]); if(nd < d) { d = nd; ans = i; } } cout << ans << endl; }
#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; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> PII; const int MM = 1e9 + 7; const double eps = 1e-8; const int MAXN = 2e6 + 10; int n, m; int a[4][MAXN]; int b[MAXN]; int c[MAXN], d[MAXN]; void read(){ scanf("%d", &n); for(int i = 1; i <= 3; i++){ for(int j = 1; j <= n; j++) scanf("%d", &a[i][j]); } } int solve(){ for(int i = 1; i <= n; i++){ int p = a[1][i], q = a[2][i], r = a[3][i]; if (q % 3 != 2) return 0; if (p + r != q * 2) return 0; if (abs(p - q) != 1 || abs(r - q) != 1) return 0; if (p < r) b[i] = r / 3; else b[i] = -p / 3; } for(int i = 1; i <= n; i++){ if (abs(b[i]) % 2 != i % 2) return 0; } for(int i = 1; i <= n; i++){ c[abs(b[i])] = i; } for(int i = 1; i <= n; i++){ int x = c[i]; if (x != i){ d[i] = b[x] * ((x - i) % 4 == 0 ? 1 : -1); b[i+1] *= -1; b[x] = b[i] * ((x - i) % 4 == 0 ? 1 : -1); c[abs(b[x])] = x; } else d[i] = b[i]; } for(int i = 1; i <= n - 2; i++){ if (d[i] < 0){ d[i + 2] *= -1; } } return d[n-1] > 0 && d[n] > 0; /* for(int i = 1; i <= n; i++) cout<<b[i]<<' '; cout<<endl; int p = 0, q = 0; for(int i = 1; i <= n; i++){ if (i & 1){ if (abs(abs(b[i]) - i) / 2 % 2 != (b[i] < 0)) p++; } else{ if (abs(abs(b[i]) - i) / 2 % 2 != (b[i] < 0)) q++; } } cout<<p<<' '<<q<<endl; return p % 2 == q % 2;*/ } void printans(int flag){ puts(flag ? "Yes" : "No"); } int main(){ int T = 1; // cin>>T; while(T--){ read(); ; printans(solve()); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define MOD 1000000007 #define mod9 1000000009 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); #define mp make_pair #define pb push_back #define ct ll t;cin>>t;while(t--) #define bi begin() #define ei end() #define fi first #define se second #define foe(i,a,b) for(ll i=a;i<b;i++) #define sz(s) s.size() #define mem(a,s) memset(a,s,sizeof(a)) #define MAX 8000000000000000064LL #define MIN -8000000000000000064LL bool isPrime(ll n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (ll i = 2; i*i <= n; i++) if (n % i == 0) return false; return true; } long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res) * (a); a = (a) * (a); b >>= 1; } return res; } bool compare(const pair<ll,ll>&a,const pair<ll,ll>&b) { return (a.se>b.se); } ll lcm(ll a,ll b) { return(a*b)/__gcd(a,b); } // vector<ll>s; // // set<ll>s1; // bool prime[1000009]={false}; // // set<ll>s1; // void Sieve() // { // ll n=1000009; // prime[0]=prime[1]=true; // // s[0]=0; // // s[1]=1; // // s.pb(1); // // t.insert(1); // for (int p=2; p*p<=n; p++) // { // if(prime[p]==false) // { // // s.pb(p); // for(ll j=p*p;j<=n;j+=p) // { // prime[j]=true; // } // } // } // for(ll i=2;i<n;i++) // { // if(!prime[i]) // { // // s.pb(i); // // s1.insert(i); // } // } // } // ll eea(ll a, ll b, ll & x, ll & y) { // if (a == 0) { // x = 0; // y = 1; // return b; // } // ll x1, y1; // ll d=eea(b % a, a, x1, y1); // x = y1 - (b / a) * x1; // y = x1; // return d; // } // bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) { // g = eea(abs(a), abs(b), x0, y0); // if (c % g) { // return false; // } // x0 *= c / g; // y0 *= c / g; // if (a < 0) x0 = -x0; // if (b < 0) y0 = -y0; // return true; // } // ll modinverse(ll a,ll m) // { // ll x, y; // eea(a, m, x, y); // if (__gcd(a,m) != 1) { // return -1; // } // else { // x = (x % m + m) % m; // cout << x << endl; // return x; // } // } int add(int a, int b){return (a%MOD + b%MOD + ((MAX)/MOD)*MOD)%MOD;} int sub(int a, int b){return (a%MOD - b%MOD + ((MAX)/MOD)*MOD)%MOD;} int mul(int a, int b){return ((a%MOD)*(b%MOD) + ((MAX)/MOD)*MOD)%MOD;} int fact[1000007]={0}; ll expo(ll x, ll y) {ll res=1;x=x%MOD;while(y>0){if(y&1)res=(1ll*res*x)%MOD; y=y>>1;x=(1ll*x*x)%MOD;} return res;} void facto() {fact[0]=1;fact[1]=1;for(ll i=2;i<1000007;i++)fact[i]=(fact[i-1]*i)%MOD;} int ncr(int n,int r) {int res=1; res=fact[n]; res=(res*(expo(fact[r],MOD-2)))%MOD; res=(res*(expo(fact[n-r],MOD-2)))%MOD; return res;} ll npr(ll n,ll r) {facto(); ll res=1; res=fact[n]; res=(res*(expo(fact[n-r],MOD-2)))%MOD; return res; } int const N=1e7+9; int const inf = 1e9+5; void solve() { ll h1,m1,h2,m2,k; cin>>h1>>m1>>h2>>m2>>k; ll t=0; if(m1==0) { t=h2-h1; } else { t=h2-h1-1; } ll ans=t*60-m1+m2; if(m1!=0) { ans+=60; } cout<<ans-k<<"\n"; } int main() { fast // Sieve(); // ct { solve(); } }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pli = pair<ll, int>; using Graph = vector<vector<bool>>; const int MOD = 1e9 + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); int A, B; cin >> A >> B; A--, B--; int C = 0, D = 0, W = 0; for (int i = 0; i * i < max(A, B); ++i) W++; W = max(2 * W, 1); C = (4 * A + W - 1) / W, D = (4 * B + W - 1) / W; C += C & 1, D += D & 1; C = max(C + (C & 1), 1), D = max(D + (D & 1), 1); printf("%d %d\n", C + D, W); for (int i = 0; i < C; ++i) { for (int j = 0; j < W; ++j) { bool iswhite = (A > 0) & !((i | j) & 1); A -= iswhite; putchar(iswhite ? '.' : '#'); } putchar('\n'); } for (int i = 0; i < D; ++i) { for (int j = 0; j < W; ++j) { bool isblack = (B > 0) & (i & j & 1); B -= isblack; putchar(isblack ? '#' : '.'); } putchar('\n'); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define endl '\n' #define FASTINOUT ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); const ll mod = 1000000007; const double PI = 3.141592653589793238463; const int N=2e5+9; char a[109][109]; int main() { FASTINOUT; int r,c; cin>>r>>c; r--,c--; char l='.'; for (int i=1;i<=100;i++){ if (i==50) l='#'; for (int j=1;j<=100;j++) a[i][j]=l; } for (int i=1;i<=49&&c;i+=2){ for (int j=1;j<=100&&c;j++){ if (a[i][j-1]!='#'&&a[i-1][j]!='#'){ a[i][j]='#'; c--; } } } for (int i=52;i<=100&&r;i+=2){ for (int j=1;j<=100&&r;j++){ if (a[i][j-1]!='.'&&a[i-1][j]!='.'){ a[i][j]='.'; r--; } } } cout<<100<<" "<<100<<endl; for (int i=1;i<=100;i++){ for (int j=1;j<=100;j++){ cout<<a[i][j]; } cout<<endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(){ vector<string> s(3); rep(i,3){ cin >> s.at(i); } int c=0; vector<int> a(3,0); while(1){ int cp=c; a.at(c)++; for(int i=0;i<3;i++){ if(a.at(i)>s.at(i).size()){ char d='A'+i; cout << d; return 0; } } c=s.at(cp).at(a.at(cp)-1)-'a'; } }
#include<bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t=1; //cin>>t; while(t--) { string a,b,c; cin>>a>>b>>c; char ch = a[0]; int i=0; a.erase(i,1); char ans; while(true) { if(ch=='a') { if(a.size()==0) { ans = 'A'; break; } ch = a[0]; a.erase(i,1); } else if(ch=='b') { if(b.size()==0) { ans = 'B'; break; } ch = b[0]; b.erase(i,1); } else { if(c.size()==0) { ans = 'C'; break; } ch = c[0]; c.erase(i,1); } } cout<<ans; } }
1
#include <bits/stdc++.h> using namespace std; // type alias typedef long long LL; typedef pair<int,int> II; typedef tuple<int,int,int> III; typedef vector<int> VI; typedef vector<string> VS; typedef unordered_map<int,int> MAPII; typedef unordered_set<int> SETI; template<class T> using VV=vector<vector<T>>; // minmax template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; } template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; } // repetition #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) for(int i=0;i<=(n);++i) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) for(int i=0;i<(n);++i) #define FORR(x,arr) for(auto& x:arr) #define SZ(a) int((a).size()) // collection #define ALL(c) (c).begin(),(c).end() // DP #define MINUS(dp) memset(dp, -1, sizeof(dp)) #define ZERO(dp) memset(dp, 0, sizeof(dp)) // stdout #define println(args...) fprintf(stdout, ##args),putchar('\n'); // debug cerr template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; } void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; } template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); } template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; } #define __KUMATRACE__ true #ifdef __KUMATRACE__ #define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); } #define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; } #define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); } #else #define dump(args...) #define dumpc(ar) #define dumpC(beg,end) #endif // $ cp-batch VotingJudges | diff VotingJudges.out - // $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address VotingJudges.cpp && ./a.out /* 6/28/2020 9:50- */ const int MAX_N=1e6+1; LL A[MAX_N],M; int N,V,P; bool ok(int m) { vector<LL> as(A,A+N); int v=0; as[m]+=M,++v; REP(i,P-1) if(v<V) as[i]+=M,++v; int i=N-1; for(; i>=0&&v<V&&as[i]<=as[m]-M; --i) { LL x=min(as[m]-as[i],(LL)M); if(x>0) as[i]+=x,++v; } LL rem=M*(V-v); //dump(p,rem,i,as[P-1],as[p]); //dumpc(as); while(rem>0&&i>P-2) { LL x=min(as[m]-as[i],rem); rem-=x,as[i]+=x; --i; } if(rem>0) return false; else return as[m]>=as[P-1]; } void solve() { sort(A,A+N),reverse(A,A+N); //dumpC(A,A+N); int good=P-1,bad=N; while(abs(good-bad)>1) { int mid=(good+bad)/2; bool res=ok(mid); //dump(mid,res); (res?good:bad)=mid; } cout<<good+1<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout<<setprecision(12)<<fixed; cin>>N>>M>>V>>P; REP(i,N) cin>>A[i]; solve(); return 0; }
#include <bits/stdc++.h> #include <boost/multiprecision/cpp_int.hpp> using namespace std; using bint = boost::multiprecision::cpp_int; using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>; #define rep(i,n) for (int i = 0; i < (n); i++) #define repSE(i,s,n) for (int i = (s); i < (n); i++) #define rrepSE(i,s,e) for (int i = (s); i > (e); i--) #define ssort(v) sort(v.begin(), v.end()) #define gsort(v) sort(v.rbegin(), v.rend()) template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; } #define DEBUG_PRINT(var) std::cout << #var << " = " << var << "\n"; int main() { int n; cin >> n; vector<int> a(n+1), b(n+1); repSE(i,1,n+1) cin >> a[i]; rrepSE(i,n,n/2) { b[i] = a[i]; } rrepSE(i,n/2,0) { int sum = a[i]; repSE(j,2,n/i+1) { sum = (sum - b[j*i] + 2) % 2; } b[i] = sum % 2; } int m = 0; vector<int> ans; repSE(i,1,n+1) { if (b[i] == 1) { m++; ans.push_back(i); } } cout << m << "\n"; rep(i,m) cout << ans[i] << " "; cout << "\n"; return 0; }
0
#include<bits/stdc++.h> using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++) #define INF 10000000000 #define MOD 1000000007 using ll = long long; using pint = pair<int,int>; using pll = pair<ll,ll>; using Graph = vector<vector<int>>; int main(){ set<char> m; string s; cin>>s; for(auto c:s)m.insert(c); for(char i = 'a'; i <= 'z'; i++){ if(!m.count(i)){ cout<<i<<endl; return 0; } } cout<<"None"<<endl; }
#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; using ll=long long; using ld=long double; #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update> #define endl "\n" #define pb push_back #define mp make_pair #define F first #define S second #define int long long int #define pll pair<int , int> #define ALL(v) v.begin(),v.end() #define ALLR(v) v.rbegin(),v.rend() #define pii 3.14159265358979323 #define inf LLONG_MAX #define ones(x) __builtin_popcount(x) #define fill(a,b) memset(a,b,sizeof(a)) #define mod 1000000007 #define hell 998244353 ll mod_pow(ll a,ll b,ll m) { ll res = 1; while(b) { if(b&1) { res=(res*a) % m; } a=(a*a) % m; b>>=1; } return res; } ll mod_inverse(ll a) { return mod_pow(a , mod-2 , mod); } void solve() { string s; cin >> s; int f[26] {}; for(auto u : s) { f[u-'a']++; } for(int i = 0; i < 26; ++i) { if(f[i] == 0) { cout << (char)(i+'a'); return; } } cout << "None"; } signed main() { fast; int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define reps(i, n) for (int i = 0; i <= (int)(n); i++) #define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define rreps(i, x) for (int i = (int)(x); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define fi first #define se second typedef long long ll; typedef pair<int, int> P; const int inf = INT_MAX; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; const double EPS = 1e-10; const double PI = acos(-1.0); template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill( (T*)array, (T*)(array+N), val ); } template <class T1, class T2, class Pred = std::less<T2> > struct sort_pair_second { bool operator()(const std::pair<T1,T2>&left, const std::pair<T1,T2>&right) { Pred p; return p(left.second, right.second); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<ll> a(N); rep(i, N) cin >> a[i]; ll s = 0; rep(i, N) s ^= a[i]; ll ans = s; rep(i, N) { // s で1が立っていたビットを強制的にa[i]を0にビットを変換(考慮しない) a[i] &= ~s; } int r = 0; vector<int> top(N); for (int i = 60; i >= 0; i--) { int j = r; for (; j < N; j++) { if (a[j] >> i & 1) { swap(a[r], a[j]); break; } } if (j == N) continue; top[r] = i; rep(k, N) { if (r == k) continue; if (a[k] >> i & 1) a[k] ^= a[r]; } r++; } ll x = 0; for (int i = 60; i >= 0; i--) { ll nx = x | (1ll << i); ll b = nx; rep(j, r) { if (b >> top[j] & 1) b ^= a[j]; } if ((b & nx) == 0) { x = nx; } } ans += x * 2; cout << ans << endl; return 0; }
#include <iostream> #include <string> #include <cmath> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<iomanip> #define _USE_MATH_DEFINES #include <math.h> #include <functional> using namespace std; #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 1000000007; const ll MAX = 1000001; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a%b); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } /////////////////////////// ll p(ll n) { if (n == 0) { return 1; } return 2 * p(n - 1); } int main() { ll N; cin >> N; vector<ll> A(N + 1); repn(i, N) cin >> A[i]; sort(++A.begin(), A.end(), greater<ll>()); vector<vector<ll>> d(N + 1,vector<ll>(60)); vector<ll> count1(60); repn(i, N) { ll t = A[i]; rep(j,60){ d[i][59-j] = t%2; count1[59-j]+= d[i][59-j]; t /= 2; } } repn(i, N) { rep(j, 60) { if (count1[j] % 2 == 1) { d[i][j] = 0; } } } /* repn(i, N) { rep(j, 60) { cout << d[i][j]; } cout << endl; } */ repn(i, N) { ll K=-1; rep(j,60){ if (d[i][j] == 1) { K = j; break; } } //cout << i << K << endl; if (K == -1) { continue; } repn(l, N) { if (l == i) { continue; } if (d[l][K] == 1) { for (int j = K; j < 60; j++) { d[l][j] = (d[i][j] + d[l][j]) % 2; } } } /* repn(i, N) { rep(j, 60) { cout << d[i][j]; } cout << endl; } */ } vector<ll> sum(60, 0); repn(i, N) { rep(j, 60) { sum[j]+=d[i][j]; } } //rep(j, 60) { cout << count1[j] << endl; } ll ans=0; rep(j, 60) { ans += (count1[j]%2+2*(sum[j]%2)) * p(59-j); } cout << ans; system("PAUSE"); }
1
#include <bits/stdc++.h> using namespace std; #define fastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define endl '\n' #define ft first #define sd second #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() typedef long long ll; typedef pair<int, int> pii; typedef pair<char, char> pcc; typedef pair<double, double> pdd; typedef pair<long long, long long> pll; // scanf("%d %d %d %d", &w, &b, &d, &s); %lld // printf("%d\n", points); // const double PI = acos(-1); int main() { fastIO; int n; cin >> n; int ans = n + (n*n) + (n*n*n); cout << ans << endl; return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #define pp pair<int,int> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define ld long double #define al(a) (a).begin(),(a).end() #define mk make_pair #define check cout<<"?"<<endl; ll MOD=1000000007; ll mod=998244353; int inf=1000001000; ll INF=1e18+5; int main(){ string s; cin>>s; int n=s.size(),ans=inf; rep(i,n-2){ int num=100*(s[i]-'0')+10*(s[i+1]-'0')+s[i+2]-'0'; ans=min(ans,abs(753-num)); } cout<<ans<<endl; }
0
#include<bits/stdc++.h> #define For(i,x,y) for (register int i=(x);i<=(y);i++) #define FOR(i,x,y) for (register int i=(x);i<(y);i++) #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) #define Debug(v) for (auto i:v) printf("%lld ",i);puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define fil(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pa; typedef pair<ll,ll> PA; typedef vector<int> poly; inline ll read(){ ll x=0,f=1;char c=getchar(); while ((c<'0'||c>'9')&&(c!='-')) c=getchar(); if (c=='-') f=-1,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } ll f[100][100]; int main(){ int n=read(); For(i,1,n){ double x; scanf("%lf",&x); ll y=(ll)(x*1e9+0.5); int a=0,b=0; while (y%2==0) y/=2,a++; while (y%5==0) y/=5,b++; f[a][b]++; } ll ans=0,ans2=0; For(i,0,60) For(j,0,60) if (f[i][j]){ For(_i,0,60) For(_j,0,60) if (f[_i][_j]&&min(i+_i,j+_j)>=18){ if (i!=_i||j!=_j) ans+=f[i][j]*f[_i][_j]; else ans2+=f[i][j]*(f[i][j]-1)/2; } } printf("%lld\n",ans/2+ans2); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pdd; typedef vector<ll> vll; typedef vector<ld> vld; typedef vector<pll> vpl; typedef vector<vll> vvll; #define ALL(a) a.begin(),a.end() #define SZ(a) ((int)a.size()) #define FI first #define SE second #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 FOR(i,a,b) for(int i=(a);i<(b);i++) #define PB push_back #define EB emplace_back #define MP(a,b) make_pair(a,b) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define yes cout<<"Yes"<<endl #define YES cout<<"YES"<<endl #define no cout<<"No"<<endl #define NO cout<<"NO"<<endl #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL const int inf = 1e9; const ll linf = 1LL << 50; const double eps = 1e-10; const int MOD = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vpl r(n); map<pll, ll> m; vpl f; REP(i, n) { string s; cin >> s; ll k = s.size(); ll p = k - 1; REP(i, k) { if (s[i] == '.') p = i; } ll num = 0; REP(i, k) { if (s[i] == '.') continue; num *= 10; num += s[i] - '0'; } ll r2 = 0, r5 = 0; REP(i, k - p - 1) { if (num % 2 == 0)num /= 2; else r2 -= 1; if (num % 5 == 0)num /= 5; else r5 -= 1; } while (num % 2 == 0) { num /= 2; r2++; } while (num % 5 == 0) { num /= 5; r5++; } r[i].first = r2; r[i].second = r5; m[r[i]] += 1; } ll ans = 0; // for(ll i=-10; i<=14; i++){ // for (ll j = -10; j <= 14; j++) { // ll t = m[pll(i, j)]; // if (t == 0) // continue; // // ll res = 0; // for (ll k = i; k <= 14; k++) { // for (ll q = -10; q <= 14; q++) { // if (k + i < 0 || q + j < 0) // continue; // // // if (k == i && q < j) // continue; // if (k == i && q == j) // res += t * (t - 1) / 2; // else // res += t * m[pll(k, q)]; // // // } // } // // ans += res; // // } // } for (auto p1 : m) { ll res = 0; for (auto p2 : m) { if (p1.FI.FI + p2.FI.FI < 0 || p1.FI.SE + p2.FI.SE < 0) continue; if (p1.FI > p2.FI) continue; if (p1 == p2) res += p1.SE * (p1.SE - 1) / 2; else res += p1.SE * p2.SE; } ans += res; } cout << ans << endl; }
1
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int a,b,m; cin>>a>>b>>m; int ref[a],mic[b]; for (int i=0;i<a;++i){ cin>>ref[i]; } for (int i=0;i<b;++i){ cin>>mic[i]; } int mini = INT_MAX; for(int i=0;i<m;++i){ int x,y,c; cin>>x>>y>>c; mini = min(mini,ref[x-1]+mic[y-1]-c); } sort(ref,ref+a); sort(mic,mic+b); cout<<min(mini,ref[0]+mic[0]); }
#include <bits/stdc++.h> using namespace std; int main() { int A, B, M; cin >> A >> B >> M; vector<int>a(A); vector<int>b(B); vector<vector<int>>xyc(3, vector<int>(M)); for(int i=0; i<A; i++)cin >> a.at(i); for(int i=0; i<B; i++)cin >> b.at(i); for(int i=0; i<M; i++){ for(int j=0; j<3; j++){ cin >> xyc.at(j).at(i); } } int mina = a.at(0); int minb = b.at(0); for(int i=0; i<A; i++){ if(mina > a.at(i))mina = a.at(i); } for(int i=0; i<B; i++){ if(minb > b.at(i))minb = b.at(i); } int ans = mina + minb; for(int i=0; i<M; i++){ int waribiki = a.at(xyc.at(0).at(i)-1) + b.at(xyc.at(1).at(i)-1) - xyc.at(2).at(i); if(ans > waribiki)ans = waribiki; } cout << ans << endl; }
1
#include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<queue> #define int long long using namespace std; int cost[101][101]; int d[101]; bool used[101]; int V,k,o,a,b,c,di,e,ans; void dij(int s){ fill(d,d+101,10000000); fill(used,used+101,false); d[s]=0; while(true){ int v=-1; for(int u=0;u<V;u++){ if(!used[u]&&(v==-1||d[u]<d[v]))v=u; } if(v==-1)break; used[v]=true; for(int u=0;u<V;u++){ d[u]=min(d[u],d[v]+cost[v][u]); } } } signed main(){ for(;;){ cin>>V>>k; if(V==0&&k==0)break; fill(cost[0],cost[0]+101*101,10000000); for(int i=0;i<k;i++){ cin>>o; if(o==1){ cin>>c>>di>>e; cost[c-1][di-1]=min(cost[c-1][di-1],e); cost[di-1][c-1]=min(cost[di-1][c-1],e); } if(o==0){ cin>>a>>b; dij(a-1); if(d[b-1]==10000000)cout<<-1<<endl; else cout<<d[b-1]<<endl; } } } return 0; }
#include<iostream> using namespace std; int main(){ while(1){ int INF=100000100; int n,k,ans[105][105],t,a,b,c; scanf("%d%d",&n,&k); if(n==0&&k==0)break; for(int i=1;i<=n+2;i++){ for(int j=1;j<=n+2;j++){ ans[i][j]=INF; if(i==j)ans[i][j]=0; } } for(int i=0;i<k;i++){ scanf("%d",&t); if(t==0){ scanf("%d%d",&a,&b); if(ans[a][b]==INF)printf("%d\n",-1); else printf("%d\n",ans[a][b]); } if(t==1){ scanf("%d%d%d",&a,&b,&c); if(ans[a][b]>c){ ans[a][b]=c; ans[b][a]=c; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(ans[i][j]>ans[i][a]+ans[a][b]+ans[b][j])ans[i][j]=ans[i][a]+ans[a][b]+ans[b][j]; if(ans[i][j]>ans[i][b]+ans[b][a]+ans[a][j])ans[i][j]=ans[i][b]+ans[b][a]+ans[a][j]; } } } } } } }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int a,b,c,d; cin >> a; cin >> b; cin >> c; cin >> d; cout << min(a,b)+min(c,d) <<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; int sum = 0; for (int i = 0; i < 4; i++) { cin >> a >> b >> c >> d; } sum += (a < b) ? a : b; sum += (c < d) ? c : d; cout << sum << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD (long long)(1e9+7) #define INF (1LL<<60) #define rep(i,n) for(ll i = 0; i < (n); i++) #define rep1(i,n) for(ll i = 1; i <= (n); i++) 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 gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); } // mod m におけるa の逆元 ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } // 素因数分解 vector<pair<ll, ll>> prim; void pf(ll n) { ll s = sqrt(n); ll r = 0; for(ll i = 2; i <= s; i++) { if((n % i) == 0) { r = 0; do { r++; n = n / i; } while((n % i) == 0); prim.push_back({i, r}); } } if(n > s) { prim.push_back({n, 1}); } } void solve() { ll N, K; cin >> N >> K; vector<ll> a(N); rep(i, N) cin >> a[i]; ll ans = INF; ll d, a1, a2; rep(i, N - K + 1) { d = a[i + K - 1] - a[i]; a1 = d + abs(a[i + K - 1]); a2 = d + abs(a[i]); ans = min(ans, min(a1, a2)); } cout << ans << endl; } int main(void) { // ll t; cin >> t; rep(i, t) solve(); }
#include <bits/stdc++.h> #define NM_MAX 2002 using namespace std; int n, m; bool ma[NM_MAX][NM_MAX]; int lg[NM_MAX][NM_MAX]; int v[NM_MAX]; int mxl[NM_MAX]; int mxr[NM_MAX]; stack <int> st; int main() { cin >> n >> m; for(int i = 1; i <= n; i++) { string s; cin >> s; for(int j = 1; j <= m; j++) ma[i][j] = (s[j - 1] == '#'); } for(int i = 1; i < n; i++) for(int j = 1; j < m; j++) ma[i][j] = ma[i][j] ^ ma[i + 1][j] ^ ma[i][j + 1] ^ ma[i + 1][j + 1]; n--; m--; for(int i = n; i >= 1; i--) for(int j = 1; j <= m; j++) if(ma[i][j] == 1) lg[i][j] = 0; else lg[i][j] = lg[i + 1][j] + 1; int ans = max(n, m) + 1; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) v[j] = lg[i][j]; for(int j = 1; j <= m; j++) { while(!st.empty() && v[st.top()] >= v[j]) st.pop(); if(st.empty()) mxl[j] = j - 1; else mxl[j] = j - (st.top() + 1); st.push(j); } while(!st.empty()) st.pop(); for(int j = m; j >= 1; j--) { while(!st.empty() && v[st.top()] >= v[j]) st.pop(); if(st.empty()) mxr[j] = m - j; else mxr[j] = st.top() - 1 - j; st.push(j); } while(!st.empty()) st.pop(); for(int j = 1; j <= m; j++) ans = max(ans, (mxl[j] + mxr[j] + 2) * (v[j] + 1)); } cout << ans << "\n"; return 0; }
0
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); const long long mod = 1e9 + 7; int x[2]; cin >> x[0] >> x[1]; static long long line[2][100000] = {}; for (int i = 0; i < 2; i++) { for (int j = 0; j < x[i]; j++) { cin >> line[i][j]; } } long long sum[2] = {}; for (int i = 0; i < 2; i++) { for (int j = 0; j < x[i]; j++) { sum[i] += line[i][j] * j; sum[i] = (sum[i] % mod + mod) % mod; sum[i] -= line[i][j] * (x[i] - 1 - j); sum[i] = (sum[i] % mod + mod) % mod; } } cout << sum[0] * sum[1] % mod << endl; return 0; }
/* Written by Nitrogens Desire for getting accepted!! */ #include <cstdio> #include <ctime> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream> #include <algorithm> #include <queue> #include <map> #include <bitset> #include <stack> #include <set> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair <ll, int> pli; typedef pair <db, db> pdd; const int maxn = 1e5+5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const db PI = acos(-1); const db ERR = 1e-10; #define Se second #define Fi first #define pb push_back #define dbg(x) cout<<#x<<" = "<< (x)<< endl #define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl #define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl ll x[maxn], y[maxn]; ll a[maxn], b[maxn]; ll sum(ll n) { return n * (n + 1) / 2 % Mod; } int main() { //ios::sync_with_stdio(false); //freopen("a.txt","r",stdin); //freopen("b.txt","w",stdout); int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", &x[i]); for (int i = 1; i <= m; i++) scanf("%lld", &y[i]); for (int i = 1; i < n; i++) a[i] = (x[i + 1] - x[i] + Mod) % Mod; for (int i = 1; i < m; i++) b[i] = (y[i + 1] - y[i] + Mod) % Mod; n--, m--; ll X = 0, Y = 0; for (int i = 1; i <= n; i++) { ll cnt = sum(n) - sum(i - 1) - sum(n - i); cnt %= Mod; cnt = (cnt + Mod) % Mod; ll delta = cnt * a[i] % Mod; X = (X + delta) % Mod; } for (int i = 1; i <= m; i++) { ll cnt = sum(m) - sum(i - 1) - sum(m - i); cnt %= Mod; cnt = (cnt + Mod) % Mod; ll delta = cnt * b[i] % Mod; Y = (Y + delta) % Mod; } ll ans = X * Y % Mod; printf("%lld\n", ans); //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl; return 0; }
1
#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <stack> #include <vector> #include <map> #include <set> #include <iostream> using namespace std; int c[5]; int counts[14]; int countscounts[5]; bool IsFourCard() { return countscounts[4] == 1; } bool IsFullHouse() { return countscounts[3] == 1 && countscounts[2] == 1; } bool IsStraight() { for (int i = 0; i < 4; ++i) { if (c[i]+1 != c[i+1]) { int t[] = { 1, 10, 11, 12, 13 }; for (int j = 0; j < 5; ++j) { if (c[j] != t[j]) return false; } return true; } } return true; } bool IsThreeCard() { return countscounts[3] == 1; } bool IsTwoPair() { return countscounts[2] == 2; } bool IsOnePair() { return countscounts[2] == 1; } bool IsNull() { return true; } int main() { bool (*judge[])() = { IsFourCard, IsFullHouse, IsStraight , IsThreeCard, IsTwoPair, IsOnePair, IsNull }; char* names[] = { "four card", "full house", "straight" , "three card", "two pair", "one pair", "null" }; while (scanf("%d,%d,%d,%d,%d", c, c+1, c+2, c+3, c+4) != EOF) { sort(c, c+5); memset(counts, 0, sizeof(counts)); memset(countscounts, 0, sizeof(countscounts)); for (int i = 0; i < 5; ++i) ++counts[c[i]]; for (int i = 1; i < 14; ++i) ++countscounts[counts[i]]; for (int i = 0; i < sizeof(judge)/sizeof(judge[0]); ++i) { if (judge[i]()) { puts(names[i]); break; } } } return 0; }
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; int cards[5]; bool check () { for (int i = 0; i < 5; i++) if (cards[i] > 0) return true; return false; } void ref() { for (int i = 0; i < 5; i++) cards[i] = 0; } int main() { while (scanf("%d,%d,%d,%d,%d", &cards[0], &cards[1], &cards[2], &cards[3], &cards[4]) != EOF) { int yaku = 0; sort(cards, cards + 5); if (check() && cards[0] == cards[1] && cards[1] == cards[2] && cards[2] == cards[3]) {yaku = 6; ref();} if (check() && cards[1] == cards[2] && cards[2] == cards[3] && cards[3] == cards[4]) {yaku = 6; ref();} if (check() && cards[0] == cards[1] && cards[1] == cards[2] && cards[3] == cards[4]) {yaku = 5; ref();} if (check() && cards[2] == cards[3] && cards[3] == cards[4] && cards[0] == cards[1]) {yaku = 5; ref();} if (check() && cards[0] + 1 == cards[1] && cards[1] + 1 == cards[2] && cards[2] + 1 == cards[3] && cards[3] + 1 == cards[4]) {yaku = 4; ref();} if (check() && cards[0] == 1 && cards[1] == 10 && cards[2] == 11 && cards[3] == 12 && cards[4] == 13) {yaku = 4; ref();} int tmp = 0; for (int i = 0; i < 5 && check(); i++) { int t = cards[i]; cards[i] = 0; if (t == 0) continue; for (int j = 0; j < 5; j++) { if (i == j) continue; if (cards[j] == 0) continue; if (t == cards[j]) tmp++; } yaku = tmp; } switch (yaku) { case 1: cout << "one pair" << endl; break; case 2: cout << "two pair" << endl; break; case 3: cout << "three card" << endl; break; case 5: cout << "full house" << endl; break; case 6: cout << "four card" << endl; break; case 4: cout << "straight" << endl; break; default: cout << "null" << endl; } } return 0; }
1
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,s,n) for (int i = (s); i < (n); ++i) #define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define len(x) (int)(x).size() #define dup(x,y) (((x)+(y)-1)/(y)) #define pb push_back #define Field(T) vector<vector<T>> using namespace std; using ll = long long; using P = pair<int,int>; using PP = pair<int,P>; vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1}; int inf = 1000000000; int main() { int h, w; cin >> h >> w; vector<string> s(h); rep(i,0,h) cin >> s[i]; priority_queue<PP, vector<PP>, greater<PP>> q; vector<vector<int>> dist(h, vector<int>(w, inf)); dist[0][0] = 1; q.push(PP(0,P(0,0))); while(!q.empty()) { PP p = q.top(); q.pop(); int x = p.se.fi, y = p.se.se; rep(i,0,4) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (s[nx][ny] == '#') continue; if (dist[nx][ny] > dist[x][y] + 1) { dist[nx][ny] = dist[x][y] + 1; q.push(PP(dist[nx][ny], P(nx, ny))); } } } if (dist[h-1][w-1] == inf) { cout << -1 << endl; return 0; } int hash = 0; rep(i,0,h) rep(j,0,w) if (s[i][j] == '#') hash++; int ans = h*w - hash - dist[h-1][w-1]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree < pairs , null_type , greater<pairs>, rb_tree_tag , tree_order_statistics_node_update > #define ll long long #define lld long double #define vc vector<ll> const ll MOD=(1e9 +7); typedef pair<ll,ll>pairs; ll power(ll a, ll b){ll res=1;a=a%MOD;while(b>0){if(b&1){res=(res*a)%MOD;b--;}a=(a*a)%MOD;b>>=1;} return res;} int main() { // your code goes here std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); ll t,n,i,j,k,l,c,f; cin>>n>>k; ll temp; vc a(n+1); for(i=1;i<=n;i++)cin>>a[i]; ll ans=0; for(i=1;i<n;i++) { temp=a[i]+a[i+1]; if(temp>k) { if(a[i+1]>=(temp-k)) a[i+1]-=(temp-k); else a[i+1]=0; ans+=temp-k; } } cout<<ans; return 0; }
0
#include<iostream> #include<math.h> #include<algorithm> #include<stdint.h> #include<vector> #include<deque> #include<stack> #include<functional> #include<string> #include<cstring> #include<time.h> #include<array> #include<iomanip> #include<list> #include<set> #include<map> #include<random> #include<unordered_map> #include<unordered_set> #include<bitset> #include <queue> using namespace std; using ll = long long; using ull = unsigned long long; using uint = unsigned int; using ldouble = long double; //BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney, tree, chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu, topologicalsort, kyourenketuseibun #define REP(i,a,b) for(int i = a; i < b; ++i) #define PRI(s) cout << s << endl #define PRIY PRI("Yes") #define PRIN PRI("No") using b = bitset<6400 * 2 + 64>; int main() { ll H, W; vector<vector<b>> f; vector<vector<ll>> a, b; cin >> H >> W; f.resize(H); a.resize(H); b.resize(H); REP(i, 0, H) { f[i].resize(W); a[i].resize(W); REP(j, 0, W) { f[i][j].reset(); cin >> a[i][j]; } } REP(i, 0, H) { b[i].resize(W); REP(j, 0, W) { cin >> b[i][j]; } } int i0 = 6400; f[0][0][i0 + b[0][0] - a[0][0]] = true; f[0][0][i0 - b[0][0] + a[0][0]] = true; auto bs = [](auto& b, int d) { if (d >= 0) return b << d; else return b >> -d; }; REP(j, 1, W) f[0][j] = bs(f[0][j - 1], b[0][j] - a[0][j]) | bs(f[0][j - 1], -b[0][j] + a[0][j]); REP(i, 1, H) f[i][0] = bs(f[i - 1][0], b[i][0] - a[i][0]) | bs(f[i - 1][0], -b[i][0] + a[i][0]); REP(i, 1, H)REP(j, 1, W) { f[i][j] = bs(f[i][j - 1], b[i][j] - a[i][j]) | bs(f[i - 1][j], b[i][j] - a[i][j]); f[i][j] |= bs(f[i][j - 1], -b[i][j] + a[i][j]) | bs(f[i - 1][j], -b[i][j] + a[i][j]); } for (int i = 0; i <= 6400; ++i) { if (f[H - 1][W - 1][i0 + i] || f[H - 1][W - 1][i0 - i]) { PRI(i); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for(int i = a; i < b; i++) #define rrep(i, a, b) for(int i = b - 1; i >= a; i--) int main(){ int H, W; cin >> H >> W; vector<vector<int> > A(H+1, vector<int>(W+1)); rep(i , 1, H+1) rep(j, 1, W+1) cin >> A[i][j]; rep(i , 1, H+1) rep(j, 1, W+1) {int t;cin >> t; A[i][j] = abs(A[i][j] - t);} bitset<12801> dp[81] = {}; dp[0][6400] = 1; rep(i, 1, H+1){ rep(j,1,W+1){ int a = A[i][j]; dp[j] = ((dp[j-1] | dp[j]) << a) | ((dp[j-1] | dp[j]) >> a); } dp[0][6400]=0; } rep(j , 0, 81){ //cerr << dp[W][6400 + j] << endl; if(dp[W][6400 + j]){cout << j << endl; return 0;} } }
1
#include <stdio.h> int main() { int h1, h2, m1, m2, k, ans; scanf("%d%d%d%d%d", &h1, &m1, &h2, &m2, &k); ans = (h2 - h1) * 60 + (m2 - m1) - k; printf("%d\n", ans); }
// Bismillahirrahmanirrahim // by Batrrr Tolkynbayev #include <bits/stdc++.h> // skripi krasava #define respa return 0 #define all(x) x.begin(), x.end() #define sz size #define pb push_back #define eb emplace_back using namespace std; typedef long long ll; typedef long double ld; const int N = 2e5 + 17, M = N << 2, mod = 1e9 + 7; int tt = 1, n; string s, t; void solve () { cin >> n >> s >> t; for (int i = 0; i < n; i++) { string f = s.substr(i, n - i); string h = t.substr(0, n - i); if (f == h) { cout << n + i; return; } } cout << (n << 1); } int main () { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cin >> tt; while (tt--) solve(); respa; }
0
#include<iostream> #include<string> #include<algorithm> using namespace std; static const int N = 1000; int Edit_Distance(string s1,string s2){ int dp[N + 1][N + 1] = {}; int m,n; m = s1.size(); n = s2.size(); for(int i = 0;i <= m;i++){ dp[i][0] = i; } for(int i = 0;i <= n;i++){ dp[0][i] = i; } for(int i = 1;i <= m;i++){ for(int j = 1;j <= n;j++){ int c; if(s1[i - 1] == s2[j - 1]) c = 0; else c = 1; int m = min(dp[i - 1][j] + 1,dp[i - 1][j - 1] + c); dp[i][j] = min(m,dp[i][j - 1] + 1); } } return dp[m][n]; } int main(){ string s1,s2; cin >> s1 >> s2; cout << Edit_Distance(s1,s2) << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long ll; typedef pair<ll,ll> P; const ll mod=1000000007; const ll LINF=1LL<<60; const int INF=1<<30; vector<vector<bool>> used(500,vector<bool>(500,false)); P bfs(int N,int M,vector<string> field,int sx,int sy){ int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; queue<P> que; que.push(P(sx,sy)); used[sx][sy] = true; P ans = mp(0,0); if (field[sx][sy] == '.'){ ans.fs++; } else{ ans.sc++; } //キューが空になるまでループ while(que.size()){ P p = que.front(); que.pop();//先頭を取り出す for (int i = 0;i < 4;i++){ int nx = p.fs + dx[i], ny = p.sc + dy[i]; //nx,nyが進めるマスか、行ったことのないマスかどうかを判定 if (field[p.fs][p.sc] == '.'){ if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '.' && !used[nx][ny]){ que.push(P(nx,ny));//追加 used[nx][ny] = true; ans.sc++; } } else{ if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' && !used[nx][ny]){ que.push(P(nx,ny));//追加 used[nx][ny] = true; ans.fs++; } } } } return ans; } int main(){ int h,w;cin>>h>>w; vector<string> s(h); for(int i = 0; i < h; i++) { cin>>s[i]; } ll ans = 0; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if (!used[i][j]){ P res = bfs(h,w,s,i,j); ans += (res.fs * res.sc); } } } cout << ans << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; #define ll long long ll cl[1000000][2]; int main(void){ ll i,len,lv,cnt,p,pp,x; char s[1000001]; cin>>s; len=strlen(s); for(i=0;i<len;i++) cl[i][1]=0; cnt=1; p=0; pp=0; if(s[0]=='J') cl[0][0]=0; else if(s[0]=='O') cl[0][0]=1; else cl[0][0]=2; for(i=0;i<len;i++){ if(s[pp]!=s[i]){ p++; pp=i; if(s[pp]=='J') cl[p][0]=0; else if(s[pp]=='O') cl[p][0]=1; else cl[p][0]=2; } cl[p][1]++; } /* for(i=0;i<=p;i++) cout<<cl[i][1]<<' '; cout<<endl;*/ lv=0; for(i=1;i<len-1;i++){ if(cl[i][0]==1){ x=cl[i][1]; if(cl[i-1][0]==0&&cl[i+1][0]==2){ if(cl[i-1][1]>=x&&cl[i+1][1]>=x) lv=max(lv,x); } } } cout<<lv<<endl; return 0; }
#include <cstdio> #include <string> #include <algorithm> using namespace std; string s; char t[1000001]; int ans; int main(){ scanf("%s", t); s = t; for(int i=0; i<s.size(); i++){ int J, O, I; if(s[i] == 'J'){ J = 0; while(s[i] == 'J'){ i++; J++; } if(s[i] == 'O'){ O = 0; while(s[i] == 'O'){ i++; O++; } if(s[i] == 'I'){ I = 0; while(s[i] == 'I'){ i++; I++; } if(O <= J && O <= I) ans = max(ans, O); } } i--; } } printf("%d\n", ans); return 0; }
1
//--------------------------------------------------------------- #include <bits/stdc++.h> using namespace std; #include <iostream> #include <vector> #include <cstdlib> #include <cmath> #include <math.h> #include <sstream> #include <numeric> #include <cctype> #include <bitset> #include <cassert> #include<algorithm> //--------------------------------------------------------------- #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 rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define SIZE 100005 #define INF 1000000000000000LL #define all(x) x.begin(),x.end() using pint = pair<int,int>; using vec = vector<int>; using ll=long long; using vll = vector<ll>; using vvll = vector<vector<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 int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; //--------------------------------------------------------------- //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n;cin>>n; string s; cin>>s; ll r=0,g=0,b=0; rep(i,n){ if(s[i]=='R') r++; if(s[i]=='G') g++; if(s[i]=='B') b++; } ll ans=r*b*g; ll sub=0; rep(i,n)for(int j=i; j<n; j++){ if(s[i]==s[j])continue; int k = j*2 - i; if(k>=n || s[j]==s[k] || s[k]==s[i])continue; sub++; } cout<<ans - sub<<endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n)for(long long i=0;i<(n);i++) using namespace std; typedef unsigned long long ull; typedef long long ll; const int INF=1e9; const int MOD=1e9+7; const double pi=acos(-1); int main(){ int n; cin >> n; ll s[200010]; s[0]=0; rep(i,n){ ll a; cin >> a; s[i+1]=s[i]+a; } ll ans=1e18; for(int i=2;i<n-1;i++){ ll *l,*r; ll gap; l=lower_bound(s+1,s+i+1,s[i]/2+1); r=lower_bound(s+i+1,s+n+1,(s[n]+s[i])/2+1); rep(j,2){ rep(k,2){ gap=max({*l,s[i]-*l,*r-s[i],s[n]-*r})-min({*l,s[i]-*l,*r-s[i],s[n]-*r}); ans=min(ans,gap); l--; } l+=2; r--; } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N, M, Q; cin >> N >> M >> Q; vector<vector<int>> cnt_vec(N + 1, vector<int>(N + 1, 0)); int L, R; for (int i = 0; i < M; ++i) { cin >> L >> R; cnt_vec.at(L).at(R) += 1; } vector<vector<int>> cumsum(N + 1, vector<int>(N + 1, 0)); for (int i = N; i > 0; --i) { for (int j = 1; j <= N; ++j) { cumsum.at(i).at(j) = cnt_vec.at(i).at(j); cumsum.at(i).at(j) += cumsum.at(i).at(j - 1); if (i < N) { cumsum.at(i).at(j) += cumsum.at(i + 1).at(j); cumsum.at(i).at(j) -= cumsum.at(i + 1).at(j - 1); } } } int p, q; for (int i = 0; i < Q; ++i) { cin >> p >> q; cout << cumsum.at(p).at(q) << endl; } }
#include <cstdio> using namespace std; int n,d1,d2,s; int f[620][620]; void work(int d){ int p=0; while(d%4==0){ d/=4; p++; } if(d&1){ for(int i=0;i<2*n;i++) for (int j=0;j<2*n;j++) if(((i>>p)+(j>>p))&1) f[i][j]=1; }else{ for(int i=0;i<2*n;i++) for(int j=0;j<2*n;j++) if((i>>p)&1) f[i][j]=1; } } int main(){ scanf("%d%d%d",&n,&d1,&d2); work(d1); work(d2); for(int i=0;i<2*n;i++){ for(int j=0;j<2*n;j++){ if(s<n*n && !f[i][j]){ printf("%d %d\n",i,j); s++; } } } return 0; }
0
#include <iostream> #include <climits> using namespace std; #define int long long const int INF=1e18; const int MAX_V=2000; int v,g[MAX_V][MAX_V],r; void warshall(){ for(int k=0;k<v;k++) for(int i=0;i<v;i++) for(int j=0;j<v;j++) g[i][j]=min(g[i][j],g[i][k]+g[k][j]); } signed main(){ int e; cin>>v>>e; for(int i=0;i<v;i++) for(int j=0;j<v;j++) g[i][j]=i==j?0:INF; for(int i=0,a,b,c;i<e&&cin>>a>>b>>c;i++) g[a][b]=c; warshall(); //for(int i=0;i<v;i++) for(int j=0;j<v;j++) if(INF/2<g[i][j]&&g[i][j]<INF) r++; for(int i=0;i<v;i++) if(g[i][i]) r++; if(r){ cout<<"NEGATIVE CYCLE"<<endl; return 0; } for(int i=0;i<v;i++){ for(int j=0;j<v;j++){ if(j) cout<<" "; if(g[i][j]>=INF/2) cout<<"INF"; else cout<<g[i][j]; } cout<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; using vll = vector<vl>; using Pll = pair<ll, ll>; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(v) v.begin(), v.end() #define sz(x) ((int) x.size()) #define pb push_back #define mp make_pair #define mt make_tuple #define F first #define S second const int MOD = 1e9+7; const ll INF = 2e15; template<class T> void print(const T& t){ cout << t << 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; } int gcd(int a,int b){return b?gcd(b,a%b):a;} int main(){ ll n; cin >> n; string s,t; cin>>s>>t; ll count = 0; t.pb('1'); for (ll i = n; i > 0; i--){ string a = s.substr(n - i); string b = t; b.erase(b.begin() + i,b.begin()+n+1); if (a == b) { count = i; break; } } print(2 * n - count); }
0
#include <iostream> #include <climits> #include <vector> #include <cstring> using namespace std; #define V_MAX 10040 int N,E; int INF = INT_MAX/4; struct edge { int to,cap,rev; edge(int _to, int _cap, int _rev) {to = _to; cap=_cap; rev=_rev;} }; vector<edge> G[V_MAX]; bool used[V_MAX]; void add_edge(int from, int to, int cap){ G[from].push_back(edge(to,cap,G[to].size())); G[to].push_back(edge(from,0,G[from].size()-1)); } int dfs(int v, int t, int f){ if(v==t) return f; used[v]=true; for(int i=0;i<G[v].size();i++){ edge &e = G[v][i]; if(!used[e.to] && e.cap>0){ int d = dfs(e.to, t, min(f,e.cap)); if(d>0){ e.cap-=d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t){ int flow = 0; for(;;){ memset(used,0,sizeof(used)); int f = dfs(s,t,INF); if(f==0) return flow; flow += f; } } int main(){ cin>>N; int s, t; s = 2 * N; t = s + 1; vector<pair<int,int>> r(N); for(int i=0; i<N; i++){ cin >> r[i].first >> r[i].second; add_edge(s, i, 1); } vector<pair<int,int>> b(N); for(int i=0; i<N; i++){ cin >> b[i].first >> b[i].second; add_edge(N+i, t, 1); } for(int i=0; i<N; i++){ for(int j=0; j<N; j++){ if (r[i].first < b[j].first && r[i].second < b[j].second){ add_edge(i, N+j, 1); } } } cout << max_flow(s, t) << endl; return 0; }
#include <algorithm> // #include <cstdlib> #include <cstring> #include <cstdio> #include <string> #include <queue> #include <cmath> #include <set> #include <map> #include <bitset> using namespace std; typedef long long LL; typedef double db; const int oo=2139063143; const int N=1010000; const int P=1000000007; const db eps=1e-7; #define pritnf printf //char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf; //#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) template <typename T> inline void sc (T &x) { x=0; static int p; p=1; static char c; c=getchar(); while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); } while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); } x*=p; } template <typename T> inline void print (T x) { if (x< 0) putchar('-'),x=-x; if (x>=10) print(x/10); putchar(x%10+'0'); } template <typename T> inline void pr (T x) { print(x),putchar('\n'); } template <typename T1,typename T2> void chkmax (T1 &A,T2 B) { A=A>=B?A:B; } template <typename T1,typename T2> void chkmin (T1 &A,T2 B) { A=A<=B?A:B; } int pri[N]; bool vis[N]; void init (int n) { for (int i=2; ; i++) { if (!vis[i]) pri[++pri[0]]=i; if (pri[0]==n) break; for (int j=1; j<=pri[0]; j++) { int p=pri[j],k=i*p; if (k> 10000) break; vis[k]=1; if (i%p==0) break; } } } LL lcm (LL x,LL y) { return (!x||!y)?(x+y):(x/__gcd (x,y)*y); } int n; LL a[1010][1010]; int main () { // freopen (".in","r",stdin); // freopen (".out","w",stdout); sc(n); init (1000); if (n==2) return puts("4 7\n23 10"),0; for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if ((i+j+1)&1) a[i][j]=pri[(i+j)/2]*pri[n+(i-j)/2+(n+1)/2]; for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (!a[i][j]) a[i][j]=lcm (lcm (a[i-1][j],a[i][j-1]),lcm (a[i+1][j],a[i][j+1]))+1; for (int i=1; i<=n; i++,putchar('\n')) for (int j=1; j<=n; j++,putchar(' ')) print(a[i][j]); return 0; }
0
// darksta5's template v2.0 #include <bits/stdc++.h> #define ll long long using namespace std; class DisjointSet { private: int _sz, _numSets; vector<int> _par, _setSz, _rank; public: DisjointSet(int sz) { _sz = sz; _numSets = sz; _setSz.assign(sz, 1); _rank.assign(sz, 0); _par.assign(sz, 0); for (int i = 0; i < sz; i++) { _par[i] = i; } } int sz() { return _sz; } int numSets() { return _numSets; } int setSz(int i) { return _setSz[find(i)]; } int find(int i) { int x = i; while (x != _par[x]) { x = _par[x]; } return _par[i] = x; } bool isSame(int i, int j) { return find(i) == find(j); } void uni(int i, int j) { if (!isSame(i, j)) { _numSets--; int x = find(i); int y = find(j); if (_rank[x] < _rank[y]) { _par[x] = y; _setSz[y] += _setSz[x]; } else { _par[y] = x; _setSz[x] += _setSz[y]; if (_rank[x] == _rank[y]) { _rank[x]++; } } } } }; ll make_key(int x, int y) { return (ll)x * 200050LL + (ll)y; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, r1, r2; cin >> n >> r1 >> r2; DisjointSet ds1(n), ds2(n); for (int i = 0; i < r1; i++) { int x, y; cin >> x >> y; ds1.uni(x - 1, y - 1); } for (int i = 0; i < r2; i++) { int x, y; cin >> x >> y; ds2.uni(x - 1, y - 1); } unordered_map<ll, int> cnt; for (int i = 0; i < n; i++) { ll key = make_key(ds1.find(i), ds2.find(i)); cnt[key]++; } for (int i = 0; i < n; i++) { ll key = make_key(ds1.find(i), ds2.find(i)); if (i < n - 1) { cout << cnt[key] << " "; } else { cout << cnt[key] << endl; } } }
#include<bits/stdc++.h> #define rep(i,n) for (int i=0; i<n; i++) #define REP(i,x,n) for (int i=x; i<n; i++) using namespace std; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; /*int main(){ int n, m; cin >> n >> m; vi vec(n); vi num(n); rep(i,m){ int si, ci; cin >> si >> ci; si -= 1; if (vec.at(si) != ci) num.at(si)++; vec.at(si) = ci; } if (num.at(0) != 1 ){ cout << -1 << endl; return 0; } REP(i,1,n){ if (num.at(i) > 1){ cout << -1 << endl; return 0; } } rep(i,n){ cout << vec.at(i); } cout << endl; return 0; }*/ int main(){ int n, m; cin >> n >> m; vector<pair<int, int>> p(m); rep(i,m) cin >> p.at(i).first >> p.at(i).second; rep(i,1000){ vi d(1, i%10); int nx = i / 10; while (nx){ d.push_back(nx%10); nx /= 10; } if ((int)d.size() != n) continue; reverse(d.begin(), d.end()); bool ok = true; rep(i,m){ if (d[p.at(i).first-1] != p.at(i).second) ok = false; } if (ok){ cout << i << endl; return 0; } } cout << -1 << endl; return 0; }
0
#include<iostream> #include<algorithm> using namespace std; int main() { int n, q; while (cin >> n >> q, n, q) { int mem[110] = {}; int maxv = -1; for (int i = 0; i < n; i++){ int m; cin >> m; for (int j = 0; j < m; j++) { int d; cin >> d; mem[d]++; maxv = max(maxv, d); } } int ans = 0, num = -1; for (int i = 0; i <= maxv; i++) { if (ans == 0) { if (mem[i] >= q) { ans = i; num = mem[i]; } } else if (mem[i] > num) { ans = i; num = mem[i]; } } cout << ans << endl; } }
#include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; int main (int argc, char *argv[]) { int N; cin >> N; cin.ignore(); for(int i = 0; i < N; ++i) { string str; int out_count = 0, base[3] = {0, 0, 0}, point = 0; while (true) { getline(cin,str); if (str == "OUT") out_count++; else if (str == "HIT") { if (base[2] == 1) { point++; base[2] = 0; } base[2] = base[1]; base[1] = base[0]; base[0] = 1; } else if (str == "HOMERUN") { point += 1 + count(base,base+3,1); memset(base, 0, sizeof base); } if (out_count == 3) break; } cout << point << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, r; cin >> n >> r; n = min(n, 10); cout << r + 100 * (10-n) << endl; return 0; }
#include <iostream> #include <vector> using namespace std; void abc_150a(); void abc_151a(); void abc_152a(); void abc_154a(); void abc_156a(); int main() { //abc_150a(); //abc_151a(); //abc_152a(); //abc_154a(); abc_156a(); } void abc_150a() { int K, X; cin >> K >> X; if (500 * K >= X) cout << "Yes\n"; else cout << "No\n"; } void abc_151a() { char C; cin >> C; cout << (char)((int)C + 1) << "\n"; } void abc_152a() { int N, M; cin >> N >> M; if (N == M) cout << "Yes\n"; else cout << "No\n"; } void abc_154a() { string S, T; cin >> S >> T; int a, b; cin >> a >> b; string U; cin >> U; if (S == U) cout << a - 1 << " " << b << "\n"; else cout << a << " " << b - 1 << "\n"; } void abc_156a() { int N, R; cin >> N >> R; if (N >= 10) cout << R << "\n"; else cout << R + 100 * (10 - N) << "\n"; }
1
#include <iostream> #include <vector> using namespace std; int main() { uint N; cin >> N; vector<int> A(N, 0); for (auto &a: A) cin >> a; int cnt = 0; bool exist = false; while (!exist) { for (size_t i=0; i<A.size(); ++i) { if (A[i] % 2 != 0) { exist = true; break; } A[i] /= 2; } if (!exist) ++cnt; } cout << cnt << endl; }
/* ID: anonymo14 TASK: wormhole LANG: C++ */ #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pair<int,int>> vpii; #define F first #define S second #define PU push #define PUF push_front #define PUB push_back #define PO pop #define POF pop_front #define POB pop_back #define REP(i,a,b) for(int i=a; i<=b; i++) #define MOD 1000000007 int me(int val) { for(int i=1;;i<<=1) { if(i&val)return log2(i); } return log2(val); } void solve(int test_case) {//227 int n; cin>>n; int mi = 64; REP(i,1,n) { int val; cin>>val; mi=min(mi,me(val)); } cout<<mi; } int main() { ////// FILE BASED IO//// //freopen("wormhole.in", "r", stdin); //freopen("wormhole.out", "w", stdout); /////////////// ios::sync_with_stdio(0); cin.tie(0); int t=1; //cin>>t; REP(i,1,t) { solve(i); } return 0; }
1
#include <bits/stdc++.h> using namespace std; //#define int long long #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define yn(x) ((x) ? "Yes" : "No") typedef pair <int, int> ii; typedef pair <int, ii> iii; const int siz = 80 + 1; const int SIZ = 2 * 6400 + 1; const int mod = 1e9 + 7; const int maxx = 2e9; const int MAXX = 1e18; const string file = "1"; int a[siz][siz]; bool f[siz][siz][SIZ]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen ((file + ".inp").c_str(), "r", stdin); // freopen ((file + ".out").c_str(), "w", stdout); int m, n; cin >> m >> n; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { int x; cin >> x; a[i][j] = abs(a[i][j] - x); } } f[1][1][a[1][1]] = true; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k < SIZ; k++) { if (f[i][j][k] == true) { if (i < m) { f[i + 1][j][abs(k - a[i + 1][j])] = true; f[i + 1][j][k + a[i + 1][j]] = true; } if (j < n) { f[i][j + 1][abs(k - a[i][j + 1])] = true; f[i][j + 1][k + a[i][j + 1]] = true; } } } } } for (int k = 0; k < SIZ; k++) { if (f[m][n][k] == true) { return cout << k << "\n", 0; } } // cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> #define rp(i,n) for(int i=0; i<n; i++) using namespace std; using ll=long long; using P=pair<int,int>; using G=vector<vector<int>>; bool dp[6410][20000]; int abs(int x,int y){ if(x>=y) return x-y; else return y-x; } int main(){ int h,w; cin >> h >> w; vector<int> a(h*w), b(h*w); rp(i,h*w) cin >> a[i]; rp(i,h*w) cin >> b[i]; rp(i,6410){ rp(j,20000) dp[i][j]=false; } int m=0; rp(i,h*w) m=max(m,abs(a[i],b[i])); dp[0][abs(a[0],b[0])]=true; for(int i=1; i<h*w; i++){ int dif=abs(a[i],b[i]); if(i%w){ for(int k=0; k<=(h+w)*m;k++){ if(dp[i-1][k]){ dp[i][abs(k,dif)]=true; if(k+dif<=(h+w)*m*2) dp[i][k+dif]=true; } } } if(i>=w){ for(int k=0; k<=(h+w)*m;k++){ if(dp[i-w][k]){ dp[i][abs(k,dif)]=true; if(k+dif<=(h+w)*m*2) dp[i][k+dif]=true; } } } } int ans; int c=0; while(1){ if(dp[h*w-1][c]){ ans=c; break; } c++; } cout << ans << endl; return 0; }
1
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int waseda[10]; int keo[10]; for(int i = 0; i < 10; i++){ cin >> waseda[i]; } for(int i = 0; i < 10; i++){ cin >> keo[i]; } sort(waseda,waseda+10,greater<int>()); sort(keo,keo+10,greater<int>()); int sumW=0; int sumK=0; for(int i = 0; i < 3; i++){ sumW += waseda[i]; sumK += keo[i]; } cout << sumW << " " << sumK << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(x, n) for (int x = 0; x < n; x++) #define REP(x, a, b) for (int x = a; x <= b; x++) #define all(container) (container).begin(), container.end() #define rall(container) (container).begin(), container.end() #define sz(container) (int) (container).size() using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #ifdef LOCAL_DEFINE #include "DEBUG.hpp" #else #define debug(...) 0 #endif template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } int main(void) { int n,m; scanf("%d%d",&n,&m); if(m&1){ int end=2*m+1; REP(i,m+1,3*m/2+1) printf("%d %d\n",i,end-i+m+1); end=m; REP(i,1,m/2) printf("%d %d\n",i,end-i+1); }else{ int end=2*m+1; REP(i,m+2,3*m/2+1) printf("%d %d\n",i,end-i+m+2); end=m+1; REP(i,1,m/2) printf("%d %d\n",i,end-i+1); } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(void){ int N; cin>>N; vector<char> S(N); for(int i=0;i<N;i++){ cin>>S[i]; } int ans=0; vector<int> E(N+1); vector<int> W(N+1); E[0]=0; W[0]=0; for(int i=0;i<N;i++){ if(S[i]=='E'){ E[i+1]=E[i]+1; }else{ E[i+1]=E[i]; } if(S[i]=='W'){ W[i+1]=W[i]+1; }else{ W[i+1]=W[i]; } } ans=N; for(int i=0;i<N;i++){ int result=W[i+1]+E[N]-E[i]-1; ans=min(result,ans); } cout<<ans<<endl; return 0; }
#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; typedef pair<ll,ll> P; #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 cnt=0,ans=INF; string s; cin>>s; //EEEEEEEEEEEEEEEEE rep(i,sz(s)){ if(s[i]=='W') cnt++; } ans=cnt; rrep(i,sz(s)){ if(s[i]=='W') cnt--; else cnt++; chmin(ans,cnt); } cout<<ans<<endl; }
1
/* / \ (Hello)//JSM//*/ /* /*****\ */ /* Jai Shree Mataji / \ |\| |_| $ |-| K */ #include<vector> #include<iostream> #include<algorithm> #include<string.h> #include<bits/stdc++.h> #include<cstring> #include<map> #include<cmath> #include<set> using namespace std; #define ll long long int #define ld long double #define MOD 1000000007 #define PI 3.1415926535897932384626433832795 #define YY cout<<"YES" #define NN cout<<"NO" #define EE cout<<"\n" #define ee cout<<"\n" #define ne cout<<"-1" #define pb push_back #define fi first #define se second #define mkp make_pair #define mkt make_tuple #define sall(v ) sort(v.begin(), v.end()) #define all(v ) v.begin(), v.end() #define fo(i , n) for(ll i = 0 ; i < n ; i++) #define vl vector<ll> #define vc vector<char> #define vll vector< pair<ll,ll> > #define mps map<string ,ll> #define mpc map<char ,ll> #define mpl map<ll, ll> #define pr pair<ll,ll> #define tpl tuple<ll,ll,ll> ll containsDigit(ll number, ll digit){while (number != 0){int curr_digit = number % 10;if (curr_digit == digit) return 1;number /= 10;}return 0;} ll soltn(ll x, ll n) {if(x>=0) return x%n; else return n+x%n;} ll pd(ll x,ll y){if(x%y==0) return x/y;else return x/y+1;} ll coda(ll n,ll d){ll count=0;while(n!=0){if(n%10==d)count++;n/=10;}return count;} ll mdls(ll x){return max(x,-x);} ll pow1(ll n,ll p){if(p==0)return 1;ll x=pow1(n, p/2);x=(x%MOD*x%MOD)%MOD;if(p%2==0) return x;else return (x*n)%MOD;} ll isSubstr(string s1, string s2) { ll M = s1.length(); ll N = s2.length(); for (ll i = 0; i <= N - M; i++) { ll j; for (j = 0; j < M; j++) if (s2[i + j] != s1[j]) break; if (j == M) return i; } return -1; } //s2>s1;if(not) rtrn -1; ll maxSubArraySum(ll a[], ll size) { ll max_so_far = a[0]; ll curr_max = a[0]; for (ll i = 1; i < size; i++) { curr_max = max(a[i], curr_max+a[i]); max_so_far = max(max_so_far, curr_max); } return max_so_far; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll tt=1; // cin>>tt; while(tt--) { /* 3 2 3*/ ll n,j,i; ll a[3]; fo(i,3) cin>>a[i]; sort(a,a+3); cout<<a[0]+a[1]; ee; } return 0; }
#include <iostream> #include <vector> using namespace std; int main(){ int A; int B; int C; cin >> A; cin >> B; cin>> C; vector <int> seged_vector; seged_vector.push_back(A+B); seged_vector.push_back(A+C); seged_vector.push_back(B+A); seged_vector.push_back(B+C); seged_vector.push_back(C+A); seged_vector.push_back(C+B); int seged=seged_vector[0]; for(int i=1;i<seged_vector.size();i++){ if(seged>seged_vector[i]){ seged=seged_vector[i]; } } cout << seged; return 0; }
1
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<vector> #include<string> #include<algorithm> #include<map> using namespace std; #define rep(i,x) for(ll i = 0; i < (ll)(x); i++) #define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--) #define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++) #define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--) #define debug(x) cerr << #x << ": " << (x) << "\n"; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int,int> P; typedef pair<ll,ll> Pll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; const ll INF = numeric_limits<ll>::max()/4; const int n_max = 1e5+10; void print() { cout << endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << ' '; print(forward<Tail>(tail)...); } template <class T> void print(vector<T> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << '\n'; } cout << endl; } template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec); } } template<class T, class U> void print(pair<T,U> &p){ print(p.first, p.second); } int main(){ ll n,m; cin >> n >> m; vector<vector<ll>> graph(n); vector<ll> ans(n, INF), in(n, 0); rep(i,n - 1 + m) { ll a,b;cin >> a >> b; a--;b--; graph[a].emplace_back(b); // graph[b].emplace_back(a); in[b]++; } ll root; rep(i,n)if(in[i] == 0){ root = i; ans[root] = 0; } // print(in); queue<Pll> que; que.push(Pll(root, 0)); while(!que.empty()){ Pll t = que.front();que.pop(); for(auto to : graph[t.first]){ if(in[to] == 1){ ans[to] = t.first + 1; que.push(Pll(to, t.second + 1)); } in[to]--; } } print(ans); }
#include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <string> #include <vector> #include <queue> #include <cmath> #include <stack> #include <set> #include <map> typedef long long ll; using namespace std; class Graph { public: int N; vector<vector<int>> G; vector<bool> visit; vector<int> cir; vector<int> exist; Graph(int N) : N(N) { G.resize(N); visit.resize(N, false); exist.resize(N, -1); } void add_edge(int s, int t){ G[s].push_back(t); } bool dfs(int n, int s){ if(n == s && visit[n]){ return true; } if(visit[n]){ return false; } visit[n] = true; for(int i = 0; i < (int)G[n].size(); i++){ int nx = G[n][i]; if(dfs(nx, s)){ cir.push_back(n); return true; } } return false; } void solve(){ //0 ~ N-1について自身に戻れるかdfsをN回 bool found = false; //閉路があるか for(int i = 0; i < N; i++){ fill(visit.begin(), visit.end(), false); if(dfs(i, i)){ found = true; break; } } if(!found){ cout << -1 << endl; return; } reverse(cir.begin(), cir.end()); while(true){ //debug /* cout << "cir = "; for(int val : cir){ cout << val + 1 << " "; } cout << endl; */ //閉路上の点かどうかを示すexist更新 fill(exist.begin(), exist.end(), -1); for(int i = 0; i < (int)cir.size(); i++){ exist[cir[i]] = i; } cir.push_back(cir[0]); bool check = false; for(int i = 0; i < (int)cir.size() - 1; i++){ int n = cir[i]; for(int j = 0; j < (int)G[n].size(); j++){ int nx = G[n][j]; if(exist[nx] >= 0 && nx != cir[i + 1]){ //閉路作り直し check = true; vector<int> tmp; if(exist[n] < exist[nx]){ for(int k = 0; k < (int)cir.size() - 1; k++){ if(k <= exist[n] || exist[nx] <= k) tmp.push_back(cir[k]); } } else { //cout << "hoge" << endl; for(int k = exist[nx]; k <= exist[n]; k++){ tmp.push_back(cir[k]); } } cir = tmp; break; } } if(check) break; } if(!check) break; } cout << cir.size() - 1 << endl; for(int i = 0; i < (int)cir.size() - 1; i++){ cout << cir[i] + 1 << endl; } return; } }; int main(){ int N, M; cin >> N >> M; Graph gr = Graph(N); for(int i = 0; i < M; i++){ int a, b; cin >> a >> b; gr.add_edge(a - 1, b - 1); } gr.solve(); return 0; }
0
//ALDS1_13_A #include <iostream> #include <vector> using namespace std; void printChess(int chess[]){ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(j == chess[i]) cout<<"Q"; else cout<<"."; } cout<<endl; } } bool isBlank(int chess[], int &r){ for(int i=0;i<8;i++){ if(chess[i] == 100){ r = i; return true; } } return false; } bool setQ(int chess[], int r, int c){ for(int i=0;i<8;i++){ if(chess[i] == c) return false; if(i+chess[i] == r+c) return false; if(i-chess[i] == r-c) return false; } return true; } void solve(int chess[]){ int r; if(isBlank(chess, r)){ for(int c=0;c<8;c++){ if(setQ(chess, r, c)){ chess[r] = c; solve(chess); chess[r] = 100; } } }else{ printChess(chess); } } int main(){ int k, r, c, chess[8]; for(int i=0;i<8;i++) chess[i] = 100; cin>>k; for(int i=0;i<k;i++){ cin>>r>>c; chess[r] = c; } solve(chess); return 0; }
// Phttps://ei1333.github.io/luzhiled/ // http://beet-aizu.hatenablog.com/entry/2017/01/04/113233 // http://www.prefield.com/algorithm/ #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vb = vector<bool>; using vvl = vector<vector<ll>>; using vvd = vector<vector<ld>>; using vvs = vector<vector<string>>; using vvb = vector<vector<bool>>; #define pb push_back #define eb emplace_back #define fi first #define se second template<class T> using vc = vector<T>; template<class T> using vvc = vector<vector<T>>; using Graph = vector<vector<ll>>; const ll INF = 1LL << 60; void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);} const ll tl= 1'000'000'007; #define REP(i, n) for (ll i = 0; i < n; i++) #define REREP(i, n) for (ll i = n; i >= 0; i--) #define FOR(i, a, n) for (ll i = a; i < n; i++) #define REFOR(i, n, a) for (ll i = n; i >= a; i--) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除 #define debug cout << "line : " << __LINE__ << " debug" << endl; #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ind(...) long double __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);} template<typename T>void die(T x){out(x); exit(0);} #define in1(A) REP(i,A.size()) in(A[i]); #define in2(A,B) REP(i,A.size()) in(A[i],B[i]); #define in3(s,t,u) REP(i,sz(s)){in(s[i] , t[i] , u[i]);} #define in4(s,t,u,v) REP(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);} #define each(x,v) for(auto& x : v) #define all(v) (v).begin(),(v).end() #define sz(v) ((int)(v).size()) struct Point{ ld x,y; }; ld dist(Point a, Point b){return sqrt(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y));} // 2点間の距離 ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} ll fact(ll n){ if(n < 2) return 1; return (n * fact(n-1))%tl; } //階乗 inline ll updiv(ll a,ll b){ return (a + b - 1) / b; } //切り上げ template<typename T,typename U>ll ceil(T a,U b){return (a + b - 1) / b;} template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } //------------------------------------------------------------------------------------------------- ll n=8; vvl bd(n,vl(n,0)); vvl memo(n); //-1,0,1 vector<pair<ll,ll>>A; void queen_dfs(int cnt, int p,vvl bd, vvl old_memo){ bd[cnt][p]=1; if(cnt==n-1){ int f=1; each(x,A){ if(!bd[x.first][x.second]) f=0; } if(f){ REP(i,n){ REP(j,n){ cout << ((bd[i][j])?"Q":"."); }out(); } } return; } //memo 更新 vvl new_memo(n); REP(j,n){ REP(t,old_memo[j].size()){ ll x= j+old_memo[j][t]; if(0<=x && x<n) new_memo[x].pb(old_memo[j][t]); } } // bd REP(i,n){ if(!new_memo[i].size()){ vvl n_memo= new_memo; n_memo[i].pb(-1); n_memo[i].pb(0); n_memo[i].pb(1); queen_dfs(cnt+1, i, bd, n_memo); } } } int main(){ init(); inl(k); A.resize(k); REP(i,k){ inl(r,c); A[i]= {r,c}; } REP(i,n){ vvl memo(8); memo[i].pb(-1); memo[i].pb(0); memo[i].pb(1); queen_dfs(0,i,bd,memo); } }
1
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; int main(){ int n, l; cin >> n >> l; vector<int> a(n); rep(i, n) a.at(i) = l + i; sort(ALL(a), [](const auto &l1, const auto &l2){ return abs(l1) < abs(l2); }); int ans = 0; rep2(i, 1, n) ans += a.at(i); cout << ans << endl; return 0; }
#include<iostream> #include<iomanip> #include<string> #include<algorithm> #include<vector> #include<utility> #include<tuple> #include<map> #include<queue> #include<stack> #include<deque> #include<bitset> #include<math.h> using namespace std; using ll = int64_t; using Graph = vector<vector<int> >; const ll M = 1000000007; int main(){ int n,l; cin >> n >> l; int a[n]; for(int i=0;i<n;i++){ a[i] = l+i; } int c=1; if(a[0]<0) c=0; int m = abs(a[0]); for(int i=0;i<n;i++){ if(m>abs(a[i])){ if(a[i]>=0){ c=1; }else{ c=0; } m = abs(a[i]); } } int sum =0; for(int i=0;i<n;i++){ sum += a[i]; } if(c){ cout << sum-m << endl; }else{ cout << sum+m << endl; } }
1
#include<bits/stdc++.h> using namespace std; const int N=305; #define fi first #define se second typedef pair<int,int>P; priority_queue<P>que; int n,m,b[N],vi[N],cnt[N],a[N][N]; bool check(int mid){ while(!que.empty()) que.pop(); for(int i=1;i<=m;i++) b[i]=0; for(int i=1;i<=n;i++) ++b[a[i][cnt[i]=1]]; for(int i=1;i<=m;i++) vi[i]=0,que.push(P(b[i],i)); while(!que.empty()){ P x=que.top(); que.pop(); if(vi[x.se]) continue; if(x.fi<=mid) return true; vi[x.se]=1; for(int i=1;i<=n;i++){ if(a[i][cnt[i]]!=x.se) continue; while(vi[a[i][cnt[i]]]&&cnt[i]<=m) ++cnt[i]; if(cnt[i]>m) return false; que.push(P(++b[a[i][cnt[i]]],a[i][cnt[i]])); } } return false; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); int l=1,r=n; while(l<r){ int mid=(l+r)>>1; if(check(mid)) r=mid; else l=++mid; } printf("%d\n",l); return 0; }
#include <iostream> #include <vector> #include <queue> #include <map> #include <set> using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;} template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;} int main() { int N, M; cin >> N >> M; vector<queue<int>> A(N, queue<int>()); rep(i, N) rep(j, M) { int a; cin >> a; A[i].push(a); } set<int> used; int ans = N; rep(i, M) { map<int, int> count; rep(j, N) { while (!A[j].empty() && used.find(A[j].front()) != used.end()) A[j].pop(); ++count[A[j].front()]; } int max_count = 0, max_num; for (const auto& e : count) { int num = e.first, count = e.second; if (chmax(max_count, count)) max_num = num; } chmin(ans, max_count); used.insert(max_num); } cout << ans << endl; return 0; }
1
// J'aime // Chemise Blanche #include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int,int> #define fi first #define sc second #define all(x) (x).begin(),(x).end() #define dbg(x) cerr << __LINE__ << " > " << #x << " = " << (x) << endl void MAIN() { int n, y; cin >> n >> y; for (int i = 0; i <= n; i++) for (int j = 0; j <= n - i; j++) { int k = n - i - j; if (10000 * i + 5000 * j + 1000 * k == y) { cout << i << ' ' << j << ' ' << k << '\n'; return; } } cout << "-1 -1 -1" << '\n'; } signed main() { #ifdef _DEBUG // freopen("in" , "r", stdin ); // freopen("out", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while (T--) MAIN(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll INF = 100000000; #define rep1(i, n) for (ll i=0; i<(n); i++) #define rep2(i, k, n) for (ll i=k; 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; } ll n, y; ll count(ll mon){ ll cnt = y / mon; y %= mon; return cnt; } int main() { cin >> n >> y; vector<ll> a(3, 0); a[2] = count(10000); a[1] = count(5000); a[0] = count(1000); ll d = n - (a[0]+a[1]+a[2]); if (d<0){ cout << -1 << " " << -1 << " " << -1 << endl; return 0; } ll val = min(a[2], d/9); a[0] += val*10; a[2] -= val; d -= val*9; val = min(a[1], d/4); a[0] += val*5; a[1] -= val; d -= val*4; val = min(a[2], d); a[1] += val*2; a[2] -= val; d -= val; if (d!=0){ cout << -1 << " " << -1 << " " << -1 << endl; return 0; } cout << a[2] << " " << a[1] << " " << a[0] << endl; //printf("%.12f", ans); }
1
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0; int mini = 101; for (int i = 0; i < 4; ++i) { int s; cin >> s; sum += s; mini = min(s, mini); } int s1, s2; cin >> s1 >> s2; cout << sum - mini + max(s1, s2) << '\n'; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n) ;i++) using namespace std; using p=pair<int,int>; typedef long long ll; int main() { int n; cin >> n; vector<int>a(n); rep(i,n){ cin >> a[i]; } int k=1000000; int ans=0; rep(i,n){ k=min(k,a[i]); if(k==a[i]){ ans++; } } cout << ans << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; #define fo(i,a,n) for(i=a;i<n;i++) #define ll long long #define ld long double #define deb(x) cout << #x << "=" << x << endl #define pb push_back #define mp make_pair #define ff first #define ss second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define sortrev(x) sort(all(x),greater<int>()) #define pi 3.1415926535897932384626 typedef pair<int,int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; const int mod = 1000000007 ; const int inf = 100005; const int N = 3e5, M = N; ll i,j,n,m,idx,cnt; char d[31][31]; int dx[] = {-1,1,0,0}; int dy[] = {0,0,-1,1}; void solve() { ll n; cin>>n; vl vec(61); ll ans = 0,x; cin>>x; fo(i,0,61) { if(x & (1LL<<i)) vec[i]++; } ll k; fo(i,1,n) { cin>>x; fo(j,0,61) { if(x & (1LL<<j)) { k = i - vec[j]; ans = (ans%mod + ((1LL<<j)%mod * k%mod)%mod)%mod; vec[j]++; } else { k = vec[j]; ans = (ans%mod + ((1LL<<j)%mod * k%mod)%mod)%mod; } } } cout<<ans<<"\n"; } 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> using namespace std; #define rep(i,a,n) for (long long i=a;i<n;i++) #define sz(A) (int)(A.size()) #define all(x) (x).begin(),(x).end() #define get(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) typedef long long ll; typedef pair<int, int> PII; typedef vector<int> VI; void solve() { int n; cin >> n; map<int,set<int> > L, R, U, D; map<int, set<int> > L2, R2, U2, D2; map<int, set<int> > L3, R3, U3, D3; rep(i, 0, n) { int x, y; char dir; cin >> x >> y >> dir; if (dir == 'U') { U[x].insert(y); U2[x + y].insert(y); U3[x - y].insert(y); } if (dir == 'D') { D[x].insert(y); D2[x + y].insert(y); D3[x - y].insert(y); } if (dir == 'L') { L[y].insert(x); L2[x + y].insert(y); L3[x - y].insert(y); } if (dir == 'R') { R[y].insert(x); R2[x + y].insert(y); R3[x - y].insert(y); } } int dis = INT_MAX, dis2 = INT_MAX; for (auto m : U) { set<int> up = m.second; set<int> down = D[m.first]; for (auto y : up) { auto it = down.lower_bound(y); if (it == down.end()) { break; } else { dis = min(dis, (*it) - y); } } } for (auto m : R) { set<int> right = m.second; set<int> left = L[m.first]; for (auto x : right) { auto it = left.lower_bound(x); if (it == left.end()) { break; } else { dis = min(dis, (*it) - x); } } } for (auto m : L2) { int sum = m.first; set<int> left = m.second; for (auto y : left) { set<int> down = D2[sum]; auto it = down.lower_bound(y); if (it == down.end()) { break; } else { dis2 = min(dis2, (*it) - y); } } } for (auto m : R3) { int diff = m.first; set<int> right = m.second; for (auto y : right) { set<int> down = D3[diff]; auto it = down.lower_bound(y); if (it == down.end()) { break; } else { dis2 = min(dis2, (*it) - y); } } } for (auto m : U2) { int sum = m.first; set<int> up = m.second; for (auto y : up) { set<int> right = R2[sum]; auto it = right.upper_bound(y); if (it == right.end()) { break; } else { dis2 = min(dis2, (*it) - y); } } } for (auto m : U3) { int diff = m.first; set<int> up = m.second; for (auto y : up) { set<int> left = L3[diff]; auto it = left.upper_bound(y); if (it == left.end()) { break; } else { dis2 = min(dis2, (*it) - y); } } } if (dis == INT_MAX && dis2 == INT_MAX) { cout << "SAFE" << endl; } else { int ans = INT_MAX; if (dis != INT_MAX) { ans = min(ans, dis * 5); } if (dis2 != INT_MAX) { ans = min(ans, dis2 * 10); } cout << ans << endl; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T_ = 1; //cin >> T_; for (int i = 0; i < T_; i++)solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N, K, x; cin >> N; cin >> K; x = 1; for (int i = 0; i < N; ++i) { if (x * 2 < x + K) { x = x * 2; } else { x = x + K; } } cout << x << endl; }
// // main.cpp // #include <algorithm> #include <array> #include <assert.h> #include <functional> #include <iostream> #include <iomanip> #include <limits> #include <map> #include <math.h> #include <memory> #include <queue> #include <random> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; template<typename T> vector<T> make_vec_nd(T init, size_t size) { return vector<T>(size, init); } template<typename T, typename... Args> auto make_vec_nd(T init, size_t size, Args... rest) { auto inner = make_vec_nd(init, rest...); return vector<decltype(inner)>(size, inner); } #define rep(i,a,b) for(ll i=(a);i<(b);i++) #define rrep(i,a,b) for(ll i=(a)-1;i>=(b);i--) int main() { ll N; cin >> N; string s; cin >> s; ll ans = 0; rep(bits,0,1<<N) { string zeros; string ones; rrep(i,N,0) { if (bits & (1 << i)) { ones.push_back(s[i]); } else { zeros.push_back(s[i]); } } /* if (bits == 1) { cout << "zeros:" << zeros << endl; cout << "ones:" << ones << endl; } */ auto dp = make_vec_nd(0, 1+zeros.size(), 1+ones.size()); dp[0][0] = 1; rep(n,1,N+1) { rep(z,0,min((ll)zeros.size(),n)+1) { ll o = n - z; if (o > ones.size()) { continue; } if (z >= 1 && s[2*N-n] == zeros[zeros.size()-z]) { dp[z][o] += dp[z-1][o]; } if (o >= 1 && s[2*N-n] == ones[ones.size()-o]) { dp[z][o] += dp[z][o-1]; } // cout << z << "," << o << ":" << dp[z][o] << endl; } } ans += dp[zeros.size()][ones.size()]; } cout << ans << endl; }
0
#include <bits/stdc++.h> #define ll unsigned long long #define MOD 1000000007 using namespace std; ll a[2000005]; ll S[2000005]; ll add(ll a,ll b){ return (a%MOD+b%MOD)%MOD; } ll mul(ll a,ll b){ return (a%MOD*b%MOD)%MOD; } int main(){ int n ; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } S[1] = a[1]; for(int i=2;i<=n;i++){ S[i] = add(S[i-1],a[i]); } ll ans = 0; for(int i=1;i<=n-1;i++){ // 注意大整数取模的时候需要先加上MOd防止暴毙。 ans = add(ans,mul(a[i],(S[n]-S[i]+MOD)%MOD))%MOD; } cout<<ans; // // ull n,sum = 0; // cin >> n; // for(int i = 1;i <= n;i++) cin >> arr[i]; // for(int i = 1;i <= n;i++) ans[i] = (arr[i]+ans[i-1]); // for(int i = 1;i <= n;i++) sum = ((sum + arr[i]*((ans[n]-ans[i])%mod))%mod)%mod; // cout << sum << endl; }
#include <bits/stdc++.h> #define ll long long using namespace std; ll mod = 1e9+7; int main() { int n; cin>>n; vector<ll> a(n); ll s1 = 0, s2 = 0; for (int i=0; i<n; i++) { cin>>a[i]; s1 += a[i]; s2 += (a[i] * a[i]); s1 %= mod; s2 %= mod; } ll res = (s1 * s1 - s2); res %= mod; while (res < 0) res += mod; ll ans; if (res % 2) ans = (res + mod) / 2; else ans = res / 2; cout << ans << '\n'; }
1
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #define pp pair<int,int> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define ld long double #define al(a) (a).begin(),(a).end() #define mk make_pair #define check cout<<"?"<<endl; ll MOD=1000000007; ll mod=998244353; int inf=1000001000; ll INF=1e18+5; int main(){ int x,y,ans=0; cin>>x>>y; if(x<=3) ans+=(4-x)*100000; if(y<=3) ans+=(4-y)*100000; if(x==1 && y==1) ans+=400000; cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B, C, D, E, F; cin >> A >> B >> C >> D >> E >> F; cout << A + B + C + D + E + F - min(A, min(B, min(C, D))) - min(E, F) << endl; }
0
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using vi=vector<int>; using P = pair<int,int>; using Graph = vector<vector<int>>; int main(){ cin.tie(0); ios::sync_with_stdio(false); int x,y; ll cnt=0; cin >> x >> y; if(x>=0 && y>=0){ if(x>y){ x=0-x; ++cnt; while(1){ if(abs(x)==abs(y)) break; ++x; ++cnt; } if(y!=0) ++cnt; }else{ while(1){ if(x==y) break; ++x; ++cnt; } } }else if(x<0 && y<0){ if(abs(x)>=abs(y)){ while(1){ if(x==y) break; ++x; ++cnt; } }else{ x=0-x; ++cnt; while(1){ if(abs(x)==abs(y)) break; ++x; ++cnt; } ++cnt; } }else if(x<0 && y>=0){ if(abs(x)<=abs(y)){ x=0-x; ++cnt; } while(1){ if(abs(x)==abs(y)) break; ++x; ++cnt; } if(x!=y) ++cnt; }else if(x>=0 && y<0){ if(abs(x)<abs(y)){ while(1){ if(abs(x)==abs(y)) break; ++x; ++cnt; } ++cnt; }else{ x=0-x; ++cnt; while(1){ if(x==y) break; ++x; ++cnt; } } } cout << cnt << "\n"; return 0; }
#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; int main() { ll a, b; cin >> a >> b; if(a == 0){ if(b < 0) cout << abs(b) + 1 << endl; else cout << abs(b) << endl; }else if(b == 0){ if(a < 0) cout << abs(a) << endl; else cout << abs(a) + 1 << endl; }else if(0 < a && 0 < b){ if(a < b){ cout << b - a << endl; }else{ cout << abs(b - a) + 2 << endl; } }else if(a < 0 && b < 0){ if(abs(a) < abs(b)){ cout << abs(b) - abs(a) + 2 << endl; }else{ cout << abs(a) - abs(b) << endl; } }else if(a < 0 && 0 < b){ if(abs(a) < b){ cout << b - abs(a) + 1 << endl; }else{ cout << abs(a) - b + 1 << endl; } }else if(0 < a && b < 0){ if(a < abs(b)){ cout << abs(b) - a + 1 << endl; }else{ cout << a - abs(b) + 1 << endl; } } return 0; }
1
#include <iostream> using namespace std; static const int MAX = 101; static const int INFTY = (1<<21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; int prim() { int u, mincost; int d[MAX], p[MAX], color[MAX]; for(int i=0; i<MAX; i++) { color[i] = WHITE; d[i] = INFTY; p[i] = -1; } d[0] = 0; while(1) { mincost = INFTY; for(int i=0; i<n; i++) { if(color[i] != BLACK && d[i] < mincost) { mincost = d[i]; u = i; } } if(mincost == INFTY) break; color[u] = BLACK; for(int v=0; v<n; v++) { if(color[v] != BLACK && M[u][v] != -1) { if(M[u][v] < d[v]) { d[v] = M[u][v]; p[v] = u; color[v] = GRAY; } } } } int sum = 0; for(int i=0; i<n; i++) { if(p[i] != -1) sum += M[i][p[i]]; } return sum; } int main() { cin >> n; int e; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin >> e; if(e == -1) M[i][j] = INFTY; else M[i][j] = e; } } cout << prim() << endl; return 0; }
#include <iostream> #include <utility> #include <algorithm> using namespace std; int p[100]; pair<int, int> a[10000]; int root( int m ) { if ( p[m] != m ) p[m] = root( p[m] ); return p[m]; } void unit( int x, int y ) { p[root( y )] = root( x ); } bool same( int x, int y ) { return root( x ) == root( y ); } int main() { int n, ai, pn, ans; cin >> n; for ( int i = 0; i < n * n; i++ ) { cin >> ai; if ( ai == -1 || i % n <= i / n ) ai = 2001; a[i] = make_pair( ai, i ); } sort( a, a + ( n * n ) ); pn = 0; ans = 0; for ( int i = 0; i < n; i++ ) p[i] = i; for ( int i = 0; i < n * n; i++ ) { if ( ! same( a[i].second % n, a[i].second / n ) ) { ans += a[i].first; pn++; unit( a[i].second % n, a[i].second / n ); if ( pn >= n - 1 ) break; } } cout << ans << endl; }
1
#include <iostream> #include <bits/stdc++.h> using namespace std; #define ll long long const ll mod = 1e9 + 7; const ll INF = 1e14; void solve(){ int n; cin >> n; string s; cin >> s; if(s.length() <= n) cout << s; else cout << s.substr(0, n) << "..."; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //int test; cin >> test; //for(int t = 1; t <= test; ++t){ solve(); //} return 0; }
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <bitset> // bitset #include <deque> // deque #include <iomanip> //setprecsion #include <iostream> // cout, endl, cin #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, to_string, stoi #include <tuple> // tuple, make_tuple #include <utility> // pair, make_pair #include <vector> // vector #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <cmath> //sin, cos, tan using namespace std; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define mp(p, q) make_pair(p, q) #define pb(n) push_back(n) #define all(a) a.begin(), a.end() typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef string str; typedef vector<ll> vll; typedef vector<ld> vd; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<str> vs; typedef vector<vector<ll>> vvll; typedef vector<vector<ld>> vvd; typedef vector<vector<bool>> vvb; typedef vector<vector<char>> vvc; typedef vector<vector<str>> vvs; typedef vector<pair<ll, ll>> vpll; const ld PI = acos(-1.0); const ll MAX = 9000000000000000000; const ll MIN = -9000000000000000000; const ll MOD = 1000000007; template <typename T> void fin(T a) { cout << a << endl; exit(0); } void Main() { ll n; cin >> n; str ans; while(n) { n--; ans.pb((char)(n % 26 + 'a')); n /= 26; } reverse(all(ans)); fin(ans); } //前処理 int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); //高精度少数表示 Main(); return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct node { ll x; ll y; }; node a[5007]; int n; bool cmp(node p,node q) { if (p.x+p.y==q.x+q.y) return p.y<q.y; return p.x+p.y<q.x+q.y; } int main(){ ios::sync_with_stdio(false); scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y); priority_queue<int>q; // n=n+1; // a[n].x=1e9+10; // a[n].y=1e9+10; sort(a+1,a+n+1,cmp); ll tmp=0; ll t=0; int ans=0; for (int i=1;i<=n;i++) { // cout<<ans<<" "<<a[i].x<<a[] if (tmp<=a[i].x) { tmp+=a[i].y; q.push(a[i].y); ans++; } else { if (a[i].y<q.top()) { tmp-=q.top(); q.pop(); tmp+=a[i].y; q.push(a[i].y); } } } printf("%d\n",ans); }
#include <iostream> #include <fstream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <set> #include <map> #include <stack> #include <queue> #include <deque> #include <algorithm> using namespace std; #define REP(i,n) for(int i=0; i<n; ++i) #define FOR(i,a,b) for(int i=a; i<=b; ++i) #define FORR(i,a,b) for (int i=a; i>=b; --i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef pair<int,int> P; typedef pair<ll,ll> PL; const int N = 5010; const ll INF = 1e18; ll dp[N][N]; int main() { int n; cin >> n; vector<PL> a(n); REP(i,n){ cin >> a[i].first >> a[i].second; a[i].first += a[i].second; } sort(ALL(a)); VL h(n), p(n); REP(i,n){ h[i] = a[i].first - a[i].second; p[i] = a[i].second; // cout << h[i] << " " << p[i] << endl; } REP(i,n+1) REP(j,n+1) dp[i][j] = INF; dp[0][0] = 0; REP(i,n) REP(j,n){ dp[i+1][j] = min(dp[i+1][j], dp[i][j]); if (dp[i][j] <= h[i]) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + p[i]); } int ans = 0; REP(i,n+1) if (dp[n][i] < INF) ans = i; cout << ans << endl; return 0; }
1
#include<iostream> #include<math.h> using namespace std; int main() { int n; cin >> n; cout << n / 3; }
#include <bits/stdc++.h> using namespace std; #define repr(i,a,b) for (int i=a; i<b; i++) #define rep(i,n) for (int i=0; i< n; i++) 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 long long INF = 1LL << 60; #define PI 3.14159265359 template<typename T> void remove(std::vector<T>& vector, unsigned int index) { vector.erase(vector.begin() + index); } int gcd(int a, int b){ if(b == 0) return a; else return gcd(b, a%b); } long long lcm (int a, int b){ return (long long) a*b /gcd(a,b); } long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; } int collatz(int n){ int a=n; if(a%2 == 0)a/=2; else a = a*3+1; return a; } int main(){ int R; cin >> R; if(R<1200)cout << "ABC" << endl; else if (R>=1200 && R<2800)cout << "ARC" << endl; else cout << "AGC" << endl; }
0
#include <iostream> #include <vector> using namespace std; typedef long long ll; const ll MOD = 1000000007; ll modpow(ll x, ll n){ ll res = 1; ll mul = x; while(n){ if(n % 2) res = res * mul % MOD; mul = mul * mul % MOD; n /= 2; } return res; } struct Combination { int maxn; vector<ll> fmemo; vector<ll> imemo; vector<ll> ifmemo; Combination(int maxn) : maxn(maxn){ fmemo.resize(maxn + 1); imemo.resize(maxn + 1); ifmemo.resize(maxn + 1); for(ll i = 1; i <= maxn; i++){ imemo[i] = modpow(i, MOD - 2); } } ll fact(ll n){ if(n == 0) return 1; if(fmemo[n]) return fmemo[n]; return fmemo[n] = n * fact(n - 1) % MOD; } ll ifact(ll n){ if(n == 0) return 1; if(ifmemo[n]) return ifmemo[n]; return ifmemo[n] = imemo[n] * ifact(n - 1) % MOD; } ll com(ll n, ll k){ if(k > n) return 0; if(k == n || k == 0) return 1; return (fact(n) * ifact(k) % MOD) * ifact(n - k) % MOD; } }; vector<int> enum_prime(int n){ // containing n vector<int> res; if (n <= 1) return res; vector<bool> p(n + 1); fill(p.begin() + 2, p.end(), true); for(int i = 2; i <= n; i++){ if(p[i]){ res.push_back(i); for(int j = i * 2; j <= n; j += i) p[j] = false; } } return res; } int main() { int n, m; cin >> n >> m; vector<int> prime = enum_prime(100000); Combination com(150000); ll ans = 1; for(int p : prime){ int k = 0; while(m % p == 0){ m /= p; k++; } ans = ans * com.com(n + k - 1, k) % MOD; } if(m > 1) ans = ans * n % MOD; cout << ans << endl; }
#include <bits/stdc++.h> #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 ALL(x) (x).begin(),(x).end() #define SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define INF 1000000000 using namespace std; typedef long long ll; int main() { int A, B; cin >> A >> B; int ans = 0; if(A > B) { ans += A; A--; } else{ ans += B; B--; } ans += max(A, B); cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MOD=1e9+7; const int INF=1e9; //const int MOD=998244353; const long long LINF=1e18; #define int long long //template int black[2005][2005],ver[2005][2005],wid[2005][2005]; //main signed main(){ int N,M,Q;cin>>N>>M>>Q; std::vector<string> s(N+1); for(int i=0;i<N;i++)cin>>s[i]; s[N]=(string(M,'#')); for(int i=0;i<=N;i++)s[i]+="#"; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ if(s[i][j]=='1')black[i+1][j+1]++; if(s[i][j]=='1'&&s[i][j+1]=='1')ver[i+1][j+1]++; if(s[i][j]=='1'&&s[i+1][j]=='1')wid[i+1][j+1]++; } } for(int i=0;i<=N;i++){ for(int j=0;j<=M;j++){ black[i][j+1]+=black[i][j]; ver[i][j+1]+=ver[i][j]; wid[i][j+1]+=wid[i][j]; } } for(int i=0;i<=N;i++){ for(int j=0;j<=M;j++){ black[i+1][j]+=black[i][j]; ver[i+1][j]+=ver[i][j]; wid[i+1][j]+=wid[i][j]; } } std::vector<int> ans(Q); for(int i=0;i<Q;i++){ int x1,x2,y1,y2;cin>>x1>>y1>>x2>>y2; int B=black[x2][y2]+black[x1-1][y1-1]-black[x2][y1-1]-black[x1-1][y2]; int E1=ver[x2][y2-1]+ver[x1-1][y1-1]-ver[x2][y1-1]-ver[x1-1][y2-1]; int E2=wid[x2-1][y2]+wid[x1-1][y1-1]-wid[x2-1][y1-1]-wid[x1-1][y2]; ans[i]=B-E1-E2; } for(int i=0;i<Q;i++)cout<<ans[i]<<endl; } /* 解説AC 連結成分の個数は森なら頂点の個数と辺の数から数えられるというもの 森じゃなくても各連結成分の頂点の数と辺の数に何かしら関係があれば使えるテクだね 数えやすい成分に言い換えてしまうテクニック あとそもそも今回の問題の設定を閉路がないとしか思わなかったけど閉路がない=木(森)を忘れていた */
#pragma GCC optimize(2) #include <bits/stdc++.h> using namespace std; template<class t> inline t read(t &x){ x=0;char c=getchar();bool f=0; while(!isdigit(c)) f|=c=='-',c=getchar(); while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar(); if(f) x=-x;return x; } template<class t> inline void write(t x){ if(x<0){putchar('-'),write(-x);} else{if(x>9)write(x/10);putchar('0'+x%10);} } const int N=2005; int n,m,q,a[N][N],s[N][N],sx[N][N],sy[N][N]; void doit(){ int x,y,xx,yy; read(x);read(y);read(xx);read(yy); int n=s[xx][yy]-s[xx][y-1]-s[x-1][yy]+s[x-1][y-1]; int m=(sx[xx][yy-1]-sx[xx][y-1]-sx[x-1][yy-1]+sx[x-1][y-1])+(sy[xx-1][yy]-sy[x-1][yy]-sy[xx-1][y-1]+sy[x-1][y-1]); write(n-m);puts(""); } signed main(){ read(n);read(m);read(q); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%1d",&a[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j]; sx[i][j]=sx[i-1][j]+sx[i][j-1]-sx[i-1][j-1]+(a[i][j]+a[i][j+1]==2); sy[i][j]=sy[i-1][j]+sy[i][j-1]-sy[i-1][j-1]+(a[i][j]+a[i+1][j]==2); } while(q--) doit(); }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1000000007; #define REP(i, n) for (ll i = 0; i < (n); i++) #define REP2(i, x, n) for (ll i = x; i < (n); i++) #define PR(x) cout << (x) << "\n" #define PS(x) cout << (x) << " " const long long INF = numeric_limits<long long>::max(); int main() { ll A, B; cin >> A >> B; string str; cin >> str; bool isOk = true; REP(i, str.size()) { if (i == A) { isOk &= str[i] == '-'; } else { isOk &= str[i] != '-'; } if (!isOk) break; } PR(isOk ? "Yes" : "No"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; vector<char> S(A + B + 1); for (int i = 0; i < A + B + 1; i++) { cin >> S.at(i); } string ans = "Yes"; if (S.at(A) != '-') ans = "No"; for (int i = 0; i < S.size(); i++) { if (S.at(i) == '-' && i != A) ans = "No"; } cout << ans << endl; }
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 REP(i, n) for(int i = 0; i < (int)(n); i++) #define FOR(i, a, b) for(int i = (a); i < (int)(b); i++) #define pw(x) (1LL<<(x)) static const int fast_io = [](){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}(); static const int precise_doubles = [](){cout<<fixed<<setprecision(20);return 0;}(); typedef pair<int, int> PII; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // find_by_order(k) - iterator to kth largest, order_of_key(k) - #things < k typedef long double LD; typedef long long LL; #define EVAL(x) x #define SPA(x) EVAL({cout << #x" = " << x << ' ';}) #define NEWL EVAL({cout << '\n';}) #define SPAR(a, b) EVAL({cout<<#a" = ";for(auto it=a;it!=b;)cout<<*(it++)<<" ";cout<<'\n';}) #define SPAV(v) EVAL({cout<<#v" = ";for(auto it=v.begin();it!=v.end();)cout<<*(it++)<<" ";cout<<'\n';}) const int INF = 0x3f3f3f3f; // 1.0e9 const LL LINF = 0x3f3f3f3f3f3f3f3fll; // 4.5e18 const LD eps = 1e-20; const int mod = 3; inline int msum(int x, int y) {return (x+y<mod ? x+y : x+y-mod);} inline int mdif(int x, int y) {return (x>=y ? x-y : x-y+mod);} inline int mprod(int x, int y) { return (1ll*x*y) % mod; } inline int mpow(int x, LL y) {LL r=1;while(y){if(y&1)r=mprod(r,x);x=mprod(x,x);y>>=1;}return r;} inline int minv(int x) { return mpow(x, mod-2); } #define DIE EVAL({cout << -1 << '\n'; exit(0);}) //------------------------------------------------------------------------------------------------------ LL K; int main() { cin >> K; LL x = K % 50; LL m = (K-x)/50 + 49; cout << 50 << '\n'; REP(i, 50-x) cout << (m-x) << ' '; REP(i, x) cout << (m+51-x) << ' '; cout << '\n'; }
#include <iostream> #include <vector> using namespace std; int simulate(vector<long long int> A){ int N = A.size(), ret = 0; while(1){ int t = 0; for(int i = 0; i < N; ++i){ if(A[i] > A[t]) t = i; } if(A[t] < N) break; for(int i = 0; i < N; ++i){ if(i != t) ++A[i]; else A[i] -= N; } ++ret; } return ret; } int main(){ long long int K, M = 1e16 + 1e3 + 1; cin >> K; vector<long long int> A(50); for(long long int i = 0; i < 50; ++i) A[i] = i + K/50; K %= 50; for(int i = 0; i < K; ++i) ++A[49-i]; //cout << simulate(A) << endl; cout << 50 << endl; cout << A[0]; for(int i = 1; i < 50; ++i) cout << " " << A[i]; cout << endl; return 0; }
1
#include "bits/stdc++.h" #define ll long long #define mp(a, b) make_pair(a, b) using namespace std; typedef pair<ll, ll> pairs; typedef pair<ll, pairs> tpl; ll c[4]; double dp[301][301][301]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, a; cin >> n; for (ll i = 0; i < n; i++) cin >> a, c[a]++; dp[0][0][0] = 0; for (ll i = 0; i <= n; i++) { for (ll j = 0; j <= n; j++) { for (ll k = 0; k <= n; k++) { double z = n - (i + j + k), val = 1; if (z == n) continue; if (i + j + k > n) continue; if (i > 0) val += (1.0 * i / n) * dp[i - 1][j + 1][k]; if (j > 0) val += (1.0 * j / n) * dp[i][j - 1][k + 1]; if (k > 0) val += (1.0 * k / n) * dp[i][j][k - 1]; if (z > 0) val /= (1.0 - 1.0 * z / n); dp[i][j][k] = val; } } } cout << setprecision(9) << fixed << dp[c[3]][c[2]][c[1]]; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0;i < (int)(n);i++) using ll = long long; const ll MOD=1000000007; const long long INF = 1LL << 60; const double pi=acos(-1.0); 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 N; //dp vector<vector<vector<double>>> table; double dp(ll i,ll j,ll k){ //cout<<i<<" "<<j<<" "<<k<<endl; if(i==0&&j==0&&k==0) return 0; if(table.at(i).at(j).at(k)>-1) return table.at(i).at(j).at(k); table.at(i).at(j).at(k)=0; if(i>0) table.at(i).at(j).at(k)+=dp(i-1,j,k)*i/N; if(j>0) table.at(i).at(j).at(k)+=dp(i+1,j-1,k)*j/N; if(k>0) table.at(i).at(j).at(k)+=dp(i,j+1,k-1)*k/N; table.at(i).at(j).at(k)+=1; return table.at(i).at(j).at(k)/=(double)(i+j+k)/N; } int main() { cin>>N; vector<ll> vec(4,0); rep(i,N) {ll a; cin>>a; vec.at(a)++;} //dp table=vector<vector<vector<double>>>(N+1,vector<vector<double>>(N+1,vector<double>(N+1,-1))); cout<<fixed<<setprecision(15)<<dp(vec.at(1),vec.at(2),vec.at(3))<<endl; /* rep(i,2) { if(i!=0) cout<<" "; cout<<table.at(0).at(0).at(i); } cout<<fixed<<setprecision(15)<<endl; */ return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> pipii; typedef pair<pii, pii> piipii; #define mp make_pair #define fi first #define se second #define all(a) (a).begin(), (a).end() #define sz(a) (int)(a).size() #define eb emplace_back pii p[100005]; int x[100005], y[100005]; int r[100005]; int root(int a){ return r[a] == a?a:(r[a]=root(r[a])); } void merge(int a, int b){ a = root(a), b = root(b); if(a != b) r[a] = b; } int main(){ int n; scanf("%d", &n); for(int i=1;i<=n;i++){ scanf("%d%d", &x[i], &y[i]); p[i] = pii(x[i], i); r[i] = i; } sort(p+1, p+1+n); vector<pipii> edges; for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se))); sort(all(edges)); for(int i=1;i<=n;i++) p[i] = pii(y[i], i); sort(p+1, p+1+n); for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se))); sort(all(edges)); int ans = 0; for(int i=0;i<sz(edges);i++){ if(root(edges[i].se.fi) != root(edges[i].se.se)){ merge(edges[i].se.fi, edges[i].se.se); ans += edges[i].fi; } } printf("%d\n", ans); }
#include <iostream> #include <vector> #include <functional> using namespace std; const int INF = 1001001001; template<class M, class L> struct LazyPropagationSegmentTree { int n; int size; vector<M> data; vector<L> lazy; const function<M(M, M)> f_m; const M e_m; const function<L(L, L)> f_l; const L e_l; const function<M(M, L)> f; LazyPropagationSegmentTree(int n, function<M(M, M)> f_m, M e_m, function<L(L, L)> f_l, L e_l, function<M(M, L)> f) : n(n), f_m(f_m), e_m(e_m), f_l(f_l), e_l(e_l), f(f) { size = 1; while (size < n) size <<= 1; data.assign(size * 2, e_m); lazy.assign(size * 2, e_l); } void init(const vector<M> &init_vals) { data.assign(size * 2, e_m); for (int i = 0; i < init_vals.size(); i++) data[size+i] = init_vals[i]; for (int i = size-1; i > 0; i--) data[i] = f_m(data[2*i], data[2*i+1]); } void eval(int k) { if (lazy[k] == e_l) return; if (k < size) { lazy[2*k] = f_l(lazy[2*k], lazy[k]); lazy[2*k + 1] = f_l(lazy[2*k + 1], lazy[k]); } data[k] = f(data[k], lazy[k]); lazy[k] = e_l; } void update(int l, int r, L l_prop) { update_aux(l, r, l_prop, 1, 0, size); } void update_aux(int l, int r, L l_prop, int k, int k_l, int k_r) { eval(k); if (r <= k_l || k_r <= l) return; else if (l <= k_l && k_r <= r) { lazy[k] = f_l(lazy[k], l_prop); eval(k); } else { update_aux(l, r, l_prop, 2*k, k_l, (k_l + k_r) / 2); update_aux(l, r, l_prop, 2*k + 1, (k_l + k_r) / 2, k_r); data[k] = f_m(data[2*k], data[2*k + 1]); } } M query(int l, int r) { return query_aux(l, r, 1, 0, size); } M query_aux(int l, int r, int k, int k_l, int k_r) { eval(k); if (r <= k_l || k_r <= l) return e_m; else if (l <= k_l && k_r <= r) return data[k]; else { M vl = query_aux(l, r, k*2, k_l, (k_l + k_r) / 2); M vr = query_aux(l, r, k*2 + 1, (k_l + k_r) / 2, k_r); return f_m(vl, vr); } } void eval_all() { for (int i = 1; i < 2*size; i++) eval(i); } M val(int idx) { return data[size + idx]; } vector<M> val_vec() { vector<M> res(n); for (int i = 0; i < n; i++) res[i] = data[size+i]; return res; } }; int main() { int h, w; cin >> h >> w; struct M { int val, idx, l; }; using L = int; M e_m = {-INF, INF, -1}; L e_l = -INF-1; auto f_m = [&](M m1, M m2) -> M { if (m1.idx - m1.val < m2.idx - m2.val) return m1; else return {m2.val, m2.idx, m1.l}; }; auto f_l = [&](L l1, L l2) -> L { if (l2 == e_l) return l1; return l2; }; auto f = [&](M m, L l) -> M { if (l == e_l) return m; return {l, m.l, m.l}; }; LazyPropagationSegmentTree<M, L> st(w, f_m, e_m, f_l, e_l, f); vector<M> init_vals(w); for (int i = 0; i < w; i++) { init_vals[i] = {i, i, i}; } st.init(init_vals); for (int i = 1; i <= h; i++) { int a, b; scanf("%d%d", &a, &b); a--; int val; if (a == 0) val = -INF; else val = st.query(a-1, a).val; st.update(a, b, val); M q = st.query(0, w); int ans = i + q.idx - q.val; if (q.val == -INF) ans = -1; cout << ans << endl; } return 0; }
0
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> using namespace std; #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define eprintf(...) 42 #endif typedef long long ll; typedef pair<int, int> pii; #define mp make_pair const ll MOD = 998244353; ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; } ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; } ll mult(ll x, ll y) { return (x * y) % MOD; } ll bin_pow(ll x, ll p) { if (p == 0) return 1; if (p & 1) return mult(x, bin_pow(x, p - 1)); return bin_pow(mult(x, x), p / 2); } ll Rev(ll x) { return bin_pow(x, MOD - 2); } ll W; const int LOG = 20; const int N = 1 << 20; ll w[N]; int rev[N]; void initFFT() { for (ll x = 2;; x++) { ll y = x; for (int i = 1; i < LOG; i++) y = mult(y, y); if (y == MOD - 1) { W = x; break; } } w[0] = 1; for (int i = 1; i < N; i++) w[i] = mult(w[i - 1], W); rev[0] = 0; for (int mask = 1; mask < N; mask++) { int k = 0; while(((mask >> k) & 1) == 0) k++; rev[mask] = rev[mask ^ (1 << k)] ^ (1 << (LOG - 1 - k)); } } ll F[2][N]; void FFT(ll *A) { for (int i = 0; i < N; i++) F[0][rev[i]] = A[i]; int t = 0, nt = 1; for (int lvl = 0; lvl < LOG; lvl++) { int len = 1 << lvl; for (int st = 0; st < N; st += (len << 1)) for (int i = 0; i < len; i++) { ll ad = mult(F[t][st + len + i], w[i << (LOG - 1 - lvl)]); F[nt][st + i] = add(F[t][st + i], ad); F[nt][st + len + i] = sub(F[t][st + i], ad); } swap(t, nt); } for (int i = 0; i < N; i++) A[i] = F[t][i]; } ll f[N], rf[N]; ll a[N]; ll getC(int n, int k) { if (k < 0 || k > n) return 0; return mult(f[n], mult(rf[k], rf[n - k])); } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); initFFT(); f[0] = 1; for (int i = 1; i < N; i++) f[i] = mult(f[i - 1], i); rf[N - 1] = Rev(f[N - 1]); for (int i = N - 1; i > 0; i--) rf[i - 1] = mult(rf[i], i); for (int k = 0; k < N / 2; k++) { a[k] = mult(f[2 * k], mult(rf[k], rf[k])); } FFT(a); for (int i = 0; i < N; i++) a[i] = mult(a[i], a[i]); FFT(a); reverse(a + 1, a + N); ll rN = Rev(N); for (int i = 0; i < N; i++) { a[i] = mult(a[i], rN); a[i] = mult(a[i], mult(rf[2 * i], mult(f[i], f[i]))); a[i] = sub(a[i], 1); a[i] = mult(a[i], (MOD + 1) / 2); a[i] += i; } int n, m; scanf("%d%d", &n, &m); if (n < m) swap(n, m); if (n == m) { printf("%lld\n", a[n]); return 0; } ll ans = 0; for (int k = 0; k <= m; k++) { int L = n - m + k - 1; ll p = getC(L + k, k); if (k > 0) p = sub(p, getC(L + k, k - 1)); p = mult(p, getC(2 * (m - k), m - k)); ans = add(ans, mult(p, add(L + 1, a[m - k]))); } ans = mult(ans, mult(rf[n + m], mult(f[n], f[m]))); printf("%lld\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i) #define per(i,a,b) for(int i=(b),i##_end=(a);i>=i##_end;--i) #define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i) #define foe(i,a) for(__typeof(a.begin())i=a.begin();i!=a.end();++i) #define X first #define Y second #define mp make_pair #define pb push_back #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() typedef double ld; typedef long long L; typedef vector<int> VI; typedef pair<int,int> pa; #ifdef flukehn #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif const int N=1e6+11,mo=998244353; int fpow(int a,int b){ int r=1; for(;b;b>>=1,a=1ll*a*a%mo) if(b&1)r=1ll*r*a%mo; return r; } int inv(int x){return fpow(x,mo-2);} int jc[N+1],jcv[N+1]; int C(int a,int b){ return a<b?0:1ll*jc[a]*jcv[b]%mo*jcv[a-b]%mo; } int n,m; int main(){ #ifdef flukehn freopen("c.in","r",stdin); #endif ios::sync_with_stdio(0); jc[0]=1; rep(i,1,N)jc[i]=1ll*jc[i-1]*i%mo; jcv[N]=inv(jc[N]); per(i,1,N)jcv[i-1]=1ll*jcv[i]*i%mo; cin>>n>>m; if(n<m)swap(n,m); int ans=0; rep(i,1,m){ ans=(ans+1ll*C(2*i,i)*C(n+m-2*i,n-i))%mo; } ans=1ll*ans*inv(C(n+m,n))%mo; ans=1ll*ans*inv(2)%mo; printf("%d\n",(ans+n)%mo); }
1
#include <bits/stdc++.h> using namespace std; template<typename T> struct Segtree { int n; T e; vector<T> dat; typedef function<T(T a, T b)> Func; Func f; Segtree(){} Segtree(int n_input, Func f_input, T e_input){ initialize(n_input, f_input, e_input); } void initialize(int n_input, Func f_input, T e_input){ f = f_input; e = e_input; n = 1; while(n < n_input) n <<= 1; dat.resize(2*n-1, e); } void update(int k, T a){ k += n - 1; dat[k] = a; while(k > 0){ k = (k - 1)/2; dat[k] = f(dat[2*k+1], dat[2*k+2]); } } T get(int k){ return dat[k+n-1]; } T between(int a, int b){ return query(a, b+1, 0, 0, n); } T query(int a, int b, int k, int l, int r){ if(r<=a || b<=l) return e; if(a<=l && r<=b) return dat[k]; T vl = query(a, b, 2*k+1, l, (l+r)/2); T vr = query(a, b, 2*k+2, (l+r)/2, r); return f(vl, vr); } }; #include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; vector<int> sz; UnionFind(int n=0){ if(n>0) initialize(n); } void initialize(int n){ par.resize(n); sz.resize(n); for(int i=0; i<n; i++){ par[i] = i; sz[i] = 1; } } int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } } void unite(int x, int y){ x = find(x); y = find(y); if(x == y) return; if(sz[x] < sz[y]){ par[x] = y; sz[y] += sz[x]; }else{ par[y] = x; sz[x] += sz[y]; } } bool same(int x, int y){ return find(x) == find(y); } }; int main(){ int N; int64_t D, A[200000]; cin >> N >> D; for(int i=0; i<N; i++) cin >> A[i]; typedef pair<int64_t, int> P; const int64_t INF = 1e18; const P INFP = {INF, 0}; Segtree<P> stL(N, [](P a, P b){ return min(a, b);}, INFP); Segtree<P> stR(N, [](P a, P b){ return min(a, b);}, INFP); vector<int> order; for(int i=0; i<N; i++) order.push_back(i); sort(order.begin(), order.end(), [&](int i, int j){ return A[i] < A[j];}); vector<vector<int64_t>> edges; for(int i : order){ auto p = stL.between(0, i); if(p != INFP){ int64_t cost = p.first + A[i] - (N-i)*D; edges.push_back({cost, i, p.second}); } p = stR.between(i, N-1); if(p != INFP){ int64_t cost = p.first + A[i] - i*D; edges.push_back({cost, i, p.second}); } stL.update(i, {A[i] + (N-i)*D, i}); stR.update(i, {A[i] + i*D, i}); } sort(edges.begin(), edges.end()); UnionFind uf(N); int64_t ans = 0; for(auto& e : edges){ if(!uf.same(e[1], e[2])){ uf.unite(e[1], e[2]); ans += e[0]; } } cout << ans << endl; return 0; }
#include<iostream> #include<algorithm> #include<functional> #include<vector> #include<queue> using namespace std; #define LL long long #define LP pair<LL, LL> const LL inf = 1e17; struct segtree { int n = 1; vector<LP> node;//cost, place void init(int N) { node.clear(); while (N > n) n *= 2; node.resize(n * 2, { inf, inf }); } void update(int k, LL x, int a) { k += n - 1; node[k] = { x, a }; while (k > 0) { k = (k - 1) / 2; int l = k * 2 + 1; int r = k * 2 + 2; if (node[l].first < node[r].first) node[k] = node[l]; else node[k] = node[r]; } } LP mini(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (b <= l || r <= a) return { inf, inf }; if (a <= l && r <= b) return node[k]; LP ln = mini(a, b, k * 2 + 1, l, (l + r) / 2); LP rn = mini(a, b, k * 2 + 2, (l + r) / 2, r); if (ln.first < rn.first) return ln; else return rn; } }; struct edge { LL to; LL cost; bool operator<(const edge &e1) const { return e1.cost < cost; } }; int N; int idx[200010]; LL D; bool used[200010]; vector<LL> A; vector<LP> B; vector<edge> G[200010]; LL prim() { priority_queue<edge> pq; pq.push(edge{ 0, 0 }); LL total = 0; while (!pq.empty()) { edge e = pq.top(); pq.pop(); if (!used[e.to]) { used[e.to] = true; total += e.cost; for (auto u : G[e.to]) { if (!used[u.to]) pq.push(u); } } } return total; } void build() { segtree seg; seg.init(N); for (int i = 0; i < N; i++) { seg.update(idx[i], A[i] + D * (N - i - 1), idx[i]); if (i != 0) { LP lp = seg.mini(0, idx[i]); if (lp.first == inf) continue; lp.first -= D * (N - i - 1) - A[i]; G[B[lp.second].second].push_back(edge{ i, lp.first }); G[i].push_back(edge{ B[lp.second].second, lp.first }); } } seg.init(N); for (int i = N - 1; i >= 0; i--) { seg.update(idx[i], A[i] + D * i, idx[i]); if (i != N - 1) { LP lp = seg.mini(0, idx[i]); if (lp.first == inf) continue; lp.first -= D * i - A[i]; G[B[lp.second].second].push_back(edge{ i, lp.first }); G[i].push_back(edge{ B[lp.second].second, lp.first }); } } } void init() { cin >> N >> D; for (int i = 0; i < N; i++) { LP tmp; cin >> tmp.first; tmp.second = i; A.push_back(tmp.first); B.push_back(tmp); } sort(B.begin(), B.end()); for (int i = 0; i < N; i++) { idx[B[i].second] = i; } } int main() { ios::sync_with_stdio(false); cin.tie(0); init(); build(); cout << prim() << endl; return 0; }
1
#define _GLIBCXX_DEBUG //エラーを出すためのおまじない #include <bits/stdc++.h> //#include <cstdlib> // abs() for integer #include <cmath> // abs() for float, and fabs() #include <math.h> using namespace std; #define coYES cout << "YES" << endl #define coNO cout << "NO" << endl #define coYes cout << "Yes" << endl #define coNo cout << "No" << endl #define coyes cout << "yes" << endl #define cono cout << "no" << endl #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int a, b; cin >> a >> b; int count = 0; for (int i = a; i < b+1; i++) { int ii = i; vector<int> vec(5); rep(j, 5) { vec.at(j) = (ii % 10); ii /= 10; } if (vec.at(0) == vec.at(4)) { if (vec.at(1) == vec.at(3)) { count++; } } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; /////////////////////////////////////////// const long long int INF = INT64_MAX; const long long int Mod = 1000000007; using ll = long long int; using ci = const int; using vi = vector<int>; using Vi = vector<long long int>; typedef pair<int, int> P; typedef pair<ll, ll> PLL; #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) #define all(x) (x).begin(),(x).end() #define rep(i,N) for(ll i=0;i<N;i++) auto max(auto x,auto y){if(x>y){return x;}else{return y;}} auto min(auto x,auto y){if(x>y){return y;}else{return x;}} template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; } template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; } template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根; #define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート typedef vector<vector<ll> > matrix; void princ(auto x){cout<<x<<" ";}; void print(auto x){cout<<x<<"\n";}; /////////////////////////////////////////////////////////////////////////////////// ll n,m; Vi a; int mj[9]; vector<string> dp; string calc(ll nu){ if(!dp[nu].empty())return dp[nu]; string ans,tmp; if(nu==0){ ans.clear(); return ans; } rep(i,9){ if(mj[i]!=0){ if(nu-i>=0){ if(calc(nu-i).empty() && (nu-i)!=0)continue; tmp.assign(calc(nu-i)); tmp += to_string(mj[i]); //cout<<"tmp"<<tmp<<endl;; if(ans.size()<tmp.size())ans = tmp; if(ans.size()== tmp.size() && ans < tmp)ans = tmp; tmp.clear(); } } } dp[nu]=ans; return ans; } int main(){ cin.tie(0);ios::sync_with_stdio(false); cin>>n>>m; a.resize(m); dp.resize(n+2); rep(i,m)cin>>a[i]; sort(all(a)); rep(i,9)mj[i]=0; rep(i,m){ switch (a[i]){ case 1: mj[2]=1; break; case 2: mj[5]=2; break; case 3: mj[5]=3; break; case 4: mj[4]=4; break; case 5: mj[5]=5; break; case 6: mj[6]=6; break; case 7: mj[3]=7; break; case 8: mj[7]=8; break; case 9: mj[6]=9; break; } } print(calc(n)); return 0; }
0
#include<bits/stdc++.h> #define ll long long using namespace std; const int p=1e9+7; char s[300010]; int n,i,j,ans,k,f[300010][3][3]; int main(){ scanf("%s",s+1); n=strlen(s+1); f[0][0][0]=1; for(i=1;i<=n;i++){ if(s[i]!='1'){ for(j=0;j<3;j++) for(k=0;k<3;k++){ if(k==2){ f[i][j][k-1]=(f[i][j][k-1]+f[i-1][j][k])%p; } else f[i][j][k+1]=(f[i][j][k+1]+f[i-1][j][k])%p; } } if(s[i]!='0'){ for(j=0;j<3;j++) for(k=0;k<3;k++){ if(k==0)(f[i][j+(j!=2)][k]+=f[i-1][j][k])%=p; else (f[i][j][k-1]+=f[i-1][j][k])%=p; } } } for(i=0;i<3;i++) for(j=0;j<=i;j++)ans=(ans+f[n][i][j])%p; printf("%d",ans); }
#include<cstdio> #include<algorithm> #include<cstring> #define rep(a,b,c) for (int a=b;a<=c;a++) #define per(a,b,c) for (int a=b;a>=c;a--) #define go(u) for (int o=ft[u],v;v=E[o].t,o;o=E[o].n) #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int,int> par; typedef double dob; /* 0: 1:0 2:00 3:1 4:10 5:100 6:11 */ const int N=300010,P=1000000007; int n,g[7],f[7],h[7]; char s[N]; void add(int &x,int y){x=(x+y)%P;} void tran(int k){ memset(g,0,sizeof(g)); memset(h,0,sizeof(h)); add(g[1],f[0]); add(g[2],f[1]); add(g[1],f[2]); add(g[4],f[3]); add(g[5],f[4]); add(g[4],f[5]); add(g[6],f[6]); add(h[3],f[0]); add(h[0],f[1]); add(h[1],f[2]); add(h[6],f[3]); add(h[3],f[4]); add(h[4],f[5]); add(h[6],f[6]); if (k==0) swap(f,g); else if (k==1) swap(f,h); else{ rep(i,0,6) add(g[i],h[i]); swap(f,g); } } int main(){ scanf("%s",s+1); n=strlen(s+1); f[0]=1; rep(i,1,n) tran(s[i]=='?'?2:s[i]-'0'); LL ans=f[6]+f[3]; ans=(ans%P+P)%P; printf("%lld\n",ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; //TEMPLATE #define pb push_back #define mp make_pair #define ll long long #define ld long double #define pii pair <ll, ll> #define piii pair <ll, pii> #define F first #define S second #define newline printf("\n") #define minusone printf("-1\n") #define zeroo printf("0\n") #define scl1(a) scanf("%lld", &a) #define scl2(a, b) scanf("%lld %lld", &a, &b) #define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c) #define prl1(a) printf("%lld\n", a) #define prl2(a, b) printf("%lld %lld\n", a, b) #define ssl1(s) scanf(" %[^\n]", s) #define scd1(a) scanf("%lf", &a) #define scd2(a, b) scanf("%lf %lf", &a, &b) #define prd(a) printf("%lf\n", a) #define prld(a) printf("%Lf\n", a) #define prcase(cs) printf("Case %lld: ", cs) #define cin1(a) cin >> a #define cin2(a, b) cin >> a >> b #define cin3(a, b, c) cin >> a >> b >> c #define cin4(a, b, c, d) cin >> a >> b >> c >> d #define cot1(a) cout << a << "\n" #define cot2(a, b) cout << a << " " << b << "\n" #define cot3(a, b, c) cout << a << " " << b << " " << c << "\n" #define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n" #define cotline cout << "\n" #define cotminus cout << "-1\n" #define cot0 cout << "0\n" #define cotyes cout << "YES\n" #define cotno cout << "NO\n" #define cotcase(cs) cout << "Case " << cs << ": " #define reversed(s) reverse(s.begin(), s.end()) #define asort(s) sort(s.begin(), s.end()) #define dsort(s) sort(s.rbegin(), s.rend()) #define all(s) s.begin(), s.end() #define uniq(s) s.resize(distance(s.begin(),unique(s.begin(), s.end()))) #define found(s, x) (s.find(x) != s.end()) #define for0(i, n) for (i = 0; i < n; i++) #define for1(i, n) for (i = 1; i <= n; i++) #define fora(i, a, b) for (i = a; i <= b; i++) #define forb(i, b, a) for (i = b; i >= a; i--) #define fori(it, s) for(auto it = s.begin(); it != s.end(); it++) #define FR ios_base::sync_with_stdio(false);cin.tie(NULL) #define ms(a, x) memset(a, x, sizeof a) #define bitcount(n) __builtin_popcountll(n) //#define pi 3.1415926536 #define pi acos(-1) const ll INF = LLONG_MAX; const ll SZ = 1e3+5; const ll mod = 1e9+7; ll n, mt; bool cycle = 0; ll in[SZ][SZ]; vector <ll> adj[SZ*SZ]; ll vis[SZ*SZ]; ll ind[SZ*SZ]; void dfs(ll u) { if(vis[u] == 2 || cycle) return; if(vis[u] == 1) { cycle = 1; return; } vis[u] = 1; for(auto v: adj[u]) { dfs(v); } vis[u] = 2; } int main() { ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0, t = 0; // ll a, b, d; string s, s1, s2; cin1(n); mt = ((n *(n-1)) / 2); for1(i, n) fora(j, i+1, n) in[i][j] = in[j][i] = ++c; for1(i, n) { cin1(p); p = in[i][p]; fora(j, 2, n-1) { cin1(x); x = in[i][x]; adj[p].pb(x); ind[x]++; p = x; } } for1(i, mt) { if(!vis[i]) dfs(i); } if(cycle) { cotminus; return 0; } vector <ll> now; for1(i, mt) if(ind[i] == 0) now.pb(i); while(1) { ans++; vector <ll> tmp; for(auto u: now) { for(auto v: adj[u]) { ind[v]--; if(ind[v] == 0) tmp.pb(v); } } if(tmp.empty()) break; now = tmp; } cot1(ans); return 0; }
#include "bits/stdc++.h" #define REP(i,num) for(ll i=0;i<(num);++i) #define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i)) #define LOOP(i) while(i--) #define ALL(c) c.begin(),c.end() #define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl; #define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;} using namespace std; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; constexpr ll atcoder_mod = 1e9+7; template<typename T=ll> T in(){T x; cin >> x; return (x);} template<typename T=ll,typename C=vector<T>> C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);} void vout(){cout << endl;} template<typename Head,typename... Tail> void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);} void out(){cout << endl;} template<typename Head,typename... Tail> void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);} template<typename T> bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;} template<typename T> bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;} class ConnectNodeInfo{ vector<vector<pair<ll,ll>>> graph; public: ConnectNodeInfo(int node_num){ graph.resize(node_num); } void AddNonDirectionalConnection(ll u,ll v,ll w){ graph[u].emplace_back(v,w); graph[v].emplace_back(u,w); } void AddDirectionalConnection(ll u,ll v,ll w){ graph[u].emplace_back(v,w); } vector<pair<ll,ll>>& operator[](ll index){ return graph[index]; } size_t size(){return graph.size();} }; vector<ll> TopologicalSort(ConnectNodeInfo& connect){ int N=connect.size(); stack<ll> vertex; vector<ll> input(N),sorted; REP(i,N){ for(auto& x:connect[i]) input[x.first]++; } REP(i,N){ if(input[i]==0) vertex.push(i); } while(!vertex.empty()){ auto v=vertex.top();vertex.pop(); sorted.push_back(v); for(auto& x:connect[v]){ input[x.first]--; if(!input[x.first]) vertex.push(x.first); } } if(sorted.size()!=N) sorted.clear(); return sorted; } ll CalcLongestPathOnDAG(ConnectNodeInfo& connect,vector<ll>& DAG){ int N=connect.size(); vll dist(N,-1); ll maxD=0; REP(i,N){ if(dist[DAG[i]]<0) dist[DAG[i]] = 0; for(auto& x:connect[DAG[i]]){ dist[x.first] = max(dist[x.first],dist[DAG[i]]+x.second); maxD = max(maxD,dist[x.first]); } } return maxD; } int main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); int N=in(); vvll L(N,vll(N,-1)); int index=0; REP(i,N){ FOR(j,i+1,N){ L[i][j] = index++; L[j][i] = L[i][j]; } } ConnectNodeInfo connect(index); REP(i,N){ ll s=in()-1; REP(j,N-2){ ll t=in()-1; connect.AddDirectionalConnection(L[i][s],L[i][t],1); s = t; } } auto DAG = TopologicalSort(connect); if(DAG.empty()){ out(-1); return 0; } ll maxD = CalcLongestPathOnDAG(connect,DAG); out(maxD+1); return 0; }
1
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(ll (i) = (0);(i) < (n);++i) #define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;} #define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;} #define ALL(v) v.begin(),v.end() #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 1000000007 typedef long long ll; typedef pair<ll,ll> P; char mp[2222][2222]; bool ng[2222][2222]; int high[2222][2222]; int seica(ll h,ll w){ ll ret = 0; REP(i,h){ stack<pair<ll,ll>> st; REP(j,w){ ll tmp = j; while(!st.empty() && st.top().FI > high[i][j]){ auto now = st.top(); st.pop(); ret = max(ret,(j - now.SE) * now.FI); tmp = min(tmp,now.SE); //cout << i << " " << j << " " << now.FI << " " << now.SE << " " << (j - now.SE) * now.FI << endl; } if(st.empty() || st.top().FI < high[i][j])st.push(MP(high[i][j],tmp)); if(st.empty() || st.top().FI < i + 1)st.push(MP(i + 1,j)); } while(!st.empty()){ auto now = st.top(); st.pop(); ret = max(ret, (w - now.SE) * now.FI); //cout << i << " " << w << " " << now.FI << " " << now.SE << " " << (w - now.SE) * now.FI << endl; } } return ret; } int main(){ int h,w;cin >> h >> w; REP(i,h){ REP(j,w){ cin >> mp[i][j]; } } REP(i,h-1){ REP(j,w-1){ int y = i + 1; int x = j + 1; int cou = 0; if(mp[y][x] == '#')cou++; if(mp[y][x-1] == '#')cou++; if(mp[y-1][x] == '#')cou++; if(mp[y-1][x-1] == '#')cou++; if(cou % 2)ng[y][x] = true; } } REP(i,h){ REP(j,w){ if(i == 0)high[i][j] = 1; else { if(ng[i][j])high[i][j] = 1; else high[i][j] = high[i-1][j] + 1; } } } cout << seica(h,w) << endl; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fore(b,c) for(int val0=b;val0<c;val0++) #define forr(k,c,s) for(int k=c;k<s;k++) #define pb push_back #define mmp make_pair #define oset(T) tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef pair<int,int> ii; typedef long long ll; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; typedef long double ld; typedef vector<vii> al; typedef vector<ll> vl; const int INF = 1e9; const ll INFL = 1LL<<61; int g[2020][2020],h[2020][2020]; ii dp[2020][2020]; int main() { ios::sync_with_stdio(0);cout.precision(20);cout.tie(0);cin.tie(0); int n,m; cin >> n >> m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin >> c; g[i][j] = (c == '#'); } } for(int i=0;i<n-1;i++) { for(int j=0;j<m-1;j++) { h[i][j] = 1-((g[i][j] + g[i+1][j] + g[i][j+1] + g[i+1][j+1])%2); } } int ma = max(n,m); for(int i=0;i<n;i++) { for(int j=0;j<m-1;j++) { if(h[i][j]) { if(i > 0) { h[i][j] = max(h[i-1][j]+1,2); } else { h[i][j] = 2; } } } stack<int> st; int v,mc = 0; int id = 0; while(id < m-1) { if(st.empty() || h[i][st.top()] <= h[i][id]) { st.push(id++); } else { v = h[i][st.top()]; st.pop(); mc = v*(id+1); if(!st.empty()) { mc = v*(id-st.top()); } ma = max(ma,mc); } } while(!st.empty()) { v = h[i][st.top()];st.pop(); mc = v*(id+1); if(!st.empty()) { mc = v*(id-st.top()); } ma = max(ma,mc); } } cout << ma << '\n'; }
1
#include<bits/stdc++.h> const int mod = 1e9 + 7; using namespace std; int main(){ int N, M; cin >> N >> M; vector<int> A(N), B(M); for(int i = 0; i < N; ++i) cin >> A[i]; for(int j = 0; j < M; ++j) cin >> B[j]; sort(A.begin(), A.end(), greater<int>()); sort(B.begin(), B.end(), greater<int>()); long long p = 1; for(int d = N * M, i = 0, j = 0; d; --d){ if(i < N && j < M && A[i] == d && B[j] == d){ ++i; ++j; }else if(i < N && A[i] == d){ p *= j; ++i; }else if(j < M && B[j] == d){ p *= i; ++j; }else{ p *= (d - N * M + i * j); } p %= mod; } cout << (p + mod) % mod << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 7; long long M = 1e9 + 7; int a[N], b[N], n, m; long long res = 1; long long mrk[N * N]; vector <pair <int, int> > v; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j < m; j++) cin >> b[j]; for (int i = 0; i < n; i++) for (int ii = i + 1; ii < n; ii++) if (a[ii] == a[i]) { cout << "0"; return 0; } for (int j = 0; j < m; j++) for (int jj = j + 1; jj < m; jj++) if (b[jj] == b[j]) { cout << 0; return 0; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[i] == b[j]) mrk[a[i]] = 1; for (int i = 0; i < n; i++) { if (mrk[a[i]]) continue; for (int j = 0; j < m; j++) if (b[j] > a[i]) mrk[a[i]]++; } for (int j = 0; j < m; j++) { if (mrk[b[j]]) continue; for (int i = 0; i < n; i++) if (a[i] > b[j]) mrk[b[j]]++; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) v.push_back({i, j}); sort(v.begin(), v.end(), [](pair <int, int> x, pair <int, int> y) { return min(a[x.first], b[x.second]) < min(a[y.first], b[y.second]); });/* for (int i = 0; i < v.size(); i++) cout << i << " v[i] " << v[i].first << " " << v[i].second << endl; */ for (int pnt = n * m; pnt >= 1; pnt--) { if (mrk[pnt]) { res *= mrk[pnt]; res %= M; // cout << pnt << " RES :::::" << res << " " << mrk[pnt] << endl; continue; } int l = -1, r = v.size(), md; while (r - l > 1) { md = (r + l) / 2; if (min(a[v[md].first], b[v[md].second]) > pnt) r = md; else l = md; } res *= (v.size() - r - (n * m - pnt)); res %= M; // cout << pnt << " RES " << res << " R " << r << " " << (v.size() - r - (n * m - pnt)) << endl; } cout << res; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; //par[i]:iの親の番号 //初めは全てが根であるとして初期化する UnionFind(int N): par(N) { for (int i=0; i<N; i++) par[i]=i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main() { int V, E; cin >> V >> E; vector<vector<int>> G(E, vector<int>(2)); priority_queue<vector<int>, vector<vector<int>> ,greater<vector<int>>> W; for (int i=0; i<E; i++) { vector<int> w(3); cin >> w[1] >> w[2] >> w[0]; W.push(w); } UnionFind tree(V); int answer = 0; while(!W.empty()) { vector<int> w = W.top(); W.pop(); if (!tree.same(w[1], w[2])) { tree.unite(w[1], w[2]); answer+=w[0]; } } cout << answer << endl; }
#include <iostream> #include <vector> #include <algorithm> #include<string> using namespace std; struct Edge{ int P1; int P2; int Wei; }; int main(void){ vector<Edge> EdgeCol; int V, E; cin >> V >> E; if (V == 1){cout << 0<<endl;return 0;} vector<int>Select(V,0); for (int i = 0; i < E; ++i){ int s, t, w; cin >> s >> t >> w; EdgeCol.push_back(Edge{ s, t, w }); } sort(EdgeCol.begin(), EdgeCol.end(), [](Edge E1, Edge E2){return E1.Wei > E2.Wei; }); int WeiSum = 0; int a=1; while (1){ int aP1 = Select[EdgeCol.back().P1]; int aP2 = Select[EdgeCol.back().P2]; if (aP1 && aP2){ if (aP1 != aP2){ replace(Select.begin(), Select.end(), aP2, aP1); WeiSum += EdgeCol.back().Wei; } } else if (!aP1 && !aP2){ Select[EdgeCol.back().P1] = a; Select[EdgeCol.back().P2] = a; a++; WeiSum += EdgeCol.back().Wei; } else{ Select[EdgeCol.back().P1] = max(aP1, aP2); Select[EdgeCol.back().P2] = max(aP1, aP2); WeiSum += EdgeCol.back().Wei; } EdgeCol.pop_back(); if (all_of(Select.cbegin(), Select.cend(), [=](int s){return s == *Select.cbegin();})){ cout << WeiSum << endl; return 0; } } return 0; }
1
/** *Makacha **/ #include <bits/stdc++.h> using namespace std; template <typename T> inline void read (T &x) {bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-'); if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;} template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); } template <typename T> inline void write(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar(' ');} template <typename T> inline void writeln(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar('\n');} inline char readch() {char c; do c = getchar(); while (c == '\n' || c == ' '); return c;} inline void reads(string &s) {char c; while((c=getchar())==' '||c =='\n'); s=c; while((c=getchar())!=' '&&c!='\n') s+=c;} inline void getlines(string &s) {char c; while((c=getchar())=='\n'); s=c; while((c=getchar())!='\n') s+=c;} typedef const int csint; #define fi first #define se second #define mp make_pair typedef long long ll; typedef long double ld; typedef pair <int, int> ii; typedef vector <int> vi; const ld pi = acos(-1); const ll inf = 1e9, mod = 1e9 + 7, N = 1011; int n, a, b, c, d; ll f[N][N], cb[N][N], g[N][N]; ll fac[N], inv[N]; void Inc(ll &a, ll b) { a += b; if (a >= mod) a -= mod; } ll pw(ll a, ll b) { if (b == 0) return 1; ll t = pw(a, b / 2); t = t * t % mod; if (b % 2) return t * a % mod; return t; } int res, dx[111], pa[111]; void Try(int x) { if (x > n) { int cnt[21], num[21]; memset(cnt, 0, sizeof(cnt)); memset(num, 0, sizeof(num)); for (int i = 1; i <= n; i++) cnt[pa[i]]++; for (int i = 1; i <= n; i++) { if (!cnt[i]) continue; if (cnt[i] < a || cnt[i] > b) return; num[cnt[i]]++; } for (int i = 1; i <= n; i++) { if (!num[i]) continue; if (num[i] > d || num[i] < c) return; } res++; return; } for (int i = 1; i <= x; i++) { if (dx[i]) continue; dx[x] = i != x; pa[x] = i; Try(x + 1); dx[x] = 0; } } int main() { #ifdef makacha freopen("m.inp", "r", stdin); freopen("m.out", "w", stdout); #endif // makacha read(n); read(a); read(b); read(c); read(d); // Try(1); // cout << res << '\n'; cb[0][0] = 1; for (int i = 1; i <= n; i++) { cb[i][0] = 1; for (int j = 1; j <= i; j++) cb[i][j] = (cb[i - 1][j] + cb[i - 1][j - 1]) % mod; } g[0][0] = 1; for (int i = 1; i <= n; i++) { g[i][0] = 1; for (int j = i; j <= n; j += i) g[i][j] = (ll) cb[j][j / i] * g[i - 1][j - j / i] % mod; } fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (ll)fac[i - 1] * i % mod; inv[n] = pw(fac[n], mod - 2); for (int i = n; i >= 1; i--) inv[i - 1] = (ll)inv[i] * i % mod; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j += i) g[i][j] = (ll)g[i][j] * inv[i] % mod; f[a - 1][0] = 1; for (int i = a; i <= b; i++) { int lim = min(d * i, n); int im = i - 1; for (int j = i * c, grs = c; j <= lim; j += i, grs++) for (int k = j, rem = n, kk = k - j; k <= n; k++, rem--, kk++) { ll tmp = f[im][kk]; tmp = tmp * cb[rem][j] % mod; tmp = tmp * g[grs][j] % mod; Inc(f[i][k], tmp); } for (int j = 0; j <= n; j++) Inc(f[i][j], f[im][j]); } cout << f[b][n]; }
#include <iostream> #include <string> #include <vector> #include <deque> #include <queue> #include <algorithm> #include <set> #include <map> #include <bitset> #define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c)) #define vvi std::vector<std::vector<int> > #define vvl std::vector<std::vector<ll> > #define MODs 1000000007; typedef long long int ll; using namespace std; //////////////////////////////// #define P 1000000007 #define N_MAX 2000000 typedef long long int ll; ll fac[N_MAX+1]; ll inv[N_MAX+1]; ll finv[N_MAX+1]; ll comb(ll n, ll k){ return (((fac[n]*finv[n-k])%P)*finv[k])%P; } void init(){ fac[0] = finv[0] = fac[1] = finv[1] = inv[1] = 1; for(int i = 2; i <= N_MAX; i++){ fac[i] = (fac[i-1]*i)%P; inv[i] = ((-(P/i)*inv[P%i])%P+P)%P; finv[i] = (finv[i-1]*inv[i])%P; } } ll pp(ll a, ll b){ return (a * b)%P; } ll pd(ll a, ll b){ return (a * inv[b])%P; } ////////////////////////////////////// int main(int argc, char const *argv[]) { ll N, A, B, C, D; init(); std::cin >> N >> A >> B >> C >> D; vvl dp = vv(B+1, N+1, 0, ll); dp[A-1][0] = 1; for(int i=A;i<=B;i++){ ll tmp = 1; for(int j=0;j<N+1;j++) dp[i][j] = dp[i-1][j]; for(int j=0;j<C;j++) tmp = pp(tmp, finv[i]); for(int j=C;j<=D;j++, tmp = pp(tmp, finv[i])){ for(int k=0;k+i*j<N+1;k++){ dp[i][i*j+k] = (dp[i][i*j+k] + pp(pp(dp[i-1][k], pp(pp(fac[N-k], tmp), finv[N-k-i*j])), finv[j]))%P; } } } /* for(int i=1;i<=B;i++){ for(int j=0;j<=N;j++) std::cout << dp[i][j] << " "; std::cout << '\n'; } */ std::cout << dp[B][N] << '\n'; return 0; }
1
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif // C++ #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif #define int long long #define rng(i,l,r) for(size_t i=(l);i<(r);++i) #define rep(i,n) rng(i,0,n) #define gnr(i,l,r) for(size_t i=(r)-1;i>=(l);i--) #define per(i,b) gnr(i,0,b) #define ALL(obj) (obj).begin(), (obj).end() //1,2,3,... #define rALL(obj) (obj).rbegin(), (obj).rend()//...,3,2,1 using namespace std; //constant const int MOD = 1e9+7; const int INF = 1e18; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1,1,0,-1,-1,-1,0,1}; const int dy8[8] = {0,1,1,1,0,-1,-1,-1}; const double pi = acos(-1); 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 gcd(int a,int b){return b?gcd(b,a%b):a;} //*max_element(ALL(vector)) //count(ALL(vector),num) -> n //memset(obj,value,sizeof(obj)); map<int,int> result; void prime_factorize(int n){ for (int factor = 2; factor*factor <= n; factor++) { if(n%factor != 0) continue; int power = 0; while(n%factor == 0){ power++; n /= factor; } result[factor] += power; } if (n != 1) result[n] += 1; } void solve(){ // remove the bottom 3 lines when you submit this code. std::ifstream in("sample.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { prime_factorize(i); } int ans = 1; for (auto itr = result.begin(); itr != result.end(); itr++) { ans *= (itr->second+1); ans %= MOD; } cout << ans << endl; } signed main() { solve(); return 0; }
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> #include <chrono> #include<unordered_map> using namespace std; #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(u) (u).begin(),(u).end() #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef tuple<int, int, int> TIII; typedef vector<int> VI; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int MN = 1e9; int main() { fast_io(); map<PII, int> M; int N; scanf("%d", &N); REP(i, N) { double A; scanf("%lf", &A); LL llA = llround(A * MN); // wylicz ile zawieraja 2 i 5 int x = 0, y = 0; while (llA % 2 == 0) { x++; llA /= 2; } while (llA % 5 == 0) { y++; llA /= 5; } x = min(x, 18), y = min(y, 18); M[MP(x, y)]++; } LL res = 0; // zobacz ile jest par, ktore maja sume x >= 0 i sume y >= 0 for (const auto &p : M) { if (p.first.first >= 9 && p.first.second >= 9) res += p.second*(LL)(p.second - 1) / 2; auto it = M.find(p.first); it++; while (it != M.end()) { if (p.first.first + it->first.first >= 18 && p.first.second + it->first.second >= 18) res += p.second*(LL)it->second; it++; } } printf("%lld\n", res); return 0; } // FOR GNU C++ use the following pattern: // Uncomment the code below and change your main into main2 // It does not build in MS C++ // But it does increase the stack size from 256 MB on CF and uses GNU C++ //#include <Processthreadsapi.h> //#include <iostream> //#include <Synchapi.h> //#include <windows.h> //#include <process.h> // //DWORD WINAPI MyThreadFunction(LPVOID lpParam) { // main2(nullptr); // return 0; //} //int main() { // auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr); // WaitForSingleObject(h, INFINITE); //}
0
#include<iostream> #include<iomanip> #include<cassert> #include<stdexcept> #include<utility> #include<functional> #include<numeric> #include<cmath> #include<algorithm> #include<cstdio> #include<cstdlib> #include<array> #include<stack> #include<queue> #include<deque> #include<vector> #include<complex> #include<set> #include<map> #include<unordered_map> #include<unordered_set> #include<string> #include<bitset> #include<memory> using namespace std; using ll=long long; const int SIZE=18; const ll MOD=1e9+7; void add(ll &lhs,ll rhs){ lhs=(lhs+rhs)%MOD; } int main(){ int n; cin>>n; int xyz[3]; for(int i=0;i<3;i++) cin>>xyz[i]; vector<vector<ll>> dp(n+1,vector<ll>(1<<SIZE)); dp[0][(1<<SIZE)-1]=1; vector<int> isAccept(1<<SIZE); for(int bit=0;bit<(1<<SIZE);bit++){ int st=0; int buf=0; for(int i=0;i<SIZE;i++){ buf++; if(!((bit>>i)&1) && xyz[st]==buf){ st++; buf=0; if(st==3){ isAccept[bit]=true; break; } } } } vector<ll> pow10(n); pow10[0]=1; for(int i=0;i+1<n;i++) pow10[i+1]=pow10[i]*10%MOD; ll res=0; int mask=(1<<SIZE)-1; for(int i=0;i<n;i++){ for(int bit=0;bit<(1<<SIZE);bit++){ for(int v=1;v<=10;v++){ int nex=(bit<<v)+(1<<(v-1))-1; nex&=mask; add(dp[i+1][nex],dp[i][bit]); } } for(int bit=0;bit<(1<<SIZE);bit++){ if(isAccept[bit]){ ll r=pow10[n-1-i]; res+=r*dp[i+1][bit]; res%=MOD; dp[i+1][bit]=0; } } } cout<<res<<endl; return 0; }
#include<iostream> #include<iomanip> #include<cstdio> #include<string> #include<cstring> #include<climits> #include<algorithm> #include<vector> #include<cmath> #include<map> #include<set> #include<stack> #include<queue> #include<deque> #include<tuple> #include<list> #include<unordered_map> #define ll long long #define pii pair<int,int> #define pll pair<ll,ll> #define vii vector<int> #define vll vector<ll> #define lb lower_bound #define pb push_back #define mp make_pair #define fi first #define sc second #define rep(i,n) for(ll i=0;i<n;i++) #define rep2(i,a,b) for(ll i=a;i<b;i++) #define repr(i,n) for(ll i=n-1;i>=0;i--) #define all(x) x.begin(),x.end() #define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin()) #define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin()) #define ERASE(v) v.erase(unique(v.begin(),v.end()),v.end()) // #define int ll using namespace std; const int INF (1 << 30); const ll LLINF (1LL << 55LL); const int MOD = 1000000007; const int MAX = 510000; const double pi = acos(-1); const double eps = 1e-9; ll C[3000][3000]; void com(){ C[0][0]=1; for(ll i=1;i<2010;i++){ for(ll j=0;j<i+1;j++){ if(j==0||j==i) C[i][j]=1; else{ C[i][j]=C[i-1][j-1]+C[i-1][j]; C[i][j]%=1000000007; } } } } int main(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); com(); ll n,x,y,z,ans=1; ll dp[50][1<<19]; cin>>n>>x>>y>>z; rep(i,n){ ans*=10; ans%=MOD; } ll ng = 0; ng |= 1<<z; ng |= 1<<(y+z); ng |= 1<<(x+y+z); ll mask = 1<<(x+y+z+1); mask--; rep(i,42)rep(j,(1<<19)) dp[i][j]=0; dp[0][1]=1; rep(i,n){ rep(bit,mask+1){ rep2(j,1,11){ ll ns = ((bit<<j)&mask)+1; if((ns&ng) == ng) continue; dp[i+1][ns] += dp[i][bit]; dp[i+1][ns] %= MOD; } } } rep(i,mask){ ans-=dp[n][i]; ans%=MOD; } cout<<(ans+MOD)%MOD<<endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define sz(x) (int)x.size() #define int long long typedef pair<int,int>pii; typedef vector<int> vi; typedef vector<vi> vii; const int mxN=2e5+5,M=1e9+7; const int LINF = 0x3f3f3f3f3f3f3f3f; //did u check when n=1? //is mxN correct? set<int>s; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); for(int i=0;i<3;i++){ int x;cin>>x; s.insert(x); } if(sz(s)!=2)cout<<"No"; else cout<<"Yes"; }
#include<bits/stdc++.h> #define int long long using namespace std; signed main(){ int N,n,ans=0; cin>>N>>n; if(n){ puts("-1"); return 0; } for(int i=1;i<N;i++){ int a; cin>>a; if(n>=a)ans+=a; else if(n+1==a)ans++; else{ puts("-1"); return 0; } n=a; } cout<<ans<<endl; }
0
#include "bits/stdc++.h" #define ll long long #define mp(a, b) make_pair(a, b) using namespace std; typedef pair<ll, ll> pairs; typedef pair<ll, pairs> tpl; ll c[4]; double dp[301][301][301]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, a; cin >> n; for (ll i = 0; i < n; i++) cin >> a, c[a]++; dp[0][0][0] = 0; for (ll i = 0; i <= n; i++) { for (ll j = 0; j <= n; j++) { for (ll k = 0; k <= n; k++) { double z = n - (i + j + k), val = 1; if (z == n) continue; if (i + j + k > n) continue; if (i > 0) val += (1.0 * i / n) * dp[i - 1][j + 1][k]; if (j > 0) val += (1.0 * j / n) * dp[i][j - 1][k + 1]; if (k > 0) val += (1.0 * k / n) * dp[i][j][k - 1]; if (z > 0) val /= (1.0 - 1.0 * z / n); dp[i][j][k] = val; } } } cout << setprecision(9) << fixed << dp[c[3]][c[2]][c[1]]; }
#include <bits/stdc++.h> using namespace std; #define long long ll #define f(i,n) for(int i=0;i<n;i++) const int maxn=305; double dp[maxn][maxn][maxn]; double recur(int k, int b, int c,int n){ if(dp[k][b][c]>=0)return dp[k][b][c]; if(k==0&&b==0&&c==0)return 0.0; double res=0.0; // dp[k][b][c]=0.0; if(k>0)res+=(k)*recur(k-1,b,c,n); if(b>0)res+=(b)*recur(k+1,b-1,c,n); if(c>0)res+=(c)*recur(k,b+1,c-1,n); // dp[k][b][c]/=(n-k-c-b); res+=n; res*=1.0/(k+b+c); return dp[k][b][c]=res; } int main() { cout<<fixed<<setprecision(10); int n; cin>>n; vector<int>a(4,0); f(i,n){int x; cin>>x; a[x]++; } memset(dp, -1, sizeof(dp)); cout<<fixed<<setprecision(9)<<recur(a[1],a[2],a[3],n)<<endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(){ string s; int ans, t; cin >> s; ans = 1000; for ( int i = 2;i < s.size(); i++ ) { t = (s[i-2] - '0') * 100 + (s[i-1] - '0') * 10 + s[i] - '0'; ans = min(ans, abs(t-753)); } cout << ans << endl; return (0); }
#include<bits/stdc++.h> int main() { int arr[26]{}; std::string name; std::cin >> name; int n=name.size(); bool yes = 1; for (int x = 0; x < n; x += 2) { if (name.substr(x, 2) != "hi") { yes = false; } } std::cout << (yes == 1 ? "Yes" : "No"); }
0
#include <bits/stdc++.h> #define loop(n, i) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define HERE cout << "HERE: " << __LINE__ << endl; #define INSP(v) cout << v << " at " << __LINE__ << endl; using namespace std; typedef long long ll; int main() { int m, min, max; while (cin >> m >> min >> max && m) { vector<int> v; loop(m, i) { int j; cin >> j; v.push_back(j); } int gap = 0; int ans = 0; for (int i = min; i <= max; i++) { if (gap <= v[i-1] - v[i]) { gap = v[i-1] - v[i]; ans = i; } } cout << ans << endl; } }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define prior priority_queue #define MOD 1000000007 #define INF64 (long long)1e18 #define INF (int)1e9 #define PI 3.1415926535897932384626433832795 #define ll long long #define ld long double #define ret return #define NUM 1000001 const int SIZE = 2e5+4; void fastio(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void input(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } void swap(int& a, int& b){ int t=a; a = b; b = t; } void solveTest(){ int A,B,M; cin>>A>>B>>M; int a[A+1],b[B+1]; int minA = INT_MAX; int minB = INT_MAX; for(int i=1;i<=A;i++){ cin>>a[i]; if(a[i]<minA){ minA=a[i]; } } for(int i=1;i<=B;i++){ cin>>b[i]; if(b[i]<minB){ minB = b[i]; } } int minprice = INT_MAX; for(int i=0;i<M;i++){ int x,y,c; cin>>x>>y>>c; if((a[x]+b[y]-c)<minprice){ minprice = a[x]+b[y]-c; } } cout<<min(minA+minB,minprice)<<endl; return ; } int main(){ fastio(); //input(); int t=1; //cin>>t; while(t--){ solveTest(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ long int n,x; cin >> n >> x; vector<long int> a(n); long int sum=0; int i; for(i=0; i<n; i++){ cin >> a.at(i); sum+=a.at(i); } sort(a.begin(),a.end()); if(x>sum) cout << n-1 << endl; else if(x==sum) cout << n << endl; else{ for(i=0; i<n; i++){ sum-=a.at(n-1-i); if(x>=sum) break; } cout << n-1-i << endl; } }
///Bismillahir Rahmanir Rahim #include<bits/stdc++.h> #define int long long #define fi first #define si second #define mp make_pair #define pb push_back #define pi pair<int,int> #define clr(x) memset(x,0,sizeof(x)); #define f(i,l,r) for(int i=l;i<=r;i++) #define rf(i,r,l) for(int i=r;i>=l;i--) #define done(i) cout<<"done = "<<i<<endl; #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; const int inf=1e18; const int mod=1e9+7; const int M=100009; inline int bigmod(int B,int P){int R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;} inline int ad(int x,int y){int ret=(x%mod+y%mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;} inline int sub(int x,int y){int ret=((x%mod)-(y%mod)+mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;} inline int gun(int x,int y){int ret=((x%mod)*(y%mod))%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;} main() { fast int n,k,x,mx=0; cin>>n>>k; int g=0; f(i,1,n) { cin>>x; mx=max(mx,x); g=__gcd(x,g); } if(k%g==0 && k<=mx) { cout<<"POSSIBLE"<<endl; } else { cout<<"IMPOSSIBLE"<<endl; } return 0; }
0
#include<iostream> #include<algorithm> #include<climits> #include<cmath> #include<cstdio> #include<cstdlib> #include<ctime> #include<string> #include<cstring> #include<vector> #include<stack> #include<queue> #include<set> #include<bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for(i = 0;i < n;++i) #define rep1(i, n) for(i = 1;i < n;++i) #define per(i, n) for(i = n - 1;i > -1;--i) #define int(x) int x; scanf("%d",&x) #define int2(x, y) int x, y; scanf("%d%d",&x, &y) #define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z) #define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z) #define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z) #define ll2(x, y) ll x, y; cin >> x >> y; #define scn(n, a) rep(i, n)cin >> a[i] #define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i] #define pri(x) cout << (x) << "\n" #define pri2(x, y) cout << (x) << " " << (y) << "\n" #define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(),(a).end() #define endl "\n" #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << (x) << "\n" #define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n" #define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n" #define X first #define Y second #define eps 0.0001 #define prid(x) printf("%.15lf\n", x) string s; int res = 0; int cnt[3] = {0}; signed main(void){ int i, j; for(int testcase = 0;testcase >= 0;testcase++){ cin >> s; s += 'J'; int mode = 1; rep(i, s.size()){ if(mode == 1){ if(s[i] == 'J')++cnt[0]; else if(s[i] == 'O'){ mode = 2; ++cnt[1]; }else cnt[0] = 0; }else if(mode == 2){ if(s[i] == 'J'){ mode = 1; rep(j, 2)cnt[j] = 0; ++cnt[0]; }else if(s[i] == 'O')++cnt[1]; else{ mode = 3; ++cnt[2]; } }else{ if(s[i] == 'J'){ if(cnt[0] >= cnt[1] && cnt[1] <= cnt[2])res = max(res, cnt[1]); mode = 1; rep(j, 3)cnt[j] = 0; ++cnt[0]; }else if(s[i] == 'O'){ if(cnt[0] >= cnt[1] && cnt[1] <= cnt[2])res = max(res, cnt[1]); mode = 1; rep(j, 3)cnt[j] = 0; }else ++cnt[2]; } //printf("%d %d %d %d %d\n", i, mode, cnt[0], cnt[1], cnt[2]); } pri(res); /*/ //*/ break; } return 0; }
#include<iostream> using namespace std; int main(){ string s; cin >> s; int n = s.size(); int ans = 0; for(int i=0;i<n;i++){ if(s[i] == 'O'){ int j = 0; while( (i+j<n) && (s[i+j] == 'O') )j++; int k = 0; while( (0<=i-1-k) && (k<j) && (s[i-1-k] == 'J'))k++; int l = 0; while( (i+j+l<n) && (l<j) && (s[i+j+l] == 'I'))l++; if(j==k && j==l)ans = max(ans,j); i += j+l-1; } } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define repo(i,n) for(int i = 1; i < (int)(n); i++) #define pb push_back #define mp make_pair #define np next_permutation #define lb lower_bound #define ub upper_bound #define fi first #define se second #define all(x) (x).begin(),(x).end() #define mod 1000000007 #define pi acos(-1.0) const ll INF = 1LL<<61; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } ll jou(ll N, ll P){if(P==0) return 1; if(P%2==0){ll t = jou(N, P/2);return t*t % mod; }return (N * jou(N, P-1)) % mod;} //jou(n,mod-2)で逆元 //intの最大値2147483647 ≒ 2×10^9 //long longの最大値9223372036854775807 ≒ 9×10^18 //'大文字'+=32; で小文字に // cout << fixed << setprecision (20); 小数点以下20桁まで //実行時間制約2秒では2×10^8回くらいまで計算できる int main(){ ll n; cin>>n; vector<ll> p(n); rep(i,n){ cin>>p[i]; } ll ans=1; vector<ll> q={0,0,0}; rep(i,n){ int cnt=0; int memo; rep(j,3){ if(p[i]==q[j]) { cnt++; memo=j; } } if(cnt==0){ cout << 0 << endl; return 0; } ans*=cnt; ans%=mod; q[memo]++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, a, b) for(int i = a; i < b; 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>; 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; } const int mod = 1e9+7; int main(){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; vector<int> cnt(n); ll ans = 1; rep(i,n){ if (a[i] == 0){ ans *= 3 - cnt[a[i]]; }else{ ans *= (cnt[a[i]-1] - cnt[a[i]]); } cnt[a[i]]++; ans %= mod; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; int64_t res = 0; vector table(20, vector(20, 0)); for (int i = 0; i < N; i++) { string s; cin >> s; auto pos = s.find("."); if (pos == string::npos) { s += string(9, '0'); } else { s.erase(s.begin() + pos); s += string(9 - ((int)s.size() - pos), '0'); } int64_t x = static_cast<int64_t>(stoll(s)); pair<int, int> a = make_pair(0, 0); while (x % 2 == 0) { x /= 2; a.first++; } while (x % 5 == 0) { x /= 5; a.second++; } a.first = min(a.first, 18); a.second = min(a.second, 18); for (int j = 18 - a.first; j < 20; j++) { for (int k = 18 - a.second; k < 20; k++) { res += table[j][k]; } } table[a.first][a.second]++; } cout << res << '\n'; return 0; }
// // main.cpp // ALDS1_4_A // // Created by ptyuan on 2018/10/22. // Copyright © 2018 YY. All rights reserved. // #include <iostream> #include <cstdio> using namespace std; int linearSearch(int *array, int n, int key) { *(array + n ) = key; int i = 0; while (array[i] != key) { i++; } return i == n ? -1 : i; } int main(int argc, const char * argv[]) { int n = 0; int S[10000 + 1] = {0}; int q = 0; int sum = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", S + i); } scanf("%d", &q); for (int i = 0; i < q; i++) { int key = 0; scanf("%d", &key); if (-1 != linearSearch(S, n, key)) { sum++; } } printf("%d\n", sum); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> p(n); int cnt = 0, min = 200001; for (int i = 0; i < n; i++) { cin >> p.at(i); if (p.at(i) <= min) cnt++; if (p.at(i) < min) min = p.at(i); } cout << cnt << endl; return 0; }
//...Bismillahir Rahmanir Rahim... // Code by Asad Bin Saber #include <bits/stdc++.h> using namespace std; // typedefs... typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<ll, ll> pll; // constants... const double PI = acos(-1); const int Mod = 1e9+7; // 998244353; const int MXS = 2e5+5; const int MXI = 1e9+5; const ll MXL = 1e15+5; const int INF = 1e5+5; // defines... #define MP make_pair #define PB push_back #define fi first #define se second #define si(a) scanf("%d", &a) #define sii(a, b) scanf("%d%d", &a, &b) #define siii(a, b, c) scanf("%d%d%d", &a, &b, &c) #define sl(a) scanf("%lld", &a) #define sll(a, b) scanf("%lld%lld", &a, &b) #define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c) #define sz(x) (int)x.size() #define all(x) begin(x), end(x) // chess moves... // int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right // int dy[] = {+1, -1, +0, +0}; // int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move // int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; // int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; ///Knight's Move // int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; // structs... struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; // functions... ll gcd(ll a, ll b) { while (b) { a %= b; swap(a, b); } return a; } ll lcm(ll a, ll b) { return (a/gcd(a, b)*b); } ll comb(ll a, ll b, ll M) { ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans *= K; ans /= L; } return ans % M; } void task() { //code here... int n, ans, mn = MXI; cin >> n; int ara[n]; for(int K=0; K<n; K++) cin >> ara[K]; ans = n; for(int K=0; K<n; K++){ mn = min(mn, ara[K]); if(mn<ara[K]) ans--; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); task(); return 0; }
1
#include<iostream> #include<cstdio> #include<vector> #include<string> #include<cstring> #include<functional> #include<stack> #include<queue> #include <iomanip> #include<map> #include<limits> #include<cmath> #include<algorithm> #include<bitset> #include<utility> #include<complex> #include<cstdlib> #include<set> #include<cctype> #define DBG cerr << '!' << endl; #define REP(i,n) for(int (i) = (0);(i) < (n);++i) #define rep(i,s,g) for(int (i) = (s);(i) < (g);++i) #define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i)) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;} #define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;} #define ALL(v) v.begin(),v.end() #define Decimal fixed<<setprecision(10) using namespace std; typedef long long ll; typedef vector<int> iv; typedef vector<iv> iiv; typedef vector<string> sv; #define N 262144 ll dp[2][2*N+5]; int n,q,a,b; #define INM 100000000000000000LL //0-index void update(int id,int k,ll a) { k += N-1; dp[id][k] = a; while(k > 0) { k = (k-1)/2; dp[id][k] = min(dp[id][k*2+1],dp[id][k*2+2]); } } ll query(int id,int a,int b,int k=0,ll l=0,ll r=N) { if(r <= a || b <= l)return INM; if(a <= l && r <= b)return dp[id][k]; else { ll vr = query(id,a,b,k*2+1,l,(l+r)/2); ll vl = query(id,a,b,k*2+2,(l+r)/2,r); return min(vr,vl); } } int main() { cin >> n >> q >> a >> b; REP(i,2)REP(j,N)update(i,j,INM); vector<int> que;que.PB(a); REP(i,q) { int tmp;cin >> tmp;que.PB(tmp); } update(0,b,b); update(1,b,n-b); //cout << query(1,0,n+1) << endl; ll sum = 0; for(int i = 1;i <= q;i++) { ll tmp = abs(que[i-1]-que[i]); sum += tmp; ll mi = min(query(1,0,que[i]+1)-(n-que[i]),query(0,que[i],n+1)-que[i]); //cout << query(1,0,que[i]+1)-que[i]+1<< ' ' << query(0,que[i],n+1)-que[i] << endl; update(0,que[i-1],mi-tmp+que[i-1]); update(1,que[i-1],mi-tmp+n-que[i-1]); } ll ans = INM; REP(i,n+1) { ans = min(ans,dp[0][i+N-1]-i); ans = min(ans,dp[1][i+N-1]-(n-i)); } cout << ans + sum << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define fi first #define se second #define pb push_back using namespace std; ll n,arr[200010],a,b,c,ans=1e18,s1,s2,s3,s4; ll sum( ll l , ll r ){ return arr[r]-arr[l-1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for(ll i=1; i<=n; i++) cin >> arr[i] , arr[i]+=arr[i-1]; a = 1; c = 3; for(b=2; b<n-1; b++){ while( a+1 < b && abs( sum( 1 , a+1 )-sum( a+2 , b ) ) <= abs( sum( 1 , a ) - sum( a+1 , b ) ) ) a++; while( c <= b ) c++; while( c+1 < n && abs( sum( b+1 , c+1 )-sum( c+2 , n ) ) <= abs( sum( b+1 , c ) - sum(c+1,n)) ) c++; s1 = sum( 1 , a ); s2 = sum( a+1 , b ); s3 = sum( b+1 , c ); s4 = sum( c+1 , n ); //cout << a << " " << b << " " << c << " " << s1 << " " << s2 << " " << s3 << " " << s4 << "\n"; ans = min( ans , max( max( s1 , s2 ) , max( s3 , s4 ) ) - min( min( s1 , s2 ) , min( s3 , s4 ) ) ); } cout << ans; return 0; }
0
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 //#define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template < typename T > ostream &operator<<(ostream &os, const vector< T > &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector< vector< int > > &A) { int N = A.size(); for (int i = 0; i < N; i++) { for (int j = 0; j < A[i].size(); j++) os << A[i][j] << " "; os << endl; } return os; } template < typename T, typename U > ostream &operator<<(ostream &os, const pair< T, U > &p) { os << "(" << p.first << "," << p.second << ")"; return os; } typedef pair< int, int > pii; typedef long long ll; struct edge { int from, to, d, c, i; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector< edge > edges; typedef vector< edges > graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector< vector< flow > > flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template < typename T, typename U > bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template < typename T, typename U > bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } template < typename A, size_t N, typename T > void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> T; while (T--) { cin >> N; vector< int > A(N); rep(i, N) cin >> A[i]; string S; cin >> S; vector< int > es; int ans = 0; for (int i = N - 1; i >= 0; i--) { int v = A[i]; sort(es.rbegin(), es.rend()); for (auto e : es) { chmin(v, v ^ e); } if (S[i] == '0') { if (v != 0) { es.push_back(v); } } else { if (v) { ans = 1; break; } } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll int64_t #include <cstdlib> #include <math.h> #include<cstdio> #include<cstring> #define FOR(I, A, B) for (ll I = (A); I <= (B); I++) #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define fo(i,n) for(ll i=0;i<n;i++) #define sz(a) ll((a).size()) #define pb push_back #define all(c) (c).begin(),(c).end() #define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define dbg(x) cout << #x << " = " << x << endl #define dbg2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl #define dbg3(x,y,z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl #define dbg4(x,y,z,q) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << ", " << #q << " = " << q << endl #define scan(char_array) scanf("%[^\n]s",&char_array); struct three{ ll a,b,c; }; int main(){ ll n,q; IOS cin>>n>>q; std::vector<three> v; ll aa,bb,cc; auto compare=[&](three a,three b){ return a.a<b.a; }; fo(i,n){ three tmp; cin>>aa>>bb>>cc; tmp.a=cc; ll hi,lo; if(bb-1>=cc){ hi=bb-1-cc; } else hi=-1; lo=max((ll)0,aa-cc); tmp.b=lo; tmp.c=hi; v.pb(tmp); } map<ll,ll> d; sort(all(v),compare); set<ll> st; fo(i,q){ cin>>aa; d[aa]=-1; st.insert(aa); } for(auto kk:v){ //dbg3(kk.a,kk.b,kk.c); std::vector<ll> out; auto it=st.lower_bound(kk.b); while(it!=st.end()){ ll our=*it; if(our>=kk.b&&our<=kk.c){ out.pb(our); } else break; it++; } for(auto pp:out){ d[pp]=kk.a; st.erase(pp); } } for(auto ii:d){ cout<<ii.second<<endl; } }
0
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = int64_t; #define rep(i, j, n) for (int i = j; i < (int)n; ++i) constexpr ll MOD = 1000000007; int main() { ll n; cin >> n; ll a; vector<ll> one(60); rep(i, 0, n) { cin >> a; rep(j, 0, 60) if (a >> j & 1) one[j]++; } ll ans = 0; rep(i, 0, 60) ans = (ans + (1ll << i) % MOD * one[i] % MOD * (n - one[i]) % MOD) % MOD; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) #define repr(i, n) for(int i = n; i >= 0; i--) #define reps(i,a,b) for(int i= a; i < b; i++) #define INF 2e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; int n; int cnt = 62; vector<ll> vec; ll mod = 1e9+7; ll solve() { vector<vector<int>> sum_zero(n+1, vector<int>(cnt, 0)); vector<vector<int>> sum_one(n+1, vector<int>(cnt, 0)); ll ans = 0; reps(i, 1, n+1) { rep(j,cnt) { if (vec.at(i-1) >> j & 1ll) { sum_one.at(i).at(j) = sum_one.at(i-1).at(j) + 1; sum_zero.at(i).at(j) = sum_zero.at(i-1).at(j); } else { sum_one.at(i).at(j) = sum_one.at(i-1).at(j); sum_zero.at(i).at(j) = sum_zero.at(i-1).at(j) + 1; } } } rep(i, n-1) { ll t = 1; rep(j, cnt) { ll v; if (vec.at(i) >> j & 1ll) { v = (sum_zero.at(n).at(j) - sum_zero.at(i+1).at(j)); } else { v = (sum_one.at(n).at(j) - sum_one.at(i+1).at(j)); } ans += v * t; ans %= mod; t *= 2; t %= mod; } } return ans; } int main() { cin >> n; vec.resize(n); rep(i, n) cin >> vec.at(i); cout << solve() << endl; return 0; }
1
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2e5+500; int w[N],n; int main() { scanf("%d",&n);int ans=0; for(int i=1;i<=2*n;i++)scanf("%d",w+i); sort(w+1,w+n*2+1); for(int i=1;i<=2*n;i+=2)ans+=w[i]; cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int n,ans; ans=0; cin >> n; vector<string> s(3); for(int i=0;i<3;i++){ cin >> s[i]; } for(int i=0;i<n;i++){ if(s[0][i]==s[1][i] && s[0][i]==s[2][i]); else if(s[0][i]==s[1][i] || s[0][i]==s[2][i] || s[1][i]==s[2][i])ans++; else ans+=2; } cout << ans; return 0; }
0
#include<iostream> using namespace std; int main(){ int N,x; cin >> N >> x; int a[N]; for(int i=0; i<N; i++){ cin >> a[i]; } long long total = 0; for(int pairIndex=0; pairIndex<N-1; pairIndex++){ int first = pairIndex; int second = pairIndex+1; if(a[first] > x){ int diff = a[first] - x; a[first] -= diff; total += diff; } if(a[first] + a[second] > x){ int diff = a[first] + a[second] - x; a[second] -= diff; total += diff; } } cout << total << '\n'; }
#include "bits/stdc++.h" using ll = long long; using namespace std; void go(int x) { cout << x << endl; exit(0); } void solve() { int x, y; cin >> x >> y; if(x == 0 || y == 0) { if(x == 0) { if(y >= 0) go(y - x); else go(abs(y) + 1); } else if(y == 0) { if(x < 0) go(abs(x)); else go(abs(x) + 1); } } if(x < y) { if(x * 1LL * y >= 0) { go(y - x); } else { go(1 + abs(abs(y) - abs(x))); } } else if(x == y) { go(0); } else { //x > y; if(x * 1LL * y >= 0) { go(2 + abs(abs(x) - abs(y))); } else { go(1 + abs(abs(x) - abs(y))); } } } int main() { cin.tie(0); cin.sync_with_stdio(0); int testcase = 1; // cin >> testcase; for(int i = 0; i < testcase; i++) { solve(); } return 0; }
0
// // main.c // ITP1_5_D // // Created by ??±??°?????? on 2015/10/11. // Copyright ?? 2015??´ HamadaShogo. All rights reserved. // #include <stdio.h> void call(int n); int main(int argc, const char * argv[]) { int num; scanf("%d", &num); call(num); return 0; } void call(int n){ int i; int x; for (i=1; i<=n; i++) { x = i; if (x%3 == 0) { printf(" %d", i); } else{ while (x){ if (x%10 == 3) { printf(" %d", i); break; } x /= 10; } } } puts(""); }
#include <bits/stdc++.h> #define int long long #define fast_io(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define loop(i,a,b) for(int i=a;i<b;i++) #define all(x) x.begin(),x.end() #define us unordered_set #define um unordered_map #define ms multiset #define mm multimap #define pb push_back #define pf push_front #define ins insert #define mp make_pair #define mt make_tuple #define fi first #define se second #define _max max_element #define _min min_element #define tc(T) int T; cin>>T; while(T--) #define q(q) int q; cin>>q; while(q--) #define pi 3.1415926535897932384626433832 const int pinf=((int)9e18); const int ninf=((int)-9e18); const int mod=1000000007; using namespace std; int32_t main() { fast_io(); //FILE *fptr1=freopen("in.txt","r",stdin); //FILE *fptr2=freopen("out.txt","w",stdout); int r; cin>>r; double ans=2LL*pi*r; printf("%0.12f",ans); return 0; }
0
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); i++) #define rrep(i, a, b) for (int i = a; i >= (b); i--) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; using P = pair<int, int>; 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 hr() { cerr << "--------------" << endl; } const int INF = 1001001001; const int MOD = 1000000007; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> a(n); rep(i, 0, n) cin >> a[i]; int ans = 0; rep(l, 0, k + 1) rep(r, 0, k - l + 1) { if (l + r > n) continue; int d = k - l - r; int now = 0; vector<int> s; rep(i, 0, l) { now += a[i]; s.push_back(a[i]); } rep(i, n - r, n) { now += a[i]; s.push_back(a[i]); } sort(s.begin(), s.end()); rep(i, 0, d) { if (i >= s.size()) break; if (s[i] >= 0) break; now -= s[i]; } ans = max(ans, now); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; #define REP(i, n) for(int i = 0; i < n; i++) #define REP2(i, a, b) for(int i = a;i <= b;i++) #define REPR(i, a, b) for(int i = a; i >= b; i--) #define FOR(i, m, n) for(int i = m; i < n; i++) #define INF 2e9 #define MOD 1000000007 #define PI 3.14159265358979323846 #define vi vector<int> #define vll vector<ll> #define vi2 vector<vector<int>> #define eb emplace_back #define fi first #define se second #define ALL(v) v.begin(), v.end() #define sz(x) int(x.size()) using namespace std; using P = pair<ll,ll>; const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1}; #define INT(name) int name;cin >> name; #define VEC(type,name,n) vector<type> name(n);REP(i,n) cin >> name[i]; 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 solve(int N,int K,vector<ll> A){ deque<ll> deq2; REP(i,N){ deq2.push_back(A[i]); } ll MAX = 0; REP(i,K+1){ if(i>=N){ vector<ll> vec=A; if(vec.size()>0){ sort(ALL(vec)); ll sum = 0; REP(k,vec.size()){ sum+=vec[k]; } REP(k,min(sz(vec),K-i)){ if(vec[k]<0){ sum-=vec[k]; } } MAX=max(MAX,sum); } }else{ REP(j,i+1){ vector<ll> vec; deque<ll> deq=deq2; REP(k,j){ vec.push_back(deq.front()); deq.pop_front(); } REP(k,i-j){ vec.push_back(deq.back()); deq.pop_back(); } if(vec.size()>0){ sort(ALL(vec)); // REP(k,vec.size()){ // cout << vec[k] << " "; // } // cout << endl; ll sum = 0; REP(k,vec.size()){ sum+=vec[k]; } REP(k,min(sz(vec),K-i)){ if(vec[k]<0){ sum-=vec[k]; } } MAX=max(MAX,sum); // cout << MAX << endl; // cout << i << "a" << j << endl; } } } } return MAX; } ll dfs(deque<ll> deq,vector<ll> A,int cnt,int K){ ll ret = 0; REP(i,sz(A)){ ret+=A[i]; } if(cnt==K){ return ret; } cnt++; ll MAX = ret; REP(i,sz(A)){ vector<ll> B=A; deque<ll> deq2=deq; deq2.push_back(B[i]); B.erase(B.begin()+i); MAX=max(MAX,dfs(deq2,B,cnt,K)); deque<ll> deq3=deq; vector<ll> C=A; deq3.push_front(C[i]); C.erase(C.begin()+i); MAX=max(MAX,dfs(deq3,C,cnt,K)); } if(deq.size()>0){ deque<ll> deq4=deq; vector<ll> D=A; D.push_back(deq4.back()); deq4.pop_back(); MAX=max(MAX,dfs(deq4,D,cnt,K)); deque<ll> deq5=deq; vector<ll> E=A; E.push_back(deq5.front()); deq5.pop_front(); MAX=max(MAX,dfs(deq5,E,cnt,K)); } return MAX; } ll solve2(int N,int K,vector<ll> A){ deque<ll> deq2; REP(i,N){ deq2.push_back(A[i]); } vector<ll> EMPTY; return max(0ll,dfs(deq2,EMPTY,0,K)); } int main() { INT(N); INT(K); VEC(ll,A,N); cout << solve(N,K,A) << endl; // cout << solve2(N,K,A) << endl; random_device seed_gen; mt19937_64 engine(seed_gen()); while(0){ ll N=engine()%(ll)10+1; ll K=engine()%(ll)3+1; vector<ll> A; REP(i,N){ ll x=engine()%(ll)2e7+1-(ll)1e7; A.push_back(x); } cout << "checking input : " << N << " " << K << endl; REP(i,N){ cout << A[i] << " "; } cout << endl; ll sol1=solve(N,K,A); ll sol2=solve2(N,K,A); if(sol1==sol2){ cout << "OK" << endl; }else{ cout << "NG" << endl; cout << "solver1 " << sol1 << endl; cout << "solver2 " << sol2 << endl; break; } } }
1
// AtCoder Beginner Contest 167 - Problem F: Bracketing Sequencing (https://atcoder.jp/contests/abc167/tasks/abc167_f) #include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<ll, ll>; string solve(int N, const vector<string>& xs) { vector<int> cs; vector<ii> ds, es; int open = 0, close = 0; for (int i = 0; i < N; ++i) { int a = 0, b = 0; for (auto c : xs[i]) { open += (c == '(' ? 1 : 0); close += (c == ')' ? 1 : 0); b += (c == '(' ? 1 : -1); if (b < 0) { a--; b = 0; } } if (b + a >= 0) ds.push_back(ii(-a, i)); else es.push_back(ii(b, i)); } if (open != close) return "No"; sort(ds.begin(), ds.end()); sort(es.begin(), es.end(), greater<ii>()); int s = 0; for (auto p : ds) { auto i = p.second; for (auto c : xs[i]) { s += (c == '(' ? 1 : -1); if (s < 0) return "No"; } } for (auto p : es) { auto i = p.second; for (auto c : xs[i]) { s += (c == '(' ? 1 : -1); if (s < 0) return "No"; } } return "Yes"; } int main() { ios::sync_with_stdio(false); int N; cin >> N; vector<string> xs(N); for (int i = 0; i < N; ++i) cin >> xs[i]; auto ans = solve(N, xs); cout << ans << '\n'; return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <cmath> #include <queue> #include <set> #include <map> #include <iomanip> #include <fstream> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int mod = 1000000007; bool cmp(pii x, pii y) { return 1ll * x.first * y.second < 1ll * x.second * y.first; } int main() { ios::sync_with_stdio(false); int n, m = 0; pii a[1000006] = {}; string s; cin >> n; for (int i = 0; i < n; i++) { cin >> s; int x = 0, y = 0; for (int j = 0; j < s.length(); j++) { if (s[j] == '(') x++; else x--; y = min(y, x); } a[i] = {-y, x}; } sort(a, a + n, cmp); //for (int i = 0; i < n; i++) cout << a[i].first << ' ' << a[i].second << '\n'; for (int i = 0; i < n; i++) { if (m < a[i].first) { cout << "No"; return 0; } m += a[i].second; } if (m) cout << "No"; else cout << "Yes"; }
1
#include <bits/stdc++.h> #define mp make_pair #define X first #define Y second #define FOR(i, a, b) for(int i = a; i <= b; i++) #define FOD(i, a, b) for(int i = a; i >= b; i--) using namespace std; typedef long long ll; typedef pair <int, int> ii; const int N = 1e5 + 10; int n, m, res[N], deg[N]; vector <int> adj[N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; FOR(i, 1, m + n - 1){ int u, v; cin >> u >> v; adj[u].push_back(v); deg[v]++; } int root = 0; FOR(i, 1, n) if (deg[i] == 0) root = i; queue <int> Q; Q.push(root); while (!Q.empty()){ int u = Q.front(); Q.pop(); for(int v : adj[u]) if (--deg[v] == 0){ Q.push(v); res[v] = u; } } FOR(i, 1, n) cout << res[i] << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define INF (1 << 30) #define LLINF (1LL << 62) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) constexpr int MOD = 1000000007; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; template <class T> string to_string(T s); template <class S, class T> string to_string(pair<S, T> p); string to_string(string s); template <class T> string to_string(T v) { if (v.empty()) return "{}"; string ret = "{"; auto itr = v.begin(), next = ++v.begin(); for (; next != v.end(); ++itr, ++next) ret += to_string(*itr) + ","; ret += to_string(*itr) + "}"; return ret; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + "," + to_string(p.second) + "}"; } string to_string(string s) { return s; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } void print() { cout << endl; } template <class Head, class... Tail> void print(Head head, Tail... tail) { cout << to_string(head) << " "; print(tail...); } struct IO { #ifdef _WIN32 inline char getchar_unlocked() { return getchar(); } inline void putchar_unlocked(char c) { putchar(c); } #endif string separator = " "; template <class T> inline void read(T &x) { char c; do { c = getchar_unlocked(); } while (c != '-' && (c < '0' || '9' < c)); bool minus = 0; if (c == '-') { minus = 1; c = getchar_unlocked(); } x = 0; while ('0' <= c && c <= '9') { x *= 10; x += c - '0'; c = getchar_unlocked(); } if (minus) x = -x; } inline void read(string &x) { char c; do { c = getchar_unlocked(); } while (c == ' ' || c == '\n'); x.clear(); do { x += c; c = getchar_unlocked(); } while (c != ' ' && c != '\n' && c != EOF); } template <class T> inline void read(vector<T> &v) { for (auto &x : v) read(x); } template <class Head, class... Tail> inline void read(Head &head, Tail &... tail) { read(head); read(tail...); } template <class T> inline void write(T x) { char buf[32]; int p = 0; if (x < 0) { x = -x; putchar_unlocked('-'); } if (x == 0) putchar_unlocked('0'); while (x > 0) { buf[p++] = (x % 10) + '0'; x /= 10; } while (p) { putchar_unlocked(buf[--p]); } } inline void write(string x) { for (char c : x) putchar_unlocked(c); } inline void write(const char s[]) { for (int i = 0; s[i] != 0; ++i) putchar_unlocked(s[i]); } template <class T> inline void write(vector<T> v) { for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) { write(*itr); write(separator); } write(v.back()); } template <class Head, class... Tail> inline void write(Head head, Tail... tail) { write(head); write(separator); write(tail...); } void set_separator(string s) { separator = s; } } io; int main() { int N, M; io.read(N, M); vector<vector<int>> G(N), RG(N); REP(i, N + M - 1) { int a, b; io.read(a, b); G[a - 1].push_back(b - 1); RG[b - 1].push_back(a - 1); } int root = -1; REP(i, N) if (RG[i].empty()) root = i; vector<int> ans(N, -1); function<void(int)> dfs = [&](int u) { for (auto v : G[u]) { if (ans[v] == -1) { bool f = 1; for (int k : RG[v]) if (ans[k] == -1 && k != root) f = 0; if (f) { ans[v] = u; dfs(v); } } } }; dfs(root); REP(i, N) { io.write(ans[i] + 1); io.write("\n"); } return 0; }
1
#include <iostream> #include <vector> long int cnt = 0; void swap(std::vector<int>& arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } void merge(std::vector<int>& arr, int start, int mid, int end) { int length = end -start; int left = start, right = mid; std::vector<int> tmp(length); for (int i=0; i<length; i++) { if ( !(right < end) ) { tmp[i] = arr[left]; left++; } else if ( !(left < mid) ) { tmp[i] = arr[right]; right++; } else if ( arr[left] < arr[right] ) { tmp[i] = arr[left]; left++; } else { tmp[i] = arr[right]; right++; cnt+= mid - left; } } for (int i=0; i<length; i++) { arr[start+i] = tmp[i]; } } void merge_sort(std::vector<int>& arr, int start, int end) { if ( end - start > 1 ) { if ( end - start == 2 ) { if ( arr[start] > arr[start+1] ) { swap(arr, start, start+1); cnt++; } } else { int mid = ( start + end ) / 2; merge_sort(arr, start, mid); merge_sort(arr, mid, end); merge(arr, start, mid, end); } } } int main() { int n; std::cin >> n; std::vector<int> a(n); for (int i=0; i<n; i++) { std::cin >> a[i]; } merge_sort(a, 0, n); std::cout << cnt << std::endl; }
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int main(){ int e; cin >> e; while(e!=0){ int Min = 1000000; for(int i=0; i<e+1; i++){ int z = i; if(z*z*z > e){ break; } int y = 0; int x = 0; while( (y+1)*(y+1) <= e-z*z*z ){ y++; } x = e-z*z*z-y*y; if(Min > x+y+z){ Min = x+y+z; } } cout << Min << endl; cin >> e; } }
0