C. Maximum Set
C. Maximum Set
A set of positive integers is called beautiful if, for every two integers and from this set, either divides or divides (or both).
You are given two integers and . Consider all beautiful sets consisting of integers not less than and not greater than . You have to print two numbers:
- the maximum possible size of a beautiful set where all elements are from to ;
- the number of beautiful sets consisting of integers from to with the maximum possible size.
Since the second number can be very large, print it modulo .
Input
The first line contains one integer () — the number of test cases.
Each test case consists of one line containing two integers and ().
Output
For each test case, print two integers — the maximum possible size of a beautiful set consisting of integers from to , and the number of such sets with maximum possible size. Since the second number can be very large, print it modulo .
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 to is . There are such sets which have the maximum possible size:
- ;
- ;
- ;
- .
解题思路
先解决第一个问题,集合中最多可以有多少个数?不失一般性的,假设集合中的数从小到大为共个数,其中。很明显从集合中任取两个数,小的那个数一定能整除大的那个数。为了使得集合中的数尽可能多,我们很自然想到应该取尽可能小的,因此可以每个都取,同时取,那么此时集合中最大的数就是,因此要满足,因此有,因此最大可以取到。
按照上面的方法都已经取到集合数量的最大值了,为什么还会有多种方案呢?这是因为不一定能整除,意味着如果所有的都取,在保证集合中有个元素的前提下,的取值不一定是。实际上,因此的取值范围是。这时就已经有种方案了。
然后也不一定要全部取,还可以将其中一个变成。考虑一下如果某个,那么我们可以把这个拆成,集合中的元素会增加。因此每个。如果有两个,那么我们那么我们把拆成,集合中的元素也会增加。因此最多只有一个。
对于有一个的情况,的取值范围是,其中取的位置有个,因此这种情况就有种方案。
两种情况加起来,那么答案就是
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17176919.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-03-03 乘积最大