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 1097 A hard puzzle(循环问题)的更多相关文章

  1. HDOJ 1097 A hard puzzle

    Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...

  2. 【HDOJ】1097 A hard puzzle

    题目和1061非常相似,几乎可以复用. #include <stdio.h> ][]; int main() { int a, b; int i, j; ; i<; ++i) { b ...

  3. hdu 1097 A hard puzzle

    Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...

  4. hdu 1097 A hard puzzle 快速幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...

  5. 【HDOJ】1857 Word Puzzle

    trie树.以puzzle做trie树内存不够,从puzzle中直接找串应该会TLE.其实可以将查询组成trie树,离线做.扫描puzzle时注意仅三个方向即可. /* 1857 */ #includ ...

  6. HDOJ 1098 Ignatius's puzzle

    Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...

  7. HDOJ 1061 Rightmost Digit(循环问题)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  8. HDOJ 5411 CRB and Puzzle 矩阵高速幂

    直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP

    dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...

随机推荐

  1. Linux下得到显示屏参数的方法

    先安装一个软件,然后使用权ddcprobe可以查看当前支持的分辨率和刷新频率#sudo apt-get install xresprobe运行 #sudo ddcprobe会得到下面的信息vbe: V ...

  2. android图片处理方法(转)

    //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...

  3. 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

  4. jquery之获取当前时间

    /** * * 获取当前时间 */ function p(s) { return s < 10 ? '0' + s: s; } var myDate = new Date(); //获取当前年 ...

  5. android背景平铺方式 tileMode

    创建重复的背景图片  在drawable目录下创建一个repeat_bg.xml:    然后在布局的xml文件中可以这样引用:    ================================ ...

  6. 【开源java游戏框架libgdx专题】-01-libgdx介绍

    libgdx是一款开源的java游戏框架,而且还实现了Desktop/Android/BlackBerry/iOS/HTML5这些些平台的跨平台开发.官方网址:https://libgdx.badlo ...

  7. java直接输出Excel 页面实现下载

    public String excelDomnload() throws Exception{ try{ /* String path = GlobalKeys.getFunctionConfig(& ...

  8. LINQ:使用Take和Skip实现分页

    随便找的,还没有试过代码. class Program { static int Main() { //每页大小 ; //页码 ; //源数据 string[] names = { "贤静& ...

  9. Wpf Binding.Path设置

    Binding.Path 获取或设置绑定源属性的路径. 每个绑定通常都具有四个组件:绑定目标对象.目标属性.绑定源,以及要使用的绑定源值的路径.有关这些数据绑定概念的更多信息,请参见数据绑定概述. 使 ...

  10. [转帖]gesture recognition

    http://wenku.baidu.com/view/53c3331a6bd97f192279e9c9.html HSI与RGB的Matlab实现. http://wenku.baidu.com/v ...