[Codeforces] Different ways to pack present.
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);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.