Description
Set P (n) to the number of different paths from (0,0) to the point (n,0), moving in the following three ways: (x, Y), (x+1,y-1), (x, y), (X+1,y), (X+y), And the path cannot intersect with the fourth quadrant. P (n) is obtained and the 10^9+7 is modeled.
Input
The first line is an integer t that represents the number of data groups.
For each set of data, one line is an integer n.
Output
For each set of data, output the answer.
Data Range
20%:n≤10;
50%:n≤10000;
100%:n≤106,t≤10.
Solution
20%
Direct O (3^n*n) is a brute force enumeration.
50%
Consider the point of the first line y=0, assuming it is (i,0).
Then the path from (to i-1,1) is above the line Y=1, obviously there are P (i-2) species. Similarly, the path from (x,0) to (n,0) is above the line y=0, with the P (n-i) species.
So, all together you can get P (n) = Σni=1 P (i-2) p (n-i), wherein the specified p ( -1) =p (0) = 1.
In fact, you can make a table on the basis of 20%, the examination room has more than 5 people to write ...
100%
Assuming that there is a total of I (x, y) (x+1,y+1) in the path of the move, then there is bound to be an I (x, y) (x+1,y-1), n-2i (x, y) (x+1,y).
and if all (x, y)-x+1,y is not considered, then the number of paths is the number of Catalan, set to C_i. If you join (x, y) (X+1,y), which is to insert n-2i the same ball in 2i+1 gouges, the scheme number is C2iN.
So, P (n) = Σni=1 c_i C2in.
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace Std;
typedef long Long LL;
#define N 1000010
#define MOD 1000000007
int t;
int n;
LL F[n],d[n];
inline ll Qpow (ll A,ll b)
{
LL Ans=1;
while (b)
{
if (b&1)
Ans= (1ll*ans*a)%mod;
b>>=1;
A= (1ll*a*a)%mod;
}
return ans;
}
inline ll C (ll X,ll y)
{
return f[x]*d[y]%mod*d[x-y]%mod;
}
inline ll Catalan (ll x)
{
Return (C (x<<1,x)-C (x<<1,x-1) +mod)%mod;
}
int main ()
{
Freopen ("Move.in", "R", stdin); Freopen ("Move.out", "w", stdout);
scanf ("%d", &t);
F[0]=1;
for (int i=1;i<=1000000;i++)
F[i]=f[i-1]*i%mod;
for (int i=0;i<=1000000;i++)
D[i]=qpow (f[i],mod-2)%mod;
while (t--)
{
scanf ("%d", &n);
LL ans=0;
for (int i=0;i<=n;i+=2)
Ans+=c (n,i) *catalan (i>>1), ans%=mod;
printf ("%lld\n", ans);
}
return 0;
}
2.1 20%
†O(3N∗N)Åqþ=œ "
2.2 50%
ä1˜g† 'y= 0: §b´(I,0)"
@ol(1,1)(I−1,1)ƒm´»þ3† 'y= 1Þ §w,kP(I−
2)«"Ón§l(x,0)(N,0)ƒm´»þ3† 'y= 0Þ §kP(N−
I)«"
¤±§üüå5œ±P(N) =Pn i=1P(I−2)P(N−I)§ù¥§5
½P(−1) =P(0) = 1"
2.3 100%
B£ä´»¥˜kI‡(x, y)−>(x+ 1, y+ 1)§@oò˜½¬
KI‡(x, y)−>(x+ 1, y−1)§N−2I‡(x, y)−>(x+ 1, y)"
Xjø ä¤k(x, y)−>(x+ 1, y)§@o´»«êò´
1I‡CatalanʧCI"Xj\\(x, y)−>(x+ 1, y)§ ò´32I+ 1‡
˜?¥\N−2IƑó¥§ Yê´C2I
N"
¤±§P(n) = pn i=1 Ci Cn 2i"
"Hnoi Analog by YMD" move