結果

問題 No.473 和と積の和
ユーザー yosupot
提出日時 2016-12-24 15:48:37
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2,660 ms / 3,000 ms
コード長 1,901 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 103,296 KB
実行使用メモリ 55,840 KB
最終ジャッジ日時 2024-06-12 06:03:00
合計ジャッジ時間 16,492 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

/+ dub.sdl:
	name "A"
	dependency "dcomp" version=">=0.0.2"
+/
import std.stdio, std.conv, std.algorithm;
// import dcomp.scanner;

Scanner sc;
static this() {sc = new Scanner();}

int[] divlist;

int[int[3]] dp;
int calc(int N, int X, int B) {
    if (N == 0) {
        if (X == 1) return 1;
        return 0;
    }
    int[3] key = [N, X, B];
    if (key in dp) {
        return dp[key];
    }

    int ans = 0;
    foreach (d; divlist) {
        if (d < B) continue;
        if (X < d) break;
        if (X % d) continue;
        ans += calc(N-1, X/d, d);
    }
    return dp[key] = ans;
}

int main() {
    int N, X;
    sc.read(N, X);
    X++;
    for (int i = 1; i*i <= X; i++) {
        if (X % i == 0) {
            divlist ~= i;
            if (i*i != X) divlist ~= X/i;
        }
    }
    sort(divlist);
    
    writeln(calc(N, X, 2));
	return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

class Scanner {
    import std.stdio : File, readln, stdin;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeString, isDynamicArray;
    import std.algorithm : map;
    File f;
    this(File f = stdin) {
        this.f = f;
    }
    string[] buf;
    bool succ() {
        while (!buf.length) {
            if (f.eof) return false;
            buf = readln.split;
        }
        return true;
    }
    int read(Args...)(auto ref Args args) {
        foreach (i, ref v; args) {
            if (!succ()) return i;
            alias VT = typeof(v);
            static if (!isSomeString!VT && isDynamicArray!VT) {
                v = buf.map!(to!(ElementType!VT)).array;
                buf.length = 0;
            } else {
                v = buf.front.to!VT;
                buf.popFront();
            }
        }
        return args.length;
    }
}
0