Codeforces Round #104 (Div. 1) C. Lucky Subsequence

C. Lucky Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya has sequence a consisting of n integers.

The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.

Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).

A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).

Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n integers ai (1 ≤ ai ≤ 109) — the sequence a.

Output

On the single line print the single number — the answer to the problem modulo prime number 1000000007 (109 + 7).

Sample test(s)
input
3 2
10 10 10
output
3
input
4 2
4 4 7 7
output
4
Note

In the first sample all 3 subsequences of the needed length are considered lucky.

In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4},{2, 3} and {2, 4}.

题意:在n个数里面选出k个,lucky数不能有相同两个被选出来,问方案数

把数分成两部分,在lucky num 里面dp 

dp[i][j] 表示前i 个数里面选j个数方案数dp[i][j] = dp[i-1][j]+dp[i-1][j-1]*num[i] 

num[i]表示第i个lucky数出现的次数

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define ll long long
#define INF 0x3f3f3f3f
#define maxn 100010
#define eps 1e-6
#define mod 1000000007
using namespace std;

LL f[maxn],e[maxn] ;
LL dp[2500][2500] ;
int num[2500] ;
LL pow1(int a,int n)
{
    if(n==0) return 1 ;
    LL ans=pow1(a,n/2) ;
    ans=ans*ans%mod;
    if(n&1) ans=ans*a%mod;
    return ans;
}
LL C(int n ,int k )
{
    if(k>n) return 0 ;
    if(k==0||n==k) return 1;
    return f[n]*e[k]%mod*e[n-k]%mod ;
}
void init()
{
    f[0]=1;
    e[0]=1;
    for( int i = 1 ; i < maxn ;i++){
        f[i]=f[i-1]*i%mod;
        e[i]=pow1(f[i],mod-2) ;
    }
}
map<int,int>mm;
bool check(int n )
{
    while(n)
    {
        if(n%10 != 4 && n%10 != 7) return false;
        n /= 10 ;
    }
    return true;
}
int main()
{
    int n,m,i,j;
    int k ,cnt1,cnt2 ,cnt ;
    init();
    LL ans;
    while(scanf("%d%d",&n,&k) != EOF)
    {
        ans=0;
        cnt1=cnt=0;
        memset(num,0,sizeof(num));
        mm.clear();
        for( i = 1 ; i <= n ;i++)
        {
            scanf("%d",&m) ;
            if(check(m)){
                if(mm[m])num[mm[m]]++ ;
                else
                {
                    cnt++;
                    mm[m]=cnt;
                    num[cnt]++;
                }
            }
            else cnt1++;
        }
        memset(dp,0,sizeof(dp)) ;
        dp[0][0]=1;
        for( i = 1 ; i <= cnt ;i++)
        {
            dp[i][0]=dp[i-1][0];
            for( j = 1 ; j <= i ;j++)
                dp[i][j]=(dp[i-1][j]+dp[i-1][j-1]*num[i]%mod)%mod;
        }
        for( i = 0 ; i <= cnt&&i<=k;i++)
        {
            ans=(ans+dp[cnt][i]*C(cnt1,k-i)%mod)%mod;
        }
        cout << ans<<endl;
    }
    return 0 ;
}
View Code

 

posted @ 2014-11-05 20:52  _log__  阅读(387)  评论(0编辑  收藏  举报