-
Notifications
You must be signed in to change notification settings - Fork 1
/
B - Count Subrectangles
80 lines (62 loc) · 1.23 KB
/
B - Count Subrectangles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n + 1];
int[] b = new int[m+1];
for (int i = 1; i < a.length; i++) {
a[i] = sc.nextInt();
a[i] = a[i] + a[i - 1];
}
for (int j = 1; j < b.length; j++) {
b[j] = sc.nextInt();
b[j] = b[j] + b[j - 1];
}
// for divisors
long ans = 0;
for (int j = 1; j * j <= k; j++) {
if (k % j == 0) {
int x = k / j;
int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;
// 1*4
// 1
for (int i = j; i < a.length; i++) {
if (a[i] - a[i - j] == j) {
c1++;
}
}
// 4
for (int i = x; i < b.length; i++) {
if (b[i] - b[i - x] == x) {
c2++;
}
}
// 4
for (int i = x; i < a.length; i++) {
if (a[i] - a[i - x] == x) {
c3++;
}
}
// 1
for (int i = j; i < b.length; i++) {
if (b[i] - b[i - j] == j) {
c4++;
}
}
if (x == j) {
ans = ans + c1 * c2;
} else {
ans = ans + c1 * c2 + c3 * c4;
}
}
}
System.out.println(ans);
}
}