Codeforces Round #271 (Div. 2) d.flowers DP

Source: Internet
Author: User

D. Flowers

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 being tasty, there is a rule:marmot wants to eat white flowers only in groups of size K.

Now Marmot wonders on how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (9 + 7).

Input

Input contains several test cases.

The first line contains integers t and k (1≤ t, k ≤105), where c9>T represents the number of test cases.

The next t lines contain II integers ai and b i (1≤ aibi ≤105), describing the C30>i-th test.

Output

Print T lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (9 + 7).

Sample Test (s) input
3 2
1 3
2 3
4 4
Output
6
5
5
Note
  • For K = 2 and length 1 Marmot can Eat (R).
  • For K = 2 and length 2 Marmot can eat (RR) and (WW).
  • For K = 2 and length 3 Marmot can eat (RRR), (RWW) and ( WWR).
  • For K = 2 and length 4 Marmot can eat, for example, (wwww) or (rwwr), but F or example he can ' t eat (wwwr).

Test Instructions : There is a person like to eat flowers, there are white flowers, safflower two flowers, but eat white can only eat k flower or not to eat flowers, now ask you to eat N Flower program number is how much.

Solution: dp[i] means to eat I flower scheme number, then dp[i+k]+=dp[i] to update eat a white flower, dp[i+1]+=dp[i] used to update to eat a red flower

///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineINF 0x7fffffffinline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//****************************************#defineMAXN 100000+5#defineMoD 1000000007intDP[MAXN];intMain () {intN=read (), k=read ();     MEM (DP); dp[0]=1;  for(intI=0; i<=100000; i++){           if(i+1<=100000) Dp[i+1]= (dp[i+1]+dp[i])%MoD; if(i+k<=100000) Dp[i+k]= (Dp[i+k]+dp[i])%MoD;     } ll SUM[MAXN];     MEM (sum); sum[0]=0;  for(intI=1; i<=100000; i++) {Sum[i]+=sum[i-1]+Dp[i]; }     intb;  for(intI=1; i<=n;i++) {scanf ("%d%d",&a,&b); cout<< (sum[b]-sum[a-1])%mod<<Endl; }  return 0;}
Code

Codeforces Round #271 (Div. 2) d.flowers DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.