D. Polycarp and Div 3
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 mm such cuts, there will be m+1m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 33.
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 33.
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?
Input
The 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.
Output
Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.
Examples
input
3121output
2
input
6output
1input
1000000000000000000000000000000000output
33input
201920181output
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.
题意:这个题的大致意思就是说给你一个由0~9这些数字组成的字符串,让你随意把他们分割,并且让分割之后的数字里面能被三整除的数字的个数最大,让你求这个字符串最多能分成几个可以被三整除的数字。
思路:假如第一个数不能被三整除,那么考虑第二个数,若是不能被三整除,则看他们两个数的和,他们的与3分别的余数只有「1,1」、「1,2」、「2,2」这么三种情况,若是「1,2」到这就可以停下来,重新开始判断下一个了,否则无论是上述1、3情况下的哪一种都会被第三个一起给3整除掉,举个例子如果下一个数%3=1,遇上情况1,三个数和一定为三的倍数;情况2,第二个数和第三个数一定为三的倍数。所以每个式子长度不过三。
根据这个思路我们就可以写出如下的代码,下面给出AC代码,
#include<bits/stdc++.h> using namespace std;; const int maxn=2*1e5+7; char ch[maxn]; int main() { scanf("%s",ch); int l=strlen(ch); int cnt=0; int sum=0; int a=0; for(int i=0;i<l;i++) { cnt++; sum=sum+ch[i]-48; if(cnt==3||sum%3==0||(ch[i]-48)%3==0) { cnt=0; a++; sum=0; } } printf("%d",a); return 0; }
CF-D. Polycarp and Div 3
最新推荐文章于 2020-01-19 00:07:19 发布