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 &lt; j ≤ n ) (1≤i&lt;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<bits/stdc++.h>
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<len)a.a[i+1][i]=max(0,(i+1)*(n+i+1-2*len));
    }
    while(m)
    {
        if(m&1)res=cal(a,res);
        a=cal(a,a);
        m/=2;
    }
    d=cal(d,res);
    ll ans=0;
    for(int i=0;i<=len;i++)(ans+=d.a[0][i])%=MOD;
    return d.a[0][len]*POW(ans,MOD-2)%MOD;
}
int main()
{
    int a[101];
    cin>>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<<POW(d)<<endl;
    return 0;
}

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值