首页 > 代码库 > Smallest multiple

Smallest multiple

problem 5:Smallest multiple

题意:求最小的正数,使得其可以被1-20整除

代码如下:

 1 #ifndef PRO5_H_INCLUDED
 2 #define PRO5_H_INCLUDED
 3 
 4 #include "prime.h"
 5 
 6 namespace pro5{
 7     long long solve(){
 8         long long ans=1;
 9         for(int i=2;i<=20;++i)
10             ans=lcm(ans,i);
11         return ans;
12     }
13 }
14 
15 #endif // PRO5_H_INCLUDED

有关函数:

技术分享
1 long long gcd(long a,long b){
2     return b==0?a:gcd(b,a%b);
3 }
4 
5 long long lcm(long a,long b){
6     return a/gcd(a,b)*b;
7 }
View Code

 

Smallest multiple