10814 - Simplifying Fractions

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

Moderator: Board moderators

Pavel Nalivaiko
New poster
Posts: 15
Joined: Sun Mar 07, 2004 7:47 pm
Location: Moscow, Russia

10814 - Simplifying Fractions

Post by Pavel Nalivaiko » Sat Feb 12, 2005 7:03 pm

Hi! :D

If there exist a simple way to solve this problem
(without writing classic "long arithmetics" )?
May be we could use that P,Q < 10**30, so both of them could be represented as a pair of two 64-bit integers?

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Sat Feb 12, 2005 9:14 pm

Hello Pavel Nalivaiko.
MaxNumber for 64bit unsignt int is not bigger then 10^20 so you can't use it.I don't think that this problam can be solvable without using BigNumbers.I have solved this problem during the contest using my BigNumber calculations.If someone know other solution please tell.
Eduard
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

Pavel Nalivaiko
New poster
Posts: 15
Joined: Sun Mar 07, 2004 7:47 pm
Location: Moscow, Russia

Post by Pavel Nalivaiko » Sat Feb 12, 2005 11:11 pm

Hello, Eduard.
Sure, I know that 10**30 is greater then 2**64. But I mean, that we could represent our numbers as A*10**15+B, where A and B are Int64 numbers.
So, may be the implementation of BigNumbers using this representation is easier than implementation in general case?

I think that it's not fair to use in the contest code written before the contest starts - because you cant do in the real ACM ICPC competition.
So if there exist a way to simplify implementation, I just want to now how to do it.

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Sun Feb 13, 2005 9:16 pm

So if there exist a way to simplify implementation, I just want to now how to do it.
If there exists I want to know about it too. :)
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

technobug
Learning poster
Posts: 88
Joined: Sat Nov 15, 2003 6:41 pm
Location: Brasilien
Contact:

Post by technobug » Wed Feb 16, 2005 5:17 am

i tried to use java (copied BigInteger and adapted it) but it did not work out... java sucks here

but back to C, I only have the basic arithmetic implemented, does anyone have an 'open source' one? :)

technobug
Learning poster
Posts: 88
Joined: Sat Nov 15, 2003 6:41 pm
Location: Brasilien
Contact:

Post by technobug » Wed Feb 16, 2005 5:17 am

ps: and wants to share it :)

nukeu666
New poster
Posts: 44
Joined: Sun Feb 13, 2005 1:13 am
Location: India
Contact:

Post by nukeu666 » Sat Mar 05, 2005 3:19 am

im using the binary gcd (http://www.nist.gov/dads/HTML/binaryGCD.html) but it also is well above timelimit....
what else can i use?
and is bigint necessary to get the solution accepted here :roll:
google

User avatar
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego » Sat Mar 05, 2005 11:24 am

I have an open source big int implementation here:
http://www.lexansoft.com:8081/tools/
There is other stuff, too.
If only I had as much free time as I did in college...

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Sat Mar 05, 2005 11:38 am

nukeu666:
The binaryGCD as described in the link can go into an endless loop for some values of u and/or v. Think about it, the cure is simple.

Code: Select all

int gcd(int u, int v){
   int g=1;

   while((u%2==0)&&(v%2==0)){
      u/=2;
      v/=2;
      g*=2;
      }
   while(u>0){
      if(u%2==0) u/=2;
      else if(v%2==0) v/=2;
      else if(u>=v) u-=v;
      else v-=u;
      }
   return g*v;
   }
Suffers from this problem :)

User avatar
dovier_antonio
New poster
Posts: 47
Joined: Fri Feb 18, 2005 5:00 am
Location: Havana, Cuba

Hi Litle

Post by dovier_antonio » Sun Mar 13, 2005 11:48 am

I think that your algorithm looks so good!
Last edited by dovier_antonio on Fri Feb 03, 2012 9:21 am, edited 1 time in total.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Fri Mar 25, 2005 7:56 am

I am getting TLE! Can someone post some tricky inputs ?

Regards,
Suman.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Mon Mar 28, 2005 4:37 pm

Since inputs are not forthcoming... :cry:
I'll assume something is going awry in my scan input part which is as follows:

Code: Select all

scanf("%d", &n);
   getchar();

   while ( n-- )
   { 
       /* read somthing like dddd[ ]/[ ]dddd from the command line*/
       fgets(buf, 1024, stdin);
      
       k = i = 0;
	while( !isdigit(buf[k]) ) k++;
       while ( isdigit(buf[k]) )
       {
           num[i++] = buf[k];
           k++;
       }
       num[i] = '\0';
       i = 0;
	while( !isdigit(buf[k]) ) k++;
        /*skip the `/' */
       while ( isdigit(buf[k]) )
       {
           den[i++] = buf[k++];
       }
       den[i] = '\0';       
Any comments?
[edit]
That part is the root of all evil :lol:
[/edit]
Regards,
Suman.

User avatar
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Post by Sedefcho » Sat Jul 30, 2005 2:09 pm

Isn't the C++ type unsigned long long enough for
this problem. If I am not completely wrong then
unsigned long long can hold values up to
2 ^ 128 - 1 which is much more than 10 ^ 30.

Is that not the case ?

2 ^ 128 = 16 ^ 32 > 10 ^ 32 > 10 ^ 30 .

As noone here it talking about using built-in C++ types I guess
I am fundamentally wrong about that. But ... Isn't it true that
on 32-bit machines we have:
size of INT is 4 bytes - 32 bits;
size of LONG is 8 bytes - 64 bits;
size of LONG LONG is 16 bytes - 128 bits ?

One more thing. If I am right about these then how do
we read and print unsigned long long values. Actually even
long long should be anough if it can hold values up to
2 ^ 127 - 1 as we have :
2 ^ 127 - 1 == 8 . 16 . 2^120 - 1 == 8. 16 . 16^30 - 1 > 10 ^ 30
Is that not the case ?

I have tried using
scanf with %llu, %Lu for unsigned long long and
%lld, %Ld for long long, both don't work quite well. I mean
the value read is not okay if the number is 10^30.
Apparently long long and/or unsigned long long can not hold
such long integers , why is that ?

Then I tried printing with cout - this seems to work.
But reading the values with cin also does not work.

Any help is welcome.

User avatar
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Post by Sedefcho » Sat Jul 30, 2005 3:00 pm

Abendego,

I took your C++ implementation of BigInt from
http://www.lexansoft.com:8081/tools/
and it works fine. The only thing I had to modify was to remove
the method which uses the operator >?= which is, as far
as I understood, a C++ extension supported only by
the g++ compiler. I think this operator was only used in
one following method :
BigInt operator* ( long double n );


Can someone point me to a reference of all these extensions ?!
I mean the extensions which g++ defines and supports.

I just don't have any other compiler than the one from
MS Visual Studio. Sorry if mentioning that would disturb you.
I am not a C++ expert at the end of the day. I am mostly using
Java as a programming language.

Thanks in advance.

stubbscroll
Experienced poster
Posts: 151
Joined: Tue Nov 16, 2004 7:23 pm
Location: Norway
Contact:

Post by stubbscroll » Sat Jul 30, 2005 4:44 pm

Sedefcho wrote:Isn't the C++ type unsigned long long enough for
this problem. If I am not completely wrong then
unsigned long long can hold values up to
2 ^ 128 - 1 which is much more than 10 ^ 30.
[snip]
As noone here it talking about using built-in C++ types I guess
I am fundamentally wrong about that. But ... Isn't it true that
on 32-bit machines we have:
size of INT is 4 bytes - 32 bits;
size of LONG is 8 bytes - 64 bits;
size of LONG LONG is 16 bytes - 128 bits ?
[snip]
The sizes you wrote above are incorrect. 32-bit machines usually have (in C/C++):

int: 4 bytes
long: 4 bytes (Java uses 8 bytes, I think)
long long: 8 bytes

Post Reply

Return to “Volume 108 (10800-10899)”