F. Oppa Funcan Style Remastered

Codeforces
IDCF986F
Time5000ms
Memory256MB
Difficulty
graphsmathnumber theoryshortest paths
English · Original
Chinese · Translation
Formal · Original
Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find original description [here](http://acm.timus.ru/problem.aspx?space=1&num=2107&locale=en)): On the ground there are $n$ platforms, which are numbered with integers from $1$ to $n$, on $i$\-th platform there is a dancer with number $i$. Further, every second all the dancers standing on the platform with number $i$ jump to the platform with the number $f(i)$. The moving rule $f$ is selected in advance and is not changed throughout the clip. The duration of the clip was $k$ seconds and the rule $f$ was chosen in such a way that after $k$ seconds all dancers were in their initial positions (i.e. the $i$\-th dancer stood on the platform with the number $i$). That allowed to loop the clip and collect even more likes. PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video. In his case "enhanced version" means even more insanity, so the number of platforms can be up to $10^{18}$! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all $x$ from $1$ to $n$ $f(x) \neq x$ must hold. Big part of classic video's success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well. PSY hasn't decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule $f$ for different options. ## Input In the first line of input there is one integer $t$ ($1 \le t \le 10^{4}$) — the number of options for $n$ and $k$ to check. In the next $t$ lines the options are given: each option is described with two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^{15}$) — the number of dancers and the duration in seconds. **It is guaranteed that the number of different values of $k$ in one test is not greater than $50$.** ## Output Print $t$ lines. If the $i$\-th option of the video is feasible, print "_YES_" (without quotes) in $i$\-th line, otherwise print "_NO_" (without quotes). [samples]
你一定看过韩国歌手PSY的疯狂视频,比如《江南Style》、《绅士》和《Daddy》。你可能也听说过PSY两年前录制了视频《Oppa Funcan Style》(遗憾的是我们未能在互联网上找到它)。我们将为你回顾一下这首热门歌曲的原版样子(你可以在原描述中找到更多信息): 地面上有 $n$ 个平台,编号为从 $1$ 到 $n$ 的整数,第 $i$ 个平台上有一个编号为 $i$ 的舞者。接下来,每一秒,所有站在编号为 $i$ 的平台上的舞者都会跳到编号为 $f(i)$ 的平台上。移动规则 $f$ 在拍摄前就已选定,并在整个视频中保持不变。 视频时长为 $k$ 秒,且规则 $f$ 被选择为使得 $k$ 秒后所有舞者都回到了初始位置(即第 $i$ 个舞者站在编号为 $i$ 的平台上)。这使得视频可以循环播放,从而获得更多的点赞。 PSY知道,经典艺术作品的增强版每天都在变得越来越流行。因此,他决定发布他视频的重制版。 在他看来,“增强版”意味着更加疯狂,平台数量可以高达 $10^{18}$!但视频导演指出,如果某个舞者始终停留在同一个平台上,观众会感到无聊并立即关闭视频。因此,对于所有 $x$ 从 $1$ 到 $n$,必须满足 $f(x) \ne x$。 经典视频成功的重要部分在于其循环播放,因此在重制版中,所有舞者在视频结束时也必须回到初始位置。 PSY尚未确定确切的平台数量和视频时长,因此他请你检查不同选项下是否存在一个合适的规则 $f$。 输入的第一行包含一个整数 $t$($1 \le t \le 10^4$)——表示需要检查的 $n$ 和 $k$ 的选项数量。 接下来的 $t$ 行给出各个选项:每个选项由两个整数 $n$ 和 $k$ 描述($1 \le n \le 10^{18}$,$1 \le k \le 10^{15}$)——分别表示舞者数量和视频时长(秒)。 *保证在一个测试用例中,不同的 $k$ 值的数量不超过 50。* 请输出 $t$ 行。如果第 $i$ 个视频选项可行,请在第 $i$ 行输出 "_YES_"(不含引号);否则输出 "_NO_"(不含引号)。 ## Input 在输入的第一行中有一个整数 $t$($1 \le t \le 10^4$)——表示需要检查的 $n$ 和 $k$ 的选项数量。接下来的 $t$ 行给出各个选项:每个选项由两个整数 $n$ 和 $k$ 描述($1 \le n \le 10^{18}$,$1 \le k \le 10^{15}$)——分别表示舞者数量和视频时长(秒)。*保证在一个测试用例中,不同的 $k$ 值的数量不超过 50。* ## Output 请输出 $t$ 行。如果第 $i$ 个视频选项可行,请在第 $i$ 行输出 "_YES_"(不含引号);否则输出 "_NO_"(不含引号)。 [samples]
**Definitions** Let $ t \in \mathbb{Z}^+ $ be the number of test cases. For each test case, let $ n \in \mathbb{Z}^+ $ be the number of platforms (dancers), and $ k \in \mathbb{Z}^+ $ be the duration in seconds. Let $ f: \{1, 2, \dots, n\} \to \{1, 2, \dots, n\} $ be a permutation (since dancers move bijectively between platforms). **Constraints** 1. $ 1 \le t \le 10^4 $ 2. $ 1 \le n \le 10^{18} $, $ 1 \le k \le 10^{15} $ 3. $ f(x) \ne x $ for all $ x \in \{1, \dots, n\} $ (no fixed points) 4. $ f^k(x) = x $ for all $ x \in \{1, \dots, n\} $ (all dancers return to initial positions after $ k $ seconds) **Objective** For each test case $ (n, k) $, determine whether there exists a permutation $ f $ on $ n $ elements with: - No fixed points: $ \forall x, f(x) \ne x $, - $ f^k = \text{id} $ (the identity permutation). That is, the permutation $ f $ has order dividing $ k $, and all its cycle lengths divide $ k $, but no cycle has length 1. Thus, the problem reduces to: > Does there exist a partition of $ n $ into positive integers $ \ell_1, \ell_2, \dots, \ell_m \ge 2 $ such that $ \sum_{i=1}^m \ell_i = n $ and $ \ell_i \mid k $ for all $ i $? Equivalently: > Can $ n $ be expressed as a sum of integers, each at least 2 and each dividing $ k $? **Final Formal Statement** For given $ n, k \in \mathbb{Z}^+ $, output "YES" if and only if: $$ \exists \text{ a multiset } S \subseteq D_k \setminus \{1\} \text{ such that } \sum_{\ell \in S} \ell = n, $$ where $ D_k = \{ d \in \mathbb{Z}^+ : d \mid k \} $ is the set of positive divisors of $ k $. Equivalently, define $ m = \min(D_k \setminus \{1\}) $ if $ D_k \setminus \{1\} \ne \emptyset $, else $ m = \infty $. Then the condition is: - If $ k = 1 $: then $ D_k = \{1\} $, so $ D_k \setminus \{1\} = \emptyset $ → impossible unless $ n = 0 $, but $ n \ge 1 $ → **NO**. - Else: let $ m = \min\{ d \mid d \mid k, d \ge 2 \} $. Then we can form any $ n \ge \text{some minimal value} $ using multiples of $ m $, but we must check if $ n $ is representable as a nonnegative integer combination of divisors of $ k $ that are $ \ge 2 $. However, since we can use any number of any divisor $ \ge 2 $ of $ k $, and the set of allowed cycle lengths is $ D_k \setminus \{1\} $, the necessary and sufficient condition is: - $ n \ge \min(D_k \setminus \{1\}) $, **and** - $ n $ is representable as a sum of elements from $ D_k \setminus \{1\} $. But since the set includes all divisors $ \ge 2 $ of $ k $, and we can use arbitrarily many copies, the problem reduces to the **Frobenius coin problem** with multiple denominations. However, since we have all divisors of $ k $ greater than 1, and the smallest such divisor is $ m $, then: > **If $ m \le n $, and $ m \mid k $, $ m \ge 2 $, then since we can use $ m $ repeatedly, we can form all $ n \ge m $ that are congruent to some residue modulo $ m $** — but we are not restricted to one cycle length. We can use any divisor $ d \ge 2 $ of $ k $. Actually, the key insight: Let $ m = \min\{ d \in \mathbb{Z} : d \mid k, d \ge 2 \} $. Then the set $ D_k \setminus \{1\} $ contains $ m $, and since $ m \mid k $, and $ m \ge 2 $, we can form any integer $ n \ge m $ as a sum of $ m $'s if $ n $ is a multiple of $ m $? No — we can use other divisors too. But since $ m \mid d $ for all $ d \in D_k $ (because $ m $ is the smallest divisor $ \ge 2 $, and all divisors are multiples of the smallest prime factor), then **all elements of $ D_k \setminus \{1\} $ are multiples of $ m $**. Therefore, any sum of them is a multiple of $ m $. Thus: - Necessary: $ m \mid n $ - Also: $ n \ge m $ But wait — is that sufficient? Suppose $ m \mid n $ and $ n \ge m $. Then we can write $ n = q \cdot m $, with $ q \ge 1 $, and use $ q $ cycles of length $ m $. Since $ m \mid k $, each cycle of length $ m $ satisfies $ f^k(x) = x $. And $ m \ge 2 $, so no fixed points. Thus, the condition is: > Let $ m = \min\{ d \mid d \mid k, d \ge 2 \} $. > Then the answer is "YES" if and only if $ m \le n $ and $ m \mid n $. But what if $ k = 1 $? Then $ D_k = \{1\} $, so $ D_k \setminus \{1\} = \emptyset $, so no possible cycle lengths $ \ge 2 $ → "NO". What if $ k \ge 2 $? Then $ m $ exists. But what if $ n < m $? Then even the smallest allowed cycle is too big → "NO". Thus: **Final Answer Condition** Let $ m = \begin{cases} \min\{ d \in \mathbb{Z}^+ : d \mid k, d \ge 2 \} & \text{if } k \ge 2 \\ \infty & \text{if } k = 1 \end{cases} $ Then: $$ \boxed{\text{YES} \iff k \ge 2 \text{ and } m \le n \text{ and } m \mid n} $$ But note: $ m $ is the **smallest prime factor** of $ k $, because the smallest divisor $ \ge 2 $ of any integer $ k \ge 2 $ is its smallest prime factor. So we can restate: Let $ p = \text{smallest prime divisor of } k $. Then: - If $ k = 1 $: NO - Else if $ n < p $: NO - Else if $ p \nmid n $: NO - Else: YES But wait — is it sufficient to use only cycles of length $ p $? Yes, because $ p \mid k $, so $ f^k = \text{id} $, and $ p \ge 2 $, so no fixed points. And if $ p \mid n $, then $ n = p \cdot q $, and we can have $ q $ disjoint cycles of length $ p $. So the condition is: > "YES" if and only if $ k \ge 2 $ and the smallest prime factor of $ k $ divides $ n $. **Final Formal Statement** **Definitions** Let $ t \in \mathbb{Z}^+ $ be the number of test cases. For each test case, let $ n, k \in \mathbb{Z}^+ $. Let $ p(k) = \min\{ d \in \mathbb{Z}^+ : d \mid k, d > 1 \} $ — the smallest prime factor of $ k $. **Constraints** 1. $ 1 \le t \le 10^4 $ 2. $ 1 \le n \le 10^{18} $, $ 1 \le k \le 10^{15} $ **Objective** For each test case $ (n, k) $, output "YES" if and only if: $$ k \ge 2 \quad \text{and} \quad p(k) \mid n $$ Otherwise, output "NO".
Samples
Input #1
3
7 7
3 8
5 6
Output #1
YES
NO
YES
API Response (JSON)
{
  "problem": {
    "name": "F. Oppa Funcan Style Remastered",
    "description": {
      "content": "Surely you have seen insane videos by South Korean rapper PSY, such as \"Gangnam Style\", \"Gentleman\" and \"Daddy\". You might also hear that PSY has been recording video \"Oppa Funcan Style\" two years ago",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF986F"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Surely you have seen insane videos by South Korean rapper PSY, such as \"Gangnam Style\", \"Gentleman\" and \"Daddy\". You might also hear that PSY has been recording video \"Oppa Funcan Style\" two years ago...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你一定看过韩国歌手PSY的疯狂视频,比如《江南Style》、《绅士》和《Daddy》。你可能也听说过PSY两年前录制了视频《Oppa Funcan Style》(遗憾的是我们未能在互联网上找到它)。我们将为你回顾一下这首热门歌曲的原版样子(你可以在原描述中找到更多信息):\n\n地面上有 $n$ 个平台,编号为从 $1$ 到 $n$ 的整数,第 $i$ 个平台上有一个编号为 $i$ 的舞者。接下来,每...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ t \\in \\mathbb{Z}^+ $ be the number of test cases.  \nFor each test case, let $ n \\in \\mathbb{Z}^+ $ be the number of platforms (dancers), and $ k \\in \\mathbb{Z}^+ $ be the durat...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments