标签:main struct memset can color oid mod 重要 tor
题意:f(n) = a1f(n?1) + a2f(n?2) + a3f(n?3) + ... + adf(n?d), 计算这个f(n)
最重要的是推出矩阵。
#include<cstdio> #include<cstring> #define ll long long ll mod, d, n; ll a[16]; ll f[16]; struct jz { ll num[16][16]; jz(){ memset(num, 0, sizeof(num)); } jz operator*(const jz&p)const { jz ans; for (int k = 0; k < d; ++k) for (int i = 0; i < d;++i) for (int j = 0; j < d; ++j) ans.num[i][j] = (ans.num[i][j] + num[i][k] * p.num[k][j] % mod) % mod; return ans; } }p; jz POW(jz x, ll n) { jz ans; for (int i = 0; i < d; ++i)ans.num[i][i] = 1; for (; n;n>>=1, x=x*x) if (n & 1)ans = ans*x; return ans; } void init() { for (int i = 0; i < d; ++i) p.num[0][i] = a[i]; for (int i = 1; i < d; ++i) p.num[i][i - 1] = 1; } int main() { while (scanf("%lld%lld%lld", &d, &n, &mod) != EOF, d + n + mod) { for (int i = 0; i < d; ++i)scanf("%lld", &a[i]); for (int i = 0; i < d; ++i)scanf("%lld", &f[i]); if (n <= d){ printf("%lld\n", f[n - 1]); } else { init(); jz ans = POW(p, n - d); ll kk = 0; for (int i = 0, j = d - 1; i < d; ++i, --j) { kk = (kk + ans.num[0][i] * f[j] % mod) % mod; } printf("%lld\n", kk); } } return 0; }
Recurrences UVA - 10870 (斐波拉契的一般形式推广)
标签:main struct memset can color oid mod 重要 tor
原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9501816.html