A very hard Aoshu problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1409 Accepted Submission(s): 958
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<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<climits> #include<string> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i--) #define MS(x,y)memset(x,y,sizeof(x)) typedef long long LL; const int INF=0x7ffffff; const int M=25; char s[M]; LL num[M][M]; int i,j,k,n,m; int ans ,len,l; LL get(int x,int y) { LL sum=0; for(int i=x;i<=y;i++) sum=sum*10+s[i]-'0'; return sum; } void f(int i,int sum1,int sum2) { if(i>=len){ if(sum1==sum2)ans++; return; } for(int k=i;k<len;k++) f(k+1,sum1,sum2+num[i][k]); } void dfs(int i,int sum1) { if(i>=l) f(i,sum1,0); for(int k=i;k<l;k++) dfs(k+1,sum1+num[i][k]); } int main() { while(~scanf("%s",s)) { if(s[0]=='E')break; len=strlen(s); rep(i,0,len) rep(j,i,len) num[i][j]=get(i,j); int r;ans=0; rep(r,1,len){ l=r; dfs(0,0); } printf("%d\n",ans); } return 0; }