A15526 | Almost All Multiples
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Given two integers $n$ and $x$ , a permutation $^{\dagger}$ $p$ of length $n$ is called funny if $p_i$ is a multiple of $i$ for all $1 \leq i \leq n - 1$ , $p_n = 1$ , and $p_1 = x$ .
Find the lexicographically minimal $^{\ddagger}$ funny permutation, or report that no such permutation exists.
$^{\dagger}$ A permutation of length $n$ is an array consisting of each of the integers from $1$ to $n$ exactly once.
$^{\ddagger}$ Let $a$ and $b$ be permutations of length $n$ . Then $a$ is lexicographically smaller than $b$ if in the first position $i$ where $a$ and $b$ differ, $a_i < b_i$ . A permutation is lexicographically minimal if it is lexicographically smaller than all other permutations.
Find the lexicographically minimal $^{\ddagger}$ funny permutation, or report that no such permutation exists.
$^{\dagger}$ A permutation of length $n$ is an array consisting of each of the integers from $1$ to $n$ exactly once.
$^{\ddagger}$ Let $a$ and $b$ be permutations of length $n$ . Then $a$ is lexicographically smaller than $b$ if in the first position $i$ where $a$ and $b$ differ, $a_i < b_i$ . A permutation is lexicographically minimal if it is lexicographically smaller than all other permutations.
输入格式
The input consists of multiple test cases. The first line contains an integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. The description of the test cases follows.
The only line of each test case contains two integers $n$ and $x$ ( $2 \leq n \leq 2 \cdot 10^5$ ; $1 < x \leq n$ ).
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$ .
The only line of each test case contains two integers $n$ and $x$ ( $2 \leq n \leq 2 \cdot 10^5$ ; $1 < x \leq n$ ).
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, if the answer exists, output $n$ distinct integers $p_1, p_2, \dots, p_n$ ( $1 \leq p_i \leq n$ ) — the lexicographically minimal funny permutation $p$ . Otherwise, output $-1$ .
输入输出样例
输入 #1
3 3 3 4 2 5 4
输出 #1
3 2 1 2 4 3 1 -1
In the first test case, the permutation $[3,2,1]$ satisfies all the conditions: $p_1=3$ , $p_3=1$ , and:
- $p_1=3$ is a multiple of $1$ .
- $p_2=2$ is a multiple of $2$ .
In the second test case, the permutation $[2,4,3,1]$ satisfies all the conditions: $p_1=2$ , $p_4=1$ , and:
- $p_1=2$ is a multiple of $1$ .
- $p_2=4$ is a multiple of $2$ .
- $p_3=3$ is a multiple of $3$ .
We can show that these permutations are lexicographically minimal.
No such permutations exist in the third test case.
- $p_1=3$ is a multiple of $1$ .
- $p_2=2$ is a multiple of $2$ .
In the second test case, the permutation $[2,4,3,1]$ satisfies all the conditions: $p_1=2$ , $p_4=1$ , and:
- $p_1=2$ is a multiple of $1$ .
- $p_2=4$ is a multiple of $2$ .
- $p_3=3$ is a multiple of $3$ .
We can show that these permutations are lexicographically minimal.
No such permutations exist in the third test case.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted