求调 WA on #11
查看原帖
求调 WA on #11
856459
yangjunhan1楼主2025/2/1 17:53

Wrong Answer.wrong answer On line 154 column 1, read 0, expected 1.

#include<bits/stdc++.h>
using namespace std;
#define int long long
int p,b,n,d,N;
bool vis;
int qp(int a,int b){
    int res=1;
    while(b){
        if(b&1) res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
int gcd(int a,int b){
    return !b?a:gcd(b,a%b);
}
signed main(){
    while(cin>>b>>p>>n){
        if(b==p && p==n  && !b) break; 
        b%=p,n%=p;
        if(n==1 || p==1){
            cout<<0<<endl;
            continue;
        }
        N=n;
        map<int,int> a;
        vis=1;
        d=gcd(b,p);
        while(d!=1){
            //cout<<d<<" "<<N<<" "<<p<<endl;
            if(N%d){
                cout<<"No Solution"<<endl;
                vis=0;
                break;
            }
            p/=d;
            N/=d;
            d=gcd(b,p);
        }
        //cout<<endl<<p<<endl;
        if(!vis)    continue;
        int k=sqrt(p);
        if(k*k<p)   k++;
        int bb=qp(b,k);
        int res=n;
        for(int i=0;i<=k;i++,res=res*b%p)    a[res%p]=i;
        res=bb;
        for(int i=1;i<=k;i++,res=res*bb%p)
            if(a[res%p] && i*k>=a[res%p]){
                cout<<i*k-a[res%p]<<endl;
                //cout<<res<<" "<<a[res%p]<<endl;
                vis=0;
                break;
            }
            if(vis) cout<<"No Solution"<<endl;
    }
    return 0;
}

//  g++ bsgs.cpp -O2 -std=c++14 -Wall -o bsgs
2025/2/1 17:53
加载中...