题目链接 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的更多相关文章

  1. 【POJ 3243】Clever Y 拓展BSGS

    调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...

  2. bzoj 1467: Pku3243 clever Y 扩展BSGS

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...

  3. poj3243 Clever Y[扩展BSGS]

    Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8666   Accepted: 2155 Descript ...

  4. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

  5. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  6. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  7. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

  8. 数学:拓展BSGS

    当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...

  9. 【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      ...

随机推荐

  1. [20190401]隐含参数_mutex_spin_count.txt

    [20190401]隐含参数_mutex_spin_count.txt --//上午做了一些测试关于semtimedop函数调用,发现自己上个星期在一些问题上理解错误.--//相关链接:--//htt ...

  2. iOS 常用三方(持续更新)

    iOS 常用三方 1.ZWMSegmentController 分页控制器 https://github.com/weiming4219/ZWMSegmentController

  3. Redis操作集合,有序集合

    Set操作,Set集合就是不允许重复的列表 sadd(name,values) 1 # name对应的集合中添加元素 scard(name) 1 获取name对应的集合中元素个数 sdiff(keys ...

  4. springboot项目

    https://my.oschina.net/ityouknow/blog/1629066

  5. vs 2015安装包

    Visual Studio 2015 下载含(更新3)及密钥 Visual Studio 2015 是一个丰富的集成开发环境,可用于创建出色的 Windows.Android 和 iOS 应用程序以及 ...

  6. vue2.0阻止事件冒泡

    <!--picker弹窗--><transition name="fade"> <div class="picker_wrap" ...

  7. linux下 启动node 和关闭node

    1.用forever  进行管理 npm install -g forever forever start app.js //启动 forever stop app.js //关闭 2.用自带的服务n ...

  8. Spring Security(三十三):10.3 Password Encoding

    Spring Security’s PasswordEncoder interface is used to support the use of passwords which are encode ...

  9. TensorRT&Sample&Python[network_api_pytorch_mnist]

    本文是基于TensorRT 5.0.2基础上,关于其内部的network_api_pytorch_mnist例子的分析和介绍. 本例子直接基于pytorch进行训练,然后直接导出权重值为字典,此时并未 ...

  10. SoapUI 学习总结-01 环境配置

    遇到的问题 1,怎么SoapUI的Request URL不支持大写怎么办? 问题:在SoapUI的Request URL中,每次输入的URL中含有的大写字母会自动转换为小写字母,导致请求不了对应的地址 ...