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剩下的数就可以了,算一下位数。当然要加一个最优性剪枝。

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 int ans,ansa,ansb;
16 LL mi[19];
17
18 inline int getint()
19 {
20        int w=0,q=0; char c=getchar();
21        while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if(c==‘-‘) q=1,c=getchar();
22        while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); return q ? -w : w;
23 }
24
25 inline void dfs(LL a,LL b,LL c,LL nowa,LL nowb,LL jin,int nowl,int wei){
26     if(nowl>=ans) return ;
27     if(a==0&&b==0&&c==0&&jin==0) { ans=nowl; ansa=nowa; ansb=nowb; return ; }
28     if(c==0) {
29     int tot=0; LL lin=a+b+jin; while(lin) tot++,lin/=10;//全部加给c
30     dfs(0,0,0,nowa+a*mi[wei],nowb+b*mi[wei],0,nowl+tot,wei);
31     return;
32     }
33     if((a+b+jin)%10==c%10) dfs(a/10,b/10,c/10,nowa+a%10*mi[wei],nowb+b%10*mi[wei],(a%10+b%10+jin)/10,nowl,wei+1);//去掉已经相等的低位部分,记得给公共的低位部分进位
34     else{
35     dfs(a*10+(c+10-b%10-jin)%10,b,c,nowa,nowb,jin,nowl+1,wei);//a后面加一位与前两个数还有进位的和的个位部分
36     dfs(a,b*10+(c+10-a%10-jin)%10,c,nowa,nowb,jin,nowl+1,wei);//b后面加一位与前两个数还有进位的和的个位部分
37     dfs(a,b,c*10+(a+b+jin)%10,nowa,nowb,jin,nowl+1,wei);///c后面加一位与前两个数还有进位的和的个位部分
38     }
39 }
40
41 inline void work(){
42     int a,b,c;  scanf("%d+%d=%d",&a,&b,&c);
43     ans=12; mi[0]=1; for(int i=1;i<=18;i++) mi[i]=mi[i-1]*10;
44     dfs(a,b,c,0,0,0,0,0);
45     printf("%d+%d=%d",ansa,ansb,ansa+ansb);
46 }
47
48 int main()
49 {
50   work();
51   return 0;
52 }
时间: 2025-01-02 19:10:39

codeforces 58E:Expression的相关文章

Codeforces 449D:Jzzhu and Numbers

Codeforces 449D:Jzzhu and Numbers 题目链接:http://codeforces.com/problemset/status?friends=on 题目大意:给出$n$个数,求有多少种组合使得$a_{i_1}\&a_{i_2}\&...\&a_{i_k}=0(0 \leqslant i < n)$,答案对$10^9+7$取模. 容斥原理+DP 设位与$(\&)$后二进制表示中有$k$个$1$的组合数为$A_k$,则有, $A_0=$所有

Codeforces 757B:Bash&#39;s Big Day(分解因子+Hash)

http://codeforces.com/problemset/problem/757/B 题意:给出n个数,求一个最大的集合并且这个集合中的元素gcd的结果不等于1. 思路:一开始把素数表打出来,发现有9k+个数,不能暴力枚举.发现O(sqrt(n)*n)似乎可行,就枚举因子,然后出现过的因子就在Hash[]加1,最后枚举素数表里的元素,找出现次数最多的,因为那些数都可以映射在素数表里面.注意最少的ans是1. 1 #include <cstdio> 2 #include <algo

Codeforces 754E:Dasha and cyclic table

Codeforces 754E:Dasha and cyclic table 题目链接:http://codeforces.com/problemset/problem/754/E 题目大意:$A$矩阵($size(A)=n \times m$,仅含'a'-'z')在整个平面做周期延拓,问$B$矩阵($size(B)=r \times c$,包含'a'-'z'及'?','?'为通配符)在哪些位置能与$A$矩阵匹配.输出$n \times m$的01矩阵,1表示在该位置匹配. 枚举+bitset常

解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column &#39;information_schema.PROFILING.SEQ&#39;

mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_fu

Codeforces 798D:Mike and distribution

Codeforces 798D:Mike and distributio 题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:给出两个大小为$n$的数列$A,B$,现要求从这两个数列相同位置取出$K(K \leqslant n/2+1)$个数,使得$2 \times subA>sumA$且$2 \times subB>sumB$. 想法题 我们需要从数列$A$和数列$B$中取出$K$个数,使得这$K$个数的和比剩下$n-K$个数的和

[转]打造自己的LINQ Provider(上):Expression Tree揭秘

概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHibernate.LINQ to Google等,大有“一切皆LINQ”的趋势.LINQ本身也提供了很好的扩展性,使得我们可以轻松的编写属于自己的LINQ Provider. 本文为打造自己的LINQ Provider系列文章第一篇,主要介绍表达式目录树(Expression Tree)的相关知识. 认识表

Codeforces 855B:Marvolo Gaunt&#39;s Ring(枚举,前后缀)

B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is sti

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {

Codeforces 747C:Servers(模拟)

http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开始,如果可以完成任务,那么输出id总和,否则输出-1. 思路:简单的模拟,注意如果不能完成任务,那么ser数组是不能更新的. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #includ