run (简单DP)
链接: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)的更多相关文章
- Kattis - bank 【简单DP】
Kattis - bank [简单DP] Description Oliver is a manager of a bank near KTH and wants to close soon. The ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- 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 ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
随机推荐
- C#使用Process启动exe程序,不弹出控制台窗口的方法
背景:使用wkhtmltopdf工具将html转换成pdf时,这个工具在进行转换时会弹出命令行窗口显示转换过程,但是在项目运行时弹出服务器突然弹出控制台窗口会很奇怪,尤其是当转换多个时.解决这个问题 ...
- elasticsearch 基础 —— 索引、更新文档
索引文档 通过使用 index API ,文档可以被 索引 -- 存储和使文档可被搜索 . 但是首先,我们要确定文档的位置.正如我们刚刚讨论的,一个文档的 _index . _type 和 _id 唯 ...
- 2018-2-13-win10-uwp-分治法
title author date CreateTime categories win10 uwp 分治法 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...
- 让centos使用ubuntu的make命令补全功能
一直习惯使用debian.ubuntu做开发机,最近it要求各种安全加固,且只提供centos自动化脚本,而ubuntu版本比较乱,14.16.17都要自己整一遍太麻烦,索性换装centos7. 换了 ...
- Linux系统中创建大文件,并作为文件系统使用
在LInux系统的使用过程中,有时候会遇到诸如某个磁盘分区的大小不够用了,导致其下的文件系统不能正常写入数据.亦或者是系统swap分区太小,不够用或者不满足条件而导致的其他一系列问题.如果我们系统上挂 ...
- 关于WTSAPI32
一般在windows编程都是用用从ntdll导出的Native API,现在看到一点COM编程或者其他的一些不常用的接口函数总觉得蛮有意思,准备以后多积累一下. 先简单总结WTSAPI32.以下实在W ...
- java下载文件demo
java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html
- 编写mapreduce的程序的套路
https://blog.csdn.net/qq_42881421/article/details/83543926 给出下面6个经典案例: http://www.cnblogs.com/xia520 ...
- equals区别==
来自:https://blog.csdn.net/m0_37721946/article/details/78405595 java中的数据类型,可分为两类: 1.基本数据类型 byte,short, ...
- 25-Node.js学习笔记-express-app.locals对象
app.locals对象 将变量设置到app.locals对象下面,这个数据在所有的模板中都可以获取到 app.locals.users=[{ name:'柠檬不酸', age:20 },{ name ...