给定整数N。考虑前N个自然数A = {1,2,3,…,N}的集合。令M和P为A的两个非空子集。任务是计算(M,P)的无序对的数量,以使M和P为不交集。请注意,M和P的顺序无关紧要。
例子:
Input: N = 3
Output: 6
The unordered pairs are ({1}, {2}), ({1}, {3}),
({2}, {3}), ({1}, {2, 3}), ({2}, {1, 3}), ({3}, {1, 2}).
Input: N = 2
Output: 1
Input: N = 10
Output: 28501
方法:
- 假设集合{1、2、3、4、5、6}中只有6个元素。
- 当您以1作为第一子集的元素之一来计算子集的数量时,得出的结果是211。
- 用2作为第一个子集的元素之一来计算子集的数量,得出的结果是65,因为不包括1,因为集的顺序无关紧要。
- 以3作为第一个集合的元素之一的子集数得出65,在这里可以观察到一个模式。
- 图案:
5 = 3 * 1 + 2
19 = 3 * 5 + 4
65 = 3 * 19 + 8
211 = 3 * 65 + 16
S(n) = 3 * S(n-1) + 2(n – 2) - 扩展到n-> 2(表示元素n-2 + 1 = n-1的数量)
2 (n-2) * 3 (0) + 2 (n – 3) * 3 1 + 2 (n – 4) * 3 2 + 2 (n – 5) * 3 3 +…+ 2 (0) * 3 (n – 2)
根据几何级数,a + a * r 0 + a * r 1 +…+ a * r (n – 1) = a *(r n – 1)/(r – 1) - S(n)= 3 (n – 1) – 2 (n – 1) 。请记住,S(n)是子集的个数,其中第一个子集的元素之一为1,但要获得所需的结果,用T(n)= S(1)+ S(2)+ S(3)+…表示。 + S(n)
- 它还形成了几何级数,因此我们通过GP的总和公式进行计算
T(n)=(3 n – 2 n +1 + 1)/ 2 - 因为我们需要T(n)%p,其中p = 10 9 + 7
我们必须使用费马斯的小定理
a -1 = a (m – 2) (mod m)用于模除下面是上述方法的实现:
C++
// C++ implementation of the approach #include
using namespace std; #define p 1000000007 // Modulo exponentiation function long long power(long long x, long long y) { // Function to calculate (x^y)%p in O(log(y)) long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res % p; } // Driver function int main() { long long n = 3; // Evaluating ((3^n-2^(n+1)+1)/2)%p long long x = (power(3, n) % p + 1) % p; x = (x - power(2, n + 1) + p) % p; // From Fermats’s little theorem // a^-1 ? a^(m-2) (mod m) x = (x * power(2, p - 2)) % p; cout << x << "\n"; }
Java
// Java implementation of the approach class GFG { static int p = 1000000007; // Modulo exponentiation function static long power(long x, long y) { // Function to calculate (x^y)%p in O(log(y)) long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res % p; } // Driver Code public static void main(String[] args) { long n = 3; // Evaluating ((3^n-2^(n+1)+1)/2)%p long x = (power(3, n) % p + 1) % p; x = (x - power(2, n + 1) + p) % p; // From Fermats's little theorem // a^-1 ? a^(m-2) (mod m) x = (x * power(2, p - 2)) % p; System.out.println(x); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach p = 1000000007 # Modulo exponentiation function def power(x, y): # Function to calculate (x^y)%p in O(log(y)) res = 1 x = x % p while (y > 0): if (y & 1): res = (res * x) % p; y = y >> 1 x = (x * x) % p return res % p # Driver Code n = 3 # Evaluating ((3^n-2^(n+1)+1)/2)%p x = (power(3, n) % p + 1) % p x = (x - power(2, n + 1) + p) % p # From Fermats’s little theorem # a^-1 ? a^(m-2) (mod m) x = (x * power(2, p - 2)) % p print(x) # This code is contributed by Mohit Kumar
C#
// C# implementation of the approach using System; class GFG { static int p = 1000000007; // Modulo exponentiation function static long power(long x, long y) { // Function to calculate (x^y)%p in O(log(y)) long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res % p; } // Driver Code static public void Main () { long n = 3; // Evaluating ((3^n-2^(n+1)+1)/2)%p long x = (power(3, n) % p + 1) % p; x = (x - power(2, n + 1) + p) % p; // From Fermats's little theorem // a^-1 ? a^(m-2) (mod m) x = (x * power(2, p - 2)) % p; Console.Write(x); } } // This code is contributed by ajit.
输出:6