Codeforces Round #271 (Div. 2)D(递推,前缀和)
很简单的递推题。d[n]=d[n-1]+d[n-k]
注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时。
所以用预处理前缀和来解决重复累加问题。
最后一个细节坑了我多次:
printf("%I64d\n",(s[b]-s[a-1]+mod)%mod);
这句话中加mod万万不能少,因为理论上s[b]-s[a-1]肯定大于0,但是由于两个都是模1000000007以后的结果,那么就不一定了,当s[b]-s[a-1]<0时结果是负的,不是题意中应该输出的结果,所以一定要加mod。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
#define mod 1000000007
LL t,k,a,b,d[],s[];
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%I64d%I64d",&t,&k);
for(int i=;i<k;i++) d[i]=;
d[k]=;
for(int i=k+;i<=;i++)
{
d[i]=(d[i-]+d[i-k])%mod;
}
s[]=;
s[]=d[];
for(int i=;i<=;i++)
{
s[i]=(s[i-]+d[i])%mod;
}
for(int i=;i<t;i++)
{
scanf("%I64d%I64d",&a,&b);
printf("%I64d\n",(s[b]-s[a-]+mod)%mod);
}
//fclose(stdin);
//fclose(stdout);
return ;
}
Codeforces Round #271 (Div. 2)D(递推,前缀和)的更多相关文章
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #271 (Div. 2) D. Flowers (递推)
题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...
- Codeforces Round #271 (Div. 2) F ,E, D, C, B, A
前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...
- Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all kn ...
- Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案
http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory limit p ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #271 (Div. 2)-B. Worms
http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...
随机推荐
- python读写Excel文件--使用xlrd模块读取,xlwt模块写入
一.安装xlrd模块和xlwt模块 1. 下载xlrd模块和xlwt模块 到python官网http://pypi.python.org/pypi/xlrd下载模块.下载的文件例如:xlrd-0.9. ...
- nodejs socket server 强制关闭客户端连接
nodejs socket server 强制关闭客户端连接: client.destroy()
- asp.net 利用Response.Filter 获取输出内容, 变更输出内容
重写 Response.Filter 就可以获取或更新输出到浏览器的内容 资料: https://weblog.west-wind.com/posts/2009/Nov/13/Captur ...
- Centos---linux配置 集群搭建
网络配置 1.创建虚拟机mini1: 1.1.网络配置 NAT网络模式模式 直接修改 /etc/sysconfig/network-script/ifcfg-eth0 输入service netwo ...
- Linux权限管理 chattr命令、lsattr命令、sudo命令
chattr命令 chattr命令用来修改文件系统的权限属性 chatrr 只有 root 用户可以使用,用来修改文件系统的权限属性,建立凌驾于 rwx 基础权限之上的授权. chatrr 命令格式如 ...
- 支持鼠标拖拽滑动的jQuery焦点图
在线演示 本地下载
- nginx最常见的18道面试题
Nginx的并发能力在同类型网页服务器中的表现,相对而言是比较好的,因此受到了很多企业的青睐,我国使用Nginx网站的知名用户包括腾讯.淘宝.百度.京东.新浪.网易等等.Nginx是网页服务器运维人员 ...
- 【bzoj1258】三角形tri[CQOI2007](乱搞)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1258 这是道很意思的大水题,虽然看上去一脸懵逼,但画张图出来看看也可以窥见一丝端倪. ...
- HDU 5992 kd-tree
还记得青岛的时候和蕾姐讨论了近三个小时也不知道这是什么东西 后来发现是kdtree 于是拖到寒假才补这个算法 写完几道模板题发现多维的kdtree查找最近也是很简单的拓展 于是很快1A了这道题 它真的 ...
- js运算符、关键字、保留字、转义字符