Skip to content

Instantly share code, notes, and snippets.

@potetisensei
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potetisensei/d1a51a77d17a133effe1 to your computer and use it in GitHub Desktop.
Save potetisensei/d1a51a77d17a133effe1 to your computer and use it in GitHub Desktop.
POJ 2100
#include <cstdio>
#include <cmath>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int, int> Pair;
int maxt;
int s;
int t;
int idx;
long long int n;
long long int sum;
Pair ans[500000];
bool cmp(const Pair &a, const Pair &b) {
return a.second - a.first > b.second - b.first;
}
int main() {
scanf("%lld", &n);
maxt = (int)ceil(sqrt((double)n));
s = 1;
t = 0;
sum = 0;
while (t <= maxt) {
while (sum <= n) {
++t;
sum += (long long int)t*(long long int)t;
if (sum == n) {
ans[idx++] = Pair(s, t);
}
}
while (sum > n) {
sum -= (long long int)s*(long long int)s;
s++;
if (sum == n) {
ans[idx++] = Pair(s, t);
}
}
}
sort(ans, ans+idx, cmp);
printf("%d\n", idx);
for (int j=0; j<idx; j++) {
int s = ans[j].first;
int t = ans[j].second;
printf("%d", t-s+1);
for (int i=s; i<=t; i++) {
printf(" %d", i);
}
puts("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment