您的位置:首页 > 产品设计 > UI/UE

CodeChef:Chef and Subsequences(思维 & dfs)

2017-09-15 21:41 387 查看
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 {2, 3} and {1, 2, 3}.
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: