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);
}
}

这题还是非常高妙的啊~

最新文章

  1. android获取状态栏高度
  2. 3D touch 静态、动态设置及进入APP的跳转方式
  3. NOI2011阿狸的打字机(fail树+DFS序)
  4. LA 3213 Ancient Cipher
  5. Sde表结构分析
  6. ASP.NET MVC 修改视图的默认路径(MVC2,MVC3)
  7. WPF-控件-ListView
  8. jQuery树叶掉落特效代码
  9. LeeCode-Same Tree
  10. Makefile里面的$(MAKE)到底是啥
  11. 自定义Annotation
  12. python cookbook第三版学习笔记 一
  13. JS表单对象和图片对象--JavaScript基础
  14. ERP口碑后付关于如何设置后厨小票打印时间的问题解决方法
  15. 第三方API使用的好习惯
  16. socket练习--ssh
  17. 我的互联网30年。永远的8U8 永远的Y365
  18. openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
  19. Ubuntu下配置用msmtp发送gmail邮件
  20. L1-012 计算指数

热门文章

  1. 移动app非功能测试点收集
  2. shell 入门基础
  3. Shachar Fleishma的论文,做点云重建的几篇论文都不错
  4. 手把手教你使用node-inspector调试nodejs
  5. myBaits缓存
  6. effective javascript 学习心得
  7. inline-block元素出现位置错位的解决方法
  8. siebel简介
  9. NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)
  10. 78. Subsets 求所有子集(有重复就continue)