Count Subrectangles

给定两个由0和1组成的数组a和b,构建矩阵c,其中c的每个元素是a和b对应位置的乘积。计算矩阵c中大小为k的全一子矩形的数量。输入包含矩阵的行数、列数和目标面积,输出符合条件的子矩形个数。题目提供样例解释了计算方法,可以通过分解k并检查a和b中的连续1段来找到答案。
摘要由CSDN通过智能技术生成

Count Subrectangles

You are given an array a of length n n n and array b b b of length m m m both consisting of only integers 0 and 1. Consider a matrix c c c of size n × m n×m n×m formed by following rule: c i , j = a i ⋅ b j ci,j=ai⋅bj ci,j=aibj ( i . e . a i (i.e. ai (i.e.ai multiplied by b j bj bj). It’s easy to see that c c c consists of only zeroes and ones too.

How many subrectangles of size (area) k k k consisting only of ones are there in c c c?

A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e.consider four integers x 1 , x 2 , y 1 , y 2 ( 1 ≤ x 1 ≤ x 2 ≤ n , 1 ≤ y 1 ≤ y 2 ≤ m ) x1,x2,y1,y2 (1≤x1≤x2≤n, 1≤y1≤y2≤m) x1,x2,y1,y2(1x1x2n,1y1y2m) a subrectangle c [ x 1 … x 2 ] [ y 1 … y 2 ] c[x1…x2][y1…y2] c[x1x2][y1y2] is an intersection of the rows x 1 , x 1 + 1 , x 1 + 2 , … , x 2 x1,x1+1,x1+2,…,x2 x1,x1+1,x1+2,,x2 and the columns y 1 , y 1 + 1 , y 1 + 2 , … , y 2 y1,y1+1,y1+2,…,y2 y1,y1+1,y1+2,,y2.

The size (area) of a subrectangle is the total number of cells in it.

Input

The first line contains three integers n , m n, m n,m and k ( 1 ≤ n , m ≤ 40000 , 1 ≤ k ≤ n ⋅ m ) k (1≤n,m≤40000,1≤k≤n⋅m) k(1n,m40000,1knm), length of array a a a, length of array b b b and required size of subrectangles.

The second line contains n n n integers a 1 , a 2 , … , a n ( 0 ≤ a i ≤ 1 ) a1,a2,…,an (0≤ai≤1) a1,a2,,an(0ai1), elements of a a a.

The third line contains m m m integers b 1 , b 2 , … , b m ( 0 ≤ b i ≤ 1 ) b1,b2,…,bm (0≤bi≤1) b1,b2,,bm(0bi1), elements of b b b.

Output

Output single integer — the number of subrectangles of c c c with size (area) k k k consisting only of ones.

Examples
input
3 3 2
1 0 1
1 1 1
output
4
input
3 5 4
1 1 1
1 1 1 1 1
output
14

Note

In first example matrix c c c is:
                                                   在这里插入图片描述
There are 4 subrectangles of size 2 consisting of only ones in it:
           在这里插入图片描述
In second example matrix c is:
                                            在这里插入图片描述

思路

题意:给出长度为n和m且大小只能为0或1的数组a和b,其中矩阵c为a和b的乘积,求矩阵c中有多少满足面积为k的由1组成的数量。
分析:首先数据范围为4e4,弄成矩阵来做不太现实= =,所以我们要分析最后矩阵c中那些为可构成的面积——a决定了每一列的数量,b决定每一行的数量,只有当a和b都为1的时矩阵c才为1。所以我们可以把k分解为k1*k2,只要满足数组a中1连续长度为k1和数组b中1连续长度为k2的情况,那么就有一个满足面积为k的情况~就分解k判断a或b中有多少满足连续长度即可了( P s : Ps: Ps:见代码好理解)

代码如下
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=4e4+10;
int la[mx],lb[mx];//用来统计a和b数组中1的连续长度
int n,m,k,s;
ll ans;
 
int main()
{
	memset(la,0,sizeof(la));
	memset(lb,0,sizeof(lb));
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&s);
		if(s==1)
		la[i]=la[i-1]+1;//统计连续长度
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&s);
		if(s==1)
		lb[i]=lb[i-1]+1;
	}
	ans=0;
	for(int i=1;i<=n;i++)因为a数组长度为n,所以最大可能为n
	{
		if(k%i==0)//分解因数
		{
			int j=k/i;//j为b数组需要满足的长度
		    ll t1=0,t2=0;//分别统计满足的数量
		    for(int k=1;k<=n;k++)
		    {
		    	if(la[k]>=i)//大于等于就说明有一个满足
		    	t1++;
		    }
		    for(int k=1;k<=m;k++)
		    {
		    	if(lb[k]>=j)
		    	t2++;
		    }
		    ans+=t1*t2;//最后加上乘积即可
		}
	}
	printf("%lld\n",ans);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值