日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 474D】Flowers (线性dp)

發(fā)布時(shí)間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 474D】Flowers (线性dp) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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 (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 for example he can't eat (WWWR).

題目大意:

把紅花和白花擺成一排,并且要求若出現(xiàn)白花,它們連續(xù)的數(shù)量必須是k的倍數(shù)。

給我們 n,接下來(lái)的n次詢(xún)問(wèn)。和k???(n,k<100000)

每行 一個(gè) a和b。

設(shè)f(k)為長(zhǎng)度為k的滿(mǎn)足條件的一排花 的 可能的數(shù)量

最后求f(a)+f(a+1)+...+f(b)

(不要理解成給定一個(gè)區(qū)間一共b個(gè),去求a到b的,而是一共a個(gè),一共a+1個(gè)....一共b個(gè)? 的可能情況的和)

另一個(gè)題目大意:

話說(shuō)某個(gè)幸運(yùn)的小伙伴X拿到了kevin女神送的蛋糕,然而他的吃法非常奇特,他獨(dú)創(chuàng)了兩種吃蛋糕的辦法:一、一次吃一整個(gè)蛋糕;二、一次吃k個(gè)蛋糕。

那么,當(dāng)?shù)案鈹?shù)量為x1到x2之間時(shí),一共能有幾種不同的吃法呢?

由于答案很大,輸出結(jié)果mod?1000000007的值

解題報(bào)告:

? ?直接dp就完事了,然后再維護(hù)一個(gè)前綴和。如果按照第一種題意,那就看AC代碼2,第二種題意就看AC代碼1。但是第一種題意可以轉(zhuǎn)化成第二種,因?yàn)椴还苁裁椿ㄆ鋵?shí)可以看成是同一種,只不過(guò)有兩種擺法就是了。

AC代碼1:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; ll dp[MAX],ans[MAX]; const ll mod = 1e9 + 7; int main() {int t,k;cin>>t>>k;dp[0]=1;for(int i = 1; i<MAX; i++) {dp[i] = dp[i-1];if(i >= k) dp[i] += dp[i-k];dp[i]%=mod;}int x,y;for(int i = 1; i<MAX; i++) {ans[i] = ans[i-1] + dp[i];}while(t--) {scanf("%d%d",&x,&y);printf("%lld\n",(ans[y] - ans[x-1] + mod)%mod);}return 0 ;}

AC代碼2:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; ll dp[MAX][2],ans[MAX];//dp[][0]代表紅花,dp[][1]代表白花 const ll mod = 1e9 + 7; int main() {int t,k;cin>>t>>k;dp[0][0]=1;dp[0][1]=0;for(int i = 1; i<MAX; i++) {dp[i][0] = dp[i-1][0];dp[i][1] = dp[i-1][1];if(i >= k) dp[i][1] += dp[i-k][0] + dp[i-k][1];dp[i][1]%=mod;dp[i][0]%=mod;}int x,y;for(int i = 1; i<MAX; i++) {ans[i] = ans[i-1] + dp[i][0] + dp[i][1];}while(t--) {scanf("%d%d",&x,&y);printf("%lld\n",(ans[y] - ans[x-1] + mod)%mod);}return 0 ;}

?

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 474D】Flowers (线性dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。