给定一个数字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) }.
上述递归关系类似于第n个加泰罗尼亚数的递归关系,其等于2n C n / (n+1) 。将数字与分母的模倒数相乘,而不是将数字除以分母,因为在模域中不允许除法。
下面是上述方法的实现:
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
Javascript
输出:
2
时间复杂度: O(N*log(mod))
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。