Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3121    Accepted Submission(s):
778

Problem Description
Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10)
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).
 
Input
The first line contains a single positive integer T.
which is the number of test cases. T lines follows.Each case consists of one
line containing two positive integers n and m.
 
Output
One integer indicating the value of f(n)%m.
 
Sample Input
2
24 20
25 20
 
Sample Output
16
5
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar
problems for you:  2841 2839 2838 2840 2836 
 
$a^x \equiv a^{x \  \% \  phi(m) + phi(m)} \pmod m$
然后直接上就行了。
有很多奇怪的边界问题,比如求$f(n)$的时候一模就炸。。
 
#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(扩展欧拉定理)的更多相关文章

  1. [luogu4139]上帝与集合的正确用法【欧拉定理+扩展欧拉定理】

    题目大意 让你求\(2^{2^{2^{\cdots}}}(mod)P\)的值. 前置知识 知识1:无限次幂怎么解决 让我们先来看一道全国数学竞赛的一道水题: 让你求解:\(x^{x^{x^{\cdot ...

  2. BZOJ.3884.上帝与集合的正确用法(扩展欧拉定理)

    \(Description\) 给定p, \(Solution\) 欧拉定理:\(若(a,p)=1\),则\(a^b\equiv a^{b\%\varphi(p)}(mod\ p)\). 扩展欧拉定理 ...

  3. SHOI 2017 相逢是问候(扩展欧拉定理+线段树)

    题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...

  4. bzoj3884: 上帝与集合的正确用法 扩展欧拉定理

    题意:求\(2^{2^{2^{2^{...}}}}\%p\) 题解:可以发现用扩展欧拉定理不需要很多次就能使模数变成1,后面的就不用算了 \(a^b\%c=a^{b\%\phi c} gcd(b,c) ...

  5. 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]

    题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...

  6. 【CodeForces】906 D. Power Tower 扩展欧拉定理

    [题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...

  7. 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]

    题目传送门 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”. ...

  8. [BZOJ4869][六省联考2017]相逢是问候(线段树+扩展欧拉定理)

    4869: [Shoi2017]相逢是问候 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1313  Solved: 471[Submit][Stat ...

  9. CodeForces 907F Power Tower(扩展欧拉定理)

    Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is u ...

随机推荐

  1. 编译opencv python接口

    首先,安装依赖1 sudo apt-get install build-essential 2 sudo apt-get install cmake git libgtk2.0-dev pkg-con ...

  2. empty,isset,is_null比较(差异与异同)

    做php开发时候,想必在使用:empty,isset,is_null  这几个函数时候,遇到一些问题.甚至给自己的程序带来一些安全隐患的bug.很多时候,对于isset,empty都认为差不多.因此开 ...

  3. 基于JQUERY 的图片查看插件

    viewer是一款功能强大的图片查看器.它可以实现ACDsee等看图软件的部分功能.它可以对图片进行移动,缩放,旋转,翻转,可以前后浏览一组图片.该图片查看器还支持移动设备,支持键盘控制,功能十分强大 ...

  4. AFNetworking 3.0修改指南

    AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于NSURLConnectionAPI的所有支持.如果 ...

  5. mybatis批量插入insert时报错

    报错信息: 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确.此 RPC 请求中提供了过多的参数.最多应为2100 错误分析: 由于mybatis拼接的sql语句参数过多导致 解决办法 ...

  6. (一)Hybrid app混合开发模式

    hybrid app是什么? 这里我们先看一下词条上的定义 Hybrid App:Hybrid App is a mobile application that is coded in both br ...

  7. 基于Python3 神经网络的实现

    基于Python3 神经网络的实现(下载源码) 本次学习是Denny Britz(作者)的Python2神经网络项目修改为基于Python3实现的神经网络(本篇博文代码完整).重在理解原理和实现方法, ...

  8. VirtualBox中linux虚拟机和主机间的共享文件设置

    设置共享文件路径 点击虚拟机 设置-->选择 共享文件夹 (图1 设置共享文件夹) 设置共享文件夹路径 1 选择路径 2 填写自定义的共享名称(在后面需要与挂载路径相对应) 3 设置自动挂载/固 ...

  9. selnium截屏操作

    这个算是难找的.C# ITakeScreenShot 接口来实现截图.不是ScreenShot这个坑弄了很长时间啊. var folderLocation = Environment.CurrentD ...

  10. sharepoint知识点总结

    { users.Add(value.User); } else { SPGroup group = web.Groups.GetByID(value.LookupId); groups.Add(gro ...