hdoj-acm2035 (JAVA) People see people love a^b

Source: Internet
Author: User

The ingenious method of this problem is not thought out, but it is an optimized brute force hack. Accepted

ImportJava.io.BufferedInputStream;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner scan=NewScanner (NewBufferedinputstream (system.in));  while(Scan.hasnextint ()) {intn =Scan.nextint (); intm =Scan.nextint (); if(n==0&&m==0){                 Break; }            intRight3ofn = n%1000; intresult =right3ofn;  while(--m!=0) {result= result*right3ofn%1000;        } System.out.println (Result);    } scan.close (); }}

But, I found a problem.

If I save the 1-999 m-second possible results in a static container, the result is wrong Answer

No idea, who knows?

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));  while(Scan.hasnextint ()) {intn =Scan.nextint (); intm =Scan.nextint (); if(n==0&&m==0){                 Break; }            intRight3ofn = n%1000; ArrayList<Integer> right3s =Arrays.get (RIGHT3OFN); System.out.println (Right3s.get ((M-1)%right3s.size ()));    } scan.close (); }    Staticarraylist<arraylist<integer>> arrays =Getrightmost3digitarray (); StaticArraylist<arraylist<integer>>Getrightmost3digitarray () {ArrayList<ArrayList<Integer>> arrays =NewArraylist<>();  for(inti = 0; I! = 1000; i + +) {ArrayList<Integer> integers =NewArraylist<>(); intRight3ofi =i;            Integers.add (Right3ofi); intRIGHT3 =Right3ofi;  while(true) {RIGHT3= RIGHT3 * right3ofi%1000; if(Integers.contains (RIGHT3)) { Break; }Else{integers.add (RIGHT3);        }} arrays.add (integers); }        returnarrays; }}

hdoj-acm2035 (JAVA) People see people love a^b

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.