POJ 2417 Discrete Logging(高次同余方程-Baby-Step,Giant-Step)

201 篇文章 10 订阅

Description
给出P,B,N(P是素数),解方程B^L = N (mod P)
Input
多组输入,每组用例占一行包括三个整数P,B,N,以文件为结束输入
Output
对于每组用例,如果方程有解则输出最小解,否则输出no solution
Sample Input
5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111
Sample Output
0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587
Solution
解高次同余方程a^x=b(mod p),由于数据范围太大所以暴力显然不行,此处因为b,p互素所以可以采用Baby-Step,Giant-Step算法,令n=sqrt(p),则x可以表示成k*n+r的形式(0 <= k,r <= n),原方程即可转化为a^(k*n+r)=b(mod p),即为a^r=b*(a^(-n))^k(mod p),这样一来我们只需要用O(n)的时间将a^i(0 <= i <= r)预处理从出来存到一个map中,再用O(n)的时间枚举k(先算出a^(-n),此处需要用到费马小定理求逆元),对于每个k用O(log n)的时间在map中查找即可,所以总复杂度是O(n+nlog n),即O(sqrt(p)log(sqrt(p)))
Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector> 
using namespace std;
typedef long long ll;
ll mod_pow(ll a,ll b,ll p)//快速幂 
{   
    ll ans=1;
    a%=p;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%p;
        a=a*a%p;
        b>>=1;
    }
    return ans;
}
ll mul_linear(ll a,ll b,ll p)//求解高次同余方程a^x=b(mod p) 
{
    ll n=ceil(sqrt((double)(p+1)));
    map<ll,ll>Map;
    Map[1]=n;//本来a^r=1映射的r值是0,但为便于后面通过Map[x]=0?来判断x是否在Map中所以此处用n代替 
    ll m=mod_pow(a,p-2,p);//求a的逆元 
    m=mod_pow(m,n,p);//求a^(-n) 
    ll temp=1;
    for(ll i=1;i<n;i++)//将a^i存入map中 
    {
        temp=(temp*a)%p;
        if(!Map[temp])
            Map[temp]=i;
    }
    for(ll i=0;i<n;i++)//枚举k寻找可行解 
    {
        if(Map[b])
        {
            ll r=Map[b];
            Map.clear();
            return i*n+(r==n?0:r);//如果是n表示r=0 
        }
        b=(b*m)%p;
    }
    return -1;//无解 
}
int main()
{
    ll B,N,P;
    while(~scanf("%lld%lld%lld",&P,&B,&N))
    {
        ll ans=mul_linear(B,N,P); 
        if(ans==-1)
            printf("no solution\n");
        else
            printf("%lld\n",ans);   
    }
    return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值