bsgs算法 a^x=bmod(c) 求x (模板)

题目链接:

https://cn.vjudge.net/problem/POJ-2417

题目大意:

具体思路:

https://blog.csdn.net/clover_hxy/article/details/50683832

AC代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 2e5+100;
ll a,b,c,m;
map<ll,int>vis;
ll qsm(ll t1,ll t2,ll mod)
{
    ll ans=1ll;
    while(t2)
    {
        if(t2&1)
            ans=ans*t1%mod;
        t1=t1*t1%mod;
        t2>>=1;
    }
    return ans%mod;
}
int main()
{
    while(~scanf("%lld %lld %lld",&c,&a,&b))
    {
        vis.clear();
        if(a%c==0)
        {
            printf("no solution\n");
            continue;
        }
        int flag=0;
        m=ceil(sqrt(c));
        ll ans;
        for(int i=0; i<=m; i++)
        {
            if(!i)
            {
                ans=b%c;
                vis[ans]=i;
                continue;
            }
            else
            {
                ans=(ans*a)%c;
                vis[ans]=i;
            }
        }
        ll t=qsm(a,m,c);
        ans=1;
        for(int i=1; i<=m; i++)
        {
            ans=ans*t%c;
            if(vis[ans])
            {
                int tmp=i*m-vis[ans];
                printf("%d\n",(tmp%c+c)%c);
                flag=1;
                break;
            }
        }
        if(!flag)
        {
            printf("no solution\n");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/letlifestop/p/11019512.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值