CF1005D Polycarp and Div 3 思维
3 seconds
256 megabytes
standard input
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?
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.
Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.
3121
2
6
1
1000000000000000000000000000000000
33
201920181
4
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 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main() {
string s;
while( cin >> s ) {
ll now = , cnt = , vis[] = { , , };
for( ll i = ; i < s.length(); i ++ ) {
now += s[i]-'';
now %= ;
if( vis[now] ) {
cnt ++;
now = ;
vis[] = vis[] = ;
} else {
vis[now] = ;
}
}
cout << cnt << endl;
}
return ;
}
CF1005D Polycarp and Div 3 思维的更多相关文章
- 『ACM C++』 Codeforces | 1005D - Polycarp and Div 3
今天佛了,魔鬼周一,在线教学,有点小累,但还好,今天AC了一道,每日一道,还好达成目标,还以为今天完不成了,最近任务越来越多,如何高效完成该好好思考一下了~最重要的还是学业的复习和预习. 今日兴趣新闻 ...
- CodeForces 1005D Polycarp and Div 3(思维、贪心、dp)
http://codeforces.com/problemset/problem/1005/D 题意: 给一个仅包含数字的字符串,将字符串分割成多个片段(无前导0),求这些片段里最多有多少是3的倍数 ...
- codeforces 887A Div. 64 思维 模拟
A. Div. 64 time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
- Polycarp's Pockets(思维)
Polycarp has nn coins, the value of the ii-th coin is aiai. Polycarp wants to distribute all the coi ...
- E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题
E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 1005D:Polycarp and Div 3
题目链接:http://codeforces.com/problemset/problem/1005/D 题意 给出个字符串(全是数字),把这个字符串换分成一些子串,最多能划分多少个能够被3整除的子串 ...
- Codeforces #496 (Div. 3) Polycarp and Div 3
思路1: https://blog.csdn.net/qq_41730082/article/details/80984316 题目的意思是给你一串数字长度为(1-2e5),当然由于它的这一串数字是不 ...
随机推荐
- 真千兆路由的极限之OPENWRT MAKE, 某品牌白菜价QCA9558/QCA9880/QCA8337N纯种组合OS搭建时记
自从上次仙人梦里放了一张无字天书,解惑了WPR003N的秘诀后,渐渐的,就忘了这件这事情,连想好的评测都拖延了好多月了,毕竟路由是拿来用的,不是用来写什么陈词滥调的评测的,无意间,热爱白菜的我发现了一 ...
- Codeforces Round #192 (Div. 2) (329A)C.Purification
题意: 在一个正常的点可以净化该行该列的所有细胞,判断是否可以净化所有的细胞,并且输出所选的点. 思路: 如果可以的话,一定会选n个点. 先判断每一行是否有正常细胞,然后判断每一列是否有,如果都没有肯 ...
- 第十章 Fisco Bcos 权限控制下的数据上链实操演练
一.目的 前面已经完成fisco bcos 相关底层搭建.sdk使用.控制台.webase中间件平台等系列实战开发, 本次进行最后一个部分,体系化管理区块链底层,建立有序的底层控管制度,实现权限化管理 ...
- AUTOSAR学习之RTE - 可运行实体
本文介绍RTE的运行体(runnable). An AUTOSAR component defines one or more "runnable entities". A run ...
- 一份新的lilypond谱子,能设置页边距和设置换页符了
给学生做的一份乐谱,这回能设置页边距了,以及设置换页符了. 顺带能设置一些代码片段(snippet),可以用热键代替使用 设置页边距的snippet: \paper { %双引号里面填页面大小 #(s ...
- FLV协议5分钟入门浅析
FLV协议简介 FLV(Flash Video)是一种流媒体格式,因其体积小.协议相对简单,很快便流行开来,并得到广泛的支持. 常见的HTTP-FLV直播协议,就是使用HTTP流式传输通过FLV封装的 ...
- Python3 反射
反射 python面向对象中的反射:通过字符串的形式操作对象相关的属性 hasattr(obj,name) # hasattr(obj, name) # 判断一个对象是否有指定的属性name,返回Tr ...
- c#小灶——常量、变量和赋值
常量 常量很好理解,和变量相对,就是不会变的量.比如,1就是常量,3.6也是常量,‘a’也是常量,“aaaaa”也是常量,只是不同类型.这些都是表面上一眼就看出来的常量,还有一种表面上看不出来的常量, ...
- 洛谷 P3870 [TJOI2009]开关
题意简述 有n盏灯,默认为关,有两个操作: 1.改变l~r的灯的状态(把开着的灯关上,关着的灯打开) 2.查询l~r开着的灯的数量 题解思路 维护一个线段树,支持区间修改,区间查询 懒标记每次^1 代 ...
- abap简单实现form递归
需求:根据物料号查询下层物料清单 DATA LV_MATNR LIKE ZMARA_TEST-MATNR VALUE '000000000000000001'. DATA: LT_MAT LIKE T ...