【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的序列的所有数 ...
随机推荐
- activity工作流表结构分析
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.结构设计 1.1. 逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository( ...
- Vue.js实战 5.5章 购物车
<!DOCTYPE html> <html lang="en"> <head> <title>购物车示例</title> ...
- vue 加载文件,省略后缀后的加载顺序
Vue使用import ... from ...来导入组件,库,变量等.而from后的来源可以是js,vue,json.这个是在webpack.base.conf.js中设置的: module.exp ...
- MySQL基础 — 详细安装
MySQL--安装 打开MySQL 5.5 安装文件开始: 点击Next 打上勾,再点击Next 点击Custom,说明如下: Typical(典型安装) Installs the mo ...
- 17年day2
/* 嗯,明天就出发了. 嗯,终于快要结束了. 考试日常挂T1 今天晚上老师们请我们吃水饺,还有一个大蛋糕. 虽然没怎么吃蛋糕23333 还好我的水饺是白菜馅的~~~ 晚上学哥学姐们发视频送祝福,谢谢 ...
- Android 性能优化(1)性能工具之「 lint 」 :Improving Your Code with lint:优化代码
Improving Your Code with lint 1.See Also lint (reference) Using Android Annotations In addition to t ...
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- 支持多种格式的播放器js代码
FLV需要播放器,其它视频格式直接插入相应的代码即可. ------------------------------------- /** *视频播放 by zhensheng@ *参数说明 ...
- opencv总结
2018-02-2623:59:02 唉,这软件我很烦躁,今天又搞了好几遍,出错提示的时候总是出问题! 而且,无论什么错误,都是提示一堆乱码! 定义ROI区域有两种方法,第一种是使用cv:Rect.顾 ...