You have a string of decimal digits s. Let's define bij = si·sj. Find in matrix b the number of such rectangles that the sum bij for all cells (i, j) that are the elements of the rectangle equals a in each rectangle.
A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t.
The first line contains integer a (0 ≤ a ≤ 109), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000).
Print a single integer — the answer to a problem.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
10 12345
6
16 439873893693495623498263984765
40
题意:给出定值K, 给出一个字符串S(|S| <= 4000)只包含0~9, 定义矩阵B每个元素B(i,j) = SiSj, 问B有多少子矩阵使子矩阵sum({x1,y1},{x2,y2}) = K.
题解:刚开始想从矩阵入手,发现贼SB,其实对于一个子矩阵设左上角右上角分别为(x1,y1),(x2,y2),其和就等于sum_S{x1...x2}* sum_S{y1...y2}, 所以我们枚举a的因子, 在算下贡献即可。注意特殊K = 0的情况。
#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 7;
int mp[N];
int pre[N];
char s[N];
int main()
{
int a;
scanf("%d ",&a);
scanf("%s",s+1);
int n = strlen(s+1);
for(int i = 1;i <= n;i ++) pre[i] = pre[i-1] + (s[i]-'0');
for(int i = 1;i <= n;i ++) {
for(int j = i;j <= n;j ++) {
mp[pre[j]-pre[i-1]] ++;
}
}
if(!a) {
long long res = mp[0] * 1LL * mp[0];
for(int i = 1;i < N;i ++) res = res + mp[0] * 2LL * mp[i];
printf("%lld\n", res);
} else {
long long res = 0;
for(int i = 1;i <= a && i < 40000;i ++) {
if(a % i == 0 && a / i < 40000) res += mp[i] * 1LL * mp[a / i];
}
printf("%lld\n", res);
}
return 0;
}