求n个数的最小公倍数 比如 1 2 3 12
先求1 2 的最小公倍数m
然后求 m 3的最小公倍数 m
再求m 12.。。。 循环
先求1 2 的最小公倍数m
然后求 m 3的最小公倍数 m
再求m 12.。。。 循环
ll mid = lcm(arr[i], lcm(arr[j], lcm(arr[l],arr[k]))); 求4个数的最小公倍数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std ;
#define ll long long
const int M = 51;
int n,t;
ll arr[M];
ll gcd(ll a, ll b)
{
if(b == 0) return a;
else return gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a/gcd(a,b)*b;
}
int main()
{
while(scanf("%d%d",&n,&t))
{
if(n < 4) break;
if(!n || !t) break;
for(int i = 0; i < n; ++i)
scanf("%d",&arr[i]);
ll h;
while(t--)
{
scanf("%d",&h);
int flag = 0;
ll mmin = 1000000000;
ll a , b = 1000000000;
for(int i = 0; i < n-3; i++)
{
for(int j = i+1; j < n-2; j++)
{
for(int l = j+1; l < n-1; l++)
{
for(int k = l+1; k < n; k++)
{
ll mid = lcm(arr[i], lcm(arr[j], lcm(arr[l],arr[k])));
ll temp = h % mid;
a = mid - temp;
if(temp == 0)
{
b = 0;
mmin = 0;
flag =1 ;break;
}
else
{
if(temp < mmin) mmin = temp;
if(a < b) b = a;
}
}
if(flag) break;
}
if(flag) break;
}
if(flag) break;
}
printf("%d %d\n",h-mmin,h+b);
}
}