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
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
Print the answer to the given test case in a separate line.
1 ≤ N ≤ 30
1 ≤ K, Ai ≤ 10^18
Subtask #1 : (30 points)
1 ≤ N ≤ 10
Subtask #2 : (70 points)
1 ≤ N ≤ 30
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;
}
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 spaceseparated integers
Output
Print the answer to the given test case in a separate line.
Constraints
1 ≤ N ≤ 301 ≤ 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;
}
相关文章推荐
- CodeChef:A temple of Snakes(思维 & 二分)
- [主席树维护HASH && SET维护DFS序] Codechef. Walks on the binary tree
- CodeChef:Desik And Divisors(数学 & 二分 & 思维)
- CodeChef:Magic Board(思维 & 树状数组)
- CodeChef:Two Closest(最短路 & 思维)
- codechef Partition the numbers(思维)
- AtCoder:Chef Hates Palindromes(思维 & 字符串)
- CodeChef:Fombinatorial(数论 & 逆元)
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
- 4408: [Fj Winter Camp 2016]神秘数&&4299: Codechef FRBSUM|主席树
- [分治最短路 && 树链剖分]Codechef September Challenge 2017 QGRID. Querying on a Grid
- HDU5527:Too Rich(DFS & 贪心 & 思维)
- codeforces#290 B&&510 B Fox And Two Dots(简单dfs)
- Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )
- codevs 2033 邮票 (背包)&& codevs 1047 邮票面值设计 (dfs+dp)
- SDOI DAY1 T1 & CodeChef Divisible Subset 前缀和抽屉原理
- codechef May Challenge 2016 CHSC: Che and ig Soccer dfs处理
- BZOJ1064:假面舞会(DFS & 思维)
- CodeChef - AMJMP Jump on Buildings 【dp + 思维】
- Codechef A temple of Snakes(思维)