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 }



  • 相关阅读:
    设计模式(观察者模式,MVC模式)
    设计模式(单例,委托模式)
    iOS API
    iOS介绍
    多种服饰颜色搭配
    国内npm镜像使用
    iOS开发拓展篇—应用之间的跳转和数据传
    iOS开发网络篇—使用ASI框架进行文件下载
    iOS开发网络篇—数据缓存(使用NSURLCache)
    iOS开发网络篇—发送json数据给服务器以及多值参数
  • 原文地址:https://www.cnblogs.com/hsd-/p/6798615.html
Copyright © 2011-2022 走看看