Codeforces 58E Expression (搜索)
题意:给你一个可能不正确的算式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 (搜索)的更多相关文章
- codeforces 58E:Expression
Description One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c i ...
- CF58E Expression 搜索
题目传送门:http://codeforces.com/problemset/problem/58/E 题意:给出一个形如$x+y=z$(不一定正确)的式子,试输出一个$a+b=c$的式子,满足:$1 ...
- codeforces 数字区分 搜索
Jokewithpermutation Input file: joke.inOutput file: joke.outJoey had saved a permutation of integers f ...
- Codeforces 161E(搜索)
要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...
- 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 ...
- Codeforces 730L - Expression Queries(大模拟)
Codeforces 题面传送门 & 洛谷题面传送门 大模拟(?)+阿巴细节题,模拟赛时刚了 3h 最后因为某个细节写挂 100->40/ll/ll(下次一定不能再挂分了啊 awa) 首 ...
- Cleaner Robot - CodeForces 589J(搜索)
有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面 ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
- [GodLove]Wine93 Tarining Round #7
比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...
随机推荐
- Android中播放DSD音乐
Github上有个简单的Alsa DSD测试程序,可以播放DSD,地址位于:https://github.com/zonque/alsa-dsd-player 细看其代码,发现有ALSA_FORMAT ...
- 格式化SQL和逆格式SQL
上篇说过SQLyog中的计算合适数据类型的功能,现在说下Navicat格式化SQL和逆格式化SQL 啥也不说了,一看图,一目了然,Beautify SQL(格式化,即变成最美的语句)和Minify S ...
- LeetCode OJ:Largest Number(最大数字)
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- hdoj-2141-Can you find it?(二分查找)
题目链接 /* Name:HDU-2141-Can you find it? Copyright: Author: Date: 2018/4/12 17:15:46 Description: 暴力,复 ...
- BEC translation exercise 4
People have long known that nuts are part of a healthy diet.人们早就知道坚果是健康饮食的一部分.People, who you know w ...
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
- java 实现拖动文件到窗口功能
package tuo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.datatransfer.DataF ...
- 使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】
查看dll导出函数,一般使用Viewdll等第三方工具. VS开发环境中,可以查看32位和64位的dll.具体使用方法如下: 1. 进入VS开发环境,然后Tools -> Visual stud ...
- Win7服务器搭建实例教程:教你Win7如何搭建Web服务器【转载】
原文地址:http://www.pc841.com/article/20140607-30534.html 局域网Web服务器的主要功能是实现资源共享,同时借助于局域网服务器访问页面可有效的实现信息的 ...
- 第五篇 Nginx的简单配置
先安装: sudo apt-get install nginx php5-fpm 我是在新安装的Ubuntu13上测试通过的,真的只安装这两个东西就够了. 然后编辑配置文件. sudo gedit / ...