zoukankan      html  css  js  c++  java
  • Divisibility by 25 CodeForces

    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 (1n10181≤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
    5071
    Output
    4
    Input
    705
    Output
    1
    Input
    1241367
    Output
    -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+3;
    string str;
    int ans;
    void solve(char a,char b)
    {
        string s=str;
        int cnt1=s.rfind(a),cnt2=s.rfind(b);
        if(a=='0' && b=='0')
            cnt1=s.rfind(a,cnt1-1);
        if(cnt1==string::npos || cnt2==string::npos)
            return ;
        int cnt=0;
        if (cnt1 > cnt2)
        {
            cnt++;
            swap(cnt1, cnt2);
        }
        for (int i = cnt2; i+1 < s.size(); i++)
        {
            cnt++;
            swap(s[i], s[i+1]);
        }
        for (int i = cnt1; i+1 < s.size()-1; i++)
        {
            cnt++;
            swap(s[i], s[i+1]);
        }
        cnt += find_if(s.begin(), s.end(), [](char c){return c != '0';}) - s.begin();
        ans = min(ans, cnt);
    }
        
    
    int main()
    {
        cin>>str;
        ans=INF;
        solve('0','0');
        solve('2','5');
        solve('5','0');
        solve('7','5');
        if(ans == INF)
            ans=-1;
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    慕课网-安卓工程师初养成-3-2 Java中的算术运算符
    慕课网-安卓工程师初养成-3-1 什么是运算符
    慕课网-安卓工程师初养成-2-13 练习题
    慕课网-安卓工程师初养成-2-12 如何在Java中使用注释
    慕课网-安卓工程师初养成-2-11 Java常量
    慕课网-安卓工程师初养成-2-10 Java中的强制类型转换
    试把一个正整数n拆分为若干个
    求解两个给定正整数m、n的最大公约数(m、n)
    统计n!尾部零
    横竖折对称方阵
  • 原文地址:https://www.cnblogs.com/smallhester/p/9500286.html
Copyright © 2011-2022 走看看