LintCode 896. Prime Product 简明题解
Given a non-repeating prime array arr, and each prime number is used at most once, find all the product without duplicate and sort them from small to large.
Notice
2 <= |arr| <= 92 <= arr[i] <= 23
Given arr = [2,3], return [6].
Explanation:
2 * 3 = 6.
Gven arr = [2,3,5], return [6,10,15,30].
Explanation:
2 * 3 = 6, 2 * 5 = 10, 3 * 5 = 15, 2 * 3 *5 = 30
网上的解法用到了位运算和哈希表来避免重复计算。简单说就是用一个int的各个位来表示当前数组各个素数取舍状态。把这个int值从1开始一直加到2^n即可遍历所有状态了。
不足之处是写起来略繁琐。
这个题目其实可以看作自底向上建立一棵二叉树。每处理一个数,可以看作将当前二叉树复制一份,将当前的数字和1分别作为两个子树的父节点。求积也就是把二叉树从根节点到子节点所有路径遍历一遍。
进一步可以看出,这个过程也就是把当前数字和已有的乘积乘一遍而已。
因为素数本身不能算作乘积,因此不能存在结果数组中,所以我们可以单独处理一遍两个素数相乘的情况,同样加入结果集里。
代码:
vector<int> getPrimeProduct(vector<int> &arr) {
vector<int> ans;
for(size_t i = ; i < arr.size(); i++){
size_t len = ans.size();
for(size_t j = ; j < len; j++){
ans.emplace_back(arr[i] * ans[j]);
}
for(size_t j = ; j < i; j++){
ans.emplace_back(arr[j] * arr[i]);
}
}
sort(ans.begin(), ans.end());
return ans;
}
LintCode 896. Prime Product 简明题解的更多相关文章
- HDU 1016 Prime Ring Problem 题解
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- Lintcode: Kth Prime Number (Original Name: Ugly Number)
Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...
- 数学补天 By cellur925
质数 bool prime(int q) { ||q==) ; ) ; !=||q%!=) ; int cnt=sqrt(q); ;i<=cnt;i+=) !=||q%(i+)!=) ; ; } ...
- Codeforces Beta Round #24 D. Broken robot (打表找规律)
题目链接: 点击我打开链接 题目大意: 给你 \(n,j\),再给出 \(m[0]\) 的坐标和\(a[0]-a[n-1]\) 的坐标. 让你输出 \(m[j]\) 的坐标,其中 \(m[i]\) 和 ...
- Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- 【题解】CF45G Prime Problem
[题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...
随机推荐
- 设置OWA访问HTTP到HTTPS的重定向
需求:公司要求使用Http://mail.ever.com的方式来统一访问邮箱,所以需要进行相关的设置,将访问需求重定向到https://extest.ever.com/owa 首先在DNS里的eve ...
- TMG 2010 为HTTPS协议添加非标准端口(443)
1.添加加密端口时,编辑脚本addsslports.vbs addsslports.vbs 脚本内容如下: Dim root Dim tpRanges Dim newRange Set root = ...
- Win10开启PIN码使用教程
很多电脑爱好者对于Win10内置的PIN码功能不太了解,很多朋友都还没有使用.其实,创建PIN码可以提到密码使用,当你登录到Windows和其它应用服务时,可以通过PIN码替代输入账户密码,提升安全性 ...
- github发布静态页面
github发布静态页面:https://wangc1993.github.io/2019/01/07/2/
- PetaPoco轻量级ORM框架 - Database API 手册
PetaPoco Database API #region IDisposable public void Dispose() #endregion #region Constructors publ ...
- 转一篇shell中关于各种括号的讲解
shell中各种括号的作用().(()).[].[[]].{} 一.小括号,圆括号()1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的 ...
- JavaScript正则表达式_常用的正则
一.检查邮政编码 var pattern = /[1-9][0-9]{5}/; //共 6 位数字,第一位不能为0 {5}表示后面5位0到9 var str = '224000'; alert(pa ...
- virtualbox+vagrant学习-4-Vagrantfile-3-Minimum Vagrant Version
Minimum Vagrant Version 可以在Vagrantfile中指定一组vagrant版本需求,以强制人们使用带有Vagrantfile文件的vagrant特定版本.这可以帮助解决使用带 ...
- SpringMVC DELETE,PUT请求报错 添加支持Http的DELETE、PUT请求
SpringMVC删除与修改操作需要用DELETE,PUT请求方式提交. 但要知道浏览器form表单只支持GET与POST请求,而DELETE.PUT等method并不支持. spring3.0添加了 ...
- 在hyper-v中安装centos后配置网络
/etc/sysconfig/network-scripts vi ifcfg-eth0 改为以下配置 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no ...