0% found this document useful (0 votes)
5 views

lab6 Q1.cpp

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

lab6 Q1.cpp

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <queue>
#include <unordered_map>
#include <cmath>
#include <vector>

struct Key {
int sum, i, j;
bool operator>(const Key& other) const {
return sum > other.sum;
}
};

using MinPQ = std::priority_queue<Key, std::vector<Key>, std::greater<Key>>;

int main() {
int n = 100;
MinPQ pq;
std::unordered_map<int, std::pair<int, int>> sum_map;
for (int i = 0; i <= n; ++i) {
pq.push(Key{ i*i*i + i*i*i, i, i });
}

while (!pq.empty()) {
Key curr = pq.top(); pq.pop();
if (sum_map.find(curr.sum) != sum_map.end()) {
auto previous_pair = sum_map[curr.sum];
std::cout << curr.sum << " = " << previous_pair.first << "^3 + "
<< previous_pair.second << "^3 = " << curr.i << "^3 + "
<< curr.j << "^3\n";
} else {
sum_map[curr.sum] = {curr.i, curr.j};
}
if (curr.j < n) {
pq.push(Key{ curr.i*curr.i*curr.i + (curr.j+1)*(curr.j+1)*(curr.j+1),
curr.i, curr.j+1 });
}
}

You might also like