Codeforces 365C 思维或二分

31 篇文章 0 订阅
19 篇文章 0 订阅
C. 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 cin, cout streams or the %I64d specifier.

Examples
Input
Copy
10
12345
Output
6
Input
Copy
16
439873893693495623498263984765
Output
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值