来自ZZY的博客:http://blog.csdn.net/kk303/article/details/8008058
D - A very hard Aoshu problem
数据范围很小呃... 暴力搜索即可...枚举等号的位置...再DFS得出左边的值..再DFS找右边有多少种方案可以等于左边的加法值...
Program:
- #include<iostream>
- #include<stdio.h>
- #include<algorithm>
- #include<string.h>
- #include<math.h>
- #include<map>
- #include<queue>
- #include<stack>
- #define ll long long
- #define oo 1000000000
- #define pi acos(-1)
- using namespace std;
- ll ans,T[20][20],len,mid;
- char s[20];
- void dfs2(int i,ll data,ll pre)
- {
- int k;
- if (i>len)
- {
- if (data==pre) ans++;
- return ;
- }
- for (k=i;k<=len;k++)
- dfs2(k+1,data+T[i][k],pre);
- return;
- }
- void dfs1(int i,ll data)
- {
- int k;
- if (i>mid) dfs2(mid+1,0,data);
- for (k=i;k<=mid;k++)
- dfs1(k+1,data+T[i][k]);
- return;
- }
- int main()
- {
- int i,j,k;
- while (gets(s+1))
- {
- if (s[1]=='E')break;
- len=strlen(s+1);
- for (i=1;i<=len;i++)
- for (j=i;j<=len;j++)
- {
- T[i][j]=0;
- for (k=i;k<=j;k++) T[i][j]=T[i][j]*10+s[k]-'0';
- }
- ans=0;
- for (mid=1;mid<len;mid++)
- dfs1(1,0);
- printf("%I64d\n",ans);
- }
- return 0;
- }
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define ll long long
#define oo 1000000000
#define pi acos(-1)
using namespace std;
ll ans,T[20][20],len,mid;
char s[20];
void dfs2(int i,ll data,ll pre)
{
int k;
if (i>len)
{
if (data==pre) ans++;
return ;
}
for (k=i;k<=len;k++)
dfs2(k+1,data+T[i][k],pre);
return;
}
void dfs1(int i,ll data)
{
int k;
if (i>mid) dfs2(mid+1,0,data);
for (k=i;k<=mid;k++)
dfs1(k+1,data+T[i][k]);
return;
}
int main()
{
int i,j,k;
while (gets(s+1))
{
if (s[1]=='E') break;
len=strlen(s+1);
for (i=1;i<=len;i++)
for (j=i;j<=len;j++)
{
T[i][j]=0;
for (k=i;k<=j;k++) T[i][j]=T[i][j]*10+s[k]-'0';
}
ans=0;
for (mid=1;mid<len;mid++)
dfs1(1,0);
printf("%I64d\n",ans);
}
return 0;
}