【poj2417】 Discrete Logging
http://poj.org/problem?id=2417 (题目链接)
题意
求解$${A^X≡B~(mod~P)}$$
Solution
BSGS。
细节
map TLE飞,只好写了hash挂链。。从TLE到110MS→_→
代码
// poj2417
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf (1ll<<29)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std; const int M=1000007;
struct Hash {int w,id,next;}h[1000010];
int head[M],cnt; void insert(LL x,int I) {
int id=x%M;
h[++cnt]=(Hash){x,I,head[id]};head[id]=cnt;
}
int find(LL x) {
int id=x%M;
if (!head[id]) return -1;
for (int i=head[id];i;i=h[i].next) if (h[i].w==x) return h[i].id;
return -1;
}
LL power(LL a,LL b,LL p) {
LL res=1;
while (b) {
if (b&1) (res*=a)%=p;
b>>=1;(a*=a)%=p;
}
return res;
}
LL BSGS(LL a,LL b,LL p) {
int m=sqrt(p);
memset(head,0,sizeof(head));
insert(1,0);
LL inv=power(a,p-1-m,p),e=1;
for (int i=1;i<=m;i++) {
e=e*a%p;
if (find(e)==-1) insert(e,i);
}
for (int i=0;i*m<p;i++) {
int x=find(b);
if (x!=-1) return x+i*m;
b=b*inv%p;
}
return -1;
}
int main() {
LL p,a,b;
while (scanf("%lld%lld%lld",&p,&a,&b)!=EOF) {
LL ans=BSGS(a,b,p);
if (ans==-1) puts("no solution");
else printf("%lld\n",ans);
}
return 0;
}
【poj2417】 Discrete Logging的更多相关文章
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- 【bzoj3239】Discrete Logging
[吐槽] 这题和[bzoj]2480一毛一样. 就是输入顺序和输出变了一下. 传送门:http://www.cnblogs.com/chty/p/6043707.html
- 【BZOJ】【3239】Discrete Logging
BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...
- 【POJ 2417】 Discrete Logging
[题目链接] http://poj.org/problem?id=2417 [算法] Baby-Step,Giant-Step算法 [代码] #include <algorithm> #i ...
- 【POJ2417】baby step giant step
最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...
- 【译文】Java Logging
本文讲Java内置的java.util.logging软件包中的 api.主要解释怎样使用该api添加logging到你的application中,怎样加配置它等.但是本文不谈你应该把什么东西写到日志 ...
- 函数和常用模块【day06】:logging模块(八)
本节内容 1.简述 2.简单用法 3.复杂日志输出 4.handler详解 5.控制台和文件日志共同输出 一.简述 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误 ...
- Python开发【模块】:logging日志
logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
随机推荐
- vijos P1448 校门外的树
描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的--如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:\(K=1\),读入\(l, ...
- 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验
Web应用中,有时会遇到一些耗时很长的操作(比如:在后台生成100张报表再呈现,或 从ftp下载若干文件,综合处理后再返回给页面下载),用户在网页上点完按钮后,通常会遇到二个问题:页面超时.看不到处理 ...
- logback + slf4j + jboss + spring mvc
logback.log4j.log4j2 全是以同一个人为首的团伙搞出来的(日志专业户!),这几个各有所长,log4j性能相对最差,log4j2性能不错,但是目前跟mybatis有些犯冲(log4j2 ...
- 无法连接windows虚拟机oracle的解决办法
在mac机上玩基于oracle db的开发真心不容易,oracle公司死活不出oracle express edition for mac OS,曾经发布过的oracle 10 for mac下载地址 ...
- MD5算法的C语言实现
1 #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h ...
- [译]用AngularJS构建大型ASP.NET单页应用(三)
原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single A ...
- gravity、layout_gravity及orientation
gravity.layout_gravity及orientation 最近在弄一个简单的界面:横向,添加一张准备好的背景图,在界面右边居中放置一个按钮.实现过程中发现对布局的主要属性没有想象中地那么熟 ...
- [HDOJ5442]Favorite Donut(最大表示法)
嗯……就是最小表示法改一下…… 这题就是把S串当作两个判断同构的串,然后就搞出最大的表示了 然后在反向再做一次 O(n)求最大表示,O(n)判断正反谁大
- xshell4|5远程连接工具
志同道合,方能谈天说地! 对比其他的工具,对于功能来说xshell是比较厉害的.有能力的可以支持正版! Xshell4 链接: http://pan.baidu.com/s/1jHAgboa 密码: ...
- 常用数据库高可用和分区解决方案(1) — MySQL篇
在本文中我们将会讨论MySQL.Oracle.MongoDB.Redis以及Oceanbase数据库,大家可能会奇怪为什么看不到有名关系型数据库MSSQL.DB2或者有名NoSQL数据库Hbase.L ...