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. SpringMVC的Controller中使用线程安全的初始化

    因为SpringMVC的Controller默认是单例, 在这种情况下, Controller中使用的私有变量必须也是单例, 例如各种service, 否则会有多线程访问数据互相修改的问题. 对于需要 ...

  2. Java synchronized

    1. 将synchronized加在方法上, 即可实现对此方法的同步 public synchronized void deposit(float amt) { float tmp = amount; ...

  3. Java手动添加SSL证书

    出现错误为 SSLHandshakeException - unable to find valid certification path to requested target 在服务器上找到对应的 ...

  4. 判断百度某一经纬度的地图颜色值python

    from PIL import Image import MySQLdb import os import urllib import time from multiprocessing.dummy ...

  5. 05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能

    这篇文章讲的是在线编辑器功能,之前的部门模块中,增加部门的功能jsp页面起先是这么做的.

  6. NOI2018准备 Day11

    今天7点半到9点我都不知道自己在干啥, 一共A了3道题,2道钻石,1道大师. 下午调一道线段树3个小时没调出来,一个单调栈2小时没搞出来...... 学了个算法:求极大子矩阵. 昨天定的目标是学指针, ...

  7. jquery 实现邮箱输入自动提示功能

    邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作为账号名 为了提高用户的体验,很多网站都会实现邮箱输入的自动提示功能,所有自己也实现了一个,先看下效果吧,觉得效果还行的就拿去 ...

  8. EF分页中的陷阱

    (一) 前言                                                                   EF使用非常简单,但是如果使用不当就会误入EF陷阱中. ...

  9. 谈谈数据监听observable的实现

    一.概述 数据监听实现上就是当数据变化时会通知我们的监听器去更新所有的订阅处理,如: var vm = new Observer({a:{b:{x:1,y:2}}}); vm.watch('a.b.x ...

  10. 【开源】分享一个前后端分离方案-前端angularjs+requirejs+dhtmlx 后端asp.net webapi

    一.前言 半年前左右折腾了一个前后端分离的架子,这几天才想起来翻出来分享给大家.关于前后端分离这个话题大家也谈了很久了,希望我这个实践能对大家有点点帮助,演示和源码都贴在后面. 二.技术架构 这两年a ...