POJ - 2100 - Graveyard Design(尺取)
题目链接:https://vjudge.net/problem/POJ-2100#author=DlPF
题目大意:给出一个数n。你要求一段连续的数,这些数的平方和等于n。
直接尺取,因为n最大1e14,所以尺取到1e7肯定出结果
#include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<stdio.h> #include<cctype> #include<string> #include<vector> #include<climits> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define endl '\n' #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mst(a) memset(a, 0, sizeof(a)) #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n") using namespace std; typedef long long ll; typedef pair<int, int> P; const double pi = acos(-1.0); const double eps = 1e-7; const int INF = 0x3f3f3f3f; const int _NAN = -0x3f3f3f3f; const int maxn = 1e7+10; int* ans = new int[maxn]; int head[maxn/10]; int main(void) { ll num; scanf("%lld", &num); ll l = 1, r = 1, sum = 0, times = INF, cnt = 0, kase = 0; while(times--) { while(sum < num && r*r <= num) { sum += r*r; ++r; } if (sum < num) break; if (sum == num) { head[cnt++] = r-l; for (int i = l; i<r; ++i) ans[kase++] = i; } sum -= l*l; sum = max(sum, 0); ++l; } int len = 0; printf("%lld\n", cnt); for (int i = 0; i<cnt; ++i) { printf("%d ", head[i]); len += head[i]; for (int j = len-head[i]; j<len; ++j) printf(j == len-1 ? "%d\n" : "%d ", ans[j]); } return 0; }