Calculation

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

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
 

题意:求解$f(n) = {(n\%10)}^{f(n/10)}\%m $,其中$(2 <= n, m <= 10^9)$;

思路:一看到指数模除,并且没有说明是否互素,直接上指数循环节,化简指数==套路...

注:

1. 使用指数循环节都是递归形式,所以欧拉函数也需要是递归形式;

2. 在指数循环节中的快速幂时,需要在ans >= mod时,模完之后还要加上mod;

 #include <iostream>
#include<cstdio>
using namespace std;
#define ll long long
ll phi(ll c)
{
ll ans = c;
for(int i = ; i*i <= c; i++) {
if(c%i == ){
ans -= ans/i;
while(c%i == ) c /= i;
}
}
if(c > ) ans -= ans/c;
return ans;
}
ll quick_mod(ll a, ll b, ll mod)
{
if(a >= mod) a = a%mod + mod; // 并不是直接%mod
ll ans = ;
while(b){
if(b&){
ans = ans*a;
if(ans >= mod) ans = ans%mod + mod;  //**
}
a *= a;
if(a >= mod) a = a%mod + mod;  //**
b >>= ;
}
return ans;
}
ll solve(ll n, ll m)
{
ll p = phi(m);
if(n == ) return ;
ll index = solve(n/, p); return quick_mod(n%, index, m);
}
int main()
{
ll n, m, T;
cin >> T;
while(T--){
scanf("%I64d%I64d", &n, &m);
printf("%I64d\n", solve(n,m)%m);
}
return ;
}

hdu 2837 Calculation 指数循环节套路题的更多相关文章

  1. HDU 4335 What is N?(指数循环节)题解

    题意: 询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\) 思路: 这题显然要 ...

  2. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. hdu 3054 Fibonacci 找循环节的公式题

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Proble ...

  4. HDU2837 Calculation(指数循环节)题解

    题意: 已知\(f(0)=1,f(n)=(n\%10)^{f(n/10)}\),求\(f(n)\mod m\) 思路: 由扩展欧拉定理可知:当\(b>=m\)时,\(a^b\equiv a^{b ...

  5. hdu 2837 Calculation

    公式:a^b%p=a^(b%phi(p)+phi(p))%p   b>=phi(p) #include<iostream> #include<stdio.h> #incl ...

  6. 指数循环节 求A的B次方模C

    phi(c)为欧拉函数, 欧拉定理 : 对于互质的正整数 a 和 n ,有 aφ(n)  ≡ 1 mod n  . A^x = A^(x % Phi(C) + Phi(C)) (mod C) (x & ...

  7. 指数循环节&欧拉降幂

    证明:https://www.cnblogs.com/maijing/p/5046628.html 注意使用条件(B的范围) 例题: FZU1759 HDU2837 ZOJ1674 HDU4335

  8. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  9. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

随机推荐

  1. UILabel的简单用法和实际操作

    1.UILabel   **//设置文字 label.text = @"欢迎收看灌篮高手,我是安溪教练";**//设置文字颜色label.textColor = [UIColor  ...

  2. OC之Block的用法和实现委托

    Block的基本概念 Block的基本用法 Block实现委托机制 一.Block的基本概念 Block是程序的代码块,这个代码块可以在需要的时候执行.IOS开发中,block到处可见,所以学好很重要 ...

  3. (转)内网网站发布到外网-nat123动态公网IP动态域名解析

    环境描述: 路由器分配的是动态公网IP,且有路由器登录管理权限,网站服务器部署在路由器内部网络.如何将内网网站发布到外网大众访问? 解决方案: 内网使用nat123动态域名解析,将域名实时固定解析到路 ...

  4. 精通CSS高级Web标准解决方案(1-2 层叠与特殊性)

     层叠与特殊性 选择器的特殊性分成四个等级,a.b.c . d 如果样式是行内样式,那么a=1 b=ID选择器的总数 c=类.伪类.属性选择器的总数 d=标签选择器与伪元素选择器数量 例如:style ...

  5. rpm build error: invalid predicate

    rpm build error error message:/usr/lib/rpm/find-debuginfo.sh /usr/src/redhat/BUILD/RPMS find: invali ...

  6. codeforces 434A A. Ryouko's Memory Note(数学)

    题目链接: A. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input ...

  7. Linux之zsh

    0.安装zsh: sudo apt-get install zsh git wget 1.启用zsh:chsh -s /bin/zsh 2.github: https://github.com/rob ...

  8. c#中文件上传(1)

    * * ;//3M picPath = Server.MapPath("........."); HttpFileCollection postfile = Context.Req ...

  9. C#.NET实现Word或Excel文件转为HTML文件

    Word文件转html,返回相对路径 private string GetPathByDocToHTML(string strFile) { if (string.IsNullOrEmpty(strF ...

  10. Eclipse+GitHub

    之前一直想研究github的使用,但一直没时间,今天抽空学习了一下,发现真的是非常好用!!! 准备材料 1.你要有最新版的Eclipse(不要问我为什么要最新版的,反正我用的是最新版本) 2.一个gi ...