【题目链接】 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. 牛客网刷题(纯java题型 1~30题)

    牛客网刷题(纯java题型 1~30题) 应该是先extend,然后implement class test extends A implements B { public static void m ...

  2. 类图(Class Diagram)

    类图(Class Diagram): 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 类一般由三部分组成: 类名(Class):每个类都必须 ...

  3. 土司论坛nc反弹神器使用方法

    说明: PS:我本机是linux,因为没有服务器所以使用win7来演示.倘若你是windows可以在本机生成dll以后再放到服务器上面去执行dll即可反弹shell物理机ip:192.168.1.12 ...

  4. python基础===discover函数介绍

    discover(start_dir,pattern='test*.py',top_level_dir=None) 找到指定目录下所有测试模块,并可递归查到子目录下的测试木块,只有匹配到的文件名才会被 ...

  5. 神秘的subsys_initcall【转】

    转自:http://blog.chinaunix.net/uid-12567959-id-161015.html 在内核代码里到处都能看到这个subsys_initcall(),而它到底是干什么的呢? ...

  6. 解决sql server中批处理过程中“'CREATE/ALTER PROCEDURE 必须是查询批次中的第一个语句”

    在批处理中加字段或表或视图或存储过程是否存在的判断 -----------------------------------------line----------------------------- ...

  7. Oracle 集合

    --合并(UNION.UNION ALL) select * from empwhere ename like '%A%'unionselect * from empwhere ename like ...

  8. cvs 日常使用

    http://www.51testing.com/html/44/17144-2954.html http://www.chedong.com/tech/cvs_card.html

  9. CentOS7安装Pycharm后无法使用日常的快捷键

    1.在百度查了一圈,原来是vim在搞鬼 楼主在安装的时候,有另外添加了vim插件,所以ctrl +c之类的键都用不了,而且每次写py文件前都要先按insert键,真坑! 2.卸载pycharm里面的v ...

  10. LoadRunner:关联HTTP请求

    LoadRunner:关联HTTP请求 本例通过一个使用HTTP/HTML协议发送.获取服务器数据的vuser脚本,分析LoadRunner如何进行HTTP关联. 下面这个例子包括两个事务:上传数据到 ...