Submission #3259523

Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> llll;
typedef pair<double, double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a) int((a).size())
#define pb push_back
#define FOR(var, from, to) for (int var = (from); var <= (to); ++var)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define rep1(var, n) for (int var = 1; var <= (n); ++var)
#define repC2(vari, varj, n)               \
  for (int vari = 0; vari < (n)-1; ++vari) \
    for (int varj = vari + 1; varj < (n); ++varj)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define tr(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define found(s, e) ((s).find(e) != (s).end())
#define mset(arr, val) memset(arr, val, sizeof(arr))
#define mid(x, y) ((x) + ((y) - (x)) / 2)
#define IN(x, a, b) ((a) <= (x) && (x) <= (b))
#define cons make_pair

ll gcd(ll a, ll b) {
  while (a) swap(a, b %= a);
  return b;
}

const ll MOD = 1000000007LL;

ll ADD(ll x, ll y) { return (x + y) % MOD; }
ll SUB(ll x, ll y) { return (x - y + MOD) % MOD; }
ll MUL(ll x, ll y) { return x * y % MOD; }
ll POW(ll x, ll e) {
  ll v = 1;
  for (; e; x = MUL(x, x), e >>= 1)
    if (e & 1) v = MUL(v, x);
  return v;
}
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/
  return MUL(x, POW(y, MOD - 2));
}
ll comb(ll n, ll k) { ll v=1; for(ll i=1; i<=k; i++) v = DIV(MUL(v, n-i+1),i); return v; }

#include <vector>
using namespace std;

vector<int> primes;
vector<int> smallest_prime_factor;

int sieve(int nmax) {
  primes.clear();
  smallest_prime_factor.assign(nmax + 1, 0);

  // ここではあえて愚直な書き方
  for (int n = 2; n <= nmax; ++n) {
    if (!smallest_prime_factor[n]) {
      primes.push_back(n);
      for (int kn = n; kn <= nmax; kn += n) {
        if (!smallest_prime_factor[kn]) {  // ←このチェックをせずに上書きを続けると largest_prime_factor[]
                                           // が出来上がる
          smallest_prime_factor[kn] = n;
        }
      }
    }
  }
  return primes.size();
}

vector<ii> factorize(int M) {
  sieve(32000);
  vector<ii> res;
      // int sq = sqrt(M);
      int x = M;
  for (int p : primes) {
    int cnt = 0;
    while (x % p == 0) { x /= p; ++cnt; }
    if (cnt) res.pb(ii(p, cnt));
  }
  if (x != 1) res.pb(ii(x, 1));
  return res;
}

ll solve(int N, int M) {
  vector<ii> fac = factorize(M);
  int F = fac.size();
#ifdef DEBUG
  cerr << M << fac << F << endl;
#endif
  ll ans = 1;

  for (ii p : fac) {
    int k = p.second;
    ll c = comb(N-1+k, N-1);
    ans = MUL(ans, c);
  }
  return ans;
}

int main() {
  int N, M;
  cin >> N >> M;
  cout << solve(N, M) << endl;
  // fprintf(stderr, "[[ %d %d ]]\n", _s_pow2, _assign_mul);
  return 0;
}

Submission Info

Submission Time
Task D - Factorization
User naoya_t
Language C++14 (GCC 5.4.1)
Score 400
Code Size 3261 Byte
Status
Exec Time 139 ms
Memory 384 KB

Test Cases

Set Name Score / Max Score Test Cases
All 400 / 400 0_small_1, 0_small_2, 0_small_3, 1_large_1, 1_large_2, 1_large_3, 2_large_1, 2_large_2, 3_prime_1, 3_prime_10, 3_prime_11, 3_prime_12, 3_prime_13, 3_prime_14, 3_prime_15, 3_prime_16, 3_prime_17, 3_prime_18, 3_prime_19, 3_prime_2, 3_prime_20, 3_prime_21, 3_prime_22, 3_prime_3, 3_prime_4, 3_prime_5, 3_prime_6, 3_prime_7, 3_prime_8, 3_prime_9, 4_hand_1, 4_hand_2, 4_hand_3, sample_01, sample_02, sample_03
Sample 0 / 0 sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
0_small_1 2 ms 384 KB
0_small_2 2 ms 384 KB
0_small_3 2 ms 384 KB
1_large_1 43 ms 384 KB
1_large_2 16 ms 384 KB
1_large_3 59 ms 384 KB
2_large_1 63 ms 384 KB
2_large_2 47 ms 384 KB
3_prime_1 78 ms 384 KB
3_prime_10 139 ms 384 KB
3_prime_11 139 ms 384 KB
3_prime_12 139 ms 384 KB
3_prime_13 139 ms 384 KB
3_prime_14 139 ms 384 KB
3_prime_15 139 ms 384 KB
3_prime_16 139 ms 384 KB
3_prime_17 139 ms 384 KB
3_prime_18 139 ms 384 KB
3_prime_19 124 ms 384 KB
3_prime_2 78 ms 384 KB
3_prime_20 124 ms 384 KB
3_prime_21 109 ms 384 KB
3_prime_22 17 ms 384 KB
3_prime_3 63 ms 384 KB
3_prime_4 78 ms 384 KB
3_prime_5 78 ms 384 KB
3_prime_6 109 ms 384 KB
3_prime_7 93 ms 384 KB
3_prime_8 93 ms 384 KB
3_prime_9 139 ms 384 KB
4_hand_1 47 ms 384 KB
4_hand_2 1 ms 384 KB
4_hand_3 1 ms 384 KB
sample_01 1 ms 384 KB
sample_02 1 ms 384 KB
sample_03 32 ms 384 KB