Mahmoud_Hawara

Untitled

Jan 2nd, 2025
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  3. using namespace std;
  4.  
  5. int n, LCM = 1, fact_cnt, glob_msk, ans = INT_MAX;
  6. vector<int> a, a_mask;
  7. vector<vector<int>> g, dp;
  8.  
  9. int lcm(int a, int b)
  10. {
  11. return a / __gcd(a, b) * b;
  12. }
  13.  
  14. vector<int> factorize(int x)
  15. {
  16. vector<int> ret;
  17. for (int i = 2; 1ll * i * i <= x; i++)
  18. {
  19. if (x % i)
  20. continue;
  21. ret.push_back(1);
  22. while (x % i == 0)
  23. {
  24. x /= i;
  25. ret.back() *= i;
  26. }
  27. }
  28. if (x > 1)
  29. {
  30. ret.push_back(x);
  31. }
  32. return ret;
  33. }
  34.  
  35. void dfs(int u, int p)
  36. {
  37. dp[u][0] = dp[u][a_mask[u]] = 1;
  38. for (auto v : g[u])
  39. {
  40. if (v == p)
  41. continue;
  42. dfs(v, u);
  43. for (int i = 0; i <= glob_msk; i++)
  44. for (int j = 0; j <= glob_msk; j++)
  45. {
  46. dp[u][i | j] = min(dp[u][i | j], dp[u][i] + dp[v][j]);
  47. }
  48. }
  49. }
  50.  
  51. bool test(int testnum)
  52. {
  53. string in = "test-data/";
  54. if (testnum < 10)
  55. in += "0";
  56. in += to_string(testnum);
  57. in += ".in";
  58. string out = "test-data/";
  59. if (testnum < 10)
  60. out += "0";
  61. out += to_string(testnum);
  62. out += ".ok";
  63. fstream myfile(out.c_str(), std::ios_base::in);
  64. // freopen(out.c_str(), "r", stdin);
  65. freopen(in.c_str(), "r", stdin);
  66.  
  67.  
  68. cin >> n;
  69.  
  70. a.resize(n + 1);
  71. a_mask.resize(n + 1);
  72. g.resize(n + 1);
  73.  
  74. for (int i = 1; i <= n; i++)
  75. {
  76. cin >> a[i];
  77. LCM = lcm(LCM, a[i]);
  78. }
  79.  
  80. vector<int> fact = factorize(LCM);
  81. fact_cnt = fact.size();
  82. for (int i = 1; i <= n; i++)
  83. {
  84. int msk = 0;
  85. for (int j = 0; j < fact_cnt; j++)
  86. {
  87. if (a[i] % fact[j] == 0)
  88. {
  89. msk |= (1 << j);
  90. }
  91. }
  92. a_mask[i] = msk;
  93. }
  94.  
  95.  
  96. for (int i = 1; i < n; i++)
  97. {
  98. int u, v;
  99. cin >> u >> v;
  100. g[u].push_back(v);
  101. g[v].push_back(u);
  102. }
  103.  
  104. bool yes = 0;
  105. for (int i = 1; i <= n; i++)
  106. {
  107. for (auto j : g[i])
  108. {
  109. if (lcm(a[i], a[j]) == LCM) yes = 1;
  110. }
  111. }
  112.  
  113. glob_msk = (1 << fact_cnt) - 1;
  114. dp = vector<vector<int>>(n + 1, vector<int>(glob_msk + 1, n));
  115.  
  116. dfs(1, -1);
  117.  
  118. for (int i = 1; i <= n; i++)
  119. {
  120. ans = min(ans, dp[i][glob_msk]);
  121. }
  122.  
  123. int judge_ans;
  124. myfile >> judge_ans;
  125.  
  126. if (ans != judge_ans)
  127. {
  128. cout << ans << ' ' << judge_ans << ' ' << yes << '\n';
  129. return 0;
  130. }
  131.  
  132. return 1;
  133. }
  134. int main()
  135. {
  136. IO;
  137. clock_t start, end;
  138. // Recording the end clock tick.
  139. // Calculating total time taken by the program.
  140. for (int i = 15; i <= 15; i++)
  141. {
  142. start = clock();
  143. cout << "test " << i << " ";
  144. if (test(i))
  145. cout << " ok\n";
  146. else
  147. cout << " WA\n";
  148. end = clock();
  149. double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  150. cout << "Time taken by program is : " << fixed
  151. << time_taken << setprecision(5);
  152. cout << " sec " << endl;
  153. }
  154. return 0;
  155. }
  156.  
  157. // 8 11 12 15
Advertisement
Add Comment
Please, Sign In to add comment