AtCoder Beginner Contest 045 C - たくさんの数式 / Many Formulas

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 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

Copy
125

Sample Output 1

Copy
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

Copy
9999999999

Sample Output 2

Copy
12656242944

题解:就是给一个数字字符串,然后在字符串中添加“ + ”把所有情况的  和 加和的结果;

   二进制枚举 "+" 位置,字符串最多10位,总共也就2^9 种情况,然后简单操作一下求加上“+” 后的和即可
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
char s[];
int pos[];
ll num(int l,int r)
{
ll ans=;
for(int i=l;i<=r;i++){
ans=ans*+s[i]-'';
}
return ans;
}
ll solve(int n)
{
int l=-,r=;
ll ans=;
pos[n]=;
for(int i=;i<=n;i++){
if(pos[i]==) r++;
else{
ans+=num(l+,i);
l=i;
}
}
return ans;
}
int main()
{
while(cin>>s){
int n=strlen(s);
ll ans=;
int R= (<<(n-))- ;
for(int i=;i<=R;i++){
mem(pos);
int tot=,temp=i;
while(temp){
pos[tot++]=temp%;
temp=temp/;
}
ans+=solve(n-);
}
printf("%lld\n",ans);
}
return ;
}
上一篇:跟随我在oracle学习php(8)


下一篇:CodeForce 424C Magic Formulas