Description

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

XY mod Z = K

Given XYZ, we all know how to figure out K fast. However, given XZK, 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 XZK (0 ≤ XZK ≤ 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

转载自:Navi

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

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
int MOD=;
lol hash[],id[];
lol gcd(lol a,lol b)
{
if (!b) return a;
return gcd(b,a%b);
}
void insert(lol x,lol d)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==-||hash[pos]==x)
{
hash[pos]=x;
id[pos]=d;
return;
}
pos++;
if (pos>=MOD) pos-=MOD;
}
}
bool count(lol x)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==-) return ;
if (hash[pos]==x) return ;
pos++;
if (pos>=MOD) pos-=MOD;
}
}
lol query(lol x)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==x) return id[pos];
pos++;
if (pos>=MOD) pos-=MOD;
}
}
lol qpow(lol x,lol y,lol Mod)
{
lol res=;
while (y)
{
if (y&) res=res*x%Mod;
x=x*x%Mod;
y>>=;
}
return res;
}
lol exBSGS(lol a,lol b,lol Mod)
{lol i;
if (b==) return ;
memset(hash,-,sizeof(hash));
memset(id,,sizeof(id));
lol cnt=,d=,t;
while ((t=gcd(a,Mod))!=)
{
if (b%t) return -;
cnt++;
b/=t;Mod/=t;
d=d*(a/t)%Mod;
if (d==b) return cnt;
}
lol tim=ceil(sqrt((double)Mod));
lol tmp=b%Mod;
for (i=;i<=tim;i++)
{
insert(tmp,i);
tmp=tmp*a%Mod;
}
t=tmp=qpow(a,tim,Mod);
tmp=tmp*d%Mod;
for (i=;i<=tim;i++)
{
if (count(tmp))
return i*tim-query(tmp)+cnt;
tmp=tmp*t%Mod;
}
return -;
}
int main()
{lol p,a,b,ans;
while (scanf("%lld%lld%lld",&a,&p,&b))
{
if (p==) return ;
if ((ans=exBSGS(a,b,p))==-) printf("No Solution\n");
else printf("%lld\n",ans);
}
}

BZOJ 3243 Clever Y的更多相关文章

  1. 【POJ】3243 Clever Y

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

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

  3. POJ 3243 Clever Y 扩展BSGS

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

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

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

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

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

  6. poj 3243 Clever Y 高次方程

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

  7. [POJ 3243]Clever Y

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

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

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

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

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

随机推荐

  1. 配置tomcat8数据源(采用局部数据源方式)

    tomcat提供两种数据源配置方式,全局和局部.全局的话对于所有web应用都生效,局部只对于配置的某一个web生效. 步骤: 1.将mysql的jdbc驱动复制到tomcat的lib路径下. 2.在t ...

  2. alpha冲刺第四天

    一.合照 二.项目燃尽图 三.项目进展 今天实现了登录界面和服务器的连接了,牵手成功. 一些具体的界面细化实现,一些button的响应实现 四.明日规划 登录界面和服务器的连接实现耗费了太多时间,接下 ...

  3. 高级软件工程2017第6次作业--团队项目:Alpha阶段综合报告

    高级软件工程2017第6次作业--团队项目:Alpha阶段综合报告 Deadline:2017-10-30(周一)21:00pm (注:以下内容参考集大作业4,集大作业5,集大作业6,集大作业7 一. ...

  4. Flask 扩展 HTTP认证

    Restful API不保存状态,无法依赖Cookie及Session来保存用户信息,自然也无法使用Flask-Login扩展来实现用户认证.所以这里,我们就要介绍另一个扩展,Flask-HTTPAu ...

  5. css3动画transition详解

    一.transition-property 语法: transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ] ...

  6. jsMath对象

    Math对象: abs.用来求绝对值. ceil:用来向上取整. floor:用来向下取整. round:用来四舍五入取近似值. sqrt:用来开方. pow:括号内有2位参数.如pow(2,5)表示 ...

  7. 1-51单片机WIFI学习(开发板介绍)

    源码链接都在后面 前面的都是介绍单独的WIFI,没有和单片机结合起来,因为做项目很少会只用WIFI模块.大多数都是WIFI模块作为中转数据的桥梁,单片机负责 数据采集,控制等等,所以自己准备出一套51 ...

  8. Docker学习笔记 - Docker的仓库

  9. js常用的数组方法

    1.创建数组的基本方法:  1.1 空数组  var obj=new Array();                 1.2 指定长度数组  var obj=new Array(size);     ...

  10. python基础——继承实现的原理

    python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...