Maximal GCD CodeForces
来源:互联网 发布:linux x264命令 编辑:程序博客网 时间:2024/05/22 01:51
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.
Example
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
找出一个递增数列,长度为k,总和为n,使得数列公因数最大,可知数列的形式应该是a,2a,…,ka最优,即要满足a*k(k+1)/2<=n,从大到小找n的因数,判断个数是否满足,可以把循环分为两次1~sqrt节约时间
#include <iostream>#include <stdio.h>#include <math.h>using namespace std;int main(){ long long i,j,n,k,t,q,sk; double qq; while(cin>>n>>k){ if(k>1e6||2*n<(k*(k+1))){ printf("-1\n"); continue; } qq=sqrt(n*1.0); q=(long long)qq; sk=k*(k+1)/2; t=0; for(i=1;i<=q;i++){ if(n%i==0){ j=n/i; if(sk<=i){ t=j; break; } } } if(t==0){ for(i=q+1;i>=1;i--){ if(n%i==0){ if(i*sk<=n){ t=i; break; } } } } for(i=1;i<k;i++){ j=i*t; printf("%I64d ",j); } j=n-t*k*(k-1)/2; printf("%I64d\n",j); } return 0;}
- Maximal GCD CodeForces
- codeforces 803C Maximal GCD
- 【Codeforces 803 C. Maximal GCD】
- Codeforces 803C Maximal GCD 题解
- Educational Codeforces Round 20-C. Maximal GCD
- Codeforces 803C Maximal GCD【思维】
- Maximal GCD
- Maximal GCD
- Educational Codeforces Round 20 C || Codeforces803C Maximal GCD (水题)
- 803C Maximal GCD
- Codeforces803C Maximal GCD
- Codeforces803C Maximal GCD 思维
- codeforce 803C Maximal GCD(构造)
- CF803C:Maximal GCD(思维 & 构造)
- CF 803C Maximal GCD 枚举,构造
- Educational Codeforces Round 20 Maximal Binary Matrix
- Codeforces 803 A. Maximal Binary Matrix (模拟
- Codeforces 803A Maximal Binary Matrix 题解
- 【boolan c++】 带有指针成员的class
- 《汇编语言》(王爽)检测点2.2
- Bloom Filter原理介绍
- Xor Sum (01字典树)基础的字典树题目
- Wannafly模拟赛5 D Aria(全排列)
- Maximal GCD CodeForces
- hdu 4857
- 高级面试题-String-StringBuilder-StringBuffer 的区别
- 2017CCPC秦皇岛 M:Safest Buildings
- 2017.11.5总结
- 习题6.1
- 类的声明与定义
- 常见错误解决方式
- 1103