Usaco Section1.4 Arithmetic progressions Problem solving report

Source: Internet
Author: User

Ariprog Problem Solving report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
Find all arithmetic progression with length n in the double-squared set of S.
The double-squared-Number set S: All sets of numbers that can be represented as p²+q², where 0<=p,q<=m.
"Data Range"
3<=n<=25
1<=m<=250
"Input Format"
N
M
"Output Format"
No sequence was found, output "NONE".
When we find the sequence number, we output all the series, each one row, the format is "a B", the first item, the B is the tolerance.
Output order: Press B Ascending, then a ascending.
"Input Sample"
5
7
"Output Example"
1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Get S out first.
Then with either of the number a as the first item, any one larger than a number B is the second item, you can get a arithmetic progression. Count to Nth, if the n is in set S, then the sequence is legal.
Finally, all the series are sorted according to the topic requirements, output can be.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
The first commit, the card at the 7th Point, the run-time error.
Take the data a try, found that the machine run time 1.5s, it seems that the algorithm needs to be improved.
Change my OK function * to +, time to 1s, less insurance, continue to improve. (but continue to improve the temporary no train of thought, first hand to see ...) )
This time I found myself careless again! OJ did not say I timed out, my code is only run 0.003s stopped, is a simple run-time error!
But the operation of the machine did not error ... Look at the code can not find the cross-border, first to eat ~
Go to eat on the road to think of what is wrong ~ Indeed there is a place array subscript out of bounds, rigor or too poor ah!
Second Commit, AC.
Originally this Question time limit is not 1s ah ... 2.2s can also be too, I worry about ...

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Code

1 /*2 id:icedrea13 Prob:ariprog4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten  One Const intMaxd =125000; A  - intn,m; - BOOLd[1+Maxd]; the  - intL; - intNum[maxd];//Maxd just wrote it casually. -  + intl; - structResult + { A     intb; atFriendBOOL operator< (ResultConst&x,resultConst&y) {returnx.b<y.b | | x.b==y.b && x.a<y.a;} -}a[10001]; -  - BOOLOkintAintb) - { -     //cout<< "a=" <<a<< "\tb=" <<b<<endl; in      for(intI=1; i<n;++i) -     { toa+=b; +         if(A>maxd | |!d[a])return false; -     } the     return true; * } $ Panax Notoginseng voidQsortintLintR//Note that the L and the global l coexist, there will be no Error ~ - { the     if(L&GT;=R)return; +     inti=l,j=R; AResult x=a[(l+r) >>1]; the      while(1) +     { -          while(a[i]<x) + +i; $          while(X<a[j])--J; $         if(I&GT;J) Break; - swap (a[i],a[j]); -++i; --J; the     } - qsort (L,J); qsort (i,r);Wuyi } the  - intMain () Wu { -Ifstreaminch("ariprog.in"); AboutOfstream out("Ariprog.out"); $  -     inch>>N>>M; -  -      for(intp=0;p <=m;++p) A          for(intQ=P;Q&LT;=M;++Q) d[p*p+q*q]=true; +  the      for(intI=0; i<=maxd;++i) -         if(D[i]) num[++l]=i; $  the      for(intI=1; i<=l;++i) the     { the         intA=Num[i]; the          for(intj=i+1; j<=l;++j) -         { in             intb=num[j]-A; the             if(ok (b)) the             { About++l; A[l].a=a; a[l].b=b; the                 //cout<< "a= (" <<a<< "," <<b<< ")" <<endl; cin.get (); the             } the         } +     } -  theQsort1, L);Bayi  the     if(!l) out<<"NONE"<<Endl; the     Else  for(intI=1; i<=l;++i) out<<A[i].a<<" "<<A[i].b<<Endl; -  -     inch. Close (); the      out. Close (); the     return 0; the}

Usaco Section1.4 Arithmetic progressions Problem solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.