因数个数、因数和、因数积

对于整数是质因数,是指数。


因数个数

因为每个质因数可以取 0 , 1 , 2 , ... , 次幂,所以


因数和

对每个质因数,其幂次的几何级数和为:

总因数和为:


因数积

另一种计算方法:

对于每个质因数,在所有因数中出现的总次数:

总次数=

所以,


例题

题目描述

Given an integer, your task is to find the number, sum and product of its divisors. As an example, let us consider the number 12:

the number of divisors is 6 (they are 1, 2, 3, 4, 6, 12)
the sum of divisors is 1+2+3+4+6+12=28
the product of divisors is 1* 2* 3* 4* 6* 12 = 1728

Since the input number may be large, it is given as a prime factorization.

输入

The first line has an integer n: the number of parts in the prime factorization.

After this, there are n lines that describe the factorization. Each line has two numbers x and k where x is a prime and k is its power.

Constraints

1 ≤ n ≤

2 ≤ x ≤

each x is a distinct prime

1 ≤ k ≤

输出

Print three integers modulo : the number, sum and product of the divisors.

样例输入
cpp 复制代码
2
2 2
3 1
样例输出

6 28 1728

代码
cpp 复制代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll fast_power(ll a,ll b,ll p){
    if(a==0){
        if(b==0)return 1;
        else return 0;
    }
    ll ans=1;
    while(b){
        if(b&1){
            ans=ans*a%p;
        }
        b>>=1;
        a=a*a%p;
    }
    return ans;
}

const ll mod=1e9+7;

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll n;
    cin>>n;
    vector<ll>x(n),k(n);
    ll number=1,sum=1,product=1,number_phi=1;
    for(ll i=0;i<n;i++){
        cin>>x[i]>>k[i];
        number=number*(k[i]+1)%mod;
        number_phi=number_phi*(k[i]+1)%((mod-1)*2);
        ll inv=fast_power(x[i]-1,mod-2,mod);
        ll tmp=(fast_power(x[i],k[i]+1,mod)-1+mod)%mod;
        tmp=tmp*inv%mod;//分步计算,防止中间值溢出
        sum=sum*tmp%mod;
    }
    for(int i=0;i<n;i++){
        ll k_phi=k[i]%(2*(mod-1));
        ll exp=k_phi*number_phi%(2*(mod-1));
        exp/=2;
        exp%=(mod-1);
        product=product*fast_power(x[i],exp,mod)%mod;
    }
    cout<<number<<" "<<sum<<" "<<product;
    return 0;
}

如果整数没有以质因数分解的形式给出,而仅仅是一个整数n,要求计算n的因数个数。

可以参考下面这道题的做法。

Euler~高度可约的三角形数

题目描述

三角形数数列是通过逐个加上自然数来生成的。例如,第7个三角形数是 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28。三角形数数列的前十项分别是:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

让我们列举出前七个三角形数的所有约数:

1: 1

3: 1,3

6: 1,2,3,6

10: 1,2,5,10

15: 1,3,5,15

21: 1,3,7,21

28: 1,2,4,7,14,28

我们可以看出,28是第一个拥有超过5个约数的三角形数。

第一个拥有超过n个约数的三角形数是多少?

输入

一行,一个整数n(n≤1000)

输出

第一个拥有超过n个约数的三角形数

样例输入

5

样例输出

28

代码
cpp 复制代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll>prime;
void greate_prime(ll limit){
    vector<int>is_prime(limit+1,true);
    is_prime[0]=is_prime[1]=false;
    for(ll i=2;i<=limit;i++){
        if(!is_prime[i])continue;
        prime.push_back(i);
        for(ll j=i*i;j<=limit;j+=i){
            is_prime[j]=false;
        }
    }
}
ll count_divisors(ll n){
    if(n==1)return 1;
    ll ans=1;
    for(ll p:prime){
        if(p*p>n)break;
        int cnt=0;
        while(n%p==0){
            cnt++;
            n/=p;
        }
        if(cnt>0){
            ans*=(cnt+1);
        }
    }
    if(n>1){
        ans*=2;
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll n;
    cin>>n;
    greate_prime(2e5);
    ll k=1;
    while(1){
        ll tri,divs;
        if(k&1){
            ll a=(k+1)/2;
            ll b=k;
            tri=a*b;
            divs=count_divisors(a)*count_divisors(b);
        }
        else{
            ll a=k+1;
            ll b=k/2;
            tri=a*b;
            divs=count_divisors(a)*count_divisors(b);
        }
        if(divs>n){
            cout<<tri;
            break;
        }
        k++;
    }
    return 0;
}
相关推荐
IronMurphy5 小时前
【算法四十三】279. 完全平方数
算法
墨染天姬5 小时前
【AI】Hermes的GEPA算法
人工智能·算法
papership6 小时前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
smj2302_796826526 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
Beginner x_u6 小时前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表
c++之路8 小时前
C++信号处理
开发语言·c++·信号处理
_深海凉_10 小时前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
故事还在继续吗10 小时前
C++20关键特性
开发语言·c++·c++20
青少儿编程课堂10 小时前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
旖-旎10 小时前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历