Educational Codeforces Round 20 C 数学/贪心/构造
2017-05-02 21:37
399 查看
C. Maximal GCDtime limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.ExamplesInput
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.ExamplesInput
6 3Output
1 2 3Input
8 2Output
2 6Input
5 3Output
-1 题意:构造一个递增的长度为k 和为n 的数列,gcd尽可能的大. 题解:gcd 从1~i 所以寻找长度为k 首项为i 等差为i 数列和为n的一个序列 细细考虑 n应当模尽i 那么枚举n的因子 check 一下 取最大的i
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<set> #include<vector> #define ll __int64 using namespace std; ll n,k; bool fun(ll s){ if(k*(k+1)*s/2>n) return false; if((n-k*(k-1)*s/2)%s==0) return true; return false; } int main() { scanf("%I64d %I64d",&n,&k); if(k>(2*n/(1+k))) printf("-1\n"); else { ll e=1; for(ll i=1;i*i<=n;i++){ if(n%i==0&&fun(i)) e=max(e,i); if(n%i==0&&fun(n/i)) e=max(e,n/i); } for(int j=1; j<k; j++) printf("%I64d ",j*e); printf("%I64d\n",n-k*(k-1)*e/2); } return 0; }
相关文章推荐
- Educational Codeforces Round 2C. Make Palindrome(贪心构造)
- Educational Codeforces Round 20 C 数学
- Codeforces Round #342 (Div. 2)(A)贪心,数学
- Educational Codeforces Round 20 A - Maximal Binary Matrix
- Educational Codeforces Round 20 F. Coprime Subsequences(容斥)
- Educational Codeforces Round 12(C)贪心
- Educational Codeforces Round 8 C. Bear and String Distance(贪心)
- codefoces Educational Codeforces Round 20
- Educational Codeforces Round 27 A B 暴力+贪心 C 贪心+排序
- Educational Codeforces Round 21 E - Selling Souvenirs(三分or贪心背包)
- Educational Codeforces Round 5 E. Sum of Remainders 思维 数学转换
- Educational Codeforces Round 6(C)尺取法+贪心
- Educational Codeforces Round 15 D. Road to Post Office 数学
- Educational Codeforces Round 15 D. Road to Post Office (数学)
- Educational Codeforces Round 4 E. Square Root of Permutation(置换、构造)
- Educational Codeforces Round 7 E. Ants in Leaves(DFS+贪心)
- Educational Codeforces Round 20 B - Distances to Zero
- Educational Codeforces Round 2 C. Make Palindrome 贪心
- Educational Codeforces Round 21-E-贪心背包or
- 【Educational Codeforces Round 3 C】【贪心 排序】Load Balancing 最小操作步数使得数字之差尽可能小