链接:https://www.nowcoder.com/acm/contest/140/A
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

White Cloud is exercising in the playground.
White Cloud can walk 1 meters or run k meters per second.
Since White Cloud is tired,it can't run for two or more continuous seconds.
White Cloud will move L to R meters. It wants to know how many different ways there are to achieve its goal.
Two ways are different if and only if they move different meters or spend different seconds or in one second, one of them walks and the other runs.

输入描述:

The first line of input contains 2 integers Q and k.Q is the number of queries.(Q<=100000,2<=k<=100000)
For the next Q lines,each line contains two integers L and R.(1<=L<=R<=100000)

输出描述:

For each query,print a line which contains an integer,denoting the answer of the query modulo 1000000007.

输入例子:
3 3
3 3
1 4
1 5
输出例子:
2
7
11

-->

示例1

输入

复制

3 3
3 3
1 4
1 5

输出

2
7
11 思路分析:
本题背景抛开,其实就是一个L到R区间内的一个数可以拆成多少种1和K两个数的和
本题最开始想用排列组合做,因为K的不同位置和不同个数。。。引起不同种类,后来发现需要求组合数1-100000,这明显是不切实际的。。。
换种思路:其实用DP的思想想一想,每个状态都是上面两个状态传来,分别是DP[i]=DP[i-1]+DP[i-K],但是不要着急。。。很明显题意给了一个限制条件,不能连续的跑
我们可以用一个DP[i][3]来表示各个状态
首先DP[i][0]代表走路到这个位置
DP[i][1]代表跑步到这个位置
DP[i][2]代表到这个位置的所有种类
这样我们就可以写出转移方程
DP[i][0]=DP[i-1][2]
DP[i][1]=DP[i-k][0]
DP[i][2]=DP[i][0]+DP[i][1]
当然有大神找规律得出这个动态转移方程
DP[i]=DP[i-1]+DP[i-K-1]
是不是很6???仔细想想其实也是对的,我们由于不能直接从DP[i-K]因为不能连续的跑步,我们知道DP[i-K-1]肯定是走路到DP[i-k]那么其实直接用DP[i-K-1]
就好了,最后前缀和数保存就行
最后一定要注意取模的地方。。。
(a+b) mod p = (a mod p + b mod p) mod p 
(a*b) mod p = ((a mod p) * (b mod p)) mod p 
(a-b) mod p = ((a mod p)-(b mod p) + p) mod p 
所以取模应该是
ans=(DP[r]-DP[l-1]+mod)%mod
代码如下
#include<iostream>
#include<string.h>
#include<stdio.h>
#define ll long long
using namespace std;
const int N = +;
const int mod = ;
ll dp[N][];
ll sum[N];
int main(){
int q,k;
while(~scanf("%d%d",&q,&k)){
sum[]=;
dp[][]=;
dp[k][]=;
dp[][]=;
for (int i=;i<=;i++){
dp[i][]=dp[i-][];
if(i-k>=){
dp[i][]=dp[i-k][];
}
dp[i][]=(dp[i][]+dp[i][])%mod;
sum[i]=(sum[i-]+dp[i][])%mod;
}
int l,r;
ll ans=;
for(int i=;i<=q;i++){
scanf("%d%d",&l,&r);
ans=(sum[r]%mod-sum[l-]+mod)%mod;
printf("%lld\n",ans);
}
}
return ;
}


最新文章

  1. [Java Web整合开发王者归来&#183;刘京华] 2、 Java Web开发概述
  2. Spark metrics on wordcount example
  3. iOS开发之Xcode 6 国际化
  4. ORACLE创建、修改、删除序列
  5. wget 测试cdn
  6. HTML网页制作:[12]使用框架结构之frameset
  7. python demo整理
  8. CodeForces 705B Spider Man
  9. bzoj 1597: [Usaco2008 Mar]土地购买
  10. mybatis 一对多查询
  11. PHPExcel导出数据时字段超过26列出错Invalid cell coordinate [1
  12. [runtime] MAObjCRuntime
  13. centos7 修改主机名的方法(在centos7有效)
  14. nodeJs 常用模块(一)
  15. ubuntu 将&amp;quot;/TMP&amp;quot;挂载到内存中
  16. ExtJs之列表(grid)
  17. javascript一些小的注意点
  18. 图像处理检测方法 — SIFT和SURF
  19. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
  20. 《JavaScript设计模式》笔记之第三章:封装和信息隐藏

热门文章

  1. Linux 内存文件系统
  2. C#项目实践之一——WPF多媒体通讯录
  3. Activiti工作流与BPMN2.0规范
  4. PCB (3)创建新工程PCB
  5. MVC知识点记录
  6. 遇到的web请求错误码集合与解释
  7. E325: ATTENTION
  8. MyBatis原理简介和小试牛刀
  9. Echo团队Alpha冲刺随笔集合
  10. android RadioGroup中设置selector后出现多个别选中的RadioButton的解决办法