Topic Portal
analysis:
This question is immortal, we give low-equipped version of the solution and high version 2333 solution
Low version:
First, we know that such a formula. . . (To prove high version)
When a string S wherein S [1, i] = S [n - i + 1, n] when the called S [1, i] as a border of S
Ans [n] = sigma (S [1, i] is the S border) m ^ i
Ok. . .
Once you have this, we can kmp or hash solved
However, hash to get the answer can only handle S, and the answer can be done kmp handle all S prefix
Here with kmp (hash believe very simple (fog))
So we set f [i] is the i-th bit of recursion to answer
We turn first to the array fail
Then the border of the set S [1, i] is the S [1, i] itself plus S [1, fail [i]] is set border
So come recursive
f [ i ] = f [ fail [ i ] ] + m ^ i
N is the answer to the count
Good offices version:
First of all we need to know the expression builder function:
F (i) = sigma (i = 0 ... + ∞) Ai * x ^ i
We define the probability generating function generating function from which the probability of occurrence of the cost for the event i is Ai:
Then we can know:
F(1) = 1
When x is equivalent to taking 1, F (1) represents the probability and in all cases with a value of 1 is obviously correct, is the event itself
Then we consider the expected cost of E (x)
E (x) = sigma (i = 0 ... + ∞) Ai * i
Because when i do contribution is zero, so i can start from 1
Then there is an interesting thing, we have F (x) the derivative
F (x) = sigma (i = 1 ... + ∞) Ai * i * x ^ (i - 1)
We then take the F '(1) = sigma (i = 1 ... + ∞) Ai * i
And E (x) A comparison, we were surprised to find that:
E(x) = F'(1)
Is this a coincidence? Or some kind of inevitable?
This also explains the probability and expectations are necessarily linked
So we get to the point
Provided the function F (x) denotes the probability of the end of the length of the i, G (x) represents the length to the probability that i is not over
Then we get this equation:
F(x) + G(x) = 1 + G(x) * x
Into every consideration, in fact, the whole recursive process, you are i - the probability of a no end, is that you end position and i did not end in 'i' and the probability
This formula referred to as type 1
Then we consider solving. . .
Force plus (1 / mx) we go G (x) ^ n will be the end of the string, the string may be ended prematurely, however, because there might string before the border S
We enumerate the length of the border
First define ai = 0 or 1 represents S [1, i] whether the border S
So you can get this formula. . .
G(x) * ( 1/m * x) ^ n = sigma(i=1...n) ai * F(x) * (1/m * x) ^ ( n - i )
Hard to say this formula, but the sense of (hu) of (luan) analyze very correct 2333
Only Darknesses simple-minded, I do not know about
This formula referred to as type 2
Then we push large ♂ formula
First, a derivative of formula
F'(x) + G'(x) = G(x) + G'(x) * x
Requirements F '(1) Oh. . .
Direct access to it 2333
Then surprised to find
F'(1)=G(1)
Then take a second try
G (1) * (1 / m) * n = sigma (i = 1 ... n) and F * (1) * (1 / m) * (n - i)
Since F (1) = 1, then we have (1 / m) ^ n last addition
G (1) = sigma (i = 1 ... n) and i * m *
Therefore Ans [n] = E (x) = F '(1) = G (1) = sigma (S [1, i] to the border S) m ^ i
Get a permit. . .
Ha ha ha. . .
Generating function really immortal. . .
Mathematics too dishes 2333
#include<cstdio> #include<algorithm> #include<vector> #include<cmath> #include<cstring> #include<string> #include<map> #include<queue> #include<iostream> #define maxn 1000005 #define MOD 1000000007 using namespace std; inline int getint() { int num=0,flag=1;char c; while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1; while(c>='0'&&c<='9')num=num*10+c-48,c=getchar(); return num*flag; } int n,m; long long a[maxn],pw[maxn]; long long ans; long long fail[maxn],f[maxn]; int main() { m=getint(),n=getint(),f[0]=1; for(int i=1;i<=n;i++)a[i]=getint(),f[i]=f[i-1]*m%MOD; fail[0]=-1; for(int i=2;i<=n;i++) { for(int j=fail[i-1];j>=0;j=fail[j]) if(a[i]==a[j+1]){fail[i]=j+1,(f[i]+=f[j+1])%=MOD;break;} } for(int i=1;i<=n;i++)printf("%lld\n",f[i]); }
AC code can not be Oh, it outputs a direct answer all prefixes type S 2333
Code short, it proved really long 2333