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 ...
随机推荐
- 沉淀再出发:jetty的架构和本质
沉淀再出发:jetty的架构和本质 一.前言 我们在使用Tomcat的时候,总是会想到jetty,这两者的合理选用是和我们项目的类型和大小息息相关的,Tomcat属于比较重量级的容器,通过很多的容器层 ...
- SQL查询含有%号的字段
select * from EMS_ANNOUNCEMENT where 1=1 and title like '%\%%' escape '\'
- August 10th 2017 Week 32nd Thursday
Break through the psychological barrier to surpass themselves. 突破心理障碍,才能超越自己. To break through those ...
- December 26th 2016 Week 53rd Monday
Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. If the world is so cruel, I wo ...
- FOR YOU
给你 作者:余秀华 一家朴素的茶馆, 面前目光朴素的你皆为我喜欢 你的胡子,昨夜辗转的面色让我忧伤 我想带给你的,一路已经丢失得差不多 除了窗外凋谢的春色 遇见你以后,你不停地爱别人,一个接一个 我没 ...
- codeforces 414D Mashmokh and Water Tanks
codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...
- java继承-重写-super实例补充
方法重写: 是指子类根据需要父类继承来的方法进行改写,是多态机制的前奏. 重写注意点: 1.重写方法必须和被重写方法具有相同的方法名,参数列表和返回值. 2.重写方法方法不能使用比被重写方法更严格的访 ...
- Scala 经典的模式匹配和尾递归
Scala 经典的模式匹配和尾递归 package io import java.io.{BufferedWriter, File, FileWriter} import java.text.Simp ...
- 随手练—— 洛谷-P2945 Sand Castle(贪心)
题目链接:https://www.luogu.org/problemnew/show/P2945 (原题 USACO) 要求钱最少,就是试着让M和B的离散程度最小(我自己脑补的,就是总体更接近,我不知 ...
- wireMock快速伪造restful服务
官网地址:http://wiremock.org/ Jar下载:http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock/1.57/w ...