QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#584048#9248. An Easy Math Problemhos_lyricAC ✓8ms4084kbC++144.2kb2024-09-23 04:54:152024-10-31 22:07:03

Judging History

你现在查看的是测评时间为 2024-10-31 22:07:03 的历史记录

  • [2024-10-31 22:40:46]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:7ms
  • 内存:4088kb
  • [2024-10-31 22:36:43]
  • hack成功,自动添加数据
  • (/hack/1098)
  • [2024-10-31 22:17:51]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:100
  • 用时:8ms
  • 内存:3832kb
  • [2024-10-31 22:13:58]
  • hack成功,自动添加数据
  • (/hack/1096)
  • [2024-10-31 22:07:03]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:100
  • 用时:8ms
  • 内存:4084kb
  • [2024-10-31 22:00:43]
  • hack成功,自动添加数据
  • (/hack/1095)
  • [2024-09-23 04:54:16]
  • 评测
  • 测评结果:100
  • 用时:7ms
  • 内存:4076kb
  • [2024-09-23 04:54:15]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


template <class T> T power(T a, long long e, T m) {
  for (T b = 1; ; (a *= a) %= m) {
    if (e & 1) (b *= a) %= m;
    if (!(e >>= 1)) return b;
  }
}

long long gcd(long long a, long long b) {
  if (a < 0) a = -a;
  if (b < 0) b = -b;
  if (a == 0) return b;
  if (b == 0) return a;
  const int s = __builtin_ctzll(a | b);
  a >>= __builtin_ctzll(a);
  do {
    b >>= __builtin_ctzll(b);
    if (a > b) swap(a, b);
    b -= a;
  } while (b);
  return a << s;
}

bool isPrime(long long n) {
  if (n <= 1 || n % 2 == 0) return (n == 2);
  const int s = __builtin_ctzll(n - 1);
  const long long d = (n - 1) >> s;
  // http://miller-rabin.appspot.com/
  for (const long long base : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
    __int128 a = base % n;
    if (a == 0) continue;
    a = power<__int128>(a, d, n);
    if (a == 1 || a == n - 1) continue;
    bool ok = false;
    for (int i = 0; i < s - 1; ++i) {
      (a *= a) %= n;
      if (a == n - 1) {
        ok = true;
        break;
      }
    }
    if (!ok) return false;
  }
  return true;
}

// n >= 3, odd
void factorizeRec(long long n, vector<long long> &ps) {
  static constexpr int BLOCK = 256;
  if (isPrime(n)) {
    ps.push_back(n);
  } else {
    for (long long c = 2; ; ++c) {
      long long x, y = 2, y0, z = 1, d = 1;
      for (int l = 1; d == 1; l <<= 1) {
        x = y;
        for (int i = 0; i < l; ++i) y = (static_cast<__int128>(y) * y + c) % n;
        for (int i = 0; i < l; i += BLOCK) {
          y0 = y;
          for (int j = 0; j < BLOCK && j < l - i; ++j) {
            y = (static_cast<__int128>(y) * y + c) % n;
            z = (static_cast<__int128>(z) * (y - x)) % n;
          }
          if ((d = gcd(z, n)) != 1) break;
        }
      }
      if (d == n) {
        for (y = y0; ; ) {
          y = (static_cast<__int128>(y) * y + c) % n;
          if ((d = gcd(y - x, n)) != 1) break;
        }
      }
      if (d != n) {
        factorizeRec(d, ps);
        factorizeRec(n / d, ps);
        return;
      }
    }
  }
}

vector<pair<long long, int>> factorize(long long n) {
  vector<pair<long long, int>> pes;
  if (n >= 2) {
    const int s = __builtin_ctzll(n);
    if (s) pes.emplace_back(2, s);
    if (n >> s >= 2) {
      vector<long long> ps;
      factorizeRec(n >> s, ps);
      std::sort(ps.begin(), ps.end());
      const int psLen = ps.size();
      for (int i = 0, j = 0; i < psLen; i = j) {
        for (; j < psLen && ps[i] == ps[j]; ++j) {}
        pes.emplace_back(ps[i], j - i);
      }
    }
  }
  return pes;
}


int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    Int N;
    scanf("%lld", &N);
    
    const auto pes = factorize(N);
    Int ans = 1;
    for (const auto &pe : pes) {
      const Int e = pe.second;
      ans *= (2 * e + 1);
    }
    ans = (ans + 1) / 2;
    printf("%lld\n", ans);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3808kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
2
3
2
5
2
4
3
5

result:

ok 10 lines

Test #2:

score: 0
Accepted
time: 7ms
memory: 4084kb

input:

2000
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
646969323...

output:

29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
29525
...

result:

ok 2000 lines

Test #3:

score: 0
Accepted
time: 8ms
memory: 3812kb

input:

2000
1763047095
79735483
1016286871
2864801397
2327774116
2668010360
3469893354
3634459021
1613699068
781737219
574741575
2763134701
1458502604
1822260248
2281150332
2924219311
2493931196
3735904708
158802001
2006921221
729928782
1974841034
727412600
2873393292
1291087179
2741607663
1893408215
29827...

output:

14
5
2
5
23
95
68
14
8
68
203
14
23
32
38
41
8
8
14
2
608
41
158
338
23
41
14
5
14
41
14
203
41
14
17
446
5
53
59
878
2
14
365
203
14
203
2
122
32
95
41
41
5
23
14
41
5
5
14
122
23
203
608
23
41
122
2
14
95
2
68
41
203
14
230
41
68
23
50
14
32
14
8
5
5
5
68
68
122
293
473
5
41
41
14
2
14
14
5
2
122
...

result:

ok 2000 lines

Extra Test:

score: 0
Extra Test Passed