Divisors and Reciprocals
Divisors and Reciprocals:
Alice is teaching Bob maths via a game called -guesser.
Alice has a positive integer which Bob needs to guess. She gives him two pieces of information with which to do this:
- A positive integer , which denotes the sum of divisors of .
- Two positive integers and , which denote that the sum of reciprocals of divisors of is .
Bob either needs to guess or tell that no such number exists.
It can be shown that if a valid exists, it is unique.
Input Format
- The first line of input contains a single integer , denoting the number of test cases. The description of test cases follows.
- Each test case consists of a single line of input, containing three space-separated integers .
Output Format
For each test case, output a new line containing the answer — Alice's number , or if no such number exists.
Constraints
Sample Input 1
2
4 4 3
4 1 1
Sample Output 1
3
-1
Explanation
Test case : The divisors of are and . Their sum is and the sum of their reciprocals is .
Test case : It can be proved that no positive integer exists whose divisors sum to and reciprocals of divisors sum to .
SOLUTION:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int tt=1;cin>>tt;
while(tt--)
{
ll x,a,b;cin>>x>>a>>b;
if(x%a==0)
{
ll p=x/a;
ll den=b*p;
ll sum=0;
for(int i=1;i<=sqrtl(den);i++)
{
if(sum>x)
break;
if(den%i==0)
{
sum=sum+i;
if(den/i!=i)
sum=sum+den/i;
}
}
if(sum!=x)
{
cout<<-1<<"\n";
}
else
cout<<den<<"\n";
}
else
cout<<-1<<"\n";
}
}
Comments
Post a Comment