3 条题解

  • 1
    @ 2025-9-8 19:07:54

    这道题我优化到最简了,可还是只能对3个样例先发出来吧,等我找到过的方法再修改

    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    #include <cstdio>
    #define ll long long
    
    inline ll read() {
        ll x = 0; char c = getchar();
        while (c < '0' || c > '9') c = getchar();
        while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
        return x;
    }
    
    inline void write(ll x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
    
    inline ll mul(ll a, ll b, ll mod) {
        return (__int128)a * b % mod;
    }
    
    inline ll pow_mod(ll a, ll b, ll mod) {
        ll res = 1;
        for (; b; b >>= 1, a = mul(a, a, mod))
            if (b & 1) res = mul(res, a, mod);
        return res;
    }
    
    int main() {
        ll p = read(), b = read(), n = read();
        
        ll phi = p - 1, c2 = 0, c3 = 0, t = phi;
        while (!(t & 1)) t >>= 1, c2++;
        while (t % 3 == 0) t /= 3, c3++;
        
        ll p2 = 1, p3 = 1;
        for (int i = 0; i < c2; i++) p2 <<= 1;
        for (int i = 0; i < c3; i++) p3 *= 3;
        
        auto solve_log = [&](ll g, ll h, ll mod, ll order) {
            ll res = 1;
            for (ll i = 0; i < order; i++) {
                if (res == h) return i;
                res = mul(res, g, mod);
            }
            return -1LL;
        };
        
        ll x1 = 0, x2 = 0;
        if (p2 > 1) {
            ll g1 = pow_mod(b, phi / p2, p);
            ll h1 = pow_mod(n, phi / p2, p);
            x1 = solve_log(g1, h1, p, p2);
            if (x1 == -1) {puts("-1"); return 0;}
        }
        if (p3 > 1) {
            ll g2 = pow_mod(b, phi / p3, p);
            ll h2 = pow_mod(n, phi / p3, p);
            x2 = solve_log(g2, h2, p, p3);
            if (x2 == -1) {puts("-1"); return 0;}
        }
        
        if (p2 == 1) write(x2);
        else if (p3 == 1) write(x1);
        else {
            for (ll i = x1; i < p2 * p3; i += p2) {
                if (i % p3 == x2) {
                    write(i);
                    return 0;
                }
            }
            puts("-1");
        }
        
        return 0;
    }
    
    • -8
      @ 2025-1-1 12:34:11
      #include <iostream>
      using namespace std;
      
      // 快速幂取模函数,计算 (base^exponent) % mod
      int fastPower(int base, int exponent, int mod) {
          int result = 1;
          base %= mod;
          while (exponent > 0) {
              if (exponent & 1) {
                  result = (result * base) % mod;
              }
              base = (base * base) % mod;
              exponent >>= 1;
          }
          return result;
      }
      
      int main() {
          int p, b, n;
          cin >> p >> b >> n;
          for (int l = 0; l < p; l++) {
              if (fastPower(b, l, p) == n) {
                  cout << l << endl;
                  return 0;
              }
          }
          cout << -1 << endl;
          return 0;
      }
      
      • -9
        @ 2024-12-29 17:23:23
        #include <iostream>
        using namespace std;
        
        // 计算最大公约数,用于判断两个数是否互质
        long long gcd(long long a, long long b) {
            return b == 0? a : gcd(b, a % b);
        }
        
        // 快速幂取模函数,用于高效计算 (a^n) % m
        long long powMod(long long a, long long n, long long m) {
            long long res = 1;
            a %= m;
            while (n > 0) {
                if (n & 1) {
                    // 如果n的二进制最低位为1,累乘当前的a
                    res = res * a % m;
                }
                a = a * a % m;
                n >>= 1;  // 右移一位,相当于n /= 2
            }
            return res;
        }
        
        int main() {
            long long p, b, n;
            cin >> p >> b >> n;
            // 判断b与p是否互质,如果不互质直接输出 -1
            if (gcd(b, p)!= 1) {
                cout << -1 << endl;
                return 0;
            }
        
            bool found = false;
            for (long long l = 1; l < p; l++) {
                if (powMod(b, l, p) == n) {
                    cout << l << endl;
                    found = true;
                    break;
                }
            }
            if (!found) {
                cout << -1 << endl;
            }
            return 0;
        }
        
      • 1

      信息

      ID
      61
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      416
      已通过
      8
      上传者