HDU 3422, POJ 1305简单的本原勾股数组

来源:互联网 发布:网络监控软件哪个好用 编辑:程序博客网 时间:2024/05/21 06:31

Triangle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 722    Accepted Submission(s): 399


Problem Description
K likes to play with the balls.That day he piled a triangle (n layers, layer 1 start,the first layer has 1 ball,the secode layer has 2 balls,……,the nth layer has n balls), but he felt uncomfortable after completion, and want to pile a right triangle, because he felt that the number 4 is a lucky one (because of “si ji fa cai”). So he decided to use 4 times of the balls he just used as a right-edge side of the right triangle(the three edges have no common factor).He only to pile the three edges,that is the middle is empty.But there will not have so many balls,so he wants to know the minimum of balls he must use.Now ,he turn to you for help.
 

Input
The layer of the triangle as the promble describes n,1 <= n < 2^16
 

Output
The minimum of balls he must use and the length of the hypotenuse. One case one line
 

Sample Input
123
 

Sample Output
9 527 1353 25The second case:Let the right_edge promble describles is b ,The layer is 2,so b is 4 * (1 + 2) = 12.wo can know the edges of right triangle is 5 , 12 ,13.So the minimum of balls he must use is (5 + 12 + 13 – 3) = 27
 
题目意思是,本原勾股数组的一条边b = 2 * n * (n + 1), 输出 a + b + c - 3的最小值, 和 斜边c的值
脑残,还想着去枚举S 和 T。
根据本原勾股数组 a = s * t, b = (s * s - t * t) / 2, c = (s * s + t * t) / 2的特性我们知道,b边已经确定,要使周长最小,必须使 t 最小,才能使a = s * t, 和 c = (s * s + t * t) / 2最小。
而 t >= 1, 所以 t 必须取1 才能使 另外两边 a 和 c最小。
所以 s * s - t = 4 * n * (n + 1) , 能求出s (实际上不用求出s)。要输出的即为
4 * n * n + 6 * n - 1
2 * n * n + 2 * n + 1
这里特别注意一下, n < 2^16 , 当n = 2^16   - 1时, 上两式会超int
头一次做数论的题目。。。
一行代码就行了(没写贴吓死人的头文件)。。。
<span style="font-size:14px;">int n, m;int s,t;int main() {#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);//  freopen("Out.txt", "w", stdout);#endif    while(~sf(n)) {printf("%I64d %I64d\n", (4 * n * n + 6 * n - 1), (2 * n * n + 2 * n + 1));    }    return 0;}</span>



Fermat vs. Pythagoras
Time Limit: 2000MS
Memory Limit: 10000KTotal Submissions: 1549
Accepted: 903

Description

Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the four-color problem was completed with the assistance of a computer program and current efforts in verification have succeeded in verifying the translation of high-level code down to the chip level.
This problem deals with computing quantities relating to part of Fermat's Last Theorem: that there are no integer solutions of a^n + b^n = c^n for n > 2.
Given a positive integer N, you are to write a program that computes two quantities regarding the solution of x^2 + y^2 = z^2, where x, y, and z are constrained to be positive integers less than or equal to N. You are to compute the number of triples (x,y,z) such that x < y < z, and they are relatively prime, i.e., have no common divisor larger than 1. You are also to compute the number of values 0 < p <= N such that p is not part of any triple (not just relatively prime triples).

Input

The input consists of a sequence of positive integers, one per line. Each integer in the input file will be less than or equal to 1,000,000. Input is terminated by end-of-file

Output

For each integer N in the input file print two integers separated by a space. The first integer is the number of relatively prime triples (such that each component of the triple is <=N). The second number is the number of positive integers <=N that are not part of any triple whose components are all <=N. There should be one output line for each input line.

Sample Input

1025100

Sample Output

1 44 916 27


题意:要你求出小于等于N的本原勾股数组个数,和不涉及本原勾股数组的整数的个数(就是不属于任何一个勾股数组)。
还是用 S 和 T来生成本原勾股数组,(因为U 和 V生成的话需要限制条件,目前我还不知道。。。)
(a , b , c) = (s * t, (s^2 - t^2) / 2, (s^2 + t^2) /2), 这里千万注意!!!s > t >= 1   &&   gcd(s, t) == 1   !!!!!!
忽略了这个条件导致我第三组数据一直是 17 27,de了半天
思路:
枚举s , t, 记录 ,最后遍历一遍
代码:
/*************************************************************************    > File Name: 1305.cpp    > Author: Triose    > Mail: Triose@163.com     > Created Time: 2016年07月09日 星期六 14时45分33秒 ************************************************************************///#include<bits/stdc++.h>#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<algorithm>#include<vector>#include<queue>#include<stack>#include<iterator>#include<math.h>#include<stdlib.h>#include<time.h>#include<map>#include<set>using namespace std;//#define ONLINE_JUDGE#define eps 1e-8#define inf 0x3f3f3f3f#define INF 0x7fffffff#define INFL 0x3f3f3f3f3f3f3f3fLL#define enter putchar(10)#define rep(i,a,b) for(int i = (a); i < (b); ++i)#define repe(i,a,b) for(int i = (a); i <= (b); ++i)#define mem(a,b) (memset((a),b,sizeof(a)))#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sfs(a) scanf("%s",a)#define pf(a) printf("%d\n",a)#define pfd(a,b) printf("%d %d\n",a,b)#define pfP(a) printf("%d %d\n",a.fi,a.se)#define pfs(a) printf("%s\n",a)#define pfI(a) printf("%I64d\n",a)#define PR(a,b) pair<a,b>#define fi first#define se second#define LL long long#define DB doubleconst double PI = acos(-1.0);const double E = exp(1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }int n, m;#define N 1000010bool vis[N];void solve() {    mem(vis, false);    int ans1 = 0, ans2 = 0;    int s = 3, t = 1;    while(s * s + 1 <= 2 * n) {t = 1;while(t < s) {    if(s * s + t * t <= 2 * n && gcd(s, t) == 1) {int a = s * t, b = (s * s - t * t) / 2, c = (s * s + t * t) / 2;ans1++;for(int i = 1; i * c <= n; i++) {    vis[i * a] = true; vis[i * b] = true; vis[i * c] = true;}    }    t += 2;}s += 2;    }    repe(i, 1, n) {if(!vis[i]) ans2++;    }    pfd(ans1, ans2);}int main() {#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);//  freopen("Out.txt", "w", stdout);#endif    while(~sf(n)) {solve();    }    return 0;}


总结:(关于本原勾股数组)
一些结论:
1、a, b奇偶性不同,c总为奇数(一般设a为奇数b为偶数)
2,a^2 = (c + b) * (c - b),(c + b) 和 (c - b)都为平方数(无公约数且相乘为平方数)
两种描述勾股数组的方法:
一、 s > t >= 1 && gcd(s, t) == 1 , a = s * t, b = (s^2 - t^2) / 2, c = (s^2 + t^2) /2 可用来描述本原
二、(a,b,c) = (u^2 - v^2, u * v, u^2 + v^2)可用来描述所有(由单位圆导出)
0 0
原创粉丝点击