算一下每種零售最少的錢就行,而後優先買2,零頭買1c++
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 100005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int Q,H,S,D,N; int64 ans; int main() { read(Q);read(H);read(S);read(D); read(N); H = min(2 * Q,H); S = min(2 * H,S); D = min(2 * S,D); ans = 1LL * (N / 2) * D + (N & 1) * S; out(ans);enter; }
對於兩個相同的字符,在這個字符之間的翻轉確定能取代以這兩個字符爲端點的翻轉spa
那麼若是認爲一個翻轉會造成一個字符串,那麼咱們刪掉左右兩端點相同的翻轉code
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 200005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int64 ans; int N,L,cnt[30]; char s[MAXN]; void Solve() { scanf("%s",s + 1); N = strlen(s + 1); ans = 1 + 1LL * N * (N - 1) / 2; for(int i = 1 ; i <= N ; ++i) { cnt[s[i] - 'a']++; } for(int i = 0 ; i <= 25 ; ++i) { ans -= 1LL * cnt[i] * (cnt[i] - 1) / 2; } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
以橫座標上升對y求一個最長上升子序列ip
設長度爲\(k\)字符串
若是\(k < min(x_2 - x_1,y_2 - y_1) + 1\)get
那麼我就能夠拐角k次,減小\((20 - 5\pi)k\)it
不然我只能拐角k - 1次,不得不走一個半圓,減小\((20 - 5\pi)(k - 1)\)加上\((10\pi - 20)\)class
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 200005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } const double PI = acos(-1.0); int64 x[MAXN],y[MAXN],id[MAXN]; int64 X1,X2,Y1,Y2,num[MAXN],val[MAXN]; int N,tot,c; void Solve() { read(X1);read(Y1);read(X2);read(Y2); read(N); for(int i = 1 ; i <= N ; ++i) { read(x[i]);read(y[i]); } if(X1 > X2) { swap(X1,X2);swap(Y1,Y2); } if(Y1 > Y2) { swap(Y1,Y2); for(int i = 1 ; i <= N ; ++i) { y[i] = Y1 + Y2 - y[i]; } } for(int i = 1 ; i <= N ; ++i) { id[i] = i; } sort(id + 1,id + N + 1,[](int a,int b){return y[a] < y[b];}); for(int i = 1 ; i <= N ; ++i) { int u = id[i]; if(y[u] >= Y1 && y[u] <= Y2 && x[u] >= X1 && x[u] <= X2) { num[++tot] = x[u]; } } c = 0; val[c] = -1; for(int i = 1 ; i <= tot ; ++i) { if(num[i] > val[c]) val[++c] = num[i]; else { int t = upper_bound(val + 1,val + c + 1,num[i]) - val; val[t] = num[i]; } } if(c < min(X2 - X1,Y2 - Y1) + 1) { double ans = 100.0 * (X2 - X1 + Y2 - Y1) - (20 - PI * 5) * c; printf("%.12lf\n",ans); } else { double ans = 100.0 * (X2 - X1 + Y2 - Y1) - (20 - PI * 5) * (c - 1) + (10 * PI - 20); printf("%.12lf\n",ans); } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
枚舉B的第一位和A的哪一位對齊date
而後對於A的每一位1,若是對着的B不是1,那麼咱們想辦法把它消成0,這找這個1最近的兩個1,這分別對應着兩個和B第一位對齊的位置,放在數軸上im
而後用twopoints,枚舉當左邊選擇數軸上的點在這的時候,右邊最遠選到哪,計算兩種走法,而後計算端點到目標點的距離
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 4005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int L[MAXN],R[MAXN],N; char a[MAXN],b[MAXN]; vector<int> v[MAXN]; int cnt[MAXN]; bool checkpos(int x) { if(x >= N) return false; for(auto t : v[x]) { cnt[t]--; } bool flag = 1; for(auto t : v[x]) if(!cnt[t]) flag = 0; for(auto t : v[x]) cnt[t]++; return flag; } void Solve() { scanf("%s%s",a,b); N = strlen(a); int ca = 0,cb = 0; for(int i = 0 ; i < N ; ++i) { if(a[i] == '1') ++ca; if(b[i] == '1') ++cb; } if(!cb) { if(!ca) puts("0"); else puts("-1"); return; } for(int i = 0 ; i < N ; ++i) { if(a[i] == '1') { int t = i; int cnt = 0; while(b[t] == '0') { t = (t - 1 + N) % N; ++cnt; } L[i] = cnt % N; t = i,cnt = 0; while(b[t] == '0') { t = (t + 1) % N; ++cnt; } R[i] = (N - cnt) % N; } } for(int i = 0 ; i < N ; ++i) a[i + N] = a[i]; int ans = 1e9; for(int i = 0 ; i < N ; ++i) { int now = 0; for(int j = 0 ; j < N ; ++j) v[j].clear(); memset(cnt,0,sizeof(cnt)); for(int j = 0 ; j < N ; ++j) { if(b[j] != a[i + j]) { ++now; if(a[i + j] == '1') { int t = (i + j) % N; v[L[t]].pb(t);v[R[t]].pb(t); cnt[t] += 2; } } } int p = 0,q = 1; while(p < N) { while(checkpos(q)) { for(auto t : v[q]) cnt[t]--; ++q; } int tmp = p + 2 * (N - q); tmp += min(abs(p - i),N - abs(p - i)); ans = min(ans,tmp + now); tmp = N - q + 2 * p; tmp += min(abs(q - i),N - abs(q - i)); ans = min(ans,tmp + now); ++p; for(auto t : v[p]) cnt[t]++; } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
就是對於01 和 10這兩種序列配對,而後能夠插任意個11進去
假若有d個01,總共k個1
這個方案數就是\((\frac{1}{1!}x^{1} + \frac{1}{2!}x^{2}.... + \frac{1}{k!}x^{k})^d\)
而後記\(f(i)\)是係數,那麼方案數就是\(f(i) d!(k - d)!k!\),對於每一個i都累加一下
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 10005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } const int MOD = 998244353,MAXL = (1 << 16); char a[MAXN],b[MAXN]; int N,k,d,fac[MAXN],invfac[MAXN],W[MAXL]; int inc(int a,int b) { return a + b >= MOD ? a + b - MOD : a + b; } int mul(int a,int b) { return 1LL * a * b % MOD; } int C(int n,int m) { if(n < m) return 0; return mul(fac[n],mul(invfac[m],invfac[n - m])); } int fpow(int x,int c) { int res = 1,t = x; while(c) { if(c & 1) res = mul(res,t); t = mul(t,t); c >>= 1; } return res; } struct poly { vector<int> v; void limit(int n = -1) { if(n != -1) v.resize(n); while(v.size() > 1 && (v.back()) == 0) v.pop_back(); } friend void NTT(poly &f,int L,int on) { f.v.resize(L); for(int i = 1, j = L >> 1 ; i < L - 1 ; ++i) { if(i < j) swap(f.v[i],f.v[j]); int k = L >> 1; while(j >= k) { j -= k; k >>= 1; } j += k; } for(int h = 2 ; h <= L ; h <<= 1) { int wn = W[(MAXL + on * MAXL / h) % MAXL]; for(int k = 0 ; k < L ; k += h) { int w = 1; for(int j = k ; j < k + h / 2 ; ++j) { int u = f.v[j],v = mul(w,f.v[j + h / 2]); f.v[j] = inc(u,v); f.v[j + h / 2] = inc(u,MOD - v); w = mul(w,wn); } } } if(on == -1) { int invL = fpow(L,MOD - 2); for(int i = 0 ; i < L ; ++i) { f.v[i] = mul(f.v[i],invL); } } } friend poly operator * (poly a,poly b) { poly c;c.v.clear(); int s = a.v.size() + b.v.size(); int L = 1; while(L <= s) L <<= 1; NTT(a,L,1);NTT(b,L,1); for(int i = 0 ; i < L ; ++i) { c.v.pb(mul(a.v[i],b.v[i])); } NTT(c,L,-1); return c; } friend poly fpow(const poly &a,int c,int n) { poly res,t = a; res.v.clear();res.v.pb(1); while(c) { if(c & 1) { res = res * t; res.limit(n); } t = t * t;t.limit(n); c >>= 1; } return res; } }f; void Solve() { scanf("%s%s",a + 1,b + 1); N = strlen(a + 1); for(int i = 1 ; i <= N ; ++i) { if(a[i] == '1') { ++k; if(b[i] == '0') ++d; } } fac[0] = 1; for(int i = 1 ; i <= N ; ++i) { fac[i] = mul(fac[i - 1],i); } invfac[N] = fpow(fac[N],MOD - 2); for(int i = N - 1 ; i >= 0 ; --i) { invfac[i] = mul(invfac[i + 1],i + 1); } W[0] = 1;W[1] = fpow(3,(MOD - 1) / MAXL); for(int i = 2 ; i < MAXL ; ++i) W[i] = mul(W[i - 1],W[1]); f.v.clear(); f.v.resize(N); for(int i = 1 ; i <= k ; ++i) { f.v[i] = invfac[i]; } f = fpow(f,d,N); int ans = 0; for(int i = d ; i <= k ; ++i) { if(i > f.v.size() - 1) break; int t = mul(f.v[i],mul(fac[i],fac[d])); t = mul(t,C(k,i)); t = mul(t,mul(fac[k - d],fac[k - i])); ans = inc(ans,t); } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
原本是個組合數前綴和,可是根據遞推只須要改一項???
這座標算的也太難受了,不想說話了。。。
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 1000005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } const int MOD = 998244353; int N,M; int fac[MAXN],invfac[MAXN],f[MAXN],v[MAXN],h[MAXN],inv[MAXN]; int inc(int a,int b) { return a + b >= MOD ? a + b - MOD : a + b; } int mul(int a,int b) { return 1LL * a * b % MOD; } int C(int n,int m) { if(n < m) return 0; return mul(fac[n],mul(invfac[m],invfac[n - m])); } int way(int x1,int y1,int x2,int y2) { return C(x2 - x1 + y2 - y1,y2 - y1); } void update(int &x,int y) { x = inc(x,y); } int fpow(int x,int c) { int res = 1,t = x; while(c) { if(c & 1) res = mul(res,t); t = mul(t,t); c >>= 1; } return res; } void Solve() { read(N);read(M); fac[0] = 1; inv[1] = 1; for(int i = 1 ; i <= N + M ; ++i) { fac[i] = mul(fac[i - 1],i); if(i > 1) inv[i] = mul(inv[MOD % i],MOD - MOD / i); } invfac[N + M] = fpow(fac[N + M],MOD - 2); for(int i = N + M - 1 ; i >= 0 ; --i) { invfac[i] = mul(invfac[i + 1],i + 1); } if(M > N) h[0] = 1; if(N > M) v[0] = 1; for(int i = 1 ; i <= N + M ; ++i) { h[i] = h[i - 1]; int r = min(i - 1,N) - h[i - 1] + 1,c = i - 1 - r + 1; if(i <= N && M - (N - r) >= c + 1) ++h[i]; if(i > N && M - (N - r) <= c) --h[i]; v[i] = v[i - 1]; c = min(i - 1,M) - v[i - 1] + 1,r = i - 1 - c + 1; if(i <= M && N - (M - c) >= r + 1) ++v[i]; if(i > M && N - (M - c) <= r) --v[i]; } f[0] = mul(C(N + M, N),max(N,M)); int valh = 0,valv = 0; if(h[0]) valh = C(N + M - 1, M - 1); if(v[0]) valv = C(N + M - 1, N - 1); for(int i = 1 ; i < N + M ; ++i) { f[i] = f[i - 1]; if(h[i - 1]) f[i] = inc(f[i],MOD - valh); if(v[i - 1]) f[i] = inc(f[i],MOD - valv); if(!h[i - 1] && h[i]) valh = way(i,0,N,M - 1); if(!v[i - 1] && v[i]) valv = way(0,i,N - 1,M); int r1 = min(i - 1,N) - h[i - 1] + 1,c1 = i - 1 - r1; int r2 = min(i,N) - h[i] + 1,c2 = i - r2; if(h[i - 1]) { if(r2 > r1) { valh = inc(valh, MOD - mul(way(0,0,r1,c1),way(r1,c1 + 1,N,M - 1))); } else { if(r2 >= 1) valh = inc(valh,mul(way(0,0,r2 - 1,c2),way(r2,c2,N,M - 1))); } } c1 = min(i - 1,M) - v[i - 1] + 1,r1 = i - 1 - c1; c2 = min(i,M) - v[i] + 1,r2 = i - c2; if(v[i - 1]) { if(c2 > c1) { valv = inc(valv, MOD - mul(way(0,0,r1,c1),way(r1 + 1,c1,N - 1,M))); } else { if(c2 >= 1) valv = inc(valv,mul(way(0,0,r2,c2 - 1),way(r2,c2,N - 1,M))); } } } int ans = 0; for(int i = 0 ; i < N + M ; ++i) { ans = inc(ans,mul(f[i],inv[N + M - i])); } ans = mul(ans,fpow(C(N + M,N),MOD - 2)); out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }