C. Ehab and a Special Coloring Problem

链接:https://codeforces.com/contest/1174/problem/C

题意:

You're given an integer nn. For every integer ii from 22 to nn, assign a positive integer aiai such that the following conditions hold:

  • For any pair of integers (i,j)(i,j), if ii and jj are coprime, aiajai≠aj.
  • The maximal value of all aiai should be minimized (that is, as small as possible).

A pair of integers is called coprime if their greatest common divisor is 11.

思路:

筛法,把下标为素数的赋予新值,他的倍数再赋予同样的值即可。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;

int a[MAXN];

int main()
{
    cin >> n;
    int cnt = 0, pos = 0;
    for (int i = 2;i <= n;i++)
    {
        if (a[i] != 0)
            continue;
//        cout << i << endl;
        a[i] = ++pos;
        cnt++;
        int j = 2*i;
        while (j <= n)
        {
            a[j] = pos;
            j += i;
            cnt++;
        }
//        if (cnt >= n-1)
//            break;
    }
    for (int i = 2;i <= n;i++)
        cout << a[i] << ' ' ;
    cout << endl;

    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/10972867.html