欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

loj#2531. 「CQOI2018」破解 D-H 协议(BSGS)

程序员文章站 2022-04-08 19:13:54
题意 "题目链接" Sol 搞个BSGS板子出题人也是很棒棒哦 cpp include define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second define int long long def ......

题意

sol

搞个bsgs板子出题人也是很棒棒哦

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, inf = 1e9 + 10;;
int mod;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
template <typename a, typename b> inline ll fp(a a, b p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename a> a inv(a x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int g;
map<int, int> mp;
int solve(int x) {//g^ret = x (mod p)
    mp.clear(); int block = ceil(sqrt(mod)), base = fp(g, block);
    for(int i = 0, cur = x; i <= block; i++, mul2(cur, g)) mp[cur] = i;
    for(int i = 1, cur = base; i <= block; i++, mul2(cur, base)) if(mp[cur]) return i * block - mp[cur];    
    return 0;
}
/*
int solve(int x) {
    int now = 1;
    for(int i = 0; i<= mod; i++) {
        if(now == x) return i;
        mul2(now, g);
    }
    assert(1 == 2);
}
*/
signed main() {
    //freopen("a.in", "r", stdin);
    g = read(); mod = read();
    int n = read();
    while(n--) {
        int a = read(), b = read();
        cout << fp(g, solve(a) * solve(b)) << '\n';;
    }
    return 0;
}