(割圆问题与费马小定理)牛客多校8 G题

本文探讨了一个数学竞赛题目,该题要求计算正n边形中由顶点连线形成的区域数目,并提供了解决方案。利用割圆问题的概念及费马小定理进行组合数的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://www.nowcoder.com/acm/contest/146/G
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

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

备注:

 

The following picture shows the picture which is drawn by Niuniu when n=5. Note that no three diagonals share a point when n is odd.

题目的大意是给出一个n,表示正n边型。让每个顶点两两相连求一共分割了多少块领域。

此题是图论里的割圆问题,割圆问题既在圆的的边界上选取n个点并且两两相连,求分割了多少块领域,与此题相似。割圆问题的公式如下图:

 

 

 

 

 

 

 

 

 

 

 

 

这个公式实际上包括了几条弦分割的圆的领域,可以看出n个点就有n条弦从而对圆有n个区域的划分。我们求得是内部多边形的分割数量所以要减去弦的分割。

那么最终的公式应该是:1+C(n,2)+C(n,4)-n;

这里强烈推荐3B1B的一个视频,详细的解释了割圆问题。https://www.bilibili.com/video/av19849697

不过呢,其中还有一个问题,那就是求组合数的时候有除法运算,直接取余是不行的,所以我们要用费马小定理来求分母的逆元。虽然我之前写过费马小定理的博客但是还是在这里再写一遍。

费马小定理:若p是素数那么a^p=a(mod p)   

推论 :a^(p-1)=1(mod p)   我们要求的是逆元,逆元就是某个数的倒数(粗俗的理解)  a的逆元就是1/a。那么推论的式子里两边都除以a右边就是1/a了,左边就是a的逆元,既a^(p-2)。

以下是代码:

#include<stdio.h>

unsigned long long mod=1000000007;
/*快速幂*/
unsigned long long qpow(unsigned long long b,unsigned long long m){
	unsigned long long sum=1;
	
	while(m){
		if(m%2) sum=sum*b%mod;
		m=m/2;
		b=b*b%mod;
	}
	return sum;
}
int main(){
	unsigned long long n;
	while(scanf("%lld",&n)!=EOF){
        /*求C(n,2)的逆元*/
		unsigned long long fm1=qpow(2,mod-2)%mod;
        /*求C(n,4)的逆元*/
		unsigned long long fm2=qpow(24,mod-2)%mod,n1,n2;
        /*求C(n,2)*/
		n1=n*(n-1)%mod;
        /*求C(n,4)*/
		n2=n*(n-1)%mod;
		n2=n2*(n-2)%mod;
		n2=n2*(n-3)%mod;
        
		n1=n1*fm1%mod;
		n2=n2*fm2%mod;
        
		n=(mod+n1+n2+1-n)%mod;
		printf("%llu\n",n);
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值