Educational Codeforces Round 20 C(math)

題目鏈接: http://codeforces.com/problemset/problem/803/C

題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的gcd盡量大...

思路: 顯然題目的要求是求 n = a1*cnt + a2*cnt + a3*cnt..+ak*cnt 其中a1<a2<a3..<ak 並且要求cnt盡量大;

要使cnt盡量大,那麼顯然a1...ak要盡量小, 那麼可以令a1...ak=1, 2, 3, ..k, 令key=a1+a2..+ak=(1+k)*k/2;則有: n = cnt*key+y  (n不一定剛好能分下,可能多出y;

顯然有 cnt | y, 令y=cnt*z, 所以有 n=cnt*(key+z), 顯然cnt爲n滿足條件 n/cnt>=key的最大公因子, 那麼只要for一下找出cnt即可(我這裏sb了,居然一開始用二分去找cnt;

最後輸出 1*cnt, 2*cnt ...(k-1)*cnt, (n/cnt-key+k)*cnt即可...這裏的n/cnt-key爲 z;

代碼:

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4
 5 int main(void){
 6     ll n, k, key, cnt=0;
 7     cin >> n >> k;
 8     double cc=(k+1)/2.0*k;
 9     if(cc>n){
10         cout << -1 << endl;
11         return 0;
12     }
13     key=cc;
14     for(ll i=1; i*i<=n; i++){
15         if(n%i==0){
16             if(n/i>=key) cnt=max(cnt, i);
17             if(i>=key) cnt=max(cnt, n/i);
18         }
19     }
20     for(ll i=1; i<k; i++){
21         cout << i*cnt << " ";
22     }
23     cout << (n/cnt-key+k)*cnt << endl;
24     return 0;
25 }

时间: 2024-08-12 12:49:47

Educational Codeforces Round 20 C(math)的相关文章

Educational Codeforces Round 20 B

Description You are given the array of integer numbers a0,?a1,?...,?an?-?1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line cont

Educational Codeforces Round 20 A

Description You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the botto

Educational Codeforces Round 20 D. Magazine Ad

The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this: There are space-separated non-empty words of lowercase and uppercase Latin letters. There are hyphen characters '-' in some words

Educational Codeforces Round 20 C

Description You are given positive integer number n. You should create such strictly increasing sequence of k positive numbersa1,?a2,?...,?ak, that their sum is equal to n and greatest common divisor is maximal. Greatest common divisor of sequence is

Educational Codeforces Round 20 A. Maximal Binary Matrix(模拟)

题意:给你一个n*n的全是0的矩阵,和k个数字"1",让你把这k个数字1按照从上到下,从左到右的顺序构建出来 思路:模拟即可 代码: #include <iostream> #include <cstring> using namespace std; int main() { int n,k; int data[105][105]; while(cin>>n>>k) { memset(data,0,sizeof(data)); if(k

Educational Codeforces Round 20解(bu)题记录

A. Maximal Binary Matrix 解法:暴力模拟+贪心 #include<bits/stdc++.h> using namespace std; int a[110][110]; int main(){ int n,k,cmp,f=0;scanf("%d%d",&n,&k); if(k>n*n){puts("-1");return 0;}cmp=n; int r=1; while(k>=2*cmp-1){ k-

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Educational Codeforces Round 26 D. Round Subset(dp)

题目链接:Educational Codeforces Round 26 D. Round Subset 题意: 给你n个数,让你选其中的k个数,使得这k个数的乘积的末尾的0的个数最大. 题解: 显然,末尾乘积0的个数和因子2和因子5的个数有关. 然后考虑dp[i][j]表示选i个数,当前因子5的个数为j时,能得到因子2最多的为多少. 那么对于每个数,记录一下因子2和5的个数,做一些01背包就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) me

CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforces Round 26 题意: 设定数组 y = f(x) 使得 y[i] = sum(x[j]) (0 <= j < i) 求初始数组 A0 经过多少次 f(x) 后 会有一个元素 大于 k 分析: 考虑 A0 = {1, 0, 0, 0} A1 = {1, 1, 1, 1} -> {C(