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. .NET控制台程序监听程序退出

    There are mainly 2 types of Win32 applications, console application and window application. They hav ...

  2. Clouder Manager安装时出现please remove the following file: /etc/cloudera-scm-server/db.properties问题解决(图文详解)

    问题详情 bigdata@ubuntucmbigdata1:/opt/softwares/cm-$ sudo ./cloudera-manager-installer.bin This install ...

  3. java语言编程实现两个时间相差多少天、多少小时、多少分、多少秒

    不多说,直接上干货! DateDistance.java package zhouls.bigdata.DataFeatureSelection.test; import java.text.Date ...

  4. 【Linux】debian 7 安装 rz sz lrzsz

    通常linux服务器是通过ssh客户端来进行远程登录和管理的.然而如何方便的实现客户端与linux服务器端的文件交互呢?这就需要用到rz(上传).sz(下载)工具.在Ubuntu 10.10下安装rz ...

  5. mongodb3集群搭建

    三台服务器:先设置hosts 10.0.0.231 node1 10.0.0.232 node2 10.0.0.233 node3 1:下载 mongodb-linux-x86_64-rhel70-3 ...

  6. Prestashop后台模块(中英转译)

    =======================Prestashop-Modules========================= Pretashop运行起来非常慢,这个大家应该都知道,最近要给这个 ...

  7. MSSql中profiler工具介绍,

    第一次使用profiler感觉真的是很方便. 他的作用可能有很多,但是我只是简单其中一点,就是查看某一时刻数据库执行了哪些操作,执行了哪些语句,执行语句所花费的时间.都可以很方便的查到,这对于我们优化 ...

  8. Struts2_结果类型_resulttype_1

    看下面的例子: 一般使用4种:dispatcher(容器内跳转到JSP页面).redirect(重定向到jsp页面).chain(容器内跳转到另一个Action).redirectAction(重定向 ...

  9. 易客CRM-3.0.4 (OpenLogic CentOS 6.5)

    平台: CentOS 类型: 虚拟机镜像 软件包: apache1.3.8 centos6.5 mysql5.1.72 php5.2.17 commercial crm linux 服务优惠价: 按服 ...

  10. vsftpd配置

    yum -y install vsftpd useradd upload -s /sbin/nologin passwd upload mkdir /data/upload chown -R upol ...