uestc1039

Fabricate equation

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit  Status

Given an integer  Y Y, you need to find the minimal integer  K K so that there exists a  X X satisfying  XY=Z(Z0) X−Y=Z(Z≥0) and the number of different digit between  X X and  Z Zis  K K under decimal system.

For example:  Y=1 Y=1, you can find a  X=100 X=100 so that  Z=99 Z=99 and  K K is  3 3 due to  10 1≠0 and  09 0≠9. But for minimization, we should let  X=1 X=1 so that  Z=0 Z=0 and  K K can just be  1 1.

Input

Only one integer  Y(0Y1018). Y(0≤Y≤1018).

Output

The minimal  K K.

Sample input and output

Sample Input Sample Output
1
1
191
2
考虑0和9即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 +10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int num[20];
bool vis1[20];
bool vis2[20];
int main()
{
    LL y,cnt,ans,i,j,k;
    while(scanf("%lld",&y)!=EOF){
        memset(vis1,false,sizeof(vis1));
        memset(vis2,false,sizeof(vis2));
        ans=cnt=0;
        while(y){
            num[cnt++]=y%10;
            y/=10;
        }ans=cnt;
        for(i=0;i<cnt;++i){
            if(i==0&&num[i]==0){
                ans--;vis2[i]=true;
            }
            else if(i==0)continue;
            else if(num[i]==9&&(num[i-1]!=0||(!vis2[i-1]))){
                ans--;vis1[i]=true;
            }
            else if(num[i]==0&&(!vis1[i-1])){
                ans--;vis2[i]=true;
            }
        }
        if(vis1[i-1]) ans++;
        printf("%lld\n",ans);
    }
    return 0;
}



你可能感兴趣的:(uestc1039)