結果

問題 No.847 Divisors of Power
ユーザー sansaquasansaqua
提出日時 2019-08-05 22:34:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,003 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 74,356 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 10:13:29
合計ジャッジ時間 1,786 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>

using namespace std;
using ll = long long int;

template <class T> std::ostream& operator<<(std::ostream& o, const std::vector<T>& v) {
  o << "{";
  for (int i = 0; i < (int) v.size(); i++)
    o << (i > 0 ? ", " : "" ) << v[i];
  o << "}";
  return o;
}

template <class X, class Y> std::ostream& operator<<(std::ostream& o, const std::pair<X, Y>& p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}

const int MAX_PRIME = 100000;
bool is_prime[MAX_PRIME+1];
std::vector<int> prime;

void build_sieve () {
  // initialize
  for (int x = 0; x <= MAX_PRIME; x++)
    is_prime[x] = 1;
  
  // special treatment for 2
  is_prime[0] = 0;
  is_prime[1] = 0;
  prime.push_back(2);
  for (int x = 4; x <= MAX_PRIME; x += 2) {
    is_prime[x] = 0;
  }

  for (int p = 3; p <= MAX_PRIME; p += 2) {
    if (is_prime[p] == 1) {
      prime.push_back(p);
      for (int x = 2*p; x <= MAX_PRIME; x += p) {
	is_prime[x] = 0;
      }
    }
  }
}

std::vector<std::pair<long long, long long> > factorize (int x) {
  vector<std::pair<long long, long long> > res;
  for (auto p : prime) {
    if (x % p == 0) {
      std::pair<long long, long long> node = std::make_pair(p, 0);
      while (x % p == 0) {
	x = x / p;
	node.second++;
      }
      res.push_back(node);
    }
  }
  if (x != 1)
    res.push_back(std::make_pair(x, 1));
  return res;
}

int n, k, m;
int ans = 0;
std::vector<std::pair<ll, ll> > factors;

void dfs (ll pos, ll product) {
  if (pos == factors.size()) {
    ans++;
    return;
  }

  std::pair<ll, ll> node = factors[pos];
  ll d = product;
  for (ll j = 0; j <= node.second; j++) {
    if (d > m) break;
    dfs(pos + 1, d);
    d *= node.first;
  }
}

int main() {
  build_sieve();
  std::cin >> n >> k >> m;
  
  factors = factorize(n);
  for (int i = 0; i < factors.size(); i++) {
    factors[i].second *= k;
  }

  dfs(0, 1);
  // std::cout << factors << std::endl;
  std::cout << ans << std::endl;
  return 0;
}
0