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

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


因数个数

因为每个质因数可以取 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;
}
相关推荐
zyl76511 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
爱读书的小胖15 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌15 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
OPEN-F15 小时前
ROS2系列教程:上位机建图与导航(原理综合)
算法·机器人
weixin_3077791315 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
AI wins15 小时前
c++新增頭文件
c++
Shadow(⊙o⊙)16 小时前
仿muduo实现高并发服务器——事件就绪管理Channel.hpp
服务器·c++
tomwang032216 小时前
路径优化建模、优化算法分类及工程化小结
算法·数学建模·分类
小羊没烦恼!16 小时前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
by2099916 小时前
哈希表——快速找到数据:冲突、删除与扩容的实现
数据结构·c++·哈希算法·散列表