Description
Professor Ben is an old stubborn man teaching mathematics in a university. He likes to puzzle his students with perplexing (sometimes boring) problems. Today his task is: for a given integer N, a1,a2, ... ,an are the factors of N, let bi be the number of factors of ai, your job is to find the sum of cubes of all bi. Looking at the confused faces of his students, Prof. Ben explains it with a satisfied smile:
Let's assume N = 4. Then it has three factors 1, 2, and 4. Their numbers of factors are 1, 2 and 3 respectively. So the sum is 1 plus 8 plus 27 which equals 36. So 36 is the answer for N = 4.
Given an integer N, your task is to find the answer.
Input
The first line contains the number the test cases, Q(1 ≤ Q ≤ 500000). Each test case contains an integer N(1 ≤ N ≤ 5000000)
Output
For each test case output the answer in a separate line.
Sample Input
1 4
Sample Output
36
关于唯一分解定理,见以下文章:
***用于求数n的因子个数***
AC代码
#include<cstdio>
#include<iostream>
using namespace std;
const int N=5000005;
bool isnp[N];
int num[N];
int cnt;
//欧拉筛,筛前5000000个数中的素数
void prime(){
cnt=0;
isnp[1]=1;
for(int i=2;i<=N;i++){
if(!isnp[i]){
num[++cnt]=i;
}
for(int j=1;j<=cnt&&i*num[j]<=N;j++){
isnp[i*num[j]]=1;
if(i%num[j]==0){
break;
}
}
}
}
int main(){
int q; //注意不能开long long,会超时
cin>>q;
prime();
while(q--){
int n,ans=1;
cin>>n;
if(!isnp[n]){
ans=9;//如果这个数为素数,则只有两个因数
}else{
//唯一分解定理
for(int i=1;i<=cnt&&num[i]*num[i]<=n;i++){
int sum=0;
while(n%num[i]==0){
sum++;
n/=num[i];
}
ans*=((sum+1)*(sum+1)*(sum+2)*(sum+2)/4);
//推导公式,重要!!!
}
if(!isnp[n]){
ans*=9;
}
}
cout<<ans<<endl;
}
return 0;
}
文章描述了一位教授给学生出的数学题目,要求找出给定整数N的所有因子的因子个数的立方和。通过唯一分解定理和欧拉筛法,可以计算出每个数的因子个数,然后应用特定公式求解。提供的AC代码实现了这一算法,适用于处理不超过5000000的整数情况。
708

被折叠的 条评论
为什么被折叠?



