AcWing 224. Calculator (BSGS algorithm) punch

Topic: https://www.acwing.com/problem/content/226/

Meaning of the questions: There is a calculator to complete three functions

1, a given Y, Z, P, calculates the Y the Z M O D P value of YZModP;

2, a given Y, Z, P, is calculated to satisfy X the Y the Z ( m O D P ) xY≡Z (mod P) of the smallest non-negative integer;

3, a given Y, Z, P, is calculated to meet the Y X the Z ( m O D P ) Yx≡Z (mod P) of the smallest non-negative integer.

Ideas: The first obviously is a quick power modulo, second, because p is a prime number, so we can quickly compute power to take inverse yuan, a third of the board is BSGS

#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define ll long long
//快速幂求a^b
LL power(LL a,LL b,LL n)
{
    LL s=1;
    while(b)
    {
        if(b&1)
            s=(s*a)%n;
        a=(a*a)%n;
        b=b>>1;
    }
    return s;
}
ll work2(ll y,ll z,ll p)//xy=z mod p
{
    if(y%p==0&&z!=0) return -1;
    return z*power(y,p-2,p)%p;
}
ll bsgs(ll a,ll b,ll p){
    map<ll,ll> hash;
    hash.clear();
    b%=p;
    ll t=(ll)sqrt(p)+1;
    for(int j=0;j<t;j++){
        ll val = (ll)b*power(a,j,p)%p;
        hash[val]=j;
    }
    a=power(a,t,p);
    if(a==0) return b==0?1:-1;
    for(int i=0;i<=t;i++){
        ll val = power(a,i,p);
        ll j=hash.find(val)==hash.end()?-1:hash[val];
        if(j>=0&&i*t-j>=0) return i*t-j; 
    } 
    return -1;
}
int main()
{
    LL a,b,t,n;
    ll op;
    scanf("%lld%lld",&t,&op);
    for(int i=0;i<t;i++){ 
        scanf("%lld%lld%lld",&a,&b,&n);
        if(op==1){
            printf("%lld\n",power(a,b,n));
        }
        else if(op==2){
            ll w=work2(a,b,n);
            if(w==-1) printf("Orz, I cannot find x!\n");
            else printf("%lld\n",w);
        }
        else if(op==3){
            ll w=bsgs(a,b,n);
            if(w==-1)    printf("Orz, I cannot find x!\n");
            else printf("%lld\n",w);
        } 
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Lis-/p/11290502.html