Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2788665
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: C/C++

2012-04-21 17:41:15

题意:求有多少个解!!

Problem Description
Consider equations having the following form:

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0

题目意思很好理解,就是求有多少种方程的解的形式,输出来就可以了。
如果用4重循环的话,可能会超时,没有试过。
直接用hash一一对应就可以了。
int hash1[10000005]保存得数是正的,a*x*x最大值就是50*100*100,再加上b*x*x就100,0000
int hash2[1000005]; //保存得数是负的

1、两重循环把s1 = a*x1*x1+b*x2*x2 (hash函数)求出来,看做第二个数组的下标,
   如果s > 0,hash1[s]++;若s <= 0; 则 hash2[-s]++;
2、然后针对另外一部分s2=c*x2*x2+d*x3*x3(hash函数),
   s1=-s2,则就是题目的一组解,而hash表记录hash1[s1],记录符合解的个数
   所以,如果s > 0,ans+=hash2[s];若s <= 0; 则 ans+=hash1[-s];//每次是加上一组解(hash[]),
   因为数组二每次可以和一组解组合
 

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>

  4. using namespace std;

  5. int hash1[1000005];
  6. int hash2[1000005];

  7. int main()
  8. {
  9.     int a,b,c,d;
  10.     while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
  11.     {
  12.         if((a>0 && b>0 && c>0 && d>0)|| (a<0 && b<0 && c<0 && d<0))
  13.         {
  14.             printf("0\n");
  15.             continue;
  16.         }
  17.         int ans=0;
  18.         memset(hash1,0,sizeof(hash1));
  19.         memset(hash2,0,sizeof(hash2));
  20.         int i,j,s=1;
  21.         for(i=1;i<=100;i++)
  22.         {    
  23.             for(j=1;j<=100;j++)
  24.             {
  25.                 s=a*i*i+b*j*j;
  26.                 if(s>0)
  27.                     hash1[s]++;//记录这个经过hash函数映射的值的出现次数;
  28.                 else
  29.                     hash2[-s]++;
  30.             }
  31.         }

  32.         for(i=1;i<=100;i++)
  33.         {
  34.             for(j=1;j<=100;j++)
  35.             {
  36.                 s=c*i*i+d*j*j;
  37.                 if(s>=0)
  38.                     ans+=hash2[s];//将同一个s映射到hash数组,ans每次加一组
  39.                 else
  40.                     ans+=hash1[-s];
  41.             }
  42.         }
  43.         ans=ans*16;//因为是平方,所以每次的解都可以取正取负,2*2*2*2
  44.         printf("%d\n",ans);

  45.     }
  46.     
  47.     return 0;
  48. }

阅读(553) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~