题意:给你一个可能不正确的算式a + b = c, 你可以在a,b,c中随意添加数字。输出一个添加数字最少的新等式x + y  = z;

题目链接

思路:来源于这片博客:https://www.cnblogs.com/ljh2000-jump/p/5886279.html

我们可以从个位开始搜索。如果现在c % 10 == (a % 10 + b % 10 + pre) % 10 (pre是之前的进位),那么直接把a, b, c的个位消去,加上进位继续深搜。如果不满足这个条件,我们可以考虑从a, b, c3个数中选一个数添加满足关系的一位,然后继续深搜。

代码:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int ans = INF;
LL ans_a, ans_b, ans_c;
LL p[20];
void dfs(LL a, LL b, LL c, LL nowa, LL nowb, LL nowc, LL pre, int now, int deep) {
if(now >= ans) return;
if(a == 0 && b == 0 && c == 0 && pre == 0) {
ans = now;
ans_a = nowa;
ans_b = nowb;
ans_c = nowc;
return;
}
if(c == 0) {
LL tmp = a + b + pre;
int cnt = 0;
while(tmp) {
cnt++;
tmp /= 10;
}
dfs(0, 0, 0, nowa + p[deep] * a, nowb + p[deep] * b, nowc + p[deep] * (a + b + pre), 0, now + cnt, deep + 1);
return;
}
if((a + b + pre) % 10 == c % 10) {
dfs(a / 10, b / 10, c / 10, nowa + (a % 10) * p[deep], nowb + (b % 10) * p[deep], nowc + (c % 10) * p[deep], (a %10 + b % 10 + pre) / 10, now, deep + 1);
return;
}
dfs(a * 10 + (c % 10 - b % 10 - pre + 10) % 10, b, c, nowa, nowb, nowc, pre, now + 1, deep);
dfs(a, b * 10 + (c % 10 - a % 10 - pre + 10) % 10, c, nowa, nowb, nowc, pre, now + 1, deep);
dfs(a, b, c * 10 + (a % 10 + b % 10 + pre) % 10, nowa, nowb, nowc, pre, now + 1, deep);
}
int main() {
LL a, b, c;
scanf("%lld+%lld=%lld", &a, &b, &c);
p[0] = 1;
for (int i = 1; i <= 18; i++)
p[i] = p[i - 1] * 10;
dfs(a, b, c, 0, 0, 0, 0, 0, 0);
printf("%lld+%lld=%lld",ans_a, ans_b, ans_c);
}

  

Codeforces 58E Expression (搜索)的更多相关文章

  1. codeforces 58E:Expression

    Description One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c i ...

  2. CF58E Expression 搜索

    题目传送门:http://codeforces.com/problemset/problem/58/E 题意:给出一个形如$x+y=z$(不一定正确)的式子,试输出一个$a+b=c$的式子,满足:$1 ...

  3. codeforces 数字区分 搜索

    Jokewithpermutation Input file: joke.inOutput file: joke.outJoey had saved a permutation of integers f ...

  4. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...

  5. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  6. Codeforces 730L - Expression Queries(大模拟)

    Codeforces 题面传送门 & 洛谷题面传送门 大模拟(?)+阿巴细节题,模拟赛时刚了 3h 最后因为某个细节写挂 100->40/ll/ll(下次一定不能再挂分了啊 awa) 首 ...

  7. Cleaner Robot - CodeForces 589J(搜索)

    有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面 ...

  8. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  9. [GodLove]Wine93 Tarining Round #7

    比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...

随机推荐

  1. doeNET Framework 农历 ChineseLunisolarCalendar

    C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe test.cs # test.cs using System; using System.Diagnos ...

  2. mybatis报Error updating database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String

    mybatis 3.3.0中对于时间参数进行比较时的一个bug. 如果拿传入的时间类型参数与空字符串''进行对比判断则会引发异常. 所以在上面的代码中去该该判断, 只保留非空判断就正常了 <if ...

  3. 从jQuery学细节

    前言 最近看了两遍jQuery源码,感觉只是看懂了jQuery的小部分小部分,不过仅此,就已经对john resig佩服的五体投地咯.. 下面附上这位帅哥的靓照,记住吧,是他改变了世界. 看的大多是实 ...

  4. Mysql系列:高可用(HA)-keeplived

    转自:晓叹星沉 https://my.oschina.net/blueSky4Java/blog/1572905 摘要: 随着项目的发展,为了提高程序的性能,数据库层面或多或少的会用到HA.读写分离. ...

  5. 洛谷 P1854 花店橱窗布置

    题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V是花瓶的数目.花束可以移动,并且每束花用1到F的整数标识 ...

  6. 学习动态性能表(12)--v$db_object_cache

    学习动态性能表 第12篇--V$DB_OBJECT_CACHE  2007.6.4 本视图提供对象在library cache(shared pool)中对象统计,提供比v$librarycache更 ...

  7. gulp之文件合并以及整合html中的script和link

    gulp的文件合并,也就是将多个js或css文件合并为一个的插件是:gulp-concat gulp将html中的多个<script>或<link>合并为一个的插件是:gulp ...

  8. Nagios 里面监控MySQL事务一直RUNNING没有结束的报警Shell脚本 (转)

    序言:        业务报警订单提交异常,页面一直没有反应,排查后是事务没有提交或者回滚导致,想到如果及时监控事务的运行状态报警出来,那么就可以及时排查出问题所在,方便运营处理,所以自己就弄了一个s ...

  9. jenkins插件

    构建maven项目:Maven Release Plug-in Plug-in

  10. Java基础--读写文件

    Java读写文件需要注意异常的处理,下面是一个例子 写文件 public class WriteFile { public static void write(String file, String ...