直角三角形,三条边的长度都是整数。给出周长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的数量(数学,思维题)

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

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

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

  4. 51Nod 1003 阶乘后面0的数量 | 思维

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

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

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

  6. @51nod - 1196/1197/1198@ 字符串的数量

    目录 @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. iso socket基础2

    iPhone socket 编程之BSD Socket篇 收藏在进行iPhone网络通讯程序的开发中,不可避免的要利用Socket套接字.iPhone提供了Socket网络编程的接口CFSocket, ...

  2. ios5之后arc的问题

    原创: 自从ios5以后, apple官方引进了ARC (automaically reference count 自动索引计数) 这个新特性, 简单的说就是对象设置为nil(空引用)之后, 当自动索 ...

  3. hiho #1283 hiho密码 [Offer收割]编程练习赛3

    #1283 : hiho密码 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho根据最近在密码学课上学习到的知识,开发出了一款hiho密码,这款密码的秘钥是这样生成的 ...

  4. Vmware怎样使用nat和桥接方式解决虚拟机联网问题

    对于很多的linux初学者来说,最开始学习linux时通常是在虚拟机上进行的,然而对于新手来说虚拟机联网会对他们来说是比较困难的.这里我根据自己的经验写了一篇文档分享给大家.下面对几种连接方式进行简单 ...

  5. poj2240最短路 floyd

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17360   Accepted: 7308 Descri ...

  6. css3 变形(transform)、转换(transition)和动画(animation)

    http://www.w3cplus.com/content/css3-transform/  在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动tr ...

  7. JQuery $.ajax()方法详解

    jQuery Ajax 参考手册 实例 通过 AJAX 加载一段文本: jQuery 代码: $(document).ready(function(){ $("#b01").cli ...

  8. Cocos2d 中的Sprite大小调整问题

    以前用UIImageView,比如  UIImageView *view = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"b ...

  9. php字符串处理总结

    php字符串处理是php基础中重要的一部分,总结并整理了一下 1.最简单的,字符串输出单引号和双引号的区别(定义字符串用单引号和双引号都可以) $str='hello'; echo "str ...

  10. C#模拟百度登录

    目录: 1.fiddler解析百度登录地址 2.处理传入参数 1.fiddler解析百度登录地址 因工作需要,所以研究了下百度的登陆.首先打开https://passport.baidu.com/v2 ...