Divide Interval 题解
背景
太逊了,调了三次才调出来,所以写篇题解寄念。LC好睿智
题意
给你两个数 \(a,b\),现在要从 \(a\) 跑到 \(b\),每次可以将当前的 \(a\) 拆分成 \(2^n\times m(n,m\in N)\) 的形式,并将它变成 \(2^n\times (m+1)\)。问最少变几次能跑到 \(b\),输出次数和每次变化前后 \(a\) 的值。
分析
这道题有一个一眼贪心。在一次变化后不会超过 \(b\) 的情况下,我们要让 \(n\) 的值尽可能大来使得 \(a\) 变化后更大。所以我们可以写一个函数来先找到 \(n\) 最大可以是多少,具体就是看看 \(a\) 的因数中最大的 \(2\) 的整次幂是多少,下面给出:
int p(int x)
{
int ans=1;
while(x%ans==0)
{
ans=ans<<1;
}
if(x%ans)ans/=2;
return ans;
}
然后计算出 \(m\),并判断这样拆分后一次变化是否会超过 \(b\),如果超过就让 \(n>>1\),直到满足条件。因为要先输出变化次数,所以用两个数组记录每次变化前后 \(a\) 的值即可。
细节
如果 \(a\) 的初值为 \(0\),我们发现此时 \(n\) 可以是任意值,所以我们特判一下,直接找到不大于 \(b\) 的最大的 \(2\) 的整次幂,让 \(a\) 变成它就行了。
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;
}
Divide Interval 题解的更多相关文章
- 算法与数据结构基础 - 排序(Sort)
排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...
- 【题解】The Great Divide [Uva10256]
[题解]The Great Divide [Uva10256] 传送门:\(\text{The Great Divide [Uva10256]}\) [题目描述] 输入多组数据,每组数据给定 \(n\ ...
- [LeetCode]题解(python):057-Insert Interval
题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Codeforces 311D Interval Cubing 数学 + 线段树 (看题解)
Interval Cubing 这种数学题谁顶得住啊. 因为 (3 ^ 48) % (mod - 1)为 1 , 所以48个一个循环节, 用线段树直接维护. #include<bits/stdc ...
- 题解-CF1401E Divide Square
题面 CF1401E Divide Square 给一个正方形平面边长为 \(10^6\),给 \(n\) 条横线段和 \(m\) 条竖线段,每条线段都与正方形边缘相交且一条直线上不会有两条线段,求被 ...
- UVA10375 选择与除法 Choose and divide 题解
题目链接: https://www.luogu.org/problemnew/show/UVA10375 分析: 这道题可以用唯一分解定理来做. 什么是唯一分解定理?百度即可,这里也简介一下. 对于任 ...
- CH 4302 Interval GCD 题解
题意 给定一个长度为N的数列A,以及M条指令 (N≤5* 10^5, M<=10^5),每条指令可能是以下两种之一: "C l r d",表示把 A[l],A[l+1],-, ...
- 【CF1445D】Divide and Sum 题解
题目链接 题意简介 将一个长度为 2n 的数列平均分为两个子数列 p 和 q 后,p 按从小到大排序,q 按从大到小排序. 排序后,记 p 为 \(\{x_i\}\) ,q 为 \(\{y_i\}\) ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
随机推荐
- 推荐几款卓越的 .NET 开源搜索组件
前言 在当今日益数据化的世界中,信息的检索和搜索功能对于各种应用来说变得至关重要. 无论是电子商务网站.企业资源规划系统.还是内容管理系统,高效的搜索功能都是提升用户体验.促进业务发展的关键. 因此, ...
- C# winform GDI+ 五子棋 (二):根据博弈算法写的人机AI(抄的别人的)
白棋是ai,最后ai走赢了. 根据博弈算法的一个AI.遍历深度6层,下子很慢.其实我是从别人的代码里复制的算法,改到自己上面用了. 这个博弈算法 class GameAI { /// <summ ...
- CSS---鼠标悬浮时逐渐变大样式
.tuijian_2:hover{ transform: scale(1.3); transition: all 1s; }
- JS / jQuery 刷新页面的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue cli之使用Vue-CLI初始化创建前端项目
1.生成项目目录 使用vue自动化工具可以快速搭建单页应用项目目录. 该工具为现代化的前端开发工作流提供了开箱即用的构建配置.只需几分钟即可创建并启动一个带热重载.保存时静态检查以及可用于生产环境的构 ...
- mac http&git代理配置
git代理清除git config --global --unset http.proxygit config --global --unset https.proxy 输出代理:$echo $htt ...
- python获取豆瓣电影TOP250的所有电影的相关信息
参考文档:https://weread.qq.com/web/reader/37132a705e2b2f37196c138k98f3284021498f137082c2e 说明:我才接触网络爬虫,在看 ...
- vue3 Suspense
在Vue.js 3中,Suspense 是一个用于处理异步组件的特殊组件,它允许你在等待异步组件加载时展示备用内容.这对于优化用户体验.处理懒加载组件或异步数据获取时非常有用.Suspense 的主要 ...
- LeetCode 409. Longest Palindrome 最长回文串(C++/Java)
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...
- work09
day10作业: 第一题: 定义一个接口Animal,包含: 1.抽象方法: run() 2.默认方法: breathe(),输出"动物会呼吸",同时调用静态方法 eat(),私有 ...