【题目链接】

http://poj.org/problem?id=2417

【算法】

Baby-Step,Giant-Step算法

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
typedef long long ll;
const ll INF = 1e15;
const int MOD = 1e5 + ; ll B,N,P,ans,tot;
int head[MOD],nxt[MOD],v[MOD];
ll num[MOD]; inline ll power(ll a,ll n)
{
ll res = ,b = a;
while (n)
{
if (n & ) res = res * b % P;
b = b * b % P;
n >>= ;
}
return res;
}
inline void clear()
{
tot = ;
memset(head,,sizeof(head));
}
inline void insert(ll val,int j)
{
int h = val % MOD;
tot++;
num[tot] = val;
v[tot] = j;
nxt[tot] = head[h];
head[h] = tot;
}
inline int query(ll val)
{
int i,h = val % MOD;
for (i = head[h]; i; i = nxt[i])
{
if (num[i] == val)
return v[i];
}
return -;
}
inline ll Baby_Step_Giant_Step(ll B,ll N,ll P)
{
int i,j,t;
ll val,ans = INF;
clear();
B %= P;
t = (int)sqrt(P) + ;
for (j = ; j <= t; j++)
{
val = N * power(B,j) % P;
insert(val,j);
}
B = power(B,t);
if (B == ) return N == ? : -;
for (i = ; i <= t; i++)
{
val = power(B,i);
j = query(val);
if (j >= && i * t - j >= ) return i * t - j;
}
return -;
} int main()
{ while (scanf("%lld%lld%lld",&P,&B,&N) != EOF)
{
ans = Baby_Step_Giant_Step(B,N,P);
if (ans == -) printf("no solution\n");
else printf("%lld\n",ans);
} return ; }

【POJ 2417】 Discrete Logging的更多相关文章

  1. 【BZOJ3239】Discrete Logging BSGS

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

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. Android Studio and Gradle安装心得

    安装基于Eclipse 的ADT一段时间,感觉确实有很多功能不足,通过网上资料,决定改向AS. AS下载了最新的2.3版本,它不分64位与32位,网上说有单独版是瞎扯蛋.只要启动不同的EXE就行了. ...

  2. Linux通信之poll机制分析

    poll机制分析 韦东山 2009.12.10 所有的系统调用,基于都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数.比如系统调用open.read.write.poll,与之对应的 ...

  3. Visual Studio UI Automation 学习(二)

    今天恰好有时间,继续学习了一下UI Automation的知识.看了两篇博客,对UI Automation有了进一步的了解. https://blog.csdn.net/qq_37546891/art ...

  4. CVPR2015深度学习回顾

    原文链接:http://www.csdn.net/article/2015-08-06/2825395 本文做了少量修改,仅作转载存贮,如有疑问或版权问题,请访问原作者或告知本人. CVPR可谓计算机 ...

  5. 深圳面试一周记录——.NET(B/S)开发

    个人简单信息:2011年毕业,最高学历大专,最近一份工作在广州:有做架构设计经验,有一年的带团队(10人左右)经验:互联网和行业软件公司都待过. 为免不必要的争论,本文说地址的就不说公司行业,说公司行 ...

  6. 【Hexo】deploy出错的解决方法

    .ERROR Deployer not found: git 执行npm install hexo-deployer-git --save .$ hexo d INFO Deploying: git ...

  7. UNIX SOCKET编程简介

    1  .  Layered Model of Networking Socket  编程的层次模型如下图所示,   最上面是应用层,应用层下面的是  SOCKET API  层,再下面是传输层和网络层 ...

  8. Linux文件压缩命令笔记

    1.gzip/gunzip gzip/gunzip:主要是进行单个文件的压缩和解压缩的命令. 示例:gzip hello.txt #执行压缩hello.txt ls hello.txt.gz #查看文 ...

  9. Codeforces Round #550 (Div. 3)E. Median String

    把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...

  10. Python学习笔记之文件处理

    1.打开和关闭 要打开文件可以使用函数 open(),它位于自动导入模块 io 中.函数将文件名作为唯一不可缺少的函数,并返回一个文件对象.它还有一个名叫 mode 的参数,用于指定文件模式,其可取值 ...