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

9 篇文章 0 订阅
4 篇文章 0 订阅

F.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
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 ≤ 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
      
      
2 1 2
 

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

Source
 

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

Statistic |  Submit |  Discuss |  Note
Hangzhou 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 disabled





恶心的卡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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值