AT2067 a lot of formulas / Many Formulas

AT2067 a lot of formulas / Many Formulas

Topic Portal

The meaning of problems

A string S (1≤|S|≤10) consisting only of characters 1-9, lets you add + intermediate, was added so that it becomes a formula. Summing the values of all programs (see examples explained).
Input 125, output 176.

There are four:

125
1+25=26
12+5=17
1+2+5=8

answer

(Rejected no brain storm search)
to see the title directly thought is
for each piece of digital strings are then summed to calculate the contribution of
the contribution of each section of the digital value is the number of programs * Arranging a plus sign (this number has been on both sides of the plus sign arranged a)
for a length intermediate sequence tot can be inserted plus the space for (tot-1) th
then for a period of length len number which calculated the number of plus scheme as follows:
case 1: the segment figures do not occur at the boundaries (or did not take the first digit of the last digit)
1. the digital section such that it could not be filled inside the plus sign bit reduction hole (len-1) th;
2 on both sides of the segments of the digital plus could not be filled, reducing the hole position 2;
3. reducing the total bit pit (len + 1) th.
The remaining bits of the pit (tot-1-len-1 ) = (tot-len-2) th
Case 2: The segment number is on the border
1. Internal plus could not be filled, reducing the bit pit (len-1) th ;
2. the segment numbers can not fill the plus side (the other side of the border, are not supposed to have to pit-bit), to reduce a bit the pit;
3. reducing the total pit-bit (len) months.
The remaining bits of the pit (tot-1-len) = (tot-len-1) th.
So you know the number of pit-bit program can calculate the number of the bar
can also fill in for each pit can not fill, so the program number is 2 ^ (hole number).
Finally accumulate on the line.
Another key point is how to enumerate every piece of digital, where my approach is to enumerate length, and then enumerate the starting point.
(Time seems faster than other codes so few milliseconds, win purely on the algorithm?)

Code

#include <cstdio>
#define ll long long
using namespace std;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') {x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return x*f;
}
int a[20],q[20];
int tot;
ll ans;

signed main(){
    ll b=read();
    while (b){
        q[++tot]=b%10;
        b/=10;
    }
    for (int i(1);i<=tot;++i) a[i]=q[tot-i+1];//可能这一步还有更优处理。
    for (int l(1);l<=tot;++l)
        for (int i(1);i+l-1<=tot;++i){
            ll num=0;
            for (int j(i);j<=i+l-1;++j) num*=10,num+=a[j];
            //printf("%lld ",num);
            if (i==1||i+l-1==tot){//靠在边界上
                for (int i(1);i<=tot-l-1;++i) num*=2;
                ans+=num;
            }
            else {//没靠在边界上
                for (int i(1);i<=tot-l-2;++i) num*=2;
                ans+=num;
            }
        }
    printf("%lld",ans);
    return 0; 
}

Guess you like

Origin www.cnblogs.com/cancers/p/11605067.html