#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
struct Node{
int idx;
LL val;
Node(int cidx=, LL cval=){
idx=cidx;
val=cval;
}
bool operator <(const Node &rhs)const{
return val<rhs.val||( val == rhs.val && idx<rhs.idx );
}
}P[];
LL pow_mod(LL a, LL n, LL mod)
{
LL ans=;
while(n)
{
if(n&)ans=(ans*a)%mod;
n>>=;
a=(a*a)%mod;
}
return ans;
}
int bitsearch(LL d,int n)
{
int L=,R=n-;
while(L<=R)
{
int mid=(L+R)>>;
if(P[mid].val==d) return P[mid].idx;
if(P[mid].val<d) L=mid+;
else R=mid-;
}
return -;
}
LL log_mod(LL a, LL b, LL n)
{
LL m,v,e=;
m=(sqrt(n+0.5))+;
v=pow_mod(a,n-m-,n);
P[]=Node(,);
for(int i=; i<m; i++)
{
e=(e*a)%n;
P[i]=Node(i,e);
}
sort(P,P+m);
int cnt=;
for(int i=; i<m; i++)
if(P[i].val!=P[cnt-].val) P[cnt++]=P[i];
for(int i=; i<m; i++)
{
int loc=bitsearch(b,cnt);
if(loc!=-){
return i*m+loc;
}
b=(b*v)%n;
} return -;
}
int main()
{
LL P,B,N;
while(scanf("%I64d%I64d%I64d",&P,&B,&N)==)
{
LL d =log_mod(B,N,P);
if(d==-)puts("no solution");
else printf("%I64d\n",d);
}
return ;
}

poj2417 Baby-StepGiant-StepAlgorithm a^x=b%P的更多相关文章

  1. [原博客] BZOJ 2242 [SDOI2011] 计算器

    题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...

  2. 【POJ2417】baby step giant step

    最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...

  3. 【poj2417】 Discrete Logging

    http://poj.org/problem?id=2417 (题目链接) 题意 求解$${A^X≡B~(mod~P)}$$ Solution BSGS. 细节 map TLE飞,只好写了hash挂链 ...

  4. nodejs+express+jade给我baby做个小相册

    去年年底迎来了my little star.从此人生多了一个最重要的牵挂.生了宝宝全家人都太忙了.最近宝宝稍微大点了,终于有空可以研究下技术了.这是14年第一帖.废话不多了.开始吧 1.安装NTVS ...

  5. 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 ...

  6. 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法

    先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝  扩展Baby Step Gian ...

  7. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  8. UVA 12897 Decoding Baby Boos 暴力

    Decoding Baby Boos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  9. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  10. hdu 5612 Baby Ming and Matrix games

    Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...

随机推荐

  1. Python中的format函数

    format是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 2.单个参数可以多次输出,参数顺序 ...

  2. Java之旅_高级教程_URL处理

    摘自 :http://www.runoob.com/java/java-url-processing.html Java URL 处理 URL(Uniform Resource Locator)中文名 ...

  3. 基于sendEmail的简单zabbix邮件报警

    一.sendmail和sendEmail区别 sendmail是一款邮件服务器软件(MTA),sendEmail是命令行SMTP邮件客户端(MUA) 二.senEmail安装 下载地址:http:// ...

  4. LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  5. Http服务基础原理

    http服务相关解释 http :  Hyper Text Transfer Protocol,  80/tcp 超文本传输协议,基于tcp传输协议的80端口传输 html: Hyper Text M ...

  6. VS2017使用Git进行源代码管理

    步骤一:将解决方案添加到源代码管理 步骤二:进入团队资源管理器 双击存储库项目进入Git操作页面. 步骤三:同步本地代码到远程仓库 选择同步功能 步骤四:发布代码到Git 点击之后输入你要发布的git ...

  7. 虚函数后面的const=0

    const 和 =0要分开理解. 成员函数后面用 const 修饰,const表示this是一个指向常量的指针,即对象成为一个常量,即它的成员不能够变化.(默认情况下,this的类型是指向类类型非常量 ...

  8. Centos7下ups监控apcupsd的使用

    什么是UPS UPS-Uninterrupted Power System:利用电池化学能作为后备能量,在市电断电等电网故障时,不间断地为用户设备提供(交流)电能的一种能量转换装置. UPS的主要功能 ...

  9. linux下禁止root远程登录和添加新用户

    https://www.cnblogs.com/jianz/p/7979250.html 一.添加和root权限一样的用户 1. adduser admin passwd  admin (修改密码) ...

  10. Go包管理工具Vendor使用

    一.Go包管理工具Vendor 一.使用步骤 1.首先,从go get -u github.com/kardianos/govendor下载govendor工具到本地. 2.govendor使用时,必 ...