POJ 1840 Eqs

解题思路:hash+枚举

0 0 0 0 0 输出 1e10,因此需要64位整数

某一系数为0,做一点优化

NULL
   
     
#include < iostream >
using namespace std;
#define PRIME 100003
#define MAXN 1000001
#define Cube(n) ((n)*(n)*(n))
int p[MAXN],first[PRIME],next[MAXN];
int main()
{
int i,j,a,b,c,h,t,coef[ 5 ],iter = 1000000 ,zero = 0 ;
long long ans = 0 ;
for (i = 0 ;i < PRIME;i ++ )first[i] =- 1 ;
for (i = 0 ;i < 5 ;i ++ )scanf( " %d " , & coef[i]);
for (i = 2 ;i >= 0 ;i -- ) if ( ! coef[i]){iter /= 100 ;zero ++ ;} else if (zero){coef[i + zero] = coef[i];coef[i] = 0 ;}
for (j = i = 0 ;i < iter;i ++ ,j = i)
{
if ( ! coef[ 0 ])a = 0 ; else {a = j / 10000 - 50 ; if (a >= 0 )a ++ ;j %= 10000 ;}
if ( ! coef[ 1 ])b = 0 ; else {b = j / 100 - 50 ; if (b >= 0 )b ++ ;j %= 100 ;}
if ( ! coef[ 2 ])c = 0 ; else {c = j - 50 ; if (c >= 0 )c ++ ;}
h
= coef[ 0 ] * Cube(a) + coef[ 1 ] * Cube(b) + coef[ 2 ] * Cube(c);
p[i]
= h;
h
%= PRIME; if (h < 0 )h += PRIME;
next[i]
= first[h];first[h] = i;
}
for (iter = 1e4,i = 3 ;i < 5 ;i ++ ) if ( ! coef[i])iter /= 100 ,zero ++ ;
if ( ! coef[ 4 ]){coef[ 4 ] = coef[ 3 ];coef[ 3 ] = 0 ;}
for (j = i = 0 ;i < iter;i ++ ,j = i)
{
if ( ! coef[ 3 ])a = 0 ; else {a = j / 100 - 50 ; if (a >= 0 )a ++ ;}
if ( ! coef[ 4 ])b = 0 ; else {b = j % 100 - 50 ; if (b >= 0 )b ++ ;}
c
=- coef[ 3 ] * Cube(a) - coef[ 4 ] * Cube(b);
h
= c % PRIME; if (h < 0 )h += PRIME;
for (t = first[h];t !=- 1 ;t = next[t])
if (p[t] == c)ans ++ ;
}
while (zero -- )ans *= 100 ;
printf(
" %lld\n " ,ans);
return 0 ;
}

 

你可能感兴趣的:(poj)