HDOJ 1061 Rightmost Digit

來源:互聯網
上載者:User

標籤:

找出數學規律

 

原題:

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6515    Accepted Submission(s): 2454


Problem DescriptionGiven a positive integer N, you should output the most right digit of N^N. 

 

InputThe input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000). 

 

OutputFor each test case, you should output the rightmost digit of N^N. 

 

Sample Input
234
 

 

Sample Output
76
HintIn the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

 

 

講解:(摘自網上)

  對於數字0~9, 它們的N次方,我們只看最後一個數字:
  0^1=0; 0^2=0; 重複持續時間T=1;
  1^1=1; 1^2=1; 重複持續時間T=1;
  2^1=2; 2^2=4; 2^3=8; 2^4=6 2^5=2; 重複持續時間T=4;
  3^1=3; 3^2=9; 3^3=7; 3^4=1;3^5=3; 重複持續時間T=4;
  4^1=4; 4^2=6; 4^3=4; 重複持續時間T=2;
  
  後面的大家可以自己算,會得出一個規律:
  他們的重複持續時間只有1,2,4這三種,所以對於原始碼裡的a,我們可以只算a%4次就可以了,大大減少了迴圈的次數

 

 

原始碼:

 1 #include <iostream> 2 using namespace std; 3  4 int zhou[10]; 5  6 int main()    { 7     int N;    cin >> N; 8     while (N--)    { 9         long long int num;10         cin >> num;11         int a = num % 10;12         int b = (a * a) % 10;13         int c = (b * a) % 10;14         int d = (c * a) % 10;15         int m = num % 4;16         switch(m)    {17             case 0: cout << d << endl;    break;18             case 1:    cout << a << endl;    break;19             case 2: cout << b << endl;    break;20             case 3: cout << c << endl;    break;21         }22     }23     return 0;24 } 

 

HDOJ 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.