Counting regions

链接: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);
		}
	}
} 

②代码:

猜你喜欢

转载自blog.csdn.net/DaDaguai001/article/details/81590802