1041: [HAOI2008]圆上的整点
Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4319 Solved: 1955 [Submit][Status][Discuss]Description
求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。
Input
只有一个正整数n,n<=2000 000 000
Output
整点个数
Sample Input
4
Sample Output
4
HINT
太神了,看这个
#include <cmath> #include <iostream> using namespace std; typedef long long ll; ll Gcd(ll x, ll y){ ll t; if(x > y){ t = x; x = y; y = t; } while(x){ t = x; x = y % x; y = t; } return y; } inline bool Judge(ll a, ll b){ double t = sqrt(b); if(t != floor(t)) return false; if(Gcd(a, b) == 1 && a != b) return true; return false; } int main(){ ll r, ans = 0; cin >> r; for(ll d = 1; d * d <= r * 2; d++) if(2 * r % d == 0){ for(ll i = 1; i * i * d <= r; i++) if(Judge(i * i, 2 * r / d - i * i)) ans++; if(d * d != 2 * r) for(ll i = 1; i * i * 2 <= d; i++) if(Judge(i * i, d - i * i)) ans++; } cout << (ans + 1) * 4 << endl; return 0; }