题意:给你一个可能不正确的算式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. web自动化:selenium原理和元素定位(一)

    一. Selenium2 WebDriver 当Selenium2.x提出了WebDriver的概念后,它提供了完全另外的一种方式与浏览器交互 那就是利用浏览器原生的API,封装成一套更加面向对象的S ...

  2. BW信息STMS传输

    项目时先接过来的.前面的人建了很多数据源,信息对象.我也建了不少.最后分成三个包.非常混乱,信息对象不在一个包里.导入报错.先后顺序也没有逻辑.可为难我了. 本来想着是全部释放掉,然后在传输连接收集自 ...

  3. 32 python 并发编程之协程

    一 引子 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去 ...

  4. 3.4 常用的两种 layer 层 3.7 字体与文本

    3.4 常用的两种 layer 层  //在cocos2d-x中,经常使用到的两种 layer 层 : CCLayer 和 CCLayerColor //CCLayer 的创建 CCLayer* la ...

  5. linux下的制作静态库并使用它

    静态库所要用的源文件 文件 fred.c   #include <stdio.h>   void fred(int argc) {     printf("void fred(i ...

  6. CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)

    Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. Howev ...

  7. 2017-2018-1 20179215《Linux内核原理与分析》第十二周作业

    Sql注入基础原理介绍 分组:和20179205王雅哲共同完成实验 一.实验说明 1.1 sql注入  SQL注入攻击通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组 ...

  8. java 简单实现socket

    server端 package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  9. Node.js + Express

    相关链接: 1.https://www.jianshu.com/p/db4df1938eca 2.前端发起GET请求:http://localhost:3000/api/login?name=admi ...

  10. BZOJ2724:[Violet 6]蒲公英

    浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...