HDU2837 Calculation(扩展欧拉定理)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3121 Accepted Submission(s):
778
for all n bigger than zero. Please calculate f(n)%m. (2 ≤ n , m ≤ 10^9, x^y
means the y th power of x).
which is the number of test cases. T lines follows.Each case consists of one
line containing two positive integers n and m.
24 20
25 20
5
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define int long long
using namespace std;
const int MAXN = 1e5 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M, PhiM;
int fastpow(int a, int p, int mod) {
if(a == ) return p == ;
int base = ;
while(p) {
if(p & ) base = (base * a) % mod;
p >>= ; a = (a * a) % mod;
}
return base == ? mod : (base + mod)% mod;
}
int GetPhi(int x) {
int limit = sqrt(x), ans = x;
for(int i = ; i <= limit; i++) {
if(!(x % i)) ans = ans / i * (i - ) ;
while(!(x % i)) x /= i;
}
if(x > ) ans = ans / x * (x - );
return ans;
}
int F(int N, int mod) {
if(N < ) return N;
return fastpow((N % ), F(N / , GetPhi(mod)), mod);
}
main() {
int QwQ = read();
while(QwQ--) {
N = read(); M = read();
printf("%I64d\n", F(N, M));
}
return ;
}
/*
4
24 20
37 25
123456 321654
123456789 456789321
*/
HDU2837 Calculation(扩展欧拉定理)的更多相关文章
- [luogu4139]上帝与集合的正确用法【欧拉定理+扩展欧拉定理】
题目大意 让你求\(2^{2^{2^{\cdots}}}(mod)P\)的值. 前置知识 知识1:无限次幂怎么解决 让我们先来看一道全国数学竞赛的一道水题: 让你求解:\(x^{x^{x^{\cdot ...
- BZOJ.3884.上帝与集合的正确用法(扩展欧拉定理)
\(Description\) 给定p, \(Solution\) 欧拉定理:\(若(a,p)=1\),则\(a^b\equiv a^{b\%\varphi(p)}(mod\ p)\). 扩展欧拉定理 ...
- SHOI 2017 相逢是问候(扩展欧拉定理+线段树)
题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...
- bzoj3884: 上帝与集合的正确用法 扩展欧拉定理
题意:求\(2^{2^{2^{2^{...}}}}\%p\) 题解:可以发现用扩展欧拉定理不需要很多次就能使模数变成1,后面的就不用算了 \(a^b\%c=a^{b\%\phi c} gcd(b,c) ...
- 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...
- 【CodeForces】906 D. Power Tower 扩展欧拉定理
[题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...
- 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]
题目传送门 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”. ...
- [BZOJ4869][六省联考2017]相逢是问候(线段树+扩展欧拉定理)
4869: [Shoi2017]相逢是问候 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 1313 Solved: 471[Submit][Stat ...
- CodeForces 907F Power Tower(扩展欧拉定理)
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is u ...
随机推荐
- (转)stty 命令说明及使用讲解
stty 命令说明及使用讲解 UNIX系统的命令很多,但是巧妙使用命令的方法更多.随着经验的积累和观察学习其他用户的实践,我们也可学会解决特殊问题的方法.这里谈谈自己使用UNIX系统中stty ...
- PhpStorm快捷键设置,个性化设置,多项目共存,更改样式主题字体
自定义常用快捷键 按照路径:File -> Settings -> Appearance & Behavior -> Keymap -> Copy 一份 Eclipse ...
- 添加、修改、删除XML节点代码例子
version="1.0" encoding="gb2312"?> . <bookstore> . <book genre=" ...
- SSRS 参数 单选 多选
前段时间 公司要求报表的选项可以多选. 知道需求后,研究了下实现. 首先我们创建一个报表,然后添加3个数据集,2个参数,如下图. DataSet1数据集:存放主数据. ddl_emplid数据集:存放 ...
- 实现Callable的对象中,用@Autowired注入别的对象失败
场景是这样: 我需要在一个实现类A中写一个拿到返回值的多线程,于是用的Callable,在这个实现类A外我又写了一个专门实现Callable的实现类B,在B中用spring注解@Autowired注入 ...
- Java基础入门 - Hello world
JDK安装完成后,将一下代码写入文件HelloWorld.java中 // HelloWorld.java public class HelloWorld { public static void m ...
- 函数的返回值 return
布尔类型返回 return 0:返回假: return 1:返回真:
- WiFi调试手机
转自http://blog.csdn.net/Yejianyun1/article/details/55511726 使用场景: 1.多设备执行测试用例 2.数据线无法满足使用 电脑与手机的网络需要 ...
- (原创)北美信用卡(Credit Card)个人使用心得与总结(个人理财版) [精华]
http://forum.chasedream.com/thread-766972-1-1.html 本人2010年 8月F1 二度来美,现在credit score 在724-728之间浮动,最高的 ...
- CentOS7.3下关于DHCP中继代理服务器的详细配置
DHCP服务器只作用于局域网同一网段内,客户端是通过广播消息来获得DHCP服务器响应后才能得到IP地址的,但广播消息不能跨越子网,那么如何让客户端获取到DHCP服务器提供的IP地址呢?这就是DHCP中 ...