Count Subrectangles CodeForces - 1323B(思维)

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

How many subrectangles of size (area) k consisting only of ones are there in 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 x1,x2,y1,y2 (1≤x1≤x2≤n, 1≤y1≤y2≤m) a subrectangle c[x1…x2][y1…y2] is an intersection of the rows x1,x1+1,x1+2,…,x2 and the columns 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 and k (1≤n,m≤40000,1≤k≤n⋅m), length of array a, length of array b and required size of subrectangles.

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

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

Output
Output single integer — the number of subrectangles of c with size (area) 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 is:
在这里插入图片描述

There are 4 subrectangles of size 2 consisting of only ones in it:
在这里插入图片描述

In second example matrix c is:
在这里插入图片描述
思路:思路感觉挺好想的,但是有可能容易超时。对于连续的1个数相同的情况,我们可以合并考虑,因为他们的情况都是一样的。因此,我们可以分别计算出a序列,连续的1的个数有多少种情况,并记录每一种情况的个数。同理求出b序列的。然后我们分别遍历a,b序列的这些情况,求出符合条件的就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=4e4+100;
map<int,int> p1,p2;
int a[maxx];
int b[maxx];
int n,m,k;

inline void dfs(int c[],int &j,int limit)
{
	if(j>limit) return ;
	if(c[j]==0) return ;
	dfs(c,j+=1,limit);
}
inline void fcs()
{
	int j;
	for(int i=1;i<=n;)
	{
		if(a[i]==1)
		{
			j=i;
			dfs(a,j,n);
			p1[j-i]++;
			i=j;
		}
		else i++;
	}
	for(int i=1;i<=m;)
	{
		if(b[i]==1)
		{
			j=i;
			dfs(b,j,m);
			p2[j-i]++;
			i=j;
		}
		else i++;
	}
}
inline void solve()
{
	int len1,len2,num1,num2,_min,_max;
	ll sum=0;
	for(map<int,int> ::iterator it1=p1.begin();it1!=p1.end();it1++)
	{
		len1=it1->first;num1=it1->second;
		for(map<int,int> ::iterator it2=p2.begin();it2!=p2.end();it2++)
		{
			len2=it2->first;num2=it2->second;
			_min=min(len1,len2);
			_max=max(len1,len2);
			for(int i=1;i<=min(_min,k);i++)
			{
				if(k%i==0&&k/i<=_max) sum+=((_max-k/i+1)*(_min-i+1))*num1*num2;
			}
		}
	}
	cout<<sum<<endl;
}
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++) cin>>b[i];
	p1.clear();p2.clear();
	fcs();
	solve();
	return 0;
}

努力加油a啊,(o)/~

发布了502 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104747264