poj 2417 Discrete Logging(baby-step算法)
来源:互联网 发布:php微信支付视频教程 编辑:程序博客网 时间:2024/06/04 19:13
Description
给出P,B,N(P是素数),解方程B^L = N (mod P)。
Input
多组输入,每组用例占一行包括三个整数P,B,N,以文件为结束输入。
Output
对于每组用例,如果方程有解则输出最小解,否则输出no solution。
参考资料:
http://www.cppblog.com/csu-yx-2013/archive/2012/07/29/185562.html?opt=admin
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;#define MAX (1000000)long long nData[MAX];long long nKey[MAX];long long egcd(long long a, long long b, long long& x, long long& y){ if (b == 0) { x = 1; y = 0; return a; } long long ret = egcd(b, a % b, x, y); long long t = x; x = y; y = t - (a / b) * y; return ret;}long long GetPos(long long key){ return (key ^ 0xA5A5A5A5) % MAX;}void Add(long long key, long long data){ long long nPos = GetPos(key); while (nData[nPos] != -1) { nPos = (nPos + 1) % MAX; } nData[nPos] = data; nKey[nPos] = key;}int Query(int key){ int nPos = GetPos(key); while (nData[nPos] != -1) { if (nKey[nPos] == key) { return nData[nPos]; } nPos = (nPos + 1) % MAX; } return -1;}long long BabyStep(long long nA, long long nB, long long nP){ long long nM = ceil(sqrt((double)(nP - 1))); long long x, y; egcd(nP, nA, x, y);//y是nA%p的乘法逆 y = (y + nP) % nP; long long nTemp = 1; long long c = 1;//c是nA的—m次 memset(nData, -1, sizeof(nData)); memset(nKey, -1, sizeof(nKey)); for (long long j = 0; j < nM; ++j) { Add(nTemp, j); nTemp = (nTemp * nA) % nP; c = (c * y) % nP; } long long r = nB; for (int i = 0; i < nM; ++i) { long long j = Query(r); if (j != -1) { return i * nM + j; } r = (r * c) % nP; } return -1;}int main(){ long long nP, nB, nN; while (scanf("%I64d%I64d%I64d", &nP, &nB, &nN) == 3) { long long nAns = BabyStep(nB, nN, nP); if (nAns == -1)printf("no solution\n"); else printf("%I64d\n", nAns); } return 0;}
0 0
- poj 2417 Discrete Logging(baby-step算法)
- POJ 2417 Discrete Logging (Baby Step Giant Step算法)
- POJ 2417:Discrete Logging——Baby Step, Giant Step
- POJ 2417 Discrete Logging Baby-Step-Gaint-Step
- poj Discrete Logging (Baby-step-Giant - step)
- POJ 2417 Discrete Logging(高次同余方程-Baby-Step,Giant-Step)
- poj2417(Discrete Logging)-Baby Step Giant Step
- 【POJ 3243-Clever Y】 与【POJ 2417-Discrete Logging】(解高次同余方程 Baby-Step-Gaint-Step)
- Poj 2417 Discrete Logging (Baby Step Giant Step 解 a^x = b (mod n) n为素数)
- BZOJ 3239 Discrete Logging Baby-Step-Giant-Step
- poj2417 Discrete Logging(BSGS:Baby Step Giant Step)
- POJ 2417 Discrete Logging (baby_step,giant_step算法)
- Discrete Logging[POJ 2417]
- POJ 2417 Discrete Logging
- POJ 2417 Discrete Logging
- poj 2417 Discrete Logging
- POJ 2417 Discrete Logging BSGS
- [POJ 2417]Discrete Logging:BSGS
- SDUSTOJ Problem C: 平面上的点——Point类 (III)
- HashMap和Hashtable的区别
- iOS_Block详解1
- QT中的setAttribute()方法
- SDUSTOJ Problem D: 时间类的构造和输出
- poj 2417 Discrete Logging(baby-step算法)
- 无题
- Unity动态更新
- Android 启动APP时黑屏白屏的三个解决方案
- 流对象缓存的宏定义
- argparse - 命令行选项与参数解析(译)
- SDUSTOJ Problem E: 时间类的成员读写
- 寻找百度图片搜索接口历程--one
- HDU 4673 Theme Section