20141001 【 高精度 】 2014-上海区域赛-网络预选赛 hdoj 5047 Sawtooth

来源:互联网 发布:怎么加入淘宝联盟 编辑:程序博客网 时间:2024/05/16 13:01

Online JudgeOnline ExerciseOnline TeachingOnline ContestsExercise AuthorF.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts

Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author wilson1068
Mail Mail 0(0)
Control Panel Control Panel 
Sign Out Sign Out
BestCoder官方群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Sawtooth

Time 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 ≤ 1012)
 

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
212
 

Sample Output
Case #1: 2Case #2: 19
 

Source
2014 ACM/ICPC Asia Regional Shanghai Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5056 5055 5054 5053 5052 
 

Statistic | Submit | Discuss | Note
Home | TopHangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2014 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.007610(s) query 7, Server time : 2014-10-01 10:04:13, Gzip disabledAdministration





恶心的卡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();}}}


0 0
原创粉丝点击