hdu 1868

简单的找规律题,假设连续序列为a, a+1, a+2,...,a+k则(2*a+k)*(k+1)/2=N,从而a可以用N和k表示出来,而通过这个公式能够发现k一定小于sqrt(2*N+1),枚举k,计算出a判断是否符合题意即可。

/*
* hdu1868/win.cpp
* Created on: 2011-10-8
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int N, end, itmp;
double dtmp;
int ans;
while (scanf("%d", &N) == 1) {
ans = 0;
end = (int) sqrt(2 * N + 1.0);
for (int k = 1; k < end; k++) {
dtmp = (N - k * k / 2.0 - k / 2.0) / (k + 1.0);
itmp = (int) dtmp;
if (itmp == dtmp && itmp >= 1 && itmp < N) {
ans++;
}
}
printf("%d\n", ans);
}
return 0;
}

posted @ 2011-10-08 14:27  moonbay  阅读(213)  评论(0编辑  收藏  举报