[CSP-S Simulation Test]: permutations (or find a mathematical law)

Title Description

$ T $ sets of data, each given n-$ $, requesting the value of the following formula, of $ 7 $ 10 ^ 9 + modulo:

$$C_n^0\times C_n^0+C_n^1\times C_n^1+C_n^2\times C_n^2+...+C_n^n\times C_n^n$$


Input Format

A first line integer $ T $, represents the number of data sets.
Then $ T $ rows, each comprising a n-$ $ integer, such as the title meanings shown.


Output Format

$ T $ output lines, each comprising an integer representing $ 10 to $ ^ 7 + 9 taken after the answer mode.


Sample

Sample input:

2
1
2

Sample output:

2
6


Data range and tips

For $ 30 \% $ data, $ T \ leqslant 500, n \ leqslant 10,000 $.
For $ 100 \% $ data, $ T \ leqslant 100,000, n \ leqslant 1,000,000 $.


answer

I wish you all a happy National Day, Happy training!

Hit the table to find the law can be found in the answer is actually $ C_ {2n} ^ n $, so we are now speaking about this in the end is why.

First in terms of a story:

Front of my house there are two trees, one is the jujube, jujube is another one. - Lu Xun

Front of my house there are two jujube, there is an $ n $ particles jujube jujube trees, the other one also has $ n $ particles jujube jujube trees. - $ HEOI- $ Dodo

Ma Ma let me pick $ n $ jujube trees in this two jujube, how many programs there are a few do?

Abstract We assume $ I $ particles on a first jujube tree, then another tree to pick $ $ Ni particles date, program number is: $ C_n ^ i \ times C_n ^ {ni} $, then the total program is the number of $ \ sum \ limits_ {i = 0} ^ n C_n ^ i \ times C_n ^ {ni} $.

However, the date is the same, the tree is the same, then the number of programs can also be written: $ C_ {2n} ^ n $.

That is $ C_ {2n} ^ n = \ sum \ limits_ {i = 0} ^ n C_n ^ i \ times C_n ^ {ni} $.

We also know $ C_n ^ i = C_n {ni} $, then the formula becomes: $ C_ {2n} ^ n = \ sum \ limits_ {i = 0} ^ n {(C_n ^ i)} ^ 2 $.

那么$C_n^0\times C_n^0+C_n^1\times C_n^1+C_n^2\times C_n^2+...+C_n^n\times C_n^n=C_{2n}^n$。

I finished the story, the children understand about the Well ~

Time complexity: $ \ Theta (T \ times \ log_ {mod} n) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int n;
long long jc[2000001],inv[2000001];
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<=2000000;i++)
		jc[i]=jc[i-1]*i%mod;
	inv[2000000]=qpow(jc[2000000],1000000005)%mod;
	for(long long i=2000000;i>0;i--)
		inv[i-1]=inv[i]*i%mod;
}
long long get_C(int x,int y){return jc[x]*inv[y]%mod*inv[x-y]%mod;}
int lucas(int x,int y)
{
	if(!y)return 1;
	return get_C(x%mod,y%mod)*lucas(x/mod,y/mod)%mod;
}
int main()
{
	pre_work();
	int T;scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		printf("%d\n",lucas(n<<1,n));
	}
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11616116.html