当前位置:网站首页>【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
2022-07-29 00:53:00 【ZhgDgE】
#607. 平方计数
题意:从序列 M M M 个数中顺序选出 N ( n ≤ 1 0 6 ) N(n\leq 10^6) N(n≤106) 个不同的数, 使得这 N N N 个数的字典序最小。
思路:如果点对 ( i , j ) (i,j) (i,j) 满足 a i 2 + a j a_i ^2+a_j ai2+aj 为完全平方数,那么 a j = ( x + a i ) × ( x − a i ) a_j=(x+a_i)\times (x-a_i) aj=(x+ai)×(x−ai) 。我们可以枚举 a j a_j aj 的因子对,然后就能得到满足条件的 a i a_i ai 是多少。
这里要用到上一个文章里类似的优化。筛一个数的因子的方法:
- 暴力根号枚举,时间复杂度 O ( 0 ∼ n ) O(0\sim \sqrt n) O(0∼n)
- 埃氏筛优化,枚举每个数的倍数,那么该数一定是倍数的因子,就能得到一个因子对。时间复杂度 O ( n × log n ∼ log n ) O(n\times \log n\sim \log n) O(n×logn∼logn)
这道题用的是第二个优化。不过代码实现的话没有必要储存起来因子对。
rep(i, 1, 1000000){
for(int j = i; j <= 1000000; j += i){
if(j / i - i < 0) continue;
// (i, j / i) 即为一对因子对
}
}
AC代码:http://oj.daimayuan.top/submission/301674
#608. 字典序最小
题意:从序列 M M M 个数中顺序选出 N N N 个不同的数, 使得这 N N N 个数的字典序最小。 ( n , m ≤ 1 0 6 , 1 ≤ a i ≤ N ) (n,m\leq 10^6,1\leq a_i \leq N) (n,m≤106,1≤ai≤N) ,数据保证 [ 1 , N ] [1,N] [1,N] 范围内每个数至少出现一次。
思路:和上一场的 div2 的题的贪心思路很像。首先我们要向最小字典序去靠近,也就是 [ 1 , N ] [1,N] [1,N] 原排列。于是我们维护一个栈,向后扫,把栈尾部所有比新来的数大的数弹出来,新数放进去。这样能贪心的向最小字典序靠拢。但是我们的决策可能出现问题,如果后面没有和要弹出的数相等的元素,就会无解。因此,预处理每个数后面是否还有数,如有数则当前数可弹出;否则不可弹出,因为会造成无解。
AC代码:http://oj.daimayuan.top/submission/292414
#614. “Z”型矩阵
题意:问一个矩阵有多少个子矩阵满足矩阵形状为 z 。
题解:(离线/树状数组)代码源每日一题 Div1“Z”型矩阵
思路:枚举矩阵端点是 O ( n 4 ) O(n^4) O(n4) 的。如何优化?我们在对角线上求解。对于每个对角线,上面的每一个点都有左向线、右向线,我们从右上向左下扫对角线,扫的过程中按照左向线的长度维护,存到树状数组里。因为每个左向线都有一个生存周期,我们标记每个左向线的结束时间。对于右向线,能和当前右向线匹配的一定是长度小于等于右向线的仍然存活的左向线,查询树状数组即可。
边栏推荐
- 规划数学期末模拟考试一
- 【搜索】—— DFS之剪枝与优化
- What is the ISO assessment? How to do the waiting insurance scheme
- Cloud native application comprehensive exercise
- Openpyxl cell center
- Timer of BOM series
- matplotlib中文问题
- It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
- [hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
- Comprehensive upgrade, complete collection of Taobao / tmall API interfaces
猜你喜欢

We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos

【HCIP】MPLS 基础

How many of the top ten test tools in 2022 do you master

了解各种路径

Redis is installed on Linux
![About df['a column name'] [serial number]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
About df['a column name'] [serial number]

了解网址url的组成后 运用url模块、querystring模块和mime模块完善静态网站

560 and K

Nacos installation guide on win system
![[hcip] two mGRE networks are interconnected through OSPF (ENSP)](/img/fe/8bb51ac48f52d61e8d31af490300bb.png)
[hcip] two mGRE networks are interconnected through OSPF (ENSP)
随机推荐
T-sne降维
Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value
JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
BOM系列之定时器
[机缘参悟-54]:《素书》-1-事物缘起[原始章第一]:大道至简。
Analyze OP based on autoware_ global_ Planner global path planning module re planning
10 major network security incidents in the past 10 years
云原生应用综合练习上
Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
云原生应用综合练习下
Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device
Basic label in body
ELS square movement
golang启动报错【已解决】
How to protect WordPress website from network attack? It is essential to take safety measures
mysql的执行顺序
[search] - iteration deepening / bidirectional dfs/ida*
DSP震动座椅
Cross modal alignment 20220728
Reinforcement learning (II): SARS, with code rewriting