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

题目:

代码实现如下:

 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

时间: 2024-10-13 11:36:18

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

【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 每个测试文件中最多包含100组测试数据. 每组数据中,每行包含3个正整数a,p,b. 当a=p=b=0时,表示测试数据读入完全. Output 对于每组数据,输出一行. 如果无解,输出“No Solution”(不含引号),否则输出最小自然数解. Sample Input 5 58 33 2 4 3 0 0

POJ 3243 Clever Y BSGS

Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6861   Accepted: 1676 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, give

【EXT-BSGS算法求离散对数】POJ Clever Y 3243

Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7259 Accepted: 1795 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, given X,

Clever Y POJ - 3243 (扩展BSGS)

Clever Y POJ - 3243 题意:给a,c,b,求最小的x使得 ax≡b (mod c). 扩展BSGS算法~ 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #define ll long long 6 using namespace std; 7 const int mod=99991; 8 ll head[mod],nex

poj 3243 Clever Y 高次方程

1 Accepted 8508K 579MS C++ 2237B/** 2 hash的强大,,还是高次方程,不过要求n不一定是素数 3 **/ 4 #include <iostream> 5 #include <cstdio> 6 #include <cmath> 7 #include <cstring> 8 #include <algorithm> 9 using namespace std; 10 long long a,b,n; 11 co

【POJ3243】【拓展BSGS】Clever Y

Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, could you figure out Y fast? Input Input data consists of no more than 20 test ca

【数论】【ex-BSGS】poj3243 Clever Y

用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题的坑点请去看discuss. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; vo

POJ3243 Clever Y【高次同余方程】

题目链接: http://poj.org/problem?id=3243 题目大意: 已知公式A^x mod C= B,以及A.C.B的值,求解x的值为多少. 思路: 典型的求解方程A^x = B(mod C),直接模板解决. AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #define LL __in

BZOJ 1467 Pku3243 clever Y EXBSGS

题意:链接 方法: EXBSGS 解析: 这题与BSGS不同的地方就是模数可能不是质数了. 那怎么办呢? 其实也没什么,就是我们不断地分解A和当前的C的最大公约数,注意是当前的C. 假设我们最多分出来了K个最大公约数. Akd1?d2-?dk?Ax?k=Bd1?d2-?dk(modCd1?d2-?dk) 那么怎么做呢? 首先暴力枚举[0,logC]的值是否可以成为解. 为什么取log呢?因为每一次拿出来的最大公约数一定是大于1的,也就是说我们至多拿出来了logC个数,此时的k就是logC,而我们