【数学】Codeforces 707C Pythagorean Triples

题目链接:

  http://codeforces.com/problemset/problem/707/C

题目大意:

  给你一个数,构造其余两个勾股数。任意一组答案即可,没法构造输出-1.

  答案long long 范围。

题目思路:

  【数学】

  这里贴一下勾股数的构造:

  当a为大于1的奇数2n+1时,b=2n2+2n, c=2n2+2n+1。

  实际上就是把a的平方数拆成两个连续自然数,例如:

  n=1时(a,b,c)=(3,4,5)

  n=2时(a,b,c)=(5,12,13)

  n=3时(a,b,c)=(7,24,25)

  当a为大于2的偶数2n时,b=n2-1, c=n2+1

  也就是把a的一半的平方分别减1和加1,例如:

  n=2时(a,b,c)=(4,3,5)

  n=3时(a,b,c)=(6,8,10)

  n=4时(a,b,c)=(8,15,17)

  n=5时(a,b,c)=(10,24,26)

  n=6时(a,b,c)=(12,35,37)

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 100004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 LL a,b,c;
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37 //    freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j,k;
41     int x,y,z;
42 //    for(scanf("%d",&cas);cas;cas--)
43 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
44 //    while(~scanf("%s",s+1))
45     while(~scanf("%I64d",&a))
46     {
47         if(a<3)puts("-1");
48         else if(a%2)
49         {
50             a=a/2;
51             b=2*sqr(a)+2*a;
52             c=2*sqr(a)+2*a+1;
53             printf("%I64d %I64d\n",b,c);
54         }
55         else
56         {
57             a/=2;
58             b=sqr(a)-1;
59             c=sqr(a)+1;
60             printf("%I64d %I64d\n",b,c);
61         }
62     }
63     return 0;
64 }
65 /*
66 //
67
68 //
69 */

时间: 2024-11-09 22:39:10

【数学】Codeforces 707C Pythagorean Triples的相关文章

Codeforces 707C. Pythagorean Triples

题目链接:http://codeforces.com/problemset/problem/707/C 题意: 给你直角三角形其中一条边的长度,让你输出另外两条边的长度. 思路: 直接构造勾股数即可,构造勾股数的方法: 当 a 为大于 1 的奇数 2 * n+1 时, b = 2 * n * n + 2 * n, c = 2 * n * n + 2 * n + 1. 当 a 为大于 4 的偶数 2 * n 时,b = n * n - 1, c = n * n + 1. 然后不满足上述构造方法的数

CodeForces 707C Pythagorean Triples (数论)

题意:给定一个数n,问你其他两边,能够组成直角三角形. 析:这是一个数论题. 如果 n 是奇数,那么那两边就是 (n*n-1)/2 和 (n*n+1)/2. 如果 n 是偶数,那么那两边就是 (n/2*n/2-1) 和 (n/2*n/2+1).那么剩下的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #

Codeforces Round #368 (Div. 2) Pythagorean Triples

Pythagorean Triples Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths

Codeforces 707 C. Pythagorean Triples(找规律)——Codeforces Round #368 (Div. 2)

传送门 Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding t

codeforces707C:Pythagorean Triples

Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresp

Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

给定一个直角三角形的一边长度.问是否存在一个直角三角形,使得它满足有一边的长度是x 当x=1.2的时候是无解的,可以暴力打表看看. 注意到,相邻的两个数的平方的差值是奇数 x^2 - (x-1)^2 = 2*x-1 间隔为2的两个数的平方的差值是偶数 (x+1)^2 - (x-1)^2 = 4*x 这样就可以分类讨论了. 把n永远当成是指角边就OK #include <cstdio> #include <cstdlib> #include <cstring> #incl

【Codeforces 707C】Pythagorean Triples(找规律)

一边长为a的直角三角形,a^2=c^2-b^2.可以发现1.4.9.16.25依次差3.5.7.9...,所以任何一条长度为奇数的边a,a^2还是奇数,那么c=a^2/2,b=c+1.我们还可以发现,1.4.9.16.25.36各项差为8.12.16.20,偶数的平方是4的倍数,那么c=a^2/4-1,b=a^2/4+1. #include <iostream> using namespace std; int main() { long long n; cin>>n; n*=n;

Pythagorean Triples CodeForces - 707C 推理题,大水题

给定一个数n(1 <= n <= 1e9),判断这个数是否是一个直角三角形的边长,如果是,则输出另外两条边(1 <= x <= 1e18),否则输出-1. 参考题解:http://blog.csdn.net/harlow_cheng/article/details/69055614 首先,当n <= 2 的时候无解,其他时候都有解 假设n是直角边,a是斜边,则n^2 + b^2 = a^2; n^2 = (a + b)*(a - b); ①假设n是偶数,则另(a - b) =

贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

题目传送门 1 /* 2 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 const int MAXN = 11; 10 const int INF = 0x3f3f3f3f; 11 char s[MAXN][MAXN]; 12 1