组队训练赛第一场5215 Problem D Fence Building

题目描述

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.

 

输入

The first line contains an integer 1 ≤ 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 ≤ T ≤ 105 and 1 ≤ n ≤ 1018 .

 

输出

For each test case, you should output a line ”Case #i: ans” where i is the test case number, starting from 1 and ans is the remainder of the maximum number of cows farmer John can feed when divided by 109 + 7.
 

 

样例输入

3
1
3
5
​

 

样例输出

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

题意:在一个圆中有N个点,链接N个点,尽可能划分最多的区域。

题解:最优策略总是在选定 n 个点后,两两连线,且满足任意三条线不交于一点。
尝试统计总的结点个数 A(n)与独立线段(包括圆弧上的 n 段小弧)的总个数 B(n),然后
利用欧拉公式就可以得到答案 Ans(n)=B(n)-A(n)+1。
任意四个点,会形成一个交点,并贡献额外的 2 条独立线段。所以 A(n)=n+C(n,4),而
B(n)=n+2C(n,4)+C(n,2),所以最后答案为 C(n,2)+C(n,4)+1。

1——1

2——2

3——4

4——8

5——16

6——31

7——57

数列1 ,2,4,8,16,31,57~

ll qpow(ll x,ll y){
    ll ans=1;
    while(y){
        if(y%2) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}
ll inv(ll x){
    return qpow(x,mod-2);
}


//inv(24)=1/24
#include
using namespace std;
typedef long long ll;
const int N=1e5+7;
const ll mod=1e9+7;
ll qpow(ll x,ll y){
    ll ans=1;
    while(y){
        if(y%2) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}
ll inv(ll x){
    return qpow(x,mod-2);
}

int main()
{
    int t;ll n;
    scanf("%d",&t);
    ll x,y,x1,y1;
    int flag=1;
    while(t--){
        ll ans=0;
        scanf("%lld",&n);
        ans=((n%mod*((n-1)%mod)%mod)*((n-2)%mod)%mod*((n-3)%mod)%mod)*(inv(24)%mod)%mod;
        ans=(ans+(n%mod*((n-1)%mod)%mod)*(inv(2)%mod)%mod+1)%mod;
        printf("Case #%d: %lld\n",flag++,(ans+mod)%mod);

    }
    return 0;
}

 

你可能感兴趣的:(思路,倒数)