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. Win10删除 6个多余文件夹

    下面附上注册表地址,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace ...

  2. Linux常用命令笔记

    ~ 我的home目录/ 系统根目录进入home目录:cd \进入跟目录:cd /Maven编译:mvn clean deploy -U -Dmaven.test.skip=true dependenc ...

  3. Hangfire项目实践

    Hangfire项目实践分享 Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget ...

  4. SHGetFileInfo函数详解

    SHGetFileInfo函数: WINSHELLAPI DWORD WINAPI SHGetFileInfo( LPCTSTR pszPath, DWORD dwFileAttributes, SH ...

  5. python 播放 wav 文件

    未使用其他库, 只是使用 pywin32 调用系统底层 API 播放 wav 文件. # Our raison d'etre - playing sounds import pywintypes im ...

  6. FastFourierTransform (FFT)

    FastFourierTransform.h #pragma once #include <stdio.h> #include <math.h> #ifndef INCLUDE ...

  7. 海王星给你好看!FineUI v4.0公测版发布暨《你找BUG我送书》活动开始(活动已结束!)

    <FineUI v4.0 你找BUG我送书>活动已结束,恭喜如下三位网友获得由 FineUI 作者亲自翻译的图书<jQuery实战 第二版>! 奋斗~ 吉吉﹑ purplebo ...

  8. Code First操作Mysql数据库

    前面博客也讲了,自己做一个网站,选用的是MVC+EF Code First+MySql+EasyUI,先说下技术选型.一.为什么选择MVC? 因为之前自己做的系统大部分是webForm,MVC的之前也 ...

  9. [BZOJ1264][AHOI2006]Match(DP+树状数组)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1264 分析: 考虑做一般的LCS的时候,更新结果的条件是a[i]==b[j]时候 于是 ...

  10. 维特比算法(Viterbi Algorithm)

      寻找最可能的隐藏状态序列(Finding most probable sequence of hidden states) 对于一个特殊的隐马尔科夫模型(HMM)及一个相应的观察序列,我们常常希望 ...