#include #include #include #include #include using namespace std ; typedef long long ll ; const ll MOD = 1000000007LL ; ll expo(ll a, ll n) { n %= (MOD-1) ; ll r = 1 ; while (n) { if (n & 1) r = (r * a) % MOD ; a = (a * a) % MOD ; n >>= 1 ; } return r ; } ll inv(ll a) { return expo(a, MOD-2) ; } vector fact ; ll perm(int a, int b) { return fact[a] * inv(fact[a-b]) % MOD ; } vector bittree ; int sumbelow(int b) { int r = 0 ; while (b) { r += bittree[b] ; b &= b-1 ; } return r ; } void addone(int b) { b++ ; while (b < bittree.size()) { bittree[b]++ ; b += (b & - b) ; } } // string comparison is O(n) and that gives us a quadratic factor. // we stick a prehash based on a simple sum of characters that // tells us whether we want to actually do the hash lookup or not. const int SIZE = 1048576 ; char prehash[SIZE] ; int main() { int n, k ; cin >> n >> k ; fact.push_back(1) ; for (int i=1; i<=n; i++) fact.push_back(fact[i-1]*i%MOD) ; vector dict(n) ; for (int i=0; i> dict[i] ; unsigned int s = 0 ; for (int j=0; j lookup ; for (int i=0; i digs ; char c ; string w ; int s = 0 ; while (cin >> c) { w.push_back(c) ; s += c ; if (prehash[s & (SIZE-1)] && lookup.find(w) != lookup.end()) { digs.push_back(lookup[w]) ; w.clear() ; s = 0 ; } } if (w.size() != 0 || digs.size() != k) cout << "Bad input data" << endl ; ll r = 1 ; bittree = vector(2*n) ; for (int i=0; i