HDU 1496(求多次方程的个数)

spoiler posted @ 2011年4月16日 03:52 in 未分类 , 1576 阅读
题目意思很好理解,就是求有多少种方程的解的形式,输出来就可以了。
如果用4重循环的话,可能会超时,没有试过。。直接用hash一一对应就可以了。
2重循环把s = a*x1*x1+b*x2*x2 看做一个数组的下标,直接hash[1000000+s]++;

再2重循环把s = c*x3*x3+d*x4*x4 看做下标,开始查找,如果有hash[1000000-s]跟他对应,说明他们两个值加起来等于0,即为方程的解。因为解可正可负,所以最后要乘16(2的四次方)!

代码+部分注释:

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int a,b,c,d,ans;
int s[2000000];
int solve(){
	int i,j;
	if((a>=0&&b>=0&&c>=0&&d>=0)||(a<=0&&b<=0&&c<=0&&d<=0))
		return 0;
	 memset(s,0,sizeof(s));
       //初始化放在这里 可以避免TLE因为,如果解不合法就没必要
        //初始化,7位数的初始化 很可怕的!
	for(i=1;i<=100;i++)
		for(j=1;j<=100;j++)
			s[1000000+(a*i*i+b*j*j)]+=1;
	for(i=1;i<=100;i++)
		for(j=1;j<=100;j++)
			ans+=s[1000000-(c*i*i+d*j*j)];
	return 0;
}
int main(){
	while(scanf("%d %d %d %d",&a,&b,&c,&d)!=EOF){
		ans=0;
		solve();	
		printf("%d\n",ans*16);
	}
	return 0;
}

 

Avatar_small
seo service london 说:
2024年1月14日 01:23

Everyone needs a good pair or two of casual shoes for Mens and Walkeaz has you covered with our Best Mens Casual Shoes Collection. Add extraordinary style to your everyday excursions when you wear Best Mens Casual Shoes


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter