Graveyard Design POJ - 2100
Posted 啦啦啦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Graveyard Design POJ - 2100相关的知识,希望对你有一定的参考价值。
King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.
After a consultation with his astrologer, King George decided that the lengths of section sides must be a sequence of successive positive integer numbers. A section with side length s contains s 2 graves. George has estimated the total number of graves that will be located on the graveyard and now wants to know all possible graveyard designs satisfying the condition. You were asked to find them.Input
Output
Sample Input
2030
Sample Output
2 4 21 22 23 24 3 25 26 27
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<string> 5 #include<cstring> 6 #include<cmath> 7 #define MAX 10000005 8 using namespace std; 9 typedef long long ll; 10 11 ll n; 12 ll a[MAX],b[MAX],c[MAX]; 13 14 int main() 15 { while(~scanf("%lld",&n)){ 16 int temp=sqrt(n)+1; 17 ll l=1,r=1,t=0; //用long long!!!调了一个多小时。。。 18 ll ans=0; 19 while(r<=temp){ 20 if(r*r>n) break; 21 ans+=r*r; 22 while(ans>n){ 23 ans-=l*l; 24 l++; 25 } 26 r++; 27 if(ans==n){ 28 t++; 29 b[t]=r; 30 c[t]=r-l; 31 } 32 } 33 if(t==0) printf("0\n"); 34 else{ 35 printf("%d\n",t); 36 for(int i=1;i<=t;i++){ 37 printf("%lld",c[i]); 38 for(int j=c[i];j>0;j--) printf(" %lld",b[i]-j); 39 printf("\n"); 40 } 41 } 42 } 43 }
以上是关于Graveyard Design POJ - 2100的主要内容,如果未能解决你的问题,请参考以下文章
Greedy:Graveyard Design(POJ 2100)