Xingqiu at your service, my liege! I humbly trust that even one such as I, a mere bookworm, may yet prove to be of some utility under your wise leadership. Nice, I don’t often get a chance to speak with such formality. It felt pretty good! —Xingqiu As a self-proclaimed practitioner of the Guhua Clan (Aka. Kokaha)’s arts, Xingqiu (Aka. Yukuaki) is planning to forge a longsword with length n. The following picture shows a longsword with length n = 4.
In order to forge his longsword, Xingqiu has gotten some materials. There are two types of materials, and Xingqiu has infinite pieces of both of them. The following picture shows the shapes of materials.
Now, Xingqiu wants to calculate the number of ways to forge his longsword. As the result can be very large, you should output the answer modulo 998244353. Input The first line contains a single integer n (2 ≤ n ≤ 106 ) — the length of Xingqiu’s longsword. Output Output a single integer — the number of ways to forge Xingqiu’s longsword modulo 998244353.
思路:递推思想,我们可以发现“3”这个边长,可以有两种情况组成,所以a[i] 的一种情况就是2*a[i-3];另一种情况就是前边已经排好了,需要再在后边加一个“2”的平行四边形,就比如a[6]和a[8],a[8]中存在a[6]加一个平行四边形的情况,所以a[i] = 2*a[i-3] + a[i - 2];
代码
#include <bits/stdc++.h>
#define mm(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
const int N = 2e6 + 7;
ll a[N] = {0, 0, 1, 0, 1};
int main()
{
int n;
cin >> n;
for(int i = 5; i <= n; i ++)
{
a[i] = (2 * a[i - 3] + a[i - 2]) % 998244353;
}
cout << a[n] << endl;
return 0;
}