约数 (数论)

试除法求约数

题目描述 :

给定 n 个正整数 ai,对于每个整数 ai,请你按照从小到大的顺序输出它的所有约数。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一个整数 ai。
输出格式
输出共 n 行,其中第 i 行输出第 ii 个整数 ai 的所有约数。
数据范围
1≤n≤100,
2≤ai≤2×109

输入样例:

2
6
8

输出描述 :

1 2 3 6 
1 2 4 8 

AC代码

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<int> get_divisors(int x){
    vector<int> res;
    for (int i = 1; i <= x / i; i ++ )
        if (x % i == 0)
        {
            res.push_back(i);
            if (i != x / i) res.push_back(x / i);
        }
    sort(res.begin(), res.end());
    return res;
}

int main(){
    int n;
    cin >> n;

    while (n -- ){
        int x;
        cin >> x;
        auto res = get_divisors(x);

        for (auto x : res) cout << x << ' ';
        cout << endl;
    }

    return 0;
}

约数个数

如果 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)
在这里插入图片描述
题目描述 :

给定 n 个正整数 ai,请你输出这些数的乘积的约数个数,答案对 1e9+7 取模。

输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个整数 ai。

输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案需对 1e9+7 取模。

数据范围
1≤n≤100,
1≤ai≤2×1e9
输入样例:

3
2
6
8

输出样例:

12

AC代码

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
const int N = 110, mod = 1e9 + 7;

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

    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] ++ ;
    }
    LL res = 1;
    for (auto p : primes) res = res * (p.second + 1) % mod;
    cout << res << endl;
    return 0;
}

约数和

给定 n 个正整数 ai,请你输出这些数的乘积的约数之和,答案对 1e9+7 取模。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一个整数 ai。
输出格式
输出一个整数,表示所给正整数的乘积的约数之和,答案需对 1e9+7 取模。
数据范围
1≤n≤100
1≤ai≤2×1e9
输入样例:

3
2
6
8

输出样例:

252

N = p1 ^ a1 + p2 ^ a2 + p3 ^ a3 + …+pn ^an
p1 ^ a1 = p1 ^ 0 + p1 ^ 1 + p1 ^2 + …p1 ^ a1
每一个pi ^ ai都是一个约数,那么约数之和就是
约数之和: (p1^0 + p1^1 + … + p1^c1) * … * (pk^0 + pk^1 + … + pk^ck)

核心代码

while (b -- ) t = (t * a + 1) % mod;

t=t∗p+1t=t∗p+1
t=1t=1
t=p+1t=p+1
t=p2+p+1t=p2+p+1
……
t=pb+pb−1+…+1

目的 : 求解每一个(p1^0 + p1^1 + ... + p1^c1) ;

AC代码

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
const int N = 110, mod = 1e9 + 7;

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

    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] ++ ;
    }
    LL res = 1;
    for (auto p : primes) res = res * (p.second + 1) % mod;
    cout << res << endl;
    return 0;
}

GCD/LCM

//最大公约数
int gcd(int a,int b){
	return (b==0?a:gcd(b,a%b));
}
//最小公倍数
int lcm(int a,int b){
	return a*b/gcd(a,b);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值