HDU 4873ZCC Loves Intersection

来源:互联网 发布:中世纪2mod原版优化9 编辑:程序博客网 时间:2024/06/05 17:28
After beats all opponents in 3-dimension-world OI, ZCC feels bored and sets about going to other universes. In a universe with D dimension(s), ZCC finds D segments floating in the air. To be more precise: if we build a rectangular coordinate system with D axis, each of the segments is parallel with one axis, whose endpoints have a coordination of which all components belong to the set {x∈Z|0≤x<N}. For each axis, there is exactly one segment parallel with it. 
 Each of the D segments changes location every second. Read the pseudo code below for more details: 

Every second, from every pair of segments intersect, ZCC acquires a unit of Energy. Calculate the Expectation of the amount of the acquired energy per second please.
Input
There are several test cases in one input file. EOF indicates the end of input file.
Every test case contain two positive numbers N, D in one line. 
 It is guaranteed that 1<N≤10^9, D≤99. The number of test cases≤10.
Output
For each test case, output a line with an integer or an irreducible fraction p/q, which is the Expectation.
Sample Input
2 23 35 5
Sample Output
149/8118/625问一个d维空间里,d条分别平行于d维的随机线段相交的期望。
首先,任意两条线段相交的几率是相同的,所以答案是C(d,2)*任意两条线段相交的概率
两条线段要相交,那么出去平行的两维以外,其他维度必须相同,那么概率是1/n^(d-2)
然后考虑平面上两条平行于x轴和y轴的随机线段相交的概率。
假设两条线段长度分别为i和j,相交的全部情况为 
∑∑n*(n-i)(长度为i的条数)*n*(n-j)(长度为j的条数)*i/n*j/n(相交的前提)
除于全部的情况,(n*n*(n-1)/2)^2
然后就是喜闻乐见的化简过程,最后答案就是   d(d-1)(n+4)^2/18n^d
显然答案可能很大,所以上个java大数就ok了。
import java.math.BigInteger;import java.util.Scanner;public class Main {public static void main(String[] args){Scanner in=new Scanner(System.in);while (in.hasNext()){BigInteger n = in.nextBigInteger();BigInteger d = in.nextBigInteger();BigInteger x = d.multiply(d.subtract(BigInteger.ONE));x = x.multiply(n.add(BigInteger.valueOf(4)));x = x.multiply(n.add(BigInteger.valueOf(4)));BigInteger y = BigInteger.valueOf(18);for (int i=1;i<=d.intValue();i++) y = y.multiply(n);System.out.println(x.divide(x.gcd(y))+"/"+y.divide(y.gcd(x)));}in.close();}}


0 0
原创粉丝点击