C. Maximum Set

C. Maximum Set

A set of positive integers $S$ is called beautiful if, for every two integers $x$ and $y$ from this set, either $x$ divides $y$ or $y$ divides $x$ (or both).

You are given two integers $l$ and $r$. Consider all beautiful sets consisting of integers not less than $l$ and not greater than $r$. You have to print two numbers:

  • the maximum possible size of a beautiful set where all elements are from $l$ to $r$;
  • the number of beautiful sets consisting of integers from $l$ to $r$ with the maximum possible size.

Since the second number can be very large, print it modulo $998244353$.

Input

The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases.

Each test case consists of one line containing two integers $l$ and $r$ ($1 \le l \le r \le 10^6$).

Output

For each test case, print two integers — the maximum possible size of a beautiful set consisting of integers from $l$ to $r$, and the number of such sets with maximum possible size. Since the second number can be very large, print it modulo $998244353$.

Example

input

4
3 11
13 37
1 22
4 100

output

2 4
2 6
5 1
5 7

Note

In the first test case, the maximum possible size of a beautiful set with integers from $3$ to $11$ is $2$. There are $4$ such sets which have the maximum possible size:

  • $\{ 3, 6 \}$;
  • $\{ 3, 9 \}$;
  • $\{ 4, 8 \}$;
  • $\{ 5, 10 \}$.

 

解题思路

  先解决第一个问题,集合中最多可以有多少个数?不失一般性的,假设集合中的数从小到大为$p, \ k_1 \cdot p, \ k_1 \cdot k_2 \cdot p, \ \ldots , \ k_1 \cdots k_m \cdot p $共$m+1$个数,其中$k_i > 1$。很明显从集合中任取两个数,小的那个数一定能整除大的那个数。为了使得集合中的数尽可能多,我们很自然想到应该取尽可能小的$k_i$,因此可以每个$k_i$都取$2$,同时$p$取$l$,那么此时集合中最大的数就是$l \cdot 2^{m}$,因此要满足$l \cdot 2^{m} \leq r$,因此有$m \leq \left\lfloor {\log \left\lfloor \frac{r}{l} \right\rfloor} \right\rfloor$,因此$m$最大可以取到$\left\lfloor {\log \left\lfloor \frac{r}{l} \right\rfloor} \right\rfloor$。

  按照上面的方法都已经取到集合数量的最大值了,为什么还会有多种方案呢?这是因为$l$不一定能整除$r$,意味着如果所有的$k_i$都取$2$,在保证集合中有$m+1$个元素的前提下,$p$的取值不一定是$l$。实际上$p \leq \left\lfloor \frac{r}{2^m} \right\rfloor$,因此$p$的取值范围是$p \in \left[l, \left\lfloor \frac{r}{2^m} \right\rfloor \right]$。这时就已经有$\left\lfloor \frac{r}{2^m} \right\rfloor - l + 1$种方案了。

  然后$k_i$也不一定要全部取$2$,还可以将其中一个$k_i$变成$3$。考虑一下如果某个$k_i \geq 4$,那么我们可以把这个$k_i$拆成$2 \cdot 2$,集合中的元素会增加。因此每个$k_i \leq 3$。如果有两个$k_i = 3$,那么我们那么我们把$3 \cdot 3$拆成$2 \cdot 2 \cdot 2$,集合中的元素也会增加。因此最多只有一个$k_i = 3$。

  对于有一个$k_i = 3$的情况,$p$的取值范围是$p \in \left[l, \left\lfloor \frac{r}{2^{m-1} \cdot \  3} \right\rfloor \right]$,其中取$3$的位置有$m$个,因此这种情况就有$\left( \left\lfloor \frac{r}{2^{m-1} \cdot \ 3} \right\rfloor - l + 1 \right) \times m$种方案。

  两种情况加起来,那么答案就是$$\left\lfloor \frac{r}{2^m} \right\rfloor - l + 1 + \left( \left\lfloor \frac{r}{2^{m-1} \cdot 3} \right\rfloor - l + 1 \right) \times m$$

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 void solve() {
 5     int l, r;
 6     scanf("%d %d", &l, &r);
 7     int m = __lg(r / l);
 8     printf("%d %d\n", m + 1, ((r >> m) - l + 1) + max(0, (r / 3 >> m - 1) - l + 1) * m);
 9 }
10 
11 int main() {
12     int t;
13     scanf("%d", &t);
14     while (t--) {
15         solve();
16     }
17     
18     return 0;
19 }

 

参考资料

  Educational Codeforces Round 144 Editorial:https://codeforces.com/blog/entry/113408

posted @ 2023-03-03 21:53  onlyblues  阅读(40)  评论(0编辑  收藏  举报
Web Analytics