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 size k.
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 modulo 1000000007 (109 + 7).
Input
Input contains several test cases.
The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases.
The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the 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 (109 + 7).
Examples
Input
3 2
1 3
2 3
4 4
Output
6
5
5
Note
For K = 2 and length 1 Marmot can eat ®.
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 for example he can’t eat (WWWR).
这个题我吐了,照着翻译读了半天的题目才明白啥意思,就是排WR两种字符,只不过 W只能成组的排在一起,这个题我想肯定得dp 自己在这道做题中出现了很多的错误;
以下是AC代码:
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
long long a[100050];
long long s[100050];
int main()
{
int n, k; cin >> n >> k;
for (int i = 1; i <= 100001; i++)
{
if (i < k) a[i] = 1;
a[k] = 2;
if (i > k)
{
a[i]=(a[i - 1] + a[i-k])% 1000000007;
}
}
for (int i =1; i <= 100001; i++)
{
s[i] = (s[i-1]+a[i])% 1000000007;
}
while (n--)
{
int x, y; cin >> x >> y;
cout <<(s[y]-s[x-1]+ 1000000007)% 1000000007 << endl;
}
}
**```
我在后边说一下自己的错误
1.第一次错 超时问题,我当时没想到要把和求到数组里去 我想的就是 一遍一遍的加,哇这就造成了超时问题
2.这个s的数组和好求出来但是 问题是我想要x-y的和,要用s【y】-s【x-1】
因为 s[x]=s[x-1]+a[x];s[x-1]=s[x-2]+s[x-1]看见了没 s[x]要是被剪掉了 a[x]
就木有了
3.最后输出结果问题 ,我没加上那个数,为什么加这个数呢
原因就是 你之前的结果都对它求模了 所以 可能后边的变小了。。**