poj 3243 Clever Y(离散对数-高次同余方程)

题意:求解x,满足A^x=B(mod C)

思路:数论太难学……勉强看懂了 AekdyCoin 的博文:http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4

            代码也基本是照着写的Orz……


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=(1<<17)-1;
struct HashNode
{
    ll data,id,next;
    HashNode(){}
    HashNode(ll data,ll id,ll next):data(data),id(id),next(next){}
}hash[maxn<<1];
bool flag[maxn<<1];
int tot;
void Insert(ll val,ll i)
{
    ll p=val&maxn;
    if(!flag[p])
    {
        flag[p]=true;
        hash[p]=HashNode(val,i,-1);
    }
    else
    {
        while(hash[p].next!=-1)
        {
            if(hash[p].data==val) return;
            p=hash[p].next;
        }
        if(hash[p].data==val) return ;
        hash[p].next=++tot;
        hash[tot]=HashNode(val,i,-1);
    }
}
ll find(ll val)
{
    ll p=val&maxn;
    if(!flag[p]) return -1;
    while(p!=-1)
    {
        if(hash[p].data==val) return hash[p].id;
        p=hash[p].next;
    }
    return -1;
}
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }
ll ext_gcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll d=ext_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
ll pow_mod(ll a,ll n,ll m)
{
    ll res=1;
    a%=m;
    while(n)
    {
        if(n&1) res=res*a%m;
        a=a*a%m;
        n>>=1;
    }
    return res;
}
ll BabyStep_GiantStep(ll A,ll B,ll C)
{
    tot=maxn;B%=C;
    ll tmp=1;
    for(int i=0;i<=100;++i)
    {
        if(tmp==B%C) return i;
        tmp=tmp*A%C;
    }
    ll D=1,d=0;
    while((tmp=gcd(A,C))!=1)
    {
        if(B%tmp) return -1;
        C/=tmp;
        B/=tmp;
        D=D*A/tmp%C;
        d++;
    }
    ll m=(ll)ceil(sqrt((double)C));
    tmp=1;
    for(int i=0;i<=m;++i)
    {
        Insert(tmp,i);
        tmp=tmp*A%C;
    }
    ll x,y,K=pow_mod(A,m,C);
    for(int i=0;i<=m;++i)
    {
        ext_gcd(D,C,x,y);
        tmp=((B*x)%C+C)%C;
        if((y=find(tmp))!=-1)
            return i*m+y+d;
        D=D*K%C;
    }
    return -1;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll A,B,C;
    while(cin>>A>>C>>B)
    {
        if(!A&&!B&&!C) break;
        memset(flag,0,sizeof(flag));
        ll ans=BabyStep_GiantStep(A,B,C);
        if(ans==-1)
            cout<<"No Solution"<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值