码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10213 - How Many Pieces of Land ?(欧拉公式)

时间:2018-09-12 01:20:49      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:family   put   ref   get   imp   input   com   target   mes   

链接:

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 }

 

UVa 10213 - How Many Pieces of Land ?(欧拉公式)

标签:family   put   ref   get   imp   input   com   target   mes   

原文地址:https://www.cnblogs.com/hkxy125/p/9631777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!