A hard puzzle—1097
来源:互联网 发布:软件体系结构风格 编辑:程序博客网 时间:2024/06/04 18:59
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
7 668 800
Sample Output
96
分析
求a的b次方得到的结果的最后一位,其实就是求a的个位数的b次方的最后一位数,例如13的124次方(13^124):3,3×3=9,3×9=27,3×7=21,3×1=3,最后一位总是呈现出一个循环的状态,可以利用这一性质来求解问题,用到一个vector来存储每次的最后一位。
C++代码
/************************************************* Copyright: 武汉大学计算机学院B507 Author: RyanDate: 2015-11-25 Description:求a^b的最后一位 **************************************************/ #include<iostream>#include<vector>using namespace std;int main(){ int a,b; while(cin>>a>>b) { //0的0次方没有意义 if(!a && !b) return 0; a=a%10; //取a的个位数 //个位为0,1,5,6的数字得到的结果不变 if(a==0 || a==1 || a==5 || a==6){ cout<<a<<endl; } else { vector<int> vec; vec.push_back(a); int temp=(a*a)%10; while(temp!=a) { vec.push_back(temp); temp=(a*temp)%10; } int res = b%vec.size(); cout<<(res==0?vec.back():vec[res-1])<<endl; } } return 0; }
总结
最后代码AC了,一次性通过。
0 0
- A hard puzzle—1097
- 1097 A hard puzzle
- 1097A hard puzzle
- 1097 A hard puzzle
- A hard puzzle(1097)
- 1097 A hard puzzle
- 1097 A hard puzzle
- 1097A hard puzzle
- 1097 A hard puzzle
- hdu 1097 A hard puzzle
- hdu-1097 A hard puzzle
- HDU 1097 A Hard Puzzle
- HDU 1097 A hard puzzle
- HDOJ 1097 A hard puzzle
- HDU-1097A hard puzzle
- hdu 1097 A hard puzzle
- HDOJ 1097 A hard puzzle
- HDU 1097 A hard puzzle
- GDB技巧整理
- PHPExcel读取excel并导入数据库
- 安卓开发中Theme.AppCompat.Light的解决方法
- Linux TCP服务端接收连接数量突破65535的方法
- rust 的内存管理
- A hard puzzle—1097
- Android Eclipse集成NDK
- arcgis二次开发无法引用 ESRI.ArcGIS.DataSourcesFile解决方法
- SVN上实现代码风格自动检查
- Java中的HashMap和HashTable的不同点
- Android KeyCode列表
- 比较简单的图论最短路+搜索 CF The Two Routes
- fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
- C#读取设置Cookie