转: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));
}
} }

ZOJ3477&JAVA大数类的更多相关文章

  1. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

  2. JAVA大数类练手

    今天突然看到了OJ上的大数类题目,由于学习了一点大数类的知识.果断水了6道题......都是非常基础的.就当的练手的吧. 学到的只是一些大数类的基本操作.以后多做点这样的题,争取熟练运用水大数题... ...

  3. Java大数类介绍

    java能处理大数的类有两个高精度大整数BigInteger 和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math. ...

  4. HDU高精度总结(java大数类)

      HDU1002   A + B Problem II [题意]大数相加 [链接]http://acm.hdu.edu.cn/showproblem.php?pid=1002 Sample Inpu ...

  5. JAVA大数类—基础操作(加减乘除、取模、四舍五入、设置保留位数)

    当基础数据类型长度无法满足需求时可以使用大数类 构造方法接受字符串为参数 BigInteger bInt = new BigInteger("123123"); BigDecima ...

  6. JAVA - 大数类详解

    写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...

  7. Java 大数类BigInteger和BigDecimal的基本函数

    在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运 ...

  8. Java大数类BigDecimal及八种舍入模式的介绍

    BigDecimal的引入 在利用Java编程语言开发银行.金融类等需要对数值进行高精度计算的软件时,我们经常使用BigDecimal和BigInteger这两个大数据类,而不是常见的int.long ...

  9. Java 大数类

    划分结果存在数组.供应商下标0 在剩下的标记1 import java.math.BigInteger; import java.util.Scanner; public class Main { p ...

随机推荐

  1. linux之chdir函数解析

    [lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c  /********************* ...

  2. Maxiee的Vim入门日记(4)——安装windows下的Cscope

    Maxiee今天又学到了一个插件——Cscope.Cscope 是一款用于查看大型工程中的代码的软件.它使用方便,支持快速查找 C Symbol.function 等在工程中所有出现的位置,而不用自己 ...

  3. 基于RSA的加密/解密示例C#代码

    using System;using System.Security.Cryptography;using System.Text; class RSACSPSample{ static void M ...

  4. Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8577789 在Android系统中,Activ ...

  5. fopen 參数具体解释

    fopen fopen(打开文件) 相关函数 open,fclose 表头文件 #include<stdio.h> 定义函数 FILE * fopen(const char * path, ...

  6. IoC容器Autofac之实例优化(三)

    回顾之前的代码 //这个类的作用是筛选出MPG类型的电影 public class MPGMovieLister { public Movie[] GetMPG() { var finder = Mo ...

  7. 自制获取data-自定义属性

    jQuery.fn.dataset = function(attr, val) { // 获取数据集 if (arguments.length == 0) { var dataset = {}; jQ ...

  8. 转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!

    怎么获取Dictionary<int, string>的值?我知道这个是键值对的,我知道可以根据key得到value,但关键是现在连key也不知道啊就是想让这个显示在listbox中,应该 ...

  9. 数学期望和概率DP题目泛做(为了对应AD的课件)

    题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...

  10. ARM指令和Thumb指令区别

    Thumb指令集 ]的问题而提出的,它具有16为的代码密度.Thumb不是一个完整的体系结构,不能指望处理程序只执行Thumb指令而不支持ARM指令集.因此,Thumb指令只需要支持通用功能,必要时, ...