たくさんの数式 / Many Formulas(AtCoder-2067)

Problem Description

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.

Input

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

S

Output

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

Example

Sample Input 1

125

Sample Output 1

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.

Sample Input 2

9999999999

Sample Output 2

12656242944

题意:给一个包含数字 1 到 9 的数字串,现在可以在这个数字串中任意两字母间插入加号获得一个式子,问所有可能的式子的和

思路:

dfs 即可,对于每一位,分别去深搜是否添加加号,然后统计和,由于最后一位不能加,因此需要注意边界条件,同时,在每次深搜完成后要加上最后一位

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 10+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
char str[N];
int bit[N],len;
bool vis[N];
LL res;
void dfs(int x){
    if(x==len){
        LL sum1=0,sum2=0;
        for(int i=1;i<=len-1;i++){
            sum1=sum1*10+bit[i];
            if(vis[i]){
                sum2+=sum1;
                sum1=0;
            }
        }
        sum1=sum1*10+bit[len];
        res+=(sum1+sum2);
        return;
    }

    vis[x]=true;
    dfs(x+1);

    vis[x]=false;
    dfs(x+1);
}
int main(){
    scanf("%s",str+1);
    len=strlen(str+1);
    for(int i=1;i<=len;i++)
        bit[i]=str[i]-'0';
    dfs(1);
    printf("%lld\n",res);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值