2019 cattle off network training school summer vacation more than a fourth field K -number

Links: https://ac.nowcoder.com/acm/contest/884/K
Source: Cattle-off network

Title Description

300iq loves numbers who are multiple of 300.
One day he got a string consisted of numbers. He wants to know how many substrings in the string are multiples of 300 when considered as decimal integers.
Note that leading and trailing zeros are allowed (both in original string and substrings you chose) and the same substring appearing in different places can be counted multiple times.

Enter a description:

A single line consisting a string consisted of characters '0' to '9'.

Output Description:

The number of substrings that are multiples of 300 when considered as decimal integers. 


Solution: First, we analyze the condition is satisfied: be divisible by 300 substring
   1. string of the last two certainly 00
   2. front of you and certainly divisible by 3
   
   so we can open a variable sum, "before i bits and mod 3 case" record. There are three cases: sum == 0, sum == 1 , sum == 2
   to open an array cnt [], "and the number i is the front case 3 mod occurring" recorded. That is only recorded cnt [0], cnt [1 ], cnt [2]
   
   Why recorded cases 1 and 2 it? For example:
   in [L, R & lt] the interval, if the sum L of the sum R and at equal, then it means that the number of bits of L ~ R and mod 3 == 0. That is [L, R] is a sequence capable of 3 sub-divisible.
   
   
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include<fstream>
#include<sstream>
#include<iterator>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>

using namespace std;
typedef double dou;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;

#define pai acos(-1.0)
#define M 100005
#define inf 0x3f3f3f3f
#define mod 1000000007
#define left k<<1
#define right k<<1|1
#define lson L, mid, left
#define rson mid + 1, R, right
#define W(a) while(a)
#define ms(a,b) memset(a,b,sizeof(a))
#define Abs(a) (a ^ (a >> 31)) - (a >> 31)

char str[M];
int cnt[M];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    W (cin>>str) {        
        ll ans = 0;
        int len = strlen(str);
        for (int i = 0; i < len; i++) {
             IF (STR [i] == ' 0 ' ) ANS ++; // first counting individual case 0 
        } 
        CNT [ 0 ] = . 1 ;
         int SUM = 0 ; // before i mod value of 3 bits 
        for ( int i = 0 ; I <len; I ++ ) { 
            SUM + STR = [I] - ' 0 ' ; 
            SUM % = . 3 ;
             IF (I + . 1 <len && STR [I] == ' 0 ' && STR [I +1 ] == ' 0 ' ) { // satisfies 00 and ending within the length of 
                ANS + = CNT [SUM]; 
            } 
            CNT [SUM] ++; // CNT [0], CNT [1], CNT [ 2] 
        } 
        COUT << ANS << endl; 
    } 
    return  0 ; 
}

 




Guess you like

Origin www.cnblogs.com/caibingxu/p/11258190.html