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 ...
随机推荐
- 老生常谈系列之Aop--AspectJ
老生常谈系列之Aop--AspectJ 这篇文章的目的是大概讲解AspectJ是什么,所以这个文章会花比较长的篇幅去解释一些概念(这对于日常开发来说没一点卵用,但我就是想写),本文主要参考Aspect ...
- yarn/npm 设置镜像地址
注意 如果开发 electron 桌面软件,需要设置以下两个镜像地址 disturl.electron_mirror 如果用到了 node-sass 需要设置以下一个镜像地址 sass_binary_ ...
- ubuntu helpers
linux 命令大全 Apt proxy configuration on Ubuntu 20.04 Focal Fossa Linux 临时使用socks代理apt-get的方法 docker - ...
- nginx反向代理获取用户真实ip
nginx做反向代理时,默认的配置后端获取到的ip都是来自于nginx,如何转发用户的真实ip到后端程序呢?如是是java后端,用request.getRemoteAddr();获取到的是nginx的 ...
- Netty源码研究笔记(4)——EventLoop系列
1. Netty源码研究笔记(4)--EventLoop系列 EventLoop,即事件驱动,它是Netty的I/O模型的抽象,负责处理I/O事件.任务. 不同的EventLoop代表着不同的I/O模 ...
- Python常用标准库(pickle序列化和JSON序列化)
常用的标准库 序列化模块 import pickle 序列化和反序列化 把不能直接存储的数据变得可存储,这个过程叫做序列化.把文件中的数据拿出来,回复称原来的数据类型,这个过程叫做反序列化. 在文件中 ...
- Canvas 线性图形(五):多边形
前言 CanvasRenderingContext2D 没有提供绘制多边形的函数,所以只能由我们自己来实现绘制多边形的函数.以六边形为基础,需要用到三角函数:sin 和 cos. 点 A 坐标 (一) ...
- 分享JAVA的FTP和SFTP相关操作工具类
1.导入相关jar <!--FTPClient--><dependency> <groupId>commons-net</groupId> <a ...
- Wget命令解释
Wget主要用于下载文件,在安装软件时会经常用到,以下对wget做简单说明. 1.下载单个文件:wget http://www.baidu.com.命令会直接在当前目录下载一个index.html的文 ...
- GDKOI 2021 Day2 PJ 去世记
比赛时和昨天一样困,后面的大奆打代码的速度简直了 T1 用 2.4.6.8 来与 5 抵消掉末尾的 0 ,然后用周期问题的方法直接乘起来并取个位 #include<bits/stdc++.h&g ...