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 33
2 4 3
0 0 0

Sample Output

9
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$ ,所以一开始需要特判。

 //It is made by Awson on 2018.1.15
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
const LL MOD = ;
void read(LL &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(LL x) {
if (x > ) write(x/);
putchar(x%+);
} LL a, b, c, ans;
struct MAP {
LL ha[MOD+]; int id[MOD+];
void clear() {for (int i = ; i < MOD; i++) ha[i] = id[i] = -; }
int count(LL x) {
LL pos = x%MOD;
while (true) {
if (ha[pos] == -) return ;
if (ha[pos] == x) return ;
++pos; if (pos >= MOD) pos -= MOD;
}
}
void insert(LL x, int idex) {
LL pos = x%MOD;
while (true) {
if (ha[pos] == - || ha[pos] == x) {ha[pos] = x, id[pos] = idex; return; }
++pos; if (pos >= MOD) pos -= MOD;
}
}
int query(LL x) {
LL pos = x%MOD;
while (true) {
if (ha[pos] == x) return id[pos];
++pos; if (pos >= MOD) pos -= MOD;
}
}
}mp; LL quick_pow(LL a, LL b, LL c) {
LL ans = ;
while (b) {
if (b&) ans = ans*a%c;
a = a*a%c, b >>= ;
}
return ans;
}
LL gcd(LL a, LL b) {return b ? gcd(b, a%b) : a; }
LL exBSGS(LL a, LL b, LL c) {
if (b == ) return ;
LL cnt = , d = , t;
while ((t = gcd(a, c)) != ) {
if (b%t) return -;
++cnt, b /= t, c /= t, d = d*(a/t)%c;
if (d == b) return cnt;
}
mp.clear();
LL tim = ceil(sqrt(c)), tmp = b%c;
for (int i = ; i <= tim; i++) {
mp.insert(tmp, i); tmp = tmp*a%c;
}
t = tmp = quick_pow(a, tim, c); tmp = (tmp*d)%c;
for (int i = ; i <= tim; i++) {
if (mp.count(tmp)) return tim*i-mp.query(tmp)+cnt;
tmp = tmp*t%c;
}
return -;
}
void work() {
while ((~scanf("%lld%lld%lld", &a, &c, &b))) {
if (c == ) return;
if ((ans = exBSGS(a%c, b%c, c)) == -) printf("No Solution\n");
else write(ans), putchar('\n');
}
}
int main() {
work();
return ;
}

[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. RTMP规范协议

    本文参照rtmp协议英文版,进行简单的协议分析 1.什么是RTMP 关于 Adobe 的实时消息协议(Real Time Messaging Protocol,RTMP),是一种多媒体的复用和分组的应 ...

  2. oracle导入dmp文件的2种方法

    使用imp.impdp方式导入数据 1.使用imp导入数据 打开cmd窗口,然后直接敲入一下命令即可,需要注意的是,要事先把dmp文件放到正确的路径中去 imp yx_base/@yx_192. fi ...

  3. Build to win

    UPDATE:看到周筠老师的评论里的链接,那版式真的非常舒服.我想想模仿模仿他的布局来看看,虽然感觉做的也不是太好.另外对博客内容稍作修改. 一.获得小黄衫的感受 很幸运能够获得"领骑衫&q ...

  4. Alpha阶段小结

    1 团队的源码仓库地址 https://github.com/WHUSE2017/MyGod 2 Alpha过程回顾 2.1 团队项目预期 有一个可视化的安卓APP,实现二手交易基本功能.预期的典型用 ...

  5. Struts2之配置文件中Action的详细配置

    在Struts2之配置一文中,我们知道一个struts配置文件可以分为三部分:常量配置    包含其他配置文件的配置    Action配置  . 这其中 常量配置  和 包含其他配置文件的配置  二 ...

  6. HP DL380服务器RAID信息丢失数据恢复方法和数据恢复过程分享

    [数据恢复故障描述]    客户服务器属于HP品牌DL380系列,存储是由6块73GB SAS硬盘组成的RAID5,操作系统是WINDOWS 2003 SERVER,主要作为企业部门内部的文件服务器来 ...

  7. cookie中存中文

    cookie中存中文 1:想要在cookie中存中文:需要用到URLEncoder(在jdkAPI中有介绍) Cookie cookie = new Cookie("User",U ...

  8. kubernetes进阶(03)kubernetes的namespace

    服务发现与负载均衡Kubernetes在设计之初就充分考虑了针对容器的服务发现与负载均衡机制,提供了Service资源,并通过kube-proxy配合cloud provider来适应不同的应用场景. ...

  9. SpringCloud的服务消费者 (二):(rest+feign/ribbon)声明式访问注册的微服务

    采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...

  10. 版本名称GA的含义:SNAPSHOT->alpha->beta->release->GA

    SNAPSHOT->alpha->beta->release->GA ----------------------------------------------------- ...