过程很美妙啊
Problem Description
Believe it or not, in the next exam she faces a hard problem described as follows.
Let’s denote f(x) number of ordered pairs satisfying (a * b)|x (that is, x mod (a * b) = 0) where a and b are positive integers. Given a positive integer n, Rikka is required to solve for f(1) + f(2) + . . . + f(n).
According to story development we know that Rikka scores slightly higher than average, meaning she must have solved this problem. So, how does she manage to do so?
Input
For each test case, there is a single line containing only one integer n (1 ≤ n ≤ 1011).
Input is terminated by EOF.
Output
题目大意
求有序三元组$(a,b,c)$满足$a*b*c=n$的个数
题目分析
考虑以下三种做法:
大力卷积吧!
发现$\sum_{abc=n} \textbf{1}$这是一个卷积的形式,那么卷两次即可。
时间复杂度:$O(n\ln n)$
线性筛
注意到$n$的质因数之间互不影响。那么考虑将$n$分解为$n=p_1^{a_1}\times p_2^{a_2}\times \cdots \times p_k^{a_k}$的形式,于是答案就是${\rm f(n)}={(a_1+1)\times (a_1+2)\over{2}}\times {(a_2+1)\times (a_2+2)\over{2}}\times \cdots \times {(a_k+1)\times (a_k+2)\over{2}}$.
这样子做一遍线性筛就好了。
时间复杂度:$O(n)$
转化一下
注意到这个顺序实际上不是必要的,也就是说完全可以算出无序的答案之后反过来考虑有序,即$abc≤n$的答案数.
那么只需要枚举$a,b$,就可以得到$c$的范围即$[b,{\left \lfloor \frac{n}{ab} \right \rfloor}]$。
此时若$a=b$,如果$c=b$会产生1种方案;$c≠b$有${\left \lfloor \frac{n}{ab} \right \rfloor}-b$种情况、而每一种情况会产生3种方案。这里所谓产生的方案即有序所带来的额外贡献。那么$a≠b$时同理。
时间复杂度:$O(n^{\frac{2}{3}})$
1 #include<bits/stdc++.h> 2 typedef long long ll; 3 4 ll n,ans; 5 int scenario; 6 7 int main() 8 { 9 while (scanf("%lld",&n)!=EOF) 10 { 11 ans = 0; 12 for (ll i=1; i*i*i<=n; i++) 13 for (ll j=i; i*j*j<=n; j++) 14 { 15 ll k = n/(i*j); 16 if (j > k) break; 17 if (i==j) ans += (k-j)*3ll+1; 18 else ans += (k-j)*6ll+3; 19 } 20 printf("Case %d: %lld\n",++scenario,ans); 21 } 22 return 0; 23 }
END