D. Flowers Codeforces Round #271 (div2 ),
D. Flowerstime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
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 eatAAndBFlowers. As the number of ways cocould be very large, print it modulo1000000007 (109 bytes + limit 7 ).
Input
Input contains several test cases.
The first line contains two integersTAndK(1 digit ≤ DigitT, Bytes,KLimit ≤ limit 105), whereTRepresents the number of test cases.
The nextTLines contain two integersAIAndBI(1 digit ≤ DigitAILimit ≤ limitBILimit ≤ limit 105), describingI-Th test.
Output
PrintTLines to the standard output.I-Th line shoshould contain the number of ways in which Marmot can eatAIAndBIFlowers at dinner modulo 1000000007 (109 bytes + limit 7 ).
Sample test (s) input
3 21 32 34 4
Output
655
Note
- ForK= 2 and length 1 Marmot can eat (R).
- ForK= 2 and length 2 Marmot can eat (RR) And (WW).
- ForK= 2 and length 3 Marmot can eat (RRR),(RWW) And (WWR).
- ForK= 2 and length 4 Marmot can eat, for example ,(WWWW) Or (RWWR), But for example he can't eat (WWWR).
State transition equation dp [I] = dp [I-1] + dp [I-k]. Pay special attention to the modulo operation.
Code:
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>const long long mod = 1000000007;const int maxn=100000;long long dp[maxn+100];long long sum[maxn+100];using namespace std;int main(){ int n,k; memset(sum, 0, sizeof(sum)); scanf("%d %d",&n,&k); sum[0]=0; dp[0]=1; for(int i=1;i<k;++i){ dp[i]=1; sum[i]=sum[i-1]+dp[i]; } for(int i=k;i<=maxn;++i){ dp[i]=dp[i-1]+dp[i-k]; dp[i]%=mod; sum[i]=sum[i-1]+dp[i]; sum[i]%=mod; } int a,b; for(int i=0;i<n;++i){ scanf("%d %d",&a,&b); printf("%I64d\n",(sum[b]-sum[a-1]+mod)%mod); } return 0;}