Guest User

Untitled

a guest
Jul 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. /*
  2. * ID: eckanka1
  3. * PROG: ariprog
  4. * LANG: C++
  5. */
  6.  
  7. #include <fstream>
  8. #include <vector>
  9. #include <utility>
  10. #include <cstring>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. int main() {
  16. ifstream fin("ariprog.in");
  17. ofstream fout("ariprog.out");
  18.  
  19. int n, m;
  20. fin >> n >> m;
  21.  
  22. // Our sum of two squares cannot be larger than this.
  23. int biLimit = 2 * m * m;
  24.  
  25. // Build table of squares
  26. bool bsq[biLimit + 1];
  27. memset(bsq, 0, sizeof(bsq));
  28. for (int i = 0; i <= m; i++) {
  29. for (int j = 0; j <= i; j++) {
  30. bsq[i*i + j*j] = true;
  31. }
  32. }
  33.  
  34. // Store progressions as a tuple (diff, start)
  35. vector< pair<int, int> > progs;
  36.  
  37. for (int i = 0; i < biLimit; i++) {
  38. if (!bsq[i]) continue;
  39.  
  40. // Only check the cases that're within the bounds of our
  41. // largest sum of squares.
  42. int maxDiff = (biLimit - i) / (n-1);
  43.  
  44. // Check sequences; add to progs if successful.
  45. for (int diff = 1; diff <= maxDiff; diff++) {
  46. bool seqWorks = true;
  47. for (int b = 1; seqWorks && b < n; b++) {
  48. seqWorks = bsq[i + b*diff];
  49. }
  50. if (seqWorks) {
  51. progs.push_back( make_pair( diff, i ) );
  52. }
  53. }
  54. }
  55.  
  56. // Sorted lexicographically, our vector of progressions
  57. // gives us the output in the correct order
  58. sort(progs.begin(), progs.end());
  59.  
  60. for (int i = 0; i < progs.size(); i++) {
  61. pair<int, int> prog = progs[i];
  62. fout << prog.second << " " << prog.first << endl;
  63. }
  64. }
Add Comment
Please, Sign In to add comment