BZOJ 3243 Clever Y
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 file ends with 3 zeros separated by spaces.
Output
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的更多相关文章
- 【POJ】3243 Clever Y
http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...
- 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 ...
- POJ 3243 Clever Y 扩展BSGS
http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...
- POJ 3243 Clever Y(离散对数-拓展小步大步算法)
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
- poj 3243 Clever Y 高次方程
1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...
- [POJ 3243]Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
- POJ 3243 Clever Y | BSGS算法完全版
题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...
- POJ 3243 Clever Y Extended-Baby-Step-Giant-Step
题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quarter ...
随机推荐
- 巨人大哥谈Java中的Synchronized关键字用法
巨人大哥谈Java中的Synchronized关键字用法 认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方价格synchronized基本上就 ...
- 敏捷冲刺每日报告——Day2
1.情况简述 Alpha阶段第一次Scrum Meeting 敏捷开发起止时间 2017.10.26 00:00 -- 2017.10.27 00:00 讨论时间地点 2017.10.26晚9:30, ...
- c++第0次作业
1.你认为大学的学习生活.同学关系.师生应该是怎样? 随着大学生活的慢慢到来,我开始领悟到大学并不是自由的天堂,相反,我们更加的走进社会这个牢笼.在这个牢笼中有着从前的我们并不需要在意和考虑的规则与问 ...
- 201621123062《java程序设计》第七周作业总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 1.布局管理器的具体使用方法 2.事件处理模型及其代码的编写 3.Swing中的常用组件 4. ...
- pandas 数据分析使用
https://github.com/Erick-LONG/data_analysis/blob/master/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%20%E9%8 ...
- 大神都在看的RxSwift 的完全入坑手册
大神都在看的RxSwift 的完全入坑手册 2015-09-24 18:25 CallMeWhy callmewhy 字号:T | T 我主要是通过项目里的 Rx.playground 进行学习和了解 ...
- Unix下zfs文件系统重组RAID-5后可以这样恢复
存储做的RAID-5, SCSI硬盘,操作系统是FreeBSD,文件系统是zfs.本案例共有12块硬盘,11块硬盘里有数据,1块硬盘是热备盘.其中第6块数据硬盘出现故障,重组时需要将其剔除. 物理盘: ...
- :after/:before使用技巧
伪类:after/:before基本使用 div:before{ content:'';//必须要写,没写则伪元素无效 display:; position:''; ... } //在一个div子元素 ...
- .NET Core/.NET之Stream简介
之前写了一篇C#装饰模式的文章提到了.NET Core的Stream, 所以这里尽量把Stream介绍全点. (都是书上的内容) .NET Core/.NET的Streams 首先需要知道, Syst ...
- python 基础 set 集合类型补充
为啥今天又重提这个数据类型呢?平时用的少,等要用起来的时候才发现,自己对这块啥都不知道了,so,今天就把这块再梳理一下咯. 一.set集合,是一个无序且不重复的元素集合.这一点是非常重要的. 二.集合 ...