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;
    }
}
目录
相关文章
|
9月前
|
算法
hdoj 3732 Ahui Writes Word (多重背包)
来看题目吧,可能有100000个单词,然后只有1000ms,但看包的大小,有10000,这样只能允许nlog(n)的算法,还有,每个单词的价值和花费都很小(不大于十),如果不考虑单词的不同,只考虑价值和花费只有最多100种东西,但如果把这些按多重背包的方法来计算依旧会超时,很容易想到和之前01背包的时间复杂度是一样的。
29 0
|
机器学习/深度学习
HDOJ 1061 Rightmost Digit(循环问题)
HDOJ 1061 Rightmost Digit(循环问题)
102 0
HDOJ 1061 Rightmost Digit(循环问题)
|
Web App开发 Java 数据安全/隐私保护
HDOJ(HDU) 1563 Find your present!(异或)
HDOJ(HDU) 1563 Find your present!(异或)
223 0
|
机器学习/深度学习
HDOJ 1334 Perfect Cubes(暴力)
HDOJ 1334 Perfect Cubes(暴力)
87 0
HDOJ 2058 The sum problem
HDOJ 2058 The sum problem
95 0
HDOJ(HDU) 1678 Shopaholic
HDOJ(HDU) 1678 Shopaholic
94 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
95 0
HDOJ(HDU) 1673 Optimal Parking
HDOJ(HDU) 1673 Optimal Parking
109 0
|
人工智能
HDOJ 1028 Ignatius and the Princess III(递推)
HDOJ 1028 Ignatius and the Princess III(递推)
103 0