题意:

给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n,并且要使得这m个数的或值最小。

思路:

从二进制的角度分析,如果这m个数中有一个数某一位为1,那么最后或起来这一位肯定是为1的,所以如果某一位为1了,那么我们尽量就让其余位也等于1。所以我们从最高位开始枚举,看看这一位是否需要为1,如果需要为1的话,那么剩下的几个数也尽量让这一位等于1。

代码:

不懂啊,还是看别人的代码写的,觉得有用就拿去吧。(卧槽,第一次开我的IDEA写java还不错的嘛)

import java.math.*;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
BigInteger n, m;
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for(int cas=0;cas<T;cas++){
n = in.nextBigInteger();
m = in.nextBigInteger();
int len = 0;
BigInteger ss = n;
while(ss.compareTo(BigInteger.ZERO)>0){
ss = ss.divide(BigInteger.valueOf(2));
len++;
}
BigInteger tmp;
BigInteger ans = new BigInteger("0");
for(int i = len; i>0; i--){
if(n.compareTo(BigInteger.ZERO)<=0) break;
tmp = BigInteger.valueOf(2).pow(i-1).subtract(BigInteger.valueOf(1));
BigInteger sum = tmp.multiply(m);
if(sum.compareTo(n)<0){
BigInteger num = n.divide(BigInteger.valueOf(2).pow(i-1));
if(num.compareTo(m)>0) num = m;
n = n.subtract(BigInteger.valueOf(2).pow(i-1).multiply(num));
ans = ans.add(BigInteger.valueOf(2).pow(i-1));
}
}
System.out.println(ans);
}
in.close();
}
}

2017CCPC秦皇岛 G题Numbers&&ZOJ3987【大数】的更多相关文章

  1. 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)

    Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...

  2. 2017 CCPC秦皇岛 G题 Numbers

    DreamGrid has a nonnegative integer . He would like to divide  into nonnegative integers  and minimi ...

  3. 2017CCPC秦皇岛 H题Prime Set&&ZOJ3988

    题意: 定义一种集合,只有两个数,两个数不同且加起来为素数.要从n个数里抽出数字组成该集合(数字也可以是1~n,这个好懵圈啊),要求你选择最多k个该种集合组成一个有最多元素的集合,求出元素的数量. 思 ...

  4. 2017CCPC秦皇岛 A题Balloon Robot&&ZOJ3981【模拟】

    题意: 一个机器人在长为M的圆形轨道上送气球,当机器人到达M号点的时候下一站会回到1号点,且全程不会停止运动.现在在长为M的轨道上有N个队伍,队伍会在某个时间做需要一个气球,机器人需要送过去.一共有P ...

  5. 2017CCPC秦皇岛 C题Crusaders Quest&&ZOJ3983【模拟+STL】

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3983 题意: 给定9个血槽,有三种物品,每次可以把连续相同的物品抵消 ...

  6. 2017CCPC秦皇岛 M题Safest Buildings&&ZOJ3993【复杂模拟】

    题意: 给出两个半径R,r,R表示第一次的大圈半径,r表示第二次的小圈半径.第一次大圈的圆心位于(0,0),第二次小圈的圆心未知,但在大圈内,给你一个n,然后给出n个屋子的位置,问这些屋子中,第二次在 ...

  7. 2017CCPC秦皇岛 E题String of CCPC&&ZOJ3985【模拟】

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3985 题意: 给定一个字符串,由c和p组成,可以添加c或者p. 串中出现一 ...

  8. 2017CCPC秦皇岛 L题One-Dimensional Maze&&ZOJ3992【模拟】

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3992 题意: 走迷宫,一个一维字符串迷宫,由'L'.'R'组成,分别 ...

  9. G - Self Numbers(2.2.1)

    G - Self Numbers(2.2.1) Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & % ...

随机推荐

  1. Ecplise 快捷键笔记

    1.显示出这个方法被哪些方法调用(Ctrl+Alt+H) 选中方法名,点右键,选“open call hierarchy”,其快捷键“Ctrl+Alt+H”,Eclipse就会显示出这个方法被哪些方法 ...

  2. 【洛谷P1601 A+B Problem(高精)】

    题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...

  3. 怎么理解本征无序态的蛋白质(Intrinsically disordered proteins)

    见维基的解释: An intrinsically disordered protein (IDP) is a protein that lacks a fixed or ordered three-d ...

  4. 第十四节、FAST角点检测(附源码)

    在前面我们已经陆续介绍了许多特征检测算子,我们可以根据图像局部的自相关函数求得Harris角点,后面又提到了两种十分优秀的特征点以及他们的描述方法SIFT特征和SURF特征.SURF特征是为了提高运算 ...

  5. c#大文件的拷贝

    using System.IO; namespace 数据流 { class Demo2 { private string _strSourcePath = @"D:\httpd-2.4.3 ...

  6. (二叉树 BFS) leetcode513. Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  7. (水题)P1424 小鱼的航程(改进版) 洛谷

    题目背景 原来的题目太简单,现改进让小鱼周末也休息,请已经做过重做该题. 题目描述 有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上和周末都休息(实行双休日),假设从周x(1<=x&l ...

  8. 解决python安装错误问题

    遇到的问题描述: An error occurred during the installation of assemblyA Microsoft.VC90. CRT, version=9-0.307 ...

  9. jvm垃圾收集器与内存分配策略

    一.垃圾回收 1.对象是否已经变为垃圾 1.1.引用计数法:给对象添加一个引用计数器,每当有地方引用它时,计数器就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象就是不可能再被使用的. 这 ...

  10. java用流的方式加载图片

    前端: <img ng-show="photo" class="vertical_b" id="photoImg" src=" ...