给定数字N。任务是找到在2 * N点的圆中绘制N个和弦的方式的数量,以确保没有两个和弦相交。如果存在以一种方式而不是另一种方式存在的和弦,则两种方式是不同的。由于答案可能很大,因此以10 ^ 9 + 7为模。
例子:
Input : N = 2
Output : 2
If points are numbered 1 to 4 in clockwise direction,
then different ways to draw chords are:
{(1-2), (3-4)} and {(1-4), (2-3)}
Input :N = 1
Output : 1
方法:
如果我们在任意两个点之间绘制和弦,则当前的点集将分成两个较小的集S_1和S_2。如果我们从S_1中的一个点到S_2中的一个点绘制和弦,则它肯定会与我们刚刚绘制的和弦相交。因此,我们可以得出以下循环:
Ways(n) = sum[i = 0 to n-1] { Ways(i)*Ways(n-i-1) }.
上面的递归关系类似于等于2n C n /(n + 1)的第n个加泰罗尼亚数的递归关系。除了将分子除以面额之外,还应将数字与分母的模逆数相乘,因为在模域中不允许除法。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to calculate x^y %mod efficiently
int power(long long x, int y, int mod)
{
// Initialize the answer
long long res = 1;
while (y) {
// If power is odd
if (y & 1)
// Update the answer
res = (res * x) % mod;
// Square the base and half the exponent
x = (x * x) % mod;
y = (y >> 1);
}
// Return the value
return (int)(res % mod);
}
// Function to calculate ncr%mod efficiently
int ncr(int n, int r, int mod)
{
// Initialize the answer
long long res = 1;
// Calculate ncr in O(r)
for (int i = 1; i <= r; i += 1) {
// Multiply with the numerator factor
res = (res * (n - i + 1)) % mod;
// Calculate the inverse of factor of denominator
int inv = power(i, mod - 2, mod);
// Multiply with inverse value
res = (res * inv) % mod;
}
// Return answer value
return (int)(res%mod);
}
// Function to return the number
// of non intersecting chords
int NoOfChords(int A)
{
// define mod value
int mod = 1e9 + 7;
// Value of C(2n, n)
long long ans = ncr(2 * A, A, mod);
// Modulo inverse of (n+1)
int inv = power(A + 1, mod - 2, mod);
// Multiply with modulo inverse
ans = (ans * inv) % mod;
// Return the answer
return (int)(ans%mod);
}
// Driver code
int main()
{
int N = 2;
// Function call
cout << NoOfChords(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to calculate x^y %mod efficiently
static int power(long x, int y, int mod)
{
// Initialize the answer
long res = 1;
while (y != 0)
{
// If power is odd
if ((y & 1) == 1)
// Update the answer
res = (res * x) % mod;
// Square the base and half the exponent
x = (x * x) % mod;
y = (y >> 1);
}
// Return the value
return (int)(res % mod);
}
// Function to calculate ncr%mod efficiently
static int ncr(int n, int r, int mod)
{
// Initialize the answer
long res = 1;
// Calculate ncr in O(r)
for (int i = 1; i <= r; i += 1)
{
// Multiply with the numerator factor
res = (res * (n - i + 1)) % mod;
// Calculate the inverse of
// factor of denominator
int inv = power(i, mod - 2, mod);
// Multiply with inverse value
res = (res * inv) % mod;
}
// Return answer value
return (int)(res % mod);
}
// Function to return the number
// of non intersecting chords
static int NoOfChords(int A)
{
// define mod value
int mod = (int)(1e9 + 7);
// Value of C(2n, n)
long ans = ncr(2 * A, A, mod);
// Modulo inverse of (n+1)
int inv = power(A + 1, mod - 2, mod);
// Multiply with modulo inverse
ans = (ans * inv) % mod;
// Return the answer
return (int)(ans % mod);
}
// Driver code
public static void main(String[] args)
{
int N = 2;
// Function call
System.out.println(NoOfChords(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the above approach
# Function to calculate x^y %mod efficiently
def power(x, y, mod):
# Initialize the answer
res = 1
while (y):
# If power is odd
if (y & 1):
# Update the answer
res = (res * x) % mod
# Square the base and half the exponent
x = (x * x) % mod
y = (y >> 1)
# Return the value
return (res % mod)
# Function to calculate ncr%mod efficiently
def ncr(n, r, mod):
# Initialize the answer
res = 1
# Calculate ncr in O(r)
for i in range(1,r+1):
# Multiply with the numerator factor
res = (res * (n - i + 1)) % mod
# Calculate the inverse of factor of denominator
inv = power(i, mod - 2, mod)
# Multiply with inverse value
res = (res * inv) % mod
# Return answer value
return (res%mod)
# Function to return the number
# of non intersecting chords
def NoOfChords(A):
# define mod value
mod = 10**9 + 7
# Value of C(2n, n)
ans = ncr(2 * A, A, mod)
# Modulo inverse of (n+1)
inv = power(A + 1, mod - 2, mod)
# Multiply with modulo inverse
ans = (ans * inv) % mod
# Return the answer
return (ans%mod)
# Driver code
N = 2
# Function call
print(NoOfChords(N))
# This code is contributed by mohit kumar 29
C#
// Java implementation of the above approach
using System;
class GFG
{
// Function to calculate x^y %mod efficiently
static int power(long x, int y, int mod)
{
// Initialize the answer
long res = 1;
while (y != 0)
{
// If power is odd
if ((y & 1) == 1)
// Update the answer
res = (res * x) % mod;
// Square the base and half the exponent
x = (x * x) % mod;
y = (y >> 1);
}
// Return the value
return (int)(res % mod);
}
// Function to calculate ncr%mod efficiently
static int ncr(int n, int r, int mod)
{
// Initialize the answer
long res = 1;
// Calculate ncr in O(r)
for (int i = 1; i <= r; i += 1)
{
// Multiply with the numerator factor
res = (res * (n - i + 1)) % mod;
// Calculate the inverse of factor of denominator
int inv = power(i, mod - 2, mod);
// Multiply with inverse value
res = (res * inv) % mod;
}
// Return answer value
return (int)(res % mod);
}
// Function to return the number
// of non intersecting chords
static int NoOfChords(int A)
{
// define mod value
int mod = (int)(1e9 + 7);
// Value of C(2n, n)
long ans = ncr(2 * A, A, mod);
// Modulo inverse of (n+1)
int inv = power(A + 1, mod - 2, mod);
// Multiply with modulo inverse
ans = (ans * inv) % mod;
// Return the answer
return (int)(ans % mod);
}
// Driver code
public static void Main ()
{
int N = 2;
// Function call
Console.WriteLine(NoOfChords(N));
}
}
// This code is contributed by AnkitRai01
输出:
2
时间复杂度: O(N * log(mod))