牛客网暑期ACM多校训练营(第八场)G - Counting regions(规律+逆元)

链接: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.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
 
const int MOD = 1e9+7;
 
LL n,ans;
 
LL qpow(LL a,LL b,LL m)//快速幂
{
    LL ans = 1;
    a %= m;
    while(b > 0)
    {
        if(b & 1)
           ans = (ans * a) % m;
        a = a * a % m;
        b >>= 1;
    }
    return ans;
}
LL Fermat(LL a,LL p)//前提p是质数
{
    return qpow(a,p-2,p);
}
int main(){
    while(cin>>n){
        LL a = (((((n*n)%MOD)*n)%MOD)*n)%MOD;
        LL b = 6 * (((n*n)%MOD)*n)%MOD;
        LL c = (23 * ((n*n)%MOD))%MOD;
        ans = (( a - b + c - (42*n)%MOD +24) + 2 * MOD) % MOD;
        ans = ans * Fermat(24,MOD);
        ans %= MOD;
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81589253
今日推荐