ZOJ3477&JAVA大数类

转:http://blog.csdn.net/sunkun2013/article/details/11822927

 import java.util.*;
import java.math.BigInteger;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Switch {
public static void main(String[] args) {
// TODO Auto-generated method stub BigInteger a = new BigInteger("997945672345647898769");
BigInteger b = new BigInteger("59164562345721340329"); System.out.println("两大数运算结果为:"); BigInteger c = a.add(b);
BigInteger d = a.subtract(b);
BigInteger e = a.multiply(b);
BigInteger f = a.divide(b); // 若除数为0,程序会自动抛出异常
BigInteger g = a.remainder(b); System.out.println(a + " + " + b + " = " + c);
System.out.println(a + " - " + b + " = " + d);
System.out.println(a + " * " + b + " = " + e);
System.out.println(a + " / " + b + " = " + f);
System.out.println(a + " % " + b + " = " + g);
}
}

Doraemon's Number Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Doraemon and Nobita are playing a number game. First, Doraemon will give Nobita N positive numbers. Then Nobita can deal with these numbers for two rounds. Every time Nobita can delete i (2 ≤ i ≤ K) numbers from the number set. Assume that the numbers deleted is a[ 1 ], a[ 2 ] ... a[ i ]. There should be a new number X= ( a[ 1 ] * a[ 2 ] * ... * a[ i ] + 1 ) to be inserted back into the number set. The operation will be applied to the number set over and over until there remains only one number in the set. The number is the result of round. Assume two results A and B are produced after two rounds. Nobita can get |A - B| scores.

Now Nobita wants to get the highest score. Please help him.

Input

Input will contain no more than 10 cases. The first line of each case contains two positive integers N and K (1 ≤ N ≤ 100, 1 ≤ K ≤ 50). The following line contains Npositive integers no larger than 50, indicating the numbers given by Doraemon.

Output

For each case, you should output highest score in one line.

Sample Input

6 3
1 3 4 10 7 15

Sample Output

5563

Hint

For most cases, N ≤ 20

题意:在一个正数集合中,可以删去任意i(2<=i<=k)个数,加上这i个数的乘积+1的数,最后只剩下一个数。因为有多种情况每种情况对应一个数,问:在这些只剩下一个数的数中选取两个数绝对值之差最大(|A-B|)的即是答案。

思路:猜想到:在数集中每次删去最小的两个数加上一个数,这样最后剩下的一个数是最大的;同样的,在数集中每次删去最多个(即K)数再加上一个数,这样最后剩下的一个数是最小的。

难点:由于这里可能达到50个数相乘,所以考虑到用JAVA大数类。

 import java.util.*;
import java.math.BigInteger;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
//int i;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
while(in.hasNext())
{
int n=in.nextInt();
int k=in.nextInt();
if(n==1)
{
int e=in.nextInt();
System.out.println("0");
continue;
}
BigInteger a,b,one,temp;
PriorityQueue<BigInteger> minq=new PriorityQueue<BigInteger>();
PriorityQueue<BigInteger> maxq=new PriorityQueue<BigInteger>(1000,Collections.reverseOrder());
for(int i=0;i<n;i++)
{
a=in.nextBigInteger();
minq.add(a);
maxq.add(a);
}
one=BigInteger.ONE;
while(minq.size()>1)
{
a=minq.peek();
minq.remove(a);
b=minq.peek();
minq.remove(b);
a=a.multiply(b);
a=a.add(BigInteger.ONE);
minq.add(a);
}
one=minq.peek(); while(maxq.size()>k)
{
temp=BigInteger.ONE;
for(int i=0;i<k;i++)
{
a=maxq.peek();
maxq.remove(a);
temp=temp.multiply(a);
}
temp=temp.add(BigInteger.ONE);
maxq.add(temp);
}
temp=BigInteger.ONE;
while(!maxq.isEmpty())
{
a=maxq.peek();
maxq.remove(a);
temp=temp.multiply(a);
}
temp=temp.add(BigInteger.ONE);
//System.out.println(temp);
//System.out.println(one);
//System.out.println(temp);
System.out.println(one.subtract(temp));
}
} }
上一篇:cocos2d-x编译到android平台后,增加返回键和菜单键支持


下一篇:React Starter Kit 中文文档