codeforces #Round 368 div2-C(勾股数)
来源:互联网 发布:软件渠道代理 编辑:程序博客网 时间:2024/06/05 16:57
题目链接:http://codeforces.com/contest/707/problem/C
题意:给出一个数,输出两个数,这三个数能构成勾股数。
思路:先观察平方数1,4,9,16,25,36,49.......
显然n==1(无法构成三角形)或n==2(三边2,3,4不能构成直角三角形)无解。
数列相邻两项之差( n + 1 ) ^ 2 - n ^ 2 = 2n + 1为奇数,故:
当输入a为奇数时,令√(2n+1) = x => b = n = (x ^ 2 - 1) / 2,c = n + 1 = ( x ^ 2 + 1 ) / 2 ;
数列每隔两项的差( n + 2 ) ^ 2 - n ^ 2 = 4 ( n + 1 )为偶数,故:
当输入a为偶数时,令2√(n+1) = x => b = n = (x ^ 2 - 4) / 4 , c = n + 2 = (x ^ 2 + 4) / 4。
#include<bits/stdc++.h>using namespace std;typedef long long ll;int main(){ ll n; scanf("%I64d",&n); if(n<3) { printf("-1\n"); return 0; } if(n&1) { printf("%I64d %I64d\n",(n * n - 1)>>1,(n * n + 1) >> 1); return 0; } printf("%I64d %I64d\n",(n * n - 4) >> 2,(n * n + 4) >> 2); return 0;}
0 0
- codeforces #Round 368 div2-C(勾股数)
- Codeforces round 181 div2 C
- codeforces div2 round#231 C
- codeforces div2 round#230 C
- Codeforces - Round.313.Div2.C
- codeforces Round #352 div2 C
- Codeforces Round #409 div2 C
- Codeforces round#429 div2 C
- Codeforces Round #429 (Div2) C
- Codeforces Round #326 (div2)
- Codeforces Round #440(div2)
- Codeforces Round #263(div2)C. Appleman and Toastman
- Codeforces Round #265(div2) C. No to Palindromes!
- C. Writing Code(Codeforces Round #302(div2)
- codeforces Round #377 div2 A B C(数学推导)
- codeforces Round #237(div2) C解题报告
- codeforces Round #241(div2) C解题报告
- codeforces round #257 div2 C、D
- 主成分分析PCA
- Java在linux下抓取12306车票信息
- pat1098. Insertion or Heap Sort
- Kubernetes 容器之rkt
- 进程间的通信方式之无名管道
- codeforces #Round 368 div2-C(勾股数)
- caffe.proto 源码解析
- epoll简单注释
- mysql中间件研究(Atlas,cobar,TDDL、sharding-jdbc)
- JavaScript toFixed() 方法
- 利用 LeakCanary 来检查 Android 内存泄漏
- HDOJ 1102 Constructing Roads(最小生成树)
- protobuf,thrift,avro之序列化性能测试
- Qt之QProgressBar