UVALive 7457 Discrete Logarithm Problem

来源:互联网 发布:jquery.jsoncookie.js 编辑:程序博客网 时间:2024/06/16 01:44

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5479


题意:给出a,b,p。求( a ^ x ) % p = b中的x。


思路:由于p不超过1W,那么%p后的答案也是小于p的,所以我们乘1W次如果能找到符合条件的x,那么就是存在的;否则无解。


#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007const int maxn = 10009;int p;int a,b;int main(){    cin>>p;    while( scanf("%d%d",&a,&b) == 2 )    {        if ( b == 1 )        {            puts("0");            continue;        }        int ans = 0;        int temp = 1;        bool f = false;        rep(i,1,maxn)        {            ans++;            temp = ( temp * a ) % p;            if ( temp == b )            {                f = true;                printf("%d\n",ans);                break;            }        }        if ( !f ) puts("0");    }    return 0;}


0 0
原创粉丝点击