Codeforces 678C Joty and Chocolate

本文介绍了一个有趣的数学问题:如何通过给特定编号的瓷砖上色来最大化获得巧克力的数量。瓷砖根据编号的不同分别可以被涂成红色或蓝色,并依据颜色不同获得数量不等的巧克力。文章提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Joty and Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has to paint them in a strange pattern.

An unpainted tile should be painted Red if it's index is divisible by a and an unpainted tile should be painted Blue if it's index is divisible by b. So the tile with the number divisible by a and b can be either painted Red or Blue.

After her painting is done, she will get p chocolates for each tile that is painted Red and q chocolates for each tile that is painted Blue.

Note that she can paint tiles in any order she wants.

Given the required information, find the maximum number of chocolates Joty can get.

Input

The only line contains five integers nabp and q (1 ≤ n, a, b, p, q ≤ 109).

Output

Print the only integer s — the maximum number of chocolates Joty can get.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Examples
input
5 2 3 12 15
output
39
input
20 2 3 3 5
output
51

题目大意:有下标为1到n的n个瓷砖,如果某个瓷砖下标能被a整除,那么就能获得p块巧克力,如果能被b整除,就能获得q块巧克力,如果同时被a和b整除,就随便你要哪种巧克力

如果能同时被a和b整除,肯定选最多的那一个啊

#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
int main(){
	ll n,a,b,p,q;
	while(cin>>n>>a>>b>>p>>q){
		cout<<n/a*p+n/b*q-min(p,q)*(n/lcm(a,b))<<endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值