HDU1097 A hard puzzle,hdu1097hardpuzzle

來源:互聯網
上載者:User

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));
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.