HDU1097 A hard puzzle,hdu1097hardpuzzle
A hard puzzleTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29415 Accepted Submission(s): 10581
Problem Descriptionlcy 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.
InputThere are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
OutputFor each test case, you should output the a^b's last digit number.
Sample Input
7 668 800
Sample Output
96
#include <stdio.h>#include <string.h>const char *sam[] = {"0", "1", "2486", "3971", "46", "5", "6", "7931", "8426", "91"};int main(){ int a, b; while(scanf("%d%d", &a, &b) == 2) printf("%c\n", sam[a % 10][(b-1) % strlen(sam[a%10])]); return 0;}
C++杭電OJ 1097 A Hard Puzzle的問題
演算法上出了問題 :
其實只用尋找規律即可.
a % 10 | 情況數 (由b mod 情況數 (如1的情況數為1 , 2的情況數為4 ... )得到)
0 | 只能是0
1 | 只能是1
2 | 2 4 8 6
3 | 3 9 7 1
4 | 4 6
5 | 只能是5
6 | 只能是6
7 | 7 9 3 1
8 | 8 4 2 6
9 | 9 1
AC 代碼如下所示:
#include <iostream>
using namespace std;
int a,b;
int main() {
while(cin >> a >> b)
{
int a1 = a % 10;
if(a1 == 1 || a1 == 5 || a1 == 6 || a1 == 0)
cout << a1 << endl;
else
{
int c = b%4;
if(a1 == 2)
{
if(c==0)
cout << 6 << endl;
else if(c==1)
cout << 2 << endl;
else if(c==2)
cout << 4 << endl;
else
cout << 8 << endl;
}
else if(a1 == 3)
{
if(c==0)
cout << 1 << endl;
else if(c==1)
cout << 3 << endl;
else if(c==2)
cout << 9 << endl;
else
cout << 7 << endl;
}
else if(a1 == 7)
{
if(c==0)
cout << 1 << endl;
else if(c==1)
cout << 7 << endl;
else if(c==2)
cout << 9 << endl;
else
cout << 3 << endl;
}
else if(a1 == 8)
{
if(c==0)
cout << 6 << endl;
else if(c==1)
cout << 8 << endl;
else if(c==2)
cout << 4 << endl;
else
cout << 2 << endl;
}
else if(a1 == 4)
{
c = b%2;
if(c==0)
cout << 6 << endl;
else
cout << 4 << endl;
}
else if(a1 == 9)
{
c = b%2;
if(c==0)
cout << 1 << endl;
else
cout << 9 << endl;
}
}
}
return 0;
}...餘下全文>>
杭電acm1097為何是wrong answer
//修改你的代碼AC
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
__int64 n,m;
int a[5];
while(scanf("%I64d%I64d",&n,&m)==2)
{
if(n%10==0||n%10==1||n%10==5||n%10==6)
{
printf("%I64d\n",n%10);
}
else
{
a[1]=n%10;
a[2]=(a[1]*a[1])%10;//a[n]=a[n-1]*a[1]
a[3]=(a[2]*a[1])%10;//
a[4]=(a[3]*a[1])%10;//
a[0]=a[4];
printf("%d\n",a[m%4]);
}
}
system("PAUSE");
return 0;
}
//AC
//題目意思就是求a^b mod 10
//用二分法求冪
#include<iostream>
typedef unsigned long long ull;
ull MOD_EXP(ull a,ull b,ull n){
ull ret=1;
for(;b;b>>=1,a=a*a%n)
if (b&1)
ret=ret*a%n;
return ret;
}
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)==2)
printf("%d\n",MOD_EXP(a,b,10));
}