牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp
链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网
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.
输入例子:
3 3
3 3
1 4
1 5
输出例子:
2
7
11
-->
分析:题目让我们求从0到(l,r)的所有可能走法,每次可以走1或者k,但是走了k必须停一次
从0到x(1<=x<=n)每次走1或者k,不考虑停一次的种数符合递推式:
f(x) = f(x-1) + f(x-k)(x>=k) f(x) = 1(x>=1) f(0) = 0(x=0)
比赛时我是看其中k=2时符合斐波拉数列,于是想到了递推,然后推了3写出了递推式
然后我们看连续走k的情况,可以发现:
g(x) = 0(x<2*k) g(x) = x-k+1(2*k<=x<=3*k-1) g(x) = g(x-1) + g(x-k+1) (x>=3*k)
最后到x符合情况的种数就是:f(x)-g(x)
而每次要求到(l,r),我们可以先算个前缀和,在开始打表出所有值就行
当然k=1的时候,因为走和跑不同,所以不是每种都为1,计算一下可以发现符合斐波拉数列(可以考虑把走看成1,跑看成0,然后求在1中间插0的排列数)
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5;
const int mod = 1e9 + 7;
typedef long long ll;
ll a[10*maxn+10], sum[10*maxn+10], b[10*maxn+10];
int main() {
std::ios::sync_with_stdio(false);
ll n, k;
cin >> n >> k;
if( k != 1 ) {
sum[0] = 0;
for( ll i = 1; i <= k-1; i ++ ) {
a[i] = 1;
sum[i] = sum[i-1] + a[i];
}
a[k] = 2;
sum[k] = sum[k-1] + a[k];
ll num = 1;
for( ll i = 2*k; i <= 3*k-1; i ++ ) {
b[i] = num;
num ++;
}
for( ll i = k+1; i <= maxn; i ++ ) {
a[i] = ( a[i-1] + a[i-k] ) % mod;
if( i >= 3*k ) {
b[i] = ( b[i-1] + b[i-k+1] ) % mod;
sum[i] = ( sum[i-1] + ( a[i] - b[i] ) % mod ) % mod;
} else if( i < 3*k && i >= 2*k ) {
sum[i] = ( sum[i-1] + ( a[i] - b[i] ) % mod ) % mod;
} else {
sum[i] = ( sum[i-1] + a[i] ) % mod;
}
}
} else {
sum[1] = 2, sum[2] = 5;
a[1] = 2, a[2] = 3;
for( ll i = 3; i <= maxn; i ++ ) {
a[i] = ( a[i-1] + a[i-2] ) % mod;
sum[i] = ( sum[i-1] + a[i] ) % mod;
}
}
while( n -- ) {
ll le, ri;
cin >> le >> ri;
if( le == 1 ) {
cout << sum[ri] << endl;
} else {
cout << ( sum[ri] - sum[le-1] + mod ) % mod << endl;
}
}
return 0;
}
再贴一个dp的做法
f[i][0/1]表示已经跑了i米,最后一步是跑还是走的方案数。 f[i][1]=f[i-k][0],f[i][0]=f[i-1][0]+f[i-1][1] 答案即为l到r的f[i][0]和f[i][1]的累加 ,我们只需记录前缀和即可
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5;
const int mod = 1e9 + 7;
typedef long long ll;
ll sum[2*maxn], dp[2*maxn][3];
int main() {
std::ios::sync_with_stdio(false);
ll n, k, le, ri;
cin >> n >> k;
memset( dp, 0, sizeof(dp) );
memset( sum, 0, sizeof(sum) );
sum[0] = 0, dp[0][0] = 0, dp[0][1] = 0;
for( ll i = 1; i <= k; i ++ ) {
dp[i][0] = 1;
if( i == k ) {
dp[k][1] = 1;
}
sum[i] = ( dp[i][0] + dp[i][1] + sum[i-1] ) % mod;
}
for( ll i = k+1; i <= maxn; i ++ ) {
dp[i][0] = dp[i-1][0] + dp[i-1][1], dp[i][1] = dp[i-k][0];
sum[i] = ( sum[i-1] + dp[i][1] + dp[i][0] ) % mod;
}
while( n -- ) {
cin >> le >> ri;
cout << ( sum[ri] - sum[le-1] + mod ) % mod << endl;
}
return 0;
}
牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp的更多相关文章
- 牛客网暑期ACM多校训练营 第九场
HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
- 牛客网暑期ACM多校训练营(第五场):F - take
链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学
牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...
- 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)
牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
随机推荐
- 并发栅栏CyclicBarrier---简单问2
并发栅栏CyclicBarrier---简单问 背景:前几天在网上看到关于Java并发包java.concurrent中一个连环炮的面试题,整理下以备不时之需. CyclicBarrier简介: 栅栏 ...
- light oj 1159 - Batman LCS
学过简单动态规划的人应该对最长公共子序列的问题很熟悉了,这道题只不过多加了一条字符串变成三条了,还记得,只要把状态变成三维的即可. //http://lightoj.com/volume_showpr ...
- 关于input[type='checkbox']全选的问题
今天在做一个全选功能的时候,发现了一个问题,就是如果我在选择全选之前,我就已经选择了一个input,然后我再去选择全选并且以后再取消全选的时候,这个我之前选择的input始终处于选择状态,但是他的ch ...
- 转载 | Sublime text3 实用快捷键整理
实用快捷键 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+ ...
- 帝国CMS(EmpireCMS) v7.5后台getshell分析(CVE-2018-18086)
帝国CMS(EmpireCMS) v7.5后台getshell分析(CVE-2018-18086) 一.漏洞描述 EmpireCMS 7.5版本及之前版本在后台备份数据库时,未对数据库表名做验证,通过 ...
- MySQL高可用架构:mysql+keepalived实现
系统环境及架构 #主机名 系统版本 mysql版本 ip地址 mysqlMaster <a href="https://www.linuxprobe.com/" title= ...
- 性能测试学习第一天-----概念、环境、LR录制&参数化
1.性能测试的概念: 通过一定的手段,在多并发情况下,获取被测系统的各项性能指标, 验证被测系统在高并发下的处理能力.响应能力.稳定性等,能否满足预期.定位性能瓶颈,排查性能隐患,保障系统的质量,提升 ...
- 如何用Python实现敏感词的过滤
题目要求如下: 从文件解析敏感词,从终端获取用户输入.根据敏感词对用户输入进行过滤.这里过滤需要考虑不止一个过滤词:即将读取的所有过滤词,放进一个列表,用屏蔽词检索用户输入,如果有屏蔽词,则将其替换为 ...
- 使用Counter进行计数统计
使用Counter进行计数统计 想必大家对计数统计都不陌生吧!,简单的说就是统计某一项出现的次数.实际应用中很多需求都需要用到这个模型,如检测样本中某一值出现的次数.日志分析某一消息出现的频率分析文件 ...
- 一个接口多个实现类的Spring注入方式
1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...