日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 803C Maximal GCD(贪心 + 枚举)

發(fā)布時(shí)間:2024/4/18 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 803C Maximal GCD(贪心 + 枚举) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

鏈接一
鏈接二
You 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

Output

-1

思路

題目轉(zhuǎn)換:
N >= gcd * (1 + 2 + ….+ k)
N在符合上面的式子下求最大的gcd。
可以從1開始枚舉gcd 直到剛好大于或等于N的時(shí)候停止,最后貪心輸出答案

注意細(xì)節(jié)
  • 求(1 + 2 + … + k)的時(shí)候可能會爆 long long
  • 可以用sqrt枚舉,降低復(fù)雜度
  • AC

    #include<bits/stdc++.h> #define N 10006 #define ll long long using namespace std; ll n, k, limit; int judge(ll x) { //判斷此時(shí)的gcd是否合適 if(limit <= x) return 1;else return 0; } int main() { // freopen("in.txt", "r", stdin);while (scanf("%lld%lld", &n, &k) != EOF) {limit = (1 + k) * k / 2; if (k > 141421 || limit > n){ //特判不符合條件,limit 也可能爆long longprintf("-1\n");continue;} if (limit == n) { //特判相等 答案:1--k for (ll i = 1; i <= k; i++) printf("%d ", i);printf("\n");}else {ll gcd;for (ll i = 1; i <= sqrt(n); i++) { //每次找n的因子,sqrt降低復(fù)雜度,否則1e10 if (n % i == 0) {if (judge(i)) { //此時(shí)使得 gcd = n / i 最大,即為答案 gcd = n / i;break;}if (judge(n / i)) { //gcd從小找,找到不合適的就退出 gcd = i;}else {break;}}//同理也可以寫成 // if (n % i == 0) { // if (i >= limit) { // gcd = n / i; // break; // }; // if(n / i >= limit) { // gcd = i; // } // } }//貪心輸出答案,前K - 1項(xiàng)為 1*gcd到k-1*gcd ll temp = n / gcd;for(ll i = 1; i < k; i++) { //輸出前K項(xiàng) printf("%lld ", gcd * i);temp -= i;}printf("%lld\n", gcd * temp); //輸出最后一項(xiàng)}} return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的CodeForces - 803C Maximal GCD(贪心 + 枚举)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。