zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    Code First 迁移----官方 应用程序启动时自动升级(MigrateDatabaseToLatestVersion 初始值设定项)
    使用 Entity Framework Core 时,通过代码自动 Migration
    泛型反射获取特性值
    使用Expression动态创建lambda表达式
    ElasticSearch + Logstash + Kibana 搭建笔记
    PHP获取指定函数定义在哪个文件中及行号
    Wordpress中文章的特色图像Featured Image究竟存在哪里?
    PhpSpreadsheet生成Excel时实现单元格自动换行
    Composer使用体验
    PHP命名空间学习笔记
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5585851.html
Copyright © 2011-2022 走看看