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

Copy
1 2 3
input

Copy
8 2
output

Copy
2 6
input

Copy
5 3
output

Copy
-1

题目链接: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整除)
这样我们首先O(sqrt(N))的时间里求出N的所有因子,然后从大到小排序(贪心的思想,因为题目要求gcd尽量大)。
并且只有当n/gcd >= ((K*(1+K))/2) 的时候,才会构造出以gcd为a1值公差为gcd的等差数列。 然后我们就只需要用代码实现出这个过程就好了。(本文原文出处:https://www.cnblogs.com/qieqiemin/
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n,k;
vector<ll> foc;
int main()
{
// gbtb;
// cin>>n>>k;
scanf("%lld %lld",&n,&k);
ll sum=(k*(1ll+k)/2ll);
if(sum>n||k>=141421ll)
{
// cout<<-1<<'\n';
printf("-1\n");
}else
{
if(k==1ll)
{
// cout<<n<<'\n';
printf("%lld\n",n);
}else
{
int num;
ll js=sqrt(n);
for(ll i=1ll;i<=js;i++)
{
if(n%i==)
{
foc.push_back(i);
if(i*i!=n)
{
foc.push_back(n/i);
}
}
}
sort(foc.begin(),foc.end());
reverse(foc.begin(),foc.end());
ll gcd;
num=foc.size();
for(int j=;j<num;j++)
{
ll i=foc[j];
if(n%i==)
{
ll cnt=n/i;
if(cnt>=sum)
{
gcd=i;
break;
}
}
}
ll now=gcd;
ll he=0ll;
for(ll i=1ll;i<=k-;i++)
{
he+=now;
printf("%lld ",now);
now+=gcd;
}
printf("%lld ",n-he);
}
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
 

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. MarkDown 排版测试

    大标题 小标题(正常) 小标题(多一杠) 一级标题 二级标题 三级标题 四级标题(未空格) 四级标题(正常) 个人编程,写一个命令行程序 注册Github账号,建立项目仓库 添加ReadMe.md并编 ...

  2. Codeforces gym 101343 A. On The Way to Lucky Plaza【概率+逆元+精度问题】

     2017 JUST Programming Contest 2.0 题目链接:http://codeforces.com/gym/101343/problem/A A. On The Way to ...

  3. gitlab 数据同步

    为了统一化管理,需要把老版本的 gitlab 仓库 同步到新的gitlab上. 1. 新建组, 新gitlab 建立的group 与 原gitlab相同.2.  新建project 3.  选择导入

  4. 第一行代码 3-5 软件也要拼脸蛋-UI界面-更强大的滚动条- 聊天室

    动画 https://www.jianshu.com/p/4fc6164e4709 前面说过,RecyclerView可以设置列表中Item删除和添加的动画,在v7包中给我们提供了一种默认的Item删 ...

  5. hadoop学习笔记壹 --环境搭建及配置文件的修改

    Hadoop生态和其他生态最大的不同之一就是“单一平台多种应用”的理念了. hadoop能解决是什么问题: 1.HDFS :海量数据存储 MapReduce: 海量数据分析   YARN :资源管理调 ...

  6. 树莓派开启SSH

    2017-04-10-raspbian-jessie默认SSH功能时关闭的 boot下新建一个ssh的空文件

  7. js删除数组元素、清空数组的简单方法

    一.清空数组 ? 1 2 3 var ary = [1,2,3,4]; ary.splice(0,ary.length);//清空数组 console.log(ary); // 输出 [],空数组,即 ...

  8. 【转】对random_state参数的理解

    转自:https://blog.csdn.net/az9996/article/details/86616668 在学习机器学习的过程中,常常遇到random_state这个参数,下面来简单叙述一下它 ...

  9. jmeter(四)HTTP请求

    启动jmeter,建立一个测试计划 这里再次说说怎么安装和启动jmeter吧,昨天下午又被人问到怎样安装和使用,我也是醉了:在我看来,百度能解决百分之八十的问题,特别是基础的问题... 安装:去官网下 ...

  10. linux笔记 - 配置与编译

    linux内核下载地址:https://www.kernel.org/ ubuntu下载内核对应源码: sudo apt-get source linux-$(uname -r) #此命令下载的源码存 ...