POJ 3243 Clever Y(擴充套件BSGS,gcd(a,p)!=1)
題目連結:
POJ 3243 Clever Y
題意:
跟POJ 2417 Discrete Logging類似,只不過
分析;
初始化
令
通過若干輪消掉
但是這樣求得的解是
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#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)
{
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)
{
ll d, x, y;
d = ex_gcd(a, p, x, y);
return d == 1 ? (x % p + p) % p : -1;
}
ll BSGS(ll a, ll b, ll 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);
ll res = b, z;
for(ll i = 0; i < m; i++) {
if((z = get(res)) != -1) return i * m + z;
res = res * base % p;
}
return -1;
}
ll gcd(ll x, ll y)
{
return y == 0 ? x : gcd(y, x % y);
}
ll solve(ll a, ll b, ll p)
{
ll tmp = 1;
for(int i = 0; i <= 50; ++ i) {
if(tmp == b) return i;
tmp = tmp * a % p;
}
ll cnt = 0, d = 1 % p;
while((tmp = gcd(a, p)) != 1) {
if(b % tmp) return -1;
b /= tmp;
p /= tmp;
d = a / tmp * d % p;
cnt++;
}
b = b * inv(d, p) % p;
ll ans = BSGS(a, b, p);
if(ans == -1) return -1;
else return ans + cnt;
}
int main()
{
ll a, b, p;
while(~scanf("%lld%lld%lld", &a, &p, &b) && (a || p || b)){
ll ans = solve(a, b % p, p);
if(ans == -1) printf("No Solution\n");
else printf("%lld\n", ans);
}
return 0;
}
相關推薦
POJ 3243 Clever Y(擴充套件BSGS,gcd(a,p)!=1)
題目連結: POJ 3243 Clever Y 題意: 跟POJ 2417 Discrete Logging類似,只不過gcd(a,p)!=1. 分析; 初始化cnt=0(消因子輪數),d=1(消掉的gcd乘積). 令tmp=gcd(a,p) 當tm
POJ-3243-Clever Y-擴充套件Bsgs演算法-已知a,c,p,和a^b=c(mod p),求b
【Description】 Little Y finds there is a very interesting formula in mathematics: XYmod Z=K X^Y mod\:Z=K XYmodZ=K
[BZOJ 1467] [POJ 3243] clever Y
題目描述 小Y發現,數學中有一個很有趣的式子: XYmodZ=KX^Y\ mod\ Z = KXYmodZ=K 給出XXX、YYY、ZZZ,我們都知道如何很快的計算KKK。但是如果給出XXX、ZZZ
POJ 2417 Discrete Logging(BSGS演算法,gcd(a,p)=1)
題目連結: POJ 2417 Discrete Logging 題意: 求ax≡b(modp)的最小x。(p為素數,2≤a<p,1≤b<p) 分析: BSGS算法。 一開始用map一直T,參(zhao)考(ban)了這裡 用了hash才
POJ 3243 Clever Y (求X^Y mod Z = K)
Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7301 Accepted: 1807 Description Little Y finds there is a very i
BZOJ_P1467/POJ_P3243 Clever Y(擴充套件BSGS+雜湊)
Time Limit: 4 Sec Memory Limit: 64 MB Submit: 202 Solved: 106 [Submit][Status][Discuss] Description 小Y發現,數學中有一個很有趣的式子: X^Y
POJ 3243 Clever Y 解高次同餘方程
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3104 Accepted: 1495 Description Given a prime P, 2 <= P < 231, an
BZOJ 3243 Clever Y
more lol pmo .html zoj tdi end nav blog Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K
[拓展Bsgs] Clever - Y
題目連結 Clever - Y 題意 有同餘方程 \(X^Y \equiv K\ (mod\ Z)\),給定\(X\),\(Z\),\(K\),求\(Y\)。 解法 如題,是拓展 \(Bsgs\) 板子,部分學習內容在這裡 \((Click\ here)\)。 敲完板子就能獲得至少 5
【BSGS】POJ2417[Discrete Logging]&POJ3243[Clever Y]題解
POJ2417 題目概述 求滿足Ax≡B(mod C)的最小x,C是素數。 解題報告 這就是經典的BSGS,由於要求最小的,所以雜湊表儲存時刷個小的就行了。 示例程式 #include<cstdio> #include&l
POJ 2115 C Looooops 擴充套件gcd
很裸的擴充套件gcd 3月份做過一次 不過那時候稀裡糊塗不求甚解 最近又好好看了下擴充套件gcd 這就一sb題 ACcode: #include <iostream> #include &
【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS
sof i++ long mod data -s for scrip input 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod Description 已知數a,p,b,求滿足a^x≡b(mod p)的最小自然
SPOJ4491. Primes in GCD Table(gcd(a,b)=d素數,(1<=a<=n,1<=b<=m))加強版
function ted solid result writing set silver %d ron SPOJ4491. Primes in GCD Table Problem code: PGCD
[BZOJ1467]Pku3243 clever Y
mil post puts template targe while return eve 0.11 傳送門 傳送門 exbsgs模板。 //Achen #include<algorithm> #include<iostream> #includ
POJ3243:Clever Y——題解
++ code ostream AI names 整數 是的 += cnblogs http://poj.org/problem?id=3243 求最小的非負整數y滿足x^y=k(mod z) 寫完板子之後等待了半個小時poj才終於進入…&hell
【HDU 2814 擴充套件尤拉 a^b ≡ (a mod c)^b mod ϕ(c)+ϕ(c) modc,b>=ϕ(c) 】
G(1)=F(ab)G(1)=F(ab) G(n)=G(n−1)F(ab)(n>=2)G(n)=G(n−1)F(ab)(n>=2) 求G(n)modc 具體: In mathematics, the Fibonacci numbers are a sequence of
擴充套件BSGS求解離散對數問題
擴充套件BSGS用於求解axΞb mod(n) 同餘方程中gcd(a,n)≠1的情況 基本思路,將原方程轉化為a與n互質的情況後再套用普通的BSGS求解即可 struct Hashmap{ static const int Ha=999917,maxe=46340; int E,ln
POJ 2449 Remmarguts' Date 第K短路 A* + dij
A*/第k短路模板題 #include <iostream> #include <vector> #include <cstdio> #include <string> #include <cstring> #in
Kuangbin專題十六KMP & 擴充套件KMP & Manacher(A~L)
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 10
Poj 2142 The Balance 擴充套件歐幾里得
Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 20