zoukankan      html  css  js  c++  java
  • Maximal GCD CodeForces

    C. Maximal GCD
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.

    Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.

    If there is no possible sequence then output -1.

    Input

    The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).

    Output

    If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.

    Examples
    input
    Copy
    6 3
    output
    Copy
    1 2 3
    input
    Copy
    8 2
    output
    Copy
    2 6
    input
    Copy
    5 3
    output
    Copy
    -1

    题目链接:https://vjudge.net/problem/CodeForces-803C#author=0
    一题目大意:给你一个N和K,让你输出K个数,使其K个数的和为N,并且这K个数是严格递增的,同时要求这K个数的gcd尽可能的大。
    思路:
    首先我们要用思维优化一下:
    通过观察数据范围,我们可以看到K最值可以取到1e10,那么题目又要输出K个数,光输出K个数的时间复杂度就是O(K)了,题目要求1000ms,肯定会TLE。
    那么我们应该知道肯定当K大于一个数值的时候,就不存在答案输出-1了。
    由于要求这K个数是严格递增的,那么不难想到,当gcd最小为1的时候,也是1,2,3,5,6,,..,k。
    那么这K个数的总和就是(K*(1+K))/2,(等差数列的公式),即如果这个数大于N的时候,是无解的输出-1,那么根据N最大可以取到1e10,我们可以算出K的最值应该是141420,
    当K>141420的时候,直接输出-1表示无解。原因上边已经论述,这时候的总和是大于1e10的,即大于N的值。
    然后我们进行有解的情况。
    首先要使K个数和为N,并且GCD尽可能的大, 我们应该可以得知这样一个信息,这个GCD一定是N的一个因子(因为K个数都可以被gcd整除,那么他的和也一定被gcd整除)
    这样我们首先O(sqrt(N))的时间里求出N的所有因子,然后从大到小排序(贪心的思想,因为题目要求gcd尽量大)。
    并且只有当n/gcd >= ((K*(1+K))/2) 的时候,才会构造出以gcd为a1值公差为gcd的等差数列。

    然后我们就只需要用代码实现出这个过程就好了。(本文原文出处:https://www.cnblogs.com/qieqiemin/
    我的AC代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb std::ios::sync_with_stdio(false)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define gg(x) getInt(&x)
    using namespace std;
    typedef long long ll;
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    ll n,k;
    vector<ll> foc;
    int main()
    {
    //    gbtb;
    //    cin>>n>>k;
        scanf("%lld %lld",&n,&k);
        ll sum=(k*(1ll+k)/2ll);
        if(sum>n||k>=141421ll)
        {
    //        cout<<-1<<'
    ';
            printf("-1
    ");
        }else
        {
            if(k==1ll)
            {
    //            cout<<n<<'
    ';
                printf("%lld
    ",n);
            }else
            {
                int num;
                ll js=sqrt(n);
                for(ll i=1ll;i<=js;i++)
                {
                    if(n%i==0)
                    {
                        foc.push_back(i);
                        if(i*i!=n)
                        {
                            foc.push_back(n/i);
                        }
                    }
                }
                sort(foc.begin(),foc.end());
                reverse(foc.begin(),foc.end());
                ll gcd;
                num=foc.size();
                for(int j=0;j<num;j++)
                {
                    ll i=foc[j];
                    if(n%i==0)
                    {
                        ll cnt=n/i;
                        if(cnt>=sum)
                        {
                            gcd=i;
                            break;
                        }
                    }
                }
                ll now=gcd;
                ll he=0ll;
                for(ll i=1ll;i<=k-1;i++)
                {
                    he+=now;
                    printf("%lld ",now);
                    now+=gcd;
                }
                printf("%lld ",n-he);
            }
        }
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    View Code
     
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    ios 数据类型转换 UIImage转换为NSData NSData转换为NSString
    iOS UI 12 block传值
    iOS UI 11 单例
    iOS UI 08 uitableview 自定义cell
    iOS UI 07 uitableviewi3
    iOS UI 07 uitableviewi2
    iOS UI 07 uitableview
    iOS UI 05 传值
    iOS UI 04 轨道和动画
    iOS UI 03 事件和手势
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10241573.html
Copyright © 2011-2022 走看看