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