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. TBB入门

    获取TBB TBB的官方网站在http://threadingbuildingblocks.org/,可以在它的Downloads页面里找到Commercial Aligned Release,最新版 ...

  2. CSS Gradient详解

    啥也不说,先看代码: <style> div { width:100px;height:100px;border-radius:50px;/**第一行样式是设置容器的宽高,并且把圆角设置成 ...

  3. 前端--关于HTML

    在讲HTML之前不得不先简单粗略提一下浏览器以及浏览器与HTML的关系.众所周知,浏览器就是一个应用程序,这个应用程序可以完成网络调用.展示接收的html文档等.严格来讲HTML文档就是按照某个规则写 ...

  4. jQuery之.html()和.text()区别

    .html()//获取标签和内容 .text()//只获取内容

  5. PL/SQL破解方法(不需要注册码)

    打开注册表在run下输入regedit删除1.HKEY_CURRENT_USER/Software/Allround Automations2.HKEY_CURRENT_USER/Software/M ...

  6. 重写OnPaint事件对窗体重绘 实例1

    public class WindowOne:Form { public WindowOne() { InitializeCompnent(); } public void InitializeCom ...

  7. jwplayer修改logo右键版权

    jwplayer二次编译,可以自定义自己的logo和右键版权.

  8. 解决linux下oracle进入sqlplus环境中后退键显示^H、上下键无效与ctrl+l无法清屏等问题【weber出品必属精品】

    习惯linux中上下键回退历史输入的人们肯定也希望sqlplus中也能实现相同的效果,可是不幸的是,sqlplus中不提供诸多方便的快捷键,这个时候我们就需要另外安装一个插件来实现这个想法. 这个插件 ...

  9. SVN的初步使用方法

    1.需要公司提供SVN账号密码 2.cd 输入本地存储路径 3.输入服务器地址 --uesr= (账号名) --password=(账号密码) 4.本地路径会自动创建文件 5.经理会初始化项目 5.1 ...

  10. UI基础视图----UIScrollView总结

    UIScrollView是UIKit框架下的很重要的视图类,是UIView的子类.UILabel,UIImageView,UIWebView分别用于展示文字,图片,网页,UILabel通过属性text ...