Joty and Chocolate

链接:http://acm.hust.edu.cn/vjudge/problem/401744/origin

题目:

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.



题意:共有从1到n个数字1.2.3.4.3.....n,遇到a的倍数可以领p个巧克力,遇到b的倍数可以领q个巧克力,遇到既是a,又是b的倍数的领哪个都可以,问最后最多可以拿到多少巧克力。

分析:纯数学计算,只要保证重复之处的取pq的最大值就好。-

题解:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

__int64 gcd(__int64 a,__int64 b)
{
	return (b)?gcd(b,a%b):a;
}
__int64 gd(__int64 a,__int64 b)
{
	return a*b/gcd(a,b);
}

int main()
{
	//freopen("in.txt","r",stdin);
	__int64 n,a,b,p,q;
	while(~scanf("%I64d %I64d %I64d %I64d %I64d",&n,&a,&b,&p,&q))
	{
		__int64 ans=0;
		__int64 na,nb,nab,ab;
		if(p<q)
		{
			swap(a,b);
			swap(p,q);
		}
		ab=gd(a,b);
		na=n/a;
		nb=n/b;
		nab=n/ab;
		nb-=nab;
		ans=p*na+q*nb;
		printf("%I64d\n",ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值