Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #ifdef LOCAL
  2. # define _GLIBCXX_DEBUG
  3. #else
  4. # define cerr __get_ce
  5. #endif
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9. #define next __next
  10. #define prev __prev
  11. #define right __right
  12. #define left __left
  13. #define index __index
  14.  
  15. typedef long long ll;
  16. typedef long double ld;
  17. typedef unsigned int uint;
  18. typedef unsigned long long ull;
  19.  
  20. #define szof(x) ((int)(x).size())
  21. #define bend(x) (x).begin(), (x).end()
  22. #define ff first
  23. #define ss second
  24.  
  25. int const INF = 100 + (int) 1e9;
  26. ll const INFL = 100 + (ll) 1e18;
  27. ld const PI = 3.141592653589793238462643L;
  28. mt19937 tw(960172);
  29.  
  30. int millis() { static auto s = chrono::system_clock::now(); return 1e3 * chrono::duration<double>(chrono::system_clock::now() - s).count(); }
  31. bool is_prime(ll x) { for (ll y = 2; y * y <= x; ++y) if (x % y == 0) return 0; return x > 1; }
  32. ll rnd(ll x, ll y) { static uniform_int_distribution<ll> d; return d(tw) % (y - x + 1) + x; }
  33. ll sqr(int a) { return (ll) a * a; } template<class T> T sqr(T const& a) { return a * a; }
  34. ll gcd(ll a, ll b) { while (b > 0) { ll t = a % b; a = b; b = t; } return a; }
  35.  
  36. template <typename T>
  37. T input() {
  38. T res;
  39. cin >> res;
  40. return res;
  41. }
  42.  
  43. #define ALL(A) A.begin(), A.end()
  44. #define SZ(A) int(A.size())
  45.  
  46. struct node;
  47.  
  48. vector<node*> lst;
  49. struct node {
  50. node() {
  51. std::fill(next, next + 6, nullptr);
  52. uid = SZ(lst);
  53. lst.push_back(this);
  54. }
  55.  
  56. node* suf = NULL;
  57. node* next[6];
  58. int uid;
  59. int term = -1;
  60. };
  61.  
  62. int main() {
  63. millis();
  64. //freopen("", "r", stdin);
  65. //freopen("", "w", stdout);
  66. ios::sync_with_stdio(false);
  67. cin.tie(nullptr);
  68.  
  69. cout.precision(10);
  70. for (int T = input<int>(); T != 0; --T) {
  71. lst.clear();
  72.  
  73. int N, L;
  74. cin >> N >> L;
  75.  
  76. node* root = new node();
  77. for (int i = 0; i != N; ++i) {
  78. node* cur = root;
  79. for (int j = 0; j != L; ++j) {
  80. int a = input<int>() - 1;
  81.  
  82. if (cur->next[a] == NULL)
  83. cur->next[a] = new node();
  84. cur = cur->next[a];
  85. }
  86.  
  87. cur->term = i;
  88. }
  89.  
  90. // karasik
  91. root->suf = root;
  92.  
  93. queue<node*> q;
  94. for (int i = 0; i != 6; ++i)
  95. if (root->next[i] == NULL)
  96. root->next[i] = root;
  97. else {
  98. root->next[i]->suf = root;
  99. q.push(root->next[i]);
  100. }
  101.  
  102. cerr << "123" << std::endl;
  103.  
  104. while (not q.empty()) {
  105. node* v = q.front();
  106. q.pop();
  107.  
  108. if (v->term == -1)
  109. for (int i = 0; i != 6; ++i)
  110. v->next[i] = v;
  111. else
  112. for (int i = 0; i != 6; ++i)
  113. if (v->next[i] == NULL)
  114. v->next[i] = v->suf->next[i];
  115. else {
  116. v->next[i]->suf = v->suf->next[i];
  117. q.push(v->next[i]);
  118. }
  119. }
  120.  
  121. cout << "456" << std::endl;
  122.  
  123. for (int i = 0; i != SZ(lst); ++i, cerr << std::endl)
  124. for (int j = 0; j != 6; ++j)
  125. cerr << lst[i]->next[j] << " ";
  126.  
  127. vector<vector<double>> mat(SZ(lst), vector<double>(SZ(lst)));
  128. vector<vector<double>> nmat(SZ(lst), vector<double>(SZ(lst)));
  129. for (int i = 0; i != SZ(lst); ++i) {
  130. node* v = lst[i];
  131.  
  132. for (int j = 0; j != 6; ++j)
  133. mat[i][v->next[j]->uid] += 1.0/6;
  134. }
  135.  
  136. cout << "789" << std::endl;
  137.  
  138. for (int ntimes = 0; ntimes != 20; ++ntimes) {
  139. for (int i = 0; i != SZ(lst); ++i)
  140. for (int j = 0; j != SZ(lst); ++j)
  141. nmat[i][j] = 0.0;
  142.  
  143. for (int i = 0; i != SZ(lst); ++i)
  144. for (int j = 0; j != SZ(lst); ++j)
  145. for (int k = 0; k != SZ(lst); ++k)
  146. nmat[i][k] += mat[i][j] * mat[j][k];
  147.  
  148. std::swap(mat, nmat);
  149. }
  150.  
  151. vector<double> ans(N, 0.0);
  152. for (int i = 0; i != SZ(lst); ++i)
  153. if (lst[i]->term != -1)
  154. ans[lst[i]->term] += mat[0][i];
  155.  
  156. for (int i = 0; i != N; ++i)
  157. cout << ans[i] << " ";
  158. cout << '\n';
  159. }
  160.  
  161.  
  162. #ifdef LOCAL
  163. cerr << "time: " << millis() << " ms" << endl;
  164. #endif
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement