Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef LOCAL
- # define _GLIBCXX_DEBUG
- #else
- # define cerr __get_ce
- #endif
- #include <bits/stdc++.h>
- using namespace std;
- #define next __next
- #define prev __prev
- #define right __right
- #define left __left
- #define index __index
- typedef long long ll;
- typedef long double ld;
- typedef unsigned int uint;
- typedef unsigned long long ull;
- #define szof(x) ((int)(x).size())
- #define bend(x) (x).begin(), (x).end()
- #define ff first
- #define ss second
- int const INF = 100 + (int) 1e9;
- ll const INFL = 100 + (ll) 1e18;
- ld const PI = 3.141592653589793238462643L;
- mt19937 tw(960172);
- int millis() { static auto s = chrono::system_clock::now(); return 1e3 * chrono::duration<double>(chrono::system_clock::now() - s).count(); }
- bool is_prime(ll x) { for (ll y = 2; y * y <= x; ++y) if (x % y == 0) return 0; return x > 1; }
- ll rnd(ll x, ll y) { static uniform_int_distribution<ll> d; return d(tw) % (y - x + 1) + x; }
- ll sqr(int a) { return (ll) a * a; } template<class T> T sqr(T const& a) { return a * a; }
- ll gcd(ll a, ll b) { while (b > 0) { ll t = a % b; a = b; b = t; } return a; }
- template <typename T>
- T input() {
- T res;
- cin >> res;
- return res;
- }
- #define ALL(A) A.begin(), A.end()
- #define SZ(A) int(A.size())
- struct node;
- vector<node*> lst;
- struct node {
- node() {
- std::fill(next, next + 6, nullptr);
- uid = SZ(lst);
- lst.push_back(this);
- }
- node* suf = NULL;
- node* next[6];
- int uid;
- int term = -1;
- };
- int main() {
- millis();
- //freopen("", "r", stdin);
- //freopen("", "w", stdout);
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.precision(10);
- for (int T = input<int>(); T != 0; --T) {
- lst.clear();
- int N, L;
- cin >> N >> L;
- node* root = new node();
- for (int i = 0; i != N; ++i) {
- node* cur = root;
- for (int j = 0; j != L; ++j) {
- int a = input<int>() - 1;
- if (cur->next[a] == NULL)
- cur->next[a] = new node();
- cur = cur->next[a];
- }
- cur->term = i;
- }
- // karasik
- root->suf = root;
- queue<node*> q;
- for (int i = 0; i != 6; ++i)
- if (root->next[i] == NULL)
- root->next[i] = root;
- else {
- root->next[i]->suf = root;
- q.push(root->next[i]);
- }
- cerr << "123" << std::endl;
- while (not q.empty()) {
- node* v = q.front();
- q.pop();
- if (v->term == -1)
- for (int i = 0; i != 6; ++i)
- v->next[i] = v;
- else
- for (int i = 0; i != 6; ++i)
- if (v->next[i] == NULL)
- v->next[i] = v->suf->next[i];
- else {
- v->next[i]->suf = v->suf->next[i];
- q.push(v->next[i]);
- }
- }
- cout << "456" << std::endl;
- for (int i = 0; i != SZ(lst); ++i, cerr << std::endl)
- for (int j = 0; j != 6; ++j)
- cerr << lst[i]->next[j] << " ";
- vector<vector<double>> mat(SZ(lst), vector<double>(SZ(lst)));
- vector<vector<double>> nmat(SZ(lst), vector<double>(SZ(lst)));
- for (int i = 0; i != SZ(lst); ++i) {
- node* v = lst[i];
- for (int j = 0; j != 6; ++j)
- mat[i][v->next[j]->uid] += 1.0/6;
- }
- cout << "789" << std::endl;
- for (int ntimes = 0; ntimes != 20; ++ntimes) {
- for (int i = 0; i != SZ(lst); ++i)
- for (int j = 0; j != SZ(lst); ++j)
- nmat[i][j] = 0.0;
- for (int i = 0; i != SZ(lst); ++i)
- for (int j = 0; j != SZ(lst); ++j)
- for (int k = 0; k != SZ(lst); ++k)
- nmat[i][k] += mat[i][j] * mat[j][k];
- std::swap(mat, nmat);
- }
- vector<double> ans(N, 0.0);
- for (int i = 0; i != SZ(lst); ++i)
- if (lst[i]->term != -1)
- ans[lst[i]->term] += mat[0][i];
- for (int i = 0; i != N; ++i)
- cout << ans[i] << " ";
- cout << '\n';
- }
- #ifdef LOCAL
- cerr << "time: " << millis() << " ms" << endl;
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement