6467: Many Formulas(二进制暴力)

6467: Many Formulas

时间限制: 1 Sec   内存限制: 128 MB
提交: 145   解决: 80
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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.


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const long long INF=0x7FFFFFFFFFFFFFFFLL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
 
 
int main()
{
    char s[105];
    scanf("%s", s);
    int a[1005], len=strlen(s);
    long long x=1, ans=0;
    for(int i=0;i<pow(2, len-1);i++)
    {
        memset(a, 0, sizeof(a));
        long long temp=i, cnt=0;
        while(temp)
        {
            a[cnt++]=temp%2;
            temp/=2;
        }
        long long sum=0;
        temp=s[0]-'0';
        for(int j=0;j<len-1;j++)
        {
            if(a[j])
            {
                sum+=temp;
                temp=s[j+1]-'0';
            }
            else
                temp=temp*10+s[j+1]-'0';
        }
        ans+=sum+temp;
    }
    printf("%lld\n", ans);
    return 0;
}
 
/**************************************************************
    Problem: 6467
    User: ldu_reserver201701
    Language: C++
    Result: 正确
    Time:0 ms
    Memory:1892 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/qq_30796379/article/details/80498328