Educational Codeforces Round 20 C 数学
2017-04-29 11:24
316 查看
题目:
C. Maximal GCD
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given positive integer number n. You should create such strictly
increasing sequence of kpositive 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
output
input
output
input
output
分析:
求k个数(严格递增),让他们加起来等于n,且它们的最大公约数最大。 我假设最大公约数是maxDiv,因为这几个数是严格递增的,所以它们的和最小sum=sigm(1...k);所以最大公约数不超过MAX=n/sum. k1*g+k2*g+...kk*g=n,然后分解n,g一定是n的因数。k1+k2+..+kk=n/g。右分析可知n/g>=sum,我为了方便,我让k1=1*g,k2=2*g,k(k-1)=(k-1)*g,然后kk=n-sigma(1...k-1)。为什么这样保证一定是递增的?因为n>=g*sigma(1...k)所以kk=n-g*sigma(1...k-1)>g*k的,故,kk大于(k1,k2,..k(k-1))。
code:
#include<cstdio>
#define max(a,b) (a>b?a:b)
typedef long long LL;
int main(void){
LL n,k;scanf("%I64d%I64d",&n,&k);
if(k>1000000){printf("-1\n");return 0;}
LL sum=(1+k)*k/2;
LL MAX=n/sum;
int maxDiv=-1;
for(LL i=1;i*i<=n;++i){
if(n%i==0){
if(i<=MAX)maxDiv=max(maxDiv,i);
if(n/i<=MAX)maxDiv=max(maxDiv,n/i);
}
}
if(maxDiv==-1){printf("-1\n");return 0;}
for(LL i=1;i<=k-1;++i)
printf("%I64d ",maxDiv*i);
printf("%I64d\n",n-k*(k-1)/2*maxDiv);
}
另外就是我犯低级(要命)错误的两个地方:
图1会产生奇怪的答案,因为maxDiv是int型
tu
图1会导致死循环,i*i会变成负数
结论:涉及到溢出就用LL吧,0rzzzz
C. Maximal GCD
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given positive integer number n. You should create such strictly
increasing sequence of kpositive 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
分析:
求k个数(严格递增),让他们加起来等于n,且它们的最大公约数最大。 我假设最大公约数是maxDiv,因为这几个数是严格递增的,所以它们的和最小sum=sigm(1...k);所以最大公约数不超过MAX=n/sum. k1*g+k2*g+...kk*g=n,然后分解n,g一定是n的因数。k1+k2+..+kk=n/g。右分析可知n/g>=sum,我为了方便,我让k1=1*g,k2=2*g,k(k-1)=(k-1)*g,然后kk=n-sigma(1...k-1)。为什么这样保证一定是递增的?因为n>=g*sigma(1...k)所以kk=n-g*sigma(1...k-1)>g*k的,故,kk大于(k1,k2,..k(k-1))。
code:
#include<cstdio>
#define max(a,b) (a>b?a:b)
typedef long long LL;
int main(void){
LL n,k;scanf("%I64d%I64d",&n,&k);
if(k>1000000){printf("-1\n");return 0;}
LL sum=(1+k)*k/2;
LL MAX=n/sum;
int maxDiv=-1;
for(LL i=1;i*i<=n;++i){
if(n%i==0){
if(i<=MAX)maxDiv=max(maxDiv,i);
if(n/i<=MAX)maxDiv=max(maxDiv,n/i);
}
}
if(maxDiv==-1){printf("-1\n");return 0;}
for(LL i=1;i<=k-1;++i)
printf("%I64d ",maxDiv*i);
printf("%I64d\n",n-k*(k-1)/2*maxDiv);
}
另外就是我犯低级(要命)错误的两个地方:
图1会产生奇怪的答案,因为maxDiv是int型
tu
图1会导致死循环,i*i会变成负数
结论:涉及到溢出就用LL吧,0rzzzz
相关文章推荐
- Educational Codeforces Round 20 C 数学/贪心/构造
- Educational Codeforces Round 15 D 数学推公式
- Educational Codeforces Round 20 B - Distances to Zero
- Educational Codeforces Round 20 C
- Educational Codeforces Round 34 (Rated for Div. 2)D. Almost Difference(数学)
- Educational Codeforces Round 15 D. Road to Post Office (数学)
- Educational Codeforces Round 20-C. Maximal GCD
- Educational Codeforces Round 13 C Joty and Chocolate(数学)
- Educational Codeforces Round 26 E. Vasya's Function 数学,迭代法
- Educational Codeforces Round 15 D 数学推导
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树+主席树)
- [Updating] Educational Codeforces Round 20 解题报告
- 【解题报告】Educational Codeforces Round 20
- Educational Codeforces Round 5 E. Sum of Remainders 思维 数学转换
- codefoces Educational Codeforces Round 20
- CF Educational Codeforces Round 20 D. Magazine Ad
- Educational Codeforces Round 20 F. Coprime Subsequences(莫比乌斯反演)
- Educational Codeforces Round 3 B.The Best Gift (数学)
- Educational Codeforces Round 20总结
- Educational Codeforces Round 20 Distances to Zero