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
题意:
给你一个等号,和若干个加号,让你分割上面的数字使等式成立。
分析:枚举等号位置,二进制枚举加号位置。二进制可状态压缩。
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; }
相关文章推荐
- HDU 4403 A very hard Aoshu problem (暴力+状态压缩)
- HDU 4403 A very hard Aoshu problem (DFS暴力)
- HDU 4403 A very hard Aoshu problem(DFS+暴力)
- hdu 4403 A very hard Aoshu problem 状态压缩
- hdu_4403 A very hard Aoshu problem 暴力搜索
- HDU 4403 A very hard Aoshu problem (DFS暴力)
- HDOJ 题目A very hard Aoshu problem(暴力,DFS)
- HDU 4403 A very hard Aoshu problem (状态压缩+枚举)
- HDU3699(POJ 3989)A hard Aoshu Problem(暴力模拟dfs)
- HDU 3699(J) ——A hard Aoshu Problem(DFS暴力搜索)
- [ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)
- hdu 4403 A very hard Aoshu problem【dfs枚举】
- HDU 4403 A very hard Aoshu problem(dfs爆搜)
- HDU 3699 A hard Aoshu Problem (暴力搜索)
- D - A very hard Aoshu problem----(2015 summer training #12(Team_Qualifying))
- HDU 4403 A very hard Aoshu problem(暴搜)
- Hdu 4403 A very hard Aoshu problem - 爆搜
- HDU 3699 A hard Aoshu Problem (暴力搜索)
- HDOJ3699 A hard Aoshu Problem[暴力]
- HDU - 4403 A very hard Aoshu problem ——dfs 数值符号插入