1. 程式人生 > >たくさんの數式 / Many Formulas AtCoder - 2067 (枚舉二進制)

たくさんの數式 / Many Formulas AtCoder - 2067 (枚舉二進制)

turn n) cst format 思路 eof inf max 時間

Problem Statement

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.

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


思路:
每兩個數之間的是否加“+“只有兩種狀態,即加和不加,那麽字符串的最大長度是10,我們來用二進制思想來枚舉所有狀態,時間復雜度也就是2^n
顯然不會TLE,用字符串substr函數來分割字符串,stringstream來轉成int數字,寫起來會很方便。
細節見代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll getx(string str)
{
    if(str=="")
    {
        return 0ll;
    }
    stringstream ss;
    ss.clear();
    ss<<str;
    ll res;
    ss>>res;
    return res;
}
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    string s;
    cin>>s;
    int len=s.length();
    ll ans=0ll;
    std::vector<string> v;
    for(int i=1;i<=(1<<(len-1));i++)
    {
        v.clear();
        int num=1;
        for(int j=0;j<=(len-1);j++)
        {
            if((i&(1<<j)))
            {
                string temp=s.substr(len-1-j,num);
                v.push_back(temp);
                num=1;
            }else
            {
                num++;
            }
        }
        string temp=s.substr(0,num-1);
                v.push_back(temp);
        // ll tt=0ll;
        for(auto x:v)
        {
//            db(x);
            ans+=getx(x);
        }

    }
    cout<<ans<<endl;

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch ==   || ch == \n);
    if (ch == -) {
        *p = -(getchar() - 0);
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 - ch + 0;
        }
    }
    else {
        *p = ch - 0;
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 + ch - 0;
        }
    }
}

たくさんの數式 / Many Formulas AtCoder - 2067 (枚舉二進制)