poj 2417 Discrete Logging
来源:互联网 发布:centos 6.8 搭建lnmp 编辑:程序博客网 时间:2024/05/22 00:54
转载请注明出处,谢谢http://blog.csdn.net/bigtiao097?viewmode=contents
题意:
给定A、B、C,求
这题的C一定是质数
思路:
模板题,BSGS算法
具体代码如下:
Result:Accepted Memory: 1492K Time : 32MS
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;//baby_step giant_step// a^x = b (mod n) n为素数,a,b < n// 求解上式 0<=x < n的解typedef long long ll;const int MOD = 76543;int hs[MOD],head[MOD],next[MOD],id[MOD],top;void insert(int x,int y){ int k = x%MOD; hs[top] = x, id[top] = y, next[top] = head[k], head[k] = top++;}int find(int x){ int k = x%MOD; for(int i = head[k]; i != -1; i = next[i]) if(hs[i] == x) return id[i]; return -1;}int BSGS(int a,int b,int n){ memset(head,-1,sizeof(head)); top = 1; if(b == 1)return 0; int m = sqrt(n*1.0), j; ll x = 1, p = 1; for(int i = 0; i < m; ++i, p = p*a%n)insert(p*b%n,i); for(ll i = m; ;i += m) { if( (j = find(x = x*p%n)) != -1 )return i-j; if(i > n)break; } return -1;}int main(){ int a,b,n; while(scanf("%d%d%d",&n,&a,&b) == 3) { int ans = BSGS(a,b,n); if(ans == -1)printf("no solution\n"); else printf("%d\n",ans); } return 0;}
阅读全文
0 0
- 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
- POJ 2417 Discrete Logging 笔记
- POJ 2417 Discrete Logging 离散对数/BabyStep_GiantStep
- POJ 2417 Discrete Logging (baby_step,giant_step算法)
- [poj 2417]Discrete Logging 数论 BSGS
- POJ 2417 Discrete Logging 离散对数
- [poj 2417] Discrete Logging · BSGS
- poj 2417 Discrete Logging(baby-step算法)
- POJ 2417 Discrete Logging (BSGS)
- POJ 2417 Discrete Logging【大步小步
- PoJ 2417 Discrete Logging BSGS裸题
- POJ 2417 Discrete Logging(解高次同余方程)
- poj 2417 Discrete Logging(求离散对数)
- 递归和非递归分别实现求n的阶乘
- 实现自己的strcpy和memcpy
- epoll源码剖析
- 二叉搜索树
- java中设置网络代理
- poj 2417 Discrete Logging
- python中的多线程-threading
- HDU 6106 Classes【简单思维】
- Hibernate 使用HQL方法进行模糊查询(3张表联合查询)
- HEVC评测
- Python基础总结(5)
- 寻找多数元素(主元素)
- POJ3461
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 记忆化搜索