LightOJ 1098(均值不等式,整除分块玄学优化)
We all know that any integer number n is divisible by 1 and n. That is why these two numbers are not the actual divisors of any numbers. The function SOD(n) (sum of divisors) is defined as the summation of all the actual divisors of an integer number n. For example,
SOD(24) = 2+3+4+6+8+12 = 35.
The function CSOD(n) (cumulative SOD) of an integer n, is defined as below:
Given the value of n, your job is to find the value of CSOD(n).
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case contains an integer n (0 ≤ n ≤ 2 * 109).
Output
For each case, print the case number and the result. You may assume that each output will fit into a 64 bit signed integer.
Sample Input |
Output for Sample Input |
3 2 100 200000000 |
Case 1: 0 Case 2: 3150 Case 3: 12898681201837053 |
题意:求1-n(n<=2e9)之间每个数的SOD之和,定义SOD(x)为x除去1和自己以外的所有因数之和
题解:首先考虑O(n)做法
对于数i,在1-n的因子中出现的次数为(n/i)-1
所产生的贡献是[(n/i)-1]*i
这样得到了O(n)的算法
for (int i=; i<=n; ++i)
{
sum+=(n/i-)*i;
}
但是数据的范围是2e9显然是会TLE的
我们考虑优化,如果你做过一些莫比乌斯反演的题目,你会下意识地发现:当i非常大的时候,因子出现次数很久才会变化一次,比如说(n/3+1)~n/2之间可能有几万个数但是他们都只出现了一次,这种情况就可以使用整除分块来优化
这些数是连续的,他们出现的次数又相同,sum=i*time+(i+1)*time+....+(i+k)*time 发现了吗?这就是个等差数列,可以直接用等差数列求和公式搞。但是这个东西直接将次数从出现一次枚举到出现n次复杂度还是O(n)的
怎么办呢?思考一下对于出现1-sqrt(n)次的数字我们进行第二种操作,那么出现sqrt(n)+1次的数字有几个?只有n/[sqrt(n)+1]左右个了,对于这sqrt(n)个数字,我们可以直接用第一种方法暴力搞
总的复杂度是O(sqrt(n))的
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,pos,next,lim;
long long ans; int main()
{
int t,ttt=;
scanf("%d",&t);
while(t--)
{
ans=0ll;
ttt++;
scanf("%d",&n);
lim=sqrt(n);
pos=n/;
for(int i=; i<=lim; i++)
{
next=n/(i+);
ans+=1ll*(pos-next)*(pos+next+)*(i-)/;
pos=next;
}
for(int i=; i<=pos; i++)
{
ans+=1ll*i*(n/i-);
}
printf("Case %d: %lld\n",ttt,ans);
}
}
这题还是非常高妙的啊~
LightOJ 1098(均值不等式,整除分块玄学优化)的更多相关文章
- C - A New Function (整除分块 + 玄学优化)
题目链接:https://cn.vjudge.net/contest/270608#problem/C 题目大意:给你一个n,让你求从1->n中间每个数的因子之和(每个数在求因子的过程中不包括本 ...
- LOJ #2802. 「CCC 2018」平衡树(整除分块 + dp)
题面 LOJ #2802. 「CCC 2018」平衡树 题面有点难看...请认真阅读理解题意. 转化后就是,给你一个数 \(N\) ,每次选择一个 \(k \in [2, N]\) 将 \(N\) 变 ...
- 一种基于均值不等式的Listwise损失函数
一种基于均值不等式的Listwise损失函数 1 前言 1.1 Learning to Rank 简介 Learning to Rank (LTR) , 也被叫做排序学习, 是搜索中的重要技术, 其目 ...
- 洛谷 P6788 - 「EZEC-3」四月樱花(整除分块)
题面传送门 题意: 求 \[\prod\limits_{x=1}^n\prod\limits_{y|x}\frac{y^{d(y)}}{\prod\limits_{z|y}z+1} \pmod{p} ...
- 洛谷 P2257 - YY的GCD(莫比乌斯反演+整除分块)
题面传送门 题意: 求满足 \(1 \leq x \leq n\),\(1 \leq y \leq m\),\(\gcd(x,y)\) 为质数的数对 \((x,y)\) 的个数. \(T\) 组询问. ...
- 洛谷 P5518 - [MtOI2019]幽灵乐团 / 莫比乌斯反演基础练习题(莫比乌斯反演+整除分块)
洛谷题面传送门 一道究极恶心的毒瘤六合一题,式子推了我满满两面 A4 纸-- 首先我们可以将式子拆成: \[ans=\prod\limits_{i=1}^A\prod\limits_{j=1}^B\p ...
- 51Nod 1225 余数之和 [整除分块]
1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...
- [Bzoj 2956] 模积和 (整除分块)
整除分块 一般形式:\(\sum_{i = 1}^n \lfloor \frac{n}{i} \rfloor * f(i)\). 需要一种高效求得函数 \(f(i)\) 的前缀和的方法,比如等差等比数 ...
- P2568 莫比乌斯反演+整除分块
#include<bits/stdc++.h> #define LL long long using namespace std; ; bool vis[maxn]; int prime[ ...
随机推荐
- 记:cloudstack--gluster主存储上的一个文件损坏导致SSVM启动失败
cloudstack的系统vm(ssvm不停的重建失败).- 1.cloudstack-management 的关键日志 这行 cannot read header 'mnt.......':Inva ...
- 关于where和having的直观理解
一,查询区别 where是对前面select的字段没有要求,直接查询库表的 having是对前面的select的字段有要求,字段已经select出来的 可以用having进行处理 select id, ...
- Newtonsoft.Json(Json.Net)学习
转自原文 Newtonsoft.Json(Json.Net)学习笔记 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库.软件下载地址: http://www.newto ...
- leetcode384
public class Solution { private int[] nums; private Random random; public Solution(int[] nums) { thi ...
- django -- url 的 命名空间
命名空间 a. project.urls.py 1 2 3 4 5 6 from django.conf.urls import url,include urlpatterns = [ u ...
- Rust 初始配置
学习 Rust 初始配置 运行环境:Window7 64bit,Rust nightly 1.23; 作者:乌龙哈里 2017-10-15 参考: Rust 中文教程 Rust 官方网站 Rust G ...
- Mysql事务及行级锁
事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...
- VUE+WebPack实现精美前端游戏:CardBattle的游戏场景设置
C:\Users\ZHONGZHENHUA\cardBattle\src\App.vue src/App.vue <template> <div id="app" ...
- PencilWang博客目录
在这里有一坨目录,以后自己和别人看随笔都会方便很多 一 .刷题相关 1.BZOJ BZOJ1001(最大流,最短路)(EASY+) BZOJ1002(数学)(NORMAL+) BZOJ1003( ...
- Opencv读取图片像素值
#include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...