题目传送门

前置知识

大步小步算法

题意

多组询问,每次询问依次给定 \(p,a,b\),求 \(a^{x} \equiv b \pmod{p}\) 的最小非负整数解,其中 \(a,p\) 互质。

解法

BSGS 板子题,不做过多介绍。

貌似本题比 P3846 [TJOI2007] 可爱的质数/【模板】BSGSBZOJ3239 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 题解的更多相关文章

  1. 题解 UVA10225 Discrete Logging

    本题是一道 \(BSGS\) 裸题,用于求解高次同余方程,形如 \(a^x\equiv b(\mod p)\),其中 \(a\),\(p\) 互质(不互质还有 \(EXBSGS\)). 建议多使用 \ ...

  2. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

  3. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

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

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

  5. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  6. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

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

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

  8. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  9. Discrete Logging

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5865   Accepted: 2618 ...

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

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

随机推荐

  1. OpenStack 工作流组件: Mistral

    1 Mistral 简介 Mistral 是由 Mirantis 开发,贡献给 OpenStack 社区的工作流组件,它提供 Workflow As a Service 服务. 在计算机中通常处理的任 ...

  2. [转帖]TiDB 内存控制文档

    https://docs.pingcap.com/zh/tidb/stable/configure-memory-usage 目前 TiDB 已经能够做到追踪单条 SQL 查询过程中的内存使用情况,当 ...

  3. 【转帖】Linux性能优化(十三)——CPU性能测试

    一.CPU上下文切换测试场景 使用sysbench模拟多线程调度: sysbench --threads=10 --time=300 threads run 使用vmstat查看CPU上下文切换: c ...

  4. [转帖]Shell~echo -e 颜色输出

    https://www.cnblogs.com/ElegantSmile/p/11144879.html echo -e 可以控制字体颜色和背景颜色输出 从一个例子开始: # echo -e &quo ...

  5. [转帖]018 磁盘 IO 性能监控 / 压测工具 (sar、iotop、fio、iostat)

    https://my.oschina.net/u/3113381/blog/5465063   1 sar 命令查看当前磁盘 IO 读写 sar(System Activity Reporter 系统 ...

  6. [转帖]java启动jar包设置启动参数

    目录 一.代码介绍 1.代码: 二.linux命令 1.命令 三.idea本地调试 1.找到Edit Configurations 2.修改Edit Configurations 参数 3.Edit ...

  7. Windows 2019 standard 安装 Sqlserver 2019 Standard 时报错 不知道如何处理

    Microsoft Windows [版本 10.0.17763.1] (c) 2018 Microsoft Corporation.保留所有权利. D:\>setup.exe Microsof ...

  8. Python设计模式:你的代码真的够优雅吗?

    当涉及到代码优化时,Python作为一种高级编程语言,具有广泛的应用领域和强大的功能.在软件开发中,设计模式是一种被广泛采用的解决问题的方案,它提供了一种在特定情境中重复使用的可行方案.在Python ...

  9. 性能加速包: SpringBoot 2.7&JDK 17,你敢尝一尝吗 | 京东物流技术团队

    前言 众所周知,SpringBoot3.0迎来了全面支持JDK17的局面,且最低支持版本就是JDK17,这就意味着,Spring社区将完全抛弃JDK8,全面转战JDK17.作为JAVA开源生态里的扛把 ...

  10. 从 WebStorm 转到 VSCode!使用一周体验报告

    前言 最近我的 Jetbrains 开源项目授权到期了,想要续订的时候发现 Jetbrains 提高了开源项目申请门槛,我的 StarBlog 项目因为名字里包含 blog 这个词无法申请,虽然我在 ...