HDU 4919 table Search Rule java large number map recursion, hdu4919

Source: Internet
Author: User

HDU 4919 table Search Rule java large number map recursion, hdu4919

= Oeis:

Click Open Link


Blind, x. mod (BigInteger. ValueOf (2). equal (BigInteger. ValueOf (1 ))

Written as x. mod (BigInteger. ValueOf (2). equal (1)

T ^ T100 blocks are missing...

import java.math.*;import java.util.*;import static java.lang.System.out;import java.io.*;public class Main {static Map map = new HashMap<BigInteger, BigInteger>();static BigInteger x0 = new BigInteger("0");      static BigInteger x1 = new BigInteger("1");      static BigInteger x2 = new BigInteger("2");      static BigInteger x4 = new BigInteger("4");      static BigInteger x6 = new BigInteger("6");  BigInteger dfs(BigInteger x) {if(map.get(x) != null) return (BigInteger)map.get(x);BigInteger d = dfs(x.divide(x2)); // dfs(x/2)BigInteger q1 = x.divide(x2);  // x/2BigInteger q2 = q1.subtract(x1);  // x/2-1BigInteger f1;if (x.mod(BigInteger.valueOf(2)).equals(x1)) {f1 = d.multiply(x4);f1 = f1.add(x6.multiply(q1));}  else {f1 = d;BigInteger d2 = dfs(q2);f1 = f1.add(d2);f1 = f1.add(x);f1 = f1.subtract(x2);f1 = f1.multiply(x2);}map.put(x, f1);return f1;}public void work() {map.put(BigInteger.valueOf(0), BigInteger.valueOf(0));map.put(BigInteger.valueOf(1), BigInteger.valueOf(0));map.put(BigInteger.valueOf(2), BigInteger.valueOf(0));map.put(BigInteger.valueOf(3), BigInteger.valueOf(6));while(cin.hasNext()){String s = cin.next();System.out.println(dfs(new BigInteger(s)));}}Main() {cin = new Scanner(System.in);}public static void main(String[] args) {Main e = new Main();e.work();}public Scanner cin;}



The AC java code for the N-factorial of a large number in HDU
ACM informs JAVA of its usage in some ACM questions

Let's talk about some advantages of Java for ACM:
(1) For programmers familiar with C/C ++, Java is not difficult to learn. general programming can be basically done in two weeks. Just take some time to understand the Java library. The syntax of Java is very similar to that of C ++. It can be said that it is an upgraded version of C ++, but it only emphasizes Object-oriented thinking. (Personal opinion ...)
(2) In general competitions, Java programs will have extra time and space, but Java is not slower than C/C ++ in real large-scale computation, and the input and output efficiency is lower.
(3) Java code is simple and powerful, and some algorithms such as high precision are implemented in Java more concisely and conveniently (ACM really focuses on the speed of question-making during the competition, any question can only be passed, without requiring too much speed of the program ).

*** Tips: If Java times out, you can use Java to create tables and then use C/C ++ to submit them.

(4) using Java is not easy to make minor errors, such as pointers in C/C ++ and "if (n = m.
(5) At present, Eclipse has become a basic configuration. Writing Java programs is more convenient for debugging than C/C ++. It is also an option for a specific competition.

Some Questions about ACM application:
(1) JDK 1.5.0 and later versions provide a good foundation for input and optimize Java input.
The Code is as follows:

Import java. io. * import java. util .*
Public class Main {
Public static void main (String args [])
{
Partition cin = new partition (new BufferedInputStream (System. in ));

}
}

You can also directly convert cin = new partition (System. in); adding a Buffer may be faster.

(2) read an integer: int n = cin. nextInt (); equivalent to scanf ("% d", & n); or cin> n;
Read a String: String s = cin. next (); equivalent to scanf ("% s", s); or cin> s;
Read a floating point number: double t = cin. nextDouble (); equivalent to scanf ("% lf", & t); or cin> t;
Read the entire row: String s = cin. nextLine (); equivalent to gets (s); or cin. getline (...);
You can use cin. hasNext (), cin. hasNextInt (), or cin. hasNextDouble () to determine whether a next input exists ()

(3) Generally, System. out. print () and System. out. println () can be used for output directly. The former does not output line breaks, while the latter does.

System. out. println (n); // n is an int type. Multiple integers can be output in the same row.

System. out. println (new Integer (n). toString () + "" + new Integer (m). toString ());

<G id = "1"> </G>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.