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
  1. 6 3
output

Copy
  1. 1 2 3
input

Copy
  1. 8 2
output

Copy
  1. 2 6
input

Copy
  1. 5 3
output

Copy
  1. -1
  2.  
  3. 题目链接: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整除)
    这样我们首先OsqrtN))的时间里求出N的所有因子,然后从大到小排序(贪心的思想,因为题目要求gcd尽量大)。
    并且只有当n/gcd >= ((K*(1+K))/2 的时候,才会构造出以gcda1值公差为gcd的等差数列。
  4.  
  5. 然后我们就只需要用代码实现出这个过程就好了。(本文原文出处:https://www.cnblogs.com/qieqiemin/
    我的AC代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #define rep(i,x,n) for(int i=x;i<n;i++)
  12. #define repd(i,x,n) for(int i=x;i<=n;i++)
  13. #define pii pair<int,int>
  14. #define pll pair<long long ,long long>
  15. #define gbtb std::ios::sync_with_stdio(false)
  16. #define MS0(X) memset((X), 0, sizeof((X)))
  17. #define MSC0(X) memset((X), '\0', sizeof((X)))
  18. #define pb push_back
  19. #define mp make_pair
  20. #define fi first
  21. #define se second
  22. #define gg(x) getInt(&x)
  23. using namespace std;
  24. typedef long long ll;
  25. inline void getInt(int* p);
  26. const int maxn=;
  27. const int inf=0x3f3f3f3f;
  28. /*** TEMPLATE CODE * * STARTS HERE ***/
  29. ll n,k;
  30. vector<ll> foc;
  31. int main()
  32. {
  33. // gbtb;
  34. // cin>>n>>k;
  35. scanf("%lld %lld",&n,&k);
  36. ll sum=(k*(1ll+k)/2ll);
  37. if(sum>n||k>=141421ll)
  38. {
  39. // cout<<-1<<'\n';
  40. printf("-1\n");
  41. }else
  42. {
  43. if(k==1ll)
  44. {
  45. // cout<<n<<'\n';
  46. printf("%lld\n",n);
  47. }else
  48. {
  49. int num;
  50. ll js=sqrt(n);
  51. for(ll i=1ll;i<=js;i++)
  52. {
  53. if(n%i==)
  54. {
  55. foc.push_back(i);
  56. if(i*i!=n)
  57. {
  58. foc.push_back(n/i);
  59. }
  60. }
  61. }
  62. sort(foc.begin(),foc.end());
  63. reverse(foc.begin(),foc.end());
  64. ll gcd;
  65. num=foc.size();
  66. for(int j=;j<num;j++)
  67. {
  68. ll i=foc[j];
  69. if(n%i==)
  70. {
  71. ll cnt=n/i;
  72. if(cnt>=sum)
  73. {
  74. gcd=i;
  75. break;
  76. }
  77. }
  78. }
  79. ll now=gcd;
  80. ll he=0ll;
  81. for(ll i=1ll;i<=k-;i++)
  82. {
  83. he+=now;
  84. printf("%lld ",now);
  85. now+=gcd;
  86. }
  87. printf("%lld ",n-he);
  88. }
  89. }
  90. return ;
  91. }
  92.  
  93. inline void getInt(int* p) {
  94. char ch;
  95. do {
  96. ch = getchar();
  97. } while (ch == ' ' || ch == '\n');
  98. if (ch == '-') {
  99. *p = -(getchar() - '');
  100. while ((ch = getchar()) >= '' && ch <= '') {
  101. *p = *p * - ch + '';
  102. }
  103. }
  104. else {
  105. *p = ch - '';
  106. while ((ch = getchar()) >= '' && ch <= '') {
  107. *p = *p * + ch - '';
  108. }
  109. }
  110. }
  1.  

Maximal GCD CodeForces - 803C (数论+思维优化)的更多相关文章

  1. AC日记——Maximal GCD codeforces 803c

    803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...

  2. CodeForce-803C Maximal GCD(贪心数学)

    Maximal GCD CodeForces - 803C 现在给定一个正整数 n.你需要找到 k 个严格递增的正整数 a1, a2, ..., ak,满足他们的和等于 n 并且他们的最大公因数尽量大 ...

  3. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

  4. Codeforces 803C. Maximal GCD 二分

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

  5. Codeforces H. Maximal GCD(贪心)

    题目描述: H. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Educational Codeforces Round 20 C. Maximal GCD

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

  7. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  8. 【CodeForces 803 C】Maximal GCD(GCD+思维)

    You are given positive integer number n. You should create such strictly increasingsequence of k pos ...

  9. codeforces 798 C. Mike and gcd problem(贪心+思维+数论)

    题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...

随机推荐

  1. leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)

    只能说还是太菜,抄的网上大神的做法: idea: mask 的每一位代表该位字母够不够k次,够k次为0,不够为1 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从m ...

  2. javascript 正则表达式学习

    正则表达式 正则表达式,一个描述字符模式的对象.javascript的RegExp类表示正则表达式,String和RegExp都定义了方法,后者使用正则表达式进行强大的模式和匹配与替换功能. -- f ...

  3. 如何根据name和value选中radio [问题点数:40分,结帖人zzxap

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>  <he ...

  4. 【知识碎片】Mysql语句

    (1)mysql 更新最新的一条记录  ;

  5. 1506 传话 (暴力DFS或者Tarjan模板题)

    题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b,以及所有a认识的人. 如果a认识b,b不一定认识a. 所有人从1到n编号,给出所有“ ...

  6. usb-cam(1)安装

    http://www.liuxiao.org/2015/10/ros-%E5%AE%89%E8%A3%85-usb-camera-%E9%A9%B1%E5%8A%A8%E5%B9%B6%E8%B0%8 ...

  7. springboot+mybatis+springmvc整合实例

    以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是“零配置”,&quo ...

  8. springdashboard环境搭建

    SpringCloud Hystrix Dashboard Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各H ...

  9. scrapy实验1 爬取中国人寿官网新闻,保存为xml

    一.scrapy 实验  爬中国人寿新闻,保存为xml 如需转发,请注明出处:小婷儿的python  https://www.cnblogs.com/xxtalhr/p/10517297.html 链 ...

  10. 运行Android Studio自带模拟器报:Guest isn't online after 7 second...

    今天在运行Android Studio自带的手机模拟器时,出现如下异常情况 : 解决办法: 1.打开Android Virtue Device Manager,点击编辑选项 2.点击show Adva ...