HDU-1496 Equations
Time Limit: 3000 ms / Memory Limit: 32768 kb
Description
Consider equations having the following form:
a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.
It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.
Determine how many solutions satisfy the given equation.
Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.
Output
For each test case, output a single line containing the number of the solutions.
Sample Input
1 2 3 -4
1 1 1 1
Sample Output
39088
0
Source
“2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛
老师让理解优化算法时举的例题,不知道为什么给的时间,明明可以三重循环过,为什么最后只能n^2log2(n^n)才能过
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 4;
int ans[105];
int tmp[N];
int main()
{
int a,b,c,d;
for(int i = 1;i <= 100;++i)
{
ans[i] = i * i;
}
while(~scanf("%d %d %d %d",&a,&b,&c,&d))
{
int cnt = 0;
for(int i = 1;i <= 100;++i)
{
for(int j = 1;j <= 100;++j)
{
tmp[cnt++] = a * ans[i] + b * ans[j];
}
}
sort(tmp,tmp + cnt);
int sum = 0;
for(int i = 1;i <= 100;++i)
{
for(int j = 1;j <= 100;++j)
{
int num = c * ans[i] + d * ans[j];
num = -num;
sum += (upper_bound(tmp,tmp + cnt,num) - lower_bound(tmp,tmp + cnt,num));
}
}
sum *= 16;
printf("%d\n",sum);
}
return 0;
}
upper_bound()与lower_bound()使用方法
c++中二分的函数
#include <algorithm>//必须包含的头文件
#include <stdio.h>
using namespace std;
int main()
{
int n,a[100],m;
int left,right,i;
scanf("%d",&n);//设初始数组内元素有n个
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);//插入的数为m
left = upper_bound(a,a+n,m)-a;//按从小到大,m最多能插入数组a的哪个位置
right = lower_bound(a,a+n,m)-a;//按从小到大,m最少能插入数组a的哪个位置
printf("m最多能插入数组a的%d\n",left);
for(i=0;i<left;i++)
printf("%d ",a[i]);
printf("%d ",m);
for(i=left;i<n;i++)
printf("%d ",a[i]);
printf("\n");
printf("m最少能插入数组a的%d\n",right);
for(i=0;i<right;i++)
printf("%d ",a[i]);
printf("%d ",m);
for(i=right;i<n;i++)
printf("%d ",a[i]);
return 0;
}