题解分享
题解分享简介
积木画(编程题) - 题解
```c++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e9 + 7;
int n;
int dp[2][3];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
dp[1][0] = 1;
dp[1][1] = 2;
dp[1][2] = 1;
for(int i = 2; i <= n; i ++ ) {
dp[i & 1][0] = (dp[(i - 1) & 1][0] + dp[(i - 1) & 1][2]) % N;
dp[i & 1][1] = (dp[(i - 1) & 1][0] * 2 + dp[(i - 1) & 1][1]) % N;
dp[i & 1][2] = (dp[(i - 1) & 1][0] + dp[(i - 1) & 1][1]) % N;
}
cout << dp[n & 1][0];
}
```
查看全文
0
0
1
1
积木画(编程题) - 题解
本体要运用到数学知识,进行推理
include
using namespace std;
const int N=1e7+10,mod=1000000007;
int f[N];
int main(){
ios::sync\_with\_stdio(false),cout.tie(0),cin.tie(0);
f[1]=1;
f[2]=2;
f[3]=5;
int n;cin>>n;
```
for(int i=4;i<=n;i++)
{
f[i]=(2*f[i-1]%mod+f[i-3]%mod)%mod;
}
cout<<f[n];
return 0;
```
}
查看全文
0
0
0
1
积木画(编程题) - 题解
递推,模拟
```
#include<bits/stdc++.h>
#include<vector>
using namespace std;
int N;
const int K = 1e7 + 10;
int arr[K];
long long MOD = 1000000007;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);//取消同步流,让C++代码更快
cin>>N;
arr[1] = 1,arr[2] = 2,arr[3] = 5;
for(int i = 4;i <= N;i++)
{
arr[i] = (2 * arr[i - 1] % MOD + arr[i - 3] % MOD) % MOD;
}
cout<<arr[N]<<endl;
return 0;
}
```
查看全文
0
0
0
0



