[BZOJ 2480] [SPOJ 3105] Mod
Description
已知数 \(a,p,b\),求满足 \(a^x\equiv b\pmod p\) 的最小自然数 \(x\)。
Input
每个测试文件中最多包含 \(100\) 组测试数据。
每组数据中,每行包含 \(3\) 个正整数 \(a,p,b\)。
当 \(a=p=b=0\) 时,表示测试数据读入完全。
Output
对于每组数据,输出一行。
如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。
Sample Input
5 58 33
2 4 3
0 0 0
Sample Output
9
No Solution
HINT
\(a,p,b≤10^9\)
Solution
当 \(\gcd(a,q)\not\mid b\) 且 \(b\ne 1\) 时无解。
a^{x-1}\frac{a}{(a,p)}\equiv \frac{b}{(a,p)}\pmod{\frac{p}{(a,p)}}
\]
注意这里不能写成 \(a^{x-1}\equiv b\times a^{-1}\pmod{\frac{p}{(a,p)}}\),因为此时 \(a\not\perp p\),没有逆元。
递归求解,直到 \(a\perp p\),此时式子的形式是
\]
从 \(0\dots m\) 枚举 \(j\),将 \(a^jb\) 的值存入 \(hash\) 表中,然后从 \(1\dots m\) 枚举 \(i\),若表中存在 \(k\times (a^m)^i\),则当前 \(i\times m-j\) 就是答案。
Code
#include <cmath>
#include <cstdio>
#include <tr1/unordered_map>
std::tr1::unordered_map<int,int> hash;
int read() {
int x = 0; char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
void exbsgs(int a, int b, int p) {
if (p == 1 || b == 1) { puts("0"); return; }
int t = gcd(a, p), k = 0, c = 1;
while (t > 1) {
if (b % t) { puts("No Solution"); return; }
b /= t, p /= t, c = 1LL * c * a / t % p, t = gcd(a, p), ++k;
if (b == c) { printf("%d\n", k); return; }
}
int m = ceil(sqrt(p)); hash.clear(), hash[b] = 0;
for (int i = 1; i <= m; ++i) t = 1LL * t * a % p, hash[1LL * t * b % p] = i;
a = t, t = 1LL * t * c % p;
for (int i = 1; i <= m; ++i, t = 1LL * t * a % p)
if (hash.count(t)) { printf("%d\n", i * m - hash[t] + k); return; }
puts("No Solution");
}
int main() {
int a = read(), p = read(), b = read();
while (a) exbsgs(a % p, b % p, p), a = read(), p = read(), b = read();
return 0;
}
[BZOJ 2480] [SPOJ 3105] Mod的更多相关文章
- 【BZOJ】【2480】【SPOJ 3105】Mod
扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...
- 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster
目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...
- 「SPOJ 3105」Power Modulo Inverted
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...
- BZOJ 2480 && 3239 && 2995 高次不定方程(高次同余方程)
链接 BZOJ 2480 虽然是个三倍经验题(2333),但是只有上面这道(BZOJ2480)有 p = 1 的加强数据,推荐大家做这道. 题解 这是一道BSGS(Baby Step Giant St ...
- BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...
- BZOJ.2616.SPOJ PERIODNI(笛卡尔树 树形DP)
BZOJ SPOJ 直观的想法是构建笛卡尔树(每次取最小值位置划分到两边),在树上DP,这样两个儿子的子树是互不影响的. 令\(f[i][j]\)表示第\(i\)个节点,放了\(j\)个车的方案数. ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
随机推荐
- Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM 2016 Performance and Scalability Documentation
摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复285或者20181126可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...
- HA总结:AWS 网络连接
单数据中心 网络HA总结 参考:https://d1.awsstatic-china.com/aws-answers/AWS_Single_Data_Center_HA_Network_Connect ...
- iOS ----------各种判断
iOS 判断数字 - (BOOL) deptNumInputShouldNumber:(NSString *)str { if (str.length == 0) { return NO; } NSS ...
- iOS----------输入框UITextField禁止输入空格
方法一:添加代理 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range rep ...
- JAVA forname classnotfoundexception 错误
今日在使用Class.forName方法的时候报了错误: JAVA forname classnotfoundexception 原因是Class.forName(className);里面的clas ...
- Android Studio遇到Failed to sync Gradle project错误时的解决办法
一 报错显示 Gradle sync failed: Unknown host 'd29vzk4ow07wi7.cloudfront.net'. You may need to adjust th ...
- C# List集合去重使用lambda表达式
name age sex Lucy 22 woman Lily 23 woman Tom 24 man Lucy 22 woman Lily 23 woman LiLei 25 man List< ...
- Oracle导入、导出数据库dmp文件
版本 1.实例数据完全导出 即导出指定实例下的所有数据 exp username/password@192.168.234.73/orcl file=d:/daochu/test.dmp full=y ...
- LeetCode算法题-Island Perimeter(Java实现)
这是悦乐书的第238次更新,第251篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第105题(顺位题号是463).您将获得一个二维整数网格形式的地图,其中1代表土地,0代 ...
- Python爬虫 selenium
库的安装 pip3 install selenium 声明浏览器对象 from selenium import webdriver browser = webdriver.Chrome() brows ...