intfindrt() { std::vector<int> fac; int cur = Mod - 1; for (int i = 2; i <= cur / i; ++ i) if (cur % i == 0) { fac.push_back(i); while (cur % i == 0) cur /= i; } if (cur ^ 1) fac.push_back(cur); auto check = [&](int x) { for (int t : fac) if (qpow(x, (Mod - 1) / t) == 1) returnfalse; returntrue; }; for (int i = 1; i < Mod; ++ i) if (check(i)) return i; return-1; }
voidwork() { LL n; int k, res = 0; std::cin >> n >> k >> Mod; wn[0] = 1, wn[1] = qpow(findrt(), (Mod - 1) / k); for (int i = 2; i < k; ++ i) wn[i] = (LL) wn[i - 1] * wn[1] % Mod; for (int i = 0; i < k; ++ i) { Matrix cur{{{1, wn[i]}, {wn[i], wn[i] + 1}}}; cur = qpow(cur, n), adj(res += cur[1][1] - Mod); } res = (LL) res * qpow(k) % Mod; std::cout << res << std::endl; }
Gitalking ...