Codeforces Round #213 (diy.2) C. Matrix

题目链接:点击打开链接


这题昨天做的时候题意读错了。。。关于数矩形和=a的部分,矩形a的值的计算这里

理解错误。



题意:(解题报告来源:cherish_)

用题目中的方法构造一个矩阵。

比如第一个样例

1 2 3 4 5

2 4 6 8 10

3 6 9 12 15

4 8 12 16 20

5 10 15 20 25


然后求出矩阵中长方形的和等于a的长方形的数量


思路: 

1、计算构成它的长的那些数字的和  

2、构成它的宽的那些数字的和    

3、长方形的和=两个和的积


枚举出来S可以凑出的数字

然后行和列能凑出来的东西是一样的

那么枚举出现过的东西   然后去凑出a


当a==0的时候特判一下就Ok啦 

记得longlong


//31ms

//cnt数组要用long long类型~~~~~~

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

char str[4010];
int b[4010];
long long cnt[40010];
long long ans;

int main()
{
    int a;
    while(scanf("%d%s",&a,&str)!=EOF)
    {
        memset(cnt,0,sizeof(cnt));
        memset(b,0,sizeof(b));
        ans=0;

        int len=strlen(str);
        for(int i=0;i<len;i++)
            b[i]=str[i]-'0';

        for(int i=0;i<len;i++)
        {
            int s=0;
            for(int j=i;j<len;j++)
            {
                s+=b[j];
                cnt[s]++;
                //cout<<s<<' '<<cnt[s]<<endl;
            }
        }
        if(a==0)
        {
            for(int i=0;i<40000;i++)
                ans+=cnt[i]*cnt[0];
        }
        //else
        //{
            for(int i=1;i<40000;i++)
            {
                if(a%i==0 && a/i>=0 && a/i<=len*9)
                    ans+=cnt[i]*cnt[a/i];
            }
        //}
        printf("%I64d\n",ans);

        memset(str,0,sizeof(str));
    }
    return 0;
}

另外一种~46ms

#include<cstdio>
#include<iostream>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<cstdlib>

using namespace std;

char str[4050];
int a[4050];
int b[40010];

int main()
{
    int m,n;
    while(scanf("%d%s",&m,str)!=EOF)
    {
        n = strlen(str);
        for(int i=0;i<n;i++)
            a[i]=str[i]-'0';
        memset(b,0,sizeof(b));
        for(int i=0;i<n;i++)
        {
            int s=0;
            for(int j=i;j<n;j++)
            {
                s+=a[j];
                b[s]++;
                //cout<<"test:"<<b[s]<<' '<<s<<endl;
            }
        }
        long long ans=0;
        for(int i=0;i<n;i++)
        {
            int s=0;
            for(int j=i;j<n;j++)
            {
                s+=a[j];
                if(s>0 && m%s==0 && m/s<=n*9)
                {
                    //cout<<s<<' '<<b[m/s]<<' '<<m/s<<endl;
                    ans+=b[m/s];
                }
                else if(s==0 && m==0)
                    ans+=(n*(n+1)/2);
            }
        }
        printf("%I64d\n",ans);
        //return 0;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值