zoukankan      html  css  js  c++  java
  • CodeForces 146E Lucky Subsequence (排列组合 + DP)

    CodeForces 146E Lucky Subsequence

    题意

    首先定义了一种叫幸运数,幸运数是只包含数字4和数字7的数字。给定n((n le 10^5))个数,每个数字都不大于 (10^9),让你从中选出k个数,这k个数只要下标不同就算不同,并且这k个数中都不包含两个相同的幸运数,让你求出有多少种选择。

    输入

    4 2
    4 4 7 7
    

    输出

    4
    

    样例解释

    这里有4种选择,分别是从下标来看,{1, 3}, {1, 4}, {2, 3} 和 {2, 4}。

    解析

    在不考虑不包含两个相同的幸运数字的条件,那么这个题就是一个简单的乘法原理。首先把这些数字分成两组,一组是非幸运数字,一组是幸运数字。然后可以考虑使用乘法原理,从非幸运数字中选取 (i) 个,再从幸运数字中选取 (k-i) 个,然后组成一组,求所有可能结果就是答案。对于非幸运数字来说就是一个组合数,当然需要使用逆元来处理。对于幸运数字来说,需要好好考虑。因为每个数字都不大于 (10^9),可以计算出来,所有可能的幸运数字不超过3000个,并且不可以选取相同的幸运数字,所以可以考虑使用动态规划来计算,(dp[i][j]) 表示前 (i) 种幸运数字选取 (j) 个不同的幸运数字的种类数量。状态方程为 (dp[i][j] = dp[i-1][j] + dp[i-1][j-1] * num[i]),其中 (num[i]) 表示第 (i) 种幸运数字出现的次数。

    代码

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <ctime>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #include <unordered_map>
    #define debug() puts("++++")
    #define print(x) cout<<"====== "<<(x)<<" ====="<<endl;
    // #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define _mod(x) ((x) % mod + mod) % mod
    #define sz size()
    #define be begin()
    #define ed end()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    // #define all 1,n,1
    #define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.in", "r", stdin)
    #define freopenw freopen("out.out", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-12;
    const int maxn = 1e5 + 7;
    const int maxm = 3000 + 7;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
    const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
    const P null = P(-1, -1);
    int n, m;
    
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    inline int read_int(){
      int x;  scanf("%d", &x);  return x;
    }
    
    
    unordered_map<int, int> lucky_map;
    std::vector<int> luckys;
    int cnt = 0;
    LL inv[maxn];
    LL dp[maxm][maxm];
    
    inline LL fast_pow(LL x, int n){
      LL res = 1;
      while(n){
        if(n&1)  res = res * x % mod;
        x = sqr(x) % mod;
        n >>= 1;
      }
      return res;
    }
    
    inline bool is_lucky_number(int x){
      while(x)  if(x % 10 != 4 && x % 10 != 7) return false; else x /= 10;
      return true;
    }
    
    inline void add(int x){
      if(is_lucky_number(x)){
        if(!lucky_map.count(x))  lucky_map[x] = 0, luckys.pb(x);
        ++lucky_map[x];
        return;
      }
      ++cnt;
    }
    
    
    int main(){
      scanf("%d %d", &n, &m);  luckys.pb(0);
      for(int i = 0; i < n; ++i)  add(read_int());
      dp[0][0] = 1;  LL ans = 0;
      LL f = 1;
      for(int i = 1; i <= cnt; ++i)  f = f * i % mod;
      inv[cnt] = fast_pow(f, mod - 2);
      for(int i = cnt - 1; i >= 0; --i)  inv[i] = (i+1) * inv[i+1] % mod;
      for(int i = 1; i < luckys.sz; ++i)
        for(int j = 0; j <= i; ++j){
          dp[i][j] = dp[i-1][j];
          if(j)  dp[i][j] = (dp[i][j] + dp[i-1][j-1] * lucky_map[luckys[i]]) % mod;
        }
      int up = min(m, (int)luckys.sz-1);
      for(int i = max(0, m - cnt); i <= up; ++i)  ans = (ans + dp[luckys.sz-1][i] * f % mod * inv[m-i] % mod * inv[cnt-m+i]) % mod;
      cout << ans << endl;
      return 0;
    }
    
  • 相关阅读:
    音频编辑大师 3.3 注冊名 注冊码
    Cocos2d_x的特点及环境配置
    strcpy_s与strcpy的比較
    Android Bundle类
    DB9 公头母头引脚定义及连接
    80x86汇编小站站长简单介绍
    腾讯webqq最新password加密算法,hash算法
    八大排序算法总结
    xpage 获取 附件
    转基因大豆提高大豆油脂产量80%
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/14910098.html
Copyright © 2011-2022 走看看