热身训练-b

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了热身训练-b相关的知识,希望对你有一定的参考价值。

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 n, a, b, p 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.

题目大意一共有n块砖要涂,如果是a的倍数涂红色,b的倍数涂蓝色。一块红色价值p,一块蓝色价值q。难点在于既是a的倍数也是b的倍数,如果用循环暴力则会超时,本题利用辗转相除法,算出最小公倍数;

然后用n除以最小公倍数即可以得既是a的倍数又是b的倍数的数目。

#include <iostream>
using namespace std;
int main()
{
    long long int n,a,b,p,q,sum=0,x,y,z,a1,b1,c;
    cin>>n>>a>>b>>p>>q;
    x=n/a;
    y=n/b;
    a1=a;
    b1=b;
    while(b1!=0)
    {
        c=a1%b1;
        a1=b1;
        b1=c;
    }
    z=(n*a1)/(a*b);
    if(p>q)
    {
        sum=p*x+q*(y-z);
    }
    else
    {
        sum=q*y+p*(x-z);
    }
    cout<<sum<<endl;
}

 

以上是关于热身训练-b的主要内容,如果未能解决你的问题,请参考以下文章

寒假训练——热身---递推——数塔

2021年度训练联盟热身训练赛第一场

[Nowcoder] 2021年度训练联盟热身训练赛第六场 Mini Battleship | 深搜 回溯 乱搞

热身训练-k皇后问题(主副对角线计算)

[Nowcoder | UPC] 2021年度训练联盟热身训练赛第六场 Hopscotch | 最短路 bfs

2020-07-06 热身训练赛