您的位置:首页 > 其它

codeforces 45G Prime Problem

2015-08-21 10:06 295 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: