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. 【pycharm】pycharm远程连接服务器的Python解释器,远程编写代码!!!

    今天讲讲如何用pycharm连接远程服务器,使用远程服务器的Python解释器,比如说是你公司的服务器,在家里就可以编写或修改项目的代码! 第一步,先找到服务器上的ip地址 Linux查看IP命令:i ...

  2. 自定义SWT控件七之自定义Shell(可伸缩窗口)

    7.可伸缩窗口 该自定义窗口可以通过鼠标随意更改窗口大小 package com.hikvision.encapsulate.view.control.shell; import org.eclips ...

  3. 当下最流行的微服务与spring cloud,你搞清楚了吗?

    微服务架构:Spring-Cloud 什么是微服务? 微服务就是把原本臃肿的一个项目的所有模块拆分开来并做到互相没有关联,甚至可以不使用同一个数据库. 比 如:项目里面有User模块和Power模块, ...

  4. asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)

    常规的中小型项目搭建方式一般是三层架构加上mvc与webapi作为一个主要框架,再加上一些第三方库,例如orm框架(EF.SqlSugar.Dapper等),API文档工具(Swagger)这些的应用 ...

  5. JAVA基础知识(四):final关键字

    final关键字可以用于成员变量.本地变量.方法以及类. 2. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. 3. 你不能够对final变量再次赋值. 4. 本地 ...

  6. 使用webstorm调试node.js

    折腾半天,还是webstorm顺手,但也遇到一些小问题. 1.代码补全问题 nodeJS自身的补全 File->Project Setting->JavaScript->Librar ...

  7. Linux系统关机与重新引导流程简介

    引言 在<Linux启动之旅>中,我们了解了Linux的启动过程,下面我们一同来学习Linux关机与重新引导流程. 不同于桌面系统,作为服务器,我们较少对Linux系统进行系统重启,但在以 ...

  8. vs2013 在按F5调试时,总是提示 “项目已经过期”的解决方案

    这个是由于缺少某些文件(如.h,xxx.icon),或者文件时间不对 引起的. 如图在工具选项设置 最小为 “诊断”. 然后编译一下,会提示 xxx过期,确认下即可.

  9. 如何以python风格高逼格的改成购物车逻辑

    之前有一篇博文写到关于购物车的业务逻辑,分别运用cookie和redis存储未登录和登录用户的购物车数据,虽然已经很好的完成了业务逻辑,但是会发现代码的冗余很严重,也不够具有python特色,今天就让 ...

  10. 中间件增强框架之InterceptFramework

    本文讲解MOF中的InterceptFramework框架.该框架可以在应用启动过程中获取画像信息,实现应用画像数据采集和存储. 一.前言 在智能运维中,应用服务所使用的组件及JAR包等相关信息非常重 ...