设为首页 加入收藏

TOP

BZOJ1041 圆上的整点 Solution
2015-07-20 17:33:53 来源: 作者: 【 】 浏览:3
Tags:BZOJ1041 整点 Solution

题意:给定r,求x^2+y^2=r^2的图象上存在多少个整点。


Sol:问题显然可以转化为x^2+y^2=r^2有多少个正整数解。我们考虑如何快速的解出这个方程。

引入本源勾股数组(x,y,z)(x,y,z为正整数),满足x^2+y^2=z^2且gcd(x,y,z)=1.

我们能够证明一些性质,z为奇数,x,y一奇一偶,不妨设x为奇数,y为偶数,则有z-x为完全平方数的二倍,z-y为完全平方数。

有兴趣的可以自己证一下,当做结论记住也行。

那么我们枚举最大公约数,然后计算对应的本源勾股数组数目。

具体怎么计算看代码,枚举量很小。

最终算出的答案只是1/8个象限,于是*8+4得到最终答案。

Code:

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         using namespace std; typedef long long LL; inline int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); } inline int isint(long long x) { int tmp = (int)sqrt(x); if ((LL)tmp * tmp != x) return -1; return tmp; } inline int Calc(int z) { if ((z & 1) == 0) return 0; if (z < 5) return 0; int res = 0; int x, y; for(int i = 1; i * i < z; i += 2) { x = z - i * i; y = isint((LL)z * z - (LL)x * x); if (y == -1 || gcd(z, gcd(x, y)) != 1) continue; ++res; } return res; } int main() { int x; scanf("%d", &x); if (!x) { puts("1"); return 0; } long long res = 0; for(int i = 1; i * i <= x; ++i) { if (x % i == 0) { res += Calc(x / i); if (i != x / i) res += Calc(i); } } printf("%lld", (res << 3) + 4); return 0; }
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 3635 Dragon Balls(带权并查.. 下一篇BZOJ3262 陌上花开 Solution

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)