bzoj 1041 圆上的整点 分类: Brush Mode 2014-11-11 20:15 80人阅读 评论(0) 收藏

这里先只考虑x,y都大于0的情况

如果x^2+y^2=r^2,则(r-x)(r+x)=y*y

令d=gcd(r-x,r+x),r-x=d*u^2,r+x=d*v^2,显然有gcd(u,v)=1且u<v

有2r=d*(u^2+v^2),y=d*u*v,x=d(v^2-u^2)/2

枚举2r的约数d,再花费sqrt(2r/d)的时间枚举u,求出v=sqrt(2r/d-u^2)然后判断gcd(u,v)=1

最后结果乘以4(四个象限)+4(坐标轴上)即可

/**************************************************************
    Problem: 1041
    User: ******
    Language: C++
    Result: Accepted
    Time:92 ms
    Memory:1284 kb
****************************************************************/
 
#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdlib.h>
#include <cassert>
 
using namespace std;
 
int main() {
    long long r;
    cin >> r;
    long long d, a, b;
    long long ans = 0;
    for (d = 1; d * d <= 2*r; ++d) {
        if ((2 * r) % d) continue;
        long long t = (2 * r) / d;
        for (a = 1; a * a <= t; ++a) {
            b = sqrt(t - a * a);
            if (b * b != t - a * a) continue;
            if (a >= b) continue;
            if (__gcd(a, b) > 1) continue;
            // cout << d << " " << a << " " << b << endl;
            // cout << "X: " << - a * a * d + r << endl;
            ++ans;
        }
        if (d * d == 2 * r) continue;
        t = d;      
        for (a = 1; a * a <= t; ++a) {
            b = sqrt(t - a * a);
            if (b * b != t - a * a) continue;
            if (a >= b) continue;
            if (__gcd(a, b) > 1) continue;
            // cout << t << " " << a << " " << b << endl;
            // cout << "X: " << - a * a * (2*r/d) + r << endl;
            ++ans;
        }
    }
    ans = (ans + 1) * 4;
    cout << ans << endl;
    return 0;
}
转摘至:http://blog.csdn.net/lwfcgz/article/details/39927801

posted @ 2014-11-11 20:15  天I火  阅读(48)  评论(0编辑  收藏