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 次,故有:
因此減去外面的無限面,有:
這樣一來,時間復雜度為 O(n),但 n 最大到 2^31,仍然會 TLE
根據?
可得:
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);}} }?
總結
以上是生活随笔為你收集整理的How Many Pieces of Land ? (UVA-10213)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支配树(洛谷-P5180)
- 下一篇: Sigma Function(Light