luogu3935 Calculating
标题也许叫整除分块吧
求\(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的更多相关文章
- 长时间停留在calculating requirements and dependencies 解决方案
如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...
- 长时间停留在calculating requirements and dependencies 的解决方案
如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...
- Calculating Stereo Pairs
Calculating Stereo Pairs Written by Paul BourkeJuly 1999 Introduction The following discusses comput ...
- Calculating simple running totals in SQL Server
Running total for Oracle: SELECT somedate, somevalue,SUM(somevalue) OVER(ORDER BY somedate ROWS BETW ...
- Codeforces Round #277 (Div. 2) A. Calculating Function 水题
A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...
- cf486A Calculating Function
A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Android ADT安装时卡在Calculating requirements and dependencies
AndroidSDK及Eclipse安装都很顺利,但是在Eclipse下安装ADT插件时,先采用点击Help->installnew software->Add...,无论输入https: ...
- 长时间停留在calculating requirements and dependencies
如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 )这个问题通常就是在点击安装之后显示“Calcu ...
- 洛谷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 ...
随机推荐
- MySQL 同一实例不同库之间表同步(Otter 应用)
1 需求 在同一台服务器同一MySQL实例中的source库和target库都存在student表.如果source库中该表发生增删改操作时,也需要体现到target库的student表中: 2 解决 ...
- find a way to escape--hdu1593
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1593 找到二者角速度相等时水中人的R,在此之前二者保持在一条直线上,之后水中的人沿直线到岸边S点匀 ...
- Java-SpringMvc-响应Html代码展示
代码 @RequestMapping(value = "/test.do", method = {RequestMethod.GET}) public void test(Http ...
- 计划评审技术PERT
概念 编辑 PERT(Program Evaluation and Review Technique)即 [2] 计划评审技术,最早是由美国海军在计划和控制北极星导弹的研制时发展起来的.PERT技术 ...
- 记两个国外CTF的弱pwn
两道题都来自CSAW CTF 18.PWN学得不够多,如果哪里错了,欢迎留言交流. 第一个题 get_it checksec检查之后,发现栈保护没开,很可能是栈溢出.IDA打开F5看伪源码. int ...
- [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- linux 引导流程二
grep -v “^#” /etc/inittab | more 提取etc文件中的有效行. 用命令man 可以获得配置文件和命令的帮助信息.配置文件必须是系统的配置文件或系统默认安装的某个服务的配 ...
- 前端开发必备之Chrome开发者工具(上篇)
本文介绍的 Chrome 开发者工具基于 Chrome 65版本,如果你的 Chrome 开发者工具没有下文提到的那些内容,请检查下 Chrome 的版本 简介 Chrome 开发者工具是一套内置于 ...
- cc150 --链表中倒数第k个节点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个. public class Solution { public Li ...
- 机器学习中的范数规则化 L0、L1与L2范数 核范数与规则项参数选择
http://blog.csdn.net/zouxy09/article/details/24971995 机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http: ...