Time limit : 2sec / Memory limit : 256MB
Score : 300 points
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
and9
, 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
Copy
125
Sample Output 1
Copy
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
Copy
9999999999
Sample Output 2
Copy
12656242944
题解:就是给一个数字字符串,然后在字符串中添加“ + ”把所有情况的 和 加和的结果;
二进制枚举 "+" 位置,字符串最多10位,总共也就2^9 种情况,然后简单操作一下求加上“+” 后的和即可
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime> 10 #include <map> 11 #include <set> 12 #include <queue> 13 using namespace std; 14 #define lowbit(x) (x&(-x)) 15 #define max(x,y) (x>y?x:y) 16 #define min(x,y) (x<y?x:y) 17 #define MAX 100000000000000000 18 #define MOD 1000000007 19 #define pi acos(-1.0) 20 #define ei exp(1) 21 #define PI 3.141592653589793238462 22 #define INF 0x3f3f3f3f3f 23 #define mem(a) (memset(a,0,sizeof(a))) 24 typedef long long ll; 25 ll gcd(ll a,ll b){ 26 return b?gcd(b,a%b):a; 27 } 28 bool cmp(int x,int y) 29 { 30 return x>y; 31 } 32 const int N=55; 33 const int mod=1e9+7; 34 char s[15]; 35 int pos[15]; 36 ll num(int l,int r) 37 { 38 ll ans=0; 39 for(int i=l;i<=r;i++){ 40 ans=ans*10+s[i]-'0'; 41 } 42 return ans; 43 } 44 ll solve(int n) 45 { 46 int l=-1,r=0; 47 ll ans=0; 48 pos[n]=1; 49 for(int i=0;i<=n;i++){ 50 if(pos[i]==0) r++; 51 else{ 52 ans+=num(l+1,i); 53 l=i; 54 } 55 } 56 return ans; 57 } 58 int main() 59 { 60 while(cin>>s){ 61 int n=strlen(s); 62 ll ans=0; 63 int R= (1<<(n-1))-1 ; 64 for(int i=0;i<=R;i++){ 65 mem(pos); 66 int tot=0,temp=i; 67 while(temp){ 68 pos[tot++]=temp%2; 69 temp=temp/2; 70 } 71 ans+=solve(n-1); 72 } 73 printf("%lld ",ans); 74 } 75 return 0; 76 }