2013
11-11

Hamming Problem

For each three prime numbers p1, p2 and p3, let’s define Hamming sequence Hi(p1, p2, p3), i=1, … as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3.

For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, …

So H5(2, 3, 5)=6.

In the single line of input file there are space-separated integers p1 p2 p3 i.

The output file must contain the single integer – Hi(p1, p2, p3). All numbers in input and output are less than 10^18.

7 13 19 100

26590291

//* @author: [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
TreeSet< Long> t=new TreeSet< Long>();
Scanner in=new Scanner(System.in);
long a=in.nextInt();
long b=in.nextInt();
long c=in.nextInt();
int d=in.nextInt();
long[] arr=new long[d+1];
arr[0]=1;
int x1=0,x2=0,x3=0;
for(int i=1;i<=d;i++)
{
long y1=arr[x1]*a;
long y2=arr[x2]*b;
long y3=arr[x3]*c;
long min=Math.min(y1, y2);
min=Math.min(min, y3);
arr[i]=min;
if(min==y1) x1++;
if(min==y2) x2++;
if(min==y3) x3++;
}
System.out.println(arr[d]);

}
}

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