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);
}
}
}
相关文章推荐
- ACM-ICPC 2017 Asia Urumqi:A. Coins(DP)
- ACM-ICPC 2017 Asia Urumqi: K. Sum of the Line(容斥)
- ACM-ICPC 2017 Asia Urumqi: H. Count Numbers(DP+矩阵快速幂优化)
- 2017 ACM/ICPC Asia Regional Urumqi Online
- ACM-ICPC 2017 Asia Urumqi K.
- 2017 ACM/ICPC Asia Regional Shenyang Online:number number number
- 2017 ACM/ICPC Asia Regional Shenyang Online D
- 2017 ACM/ICPC Asia Regional Qingdao Online:1001 Apple
- 2017 ACM/ICPC Asia Regional Shenyang Online number number number
- 2017 ACM/ICPC Asia Regional Shenyang Online
- 【HDU6198 2017 ACM ICPC Asia Regional Shenyang Online E】【找规律 + 矩阵快速幂 + 粗略证明】number number number 无法用K
- ACM-ICPC 2017 Asia HongKong - E (二分答案)
- 2017 ACM/ICPC Asia Regional Qingdao Online:1003 The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Qingdao Online C The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction
- 2017 ACM/ICPC Asia Regional Shenyang Online(部分题解)
- 【HDU6195 2017 ACM ICPC Asia Regional Shenyang Online B】【水题 匹配】cable cable cable M个格子任选K个放K个物品的最小连边匹配
- array array array (2017 ACM/ICPC Asia Regional Shenyang Online 1004)简单DP,最长公共子序列(Ologn)
- HDU6216 A Cubic number and A Cubic Number 找规律 2017 ACM/ICPC Asia Regional Qingdao Online
- 2017 ACM/ICPC Asia Regional Qingdao Online Smallest Minimum Cut 网络流