poj2100(尺取法)
题意:选取一系列数,使得这些数的平方和等于n;
解题思路:尺取法扫一遍;
#include<iostream> #include<algorithm> using namespace std; struct node { int bin; int len; }a[1000005]; int main() { long long right=1,left=1,cnt=0; long long sum=0,n; cin>>n; while(1) { while(sum<n) { sum+=right*right; right++; } if(sum==n) { cnt++; a[cnt].bin=left; a[cnt].len=right-left; } sum=sum-left*left; left++; if(left*left>n) break; } cout<<cnt<<endl; for(int i=1;i<=cnt;i++) { cout<<a[i].len<<" "; for(int j=1;j<=a[i].len;j++) { cout<<a[i].bin++; if(j<=a[i].len-1) cout<<" "; } cout<<endl; } return 0; }