codeforces 474D flowers 普通计数dp

We saw the little game Marmot made for Mole’s lunch. Now it’s Marmot’s dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.

Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).

有x朵花,其中有白花、红花不一。让你找出连续k朵花为白花的种数(当然x朵花里可以有多组k朵白花)。
现在让你找出a朵花~b朵花之间究竟有多少符合条件的。

那么先求出每一种长度的方法数 再统计起来就好

当前长度 dp[i]=dp[i-1]+dp[i-k] 就是取红花和取白花的情况
因为是种类数 就是相加起来就好。如果直接线性统计总数其实是不好弄的。因为总数和长度没有状态转移。不能转移,但是长度和长度有状态转移。

#include 
using namespace std;
const int maxn=1e5+10;
const int mod=1e9+7;
int dp[maxn];
int sum[maxn];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<=maxn;i++)
    {
        dp[i]=1;
    }
    for(int i=k;i1]+dp[i-k];
        dp[i]%=mod;
    }
    sum[0]=0;
    for(int i=1;isum[i]=sum[i-1]+dp[i];
        sum[i]%=mod;
    }
    for(int i=0;iint l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",(sum[r]-sum[l-1]+mod)%mod );
    }
}

你可能感兴趣的:(dp)