Think about a plane:
● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...
Now we have some figure constructed with two parallel rays in the same direction, joined by two straight segments. It looks like a character “M”. You are given N such “M”s. What is the maximum number of regions that these “M”s can divide a plane ?
● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...
Now we have some figure constructed with two parallel rays in the same direction, joined by two straight segments. It looks like a character “M”. You are given N such “M”s. What is the maximum number of regions that these “M”s can divide a plane ?
Each case contains one single non-negative integer, indicating number of “M”s. (0 ≤ N ≤ 10 12)
2 1 2
Case #1: 2 Case #2: 19题意:
有一个上图样子的M形的折线,每次添加一个M进去,给一个n问你添加n个M之后能把矩形空间最多分成多少部分。
解题思路:
首先使用一个叫“画图”的软件模拟一下这个过程,你会发现一些规律:
发现新增的块数满足一个规律:新增=(n-1)*4*4+1
那么从第二项新增的开始满足一个等差数列(即上式)
那么求ans就是求前n项的和,用等差数列求和公式可推出:
ans=(16*n+2)*(n-1)/2+2 (n>=2)
ans=2 (n==1)
推出来公式以后看数据范围比较大,用java的高精度可解。
import java.util.*;
import java.math.BigInteger;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int t=scanner.nextInt();
BigInteger two=new BigInteger("2");
BigInteger one=new BigInteger("1");
BigInteger data16=new BigInteger("16");
for(int cas=1;cas<=t;cas++)
{
String str=scanner.next();
BigInteger a=new BigInteger(str);
BigInteger ans=a.multiply(data16).add(two).multiply(a.subtract(one)).divide(two).add(two);
System.out.print("Case #"+cas+": ");
if(a.equals(one))
System.out.println(two);
else
System.out.println(ans);
}
}
}