题目描述
You are given a string S consisting of digits between 1 and 9, inclusive. You can insert the letter + into some of the positions (possibly none) between two letters in this string. Here, + must not occur consecutively after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results.

Constraints
1≤|S|≤10
All letters in S are digits between 1 and 9, inclusive.

输入
The input is given from Standard Input in the following format:
S

输出
Print the sum of the evaluated value over all possible formulas.

样例输入
125

样例输出
176

提示
There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

125
1+25=26
12+5=17
1+2+5=8
Thus, the sum is 125+26+17+8=176.

思路
二进制暴力,将插入加号的位置视为二进制的一位,当其为1时,将其视为‘+’号,否则则不插入

代码实现
string模拟法

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=105;
string s;
int a[N];
ll ans;
ll getsum(string t)
{ll sum=0,tmp=0;for(int i=0;i<t.size();i++){if(t[i]=='+'){sum+=tmp;tmp=0;}else tmp=tmp*10+t[i]-'0';}return sum+tmp;
}
int main()
{cin>>s;int l=s.size()-1;while(1){string temp;for(int i=0;i<l;i++){a[i+1]+=a[i]/2;a[i]%=2;}if(a[l]) break;for(int i=0;i<l;i++){temp+=s[i];if(a[i]) temp+="+";}temp+=s[l];ans+=getsum(temp);a[0]++;}cout<<ans<<endl;return 0;
}

二进制实现

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=105;
char s[N];
ll ans;
int main()
{scanf("%s",s);int l=strlen(s);int m=1<<(l-1);for(int i=0;i<m;i++){ll t=s[0]-'0';for(int j=0;j<l;j++){if(j==l-1 || i&1<<j) //当i&1<<j ==1 时 说明i的第j位 =1(二进制下){ans+=t;t=0;if(j==l-1) break;}t=t*10+s[j+1]-'0';}}cout<<ans<<endl;return 0;
}

Many Formulas(二进制暴力)相关推荐

  1. Many Formulas(暴力+有技巧)

    题目链接:http://exam.upc.edu.cn/problem.php?id=6467 题意:给你一个数字,在数字里任意加+号,问所有情况下,这个sum是多少 我本来想了一种,后来发现这个题真 ...

  2. USACO健康的荷斯坦奶牛(DFS,二进制暴力枚举)

    农夫约翰以拥有世界上最健康的奶牛而感到自豪. 奶牛想要保持健康,每天就要补充足量的多种维生素. 约翰为奶牛们准备了多种牛饲料,每种牛饲料中都富含奶牛所需的多种维生素,但是每种维生素的具体含量可能并不相 ...

  3. Atcoder 045C - Many Formulas(二进制枚举)

    在给定的一段数字间添加+号构成等式,再求和.求所有可能的等式的和.每两个数字之间要么有+要么没有+,这可以对应于"1"和"0".所以这个题就一个怎么放" ...

  4. 51 nod 1625 夹克爷发红包【贪心、二进制枚举】

    1625 夹克爷发红包 题意: 1.给你一个 nn 行 mm 列的二维矩阵,代表每个人的初始红包值. 2.夹克爷可以至多给 kk 行 / 列的每个人发红包,每一个价值为 xx,同时会覆盖掉初始红包. ...

  5. CCPC-Wannafly Winter Camp Day8 (Div2, onsite) 补题

    A Aqours 题解: https://www.cnblogs.com/qieqiemin/p/11251645.html D:吉良吉影的奇妙计划 (暴力打表) 题目描述 吉良吉影是一个平凡的上班族 ...

  6. 2020.7.18【算协集训】[第1次积分赛]

    第1次积分赛 Problem A. 真假签到题 分析 代码 Problem B. 渣渣伟的贪玩红月 分析 代码 Problem C. 那个雨季 分析 代码 Problem D. 憨憨龙的烦恼 分析 代 ...

  7. [USACO06NOV]玉米田Corn Fields题解

    [USACO06NOV]玉米田Corn Fields题解 --HM 题目描述 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都 ...

  8. 2020ICPC昆明题解

    最近VP了2021年的昆明区域赛,补几道题并总结一下教训 文章目录 前言 A.AC C. Cities G.Gift J.Parallel Sort L.Simone and graph colori ...

  9. 数位dp ---- 暴力 + 二进制的数位dp 2020济南 L Bit Sequence

    题目链接 题目大意 f(x)=x的二进制中1的个数f(x)=x的二进制中1的个数f(x)=x的二进制中1的个数 给你一个数组[a1,a2,a3...,am]m∈[1,100][a_1,a_2,a_3. ...

最新文章

  1. VS2013 编译使用 stlport
  2. Matplotlib实例教程(九)热力图
  3. 样式缓存没更新_差点没认出来:Office 2019/365桌面新图标来啦
  4. win7系统开机蓝屏提示hardware Monitor的解决方法
  5. excel删除无尽空白行_【辅导员说】学好办公软件word、excel、ppt的意义
  6. cad插件制作教程_CAD电子签名制作教程
  7. ES6的开发环境搭建
  8. 大整数相乘 + 分治法(JS)
  9. 准确率(accuracy),精确率(Precision),召回率(Recall)和F1-Measure
  10. Atitit.搜索引擎内容来源解决之道
  11. 自动化运维--python_自动化-设计师的朋友还是敌人?
  12. 高等应用数学问题的matlab求解pdf,《高等应用数学问题的matlab求解》(薛定宇 陈阳泉)[PDF]...
  13. java eclipse 下载_JAVA下载安装及eclipse下载安装
  14. 使用dom4j把XML文件解析成JavaBean
  15. 一个PHP的QRcode类,与大家分享
  16. 信息系统项目管理师考试论文写作复习笔记(4)-范围管理论文2
  17. 连点器安卓手机版_【百度识图器安卓下载】百度识图器官方app下载 v3.6.0 手机版...
  18. PayPal的钱如何提现?PayPal商家终于可以松口气了!
  19. windows文件夹图标只显示一半,另一半被白框遮挡解决办法
  20. android 阻尼动画,Android阻尼效果 | 自定义进阶之实现MIUI的拖动视差效果

热门文章

  1. 关于2017GDC上OverWatch团队演讲ECSArchitecture的思考
  2. iOS平台游戏安全再议之八门神器内存修改,IAP Free游戏内购破解的防御
  3. Spoke to Spoke Flex
  4. 电脑端微信内置浏览器开启调试模式
  5. 机器人 瓷砖墙面清洗_瓷砖的清洁方法
  6. 图扑软件亮相 2022 福州数博会,携手共创数字新时代
  7. ScyllaDB 介绍
  8. 2023发卡商城源码,最新自助下单彩虹云商城系统免授权无后门源码
  9. TURNING THE CURSE OF HETEROGENEITY IN FEDERATED LEARNING INTO A BLESSING FOR OUT-OFDISTRIBUTION DETE
  10. EXCEL中如何根据某列找到重复的行,并将重复只保留一条其余的删除?