2013
12-12

# 九度-1085-求root(N, k)[解题代码]

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

cpp 代码如下：
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
using namespace std;

typedef long long INT;

//计算幂取模a^b mod n, O(logb)
int modular_exponent(INT a,INT b,INT n){
int ret=1;
for (;b;b>>=1,a=a*a%n)
if (b&1)
ret=ret*a%n;
return ret;
}

int main()
{
int x, y, k;
int i;
int ret;

while(scanf("%d%d%d", &x, &y, &k) == 3)
{
ret = modular_exponent(x, y, k-1);
if(ret == 0)
ret = k-1;
printf("%d\n", ret);
}
return 0;
}

/**************************************************************
Problem: 1085
User: coder
Language: C++
Result: Accepted
Time:10 ms
Memory:1512 kb
****************************************************************/

1. [email protected]

2. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.