计蒜客暑假集训第八场 d题

Farmer John owns a farm. He first builds a circle fence. Then, he will choose n points and build some straight fences connecting them. Next, he will feed a cow in each region so that cows cannot play with each other without breaking the fences. In order to feed more cows, he also wants to have as many regions as possible. However, he is busy building fences now, so he needs your help to determine what is the maximum number of cows he can feed if he chooses these n points properly.

Input

The first line contains an integer 1 \le T \le 1000001≤T≤100000, the number of test cases. For each test case, there is one line that contains an integer n. It is guaranteed that 1 \le T \le 10^51≤T≤105 and 1 \le n \le 10^{18}1≤n≤1018.

Output

For each test case, you should output a line ”Case #ii: ans” where ii is the test caseS number, starting from 11and ans is the remainder of the maximum number of cows farmer John can feed when divided by 10^9 + 7109+7.

样例输入

3
1
3
5

样例输出

Case #1: 1
Case #2: 4
Case #3: 16

题目大意:

有一个圆,然后有n个点,将这n个点任意两个相连,这样就可以将圆划分成多个部分,输入n,代表n个点,让你求将这n个点任意两个相连后,最多能够将圆划分成几个部分。

思路:

我们通过欧拉方程来求解这个问题。如果要使所划分的区域最多,那么必须保证不能够存在三线共点,也就是说圆内任意一点最多只有两条边经过。假设有n个顶点,任意两个顶点可以连出一条边,一共可以连出C( n , 2 )条边,任意4个顶点可以确定一个圆内点,通过这个圆内点,又可以多出两条边,那么,我们假设边的个数为E,则E=C( n , 2 )+ 2*C( n ,4 )+ n  (圆弧也算边),然后我们假设顶点个数为V,则V=C( n , 4 )+ n 。我们假设区域数为R,则有欧拉公式得:R=E+2-V。除去圆外的部分,则区域数R为:R=E+1-V。代入得:R=C( n , 2 )+C( n , 4 )+1。通过这个公式计算就可以了。

(a +  b) % p = (a%p +  b%p) %p  (对)

(a  -  b) % p = (a%p  -  b%p) %p  (对)

(a  *  b) % p = (a%p *  b%p) %p  (对)

代码一:(通过Lucas定理计算组合数取模,然后累加取模)

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#define mod 1000000007
typedef long long ll;
ll t;
ll n;
ll power(ll a,ll b)//快速幂取模
{
    ll t=1;
    while (b)
    {
        if(b&1)
            t=t*a%mod;
        a=a*a%mod;
        b=b>>1;
    }
    return t;
}
ll inv(ll a)//求a的逆元
{
    return power(a,mod-2);
}
ll c(ll a,ll b)//计算组合数C(a,b)%mod的值,不是通过阶乘的方法来求了。
{
    if(b>a)
        return 0;
    ll ans=1;
    for(int i=1;i<=b;i++)
        ans=(ans*((((a-i+1)%mod)*(inv(i)%mod))%mod))%mod;//累乘取模
    return ans;
}
ll Lucas(ll a,ll b)//求C(a,b)%mod的值
{
    if(b==0)
        return 1;
    return c(a%mod,b%mod)*Lucas(a/mod,b/mod)%mod;
}
int main()
{
    scanf("%lld",&t);
    ll o=1;
    while (t--)
    {
        scanf("%lld",&n);
        ll c1=Lucas(n,2);
        ll c2=Lucas(n,4);
        ll jieguo=(c1+c2+1)%mod;//这个地方可以放心大胆的加,不用害怕变成大数,因为c1小于mod,c2也小于mod,mod等于1e9,c1加c2最大也不会超过1e10,在long long所能够表示的范围内
        printf("Case #%lld: %lld\n",o++,jieguo);
    }
}

收获:

知道了欧拉公式:

传送门:

https://baike.baidu.com/item/%E6%AC%A7%E6%8B%89%E5%85%AC%E5%BC%8F

猜你喜欢

转载自blog.csdn.net/qq_40938077/article/details/81430925