您的位置:首页 > 其它

UVALive 7457 Discrete Logarithm Problem

2016-08-17 18:45 441 查看
题目链接: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 100000007
const 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: