原文:https://www . geeksforgeeks . org/除最小和最大元素之外的所有 k 大小子序列的乘积/

给定一个包含 n 元素和一个整数 k 的数组[]。任务是计算大小为 k 的子序列的所有元素的乘积,除了每个子序列的最小和最大元素。 :由于答案可能会很大,所以将最终答案打印为 10 9 7 的 mod。

示例:

input : arr[] = {1, 2, 3 4}, k = 3
output : 36
*subsequences of length 3 are*:
{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}
excluding minimum and maximum elements from 
each of the above subsequences, product will be:
*(2 * 2 * 3 * 3) = 36*.
input : arr[] = {10, 5, 16, 6}, k=3
output : 3600 

天真方法:一个简单的方法是逐个生成所有可能的子序列,然后将除最大值和最小值之外的所有元素相乘,并进一步将所有元素相乘。因为总共会有【n】c【k】个子序列,所有子序列都有 k-2 个元素需要相乘,这是一项繁琐的工作。

高效方法:想法是首先对数组进行排序,因为我们是否考虑子序列或子集并不重要。 现在逐一统计每个元素的出现次数。 总的来说,一个数可以出现在(n-1)c(k-1)子序列中,其中(i)c(k-1)次作为最大元素出现,而(n-i-1)c(k-1)【t20 t21】次作为最小元素出现 因此,总共会出现元素:

(n-1)c(k-1)  - (i)c(k-1) - (n-i-1)c(k-1) times. (let's say it x)

所以,首先我们要计算每个元素 a[i]的 x,然后乘以 a[i] x 倍。即()。 既然,对于大型阵列来说计算这个太难了,那么我们就用费马小定理

下面是上述方法的实现:

c

// c   program to find product of all
// subsequences of size k except the
// minimum and maximum elements
#include 
using namespace std;
#define mod 1000000007
#define ll long long
#define max 101
// 2d array to store value of
// combinations ncr
ll c[max - 1][max - 1];
ll power(ll x, unsigned ll y)
{
    unsigned ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) {
            res = (res * x) % mod;
        }
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res % mod;
}
// function to pre-calculate value of all
// combinations ncr
void combi(int n, int k)
{
    int i, j;
    for (i = 0; i <= n; i  ) {
        for (j = 0; j <= min(i, k); j  ) {
            if (j == 0 || j == i)
                c[i][j] = 1;
            else
                c[i][j] = (c[i - 1][j - 1] % mod
                              c[i - 1][j] % mod) % mod;
        }
    }
}
// function to calculate product of all subsequences
// except the minimum and maximum elements
unsigned ll product(ll a[], int n, int k)
{
    unsigned ll ans = 1;
    // sorting array so that it becomes easy
    // to calculate the number of times an
    // element will come in first or last place
    sort(a, a   n);
    // an element will occur 'powa' times in total
    // of which 'powla' times it will be last element
    // and 'powfa' times it will be first element
    ll powa = c[n - 1][k - 1];
    for (int i = 0; i < n; i  ) {
        ll powla = c[i][k - 1];
        ll powfa = c[n - i - 1][k - 1];
        // in total it will come
        // powe = powa-powla-powfa times
        ll powe = ((powa % mod) - (powla   powfa) % mod   mod) % mod;
        // multiplying a[i] powe times using
        // fermat little theorem under modulo
        // mod for fast exponentiation
        unsigned ll mul = power(a[i], powe) % mod;
        ans = ((ans % mod) * (mul % mod)) % mod;
    }
    return ans % mod;
}
// driver code
int main()
{
    // pre-calculation of all combinations
    combi(100, 100);
    ll arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof arr[0];
    int k = 3;
    unsigned ll ans = product(arr, n, k);
    cout << ans << endl;
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find product of all
// subsequences of size k except the
// minimum and maximum elements
import java.util.arrays;
class gfg
{
static int mod= 1000000007;
static int max =101;
// 2d array to store value of
// combinations ncr
static long c[][] = new long[max ][max];
static long power(long x, long y)
{
    long res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y % 2== 1)
        {
            res = (res * x) % mod;
        }
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res % mod;
}
// function to pre-calculate value of all
// combinations ncr
static void combi(int n, int k)
{
    int i, j;
    for (i = 0; i <= n; i  )
    {
        for (j = 0; j <= math.min(i, k); j  )
        {
            if (j == 0 || j == i)
                c[i][j] = 1;
            else
                c[i][j] = (c[i - 1][j - 1] % mod
                              c[i - 1][j] % mod) % mod;
        }
    }
}
// function to calculate product of all subsequences
// except the minimum and maximum elements
static long product(long a[], int n, int k)
{
    long ans = 1;
    // sorting array so that it becomes easy
    // to calculate the number of times an
    // element will come in first or last place
    arrays.sort(a);
    // an element will occur 'powa' times in total
    // of which 'powla' times it will be last element
    // and 'powfa' times it will be first element
    long powa = c[n - 1][k - 1];
    for (int i = 0; i < n; i  )
    {
        long powla = c[i][k - 1];
        long powfa = c[n - i - 1][k - 1];
        // in total it will come
        // powe = powa-powla-powfa times
        long powe = ((powa % mod) - (powla   powfa) % mod   mod) % mod;
        // multiplying a[i] powe times using
        // fermat little theorem under modulo
        // mod for fast exponentiation
        long mul = power(a[i], powe) % mod;
        ans = ((ans % mod) * (mul % mod)) % mod;
    }
    return ans % mod;
}
// driver code
public static void main(string[] args)
{
    // pre-calculation of all combinations
    combi(100, 100);
    long arr[] = { 1, 2, 3, 4 };
    int n = arr.length;
    int k = 3;
    long ans = product(arr, n, k);
    system.out.println(ans);
}
}
/* this code contributed by princiraj1992 */

python 3

# python 3 program to find product of all
# subsequences of size k except the
# minimum and maximum elements
mod = 1000000007
max = 101
# 2d array to store value of
# combinations ncr
c = [[0 for i in range(max)] for j in range(max)]
def power(x,y):
    res = 1
    x = x % mod
    while (y > 0):
        if (y & 1):
            res = (res * x) % mod
        y = y >> 1
        x = (x * x) % mod
    return res % mod
# function to pre-calculate value of all
# combinations ncr
def combi(n, k):
    for i in range(n   1):
        for j in range(min(i, k)   1):
            if (j == 0 or j == i):
                c[i][j] = 1
            else:
                c[i][j] = (c[i - 1][j - 1] % mod  
                            c[i - 1][j] % mod) % mod
# function to calculate product of all subsequences
# except the minimum and maximum elements
def product(a, n, k):
    ans = 1
    # sorting array so that it becomes easy
    # to calculate the number of times an
    # element will come in first or last place
    a.sort(reverse = false)
    # an element will occur 'powa' times in total
    # of which 'powla' times it will be last element
    # and 'powfa' times it will be first element
    powa = c[n - 1][k - 1]
    for i in range(n):
        powla = c[i][k - 1]
        powfa = c[n - i - 1][k - 1]
        # in total it will come
        # powe = powa-powla-powfa times
        powe = ((powa % mod) - (powla   powfa) % mod   mod) % mod
        # multiplying a[i] powe times using
        # fermat little theorem under modulo
        # mod for fast exponentiation
        mul = power(a[i], powe) % mod
        ans = ((ans % mod) * (mul % mod)) % mod
    return ans % mod
# driver code
if __name__ == '__main__':
    # pre-calculation of all combinations
    combi(100, 100)
    arr = [1, 2, 3, 4]
    n = len(arr)
    k = 3
    ans = product(arr, n, k)
    print(ans)
# this code is contributed by
# surendra_gangwar

c

// c# program to find product of all
// subsequences of size k except the
// minimum and maximum elements
using system;
class gfg
{
static int mod = 1000000007;
static int max = 101;
// 2d array to store value of
// combinations ncr
static long [,]c = new long[max, max];
static long power(long x, long y)
{
    long res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y % 2 == 1)
        {
            res = (res * x) % mod;
        }
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res % mod;
}
// function to pre-calculate value
// of all combinations ncr
static void combi(int n, int k)
{
    int i, j;
    for (i = 0; i <= n; i  )
    {
        for (j = 0;
             j <= math.min(i, k); j  )
        {
            if (j == 0 || j == i)
                c[i, j] = 1;
            else
                c[i, j] = (c[i - 1, j - 1] % mod  
                           c[i - 1, j] % mod) % mod;
        }
    }
}
// function to calculate product of
// all subsequences except
// the minimum and maximum elements
static long product(long []a, int n, int k)
{
    long ans = 1;
    // sorting array so that it becomes easy
    // to calculate the number of times an
    // element will come in first or last place
    array.sort(a);
    // an element will occur 'powa' times
    // in total of which 'powla' times it
    // will be last element and 'powfa' times
    // it will be first element
    long powa = c[n - 1, k - 1];
    for (int i = 0; i < n; i  )
    {
        long powla = c[i, k - 1];
        long powfa = c[n - i - 1, k - 1];
        // in total it will come
        // powe = powa-powla-powfa times
        long powe = ((powa % mod) -    
                     (powla   powfa) %
                      mod   mod) % mod;
        // multiplying a[i] powe times using
        // fermat little theorem under modulo
        // mod for fast exponentiation
        long mul = power(a[i], powe) % mod;
        ans = ((ans % mod) *
               (mul % mod)) % mod;
    }
    return ans % mod;
}
// driver code
static public void main ()
{
    // pre-calculation of all combinations
    combi(100, 100);
    long []arr = { 1, 2, 3, 4 };
    int n = arr.length;
    int k = 3;
    long ans = product(arr, n, k);
    console.writeline(ans);
}
}
// this code contributed by ajit

java 描述语言


output: 

36