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);
        }
  • 相关阅读:
    Visual Studio使用技巧笔记(引用程序集自动复制dll到引用项目目录)
    图解-Excel的csv格式特殊字符处理方式尝试笔记(个人拙笔)
    Nuget.config格式错误,请检查nuget.config配置文件
    securecrt切换会话(session)的显示方式
    javascript将分,秒,毫秒转换为xx天xx小时xx秒(任何语言通用,最通俗易懂)
    Http状态码枚举(摘自 Microsoft 程序集 System.dll)
    Visual Studio 提示某个dll文件(已在Microsoft Visual Studio 外对该文件进行了修改,是否重新加载它)
    IIS Express mime type 列表。
    为什么要 MySQL 迁移到 Maria DB
    降维技术---PCA
  • 原文地址:https://www.cnblogs.com/lz87/p/11697350.html
Copyright © 2011-2022 走看看