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[];
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] == ){
if(kg != && !(cur == tp - && a[tp - ] == ))dfs(cur + , cnt + , tp, );
else dfs(cur + , cnt, tp, );
}
else if(a[cur] == ){
if((kg == || kg == ) && cur != tp - && cur != )
dfs(cur + , cnt + , tp, );
else
dfs(cur + , cnt, tp, );
}
else
dfs(cur + , cnt, tp, );
}
int main(){
LL Y;
while(~scanf("%lld", &Y)){
int tp = ;
while(Y){
a[tp++] = Y % ;
Y /= ;
}
ans = ;
dfs(, , tp, );
printf("%d\n", tp - ans);
}
return ;
}

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

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

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  2. cdoj第13th校赛初赛F - Fabricate equation

    http://acm.uestc.edu.cn/#/contest/show/54 F - Fabricate equation Time Limit: 3000/1000MS (Java/Other ...

  3. HDU 5438 Ponds dfs模拟

    2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...

  4. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  5. 牛客~~扫雷~~~DFS+模拟

    链接:https://www.nowcoder.com/acm/contest/118/F来源:牛客网 题目描述 <扫雷>是一款大众类的益智小游戏,于1992年发行.游戏目标是在最短的时间 ...

  6. zzulioj--1089--make pair(dfs+模拟)

    1809: make pair Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 60  Solved: 44 SubmitStatusWeb Board ...

  7. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. DFS+模拟 ZOJ 3861 Valid Pattern Lock

    题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...

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

     K Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

随机推荐

  1. Qt on Android:让 Qt Widgets 和 Qt Quick 应用全屏显示

    Android 系统版本号非常多,较新的 4.4 ,较老的 2.3 ,都有人用. Qt on Android 开发的 Android 应用.默认在 Android 设备上是非全屏的. 而有些应用的需求 ...

  2. QtWebkit2.2.0 HTML5.0支持情况

      Canvas: 支持element, 2d context以及文本 解析规则:支持 HTML5 tokenizer/tree building,  SVG in text/html, MathML ...

  3. Ubuntu中设置静态IP和DNS(转载)

    原文地址:http://blog.sina.com.cn/s/blog_669421480102v3bb.html VMware 中使用网络,对虚拟机设置静态IP:在Ubuntu中设置静态IP共两步: ...

  4. 使用RDS不得不知的注意事项

    使用RDS不得不知的注意事项 1.RDS实例升级需要注意的事项 RDS在进行实例升级的过程中会出现最长30秒左右的连接闪断,需要您提前做好准备,并设置好程序跟RDS的自动重连,避免因为升级的闪断导致您 ...

  5. jquery商城类封装插件

    自从解决了定时器的问题后,什么都好弄了 这是仿苏宁商城banner的,当然我没弄得那么好啦,但是我想就是那个缩略图,我没弄好吧,方法我猜想是通过把所有li都放进数组,然后通过遍历,就可以做出相应的效果 ...

  6. 让qq图标在自己的网站上显示方法

    代码如下: <div id="xixi" onmouseover="toBig()" style="top: 260px; left: 5px; ...

  7. 一个Banner广告收缩效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Java的Object对象

    Object对象是除了基础对象之外,所有的对象都需要继承的父对象,包括数组也继承了Object Object里面的关键函数罗列如下: clone();调用该函数需要实现 Cloneable,否则会抛出 ...

  9. 理解SQL SERVER中的分区表(转)

    简介 分区表是在SQL SERVER2005之后的版本引入的特性.这个特性允许把逻辑上的一个表在物理上分为很多部分.而对于SQL SERVER2005之前版本,所谓的分区表仅仅是分布式视图,也就是多个 ...

  10. table超过30个字段如何处理呢? bootstrap

    样式: @media (max-width: 768px) { .table-supplier { width: 100%; height: 100%; margin-bottom: 12.75px; ...