POJ - 2100 Graveyard Design
题目大意:
给定一个t,求一段连续数的平方和(例如:x1*x1+x2*x2+x3*x3)等于t的方法有几种,并要求输出方案
分析:
尺取法。应为连续的数时单调的,所以能使用尺取法。个人是尺取时看sum==t,是就记录下l,r,并将其存 放到vector里,则size()就是方案书,l-r就是方案里包含几个数,[l,r)区间内数就是方案。
code:
#define debug //#define opentext #include<stdio.h> #include<math.h> #include<cmath> #include<queue> #include<stack> #include<string> #include<cstring> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<functional> #include<iomanip> #include<map> #include<set> #define pb push_back #define dbg(x) cout<<#x<<" = "<<(x)<<endl; #define lson l,m,rt<<1 #define cmm(x) cout<<"("<<(x)<<")"; #define rson m+1,r,rt<<1|1 using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll>PLL; typedef pair<int,ll>Pil; typedef pair<ll,int>Pli; const ll INF = 0x3f3f3f3f; const ll inf=0x7fffffff; const double eps=1e-8; const int maxn =1000000; const int N = 510; const ll mod=1e9+7; const ll MOD=10007; //------ //define ll s[maxn]; const int n=1e7; struct node { int l,r; node(int l,int r):r(r),l(l) {} }; vector<node>m; //solve void solve() { ll t; while(cin>>t) { ll sum=0; int l=1,r=1,cnt=0; for(;;) { while(r*r<=t&&sum<t) {//r*r>t时不管怎么取都是不可能组成t的 sum+=(1ll*r*r); r++; } if(sum==t) { m.push_back(node(l,r)); } sum-=(1ll*l*l); l++; if(sum<=0)break; } cout<<m.size()<<endl; for(int i=0; i<m.size(); i++) { int l=m[i].l,r=m[i].r; cout<<r-l; for(int j=l; j<r; j++)cout<<" "<<j; cout<<endl; } m.clear(); } } int main() { ios_base::sync_with_stdio(false); //-----------freopen------------------- #ifdef debug freopen("in.txt", "r", stdin); #ifdef opentext freopen("out.txt","w",stdout); #endif #endif //------------------------------------- cin.tie(0); cout.tie(0); solve(); //-----------opentext------------------ #ifdef opentext fclose(stdin); fclose(stdout); system("out.txt"); #endif //------------------------------------- return 0; }