当前位置:网站首页>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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
边栏推荐
- AcWing第 55 场周赛
- Brief description of redo logs and undo logs in MySQL
- Matplotlib learning notes
- 报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
- Solution to qt5.12 unable to input Chinese (unable to switch Chinese input method) in deepin system
- Test cases that testers must master
- Navicat connection MySQL in Pagoda
- Record several interesting XSS vulnerability discoveries
- Nim游戏阶梯 Nim游戏和SG函数应用(集合游戏)
- CommonAPI与AUTOSAR AP通讯管理的异同
猜你喜欢

vivo大规模 Kubernetes 集群自动化运维实践

Chapter VII document management

How to optimize MySQL?

Understand an article: Spark operation mode

Go zero microservice Practice Series (III. API definition and table structure design)

Database learning notes (Chapter 16)

Brief introduction to memory structure of virtual machine

第七章 文件管理作业

Solution to qt5.12 unable to input Chinese (unable to switch Chinese input method) in deepin system

Install Kubernetes 1.24
随机推荐
21世纪以来的历次“粮食危机”,发生了什么?
ue5 小知识点 random point in Bounding Boxf From Stream
Review of last week's hot spots (6.6-6.12)
DNS protocol analysis
Flutter simple and excellent open source dialog uses free_ dialog
第七章 文件管理作业
Read pgstat [this time Gauss is not a mathematician]
2022 Gansu Province safety officer C certificate work certificate title and online simulation examination
[cloud enjoying freshness] community weekly · vol.66- Huawei partners and Developers Conference 2022 wonderful agenda announcement
Actual combat simulation │ real time error alarm of enterprise wechat robot
vivo大规模 Kubernetes 集群自动化运维实践
Ubuntu installs MySQL compressed package for future reference
Test cases that testers must master
Environ. Sci. Technol. (if=9.028) | impact of urban greening on atmospheric environment
Codeforces Round #798 (Div. 2)ABCD
Go zero microservice Practice Series (III. API definition and table structure design)
About instruction set bits and instruction architecture bits
报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
Vivo large scale kubernetes cluster automation operation and maintenance practice
很妙的贪心(F2. Nearest Beautiful Number (hard version))