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 ...
随机推荐
- C语言程序设计(基础)- 第14、15周作业
从本周开始,将作业标记为学校自然周,而不是开课的周数. 要求一(25经验值) 完成14.15周的所有PTA中题目集. 注意1:一周两次pta作业,包括四次. 要求二(50经验值) 博客的具体书写内容和 ...
- 《团队-手机app便签-开发文档》
项目托管平台地址:https://github.com/Vcandoit/Notepad.git 我主要负责文件存储部分,文字部分使用sqlite保存. 因为我们想实现备忘录记录照片.语音的功能,所以 ...
- Beta冲刺NO.6
Beta冲刺 第六天 1. 昨天的困难 1.对于设计模式的应用不熟悉,所以在应用上出现了很大的困难. 2.SSH中数据库的管理是用HQL语句实现的,所以在多表查询时出现了很大的问题. 3.页面结构太凌 ...
- electron-vue工程创建
没有vue创建经验请移步至 vue下载与安装 使用vue创建electron-vue工程 vue init simulatedgreg/electron-vue my-project 安装elemen ...
- JavaScript 相关知识
一.数组 var a = [1,2,3,4]; console.log(a.length); a.push(5); console.log(a); // [1, 2, 3, 4, 5] var r ...
- 关于Java的异常
异常机制概述 异常机制是指当程序出现错误后,程序如何处理.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器. 异常处理的流程 当程序中抛出 ...
- Linux 磁盘和文件管理系统 文件打包解压备份 VIM、VI编辑器
- JAVA_SE基础——59.权限访问修饰符
了解了包的概念,就可以系统的介绍Java中的访问控制级别.在Java中,针对类.成员方法和属性提供了四种访问级别,分别是private.default.protected和public. 权限访问修饰 ...
- monog和github学习
1.导出服务器数据库到本地以json的格式储存:mongoexport -h ip -d dbname -c user -o D:\mondb\user.json2.导入本地Json到本地项目中:D: ...
- Spring Security 入门(1-1)Spring Security是什么?
1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...