您的位置:首页 > 其它

codeforces #323 div 2 C. GCD Table (暴力?)

2015-10-04 16:16 519 查看
C. GCD Table

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

The GCD table G of size n × n for an array of positive integers a of length n is defined by formula

/*************************************************************************
> File Name: code/#323/CC.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年10月04日 星期日 14时51分08秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>

#define yn hez111qqz
#define j1 cute111qqz
#define ms(a,x) memset(a,x,sizeof(a))
using namespace std;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
const int N=5E2+7;
int a[N*N];
int n;
map<int,int>mp;
vector<int> ans;
int gcd(int a,int b) { return b==0?a:gcd(b,a%b);}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
mp.clear();
scanf("%d",&n);
for ( int i = 0 ; i < n*n ; i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
}
sort(a,a+n*n);

for ( int i = n*n-1 ;i>=0 ; i--)
{
if (!mp[a[i]]) continue;
mp[a[i]]--;
for ( int j = 0 ; j< ans.size() ; j++)
{
int tmp = gcd(a[i],ans[j]);
mp[tmp]--;
mp[tmp]--;
}
ans.push_back(a[i]);
}
for ( int i = 0 ; i < n-1 ; i++)
printf("%d ",ans[i]);
printf("%d",ans[n-1]);

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}


View Code


偶数次
然后我从大往小加,直到有n个为止(算上之前的奇数次的)
错了是因为我想当然得以为...n个数的gcd table 中...对于任意一对gcd(a,b)(a!=b)一定小于等于所有的原始数列的数...
这显然不对...因为第四点的小于等于关系只是针对某对数的局部结论,而没办法推到整体...想当然了.好傻逼啊...

正确的做法其实很接近了...
由于第四个可以知道...最大的数一定是原始数列中的数...
我们可以从已经确定为原始数列的数来排除掉答案.
由于对称性...将a[i]排序后从大到小枚举...每个新出现的数和已经确定为原始数列的数为产生一对相等的gcd...减掉就可以了.
具体见代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: