HDU 5698 instantaneous movement (2016 "Baidu Star"-Preliminary round (Astar round2b) 1003)

Source: Internet
Author: User

Portal
Move instantly

Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 66

Problem Description
There is an infinitely large rectangle, initially when you are in the upper left corner (that is, the first column of the first row), each time you can select a lower right lattice, and teleport past (such as the red lattice from the direct blink to the blue lattice), the nth row m column lattice has several options, the answer to 1000000007 modulo.

Input
Multiple sets of test data.

Two integers n,m (2≤n,m≤100000)

Output
An integer representing the answer

Sample Input
4 5

Sample Output
10

Source
2016 "Baidu Star"-Preliminary round (Astar round2b)

Problem Solving Ideas:
First of all we analyze the problem is that we have to go from (1, 1) point to (N, m) point, and can only go to the lower right, then we are the equivalent from (n+m-4) a lattice to choose n-2 (or m-2) lattice, because we have to remove the n-1 row and m-1 column (this cannot be chosen), Then the first row and the first column are removed, so that is C (m+N?4,N+2)= (n+m? ) 4 )!(N?2)!?(m?2)!
Because this number to the MoD to take the remainder, then we need to ask (N-2)!* (m-2)! About the MoD's inverse, this actually a loop on the line, each time the expansion of Euclid, on the OK, and finally the code,
The first step: write an extended Euclidean algorithm, the inverse of a about B;
The second step: according to the formula, step by step, seek First (n+m-4)! Modulo mod, and then for the loop to find the inverse of two factorial
Step three: Output results.

On the code:

#include <iostream>Using namespace Std;typedef long Long ll;const LL MOD =1000000007void Exgcd (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 () {LL n,m; while(cin>>n>>m) {LL ans =1; for(LL i=1; i<=n+m-4; i++) ans = (ans*i)%mod; for(LL i=1; i<=n-2; i++) {LLx,y; EXGCD (i, MOD,x,y);x= (x%mod+MOD)%mod; Ans = (ans*x)%mod; } for(LL i=1; i<=m-2; i++) {LLx,y; EXGCD (i, MOD,x,y);x= (x%mod+MOD)%mod; Ans = (ans*x)%mod; } ans = (ans%mod+MOD)%mod;    cout<<ans<<endl; }return 0;}

HDU 5698 instantaneous movement (2016 "Baidu Star"-Preliminary round (Astar round2b) 1003)

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.