51nod 1120 Robot Walk Square V3 Cattleya number Lucas theorem

Source: Internet
Author: User

n * n Squares, draw a line from top left to bottom right. A robot moves from top to bottom to the right and can only go right or down. and ask only on the line above or below to walk, cannot cross this line, how many different way? Because the number of methods can be large, you only need to output mod 10007 results.

 Input
Enter a number n (2 <= n <= 10^9).
Output
The number of output methods Mod 10007.
Input Example
4
Output Example
10


Obviously a Cattleya number, launched ans = C (2*n-2,n-1) * 2/n% MOD
First let n--, ans = C (2*n,n) * 2/(n+1)% MOD so the formula looks good.

Since n <= 10^9, but MOD = 10007, it is necessary to use the Lucas theorem for Ans


                                              //File Name:nod1120.cpp//Author:long//Mail: [email protected]//Created time:2016 May 27 Friday 15:46 58 seconds#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#defineLL Long Longusing namespacestd;Const intMOD =10007; LL Jie[mod]; ll QP (ll X,ll y) {ll res=1;  while(y) {if(Y &1) Res = res * x%MOD; X= x * x%MOD; Y>>=1; }    returnRes;}voidinit () {jie[0] =1;  for(intI=1; i<mod;i++) Jie[i]= jie[i-1] * I%MOD;} LL Get_c (intXinty) {    if(Y = =0|| y = = x)return 1; returnJIE[X] * QP (jie[y] * jie[x-y]% Mod,mod-2) %MOD;} ll Lucas (ll X,ll y) {ll ans=1; intu,v;  while(X >0|| Y >0) {u= x%MOD; V= y%MOD; Ans= ans * get_c (u,v)%MOD; X/=MOD; Y/=MOD; }    returnans;}intMain () {init (); intN;  while(~SCANF ("%d",&N)) {N--; printf ("%d\n",(int) Lucas (2*n,n) * QP (n+1, mod-2) *2%MOD); }    return 0;}






51nod 1120 Robot Walk Square V3 Cattleya number Lucas theorem

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.