天天写算法之(hash散列)Equations

地址: 点击打开链接
这个题目,emmm,第一次解除用hash解题的。很新奇的思路。直接上代码吧,其实就是把方程左右改了一下位置。最后乘以16(2^4)

code:

#include <stdio.h>
#include <string.h>
 
int hash[2000005];
 
int main(  )
{
    int a, b, c, d;
    while( ~scanf( "%d%d%d%d", &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;
        }
        memset( hash, 0, sizeof( hash ) ); //置于上面会超时
        int cnt= 0;
        for( int i= 1; i<= 100; ++i )
        {
            for( int j= 1; j<= 100; ++j )
            {
                hash[ a* i* i+ b* j* j+ 1000000 ]++;
            }
        }
        for( int i= 1; i<= 100; ++i )
        {
            for( int j= 1; j<= 100; ++j )
            {
                cnt+= hash[ 1000000- c*i *i- d*j *j ];
            }
        }
        printf( "%d\n", cnt* 16 );
    }
     
}

猜你喜欢

转载自blog.csdn.net/qq_36616268/article/details/80551867