zoukankan      html  css  js  c++  java
  • loj#2531. 「CQOI2018」破解 D-H 协议(BSGS)

    题意

    题目链接

    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 << '
    ';}
    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)) << '
    ';;
    	}
    	return 0;
    }
    
  • 相关阅读:
    浏览器基本的工作原理
    ES6异步操作之Promise
    vux中x-input在安卓手机输入框的删除按钮(@on-click-clear-icon)点击没反应
    浏览器工作原理
    form表单的两种提交方式,submit和button的用法
    HTML DOM submit() 方法
    JavaScript test() 方法
    eval() 函数
    正则表达式
    onblur 事件
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10596157.html
Copyright © 2011-2022 走看看