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\equiv b\pmod p\\
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\),此时式子的形式是

\[k\times(a^m)^i\equiv a^jb\pmod 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的更多相关文章

  1. 【BZOJ】【2480】【SPOJ 3105】Mod

    扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...

  2. 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster

    目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...

  3. 「SPOJ 3105」Power Modulo Inverted

    「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...

  4. BZOJ 2480 && 3239 && 2995 高次不定方程(高次同余方程)

    链接 BZOJ 2480 虽然是个三倍经验题(2333),但是只有上面这道(BZOJ2480)有 p = 1 的加强数据,推荐大家做这道. 题解 这是一道BSGS(Baby Step Giant St ...

  5. BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论

    BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...

  6. BZOJ.2616.SPOJ PERIODNI(笛卡尔树 树形DP)

    BZOJ SPOJ 直观的想法是构建笛卡尔树(每次取最小值位置划分到两边),在树上DP,这样两个儿子的子树是互不影响的. 令\(f[i][j]\)表示第\(i\)个节点,放了\(j\)个车的方案数. ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Dynamics 365 CE中AsyncOperationBase表记录太多,影响系统性能怎么办?

    微软动态CRM专家罗勇 ,回复311或者20190311可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文主要是根据微软官 ...

  2. kmalloc分配物理内存与高端内存映射--Linux内存管理(十八)

    1 前景回顾 1.1 内核映射区 尽管vmalloc函数族可用于从高端内存域向内核映射页帧(这些在内核空间中通常是无法直接看到的), 但这并不是这些函数的实际用途. 重要的是强调以下事实 : 内核提供 ...

  3. web 项目运用通用的xml配置

    jdk10的转换: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncod ...

  4. 对java中的equals()方法的总结

    Java的基础学习总结--equals方法 一,等于方法介绍 1.1.通过下面的例子掌握等于的用法 1 package cn.galc.test; 2 3 public class TestEqual ...

  5. android的listview以及画线--to thi tha

    https://www.cnblogs.com/896240130Master/p/6135165.html 这个的 https://www.jianshu.com/p/5522470760c1

  6. JSP七大动作

  7. 文本分类实战(二)—— textCNN 模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  8. day9-基础函数的学习(四)

    这几天一直赶着写写作业,博客的书写又落下了,要加油鸭,开写 今日份目录 1.内置函数 2.递归函数 开始今日份总结 1.内置函数 内置函数就是python内部包含的函数,总计有68种,不过有些事真的天 ...

  9. exgcd

    int exgcd(int a,int b,int &x,int &y){ if (b==0){ x=1,y=0; return a; } int d=exgcd(b,a%b,y,x) ...

  10. SQL语句检索数据排序及过滤

    阅读目录 一:排序检索数据 二:过滤数据 三:高级数据过滤 四:用通配符进行过滤 回到顶部 一:排序检索数据 1.1 排序数据 比如查询数据库中表数据的时候,我们使用如下语句: select * fr ...