zoukankan      html  css  js  c++  java
  • CF803 C. Maximal GCD

    题目传送门:https://codeforces.com/problemset/problem/803/C

    题目大意:
    给定两个数(n,k),求(k)个数(a_1<a_2<...<a_k),满足(sumlimits_{i=1}^ka_i=n),且使得(gcdlimits_{i=1}^k{a_i})最大


    假定(alpha=gcdlimits_{i=1}^k{a_i}),则有(a_i=alpha imes r_i),因为(sumlimits_{i=1}^kr_igeqslant frac{k(k+1)}{2}),故(alphaleqslant frac{2n}{k(k+1)})

    显然(n=alphasumlimits_{i=1}^kr_i),故(alpha)应为(n)的约数,且最大不超过(frac{2n}{k(k+1)})

    故我们可以(O(sqrt n))找到最大的(alpha),然后按照(r_i=i(1leqslant ileqslant k-1))(r_k=frac{n}{alpha}-sumlimits_{i=1}^{k-1}r_i)构造(a_i)即可

    /*program from Wolfycz*/
    #include<map>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define Fi first
    #define Se second
    #define ll_inf 1e18
    #define MK make_pair
    #define sqr(x) ((x)*(x))
    #define pii pair<int,int>
    #define int_inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline T frd(T x){
    	int f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    template<typename T>inline T read(T x){
    	int f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    vector<ll>vec;
    int main(){
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	ll n=read(0ll),k=read(0ll);
    	if (k>sqrt(n<<1)){
    		printf("-1
    ");
    		return 0;
    	}
    	ll m=(k+1)*k/2,limit=n/m,All=0;
    	if (!limit){
    		printf("-1
    ");
    		return 0;
    	}
    	for (ll i=1;i<=sqrt(n);i++){
    		if (n%i)	continue;
    		vec.push_back(i);
    		if (n/i!=i)	vec.push_back(n/i);
    	}
    	sort(vec.begin(),vec.end());
    	vector<ll>::iterator it=upper_bound(vec.begin(),vec.end(),limit);
    	it--;
    	for (int i=1;i<k;i++)	printf("%lld ",*it*i),All+=*it*i;
    	printf("%lld
    ",n-All);
    	return 0;
    }
    
    作者:Wolfycz
    本文版权归作者和博客园共有,欢迎转载,但必须在文章开头注明原文出处,否则保留追究法律责任的权利
  • 相关阅读:
    Decrease (Judge ver.)
    Raising Modulo Numbers
    最短Hamilton路径
    64位整数乘法
    递归系列——数组和对象的相关递归
    函数内容新增——函数表达式
    数据结构和算法(一)——栈
    (转)jQuery中append(),prepend()与after(),before()的区别
    微信端的user-Agent
    less知识点总结(二)
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/14985272.html
Copyright © 2011-2022 走看看