MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS
思路:
前两题题面相同,代码也相同,就只贴一题的题面了。这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题。
SPOJ3105题目链接:https://www.spoj.com/problems/MOD/
POJ3243题目链接:http://poj.org/problem?id=3243
题目:

代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pli;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f; int x, z, k;
unordered_map<LL, int> mp; int Mod_Pow(int x, int n, int mod) {
int res = ;
while(n) {
if(n & ) res = (LL)res * x % mod;
x = (LL)x * x % mod;
n >>= ;
}
return res;
} int gcd(int a, int b) {
return b == ? a : gcd(b, a % b);
} int EXBSGS(int A, int B, int C) {
A %= C, B %= C;
if(B == ) return ;
int cnt = ;
LL t = ;
for(int g = gcd(A, C); g != ; g = gcd(A, C)) {
if(B % g) return -;
C /= g, B /= g, t = t * A / g % C;
cnt++;
if(B == t) return cnt;
}
mp.clear();
int m = ceil(sqrt(1.0*C));
LL base = B;
for(int i = ; i < m; i++) {
mp[base] = i;
base = base * A % C;
}
base = Mod_Pow(A, m, C);
LL nw = t;
for(int i = ; i <= m; i++) {
nw = nw * base % C;
if(mp.count(nw)) {
return i * m - mp[nw] + cnt;
}
}
return -;
} int main() {
//FIN;
while(~scanf("%d%d%d", &x, &z, &k)) {
if(x == && z == && k == ) break;
int ans = EXBSGS(x, k, z);
if(ans == -) printf("No Solution\n");
else printf("%d\n", ans);
}
return ;
}
Gym 101853G题目链接:http://codeforces.com/gym/101853/problem/G
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pli;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f; int t, a, b, m;
unordered_map<LL, int> mp; LL Mod_Pow(LL x, LL n, LL mod) {
LL res = ;
while(n) {
if(n & ) res = res * x % mod;
x = x * x % mod;
n >>= ;
}
return res;
} int gcd(int a, int b) {
return b == ? a : gcd(b, a % b);
} LL EXBSGS(int A, int B, int C) {
A %= C, B %= C;
if(B == ) return ;
int cnt = ;
LL t = ;
for(int g = gcd(A, C); g != ; g = gcd(A, C)) {
if(B % g) return -;
C /= g, B /= g;
t = t * A / g % C;
cnt++;
if(B == t) return cnt;
}
mp.clear();
int m = ceil(sqrt(1.0 * C));
LL base = B;
for(int i = ; i < m; i++) {
mp[base] = i;
base = base * A % C;
}
base = Mod_Pow(A, m, C);
LL nw = t;
for(int i = ; i <= m + ; i++) {
nw = base * nw % C;
if(mp.count(nw)) {
return i * m - mp[nw] + cnt;
}
}
return -;
} int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &a, &b, &m);
LL ans = EXBSGS(a, b, m);
printf("%lld\n", ans);
}
return ;
}
MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS的更多相关文章
- spoj3105 MOD - Power Modulo Inverted(exbsgs)
传送门 关于exbsgs是个什么东东可以去看看yyb大佬的博客->这里 //minamoto #include<iostream> #include<cstdio> #i ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- 「SPOJ 3105」Power Modulo Inverted
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...
- 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS
[BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input ...
- poj3243 Clever Y[扩展BSGS]
Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8666 Accepted: 2155 Descript ...
- luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法
BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...
- bzoj 1467: Pku3243 clever Y 扩展BSGS
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...
- [拓展Bsgs] Clever - Y
题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...
- bzoj1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 313 Solved: 181[Submit][Status ...
随机推荐
- PXE推一半失败,HP服务器、曙光服务器删除数据
一.#设备:惠普HP DL380 Gen9 PXE安装失败,系统尝试从硬盘启动 需要将安装未完整的系统数据删除,以便正常装机 从控制台重启设备 重启后,HP在此界面选择Intelligent Prov ...
- 【第一周】PSP
日期 C类别 C内容 S开始时间 E结束时间 I间隔(单位:分钟) T净时间(单位:分钟) 9月2日 编程 词频统计 7:35 9:35 10 110 9月3日 读书 构建之法 8:00 9:00 5 ...
- 【Java】对ArrayList排序
java如何对ArrayList中对象按照该对象某属性排序 (从小到大) 两种方法: 方法一:Comparator<KNNNode> comparator = new Comparator ...
- [历史百科]抗战时期兵团简介 From 百度知道
中央军委1948年11月1日和1949年1月15日两次关于统一全军组织和部队番号的训令,我军先后进行了整编.西北野战军改称第一野战军,司令员兼政治委员彭德怀,第一副司令员张宗逊,第二副司令员赵寿山,参 ...
- 【C++】深度探索C++对象模型读书笔记--Data语意学(The Semantics of data)
1. 一个空类的大小是1 byte.这是为了让这一类的两个对象得以在内存中配置独一无二的地址. 2. Nonstatic data member 放置的是“个别的class object”感兴趣的数据 ...
- 【Django】Django迁移数据库
我们已经编写了博客数据库模型的代码,但那还只是 Python 代码而已,Django 还没有把它翻译成数据库语言,因此实际上这些数据库表还没有真正的在数据库中创建 为了让 Django 完成翻译,创建 ...
- 解数独(Python)
0.目录 1.介绍 2.一些通用函数 3.全局变量(宏变量) 4.数独预处理(约束传播) 5.解数独(深度优先搜索+最小代价优先) 6.主函数 7.总代码 1.介绍 数独是一个非常有趣味性的智力游戏, ...
- C++手动开O2优化
O2优化能使程序的编译效率大大提升. 从而减少程序的运行时间,达到优化的效果. C++程序中的O2开关如下所示: #pragma GCC optimize(2) 同理O1.O3优化只需修改括号中的数即 ...
- 洛谷U14667 肝活动【比赛】 【状压dp】
题目描述 Yume 最近在玩一个名为<LoveLive! School idol festival>的音乐游戏.他之所以喜欢上这个游戏,是因为这个游戏对非洲人十分友好,即便你脸黑到抽不出好 ...
- LINUX内核设计与实现第三周读书笔记
LINUX内核设计与实现第三周读书笔记 第一章 LINUX内核简介 1.1 Unix的历史 1969年的夏天,贝尔实验室的程序员们在一台PDR-7型机上实现了Unix这个全新的操作系统. 1973年, ...