HDOJ-ACM2035(JAVA) 人见人爱A^B

这道题的巧妙方法没有想出来,但是算是优化的暴力破解吧。Accepted

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        while(scan.hasNextInt()){
            int n = scan.nextInt();
            int m = scan.nextInt();
            if(n==0&&m==0){
                break;
            }
            int right3OfN = n%1000;
            int result = right3OfN;
            while(--m!=0){
                result = result*right3OfN%1000;
            }
            System.out.println(result);
        }
        scan.close();
    }
}

但是,发现了个问题

如果我把1-999的m次方可能的结果用静态容器保存起来时,结果就 Wrong Answer

没想通,有谁知道么?

以下是JAVA实现:

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        while(scan.hasNextInt()){
            int n = scan.nextInt();
            int m = scan.nextInt();
            if(n==0&&m==0){
                break;
            }
            int right3OfN = n%1000;
            ArrayList<Integer> right3s = arrays.get(right3OfN);
            System.out.println(right3s.get((m-1)%right3s.size()));
        }
        scan.close();
    }

    static ArrayList<ArrayList<Integer>> arrays  = getRightMost3DigitArray();

    static ArrayList<ArrayList<Integer>> getRightMost3DigitArray(){
        ArrayList<ArrayList<Integer>> arrays = new ArrayList<>();
        for(int i = 0 ; i != 1000 ; i ++){
            ArrayList<Integer> integers = new ArrayList<>();
            int right3OfI = i;
            integers.add(right3OfI);
            int right3 = right3OfI;
            while(true){
                right3 = right3 * right3OfI%1000;
                if(integers.contains(right3)){
                    break;
                }else{
                    integers.add(right3);
                }
            }
            arrays.add(integers);
        }
        return arrays;
    }
}
时间: 2024-10-26 06:08:28

HDOJ-ACM2035(JAVA) 人见人爱A^B的相关文章

HDOJ 1000 java

第一次做hdoj 试了好几次才通过 发现主类的名字必须是Main import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); while(scanner.hasNext()) { int A=scanner.nextInt(); int B=scanner.nextInt(); System.out.p

ACM--大数阶乘--HDOJ 1042--N!--Java

HDOJ题目地址:传送门 N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 74952    Accepted Submission(s): 21832 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Inp

HDOJ 1001 java

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int sum,n; while(scanner.hasNext()) { n=scanner.nextInt(); sum=0; for(int i=1;i<=n;i++) { sum+=i; } System.out.println(sum

HDOJ 1002 java

import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int n=scanner.nextInt(); for(int i=1;i<=n;i++) { BigInteger a=sca

hdoj 1007 java MLE通不过

参考网上的一个算法,但是一直都是MLE,实在是想不出来什么办法了. 参考博客:编程之美寻找最近点对 import java.util.Arrays; import java.util.Scanner; public class Main { public static int N=100000; public static int[] list=new int[N]; public static Point[] ptns=new Point[N]; public static float MAX

【HDOJ 1002】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 238517    Accepted Submission(s): 45969 Problem Description I have a very simple problem for you. Given two integers A and B, you

hdu 2033 人见人爱A+B (java)

问题: 在for循环时习惯用元素i,以至于换成别的时老是用i,在每次for循环时请多注意该循环的元素是否一致. 在m,y有值的情况下习惯性将它当零直接赋值了,请注意!! 人见人爱A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30111    Accepted Submission(s): 20076 Problem Descr

hdoj 2035 人见人爱A^B

人见人爱A^B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27954    Accepted Submission(s): 19117 Problem Description 求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B

(java实现)杭电oj 2034 人见人爱A-B

人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68543    Accepted Submission(s): 19203 Problem Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减