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 ...
随机推荐
- ZABBIX新功能系列1-使用Webhook将告警主动推送至第三方系统
Zabbix5以来的新版本与以前的版本除UI界面变化较大外,在很多功能上也有许多亮点,我这里计划安排1个系列来和大家交流一些新功能的使用,这是第一篇:使用Webhook将告警主动推送至第三方系统. 首 ...
- java高级用法之:JNA中的回调
目录 简介 JNA中的Callback callback的应用 callback的定义 callback的获取和应用 在多线程环境中使用callback 总结 简介 什么是callback呢?简单点说 ...
- Docker部署mysql 5.7
Docker部署mysql 5.7 准备工作 在CentOS或者Linux创建部署目录,用于存放容器的配置和MySQL数据:目的是当重装或者升级容器时,配置文件和数据不会丢失.执行以下命令: a.创建 ...
- 一起看 I/O | Flutter 休闲游戏工具包发布
作者 / Zoey Fan, Product Manager for Flutter, Google 对于大多数开发者来说,Flutter 是一个应用框架.但利用 Flutter 提供的硬件加速图形支 ...
- CentOS配置epel源
https://opsx.alibaba.com/mirror epel 配置方法 1.备份(如有配置其他epel源) mv /etc/yum.repos.d/epel.repo /etc/yum.r ...
- python之部分内置函数与迭代器与异常处理
目录 常见内置函数(部分) 可迭代对象 迭代器对象 for循环内部原理 异常处理 异常信息的组成部分 异常的分类 异常处理实操 异常处理的其他操作 for循环本质 迭代取值与索引取值的区别 常见内置函 ...
- 使用C#和MonoGame开发俄罗斯方块游戏
小的时候就看到有同学使用C语言在DOS下做过一款俄罗斯方块的游戏,当时是启用了DOS的图形化模式,感觉也挺有意思.最近上海疫情封控在家,周末也稍微有点空余时间,于是使用Visual Studio 20 ...
- git clone 问题
转自 git clone出现 fatal: unable to access 'https://github.com/...'的解决办法(亲测有效) - 山村码农 - 博客园 (cnblogs.com ...
- torch.nn.MSELoss()函数解读
转载自:https://www.cnblogs.com/tingtin/p/13902325.html
- STM32内存知识
在了解STM32内存之前需要了解 MCU 的型号和MDK 中的.map 文件,很多刚学习 stm32 时都不会过多的去了解 MCU 的选型,是在太枯燥了.这里在从新了解一下,久了就熟悉了. 一.STM ...