結果

問題 No.319 happy b1rthday 2 me
ユーザー nebukuro09nebukuro09
提出日時 2017-03-10 17:05:21
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,024 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 105,096 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 02:04:06
合計ジャッジ時間 1,977 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

long solve_one(long N) {
    string n = N.to!string;
    int m = n.length.to!int;
    auto dp = new long[][][][](m+1, 2, 2, 8); // 桁, n未満か, 前の数字が1か, これまで何個の12があったか
    dp[0][0][0][0] = 1;

    foreach (i; 0..m) {
        foreach (j; 0..2) {
            foreach (k; 0..2) {
                foreach (l; 0..7) {
                    int digit = j ? 10 : (n[i]-'0'+1);
                    foreach (d; 0..digit) {
                        dp[i+1][j|(d<n[i]-'0')][d==1][l+(k==1&&d==2)] += dp[i][j][k][l];
                    }
                }
            }
        }
    }

    long ret = 0;
    foreach (k; 0..2) {
        foreach (l; 1..8) {
            ret += (dp[m][1][k][l]) * l;
        }
    }
    return ret;
}

long solve_two(long N) {
    string n = N.to!string;
    int m = n.length.to!int;
    auto dp = new long[][][][](m+1, 2, 10, 2); // 桁, n未満か, 最初の桁の数字, 最後の桁が1か
    dp[0][0][0][0] = 1;

    foreach (i; 0..m) {
        foreach (j; 0..2) {
            foreach (k; 0..10) {
                foreach (l; 0..2) {
                    int digit = j ? 10 : (n[i]-'0'+1);
                    foreach (d; 0..digit) {
                        int first = (k == 0 && d != 0) ? d : k;
                        dp[i+1][j|(d<n[i]-'0')][first][i==m-1&&d==1] += dp[i][j][k][l];
                    }
                }
            }
        }
    }

    return dp[m][1][2][1];
    
}


void main() {
    auto s = readln.split;
    auto a = s[0].to!long;
    auto b = s[1].to!long;
    long ans1 = solve_one(b+1) - solve_one(a);
    long ans2 = max(0, solve_two(b) - solve_two(a));
    long ans = ans1 + ans2;
    if (a == 1 && b > 1) ans += 1;
    //if (s[1][0] == '2' && s[1][$-1] == '1') ans -= 1;
    ans.writeln;
    //ans1.writeln;
    //ans2.writeln;
}
0