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;
}
}
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 ...
- 【HDOJ】1097 A hard puzzle
题目和1061非常相似,几乎可以复用. #include <stdio.h> ][]; int main() { int a, b; int i, j; ; i<; ++i) { b ...
- hdu 1097 A hard puzzle
Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...
- hdu 1097 A hard puzzle 快速幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...
- 【HDOJ】1857 Word Puzzle
trie树.以puzzle做trie树内存不够,从puzzle中直接找串应该会TLE.其实可以将查询组成trie树,离线做.扫描puzzle时注意仅三个方向即可. /* 1857 */ #includ ...
- HDOJ 1098 Ignatius's puzzle
Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...
- HDOJ 1061 Rightmost Digit(循环问题)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- HDOJ 5411 CRB and Puzzle 矩阵高速幂
直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP
dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...
随机推荐
- Tomcat中Listener的使用范例(转载http://cywhoyi.iteye.com/blog/2075848)
Tomcat是非常有名的开源容器,因其开源我们可以对其做定制化的改变,而且Tomcat在其配置文件方面做了很多注释说明摘要,帮助我们更好的定制化我们所需的功能点. New Tomcat Listene ...
- CentOS7搭建SAMBA服务器实现与WIN10匿名共享文件
1.安装SAMBA yum -y install samba samba-client samba-common 2.修改文件打开数 vi /etc/security/limits.conf 最后添加 ...
- SGU 199 - Beautiful People 最长上升子序列LIS
要邀请n个人参加party,每个人有力量值strength Si和魅力值 beauty Bi,如果存在两人S i ≤ S j and B i ≥ B j 或者 S i ≥ S j and B i ≤ ...
- python简单小爬虫爬取易车网图片
上代码: import requests,urllib.request from bs4 import BeautifulSoup url = 'http://photo.bitauto.com/' ...
- Java中的浅复制和深复制 Cloneable clone
先看一个简单案例 public class Test { public static void main(String args[]) { Student stu1 = new ...
- poj 2117 Electricity
/* Tarjan求割点 */ #include<iostream> #include<cstdio> #include<cstring> #include< ...
- JavaScript Unicode字符操作
charCodeAt() 方法 定义和用法charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数.方法 charCodeAt() 与 ...
- Asp.net 事务处理
事务处理是在数据处理时经常遇到的问题,经常用到的方法有以下三种总结整理如下:方法1:直接写入到sql 中在存储过程中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRA ...
- 【转】 自定义iOS7导航栏背景,标题和返回按钮文字颜色
原文:http://blog.csdn.net/mad1989/article/details/41516743 UIBarButtonItem,navigationItem,backBarButto ...
- uvalive 5760 Alice and Bob (组合游戏,dp)
题目链接: http://vjudge.net/problem/viewProblem.action?id=25636 对于>1的堆,必然会被其中一人全部合并. 然后就是二维dp,dp[非1堆的 ...