【题目链接】

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. JavaScript(基于react+dva)

    变量声明 const 和 let:分别表示常量和变量 模板字符串 const user = 'world'; console.log(`hello ${user}`); // hello world ...

  2. 如何使用Visual Studio调试C#程序

    当代码不能正常运行时,可以通过调试定位错误.常用的程序调试操作包括设置断点.开始.中断和停止程序的执行.单步执行程序以及使程序运行到指定的位置.下面将对这几种常用的程序调试操作进行详细地介绍. 1.断 ...

  3. SQL server基本语法

    此处源于一个基本的SQL Server试题,基本上涵盖了SQL Server的全部基本语法,粘贴在此处,权当分享   --1.  创建TestDB数据库 create database TestDB; ...

  4. MyEclipse获取注册码

    最近刚装上MyEclipse,一直弹窗提示注册码过期,开始还能接受,到最后,每发布一个项目便弹窗提醒,顿时感觉烦了,得治理治理这个烦人的注册码,下面是一段自动生成注册名和注册码的代码,只需要直接拿来用 ...

  5. EnforceLearning-被动强化学习

    前言: 画图挺好:深度学习进阶之路-从迁移学习到强化学习 专家系统给出了知识节点和规则,使用粒度描述准确性,依靠分解粒度解决矛盾,并反馈知识和推理规则更新.专家系统与机器学习有本质区别,但从机器学习的 ...

  6. react功能实现-数组遍历渲染

    在react中如何将一个数组遍历,并且逐个渲染在页面上? 1.在jsx渲染中,如果这个变量是一个数组,则会展开这个数组的所有成员. var arr = [ <h1>Hello world! ...

  7. Python笔记(29)----进程

    目录: 一.进程 多任务的概念 创建子进程----fork[Linux] 全局变量在多个进程中不共享 多次fork() 创建进程----multiprocessing[windows] 二.线程 一. ...

  8. Solr学习记录:Getting started

    目录 Solr学习记录:Getting started 1.Solr Tutorial 2. A Quick Overview Solr学习记录:Getting started 本教程使用环境:jav ...

  9. Llinux,NFS服务搭建(文件共享)

    NFS配置文件权限参数说明(/etc/exports) 1.rw :表示可读写权限. 2.ro :表示只读权限. 3.sync :请求或写入数据时,数据同步写入到NFS Server的硬盘后才返回.( ...

  10. Beautifulsoup提取特定丁香园帖子回复

    DataWhale-Task3(Beautifulsoup爬取丁香园) 简要分析 完整代码 结果图 参考资料 简要分析 任务3:爬取丁香园论坛特定帖子,包括帖子主题,帖子介绍,回贴内容(用户名,用户头 ...