結果

問題 No.1973 Divisor Sequence
ユーザー tnakao0123tnakao0123
提出日時 2022-06-13 00:59:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,393 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 55,240 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 19:59:06
合計ジャッジ時間 2,195 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1973.cc:  No.1973 Divisor Sequence - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 500000 + 50;
const int MAX_K = 40;
const int MAX_P = 1000000 + 100;
const int MOD = 1000000007;

/* typedef */

typedef long long ll;
typedef vector<int> vi;

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

typedef mi vec[MAX_K];
typedef vec mat[MAX_K];

/* global variables */

bool primes[MAX_P + 1];
int cache[MAX_K];

/* subroutines */

int gen_primes(int maxp, vi &pnums) {
  memset(primes, true, sizeof(primes));
  primes[0] = primes[1] = false;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums.push_back(p);
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums.push_back(p);
  return (int)pnums.size();
}

void prime_decomp(ll n, vi &pnums, vi& pds) {
  pds.clear();

  for (auto pi: pnums) {
    if ((ll)pi * pi > n) break;
    if (n % pi == 0) {
      int fi = 0;
      while (n % pi == 0) n /= pi, fi++;
      pds.push_back(fi);
    }
  }
  if (n > 1) pds.push_back(1);
}

inline void initmat(const int n, mat a) { memset(a, 0, sizeof(mat)); }

inline void unitmat(const int n, mat a) {
  initmat(n, a);
  for (int i = 0; i < n; i++) a[i][i] = 1;
}

inline void copymat(const int n, const mat a, mat b) {
  memcpy(b, a, sizeof(mat));
}

inline void mulmat(const int n, const mat a, const mat b, mat c) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      c[i][j] = 0;
      for (int k = 0; k < n; k++)
        c[i][j] += a[i][k] * b[k][j];
    }
}

inline void powmat(const int n, const mat a, int b, mat c) {
  mat s, t;
  copymat(n, a, s);
  unitmat(n, c);

  while (b > 0) {
    if (b & 1) {
      mulmat(n, c, s, t);
      copymat(n, t, c);
    }

    mulmat(n, s, s, t);
    copymat(n, t, s);
    b >>= 1;
  }
}

mi calc(int k, int n) {
  if (cache[k] >= 0) return mi(cache[k]);

  mat ma, mb;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k; j++) ma[i][j] = (i + j < k) ? 1 : 0;

  powmat(k, ma, n, mb);

  mi sum = 0;
  for (int i = 0; i < k; i++) sum += mb[i][0];

  cache[k] = sum.v;
  return sum;
}

/* main */

int main() {
  vi pnums;
  gen_primes(MAX_P, pnums);

  int n;
  ll m;
  scanf("%d%lld", &n, &m);

  vi pds;
  prime_decomp(m, pnums, pds);
  //for (auto f: pds) printf("%d ", f); putchar('\n');

  fill(cache, cache + MAX_K, -1);

  mi p = 1;
  for (auto f: pds) p *= calc(f + 1, n);

  printf("%d\n", p.v);
  return 0;
}
0