模板_约数个数_约数之和

如果 N = p1^c1 * p2^c2 * … *pk^ck
约数个数: (c1 + 1) * (c2 + 1) * … * (ck + 1)
约数之和: (p1^0 + p1^1 + … + p1^c1) * … * (pk^0 + pk^1 + … + pk^ck)

//约数个数(a+1)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; 
const int mod = 1e9 + 7;
int main(){
    int n,x;
    LL ans = 1;
    map<int,int> hash;
    cin >> n;
    while(n--){
        cin >> x;
        for(int i = 2;i <= x/i; ++i){
            while(x % i == 0){
                x /= i;
                hash[i] ++;
            }
        }
        if(x > 1) hash[x] ++;
    }
    for(map<int,int>::iterator i = hash.begin() ; i != hash.end() ; i++) ans = ans*(i->second + 1) % mod;
    cout << ans;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod=1e9+7;

map<int,int> primes;
int main(){
    int n;
    cin>>n;

    while(n--){
        int x;
        cin>>x;

        for(int i=2;i<=x/i;i++){
            while(x%i==0){
                x/=i;
                primes[i]++;
            }
        } 
        if(x>1) primes[x]++;           //注意是x>1
    }

    ll res=1;
    for(map<int,int>::iterator prime = primes.begin() ; prime != primes.end() ; prime++){           
        int p=prime->first,a=prime->second;
        ll t=1;
        while(a--) t=(t*p+1)%mod;         //求出 p0一直加到p的k的次方 的和
        res=res*t%mod;
    }
    cout<<res<<endl;
}

//3
//2
//6
//8
//res=252
发布了12 篇原创文章 · 获赞 0 · 访问量 118

猜你喜欢

转载自blog.csdn.net/qq_45244489/article/details/105073232