链接: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.
示例1
输入
3 3
3 3
1 4
1 5
输出
2
7
11

题意:一个人从0坐标开始,1秒可以走一步或者跑k步,但是不能连续跑,问跑到 [ l,r ]这个区间的方法数;

题解:dp做法,dp[ i ] [ 0/1] 表示到 i 这里的种数,0表示走,1表示跑

初始化dp[ 0 ][ 0 ] 为1;

递推公式:dp[i][0]=dp[i-1][0]+dp[i-1][1];

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

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define N 100005
int dp[N][],l,r,q,k;
int s[N];
int main()
{
ios_base::sync_with_stdio(); cin.tie();
memset(s,,sizeof(s));
dp[][]=;
cin>>q>>k;
for(int i=;i<=N;i++)
{
dp[i][]=(dp[i-][]+dp[i-][])%mod;
if(i>=k)dp[i][]=dp[i-k][];
s[i]=(s[i-]+dp[i][]+dp[i][])%mod;
}
while(q--)
{
cin>>l>>r;
cout<<(s[r]-s[l-]+mod)%mod<<endl;//+mod防止出现负数
}
return ;
}

也可以用记忆化搜索写:

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+;
const int mod = 1e9+;
long long dp[maxn][];
long long ans[maxn];
int q,k,n,m; //flag 1 : 可以跑
long long dfs(int n,bool flag)
{
if(n == ) return ;
if(n < ) return ; if(dp[n][flag]) return dp[n][flag]; if(flag){
dp[n][flag] = (dfs(n-,) + dfs(n-k,) )%mod;
return dp[n][flag];
}else{
dp[n][flag] = dfs(n-,);
return dp[n][flag];
}
} int main()
{
int st = ;
scanf("%d%d",&q,&k);
for(int i=;i<q;i++){
scanf("%d%d",&n,&m);
dfs(m,);
for(;st<=m;st++){
ans[st] = (ans[st-] + dp[st][])%mod;
}
printf("%lld\n",(ans[m] - ans[n-] + mod)%mod );
}
return ;
}

run (简单DP)的更多相关文章

  1. Kattis - bank 【简单DP】

    Kattis - bank [简单DP] Description Oliver is a manager of a bank near KTH and wants to close soon. The ...

  2. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  4. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  5. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  6. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  7. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  8. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  9. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  10. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. TMS320C6455BCTZA 原厂订购 原装正品

    作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...

  2. 一、WebApi模型验证实践项目使用

    一.启语 前面我们说到,模型验证的原理(包含1.项目创建,2.模型创建,3.走通测试模型验证,4.在过滤器中处理返回json格式(非控制器内))-完全是新手理解使用的,新番理解 通常情况下,对于那些经 ...

  3. Git--08 Jenkins

    目录 Jenkins 01. 安装准备 02 .安装Jdk和Jenkins 03 .配置Jenkins 04. 插件安装 05. 创建项目 06. Jenkins获取Git源代码 07. 立即构建获取 ...

  4. MySQL05-- 客户端工具及SQL语句

    目录 MySQL客户端工具及SQL语句 一.客户端命令介绍 二.接收用户的SQL语句 三.字符集定义 四.字符集设置 五.select的高级用法(扩展) MySQL客户端工具及SQL语句 一.客户端命 ...

  5. shell编程基础知识3

    1.Linux下scp的用法 scp就是secure copy,一个在linux下用来进行远程拷贝文件的命令.有时我们需要获得远程服务器上的某个文件,该服务器既没有配置ftp服务器,也没有做共享,无法 ...

  6. Ubuntu12.04安装配置vncserver

    安装 sudo apt-get install vnc4server 修改配置文件 sudo vim ~/.vnc/xstartup #!/bin/sh # Uncomment the followi ...

  7. Thymeleaf入门到吃灰

    Thymeleaf 官网部分翻译:反正就是各种好 Thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎 Spring官方支持的服务的渲染模板中,并不包含jsp.而是Thymel ...

  8. boost variant

    Boost Variant resembles union. You can store values of different types in a boost::variant. 1. #incl ...

  9. 谷歌已经对Android的开源严防死守

    上周,沸沸扬扬的Android垄断案把Google又一次推向了风口浪尖,在这次的垄断案中,Google被欧盟起诉赔偿50亿美元,被起诉的其中一个原因是Google对外宣称Android是开放的,但其实 ...

  10. IGServer for Java

    Eclipse和JavaEE: DCServer是哪个? 查看服务器文件夹: Env_Var变量没有定义:JRE_HOME.JDK_HOME 这是Tomcat报错的提示,但是既然JAVA_HOME都有 ...