Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cassert>
  3.  
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. using i64 = long long;
  10. using Matrix = vector< vector<int> >;
  11. using Poly = vector<int>;
  12.  
  13. Matrix hessenberg_form_mod(const Matrix& mat, const int mod) {
  14. const int N = mat.size();
  15. Matrix H(mat);
  16. for (int y = 0; y < N; ++y) for (int x = 0; x < N; ++x) if ((H[y][x] %= mod) < 0) H[y][x] += mod;
  17. for (int x = 0; x < N - 1; ++x) {
  18. for (int y = x + 2; y < N; ++y) {
  19. while (H[y][x] > 0) {
  20. if (H[y][x] < H[x + 1][x] || H[x + 1][x] == 0) {
  21. for (int x2 = x; x2 < N; ++x2) swap(H[x + 1][x2], H[y][x2]);
  22. for (int y2 = 0; y2 < N; ++y2) swap(H[y2][x + 1], H[y2][y]);
  23. }
  24. int q = H[y][x] / H[x + 1][x], mq = mod - q;
  25. for (int x2 = x; x2 < N; ++x2) H[y][x2] = (H[y][x2] + i64(mq) * H[x + 1][x2]) % mod;
  26. for (int y2 = 0; y2 < N; ++y2) H[y2][x + 1] = (H[y2][x + 1] + i64(q) * H[y2][y]) % mod;
  27. }
  28. }
  29. }
  30. return H;
  31. }
  32.  
  33. vector<int> characteristic_polynomial_mod(const Matrix& mat, const int mod) {
  34. const int N = mat.size();
  35. const auto H = hessenberg_form_mod(mat, mod);
  36. vector<Poly> polys(N + 1);
  37. Poly f(N + 1, 0); f[0] = 1 % mod;
  38. polys[0] = Poly(f.begin(), f.begin() + 1);
  39. for (int i = 0; i < N; ++i) {
  40. if (H[i][i]) {
  41. int mc = mod - H[i][i];
  42. for (int k = i + 1; k > 0; --k) f[k] = (f[k] + i64(mc) * f[k - 1]) % mod;
  43. }
  44. for (int j = 0, t = 1; j < i; ++j) {
  45. int d = i - j - 1;
  46. t = i64(t) * H[d + 1][d] % mod;
  47. if (t == 0) break;
  48. int mc = mod - i64(H[d][i]) * t % mod;
  49. if (mc == mod) continue;
  50. const auto& g = polys[d];
  51. for (int k = j + 2; k <= i + 1; ++k) f[k] = (f[k] + i64(mc) * g[k - (j + 2)]) % mod;
  52. }
  53. polys[i + 1] = Poly(f.begin(), f.begin() + i + 2);
  54. }
  55. return f;
  56. }
  57.  
  58. int main() {
  59. const int N = 45;
  60. Matrix mat(N, vector<int>(N));
  61. for (int i = 0; i < N; ++i) {
  62. for (int j = 0; j < N; ++j) {
  63. mat[i][j] = (i ^ j) + i + j * j;
  64. }
  65. }
  66. const auto f = vector<i64>({
  67. 1, -30360, -60655660, -32910834048, -6891631652864,
  68. -607026836799488, -21771649648951296, -253220688972742656, 0, 0,
  69. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  70. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  71. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0
  73. });
  74. for (int m = 2; m <= 1000; ++m) {
  75. for (int mod : {m, (1 << 31) - m}) {
  76. auto g = characteristic_polynomial_mod(mat, mod);
  77. for (int i = 0; i <= N; ++i) {
  78. assert((g[i] - f[i] % mod) % mod == 0);
  79. }
  80. }
  81. }
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement