Description

Little Y finds there is a very interesting formula in mathematics:

XY mod Z = K

Given XYZ, we all know how to figure out K fast. However, given XZK, could you figure out Y fast?

Input

Input data consists of no more than 20 test cases. For each test case, there would be only one line containing 3 integers XZK (0 ≤ XZK ≤ 109). 
Input file ends with 3 zeros separated by spaces. 

Output

For each test case output one line. Write "No Solution" (without quotes) if you cannot find a feasible Y (0 ≤ Y < Z). Otherwise output the minimum Y you find.
 
题目大意:求离散对数。没有保证Z一定是素数。
PS:说一下上面没有提到的一个东西,最后一个O(sqrt(m))的循环中,逆元是没有必要每次都求的,可以预先求出来,让算法复杂度降至O(sqrt(m))。
在我的代码中,v = k^(-1) * a^(-i*m),可以在前面先求出k^(-1)和a^(-m),然后每次v都乘以a^(-m),而不需要每次对k*a^(i*m)求逆元。
 
代码(63MS):
 #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(离散对数-拓展小步大步算法)的更多相关文章

  1. 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 ...

  2. POJ 3243 Clever Y 扩展BSGS

    http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...

  3. poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】

    扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...

  4. poj 3243 Clever Y 高次方程

    1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...

  5. [POJ 3243]Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  6. POJ 3243 Clever Y | BSGS算法完全版

    题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...

  7. POJ 3243 Clever Y Extended-Baby-Step-Giant-Step

    题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quarter ...

  8. 【POJ】3243 Clever Y

    http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...

  9. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

随机推荐

  1. CC Debugger调试下载接口

    调试下载接口: 引脚序号 引脚名称 相关说明 1 GND 地线 2 VDD 目标板电源正端 3 DC 调试-时钟线 4 DD 调试-数据线 5 CSn 下载-片选线(低电平有效) 6 SCLK 下载- ...

  2. Java科普之加密算法

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 加密比较复杂,但今天公司有需求,就稍微再研究一下,方式只有两种,对称加密和非对称加密.对称加密是指加 ...

  3. 关于appstore多语言版本,不可不看!

    http://www.cocoachina.com/appstore/20160513/16256.html

  4. angularJS自定义属性作为条件中转

    <html> <head> <meta charset="utf-8"/> <title></title> </h ...

  5. QDir路径的测试与创建-QT

    #include <QCoreApplication> #include <QDir> #include<QtDebug > #include<QFileIn ...

  6. Asp.net MVC23 使用Areas功能的常见错误

    一般WEB项目都会不同的页面区域,如:用户前台.用户后台.管理员后台. 访问的URL: 用户前台:www.domain.com/home/index 用户后台:www.domain.com/admin ...

  7. Windows-003-桌面无显解决方法

    本文主要介绍桌面无显的解决方法,仅供参考,若有更好的解决方案,欢迎告知. 桌面无显分为两种情况:桌面快捷图标无显,任务栏正常显示:桌面仅显示背景图片(桌面快捷图标.任务栏均无显).下面针对这两种情况给 ...

  8. 微信分享JS接口失效说明及解决方案

    关键字:微信分享 JS 失效  分享到朋友圈 微信分享JS接口目前已失效,以前可以自定义分享的标题.描述.图片.链接地址在微信6.0.2版本中失效. 官方回复如下: 旧版的获取分享状态及设置分享内容的 ...

  9. 前端开发者需要的10个Sublime插件

    Sublime Text是最强大的代码编辑器之一,它具有一些神奇的功能,而且可以通过安装插件或包来变得更强大.这些插件为Sublime Text添加了额外的功能.当今有很多插件可以用来满足几乎任何你的 ...

  10. [BS-14] 打印NSArray和NSDictionary的3种方法

    打印NSArray和NSDictionary的3种方法 NSArray *arr = @[@"奔驰",@"宝马",@"路虎",@" ...