乍一看题面:ax≡b (mod m)
是一道BSGS,但是很可惜m不是质数,而且(m,a)≠1,这个叫扩展BSGS【额......
于是我们需要通过变换使得(m,a)=1
首先令g=(a,m),则原式等价于:ax+k∗m=b,k∈Z
移项可得:ag∗ax−1+k∗mg=bg
此时如果b≢则无解
令m' = \frac {m} {g}, b' = \frac {b} {g} * (\frac{a} {g}) ^ {-1}
于是得到新式:a ^ {x - 1} = b' (mod\ m')
于是可以一直迭代到(m, a) = 1,然后用BSGS来计算答案即可

1 /************************************************************** 2 Problem: 2480 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:3256 ms 7 Memory:1568 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 #include <ext/pb_ds/assoc_container.hpp> 13 #include <ext/pb_ds/hash_policy.hpp> 14 15 using namespace std; 16 using namespace std; 17 typedef long long ll; 18 typedef __gnu_pbds::cc_hash_table <int, int> hash; 19 20 inline int read(); 21 22 int a, b, m, ans; 23 hash h; 24 25 inline int pow(ll x, ll y, ll mod) { 26 static ll res; 27 res = 1; 28 while (y) { 29 if (y & 1) res = res * x % mod; 30 x = x * x % mod, y >>= 1; 31 } 32 return (int) res; 33 } 34 35 inline int BSGS(int a, int b, int p, ll now) { 36 static int m, i; 37 static ll base; 38 m = (int) ceil(sqrt(p)), base = b; 39 h.clear(); 40 for (i = 0; i < m; ++i) 41 h[base] = i, base = base * a % p; 42 43 base = pow(a, m, p); 44 for (i = 1; i <= m + 1; ++i) { 45 now = now * base % p; 46 if (h.find(now) != h.end()) return i * m - h[now]; 47 } 48 return -1; 49 } 50 51 int extend_BSGS(int a, int b, int m) { 52 static int cnt, g, res; 53 static ll t; 54 a %= m, b %= m; 55 if (b == 1) return 0; 56 cnt = 0, g = __gcd(a, m), t = 1; 57 while (g != 1) { 58 if (b % g) return -1; 59 m /= g, b /= g, t = t * a / g % m; 60 ++cnt; 61 if (b == t) return cnt; 62 g = __gcd(a, m); 63 } 64 res = BSGS(a, b, m, t); 65 return ~res ? res + cnt : res; 66 } 67 68 int main() { 69 while (1) { 70 a = read(), m = read(), b = read(); 71 if (!a && !m && !b) return 0; 72 ans = extend_BSGS(a, b, m); 73 if (!~ans) puts("No Solution"); 74 else printf("%d\n", ans); 75 } 76 return 0; 77 } 78 79 inline int read() { 80 static int x; 81 static char ch; 82 x = 0, ch = getchar(); 83 while (ch < '0' || '9' < ch) 84 ch = getchar(); 85 while ('0' <= ch && ch <= '9') { 86 x = x * 10 + ch - '0'; 87 ch = getchar(); 88 } 89 return x; 90 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理