思路:

  前两题题面相同,代码也相同,就只贴一题的题面了。这三题的意思都是求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的更多相关文章

  1. spoj3105 MOD - Power Modulo Inverted(exbsgs)

    传送门 关于exbsgs是个什么东东可以去看看yyb大佬的博客->这里 //minamoto #include<iostream> #include<cstdio> #i ...

  2. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  3. 「SPOJ 3105」Power Modulo Inverted

    「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...

  4. 【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      ...

  5. poj3243 Clever Y[扩展BSGS]

    Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8666   Accepted: 2155 Descript ...

  6. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

  7. bzoj 1467: Pku3243 clever Y 扩展BSGS

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...

  8. [拓展Bsgs] Clever - Y

    题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...

  9. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

随机推荐

  1. eclipse生成jar包 注意事项!

    原文转自:http://www.cnblogs.com/zhangfei/archive/2013/01/22/2871075.html 第一:普通类导出jar包,我说的普通类就是指此类包含main方 ...

  2. web.py 中文模版报错

    1. 作为模板的html文件,必须是utf-8编码; 2. html文件内容中的charset必须为utf-8,也就是必须包含 <meta http-equiv="Content-Ty ...

  3. QP(Quote-Printable) 编码

    QP(Quote-Printable)   方法,通常缩写为“Q”方法,其原理是把一个 8   bit   的字符用两个16进制数值表示,然后在前面加“=”.所以我们看到经过QP编码 后的文件通常是这 ...

  4. IIS部署时failed to execute url 解决方法

    web.config中增加如下节点: <system.webServer>  <validation validateIntegratedModeConfiguration=&quo ...

  5. postman优缺点

    postman优缺点分析 优点:门槛低,上手快 优点: 脚本语言是js 优点:自带各种代码模块 优点:跨平台 优点: 免费版就已经非常强大了,支持http,https协议 优点:有命令行版本,newm ...

  6. 【JavaScript&jQuery】$.ajax()

    $(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.json" ...

  7. [BZOJ4553][HEOI2016]序列 CDQ分治

    4553: [Tjoi2016&Heoi2016]序列 Time Limit: 20 Sec  Memory Limit: 128 MB Description 佳媛姐姐过生日的时候,她的小伙 ...

  8. MySQL5.7初始配置

    MySQL5.7初始配置 Windows7 环境安装MySQL5.7配置命令 <<<<<<<<<<<<<<<& ...

  9. 一些noip模拟题一句话题解

    Problem A: 序列 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 12  Solved: 9[Submit][Status][Web Boar ...

  10. 基于三个kinect的人体建模

       单个kinect的人体重建,在Kinect SDK 1.8中,Kinect Fusion的效果已经很不错了.其缺点显而易见,一是扫描时间长,重建对象也需要长时间保持静态:二是需要人体或者kine ...