1180 - Perfect Numbers
Moderator: Board moderators
-
- Experienced poster
- Posts: 148
- Joined: Sun Jul 13, 2014 4:32 am
- Location: Rangpur, Bangladesh
Re: 1180 - Perfect Numbers
Code: Select all
cut
Last edited by Shahidul.CSE on Mon Dec 08, 2014 6:18 pm, edited 2 times in total.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Re: 1180 - Perfect Numbers
Why do you think that p is one digit number? It can be greater than 9.
This problem is tricky. I checked that p can be greater than 1000.
So you can forget about checking if sum of proper divisors are equal to a number.
And you don't need to check it.
Think about this condition
Code: Select all
p=num[i]-'0';
n=pow(2,p-1)*(pow(2,p)-1);
So you can forget about checking if sum of proper divisors are equal to a number.
And you don't need to check it.
Think about this condition
And this conditionEuclid proved that an even number is perfect if it has the form
2^(p-1) * (2^p - 1)
where both p and 2^p - 1 are prime numbers.
By the way i read p this wayThe largest perfect number in this problem will not exceed 2^33
Code: Select all
scanf("%d", &t);
while (t--) {
scanf("%d", &p);
if (t) scanf(" %c", &c);
..
}
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- Experienced poster
- Posts: 148
- Joined: Sun Jul 13, 2014 4:32 am
- Location: Rangpur, Bangladesh
Re: 1180 - Perfect Numbers
Code: Select all
cut
Last edited by Shahidul.CSE on Mon Dec 08, 2014 6:17 pm, edited 1 time in total.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Re: 1180 - Perfect Numbers
If max value of perfect number is 2^33.
Then 2^(p-1) * (2^p - 1) <= 2^33. Can you get max value for p?
If given p is greater than max value i print "No" because they will be not perfect numbers according to problem description.
Then 2^(p-1) * (2^p - 1) <= 2^33. Can you get max value for p?

If given p is greater than max value i print "No" because they will be not perfect numbers according to problem description.

A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- Experienced poster
- Posts: 148
- Joined: Sun Jul 13, 2014 4:32 am
- Location: Rangpur, Bangladesh
Re: 1180 - Perfect Numbers
Code: Select all
cut
Last edited by Shahidul.CSE on Mon Dec 08, 2014 6:17 pm, edited 1 time in total.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Re: 1180 - Perfect Numbers
Your prime checking is wrong.
When you solve problems relating to prime numbers -> Add a condition that 1 is not a prime.
(if problem description didn't said anything about it)
It would be better if you make function isPrime(int n) that checks if number is prime.
When you solve problems relating to prime numbers -> Add a condition that 1 is not a prime.

(if problem description didn't said anything about it)
It would be better if you make function isPrime(int n) that checks if number is prime.

A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- New poster
- Posts: 15
- Joined: Tue Oct 21, 2014 4:08 pm
- Location: Bangladesh
- Contact:
Re: 1180 - Perfect Numbers
getting TL
any suggestion ?
here is my code
any suggestion ?
here is my code
Code: Select all
#include<stdio.h>
#include<math.h>
int main()
{
unsigned long long int n,i,j,p,sum,t;
//freopen("1180.txt","r",stdin);
while(scanf("%llu",&t)==1)
{
for(i=0;i<t;i++)
{
scanf("%llu",&p);
if(i<t-1)
scanf(",");
sum=0,n=0;
n=pow((double)2,p-1)*(pow((double)2,p)-1);
for(j=1;j<n;j++)
{
if(n%j==0)
sum+=j;
}
if(n==sum)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
Last edited by Helaluddin_brur on Thu Aug 13, 2015 7:57 pm, edited 3 times in total.
-
- Experienced poster
- Posts: 148
- Joined: Sun Jul 13, 2014 4:32 am
- Location: Rangpur, Bangladesh
Re: 1180 - Perfect Numbers
Helaluddin_brur,
According to your code, you should print Yes, when both f and f1 is 0.
According to your code, you should print Yes, when both f and f1 is 0.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
Re: 1180 - Perfect Numbers
Doesn't match sample I/O. Input numbers are separated by commas(','). You should read this commas from standart input.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 1180 - Perfect Numbers
Why I am getting TLE ?? Please Help ....
Code: Select all
#include<stdio.h>
#include<math.h>
int main()
{
long long int n, i, p, a[100000], j, d, sum;
char ch;
while(scanf("%lld",&n)==1)
{
for(i=1;i<=n;i++)
{
scanf("%lld,",&a[i]);
scanf("%c",&ch);
}
for(i=1;i<=n;i++)
{
d = (pow(2,a[i]-1))*(pow(2,a[i])-1);
sum=0;
for(j=1;j<d;j++)
{
if((d%j)==0)
sum = sum+j;
}
if(sum==d)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
I know I am a Failure Guy . 

-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 1180 - Perfect Numbers
Check input and AC output for thousands of problems on uDebug!
Re: 1180 - Perfect Numbers
ssavi, if you want to optimize your code read posts in this thread.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman