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 ...
随机推荐
- 《鸟哥的Linux私房菜-基础学习篇(第三版)》(六)
第5章 首次登陆与在线求助man page 1. 首次登陆系统 首先谈了首次登陆CentOS 5.x界面.登陆选项中的会话是能够使用不同的图形界面来操作整个Linux系统. ...
- Linux —— 压缩文件
Linux——压缩文件 为什么需要压缩文件? 文件在传输过程中,可能由于文件过大,传输所需时间过多.减少文件大小有两个明显的好处,一是可以减 少存储空间,二是通过网络传输文件时,可以减少传 ...
- Mysql Solution - Timeout error occurred trying to stop MySQL Daemon. Stopping MySQL: [FAILED] -
错误例如以下: Timeout error occurred trying to stop MySQL Daemon. Stopping mysqld: ...
- 导入项目 R.java没有
网上一搜,各种 Android tools-fix your porject或者Clean ...不好使 其实可能是由于XML布局文件有错误导致,修改掉这些错误就可以了..
- CXF Spring 使用
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- click event not triggered on bootstrap modal
I am trying to catch the click event when save changes is pushed. For some reason i can't catch the ...
- touch all contents in a folder recursively
https://superuser.com/questions/598163/powershell-touch-all-files-newer-than Powershell to use Unix ...
- I.MX6 逻辑分析仪 UART
/*********************************************************************** * I.MX6 逻辑分析仪 UART * 说明: * ...
- BZOJ_1415_[Noi2005]聪聪和可可_概率DP+bfs
BZOJ_1415_[Noi2005]聪聪和可可_概率DP+bfs Description Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2 ...
- [Usaco2018 Feb] New Barns
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5192 [算法] 维护树的直径,在树上离一个点最远的点一定是一条直径的端点. ...