Matrix CodeForces - 364A

原题链接
考察:组合数学+思维
思路:
  如果只考虑一行的和是比较容易想到的:假设该行对应的\(s\)\(x\),如果\(a\%x==0\),那么求出\(sum = \frac{a}{x}\)的个数即可.如果有多行,就统计多行的和\(x\).我们直接统计和的个数即可.
  特判\(!a\)的情况,此时只要一个因子为0,其他任意值.

Code

#include <iostream> 
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 4010,M = 36010;
char s[N];
int m,n,cnt[M],sum[N],sz;
void get(int m)
{
	LL res = 0;
	if(!m)
	{
		for(int i=0;i<n;i++)
		  for(int j=i+1;j<=n;j++)
		  {
		  	int x = sum[j]-sum[i];
		  	if(x!=0) res+=(LL)cnt[0]*2;
		  	else res+=(LL)cnt[0];
		  }
	}
	for(int i=1;i<=m/i;i++)
	{
		if(m%i==0)
		{
			if(i>=M||m/i>=M) continue;
			res+=(LL)cnt[i]*cnt[m/i];//行*列 
			if(i!=m/i) res+=(LL)cnt[i]*cnt[m/i];
		}
	}
	printf("%lld\n",res);
}
int main()
{
	scanf("%d%s",&m,s+1);
	n = strlen(s+1);
	for(int i=1;i<=n;i++)
	{
		int x = s[i]-'0';
		sum[i] = sum[i-1]+x;
		for(int j=0;j<i;j++)
		  cnt[sum[i]-sum[j]]++;
	}
	get(m);
	return 0;
}
posted @ 2021-07-27 09:47  acmloser  阅读(44)  评论(0编辑  收藏  举报