hdu 1079 A hard puzzle

第一次在这个Blog上发表文章,有点小兴奋,先来到简单点的题吧。

其实这就是一道水题,以前做过,感觉不难,但我却WA在了数据范围上,郁闷啊~~不过也学到了不少知识,平衡了~~
我的做法:
 1 #include<stdio.h>
2 #include<iostream>
3 using namespace std;
4 int main()
5 {
6 __int64 a,b,i,s[102];
7 while(scanf("%I64d%I64d",&a,&b)!=EOF)
8 {
9 s[1]=a%10;
10 for(i=2;i<=b;i++)
11 {
12 s[i]=s[i-1]*a%10;
13 if(s[i]==s[1])
14 break;
15 }
16 i=i-1;
17 s[0]=s[i];
18 b=b%i;
19 printf("%I64d\n",s[b]);
20 }
21 return 0;
22 }
在网上看到的一种做法,感觉很强大:
 1 #include<stdio.h>
2 #include<stdlib.h>
3 int cal(int a,int n,int b)
4 {
5 if(n==0) return 1;
6 if(n==1) return a%b;
7 int t=cal(a,n/2,b);
8 t=t*t%10;
9 if((n&1)==1) t=t*a%b;
10 return t;
11 }
12 int main()
13 {
14 int a,b;
15 while(scanf("%d%d",&a,&b)!=EOF)
16 {
17 int k=cal(a,b,10);
18 printf("%d\n",k);
19 }
20 return 0;
21 }
还学到了一点小知识,就是n&1只有在n为奇数时才等于1,恩,以后可以用它判断奇偶了~
原文地址:https://www.cnblogs.com/misty1/p/2260741.html