C - たくさんの数式 / Many Formulas
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 300300 points
Problem Statement
You are given a string SS 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|≤101≤|S|≤10
- All letters in SS are digits between
1
and9
, inclusive.
Input
The input is given from Standard Input in the following format:
SS
Output
Print the sum of the evaluated value over all possible formulas.
Sample Input 1 Copy
Copy
125
Sample Output 1 Copy
Copy
176
There are 44 formulas that can be obtained: 125
, 1+25
, 12+5
and 1+2+5
. When each formula is evaluated,
- 125125
- 1+25=261+25=26
- 12+5=1712+5=17
- 1+2+5=81+2+5=8
Thus, the sum is 125+26+17+8=176125+26+17+8=176.
Sample Input 2 Copy
Copy
9999999999
Sample Output 2 Copy
Copy
12656242944
#include<bits/stdc++.h>
#include <iostream>
#include <string>
#define ll long long
using namespace std;
ll SSS(string str)
{
int len=str.size();
ll sum=0;
for(int i=0;i<len;i++)
{
sum=sum*10+(str[i]-'0');
}
return sum;
}
ll int search(string s1,ll presum){
int i;
ll k,sum;
sum = 0;
for (i=1;i<=s1.size();i++){
string s2 = s1.substr(0,i);//[0,i)
string s3 = s1.substr(i);
if (s3==""){
s3 = "AC";//---//---//
}
if(i == s1.size()){
sum += SSS(s1)+presum;//---
}else{
k = SSS(s2);//---
if (s3=="AC"){
sum += k + presum;//--
}else{
sum += search(s3,k + presum);
}
}
}
return sum;//
}
int main(){
ll sum;
string s;
cin >> s;
sum = search(s,0);//dfs
cout << sum;
return 0;
}