結果
問題 | No.315 世界のなんとか3.5 |
ユーザー | 🍮かんプリン |
提出日時 | 2022-11-04 10:12:47 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 547 ms / 2,000 ms |
コード長 | 13,628 bytes |
コンパイル時間 | 3,045 ms |
コンパイル使用メモリ | 229,892 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-18 11:16:49 |
合計ジャッジ時間 | 11,151 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 4 ms
6,812 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 4 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 119 ms
6,944 KB |
testcase_13 | AC | 119 ms
6,940 KB |
testcase_14 | AC | 234 ms
6,944 KB |
testcase_15 | AC | 237 ms
6,944 KB |
testcase_16 | AC | 252 ms
6,940 KB |
testcase_17 | AC | 252 ms
6,944 KB |
testcase_18 | AC | 128 ms
6,940 KB |
testcase_19 | AC | 128 ms
6,944 KB |
testcase_20 | AC | 141 ms
6,944 KB |
testcase_21 | AC | 139 ms
6,940 KB |
testcase_22 | AC | 272 ms
6,940 KB |
testcase_23 | AC | 272 ms
6,940 KB |
testcase_24 | AC | 252 ms
6,940 KB |
testcase_25 | AC | 272 ms
6,940 KB |
testcase_26 | AC | 251 ms
6,944 KB |
testcase_27 | AC | 234 ms
6,940 KB |
testcase_28 | AC | 268 ms
6,940 KB |
testcase_29 | AC | 535 ms
6,944 KB |
testcase_30 | AC | 488 ms
6,940 KB |
testcase_31 | AC | 450 ms
6,940 KB |
testcase_32 | AC | 541 ms
6,940 KB |
testcase_33 | AC | 275 ms
6,944 KB |
testcase_34 | AC | 512 ms
6,940 KB |
testcase_35 | AC | 547 ms
6,940 KB |
コンパイルメッセージ
main.cpp: In member function 'void PartitionRefinement::print()': main.cpp:261:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 261 | for (auto [i,st] : block) { | ^ main.cpp: In function 'Automaton Minimize(const Automaton&)': main.cpp:291:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 291 | auto [c,b_id] = que.front(); que.pop(); | ^
ソースコード
/** * @FileName a.cpp * @Author kanpurin * @Created 2022.11.04 09:59:29 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; template< int MOD > struct mint { public: unsigned int x; mint() : x(0) {} mint(long long v) { long long w = (long long)(v % (long long)(MOD)); if (w < 0) w += MOD; x = (unsigned int)(w); } mint(std::string &s) { unsigned int z = 0; for (int i = 0; i < s.size(); i++) { z *= 10; z += s[i] - '0'; z %= MOD; } x = z; } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint& operator+=(const mint &a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint &a) { if ((x -= a.x) >= MOD) x += MOD; return *this; } mint& operator*=(const mint &a) { unsigned long long z = x; z *= a.x; x = (unsigned int)(z % MOD); return *this; } mint& operator/=(const mint &a) {return *this = *this * a.inv(); } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs.x == rhs.x; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs.x != rhs.x; } friend std::ostream& operator<<(std::ostream &os, const mint &n) { return os << n.x; } friend std::istream &operator>>(std::istream &is, mint &n) { unsigned int x; is >> x; n = mint(x); return is; } mint inv() const { assert(x); return pow(MOD-2); } mint pow(long long n) const { assert(0 <= n); mint p = *this, r = 1; while (n) { if (n & 1) r *= p; p *= p; n >>= 1; } return r; } mint sqrt() const { if (this->x < 2) return *this; if (this->pow((MOD-1)>>1).x != 1) return mint(0); mint b = 1, one = 1; while (b.pow((MOD-1) >> 1) == 1) b += one; long long m = MOD-1, e = 0; while (m % 2 == 0) m >>= 1, e += 1; mint x = this->pow((m - 1) >> 1); mint y = (*this) * x * x; x *= (*this); mint z = b.pow(m); while (y.x != 1) { int j = 0; mint t = y; while (t != one) j += 1, t *= t; z = z.pow(1LL << (e-j-1)); x *= z; z *= z; y *= z; e = j; } return x; } }; constexpr int MOD = 1e9 + 7; struct Monoid { using T = mint<MOD>; T val; bool undef = true; Monoid() { *this = zero(); } Monoid(T val, bool undef = true) : val(val), undef(undef) {} static Monoid zero() { return Monoid(0); } static Monoid e() { return Monoid(1,false); } Monoid& operator+=(const Monoid &a) { if (this->undef) *this = a; else if (!a.undef) this->val += a.val; return *this; } Monoid& operator*=(int c) { return *this; } friend Monoid operator+(const Monoid& a, const Monoid& b) { return Monoid(a) += b; } friend Monoid operator*(const Monoid& a, int c) { return Monoid(a) *= c; } friend std::ostream& operator<<(std::ostream &os, const Monoid &x) { return os << x.val; } }; struct Automaton { vector<vector<int>> delta; vector<bool> is_accept, is_reject; int qsize; int init; int alphabet_size = 10; inline int next(int state, int c) const { return delta[state][c]; } inline bool accept(int state) const { return is_accept[state]; } inline bool reject(int state) const { return is_reject[state]; } inline int size() const {return qsize; } }; template<typename Automaton> Monoid digitDP(const string &s, const Automaton &dfa, bool eq = 1) { vector<vector<Monoid>> dp(2,vector<Monoid>(dfa.size(),Monoid::zero())); dp[1][dfa.init] = Monoid::e(); for (int i = 0; i < s.size(); i++) { vector<vector<Monoid>> dp2(2,vector<Monoid>(dfa.size(),Monoid::zero())); for (int tight = 0; tight <= 1; tight++) { for (int state = 0; state < dfa.size(); state++) { if (dfa.reject(state) || dp[tight][state].undef) continue; int lim = (tight ? s[i] - '0' : dfa.alphabet_size - 1); for (int c = 0; c <= lim; c++) { int tight_ = tight && c == lim; int state_ = dfa.next(state,c); if (dfa.reject(state_)) continue; dp2[tight_][state_] += dp[tight][state]*c; } } } dp = move(dp2); } Monoid ans = Monoid::zero(); for (int tight = 0; tight <= eq; tight++) for (int state = 0; state < dfa.size(); state++) if (dfa.accept(state)) ans += dp[tight][state]; return ans; } struct ModuloAutomaton : public Automaton { private: int mod; void initializer() { qsize = mod; init = 0; set_delta(); set_is_accept(); set_is_reject(); } void set_delta() { delta.resize(qsize,vector<int>(alphabet_size)); for (int state = 0; state < qsize; state++) { for (int c = 0; c < alphabet_size; c++) { delta[state][c] = (state*10+c)%mod; } } } void set_is_accept() { is_accept.resize(qsize,false); is_accept[0] = true; } void set_is_reject() { is_reject.resize(qsize,false); } public: ModuloAutomaton(int mod, int alpha_size = 10) : mod(mod) { alphabet_size = alpha_size; initializer(); } }; struct PartitionRefinement { unordered_map<int,unordered_set<int>> block; vector<int> block_id; int t; PartitionRefinement(int k) : block_id(k) { for (int i = 0; i < k; i++) { block[0].insert(i); block_id[i] = 0; } t = 1; } vector<pair<int,int>> partition(vector<int> &v) { unordered_map<int,int> split; for (int i = 0; i < v.size(); i++) { if (block[block_id[v[i]]].size() == 1) { if (split.find(block_id[v[i]]) == split.end()) continue; for (int p : block[split[block_id[v[i]]]]) { block_id[p] = block_id[v[i]]; block[block_id[v[i]]].insert(p); } block.erase(split[block_id[v[i]]]); continue; } block[block_id[v[i]]].erase(v[i]); if (split.find(block_id[v[i]]) != split.end()) { block_id[v[i]] = split[block_id[v[i]]]; block[block_id[v[i]]].insert(v[i]); } else { split[block_id[v[i]]] = t; block_id[v[i]] = t++; block[block_id[v[i]]].insert(v[i]); } } vector<pair<int,int>> res; for (auto p : split) { res.push_back(p); } return res; } void print() { for (auto [i,st] : block) { cerr << i << " [ "; for (int v : st) { cerr << v << " "; } cerr << "]" << endl; } } }; Automaton Minimize(const Automaton& dfa) { vector<vector<vector<int>>> inv_delta(dfa.size(),vector<vector<int>>(dfa.alphabet_size)); for (int state = 0; state < dfa.size(); state++) { for (int c = 0; c < dfa.alphabet_size; c++) { int t = dfa.delta[state][c]; inv_delta[t][c].push_back(state); } } PartitionRefinement pr(dfa.size()); vector<int> f; for (int state = 0; state < dfa.size(); state++) { if (dfa.accept(state)) f.push_back(state); } pr.partition(f); queue<pair<int,int>> que; for (int c = 0; c < dfa.alphabet_size; c++) { que.push({c,0}); que.push({c,1}); } while(!que.empty()) { auto [c,b_id] = que.front(); que.pop(); vector<int> v; for (int state : pr.block[b_id]) { for (int p : inv_delta[state][c]) { v.push_back(p); } } if (v.size() == 0) continue; auto par = pr.partition(v); for (auto p : par) { if (pr.block[p.first].size() > pr.block[p.second].size()) { swap(p.first,p.second); } if (pr.block[p.first].size() == 0) continue; for (int c2 = 0; c2 < dfa.alphabet_size; c2++) { que.push({c2,p.first}); } } } map<int,int> mp; for (int state = 0; state < dfa.size(); state++) { int b_id = pr.block_id[state]; if (mp.find(b_id) != mp.end()) continue; mp[b_id] = mp.size(); } vector<int> to_state(dfa.size()); for (int state = 0; state < dfa.size(); state++) { to_state[state] = mp[pr.block_id[state]]; } Automaton M; M.init = to_state[dfa.init]; M.alphabet_size = dfa.alphabet_size; M.qsize = mp.size(); M.delta.resize(M.qsize,vector<int>(M.alphabet_size)); M.is_accept.resize(M.qsize); M.is_reject.resize(M.qsize); for (int state = 0; state < dfa.size(); state++) { for (int c = 0; c < dfa.alphabet_size; c++) { M.delta[to_state[state]][c] = to_state[dfa.next(state,c)]; } if (dfa.accept(state)) M.is_accept[to_state[state]] = true; if (dfa.reject(state)) M.is_reject[to_state[state]] = true; } return M; } struct IncludeAllAutomaton : public Automaton { private: vector<int> elems; void initializer() { qsize = 1+(1<<(int)elems.size()); init = (1<<(int)elems.size()); set_delta(); set_is_accept(); set_is_reject(); } void set_delta() { delta.resize(qsize,vector<int>(alphabet_size)); for (int state = 0; state < qsize; state++) { for (int c = 0; c < alphabet_size; c++) { if (state == init && c == 0) delta[state][c] = init; else { delta[state][c] = state==init?0:state; for (int i = 0; i < elems.size(); i++) { if (c == elems[i]) { delta[state][c] = delta[state][c]|1<<i; break; } } } } } } void set_is_accept() { is_accept.resize(qsize,false); is_accept[(1<<(int)elems.size())-1] = true; } void set_is_reject() { is_reject.resize(qsize,false); } public: IncludeAllAutomaton(vector<int> elems, int alpha_size = 10) : elems(elems) { alphabet_size = alpha_size; initializer(); } }; template<class Automaton1, class Automaton2> Automaton UnionAutomaton(const Automaton1 &A, const Automaton2 &B) { assert(A.alphabet_size == B.alphabet_size); Automaton M; M.alphabet_size = A.alphabet_size; vector<vector<int>> table(A.size(), vector<int>(B.size(),-1)); vector<int> x = {A.init}, y = {B.init}; table[x[0]][y[0]] = 0; M.init = 0; for (int i = 0; i < x.size(); ++i) { M.delta.push_back(vector<int>(M.alphabet_size, -1)); M.is_accept.push_back(A.accept(x[i]) || B.accept(y[i])); M.is_reject.push_back(A.reject(x[i]) && B.reject(y[i])); for (int c = 0; c < A.alphabet_size; c++) { int u = A.next(x[i],c), v = B.next(y[i],c); if (table[u][v] == -1) { table[u][v] = x.size(); x.push_back(u); y.push_back(v); } M.delta[i][c] = table[u][v]; } } M.qsize = M.delta.size(); return M; } template<class Automaton1, class Automaton2> Automaton IntersectionAutomaton(const Automaton1 &A, const Automaton2 &B) { assert(A.alphabet_size == B.alphabet_size); Automaton M; M.alphabet_size = A.alphabet_size; vector<vector<int>> table(A.size(), vector<int>(B.size(),-1)); vector<int> x = {A.init}, y = {B.init}; table[x[0]][y[0]] = 0; M.init = 0; for (int i = 0; i < x.size(); ++i) { M.delta.push_back(vector<int>(M.alphabet_size, -1)); M.is_accept.push_back(A.accept(x[i]) && B.accept(y[i])); M.is_reject.push_back(A.reject(x[i]) || B.reject(y[i])); for (int c = 0; c < A.alphabet_size; c++) { int u = A.next(x[i],c), v = B.next(y[i],c); if (table[u][v] == -1) { table[u][v] = x.size(); x.push_back(u); y.push_back(v); } M.delta[i][c] = table[u][v]; } } M.qsize = M.delta.size(); return M; } int main() { string a,b;cin >> a >> b; int p;cin >> p; auto M1 = Minimize(ModuloAutomaton(p)); auto M2 = ModuloAutomaton(3); auto M3 = IncludeAllAutomaton({3}); auto M4 = Minimize(UnionAutomaton(M2,M3)); auto M5 = Minimize(IntersectionAutomaton(M1,M4)); cout << digitDP(b,M4).val-digitDP(a,M4,false).val-digitDP(b,M5).val+digitDP(a,M5,false).val << endl; return 0; }