Cattle off winter training camp 5 - F

Topic Portal
went to the happy two hours through 6 three hours a link title card title ... (too dishes) QWQ

What does all this all because what is it?
Here Insert Picture Description
TA This small (zhi) machine (zhang) Spirit (er) Ghost (tong) see this phrase lost !!!

The first (du) see (cuo) This question of the TA is very excited that this thing is not that a number of combinations! .. then wrote a long number of combinations (acuity of the DP is not enough) to write the number of combinations even if ... but the machine (yu) Chile (chun) of zbw also found that the complexity of the number of combinations is too high! (lr query is a layer of complexity for each query there is a r / k, even with the prefix and also len * r / k complexity of), no, finally write out the number of combinations can not just give up, so the machine (fan) Chile (er) of the TA zbw resorted to special skill -! you do not find the law !!! say really let zbw found a vague law:
Above:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

When k is equal to zbw see that when a series of familiar numbers 2, TA thrilled that this is not the legendary (magic) (ubiquitous) Fibonacci columns Well!

When zbw see k is equal to that of a series of similar numbers 3 and 4, he feels that he has no (bing) person (ru) energy (gao) enemy (huang) it! Is not that the result obtained by adding the number of k Well! nothing but wrong k-2 bits.

So he wrote the law of TA a n s [ l + k + k 2 ] = i = l l + k 1 a n s [ i ] years [t + k + k-2] = \ sum ^ {l + k-1} _ {i = l} years [i]

So let zbw wrote on TA looking for a three-hour BUG code:

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define MAX 200020 
#define ll long long
#define mod 1000000007 
using namespace std;
ll sum[MAX];
ll jc[MAX];
ll ans[MAX];
int k,n;
void getjc()
{
	jc[0]=1;
	for(int i=1;i<MAX;i++)
	{
		jc[i]=(jc[i-1]*i)%mod;
	}
}

ll mul(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b%2)
			ans=(ans*a)%mod;
		b>>=1;
		a=(a*a)%mod;
	}
	return ans;
}

ll getinv(ll x)
{
	return mul(x,mod-2);
}

ll getans(int num)
{
	ll ans=0;
	for(int i=0;i*k<=num;i++)
	{
		int rj=i;
		int ac=num-i*k;
		ll k=(jc[ac+rj]*getinv(jc[ac]))%mod;
		k=(k*getinv(jc[rj]))%mod;
		ans=(ans+k)%mod;
	}
	return ans;
}

int main()
{
	getjc();
	cin>>k>>n;
	for(int i=1;i<=k+k-2;i++)
	{
		ans[i]=getans(i);
	}
	ll now=0;
	for(int i=1;i<=k;i++)
	now=(now+ans[i])%mod;
	for(int i=1;i+k+k-2<MAX;i++)
	{
		ans[i+k+k-2]=now;
		now=((now-ans[i])%mod+mod)%mod;
		now=(now+ans[i+k])%mod;
	}
	sum[0]=0;
	for(int i=1;i<MAX;i++)
	{
		sum[i]=(sum[i-1]+ans[i])%mod;
	}
	for(int i=0;i<n;i++)
	{
		int l,r;
		cin>>l>>r;
		cout<<((sum[r]-sum[l-1])%mod+mod)%mod<<endl;
	}

	return 0;
}

Wait until zbw reading positive solution, the people are the collapse of the ... (DP really is a science)

AC Code:

#include <iostream>
#include <algorithm>
#include <cstring>
#define MAX 100005
#define mod 1000000007
using namespace std;
typedef long long ll;
int dp[MAX][4]={0};
int sum[MAX];
int main() {
    int q,k;
  
    scanf("%d%d",&k,&q);
    dp[0][0]=1;
    dp[0][1]=0;
    for(int i=1;i<MAX;i++)
    {
        if(i>=k)
        dp[i][1]=dp[i-k][0];
        dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
    }
    sum[0]=0;
    for(int i=1;i<MAX;i++)
    {
        sum[i]=(sum[i-1]+(dp[i][0]+dp[i][1])%mod)%mod;
    }
    for(int i=0;i<q;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",((sum[b]-sum[a-1])%mod+mod)%mod);
    }
    return 0;
}
}

Posted official explanations:
Here Insert Picture Description

Do title 10 million, reading the first question, the question is not standardized reading, two lines of relatives of tears !!!

Published 30 original articles · won praise 9 · views 1296

Guess you like

Origin blog.csdn.net/Zhang_sir00/article/details/104303980