题目链接 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. C#中Skip和Take的用法

    Skip()和Take()方法都是IEnumerable<T> 接口的扩展方法,包括C#中的所有Collections类,如ArrayList,Queue,Stack等等,还有数组和字符串 ...

  2. SQLServer之删除函数

    删除函数注意事项 从当前数据库中删除一个或多个用户定义函数.DROP 函数支持本机编译的标量用户定义函数. 如果数据库中存在引用 DROP FUNCTION 的 Transact-SQL 函数或视图并 ...

  3. Yii2.0调用sql server存储过程并获取返回值

    1.首先展示创建sql server存储过程的语句,创建一个简单的存储过程,测试用. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE P ...

  4. 【原】Java学习笔记024 - 包装类

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 之前对于基本数据类 ...

  5. adb server version doesn’t match this client

    上传文件到海马玩模拟器 环境变量:ANDROID_SDK_HOME配置是D:\Android\android_sdk_windows 报错:adb server version (31) doesn' ...

  6. PowerDesigner导出SQL,注释为空时以name代替

    版本 操作步骤 打开Edit Current DBMS 选中Script->Objects->Column->Add 将Value中的内容全部替换为如下 %20:COLUMN% [% ...

  7. docker容器日志收集方案(方案一 filebeat+本地日志收集)

    filebeat不用多说就是扫描本地磁盘日志文件,读取文件内容然后远程传输. docker容器日志默认记录方式为 json-file 就是将日志以json格式记录在磁盘上 格式如下: { " ...

  8. Github: 从github上拉取别人的源码,并推送到自己的github仓库

    比如说,将 https://github.com/lizhenliang/tomcat-java-demo 迁移到 https://github.com/lousia001/tomcat-java-d ...

  9. ASP.NET MVC学习系列(4)——MVC过滤器FilterAttribute

    1.概括 MVC提供的几种过滤器其实也是一种特性(Attribute),MVC支持的过滤器类型有四种,分别是:AuthorizationFilter(授权),ActionFilter(行为),Resu ...

  10. kernel笔记——网络收发包流程

    本文将介绍网络连接建立的过程.收发包流程,以及其中应用层.tcp层.ip层.设备层和驱动层各层发挥的作用. 应用层 对于使用socket进行网络连接的服务器端程序,我们会先调用socket函数创建一个 ...