a^2 + b^2 = c^2


题目来源:CodeForce 304

Description

In mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states:

In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle).

The theorem can be written as an equation relating the lengths of the sides ab and c, often called the Pythagorean equation:

a2 + b2 = c2

where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.

Given n, your task is to count how many right-angled triangles with side-lengths ab and c that satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n.

Input

The only line contains one integer n (1 ≤ n ≤ 104) as we mentioned above.

Output

Print a single integer — the answer to the problem.

Sample Input

Input
5
Output
1
Input
74
Output
35

首先,我们需要理解题意(^.^  很简单,判断1《a《b《c《n中,有多少个三角形满足a^2+b^2=c^2)。勾股定理。。。。

OK,理解了题意之后,我们开始来做题。啦啦啦。。。

解题思路:

刚开始,最先想到的是暴力枚举每一个a,b,c,看是否满足勾股定理。对,这是最容易想到的方法。

但是!!!请注意n的取值范围,小于等于10000,所以暴力枚举的算法是n^3。不好意思,你已经华丽丽的TLE(超时)了。

所以,再进一步想可不可以优化到时间复杂度为n^2,甚至更小呢......

所以,我们来想只枚举c和b的值,再判断a是否存在。是不是就是n^2的复杂度了吗?

如果a是整数,那么这个三角形肯定存在.....

如果你还怕超时,那么请进行进一步优化。剪枝.......

把需要重复计算的标记,下次再需要用的时候直接返回,不用再一步步的重新计算得出。

对的,就是这样、、、我想你已经知道怎么做了。。。。

代码如下

本人是大水比一枚,代码如有错请积极指出。。。。

#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
int ans[10005],key,n;
int main()
{
    memset(ans,0,sizeof(ans));
    key=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(key>=n)
            {printf("%d\n",ans[n]);
        continue;}
        for(int i=n;i>=5;i--)
        {
            if(i==key)
            {ans[n]+=ans[key];break;}
            for(int j=i-1;j*j>=(i*i)/2;j--)
            {
                int k=sqrt(i*i-j*j);
                if(k*k+j*j==i*i)
                    ans[n]++;
            }
        }
        key=n;
        printf("%d\n",ans[n]);
    }
    return 0;
}



DETR(Detection Transformer)是一种基于Transformer架构的目标检测模型,它摒弃了传统的目标检测方法中使用的锚框(anchor boxes)和非极大值抑制(NMS)等组件,直接通过Transformer解码器生成固定数量的预测框。自DETR提出以来,研究人员对其进行了多种扩展和改进,以提升其性能、效率以及与其他技术的结合能力。 一种常见的扩展方向是改进DETR的收敛速度和检测精度。例如,Deformable DETR引入了可变形卷积机制,通过关注图像上的关键采样点来减少计算复杂度,并加快训练过程。该方法显著提升了模型在COCO数据集上的性能,同时减少了训练所需的时间[^1]。此外,DAB-DETR提出了动态锚框机制,将锚框作为解码器中的可学习查询向量,从而进一步提升检测精度。 另一种扩展方向是将DETR与其他目标检测框架相结合。例如,Sparse DETR通过引入稀疏注意力机制,减少了Transformer中的冗余计算,提高了推理效率。而Conditional DETR则引入了条件空间查询(Conditional Spatial Query),使得模型能够更有效地进行跨模态信息融合,从而在复杂场景下取得更好的检测效果[^1]。 DETR还被扩展到多模态目标检测任务中。例如,MDETR结合了视觉和语言信息,通过Transformer架构实现跨模态对齐,从而实现基于文本描述的目标检测。这种扩展使得DETR能够应用于更广泛的场景,如视觉问答(VQA)和图像描述生成任务[^1]。 在实现方面,许多研究团队已经开源了其改进版本的DETR模型代码。这些实现通常基于PyTorch框架,并依赖于Hugging Face Transformers库或Facebook的Detectron2库。开发者可以通过这些开源项目快速复现和扩展DETR模型,探索不同的改进策略和技术组合。 以下是一个基于Hugging Face Transformers库实现的DETR模型的简单代码示例: ```python from transformers import DetrModel, DetrConfig import torch # 配置DETR模型参数 config = DetrConfig(num_queries=100, hidden_size=256) # 初始化DETR模型 model = DetrModel(config) # 生成随机输入张量 pixel_values = torch.rand(1, 3, 800, 800) # 假设输入图像大小为800x800 # 前向传播 outputs = model(pixel_values=pixel_values) # 输出结果 print(outputs.last_hidden_state.shape) # 输出形状为[1, 100, 256] ``` 该代码示例展示了如何使用Hugging Face Transformers库初始化一个DETR模型并进行前向传播。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值