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. C++初始化顺序

    1. 全局和类的静态变量成员在main之前构造和初始化,静态成员不能在类的内部构造初始化 2. 类的普通成员依据在类内的定义顺序初始化,类的构造函数的初始化类表只能决定成员的构造函数,不能决定构造顺序 ...

  2. ie兼容CSS3渐变写法

    在css3之前要想做背景色渐变就只能采用添加背景图片的方法,但是随着css3:linear-gradient属性的出现,就可以避免使用添加背景图片的方法,从而优化了性能.但是inear-gradien ...

  3. Linux字符串函数集

    //Linux字符串函数集: 头文件:string.h 函数名: strstr 函数原型:extern char *strstr(char *str1, char *str2); 功能:找出str2字 ...

  4. js的new操作符

    1.创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型. 2.属性和方法被加入到 this 引用的对象中. 3.新创建的对象由 this 所引用,并且最后隐式的返回 this . ...

  5. ASPNET5 诊断

    1. 配置一个错误的处理页 在ASP.NET5, 可以在Startup的Configure里配置一个错误处理页,对于开发来说,非常简单,只要增加Microsoft.AspNet.Diagnostics ...

  6. MySQL 管理

    MySQL 管理 启动及关闭 MySQL 服务器 首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql ...

  7. Java数据库连接池的几种配置方法(以MySQL数据库为例)

    Java数据库连接池的几种配置方法(以MySQL数据库为例) 一.Tomcat配置数据源: 前提:需要将连接MySQL数据库驱动jar包放进Tomcat安装目录中common文件夹下的lib目录中 1 ...

  8. 为什么struts2 ajax 方法执行两次

    struts2中使用json插件执行ajax处理时,如果方法名是get方法的时候,方法会莫名其妙的执行两次. 原因: struts2 中JSON的原理是在ACTION中的get方法都会序列化,所以前面 ...

  9. javascript学习(知识点整理)

    有了这个代码,就可以在定义 中增加更多的控制了 后面会举例关于extjs定义的更多控制     此种方案可以解决定义时需要一些函数调用的情况 函数作用域和声明提前: 即由于js是解释性语言,在执行前会 ...

  10. linux变量心得

    前一段时间学习了一下linux的变量,现在总结有3点需要特别注意: linux变量和C/C++变量的区别 linux变量的引用 linux变量特有的命令替换 先说第一点,linux变量更像是宏定义,只 ...