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="样式名" ...
随机推荐
- Qt中截图功能的实现
提要 需求:载入一张图片并显示,能够放大缩小,能够截取图片的某个矩形并保存. 原以为蛮简单的一个功能,事实上还是有点小复杂. 最简单Qt图片浏览器能够參考Qt自带的Demo:Image Viewer ...
- 【HTML+CSS】(1)基本语法
HTML基金会 <em>他强调标签,<strong>加粗标签 <q>短文本引用.<blockquote>长文本引用,这两个标签会让文字带双引號. 空 ...
- 读懂IL
读懂IL 先说说学IL有什么用,有人可能觉得这玩意平常写代码又用不上,学了有个卵用.到底有没有卵用呢,暂且也不说什么学了可以看看一些语法糖的实现,或对.net理解更深一点这些虚头巴脑的东西.最重要的理 ...
- Swift语言教程中文文档
Swift语言教程中文文档 Swift语言教程(一)基础数据类型 Swift语言教程(二)基础数据类型 Swift语言教程(三)集合类型 Swift语言教程(四) 集合类型 Swift语言教程(五)控 ...
- iOS缓存类的设计
使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...
- Cocos2d-x学习笔记(六) 定时器Schedule的简单应用
Cocos2d-x中的定时器使用非常easy,共同拥有3种:schedule.scheduleUpdate和scheduleOnce.简介一下三种的差别: schedule,每隔指定时间运行某个 ...
- 关于scope_identity()与 @@IDENTITY
原文:关于scope_identity()与 @@IDENTITY 参考:https://msdn.microsoft.com/zh-cn/library/ms190315.aspx scope_id ...
- Cocos2d-x 3.0final 终结者系列教程01-无论是从cocos2d-x2.x升级到版本cocos2d-x3.x
诡谲的江湖,易变. 花花世界,车来人往. 最终确定.安家,将Cocos2d-x3.0final相关技术精加工的版本.并推出了博客文章, 不为他人,只为自己. 学习交流QQ群:301954471 --- ...
- UNITY3D MAC版本号破解
首先,解释一下.是公司做开发建议去购买正版. 之前网上也有非常多人贴出了破解方法,有些也是能够的.可是大多数解说不太具体,在这里贴出相对具体点的教程.本人亲測成功(測试版本Unity4.0.1 mac ...
- MailTest
GridBagLayout把一个界面分为m行n列的网格 GridBagConstraints的一个实例:gridx = 2; // X2,表示组件位于第2列gridy = 0; // Y0,表示组件位 ...