using
System;
public
class
Node
{
public
int
Value {
get
;
set
; }
public
int
Weight {
get
;
set
; }
public
Node Next {
get
;
set
; }
public
Node(
int
val,
int
w)
{
Value = val;
Weight = w;
Next =
null
;
}
}
public
class
Program
{
public
static
int
ProductOfBellNumbers(Node head)
{
int
product = 1;
Node current = head;
int
maxWeight = 0;
while
(current !=
null
)
{
if
(current.Weight > maxWeight)
{
maxWeight = current.Weight;
}
current = current.Next;
}
int
[,] bell =
new
int
[maxWeight + 1, maxWeight + 1];
bell[0, 0] = 1;
for
(
int
i = 1; i <= maxWeight; i++)
{
bell[i, 0] = bell[i - 1, i - 1];
for
(
int
j = 1; j <= i; j++)
{
bell[i, j] = bell[i - 1, j - 1] + bell[i, j - 1];
}
}
current = head;
while
(current !=
null
)
{
int
weight = current.Weight;
for
(
int
i = 0; i <= weight; i++)
{
if
(i < maxWeight + 1 && bell[i, 0] == weight)
{
product *= current.Value;
break
;
}
}
current = current.Next;
}
return
product;
}
public
static
void
PrintLinkedList(Node head)
{
Node current = head;
while
(current !=
null
)
{
Console.Write($
"{current.Value} ({current.Weight}) -> "
);
current = current.Next;
}
Console.WriteLine(
"NULL"
);
}
public
static
void
Main()
{
Node head1 =
new
Node(12, 4);
head1.Next =
new
Node(23, 1);
head1.Next.Next =
new
Node(4, 8);
head1.Next.Next.Next =
new
Node(14, 52);
int
result1 = ProductOfBellNumbers(head1);
Console.WriteLine($
"Output 1: {result1}"
);
while
(head1 !=
null
)
{
Node temp = head1;
head1 = head1.Next;
temp.Next =
null
;
}
Node head2 =
new
Node(4, 9);
head2.Next =
new
Node(5, 1);
head2.Next.Next =
new
Node(26, 5);
head2.Next.Next.Next =
new
Node(1, 12);
head2.Next.Next.Next.Next =
new
Node(56, 203);
int
result2 = ProductOfBellNumbers(head2);
Console.WriteLine($
"Output 2: {result2}"
);
while
(head2 !=
null
)
{
Node temp = head2;
head2 = head2.Next;
temp.Next =
null
;
}
}
}