题目链接 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. Linux 中磁盘阵列RAID10损坏以及修复

    在Linux 中磁盘阵列RAID10配置中我们已经正确配置了RAID10 ,一般来说在RAID10中最多允许50%的磁盘损毁,当然除了,同一磁盘RAID1中的硬盘设备全部损毁. 这次我们讨论一下:假设 ...

  2. t-sql语句创建表(基础)

    create table ta1 (     id int identity(1,2) not null,     name nvarchar(20) not null,     identify v ...

  3. 基于TCP 协议的RPC

    前言: 环境: windown 10 Eclipse JDK 1.8 RPC的概念: RPC 是远程过程调用,是分布式网站的基础. 实验 SayHelloService.java 接口类,用于规范 S ...

  4. Scheme来实现八皇后问题(1)

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/9768105.html 作者:窗户 Q ...

  5. eclipse弃坑记第一篇之在idea上配置Tomcat环境并创建Javaweb项目的详细步骤原创

    IntelliJ IDEA是一款功能强大的开发工具,在代码自动提示.重构.J2EE支持.各类版本工具(如git.svn.github).maven等方面都有很好的应用. IntelliJ IDEA有免 ...

  6. Python爬虫 selenium

    库的安装 pip3 install selenium 声明浏览器对象 from selenium import webdriver browser = webdriver.Chrome() brows ...

  7. Offset Management For Apache Kafka With Apache Spark Streaming

    An ingest pattern that we commonly see being adopted at Cloudera customers is Apache Spark Streaming ...

  8. Zabbix 3.4.7针对一些主机设置期间维护

    场景说明: 由于公司有些主机设置了定时开机关机,每次开机关机得时候都会发邮件告警,每次都需要值班人员提醒,为了处理这种无效告警,可以在zabbix中设置维护 zabbix中的维护---维护期间:用来设 ...

  9. js 获取纯web地址栏中URL传参

       function GetQueryString(name)    {         var reg = new RegExp("(^|&)"+ name +&quo ...

  10. 人撒娇地撒基督教扫ID祭扫我京东is啊单间

    下下卷惜西,清首花我下望心如单水.低如见惜折 卷水满门我,如二折莲开低下悠子鸦.南水水暮洲 凄暮红过在,莫处言树鸿怀莲绿门莲.杆鸿中花海 见门塘采心,何西杏知底底梅即色花.红两霜言海 秋飞曲杆明,花南 ...