#46213: c++ ans


yp11351205@yphs.tp.edu.tw (809-36黃泊霖)

學校 : 臺北市私立延平高級中學
編號 : 276130
來源 : [203.72.178.1]
最後登入時間 :
2025-07-16 13:08:26

#include <iostream>
#include <vector>
#include <map>
#include <cmath>      // For pow function
#include <functional> // <--- ADD THIS LINE

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

    int n;
    std::cin >> n;

    int M;
    std::cin >> M;

    std::vector<long long> k(n);
    std::vector<int> p(n);

    for (int i = 0; i < n; ++i) {
        std::cin >> k[i] >> p[i];
    }

    std::map<long long, int> sums_first_half;
    long long total_solutions = 0;

 
    int n1 = n / 2;


    std::function<void(int, long long)> generate_sums_1 =
        [&](int index, long long current_sum) {
        if (index == n1) {
            sums_first_half[current_sum]++;
            return;
        }

        for (long long x = 1; x <= M; ++x) {
            
            long long term = k[index] * static_cast<long long>(std::pow(x, p[index]));
            generate_sums_1(index + 1, current_sum + term);
        }
    };

    generate_sums_1(0, 0); 

    // Generate sums for the second half and find solutions
    std::function<void(int, long long)> generate_sums_2 =
        [&](int index, long long current_sum) {
        if (index == n) {
            // We are looking for current_sum + sum_first_half = 0
            // So, sum_first_half = -current_sum
            if (sums_first_half.count(-current_sum)) {
                total_solutions += sums_first_half[-current_sum];
            }
            return;
        }

        for (long long x = 1; x <= M; ++x) {
            long long term = k[index] * static_cast<long long>(std::pow(x, p[index]));
            generate_sums_2(index + 1, current_sum + term);
        }
    };

    generate_sums_2(n1, 0);

    std::cout << total_solutions << std::endl;

    return 0;
}