Luogu T300217 essay AMCPlus chapter 1 problem solution

The title requirement is the least common multiple of ab and the greatest common factor of cd

Looking at the topic, here are 2 key points and 3 ideas

First, the least common multiple

Second, the greatest common factor

Third, calculate the result

1. Least common multiple

We can solve him with Python:

def LLO(a,b):
    i=a
    while 1:
        if i%a==0 and i%b==0:
            return i

But in this way, RE will not return, so we add a judgment on 1

def LLO(a,b):
    if a==1 or b==1:
        return 1
    i=a
    while 1:
        if i%a==0 and i%b==0:
            return i

We can find it with a function (c++)

long LLO(long a,long b){
	long i=a;
	while (1){
		if(i%a==0 and i%b==0)return i;
		i++;
	}
}

2 greatest common factor

def GCF(a,b):
    for i in range(1,a):
        if(a%i==0 and b%i==0):
            c=i
    return c

Same, a function can solve it (Python)

But in this way, RE will not return, so we add a special judgment for 1

def GCF(a,b):
    if a==1 or b==1:
        return 1
    for i in range(1,a):
        if(a%i==0 and b%i==0):
            c=i
    return c

(c++ function)

long GCF(long a,long b){
	for(long long i=a;i>0;i--)if(a%i==0 and b%i==0)return i;
}

3 Integrate the overall program

Let’s talk again, we have another formula, all the same: LLO(a,b)*GCF(c,d)

Above code (Python)

def LLO(a,b):
    if a==1 or b==1:
        return 1
    i=a
    while 1:
        if i%a==0 and i%b==0:
            return i
def GCF(a,b):
    if a==1 or b==1:
        return 1
    for i in range(1,a):
        if(a%i==0 and b%i==0):
            c=i
    return c
l=input().split(' ')#输入去空格
a=int(l[0])
b=int(l[1])
c=int(l[2])
d=int(l[3])
print(LLO(a,b)*GCF(c,d))

on c++ code

#include<iostream>
using namespace std;
long LLO(long a,long b){
	long i=a;
	while (1){
		if(i%a==0 and i%b==0)return i;
		i++;
	}
}
long GCF(long a,long b){
	for(long long i=a;i>0;i--)if(a%i==0 and b%i==0)return i;
}
int main(){
	long a,b,c,d;
	cin>>a>>b>>c>>d;
	cout<<LLO(a,b)*GCF(c,d);
	return 0;
}

 AC Yeah passed

Guess you like

Origin blog.csdn.net/m0_72193379/article/details/128292468