[codeforces 1333F] Kate and imperfection first add all prime numbers, then add the number with a maximum divisor of 2, followed by the number with a maximum divisor of 3, and so on

Codeforces Round # 632 (Div. 2)   Number of matches 12810

[codeforces 1333F] Kate and imperfection first add all prime numbers, then add the number with a maximum divisor of 2, followed by the number with a maximum divisor of 3, and so on

See https://blog.csdn.net/mrcrack/article/details/103564004 for the general catalog

Online evaluation address https://codeforces.com/contest/1333/problem/F

Problem Just Verdict Time Memory
F - Kate and imperfection GNU C ++ 17 Accepted 155 ms 3900 KB

The manual algorithm is as follows

11


1 1 1 1 1 2 3 3 4 5


1,2,3,4,5,6,7,8,9,10,11

先加入所有质数,还包括了1,得到如下数据
1,2,3,5,7,11可以管到I2-I6的输出
1 1 1 1 1 1

I7的情况:1,2,3,5,7,11,4.加入了最大约数是4=2*2的情况。最大值gcd(2,4)=2
I8的情况:1,2,3,5,7,11,4,6.加入了最大约数是6=3*2的情况。最大值gcd(3,6)=3
I9的情况:1,2,3,5,7,11,4,6,9.加入了最大约数是9=3*3的情况。最大值gcd(3,9)=3
I10的情况:1,2,3,5,7,11,4,6,9,8.加入了最大约数是8=4*2的情况。最大值gcd(4,8)=4
I11的情况:1,2,3,5,7,11,4,6,9,8,10.加入了最大约数是10=5*2的情况。最大值gcd(5,10)=5


AC code is as follows

#include <stdio.h>
#define maxn 500010
int p[maxn],ans[maxn],an=1;//p[i]=0表示i是质数
int main(){
	int n,i,j;
	scanf("%d",&n);
	p[1]=1;//因之后的数据处理而设置
	for(i=2;i<=n;i++)
		for(j=i+i;j<=n;j+=i)//j是以i为约数的合数
			p[j]=1;//合数
	for(i=2;i<=n;i++)
		if(p[i]==0)ans[++an]=1;//加入所有质数
	for(i=2;i<=n;i++)//i是(i*j)对应的最大约数
		for(j=1;i*j<=n;j++)
			if(p[j]==0){//合数(i*j)另外一个因子是质数j
				ans[++an]=i;//加入最大约数为i的合数(i*j);
				if(!(i%j))break;//为了保证i是最大约数,此处必须中断
			}
	for(i=2;i<n;i++)printf("%d ",ans[i]);
	printf("%d\n",ans[n]);
	return 0;
}

 

 

 

 

Published 660 original articles · praised 562 · 480,000 views

Guess you like

Origin blog.csdn.net/mrcrack/article/details/105434278