Discrete Logging
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5865   Accepted: 2618

Description

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that

    B

L

 == N (mod P)

Input

Read several lines of input, each containing P,B,N separated by a space.

Output

For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".

Sample Input

5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111

Sample Output

0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587

Hint

The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states

   B

(P-1)

 == 1 (mod P)

for any prime P and some other (fairly rare) numbers known as base-B
pseudoprimes. A rarer subset of the base-B pseudoprimes, known as
Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A
corollary to Fermat's theorem is that for any m

   B

(-m)

 == B

(P-1-m)

 (mod P) .

Source

BSGS模板题
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#define LL long long
using namespace std;
LL a,b,c;
map<LL,LL>mp;
LL fastpow(LL a,LL p,LL c)
{
LL base=a;LL ans=;
while(p!=)
{
if(p%==)ans=(ans*base)%c;
base=(base*base)%c;
p=p/;
}
return ans;
}
int main()
{
// a^x = b (mod c)
while(scanf("%lld%lld%lld",&c,&a,&b)!=EOF)
{
LL m=ceil(sqrt(c));// 注意要向上取整
mp.clear();
if(a%c==)
{
printf("no solution\n");
continue;
}
// 费马小定理的有解条件
LL ans;//储存每一次枚举的结果 b* a^j
for(LL j=;j<=m;j++) // a^(i*m) = b * a^j
{
if(j==)
{
ans=b%c;
mp[ans]=j;// 处理 a^0 = 1
continue;
}
ans=(ans*a)%c;// a^j
mp[ans]=j;// 储存每一次枚举的结果
}
LL t=fastpow(a,m,c);
ans=;//a ^(i*m)
LL flag=;
for(LL i=;i<=m;i++)
{
ans=(ans*t)%c;
if(mp[ans])
{
LL out=i*m-mp[ans];// x= i*m-j
printf("%lld\n",(out%c+c)%c);
flag=;
break;
} }
if(!flag)
printf("no solution\n");
} return ;
}

Discrete Logging的更多相关文章

  1. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

  2. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  3. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  4. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  5. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  6. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  7. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

  8. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  9. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

随机推荐

  1. Javascript调试命令

    Javascript调试命令——你只会Console.log() ? Console 对象提供对浏览器控制台的接入(如:Firefox 的 Web Console).不同浏览器上它的工作方式是不一样的 ...

  2. Elasticsearch - 搜索类型与搜索位置

    一.搜索类型: Elasticsearch同意用户选择其所希望的处理查询的方式. 由于存在一些不同的情形,对其使用不同的搜索类型才是合适的.为了控制查询的运行方式,我们能够在请求中使用search_t ...

  3. Android调用本地WebService

    package com.example.testinvokewebservice; import org.ksoap2.SoapEnvelope; import org.ksoap2.serializ ...

  4. Django的缓存,序列化,ORM操作的性能

    1,缓存:把数据先保存在某个地方,下次再读取的时候不用再去原位置读取

  5. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

  6. accp

    ACCP(Aptech Certified Computer Professional)是印度最早从事IT职业教育的Aptech计算机教育公司推出的培养软件程序设计人员的课程体系,由北大青鸟集团于20 ...

  7. YTU 2852: 二分查找

    2852: 二分查找 时间限制: 1 Sec  内存限制: 128 MB 提交: 215  解决: 79 题目描述 输入不多于20个升序排列的整数,以及一个待查找的数key,输出key在序列中的位置( ...

  8. js的location对象

    js的location对象 location基础知识 BOM(浏览器对象模型)中最有用的对象之一就是location,它是window对象和document对象的属性.location对象表示载入窗口 ...

  9. aapt的常用命令

    1. 列出apk包的内容 aapt l[ist] [-v] [-a] file.{zip,jar,apk} -v 以table形式列出来 -a 详细列出内容 例如:aapt l <你的apk文件 ...

  10. spring-boot快速搭建解析

    创建方式: 1.在File菜单里面选择 New > Project,然后选择Spring Initializr: 2.使用maven直接构建,添加依赖. 1 2 3 4 pom.xml:Mave ...