快速幂取余

来源:互联网 发布:泰国旅游 知乎 编辑:程序博客网 时间:2024/04/29 19:47

http://codevs.cn/problem/3500/
http://www.acmicpc.sdnu.edu.cn/problem/show/1056
快速幂取余,原来看代码以为特别难,但没想到,原来这么简单啊。
详情看以下文章:http://blog.csdn.net/xuruoxin/article/details/8578992
PS:第二个链接的数据很弱int 就可以过,第一个很强,longlong 也不过 有一个是9亿多,我也没继续做。

#include<iostream>#include<stdio.h>#include<cmath>#include<string>#include<algorithm>using namespace std;int main(){    long long a, b, k;    long long daan = 1;    cin >> a >> b >> k;    a = a%k;    while (b > 0)    {        if (b%2==1)        {            daan = (daan*a) % k;        }        b = b / 2;        a = a*a%k;    }    cout << daan << endl;    system("pause");    return 0;}
0 0
原创粉丝点击