Codeforces 474d Flowers | dp

看了题解才知道用dp。。。原来题意都已经理解错了。。。查

题意:

有x朵花,其中有白花、红花不一。让你找出连续k朵花为白花的种数(当然x朵花里可以有多组k朵白花)。

现在让你找出a朵花~b朵花之间究竟有多少符合条件的。

思路:

DP

状态定义:dp[i]:有i朵花时符合条件的种数。

状态转移:dp[i] = dp[i-1]+dp[i-k];  dp[i-1]:i位置放置红花;dp[i-k]:放白花。

AC代码:

/* **********************************************
Created Time: 2014/10/7 15:20:20
File Name   : cf.cpp
*********************************************** */
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
typedef long long LL;
using namespace std;
const int MAXN = 1e5+5;
const int MOD = 1e9+7;
LL dp[MAXN], ans[MAXN];
int main()
{
        int t, k;
        cin>>t>>k;
        dp[0] = 1;
        for(int i = 1;i <= MAXN; i++)
                dp[i] = (dp[i-1] + (i >= k? dp[i-k] : 0)) % MOD;
        for(int i = 1;i <= MAXN; i++)
                ans[i] = (ans[i-1] + dp[i]) % MOD;
        int l, r;
        for(int i = 0;i < t; i++)
        {
                cin>>l>>r;
                cout<<(ans[r] - ans[l-1] + MOD)%MOD<<endl;
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值