題目鏈接:uva 1549 - Lattice Point
題目大意:給定圓半徑,以原點為圓心,求園內有多少個整數點。
解題思路:首先坐標軸將圓分成4份,所以只要單獨考慮每一塊的個數乘4再加1即可(原點)
#include
#include
#include
#include
using namespace std;
const double pi = 4 * atan(1.0);
typedef long long ll;
ll solve (ll R) {
ll ret = 0;
ll r = R, M = R * R;
for (ll i = 0; i <= R; i++) {
while (r * r + i * i > M)
r--;
ret += r;
}
return 4 * ret + 1;
}
int main () {
ll n;
while (scanf("%lld", &n) == 1) {
printf("%lld\n%lld\n", n, solve(n));
}
return 0;
}