CodeChef:Chef and Subsequences(思维 & dfs)

Though our Head Chef retired from sport programming long back, but that did not affect his passion to contribute to the programming community. He still remains engaged by creating new problems and passing it on to the online judges. Last Night, he thought of a following problem for which he could not figure out a solution. You being his friend from the older days of programming decide to help him.

Find the number of non-empty subsequences in the given list of N integers such that the product of the values in the subsequences does not exceed K

Input

First line of the input will contain two space separated integers N and K. Next and last line of the input contains N space separated integers

Output

Print the answer to the given test case in a separate line.

Constraints

  • 1 ≤ N ≤ 30
  • 1 ≤ K, Ai ≤ 10^18

Subtasks

Subtask #1 : (30 points)

  • 1 ≤ N ≤ 10

Subtask #2 : (70 points)

  • 1 ≤ N ≤ 30

Example

Input:
3 4
1 2 3

Output:
5

Explanation

For the given sample case, there are 7 non-empty subsequences out of which 2 have their product > 4. Those include {23} and {123}. Hence, the answer is 7 - 2 = 5.

题意:给N个数和M,问有多少个子集乘积<=M。
思路:折半dfs+二分。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[33], m;
vector<LL>x, y;
int n;
void dfs1(int cur, LL num)
{
    if(cur == n/2)
    {
        x.emplace_back(num);
        return;
    }
    dfs1(cur+1, num);
    if(a[cur]>m || m*1.0/a[cur]<num) return;
    dfs1(cur+1, num*a[cur]);
}

void dfs2(int cur, LL num)
{
    if(cur == n)
    {
        y.emplace_back(num);
        return;
    }
    dfs2(cur+1, num);
    if(a[cur]>m || m*1.0/a[cur]<num) return;
    dfs2(cur+1, num*a[cur]);
}
int main()
{
    LL ans = 0;
    scanf("%d%lld",&n,&m);
    for(int i=0; i<n; ++i) scanf("%lld",&a[i]);
    dfs1(0, 1);
    dfs2(n/2, 1);
    x.erase(x.begin()), y.erase(y.begin());
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    ans = x.size()+y.size();
    for(auto i : x)
    {
        LL tmp = (LL)ceil(m*1.0/i);
        int pos = lower_bound(y.begin(), y.end(), tmp)-y.begin();
        ans += pos;
    }
    printf("%lld\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值