【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3239

【题目大意】

  计算满足 Y^x ≡ Z ( mod P) 的最小非负整数

【题解】

  BSGS裸题。

【代码】

#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std::tr1;
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
int phi(int n){
int t=1,i;
for(i=2;i*i<=n;i++)if(n%i==0)for(n/=i,t*=i-1;n%i==0;n/=i,t*=i);
if(n>1)t*=n-1;
return t;
}
int pow(ll a,int b,int m){ll t=1;for(;b;b>>=1,a=a*a%m)if(b&1)t=t*a%m;return t;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int exgcd(int a,int b,int&x,int&y){
if(!b)return x=1,y=0,a;
int d=exgcd(b,a%b,x,y),t=x;
return x=y,y=t-a/b*y,d;
}
int bsgs(int a,int r,int m){
if(r>=m)return -1;
int i,g,x,c=0,at=int(2+sqrt(m));
for(i=0,x=1%m;i<50;i++,x=ll(x)*a%m)if(x==r)return i;
for(g=x=1;__gcd(int(ll(x)*a%m),m)!=g;c++)g=__gcd(x=ll(x)*a%m,m);
if(r%g)return -1;
if(x==r)return c;
unordered_map<int,int>u;
g=phi(m/g),u[x]=0;g=pow(a,g-at%g,m);
for(i=1;i<at;i++){
u.insert(P(x=ll(x)*a%m,i));
if(x==r)return c+i;
}
for(i=1;i<at;i++){
unordered_map<int,int>::iterator t=u.find(r=ll(r)*g%m);
if(t!=u.end())return c+i*at+t->second;
}return -1;
}
void solve(int y,int z,int p){
y%=p; z%=p;
int t=bsgs(y,z,p);
if(t==-1){puts("no solution");return;}
else printf("%d\n",t);
}
int main(){
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c))solve(b,c,a);
return 0;
}

BZOJ 3239 Discrete Logging(BSGS)的更多相关文章

  1. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  2. bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】

    都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...

  3. POJ-2417-Discrete Logging(BSGS)

    Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N &l ...

  4. BZOJ 3239: Discrete Logging [BGSG]

    裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...

  5. 【BZOJ5296】【CQOI2018】破解D-H协议(BSGS)

    [BZOJ5296][CQOI2018]破解D-H协议(BSGS) 题面 BZOJ 洛谷 Description Diffie-Hellman密钥交换协议是一种简单有效的密钥交换方法.它可以让通讯双方 ...

  6. LTE Manual ——Logging(翻译)

    LTE Manual ——Logging(翻译) (本文为个人学习笔记,如有不当的地方,欢迎指正!) 9 Logging   ns-3 日志功能可以用于监测或调试仿真程序的进展.日志输出可以通过 ma ...

  7. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

  8. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  9. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

随机推荐

  1. CSS哪些样式属性可以继承

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  2. this可以通过call改变的测试

  3. 【Sqlite3】sqlite_sequence表(转)

    sqlite_sequence表也是SQLite的系统表.该表用来保存其他表的RowID的最大值.数据库被创建时,sqlite_sequence表会被自动创建.该表包括两列.第一列为name,用来存储 ...

  4. java基础 运算符

    算数运算符 加号:在操作数值.字符.字符串时其结果是不同的,当两个字符相加得到的是ASCII码表值, 当两个字符串相加时表示将两个字符串连接在一起,从而组成新的字符串. 除号:整数在使用除号操作时,得 ...

  5. device tree --- #interrupt-cells property

    device tree source Example1 interrupt-controller@e000e100 { ... ... #interrupt-cells = <0x1>; ...

  6. (十七)vmware无法将网络更改为桥接状态

    故障现象,导致虚拟机无法正常上网 设备管理器中的驱动设备正常加载,但是注意这两个虚拟网卡是有问题的 将这两个虚拟网卡删除 只剩物理网卡了,重新启动电脑 将虚拟机里的网络设置删除 清空网卡后点击恢复默认 ...

  7. 【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法

    详细解释 参见:http://blog.csdn.net/wall_f/article/details/8248373 简要过程 HK算法可以当成是匈牙利算法的优化版,和dinic算法的思想比较类似. ...

  8. BZOJ 2460: [BeiJing2011]元素 贪心,线性基

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2460 解法:从大到小排序,依次贪心的添加到当前集合就可以了,需要动态维护线性基.用拟阵证明 ...

  9. 网站服务器压力Web性能测试(3):http_load:测试web服务器的吞吐量与负载

    1.http_load是国外一个博主写的一个基于Linux的性能测工具,小巧轻便,解压缩后不到100k,下载安装方法: wget https://acme.com/software/http_load ...

  10. C/C++——[01] 程序的基本框架

    我们以HelloWorld这个简单程序为例,该程序在终端打印一行文本: Hello World! 代码如下: #include <stdio.h> int main(){ printf(& ...