288 - Arithmetic Operations With Large Integers

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

Moderator: Board moderators

288 - Arithmetic Operations With Large Integers

Postby Christian Schuster » Sat Apr 27, 2002 8:43 pm

Hello World,

I tried to get AC for that one several times, but no way...

I'm currently using term splitting, scanning for "+" and "-" from the right (they associate left), afterwards for "*" from the right (dito) and for "**" (which I replaced before) from the left (right associative). I'm then calling that thing recursively. If no operator is found, it's a simple number, where I remove leading zeroes and return.

My program seems to work quite correct for all of my test cases, so I'm sure there's some tricky input. BTW: What's the result of 0**0?

Could someone help me? Any hints?

Here are some (in my opinion) critical samples:
3**4**2 => 43046721
000333-0001000 => -667
3-5+2 => 0
2**9999-4**4999*2 => 0
Last edited by Christian Schuster on Sat Apr 27, 2002 9:12 pm, edited 1 time in total.
User avatar
Christian Schuster
Learning poster
 
Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

Postby Adrian Kuegel » Sat Apr 27, 2002 9:09 pm

I have to add some questions:
What about 0**0**0? And 1-2**2?
And I hope that I did understand it right that there is not a input like -2*-2.
I also print the result in a line, not in several lines like in the sample output, because the description says: "Print each test case in a different line."
I remove leading zeros all time, even directly before output, and I print 0 if the result is 0.
What could be the reason that I get Wrong Answer?
Adrian Kuegel
Guru
 
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Postby Adrian Kuegel » Sat Apr 27, 2002 9:18 pm

Now I got Accepted. I forgot to store the sign correctly for the result after addition. If the mentioned cases are in the input, then:
0**0 = 1
0**0**0 = 0
1-2**2 = -3
and the other results are like Christian wrote.
Adrian Kuegel
Guru
 
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Postby Christian Schuster » Sat Apr 27, 2002 10:30 pm

I finally got AC - forgot to reset the sign flag in multiplication... :-?

Thanks for the inspiration... :D
User avatar
Christian Schuster
Learning poster
 
Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

Postby ha_tran » Sat May 04, 2002 1:32 am

I can't understand why I got it right with all of your results above, however, it's still not AC.
Is there any trick in the input?
ha_tran
New poster
 
Posts: 1
Joined: Sat May 04, 2002 1:30 am

Postby bluna » Mon May 27, 2002 9:11 am

I don't know what to do!
On my computer it works just fine, but when I send it, it always, and I mean ALWAYS, come back the SIGSEGV error:
Invalid memory reference!!

Have you got any ideas, what is wrong??

Bye
bluna
New poster
 
Posts: 3
Joined: Mon May 27, 2002 9:09 am
Location: Slovenia

Postby Adrian Kuegel » Mon May 27, 2002 11:52 am

I think, you are solving the problem recursively. Therefore you have to try that your recursion depth is as small as possible to avoid stack overflow.
Adrian Kuegel
Guru
 
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Postby bluna » Tue May 28, 2002 12:51 pm

I had a solution with recurision, but when I got runtime error message, I changed it into non-recursion way.
bluna
New poster
 
Posts: 3
Joined: Mon May 27, 2002 9:09 am
Location: Slovenia

Postby bluna » Tue May 28, 2002 12:51 pm

I had a solution with recurision, but when I got runtime error message, I changed it into non-recursion way.
10x anyway for suggestion
bluna
New poster
 
Posts: 3
Joined: Mon May 27, 2002 9:09 am
Location: Slovenia

Postby windows2k » Thu May 27, 2004 4:00 am

Sorry, I tried to solve the problem and get WA all the time.
I have passed all the input/output above.
Could someone give more tircky Input/Output? Thx :D
windows2k
Experienced poster
 
Posts: 136
Joined: Sat Apr 05, 2003 3:29 pm
Location: Taiwan

Postby Adrian Kuegel » Thu May 27, 2004 11:51 am

Perhaps the following information helps you:
There may be more than 2500 digits in a (intermediate) result, but never more than 3000 digits.
Each input line contains at most 200000 characters.
And there may be leading zeros.
Adrian Kuegel
Guru
 
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Postby Ryan Pai » Sun Mar 26, 2006 6:53 am

So I keep getting WA. I implemented, but didn't post, a class Z which is meant to be an arbitrary precision integer class (it is meant to act like int, overloaded operaters etc.). I'm pretty sure the +,* and - work with the Z class, since I've used it on other problems. ** also seems to work (I tested it with quite a few powers of 2). Could there be a problem with the parsing?

Code: Select all
string parse(string);
Z expression(istream&);
Z term(istream&);
Z factor(istream&);

int main(){
  string s;
  while(getline(cin,s)){
    istringstream sin(parse(s));
    ostringstream sout;
    sout<<expression(sin);
    s=sout.str();
    //while(s.size()>75){ cout<<s.substr(0,75)<<"\\\\\n"; s=s.substr(75); }
    cout<<s<<'\n';
  }
}

Z expression(istream& in){
  Z a=term(in);
  char op;
  while(in>>op){
    Z b=term(in);
    if(op=='+')
      a+=b;
    else
      a-=b;
  }
  return a;
}

Z term(istream& in){
  Z a=factor(in);
  while(in.peek()=='*'){
    in.get();
    Z b=factor(in);
    a*=b;
  }
  return a;
}

Z factor(istream& in){
  Z a;
  in>>a;
  if(in.peek()=='^'){
    in.get();
    Z b=factor(in);
    a^=b;
  }
  return a;
}

string parse(string s){
  string t;
  for(int i=0;i<s.size();i++)
    if(s[i]=='*' && i+1<s.size() && s[i+1]=='*'){
      t+='^'; i++;
    }else
      t+=s[i];
  return t;
}
I'm always willing to help, if you do the same.
Ryan Pai
Learning poster
 
Posts: 67
Joined: Fri Jul 04, 2003 9:59 am
Location: USA

Postby Adrian Kuegel » Mon Mar 27, 2006 3:16 pm

I think your parsing looks good. The only thing which I can see which may cause some problems is a leading unary -, for example:
-200+1
I don't know if something like this occurs in the judge input, but my program handles it.
Edit: I just read that such a case would be invalid.
Adrian Kuegel
Guru
 
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany


Return to Volume II

Who is online

Users browsing this forum: No registered users and 0 guests