• BZOJ 2257: [Jsoi2009]瓶子和燃料 裴蜀定理


    2257: [Jsoi2009]瓶子和燃料

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://www.lydsy.com/JudgeOnline/problem.php?id=2257

    Description

    jyy就一直想着尽快回地球,可惜他飞船的燃料不够了。 
    有一天他又去向火星人要燃料,这次火星人答应了,要jyy用飞船上的瓶子来换。jyy
    的飞船上共有 N个瓶子(1<=N<=1000) ,经过协商,火星人只要其中的K 个 。 jyy
    将 K个瓶子交给火星人之后,火星人用它们装一些燃料给 jyy。所有的瓶子都没有刻度,只
    在瓶口标注了容量,第i个瓶子的容量为Vi(Vi 为整数,并且满足1<=Vi<=1000000000 ) 。 
    火星人比较吝啬,他们并不会把所有的瓶子都装满燃料。他们拿到瓶子后,会跑到燃料
    库里鼓捣一通,弄出一小点燃料来交差。jyy当然知道他们会来这一手,于是事先了解了火
    星人鼓捣的具体内容。火星人在燃料库里只会做如下的3种操作:1、将某个瓶子装满燃料;
    2、将某个瓶子中的燃料全部倒回燃料库;3、将燃料从瓶子a倒向瓶子b,直到瓶子b满
    或者瓶子a空。燃料倾倒过程中的损耗可以忽略。火星人拿出的燃料,当然是这些操作能
    得到的最小正体积。 
    jyy知道,对于不同的瓶子组合,火星人可能会被迫给出不同体积的燃料。jyy希望找
    到最优的瓶子组合,使得火星人给出尽量多的燃料

    Input

    第1行:2个整数N,K,  
    第2..N 行:每行1个整数,第i+1 行的整数为Vi  

    Output

    仅1行,一个整数,表示火星人给出燃料的最大值。

    Sample Input

    3 2
    3
    4
    4

    Sample Output

    4

    HINT

    题意

    题解:

    先考虑一个问题,如果给k个数,那么能得到最大的答案是多少呢?

    裴蜀定理,答案是这几个数的最大公因数

    那么我们枚举所有数的因数,取出现次数大于K次的最大的数就ok了~

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200500
    #define mod 1001
    #define eps 1e-9
    #define pi 3.1415926
    int Num;
    //const int inf=0x7fffffff;
    const ll inf=999999999;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //*************************************************************************************
    
    map<int,int> H;
    vector<int> Q;
    int main()
    {
        int n=read(),k=read();
        for(int i=0;i<n;i++)
        {
            int x=read();
            for(int j=1;j<=sqrt(x);j++)
            {
                if(x%j==0)
                {
                    if(!H[j])
                        Q.push_back(j);
                    H[j]++;
                    if(j*j==x)
                        continue;
                    if(!H[x/j])
                        Q.push_back(x/j);
                    H[x/j]++;
                }
    
            }
        }
        sort(Q.begin(),Q.end());
        for(int i=Q.size()-1;i>=0;i--)
        {
            if(H[Q[i]]>=k)
            {
                printf("%d
    ",Q[i]);
                return 0;
            }
        }
    }
  • 相关阅读:
    Win7+CentOS7双系统安装
    python中的 __getattr__ __setattr__ __getitem__ __add__
    对象.函数名 叫方法 和 类.函数名 叫函数
    python中的__str__ __name__ 和__call__方法
    flask中的登录验证 装饰器版 befor_request版
    装饰器
    Django model中的 class Meta 详解
    跨域问题cors
    reids缓存
    python-django目录
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4791285.html
Copyright © 2020-2023  润新知