hdu5902 GCD is Funny

3D坐标系转换实战:如何用Python快速实现旋转矩阵与平移向量的组合变换 本文详细介绍了如何使用Python和NumPy实现3D坐标系转换中的旋转矩阵与平移向量的组合变换。通过齐次坐标和变换矩阵的数学原理,结合代码示例,帮助开发者高效处理机器人路径规划和计算机视觉中的空间变换问题,并解决实际应用中的常见错误。 阅读详情

GCD is Funny

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 354    Accepted Submission(s): 83


Problem Description
Alex has invented a new game for fun. There are n integers at a board and he performs the following moves repeatedly:

1. He chooses three numbers a, b and c written at the board and erases them.
2. He chooses two numbers from the triple a, b and c and calculates their greatest common divisor, getting the number d (d maybe gcd(a,b), gcd(a,c) or gcd(b,c)).
3. He writes the number d to the board two times.

It can be seen that after performing the move n−2 times, there will be only two numbers with the same value left on the board. Alex wants to know which numbers can left on the board possibly. Can you help him?
 

Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤100), indicating the number of test cases. For each test case:

The first line contains an integer n (3≤n≤500) -- the number of integers written on the board. The next line contains n integers: a1,a2,...,an (1≤ai≤1000)-- the numbers on the board.
 

Output
For each test case, output the numbers which can left on the board in increasing order.
 

Sample Input
3 4 1 2 3 4 4 2 2 2 2 5 5 6 2 3 4
 

Sample Output
1 2 2 1 2 3
 


BC这一场这道题真是惨烈……MyRoom只有一人过的……什么时候BC第一题这么难过了……

本来是想直接两两GCD去重输出,因为当时想的是a, b, c, e, f

取a, b, c,那么gcd(a, b) = d,丢掉c,最后得到d, d, e, f,然后再取

d, d, e,那么gcd(d, d) = d,丢掉e,最后得到d, d, f,然后再gcd(d, d) = d,

丢掉f,最后得到d, d

也就是说任选两对都可以最后得到这两对的gcd作为最后结果,数据太水,pretest过了就没多想


后来仔细想想,谁跟你说d, d, e的时候一定选gcd(d, d) = d丢掉e的……也可能gcd(d, e) = x,丢掉d啊……这样结果就可能变成3个数,4个数或者更多个数的GCD了


这样的话也就是说在我原来的想法中,每一次求gcd都有可能做一次gcd(d, e) = x,从而变成多一个数的gcd,也就是说最后的答案应该是序列中任选i个数的gcd(2 <= i <= n - 1),因为第一次操作无论如何都会直接丢弃一个数,所以最多只能求出来n - 1个数的GCD


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 500;
int T, n, a[maxn + 5];
bool g[1000 + 5]; //用来标记某gcd是否之前出现过,用于去重

int gcd(int m, int n) {
	if (!n) return m;
	return gcd(n, m % n);
}

int main()
{
	cin >> T;
	while (T--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		memset(g, false, sizeof(g));
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				g[gcd(a[i], a[j])] = true;
			}
		}
		bool flag = true;
		//需要跑n - 3次,因为第一步的操作相当于去掉了3个元素,然后把gcd的初始信息存储在g中了
		//不能多也不能少,多了的话就有可能计算出n个数的gcd了,但最多只能是n - 1个的,因为无论如何第一步操作会直接丢弃一个数
		int cnt = 1;
		while (flag && cnt <= n - 3) {
			cnt++;
			flag = false;
			for (int i = 1; i <= 1000; i++) {
				if (g[i]) {
					for (int j = 0; j < n; j++) {
						if (!g[gcd(a[j], i)]) {
							flag = true;
							g[gcd(a[j], i)] = true;
						}
					}
				}
			}
		}
		bool first = true;
		for (int i = 1; i <= 1000; i++) {
			if (g[i]) {
				if (first) {
					printf("%d", i);
					first = false;
				}
				else {
					printf(" %d", i);
				}
			}
		}
		puts("");
	}
	return 0;
}



tkinter动态表格 - 实时更新数据(TkinterTable) tkinter动态表格 - 实时更新数据(TkinterTable) 参考 [TkinterTable] Tkinter上的表格GUI】 https://github.com/dmnfarrell/tkintertable/wiki/Usage https://github.com/dmnfarrell/tkintertable/issues/47 python使用tkinter编写一个数据显示窗口(实时显示更新的数据) 首先安装TkinterTable,即pip install tki 阅读详情

相关推荐

Pixhawk RPi CM4 Baseboard 树莓派CM4安装Ubuntu20.04 server 配置ros mavros mavsdk

cm4配置ros mavros连接pixhawk4 Pixhawk RPi CM4 Baseboard

baidu_41704597的博客 2127

hdu 5902 GCD is Funny

Problem Description Alex has invented a new game for fun. There arenintegers at a board and he performs the following moves repeatedly:1. He chooses three numbersa,bandcwritten at the boa...

awow80285的博客 167

图解凸函数与凹函数:别再被数学定义搞晕了!

本文通过图解和生活化类比,深入浅出地解释了凸函数与凹函数的数学定义及其与日常直觉的差异。文章详细介绍了凸函数的图像特征、数学定义(包括二阶导数判断)、在机器学习中的重要性,以及常见误区和实际应用中的判断技巧,帮助读者彻底理解这一关键数学概念。

weixin_29159441的博客 648

HDU5902

问题描述 Alex发明了一个有趣的游戏. 一开始他在黑板上写了nn个正整数, 然后他开始重复进行如下的操作: 他选择黑板上三个数字aa, bb和cc, 把他们从黑板上擦掉. 他从这三个数aa, bb和cc中选择了两个数, 并计算出他们的最大公约数, 记这个数为dd (dd 可以是\gcd(a,b)gcd(a,b), \gcd(a,c)gcd(a,c)或者\gcd(b, c)gcd(b,c)). 他

NineFailure的博客 544

GEO芯片数据下载和探针ID转换(保姆级教程)

R语言安装、GEO芯片数据下载、探针ID转换

qq_43138237的博客 3万+

CANoe实战:手把手教你用J1939.dbc发送超8字节长帧报文(附完整CAPL代码)

本文详细解析了使用CANoe和J1939.dbc实现超8字节长帧报文的分包发送技术,提供完整的CAPL代码实现与优化策略。内容涵盖J1939.dbc配置、分包发送模块设计、接收端处理逻辑,并与ISO 15765-2协议进行深度对比,帮助开发者构建高效可靠的商用车通信解决方案。

weixin_29169899的博客 392

HDU-5902-GCD is Funny解题笔记

Alex has invented a new game for fun. There arenintegers at a board and he performs the following moves repeatedly:1. He chooses three numbersa,bandcwritten at the board and erases them....

darren0424的博客 243

C#:实现统计图像主颜色的算法(附完整源码)

C#:实现统计图像主颜色的算法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 517

HDU - 5902 GCD is Funny 数论(GCD相关)

思路: 首先这个题我们可能想到的就是任意两个数的 gcd , 但是这样还不够,可能会有 已经得出的gcd 跟其他的数产生新的 gcd,所以,这个题的答案就是 n 个数所有子集的gcd (上面这些想了好久没能证明== 感谢Q巨 tls 九哥 等大神 瞬间给出我没想出来的反例,当头一棒,叫我清醒,对数学更加喜欢) #include #include #include #include

床前明月光 396

STM32(HAL库)驱动OLED

通过STM32F103C8T6单片机采用HAL库方式对0.96寸OLED(IIC通讯)屏幕进行驱动。

weixin_44597885的博客 8789

【C2000系列DSP的双向DC-DC电源】基于TMS320F28034的DC-DC项目开发流程

理解DC-DC 双向电源(基于 TMS320F28034)的开发流程,需要从功能拆解、开发步骤、验证 / 测试项

昔时扬尘处 323

Hdu-5902-GCD is Funny (gcd)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5902题目大意: 给定n个数,每次操作可以取其中3个,选择其中两个作gcd运算,并向原串中添加两次gcd结果,即a,b,c三个数,可取a,b作gcd(a,b)=g,将g写入原序列两次,求进行n-2次操作后,留下的数可能是什么分析: 每次操作,取三个数,放回两个数,同时放回的数必定是原序列中数两两g

SD_Stjean的博客 401

Power domain 划分该如何下手

假如PD1比PD3更always on的话,我们只需要插入普通buffer即可,因为PD3 on,PD1也肯定是ON,PD1 off,PD3肯定时off,PD1能cover PD3的电源情况,因此使用普通buffer也可,但是处于省电角度,我们还是使用AO buffer;在我们做多power domain时,在切分power domain时,需要谨慎,稍有不慎,就会出现congestion以及timing问题,那问题来了,我们在切分power domain时有什么考究么,今天小编就和大家一起讨论讨论。

芯冰乐 206

HDU-5902-GCD is Funny

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5902 题意:给出数组a[],从中取出3个数,放回其中任意两个数的GCD 两次,直到最后剩下两个数字,问最后剩下的数字可能有什么。 题解: 跑n-2轮两两GCD,不断更新gcd数组直到无新的数产出。 CODE: #include //#pragma comment(linker, "/STACK

不会吃鱼的喵 479

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

HDU5902 GCD is Funny【GCD】

GCD is Funny Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2577 Accepted Submission(s): 626 Problem Description Alex has invented a new game for fun. There are n integers at a board and he perfo

海岛Blog 402

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

『HDU 5902』GCD is Funny

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5902 转载链接:http://blog.youkuaiyun.com/xtulollipop/article/details/52664876题意: 个人感想: 当时BC的时候我一看到题目想都不想 就直接用n^2的跑一遍两个数得gcd就是答案了… 当场交直接AC,玛德智障,原来出题人标程都搞错

Gavinjou大笨象的博客 522

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

[HDU 5902] GCD is Funny

一、题目

C202044zxy的博客 169

hdu5902GCD is Funny+数论

Problem Description Alex has invented a new game for fun. There are n integers at a board and he performs the following moves repeatedly: He chooses three numbers a, b and c written at the board and e

xtulollipop 1197

HDU - 5902 GCD is Funny 思维+数学推导+gcd

题意: 黑板上有n个数,任意选择其中三个,把其中两个的gcd加入到黑板上,之前的三个数删去; 问最后剩下的两个数(相同)可能是什么,会有很多种情况 思路: 首先我们可以想到的是任意两个数求gcd,然后后面每一次都用这两个数的gcd,最后就会剩下这个数了; 但是这样还不够,有可能我们求完gcd的这个数会和其他的数产生新的gcd,例如:30,42,14,15;所以答案就是n个数一些子集的gc...

床前明月光 557

HDU 5902 GCD is Funny 数学

GCD is Funny 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5902 Description Alex has invented a new game for fun. There are n integers at a board and he performs the following moves repeatedly: He ...

weixin_30248399的博客 118

HDU 5902 GCD is Funny(GCD)

GCD is Funny Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 468    Accepted Submission(s): 125 Problem Description Alex has invent

KeepTing 635

HDU 5902:GCD is Funny (GCD)

GCD is Funny Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 306    Accepted Submission(s): 65 Problem Description Alex has invente

小坏蛋_千千 828
上一篇: codeforces 712B Memory and Trident
下一篇: hdu5904 LCIS dp
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值