結果

問題 No.260 世界のなんとか3
ユーザー kimiyukikimiyuki
提出日時 2017-01-08 17:59:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 344 ms / 2,000 ms
コード長 2,557 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 86,444 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 17:39:20
合計ジャッジ時間 5,730 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 275 ms
4,380 KB
testcase_04 AC 276 ms
4,376 KB
testcase_05 AC 53 ms
4,376 KB
testcase_06 AC 36 ms
4,380 KB
testcase_07 AC 187 ms
4,380 KB
testcase_08 AC 130 ms
4,376 KB
testcase_09 AC 72 ms
4,380 KB
testcase_10 AC 211 ms
4,380 KB
testcase_11 AC 197 ms
4,376 KB
testcase_12 AC 119 ms
4,376 KB
testcase_13 AC 34 ms
4,380 KB
testcase_14 AC 178 ms
4,376 KB
testcase_15 AC 48 ms
4,380 KB
testcase_16 AC 157 ms
4,376 KB
testcase_17 AC 123 ms
4,380 KB
testcase_18 AC 119 ms
4,376 KB
testcase_19 AC 155 ms
4,376 KB
testcase_20 AC 109 ms
4,376 KB
testcase_21 AC 97 ms
4,380 KB
testcase_22 AC 179 ms
4,376 KB
testcase_23 AC 18 ms
4,376 KB
testcase_24 AC 138 ms
4,380 KB
testcase_25 AC 172 ms
4,380 KB
testcase_26 AC 104 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 206 ms
4,376 KB
testcase_29 AC 344 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }
ll gcd(ll a, ll b) { while (a) { b %= a; swap(a, b); } return b; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); }
// strmodin(s, a, b, m)[i][j] = the number of digits t in mod m  for  t \le s, i = t \bmod \prod a and t contains digits j \subset b
vector<vector<ll> > strmodin(string const & s, vector<int> const & a, vector<int> const & b, ll m) {
    int al = whole(accumulate, a, 1ll, lcm);
    int bl = 1ll << b.size();
    auto cur = vectors(2, bl, al, ll());
    auto prv = vectors(2, bl, al, ll());
    cur[0][0][0] = 1;
    for (char c : s) {
        cur.swap(prv);
        repeat (p,2) repeat (j,bl) repeat (i,al) {
            repeat (d,10) {
                bool np;
                if (d < (c-'0') or p) np = true;
                else if (d == (c-'0')) np = false;
                else continue;
                int nj = j; {
                    auto it = whole(find, b, d);
                    if (it != b.end()) nj |= 1 << (it - b.begin());
                }
                int ni = (i * 10 + d) % al;
                ll & it = cur[np][nj][ni];
                it = (it + prv[p][j][i]) % m;
            }
            prv[p][j][i] = 0;
        }
    }
    auto result = vectors(al, bl, ll());
    repeat (p,2) repeat (j,bl) repeat (i,al) {
        result[i][j] += cur[p][j][i];
        result[i][j] %= m;
    }
    return result;
}
const int mod = 1e9+7;
ll f(string const & s) {
    auto dp = strmodin(s, { 3, 8 }, { 3 }, mod);
    ll cnt = 0;
    repeat (i,3*8) repeat (j,2) {
        if ((i % 3 == 0 or j) and i % 8 != 0) {
            cnt += dp[i][j];
            cnt %= mod;
        }
    }
    return cnt;
}
bool g(string const & s) {
    auto modstr = [&](ll m) {
        ll q = 0;
        for (char c : s) q = (q * 10 + (c - '0')) % m;
        return q;
    };
    return (modstr(3) == 0 or whole(count, s, '3')) and modstr(8) != 0;
}
int main() {
    string a, b; cin >> a >> b;
    cout << ((f(b) - f(a) + g(a)) % mod + mod) % mod << endl;
    return 0;
}
0