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 每一个格子仅仅 ...
随机推荐
- docker ssh连接不上
docker ssh连接报下面的错 Last login: Thu Apr 13 09:17:23 2017 from localhost Connection to 127.0.0.1 closed ...
- 在iOS中去掉input的光标
在input上添加 readonly unselectable="on" οnfοcus="this.blur()" 就可以了.
- Dubbo源码学习总结系列一 总体认识
本文写作时,dubbo最高版本是V2.6.0. 写这篇文章主要想回答以下4个问题: 一.dubbo是什么?完成了哪些主要需求? 二.dubbo适用于什么场景? 三.dubbo的总体架构是什么样的? ...
- centos7 利用mailx发送邮件
当需要服务器定时发送邮件到自己邮箱时,一个邮件服务就很重要了,以下主要是mailx的实现,主要是利用 1.安装mailx 1 yum install mailx -y 2.使用到的配置文件只有一个 ...
- linux性能分析工具Cpu
- linux的iptables设置---防火墙
1.首先介绍一下指令和相关配置文件 启动指令:service iptables start 重启指令:service iptables restart 关闭指令:service iptables st ...
- 使用stylelint进行Vue项目样式检查
stylelint是一个强大的现代 CSS 检测器,可以让开发者在样式表中遵循一致的约定和避免错误.拥有超过170条的规则,包括捕捉错误.最佳实践.控制可以使用的语言特性和强制代码风格规范.它支持最新 ...
- python多个装饰器
'''在装饰器中加上参数:1.实现在源代码中加上时间统计:函数timmer2.实现用户名认证功能:函数auth23.实现一次认证,刷新后自动登录功能,index函数已经认证并登录,在执行home函数时 ...
- 英语单词SYNOPSIS
SYNOPSIS 来源——man帮助内容 BASH() General Commands Manual BASH() NAME bash - GNU Bourne-Again SHell SYNOPS ...
- Redis5离线安装
1. 直接上redis官网安装包, 然后上传服务器 https://redis.io/download 2. 解压 tar -zxvf redis-5.0.6.tar.gz 3. 进入redis根目标 ...