2008年9月26日

623 - 500!


 
UVa網站題目連結
My Solved Problems Performance

In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.

As an examples we can give programs which calculate primary numbers.

One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no standard, neither integer nor floating, data type is applicable here.

Your task is to write a programs which calculates a factorial of a given number.

Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).

Input

Any number of lines, each containing value ``n" for which you should provide value of n!

Output

2 lines for each input case. First should contain value ``n" followed by character `!'. The second should contain calculated value n!.

Sample Input

10
30
50
100

Sample Output

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
9332621544394415268169923885626670049071596826438162146859296389521759999322991
5608941463976156518286253697920827223758251185210916864000000000000000000000000

Solution

  1. import java.io.BufferedInputStream;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;

  4. public class Main {

  5. static int size = 1001;
  6. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  7. static BigInteger[] bigIntArray = new BigInteger[size];
  8. static int input;

  9. public static void main(String[] args) {

  10. bigIntArray[0] = BigInteger.ONE;
  11. bigIntArray[1] = BigInteger.ONE;
  12. for (int i=2; i<size; i++) {
  13. bigIntArray[i] = bigIntArray[i-1].multiply(new BigInteger(String.
  14. valueOf(i)));
  15. }

  16. while (in.hasNext()) {
  17. input = in.nextInt();
  18. System.out.println(input+"!");
  19. System.out.println(bigIntArray[input]);
  20. }
  21. }
  22. }
關鍵字:UVa Online Judge, ACM. Java

326 - Extrapolation Using a Difference Table


 
UVa網站題目連結
My Solved Problems Performance

A very old technique for extrapolating a sequence of values is based on the use of a difference table. The difference table used in the extrapolation of a sequence of 4 values, say 3, 6, 10, and 15, might be displayed as follows:

The original sequence of values appears in the first column of the table. Each entry in the second column of the table is formed by computing the difference between the adjacent entries in the first column. These values (in the second column) are called first differences. Each entry in the third column is similarly the difference between the adjacent entries in the second column; the third column entries are naturally called second differences. Computation of the last column in this example should now be obvious (but beware that this value is not always zero). Note that the last column will always contain only a single value. If we begin with a sequence of n values, the completed difference table will have n columns, with the single value in column n representing the single n-1st difference.

To extrapolate using a difference table we assume the n-1st differences are constant (since we have no additional information to refute that assumption). Given that assumption, we can compute the next entry in the n-2nd difference column, the n-3rd difference column, and so forth until we compute the next entry in the first column which, of course, is the next value in the sequence. The table below shows the four additional entries (in boxes) added to the table to compute the next entry in the example sequence, which in this case is 21. We could obviously continue this extraolation process as far as desired by adding additional entries to the columns using the assumption that the n-1st differences are constant.

Input and Output

The input for this problem will be a set of extraolation requests. For each request the input will contain first an integer n, which specifies the number of values in the sequence to be extended. When n is zero your program should terminate. If n is non-zero (it will never be larger than 10), it will be followed by n integers representing the given elements in the sequence. The last item in the input for each extrapolation request is k, the number of extrapolation operations to perform; it will always be at least 1. In effect, you are to add k entries to each column of the difference table, finally reporting the last value of the sequence computed by such extrapolation. More precisely, assume the first n entries (the given values) in the sequence are numbered 1 through n.

Your program is to determine the n+kth value in the sequence by extrapolation of the original sequence k times.

Hint: no upper limit is given for k, and you might not be able to acquire enough memory to construct a complete difference table.

Sample Input

4 3 6 10 15 1
4 3 6 10 15 2
3 2 4 6 20
6 3 9 12 5 18 -4 10
0

Sample Output

Term 5 of the sequence is 21
Term 6 of the sequence is 28
Term 23 of the sequence is 46
Term 16 of the sequence is -319268

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;
  3. import java.util.Vector;
  4.  
  5. public class Main {
  6.  
  7. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  8. static int n, k;
  9. static Vector<Vector<Integer>> int2DArray = new Vector<Vector<Integer>>();
  10.  
  11. public static void main(String[] args) {
  12.  
  13. while ((n=in.nextInt())!=0) {
  14.  
  15. int2DArray = new Vector<Vector<Integer>>();
  16. for (int i=0; i<n; i++) {
  17. int2DArray.add(new Vector<Integer>());
  18. }
  19. for (int i=0; i<n; i++) {
  20. int2DArray.elementAt(0).add(i, in.nextInt());
  21. }
  22. k = in.nextInt();
  23. for (int i=1; i<n; i++) {
  24. for (int j=0; j<n-i; j++) {
  25. int2DArray.elementAt(i).add(j, int2DArray.elementAt(i-1).
  26. elementAt(j+1)-int2DArray.elementAt(i-1)
  27. .elementAt(j));
  28. }
  29. }
  30.  
  31. for (int i=0; i<k; i++) {
  32. for (int j=n-1; j>=0; j--) {
  33. if (j!=n-1) {
  34. int2DArray.elementAt(j).add(int2DArray.elementAt(j).
  35. lastElement()+int2DArray.elementAt(
  36. j+1).lastElement());
  37. } else {
  38. int2DArray.elementAt(j).add(int2DArray.elementAt(j).
  39. lastElement());
  40. }
  41. }
  42. }
  43. System.out.println("Term " + (n+k) + " of the sequence is " +
  44. int2DArray.elementAt(0).lastElement());
  45. }
  46. }
  47. }
關鍵字:UVa Online Judge, ACM. Java

324 - Factorial Frequencies


 

UVa網站題目連結
My Solved Problems Performance

In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to determine these values.

Recall that the definition of n! (that is, n factorial) is just tex2html_wrap_inline28 . As she expects to use either the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.

Input and Output

The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don't bother to process this zero value; just stop your program at that point. The output format isn't too critical, but you should make your program produce results that look similar to those shown below.

Madam Phoenix will be forever (or longer) in your debt; she might even give you a trip if you do your job well!

Sample Input

3
8
100
0

Sample Output

3! --
(0) 0 (1) 0 (2) 0 (3) 0 (4) 0
(5) 0 (6) 1 (7) 0 (8) 0 (9) 0
8! --
(0) 2 (1) 0 (2) 1 (3) 1 (4) 1
(5) 0 (6) 0 (7) 0 (8) 0 (9) 0
100! --
(0) 30 (1) 15 (2) 19 (3) 10 (4) 10
(5) 14 (6) 19 (7) 7 (8) 14 (9) 20

Solution

  1. import java.io.BufferedInputStream;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  8. static int input, temp;
  9. static int[] intArray = new int[10];
  10. static BigInteger bigInt = new BigInteger("1");
  11. static BigInteger[] bigIntArray = new BigInteger[367];
  12.  
  13. public static void main(String[] args) {
  14. bigIntArray[0] = new BigInteger("0");
  15. bigIntArray[1] = new BigInteger("1");
  16. for (int i=2; i<367; i++) {
  17. bigIntArray[i] = bigIntArray[i-1].multiply(new BigInteger(
  18. String.valueOf(i)));
  19. }
  20.  
  21. while ((input=in.nextInt())!=0) {
  22. process(input);
  23. }
  24. }
  25.  
  26. private static void process(int input) {
  27. for (int i=0; i<intArray.length; i++) {
  28. intArray[i]=0;
  29. }
  30. bigInt = bigIntArray[input];
  31. while (bigInt.compareTo(BigInteger.ZERO)>0) {
  32. temp = bigInt.divideAndRemainder(BigInteger.TEN)[1].intValue();
  33. bigInt = bigInt.divide(BigInteger.TEN);
  34. intArray[temp]++;
  35. }
  36. System.out.println(input+"! --");
  37. System.out.println(" (0) "+intArray[0]+" (1) "+intArray[1]+
  38. " (2) "+intArray[2]+" (3) "+intArray[3]+
  39. " (4) "+intArray[4]);
  40. System.out.println(" (5) "+intArray[5]+" (6) "+intArray[6]+
  41. " (7) "+intArray[7]+" (8) "+intArray[8]+
  42. " (9) "+intArray[9]);
  43. }
  44. }
關鍵字:UVa Online Judge, ACM. Java

305 - Joseph


 
UVa網站題目連結
My Solved Problems Performance

The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, ..., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.

Sample Input

3
4
0

Sample Output

5
30

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;

  3. public class Main {

  4. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  5. static int k, m, p, now, kill;

  6. public static void main (String[] args) {

  7. while ((k=in.nextInt())!=0) {
  8. Joseph(k);
  9. }
  10. }

  11. private static void Joseph(int k) {
  12. p = 2*k;
  13. now = 1;
  14. m = 2;
  15. while (p > k) {
  16. kill = (now+m-1)%p;
  17. if ((kill<=k)&&(kill!=0)) {
  18. p = 2*k;
  19. m++;
  20. now = 1;
  21. } else {
  22. now = (kill==0) ? 1 : kill;
  23. p--;
  24. }
  25. }
  26. System.out.println(m);
  27. }
  28. }
關鍵字:UVa Online Judge, ACM. Java

136 - Ugly Numbers


 
UVa網站題目連結
My Solved Problems Performance

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...

shows the first 11 ugly numbers. By convention, 1 is included.

Write a program to find and print the 1500'th ugly number.

Input and Output

There is no input to this program. Output should consist of a single line as shown below, with replaced by the number computed.

Sample output

The 1500'th ugly number is <number>.

Solution

  1. public class Main {

  2. public static void main (String[] args) {

  3. int size = 1500;

  4. int counter = 1;
  5. int result = 0;
  6. for (int i=2; counter<size; i++) {
  7. int x = i;

  8. while ((x%5==0)) {
  9. x/=5;
  10. };
  11. while ((x%3==0)) {
  12. x/=3;
  13. };
  14. while ((x%2==0)) {
  15. x/=2;
  16. };

  17. if (x==1) {
  18. counter++;
  19. }
  20. if (counter == size) result = i;
  21. }
  22. System.out.println("The 1500'th ugly number is " + result + ".");
  23. // The 1500'th ugly number is 859963392.
  24. }
  25. }

關鍵字:UVa Online Judge, ACM. Java

2008年9月25日

102 - Ecological Bin Packing


 
UVa網站題目連結
My Solved Problems Performance

Background

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.

In this problem you will be solving a bin packing problem that deals with recycling glass.

The Problem

Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.

The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.

For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

The Input

The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31

indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.

Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.

The Output

For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.

The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.

The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.

The integer indicating the minimum number of bottle movements should follow the string.

If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.

Sample Input

1 2 3 4 5 6 7 8 9
5 10 5 20 10 5 10 20 10

Sample Output

BCG 30
CBG 50

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;

  3. public class Main {

  4. static String sArray[] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"};
  5. static int iArray[] = new int[6];
  6. static int inArray[] = new int[9];
  7. static Scanner in = new Scanner(new BufferedInputStream(System.in));

  8. public static void main (String[] args) {

  9. while (in.hasNext()) {
  10. process();
  11. }
  12. }

  13. private static void process() {

  14. int total = 0;
  15. for (int i=0; i<9; i++) {
  16. inArray[i] = in.nextInt();
  17. total += inArray[i];
  18. }
  19. iArray[0] = total - inArray[0] - inArray[5] - inArray[7];
  20. iArray[1] = total - inArray[0] - inArray[4] - inArray[8];
  21. iArray[2] = total - inArray[2] - inArray[3] - inArray[7];
  22. iArray[3] = total - inArray[2] - inArray[4] - inArray[6];
  23. iArray[4] = total - inArray[1] - inArray[3] - inArray[8];
  24. iArray[5] = total - inArray[1] - inArray[5] - inArray[6];

  25. int min = iArray[0];
  26. int result = 0;
  27. for (int i=1; i<6; i++) {
  28. if (min > iArray[i]) {
  29. min = iArray[i];
  30. result = i;
  31. }
  32. }
  33. System.out.println(sArray[result] + " " + iArray[result]);
  34. }
  35. }
關鍵字:UVa Online Judge, ACM. Java

100 - The 3n + 1 problem


 
UVa網站題目連結
My Solved Problems Performance

Background

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

The Problem

Consider the following algorithm:

 1.   input n

2. print n

3. if n = 1 then STOP

4. if n is odd then tex2html_wrap_inline44

5. else tex2html_wrap_inline46

6. GOTO 2

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

The Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no operation overflows a 32-bit integer.

The Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;

  3. public class Main {

  4. public static void main (String[] args) {

  5. int in1, in2, x, min, max, counter = 0, counterMax = 0;
  6. Scanner in = new Scanner(new BufferedInputStream(System.in));
  7. while (in.hasNext()) {
  8. in1 = in.nextInt();
  9. in2 = in.nextInt();
  10. if (in1 > in2) {
  11. max=in1;
  12. min=in2;
  13. } else {
  14. max=in2;
  15. min=in1;
  16. }
  17. counterMax = 0;
  18. for (x=min; min<=max; min++) {
  19. x = min;
  20. for (counter = 1; x!=1; counter++) {
  21. if ((x&1)==1) x=3*x+1;
  22. else x>>=1;
  23. }
  24. if (counter>counterMax)counterMax =counter;
  25. }
  26. System.out.println(in1 + " " + in2 + " " + counterMax);
  27. }
  28. }
  29. }

關鍵字:UVa Online Judge, ACM. Java

2008年9月2日

[發佈] werdna-feeds


 


上禮拜花了兩天研究google gadget
除了修改了未來圖書館可能會發表的個人化小工具外
也順手為自己打造一個專屬的RSS reader
功能其實很簡單 會自動讀取我兩個網誌和相簿的rss
所以只要有更新就可以在這個小工具上看見

這個小工具最簡便就是放在igoogle個人首頁上
網址: http://www.google.com/ig 以google帳號登入
只要在樣本圖下面找到[+google]標籤按下即可加入

320x250


420x250


目前的設定比較推薦在解析度1280以上
不然有些太長的句子可能會被截斷
螢幕解析度在1024x768的話畫面大概會像第一個圖
若是1280以上的會像第二個 (420x250那個)
不過在igoogle上不會有邊框 邊框是放在個人部落格才有的
另外對firefox支援較佳
大家趕快捨棄萬惡的ie吧 xd

另外也可以放在個人的網站部落格之類的
只要可以自己插入html或javascript就可以
這邊可以調整邊框顯示等等然後放上自己的網頁喔
取得原始碼將此小工具新增至個人網頁

喔對了其實應該也支援google desktop的
但目前捲軸功能不知道為什麼出不來
而且寬度太窄的話顯示也不好看
所以有想試的人再跟我說
不然就先以上面兩種為主囉 :p

歡迎大家來玩看看 有bug的話歡迎報修
如果有人也想玩google gadget可以找我討論 ^^