1487D - Pythagorean Triples - CodeForces Solution


binary search brute force math number theory *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")

#define ll long long
#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

const ll N = (ll)2e9 + 5, M = 500000;
const double PI = acos(-1);
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
const long long MOD = 1e9 + 7, MOD2 = 998244353, OO = 1e18;

ll t = 1;

void solve()
{
    ll n;
    cin >> n;
    if (n < 5)
    {
        return void(cout << "0\n");
    }
    ll l = 2, r = (n + 1) / 2;
    while (l < r)
    {
        ll md = (l + r + 1) >> 1;
        ll a = 2 * md - 1;
        ll b = a * a / 2;
        ll c = a * a - b;
        if (a <= b && b <= c && c <= n)l = md;
        else r = md - 1;
    }
    cout << l - 1 << '\n';
    return;
}

int main()
{    
    IO

    cin >> t;
    while (t--)
    {
        solve();
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It