Page **1** of **3**

### 10814 - Simplifying Fractions

Posted: **Sat Feb 12, 2005 7:03 pm**

by **Pavel Nalivaiko**

Hi!

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?

Posted: **Sat Feb 12, 2005 9:14 pm**

by **Eduard**

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

Posted: **Sat Feb 12, 2005 11:11 pm**

by **Pavel Nalivaiko**

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.

Posted: **Sun Feb 13, 2005 9:16 pm**

by **Eduard**

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.

Posted: **Wed Feb 16, 2005 5:17 am**

by **technobug**

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?

Posted: **Wed Feb 16, 2005 5:17 am**

by **technobug**

ps: and wants to share it

Posted: **Sat Mar 05, 2005 3:19 am**

by **nukeu666**

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

Posted: **Sat Mar 05, 2005 11:24 am**

by **Abednego**

I have an open source big int implementation here:

http://www.lexansoft.com:8081/tools/
There is other stuff, too.

Posted: **Sat Mar 05, 2005 11:38 am**

by **little joey**

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

### Hi Litle

Posted: **Sun Mar 13, 2005 11:48 am**

by **dovier_antonio**

I think that your algorithm looks so good!

Posted: **Fri Mar 25, 2005 7:56 am**

by **sumankar**

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

Regards,

Suman.

Posted: **Mon Mar 28, 2005 4:37 pm**

by **sumankar**

Since inputs are not forthcoming...

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

[/edit]

Regards,

Suman.

Posted: **Sat Jul 30, 2005 2:09 pm**

by **Sedefcho**

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.

Posted: **Sat Jul 30, 2005 3:00 pm**

by **Sedefcho**

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.

Posted: **Sat Jul 30, 2005 4:44 pm**

by **stubbscroll**

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