按照题意构造集合即可
注意无解情况的判断
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <map> using namespace std; int n,sum; int main(){ scanf("%d",&n); if(n==1){ printf("No\n"); return 0; } sum=(n+1)*n/2; // printf("%d\n",sum); for(int i=2;i<=sum;i++){ for(int j=1;j<=sqrt(i);j++){ if(i%j==0){ if(j!=i/j){ if(j!=1&&sum!=i){ if((sum-i)%j==0){ vector<int> ax,bx; int cnta=0,cntb=0; int now=n; for(int k=i;k>0;){ if(k>=now){ k-=now; cnta++; ax.push_back(now); now--; } else{ cntb++; bx.push_back(now); now--; } } while(now){ bx.push_back(now); now--; cntb++; } printf("Yes\n"); printf("%d ",cnta); for(int i=0;i<ax.size();i++) printf("%d ",ax[i]); printf("\n"); printf("%d ",cntb); for(int i=0;i<bx.size();i++) printf("%d ",bx[i]); printf("\n"); return 0; } } if(i/j!=1&&sum!=i){ if((sum-i)%(i/j)==0){ vector<int> ax,bx; int cnta=0,cntb=0; int now=n; for(int k=i;k>0;){ if(k>=now){ k-=now; cnta++; ax.push_back(now); now--; } else{ cntb++; bx.push_back(now); now--; } } while(now){ bx.push_back(now); now--; cntb++; } printf("Yes\n"); printf("%d ",cnta); for(int i=0;i<ax.size();i++) printf("%d ",ax[i]); printf("\n"); printf("%d ",cntb); for(int i=0;i<bx.size();i++) printf("%d ",bx[i]); printf("\n"); return 0; } } } else{ if(j!=1&&i!=sum) if((sum-i)%j==0){ vector<int> ax,bx; int cnta=0,cntb=0; int now=n; for(int k=i;k>0;){ if(k>=now){ k-=now; cnta++; ax.push_back(now); now--; } else{ cntb++; bx.push_back(now); now--; } } while(now){ bx.push_back(now); now--; cntb++; } printf("Yes\n"); printf("%d ",cnta); for(int i=0;i<ax.size();i++) printf("%d ",ax[i]); printf("\n"); printf("%d ",cntb); for(int i=0;i<bx.size();i++) printf("%d ",bx[i]); printf("\n"); return 0; } } } } } printf("No\n"); return 0; }
原文地址:https://www.cnblogs.com/dreagonm/p/9602507.html
时间: 2024-11-09 17:25:14