【数学】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 ...
随机推荐
- 类成员的指针必须NULL化,否则是乱七八糟的东西
class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...
- Docker - Image创建
自己创建Image会有一些好处,可以选择最新的版本,而且从国内的镜像创建时更新软件也会从该镜像获取,速度更快. (1)安装debootstrap zhouh1@uhome:/media/zhouh1/ ...
- mac homebrew安装
http://book.51cto.com/art/201107/278761.htm 3.2.3 使用 Homebrew 安装 Git Mac OS X 有好几个包管理器,用于管理一些开源软件在 M ...
- (译文)IOS block编程指南 3 概念总览
Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...
- SQLite与MySQL、SQLServer等异构数据库之间的数据同步
SQLite DBSync是开源嵌入式数据库SQLite的数据同步引擎,实现了SQLite与SQLite数据库之间以及SQLite与异构数据库(Oracle.MySQL.SQLServer)之间的增量 ...
- [整理] webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用
webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用 https://www.cnblogs.com/moqiutao/p/7496718.html
- JS 冒泡事件顺序
参考:https://www.cnblogs.com/diaoyan/p/5630014.html
- python基础一 day2
内容: 3%%s 输出:3%s 后面的全部转义 结果: 如果是因为执行break语句导致循环提前结束,就不会执行else. 单位换算: 编码方式: ascii unicode u ...
- 一条update语句优化小记
遇到性能问题的sql如下: sql1: UPDATE amlclientlevel a SET a.client_value = (SELECT l.client_value ...
- Session/EntityManager is closed
Hinbernate操作数据库必须要开启事务, 但是在添加事务的时候遇到这个问题也是郁闷, 说Session被关闭了, 而这个Session又是必须的. 关键是我并没有关闭, 也找不到是哪里被关闭了的 ...