题目描述

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 aa consisting of nn integers.

The subsequence of the sequence aa is such subsequence that can be obtained from aa 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 nn has exactly 2^{n}2n different subsequences (including an empty subsequence).

A subsequence is considered lucky if it has a length exactly kk 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 aa . As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 10000000071000000007 (10^{9}+7)(109+7) .

输入输出格式

输入格式:

 

The first line contains two integers nn and kk (1<=k<=n<=10^{5})(1<=k<=n<=105) . The next line contains nn integers a_{i}ai​ ( 1<=a_{i}<=10^{9}1<=ai​<=109 ) — the sequence aa .

 


输出格式:

 

On the single line print the single number — the answer to the problem modulo prime number 10000000071000000007 (10^{9}+7)(109+7) .

 

输入输出样例

输入样例#1: 

3 2
10 10 10
输出样例#1: 

3
输入样例#2: 

4 2
4 4 7 7
输出样例#2: 

4

说明

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,3 , {1,4}1,4 , {2,3}2,3 and {2,4}2,4 .

题意:

定义神仙数为只含4和7的数字

给出n个数字,让你从里面取k个,同一个神仙数不能重复取,求取法数

题解

首先如果没有重复取这个限制,这就是一道线性求组合数。

就算有这个限制,全部不能重复取也是可做的,所以自然会感觉这种部分可重复部分不能的题很难受,于是会想到将原来的数组拆成两组,一组只有神仙数,一组只有非神仙数

然后答案就是在神仙数中取i个,在非神仙数中取k-i个的方案相乘,再将i=1~n的所有方案相加起来,就是答案

非神仙数显然可以线性处理组合数,O(1)查询

如今的问题是如何解决神仙数

首先需要搞清楚神仙数的一个性质:他非常少

这是可以证明的

一位的时候有4/7两种,两位有4种,反正1e9内大概不到3000个

这可以n^2DP

令dp[i][j]表示到第i位取j个数的方案数

显然dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*num[a[i]]

num[a[i]]表示大小为a[i]的神仙数个数

于是就可以A掉这题了

代码如下:

#include<map>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mod 1000000007
using namespace std; long long fac[],inv[],n,m,ans;
map<long long,int> m1;
vector<long long> a,b;
long long dp[][]; long long kasumi(long long a,long long b)
{
long long ans=;
while(b)
{
if(b&)
{
ans=ans*a%mod;
}
a=a*a%mod;
b>>=;
}
return ans;
} long long c(int n,int m)
{
if(n>m) return 0ll;
return fac[m]*inv[n]%mod*inv[m-n]%mod;
} int check(long long x)
{
while(x)
{
if(x%!=&&x%!=) return ;
x/=;
}
return ;
} int main()
{
fac[]=;
inv[]=;
for(int i=; i<=; i++)
{
fac[i]=fac[i-]*i%mod;
}
inv[]=kasumi(fac[],mod-);
for(int i=; i>=; i--)
{
inv[i]=inv[i+]*(i+)%mod;
}
scanf("%lld%lld",&n,&m);
long long tmp;
for(int i=; i<=n; i++)
{
scanf("%lld",&tmp);
if(check(tmp))
{
if(m1.count(tmp))
{
m1[tmp]++;
}
else
{
m1[tmp]++;
a.push_back(tmp);
}
}
else
{
b.push_back(tmp);
}
}
for(int i=;i<=;i++)
{
dp[i][]=;
}
if(a.size()>=)
{
dp[][]=m1[a[]];
for(int i=; i<a.size(); i++)
{
for(int j=; j<=a.size(); j++)
{
dp[i][j]=dp[i-][j]+dp[i-][j-]*m1[a[i]];
dp[i][j]%=mod;
}
}
}
int lena=a.size(),lenb=b.size();
ans+=c(m,lenb);
for(int i=; i<=min(lena*1ll,m); i++)
{
ans+=dp[lena-][i]*c(m-i,lenb);
ans%=mod;
}
printf("%lld\n",ans);
}

CodeForces 146E Lucky Subsequence(组合数+DP)的更多相关文章

  1. CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元

    题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  4. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  5. 【bzoj4517】[Sdoi2016]排列计数 组合数+dp

    题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...

  6. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  7. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  8. D. Yet Another Problem On a Subsequence 解析(DP)

    Codeforce 1000 D. Yet Another Problem On a Subsequence 解析(DP) 今天我們來看看CF1000D 題目連結 題目 略,請直接看原題 前言 這題提 ...

  9. CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)

    The sequence of integers a1,a2,…,aka1,a2,…,ak is called a good array if a1=k−1a1=k−1 and a1>0a1&g ...

随机推荐

  1. 第八篇 实例化Flask的参数 及 对app的配置

    Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...

  2. 【airflow实战系列】 基于 python 的调度和监控工作流的平台

    简介 airflow 是一个使用python语言编写的data pipeline调度和监控工作流的平台.Airflow被Airbnb内部用来创建.监控和调整数据管道.任何工作流都可以在这个使用Pyth ...

  3. ZooKeeper与仲裁模式

    为了让服务器之间可以通信,服务器间需要一些联系信息.理论上,服务器可以使用多播来发现彼此,但我们想让ZooKeeper集合支持跨多个网 络而不是单个网络,这样就可以支持多个集合的情况. 为了完成这些, ...

  4. 通过Java代码装配Bean

    上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...

  5. php yii 学习笔记

    yii 归档安装 1,下载 yii  Yii2的高级应用程序模板 2,解压模板到目录,进入控制台进入目录 运行 php init 安装YII 3,进入 http://localhost/phpmyad ...

  6. 面试------Android 版本之前的差异(常见,欢迎补充)。

    不管你技术如何,只要背点这个,能忽悠倒一片.. 1.WebView JS漏洞 ,Android4.2之前 ,解决办法,不用addJavascriptInterface,webchrome的onJsPr ...

  7. git 转移

    git push --mirror https://github.com/cloud-pi/drbd-docker-plugin.git

  8. chrome浏览器控制台创建js脚本并执行

    Chrome的snippets是小脚本,还可以创作并在Chrome DevTools的来源面板中执行.您可以访问和从任何页面运行它们.当你运行一个片段,它从当前打开的页面的上下文中执行.本文主要讲如何 ...

  9. 高性能Web服务器Nginx的配置与部署研究(11)应用模块之Memcached模块的两大应用场景

    一.应用场景1 最近在一个项目中,用到了Nginx的Memcached模块,所以就在这个系列教程中提前把Memcached模块拿出来写了.另外发现最近我的 博客文章频频被很多用采集器的网站拿走,帮我发 ...

  10. 【总结整理】AI产品经理大会2017(转载)

    从企业大数据到企业 AI | 易观智慧院院长 李智 1.AI 不是目的,而是要了解 AI 是什么,真正意义上的强人工智能在前沿领域尚未取得突破,暂时只能在影视文学作品中去思考人机关系.机器人三定律在未 ...