您的位置:首页 > 其它

B - A very hard Aoshu problem 【状态压缩+暴力】

2017-11-10 22:49 274 查看
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

题意:

给你一个等号,和若干个加号,让你分割上面的数字使等式成立。

分析:枚举等号位置,二进制枚举加号位置。二进制可状态压缩。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 20;
#define ll long long int
char str[20];
int a[20];
int v[20];
int ans;
int len;
int solve(int s1, int s2, int mid)
{
int L, R;
L = 0;
R = 0;
int temp = a[1];
int p = 1;
for (int i = 1; i < mid; i++)
{
if (s1&(1 << (i - 1)))
{
L += temp;
temp = a[i + 1];
}
else
{
temp *= 10;
temp += a[i + 1];
}
}
L += temp;
temp = a[mid + 1];
for (int i = mid + 1; i < len; i++)
{

int qq = i - mid - 1;
if (s2&(1 << qq))
{
R += temp;
temp = a[i + 1];
}
else
{
temp *= 10;
temp += a[i + 1];
}
}
R += temp;
if (L == R)
return true;
else
return false;
}
int main()
{
while (~scanf("%s", str))
{
ans = 0;
memset(a, 0, sizeof(a));
if (str[0] == 'E')
break;
len = strlen(str);
for (int i = 0; i < len; i++)
{
a[i+1] = str[i] - '0';
}
for (int i = 1; i < len; i++)
{
int t1 = (1 << (i - 1));
int t2 = (1 << (len - i - 1));
for (int s1 = 0; s1 < t1; s1++)
{
for (int s2 = 0; s2 < t2; s2++)
{
if (solve(s1, s2, i))
ans++;
}
}
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: