HDU 3939 毕达哥拉斯三元组

简介:

题意:求形成直角三角形切个边长小于L的个数,并且三边两两互素。

也就是求前L本原毕达哥拉斯三元组解的个数。令,其中m>n,。

由上式得z为直角三角形斜边且z<=L



#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 1000008
bool isprime[maxn];
int prime[maxn],nprime,phi[maxn];
void getprime()
{
    long long i,j;
    memset(isprime,1,sizeof(isprime));
    nprime=0;
    for(i=2; i<maxn; i++)
        if(isprime[i])
        {
            prime[nprime++]=i;
            for(j=i*i; j<maxn; j+=i)
                isprime[j]=0;
        }
}
void getphi()
{
    for(int i=1; i<maxn; i++) phi[i]=i;
    for(int i=2; i<maxn; i+=2) phi[i]>>=1;
    for(int i=3; i<maxn; i+=2)
        if(phi[i]==i)
            for(int j=i; j<maxn; j+=i)
                phi[j]=phi[j]/i*(i-1);
}
int check[35],num;
long long ans;
void getcheck(int x)
{
    num=0;
    int d=x;
    if(isprime[x])
    {
        check[num++]=x;
        return;
    }
    for(int i=0; prime[i]*prime[i]<=x; i++)
        if(d%prime[i]==0)
        {
            check[num++]=prime[i];
            while(d%prime[i]==0)d/=prime[i];
        }
    if(d>1&&isprime[d]) check[num++]=d;
}
void dfs(int k,int r,int s,int n)
{
    if(k==num)
    {
        if(r&1) ans-=n/s;
        else ans+=n/s;
        return;
    }
    dfs(k+1,r,s,n);
    dfs(k+1,r+1,s*check[k],n);
}
int main()
{
    int t;
    long long l;
    getprime();
    getphi();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d",&l);
        ans=0;
        int m=sqrt(double(l-1));
        for(int i=m; i>0; i--)
        {
            int p=sqrt(double(l-(long long)i*i));
            if(i&1)
            {
                getcheck(i);
                if(p<i) dfs(0,0,1,p>>1);
                else dfs(0,0,1,i>>1);
            }
            else
            {
                if(i<p) ans+=phi[i];
                else getcheck(i),dfs(0,0,1,p);
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}



目录
相关文章
|
23天前
lanqiao OJ 246 矩阵计数
lanqiao OJ 246 矩阵计数
11 0
|
6月前
|
存储 算法 搜索推荐
Acwing862. 三元组排序
Acwing862. 三元组排序
|
6月前
【每日一题Day218】LC1091 二进制矩阵中的最短路径 | BFS
【每日一题Day218】LC1091 二进制矩阵中的最短路径 | BFS
32 0
AcWing 862. 三元组排序
AcWing 862. 三元组排序
63 0
AcWing 862. 三元组排序
|
机器学习/深度学习
[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图
Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible.
142 0
[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图
[POJ | Nowcoder] Watchcow | 欧拉回路 点路径输出
Description Bessie’s been appointed the new watch-cow for the farm. Every night, it’s her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she’s done.
164 0
|
算法 Oracle 关系型数据库
LeetCode(算法)- 54. 螺旋矩阵
LeetCode(算法)- 54. 螺旋矩阵
129 0
【2020】三元组(水题)
取余的结果都是相同的;如果选不出这样三个整数,则这五个整数的权值为 -1。 现在给定 T 组数据,每组数据包含五个 0~9 范围内的整数,分别求这 T 组数据中五个整数的权值。
198 0