Submission #68772020


Source Code Expand

#include <bits/stdc++.h>

// what the fuck
template<typename T, int N>
struct NDVector { using type = std::vector<typename NDVector<T, N - 1>::type>; };
template<typename T>
struct NDVector<T, 1> { using type = std::vector<T>; };

// A tensor is essentially a vector of tensors. (or multidimensional array)
template<typename T, int N>
using Tensor = typename NDVector<T, N>::type;

/**
 * Create a multidimensional vector with the given dimension sizes.
 *
 * In particular, create_vector(N) = create_tensor(N), create_matrix(N, M) = create_tensor(N, M).
 * If you have some weird multidimensional DP, you can create the DP table by doing:
 *      dp = create_tensor(5, 5, 5, 5, 5);
 *
 * Be careful, for a large number of dimensions, this uses a lot of memory and is very cache unfriendly.
 */
template<typename T>
std::vector<T> create_tensor(int N) {
    return std::vector<T>(N);
}
template <typename T, typename... ArgTypes>
Tensor<T, sizeof...(ArgTypes) + 1> create_tensor(int N, ArgTypes... args) {
    auto under = create_tensor<T>(args...);
    return std::vector(N, under);
}

/**
 * Create a matrix of the given dimensions.
 */
template<typename T>
Tensor<T, 2> create_matrix(int N, int M) {
    return create_tensor<T>(N, M);
}

/**
 * Frequently used definitions, like Vector, Matrices, pairs of ints, pairs, triples, etc.
 */
template<typename T>
using Vector = Tensor<T, 1>; // I could use std::vector<T>, but this is just too cool.
template<typename T>
using Matrix = Tensor<T, 2>;

template<typename T1, typename T2>
using Pair = std::pair<T1, T2>;
using PairII = Pair<int, int>;
using PairLL = Pair<long long, long long>;

template<typename T1, typename T2, typename T3>
using Triple = std::tuple<T1, T2, T3>;

/**
 * Read a vector from input. Set start to 1 if you want it to be 1-indexed.
 */
template<typename T>
Vector<T> read_vector(int N, int start = 0) {
    Vector<T> v(start + N);
    for (int i = start; i < (int)v.size(); i++) {
        std::cin >> v[i];
    }
    return v;
}

/**
 * Read a matrix from input. Set start_l to make lines 1-indexed. Same thing for start_c.
 */
template<typename T>
Matrix<T> read_matrix(int N, int M, int start_l = 0, int start_c = 0) {
    Matrix<T> matr = create_matrix<T>(N + start_l, M + start_c);

    for (int l = start_l; l < N + start_l; l++)
        for (int c = start_c; c < M + start_c; c++)
            std::cin >> matr[l][c];

    return matr;
}

/**
 * Print a tensor to the output stream. Prints all indices between i and j, and the elements 
 * are separated by the given separator.
 *
 * To generalize, for each dimension, you give the bounds that you want to print and the separator
 * between each order. To print a matrix, you would do:
 *
 *      print_tensor(matr, std::cout, 0, N - 1, "\n", 0, M - 1, " ");
 */
template<typename T>
void print_tensor(Tensor<T, 1>& tens, std::ostream&fout, int i, int j, const char* sep) {
    for (int t = std::max(i, 0); t <= j && t < (int)tens.size(); t++) {
        fout << tens[t];
        if (t + 1 <= j)
            fout << sep;
    }
}

template<typename T, typename... Sizes>
void print_tensor(
        Tensor<T, sizeof...(Sizes) / 3 + 1>& tens,
        std::ostream& fout, 
        int i, int j, const char* sep, Sizes... sizes) {
    for (int t = std::max(i, 0); t <= j && t < (int)tens.size(); t++) {
        print_tensor<T>(tens[t], fout, sizes...);
        if (t + 1 <= j)
            fout << sep;
    }
}

/**
 * Print a vector to the given output stream with given bounds and separator.
 */
template<typename T>
void print_vector(std::vector<T>& v, std::ostream& fout, int i = 0, int j = (1 << 30), const char* sep = " ") {
    print_tensor<T>(v, fout, i, j, sep);
}

/**
 * Read a vector of pairs. Set start to 1 if you want this to be 1-indexed.
 */
template<typename T1, typename T2>
Vector<Pair<T1, T2>> read_pairvec(int N, int start = 0) {
    Vector<Pair<T1, T2>> input = Vector<Pair<T1, T2>>(start + N);
    for (int i = start; i < start + N; i++)
        std::cin >> input[i].first >> input[i].second;
    return input;
}

/**
 * Read a vector of triples. Set start to 1 if you want this to be 1-indexed.
 *
 * If you need higher order tuples, like quadruples, you're better off using a matrix instead.
 */
template<typename T1, typename T2, typename T3>
Vector<Triple<T1, T2, T3>> read_triplevec(int N, int start = 0) {
    Vector<Triple<T1, T2, T3>> input = Vector<Triple<T1, T2, T3>>(start + N);
    for (int i = start; i < N + start; i++) {
        T1 a;
        T2 b;
        T3 c;
        std::cin >> a >> b >> c;
        input[i] = {a, b, c};
    }
    return input;
}

/**
 * Removes duplicates from vector. Assumes it is sorted.
 */
template<typename T>
void deduplicate(Vector<T>& v) {
    v.resize(std::unique(v.begin(), v.end()) - v.begin());    
}

/**
 * Solve a testcase of the problem. You will code your solution here instead of main.
 */
void solve_test();

/**
 * Call this function if you have a problem with multiple testcases.
 */
void multitest_problem() {
    int T;
    std::cin >> T;

    while (T--) solve_test();
}

int main() {
    std::cin.tie(NULL);
    std::iostream::sync_with_stdio(false);

    // Choose one of the following functions, depending on the problem type.
    solve_test();
    //multitest_problem();

    return 0;
}

void solve_test() {
    int64_t X;
    std::cin >> X;

    Vector<int64_t> sol;

    auto is_sqr = [&](int64_t X) {
        int64_t l = 0, r = 10000001;
        while (r - l > 1) {
            int64_t mid = (l + r) / 2;
            if (mid * mid <= X)
                l = mid;
            else
                r = mid;
        }

        return l * l == X;
    };

    if (X == 0) {
        sol.push_back(0);
        sol.push_back(-1);
    } else {
        bool swap = false;

        if (is_sqr(X)) {
            sol.push_back(0);
            sol.push_back(-1);
        }
        if (X < 0) {
            swap = true;
        }


        for (int k = 0; (int64_t)k * k <= std::abs(X); k++) {
            int64_t form = X - (int64_t)k * k;
            if (form % (2 * k + 1) != 0) continue;
            form = form / (2 * k + 1);
            form--;

            int64_t l = form, r = form + k;

            if (swap) {
                sol.push_back(l);
                sol.push_back(-l - 1);
            } else {
                sol.push_back(l);
                sol.push_back(-l - 1);
            }
        }
    }

    std::sort(sol.begin(), sol.end());
    deduplicate<int64_t>(sol);
    std::cout << sol.size() << "\n";
    print_vector(sol, std::cout);
    std::cout << std::endl;

    // for (auto it : sol) {
    //     int64_t val = (int64_t)it * it + it + X;
    //     if (!is_sqr((int64_t)it * it + it + X))
    //         std::cerr << "Bad solution: " << val << " " << it << "\n";
    // }
}


Submission Info

Submission Time
Task G - sqrt(n²+n+X)
User Tinca_Matei
Language C++ 23 (gcc 12.2)
Score 575
Code Size 7060 Byte
Status AC
Exec Time 30 ms
Memory 3616 KiB

Compile Error

Main.cpp: In function ‘void solve_test()’:
Main.cpp:223:31: warning: unused variable ‘r’ [-Wunused-variable]
  223 |             int64_t l = form, r = form + k;
      |                               ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 575 / 575
Status
AC × 3
AC × 63
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 01_handmade_11.txt, 01_handmade_12.txt, 01_handmade_13.txt, 01_handmade_14.txt, 01_handmade_15.txt, 01_handmade_16.txt, 01_handmade_17.txt, 01_handmade_18.txt, 01_handmade_19.txt, 01_handmade_20.txt, 01_handmade_21.txt, 01_handmade_22.txt, 01_handmade_23.txt, 01_handmade_24.txt, 01_handmade_25.txt, 01_handmade_26.txt, 01_handmade_27.txt, 01_handmade_28.txt, 01_handmade_29.txt, 01_handmade_30.txt, 01_handmade_31.txt, 01_handmade_32.txt, 01_handmade_33.txt, 01_handmade_34.txt, 01_handmade_35.txt, 01_handmade_36.txt, 01_handmade_37.txt, 01_handmade_38.txt, 01_handmade_39.txt, 01_handmade_40.txt, 01_handmade_41.txt, 01_handmade_42.txt, 01_handmade_43.txt, 01_handmade_44.txt, 01_handmade_45.txt, 01_handmade_46.txt, 01_handmade_47.txt, 01_handmade_48.txt, 01_handmade_49.txt, 02_radom_00.txt, 02_radom_01.txt, 02_radom_02.txt, 02_radom_03.txt, 02_radom_04.txt, 02_radom_05.txt, 02_radom_06.txt, 02_radom_07.txt, 02_radom_08.txt, 02_radom_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3420 KiB
00_sample_01.txt AC 1 ms 3384 KiB
00_sample_02.txt AC 1 ms 3356 KiB
01_handmade_00.txt AC 1 ms 3456 KiB
01_handmade_01.txt AC 1 ms 3428 KiB
01_handmade_02.txt AC 1 ms 3528 KiB
01_handmade_03.txt AC 1 ms 3456 KiB
01_handmade_04.txt AC 1 ms 3424 KiB
01_handmade_05.txt AC 1 ms 3464 KiB
01_handmade_06.txt AC 1 ms 3380 KiB
01_handmade_07.txt AC 1 ms 3472 KiB
01_handmade_08.txt AC 1 ms 3440 KiB
01_handmade_09.txt AC 1 ms 3412 KiB
01_handmade_10.txt AC 1 ms 3400 KiB
01_handmade_11.txt AC 1 ms 3464 KiB
01_handmade_12.txt AC 1 ms 3532 KiB
01_handmade_13.txt AC 1 ms 3416 KiB
01_handmade_14.txt AC 1 ms 3480 KiB
01_handmade_15.txt AC 1 ms 3360 KiB
01_handmade_16.txt AC 1 ms 3420 KiB
01_handmade_17.txt AC 1 ms 3528 KiB
01_handmade_18.txt AC 1 ms 3468 KiB
01_handmade_19.txt AC 1 ms 3456 KiB
01_handmade_20.txt AC 1 ms 3416 KiB
01_handmade_21.txt AC 30 ms 3408 KiB
01_handmade_22.txt AC 23 ms 3356 KiB
01_handmade_23.txt AC 24 ms 3492 KiB
01_handmade_24.txt AC 12 ms 3612 KiB
01_handmade_25.txt AC 16 ms 3424 KiB
01_handmade_26.txt AC 24 ms 3524 KiB
01_handmade_27.txt AC 30 ms 3520 KiB
01_handmade_28.txt AC 30 ms 3440 KiB
01_handmade_29.txt AC 29 ms 3436 KiB
01_handmade_30.txt AC 29 ms 3424 KiB
01_handmade_31.txt AC 30 ms 3388 KiB
01_handmade_32.txt AC 30 ms 3536 KiB
01_handmade_33.txt AC 30 ms 3528 KiB
01_handmade_34.txt AC 30 ms 3324 KiB
01_handmade_35.txt AC 30 ms 3304 KiB
01_handmade_36.txt AC 30 ms 3616 KiB
01_handmade_37.txt AC 30 ms 3420 KiB
01_handmade_38.txt AC 30 ms 3460 KiB
01_handmade_39.txt AC 30 ms 3428 KiB
01_handmade_40.txt AC 30 ms 3328 KiB
01_handmade_41.txt AC 30 ms 3344 KiB
01_handmade_42.txt AC 30 ms 3412 KiB
01_handmade_43.txt AC 30 ms 3420 KiB
01_handmade_44.txt AC 30 ms 3608 KiB
01_handmade_45.txt AC 30 ms 3520 KiB
01_handmade_46.txt AC 29 ms 3472 KiB
01_handmade_47.txt AC 29 ms 3416 KiB
01_handmade_48.txt AC 30 ms 3460 KiB
01_handmade_49.txt AC 26 ms 3584 KiB
02_radom_00.txt AC 25 ms 3512 KiB
02_radom_01.txt AC 22 ms 3468 KiB
02_radom_02.txt AC 12 ms 3420 KiB
02_radom_03.txt AC 28 ms 3608 KiB
02_radom_04.txt AC 28 ms 3424 KiB
02_radom_05.txt AC 22 ms 3472 KiB
02_radom_06.txt AC 19 ms 3420 KiB
02_radom_07.txt AC 15 ms 3528 KiB
02_radom_08.txt AC 22 ms 3428 KiB
02_radom_09.txt AC 26 ms 3460 KiB