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=ai⋅bj ( 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(1≤x1≤x2≤n,1≤y1≤y2≤m) a subrectangle c [ x 1 … x 2 ] [ y 1 … y 2 ] c[x1…x2][y1…y2] c[x1…x2][y1…y2] 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(1≤n,m≤40000,1≤k≤n⋅m), 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(0≤ai≤1), 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(0≤bi≤1), 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;
}