A very hard Aoshu problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
Problem 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.
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 <algorithm> #include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <utility> #include <set> #include <bitset> using namespace std; char s[100]; template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key> > class multiset; map <__int64, int > s1; map <__int64, int > s2; __int64 mypow(int x, int y) { __int64 t; if (y == 0) return 1; else if (y == 1) return x; else if (y == 2) return x * x; else if (y % 2) { t = mypow(x, (y - 1) / 2); return t * t * x; } else { t = mypow(x ,y / 2); return t * t; } } void f(map <__int64, int> &ss, int l, int h) { int i, j, k, p, t[100]; __int64 num, n; n = 0; p = 0; for (i = (1 << (h - l)) - 1; i >= 0; i--) { num = 0; for (j = 0; j < (h - l); j++) { if (i & (1 << j)) { n = 0; t[p++] = (int) (s[h - j] - 48); for (k = 0; k < p; k++) { n += (t[k] * mypow(10, k)); } num += n; n = 0; p = 0; } else { t[p] = (int)(s[h-j] - 48); p++; } } t[p++] = (int)(s[l] - 48); for (k = 0; k < p; k++) { n += (t[k] * mypow(10, k)); } num+= n; // ss.insert(num); ss[num]++; p = 0; n = 0; num = 0; } } int main() { int ans, len, j; map <__int64, int> :: iterator it; map <__int64, int> :: iterator it2; scanf("%s", s); while ( strcmp(s, "END") != 0) { ans = 0; len = strlen(s); for (j = 1; j <= len - 1; j++) { s1.clear(); s2.clear(); f(s1, 0, j - 1); f(s2, j, len - 1); for (it = s1.begin(); it != s1.end(); it++) { for (it2 = s2.begin(); it2 != s2.end(); it2++) { if (it ->first == it2 -> first) { ans += it -> second * it2 -> second; break; } } } } printf("%d\n", ans); scanf("%s", s); } //system("pause"); return 0; }
子集划分!
有不懂就留言吧!
本文探讨了Aoshu数学难题的解决方法,通过子集划分技巧求解给定数字序列中可能形成的等式数量。适用于初级数学爱好者。
1805

被折叠的 条评论
为什么被折叠?



