您的位置:首页 > 产品设计 > UI/UE

ACM-ICPC 2017 Asia Urumqi: D. Fence Building(递推//矩阵快速幂)

2018-02-10 21:52 906 查看



Farmer John owns a farm. He first builds a circle fence. Then, he will choose n points and build some straight fences connecting them. Next, he will feed a cow in each region so that cows cannot play with each other without breaking the fences. In order to feed more cows, he also wants to have as many regions as possible. However, he is busy building fences now, so he needs your help to determine what is the maximum number of cows he can feed if he chooses these n points properly.

Input

The first line contains an integer 1≤T≤100000, the number of test cases. For each test case, there is one line that contains an integer n. It is guaranteed that 1≤T≤10^5 and 1≤n≤10^18.

Output

For each test case, you should output a line ”Case #i: ans” where i is the test caseS number, starting from 1and ans is the remainder of the maximum number of cows farmer John can feed when divided by 10^9 + 7.

样例输入

3
1
3
5

样例输出

Case #1: 1
Case #2: 4
Case #3: 16

最优策略总是在选定 n 个点后,两两连线,且满足任意三条线不交于一点。尝试统计总的结点个数 A(n)与独立线段(包括圆弧上的 n 段小弧)的总个数 B(n),然后利用欧拉公式就可以得到答案 Ans(n)=B(n)-A(n)+1。任意四个点,会形成一个交点,并贡献额外的 2 条独立线段。所以 A(n)=n+C(n,4),而B(n)=n+2C(n,4)+C(n,2),所以最后答案为 C(n,2)+C(n,4)+1。

然而现场赛,感觉有不少队伍是用矩阵快速幂做出来的。我们队就足足花了差不多4个小时推出来递推公式。本来我试了一下二次项和三次项求解系数,然而发现不对就放弃了,结果答案是4次项的。。题解倒是挺简洁的。还是实力不够啊。然而比赛结束后,竟然听到有人说是紫书的原题。。。翻书一看,紫书336页UVA10213原题。。。import java.util.*;
import java.math.*;
public class Main {
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int T=cin.nextInt(),cas=1;
while((T--)>0)
{
BigInteger n=cin.nextBigInteger();
BigInteger a=n.subtract(BigInteger.ONE);
BigInteger b=a.subtract(BigInteger.ONE);
BigInteger c=b.subtract(BigInteger.ONE);
BigInteger ans=n.multiply(a).divide(new BigInteger("2"));
ans=ans.add(n.multiply(a.multiply(b.multiply(c))).divide(new BigInteger("24")));
ans=ans.add(BigInteger.ONE);
ans=ans.mod(new BigInteger("1000000007"));
System.out.println("Case #"+(cas++)+": "+ans);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: