题意:

询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\)

思路:

这题显然要用欧拉降幂,\(n!\)小于\(\varphi(p)\)的直接暴力算,\(n!\neq 0\mod \varphi(p)\)也直接暴力。

\(n!\equiv 0\mod \varphi(p)\)显然这时质数恒为\(\varphi(p)\),由鸽笼定理得:

当\(x\)是常数时,\(1^x,2^x,\dots,n^x,\dots\mod p\)有循环节为\(\varphi(p)\)

那么直接按循环节搞一下即可。

注意一下,当\(b=0,M=2^{64}-1,p=1\)时,答案爆\(long long\)。

代码:

#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 1e5 + 5;
const int MAXM = 3e6;
const ll MOD = 998244353;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
ull euler(ull n){
ull res = n, a = n;
for(int i = 2; i * i <= a; i++){
if(a % i == 0){
res = res / i * (i - 1);
while(a % i == 0) a/= i;
}
}
if(a > 1) res = res / a * (a - 1);
return res;
}
ull ppow(ull a, ull b, ull mod){
ull ret = 1;
while(b){
if(b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
ull rec[maxn];
int main(){
int T, ca = 1;
scanf("%d", &T);
while(T--){
ull b, p, m;
scanf("%I64u%I64u%I64u", &b, &p, &m);
if(b == 0 && p == 1){
if(m == 18446744073709551615ULL)
printf("Case #%d: 18446744073709551616\n", ca++);
else
printf("Case #%d: %I64u\n", ca++, m + 1);
continue;
}
ull phi = euler(p);
ull ans = 0, fac = 1;
ull i = 1;
if(b == 0) ans++;
for(i = 1; i <= m; i++){
if(fac * i >= phi) break;
fac = fac * i;
if(ppow(i, fac, p) == b) ans++;
}
for(; i <= m; i++){
if(fac * i % phi == 0) break;
fac = fac * i % phi;
if(ppow(i, fac + phi, p) == b) ans++;
}
if(i <= m){
ull cnt = 0;
for(int j = 1; j <= p; j++){
rec[j] = ppow(j, phi, p);
if(rec[j] == b) cnt++;
}
for(; i <= p && i <= m; i++){
if(rec[i] == b) ans++;
}
if(i <= m){
ull rest = m - p;
ans += rest / p * cnt;
rest -= rest / p * p;
for(i = 1; i <= rest; i++){
if(rec[i] == b) ans++;
}
}
}
printf("Case #%d: %I64u\n", ca++, ans);
}
return 0;
}

HDU 4335 What is N?(指数循环节)题解的更多相关文章

  1. hdu 2837 Calculation 指数循环节套路题

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

  2. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  3. HDU 2814 斐波那契循环节 欧拉降幂

    一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...

  4. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  5. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  6. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. 指数循环节 求A的B次方模C

    phi(c)为欧拉函数, 欧拉定理 : 对于互质的正整数 a 和 n ,有 aφ(n)  ≡ 1 mod n  . A^x = A^(x % Phi(C) + Phi(C)) (mod C) (x & ...

  8. 指数循环节&欧拉降幂

    证明:https://www.cnblogs.com/maijing/p/5046628.html 注意使用条件(B的范围) 例题: FZU1759 HDU2837 ZOJ1674 HDU4335

  9. HDU2837 Calculation(指数循环节)题解

    题意: 已知\(f(0)=1,f(n)=(n\%10)^{f(n/10)}\),求\(f(n)\mod m\) 思路: 由扩展欧拉定理可知:当\(b>=m\)时,\(a^b\equiv a^{b ...

随机推荐

  1. Spring Bean详解

    Spring Bean 在Spring的应用中,Spring IoC容器可以创建.装配和配置应用组件对象,这里的组件对象称为Bean. Bean的配置 Spring可以看作一个大型工厂,用于生产和管理 ...

  2. 在.NET Core 中使用Quartz.NET

    Quartz.NET是功能齐全的开源作业调度系统,可用于最小的应用程序到大型企业系统. Quartz.NET具有三个主要概念: job:运行的后台任务 trigger:控制后台任务运行的触发器. sc ...

  3. mysqlG基于TID模式同步报错 (Last_IO_Errno: 1236)

    mysqlG基于TID模式同步报错Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading da ...

  4. Obligations for calling close() on the iterable returned by a WSGI application

    Graham Dumpleton: Obligations for calling close() on the iterable returned by a WSGI application. ht ...

  5. https://learnku.com/docs/go-blog/qihoo/6532 。 heap size went up to 69G, with maximum garbage collection (GC)

    https://learnku.com/docs/go-blog/qihoo/6532 Use a Task Pool, a mechanism with a group of long-lived ...

  6. (转载)微软数据挖掘算法:Microsoft 关联规则分析算法(7)

    前言 本篇继续我们的微软挖掘算法系列总结,前几篇我们分别介绍了:微软数据挖掘算法:Microsoft 决策树分析算法(1).微软数据挖掘算法:Microsoft 聚类分析算法(2).微软数据挖掘算法: ...

  7. LOJ10102旅游航道

    题目描述 SGOI 旅游局在 SG-III 星团开设了旅游业务,每天有数以万计的地球人来这里观光,包括联合国秘书长,各国总统和 SGOI 总局局长等.旅游线路四通八达,每天都有众多的载客太空飞船在星团 ...

  8. QTREE----树剖

    题目内容: ---------------------------------------------------- Query on a tree Time Limit: 851MS   Memor ...

  9. python 基础学习3 列表和元组 、字符串

    作为小白,坚持每日写学习记录,是督促坚持学习的动力, 今天主要是学习 列表和元组,列表是可以修改的,元组是不可变的.列表和元组的索引都是从0开始 列表可以修改, 可以对列表进行赋值,修改移除等各种方法 ...

  10. HaspMap源码分析(JDK 1.8)

    底层结构分析 上面这两张图分别画出了JDK 1.7.1.8底层数据结构,在JDK 1.7.1.8中都使用 了散列算法,但是在JDK 1.8中引入了红黑树,在链表的长度大于等于8并且hash桶的长度大于 ...