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 ...
随机推荐
- 查看sqlserver数据库的编码格式
查询语句:SELECT COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage'): 查询结果: 936 简体中文GBK 950 ...
- CNN误差反传时旋转卷积核的简明分析(转)
CNN(卷积神经网络)的误差反传(error back propagation)中有一个非常关键的的步骤就是将某个卷积(Convolve)层的误差传到前一层的池化(Pool)层上,因为在CNN中是2D ...
- Django之logging日志
简介 Django使用python自带的logging 作为日志打印工具.简单介绍下logging. logging 是线程安全的,其主要由4部分组成: Logger 用户使用的直接接口,将日志传递给 ...
- solr源码分析之solrclound
一.简介 SolrCloud是Solr4.0版本以后基于Solr和Zookeeper的分布式搜索方案.SolrCloud是Solr的基于Zookeeper一种部署方式.Solr可以以多种方式部署,例如 ...
- java网络编程(二)可中断套接字
参考资料:java核心技术 卷II 为中断套接字操作,可使用java.nio包提供的SocketChannel类.可以使用如下方式打开SocketChannel: SocketChannel chan ...
- Django 2.0 学习(18):Django 缓存、信号和extra
Django 缓存.信号和extra Django 缓存 由于Django是动态网站,所以每次请求均会去数据库进行相应的操作,当程序访问量大时,耗时必然会显著增加.最简单的解决方法是:使用缓存,缓存将 ...
- BZOJ3811 玛里苟斯(线性基+概率期望)
k=1的话非常好做,每个有1的位都有一半可能性提供贡献.由组合数的一些性质非常容易证明. k=2的话,平方的式子展开可以发现要计算的是每一对位提供的贡献,于是需要计算每一对位被同时选中的概率.找出所有 ...
- 洛谷 Roy&October之取石子
题目背景 Roy和October两人在玩一个取石子的游戏. 题目描述 游戏规则是这样的:共有n个石子,两人每次都只能取pk 个(p为质数,k为自然数,且pk小于等于当前剩余石子数),谁取走最后一个石子 ...
- 【刷题】HDU 4966 GGS-DDU
Problem Description Do you think this is a strange problem name? That is because you don't know its ...
- 【WPF】日常笔记(持续更新)
本文专用于记录WPF开发中的小细节,作为备忘录使用. 1. 关于绑定: Text ="{Binding AnchorageValue,Mode=TwoWay,UpdateSourceTrig ...