In: Computer Science
Lili was working on her mathematic homework, and suddenly, she found another classic problem.
The problem is: “Given three light bulbs X , Y , and Z. Bulb X light up every A seconds, bulbs Y light up
every B seconds, and bulb Z light up every C seconds. If three bulbs light up together for the 1-st
time at 0-th second, the 2-nd time this three bulbs will light up together at the same time will be at
K1-th second. Find K1!”
Lili got bored by this question. Lili challenge herself and she figured out a challenging problem. She
wants to find the sum of all Ki which satisfy L ≤ Ki ≤ R, where when at Ki-th seconds, the three bulbs
light up together for the i + 1-th time. Solve Lili’s challenge!
Format Input :
There are T testcases. Every testcase consists of five integers A , B , C , L, and R as described above.
Format Output:
Output T testcases with format “Case # X: ”, where X indicates the testcase number and then
followed by an integer indicates the answer to Lili’s challenge.
Constraints:
• 1 ≤ T ≤ 10
• 1 ≤ A, B, C ≤ 100
• 1 ≤ L ≤ R ≤ 109
Sample Input (standard input):
5
1 2 3 1 20
2 3 5 30 30
2 3 5 30 90
11 11 11 10 40
2 3 4 10 30
Sample Output (standard output):
Case #1: 36
Case #2: 30
Case #3: 180
Case #4: 66
Case #5: 36
NOTE: USE C LANGUAGE, DONT USE FUNCTION(RESULT,RETURN),VOID,RECURSIVE,
USE BASIC CODE AND CODE IT UNDER int main (){, constraint must be the same.
: : C code : :
#include <stdio.h>
int main()
{
int lcm,sum,st,size,i,m,l,r,t,p;
p=0;
scanf("%d",&t);
while(t>0)
{
p++;
t--;sum=0;
int arr[3];
scanf("%d %d %d %d %d",&arr[0],&arr[1],&arr[2],&l,&r);
size = 3;//sizeof(arr) / sizeof(int);
lcm = arr[0];
//calculating lcm of 3 numbers
for (i = 1; i < size; i++){
m=1;
while( m%lcm || m%arr[i]) m++;
lcm = m;
}
//finding starting index to start sum from for lcm multiple
if(l>= lcm) st=l/lcm;
else st=1;
for(i=st; lcm*i <= r ; i++ ){
sum+=lcm*i;
}
printf("Case #%d: %d\n",p,sum);
}return 0;
}
Algorithm
- We basically have to find the lcm of A,B,C ; first ,
-Then sum up multiples of lcm that lie in the range [L,R]
: : Sample output : :
Please refer to the screenshot of the code to understand the indentation of he code: