結果

問題 No.1791 Repeat Multiplication
ユーザー 👑 emthrmemthrm
提出日時 2021-12-20 03:59:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 507 ms / 3,000 ms
コード長 2,356 bytes
コンパイル時間 4,224 ms
コンパイル使用メモリ 203,784 KB
実行使用メモリ 32,580 KB
最終ジャッジ日時 2023-10-13 19:24:29
合計ジャッジ時間 14,366 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 39 ms
5,012 KB
testcase_09 AC 35 ms
4,924 KB
testcase_10 AC 34 ms
4,908 KB
testcase_11 AC 32 ms
4,632 KB
testcase_12 AC 19 ms
4,356 KB
testcase_13 AC 466 ms
30,376 KB
testcase_14 AC 474 ms
30,776 KB
testcase_15 AC 338 ms
23,232 KB
testcase_16 AC 335 ms
22,932 KB
testcase_17 AC 500 ms
31,664 KB
testcase_18 AC 406 ms
26,204 KB
testcase_19 AC 463 ms
29,780 KB
testcase_20 AC 354 ms
24,192 KB
testcase_21 AC 452 ms
29,376 KB
testcase_22 AC 490 ms
31,888 KB
testcase_23 AC 365 ms
24,892 KB
testcase_24 AC 471 ms
30,572 KB
testcase_25 AC 433 ms
28,500 KB
testcase_26 AC 484 ms
31,292 KB
testcase_27 AC 391 ms
26,160 KB
testcase_28 AC 504 ms
32,540 KB
testcase_29 AC 503 ms
32,508 KB
testcase_30 AC 507 ms
32,580 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 501 ms
32,496 KB
testcase_33 AC 501 ms
32,548 KB
testcase_34 AC 504 ms
32,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

std::vector<int> prime_sieve(int n, bool get_only_prime) {
  std::vector<int> prime, smallest_prime_factor(n + 1);
  std::iota(smallest_prime_factor.begin(), smallest_prime_factor.end(), 0);
  for (int i = 2; i <= n; ++i) {
    if (smallest_prime_factor[i] == i) prime.emplace_back(i);
    for (int p : prime) {
      if (i * p > n || p > smallest_prime_factor[i]) break;
      smallest_prime_factor[i * p] = p;
    }
  }
  return get_only_prime ? prime : smallest_prime_factor;
}

struct Divisor {
  std::vector<int> smallest_prime_factor;

  Divisor(const int n) : smallest_prime_factor(prime_sieve(n, false)) {}

  std::vector<int> query(int n) {
    std::vector<int> res{1};
    while (n > 1) {
      const int prime_factor = smallest_prime_factor[n], d = res.size();
      int tmp = 1;
      for (; n % prime_factor == 0; n /= prime_factor) {
        tmp *= prime_factor;
        for (int i = 0; i < d; ++i) {
          res.emplace_back(res[i] * tmp);
        }
      }
    }
    // std::sort(res.begin(), res.end());
    return res;
  }
};

int main() {
  int n, q; cin >> n >> q;
  Divisor divisor(n);
  vector<ll> dp1(n + 1, 0), dp2(n + 1, 1);
  dp1[1] = 1;
  FOR(i, 1, n + 1) {
    for (int j = i * 2; j <= n; j += i) dp1[j] += dp1[i];
  }
  for (int i = n; i >= 1; --i) {
    vector<int> d = divisor.query(i);
    d.pop_back();
    for (int j : d) dp2[j] += dp2[i];
  }
  while (q--) {
    int x; cin >> x;
    cout << dp1[x] * dp2[x] << '\n';
  }
  return 0;
}
0