UPC 5215 逆元的应用+不知道的数学公式

5215: Fence Building

时间限制: 1 Sec  内存限制: 128 MB
提交: 404  解决: 75
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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时分割区域为多少

学一下这个视频

再参考一下这个

最终代码如下

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=1e9+7;
void ex_gcd(ll a,ll b,ll &x,ll &y,ll &d)
{
    if(!b){d=a;x=1;y=0;}
    else
    {
        ex_gcd(b,a%b,y,x,d);
        y-=x*(a/b);
    }
}
ll inv(ll a,ll p)
{
    ll d,x,y;
    ex_gcd(a,p,x,y,d);
    return d==1?(x%p+p)%p:-1;
}
int main(){
    ll n;
    int t;
    scanf("%d",&t);
    //ios::sync_with_stdio(0);cin.tie(0);
    int tot=1;
    while(t--){
            ll ans1=inv(2,N);//cout<<ans1<<endl;
            ll ans2=inv(24,N);
            scanf("%lld",&n);
            ll n1=n%N;
            ll n2=(n-1)%N;
            ll n3=(n-2)%N;
            ll n4=(n-3)%N;
            ll ans=n1*n2%N*ans1%N+n1*n2%N*n3%N*n4%N*ans2%N;
          printf("Case #%d: %lld\n",tot++,(ans+1)%N);
    }
    return 0;
}

这题用cin,cout会超时注意一下!!以后还是用老c的输入吧>==<

逆元参考这个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值