[POJ 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
题解
扩展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的更多相关文章
- 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 | 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 ...
- 【POJ】3243 Clever Y
http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...
- BZOJ 3243 Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
随机推荐
- <经验杂谈>C#对CA证书加密解密的简单介绍
最近做项目接触了一些关于用CA证书加密解密的知识,现在分享一下,加密主要分为对称加密和非对称加密以及单项加密这三种,CA是一个权威的第三方认证机构,CA加密有公钥和私钥之分. 以下是C#读取证书文件进 ...
- APP案例分析--扇贝单词
APP案例分析 一.调研 1.第一次上手 第一次使用时,一进APP,有一个每日一句,然后就是登录界面.有点不舒服,我都还不知道你这个APP好不好用,不让我体验一下就要注册.简单的测试了我的英语水平 ...
- fs检测文件夹状态
var http = require("http"); var fs = require("fs"); var server = http.createServ ...
- [Android FrameWork 6.0源码学习] View的重绘ViewRootImpl的setView方法
博客首页:http://www.cnblogs.com/kezhuang/p/ 本篇文章来分析一下WindowManager的后续工作,也就是ViewRootImpl的setView函数的工作 /i* ...
- django models的点查询/跨表查询/双下划线查询
django models 在日常的编程中,我们需要建立数据库模型 而往往会用到表与表之间的关系,这就比单表取数据要复杂一些 在多表之间发生关系的情形下,我们如何利用models提供的API的特性获得 ...
- js前端对后台数据的获取,如果是汉字则需要添上引号
js前端对后台数据的获取,如果是汉字则需要添上引号
- CentOS 7 PHP-redis扩展安装,浏览器不显示数据及redis无法储存数据常见问题解决办法
首先使用php -m 可以查看到自己安装了那些扩展. 1.使用wget下载redis压缩包 wget https://github.com/phpredis/phpredis/archive/deve ...
- LAMP 搭建
p { margin-bottom: 0.25cm; line-height: 120% } LAMP 搭建 承 Ubuntu 17.10.1安装, 定制. 参考 电子工业出版社, Ubuntu完美应 ...
- Spring中获取request的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Java线程池是如何诞生的?
时间回到2003年,那时我还是一个名不见经传的程序员,但是上级却非常看好我,他们把整个并发模块,都交给了我一个人开发. 这个星期,我必须要完成并发模块中非常重要的一个功能--线程池. 注:文末有福利 ...