[Cattle] was off -21,302 sequence divisible by 3 (dynamic programming)

Sequence is divisible by 3


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

Example 1

Entry

132

Export

3

Example 2

Entry

9

 

Export
1
Topic links:
 
Think about this question can be written, each of the first numeric string may be considered to last from the initial number of sequences is provided len, each additional number, sequence becomes 2 * len + 1;
Provided dp [0], dp [1], dp [2], 3 denotes a remainder obtained are 0,1,2. Each additional number of updates over the array.
The remainder is 0: dp [0] = dp [0] + dp [0] +1;
      dp[1]=dp[1]+dp[1];
      dp[2]=dp[2]+dp[2];
The remainder is 1: dp [0] = dp [0] + dp [2];
      dp[1]=dp[1]+dp[0]+1;
      dp[2]=dp[2]+dp[1];
The remainder is 2: dp [0] = dp [0] + dp [1];
      dp[1]=dp[1]+dp[2];
      dp[2]=dp[2]+dp[0]+1;
The law of their own to find a few numbers measure what there
 
AC Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <fstream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <deque>
 7 #include <vector>
 8 #include <queue>
 9 #include <string>
10 #include <cstring>
11 #include <map>
12 #include <stack>
13 #include <set>
14 using namespace std;
15 const int Mod=1e9+7;
16 char CH [ 1000 ];
 . 17  int DP [ . 3 ];
 18 is  int F ( char S) // String integer conversion 
. 19  {
 20 is      return S- ' 0 ' ;
 21 is  }
 22 is  int main ()
 23 is  {
 24      cin.getline ( CH, 1000 );
 25      Memset (DP, 0 , the sizeof (DP));
 26 is  
27      int len = strlen (CH);
 28      for ( int= I 0 ; I <len; I ++) // traversing from the first number of 
29      {
 30          / * 
31          focus here, this step should not be spared, the number must be open a new saved temporarily unable to change the size dp
 32          etc. s0, s1, s2 all updated after completion to replace it, or will affect the results,
 33          assume the remainder is 2: DP [0] = DP [0] + DP [. 1];
 34 is                   DP [. 1] = DP [. 1] + DP [2];
 35                   DP [2] DP = [2] + DP [0] + 1'd;
 36          If after this there is a problem of such update, dp [0] change, dp [2] = dp [ 2] + DP [0] + 1'd;
 37 [          use is new dp [0], not in compliance with the answer
 38 is          * / 
39          int S0 = 0 , S1 = 0 , S2 = 0 ;
 40          int X =f(ch[i]);
41         if(x%3==0)
42         {
43            s0+=dp[0]+1;
44            s1+=dp[1];
45            s2+=dp[2];
46         }
47         if(x%3==1)
48         {
49             s0+=dp[2];
50             s1+=dp[0]+1;
51             s2+=dp[1];
 52 is          }
 53 is          IF (X% . 3 == 2 )
 54 is          {
 55              S0 + = DP [ . 1 ];
 56 is              S1 + = DP [ 2 ];
 57 is              S2 + = DP [ 0 ] + . 1 ;
 58          }
 59          // uniform update 
60          DP [ 0 ] + = S0;
 61 is          DP [ . 1 ] + = S1;
 62 is          DP [ 2 ] + = S2;
 63 is          for (int J = 0 ; J < . 3 ; J ++) // each time the modulo 1e9 + 7; 
64              DP [J] DP = [J]% Mod;
 65      }
 66      COUT << DP [ 0 ] << endl;
 67 }

 

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/10935644.html