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

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


因数个数

因为每个质因数可以取 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;
}
相关推荐
十五年专注C++开发18 小时前
VS2019编译的C++程序,在win10正常运行,在win7上Debug正常运行,Release运行报错0xC0000005,进不了main函数
开发语言·c++·报错c0x0000005
云里雾里!18 小时前
LeetCode 744. 寻找比目标字母大的最小字母 | 从低效到最优的二分解法优化
算法·leetcode
fy zs18 小时前
网络编程套接字
linux·服务器·网络·c++
fpcc18 小时前
模板编程—模板和元编程中的错误输出
c++·模板编程
xie_pin_an18 小时前
C++ 类和对象全解析:从基础语法到高级特性
java·jvm·c++
小温冲冲19 小时前
C++与QML信号绑定完全指南:实现跨语言无缝通信
c++
GIS 数据栈19 小时前
【Seggis遥感系统升级】用C++高性能服务Drogon重构软件服务架构|QPS提升300%,性能再升级!
java·开发语言·c++·重构·架构
一条大祥脚19 小时前
26.1.3 快速幂+容斥 树上dp+快速幂 带前缀和的快速幂 正序转倒序 子序列自动机 线段树维护滑窗
数据结构·算法
二狗哈19 小时前
czsc入门5: Tick RawBar(原始k线) NewBar (新K线)
算法·czsc