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

  1. 5
    1
    2
    3
    10
    100

Output

  1. 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)。

  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. int zs[10000005],t=0,T,sq[50000005];
  5. int miu[50000005],low[50000005],maxn;
  6. bool v[50000005];
  7. ll d[50000005],n;
  8.  
  9. inline void init(){
  10. miu[1]=1,d[1]=1,low[1]=1;
  11. for(int i=2;i<=maxn;i++){
  12. if(!v[i]) zs[++t]=i,miu[i]=-1,d[i]=2,low[i]=i;
  13. for(int j=1,u;j<=t&&(u=zs[j]*i)<=maxn;j++){
  14. v[u]=1;
  15. if(!(i%zs[j])){
  16. low[u]=low[i]*zs[j];
  17. if(low[i]==i) d[u]=d[i]+1;
  18. else d[u]=d[low[u]]*d[i/low[i]];
  19. break;
  20. }
  21.  
  22. low[u]=zs[j];
  23. d[u]=d[i]<<1;
  24. miu[u]=-miu[i];
  25. }
  26. }
  27.  
  28. for(int i=1;i<=maxn;i++) d[i]+=d[i-1];
  29. for(int i=1;i<=maxn;i++) sq[i]=sq[i-1]+miu[i]*miu[i];
  30. }
  31.  
  32. inline ll getsq(ll x){
  33. if(x<=maxn) return sq[x];
  34.  
  35. ll an=0;
  36. for(int i=1;i*(ll)i<=x;i++){
  37. an+=miu[i]*(x/(i*(ll)i));
  38. }
  39. return an;
  40. }
  41.  
  42. inline ll getd(ll x){
  43. if(x<=maxn) return d[x];
  44.  
  45. ll an=0;
  46. for(ll i=1,j,now;i<=x;i=j+1){
  47. now=x/i,j=x/now;
  48. an+=(j-i+1)*now;
  49. }
  50. return an;
  51. }
  52.  
  53. inline ll query(ll x){
  54. ll an=0;
  55. for(ll i=1,j,now;i<=x;i=j+1){
  56. now=x/i,j=x/now;
  57. an+=(getsq(j)-getsq(i-1))*getd(now);
  58. }
  59. return an;
  60. }
  61.  
  62. int main(){
  63. scanf("%d",&T);
  64. if(T>800) maxn=1000000;
  65. else maxn=50000000;
  66. init();
  67. while(T--){
  68. scanf("%lld",&n);
  69. printf("%lld\n",query(n));
  70. }
  71. return 0;
  72. }

  

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. android shape.xml 文件使用

    设置背景色可以通过在res/drawable里定义一个xml,如下: <?xml version="1.0" encoding="utf-8"?> ...

  2. loj2042 「CQOI2016」不同的最小割

    分治+最小割 看到题解的第一句话是这个就秒懂了,然后乱七八糟的错误.越界.RE-- #include <algorithm> #include <iostream> #incl ...

  3. requireJS入门学习

    前言 最近网上.群里各种随便看,随便学.暑期实习还没找到,昨天开题过了,好好学习吧.最近一直看到前端的ADM,CMD规范,然后网上各种找资料看,看了好几个牛人的博客,写的很好,然后自我感觉了解了点,介 ...

  4. 47.关于gradle的解疑

    Short Answer Gradle is a build system. Long Answer Before Android Studio you were using Eclipse for ...

  5. Java的移位运算符

    1.左移运算符:<< 丢弃左边指定位数,右边补0. 注意: 当int类型进行左移操作时,左移位数大于等于32位操作时,会先求余(%)后再进行左移操作.也就是说左移32位相当于不进行移位操作 ...

  6. ALPHA(五)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  7. 习题:Wormhole(思路题)

    tyvj1763 描述 一维的世界就是一个数轴.这个世界的狭小我们几乎无法想象.在这个数轴上,有N个点.从左到右依次标记为点1到N.第i个点的坐标为Xi.经过漫长时间的物理变化和化学变化,这个一维世界 ...

  8. [HDU2829] Lawrence [四边形不等式优化dp]

    题面: 传送门 思路: 依然是一道很明显的区间dp 我们设$dp\left[i\right]\left[j\right]$表示前$j$个节点分成了$i$块的最小花费,$w\left[i\right]\ ...

  9. Prime Gift(prime)

    Prime Gift(prime) 题目描述 Jyt有nn个质数,分别为p1,p2,p3-,pnp1,p2,p3-,pn. 她认为一个数xx是优秀的,当且仅当xx的所有质因子都在这nn个质数中. 她想 ...

  10. Partition Refinement

    今天613问我怎么做DFA最小化..呃..这个我怎么可能会做呢.. 于是我就去学习了一点姿势,先把我Partition Refinement Data Structure的代码发上来好了.. 我挺菜的 ...