HDU1496 Equations

这道题目如果直接用4个for循环暴力求解显然是不行的 ,所以我用一个优化减少一个循环,又由于值是正负的,所以又可以从-100,100  变成只要循环1-100就足够



#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
bool vis[10005];
void init()
{
    int i;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=100;i++)
        vis[i*i]=true;
}
int main()
{
    int i1, i2, i3, i4, a, b, c, d, ans, tmp, xx;
    init();
    while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
    {
        ans=0;
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
        {
            puts("0");
            continue;
        }


        for(i1=1; i1<=100; i1++)
        {
            for(i2=1; i2<=100; i2++)
            {
                for(i3=1; i3<=100; i3++)
                {
                    tmp=a*i1*i1+b*i2*i2+c*i3*i3;
                    if((-tmp)%d==0)
                    {
                        xx=-tmp/d;
                        if(xx<=10000&&xx>0&&vis[xx]) ans+=16;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


posted @ 2013-08-15 20:28  Ink_syk  阅读(145)  评论(0编辑  收藏  举报