結果

問題 No.1973 Divisor Sequence
ユーザー 👑 NachiaNachia
提出日時 2022-06-10 21:35:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 4,856 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 90,976 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 06:17:48
合計ジャッジ時間 1,633 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #


#include <vector>
#include <cassert>
#include <utility>


namespace nachia{

template<class Elem>
struct MatrixModulo{
private:
    int h;
    int w;
    std::vector<Elem> elems;

public:
    
    MatrixModulo(int new_h=0, int new_w=0){ h = new_h; w = new_w; elems.assign(h * w, 0); }
    MatrixModulo(const MatrixModulo&) = default;
    int height() const { return h; }
    int width() const { return w; }
    typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y * w); }
    typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y * w); }
    static MatrixModulo identity(int idx){ auto res = MatrixModulo(idx, idx); for(int i = 0; i < idx; i++) res[i][i] = 1; return res; }
    MatrixModulo operator*(const MatrixModulo& r) const {
        assert(width() == r.height());
        auto res = MatrixModulo(h, r.w);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) for (int k=0; k<r.w; k++) res[i][k] += (*this)[i][j] * r[j][k];
        return res;
    }
    Elem det() const {
        assert(height() == width());
        MatrixModulo g = *this;
        Elem ans = 1;
        for (int i=0; i<h; i++) {
            int tg = -1;
            for (int j=i; j<h; j++) { if (g[j][i].val() != 0) tg = j; }
            if (tg == -1) return 0;
            if (tg != i) ans = -ans;
            for (int j=0; j<h; j++) std::swap(g[i][j], g[tg][j]);
            tg = i;
            ans *= g[i][i];
            Elem const_coeff = g[i][i].inv();
            for (int j=0; j<h; j++) g[i][j] *= const_coeff;
            for (int j=i+1; j<h; j++) for(int k=h-1; k>=i; k--) g[j][k] -= g[j][i] * g[i][k];
        }
        return ans;
    }
    int rank() const {
        MatrixModulo g = *this;
        int y = 0;
        for (int d=0; d<w; d++) {
            if(y == h) break;
            int tg = -1;
            for (int i=y; i<h; i++) { if (g[i][d].val() != 0){ tg = i; break; } }
            if (tg == -1) continue;
            for (int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]);
            tg = y;
            Elem const_coeff = g[y][d].inv();
            for (int j=d; j<w; j++) g[y][j] *= const_coeff;
            for (int i=y+1; i<h; i++) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            y++;
        }
        return y;
    }
    MatrixModulo linear_equation() const {
        MatrixModulo g = *this;
        int y = 0;
        std::vector<std::pair<int,int>> det_var;
        std::vector<int> rank_var;
        for (int d=0; d<w-1; d++) {
            int tg = -1;
            for (int i=y; i<h; i++) { if (g[i][d].val() != 0){ tg = i; break; } }
            if (tg == -1){ rank_var.push_back(d); continue; }
            for (int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]);
            tg = y;
            Elem const_coeff = g[y][d].inv();
            for (int j=d; j<w; j++) g[y][j] *= const_coeff;
            for (int i=0; i<h; i++) if (i != y) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            det_var.push_back(std::make_pair(d,y));
            y++;
        }
        for (int i=y; i<h; i++) if (g[i][w-1].val() != 0) return MatrixModulo(0,0);
        MatrixModulo solution(1 + rank_var.size(), w);
        for (auto [x,i] : det_var) { solution[0][x] = -g[i][w-1]; }
        solution[0][w-1] = 1;
        for (int d=0; d<(int)rank_var.size(); d++) {
            int varid = rank_var[d];
            solution[d+1][varid] = -Elem(1);
            for (auto [x,i] : det_var) { solution[d+1][x] = g[i][varid]; }
        }
        return solution;
    }
    MatrixModulo pow(unsigned long long i){
        auto a = *this;
        auto res = identity(height());
        while(i){
            if(i % 2 == 1) res = res * a;
            a = a * a;
            i /= 2;
        }
        return res;
    }
};


} // namespace nachia

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <atcoder/modint>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)


const i64 INF = 1001001001001001001;
using modint = atcoder::static_modint<1000000007>;
using matrix = nachia::MatrixModulo<modint>;


modint f(int n, int N){
    matrix G(n+1, n+1);
    rep(i,n+1) rep(j,n+1) if(i+j <= n) G[i][j] = 1;
    G = G.pow(N);
    modint ans = 0;
    rep(i,n+1) ans += G[0][i];
    return ans;;
}


int main(){
    int N; cin >> N;
    i64 M; cin >> M;
    modint ans = 1;
    for(i64 x=2; x*x<=M; x++) if(M%x == 0){
        int t = 0;
        while(M%x==0){ t++; M/=x; }
        ans *= f(t, N);
    }
    if(M != 1) ans *= f(1, N);
    cout << ans.val() << '\n';
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0