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}\))的同余方程,即著名的 ...
随机推荐
- Tornado 用户身份验证框架
1.安全cookie机制 import tornado.web session_id = 1 class MainHandler(tornado.web.RequestHandler): def ge ...
- Android 4.4 沉浸式透明状态栏
原文链接:http://www.bkjia.com/Androidjc/913061.html 第一种方法 这里写代码片第一种方法,在代码设置: if(VERSION.SDK_INT >= VE ...
- JUnit单元测试遇到的问题及解决思路
JUnit是Java单元测试框架,我们在对开发的系统进行单元测试的时候,也遇到了如何测试多个测试用例的问题. 背景:我们的所有测试用例都保存在Excel文件中,该文件包含测试用例和预期输出.我们希望 ...
- OpenShift实战(一):OpenShift高级安装
1.1 服务器基本信息 本次安装采用一个master.5个node.3个etcd,node节点两块硬盘,60G磁盘用于docker storage,xxx改为自己的域名或主机名. 节点 功能 IP 内 ...
- mongodb 定时备份
通过centos 脚步来执行备份操作,使用crontab实现定时功能,并删除指定天数前的备份 具体操作: 1.创建Mongodb数据库备份目录 mkdir -p /home/backup/mongod ...
- python 面向对象之封装与类与对象
封装 一,引子 从封装本身的意思去理解,封装就好像是拿来一个麻袋,把小猫,小狗,小王八,小老虎一起装进麻袋,然后把麻袋封上口子.照这种逻辑看,封装='隐藏',这种理解是相当片面的 二,先看如何隐藏 在 ...
- ASP.NET Web API编程——路由
路由过程大致分为三个阶段: 1)请求URI匹配已存在路由模板 2)选择控制器 3)选择操作 1匹配已存在的路由模板 路由模板 在WebApiConfig.Register方法中定义路由,例如模板默认生 ...
- Mysql编译安装详解
wget http://mirrors.cnnic.cn/apache/httpd/mysql-5.5.20.tar.gz root@Mysql-server ~]# yum install -y c ...
- Spring知识点回顾(06)Profile 和 条件注解 @Conditional
1.设定环境中的active profiles 如:DispatcherServerlet的init-param spring.profiles.active=production spring.pr ...
- MySql入门(2-1)windows下安装mysql的两种方式
一.下载mysql 1.下载解压MySQL 登录oracle主页,需要用户名和口令: lshengqi@netease.com/1wsx**** 下载路径:: https://dev.mysql.co ...