"Explanations" Just A String

topic

The original title

click here

Simple meaning of the questions

After a valid string is now defined to meet and break up any combination thereof capable of forming a palindromic sequence.

You \ (m \) kinds of letters, Q random configuration of length \ (n-\) number of legal string substring desired .

For the desired answer \ (E \) , it is now required output \ (E \ cdot m ^ n \ pmod {1000000007} \) values.

Ideas and Analysis

First, the solution was found weird output requirements - required output \ (E \ n \ cdot m ^ pmod {1000000007} \) values.

Analyze:

If we have \ (K \) legitimate substring, then \ (E = \ FRAC {K} {m ^ n-} \) , and the answer is \ (E \ cdot m ^ m = \ frac {k} {m } ^ n-n-m ^ K = \) .

In other words, questions asked:

With \ (m \) the length of the character types is configured \ (n-\) string substring legitimate number, its modulus (9 + 10 ^ 7 \) \ output later.

Consider enumeration length of the substring \ (K \) , how to construct Reflection generating function.


  • For \ (2 \ nmid k \) conditions.

Is constant and there is only one character occurs odd number of times, the remaining characters occurs even number of times, then the parent function is to
\ [\ left (\ frac { e ^ x + e ^ {- x}} {2} \ right) ^ {m-1} \ cdot
\ frac {e ^ x + e ^ {- x}} {2} \] consider choosing \ (m \) characters in \ (1 \) a program number \ ( C_m ^. 1 \) , then the true master function
\ [C_m ^ 1 \ cdot \ left (\ frac {e ^ x + e ^ {- x}} {2} \ right) ^ {m-1} \ cdot \ frac {e ^ x + e ^ {- x}} {2} = m \ cdot \ left (\ frac {e ^ x + e ^ {- x}} {2} \ right) ^ {m-1 } \ cdot \ frac {e ^
x + e ^ {- x}} {2} \] binomial expansion, available
\ [m \ cdot \ left ( \ frac {e ^ x + e ^ {- x} } {2} \ right) ^ {m-1} \ cdot \ frac {e ^ x + e ^ {- x}} {2} = m \ cdot 2 ^ {- m} \ cdot \ left (\ sum_ { i = 0} ^ {m-
1} C_ {m-1} ^ ie ^ {(2i-m + 2) x} -e ^ {(2i-m) x} \ right) \] the \ (e ^ K \) reduction, can be obtained the original formula
\ [m \ cdot 2 ^ { - m} \ sum_ {i = 0} ^ {m-1} C_ {m-1} ^ i \ left (\ sum_ {j = 0} ^ \ infty \ frac {
(2i-m + 2) ^ j- (2i-m) ^ j} {j!} x ^ j \ right) \] then, the first\ (K \) coefficients of terms \ (a_k \) equation it is
\ [a_k = m \ cdot 2 ^ {- m} \ cdot \ sum_ {i = 0} ^ {m-1} C_ {m- 1} ^ i \ frac {(
2i-m + 2) ^ k- (2i-m) ^ k} {k!} \] Since we have to multiply last full discharge, i.e. \ (K! \) , so \ (a_k \) inside \ (! K \) may be directly disappear, i.e.
\ [a_k = m \ cdot 2 ^ {- m} \ cdot \ sum_ {i = 0} ^ {m-1} C_ {m -1} ^ i [(2i- m + 2) ^ k- (2i-m) ^ k] \]

And we have to consider the length \ (k \) legal substring maximum length \ (n \) the number of strings that appear, and the rest of the \ (nk \) positions are taken lightly characters so we claim is the last
\ [a_k \ cdot m ^ { nk} \ cdot (nk + 1) \]
which is the length of an odd number.


  • For \ (2 \ mid k \) case.

In this case, all characters are even number of occurrences, the generating function is preferably configured as a
\ [\ left (\ frac { e ^ x + e ^ {- x}} {2} \ right) ^ m \]
Similarly, to expand vigorously, available
\ [2 ^ {- m} \ cdot \ left (\ sum_ {i = 0} ^ {m} C_m ^ i \ cdot e ^ {(2i-m) x } \ right) \]
the \ (e ^ {(2i- m) x} \) reduction, available
\ [2 ^ {- m} \ cdot \ sum_ {i = 0} ^ {m} C_m ^ i \ left (\ sum_ {j = 0
} ^ \ infty \ frac {(2i-m) ^ j} {j!} x ^ j \ right) \] Therefore, the \ (K \) coefficient \ (b_k \) is
\ [b_k = 2 ^ {-
m} \ sum_ {i = 0} ^ {m} C_m ^ i \ frac {(2i-m) ^ k} {! k} \] because we multiplied by \ (K ! \) , so \ (b_k \) in ! \ (K \) can eliminate, i.e.
\ [b_k = 2 ^ {- m} \ sum_ {i = 0} ^ {m} C_m ^ i (2i- m) ^ k \]
we claim last
\ [b_k \ cdot m ^ { nk} \ cdot (nk + 1) \]


Code

This is out of TLE code, but I do not want to change the ...

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

#include<cstdio>

#define rep(i,__l,__r) for(register int i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(register int i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair<int,int>
#define Endl putchar('\n')
// #define FILEOI
// #define int long long

#ifdef FILEOI
    #define MAXBUFFERSIZE 500000
    inline char fgetc(){
        static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
    }
    #undef MAXBUFFERSIZE
    #define cg (c=fgetc())
#else
    #define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=2000;
const int MOD=1e9+7;

int C[MAXN+5][MAXN+5];
int inv2,inv2_fac[MAXN+5];
int Pow[MAXN*2+5][MAXN+5];
int n,m,T;

inline int qkpow(int a,int n){
    if(n<=MAXN)return Pow[a+2000][n];
    int ret=1;
    for(;n>0;n>>=1,a=1ll*a*a%MOD)if(n&1)ret=1ll*ret*a%MOD;
    return ret;
}

inline void init(){
    rep(i,0,MAXN*2+2)rep(j,0,MAXN){
        if(j==0)Pow[i][j]=1;
        else Pow[i][j]=1ll*Pow[i][j-1]*(i-2000)%MOD;
    }
    inv2=qkpow(2,MOD-2);
    inv2_fac[0]=1;
    C[0][0]=1;
    rep(i,1,MAXN){
        inv2_fac[i]=1ll*inv2_fac[i-1]*inv2%MOD;
        C[i][0]=C[i][i]=1;
        rep(j,1,i){
            C[i][j]=C[i-1][j-1]+C[i-1][j];
            if(C[i][j]>MOD)C[i][j]-=MOD;
        }
    }
}

int ans1,ans2,ans;

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    init();
    qread(T);
    while(T--){ans=0;
        qread(n,m);
        rep(k,1,n){
            if(k&1){
                ans1=1ll*inv2_fac[m]*(n-k+1)*qkpow(m,n-k+1)%MOD,ans2=0;
                rep(i,0,m-1)ans2=(0ll+ans2+1ll*C[m-1][i]*(qkpow((i<<1)-m+2,k)-qkpow((i<<1)-m,k)))%MOD;
                ans1=1ll*ans1*ans2%MOD;
            }
            else{
                ans1=1ll*inv2_fac[m]*(n-k+1)*qkpow(m,n-k)%MOD,ans2=0;
                rep(i,0,m)ans2=(0ll+ans2+1ll*C[m][i]*qkpow((i<<1)-m,k))%MOD;
                ans1=1ll*ans1*ans2%MOD;
            }
            ans=(0ll+ans+1ll*ans1*2)%MOD;
            if(ans<0)ans+=MOD;
            else if(ans>MOD)ans-=MOD;
        }
        writc(ans,'\n');
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/MachineryCountry/p/12112446.html