SPOJ 20713 DIVCNT2 - Counting Divisors (square)
DIVCNT2 - Counting Divisors (square)
Let \sigma_0(n)σ0(n) be the number of positive divisors of nn.
For example, \sigma_0(1) = 1σ0(1)=1, \sigma_0(2) = 2σ0(2)=2 and \sigma_0(6) = 4σ0(6)=4.
LetS_2(n) = \sum _{i=1}^n \sigma_0(i^2).S2(n)=i=1∑nσ0(i2).
Given NN, find S_2(N)S2(N).
Input
First line contains TT (1 \le T \le 100001≤T≤10000), the number of test cases.
Each of the next TT lines contains a single integer NN. (1 \le N \le 10^{12}1≤N≤1012)
Output
For each number NN, output a single line containing S_2(N)S2(N).
Example
Input
5
1
2
3
10
100
Output
1
4
7
48
1194
Explanation for Input
- S_2(3) = \sigma_0(1^2) + \sigma_0(2^2) + \sigma_0(3^2) = 1 + 3 + 3 = 7S2(3)=σ0(12)+σ0(22)+σ0(32)=1+3+3=7
Information
There are 6 Input files.
- Input #1: 1 \le N \le 100001≤N≤10000, TL = 1s.
- Input #2: 1 \le T \le 800,\ 1 \le N \le 10^{8}1≤T≤800, 1≤N≤108, TL = 20s.
- Input #3: 1 \le T \le 200,\ 1 \le N \le 10^{9}1≤T≤200, 1≤N≤109, TL = 20s.
- Input #4: 1 \le T \le 40,\ 1 \le N \le 10^{10}1≤T≤40, 1≤N≤1010, TL = 20s.
- Input #5: 1 \le T \le 10,\ 1 \le N \le 10^{11}1≤T≤10, 1≤N≤1011, TL = 20s.
- Input #6: T = 1,\ 1 \le N \le 10^{12}T=1, 1≤N≤1012, TL = 20s.
My C++ solution runs in 5.3 sec. (total time)
Source Limit is 6 KB.
很迷的函数题。
如何求 d(i^2)?
d(i^2)= (2*a1+1)(2*a2+1)(2*a3+1)...(2*ak+1)
我们考虑一下选哪些质因子的集合,上式
=Σ2^|S| *π a[i] ,i属于S
=Σ(p|i) 2^w(p)。
其中w(x)为x的质因子数。
然后发现2^w(x)=Σ(i|x) μ^2(i)
所以ANS= Σμ^2(i) *Σd(j) ,其中1<=i<=n,1<=j<=(n/i)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int zs[10000005],t=0,T,sq[50000005];
int miu[50000005],low[50000005],maxn;
bool v[50000005];
ll d[50000005],n; inline void init(){
miu[1]=1,d[1]=1,low[1]=1;
for(int i=2;i<=maxn;i++){
if(!v[i]) zs[++t]=i,miu[i]=-1,d[i]=2,low[i]=i;
for(int j=1,u;j<=t&&(u=zs[j]*i)<=maxn;j++){
v[u]=1;
if(!(i%zs[j])){
low[u]=low[i]*zs[j];
if(low[i]==i) d[u]=d[i]+1;
else d[u]=d[low[u]]*d[i/low[i]];
break;
} low[u]=zs[j];
d[u]=d[i]<<1;
miu[u]=-miu[i];
}
} for(int i=1;i<=maxn;i++) d[i]+=d[i-1];
for(int i=1;i<=maxn;i++) sq[i]=sq[i-1]+miu[i]*miu[i];
} inline ll getsq(ll x){
if(x<=maxn) return sq[x]; ll an=0;
for(int i=1;i*(ll)i<=x;i++){
an+=miu[i]*(x/(i*(ll)i));
}
return an;
} inline ll getd(ll x){
if(x<=maxn) return d[x]; ll an=0;
for(ll i=1,j,now;i<=x;i=j+1){
now=x/i,j=x/now;
an+=(j-i+1)*now;
}
return an;
} inline ll query(ll x){
ll an=0;
for(ll i=1,j,now;i<=x;i=j+1){
now=x/i,j=x/now;
an+=(getsq(j)-getsq(i-1))*getd(now);
}
return an;
} int main(){
scanf("%d",&T);
if(T>800) maxn=1000000;
else maxn=50000000;
init();
while(T--){
scanf("%lld",&n);
printf("%lld\n",query(n));
}
return 0;
}
SPOJ 20713 DIVCNT2 - Counting Divisors (square)的更多相关文章
- [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)
题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0(n) be the number of positive diviso ...
- SPOJ : DIVCNT2 - Counting Divisors (square)
设 \[f(n)=\sum_{d|n}\mu^2(d)\] 则 \[\begin{eqnarray*}\sigma_0(n^2)&=&\sum_{d|n}f(d)\\ans&= ...
- SP20173 DIVCNT2 - Counting Divisors (square)
Refer 主要思路参考了 Command_block 的题解. Description 给定 \(n\)(\(n\le 10^{10}\)),求 \[\sum_{i=1}^n\sigma_0(i^2 ...
- DIVCNT2&&3 - Counting Divisors
DIVCNT2 - Counting Divisors (square) DIVCNT3 - Counting Divisors (cube) 杜教筛 [学习笔记]杜教筛 (其实不算是杜教筛,类似杜教 ...
- SPOJDIVCNT2: Counting Divisors(莫比乌斯反演)
http://acm.tzc.edu.cn/acmhome/vProblemList.do?method=problemdetail&oj=SPOJ&pid=DIVCNT2 给出n求 ...
- HDU 6069 Counting Divisors
Counting Divisors Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- hdu 6069 Counting Divisors(求因子的个数)
Counting Divisors Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- hdu 6069 Counting Divisors 筛法
Counting Divisors Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- 2017 Multi-University Training Contest - Team 4 hdu6069 Counting Divisors
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6069 题目: Counting Divisors Time Limit: 10000/5000 ...
随机推荐
- BZOJ 4247: 挂饰
背包裸题 #include<cstdio> #include<algorithm> using namespace std; int F[2005]; struct node{ ...
- 记一次开发过程中,iview遇到的一些坑以及解决办法
写在开头:本次项目采用的是vue2.0+iview3.0,最近公司没啥事,来总结一下开发过程中遇到的问题. 1.Modal关闭问题 需求背景:modal框里面是个form表单,点击确定之后,先验证fo ...
- java面向对象之关键字,权限修饰符
1.关键字:this,static,package,importthis:1.表示对当前对象的引用!2.表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特 ...
- Python3.0-3.6的版本变化
Table of Contents Python3.0 简单的变化 语法的变化 新语法 改动的语法 剩下的变化 Python3.1 Python3.2 Python3.3 Python3.4 Pyth ...
- 最少的硬币数量组合出1到m之间的任意面值(贪心算法)
题目描述: 你有n种不同面值的硬币,每种面值的硬币都有无限多个,为了方便购物,你希望带尽量少的硬币,并且要能组合出 1 到 m 之间(包含1和m)的所有面值. 输入描述: 第一行包含两个整数:m ,n ...
- LeetCode with Python -> Linked List
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- jira从windows迁移到linux
说明:迁移的就是 jira安装路径/atlassian/jira/atlassian-jira/WEB-INF/classes/jira-application.properties文件中的jira_ ...
- xctf --Hctf2014 Quals write up
描述 猫流大大发现一个女神,你能告诉我女神的名字么(名字即是flag) nvshen.zip Solution: Extract the file and we could find a txt wh ...
- 数组洗牌算法-shuffle
数组洗牌,最近直接的想法是从数组随机取出一个元素,放到另一个数组中,但是这样取出的元素会有重复,必须采取一定的方法保证: 1. 元素不能重复2. 元素被抽取的概率相等,即随机性 数组洗牌经典算法有两种 ...
- 城市改造 (city)
城市改造 (city) 题目描述 为了节约当前社会巨大的能源消耗,A 城市发起了新能源改造计划,也就是用太阳能代替旧能源.为了简化题意,可以假设 A 城市在一个二维平面上,平面上从11到nn每一个地方 ...