为何只AC 1~4 共40分
查看原帖
为何只AC 1~4 共40分
1495012
Wsl886楼主2025/7/21 18:52

各位dalaodalao帮帮忙

OrzOrz

#include<bits/stdc++.h>
#define int long long
using namespace std;
int BSGS(int a, int b, int p, int c) {
    map<int, int> mp; mp.clear();
    if(b==1) return 0;
    int t = (int)sqrt(p) + 1;
    int sum = 1;  
    for (int j = 0; j < t; j++) {
        int key = (b * sum) % p;
        if (mp[key] == 0) {
            mp[key] = j;
        }
        sum = sum * 1LL * a % p;  
    }
    int a_t = sum;  
    sum = c;  
    for (int i = 0; i <= t; i++) {
        if (mp[sum]) {
            int j = mp[sum];
            int x = i * t - j;
            if (x >= 0) {  
                return x;
            }
        }
        sum = sum * 1LL * a_t % p;  
    }
    return -1; 
}
int EXBSGS(int a, int b, int p) {
    a %= p;
    b %= p;
	if(b==1||p==1) return 0;
    int x = 1;  
    int cnt = 0;  
    while (true) {
        int d = __gcd(a, p);
        if (d == 1) break;  
        if (b % d != 0) return -1;
        b /= d;
        p /= d;       
		if (b == x) return cnt;
        ++cnt;
        x = x * 1LL * (a / d) % p;  
    }
    int ret = BSGS(a, b, p, x);
    if (ret == -1) return -1;
    return ret + cnt;  
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int a, b, p;
    while (cin >> a >> p >> b) {
        if (a == 0 && b == 0 && p == 0) break;
        int ans = EXBSGS(a, b, p);
        if (ans == -1) cout << "No Solution\n";
        else cout << ans << "\n";
    }
    return 0;
}
2025/7/21 18:52
加载中...