【Codeforces 474D】Flowers
【链接】 我是链接,点我呀:)
【题意】
让你吃东西
B食物一次必须要吃连续k个
但是对A食物没有要求
问你有多少种吃n个食物的方法(吃的序列)
【题解】
设f[i]表示长度为i的吃的序列且符合要求的方法
有两种转移方法
一种是吃一个A食物
一种是吃k个食物
f[i] = f[i-1]+f[i-k]
f[0] = 1
然后做一个前缀和输出区间和就好
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)1e5;
static int MOD = (int)1e9+7;
static class Task{
public void solve(InputReader in,PrintWriter out) {
int f[] = new int[N+10];
f[0] = 1;
int t,k;
t = in.nextInt();k = in.nextInt();
for (int i = 1;i <= N;i++) {
f[i] = f[i-1];
if (i-k>=0) f[i] = (f[i]+f[i-k])%MOD;
}
for (int i = 1;i <= N;i++) {
f[i] = (f[i]+f[i-1])%MOD;
}
for (int i = 1;i <= t;i++) {
int a,b;
a = in.nextInt();b = in.nextInt();
out.println((f[b]-f[a-1]+MOD)%MOD);
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 474D】Flowers的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 258E】 Devu and Flowers
[题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- bzoj 3312 No Change
题目大意: 到商场购物,他的钱包里有K个硬币 想按顺序买 N个物品,第i个物品需要花费c(i)块钱 在依次进行的购买N个物品的过程中,可以随时停下来付款,每次付款只用一个硬币 支付购买的内容是从上一次 ...
- [SCOI 2010] 连续攻击游戏
[题目链接] https://www.luogu.org/problemnew/show/P1640 [算法] 二分图匹配 实现时需要常数优化和特判 [代码] //code by byf and lm ...
- php从数据库读取中文显示问号??的解决办法
出错原因:1.数据库编码格式不对 2.PHP编码格式不对 3.浏览器编码格式不对 上面三者编码格式不统一,就会出现问题 数据库读取的时候在mysqli_connect()之后要设置连接字符编码mysq ...
- 悼念512汶川大地震遇难同胞——老人是真饿了 hdu 2187
在此对 曾经 努力参加 救援的人 致以深深的敬意 . 这一道题 挺简单的 就是简单的 结构体+贪心 而已 不过 用英文 注释 是一个 很大的 进步 , 以后 要习惯 http://acm.hdu ...
- 第一个只出现一次的字符--java实现
/** * 主要思想是通过数组来保存每个字符的出现次数,数组访问O(1),所以总时间复杂度可以保持O(n),通过两次遍历可以解决问题 * @param ch * @return */ public s ...
- 【Leetcode】115. Distinct Subsequences
Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...
- EF 新增数据时提示it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element
it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionM ...
- 将电脑浏览器User-Agent识别改成手机浏览器UA几种简单方法
第一种方法:修改浏览器的快捷方式 右击桌面上的Chrome浏览器图标,在弹出的右键菜单中选择“复制”,复制一个图标副本到桌面.右击该副本,选择“属性”,打开相应的对话框,在“目标”文本框的字符后面添加 ...
- Canvas——基本入门
基本概念 1.canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今 ...