您的位置:首页 > 其它

zoj 3725 DP排列

2013-07-16 20:05 197 查看
DP计数,

有限制性条件下的计数无法直接推公式,一般含有重复,无法完全吻合

这时选择dp,调用前面算出的结果

(two[i-m-1]-dp[i-m-1])用的妙!


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define mod 1000000007
#define N 100500

ll two
;
ll dp
;
int main ()
{
ll n,m;
two[0]=1;
for(int i=1;i<N;++i)
two[i]=two[i-1]*2%mod;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[m]=1;
for(int i=m+1;i<=n;++i)
{
dp[i]=2*dp[i-1]+(two[i-m-1]-dp[i-m-1]);
dp[i]%=mod;
}
dp
=(dp
%mod+mod)%mod;
printf("%lld\n",dp
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: