NYOJ 473 A^B Problem

#include<stdio.h>
int main()
{
  int a,b;
  int i;
  while(scanf("%d%d",&a,&b)!=EOF)
  {
    if(b==0)
      printf("1\n");
    else
    {
      a%=10;
      b=b%4+4;
      int s=a;
      for(i=1;i<b;i++)
      s*=a;
      s%=10;
      printf("%d\n",s);
    }
  }
}       //   借鉴

说一下原理,因为本题很特殊,只需要求出个位数字,开始的时候用同余定理,肯定会超时,换一种办法
下面是个位数字的规律
1: 周期为1, 结尾为1
2: 周期为4, 结尾为2 4 8 6
3: 周期为4, 结尾为3 9 7 1
4: 周期为2, 结尾为4 6
5: 周期为1, 结尾为5
6: 周期为1, 结尾为6
7: 周期为4, 结尾为7 9 3 1
8: 周期为4, 结尾为8 4 2 6
9: 周期为2, 结尾为9 
10: 周期为1, 结尾为0 

通过上面的规律很快就可以得到周期最大是4,只要对b%4,因为求的是个位数字,那么也直接对a%10

posted @ 2015-07-26 17:13  小松鼠。  阅读(109)  评论(0编辑  收藏  举报