Description
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 containingA, B andC 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 grewA tons of corn, B on another one andC on the remaining one.
Input
The first line contains space-separated integers n andm — 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 integersA, 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 containedA tons of corn, another one contained B tons, and the remaining one contained C tons. If no such way exists, print 0.
Sample Input
3 3 1 1 1 1 1 1 1 1 1 3 3 3
2
2 5 1 1 1 1 1 2 2 2 2 2 3 6 6
3
3 3 1 2 3 3 1 2 2 3 1 5 6 7
#include<stdio.h> int a[1000][1000]; int c[1000]= {0}; int d[1000]= {0}; int s[4]; int main() { freopen("input.txt","r",stdin);//有的题目要求有这两行 freopen("output.txt","w",stdout); int n,m,i,j,k; int t,y,u; while(scanf("%d%d",&n,&m)!=-1) { int o=0; for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { scanf("%d",&a[i][j]); } } scanf("%d%d%d",&t,&y,&u); int p=1; for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { c[p]+=a[i][j]; } p++; } int w=1; for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { d[w]+=a[j][i]; } w++; } int sum=0,x; for(i=1; i<=n-2; i++)//从行开始:固定一部分,遍历第二部分的所有可能,第三个是剩下的所有和
{ sum+=c[i]; int f=0; for(j=i+1; j<=n-1; j++) { f+=c[j]; int l=0; for(k=j+1; k<=n; k++) { l+=c[k]; } s[1]=t;//防止从头开始的时候t,y,u都变成-1; s[2]=y; s[3]=u; int z=0; for(x=1; x<=3; x++) { if(sum==s[x]&&s[x]!=-1) { z++; s[x]=-1; } if(f==s[x]&&s[x]!=-1) { z++; s[x]=-1; } if(l==s[x]&&s[x]!=-1) { z++; s[x]=-1; } if(z==3)//满足三个要求了就可能的情况加一 { o++; } } } } int yu=0; for(i=1; i<=m-2; i++)//从列开始 { yu+=d[i]; int hu=0; for(j=i+1; j<=m-1; j++) { hu+=d[j]; int ben=0; for(k=j+1; k<=m; k++) { ben+=d[k]; } s[1]=t; s[2]=y; s[3]=u; int z=0; for(x=1; x<=3; x++) { if(yu==s[x]&&s[x]!=-1) { z++; s[x]=-1; } if(hu==s[x]&&s[x]!=-1) { z++; s[x]=-1; } if(ben==s[x]&&s[x]!=-1) { z++; s[x]=-1; } if(z==3) { o++; } } // printf("%d\n",o); } } printf("%d\n",o); } }