【百度之星】2020初赛第三场 1005(hdu6787) | 经典dp

 题目思路:

如果掷骰子得话,会最多向前移动11位,所以说如果有连续的11个传送器,那无论结果如何都是过不去得

所以题目转换为给你m个传送器,构造长度为n不能有m个连续的传送器得方案数

如果没有传送方向,是一个很经典的dp

考虑dp j,i,k 代表以i结尾连续的传送器有j个,当前已经放了k个

不放:

考虑当前位置放还是不放,不放的话应该加上之前连续0~10个:

\sum_{j=0}^{j=10}{dp[j][i-1][k]}

放:

首先考虑如何放!

第i个连续j个放k个的方案数 应该有dp[j-1][i-1][k-1]转移过来

因为此时目的地还有所不同,所以直接*(i-1)即可

完事~

Code:

/*** keep hungry and calm CoolGuang!***/
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pp;
const ll INF=1e17;
const int Maxn=2e7+10;
const int maxn =1e5+10;
const int mod= 1e9+7;
const int Mod = 1e9+7;
///const double eps=1e-10;
inline bool read(ll &num)
{char in;bool IsN=false;
    in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
int dp[11][1005][1005];
int main()
{
    int T;scanf("%d",&T);
    while(T--){
        read(n);read(m);
        memset(dp,0,sizeof(dp));
        dp[0][1][0] = 1;
        for(int i=2;i<=n;i++){
            for(int k=0;k<=m;k++){
                for(int j=0;j<=10;j++) dp[0][i][k] = (dp[j][i-1][k] + dp[0][i][k])%mod;
                if(i!=n){
                    for(int j=1;j<=10;j++){
                        if(k>=1) dp[j][i][k] = (dp[j][i][k] + 1ll*dp[j-1][i-1][k-1]*(i-1)%mod)%mod;
                    }
                }
            }
        }
        printf("%d\n",dp[0][n][m]?dp[0][n][m]:-1);
    }
    return 0;
}
/**
13 10
1 2~12 13
**/

 

猜你喜欢

转载自blog.csdn.net/qq_43857314/article/details/107596786
今日推荐