博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF1005D Polycarp and Div 3 思维
阅读量:6526 次
发布时间:2019-06-24

本文共 3188 字,大约阅读时间需要 10 分钟。

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 21052⋅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
Copy
3121
output
Copy
2
input
Copy
6
output
Copy
1
input
Copy
1000000000000000000000000000000000
output
Copy
33
input
Copy
201920181
output
Copy
4
Note

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 #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << #a << " " << a << endlusing 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;}

 

转载于:https://www.cnblogs.com/l609929321/p/9313534.html

你可能感兴趣的文章
vue2.0 引用qrcode.js实现获取改变二维码的样式
查看>>
Python 判断闰年,判断日期是当前年的第几天
查看>>
web.xml 中的listener、 filter、servlet 加载顺序
查看>>
MyBatis原理简介和小试牛刀
查看>>
js部分基础
查看>>
脏读,幻读,不可重复读解释和例子
查看>>
Tomcat指定(JDK路径)JAVA_HOME而不用环境变量
查看>>
银行卡信息安全事件频发 互联网站成数据泄露"重灾区"
查看>>
云服务器 ECS 使用OpenAPI管理ECS:使用OpenAPI弹性创建ECS实例
查看>>
写个软件来防止服务器网站CPU百分百
查看>>
智能城市里,“公共电话亭”的存在意味着什么?
查看>>
JVM分代垃圾回收策略的基础概念
查看>>
5G技术的5大猜想
查看>>
MongoDB 3.0(1):CentOS7 安装MongoDB 3.0服务
查看>>
别随便安装 Pokemon GO被曝藏恶意后门
查看>>
让数据会思考会说话,为出海企业提供多样化数据智能解决方案
查看>>
我眼中的自动化测试框架设计要点
查看>>
FLIF:自由的无损图像格式
查看>>
Google开源Inception-ResNet-v2,提升图像分类水准
查看>>
Opera 出售细节曝光:昆仑出资1.68亿美元
查看>>