51nod 1165 整边直角三角形的数量

时间:2022-06-15 23:40:49
直角三角形,三条边的长度都是整数。给出周长N,求符合条件的三角形数量。
例如:N = 120,共有3种不同的满足条件的直角3角行。分别是:{20,48,52}, {24,45,51}, {30,40,50}。
 
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 50000)
第2 - T + 1行:每行1个数N(12 <= N <= 10^7)。
Output
输出共T行,每行1个对应的数量。
Input示例
2
120
13
Output示例
3
0 题意:问有多少种直角三角形的周长恰好为n
分析:
假设a<b<c
a+b+c=n
a^2+b^2=c^2
c = sqrt(a^2+b^2)
c=n-(a+b)
c^2 = a^2+b^2 = (n-(a+b))^2
a^2+b^2 = n^2 - 2*(a+b)*n + a^2 + b^2 + 2*a*b
2*(a+b)*n = n^2 + 2*a*b
2*n*b - 2*a*b = n^2 - 2*m*a
b = (n^2 - 2*n*a) / (2*n - 2*a)
设 t = n-a
b = (2*(n^2 - n*a) - n^2) / 2*(n-a)
= n - n^2/(2*t) 因为 t = n-a, a < b < c, a+b > c, c < n/2
所以 n-t = a < b = n- n^2/(2*t)
n^2/(2*t) < t
n^2 < 2*t^2
2*t > sqrt(2)*n t < n 所以 sqrt(2)*n < 2*t < 2*n 所以,只需要找出n的所有因数,把它们个数*2,就是L^2因数的个数
然后枚举每个因数多少个(注意,2*t显然是2的倍数),记一下在那个范围里面的个数,即为答案 下面上代码
 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name) {
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} const int N = , M = ;
int Prime[M], Tot;
bool Visit[N];
int n;
int Factor[M], Num[M], Len;
int Left, Right, Answer[N], Ans; inline void GetPrime() {
For(i, , N-) {
if(!Visit[i]) Prime[++Tot] = i;
For(j, , Tot) {
if(i*Prime[j] >= N) break;
Visit[i*Prime[j]] = ;
if(!(i%Prime[j])) break;
}
}
} inline void Solve(); inline void Input() {
GetPrime(); int TestNumber;
scanf("%d", &TestNumber);
while(TestNumber--) {
scanf("%d", &n);
Solve();
}
} inline void GetFactor(int x) {
For(i, , Tot) {
if(x == || Prime[i] > x) break;
if(!(x%Prime[i])) {
Len++;
Factor[Len] = Prime[i], Num[Len] = ;
while(!(x%Prime[i])) x /= Prime[i], Num[Len]++;
}
} if(x > ) {
Len++;
Factor[Len] = x, Num[Len] = ;
}
} LL Temp;
inline void Search(int x, int Val) {
if(Left < Val && Val < Right && !(Val%)) Answer[++Ans] = Val;
if(x > Len || Val >= Right) return; int Cnt = ;
For(i, , Num[x]) {
Search(x+, Val*Cnt);
if(Val*Cnt >= Right/Factor[x]) break;
Cnt *= Factor[x];
}
} inline void Solve() {
if(n&) {
puts("");
return;
} Len = ;
GetFactor(n);
For(i, , Len) Num[i] <<= ; Ans = ;
Left = n*sqrt(), Right = *n;
Search(, ); sort(Answer+, Answer++Ans);
int p = ;
For(i, , Ans)
if(Answer[p] != Answer[i]) Answer[++p] = Answer[i];
/*For(i, 1, p) {
int b = n-n*n/Answer[i];
int a = n-Answer[i]/2;
int c = n-a-b;
printf("%d %d %d %d\n", Answer[i], a, b, c);
}*/
printf("%d\n", p);
} int main() {
#ifndef ONLINE_JUDGE
SetIO("");
#endif
Input();
//printf("%d\n", Tot);
//Solve();
return ;
}
 

51nod 1165 整边直角三角形的数量的更多相关文章

  1. 51nod 1165 整边直角三角形的数量(两种解法)

    链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1165 直角三角形,三条边的长度都是整数.给出周长N,求符合条件的三角形数量. ...

  2. 51Nod 1003 阶乘后面0的数量&lpar;数学&comma;思维题&rpar;

    1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720 ...

  3. pku 1401 Factorial 算数基本定理 &amp&semi;&amp&semi; 51nod 1003 阶乘后面0的数量

    链接:http://poj.org/problem?id=1401 题意:计算N!的末尾0的个数 思路:算数基本定理 有0,分解为2*5,寻找2*5的对数,2的因子个数大于5,转化为寻找因子5的个数. ...

  4. 51Nod 1003 阶乘后面0的数量 &vert; 思维

    题意:n的阶乘后面0的个数,如果直接算出阶乘再数0的数量一定会超时的. 因为10=2*5,所以求出5贡献的次数就行. #include "bits/stdc++.h" using ...

  5. 【51nod 1251】 Fox序列的数量(以及带限制插板法讲解)

    为什么网上没有篇详细的题解[雾 可能各位聚聚觉得这道题太简单了吧 /kk 题意 首先题目是求满足条件的序列个数,条件为:出现次数最多的数仅有一个 分析 感谢 刚睡醒的 JZ姐姐在咱写题解忽然陷入自闭的 ...

  6. &commat;51nod - 1196&sol;1197&sol;1198&commat; 字符串的数量

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...

  7. 51nod 1009:数字1的数量

    1009 数字1的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个 ...

  8. 51nod 1003 阶乘后面0的数量

    每一个 2 与一个 5 相乘,结果就增加一个零. 所以求 n! 后面的连续零的个数,其实就是求其中相乘的数含有因子每对因子 2 与 5  的个数. 又因为从1到某个数,所含 2 的个数比 5 多,所以 ...

  9. 【51nod】1251 Fox序列的数量

    题解 容斥题 我们枚举出现次数最多的数出现了K次 然后我们需要计算的序列是所有数字出现个数都不超过K - 1次 我们枚举不合法的数字的数目j,说明这个排列里除了我们固定出现K次的数至少有j个数是不合法 ...

随机推荐

  1. SVN 错误 Access to SVN Repository Forbidden的原因及解决方法

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/SVN_Access_to_SVN_Repository_Forbidden.html   当我们新 ...

  2. python http代理代码

    googlecode :https://code.google.com/archive/p/python-proxy/source/default/source # -*- coding: cp125 ...

  3. SQL Server附加数据库出现错误5123的正确解决方法

    因为自己有一本基于SQL Server 2005的数据库教程,里边使用的示例数据库是AdventureWorks for SQL Server 2005,而我的机子上装的是SQL Server 200 ...

  4. 51Nod 1046 A&Hat;B Mod C Label&colon;快速幂

    给出3个正整数A B C,求A^B Mod C.   例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...

  5. cf B&period; Hungry Sequence

    http://codeforces.com/contest/327/problem/B 这道题素数打表就行. #include <cstdio> #include <cstring& ...

  6. Spring创建对象的方式3种方式

    此文为基础回顾,估计是最后一次总结. 项目利用maven进行架构,其基本项目结构为: 其中pom.xml中的内容为: <project xmlns="http://maven.apac ...

  7. springMvc接收ajax数组参数,以及jquery复选框选中、反选、全选、全不选

    一.复选框选中.反选.全选.全不选 html代码: <input type='checkbox' name='menuCheckBox' value='10' >苹果 <input ...

  8. React-菜鸟学习笔记(一)

    新公司的项目前端架构用的是react.js 之前孤陋寡闻并没听说过,想着后期开发和维护多少要会点前端的东西,就简单研究一下.个人的学习习惯能写代码的就不写文字,必要的地方加两行注释,代码一行行敲下去, ...

  9. Android Studio开发之Gradle科普

    我们以前开发都是用 Eclipse ,而 Eclipse 大家都知道是一种 IDE (集成开发环境),最初是用来做 Java 开发的,而 Android 是基于 Java 语言的,所以最初 Googl ...

  10. 10 个理由让你继续干 IT

    1.钱,钱,钱 对,我们努力工作就是为了赚钱,而IT专业人士的努力工作的确得到了很好的补偿.报酬不仅仅是好而已,而是非常棒.根据美国劳工部<2010年美国 就业与报酬情况概览>(表6,PD ...