Modular Inverse

http://codeforces.com/problemset/problem/145/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 477444 are lucky and 517467 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}.












DP + Combinatorial Number + Modular Inverse

Calculate modular inverse: method1) extGCD; method2) Euler's theorem + PowerMod

#include <cstdio>
#include <functional>
#include <algorithm>

using namespace std;

const int MAX=100010;
const int MOD=1000000007;

int a[MAX],dp[MAX];

inline bool lucky(int n)
{
	while(n>0)
	{
		if(n%10!=4 && n%10!=7)
			return false;
		n/=10;
	}
	return true;
}

inline int power(int n,int k)
{
	if(k==0)
		return 1;
	long long a=power(n,k>>1);
	if(k&1)
		return n*a%MOD*a%MOD;
	else
		return a*a%MOD;
}

inline int inv(int n)
{
	return power(n,MOD-2);
}

inline int extgcd(int a,int b,int &x,int &y)
{
	int t,ret;
	if(!b)
	{
		x=1,y=0;
		return a;
	}
	ret=extgcd(b,a%b,x,y);
	t=x;
	x=y;
	y=t-a/b*y;
	return ret;
}

inline int inverse(int n)
{
	int x,y;
	extgcd(n,MOD,x,y);
	x%=MOD;
	return (x>=0)?x:x+MOD;
}

int main()
{
	int n,m,t;
	scanf("%d%d",&n,&t);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	m=0;
	for(int i=0;i<n;i++,m++)
	{
		if(lucky(a[i]))
		{
			int k=i+1;
			while(k<n && a[k]==a[i])
				k++;
			a[m]=k-i;
			i=k-1;
		}
		else
			a[m]=1;
	}
	sort(a,a+m,greater<int>());
	int num=0,one;
	while(a[num]>1 && num<m)
		num++;
	one=m-num;
	dp[0]=1;
	for(int i=0;i<num;i++)
		for(int j=min(i+1,t);j>0;j--)
			dp[j]=(dp[j]+(long long)a[i]*dp[j-1])%MOD;
	int ans=dp[t],comb=1;
	for(int i=1;i<=t && i<=one;i++)
	{
		comb=(long long)(one-i+1)*inverse(i)%MOD*comb%MOD;
		ans=(ans+(long long)dp[t-i]*comb)%MOD;
	}
	printf("%d\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值