Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <set>
- #include <map>
- using namespace std;
- vector<int> generate_primes(int range, bool* seive) {
- fill(seive, seive + range, true);
- for (int i = 2; i < sqrt(range); i++) {
- if (seive[i] == true) {
- for (int j = i * 2; j < range; j += i) {
- seive[j] = false;
- }
- }
- }
- vector<int> result;
- for (int i = 2; i < range; i++) {
- if (seive[i]) {
- result.push_back(i);
- }
- }
- return result;
- }
- int gcd(int a, int b) {
- return b == 0 ? a : gcd(b, a % b);
- }
- int main() {
- int a0 = 2, a1 = 100, b0 = 2, b1 = 100;
- bool seive[a1 + 1]; // seive[x] is true for prime x
- vector<int> primes = generate_primes(a1 + 1, seive);
- set<pair<int, int>> values; // pairs of <a, b> that give unique a^b
- for (int a = a0; a <= a1; a++) {
- int _a = a;
- map<int, int> primes_count; // key: prime multiplier of a, value: count of such multipliers
- while (!seive[_a]) {
- for (size_t i = 0; i < primes.size(); i++) {
- if (_a % primes[i] == 0) {
- primes_count[primes[i]]++;
- _a /= primes[i];
- break;
- }
- }
- }
- primes_count[_a]++;
- // find max power to represent a:
- int to_power = primes_count.begin()->second;
- for (auto it = primes_count.begin()++; it != primes_count.end(); ++it) {
- to_power = gcd(to_power, it->second);
- }
- // find base for such power:
- int base = 1;
- for (auto it = primes_count.begin(); it != primes_count.end(); ++it) {
- base *= pow(it->first, (it->second) / to_power);
- }
- for (int b = b0; b <= b1; b++) {
- values.insert(make_pair(base, to_power * b));
- }
- }
- cout << values.size() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement