codeforces 45G Prime Problem

G. Prime Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In Berland prime numbers are fashionable — the respectable citizens dwell only on the floors with numbers that are prime numbers. The numismatists value particularly high the coins with prime nominal values. All the prime days are announced holidays!

Yet even this is not enough to make the Berland people happy. On the main street of the capital stand n houses, numbered from 1 to n. The government decided to paint every house a color so that the sum of the numbers of the houses painted every color is a prime number.

However it turned out that not all the citizens approve of this decision — many of them protest because they don't want many colored houses on the capital's main street. That's why it is decided to use the minimal possible number of colors. The houses don't have to be painted consecutively, but every one of n houses should be painted some color. The one-colored houses should not stand consecutively, any way of painting is acceptable.

There are no more than 5 hours left before the start of painting, help the government find the way when the sum of house numbers for every color is a prime number and the number of used colors is minimal.

Input

The single input line contains an integer n (2 ≤ n ≤ 6000) — the number of houses on the main streets of the capital.

Output

Print the sequence of n numbers, where the i-th number stands for the number of color for house number i. Number the colors consecutively starting from 1. Any painting order is allowed. If there are several solutions to that problem, print any of them. If there's no such way of painting print the single number -1.

Sample test(s)
input
8
output
1 2 2 1 1 1 1 2
题意:
给一个数,用最少的颜色填充。要求:
每个数字的编号和是素数。
思路:
这里用到哥德巴赫猜想,每个大于2的正偶数可以写成两个素数的和。首先求总的编号的和
(1)如果是素数,全部填充1
(2)如果不是素数,看是否是能分成连个素数的和
(3)如果不能分,则这个数一定是奇数,减去一个三,变成偶数,进行分解。最多三种颜色足够。
付代码:
#include <stdio.h>
#define nmax 10000
int n; int t;
int color[nmax];
bool isprime[50000000];
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
		color[i] = 0;
	t = n * (n + 1) / 2;
	for(int i=0; i<=t; i++)
		isprime[i] = true;
	for(int i=2; i*i<=t; i++)
	{
		if(!isprime[i])
			continue;
		for(int j = i * i; j <= t; j+=i)
		{
			isprime[j] = false;
		}
	}
	if(isprime[t])
	{
		for(int i=1; i<=n; i++)
			color[i] = 1;
	}
	else
	{
		int u = -1;
		for(int i=2; i<t-1; i++)
			if(isprime[i] && isprime[t - i])
				u = i;
		if(u == -1)
		{
			color[3] = 3;
			t -= 3;
			u = -1;
			for(int i=2; i<t-1; i++)
				if(isprime[i] && isprime[t - i])
					u = i;
		}
		if(true)
		{
			for(int i=n; i>=1; i--)
			{
				if(color[i] == 0 && i <= u)
				{
					color[i] = 1;
					u -= i;
				}
			}
			for(int i=1; i<=n; i++)
				if(color[i] == 0) color[i] = 2;
		}
	}
	for(int i=1; i<=n; i++) printf("%d ", color[i]);
	printf("\n");
	return 0;
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值