11403 - Binary Multiplication

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

Moderator: Board moderators

adelar
New poster
Posts: 35
Joined: Wed May 02, 2007 11:48 pm
Location: Brasil

Re: 11403 - Binary Multiplication

Post by adelar »

Hi Ron,
try this input:

Code: Select all

0 11
11 0
0 0
output:

Code: Select all

 0
11
--
 0
0
--
 0

11
 0
--
00
--
 0

Ron
Learning poster
Posts: 55
Joined: Mon Jul 23, 2007 5:01 pm
Location: INDIA

Re: 11403 - Binary Multiplication

Post by Ron »

hi adelar...
i removed that error...but still WA...
help......
thnx in advance...

input :

Code: Select all

0 11
11 0
11 111
0 0
my code's output:

Code: Select all

 0
11
--
 0
0
--
 0

11
 0
--
00
--
 0

   11
  111
  ---
   11
  11
 11
-----
10101

new code..

Code: Select all

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

string binary_sum(string x,string y){
	int i,j,c=0,sum,l1=x.length(),l2=y.length();
	string s1,s2;
	if(l1>l2){
		j=l1;l1=l2;l2=j;
		s2=x;
		s1=y;
	}
	else{
		s2=y;
		s1=x;
	}
	for(i=l1-1,j=l2-1;i>=0;i--,j--){
		sum=(s1[i]-'0')+(s2[j]-'0')+c;
		if(sum<2){
			s2[j]=(char)(sum+'0');
			c=0;
		}
		else{
			c=1;
			s2[j]=(char)(sum%2+'0');
		}
	}
	while(j>=0&&c>0){
		sum=(s2[j]-'0')+c;
		if(sum<2){
			s2[j]=(char)(sum+'0');
			c=0;
		}
		else{
			c=1;
			s2[j]='0';
		}
		j--;
	}
	if(c>0)	s2=(char)('1')+s2;
	if(s2.length()==0)	return "0";
	return s2;
}

int main(){
	int i,j,k,l,m;
	string s1,s2,s;
	bool f=1;
	while(cin>>s1>>s2&&(s1[0]!='0'||s2[0]!='0')){
		if(f)	f=0;
		else	cout<<endl;
		vector<string> v;
		k=s1.length();
		l=s2.length();
		s=string(k,'0');
		string ans=string(k,'0');
		for(i=l-1;i>=0;i--){
			if(s2[i]=='1'){
				v.push_back(s1);
				ans=binary_sum(ans,s1+string(l-i-1,'0'));
			}
			else{
				v.push_back(s);
			}
		}
		m=ans.length();
		j=v.size();
		cout<<string(m-k,' ')+s1<<endl;
		cout<<string(m-l,' ')+s2<<endl;
		s=string(max(l,k),'-');
		cout<<string(m-s.length(),' ')+s<<endl;
		for(i=0;i<j;i++){
			cout<<string(m-v[i].length()-i,' ')<<v[i]<<endl;
		}
		cout<<string(m,'-')<<endl;
		if(ans<"01"){
			cout<<string(m-1,' ')+"0"<<endl;
		}
		else
			cout<<ans<<endl;
	}
	return 0;
}

adelar
New poster
Posts: 35
Joined: Wed May 02, 2007 11:48 pm
Location: Brasil

Re: 11403 - Binary Multiplication

Post by adelar »

Hi Ron,
the AC output is:

Code: Select all

S0
11
--
S0
0
--
S0

11
S0
--
00
--
S0

SSS11
SS111
SS---
SSS11
SS11
S11
-----
10101
S = space (" ")
Ron
Learning poster
Posts: 55
Joined: Mon Jul 23, 2007 5:01 pm
Location: INDIA

Re: 11403 - Binary Multiplication

Post by Ron »

But my code gives same output as AC' code....
you can check output of my code.....
now what is the error ??
annhy
New poster
Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

Re: 11403 - Binary Multiplication

Post by annhy »

It is really strange that I got lots of 'Runtime error' even if my Java code reads in the input data without doing anything.
I have tried following methods to read the data in:
(1) Scanner.nextInt()
(2) Scanner.next()
(3) Scanner.nextLine() + String.split(" ")
(4) BufferedReader.readLine() + String.split(" ")
(5) using System.in.read() to read each byte of all input into a StringBuilder
But none of above worked.
I have solved more than 400 problems with similar Java code in this site.
And I can't imagine what's wrong with the judge input?! :o

My friends solved this problem easily by using C/C++ with cin or scanf().
But I hate to rewrite my code in C++.

Does anybody have similar experience with this strange error of Java code?
Last edited by annhy on Thu Apr 19, 2012 5:05 am, edited 1 time in total.
Ron
Learning poster
Posts: 55
Joined: Mon Jul 23, 2007 5:01 pm
Location: INDIA

Re: 11403 - Binary Multiplication

Post by Ron »

Please .. someone help me.
I am stuck in this problem since so many time.
what is the problem in my code posted above.
quol
New poster
Posts: 3
Joined: Sat Sep 13, 2008 11:53 pm

Re: 11403 - Binary Multiplication

Post by quol »

It is really strange that I got lots of 'Runtime error' even if my Java code reads in the input data without doing anything.
It works for me, two possible problems I can think of:
1) As was written earlier in this thread, the input is terminated by EOF, not by 0 0!
2) If you use scanner.nextInt or Integer.parseInt or something similar, be sure to pass the correct radix.
annhy
New poster
Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

Re: 11403 - Binary Multiplication

Post by annhy »

quol wrote:
It is really strange that I got lots of 'Runtime error' even if my Java code reads in the input data without doing anything.
It works for me, two possible problems I can think of:
1) As was written earlier in this thread, the input is terminated by EOF, not by 0 0!
2) If you use scanner.nextInt or Integer.parseInt or something similar, be sure to pass the correct radix.
Thanks quol !!
After I read the earlier posts in this thread and try again, I got AC.

Correct answer is 1)
the input is terminated by EOF, not by 0 0

Shouldn't someone fix this problem?!
aerofoil.kite
New poster
Posts: 12
Joined: Tue Dec 06, 2011 8:59 pm
Location: Bangladesh
Contact:

Re: 11403 - Binary Multiplication

Post by aerofoil.kite »

Code: Select all

removed
Last edited by aerofoil.kite on Mon May 26, 2014 8:42 pm, edited 1 time in total.
aerofoil.kite
New poster
Posts: 12
Joined: Tue Dec 06, 2011 8:59 pm
Location: Bangladesh
Contact:

Re: 11403 - Binary Multiplication

Post by aerofoil.kite »

There is a clarification in this forum about this problem:
  • 1. The program will terminate with EOF
    2. An empty line after each test case
I thing these clarification is not valid now. I got WA for this clarification. Problem statement is correct now.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11403 - Binary Multiplication

Post by brianfry713 »

I accidentally deleted these posts:

Problem I in Hasty contest 11403
Post Posted Sat Jan 19, 2008 8:12 am by tobby

So the mistakes in the input and output files cost me 20+ submissions, and made me spent more than 90 minutes to debug my bug-less program. And I know I'm not alone. Big deal. And you are not even giving us extra contest time for that. I could solve at least one more problem if I have my wasted hour back. Then I could solve 7 tasks.

Thank you very much for such a great mistake. :evil: Also I greatly appreciate the contestants who spotted the mistakes.
Select:
Problem I in Hasty contest 11403
Post Posted Fri Jan 25, 2008 6:17 am by Hadi

Although the problem-setters could do better, but I believe they have spent much time to prepare this problem-set. so It might be not correct to be such 'harsh' to them.

And this was just a practice contest, and it wasn't their duty to prepare the contest for us, but they were so kind that they created the problem-set, so we can learn new things.

And I should mention that such things might happen in other contests as serious as ICPC World Finals. I think one problem had invalid judge data last year. So, you might try to learn how to behave in such situations instead of just becoming angry.

And I think these problem-setters had many nice and bug-less problems before, but I don't think many people thanked them. But you are too harsh when you find a something disappointing just in one of their contests.

and the last point: Getting the problem-set right is a difficult task, and needs much help, you might like to propose for help next time :)
Select:
Problem I in Hasty contest 11403
Post Posted Fri Jan 25, 2008 6:34 am by tobby

I think my comments are fair enough. There were some good questions, but those mistakes overshadowed them all. Both the input and output files are flawed, not just "a something". In other words, that whole problem was completely wrong.

I use no strong words in my comments, and I think I behaved well enough. I just cannot see how I can appreciate a wrong problem. I cannot say anything about ICPC World Finals. I use this site just for fun.

But I think I shall keep quiet next time. Sorry if my comments make you feel bad.

By the way, I really like problem E, although again the input file is wrong.
Select:
Problem I in Hasty contest 11403
Post Posted Fri Jan 25, 2008 6:46 am by Hadi

I wasn't one of the problem-setters, and I don't have any relations with them.

Don't keep quiet next time, but there are always better ways to express things.
Select:
Problem I in Hasty contest 11403
Post Posted Fri Jan 25, 2008 6:59 am by Hadi

BTW, There are online contests tomorrow (at http://acm.zju.edu.cn/ ) and the day after tomorrow (at http://acm.pku.edu.cn/JudgeOnline ). You might try doing them. The problem-sets of their previous contests were really EXCELLENT, so I expect some great problems :)
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 114 (11400-11499)”