[SDOI2011]计算器

题意

  给定三个数yzp,求:
  .xyz mod p
  .满足xyz mod p的最小正整数x
  .满足yxz mod p的最小正整数x
  多组询问

解法

  对于①操作,直接套用快速幂即可
  对于②操作,移项得到:x=zy1%p,因为p是质数,所以直接求y的逆元然后算出x即可
  对于③操作,使用BSGS算法(拔山盖世算法)求解:
  对于方程:xAB mod p,令n=pA=knb,那么有:xknbB mod p,移项得到:xknBxb mod p,通过枚举[0,n]内的每一个b,求出xbB,并用哈希表或者map记录下来,然后枚举[1,n]内的每一个k,找到满足xknBxb mod pb,那么A=knb

代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<map>
#define Rint register int
#define Lint long long int
using namespace std;
int T,L;
map<Lint,Lint> f;
Lint extgcd(Lint a,Lint b,Lint &x,Lint &y)
{
    if( !b )   { x=1,y=0;return a; }
    Lint d=extgcd( b,a%b,y,x );
    y-=a/b*x;
    return d;
}
Lint pow(Lint x,Lint k,Lint p)
{
    Lint ret=1;
    while( k )
    {
        if( k&1 )   ret=ret*x%p;
        x=x*x%p,k/=2;
    }
    return ret;
}
void BSGS(int y,int z,int p)
{
    f.clear();
    Lint L=ceil(sqrt(p)),tmp;
    tmp=z%p;f[tmp]=0;
    for(Lint i=1;i<=L;i++)
    {
        tmp=tmp*y%p;
        f[tmp]=i;
    }
    Lint t=pow( y,L,p );tmp=1;
    for(Lint i=1;i<=L;i++)
    {
        tmp=tmp*t%p;
        if( f[tmp] )
        {
            Lint g=i*L-f[tmp];
            printf("%lld\n",(g%p+p)%p);
            return ;
        }
    }
    printf("Orz, I cannot find x!\n");
}
int main()
{
    Lint y,z,p;
    scanf("%d%d",&T,&L);
    while( T-- )
    {
        scanf("%lld%lld%lld",&y,&z,&p);y%=p;
        if( L==1 )   printf("%lld\n",pow(y,z,p));
        if( L==2 )
        {
            z%=p;
            if( y==0 && z!=0 )   printf("Orz, I cannot find x!\n");
            else   printf("%lld\n",z*pow(y,p-2,p)%p);
        }
        if( L==3 )
        {
            if( y%p==0 )   { printf( !z ? "1\n":"Orz, I cannot find x!\n");continue ; }
            BSGS( y,z,p );
        }
    }
    return 0;
}
posted @ 2018-01-14 14:33  清疚  阅读(106)  评论(0)    收藏  举报