求大组合数(卢卡斯定理) 牛客2018暑假多校第八场G题

题目描述

Niuniu likes mathematics. He also likes drawing pictures. One day, he was trying to draw a regular polygon with n vertices. He connected every pair of the vertices by a straight line as well. He counted the number of regions inside the polygon after he completed his picture. He was wondering how to calculate the number of regions without the picture. Can you calculate the number of regions modulo 1000000007? It is guaranteed that n is odd.

输入描述:

The only line contains one odd number n(3 ≤ n ≤ 1000000000), which is the number of vertices.

输出描述:

Print a single line with one number, which is the answer modulo 1000000007.

输入

3

输出

1

输入

5

输出

11

通过找规律分析可以得到公式,n个点构成的图形最多的区域数C_{n}^{4}\textrm{}+C_{n-1}^{2}\textrm{}

卢卡斯定理:用来求C_{n}^{m}\textrm{}对P(素数)取模以后的结果

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL m,n,p;
LL Pow(LL a,LL b,LL mod){
    LL ans=1;
    while(b){
        if(b&1){
            b--;
            ans=(ans*a)%mod;
        } else{
            b/=2;
            a=(a*a)%mod;
        }
    }
    return ans;
}
LL C(LL n,LL m){
    if(n<m)
        return 0;
    LL ans=1;
    for(int i=1;i<=m;i++){
        ans=ans*(((n-m+i)%p)*Pow(i,p-2,p)%p)%p;
    }
    return ans;
}
LL Lucas(LL n,LL m){
    if(m==0)
        return 1;
    return (Lucas(n/p,m/p)*C(n%p,m%p))%p;
}
int main() {
    p = 1000000007;
    scanf("%lld",&n);
    printf("%lld\n",(Lucas(n,4) + Lucas(n-1,2))%p);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Adusts/article/details/81588227
今日推荐