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

来源:互联网 发布:淘宝店铺主营在哪里改 编辑:程序博客网 时间:2024/06/05 16:45

题目链接:点击打开链接


这题昨天做的时候题意读错了。。。关于数矩形和=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;}


原创粉丝点击