poj3101

不难,结果:

poj3101_第1张图片

程序:

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

public class Main
{
    public static void main(String[] args)
    {
    	Scanner cin = new Scanner(System.in);
    	while(cin.hasNext())
    	{
    		Integer n;
    		n = cin.nextInt();
    		long a[] = new long [1005];
    		BigInteger b[] = new BigInteger [1005];
    		BigInteger c[] = new BigInteger [1005];
    		long Maxx = 0;
    		for(int i = 0; i < n; i++)
    		{
    			a[i] = cin.nextLong();
    			if(a[i]>Maxx) Maxx = a[i];
    		}
    		int k = 0;
    		for(int i = 0; i < n; i++)
    		{
    			if(a[i] == Maxx) continue;
    			else
    			{
    				b[i] = BigInteger.valueOf(Maxx * a[i]);
    				c[i] = BigInteger.valueOf(2 * (Maxx - a[i]));
    				k = i;
    			}
    		}
    		BigInteger t = BigInteger.ONE;
    		for(int i = 0; i < n; i++)
    		{
    			if (a[i]==Maxx) continue;
				t = b[k].multiply(c[i]).gcd(b[i].multiply(c[k]));
				b[k] = b[k].multiply(c[i]).multiply(b[i].multiply(c[k])).divide(t);
				c[k] = c[k].multiply(c[i]);
				t = b[k].gcd(c[k]);
				b[k] = b[k].divide(t);
				c[k] = c[k].divide(t);
			}
			System.out.println(b[k]+" "+c[k]);
    	}
    }
}

很邪门的是,时限2000ms,java用了4094ms。。

另外更邪门的是,下面这个程序在本地,3 1 1 1这组都说除以0。。费解!

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

public class Main
{
    public static void main(String[] args)
    {
    	Scanner cin = new Scanner(System.in);
    	while(cin.hasNext())
    	{
    		Integer n;
    		n = cin.nextInt();
    		BigInteger a[] = new BigInteger [1005];
    		BigInteger b[] = new BigInteger [1005];
    		BigInteger c[] = new BigInteger [1005];
    		BigInteger Maxx = BigInteger.valueOf(0);
    		for(int i = 0; i < n; i++)
    		{
    			a[i] = cin.nextBigInteger();
    			if(a[i].compareTo(Maxx) == 1) Maxx = a[i];
    		}
    		int k = 0;
    		for(int i = 0; i < n; i++)
    		{
    			if(a[i] == Maxx) continue;
    			else
    			{
    				b[i] = Maxx.multiply(a[i]);
    				c[i] = (Maxx.subtract(a[i])).multiply(BigInteger.valueOf(2));
    				k = i;
    			}
    		}
    		BigInteger t = BigInteger.ONE;
    		for(int i = 0; i < n; i++)
    		{
    			if (a[i]==Maxx) continue;
				t = b[k].multiply(c[i]).gcd(b[i].multiply(c[k]));
				b[k] = b[k].multiply(c[i]).multiply(b[i].multiply(c[k])).divide(t);
				c[k] = c[k].multiply(c[i]);
				t = b[k].gcd(c[k]);
				b[k] = b[k].divide(t);
				c[k] = c[k].divide(t);
			}
			System.out.println(b[k]+" "+c[k]);
    	}
    }
}


你可能感兴趣的:(数论,ACM题解报告)