链接:https://www.nowcoder.com/acm/contest/140/A

题目描述:

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

大概题意:

小白喜欢运动,他每秒可以走一米或者跑K米,有Q次查询,每次查询求跑0 ~ 【L,R】米的方案数;

思路:

一开始看到这道题想着是道规律题,结果花时间列数据找规律。。。

果然还是too young too simple,师兄花了短短的时间AC之后在群里丢给我们八个字“记忆化搜索,过了”。

想了那么多,这就是道纯粹的DP了啊,关键在于分开跑和走的方案,不能连续跑

状态:

dp[i][0] 走 i  meters 的方案数

dp[i][1] 跑 i meters 的方案数

转移方程:

dp[i][0] = dp[i-1][1] + dp[i-1][0];

dp[i][1] = dp[i-k][0] (注意题目条件:不能连续跑,有队友一直卡在60%这里的原因)

!!!更新dp值的同时也要更新前缀和sum[i] (用于最后求答, 本人傻傻的还在后面查询的时候才计算前缀和,一直T)!!!

AC code:

#include <cstdio>
#include <iostream>
#include <cstring>
#define mod 1000000007
using namespace std; const int maxk = ; int Q, k;
long long int dp[maxk][], sum[maxk]; int main()
{
scanf("%d%d", &Q, &k);
for(int i = ; i < k; i++)
{
dp[i][] = ;
dp[i][] = ;
if(i > )
sum[i] = sum[i-] + dp[i][]+dp[i][];
}
dp[k][] = ;
sum[k] = sum[k-] + dp[k][];
for(int i = k; i < maxk; i++)
{
dp[i][] = ((dp[i-][] + dp[i-][]))%mod ;
dp[i][] = dp[i-k][];
sum[i] = sum[i-]+dp[i][]+dp[i][];
}
while(Q--)
{
int L, R;
long long int ans = ;
scanf("%d%d", &L, &R);
ans =(sum[R] - sum[L-] + mod)%mod;
printf("%lld\n", ans);
}
return ;
}

最新文章

  1. ajax表单提交
  2. IplImage结构体
  3. nyoj 364 田忌赛马(贪心)
  4. 使用C#实现FTP的文件上传和下载【转】
  5. Jquery 格式化时间
  6. mysql事务问题
  7. Java简介(3)-基本语法
  8. JavaEE Tutorials (27) - Java EE的并发工具
  9. 在Windows环境下设置terminal下调试adb
  10. SAP PI入门
  11. Java加密与解密笔记(二) 对称加密
  12. ●BZOJ 4453 cys就是要拿英魂!
  13. java 中Excel的导入导出
  14. unicode utf-8 ascll编码比较
  15. 洛谷P3808 【模板】AC自动机(简单版)
  16. Unable to cast COM object of type &#39;Shell32.ShellClass&#39; to interface type &#39;Shell32.IShellDispatch6&#39;.
  17. ReactiveX 学习笔记(17)使用 RxSwift + Alamofire 调用 REST API
  18. django系列5.4--ORM中执行原生SQL语句, Python脚本中调用django环境
  19. python爬虫实战之bilibili弹幕生成云图
  20. 接口日志记录AOP实现-LogAspect

热门文章

  1. Linux学习笔记之(1)~Linux有趣的历史概览
  2. ajax返回整个页面
  3. css常用左右布局方案整理
  4. 2017年11月30日 C#TreeNode递归&amp;邮箱验证&amp;新用户窗体
  5. Servlet开发(三)之ServletConfig,ServletContext
  6. base64编码 的 图片 另存为下载
  7. 实现移动端touch事件的横向滑动列表效果
  8. [小北De编程手记] : Lesson 02 - Selenium For C# 之 核心对象
  9. arcgis 地理国情建库软件已完成
  10. ArcGIS中国工具(ArcGISCTools)2.0在线视频