Description

Little Y finds there is a very interesting formula in mathematics:

XY mod Z = K

Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, could you figure out Y fast?

Input

Input data consists of no more than 20 test cases. For each test case, there would be only one line containing 3 integers X, Z, K (0 ≤ X, Z, K ≤ 109).
Input file ends with 3 zeros separated by spaces.

Output

For each test case output one line. Write "No Solution" (without quotes) if you cannot find a feasible Y (0 ≤ Y < Z). Otherwise output the minimum Y you find.

Sample Input

  1. 5 58 33
  2. 2 4 3
  3. 0 0 0

Sample Output

  1. 9
  2. No Solution

题解

扩展BSGS:

当模数 $c$ 不是质数的时候,显然不能直接使用 $BSGS$ 了,考虑它的扩展算法。

前提:同余性质。

令 $d = gcd(a, c)$ , $A = a \cdot d,B = b \cdot d, C = c \cdot d$

则 $a \cdot d \equiv b \cdot d \pmod{c \cdot d}$

等价于 $a \equiv b \pmod{c}$

因此我们可以先消除因子。

对于现在的问题 $(A \cdot d)^x \equiv B \cdot d \pmod{C \cdot d}$ 当我们提出 $d = gcd(a, c)$ ($d \neq 1$)后,原式化为 $A \cdot (A \cdot d)^{x-1} \equiv B \pmod{C}$ 。

即求 $D \cdot A^{x-cnt} \equiv B \pmod{C}$ ,令 $x = i \cdot r-j+cnt$ 。之后的做法就和 $BSGS$ 一样了。

值得注意的是因为这样求出来的解 $x \geq cnt$ 的,但有可能存在解 $x < cnt$ ,所以一开始需要特判。

  1. //It is made by Awson on 2018.1.15
  2. #include <set>
  3. #include <map>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <queue>
  7. #include <stack>
  8. #include <cstdio>
  9. #include <string>
  10. #include <vector>
  11. #include <cstdlib>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #define LL long long
  16. #define Abs(a) ((a) < 0 ? (-(a)) : (a))
  17. #define Max(a, b) ((a) > (b) ? (a) : (b))
  18. #define Min(a, b) ((a) < (b) ? (a) : (b))
  19. #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
  20. using namespace std;
  21. const LL MOD = ;
  22. void read(LL &x) {
  23. char ch; bool flag = ;
  24. for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
  25. for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
  26. x *= -*flag;
  27. }
  28. void write(LL x) {
  29. if (x > ) write(x/);
  30. putchar(x%+);
  31. }
  32.  
  33. LL a, b, c, ans;
  34. struct MAP {
  35. LL ha[MOD+]; int id[MOD+];
  36. void clear() {for (int i = ; i < MOD; i++) ha[i] = id[i] = -; }
  37. int count(LL x) {
  38. LL pos = x%MOD;
  39. while (true) {
  40. if (ha[pos] == -) return ;
  41. if (ha[pos] == x) return ;
  42. ++pos; if (pos >= MOD) pos -= MOD;
  43. }
  44. }
  45. void insert(LL x, int idex) {
  46. LL pos = x%MOD;
  47. while (true) {
  48. if (ha[pos] == - || ha[pos] == x) {ha[pos] = x, id[pos] = idex; return; }
  49. ++pos; if (pos >= MOD) pos -= MOD;
  50. }
  51. }
  52. int query(LL x) {
  53. LL pos = x%MOD;
  54. while (true) {
  55. if (ha[pos] == x) return id[pos];
  56. ++pos; if (pos >= MOD) pos -= MOD;
  57. }
  58. }
  59. }mp;
  60.  
  61. LL quick_pow(LL a, LL b, LL c) {
  62. LL ans = ;
  63. while (b) {
  64. if (b&) ans = ans*a%c;
  65. a = a*a%c, b >>= ;
  66. }
  67. return ans;
  68. }
  69. LL gcd(LL a, LL b) {return b ? gcd(b, a%b) : a; }
  70. LL exBSGS(LL a, LL b, LL c) {
  71. if (b == ) return ;
  72. LL cnt = , d = , t;
  73. while ((t = gcd(a, c)) != ) {
  74. if (b%t) return -;
  75. ++cnt, b /= t, c /= t, d = d*(a/t)%c;
  76. if (d == b) return cnt;
  77. }
  78. mp.clear();
  79. LL tim = ceil(sqrt(c)), tmp = b%c;
  80. for (int i = ; i <= tim; i++) {
  81. mp.insert(tmp, i); tmp = tmp*a%c;
  82. }
  83. t = tmp = quick_pow(a, tim, c); tmp = (tmp*d)%c;
  84. for (int i = ; i <= tim; i++) {
  85. if (mp.count(tmp)) return tim*i-mp.query(tmp)+cnt;
  86. tmp = tmp*t%c;
  87. }
  88. return -;
  89. }
  90. void work() {
  91. while ((~scanf("%lld%lld%lld", &a, &c, &b))) {
  92. if (c == ) return;
  93. if ((ans = exBSGS(a%c, b%c, c)) == -) printf("No Solution\n");
  94. else write(ans), putchar('\n');
  95. }
  96. }
  97. int main() {
  98. work();
  99. return ;
  100. }

[POJ 3243]Clever Y的更多相关文章

  1. POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)

    不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...

  2. POJ 3243 Clever Y 扩展BSGS

    http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...

  3. poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】

    扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...

  4. POJ 3243 Clever Y(离散对数-拓展小步大步算法)

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  5. poj 3243 Clever Y 高次方程

    1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...

  6. POJ 3243 Clever Y | BSGS算法完全版

    题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...

  7. POJ 3243 Clever Y Extended-Baby-Step-Giant-Step

    题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quarter ...

  8. 【POJ】3243 Clever Y

    http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...

  9. BZOJ 3243 Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

随机推荐

  1. C语言——总结回顾

    1.当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 答:①当初选择计算机专业是出于一种选择,是一种带着冲 ...

  2. Flask 扩展 自定义扩展

    创建一个为视图访问加日志的扩展Flask-Logging,并从中了解到写Flask扩展的规范. 创建工程 先创建一个工程,目录结构如下: flask-logging/ ├ LICENSE # 授权说明 ...

  3. 第八条:覆盖equals时请遵守通用约定

    ==是物理相等 equals是逻辑相等 因为每个类的实例对象本质上都是唯一的 ,利用物理相等(==)是指一个实例只能相等于它自己. 利用逻辑相等是(equals)指 一个实例是否和另一个实例的某些关键 ...

  4. 06_Python的数据类型3元组,集合和字典_Python编程之路

    上一节跟大家讲了Python的列表,当然不是完整的讲完,后续我们还会提到,这一节我们还是来讲Python的数据类型 首先要讲到的就是元组 元组其实拥有列表的一些特性,可以存储不同类型的值,但在某些方面 ...

  5. c# gridview 新增行

    string[] newRow = {"long","d","b"}; Gridview.Rows.Insert(Gridview.Rows ...

  6. php_类的定义

    此文章为原创见解,例子各方面也是东拼西凑.如果有错请留言.谢谢 在面向对象的思维中提出了两个概念,类和对象. 类是对某一类实物的抽象描述,而对象用于表示现实中该类事物的个体, 例子:老虎是父类,东北虎 ...

  7. LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析

    Array DP Divide and Conquer Description: Find the contiguous subarray within an array (containing at ...

  8. Nagios监控的部署与配置

    [安装Nagios] yum install -y httpd httpd-devel httpd-tools mysql mysql-devel mysql-server php php-devel ...

  9. Docker学习笔记 - Docker的数据卷容器

    一.什么是数据卷容器 如果你有一些持续更新的数据需要在容器之间共享,最好创建数据卷容器. 数据卷容器:用于容器间的数据共享,主动挂载宿主机目录,用于其他容器挂载和共享. 二.数据卷容器的操作 1.创建 ...

  10. python入门(13)获取函数帮助和调用函数

    Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: http://doc ...