FACPOW - Editorial

PROBLEM LINK: FACPOW Problem - CodeChef

Practice
Contest

Author: Shreyansh Singh
Tester: Shreyansh Singh
Editorialist: Shreyansh Singh

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Implementation, Math

PROBLEM:

Given two numbers, X and K, find the sum of Kth power of every factor of X and the sum of X times every factor of K.

QUICK EXPLANATION:

Iterate over a range from 2 to X and K successively, find their factors and maintain the two sums as the loop proceeds.

EXPLANATION:

Take the input in two different variables. Initialize two variables as zero for the required sums. Iterate over a range from 2 to X. Find the factors and keep adding the Kth power of the factors to one of the answer variables. Iterate over another range, from 2 to K. Find the factors and keep adding their Xth multiple to the other sum variable .
After both the iterations, print both the sum variables.

SOLUTIONS:

Setter's Solution

#include <bits/stdc++.h>

using namespace std;

int main() {
int t;
cin >> t;
while(t–) {
int x, k;
cin >> x >> k;
vector fact;
for(int i = 2; i <= x; ++i) {
if(x % i == 0)
fact.push_back(i);
}
int n = fact.size(), sumx = 0;
for(int i = 0; i < n; ++i) {
sumx += pow(fact[i], k);
}
int sumk = 0;
for(int i = 2; i <= k; ++i) {
if(k % i == 0)
sumk += x * i;
}
cout << sumx << " " << sumk << “\n”;
}
return 0;
}