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. 遇到的有关js继承和原型链的一个问题

    function A(var1){ this.var1 = var1; } A.prototype = { var1:1 } var b=new A(); b.var1 = 2; var c = ne ...

  2. 转 Quartz将Job持久化所需表的说明

      QRTZ_CALENDARS 以 Blob 类型存储 Quartz 的 Calendar 信息 QRTZ_CRON_TRIGGERS 存储 Cron Trigger,包括 Cron表达式和时区信息 ...

  3. pycharm git工具与coding.net结合

    前提:coding.net中的项目是私密项目 问题描述:在使用pycharm自带的git工具clone(或者push)代码时出现 错误如下:Push failed: Failed with error ...

  4. Java--神奇的hashcode

    一.Object的HashCode定义 public native int hashCode(); Object类的hashCode方式使用了native修饰也就意味着真正的实现调用的其他语言编写的方 ...

  5. LeetCode题解 #12 Integer to Roman

    题目大意:给定数字,将其转化为罗马数字的形式 罗马数字其实只有 I V X L C D M 这几种形式,其余均为组合的,去百度了解一下就ok. 所以首先想到的就是,将个.十.百.千位的数字构造出来,然 ...

  6. teamviewer14商用试用期到期从新安装使用

    teamviewer14商用试用期到期从新安装使用 1)1.退出TeamViewer远程软件,卸载软件.2)2.按键盘的[win]+[R]组合键打开[运行],输入 %appdata%3)3.在弹出的窗 ...

  7. node / npm 配置问题

    安装nodejs 后运行 npm 命令无响应处理方法 安装和卸载过nodejs, 也编辑过 C:\Users\{账户}\下的.npmrc文件. 再全新安装nodejs ,运行npm 命令,无响应. 处 ...

  8. UNITY所谓的异步加载几乎全部是协程,不是线程;MAP3加载时解压非常慢

    实践证明,以下东西都是协程,并非线程(thread): 1,WWW 2,AssetBundle.LoadFromFileAsync 3,LoadSceneAsync 其它未经测试 此问题的提出是由于一 ...

  9. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 13—Clustering 聚类

    Lecture 13 聚类 Clustering 13.1 无监督学习简介  Unsupervised Learning Introduction 现在开始学习第一个无监督学习算法:聚类.我们的数据没 ...

  10. C# 对象封装为json格式

    1 对象 public class Person { public string Name { get; set; } public int Age { get; set; } public Date ...