4-1 sequence is divisible by 3

Topic Source: Cattle-off network

Title Description

To give you a numeric string of length 50, you ask how many sub sequence of digital may be divisible
answers modulo 1e9 + 7

Enter a description:

Enter a string composed of a number, equal to the length of less than 50

Output Description:

An integer output

Entry

132
9

Export

3
1

Ideas:

3 because the remainder can only be taken 012
so every time it extracts a number of conventional and may be combined before the total number of possibilities of each divisible by 3 can be obtained.

dp [i] [j] + = (dp [i - 1] [j] + dp [i - 1] [(j + 3 - x)% 3])% mod; // itself before addition of the present without prior

AC Code:

#include<bits/stdc++.h>
using namespace std;

string t;
int dp[55][3];
int x;
const int mod=1e9+7;
int main(){
	while(cin>>t){
		memset(dp,0,sizeof dp);//数据清空
		x=t[0]-'0';
		dp[0][x%3]++;//本身的可能性
		for(int i=1;i<t.length();i++){
			x=t[i]-'0';
			x=x%3;
			dp[i][x]=dp[i][x]+1;//
			for(int j=0;j<3;j++){
				dp[i][j]+=(dp[i-1][j]+dp[i-1][(j+3-x)%3])%mod;//本身  之前不加 之前加本 
			}
		}
		cout<<dp[t.length()-1][0]%mod<<endl; 
	}
	return 0;
}
Published 34 original articles · won praise 6 · views 1334

Guess you like

Origin blog.csdn.net/qq_44669377/article/details/105240775