# 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;
}

#### LA 7457 Discrete Logarithm Problem

2017-01-05 16:55:46

#### UVALive 7457 Discrete Logarithm Problem

2016-08-17 18:45:38

#### The 2015 ACM-ICPC Asia Beijing Regional Contest K A Math Problem

2015-11-16 18:55:35

#### HDU 5476 Explore Track of Point 几何题 —— 2015 ACM/ICPC Asia Regional Shanghai Online

2015-09-26 19:06:18

#### UVALive Problem 7456 Least Crucial Node——Regionals 2015 :: Asia - Taipei

2016-08-29 22:19:43

#### UVALive Problem 7454 Parentheses——Regionals 2015 :: Asia - Taipei

2016-08-29 16:46:27

#### HihoCoder 1249（2015 Asia Beijing Regional Contest）

2016-10-09 19:38:56

#### UVALive - 7457 Discrete Logarithm Problem 费马小定理+暴力枚举+快速幂

2017-10-28 18:22:33

#### FZU Problem 2238 Daxia & Wzc's problem(Lucas定理求组合数)

2016-08-20 12:17:00

#### 椭圆曲线数字签名算法（ECDSA）英文版

2011年11月02日 602KB 下载