poj 2417 && poj3243(Baby-Step Giant-Step)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 4624 | Accepted: 2113 |
Description
B
L
== N (mod P)
Input
Output
Sample Input
5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111
Sample Output
0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587
题意:
a^x = b(mod n) ,求解x(模板题)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std;
#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(ll i = m;;i+=m)
{
if((j = find(x = x*p % n)) != -) return i-j;
if(i > n) break;
}
return -;
} int main()
{
int p,b,n;
while(scanf("%d%d%d",&p,&b,&n) != EOF)
{
int ans = BSGS(b,n,p);
if(ans == -)
printf("no solution\n");
else
printf("%d\n",ans);
}
return ;
}
poj 2417 && poj3243(Baby-Step Giant-Step)的更多相关文章
- POJ 2417 Discrete Logging ( Baby step giant step )
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3696 Accepted: 1727 ...
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...
- 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法
先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝 扩展Baby Step Gian ...
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...
- 【POJ2417】baby step giant step
最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...
- [置顶] hdu2815 扩展Baby step,Giant step入门
题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- 『高次同余方程 Baby Step Giant Step算法』
高次同余方程 一般来说,高次同余方程分\(a^x \equiv b(mod\ p)\)和\(x^a \equiv b(mod\ p)\)两种,其中后者的难度较大,本片博客仅将介绍第一类方程的解决方法. ...
- HDU 2815 扩展baby step giant step 算法
题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...
- 【学习笔记】Baby Step Giant Step算法及其扩展
1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...
随机推荐
- 视频聊天 Demo
ESFramework Demo -- 入门Demo,简单的即时通讯系统(附源码) 是基于ESFramework实现的一个简单的文字聊天demo,现在,我们将在这个demo的基础上,使用OMCS为其增 ...
- MySql数据库的常用命令
1.连接Mysql 连接本地的mysql数据库 : mysql -u root -p (回车之后会提示输入密码) 连接远程主机的mysql数据库 : 假设远程主机的IP为:110.110.1 ...
- 从PRISM开始学WPF(二)Prism?
目录: 从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Modu ...
- BM V7000数据恢复成功案例;服务器数据恢复
IBM V7000存储是一款定位中端的存储设备,很多企业选择该服务器作为存储,最近北亚数据恢复中心接到一例V7000服务器数据恢复案例,下面将对本次数据恢复的过程和数据恢复方法进行归纳总结,希望对各位 ...
- JS中的 map, filter, some, every, forEach, for...in, for...of 用法总结
1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5]; let other = list.map((d, i) => { ...
- c# BinaryWriter 和 BinaryReader
string path = @"C:\Users\Administrator\Desktop\1.txt"; using (FileStream ws = new FileStre ...
- Python基础数据类型之int、bool、str
数据类型:int bool str list 元祖 dict 集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...
- Python之几种常用模块
模块 注意事项: 所有的模块导入都应该尽量往上写 内置模块 扩展模块 自定义模块 模块不会重复被导入 : sys.moudles 从哪儿导入模块 : sys.path import import 模块 ...
- Xshell与虚拟机不能正常连接
1.发现Xshell与虚拟机下的两个CentOS都不能正常连接,在这些系统下采用ifconfig查询发现eth0都没有ip地址,进而想到可能是虚拟机的设置出了问题,后来又想到自己之前曾经尝试过设置VM ...
- Spring(四):Spring整合Hibernate,之后整合Struts2
背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...