1. 程式人生 > >CF 988E Divisibility by 25 思維 第十二

CF 988E Divisibility by 25 思維 第十二

without its 可能 tty ID NPU left rap sta

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

CF 988E Divisibility by 25 思維 第十二