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

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


因数个数

因为每个质因数可以取 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;
}
相关推荐
naruto_lnq13 小时前
分布式计算C++库
开发语言·c++·算法
m0_7066532313 小时前
模板编译期排序算法
开发语言·c++·算法
历程里程碑13 小时前
Linxu14 进程一
linux·c语言·开发语言·数据结构·c++·笔记·算法
木井巳14 小时前
【递归算法】验证二叉搜索树
java·算法·leetcode·深度优先·剪枝
m0_5613596714 小时前
嵌入式C++加密库
开发语言·c++·算法
近津薪荼14 小时前
优选算法——双指针专题7(单调性)
c++·学习·算法
JiL 奥14 小时前
Nexus制品归档(c/c++项目)
c语言·c++
j4455661114 小时前
C++中的职责链模式实战
开发语言·c++·算法
m0_6860416114 小时前
实时数据流处理
开发语言·c++·算法
波波侠814 小时前
代码随想录算法训练营打卡第31天|56. 合并区间、738.单调递增的数字
算法