equation

In Olympiad Of Mathematics, a problem which require participants to insert ‘+’ or ‘-‘ between two numbers to let the equation correct.
For instance:
1 2 3 4 5 6 7 8 9=108
You can insert ‘+’ or ‘-‘ or nothing between each two numbers. If you leave it empty, numbers between the gap will combine into one number(If I leave blank between 1 and 2 empty, the number is 12). You are required to find the number of total ways, which can let the equation correct.

Problem Description

In Olympiad Of Mathematics, a problem which require participants to insert ‘+’ or ‘-‘ between two numbers to let the equation correct.
For instance:
1 2 3 4 5 6 7 8 9=108
You can insert ‘+’ or ‘-‘ or nothing between each two numbers. If you leave it empty, numbers between the gap will combine into one number(If I leave blank between 1 and 2 empty, the number is 12). You are required to find the number of total ways, which can let the equation correct.

Input: N, represents the total sum
Output: the number of total methods to fill the blanks.

Sample Input:
108

Output:
15

Problem Analysis

Apparently, we can enumerate each status of each blanks(3 status and 8 blanks). The algorithm complexity will be 3^8, it’s small enough to pass the test.
This time , I used a wired way to implement the algorithm: just use loops. By combine 8 loops together, and enumerate 3 status (using 0 represents ‘+’, 1 represents ‘-‘ and 2 represents leaving empty).

AC Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;

#define rep(i) for(i=0;i<3;++i)

int a[10]={1,2,3,4,5,6,7,8,9};
const int Pow10[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

int main(){
    freopen("4.in","r",stdin);
    freopen("4.out","w",stdout);
    int sum;
    bool flag=false;
    cin>>sum;
    int ans=0;
    int op[10];
    memset(op,0,sizeof(op));
    int res;
    rep(op[2]){
        rep(op[3]){
            rep(op[4]){
                rep(op[5]){
                    rep(op[6]){
                        rep(op[7]){
                            rep(op[8]){
                                rep(op[9]){
                                    res=0;
                                    for(int kk=9;kk>=2;--kk){
                                        if(op[kk]==0){
                                            res+=kk;
                                        }else if(op[kk]==1){
                                            res-=kk;
                                        }else{
                                            int cur=0;
                                            int num=0;
                                            while(op[kk]==2){
                                                num+=kk*Pow10[cur++];
                                                --kk;
                                            }
                                            num+=kk*Pow10[cur];
                                            
                                            if(op[kk]==0){
                                                res+=num;
                                            }else if(op[kk]==1){
                                                res-=num;
                                            }

                                        }
                                    }
                                    if(op[2]!=2){
                                        res++;
                                    }
                                    //puts("");
                                    //cout<<"res:"<<res<<'n';
                                    if(res==sum){
                                        ans++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    fclose(stdin);
    fclose(stdout);
    return 0;
}