codeforces 120D Three Sons

9 篇文章 0 订阅

题目链接:传送门

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 A, B 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.

Examples
Input

3 3
1 1 1
1 1 1
1 1 1
3 3 3

Output

2

Input

2 5
1 1 1 1 1
2 2 2 2 2
3 6 6

Output

3

Input

3 3
1 2 3
3 1 2
2 3 1
5 6 7

Output

0

Note

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

【题意】

有一个人有三个儿子,但是他对每个儿子的喜爱程度是不一样的。他的遗产是一块土地,土地被划分成n*m的方块型,每个方块的产量是不一样的。他对孩子的喜爱程度就是分给孩子土地的产量和。问你能否划两条平行线将这个土地分成三块,需要注意的是,你不能将一个方块分开,问你有多少种可行方案使得老人的遗愿能够得以实现。问你方案数是多少。

【分析】

因为给你的图形很小,所以我们直接暴力就行了,如果有一个这样的我们就加上对应的答案,然后输出就行了。

【代码】

#include<bits/stdc++.h>
using namespace std;
int num[100][100];
int sum_row[100];
int sum_col[100];
int n,m;
int cal(int a,int b,int c) {
    int ret = 0;
    for(int i = 1; i<n; i++)
        if(sum_row[i]==a)
            for(int j = i+1; j<n; j++)
                if(sum_row[j]-sum_row[i]==b)
                    ret++;
    for(int i = 1;i<m;i++)
        if(sum_col[i]==a)
            for(int j = i+1;j<m;j++)
                if(sum_col[j]-sum_col[i]==b)
                    ret++;
    return ret;
}
int main() {
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    memset(sum_row,0,sizeof(sum_row));
    memset(sum_col,0,sizeof(sum_col));
    scanf("%d%d",&n,&m);
    for(int i = 1; i<=n; i++)
        for(int j = 1; j<=m; j++)
            scanf("%d",&num[i][j]);
    for(int i = 1; i<=n; i++) {
        sum_row[i] = sum_row[i-1];
        for(int j = 1; j<=m; j++)
            sum_row[i]+=num[i][j];
    }
    for(int i = 1; i<=m; i++) {
        sum_col[i] = sum_col[i-1];
        for(int j = 1; j<=n; j++)
            sum_col[i]+=num[j][i];
    }
    int a[3];
    scanf("%d%d%d",a,a+1,a+2);
    if(sum_row[n]!=a[0]+a[1]+a[2]) puts("0");
    else {
        sort(a,a+3);
        int ans = 0;
        if(a[0]==a[1]) {
            if(a[1]==a[2]) {
                //aaa
                ans+=cal(a[0],a[1],a[2]);
            } else {
                //aab
                ans+=cal(a[0],a[1],a[2]);
                ans+=cal(a[0],a[2],a[1]);
                ans+=cal(a[2],a[0],a[1]);
            }
        } else {
            if(a[1]==a[2]) {
                //abb
                ans+=cal(a[0],a[1],a[2]);
                ans+=cal(a[2],a[0],a[1]);
                ans+=cal(a[2],a[1],a[0]);
            } else {
                //abc
                ans+=cal(a[0],a[1],a[2]);
                ans+=cal(a[0],a[2],a[1]);
                ans+=cal(a[1],a[0],a[2]);
                ans+=cal(a[1],a[2],a[0]);
                ans+=cal(a[2],a[0],a[1]);
                ans+=cal(a[2],a[1],a[0]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值