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(均值不等式,整除分块玄学优化)的更多相关文章

  1. C - A New Function (整除分块 + 玄学优化)

    题目链接:https://cn.vjudge.net/contest/270608#problem/C 题目大意:给你一个n,让你求从1->n中间每个数的因子之和(每个数在求因子的过程中不包括本 ...

  2. LOJ #2802. 「CCC 2018」平衡树(整除分块 + dp)

    题面 LOJ #2802. 「CCC 2018」平衡树 题面有点难看...请认真阅读理解题意. 转化后就是,给你一个数 \(N\) ,每次选择一个 \(k \in [2, N]\) 将 \(N\) 变 ...

  3. 一种基于均值不等式的Listwise损失函数

    一种基于均值不等式的Listwise损失函数 1 前言 1.1 Learning to Rank 简介 Learning to Rank (LTR) , 也被叫做排序学习, 是搜索中的重要技术, 其目 ...

  4. 洛谷 P6788 - 「EZEC-3」四月樱花(整除分块)

    题面传送门 题意: 求 \[\prod\limits_{x=1}^n\prod\limits_{y|x}\frac{y^{d(y)}}{\prod\limits_{z|y}z+1} \pmod{p} ...

  5. 洛谷 P2257 - YY的GCD(莫比乌斯反演+整除分块)

    题面传送门 题意: 求满足 \(1 \leq x \leq n\),\(1 \leq y \leq m\),\(\gcd(x,y)\) 为质数的数对 \((x,y)\) 的个数. \(T\) 组询问. ...

  6. 洛谷 P5518 - [MtOI2019]幽灵乐团 / 莫比乌斯反演基础练习题(莫比乌斯反演+整除分块)

    洛谷题面传送门 一道究极恶心的毒瘤六合一题,式子推了我满满两面 A4 纸-- 首先我们可以将式子拆成: \[ans=\prod\limits_{i=1}^A\prod\limits_{j=1}^B\p ...

  7. 51Nod 1225 余数之和 [整除分块]

    1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...

  8. [Bzoj 2956] 模积和 (整除分块)

    整除分块 一般形式:\(\sum_{i = 1}^n \lfloor \frac{n}{i} \rfloor * f(i)\). 需要一种高效求得函数 \(f(i)\) 的前缀和的方法,比如等差等比数 ...

  9. P2568 莫比乌斯反演+整除分块

    #include<bits/stdc++.h> #define LL long long using namespace std; ; bool vis[maxn]; int prime[ ...

随机推荐

  1. C 语言 - 分支、跳转和循环语句

    if 条件判断语句 if 语句结构 格式: if (表达式) { 语句; } 如果表达式成立,就执行大括号中的语句:否则跳过该 if 语句 #include <stdio.h> int m ...

  2. FastJSON 转换List<T> ,Map<T,T>泛型失败 处理方法

    dictDataMap = JSON.parseObject(dictAllCacheResult,new TypeReference<Map<String, DictionaryData ...

  3. 关于Floyd-Warshall算法由前趋矩阵计算出的最短路径反映出了算法的执行过程特性的证明

    引言:Floyd-Warshall算法作为经典的动态规划算法,能够在O(n3)复杂度之内计算出所有点对之间的最短路径,且由于其常数较小,对于中等规模数据运行效率依然可观.算法共使用n此迭代,n为顶点个 ...

  4. pandas入门学习

    索引对象 pandas的索引对象负责管理轴标签和其他元数据(比如轴名称等).构建series或DataFrame时,所用到的任何数组或其他序列的标签都会转换成一个index: In [1]: impo ...

  5. 使用Spring MVC创建 REST API--2

    1.提供资源之外的其他内容 @ResponseBody提供了一种很有用的方式,能够将控制器返回的Java对象转换为发送到客户端的资源表述.实际上,将资源表述发送给客户端只是整个过程的一部分.一个好的R ...

  6. 「小程序JAVA实战」小程序多媒体组件(27)

    转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxuduomeitizujian27/ 来说下 ,小程序的多媒体组件.源码 ...

  7. Weak References

    http://docwiki.embarcadero.com/RADStudio/Seattle/en/Automatic_Reference_Counting_in_Delphi_Mobile_Co ...

  8. JNI 里使用STL

    JNI里的c或者c++ 调用stl 的时候,比如引入map头文件: #include <map> 在cygwin使用NDK编译的时候,会提示: fatal error:map: No su ...

  9. IE11 - Object doesn't support property or method 'includes'

    IE不支持字符串的includes()方法:可以用indexOf()替换: includes()方法返回true和false; var str = "asdklmn": if(st ...

  10. Elasticsearch-PHP 安装

    安装 Elasticsearch-PHP只有三个要求你需要担心: PHP 5.3.9 或更高版本(查看更多信息) Composer ext-curl: Libcurl的PHP扩展 其它的依赖会通过Co ...