code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include <stdio.h> #include <algorithm> #include <set> #include <tuple> #include <queue> #include <vector> using namespace std; using ll = unsigned long long; using pii = pair<int, int>; int n, m, a[10000], b[10000], c[200], d[10000], v[200]; vector<pii> e[200]; void dfs(int x, int p) { if (v[x]) return; v[x] = 1; for (pii ed : e[x]) { int y, id; tie(y, id) = ed; if (y == p) continue; if (d[id]) continue; d[id] = (a[id] == x ? 2 : 3); dfs(y, x); } } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) scanf("%d %d", a + i, b + i); for (int i = 1; i <= n; i++) scanf("%d", c + i); for (int i = 0; i < m; i++) { if (c[a[i]] != c[b[i]]) d[i] = ((c[a[i]] > c[b[i]]) ? 2 : 3); else { e[a[i]].emplace_back(b[i], i); e[b[i]].emplace_back(a[i], i); } } for (int i = 1; i <= n; i++) dfs(i, 0); for (int i = 0; i < m; i++) printf((d[i] ^ 2) ? "<-\n" : "->\n"); }
#include <bits/stdc++.h> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second #define RF(x) freopen(x,"r",stdin) #define WF(x) freopen(x,"w",stdout) typedef long long LL; using namespace std; typedef pair<LL,LL> PLL; typedef pair<int,int> PII; const LL MOD = 1e9+7; const int SIZE = 1e6+9; const LL INF = 1LL<<60; const double eps = 1e-4; const double PI=3.1415926535897932; map<pair<int,int>, int> e; int ans[100009]; void f(int a,int b) {// a->b if(e.count({b,a}))ans[e[{b,a}]]=1; } vector<int> adj[100009]; bool vis[100009]; int c[100009]; int h[100009]; void dfs(int x, int p){ //printf("D %d %d\n",x,p); for(int i:adj[x]){ //printf("N %d\n",i); if(i==p||c[i]!=c[x])continue; if(!vis[i] || h[i]<h[x])f(x,i); if(!vis[i]){ h[i]=h[x]+1; vis[i]=1; dfs(i,x); } } } int main(){ DRII(n,m); REP(i,m){ DRII(a,b);a--;b--; e[MP(a,b)]=i; adj[a].PB(b); adj[b].PB(a); } REP(i,n)RI(c[i]); for(auto v:e){ if(c[v.F.F]==c[v.F.S])continue; if(c[v.F.F]>c[v.F.S])f(v.F.F,v.F.S); else f(v.F.S,v.F.F); } REP(i,n){ if(vis[i])continue; vis[i]=1; dfs(i,-1); } REP(i,m){ if(ans[i])puts("<-"); else puts("->"); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define mii map <int, int> #define mll map <ll, ll> #define pii pair <int, int> #define pll pair <ll, ll> #define vi vector <int> #define vd vector <double> #define vll vector <ll> #define fi first #define se second #define si set <int> #define sll set <ll> #define spii set <pii> #define vs vector <string> #define vpii vector <pair <int, int> > #define vpll vector <pair <long long, long long> > #define vvi vector <vector <int> > #define vvpii vector <vector <pii > > #define vb vector <bool> #define vvb vector <vb> #define mp make_pair #define vvll vector <vll> #define vsi vector <si> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define MANX MAXN #define itn int #define dbg(x); {cout << #x << "=" << x << ", ";} #define in(x); { for (auto &to : x) cin >> to;} #define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; } const ll INFLL = 1e18; const int MAXN = 1e6+100; const ll INF = 1e9; const int mod1 = 1e9+7; const int mod2 = 1e9+21; int a,b; void solve() { cin>>a>>b; int x=a+b; if (x>=15&&b>=8) cout<<1; else if (x>=10&&b>=3) cout<<2; else if (x>=3) cout<<3; else cout<<4; } int main() { #ifdef Mip182 freopen("a.in", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif int _t; _t=1; // cin>>_t; while (_t--) solve(); #ifdef Mip182 cout<<'\n'<<"Time : "<<(double)(clock())/CLOCKS_PER_SEC<<'\n'; #endif }
#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> #include <chrono> #include <random> #include <bitset> using namespace std; #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); #else #define eprintf(...) 42 #endif using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; template<typename T> using pair2 = pair<T, T>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } const int N = 101010; int n, m; int ed[N][2]; vector<int> g[N]; int c[N]; int ans[N]; bool used[N]; int getOther(int id, int v) { return ed[id][0] ^ ed[id][1] ^ v; } void dfs(int v) { used[v] = 1; for (int id : g[v]) { int u = getOther(id, v); if (used[u]) { if (ans[id] == -1) ans[id] = (int)(ed[id][1] == v); } else { ans[id] = (int)(ed[id][1] == v); dfs(u); } } } int main() { startTime = clock(); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { ans[i] = -1; for (int j = 0; j < 2; j++) { scanf("%d", &ed[i][j]); ed[i][j]--; } } for (int i = 0; i < n; i++) scanf("%d", &c[i]); for (int i = 0; i < m; i++) { int v = ed[i][0], u = ed[i][1]; if (c[v] != c[u]) { if (c[v] > c[u]) ans[i] = 0; else ans[i] = 1; } else { g[v].push_back(i); g[u].push_back(i); } } for (int i = 0; i < n; i++) { if (used[i]) continue; dfs(i); } for (int i = 0; i < m; i++) { if (ans[i] == 0) printf("->\n"); else printf("<-\n"); } return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int l,int r){return l+rng()%(r-l+1);} #define forinc(a,b,c) for(int a=b, _c=c; a<=_c; ++a) #define fordec(a,b,c) for(int a=b, _c=c; a>=_c; --a) #define forv(a,b) for(auto &a:b) #define func function #define fasty ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define fi first #define se second #define pb push_back #define all(a) begin(a),end(a) #define reset(f,x) memset(f,x,sizeof(f)) #define bit(x,i) (x>>(i-1)&1ll) #define on(x,i) (x|(1ll<<(i))) #define off(x,i) (x&~(1ll<<(i-1))) using ii=pair<int,int>; using ll=long long; using ull=unsigned long long; const int N=5005; int n; string s; int a[N],b[N],c[N],d[N]; int32_t main(){ fasty; cin>>n>>s; s=' '+s; forinc(i,1,n){ a[i]=a[i-1]+(s[i]=='A'); b[i]=b[i-1]+(s[i]=='T'); c[i]=c[i-1]+(s[i]=='G'); d[i]=d[i-1]+(s[i]=='C'); } int ans=0; forinc(i,1,n) forinc(j,i,n){ if(a[j]-a[i-1]==b[j]-b[i-1] && c[j]-c[i-1]==d[j]-d[i-1]) ans++; } cout<<ans<<'\n'; }
#include <bits/stdc++.h> using namespace std; int main() { int N; string s; cin >> N >> s; int cnt = 0, ans = 0; for(int i = 0; i<N; i++){ int a = 0, b = 0; for(int j = i; j<N; j++){ if(s[j] == 'A'){ a++; } else if(s[j] == 'T'){ a--; } else if(s[j] == 'C'){ b++; } else{ b--; } if(a==0 && b==0){ ans++; } } } cout << ans; }
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define pii pair<int, int> #define pll pair<long long,long long> #define vl vector<long long> #define vll vector<pll> #define vi vector<int> #define vii vector<pii> #define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define endl '\n' #define tcase int tt; cin>>tt; while(tt--) #define tcase2 int tt; cin>>tt; for(int qq=1;qq<=tt;qq++) using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; const int MAXN = 1000002; const int MOD2 = 998244353; const int MOD = 1000000007; const int INF = 1e8; const ld EPS = 1e-7; // Extra #define forn(i, n) for(int i = 0; i < (int)n; i++) #define forne(i, a, b) for(int i = a; i <= b; i++) #define all(x) x.begin(), x.end() #define trav(a, x) for(auto& a : x) #define fill(x,y) memset(x,y,sizeof(x)) /* run this program using the console pauser or add your own getch, system("pause") or input loop */ ll mul(ll x, ll y) { return (x * 1ll * y) % MOD2; } ll fastpow(ll x, ll y) { ll z = 1; while(y) { if(y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } ll modinv(ll n,ll p) { return fastpow(n,p-2) ; } struct Comp { bool operator()(const std::pair<int, int> &a, const std::pair<int, int> &b) { if (a.first != b.first) { return a.first > b.first; } return a.second >b.second; } }; int main(int argc, char** argv) { sws ; cout<<setprecision(10); //tcase { ll a,b ; cin>>a>>b ; cout<<(a+b)/2<<" "<<(a-b)/2<<"\n" ; } return 0 ; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define x first #define y second #define FOR(i, m, n) for (ll i(m); i < n; i++) #define DWN(i, m, n) for (ll i(m); i >= n; i--) #define REP(i, n) FOR(i, 0, n) #define DW(i, n) DWN(i, n, 0) #define F(n) REP(i, n) #define FF(n) REP(j, n) #define D(n) DW(i, n) #define DD(n) DW(j, n) using ll = long long; using ld = long double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vtll = vector<tll>; using gr = vector<vll>; using wgr = vector<vpll>; void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); } void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); } struct d_ { template<typename T> d_& operator,(const T& x) { cerr << ' ' << x; return *this;} template<typename T,typename U> d_& operator,(const pair<T,U>& x) { cerr << ' ' << x.x << ',' << x.y; return *this;} template<typename T> d_& operator,(const vector<T>& x) { for(auto x: x) cerr << ' ' << x; return *this;} } d_t; #define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; } #define deb(X ...) dbg(#X, "=", X); #define EPS (1e-10) #define INF (1LL<<61) #define CL(A,I) (memset(A,I,sizeof(A))) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() int main(void) { ios_base::sync_with_stdio(false); ll a,b; cin >> a >> b; ll x = (a+b)/2; ll y = a-x; cout << x << ' ' << y << endl; return 0; }
#include<bits/stdc++.h> #define lli unsigned long long int #define endl "\n" #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL); using namespace std; lli min(lli a,lli b) { if (a<b) { return a; } else { return b; } } lli max(lli a,lli b) { if (a>b) { return a; } else { return b; } } void solve() { lli x,y,a,b; cin>>x>>y>>a>>b; lli cnt=0; while((__int128)x*a<y && x*a<=x+b ) { x*=a; cnt++; } cnt+=(y-x-1)/b; cout<<cnt<<endl; } int main(){ fastio; cout<<fixed; cout<<setprecision(10); lli t=1; // cin>>t; for(lli i=1;i<=t;i++) { solve(); } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<set> #include<map> #include<queue> #include<cmath> #include<iomanip> #include<cstring> #include<complex> #include<cstdio> #define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1; #define fi first #define se second #define pb push_back #define pii pair<int,int> #define ppi pair<pii,int> #define pip pair<int,pii> #define ll long long #define pll pair<ll,ll> #define rep(i,n) for(int i=0;i<n;i++) #define repd(i,n) for(int i=n-1;i>=0;i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 100005 #define mid(l,r) l+(r-l)/2 #define removeduplicates(vec) vec.erase( unique( vec.begin(), vec.end() ), vec.end() ) #define memset1(v) memset(v,-1,sizeof(v)) #define memset0(v) memset(v,0,sizeof(v)) using namespace std; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1}; void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;} ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);} void solve(){ ll x,y,a,b; ll c=0; cin>>x>>y>>a>>b; ll maxi=0; while(1){ ll rem=y-x; maxi=max(maxi,c+rem/b+(rem%b!=0)); c++; if(x>=y/a)break; x*=a; } maxi=max(maxi,c); cout<<maxi-1; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while(t--){ solve(); } }
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 5; typedef long long LL; LL const mod = 1e9 + 7; LL n, dp[N][2]; LL qmi(LL a, LL k, LL p) { LL res = 1 % p; // res记录答案, 模上p是为了防止k为0,p为1的特殊情况 while (k) { // 只要还有剩下位数 if (k & 1) res = (LL)res * a % p; // 判断最后一位是否为1,如果为1就乘上a,模上p, // 乘法时可能爆int,所以变成long long k >>= 1; // 右移一位 a = (LL)a * a % p; // 当前a等于上一次的a平方,取模,平方时可能爆int,所以变成long // long } return res; } int main() { cin >> n; char caa, cab, cba, cbb; cin >> caa >> cab >> cba >> cbb; if (n <= 3) cout << 1 << endl; else if (cab == 'A') { if (caa == 'A') { cout << 1 << endl; return 0; } if (cba == 'A') { dp[0][0] = 1; dp[0][1] = 0; for (int i = 1; i <= n - 2; i++) { dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod; dp[i][1] = dp[i - 1][0]; } cout << (dp[n - 2][0]) % mod << endl; return 0; } else { cout << qmi(2, n - 3, mod) << endl; return 0; } } else if (cab == 'B') { if (cbb == 'B') { cout << 1 << endl; return 0; } if (cba == 'B') { dp[0][0] = 1; dp[0][1] = 0; for (int i = 1; i <= n - 2; i++) { dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod; dp[i][1] = dp[i - 1][0]; } cout << (dp[n - 2][0]) % mod << endl; return 0; } else { cout << qmi(2, n - 3, mod) << endl; return 0; } } return 0; }
/* Generated by powerful Codeforces Tool * You can download the binary file in here https://github.com/xalanq/cf-tool (Windows, macOS, Linux) * Author: alireza_kaviani * Time: 2020-12-05 16:43:07 **/ #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; #define all(x) (x).begin(),(x).end() #define Sort(x) sort(all((x))) #define X first #define Y second #define sep ' ' #define endl '\n' #define SZ(x) ll(x.size()) ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const ll MAXN = 1e6 + 10; const ll LOG = 22; const ll INF = 8e18; const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9; ll n , m , A[MAXN] , s; int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin >> n >> m; m += n; for(int i = 0 ; i < n ; i++){ cin >> A[i]; s += A[i] + 1; } ll ans = 1 , x = 1; // cout << s << sep << m << endl; for(int i = 1 ; i <= s ; i++){ ans = ans * (m - i + 1) % MOD; x = x * i % MOD; } cout << ans * poww(x , MOD - 2 , MOD) % MOD << endl; return 0; } /* */
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; const int mod = 1e9 + 7; const double pi = acos(-1.0); const int inf = INT_MAX; const int N = 50 + 5; int n; ll x, a[N], lim[N], dp[N][2], num_x[N]; ll recurse(int i, int c) { if (dp[i][c] != -1) { return dp[i][c]; } if (i == n - 1) { return 1; } if (c) { if (1 + num_x[i] == lim[i]) { dp[i][c] = recurse(i + 1, 1); } else { dp[i][c] = recurse(i + 1, 0) + recurse(i + 1, 1); } } else { if (num_x[i]) { dp[i][c] = recurse(i + 1, 0) + recurse(i + 1, 1); } else { dp[i][c] = recurse(i + 1, 0); } } return dp[i][c]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> x; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { lim[i] = a[i + 1] / a[i]; } lim[n - 1] = LLONG_MAX; ll temp = x; for (int i = 0; i < n; i++) { num_x[i] = temp % lim[i]; temp /= lim[i]; // cout << num_x[i] << ' '; } // cout << '\n'; memset(dp, -1, sizeof(dp)); cout << recurse(0, 0) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; ll X; cin >> X; vector<ll> a(n); for(int i=0;i<n;i++){ cin >> a[i]; } vector<ll> x(n); for(int i=n-1;i>=0;i--){ x[i]=X/a[i]; X%=a[i]; } vector<ll> t(n); for(int i=0;i+1<n;i++){ t[i]=a[i+1]/a[i]; } t.back()=1e18; vector<ll> dp(2); dp[0]=1; for(int i=0;i<n;i++){ vector<ll> pre(2); swap(dp,pre); for(int j=0;j<2;j++){ for(int k=0;k<2;k++){ {// Yが0 ll z=t[i]*k-x[i]-j; if(0<=z and z<t[i]){ dp[k]+=pre[j]; } } {// お釣りが0 ll y=x[i]+j-t[i]*k; if(0<y and y<t[i]){ dp[k]+=pre[j]; } } } } } cout << dp[0] << endl; }
// g++ -std=c++11 a.cpp #include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<unordered_map> #include<utility> #include<cmath> #include<random> #include<cstring> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<sstream> #include<random> #include<iomanip> #include<assert.h> #include<typeinfo> #define loop(i,a,b) for(long long i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define FOR(i,a) for(auto i:a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) #define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;} #define show2d(v) {rep(__,v.size())show1d(v[__]);} using namespace std; //kaewasuretyuui typedef long long ll; #define int ll typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; #define mt make_tuple typedef tuple<int,int,int> tp; typedef vector<tp> vt; typedef vector<vt>vvt; template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;} template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;} const double PI=acos(-1); const long double EPS=1e-9; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; #define yes cout<<"Yes\n" #define no cout<<"No\n" const int MOD=3; const int M=400100; vi fact,rev; bool do_init=false; ll powmod(ll a,ll b,ll m=MOD){ ll out=1; ll p=a%m; while(b){ if(b&1)out=out*p%m; p=p*p%m; b>>=1; } return out; } void init(){ do_init=true; fact=rev=vector<ll>(M); fact[0]=fact[1]=1; loop(i,2,M)fact[i]=fact[i-1]*i%MOD; rep(i,M)rev[i]=fact[i]; } //nCr ll nCr(ll n,ll r,ll m=MOD){ if(!do_init)init(); if(n<0||r<0||n<r)return 0; // 0 or 1 ll out=fact[n]*rev[r]%m*rev[n-r]%m; return out; } ll nCr_l(ll n,ll r,ll m){ ll out=1; while(n>0){ (out*=nCr(n%m,r%m,m))%=m; n/=m;r/=m; } return out; } signed main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; string s; cin>>s; int out=0; rep(i,n){ int a; if(s[i]=='W')a=0; if(s[i]=='R')a=1; if(s[i]=='B')a=2; int t=nCr_l(n-1,i,3); (out+=t*a)%=MOD; } if(n%2==0)out=(3-out)%3; if(out==0)cout<<"W"<<endl; if(out==1)cout<<"R"<<endl; if(out==2)cout<<"B"<<endl; }
#pragma region Macros #include <bits/stdc++.h> #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define ALL(v) (v).begin(), (v).end() #define LLA(v) (v).rbegin(), (v).rend() #define SZ(v) (int)(v).size() #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define STRING(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ read(name) #define VEC2(type, name, height, width) \ vector<vector<type>> name(height, vector<type>(width)); \ read(name) using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using Graph = vector<vector<int>>; template <typename T> struct edge { int from, to; T cost; edge(int f, int t, T c) : from(f), to(t), cost(c) {} }; template <typename T> using WGraph = vector<vector<edge<T>>>; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; const char newl = '\n'; template <class T> inline vector<T> make_vec(size_t a, T val) { return vector<T>(a, val); } template <class... Ts> inline auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec(ts...))>(a, make_vec(ts...)); } void read() {} template <class T> inline void read(T &a) { cin >> a; } template <class T, class S> inline void read(pair<T, S> &p) { read(p.first), read(p.second); } template <class T> inline void read(vector<T> &v) { for(auto &&a : v) read(a); } template <class Head, class... Tail> inline void read(Head &head, Tail &...tail) { read(head), read(tail...); } template <class T> void write(const T &a) { cout << a << '\n'; } template <class T> void write(const vector<T> &a) { for(int i = 0; i < a.size(); i++) cout << a[i] << (i + 1 == a.size() ? '\n' : ' '); } template <class Head, class... Tail> void write(const Head &head, const Tail &...tail) { cout << head << ' '; write(tail...); } template <class T> void writel(const T &a) { cout << a << '\n'; } template <class T> void writel(const vector<T> &a) { for(int i = 0; i < a.size(); i++) cout << a[i] << '\n'; } template <class Head, class... Tail> void writel(const Head &head, const Tail &...tail) { cout << head << '\n'; write(tail...); } template <class T> auto sum(const vector<T> &a) { return accumulate(ALL(a), T(0)); } template <class T> auto min(const vector<T> &a) { return *min_element(ALL(a)); } template <class T> auto max(const vector<T> &a) { return *max_element(ALL(a)); } template <class T> inline void chmax(T &a, T b) { (a < b ? a = b : a); } template <class T> inline void chmin(T &a, T b) { (a > b ? a = b : a); } struct IO { IO() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); } } io; #pragma endregion int main() { INT(n); write(n == 1 ? 0 : n - 1); }
#include <iostream> #include <algorithm> template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} using namespace std; int main() { int a, b, x, y; cin >> a >> b >> x >> y; --a, --b; int res = x+y*abs(b-a); if(a>0) chmin(res, x+y*abs(b-(a-1))); if(a>b) chmin(res, x*((a-b)*2-1)); else chmin(res, x*((b-a)*2+1)); cout << res << endl; return 0; }
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <iomanip> #include <functional> #include <bitset> #include <limits> #include <cstdio> #include <cmath> #include <cassert> #include <random> #ifdef DEBUG #include "library/Utility/debug.cpp" #else #define debug(...) #endif #define rep(i,n) for(int i=0;i<(n);++i) #define EL '\n' #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() using lnt = long long; struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO; template<typename T> using V = std::vector<T>; template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; } /*-*/ int main() { int a,b,x,y; std::cin >> a >> b >> x >> y; V<V<int> > dp(100,V<int>(2,-1)); V<V<int> > dp2(100,V<int>(2,-1)); dp[0][1]=x; dp[0][0]=0; dp2[0][1]=x; dp2[0][0]=0; rep(i,100) { if(i==0) continue; dp[i][0]=std::min(dp[i-1][0]+y,dp[i-1][1]+x); dp[i][1]=dp[i-1][1]+y; rep(j,2) dp[i][j]=std::min(dp[i][j],dp[i][j^1]+x); } rep(i,100) { if(i==0) continue; dp2[i][1]=std::min(dp2[i-1][1]+y,dp2[i-1][0]+x); dp2[i][0]=dp2[i-1][0]+y; rep(j,2) dp2[i][j]=std::min(dp2[i][j],dp2[i][j^1]+x); } if(b>a) print(dp[std::abs(b-a)][1]); else print(dp2[std::abs(b-a)][1]); }
// Author: wlzhouzhuan #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pb push_back #define fir first #define sec second #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, t) memset(s, t, sizeof(s)) #define mcpy(s, t) memcpy(s, t, sizeof(t)) template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); return f ? -x : x; } template<typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } template<typename T> void print(T x, char let) { print(x), putchar(let); } int main() { int n,k;cin>>n>>k; int ans=0; for(int i=1;i<=n;i++){ for(int j=1;j<=k;j++){ ans+=100*i+j; } } printf("%d\n",ans); }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll=long long; using ld=long double; using pll=pair<ll, ll>; //using mint = modint1000000007; #define rep(i,n) for (ll i=0; i<n; ++i) #define all(c) begin(c),end(c) #define PI acos(-1) #define oo 2e18 template<typename T1, typename T2> bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} template<typename T1, typename T2> bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} //priority_queue<ll, vector<ll>, greater<ll>> Q; // LMAX = 18446744073709551615 (1.8*10^19) // IMAX = 2147483647 (2.1*10^9) /* 400*8*2 */ int main(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); rep(i, 20){ string S; rep(j, 20){ S += 'A' + rand()%8; } cout << S << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector<int> a(h * w); int mi = INT_MAX; for (int i = 0; i < h * w; i++) { cin >> a[i]; mi = min(mi, a[i]); } int ans = 0; for (int i = 0; i < h * w; i++) { ans += a[i] - mi; } cout << ans << "\n"; return 0; }
//デバッグ用オプション:-fsanitize=undefined,address //コンパイラ最適化 #pragma GCC optimize("Ofast") //インクルードなど #include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; typedef long long ll; using vint = vector<int>; using vll = vector<ll>; using vs = vector<string>; using vbool = vector<bool>; template <class T> using arr = vector<vector<T>>; //マクロ // forループ //引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか // Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる // FORAは範囲for文(使いにくかったら消す) #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 = b - 1; i >= ll(a); i--) #define FORA(i, I) for (const auto &i : I) // xにはvectorなどのコンテナ #define ALL(x) x.begin(), x.end() #define SIZE(x) ll(x.size()) //定数 #define INF 1000000000000 // 10^12:∞ #define MOD 1000000007 // 10^9+7:合同式の法 #define MAXR 100000 // 10^5:配列の最大のrange // aよりもbが大きいならばaをbで更新する // (更新されたならばtrueを返す) template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; // aをbで更新 return true; } return false; } // aよりもbが小さいならばaをbで更新する // (更新されたならばtrueを返す) template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; // aをbで更新 return true; } return false; } template <class T> void pl(T x) { cout << x << " "; } template <class T> void pr(T x) { cout << x << endl; } template <class T> void prvec(const vector<T> &a) { REP(i, a.size() - 1) { cout << a[i] << " "; } pr(a[a.size() - 1]); } template <class T> void prarr(const arr<T> &a) { REP(i, a.size()) if (a[i].empty()) pr(""); else prvec(a[i]); } using P = pair<ll, ll>; void prp(const P &p) { cout << p.first << " " << p.second << endl; } struct Constants { ll H, W; Constants() { cin >> H >> W; } } C; struct Args { arr<ll> a; Args() : a(C.H, vll(C.W, 0)) { ios::sync_with_stdio(false); cin.tie(nullptr); REP(i, C.H) { REP(j, C.W) { cin >> a.at(i).at(j); } } } } args; struct Solver { ll ans; Solver() : ans(0) {} // void solve() { map<ll, ll> counts; REP(i, C.H) { REP(j, C.W) { counts[args.a.at(i).at(j)]++; } } // ll minb = -INF; FORA(p, counts) { if (minb == -INF) { minb = p.first; continue; } ans += (p.first - minb) * p.second; } } void output() { pr(ans); } } s; int main() { s.solve(); s.output(); return 0; }
#include <bits/stdc++.h> using namespace std; int N, vis[10005]; int main () { #ifndef ONLINE_JUDGE freopen("cpp.in", "r", stdin); #endif cin >> N; if (N == 3) return puts("6 10 15") & 0; for (int i = 6; i <= 10000; i += 6) vis[i] = 1; for (int i = 10; i <= 10000; i += 10) vis[i] = 1; for (int i = 15; i <= 10000; i += 15) vis[i] = 1; for (int i = 1; i <= 10000 && N; i++) if (vis[i]) printf("%d ", i), N--; putchar('\n'); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep0(i,n) for(int (i) = 0; (i) < (n); ++(i)) #define pf(x) cout << (x) << endl #define all(x) (x).begin(),(x).end() template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; const char mc[4]={'U','L','D','R'}; const int max_e = 9000; const int inf = 1000000000; int mh[29][29],mv[29][29]; int dijkstra(vector<vector<int> > &m, pair<int, int> s){ priority_queue<pair<int,pair<int,int> >, vector<pair<int,pair<int,int> > >, greater<pair<int,pair<int,int> > > > next; next.push(make_pair(0,s)); while (!next.empty()) { int x = next.top().second.first,y = next.top().second.second; int p = next.top().first; next.pop(); if(x > 0){ if(chmin(m[x - 1][y], p + mv[x - 1][y])){ next.push(make_pair(p + mv[x - 1][y], make_pair(x - 1, y))); } } if(x < 29){ if(chmin(m[x + 1][y], p + mv[x][y])){ next.push(make_pair(p + mv[x][y], make_pair(x + 1, y))); } } if(y > 0){ if(chmin(m[x][y - 1], p + mv[x][y - 1])){ next.push(make_pair(p + mv[x][y - 1], make_pair(x, y - 1))); } } if(y < 29){ if(chmin(m[x][y + 1], p + mv[x][y])){ next.push(make_pair(p + mv[x][y], make_pair(x, y + 1))); } } } return 0; } string restore(vector<vector<int> > &m, int x, int y){ string root = ""; int s = 0; while (m[x][y] != 0) { ++s; int next_x = x,next_y = y,p = m[x][y]; rep0(i, 4)if(chmin(p, m[min(max(0,x + dx[i]),29)][min(max(0,y + dy[i]),29)])){ next_x = x + dx[i]; next_y = y + dy[i]; if(s != root.size())root.push_back(mc[i]); else root[s - 1] = mc[i]; } x = next_x; y = next_y; } reverse(all(root)); return root; } int update(string s, int p, int x, int y){ rep0(i, s.size()){ if(s[i] == 'U'){ mv[x - 1][y] = (mv[x - 1][y] + p + 1) / 2; x--; }else if(s[i] == 'D'){ mv[x][y] = (mv[x][y] + p + 1) / 2; x++; }else if(s[i] == 'R'){ mh[x][y] = (mh[x][y] + p + 1) / 2; y++; }else{ mh[x][y - 1] = (mh[x][y - 1] + p + 1) / 2; y--; } } return 0; } //modを確認すること int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); rep0(i, 30)rep0(j, 30){ //mh[i][j] = max_e; //mv[i][j] = max_e; mh[i][j] = rand() % 8000 + 1000; mv[i][j] = rand() % 8000 + 1000; } rep0(i, 1000){ vector<vector<int> > m(30,vector<int>(30,inf)); int sx,sy,gx,gy,p; string s = ""; cin >> sx >> sy >> gx >> gy; m[sx][sy] = 0; dijkstra(m, make_pair(sx, sy)); string ans = restore(m, gx, gy); pf(ans); cin >> p; update(ans, int((p + ans.size() - 1) / ans.size()), sx, sy); } return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for(int i = 0;i<(int)(n);i++) #define all(v) (v).begin(),(v).end() using ll = long long; using P = pair<int,int>; struct UnionFind { vector<int> par, size; UnionFind(int x) { par.resize(x); size.resize(x, 1); for(int i = 0; i < x; i++) { par[i] = i; } } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } int consize(int x) { return size[find(x)]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (size[x] < size[y]) { par[x] = y; size[y] += size[x]; } else { par[y] = x; size[x] += size[y]; } } }; signed main() { int n,m;cin >> n >> m; // vector<vector<int>> G(n); UnionFind uf(n); vector<int> a(n),b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; vector<int> v(n); rep(i, n) v[i]=b[i]-a[i]; rep(i, m) { int a,b;cin >> a >> b; a--;b--; uf.unite(a,b); } map<int,int> mp; rep(i, n) { mp[uf.find(i)]+=v[i]; } for(auto p:mp) { if(p.second) { printf("No\n"); return 0; } } printf("Yes\n"); return 0; }
// Problem : B - Values // Contest : AtCoder - AtCoder Regular Contest 106 // URL : https://atcoder.jp/contests/arc106/tasks/arc106_b // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; int N, M; int dsu[200005]; long long val1[200005], val2[200005]; int getrt(int n){ return dsu[n] = (dsu[n] == n ? n : getrt(dsu[n])); } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M; for(int i= 1; i<=N; i++){ dsu[i] = i; cin >> val1[i]; } for(int i = 1; i<=N; i++){ cin >> val2[i]; } while(M--){ int a, b; cin >> a >> b; if(getrt(a) != getrt(b)){ val1[getrt(a)] += val1[getrt(b)]; val2[getrt(a)] += val2[getrt(b)]; dsu[getrt(b)] = getrt(a); } } for(int i = 1; i<=N; i++){ if(getrt(i) == i){ if(val1[i] != val2[i]){ cout << "No\n"; return 0; } } } cout << "Yes"; }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++) #define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define vector1d(type,name,...) vector<type>name(__VA_ARGS__) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__))) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=(ll)1<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} int main(){ ll ans=0; LL(n); if(n<=999){ out(0); return 0; } ll d=1000; while(d<=n){ ans+=n-(d-1); d*=1000; } out(ans); }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; int a[105][105]; int s1[18]; int s2[18]; int v[105]; int main(){ int n,m; cin >> n >> m; int x,y; for(int i = 0; i < m; i++){ cin >> x >> y; a[x][y]+=1; } int t; cin >> t; for(int i = 0; i < t; i++){ int sx,sy; cin >> sx >> sy; s1[i] = sx; s2[i] = sy; } int sum = 0; for(int i = 0; i < pow(2,t); i++){ int p = i; ll ans = 0; memset(v,0,sizeof(v)); int j = t; while(j--){//取t次 if(p & 1){ v[s1[t-j-1]] = 1; //cout << s1[t-j-1]<<' '; } else { v[s2[t-j-1]] = 1; //cout << s2[t-j-1] << ' '; } p>>=1; //cout << p; } //cout << ' '; //for(int i = 1; i <= n; i++){ // if(v[i] == 1)cout << i<<' '; //} //cout << endl; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(a[i][j]&&v[i]&&v[j])ans+=a[i][j]; } } if(ans>sum)sum = ans; } cout << sum << endl; }
#pragma GCC optimize("Ofast") //#pragma GCC target ("sse4") #include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<bitset> #include<stack> #include<unordered_map> #include<unordered_set> #include<utility> #include<cassert> #include<complex> #include<numeric> #include<array> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; constexpr ll mod = 998244353; const ll INF = mod * mod; typedef pair<int, int>P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) #define all(v) (v).begin(),(v).end() typedef pair<ll, ll> LP; typedef double ld; typedef pair<ld, ld> LDP; const ld eps = 1e-12; const ld pi = acosl(-1.0); ll mod_pow(ll x, ll n, ll m = mod) { if (n < 0) { ll res = mod_pow(x, -n, m); return mod_pow(res, m - 2, m); } if (abs(x) >= m)x %= m; if (x < 0)x += m; ll res = 1; while (n) { if (n & 1)res = res * x % m; x = x * x % m; n >>= 1; } return res; } struct modint { ll n; modint() :n(0) { ; } modint(ll m) :n(m) { if (n >= mod)n %= mod; else if (n < 0)n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; } modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; } modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, ll n) { if (n == 0)return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2)res = res * a; return res; } ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } modint operator/=(modint& a, modint b) { a = a / b; return a; } const int max_n = 1 << 10; modint fact[max_n], factinv[max_n]; void init_f() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[b] * factinv[a - b]; } modint combP(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[a - b]; } bool exi[18][18]; void solve() { int n; cin >> n; int m; cin >> m; rep(i, m) { int a, b; cin >> a >> b; a--; b--; exi[a][b] = exi[b][a] = true; } vector<bool> can(1 << n); rep(i, 1<<n) { can[i] = true; rep(j, n) { if (i & (1 << j)) { Rep(k, j + 1, n) { if (i & (1 << k)) { if (!exi[j][k])can[i] = false; } } } } } vector<int> dp(1 << n, mod); dp[0] = 0; rep(i, (1 << n)) { for (int s = i;; s = (s - 1) & i) { if (can[s]) { dp[i] = min(dp[i], dp[i ^ s] + 1); } if (s == 0)break; } } cout << dp[(1 << n) - 1] << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(15); //init_f(); //init(); //expr(); //int t; cin >> t; rep(i, t) solve(); return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define next Cry_For_theMoon using namespace std; const int MAXN=25,LIM=1<<19; int n,m,g[MAXN][MAXN]; int f[LIM],d[LIM],ans,vis[MAXN]; inline int Min(int a,int b){ return (a<b)?a:b; } int main(){ scanf("%d%d",&n,&m); ans=n; for(int i=1;i<=n;i++){ g[i][i]=1; } for(int i=1,u,v;i<=m;i++){ scanf("%d%d",&u,&v); g[u][v]=g[v][u]=1; } for(int i=0;i<n;i++){ f[1<<i]=1; } for(int i=1;i<(1<<n);i++){ if(f[i])continue; memset(vis,0,sizeof vis); f[i]=1; for(int j=0;j<n;j++){ if(i&(1<<j)){ vis[j+1]=1; } } for(int j=1;j<=n;j++){ if(!vis[j])continue; for(int k=j+1;k<=n;k++){ if(!vis[k])continue; if(!g[j][k]){ f[i]=0; break; } } if(!f[i])break; } } for(int i=1;i<(1<<n);i++){ d[i]=100; for(int j=i;j;j=(j-1)&i){ if(!f[j])continue; d[i]=Min(d[i],1+d[j xor i]); } } printf("%d",d[(1<<n)-1]); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define chmin(x,y) x = min((x),(y)) #define chmax(x,y) x = max((x),(y)) #define popcount(x) __builtin_popcount(x) using namespace std; using ll = long long ; using P = pair<int,int> ; using pll = pair<long long,long long>; const int INF = 1e9; const long long LINF = 1e17; const int MOD = 1000000007; //const int MOD = 998244353; const double PI = 3.14159265358979323846; int main(){ int n; cin >> n; vector<ll> x(n),c(n); rep(i,n){ cin >> x[i] >> c[i]; } vector<ll> dp(2,0); vector<vector<ll>> G(n+2); G[0].push_back(0); G[n+1].push_back(0); rep(i,n){ G[c[i]].push_back(x[i]); } rep(i,n+1){ sort(G[i].begin(),G[i].end()); //rep(j,(ll)G[i].size()) cout << G[i][j] << " "; //cout << endl; } int before = 0; for(int i=1;i<=n+1;i++){ if((int)G[i].size() == 0) continue; vector<ll> dp_new(2,LINF); ll l = G[before][0]; ll r = G[before].back(); if(l < G[i][0]){ dp_new[0] = dp[0] + G[i].back() - l + G[i].back() - G[i][0]; dp_new[1] = dp[0] + G[i].back() - l; }else if(l < G[i].back()){ dp_new[0] = dp[0] + G[i].back() - l + G[i].back() - G[i][0]; dp_new[1] = dp[0] + l - G[i][0] + G[i].back() - G[i][0]; }else{ dp_new[0] = dp[0] + l - G[i][0]; dp_new[1] = dp[0] + l - G[i][0] + G[i].back() - G[i][0]; } if(r < G[i][0]){ chmin(dp_new[0],dp[1] + G[i].back() - r + G[i].back() - G[i][0]); chmin(dp_new[1],dp[1] + G[i].back() - r); }else if(r < G[i].back()){ chmin(dp_new[0],dp[1] + G[i].back() - r + G[i].back() - G[i][0]); chmin(dp_new[1],dp[1] + r - G[i][0] + G[i].back() - G[i][0]); }else{ chmin(dp_new[0],dp[1] + r - G[i][0]); chmin(dp_new[1],dp[1] + r - G[i][0] + G[i].back() - G[i][0]); } before = i; swap(dp,dp_new); //cout << dp[0] << " " << dp[1] << endl; } cout << min(dp[0],dp[1]) << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int MAX = 200010; const int64_t INF = 1e18; vector<vector<int>> G(MAX,vector<int>(0)); int64_t dp[MAX][2]; int main(){ int N; cin >> N; for(int i=0; i<N; i++){ int x, c; cin >> x >> c; G[c].push_back(x); } G[N+1].push_back(0); for(int i=0; i<MAX; i++){ for(int j=0; j<2; j++){ dp[i][j] = INF; } } dp[0][0] = 0; dp[0][1] = 0; int64_t l = 0; int64_t r = 0; for(int i=1; i<=N+1; i++){ if(G[i].size() == 0){ dp[i][0] = dp[i-1][0]; dp[i][1] = dp[i-1][1]; continue; } sort(G[i].begin(),G[i].end()); int g = G[i].size(); //大きい方に進んで小さい方に進む dp[i][0] = min(dp[i-1][0] + abs(l-G[i][g-1]) + abs(G[i][g-1] - G[i][0]), dp[i-1][1] + abs(r-G[i][g-1]) + abs(G[i][g-1] - G[i][0])); //小さい方に進んで大きい方に進む dp[i][1] = min(dp[i-1][0] + abs(l-G[i][0]) + abs(G[i][g-1] - G[i][0]), dp[i-1][1] + abs(r-G[i][0]) + abs(G[i][g-1] - G[i][0])); l = G[i][0]; r = G[i][g-1]; } cout << min(dp[N+1][0],dp[N+1][1]) << endl; }
#include<iostream> using namespace std; int main(){ int n,x=0; cin>>n>>x; char a[n+1]; cin>>a; for(int i=0;i<n;i++){ if(a[i]=='o'){ x++; }else if(a[i]=='x' && x!=0){ x--; } } cout<<x; return 0; }
#include <iostream> #include <math.h> #include <string> #include <vector> #include <algorithm> //#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c; cin >> a >> b >> c; cout << 21 - (a + b + c); return 0; }
//HAR HAR MAHADEV //WHEN IT GETS HARDER REMEMBER WHY YOU STARTED💪 /*ヽ`、ヽ``、ヽ`ヽ`、、ヽ `ヽ 、ヽ`🌙`ヽヽ`ヽ、ヽ` ヽ`、ヽ``、ヽ 、``、 `、ヽ` 、` ヽ`ヽ、ヽ `、ヽ``、 ヽ、``、`、ヽ``、 、ヽヽ`、`、、ヽヽ、``、 、 ヽ`、 ヽ``、 ヽ`ヽ`、、ヽ `ヽ 、 🚶ヽ````ヽヽヽ`、、ヽ`、、ヽ*/ #include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef complex< ld > cd; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; typedef vector< int > vi; typedef vector< ld > vd; typedef vector< ll > vl; typedef vector< pi > vpi; typedef vector< pl > vpl; typedef vector< cd > vcd; #define l00p(i, a, b) for (int i=a; i<(b); i++) #define loop(i, a) for (int i=0; i<(a); i++) #define rep1(i, a, b) for (int i = (b)-1; i >= a; i--) #define rep(i, a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define vt vector #define bug(x) cout<<#x<<"="<<x<<endl; #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ins insert //#define MOD 998244353 template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<class T> using PR = pair<T, T>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ld pii = 3.14159265359; const int MOD = 1000000007; const char cl = '\n'; const ll INF = 1e18; const int MX = 100001; //check the limits, dummy constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set void solve() { string s; cin >> s; vi cnt(26); ll ans = 0; rep(i, sz(s)) { int c = s[i] - 'a'; cnt[c]++; if(i + 2 < sz(s)) { if(s[i] == s[i + 1]) { ans += sz(s) - i - cnt[c]; cnt.assign(26, 0); cnt[s[i] - 'a'] = sz(s) - i; } } } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; clock_t clk = clock(); //cin >> t; while (t--) { solve(); } return 0; } /* stuff you should look for * jab answer dhoondhna ho to matlb binary search beta * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#include <iostream> #include <iomanip> #include <utility> #include <cmath> #include <random> #include <vector> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <string> #include <algorithm> using namespace std; #define rep(i,n) for(int i = 0; i<n; ++i) #define REP(i,n) for(int i = 1; i<=n; ++i) #define all(x) begin(x),end(x) #define show(obj) for(auto x:obj)cout<<x<<' ';cout<<endl; typedef long long ll; typedef pair<int,int> P; typedef pair<ll,ll> lp; typedef pair<double, double> FP; const int inf = 1001001000; const ll INF = 1LL<<60; const int MOD = (int)1e9 + 7; bool com(lp a, lp b){ return a.first*2 + a.second > b.first*2 + b.second; } int main(){ int n; cin >> n; vector<lp> vot(n); rep(i,n)cin >> vot[i].first >> vot[i].second; sort(all(vot), com); ll aoki_sum = 0; rep(i,n)aoki_sum += vot[i].first; ll tak_sum = 0; int pos = 0; while(tak_sum <= aoki_sum){ tak_sum += vot[pos].first + vot[pos].second; aoki_sum -= vot[pos].first; ++pos; } cout << pos << endl; return 0; }
#include <cstdlib> #include <bits/stdc++.h> using namespace std; #define ll long long #define ii pair<int,int> #define vi vector<int> #define vii vector<ii> #define vc vector<char> #define vs vector<string> #define vd vector<double> #define vll vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vvc vector<vc> #define vvs vector<vs> #define vvll vector<vll> #define vvd vector<vd> #define FOR(x,n) for(int x=0;x<n;x++) #define FORS(x,n) for(int x=1;x<=n;x++) #define FORE(x,a) for(auto &x: a) #define ALL(x) x.begin(),x.end() #define REP(n) for(int _ = 0; _ < n; _++) #define MT make_tuple #define pb push_back #define endl '\n' #define F first #define S second vvi con; vi vis; void dfs(int u) { if(vis[u]) return; vis[u] = 1; for(auto x: con[u]) dfs(x); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h,w; cin >> h >> w; vs b(h); FOR(i,h) cin >> b[i]; con.resize(h+w); vis.resize(h+w); FOR(i,h) { FOR(j,w) { if(b[i][j] == '#') { con[i].pb(h+j); con[h+j].pb(i); } } } int t = 0; int th=0,tw=0; con[0].pb(h); con[0].pb(h-1); con[0].pb(h+w-1); con[h].pb(0); con[h-1].pb(0); con[h+w-1].pb(0); dfs(0); FOR(i,h) { if(con[i].size() && !vis[i]) { dfs(i);t++; } else if(con[i].empty()) { th++; } } FOR(i,w) { if(con[h+i].size() && !vis[h+i]) { dfs(h+i);t++; } else if(con[h+i].empty()) { tw++; } } //cerr << t << " " << th << " " << tw << endl; cout << t + min(th,tw) << endl; }
#include <bits/stdc++.h> #define f first #define s second #define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' #define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) using namespace std; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; typedef long long lli; typedef pair<int,int> ii; typedef vector<int> vi; #define deb(x) cout << #x": " << (x) << endl; lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);} lli lcm(lli a, lli b){ if(!a || !b) return 0; return a * b / gcd(a, b); } int popcount(lli x) { return __builtin_popcountll(x); } // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);} lli poww(lli a, lli b){ lli res =1; while(b){ if(b&1) res = res * a; a = a*a; b/=2; } return res; } vvc<int> graph(int n, int m, bool dir=1){ vv(int,v,n+1,0); fore(i,0,m){ int a,b; cin>>a>>b; v[a].pb(b); if(dir)v[b].pb(a); } return v; } template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2; // ---- コーディングはここから! ('-')7 void solve(){ using t = array<long double,2>; int n,m; cin>>n>>m; int k; cin>>k; vi v(n+1,0); fore(i,0,k){int a; cin>>a; v[a]=1;} fore(i,0,n){ if(v[i]==0)continue; int j = i; while(j<=n and v[j])j++; if(j-i>=m){cout<<-1<<ENDL;return;} i = j-1; } vc<t>dp(n+m); t sum = {0,0}; for(int i = n+m-1;i>=0;i--){ if(i>=n)dp[i]={0,0}; else if(v[i])dp[i]={1,0}; else dp[i]={sum[0]/(1.0*m),sum[1]/(1.*m)+1}; sum[0]+=dp[i][0]; sum[1]+=dp[i][1]; if(i+m<n+m)sum[0]-=dp[i+m][0],sum[1]-=dp[i+m][1]; } long double ans = dp[0][1]/(1.0-dp[0][0]); cout<<fixed<<setprecision(7)<<ans<<ENDL; } int main(){_ //int t; cin>>t; while(t--) solve(); }
#include<bits/stdc++.h> #define ll long long int #define pii pair<int,int> #define pll pair<ll,ll> #define vpii vector< pii > #define vpll vector< pll > #define mpii map<int,int> #define mpll map<ll,ll> #define MOD 1000000007 #define all(v) v.begin(),v.end() #define s(v) v.size() #define test ll t;cin>>t;while(t--) #define vec vector<ll> #define read0(v,n) for(int i=0;i<n;i++)cin>>v[i]; #define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i]; #define trav(a,x) for (auto& a: x) #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define cut(x) {cout<<x;return 0;} #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORB(i,a,b) for(int i=a;i>=b;i--) #define mp make_pair #define pb push_back #define eb emplace_back #define f first #define sc second #define lb lower_bound #define ub upper_bound #define nl '\n' #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> ll gcd(ll a, ll b) { if (b==0)return a; return gcd(b, a % b); } ll lcm(ll a,ll b) { return (a*b)/gcd(a,b); } ll pow(ll a, ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%MOD; b/=2; a=(a*a)%MOD; } return ans; } const int N=2005; ll a[N][N],row[N][N],col[N][N],dia[N][N]; char g[N][N]; int main() { fast ll n,m; cin>>n>>m; FOR(i,1,n) { string s; cin>>s; FOR(j,1,m) { g[i][j]=s[j-1]; } } FOR(i,1,n) { FOR(j,1,m) { if(i==1 and j==1){a[i][j]=0;row[i][j]=1;col[i][j]=1;dia[i][j]=1;continue;} if(g[i][j]=='#') { row[i][j]=0;col[i][j]=0;row[i][j]=0; a[i][j]=0; } else { ll x=0,y=0,z=0; if(i-1>=1)x=col[i-1][j]; if(i-1>=1 and j-1>=1)y=dia[i-1][j-1]; if(j-1>=1)z=row[i][j-1]; a[i][j]=x+y+z; row[i][j]=a[i][j]+(j-1>=1?row[i][j-1]:0); col[i][j]=a[i][j]+(i-1>=1?col[i-1][j]:0); dia[i][j]=a[i][j]+(j-1>=1 and i-1>=1?dia[i-1][j-1]:0); row[i][j]%=MOD; col[i][j]%=MOD; dia[i][j]%=MOD; a[i][j]%=MOD; } } } // FOR(i,1,n)FOR(j,1,m)cout<<dp[i][j]<<(j==m?nl:' '); cout<<a[n][m]; }
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back using namespace std; typedef long long ll; typedef pair<int,int> pi; const int mod=1000000007; inline int add(int x,int y) { return ((ll)x+y)%mod; } bool g[2005][2005]; int dp[2005][2005],lr[2005][2005],tb[2005][2005],di[2005][2005]; int h,w; int main() { cin.tie(0);cout.tie(0); cin>>h>>w; for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { char c; cin>>c; if(c=='.') g[i][j]=1; } } dp[0][0]=1; lr[0][0]=1; tb[0][0]=1; di[0][0]=1; for(int i=1;i<w;i++) if(g[0][i]) { dp[0][i]=lr[0][i-1]; lr[0][i]=add(dp[0][i],lr[0][i-1]); tb[0][i]=dp[0][i]; di[0][i]=dp[0][i]; } int la; for(int i=1;i<h;i++) { if(g[i][0]) { dp[i][0]=tb[i-1][0]; tb[i][0]=add(tb[i-1][0],dp[i][0]); lr[i][0]=dp[i][0]; di[i][0]=dp[i][0]; } for(int j=1;j<w;j++) if(g[i][j]) { dp[i][j]=add(add(tb[i-1][j],lr[i][j-1]),di[i-1][j-1]); tb[i][j]=add(tb[i-1][j],dp[i][j]); lr[i][j]=add(lr[i][j-1],dp[i][j]); di[i][j]=add(di[i-1][j-1],dp[i][j]); } } cout<<dp[h-1][w-1]<<endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> #include <iostream> #include <string> #include <vector> #include <list> #include <utility> #include <algorithm> #include <functional> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <iomanip> #include <sstream> #include <bitset> #include <limits> #include <numeric> #include <valarray> #include <fstream> #include <array> #include <random> #include <unordered_set> #include <unordered_map> using namespace std; using uint = uint32_t; using LL = int64_t; using ULL = uint64_t; using PP = pair<LL, LL>; template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >; #define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i) #define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i) #define FLOAT fixed << setprecision(16) #define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); } const int INF = 0x3FFFFFFF; const LL INFLL = 0x3FFFFFFF3FFFFFFF; const double INFD = 1.0e+308; const double EPS = 1.0e-9; void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; } template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; } template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); } template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); } template <class T> T Next() { T buf; cin >> buf; return buf; } istream& Ignore(istream& ist) { string s; ist >> s; return ist; } bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; } #ifdef ONLY_MY_ENVIR #include "Accumulator.h" #include "Algebraic.h" #include "BinaryMatrix.h" #include "BinaryTree.h" #include "Bipartite.h" #include "BIT.h" #include "Compressor.h" #include "Decompositions.h" #include "DiscreteLog.h" #include "DominatorTree.h" #include "DynamicMod.h" #include "Exponential.h" #include "Factorization.h" #include "FFT.h" #include "Field.h" #include "FlatTree.h" #include "FlowSolver.h" #include "Geometric2D.h" #include "Geometric2DFloat.h" #include "Geometric3D.h" #include "Geometric3DFloat.h" #include "Graph.h" #include "GraphUtil.h" #include "HLD.h" #include "Interpolation.h" #include "IntMod.h" #include "KDTree.h" #include "LazySegmentTree.h" #include "LCA.h" #include "LIS.h" #include "List.h" #include "Math.h" #include "MathUtil.h" #include "Matrix.h" #include "MinCostFlowSolver.h" #include "MinMax.h" #include "Numbers.h" #include "Optimize.h" #include "Permutation.h" #include "Polynomial.h" #include "Position.h" #include "Quadratic.h" #include "Random.h" #include "Range.h" #include "Rational.h" #include "RollingHash.h" #include "RuntimeMod.h" #include "SafeInt.h" #include "SegmentTree.h" #include "SegmentTree2D.h" #include "Sets.h" #include "Shortest.h" #include "SlidingWindow.h" #include "SpanningTree.h" #include "StringSearching.h" #include "SuffixArray.h" #include "SwitchList.h" #include "Timer.h" #include "Tree.h" #include "TreeUtil.h" #include "UnionFind.h" #include "Util.h" #include "VectorUtil.h" #endif #ifdef __GNUC__ typedef __int128 LLL; istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; } ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; } #endif int main() { int K; cin >> K; int cnt = 0; REP(i, 1, K + 1) { REP(j, 1, K + 1) { if (i * j > K) break; REP(k, 1, K + 1) { if (i * j * k > K) break; ++cnt; } } } cout << cnt << endl; return 0; }
#include<bits/stdc++.h> #define int long long #define forr(i,l,r) for(int i=l;i<=r;i++) #define ffor(i,r,l) for(int i=r;i>=l;i--) using namespace std; signed main() { int a,b,c; cin>>a>>b>>c; cout<<a+b+c-min(a,min(b,c))<<endl; return 0; }
/** * Dont raise your voice, improve your argument. * --Desmond Tutu */ #include <bits/stdc++.h> using namespace std; const bool ready = [](){ ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); return true; }(); using ld=long double; const ld PI = acos((ld)-1); using ll= long long; #define int ll #define all(v) (v).begin(), (v).end() #define fori(n) for(int i=0; i<int(n); i++) #define cini(i) int i; cin>>i; #define cins(s) string s; cin>>s; #define cind(d) ld d; cin>>d; #define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; } #define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; } #define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; } using pii= pair<int, int>; using pdd= pair<ld, ld>; using vd= vector<ld>; using vb= vector<bool>; using vi= vector<int>; using vvi= vector<vi>; using vs= vector<string>; #define endl "\n" /* * dijkstra? * I mean, why not? * ->There are 500^2 vertex, so a lot of edges... * * Is it ever beneficial to go north? * -> Yes, can be :/ * * Can we implement the North moves somewhat faster? * What about starting from the other end? * ...nothing * * Try dijkstra */ const int INF=1e18; void solve() { cini(r); cini(c); vvi a(r, vi(c-1)); for(int i=0; i<r; i++) for(int j=0; j<c-1; j++) cin>>a[i][j]; vvi b(r-1, vi(c)); for(int i=0; i<r-1; i++) for(int j=0; j<c; j++) cin>>b[i][j]; vvi dp(r, vi(c, INF)); dp[0][0]=0; using t3=tuple<int,int,int>; priority_queue<t3> q; /* c, i, j */ q.emplace(0, 0, 0); while(q.size()) { auto [cost,i,j]=q.top(); if(i==r-1 && j==c-1) { cout<<dp[r-1][c-1]<<endl; return; } q.pop(); if(-cost!=dp[i][j]) continue; if(j+1<c && dp[i][j]+a[i][j]<dp[i][j+1]) { dp[i][j+1]=dp[i][j]+a[i][j]; q.emplace(-dp[i][j+1], i, j+1); } if(j>0 && dp[i][j-1]>dp[i][j]+a[i][j-1]) { dp[i][j-1]=dp[i][j]+a[i][j-1]; q.emplace(-dp[i][j-1], i, j-1); } if(i+1<r && dp[i+1][j]>dp[i][j]+b[i][j]) { dp[i+1][j]=dp[i][j]+b[i][j]; q.emplace(-dp[i+1][j], i+1, j); } for(int k=0; k<i; k++) { if(dp[k][j]>dp[i][j]+1+i-k) { dp[k][j]=dp[i][j]+1+i-k; q.emplace(-dp[k][j], k, j); } } } assert(false); } signed main() { solve(); }
#include <bits/stdc++.h> #include <random> #include <unordered_map> #include <unordered_set> using namespace std; #pragma GCC target("avx2") #pragma GCC optimize("O3,unroll-loops") using ll = long long; using ull = unsigned long long; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) for (int(i) = 0; (i) < int(n); (i)++) #define repi(i, a, b) for (int(i) = (a); (i) < int(b); (i)++) #define rep(...) _overload3(__VA_ARGS__, repi, _rep)(__VA_ARGS__) 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; } constexpr int dy[4] = {-1, 0, 1, 0}; constexpr int dx[4] = {0, -1, 0, 1}; constexpr ll MOD = 1'000'000'007LL; /*998'244'353LL;*/ template <typename T> struct Dijkstra { vector<vector<pair<int, short>>> e; vector<T> cost; Dijkstra(int V) : e(V) {} void add(int u, int v, T weight) { e[u].push_back({v, weight}); e[v].push_back({u, weight}); } void add_directed(int from, int to, T weight) { e[from].push_back({to, weight}); } vector<T>& calculate(int s, T def) { cost.resize(e.size(), def); priority_queue<pair<T, int>> pq; cost[s] = 0; pq.push({0, s}); while (!pq.empty()) { int n = pq.top().second; if (-pq.top().first != cost[n]) { pq.pop(); continue; } pq.pop(); for (int i = 0; i < e[n].size(); i++) { if (cost[e[n][i].first] > cost[n] + e[n][i].second) { cost[e[n][i].first] = cost[n] + e[n][i].second; pq.push({-cost[e[n][i].first], e[n][i].first}); } } } return cost; } }; int R, C; Dijkstra<int> G(0); signed main() { cin >> R >> C; G = Dijkstra<int>(2 * R * C); rep(i, R) { rep(j, C - 1) { int a; cin >> a; G.add(i * C + j, i * C + j + 1, a); } } rep(i, R - 1) { rep(j, C) { int a; cin >> a; G.add_directed(i * C + j, (i + 1) * C + j, a); } } rep(i, R) { rep(j, C) { G.add_directed(i * C + j, R * C + i * C + j, 1); G.add_directed(R * C + i * C + j, i * C + j, 0); if (i - 1 >= 0) G.add_directed(R * C + i * C + j, R * C + (i - 1) * C + j, 1); } } cout << G.calculate(0, 2e9)[R * C - 1] << endl; }
#include<bits/stdc++.h> // no of bits to store no 2e5 is 19 // 2e5 ka 5 times square root lene se 2 or 3 ke pass pahunch jate hai using namespace std; #define int long long #define mod 1000000007 #define MAX 1000000000 #define ff first #define ss second #define mp make_pair #define pb push_back #define pii pair<int,int> #define sp(x,y) fixed<<setprecision(y)<<x #define setbits(x) __builtin_popcount(x) #define fast_in_out ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define rep(i,a,b) for(int i=a;i<b;i++) #define ll long long // powermod(a,b,m) == powermod(a,b%(m-1),m) when m is prime fermat's theorem a^(p-1) %p=1 p is prime int powermod(int a,int b,int c) // gives (a^b)%c { int ans = 1,tempans=a%c; while(b>0) { if(b&1==1) { ans=(ans*tempans)%c; } tempans=(tempans*tempans)%c; b=b>>1; } return ans; } int modinverse(int a,int m) // gives a inverse wrt m and m is prime { return powermod(a,m-2,m); } 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; } // my template m // prime factorization pf // ncr and factorials ncr // matrix exponentiation of fibonacci series in log(n) matexpofib // cycles in a graph cycle bool compare(pii p1,pii p2) { int x1=p1.ff,y1=p1.ss; int x2=p2.ff,y2=p2.ss; int on= 2*x1 + y1; int se=2*x2 + y2; return on>se; } void solve() { int n; cin>>n; pii a[n];int sum=0; rep(i,0,n) { cin>>a[i].ff>>a[i].ss; sum+=a[i].ff; } sort(a,a+n,compare); int temp=0;int ans=0; for(int i=0;i<n;i++) { int x=a[i].ff; int y=a[i].ss; sum-=x; temp+=(x+y);ans++; if(temp>sum) { break; } } cout<<ans<<endl; } int32_t main() { fast_in_out; int t=1;//cin>>t; int count=0; while(t--) { count++; solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n],b[n],c[n]; for(int i=0; i<n; i++) { cin >> a[i]; a[i]--; } for(int i=0; i<n; i++) { cin >> b[i]; b[i]--; } for(int i=0; i<n; i++) { cin >> c[i]; c[i]--; } vector<int>cnt(n); for(int j=0; j<n; j++) { cnt[b[c[j]]]++; } long long ans=0; for(int i=0; i<n; i++) ans+=cnt[a[i]]; cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long #define vi vector<int> #define vl vector<long long> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep_lr(i, f, n) for (int i = f; i < (int)(n); i++) #define maxe(v) *max_element(v.begin(),v.end()) #define mine(v) *min_element(v.begin(),v.end()) #define sorts(v) sort(v.begin(),v.end()) #define sortg(v) sort(v.begin(),v.end(),greater<int>) using namespace std; string replace_all(string &s, string from, string to) { ll pos = s.find(from); while(true){ pos = s.find(from, pos); if(pos==-1) break; s.replace(pos, from.length(), to); pos += to.length(); } return s; } void solve(){ setprecision(15); int N; cin >> N; vl x(N); rep(i,N){ cin>>x.at(i); x.at(i) = abs(x.at(i)); } ll ans=0; rep(i,N){ ans += x.at(i); } cout << ans << endl; ans = 0; rep(i,N){ ans += pow(x.at(i),2); } cout << setprecision(20) << sqrtl(ans) << endl; cout << maxe(x) << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <functional> #include <iterator> #include <numeric> #include <cstdint> #include <cmath> #include <bitset> #include <string> #include <list> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <random> #include <chrono> #include <cassert> #define N_TIMES(i, n) for ( uint64_t i = 0; i < n; ++i ) #define N_TIMES_REV(i, n) for ( int64_t i = n - 1; i >= 0; --i ) template<typename T> std::istream& operator>>(std::istream &is, std::vector<T> &v) { for (typename std::vector<T>::size_type i = 0; i < v.size(); ++i) { is >> v[i]; } return is; } template<typename T> const T& max(const std::vector<T> &v) { return *std::max_element(v.begin(), v.end()); } template<typename T> const T& min(const std::vector<T> &v) { return *std::min_element(v.begin(), v.end()); } using namespace std; ostream& operator<<(ostream &os, const vector<int64_t> &a) { for (vector<int64_t>::size_type i = 0; i < a.size(); ++i) { os << a[i]; if (i + 1 < a.size()) { os << ' '; } } return os; } int main() { uint64_t N; cin >> N; string S; cin >> S; vector<int64_t> A(N + 1); cin >> A; vector<int64_t> D(N, 0); N_TIMES(n, N) { D[n] = abs(A[n + 1] - A[n]); } int64_t min_D = min(D); vector< vector<int64_t> > Bs(min_D, vector<int64_t>(N + 1, 0)); N_TIMES(n, N + 1) { N_TIMES(i, min_D) { Bs[i][n] = (A[n] / min_D) + (i < (A[n] % min_D) ? 1 : 0); } } cout << Bs.size() << endl; for (const vector<int64_t> &B : Bs) { cout << B << endl; } return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> using namespace std; #define int long long #define double long double #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define mod 1000000007 #define M2 1000000009 #define M1 998244353 #define inf 1e18 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> new_data_set; bool isprime(int n){ if(n==1) return false; for(int i = 2;i*i<=n;i++){ if(n%i==0) return false; } return true; } int lcm(int a,int b){ return (a*b)/__gcd(a,b); } int gcd(int a, int b){ if (b == 0){ return a; } return gcd(b, a % b); } int power(int x,int y,int p){ int res=1; if(x==0)return 0; while(y>0){ if(y&1){ res*=x; res%=p; } y=y>>1; x=(x*x); x%=p; res%=p; } return res; } int modInverse(int n, int p){ return power(n, p-2,p); } struct node{ int a,b; }; /* BAAP BAAP HOTA HAI Author : Sagar */ signed main(){ FAST int n; cin>>n; int ans=n; int tp=1; for(int i=1;i<60;i++){ tp*=2; if(tp>n)break; int a=n/tp; int c=n%tp; ans=min(ans,a+i+c); } cout<<ans; }
#include <bits/stdc++.h> using namespace std; int main() { int64_t N; cin >> N; int64_t a, b, c; int64_t kai; int64_t ans = -1; if(N == 1) cout << 1 << endl; else { b = 0; kai = 1; while(true) { b++; kai *= 2; if(N < kai) break; a = N / kai; c = N - a * kai; if(ans == -1) ans = a + b + c; else if(a + b + c < ans) ans = a + b + c; } cout << ans << endl; } }
#include<bits/stdc++.h> //#include <atcoder/all> #define rep(i,n)for(int i=0;(i)<(int)(n);i++) #define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++) #define ALL(a) (a).begin(),(a).end() #define pb push_back #define fi first #define se second #define sz(x) ((int)x.size()) using namespace std; //using namespace atcoder; using ld = long double; using ll = long long; using P = pair<ll, ll>; template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; } template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; } const ll ZER = 0; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ll LINF = 1e18; int main(){ ll od = 0, ev = 0; int n, k; cin >> n >> k; rep(i, n){ int cnt = 0; string s; cin >> s; rep(j, sz(s)){ if(s[j] == '1')cnt++; } // cout << cnt << endl; if(cnt % 2 == 1)od++; else ev++; } cout << ev * od << endl; }
#include<bits/stdc++.h> #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define debug(x) cout << #x << ':' << x << endl; #define rep(i,n) for(long long i = 0; i < n ; i++) #define reps(i,n) for(long long i = 1; i <= n ; i++) #define make_unique(v) sort(all(v));v.erase(unique(all(v)), v.end()); #define REMOVE(v,a) v.erase(remove(v.begin(),v.end(), a), v.end()); #define pb push_back #define mp make_pair #define endl '\n' #define fi first #define se second using namespace std; typedef long long ll; typedef vector<long long> vll; typedef pair<ll,ll> pll; const ll MOD = 1e9+7; bool cmp(int X, int Y) {return X > Y;} ll gcd(ll a, ll b) {return !b ? a : gcd(b, a % b);} ll lcm(ll a, ll b) {return (a / gcd(a, b)) * b;} ll modpow(ll a, ll b, ll m=MOD){a %= m; ll res = 1; while (b) {if (b & 1)res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;} ll bpow(ll a, ll b){if(b<0){return 0;}ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;} ll modinv(ll a, ll m = MOD) {return modpow(a, m - 2, m);} const int N = 100005; const int dx4[4] = {0, 1, 0, -1}; const int dy4[4] = {-1, 0, 1, 0}; ll vis[N]; ll fact(ll x){ ll res = 1; for(ll i = 1; i<=x ; i++){ res*=i; res = (res+MOD)%MOD; } return res; } int main(){ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s;cin>>s; ll len = s.length(); if(len<3){ int y = stoi(s); if(y%8 == 0){cout<<"Yes"<<endl;} else{ reverse(all(s)); int tt = stoi(s); if(tt%8 == 0){cout<<"Yes";} else{ cout<<"No"; } } return 0; } vector<ll>se[112]; ll k = 0; for(ll i = 101; i<1000; i++){ if(i%8 == 0){ ll n = i; while(n>0){ ll num = n%10; se[k].push_back(num); n = n/10; } k++; } } map<ll,ll>cnt; rep(i,(ll)s.length()){ cnt[(ll)(s[i] - '0')]++; } bool check = false; for(ll i = 0; i<112; i++){ bool ch = true; map<ll,ll>cnt2; for(auto u : se[i]){ cnt2[u]++; } for(auto u : se[i]){ if(cnt[u] < cnt2[u]){ch = false;} } if(ch && (se[i].size() > 0)){check = true;} } if(check){ cout<<"Yes"<<endl; }else{ cout<<"No"<<endl; } }
#include<stdio.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int x, y, z; cin >> x >> y >> z; cout << (y * z - 1) / x << endl; }
//#include<bits/stdc++.h> #include<iostream> #include<string> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #include<cctype> #include<vector> #include<map> #include<deque> #include<queue> using namespace std; #define re register #define inf 0x3f3f3f3f //#define inf 0x3f3f3f3f3f3f3f3f #define ll long long //#define int ll //#define x first //#define y second typedef pair<int,int> PII; template<typename T> inline void read(T &x){ re char ch=getchar();re bool f=false;x=0; while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();} while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} x=f?-x:x; return ; } template<typename T> inline void write(T x){ if(x<0){putchar('-');x=-x;} if(x>=10){write(x/10);} putchar(x%10+'0'); } signed main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); int x,y,z; read(x),read(y),read(z); int ans; ans=z*y; if(ans%x==0) ans=ans/x-1; else ans/=x; write(ans); return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> #include <cmath> using namespace std; const int INF = 2000000000; double X,Y,Z; int main() { // Step #1. Input cin >> X >> Y >> Z; double ans=Y*Z/X; int re; if(ans-floor(ans)==0){ re=ans-1; }else{ re=ans; } cout << re << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,Xcor,Xstep; cin >> a >> b >> Xcor >> Xstep; int Xupdown= min(2*Xcor, Xstep); int ans= (a <= b) ? Xupdown*(b-a) + Xcor : Xupdown*(a-b-1)+ Xcor; cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; int n,f[100001][10]; string s,x; int main(){ cin>>n; cin>>s>>x; f[n][0]=1; for(int i=n;i>=1;i--){ for(int j=0;j<7;j++){ bool a=f[i][(j*10)%7]; bool b=f[i][(j*10+(s[i-1]-'0'))%7]; if(x[i-1]=='T'){ f[i-1][j]=a|b; } else{ f[i-1][j]=a&b; } } } if(f[0][0]){ cout<<"Takahashi"; } else{ cout<<"Aoki"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ char S,T; cin >> S; cin >> T; if(S=='Y'){ if(T=='a') cout << "A" << endl; if(T=='b') cout << "B" << endl; if(T=='c') cout << "C" << endl; }else{ cout << T << endl; } }
#include<cstdio> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<iomanip> #include<map> #include<unordered_map> #include<set> #include<unordered_set> #include<vector> #include<algorithm> #include<string> #include<queue> using namespace std; #define DEBUG(x) cout<<#x<<"="<<x<<endl #define DEBUG2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl typedef long long ll; const int MAXN=2e5+10; const ll MOD=17; ll _gcd(ll a,ll b){if(b==0)return a;return _gcd(b,a%b);} ll gcd(ll a,ll b){a=abs(a),b=abs(b);if(a<b)swap(a,b);return _gcd(a,b);} ll qpow(ll a,ll n){ll rt=1;while(n){if(n&1)rt=(rt*a)%MOD;a=a*a%MOD;n>>=1;}return rt;} ll factor[MAXN]; void cal_factor(){factor[0]=1;for(int u=1;u<MAXN;u++){factor[u]=(factor[u-1]*u)%MOD;}} ll C(ll n,ll k){if(n<k)return 0;return factor[n]*qpow(factor[n-k],MOD-2)%MOD*qpow(factor[k],MOD-2)%MOD;} void solve(){ multiset<int>dig; string s; cin>>s; for(char c:s){ dig.insert(c-'0'); } vector<int>m8[4]; for(int m=8;m<1000;m+=8){ int len=0; int t=m; while(t){ t/=10; len++; } m8[len].push_back(m); } if(dig.size()==1){ if(*dig.begin()==8){ cout<<"Yes\n"; }else { cout<<"No"; } return; }else { int f=dig.size()>2?3:2; bool match=false; for(int m:m8[f]){ int t=m; multiset<int>tdig=dig; bool ed=true; while(t){ if(tdig.count(t%10)==0){ ed=false;break; }else tdig.erase(tdig.find(t%10)); t/=10; } if(ed){ match=true; break; } } if(match)cout<<"Yes\n"; else cout<<"No\n"; } } // #define LOCAL int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif ios::sync_with_stdio(false); cin.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; //using mint = modint1000000007; //using mint = modint998244353; template <typename T> bool chmax(T &u, const T z) { if (u < z) {u = z; return true;} else return false; } template <typename T> bool chmin(T &u, const T z) { if (u > z) {u = z; return true;} else return false; } #define ll long long #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) typedef pair<ll, int> P; ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b){return a * b / gcd(a, b);} //解説写し int main(){ int n; cin>>n; int m=5; vector<vector<int>>a(n,vector<int>(m)); rep(i,n)rep(j,m)cin>>a[i][j]; int l=0,r=1001001001; while(r-l>1){ int mid=(l+r)/2; vector<int>s; rep(i,n){ int x=0; rep(j,m){ if(a[i][j]>=mid)x|=1<<j; } s.push_back(x); } sort(s.begin(),s.end()); s.erase(unique(s.begin(),s.end()),s.end()); bool ok=false; rep(i,s.size())rep(j,i+1)rep(k,j+1){ if((s[i]|s[j]|s[k])==(1<<m)-1)ok=true; } if(ok)l=mid;else r=mid; } cout<<l<<endl; return 0; }
#include <bits/stdc++.h> using Int = long long; // clang-format off using pii = std::pair<Int, Int>; #define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define RREP_(i, a_, b_, a, b, ...) for (Int i = Int(b) - 1, low##i = (a); i >= low##i; i--) #define RREP(i, ...) RREP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define ALL(v) std::begin(v), std::end(v) struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io; #ifndef dump #define dump(...) #endif // clang-format on struct in { template <class T> operator T() { T t; std::cin >> t; return t; } }; void out() { std::cout << "\n"; } template <class Head, class... Tail> void out(Head&& h, Tail&&... t) { std::cout << h << (sizeof...(Tail) == 0 ? "" : " "), out(std::forward<Tail>(t)...); } template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; } template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; } template <class T> using V = std::vector<T>; /** * author: knshnb * created: Sun May 23 20:22:56 JST 2021 **/ /// @docs src/DataStructure/SegmentTree.md template <class T, class F> struct SegmentTree { const F op; const T e; SegmentTree(F op_, T e_) : op(op_), e(e_) {} int n; std::vector<T> t; void set_by_identity(int n_) { n = n_; t.clear(), t.resize(2 * n, e); } void set_by_vector(const std::vector<T>& a) { n = a.size(); t.clear(), t.resize(2 * n, e); for (int i = 0; i < n; i++) t[i + n] = a[i]; build(); } void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); } T& operator[](int i) { return t[i + n]; } // [l, r) T query(int l, int r) const { assert(0 <= l && l <= r && r <= n); T resl = e, resr = e; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = op(resl, t[l++]); if (r & 1) resr = op(t[--r], resr); } return op(resl, resr); } // i番目をxに変更 void update(int i, const T& x) { assert(0 <= i && i < n); t[i += n] = x; while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } // i番目にxを作用 (a[i] = op(a[i], x)) void operate(int i, const T& x) { assert(0 <= i && i < n); i += n; t[i] = op(t[i], x); while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } friend std::string to_string(const SegmentTree<T, F>& seg) { return to_string(std::vector<T>(seg.t.begin() + seg.n, seg.t.end())); } }; template <class T, class F> auto make_segment_tree(F op, T e) { return SegmentTree<T, F>(op, e); } signed main() { Int n = in(); V<Int> a(n), b(n); REP(i, n) a[i] = in(); REP(i, n) b[i] = in(); std::reverse(ALL(a)); std::reverse(ALL(b)); REP(i, n) a[i] -= i; REP(i, n) b[i] -= i; std::map<Int, std::set<Int>> mpb; V<Int> idx(n); REP(i, n) { mpb[b[i]].insert(i); } REP(i, n) { auto it = mpb[a[i]].begin(); if (it == mpb[a[i]].end()) { out(-1); return 0; } idx[i] = *it; mpb[a[i]].erase(it); } dump(idx); Int ans = 0; auto seg = make_segment_tree<Int>([](Int x, Int y) { return x + y; }, 0); seg.set_by_identity(n); REP(i, n) { ans += i - seg.query(0, idx[i]); seg.operate(idx[i], 1); } out(ans); }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> #include<stack> #include<bitset> #include<climits> #include<complex> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';} template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #define int long long template<typename T> void dbg_a(T *a,int l,int r){cout<<" {";for(int i = l;i<r;i++) cout<<a[i]<<", ";cout<<a[r]<<"}"<<endl;} typedef long long ll; template<typename T = int> inline T read(){ T f = 1,r = 0; char c = getchar(); while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();} while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();} return f*r; } typedef pair<int,int> PII; #define fi first #define se second #define mst(a,b) memset(a,b,sizeof(a)) #define For(i,a,b) for(int i = a;i<=b;i++) #define For_(i,a,b) for(int i = a;i>=b;i--) #define _for(i,a) for(int i = 0;i<a;i++) #define All(x) x.begin(),x.end() // For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",a,b,c); const int N = 50 + 2,M = N*2 + 10,INF = 0x3f3f3f3f; const int mod = 1e9 + 7; int n,m,x; int a[N],mx[N],dp[N][2],k[N]; signed main(){ n = read();x = read(); For(i,1,n) a[i] = read(); For(i,1,n-1) mx[i] = a[i+1]/a[i]; For_(i,n,1){ k[i] = x/a[i]; x %= a[i]; } dp[1][0] = 1; if(k[1]) dp[1][1] = 1; For(i,1,n-1){ dp[i+1][0] = dp[i][0]; if(k[i + 1]) dp[i+1][1] = dp[i][0]; dp[i+1][1] += dp[i][1]; if(k[i+1] + 1 != mx[i + 1]) dp[i+1][0] += dp[i][1]; } cout<<dp[n][0]<<"\n"; return 0; }
//dragon->emperor #include <bits/stdc++.h> using namespace std; /**********************************************************************************/ #define mod 1000000007 #define mod2 998244353 const double pi = acos(-1); #define inf 1e18+5 #define iinf 1e9+5 #define ll long long #define ld long double #define floop(i,a,n) for(ll i=a;i<n;i++) #define loopf(i,a,n) for(ll i=n-1;i>=a;i--) #define space " " #define TEST(t) ll t; cin>>t; while(t--) #define r(n) ll n; cin>>n; #define r1(a,n) ll a[n]; for(ll i=0;i<n;i++) cin>>a[i]; #define p1(a,n) for(ll i=0;i<n;i++) cout<<a[i]<<" "; cout<<endll; #define p2(n) cout<<n<<" "; #define p(n) cout<<n<<endll; #define endll "\n" #define pb push_back #define mp make_pair #define dec(n) fixed<<setprecision(n) #define F first #define S second #define I insert #define E end() #define B begin() #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define reverseall(x) reverse(all(x)) typedef vector<ll> vl; typedef pair<ll,ll> pll; typedef map<ll,ll> mll; /************************************FUNCTIONS*************************************/ bitset<200000>vis(0); vector<bool> prime; void primes(ll n) { prime.resize(n+1); prime.assign(n+1,true); prime[0]=0; prime[1]=0; for(ll i=2; i*i<=n; i++) { if(prime[i]) { for(ll j=(i*i); j<n; j=j+i) prime[j]=false; } } } void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } ll gcd(ll x, ll y) { if(x==0) return y; return gcd( y%x ,x); } ll powM(ll x, ll y, ll m) { ll ans=1, r=1; x=x%m; while(r>0 && r<=y) { if(r & y) { ans=ans*x; ans=ans%m; } r=r<<1; x=x*x; x=x%m; } return ans; } ll powN(ll a, ll n) { ll ans=1; while(n>0) { if(n & 1) ans=ans*a; a=a*a; n >>= 1; } return ans; } ll modinv(ll a,ll m) { return powM(a,m-2,m); } ll ncr(ll n, ll r, ll m) { if(n<r) return 0; if(n==r || r==0) return 1; if(r==1) return n; if(n-r<r) return ncr(n,n-r,m); ll a=n; ll b=r; for(ll i=1; i<r; i++) { a=(a*(n-i))%m; b=(b*(r-i))%m; } b=modinv(b,m); a=(a*b)%m; return a; } ll countbits(ll n) { ll cot=0; while(n) { cot++; n >>= 1; } return cot; } ll check_prime(ll n) { ll ans=1; for(ll i=2; i*i<=n; i++) { if(n%i==0) { ans=0; break;} } if(n==0) ans=0; return ans; } /*************************************CODE*****************************************/ int main() { fast(); ll t=1; // cin>>t; while(t--) { ll n; cin>>n; vector<ll> a(n); floop(i,0,n) cin>>a[i]; sortall(a); ll ans; ans=gcd(a[0],a[1]); floop(i,2,n) { ans=gcd(ans,a[i]); } p(ans); } return 0; }
#include <bits/stdc++.h> using namespace std; #define PB push_back #define ll long long #define endl "\n" #define uwu \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define F0R(i, a, b) for (int i=a; i<(b); i++) #define FOR(i, a) for (int i=0; i<(a); i++) void solve() { int n; cin >> n; cout << n - 1 << endl; } int main() { uwu ll t = 1; while (t--) { solve(); } return 0; }
//Coded by Chirath Nirodha #include<bits/stdc++.h> #define F first #define S second #define PB push_back #define MP make_pair typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; const ll mod=1e9+7; inline void io(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } pair<int,int> s; int t[50][50]; ll p[50][50]; ll dp[50][50]; bool visited[50][50]; string path[50][50]; void makevisit(int a,int b){ visited[a][b]=true; if(a!=49 && t[a+1][b]==t[a][b])visited[a+1][b]=true; if(a!=0 && t[a-1][b]==t[a][b])visited[a-1][b]=true; if(b!=49 && t[a][b+1]==t[a][b])visited[a][b+1]=true; if(b!=0 && t[a][b-1]==t[a][b])visited[a][b-1]=true; } void makeunvisit(int a,int b){ visited[a][b]=false; if(a!=49 && t[a+1][b]==t[a][b])visited[a+1][b]=false; if(a!=0 && t[a-1][b]==t[a][b])visited[a-1][b]=false; if(b!=49 && t[a][b+1]==t[a][b])visited[a][b+1]=false; if(b!=0 && t[a][b-1]==t[a][b])visited[a][b-1]=false; } ll ans=dp[s.F][s.S]; string as; void dfs(int a,int b){ if(a!=49 && t[a+1][b]!=t[a][b] && visited[a+1][b]==false){ if(dp[a][b]+p[a+1][b]+10>=dp[a+1][b]){ path[a+1][b]=path[a][b]; path[a+1][b].PB('D'); dp[a+1][b]=dp[a][b]+p[a+1][b]; if(ans<dp[a+1][b]){ ans=dp[a+1][b]; as=path[a+1][b]; } makevisit(a+1,b); dfs(a+1,b); makeunvisit(a+1,b); } } if(a!=0 && t[a-1][b]!=t[a][b] && visited[a-1][b]==false){ if(dp[a][b]+p[a-1][b]+10>=dp[a-1][b]){ path[a-1][b]=path[a][b]; path[a-1][b].PB('U'); dp[a-1][b]=dp[a][b]+p[a-1][b]; if(ans<dp[a-1][b]){ ans=dp[a-1][b]; as=path[a-1][b]; } makevisit(a-1,b); dfs(a-1,b); makeunvisit(a-1,b); } } if(b!=49 && t[a][b+1]!=t[a][b] && visited[a][b+1]==false){ if(dp[a][b]+p[a][b+1]+10>=dp[a][b+1]){ path[a][b+1]=path[a][b]; path[a][b+1].PB('R'); dp[a][b+1]=dp[a][b]+p[a][b+1]; if(ans<dp[a][b+1]){ ans=dp[a][b+1]; as=path[a][b+1]; } makevisit(a,b+1); dfs(a,b+1); makeunvisit(a,b+1); } } if(b!=0 && t[a][b-1]!=t[a][b] && visited[a][b-1]==false){ if(dp[a][b]+p[a][b-1]+10>=dp[a][b-1]){ path[a][b-1]=path[a][b]; path[a][b-1].PB('L'); dp[a][b-1]=dp[a][b]+p[a][b-1]; if(ans<dp[a][b-1]){ ans=dp[a][b-1]; as=path[a][b-1]; } makevisit(a,b-1); dfs(a,b-1); makeunvisit(a,b-1); } } } void solve(){ cin>>s.F>>s.S; for(int i=0;i<50;i++)for(int j=0;j<50;j++)cin>>t[i][j]; for(int i=0;i<50;i++)for(int j=0;j<50;j++){cin>>p[i][j];dp[i][j]=p[i][j];} int a=s.F,b=s.S; makevisit(a,b); dfs(a,b); cout<<as<<endl; } int main(){ io(); solve(); //int t;cin>>t;for(int i=0;i<t;i++)solve(); return 0; }
#pragma GCC optimize("O3", "unroll-loops") #define fs first #define sc second #include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; const int maxn = 2e5 + 10; long long n, m, h[maxn], wi, pre1[maxn], pre2[maxn], pos, ans = 1e18; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> h[i]; sort(h + 1, h + n + 1); for (int i = 2; i <= n; i += 2) { pre1[i] = pre1[i - 2] + (h[i] - h[i - 1]); pre2[i + 1] = pre2[i - 1] + (h[i + 1] - h[i]); } for (int j = 0; j < m; j++) { cin >> wi, pos = lower_bound(h + 1, h + n + 1, wi) - h; if (pos % 2) ans = min(ans, pre1[pos - 1] + (h[pos] - wi) + (pre2[n] - pre2[pos])); else ans = min(ans, pre1[pos - 2] + (wi - h[pos - 1]) + (pre2[n] - pre2[pos - 1])); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long long #define forn(i, n) for (int i = 0; i < n; i++) #define fori(i, a, b) for (int i = a; i <= b; i++) #define ff first #define ss second int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); LL n, m; cin >> n >> m; vector<LL> a(n), b(m); forn(i, n) cin >> a[i]; forn(i, m) cin >> b[i]; LL dp[1005][1005]; forn(i, n + 1) forn(j, m + 1) { if (i == 0) dp[i][j] = j; else if (j == 0) dp[i][j] = i; else dp[i][j] = min(dp[i][j - 1] + 1, min(dp[i - 1][j] + 1, dp[i - 1][j - 1] + (a[i - 1] != b[j - 1]))); } cout << dp[n][m] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long signed main(){ cin.tie(nullptr)->sync_with_stdio(false); int n,k; cin >> n >> k; for(int i = 0; i<k; i++){ string s = to_string(n); sort(s.begin(),s.end()); int low = stoll(s); reverse(s.begin(),s.end()); int high = stoll(s); n = high-low; } cout << n << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long int /**********************************************/ void solve(ll n, ll k) { ll x = n; while (k--) { string s = to_string(x); sort(s.begin(), s.end(), greater<char>()); ll g1 = stoll(s); reverse(s.begin(), s.end()); ll g2 = stoll(s); x = g1 - g2; } cout << x << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t = 1; // cin >> t; while (t--) { ll n, k; cin >> n >> k; solve(n, k); } }
#include <bits/stdc++.h> #define int long long int #define vi vector<int> #define vvi vector<vector<int>> #define u_m unordered_map<int,int> #define u_s unordered_set<int> #define pii pair<int,int> #define pb(a) push_back(a) #define forr(a,n) for(int i=a;i<n;i++) #define forrr(j,a,n) for(int j=a;j<n;j++) #define mp make_pair #define ff first #define ss second #define clr(val) memset(val, 0, sizeof(val)) #define read(a) for(auto &i:a) cin>>i #define display(a) for(auto i:a) cout<<i<<" " #define MOD 1000000007 inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; } inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; } inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; } using namespace std; void solve(); int32_t main() { solve(); } void solve(){ string n;int k; cin>>n>>k; int a = stoi(n); while(k--){ int b=0; sort(n.begin(),n.end()); string x = n; reverse(n.begin(),n.end()); string y =n; b = stoi(y)-stoi(x); if(a==b) break; else {a = b;n=to_string(b);} } cout<<a<<endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; if(a!=b && b!=c && c!=a){ cout<<"0"<<endl; } else{ // int sum=a+b+c; if(a==b) cout<<c<<endl; else if(b==c) cout<<a<<endl; else cout<<b<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; #define debug(args...) (Debugger()), args class Debugger { public: Debugger(const std::string &_separator = " => ") : first(true), separator(_separator) {} template <typename ObjectType> Debugger &operator,(const ObjectType &v) { if (!first) cerr << separator; cerr << v; first = false; return *this; } ~Debugger() { cerr << endl; } private: bool first; string separator; }; #define debug_var(x) debug(#x, x) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define rep(i, a, b) for (int i = a; i < b; i++) #define all(container) container.begin(), container.end() #define rall(container) container.rbegin(), container.rend() #define forin(it, container) for (auto it = container.begin(); it != container.end(); it++) #define rforin(it, container) for (auto it = container.rbegin(); it != container.rend(); it++) typedef long l; typedef long long ll; typedef pair<int, int> ipi; typedef pair<ll, ll> lpl; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; #define MOD 1'000'000'007 ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll xpow(ll a, ll b, ll m) { ll res = 1; while (b > 0) { if (b & 1) { res = (res * a) % m; } a = (a * a) % m; b /= 2; } return res; } ll xmul(ll a, ll b, ll m) { ll res = 0; ll v = 1, k; while (b > 0) { if (b & 1) { k = (a * v) % m; res = (res + k) % m; } v = (v * 2) % m; b /= 2; } return res; } int main() { int n; ll ans = LONG_LONG_MAX; ll a, p, x; cin >> n; rep(i, 0, n) { cin >> a >> p >> x; if (x > a) { chmin(ans, p); } } cout << (ans == LONG_LONG_MAX ? -1 : ans); return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n; cin >> n; vector<string> s(3); vector<int> a(4); for (int i = 0; i < 3; i++) { cin >> s[i]; a[(s[i][0] - '0') * 2 + (s[i].back() - '0')] = 1; } for (int i = 0; i < 4; i++) { if (!a[i]) { string ans; if (i == 0) { ans += string(n, '0'); ans += "1"; ans += string(n, '0'); } else if (i == 1) { ans += string(n, '1'); ans += string(n, '0'); ans += "1"; } else if (i == 2) { ans += string(n, '0'); ans += string(n, '1'); ans += "0"; } else { ans += string(n, '1'); ans += "0"; ans += string(n, '1'); } cout << ans << '\n'; break; } } } return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s >> s >> s; for (int i = 0; i < n; i++) cout << "1"; for (int i = 0; i < n; i++) cout << "0"; cout << 1 << endl; } return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; #define rep(i,x,y) for(ll i=(x);i<(y);i++) #define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--) #define all(x) (x).begin(),(x).end() #define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");} #ifdef LOCAL #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl #define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl #define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl #define dassert(...) assert(__VA_ARGS__) #else #define debug(x) #define debugbit(x, n) #define itrdebug(x) #define dassert(...) #endif //#define int long long //using mint = atcoder::modint; typedef long long ll; const ll MOD = 1e9 + 7; const long double EPS = 1e-8; void solve(long long x){ cout << ((x >= 0) ? x : 0) << endl; } signed main(){ // ios_base::sync_with_stdio(false); // cin.tie(NULL); long long x; scanf("%lld",&x); solve(x); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define ndl cout << '\n'; #define yes cout<<"YES"<<'\n'; #define no cout << "NO" << '\n'; #define TC int ___T,case_n = 1;scanf("%d ",&___T);while(___T-->0) int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll x; cin>>x; if(x>=0)cout << x<< endl; else cout << 0 << endl; return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int maxN = 1e2 + 10; int n, a[maxN], k = 1e4; int read(); string s; int main(){ n = read(); n++; cin >> s; a[0] = read(); int t = 0; for(int i = 1; i < n; i++){ a[i] = read(); k = min(k, abs(a[i] - a[i - 1])); } printf("%d\n", k); while(k--){ for(int i = 0; i < n; i++){ printf("%d ", a[i] / (k + 1)); a[i] -= a[i] / (k + 1); } printf("\n"); } return 0; } int read(){ int s = 0, f = 1; char ch = getchar(); while(!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * f; }
#include <iostream> #include <string> #include <complex> #include <cstdio> #include<functional> #include<cmath> #include<string> #include<ctime> #include<cstring> #include<algorithm> using namespace std; typedef complex<double> cp; #define ll long long #define sci(x) scanf("%d",&x) #define scl(x) scanf("%lld",&x) #define scf(x) scanf("%lf",&x) #define fo(a,b,c)for(int a=b;a<=c;a++) #define getname(x) #x template<typename T> inline void printT(T *arr,int n) { for(int i=0;i<n;i++) cout<<arr[i]<<' '; cout<<endl; } inline void printcarr(cp *arr,int n,string d) { for(int i=0;i<n;i++) { cout<<(int)arr[i].real()<<' '; } cout<<d<<endl; } inline void printBit(int x) { for(int i=0;i<20;i++) cout<<bool(x&(1<<i)); cout<<endl; } inline void copyarr(int *arr1,int *arr2,int n) { for(int i=0;i<n;i++)*(arr1+i)=*(arr2+i); } inline void randarr(int *arr,int n) { srand(time(0)); for(int i=0;i<n;i++)*(arr+i)=rand()%100; } inline void qsctr(char *a,int &len) { len=0; char c; while(c=getchar(),!isdigit(c)); while(isdigit(c)){a[len++]=c;c=getchar();} a[len]='\0'; } inline void showint(int *arr,int n) { for(int i=0;i<n;i++) cout<<arr[i]<<' '; cout<<endl; } inline ll read(ll mod)//快速输入(取模) { char ch; ll ans=0;bool isN=false; while(ch=getchar(),!isdigit(ch)&&ch!='-'); if(ch=='-')isN=true; else ans=(ch^48); while(ch=getchar(),isdigit(ch)){ans=(ans<<3)+(ans<<1)+(ch^48);ans%=mod;} if(isN)ans*=-1; return ans; } inline ll QJY(ll *a,ll n,ll x,ll mod)//秦九韶算法 快速计算多项式的和(02n)取模 { ll ans=0; for(int i=n;i>0;i--) ans=(ans*x%mod+a[i-1])%mod; return ans; } inline void init(ll* eular,ll maxn)//埃氏筛 不能用欧拉 欧拉不全面 而且从 j=i开始 { for(int i=0;i<1e5+5;i++)eular[i]=i; for(int i=2;i<1e5+5;i++) { if(eular[i]==i) for(int j=i;j<=maxn;j+=i) eular[j]=eular[j]/i*(i-1); } } inline void elp(ll *jug,ll *pri,ll maxn)//欧拉筛 jug为最小质因子 maxn为数据范围 { for(ll i=2;i<maxn;i++) { if(!jug[i]) { pri[++pri[0]]=i; jug[i]=i; } for(ll j=1;j<=pri[0];j++) { if(1LL*i*pri[j]>maxn)break; jug[i*pri[j]]=pri[j]; if(i%pri[j]==0)break;//放后面 } } } inline void dispose(ll x,ll *pri,ll *ert,ll *stp)//分解质因子 数据 质因子集合 质因子个数集合 分解后质因子集合 { for(ll j=1;j<=pri[0]&&x>=pri[j]*pri[j];j++) { if(x%pri[j]==0) { stp[++stp[0]]=pri[j];++ert[0]; while(x%pri[j]==0){x/=pri[j];ert[ert[0]]++;} } } if(x>1) {ert[++ert[0]]++;stp[++stp[0]]=x;} } inline void disposefact(ll x,ll *p,ll *ans)//分解阶乘的质因子 p为x的质因子集合 ans为答案集合 { for(int i=1;i<=p[0];i++)//阶乘分解的原理 x的幂在(1~n)自身的倍数中,出现总次数恰好等于幂次。 { ll now=p[i]; ans[i]=0; while(now<=x) { ans[i]+=x/now; now*=p[i]; } } } inline ll ext_gcd(ll a,ll b,ll &x,ll &y) { if(b==0) { x=1;y=0; return a; } ll r=ext_gcd(b,a%b,y,x);//x=y' y=x'-(a/b)*y' -> y=x' x=y' y-=(a/b)*x; return r; } int n; string s; const int maxn=200; int st[maxn]; bool judge(int mid) { for(int i=1;i<n;i++){ if(st[i]/mid==st[i+1]/mid) return false; } return true; } int main() { cin>>n; n++; cin>>s;for(int i=1;i<=n;i++)sci(st[i]); int l=10001; for(int i=1;i<n;i++){ l=min(l,abs(st[i+1]-st[i])); } cout<<l<<endl; for(int i=1;i<=l;i++){ for(int j=1;j<=n;j++) cout<<(st[j]+i-1)/l<<' '; cout<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; //int:2*10**9 typedef long double ld; typedef pair<ll,ll> P; #define REP(i,n) for(ll i = 0; i<(ll)(n); i++) #define FOR(i,a,b) for(ll i=(a);i<=(b);i++) #define FORD(i,a,b) for(ll i=(a);i>=(b);i--) #define vec2(name,i,j,k) vector<vector<ll>> name(i,vector<ll>(j,k)) #define vec3(name,i,j,k,l) vector<vector<vector<ll>>> name(i,vector<vector<ll>>(j,vector<ll>(k,l))) #define pb push_back #define MOD 1000000007 #define PI 3.141592653 #define INF 100000000000000 //14 //cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); void solve() { ll n, a, b; cin >> n >> a >> b; if (a<b) swap(a,b); if (n<a+b){ cout << 0 << endl; return; } ll x = ((n-a-b+1)*(n-a-b+2)/2)%MOD; ll z; if (n-a>=b) { z=(((b-1)*b/2)%MOD+((n-a-b+1)*(b-1))%MOD)%MOD; } else { z=((n-a)*(n-a+1)/2)%MOD; } ll y = (((a-b+1)*(n-a+1))%MOD+(2*z%MOD))%MOD; ll ans = (((4*x)%MOD)*((x+y)%MOD))%MOD; cout << ans << endl; } int main(){ ll t; cin >> t; REP(i,t) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define debug(n) cerr << #n << ':' << n << endl; #define dline cerr << __LINE__ << endl; using ll = long long; template<class T, class U> using P = pair<T,U>; template<class T> using Heap = priority_queue<T>; template<class T> using heaP = priority_queue<T,vector<T>,greater<T>>; template<class T,class U> using umap = unordered_map<T,U>; template<class T> using uset = unordered_set<T>; template<class T> bool ChangeMax(T&a,const T&b){ if(a >= b) return false; a = b; return true; } template<class T> bool ChangeMin(T&a,const T&b){ if(a <= b) return false; a = b; return true; } template<class T, size_t N, class U> void Fill(T (&a)[N], const U&v){ fill((U*)a,(U*)(a+N),v); } template<class T> istream& operator >> (istream&is, vector<T>&v){ for(auto&e:v)is >> e; return is; } template<long long mod> class modint{ public: long long num; long long extgcd(long long a, long long b, long long&x, long long&y)const { long long d = a; if(b != 0){d = extgcd(b, a%b, y, x); y -= (a/b) * x;} else{x = 1; y = 0;} return d; } long long modinverse()const{ long long x, y; extgcd(num, mod, x, y); return (mod + x%mod)%mod; } modint():num(0){} modint(const long long x):num((x < 0?(x+mod):x)%mod ){} friend modint operator+(const modint&a, const modint&b) { return modint(a.num + b.num); } friend modint operator-(const modint&a, const modint&b) { return modint(a.num - b.num); } friend modint operator*(const modint&a, const modint&b) { return modint(a.num * b.num); } friend modint operator/(const modint&a, const modint&b) { return modint(a*b.modinverse()); } modint& operator+=(const modint&b) { return (*this) = (*this) + b; } modint& operator-=(const modint&b) { return (*this) = (*this) - b; } modint& operator*=(const modint&b) { return (*this) = (*this) * b; } modint& operator/=(const modint&b) { return (*this) = (*this) / b; } modint& operator++() { (*this)+=1; return (*this); } modint& operator--() { (*this)-=1; return (*this); } modint operator++(int){ modint tmp(*this); ++(*this); return tmp; } modint operator--(int){ modint tmp(*this); --(*this); return tmp; } modint pow(unsigned long long i){ if(i == 0) return 1; if(i & 1){ return (*this) * (this->pow(i-1)); } modint<mod>hpow = (this->pow(i>>1)); return hpow * hpow; } friend ostream& operator<<(ostream&os, const modint&a){ os << a.num; return os; } friend istream& operator>>(istream&is, modint&a){ long long x; is >> x; a = x; return is; } }; constexpr int mod = 1'000'000'007; using mint = modint<mod>; mint S(mint n, mint a,mint b){ mint x = n - (a+b) + 1; return x*(x+1); } int main(){ int t; cin >> t; for(int sdfafds = 0; sdfafds < t; ++sdfafds){ mint n,a,b; cin >> n >> a >> b; if(a.num+b.num > n.num){ cout<< 0 << endl; continue; } mint x = S(n,a,b); cout << (n-a+1)*(n-b+1)*x*2-x*x << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; bool vis[17][17]; int h,w,a,b; int ans; void dfs(int x,int y,int a,int b) { if(x==h&&y==w+1) { ans++; return ; } if(y==w+1) { dfs(x+1,1,a,b); return ; } if(vis[x][y]) { dfs(x,y+1,a,b); return ; } if(a) { if(y!=w&&vis[x][y+1]==0) { vis[x][y]=1; vis[x][y+1]=1; dfs(x,y+2,a-1,b); vis[x][y+1]=0; vis[x][y]=0; } if(x!=h&&vis[x+1][y]==0) { vis[x][y]=1; vis[x+1][y]=1; dfs(x,y+1,a-1,b); vis[x+1][y]=0; vis[x][y]=0; } } if(b) { vis[x][y]=1; dfs(x,y+1,a,b-1); vis[x][y]=0; } } int main() { scanf("%d%d%d%d",&h,&w,&a,&b); dfs(1,1,a,b); printf("%d",ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define all(x) (x).begin(),(x).end() #define SIZE(x) ll(x.size()) template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long ll; const long long INF = 1LL << 60; const long long MOD = 1000000007; typedef pair<int, int> P; int main() { int H, W, A, B; cin >> H >> W >> A >> B; vector<vector<int>> Y, T; rep(bit, 1 << (H * W)) { vector<int> t; rep(i, H * W) { if ((1 << i) & bit) { t.push_back(i); } } vector<vector<int>> check(H, vector<int>(W + 1)), check2(H + 1, vector<int>(W)); bool yb = true, tb = true; rep(i, t.size()) { int h = t[i] / W; int w = t[i] - (h * W); if ((W - 1) == w) yb = false; if ((H - 1) == h) tb = false; check[h][w] += 1; check[h][w + 1] += 1; check2[h][w] += 1; check2[h + 1][w] += 1; } rep(i, H) { rep(j, W) { if (check[i][j] >= 2) yb = false; if (check2[i][j] >= 2) tb = false; } } if (tb) { T.push_back(t); } if (yb) { Y.push_back(t); } } ll ans = 0; rep(i, Y.size()) { int cy = (int)Y[i].size(); rep(j, T.size()) { int ct = (int)T[j].size(); if ((cy + ct) != A) continue; vector<vector<int>> check(H, vector<int>(W)); bool flag = true; rep(k, Y[i].size()) { int h = Y[i][k] / W; int w = Y[i][k] - (h * W); check[h][w]++; check[h][w + 1]++; } rep(k, T[j].size()) { int h = T[j][k] / W; int w = T[j][k] - (h * W); if (check[h][w] >= 1 || check[h + 1][w] >= 1) { flag = false; break; } } if (flag) ans++;/* if (flag) { cout << "Y: "; rep(k, Y[i].size()) { cout << Y[i][k] << " "; }cout << endl; cout << "T: "; rep(k, T[j].size()) { cout << T[j][k] << " "; }cout << endl; }*/ } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define MAXX 300005 #define pb push_back #define fixx(n) fixed << setprecision(n) #define F first #define S second #define PI 3.14159265358979323846 #define ll long long int #define int ll #define double long double const int mod = 1e6 + 11; const int N = 1e10; int solve(double &x, double &y, double &r) { int ans = 0; r = nextafter(r, 1e9); int y2 = floor(y + r), y1 = ceil(y - r); for(; y1 <= y2; y1 ++) { double l = sqrt(r * r - (y - y1)*(y - y1)); int x1 = ceil(x - l), x2 = floor(x + l); ans += x2 - x1 + 1; } return ans; } signed main() { FAST; double x, y, r; cin >> x >> y >> r; cout << solve(x, y , r); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;} const char newl = '\n'; int main() { double xf,yf,rf; cin >> xf >> yf >> rf; ll x = round(xf*10000),y = round(yf*10000),r = round(rf*10000); ll ans = 0; for (ll i = (y-r)/10000;i <= (y+r)/10000;i += 1) { ll ok,ng; ok = x/10000;ng = -2e5-1; while (abs(ok-ng) > 1) { ll mid = (ok+ng)/2; ((i*10000-y)*(i*10000-y)+(mid*10000-x)*(mid*10000-x) > r*r ? ng : ok) = mid; } if ((i*10000-y)*(i*10000-y)+(ok*10000-x)*(ok*10000-x) <= r*r) ans += x/10000-ok+1; ok = x/10000+1;ng = 2e5+1; while (abs(ok-ng) > 1) { ll mid = (ok+ng)/2; ((i*10000-y)*(i*10000-y)+(mid*10000-x)*(mid*10000-x) > r*r ? ng : ok) = mid; } if ((i*10000-y)*(i*10000-y)+(ok*10000-x)*(ok*10000-x) <= r*r) ans += ok-(x/10000+1)+1; } cout << ans << newl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define mii map <int, int> #define mll map <ll, ll> #define pii pair <int, int> #define pll pair <ll, ll> #define vi vector <int> #define vd vector <double> #define vll vector <ll> #define fi first #define se second #define si set <int> #define sll set <ll> #define spii set <pii> #define vs vector <string> #define vpii vector <pair <int, int> > #define vpll vector <pair <long long, long long> > #define vvi vector <vector <int> > #define vvpii vector <vector <pii > > #define vb vector <bool> #define vvb vector <vb> #define mp make_pair #define vvll vector <vll> #define vsi vector <si> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define MANX MAXN #define itn int #define dbg(x); {cout << #x << "=" << x << ", ";} #define in(x); { for (auto &to : x) cin >> to;} #define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; } const ll INFLL = 1e18; const int MAXN = 1e6+100; const ll INF = 1e9; const int mod1 = 1e9+7; const int mod2 = 1e9+21; void solve() { int n,m; cin>>n>>m; vvi g(n); while (m--){ int u,v; cin>>u>>v; u--,v--; g[u].pb(v); g[v].pb(u); } int k; cin>>k; if (k==1){ cout<<1<<'\n'; return; } vector <vector <int> > dist(k); vector <int> c(k); rep(i,k) cin>>c[i],c[i]--; for (int i=0;i<k;i++){ vector <int> d(n,INF); d[c[i]]=0; queue<int> q; q.push(c[i]); while (!q.empty()) { int f=q.front(); q.pop(); for (auto to:g[f]) { if (d[to]!=INF) continue; d[to]=d[f]+1; q.push(to); } } swap(dist[i],d); } vector <vi> dp((1<<k),vi(k,INF)); for (int i=0;i<k;i++) dp[(1<<i)][i]=1; for (int i=0;i<k-1;i++) { for (int mask=(1<<k)-1;mask>=0;mask--) { for (int was=0;was<k;was++) { if (dp[mask][was]==INF) continue; for (int j=0;j<k;j++) { dp[mask|(1<<j)][j]=min(dp[mask|(1<<j)][j],dp[mask][was]+dist[was][c[j]]); } } } } int ans=INF; for (int i=0;i<k;i++) ans=min(ans,dp[(1<<k)-1][i]); cout<<(ans==INF?-1:ans)<<'\n'; } int main() { #ifdef Mip182 freopen("a.in", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif int _t; _t=1; // cin>>_t; while (_t--) solve(); #ifdef Mip182 cout<<'\n'<<"Time : "<<(double)(clock())/CLOCKS_PER_SEC<<'\n'; #endif }
#include<bits/stdc++.h> using namespace std; #define pb emplace_back #define st first #define nd second typedef long long ll; typedef vector<int> vi; typedef pair<int,int> ii; typedef vector<ii> vii; typedef vector<vi> vvi; #define sz(a) int((a).size()) #define all(c) (c).begin(),(c).end() #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>b;i--) #define endl "\n" #define fre freopen("in.txt","r",stdin);freopen("out.txt","w",stdout) #define start ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define N 200005 //2 4 4 +1 // ll yen[20][1<<20]; queue<pair<int,int>> q; vector<int> v[N]; ll vis[N]; ll dp[20][20]; ll k; int idx[N]; vector<int> c; ll sum=0; bool check=true; void solve() { int n,m; cin>>n>>m; rep(i,0,m) { int a,b; cin>>a>>b; v[a].pb(b);v[b].pb(a); } cin>>k; memset(idx,-1,sizeof(idx)); rep(i,0,k) { int a; cin>>a; c.pb(a); idx[c[i]]=i; } rep(i,0,k) { memset(vis,0,sizeof(vis)); q.push(make_pair(c[i],0)); rep(j,0,k) { dp[i][j]=N; } while(!q.empty()) { ll temp=q.front().st; ll cur=q.front().nd; q.pop(); if(vis[temp]) continue; vis[temp]=1; if(idx[temp] != -1) dp[i][idx[temp]] = cur; rep(j,0,v[temp].size()) { if(vis[v[temp][j]]) continue; q.push({v[temp][j],cur+1}); } } } memset(yen, N, sizeof(yen)); for (int i=0; i<k; i++) yen[i][1<<i] = 0; for (int j=0; j<(1<<k); j++) { for (int i=0; i<k; i++) if (j & (1<<i)) { int cur = j & ~(1<<i); for (int l=0; l<k; l++) if (cur & (1<<l)) { yen[i][j] = min(yen[i][j], yen[l][cur] + dp[l][i]); } } } ll ans = N; for (int i=0; i<k; i++) ans = min(ans, yen[i][(1<<k)-1]); if (ans == N) cout<<-1<<endl; else cout<<ans+1<<endl; } int main(){ start int t=1; //cin>>t; rep(i,0,t) solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define sz(v) int((v).size()) #define all(x) (x).begin(), (x).end() #define forn(i, n) for (int i = 1; i <= int(n); ++i) //출처: https://codeforces.com/blog/entry/68809 void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << (#x) << "] = [", _print(x) #else #define debug(x...) #endif #define int ll const int MOD = 998244353; int power(int a, int n) { int res = 1; for (;n; n >>= 1, a = (a * a) % MOD) if (n & 1) res = (res * a) % MOD; return res; } void add(int& a, int b){a = (a%MOD + b%MOD)%MOD;} int h, w, k; char grid[5050][5050]; int dp[5050][5050][4]; int f(char c) { return c == 'R'? 2 : (c== 'D'); } void solve() { cin>>h>>w>>k; forn(_, k) { int a, b; char c; cin>>a>>b>>c; grid[a][b] = c; } int r = h*w - k; if (grid[1][1]) dp[1][1][f(grid[1][1])] = power(3, r); else dp[1][1][3] = power(3, r); int p3 = power(3, MOD - 2); forn(i, h) forn(j, w) { if (grid[i+1][j]) { add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('D')]); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('X')]); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3); add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3); } else { add(dp[i + 1][j][3], dp[i][j][f('D')]); add(dp[i + 1][j][3], dp[i][j][f('X')]); add(dp[i + 1][j][3], dp[i][j][3] * p3); add(dp[i + 1][j][3], dp[i][j][3] * p3); } if (grid[i][j+1]) { add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('R')]); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('X')]); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3); add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3); } else { add(dp[i][j + 1][3], dp[i][j][f('R')]); add(dp[i][j + 1][3], dp[i][j][f('X')]); add(dp[i][j + 1][3], dp[i][j][3] * p3); add(dp[i][j + 1][3], dp[i][j][3] * p3); } // for (int k = 0; k < 4; ++k) debug(i, j, k, dp[i][j][k]); } if (grid[h][w]) cout << dp[h][w][f(grid[h][w])] << '\n'; else cout << dp[h][w][3] << '\n'; } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; int main(){ ll n, k; cin >> n >> k; vvl t(n, vl(n)); rep(i,n)rep(j,n) cin >> t[i][j]; ll ans = 0; vl v(n-1); rep(i,n-1) v[i] = i+1; do{ ll sum = 0; rep(i,n){ if(i==0) sum += t[0][v[i]]; else if(i == n-1) sum += t[v[i-1]][0]; else sum += t[v[i-1]][v[i]]; if(sum == k) ans ++; } }while(next_permutation(v.begin(),v.end())); cout << ans << endl; }
#ifdef LC #include "pch.h" #else #include <bits/stdc++.h> #endif using namespace std; using ll = long long; #define int ll #define all(x) x.begin(), x.end() #define x first #define y second #define mp make_pair #define mt make_tuple signed main() { #ifdef LC assert(freopen("input.txt", "r", stdin)); #endif ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { cout << (2 * i) % n + 1 << " " << (2 * i + 1) % n + 1 << "\n"; } return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; #define DEBUG #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef pair<int,int> pii; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = (1<<30) - 1; const ll linf = 1LL<<61; const int MAX = 510000; int dy[8] = {0,1,0,-1,1,-1,-1,1}; int dx[8] = {-1,0,1,0,1,-1,1,-1}; const double pi = acos(-1); const double eps = 1e-7; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ int sz = a.size(); for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr; sz--; if(sz) cout << " "; } cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){ cout << a << " " << b << "\n"; } template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} //const int mod = 1e9 + 7; const int mod = 998244353; int main(){ int n; cin >> n; rep(i,n) cout << i/2+1 << " " << (i+n)/2+1 << "\n"; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define mt make_tuple #define eb emplace_back #define f first #define s second #define all(c) begin(c),end(c) using namespace std; using namespace __gnu_pbds; typedef long long int ll; typedef long double ld; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; const ll MAXN = 1e5; const ll INF = 1e17; const ll MOD = 1e9 + 7; const ll EPS = 1e-9; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll xs[] = {-1, 1, 0, 0, -1, 1, 1, -1}; ll ys[] = {0, 0, -1, 1, -1, 1, -1, 1}; ll ipow(ll b, ll p, ll mod){ if(p == 0) return 1; if(p == 1) return b%mod; if(p&1) return ((b%mod)*(ipow((b%mod * b%mod)%mod,p>>1,mod)))%mod; return ipow((b%mod * b%mod)%mod,p>>1,mod); } int main(){ cin.sync_with_stdio(false); cin.tie(0); ll a, b, c; cin >> a >> b >> c; a %= 10; if(a == 1 or a == 5 or a == 6 or a == 0) cout << a; else{ vector<ll> A[] = {{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}}; ll q = ipow(b,c,A[a].size()); cout << A[a][q]; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<long long> Vll; ll mm=1000000000;ll MM=mm+7; #define rep(i, n) for(int i=0;i<n;i++) #define PI 3.141592653589793 int main(){ ll a,b,c; cin >> a >> b >> c; ll x=a%10; if(x==0 || x==1|| x==5 || x==6 )cout << x << endl; else if(x==4){ if(b%2==0){ cout << 6 << endl; } else if(b%2==1){ cout << 4 << endl; } } else if(x==9){ if(b%2==0){ cout << 1 << endl; } else if(b%2==1){ cout << 9 << endl; } } else if(x==2){ if(b%4==0){ cout << 6 << endl; } else if(b%4==2){ if(c>=2)cout << 6 << endl; else cout << 4 << endl; } else{ if(b%4==1)cout << 2 << endl; else{ if(c%2==0)cout << 2 << endl; else if(c%2==1) cout << 8 << endl; } } } else if(x==3){ if(b%4==0){ cout << 1 << endl; } else if(b%4==2){ if(c>=2)cout << 1 << endl; else cout << 9 << endl; } else{ if(b%4==1)cout << 3 << endl; else{ if(c%2==0)cout << 3 << endl; else if(c%2==1)cout << 7 << endl; } } } else if(x==7){ if(b%4==0){ cout << 1 << endl; } else if(b%4==2){ if(c>=2)cout << 1 << endl; else cout << 9 << endl; } else{ if(b%4==1)cout << 7 << endl; else{ if(c%2==0)cout << 7 << endl; else if(c%2==1)cout << 3 << endl; } } } else if(x==8){ if(b%4==0){ cout << 6 << endl; } else if(b%4==2){ if(c>=2)cout << 6 << endl; else cout << 4 << endl; } else{ if(b%4==1)cout << 8 << endl; else{ if(c%2==0)cout << 8 << endl; else if(c%2==1)cout << 2 << endl; } } } }
// E - White Pawn #include <bits/stdc++.h> using namespace std; int main(){ int N, M; cin>>N>>M; int C = min(N, M), L = C*2 + 1; auto ixOk = [&](int y){ return 0 <= y && y < L; }; vector<pair<int,int>> D; while(M--){ int x, y; cin>>x>>y; y -= N - C; if(ixOk(y)) D.emplace_back(x, y); } sort(D.begin(), D.end()); vector<int> Y, S(L); S[C] = 1; auto update = [&](){ vector<int> A; for(int&y:Y) if((ixOk(y-1) && S[y-1]) || (ixOk(y+1) && S[y+1])) A.push_back(y); for(int&y:Y) S[y] = 0; for(int&y:A) S[y] = 1; }; int px = 0; for(auto&[x, y]:D){ if(px != x) update(), Y.clear(), px = x; Y.push_back(y); } update(); cout<< accumulate(S.begin(), S.end(), 0) <<endl; }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define F first #define S second int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, m; cin >> n >> m; map<ll, vector<ll>> mp; for(int i = 0; i < m; i++){ ll x, y; cin >> x >> y; mp[x].push_back(y); } set<ll> s; s.insert(n); for(auto &i: mp){ vector<ll> rem, ins; for(auto &j: i.S){ ll l = s.count(j + 1); ll r = s.count(j - 1); if(l + r == 0) rem.push_back(j); else ins.push_back(j); } for(auto &j: rem) s.erase(j); for(auto &j: ins) s.insert(j); } cout << s.size() << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int H,W; cin >> H >> W; int block_min=101; int block_grid=0; int block_mins=0; vector<int> A(W); for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin >> A[j]; int a=A[j]; if(a<block_min){ block_min=a; } } if(block_min<block_mins){ block_grid+=W*i*(block_mins-block_min); } for(int k=0;k<W;k++){ block_grid+=A[k]-block_min; } block_mins=block_min; } cout << block_grid << endl; }
#include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a, b; cin>>a>>b; int ans = 2*a+100; ans-=b; cout<<ans; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <sys/types.h> #include <unistd.h> #include <vector> #pragma region macros #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) _range(i, 0, n) #define _range(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) #define _rrep(i, n) _rrange(i, n, 0) #define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i) #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) #pragma endregion macros using namespace std; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } using ll = long long; using R = long double; const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7 inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); } inline R sq(R x) { return sqrt(max(x, 0.0L)); } const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const pid_t pid = getpid(); // Problem Specific Parameter: int main(void) { int t; cin >> t; rep(loop, t) { int n; cin >> n; string s1, s2, s3; cin >> s1 >> s2 >> s3; cout << string(n, '1') + string(n, '0') + "1" << endl; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using vi = vector<int>; using si = set<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define P pair<int,int> #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define rev(i,s,n) for(int i=(s);i>=(int)(n);i--) #define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i] #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) ll x;cin >> x #define cci(x,y) ll x,y;cin >> x >> y #define co(x) cout << x << endl #define pb push_back #define eb emplace_back #define rz resize #define sz(x) int(x.size()) #define yn cout<<"Yes"<<endl;else cout<<"No"<<endl #define YN cout<<"YES"<<endl;else cout<<"NO"<<endl template<class T>void chmax(T &x,T y){x=max(x,y);} template<class T>void chmin(T &x,T y){x=min(x,y);} int main(){ cii(t); V<ss> ans; while(t--){ cii(n); vvi v(2,vi(2,0)); V<ss> s(3); rep(i,0,3)ci(s[i]); rep(i,0,3){ int fr=s[i][0]-'0'; int ba=s[i][n*2-1]-'0'; v[ba][fr]=1; } ss now; ss s0,s1; rep(i,0,n){ s0+="0"; s1+="1"; } if(v[0][0]==0){ now=s0+"1"+s0; } else { if(v[1][1]==0){ now=s1+"0"+s1; } else { if(v[0][1]==0)now=s0+s1+"0"; else now=s1+s0+"1"; } } ans.pb(now); } for(ss s:ans)co(s); }
#include <bits/stdc++.h> using namespace std; #define FREP(i,a,b) for(long long i=a;i<b;i++) #define REP(i,n) FREP(i,0,n) #define PB push_back #define ll long long #define MP make_pair #define mod 1000000007 #define endl "\n" #define vi vector<ll> #define si set<ll> #define mi map<ll,ll> #define bs binary_search #define pri(a,n) REP(i,n)cout<<a[i]<<" "; #define all(x) x.begin(),x.end() #define sz(a) (ll)a.size() #define sa(a,n) sort(a,a+n) #define sra(a,n) sort(a,a+n,greater<ll>()) #define sv(v) sort(all(v)) #define srv(v) sort(v.rbegin(),v.rend()) #define sp(x) fixed<<setprecision(x) #define mem(a) memset(a,-1,sizeof(a)) #define ff first #define ss second #define deb(x) cout<<#x<<' '<<x<<endl; #define FASTIO ios_base::sync_with_stdio(false);\ cin.tie(NULL);\ cout.tie(NULL); void _a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z() { ll n, k, q = 0, c = 0; string s; cin >> n; FREP(i, 1, 10000001) if (((2 * n + i - i * i)) % (2 * i) == 0 && 2 * n + i - i * i > 0) ++c; cout << c * 2; } int main() { FASTIO; int t = 1; // cin >> t; // Test Case :-||-: // while (t--) { _a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z(); } return 0; } // Thanks for stalking :P
#include<bits/stdc++.h> using namespace std; using ll=long long; ll mod=1000000007; set<ll> enum_divisors(ll N){ set<ll> res; for(ll i=1;i*i<=N;i++){ if(N%i==0){ res.insert(i); res.insert(N/i); } } return res; } int main(){ ll N,count=0; cin >> N; const auto &P=enum_divisors(2*N); for(auto it=P.begin();it!=P.end();it++){ if(*it%2==1){ count+=2; } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; const int MAX = (int)(1e5 + 5); const ll INF = (ll)(1e10 + 5); const int MAX_N = (int)(2e2 + 5); int n; int a[MAX_N]; int dp[MAX_N][200]; vector<vector<int>> candidates; int get_valid_modulo() { if (dp[n][0] >= 3) return 0; for (int i = 1; i < 200; ++i) { if (dp[n][i] >= 2) return i; } return -1; } bool dfs(int cur, int modulo, vector<int> &hold) { if (cur == 0 && modulo == 0) { if (hold.empty()) return false; vector<int> tmp(hold); candidates.push_back(tmp); return candidates.size() >= 2; } if (cur == 0) return false; // do not use if (dp[cur - 1][modulo] != 0) { bool done = dfs(cur - 1, modulo, hold); if (done) return true; } // use hold.push_back(cur); int prev_modulo = (modulo - a[cur] + 200) % 200; if (dp[cur - 1][prev_modulo] != 0) { bool done = dfs(cur - 1, prev_modulo, hold); if (done) return true; } hold.pop_back(); return false; } void retrieve(int modulo) { vector<int> hold; dfs(n, modulo, hold); } int main(void) { // Here your code ! scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); a[i] %= 200; } // initialize for (int i = 0; i <= n; ++i) { for (int j = 0; j < 200; ++j) { dp[i][j] = 0; } } dp[0][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < 200; ++j) { if (dp[i - 1][j] == 0) continue; int next_modulo = (j + a[i]) % 200; dp[i][next_modulo] += dp[i - 1][j]; // use a[i] dp[i][j] += dp[i - 1][j]; // do not use a[i] dp[i][next_modulo] = min(dp[i][next_modulo], 3); dp[i][j] = min(dp[i][j], 3); } } int modulo = get_valid_modulo(); if (modulo < 0) { printf("No\n"); return 0; } printf("Yes\n"); retrieve(modulo); printf("%d ", candidates[0].size()); sort(candidates[0].begin(), candidates[0].end()); for (auto e : candidates[0]) { printf("%d ", e); } printf("\n"); printf("%d ", candidates[1].size()); sort(candidates[1].begin(), candidates[1].end()); for (auto e : candidates[1]) { printf("%d ", e); } printf("\n"); return 0; }
#include <iostream> #include <iomanip> #include <sstream> #include <vector> #include <string> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <functional> #include <iterator> #include <limits> #include <numeric> #include <utility> #include <type_traits> #include <cmath> #include <cassert> #include <cstdio> using namespace std; using namespace placeholders; using LL = long long; using ULL = unsigned long long; using VI = vector< int >; using VVI = vector< vector< int > >; using VS = vector< string >; using ISS = istringstream; using OSS = ostringstream; using PII = pair< int, int >; using VPII = vector< pair< int, int > >; template < typename T = int > using VT = vector< T >; template < typename T = int > using VVT = vector< vector< T > >; template < typename T = int > using LIM = numeric_limits< T >; template < typename T = int > using OSI = ostream_iterator< T >; template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; } template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; } void in_impl(){}; template < typename T, typename... TS > void in_impl( T &head, TS &... tail ){ cin >> head; in_impl( tail ... ); } #define IN( T, ... ) T __VA_ARGS__; in_impl( __VA_ARGS__ ); template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; } template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); } #define NUMBERED( name, number ) NUMBERED2( name, number ) #define NUMBERED2( name, number ) name ## _ ## number #define REP1( n ) REP2( NUMBERED( REP_COUNTER, __LINE__ ), n ) #define REP2( i, n ) REP3( i, 0, n ) #define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i ) #define GET_REP( a, b, c, F, ... ) F #define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2, REP1 )( __VA_ARGS__ ) #define FOR( e, c ) for ( auto &&e : c ) #define ALL( c ) begin( c ), end( c ) #define AALL( a ) ( remove_all_extents< decltype( a ) >::type * )a, ( remove_all_extents< decltype( a ) >::type * )a + sizeof( a ) / sizeof( remove_all_extents< decltype( a ) >::type ) #define SZ( v ) ( (int)( v ).size() ) #define EXISTS( c, e ) ( ( c ).find( e ) != ( c ).end() ) template < typename T > inline bool chmin( T &a, const T &b ){ if ( b < a ) { a = b; return true; } return false; } template < typename T > inline bool chmax( T &a, const T &b ){ if ( a < b ) { a = b; return true; } return false; } #define PB push_back #define EM emplace #define EB emplace_back #define BI back_inserter #define MP make_pair #define fst first #define snd second #define DUMP( x ) cerr << #x << " = " << ( x ) << endl // Λ Λ__ // /(*゚ー゚)/\ // /|  ̄U U ̄|\/ // | |/ int solve() { IN( int, N, S, K ); K %= N; const int b = ceil( sqrt( N ) ); map< int, int > exps; { int k = 0; REP( i, b + 2 ) { if ( EXISTS( exps, k ) ) { break; } exps[k] = i; ( k += K ) %= N; } } REP( i, b + 1 ) { const int s = ( S + LL( K ) * b * i ) % N; const int t = N - s; if ( EXISTS( exps, t ) ) { return i * b + exps[t]; } } return -1; } int main() { cin.tie( nullptr ); ios::sync_with_stdio( false ); cout << setprecision( 12 ) << fixed; IN( int, T ); REP( T ) { cout << solve() << '\n'; } cout << flush; return 0; }
#include <bits/stdc++.h> #include <fstream> #define vi vector<int> #define tests int t; cin>>t; while(t--) #define ll long long #define vll vector<long long> #define srt(v) sort(v.begin(), v.end()) #define srtg(v) sort(v.begin(), v.end(), greater<int> ()) #define FOR(k, n) for(int k=0; k<n; k++) #define pb push_back #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define printarr(v) for(auto& x : v) cout<<x<<" "; cout<<endl using namespace std; char nums[10] = { '0','1','2','3','4','5','6','7','8','9' }; char alphsl[26] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; const int MOD = 1000000007; char alphs[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' }; bool five(ll n){ ll curr=5; while(curr<n) curr*=5; if(curr==n) return true; return false; } void solve() { ll n; cin>>n; ll curr=3, cnt=1; while(curr<n){ if(five(n-curr)){ cout<<cnt<<" "; n-=curr; ll c=0; while(pow(5, c)<n) c++; cout<<c<<endl; return; } curr*=3; cnt++; } cout<<"-1"<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // tests solve(); return 0; }
#include <iostream> #include <math.h> using namespace std; int main() { ios_base::sync_with_stdio(false);cin.tie(0); long long N,i,j,arr1[38]={0},arr2[38]={0},M=0; cin >> N; arr1[0] = 1; arr2[0] = 1; arr1[1] = 3; arr2[1] = 5; for(i=2;i<38;i++){ arr1[i] = arr1[i-1]*3; arr2[i] = arr2[i-1]*5; } for(i=1;i<=38&&M<N;i++){ for(j=1;j<=25&&M<N;j++){ if(arr1[i]+arr2[j] == N){ cout << i <<" "<< j; return 0; } } } cout << -1 ; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; // int64_t using ull = unsigned long long; using pii = pair<int,int>; #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) const ll INF = 1e9+7; //10oku7 int N; vector<vector<ll>> dist; vector<vector<ll>> dp; ll binary(ll x){ ll ans = 0; ll count = 0; while ( x >0 ){ ans = ans + (x%2)*pow(10ll,count); x /= 2; count ++; } return ans; } ll rec(ll bits, ll j ){ if (dp.at(bits).at(j) < INF*2) return dp.at(bits).at(j); if (bits == 0 && j == 0) return 0; if (!((bits >> j ) & 1 )) { dp.at(bits).at(j) = INF; return INF; } rep(i,N){ if ( bits == 1<<i ){ dp.at(bits).at(i) = dist.at(0).at(j); return dp.at(bits).at(i); } } ll temp = INF; rep(i,N){ if (((bits >> i ) & 1) ){ // cout << binary(bits) << " " << i << endl; temp = min (temp, rec(bits - (1<<j), i) + dist.at(i).at(j)); } } dp.at(bits).at(j) = temp; return temp; } int main() { cin >> N; vector<ll> X(N); vector<ll> Y(N); vector<ll> Z(N); rep(i,N){ cin >> X.at(i) >> Y.at(i) >> Z.at(i); } dist = vector<vector<ll>> (N,vector<ll>(N,INF)); dp = vector<vector<ll>> ((1 << N) ,vector<ll>(N,INF*2)); rep(i,N){ rep (j,N){ dist.at(i).at(j) = abs (X.at(i) - X.at(j) ) + abs( Y.at(i) - Y.at(j)) + max (0ll, Z.at(j) - Z.at(i)); } } cout << rec ((1<<N) -1,0) << endl; }
#include "cstdio" #include "cstring" #include "cctype" #include "queue" #define R register #define getchar() (_==__&&(__=(_=___)+fread(___,1,1<<16,stdin),_==__)?EOF:*_++) char ___[1<<16],*_=___,*__=___; const int big=100010; int n; long long ans,a[big]; std::priority_queue<int>q[2]; inline int read() { char c(getchar());int x(0),f(1); for(;!isdigit(c);(c=='-')&&(f=-1),c=getchar()); for(;isdigit(c);x=(x*10)+(c^48),c=getchar()); return f*x; } int main(void) { // freopen("data.in","r",stdin); n=read(); for(R int i(1);i<=n;++i)a[i]=read(),ans+=a[i]; for(R int i(1);i<=n;++i)q[i&1].push(read()-a[i]); while(!q[0].empty()&&!q[1].empty()&&q[0].top()+q[1].top()>0)ans+=q[0].top()+q[1].top(),q[0].pop(),q[1].pop(); printf("%lld\n",ans); end:return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define all(v) v.begin(), v.end() using ll = long long; const int inf = (int)1e9; const ll inf_ll = (ll)1e19; const int mod = 1000000007; const int mod2 = 998244353; int main(){ char s, t; cin >> s >> t; if(s == 'Y') cout << char(t - 32) << endl; else cout << t << endl; }
/*@author Vipen Loka*/ #include <bits/stdc++.h> #define endl '\n' #define ff first #define ss second #define ll long long #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #define deb(x) cout << #x << ':' << x << '\n'; #define int long long using namespace std; template <class T, class K>ostream& operator<<(ostream &cout, pair<T, K> a) {cout << a.first << ' ' << a.second; return cout;} template<class T>void show(vector<T> &a) {cerr << "[ "; for (int ij = 0; ij < (int)a.size(); ij++) {cerr << a[ij] << " ";} cerr << "]\n";} template<class T>void show(T a) {cerr << a << endl;} template<typename... T>void show(T... args) {((cerr << args << ' '), ...); cerr << endl;} template<class T>void read(vector<T> &a) {for (auto &x : a) {cin >> x;}} template<class T> void read(T &a) {cin >> a;} void solve() { int a, b, c; cin >> a >> b >> c; char ans = '='; if (c % 2 == 0) { a = abs(a); b = abs(b); } if (a < b) { ans = '<'; } else if (a > b) { ans = '>'; } cout << ans << endl; } int32_t main(int32_t argc, char *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); int T = 1; // cin >> T; while (T--) { solve(); } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #include <map> #include <cmath> #include <queue> #include <cstdio> #include <limits> #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) using namespace std; template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; } template<class T> inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; const int inf = numeric_limits<int>::max(); const ll infll = numeric_limits<ll>::max(); int main() { int n,m,k; cin >> n >> m >> k; vi a(n+1, 0); rep(i,k) { int tmp; cin >> tmp; a[tmp] = 1; } vector<ld> x(n+1), y(n+1); vector<ld> sumx(n+1, 0), sumy(n+1, 0); int id = 0; for(int i = n-1; i >= 0; --i) { if(a[i]) { x[i] = 1; y[i] = 0; } else { x[i] = ld(1)/ld(m) * (sumx[id] - sumx[max(0, id-m)]); y[i] = ld(1)/ld(m) * (sumy[id] - sumy[max(0, id-m)]) + 1; } sumx[id+1] = sumx[id] + x[i]; sumy[id+1] = sumy[id] + y[i]; id++; } if(x[0] == 1) cout << -1 << "\n"; else { ld res = y[0] / (1. - x[0]); printf("%.4Lf\n", res); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { ll N, M, K, A; cin >> N >> M >> K; vector<bool> start(N + 1, false); rep(i, K) { cin >> A; start[A] = true; } double L = 0, R = 1e20; vector<double> dp(N + 1 + M, 0); rep(i, 100) { double m = (L + R) / 2, s = 0; rep(j, N) { if (start[N - 1 - j]) { dp[N - 1 - j] = m; } else { dp[N - 1 - j] = 1 + s / M; } s += dp[N - 1 - j]; if (j >= M - 1) s -= dp[N - 1 - j + M]; //cout << "s=" << s << " "; } if (dp[0] > m) L = m; else R = m; //rep(i, N + 1) cout << dp[i] << " "; cout << "\n"; //cout << L << " " << R << "\n"; } if (dp[0] > 1e15) cout << "-1\n"; else cout << fixed << setprecision(20) << dp[0] << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MAXX 3000002 ll pre[3][MAXX]; ll dp[3][MAXX]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll N,K; cin>>N>>K; ll MAX_SUM = 3LL*N; for(ll j=1;j<=MAX_SUM;++j) { if(j <= N) dp[0][j] = 1; pre[0][j] = pre[0][j - 1] + dp[0][j]; } for(ll i=1;i<3;++i) { for(ll j=1;j<=MAX_SUM;++j) { ll L = max(0LL, j - N); ll R = max(0LL, j - 1); dp[i][j] = pre[i - 1][R] - ((L > 0) ? pre[i - 1][L - 1] : 0); pre[i][j] = pre[i][j - 1] + dp[i][j]; } } ll sum = 0; for(ll i=1;i<=MAX_SUM;++i) { if(K <= dp[2][i]) { sum = i; break; } K -= dp[2][i]; } ll arr[3]; for(ll i=1;i<=N;++i) { //Checking beauty = i ll rem_sum = sum - i; if(!dp[1][rem_sum]) continue; if(K <= dp[1][rem_sum]) { sum -= i; arr[0] = i; break; } K -= dp[1][rem_sum]; } for(ll i=1;i<=N;++i) { //Checking taste = i ll rem_sum = sum - i; if(!dp[0][rem_sum]) continue; if(K <= dp[0][rem_sum]) { sum -= i; arr[1] = i; break; } K -= dp[0][rem_sum]; } assert(K <= N); arr[2] = sum; cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<endl; }
#include<bits/stdc++.h> typedef unsigned long long ull; typedef long long ll; #define rep(i,l,r) for(int i=l;i<=r;i++) #define nep(i,r,l) for(int i=r;i>=l;i--) void sc(int &x){scanf("%d",&x);} void sc(int &x,int &y){scanf("%d%d",&x,&y);} void sc(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);} void sc(ll &x){scanf("%lld",&x);} void sc(ll &x,ll &y){scanf("%lld%lld",&x,&y);} void sc(ll &x,ll &y,ll &z){scanf("%lld%lld%lld",&x,&y,&z);} void sc(char *x){scanf("%s",x);} void sc(char *x,char *y){scanf("%s%s",x,y);} void sc(char *x,char *y,char *z){scanf("%s%s%s",x,y,z);} void out(int x){printf("%d\n",x);} void out(ll x){printf("%lld\n",x);} void out(int x,int y){printf("%d %d\n",x,y);} void out(ll x,ll y){printf("%lld %lld\n",x,y);} void out(int x,int y,int z){printf("%d %d %d\n",x,y,z);} void out(ll x,ll y,ll z){printf("%lld %lld %lld\n",x,y,z);} using namespace std; const int N=3e6+5; ll n,k,s2[N]; ll cal1(ll sum) { if(sum<=2) return 0; ll l=max(1ll,sum-2*n),r=min(n,sum-2); l=sum-l;r=sum-r; swap(l,r); return s2[r]-s2[l-1]; } ll cal2(ll sum) { return min(n,sum-1)-max(1ll,sum-n)+1; } int main() { //freopen("1.in","r",stdin);freopen("1.out","w",stdout); sc(n,k); ll ans=0; for(int i=2;i<=n*2;i++) s2[i]=cal2(i)+s2[i-1]; for(ll i=3;i<=n*3;i++) { ll t=cal1(i); if(k>t) k-=t; else { for(ll j=max(1ll,i-2*n);j<=min(n,i-2);j++) { ll t=cal2(i-j); if(k>t) k-=t; else { for(ll h=max(1ll,i-j-n);h<=min(n,i-j-1);h++) { k--; if(k==0) { printf("%lld %lld %lld\n",j,h,i-j-h); break; } } break; } } break; } } }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; using lint = long long; int main(void) { string S; cin >> S; string T1 = ""; string T2 = ""; int t = 1; for(int i = 0; i < S.size(); i++){ if(S[i] != 'R'){ if(t == 1){ T1.push_back(S[i]); } else{ T2.push_back(S[i]); } int n_t1 = T1.size() - 1; int n_t2 = T2.size() - 1; if(T1.size() >= 2 && T1[n_t1] == S[i] && T1[n_t1-1] == S[i]){ T1.pop_back(); T1.pop_back(); } else if(T2.size() >= 2 && T2[n_t2] == S[i] && T2[n_t2-1] == S[i]){ T2.pop_back(); T2.pop_back(); } else if(T1.size() >= 1 && T2.size() >= 1 && T1[0] == S[i] && T2[0] == S[i]){ T1.erase(T1.begin()); T2.erase(T2.begin()); } //cout <<S[i] << " "<< T1 << " " << T2 << endl; } else{ t *= -1; } } if(t == 1){ reverse(T2.begin(), T2.end()); cout << T2 << T1 << endl; } else{ reverse(T1.begin(), T1.end()); cout << T1 << T2 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t = ""; cin >> s; int n = s.length(); bool flip = true; deque<char> deq; for (int i = 0; i < n; i++) { if (s[i] == 'R') { flip = !flip; } else { if (flip) { deq.emplace_front(s[i]); } else { deq.emplace_back(s[i]); } } } if (flip) reverse(deq.begin(), deq.end()); stack<char> st; for (auto c: deq) { if (st.size() and st.top() == c) { st.pop(); continue; } st.push(c); } stack<char> tmp; while (st.size()) { char c = st.top(); st.pop(); tmp.push(c); } while (tmp.size()) { char c = tmp.top(); tmp.pop(); cout << c; } cout << endl; }
#include <bits/stdc++.h> // #include "icld.cpp" using namespace std; using ll = long long int; using pii = pair<int,int>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; //#define pi 3.14159265358979 #define ss string #define db double #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(),v.end() #define ci(x) cin >> (x) #define cii(x) int (x);cin >> (x) #define cci(x,y) int (x),(y);cin >> (x) >>(y) #define co(x) cout << (x) << endl int t[200010]; int main(){ cii(n); vi v(n); rep(i,0,n)ci(v[i]); int at=0; rep(i,0,n){ t[v[i]]++; while(t[at])at++; co(at); } }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; #define rep(i,a,b) for(ll i=a;i<b;i++) #define nl cout<<endl #define pii pair<ll,ll> #define vi vector<ll> #define vii vector<pii> #define mi map<ll,ll> #define all(a) (a).begin(),(a).end() #define pb push_back #define ff first #define ss second #define hell 1000000007 #define test4(x,y,z,a) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<" a is "<<a<<endl; #define test3(x,y,z) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<endl; #define test2(x,y) cout<<"x is "<<x<<" y is "<<y<<endl; #define test1(x) cout<<"x is "<<x<<endl; #define N 300009 ll power(ll a,ll b,ll m) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n;cin>>n; stack<char> st; ll ans = n; string s;cin>>s; rep(i,0,n) { st.push(s[i]); while(1) { ll cnt = 0; if(!st.empty() and st.top() == 'x' ) { cnt ++; st.pop(); if(!st.empty() and st.top() == 'o' ) { cnt ++; st.pop(); if(!st.empty() and st.top() == 'f' ) { cnt ++; st.pop(); } } } //test2(i,cnt); if(cnt == 3) { ans -=3; } else { if(cnt == 1) { st.push('x'); } if(cnt == 2) { st.push('o'); st.push('x'); } break; } } } cout << ans << endl; }
#pragma GCC optimize("O3") //#pragma GCC target("avx") #include <bits/stdc++.h> using namespace std; #define re return #define pb push_back #define all(x) (x).begin(), (x).end() #define make_unique(x) sort(all(x)),x.resize(unique(all(x))-x.begin()) #define fi first #define se second #define ss second.second #define sf second.first #define ff first.first #define fs first.second #define sqrt(x) sqrt(abs(x)) #define mp make_pair #define PI 3.14159265358979323846 #define E 2.71828182845904523536 #define er erase #define in insert #define fo(i,n) for((i)=0;(i)<(n);(i)++) #define ro(i,n) for((i)=n-1;(i)>=0;(i)--) #define fr(i,j,n) for((i)=(j);(i)<(n);(i)++) #define rf(i,j,n) for((i)=((n)-1);(i)>=(j);(i)--) typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; void eras(map<int,int> &m,int x) { m[x]--; if (!m[x]) m.erase(x); } const int N=(int)3e5+100; const int M=(int)2e6+100; const int inf=(int)1e9+2; #define filename "" vector<int> v[N]; int h[N]; pair<int,int> o[N]; bool sss(pair<int,int> &x,pair<int,int> &y) { if (x.fi-x.se==y.fi-y.se) re x<y; re x.fi-x.se>y.fi-y.se; } void dfs(int x) { h[x]=1; vector<pair<int,int> > vv[2]; for(auto y:v[x]) { dfs(y); h[x]+=h[y]; vv[h[y]%2].pb(o[y]); } o[x]={0,0}; if (vv[1].size()%2) { for(auto &j:vv[0]) { o[x].fi+=max(j.fi,j.se); o[x].se+=min(j.fi,j.se); } } else { for(auto &j:vv[0]) { o[x].fi+=j.fi; o[x].se+=j.se; } } sort(all(vv[1]),sss); int i; fo(i,vv[1].size()) { if (i%2) { o[x].fi+=vv[1][i].se; o[x].se+=vv[1][i].fi; } else { o[x].fi+=vv[1][i].fi; o[x].se+=vv[1][i].se; } } o[x].se++; } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); //freopen(filename".in","r",stdin); //freopen(filename".out","w",stdout); //freopen("ans.txt","w",stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); srand(time(0)); ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int n,i; cin>>n; fo(i,n-1) { int a; cin>>a; a--; v[a].pb(i+1); } dfs(0); cout<<o[0].se; }
///Bismillahir Rahmanir Rahim #include<bits/stdc++.h> using namespace std; #define FAST ios::sync_with_stdio(0); cin.tie(0) #define int long long #define pb push_back #define pp pop_back #define mp make_pair #define pi 2*acos(0.0) #define scan(i) scanf("%lld",&i) #define scann(i,j) scanf("%lld%lld",&i,&j) #define print(i) printf("%lld\n",i) #define printt(i,j) printf("%lld %lld\n",i,j) #define loop(i,n) for(int i = 0 ; i <n ; i++) #define loopp(i,n) for(int i = 1 ; i <n ; i++) #define rep(i,n) for(int i=n-1;i>=0;i--) #define rev(v) reverse(v.begin(),v.end()) #define reset(a,b) memset(a,b,sizeof(a)) #define vect vector<int> #define vec_it vector<int>::reverse_iterator vi #define sorta(v) sort(v.begin(),v.end()) #define sortd(v) sort(v.begin(),v.end(),greater<>()) #define sorted(v) is_sorted(v.begin(),v.end()) #define mnv(v) *min_element(v.begin(),v.end()) #define mxv(v) *max_element(v.begin(),v.end()) #define countv(v,a) count(v.begin(),v.end(),a) #define vplusa(v,a) v.insert(v.end(),a.begin(),a.end()) #define mapp map<int,int> #define rmap map<int,int,greater<>>m #define pairr pair<int,int> #define sett set<int> #define rset set<int,greater<>>s #define set_it set<int>::reverse_iterator si #define map_it map<int,int> :: reverse_iterator mi #define digit(n) floor(log10(n)+1) #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define ff first #define ss second #define mx INT_MAX #define lmx LLONG_MAX #define mn INT_MIN #define lmn LLONG_MIN #define inf 0x3f3f3f3f #define inff 10e18 #define GCD __gcd(a,b) #define LCM (a/GCD)*b template <typename T> T gcd(T a,T b){return (b==0)?a:gcd(b,a%b);} template <typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));} int modulo (int a, int b) { return a >= 0 ? a % b : ( b - abs ( a%b ) ) % b; } void solve() { // freopen ("(input).txt" , "r", stdin) ; string st,stt; int low=0,high=0; int l,r; char ch; int n,k,t,x,y,z,d=0,cnt=0,temp=mx,sum=0,f=0,flag=0; cin>>n>>k ; vect v(n) ,a ,b ; sett s ; mapp m,mm ; pairr p ; for(int i=0;i<=n;i++) m[i]++; loop(i,n) cin>>v[i]; for(int i=0;i<n;i++) { mm[v[i]]++; m.erase(v[i]); cnt++; if(cnt==k) { cnt--; for(auto i:m) { temp=min(temp,i.ff); if(d==0) break; } mm[v[low]]--; if(mm[v[low]]==0) m[v[low]]++; low++; } } cout<<temp<<endl; } main() { int t; // cin>>t; // while(t--) solve(); }
#include<bits/stdc++.h> using namespace std; string s = "atcoder"; int main() { ios_base::sync_with_stdio(false); cin.tie(0); string a; int t,n,i,j; cin >> t; while(t--) { cin >> a; n = a.length(); if(a > s) cout << 0 << endl; else { for(i=0;i<n;i++) { if(a[i]!='a') break; } if(i==n) cout << -1 << endl; else if(a[i]>'t') cout << i-1 << endl; else cout << i << endl; } } }
#include <iostream> #include <vector> using namespace std; vector <long long> x; long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b % a, a); } int main() { long long primes[15] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 }; int n; cin >> n; long long temp; long long currmin = 9223372036854775806; for (int i = 0; i < n; i++) { cin >> temp; x.push_back(temp); } long long y; bool can; for (long long i = 1; i < (1 << 16); i++) { y = 1; can = true; long long t; long long s; for (int j = 0; j < 15; j++) { t = (long long)1 << j; s = i & t; if (s != 0) y *= primes[j]; } for (int j = 0; j < n; j++) { if (gcd(y, x[j]) == 1) { can = false; break; } } if (can) currmin = min(currmin, y); } cout << currmin << endl; return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using s = string; using P = pair<int, int>; template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const long long inf = 1LL << 60; class BQuizzes { public: void solve(std::istream& in, std::ostream& out) { ll n, x; s s; in >> n >> x >> s; for (int i = 0; i < n; ++i) { if(s[i] == 'x'){ if(x > 0) x--; }else{ x++; } } out << x; } }; int main() { BQuizzes solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <iostream> using namespace std; int main() { int a,sum=0,dex=3; while(dex--) { cin >> a; if(a==1) { a=6; } else if(a==2) { a=5; } else if(a==3) { a=4; } else if(a==4) { a=3; } else if(a==5) { a=2; } else if(a==6) { a=1; } sum+=a; } cout << sum << endl; return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <cmath> #include <tuple> #include <cstdio> #include <bitset> #include <sstream> #include <iterator> #include <numeric> #include <map> #include <cstring> #include <set> #include <functional> #include <iomanip> #include <cassert> using namespace std; #define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!! #ifdef DEBUG_ #define dump(x) cerr << #x << " = " << (x) << endl; #else #define dump(x) ; #endif #define equals(a,b) (fabs((a)-(b)) < EPS) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back //#define int long long typedef long long LL; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template <typename T> std::string printVector(const std::vector<T> &data) { std::stringstream ss; std::ostream_iterator<T> out_it(ss, ", "); ss << "["; std::copy(data.begin(), data.end() - 1, out_it); ss << data.back() << "]"; return ss.str(); } template <typename T> void print_array(const T &ary, int size){ REP(i,size){ cout << ary[i] << " "; } cout << endl; } const int mod = 1e9+7; //const int mod = 998244353; const LL LINF = 1001002003004005006ll; const int INF = 1001001001; const double EPS = (1e-10); const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899; int dx[] = {0,0,-1,1}; int dy[] = {-1,1,0,0}; int N; VVI g; VI d; VVI Qv; //頂点iのqidxが格納 vector<PII> qlist; //クエスチョンが連番 VI ans; void dfs1(int v, int p, int dep){ d[v] = dep; for(int u : g[v]){ if(u == p) continue; dfs1(u,v,dep+1); } } map<int,int> dfs2(int v, int p){ map<int,int> res; res[d[v]]++; for(int u : g[v]){ if(u == p) continue; auto qmap = dfs2(u,v); if(res.size() < qmap.size()){ swap(res, qmap); } for(auto p : qmap){ res[p.first] += p.second; } } int sz = SZ(Qv[v]); REP(i,sz){ //この時点でクエリに答える int qidx = Qv[v][i]; int x = qlist[qidx].second; if(res.find(x) == res.end()){ ans[qidx] = 0; }else{ ans[qidx] = res[x]; } } return res; } signed main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12); cin >> N; g.resize(N); d.resize(N); Qv.resize(N); FOR(i,1,N){ int p; cin >> p; p--; g[i].eb(p); g[p].eb(i); } int Q; cin >> Q; qlist.resize(Q); ans.resize(Q); REP(i,Q){ int u,d; cin >> u >> d; u--; qlist[i] = make_pair(u,d); Qv[u].eb(i); } dfs1(0,-1,0); dfs2(0,-1); REP(i,Q){ cout << ans[i] << "\n"; } }
// E - Count Descendants #include <bits/stdc++.h> using namespace std; #define vec vector using vi = vec<int>; vec<vi> adj, T; vi L, R; int dfs(int d, int p, int t){ T[d].push_back(t); L[p] = t; for(int&c:adj[p]) t = dfs(d+1, c, t+1); R[p] = t+1; return t+1; } int f(int d, int t){ return lower_bound(T[d].begin(), T[d].end(), t) - T[d].begin(); } int main(){ int n; cin>>n; adj.resize(n); for(int i=0; i<n-1; ++i){ int p; cin>>p; adj[p-1].push_back(i+1); } T.resize(n), L.resize(n), R.resize(n); dfs(0, 0, 0); int q; cin>>q; while(q--){ int u, d; cin>>u>>d; cout<< f(d, R[u-1]) - f(d, L[u-1]) <<endl; } }
/* #region header */ #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; // types using ll = long long; using ull = unsigned long long; using ld = long double; typedef pair<ll, ll> Pl; typedef pair<int, int> Pi; typedef vector<ll> vl; typedef vector<int> vi; typedef vector<char> vc; template <typename T> using mat = vector<vector<T>>; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvl; typedef vector<vector<char>> vvc; // abreviations #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep_(i, a_, b_, a, b, ...) for (ll i = (a), max_i = (b); i < max_i; i++) #define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define rrep_(i, a_, b_, a, b, ...) \ for (ll i = (b - 1), min_i = (a); i >= min_i; i--) #define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define srep(i, a, b, c) for (ll i = (a), max_i = (b); i < max_i; i += c) #define SZ(x) ((int)(x).size()) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define mp make_pair //入出力 #define print(x) cout << x << endl template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto& e : v) cout << e << " "; cout << endl; return os; } void scan(int& a) { cin >> a; } void scan(long long& a) { cin >> a; } void scan(char& a) { cin >> a; } void scan(double& a) { cin >> a; } void scan(string& a) { cin >> a; } template <class T> void scan(vector<T>& a) { for (auto& i : a) scan(i); } #define vsum(x) accumulate(all(x), 0LL) #define vmax(a) *max_element(all(a)) #define vmin(a) *min_element(all(a)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) // functions // gcd(0, x) fails. ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> T mypow(T x, ll n) { T ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; } return ret; } ll modpow(ll x, ll n, const ll mod) { ll ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; x %= mod; ret %= mod; } return ret; } uint64_t my_rand(void) { static uint64_t x = 88172645463325252ULL; x = x ^ (x << 13); x = x ^ (x >> 7); return x = x ^ (x << 17); } int popcnt(ull x) { return __builtin_popcountll(x); } template <typename T> vector<int> IOTA(vector<T> a) { int n = a.size(); vector<int> id(n); iota(all(id), 0); sort(all(id), [&](int i, int j) { return a[i] < a[j]; }); return id; } struct Timer { clock_t start_time; void start() { start_time = clock(); } int lap() { // return x ms. return (clock() - start_time) * 1000 / CLOCKS_PER_SEC; } }; /* #endregion*/ // constant #define inf 1000000000ll #define INF 4000000004000000000LL #define mod 998244353ll #define endl '\n' const long double eps = 0.000000000000001; const long double PI = 3.141592653589793; // give the solution which minimize |x|+|y| template <typename T> T extgcd(T a, T b, T& x, T& y) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; // gcd(a, b) } void solve() { ll n, s, k; cin >> n >> s >> k; ll x, y; ll d = extgcd(k, n, x, y); if (s % d != 0) { print(-1); return; } x *= s / d; x *= -1; x = (x % (n / d) + n / d) % (n / d); print(x); } int main() { cin.tie(0); ios::sync_with_stdio(0); cout << setprecision(30) << fixed; int t; cin >> t; while (t--) { solve(); } }
#include <iostream> #include <cstdint> using namespace std; int main() { int N, K, M; cin >> N >> K >> M; int64_t sum = 0; for (size_t i = 1; i < N; ++i) { int n; cin >> n; sum += n; } int64_t diff = (M * N) - sum; if (diff > K) { cout << -1 << endl; } else { cout << max(diff, static_cast<int64_t>(0)) << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } int main() { ll n; cin>>n; ll ans=1; for(int i=2;i<=n;i++) ans=lcm(ans,i); cout<<ans+1<<endl; }
#include <bits/stdc++.h> using namespace std; #define forn(i,n) for(int i=0;i<(int)(n);i++) #define si(c) ((int)(c).size()) #define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++) #define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--) #define all(c) (c).begin(), (c).end() #define D(a) cerr << #a << "=" << (a) << endl; #define pb push_back #define eb emplace_back #define mp make_pair typedef long long int ll; typedef vector<int> vi; typedef pair<int,int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll gcd(ll a, ll b) { return a ? gcd(b%a,a) : b; } ll lcm(ll a, ll b) { return a / gcd(a,b) * b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; ll l = 1; for (int x = 2; x <= n; x++) l = lcm(l, x); auto ans = l+1; for (int x = 2; x <= n; x++) assert(ans%x == 1); cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll,ll>pll; const int mod = 1e9 + 7; pair<int,int> find(vector<vector<int>>& grid){ for(int i = 0; i < grid.size(); i++){ for(int j = 0; j < grid[0].size(); j++){ if(grid[i][j] == 0){ return mp(i, j); } } } return mp(-1,-1); } void dfs(int& ans, vector<vector<int>>& grid, int a, int b){ int n = grid.size(); int m = grid[0].size(); pair<int,int>start = find(grid); if(start.first == -1 && start.second == -1){ ans++; return; } int i = start.first; int j = start.second; if(a > 0){ // use 2x1 // I // I if(i+1 < n && grid[i+1][j] == 0){ grid[i][j] = 1; grid[i+1][j] = 1; dfs(ans, grid, a-1, b); grid[i][j] = 0; grid[i+1][j] = 0; } if(j+1 < m && grid[i][j+1] == 0){ grid[i][j] = 1; grid[i][j+1] = 1; dfs(ans, grid, a-1, b); grid[i][j] = 0; grid[i][j+1] = 0; } } if(b > 0){ grid[i][j] = 1; dfs(ans, grid, a, b-1); grid[i][j] = 0; } } int main(){ int h, w, a, b; cin >> h >> w >> a >> b; vector<vector<int>>grid(h, vector<int>(w, 0)); int ans = 0; dfs(ans, grid, a, b); cout << ans << endl; }
#include <sstream> #include <iostream> #include <string> #include <vector> #include <deque> #include <numeric> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <list> #include <cassert> #include <cmath> #include <climits> #include <map> #include <queue> #include <functional> #include <cassert> using namespace std; using ll = int64_t; #define rep(i, a) for (int i = 0; i < (int)(a); ++i) #define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define repr(i, a) for (int i = (int)((a) - 1); i >= 0; --i) template <class T> class V1 : public vector<T> { public: V1(size_t n1, T init = T()) : vector<T>(n1, init) {} }; template <class T> class V2 : public vector<V1<T>> { public: V2(size_t n1, size_t n2, T init = T()) : vector<V1<T>>(n1, V1<T>(n2, init)) {} }; template <class T> class V3 : public vector<V2<T>> { public: V3(size_t n1, size_t n2, size_t n3, T init = T()) : vector<V2<T>>(n1, V2<T>(n2, n3, init)) {} }; template <class T> class V4 : public vector<V3<T>> { public: V4(size_t n1, size_t n2, size_t n3, size_t n4, T init = T()) : vector<V3<T>>(n1, V3<T>(n2, n3, n4, init)) {} }; template <class T> class V5 : public vector<V4<T>> { public: V5(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, T init = T()) : vector<V4<T>>(n1, V4<T>(n2, n3, n4, n5, init)) {} }; template <class T> vector<T> GetV1(istream& in, size_t N) { vector<T> ret; ret.reserve(N); rep(i, N) { T a; in >> a; ret.emplace_back(a); } return ret; } ll Calc(vector<pair<int, int>> items, vector<int> xs) { sort(xs.begin(), xs.end()); sort(items.begin(), items.end()); ll sum = 0; for (int x : xs) { int vmax = 0; int imax = -1; rep(i, items.size()) { if (items[i].first > x) break; if (vmax < items[i].second) { vmax = items[i].second; imax = i; } } if (imax >= 0) { sum += vmax; items[imax].second = 0; } } return sum; } int main() { #if 0 stringstream sstr; sstr << R"()"; auto& in = sstr; #else auto& in = cin; #endif int N, M, Q; in >> N >> M >> Q; vector<pair<int, int>> items(N); // w, v rep(i, N) { in >> items[i].first >> items[i].second; } vector<int> xs(M); rep(i, M) { in >> xs[i]; } rep(q, Q) { int l, r; in >> l >> r; ll vsum = 0; auto cxs = xs; for (int i = l - 1; i <= r - 1; ++i) { cxs[i] = 0; } cout << Calc(items, cxs) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; bool reversed = false; deque<char> T; for (auto c : S) { if (c == 'R') { reversed = !reversed; continue; } if (reversed) { if (!T.empty() && T.front() == c) { T.pop_front(); } else { T.push_front(c); } } else { if (!T.empty() && T.back() == c) { T.pop_back(); } else { T.push_back(c); } } } if (reversed) { reverse(T.begin(), T.end()); } while (!T.empty()) { cout << T.front(); T.pop_front(); } cout << endl; }
// Problem: D - Message from Aliens // Contest: AtCoder - ZONe Energy Programming Contest // URL: https://atcoder.jp/contests/zone2021/tasks/zone2021_d // Memory Limit: 1024 MB // Time Limit: 2000 ms #include <bits/stdc++.h> using namespace std; #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define ll long long #define int long long #define ld long double #define endl "\n" #define pb push_back #define fill(a,val) memset(a,val,sizeof(a)) #define ff first #define ss second #define test ll t; cin>>t; while(t--) #define loop(i,a,b) for(ll i=a;i<b;i++) #define loopr(i,a,b) for(ll i=a;i>=b;i--) #define pii pair<ll,ll> #define all(v) v.begin(),v.end() const ll mod = 1000*1000*1000+7; const ll inf = 1ll*1000*1000*1000*1000*1000*1000 + 7; const ll mod2 = 998244353; const ll N = 1000 * 1000 + 10; const ld pi = 3.141592653589793; ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;} ll ncr(ll n,ll r,ll m){if(r>n)return 0;ll a=1,b=1,i;for(i=0;i<r;i++){a=(a*n)%m;--n;}while(r){b=(b*r)%m;--r;}return (a*power(b,m-2,m))%m;} void solve(){ string s; cin>>s; vector<char> ans; for(ll i=0;i<s.length();i++){ if(s[i]!='R'){ if(ans.size() > 0 && ans[ans.size() - 1]==s[i]){ ans.pop_back(); } else{ ans.pb(s[i]); } } else{ reverse(all(ans)); } } for(auto i:ans) cout<<i; cout<<endl; } signed main(){ fastio(); // test solve(); return 0; }
#include<bits/stdc++.h> #define ll long long using namespace std; int read() { char c; int w=1; while((c=getchar())>'9'||c<'0')if(c=='-')w=-1; int ans=c-'0'; while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0'; return ans*w; } int n,m; struct nod { int next,to; }e[1000005]; int cnt; int h[1000005]; void add(int x,int y) { cnt++; e[cnt].next=h[x]; h[x]=cnt; e[cnt].to=y; } ll dis[18][100005]; int vis[18][100005]; struct node { ll x,dis; bool operator<(const node&w)const { return dis>w.dis; } node(){} node(int a,int b){x=a,dis=b;} }; priority_queue<node>q; int k; int id[25]; void dij(int x) { while(!q.empty())q.pop(); dis[x][id[x]]=0; q.push(node(id[x],0)); while(!q.empty()) { // cout<<q.size()<<endl; int f=q.top().x; q.pop(); if(vis[x][f])continue; vis[x][f]=1; // cout<<f<<endl; for(int i=h[f];i;i=e[i].next) { if(dis[x][e[i].to]>dis[x][f]+1) { dis[x][e[i].to]=dis[x][f]+1; if(!vis[x][e[i].to])q.push(node(e[i].to,dis[x][f]+1)); } } } } ll s[25][25]; ll f[131078][18];//131072 signed main(){ memset(dis,0x3f,sizeof(dis)); n=read(); m=read(); for(int i=1;i<=m;i++) { int a,b; a=read(); b=read(); add(a,b); add(b,a); } k=read(); for(int i=1;i<=k;i++)id[i]=read(); for(int i=1;i<=k;i++)dij(i); for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) s[i][j]=dis[i][id[j]]; memset(f,0x3f,sizeof(f)); for(int i=1;i<=k;i++)f[(1<<i-1)][i]=0; for(int i=0;i<(1<<k);i++) { for(int j=1;j<=k;j++) { if(f[i][j]>1e9)continue; for(int t=1;t<=k;t++) { if(i&(1<<t-1))continue; f[i+(1<<t-1)][t]=min(f[i+(1<<t-1)][t],f[i][j]+s[j][t]); } } } ll ans=1e18; for(int j=1;j<=k;j++) { ans=min(ans,f[(1<<k)-1][j]); } if(ans>1e9) { puts("-1"); } else cout<<ans+1<<endl; return 0; }
#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 = (int)(a); i <= (int)(b); ++i) #define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i) #define ALL(c) (c).begin(), (c).end() using ll = long long; using VI = vector<int>; using VL = vector<ll>; using VD = vector<double>; using VII = vector<VI>; using VLL = vector<VL>; using VDD = vector<VD>; using P = pair<int, int>; using PL = pair<ll, ll>; template <typename T> bool chmax(T& a, const T& b) { if(a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T& a, const T& b) { if(a > b) { a = b; return true; } return false; } void solve(std::vector<std::string> S) { set<char> moji_set; vector<char> moji; REP(i, 3) { REP(j, S[i].size()) { if(moji_set.count(S[i][j]) > 0) continue; moji.push_back(S[i][j]); moji_set.insert(S[i][j]); } } sort(moji.begin(), moji.end()); if(moji.size() > 10) { cout << "UNSOLVABLE" << endl; return; } if(moji.size() < 10) { char last = moji[moji.size() - 1]; while(moji.size() != 10) { moji.push_back(last + 1); last++; } } do { map<char, int> moji_map; REP(i, moji.size()) { moji_map[moji[i]] = i; } VLL N_dash(3); bool num_ok = true; REP(i, 3) { REP(j, S[i].size()) { char tmp = S[i][j]; if(j == 0 && moji_map[tmp] == 0) num_ok = false; N_dash[i].push_back(moji_map[tmp]); } } if(!num_ok) continue; VL N(3); REP(i, 3) { ll mul = 1; N[i] = 0; reverse(N_dash[i].begin(), N_dash[i].end()); REP(j, N_dash[i].size()) { N[i] += N_dash[i][j] * mul; mul *= 10; } } if(N[0] + N[1] == N[2]) { REP(i, 3) { cout << N[i] << endl; } return; } } while(next_permutation(moji.begin(), moji.end())); cout << "UNSOLVABLE" << endl; return; } int main() { std::vector<std::string> S(3); for(int i = 0; i < 3; i++) { std::cin >> S[i]; } solve(std::move(S)); return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define p push #define pp pop #define B begin() #define E end() #define rB rbegin() #define rE rend() #define Si size() #define C count() #define yes cout<<"Yes\n" #define no cout<<"No\n" #define mp make_pair() typedef long long ll; #define V(type) vector<type> #define PQ(type) priority_queue<type> #define pll pair<ll, ll> #define F first #define S second #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;i>0;i--) #define mod 1000000007 #define INF 9223372036854775807 const double delta = 0.0000000001; const double pi = 3.1415926535; void FAST() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll fact(ll n) { if(n<2)return 1; else return n*fact(n-1); } ll xi(ll x,ll y,ll p){ //gives (x^y)%p ll r=1; x=x%p; while(y>0){ if(y%2==0){ x*=x; x=x%p; y/=2; } else { r=((r%p)*(x%p))%p; y--; } } return r; } ll countBits(ll n) { ll count = 0; while (n) { count++; n >>= 1; } return count; } ll isP(ll n) { //returns 1 if prime if (n == 0 || n == 1) return 0; for (ll i = 2; i <= sqrt(n); i++) { if (n % i == 0) return 0; } return 1; } int main() { ll n; cin>>n; V(ll) arr(n); rep(i,n) {cin>>arr[i];} ll ans=0; sort(arr.B,arr.E); for(ll i=n-1;i>=0;i--) { ans+=((arr[i]*i)-(arr[i]*(n-i-1))); } cout<<ans; return 0; }
/* Auth: Loxilante Date: 2021-02-13 20:18:37 Prog: B.cpp */ #define F_C #include <bits/extc++.h> #define rep(i, l, r) for(int i = l; i < r; i++) #define hrp(i, l, r) for(int i = l; i <= r; i++) #define rev(i, r, l) for(int i = r; i >= l; i--) #define ms(n, t) memset(n, t, sizeof(n)) #define pb push_back #define int ll #ifndef JOEON #define D(...) 97 #endif using namespace std; typedef long long ll; typedef pair<int, int> pii; template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; } signed main(void) { #ifdef JOEON // freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin); // freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); int k, m, ans = 0; cin>>k>>m; if (!m) return cout<<1<<endl, 0; if (m == 1) return cout<<1+(k != 0)<<endl, 0; if (m == 2) return cout<<2+(k != 0)<<endl, 0; if (k > 0) { int minn = -k-(m-1)/2; int maxx = k+(m-2)/2; int pmin = max(0LL, k-m/2); int pmax = min(-1LL, -k+(m-1)/2); D(minn, maxx, pmin, pmax, pmax); ans = -k-minn+1+maxx-pmin+1+pmax+k; } else if (!k) ans = m; else { int minn = k-m/2; int maxx = -(k-(m-1)/2); int pmin = max(0LL, -k-(m-1)/2); int pmin2 = min(-1LL, k+(m-2)/2); ans = k-minn+1+maxx-pmin+1+pmin2-k; } cout<<ans<<endl; return 0; } /* */
#include<bits/stdc++.h> #define il inline #define db double const int N=105; int n,tot; char g[N][N]; bool ok[N]; db ans; il void dfs(int u) { ok[u]=1,tot++; int v; for (v=1; v<=n; v++) if (!ok[v]&&g[v][u]>48) dfs(v); } int main() { scanf("%d",&n); int i; for (i=1; i<=n; i++) scanf("%s",g[i]+1); for (i=1; i<=n; i++) memset(ok,0,n+1),tot=0,dfs(i),ans+=1.0/tot; printf("%.12lf\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin >> n; int a[n]; map<int,int> m; for(int i=0;i<n;i++) { cin >> a[i]; m[a[i]]++; } long long ans=0; for(int i=0;i<n;i++) { ans+=(n-i-1)-(m[a[i]]-1); m[a[i]]--; } cout << ans; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;} template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;} int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0}; long double eps = 1e-9; long double pi = acos(-1); signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int n; cin>>n; string s; cin>>s; int m = n+1; int a[m]; int k = 1e9; for(int i=0;i<m;i++)cin>>a[i]; for(int i=1;i<m;i++)chmin(k,abs(a[i]-a[i-1])); cout << k << endl; vector<vector<int>> ans(k); for(int i=0;i<k;i++){ for(int j=0;j<m;j++){ ans[i].push_back(a[j]/k); } } int rem[m]={}; for(int i=0;i<m;i++)rem[i] = a[i] % k; for(int i=0;i<k;i++){ for(int j=0;j<m;j++){ if(rem[j]){ ans[i][j]+=1; rem[j]--; } cout << ans[i][j] << " "; } cout << endl; } }
#define LOCAL #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++) #define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--) #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define All(x) x.begin(), x.end() #define Range(x, i, j) x.begin() + i, x.begin() + j // #define M_PI 3.14159265358979323846 // CF #define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI)) #define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360) #define Find(set, element) set.find(element) != set.end() #define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank #define endl "\n" #define Case(x) printf("Case #%d: ", x); // gcj typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; typedef vector<PI> vpi; typedef vector<vector<PI>> vvpi; typedef vector<PLL> vpl; typedef vector<vector<PLL>> vvpl; typedef vector<char> vch; typedef vector<vector<char>> vvch; constexpr ll INF = 1001002003004005006ll; constexpr int n_max = 2e5+10; template<class T> inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; }; template<class T> inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; }; template<class T, class U> T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;}; // debug template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) {return '"' + s + '"';}; string to_string(const char c) {return to_string((string) &c);}; string to_string(bool b) {return (b ? "true" : "false");}; template <size_t N> string to_string(bitset<N> v){ string res = ""; for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; }; template <typename A> string to_string(A v) { bool first = true; string res = "{"; for(const auto &x : v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; }; template <typename A, typename B> string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";} void debug_out() {cerr << endl;}; template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }; void LINE_OUT() { cout << "--------------" << endl; }; #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define LINE LINE_OUT(); #else #define debug(...) 71 #define LINE 71; #endif void print() { cout << endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }; template <class T> void print(vector<T> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }; template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec); } }; vvi f(vi &A, string &s, int k) { int N = s.size(); vvi ans(k, vi(N+1)); rep(i,0,N+1) { int a = A[i]; int q = a/k; int re = a%k; rep(j,0,k) { ans[j][i] += q; } rep(j,0,k) { if (re) { ans[j][i] += 1; re--; } else break; } } return ans; }; signed main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; string s; cin >> s; vi A(N+1); rep(i,0,N+1) cin >> A[i]; int k = INF; rep(i,0,N) chmin(k, abs(A[i]-A[i+1])); vvi ans = f(A, s, k); cout << ans.size() << endl; for (vi &a: ans) { rep(i,0,N+1) { cout << a[i]; if (i < N) cout << " "; } cout << endl; } return 0; };
#include <bits/stdc++.h> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("tpl.txt","r",stdin); #endif int n; cin >> n; vector<int> vc; for(int i = 0; i < 1 << n; i++) { int tmp; cin >> tmp; vc.push_back(tmp); } int max_pos = max_element(vc.begin(),vc.end()) - vc.begin(); int half = 1 << (n-1); auto start = max_pos < half ? vc.begin() + half : vc.begin(); int ans = max_element(start,start + half) - vc.begin(); cout << ans + 1<< endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef pair <int, int> pin; const int N = 2e5 + 5; int n, ans, top = 0, stk[N]; char s[N]; template <typename T> inline void read(T &X) { char ch = 0; T op = 1; for (X = 0; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') op = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) X = (X * 10) + ch - '0'; X *= op; } int main() { #ifndef ONLINE_JUDGE freopen("sample.in", "r", stdin); #endif read(n); scanf("%s", s + 1); ans = n; for (int i = 1; i <= n; i++) { if (s[i] == 'f') { stk[++top] = i; } else if (s[i] == 'o') { if (s[stk[top]] == 'o') top = 0; else stk[++top] = i; } else if (s[i] == 'x') { if (top < 2) top = 0; else { if (s[stk[top - 1]] = 'f' && s[stk[top]] == 'o') { top -= 2; ans -= 3; } else top = 0; } } else top = 0; } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1001001001001001001; const int MOD = 1000000007; int main(){ int n;cin>>n; vector<ll> r; vector<ll> g; vector<ll> b; for(int i=0;i<2*n;i++) { ll a;char c;cin>>a>>c; if(c=='R') r.push_back(a); else if(c=='G') g.push_back(a); else if(c=='B') b.push_back(a); } sort(r.begin(),r.end()); sort(g.begin(),g.end()); sort(b.begin(),b.end()); if(r.size()%2==0 && g.size()%2==0 && b.size()%2==0) cout << 0 << endl; else { if(r.size()%2==0) swap(r,b); else if(g.size()%2==0) swap(g,b); ll mi = INF; ll rbmi = INF; ll gbmi = INF; int rsize = r.size(); int gsize = g.size(); int bsize = b.size(); if(bsize!=0) { for(int i=0;i<rsize;i++) { int idx = lower_bound(b.begin(),b.end(),r.at(i)) - b.begin(); if(idx!=0 && idx!=bsize) { ll temp = min(abs(r.at(i)-b.at(idx)), abs(r.at(i)-b.at(idx-1))); rbmi = min(rbmi,temp); } else if(idx==bsize) { ll temp = abs(r.at(i)-b.at(idx-1)); rbmi = min(rbmi,temp); } else { ll temp = abs(r.at(i)-b.at(idx)); rbmi = min(rbmi,temp); } } for(int i=0;i<gsize;i++) { int idx = lower_bound(b.begin(),b.end(),g.at(i)) - b.begin(); if(idx!=0 && idx!=bsize) { ll temp = min(abs(g.at(i)-b.at(idx)), abs(g.at(i)-b.at(idx-1))); gbmi = min(gbmi,temp); } else if(idx==bsize) { ll temp = abs(g.at(i)-b.at(idx-1)); gbmi = min(gbmi,temp); } else { ll temp = abs(g.at(i)-b.at(idx)); gbmi = min(gbmi,temp); } } mi = min(mi,rbmi + gbmi); } for(int i=0;i<rsize;i++) { int idx = lower_bound(g.begin(),g.end(),r.at(i)) - g.begin(); ll mi2; if(idx!=0 && idx!=gsize) mi2 = min(abs(r.at(i)-g.at(idx)), abs(r.at(i)-g.at(idx-1))); else if(idx==gsize) mi2 = abs(r.at(i)-g.at(idx-1)); else mi2 = abs(r.at(i)-g.at(idx)); mi = min(mi,mi2); } cout << mi << endl; } }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define int long long int #define popcount(x) __builtin_popcountll(x) #define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define sc second #define pb push_back #define fr first #define all(a) (a).begin(),(a).end() #define mem0(a) memset(a,0,sizeof(a)) #define ld long double // const int INF = __LONG_LONG_MAX__; #define rall(a) (a).rbegin(),(a).rend() #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroint-loops") int power(int b,int e,int m) { if(b==0) return 0; if(e==0) return 1; if(e&1) return b*power(b*b%m,e/2,m)%m; return power(b*b%m,e/2,m); } int power( int b, int e) { if(b==0) return 0; if(e==0) return 1; if(e&1) return b*power(b*b,e/2); return power(b*b,e/2); } bool isPowerOfTwo(int x) { // x wiint check if x == 0 and !(x & (x - 1)) wiint check if x is a power of 2 or not return (x && !(x & (x - 1))); } int collinear(int x1, int y1, int x2, int y2, int x3, int y3) { // Calculation the area of // triangle. We have skipped // multiplication with 0.5 // to avoid floating point // computations int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if (a == 0) return 1; else return 0; } void solve() { int n; cin>>n; vector <pair<int,int> > v; for(int i=0;i<n;i++) { int a,b; cin>>a>>b; v.pb({a,b}); } for(int i=0;i<n-2;i++) { for(int j=i+1;j<n-1;j++) { for(int k=j+1;k<n;k++) { if(collinear(v[i].fr,v[i].sc,v[j].fr,v[j].sc,v[k].fr,v[k].sc)) { cout<<"Yes"; return; } } } } cout<<"No"; } signed main() { fastio int tt=1; // cin>>tt; // freopen("input.txt", "r" , stdin); // freopen("output.txt", "w", stdout); while(tt--) { solve(); cout<<"\n"; } return 0; }
/*...................................................................* *............___..................___.....____...______......___....* *.../|....../...\........./|...../...\...|.............|..../...\...* *../.|...../.....\......./.|....|.....|..|.............|.../........* *....|....|.......|...../..|....|.....|..|............/...|.........* *....|....|.......|..../...|.....\___/...|___......../....|..___....* *....|....|.......|.../....|...../...\.......\....../.....|./...\...* *....|....|.......|../_____|__..|.....|.......|..../......|/.....\..* *....|.....\...../.........|....|.....|.......|.../........\...../..* *..__|__....\___/..........|.....\___/...\___/.../..........\___/...* *...................................................................* */ #include <bits/stdc++.h> using namespace std; #define int long long #define INF 1000000000000000000 int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt=1; //cin >> tt; while(tt--) { int n,m; cin >> n >> m; string s[n]; for(int i=0;i<n;i++) { cin >> s[i]; } int o=0,e=0; for(int i=0;i<n;i++) { int c=0; for(int j=0;j<m;j++) { if(s[i][j]=='1') c++; } if(c%2==0) e++; else o++; } cout << o*e << '\n'; } return 0; }
#include<iostream> #include<cstdio> using namespace std; #define fo(i,a,b) for(int i=a;i<=b;++i) #define fod(i,a,b) for(int i=a;i>=b;--i) #define LL long long int n,m; LL ans=0; int main(){ scanf("%d%d",&n,&m); char s[30]; int sum=0; fo(i,1,n){ scanf("%s",s); int u=0; fo(j,1,m){ if(s[j-1]=='1')++u; } if(u&1){ ans+=i-sum-1; ++sum; } else ans+=sum; } printf("%lld",ans); return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <algorithm> #define rep(i,n) for(int i=0;i<(n);++i) #define all(a) (a).begin(),(a).end() using namespace std; //using Graph = vector<vector<int>>; typedef long long ll; using Graph = vector<vector<pair<ll,ll>>>; const int mod =1e+9+7; const int dy[4]={0,1,0,-1}; const int dx[4]={1,0,-1,0}; const ll INF=1e10; int main(){ ll n; cin>>n; vector<ll>a(n); map<ll,ll>mp; rep(i,n){ cin>>a[i]; mp[a[i]]++; } vector<bool>f(200010,true); rep(i,n){ f[a[i]]=false; } ll maxdiff=0; rep(i,n+1){ if(f[i]==true){ maxdiff=i; break; } } vector<ll>ans(n); ans[0]=maxdiff; for(int i=1;i<n;i++){ if(a[n-i]>=maxdiff){ ans[i]=maxdiff; } else{ if(mp[a[n-i]]==1){ ans[i]=a[n-i]; maxdiff=a[n-i]; mp[a[n-i]]--; } else{ mp[a[n-i]]--; ans[i]=maxdiff; } } } reverse(all(ans)); rep(i,n){ cout<<ans[i]<<endl; } }
#include <iostream> #include <cstring> #include <cstdio> #define LL long long #define maxn 210005 using namespace std; int n,a[maxn]; bool vis[maxn]; int main() { memset(vis,0,sizeof(vis)); scanf("%d",&n); for (int i=0;i<n;i++) scanf("%d",&a[i]); int now=0; for (int i=0;i<n;i++) { vis[a[i]]=1; while (vis[now]) now++; printf("%d\n",now); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int A, B, C; cin >> A >> B >> C; cout << ( A + B + C - min( { A, B, C } ) ) << endl; return 0; }
// Created by Pratik // NIT PATNA #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define M 1000000007 #define pi 3.14159265358979323846 #define ll long long #define lld long double // Pair #define pii pair<int, int> #define pll pair<ll, ll> // Vector #define vl vector<ll > #define vi vector<int> #define vpi vector<pii> #define vpl vector<pll> #define pb push_back #define mp make_pair // Search #define lb lower_bound #define ub upper_bound #define mina *min_element #define mama *max_element #define bsrch binary_search //////// #define F first #define S second #define cel(x,a) (((x) + (a) - 1) / (a)) #define all(v) v.begin(), v.end() #define allrev(v) v.rbegin(), v.rend() #define lcm(m, n) ((m / __gcd(m, n))*n) #define deb(...) cerr << "(" << #__VA_ARGS__ << "):", dbg(__VA_ARGS__) #define ms(arr, v) memset(arr, v, sizeof(arr)) #define ps(x,y) fixed << setprecision(y) << x #define _X_ ios_base::sync_with_stdio(false); cin.tie(NULL) //pbds #define ordered tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // Input / Output #define scn(str) scanf("%s", str) #define pri(str) printf("%s\n", str) const int N = 2.5e6 + 4; inline void dbg() { cerr << endl; } template <typename T, typename... V> inline void dbg(T t, V... v) {cerr << ' ' << t; dbg(v...);} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void solve() { //int n; //cin>> n; int arr[3]; for(int &i:arr)cin >> i; sort(arr,arr+3); cout << arr[2]+arr[1] << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); // clock_t time_req = clock(); int t = 1; // cin>> t; while (t--) { solve(); } // time_req = clock() - time_req; // cout << (float)time_req / CLOCKS_PER_SEC; return 0; }
// a(i) a(i+1) // a(i+1)+1 a(i)-1 // // a(i) + i // // A = 8 5 4 7 4 5 // A(i) + i = 8 6 6 10 8 10 // B(i) + i = 10 6 8 10 8 6 // // B = 10 5 6 7 4 1 #include "bits/stdc++.h" #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; #define int long long #define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i) #define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i) #define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i) #define DEBUG(X) { auto _X = (X); cerr << "L" << __LINE__ << ": " << #X << " = " << (_X) << endl; } #define PR(A, n) { cerr << "L" << __LINE__ << ": " << #A << " = "; FOR(_, 1, n) cerr << A[_] << ' '; cerr << endl; } #define PR0(A, n) { cerr << "L" << __LINE__ << ": " << #A << " = "; REP(_, n) cerr << A[_] << ' '; cerr << endl; } #define __builtin_popcount __builtin_popcountll #define SZ(x) ((int)(x).size()) #define ALL(a) (a).begin(), (a).end() #define TWO(x) (1LL<<(x)) inline int gcd(int a, int b) {int r; while (b) {r = a % b; a = b; b = r;} return a;} // for 64-bit, use mt19937_64 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use shuffle instead of random_shuffle #define random_shuffle askcjaljc const int MN = 200111; int a[MN], b[MN], bit[MN]; int n; map<int, vector<int>> ids; void update(int u) { for (int x = u; x <= n; x += x & -x) { bit[x]++; } } int get(int u) { int res = 0; for (int x = u; x > 0; x -= x & -x) { res += bit[x]; } return res; } int32_t main() { ios::sync_with_stdio(0); while (cin >> n) { ids.clear(); memset(bit, 0, sizeof bit); FOR(i,1,n) { cin >> a[i]; a[i] += i; ids[a[i]].push_back(i); } FOR(i,1,n) { cin >> b[i]; b[i] += i; } try { int res = 0; FORD(i,n,1) { if (ids[b[i]].empty()) { throw 1; } int x = ids[b[i]].back(); ids[b[i]].pop_back(); res += get(x-1); update(x); } cout << res << endl; } catch (...) { cout << -1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; int n,a[maxn],b[maxn]; ll sum1,sum2; int tot,head[maxn],pos[maxn]; unordered_map<int,int>mapp; vector<int>qwq[maxn],Tmp; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]),sum1+=a[i]; for(int i=1;i<=n;++i) scanf("%d",&b[i]),sum2+=b[i]; if(sum1!=sum2) puts("-1"),exit(0); for(int i=1;i<=n;++i) { if(!mapp[a[i]+i]) mapp[a[i]+i]=++tot; qwq[mapp[a[i]+i]].push_back(i); } int flag=1; for(int i=1;i<=n;++i) { int p=mapp[b[i]+i]; if(head[p]==qwq[p].size()) flag=0; else pos[i]=qwq[p][head[p]],head[p]++; Tmp.push_back(i); } if(!flag) puts("-1"),exit(0); ll ans=0; for(int i=1;i<=n;++i) { int p=lower_bound(Tmp.begin(),Tmp.end(),pos[i])-Tmp.begin(); ans+=p; Tmp.erase(Tmp.begin()+p); } printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ int N; cin>>N; vector<ll>A(N+1,0); for (int i=1;i<A.size();i++) cin>>A.at(i); vector<ll>p(N+1,0); vector<ll>q(N+1,0); for (int i=0;i<A.size()-1;i++){ p.at(i+1)=p.at(i)+A.at(i+1); q.at(i+1)=q.at(i)>p.at(i+1)?q.at(i):p.at(i+1); } ll x=p.at(0); ll ans=q.at(0); for (int i=1;i<A.size();i++){ ans=ans>x+q.at(i)?ans:x+q.at(i); x+=p.at(i); } cout<<ans<<endl; return 0; }
/* *author:DJ *CODE GIVEN ABOVE ;) */ #pragma GCC optimize "trapv" #pragma GCC optimize("O3") #include <bits/stdc++.h> #define sq(a) a*a const double pi = 2 * acos(0.0) ; using namespace std ; using ll = long long ; template<typename... T> void read(T&... args){ ((cin >> args),...) ; } int main(){ #ifdef LOCAL freopen("in1","r",stdin) ; #endif ios::sync_with_stdio(0) ; cin.tie(nullptr) ; int a,b,c ; read(a,b,c) ; cout << a + (c-b) << "\n" ; #ifdef LOCAL cerr <<"The time elapsed is: " << 1.0 * clock()/CLOCKS_PER_SEC << "s.\n" ; #endif return 0 ; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> string s; bool ch1,ch2; int main(){ cin>>s; ch1=true; ch2=true; for(int i = 0;i < s.size();i+=2){ if(s[i] >='a' && s[i] <= 'z') continue; else{ ch1=false; break; } } for(int i = 1;i < s.size();i+=2){ if(s[i] >= 'A'&& s[i] <= 'Z') continue; else{ ch2=false; break; } } if(ch1 & ch2) cout<<"Yes"<<endl; else cout<<"No"<<endl; }
#include<bits/stdc++.h> #define endl '\n' using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; cout<<n-1<<endl; }
// Problem: B - Gentle Pairs // Contest: AtCoder - AtCoder Beginner Contest 187 // URL: https://atcoder.jp/contests/abc187/tasks/abc187_b // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef long long ll; #define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define mp make_pair #define pb push_back #define ff first #define ss second #define vi vector<int> #define vll vector<ll> #define all(x) (x).begin() , (x).end() void dbg(){ cerr << endl; } template<typename Head , typename... Tail> void dbg(Head h , Tail... t){ cerr << h << " "; dbg(t...); } #ifdef EMBI_DEBUG #define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", dbg(__VA_ARGS__) #else #define debug(...) #endif const int max_n = 1e5 + 9; const int mod = 1e9 + 7; const int inf = 1e9; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; ll power(ll a , ll b) { ll prod = 1; while(b) { if(b&1) prod = (prod*a)%mod; a = (a*a)%mod; b >>= 1; } return prod; } int check(pair<int , int> x , pair<int , int> y){ return abs(x.ff - y.ff) >= abs(x.ss - y.ss); } void solve(){ int n; cin >> n; pair<int , int> a[n]; for(int i = 0 ; i < n ; i++){ int x , y; cin >> x >> y; a[i] = {x , y}; } int ans = 0; for(int i = 0 ; i < n ; i++){ for(int j = i+1 ; j < n ; j++){ // if(i == j)continue; ans += check(a[i] , a[j]); debug(i , j , check(a[i] , a[j])); } } cout << ans << "\n"; } signed main(){ int t = 1; // cin >> t; for(int i = 1 ; i <= t ; i++){ solve(); } }
//git pull --rebase origin master #include<bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i,n) for(int i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ll long long #define si(x) scanf("%d",&x) #define sl(x) scanf("%lld",&x) #define ss(s) scanf("%s",s) #define pi(x) printf("%d\n",x) #define pl(x) printf("%lld\n",x) #define ps(s) printf("%s\n",s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define wi(t) int t;cin>>t;while(t--) 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; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0,lim-1); return uid(rang); } int mpow(int base, int exp); void ipgraph(int n, int m); void dfs(int u, int par); const int mod = 1000000007; const int N = 3e5, M = N; //======================= vi g[N]; int a[N]; ll int gcd(ll int a, ll int b) { if (b == 0) return a; return gcd(b, a % b); } int tc=1; void solve() { ll int n; cin>>n; if(n%2) { cout<<"Black\n"; } else { cout<<"White\n"; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); // wi(t) { solve(); } return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; } void ipgraph(int n, int m){ int i, u, v; while(m--){ cin>>u>>v; u--, v--; g[u].pb(v); g[v].pb(u); } } void dfs(int u, int par){ for(int v:g[u]){ if (v == par) continue; dfs(v, u); } }
#include <stdio.h> #include <iostream> #include <vector> // #include <bits/stdc++.h> #include <queue> #include <algorithm> #include <string> #include <iomanip> #include <cmath> using namespace std; typedef long long ll; #define rep(i,k,n) for(ll i=k; i<(ll)(n); i++) #define Q 1000000007 ll max(vector<ll> a) { return *max_element(a.begin(), a.end()); } ll max_index(vector<ll> a) { vector<ll>::iterator max_it = max_element(a.begin(), a.end()); return distance(a.begin(), max_it); } int main(){ ll r1,r2,c1,c2; cin >> r1 >> c1; cin >> r2 >> c2; ll r = r1-r2, c = c1-c2; ll ans = 3; if(r==0 && c==0) ans = 0; else if(r==c || r+c==0 || abs(r)+abs(c)<=3) ans = 1; else if((r1+r2+c1+c2)%2==0 || abs(r)+abs(c)<=6 || abs(r+c)<=3 || abs(r-c)<=3) ans = 2; cout << ans << endl; return 0; }
/*It is better to taste failure than to regret*/ #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <climits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <ratio> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef long double LD; #define int ll #define double LD #define pb push_back #define mp make_pair #define REP(i,n) for (int i = 0; i < n; i++) #define loop(i,n) for (int i = 0; i < n; i++) #define FOR(i,a,b) for (int i = a; i < b; i++) #define REPD(i,n) for (int i = n-1; i >= 0; i--) #define FORD(i,a,b) for (int i = a; i >= b; i--) #define remax(a,b) a = max(a,b) #define remin(a,b) a = min(a,b) #define mem(a, b) memset(a, b, sizeof a) #define append push_back #define all(v) v.begin(),v.end() #define F first #define S second #define print(x) cout << (x) << endl #define PRECISION(x) cout << fixed << setprecision(x) #define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define Test(){int t;cin >> t;for(int i=1;i<=t;i++){cout << "Case #" << i << ": ";solve();}} #define test(){int t; cin>>t; for(int i=1;i<=t;i++){solve();}} #define vi vector<int> #define vii vector<pair<int,int> > #define vci vector<pair<char,int> > #define vic vector<pair<int,char> > #define N 200005 #define pb push_back const double PI=acos(-1); const int MOD = 1000000007; const int FMOD = 998244353; int maxSumsubarray(int arr[], int n, int maxSum){ int sum = arr[0], overallMax = 0, start = 0; for (int i = 1; i < n; i++) { if (sum <= maxSum) overallMax = max(overallMax, sum); while (sum + arr[i] > maxSum && start < i) { sum -= arr[start]; start++; } sum += arr[i]; } if (sum <= maxSum) overallMax = max(overallMax, sum); return overallMax; } void solve() { int a,b,c,d; cin >>a>>b>>c>>d; if(a==c && b==d){ cout << "0\n"; return; } if(a-b == c-d || a+b == c+d){ cout << "1\n"; return; } else if(abs(a-c) + abs(b-d) <= 3){ cout << "1\n"; return; } if((a+b)%2 == (c+d)%2){ cout << "2\n"; } else{ for(int i=-3;i<=3;i++){ for(int j=-3;j<=3;j++){ int x = c + i; int y = d + j; if(abs(c-x) + abs(d-y) <= 3){ if(x-y == a-b || x+y == a+b){ cout << "2\n"; return; } } } } cout << "3\n"; } } signed main() { // test(); solve(); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<ll> vl; // input int N; vi A; void input() { cin >> N; A = vi(N); rep(i, N) cin >> A[i]; } int main() { input(); ll ans = 0; ll x = 0; ll s = 0; ll mx = 0; rep(i, N) { s += A[i]; mx = max(mx, s); ans = max(ans, x + mx); x += s; } cout << ans << endl; }
//{{{ #include<algorithm> #include<cmath> #include<deque> #include<fstream> #include<iomanip> #include<iostream> #include<map> #include<numeric> #include<queue> #include<random> #include<set> #include<sstream> #include<sys/time.h> #include<unordered_map> #include<unordered_set> #include<vector> using ll = long long; enum : int { M = (int)1e9 + 7 }; enum : ll { MLL = (ll)1e18L + 9 }; using namespace std; #ifdef LOCAL #include"rprint2.hpp" #else #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ } FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0); #endif template <template <class T, class = std::allocator<T>> class V, class E> istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; } struct Init { Init(){ cin.tie(0); ios::sync_with_stdio(false); } } init; //}}} int main(){ // for(int i = 1; i < 1000; i++){ // int e = 0, o = 0; // for(int j = 1; j <= i; j++){ // if(i % j == 0){ // (j & 1 ? o : e)++; // } // } // printe(i, e, o, e < o ? "Odd" : e == o ? "Same" : "Even"); // } int t; cin >> t; vector<string> strs({"Even", "Odd", "Same", "Odd"}); for(int _ = 0; _ < t; _++){ ll n; cin >> n; cout << strs[n & 3] << '\n'; } }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------- void _main(){ char a, b, c; cin >> a >> b >> c; cout << b << c << a << endl; return; }
#include<bits/stdc++.h> using namespace std; #define int long long #define double long double #define endl '\n' #define pb push_back #define pob pop_back #define pii pair<int,int> #define mod 1000000007 // #define mod 1000000009 // #define mod 163577857 // #define mod 998244353 #define rep(i,n) for (int i = 0; i < n; i++) #define repp(i,a,b) for(int i = a ; i<b ; i++) #define reppr(i,a,b) for(int i = a-1 ; i>=b ; i--) #define repr(i,n) for (int i = n - 1; i >= 0; i--) #define ff first #define ss second #define inf 9223372036854775807 #define infn -9223372036854775807 #define pi 3.14159265358979323846 #define eps 0.0000000001 #define setprec(x) cout << fixed << setprecision(x); #define REVERSE(a) reverse(all(a)); #define SORT(a) sort(all(a)); #define all(n) n.begin(),n.end() #define setbits(x) __builtin_popcountll(x) //GCD and LCM int gcd (int a, int b) { return b ? gcd (b, a % b) : a; } int lcm (int a, int b) { return a / gcd(a, b) * b; } //Modular Exponentiation int powmod(int x,int y) { if (y == 0) return 1; int p = powmod(x, y/2) % mod; p = (p * p) % mod; return (y%2 == 0)? p : (x * p) % mod; } //Modular Inverse int inverse(int a) { return powmod(a,mod-2); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////NO TEXT ABOVE IT////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool codejam = 0, testcases = 0; void solve() { int n; cin >> n; int l = 1 , r = 2e9; while(l<r) { int mid = (l+r+1)/2; if((mid*(mid+1))/2<=n+1) l = mid; else r = mid-1; } cout << n-l+1 << endl; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////NO TEXT BELOW IT////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// signed main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int t = 1; if(testcases) cin >> t; rep(i,t) { if(codejam) cout << "Case #" << i+ 1<< ": "; solve(); } }
#include <bits/stdc++.h> #define F first #define S second #define MP make_pair #define pb push_back #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define LCM(a, b) (a) / __gcd((a), (b)) * (b) #define log_2(a) (log((a)) / log(2)) #define ln '\n' using namespace std; using LL = long long; using ldouble = long double; using P = pair<int, int>; using LP = pair<LL, LL>; static const int INF = INT_MAX; static const LL LINF = LLONG_MAX; static const int MIN = INT_MIN; static const LL LMIN = LLONG_MIN; static const int MOD = 1e9 + 7; static const int SIZE = 200005; const int dx[] = {0, -1, 1, 0}; const int dy[] = {-1, 0, 0, 1}; template<typename T> void printArray(vector<T> &printVec) { if(printVec.empty()) return; cout << printVec[0]; for(int i = 1; i < printVec.size(); ++i) cout << " " << printVec[i]; cout << endl; } vector<LL> Div(LL n) { vector<LL> ret; for(LL i = 1; i * i <= n; ++i) { if(n % i == 0) { ret.pb(i); if(i * i != n) ret.pb(n / i); } } sort(all(ret)); return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; int res = INF; vector<int> a(N), b(N); for(int i = 0; i < N; ++i) { cin >> a[i] >> b[i]; res = min(res, a[i] + b[i]); } for(int i = 0; i < N; ++i) { for(int j = 0; j < N; ++j) { if(i == j) continue; res = min(res, max(a[i], b[j])); } } cout << res << endl; return 0; }
#include<cstdio> #define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i) using namespace std; const int N=1005,INF=0x3fffffff; template<typename T>void read(T &x) { bool neg=false; unsigned char c=getchar(); for(;(c^48)>9;c=getchar())if(c=='-')neg=true; for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48); if(neg)x=-x; } int n,a[N],b[N],min=INF,ans=INF; int main() { read(n);F(i,0,n)read(a[i]),read(b[i]); F(i,0,n) { if(b[i]<ans&&min<ans)ans=b[i]>min?b[i]:min; if(b[i]+a[i]<ans)ans=b[i]+a[i]; min=a[i]<min?a[i]:min; } min=INF; for(int i=n-1;~i;--i) { if(b[i]<ans&&min<ans)ans=b[i]>min?b[i]:min; if(b[i]+a[i]<ans)ans=b[i]+a[i]; min=a[i]<min?a[i]:min; } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ char a, b, c; cin >> a >> b >> c; if(a == b && b == c)cout << "Won"; else cout << "Lost"; }
#include<iostream> #include<string> #include<vector> #include<set> #include<iomanip> #include<algorithm> #include<cmath> #include<bitset> #include<queue> #include<stack> #include<utility> #include<map> using namespace std; typedef long long ll; typedef long double lb; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; using Graph = vector<vector<int>>; #define FOR(i, a, b) for(ll i=(a); i<(b); ++i) #define REP(i, n) FOR(i, 0, n) #define NREP(i, n) FOR(i, 1, n+1) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; const ll INF=2e9+1; //for some test case (near inf) const ll INFLL=1e18; const ll MOD=998244353; int main(void){ string S; cin >> S; if(S[0]==S[1]&&S[1]==S[2]) cout << "Won" << endl; else cout << "Lost" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; //using int = long long int; using ll = long long int; using pii = pair < int, int >; #define ff first #define ss second #define endl '\n' #define pb push_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define rep(i, j, n) for(int i = j; i < n; i++) #define ren(i, j, n) for(int i = n-1; i >= j; i--) #define cerr cout void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) { cerr << t; if(sizeof...(v)) cerr << ", "; _print(v...); } #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) const int mod = 1000000007; void io_set(){ #ifndef ONLINE_JUDGE freopen("inputf.in", "r", stdin); freopen("outputf.in", "w", stdout); #endif } void solve(){ int a, b, c; cin >> a >> b >> c; cout << (((c*c - a*a - b*b) > 0)? "Yes" : "No") << endl; } signed main(int argc, char **argv){ //io_set(); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tt = 1; //cin >> tt; while(tt--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B, C; cin >> A >> B >> C; if( A*A + B*B < C*C ) cout << "Yes"; else cout << "No"; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define PI 3.14159265 template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } //mod Inverse long long modInverse(long long a, long long m) //m and a should be coprime. { long long m0 = m; long long y = 0, x = 1; if (m == 1) return -1; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } // HERE a = numerator, b = denominator, m = mod; long long modDivide(long long a, long long b, int m) { a = a % m; long long inv = modInverse(b, m); if (inv == -1) return -1; else { long long ans = (inv * a) % m; return ans; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); ll t; cin >> t; while(t--) { ll n, s, k; cin >> n >> s >> k; ll g = __gcd(k, n); if(s%g != 0) { cout << "-1" << "\n"; } else { n /= g, k /= g, s /= g; ll a = n-s; ll b = k; cout << modDivide(a, b, n) << "\n"; } } }
#define _USE_MATH_DEFINES #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <complex> #include <deque> #include <iostream> #include <list> #include <map> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #include <queue> #define INF 1010101010LL #define INFLL 1010101010101010101LL using namespace std; const int mod = 1000000007; //const int mod = 998244353; int main() { int n; cin >> n; vector<int> a(n), p(n), x(n); for (int i = 0; i < n; i++) { cin >> a[i] >> p[i] >> x[i]; } int mn = INF; for (int i = 0; i < n; i++) { if (x[i] - a[i] > 0) { mn = min(mn, p[i]); } } cout << (mn == INF ? -1 : mn) << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { char S, T; cin >> S >> T; if(S=='Y'){ char s = toupper(T); cout <<s<< endl; }else{ cout << T << endl; } }
#include <bits/stdc++.h> #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using namespace std; using ll = long long; struct Edge { ll to; ll cost; }; using Graph = vector<vector<Edge>>; using P = pair<ll, ll>; #define mp make_pair #define REP(i, n) for (int i = 0; i < (n); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(v) (v).begin(), v.end() const ll MOD = 1000000007; const ll nmax = 8; const ll INF = LLONG_MAX; const int MAX = 510000; bool graph[nmax][nmax]; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF)); void warshall_floyd(ll n) { for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < n; j++) { for (size_t k = 0; k < n; k++) { dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]); } } } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); } ll powMod(ll a, ll p) { if (p == 0) { return 1; } else if (p % 2 == 0) { ll half = powMod(a, p / 2); return mulMod(half, half); } else { return mulMod(powMod(a, p - 1), a); } } ll ceil(ll a, ll b) { return (a + b - 1) / b; } bool is_sum_overflow(ll a, ll b) { if (a < LLONG_MAX / 2) { if (b < LLONG_MAX / 2) return false; else return ((b - LLONG_MAX / 2) + a > LLONG_MAX / 2); } else { if (b < LLONG_MAX / 2) return true; else return ((a - LLONG_MAX / 2) + b > LLONG_MAX / 2); } } bool isOK(ll mid, string X, ll M) { ll sum = 0; for (int i = X.size() - 1; i >= 0; i--) { ll tmp = X[i] - '0'; for (int j = 0; j < X.size() - i - 1; j++) { ll prev = tmp; if (tmp >= LLONG_MAX / mid) { // 多分オーバーフローしたからMは超えてる return true; } tmp *= mid; } if (is_sum_overflow(sum, tmp)) { // overflow下からMは超えてる return true; } sum += tmp; } if (sum > M) { return true; } else { return false; } } void solve(std::string X, long long M) { if (X.length() == 1) { ll d = (X[0] - '0'); if (d <= M) cout << 1 << endl; else cout << 0 << endl; return; } ll d = 0; for (int i = 0; i < X.size(); i++) { d = max(d, (ll)(X[i] - '0')); } ll left = d; ll right = 1e18; right += 10; while (right - left > 1) { ll mid = left + (right - left) / 2; if (isOK(mid, X, M)) right = mid; else left = mid; } cerr << left << endl; cout << left - (d + 1) + 1 << endl; } int main() { std::string X; std::cin >> X; long long M; scanf("%lld", &M); solve(X, M); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define POPCOUNT(x) __builtin_popcount(x) template <typename T> void chmin(T &a, const T &b) { a = min(a, b); } template <typename T> void chmax(T &a, const T &b) { a = max(a, b); } const ll INF = 1LL << 60; struct FastIO { FastIO() { cin.tie(0); ios::sync_with_stdio(0); } } fastiofastio; const ll MOD = 1e9 + 7; int main() { int N, M; cin >> N >> M; vector<ll> w(N); REP(i, N) cin >> w[i]; vector<ll> l(M), v(M); REP(i, M) cin >> l[i] >> v[i]; vector<ll> vec = v; sort(ALL(vec)); vec.erase(unique(ALL(vec)), vec.end()); int sz = vec.size(); vector<ll> mi(sz + 1); REP(i, M) { int idx = lower_bound(ALL(vec), v[i]) - vec.begin(); chmax(mi[idx], l[i]); } REP(i, sz) chmax(mi[i + 1], mi[i]); vector<int> perm(N); iota(ALL(perm), 0); ll ans = INF; do { vector<ll> pos(N); vector<vector<PII>> G(N, vector<PII>()); bool NG = 0; REP(i, N) { ll sum = 0; FOR(j, i, N) { sum += w[perm[j]]; int idx = lower_bound(ALL(vec), sum) - vec.begin(); if (idx > 0) { if (i == j) { cout << -1 << endl; return 0; } G[i].emplace_back(j, mi[idx - 1]); } } } REP(i, N) { for (auto [to, len] : G[i]) { chmax(pos[to], pos[i] + len); } } chmin(ans, pos[N - 1]); } while (next_permutation(ALL(perm))); cout << ans << endl; }
#include<bits/stdc++.h> #define llinf 4154118101919364364 using namespace std; long long n,m; long long w[32],l[131072],v[131072]; long long res=llinf; long long sigw[1024]={0}; map<long long,long long> mp; long long find(long long wei){ if(mp[wei]!=-1){return mp[wei];} long long d=0; for(int i=0;i<m;i++){ if(v[i]<wei){d=max(l[i],d);} } mp[wei]=d; return mp[wei]; } long long p[16],d[16]; void rep(long long fl,long long cnt){ //cerr << fl << ' ' << d[cnt-1] << '\n'; if(cnt==n){ res=min(d[n-1],res); return; } for(int i=0;i<n;i++){ if(!(fl&(1ll<<i))){continue;} p[cnt]=i; d[cnt]=0; long long cw=0; for(int j=0;j<cnt;j++){ cw+=w[p[j]]; } cw+=w[i]; for(int j=0;j<cnt;j++){ //cout << fl << ' ' << j << ' ' << cw << ' ' << find(cw)<< '\n'; d[cnt]=max(d[j]+find(cw),d[cnt]); cw-=w[p[j]]; } rep(fl^(1ll<<i),cnt+1); } return; } int main(){ long long mr=0; scanf("%lld%lld",&n,&m); for(int i=0;i<n;i++){ scanf("%lld",&w[i]); mr=max(w[i],mr); } for(int i=0;i<m;i++){ scanf("%lld%lld",&l[i],&v[i]); if(v[i]<mr){puts("-1");return 0;} } mp[0]=0; for(int i=1;i<(1ll<<n);i++){ for(int j=0;j<n;j++){ if(i&(1ll<<j)){sigw[i]+=w[j];} } mp[sigw[i]]=-1; //cout << sigw[i] << '\n'; } rep((1ll<<n)-1,0); printf("%lld\n",res); return 0; }
#include <bits/stdc++.h> #define mod 1000000007 #define fast_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define vi vector<int> #define vl vector<ll> #define REP(s, e) for (auto i = s; i <= e; i++) #define show(s) cout<<s<<" " #define MAXI 2147483647 #define MAXL 9223372036854775807 #define FOR(a,b) for (ll i = a; i < b; i++) using namespace std; typedef signed long long int ll; int main() { fast_io; int a,b,c; cin >> a >> b >> c; cout << 21 - (a+b+c); }
#include <bits/stdc++.h> using namespace std; #define int long long #define deb(x) cout <<"\n"<< (#x) << " = " << (x) << "\n" using pi = pair<int,int>; using vi = vector<int>; using vb = vector<bool>; using vpi = vector<pi>; using vvi = vector<vi> ; #define mp make_pair #define f first #define s second #define sz(x) (int)(x).size() #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define sor(x) sort(all(x)) #define ft front() #define bk back() #define pf push_front #define pb push_back #define lb lower_bound #define ub upper_bound #define loop(i,a,b) for(int i = (a); i <=(b); ++i) #define loopr(i,a,b) for(int i = (a); i >= (b); --i) #define trav(a,x) for (auto& a: x) #define rep(i,n) for(int i = 0 ; i<n ; ++i) #define max3(a,b,c) max(a,max(b,c)) const int MOD = 1e9+7; // 998244353; const int INF = 1e18; const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; // for every grid problem!! /* */ void solve() { int a,b,x,y; cin>>a>>b>>x>>y; int ans=x ; if(a==b) { cout<<ans<<"\n"; return; } else if(a<b) { int op1=(b-a)*y; int op2=(b-a)*2*x; ans=ans+min(op2,op1); cout<<ans<<"\n"; return ; } else { ans=0; int op1=(a-b-1)*y+x; int op2=2*(a-b-1)*x+x ; // deb(op2); ans=ans+min(op1,op2); cout<<ans<<"\n"; return; } } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
#include <iostream> // cout, endl, cin #include <string> // string, to_string, stoi #include <vector> // vector #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <utility> // pair, make_pair #include <tuple> // tuple, make_tuple #include <cstdint> // int64_t, int*_t #include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <iomanip>//setprecision #include<math.h> #include<numeric>//gcd(,),lcm(,) #include<cmath>//sin(),cos(),tan() kodoho #include<ctime>//jikan(clock.()) #include <stdio.h> #include <cstring> #define _GLIBCXX_DEBUG using namespace std; const long long INF = 10000000; using ll = long long; int main() { set<int>mar; set<int>bat; for (int i = 0; i < 10; i++) { char k; cin >> k; if (k == 'o') { mar.insert(i); } if (k == 'x') { bat.insert(i); } } ll ans = 0; for (int i = 0; i <= 9999; i++) { set<int>L = mar; bool OK = true; for (int h = 0; h<4; h++) { int c = pow(10, h); int G = i /c ; G = G % 10; if (L.count(G)) { L.erase(G); } if (bat.count(G)) { OK = false; break; } } if (OK == true) { if (L.empty()) { ans++; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long i64; typedef unsigned long long ui64; typedef vector<i64> vi; typedef vector<vi> vvi; typedef pair<i64, i64> pi; #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) inline void RI(i64 &i) {scanf("%lld", &(i));} inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } } inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } } #define BUF_LENGTH 1000000 inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;} template<typename T> inline bool IN(T &S, const typename T::key_type &key) { return S.find(key) != S.end(); } class SegTree { public: //////////////////////////////////////////////////////// // monoid using X = long long; X X_dot_X(const X &v1, const X &v2) { return v1 + v2; } const X X_unit = 0; //////////////////////////////////////////////////////// typedef size_t idx_type; vector<X> data; idx_type n; SegTree(idx_type n_) { n = 1; while(n <= size_t(n_)) { n *= 2; } data.resize(n * 2 - 1, X_unit); } ~SegTree() {} X query(idx_type a, idx_type b, idx_type k, idx_type l, idx_type r) { if(r <= a || b <= l) { return X_unit; } if(a <= l && r <= b) { return data[k]; } else { X vl = query(a, b, k * 2 + 1, l, (l + r) / 2); X vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return X_dot_X(vl, vr); } } X query(idx_type a, idx_type b) { return query(a, b, 0, 0, n); } void update(idx_type i, X x) { i += n - 1; data[i] = x; while(i > 0) { i = (i - 1) / 2; data[i] = X_dot_X(data[i*2+1], data[i*2+2]); } } }; int main(int argc, char *argv[]) { i64 H, W, M, i, x; cin >> H >> W >> M; map<i64, vi> D; REP(0, M, i) { i64 x, y; cin >> y >> x; x--; y--; D[y].pb(x); } for(auto &p: D) { sort(all(p.second)); } if(D.empty()) { cout << H*W << endl; return 0; } i64 y_prev = -1; i64 ans = 0; bool blk = false; SegTree tree(W); REP(0, W, x) { tree.update(x, 1); } for(auto &d: D) { const i64 &y = d.first; vi &v = d.second; if(y == 0) { i64 x0 = v[0]; REP(x0, W, x) { tree.update(x, 0); } ans += x0; } else { i64 dy = y - y_prev - 1; if(blk) { ans += dy * tree.query(0, W); //cerr << "interval " << dy * tree.query(0, W) << endl; } else { ans += dy * W; //cerr << "interval " << dy * W << endl; } for(auto &vv: v) { tree.update(vv, 0); } i64 x0 = v[0]; if(x0 == 0) { blk = true; } if(blk) { ans += tree.query(0, W); } else { ans += tree.query(x0, W) + x0; } //cerr << "y " << tree.query(x0, W) + x0 << endl; } //cerr << y_prev << " " << y << " " << ans << endl; y_prev = y; } if(blk) { ans += (H - y_prev - 1) * tree.query(0, W); } else { ans += (H - y_prev - 1) * W; } cout << ans << endl; return 0; }
#include <iostream> #include <string> #include <math.h> using namespace std; typedef long long llong; int main() { string s; llong sum = 0; llong i = 0; cin >> s; int n = s.length() - 1; for (i = 0; i <= n - 1; i++) { sum += (llong)((pow(10, i + 1) - pow(10, i)) * (i / 3)); } llong t = stoll(s); sum += (t - pow(10, i) + 1) * (i / 3); cout << sum << endl; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define per(i,r,l) for(int i=(r);i>=(l);--i) using namespace std; typedef long long ll; const int N=1e5+10; int n,a[N],b[N],c[N],x;ll ans; int main(){ scanf("%d",&n); rep(i,1,n) scanf("%d",a+i),ans+=a[i]; rep(i,1,n) scanf("%d",&x),i&1?b[i+1>>1]=x-a[i]:c[i>>1]=x-a[i]; sort(b+1,b+(n>>1)+1);sort(c+1,c+(n>>1)+1); rep(i,1,n>>1) if(b[i]+c[i]>0) ans+=b[i]+c[i]; printf("%lld\n",ans); return 0; }
#include<bits/stdc++.h> #define ll long long using namespace std; int main() { ll H,W; cin>>H>>W; char mat[H+1][W+1]; for(ll i=0;i<H;i++) { for(ll j=0;j<W;j++) { cin>>mat[i][j]; } } ll count=0; for(ll i=1;i<H-1;i++) { for(ll j=1;j<W-1;j++) { if(mat[i][j]=='#') { if(mat[i][j-1]=='#') { if(mat[i-1][j-1]=='#'&&mat[i-1][j]=='.') { count++; } if(mat[i+1][j-1]=='#'&&mat[i+1][j]=='.') { count++; } } else { if(mat[i+1][j]=='.') { count++; } if(mat[i-1][j]=='.') { count++; } } } } } for(ll i=1;i<H-1;i++) { for(ll j=1;j<W-1;j++) { if(mat[i][j]=='#') { if(mat[i-1][j]=='#') { if(mat[i-1][j-1]=='#'&&mat[i][j-1]=='.') { count++; } if(mat[i-1][j+1]=='#'&&mat[i][j+1]=='.') { count++; } } else { if(mat[i][j+1]=='.') { count++; } if(mat[i][j-1]=='.') { count++; } } } } } cout<<count; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl ("\n") #define int long long #define pb push_back #define mp make_pair #define ff first #define ss second #define all(c) c.begin(), c.end() #define rep(i,n) for(int i=0;i<n;i++) #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int32_t main(){ fast int t=1; // cin>>t; while(t--){ int n, k;cin>>n>>k; vector<pair<int, int>>vv, v; map<int, int>mm; set<int>s; rep(i, n){ int x, y;cin>>x>>y; mm[x]+=y; s.insert(x); } for(auto i:s){ v.pb({i, mm[i]}); } // for(auto i:v){ // cout<<i.ff<<" "<<i.ss<<endl; // } sort(all(v)); int pre[v.size()+1]={0}; int val[v.size()+1]={0}; pre[0]=v[0].ff, val[0]=v[0].ss; for(int i=1;i<v.size();i++){ pre[i]=v[i].ff+pre[i-1]; val[i]=v[i].ss+val[i-1]; } int ans=k, stop=0;; for(int i=0;i<v.size();i++){ if(ans>=(v[i].ff-stop) and stop!=v[i].ff){ ans =k+val[i]; ans-=v[i].ff; stop=v[i].ff; } else if(stop==v[i].ff){ ans+=v[i].ss; } else break; } // cout<<stop<<" "<<ans<<endl; cout<<stop+ans<<endl; } return 0; }
#include<bits/stdc++.h> #define rg register #define fp( i , x , y ) for( rg int i=(x); i<=(y); ++i ) #define fq( i , x , y ) for( rg int i=(y); i>=(x); --i ) #define i60 long long using namespace std ; const int N = 2e5+10 , skc = 998244353 ; int pre[N] , deg[N] , n , pi[N] ; queue <int> q ; bool is[N] ; int f( int x ) { return pre[x] == x ? x : pre[x] = f(pre[x]) ; } void merge( int x , int y ) { if( f(x) == f(y) ) return ; pre[f(x)] = f(y) ; } signed main( ) { ios::sync_with_stdio(false) ; cin.tie(0) ; cin >> n ; int x , y ; fp( i , 1 , n ) pre[i] = i ; fp( i , 1 , n ) cin >> x , pi[i] = x , merge( x , i ) , deg[x] ++ ; fp( i , 1 , n ) if( !deg[i] ) q.push(i) ; while( q.size() ) { x = q.front() ; q.pop() ; y = pi[x] ; deg[y] -- ; if( !deg[y] ) q.push(y) ; } int ans = 1 , tot = 0 ; fp( i , 1 , n ) if( deg[i] ) is[f(i)] = 1 ; fp( i , 1 , n ) tot += is[i] ; fp( i , 1 , tot ) ans = 2ll * ans % skc ; cout << (ans-1+skc)%skc << '\n' ; return 0 ; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++) #define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++) #define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--) #define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--) #define ALL(x) (x).begin(), (x).end() #define PB(x) push_back(x) #define MP(x, y) make_pair(x, y) const ll INF = 100100100100100100; const ll MOD = 1000000007; // get abs ll my_abs(ll a); // a^n ll a_n(ll a, ll n); // get gcd ll my_gcd(ll a, ll b); // a^(-1) % MOD ll inv(ll a); // (a+b+c)%MOD ll madd(ll a, ll b, ll c); // (a-b)%MOD ll msub(ll a, ll b); // (a*b*c)%MOD ll mtime(ll a, ll b, ll c); int main() { int n, q; bool flip = false; string s; cin >> n >> s >> q; rep(i, q) { int t, a, b; cin >> t >> a >> b; a--; b--; if(t == 1) { if(flip) { if(a < n) { a += n; } else { a -= n; } if(b < n) { b += n; } else { b -= n; } auto tmp = s[a]; s[a] = s[b]; s[b] = tmp; } else { auto tmp = s[a]; s[a] = s[b]; s[b] = tmp; } } else { flip = !flip; } } if(flip) { cout << s.substr(n) + s.substr(0, n) << endl; } else { cout << s << endl; } } ll my_abs(ll a) { if(a >= 0) return a; else return -1 *a; } ll a_n(ll a, ll n) { if(n == 0) return 1; ll ret = a, count = 1; while(count * 2 < n) { ret *= ret; count *= 2; } if(count == n) return ret; else return (ret * a_n(a, n-count)); } ll my_gcd(ll a, ll b) { if(b == 0) return a; return my_gcd(b, a%b); } ll inv(ll a) { return a_n(a, MOD-2); } ll madd(ll a, ll b, ll c) { ll ret = (a+b) % MOD; return (ret+c) % MOD; } ll msub(ll a, ll b) { if(a < b) return (a-b+MOD) % MOD; else return (a-b) % MOD; } ll mtime(ll a, ll b, ll c) { ll ret = (a*b) % MOD; return (ret*c) % MOD; }
#include <bits/stdc++.h> #define MOD 1000000007 #define PI (2*acos(0.0)) #define eps 1e-9 #define ff first #define ss second #define pb push_back #define endl '\n' #define fu cout << "lol" << endl; #define precision(n) cout << fixed << setprecision(n); #define debug(x) cout << (#x) << " is " << (x) << endl; #define printv(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl; #define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl; #define all(a) a.begin(), a.end() #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define mem(a,val) memset(a,val,sizeof(a)) #define FORN(i, n) for(int i = ; i < n; i++) #define REP(i, a, b) for(int i = a; i < b; i++) #define SQ(a) (a)*(a) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<ll> vll; typedef vector<ull> vull; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<vb> vbb; ull mod = 1e9 + 7; // ull mod = 998244353; ull fact(ull n){ ull ans = 1; for (ull i = 2; i <= n; i++) ans = ((ans%MOD) * (i%MOD) )%MOD; return ans; } ll powMod(ll base, ll exp){ ll ans = 1; for (int i = 0; i < exp; i++) { ans*= base; ans %= mod; } return ans; } ll powMod(ll base, ll exp, ll md){ ll ans = 1; for (int i = 0; i < exp; i++) { ans*= base; ans %= md; } return ans; } double solve(int D, int H, int d, int h){ double slop = (double) (H - h)/(double) (D-d); double b = h - slop*d; return b; } void answer(){ int n; cin >> n; int D, H; cin >> D >> H; vi d(n), h(n); double ans = 0; for (int i = 0; i < n; i++) { cin >> d[i] >> h[i]; ans = max(ans, solve(D, H, d[i], h[i])); } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed; int t = 1; // cin >> t; cin.ignore(); while (t--) { answer(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define fo(i,a,b) for(int i=a; i<b; i++) #define vi vector<int> #define vii vector<int,int> #define pi pair<int,int> #define F first #define S second #define PB push_back #define MP make_pair #define FASTIO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int main(){ FASTIO double d,h; int n; cin>>n>>d>>h; double minslope = h/d; double x1,y1; double tslope; fo(i,0,n){ cin>>x1>>y1; tslope = (h-y1)/(d-x1); minslope = min(minslope,tslope); } double ans = h - minslope*d; cout<<ans; return 0; }
#include<bits/stdc++.h> // #include<atcoder/all> using namespace std; // using namespace atcoder; using lint = long long; using ulint = unsigned long long; template<class T>using graph = vector<vector<T>>; #define endl '\n' int const INF = 1<<30; lint const INF64 = 1LL<<61; lint const mod = 1e9+7; //long const mod = 998244353; lint ceilDiv(lint x, lint y){if(x >= 0){return (x+y-1)/y;}else{return x/y;}} lint floorDiv(lint x, lint y){if(x >= 0){return x/y;}else{return (x-y+1)/y;}} lint Sqrt(lint x){lint upper = 1e9;lint lower = 0;while(upper - lower > 0){lint mid = (1+upper + lower)/2;if(mid * mid > x){upper = mid-1;}else{lower = mid;}}return upper;} lint gcd(lint a,lint b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);} lint lcm(lint a,lint b){return (a / gcd(a,b)) * b;} lint chmin(vector<lint>&v){lint ans = INF64;for(lint i:v){ans = min(ans, i);}return ans;} lint chmax(vector<lint>&v){lint ans = -INF64;for(lint i:v){ans = max(ans, i);}return ans;} double dist(double x1, double y1, double x2, double y2){return sqrt(pow(x1-x2, 2) + pow(y1-y2,2));} vector<lint>prime;void makePrime(lint n){prime.push_back(2);for(lint i=3;i<=n;i+=2){bool chk = true;for(lint j=0;j<prime.size() && prime[j]*prime[j] <= i;j++){if(i % prime[j]==0){chk=false;break;}}if(chk)prime.push_back(i);}} int main(){ vector<int>v(100001,1); int tmp = 1; for(int i=1;i<=100000;i++){ if(i < (1<<tmp))v[i] = tmp; else{ tmp++; v[i] = tmp; } } int n; cin >> n; for(int i=1;i<=n;i++)cout << v[i] << " "; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pint; typedef pair<ll,ll> pll; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<string> vstr; typedef vector<pint> vpint; typedef vector<pll> vpll; #define vint2(v,n,m,init) vector<vector<int>> v(n, vector<int>(m, init)) #define vll2(v,n,m,init) vector<vector<ll>> v(n, vector<ll>(m, init)) #define rep(i,n) for(ll i=(ll)0; i<(ll)n; i++) #define REP(i,m,n) for(ll i=(ll)m; i<(ll)n; i++) #define arr(var, n) vint var(n); rep(i,n){cin >> var[i];} #define arrll(var, n) vll var(n); rep(i,n){cin >> var[i];} #define arrst(var, n) vstr var(n); rep(i,n){cin >> var[i];} #define ALL(var) (var).begin(), (var).end() #define sortall(var) sort(ALL(var)) #define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end()); #define prt(var) cout << (var) << "\n" #define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n" #define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n" #define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n" #define prtfill(n, var) cout << setw(n) << setfill('0') << (var); #define prtall(v) rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");} template <typename T> bool chmax(T &a, const T& b){if(a<b){a=b; return true;} return false;} template <typename T> bool chmin(T &a, const T& b){if(a>b){a=b; return true;} return false;} ll gcd(ll a, ll b){ return b?gcd(b,a%b):a;} ll lcm(ll a, ll b){ return a/gcd(a,b)*b;} //---------------------------------------------------------------- int main(void) { int n; cin >> n; vint a(n+1, 1); for(int i=1; i<=n; i++){ for(int j=2; i*j<=n; j++){ if(a[i]==a[i*j]) a[i*j]++; } } // for(int i=1; i<n+1; i++){ // for(int j=1; j<n+1; j++){ // if(j%i==0 && j!=i && a[j]==a[i]) prt2(i, j); // } // } //prtall(a); vint ans; rep(i,n){ ans.push_back(a[i+1]); } prtall(ans); }
/*Be patient && calm! All our dreams can come true, if we have the courage to pursue them. I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times. Do not pray for an easy life, pray for the strength to endure a difficult one. When something is important enough, you do it even if the odds are not in your favor. First, solve the problem. Then, write the code. Fix the cause, not the symptom. Simplicity is the soul of efficiency. Make it work, make it right, make it fast. No matter how hard it gets, NEVER GIVE UP*/ #include <bits/stdc++.h> using namespace std; #define ll long long #define MOD 1000000007 string longDivision(string number, int divisor) { // As result can be very large store it in string string ans; // Find prefix of number that is larger // than divisor. int idx = 0; int temp = number[idx] - '0'; while (temp < divisor) temp = temp * 10 + (number[++idx] - '0'); // Repeatedly divide divisor with temp. After // every division, update temp to include one // more digit. while (number.size() > idx) { // Store result in answer i.e. temp / divisor ans += (temp / divisor) + '0'; // Take next digit of number temp = (temp % divisor) * 10 + number[++idx] - '0'; } // If divisor is greater than number if (ans.length() == 0) return "0"; // else return ans return ans; } bool mul200(string n){ int l=n.length(); if(l<3){ // cout<<"r\n"; return false; } if(n[l-1]!='0'||n[l-2]!='0'){ // cout<<"y\n"; return false; } if(n[l-3]&1){ // cout<<"u\n"; return false; } return true; } void solve(){ ll n; cin>>n; ll a[n]; map<ll,ll> mp; ll sum=0; for(int i=0;i<n;i++){ cin>>a[i]; sum+=mp[a[i]%200]; mp[a[i]%200]++; } cout<<sum<<"\n"; } int main(){ // ios::sync_with_stdio(false); // cout.tie(0); // cin.tie(NULL); int t; // cin>>t; t=1; while(t--) { // int n; // cin>>n; solve(); } }
#include <iostream> #include <bits/stdc++.h> #define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define mod 1000000007 #define ll long long #define pb push_back #define mp make_pair #define gcd __gcd #define F first #define S second #define PI 3.14159265358979323 using namespace std; vector<ll>gr[200010]; bool vis[200010]={0}; ll ans=0; void dfs(ll s){ vis[s]=0; for(auto it:gr[s]) if(vis[it]) dfs(it); } int main() { fio; int TC=1; //cin>>TC; while(TC--){ ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++) { cin>>a[i]; if(!vis[a[i]]){ vis[a[i]]=true; ans++; } } for(ll i=0;i<n;i++){ if(a[i]!=a[n-i-1]){ gr[a[i]].pb(a[n-i-1]); gr[a[n-i-1]].pb(a[i]); } } for(ll i=0;i<200001;i++){ if(vis[i]){ dfs(i); ans--; } } cout<<ans<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int const double pi = 3.14159265358979; const int inf = 1e13; #define pii pair<int,int> #define endl "\n" #define dtor(deg) (((deg)/360)*2*pi) #define all(a) a.begin(),a.end() #define overload(_1,_2,_3,_4,name,...) name #define _rep1(n) for(int i = 0; i < (n); i++) #define _rep2(i,n) for(int i = 0; i < (n); i++) #define _rep3(i,a,b) for(int i = (a); i < (b); i++) #define _rep4(i,a,b,c) for(int i = (a); i < (b); i += (c)) #define rep(...) overload(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__) #define _rrep1(n) for(int i = (n) - 1; i >= 0; i--) #define _rrep2(i,n) for(int i = (n) - 1; i >= 0; i--) #define _rrep3(i,a,b) for(int i = (b) - 1; i >= (a); i--) #define rrep(...) overload(__VA_ARGS__,_null,_rrep3,_rrep2,_rrep1)(__VA_ARGS__) #define vec(type,name,...) vector<type> name(__VA_ARGS__) #define vv(type,name,size,...) vector<vector<type>> name(size,vector<type>(__VA_ARGS__)) #define ForEach(a,b) for_each(a.begin(),a.end(),b) struct Edge { int to, cost; Edge(int to, int cost) : to(to), cost(cost) {} }; using Graph = vector<vector<Edge>>; template <class T> bool chmin(T& a, T b){ if(a > b){ a = b; return 1; } return 0; } template <class T> bool chmax(T& a, T b){ if(a < b){ a = b; return 1; } return 0; } void Main(){ int n, t, ans = 0; cin >> n >> t; vector<int> a, b, sum_a, sum_b; rep(n){ int input; cin >> input; if(i % 2) a.push_back(input); else b.push_back(input); } rep(bit, 1 << a.size()){ int temp = 0; rep(a.size()) if(bit >> i & 1) temp += a[i]; sum_a.push_back(temp); } rep(bit, 1 << b.size()){ int temp = 0; rep(b.size()) if(bit >> i & 1) temp += b[i]; sum_b.push_back(temp); } sort(all(sum_a)); for(auto x : sum_b){ int temp = t - x; int ind = upper_bound(all(sum_a), temp) - sum_a.begin() - 1; if(ind == -1) continue; chmax(ans, sum_a[ind] + x); } cout << ans << endl; } signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(10) << fixed; //int n; cin >> n; rep(n) Main(); }
#include<iostream> #include<math.h> #include<vector> #include<algorithm> #include<string> #include<map> #define rep(i,n) for(int i=0;i<(n);i++) bool is_prime(int x){ if(x<=1) return false; for(int i=2;i*i<x;i++){ if(x%i==0) return false;} return true;} template<class T>void chmin(T& a,T b){ if(a>b){ a=b; } } using namespace std; int main(){ int n; cin>>n; double x[n],y[n]; rep(i,n) cin>>x[i]>>y[i]; int ans=0; rep(i,n)rep(j,n){ if(i==j)continue; if((y[i]-y[j])/(x[i]-x[j])>=-1 and (y[i]-y[j])/(x[i]-x[j])<=1){ ans++; } } cout << ans/2 << endl; }
#include <bits/stdc++.h> #define pb push_back typedef long long ll; using namespace std; int solve(int n){ if (n == 0) return 0; vector <int> digits; while (n){ digits.pb(n % 10); n /= 10; } sort(digits.begin(), digits.end()); int small = 0; int i = 0; n = digits.size(); while (!digits[i] and i < n) ++i; while (i < n){ small = small * 10 + digits[i++]; } reverse(digits.begin(), digits.end()); int big = 0; for (int a : digits) big = big * 10 + a; return big - small; } int main() { int n, k; cin >> n >> k; int ans[k + 1]; ans[0] = n; for (int i = 1; i <= k; ++i) ans[i] = solve(ans[i - 1]); cout << ans[k] << endl; }
//xtwakioi! xtwddYnoi(双重含义)! #include <bits/stdc++.h> #define ri register #define int long long #define E (n+1) #define mk make_pair using namespace std; inline int read() { int s=0, w=1; ri char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w; } signed main() { int a,b; a=read(), b=read(); printf("%lld %lld\n",(a+b)/2,a-(a+b)/2); return 0; }
#include<bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x){ x=0;char ch=getchar();bool f=false; while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } template <typename T> inline void write(T x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10^48); return ; } #define ll long long const int N=2e5+5,M=1e8; int n,m,q; ll a[N],b[N]; struct SGTree{ int cur,root; ll sum[N*80]; int num[N*80],ls[N*80],rs[N*80]; void Modify(int &x,int l,int r,int pos,int v,int tp){ if(!x) x=++cur; int mid=l+r>>1;sum[x]+=v*tp;num[x]+=tp; if(l==r) return ; if(pos<=mid) Modify(ls[x],l,mid,pos,v,tp); else Modify(rs[x],mid+1,r,pos,v,tp); return ; } ll QuerySum(int x,int l,int r,int ql,int qr){ if(ql>qr||!x) return 0; if(ql<=l&&r<=qr) return sum[x]; int mid=l+r>>1;ll res=0; if(ql<=mid) res+=QuerySum(ls[x],l,mid,ql,qr); if(qr>mid) res+=QuerySum(rs[x],mid+1,r,ql,qr); return res; } int QueryNum(int x,int l,int r,int ql,int qr){ if(ql>qr||!x) return 0; if(ql<=l&&r<=qr) return num[x]; int mid=l+r>>1,res=0; if(ql<=mid) res+=QueryNum(ls[x],l,mid,ql,qr); if(qr>mid) res+=QueryNum(rs[x],mid+1,r,ql,qr); return res; } }t1,t2; ll Ans; int main(){ read(n),read(m),read(q); for(int i=1;i<=n;i++) t1.Modify(t1.root,0,M,a[i],a[i],1); for(int i=1;i<=m;i++) t2.Modify(t2.root,0,M,b[i],b[i],1); // for(int i=1;i<=n;i++){ // Ans+=1ll*t2.QueryNum(t2.root,0,M,1,a[i])*a[i]; // Ans+=1ll*t2.QuerySum(t2.root,0,M,a[i]+1,M); // } for(int i=1;i<=q;i++){ int op,pos;ll v; read(op),read(pos),read(v); if(op==1){ Ans-=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos]; Ans-=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M); t1.Modify(t1.root,0,M,a[pos],a[pos],-1); a[pos]=v; Ans+=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos]; Ans+=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M); t1.Modify(t1.root,0,M,a[pos],a[pos],1); } else{ Ans-=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos]; Ans-=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M); t2.Modify(t2.root,0,M,b[pos],b[pos],-1); b[pos]=v; Ans+=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos]; Ans+=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M); t2.Modify(t2.root,0,M,b[pos],b[pos],1); } write(Ans),putchar('\n'); } return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #ifdef local #define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n" #define pary(a...) danb(#a, a) #define debug(a...) qqbx(#a, a) template <typename ...T> void qqbx(const char *s, T ...a) { int cnt = sizeof...(T); ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n"))); } template <typename T> void danb(const char *s, T L, T R) { std::cerr << "\033[1;32m[ " << s << " ] = [ "; for (int f = 0; L != R; ++L) std::cerr << (f++ ? ", " : "") << *L; std::cerr << " ]\033[0m\n"; } #else #define debug(...) ((void)0) #define safe ((void)0) #define pary(...) ((void)0) #endif // local #define all(v) begin(v),end(v) #define pb emplace_back #define get_pos(u,x) int(lower_bound(all(u),x)-begin(u)) using namespace std; using ll = int_fast64_t; const int inf = 1e9; const int mod = 1000000007; const ll INF = 1e18; const int maxn = 200025; struct Fenwick { ll b[maxn]; void add(int p, int d) { for (++p; p < maxn; p += p & -p) b[p] += d; } ll query(int p) { ll r = 0; for (++p; p > 0; p -= p & -p) r += b[p]; return r; } } cnt_a, cnt_b, sum_a, sum_b; signed main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<tuple<int,int,int>> Qs(q); vector<int> u; u.pb(0); for (auto &[t, x, y]: Qs) cin >> t >> x >> y, u.pb(y), --x; sort(all(u)), u.erase(unique(all(u)), u.end()); vector<int> a(n), b(m); auto calc = [&](int val, Fenwick &cnt, Fenwick &sum) { int pos = get_pos(u, val); return val * cnt.query(pos - 1) + (sum.query(maxn - 2) - sum.query(pos - 1)); }; ll ans = 0; cnt_a.add(get_pos(u, 0), n); cnt_b.add(get_pos(u, 0), m); for (auto [t, x, y]: Qs) { if (t == 1) { cnt_a.add(get_pos(u, a[x]), -1); sum_a.add(get_pos(u, a[x]), -a[x]); ans -= calc(a[x], cnt_b, sum_b); a[x] = y; ans += calc(a[x], cnt_b, sum_b); cnt_a.add(get_pos(u, a[x]), 1); sum_a.add(get_pos(u, a[x]), a[x]); } else { cnt_b.add(get_pos(u, b[x]), -1); sum_b.add(get_pos(u, b[x]), -b[x]); ans -= calc(b[x], cnt_a, sum_a); b[x] = y; ans += calc(b[x], cnt_a, sum_a); cnt_b.add(get_pos(u, b[x]), 1); sum_b.add(get_pos(u, b[x]), b[x]); } cout << ans << '\n'; } }
#include<bits/stdc++.h> using namespace std; const int N=2e5+11; typedef pair<int,int> PII; struct q{ int x,y,num; }a[N],c[8]; struct w{ int x,y,num; }b[N]; int st[8],number[200]; unordered_map<int,int>m; bool cmp1( q a, q b) { return a.x<b.x; } bool cmp2( w a, w b) { return a.y<b.y; } int main() { int n; cin>>n; int i,j; for(i=0;i<n;i++) { int x1,y1; scanf("%d%d",&x1,&y1); a[i].x=b[i].x=x1; a[i].y=b[i].y=y1; a[i].num=b[i].num=i; } sort(a,a+n,cmp1); sort(b,b+n,cmp2); c[0]=a[0]; c[1]=a[1]; c[2]=a[n-2]; c[3]=a[n-1]; c[4]={b[0].x,b[0].y,b[0].num}; c[5]={b[1].x,b[1].y,b[1].num}; c[6]={b[n-2].x,b[n-2].y,b[n-2].num}; c[7]={b[n-1].x,b[n-1].y,b[n-1].num}; int idx=0,pos=0; // for(i=0;i<8;i++) // { // printf("%d %d\n",c[i].x,c[i].y); // } for(i=0;i<8;i++) { if(m.count(c[i].num)>=1)st[i]++; m[c[i].num]++; } for(i=0;i<8;i++) { if(st[i])idx++; else c[pos++]=c[i]; } idx=8-idx; pos=0; sort(c,c+idx,cmp1); for(i=0;i<idx;i++) { for(j=i+1;j<idx;j++) { number[pos++]=max(abs(c[i].x-c[j].x),abs(c[i].y-c[j].y)); } } // for(i=0;i<idx;i++) // { // printf("%d %d\n",c[i].x,c[i].y); // } sort(number,number+pos); // for(i=0;i<pos;i++) // { // printf("%d \n",number[i]); // } cout<<number[pos-2]; return 0; }
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #include <bits/stdc++.h> #include<set> #include <array> using namespace std; #define ll long long #define endl '\n' #define mod 1000000007 #define pb push_back #define ff first #define ss second #define con continue #define ub upper_bound #define lb lower_bound #define si(x) int(x.size()) #define sum_all(a) ( accumulate ((a).begin(), (a).end(), 0ll)) #define mine(a) (*min_element((a).begin(), (a).end())) #define maxe(a) (*max_element((a).begin(), (a).end())) #define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin()) #define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin()) #define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) const double pi = 2 * acos(0.0); const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, -1, 0, 1 }; const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 }; const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 }; ll min(ll a,ll b) { if(a<b)return a; return b; } ll max(ll a,ll b) { if(a>b)return a; return b; } ll ceil1(ll a,ll b) { return(a+b-1)/b; } void read(vector<ll> & arr) { for(ll i=0;i<si(arr);i++) cin >> arr[i]; } void read_graph(vector<vector<ll>>& g, ll m) { while(m--) { ll x,y; cin >> x>> y ; x--,y--; g[x].pb(y); g[y].pb(x); } } int lim=1e5 +13; vector<bool> col; vector<int> c; vector<vector<int>> g; int cnt; vector<int> ans; void dfs(int r,int p=-1) { for(auto a:g[r]) { if(a==p) continue; if(!col[c[a]]) { cnt++; ans.pb(a); } bool old= col[c[a]]; col[c[a]]=true; dfs(a,r); col[c[a]]=old; } } void solve() { int n ; cin >> n; c=vector<int>(n); g=vector<vector<int>>(n); for(int i=0;i<n;i++) cin >> c[i]; for(int i=0;i<n-1;i++) { int x,y; cin >>x >> y ; x--,y--; g[x].pb(y); g[y].pb(x); } col=vector<bool>(lim,false); col[c[0]]=true; cnt=0; ans.clear(); ans.pb(0); dfs(0); sort(ans.begin(),ans.end()); for(int i=0;i<si(ans);i++) cout << 1+ans[i] << endl; // cout << cnt << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif //int t; cin >> t; while (t--) solve(); }