hdu2837 Calculation a^b%p=a^(b%phi(p)+phi(p))%p
来源:互联网 发布:算法注册机apk 编辑:程序博客网 时间:2024/05/08 02:45
Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 873 Accepted Submission(s): 192
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
224 2025 20
Sample Output
165
Source
2009 Multi-University Training Contest 3 - Host by WHU
Recommend
gaojie
a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
#include<iostream>#include<cstdlib>#include<stdio.h>#define ll __int64using namespace std;ll powermod(ll a,ll b,ll p){ ll res=1; while(b) { if(b&1)res=res*a%p; b>>=1; a=a*a%p; } return res;}ll get_phi(int m){ ll res=1; for(int i=2;i*i<=m;i++) { if(m%i==0) { m/=i; res*=(i-1); while(m%i==0){m/=i;res*=i;} } } if(m!=1) res*=(m-1); return res;}ll check(int a,int b,int p){ ll res=1; for(int i=1;i<=b;i++) { res*=a; if(res>=p) return res; } return res;}
ll dfs(int n,int m){ ll phi=get_phi(m); if(n<10) return n; ll x=dfs(n/10,phi); ll yy=check(n%10,x,m); if(yy>=m) { ll res=powermod(n%10,x+phi,m); if(res==0) res+=m; return res; } else return yy;}
int main(){ int n,m,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); printf("%I64d\n",dfs(n,m)%m); } return 0;}
- hdu2837 Calculation a^b%p=a^(b%phi(p)+phi(p))%p
- hdu3221 Brute-force Algorithm矩阵快速幂&&a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
- CH BR13数学(啥?-a^b≡a^b mod phi(p)+phi(p)(mod p)(b>=phi(p))公式)
- p(a|b)=p(ab)/p(b)为什么?
- 1063 A^B%P
- XDOJ1265 - A^B % P
- A *p=new B;
- 为什么phi(p^n)=p^n-p^(n-1)
- P(AB)与P(B|A)区别!!!!!!
- 条件概率 -- P(A|B) P(B|A)
- BSGS(a^x%p=b)
- 第四周 项目一 改错 s(面积)=sqrt(p*(p-a)*(p-b)*(p-c))
- class A 和 class B: public A中A a; B b; A *p = &a; p->foo();相关题目
- a^b === c (mod p)知二求一: p已知
- 为什么A *a=new B不直接写成B b,或者B *p呢?
- 解一类x^a = b (mod p)的方程
- int *p=&a
- *p=&a
- [转帖]fstream的使用方法介绍
- verilog部分语法
- J2SE基础夯实系列之抽象类和接口的区别(太透彻了!)
- uevent分析
- iOS开发如何实现消息推送机制
- hdu2837 Calculation a^b%p=a^(b%phi(p)+phi(p))%p
- Calendar,本月中最小或最大的一天
- argc and argv
- Android 4.0 Notification
- 把数组排成最小的数--总结
- 【备忘录】学术会议&论文专辑
- 编译directshow filter的vc6的配置
- Android异步通信机制(四)
- Myeclipse常见错误1