51Nod-1586-约数和

本文介绍了一道ACM竞赛题目,通过预处理每个数的约数个数来优化算法效率,采用C++实现并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ACM模版

描述

描述
描述

题解

为什么我感觉这个题加不加输入输出外挂没啥两样呢?都是 1300ms 左右 AC……

先预处理一下每个数的约数个数,然后直接暴力就好了。

代码

#include <cstdio>

using namespace std;

typedef long long ll;

const int MAXN = 1e6 + 10;

int n, q;
int cnt[MAXN];
ll a[MAXN];

template <class T>
inline void scan_d(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

template <class T>
inline void print_d(T x)
{
    if (x > 9)
    {
        print_d(x / 10);
    }
    putchar(x % 10 + '0');
}

int main()
{
    scan_d(n), scan_d(q);
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j <= n; j += i)
        {
            cnt[j]++;
        }
    }

    int odr, x, y;
    while (q--)
    {
        scan_d(odr);
        if (odr == 1)
        {
            scan_d(x), scan_d(y);
            for (int i = x, j = 1; i <= n; i += x, j++)
            {
                a[i] += y * cnt[j];
            }
        }
        else
        {
            scan_d(x);
            print_d(a[x]);
            putchar(10);
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值