http://codeforces.com/contest/803/problem/C

【题意】

给定两个数n,k(1 ≤ n, k ≤ 10^10)

要你输出k个数,满足以下条件:

①这k个数之和等于n

②严格递增

②输出的这k个数的最大公约数q尽可能大。

【思路】

因为是严格递增,所以sum[k]>=k(k+1)/2,而n<=1e10,所以k应该在1e5多一点。

可以找出最大的满足n<=1e10的k,给定的k超出这个直接输出-1.

然后接下来考虑怎么使最大公约数最大:

因为q肯定也是n的约数,所以可以先把n的公约数都找出来,时间复杂度是O(sqrt(n)),从大到小排序后一次判断能不能满足q*(1+2+···+k)<=n就可以了。

【注意】

1. 判断q*(1+2+···+k)<=n不能写if(q*sum[k]<=n)

会爆的,最大的q就是n本身,当n=1e10,k=1e5的时候,q*sum[k]就爆long long了,所以要写成

  1. if(sum[m]<=n/d[i])
  2. {
  3. return true;
  4. }
  5. return false;

2. 对于i*i==n的要特别判断

3. 要特别考虑1 1的corner case.

【Accepted】

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <deque>
  10. #include <stack>
  11. #include <string>
  12. #include <bitset>
  13. #include <ctime>
  14. #include<algorithm>
  15. #include<cstring>
  16. using namespace std;
  17. typedef long long ll;
  18. ll n,m;
  19. const int maxn=1e6+;
  20. ll sum[maxn];
  21. int cou;
  22. void Init()
  23. {
  24. memset(sum,,sizeof(sum));
  25. for(int i=;i<maxn;i++)
  26. {
  27. sum[i]=sum[i-]+(ll)i;
  28. if(sum[i]>=)
  29. {
  30. cou=i;
  31. break;
  32. }
  33. }
  34. }
  35. ll d[];
  36. bool cmp(ll a,ll b)
  37. {
  38. return a>b;
  39. }
  40.  
  41. bool judge(int i)
  42. {
  43. if(sum[m]<=n/d[i])
  44. {
  45. return true;
  46. }
  47. return false;
  48. }
  49. void Print(int i)
  50. {
  51. for(int k=;k<=m-;k++)
  52. {
  53. cout<<d[i]*(ll)k<<" ";
  54. }
  55. cout<<n-d[i]*sum[m-]<<endl;
  56. }
  57. int main()
  58. {
  59. Init();
  60. cin>>n>>m;
  61. if(m>=cou)
  62. {
  63. printf("-1\n");
  64. return ;
  65. }
  66. if(sum[m]>n)
  67. {
  68. printf("-1\n");
  69. return ;
  70. }
  71. int cnt=;
  72. ll index=;
  73. for(int i=;(ll)i*(ll)i<n;i++)
  74. {
  75. if(n%(ll)i==)
  76. {
  77. d[cnt++]=(ll)i;
  78. d[cnt++]=n/(ll)i;
  79. }
  80. index=(ll)i;
  81. }
  82. index++;
  83. if(index*index==n)
  84. {
  85. d[cnt++]=index;
  86. }
  87. sort(d,d+cnt,cmp);
  88. int flag=;
  89. for(int i=;i<cnt;i++)
  90. {
  91. if(judge(i))
  92. {
  93. flag=;
  94. Print(i);
  95. break;
  96. }
  97. }
  98. if(flag==)
  99. {
  100. printf("-1\n");
  101. }
  102. return ;
  103. }

注意爆ll,注意corner case

【数学】codeforces C. Maximal GCD的更多相关文章

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

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

  2. Codeforces 803C. Maximal GCD 二分

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

  3. Codeforces H. Maximal GCD(贪心)

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

  4. CodeForces - 803C Maximal GCD 【构造】

    You are given positive integer number n. You should create such strictly increasing sequence of k po ...

  5. Codeforces 803C. Maximal GCD

    题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...

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

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

  7. Maximal GCD CodeForces - 803C (数论+思维优化)

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

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

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

  9. 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 ...

随机推荐

  1. 类成员的指针必须NULL化,否则是乱七八糟的东西

    class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...

  2. Docker - Image创建

    自己创建Image会有一些好处,可以选择最新的版本,而且从国内的镜像创建时更新软件也会从该镜像获取,速度更快. (1)安装debootstrap zhouh1@uhome:/media/zhouh1/ ...

  3. mac homebrew安装

    http://book.51cto.com/art/201107/278761.htm 3.2.3 使用 Homebrew 安装 Git Mac OS X 有好几个包管理器,用于管理一些开源软件在 M ...

  4. (译文)IOS block编程指南 3 概念总览

    Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...

  5. SQLite与MySQL、SQLServer等异构数据库之间的数据同步

    SQLite DBSync是开源嵌入式数据库SQLite的数据同步引擎,实现了SQLite与SQLite数据库之间以及SQLite与异构数据库(Oracle.MySQL.SQLServer)之间的增量 ...

  6. [整理] webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用

    webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用 https://www.cnblogs.com/moqiutao/p/7496718.html

  7. JS 冒泡事件顺序

    参考:https://www.cnblogs.com/diaoyan/p/5630014.html

  8. python基础一 day2

    内容:   3%%s   输出:3%s       后面的全部转义 结果: 如果是因为执行break语句导致循环提前结束,就不会执行else. 单位换算: 编码方式: ascii  unicode u ...

  9. 一条update语句优化小记

    遇到性能问题的sql如下: sql1: UPDATE amlclientlevel a SET    a.client_value = (SELECT l.client_value           ...

  10. Session/EntityManager is closed

    Hinbernate操作数据库必须要开启事务, 但是在添加事务的时候遇到这个问题也是郁闷, 说Session被关闭了, 而这个Session又是必须的. 关键是我并没有关闭, 也找不到是哪里被关闭了的 ...