zoukankan      html  css  js  c++  java
  • 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>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    
    void work ()
    {
        LL n;
        cin>>n;
        if (n==1 || n==2 )
        {
            cout<<-1<<endl;
            return ;
        }
        LL t = n*n;
        if (t&1)
        {
            LL k = (n*n+1)>>1;
            cout<<k<<" "<<k-1<<endl;
        }
        else
        {
            LL k = t>>2;
            cout<<k+1<<" "<<k-1<<endl;
        }
    }
    int main()
    {
    #ifdef local
        freopen("data.txt","r",stdin);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    python中字母的大小写转换
    十进制转换为16进制
    查找数组中出现次数超过一半的数
    leetcode二分查找
    leetcode 3 字符串
    leetcode链表篇
    leetcode数组篇
    重构二叉树
    矩阵的特征向量和特征值
    微软编程
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/5797052.html
Copyright © 2011-2022 走看看