Counting regions(图论+数论)

原题链接:G-Counting regions_2022牛客五一集训派对day1 (nowcoder.com)
 

题目描述

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.

示例1

输入

3

输出

1

示例2

输入

5

输出

11

有这样一个公式:
Counting regions(图论+数论)_第1张图片
欧拉公式:V-E+F=2
V=4 the number of vertices
E=5 the number of edges
F=3 the number of regions(这里包括外面的大区域)

根据欧拉公式我们要求的是F-1,所以我们只需要把V和E先求出来,由于点与线的关系,我们肯定是先求点再求线,因为没有点也就没有线.

对于这个题目:

V:首先是外面一圈的点也就是n,然后对角线会有交点从n个点中选取4个点形成两条对角线交于一点,也就是C[n][4],C[n][4]表示组合数.那么点的总和就是n+C[n][4].

E:首先对于其中n个点可以连成C[n][2]条边,并且对角线对角线增加两条边的组数为C[n][4],那么边的总数就是C[n][4]*2+c[n][2].

最终要求的答案就是:C[n][4]+C[n][2]-n+1

n的数值很大,不可能直接用阶乘来存,并且答案要模上mod,这里要用到快速幂和费马小定理

费马小定理结论:结论是若存在整数 a , p 且gcd(a,p)=1,即二者互为质数,则有a(p-1)≡ 1(mod p)。(这里的 ≡ 指的是恒等于,a(p-1)≡ 1(mod p)是指a的p-1次幂取模与1取模恒等),再进一步就是ap≡a(mod p)。

其实就是(a/b)%p等于a*ksm(b,p-2,p)%p;

这里n为奇数,一定满足费马小定理a与p互质,所以有:

C[n][4]=n*(n-1)%mod*(n-2)%mod*(n-3)%mod*ksm(4!,mod-2,mod)%mod

C[n][2]=n*(n-1)%mod*(2!,mod-2,mod)%mod

上代码:

#include
using namespace std;
typedef long long LL;
const LL mod=1000000007;//这里一定要开LL,不然int这题只能过40%
LL ksm(LL a,LL b,LL c)
{
    LL ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%c;
        b>>=1;
        a=a*a%c;
    }
    return ans;
}
int main()
{
    int n;
    cin>>n;
    LL c1=1,c2=1;
    for(int i=n;i>=n-3;i--) c1=c1*i%mod;
    c2=n*(n-1)%mod;
    LL temp1=ksm(24,mod-2,mod),temp2=ksm(2,mod-2,mod);
    LL ans=c1*temp1%mod+c2*temp2%mod-n%mod+1%mod;
    printf("%lld",(ans+mod)%mod);
    return 0;
}

 

你可能感兴趣的:(图论)