You are given positive integer number n. You should create such strictly increasingsequence 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
题意:

给出正整数n。我们要创建k个正数1,  2,...,  k的严格增加序列,它们的和等于n,并使其最大公约数是最大的。

序列的最大公约数是:序列的每个元素都可以被它们整除。

如果没有可能的序列,则输出-1。

思路:

(1) 若k个数字的gcd为A,那么n一定可以整除A,所以考虑从n的因子里选出这个A来,先求出k个数字可以表示的最小的数,即 1+2+3+...+k = k(k+1)/2

(2) 设a1 a2 ... ak为解,那么a1+a2+...+ak=n,设gcd(a1,a2...,ak)=A,那么 a1=A*b1 ,a2=A*b2 … ak=A*bk。

(3) 那么gcd(b1,b2,...,bk)=1,且A*(b1+b2+...+bk)=n,所以n%A=0,说明A为n的因数。

ps:n/A = b1+b2+...+bk

(1) 首先因为n≤1010,所以通过 k(k+1)/2 可知:k最大为141420。

(2) 然后处理出所有n的因数,因数从大往小找,找到第一个符合 n/A≥k(k+1)/2 的因数即可。反正b1,b2,…,bk它们的gcd是1,直接令前k-1个数分别是1~k-1,第k项为 (n/A-前k项的总和)

(3) 将最后的答案全部乘A即可!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A[],cnt;
//A[]储存n的因子 cnt为因子个数
void fact(ll n)
{
ll i;
for(i=,cnt=;i*i<=n;i++)
{
if(n%i==)
{
if(n%i!=i)
{
A[++cnt]=i;
A[++cnt]=n/i;
}
else A[++cnt]=i;
}
}
}
int main()
{
//k最大为141420
ll n,k,i,j;
while(scanf("%lld%lld",&n,&k)!=EOF)
{
ll fk=k*(k+)/; //k个数所能组成的最小值
if(k>)
printf("-1\n");
else if(n<fk)
printf("-1\n");
else
{
fact(n);
sort(A+,A+cnt+);
ll ksum;
int flag=;
for(i=cnt;i>;i--)
{
ksum=n/A[i]; //k个数的总和
if(ksum-fk>=)
{
flag=;
ll sum=; //前k-1个数的总和
for(j=;j<k;j++)
{
printf("%lld ",j*A[i]);
sum+=j;
}
printf("%lld\n",A[i]*(ksum-sum));
break;
}
}
if(!flag)printf("-1\n");
}
}
return ;
}

【CodeForces 803 C】Maximal GCD(GCD+思维)的更多相关文章

  1. Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)

    Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...

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

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

  3. Codeforces 305B:Continued Fractions(思维+gcd)

    http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...

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

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

  5. 【codeforces 803C】Maximal GCD

    [题目链接]:http://codeforces.com/contest/803/problem/C [题意] 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列的所有数 ...

  6. codeforces 1030D Vasya and Triangle【思维+gcd】

    题目:戳这里 题意:选出三个点构成三角形,要求面积为n*m/k. 解题思路:因为三个点的坐标都是正整数,根据三角形面积公式(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2=n* ...

  7. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

  8. [Codeforces 364D]Ghd(随机算法+gcd)

    [Codeforces 364D]Ghd(随机算法) 题面 给出n个正整数,在其中选出n/2(向上取整)个数,要求这些数的最大公约数最大,求最大公约数的最大值 分析 每个数被选到的概率\(\geq \ ...

  9. codeforces 1058D.Vasya and Triangle (gcd)

    <题目链接> <转载于  >>> > 题目大意: 给出n.m.k.求一个三角形使它的面积等于n*m/k  并且这个三角形的三个顶点所在的坐标为整数点,且顶点满 ...

随机推荐

  1. common-jdbc:一个基于SpringJdbcTemplate的高性能数据库操作工具类库

    项目地址:https://gitee.com/cnsugar/common-jdbc 一.简介 基于SpringJdbcTemplate的高性能数据库操作工具类库,支持mysql.oracle数据库, ...

  2. 初识MySQL——人生若如初相逢

    CREATE TABLE `student`(`studentNo` INT (4) NOT NULL PRIMARY KEY COMMENT '学号',`loginPwd` VARCHAR(20) ...

  3. 前端框架——Bootstrap

    一.Bootstrap介绍 凡是使用过bootstrap的开发者,不外乎做这么两件事情:复制and粘贴. Bootstrap官方网址:http://www.bootcss.com Bootstrap, ...

  4. rest-framework框架——认证、权限、频率组件

    一.rest-framework登录验证 1.models.py添加User和Token模型 class User(models.Model): name = models.CharField(max ...

  5. YCRefreshView-自定义支持上拉加载更多,下拉刷新。。。

    自定义支持上拉加载更多,下拉刷新,支持自由切换状态[加载中,加载成功,加载失败,没网络等状态]的控件,拓展功能[支持长按拖拽,侧滑删除]可以选择性添加 .具体使用方法,可以直接参考demo. 轻量级侧 ...

  6. wxpython,wx.EVT_ENTER_WINDOW

    这个例子是鼠标移入,button的label显示“Over Me”,但是我运行没有显示求怎么回事

  7. 【Leetcode】【Easy】Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. Nginx+Tomcat+Session 高性能群集搭建

    随着IT行业的发展,linux服务器在企业中应用广泛,人们对linux上的应用服务要求也越来越高,早先的apache服务器.apache有优点也 有不足,apache渐渐不能满足人们的要求,目前ngi ...

  9. Android笔记之 图片自由裁剪

    前言--项目中须要用到对用户头像的裁剪和上传功能.关于裁剪.一開始是想自己来做,可是认为这个东西应该谷歌有开发吧,于是一搜索官方文档.果然有.于是.就果断无耻地用了Android自带有关于照片的自由裁 ...

  10. hiho 第155周 任务分配

    最小路径覆盖会超时: 贪心思路: 按照开始时间排序,然后根据结束时间,维护一个以结束时间的单调递增的队列,每次与最快结束的任务进行比较即可: /* #include <cstdio> #i ...