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 ...
随机推荐
- 对象库(UI MAP)
目的:能够使用配置文件存储被测页面上元素的定位方式和定位表达式,做到定位数据和程序的分离. 测试程序写好以后,可以方便不具备编码能力的测试人员进行自定义修改和配置 : package dataDriv ...
- mysql导出/导入表结构以及表数据
导出: 命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 脚本名; 1.导出数据库为dbname的表结构(其中用戶名为root,密码为dbpasswd,生成的脚 ...
- linux下sublime text 3安装到配置
1. Sublime Text 3的下载安装 到官方网站上http://www.sublimetext.com/3下载64位(系统位64位)的.deb安装包(http://c758482.r82.cf ...
- C# 模拟串口发送接收
一.准备虚拟串口驱动工具 创建俩个虚拟串口,如图: 二.创建两个控制台程序 模拟串口的发送接收数据 1. 接收数据,代码如下: //遍历串行端口名称数组 foreach (string port in ...
- equals与==区别
equals与==区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间 ...
- 实现对一个8bit数据的指定位的置0或者置1操作,并保持其他位不变。
给定函数原型:void bit_set(unsigned char *p_data,unsigned char positin,int flag) 参数说明:p_data是指定的源数据:positio ...
- BZOJ4953 Wf2017Posterize(动态规划)
设f[i][j]为前i种强度选了j种且其中第i种选时前i个的最小误差.转移枚举上个选啥前缀和优化即可. #include<iostream> #include<cstdio> ...
- [Violet]蒲公英 分块
发现写算法专题老是写不动,,,, 所以就先把我在luogu上的题解搬过来吧! 题目大意:查询区间众数,无修改,强制在线 乍一看是一道恐怖的题,仔细一看发现并没有那么难: 大致思路是这样的,首先我们要充 ...
- max os取消开机启动
原文:http://liuzhichao.com/p/1667.html 安装MAMP后,启动服务时提示Apache启动失败,80端口被占用.查看进程发现存在几个httpd. OS X自带Apache ...
- iframe在ios上不能滚动问题解决
同这篇文章一样,用到了-webkit-overflow-scrolling: touch;属性.主要解决方案是在iframe外层添加一个div,然后设置-webkit-overflow-scrolli ...