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

题目链接: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[n][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[n][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[n][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[n][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();
    }
}
时间: 2024-10-14 06:01:26

hdu 5047 Sawtooth--2014acm上海赛区邀请赛(附java模板)的相关文章

HDU 5047 Sawtooth (JAVA大数类)

题目链接: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): 1636    Accepted Submission(s): 637 Problem Description Think about a plane: ● O

HDU 5047 Sawtooth(有趣的思维题+证明)

Sawtooth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 979    Accepted Submission(s): 375 Problem Description Think about a plane: ● One straight line can divide a plane into two regions. ● T

HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现在已经推出这个公式应该是8 * n^2 - 7 * n + 1,但是这个n的范围达到了10^12次方,只要平方一次就超出long long  的范围了,怎么办呢,用大数? 都试过了,很奇怪,会超时,按照估算的话感觉不会,可能是中间结果比较大吧,这个还在思考,但是10^12平方一次乘以八也只达到了10^25次方

2014 网选 上海赛区 hdu 5047 Sawtooth

题意:求n个'M'型的折线将一个平面分成的最多的面数! 思路:我们都知道n条直线将一个平面分成的最多平面数是 An = An-1 + n+1 也就是f(n) = (n*n + n +2)/2 对于一个'M'型的折线呢?它有四条线,但是由于三个顶点的关系导致划分的平面 的数目减少了9个!所以有递推公式 f(n) = (m*m + m + 2)/2 - 9*n; m = 4*n 最后 f(n) = (8*n+1)*(n-1)+2) 由于 n<=1e12 , 所以回报 long long!那么对于大于

HDU 5047 Sawtooth(数学 公式 大数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 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 m

HDU 5047 Sawtooth(大数优化+递推公式)

http://acm.hdu.edu.cn/showproblem.php?pid=5047 题目大意: 给n条样子像“m”的折线,求它们能把二维平面分成的面最多是多少. 解题思路: 我们发现直线1条:2平面:2直线:4平面:3直线:7平面......因为第n条直线要与前面n-1条直线都相交,才能使分的平面最多,则添加第n条直线,平面增加n个: 所以公式是面F = 2 + 2 + 3 + ......+ n = (1+n)*n/2 + 1 因为题目的是“M”的折线,一个“M”有4条线将平面分成2

hdu 5050 Divided Land---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5050 Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 115    Accepted Submission(s): 57 Problem Description It's time to fight the loc

HDU - 5047 Sawtooth

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 f

HDU 5047 Sawtooth 规律+ C++大数模拟 2014 ACM/ICPC Asia Regional Shanghai Online

题意: 用x个大M 可以把平面分成至多几块. 就是折线切割平面的加强版. 一个简单的递推式 : F(x+1) = 16x+1+F(x) 然后转成通项公式,然后C++ 位压大数模拟 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int mod = 1000