n个数字的序列 - 计算"幸运"的所有可能的k-子序列数字

时间:2015-11-16 22:16:34

标签: c++ algorithm math combinatorics binomial-coefficients

我遇到一个任务有问题,所以如果你能帮助我一点点。

  

数字是幸运的&#34;或者&#34;运气不好&#34;。数字是&#34;幸运&#34;就好   数字7        或者每个数字都是4.所以&#34;幸运&#34;数字例如是4,44,7,77。        &#34;不幸&#34;是其他数字        你将获得n元素序列和数字K.你的任务是        计算所有可能的k元素子序列的数量,它们满足一个        条件。条件是在子序列中不能是两个相同的&#34;幸运&#34;        数字。所以例如那里没有77和77 ......        所有可能的k元素的子序列mod 10 ^ 9 + 7的输出数量        0&lt; N,K < 10 ^ 5

Few examples:
Input:

5 2
7 7 3 7 77
Output:

7

Input:

5 3
3 7 77 7 77
Output:

4

Input:

34 17
14 14 14 ... 14 14 14
Output:

333606206

我的代码似乎有用,但是当我尝试计算二项式系数时它太慢了。我正在使用地图。在字符串I中以字符串格式存储数字。在第二个 - 地图 - 部分地图是数字,表示使用的数字(在第一个地图参数中)的次数。所以现在我已经存储了所有不幸的#34;数字存储在一起。也是每一个&#34;幸运&#34;数字在一起。当我像这样存储它时,我只计算所有乘法。例如:

Input 
5 2
3 7 7 77 7
Are stored like this:  map["other"] = 1 map["7"] = 3 map["77"] = 1 
Because k = 2 --> result is: 1*3 + 1*1 + 1*3 = 7.

我认为问题在于计算二项式系数。对于第三个例子,它需要计算(34选择17)并计算很长时间。我找到了this articlethis,但我不明白它们是怎样的正在解决这个问题。

我的代码:

#include<iostream>
#include<string>
#include<map>
#include <algorithm>
#include <vector>

using namespace std;

int binomialCoeff(int n, int k)
{
    // Base Cases
    if (k == 0 || k == n)
        return 1;

    // Recur
    return  binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k);
}

int main()
{
    int n, k;
    cin >> n >> k;
    map<string, int> mapa;  // create map, string is a number, int represents number of used string-stored numbers ---> so if 7 was used two times, in the map it will be stored like this mapa["7"] == 2 and so on)
    for (int i = 0; i < n; i++)  // I will load number as string, if this number is "lucky" - digist are all 7 or all 4
    {                            // every "unlucky" numbers are together, as well as all same "lucky" numbers ---> so 77 and 77 will be stored in one element....
        string number;
        cin >> number;
        char digit = number[0];
        bool lucky = false;
        if (digit == '7' || digit == '4')
            lucky = true;
        for (int j = 1; j < number.length(); j++) {
            if (digit != '7' && digit != '4')
                break;
            if (number[j] != digit) {
                lucky = false;
                break;
            }
        }
        if (lucky)
            mapa[number]++;
        else
            mapa["other"]++;
    }
    vector<bool> v(mapa.size());
    bool lack = k > mapa.size(); //lack of elements in map --> it is when mapa.size() < k; i. e. number of elements in array can't make k-element subsequence.
    int rest = lack ? k - mapa.size() + 1 : 1;  // how many elements from "unlucky" numbers I must choose, so it makes base for binomial coefficient (n choose rest)
    if (lack)  //if lack is true, different size of vector
        fill(v.begin() + mapa.size(), v.end(), true);
    else
        fill(v.begin() + k, v.end(), true);
    int *array = new int[mapa.size()];   //easier to manipulate with array for me
    int sum = 0;  
    int product = 1;
    int index = 0; 
    for (map<string, int> ::iterator pos = mapa.begin(); pos != mapa.end(); ++pos)  // create array from map
    {
        if (lack && pos->first == "other") {    //if lack of elements in map, the number in elemets representing "unlucky" numbers will be binomial coefficient (mapa["other] choose rest)
            array[index++] = binomialCoeff(mapa["other"], rest);
            continue;
        }
        array[index++] = pos->second;
    }
    do {   // this will create every posible multiplication for k-elements subsequences
        product = 1;
        for (int i = 0; i < mapa.size(); ++i) {
            if (!v[i]) {
                product *= array[i];
            }
        }
        sum += product;
    } while (next_permutation(v.begin(), v.end()));
    if (mapa["other"] >= k && mapa.size() > 1) {   // if number of "unlucky" numbers is bigger than k, we need to compute all possible  k-elements subsequences just from "unlucky" number, so binomial coefficient (mapa["other] choose k)
        sum += binomialCoeff(mapa["other"], k);
    }
    cout << sum % 1000000007 << endl;
}

0 个答案:

没有答案