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="样式名" ...
随机推荐
- 游戏碰撞OBB算法(java代码)
业务需求 游戏2D型号有圆形和矩形,推断说白了就是碰撞检测 : 1.圆形跟圆形是否有相交 2.圆形跟矩形是否相交 3.矩形和矩形是否相交 ...
- GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置
配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...
- 清除Android工程中没用到的资源(转)
项目需求一改再改,UI一调再调,结果就是项目中一堆已经用不到但却没有清理的垃圾资源,不说工程大小问题,对新进入项目的人或看其他模块的代码的人来说,这些没清理的资源可能也可能会带来困扰,所以最好还是清理 ...
- 一个简单的带缓存http代理
眼下1.0版模型非常easy.即对客户机发来的请求进行简单处理后,转发到server.转发之前先检查本地缓存.假设有.则直接回送给客户本地资源 程序流程大致例如以下图: 缓存是通过把文件保存到磁盘上, ...
- 隐藏快捷方式扩展名(.lnk)
自从装了Windows 7操作系统以后,有一个问题一直困扰着我.当我去掉“目录选项中”的“隐藏已知文件类型的扩展名”选项的“√”之后,桌面上的快捷方式的扩展名也出来了,看上去非常不爽.记得在XP.Vi ...
- 如何与多个线程的操作epoll fd
自己曾经做一个接口server时候,这样的场景下我的设计是多个线程操作同一个epoll fd.彼时,我的理由是epoll的系列函数是线程安全的. 当然有人不理解为什么会有多个线程操作同一个epoll ...
- 移动web:tab选项卡
平常做移动端会用到tab选项卡,这和PC端有些区别,移动端是触摸滑动切换,PC端是点击.移入切换. 这里滑动切换就是一个移动端事件的应用,这里主要用到的触摸事件:touchstart.touchmov ...
- newinstance()和new有什么区别?(转)
在初始化一个类,生成一个实例的时候:newInstance() 和 new 有什么区别? 用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有 ...
- Amazon SQS简单介绍 上篇
SQS即Simple Queue Service, 是一个分布式的消息队列服务,使用它很easy,消息队列服务能够用来buffer burst, 使整个服务异步处理,不要求组件始终可用. 开发者最初使 ...
- decimal ? 含义
例如: decimal ? je = zfje; 意思是 将 JE赋值为 ZFJE , 并且允许 JE 为 NULL 值 这时JE为引用类型