Home Web Front-end HTML Tutorial Codeforces Round #271 (Div. 2) D. Flowers (recursive preprocessing)_html/css_WEB-ITnose

Codeforces Round #271 (Div. 2) D. Flowers (recursive preprocessing)_html/css_WEB-ITnose

Jun 24, 2016 am 11:56 AM
round preprocessing

We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of sizek.

Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo1000000007 (109? ?7).

Input

Input contains several test cases.

The first line contains two integers t andk (1?≤?t,?k?≤?105), wheret represents the number of test cases.

The next t lines contain two integers ai andbi (1?≤?ai?≤?bi?≤?105), describing the i-th test.

Output

Print t lines to the standard output. Thei-th line should contain the number of ways in which Marmot can eat betweenai andbi flowers at dinner modulo1000000007 (109? ?7).

Sample test(s)

Input

3 21 32 34 4
Copy after login

Output

655
Copy after login

Note

  • For K = 2 and length1 Marmot can eat (R).
  • For K = 2 and length2 Marmot can eat (RR) and (WW).
  • For K = 2 and length3 Marmot can eat (RRR), (RWW) and (WWR).
  • For K = 2 and length4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).

  • 考虑第n个。假如n是小于k的,那么只能都是是R,也就是只有一种情况。假如大于等于k,如果第n个是W,那么从n-k 1到n全部为W,如果第n个是R,那么数量就是前n-1个的数量。

    dp[n] = 1; (0<= n < k)

    dp[n] = dp[n-1] dp[n-k]; (n >= k)


    #include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <queue>#include <algorithm>#include <cmath>#define mem(f) memset(f,0,sizeof(f))#define M 100005#define mod 1000000007#define MAX 0X7FFFFFFF#define maxn 100005#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef long long LL;int n = maxn, k, t, a, b, dp[maxn], sum[maxn];int main(){    scanf("%d%d", &t, &k);    for(int i = 0; i < k; i++) dp[i] = 1;    for(int i = k; i < n; i++) dp[i] = (dp[i-1] + dp[i-k])%mod;    for(int i = 1; i < n; i++) sum[i] = (sum[i-1] + dp[i])%mod;    while(t--) {        scanf("%d%d", &a, &b);        printf("%d\n", ((sum[b]-sum[a-1])%mod+mod)%mod );    }    return 0;}
    Copy after login


    ??

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What does round mean in php What does round mean in php Mar 10, 2023 am 10:04 AM

    In PHP, round means "rounding" and is a built-in function that converts floating point numbers into integers. This function can round floating point numbers and return an integer value of type float. The syntax is "round(number, precision,mode);".

    How to divide and round using PHP's round() function How to divide and round using PHP's round() function Mar 21, 2023 pm 04:32 PM

    The round() function is a very useful function in the PHP number formatting library, which can round floating point numbers to a specified number of decimal places. However, since PHP's division operation may suffer from infinite decimals or loss of precision, rounding of the divisor is also necessary. Next, we will explain in detail how to use PHP's round() function to divide and round.

    Explore data cleaning and preprocessing techniques using pandas Explore data cleaning and preprocessing techniques using pandas Jan 13, 2024 pm 12:49 PM

    Discussion on methods of data cleaning and preprocessing using pandas Introduction: In data analysis and machine learning, data cleaning and preprocessing are very important steps. As a powerful data processing library in Python, pandas has rich functions and flexible operations, which can help us efficiently clean and preprocess data. This article will explore several commonly used pandas methods and provide corresponding code examples. 1. Data reading First, we need to read the data file. pandas provides many functions

    Data cleaning and preprocessing technology implemented using Java Data cleaning and preprocessing technology implemented using Java Jun 18, 2023 pm 01:45 PM

    With the popularity and use of data, data quality issues have also received increasing attention. Data cleaning and preprocessing are one of the key technologies to improve data quality. Data cleaning and preprocessing technology implemented using Java can effectively improve data quality and make data analysis results more accurate and reliable. 1. Data Cleaning Technology Data cleaning refers to processing errors, incomplete, duplicate or invalid data in the data, so as to better conduct subsequent data analysis and mining. Java provides a wealth of tools and libraries that can help us implement data

    How to use ROUND function to intercept decimal places in MySQL How to use ROUND function to intercept decimal places in MySQL Jul 13, 2023 pm 09:21 PM

    How to use the ROUND function in MySQL to intercept the number of decimal places. In MySQL, you can use the ROUND function to intercept the number of decimal places. The ROUND function rounds a number to a specified number of decimal places. The following will introduce you to the use of ROUND function in detail and provide code examples. Syntax: ROUND(X,D)X represents the number to be rounded, and D represents the number of decimal places to be retained. Example of using the ROUND function to intercept the number of decimal places: Suppose there is a table named produc

    What is the way to avoid SQL injection in PHP? What is the way to avoid SQL injection in PHP? Jun 30, 2023 am 09:57 AM

    How to deal with SQL injection problems in PHP? In recent years, with the rapid development of the Internet, the number of websites and applications has continued to increase, and one of the common development languages ​​​​is PHP. However, the use of PHP also raises some security issues, one of which is SQL injection. SQL injection attacks refer to hackers constructing malicious SQL statements to obtain, modify or destroy data in the database. In order to protect the security of websites and applications, developers need to take some measures to prevent the occurrence of SQL injection vulnerabilities. First, develop

    How to use PHP to implement data cleaning and preprocessing functions How to use PHP to implement data cleaning and preprocessing functions Sep 05, 2023 pm 12:52 PM

    How to use PHP to implement data cleaning and pre-processing functions When developing a website or application, data cleaning and pre-processing is one of the common tasks. Their purpose is to ensure that entered data meets specific standards and undergoes the necessary processing before being stored or used. PHP is a popular server-side programming language that provides a series of functions and tools to implement data cleaning and preprocessing functions. This article will discuss in detail how to implement data cleaning and preprocessing in PHP. Data cleaning Data cleaning refers to cleaning the input data

    What are macros in C programming language? What are macros in C programming language? Sep 05, 2023 am 11:29 AM

    Macro substitution is a mechanism that provides string replacement. It can be achieved through "#define". It is used to replace the first part of a macro definition with the second part before the program is executed. The first object can be a function type or an object. The syntax of the syntax macro is as follows: #definefirst_partsecond_part program In the program, every time first_part appears, it will be replaced by second_part. Online demonstration #include<stdio.h>#definesquare(a)a*aintmain(){intb,c;printf("

    See all articles