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 ...
随机推荐
- 【转】ModelAndView 学习
http://blog.csdn.net/wavaya/article/details/6185226 ModelAndView 类别就如其名称所示,是代表了Spring Web MVC程式中呈现画面 ...
- 通用TryParse
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Refle ...
- Tensorflow学习笔记(1)--安装
安装 1.ubuntu 14.04 2. 清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/ (要求sudo权限,如果报 ...
- docker学习笔记1-- 用Toolbox安装Docker--介绍Docker Machine
使用的是Docker Toolbox,非Docker for Windows 一.docker的认识与安装(windows安装) http://blog.csdn.net/tina_ttl/artic ...
- linux mint —— 图片一张
概述 Linux Mint是一種基於Ubuntu開發出的Linux操作系统.由Linux Mint Team团队于2006年开始发行.Linux Mint 的目标是为家庭用户和企业客户提供一个免费.高 ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)
系统角色篇 数据结构 用户管理 Controller代码 public class SystemUserController : Controller { //public void Log() // ...
- 3D立方体图片切换动画
在线演示 本地下载
- Ubuntu 15.10环境下安装Hive
1. 安装MySQL sudo tar -xzvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz sudo mv mysql-5.7.13-linux-glibc ...
- 13.常见模块re-正则模块
1.正则 正则表达式是计算机科学的一个概念,正则表通常被用来检索.替换那些符合某个模式(规则)的文本.也就是说使用正则表达式可以在字符串中匹配出你需要的字符或者字符串,甚至可以替换你不需要的字符或者字 ...
- INSPIRED启示录 读书笔记 - 第8章 巴顿将军的忠告
目标管理 永远不要告诉别人怎么做.告诉他们做什么,他们自然会发挥天赋,给你惊喜. ——乔治·史密斯·巴顿 首先,产品经理收集需求时,常听到客户建议“如何做”产品,而不是产品应该“做什么”.如果产 ...