1. 程式人生 > >Codeforces Round #518 (Div. 2) B LCM

Codeforces Round #518 (Div. 2) B LCM

傳送門

https://www.cnblogs.com/violet-acmer/p/10163375.html

 

題解:

  這道題有點意思,有點數學的味道。

  根據定義“[a,b] / a”可得這求得是lcm(a,b) / a。

  轉換一下:

  易知 gcd(a,b)= (a*b) / lcm(a,b) <=> lcm(a,b) = (a*b) / gcd(a,b)

  那麼 lcm(a,b) / a <=> b / gcd(a,b)

  而gcd(a,b)不就是b的約數嗎?

  因為 a 取的最大值為 1018  ;

  而 b 的最大值才為 1010 ;

  所以這道題直接轉化為求 b 的約數個數了.

AC程式碼:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 #define ll __int64
 8
9 ll b; 10 int Prime() 11 { 12 int res=2; 13 int x=sqrt(b); 14 for(int i=2;i <= x;++i) 15 { 16 if(b%i != 0) 17 continue; 18 res++; 19 if(b/i != i) 20 res++; 21 } 22 return (b == 1 ? 1:res); 23 } 24 int main() 25 { 26 scanf("
%I64d",&b); 27 printf("%d\n",Prime()); 28 return 0; 29 }
View Code