【题目链接】

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. Laravel5.1学习笔记3 HTTP中间件

    HTTP 中间件 简介 建立中间件 注册中间件 可终止中间件 简介 HTTP 中间件提供一个方便的机制来过滤进入应用程序的 HTTP 请求,例如,Laravel 默认包含了一个中间件来检验用户身份验证 ...

  2. 如何用js代码实现图片切换效果

    通过点击按钮,实现图片的隐藏与显现,切换. 实现代码:<style> .a{ width: 300px; height: 300px; border: 1px solid black; } ...

  3. 如何写出高性能SQL语句(文章摘自web开发者)

    (声明:本文内容摘自web开发者,仅供收藏学习之用,如有侵权请作者联系博主,博主将在第一时间删除) 原文地址:http://www.admin10000.com/document/484.html 1 ...

  4. C++编译错误fatal error C1004: 发现意外的文件尾

    出现这种情况就是类或者结构体的定义后面没有加“;”导致的. 而且这种问题好难排查.

  5. selenium选错弹出层的下拉框

    要先选中这个弹出层的form元素,再找下拉框 public void downSelectBox(){ driver.get("https://www.imooc.com/user/setp ...

  6. 实现Android-JNI本地C++调试

    1.       原文链接:NDK单步调试方法 如有问题或者版权要求,请拜访原作者或者通知本人. 最近为了性能需求,开始搞JNI,白手起搞真心不容易.中间差点崩溃了好几次,最终总算得到一点心得. JN ...

  7. ASP.NET MVC5 网站开发实践(一)

    一.开发环境 1.开发环境: Visual Studio 2013 2.数据库:Sql Server 2012 3.代码管理:TFS(微软免费提供的) 说明:VS2013与vs2012感觉变化不大,我 ...

  8. jquery Contains 实现查询

    var filter = $(this).val(); var filterResult = $(this).find('h2:Contains(' + filter + ')'); if (filt ...

  9. (转)基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用

    http://www.cnblogs.com/wuhuacong/p/4761637.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JST ...

  10. Apex语言(七)集合

    1.集合 集合是可以存储多个记录数的变量类型. List列表集合可以包含任何数量的数据,与数组类似. Set列表集合包含多个无序的唯一记录数,集合不能具有重复记录,与列表类似. Map地图是一个键值对 ...