a^b%c 的三种形式
求a^b%c,(1 <= a,b <= 2^62, 1 <= c <= 10^9)
最主要的高速幂
_LL mod_exp(_LL a, _LL b, int c)
{
_LL ans = 1;
_LL t = a%c; while(b)
{
if(b&1)
ans = ans*t%c;
t = t*t%c;
b >>= 1;
}
return ans;
}
求a^b%c,(1 <= a,b,c <= 2^62)
由于c在long long范围内,中间两个long long 相乘的时候会超long long。所以对乘法再写一个高速乘法的函数,将乘法变为加法,每加一次就进行取模,就不会超long long了。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define C 240
#define S 20
using namespace std;
const int maxn = 110; // a*b%c
LL mul_mod(LL a, LL b, LL c)
{
LL ans = 0;
a %= n;
b %= n;
while(b)
{
if(b&1)
{
ans = ans+a;
if(ans >= c) ans -= c;
}
a <<= 1;
if(a >= c)
a -= c;
b >>= 1;
}
return ans;
}
//a^b%c
LL pow_mod(LL a, LL b, LL c)
{
LL ans = 1;
a = a%c;
while(b)
{
if(b&1)
ans = mul_mod(ans,a,c);
a = mul_mod(a,a,c);
b >>= 1;
}
return ans;
}
int main()
{
LL a,b,c;
while(~scanf("%lld %lld %lld",&a,&b,&c))
{
LL ans = pow_mod(a,b,c);
printf("%lld\n",ans);
}
return 0;
}
求a^b%c (1 <= a,c <= 10^9, 1 <= b <= 10^1000000)
b相当大,高速幂超时。
有一个定理: a^b%c = a^(b%phi[c]+phi[c])%c,当中要满足b >= phi[c]。这样将b变为long long范围内的数,再进行高速幂。至于这个定理为什么成立,如今明确一点。这篇讲的挺具体:http://www.narutoacm.com/archives/a-pow-b-mod-m/
http://acm.fzu.edu.cn/problem.php?pid=1759
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define C 240
#define S 20
using namespace std;
const int maxn = 110; LL a,c,cc;
char b[1000010]; LL Eular(LL num)
{
LL res = num;
for(int i = 2; i*i <= num; i++)
{
if(num%i == 0)
{
res -= res/i;
while(num%i == 0)
num /= i;
}
}
if(num > 1)
res -= res/num;
return res;
} bool Comper(char s[], LL num)
{
char bit[30];
int len2 = 0,len1;
while(num)
{
bit[len2++] = num%10;
num = num/10;
}
bit[len2] = '\0'; len1 = strlen(s);
if(len1 > len2)
return true;
else if(len1 < len2)
return false;
else
{
for(int i = 0; i < len1; i++)
{
if(s[i] < bit[len1-i-1])
return false;
}
return true;
}
} //对大整数取模,一位一位的取。
LL Mod(char s[], LL num)
{
int len = strlen(s);
LL ans = 0;
for(int i = 0; i < len; i++)
{
ans = (ans*10 + s[i]-'0')%num;
}
return ans;
} LL Pow(LL a, LL b, LL c)
{
LL ans = 1;
a = a%c;
while(b)
{
if(b&1)
ans = ans*a%c;
a = a*a%c;
b >>= 1;
}
return ans;
} int main()
{
while(~scanf("%lld %s %lld",&a,b,&c))
{
LL phi_c = Eular(c);
LL ans;
if(Comper(b,phi_c)) // b >= phi[c]
{
cc = Mod(b,phi_c)+phi_c;
ans = Pow(a,cc,c);
}
else
{
int len = strlen(b);
cc = 0;
for(int i = 0; i < len; i++)
cc = cc*10 + b[i]-'0';
ans = Pow(a,cc,c);
}
printf("%lld\n",ans);
}
return 0;
}
a^b%c 的三种形式的更多相关文章
- Qt学习 之 多线程程序设计(QT通过三种形式提供了对线程的支持)
QT通过三种形式提供了对线程的支持.它们分别是, 一.平台无关的线程类 二.线程安全的事件投递 三.跨线程的信号-槽连接. 这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线 ...
- spring对事务支持的三种形式
spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...
- Spring Framework5.0 学习(3)—— spring配置文件的三种形式
Spring Framework 是 IOC (Inversion of Control 控制反转)原则的实践. IoC is also known as dependency injection ...
- spring Bean配置的三种形式
Spring Bean配置有以下三种形式: 传统的xml配置 Spring 2.5 以后新增注解配置 Spring3.0以后新增JavaConfig 1. 传统的xml配置 <?xml vers ...
- 2、shader基本语法、变量类型、shader的三种形式、subshader、fallback、Pass LOD、tags
新建一个shader,名为MyShader1内容如下: 1._MainTex 为变量名 2.“Base (RGB)”表示在unity编辑面板中显示的名字,可以定义为中文 3.2D 表示变量的类型 4. ...
- PHP数组输出三种形式 PHP打印数组
PHP数组输出三种形式 PHP打印数组 $bbbb=array("11"=>"aaa","22"=>"bbb&qu ...
- FMDB使用的数据库的三种形式
FMDB使用的数据库的三种形式 FMDB是iOS平台下一款优秀的第三方SQLite数据库框架.它以Objective-C的方式封装了SQLite的C语言API.使用起来,它更加面向对象,避免冗余的 ...
- [ch04-05] 梯度下降的三种形式
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 4.5 梯度下降的三种形式 我们比较一下目前我们用三种方 ...
- CSS样式三种形式222
markdown CSS基本表现形式只有三种:标签样式.Class类样式.ID样式 标签样式: 必须与HTML标签同名.仅仅影响同名标签 Class样式:可以在任何标签中使用: class=" ...
- CSS样式三种形式
CSS基本表现形式只有三种:标签样式.Class类样式.ID样式 标签样式: 必须与HTML标签同名.仅仅影响同名标签 Class样式:可以在任何标签中使用: class="样式名" ...
随机推荐
- 阿赫亚web安全JSON
前言 JSON(JavaScript Object Notation),可以说,这一事实,浏览器,server数据交换标准.的格式如XML,或者其他自己定义的格式会越来越少. 为什么JSON这么流行? ...
- 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示
原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(二)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...
- 在VC/MFC中嵌入Google地图——图文并茂
近期须要实验室须要将在无人机地面站中嵌入地图,在网上找了非常多资料,最终有些眉目了, 首先.做这个须要用到的知识有.MFC控件.MFC类库.JavaScript脚本语言.Google API.Goog ...
- [ Talk is Cheap Show me the CODE ] : jQuery Mobile工具栏
[ Talk is Cheap Show me the CODE ] : jQuery Mobile工具栏 Written In The Font " Wirte less Do more& ...
- Python爬虫(一)
花了四天的时间用python写了个简单的爬虫程序.整个过程分为两个部分:工具的安装和程序的实现 本文并没有讲程序的详细实现遇到的问题,而是对着手前一些前期的准备 第一部分(工具的安装) 开发工具的下载 ...
- OCP解决问题052-- DROP PROFILE app_user
133.You created a profile APP_USER and assigned it to the users. After a month, you decide to drop t ...
- jquery省市区三级联动
jquery省市区三级联动(数据来源国家统计局官网)内附源码下载 很久很久没有写博了. 今天更新了项目的省市区三级联动数据,更新后最新的海南三沙都有,分享给所有需要的小伙伴们... JQUERY + ...
- 长方柱类【C++ 类定义】
Description 编写基于对象的程序,求长方柱(Bulk)的体积.数据成员包括长(length).宽(width).高(heigth).体积,要求用成员函数实现下面的功能: (1)由键盘输入长方 ...
- [LeetCode257]Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...