POJ3252 Round Numbers

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=3252

思路:这道题可以用排列组合做,关键是多少位数插大于等于位数一半的0的种类...麻烦的地方有两点,一是解决最大的位数的问题,因为最大数不太可能是全1,如果是,那就简单了,如果不是,必须慢慢判断。其中可以使用DP的思想:从高往低慢慢推,找到一位是1的把它改为0,查找所有条件。然后把它改为1,修改条件状态。最后需要判断一下原数是不是Round Numbers。即没有把1改为0的情况。第二个麻烦是大数的处理,直接使用double是不行的,因为double不精确...所有在求排列组合时必须全部使用大数。这道题还是很有代表性的,大家可以做做。

 

ac代码:

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.math.BigInteger;
  4. public class Main {
  5.     static long[] arr = new long[] { 00126112749113206462,
  6.             848187234587554140303041456747122283229045491189,
  7.             923099197167537161117910415149469453172416160078293,
  8.             1271871572413465855097820419690941932042836017 };
  9.     public static void main(String[] args) throws Exception {
  10.         BufferedReader read = new BufferedReader(new InputStreamReader(
  11.                 System.in));
  12.         String[] s = read.readLine().split(" ");
  13.         int a = Integer.parseInt(s[0]);
  14.         int b = Integer.parseInt(s[1]);
  15.         System.out.println(jisuan(b) - jisuan(a - 1));
  16.     }
  17.     public static long jisuan(int a) {
  18.         String bi = Integer.toBinaryString(a);
  19.         long sum = arr[bi.length() - 1];
  20.         char[] ci = bi.toCharArray();
  21.         int c0 = 0;
  22.         int c1 = 1;
  23.         int temp, top, mid;
  24.         if (ci.length % 2 == 0) {
  25.             mid = ci.length / 2;
  26.         } else {
  27.             mid = ci.length / 2 + 1;
  28.         }
  29.         for (int i = 1; i < ci.length; i++) {
  30.             if (ci[i] == '0') {
  31.                 c0++;
  32.             } else {
  33.                 temp = mid - (c0 + 1);
  34.                 if (temp < 0) {
  35.                     temp = 0;
  36.                 }
  37.                 top = ci.length - i - 1;
  38.                 for (int j = temp; j <= top; j++) {
  39.                     sum += c(top, j);
  40.                 }
  41.                 c1++;
  42.             }
  43.         }
  44.         if (c0 >= c1) {
  45.             sum++;
  46.         }
  47.         return sum;
  48.     }
  49.     public static long c(int m, int n) {
  50.         BigInteger sub = new BigInteger("1");
  51.         BigInteger div = new BigInteger("1");
  52.         for (int i = 1; i <= n; i++) {
  53.             sub = sub.multiply(new BigInteger(i + ""));
  54.         }
  55.         for (int i = m; i >= m - n + 1; i--) {
  56.             div = div.multiply(new BigInteger(i + ""));
  57.         }
  58.         return div.divide(sub).longValue();
  59.     }
  60. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值