先推导出公式x*(x+1)(3*y-x+1)=6*n,然后枚举求解。
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; vector<long long >ansx,ansy; long long n; int main() { while(~scanf("%lld",&n)){ ansx.clear(); ansy.clear(); for(long long x=1;;x++) { long long A=x*(x+1); long long B=6*n/A; long long y=(B+x-1)/3; if(y<x) break; if((6*n)%A!=0) continue; if((B+x-1)%3!=0) continue; ansx.push_back(x); ansy.push_back(y); } int u; if(ansx[ansx.size()-1]==ansy[ansx.size()-1]) u=2*ansx.size()-1; else u=2*ansx.size(); printf("%d ",u); for(int i=0;i<ansx.size();i++) { printf("%lld %lld ",ansx[i],ansy[i]); } for(int i=ansx.size()-1;i>=0;i--) { if(ansy[i]==ansx[i]) continue; printf("%lld %lld ",ansy[i],ansx[i]); } } //x*(x+1)(3*y-x+1)=6*n return 0; }