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 ...
随机推荐
- c#循环迭代匿名类链表(可迭代的匿名类)
Main(){ //为什么?object是基类啊!! //报错.不能从List<anonymous>换成List<object>. //var q=(List<objec ...
- Bundle文件的创建和使用(二)
1.概念: An NSBundle object represents a location in the file system that groups code and resources tha ...
- ArcGIS Engine开发之旅02--ArcGIS Engine中的类库
原文:ArcGIS Engine开发之旅02--ArcGIS Engine中的类库 System类库 System类库是ArcGIS体系结构中最底层的类库.System类库包含给构成ArcGIS的其他 ...
- svn利用TortoiseSVN忽略文件或文件夹
忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有 自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作. ...
- javabean与map互转
/** * 将一个 JavaBean 对象转化为一个 Map * @param bean 要转化的JavaBean 对象 * @return 转化出来的 Map 对象 * ...
- apache httpclient 4.5 兼容 http https
String responseContent = ""; try { SSLContextBuilder contextBuilder = new SSLContextBuilde ...
- Java 日志管理最佳实践
转:http://blog.jobbole.com/51155/ 日志记录是应用程序运行中必不可少的一部分.具有良好格式和完备信息的日志记录可以在程序出现问题时帮助开发人员迅速地定位错误的根源.对于开 ...
- iOS ASIHTTPRequest 使用指南
http://www.devdiv.com/iOS_iPhone-ASIHTTPRequest使用指南---_lt__lt_翻译稿_gt__gt_---连载-thread-93741-1-1.html
- typedef 和 const
1. typedef 允许你为各种数据类型定义新名字 #include <stdio.h>typedef char *ptr_to_char; //这让我看起来,好奇怪,不好读void m ...
- iOS:集成环信EaseMobSDK单聊功能
当然在集成环信之前需要一些准备操作: 1.首先注册环信开发者账号,直接进入环信官网注册即可:http://www.easemob.com 2.按照文档一步一步将需要的文件全部拖入工程中:http:// ...