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).S​2​​(n)=​i=1​∑​n​​σ​0​​(i​2​​).

Given NN, find S_2(N)S​2​​(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≤10​12​​)

Output

For each number NN, output a single line containing S_2(N)S​2​​(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 = 7S​2​​(3)=σ​0​​(1​2​​)+σ​0​​(2​2​​)+σ​0​​(3​2​​)=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≤10​8​​, TL = 20s.

- Input #3: 1 \le T \le 200,\ 1 \le N \le 10^{9}1≤T≤200, 1≤N≤10​9​​, TL = 20s.

- Input #4: 1 \le T \le 40,\ 1 \le N \le 10^{10}1≤T≤40, 1≤N≤10​10​​, TL = 20s.

- Input #5: 1 \le T \le 10,\ 1 \le N \le 10^{11}1≤T≤10, 1≤N≤10​11​​, TL = 20s.

- Input #6: T = 1,\ 1 \le N \le 10^{12}T=1, 1≤N≤10​12​​, 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)的更多相关文章

  1. [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)

    题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0​(n) be the number of positive diviso ...

  2. 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&= ...

  3. SP20173 DIVCNT2 - Counting Divisors (square)

    Refer 主要思路参考了 Command_block 的题解. Description 给定 \(n\)(\(n\le 10^{10}\)),求 \[\sum_{i=1}^n\sigma_0(i^2 ...

  4. DIVCNT2&&3 - Counting Divisors

    DIVCNT2 - Counting Divisors (square) DIVCNT3 - Counting Divisors (cube) 杜教筛 [学习笔记]杜教筛 (其实不算是杜教筛,类似杜教 ...

  5. SPOJDIVCNT2: Counting Divisors(莫比乌斯反演)

    http://acm.tzc.edu.cn/acmhome/vProblemList.do?method=problemdetail&oj=SPOJ&pid=DIVCNT2 给出n求 ...

  6. HDU 6069 Counting Divisors

    Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  7. hdu 6069 Counting Divisors(求因子的个数)

    Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  8. hdu 6069 Counting Divisors 筛法

    Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  9. 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 ...

随机推荐

  1. sql优化系列3(收集来源http://bbs.csdn.net/topics/250004467)

    如何加快查询速度? 1.升级硬件   2.根据查询条件,建立索引,优化索引.优化访问方式,限制结果集的数据量. 3.扩大服务器的内存 4.增加服务器CPU个数 5.对于大的数据库不要设置数据库自动增长 ...

  2. python上数据存储 .h5格式或者h5py

    最近在做城市计算的项目,数据文件是以.h5的格式存储的,总结下其用法和特点 来自百度百科的简介: HDF(Hierarchical Data Format),可以存储不同类型的图像和数码数据的文件格式 ...

  3. LINQ体验(9)——LINQ to SQL语句之Insert/Update/Delete操作

    我们继续讲解LINQ to SQL语句,这篇我们来讨论Insert/Update/Delete操作.这个在我们的程序中最为常用了.我们直接看例子. Insert/Update/Delete操作 插入( ...

  4. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  5. Oracle 学习笔记(Windows 环境下安装 + PL/SQL)

    Oracle 安装.PL/SQL 配置使用  前言:因更换机械硬盘为 SSD 固态硬盘装了新 Windows 7 系统,需要重新搭建开发环境,把 Oracle 安装过程和 PL/SQL 配置使用做下笔 ...

  6. 微信小程序简单的数据表格及查询功能

    简介: 此项目是一个前后端分离的小demo, 开发工具:idea+微信小程序开发工具 前端:界面布局样式和js的跳转 后端:依靠SpringBoot的业务逻辑层 项目的码云地址: https://gi ...

  7. Leetcode 592.分数加减运算

    分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分 ...

  8. Selenium - WebDriver: Locating Elements

    Selenium provides the following methods to locate elements in a page: find_element_by_id find_elemen ...

  9. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  10. 【转】超简单利用UGUI制作圆形小地图

    http://sanwen.net/a/ithhtbo.html 由于UI都是Achor自己用PS做的,比较粗糙,大家见谅,不过丝毫不影响功能的实现,下面我们看看今天的笔记: 首先我们看看需要哪些组件 ...