您的位置:首页 > 编程语言 > Java开发

hdu 5047 Sawtooth--2014acm上海赛区邀请赛(附java模板)

2014-09-27 20:33 495 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047


Sawtooth

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 377 Accepted Submission(s): 116



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

2
1
2


Sample Output

Case #1: 2
Case #2: 19


Source

2014 ACM/ICPC Asia Regional Shanghai Online

Recommend

hujie | We have carefully selected several similar problems for you: 5052 5051 5049 5048 5046

Statistic | Submit | Discuss | Note

一道规律题吧。

公式是这样的 8*n^2 - 7*n + 1

不过出题人似乎故意卡java来增加题目的难度。。。

作为一个ACM弱渣。。。本屌到今天贡献了2次TLE之后才知道原来 java 里面 bufferReader比Scanner要快。。。原因是因为,前者是读入到缓冲区中,而后者直接读入到硬盘里。。。

Sacnner:是一个基于正则表达式的文本扫描器,可以从文件、输入流、字符串中解析出基本类型值和字符串值。

BufferedReader:是javaIO流中的一个字符串、包装类,它必须建立在另一个字符流的基础之上,但system.in是字节流,需用InputStreamReader将其包装成字符流。

要使用BufferReader输入一些字符之外的类型的数据,就要相对比较麻烦,需要通过一些XXXX.parseXxx();来转换相应的数据类型,虽然,麻烦一些,但通过在一些OJ系统上的和Scanner作对比,BufferReader的效率要比Scanner高一倍,这个差距可想而知,读取的数据越多,效果就越明显。

AC程序

import java.io.*;
import java.math.*;
public class Main{
public static BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter cout=new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String []args) throws Exception{
int T=Integer.parseInt(cin.readLine());
for(int nkase=1;nkase<=T;nkase++){
cout.write("Case #"+nkase+": ");
BigInteger N=new BigInteger(cin.readLine());
BigInteger ans=N.multiply(N).multiply(BigInteger.valueOf(8)).subtract(N.multiply(BigInteger.valueOf(7))).add(BigInteger.valueOf(1));
cout.write(ans.toString());
cout.newLine();
}
cout.flush();
cout.close();
}
}


TLE程序

package Sawtooth;

import java.util.*;
import java.math.*;
public class Sawtooth {
public static void main(String []args) throws Exception{
BigInteger one=new BigInteger("1");
BigInteger zero=new BigInteger("0");
BigInteger two= new BigInteger("2");
BigInteger four= new BigInteger("4");
BigInteger six = new BigInteger("6");
BigInteger eight=new  BigInteger("8");
BigInteger seven=new BigInteger("7");

Scanner cin=new Scanner(System.in);
int T=cin.nextInt();
BigInteger n,ans;

for(int nkase=1;nkase<=T;nkase++){
String str;
str=cin.next();
n=new BigInteger(str);
ans=(n.multiply(n)).multiply(eight);
BigInteger tmp=n.multiply(seven);
ans=ans.subtract(tmp);
ans=ans.add(one);
System.out.print("Case #"+nkase+": ");
System.out.println(ans.toString());
}
}
}


最后再附上某牛校的输入输出模板

import java.math.BigInteger;
import java.util.*;
import java.io.*;

class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar;
private int numChars;

public InputReader(InputStream stream) {
this.stream = stream;
}

private int read() {
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}

public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}

public int[] readIntArray(int length) {
int[] res = new int[length];
for (int i = 0; i < length; i ++) res[i] = readInt();
return res;
}

public int[][] readIntArray(int n, int m) {
int[][] res = new int
[m];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
res[i][j] = readInt();
return res;
}

public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}

public long[] readLongArray(int length) {
long[] res = new long[length];
for (int i = 0; i < length; i ++) res[i] = readLong();
return res;
}

public long[][] readLongArray(int n, int m) {
long[][] res = new long
[m];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
res[i][j] = readLong();
return res;
}

public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}

public String[] readStringArray(int length) {
String[] res = new String[length];
for (int i = 0; i < length; i ++) res[i] = readString();
return res;
}

public String next() {
return readString();
}

private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}

public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}

public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}

public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}

public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}

public char[] readCharArray(int length) {
char[] res = new char[length];
for (int i = 0; i < length; i ++)
res[i] = readCharacter();
return res;
}

public char[][] readCharArray(int n, int m) {
char[][] res = new char
[m];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
res[i][j] = readCharacter();
return res;
}

public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}

public double[] readDoubleArray(int length) {
double[] res = new double[length];
for (int i = 0; i < length; i ++) res[i] = readDouble();
return res;
}

public double[][] readDoubleArray(int n, int m) {
double[][] res = new double
[m];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
res[i][j] = readDouble();
return res;
}

private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}

class OutputWriter {
private final PrintWriter writer;

public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}

public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}

public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}

public void printDouble(double x, int precision) {
writer.printf("%." + precision + "f", x);
}

public void printLineDouble(double x, int precision) {
printDouble(x, precision);
printLine();
}

public void printLine(Object...objects) {
print(objects);
writer.println();
}

public void printIntArray(int[] data) {
for (int i = 0; i < data.length; i ++)
if (i < data.length - 1) {
print(data[i] + " ");
} else {
print(data[i]);
}
}

public void printLongArray(long[] data) {
for (int i = 0; i < data.length; i ++)
if (i < data.length - 1) {
print(data[i] + " ");
} else {
print(data[i]);
}
}

public void close() {
writer.close();
}
}

public class Main {
public static void main(String[] args) {
InputReader s = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int T = s.readInt();
for (int caseNo = 0; caseNo < T; caseNo ++) {
BigInteger n = s.readBigInteger();
BigInteger ans = BigInteger.valueOf(8).multiply(n).multiply(n.subtract(BigInteger.ONE));
ans = ans.add(n);
ans = ans.add(BigInteger.ONE);
out.printLine("Case #" + (caseNo + 1) + ": " + ans);
}
out.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: