| ||||||||||
| ||||||||||
SawtoothTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1106 Accepted Submission(s): 430
Problem Description
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 ?
Input
The first line of the input is T (1 ≤ T ≤ 100000), which stands for the number of test cases you need to solve.
Each case contains one single non-negative integer, indicating number of “M”s. (0 ≤ N ≤ 10 12)
Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then an integer that is the maximum number of regions N the “M” figures can divide.
Sample Input
Sample Output
Source
Recommend
| ||||||||||
|
恶心的卡IO。
java 常用的 Scanner IO 方法会超时。
要用缓冲区优化。
当然,最好是用 C++ 大数模板上。
import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out, true);
BigInteger n, ans, a1, a2, a7, a8;
a1 = BigInteger.valueOf(1);
a2 = BigInteger.valueOf(2);
a7 = BigInteger.valueOf(7);
a8 = BigInteger.valueOf(8);
String ss;
ss = sc.readLine();
int T;
T = Integer.parseInt(ss);
for(int i=1; i<=T; i++){
ss = sc.readLine();
n = new BigInteger(ss);
out.print("Case #"+i+": ");
if( n.equals(a1) ){
out.println("2");
continue;
}
else if( n.equals(a2) ){
out.println("19");
continue;
}
ans = n.multiply(n).multiply(a8);
ans = ans.subtract(a7.multiply(n));
ans = ans.add(a1);
out.println( ans );
}
}
}
这里优化了一下。
(上面的跑了1800+ms,下面的跑了1300+ms)
——————————————————
输入用 BufferedReader( cin ) 读入。
输出用 BufferedWriter( cout ) 写出。
这里 cout.write() 方法支持输出基本数据类型,
但不支持 BigInteger 和 BigDecimal 数据类型。
因此要先转化成 String 类型。
同时, cout.write() 不带回车。
你可以用 转义字符 '\n' 或 cout.newLine() 方法加上回车。
【Important】cout.write() 只是把其中的数据暂存起来(暂存在 cout 中),并没有输出。
想要输出数据,必须调用 cout.flush() 方法(清空 cout ,输出里面的数据)。
import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter cout = new BufferedWriter(new OutputStreamWriter(System.out));
BigInteger n, ans, a1, a2, a7, a8;
a1 = BigInteger.valueOf(1);
a2 = BigInteger.valueOf(2);
a7 = BigInteger.valueOf(7);
a8 = BigInteger.valueOf(8);
String ss = cin.readLine();
int T = Integer.parseInt(ss);
for(int i=1; i<=T; i++){
ss = cin.readLine();
n = new BigInteger( ss );
cout.write( "Case #"+i+": " );
ans = n.multiply(n).multiply(a8);
ans = ans.subtract(a7.multiply(n));
ans = ans.add(a1);
cout.write( ans.toString() );
cout.newLine();
cout.flush();
}
}
}