首页 > 其他分享 >CF776B Sherlock and his girlfriend 筛法

CF776B Sherlock and his girlfriend 筛法

时间:2022-10-22 16:01:49浏览次数:69  
标签:his jewelry color 样例 pieces colors CF776B Sherlock girlfriend

一道略显思维的筛法题

Sherlock and his girlfriend

题面翻译

题目描述

Sherlock 有一个新女朋友。现在情人节就要到了,他想送给她一些珠宝。

他买了几件首饰。第 \(i\) 件的价格等于 \(i+ 1\),也就是说,珠宝的价格分别为 \(2,3,4,n + 1\) 。

现在需要给这些珠宝首饰上色。当一件珠宝的价格是另一件珠宝的价格的素因子时,这两件的颜色就不允许相同。 此外,要最少化使用的颜色数量。

输入输出格式:

输入格式:

一行,包含单个整数 \(n(1\le n\leq 100000)\) 指珠宝的数量。

输出格式

第一行的输出包含一个整数 \(K\),指最少颜色的颜色种类数。

第二行由 \(n\) 个整数(\(1\) 到 \(k\))组成,按价格从小到大来表示每件珠宝的颜色。

如果有多种方法,则可以输出它们中的任何一种。

说明与提示

在第一个样例中,第一、第二和第三件首饰的价格分别为 \(2\)、\(3\)、\(4\),它们的颜色分别为 \(1\) 、\(1\) 和 \(2\)。

在这种情况下,由于 \(2\) 是 \(4\) 的因子,所以具有因数 \(2\) 和 \(4\) 的珠宝的颜色必须是不同的。

Translated by @皎月半洒花。

题目描述

Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry.

He bought $ n $ pieces of jewelry. The $ i $ -th piece has price equal to $ i+1 $ , that is, the prices of the jewelry are $ 2,3,4,...\ n+1 $ .

Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don't have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.

Help Sherlock complete this trivial task.

输入格式

The only line contains single integer $ n $ ( $ 1<=n<=100000 $ ) — the number of jewelry pieces.

输出格式

The first line of output should contain a single integer $ k $ , the minimum number of colors that can be used to color the pieces of jewelry with the given constraints.

The next line should consist of $ n $ space-separated integers (between $ 1 $ and $ k $ ) that specify the color of each piece in the order of increasing price.

If there are multiple ways to color the pieces using $ k $ colors, you can output any of them.

样例 #1

样例输入 #1

3

样例输出 #1

2
1 1 2

样例 #2

样例输入 #2

4

样例输出 #2

2
2 1 1 2

提示

In the first input, the colors for first, second and third pieces of jewelry having respective prices $ 2 $ , $ 3 $ and $ 4 $ are $ 1 $ , $ 1 $ and $ 2 $ respectively.

In this case, as $ 2 $ is a prime divisor of $ 4 $ , colors of jewelry having prices $ 2 $ and $ 4 $ must be distinct.


注意到,所有质数两两互质,故质数都是一个颜色。然后剩下的数只能是合数,合数又一定可以分解成这些筛出的质数的乘积,所以合数必须和质数异色,而且合数之间两两都不会是素因子,所以,最少是两种颜色。然后细节处理,发现 \(n<3\) 时,只有一种。

这道题给的时限特别大,连 \(O(\sqrt{n}n)\) 都过去了,我们可以用 \(O(n)\) 的线性筛,也可以用 \(O(n \log\log n)\) 的埃氏筛。

然后发现因子不会超过 \(\sqrt{n}\),所以只需要筛到根号n就可以。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int n;
bool flag[maxn];

int main() 
{
	cin >> n;
	flag[0] = flag[1] = 1;
	for (int i = 2; i * i <= n + 1; i++)
		if (!flag[i])
			for (int j = i << 1; j <= n + 1; j += i)
				flag[j] = 1;
	if (n < 3) cout << 1 << endl;
	else cout << 2 << endl;
	
	for (int i = 2; i <= n + 1; i++) 
	{
		printf("%d ", flag[i] + 1);
	}
	return 0;
}

标签:his,jewelry,color,样例,pieces,colors,CF776B,Sherlock,girlfriend
From: https://www.cnblogs.com/CYLSY/p/16816252.html

相关文章

  • Go常见错误总结1:'test' redeclared in this package
    Go常见错误总结1:'test'redeclaredinthispackage问题:'test'redeclaredinthispackage产生原因:变量名或方法名重名问题导致的,你这个变量和其他.go文件里面方......
  • This:从执行上下文的角度来理解这个
    This:从执行上下文的角度来理解这个。这在全局执行上下文中让我们首先看看这在全局执行上下文中是什么。可以在控制台输入console.log(this)在全局执行上下文中打印出这......
  • Linux-history 命令的介绍与使用
    Linux-history命令的介绍与使用介绍在linux下面可以使用history命令查看用户的所有历史操作,同时shell命令操作记录默认保存在用户目录的.bash_history文件中......
  • 为了讲明白继承和super、this关键字,群主发了20块钱群红包
    摘要:以群主发红包为例,带你深入了解继承和super、this关键字。本文分享自华为云社区《群主发红包带你深入了解继承和super、this关键字》,作者:共饮一杯无。需求群主发随......
  • javascript 的setTimeOut 中this指向及外部参数传参
    //外部的参数传参数,放到第三项及以后就可以myArray=['zero','one','two'];myArray.myMethod=function(sProperty){consol......
  • 小程序的var that = this
    小程序的varthat=thisthis是指当前对象,只是一个指针,真正的对象存放在堆内存中,this的指向在程序执行过程中会变化,因此如果需要在函数中使用全局数据需要合适地将this复......
  • 解决004--Loading local data is disabled; this must be enabled on both the client
    因为下载了SQLyog的ultimate版本,现在就可以导入外部的数据了。有着之前使用insertinto插入语句来添加近50条有着大概10个字段的记录的经历之后,本着能够导入现成的数据就导......
  • hash和history
    hash和history路由的区别在了解路由模式前,我们先看下什么是单页面应用,vue-router的实现原理是怎样的,这样更容易理解路由。SPA与前端路由SPA(单页面应用,全程为:Single-......
  • Gym - 101147J Whistle's New Car 树上差分
    J.Whistle'sNewCartimelimitpertestmemorylimitpertestinputoutputWhistlehasboughtanewcar,whichhasaninfinitefueltankcapacity.Hediscoveredani......
  • SVN中Revert changes from this revision 跟Revert to this revision的区别
    假如有个 文件,有十个版本,假定版本号是1,2,3,4,5,6,7,8,9,10。Reverttothisrevision:如果是在版本6这里点击“Reverttothisrevision”,表示7~10的修改全部作废,历史倒退到了版本6......