CF222C Reducing Fractions
题目大意:
给出两个集合,第一个集合数的乘积是分子,第二个集合的数的乘积是分母,要求够造一个同样的集合,但是得到的分数是最简分数。
分析:
寻找思路并不复杂,对两个集合的每个数进行质因数分解,然后统计整个集合的质因数分解情况,再将两个集合的质因数的次数大减小即可。构造时使两个集合中元素的个数不变,尽可能地构造成原先集合的数,如果不行就填一个 \(1\)。但质因数分解的过程中不能采用 \(O(\sqrt n)\) 的复杂度,会超时,接下来介绍本题中进行质因数分解的方法。
其实也不是很复杂,就是对于每个被分解的数,优先除以它最大的质因数即可。当然,需要提前处理一下每个数最大的质因数。
AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int M = 1e7;
const int MAXN = 1e4;
int n,m;
map<int,int> cnt1,cnt2;
set<int> s;
int prime[M + 5],up[M + 5],down[M + 5],a[M + 5],bb[M + 5];
signed main(){
//freopen("B.out","r",stdin);
//prime.push_back(9999991);
for(int i = 2; i <= M; i++){//预处理每个数的最大质因数
if(prime[i] == 0){
prime[i] = i;
for(int j = i + i; j <= M + 3; j+=i){
prime[j] = i;
}
}
}
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
int j;
for(j = a[i]; j > 1; j /= prime[j]){//质因数分解
s.insert(prime[j]);
up[prime[j]]++;//存储分子的质因数分解情况
}
}
for(int i = 1; i <= m; i++){
cin >> bb[i];
int j;
for(j = bb[i]; j > 1; j /= prime[j]){//质因数分解
s.insert(prime[j]);
down[prime[j]]++;//存储分母的质因数分解情况
}
}
int b;
int now = 1;
cout << n << " " << m << "\n";
for(int i = 1; i <= n; i++){
int j;
int tmp =1;
for(j = a[i]; j > 1; j /= prime[j]){
if(down[prime[j]] > 0){
down[prime[j]]--;//如果当前该数的质因数能在分母里也含油1,那么就将它约去,否则将它乘到答案里面
}
else{
tmp *= prime[j];
}
}
cout << tmp << " ";
}
puts("");
for(int i = 1; i <= m; i++){
int j;
int tmp = 1;
for(j = bb[i]; j > 1; j /= prime[j]){
if(up[prime[j]] > 0){
up[prime[j]]--;//同上
}
else{
tmp *= prime[j];
}
}
cout << tmp << " ";
}
return 0;
}
CF222C Reducing Fractions的更多相关文章
- CodeForce 222C Reducing Fractions
To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- codeforces 练习
codeforces 627 D. Preorder Test 二分 + 树dp 做logn次树dp codeforces 578D.LCS Again 给出一个字符串str,长度n<=10^6 ...
- Codeforces Round #137 (Div. 2)
A. Shooshuns and Sequence 显然\([k,n]\)之间所有数均要相同,为了求最少步数,即最多模拟\(n\)次操作即可. B. Cosmic Tables 映射\(x_i,y_i ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...
- Codeforces Round #232 (Div. 2) D. On Sum of Fractions
D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...
- 一天一经典Reducing the Dimensionality of Data with Neural Networks [Science2006]
别看本文没有几页纸,本着把经典的文多读几遍的想法,把它彩印出来看,没想到效果很好,比在屏幕上看着舒服.若用蓝色的笔圈出重点,这篇文章中几乎要全蓝.字字珠玑. Reducing the Dimensio ...
- 模拟 --- hdu 12878 : Fun With Fractions
Fun With Fractions Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit ...
随机推荐
- UART串口及Linux实现
UART,全称Universal Asynchronous Receiver Transmitter,通用异步收发器,俗称串口.作为最常用的通信接口之一,从8位单片机到64位SoC,一般都会提供UAR ...
- 不要使用Java Executors 提供的默认线程池
线程池构造方法 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUni ...
- Docker容器(container)详解 (转载自http://c.biancheng.net/view/3150.html)
- 929. Unique Email Address - LeetCode
Question 929. Unique Email Address Solution 题目大意: 给你一个邮箱地址的数组,求出有多少个不同的地址,其中localName有如下规则 加号(+)后面的字 ...
- 学习Java的第十七天——大数字运算
学习内容:大数字运算 代码实现: package 数字处理类; import java.math.BigInteger; public class BigIntegerDemo { public st ...
- [BJOI2014]想法
参考 P4581传送门 题意:给DAG,问每个点可以由多少个叶子到达. 思路: 随机化!!(题面有提示) 这道题利用在一个范围内随机的数期望均分范围的性质. 直接每个叶子在\([0,Max\_Rand ...
- 《Unix 网络编程》13:守护进程和 inetd 超级服务器
守护进程和 inetd 超级服务器 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
- MVC - forward 和 redirect 的区别
MVC - forward 和 redirect 的区别 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服 ...
- 2 Elment Ui 日期选择器 格式化问题
Elment Ui 日期选择器 格式化问题 在前后端联调过程中 我们常常会遇到日期无法被反序列化 这个问题 会有点头疼 下面以我这边为例 后端使用的是springboot 默认集成了jackjson ...
- JSON.toJSONString(joinPoint.getArgs())报错getOutputStream() has already been called for this response
nested exception is java.lang.IllegalStateException: It is illegal to call this method if the curren ...