博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2837 Calculation(扩展欧拉定理)
阅读量:4557 次
发布时间:2019-06-08

本文共 2005 字,大约阅读时间需要 6 分钟。

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3121    Accepted Submission(s): 778

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
 

 

Source
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:            
 
$a^x \equiv a^{x \  \% \  phi(m) + phi(m)} \pmod m$
然后直接上就行了。
有很多奇怪的边界问题,比如求$f(n)$的时候一模就炸。。
 
#include
#include
#include
#include
#define int long long using namespace std;const int MAXN = 1e5 + 10, INF = 1e9 + 10;inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f;}int N, M, PhiM;int fastpow(int a, int p, int mod) { if(a == 0) return p == 0; int base = 1; while(p) { if(p & 1) base = (base * a) % mod; p >>= 1; a = (a * a) % mod; } return base == 0 ? mod : (base + mod)% mod;}int GetPhi(int x) { int limit = sqrt(x), ans = x; for(int i = 2; i <= limit; i++) { if(!(x % i)) ans = ans / i * (i - 1) ; while(!(x % i)) x /= i; } if(x > 1) ans = ans / x * (x - 1); return ans;}int F(int N, int mod) { if(N < 10) return N; return fastpow((N % 10), F(N / 10, GetPhi(mod)), mod);}main() { int QwQ = read(); while(QwQ--) { N = read(); M = read(); printf("%I64d\n", F(N, M)); } return 0;}/*424 2037 25123456 321654123456789 456789321*/

 

转载于:https://www.cnblogs.com/zwfymqz/p/9279227.html

你可能感兴趣的文章
特殊的夸赞 对女生的夸奖
查看>>
非Markdown - Test
查看>>
1044 火星数字 (20 分)
查看>>
【题解】Luogu P1204 [USACO1.2]挤牛奶Milking Cows
查看>>
挂载云硬盘后的分区、格式化与挂载
查看>>
android:shape的使用
查看>>
Android自定义UI
查看>>
spring boot整合quartz实现多个定时任务
查看>>
db powerdesign CDM、LDM、PDM、OOM的区别
查看>>
ISE和modelsim的配合
查看>>
通过用户模型,对数据库进行增删改查操作。
查看>>
群发邮件功能的完善
查看>>
gradle多项目构建及依赖
查看>>
linux, windows 文件传输的问题
查看>>
php:对象(object)数据类型实例详解
查看>>
关于java环境变量配置Javac命令无效问题
查看>>
常用的正则表达式
查看>>
Spring Boot使用@Async实现异步调用
查看>>
LeetCode 79. 单词搜索(Word Search)
查看>>
MySQL 多列索引优化小记
查看>>