欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU - 1496 Equations(多种解法)

程序员文章站 2024-03-20 18:46:34
...

题目:

Consider equations having the following form:

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.

Determine how many solutions satisfy the given equation.

(懒得翻译了,直接抄原文)

这道题的解法非常多,光我见到的就至少得有五六种。就是数据比较刁钻,十组得有9组是全正或全负的,如果不加个判断,许多理论复杂度可行的方法都会TLE,囧~~~

我简单地介绍几种方法:

解法一(530ms):三重循环暴力+整除判断

枚举x1,x2,x3,并用等式性质算出x4,并判断是否在范围内。(因为正负平方会重复,所以只取1-100内的值,将结果乘16即可)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#define FRER() freopen("i.txt","r",stdin)
#define FREW() freopen("o.txt","w",stdout)

using namespace std;
typedef long long LL;
const int N=10000+100;
int vis[N];

int main()
{
    //FRER();
    memset(vis,0,sizeof vis);
    for(int i=1; i<=100; ++i)
    {
        vis[i*i]=1;
    }
    int a,b,c,d;

    while(cin>>a>>b>>c>>d)
    {
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
        {
            printf("0\n");
            continue;
        }
        int cnt=0;
        for(int x1=1; x1<=100; ++x1)
            for(int x2=1; x2<=100; ++x2)
                for(int x3=1; x3<=100; ++x3)
                {
                    int p=-(a*x1*x1+b*x2*x2+c*x3*x3);
                    if(p%d==0&&p/d>0&&p/d<=10000&&vis[p/d])++cnt;
                }
        cout<<cnt*16<<endl;
    }
    return 0;
}

解法二(764ms):用sqrt直接判断是否为平方数,省去了vis数组的空间开销,其他同解法一

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#define FRER() freopen("i.txt","r",stdin)
#define FREW() freopen("o.txt","w",stdout)

using namespace std;
typedef long long LL;

bool ok(int x)
{
    return abs(sqrt(x)-round(sqrt(x)))<1e-5;
}

int main()
{
    //FRER();
    int a,b,c,d;

    while(cin>>a>>b>>c>>d)
    {
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
        {
            printf("0\n");
            continue;
        }
        int cnt=0;
        for(int x1=1; x1<=100; ++x1)
            for(int x2=1; x2<=100; ++x2)
                for(int x3=1; x3<=100; ++x3)
                {
                    int p=-(a*x1*x1+b*x2*x2+c*x3*x3);
                    if(p%d==0&&p/d>0&&p/d<=10000)
                    {
                        if(ok(p/d))++cnt;
                    }
                }
        cout<<cnt*16<<endl;
    }
    return 0;
}

解法三(280ms):折半枚举,先枚举x1,x2的值,用map记录ax1^2+bx2^2出现的次数,再枚举x3,x4的值,将结果加上-(cx3^2+dx4^2)出现的次数。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define FRER() freopen("i.txt","r",stdin)
#define FREW() freopen("o.txt","w",stdout)

using namespace std;
typedef long long LL;
const int N=2000000+100;

map<int,int> cnt;

int main()
{
    //FRER();
    int a,b,c,d;
    while(scanf("%d%d%d%d",&a,&b,&c,&d)==4)
    {
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
        {
            printf("0\n");
            continue;
        }
        cnt.clear();
        int ans=0;
        for(int x1=1; x1<=100; ++x1)
            for(int x2=1; x2<=100; ++x2)
                cnt[-(a*x1*x1+b*x2*x2)]++;
        for(int x3=1; x3<=100; ++x3)
            for(int x4=1; x4<=100; ++x4)
                if(cnt.count(c*x3*x3+d*x4*x4))ans+=cnt[c*x3*x3+d*x4*x4];
        printf("%d\n",ans*16);
    }
    return 0;
}

解法四(31ms):用cnt数组来保存枚举过的值,并用另一个Index数组来记录出现过的值,清零的时候只清零所有Index的cnt值即可,其余同解法三。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#define FRER() freopen("i.txt","r",stdin)
#define FREW() freopen("o.txt","w",stdout)

using namespace std;
typedef long long LL;
const int N=2000000+100;

int cnt[N],Index[200000],k=0;

int main()
{
    //FRER();
    int a,b,c,d;
    while(scanf("%d%d%d%d",&a,&b,&c,&d)==4)
    {
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
        {
            printf("0\n");
            continue;
        }
        for(int i=0; i<k; ++i)
            cnt[Index[i]]=0;
        k=0;
        int ans=0;
        for(int x1=1; x1<=100; ++x1)
            for(int x2=1; x2<=100; ++x2)
                cnt[Index[k++]=-(a*x1*x1+b*x2*x2)+1000000]++;
        for(int x3=1; x3<=100; ++x3)
            for(int x4=1; x4<=100; ++x4)
                ans+=cnt[c*x3*x3+d*x4*x4+1000000];
        printf("%d\n",ans*16);
    }
    return 0;
}

解法五(46ms):用哈希表来记录cnt值,节省了大量空间,而且查找速度也较map快了许多。其余同三、四。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#define FRER() freopen("i.txt","r",stdin)
#define FREW() freopen("o.txt","w",stdout)

using namespace std;
typedef long long LL;
const int N=20000+100;
const int mod=(1<<15)-1;
int head[mod],nxt[N],num[N],cnt[N],nmem;

int Hash(int x)
{
    return (x%mod+mod)%mod;
}
void Insert(int x)
{
    int H=Hash(x);
    for(int u=head[H]; ~u; u=nxt[u])
    {
        if(num[u]==x)
        {
            cnt[u]++;
            return;
        }
    }
    nxt[nmem]=head[H],num[nmem]=x,cnt[nmem]=1,head[H]=nmem++;
}
int Search(int x)
{
    int H=Hash(x);
    for(int u=head[H]; ~u; u=nxt[u])
    {
        if(num[u]==x)
            return cnt[u];
    }
    return 0;
}

int main()
{
    //FRER();
    int a,b,c,d;
    while(scanf("%d%d%d%d",&a,&b,&c,&d)==4)
    {
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
        {
            printf("0\n");
            continue;
        }
        memset(head,-1,sizeof head);
        nmem=0;
        int ans=0;
        for(int x1=1; x1<=100; ++x1)
            for(int x2=1; x2<=100; ++x2)
                Insert(-(a*x1*x1+b*x2*x2));
        for(int x3=1; x3<=100; ++x3)
            for(int x4=1; x4<=100; ++x4)
                ans+=Search(c*x3*x3+d*x4*x4);
        printf("%d\n",ans*16);
    }
    return 0;
}

在vj上,如果不加全正或全负的判断的话,只有最后两种解法能过...

相关标签: 折半法