Matrix(数学,子串和,矩阵,数因子)

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 thatx ≤ 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 %I64dspecifier.

Examples
input
10
12345
output
6
input
16
439873893693495623498263984765
output
40

思路:
给你一串数字,只需求出他的子串和(等于给出数字的因子),求出个数。
注意,0的优化。

代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
typedef long long LL;
LL a[4000+10];
LL b[50005+10];
int main()
{
    string s;
    int k;
    cin>>k>>s;
    int len=s.size();
    for(int i=0;i<len;i++)
        a[i]=s[i]-'0';
    for(int i=0;i<len;i++)
    {
        int sum=0;
        for(int j=i;j<len;j++)
        {
            sum+=a[j];
            b[sum]++;
        }
    }
    LL sum=0;
    if(k==0)
    {
        for(int i=1;i<=50000;i++)
            sum+=b[i]*b[0]*2;
        if(b[0])
            sum+=b[0]*b[0];
    }
    else
    {
        for(int i=1;i<=50000;i++)
        {
            if(k%i==0&&b[i])
            {
                int tm=k/i;
                if(tm>50000)
                    continue;
                else
                    sum+=b[i]*b[tm];
            }
        }
    }
    printf("%lld\n",sum);
}


 
        


 
        



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值