Professor Ben 题解(欧拉筛/唯一分解定理)

文章描述了一位教授给学生出的数学题目,要求找出给定整数N的所有因子的因子个数的立方和。通过唯一分解定理和欧拉筛法,可以计算出每个数的因子个数,然后应用特定公式求解。提供的AC代码实现了这一算法,适用于处理不超过5000000的整数情况。

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 Na1,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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古谷彻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值