UVA10225 Discrete Logging 题解
前置知识
题意
多组询问,每次询问依次给定 \(p,a,b\),求 \(a^{x} \equiv b \pmod{p}\) 的最小非负整数解,其中 \(a,p\) 互质。
解法
BSGS 板子题,不做过多介绍。
貌似本题比 P3846 [TJOI2007] 可爱的质数/【模板】BSGS 和 BZOJ3239 Discrete Logging 数据较强,记得特判 \(b \bmod p=1\) 时的情况。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define sort stable_sort
#define endl '\n'
ll qpow(ll a,ll b,ll p)
{
ll ans=1;
while(b>0)
{
if(b&1)
{
ans=ans*a%p;
}
b>>=1;
a=a*a%p;
}
return ans;
}
ll bsgs(ll a,ll b,ll p)
{
if(1%p==b%p)
{
return 0;
}
else
{
map<ll,ll>vis;
ll k=sqrt(p)+1,i,sum;
for(i=0;i<=k-1;i++)
{
vis[b*qpow(a,i,p)%p]=i;
}
a=qpow(a,k,p);
for(i=0;i<=k;i++)
{
sum=qpow(a,i,p);
if(vis.find(sum)!=vis.end())
{
if(i*k-vis[sum]>=0)
{
return i*k-vis[sum];
}
}
}
return -1;
}
}
int main()
{
ll a,b,p,ans;
while(cin>>p>>a>>b)
{
ans=bsgs(a,b,p);
if(ans==-1)
{
cout<<"no solution"<<endl;
}
else
{
cout<<ans<<endl;
}
}
return 0;
}
后记
推荐一个辅助调试 Uva 题目的网站 udebug。
虽然可能已经人尽皆知了。
UVA10225 Discrete Logging 题解的更多相关文章
- 题解 UVA10225 Discrete Logging
本题是一道 \(BSGS\) 裸题,用于求解高次同余方程,形如 \(a^x\equiv b(\mod p)\),其中 \(a\),\(p\) 互质(不互质还有 \(EXBSGS\)). 建议多使用 \ ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- [POJ2417]Discrete Logging(指数级同余方程)
Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- Discrete Logging
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5865 Accepted: 2618 ...
- BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)
我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...
随机推荐
- Kubernetes 网络:Pod 和 container 的那点猫腻
1. Kubernetes 网络模型 在 Kubernetes 的网络模型中,最小的网络单位是 Pod.Pod 的网络设计原则是 IP-per-Pod,即 Pod 中 container 共享同一套网 ...
- 每天学五分钟 Liunx 001 | 用户及用户组
Liunx 文件权限 [root@controller-0 ~]# ll -al heihei -rw-r--r--. 1 root root 0 Mar 3 07:39 heihei 第一列 -rw ...
- 【SHELL】获取脚本输入参数
参数获取 EXEC_PARAMS=(${@:index}) 示例 ./do.sh test a b c d e f EXEC_PARAMS=(${@:0}) ./do.sh test a b c d ...
- 【MACRO】嵌入式实用的宏技巧 DEBUG-printf 、 #/##
from: C语言.嵌入式中几个非常实用的宏技巧 (qq.com) 宏打印函数 在我们的嵌入式开发中,使用printf打印一些信息是一种常用的调试手段.但是,在打印的信息量比较多的时候,就比较难知道哪 ...
- 【rt-thread】构建自己的项目工程 -- 初始篇
现以stm32f429igt6芯片的板子 & Keil5编译环境为例,记述构建适配自己板子的rt-thread工程的过程 1.拿到rt-thread源码,进入bsp/stm32/librari ...
- java - for循环 排序数组 - 求数组最小值
主要是利用静态变量存储 public class Bubble2 { static int minNumber; public static void main(String[] args) { in ...
- [转帖]如何部署windows版本的oswatcher
2017-02-22 没有评论 windows上也有os watcher:OSWFW. 目前支持的windows版本是: Windows XP (x86 & x64)Windows 7 (x8 ...
- [转帖]HTTP 框架 Hertz 实践入门:性能测试指南
https://maimai.cn/article/detail?fid=1767401397&efid=R2_kM5y-yEUDCK88FZWrGA 干货不迷路2021 年 9 月 8 日, ...
- ipset的学习与使用
ipset的学习与使用 场景说明 虽然可以通过: firewall-cmd --zone=trusted --add-source=$1 --permanent && firewall ...
- openssh 修改版本号显示
#背景介绍:G端项目经常收到相关漏洞但有时升级最新版本(8.8p)还是会有相关漏洞(CVE-2020-15778),只能禁用相关命令或修改版本号 #漏洞名称OpenSSH 命令注入漏洞(CVE-202 ...