当前位置:网站首页>乐山师范程序设计大赛2020- H: 最小公倍数【求因子个数】
乐山师范程序设计大赛2020- H: 最小公倍数【求因子个数】
2022-07-19 05:15:00 【星空皓月】
题目描述
给定一个整数 b,另外 a 表示 1 到 1018 中的所有整数,计算式子 [a, b] / a 有多少个不同的结果,这里 [a, b] 表示整数 a 与整数 b 的最小公倍数。
输入
输入只包含一组数据;
第一行包含一个整数 b (1 ≤ b ≤ 1010)。
输出
输出式子不同结果的数量。
样例输入
2
样例输出
2
提示
任意正整数 a 与 2 的最小公倍数必定等于 2 * a 或者 a,故式子 [a, b] / a 的结果只能是 1 或者 2。
思路
多枚举几个b,我们会发现就是去求b的因子个数。
例如b = 4;
(4, a) == >最小公倍数可能为(a,2a,4a),再除以a就为(1,2,4),观察发现这些都是4的因子。
所以直接求因子的个数有多少个,但是b的范围很大,再次观察[1,sqrt(b)]中存在这个n的一半因子,(sqrt(b),n)存在另一半,所以我们只需要枚举到sqrt(b),然后找因子的个数,当枚举到一个因子时,另一个因子就找出来了,如果i * i 恰好为n,则当前只有一个.
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll isPrime(ll n) {
ll cnt = 0;
for (ll i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
if (i * i != n) {
cnt += 2;
} else {
cnt++;
}
}
}
return cnt;
}
void solve() {
ll b;
scanf("%lld", &b);
printf("%lld\n", isPrime(b));
}
int main() {
solve();
return 0;
}
边栏推荐
- Many new features of ktor2.0 were thought of a year ago and have been implemented in the project
- Ktor 2.0? Half fragrant embarrassment
- Detailed explanation of the principle of triode series linear voltage stabilizing circuit and Multisim Simulation
- How to put a "platform" into a small box? (I) scheme comparison
- Leetcode:14. 最长公共前缀【思维+排序】
- 1人天搞定9人天的日志接入开发——基于指令集物联网操作系统的项目开发实践
- Matlab之数据筛选
- Code audit system
- Add directory navigation to personal blog website articles
- Chrome developer tool shortcut key reference to improve development efficiency
猜你喜欢
随机推荐
[Lora & nb IOT] current situation analysis
STM32 CubeIDE 断点失效的解决方法
Evaluate reading and writing speed and network of Reza rz/g2l storage
Secure Code Warrlor学习记录(二)
UltraEdit line break / tab settings
內網滲透隧道技術的相關知識
Least square linear fitting and its code implementation (C language)
When FPM generates packages, the associated Allegro cannot generate packages after it is opened. Solution to the problem
Stm32f4 uses a timer to output multiple PWM waves with different frequency duty cycles (including codes)
Detailed explanation of multiresolution decomposition and reconstruction of wavelet analysis
1人天搞定9人天的日志接入开发——基于指令集物联网操作系统的项目开发实践
TypeScript 中类型 any,void,unknown,never之间的区别
经纬度及其与坐标系的转换
Solution of STM32 cubeide breakpoint failure
Xilinx 7系列原语使用(时钟相关)——(一)
First hand evaluation of Reza g2l core board and development board
Redux main knowledge learning summary
FPGA majority voter (including code)
How to test the availability and stability of EIM bus
正则表达式的设计