【题目链接】

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. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  2. Oracle数据库基础知识总结(一)

    数据库名.实例名.数据库域名.全局数据库名.服务名,这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水. 我们现在就来把它们弄个明白. 一.数据库名 什么是数据 ...

  3. Linux 之常用操作指令详解

    1. 查看当做操作目录位置 > pwd 2. 查看(当前)目录里边的文件内容 > ls //list > ls -l 或ll //显示文件的详细信息 > ls -al //al ...

  4. webstorm for mac

    mac上使用webstrom,破解的方法 参见博客:Webstorm 破解2016.1 for mac 上面的说明有点问题,博主的写的有点问题.应该是1.先打开到注册页面:2.再关闭webstrom; ...

  5. TCP/IP数据包结构详解

    一般来说,网络编程我们只需要调用一些封装好的函数或者组件就能完成大部分的工作,但是一些特殊的情况下,就需要深入的理解网络数据包的结构,以及协议分析.如:网络监控,故障排查等…… IP包是不安全的,但是 ...

  6. C#第七节课

    for嵌套 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...

  7. 自定义数据类型使用QVariant转换的方法

    QVariant类型的放入和取出必须是相对应的,你放入一个int就必须按int取出,不能用toString(), Qt不会帮你自动转换. 数据核心无非就是一个 union,和一个标记类型的type:传 ...

  8. linux系统中给mysql配置环境变量

    安装过程就不写了,记得安装的路径就行,接下来要用到. 修改配置文件 vim /etc/profile 设置环境变量 写一个MYSQL_HOME,值为“mysql的安装路径” 在PATH后面加上$MYS ...

  9. android 权限问题分析

    Android系统是运行在Linux内核上的,Android与Linux分别有自己的一套严格的安全及权限机制, 很多像我这样的新手,尤其是习惯了windows低安全限制的用户,很容易在这方面弄混淆,下 ...

  10. lucene_04_解析语法查询

    解析语法查询就是调用方法查询的原始查询 例如: 查询所有的查询器的语法为:*:*,因为lucene查询是根据term来做的,既是:key:value类型.*:*表示所有域中的所有值. api调用语法解 ...