Pythagoras's Revenge

题目

potenuse length C, satisfies the formula
A2 + B2 = C2
It is also well known that there exist some right triangles in which all three side lengths are integral, such as the classic:
Further examples, both having A = 12, are the following:
The question of the day is, given a fixed integer value for A, how many distinct integers B > A exist such that the hypotenuse length C is integral?
Input Each line contains a single integer A, such that 2 ≤ A < 1048576 = 220. The end of the input is designated by a line containing the value 0.
Output
For each value of A, output the number of integers B > A such that a right triangle having side lengths A and B has a hypotenuse with integral length.
A Hint and a Warning: Our hint is that you need not consider any value for B that is greater than (A2 −1)/2, because for any such right triangle, hypotenuse C satisfies B < C < B + 1, and thus cannot have integral length. Our warning is that for values of A ≈ 220, there could be solutions with B ≈ 239, and thus valuesof C2 > B2 ≈ 278. You can guarantee yourself 64-bit integer calculations by using the type long long in C++ or long in Java. But neither of those types will allow you to accurately calculate the value of C2 for such an extreme case. (Which is, after all, what makes this Pythagoras’s revenge!)

Sample Input
3
12
2
1048574
1048575
0

Sample Output
1
2
0
1
175

题意:给你直角三角形的一条较小的直角边A(整数),问:有几个也是整数的直角边B使得斜边C都是整数?(C可能到达2^78,64位存不下!
思路: 题目说了,C很大,所以含有A^2+B^2的任何思路都免谈.64位存不下!!!
设C=x+B; 则由(C^2-B^2)=a^2;得x(x+2*B)=A^2;=>x是A^2的因子!
于是可以对A进行素因子分解,再dfs组合因子作为x进行枚举,后求得B,计数.详见代码.
对于时间复杂度因式分解不成问题,关键是dfs,由于有整除因数和i的剪枝,也是可以ac的!

#include <iostream>
#include <cstdio>
#define LL long long
#include <map>

using namespace std;
map<LL,LL> mp;//除重
LL A,A2,sum,cnt;//sum用于计算B的个数,cnt用于计算A的因子数,A2即是A^2
LL factor[50];//因子
void factory(LL A)//分解成素因子
{
    cnt=0;
    for (LL i=2;i*i<=A;i++)
    {
        if (A%i==0)
        {
            factor[cnt++]=i;
            while (A%i==0) A/=i;
        }
    }
    if (A>1) factor[cnt++]=A;
}
void dfs(LL num,LL i)
{
    if (i==cnt||A2%num)//关键剪枝
        return;
    LL res=(A2/num-num);
    if (res%2==0)
    {
        res/=2;
        if (res>A&&!mp[res])
        {
            sum++;//计数
            mp[res]++;//去重
        }
    }
    dfs(num*factor[i],i);//同一个素因子允许多次使用
    dfs(num,i+1);//不取第i个素因子
}
int main()
{
    while (scanf("%lld",&A)==1&&A)
    {
        mp.clear();
        A2=A*A;
        sum=0;
        factory(A);
        dfs(1,0);
        printf("%lld\n",sum);
    }
    return 0;
}

声明:个人原创,仅供相互学习交流,请勿抄袭!转载请注明出处!!!

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值