牛客网暑期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 ...
随机推荐
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- Ubuntu 系统如何用pycharm开发python—OpenCV
- 免安装版tomcat安装成服务
安装方式(前提已经安装好了jdk,并配置了环境量): (1) 下载非exe的tomcat zip包: (2) 解压缩,如:D:\tomcat: (3) 进入D:\tomcat\bin,修改servic ...
- 对API进行版本控制的重要性和实现方式
我在API设计中收到的最常见问题之一就是如何对API进行版本控制.虽然并非所有API都完全相同,但我发现在API版本控制方面,某些模式和实践适用于大多数团队.我已经将这些内容收集起来,下面将提供一些关 ...
- 使用JMS接口接入WebSphere MQ消息
在你的应用程序中利用IBM WebSphere MQ消息中间件提供Java消息服务开放接口. IBM WebSphere MQ(WMQ)是一套面向消息的中间件(message-oriented mid ...
- mui的app页面使用layui填充数据
在mui的开发中有个坑,mui.plusReady在web上使用时是不会起作用的,只能在app上才行,所以推荐自己测试时使用mui.ready去写加载时的方法. 前端请求的返回格式为json,所以在后 ...
- 洛谷 P2787 语文1(chin1)- 理理思维
题意简述 维护字符串,支持以下操作: 0 l r k:求l~r中k的出现次数 1 l r k:将l~r中元素赋值为k 2 l r:询问l~r中最大连续1的长度 题解思路 珂朵莉树暴力赋值,查询 代码 ...
- Spring 2017 Assignments2
一.作业要求 原版:http://cs231n.github.io/assignments2017/assignment2/ 翻译:http://www.mooc.ai/course/268/lear ...
- EXP查询合集提权后渗透必备
0x00 整理的一些后渗透提权需要用到的一些漏洞,后渗透提权的时候可以看一下目标机那些补丁没打,再进行下一步渗透提权. 0x01 CVE-2019-0803 [An elevation of priv ...
- Windows Server 2008配置系统安全策略
下面学习Windows Server 2008配置系统安全策略 在工作组中的计算机本地安全策略有 用户策略,密码策略,密码过期默认42天 服务账户设置成永不过期,帐户锁定策略,本地策略,审核策略,计算 ...