604人阅读 评论(0)

# UVALive Problem 7457 Discrete Logarithm Problem

Accept: 0    Submit: 0
Time Limit: 3.000 seconds

## Problem Description

Finite groups are used in almost all modern cryptosystems. Let p be a prime. The finite multiplicative group constructed by integers modulo p is denoted by . The elements of the group are 1, 2, . . . , p−1. Let a and b be two elements in . The value of a×b is define as a·b mod p. For example, let p = 13, a = 5, and b = 7. Then 5 × 7 = 5·7 mod 13 = 35 mod 13 = 9.

You are going to write a program to solve the discrete logarithm problem in . That is, given p, a, and b, compute x satisfying

For very large p, solving discrete logarithm problem in may not be easy. However, in this problem the value of p will not be very large.

## Input

The first line of the input file contains a prime p, 1 < p < 2^13 . This prime will be used in the following test cases. Each test case contains two integers a and b in a line. The last test case is followed by a line containing ‘0’.

## Output

For each test case, print out the solution x to the equation in the finite group . If there are no solutions, print ‘0’.

31
24 3
3 15
0

7
21

【题意】

【类型】

【分析】

【时间复杂度&&优化】
O(p)

## Source Code

/*Sherlock and Watson and Adler*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 15;
const int M = 100005;
const int inf = 1000000007;
const int mod = 7;
int main()
{
int p,a,b,i,s;
scanf("%d",&p);
while(~scanf("%d",&a)&&a)//'~'不能少,否则TLE
{
s=a%p;
scanf("%d",&b);
for(i=1;i<=p-2;i++,s=a%p*s%p)
if(s==b)
break;
if(i>p-2)
puts("0");
else
printf("%d\n",i);
}
return 0;
}

1
0

博客通知
访客统计
个人资料
• 访问：288431次
• 积分：6241
• 等级：
• 排名：第4699名
• 原创：318篇
• 转载：13篇
• 译文：0篇
• 评论：395条
博客专栏
 Multi-University Training Contest 文章：37篇 阅读：38995
阅读排行
最新评论