当前位置:网站首页>Lick the dog till the last one has nothing (linear DP)
Lick the dog till the last one has nothing (linear DP)
2022-06-11 15:09:00 【Xiao a Xiao Bi】
Lick the dog and lick until the last thing is nothing
Thank you, blogger :https://blog.csdn.net/qq_51282224/article/details/122638737
link :https://ac.nowcoder.com/acm/contest/24213/1002
Title Description
As the core of the team ,forever97 He is respected by the other two teammates .
Trote_w Please... Every day forever97 Take out , But unfortunately, the center of the universe forever97 There are only 3 home forever97 Favorite takeout .
If Trote_w to forever97 Bought takeout from another family ,forever97 Will shout “ I don't eat I don't eat ”.
however forever97 I don't like to eat a takeout for three days in a row .
If Trote_w One day I forgot about it and bought him the same takeout for three days , that forever97 It will Trote_w Press your head into the screen of your mobile phone .
As Trote_w Good friends , You can tell him to keep asking forever97 eat n Tianfan , How many different ways to buy ?
Input description :
Multiple sets of samples
The first line is an integer T(1<=T<=20) Represents the number of test samples
Next t Each row is an integer n, representative Trote_w Please forever97 eat n Tianfan (1<=n<=100000)
Output description :
Output T An integer represents the number of schemes , Because the answer is too big , You just need to output mod 1e9+7 The answer after .
Example 1
Input
2
3
500
Output
24
544984352
dp Ideas 
Because the topic requires not to go to the same house for three consecutive days , So we just need to think about the i-1 Days and i-2 Days are enough
State means :
f[0/1/2][i] front i Day to day 0/1/2 Home buying program
State calculation
f[0][i]=f[1][i-1]+f[2][i-1]+f[1][i-2]+f[2][i-2]
f[1][i]=f[0][i-1]+f[2][i-1]+f[0][i-2]+f[2][i-2]
f[2][i]=f[1][i-1]+f[0][i-1]+f[1][i-2]+f[0][i-2]
Then observe and find i - 1 Days and i - 2 The method of days has been used twice , So it turns into one dimension
A two-dimensional dp
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 10;
ll f[3][maxn];
int t, n;
int main(){
cin >> t;
while(t --){
cin >> n;
f[0][0] = f[1][0] = f[2][0] = 0;
f[0][1] = f[1][1] = f[2][1] = 1;
f[0][2] = f[1][2] = f[2][2] = 3;
for(int i = 3;i <= n;i ++){
f[0][i]=(f[1][i-1]+f[2][i-1]+f[1][i-2]+f[2][i-2])%mod;
f[1][i]=(f[0][i-1]+f[2][i-1]+f[0][i-2]+f[2][i-2])%mod;
f[2][i]=(f[1][i-1]+f[0][i-1]+f[1][i-2]+f[0][i-2])%mod;
}
cout << (f[0][n]%mod+f[1][n]%mod+f[2][n]%mod)%mod << endl;
}
return 0;
}
A one-dimensional dp
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 10;
ll f[maxn];
int t, n;
int main(){
cin >> t;
while(t --){
cin >> n;
f[0] = 0;
f[1] = 3;
f[2] = 9;
for(int i = 3;i <= n;i ++){
f[i] = (f[i - 1] * 2 + f[i - 2] * 2) % mod;
}
cout << f[n] << endl;
}
return 0;
}
边栏推荐
- Learn more about and use ThreadLocal
- 深度剖析「圈组」关系系统设计 | 「圈组」技术系列文章
- 19. insertion, deletion and pruning of binary search tree
- 19. 二叉搜索樹的插入删除修剪
- 3年亏损136亿,上市能救活威马吗?
- Anaconda delete virtual environment
- 高数_第6章无穷级数__马克劳林级数
- Exporting data using mysqldump
- Hashicopy之nomad应用编排方案05(访问web页面)
- Seven parameters of thread pool and reject policy
猜你喜欢

深度剖析「圈組」關系系統設計 | 「圈組」技術系列文章

树莓派知识大扫盲

B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡

你违规了吗?

Illustration of tiger international quarterly report: revenue of USD 52.63 million continued to be internationalized

uniapp设置页面跳转效果 - navigateTo切换效果 - 全局animationType动画

对于事务的认识

Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?

Cartoon: interesting "cake cutting" problem

简单的C语言版本通讯录
随机推荐
System.out.println()方法使用需要注意哪些问题
简单的C语言版本通讯录
Database optimization
Elk log analysis system
理邦仪器软件实习生面试
架构概念探索:以开发纸牌游戏为例
Nexus of repository manager
大道至简 | 设计 ViT 到底怎么配置Self-Attention才是最合理的?
[team learning] task06:for, if, and while
gensim. Models word2vec parameter
2022年湖南省安全员-C证考试练习题及在线模拟考试
Hamad application layout scheme 05 of hashicopy (visit the web page)
C语言简易版webserver
Explain the kubernetes package management tool Helm
Hashicopy之nomad应用编排方案03(运行一个job)
Learn more about and use ThreadLocal
腾讯面试官分享面试经验,如何考察面试者技术及个人综合素质,给正在面试的你一点建议
[mysql_12] MySQL data types
System. out. What should I pay attention to when using the println () method
Raspberry pie obtains the function of network installation system without the help of other devices