• codeforces 678C C. Joty and Chocolate(水题)


    题目链接:

    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

    题意:

    a的倍数给p个巧克力,b的倍数q个巧克力,即是a又是b的倍数都可以,那么[1,n]一共可以得到多少巧克力;

    思路:

    跟容斥定理差不多,求一个最小公倍数,就好了;

    AC代码

    //#include <bits/stdc++.h>
    
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef unsigned long long uLL;
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=0x3f3f3f3f;
    const int N=1e6+8;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    LL n,a,b,p,q;
    
    LL gcd(LL x,LL y)
    {
        if(y==0)return x;
        return gcd(y,x%y);
    }
    LL lcm(LL x,LL y)
    {
        LL temp=gcd(x,y);
        return x/temp*y;
    }
    int main()
    {
        read(n);
        read(a);
        read(b);
        read(p);read(q);
        LL ans=n/a*p+n/b*q-n/lcm(a,b)*min(p,q);
        cout<<ans<<"
    ";
    
        return 0;
    }



  • 相关阅读:
    第16章 Java内存模型
    第15章 原子变量与非阻塞同步机制
    第14章 构建自定义的同步工具
    第13章 显式锁
    第12章 并发程序的测试
    第11章 性能与可伸缩性
    第10章避免活跃性危险
    第8章 线程池的使用
    JDBC工具类——JdbcUtils(6)
    JDBC工具类——JdbcUtils(5)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5585851.html
Copyright © 2020-2023  润新知