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="样式名" ...
随机推荐
- Google Maps Android API v2 (4)- 地图类型
地图类型 地图内的谷歌地图的Android API的种类有很多.地图的类型管辖地图的整体代表性.例如,地图集通常包含政治地图,专注于显示边界和道路地图,显示了一个城市或地区的所有道路. Android ...
- CentOS6.2安装memcache
一,安装libevent # cd /tmp # wget http://www.monkey.org/~provos/libevent-1.3.tar.gz # tar -zxvf libevent ...
- 9、Cocos2dx 3.0游戏开发找小三之工厂方法模式与对象传值
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27704153 工厂方法模式 工厂方法是程序设计中一个 ...
- JS学习笔记-OO疑问之封装
封装是面向对象的基础,今天所要学习的匿名函数与闭包就是为了实现JS的面向对象封装.封装实现.封装变量,提高数据.系统安全性,封装正是面向对象的基础. 一.匿名函数 即没有名字的函数,其创建方式为 fu ...
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- java: org.luaj.vm2.LuaError:XXX module not found lua脚本初始化出错(转)
我遇到这个错误是因为在引用脚本目录时,设置错了位置.设置成脚本所在目录的上级目录. lua使用和加载初始化方法 在java中使用lua,使用需要引用 luaj-jse-2.0.2.jar 同时需要使用 ...
- 【rman,1】经典案例增量备份
一.备份策略: 1.星期天晚上 -level 0 backup performed(全备份) 2.星期一晚上 -level 2 backup performed 3.星期二晚上 ...
- Bulk Insert具体订单
Bulk Insert具体订单 BULK INSERT与用户指定的格式的数据文件复制到数据库表或视图. 语法: BULK INSERT [ [ 'database_name'.][ 'owner' ] ...
- Ecshop他们主动双语版切换来推断个人的计划
个人思路是基于浏览器的语言来推断自己主动,假设中国的浏览器,对使用中国模板.将英语模板.于.英国的模板差值称为不同的产品类别.文章分类,的模板可设置为相同的固定的文本language,所以你不会有打造 ...
- spring常规任务(轻便易)
spring提供了定时任务功能.我们不需要第三者jar包支持.spring够了. 代码: package com.inth.product.web.task; import java.util.Dat ...