More than 2019 cattle off summer school camp (fourth) K.number

> Portal <

Question is intended: to give you a string s, where the determined number divisible substring 300 (substring required to be contiguous, allowing front 0)

Ideas:

"Dynamic Programming

Referred to f [i] [j] satisfy mod 300 = number j of the substring right end point can be readily transferred

: The state transition equation [(F [I] 10 * J + NUM [I])% 300 ] = F [I] [ ( 10 * J + NUM [I])% 300 ] + F [I-. 1] [j]

Explanation: If you number 3, is easily obtained 3mod300 value is 3, f [3] = 1 , then add a 1 in 3 later, becomes 31, the value 31mod300 31, F [31] = 1. We make (10 * j + num [i ]) mod 300 is the X- , think carefully , you will find the current f [x] value can be added before f from the current f [x] [j] values get updates, remember to let the f [num [i]] value plus 1

Seeking the number of topic string mod 300 = 0, and therefore, each adding ans so the value f [i] [0] update like

Code

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

const int maxn = 1e5+200;
char s[maxn];
long long dp[maxn][300];

int main()
{
    cin >> s;
    int len = strlen(s);
    long long ans = 0;
    for(int i = 0 ;i < len; i++){
        dp[i][s[i]-'0']++;
        for(int j = 0; j < 300; j++){
            dp[i+1][(j*10+s[i+1]-'0')%300] += dp[i][j];
        }
        ans += dp[i][0];
    }
    cout << ans << endl;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/wizarderror/p/11258992.html