-
N<k时,root(N,k) = N,否则,root(N,k) = root(N',k)。N'为N的k进制表示的各位数字之和。输入x,y,k,输出root(x^y,k)的值 (这里^为乘方,不是异或),2=<k<=16,0<x,y<2000000000,有一半的测试点里 x^y 会溢出int的范围(>=2000000000)
-
输入:
-
每组测试数据包括一行,x(0<x<2000000000), y(0<y<2000000000), k(2<=k<=16)
-
输出:
-
输入可能有多组数据,对于每一组数据,root(x^y, k)的值
-
样例输入:
-
4 4 10
-
样例输出:
-
4
#include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> #include <algorithm> using namespace std; #define LL long long int LL clm(LL x, LL y, int n) { LL ret = 1; while(y) { if(y&1) ret = ((ret%n)*(x%n))%n; x = ((x%n)*(x%n))%n; y = y >> 1; } return ret; } int main() { LL a, b, ans; int k; while(scanf("%lld %lld %d", &a, &b, &k) == 3) { ans = clm(a,b,k-1); if(ans == 0) ans = k - 1; printf("%d\n",ans); } system("pause"); return 0; }