All about problems in Volume 111. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
DP
Learning poster
Posts: 62 Joined: Sun Aug 13, 2006 9:15 am
Location: Bangladesh
Contact:
Post
by DP » Thu Oct 26, 2006 7:01 am
Could someone check my input/output? Given below:
input:
Code: Select all
11
33
44
99
100
300
200
499
599
450
789
1000
2000
2165
3000
1111
477
7
49
679
output:
Code: Select all
2
6
9
55
35
321
141
777
1028
658
1522
2146
5638
6268
9794
2484
728
1
10
1225
all are right?
thnx
luishhh
New poster
Posts: 26 Joined: Mon Oct 25, 2004 8:11 pm
Location: Spain
Post
by luishhh » Thu Oct 26, 2006 1:23 pm
no, in fact there are only 5 results ok
does your code output the right answer for the sample input?
"From lost to the river" --> Spanish quote
DP
Learning poster
Posts: 62 Joined: Sun Aug 13, 2006 9:15 am
Location: Bangladesh
Contact:
Post
by DP » Thu Oct 26, 2006 9:17 pm
Thanks
Now, i got it how to solve.
coolguy
New poster
Posts: 30 Joined: Tue Oct 17, 2006 5:59 pm
Post
by coolguy » Sat Oct 28, 2006 6:34 pm
i guess this problem is about normal coin change problem . the algo i used is based on dp . but even the sample i/o doesnt match . i dont know wats da problem . could some one check please . the main portion of my code is as follows :
Code: Select all
val[0]=0;
for(i=1;i<=21;++i){
val[i]=i*i*i;
}
for(j=0;j<=10000;++j)memo[i]=1;
for( j = 1 ; j <= 21 ; ++j ){
for(i=val[j];i<10001;++i){
memo[i] += memo[i-val[j]];
}
}
am i doing the right thing ? please help me ...........
bye bye
In good company
asif_rahman0
Experienced poster
Posts: 209 Joined: Sun Jan 16, 2005 6:22 pm
Post
by asif_rahman0 » Sat Oct 28, 2006 7:51 pm
i did it recursively. It seems easy to me.
But my program took too much time(0.x4.xx) where others took 0.00.04/06.
Can someone tell me is there any other method without DP?
Last edited by
asif_rahman0 on Sat Oct 28, 2006 8:14 pm, edited 1 time in total.
coolguy
New poster
Posts: 30 Joined: Tue Oct 17, 2006 5:59 pm
Post
by coolguy » Sat Oct 28, 2006 8:01 pm
that was a typo
Code: Select all
val[0]=0;
for(i=1;i<=21;++i){
val[i]=i*i*i;
}
for(j=0;j<=10000;++j)memo[j]=1;
for( j = 1 ; j <= 21 ; ++j ){
for(i=val[j];i<10001;++i){
memo[i] += memo[i-val[j]];
}
}
this version is right . but still it doesnt work . i dont know y it doesnt work . please help ...
bye bye
In good company
nymo
Experienced poster
Posts: 149 Joined: Sun Jun 01, 2003 8:58 am
Location: :)
Post
by nymo » Sat Oct 28, 2006 8:20 pm
to coolguy, you need not use the following line:
basically, you just need to init your memo array so that it works correctly for the upcoming code fragment.
hope it works.
regards,
nymo
asif_rahman0
Experienced poster
Posts: 209 Joined: Sun Jan 16, 2005 6:22 pm
Post
by asif_rahman0 » Sat Oct 28, 2006 8:38 pm
nymo also right.
But in your own code you need.....
coolguy wrote: that was a typo
Code: Select all
val[0]=0;
for(i=1;i<=21;++i){
val[i]=i*i*i;
}
for(j=0;j<=10000;++j)memo[j]=1;
for( j = 1 ; j <= 21 ; ++j ){
for(i=val[j];i<10001;++i){
memo[i] += memo[i-val[j]];
}
}
i dont think u got your code. right??
just change j=2 instead of j=1. Because you already initialize memo[] with 1. Then again you do. So what is happing?? TRY to think then give it to board!
hope it helps
coolguy
New poster
Posts: 30 Joined: Tue Oct 17, 2006 5:59 pm
Post
by coolguy » Sat Oct 28, 2006 8:38 pm
to nymo :
Thanx man it works
bye bye
In good company
annhy
New poster
Posts: 40 Joined: Sun May 27, 2007 1:42 am
Location: Taiwan
Post
by annhy » Tue Jul 20, 2010 10:03 am
DP wrote: Could someone check my input/output? Given below:
input:
Code: Select all
11
33
44
99
100
300
200
499
599
450
789
1000
2000
2165
3000
1111
477
7
49
679
output:
Code: Select all
2
6
9
55
35
321
141
777
1028
658
1522
2146
5638
6268
9794
2484
728
1
10
1225
all are right?
thnx
My AC program outputs:
Code: Select all
2
6
9
39
39
683
208
3959
8015
2702
25294
73945
2789209
4446082
34188840
122148
3346
1
10
13349
Hope it helps.
hduong
New poster
Posts: 1 Joined: Fri Aug 05, 2011 8:36 am
Post
by hduong » Fri Aug 05, 2011 8:40 am
I always got Runtime Error although I already tested every case from 1 to 9999. Could anyone help me figure out the error in my code?
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
String line = "";
memo = new long[10030][30];
clearMemo();
while((line=f.readLine())!=null) {
if(line.length()==0) continue;
money = Integer.valueOf(line.trim());
System.out.println(change(money,21));
}
}
public static void clearMemo() {
for(int i=0; i<memo.length; i++) {
for(int j=0; j<memo[i].length; j++) {
memo[i][j] = -1;
}
}
}
public static long change(int value, int m) {
if(value<0) {
return 0;
}
if(value==0) {
return 1;
}
if(m<1 && value>=1) {
return 0;
}
if(memo[value][m]!=-1) return memo[value][m];
int coinValue = m*m*m;
long ans = change(value,m-1)+change(value-coinValue,m);
memo[value][m] = ans;
return ans;
}
public static long[][] memo;
public static int money;
}
annhy
New poster
Posts: 40 Joined: Sun May 27, 2007 1:42 am
Location: Taiwan
Post
by annhy » Mon Aug 08, 2011 6:03 am
I run your code on my computer and java.lang.StackOverflowError occurs. Good luck!!
hduong wrote: I always got Runtime Error although I already tested every case from 1 to 9999. Could anyone help me figure out the error in my code?
...deleted...
3tito
New poster
Posts: 2 Joined: Fri Aug 31, 2012 1:05 pm
Post
by 3tito » Fri Aug 31, 2012 1:14 pm
what data type should be used in this problem ??
brianfry713
Guru
Posts: 5947 Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA
Post
by brianfry713 » Fri Aug 31, 2012 7:40 pm
You can see from the sample I/O that the largest output is 440022018293, which fill fit in a 64-bit integer, but is too large for a 32-bit integer.
Check input and AC output for thousands of problems on
uDebug !