[拓展Bsgs] Clever - Y
题目链接 Clever - Y
题意
有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\)。
解法
如题,是拓展 \(Bsgs\) 板子,部分学习内容在这里 \((Click\ here)\)。
敲完板子就能获得至少 5 倍经验。
过程中疯狂 \(WA\) 所以总结需要注意的几点……
· 令 \(m = sqrt(p) + 1\) 比较保险,不然有的时候会枚举不到
· 在令 \(a\),\(p\) 互质的循环中,\(b = d\) 时及时返回是有必要的
· 同时在以上步骤中,\((a, p)\) 可能恒不等于\(1\),所以也要判
· \(map\) 慢的一批!慢的一批!手写个效率有保证的类似哈希表的东西
类似的题目
[Hdu 2815] Mod Tree
[Poj 2417] Discrete Logging
[CQOI2018] 破解D-H协议
代码……
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long u64;
class Hash_table {
private:
vector<u64> value;
vector<pair<u64, u64> > funcn;
public:
inline void clear() { value.clear(), funcn.clear(); }
inline void sortv() { sort(value.begin(), value.end()); }
inline void push(u64 x, u64 p) { value.push_back(x), funcn.push_back(make_pair(x, p)); }
inline bool find(u64 x) {
int k = lower_bound(value.begin(), value.end(), x) - value.begin();
return (k != value.size()) && (value[k] == x);
}
inline int posi(u64 x) {
for(int i = 0; i < funcn.size(); ++i)
if( funcn[i].first == x ) return funcn[i].second;
}
} lg;
inline u64 Fast_pow(u64 x, u64 p, u64 m) {
u64 ans = 1;
if( p < 0 ) return ans;
for( ; p; x = x * x % m, p = p >> 1) if( p & 1 ) ans = x * ans % m;
return ans;
}
inline u64 Ex_gcd(u64 a, u64 b, u64 &x, u64 &y) {
if( !b ) { x = 1, y = 0; return a; }
u64 d = Ex_gcd(b, a % b, y, x); y = y - a / b * x;
return d;
}
inline u64 Gcd(u64 a, u64 b) { return !b ? a : Gcd(b, a % b); }
inline u64 Inverse(u64 a, u64 p) {
u64 x = 0, y = 0, g = Ex_gcd(a, p, x, y);
return g == 1 ? (x + p) % p : -1ll;
}
inline u64 Solve_fun(u64 a, u64 b, u64 p) {
u64 g = Gcd(a, p), inv = 1, x = 0, y = 0;
if( b % g ) return -1ll;
a = a / g, b = b / g, p = p / g;
inv = Inverse(b, p), a = a * inv % p, b = 1;
Ex_gcd(a, p, x, y), x = (x + p) % p;
return ~inv ? x : -1ll;
}
inline u64 Ex_bsgs(u64 a, u64 b, u64 p) {
u64 m = 1, d = 1, num = 0, base = 1, pow_a = 1, ans = -1;
for(u64 g = Gcd(a, p); g != 1ll; g = Gcd(a, p), ++num) {
if( num > 31 || b % g ) return -1ll;
b = b / g, p = p / g, d = d * (a / g) % p;
if( b == d ) return num + 1;
}
m = sqrt(p) + 1, base = Fast_pow(a, m, p), lg.clear(), lg.push(1ll, 0ll);
for(u64 i = 1; i <= m + num; ++i) pow_a = pow_a * a % p, lg.push(pow_a, i);
lg.sortv();
for(u64 tmp, i = 0; i <= m; ++i) {
tmp = Solve_fun(d % p, b, p), d = d * base % p;
if( ~tmp && lg.find(tmp) ) { ans = i * m + lg.posi(tmp) + num; break; }
}
return ans;
}
int main(int argc, const char *argv[])
{
u64 a = 0, b = 0, p = 0, ans = 0;
while( ~scanf("%lld%lld%lld", &a, &p, &b) ) if( p ) {
ans = Ex_bsgs(a, b, p);
~ans ? printf("%lld\n", ans) : printf("No Solution\n");
}
return 0;
}
—— 我们还会继续与人萍水相逢,为了新的别离。
[拓展Bsgs] Clever - Y的更多相关文章
- 【POJ 3243】Clever Y 拓展BSGS
调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...
- bzoj 1467: Pku3243 clever Y 扩展BSGS
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...
- poj3243 Clever Y[扩展BSGS]
Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8666 Accepted: 2155 Descript ...
- luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法
BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- bzoj1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 313 Solved: 181[Submit][Status ...
- 数学:拓展BSGS
当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...
- 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS
[BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input ...
随机推荐
- 不能收缩 ID 为 %s 的数据库中 ID 为 %s 的文件,因为它正由其他进程收缩或为空。
SQLServer数据库通常都不建议进行SHRINKFILE操作,因为SHRINKFILE不当会造成一定的性能问题. 但是当进行了某些操作(例如某个超大的日志类型表转成分区表切换了数据文件),数据库某 ...
- shell脚本批量推送公钥
目的:新建管理机,为了实现批量管理主机,设置密匙登陆 原理:.通过密钥登陆,可以不用密码 操作过程: 1.生成密匙 ssh-keygen 2.查看密匙 ls ~/.ssh/ 有私匙id_rsa公匙 ...
- Cs231n课堂内容记录-Lecture 9 深度学习模型
Lecture 9 CNN Architectures 参见:https://blog.csdn.net/qq_29176963/article/details/82882080#GoogleNet_ ...
- SQLServer之创建AFETER DELETE触发器
DML AFTER DELETE触发器创建原理 触发器触发时,系统自动在内存中创建deleted表或inserted表,inserted表临时保存了插入或更新后的记录行,deleted表临时保存了删除 ...
- Spring Boot使用注解实现AOP
第一步: 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- springclound
https://blog.csdn.net/w893932747/article/details/80896315 //这个有怎么创建client的 https://blog.csdn.net/m0_ ...
- SQL AUTO INCREMENT 字段
Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...
- 如何在本地搭建DVWA环境
如何在本地搭建DVWA环境 1.工具下载: (1)phpStudy: http://phpstudy.php.cn/download.html (2)DVWA:http://www.dvwa.c ...
- ESP8266远程OTA升级
https://blog.csdn.net/xh870189248/article/details/80095139 https://www.wandianshenme.com/play/arduin ...
- 排序学习实践---ranknet方法
要: 1 背景 随着移动互联网的崛起,越来越多的用户开始习惯于从手机完成吃.喝.玩.乐.衣.食.住.行等各个方面的需求.打开手机,点开手淘.美团等APP,商品玲玲满目,而让用户将所有商品一页 ...