codeforces 58E:Expression
Description
One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c in his notebook. When the teacher checked Vasya's work it turned out that Vasya had solved the problem incorrectly. Now Vasya tries to find excuses. He says that he simply forgot to write down several digits in numbers a, b and c, but he can't remember what numbers they actually were. Help Vasya, find such numbers x, y and z, with which the following conditions are met:
x + y = z,
from the expression x + y = z several digits can be erased in such a way that the result will be a + b = c,
the expression x + y = z should have the minimal length.
Input
The first and only input line contains the expression a + b = c (1 ≤ a, b, c ≤ 106, a, b and c don't contain leading zeroes) which is the expression Vasya wrote down.
Output
Print the correct expression x + y = z (x, y and z are non-negative numbers without leading zeroes). The expression a + b = c must be met in x + y = z as a subsequence. The printed solution should have the minimal possible number of characters. If there are several such solutions, you can print any of them.
Examples
Input
2+4=5
Output
21+4=25
Input
1+1=3
Output
1+31=32
Input
1+1=2
Output
1+1=2
正解:搜索
解题报告:
今天考试T6,9道题里面唯一一道没动的,开始以为是E题就会很难...
简单思路就是搜索,每次看一下当前的个位是否相等,如果相等,那么显然可以约掉这个已经相等的个位并只处理高位,当然记得进位;否则,我们考虑a、b、c三个元素,保持两个不变,我们把第三个增加一位使得末位与另外两位对应,然后其余部分直接往前推一位,也就是×10。直到c=0,那么肯定只需要把c的最高位补一个a+b剩下的数就可以了,算一下位数。当然要加一个最优性剪枝。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
int ans,ansa,ansb;
LL mi[]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void dfs(LL a,LL b,LL c,LL nowa,LL nowb,LL jin,int nowl,int wei){
if(nowl>=ans) return ;
if(a==&&b==&&c==&&jin==) { ans=nowl; ansa=nowa; ansb=nowb; return ; }
if(c==) {
int tot=; LL lin=a+b+jin; while(lin) tot++,lin/=;//全部加给c
dfs(,,,nowa+a*mi[wei],nowb+b*mi[wei],,nowl+tot,wei);
return;
}
if((a+b+jin)%==c%) dfs(a/,b/,c/,nowa+a%*mi[wei],nowb+b%*mi[wei],(a%+b%+jin)/,nowl,wei+);//去掉已经相等的低位部分,记得给公共的低位部分进位
else{
dfs(a*+(c+-b%-jin)%,b,c,nowa,nowb,jin,nowl+,wei);//a后面加一位与前两个数还有进位的和的个位部分
dfs(a,b*+(c+-a%-jin)%,c,nowa,nowb,jin,nowl+,wei);//b后面加一位与前两个数还有进位的和的个位部分
dfs(a,b,c*+(a+b+jin)%,nowa,nowb,jin,nowl+,wei);///c后面加一位与前两个数还有进位的和的个位部分
}
} inline void work(){
int a,b,c; scanf("%d+%d=%d",&a,&b,&c);
ans=; mi[]=; for(int i=;i<=;i++) mi[i]=mi[i-]*;
dfs(a,b,c,,,,,);
printf("%d+%d=%d",ansa,ansb,ansa+ansb);
} int main()
{
work();
return ;
}
codeforces 58E:Expression的更多相关文章
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...
- [转]打造自己的LINQ Provider(上):Expression Tree揭秘
概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...
- ASP.NET MVC:Expression Trees 作为参数简化查询
ASP.NET MVC 引入了 ModelBinder 技术,让我们可以在 Action 中以强类型参数的形式接收 Request 中的数据,极大的方便了我们的编程,提高了生产力.在查询 Action ...
- 解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'
mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...
- 打造自己的LINQ Provider(上):Expression Tree揭秘
概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...
- js进阶正则表达式实现过滤字符串(RegExp对象操作正则表达式)(正则:regular)(表达式:expression)
js进阶正则表达式实现过滤字符串(RegExp对象操作正则表达式)(正则:regular)(表达式:expression) 一.总结 1.str_replace:正则作用:高效快速匹配 2.break ...
- Codeforces 58E Expression (搜索)
题意:给你一个可能不正确的算式a + b = c, 你可以在a,b,c中随意添加数字.输出一个添加数字最少的新等式x + y = z; 题目链接 思路:来源于这片博客:https://www.cnb ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
随机推荐
- A*寻路初探 GameDev.net
A*寻路初探 GameDev.net MulinB按:经典的智能寻路算法,一个老外写的很透彻很清晰,很容易让人理解神秘的A*算法.以下是一个中文翻译版. A*寻路初探 GameDev.net 作者: ...
- 测试 Mono 安装
测试 Mono 安装 为了测试核心编译器(mcs)和运行时(mono),应该创建一个简单的程序并编译它.可以在喜欢的任何文本编辑器中创建程序.这里采用一种快速而简陋的方法创建该文件(虽然没有任何格式化 ...
- QC学习三:Excel数据导入导出QC操作流程
环境: QC9 WindowsXP Office2007 1. 准备 1.通过Excel导入QC,需要下载Microsoft Excel Add-in: http://update.externa ...
- java 21 - 6 字符缓冲流的特殊方法以及该方法高效复制文件
字符缓冲流的特殊方法: A.BufferedWriter: public void newLine():根据系统来决定换行符 private static void write() throws IO ...
- Android的Style的使用
Style个人理解就是view的一些属性的集合,那么一系列view(例如TextVIew),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改 首先最基本的使用,多个textV ...
- 关于Yii2中count方法的使用
统计文章与分类中间表中c_id的数目,也就是category表中total字段的值 原生SQL语句:select count(c_id) from article_category where c_i ...
- Redis缓存数据库详解
Redis最为常用的数据类型主要有以下五种: 1)String 2)Hash 3)List 4)Set 5)Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Redis内部内存 ...
- zepto.js 源码解析
http://www.runoob.com/w3cnote/zepto-js-source-analysis.html Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...
- android intent隐式调用之一个应用程序启动另一个应用程序
理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似:另一种是隐式的Intent,即Inten ...
- 北京联想招聘-IOS高级 加入qq 群:220486180 或者直接在此 留言咨询
ios 高级开发 Job ID #: 47980 Position Title: 高级iOS development engineer Location: CHN-Beijing Functional ...