[Codeforces Round #452 (Div. 2)] Dividing the numbers
2017-12-19 17:37
274 查看
Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible.
Help Petya to split the integers. Each of n integers should be exactly in one group.
Input
The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.
Output
Print the smallest possible absolute difference in the first line.
In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.
Examples
Input
4
Output
0
2 1 4
Input
2
Output
1
1 1
Note
In the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.
In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.
题目大意
1−n分成两组,询问两组和的差值的最小值,并打印一组中元素。
思路
如果n是4的整数倍的话,那么可以将1和n分到一组,将2和n−1分到另一组;将3和n−2分到一组,将4和n−3分到另一组……将n/2−1和n/2+2分到一组,将n/2和n/2+1分到另一组。
如果n%4=1,那么将1分到某一组,然后剩下的按上面的分法分。
如果n%4=2,那么将1分到某一组,将2分到另一组,然后剩下的按上面的方法分。
如果n%4=3,那么将1和2分到同一组,将3分到另一组,然后剩下的还是按上面的方法分。
这样的话所有的情况都讨论完了。
代码
Help Petya to split the integers. Each of n integers should be exactly in one group.
Input
The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.
Output
Print the smallest possible absolute difference in the first line.
In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.
Examples
Input
4
Output
0
2 1 4
Input
2
Output
1
1 1
Note
In the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.
In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.
题目大意
1−n分成两组,询问两组和的差值的最小值,并打印一组中元素。
思路
如果n是4的整数倍的话,那么可以将1和n分到一组,将2和n−1分到另一组;将3和n−2分到一组,将4和n−3分到另一组……将n/2−1和n/2+2分到一组,将n/2和n/2+1分到另一组。
如果n%4=1,那么将1分到某一组,然后剩下的按上面的分法分。
如果n%4=2,那么将1分到某一组,将2分到另一组,然后剩下的按上面的方法分。
如果n%4=3,那么将1和2分到同一组,将3分到另一组,然后剩下的还是按上面的方法分。
这样的话所有的情况都讨论完了。
代码
#include <cstdio> const int maxn=60000; int n; int main() { scanf("%d",&n); if(n%4==0) { printf("0\n%d ",n/2); for(register int i=1; i<=n/2; i+=2) { printf("%d %d ",i,n-i+1); } } else if(n%4==1) { printf("1\n%d ",n/2); for(register int i=2; i<=n/2+1; i+=2) { printf("%d %d ",i,n-i+2); } } else if(n%4==2) { printf("1\n%d 1 ",n/2); for(register int i=3; i<=n/2+1; i+=2) { printf("%d %d ",i,n-i+3); } } else { printf("0\n%d 3 ",n/2); for(register int i=4; i<=n/2+2; i+=2) { printf("%d %d ",i,n-i+4); } } return 0; }
相关文章推荐
- Codeforces Round #452 (Div. 2) C - Dividing the numbers 推规律
- Codeforces Round #452 (Div. 2) - C. Dividing the numbers (思路)
- Codeforces Round #215_div2_E. Sereja and the Arrangement of Numbers
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- 【Codeforces】899 C. Dividing the numbers 贪心
- C - Dividing the numbers CodeForces - 899C 【补】
- Codeforces899C-Dividing the numbers
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树暴力)
- Codeforces Round #378 (Div. 2) A. Grasshopper And the String(水题求跳跃距离)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game(gcd,数论)
- Codeforces Round #258 (Div. 2/C)/Codeforces451C_Predict Outcome of the Game(枚举)
- The only legal comparisons are between two numbers, two strings, or two dates.
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- PAT A1069. The Black Hole of Numbers (20)
- Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function
- quickly find the median of a sequence of numbers
- LeetCode.728 Self Dividing Numbers
- Codeforces Round #177 (Div. 1) B. Polo the Penguin and Houses【组合数学】