【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());
        }
    }
}

转载于:https://www.cnblogs.com/AWCXV/p/10363876.html

你可能感兴趣的:(【Codeforces 474D】Flowers)