矩形
题意:给定一个由数字(0-9)构成的字符串s。我们可以由此定义出size(s) * size(s) 大小的矩阵b,其中b[i][j] = s[i] * s[j];请问在这个矩阵b中,有多少子矩形满足其中的b[i][j]的和为另一个给定的数字a。对100%的输入数据:0 ≤a≤1000000000,size(s)≤4000
解法:细心观察可知,
∑i=x1,j=y1i<=x2,j<=y2b[i][j]
=∑i=x1i<=x2a[i]∗∑j=y1j<=y2a[j]
那我们可以将
∑i=x1i<=x2a[i](1<=x1<=x2<=n)
放进一个hash里面,但是我们发现其实他们不超过9*size(s)=9*4000=36000,用一个数组计数就可以了。再次枚举,查询a/sum的个数就是对答案的贡献。但是要特殊处理a=0且sum=0时任意数的个数就是对答案的贡献。
贴上代码
#include<set>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const oo=2147483647;
int const maxl=4000;
typedef struct{int x,y;}note;
inline int get(){
char ch=getchar();while((ch!='-')&&((ch<'0')||(ch>'9')))ch=getchar();
int u=1,v=0;if(ch=='-')u=-1;else v=ch-'0';ch=getchar();
while((ch>='0')&&(ch<='9'))v=v*10+ch-'0',ch=getchar();
return u*v;
}
inline char getch(){
char ch=getchar();
while((ch<'A')||(ch>'Z'))ch=getchar();
return ch;
}
int a,len,sum[maxl+10],cnt[maxl*10+10];
char s[maxl+10];
inline void scan(){
a=get();
scanf("%s",&s[1]);
len=strlen(&s[1]);
}
inline void solve(){
fo(i,1,len)
sum[i]=sum[i-1]+s[i]-'0';
fo(i,1,len)
fo(j,i,len)
cnt[sum[j]-sum[i-1]]++;
int ans=0,totsum=0;
fo(k,0,10*4000)
totsum+=cnt[k];
fo(i,1,len)
fo(j,i,len)
if((sum[j]-sum[i-1]!=0)&&(a%(sum[j]-sum[i-1])==0)&&(a/(sum[j]-sum[i-1])<=10*4000))
ans+=cnt[a/(sum[j]-sum[i-1])];
else if((a==0)&&(sum[j]-sum[i-1]==0))ans+=totsum;
printf("%d",ans);
}
int main(){
scan();
solve();
return 0;
}