• ZOJ3477&JAVA大数类


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

     1 import java.util.*;
     2 import java.math.BigInteger;
     3 import java.util.Collections;
     4 import java.util.PriorityQueue;
     5 import java.util.Scanner;
     6 public class Switch {
     7 public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9 
    10         BigInteger a = new BigInteger("997945672345647898769");
    11         BigInteger b = new BigInteger("59164562345721340329");
    12 
    13             System.out.println("两大数运算结果为:");
    14         
    15         BigInteger c = a.add(b);
    16         BigInteger d = a.subtract(b);
    17         BigInteger e = a.multiply(b);
    18         BigInteger f = a.divide(b); // 若除数为0,程序会自动抛出异常
    19         BigInteger g = a.remainder(b);
    20         
    21             System.out.println(a + " + " + b + " = " + c);    
    22                 System.out.println(a + " - " + b + " = " + d);
    23             System.out.println(a + " * " + b + " = " + e);
    24         System.out.println(a + " / " + b + " = " + f);
    25         System.out.println(a + " % " + b + " = " + g);
    26     }
    27 }
    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大数类。

     1 import java.util.*;
     2 import java.math.BigInteger;
     3 import java.util.Collections;
     4 import java.util.PriorityQueue;
     5 import java.util.Scanner;
     6 public class Main {
     7  //int i;
     8     public static void main(String[] args) {
     9       Scanner in=new Scanner(System.in);
    10     while(in.hasNext())
    11     {
    12         int n=in.nextInt();
    13         int k=in.nextInt();
    14         if(n==1)
    15         {
    16             int e=in.nextInt();
    17             System.out.println("0");
    18             continue;
    19         }
    20         BigInteger a,b,one,temp;
    21         PriorityQueue<BigInteger> minq=new PriorityQueue<BigInteger>();
    22         PriorityQueue<BigInteger> maxq=new PriorityQueue<BigInteger>(1000,Collections.reverseOrder());
    23         for(int i=0;i<n;i++)
    24         {
    25             a=in.nextBigInteger();
    26             minq.add(a);
    27             maxq.add(a);
    28         }
    29          one=BigInteger.ONE;
    30         while(minq.size()>1)
    31         {
    32             a=minq.peek();
    33             minq.remove(a);
    34             b=minq.peek();
    35             minq.remove(b);
    36             a=a.multiply(b);
    37             a=a.add(BigInteger.ONE);
    38             minq.add(a);
    39         }
    40         one=minq.peek();
    41         
    42         while(maxq.size()>k)
    43         {
    44             temp=BigInteger.ONE;
    45             for(int i=0;i<k;i++)
    46             {
    47                 a=maxq.peek();
    48                 maxq.remove(a);
    49                 temp=temp.multiply(a);
    50             }
    51             temp=temp.add(BigInteger.ONE);
    52             maxq.add(temp);
    53         }
    54         temp=BigInteger.ONE;
    55         while(!maxq.isEmpty())
    56         {
    57             a=maxq.peek();
    58             maxq.remove(a);
    59             temp=temp.multiply(a);
    60         }
    61         temp=temp.add(BigInteger.ONE);
    62         //System.out.println(temp);
    63         //System.out.println(one);
    64         //System.out.println(temp);
    65         System.out.println(one.subtract(temp));
    66 }
    67 }
    68    
    69 }
  • 相关阅读:
    推荐一本SQL经典书籍
    准备升级包包版游戏大厅
    《博客园精华集软件工程分册》第三轮筛选结果
    (翻译)《Expert .NET 2.0 IL Assembler》 第八章 基本类型和签名(一)
    如何输入人名间的顿号
    推荐一个下名人传记电子书的好地方
    asp.net 2.0 中使用web.config存储数据库连接字符串
    Asp.Net小技巧之在client端调用server端事件:
    C#编码好习惯
    把ip转换成对应的城市名
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4685056.html
Copyright © 2020-2023  润新知