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. android网络请求库volley方法详解

    使用volley进行网络请求:需先将volley包导入androidstudio中 File下的Project Structrue,点加号导包 volley网络请求步骤: 1. 创建请求队列     ...

  2. Android Chronometer的正常暂停和继续

    最近做了个小实验--一个小的计数秒表,实现开始计时,暂停计时,重置的功能.界面如下

  3. git 删除已经 add 的文件

    使用 git rm 命令即可,有两种选择, 一种是 git rm --cached "文件路径",不删除物理文件,仅将该文件从缓存中删除: 一种是 git rm --f " ...

  4. linux_jvm_jhat_dump内存分析

    jhat命令   jhat命令 -- Java Head Analyse Tool 用途:是用来分析java堆的命令,可以将堆中的对象以html的形式显示出来,包括对象的数量,大小等等,并支持对象查询 ...

  5. css+div网页设计(二)--布局与定位

    在网页设计中,能否控制好各个模块中在页面中的位置是非常关键的,与传统的表格定位不同,css+div定位方式更加的灵活,本篇博客将为大家介绍css+div的布局与定位. 一.盒子模型 由图可以看出 盒子 ...

  6. SQL Server表的数据量大小查询

    今天想在服务器上还原一个DB,发现磁盘空间不够,查看发现,其中一个DB竟然有56G了.因此想收缩一下这个DB,发现大小没多大变化.然后在网上找了找SQL脚本,看能不能查看下哪个表的数据量那么大. 网上 ...

  7. KDE声音服务器 arts

    KDE声音服务器 arts arts介绍arts是KDE的核心声音系统,支持多音频流.全双工.网络声音请求.ALSA与OSS驱动后端.JACK声音服务器后端等扩展,它既是声音服务器,也 提供一套音频软 ...

  8. oralce之存储过程

    一:--循环向表emp中插入数据 declare maxnumber ; v_count number; begin v_count :; ..maxnumber LOOP v_count :; in ...

  9. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  10. 剑指Offer12 数组奇数调整至偶数前

    /************************************************************************* > File Name: 12_Reorde ...