HDU 4828 Grids (Cattleya number + multiplicative inverse)

Source: Internet
Author: User
Tags gcd

First of all, I follow my understanding of why it is Cattleya number, first Cattleya number has a very typical application is to ask 1~n a natural number of the number of types of stack. And this is exactly the case. We have to meet the conditions given in the topic, the number should be placed from small to large, 1 certainly in the upper left corner, so 1 into the stack, this time we put 2, if we put 2 in 1 of the following represents the 1 out of the stack, put 2 on the above represents 2 also into the stack (you can see the second set of hint in the sample hint), and so on, So to put the number, exactly the corresponding line into the stack, the following line out of the stack, a total of n rows, corresponding to the upper limit of n Cattleya number.

Need to pay attention to the place is in the use of Cattleya recursion, division is not followed by the same residual film theorem, so need to use the multiplication inverse, set we want to divide the number of n, take the membrane for mod, then n multiplication inverse is, when N and mod coprime, through Euclid theorem n*x + y*mod = gcd ( N,M) get x, Treat X as (X%mod + MoD)% mod form, which is the multiplication inverse we want.

The code is as follows:

  

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;#defineMAXN 1000010#defineMoD 1000000007#defineLL Long LongLL ktl[maxn],x,y; ll EXGCD (ll A,ll b) {if(b = =0) {x=1; Y=0; returnA; } LL gcd= EXGCD (b,a%b);    LL tmp; TMP=x; X=y; Y= tmp-a/b *y; returnGCD;} LL Yiyuan (intN) {LL GCD=EXGCD (N,MOD); if(GCD = =1)    return(X%mod + MoD)%MoD;}voidinit () {memset (KTL,0,sizeof(KTL)); ktl[1] =1;  for(inti =2; I <= maxn-Ten; i++) {Ktl[i]= (ktl[i-1]*(4*i-2)%mod * Yiyuan (i+1)) %MoD; }}intMain () {intT,n,ca =0;    Init (); scanf ("%d",&t);  while(t--) {scanf ("%d",&N); printf ("Case #%d:\n",++CA); printf ("%i64d\n", Ktl[n]); }    return 0;}

HDU 4828 Grids (Cattleya number + multiplicative inverse)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.