HDU4403(暴搜)

A very hard Aoshu problem

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students:

Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.

Input

There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END". 

Output

For each test case , output a integer in a line, indicating the number of equations you can get. 

Sample Input

1212
12345666
1235
END

Sample Output

2
2
0
 //2016.8.20
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 20
using namespace std; int ans, num[N][N], n;//num[i][j]用来记录从i到j位,数字的大小。
char s[N]; void dfsR(int pos, int leftsum, int rightsum)
{
if(pos == n && leftsum == rightsum){
ans++; return;
}
if(leftsum < rightsum)return;//剪枝
for(int i = pos; i < n; i++)
dfsR(i+, leftsum, rightsum+num[pos][i]);
} void dfsL(int pos, int sum, int mid)//pos表示当前处理的位置,sum表示已处理的和,mid表示等号的位置。
{
if(pos == mid)//对左边dfs完后,对右边dfs
dfsR(mid, sum, );
for(int i = pos; i < mid; i++)//枚举加号位置。
dfsL(i+, sum+num[pos][i], mid);
}
int main()
{
while(scanf("%s", s)!=EOF)
{
if(s[] == 'E')break;
ans = ;
n = strlen(s);
for(int i = ; i < n; i++)
{
int tmp = ;
for(int j = i; j < n; j++)
{
tmp += s[j]-'';
num[i][j] = tmp;
tmp *= ;
}
}
for(int i = ; i < n; i++)//枚举等号的位置,然后暴搜
dfsL(, , i);
cout<<ans<<endl;
} return ;
}
上一篇:Myeclipse 搭建Java Web 项目 《一》


下一篇:解决 Android Device Monitor 常见问题