题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1165
题意:给定一直角三角形的周长L,求有多少个这样的直角三角形。注意这里数据很多,50000组,且L<10^7
分析:这个题呢,开始以为是毕达哥拉斯三元组,后来发现不是,然后瞬间化简就出来了。分析过程如下:
由:,消去z得到:,把x,y,z都表示出来后发现只要y是整数,
那么x,z也就是整数了。我们进一步令,我们得到:,根据x,y,z的范围我们可以确定出k的范围
满足,这个可以自己推导,此处我就省略了。
然后问题就转化为求因子fac中满足条件的个数。
对于这个问题就简单了,就是这里以前用的方法:http://blog.csdn.net/acdreamers/article/details/8726812
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int N=10000005;
const int M=1005;
bool prime[N];
int p[N];
int pri[M];
int num[M];
int k,cnt,tmp,tmp1,count;
void isprime()
{
k=0;
int i,j;
memset(prime,true,sizeof(prime));
for(i=2;i<N;i++)
{
if(prime[i])
{
p[k++]=i;
for(j=i+i;j<N;j+=i)
{
prime[j]=false;
}
}
}
}
void Find(int n)
{
cnt=0;
int t=(int)sqrt(n*1.0),i,a;
for(i=0;p[i]<=t;i++)
{
if(n%p[i]==0)
{
a=0;
pri[cnt]=p[i];
while(n%p[i]==0)
{
a++;
n/=p[i];
}
num[cnt]=2*a;
cnt++;
}
}
if(n>1)
{
pri[cnt]=n;
num[cnt]=2;
cnt++;
}
}
void dfs(int dept, LL product=1)
{
if(dept==cnt)
{
if(product%2==0&&product>tmp&&product<tmp1)
count++;
return;
}
for(int i=0;i<=num[dept];i++)
{
dfs(dept+1,product);
product*=pri[dept];
}
}
int main()
{
int T,n;
isprime();
scanf("%d",&T);
while(T--)
{
count=0;
scanf("%d",&n);
tmp=(LL)(sqrt(2.0)*n);
tmp1=2*n;
Find(n);
dfs(0,1);
printf("%d\n",count);
}
return 0;
}