当前位置:网站首页>Count the number of special subsequences (0, 1, 2) DP
Count the number of special subsequences (0, 1, 2) DP
2022-06-13 11:02:00 【I can screw the bottle cap when I am born again】
Today Nordic Collegiate Programming Contest 2020, Found this G The question is exactly the same as the third question of the weekly competition last week , I doubt that the agricultural university directly changed this question after participating in the weekly competition . But because of the English problem , I read the wrong question , For the first time must have It must mean , If the word must have this meaning, it is the same as the question of Zhou Sai , Unfortunately, I lost Jingzhou carelessly .
Let's start with the weekly contest
Topic link
Special sequence By Positive integer individual 0 , Then Positive integer individual 1 , Last Positive integer individual 2 The sequence of components .
For example ,[0,1,2] and [0,0,1,1,1,2] It's a special sequence .
contrary ,[2,1,0] ,[1] and [0,1,2,0] It's not a special sequence .
Give you an array nums ( only Contains integers 0,1 and 2), Please return The number of different special subsequences . Because the answer can be big , Please put it on 109 + 7 Remainder After the return .
Of an array Subsequence After deleting zero or several elements from the original array , The sequence obtained without changing the order of the remaining elements . If two subsequences subscripted collections Different , So these two subsequences are Different .
Example 1:
Input :nums = [0,1,2,2]
Output :3
explain : The special subsequence is [0,1,2,2],[0,1,2,2] and [0,1,2,2] .
1 <= nums.length <= 105
0 <= nums[i] <= 2
class Solution {
private:
static constexpr int mod = 1000000007;
public:
int countSpecialSubsequences(vector<int>& nums) {
int f0 = 0, f1 = 0, f2 = 0;
for (int num: nums) {
if (num == 0) {
f0 = (f0 * 2 + 1) % mod;
}
else if (num == 1) {
f1 = (f1 * 2 % mod + f0) % mod;
}
else {
f2 = (f2 * 2 % mod + f1) % mod;
}
}
return f2;
}
};
ordinary DP, Although I didn't do the weekly race at that time , But I made it up , I haven't done much lately DP topic , I wish I hadn't learned the basic course .
I have to stick the question here must have = It must be , It means that all songs except the first song and the last song are rated as two ...................... speechless
Your friend Tóti is an aspiring musician. He has written nn songs, each of which has a hype rating of either 11, 22, or 33.A higher hype rating means the song has more energy. Tóti is planning his first live performance and needs your help. He wants to know how many setlists he can make. A setlist consist of at least three songs, the first song must have hype rating 11, the last song must have hype rating 33, and all other songs must have hype rating 22. Tóti also wants to play the songs in the same order he wrote them.
Given the hype rating of each of Tóti’s songs in the order he wrote them, how many setlists can he make?
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define L long long
const L mod(1e9+7);
int n;
L f1,f2,f3;
int main(){
scanf("%d",&n);
for(int i=1,x;i<=n;++i){
scanf("%d",&x);
if(x==1){
f1=(f1+1)%mod;
}
if(x==2){
f2=(f2*2%mod+f1)%mod;
}
if(x==3){
f3=(f3+f2)%mod;
}
}
printf("%lld\n",f3);
}
It's numb !!!
Another example of deformation , It can be analyzed backwards
Topic link ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
边栏推荐
- 容斥原理(能被整除的数)
- Similarities and differences between decoration mode and agency mode
- 作为一个测试人员,这些基础知识必不可少
- Install Kubernetes 1.24
- Brief introduction to memory structure of virtual machine
- MySQL transaction isolation level and mvcc
- 电赛校赛经验-程控风力摆
- Wechat applet customer service automatic reply - PHP implementation
- Initial installation and use of redis [play with Huawei cloud]
- Environ. Sci. Technol. (if=9.028) | impact of urban greening on atmospheric environment
猜你喜欢
ue5 小知识点 random point in Bounding Boxf From Stream
5.5 clock screensaver
宝塔中navicat连接mysql
ST表学习
Multithreading starts from the lockless queue of UE4 (thread safe)
The first laravel workflow engine released the official version of v1.0
Go needs to add an arrow syntax, which is more like PHP!
Go zero microservice Practice Series (III. API definition and table structure design)
Redis related
Database learning notes (Chapter 16)
随机推荐
Introduction to binary tree
数位DP例题
Flutter simple and excellent open source dialog uses free_ dialog
Database system concept (Chapter 17)
避免让转型企业走入歧途,是时候重新理解下湖仓一体了!| Q推荐
Talk about MySQL indexing mechanism
服务器的使用
元宇宙土地:是什么让数字房地产变得有价值
Finally, the monthly income is 20000!!
2022煤矿探放水特种作业证考试题库模拟考试平台操作
vivo大规模 Kubernetes 集群自动化运维实践
Necessary for Architects: system capacity status checklist
China SaaS industry panorama
Pytorch basis (II) -- tensor and gradient
Use of servers
of_ find_ compatible_ Node find all nodes
C# 文件打包下载
Easyclick run code snippet out null
DNS协议分析
作为一个测试人员,这些基础知识必不可少