【数学】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 ...
随机推荐
- java中stringBuilder的用法
java中stringBuilder的用法 String对象是不可改变的.每次使用 System.String类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需 ...
- Spring-bean(二)
命名空间 自动装配 bean之间的关系:继承:依赖 使用外部属性文件 SpEL bean的生命周期 bean的后置处理器 (一)util命名空间 当用list,set等集合时,不能将集合作为独立的be ...
- poj1862 Stripies
思路: 简单贪心. 实现: #include <iostream> #include <cstdio> #include <algorithm> #include ...
- fetch和axios区别,proxy代理配置
1.今天使用fetch调用接口时使用console.log(res.data)始终是undefined,使用anxios请求则可以成功请求到数据,非常奇怪,于是查了一圈,才搞明白是我自以为了,哎,浪费 ...
- mysql 查询数据库参数命令
1.select @@tx_isolation; 查询数据库设置的事务隔离级别 2.desc table_name; 显示表设计 3.show create table table_name; ...
- KendoUI Grid Pager部分 Nan-Nan of x Items
相关问题: http://stackoverflow.com/questions/23941065/pager-error-in-kendo-gridnan-nan-of-1-items http:/ ...
- CPP-STL:随机数发生器random_shuffle
//--------------------------------------------------------------------------- #include <string.h& ...
- VC-基础:常用的安全CRT函数
常用的安全CRT函数 安全CRT(C Runtime Library = C运行时间库)函数,是微软公司对C/C++语言的扩展.它在原来函数名后添加了“_s”后缀:一般返回出错代码:并将原来的函数返回 ...
- QT_7_资源文件_对话框_QMessageBox_界面布局_常用控件
资源文件 1.1. 将资源导入到项目下 1.2. 添加文件—>Qt -->Qt Resource File 1.3. 起名称 res ,生成res.qrc文件 1.4. 右键 open i ...
- mysql中ibatis的limit动态传参
param.put("pageNo",pageNo); param.put("pageSize",pageSize); sqlMap中的用法 limit $ ...