前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 355 - Numbers Painting

ACMSGURU 355 - Numbers Painting

作者头像
Reck Zhang
发布2021-08-11 11:03:57
3170
发布2021-08-11 11:03:57
举报
文章被收录于专栏:Reck Zhang

Numbers Painting

Problem Description

Dr. Vasechkin wants to paint all numbers from 1 to N in such a way that if number A is divisible by number B, numbers A and B have different colors.

Help Dr. Vasechkin to find such a painting, where the number of the colors used is minimal.

Input

The input contains integer number N (

)).

Output

Write the number of the colors M in the desired painting in the first line of the output. In the second line of the output write the desired painting of numbers from 1 to N. The used colors should be represented by numbers from 1 to M. If there are several solutions, choose any of them.

Example(s)

sample input

sample output

12

41 2 2 3 2 3 2 4 3 3 2 4

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<int> nums(n + 1, 1);
    int max_num = 1;
    for(int i = 2; i <= n; i++) {
        int num = i;
        int cnt = 0;
        for(int j = 2; j * j <= num; j++) {
            while(num % j == 0) {
                cnt += 1;
                num /= j;
            }
        }
        nums[i] += cnt;
        nums[i] += num > 1;
        max_num = std::max(max_num, nums[i]);
    }
    std::cout << max_num << std::endl;
    for(int i = 1; i <= n; i++) {
        std::cout << nums[i] << " ";
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-12-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Numbers Painting
    • Problem Description
      • Input
      • Output
      • Example(s)
    • Solution
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档