Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

the solution of Gym102222I

Revision en5, by tarjen, 2022-05-01 13:04:38

102222I - Bubble Sort

给定n,k询问有多少n的排列满足在进行k轮冒泡排序(这里指的是一个for交换过去)之后形成的排列的最长上升子序列为n-1(即只有一个数不有序)

n,k=50(其实只有O(n))

这傻逼题想了一年,艹

非常显然的是,如果一个排列中的数a_i前面有b_i(b_i0)个数比a_i大,那么在一轮冒泡之后,a_i前面比a_i大的数会少1(b_i-1),而a_i也会向前前进一格(这个没啥用)

我们考虑通过b_i来解决问题,我们先证明一下对于每个合法的b(b_ii),有且只有一个a与它相对应

我们考虑怎么通过b来推出a

非常显然a_n可以直接通过b_n求出,求出a_n之后a_{n-1}也同样求出,所以只要b合法就有一个a排列求出了,证毕

也就是说,对于每一次冒泡,每一个大于0b_i减一,等于0则不变

考虑什么样的终态合法:

  1. 所有b_i=0(即有序)

  2. 中间有一段b_i=1,其他的都是0 比如说0 0 1 1 0(1 4 2 3 5)

  3. 中间有一个b_i!=0,其他的都是0 比如说0 0 2 0 0(2 3 1 4 5)

这个显然是dp过去就可以了,设3个状态(前面全m,处于中间段,后面全m)

memset(dp,0,sizeof(dp));
    int ans2=0;
    cin>>n>>m>>mod;
    dp[0][0]=1;
    for(int i=1;i<=n;i++){
        if(i<=m+1){
            (dp[i][0]+=dp[i-1][0]*(i))%=mod;
        }
        else{
            (dp[i][0]+=dp[i-1][0]*(m+1))%=mod;
            (dp[i][1]+=dp[i-1][0]+dp[i-1][1])%=mod;
            (dp[i][2]+=dp[i-1][1]*(m+1)+dp[i-1][2]*(m+1)+dp[i-1][0]*max(0ll,i-1-(m+1)))%=mod;
        }
    }
    int ans=0;
    for(int i=0;i<3;i++)ans+=dp[n][i];
    cout<<"Case #"<<ts<<": "<<ans%mod<<"\n";

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en32 English tarjen 2022-05-02 07:22:04 1 Tiny change: 'ously,...)\n\nSo we ' -> 'ously,...).\n\nSo we '
en31 English tarjen 2022-05-02 07:21:35 6 Tiny change: 'stimulate ,use the concl' -> 'stimulate by the concl'
en30 English tarjen 2022-05-02 07:20:31 86
en29 English tarjen 2022-05-01 14:50:17 8
en28 English tarjen 2022-05-01 14:47:45 4 Tiny change: 'es is ok\n```\n\n ' -> 'es is ok\n\n\n```\n\n '
en27 English tarjen 2022-05-01 14:46:00 13
en26 English tarjen 2022-05-01 14:45:26 14
en25 English tarjen 2022-05-01 14:12:32 2 Tiny change: 'on $$ k<i and Ak>Ai $$\' -> 'on $$ k<i ~and~ Ak>Ai $$\'
en24 English tarjen 2022-05-01 14:11:53 4 Tiny change: 'on $$ k<i & Ak>Ai $$\' -> 'on $$ k<i and Ak>Ai $$\'
en23 English tarjen 2022-05-01 14:11:30 2 Tiny change: 'ondition $ k<i & Ak>Ai $\n\nObvio' -> 'ondition $$ k<i & Ak>Ai $$\n\nObvio'
en22 English tarjen 2022-05-01 14:09:58 2 Tiny change: 'ion $ k<i ~ Ak>Ai $\n' -> 'ion $ k<i & Ak>Ai $\n'
en21 English tarjen 2022-05-01 14:09:31 5 Tiny change: 'tion $ k<i&&Ak>Ai $\n\' -> 'tion $ k<i ~ Ak>Ai $\n\'
en20 English tarjen 2022-05-01 14:07:28 3
en19 English tarjen 2022-05-01 14:06:52 100
en18 English tarjen 2022-05-01 14:05:06 4 Tiny change: '`Bn`, and `A n-1` is also e' -> '`Bn`, and $A n-1$ is also e'
en17 English tarjen 2022-05-01 13:48:05 135
en16 English tarjen 2022-05-01 13:38:40 39
en15 English tarjen 2022-05-01 13:37:35 26 Tiny change: ' bubbling,the number of k`Ak>Ai&&k>i` will minus' -> ' bubbling,`Bi`will minus'
en14 English tarjen 2022-05-01 13:35:57 47
en13 English tarjen 2022-05-01 13:32:50 15
en12 English tarjen 2022-05-01 13:28:08 2 Tiny change: 'm:102222I][contest:1' -> 'm:102222I]\n[contest:1'
en11 English tarjen 2022-05-01 13:27:34 16 Tiny change: 'm:102222I]\n\n\nObv' -> 'm:102222I][contest:102222]\n\n\nObv'
en10 English tarjen 2022-05-01 13:25:59 0 (published)
en9 English tarjen 2022-05-01 13:25:32 208
en8 English tarjen 2022-05-01 13:21:39 126
en7 English tarjen 2022-05-01 13:16:46 1166
en6 English tarjen 2022-05-01 13:08:15 192
en5 English tarjen 2022-05-01 13:04:38 6
en4 English tarjen 2022-05-01 13:04:07 28 Tiny change: '\n\n[problem' -> '\n[problem'
en3 English tarjen 2022-05-01 13:02:31 104
en2 English tarjen 2022-05-01 13:01:06 30 Tiny change: '[contesthttpscodeforces.comgym102222]\n\n给定`n,' -> '[problem:102222I]\n\n给定`n,'
en1 English tarjen 2022-05-01 13:00:17 1193 Initial revision (saved to drafts)