Codeforces 1174C Ehab and a Special Coloring Problem

Topic links: http://codeforces.com/problemset/problem/1174/C


The meaning of problems: a n you, you should fill array subscript n of the 2 ~ A I , the subscript claim prime number is not equal to two, and the maximum value of the array is minimized.

Idea: Make a list of prime numbers, each prime to prime number so we make sure the first prime number is 1, the second prime number is 2 ... and so on, then according to the Fundamental Theorem of Arithmetic , a composite number on it equal to the first prime number decomposition of friends. 

AC Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN= 2e5 +5;
 4 int vis[MAXN];
 5 int a[MAXN];
 6 int n;
 7 void init()
 8 {
 9     vis[0] = 1;
10     vis[1] = 1;
11     for(int i = 2;i < MAXN;i ++)
12     {
13         if(! vis[i])
14         {
15             vis[i] = 0;
16             for(int j = 2*i;j <= MAXN; j += i)
17             {
18                 vis[j] = 1;
19             }
20         }
21     }
22 }
23 int gcd(int a,int b)
24 {
25     if(b == 0)
26     return a;
27     else return gcd(b,a%b);
28 }
29 int main()
30 {
31     int n;
32     scanf("%d",&n);
33     memset(vis,0,sizeof(vis));
34     init();
35     int cnt = 1;
36     for(int i = 2;i <= n;i++)
37     {
38         if(!vis[i]) a[i] = cnt++;
39         else
40         {
41             for(int j = 2;j <= i;j++)
42             {
43                 if(gcd(i,j) != 1)
44                 {
45                     a[i] = a[j];
46                     break;
47                 }
48             }
49         }
50     }
51     for(int i = 2;i <= n;i++)
52     {
53         printf("%d ",a[i]);
54     }
55     return 0;
56 }

 

Guess you like

Origin www.cnblogs.com/Carered/p/10972854.html
Recommended