CF1005D Polycarp and Div 3 思維
Polycarp likes numbers that are divisible by 3.
He has a huge number ss. Polycarp wants to cut from it the maximum number of numbers that are divisible by 33. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after
For example, if the original number is s=3121s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|213|1|21. As a result, he will get two numbers that are divisible by
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character ‘0‘). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 33 that Polycarp can obtain?
InputThe first line of the input contains a positive integer ss. The number of digits of the number ss is between 11 and 2?1052?105, inclusive. The first (leftmost) digit is not equal to 0.
OutputPrint the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.
Examples input Copy3121output Copy
2input Copy
6output Copy
1input Copy
1000000000000000000000000000000000output Copy
33input Copy
201920181output Copy
4Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 33.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 3333 digits 0. Each of the 3333 digits 0 forms a number that is divisible by 33.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 00, 99, 201201 and 8181 are divisible by 33.
題意:給我們一個數,我們可以對這個數進行任意的劃分,但是不能出現前綴零的無意義數,問我們最多可以劃分出幾個可以整除三的數?
分析:直接遍歷,考慮這四種情況就可以
1.如果單獨的數能整除三,那麽這數肯定可以,結果直接加一
2.如果不能整除三,那麽將得到一個余數,然後接下來我們會在這個數的基礎上加上新的數,如果余數和這個數的余數相同,則中間肯定加了一個能整除三的數,結果加一
3.如果不能整除三的數加上一個數後能整除三,則結果加一
4.如果不能整除三的數加上一個數既不能整除三且余數和之前不相同,則再加一個數肯定會得到和前面兩個數相同的余數或者整除三
根據以上分析我們每次加數時將余數打上標誌,然後根據上訴分析判斷結果是否能加一,記得結果加一後要把標誌置為0
#include <map> #include <set> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <vector> #include <string> #include <cstring> #include <iostream> #include <algorithm> #define debug(a) cout << #a << " " << a << endl using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; typedef long long ll; int main() { string s; while( cin >> s ) { ll now = 0, cnt = 0, vis[3] = { 1, 0, 0 }; for( ll i = 0; i < s.length(); i ++ ) { now += s[i]-‘0‘; now %= 3; if( vis[now] ) { cnt ++; now = 0; vis[1] = vis[2] = 0; } else { vis[now] = 1; } } cout << cnt << endl; } return 0; }
CF1005D Polycarp and Div 3 思維