CF1038B Non-Coprime Partition(公约数)

CF1038B Non-Coprime Partition

解题思路:先判断是否可以拆分。统计1~n的和sum,枚举i,i*i<=sum,判断i和sum-i的最大公约数是否大于1,如果有的话,就可以拼凑。接着从大到小构建i.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 1e5+10;
const ull base = 131;
const ll mod =  1e9+7;
const double PI = acos(-1.0);
const double eps = 1e-4;

inline int read(){
    
    int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){
    
    if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=getchar();}return x*f;}
inline string readstring(){
    
    string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){
    
    s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){
    
    str+=s;s=getchar();}return str;}
int random(int n){
    
    return (int)(rand()*rand())%n;}
void writestring(string s){
    
    int n = s.size();for(int i = 0;i < n;i++){
    
    printf("%c",s[i]);}}
ll fast_power(ll a,ll p){
    
    
    ll ans = 1;
    while(p){
    
    
        if(p&1) ans = (ans*a)%mod;
        p >>= 1;
        a = (a*a)%mod;
    }
    return ans;
}


vector<int>num;




int main(){
    
    
    //srand((unsigned)time(NULL));
    //freopen(  "out.txt","w",stdout);
    int n = read();
    ll sum = 0;
    set<int>st;
    for(int i = 1;i <= n;i++){
    
    
        sum += i;
        st.insert(i);
    }

    ll x = 0,y = 0;
    for(ll a = 2;a*a <= sum;a++){
    
    
        ll b = sum-a;
        if(__gcd(a,b) > 1){
    
    
            x = a;
            y = b;
            break;
        }
    }
    //cout<<x<<" "<<y<<endl;
    if(x+y == 0) puts("No");
    else {
    
    
        puts("Yes");
        sum = 0;
        for(int i = n;i >= 1;i--){
    
    
            if(sum+i < x) {
    
    
                num.push_back(i);
                sum += x;
            }else if(sum+i == x){
    
    
                num.push_back(i);
                sum += x;
                break;
            }
        }
        printf("%d ",num.size());
        for(int i = 0;i < num.size();i++){
    
    
            printf("%d ",num[i]);
            st.erase(num[i]);
        }
        puts("");
        printf("%d ",st.size());
        for(auto it = st.begin();it != st.end();it++){
    
    
            printf("%d ",*it);
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42868863/article/details/114653388