前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【板子】计算一个数的因子的个数

【板子】计算一个数的因子的个数

作者头像
灯珑LoGin
发布2022-10-31 11:40:52
5760
发布2022-10-31 11:40:52
举报
文章被收录于专栏:龙进的专栏

今天做题的时候做了一道这个题,其中需要算一个数的因子的个数.

代码语言:javascript
复制
Let’s denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum:

Find the sum modulo 1073741824 (2^30).
Input

The first line contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 100).
Output
Print a single integer — the required sum modulo 1073741824 (230).
Examples
Input
2 2 2
Output
20
Input
5 6 7
Output
1520

Note

For the first example.

    d(1·1·1) = d(1) = 1;
    d(1·1·2) = d(2) = 2;
    d(1·2·1) = d(2) = 2;
    d(1·2·2) = d(4) = 3;
    d(2·1·1) = d(2) = 2;
    d(2·1·2) = d(4) = 3;
    d(2·2·1) = d(4) = 3;
    d(2·2·2) = d(8) = 4.

So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20.
1
2
3
4
5
6
7
8
9
10
11
12

求一个数的因子的个数的方法:先进行质因数分解,然后再求各个因数的(幂+1)相乘

然后由于这道题的数据量比较小,所以直接暴力枚举了,省去了建立质数表的操作。

代码语言:javascript
复制
#include <iostream>
using namespace std;

typedef long long ll;


ll d(int n)//求因子个数--先进行质因数分解,然后再求各个因数的(幂+1)相乘
{
    ll ans = 1;
    for (int i = 2; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            ll counter=0;
            while(n%i==0)
            {
                n/=i;
                counter++;
            }
            ans = ans*(counter+1);
        }
    }
    if(n>1) ans*=2;//质数的因子有两个
    return ans;
}

int main()
{
    ll a, b, c;
    cin >> a >> b >> c;
    ll ans = 0;
    for (int i = 1; i <= a; ++i)
    {
        for (int j = 1; j <= b; ++j)
            for (int k = 1; k <= c; ++k)
            {
                ans += d(i * j * k);
                //cout << ans << endl;
            }
    }
    cout << ans%1073741824 << endl;
    //system("pause");
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年1月14日2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档