51NOD 1120 Robot Walk Square V3 (Lucas theorem + non-descending path)

Source: Internet
Author: User

Transmission Door

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
Problem Solving Ideas:
From the top left to the right can only protect down to the right, is equivalent to go from the left down to the right of a non-descending path, and then add some obstacles in the middle, in fact, is from (1,1) Walk to (n,n) The point cannot touch the non-descending path of the diagonal. Direct Set formula:
2 n ?C (2?(N?1),N?1)
Because n Too big, and the number of modulo is not very big, it is easy to think Lucas Theorem. And then it's the code, it's all a routine, asking for a little bit more. < Span class= "Mrow" id= "mathjax-span-3233" >m o d Span style= "Display:inline-block; width:0px; Height:2.616em; " > About n The inverse of the element, but there is no inverse of the situation how to ask for it seems that there is no such data.

< Span class= "Mrow" id= "mathjax-span-3241" >m y Span style= "Display:inline-block; width:0px; Height:2.616em; " > Code :

/**2016-08-05 pm Author:itakmotto: Today I want to go beyond yesterday's I, tomorrow I want to surpass today's me, to create better code as the goal, constantly surpass oneself. **/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <set>using namespace STD;typedef Long LongLL;typedef unsigned Long LongULL;Const intINF =1e9+5;Const intMAXN =1e6+5;ConstLL MOD =1e4+7;Const DoubleEPS =1e-7;Const DoublePI =ACOs(-1);using namespace STD; ll Quick_mod (ll A, ll B, ll c) {ll ans =1; while(b) {if(B &1) ans = (ans*a)%c; b>>=1;    A = (a*a)%c; }returnAns;} LL FAC[MAXN];voidGET_FAC (LL m)///m!{fac[0] =1; for(intI=1; i<=m; i++) Fac[i] = (fac[i-1]*i)% M;} ll Lucas (ll N, ll M, ll P) {ll ans =1; while(n && m)        {LL a = n% P; LL b = m% p;if(A < b)return 0; Ans = ((ans*fac[a]%p) * (Quick_mod (fac[b]*fac[a-b]%p,p-2, p)))% p;        n/= p;    M/= p; }returnAns;}voidEXGCD (ll A, ll B, LL &x, ll &y) {if(b = =0) {x =1; y =0;return;    } LL x1, y1;    EXGCD (b, a%b, x1, y1);    x = y1; y = x1-(A/b) *y1;}intMain () {GET_FAC (MOD); LL N; while(Cin>>n) {LL x, y;        EXGCD (n, MOD, x, y);        x = (x%mod+mod)%mod; x<<=1LL LL ans = Lucas (2*n-2, N-1, MOD); Ans = (ans*x)%mod;cout<<ans<<endl; }return 0;}

51NOD 1120 Robot Walk Square V3 (Lucas theorem + non-descending path)

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.