B. LCM(分析题意,最小公倍数和因子相联系)

题目链接https://vjudge.net/contest/373416#problem/F
Ivan has number bb. He is sorting through the numbers aa from 1 to 1018, and for every a writes [a,b]/a on blackboard. Here [a,b] stands for least common multiple of a and b. Ivan is very lazy, that’s why this task bored him soon. But he is interested in how many different numbers he would write on the board if he would finish the task. Help him to find the quantity of different numbers he would write on the board.

Input
The only line contains one integer — b (1≤b≤1010).

Output
Print one number — answer for the problem.
Input

1

Output

1

Input

2

Output

2

翻译
给定一个b,a的范围为[1,1018],[a,b]表示lcm(a,b)。(a从1开始)。求[a,b]/a不同的个数。
分析
b和a都是非常大的数,且时间复杂度为1000ms,必须为一层循环。
从a的范围入手:

  1. 若a>b,则lcm(a,b)/a的值都是固定的(都是b)
  2. 考虑a的范围只用考虑小于b即可。

以根号b为分界点(root)

小于root的范围为b-。
大于root的范围为b+。

枚举1~root(i):
若b%i=0即b%b-=0。则b为b-的倍数,换句话说b-为b的因子。可以得出lcm(b,b-)=b。那么lcm(b,b-)/b-则等于b的另一个因子,这另一个因子一定在b+的范围内
所以只用枚举1~root的数即可。

代码

#include<cstdio>
#include<cstring>
#include<math.h>
#include<iostream>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
    LL b;
    scanf("%lld",&b);
    LL root=sqrt(b);
    LL res=0;
    for(int i=1; i<=root; i++)
    {
        if(b%i==0)
            res+=2;
    }
    if(root*root==b)
        res-=1;
    printf("%lld\n",res);
    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值