codeforces 628B(数学)
题意描述
Max wants to buy a new skateboard. He has calculated the amount of money that is needed to buy a new skateboard. He left a calculator on the floor and went to ask some money from his parents. Meanwhile his little brother Yusuf came and started to press the keys randomly. Unfortunately Max has forgotten the number which he had calculated. The only thing he knows is that the number is divisible by 4.
You are given a string s consisting of digits (the number on the display of the calculator after Yusuf randomly pressed the keys). Your task is to find the number of substrings which are divisible by 4. A substring can start with a zero.
A substring of a string is a nonempty sequence of consecutive characters.
For example if string s is 124 then we have four substrings that are divisible by 4: 12, 4, 24 and 124. For the string 04 the answer is three: 0, 4, 04.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
判断被4整除的子串数量
思路
通过观察发现,百位以上数字的后两位如果可以被4整除,那么这个数字就可以被4整除,所以我们从后往前遍历一遍即可。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=3*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
char s[N];
void solve(){
scanf("%s",s+1);
int n=strlen(s+1);
ll ans=0;
rep(i,1,n+1){
int u=s[i]-'0';
if(u%4==0) ans++;
}
rrep(i,n,2){
int u1=s[i]-'0',u2=s[i-1]-'0';
if((u2*10+u1)%4==0) ans+=i-1;
}
cout<<ans<<endl;
}
int main(){
//IOS;
solve();
return 0;
- x64内核HOOK技术之拦截进程.拦截线程.拦截模块
- Spring开启方法异步执行
- 64位内核第一讲,和32位内核的区别
- Maven精选系列--发布jar包到Nexus私库
- 《SpringMVC从入门到放肆》五、SpringMVC配置式开发(处理器适配器)
- 《SpringMVC从入门到放肆》四、SpringMVC配置式开发(处理器映射器)
- 什么是Spring Boot?
- 《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解
- 《SpringMVC从入门到放肆》二、SpringMVC的执行流程及默认配置
- 《SpringMVC从入门到放肆》一、概述
- Spring import配置文件使用占位符
- 阿里Druid连接池的坑。。
- Go实现海量日志收集系统(二)
- Spring MVC表单防重复提交
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- C++核心准则CP.110:不要自已为初始化编写双重检查锁定代码
- BOSS直聘招聘信息获取之爬虫程序数据处理
- BOSS直聘招聘信息获取之爬虫程序分析
- 历时4个多月,学习了这 66 个CSS 特效
- ArrayList、LinkedList哪家强,据说90%人都不知道
- Windows创建克隆隐藏账号
- 学员分享-aspera踩坑记录
- BOSS直聘招聘信息获取之爬虫工具分析
- 把你的shiny网页工具部署在云服务器
- 面试中的路由问题
- BOSS直聘招聘信息获取之使用webdriver进行爬取
- es6中class的一些基础知识和es5语法的对比
- Windows 系统常用命令行命令(一):前言
- Vue项目使用mt-picker实现省市区三级联动踩坑记录
- Windows 系统常用命令行命令(二):路径与文件夹操作