zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 20 C 数学/贪心/构造

    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
    6 3
    Output
    1 2 3
    Input
    8 2
    Output
    2 6
    Input
    5 3
    Output
    -1

    题意:构造一个递增的长度为k 和为n 的数列,gcd尽可能的大.
    题解:gcd 从1~i 所以寻找长度为k 首项为i 等差为i 数列和为n的一个序列
    细细考虑 n应当模尽i 那么枚举n的因子 check 一下 取最大的i
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<set>
     7 #include<vector>
     8 #define ll __int64
     9 using namespace std;
    10 ll n,k;
    11 bool fun(ll s){
    12     if(k*(k+1)*s/2>n)
    13         return false;
    14     if((n-k*(k-1)*s/2)%s==0)
    15         return true;
    16     return false;
    17 }
    18 int main()
    19 {
    20     scanf("%I64d %I64d",&n,&k);
    21     if(k>(2*n/(1+k)))
    22         printf("-1
    ");
    23     else
    24     {
    25         ll e=1;
    26         for(ll i=1;i*i<=n;i++){
    27             if(n%i==0&&fun(i)) e=max(e,i);
    28             if(n%i==0&&fun(n/i)) e=max(e,n/i);
    29         }
    30         for(int j=1; j<k; j++)
    31             printf("%I64d ",j*e);
    32         printf("%I64d
    ",n-k*(k-1)*e/2);
    33     }
    34     return 0;
    35 }



  • 相关阅读:
    MS SQL float转nvarchar
    RDLC添加页码
    RDLC报表分页显示标题
    程序员必读书单
    SQL查询结果增加序号列
    SQL字符串分组聚合(分组后的数据查询后用逗号隔开)
    现在有一列是int的,把它变成5位的字符串,左边不够的补0
    oracle客户端精简绿色版-环境变量配置
    DISTINCT后按照DISTINCT之前的某列进行排序
    设计模式之代理模式
  • 原文地址:https://www.cnblogs.com/hsd-/p/6798615.html
Copyright © 2011-2022 走看看