2019 Nanchang network game H The Nth Item Matrix quick power

URL: https://nanti.jisuanke.com/t/41355

Meaning of the questions:

Seek times $ Q $ $ F (n) mod998244353 $ XOR of all values, $ F (n) $ is given by the recursion formulas, and must be online, $ Q \ leq 1e7 $.

answer:

See the form $ F (n) = a * F (n-1) + b * F (n-2) $ equation, the matrix is ​​fast exponentiation routine, direct matrix quickly seeking out a power, and then follow the intended title simulation can be. Matrix of Power and quick fast power the same way, but the beginning of the $ res = 1 $ matrix can be replaced. Measured $ 1e7 $ timeout, add to the memory of the pass.

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int MAXN = 1e7 + 5;
unordered_map<int, ll>mp;
struct Mat
{
	ll m[2][2];
};
Mat mul(Mat a, Mat b)
{
	Mat tmp;
	memset(tmp.m, 0, sizeof(tmp.m));
	tmp.m[0][0] = (a.m[0][0] * b.m[0][0] % mod + a.m[0][1] * b.m[1][0] % mod) % mod;
	tmp.m[0][1] = (a.m[0][0] * b.m[0][1] % mod + a.m[0][1] * b.m[1][1] % mod) % mod;
	tmp.m[1][0] = (a.m[1][0] * b.m[0][0] % mod + a.m[1][1] * b.m[1][0] % mod) % mod;
	tmp.m[1][1] = (a.m[1][0] * b.m[0][1] % mod + a.m[1][1] * b.m[1][1] % mod) % mod;
	return tmp;
}
Mat inv(Mat a,ll n)
{
	Mat res;
	memset(res.m, 0, sizeof(res.m));
	for (int i = 0; i < 2; ++i)
		res.m[i][i] = 1;
	while (n)
	{
		if (n & 1)
			res = mul(res, a);
		a = mul(a, a);
		n >>= 1;
	}
	return res;
}
Mat beg, b, mtmp;
void init()
{
	memset(beg.m, 0, sizeof(beg.m));
	beg.m[0][0] = 1;
	b.m[0][0] = 3;
	b.m[0][1] = 1;
	b.m[1][0] = 2;
}
int main()
{
	ll q, n, ans = 0;
	init();
	scanf("%lld%lld", &q, &n);
	for (int i = 1; i <= q; ++i)
	{
		ll tmp;
		if (!mp.count(n))
		{
			mtmp = mul(beg, inv(b, n - 1));
			tmp = mtmp.m[0][0] % mod;
			mp[n] = tmp;
		}
		else
			tmp = mp[n];
		ans ^= tmp;
		tmp %= mod;
		n ^= tmp * tmp;
	}
	printf("%lld\n", ans);
	return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/Aya-Uchida/p/11489234.html