思路:

  前两题题面相同,代码也相同,就只贴一题的题面了。这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题。

SPOJ3105题目链接:https://www.spoj.com/problems/MOD/

POJ3243题目链接:http://poj.org/problem?id=3243

题目:

代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <stack>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 #include <algorithm>
14 #include <unordered_map>
15 using namespace std;
16
17 typedef long long LL;
18 typedef pair<LL, LL> pLL;
19 typedef pair<LL, int> pli;
20 typedef pair<int, LL> pil;;
21 typedef pair<int, int> pii;
22 typedef unsigned long long uLL;
23
24 #define lson i<<1
25 #define rson i<<1|1
26 #define lowbit(x) x&(-x)
27 #define bug printf("*********\n");
28 #define debug(x) cout<<"["<<x<<"]" <<endl;
29 #define FIN freopen("D://code//in.txt", "r", stdin);
30 #define IO ios::sync_with_stdio(false),cin.tie(0);
31
32 const double eps = 1e-8;
33 const int mod = 1e9 + 7;
34 const int maxn = 1e6 + 7;
35 const double pi = acos(-1);
36 const int inf = 0x3f3f3f3f;
37 const LL INF = 0x3f3f3f3f3f3f3f3f;
38
39 int x, z, k;
40 unordered_map<LL, int> mp;
41
42 int Mod_Pow(int x, int n, int mod) {
43     int res = 1;
44     while(n) {
45         if(n & 1) res = (LL)res * x % mod;
46         x = (LL)x * x % mod;
47         n >>= 1;
48     }
49     return res;
50 }
51
52 int gcd(int  a, int b) {
53     return b == 0 ? a : gcd(b, a % b);
54 }
55
56 int EXBSGS(int A, int B, int C) {
57     A %= C, B %= C;
58     if(B == 1) return 0;
59     int cnt = 0;
60     LL t = 1;
61     for(int g = gcd(A, C); g != 1; g = gcd(A, C)) {
62         if(B % g) return -1;
63         C /= g, B /= g, t = t * A / g % C;
64         cnt++;
65         if(B == t) return cnt;
66     }
67     mp.clear();
68     int m = ceil(sqrt(1.0*C));
69     LL base = B;
70     for(int i = 0; i < m; i++) {
71         mp[base] = i;
72         base = base * A % C;
73     }
74     base = Mod_Pow(A, m, C);
75     LL nw = t;
76     for(int i = 1; i <= m; i++) {
77         nw = nw * base % C;
78         if(mp.count(nw)) {
79             return i * m - mp[nw] + cnt;
80         }
81     }
82     return -1;
83 }
84
85 int main() {
86     //FIN;
87     while(~scanf("%d%d%d", &x, &z, &k)) {
88         if(x == 0 && z == 0 && k == 0) break;
89         int ans = EXBSGS(x, k, z);
90         if(ans == -1) printf("No Solution\n");
91         else printf("%d\n", ans);
92     }
93     return 0;
94 }

Gym 101853G题目链接:http://codeforces.com/gym/101853/problem/G

代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <stack>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 #include <algorithm>
14 #include <unordered_map>
15 using namespace std;
16
17 typedef long long LL;
18 typedef pair<LL, LL> pLL;
19 typedef pair<LL, int> pli;
20 typedef pair<int, LL> pil;;
21 typedef pair<int, int> pii;
22 typedef unsigned long long uLL;
23
24 #define lson i<<1
25 #define rson i<<1|1
26 #define lowbit(x) x&(-x)
27 #define bug printf("*********\n");
28 #define debug(x) cout<<"["<<x<<"]" <<endl;
29 #define FIN freopen("D://code//in.txt", "r", stdin);
30 #define IO ios::sync_with_stdio(false),cin.tie(0);
31
32 const double eps = 1e-8;
33 const int mod = 1e9 + 7;
34 const int maxn = 1e6 + 7;
35 const double pi = acos(-1);
36 const int inf = 0x3f3f3f3f;
37 const LL INF = 0x3f3f3f3f3f3f3f3f;
38
39 int t, a, b, m;
40 unordered_map<LL, int> mp;
41
42 LL Mod_Pow(LL x, LL n, LL mod) {
43     LL res = 1;
44     while(n) {
45         if(n & 1) res = res * x % mod;
46         x = x * x % mod;
47         n >>= 1;
48     }
49     return res;
50 }
51
52 int gcd(int a, int b) {
53     return b == 0 ? a : gcd(b, a % b);
54 }
55
56 LL EXBSGS(int A, int B, int C) {
57     A %= C, B %= C;
58     if(B == 1) return 0;
59     int cnt = 0;
60     LL t = 1;
61     for(int g = gcd(A, C); g != 1; g = gcd(A, C)) {
62         if(B % g) return -1;
63         C /= g, B /= g;
64         t = t * A / g % C;
65         cnt++;
66         if(B == t) return cnt;
67     }
68     mp.clear();
69     int m = ceil(sqrt(1.0 * C));
70     LL base = B;
71     for(int i = 0; i < m; i++) {
72        mp[base] = i;
73        base = base * A % C;
74     }
75     base = Mod_Pow(A, m, C);
76     LL nw = t;
77     for(int i = 1; i <= m + 1; i++) {
78         nw = base * nw % C;
79         if(mp.count(nw)) {
80             return i * m - mp[nw] + cnt;
81         }
82     }
83     return -1;
84 }
85
86 int main() {
87     scanf("%d", &t);
88     while(t--) {
89         scanf("%d%d%d", &a, &b, &m);
90         LL ans = EXBSGS(a, b, m);
91         printf("%lld\n", ans);
92     }
93     return 0;
94 }

转载于:https://www.cnblogs.com/Dillonh/p/9512030.html

MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS相关推荐

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

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

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

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

  3. POJ3243 Clever Y 解 高次同余方程

    解高次同余方程A^x≡B(mod C)算法流程 S1:i从0到100循环,如果满足A^i≡B(mod C),那么i就为所求,否则继续S2: S2:令d=0,D=1,执行如下循环: while((tmp ...

  4. poj 3243 Clever Y(Baby-Step Giant-Step)

    http://poj.org/problem?id=3243 继续做一下BSGS的题,不过这题有点表述不清,我认为题目应该描述为XY mod Z ≡ K,因为的这题AC算法是不用判断余数是否大于模的. ...

  5. BZOJ 1467 Pku3243 clever Y EXBSGS

    题意:链接 方法: EXBSGS 解析: 这题与BSGS不同的地方就是模数可能不是质数了. 那怎么办呢? 其实也没什么,就是我们不断地分解A和当前的C的最大公约数,注意是当前的C. 假设我们最多分出来 ...

  6. poj 3243 Clever Y

    转载请注明出处,谢谢http://blog.csdn.net/bigtiao097?viewmode=contents 题意: 给定A.B.C,求 Ax≡B(modC) A^x \equiv B( m ...

  7. 【POJ 3243】Clever Y 拓展BSGS

    调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说"拓展BSGS是偏题,省选不会考,信我 ...

  8. 有趣题目和认知合集(持续更新)

    写写对一些算法的理解,挂几个有意思的题,可能也会挂几个板子题 算法理解偏向于能懂即可,没有严格的证明 快乐几何 [1.2]Volatile Kite 点到直线 快乐搜与暴力 [2.4]Short Co ...

  9. [数论系列一]C Looooops,跳跳棋,The Luckiest number,CF906D Power Tower,Minimal Power of Prime,仪仗队,LCMSUM

    文章目录 C Looooops description solution code 跳跳棋 description solution code The Luckiest number descript ...

最新文章

  1. 爬虫之selenium标签页的切换
  2. MapReduce的统计和排序功能
  3. JavaScript中 DOM操作方法
  4. 打印http地址打印双斜杠
  5. mysql数据库的介绍及安装
  6. css js 兼容问题
  7. 《易学C++(第2版)》——1.10 习题
  8. PAT_B_1009_Java(20分)
  9. Linux 简单打印日志(二)
  10. 怎么将翼型导入catia_CATIA导入翼型出现了问题,翼型是在网上找的。说是样条线运算有问题 - 机械 - 小木虫 - 学术 科研 互动社区...
  11. 3-服务器端添加客户端事件
  12. BorderLayout布局管理器设置3个按钮
  13. Python高级编专题 - 类的创建与销毁
  14. Python3 - 文件处理
  15. JAVA简介及环境配置(复习)
  16. 全国大学生计算机ms系统,全国计算机等级考试一级计算机基础及MS Office应用模拟练习系统...
  17. python xlwt_python中使用 xlwt 操作excel的常见方法与问题
  18. 工作督办系统功能开发设计
  19. 电话簿程序设计c语言,电话簿管理程序设计.doc
  20. 经典游戏的⑨⑨⑨个隐藏系统

热门文章

  1. 并发服务器模型——单进程服务器
  2. Win7下VS2008进入维护模式不能显示升级输入框,无法升级到正式版~~~!!!
  3. 关于汇编程序编译的一些详细步骤和要点
  4. 【opencv学习】【Canny边缘检测】
  5. 知识图谱的概念、应用与构建
  6. access vba在新建查询前查看查询名是否存在
  7. 利用制表位快速居中对齐公式,同时公式编号靠右对齐
  8. 张正友相机标定程序实现
  9. java http 下载_Java 通过 HTTP 下载文件
  10. toj 4597 字符识别?