CF1981D Turtle and Multiplication 题解

Turtle and Multiplication

传送门

Turtle just learned how to multiply two integers in his math class, and he was very excited.

Then Piggy gave him an integer n n n , and asked him to construct a sequence a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an consisting of integers which satisfied the following conditions:

  • For all 1 ≤ i ≤ n 1 \le i \le n 1in , 1 ≤ a i ≤ 3 ⋅ 1 0 5 1 \le a_i \le 3 \cdot 10^5 1ai3105 .
  • For all 1 ≤ i < j ≤ n − 1 1 \le i < j \le n - 1 1i<jn1 , a i ⋅ a i + 1 ≠ a j ⋅ a j + 1 a_i \cdot a_{i + 1} \ne a_j \cdot a_{j + 1} aiai+1=ajaj+1 .

Of all such sequences, Piggy asked Turtle to find the one with the minimum number of distinct elements.

Turtle definitely could not solve the problem, so please help him!

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 ). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2n106 ) — the length of the sequence a a a .

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 6 10^6 106 .

Output

For each test case, output n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an — the elements of the sequence a a a .

If there are multiple answers, print any of them.

Example

input #1

3
2
3
4

output #1

114514 114514
1 2 2
3 3 4 4

Note

In the third test case, a = [ 3 , 4 , 2 , 6 ] a = [3, 4, 2, 6] a=[3,4,2,6] violates the second condition since a 1 ⋅ a 2 = a 3 ⋅ a 4 a_1 \cdot a_2 = a_3 \cdot a_4 a1a2=a3a4 . a = [ 2 , 3 , 4 , 4 ] a = [2, 3, 4, 4] a=[2,3,4,4] satisfy the conditions but its number of distinct elements isn’t minimum.

题面翻译

题目描述

请构造一个长度为 n n n 的整数数列 { a n } \{a_n\} {an},满足:

  • 1 ≤ a i ≤ 3 × 1 0 5 1 \le a_i \le 3 \times 10^5 1ai3×105
  • ∀ 1 ≤ i < j ≤ n − 1 \forall 1 \le i<j\le n-1 ∀1i<jn1 ,有 a i a i + 1 ≠ a j a j + 1 a_ia_{i+1} \ne a_ja_{j+1} aiai+1=ajaj+1 ,即:相邻两项之积两两互不相等。

在满足上述要求的基础上,需要让数列中的不同元素个数最少。

输入输出格式

输入第一行一个整数 t t t 表示数据组数。

每组数据的输入包含一行一个整数 n n n ,代表数列长度。

对于每组数据,请输出一行 n n n 个整数,中间由空格分隔,代表你构造的数列。

1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 , 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2n106

对于一个测试点中所有测试数据, ∑ n ≤ 1 0 6 \sum n \le 10^6 n106

注明

以上来自 C o d e F o r c e s ,翻译来源:洛谷。 以上来自CodeForces,翻译来源:洛谷。 以上来自CodeForces,翻译来源:洛谷。

BlastMike:额,好像缺了啥?
BestMonkey:CodeForces 你什么时候能完善一下你的 Markdown 格式啊!你知道我复制下来题面还要一个一个删多余的空格吗!你看看人家洛谷啊!
BlastMike:你好像之前说过。

解题思路

可以想到填入质数。题目中 a i ≤ 3 × 1 0 5 a_i\leq 3\times 10^5 ai3×105 ,其中的质数一定可以满足组成。

BlastMike:
在这里插入图片描述
Banksy:愣着干嘛,转图论啊。
Mike:我来冒个泡。

假设我们要填入 N N N 个不同的质数,在任意两个质数之间连边,由于质数可以相同,则可以出现自环,这样子问题就转化为了选择一个质数开始并不断沿着边走 n n n 步,不能走重复的边。

BeatMonkey:这是一个欧拉路问题。
Bell:废话。

  • 如果 N N N 为奇数:度数为 N + 1 N+1 N+1 的点的数量为偶数,必存在一个欧拉回路,最多能形成长度为 N ( N + 1 ) 2 \frac{N(N+1)}{2} 2N(N+1) 的序列。
  • 如果 N N N 为偶数:删去其中的 N 2 2 − 1 \frac{N^2}{2}-1 2N21 条边且删去的边没有两条边有公共的端点,必存在一个欧拉路径,最多能够形成长度为 N 2 2 + 1 \frac{N^2}{2}+1 2N2+1 的路径。

找到最小的能形成长度超过 n n n 的路径的 N N N,再建图跑欧拉路即可。

Code

#include<bits/stdc++.h>
using namespace std;
#define Man main
const int Maxn=3e5+5,Maxm=1500;
int M,Prime_List[Maxn];
bool Mark[Maxn];
bitset<Maxm>Vis[Maxm];
stack<int>Stk;
inline void DFS(int u){
    if(Vis[u][u]){
		Vis[u][u]=0;
		DFS(u);
	}
    for(register int i=Vis[u]._Find_first();i<=M;i=Vis[u]._Find_first())
        if(Vis[u][i]){
            Vis[u][i]=Vis[i][u]=0;
			DFS(i);
		}
    Stk.push(Prime_List[u]);
}
inline void Solve(){
	int n;
	cin>>n;
    if(n==1){
		cout<<1<<endl;
		return;
	}
    for(M=1;(M*(M+1)/2)-((M&1)?0ll:((M>>1)-1))<n-1;++M);
	    for(register int i=1;i<=M;++i)
	        Vis[i].set();
    if(!(M&1))
        for(register int i=3;i<=M;i+=2)
			Vis[i][i+1]=Vis[i+1][i]=0;
    DFS(1);
    while(!Stk.empty()&&n){
		cout<<Stk.top()<<" ";
		Stk.pop();
		n--;
	}
    while(!Stk.empty())
		Stk.pop();
    cout<<endl;
}
signed Man(){
    for(register int i=2,top=0;i<=13000;++i){
        if(!Mark[i])
			Prime_List[++top]=i;
        for(register int j=1;j<=top&&i*Prime_List[j]<=13000;++j){
            Mark[i*Prime_List[j]]=1;
            if(!(i%Prime_List[j]))
				break;
        }
    }
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	int T;
	cin>>T;
    while(T--)
		Solve();
    return 0;
}
  • 16
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值