【Ex_BSGSBSGS算法模板】poj2417 poj3243
生活随笔
收集整理的這篇文章主要介紹了
【Ex_BSGSBSGS算法模板】poj2417 poj3243
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:給定a,b,p,求最小的非負整數x,滿足 ?ax?≡?b(mod?p)
Ex_BSGS,p是不是素數都可以
?X^Y mod Z = K ??
?優化的BSGS算法 使得 Z不是素數的時候可以求出最小的 y 使得同余式成立
?
//#include <bits/stdc++.h> ///復雜度為O(sqrt(n)) #include <iostream>//XY mod Z = K 模板題 #include <cstring> #include <cstdio> #include <cmath> #define X 10005 #define inF 0x3f3f3f3f #define PI 3.141592653589793238462643383 #define IO ios::sync_with_stdio(false),cin.tie(0), cout.tie(0); #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; typedef long long ll; typedef unsigned long long Ull; //2^64 const int maxn = (int)1e5 + 10; const int MOD = 9973;//(int)1e9 + 7; const ll inf = 9223372036854775807; const int mod = (int)1e5 + 10; ll hs[mod],next[mod],head[mod],id[mod],top; void insert(ll x,int y) {int k=x%mod;hs[top]=x,id[top]=y,next[top]=head[k],head[k]=top++; } ll find(ll x) {int k=x%mod;for(int i=head[k];i!=-1;i=next[i]){if(hs[i]==x)return id[i];}return -1; } ll BSGS(ll a,ll b,ll n) {memset(head,-1,sizeof(head));if(b==1)return 0;top=1;ll m=sqrt(n*1.0),x=1,j,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() {ll x,z,k;while(scanf("%lld%lld%lld",&x,&z,&k)&&(x||z||k)){ll d=BSGS(x,k,z);if(d==-1)cout<<"No Solution"<<endl;elsecout<<d<<endl;}return 0; } /*?
總結
以上是生活随笔為你收集整理的【Ex_BSGSBSGS算法模板】poj2417 poj3243的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poj 1830
- 下一篇: Bzoj 3122 随机数生成器