题目大意:

给出两个集合,第一个集合数的乘积是分子,第二个集合的数的乘积是分母,要求够造一个同样的集合,但是得到的分数是最简分数。

分析:

寻找思路并不复杂,对两个集合的每个数进行质因数分解,然后统计整个集合的质因数分解情况,再将两个集合的质因数的次数大减小即可。构造时使两个集合中元素的个数不变,尽可能地构造成原先集合的数,如果不行就填一个 \(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的更多相关文章

  1. CodeForce 222C Reducing Fractions

    To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...

  2. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  3. codeforces 练习

    codeforces 627 D. Preorder Test 二分 + 树dp 做logn次树dp codeforces 578D.LCS Again 给出一个字符串str,长度n<=10^6 ...

  4. Codeforces Round #137 (Div. 2)

    A. Shooshuns and Sequence 显然\([k,n]\)之间所有数均要相同,为了求最少步数,即最多模拟\(n\)次操作即可. B. Cosmic Tables 映射\(x_i,y_i ...

  5. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  6. 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 ...

  7. 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 ...

  8. 一天一经典Reducing the Dimensionality of Data with Neural Networks [Science2006]

    别看本文没有几页纸,本着把经典的文多读几遍的想法,把它彩印出来看,没想到效果很好,比在屏幕上看着舒服.若用蓝色的笔圈出重点,这篇文章中几乎要全蓝.字字珠玑. Reducing the Dimensio ...

  9. 模拟 --- hdu 12878 : Fun With Fractions

    Fun With Fractions Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit ...

随机推荐

  1. js动态生成vue组件

    代码奉上 install (Vue, options) { Vue.prototype.$message = function (message){ let Constructor = Vue.ext ...

  2. Java 对象头那点事

    概览 对象头 存放:关于堆对象的布局.类型.GC状态.同步状态和标识哈希码的基本信息.Java对象和vm内部对象都有一个共同的对象头格式. (后面做详细介绍) 实例数据 存放:类的数据信息,父类的信息 ...

  3. 【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"

    问题描述 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connecti ...

  4. Golang 的 `[]interface{}` 类型

    Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...

  5. 解决mysq服务无法正常启动问题

    在mysql的启动过程中,遇到什么问题都可以反馈给我,我都会尽力帮你们解决 第一种:通过net start mysql启动MySQL服务器时,出现以下信息 是因为在MySQL5.7以上的版本中默认的没 ...

  6. Git拉取远程新分支

    1.查看本地分支  git branch 2.查看远程分支  git branch -a 3.如果要拉取的远程分支本地没有 git fetch 4.拉取远程新分支到本地 git checkout -b ...

  7. Linux篇-mysql + keepalived高可用

    1上次说过了mysql的主从配置 tar zxf keepalived-1.2.7.tar.gz cd keepalived-1.2.7 yum install gcc gcc-c++ yum ins ...

  8. MMDeploy安装笔记

    MMDeploy的TensorRT教程 Step1: 创建虚拟环境并且安装MMDetection conda create -n openmmlab python=3.7 -y conda activ ...

  9. ACM 刷题记录

    HDU Multi-University Training Contest 题目来源 题目 知识点 时间复杂度 完成情况 2019 Contest8 A Acesrc and Cube Hyperne ...

  10. 1.3温度转换(中国大学Mooc-Python 语言程序设计)

    温度转换 温度刻画的两种不同体系 1.摄氏度:(中国等世界大多数国家使用) 以1标准大气压下水的结冰点为0度,沸点为100度,将温度进行等分刻画  2.华氏度:(美国.英国等国家使用) 以1标准大气压 ...