Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021
Description
Everybody loves big numbers (if you do not, you might want to stop reading at this point). There are many ways of constructing really big numbers known to humankind, for instance:
- Exponentiation: 422016=42⋅42⋅...⋅422016 times422016=42⋅42⋅...⋅42⏟2016 times.
- Factorials: 2016!=2016 ⋅ 2015 ⋅ ... ⋅ 2 ⋅ 1.

In this problem we look at their lesser-known love-child the exponial, which is an operation defined for all positive integers n as
exponial(n)=n(n − 1)(n − 2)⋯21
For example, exponial(1)=1 and exponial(5)=54321 ≈ 6.206 ⋅ 10183230 which is already pretty big. Note that exponentiation is right-associative: abc = a(bc).
Since the exponials are really big, they can be a bit unwieldy to work with. Therefore we would like you to write a program which computes exponial(n) mod m (the remainder of exponial(n) when dividing by m).
Input
There will be several test cases. For the each case, the input consists of two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 109).
Output
Output a single integer, the value of exponial(n) mod m.
Sample Input
2 42
5 123456789
94 265
Sample Output
思路:本题是一道经典的指数循环定理简记e(n)=exponial(n)e(n)=exponial(n),利用欧拉定理进行降幂即可,不过要注意会爆int。指数循环公式为指数循环公式为A^B = A^(B % φ(C) + φ(C)) % C,其中 φ(C)为1~C-1中与C互质的数的个数。

代码如下:
#include <cstdio>
#include <cstring> typedef long long ll;
int n, m; ll euler(int n) {
ll ans = n;
for(int i = ; i * i <= n; i++) {
if(n % i == ) {
ans = ans / i * (i - );
while(n % i == ) n /= i;
}
}
if(n > ) ans = ans / n * (n - );
return ans;
} ll ModPow(int x, int p, ll mod) {
ll rec = ;
while(p > ) {
if(p & ) rec = (ll)rec * x % mod;
x = (ll)x * x % mod;
p >>= ;
}
return rec;
} ll slove(int n, ll m) {
if(m == ) return ;
if(n == ) return % m;
if(n == ) return % m;
if(n == ) return % m;
if(n == ) return ( << ) % m;
return (ll)ModPow(n, euler(m), m) * ModPow(n, slove(n - , euler(m)), m) % m;
} int main() {
while(~scanf("%d%d", &n, &m)) {
printf("%lld\n",slove(n, m));
}
return ;
}
有不懂的请私聊我QQ(右侧公告里有QQ号)或在下方回复
Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)的更多相关文章
- XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】
1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 45 Solved: 8[Submit][Status][W ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 数学知识-欧拉函数&快速幂
欧拉函数 定义 对于正整数n,欧拉函数是小于或等于n的正整数中与n互质的数的数目,记作φ(n). 算法思路 既然求解每个数的欧拉函数,都需要知道他的质因子,而不需要个数 因此,我们只需求出他的质因子, ...
- 牛客训练:小a与黄金街道(欧拉函数+快速幂)
题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...
- 小a与黄金街道(欧拉函数+快速幂)
链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...
- 数论的欧拉定理证明 & 欧拉函数公式(转载)
欧拉函数 :欧拉函数是数论中很重要的一个函数,欧拉函数是指:对于一个正整数 n ,小于 n 且和 n 互质的正整数(包括 1)的个数,记作 φ(n) . 完全余数集合:定义小于 n 且和 n 互质的数 ...
- [LightOJ 1370] Bi-shoe and Phi-shoe(欧拉函数快速筛法)
题目链接: https://vjudge.net/problem/LightOJ-1370 题目描述: 给出一些数字,对于每个数字找到一个欧拉函数值大于等于这个数的数,求找到的所有数的最小和. 知识点 ...
- 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)
读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...
- CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...
随机推荐
- iOS开发libz.dylib介绍
libz.dylib这个Xcode系统库文件经常用到.这个其实是个动态链接库. 后缀名为.dylib的文件是一个动态库,这个库是运行时加载而不是编译时加载.这个也说明了obj-C是运行时语言,也就是数 ...
- C#通过SC命令和静态公共类来操作Windows服务
调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...
- 【week4】技术随笔psp
本周psp
- delphi 取得数据集某字段值的六种方法
//取name字段的示例 edit1.Text:=ADOquery1.Fields[2].AsString; //取得数据表的第二个字段的值 edit2.Text:=ADOquery1.Fie ...
- WPF以access为数据库,简单实现一个显示数据和更新数据的实例
做一个小实例,如下图,
- Ip合并
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- Python logging(日志)模块
python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...
- BZOJ4866 Ynoi2017由乃的商场之旅(莫队)
显然能重排为回文串相当于出现次数为奇数的字母不超过一个.考虑莫队,问题在于如何统计添加/删除一位的贡献.将各字母出现次数奇偶性看做二进制数,做一个前缀和一个后缀和.在右端添加一位时,更新区间的前缀.后 ...
- [bzoj] 3673 3674 可持久化并查集 || 可持久化数组
原题 加强版 题意: 可持久化并查集模板-- 题解: 用可持久化线段树维护一个可持久化数组,来记录每一次操作后的状态. 不能用路径压缩,但是要按置合并,使复杂度保证在O(log) #include&l ...
- 【转】OSI详解
本文转自牛客网友CZ❤♡ღQM对在ISO/OSI参考模型中,网络层的主要功能是一题的回答. OSI ( Open System Interconnect ),即开放式系统互联. 一般都叫 OSI 参考 ...