CF2158F2.Distinct GCDs (Hard Version)
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The difference between the versions is that in this version, n≤5000. You can hack only if you solved all versions of this problem.
Legend has it that when Gauss was a young schoolboy, his teacher tasked the class with summing the integers from 1 to 100, likely as a way to keep them occupied for a while. However, young Gauss quickly came up with the formula sum=2n(n+1) and found the answer in mere moments. Centuries later, Gauss appears before you in a nightmare with a daunting task...
You are given a positive integer n, find a sequence of integers [a1,a2,…,an] such that 1≤ai≤1018 for all 1≤i≤n, and the GCDs of pairwise adjacent elements of a are all distinct. Formally,
gcd(ai,ai+1)=gcd(aj,aj+1) for each 1≤i<j<n
Additionally, a should have the minimum possible number of distinct elements.
这是该问题的困难版本。两个版本的区别在于,在本版本中,n≤5000。仅当您解决了该问题的所有版本时,才可进行 Hack。
传说高斯尚为小学生时,他的老师曾要求全班同学计算从 1 到 100 的所有整数之和,这很可能是为了让他们暂时安静下来。然而,年少的高斯迅速推导出了公式 sum=2n(n+1),并在片刻之间就得出了答案。几个世纪之后,高斯却在你的噩梦中现身,并交给你一项艰巨的任务……
给定一个正整数 n,请构造一个整数序列 [a1,a2,…,an],使得对所有 1≤i≤n 均满足 1≤ai≤1018,且序列 a 中所有相邻元素对的最大公约数(GCD)互不相同。形式化地,
对每个 1≤i<j<n,均有 gcd(ai,ai+1)=gcd(aj,aj+1)。
此外,序列 a 中不同元素的个数应尽可能少。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤200). The description of the test cases follows.
The first and only line of each test case contains a single integer n (2≤n≤5000) — the size of the sequence to be found.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤200)。随后是测试用例的描述。
每个测试用例仅有一行,包含一个整数 n(2≤n≤5000)—— 即待求序列的长度。
输出格式
For each test case, output n space-separated positive integers a1,a2,…,an (1≤ai≤1018) on a new line that satisfy the condition in the statement. If there are multiple solutions, print any of them.
It can be proven that under the problem constraints, a solution always exists.
对于每个测试用例,在一行内输出 n 个以空格分隔的正整数 a1,a2,…,an(1≤ai≤1018),使其满足题目陈述中的条件。若存在多个解,输出任意一个即可。
可以证明,在本题的约束条件下,解总是存在的。
输入输出样例
输入#1
3 2 5 7
输出#1
2 2 1 4 4 6 6 4 4 6 6 9 9 4
说明/提示
For the second test case, the GCDs of adjacent elements are [gcd(1,4),gcd(4,4),gcd(4,6),gcd(6,6)]=[1,4,2,6], all of which are distinct.
For the third test case, the GCDs of adjacent elements are [gcd(4,4),gcd(4,6),gcd(6,6),gcd(6,9),gcd(9,9),gcd(9,4)]=[4,2,6,3,9,1], all of which are distinct.
For each test case, it can be proven that no sequence with fewer distinct elements exists such that all adjacent GCDs are distinct.
对于第二个测试用例,相邻元素的最大公约数(GCD)序列为 [gcd(1,4),gcd(4,4),gcd(4,6),gcd(6,6)]=[1,4,2,6],其中所有值互不相同。
对于第三个测试用例,相邻元素的最大公约数(GCD)序列为 [gcd(4,4),gcd(4,6),gcd(6,6),gcd(6,9),gcd(9,9),gcd(9,4)]=[4,2,6,3,9,1],其中所有值互不相同。
对于每个测试用例,均可证明:不存在元素种类数更少的序列,使得所有相邻元素的最大公约数互不相同。
输入解题思路,AI测评打分。不知道怎么写?