首页 > 专题系列 > Java解POJ > POJ 1365 Prime Land [解题报告] Java
2013
11-09

POJ 1365 Prime Land [解题报告] Java

Prime Land

问题描述 :

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,… denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only one way in the form of product of powers of prime factors. This implies that there is an integer kx and uniquely determined integers ekx, ekx-1, …, e1, e0, (ekx > 0), that The sequence

(ekx, ekx-1, … ,e1, e0)

is considered to be the representation of x in prime base number system.

It is really true that all numerical calculations in prime base number system can seem to us a little bit unusual, or even hard. In fact, the children in Prime Land learn to add to subtract numbers several years. On the other hand, multiplication and division is very simple.

Recently, somebody has returned from a holiday in the Computer Land where small smart things called computers have been used. It has turned out that they could be used to make addition and subtraction in prime base number system much easier. It has been decided to make an experiment and let a computer to do the operation “minus one”.

Help people in the Prime Land and write a corresponding program.

For practical reasons we will write here the prime base representation as a sequence of such pi and ei from the prime base representation above for which ei > 0. We will keep decreasing order with regard to pi.

输入:

The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains number 0.

输出:

The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x – 1 in prime base representation. All numbers in the line are separated by one space. There is no line in the output corresponding to the last “null” line of the input.

样例输入:

17 1
5 1 2 1
509 1 59 1
0

样例输出:

2 4
3 2
13 1 11 1 7 1 5 1 3 1 2 1

解题代码:

import java.util.*;
import java.math.*;

public class Main {

	/**
	 * @param args
	 */
	static int pp;
	static int[] p = new int[30000];
	static int[] w = new int[30000];
	static boolean[] q = new boolean[35000];

	static void init() {
		int i;
		for (i = 2; i * i < 35000; i++) {
			if (q[i])
				continue;
			p[pp++] = i;
			for (int j = i * i; j < 35000; j += i)
				q[j] = true;
		}
		for (; i < 32768; i++)
			if (!q[i])
				p[pp++] = i;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		init();
		Scanner in = new Scanner(System.in);
		String s;
		while (true) {
			s = in.nextLine();
			if (s.equals("0"))
				break;
			StringTokenizer aa = new StringTokenizer(s);
			BigInteger k = BigInteger.ONE;
			int a1, a2;
			while (aa.hasMoreTokens()) {
				a1 = Integer.parseInt(aa.nextToken());
				a2 = Integer.parseInt(aa.nextToken());
				for (int i = 0; i < a2; i++)
					k = k.multiply(BigInteger.valueOf(a1));
			}
			k=k.subtract(BigInteger.ONE);
			for (int i = pp - 1; i >= 0; --i) {
				w[i] = 0;
				while (k.mod(BigInteger.valueOf(p[i])).equals(BigInteger.ZERO)) {
					w[i]++;
					k = k.divide(BigInteger.valueOf(p[i]));
				}
				if (w[i] > 0)
					if (k.equals(BigInteger.ONE)) {
						System.out.println(p[i] + " " + w[i]);
						break;
					} else
						System.out.print(p[i] + " " + w[i] + " ");
			}
		}
	}

}

  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.

  2. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  4. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.