当前位置:网站首页>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

Title link here

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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

原网站

版权声明
本文为[I can screw the bottle cap when I am born again]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131056569060.html