• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU 1496 Equations

 

题意:给你四个数a,b,c,d,让你求满足等式a*x1^2+b*x2^2+c*x3^2+d*x4^2=0;的x的个数。

思路:经典Hash,开两个数组分别保存正数和负数,时间复杂度降到n^2。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1496

 

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <string>
 6 #include <iostream>
 7 using namespace std;
 8 const int N=1000010;
 9 
10 int k1[N];
11 int k2[N];
12 
13 int main(){
14     
15 //    freopen("data.in","r",stdin);
16 //    freopen("data.out","w",stdout);
17 
18     int i,j,k,a,b,c,d;
19     
20     while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
21         if(a>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0){
22             puts("0");
23             continue;
24         }
25         memset(k1,0,sizeof(k1));
26         memset(k2,0,sizeof(k2));
27         for(i=1;i<=100;i++){
28             for(j=1;j<=100;j++){
29                 k=a*i*i+b*j*j;
30                 if(k>=0) k1[k]++;
31                 else k2[-k]++;    
32             }
33         } 
34         int sum=0;
35         for(i=1;i<=100;i++){
36             for(j=1;j<=100;j++){
37                 k=c*i*i+d*j*j;
38                 if(k>0) sum+=k2[k];
39                 else sum+=k1[-k];    
40             }
41         } 
42         printf("%d\n",sum*16);    //X分正负,每个X有2个解,4个就有16种不同的解  
43     }
44     return 0;
45 }

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <string>
 6 #include <iostream>
 7 using namespace std;
 8 const int N=1000010;
 9 
10 int k1[N];
11 int k2[N];
12 
13 int main(){
14     
15 //    freopen("data.in","r",stdin);
16 //    freopen("data.out","w",stdout);
17 
18     int i,j,k,a,b,c,d;
19     
20     while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
21         if(a>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0){
22             puts("0");
23             continue;
24         }
25         memset(k1,0,sizeof(k1));
26         memset(k2,0,sizeof(k2));
27         for(i=1;i<=100;i++){
28             for(j=1;j<=100;j++){
29                 k=a*i*i+b*j*j;
30                 if(k>=0) k1[k]++;
31                 else k2[-k]++;    
32             }
33         } 
34         int sum=0;
35         for(i=1;i<=100;i++){
36             for(j=1;j<=100;j++){
37                 k=c*i*i+d*j*j;
38                 if(k>0) sum+=k2[k];
39                 else sum+=k1[-k];    
40             }
41         } 
42         printf("%d\n",sum*16);    //X分正负,每个X有2个解,4个就有16种不同的解  
43     }
44     return 0;
45 }
posted @ 2012-04-26 09:29  Hug_Sea  阅读(107)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3