• たくさんの数式 / Many Formulas AtCoder


    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: 1251+2512+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
    ",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), '', 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 == '
    ');
        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';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    Pycharm破解
    Web UI绕过登录的实现
    使用Docker安装Jenkins服务
    Appium 基于控件左滑操作
    Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.
    Selenium文件上传
    获取Android手机日志
    Linux机器间ssh免密登录
    JMeter中使用Put请求方式请求接口
    python发送post请求上传文件,无法解析上传的文件
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10713001.html
Copyright © 2020-2023  润新知