BSGS算法(大步小步算法)

计算\(y^x ≡ z \ mod\ p\) 中 \(x\) 的解。
这个模板是最小化了\(x\) , 无解输出\(No \ Solution!\)

map<ll,ll>data;
ll m,res,t,ans; bool flag;
Pow(int x,int y,int p){return (x^y)%p;}

IL void BSGS(RG ll y , RG ll z,RG ll p){
    y %=p;
    flag = false;
    if(!y && !z){puts("1");return;}
    if(!y && z){puts("No Solution!"); return;}
    data.clear();

    //把z*(y^j)的值存下来
    m = sqrt(p)+1;          //一定要+1!!!!
    for(RG ll j = 0; j <= m; j ++){
        if(!j){res = z % p; data[res] = j; continue;}
        res = res*y%p; if(!data[res])data[res] = j;
    }       

     //计算y^(im)的值,与之前的值比对
    t = Pow(y,m,p); res = 1;
    for(RG ll i = 1; i <= m; i ++ ){
        res = res*t%p;
        if(data[res]){
            ans = i*m - data[res];
            ans = (ans%p+p)%p; cout<<ans<<endl;
            flag = true; return;
        }
    }
    if(!flag)puts("No Solution!");
}

原文地址:https://www.cnblogs.com/Guess2/p/8358982.html

时间: 2024-08-26 18:04:13

BSGS算法(大步小步算法)的相关文章

BSGS算法学习小记(大步小步算法)

简介 先看一个式子xy≡z(modp),z是质数 现在只知道x和z,要求y. 大步小步算法(BSGS,Baby Steps Giant Steps)就是解决这个问题. 算法流程 暴搜的枚举范围 根据费马小定理:xz?1≡1. 如果y已经枚举到了z-1了,继续枚举的话就会产生循环. 所以,在暴搜中y的枚举范围就是0--z-1. 如何优化暴搜 我们想一想可不可以用分块来解决枚举的y. 把y分成p?1????√分别枚举行不行? 设m=p?1????√,y=a?m+b,这样枚举a和b就相当于分块枚举了.

【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线代算法我也不想多说,还是自行百度吧 需要注意的是矩阵没有交换律,所以在计算$B\cdot A^{-m}$的时候不要把顺序搞混 代码: 1 #include <cstring> 2 #include <cstdio> 3 #include <algorithm> 4 #inc

uva 11916 - Emoogle Grid(大步小步算法)

题目连接:uva 11916 - Emoogle Grid 题目大意:有一问题,在M行N列的网格上涂K种颜色,其中有B个格子不用涂色,其它每个格子涂一种颜色,同一列的上下两个相邻的格子不能涂相同的颜色.给出M,N,K和B个格子的位置,求出总方案数模掉1e8+7的结果R.现在已知R,求最小的M. 解题思路:有确定不用涂色格子的区域作为不变部分,总数通过计算为tmp,外加可变部分的第一行,方案数为cnt,可变部分除第一行外,每加一行都将总数乘以(K?1)N,既有 cnt?PM=Rmod(1e8+7)

大步小步算法 模板bzoj 计算器

你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数. code: // #include<bits/stdc++.h> using namespace std; #define ll long long ll T,p; ll ksm(ll a,ll b,ll c) { ll ans=1; while(b

UVA 11916 Emoogle Grid 离散对数 大步小步算法

LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <math.h> #include <set> #include <map> #include <queue> #include <algorithm> #include <string.h> #include <string> using

Shank的大步小步算法(Shank‘s Baby-Step-Giant-Step Algorithm)

1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <map> 5 typedef long long LL; 6 int mul_mod(int a,int b,int n){ // a.b都小于n 7 return a * b % n; 8 } 9 int pow_mod(int a,int p,int n){ 10 if(p == 0) return 1; 11

bzoj3929 Discrete Logging 大步小步算法

#include<cstdio> #include<algorithm> #include<cmath> #include<map> using namespace std; typedef long long ll; ll p,a,b; ll ksm(ll x,ll y) { ll res=1; while(y) { if(y&1)res=res*x%p; y>>=1; x=x*x%p; } return res; } map<l

最短路算法 :Bellman-ford算法 &amp; Dijkstra算法 &amp; floyd算法 &amp; SPFA算法 详解

 本人QQ :2319411771   邮箱 : [email protected] 若您发现本文有什么错误,请联系我,我会及时改正的,谢谢您的合作! 本文为原创文章,转载请注明出处 本文链接   :http://www.cnblogs.com/Yan-C/p/3916281.html . 很早就想写一下最短路的总结了,但是一直懒,就没有写,这几天又在看最短路,岁没什么长进,但还是加深了点理解. 于是就想写一个大点的总结,要写一个全的. 在本文中因为邻接表在比赛中不如前向星好写,而且前向星效率并

条件随机场(CRF) - 4 - 学习方法和预测算法(维特比算法)

声明: 1,本篇为个人对<2012.李航.统计学习方法.pdf>的学习总结,不得用作商用,欢迎转载,但请注明出处(即:本帖地址). 2,由于本人在学习初始时有很多数学知识都已忘记,所以为了弄懂其中的内容查阅了很多资料,所以里面应该会有引用其他帖子的小部分内容,如果原作者看到可以私信我,我会将您的帖子的地址付到下面. 3,如果有内容错误或不准确欢迎大家指正. 4,如果能帮到你,那真是太好了. 学习方法 条件随机场模型实际上是定义在时序数据上的对数线性模型,其学习方法包括极大似然估计和正则化的极大