Codeforces 474 D Flowers(DP)
2016-03-27 11:19
363 查看
题目链接:
Codeforces 474 D Flowers
题意:
有两种花可以吃,white花只能连续吃k个,red花不受限制,当吃a到b朵花时一共有多少种吃法?也就是:
a朵花时的吃法+(a+1)朵花时的吃法+…+b朵花时的吃法。
分析:
用dp[i][0]和dp[i][1]分别表示吃第i朵花时是white和red的情况。
Codeforces 474 D Flowers
题意:
有两种花可以吃,white花只能连续吃k个,red花不受限制,当吃a到b朵花时一共有多少种吃法?也就是:
a朵花时的吃法+(a+1)朵花时的吃法+…+b朵花时的吃法。
分析:
用dp[i][0]和dp[i][1]分别表示吃第i朵花时是white和red的情况。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> using namespace std; const int MAX_N=100010; const int mod=(int)(1e9+7); int T,k,a,b; int dp[MAX_N][2],ans[MAX_N],sum[MAX_N]={0}; inline void solve() { for(int i=1;i<MAX_N;i++){ if(i<k) dp[i][0]=0,dp[i][1]=1; else if(i==k) dp[i][0]=1,dp[i][1]=1; else{ dp[i][0]=(dp[i-k][0]+dp[i-k][1])%mod; //ith eat white flower dp[i][1]=(dp[i-1][0]+dp[i-1][1])%mod; //ith eat red flower } ans[i]=(dp[i][0]+dp[i][1])%mod; //ith total ways sum[i]=(sum[i-1]+ans[i])%mod; //from 1st to ith total ways } } int main() { scanf("%d%d",&T,&k); solve(); while(T--){ scanf("%d%d",&a,&b); printf("%d\n",(sum[b]-sum[a-1]+mod)%mod); // +mod just in case of the result of sum[b]-sum[a-1] is negative } return 0; }
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)