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. 导出csv文件时韩文乱码解决方法

    从asp.net导出csv这样配置可以防止韩文等乱码,在头部加上0xEF, 0xBB, 0xBF: string fileName = "attachment;filename=" ...

  2. css实现高度垂直居中

    1:单行文字垂直居中: 如果一个容器中只有一行文字的话,定义height(高度)和 line-height(行高)相等即可. 如:<div style="height:25px;lin ...

  3. java调用7zip解压压缩包

    前言 最近的项目中需要用到解压缩包的功能,客户给出的压缩包的格式主要是rar和zip,因此就打算使用java调用7zip的命令行进行解压文件,本文主要记录一下实现的过程以及其中遇到的问题. 7zip命 ...

  4. poj 1987 节点距离小于等于K(树DP)

    这题和poj 1741是一模一样的 但是1741能AC的代码,在这里却是TLE,暂时没看出哪里出现了问题.. AC代码: #include <iostream> #include < ...

  5. maven课程 项目管理利器-maven 3-8 maven依赖传递 4星

    本节主要讲了 1 maven依赖传递 本地项目路径:F:\xiangmu3\Xin\FuQiang\maven\code 2 maven排除依赖 3 注意事项 4 零散知识点 1 maven依赖传递 ...

  6. vue-devtools插件安装

    1.git clone https://github.com/vuejs/vue-devtools.git 2.此时表示vue-devtools包下载完成,进入到对应的目录下cd vue-devtoo ...

  7. layui文档阅读进度

    2017年1月9日22:13:28已看完layer弹出层部分 2017年1月9日19:15:55http://www.layui.com/doc/modules/layer.html#layer.lo ...

  8. PyYAML使用

    install yum -y install PyYAML document http://www.showyounger.com/show/101586.html http://pyyaml.org ...

  9. jQuery + Stimulsoft.Report 选择gridview多行打印

    jQuery + Stimulsoft.Report 报表空间打印多个ID split函数,多行ID 1. 获取选择多行ID(前台) //按钮-打印 function PrinterWorkQuali ...

  10. VOS3000设置落地网关优先级

    问题描述: 现在有两种套餐卡A:无限通话B:每月3000分钟两个落地网关:GW100:32线用的A套餐GW101:32线用的B套餐 比如现在是12月5号还没到月底,突然发现GW101上所剩可用通话时间 ...