Ultraviolet A 10162-last digit

Source: Internet
Author: User

Question: We know S = Σ (I ^ I) Where (1 <= I <= N) calculates the number of the last digit of S.

Analysis: number theory. Find the rule. If I = 10 * A + B, then I ^ I = (10 * A + B) ^ (10 * A + B), which includes:

Note f (I) = (I ^ I) % 10 = (10 * A + B) ^ (10 * A + B) % 10 = B ^ (10 * A + B) {binary theorem}

Use f (I) to find the rule:

F (10 * k + 0) = 0;

F (10 * k + 1) = 1;

F (10 * k + 2) = 4,6; {F (2) = 4, F (12) = 6, F (22) = 4, F (32) = 6 ,...}

F (10 * k + 3) = 7,3; {same as above}

F (10 * k + 4) = 6;

F (10 * k + 5) = 5;

F (10 * k + 6) = 6;

F (10 * k + 7) = 3,7; {same as above}

F (10 * k + 8) = 6, 4; {same as above}

F (10 * k + 3) = 9;

Therefore, we can conclude that the cycle is a multiple of 20:

,

,

,

,

6, 7,

Because the number of tails after 20 cycles is 4 more than the previous cycle, the cycle is 100;

Take out the last two digits (% 100) of the input number and output (Value % 20 + 4 * value/20) % 10.

Note: (⊙ _ ⊙)

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;int maps[20] = {0,1,5,2,8,3,9,2,8,7,7,8,4,7,3,8,4,1,5,4};int main(){string str;while ( cin >> str ) {int len = str.length();if ( len == 1 && str[0] == '0' )break;int value = str[len-1]-'0';if ( len > 1 )value += (str[len-2]-'0')*10;cout << (maps[value%20]+value/20*4)%10 << endl;}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.