POJ 2417 Discrete Logging(BSGS算法,gcd(a,p)=1)
来源:互联网 发布:澳门网络彩票 编辑:程序博客网 时间:2024/05/17 08:30
题目链接:
POJ 2417 Discrete Logging
题意:
求
分析:
一开始用
用了
令
所以可以先处理出
为什么枚举
当
由费马小定理得:
所以只需要枚举所有
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <climits>#include <cmath>#include <ctime>#include <cassert>#include <set>#include <map>#define IOS ios_base::sync_with_stdio(0); cin.tie(0);using namespace std;typedef long long ll;const ll MOD = 100007;ll hs[MOD + 100], id[MOD + 100];ll find(ll x){ ll t = x % MOD; while(hs[t] != x && hs[t] != -1) t = (t + 1) % MOD; return t;}void insert(ll x, ll ii){ ll pos = find(x); if(hs[pos] == -1){ hs[pos] = x; id[pos] = ii; }}ll get(ll x){ ll pos = find(x); return hs[pos] == x ? id[pos] : -1;}ll ex_gcd(ll a, ll b, ll& x, ll& y){//求解:ax + by = gcd(a, b) if(b == 0){ x = 1, y = 0; return a; } ll d = ex_gcd(b, a % b, y, x); y -= a / b * x; return d;}ll inv(ll a, ll p) //求解:a * x ≡ 1 (mod p){ ll x, y, d; d = ex_gcd(a, p, x, y); //ax + py = gcd(a, p) return d == 1 ? (x % p + p) % p : -1;}ll BSGS(ll a, ll b, ll p){//求解a^x ≡ b (mod p) memset(hs, -1, sizeof(hs)); memset(id, -1, sizeof(id)); ll m = (ll)ceil(sqrt( p + 0.5)); ll tmp = 1; for(ll i = 0; i < m; ++ i) { insert(tmp, i); tmp = tmp * a % p; } ll base = inv(tmp, p); //tmp = a ^ m % p if(base == -1) return -1; //在本题中p为素数且a<p所以这种情况永远不会出现 ll res = b; for(ll i = 0; i < m; ++ i) { if(get(res) != -1) return i * m + get(res); res = res * base % p; } return -1;}int main(){ ll a, b, p; while(~scanf("%lld%lld%lld", &p, &a, &b)){ ll ans = BSGS(a, b, p); if(ans == -1) printf("no solution\n"); else printf("%lld\n", ans); } return 0;}
0 0
- POJ 2417 Discrete Logging(BSGS算法,gcd(a,p)=1)
- POJ 2417 Discrete Logging BSGS
- [POJ 2417]Discrete Logging:BSGS
- POJ 2417 Discrete Logging bsgs算法模板题
- POJ-2417 Discrete Logging (BSGS算法,离散对数)
- [poj 2417]Discrete Logging 数论 BSGS
- [poj 2417] Discrete Logging · BSGS
- POJ 2417 Discrete Logging (BSGS)
- PoJ 2417 Discrete Logging BSGS裸题
- 【哈希表除留余数法+BSGS算法求离散对数】POJ Discrete Logging 2417
- POJ 2417/BZOJ 3239(Discrete Logging-BSGS)[Template:数论]
- 【bzoj 3239】【POJ 2417】Discrete Logging(BSGS)
- POJ 2417 Discrete Logging(离散对数 BSGS)
- POJ 3243 Clever Y(扩展BSGS,gcd(a,p)!=1)
- [BZOJ3239][poj2417]Discrete Logging(BSGS算法)
- 【bzoj3239】Discrete Logging BSGS
- [BZOJ3239][BSGS]Discrete Logging
- POJ 2417 Discrete Logging (baby_step,giant_step算法)
- html select
- java图片裁切工具类
- HDU 1004 Let the Balloon Rise
- Gem: whenever定时执行任务(rake脚本的编写)
- leetcode 344 Reverse String
- POJ 2417 Discrete Logging(BSGS算法,gcd(a,p)=1)
- python打印列表中指定元素的所有下标(5种方法)
- android的帧动画和补间动画总结
- CDH中impala读取不到hive创建的表
- ds1302写时间
- Codeforces Round#321 Kefa and Dishes(状压DP)
- Cow Contest poj3360()
- mysql性能优化-慢查询分析、优化索引和配置
- iOS 【如何把项目托管到GitHub】