【题目链接】

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. C# 怎么把类文件如(XXX.cs)转为dll文件

    打开VS2012或2017 ,新建项目,选择  类库(.NET Framework),创建好一个项目 在建好的项目中添加需要转的类文件 然后将项目重新生成后,在项目的Debug下就可以找到对应的dll ...

  2. Android截图截取弹框AlertDialog

    1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" ...

  3. 身份认证防止重放攻击的challenge-response方法

    或者叫询问-应答机制. 基于挑战/应答(Challenge/Response)方式的身份认证系统就是每次认证时认证服务器端都给客户端发送一个不同的"挑战"字串,客户端程序收到这个& ...

  4. Leetcode刷题笔记——查找

    33.Search in Rotated Sorted Array 题目描述: 给定一个被翻转的整型升序数组nums,数组中无重复元素,如[4,5,6,7,0,1,2],和一个整数target.要求在 ...

  5. matlab学习下拉菜单

    用matlab添加listbox控件 修改string和value值,value为几就对应第几行字符串 添加button按钮,将string值改为“选择x轴参数”,字体大小为10 再添加一个按钮,将s ...

  6. day002 计算机基础之 操作系统和编程语言的分类

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp今天主要针对计算机基础中的操作系统和编程语言的分类进行了讲解. 操作系统 &nbsp ...

  7. python PIL图像处理-生成图片验证码

    生成效果如图: 代码 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random # 打开一个jpg图像文件: im = I ...

  8. eas之常用源码整理

    //查看是否有相关权限 boolean hasAllotPermission=         PermissionFactory.getRemoteInstance().hasFunctionPer ...

  9. 02-Linux命令基础-第02天(压缩包管理、服务器搭建与使用、vim)

    01-   复习 /boot 目录 引导项 八种文件类型: 文件:- 目录:d 软链接:l 字符设备文件:c 块设备文件:b 管道:p 套接字:s 未知 cp –a 保持源文件属性(如时间属性 如果不 ...

  10. ios开发——runtime

    首先,最重要的一点,学runtime能干嘛? 1.使用runtime改变变量值 2.使用runtime交换方法 3.使用runtime添加方法 4.使用runtime给分类扩展属性 学了runtime ...