UVa 10213
鏈接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1154
題意:
有一塊橢圓形的地。在邊界上選n(0≤n<2^31)個點并兩兩連接得到n(n-1)/2條線段。
它們最多能把地分成多少個部分?
分析:
本題需要用到歐拉公式:在平面圖中,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次。
根據:
化簡得:
代碼:
1 import java.io.*;
2 import java.util.*;
3 import java.math.*;
4
5 public class Main {
6 Scanner cin = new Scanner(new BufferedInputStream(System.in));
7 final BigInteger c1 = new BigInteger("1");
8 final BigInteger c2 = new BigInteger("2");
9 final BigInteger c3 = new BigInteger("3");
10 final BigInteger c12 = new BigInteger("12");
11
12 void MAIN() {
13 int T = cin.nextInt();
14 while(T --> 0) {
15 BigInteger n = cin.nextBigInteger();
16 BigInteger one = n.multiply(n.subtract(c1)).divide(c2);
17 BigInteger two = one.multiply(n.subtract(c2)).multiply(n.subtract(c3)).divide(c12);
18 System.out.println(one.add(two).add(c1));
19 }
20 }
21
22 public static void main(String args[]) { new Main().MAIN(); }
23 }
總結
- 上一篇: 根据时间变换页面背景
- 下一篇: go代码自动生成注释