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
output
题意:
给一个数,用最少的颜色填充。要求:
每个数字的编号和是素数。
思路:
这里用到哥德巴赫猜想,每个大于2的正偶数可以写成两个素数的和。首先求总的编号的和
(1)如果是素数,全部填充1
(2)如果不是素数,看是否是能分成连个素数的和
(3)如果不能分,则这个数一定是奇数,减去一个三,变成偶数,进行分解。最多三种颜色足够。
付代码:
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; }
相关文章推荐
- Paths on a Grid(简单组合数学)
- 时间序列数据库的秘密(2)——索引
- 一培训机构设计的学习android课程内容:供大家参考
- Paths on a Grid(简单组合数学)
- [置顶] Android开发之动态库调用
- 隐马尔可夫模型的forward算法的c实现
- xml理论学习总结
- 数据挖掘该怎样的学习路线?
- 详解Hibernate中的NoSession问题
- SQL专场
- 如何删掉win8.1我的计算机里的文件夹(by 星空武哥)
- 如何教女朋友一起编程
- 质数环
- RedHat6.4使用CentOS163yum源在线安装及更新软件
- UVA 400 Unix ls
- quagga实现 isis路由(1)
- MagicalRecord使用教程
- [leetcode-160]Intersection of Two Linked Lists(c)
- tomcat为什么要禁用session?
- 解决lists标签中,加上where后其他条件失效的问题