链接: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. 【学习总结】Python-3-Python数字运算与数学函数

    菜鸟教程-Python3-Python数字 注:这一节链接中的内容比较多,表格中的具体函数耐心点进去看看 1-变量在使用前必须先"定义"(即赋予变量一个值),否则会出现错误 2-不 ...

  2. rm - 移除文件或者目录

    总览 rm [options] file... POSIX(Portable Operating System Interface 可移植的操作系统接口) 选项: [-fiRr] GNU 选项 (最短 ...

  3. React(2) --super关键字

    参考:http://www.phonegap100.com/thread-4911-1-1.html Es6中的super可以用在类的继承中,super关键字,它指代父类的实例(即父类的this对象) ...

  4. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  5. java使用对象类型作为方法的参数

  6. ExtJS5.0 菜鸟的第一天

    1.新项目开始啦,后台用户管理页面涉及到表格数据添加,修改内容比较多.准备用EXTJS框架搞下,对于我这种JS不咋地的人来说,还真是个挑战.整了2天,才搞出一个Hello,world!我也是醉了.. ...

  7. 一文带你领略虚拟化领域顶级技术会议KVM Forum 2018

    KVM Forum是由Linux基金会组织的高端技术论坛会议,主要为社区各个维护者,开发人员,和用户提供一个讨论Linux虚拟化技术发展趋势以及挑战的交流场所.参会人员都集中在KVM虚拟化相关领域,是 ...

  8. npm 常见错误记录

    1.Module build failed: ReferenceError: Unknown plugin "import" specified in "base&quo ...

  9. Java常用数据结构Set, Map, List

    1. Set Set相对于List.Map是最简单的一种集合.集合中的对象不按特定的方式排序,并且没有重复对象. 特点: 它不允许出现重复元素: 不保证和政集合中元素的顺序 允许包含值为null的元素 ...

  10. NOIp 图论算法专题总结 (3):网络流 & 二分图 简明讲义

    系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 网络流 概念 1 容量网络(capacity network)是一个有向图,图的 ...