zoukankan      html  css  js  c++  java
  • [Codeforces] Different ways to pack present.

    Given n kinds of present and m boxes. Find out the different ways to pack all boxes with presents, using the following rules.

    1. Each box can not have more than one present of each kind.

    2. For each kind at least one present should be packed into some box.  

    3. A box can be empty as long as rule 2 is met. 

    Constraints:

    1 <= n <= 10^9

    1 <= m <= 10^9

    The answer will be huge, return the modulo 10^9 + 7 of the original answer.

    This is a straightforward combinatorics problem. The only two restrictions are (1), each box can not have the same kind of present; (2) each present must be packed into at least one box. Consider any one kind of present, given m boxes, there are 2^m permutations in regard with this present.(For each box, either pack this present into it or not). For restriction No.2, we need to subtract 1 from 2^m to exclude this case: no box has this present. All n kind of presents are independent with each other, so the answer is (2^m - 1)^n. Computing this value in O(N) time is too slow since N can go up to 10^9. We need to use a faster pow method in O(logN) time.

    Solution 1. Java BigInteger

    It looks like the internal implementation of modPow(N, mod) is O(log N) instead of O(N). As it has the same performance with solution 2 and 3. But this needs to be confirmed.

         public static void main(String[] args) {
            FastScanner in = new FastScanner(System.in);
            PrintWriter out = new PrintWriter(System.out);
     
            BigInteger p = BigInteger.valueOf(in.nextInt());
            BigInteger b = BigInteger.valueOf(in.nextInt());
            BigInteger mod = BigInteger.valueOf((long)1e9 + 7);
     
            BigInteger res = BigInteger.valueOf(2);
            res = res.modPow(b, mod).subtract(BigInteger.ONE);
            res = res.modPow(p, mod);
            out.println(res.intValue());
     
            out.close();
        }    

    Solution 2. Implement binary pow method(bottom up).

        public static void main(String[] args) {
            FastScanner in = new FastScanner(System.in);
            PrintWriter out = new PrintWriter(System.out);
     
            int p = in.nextInt();
            int b = in.nextInt();
     
            out.println(task(p, b));
            out.close();
        }
     
        private static int task(int p, int b) {
            int mod = (int)1e9 + 7;
            int x = modPow(2, b, mod) - 1;
            return modPow(x, p, mod);
        }
     
        private static int modPow(int x, int n, int mod) {
            if(n == 0) {
                return 1;
            }
            long coeff = 1;
            long base = x;
            while(n > 1) {
                if(n % 2 != 0) {
                    coeff = (coeff * base % mod);
                }
                base = (base * base % mod);
                n = n / 2;
            }
            long res = coeff * base % mod;
            return (int)res;
        }
    }

    Solution 3. Implement binary pow method(top down). 

        public static void main(String[] args) {
            FastScanner in = new FastScanner(System.in);
            PrintWriter out = new PrintWriter(System.out);
     
            int p = in.nextInt();
            int b = in.nextInt();
     
            out.println(task(p, b));
            out.close();
        }
     
        private static int task(int p, int b) {
            int mod = (int)1e9 + 7;
            int x = modPow(2, b, mod) - 1;
            return modPow(x, p, mod);
        }
     
        private static int modPow(int x, int n, int mod) {
            if(n == 0) {
                return 1;
            }
            long half = modPow(x, n / 2, mod);
            long res = half * half % mod;
            if(n % 2 == 0) {
                return (int)res;
            }
            return (int)(res * x % mod);
        }
  • 相关阅读:
    如何说明白代码评审
    面试感悟----一名3年工作经验的程序员应该具备的技能(转载自@五月的仓颉)
    根据ip地址从第三方接口获取详细的地理位置
    linux安装telnet遇到的问题
    redis脑图
    数据库相关面试题
    logback系列一:名词解释
    java并发编程系列一、多线程
    logback系列二:logback在项目中的应用
    rocketmq特性(features)
  • 原文地址:https://www.cnblogs.com/lz87/p/11697350.html
Copyright © 2011-2022 走看看