博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ 3243]Clever Y
阅读量:5354 次
发布时间:2019-06-15

本文共 3696 字,大约阅读时间需要 12 分钟。

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

5 58 332 4 30 0 0

Sample Output

9No 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 
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
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 = 233333; 22 void read(LL &x) { 23 char ch; bool flag = 0; 24 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar()); 25 for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar()); 26 x *= 1-2*flag; 27 } 28 void write(LL x) { 29 if (x > 9) write(x/10); 30 putchar(x%10+48); 31 } 32 33 LL a, b, c, ans; 34 struct MAP { 35 LL ha[MOD+5]; int id[MOD+5]; 36 void clear() { for (int i = 0; i < MOD; i++) ha[i] = id[i] = -1; } 37 int count(LL x) { 38 LL pos = x%MOD; 39 while (true) { 40 if (ha[pos] == -1) return 0; 41 if (ha[pos] == x) return 1; 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] == -1 || 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 = 1; 63 while (b) { 64 if (b&1) ans = ans*a%c; 65 a = a*a%c, b >>= 1; 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 == 1) return 0; 72 LL cnt = 0, d = 1, t; 73 while ((t = gcd(a, c)) != 1) { 74 if (b%t) return -1; 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 = 0; 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 = 1; i <= tim; i++) { 85 if (mp.count(tmp)) return tim*i-mp.query(tmp)+cnt; 86 tmp = tmp*t%c; 87 } 88 return -1; 89 } 90 void work() { 91 while ((~scanf("%lld%lld%lld", &a, &c, &b))) { 92 if (c == 0) return; 93 if ((ans = exBSGS(a%c, b%c, c)) == -1) printf("No Solution\n"); 94 else write(ans), putchar('\n'); 95 } 96 } 97 int main() { 98 work(); 99 return 0;100 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/8294767.html

你可能感兴趣的文章
Ajax : load()
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>
Zookeeper概述
查看>>
Linux自己安装redis扩展
查看>>
HDU 1016 Prime Ring Problem(dfs)
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>
360浏览器兼容模式 不能$.post (不是a 连接 onclick的问题!!)
查看>>
spring注入Properties
查看>>
LeetCode : Reverse Vowels of a String
查看>>