[CSP-S simulation test]: sum (Mo + math team)

Topic Portal (internal title 63)


Input Format

The first line there is an integer $ id $, represents the number of test points.
The first line has an integer $ q $, represents a challenge group.
Then there $ Q $ rows, each row having two integers $ n_i, m_i $.


Output Format

A total of $ Q $ rows, each row represents an integer answer each inquiry $ S_ {n_i, m_i} $ 10 to $ ^ 7 + 9 $ result obtaining mode.


Sample

Sample input:

1
5
1 1
2 1
3 2
4 3
5 5

Sample output:

2
3
7
15
32


Data range and tips

For all data, $ 1 \ leqslant q, n_i, m_i \ leqslant 10 ^ 5 $.


answer

$ 80 $ examination room on the sub-sub-section are full of water, the thought of Mo Leng Shimo team ......

Consider first of all inquiries should equal $ $ n_i how to do, can be pretreated, consider $ S_ {n, m-1} $ how to transfer $ S_ {n, m} $, is nothing more than plus $ C_n ^ m $ can not repeat them.

Now consider all queries should be equal to $ $ m_i how to do, clearly pre-treatment is not so simple, considering $ S_ {n-1, m} $ how to transfer $ S_ {n, m} $, since the number of combinations can be used Pascal's Triangle push, may wish to draw Pascal's triangle.

For convenience, I will only draw the two of behavior in cases of Pascal's Triangle

Let $ 1 $ dots of the first line $ n-1 $ line, $ 4 $ dot row header $ n-$ line, using the properties of Pascal's triangle, numbered $ 4 $ points equal to number of $ 1 $ point number equal to the number of $ 5 $ points from $ 1 $ points and the number of $ 2 $ point sum, number of $ 6 $ point equal number of $ 2 $ points and numbered points $ 3 $ additivity.

Can also be found, when transferred from the $ n 1-$ row to $ n $ lines in addition to the $ 3 $ dot other points have been added to the $ 2 $ times, only $ 3 $ dot increase of only $ 1 $ times, then we It can be derived $ S_ {n, m} = S_ {n-1, m} * 2-C_ {n-1} ^ m $, empathy $ S_ {n-1, m} = \ frac {S_ {n , m} + C_ {n-1} ^ m} {2} $.

Using this property we can solve this problem child.

Come to these properties, we can consider the team Mo algorithm, $ m $ equivalent of $ l $, $ n $ equivalent of $ r $, this question will be solved.

Time complexity: $ \ Theta (n \ sqrt {n}) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
struct rec{int n,m,id,pos;}e[100001];
const int mod=1000000007;
const int inx=500000004;
int q;
long long ans[100001];
long long jc[100001],inv[100001];
long long qpow(long long x,long long y)
{
	long long res=1;
	while(y)
	{
		if(y%2)res=res*x%mod;
		y>>=1;
		x=x*x%mod;
	}
	return res;
}
void pre_work()
{
	jc[0]=1;
	for(long long i=1;i<=100000;i++)
		jc[i]=jc[i-1]*i%mod;
	inv[100000]=qpow(jc[100000],mod-2);
	for(int i=100000;i;i--)
		inv[i-1]=inv[i]*i%mod;
}
long long get_C(long long x,long long y){return jc[x]*inv[y]%mod*inv[x-y]%mod;}
long long lucas(long long x,long long y)
{
	if(!y)return 1;
	return get_C(x%mod,y%mod)*lucas(x/mod,y/mod)%mod;
}
bool cmp(rec a,rec b){return (a.pos)^(b.pos)?a.m<b.m:(((a.pos)&1)?a.n<b.n:a.n>b.n);}
int main()
{
	pre_work();int mxn=0;
	scanf("%d%d",&q,&q);
	for(int i=1;i<=q;i++)
	{
		scanf("%d%d",&e[i].n,&e[i].m);
		mxn=max(mxn,e[i].n);e[i].id=i;
	}
	int t=sqrt(mxn);
	for(int i=1;i<=q;i++)e[i].pos=(e[i].m-1)/t+1;
	sort(e+1,e+q+1,cmp);
	int m=0,n=0;
	long long now=1;
	for(int i=1;i<=q;i++)
	{
		while(n<e[i].n)now=(now*2%mod-lucas(n++,m)+mod)%mod;
		while(m<e[i].m)now=(now+lucas(n,++m))%mod;
		while(m>e[i].m)now=(now-lucas(n,m--)+mod)%mod;
		while(n>e[i].n)now=(now+lucas(--n,m))*inx%mod;
		ans[e[i].id]=now;
	}
	for(int i=1;i<=q;i++)printf("%lld\n",ans[i]);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11636670.html
Recommended