【Codeforces 582A】GCD Table
【链接】 我是链接,点我呀:)
【题意】
给你一个数组A[]经过a[i][j] = gcd(A[i],A[j])的规则生成的二维数组
让你求出原数组A
【题解】
我们假设原数组是A
然后让A数组满足A[i](感性认知一下)
每次都把A[i+1..n]这些数字产生的gcd都删掉
那么剩余的a[][]里面只有A[1..i]产生的gcd
那么因为A[i]是最大值,所以gcd(A[i],A[i])肯定也是a[][]里面的最大值,也即A[i]=剩余的a[][]里面的最大值
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 500;
static class Task{
int n;
int ans[] = new int[N+10];
TreeMap<Integer, Integer> dic = new TreeMap<Integer,Integer>();
int _findmax() {
int x = dic.lastKey();
return x;
}
int gcd(int x,int y) {
if (y==0) return x;
else return gcd(y,x%y);
}
void _delete(int x) {
int y = dic.get(x);
y--;
if (y==0) dic.remove(x);else dic.put(x, y);
}
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();
for (int i = 1;i <= n*n;i++) {
int x = in.nextInt();
if (dic.containsKey(x)) {
int y = dic.get(x);
y++;
dic.put(x, y);
}else dic.put(x, 1);
}
for (int i = n;i >= 1;i--) {
ans[i] = _findmax();
_delete(gcd(ans[i],ans[i]));
for (int j = i+1;j<= n;j++) {
_delete(gcd(ans[i],ans[j]));
_delete(gcd(ans[i],ans[j]));
}
}
for (int i = 1;i <= n;i++)
out.print(ans[i]+" ");
out.println();
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 582A】GCD Table的更多相关文章
- 【Codeforces 582A】 GCD Table
[题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数. G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【84.62%】【codeforces 552A】Vanya and Table
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 664A】 Complicated GCD
[题目链接] 点击打开链接 [算法] gcd(a,a+1) = 1 所以当a = b时,答案为a,否则为1 [代码] #include<bits/stdc++.h> using names ...
- 【codeforces 509A】Maximum in Table
[题目链接]:http://codeforces.com/contest/509/problem/A [题意] 给你一个递推式f[i][j] = f[i-1][j]+f[i][j-1]; 让你求f[i ...
- 【codeforces 803C】Maximal GCD
[题目链接]:http://codeforces.com/contest/803/problem/C [题意] 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列的所有数 ...
随机推荐
- 使用HttpClient MultipartEntityBuilder 上传文件,并解决中文文件名乱码问题
遇到一种业务场景,前端上传的文件需要经过java服务转发至文件服务.期间遇到了原生HttpClient怎么使用的问题.怎么把MultipartFile怎么重新组装成Http请求发送出去的问题.文件中文 ...
- 测试DNS服务的命令
测试DNS服务的命令 dig dig用于测试dns系统,因此不会查询hosts文件进行解析. dig - DNS lookup utility dig [@global-server] [domain ...
- bzoj题目大体分类
http://m.blog.csdn.net/article/details?id=51387623
- golang——字符串与编码
1.字符编码 (1)ASCII码 一个字节表示的英文.数字.标点符号等字符. 国际标准ASCII码为0-127即128个字符,二进制最高位为0,其余为扩展ASCII码. (2)GB2312 两字节,主 ...
- 清理TIME_WAIT
cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_tw_reuse=1net.ipv4.tcp_tw_recycle=1net.ipv4.t ...
- GIt学习之路 第二天 创建版本库
本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 创建版本库 阅读: 1859216 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文 ...
- SQL数据库——静态成员
静态: 1.普通成员普通成员都是属于对象的用对象调用 2.静态成员静态成员是属于类的用类名调用 stactic 静态关键字 静态方法里面不能包含普通成员普通方法里面可以包含静态成员 静态: 1.普通成 ...
- RS485通信和Modbus协议(转)
转自:http://www.51hei.com/bbs/dpj-23230-1.html 在工业控制.电力通讯.智能仪表等领域,通常情况下是采用串口通信的方式进行数据交换.最初采用的方式是RS232接 ...
- iOS检测耳机插入拔出
首先,需要导入两个框架 然后,注册通知检测耳机的插入与拔出操作 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sel ...
- 【工具】前端Photoshop
前端photoshop最常见问题: 字体单位换成像素:按下ctrl+k调出首选项,选择单位与标尺,在里面把文字单位由点改为像素就行了.不过要注意的是,点是很多软件里面文字的默认单位.像素是虚拟单位,如 ...