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

8 篇文章 0 订阅
1 篇文章 0 订阅

题目描述

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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值