题意:

给一些无序的数字,求解一个矩阵,使得矩阵的每一个元素都是行和列标志数的gcd,输出行标志数。

首先对数字进行排序。复杂度n*log(n^2)。

这题的证明有官方的英文题解==在这直接贴英文题解...

这题我事后自己反思了一下,其实思路并不难,而且一开始自己思考的方向也对。

问题出在两方面:

1.对平衡树等数据结构的应用还是缺乏理解的。遇到问题不知道可以用这些方法来解决。

2.思考缺乏理性和条理。没有按部就班得思考。

Let the answer be a1 ≤ a2 ≤ ... ≤ an. We will use the fact that gcd(ai, aj) ≤ amin(i, j).

It is true that gcd(an, an) = an ≥ ai ≥ gcd(ai, aj) for every 1 ≤ i, j ≤ n. That means that an is equal to maximum element in the table. Let set an to maximal element in the table and delete it from table elements set. We've deleted gcd(an, an), so the set now contains all gcd(ai, aj), for every 1 ≤ i, j ≤ n and 1 ≤ min(i, j) ≤ n - 1.

By the last two inequalities gcd(ai, aj) ≤ amin(i, j) ≤ an - 1 = gcd(an - 1, an - 1). As soon as set contains gcd(an - 1, an - 1), the maximum element in current element set is equal to an - 1. As far as we already know an, let's delete the gcd(an - 1, an - 1), gcd(an - 1, an), gcd(an, an - 1)from the element set. Now set contains all the gcd(ai, aj), for every 1 ≤ i, j ≤ n and1 ≤ min(i, j) ≤ n - 2.

We're repeating that operation for every k from n - 2 to 1, setting ak to maximum element in the set and deleting the gcd(ak, ak), gcd(ai, ak), gcd(ak, ai) for every k < i ≤ n from the set.

One could prove correctness of this algorithm by mathematical induction. For performing deleting and getting maximum element operations one could use multiset or map structure, so solution has complexity .

来源:http://codeforces.com/blog/entry/20692

这里学了一下multiset的各种应用....

#include<stdio.h>
#include<queue>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;
int n;
int ttmp[];
multiset<int>s;
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int main()
{
int n,tmp;
scanf("%d",&n);
for(int i=;i<=n*n;i++)
{
scanf("%d",&tmp);
s.insert(tmp);
}
int num=;
ttmp[num]=*s.rbegin();
s.erase(s.find(*s.rbegin()));
while(num<n)
{
num++;
ttmp[num]=*s.rbegin();
s.erase(s.find(*s.rbegin()));
for(int i=num-;i>=;i--)
{
s.erase(s.find(gcd(ttmp[i],ttmp[num])));
s.erase(s.find(gcd(ttmp[i],ttmp[num])));
}
}
for(int i=;i<=n;i++)
{
printf("%d ",ttmp[i]);
}
return ;
}

CodeForces 582A【multiset使用样例】的更多相关文章

  1. C++的性能C#的产能?! - .Net Native 系列《三》:.NET Native部署测试方案及样例

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  2. MarkDown+LaTex 数学内容编辑样例收集

    $\color{green}{MarkDown+LaTex 数学内容编辑样例收集}$ 1.大小标题的居中,大小,颜色 [例1] $\color{Blue}{一元二次方程根的分布}$ $\color{R ...

  3. 33个超级有用必须要收藏的PHP代码样例

    作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...

  4. 关于peersim样例配置文件的超详细解读(新手勿喷)

    相信很多兄弟一开始接触peersim,对配置文件还是有点不适应,我看了好久的样例的配置文件,一层层去找对应的文件的方法,终于好像悟懂了一点,记下来以后回顾. 贴上代码,一点点分析. 首先要说下所谓的配 ...

  5. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  6. CloudSim样例分析

    自带八个样例描述: cloudsim-2.1.1\examples目录下提供了一些CloudSim样例程序,每个样例模拟的环境如下: (1)CloudSimExample1.Java:创建一个一台主机 ...

  7. 样例20-汽车SHOW

    观看样例点这里 素材下载 1.设置场景大小为400*3002.执行:文件->导入->导入到库,选择需要的汽车图片文件,将其导入到库面板中3.按照同样的方式,在库面板中导入所需的背景音乐文件 ...

  8. java使用xsd校验xml样例

    知识点:XSD文件是指XML结构定义 ( XML Schemas Definition )文件,是DTD的替代品.可以用一个指定的XML Schema来验证某个XML文档,以检查该XML文档是否符合其 ...

  9. java servlet 代码样例 (demo)

    今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧. 这个web逻辑 ...

随机推荐

  1. 【HEVC帧间预测论文】P1.4 Motion Vectors Merging: Low Complexity Prediction Unit Decision

    Motion Vectors Merging: Low Complexity Prediction Unit Decision Heuristic for the inter-Prediction o ...

  2. 第16周翻译:SQL Server中的事务日志管理,级别3:事务日志、备份和恢复

    源自: http://www.sqlservercentral.com/articles/Stairway+Series/73779/ 作者: Tony Davis, 2011/09/07 翻译:刘琼 ...

  3. linux截图工具

    推荐:deepin-scrot 满足功能: 能够自定义快捷键(Ctrl+Alt+A) 小巧快速自定义选择区域进行截图 有简单的绘图功能 可以快速的保存到剪切版(双击图片) P.S.:双重截图

  4. iview table 普通表格样式

    iview table 普通表格样式 https://run.iviewui.com/UvLFPMb0 <template> <table> <thead> < ...

  5. MyBatis基本运行环境

    MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...

  6. Perl中 qw 是 “quoted Word” 或是 “quoted by whitespace”的简写

    Perl中 qw 是 “quoted Word” 或是 “quoted by whitespace”的简写 eg: @strict_pragma = qw ( a b c);

  7. dpdk快速编译使用

    QuickStart 环境 dpdk: dpdk-17.11 运行前配置 配置系统HugePages #mkdir /mnt/huge_1GB/ #vim /etc/fstab nodev /mnt/ ...

  8. luogu P3393 逃离僵尸岛-搜索剪枝+spfa

    P3393 逃离僵尸岛 题目描述 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被 ...

  9. curl 模拟post请求

    curl -H "Content-Type:application/json" -X POST --data '{"openId":"xxxxxxx& ...

  10. scp免密码拷贝和ssh免密码登录

    版权声明:本文为博主原创文章,未经允许不得转载. 在平常的工作中经常在两个服务器之间传输文件,而且经常从本地远程登录服务器,每次都要输入密码显然很没效率,这时候该怎么办呢? 首先假设服务器A和B,要想 ...