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).

InputThe 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.OutputOne integer indicating the value of f(n)%m.Sample Input

2
24 20
25 20

Sample Output

16
5 指数循环节题目就是欧拉函数加快速幂。
根据欧拉定理 
A^x mod C =A^(x mod phi(C)+ phi(C)) (x>=C)
#include <iostream>
#include<cstdio>
using namespace std;
#define ll long long
ll phi(ll c)
{
ll ans = c;
for(int i = 2; i*i <= c; i++) {
if(c%i == 0){
ans -= ans/i;
while(c%i == 0) c /= i;
}
}
if(c > 1) ans -= ans/c;
return ans;
}
ll quick_mod(ll a, ll b, ll mod)
{
if(a >= mod) a = a%mod + mod; // 并不是直接%mod
ll ans = 1;
while(b){
if(b&1){
ans = ans*a;
if(ans >= mod) ans = ans%mod + mod;//**
}
a *= a;
if(a >= mod) a = a%mod + mod;//**
b >>= 1;
}
return ans;
}
ll solve(ll n, ll m)
{
ll p = phi(m);
if(n == 0) return 1;
ll index = solve(n/10, p); return quick_mod(n%10, 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 0;
}

  

hdu_2837_Calculation(欧拉函数,快速幂求指数循环节)的更多相关文章

  1. XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...

  2. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  3. 数学知识-欧拉函数&快速幂

    欧拉函数 定义 对于正整数n,欧拉函数是小于或等于n的正整数中与n互质的数的数目,记作φ(n). 算法思路 既然求解每个数的欧拉函数,都需要知道他的质因子,而不需要个数 因此,我们只需求出他的质因子, ...

  4. 牛客训练:小a与黄金街道(欧拉函数+快速幂)

    题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...

  5. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  6. 小a与黄金街道(欧拉函数+快速幂)

    链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...

  7. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

  8. 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)

    读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...

  9. (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

    题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. Windows 10家庭版升级到专业版,系统蓝屏

    Log Name: SystemSource: Microsoft-Windows-DistributedCOMDate: 9/9/2018 7:56:57 PMEvent ID: 10016Task ...

  2. 导出csv文件时韩文乱码解决方法

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

  3. script脚本中写不写$(document).ready(function() {});的区别

    $(document).ready() 里的代码是在页面内容都加载完才执行的,如果把代码直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,此时如果你标签里执行的代码调用 ...

  4. 高精度运算——java

    java大法 java的框架. import java.io.*; import java.util.*; import java.math.*; public class Main{ public ...

  5. collides with another import statement解决办法

    如我要导入的两个包名为: import com.tesla.gateway.core.filter.Filter import ch.qos.logbak.core.filter.Filter 这样就 ...

  6. 拿到返回值,Callable示例

  7. vue-2.4.0-添加的新东东

    组件内新增实现属性继承 VUE中一个比较令人烦恼的事情是属性只能从父组件传递给子组件.这也就意味着当你想向嵌套层级比较深组件数据传递,只能由父组件传递给子组件,子组件再传递给孙子组件...像下面这样 ...

  8. 转:Windows任务计划实现自动执行ArcGIS相关功能

    今天一不小心点开了Windows任务计划,以前咩有怎么用过,发现还挺好用,于是想到了以前用户的一些问题 1:用户环境使用ArcSDE服务连接,每次运行到一定的负载量(可能是几天),就会很慢,用户就喜欢 ...

  9. Linux 下, 安装Android Studio

    Download the Android Package of Linux from Android Studio, android-studio-bundle-130.737825-linux.tg ...

  10. SourceTree Win10 安装过程及配置

    SourceTree 是一款拥有可视化界面的项目版本控制软件,适用于git项目管理,同时它集成了 git flow 工作流程,对于不熟悉 git 命令的初学者来说,可以通过 SourceTree 快速 ...