題解——Codeforces Round #508 (Div. 2) T2 (構造)
阿新 • • 發佈:2018-09-07
pre string col tdi push_back size std tor include
按照題意構造集合即可
註意無解情況的判斷
#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; }
題解——Codeforces Round #508 (Div. 2) T2 (構造)