How Many Pieces of Land ? (UVA-10213)

Problem Description

You are given an elliptical shaped land and you are asked to choose n arbitrary points on its boundary. Then you connect all these points with one another with straight lines (that’s n ∗ (n−1)/2 connections for n points). What is the maximum number of pieces of land you will get by choosing the points on the boundary carefully?

Input

The first line of the input file contains one integer S (0 < S < 3500), which indicates how many sets of input are there. The next S lines contain S sets of input. Each input contains one integer N (0 ≤ N < 231).

Output

For each set of input you should output in a single line the maximum number pieces of land possible to get for the value of N.

Sample Input

4
1
2
3
4

Sample Output

1
2
4
8

题意:t 组样例,每组给出一个数 n,代表一个椭圆上有 n 个点,将这 n 个点两两相连,问最多能将这个椭圆分成个区域

思路:欧拉公式

在平面图中,V-E+F=2,其中 V 是点数,E 是边数 F 是面数,因此只需要计算 V、E 即可,此外还需减去外面的 “无限面”

不管是点还是边,在计算时,都要枚举一条从固定点出发的对角线,因此最后要乘以 n,对角线的左边有 i 个点,右边有 n-2-i 个点,左右点连线在这条对角线上形成了 i*(n-2-i) 个交点,得到 i*(n-2-i)+1 条线段,这样一来,每个点被重复计算了 4 次,每条边被重复计算了 2 次,故有:

\left\{\begin{matrix}V=n+\frac{n}{4}\sum_{i=1}^{n-3}i*(n-2-i) \\ E=n+\frac{n}{2}\sum_{i=1}^{n-3}i*(n-2-i)+1 \end{matrix}\right.

因此减去外面的无限面,有:F=E-V+2-1=\frac{n(n-1)}{2}+\frac{n}{4}\sum_{i=0}^{n-2}i*(n-2-i)+1

这样一来,时间复杂度为 O(n),但 n 最大到 2^31,仍然会 TLE

根据 \sum i*(n+1-i)=\frac{n(n+1)(n+2)}{6}

可得:F=\frac{n(n-1)}{2}+\frac{n(n-1)(n-2)(n-3)}{24}+1

Source Program

import java.math.BigInteger;
import java.util.*;
    
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
            int T=input.nextInt();
            while(T-->0){
                BigInteger n=input.nextBigInteger();
                BigInteger temp1=n.multiply(n.subtract(BigInteger.ONE)).divide(BigInteger.valueOf(2));
                BigInteger temp2=n.multiply(n.subtract(BigInteger.ONE)).multiply(n.subtract(BigInteger.valueOf(2))).multiply(n.subtract(BigInteger.valueOf(3))).divide(BigInteger.valueOf(24));
                BigInteger res=temp1.add(temp2).add(BigInteger.ONE);
                System.out.println(res);
            }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值