还是聪哥给我讲的思路才知道的,起初我利用两两互质去求发现有问题,互质只是必要条件而非充分条件,后来还是用的标准思路

即其实最终只要保留最大的素数的幂即可,其他包含该素数幂但指数低的都不用了,这样就能保证序列最小公倍数不变,同时,为了字典序最小,只需一点小小的处理。

在处理的时候遇到些小问题,主要是没考虑到每个值得因子没有求尽。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll num[],ans[],prime[];
int n;
ll gcd(ll a,ll b)
{
if (a<b) swap(a,b);
return b==? a:gcd(b,a%b);
} int main()
{
int cnt=;
while (scanf("%d",&n))
{
if (n==) break;
cnt=;
for (int i=; i<n; i++)
{
scanf("%lld",&num[i]);
ans[i]=num[i];
}
for (int i=; i<n; i++)
{
while (ans[i]!=)//求出每个ans[i]对应的因子
{
ll g=ans[i];
for (int j=; j<n; j++)
{
if (ans[i]==) break;
ll h=gcd(g,ans[j]);
if (h!=)
{
g=h;
}
}
if (g!=){
prime[cnt++]=g;//把因子存起来
for (int k=; k<n; k++)
while (ans[k]%g==) ans[k]/=g;//这里要除干净,为了这个搞了好久没弄出来
}
}
}
sort(prime,prime+cnt);//给因子排完序,把质数因子放在了前面,后面的非质数因子其实没有用了
for (int i=; i<n; i++)
ans[i]=;
for (int i=; i<cnt; i++)
{
ll tmp=prime[i];
//cout<<tmp<<endl;
int maxn=;
int loc=;
for (int j=; j<n; j++)
{
int tm=;
while (num[j]%tmp==)
{
// cout<<num[j]<<" "<<tmp<<endl;
num[j]/=tmp;
tm++;
// cout<<tm<<endl;
}
if (tm>=maxn) //这里即可保证字典序最小
{
maxn=tm;
loc=j;
//cout<<j<<" loc "<<tmp<<endl;
}
}
for (int k=; k<maxn; k++)
ans[loc]*=tmp;
}
printf("%lld",ans[]);
for (int i=; i<n; i++)
printf(" %lld",ans[i]);
printf("\n");
}
return ;
}

最新文章

  1. 利用log4j+mongodb实现分布式系统中日志统一管理
  2. hdu 4403 dfs
  3. Qt 子窗体嵌入父窗体
  4. Leetcode 8 String to Integer (atoi) 字符串处理
  5. BZOJ 2436 Noi嘉年华(优化DP)
  6. 多线程编程-- part 3 多线程同步-&gt;synchronized关键字
  7. 获取报告 Stream转string,利用字符串分割转换成DataTable
  8. 2016.3.17__CSS3动画__第十一天
  9. MongoDB中级----&gt;关联多表查询
  10. Spring Boot与缓存
  11. ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
  12. 错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法
  13. python安装与IO编程
  14. linux命令学习之:ps
  15. Asp.net core 2.0.1 Razor 的使用学习笔记(六)
  16. PHP 5.2、5.3、5.4、5.5、5.6 对比以及功能详解
  17. rpcserver不可用
  18. JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
  19. 本地项目关联git仓库
  20. poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数

热门文章

  1. 《新标准C++程序设计》4.2-4.3(C++学习笔记13)
  2. angularJS MVC及$scope作用域
  3. ubuntu---NVIDIA驱动 + CUDA 安装完可能会遇见的问题
  4. Install and Configure NFS Server on RHEL 8 / CentOS 8
  5. 在 CentOS 中部署 KMS 服务器(vlmcsd)
  6. Redis集群环境之linux搭建多机版---已完结,跟着一步一步来你就可以集群成功
  7. ajax异步提交 有时会出现无bug的数据处理异常-----debug没有问题,正常运行却数据处理不正确,极少机会会出现正常的处理结果
  8. M: Mysterious Conch 字符串哈希
  9. 关于博主 5ab
  10. Power Tower