HDOJ 1097 A hard puzzle(循环问题)

简介: HDOJ 1097 A hard puzzle(循环问题)

Problem Description

lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.

this puzzle describes that: gave a and b,how to know the a^b’s the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.


Input

There are mutiple test cases. Each test cases consists of two numbers a and b(0< a,b<=2^30)


Output

For each test case, you should output the a^b’s last digit number.


Sample Input

7 66

8 800


Sample Output

9

6


本题重要的是循环节的判断,java的大数会超时的。

下面代码实现了循环节的寻找。

import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
    static int da[] = new int[10];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        dabiao();//打表
        while(sc.hasNext()){
            //超时
//          BigDecimal a = sc.nextBigDecimal();
//          int b = sc.nextInt();
//          a = a.pow(b);
//          String str = a.toString();
//          System.out.println(str.charAt(str.length()-1));
            //找规律
            int a = sc.nextInt();
            int b = sc.nextInt();
            a = a%10;
            switch(a){
            case 0:System.out.println(da[0]);break;
            case 1:System.out.println(da[1]);break;
            case 2:System.out.println(shuchu(b,da[2],2));break;
            case 3:System.out.println(shuchu(b,da[3],3));break;
            case 4:System.out.println(shuchu(b,da[4],4));break;
            case 5:System.out.println(shuchu(b,da[5],5));break;
            case 6:System.out.println(shuchu(b,da[6],6));break;
            case 7:System.out.println(shuchu(b,da[7],7));break;
            case 8:System.out.println(shuchu(b,da[8],8));break;
            case 9:System.out.println(shuchu(b,da[9],9));break;
            }
        }
    }
    private static int shuchu(int b, int i, int j) {
        b=b%i;
        int sum=j;
        if(b==0){
            b=i;
        }
        for(int k=1;k<b;k++){
            sum=sum*j;
        }
        return sum%10;
    }
    private static void dabiao() {
        da[0]=0;
        da[1]=1;
        int h=0;
        for(int i=2;i<10;i++){
            h=0;
            for(int k=2;k<10;k++){
                if(i==hm(k,i)){
                    h=k-1;
                    break;
                }
            }
            da[i]=h;
        }
        //0-9的循环节输出
//      for(int i=0;i<10;i++){
//          System.out.println(da[i]);
//      }
    }
    private static int hm(int k,int i) {
        int sum=1;
        for(int j=0;j<k;j++){
            sum=sum*i;
        }
        return sum%10;
    }
}
目录
相关文章
|
算法
hdoj 3732 Ahui Writes Word (多重背包)
来看题目吧,可能有100000个单词,然后只有1000ms,但看包的大小,有10000,这样只能允许nlog(n)的算法,还有,每个单词的价值和花费都很小(不大于十),如果不考虑单词的不同,只考虑价值和花费只有最多100种东西,但如果把这些按多重背包的方法来计算依旧会超时,很容易想到和之前01背包的时间复杂度是一样的。
45 0
|
6月前
Strange fuction(HDU--2899)
Strange fuction(HDU--2899)
HDU-1097,A hard puzzle(快速幂)
HDU-1097,A hard puzzle(快速幂)
HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
125 0
HDOJ/HDU 1372 Knight Moves(经典BFS)
HDOJ/HDU 1372 Knight Moves(经典BFS)
134 0
HDOJ(HDU) 1678 Shopaholic
HDOJ(HDU) 1678 Shopaholic
106 0
|
Java 文件存储
HDOJ(HDU) 2132 An easy problem
HDOJ(HDU) 2132 An easy problem
108 0
|
Java 文件存储
HDOJ(HDU) 2123 An easy problem(简单题...)
HDOJ(HDU) 2123 An easy problem(简单题...)
169 0
|
Java
HDOJ(HDU) 2164 Rock, Paper, or Scissors?
HDOJ(HDU) 2164 Rock, Paper, or Scissors?
115 0
HDOJ(HDU) 1673 Optimal Parking
HDOJ(HDU) 1673 Optimal Parking
122 0