直角三角形,三条边的长度都是整数。给出周长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. UISerachBar / UISearchDisplayController

    1. UISerachBar 继承与UIView, 包含uitextfield, 并且实现了uitextfielddelegate代理的主要内容 含有取消按钮, 默认不显示 2. UISerachDi ...

  2. IOS 中的CoreImage框架

    IOS 中的CoreImage框架(framework) - time4cnblogs 时间 2014-03-15 00:24:00  博客园-所有随笔区原文  http://www.cnblogs. ...

  3. 2014.7建兰NOIP模拟Day1 Running

    突然间翻到着题,想想那时的我真是垃圾,这么简单的tarjan缩点+树上倍增都不会..还想了3h+.. 什么时候写了它吧...

  4. 为nginx增加nginx_http_concat模块

    为nginx增加nginx_http_concat模块 时间 2013-06-05 22:14:56  我行我思 原文  http://www.fanjun.me/?p=562 主题 Nginx 缘由 ...

  5. nginx 反向代理 google

    nginx的反向代理,google一直都是不容易打开的,如果你有一台位于国外的vps或者服务器,就可以轻松解决这个问题,这次的主角是nginx,nginx的反向代理现在已经发展很强大了,很多时候拿他来 ...

  6. 使用git如何批量对文件进行rm操作

    git add -A 它会把我们未通过 git rm 删除的文件全部stage 转自: http://segmentfault.com/q/1010000000095373

  7. 【云计算】Dockerfile示例模板

    Dockerfile FROM debian:jessie MAINTAINER "Konrad Kleine" USER root ####################### ...

  8. Datasets for Data Mining and Data Science

    https://github.com/mattbane/RecommenderSystem http://grouplens.org/datasets/movielens/ KDDCUP-2012官网 ...

  9. table动态添加删除一行和改变标题

    <style type="text/css"> body{ font-size:13px; line-height:25px; } table{ border-top: ...

  10. 使用kettle转换中的JavaScript对密码进行加密和解密

    日常开发中,为了确保账号和密码的安全,时常要对密码进行加密和解密.然而kettle是怎么对密码进行加密和解密的呢? 下面的代码需要再转换中的JavaScript中运行. var encrypted_p ...