POJ 3243 Clever Y(离散对数-拓展小步大步算法)
Description
Little Y finds there is a very interesting formula in mathematics:
XY mod Z = K
Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, could you figure out Y fast?
Input
Input file ends with 3 zeros separated by spaces.
Output
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL; const int SIZEH = ; struct hash_map {
int head[SIZEH], size;
int next[SIZEH];
LL state[SIZEH], val[SIZEH]; void init() {
memset(head, -, sizeof(head));
size = ;
} void insert(LL st, LL sv) {
LL h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p])
if(state[p] == st) return ;
state[size] = st; val[size] = sv;
next[size] = head[h]; head[h] = size++;
} LL find(LL st) {
LL h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p])
if(state[p] == st) return val[p];
return -;
}
} hashmap; void exgcd(LL a, LL b, LL &x, LL &y) {
if(!b) x = , y = ;
else {
exgcd(b, a % b, y, x);
y -= x * (a / b);
}
} LL inv(LL a, LL n) {
LL x, y;
exgcd(a, n, x, y);
return (x + n) % n;
} LL pow_mod(LL x, LL p, LL n) {
LL ret = ;
while(p) {
if(p & ) ret = (ret * x) % n;
x = (x * x) % n;
p >>= ;
}
return ret;
} LL BabyStep_GiantStep(LL a, LL b, LL n) {
for(LL i = , e = ; i <= ; ++i) {
if(e == b) return i;
e = (e * a) % n;
}
LL k = , cnt = ;
while(true) {
LL t = __gcd(a, n);
if(t == ) break;
if(b % t != ) return -;
n /= t; b /= t; k = (k * a / t) % n;
++cnt;
}
hashmap.init();
hashmap.insert(, );
LL e = , m = LL(ceil(sqrt(n + 0.5)));
for(int i = ; i < m; ++i) {
e = (e * a) % n;
hashmap.insert(e, i);
}
LL p = inv(pow_mod(a, m, n), n), v = inv(k, n);
for(int i = ; i < m; ++i) {
LL t = hashmap.find((b * v) % n);
if(t != -) return i * m + t + cnt;
v = (v * p) % n;
}
return -;
} int main() {
LL x, z, k;
while(cin>>x>>z>>k) {
if(x == && z == && k == ) break;
LL ans = BabyStep_GiantStep(x % z, k % z, z);
if(ans == -) puts("No Solution");
else cout<<ans<<endl;
}
}
POJ 3243 Clever Y(离散对数-拓展小步大步算法)的更多相关文章
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...
- POJ 3243 Clever Y 扩展BSGS
http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...
- poj 3243 Clever Y 高次方程
1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...
- [POJ 3243]Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
- POJ 3243 Clever Y | BSGS算法完全版
题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...
- POJ 3243 Clever Y Extended-Baby-Step-Giant-Step
题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quarter ...
- 【POJ】3243 Clever Y
http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...
- POJ 2417 Discrete Logging(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
随机推荐
- 抓包工具Charles 【转】
今天就来看一下Mac上如何进行抓包,之前有一篇文章介绍了使用Fidder进行抓包 http://blog.csdn.net/jiangwei0910410003/article/details/1 ...
- C# Unicode编码与解码方法
public static class ExtentMethod { public static string ToUnicodeString(this string str) { StringBui ...
- node.js学习路线图
http://www.admin10000.com/document/4624.html
- Android项目框架升级尝鲜OkHttp
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 随着项目日趋稳定,需求不再总是变化,那么是时间来整理下项目了.先简单介绍下,本项目最初使用loop4 ...
- 20145211 《Java程序设计》第3周学习总结——绝知此事要躬行
教材学习内容总结 4.1何为面向对象 面向对象,面向过程都是一种思想,没有高低之分.面向对象,就像是对冰箱操作,冰箱是一个介质,用法就像是c语言中的结构体,功能定义在对象上.面向对象,角色转变,让我们 ...
- SQLAlchemy 一对多
下述範例描述了電影同導演的多對一關係.範例中說明了從用戶定義的Python類建立數據表的方法,雙方關係例項的建立方法,以及最終查詢數據的方法:包括延遲載入和預先載入兩種自動生成的SQL查詢. 結構定義 ...
- graphviz - Node Shapes
Node Shapes There are three main types of shapes : polygon-based, record-based and user-defined. The ...
- Ext中解析字符串
Ext.encode()将json转为json字符串Ext.decode()将json字符串转为json
- ps、grep和kill联合使用杀掉进程(转)
例如要杀掉hello这个进程,使用下面这个命令就能直接实现. ps -ef |grep hello |awk '{print $2}'|xargs kill -9 这里是输出ps -ef |gre ...
- Android Mina框架的学习笔记
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...