若是是按下標說的話node
若是是偶數個c++
那麼是ui
\(N,N - 2,N - 4...1,3,5...N - 1\)spa
若是是奇數個code
\(N,N - 2,N - 4...2,4,6...N - 1\)字符串
#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 eps 1e-10 #define MAXN 200005 //#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 N; int a[MAXN]; void Solve() { read(N); for(int i = 1 ; i <= N ; ++i) read(a[i]); if(N & 1) { for(int i = N ; i >= 1 ; i -= 2) {out(a[i]);space;} for(int i = i = 2 ; i <= N ; i += 2) {out(a[i]);space;} enter; } else { for(int i = N ; i >= 1 ; i -= 2) {out(a[i]);space;} for(int i = 1 ; i <= N ; i += 2) {out(a[i]);space;} enter; } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
就至關於只有兩個位置相同,那麼即爲get
\(...1 .... 1....\)it
算全部位置不一樣的子序列class
若是這個序列只包括其中一個1,剩下的數不在兩個1之間,那麼這個序列就被重複計算了,很簡單的計數next
#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 eps 1e-10 #define MAXN 100005 //#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 = 1000000007; int N,a[MAXN],fac[MAXN],invfac[MAXN]; int pos[MAXN],p; 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 fpow(int x,int c) { int t = x,res = 1; while(c) { if(c & 1) res = mul(res,t); t = mul(t,t); c >>= 1; } return res; } int C(int n,int m) { if(n < m) return 0; return mul(fac[n],mul(invfac[m],invfac[n - m])); } void Solve() { read(N); for(int i = 1 ; i <= N + 1; ++i) read(a[i]); for(int i = 1 ; i <= N + 1; ++i) { if(pos[a[i]]) p = i; else pos[a[i]] = i; } fac[0] = 1; for(int i = 1 ; i <= N + 1 ; ++i) { fac[i] = mul(fac[i - 1],i); } invfac[N + 1] = fpow(fac[N + 1],MOD - 2); for(int i = N ; i >= 0 ; --i) { invfac[i] = mul(invfac[i + 1],i + 1); } for(int i = 1 ; i <= N + 1 ; ++i) { int res = C(N + 1,i); res = inc(res,MOD - C(pos[a[p]] - 1 + N + 1 - p,i - 1)); out(res);enter; } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
顯然若是不按第二個按鈕,只用第一種狀況增長,增長的方式不能改變,把每次增長的段畫在數軸上,會發現每次增長對於一段區間至關於若是選了x當喜好按鈕,那麼費用會減小y,這個y在數軸上是個等差數列,用線段樹維護一下就好
#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 eps 1e-10 #define MAXN 100005 //#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 N,M; int a[MAXN]; int64 ans,res; struct node { int L,R; int64 a,d; }tr[MAXN * 4]; void build(int u,int L,int R) { tr[u].L = L;tr[u].R = R; tr[u].a = tr[u].d = 0; if(L == R) return; int mid = (L + R) >> 1; build(u << 1,L,mid); build(u << 1 | 1,mid + 1,R); } void addlz(int u,int64 a,int64 d) { tr[u].a += a;tr[u].d += d; } void pushdown(int u) { int l = tr[u].L,m = (tr[u].L + tr[u].R) >> 1,r = tr[u].R; addlz(u << 1,tr[u].a,tr[u].d); addlz(u << 1 | 1,tr[u].a + (m + 1 - l) * tr[u].d,tr[u].d); tr[u].a = tr[u].d = 0; } void Add(int u,int ql,int qr,int64 a,int64 d) { if(ql > qr) return; if(ql == tr[u].L && qr == tr[u].R) {addlz(u,a,d);return;} int mid = (tr[u].L + tr[u].R) >> 1; pushdown(u); if(qr <= mid) Add(u << 1,ql,qr,a,d); else if(ql > mid) Add(u << 1 | 1,ql,qr,a,d); else {Add(u << 1,ql,mid,a,d),Add(u << 1 | 1,mid + 1,qr,a + (mid + 1 - ql) * d,d);} } void Getans(int u) { if(tr[u].L == tr[u].R) {res = max(tr[u].a,res);return;} pushdown(u); Getans(u << 1);Getans(u << 1 | 1); } void Solve() { read(N);read(M); build(1,1,M); for(int i = 1 ; i <= N ; ++i) { read(a[i]); } for(int i = 2 ; i <= N ; ++i) { if(a[i] > a[i - 1]) { ans += a[i] - a[i - 1]; Add(1,a[i - 1] + 1,a[i],0,1); } else { ans += M - a[i - 1] + a[i]; Add(1,a[i - 1] + 1,M,0,1); Add(1,1,a[i],M - a[i - 1],1); } } Getans(1); out(ans - res);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
先把原始的字符串變成兩個相同的字符串的形式
設爲\(SS\),若是\(S\)最後一位的next是\(k\),那麼顯然要在後邊加\(S\)的後N-k和\(S\)的前N-k位
設\(S\)的後\(N - k\)位爲\(T\),且\(k\)最大,那麼
\(f(SS) = STST\)
設\(g(S) = ST\)
顯然\(f(SS) = g(S) + g(S)\)
咱們實際上只要求一個足夠長的\(g(S)\)就行
\(g(S) = ST\)
當\(|T|\)是\(|S|\)的約數,能夠獲得\(g(ST) = STT\)
若是\(|T|\)不是\(|S|\)的約數,那麼\(g(ST) = STS\)
而後第一種狀況\(g^{\infty}(S) = STTTTTTT....\)
第二種狀況\(g^{i + 2}(S) = g^{i + 1}(S) + g^{i}(S)\)
而後能夠直接模擬,從第二種狀況開始遞推,若是出現第一種狀況就能夠記錄退出,第二種狀況增加速度是指數級的,遞推100個左右就行
#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-10 //#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); } struct node { int64 c[26],len; node() {memset(c,0,sizeof(c));len = 0;} friend node operator + (const node &a,const node &b) { node r; r.len = a.len + b.len; for(int i = 0 ; i < 26 ; ++i) r.c[i] = a.c[i] + b.c[i]; return r; } friend node operator - (const node &a,const node &b) { node r; r.len = a.len - b.len; for(int i = 0 ; i < 26 ; ++i) r.c[i] = a.c[i] - b.c[i]; return r; } friend node operator * (const node &a,const int64 &d) { node r = a; for(int i = 0 ; i < 26 ; ++i) { r.c[i] = r.c[i] * d; } return r; } }g[MAXN]; char s[MAXN * 2],t[MAXN * 2]; int nxt[MAXN],N; int st,all; node Calc(int64 R) { if(R > g[all].len && st) { node res; res = g[st]; int64 t = (R - g[st].len) / g[st - 1].len; res = res + g[st - 1] * t; return res + Calc(R - g[st].len - g[st - 1].len * t); } else if(R <= g[1].len){ node res;res.len = R; for(int i = 1 ; i <= R ; ++i) { res.c[s[i] - 'a']++; } return res; } else { for(int i = all ; i >= 0 ; --i) { if(g[i].len <= R) { return g[i] + Calc(R - g[i].len); } } } } void Solve() { scanf("%s",s + 1); int64 r,l; read(l);read(r); N = strlen(s + 1); for(int i = 2 ; i <= N ; ++i) { int p = nxt[i - 1]; while(p && s[i] != s[p + 1]) p = nxt[p]; if(s[i] == s[p + 1]) nxt[i] = p + 1; else nxt[i] = 0; } int p = nxt[N]; while(p > (N - 1) / 2) p = nxt[p]; int L = N; for(int i = p + 1 ; i <= N - p ; ++i) { s[++L] = s[i]; } for(int i = 1 ; i <= L / 2; ++i) { g[1].c[s[i] - 'a']++; } g[1].len = L / 2; p = nxt[L / 2]; g[0].len = L / 2 - p; for(int i = 1 ; i <= L / 2 - p ; ++i) { t[i] = s[i]; g[0].c[t[i] - 'a']++; } for(int i = 2 ; i <= 100 ; ++i) { if(g[i - 1].len % (g[i - 1].len - g[i - 2].len) == 0) {all = i - 1;st = i - 1;break;} if(g[i - 1].len + g[i - 2].len <= 1e18) g[i] = g[i - 1] + g[i - 2]; else {all = i - 1;break;} } if(!all) all = 100; node ans = Calc(r) - Calc(l - 1); for(int i = 0 ; i < 26 ; ++i) { out(ans.c[i]);space; } enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }