链接: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. 【知识强化】第四章 指令系统 4.3 CISC和RISC的基本概念

    那么我们进入本章的最后一节,CISC和RISC. 我们先来回顾一下,我们这一章的一个概览.我们之前已经把指令格式和指令的寻址方式都讲完了,这两部分呢是本章的一个重点.而本章的这一部分,CISC和RIS ...

  2. linux驱动启动顺序

    首先,我们可以查看Linux内核编译完成后的System.map文件,在这个文件中我们可以看到macb(dm9161驱动模块)链接到了dm9000驱动之前,如下所示: c03b6d40 t __ini ...

  3. pass value from shell to sql

    echo 'please enter new userid need create' read new_usr echo 'please enter userid to model' read mod ...

  4. (ACM模板)集合set

    #include<iostream> #include<cstdio> #include<set> using namespace std; int main() ...

  5. 三、Angular项目,app.module.ts解析

    1. 项目主要文件存放的路径 2.app.module.ts模块解析 3.模块和组件关系 |--app.module.ts(模块) |--app.component.ts(组件)  |--app.co ...

  6. MFC程序执行过程剖析(转)

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  7. 解决swagger跨项目或跨程序集注释不显示问题

    背景 我们在使用Swagger生成.NET Core Web Api 项目接口文档时候,发现接口的入参和出参的注释是看不见的,如下: 但是我想要结果是这样: 原因分析以及方案 为什么没有显示注释呢,注 ...

  8. Oracle11g新建用户及用户表空间

    /* 建立数据表空间 */CREATE TABLESPACE SP_TAB DATAFILE '/u01/app/oracle/oradata/orcl/tab1_1.dbf' size 1024M ...

  9. boost system

    boost::system::error_code is the most basic class in Boost.System; it represents operating system-sp ...

  10. mybatis源码分析之02配置文件解析

    该篇正式开始学习mybatis的源码,本篇主要学习mybatis是如何加载配置文件mybatis-config.xml的, 先从测试代码入手. public class V1Test { public ...