NYOJ 473 A^B Problem

來源:互聯網
上載者:User

標籤:

#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

NYOJ 473 A^B Problem

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.