标题也许叫整除分块吧

求\(1\)到\(n\)因数的个数\(\sum_{i=1}^n(\sum_{d|n}1)\)

范围\(1e14\)时限3s

\(n\sqrt{n}\)的暴力铁定gg

分开考虑

\(1\)到\(n\)中含有\(1\)因数的个数有\(n/1\)个

含有2因数的个数有\(n/2\)个**

······

含有n因数的个数有\(n/n\)个

问题就转化为求\(\sum_{i=1}^{n}[\frac{n}{i}]\)

然后我们就可以把\(O(n\sqrt{n})\)的暴力转化为\(O(n)\)了

可还是过不了&1e14的数据&

我们发现,我们求得\(\frac{n}{i}\)在一段区间内是连续的

而且呈现单调递减,这样我们就可以开心的套用二分啦

那到底有多少段连续的区间

把i分开考虑

1到\(\sqrt{n}\)之内,if都不同撑死有\(\sqrt{n}\)段

\(\sqrt{n}\)到n之内,求\(\frac{n}{i}\)连续的一段,取值范围为1到\(\sqrt{n}\)之内,撑死也有\(\sqrt{n}\)个

区间个数是\(\sqrt{n}\)级别的,二分是\(log\)级别的

所以复杂度为\(O(\sqrt{n}logn)\)

一直以为这是根号的%>_<%

参见牛客练习赛25(1e9)

#include <bits/stdc++.h>
using namespace std;
long long ans;
int l,n;
int main() {
int q;
cin>>q;
while(q--) {
cin>>n;
l=1;
ans=0;
for(int i=1; i<=n; ++i) {
int r=n;
int mid=(l+r)>>1;
while(n/l!=n/r) {
mid=(l+r)>>1;
r=mid;
}
ans+=n/l*(r-l+1);
if(r==n) break;
l=r+1;
}
cout<<ans<<"\n";
}
return 0;
}

直到我遇到了这个题luogu3935以及评测80sTLE的惨痛

才发现我是个zz诶

\(i\) \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\) \(11\) \(12\)
\(n/i\) \(12\) \(6\) \(4\) \(3\) \(2\) \(2\) \(1\) \(1\) \(1\) \(1\) \(1\) \(1\)

当我们知道\(l\)的时候,也就是一段的开头,如何快速找到我们要的r呢

\(n/l\)是\(n\)中含有\(t=n/l\)块完整的\(l\)

那么\(n/t\)便是有\(t\)块最大的数,便是我们要求的\(r\)

所以\(r=n/(n/l)\)

所以我们求块的时间由二分的\(O(logn)\)变为了\(O(1)\)

复杂度为\(O(\sqrt{n})\)

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=998244353;
ll solve(ll n)
{
ll ans=0;
for(ll l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
ans+=(r-l+1)%mod*(n/l)%mod;
ans%=mod;
}
return ans;
}
int main()
{
ll x,y;
cin>>x>>y;
cout<<((solve(y)-solve(x-1))%mod+mod)%mod;
return 0;
}

http://www.cnblogs.com/1000Suns/p/9193713.html

luogu3935 Calculating的更多相关文章

  1. 长时间停留在calculating requirements and dependencies 解决方案

    如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...

  2. 长时间停留在calculating requirements and dependencies 的解决方案

    如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...

  3. Calculating Stereo Pairs

    Calculating Stereo Pairs Written by Paul BourkeJuly 1999 Introduction The following discusses comput ...

  4. Calculating simple running totals in SQL Server

    Running total for Oracle: SELECT somedate, somevalue,SUM(somevalue) OVER(ORDER BY somedate ROWS BETW ...

  5. Codeforces Round #277 (Div. 2) A. Calculating Function 水题

    A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...

  6. cf486A Calculating Function

    A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. Android ADT安装时卡在Calculating requirements and dependencies

    AndroidSDK及Eclipse安装都很顺利,但是在Eclipse下安装ADT插件时,先采用点击Help->installnew software->Add...,无论输入https: ...

  8. 长时间停留在calculating requirements and dependencies

    如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 )这个问题通常就是在点击安装之后显示“Calcu ...

  9. 洛谷P3935 Calculating (莫比乌斯反演)

    P3935 Calculating 题目描述 若xx分解质因数结果为\(x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n},令f(x)=(k_1+1)(k_2+1)\cdots ...

随机推荐

  1. Kubernetes容器调度

    Kubernetes的调度器是Kubernetes众多组件的一部分,独立于API服务器之外.调度器本身是可插拔的,任何理解调度器和API服务器之间调用关系的工程师都可以编写定制的调度器.本文后面的介绍 ...

  2. 【PyQt5-Qt Designer】读取txt文件在打印

    from PyQt5.QtGui import QFont,QTextDocument,QTextCursor from PyQt5.QtWidgets import QApplication, QM ...

  3. ifconfig 查看网卡信息

    [root@linux-node- sss]# ifconfig eno16777736: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu inet ...

  4. CentOS工作内容(五)单一网卡配置多个IP

    CentOS工作内容(五)单一网卡配置多个IP 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+e 移动到当前行的开头(e end) ct ...

  5. 002-nginx-在 nginx 反向代理中使用域名,配置动态域名解析

    一.概述 代理(proxy),即中间人,它代替客户端发送请求给服务器,收到响应后再转给客户端.通常意义上的代理是从用户的角度讲的,用户通过某个代理可以访问多个网站,这个代理是靠近用户的,比如某些公司可 ...

  6. 分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容

    问题的产生 在写JS的过程中,为了调试我们常常会写很多 console.log.console.info.console.group.console.warn.console.error代码来查看JS ...

  7. 深入理解Fabric环境搭建的详细过程(转)

    前面的准备工作我就不用多说了,也就是各种软件和开发环境的安装,安装好以后,我们git clone下来最新的代码,并切换到v1.0.0,并且下载好我们需要使用的docker镜像,也就是到步骤6,接下来我 ...

  8. (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  9. [LeetCode] 196. Delete Duplicate Emails_Easy tag: SQL

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  10. EXTJS 4:在renderer中如何控制一个CheckColumn的行为,如显示,只读等属性

    在编写grid下的column时,大家肯定会经常用到renderer这个方法来改变文字的呈现形式,那么如果该column是一个特殊的column,比如CheckColumn时,该方法应该怎样写呢?官方 ...