386 - Perfect Cubes

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

386 so simple BUT...

Post by deddy one »

my program gives me 216 lines of output and
it gives me WA

how many output lines it should be ?????
the sorting method is based ascending value of
"b" right??

Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

Post by Jalal »

Cheak ur inner loops!
yes the sorting method is based ascending value of
"b"

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one »

so, how many lines of output are there in this problem??

I try to use double data type to guard against miscalculation
and it gives me 216 lines.

thanks.

angga888
Experienced poster
Posts: 143
Joined: Sat Dec 21, 2002 11:41 am
Location: Indonesia

Post by angga888 »

My output gives 223 lines.
I just use long, not double.
Try to check your loop again. You should get AC.

Good Luck :D

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one »

thx guys
got AC now

I made stupid mistake in my loop :oops: :oops: :oops:

using long data type making the calculation faster too.
:D :D :D

the900
New poster
Posts: 3
Joined: Tue Apr 29, 2003 5:03 pm

386 - keep getting wrong answer

Post by the900 »

My program has 223 lines of output, and the first ones are exactly the same as in the problem set, but I keep getting wrong answer. Does anyone know what could be wrong?

szymcio2001
New poster
Posts: 38
Joined: Mon Dec 09, 2002 1:53 pm
Location: Poznan, Poland

Re: 386 so simple BUT...

Post by szymcio2001 »

Rong wrote:Why does give 222 lines in my output ?
I compiled it and it printed 223 line on my computer.
Rong wrote: [cpp]
for(d=c;d<a;d++)
{
if(i==(pow(b,3)+pow(c,3)+pow(d,3)))
{
printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d);
break;
}
}
}[/cpp]
This isn't verry efficient. You should check using math functions if exists a such integer d that a^3-b^3-c^3=d^3

the900
New poster
Posts: 3
Joined: Tue Apr 29, 2003 5:03 pm

Post by the900 »

I get 223 lines of output but the judge tells me WA. Here's the code, could you help me

szymcio2001
New poster
Posts: 38
Joined: Mon Dec 09, 2002 1:53 pm
Location: Poznan, Poland

Post by szymcio2001 »

[quote="the900"]I get 223 lines of output but the judge tells me WA. Here's the code, could you help me

aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

386 - Perfect Cubes

Post by aakash_mandhar »

What is wrong with this code plz help.. i thought this was a cool logic but got wa....

It seems to work and gives me 223 answers.. Plz help...
Plzz help

[cpp]

# include<iostream.h>

long a;
long init,temp;
int i,j1,j2,j3;
long cube[201];
long sol[300][3];
long sc;


int find(int x,int y,int z)
{
for(int i=0;i<sc;i++)
if((sol[0]==x && sol[1]==y && sol[2]==z) || (sol[0]==x && sol[2]==y && sol[1]==z)|| (sol[2]==x && sol[1]==y && sol[0]==z) || (sol[2]==x && sol[i][0]==y && sol[i][1]==z)|| (sol[i][1]==x && sol[i][2]==y && sol[i][0]==z) || (sol[i][1]==x && sol[i][0]==y && sol[i][2]==z)) return 1;
return 0;
}

int main()
{
sc=0;
a=1;
init=7;
temp=6;
for(i=1;i<=200;i++)
{
cube[i]=a;
//cout<<a<<",\n";
a+=init;
temp+=6;
init+=temp;
}

for(i=6;i<=200;i++)
{
//cout<<cube[i]<<"\n";

for(j1=2;j1<=i-3;j1++)
{
for(j2=j1+1;j2<=i-2;j2++)
{
for(j3=j2+1;j3<=i-1;j3++)
{
if(cube[i]==cube[j1]+cube[j2]+cube[j3])
{
if(find(j1,j2,j3)==0)
{
sol[sc][0]=j1;sol[sc][1]=j2;sol[sc][2]=j3;sc++;
cout<<"Cube = "<<i<<", Triple = ("<<j1<<","<<j2<<","<<j3<<")\n";
}
}

}

}

}
}

return 1;
}

//@end_of_source_code

[/cpp]
Last edited by aakash_mandhar on Mon Jan 05, 2004 12:48 am, edited 1 time in total.
...I was born to code...

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Nothing is wrong.

Post by sohel »

I have gone through your code and found no mistakes in it. So I decided to submit it and was not surprised to see 'accepted' as the reply. It means yours was a cool logic.

aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

AC

Post by aakash_mandhar »

Thx for the help..

I have removed my id from the post..
Thx again

Glad you liked the logic. :)
...I was born to code...

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

WA 386

Post by WR »

Hi,

using brute force (or ''complete enumeration'' as I prefer to call it)
I get 223 lines of output - but a WA.

Summarizing the rules:

1) sort by non-descending a
2) b <= c <= d
3) if there's more than one triple for a specific a,
sort by the value of b (ascending)

Example:

Cube = 150, Triple = (24,102,132)
Cube = 150, Triple = (71,73,138)
Cube = 150, Triple = (75,100,125)
Cube = 150, Triple = (76,86,132)

So what's my mistake?!

Any hints?

pavelph
Learning poster
Posts: 57
Joined: Wed Dec 10, 2003 7:32 pm
Location: Russia, Saint-Petersburg

Post by pavelph »

First outputs for this problem:
[pascal]program acm386; {a^3=b^3+c^3+d^3}
begin
writeln('Cube = 6, Triple = (3,4,5)');
writeln('Cube = 12, Triple = (6,8,10)');
writeln('Cube = 18, Triple = (2,12,16)');
writeln('Cube = 18, Triple = (9,12,15)');
writeln('Cube = 19, Triple = (3,10,18)');
writeln('Cube = 20, Triple = (7,14,17)');
writeln('Cube = 24, Triple = (12,16,20)');
writeln('Cube = 25, Triple = (4,17,22)');
writeln('Cube = 27, Triple = (3,18,24)');
writeln('Cube = 28, Triple = (18,19,21)');
writeln('Cube = 29, Triple = (11,15,27)');
writeln('Cube = 30, Triple = (15,20,25)');
writeln('Cube = 36, Triple = (4,24,32)');
writeln('Cube = 36, Triple = (18,24,30)');
[/pascal]
I hope it helps you.

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR »

Thanks for the post pavelph,

but it doesn't help because my output is exactly the same.

Code: Select all

#include <stdio.h>         /* printf */

/*****************************
 * ACM Contest - Problem 386       *
 *****************************/

int main(void)
{
  const int max = 200;
  long a, b, c, d;
  long a3, b3, c3, d3;

  for (a=2;a<=max;a++){
    a3 = a*a*a;
    for (b=2;b<=a;b++){
      b3 = b*b*b;
      for (c=b;c<=a;c++){
        c3 = c*c*c;
        for (d=c;d<=a;d++){
          d3 = d*d*d;
          if (b3+c3+d3 == a3)
            printf("Cube = %ld, Triple = (%ld,%ld,%ld)\n",a,b,c,d);
        }
      }
    }
  }
  return 0;
}

Post Reply

Return to “Volume 3 (300-399)”