Codeforces-1151F:Sonya and Informatics(DP+矩阵快速幂)

F. Sonya and Informatics
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya’s favorite subject!) invented a task for her.

Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:

Two numbers i and j are chosen equiprobable such that ( 1 ≤ i < j ≤ n ) (1≤i<j≤n) (1i<jn).
The numbers in the i and j positions are swapped.
Sonya’s task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.

It can be shown that the desired probability is either 0 or it can be represented as P Q \frac PQ QP, where P and Q are coprime integers and Q ! = 0 Q!=0 Q!=0 ( m o d 1 0 9 + 7 ) (mod10^9+7) (mod109+7).

Input
The first line contains two integers n and k ( 2 ≤ n ≤ 100 , 1 ≤ k ≤ 1 0 9 ) (2≤n≤100,1≤k≤10^9) (2n100,1k109) — the length of the array a and the number of operations.

The second line contains n integers a 1 , a 2 , … , a n ( 0 ≤ a i ≤ 1 ) a_1,a_2,…,a_n (0≤a_i≤1) a1,a2,,an(0ai1) — the description of the array a.

Output
If the desired probability is 0, print 0, otherwise print the value P ⋅ Q − 1 ( m o d 1 0 9 + 7 ) P⋅Q^{−1} (mod10^9+7) PQ1(mod109+7), where P and Q are defined above.

Examples
input
3 2
0 1 0
output
333333336
input
5 1
1 1 1 0 0
output
0
input
6 4
1 0 0 1 1 0
output
968493834

思路:假设 a a a中总共有 l e n len len个0,且前 l e n len len个数里0的个数为 x x x,用 d [ i ] [ j ] d[i][j] d[i][j]表示经过 i i i次操作后, a a a中前 l e n len len个数里有 j j j个0的方案数,那么初始 d [ 0 ] [ x ] = 1 d[0][x]=1 d[0][x]=1

则最后 a a a不下降的方案数为 d [ k ] [ l e n ] d[k][len] d[k][len],则概率 a n s = d [ k ] [ l e n ] ∑ j = 0 l e n d [ k ] [ j ] ans=\frac{d[k][len]} {\sum_{j=0}^{len} d[k][j]} ans=j=0lend[k][j]d[k][len]

接下来就是求 d [ i ] [ j ] d[i][j] d[i][j]了。

d [ i ] [ j ] d[i][j] d[i][j],我们可以根据 j j j求出 j j j变化的方案数(简单的排列组合),从而推出 d [ i ] [ j − 1 ] , d [ i ] [ j ] , d [ i ] [ j + 1 ] d[i][j-1],d[i][j],d[i][j+1] d[i][j1],d[i][j],d[i][j+1]

于是可以利用矩阵快速幂加速。

#include
using namespace std;
const int MAX=1e5+10;
const int MOD=1e9+7;
typedef long long ll;
struct lenka{int a[101][101];};
int len,n,m;
ll POW(ll x,ll R)
{
    ll res=1;
    while(R)
    {
        if(R&1)res=res*x%MOD;
        x=x*x%MOD;
        R/=2;
    }
    return res;
}
lenka cal(const lenka&A,const lenka&B)
{
    lenka C;
    memset(C.a,0,sizeof C.a);
    for(int i=0;i<=len;i++)
    for(int j=0;j<=len;j++)
    for(int k=0;k<=len;k++)
    {
        C.a[i][j]+=1ll*A.a[i][k]*B.a[k][j]%MOD;
        C.a[i][j]%=MOD;
    }
    return C;
}
ll POW(lenka& d)
{
    lenka a,res;
    memset(a.a,0,sizeof a.a);
    memset(res.a,0,sizeof res.a);
    for(int i=0;i<=len;i++)res.a[i][i]=1;
    for(int i=0;i<=len;i++)
    {
        a.a[i][i]=max(0,n*(n-1)/2-i*(n+i-2*len)-(len-i)*(len-i));
        if(i)a.a[i-1][i]=(len-i+1)*(len-i+1);
        if(i>n>>m;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)len+=a[i]^1;
    int cnt=0;
    for(int i=1;i<=len;i++)cnt+=a[i]^1;
    lenka d;
    memset(d.a,0,sizeof d.a);
    d.a[0][cnt]=1;
    cout<

你可能感兴趣的:(DP,数学-矩阵快速幂)