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<ll,ll> h;
ll solve(ll a,ll b,ll p)
//设x=im-c,则x+c=im
//a^x * a^c =a^im
{
ll m=ceil(sqrt(p));//向上取整
h.clear();
for(ll i=0; i<m; i++)
//求出b*a,b*a^2,b*a^3...即等式左边那一段,放到map中
{
if(!h.count(b))
h[b]=i;
b=b*a%p;
}
ll now=1,base=ksm(a,m);
for(ll i=1; i<=m+1; i++)
//枚举i,算a^im的结果,然后到hash表中去找
{
now=now*base%p;
if(h.count(now))return i*m-h[now];
}
return -1;
}
int main()
{
while(scanf("%lld%lld%lld",&p,&a,&b)!=EOF)
{
ll ans=solve(a,b,p);
if(ans==-1)printf("no solution\n");
else printf("%lld\n",ans);
}
return 0;
} #include<bits/stdc++.h>
using namespace std;
pair<long long, long long> x[1000001];
long long p, b, n;
long long pow(long long x, long long y, long long p)
{
long long ans = 1;
while(y)
{
if(y & 1) ans *= x, ans %= p;
y >>= 1;
x *= x;
x %= p;
}
return ans;
} long long find(long long l, long long r, long long v)
{
while(l <= r)
{
long long mid = (l + r) / 2;
if(x[mid].first < v)
{
l = mid + 1;
}
else{
r= mid-1;
}
}
return (x[l].first == v) ? l : -1;
} int main()
{
cin >> p >> b >> n;
//b^x =N % p
long long sq = (double)(sqrt(p - 2) + 0.5); x[0].first = 1;
x[0].second = 0;
for(long long i = 1; i < sq; ++i)
//求出b的若干次方Mod P的结果,放到一个表里
{
x[i].first = x[i - 1].first * b;
x[i].first %= p;
x[i].second = i;
}
sort(x, x + sq - 1);//排序
for(long long i = 0; i <= sq; ++i)
{
long long v = n * pow(b, p - 1 - i * sq, p);
v %= p;
long long j = find(0, sq - 1, v);//到表里去找
if(j != -1)
{
cout << i * sq + x[j].second << endl;
return 0;
}
}
cout << "no solution" << endl;
return 0;
}
bzoj3929 Discrete Logging 大步小步算法的更多相关文章
- 离散对数&&大步小步算法及扩展
bsgs algorithm ax≡b(mod n) 大步小步算法,这个算法有一定的局限性,只有当gcd(a,m)=1时才可以用 原理 此处讨论n为素数的时候. ax≡b(mod n)(n为素数) 由 ...
- 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...
- [模板]大步小步算法——BSGS算法
大步小步算法用于解决:已知A, B, C,求X使得 A^x = B (mod C) 成立. 我们令x = im - j | m = ceil(sqrt(C)), i = [1, m], j = [0, ...
- 离散对数及其拓展 大步小步算法 BSGS
离散对数及其拓展 离散对数是在群Zp∗Z_{p}^{*}Zp∗而言的,其中ppp是素数.即在在群Zp∗Z_{p}^{*}Zp∗内,aaa是生成元,求关于xxx的方程ax=ba^x=bax=b的解, ...
- 大步小步算法模板题, poj2417
大步小步模板 (hash稍微有一点麻烦, poj不支持C++11略坑) #include <iostream> #include <vector> #include <c ...
- BSGS-Junior·大步小步算法
本文原载于:http://www.orchidany.cf/2019/02/06/BSGS-junior/#more \(\rm{0x01}\) \(\mathcal{Preface}\) \(\rm ...
- [BSGS]大步小步算法
问题 BSGS被用于求解离散对数,即同余方程: \[ A^x\equiv B\pmod{P} \] 求\(x\)的最小非负整数解. 保证\(A\perp P\)(互质). 分析 首先,我们根据费马小定 ...
- UVA 11916 Emoogle Grid 离散对数 大步小步算法
LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...
- BSGS算法(大步小步算法)
计算\(y^x ≡ z \ mod\ p\) 中 \(x\) 的解. 这个模板是最小化了\(x\) , 无解输出\(No \ Solution!\) map<ll,ll>data; ll ...
随机推荐
- 问题:关于2.3 jmu-Java-02基本语法-03-身份证排序 (9 分)
输出未能排序 import java.util.Scanner; import java.util.Arrays; public class Main { pu ...
- python 操作符**与*的用法
- 【leetcode】Valid Palindrome II
很久没有做题了,今天写个简单难度的练练手感. Given a non-empty string s, you may delete at most one character. Judge wheth ...
- Java 数组复制之clone方法
一.源码 public class Test1 { public static void main(String[] args) { // Student[] arrs = new Student[] ...
- 22.从上往下打印二叉树(python)
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom( ...
- break continue exit return 的区别
[root@localhost day1]# cat ss.sh #!/bin/bash for ((i=0;i<5;i++)) do if [ $i -eq 3 ] then break #c ...
- spring mvc @Valid 数据验证
//对书的单价校验不能是空,价格在20-100之间 @DecimalMax(value = "100", message = "价格不超过100元") ...
- Linux基础教程 linux下查询history操作时间的方法
要在linux操作系统中查看history记录的操作时间,可以按如下步骤实现: 学习linux 1,修改/etc/profile文件,在末尾添加:exporthisttimeformat=”%f %t ...
- Android图片优化指南
图片作为内存消耗大户,一直是开发人员尝试优化的重点对象.Bitmap的内存从3.0以前的位于native,到后来改成jvm,再到8.0又改回到native.fresco花费很多精力在5.0系统之前把B ...
- UVa 725 Division (枚举)
题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...