HDOJ 1097 A hard puzzle(循环问题)

时间:2022-09-01 08:53:42

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&&num;39&semi;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 排列数字凑同余&comma;状态压缩DP

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

随机推荐

  1. C&num; 在类中反射

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Wind ...

  2. VS中的活动debug和活动cpu

    “活动”的含义就是当前项目所用的模式和平台.项目中的”活动“继承于解决方案中的配置.

  3. 在C,C&plus;&plus;,JAVA中int&comma;char&comma;各占几个字节

    int 在C和C++的占用2个字节,在java中4个字节char在C和C+中占一个字节 Java中无论是汉字还是英文字母都是用Unicode编码来表示的,一个Unicode码是16位,每字节是8位,所 ...

  4. Oracle11g新特性导致空表不能导出问题

        ORACLE 11G在用EXP导出时,发现空表(没有数据或者没有用过的表)不能导出了.     查了一下资料,说是Oracle 11G中有个新特性,当表无数据时,不分配segment,以节省空 ...

  5. Facebook HHVM 和 Hack 手册 --- 2&period; HHVM能做什么

    HHWM简介: HHWM(HipHop VM) 是Facebook推出的用来执行PHP代码的虚拟机,它是一个PHP的JIT(Just-In- Time)编译器,同时具有产生快速代码和即时编译的优点. ...

  6. 设计模式 --&gt&semi; (15)职责链模式

    职责链模式 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 示例 考虑员工要求加薪.公司的管理者一共有 ...

  7. H5 五子棋源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. springmvc上传图片《2》

    创建springboot项目 编写配置 server: port: 8082 spring: application: name: upload-service servlet: multipart: ...

  9. mongoose 根据&lowbar;id更新数据

    let photoId = mongoose.Types.ObjectId(`${virtual.productId[0]}`) await model.photo.findByIdAndUpdate ...

  10. Java占位符

    一.背景 在使用java开发的过程中,经常需要使用将字符串拼接到一起(比如,用于日志输出),常用方法如下: 使用+将不同字符串进行拼接 使用StringBuilder 使用String.format ...