1119 Robot walks square V2 (Inverse Element, Yang Hui Triangle, Mathematics)

Base Time Limit: 1 second Space Limit: 131072 KB Score: 10Difficulty  : Level 2 Algorithm Questions
 collect
 focus on
In the square of M*N, a robot goes from the upper left to the lower right, and can only go right or down. How many different moves are there? Since the number of methods can be large, only the result of Mod 10^9 + 7 needs to be output.
Input
Line 1, 2 numbers M, N, separated by spaces. (2 <= m,n <= 1000000)
Output
Output the number of moves Mod 10^9 + 7.
Input example
2 3
Output example

3

AC: code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define Mod 1000000007
const int maxn = 2000003 ;
ll vis[maxn];
 
void init()
{
    vis[0] = 1;
    for(ll i = 1;i<maxn;i++) vis[i] = (vis[i-1]*i)%Mod;
}
ll e_gcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = e_gcd(b,a%b,x,y);
    ll temp = x;
    x = y;
    y = temp-a/b*y;
    return ans;
}
ll inv(ll s)
{
    ll x,y;
    e_gcd(s,Mod,x,y);
    return (x+Mod)%Mod;
}
intmain()
{
    ll a,b;
    init();
    scanf("%lld %lld",&a,&b);
    ll c,d;
    c = a+b-2;
    d = a -1;
    printf ("% lld \ n", (vis [c]% Mod * inv (vis [cd])% Mod * inv (vis [d])% Mod));
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325946936&siteId=291194637