[Super Code Power Online Programming Contest Preliminary (2)] 4. Xiaoqi’s Pyramid Schroder Number (Super Cattelan Number)

Topic link: The Pyramid of Xiaoqi

Title

Topic picture
Xiaoqi can move between different points, suppose Xiaoqi is now at (x 1, y 1) {(x_1,y_1)}(x1,and1) , The next point he can move to(x 2, y 2) {(x_2,y_2)}(x2,and2)满足 ( x 2 > = x 1 & & y 2 > = y 1 ) {(x_2>=x_1 \&\& y_2>=y_1)} x2>=x1&&y2>=and1) . Now Xiaoqi stays at(k, k) ((k,k))(k,At k ) , since we are not sure where Xiaoqi is now, you need to ask for the sum of the number of plans reached at all points.

answer

You can do this question if you dabble in Cattleya numbers.
It's a little bit difficult to push directly. In the game, I feel that it is unlikely that I will push it out.

In this question, Xiaoqi can walk three points (x 1 + 1, y 1), (x 1, y 1 + 1), (x 1 + 1, y 1 + 1) {(x_1+1,y_1), (x_1 ,y_1+1), (x_1+1,y_1+1)}(x1+1,and1)(x1,and1+1)(x1+1,and1+1 ) , which is exactly the same as the example of Schroder number (super Cattelan number). Since the entire grid is triangular, the rightmost is(k, k) {(k,k)}(k,k ) is equivalent to replacing "y=x this straight line". In this way, we can easily get the answer to Schroder number (Super Cattelan number).

The recurrence formula of Schroder number (super Cattelan number):
(n + 1) ∗ S n = (6 n − 3) ∗ S n − 1 − (n − 2) ∗ S n − 2 {(n+ 1)*S_n=(6n-3)*S_{n-1}-(n-2)*S_{n-2}}(n+1)Sn=( 6 n3)Sn1(n2)Sn2 S 0 = 1 , S 1 = 1 {S_0=1,S_1=1} S0=1,S1=1 )
Because of the modulus, findsn {s_n}snWhen, both sides are multiplied by the inverse of (n+1).

Note the sequence S n {S_n} handed out hereSnIt is not directly the Schroder number (Super Cattelan number), but except for the first term, the rest is half of the Schroder number (Super Cattelan number), so remember to multiply by 2.

Note that n≠k.size() in the title. At first, I kept pressing n to write and caused a segfault, and finally changed to k.size() .

Code

typedef long long ll;
ll mod=1e9+7;

class Solution {
    
    
public:
	/**
	 * @param n: The number of pyramid levels n
	 * @param k: Possible coordinates k
	 * @return: Find the sum of the number of plans
	 */
	 ll f[10000010];
	ll qpow(ll a, ll b)
	{
    
    
		ll ans=1;
		while(b)
		{
    
    
			if(b&1) ans=ans*a%mod;
			a=a*a%mod;
			b>>=1;
		}
		return (ans+mod)%mod;
	}
	int pyramid(int n, vector<int> &k) {
    
    
		// write your code here
		f[1]=f[0]=1;
		for(int i=2;i<=n;i++)
		{
    
    
			f[i]=((6*i-3)*f[i-1]%mod-(i-2)*f[i-2]%mod+mod)%mod*qpow(i+1,mod-2)%mod;	
		}
		ll ans=0;
		for(int i=0;i<k.size();i++) 
		{
    
    
			if(n==k[i]) ans=(ans+f[n-k[i]])%mod;
			else ans=(ans+f[n-k[i]]*2%mod)%mod;
		}
		return ans;
	}
};

Guess you like

Origin blog.csdn.net/weixin_44235989/article/details/108329386