题解 | #求root(N, k)#

求root(N, k)

http://www.nowcoder.com/practice/9324a1458c564c4b9c4bfc3867a2aa66

#include<iostream>

using namespace std;

long root(long x,long y,int k){
    int result=1;
    while(y!=0){
        if(y%2==1){
            result *= x;
            result %= k;
        }
        x *= x;
        x %=k;
        y/=2;
    }
    return result;
}

int main(){
    int x,y,k;
    while(cin>>x>>y>>k){
        int result=root(x,y,k-1);
        if(result==0){
            result = k-1;
        }
        cout<<result;
    }
    return 0;
}
全部评论

相关推荐

吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务