题意:给你一个可能不正确的算式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. POJ1741 经典树分治

    题意:有一棵树,每条边有一个距离,求dis(u,v)<=k的点的对数 题解:树分治,对于一颗树上的两点,要么在同一颗子树上,要么在不同子树上,要么一个点是根,另一个在某一子树上,对于第一种情况我 ...

  2. linux部署python和加入mysqldb、easy_install

    一.安装easy_install 参考文章: http://www.cnblogs.com/huangjacky/archive/2012/03/28/2421866.html 安装 wget htt ...

  3. 关于调接口和腾讯云cos方面。

    腾讯云的cos js jdk那个文档使用说明不好用. 都没看懂,而且图片上传也没有具体的详细.对于新手来说强制使用这个,弄得自己一头雾水. 工作效率就会下降. 为此我在网上搜了对象存储cos的常见错误 ...

  4. java学习笔记--常用类

    一.Math类:针对数学运算进行操作的类 1.常用的方法 A:绝对值   public static int abs(int a) B:向上取整  public static double ceil( ...

  5. MySQL 添加、查看字段注释

    语法: 创建表时的COMMENT内容,要查看这些内容,使用命令: show full fields from '表名称'; 查看tb_usr表字段注释: 创建新表的脚本中, 可在字段定义脚本中添加co ...

  6. windows下matplotlib的安装

    在上一篇中我想用matplotlib,无奈一直装不上,就在卸了又装装了又卸,反反复复之后,终于装好了. 初学python,首先就装了numpy,倒也没有多复杂,有需要的朋友可以直接http://sou ...

  7. Java进阶知识点3:更优雅地关闭资源 - try-with-resource及其异常抑制

    一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制, ...

  8. Eclipse中Maven配置操作

    1.修改为自己的maven路径 2.对应的自己的仓库设置

  9. C# 代码注释和Config文件中,特殊符号的书写方法。

    App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> ...

  10. nodejs渲染到页面的理解

    一般逻辑都是: 打开页面,前端发请求到服务端,服务端返回数据到前端,前端根据数据生成DOM节点,然后append到DOM中. 如果是nodejs渲染到页面,我的理解是: 打开页面,服务端直接把数据查询 ...