hdoj-acm1061 (JAVA) rightmost Digit

Source: Internet
Author: User
Tags pow

Test instructions: n-bit digit (1<=n<=1,000,000,000)

The first and most foolish way is to brute force, no meaning, of course, to achieve to play.

The following are Java brute Force hacks:

ImportJava.io.BufferedInputStream;Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner scan=NewScanner (NewBufferedinputstream (system.in));
     int m = Scan.nextint (); while(M--! =0) {intn =Scan.nextint (); Doubleresult =Math.pow (n, N); System.out.println (Result%10); } scan.close (); }}

The second idea is to look for a pattern:

Rule: To find the single digit, in fact, equal to the N-digit number of n-square, the 252^252 is equal to the number of bits of 2^252

In addition: Regardless of the number of N, his single digit is 1-9, then such as 2 * 2 = 4; 2*4=8; 2*8 = 16; 2*16 = 32; When the result is 32 digits 2, a new round of loops (2,4,8,6,2,4 ...) is started.

Here is the Java implementation:

ImportJava.io.BufferedInputStream;Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner scan=NewScanner (NewBufferedinputstream (system.in)); intm =Scan.nextint ();  while(M--! =0){            intn =Scan.nextint (); intRightmostdigitofn = n%10; ArrayList<Integer> rightmostdigits =Arrays.get (RIGHTMOSTDIGITOFN); intSize =rightmostdigits.size (); if(Size = = 1) {System.out.println (RIGHTMOSTDIGITOFN); }Else{System.out.println (Rightmostdigits.get (n-1)%size));    }} scan.close (); }    Staticarraylist<arraylist<integer>> arrays =Getrightmostdigitarray (); StaticArraylist<arraylist<integer>>Getrightmostdigitarray () {ArrayList<ArrayList<Integer>> arrays =NewArraylist<>();  for(inti = 0; I! = 10; i + +) {ArrayList<Integer> integers =NewArraylist<>();            Integers.add (i); intn = 2;  while(true){                intRightmostdigit = (int) (Math.pow (i, N)%10); if(rightmostdigit==i) {                     Break; }Else{integers.add (rightmostdigit); } N++;        } arrays.add (integers); }        returnarrays; }}

hdoj-acm1061 (JAVA) rightmost Digit

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.