LA 7457 Discrete Logarithm Problem(shank的大步小布算法)
来源:互联网 发布:蔡仕伟淘宝 编辑:程序博客网 时间:2024/06/05 22:02
题意:求a^x % p = b的最小x。
#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<ctime> #include<cstring>#include<stack>#include<cmath>#include<queue>#define INF 0x3f3f3f3f#define eps 1e-9#define MAXN 100000using namespace std;typedef long long ll;int n,a,b;int ksm(int a,int b){int ans = 1;while(b){if(b & 1) ans = ans * a % n;a = a * a % n;b>>=1;}return ans;}void gcd(int a,int b,int &d,int &x,int &y){if(!b) {d = a;x = 1;y = 0;}else {gcd(b,a%b,d,y,x);y -= x*(a/b);}}int inv(int a,int n){int d,x,y;gcd(a,n,d,x,y);return d == 1 ? (x+n)%n : -1;}int log_mod(int a,int b,int n){int m,v,e = 1;m = (int)sqrt(n+0.5);v = inv(ksm(a,m),n);if(v == -1) return 0;map<int,int> x;x[1] = 0;for(int i = 1;i < m;i++){e = e*a % n;if(!x.count(e)) x[e] = i;}for(int i = 0;i < m;i++){if(x.count(b)) return (i*m + x[b]); b = b*v % n;}return 0;}int main(){scanf("%d",&n);while(~scanf("%d",&a) && a){scanf("%d",&b);printf("%d\n",log_mod(a,b,n));}}
0 0
- LA 7457 Discrete Logarithm Problem(shank的大步小布算法)
- LA 7457 Discrete Logarithm Problem
- UVALive 7457 Discrete Logarithm Problem
- Shank大步小步算法
- UVALive Problem 7457 Discrete Logarithm Problem——Regionals 2015 :: Asia - Taipei
- 数学 ( 解高次同余方程 )——Discrete Logarithm Problem ( UVA 7457 )
- POJ 2417 Discrete Logging【大步小步
- [省选前题目整理][SGU 261]Discrete Roots(扩展欧几里得+中国剩余定理+原根+大步小步算法)
- LA 4270 Discrete Square Roots
- shank这个游戏挺不错的
- Discrete Problem 14
- Discrete Problem 13
- poj2417大步小步法
- [BZOJ3239][poj2417]Discrete Logging(BSGS算法)
- 我的小一步,争取是人类的一大步
- bzoj1420 Discrete Root 原根 大步小步 exgcd
- Poj2417 大步小步算法
- 浅谈大步小步算法
- iOS控件:状态栏
- leetcode-java-110. Balanced Binary Tree
- 设计模式-状态模式
- 验证数学黑洞 ,用户输入一个四位数,输出变换到6174的过程
- 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
- LA 7457 Discrete Logarithm Problem(shank的大步小布算法)
- velociyt判断变量是否为null
- Linux修改默认系统启动级别为命令行模式
- 学编程的都是疯子!!!
- 黄阶低级 - 《 Java 核心》- 4.4 静态域和静态方法 - 修改0次
- 【NOIP2000】单词接龙
- 【HDU】-1179-Ollivanders: Makers of Fine Wands since 382 BC.(二分图,魔杖)
- java中的位运算也是很强大的
- spring之jdbctemplate