結果
問題 |
No.260 世界のなんとか3
|
ユーザー |
![]() |
提出日時 | 2017-01-21 21:02:28 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 493 ms / 2,000 ms |
コード長 | 3,517 bytes |
コンパイル時間 | 1,014 ms |
コンパイル使用メモリ | 75,308 KB |
実行使用メモリ | 10,508 KB |
最終ジャッジ日時 | 2024-12-23 04:29:22 |
合計ジャッジ時間 | 7,202 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <cstdio> #include <vector> #include <functional> #include <cstdint> #include <string> #include <utility> using namespace std; class DigitDP { using Func=function<size_t (size_t, size_t)>; vector<Func> funcs; vector<size_t> ubs; size_t size, numfuncs, n; int64_t mod; vector<int64_t> dp; void modify_table( const string &number, size_t pos, size_t tight, vector<int64_t> &dp, vector<size_t> &indices, size_t depth=0 ) { if (depth == numfuncs) { size_t ub=tight? 9:(number[pos]-'0'); for (size_t d=0; d<=ub; ++d) { size_t next=pos+1, prev=pos; (next <<= 1) |= (tight || d < ub); (prev <<= 1) |= tight; for (size_t i=0; i<numfuncs; ++i) { (next *= ubs[i]) += funcs[i](indices[i], d); (prev *= ubs[i]) += indices[i]; } (dp[next] += dp[prev]) %= mod; } return; } for (indices[depth]=0; indices[depth]<ubs[depth]; ++indices[depth]) modify_table(number, pos, tight, dp, indices, depth+1); } template <class Functor> void lookup_table( vector<size_t> &indices, Functor satisfies, pair<int64_t, int64_t> &res, size_t depth=0 ) { if (depth == numfuncs) { if (!satisfies(indices)) return; size_t lt=(n<<1), eq=(n<<1|1); for (size_t i=0; i<depth; ++i) { (lt *= ubs[i]) += indices[i]; (eq *= ubs[i]) += indices[i]; } (res.first += dp[eq]) %= mod; (res.second += dp[lt]) %= mod; return; } for (indices[depth]=0; indices[depth]<ubs[depth]; ++indices[depth]) lookup_table(indices, satisfies, res, depth+1); } public: DigitDP(int64_t mod): size(2), numfuncs(0), mod(mod) {} template <class Functor> pair<int64_t, int64_t> count_up(Functor satisfies) { vector<size_t> indices(numfuncs); pair<int64_t, int64_t> res; lookup_table(indices, satisfies, res); (res.second += res.first) %= mod; return res; } void append(size_t ub, const Func &&func) { size *= ub; ++numfuncs; ubs.push_back(ub); funcs.push_back(func); } void store(const string &number) { n = number.length(); dp.assign(size*(n+1), 0); dp[0]=1; vector<size_t> indices(numfuncs); for (size_t pos=0; pos<n; ++pos) for (size_t tight=0; tight<2; ++tight) modify_table(number, pos, tight, dp, indices); } }; int64_t count(const string &A, const string &B, int64_t mod=1e9+7) { DigitDP d(mod); d.append(2, [](size_t k, size_t d)->size_t { return k || d == 3; }); d.append(3, [](size_t l, size_t d)->size_t { return (l + d) % 3; }); d.append(8, [](size_t m, size_t d)->size_t { return (m * 10 + d) % 8; }); int64_t res=0; auto satisfies=[](const vector<size_t> &v)->bool { return (v[0] || v[1] == 0) && v[2] != 0; }; d.store(B); res += d.count_up(satisfies).second; d.store(A); res -= d.count_up(satisfies).first; return (res += mod) % mod; } int main() { string A, B; { char tmp[16384]; scanf("%s", tmp); A = tmp; scanf("%s", tmp); B = tmp; } printf("%lld\n", count(A, B)); return 0; }