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[ ...
随机推荐
- python学习笔记(十一):网络编程
一.python操作网络,也就是打开一个网站,或者请求一个http接口,使用urllib模块. urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib ...
- Vcf文件格式
Vcf文件格式是GATK钟爱的表示遗传变异的一种文件格式. 就拿GATK给出的vcf例子说明吧,下面这个文件只表示了一个完整vcf文件的前几个SNP. 看上去确实有点复杂,那就把它分为两部分看吧,第一 ...
- 解决“在上下文中找不到 owin.Environment 项”
网站发布到虚拟空间后,提示以下错误:在上下文中找不到 owin.Environment 项",百度了好长时间都没有解决.最后在web.config中添加以下配置. <system.we ...
- JSTL中EL表达式无法直接取size的处理
jsp中使用${list.size }会编译成list.getSize()方法,并不能获取list的长度,因为程序回去找List对象中的getSize()方法,所以只能想别的办法, 一种方法是在后台程 ...
- CodeFirst(反射+特性)
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- springMVC环境搭建(1)
工作一年以来,写的都是.net,最近比较闲,想把之前学过的java相关的东西捡起来,也学点新的东西.以前做过SSH架构,一直好奇spring mvc是怎么样的,所以今天试试看. 总体的代码结构 手动输 ...
- 添加 MyEclipse Persistence Tools 类库
1).右键点击你的项目,然后选择Properties.2).在 Java Build Path 页面, 在 Libraries 面板下选择 Add Library….3).选择 MyEclipse L ...
- Centos7 配置
参考文章: http://www.hksilicon.com/kb/articles/594621/CentOS-7 1. 查看时区是否正确timedatectl,若不正确则设置时区 timedate ...
- 用django框架开发一个B2C购物网站用户注册知识点总结2
一:用户部分: 用户注册: 用户注册序列化器: import re from django_redis import get_redis_connection from rest_framework ...
- Nginx源码完全注释(2)ngx_array.h / ngx_array.c
数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...