训练赛 Discrete Logarithm Problem

训练赛 Discrete Logarithm Problem

题意比较简单,a^x%p==b,其实刚开始的时候,我并没有想到a^x这个地方,可能是因为自己比较菜吧,连题也没读懂.但是人家题上明明写了.这个题给的数据量并不是很大,暴力就可以,之后我也自己去查了一下.如果数据很大的话,需要考虑费马小定理:

费马小定理数论中的一个定理:假如训练赛 Discrete Logarithm Problem是一个整数训练赛 Discrete Logarithm Problem是一个质数,那么训练赛 Discrete Logarithm Problem是p的倍数,可以表示为

训练赛 Discrete Logarithm Problem

如果a不是p的倍数,这个定理也可以写成

训练赛 Discrete Logarithm Problem这个书写方式更加常用。

以上内容引用自  维基百科

AC代码:

哦,对了这个题有EOF的,不是以输入0结束.

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<queue>
#include<stdlib.h>
#define INF 0x3f3f3f3f
using namespace std;
long long int p,a,b,x,sum;
int main()
{
    cin>>p;
    while(cin>>a)
    {
        int t;
        if(a==0)
            return 0;
        cin>>b;
        sum=a;
        for(int i=1; i<=p-1; i++)
        {
            sum*=a;
            sum%=p;
            if(sum==b)
            {
                t=i;
                break;
            }
        }
        if(sum==b)
            cout<<t+1<<endl;
        else
            cout<<0<<endl;
    }
    return 0;
}