由 tag 考虑欧拉降幂。
前置知识
- 定义欧拉函数 为小于等于 且与 互质的正整数的个数。
根据扩展欧拉定理,有:
当 (其中 为质数)时,欧拉函数的计算公式为:
- 皮萨诺周期指斐波那契数列在模 意义下的最小正周期 ,有 。
题目解析
本题要求计算多组 的值,可以使用皮萨诺定理配合扩展欧拉定理和矩阵快速幂求解。值得注意的是,当 时,。
于是有 。
再通过扩展欧拉定理降幂。
令:
设:
则有:
最终得到:
,其中 。
此时再使用矩阵快速幂求解即可。
注:由于模数较大,相乘时可能会超出
long long的数据范围,需要使用__int128。
代码实现
#include <bits/stdc++.h>
#define ll __int128
using namespace std;
const ll M = 15000000000;
const ll Mod = 1e10;
ll read() {
ll ret = 0, f = 1; char ch = getchar();
while (!isdigit(ch)) { if (ch == '-') f = -f; ch = getchar(); }
while (isdigit(ch)) ret = (ret << 3) + (ret << 1) + ch - 48, ch = getchar();
return ret * f;
}
ll phi(ll n) {
ll ans = n;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n > 1) ans = ans / n * (n - 1);
return ans;
}
struct matrix {
int r, c;
ll d[5][5];
void init(ll x, ll y, bool th) {
r = x, c = y;
memset(d, 0, sizeof d);
if (th) for (ll i = 1; i <= r; i++) d[i][i] = 1;
}
};
matrix operator*(const matrix &x, const matrix &y) {
matrix res;
res.init(x.r, y.c, 0);
for (int i = 1; i <= x.r; i++)
for (int j = 1; j <= y.c; j++)
for (int k = 1; k <= x.c; k++)
res.d[i][j] += x.d[i][k] * y.d[k][j] % Mod, res.d[i][j] %= Mod;
return res;
}
matrix pow(matrix x, ll p) {
matrix res;
res.init(x.r, x.c, 1);
while (p) {
if (p & 1) res = res * x;
x = x * x;
p >>= 1;
}
return res;
}
ll mod_pow(ll x, ll y, ll mod) {
ll res = 1;
while (y) {
if (y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
ll T, n;
matrix f, dp;
int main() {
T = read();
f.init(1, 2, 0); dp.init(2, 2, 0);
f.d[1][1] = 1; f.d[1][2] = 0;
dp.d[1][1] = dp.d[1][2] = dp.d[2][1] = 1;
ll m1 = phi(M);
ll m2 = phi(m1);
ll m3 = phi(m2);
while (T--) {
n = read();
ll l3 = mod_pow(7, n % m3 + (n >= m3 ? m3 : 0), m2);
ll l2 = mod_pow(7, l3 + m2, m1);
ll l1 = mod_pow(7, l2 + m1, M);
matrix ans = f * pow(dp, l1 - 1);
printf("%010lld\n", (long long)ans.d[1][1]);
}
return 0;
}