给定整数N ,任务是从0≤P,Q <2 N的范围内找到对的计数总数(P,Q) ,以使P OR Q = P XOR Q。由于计数可能非常大,因此将其打印为模数10 9 + 7。
例子:
Input: N = 1
Output: 3
Explanation: The pairs (P, Q) satisfying P OR Q = P XOR Q are (0, 0), (0, 1) and (1, 0). Hence output 3.
Input: N = 3
Output: 27
天真的方法:最简单的方法是生成所有可能的对(P,Q)并计算满足P OR Q = P XOR Q的对。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define MOD 1000000007
using namespace std;
// Function to calculate
// (x^y)%MOD
long long int power(int x, int y)
{
// Initialize result
long long int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0) {
// If y is odd, multiply
// x with result
if (y & 1)
res = (res * x) % MOD;
// y must be even now
// y = y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
void countPairs(int N)
{
// The upper bound is 2^N
long long int high = power(2, N);
// Stores the count of pairs
int count = 0;
// Generate all possible pairs
for (int i = 0; i < high; i++) {
for (int j = 0; j < high; j++) {
// Find XOR of both integers
int X = (i ^ j);
// Find OR of both integers
int Y = (i | j);
// If both are equal
if (X == Y) {
// Increment count
count++;
}
}
}
// Print count%MOD
cout << count % MOD << endl;
}
// Driver Code
int main()
{
int N = 10;
// Function Call
countPairs(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
static int MOD = 1000000007;
// Function to calculate
// (x^y)%MOD
static int power(int x, int y)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) != 0)
res = (res * x) % MOD;
// y must be even now
// y = y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
static void countPairs(int N)
{
// The upper bound is 2^N
int high = power(2, N);
// Stores the count of pairs
int count = 0;
// Generate all possible pairs
for (int i = 0; i < high; i++)
{
for (int j = 0; j < high; j++)
{
// Find XOR of both integers
int X = (i ^ j);
// Find OR of both integers
int Y = (i | j);
// If both are equal
if (X == Y)
{
// Increment count
count++;
}
}
}
// Print count%MOD
System.out.println(count % MOD);
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
// Function Call
countPairs(N);
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python program for the above approach
# Function to calculate
# (x^y)%MOD
def power(x, y):
MOD = 1000000007
# Initialize result
res = 1
# Update x if it is more than or
# equal to MOD
x = x % MOD
while (y > 0):
# If y is odd, multiply
# x with result
if (y & 1):
res = (res * x) % MOD
# y must be even now
# y = y/2
y = y >> 1
x = (x * x) % MOD
# Return (x^y)%MOD
return res
# Function to count total pairs
def countPairs( N):
MOD = 1000000007
# The upper bound is 2^N
high = power(2, N)
# Stores the count of pairs
count = 0
# Generate all possible pairs
for i in range(high):
for j in range(high):
# Find XOR of both integers
X = (i ^ j)
# Find OR of both integers
Y = (i | j)
# If both are equal
if (X == Y):
# Increment count
count += 1
# Print count%MOD
print(count % MOD)
# Driver Code
N = 10
# Function Call
countPairs(N)
# This code is contributed by rohitsingh07052.
C#
// C# program for the above approach
using System;
class GFG{
static int MOD = 1000000007;
// Function to calculate
// (x^y)%MOD
static int power(int x, int y)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) != 0)
res = (res * x) % MOD;
// y must be even now
// y = y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
static void countPairs(int N)
{
// The upper bound is 2^N
int high = power(2, N);
// Stores the count of pairs
int count = 0;
// Generate all possible pairs
for (int i = 0; i < high; i++)
{
for (int j = 0; j < high; j++)
{
// Find XOR of both integers
int X = (i ^ j);
// Find OR of both integers
int Y = (i | j);
// If both are equal
if (X == Y)
{
// Increment count
count++;
}
}
}
// Print count%MOD
Console.WriteLine(count % MOD);
}
// Driver Code
static public void Main()
{
int N = 10;
// Function Call
countPairs(N);
}
}
// This code is contributed by susmitakundugoaldanga.
Javascript
C++
// C++ program for the above approach
#include
#define MOD 1000000007
using namespace std;
// Function to find the value of (x^y)%MOD
long long int power(int x, int y)
{
// Initialize result
long long int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0) {
// If y is odd, multiply
// x with result
if (y & 1)
res = (res * x) % MOD;
// y must be even now, then
// update y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
void countPairs(int N)
{
// Finding 3^N % 10^9+7
cout << power(3, N);
}
// Driver Code
int main()
{
int N = 10;
// Function Call
countPairs(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static final int MOD = 1000000007;
// Function to find the value of (x^y)%MOD
static int power(int x, int y)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0)
{
// If y is odd, multiply
// x with result
if (y % 2== 1)
res = (res * x) % MOD;
// y must be even now, then
// update y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
static void countPairs(int N)
{
// Finding 3^N % 10^9+7
System.out.print(power(3, N));
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
// Function Call
countPairs(N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program for the above approach
MOD = 1000000007;
# Function to find the value of (x^y)%MOD
def power(x, y):
# Initialize result
res = 1;
# Update x if it is more than or
# equal to MOD
x = x % MOD;
while (y > 0):
# If y is odd, multiply
# x with result
if (y % 2 == 1):
res = (res * x) % MOD;
# y must be even now, then
# update y/2
y = y >> 1;
x = (x * x) % MOD;
# Return (x^y)%MOD
return res;
# Function to count total pairs
def countPairs(N):
# Finding 3^N % 10^9+7
print(power(3, N));
# Driver Code
if __name__ == '__main__':
N = 10;
# Function Call
countPairs(N);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
static readonly int MOD = 1000000007;
// Function to find the value of (x^y)%MOD
static int power(int x, int y)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0)
{
// If y is odd, multiply
// x with result
if (y % 2== 1)
res = (res * x) % MOD;
// y must be even now, then
// update y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
static void countPairs(int N)
{
// Finding 3^N % 10^9+7
Console.Write(power(3, N));
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
// Function Call
countPairs(N);
}
}
// This code contributed by shikhasingrajput
Javascript
输出:
59049
时间复杂度: O(2 2 * N )
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下观察结果:
- 将该对视为(P,Q) 。固定P ,然后找到所有满足该方程式的Q。因此,P中设置的所有位都不会在Q中设置。
- 对于P中的每个未设置位,有两种选择,即Q中的对应位可以为0或1 。
- 因此,对于任何P,如果P中未设置的位数为u(P),则Q的数量将为ans1 = 2 ^ u(P) 。
- 使用变量i在范围[0,N]上进行迭代,然后每2 i将贡献N C i 。让此计数为ans2 。
- 因此,对的计数由∑(ans1 * ans2)=(1 + 2) N = 3 N给出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define MOD 1000000007
using namespace std;
// Function to find the value of (x^y)%MOD
long long int power(int x, int y)
{
// Initialize result
long long int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0) {
// If y is odd, multiply
// x with result
if (y & 1)
res = (res * x) % MOD;
// y must be even now, then
// update y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
void countPairs(int N)
{
// Finding 3^N % 10^9+7
cout << power(3, N);
}
// Driver Code
int main()
{
int N = 10;
// Function Call
countPairs(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static final int MOD = 1000000007;
// Function to find the value of (x^y)%MOD
static int power(int x, int y)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0)
{
// If y is odd, multiply
// x with result
if (y % 2== 1)
res = (res * x) % MOD;
// y must be even now, then
// update y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
static void countPairs(int N)
{
// Finding 3^N % 10^9+7
System.out.print(power(3, N));
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
// Function Call
countPairs(N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program for the above approach
MOD = 1000000007;
# Function to find the value of (x^y)%MOD
def power(x, y):
# Initialize result
res = 1;
# Update x if it is more than or
# equal to MOD
x = x % MOD;
while (y > 0):
# If y is odd, multiply
# x with result
if (y % 2 == 1):
res = (res * x) % MOD;
# y must be even now, then
# update y/2
y = y >> 1;
x = (x * x) % MOD;
# Return (x^y)%MOD
return res;
# Function to count total pairs
def countPairs(N):
# Finding 3^N % 10^9+7
print(power(3, N));
# Driver Code
if __name__ == '__main__':
N = 10;
# Function Call
countPairs(N);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
static readonly int MOD = 1000000007;
// Function to find the value of (x^y)%MOD
static int power(int x, int y)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to MOD
x = x % MOD;
while (y > 0)
{
// If y is odd, multiply
// x with result
if (y % 2== 1)
res = (res * x) % MOD;
// y must be even now, then
// update y/2
y = y >> 1;
x = (x * x) % MOD;
}
// Return (x^y)%MOD
return res;
}
// Function to count total pairs
static void countPairs(int N)
{
// Finding 3^N % 10^9+7
Console.Write(power(3, N));
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
// Function Call
countPairs(N);
}
}
// This code contributed by shikhasingrajput
Java脚本
输出:
59049
时间复杂度:O(log N)
辅助空间:O(1)