关闭

UVALive Problem 7457 Discrete Logarithm Problem——Regionals 2015 :: Asia - Taipei

标签: acm暴力
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’.

 Sample Input

31
24 3
3 15
0

 Sample Output

7
21

 Problem Idea

解题思路:

【题意】
求满足的x值,若无解则输出'0'


【类型】
暴力

【分析】
其实此题已经把题目简化了

因为它给了你x的范围为[1,p-1],然而p的值又很小,所以暴力枚举x的每种可能取值,判断等式是否成立即可

另外,我们提一提如果不给你x的范围应该怎么做

由费马小定理可得




这样我们就可以把x缩小至范围[0,p-2],然后暴力枚举就可以了

因此,此题暴力枚举也无需枚举到p-1,到p-2就可以了,若到p-2还没有满足的解x,那就是无解的情况了

此外要提及的一点是,输入除了单行'0'结束外,还要判断是否到文件尾(EOF),不然会超时(TLE)

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

题目链接→UVALive Problem 7457 Discrete Logarithm Problem

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#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
查看评论

UVALive 7457 Discrete Logarithm Problem

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5479 题意:给出a,b,p。求( a ^ x ...
  • chy20142109
  • chy20142109
  • 2016-08-17 18:45
  • 229

LA 7457 Discrete Logarithm Problem

题意:给你一个质数p (1 方法:暴力。 题目中也给了提示,说当p很大的时候,求离散对数是个难题,但是本题的p比较小,在(1 所以我们只需brute force计算a所有的power mod p,如果当前的power等于b则返回当前指数;否则,如果当前的幂为1(下面的code里是如果当前的幂...
  • Skyette
  • Skyette
  • 2017-01-05 16:55
  • 249

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

UVALive Problem 7456 Least Crucial Node——Regionals 2015 :: Asia - Taipei
  • queuelovestack
  • queuelovestack
  • 2016-08-29 22:19
  • 649

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

UVALive Problem 7454 Parentheses——Regionals 2015 :: Asia - Taipei
  • queuelovestack
  • queuelovestack
  • 2016-08-29 16:46
  • 468

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

由 费马小定理可知: a ^ b % m  = a ^ ( b % (m-1) ) % m ; (费马小定理:m是质数时  a ^ (m-1) % m = 1) 所以对于本题 枚举 a 的 1 - p  次幂对 p ...
  • xiang_6
  • xiang_6
  • 2017-10-28 18:22
  • 62

数学 ( 解高次同余方程 )——Discrete Logarithm Problem ( UVA 7457 )

题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5479 分析题目就是让我们解高次同余方程 ax≡b(m...
  • FeBr2
  • FeBr2
  • 2016-07-31 15:07
  • 597

LA 7457 Discrete Logarithm Problem(shank的大步小布算法)

题意:求a^x % p = b的最小x。 #include #include #include #include #include #include #include #include #include #include #include #include #include #defi...
  • u014258433
  • u014258433
  • 2016-08-17 21:17
  • 142

Regionals 2015 :: Asia - Taipei 部分题解

【第3次区域赛训练赛】最终做了6题,完全是因为这场比赛水的缘故。。。赛后又补了个题,现在来写一下这7题的题解,总结一下。 【A - Parentheses】https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a...
  • just_sort
  • just_sort
  • 2016-08-28 10:38
  • 755

【UVALive】7338 Toll Management IV

【UVALive】7338 Toll Management IV Discription 就给个大意了:给定一张图,问每条边最多能增加和减少多少,使得原本的最小生成树还是最小生成树。
  • Pure_W
  • Pure_W
  • 2017-01-08 13:01
  • 112

UVALive 4260 Fortune Card Game (Regionals 2008 Asia Taipei +DP)

【题目链接】:click here~~ 【题目大意】: A popular card game called ``fortune" is getting popular in country X. Fig. 1 shows one of the cards. In each card,...
  • u013050857
  • u013050857
  • 2015-10-14 21:23
  • 1093
    博客通知
    访客统计
    Flag Counter
    个人资料
    • 访问:288431次
    • 积分:6241
    • 等级:
    • 排名:第4699名
    • 原创:318篇
    • 转载:13篇
    • 译文:0篇
    • 评论:395条
    博客专栏
    最新评论