【数学】codeforces C. Maximal GCD
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了,所以要写成
if(sum[m]<=n/d[i])
{
return true;
}
return false;
2. 对于i*i==n的要特别判断
3. 要特别考虑1 1的corner case.
【Accepted】
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll n,m;
const int maxn=1e6+;
ll sum[maxn];
int cou;
void Init()
{
memset(sum,,sizeof(sum));
for(int i=;i<maxn;i++)
{
sum[i]=sum[i-]+(ll)i;
if(sum[i]>=)
{
cou=i;
break;
}
}
}
ll d[];
bool cmp(ll a,ll b)
{
return a>b;
} bool judge(int i)
{
if(sum[m]<=n/d[i])
{
return true;
}
return false;
}
void Print(int i)
{
for(int k=;k<=m-;k++)
{
cout<<d[i]*(ll)k<<" ";
}
cout<<n-d[i]*sum[m-]<<endl;
}
int main()
{
Init();
cin>>n>>m;
if(m>=cou)
{
printf("-1\n");
return ;
}
if(sum[m]>n)
{
printf("-1\n");
return ;
}
int cnt=;
ll index=;
for(int i=;(ll)i*(ll)i<n;i++)
{
if(n%(ll)i==)
{
d[cnt++]=(ll)i;
d[cnt++]=n/(ll)i;
}
index=(ll)i;
}
index++;
if(index*index==n)
{
d[cnt++]=index;
}
sort(d,d+cnt,cmp);
int flag=;
for(int i=;i<cnt;i++)
{
if(judge(i))
{
flag=;
Print(i);
break;
}
}
if(flag==)
{
printf("-1\n");
}
return ;
}
注意爆ll,注意corner case
【数学】codeforces C. Maximal GCD的更多相关文章
- codeforces 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
- Codeforces 803C. Maximal GCD 二分
C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...
- Codeforces H. Maximal GCD(贪心)
题目描述: H. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CodeForces - 803C Maximal GCD 【构造】
You are given positive integer number n. You should create such strictly increasing sequence of k po ...
- Codeforces 803C. Maximal GCD
题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...
- CodeForce-803C Maximal GCD(贪心数学)
Maximal GCD CodeForces - 803C 现在给定一个正整数 n.你需要找到 k 个严格递增的正整数 a1, a2, ..., ak,满足他们的和等于 n 并且他们的最大公因数尽量大 ...
- Maximal GCD CodeForces - 803C (数论+思维优化)
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
- 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 ...
随机推荐
- linux给文件或目录添加apache权限
系统环境:ubuntu11.10/apache2/php5.3.6 在LAMP环境中,测试一个简单的php文件上传功能时,发现/var/log/apache2/error.log中出现如下php警告: ...
- [转]C#综合揭秘——细说多线程(上)
引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发. 其中委托的BeginInvoke方法以及回调函数最为常用. 而 I/O线程 ...
- AJPFX的反射学习笔记
反射是描述 数据结构的结构 属性.方法(数据)元数据 类(数据结构)描述数据的结构-->类也是特殊的对象---->元数据 CLASS类 描述数据结 ...
- 基于pymysql模块的增删改查
上课笔记 重点:(熟练)多表查询创建存储过程原生sql索引原理 pymysql 封装好的客户端cursor 底层就是一个send操作commit 告诉mysql真的要完成修改操作(不然修改不会生效)e ...
- BottomNavigationBar底部导航条用法
先来张效果图 接下来是实现过程 1.加入依赖 compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0' 2.xml布局 fragment ...
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...
- like SQL注入与防止 (bin2hex unhex)
普通的列表模糊查询,可能会被sql注入利用,造成数据泄漏,严重的甚至导致删表删库! 程序中sql语句拼装: $sql = 'student_name like '"%'.$name.'%&q ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- Linux目录结构及详细介绍
/:根目录,位于Linux文件系统目录结构的顶层,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中. /bin,/usr/bin:该 ...
- Java中类,对象,方法的内存分配
Java中类,对象,方法的内存分配 以下针对引用数据类型: 在内存中,类是静态的概念,它存在于内存中的CodeSegment中. 当我们使用new关键字生成对象时,JVM根据类的代码,去堆内存中开辟一 ...