Divisibility by 25 CodeForces - 988E
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
5071
4
705
1
1241367
-1
Note
In the first example one of the possible sequences of moves is 5071 →→ 5701 →→ 7501 →→ 7510 →→ 7150.
题意:一个大数,只能移动相邻的两位,问移动几次后可以除以25后没有余数
思路:一个模拟,末尾是00,25,50,75就可以了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const long long int maxn=2e5+;
string str;
int ans;
void solve(char a,char b)
{
string s=str;
int cnt1=s.rfind(a),cnt2=s.rfind(b);
if(a=='' && b=='')
cnt1=s.rfind(a,cnt1-);
if(cnt1==string::npos || cnt2==string::npos)
return ;
int cnt=;
if (cnt1 > cnt2)
{
cnt++;
swap(cnt1, cnt2);
}
for (int i = cnt2; i+ < s.size(); i++)
{
cnt++;
swap(s[i], s[i+]);
}
for (int i = cnt1; i+ < s.size()-; i++)
{
cnt++;
swap(s[i], s[i+]);
}
cnt += find_if(s.begin(), s.end(), [](char c){return c != '';}) - s.begin();
ans = min(ans, cnt);
} int main()
{
cin>>str;
ans=INF;
solve('','');
solve('','');
solve('','');
solve('','');
if(ans == INF)
ans=-;
cout<<ans<<endl;
return ;
}
Divisibility by 25 CodeForces - 988E的更多相关文章
- Divisibility by 25 CodeForces - 988E (技巧的暴力)
You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...
- Divisibility by 25 CodeForces - 988E(模拟)
遇见模拟题 有两种做法 例如这题: 1.直接去算次数(统计哪个数在第几位,然后去运算) 2.模拟操作 贴一个别人的代码...https://blog.csdn.net/weixin_39453270/ ...
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- CF 988E Divisibility by 25 思维 第十二
Divisibility by 25 time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 988E. Divisibility by 25
解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...
- Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类
传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...
- 题解 【Codeforces988E】Divisibility by 25
本题是数论好题! 首先我们需要了解一个关于数论的性质: 一个数只要后两位能被25(或4)整除,这个数就能被25(或4)整除. 同理,后三位:(或8).后四位:(或16)亦是如此. 所以,我们只需要判断 ...
- 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3
◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
随机推荐
- NPOI导出EXCEL后公式无结果,公式重新计算开关
但其实这个选项是可以控制的.在NPOI中,这个属性叫做XSSFSheet.ForceFormulaRecalculation. 打开该选项的话,代码为 sheet1.ForceFormulaRecal ...
- vs2012 support BI
Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2012 http://www.microsoft. ...
- Ubuntu 16.04 以太坊开发环境搭建
今天我们来一步一步从搭建以太坊智能合约开发环境. Ubuntu16.04 安装ubuntu16.04.下载链接 //先update一下(或者换国内源再update) sudo apt-get upda ...
- LCA 离线做法tarjan
tarjan(int u) { int v; for(int i=h[u];i;i=nex[i])//搜索边的 { v=to[i]; tarjan(v); marge(u,v); vis[v]=; } ...
- IQueryable和IEnumerable的区别
- hubbledotnet 使用笔记
Hubble vs 字符串 <connectionStrings> <add name="Search" connectionString="serve ...
- ListView的圆角的简单实现
今天在用ListView控件的时候,发现自带的不美观,就是找找相关的美化教程,发现都挺麻烦的,无意中发现一个开源项目,地址:点击打开链接,使用起来很简单,官方网站说的也很简单,就是导入库,然后像其他控 ...
- css:focus伪类的使用
css中:focus伪类的使用,即给已获取焦点的元素设置样式 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...
- Unity3d通过脚本生成apk
参考链接:http://www.jianshu.com/p/a9261113b4ac 照着链接的方法并没有正确生成APK,IPA没有测试过,不过大致的方法是正确的,修改如下: Environment. ...
- 51nod 1489 蜥蜴和地下室
题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 哈利喜欢玩角色扮演的电脑游戏<蜥蜴和地下室>.此时,他正在扮演一个魔术 ...