【CodeForces 803 C】Maximal GCD(GCD+思维)
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
6 3
1 2 3
8 2
2 6
5 3
-1
给出正整数n。我们要创建k个正数a 1, a 2,..., a 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+思维)的更多相关文章
- 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 ...
- codeforces 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
- Codeforces 305B:Continued Fractions(思维+gcd)
http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- 【codeforces 803C】Maximal GCD
[题目链接]:http://codeforces.com/contest/803/problem/C [题意] 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列的所有数 ...
- codeforces 1030D Vasya and Triangle【思维+gcd】
题目:戳这里 题意:选出三个点构成三角形,要求面积为n*m/k. 解题思路:因为三个点的坐标都是正整数,根据三角形面积公式(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2=n* ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
- [Codeforces 364D]Ghd(随机算法+gcd)
[Codeforces 364D]Ghd(随机算法) 题面 给出n个正整数,在其中选出n/2(向上取整)个数,要求这些数的最大公约数最大,求最大公约数的最大值 分析 每个数被选到的概率\(\geq \ ...
- codeforces 1058D.Vasya and Triangle (gcd)
<题目链接> <转载于 >>> > 题目大意: 给出n.m.k.求一个三角形使它的面积等于n*m/k 并且这个三角形的三个顶点所在的坐标为整数点,且顶点满 ...
随机推荐
- Android设备之间通过Wifi通信
之前写过PC与Android之间通过WIFI通信(通过Socket,可以在博客里面搜索),PC作为主机,Android作为客户机,现在手头有一台仪器通过wifi传输数据,如果仪器作为主机发射WIFI热 ...
- Flask插件---flask_script与flask_migrate
import app from flask_script import Manager from flask_migrate import Migrate,MigrateCommand my_app ...
- jquery获取不了ajax动态添加的内容的解决办法
在HTML页面的一个button <div class="ajaxClick"> <button>内容</button> </div> ...
- git如何进行远程分支切换
git如何进行远程分支切换 git上查看远程分支命令: git branch -a 例如: 然后我想切换到daily/1.0.0远程分支:前提是必须要创建一个本地分支,并让它和远程分支进行关联,gi ...
- (转)防止ViewPager中的Fragment被销毁的方法
在使用ViewPager与Fragment的时候,ViewPager会自动缓存1页内的数据,如下图: 当我们当前处在页面2的时候,页面1和页面3的View实际上已经创建好了,所以在我们拖动的时候是可以 ...
- 【转】成型滤波与匹配滤波的MATLAB实现
转载自:https://blog.csdn.net/yuan1164345228/article/details/45919315 Fd=1; Fs=8; Delay=3; R=0.5; [yf,tf ...
- java web的安全约束--表单的验证
例子,表单和JDBCRealm的安全验证 参考了一篇文章http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228698.html 1.要在wab ...
- spark包
spark-assembly-1.5.2-hadoop2.6.0.jar http://blog.csdn.net/ronaldo4511/article/details/53035494 http: ...
- WAKE-WIN10-SOFT-CMAKE
1,CMAKE 官网:https://cmake.org/ 下载:https://cmake.org/download/ BING:https://www.bing.com/search?q=cmak ...
- Intel® Manager for Lustre* software(一)
Intel® Manager for Lustre* software Installation 软件安装指导目录: 安装IML(Intel® Manager for Lustre* software ...