[POJ 2417]Discrete Logging:BSGS

2 篇文章 0 订阅

点击这里查看原题

模板题,这里贴BSGS教程

/*
User:Small
Language:C++
Problem No.:2417
*/
#include<stdio.h>
#include<iostream>
#include<iomanip>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#define ll long long
#define inf 999999999
using namespace std;
ll a,b,c;
map<ll,int> mp;
ll pow(ll a,ll b,ll p){
    ll res=1;
    while(b){
        if(b&1LL) res=res*a%p;
        a=a*a%p;
        b>>=1LL;
    }
    return res;
}
int main(){
    freopen("data.in","r",stdin);//
    ios::sync_with_stdio(false);
    while(cin>>c>>a>>b){
        bool flag=0;
        mp.clear();
        ll m=(ceil)(sqrt(c)),ans=1;
        ans=b%c;
        mp[ans]=0;
        for(int j=1;j<=m;j++){
            ans=ans*a%c;
            mp[ans]=j;
        }
        ll t=pow(a,m,c);
        ans=1;
        for(int i=1;i<=m;i++){
            ans=ans*t%c;
            if(mp[ans]){
                t=i*m-mp[ans];
                flag=1;
                cout<<(t%c+c)%c<<endl;
                break;
            }
        }
        if(!flag) cout<<"no solution\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值