Monday, 22 June 2015

Chef wants to hire a new assistant. He published an advertisement regarding that in a newspaper. After seeing the advertisement, many candidates have applied for the job. Now chef wants to shortlist people for the interviews, so he gave all of them one problem which they must solve in order to get shortlisted.
The problem was : For a given positive integer N, what is the maximum sum of distinct numbers such that the Least Common Multiple of all these numbers is N.
Your friend Rupsa also applied for the job, but was unable to solve this problem and hence you've decided to help her out by writing a code for solving this problem.

Input

The first line of the input contains an integer T denoting the number of test cases.
Each test case contains a single integer N.

Output

For each test case, output a single line containing an integer corresponding to the answer for that test case.

Constraints

  • 1 ≤ T ≤ 1000
  • 1 ≤ N ≤ 109
Subtask 1 (30 points):
  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 105
Subtask 2 (70 points):
  • original constraints

Example

Input:
2
1
2

Output:
1
3

Explanation

Example 1 : Only possible number is 1, so the maximum sum of distinct numbers is exactly 1
Example 2 : The distinct numbers you can have are just 1 and 2, so the sum is 3. If we consider any other number greater than 2, then the least common multiple will be more than 2.

We can observe that for each pair of divisors (pq), such that p * q = N, either p <= sqrt(N) or q <= sqrt(N), else p * qwill be greater than N. Also we can check that for each divisor p, there exists a distinct q such that p * q = N.
Without loss of generality let us assume p <= q. We can iterate for each p from 1 to sqrt(N) and if p is a divisor of N, then add both p and N / p to the answer. Complexity is O(sqrt(N)) per test case.
C++ Code
#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i = 1; i <= t; i++){
        int n, p;
        long long sum = 0;
        cin >> n;
        for(p = 1; p * p <= n; p++){
            if(n % p == 0){
                sum += p;
                if(p != n / p){
                    sum += n / p;
                }
            }
        }
        cout << sum << '\n';
    }
    return 0;
}
Common Mistakes:
  1. We should check that p is not equal to N / p while adding N / p.
  2. The answer can exceed the range of integer in C++, so it should be kept as long long.


No comments:

Post a Comment