给出3个正整数A B C,求A^B Mod C。

例如,3 5 8,3^5 Mod 8 = 3。

Input

3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)

Output

输出计算结果

Input示例

3 5 8

Output示例

3

用到了快速幂 ,挑战P123

比如x ^22 = x ^16 *x ^4*x ^2;

22 转换成二进制是10110;

#include <iostream>
using namespace std;
typedef long long ll; ll pow_mod(ll x,ll n,ll mod)
{
ll res = ;
while ( n > )
{
if(n & ) res = res * x % mod;
x = x * x % mod;
n >>= ;
//cout<< n << endl;
}
return res;
} int main()
{
int x,n,mod;
cin >> x >> n >> mod;
cout<<pow_mod(x,n,mod)<<endl;
return ;
}

利用位进制

下面是递归版本

如果n为 偶数  那么 x^n = (x^2) ^ (n/2)    n为奇数   无非多乘一个n

#include <iostream>
using namespace std;
typedef long long ll; ll pow_mod1(ll x,ll n,ll mod)
{
ll res = ;
if(n == ) return ;
res = pow_mod1(x * x % mod, n/,mod);
if(n % )
res = res * x % mod;
return res;
} int main()
{
int x,n,mod;
cin >> x >> n >> mod;
cout<<pow_mod1(x,n,mod)<<endl;
return ;
}

递归版

最新文章

  1. js动画之多物体运动
  2. html5的选择器
  3. 2014 Super Training #1 C Ice-sugar Gourd 模拟,扫描线
  4. Android模拟器disconnected问题
  5. Rsync+sersync文件实时同步
  6. Redhat 使用中文安装后更换为英文的设定
  7. Selenium自动化测试环境搭建汇总(一):Selenium+Eclipse+Junit+TestNG
  8. sendrose【SPFA】
  9. 使用weinre远程调试
  10. 如何使用webpack优化首屏渲染时间
  11. Task 的用法
  12. js两个箭头 =&gt;()=&gt;()
  13. P3331 [ZJOI2011]礼物(GIFT)
  14. 【C#】扩展方法浅谈
  15. ASP.NET MVC:WebPageBase.cs
  16. C# 一些代码小结--串口操作
  17. maven属性、profile、资源过滤、不同环境构建项目
  18. linux 监控CPU 内存情况
  19. python标准库介绍——2 os.path模块详解
  20. python中numpy计算数组的行列式numpy.linalg.det()

热门文章

  1. Elasticsearch教程-从入门到精通(转)
  2. Redis添加历史浏览记录
  3. ie兼容图片缩小后模糊失真(锯齿)问题
  4. linux上mysql安装详细教程
  5. poj3468A Simple Problem with Integers(线段树的区域更新)
  6. Py中的多维数组ndarray学习【转载】
  7. 基于comet服务器推送技术(web实时聊天)
  8. 去掉python的警告
  9. workerman定时任务使用
  10. FormatMessage函数