不会做 直接贴
Each case contains a single integer L (L<=1,000,000,000,000).
1 5
1HintIn our test case, we could find a right triangle (3,4,5) which satisfy 3,4,5<=5 and gcd(3,4)=1,gcd(3,5)=1,gcd(4,5)=1.
//
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define MAXN 1000000
using namespace std;
int pri[MAXN+10],pri_num,tab_phi[MAXN+10];
bool temp[MAXN+10];
int T,num,Dec[20];
long long ans,n;
void print_prime()
{
pri_num=0;
for(int i=2;i<MAXN;i+=2) temp[i]=1;
pri[pri_num++]=2;
tab_phi[2]=1;
tab_phi[1]=1;
for(int i=3;i<MAXN;i+=2)
{
if(!temp[i]) pri[pri_num++]=i,tab_phi[i]=i-1;
for(int j=0;j<pri_num&&i*pri[j]<MAXN;j++)
{
temp[i*pri[j]]=1;
if(i%pri[j]==0) {tab_phi[i*pri[j]]=tab_phi[i]*pri[j];break;}
else tab_phi[i*pri[j]]=tab_phi[i]*(pri[j]-1);
}
if((((i+1)>>1)&1)==0) tab_phi[i+1]=tab_phi[(i+1)>>1]<<1;
else tab_phi[i+1]=tab_phi[(i+1)>>1];
}
}
void prime_dec(int a)
{
num=0;
if(!temp[a]) {Dec[num++]=a; return;}
for(int i=0;i<pri_num&&a>1;i++)
{
if(a%pri[i]==0)
{
Dec[num++]=pri[i];
while(a%pri[i]==0)
{
a/=pri[i];
}
if(a>1&&(!temp[a])) {Dec[num++]=a; return;}
}
}
}
int dfs(int k,int l,int s,int a)
{
if(k==num)
{
if(l&1) ans-=a/s;
else ans+=a/s;
return 0;
}
dfs(k+1,l,s,a);
dfs(k+1,l+1,s*Dec[k],a);
return 0;
}
int main()
{
print_prime();
cin>>T;
while(T--)
{
ans=0;
scanf("%I64d",&n);
int m=(int)sqrt(n+0.0);
for(int i=m;i>0;i--)
{
int p=(int)sqrt(n-(long long)i*i+0.0);
if(i&1)
{
prime_dec(i);
if(i<=p) dfs(0,0,1,i>>1);
else dfs(0,0,1,p>>1);
}
else
{
if(i<=p) ans+=tab_phi[i];
else { prime_dec(i); dfs(0,0,1,p); }
}
}
printf("%I64d\n",ans);
}
return 0;
}