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的更多相关文章

  1. 【BZOJ3239】Discrete Logging BSGS

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

  2. 【bzoj3239】Discrete Logging

    [吐槽] 这题和[bzoj]2480一毛一样. 就是输入顺序和输出变了一下. 传送门:http://www.cnblogs.com/chty/p/6043707.html

  3. 【BZOJ】【3239】Discrete Logging

    BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...

  4. 【POJ 2417】 Discrete Logging

    [题目链接] http://poj.org/problem?id=2417 [算法] Baby-Step,Giant-Step算法 [代码] #include <algorithm> #i ...

  5. 【POJ2417】baby step giant step

    最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...

  6. 【译文】Java Logging

    本文讲Java内置的java.util.logging软件包中的 api.主要解释怎样使用该api添加logging到你的application中,怎样加配置它等.但是本文不谈你应该把什么东西写到日志 ...

  7. 函数和常用模块【day06】:logging模块(八)

    本节内容 1.简述 2.简单用法 3.复杂日志输出 4.handler详解 5.控制台和文件日志共同输出 一.简述 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误 ...

  8. Python开发【模块】:logging日志

    logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...

  9. POJ2417 Discrete Logging【BSGS】

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

随机推荐

  1. php配置rewrite模块

    转 (1)    启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_r ...

  2. npm淘宝镜像

    前端开发会用到npm的包,但是国外的速度有时候很慢,幸运的是,淘宝做了镜像,一起来看看吧. https://npm.taobao.org/

  3. 布局 - layout

    示例 <div id="cc" class="easyui-layout" style="width:600px;height:400px;&q ...

  4. CSS3 Media Queries 片段

    CSS3 Media Queries片段 在这里主要分成三类:移动端.PC端以及一些常见的响应式框架的Media Queries片段. 移动端Media Queries片段 iPhone5 @medi ...

  5. tomcat 新手上路

    前提:本机先安装好JDK,保证常规java环境已经具备 1.下载Tomcat 7.0现在官网上好象已经没有安装程序版了,只有免解压zip版本(现在最新的版本是7.0.42) 下载地址 http://t ...

  6. TinyFrame尾篇:整合Spring AOP实现用户认证

    创建Manager用户验证表 这一篇主要讲解使用AOP对用户操作进行验证,如果通过验证,则继续执行,反之,则不能执行.其思想和上一篇完全一致. 由于需要用到用户认证,所以我们新建一个Manager实体 ...

  7. OkHttp使用教程

    Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient.关于HttpURLConnection和HttpClient的选择>>官方博客尽管Go ...

  8. Beta版本项目展示要求

    项目评审的定在1月5日上午9:00在新主楼D225进行. 在Beta阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写成博客(可以有 ...

  9. 完美演绎DevExpress XtraPrinting Library 的打印功能

    完美演绎DevExpress XtraPrinting Library 的打印功能 2010-05-14 17:40:49|  分类: 默认分类|字号 订阅     设计报告不仅费时间,而且还乏味!但 ...

  10. 如何用 fiddler 代理调试本地手机页面

    最近在做移动端的页面,但是移动端的调试很蛋疼.虽然说 90% 的功能都能用 chrome 下的模拟器来模拟解决,但是剩余的 10% 却只能在真机上调试.比如说一两个像素的误差,比如说只有真机上才能重现 ...