hdu 1061 Rightmost Digit

來源:互聯網
上載者:User

標籤:

解決本題使用數學中的快速冪取餘:

該方法總結挺好的:具體參考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html

 

#include<iostream>#include<cmath>using namespace std;int PowerMod(__int64 a,__int64 b,int c)//快速冪取餘 {    int ans=1;    a=a%c;    while(b>0)    {        if(b%2==1)//如果為奇數時,要多求一步,可以提前放到ans中           ans=(ans*a)%c;        b=b/2;//不斷迭代         a=(a*a)%c;//把(a^2)%c看成一個整體     }    return ans;}int main(){    int n;    cin>>n;    __int64 m;    while(n--)    {        cin>>m;        cout<<PowerMod(m,m,10)<<endl;    }    return 0;}
View Code

 

還有其他的方法比如數學規律等 但個人覺得這種方法稍難:

#include<stdio.h>int main(){ __int64 n; int a[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}},d,t;//找規律 scanf("%d",&t); while(t--) {  scanf("%I64d",&n);  d=n%10;  if(d==0||d==1||d==5||d==6)   printf("%d\n",d);  else if(d==4||d==9)   printf("%d\n",a[d][n%2]);  else   printf("%d\n",a[d][n%4]); } return 0;}
View Code

 

hdu 1061 Rightmost Digit

聯繫我們

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