J - uvenile Galant

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值