CodeForces 364A Matrix

A. Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains integer a (0 ≤ a ≤ 109), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000).

Output

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 cincout streams or the%I64d specifier.

Sample test(s)
input
10
12345
output
6
input
16
439873893693495623498263984765
output
40


子矩阵的和= sum(va(a~x)*sum(va(b~y))
 要优化一下,a==0的情况要特殊考虑


#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

typedef long long int LL;

LL x_array[5000],y_array[5000];
LL dx[50001],dy[50001];

int main()
{
    int a;char str[5000];
    LL cnt=0;
    scanf("%d",&a);
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int j=i+1;
        x_array[j]=x_array[j-1]+str[i]-'0';
        y_array[j]=y_array[j-1]+str[i]-'0';
    };
    for(int i=1;i<=len;i++)
    {
        for(int j=i;j<=len;j++)
        {
            dx[x_array[j]-x_array[i-1]]++;
            dy[y_array[j]-y_array[i-1]]++;
        }
    }
    if(a!=0)
    {
        for(int i=1;i<50000;i++)
        {
            if(dx[i]&&a%i==0)
            {
                int temp=a/i;
                if(temp>=50000) continue;
                cnt+=dx[i]*dy[temp];
            }
        }
    }
    else if(a==0)
    {
        for(int i=1;i<50000;i++)
        {
            cnt+=dx[0]*dy[i]+dx[i]*dy[0];
        }
        if(dx[0]) cnt+=dx[0]*dy[0];
    }
    printf("%I64d\n",cnt);
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值