Fabricate equation(dfs + 模拟)

Fabricate equation

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

Submit Status

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

For example: Y=1Y=1, you can find a X=100X=100 so that Z=99Z=99 and KK is 33 due to 1≠01≠0 and 0≠90≠9. But for minimization, we should let X=1X=1 so that Z=0Z=0 and KK can just be 11.

Input

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

Output

The minimal KK.

Sample input and output

Sample Input Sample Output
1
1
191
 

题解:

X−Y=Z(Z≥0),已知Y,求最小K,K定义为Z与X不相同的位置的个数;例如280 - 191 = 89;

X - Z = Y;

根据减法运算我们可以想到Xi - Zi = Yi; 如果数字相同我们可以得到 Xi = Zi;

即 Xi - Xi = Yi; 所以Yi 为0的位置肯定能找到满足的;再者有可能进位,即:10 +Xi -Xi -1 = 9;

所以9也能得到,但是这两者相互影响,取09相邻只能取一个,还有特殊情况9在最后一位,最高的退位,90000对0的影响。。。考虑清就好了;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int a[25];
int ans;
void dfs(int cur, int cnt, int tp, int kg){
//    printf("%d %d\n", cur, tp);
    if(cur >= tp){
        ans = max(ans, cnt);
        return;
    }
    if(a[cur] == 0){
        if(kg != 9 && !(cur == tp - 2 && a[tp - 1] == 9))dfs(cur + 1, cnt + 1, tp, 0);
        else dfs(cur + 1, cnt, tp, 1);
    }
    else if(a[cur] == 9){
        if((kg == 1 || kg == 9) && cur != tp - 1 && cur != 0)
            dfs(cur + 1, cnt + 1, tp, 9);
        else
            dfs(cur + 1, cnt, tp, 1);
    }
    else
        dfs(cur + 1, cnt, tp, 1);
}
int main(){
    LL Y;
    while(~scanf("%lld", &Y)){
        int tp = 0;
        while(Y){
            a[tp++] = Y % 10;
            Y /= 10;
        }
        ans = 0;
        dfs(0, 0, tp, 1);
        printf("%d\n", tp - ans);
    }
    return 0;
}
时间: 2025-01-09 10:50:40

Fabricate equation(dfs + 模拟)的相关文章

HDU 5438 Ponds dfs模拟

2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque&

Vijos P1114 FBI树【DFS模拟,二叉树入门】

描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种.由一个长度为2^N的“01”串S可以构造出一棵FBI树T,递归的构造方法如下: 1) T的根结点为R,其类型与串S的类型相同: 2) 若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2:由左子串S1构造R的左子树T1,由右子串S2构造R的右子树T2. 现在给定一个长度为2^N的“0

hdoj ztr loves lucky numbers 5676 (dfs模拟)

ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 918    Accepted Submission(s): 389 Problem Description ztr loves lucky numbers. Everybody knows that positive integers ar

HDU 1112 The Proper Key DFS+模拟

这题是我做过的好恶心的模拟题之一,WA了两次,要注意很多细节,主要是题意不好懂.这题题意是给你两个字符矩阵,第一个代表钥匙,第二个代表锁.问你,钥匙能否穿过锁,如若不能,输出钥匙插入锁的最大深度. #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #define maxn 1000 #define max

POj 3009 Curling 2.0(DFS + 模拟)

题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0”和“1”组成,目的是计算从“2”走到“3”的最短步数,“1”代表障碍物,“0”代表可以通行.“2”可以往周围四个方向走,如果往某一个方向走,那么停下来的条件是,当这个方向上存在障碍物“1”,且会停在这个障碍物的前一个格子,并会击碎这个障碍物;如果选择的方向上没有障碍物“1”也没有终点“3”,那么就会

蓝桥杯 兰顿蚂蚁 (DFS+模拟)

[题目描述] 历届试题 兰顿蚂蚁 时间限制:1.0s   内存限制:256.0MB 问题描述 兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种. 平面上的正方形格子被填上黑色或白色.在其中一格正方形内有一只"蚂蚁". 蚂蚁的头部朝向为:上下左右其中一方. 蚂蚁的移动规则十分简单: 若蚂蚁在黑格,右转90度,将该格改为白格,并向前移一格: 若蚂蚁在白格,左转90度,将该格改为黑格,并向前移一格. 规则虽然简单,蚂蚁的行为却十分复杂.刚刚开始时留下的路线都会有接近对称

hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1)

UVALive 6884 GREAT + SWERC = PORTO dfs模拟

题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4896 We want to have a great SWERC at Porto this year and we approached this challenge in several ways.We even framed it as a word add

FZU 2108(dfs模拟,大数取余)

K Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2108 Description Given one non-negative integer A and one positive integer B, it’s very easy for us to calculate A Mod B. Here A Mod B means the