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;
    }
}
目录
打赏
0
0
0
0
988
分享
相关文章
|
11月前
手撕Hard--缺失的第一个正数
手撕Hard--缺失的第一个正数
|
11月前
|
【动态规划刷题 18】(hard)回文子串&& (hard)最长回文子串
【动态规划刷题 18】(hard)回文子串&& (hard)最长回文子串
51nod 2382 一半的一半 (sprintf()函数的用法)
51nod 2382 一半的一半 (sprintf()函数的用法)
76 0
Codeforces1486 C2.Guessing the Greatest (hard version)(交互题+奇怪的二分)
Codeforces1486 C2.Guessing the Greatest (hard version)(交互题+奇怪的二分)
66 0
SPOJ - COT Count on a tree(主席树 LCA)
SPOJ - COT Count on a tree(主席树 LCA)
117 0
[Atcoder ARC124] XOR Matching 2-小思维 | 暴力
题意: 给出n,两个数列a[1] -> a[n],b[1] -> b[n] 问有多少个x,可以使得在我们任意一种方式排列b[]之后,有a[i] ^ b[i] == x (1 <= i <= n) 思路: 首先我们可以确定所有的答案一定在a[1] ^ b[i] (1 <= i <= n)之内,所以我们只需要将这些个x的解空间单独放到数组c[]里,然后遍历x的解空间c[],将c[i] ^ a[i]的结果记录在d[]里面,然后判断b[],d[]是否完全相同即可,如果完全相同,就可以记录答案,注意最终答案要进行去重
131 0
[Atcoder ARC124] XOR Matching 2-小思维 | 暴力
HDU-1097,A hard puzzle(快速幂)
HDU-1097,A hard puzzle(快速幂)
1128. N Queens Puzzle (20) 判断是否是对角线
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.
1156 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等