【Codeforces Round 271 (Div 2)D】【DP】Flowers 黑色必须连续摆放k,长度为n的摆放方案数
来源:互联网 发布:微软办公软件2007 编辑:程序博客网 时间:2024/05/14 11:13
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;int f[N],s[N];int n,k,l,r;int main(){while(~scanf("%d%d",&n,&k)){f[0]=s[0]=1;for(int i=1;i<=100000;++i){f[i]=f[i-1];if(i>=k)f[i]=(f[i]+f[i-k])%Z;s[i]=(s[i-1]+f[i])%Z;}for(int i=1;i<=n;++i){scanf("%d%d",&l,&r);printf("%d\n",(s[r]-s[l-1]+Z)%Z);}}return 0;}/*【题意】我们要用黑白棋子摆成一列,其中黑色棋子必须k个一组连续摆放。问你,对于长度为[1,n]的摆放,最终有多少种方案。【类型】DP【分析】这题我们用f[i]表示长度为i的合法摆放方案有多少种。然后,对于每次在位置i的摆放,可以考虑这个位置是摆放白棋还是黑棋。对应的状态转移前驱分别是f[i-1]和f[i-k]然后记录一个前缀和,我们这道题就做完啦!【时间复杂度&&优化】O(n)*/
0 0
- 【Codeforces Round 271 (Div 2)D】【DP】Flowers 黑色必须连续摆放k,长度为n的摆放方案数
- 【Codeforces Round 263 (Div 2)D】【树形DP】Appleman and Tree 树上割k个黑点为k块的方案数
- Codeforces Round #271 (Div. 2) D. Flowers(DP)
- Codeforces Round 370 (Div 2) D.Memory and Scores 【前缀和 DP】双人随机数值增减K A比B多的方案数
- 【Codeforces Round 324 (Div 2)A】【水题】Olesya and Rodion 构造数长度为n且是t的倍数
- 【Codeforces Round 271 (Div 2)E】【离散化线段树】Pillars 最长连续序列使得序列相邻的数差值至少为k
- 【Codeforces Testing Round 12C】【DP 树状数组优化】Subsequences n个不同数,长度为m的LIS数
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- 【Codeforces Round 370 (Div 2) D】【前缀和打标记】Memory and Scores 双人随机数值增减K A比B多的方案数
- 【Codeforces Round 365 (Div 2)E】【乘除法DP map映射 约数分解】Mishka and Divisors n个数中选最小数量使得乘积为K的倍数
- 【Codeforces Round 334 (Div 2)E】【博弈-SG函数】Lieges of Legendre n个数 每次取数-1或者变2x为k个x的博弈
- Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
- Codeforces Round #273 (Div. 2)D dp
- Codeforces Round #358 (Div. 2) D dp
- 状态压缩DP----车的摆放4
- 【Codeforces Round 326 (Div 2)D】【DP】Duff in Beach 数列重复取数最多k次使得单调不下降
- DP-花瓶摆放
- D. Flowers Codeforces Round #271(div2)
- SWD与JTAG之间的区别
- android load BKS error: wrong version of key store
- 【代码分享】关于List<V>按V的某个属性分组的通用代码实现
- Django笔记教程:四、会话、注册、以及用户
- Java 中有关数组的操作
- 【Codeforces Round 271 (Div 2)D】【DP】Flowers 黑色必须连续摆放k,长度为n的摆放方案数
- easyui 滚动条
- db2 外置设备建立数据库步骤
- Objc-C 知识点回顾 六 字典、集合
- 源码安装lamp
- Gosn的使用
- opencv中的矩阵操作
- LeetCode 211 Add and Search Word - Data structure design
- Objc-C 知识点回顾 七 block