2017乌鲁木齐区域赛D题Fence Building——欧拉定理

本文介绍了一种通过在圆形围栏内建立直线围栏来最大化分割区域的方法,从而可以容纳更多的牛。使用了欧拉定理来计算最多能容纳多少头牛,并提供了具体的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意: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.

思路:

欧拉定理:f = e - v + 2

v = n + C(n,4)(每4个点的连线必有一个交点)

e = n(圆弧) + C(n,2)+ 2*C(n,4)(一个交点是两条边形成的,这个交点将这两条边划分成4条边,数量上+2,因此有多少交点就增加交点数*2条边)

ans = f - 1,圆外的不算

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
const int mod = 1e9 + 7;

LL power_mod(LL a,LL b)
{
    LL base=a%mod;
    a=a%mod;
    LL res=1LL;
    while(b)
    {
        if(b&1)
            res=base*res%mod;
        base=base*base%mod;
        b=b>>1;
    }
    return res%mod;
}

LL C(LL n, LL m)
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % mod;
        LL b = i % mod;
        ans = ans * (a * power_mod(b, mod-2) % mod) % mod;
    }
    return ans;
}

LL Lucas(LL n, LL m)
{
    if(m == 0) return 1;
    return C(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod;
}

int main() {
    int T;
    scanf("%d", &T);
    for (int kase = 1; kase <= T; kase++) {
        LL n;
        scanf("%lld", &n);
        printf("Case #%d: %lld\n", kase, (Lucas(n, 2) + Lucas(n, 4) + 1) % mod);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值