Divide Interval 题解
背景
太逊了,调了三次才调出来,所以写篇题解寄念。LC好睿智
题意
给你两个数
分析
这道题有一个一眼贪心。在一次变化后不会超过
int p(int x)
{
int ans=1;
while(x%ans==0)
{
ans=ans<<1;
}
if(x%ans)ans/=2;
return ans;
}
然后计算出
细节
如果
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int w=1,s=0;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)){s=s*10+(ch-'0');ch=getchar();}
return w*s;
}
const int maxn=6e7+10;
int l,r;
int p(int x)//得出最大因数
{
int ans=1;
while(x%ans==0)
{
ans=ans<<1;
}
if(x%ans)ans/=2;
return ans;
}
int lo(int x)//得出最大2的整次幂
{
int i;
for(i=1;i<=x;i*=2);
if(i>x)i/=2;
return i;
}
int ansl[maxn],ansr[maxn],tot;//记录答案
signed main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
cin>>l>>r;
while(l<r)
{
if(l==r)break;
ansl[++tot]=l;
if(l==0)//特判
{
int pr=lo(r);
l=pr;
ansr[tot]=l;
continue;
}
int p2=p(l);
int bei=l/p2;//计算n和m
while(p2*(bei+1)>r)//向下缩小n
{
p2=p2>>1;
bei=l/p2;
}
l=p2*(bei+1);
ansr[tot]=l;
}
cout<<tot<<endl;
for(int i=1;i<=tot;i++)
cout<<ansl[i]<<' '<<ansr[i]<<endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】