【CF285E】Positions in Permutations

题目

刷水题涨信心

显然这是个广义容斥,我们现在算一下至少有\(i\)个完美数的方案数就好了

这\(1000\)的数据范围显然在暗示\(n^2\)的dp

我们注意到这个条件大概就是\(P_i=i-1\)或\(P_i=i+1\),于是我们可以想象成左右两边各\(n\)个点去完成一个一一匹配

设\(dp[i][j][k][p]\)表示左边第\(i\)个数已经匹配完了,一共形成了\(j\)对完美数,\(k\)表示右边对应的第\(i\)个位置的使用状态\(0/1\),\(p\)表示右边第\(i+1\)个数的使用状态

转移显然

代码

#include<cstdio>
#define re register
const int mod=1e9+7;
const int maxn=1005;
int n,m,tot;
int dp[maxn][maxn][2][2],ans[maxn];
int c[maxn][maxn],fac[maxn];
int main() {
    scanf("%d%d",&n,&m);fac[0]=1;
    for(re int i=0;i<=n;i++) c[i][0]=c[i][i]=1;
    for(re int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
    for(re int i=2;i<=n;i++)
        for(re int j=1;j<i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
    dp[1][0][0][0]=1,dp[1][1][0][1]=1;
    for(re int i=1;i<n;i++)
        for(re int j=0;j<=i;j++)
            for(re int k=0;k<2;k++)
                for(re int p=0;p<2;p++) {
                    if(!dp[i][j][k][p]) continue;
                    dp[i+1][j+1][p][1]=(dp[i+1][j+1][p][1]+dp[i][j][k][p])%mod;
                    if(!k) dp[i+1][j+1][p][0]=(dp[i+1][j+1][p][0]+dp[i][j][k][p])%mod;
                    dp[i+1][j][p][0]=(dp[i+1][j][p][0]+dp[i][j][k][p])%mod;
                }
    for(re int i=0;i<=n;i++)
        ans[i]=(dp[n][i][0][0]+dp[n][i][1][0])%mod;
    for(re int j=m;j<=n;j++)
        tot=(tot+1ll*((j-m)&1?-1:1)*c[j][m]*fac[n-j]%mod*ans[j]%mod)%mod;
    printf("%d\n",(tot+mod)%mod);
    return 0;
}

原文地址:https://www.cnblogs.com/asuldb/p/10926580.html

时间: 2024-08-30 14:57:59

【CF285E】Positions in Permutations的相关文章

【CF715E】Complete the Permutations 第一类斯特林数

题目大意 有两个排列 \(p,q\),其中有一些位置是空的. 你要补全这两个排列. 定义 \(s(p,q)\) 为 每次交换 \(p\) 中的两个数,让 \(p=q\) 的最小操作次数. 求 \(s(p,q)=0,1,2,\ldots,n-1\) 的方案数. \(n\leq 300\) 题解 考虑 \(s(p,q)\) 怎么求. 对于每一个 \(i\),连一条有向边 \(p_i\to q_i\).那么 \(s(p,q)\) 就是 \(n-\) 图中环的个数. 先把 \(p,q\) 对应的图建出来

【cdq分治】【CF1093E】 Intersection of Permutations

传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有两种: \(1~,~l1~,~r1~,~l2~,~r2\) 求\((\bigcup_{i=l1}^{r1} A_i)~\bigcap~(\bigcup_{i=l2}^{r2} B_i)\) \(2~,~x~,~y\) 交换 \(B_x,~B_y\) Input 第一行是两个正整数 \(n,m\) 下

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】Permutations

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. public class Solution { public ArrayList<ArrayList<Integer>> permute

【LeetCode】Permutations 解题报告

全排列问题.常用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. [题目] Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3

【题解】POJ2279 Mr.Young′s Picture Permutations dp

[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能dp. 然后你发现\(30^5\)开不下,但是你仔细观察由于它保证\(\sum < 30\)所以你只用开\(30^5 \div 5!\)就好了. 具体为什么我相信你会 //@winlere #include<iostream> #include<cstring> #include

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]

我只能说,珍爱生命,远离卡常数的题...感谢陈老师和蔡神,没有他们,,,我调一个星期都弄不出来,,,, 哈罗哈的大披萨 [淡蓝] 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 73            测试通过 : 9 描述 热风哈罗哈(三牌楼)店正在搞活动:他们将提供一个大披萨给第一个告诉他们以下信息的人:一次购买任一种披萨,哪种单位面积的价格最低.问题初步想一想,好像是这么解决:“对于每个披萨计算平均

【BZOJ4099】Trapped in the Haybales STL

[BZOJ4099]Trapped in the Haybales Description Farmer John has received a shipment of N large hay bales (1≤N≤100,000), and placed them at various locations along the road leading to his barn. Unfortunately, he completely forgets that Bessie the cow is