CF 120 D.Three Sons【细节题】

来源:互联网 发布:安卓版数据恢复软件 编辑:程序博客网 时间:2024/05/16 06:49

D. Three Sons
time limit per test 1 second
memory limit per test 256 megabytes
input input.txt
output output.txt

Three sons inherited from their father a rectangular corn fiend divided into n × m squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing AB and C tons of corn.

The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square.

Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew A tons of corn, B on another one and C on the remaining one.

Input

The first line contains space-separated integers n and m — the sizes of the original (1 ≤ n, m ≤ 50, max(n, m) ≥ 3). Then the field's description follows: n lines, each containing m space-separated integers cij, (0 ≤ cij ≤ 100) — the number of tons of corn each square contains. The last line contains space-separated integers A, B, C (0 ≤ A, B, C ≤ 106).

Output

Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained A tons of corn, another one contained B tons, and the remaining one contained C tons. If no such way exists, print 0.

Sample Input

Input
3 31 1 11 1 11 1 13 3 3
Output
2
Input
2 51 1 1 1 12 2 2 2 23 6 6
Output
3
Input
3 31 2 33 1 22 3 15 6 7
Output
0

Hint

The lines dividing the field can be horizontal or vertical, but they should be parallel to each other.




这道题写到结束都没写出来,因为自己思维不是很清楚 ,没有考虑到0的情况,就是考虑取值范围,而且安排A题的顺序也不对,其实刚开场,别人没A 不等于难,就是考虑取值范围,这点一定要注意,还有就是思路要理清之后再写,不要边敲边写,这样在组队赛中不适用

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int mi[60][60];int x[60],y[60];int main(){   freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    int n,m,sum,q,w,e,bj,i,j,sb,r,a1,a2,a3,k;    while(~scanf("%d%d",&n,&m))    {        memset(mi,0,sizeof(mi));        memset(x,0,sizeof(x));        memset(y,0,sizeof(y));        sb=0;        sum=0;        for(i=0; i<n; i++)        {            x[i]=0;            for(j=0; j<m; j++)            {                scanf("%d",&mi[i][j]);                x[i]+=mi[i][j];                sb+=mi[i][j];            }        }        scanf("%d%d%d",&q,&w,&e);        if(sb!=(q+w+e))        {            printf("0\n");            continue;        }        for(j=0; j<m; j++)        {            y[j]=0;            for(i=0; i<n; i++)                y[j]+=mi[i][j];        }        if(n<3)            sum+=0;        else if(n>=3)        {                for(i=1; i<=n-2; i++)                {                    for(j=i+1; j<=n-1; j++)                    { a1=0,a2=0,a3=0;                        for(k=0; k<i; k++)                            a1+=x[k];                        for(k=i; k<j; k++)                            a2+=x[k];                        a3=sb-a1-a2;                      if((a1==q&&a2==w&&a3==e)||(a1==q&&a2==e&&a3==w)||(a1==w&&a2==q&&a3==e)||(a1==w&&a2==e&&a3==q)||(a1==e&&a2==q&&a3==w)||(a1==e&&a2==w&&a3==q))                        {                            sum++;                        }                    }                }        }        if(m<3)            sum+=0;        else if(m>=3)        {                for(i=1; i<=m-2; i++)                {                    for(j=i+1; j<=m-1; j++)                    { a1=0,a2=0,a3=0;                        for(k=0; k<i; k++)                            a1+=y[k];                        for(k=i; k<j; k++)                            a2+=y[k];                        a3=sb-a1-a2;                      if((a1==q&&a2==w&&a3==e)||(a1==q&&a2==e&&a3==w)||(a1==w&&a2==q&&a3==e)||(a1==w&&a2==e&&a3==q)||(a1==e&&a2==q&&a3==w)||(a1==e&&a2==w&&a3==q))                        {                            sum++;                        }                    }                }        }        printf("%d\n",sum);    }    return 0;}


0 0