[BZOJ 2480] [SPOJ 3105] Mod
Description
已知数 \(a,p,b\),求满足 \(a^x\equiv b\pmod p\) 的最小自然数 \(x\)。
Input
每个测试文件中最多包含 \(100\) 组测试数据。
每组数据中,每行包含 \(3\) 个正整数 \(a,p,b\)。
当 \(a=p=b=0\) 时,表示测试数据读入完全。
Output
对于每组数据,输出一行。
如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。
Sample Input
5 58 33
2 4 3
0 0 0
Sample Output
9
No Solution
HINT
\(a,p,b≤10^9\)
Solution
当 \(\gcd(a,q)\not\mid b\) 且 \(b\ne 1\) 时无解。
a^{x-1}\frac{a}{(a,p)}\equiv \frac{b}{(a,p)}\pmod{\frac{p}{(a,p)}}
\]
注意这里不能写成 \(a^{x-1}\equiv b\times a^{-1}\pmod{\frac{p}{(a,p)}}\),因为此时 \(a\not\perp p\),没有逆元。
递归求解,直到 \(a\perp p\),此时式子的形式是
\]
从 \(0\dots m\) 枚举 \(j\),将 \(a^jb\) 的值存入 \(hash\) 表中,然后从 \(1\dots m\) 枚举 \(i\),若表中存在 \(k\times (a^m)^i\),则当前 \(i\times m-j\) 就是答案。
Code
#include <cmath>
#include <cstdio>
#include <tr1/unordered_map>
std::tr1::unordered_map<int,int> hash;
int read() {
int x = 0; char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
void exbsgs(int a, int b, int p) {
if (p == 1 || b == 1) { puts("0"); return; }
int t = gcd(a, p), k = 0, c = 1;
while (t > 1) {
if (b % t) { puts("No Solution"); return; }
b /= t, p /= t, c = 1LL * c * a / t % p, t = gcd(a, p), ++k;
if (b == c) { printf("%d\n", k); return; }
}
int m = ceil(sqrt(p)); hash.clear(), hash[b] = 0;
for (int i = 1; i <= m; ++i) t = 1LL * t * a % p, hash[1LL * t * b % p] = i;
a = t, t = 1LL * t * c % p;
for (int i = 1; i <= m; ++i, t = 1LL * t * a % p)
if (hash.count(t)) { printf("%d\n", i * m - hash[t] + k); return; }
puts("No Solution");
}
int main() {
int a = read(), p = read(), b = read();
while (a) exbsgs(a % p, b % p, p), a = read(), p = read(), b = read();
return 0;
}
[BZOJ 2480] [SPOJ 3105] Mod的更多相关文章
- 【BZOJ】【2480】【SPOJ 3105】Mod
扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...
- 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster
目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...
- 「SPOJ 3105」Power Modulo Inverted
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...
- BZOJ 2480 && 3239 && 2995 高次不定方程(高次同余方程)
链接 BZOJ 2480 虽然是个三倍经验题(2333),但是只有上面这道(BZOJ2480)有 p = 1 的加强数据,推荐大家做这道. 题解 这是一道BSGS(Baby Step Giant St ...
- BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...
- BZOJ.2616.SPOJ PERIODNI(笛卡尔树 树形DP)
BZOJ SPOJ 直观的想法是构建笛卡尔树(每次取最小值位置划分到两边),在树上DP,这样两个儿子的子树是互不影响的. 令\(f[i][j]\)表示第\(i\)个节点,放了\(j\)个车的方案数. ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
随机推荐
- html标签种类很多,为什么不都用div?
why not divs? 所有html页面标签都可以用div解决,为什么还会存在各种不同的标签呢? 代码是写给机器阅读的,初始化标签更利于快速编程,毕竟很多标签有了自定义属性,无需编码控制,可维护性 ...
- springboot项目打包运行
在springboot项目打包成jar包时,在cmd中使用java -jar **.jar时,浏览器无法访问. 解决方法:把项目改成war包项目,在pom文件中更改,并打成war包. 使用maven命 ...
- 使用 empApi 组件实现 Change Data Capture 功能
Change Data Capture 功能是从 Winter '19 版本开始正式启用的功能. 它是基于"发布-订阅"模式设计,可以将 Salesforce 中记录的改变自动推送 ...
- 学习安卓开发[1] - 程序结构、Activity生命周期及页面通信
一.程序结构 Android原生应用采用了MVC的架构设计模式,因此可以将一个Android APP中的对象归为Model.View或Controller中的一种. 具体到某个实际的APP结构中,它一 ...
- asp.net core 2.1 部署IIS(win10/win7)
asp.net core 2.1 部署IIS(win10/win7) 概述 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器 ...
- MySQL 基础知识梳理学习(五)----详解MySQL两次写的设计及实现
一 . 两次写提出的背景或要解决的问题 两次写(InnoDB Double Write)是Innodb中很独特的一个功能点.因为Innodb中的日志是逻辑的,所谓逻辑就是比如插入一条记录时,它可能会在 ...
- 自己手写一个SpringMVC 框架
一.了解SpringMVC运行流程及九大组件 1.SpringMVC 的运行流程 · 用户发送请求至前端控制器DispatcherServlet · DispatcherServlet收到请求调用 ...
- 时序数据库InfluxDB安装及使用
时序数据库InfluxDB安装及使用 1 安装配置 安装 wget https://dl.influxdata.com/influxdb/releases/influxdb-1.3.1.x86_64. ...
- sqlbulkcopy 批量插入数据
批量插入 Datetable数据 通过sqlbulkcopy 插入1百万条数据 用时 10秒钟 (有兴趣的小伙伴可以去测试) /// <summary> /// /// </sum ...
- python粗谈面向对象(一)
1.面向过程编程vs函数式编程 面向过程编程 以计算对象的元素个数为例. str_1 = 'abcdefg' count = 0 for i in str_1: # 统计字符串元素个数 count + ...