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 ...
随机推荐
- WSGI学习系列WSME
Introduction Web Services Made Easy (WSME) simplifies the writing of REST web services by providing ...
- [Android]对字符串进行MD5加密
/** * 将字符串转成MD5值 * * @param string * @return */ public static String stringToMD5(String string) { by ...
- java多线程之原子变量
看链接博客:http://blog.csdn.net/u011116672/article/details/51068828
- php 关于时间的函数
//返回1970年1月1日零点以来的秒数. //定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. time(); ...
- 用户“Michael-PC\Michael”不具有所需的权限。请验证授予了足够的权限并且解决了 Windows 用户帐户控制(UAC)限制问题。
解决方法:在注册表增加键值 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System] 新建-选择“D ...
- ab (ApacheBench)命令
ab (ApacheBench)命令 参数 -n 在测试会话中所执行的请求个数.默认时,仅执行一个请求 -c 一次产生的请求个数.默认是一次一个 -t 测试所进行的最大秒数 -k 启用HTTP Kee ...
- JSP中的<%%>,<%! %>,<%= %>,<%-- --%>
<% %> 添加java代码 <%! %> 添加java方法 <%= %> 将变量或表达式输出到页面 <%-- - ...
- webpack-dev-server.js 服务器配置说明
connect-history-api-fallback 使用: var app = express() var histroy = require('connect-history-api-fall ...
- 转:Oracle优化总结
本文主要从大型数据库ORACLE环境四个不同级别的调整分析入手,分析ORACLE的系统结构和工作机理,从九个不同方面较全面地总结了 ORACLE数据库的优化调整方案. 关键词 ORACLE数据库 环境 ...
- Conferences
中国学术会议在线 :http://www.meeting.edu.cn 中国会议网: http://www.chinameeting.com 香山科学会议:http://www.xssc.ac.cn/ ...