Codeforces 803C. Maximal GCD 二分
C. Maximal GCD
time 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.
Examples
input
6 3
output
1 2 3
input
8 2
output
2 6
input
5 3
题意:输出递增的k个数,使得他们的和等于n并且它们的GCD最大。 不存在输出-1。
思路:二分n的约数。(1+k)*k<=n是才存在k个符合要求的数,否者输出-1。
代码:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int maxn=2e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll MAXN=1e13+100; ll n,k,sum; ll sign[maxn]; int check(ll q) { if(sum*q<=n) return 1; else return 0; } int main() { scanf("%lld%lld",&n,&k); if(k>=1e6) { cout<<-1<<endl; return 0; } sum=(1+k)*k/2; if(sum>n) { cout<<-1<<endl; return 0; } ll l=1,r=0,q=0; for(ll i=1; i*i<=n; i++) if(n%i==0) sign[++r]=i,sign[++r]=n/i; sort(sign+1,sign+r+1); while(l<=r) { ll mid=(l+r)/2; if(check(sign[mid])) l=mid+1,q=sign[mid]; else r=mid-1; } ll cou=0; for(int i=1; i<k; i++) { cout<<q*i<<" "; cou+=q*i; } cout<<n-cou<<endl; return 0; }
    I am a slow walker,but I never walk backwards.
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号