ACM-ICPC 2018 焦作赛区网络预赛 L. Poor God Water

#题解
大佬的递推式子。。本弱鸡具体怎么得到的也不是很清楚
f(1)=3,f(2)=9,f(3)=20,f(4)=46,f(5)=106
f(n)=2f(n-1)-f(n-2)+3f(n-3)+2*f(n-4)
使用矩阵快速幂求解
#AC代码

#include 
#include 
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 4;

void mul(ll a[MAXN][MAXN], const ll b[MAXN][MAXN])
{
	ll t[MAXN][MAXN] = { 0 };
	for (int i = 0; i < MAXN; i++)
		for (int j = 0; j < MAXN; j++)
			for (int k = 0; k < MAXN; k++)
				t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % MOD;
	for (int i = 0; i < MAXN; i++)
		for (int j = 0; j < MAXN; j++)
			a[i][j] = t[i][j];
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	int T;
	cin >> T;
	while (T--)
	{
		ll n;
		cin >> n;
		if (n == 1)
		{
			cout << 3 << endl;
			continue;
		}
		else if (n == 2)
		{
			cout << 9 << endl;
			continue;
		}
		else if (n == 3)
		{
			cout << 20 << endl;
			continue;
		}
		else if (n == 4)
		{
			cout << 46 << endl;
			continue;
		}
		ll t[MAXN][MAXN] = { 2, -1, 3, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, u[MAXN][MAXN] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
		ll x[MAXN] = { 106, 46, 20, 9};
		n -= 5;
		while (n)
		{
			if (n & 1)
				mul(u, t);
			mul(t, t);
			n >>= 1;
		}
		ll ans = 0;
		for (int i = 0; i < MAXN; i++)
			ans = (ans + x[i] * u[0][i]) % MOD;
		cout << ans << endl;
	}

	return 0;
}

你可能感兴趣的:(ACM-ICPC 2018 焦作赛区网络预赛 L. Poor God Water)