Discrete Logging
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 5865 | Accepted: 2618 |
Description
B
L
== N (mod P)
Input
Output
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
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
#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的更多相关文章
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- [POJ2417]Discrete Logging(指数级同余方程)
Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...
- BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)
我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...
- POJ 2417 Discrete Logging ( Baby step giant step )
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3696 Accepted: 1727 ...
随机推荐
- 10 逻辑完善以及bug修复
进行到这里,我们应用开发已经接近尾声,我这里基本就是应用开发的记录过程,讲解的东西很少,有问题可以在评论区讨论呦.下面进入最后调整的阶段. 预览我们的应用,会发现首页的职位列表,也会显示收藏的星星图标 ...
- ios app抓包分析
1 使用rvictl工具 这是mac下的一条命令.ios usb连mac,然后创建虚拟网络接口. 2 使用wireshark抓包 wireshark可以抓这个虚拟网络接口上的数据包.
- ASP.NET Web Pages (Razor) API Quick Reference
ASP.NET Web Pages (Razor) API Quick Reference By Tom FitzMacken|February 10, 2014 Print This page co ...
- bacth参数说明 cmd parameter
http://www.robvanderwoude.com/parameters.php Windows NT 4 introduced a set of new features for comma ...
- Eclipse 插件管理
查看已安装的插件: [help]⇒ [About Eclipse]⇒ [Installed Softwares] 1. 常用插件 maven:安装步骤如下: [help]⇒ [Install new ...
- 并不对劲的bzoj4199: [Noi2015]品酒大会
传送门-> 又称普及大会. 这题没什么好说的……后缀自动机裸题……并不对劲的人太菜了,之前照着标程逐行比对才过了这道题,前几天刚刚把这题一遍写对…… 这题的输出和某两点相同后缀的长度有关,那么把 ...
- BZOJ_5369_[Pkusc2018]最大前缀和_状压DP
BZOJ_5369_[Pkusc2018]最大前缀和_状压DP Description 小C是一个算法竞赛爱好者,有一天小C遇到了一个非常难的问题:求一个序列的最大子段和. 但是小C并不会做这个题,于 ...
- bzoj2660
dp 看了挺长时间的,这篇写的很好:http://97littleleaf11.xyz/oi/bzoj-2660/ 我们先把n按照斐波那契数列贪心分解,然后发现可以把现在组合的斐波那契数分解成两个较小 ...
- bzoj1951
CRT+LUCAS+费马小定理+拓展欧拉定理 幂指数太大了怎么办?欧拉定理,n太大了怎么办?上lucas,模数太大了怎么办?上crt.然后就好了,唯一注意的是要用拓展欧拉定理,n%phi(p)+phi ...
- (转) SQL Server中 ldf 文件过大的解决方法
原文地址:http://blog.itpub.net/35489/viewspace-616459/ 在SQL Server中经常遇到事务日志变大的情况,除了将数据库设置为“自动收缩”外,还可以使用下 ...