Reducing Fractions
题目链接:传送门
题意描述:
给你两个集合(数据可以重复,相当与数组),第一组集合的元素乘积表示分子,第二组集合的乘积表示分母。让你求约分后的集合(即分子和分母的gcd为1).
题目分析:
这道题就是质因数分解,先把第一组数据质因数分解放在up数组中,把第二组数据质因数分解放在down数组中。然后再进行第一组数据的质因数分解,跟down数组相匹配,输出约分之后的值,第二组数据也进行如上操作,只是down数组变成up数组而已。需要注意的是,我们不能把up数组和down数组做比较然后输出结果,因为有可能abs(up[i] - down[i])的值很大,可能超过1E7,这样子做的话可能会超时。时间复杂度大概为nlogn。
代码:
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e7;
int prime[inf+1];
int up[inf+1], down[inf+1];
int a[100005], b[100005];
int n, m;
void init(){for(int i=2;i<=inf;++i){if(!prime[i]){for(int j=i;j<=inf;j+=i){prime[j] = i;}}}
}
int main(){cin >> n >> m;init();int temp;for(int i=0;i<n;++i){scanf("%d", &a[i]);temp = a[i];while(temp > 1){up[prime[temp]]++;temp /= prime[temp];}}for(int i=0;i<m;++i){scanf("%d", &b[i]);temp = b[i];while(temp > 1){down[prime[temp]]++;temp /= prime[temp];}}cout << n << " " << m << endl;for(int i=0;i<n;++i){int res = 1;while(a[i] > 1){if(down[prime[a[i]]]){down[prime[a[i]]]--;}else res *= prime[a[i]];a[i] /= prime[a[i]];}printf("%d", res);i == n-1? printf("\n"):printf(" ");}for(int i=0;i<m;++i){int res = 1;while(b[i] > 1){// cout << b[i] << endl;if(up[prime[b[i]]]){up[prime[b[i]]]--;}else res *= prime[b[i]];b[i] /= prime[b[i]];}printf("%d", res);i == n-1? printf("\n"):printf(" ");}return 0;
}
Reducing Fractions相关推荐
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- CodeForces - 222C Reducing Fractions(唯一分解定理)
题目链接:点击查看 题目大意:给出两个数组,数组a表示的是分子的因数,数组b表示的是分母的因数,求约分后的答案 题目分析:一看题目挺简单的,但是在输出的那里这个题目做了特定的要求,题目首先给出的是1e ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- usaco ★Fractions to Decimals 分数化小数
★Fractions to Decimals 分数化小数 写一个程序,输入一个形如 N/D 的分数(N 是分子,D 是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中.例如, ...
- TZOJ--3560: Ordered Fractions (枚举)
3560: Ordered Fractions 描述 Consider the set of all reduced fractions between 0 and 1 inclusive with ...
- 数字图像处理实验(2):PROJECT 02-02, Reducing the Number of Gray Levels in an Image
实验要求: Reducing the Number of Gray Levels in an Image Objective To understand how the number of gray ...
- 5kyu Some Egyptian fractions
5kyu Some Egyptian fractions 题目背景: Given a rational number n as a string (example: "2/3" i ...
- 1439: 2.4.5 Fractions to Decimals 分数化小数
1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB 提交: 194 解决: 13 题目描述 写一个程序,输入一个形如N/ ...
- 10976 - Fractions Again?!
Fractions Again?! It is easy to see that for every fraction in the form 1k\frac{1}{k}(k>0), we ca ...
最新文章
- 外部表External table
- sublime配置python3环境_【env】Sublime配置Python3开发环境
- PHP下用正则表达式分割preg_split、替换reg_replace、匹配preg_match_all等出现乱码的解决方法...
- [luoguP2331] [SCOI2005]最大子矩阵(DP)
- 人生苦短,我用Python(Python快速教程 - 基础篇)
- mysql多表查询练习_MySQL多表查询综合练习答案
- Win Server 2012 R2 安装SQL 2016先决条件
- Ubuntu16.04 + cuda8.0 + GTX1080 + matlab14.04a + Opencv3.0 + caffe 安装教程
- QT课程设计:C++英语单词记忆软件程序开发
- Gitee生成公钥详细教程
- 数据治理:元数据及元数据管理策略、方法和技术
- php 获取微博cookie,如何获取微博 Cookie
- SQLite3 dll加载失败问题解决
- 安卓实现循环定时响铃
- SoftRendererRenderPipeline(从迷你光栅化软渲染器的实现看渲染流水线)
- Eclipse中使用ModelGoon插件生成类图及顺序图
- zcmu-1056 网址收藏夹
- telnet 正在连接127.0.0.1:9123...无法打开到主机的连接。 在端口 23: 连接失败
- 读《从架构师到卖水果再挖煤 重审青春逆袭》有感
- Linux负载CPU、内存、磁盘IO、网络IO状态分析详解
热门文章
- iOS之深入解析Memory内存
- 贝努利概率 matlab
- org.springframework.dao.InvalidDataAccessApiUsageException:The given object has a null identifi的解决方案
- Java前叉夹器_新手知识:为什么说公路直装夹器更好呢?
- 前端网站资源精编!!
- 能够自动绘制网络拓扑图的软件——WGCLOUD
- 学习u3d的几个工具和文档
- html5网页制作代码-我的班级网页 HTML期末大作业
- Firefox 使用常见问题和解决方法
- 互联网+背景下给旅行社的重大挑战