Counting regions

本文介绍了一种计算由奇数顶点构成的正多边形内部区域数量的方法,并提供了具体的C++实现代码。该算法利用数学公式进行计算,考虑了模运算的特点,确保了在大数据范围内的正确性。

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

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

                                                                         Counting regions

时间限制: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为奇数。而且OEIS出来的公式为

<1> (n-1)*(n-2)/2+n*(n-1)*(n-2)*(n-3)/24

<2> 24=2*3*4

<3> 连续的4个数字必能整除2,3,4

       5*4*3*2

       5*6*7*8

       6*7*8*9

<4> 先除4,再除3,再除2(至于为什么是这个顺序...好好思考思考)。

思路2:

①代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long int ll;
ll mod=1000000007;
int k[4],p[4]={4,3,2};
int main()
{
	ll n;
	ll res,temp;
	while(scanf("%d",&n)!=EOF){
		if(n==3) printf("1\n");
		else{
			k[0]=n-3,k[1]=n-2,k[2]=n-1,k[3]=n;
			res=((n-1)/2*(n-2))%mod;
			for(int i=0;i<3;i++){
				for(int j=0;j<4;j++){
					if(k[j]%p[i]==0){
						k[j]/=p[i];
						break;
					}
				}
			}
			temp=k[0];
			for(int i=1;i<4;i++) temp=(temp*k[i])%mod;
			res=(res+temp)%mod;
			printf("%lld\n",res);
		}
	}
} 

②代码:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值