1180 - Perfect Numbers
Posted: Thu Apr 03, 2014 3:00 pm
On this problem, note that the integers on the second line are separated by commas - and not spaces.
Code: Select all
p=num[i]-'0';
n=pow(2,p-1)*(pow(2,p)-1);
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);
..
}
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;
}
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;
}