5208 求乘方取模

时间限制: 1 s

空间限制: 1000 KB

题目等级 : 未定级

题目描述 Description

给定非负整数A、B、M,求(A ^ B) mod M。

输入描述 Input Description

包含多组输入,输入处理到EOF。

每组输入仅一行,三个用空格隔开的非负整数A、B、M。

输出描述 Output Description

对于每组输入,输出一行,一个非负整数,即(A ^ B) mod M。

样例输入 Sample Input

2 3 100006

32 71 83

900 800 777

样例输出 Sample Output

8

5

219

数据范围及提示 Data Size & Hint

0 <= A, B < 8 * 10^18。

0 < M < 8 * 10^18。

保证A和B不同时为0。

/*
快速幂.
快速乘法防爆.
*/
#include<iostream>
#include<cstdio>
#define LL unsigned long long
using namespace std;
LL a,b,k;
LL Mul(LL a,LL b,LL c)
{
LL ans=0;
while(b)
{
if(b&1)
{
b--;ans+=a;ans%=c;
}
b>>=1;a<<=1;a%=c;
}
return ans;
}
LL fast_mi(LL a,LL b,LL k){
LL tot=1;
while(b){
if(b&1) tot=Mul(tot,a,k);
a=Mul(a,a,k);
b>>=1;
}
return tot;
}
int main()
{
while(cin>>a>>b>>k){
if(!a)printf("0\n");
else if(!b) {
cout<<1%k;printf("\n");
}
else {
cout<<fast_mi(a,b,k);
printf("\n");
}
}
return 0;
}

Codevs 5208 求乘方取模的更多相关文章

  1. NYOJ--102--次方求模(快速求幂取模)

    次方求模 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 求a的b次方对c取余的值   输入 第一行输入一个整数n表示测试数据的组数(n<100)每组测试只有一 ...

  2. E - A^B mod C (大数乘方取模)

    Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63) ...

  3. POJ 3761 Bubble Sort(乘方取模)

    点我看题目 题意 : 冒泡排序的原理众所周知,需要扫描很多遍.而现在是求1到n的各种排列中,需要扫描k遍就变为有序的数列的个数,结果模20100713,当然了,只要数列有序就扫描结束,不需要像真正的冒 ...

  4. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  5. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  6. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模

    题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...

  7. 二分求幂/快速幂取模运算——root(N,k)

    二分求幂 int getMi(int a,int b) { ; ) { //当二进制位k位为1时,需要累乘a的2^k次方,然后用ans保存 == ) { ans *= a; } a *= a; b / ...

  8. C语言fmod()函数:对浮点数取模(求余)

    头文件:#include <math.h> fmod() 用来对浮点数进行取模(求余),其原型为:    double fmod (double x); 设返回值为 ret,那么 x = ...

  9. 【learning】多项式相关(求逆、开根、除法、取模)

    (首先要%miskcoo,这位dalao写的博客(这里)实在是太强啦qwq大部分多项式相关的知识都是从这位dalao博客里面学的,下面这篇东西是自己对其博客学习后的一些总结和想法,大部分是按照其博客里 ...

随机推荐

  1. org.apache.hadoop.hbase.TableNotDisabledException 解决方法

    Exception in thread "main" org.apache.hadoop.hbase.TableNotDisabledException: org.apache.h ...

  2. Dynamices CRM Permission Issue (Security role UI to privilege mapping)'s solution

    select * from privilege where  privilegeid = 'a4736385-9763-4a64-a44b-cd5933edc631' Security role UI ...

  3. 解决IE下iframe默认有白色背景的bug

    又是一个IE莫名其妙的bug:做了一个弹出层,层里面是一张透明图片,IE下的iframe默认会有一个白色的背景,所以当iframe的外围背景并不是白色的时候,这个iframe就会显得非常的“与众不同” ...

  4. javascript闭包问题

    <script type="text/javascript"> window.onload = function(){ var name = "The Win ...

  5. iOS从生成证书到打包上架-02(详细2016-10最新)

    由于篇幅的限制,这篇接着上一篇(关于证书)写的,有需要的小伙伴可以先阅读上一篇 2.在App Store创建应用 1.回到Account,点击iTunes Connect 2.点击我的App 3.点击 ...

  6. 获取datagrid中编辑列combobox的value值与text值

    var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'}); var productname = $(ed ...

  7. ios8 关于UIAlertController 代替UIActionsheet

    self.alertController=[UIAlertController alertControllerWithTitle:@"请选择\n\n\n\n\n\n\n\n\n\n\n\n\ ...

  8. scala编程笔记(三)类,字段和方法

    类,字段和方法 类是对象的蓝图,能够通过new来创建对象.在类的定义里能够有字段和方法.统称member val还是var都是指向对象的变量(var定义的字段可又一次赋值),def定义方法,包括可运行 ...

  9. 同步窗体移动 FormMove

    方法2 unit Unit1;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,  Sy ...

  10. [Webpack 2] Use Karma for Unit Testing with Webpack

    When writing tests run by Karma for an application that’s bundled with webpack, it’s easiest to inte ...