CodeForces - 1323B Count Subrectangles

一、內容

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 cconsists of only zeroes and ones too.How many subrectangles of size (area) kconsisting 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 band required size of subrectangles.The second line contains nintegers a1,a2,…,an (0≤ai≤1), elements of aThe third line contains mintegers b1,b2,…,bm (0≤bi≤1), elements of b

.

Output

Output single integer — the number of subrectangles of cwith 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

二、思路

  • ca[i] 代表 a[]數組中連續1的個數爲i的片段有多少個。 cb[i]代表b[]數組。
  • 那麼一個矩形 代表x * y , 我們要求面積爲k, 那麼必然 x * y = k 。
  • 我們只需要在ca裏面尋找 x ,在cb裏面尋找y即可。 比如 k = 4, 那麼ca[1] * cb[4] 就是面積爲4的矩形。

三、代碼

#include <cstdio>
#include <cstring>
using namespace std;
const int N = 4e4 + 5;
typedef long long ll;
int n, m, k;
int a[N], b[N];
ll ca[N], cb[N];
void get(int a[], ll ca[], int n) { //獲取連續的1的個數  
	int i = 1, t = 0;
	while (i <= n) { 
		//統計連續1的長度
		if (a[i] == 0){ i++; continue;}
		int j = 0;
		while (i <= n && a[i] == 1) j++, i++;
		//將連續1的長度累加到ca中
		for (int k = 1; k <= j; k++) ca[k] += j - k + 1;
	}	
} 
int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 
	for (int i = 1; i <= m; i++) scanf("%d", &b[i]); 
 	get(a, ca, n); 
 	get(b, cb, m); 
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		if (k % i || (k / i > m)) continue;
		if (ca[i]) ans += cb[k / i] * ca[i];
	}
	printf("%lld", ans);
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章