• 题意:给你一个巨长无比的数,你可以将这个数划成任意多个部分,求这些部分中最多有多少个能被\(3\)整除.

  • 题解:首先我们遍历累加每个位置的数字,如果某一位数或者累加和能被\(3\)整除(基础知识,不会就去百度),那这就是一部分,再来,我们可以发现一个部分最长只有\(3\)个数字.

    证:当我这个部分有\(3\)个数字的时候,前两个数字\(mod\ 3\)的余数肯定同时为\(1\)或\(2\),这个时候:

    假如第三个数字\(mod\ 3=1\)且前两个数字\(mod\ 3 = 1\),那这个三个数加起来肯定被\(3\)整除(前面说过).

    假如第三个数字\(mod\ 3= 1\)且前两个数字\(mod\ 3=2\),那第二个和第三个数字的和被\(3\)整除.

    同理,当第三个数字\(mod\ 3 = 2\)的时候与上面一样.

    ​ 于是,我们只要判断长度为\(3\),某位数是否被整除,累积和是否被整除,这三种情况即可.

  • 代码

    1. #include <iostream>
    2. #include <cstdio>
    3. #include <cstring>
    4. #include <cmath>
    5. #include <algorithm>
    6. #include <stack>
    7. #include <queue>
    8. #include <vector>
    9. #include <map>
    10. #include <set>
    11. #include <unordered_set>
    12. #include <unordered_map>
    13. #define ll long long
    14. #define fi first
    15. #define se second
    16. #define pb push_back
    17. #define me memset
    18. const int N = 1e6 + 10;
    19. const int mod = 1e9 + 7;
    20. const int INF = 0x3f3f3f3f;
    21. using namespace std;
    22. typedef pair<int,int> PII;
    23. typedef pair<ll,ll> PLL;
    24. string s;
    25. int cnt,res;
    26. int pre;
    27. int main() {
    28. ios::sync_with_stdio(false);cin.tie(0);
    29. cin>>s;
    30. for(int i=0;i<s.size();++i){
    31. pre+=s[i]-'0';
    32. cnt++;
    33. if(cnt==3 || (s[i]-'0')%3==0 || pre%3==0){
    34. res++;
    35. cnt=0;
    36. pre=0;
    37. }
    38. }
    39. printf("%d\n",res);
    40. return 0;
    41. }

Codeforces Round #496 (Div. 3) D. Polycarp and Div 3 (数论)的更多相关文章

  1. Codeforces Round #496 (Div. 3) ABCDE1

    //B. Delete from the Left #include <iostream> #include <cstdio> #include <cstring> ...

  2. CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)

    参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...

  3. Codeforces Round #496 (Div. 3)

    一如既往地四题...好久没切了 有点犯困了明显脑子感觉不够灵活. 为了熟练度还是用java写的,,,导致观赏性很差...我好不容易拉了个队友一起切结果过掉a就tm挂机了!!! A题竟然卡了,,,用了十 ...

  4. Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)

    E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 25 ...

  5. Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...

  6. Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...

  7. Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)

    题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...

  8. E. The Supersonic Rocket Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)

    http://codeforces.com/contest/1017/problem/E 凸包模板+kmp #include <cstdio> #include <cstdlib&g ...

  9. D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    http://codeforces.com/contest/868/problem/D 优化:两个串合并 原有状态+ 第一个串的尾部&第二个串的头部的状态 串变为第一个串的头部&第二个 ...

随机推荐

  1. 【Oracle】sum(..) over(..)用法分析

    今天再看sql优化详解的时候,提到了一个sum(..) over(..) 于是自己实验并在网上找了相关的一些文章来看 下面创建一张表: create sequence xulie increment ...

  2. 使用line_profiler对python代码性能进行评估优化

    性能测试的意义 在做完一个python项目之后,我们经常要考虑对软件的性能进行优化.那么我们需要一个软件优化的思路,首先我们需要明确软件本身代码以及函数的瓶颈,最理想的情况就是有这样一个工具,能够将一 ...

  3. 简单的DbContext工厂类(EFCore)

    前言 根据appsettings.json的中配置的数据库类型,使用工厂模式创建DbContext 代码实现 appsettings.json中的配置项 //使用的数据库类型 "Server ...

  4. LVM 逻辑卷扩容

    当lv空间不够用时,就需要我们对lv进行的扩容. 扩容分两种情况:(lv的有点,支持在线扩容) VG有充足的空间,可直接对LV进行扩容: VG空间不足,需要先对VG进行扩容,然后再对LV进行扩容 这里 ...

  5. Var_init class

    1 import org.apache.hadoop.conf.Configuration; 2 import org.apache.hadoop.fs.FSDataInputStream; 3 im ...

  6. CentOS 镜像下载地址

    CentOS镜像地址:http://isoredirect.centos.org/altarch/7/isos/i386/

  7. (Oracle)数据量统计存储过程

    本过程适用于Oracle数据量统计. create or replace procedure SP_GET_TAB_COUNT as v_tableName HDSD_TJ.Tablename%typ ...

  8. VMware 虚拟机逃逸漏洞

    所谓虚拟机逃逸(Escape Exploit),指的是突破虚拟机的限制,实现与宿主机操作系统交互的一个过程,攻击者可以通过虚拟机逃逸感染宿主机或者在宿主机上运行恶意软件. 针对 VMware 的虚拟机 ...

  9. 【LinuxShell】cp 用法详解

    Linux cp命令主要用于复制文件或目录. -a:此选项通常在复制目录时使用,它保留链接.文件属性,并复制目录下的所有内容.其作用等于dpR参数组合. -d:复制时保留链接.这里所说的链接相当于Wi ...

  10. springBoot controller入参LocalDateTime

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") @DateTimeForma ...