Find out if it is possible to partition the first n positive integers into two non-empty disjoint sets S1 and S2 such that:
gcd(sum(S1),sum(S2))>1
Here sum(S) denotes the sum of all elements present in set S and gcd means thegreatest common divisor.
Every integer number from 1 to n should be present in exactly one of S1 or S2.
Input
The only line of the input contains a single integer n (1≤n≤45000)
Output
If such partition doesn’t exist, print “No” (quotes for clarity).
Otherwise, print “Yes” (quotes for clarity), followed by two lines, describing S1 and S2 respectively.
Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.
If there are multiple possible partitions — print any of them.
Examples
Input
1
Output
No
Input
3
Output
Yes
1 2
2 1 3
Note
In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is “No”.
In the second example, the sums of the sets are 2 and 4 respectively. The gcd(2,4)=2>1, hence that is one of the possible answers.
题意
将1~n这n个数分成两个集合s1s_1s1 ,s2s_2s2, sum(sis_isi)代表第i个集合中所有元素之和。
问你能否找到这样的集合s1s_1s1 ,s2s_2s2使得GCD( sum(s1s_1s1) , sum(s2s_2s2) )>1。
思路
首先,数是确定的,1,2,3,⋯\cdots⋯,n 。
刚开始想使两个集合的和都为偶数,这样GCD( sum(s1s_1s1) , sum(s2s_2s2) ) == 2了,可后来发现行不通。
后观察发现:可以将前n-1个数放入集合s1s_1s1,把第n个数放入集合s2s_2s2,
证明 (当n大于2时)
1. 如果n为奇数,sum(s1s_1s1) = ∑i=1n−12(ai+an−i)\sum^{\frac {n-1}2}_{i=1} {( a_i+a_{n-i} )}∑i=12n−1(ai+an−i) = n2an{\frac n2}{a_n}2nan。
这样GCD( sum(s1s_1s1), sum(s2s_2s2) ) = GCD( n2an{\frac n2}{a_n}2nan, ana_nan ) = ana_nan > 2
2. 如果n为偶数,sum(s1s_1s1) = ∑i=1n−22(ai+an−i)\sum^{\frac {n-2}2}_{i=1} {( a_i+a_{n-i} )}∑i=12n−2(ai+an−i) + an2a_{\frac n2}a2n = (n-1)an2a_{\frac n2}a2n
这样GCD( sum(s1s_1s1), sum(s2s_2s2) ) = GCD( (n-1)an2a_{\frac n2}a2n, ana_nan ) >= an2a_{\frac n2}a2n > 2
坑点
输出有两行,第i行第一个数代表第i个集合的个数,之后是第i个集合的元素。
一开始看样例理解成多行,第一行是两个集合的元素个数。第二行是各个集合的元素。以后还是要仔细看题
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cstdio>
#include <vector>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define N 100005
#define newmax(a,b) a>b?a:b
#define newmin(a,b) a>b?b:a
#define Lowbit(x) (x&-x)
using namespace std;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
int main()
{
int n;
cin>>n;
if(n==1||n==2)
cout<<"No"<<endl;
else if(n==3)
{
printf("Yes\n%d %d\n%d %d %d\n",1,2,2,1,3);
}
else
{
printf("Yes\n");
printf("%d ",n-1);
for(int i=1; i<n; i++)
{
printf("%d%c",i,i+1>=n?'\n':' ');
}
printf("1 %d\n",n);
}
return 0;
}