时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
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.
A longsword with length n = 4 (not really long though).
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.
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.

A longsword with length n = 4 (not really long though).
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.
输入描述:
The first line contains a single integer n (2≤n≤1062 \leq n \leq 10^62≤n≤106) --- the length of Xingqiu's longsword.
输出描述:
Output a single integer --- the number of ways to forge Xingqiu's longsword modulo 998244353.
备注:
In the first example (n = 4), there are 1 way as following:
In the second example (n = 5), there are 2 ways as following:
In the third example (n = 7), there are 4 ways as following:
As is shown, materials can be rotated and flipped, but can't be cut.
#include<iostream>
using namespace std;
#define ll long long
const int maxn=1e6+5;
const ll Mod=998244353;
ll dp[maxn][6];
ll fixll(ll n)
{
return n%Mod;
}
ll quick_mod(ll x)
{
ll md=Mod-2;
ll op=1;
while(md)
{
if(md&1)
op=(op*x)%Mod;
x=(x*x)%Mod;
md>>=1;
}
return op;
}
int main()
{
int n;
cin>>n;
ll p=quick_mod(2);
dp[2][0]=dp[2][1]=1;
for(int i=3;i<=n;i++)
{
dp[i][0]=fixll(dp[i][0]+dp[i-2][2]+dp[i-2][3]);
dp[i][1]=fixll(dp[i][1]+dp[i-2][2]+dp[i-2][3]);
dp[i][2]=fixll(dp[i][2]+dp[i-1][1]+dp[i-1][5]);
dp[i][3]=fixll(dp[i][3]+dp[i-1][0]+dp[i-1][4]);
dp[i][4]=fixll(dp[i][4]+dp[i-2][0]+dp[i-2][4]);
dp[i][5]=fixll(dp[i][5]+dp[i-2][1]+dp[i-2][5]);
}
ll sum=0;
for(int i=0;i<6;i++){
if(i!=2&&i!=3)
sum+=dp[n][i];
}
cout<<fixll(sum*p)<<endl;
}