Divisibility by 25
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an integer nn from 11 to 10181018 without leading zeroes.

In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.

What is the minimum number of moves you have to make to obtain a number that is divisible by 2525? Print -1 if it is impossible to obtain a number that is divisible by 2525.

Input

The first line contains an integer nn (1≤n≤10181≤n≤1018). It is guaranteed that the first (left) digit of the number nn is not a zero.

Output

If it is impossible to obtain a number that is divisible by 2525, print -1. Otherwise print the minimum number of moves required to obtain such number.

Note that you can swap only adjacent digits in the given number.

Examples
input

Copy
5071
output

Copy
4
input

Copy
705
output

Copy
1
input

Copy
1241367
output

Copy
-1
Note

In the first example one of the possible sequences of moves is 5071 →→ 5701 →→ 7501 →→ 7510 →→ 7150.

题意: 可以移动每位数的位置,使得移动后的数字是25的倍数,问最小需要移动多少位?

emmmmm,好多特殊情况,wa了很多发

能整除25,则最后两位只能是00,25,50,75

枚举0,2,5,7的个数

然后判断这四种情况,需要注意的是如果处于后面的数在前面了,如果此时另一个数不在末位则移动次数需加一

还有如果从第二位开始有连续的0的话且要移动的数位置在第一位此时移动可能导致开头为0,需要多移动0的个数次,还有特判要用到的0在第二位此时需要加的次数少一

#include <map>
#include <set>
#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 = *1e3 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main(){
std::ios::sync_with_stdio(false);
string s;
while( cin >> s ) {
ll a = -, b = -, c = -, d = -, num = , ta;
for( ll i = ; i < s.length(); i ++ ) {
if( s[i] == '' ) {
num ++;
if( num >= ) {
ta = a;
}
a = i;
} else if( s[i] == '' ) {
b = i;
} else if( s[i] == '' ) {
c = i;
} else if( s[i] == '' ) {
d = i;
}
}
ll ans1 = 1e12, ans2 = 1e12, ans3 = 1e12, ans4 = 1e12;
bool flag = false;
//debug(a), debug(b), debug(c),debug(d),debug(num);
if( a != - && c != - ) {
if( a < c ) {
if( c == s.length() - ) {
ans1 = min( s.length() - - a, ans1 );
} else {
ans1 = min( s.length() - - a + s.length() - - c + , ans1 );
}
} else {
ans1 = min( s.length() - - a + s.length() - - c, ans1 );
}
ll t = , i = ;
while( s[i] == '' ) {
t ++;
i ++;
}
if( s[] == '' && ( a == || c == ) ) {
if( s.length() == ) {
flag = true;
} else {
ans1 += t;
if( a == ) {
ans1 --;
}
}
}
}
if( c != - && b != - ) {
if( c < b ) {
if( b == s.length() - ) {
ans2 = min( s.length() - - c, ans2 );
} else {
ans2 = min( s.length() - - c + s.length() - - b + , ans2 );
}
} else {
ans2 = min( s.length() - - c + s.length() - - b, ans2 );
}
ll t = , i = ;
while( s[i] == '' ) {
t ++;
i ++;
}
if( s[] == '' && ( b == || c == ) ) {
if( s.length() == ) {
flag = true;
} else {
ans2 += t;
}
}
}
if( c != - && d != - ) {
if( c < d ) {
if( d == s.length() - ) {
ans3 = min( s.length() - - c, ans3 );
} else {
ans3 = min( s.length() - - c + s.length() - - d + , ans3 );
//debug(ans);
}
} else {
ans3 = min( s.length() - - c + s.length() - - d, ans3 );
}
ll t = , i = ;
while( s[i] == '' ) {
t ++;
i ++;
}
if( s[] == '' && ( d == || c == ) ) {
if( s.length() == ) {
flag = true;
} else {
ans3 += t;
}
}
}
//debug(ans);
if( num >= ) {
ans4 = min( ans4, s.length() - - a + s.length() - - ta );
}
ll ans = min( min( ans1, ans2 ), min( ans3, ans4 ) );
if( ans == 1e12 && !flag ) {
cout << - << endl;
} else {
cout << ans << endl;
}
}
return ;
}

CF 988E Divisibility by 25 思维 第十二的更多相关文章

  1. Codeforces 988E. Divisibility by 25

    解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...

  2. Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

    传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...

  3. Codeforces Round #486 (Div. 3) E. Divisibility by 25

    Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  4. [分享] IT天空的二十二条军规

    Una 发表于 2014-9-19 20:25:06 https://www.itsk.com/thread-335975-1-1.html IT天空的二十二条军规 第一条.你不是什么都会,也不是什么 ...

  5. 201521123038 《Java程序设计》 第十二周学习总结

    201521123038 <Java程序设计> 第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student ...

  6. 《Two Dozen Short Lessons in Haskell》(二十二)递归

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  7. 【NOI2019十二省联合省选】部分题简要题解

    Day 1 T1 异或粽子 题意:给出一个长为n的序列,选择K个不完全重合的区间使得每个区间的异或值的总和最大. 题解:先做一个前缀异或和,对于每一个右端点我们记录三元组(l,r,x)表示在左端点在\ ...

  8. 智课雅思词汇---十二、vent是什么意思

    智课雅思词汇---十二.vent是什么意思 一.总结 一句话总结:词根:ven, vent = come, 表示“来” 词根:vent = wind 风 1.tact是什么意思? 词根:-tact-, ...

  9. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】

    2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...

随机推荐

  1. UE4 坐标系 坐标轴旋转轴

    Pitch是围绕Y轴旋转,也叫做俯仰角. Yaw是围绕Z轴旋转,也叫偏航角. Roll是围绕X轴旋转,也叫翻滚角. UE4里,蓝图中的rotation的三个依次为roll,pitch,yaw.C++中 ...

  2. 有容云-PPT | 当微服务遇见容器

    编者注: 本文为10月29日有容云高级技术顾问龙淼在Docker Live时代线下系列-广州站中演讲的PPT,本次线下沙龙为有容云倾力打造Docker Live时代系列主题线下沙龙,每月一期畅聊容器技 ...

  3. web渗透---第二天

    协议常识 HTTP协议 百度百科的解释:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议. 所有的WWW文件都必须遵守这个标准. ...

  4. CentOS 7.3下使用YUM 安装MySQL5.6

    1.检查Linux系统中是否已安装 MySQL rpm -qa | grep mysql 返回空值的话,就说明没有安装 MySQL 注意:在新版本的CentOS7中,默认的数据库已更新为了Mariad ...

  5. 【原创】TextCNN原理详解(一)

    ​ 最近一直在研究textCNN算法,准备写一个系列,每周更新一篇,大致包括以下内容: TextCNN基本原理和优劣势 TextCNN代码详解(附Github链接) TextCNN模型实践迭代经验总结 ...

  6. java文字转语音播报功能的实现方法

    java文字转语音播报功能的实现方法 一.pom.xml引入jar包依赖 <!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字转语 ...

  7. JAVA基础知识(二):List接口、ArrayList类和LinkedList类

    List接口继承了Collection接口,位于java.util包中.它包含Collection接口的所有方法,外加其他一些方法(具体实现参考源码),比较重要的有: anyType get(int ...

  8. 记我的一次 Java 服务性能优化

    背景 前段时间我们的服务遇到了性能瓶颈,由于前期需求太急没有注意这方面的优化,到了要还技术债的时候就非常痛苦了. 在很低的 QPS 压力下服务器 load 就能达到 10-20,CPU 使用率 60% ...

  9. 使用CefSharp在.NET中嵌入Chromium

    使用CefSharp可以在.NET轻松的嵌入Html,不用担心WPF与Winform 控件与它的兼容性问题,CefSharp大部分的代码是C#,它可以在VB或者其他.NET平台语言中来进行使用. 近几 ...

  10. CPU中的cache结构以及cache一致性

    一. 引子 在多线程环境中,经常会有一些计数操作,用来统计线上服务的一些qps.平均延时.error等.为了完成这些统计,可以实现一个多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值.在实 ...