Showing posts with label division. Show all posts
Showing posts with label division. Show all posts

Friday, January 30, 2009

UVa problem 100: sample code, sample algorithm

UVa problem 100 (3n + 1) is a pretty straightforward problem. There is one tricky part though. That i is not necessarily always less than j. So one needs to watch out for the case when i is greater than or equal to j.

Also one, can speed things a little bit by using the bit-shift operator for division or multiplication. A division by 2 is similar to a right bit-shift by 1 bit. A multiplication by 2 is equivalent to a left bit-shift by 1 bit.

That is, x / 2 is equivalent to x >> 1; x * 2 is equivalent to x << 1.


001 #include "stdio.h"
002
003 int main() {
004 char line[100];
005
006 int i
007 , j
008 , n
009 , n0
010 , m
011 , cycle
012 , max
013 ;
014
015 while (fgets(line, 100, stdin)) {
016 sscanf(line, "%d %d", &i, &j);
017
018 if (i <= j) {
019 for (n0 = i, max = 1; n0 <= j; n0++) {
020 n = n0;
021 for (cycle = 1; n != 1; cycle++) {
022 m = n & 1;
023 if (m) {
024 m = n << 1;
025 n = m + n + 1;
026 }
027 else {
028 n >>= 1;
029 }
030 }
031 if (cycle > max) {
032 max = cycle;
033 }
034 }
035 }
036 else {
037 for (n0 = j, max = 1; n0 <= i; n0++) {
038 n = n0;
039 for (cycle = 1; n != 1; cycle++) {
040 m = n & 1;
041 if (m) {
042 m = n << 1;
043 n = m + n + 1;
044 }
045 else {
046 n >>= 1;
047 }
048 }
049 if (cycle > max) {
050 max = cycle;
051 }
052 }
053 }
054
055 printf("%d %d %d\n", i, j, max);
056 }
057
058 return 0;
059 }

Friday, January 16, 2009

UVa problem 11417: sample algorithm

To solve problem 11417 (GCD), it would be beneficial if one knows about the Euclidean algorithm and division algorithm. One could search up "greatest common divisor" in Google and browse through the corresponding Wikipedia article.

The algorithm to get the greatest common divisor between two numbers i and j can be best explained through an example. Consider i = 18 and j = 84, to get the GCD:

84 / 18 = 4 R 12
18 / 12 = 1 R 6
12 / 6 = 2 R 0

GCD for 18 and 84 is then 6.

Another example: i = 16 and j = 56:

56 / 16 = 3 R 8
16 / 8 = 2 R 0

GCD for 16 and 56 is then 8.

In the process illustrated above, the divisor that caused the remainder to be 0 is the greatest common divisor.

Saturday, January 10, 2009

UVA problem 10633: sample algorithm

Problem 10633 may be entitled "Rare Easy Problem" but I do not think it is that easy. It is tricky as well as a little technical as well as tricky. So one has to be very careful with this before submitting.

We define the difference between N and M in Equation 1.

Y = N - M. [Equation 1]

Since M is formed by chopping off the last digit from N, we have Equation 2.

M = N / 10 [Equation 2]

Using Equation 2, Equation 1 can be expressed as Equation 3.

Y = N - (N / 10) [Equation 3]

Using a little algebra, Equation 3 can be transformed to get N as a function of Y.

N = (10 / 9) * Y [Equation 4]

Equation 4 then provides us a way of calculating N given the input Y.

But then we seem to have another problem. Equation 4 will only lead to one value of N given Y. That does not make sense because for example, given Y = 18, N can be 19 or 20, which gives us two possible answers for N, whereas Equation 4 only provides us one answer.

To solve this mystery, we have to analyze the pattern of some answers. Table 1 provides a listing of Y values followed by the corresponding N values.

Table 1. Listing of Y and corresponding N values.
Y, N
10, 11
11, 12
12, 13
13, 14
14, 15
15, 16
16, 17
17, 18
18, 19 20
19, 21
20, 22
21, 23
22, 24
23, 25
24, 26
25, 27
26, 28
27, 29 30
28, 31
29, 32
30, 33
31, 34
32, 35
33, 36
34, 37
35, 38
36, 39 40
37, 41
38, 42
106, 117
107, 118
108, 119, 120
109, 121
110, 122

Analyzing Table 1 reveals that for any given Y, there can be one or two values of N. Also notice that N has two values only when Y is divisible by 9. When Y is divisible by 9, N has two values N1 and N2 given by Equation 5.

[Equation 5]
N1 = (10 / 9) * Y
N2 = N1 - 1

So the algorithm is given by Equation 5. There's one more issue though. The problem requires that the solution be able to accept inputs as large as 10^18. That's a big input number.

If we used an ordinary integer to represent Y, the integer would most likely be able to accommodate up to 2 billion only assuming a 32-bit signed integer. 10^18 is the following number:

1,000,000,000,000,000,000
___6___5___4___3___2___1

2^31 is the following number:

2,147,483,648
___3___2___1

Obviously, the ordinary integer will not be able to hold 10^18.

If we use an unsigned long long int (assuming that the C language is used), the maximum integer value would be increased to 2^64 (subtract 1 from that to be more accurate):

18,446,744,073,709,551,616
____6___5___4___3___2___1

Well 2^64 seems quite certainly to be greater than 10^18 by a magnitude of about 18.

Alternatively, Equation 5 can be expressed as Equation 6 for N1 which is what I did in the sample code.

N1 = Y + (Y / 9) [Equation 6]

Monday, December 29, 2008

UVa problem 10851: sample algorithm

In UVa problem 10851 (2D Hieroglyphs decoder), one could explore the advantage of using right bit-shifts instead of long-cut division for division by powers of 2. For example, to divide a number by 128 (which is 2 raised to the power of 7), one could simply shift the number 7 bits to the right.Definitely, this fact could give one an advantage as a bit-shift operation is so much simpler and faster than traditional division.

Moreover, the mod operation may be quite computationally expensive as well. Since a mod by 2 operation is only needed, one could simply check whether the last binary bit of a number is 0 or 1 in order to tell if the number is divisible by 2 or not.

For example, the number 15 when represented in binary is 1111. Since its last bit (the right-most) is 1, then it is not divisible by 2. The number 12 when represented in binary is 1100. Since the last bit is 0, then it is divisible by 2. Bitwise-ANDing the number with 1 will enable one to get the last bit of the number.

For this problem, one could build a lookup table for each of the 256 ASCII characters. One could explore the use of a C++ standard map, where the key is the encrypted character and the value is the actual ASCII character. For example, we could build a map like the following:

Key, Value
//\\//\/, L
\/////\/, A
…

And so when our program sees an encryption of //\\//\/, it can simply lookup the map and find that the encryption stands for the ASCII character L.