CodeForces - 1038B Array Product

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个数分成两个集合 s 1 s_1 s1 , s 2 s_2 s2, sum( s i s_i si)代表第i个集合中所有元素之和
问你能否找到这样的集合 s 1 s_1 s1 , s 2 s_2 s2使得GCD( sum( s 1 s_1 s1) , sum( s 2 s_2 s2) )>1。

思路

首先,数是确定的,1,2,3, ⋯ \cdots ,n 。

刚开始想使两个集合的和都为偶数,这样GCD( sum( s 1 s_1 s1) , sum( s 2 s_2 s2) ) == 2了,可后来发现行不通。

后观察发现:可以将前n-1个数放入集合 s 1 s_1 s1,把第n个数放入集合 s 2 s_2 s2

证明 (当n大于2时)

1. 如果n为奇数,sum( s 1 s_1 s1) = ∑ i = 1 n − 1 2 ( a i + a n − i ) \sum^{\frac {n-1}2}_{i=1} {( a_i+a_{n-i} )} i=12n1(ai+ani) = n 2 a n {\frac n2}{a_n} 2nan
这样GCD( sum( s 1 s_1 s1), sum( s 2 s_2 s2) ) = GCD( n 2 a n {\frac n2}{a_n} 2nan, a n a_n an ) = a n a_n an > 2
2. 如果n为偶数,sum( s 1 s_1 s1) = ∑ i = 1 n − 2 2 ( a i + a n − i ) \sum^{\frac {n-2}2}_{i=1} {( a_i+a_{n-i} )} i=12n2(ai+ani) + a n 2 a_{\frac n2} a2n = (n-1) a n 2 a_{\frac n2} a2n
这样GCD( sum( s 1 s_1 s1), sum( s 2 s_2 s2) ) = GCD( (n-1) a n 2 a_{\frac n2} a2n, a n a_n an ) >= a n 2 a_{\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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值