题目:已知S = Σ(i^i)其中(1 <= i <= N ),计算S最后一位的数字。
分析:数论,找规律。设 i = 10*a+b 则 i^i = (10*a+b)^(10*a+b),有:
记f(i)=(i^i)% 10 = (10*a+b)^(10*a+b)% 10 = b ^(10*a+b) { 二项式定理 }
利用f(i)找规律:
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;{ 同上 }
f(10*k+4)= 6;
f(10*k+5)= 5;
f(10*k+6)= 6;
f(10*k+7)= 3,7;{ 同上 }
f(10*k+8)= 6,4;{ 同上 }
f(10*k+3)= 9;
因此,可以得到结论循环节为20的倍数:
0,1,5,2,8,3,9,2,8,7,7,8,4,7,3,8,4,1,5,4,
4,5,9,6,2,7,3,6,2,1,1,2,8,1,7,2,8,5,9,8,
8,9,3,0,6,1,7,0,6,5,5,6,2,5,1,6,2,9,3,2,
2,3,7,4,0,5,1,4,0,9,9,0,6,9,5,0,6,3,7,6,
6,7,1,8,4,9,5,8,4,3,3,4,0,3,9,4,0,7,1,0,
因为每次循环20个之后,尾数都比上一次的多4,因此循环节为100;
取出输入的数字最后的2位(即%100),输出 ((value%20 + 4*value / 20))% 10 即可。
说明:(⊙_⊙)
#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; }
UVa 10162 - Last Digit,布布扣,bubuko.com
原文地址:http://blog.csdn.net/mobius_strip/article/details/37757287