k尾相加
从键盘输入一个自然数K(99999999>K>1),若存在自然数M和N(M>N),使得K的M次方和K的N次方均大于或等于1000,且它们的未尾三位数相等,则称M和N是一对“K尾相等数”。请编程序,输出K尾相等数中M+N最小值。求大侠解救......
#include <iostream> unsigned ktail( unsigned k ) { unsigned tail[1000] = { 0 }; int n = 1; int kn = k; for( ; kn<1000; ++n ) kn *= k; kn %= 1000; tail[kn] = n; k %= 1000; int m = n + 1; int km = (kn*k)%1000; for( ; tail[km]==0; ++m ) { tail[km] = m; km = (km*k)%1000; } std::cout << m << '+' << tail[km] << '=' << m+tail[km] << std::endl; return m+tail[km]; } int main() { ktail( 2 ); return 0; }