POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that
B L == N (mod P) Input Read several lines of input, each containing P,B,N separated by a space.
Output For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".
Sample Input 5 2 1 Sample Output 0 Hint The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states
B (P-1) == 1 (mod P) for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m B (-m) == B (P-1-m) (mod P) . Source |
模板题。
http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4
/* ***********************************************
Author :kuangbin
Created Time :2013/8/24 0:06:54
File Name :F:\2013ACM练习\专题学习\数学\Baby_step_giant_step\POJ2417.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//baby_step giant_step
// a^x = b (mod n) n为素数,a,b < n
// 求解上式 0<=x < n的解
#define MOD 76543
int hs[MOD],head[MOD],next[MOD],id[MOD],top;
void insert(int x,int y)
{
int k = x%MOD;
hs[top] = x, id[top] = y, next[top] = head[k], head[k] = top++;
}
int find(int x)
{
int k = x%MOD;
for(int i = head[k]; i != -; i = next[i])
if(hs[i] == x)
return id[i];
return -;
}
int BSGS(int a,int b,int n)
{
memset(head,-,sizeof(head));
top = ;
if(b == )return ;
int m = sqrt(n*1.0), j;
long long x = , p = ;
for(int i = ; i < m; ++i, p = p*a%n)insert(p*b%n,i);
for(long long i = m; ;i += m)
{
if( (j = find(x = x*p%n)) != - )return i-j;
if(i > n)break;
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int a,b,n;
while(scanf("%d%d%d",&n,&a,&b) == )
{
int ans = BSGS(a,b,n);
if(ans == -)printf("no solution\n");
else printf("%d\n",ans);
}
return ;
}
POJ 2417 Discrete Logging (Baby-Step Giant-Step)的更多相关文章
- POJ 2417 Discrete Logging(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
- POJ - 2417 Discrete Logging(Baby-Step Giant-Step)
d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- BSGS(Baby Steps,Giant Steps)算法详解
BSGS(Baby Steps,Giant Steps)算法详解 简介: 此算法用于求解 Ax≡B(mod C): 由费马小定理可知: x可以在O(C)的时间内求解: 在x=c之后又会循环: 而BS ...
- POJ 2417 Discrete Logging ( Baby step giant step )
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3696 Accepted: 1727 ...
- POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- POJ 2417 Discrete Logging 离散对数
链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)
http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...
随机推荐
- C#子线程中更新主线程UI-----注意项
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- LightOJ 1410 Consistent Verdicts(找规律)
题目链接:https://vjudge.net/contest/28079#problem/Q 题目大意:题目描述很长很吓人,大概的意思就是有n个坐标代表n个人的位置,每个人听力都是一样的,每人发出一 ...
- promise应用于ajax
promise应用于ajax,可以在本页打开控制台,复制代码试验 var url = 'https://www.cnblogs.com/mvc/blog/news.aspx?blogApp=dkplu ...
- linux shell 一些命令
https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash wc: https: ...
- postgresql 数据导入导出
[转] 分类: postgresql2013-06-09 10:21 2486人阅读 评论(0) 收藏 举报 一.导出数据库及具体表 1.导出数据库:方式一:pg_dump -U postgres ...
- pyqt5之QColorDialog颜色对话框最简单使用
设置窗体背景颜色 QWidget.setStyleSheet('QWidget {background-color:#88ab45}') 颜色对话框取得颜色值是十六进制. col=QCo ...
- 开源IDS系列--snorby 进程正常,但是worker无法启动 The Snorby worker is not currently running
设置页面报错: The Snorby worker is not currently running. It's imperative you start the worker immediate ...
- Webpack, VSCode 和 Babel 组件模块导入别名
很多时候我们使用别人的库,都是通过 npm install,再简单的引入,就可以使用了. 1 2 import React from 'react' import { connect } fr ...
- info.plist文件里面添加描述 -> 配置定位,相册等
<key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能访问媒体资料库</string> ...
- chomd 1+2+4
2,把目录 /tmp/sco修改为可写可读可执行 chmod 777 /tmp/sco 要修改某目录下所有的文件夹属性为可写可读可执行 chmod 777 * 把文件夹名称用*来代替就可以了 要修改/ ...