Recurrences UVA - 10870 (斐波拉契的一般形式推广)
Posted alingmaomao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Recurrences UVA - 10870 (斐波拉契的一般形式推广)相关的知识,希望对你有一定的参考价值。
题意: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 ", 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 ", kk); } } return 0; }
以上是关于Recurrences UVA - 10870 (斐波拉契的一般形式推广)的主要内容,如果未能解决你的问题,请参考以下文章